1 /* UDP send/receive module for secnet */
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
8 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
17 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #include "unaligned.h"
26 #include "comm-common.h"
28 static comm_sendmsg_fn udp_sendmsg;
32 struct udpsocks socks;
33 bool_t addr_configured;
38 * Re comm_addr.ix: This field allows us to note in the comm_addr
39 * which socket an incoming packet was received on. This is required
40 * for conveniently logging the actual source of a packet. But the ix
41 * does not formally form part of the address: it is not used when
42 * sending, nor when comparing two comm_addrs.
44 * The special value -1 means that the comm_addr was constructed by
45 * another module in secnet (eg the resolver), rather than being a
46 * description of the source of an incoming packet.
49 static const char *udp_addr_to_string(void *commst, const struct comm_addr *ca)
51 struct udp *st=commst;
52 struct udpsocks *socks=&st->socks;
53 static char sbuf[100];
54 int ix=ca->ix>=0 ? ca->ix : 0;
56 assert(ix>=0 && ix<socks->n_socks);
57 snprintf(sbuf, sizeof(sbuf), "udp#%u@l%d:%s%s-%s",
58 st->counter, st->uc.cc.loc.line,
59 iaddr_to_string(&socks->socks[ix].addr),
60 ca->ix<0 && socks->n_socks>1 ? "&" : "",
61 iaddr_to_string(&ca->ia));
65 static int udp_socks_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
68 struct udpsocks *socks=state;
70 BEFOREPOLL_WANT_FDS(socks->n_socks);
71 for (i=0; i<socks->n_socks; i++) {
72 fds[i].fd=socks->socks[i].fd;
78 const char *af_name(int af)
81 case AF_INET6: return "IPv6";
82 case AF_INET: return "IPv4";
83 case 0: return "(any)";
88 void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
89 struct udpsocks *socks, struct udpsock *us,
90 const union iaddr *dest, int af,
94 if (us->experienced[!!dest][af][success]++)
96 lg_perror(lg, uc->cc.cl.description, &uc->cc.loc,
97 success ? M_INFO : M_WARNING,
98 success ? 0 : errnoval,
99 "%s %s experiencing some %s %s%s%s%s%s%s",
100 socks->desc,iaddr_to_string(&us->addr),
101 success?"success":"trouble",
102 dest?"transmitting":"receiving",
103 af?" ":"", af?af_name(af):"",
105 dest?iaddr_to_string(dest):"",
109 static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
111 struct udpsocks *socks=state;
112 struct udpcommon *uc=socks->uc;
119 struct commcommon *cc=&uc->cc;
121 for (i=0; i<socks->n_socks; i++) {
122 struct udpsock *us=&socks->socks[i];
123 if (i>=nfds) continue;
124 if (!(fds[i].revents & POLLIN)) continue;
125 assert(fds[i].fd == us->fd);
128 fromlen=sizeof(from);
129 BUF_ASSERT_FREE(cc->rbuf);
130 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
131 buffer_init(cc->rbuf,calculate_max_start_pad());
132 rv=recvfrom(fd, cc->rbuf->start,
133 buf_remaining_space(cc->rbuf),
134 0, &from.sa, &fromlen);
138 /* Check that the packet came from our poxy server;
139 we shouldn't be contacted directly by anybody else
140 (since they can trivially forge source addresses) */
141 if (!iaddr_equal(&from,&uc->proxy,False)) {
142 Message(M_INFO,"udp: received packet that's not "
147 /* proxy protocol supports ipv4 transport only */
148 from.sa.sa_family=AF_INET;
149 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
150 buf_unprepend(cc->rbuf,2);
151 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
157 done=comm_notify(&cc->notify, cc->rbuf, &ca);
159 udp_sock_experienced(0,uc,socks,us,0,
160 from.sa.sa_family,0,0);
163 if (cc->rbuf->size>12 /* prevents traffic amplification */
164 && ((msgtype=get_uint32(cc->rbuf->start+8))
166 uint32_t source,dest;
167 /* Manufacture and send NAK packet */
168 source=get_uint32(cc->rbuf->start); /* Us */
169 dest=get_uint32(cc->rbuf->start+4); /* Them */
170 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
174 BUF_ASSERT_FREE(cc->rbuf);
176 if (errno!=EINTR && !iswouldblock(errno))
177 udp_sock_experienced(0,uc,socks,us, 0,0, rv,errno);
184 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
185 const struct comm_addr *dest)
187 struct udp *st=commst;
188 struct udpcommon *uc=&st->uc;
189 struct udpsocks *socks=&st->socks;
193 struct udpsock *us=&socks->socks[0];
194 sa=buf_prepend(buf,8);
195 if (dest->ia.sa.sa_family != AF_INET) {
197 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
198 iaddr_to_string(&dest->ia));
201 memcpy(sa,&dest->ia.sin.sin_addr,4);
203 memcpy(sa+6,&dest->ia.sin.sin_port,2);
204 int r=sendto(us->fd,sa,buf->size+8,0,&uc->proxy.sa,
205 iaddr_socklen(&uc->proxy));
206 udp_sock_experienced(0,uc,socks,us, &dest->ia,0, r,errno);
207 buf_unprepend(buf,8);
210 bool_t allunsupported=True;
211 int af=dest->ia.sa.sa_family;
212 for (i=0; i<socks->n_socks; i++) {
213 struct udpsock *us=&socks->socks[i];
214 if (us->addr.sa.sa_family != af)
215 /* no point even trying */
217 r=sendto(us->fd, buf->start, buf->size, 0,
218 &dest->ia.sa, iaddr_socklen(&dest->ia));
219 udp_sock_experienced(0,uc,socks,us, &dest->ia,af, r,errno);
220 if (r>=0) return True;
221 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
222 /* who knows what that error means? */
223 allunsupported=False;
225 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
231 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
239 #define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
240 #define FAIL(...) do{ \
241 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
245 static bool_t record_socket_gotaddr(struct udpcommon *uc, struct udpsock *us,
248 struct commcommon *cc=&uc->cc;
249 socklen_t salen=sizeof(us->addr);
250 int r=getsockname(us->fd,&us->addr.sa,&salen);
251 if (r) FAIL("getsockname()");
252 if (salen>sizeof(us->addr)) { errno=0; FAIL("getsockname() length"); }
259 bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
260 int failmsgclass, int fd)
262 FILLZERO(us->experienced);
264 return record_socket_gotaddr(uc,us,failmsgclass);
267 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
270 const union iaddr *addr=&us->addr;
271 struct commcommon *cc=&uc->cc;
274 FILLZERO(us->experienced);
275 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
276 if (us->fd<0) FAIL("socket");
280 if (addr->sa.sa_family==AF_INET6) {
283 socklen_t optlen=sizeof(optval);
284 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
285 if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
293 snprintf(desc,sizeof(desc),"authbind for %s: %s",
294 iaddr_to_string(addr), uc->authbind);
296 /* XXX this fork() and waitpid() business needs to be hidden
297 in some system-specific library functions. */
300 FAIL("fork() for authbind");
302 char *argv[5], addrstr[33], portstr[5];
306 switch (addr->sa.sa_family) {
308 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
309 port=addr->sin.sin_port;
316 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
317 port=addr->sin6.sin6_port;
321 #endif /*CONFIG_IPV6*/
323 fatal("udp (%s:%d): unsupported address family for authbind",
324 cc->loc.file,cc->loc.line);
326 sprintf(portstr,"%04X",port);
327 argv[0]=uc->authbind;
330 argv[3]=(char*)addrfam;
333 execvp(uc->authbind,argv);
336 while (waitpid(c,&status,0)==-1) {
337 if (errno==EINTR) continue;
338 FAIL("waitpid for authbind");
341 if (WIFEXITED(status) && WEXITSTATUS(status)<127) {
342 int es=WEXITSTATUS(status);
343 lg_perror(FAIL_LG,es,
344 "%s exited with error exit status %d;"
345 " indicates error",desc,es);
347 lg_exitstatus(FAIL_LG,status,desc);
352 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0)
353 FAIL("bind (%s)",iaddr_to_string(addr));
356 bool_t ok=record_socket_gotaddr(uc,us,failmsgclass);
357 if (!ok) goto failed;
362 udp_destroy_socket(uc,us);
368 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks,
374 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
377 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
380 deregister_for_poll(socks->interest);
383 void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks)
386 for (i=0; i<socks->n_socks; i++)
387 udp_destroy_socket(uc,&socks->socks[i]);
390 static void udp_childpersist_hook(void *sst, uint32_t new_phase)
393 udp_socks_childpersist(&st->uc,&st->socks);
396 static void udp_phase_hook(void *sst, uint32_t new_phase)
399 struct udpsocks *socks=&st->socks;
400 struct udpcommon *uc=&st->uc;
404 for (i=0; i<socks->n_socks; i++) {
405 bool_t required=st->addr_configured
406 || (!anydone && i==socks->n_socks-1);
407 anydone += udp_make_socket(uc,&socks->socks[i],
408 required ? M_FATAL : M_WARNING);
411 udp_socks_register(uc,socks, uc->use_proxy ? "proxy" : "socket");
413 add_hook(PHASE_CHILDPERSIST,udp_childpersist_hook,st);
416 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
419 static unsigned counter;
427 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
428 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
429 UDP_APPLY_STANDARD(st,&st->uc,"udp");
431 struct udpcommon *uc=&st->uc;
432 struct udpsocks *socks=&st->socks;
433 struct commcommon *cc=&uc->cc;
435 st->counter=counter++;
437 union iaddr defaultaddrs[] = {
439 { .sin6 = { .sin6_family=AF_INET6,
440 .sin6_port=htons(uc->port),
441 .sin6_addr=IN6ADDR_ANY_INIT } },
443 { .sin = { .sin_family=AF_INET,
444 .sin_port=htons(uc->port),
445 .sin_addr= { .s_addr=INADDR_ANY } } }
448 caddrl=dict_lookup(d,"address");
449 st->addr_configured=!!caddrl;
450 socks->n_socks=st->addr_configured ? list_length(caddrl)
451 : (int)ARRAY_SIZE(defaultaddrs);
452 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
453 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
456 for (i=0; i<socks->n_socks; i++) {
457 struct udpsock *us=&socks->socks[i];
458 if (!st->addr_configured) {
459 us->addr=defaultaddrs[i];
461 string_item_to_iaddr(list_elem(caddrl,i),uc->port,&us->addr,"udp");
466 l=dict_lookup(d,"proxy");
469 uc->proxy.sa.sa_family=AF_INET;
471 if (!item || item->type!=t_string) {
472 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
474 a=string_item_to_ipaddr(item,"proxy");
475 uc->proxy.sin.sin_addr.s_addr=htonl(a);
477 if (!item || item->type!=t_number) {
478 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
480 uc->proxy.sin.sin_port=htons(item->data.number);
483 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
485 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
487 return new_closure(&cc->cl);
490 void udp_module(dict_t *dict)
492 add_closure(dict,"udp",udp_apply);