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 if (i>=nfds) continue;
77 if (!(fds[i].revents & POLLIN)) continue;
78 assert(fds[i].fd == socks->socks[i].fd);
79 int fd=socks->socks[i].fd;
83 BUF_ASSERT_FREE(cc->rbuf);
84 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
85 buffer_init(cc->rbuf,calculate_max_start_pad());
86 rv=recvfrom(fd, cc->rbuf->start,
87 buf_remaining_space(cc->rbuf),
88 0, (struct sockaddr *)&from, &fromlen);
92 /* Check that the packet came from our poxy server;
93 we shouldn't be contacted directly by anybody else
94 (since they can trivially forge source addresses) */
95 if (!iaddr_equal(&from,&uc->proxy)) {
96 Message(M_INFO,"udp: received packet that's not "
101 /* proxy protocol supports ipv4 transport only */
103 from.sa.sa_family=AF_INET;
104 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
105 buf_unprepend(cc->rbuf,2);
106 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
113 done=comm_notify(&cc->notify, cc->rbuf, &ca);
116 if (cc->rbuf->size>12 /* prevents traffic amplification */
117 && ((msgtype=get_uint32(cc->rbuf->start+8))
119 uint32_t source,dest;
120 /* Manufacture and send NAK packet */
121 source=get_uint32(cc->rbuf->start); /* Us */
122 dest=get_uint32(cc->rbuf->start+4); /* Them */
123 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
127 BUF_ASSERT_FREE(cc->rbuf);
135 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
136 const struct comm_addr *dest)
138 struct udp *st=commst;
139 struct udpcommon *uc=&st->uc;
140 struct udpsocks *socks=&st->socks;
144 sa=buf_prepend(buf,8);
145 if (dest->ia.sa.sa_family != AF_INET) {
147 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
148 iaddr_to_string(&dest->ia));
151 memcpy(sa,&dest->ia.sin.sin_addr,4);
153 memcpy(sa+6,&dest->ia.sin.sin_port,2);
154 sendto(socks->socks[0].fd,sa,buf->size+8,0,(struct sockaddr *)&uc->proxy,
156 buf_unprepend(buf,8);
159 bool_t allunsupported=True;
160 for (i=0; i<socks->n_socks; i++) {
161 if (dest->ia.sa.sa_family != socks->socks[i].addr.sa.sa_family)
162 /* no point even trying */
164 r=sendto(socks->socks[i].fd, buf->start, buf->size, 0,
165 &dest->ia.sa, iaddr_socklen(&dest->ia));
167 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
168 /* who knows what that error means? */
169 allunsupported=False;
171 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
177 void udp_make_socket(struct udpcommon *uc, struct udpsock *us)
179 const union iaddr *addr=&us->addr;
180 struct commcommon *cc=&uc->cc;
182 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
184 fatal_perror("udp (%s:%d): socket",cc->loc.file,cc->loc.line);
186 if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
187 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
188 cc->loc.file,cc->loc.line);
192 if (addr->sa.sa_family==AF_INET6) {
195 socklen_t optlen=sizeof(optval);
196 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
197 if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)",
198 cc->loc.file,cc->loc.line);
206 /* XXX this fork() and waitpid() business needs to be hidden
207 in some system-specific library functions. */
210 fatal_perror("udp_phase_hook: fork() for authbind");
213 char *argv[5], addrstr[33], portstr[5];
216 switch (addr->sa.sa_family) {
218 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
219 port=addr->sin.sin_port;
226 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
227 port=addr->sin6.sin6_port;
231 #endif /*CONFIG_IPV6*/
233 fatal("udp (%s:%d): unsupported address family for authbind",
234 cc->loc.file,cc->loc.line);
236 sprintf(portstr,"%04X",port);
237 argv[0]=uc->authbind;
240 argv[3]=(char*)addrfam;
243 execvp(uc->authbind,argv);
246 while (waitpid(c,&status,0)==-1) {
247 if (errno==EINTR) continue;
248 fatal_perror("udp (%s:%d): authbind",cc->loc.file,cc->loc.line);
250 if (WIFSIGNALED(status)) {
251 fatal("udp (%s:%d): authbind died on signal %d",cc->loc.file,
252 cc->loc.line, WTERMSIG(status));
254 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
255 fatal("udp (%s:%d): authbind died with status %d",cc->loc.file,
256 cc->loc.line, WEXITSTATUS(status));
259 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
260 fatal_perror("udp (%s:%d): bind",cc->loc.file,cc->loc.line);
265 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
268 register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
271 static void udp_phase_hook(void *sst, uint32_t new_phase)
274 struct udpsocks *socks=&st->socks;
275 struct udpcommon *uc=&st->uc;
277 for (i=0; i<socks->n_socks; i++)
278 udp_make_socket(uc,&socks->socks[i]);
280 udp_socks_register(uc,socks);
283 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
292 COMM_APPLY(st,&st->uc.cc,udp_,"udp",loc);
293 COMM_APPLY_STANDARD(st,&st->uc.cc,"udp",args);
294 UDP_APPLY_STANDARD(st,&st->uc,"udp");
296 struct udpcommon *uc=&st->uc;
297 struct udpsocks *socks=&st->socks;
298 struct commcommon *cc=&uc->cc;
300 union iaddr defaultaddrs[] = {
302 { .sin6 = { .sin6_family=AF_INET6,
303 .sin6_port=htons(uc->port),
304 .sin6_addr=IN6ADDR_ANY_INIT } },
306 { .sin = { .sin_family=AF_INET,
307 .sin_port=htons(uc->port),
308 .sin_addr= { .s_addr=INADDR_ANY } } }
311 caddrl=dict_lookup(d,"address");
312 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
313 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
314 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
317 for (i=0; i<socks->n_socks; i++) {
318 struct udpsock *us=&socks->socks[i];
320 if (!list_length(caddrl)) {
321 us->addr=defaultaddrs[i];
323 text2iaddr(list_elem(caddrl,i),uc->port,
329 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
330 l=dict_lookup(d,"proxy");
334 uc->proxy.sa.sa_family=AF_INET;
336 if (!item || item->type!=t_string) {
337 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
339 a=string_item_to_ipaddr(item,"proxy");
340 uc->proxy.sin.sin_addr.s_addr=htonl(a);
342 if (!item || item->type!=t_number) {
343 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
345 uc->proxy.sin.sin_port=htons(item->data.number);
348 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
350 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
352 return new_closure(&cc->cl);
355 void udp_module(dict_t *dict)
357 add_closure(dict,"udp",udp_apply);