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" |
dd9209d1 | 22 | #include "magic.h" |
794f2398 | 23 | #include "unaligned.h" |
ff05a229 | 24 | #include "ipaddr.h" |
136740e6 | 25 | #include "magic.h" |
2fe58dfd SE |
26 | |
27 | static beforepoll_fn udp_beforepoll; | |
28 | static afterpoll_fn udp_afterpoll; | |
29 | static comm_request_notify_fn request_notify; | |
30 | static comm_release_notify_fn release_notify; | |
31 | static comm_sendmsg_fn udp_sendmsg; | |
32 | ||
2fe58dfd SE |
33 | struct notify_list { |
34 | comm_notify_fn *fn; | |
35 | void *state; | |
36 | struct notify_list *next; | |
37 | }; | |
38 | ||
08b62a6c IJ |
39 | #define MAX_SOCKETS 3 /* 2 ought to do really */ |
40 | ||
41 | struct udpsock { | |
42 | union iaddr addr; | |
43 | int fd; | |
44 | }; | |
45 | ||
2fe58dfd SE |
46 | struct udp { |
47 | closure_t cl; | |
48 | struct comm_if ops; | |
49 | struct cloc loc; | |
08b62a6c IJ |
50 | int n_socks; |
51 | struct udpsock socks[MAX_SOCKETS]; | |
b2a56f7c | 52 | string_t authbind; |
2fe58dfd SE |
53 | struct buffer_if *rbuf; |
54 | struct notify_list *notify; | |
ff05a229 | 55 | bool_t use_proxy; |
a32d56fb | 56 | union iaddr proxy; |
2fe58dfd SE |
57 | }; |
58 | ||
08b62a6c IJ |
59 | /* |
60 | * Re comm_addr.ix: This field allows us to note in the comm_addr | |
61 | * which socket an incoming packet was received on. This is required | |
62 | * for conveniently logging the actual source of a packet. But the ix | |
63 | * does not formally form part of the address: it is not used when | |
64 | * sending, nor when comparing two comm_addrs. | |
65 | * | |
66 | * The special value -1 means that the comm_addr was constructed by | |
67 | * another module in secnet (eg the resolver), rather than being a | |
68 | * description of the source of an incoming packet. | |
69 | */ | |
70 | ||
5edf478f IJ |
71 | static const char *addr_to_string(void *commst, const struct comm_addr *ca) { |
72 | struct udp *st=commst; | |
73 | static char sbuf[100]; | |
08b62a6c | 74 | int ix=ca->ix>=0 ? ca->ix : 0; |
5edf478f | 75 | |
08b62a6c IJ |
76 | assert(ix>=0 && ix<st->n_socks); |
77 | snprintf(sbuf, sizeof(sbuf), "udp:%s%s-%s", | |
78 | iaddr_to_string(&st->socks[ix].addr), | |
79 | ca->ix<0 ? "&" : "", | |
80 | iaddr_to_string(&ca->ia)); | |
5edf478f IJ |
81 | return sbuf; |
82 | } | |
83 | ||
2fe58dfd | 84 | static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io, |
90a39563 | 85 | int *timeout_io) |
2fe58dfd | 86 | { |
08b62a6c | 87 | int i; |
2fe58dfd | 88 | struct udp *st=state; |
08b62a6c IJ |
89 | if (*nfds_io<st->n_socks) { |
90 | *nfds_io=st->n_socks; | |
2fe58dfd SE |
91 | return ERANGE; |
92 | } | |
08b62a6c IJ |
93 | *nfds_io=st->n_socks; |
94 | for (i=0; i<st->n_socks; i++) { | |
95 | fds[i].fd=st->socks[i].fd; | |
96 | fds[i].events=POLLIN; | |
97 | } | |
2fe58dfd SE |
98 | return 0; |
99 | } | |
100 | ||
90a39563 | 101 | static void udp_afterpoll(void *state, struct pollfd *fds, int nfds) |
2fe58dfd SE |
102 | { |
103 | struct udp *st=state; | |
a32d56fb | 104 | union iaddr from; |
b1a0f651 | 105 | socklen_t fromlen; |
2fe58dfd SE |
106 | struct notify_list *n; |
107 | bool_t done; | |
108 | int rv; | |
08b62a6c | 109 | int i; |
2fe58dfd | 110 | |
08b62a6c IJ |
111 | for (i=0; i<st->n_socks; i++) { |
112 | if (i>=nfds) continue; | |
113 | if (!(fds[i].revents & POLLIN)) continue; | |
114 | assert(fds[i].fd == st->socks[i].fd); | |
115 | int fd=st->socks[i].fd; | |
2fe58dfd SE |
116 | do { |
117 | fromlen=sizeof(from); | |
118 | BUF_ASSERT_FREE(st->rbuf); | |
119 | BUF_ALLOC(st->rbuf,"udp_afterpoll"); | |
6af9a984 | 120 | buffer_init(st->rbuf,calculate_max_start_pad()); |
08b62a6c | 121 | rv=recvfrom(fd, st->rbuf->start, |
92795040 | 122 | buf_remaining_space(st->rbuf), |
a32d56fb | 123 | 0, &from.sa, &fromlen); |
2fe58dfd SE |
124 | if (rv>0) { |
125 | st->rbuf->size=rv; | |
ff05a229 SE |
126 | if (st->use_proxy) { |
127 | /* Check that the packet came from our poxy server; | |
128 | we shouldn't be contacted directly by anybody else | |
129 | (since they can trivially forge source addresses) */ | |
a32d56fb | 130 | if (!iaddr_equal(&from,&st->proxy)) { |
ff05a229 SE |
131 | Message(M_INFO,"udp: received packet that's not " |
132 | "from the proxy\n"); | |
133 | BUF_FREE(st->rbuf); | |
134 | continue; | |
135 | } | |
a32d56fb IJ |
136 | /* proxy protocol supports ipv4 transport only */ |
137 | from.sa.sa_family=AF_INET; | |
138 | memcpy(&from.sin.sin_addr,buf_unprepend(st->rbuf,4),4); | |
ff05a229 | 139 | buf_unprepend(st->rbuf,2); |
a32d56fb | 140 | memcpy(&from.sin.sin_port,buf_unprepend(st->rbuf,2),2); |
ff05a229 | 141 | } |
8534d602 | 142 | struct comm_addr ca; |
8534d602 | 143 | ca.comm=&st->ops; |
a32d56fb | 144 | ca.ia=from; |
08b62a6c | 145 | ca.ix=i; |
2fe58dfd SE |
146 | done=False; |
147 | for (n=st->notify; n; n=n->next) { | |
a15faeb2 | 148 | if (n->fn(n->state, st->rbuf, &ca)) { |
2fe58dfd SE |
149 | done=True; |
150 | break; | |
151 | } | |
152 | } | |
153 | if (!done) { | |
bf28fc73 IJ |
154 | uint32_t msgtype; |
155 | if (st->rbuf->size>12 /* prevents traffic amplification */ | |
156 | && ((msgtype=get_uint32(st->rbuf->start+8)) | |
157 | != LABEL_NAK)) { | |
158 | uint32_t source,dest; | |
159 | /* Manufacture and send NAK packet */ | |
160 | source=get_uint32(st->rbuf->start); /* Us */ | |
161 | dest=get_uint32(st->rbuf->start+4); /* Them */ | |
8534d602 | 162 | send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted"); |
bf28fc73 | 163 | } |
2fe58dfd SE |
164 | BUF_FREE(st->rbuf); |
165 | } | |
166 | BUF_ASSERT_FREE(st->rbuf); | |
167 | } else { | |
168 | BUF_FREE(st->rbuf); | |
169 | } | |
170 | } while (rv>=0); | |
171 | } | |
172 | } | |
173 | ||
174 | static void request_notify(void *commst, void *nst, comm_notify_fn *fn) | |
175 | { | |
176 | struct udp *st=commst; | |
177 | struct notify_list *n; | |
178 | ||
179 | n=safe_malloc(sizeof(*n),"request_notify"); | |
180 | n->fn=fn; | |
181 | n->state=nst; | |
182 | n->next=st->notify; | |
183 | st->notify=n; | |
184 | } | |
185 | ||
186 | static void release_notify(void *commst, void *nst, comm_notify_fn *fn) | |
187 | { | |
188 | struct udp *st=commst; | |
189 | struct notify_list *n, **p, *t; | |
190 | ||
191 | /* XXX untested */ | |
192 | p=&st->notify; | |
193 | for (n=st->notify; n; ) | |
194 | { | |
195 | if (n->state==nst && n->fn==fn) { | |
196 | t=n; | |
197 | *p=n->next; | |
198 | n=n->next; | |
199 | free(t); | |
200 | } else { | |
201 | p=&n->next; | |
202 | n=n->next; | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | static bool_t udp_sendmsg(void *commst, struct buffer_if *buf, | |
a15faeb2 | 208 | const struct comm_addr *dest) |
2fe58dfd SE |
209 | { |
210 | struct udp *st=commst; | |
ff05a229 | 211 | uint8_t *sa; |
2fe58dfd | 212 | |
ff05a229 | 213 | if (st->use_proxy) { |
3abd18e8 | 214 | sa=buf_prepend(buf,8); |
a32d56fb IJ |
215 | if (dest->ia.sa.sa_family != AF_INET) { |
216 | Message(M_INFO, | |
217 | "udp: proxy means dropping outgoing non-IPv4 packet to %s\n", | |
218 | iaddr_to_string(&dest->ia)); | |
219 | return False; | |
220 | } | |
221 | memcpy(sa,&dest->ia.sin.sin_addr,4); | |
ff05a229 | 222 | memset(sa+4,0,4); |
a32d56fb | 223 | memcpy(sa+6,&dest->ia.sin.sin_port,2); |
08b62a6c | 224 | sendto(st->socks[0].fd,sa,buf->size+8,0,&st->proxy.sa, |
a32d56fb | 225 | iaddr_socklen(&st->proxy)); |
3abd18e8 | 226 | buf_unprepend(buf,8); |
ff05a229 | 227 | } else { |
08b62a6c IJ |
228 | int i,r; |
229 | bool_t allunsupported=True; | |
230 | for (i=0; i<st->n_socks; i++) { | |
231 | if (dest->ia.sa.sa_family != st->socks[i].addr.sa.sa_family) | |
232 | /* no point even trying */ | |
233 | continue; | |
234 | r=sendto(st->socks[i].fd, buf->start, buf->size, 0, | |
235 | &dest->ia.sa, iaddr_socklen(&dest->ia)); | |
236 | if (r>=0) return True; | |
237 | if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH)) | |
238 | /* who knows what that error means? */ | |
239 | allunsupported=False; | |
240 | } | |
241 | return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */ | |
ff05a229 | 242 | } |
2fe58dfd SE |
243 | |
244 | return True; | |
245 | } | |
246 | ||
08b62a6c | 247 | static void udp_make_socket(struct udp *st, struct udpsock *us) |
baa06aeb | 248 | { |
f164f167 | 249 | const union iaddr *addr=&us->addr; |
08b62a6c | 250 | us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); |
f164f167 | 251 | if (us->fd<0) { |
baa06aeb SE |
252 | fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line); |
253 | } | |
f164f167 | 254 | if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) { |
baa06aeb SE |
255 | fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)", |
256 | st->loc.file,st->loc.line); | |
257 | } | |
f164f167 | 258 | setcloexec(us->fd); |
08b62a6c IJ |
259 | #ifdef CONFIG_IPV6 |
260 | if (addr->sa.sa_family==AF_INET6) { | |
261 | int r; | |
262 | int optval=1; | |
263 | socklen_t optlen=sizeof(optval); | |
264 | r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen); | |
265 | if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)", | |
266 | st->loc.file,st->loc.line); | |
267 | } | |
268 | #endif | |
baa06aeb | 269 | |
b2a56f7c SE |
270 | if (st->authbind) { |
271 | pid_t c; | |
272 | int status; | |
273 | ||
274 | /* XXX this fork() and waitpid() business needs to be hidden | |
275 | in some system-specific library functions. */ | |
276 | c=fork(); | |
277 | if (c==-1) { | |
278 | fatal_perror("udp_phase_hook: fork() for authbind"); | |
279 | } | |
280 | if (c==0) { | |
3ff31eda IJ |
281 | char *argv[5], addrstr[33], portstr[5]; |
282 | const char *addrfam; | |
283 | int port; | |
f164f167 | 284 | switch (addr->sa.sa_family) { |
a32d56fb | 285 | case AF_INET: |
f164f167 | 286 | sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr); |
3ff31eda IJ |
287 | port=addr->sin.sin_port; |
288 | addrfam=NULL; | |
a32d56fb | 289 | break; |
3ff31eda IJ |
290 | #ifdef CONFIG_IPV6 |
291 | case AF_INET6: { | |
292 | int i; | |
293 | for (i=0; i<16; i++) | |
294 | sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]); | |
295 | port=addr->sin6.sin6_port; | |
296 | addrfam="6"; | |
297 | break; | |
298 | } | |
299 | #endif /*CONFIG_IPV6*/ | |
a32d56fb IJ |
300 | default: |
301 | fatal("udp (%s:%d): unsupported address family for authbind", | |
302 | st->loc.file,st->loc.line); | |
303 | } | |
3ff31eda | 304 | sprintf(portstr,"%04X",port); |
b2a56f7c | 305 | argv[0]=st->authbind; |
3b83c932 SE |
306 | argv[1]=addrstr; |
307 | argv[2]=portstr; | |
3ff31eda IJ |
308 | argv[3]=(char*)addrfam; |
309 | argv[4]=NULL; | |
f164f167 | 310 | dup2(us->fd,0); |
b2a56f7c | 311 | execvp(st->authbind,argv); |
3b83c932 | 312 | _exit(255); |
b2a56f7c | 313 | } |
3b83c932 SE |
314 | while (waitpid(c,&status,0)==-1) { |
315 | if (errno==EINTR) continue; | |
b2a56f7c SE |
316 | fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line); |
317 | } | |
3b83c932 SE |
318 | if (WIFSIGNALED(status)) { |
319 | fatal("udp (%s:%d): authbind died on signal %d",st->loc.file, | |
320 | st->loc.line, WTERMSIG(status)); | |
321 | } | |
322 | if (WIFEXITED(status) && WEXITSTATUS(status)!=0) { | |
323 | fatal("udp (%s:%d): authbind died with status %d",st->loc.file, | |
324 | st->loc.line, WEXITSTATUS(status)); | |
325 | } | |
b2a56f7c | 326 | } else { |
08b62a6c | 327 | if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) { |
b2a56f7c SE |
328 | fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line); |
329 | } | |
baa06aeb | 330 | } |
f164f167 | 331 | } |
baa06aeb | 332 | |
f164f167 IJ |
333 | static void udp_phase_hook(void *sst, uint32_t new_phase) |
334 | { | |
335 | struct udp *st=sst; | |
08b62a6c IJ |
336 | int i; |
337 | for (i=0; i<st->n_socks; i++) | |
338 | udp_make_socket(st,&st->socks[i]); | |
339 | ||
340 | register_for_poll(st,udp_beforepoll,udp_afterpoll,MAX_SOCKETS,"udp"); | |
baa06aeb SE |
341 | } |
342 | ||
2fe58dfd SE |
343 | static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context, |
344 | list_t *args) | |
345 | { | |
346 | struct udp *st; | |
c649eafe | 347 | item_t *item; |
08b62a6c | 348 | list_t *caddrl; |
2fe58dfd | 349 | dict_t *d; |
ff05a229 SE |
350 | list_t *l; |
351 | uint32_t a; | |
08b62a6c | 352 | int i; |
2fe58dfd SE |
353 | |
354 | st=safe_malloc(sizeof(*st),"udp_apply(st)"); | |
355 | st->loc=loc; | |
356 | st->cl.description="udp"; | |
357 | st->cl.type=CL_COMM; | |
358 | st->cl.apply=NULL; | |
359 | st->cl.interface=&st->ops; | |
360 | st->ops.st=st; | |
361 | st->ops.request_notify=request_notify; | |
362 | st->ops.release_notify=release_notify; | |
363 | st->ops.sendmsg=udp_sendmsg; | |
5edf478f | 364 | st->ops.addr_to_string=addr_to_string; |
ff05a229 | 365 | st->use_proxy=False; |
2fe58dfd | 366 | |
c649eafe IJ |
367 | item=list_elem(args,0); |
368 | if (!item || item->type!=t_dict) { | |
2fe58dfd SE |
369 | cfgfatal(st->loc,"udp","first argument must be a dictionary\n"); |
370 | } | |
c649eafe | 371 | d=item->data.dict; |
2fe58dfd | 372 | |
08b62a6c IJ |
373 | int port=dict_read_number(d,"port",True,"udp",st->loc,0); |
374 | ||
375 | union iaddr defaultaddrs[] = { | |
376 | #ifdef CONFIG_IPV6 | |
377 | { .sin6 = { .sin6_family=AF_INET6, | |
378 | .sin6_port=htons(port), | |
379 | .sin6_addr=IN6ADDR_ANY_INIT } }, | |
380 | #endif | |
381 | { .sin = { .sin_family=AF_INET, | |
382 | .sin_port=htons(port), | |
383 | .sin_addr= { .s_addr=INADDR_ANY } } } | |
384 | }; | |
385 | ||
386 | caddrl=dict_lookup(d,"address"); | |
387 | st->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs); | |
388 | if (st->n_socks<=0 || st->n_socks>MAX_SOCKETS) | |
389 | cfgfatal(st->loc,"udp","`address' must be 1..%d addresses", | |
390 | MAX_SOCKETS); | |
391 | ||
392 | for (i=0; i<st->n_socks; i++) { | |
393 | struct udpsock *us=&st->socks[i]; | |
394 | if (!list_length(caddrl)) { | |
395 | us->addr=defaultaddrs[i]; | |
396 | } else { | |
397 | string_item_to_iaddr(list_elem(caddrl,i),port,&us->addr,"udp"); | |
398 | } | |
399 | us->fd=-1; | |
400 | } | |
401 | ||
2fe58dfd | 402 | st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc); |
b2a56f7c | 403 | st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc); |
ff05a229 SE |
404 | l=dict_lookup(d,"proxy"); |
405 | if (l) { | |
406 | st->use_proxy=True; | |
a32d56fb | 407 | st->proxy.sa.sa_family=AF_INET; |
c649eafe IJ |
408 | item=list_elem(l,0); |
409 | if (!item || item->type!=t_string) { | |
ff05a229 SE |
410 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); |
411 | } | |
c649eafe | 412 | a=string_item_to_ipaddr(item,"proxy"); |
a32d56fb | 413 | st->proxy.sin.sin_addr.s_addr=htonl(a); |
c649eafe IJ |
414 | item=list_elem(l,1); |
415 | if (!item || item->type!=t_number) { | |
ff05a229 SE |
416 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); |
417 | } | |
c649eafe | 418 | st->proxy.sin.sin_port=htons(item->data.number); |
ff05a229 | 419 | } |
2fe58dfd | 420 | |
3abd18e8 IJ |
421 | update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0); |
422 | ||
baa06aeb | 423 | add_hook(PHASE_GETRESOURCES,udp_phase_hook,st); |
2fe58dfd SE |
424 | |
425 | return new_closure(&st->cl); | |
426 | } | |
427 | ||
2fe58dfd SE |
428 | void udp_module(dict_t *dict) |
429 | { | |
430 | add_closure(dict,"udp",udp_apply); | |
431 | } |