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"
27 static beforepoll_fn udp_beforepoll;
28 static afterpoll_fn udp_afterpoll;
29 static comm_request_notify_fn request_notify;
30 static comm_release_notify_fn release_notify;
31 static comm_sendmsg_fn udp_sendmsg;
36 struct notify_list *next;
39 #define MAX_SOCKETS 3 /* 2 ought to do really */
51 struct udpsock socks[MAX_SOCKETS];
53 struct buffer_if *rbuf;
54 struct notify_list *notify;
59 static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
60 struct udp *st=commst;
61 static char sbuf[100];
62 int ix=ca->ix>=0 ? ca->ix : 0;
64 assert(ix>=0 && ix<st->n_socks);
65 snprintf(sbuf, sizeof(sbuf), "udp:%d:%s-%s",
67 iaddr_to_string(&st->socks[ix].addr),
68 iaddr_to_string(&ca->ia));
72 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
77 if (*nfds_io<st->n_socks) {
82 for (i=0; i<st->n_socks; i++) {
83 fds[i].fd=st->socks[i].fd;
89 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
94 struct notify_list *n;
99 for (i=0; i<st->n_socks; i++) {
100 if (i>=nfds) continue;
101 if (!(fds[i].revents & POLLIN)) continue;
102 assert(fds[i].fd == st->socks[i].fd);
103 int fd=st->socks[i].fd;
106 fromlen=sizeof(from);
107 BUF_ASSERT_FREE(st->rbuf);
108 BUF_ALLOC(st->rbuf,"udp_afterpoll");
109 buffer_init(st->rbuf,calculate_max_start_pad());
110 rv=recvfrom(fd, st->rbuf->start,
111 buf_remaining_space(st->rbuf),
112 0, (struct sockaddr *)&from, &fromlen);
116 /* Check that the packet came from our poxy server;
117 we shouldn't be contacted directly by anybody else
118 (since they can trivially forge source addresses) */
119 if (!iaddr_equal(&from,&st->proxy)) {
120 Message(M_INFO,"udp: received packet that's not "
125 /* proxy protocol supports ipv4 transport only */
127 from.sa.sa_family=AF_INET;
128 memcpy(&from.sin.sin_addr,buf_unprepend(st->rbuf,4),4);
129 buf_unprepend(st->rbuf,2);
130 memcpy(&from.sin.sin_port,buf_unprepend(st->rbuf,2),2);
138 for (n=st->notify; n; n=n->next) {
139 if (n->fn(n->state, st->rbuf, &ca)) {
146 if (st->rbuf->size>12 /* prevents traffic amplification */
147 && ((msgtype=get_uint32(st->rbuf->start+8))
149 uint32_t source,dest;
150 /* Manufacture and send NAK packet */
151 source=get_uint32(st->rbuf->start); /* Us */
152 dest=get_uint32(st->rbuf->start+4); /* Them */
153 send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted");
157 BUF_ASSERT_FREE(st->rbuf);
165 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
167 struct udp *st=commst;
168 struct notify_list *n;
170 n=safe_malloc(sizeof(*n),"request_notify");
177 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
179 struct udp *st=commst;
180 struct notify_list *n, **p, *t;
184 for (n=st->notify; n; )
186 if (n->state==nst && n->fn==fn) {
198 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
199 const struct comm_addr *dest)
201 struct udp *st=commst;
205 sa=buf_prepend(buf,8);
206 if (dest->ia.sa.sa_family != AF_INET) {
208 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
209 iaddr_to_string(&dest->ia));
212 memcpy(sa,&dest->ia.sin.sin_addr,4);
214 memcpy(sa+6,&dest->ia.sin.sin_port,2);
215 sendto(st->socks[0].fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
217 buf_unprepend(buf,8);
220 bool_t allunsupported=True;
221 for (i=0; i<st->n_socks; i++) {
222 if (dest->ia.sa.sa_family != st->socks[i].addr.sa.sa_family)
223 /* no point even trying */
225 r=sendto(st->socks[i].fd, buf->start, buf->size, 0,
226 &dest->ia.sa, iaddr_socklen(&dest->ia));
228 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
229 /* who knows what that error means? */
230 allunsupported=False;
232 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
238 static void udp_make_socket(struct udp *st, struct udpsock *us)
240 const union iaddr *addr=&us->addr;
241 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
243 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
245 if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
246 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
247 st->loc.file,st->loc.line);
251 if (addr->sa.sa_family==AF_INET6) {
254 socklen_t optlen=sizeof(optval);
255 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
256 if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)",
257 st->loc.file,st->loc.line);
265 /* XXX this fork() and waitpid() business needs to be hidden
266 in some system-specific library functions. */
269 fatal_perror("udp_phase_hook: fork() for authbind");
272 char *argv[5], addrstr[33], portstr[5];
275 switch (addr->sa.sa_family) {
277 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
278 port=addr->sin.sin_port;
285 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
286 port=addr->sin6.sin6_port;
290 #endif /*CONFIG_IPV6*/
292 fatal("udp (%s:%d): unsupported address family for authbind",
293 st->loc.file,st->loc.line);
295 sprintf(portstr,"%04X",port);
296 argv[0]=st->authbind;
299 argv[3]=(char*)addrfam;
302 execvp(st->authbind,argv);
305 while (waitpid(c,&status,0)==-1) {
306 if (errno==EINTR) continue;
307 fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
309 if (WIFSIGNALED(status)) {
310 fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
311 st->loc.line, WTERMSIG(status));
313 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
314 fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
315 st->loc.line, WEXITSTATUS(status));
318 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
319 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
324 static void udp_phase_hook(void *sst, uint32_t new_phase)
328 for (i=0; i<st->n_socks; i++)
329 udp_make_socket(st,&st->socks[i]);
331 register_for_poll(st,udp_beforepoll,udp_afterpoll,MAX_SOCKETS,"udp");
334 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
345 st=safe_malloc(sizeof(*st),"udp_apply(st)");
347 st->cl.description="udp";
350 st->cl.interface=&st->ops;
352 st->ops.request_notify=request_notify;
353 st->ops.release_notify=release_notify;
354 st->ops.sendmsg=udp_sendmsg;
355 st->ops.addr_to_string=addr_to_string;
358 item=list_elem(args,0);
359 if (!item || item->type!=t_dict) {
360 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
364 int port=dict_read_number(d,"port",True,"udp",st->loc,0);
366 union iaddr defaultaddrs[] = {
368 { .sin6 = { .sin6_family=AF_INET6,
369 .sin6_port=htons(port),
370 .sin6_addr=IN6ADDR_ANY_INIT } },
372 { .sin = { .sin_family=AF_INET,
373 .sin_port=htons(port),
374 .sin_addr= { .s_addr=INADDR_ANY } } }
377 caddrl=dict_lookup(d,"address");
378 st->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
379 if (st->n_socks<=0 || st->n_socks>MAX_SOCKETS)
380 cfgfatal(st->loc,"udp","`address' must be 1..%d addresses",
383 for (i=0; i<st->n_socks; i++) {
384 struct udpsock *us=&st->socks[i];
386 if (!list_length(caddrl)) {
387 us->addr=defaultaddrs[i];
389 text2iaddr(list_elem(caddrl,i),port,
395 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
396 st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
397 l=dict_lookup(d,"proxy");
401 st->proxy.sa.sa_family=AF_INET;
403 if (!item || item->type!=t_string) {
404 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
406 a=string_item_to_ipaddr(item,"proxy");
407 st->proxy.sin.sin_addr.s_addr=htonl(a);
409 if (!item || item->type!=t_number) {
410 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
412 st->proxy.sin.sin_port=htons(item->data.number);
415 update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0);
417 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
419 return new_closure(&st->cl);
422 void udp_module(dict_t *dict)
424 add_closure(dict,"udp",udp_apply);