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;
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.
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.
47 static const char *udp_addr_to_string(void *commst, const struct comm_addr *ca)
49 struct udp *st=commst;
50 struct udpsocks *socks=&st->socks;
51 static char sbuf[100];
52 int ix=ca->ix>=0 ? ca->ix : 0;
54 assert(ix>=0 && ix<socks->n_socks);
55 snprintf(sbuf, sizeof(sbuf), "udp:%s%s-%s",
56 iaddr_to_string(&socks->socks[ix].addr),
58 iaddr_to_string(&ca->ia));
62 static int udp_socks_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
65 struct udpsocks *socks=state;
67 BEFOREPOLL_WANT_FDS(socks->n_socks);
68 for (i=0; i<socks->n_socks; i++) {
69 fds[i].fd=socks->socks[i].fd;
75 const char *af_name(int af)
78 case AF_INET6: return "IPv6";
79 case AF_INET: return "IPv4";
80 case 0: return "(any)";
85 void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
86 const char *socksdesc, struct udpsock *us,
87 bool_t recvsend, int af,
91 if (us->experienced[recvsend][af][success]++)
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",
97 socksdesc,iaddr_to_string(&us->addr),
98 success?"success":"trouble",
99 recvsend?"transmitting":"receiving",
100 af?" ":"", af?af_name(af):"");
103 static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
105 struct udpsocks *socks=state;
106 struct udpcommon *uc=socks->uc;
113 struct commcommon *cc=&uc->cc;
115 for (i=0; i<socks->n_socks; i++) {
116 struct udpsock *us=&socks->socks[i];
117 if (i>=nfds) continue;
118 if (!(fds[i].revents & POLLIN)) continue;
119 assert(fds[i].fd == us->fd);
122 fromlen=sizeof(from);
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),
128 0, &from.sa, &fromlen);
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) */
135 if (!iaddr_equal(&from,&uc->proxy,False)) {
136 Message(M_INFO,"udp: received packet that's not "
141 /* proxy protocol supports ipv4 transport only */
142 from.sa.sa_family=AF_INET;
143 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
144 buf_unprepend(cc->rbuf,2);
145 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
151 done=comm_notify(&cc->notify, cc->rbuf, &ca);
154 if (cc->rbuf->size>12 /* prevents traffic amplification */
155 && ((msgtype=get_uint32(cc->rbuf->start+8))
157 uint32_t source,dest;
158 /* Manufacture and send NAK packet */
159 source=get_uint32(cc->rbuf->start); /* Us */
160 dest=get_uint32(cc->rbuf->start+4); /* Them */
161 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
165 BUF_ASSERT_FREE(cc->rbuf);
167 if (errno!=EINTR && !iswouldblock(errno))
168 udp_sock_experienced(0,uc, "socket",us, 0,0, rv,errno);
175 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
176 const struct comm_addr *dest)
178 struct udp *st=commst;
179 struct udpcommon *uc=&st->uc;
180 struct udpsocks *socks=&st->socks;
184 struct udpsock *us=&socks->socks[0];
185 sa=buf_prepend(buf,8);
186 if (dest->ia.sa.sa_family != AF_INET) {
188 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
189 iaddr_to_string(&dest->ia));
192 memcpy(sa,&dest->ia.sin.sin_addr,4);
194 memcpy(sa+6,&dest->ia.sin.sin_port,2);
195 int r=sendto(us->fd,sa,buf->size+8,0,&uc->proxy.sa,
196 iaddr_socklen(&uc->proxy));
197 udp_sock_experienced(0,uc, "proxy",us, 1,0, r,errno);
198 buf_unprepend(buf,8);
201 bool_t allunsupported=True;
202 int af=dest->ia.sa.sa_family;
203 for (i=0; i<socks->n_socks; i++) {
204 struct udpsock *us=&socks->socks[i];
205 if (us->addr.sa.sa_family != af)
206 /* no point even trying */
208 r=sendto(us->fd, buf->start, buf->size, 0,
209 &dest->ia.sa, iaddr_socklen(&dest->ia));
210 udp_sock_experienced(0,uc, "socket",us, 1,af, r,errno);
211 if (r>=0) return True;
212 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
213 /* who knows what that error means? */
214 allunsupported=False;
216 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
222 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
230 #define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
231 #define FAIL(...) do{ \
232 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
236 static bool_t record_socket_gotaddr(struct udpcommon *uc, struct udpsock *us,
239 struct commcommon *cc=&uc->cc;
240 socklen_t salen=sizeof(us->addr);
241 int r=getsockname(us->fd,&us->addr.sa,&salen);
242 if (r) FAIL("getsockname()");
243 if (salen>sizeof(us->addr)) { errno=0; FAIL("getsockname() length"); }
250 bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
251 int failmsgclass, int fd)
253 FILLZERO(us->experienced);
255 return record_socket_gotaddr(uc,us,failmsgclass);
258 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
261 const union iaddr *addr=&us->addr;
262 struct commcommon *cc=&uc->cc;
265 FILLZERO(us->experienced);
266 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
267 if (us->fd<0) FAIL("socket");
271 if (addr->sa.sa_family==AF_INET6) {
274 socklen_t optlen=sizeof(optval);
275 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
276 if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
284 /* XXX this fork() and waitpid() business needs to be hidden
285 in some system-specific library functions. */
288 FAIL("fork() for authbind");
290 char *argv[5], addrstr[33], portstr[5];
294 switch (addr->sa.sa_family) {
296 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
297 port=addr->sin.sin_port;
304 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
305 port=addr->sin6.sin6_port;
309 #endif /*CONFIG_IPV6*/
311 fatal("udp (%s:%d): unsupported address family for authbind",
312 cc->loc.file,cc->loc.line);
314 sprintf(portstr,"%04X",port);
315 argv[0]=uc->authbind;
318 argv[3]=(char*)addrfam;
321 execvp(uc->authbind,argv);
324 while (waitpid(c,&status,0)==-1) {
325 if (errno==EINTR) continue;
326 FAIL("waitpid for authbind");
329 if (WIFEXITED(status) && WEXITSTATUS(status)<127) {
330 int es=WEXITSTATUS(status);
331 lg_perror(FAIL_LG,es,
332 "authbind exited with error exit status %d;"
333 " indicates error",es);
335 lg_exitstatus(FAIL_LG,status,"authbind");
340 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0)
341 FAIL("bind (%s)",iaddr_to_string(addr));
344 bool_t ok=record_socket_gotaddr(uc,us,failmsgclass);
345 if (!ok) goto failed;
350 udp_destroy_socket(uc,us);
356 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
360 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
363 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
366 deregister_for_poll(socks->interest);
369 void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks)
372 for (i=0; i<socks->n_socks; i++)
373 udp_destroy_socket(uc,&socks->socks[i]);
376 static void udp_childpersist_hook(void *sst, uint32_t new_phase)
379 udp_socks_childpersist(&st->uc,&st->socks);
382 static void udp_phase_hook(void *sst, uint32_t new_phase)
385 struct udpsocks *socks=&st->socks;
386 struct udpcommon *uc=&st->uc;
388 for (i=0; i<socks->n_socks; i++)
389 udp_make_socket(uc,&socks->socks[i],M_FATAL);
391 udp_socks_register(uc,socks);
393 add_hook(PHASE_CHILDPERSIST,udp_childpersist_hook,st);
396 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
405 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
406 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
407 UDP_APPLY_STANDARD(st,&st->uc,"udp");
409 struct udpcommon *uc=&st->uc;
410 struct udpsocks *socks=&st->socks;
411 struct commcommon *cc=&uc->cc;
413 union iaddr defaultaddrs[] = {
415 { .sin6 = { .sin6_family=AF_INET6,
416 .sin6_port=htons(uc->port),
417 .sin6_addr=IN6ADDR_ANY_INIT } },
419 { .sin = { .sin_family=AF_INET,
420 .sin_port=htons(uc->port),
421 .sin_addr= { .s_addr=INADDR_ANY } } }
424 caddrl=dict_lookup(d,"address");
425 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
426 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
427 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
430 for (i=0; i<socks->n_socks; i++) {
431 struct udpsock *us=&socks->socks[i];
432 if (!list_length(caddrl)) {
433 us->addr=defaultaddrs[i];
435 string_item_to_iaddr(list_elem(caddrl,i),uc->port,&us->addr,"udp");
440 l=dict_lookup(d,"proxy");
443 uc->proxy.sa.sa_family=AF_INET;
445 if (!item || item->type!=t_string) {
446 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
448 a=string_item_to_ipaddr(item,"proxy");
449 uc->proxy.sin.sin_addr.s_addr=htonl(a);
451 if (!item || item->type!=t_number) {
452 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
454 uc->proxy.sin.sin_port=htons(item->data.number);
457 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
459 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
461 return new_closure(&cc->cl);
464 void udp_module(dict_t *dict)
466 add_closure(dict,"udp",udp_apply);