udp: Print `&' in address descriptions only if multiple sockets
[secnet] / udp.c
CommitLineData
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
28static comm_sendmsg_fn udp_sendmsg;
29
2fe58dfd 30struct 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
47static 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),
bfc34aff 57 ca->ix<0 && socks->n_socks>1 ? "&" : "",
08b62a6c 58 iaddr_to_string(&ca->ia));
5edf478f
IJ
59 return sbuf;
60}
61
f7af3192
IJ
62static 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
2d7478f7
IJ
75const char *af_name(int af)
76{
77 switch (af) {
78 case AF_INET6: return "IPv6";
79 case AF_INET: return "IPv4";
80 case 0: return "(any)";
81 default: abort();
82 }
83}
84
85void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
c72d679d 86 struct udpsocks *socks, struct udpsock *us,
2d7478f7
IJ
87 bool_t recvsend, int af,
88 int r, int errnoval)
89{
90 bool_t success=r>=0;
91 if (us->experienced[recvsend][af][success]++)
92 return;
93 lg_perror(lg, uc->cc.cl.description, &uc->cc.loc,
94 success ? M_INFO : M_WARNING,
95 success ? 0 : errnoval,
96 "%s %s experiencing some %s %s%s%s",
c72d679d 97 socks->desc,iaddr_to_string(&us->addr),
2d7478f7
IJ
98 success?"success":"trouble",
99 recvsend?"transmitting":"receiving",
100 af?" ":"", af?af_name(af):"");
101}
102
f7af3192 103static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
763e458f 104{
f7af3192
IJ
105 struct udpsocks *socks=state;
106 struct udpcommon *uc=socks->uc;
a32d56fb 107 union iaddr from;
b1a0f651 108 socklen_t fromlen;
2fe58dfd
SE
109 bool_t done;
110 int rv;
08b62a6c 111 int i;
2fe58dfd 112
763e458f
IJ
113 struct commcommon *cc=&uc->cc;
114
115 for (i=0; i<socks->n_socks; i++) {
56c8ed69 116 struct udpsock *us=&socks->socks[i];
08b62a6c
IJ
117 if (i>=nfds) continue;
118 if (!(fds[i].revents & POLLIN)) continue;
56c8ed69
IJ
119 assert(fds[i].fd == us->fd);
120 int fd=us->fd;
2fe58dfd
SE
121 do {
122 fromlen=sizeof(from);
7d31df0e
IJ
123 BUF_ASSERT_FREE(cc->rbuf);
124 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
125 buffer_init(cc->rbuf,calculate_max_start_pad());
126 rv=recvfrom(fd, cc->rbuf->start,
127 buf_remaining_space(cc->rbuf),
a32d56fb 128 0, &from.sa, &fromlen);
2fe58dfd 129 if (rv>0) {
7d31df0e
IJ
130 cc->rbuf->size=rv;
131 if (uc->use_proxy) {
ff05a229
SE
132 /* Check that the packet came from our poxy server;
133 we shouldn't be contacted directly by anybody else
134 (since they can trivially forge source addresses) */
9c44ef13 135 if (!iaddr_equal(&from,&uc->proxy,False)) {
ff05a229
SE
136 Message(M_INFO,"udp: received packet that's not "
137 "from the proxy\n");
7d31df0e 138 BUF_FREE(cc->rbuf);
ff05a229
SE
139 continue;
140 }
a32d56fb
IJ
141 /* proxy protocol supports ipv4 transport only */
142 from.sa.sa_family=AF_INET;
c1ddd026 143 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
7d31df0e 144 buf_unprepend(cc->rbuf,2);
c1ddd026 145 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
ff05a229 146 }
8534d602 147 struct comm_addr ca;
7d31df0e 148 ca.comm=&cc->ops;
a32d56fb 149 ca.ia=from;
08b62a6c 150 ca.ix=i;
54d5ef00 151 done=comm_notify(&cc->notify, cc->rbuf, &ca);
c72d679d
IJ
152 if (done) {
153 udp_sock_experienced(0,uc,socks,us,0,
154 from.sa.sa_family,0,0);
155 } else {
bf28fc73 156 uint32_t msgtype;
7d31df0e
IJ
157 if (cc->rbuf->size>12 /* prevents traffic amplification */
158 && ((msgtype=get_uint32(cc->rbuf->start+8))
bf28fc73
IJ
159 != LABEL_NAK)) {
160 uint32_t source,dest;
161 /* Manufacture and send NAK packet */
7d31df0e
IJ
162 source=get_uint32(cc->rbuf->start); /* Us */
163 dest=get_uint32(cc->rbuf->start+4); /* Them */
164 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
bf28fc73 165 }
7d31df0e 166 BUF_FREE(cc->rbuf);
2fe58dfd 167 }
7d31df0e 168 BUF_ASSERT_FREE(cc->rbuf);
56c8ed69 169 } else { /* rv<=0 */
2d7478f7 170 if (errno!=EINTR && !iswouldblock(errno))
c72d679d 171 udp_sock_experienced(0,uc,socks,us, 0,0, rv,errno);
7d31df0e 172 BUF_FREE(cc->rbuf);
2fe58dfd
SE
173 }
174 } while (rv>=0);
175 }
176}
177
2fe58dfd 178static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
a15faeb2 179 const struct comm_addr *dest)
2fe58dfd
SE
180{
181 struct udp *st=commst;
763e458f
IJ
182 struct udpcommon *uc=&st->uc;
183 struct udpsocks *socks=&st->socks;
ff05a229 184 uint8_t *sa;
2fe58dfd 185
7d31df0e 186 if (uc->use_proxy) {
56c8ed69 187 struct udpsock *us=&socks->socks[0];
3abd18e8 188 sa=buf_prepend(buf,8);
a32d56fb
IJ
189 if (dest->ia.sa.sa_family != AF_INET) {
190 Message(M_INFO,
191 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
192 iaddr_to_string(&dest->ia));
193 return False;
194 }
195 memcpy(sa,&dest->ia.sin.sin_addr,4);
ff05a229 196 memset(sa+4,0,4);
a32d56fb 197 memcpy(sa+6,&dest->ia.sin.sin_port,2);
2d7478f7 198 int r=sendto(us->fd,sa,buf->size+8,0,&uc->proxy.sa,
7d31df0e 199 iaddr_socklen(&uc->proxy));
c72d679d 200 udp_sock_experienced(0,uc,socks,us, 1,0, r,errno);
3abd18e8 201 buf_unprepend(buf,8);
ff05a229 202 } else {
08b62a6c
IJ
203 int i,r;
204 bool_t allunsupported=True;
56c8ed69 205 int af=dest->ia.sa.sa_family;
7d31df0e 206 for (i=0; i<socks->n_socks; i++) {
56c8ed69
IJ
207 struct udpsock *us=&socks->socks[i];
208 if (us->addr.sa.sa_family != af)
08b62a6c
IJ
209 /* no point even trying */
210 continue;
56c8ed69 211 r=sendto(us->fd, buf->start, buf->size, 0,
08b62a6c 212 &dest->ia.sa, iaddr_socklen(&dest->ia));
c72d679d 213 udp_sock_experienced(0,uc,socks,us, 1,af, r,errno);
08b62a6c
IJ
214 if (r>=0) return True;
215 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
216 /* who knows what that error means? */
217 allunsupported=False;
218 }
219 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
ff05a229 220 }
2fe58dfd
SE
221
222 return True;
223}
224
5f8b7a8e
IJ
225void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
226{
227 if (us->fd>=0) {
228 close(us->fd);
229 us->fd=-1;
230 }
231}
232
9c44ef13
IJ
233#define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
234#define FAIL(...) do{ \
235 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
236 goto failed; \
237 }while(0)
238
239static bool_t record_socket_gotaddr(struct udpcommon *uc, struct udpsock *us,
240 int failmsgclass)
241{
242 struct commcommon *cc=&uc->cc;
243 socklen_t salen=sizeof(us->addr);
244 int r=getsockname(us->fd,&us->addr.sa,&salen);
245 if (r) FAIL("getsockname()");
246 if (salen>sizeof(us->addr)) { errno=0; FAIL("getsockname() length"); }
247 return True;
248
249 failed:
250 return False;
251}
252
253bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
254 int failmsgclass, int fd)
255{
256 FILLZERO(us->experienced);
257 us->fd=fd;
258 return record_socket_gotaddr(uc,us,failmsgclass);
259}
260
53f4e666
IJ
261bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
262 int failmsgclass)
baa06aeb 263{
f164f167 264 const union iaddr *addr=&us->addr;
763e458f 265 struct commcommon *cc=&uc->cc;
53f4e666
IJ
266 us->fd=-1;
267
2d7478f7 268 FILLZERO(us->experienced);
08b62a6c 269 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
53f4e666 270 if (us->fd<0) FAIL("socket");
f54d5ada 271 setnonblock(us->fd);
f164f167 272 setcloexec(us->fd);
08b62a6c
IJ
273#ifdef CONFIG_IPV6
274 if (addr->sa.sa_family==AF_INET6) {
275 int r;
276 int optval=1;
277 socklen_t optlen=sizeof(optval);
278 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
53f4e666 279 if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
08b62a6c
IJ
280 }
281#endif
baa06aeb 282
7d31df0e 283 if (uc->authbind) {
b2a56f7c
SE
284 pid_t c;
285 int status;
286
287 /* XXX this fork() and waitpid() business needs to be hidden
288 in some system-specific library functions. */
289 c=fork();
53f4e666
IJ
290 if (c==-1)
291 FAIL("fork() for authbind");
b2a56f7c 292 if (c==0) {
3ff31eda
IJ
293 char *argv[5], addrstr[33], portstr[5];
294 const char *addrfam;
295 int port;
5e7a5e2d 296 afterfork();
f164f167 297 switch (addr->sa.sa_family) {
a32d56fb 298 case AF_INET:
f164f167 299 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
3ff31eda
IJ
300 port=addr->sin.sin_port;
301 addrfam=NULL;
a32d56fb 302 break;
3ff31eda
IJ
303#ifdef CONFIG_IPV6
304 case AF_INET6: {
305 int i;
306 for (i=0; i<16; i++)
307 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
308 port=addr->sin6.sin6_port;
309 addrfam="6";
310 break;
311 }
312#endif /*CONFIG_IPV6*/
a32d56fb
IJ
313 default:
314 fatal("udp (%s:%d): unsupported address family for authbind",
7d31df0e 315 cc->loc.file,cc->loc.line);
a32d56fb 316 }
3ff31eda 317 sprintf(portstr,"%04X",port);
7d31df0e 318 argv[0]=uc->authbind;
3b83c932
SE
319 argv[1]=addrstr;
320 argv[2]=portstr;
3ff31eda
IJ
321 argv[3]=(char*)addrfam;
322 argv[4]=NULL;
f164f167 323 dup2(us->fd,0);
7d31df0e 324 execvp(uc->authbind,argv);
3b83c932 325 _exit(255);
b2a56f7c 326 }
3b83c932
SE
327 while (waitpid(c,&status,0)==-1) {
328 if (errno==EINTR) continue;
53f4e666 329 FAIL("waitpid for authbind");
b2a56f7c 330 }
4ac7fd3f 331 if (status) {
313e4f0f
IJ
332 if (WIFEXITED(status) && WEXITSTATUS(status)<127) {
333 int es=WEXITSTATUS(status);
334 lg_perror(FAIL_LG,es,
335 "authbind exited with error exit status %d;"
336 " indicates error",es);
337 } else {
338 lg_exitstatus(FAIL_LG,status,"authbind");
339 }
53f4e666 340 goto failed;
3b83c932 341 }
b2a56f7c 342 } else {
53f4e666
IJ
343 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0)
344 FAIL("bind (%s)",iaddr_to_string(addr));
baa06aeb 345 }
9c44ef13
IJ
346
347 bool_t ok=record_socket_gotaddr(uc,us,failmsgclass);
348 if (!ok) goto failed;
349
53f4e666
IJ
350 return True;
351
352failed:
5f8b7a8e 353 udp_destroy_socket(uc,us);
53f4e666 354 return False;
9c44ef13 355}
53f4e666
IJ
356
357#undef FAIL
baa06aeb 358
c72d679d
IJ
359void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks,
360 const char *desc)
f7af3192
IJ
361{
362 socks->uc=uc;
c72d679d 363 socks->desc=desc;
5c679ae0
IJ
364 socks->interest=
365 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
366}
367
368void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
369{
370 socks->uc=uc;
371 deregister_for_poll(socks->interest);
f7af3192
IJ
372}
373
32654a31
IJ
374void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks)
375{
376 int i;
377 for (i=0; i<socks->n_socks; i++)
378 udp_destroy_socket(uc,&socks->socks[i]);
379}
380
381static void udp_childpersist_hook(void *sst, uint32_t new_phase)
382{
383 struct udp *st=sst;
384 udp_socks_childpersist(&st->uc,&st->socks);
385}
386
f164f167
IJ
387static void udp_phase_hook(void *sst, uint32_t new_phase)
388{
389 struct udp *st=sst;
763e458f 390 struct udpsocks *socks=&st->socks;
f7af3192 391 struct udpcommon *uc=&st->uc;
08b62a6c 392 int i;
7d31df0e 393 for (i=0; i<socks->n_socks; i++)
53f4e666 394 udp_make_socket(uc,&socks->socks[i],M_FATAL);
08b62a6c 395
c72d679d 396 udp_socks_register(uc,socks, uc->use_proxy ? "proxy" : "socket");
32654a31
IJ
397
398 add_hook(PHASE_CHILDPERSIST,udp_childpersist_hook,st);
baa06aeb
SE
399}
400
2fe58dfd
SE
401static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
402 list_t *args)
403{
404 struct udp *st;
08b62a6c 405 list_t *caddrl;
ff05a229
SE
406 list_t *l;
407 uint32_t a;
08b62a6c 408 int i;
2fe58dfd 409
763e458f
IJ
410 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
411 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
412 UDP_APPLY_STANDARD(st,&st->uc,"udp");
2fe58dfd 413
763e458f
IJ
414 struct udpcommon *uc=&st->uc;
415 struct udpsocks *socks=&st->socks;
416 struct commcommon *cc=&uc->cc;
08b62a6c
IJ
417
418 union iaddr defaultaddrs[] = {
419#ifdef CONFIG_IPV6
420 { .sin6 = { .sin6_family=AF_INET6,
763e458f 421 .sin6_port=htons(uc->port),
08b62a6c
IJ
422 .sin6_addr=IN6ADDR_ANY_INIT } },
423#endif
424 { .sin = { .sin_family=AF_INET,
763e458f 425 .sin_port=htons(uc->port),
08b62a6c
IJ
426 .sin_addr= { .s_addr=INADDR_ANY } } }
427 };
428
429 caddrl=dict_lookup(d,"address");
7d31df0e
IJ
430 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
431 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
432 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
433 UDP_MAX_SOCKETS);
08b62a6c 434
7d31df0e
IJ
435 for (i=0; i<socks->n_socks; i++) {
436 struct udpsock *us=&socks->socks[i];
08b62a6c
IJ
437 if (!list_length(caddrl)) {
438 us->addr=defaultaddrs[i];
439 } else {
763e458f 440 string_item_to_iaddr(list_elem(caddrl,i),uc->port,&us->addr,"udp");
08b62a6c
IJ
441 }
442 us->fd=-1;
443 }
444
ff05a229
SE
445 l=dict_lookup(d,"proxy");
446 if (l) {
7d31df0e
IJ
447 uc->use_proxy=True;
448 uc->proxy.sa.sa_family=AF_INET;
c649eafe
IJ
449 item=list_elem(l,0);
450 if (!item || item->type!=t_string) {
7d31df0e 451 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
ff05a229 452 }
c649eafe 453 a=string_item_to_ipaddr(item,"proxy");
7d31df0e 454 uc->proxy.sin.sin_addr.s_addr=htonl(a);
c649eafe
IJ
455 item=list_elem(l,1);
456 if (!item || item->type!=t_number) {
7d31df0e 457 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
ff05a229 458 }
7d31df0e 459 uc->proxy.sin.sin_port=htons(item->data.number);
ff05a229 460 }
2fe58dfd 461
7d31df0e 462 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
3abd18e8 463
baa06aeb 464 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
2fe58dfd 465
7d31df0e 466 return new_closure(&cc->cl);
2fe58dfd
SE
467}
468
2fe58dfd
SE
469void udp_module(dict_t *dict)
470{
471 add_closure(dict,"udp",udp_apply);
472}