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