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;
35 static const char *udp_addr_to_string(void *commst, const struct comm_addr *ca)
37 struct udp *st=commst;
38 struct udpsocks *socks=&st->socks;
39 static char sbuf[100];
40 int ix=ca->ix>=0 ? ca->ix : 0;
42 assert(ix>=0 && ix<socks->n_socks);
43 snprintf(sbuf, sizeof(sbuf), "udp:%d:%s-%s",
45 iaddr_to_string(&socks->socks[ix].addr),
46 iaddr_to_string(&ca->ia));
50 static int udp_socks_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
53 struct udpsocks *socks=state;
55 BEFOREPOLL_WANT_FDS(socks->n_socks);
56 for (i=0; i<socks->n_socks; i++) {
57 fds[i].fd=socks->socks[i].fd;
63 static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
65 struct udpsocks *socks=state;
66 struct udpcommon *uc=socks->uc;
73 struct commcommon *cc=&uc->cc;
75 for (i=0; i<socks->n_socks; i++) {
76 struct udpsock *us=&socks->socks[i];
77 if (i>=nfds) continue;
78 if (!(fds[i].revents & POLLIN)) continue;
79 assert(fds[i].fd == us->fd);
84 BUF_ASSERT_FREE(cc->rbuf);
85 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
86 buffer_init(cc->rbuf,calculate_max_start_pad());
87 rv=recvfrom(fd, cc->rbuf->start,
88 buf_remaining_space(cc->rbuf),
89 0, (struct sockaddr *)&from, &fromlen);
93 /* Check that the packet came from our poxy server;
94 we shouldn't be contacted directly by anybody else
95 (since they can trivially forge source addresses) */
96 if (!iaddr_equal(&from,&uc->proxy)) {
97 Message(M_INFO,"udp: received packet that's not "
102 /* proxy protocol supports ipv4 transport only */
104 from.sa.sa_family=AF_INET;
105 memcpy(&from.sin.sin_addr,buf_unprepend(cc->rbuf,4),4);
106 buf_unprepend(cc->rbuf,2);
107 memcpy(&from.sin.sin_port,buf_unprepend(cc->rbuf,2),2);
114 done=comm_notify(&cc->notify, cc->rbuf, &ca);
117 if (cc->rbuf->size>12 /* prevents traffic amplification */
118 && ((msgtype=get_uint32(cc->rbuf->start+8))
120 uint32_t source,dest;
121 /* Manufacture and send NAK packet */
122 source=get_uint32(cc->rbuf->start); /* Us */
123 dest=get_uint32(cc->rbuf->start+4); /* Them */
124 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
128 BUF_ASSERT_FREE(cc->rbuf);
136 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
137 const struct comm_addr *dest)
139 struct udp *st=commst;
140 struct udpcommon *uc=&st->uc;
141 struct udpsocks *socks=&st->socks;
145 struct udpsock *us=&socks->socks[0];
146 sa=buf_prepend(buf,8);
147 if (dest->ia.sa.sa_family != AF_INET) {
149 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
150 iaddr_to_string(&dest->ia));
153 memcpy(sa,&dest->ia.sin.sin_addr,4);
155 memcpy(sa+6,&dest->ia.sin.sin_port,2);
156 sendto(us->fd,sa,buf->size+8,0,(struct sockaddr *)&uc->proxy,
158 buf_unprepend(buf,8);
161 bool_t allunsupported=True;
162 int af=dest->ia.sa.sa_family;
163 for (i=0; i<socks->n_socks; i++) {
164 struct udpsock *us=&socks->socks[i];
165 if (us->addr.sa.sa_family != af)
166 /* no point even trying */
168 r=sendto(us->fd, buf->start, buf->size, 0,
169 &dest->ia.sa, iaddr_socklen(&dest->ia));
171 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
172 /* who knows what that error means? */
173 allunsupported=False;
175 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
181 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
189 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
192 const union iaddr *addr=&us->addr;
193 struct commcommon *cc=&uc->cc;
196 #define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
197 #define FAIL(...) do{ \
198 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
202 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
203 if (us->fd<0) FAIL("socket");
207 if (addr->sa.sa_family==AF_INET6) {
210 socklen_t optlen=sizeof(optval);
211 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
212 if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
220 /* XXX this fork() and waitpid() business needs to be hidden
221 in some system-specific library functions. */
224 FAIL("fork() for authbind");
226 char *argv[5], addrstr[33], portstr[5];
230 switch (addr->sa.sa_family) {
232 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
233 port=addr->sin.sin_port;
240 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
241 port=addr->sin6.sin6_port;
245 #endif /*CONFIG_IPV6*/
247 fatal("udp (%s:%d): unsupported address family for authbind",
248 cc->loc.file,cc->loc.line);
250 sprintf(portstr,"%04X",port);
251 argv[0]=uc->authbind;
254 argv[3]=(char*)addrfam;
257 execvp(uc->authbind,argv);
260 while (waitpid(c,&status,0)==-1) {
261 if (errno==EINTR) continue;
262 FAIL("waitpid for authbind");
265 lg_exitstatus(FAIL_LG,status,"authbind");
269 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0)
270 FAIL("bind (%s)",iaddr_to_string(addr));
275 udp_destroy_socket(uc,us);
281 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
285 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
288 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
291 deregister_for_poll(socks->interest);
294 static void udp_phase_hook(void *sst, uint32_t new_phase)
297 struct udpsocks *socks=&st->socks;
298 struct udpcommon *uc=&st->uc;
300 for (i=0; i<socks->n_socks; i++)
301 udp_make_socket(uc,&socks->socks[i],M_FATAL);
303 udp_socks_register(uc,socks);
306 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
315 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
316 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
317 UDP_APPLY_STANDARD(st,&st->uc,"udp");
319 struct udpcommon *uc=&st->uc;
320 struct udpsocks *socks=&st->socks;
321 struct commcommon *cc=&uc->cc;
323 union iaddr defaultaddrs[] = {
325 { .sin6 = { .sin6_family=AF_INET6,
326 .sin6_port=htons(uc->port),
327 .sin6_addr=IN6ADDR_ANY_INIT } },
329 { .sin = { .sin_family=AF_INET,
330 .sin_port=htons(uc->port),
331 .sin_addr= { .s_addr=INADDR_ANY } } }
334 caddrl=dict_lookup(d,"address");
335 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
336 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
337 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
340 for (i=0; i<socks->n_socks; i++) {
341 struct udpsock *us=&socks->socks[i];
343 if (!list_length(caddrl)) {
344 us->addr=defaultaddrs[i];
346 text2iaddr(list_elem(caddrl,i),uc->port,
352 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
353 l=dict_lookup(d,"proxy");
357 uc->proxy.sa.sa_family=AF_INET;
359 if (!item || item->type!=t_string) {
360 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
362 a=string_item_to_ipaddr(item,"proxy");
363 uc->proxy.sin.sin_addr.s_addr=htonl(a);
365 if (!item || item->type!=t_number) {
366 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
368 uc->proxy.sin.sin_port=htons(item->data.number);
371 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
373 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
375 return new_closure(&cc->cl);
378 void udp_module(dict_t *dict)
380 add_closure(dict,"udp",udp_apply);