Commit | Line | Data |
---|---|---|
2fe58dfd SE |
1 | /* UDP send/receive module for secnet */ |
2 | ||
3 | /* This module enables sites to communicate by sending UDP | |
4 | * packets. When an instance of the module is created we can | |
5 | * optionally bind to a particular local IP address (not implemented | |
6 | * yet). | |
7 | * | |
2fe58dfd SE |
8 | * Packets are offered to registered receivers in turn. Once one |
9 | * accepts it, it isn't offered to any more. */ | |
10 | ||
8689b3a9 | 11 | #include "secnet.h" |
2fe58dfd SE |
12 | #include <stdio.h> |
13 | #include <unistd.h> | |
14 | #include <fcntl.h> | |
15 | #include <string.h> | |
16 | #include <errno.h> | |
2fe58dfd | 17 | #include <sys/socket.h> |
b2a56f7c | 18 | #include <sys/wait.h> |
5edf478f IJ |
19 | #include <netinet/in.h> |
20 | #include <arpa/inet.h> | |
2fe58dfd | 21 | #include "util.h" |
794f2398 | 22 | #include "unaligned.h" |
ff05a229 | 23 | #include "ipaddr.h" |
136740e6 | 24 | #include "magic.h" |
2fe58dfd SE |
25 | |
26 | static beforepoll_fn udp_beforepoll; | |
27 | static afterpoll_fn udp_afterpoll; | |
28 | static comm_request_notify_fn request_notify; | |
29 | static comm_release_notify_fn release_notify; | |
30 | static comm_sendmsg_fn udp_sendmsg; | |
31 | ||
2fe58dfd SE |
32 | struct notify_list { |
33 | comm_notify_fn *fn; | |
34 | void *state; | |
35 | struct notify_list *next; | |
36 | }; | |
37 | ||
38 | struct udp { | |
39 | closure_t cl; | |
40 | struct comm_if ops; | |
41 | struct cloc loc; | |
3b83c932 | 42 | uint32_t addr; |
baa06aeb | 43 | uint16_t port; |
2fe58dfd | 44 | int fd; |
b2a56f7c | 45 | string_t authbind; |
2fe58dfd SE |
46 | struct buffer_if *rbuf; |
47 | struct notify_list *notify; | |
ff05a229 SE |
48 | bool_t use_proxy; |
49 | struct sockaddr_in proxy; | |
2fe58dfd SE |
50 | }; |
51 | ||
5edf478f IJ |
52 | static const char *saddr_to_string(const struct sockaddr_in *sin) { |
53 | static char bufs[2][100]; | |
54 | static int b; | |
55 | ||
56 | b ^= 1; | |
57 | snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d", | |
58 | inet_ntoa(sin->sin_addr), | |
59 | ntohs(sin->sin_port)); | |
60 | return bufs[b]; | |
61 | } | |
62 | ||
63 | static const char *addr_to_string(void *commst, const struct comm_addr *ca) { | |
64 | struct udp *st=commst; | |
65 | static char sbuf[100]; | |
66 | ||
67 | struct sockaddr_in la; | |
68 | la.sin_addr.s_addr=htonl(st->addr); | |
69 | la.sin_port=htons(st->port); | |
70 | ||
71 | snprintf(sbuf, sizeof(sbuf), "udp:%s-%s", | |
72 | saddr_to_string(&la), saddr_to_string(&ca->sin)); | |
73 | return sbuf; | |
74 | } | |
75 | ||
2fe58dfd | 76 | static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io, |
90a39563 | 77 | int *timeout_io) |
2fe58dfd SE |
78 | { |
79 | struct udp *st=state; | |
80 | if (*nfds_io<1) { | |
81 | *nfds_io=1; | |
82 | return ERANGE; | |
83 | } | |
84 | *nfds_io=1; | |
85 | fds->fd=st->fd; | |
86 | fds->events=POLLIN; | |
87 | return 0; | |
88 | } | |
89 | ||
90a39563 | 90 | static void udp_afterpoll(void *state, struct pollfd *fds, int nfds) |
2fe58dfd SE |
91 | { |
92 | struct udp *st=state; | |
93 | struct sockaddr_in from; | |
b1a0f651 | 94 | socklen_t fromlen; |
2fe58dfd SE |
95 | struct notify_list *n; |
96 | bool_t done; | |
97 | int rv; | |
98 | ||
99 | if (nfds && (fds->revents & POLLIN)) { | |
100 | do { | |
a15faeb2 | 101 | FILLZERO(from); |
2fe58dfd SE |
102 | fromlen=sizeof(from); |
103 | BUF_ASSERT_FREE(st->rbuf); | |
104 | BUF_ALLOC(st->rbuf,"udp_afterpoll"); | |
6af9a984 | 105 | buffer_init(st->rbuf,calculate_max_start_pad()); |
2fe58dfd SE |
106 | rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0, |
107 | (struct sockaddr *)&from, &fromlen); | |
108 | if (rv>0) { | |
109 | st->rbuf->size=rv; | |
ff05a229 SE |
110 | if (st->use_proxy) { |
111 | /* Check that the packet came from our poxy server; | |
112 | we shouldn't be contacted directly by anybody else | |
113 | (since they can trivially forge source addresses) */ | |
114 | if (memcmp(&from.sin_addr,&st->proxy.sin_addr,4)!=0 || | |
115 | memcmp(&from.sin_port,&st->proxy.sin_port,2)!=0) { | |
116 | Message(M_INFO,"udp: received packet that's not " | |
117 | "from the proxy\n"); | |
118 | BUF_FREE(st->rbuf); | |
119 | continue; | |
120 | } | |
121 | memcpy(&from.sin_addr,buf_unprepend(st->rbuf,4),4); | |
122 | buf_unprepend(st->rbuf,2); | |
123 | memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2); | |
124 | } | |
8534d602 IJ |
125 | struct comm_addr ca; |
126 | FILLZERO(ca); | |
127 | ca.comm=&st->ops; | |
128 | ca.sin=from; | |
2fe58dfd SE |
129 | done=False; |
130 | for (n=st->notify; n; n=n->next) { | |
a15faeb2 | 131 | if (n->fn(n->state, st->rbuf, &ca)) { |
2fe58dfd SE |
132 | done=True; |
133 | break; | |
134 | } | |
135 | } | |
136 | if (!done) { | |
bf28fc73 IJ |
137 | uint32_t msgtype; |
138 | if (st->rbuf->size>12 /* prevents traffic amplification */ | |
139 | && ((msgtype=get_uint32(st->rbuf->start+8)) | |
140 | != LABEL_NAK)) { | |
141 | uint32_t source,dest; | |
142 | /* Manufacture and send NAK packet */ | |
143 | source=get_uint32(st->rbuf->start); /* Us */ | |
144 | dest=get_uint32(st->rbuf->start+4); /* Them */ | |
8534d602 | 145 | send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted"); |
bf28fc73 | 146 | } |
2fe58dfd SE |
147 | BUF_FREE(st->rbuf); |
148 | } | |
149 | BUF_ASSERT_FREE(st->rbuf); | |
150 | } else { | |
151 | BUF_FREE(st->rbuf); | |
152 | } | |
153 | } while (rv>=0); | |
154 | } | |
155 | } | |
156 | ||
157 | static void request_notify(void *commst, void *nst, comm_notify_fn *fn) | |
158 | { | |
159 | struct udp *st=commst; | |
160 | struct notify_list *n; | |
161 | ||
162 | n=safe_malloc(sizeof(*n),"request_notify"); | |
163 | n->fn=fn; | |
164 | n->state=nst; | |
165 | n->next=st->notify; | |
166 | st->notify=n; | |
167 | } | |
168 | ||
169 | static void release_notify(void *commst, void *nst, comm_notify_fn *fn) | |
170 | { | |
171 | struct udp *st=commst; | |
172 | struct notify_list *n, **p, *t; | |
173 | ||
174 | /* XXX untested */ | |
175 | p=&st->notify; | |
176 | for (n=st->notify; n; ) | |
177 | { | |
178 | if (n->state==nst && n->fn==fn) { | |
179 | t=n; | |
180 | *p=n->next; | |
181 | n=n->next; | |
182 | free(t); | |
183 | } else { | |
184 | p=&n->next; | |
185 | n=n->next; | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | static bool_t udp_sendmsg(void *commst, struct buffer_if *buf, | |
a15faeb2 | 191 | const struct comm_addr *dest) |
2fe58dfd SE |
192 | { |
193 | struct udp *st=commst; | |
ff05a229 | 194 | uint8_t *sa; |
2fe58dfd | 195 | |
ff05a229 | 196 | if (st->use_proxy) { |
3abd18e8 | 197 | sa=buf_prepend(buf,8); |
a15faeb2 | 198 | memcpy(sa,&dest->sin.sin_addr,4); |
ff05a229 | 199 | memset(sa+4,0,4); |
a15faeb2 | 200 | memcpy(sa+6,&dest->sin.sin_port,2); |
ff05a229 SE |
201 | sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy, |
202 | sizeof(st->proxy)); | |
3abd18e8 | 203 | buf_unprepend(buf,8); |
ff05a229 SE |
204 | } else { |
205 | sendto(st->fd, buf->start, buf->size, 0, | |
a15faeb2 | 206 | (struct sockaddr *)&dest->sin, sizeof(dest->sin)); |
ff05a229 | 207 | } |
2fe58dfd SE |
208 | |
209 | return True; | |
210 | } | |
211 | ||
baa06aeb SE |
212 | static void udp_phase_hook(void *sst, uint32_t new_phase) |
213 | { | |
214 | struct udp *st=sst; | |
215 | struct sockaddr_in addr; | |
216 | ||
3b83c932 | 217 | st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
baa06aeb SE |
218 | if (st->fd<0) { |
219 | fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line); | |
220 | } | |
221 | if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) { | |
222 | fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)", | |
223 | st->loc.file,st->loc.line); | |
224 | } | |
225 | if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) { | |
226 | fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)", | |
227 | st->loc.file,st->loc.line); | |
228 | } | |
229 | ||
076bb54e | 230 | FILLZERO(addr); |
baa06aeb | 231 | addr.sin_family=AF_INET; |
3b83c932 | 232 | addr.sin_addr.s_addr=htonl(st->addr); |
baa06aeb | 233 | addr.sin_port=htons(st->port); |
b2a56f7c SE |
234 | if (st->authbind) { |
235 | pid_t c; | |
236 | int status; | |
237 | ||
238 | /* XXX this fork() and waitpid() business needs to be hidden | |
239 | in some system-specific library functions. */ | |
240 | c=fork(); | |
241 | if (c==-1) { | |
242 | fatal_perror("udp_phase_hook: fork() for authbind"); | |
243 | } | |
244 | if (c==0) { | |
3b83c932 | 245 | char *argv[4], addrstr[9], portstr[5]; |
51b25953 IJ |
246 | sprintf(addrstr,"%08lX",(long)addr.sin_addr.s_addr); |
247 | sprintf(portstr,"%04X",addr.sin_port); | |
b2a56f7c | 248 | argv[0]=st->authbind; |
3b83c932 SE |
249 | argv[1]=addrstr; |
250 | argv[2]=portstr; | |
b2a56f7c SE |
251 | argv[3]=NULL; |
252 | dup2(st->fd,0); | |
253 | execvp(st->authbind,argv); | |
3b83c932 | 254 | _exit(255); |
b2a56f7c | 255 | } |
3b83c932 SE |
256 | while (waitpid(c,&status,0)==-1) { |
257 | if (errno==EINTR) continue; | |
b2a56f7c SE |
258 | fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line); |
259 | } | |
3b83c932 SE |
260 | if (WIFSIGNALED(status)) { |
261 | fatal("udp (%s:%d): authbind died on signal %d",st->loc.file, | |
262 | st->loc.line, WTERMSIG(status)); | |
263 | } | |
264 | if (WIFEXITED(status) && WEXITSTATUS(status)!=0) { | |
265 | fatal("udp (%s:%d): authbind died with status %d",st->loc.file, | |
266 | st->loc.line, WEXITSTATUS(status)); | |
267 | } | |
b2a56f7c SE |
268 | } else { |
269 | if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) { | |
270 | fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line); | |
271 | } | |
baa06aeb SE |
272 | } |
273 | ||
274 | register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp"); | |
275 | } | |
276 | ||
2fe58dfd SE |
277 | static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context, |
278 | list_t *args) | |
279 | { | |
280 | struct udp *st; | |
3b83c932 | 281 | item_t *i,*j; |
2fe58dfd | 282 | dict_t *d; |
ff05a229 SE |
283 | list_t *l; |
284 | uint32_t a; | |
2fe58dfd SE |
285 | |
286 | st=safe_malloc(sizeof(*st),"udp_apply(st)"); | |
287 | st->loc=loc; | |
288 | st->cl.description="udp"; | |
289 | st->cl.type=CL_COMM; | |
290 | st->cl.apply=NULL; | |
291 | st->cl.interface=&st->ops; | |
292 | st->ops.st=st; | |
293 | st->ops.request_notify=request_notify; | |
294 | st->ops.release_notify=release_notify; | |
295 | st->ops.sendmsg=udp_sendmsg; | |
5edf478f | 296 | st->ops.addr_to_string=addr_to_string; |
baa06aeb | 297 | st->port=0; |
ff05a229 | 298 | st->use_proxy=False; |
2fe58dfd SE |
299 | |
300 | i=list_elem(args,0); | |
301 | if (!i || i->type!=t_dict) { | |
302 | cfgfatal(st->loc,"udp","first argument must be a dictionary\n"); | |
303 | } | |
304 | d=i->data.dict; | |
305 | ||
3b83c932 | 306 | j=dict_find_item(d,"address",False,"udp",st->loc); |
44128667 | 307 | st->addr=j?string_item_to_ipaddr(j, "udp"):INADDR_ANY; |
baa06aeb | 308 | st->port=dict_read_number(d,"port",True,"udp",st->loc,0); |
2fe58dfd | 309 | st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc); |
b2a56f7c | 310 | st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc); |
ff05a229 SE |
311 | l=dict_lookup(d,"proxy"); |
312 | if (l) { | |
313 | st->use_proxy=True; | |
076bb54e | 314 | FILLZERO(st->proxy); |
ff05a229 SE |
315 | st->proxy.sin_family=AF_INET; |
316 | i=list_elem(l,0); | |
317 | if (!i || i->type!=t_string) { | |
318 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); | |
319 | } | |
320 | a=string_item_to_ipaddr(i,"proxy"); | |
321 | st->proxy.sin_addr.s_addr=htonl(a); | |
322 | i=list_elem(l,1); | |
323 | if (!i || i->type!=t_number) { | |
324 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); | |
325 | } | |
326 | st->proxy.sin_port=htons(i->data.number); | |
ff05a229 | 327 | } |
2fe58dfd | 328 | |
3abd18e8 IJ |
329 | update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0); |
330 | ||
baa06aeb | 331 | add_hook(PHASE_GETRESOURCES,udp_phase_hook,st); |
2fe58dfd SE |
332 | |
333 | return new_closure(&st->cl); | |
334 | } | |
335 | ||
2fe58dfd SE |
336 | void udp_module(dict_t *dict) |
337 | { | |
338 | add_closure(dict,"udp",udp_apply); | |
339 | } |