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 const char *af_name(int af)
66 case AF_INET6: return "IPv6";
67 case AF_INET: return "IPv4";
68 case 0: return "(any)";
73 void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
74 const char *socksdesc, struct udpsock *us,
75 bool_t recvsend, int af,
79 if (us->experienced[recvsend][af][success]++)
81 lg_perror(lg, uc->cc.cl.description, &uc->cc.loc,
82 success ? M_INFO : M_WARNING,
83 success ? 0 : errnoval,
84 "%s %s experiencing some %s %s%s%s",
85 socksdesc,iaddr_to_string(&us->addr),
86 success?"success":"trouble",
87 recvsend?"transmitting":"receiving",
88 af?" ":"", af?af_name(af):"");
91 static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
93 struct udpsocks *socks=state;
94 struct udpcommon *uc=socks->uc;
101 struct commcommon *cc=&uc->cc;
103 for (i=0; i<socks->n_socks; i++) {
104 struct udpsock *us=&socks->socks[i];
105 if (i>=nfds) continue;
106 if (!(fds[i].revents & POLLIN)) continue;
107 assert(fds[i].fd == us->fd);
111 fromlen=sizeof(from);
112 BUF_ASSERT_FREE(cc->rbuf);
113 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
114 buffer_init(cc->rbuf,calculate_max_start_pad());
115 rv=recvfrom(fd, cc->rbuf->start,
116 buf_remaining_space(cc->rbuf),
117 0, (struct sockaddr *)&from, &fromlen);
121 /* Check that the packet came from our poxy server;
122 we shouldn't be contacted directly by anybody else
123 (since they can trivially forge source addresses) */
124 if (!iaddr_equal(&from,&uc->proxy)) {
125 Message(M_INFO,"udp: received packet that's not "
130 /* proxy protocol supports ipv4 transport only */
132 from.sa.sa_family=AF_INET;
133 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
134 buf_unprepend(cc->rbuf,2);
135 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
142 done=comm_notify(&cc->notify, cc->rbuf, &ca);
145 if (cc->rbuf->size>12 /* prevents traffic amplification */
146 && ((msgtype=get_uint32(cc->rbuf->start+8))
148 uint32_t source,dest;
149 /* Manufacture and send NAK packet */
150 source=get_uint32(cc->rbuf->start); /* Us */
151 dest=get_uint32(cc->rbuf->start+4); /* Them */
152 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
156 BUF_ASSERT_FREE(cc->rbuf);
158 if (errno!=EINTR && !iswouldblock(errno))
159 udp_sock_experienced(0,uc, "socket",us, 0,0, rv,errno);
166 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
167 const struct comm_addr *dest)
169 struct udp *st=commst;
170 struct udpcommon *uc=&st->uc;
171 struct udpsocks *socks=&st->socks;
175 struct udpsock *us=&socks->socks[0];
176 sa=buf_prepend(buf,8);
177 if (dest->ia.sa.sa_family != AF_INET) {
179 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
180 iaddr_to_string(&dest->ia));
183 memcpy(sa,&dest->ia.sin.sin_addr,4);
185 memcpy(sa+6,&dest->ia.sin.sin_port,2);
186 int r=sendto(us->fd,sa,buf->size+8,0,
187 (struct sockaddr *)&uc->proxy,
189 udp_sock_experienced(0,uc, "proxy",us, 1,0, r,errno);
190 buf_unprepend(buf,8);
193 bool_t allunsupported=True;
194 int af=dest->ia.sa.sa_family;
195 for (i=0; i<socks->n_socks; i++) {
196 struct udpsock *us=&socks->socks[i];
197 if (us->addr.sa.sa_family != af)
198 /* no point even trying */
200 r=sendto(us->fd, buf->start, buf->size, 0,
201 &dest->ia.sa, iaddr_socklen(&dest->ia));
202 udp_sock_experienced(0,uc, "socket",us, 1,af, r,errno);
204 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
205 /* who knows what that error means? */
206 allunsupported=False;
208 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
214 void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
222 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
225 const union iaddr *addr=&us->addr;
226 struct commcommon *cc=&uc->cc;
229 #define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
230 #define FAIL(...) do{ \
231 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
235 FILLZERO(us->experienced);
236 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
237 if (us->fd<0) FAIL("socket");
241 if (addr->sa.sa_family==AF_INET6) {
244 socklen_t optlen=sizeof(optval);
245 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
246 if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
254 /* XXX this fork() and waitpid() business needs to be hidden
255 in some system-specific library functions. */
258 FAIL("fork() for authbind");
260 char *argv[5], addrstr[33], portstr[5];
264 switch (addr->sa.sa_family) {
266 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
267 port=addr->sin.sin_port;
274 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
275 port=addr->sin6.sin6_port;
279 #endif /*CONFIG_IPV6*/
281 fatal("udp (%s:%d): unsupported address family for authbind",
282 cc->loc.file,cc->loc.line);
284 sprintf(portstr,"%04X",port);
285 argv[0]=uc->authbind;
288 argv[3]=(char*)addrfam;
291 execvp(uc->authbind,argv);
294 while (waitpid(c,&status,0)==-1) {
295 if (errno==EINTR) continue;
296 FAIL("waitpid for authbind");
299 lg_exitstatus(FAIL_LG,status,"authbind");
303 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0)
304 FAIL("bind (%s)",iaddr_to_string(addr));
309 udp_destroy_socket(uc,us);
315 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
319 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
322 void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
325 deregister_for_poll(socks->interest);
328 static void udp_phase_hook(void *sst, uint32_t new_phase)
331 struct udpsocks *socks=&st->socks;
332 struct udpcommon *uc=&st->uc;
334 for (i=0; i<socks->n_socks; i++)
335 udp_make_socket(uc,&socks->socks[i],M_FATAL);
337 udp_socks_register(uc,socks);
340 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
349 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
350 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
351 UDP_APPLY_STANDARD(st,&st->uc,"udp");
353 struct udpcommon *uc=&st->uc;
354 struct udpsocks *socks=&st->socks;
355 struct commcommon *cc=&uc->cc;
357 union iaddr defaultaddrs[] = {
359 { .sin6 = { .sin6_family=AF_INET6,
360 .sin6_port=htons(uc->port),
361 .sin6_addr=IN6ADDR_ANY_INIT } },
363 { .sin = { .sin_family=AF_INET,
364 .sin_port=htons(uc->port),
365 .sin_addr= { .s_addr=INADDR_ANY } } }
368 caddrl=dict_lookup(d,"address");
369 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
370 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
371 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
374 for (i=0; i<socks->n_socks; i++) {
375 struct udpsock *us=&socks->socks[i];
377 if (!list_length(caddrl)) {
378 us->addr=defaultaddrs[i];
380 text2iaddr(list_elem(caddrl,i),uc->port,
386 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
387 l=dict_lookup(d,"proxy");
391 uc->proxy.sa.sa_family=AF_INET;
393 if (!item || item->type!=t_string) {
394 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
396 a=string_item_to_ipaddr(item,"proxy");
397 uc->proxy.sin.sin_addr.s_addr=htonl(a);
399 if (!item || item->type!=t_number) {
400 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
402 uc->proxy.sin.sin_port=htons(item->data.number);
405 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
407 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
409 return new_closure(&cc->cl);
412 void udp_module(dict_t *dict)
414 add_closure(dict,"udp",udp_apply);