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;
33 struct comm_notify_entry {
36 LIST_ENTRY(comm_notify_entry) entry;
38 LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
40 #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
52 struct udpsock socks[UDP_MAX_SOCKETS];
54 struct buffer_if *rbuf;
55 struct comm_notify_list notify;
60 static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
61 struct udp *st=commst;
62 struct udp *socks=st; /* for now */
63 static char sbuf[100];
64 int ix=ca->ix>=0 ? ca->ix : 0;
66 assert(ix>=0 && ix<socks->n_socks);
67 snprintf(sbuf, sizeof(sbuf), "udp:%d:%s-%s",
69 iaddr_to_string(&socks->socks[ix].addr),
70 iaddr_to_string(&ca->ia));
74 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
79 struct udp *socks=st; /* for now */
80 BEFOREPOLL_WANT_FDS(socks->n_socks);
81 for (i=0; i<socks->n_socks; i++) {
82 fds[i].fd=socks->socks[i].fd;
88 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
91 struct udp *socks=st; /* for now */
92 struct udp *cc=st; /* for now */
93 struct udp *uc=st; /* for now */
96 struct comm_notify_entry *n;
101 for (i=0; i<st->n_socks; i++) {
102 if (i>=nfds) continue;
103 if (!(fds[i].revents & POLLIN)) continue;
104 assert(fds[i].fd == socks->socks[i].fd);
105 int fd=socks->socks[i].fd;
108 fromlen=sizeof(from);
109 BUF_ASSERT_FREE(cc->rbuf);
110 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
111 buffer_init(cc->rbuf,calculate_max_start_pad());
112 rv=recvfrom(fd, cc->rbuf->start,
113 buf_remaining_space(cc->rbuf),
114 0, (struct sockaddr *)&from, &fromlen);
118 /* Check that the packet came from our poxy server;
119 we shouldn't be contacted directly by anybody else
120 (since they can trivially forge source addresses) */
121 if (!iaddr_equal(&from,&uc->proxy)) {
122 Message(M_INFO,"udp: received packet that's not "
127 /* proxy protocol supports ipv4 transport only */
129 from.sa.sa_family=AF_INET;
130 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
131 buf_unprepend(cc->rbuf,2);
132 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
140 LIST_FOREACH(n, &cc->notify, entry) {
141 if (n->fn(n->state, cc->rbuf, &ca)) {
148 if (cc->rbuf->size>12 /* prevents traffic amplification */
149 && ((msgtype=get_uint32(cc->rbuf->start+8))
151 uint32_t source,dest;
152 /* Manufacture and send NAK packet */
153 source=get_uint32(cc->rbuf->start); /* Us */
154 dest=get_uint32(cc->rbuf->start+4); /* Them */
155 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
159 BUF_ASSERT_FREE(cc->rbuf);
167 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
169 struct udp *st=commst;
170 struct udp *cc=st; /* for now */
171 struct comm_notify_entry *n;
173 n=safe_malloc(sizeof(*n),"request_notify");
176 LIST_INSERT_HEAD(&cc->notify, n, entry);
179 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
181 struct udp *st=commst;
182 struct udp *cc=st; /* for now */
183 struct comm_notify_entry *n, *t;
186 LIST_FOREACH_SAFE(n, &cc->notify, entry, t) {
187 if (n->state==nst && n->fn==fn) {
188 LIST_REMOVE(n, entry);
194 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
195 const struct comm_addr *dest)
197 struct udp *st=commst;
198 struct udp *uc=st; /* for now */
199 struct udp *socks=st; /* for now */
203 sa=buf_prepend(buf,8);
204 if (dest->ia.sa.sa_family != AF_INET) {
206 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
207 iaddr_to_string(&dest->ia));
210 memcpy(sa,&dest->ia.sin.sin_addr,4);
212 memcpy(sa+6,&dest->ia.sin.sin_port,2);
213 sendto(socks->socks[0].fd,sa,buf->size+8,0,(struct sockaddr *)&uc->proxy,
215 buf_unprepend(buf,8);
218 bool_t allunsupported=True;
219 for (i=0; i<socks->n_socks; i++) {
220 if (dest->ia.sa.sa_family != socks->socks[i].addr.sa.sa_family)
221 /* no point even trying */
223 r=sendto(socks->socks[i].fd, buf->start, buf->size, 0,
224 &dest->ia.sa, iaddr_socklen(&dest->ia));
226 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
227 /* who knows what that error means? */
228 allunsupported=False;
230 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
236 static void udp_make_socket(struct udp *st, struct udpsock *us)
238 const union iaddr *addr=&us->addr;
239 struct udp *cc=st; /* for now */
240 struct udp *uc=st; /* for now */
241 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
243 fatal_perror("udp (%s:%d): socket",cc->loc.file,cc->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 cc->loc.file,cc->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 cc->loc.file,cc->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 cc->loc.file,cc->loc.line);
295 sprintf(portstr,"%04X",port);
296 argv[0]=uc->authbind;
299 argv[3]=(char*)addrfam;
302 execvp(uc->authbind,argv);
305 while (waitpid(c,&status,0)==-1) {
306 if (errno==EINTR) continue;
307 fatal_perror("udp (%s:%d): authbind",cc->loc.file,cc->loc.line);
309 if (WIFSIGNALED(status)) {
310 fatal("udp (%s:%d): authbind died on signal %d",cc->loc.file,
311 cc->loc.line, WTERMSIG(status));
313 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
314 fatal("udp (%s:%d): authbind died with status %d",cc->loc.file,
315 cc->loc.line, WEXITSTATUS(status));
318 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
319 fatal_perror("udp (%s:%d): bind",cc->loc.file,cc->loc.line);
324 static void udp_phase_hook(void *sst, uint32_t new_phase)
327 struct udp *socks=st; /* for now */
329 for (i=0; i<socks->n_socks; i++)
330 udp_make_socket(st,&socks->socks[i]);
332 register_for_poll(st,udp_beforepoll,udp_afterpoll,"udp");
335 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
346 st=safe_malloc(sizeof(*st),"udp_apply(st)");
347 struct udp *cc=st; /* for now */
348 struct udp *uc=st; /* for now */
349 struct udp *socks=st; /* for now */
351 cc->cl.description="udp";
354 cc->cl.interface=&cc->ops;
356 cc->ops.request_notify=request_notify;
357 cc->ops.release_notify=release_notify;
358 cc->ops.sendmsg=udp_sendmsg;
359 cc->ops.addr_to_string=addr_to_string;
361 LIST_INIT(&cc->notify);
363 item=list_elem(args,0);
364 if (!item || item->type!=t_dict) {
365 cfgfatal(cc->loc,"udp","first argument must be a dictionary\n");
369 int port=dict_read_number(d,"port",True,"udp",cc->loc,0);
371 union iaddr defaultaddrs[] = {
373 { .sin6 = { .sin6_family=AF_INET6,
374 .sin6_port=htons(port),
375 .sin6_addr=IN6ADDR_ANY_INIT } },
377 { .sin = { .sin_family=AF_INET,
378 .sin_port=htons(port),
379 .sin_addr= { .s_addr=INADDR_ANY } } }
382 caddrl=dict_lookup(d,"address");
383 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
384 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
385 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
388 for (i=0; i<socks->n_socks; i++) {
389 struct udpsock *us=&socks->socks[i];
391 if (!list_length(caddrl)) {
392 us->addr=defaultaddrs[i];
394 text2iaddr(list_elem(caddrl,i),port,
400 cc->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",cc->loc);
401 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
402 l=dict_lookup(d,"proxy");
406 uc->proxy.sa.sa_family=AF_INET;
408 if (!item || item->type!=t_string) {
409 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
411 a=string_item_to_ipaddr(item,"proxy");
412 uc->proxy.sin.sin_addr.s_addr=htonl(a);
414 if (!item || item->type!=t_number) {
415 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
417 uc->proxy.sin.sin_port=htons(item->data.number);
420 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
422 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
424 return new_closure(&cc->cl);
427 void udp_module(dict_t *dict)
429 add_closure(dict,"udp",udp_apply);