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 MAX_SOCKETS 3 /* 2 ought to do really */
52 struct udpsock socks[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 static char sbuf[100];
63 int ix=ca->ix>=0 ? ca->ix : 0;
65 assert(ix>=0 && ix<st->n_socks);
66 snprintf(sbuf, sizeof(sbuf), "udp:%d:%s-%s",
68 iaddr_to_string(&st->socks[ix].addr),
69 iaddr_to_string(&ca->ia));
73 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
78 BEFOREPOLL_WANT_FDS(st->n_socks);
79 for (i=0; i<st->n_socks; i++) {
80 fds[i].fd=st->socks[i].fd;
86 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
91 struct comm_notify_entry *n;
96 for (i=0; i<st->n_socks; i++) {
97 if (i>=nfds) continue;
98 if (!(fds[i].revents & POLLIN)) continue;
99 assert(fds[i].fd == st->socks[i].fd);
100 int fd=st->socks[i].fd;
103 fromlen=sizeof(from);
104 BUF_ASSERT_FREE(st->rbuf);
105 BUF_ALLOC(st->rbuf,"udp_afterpoll");
106 buffer_init(st->rbuf,calculate_max_start_pad());
107 rv=recvfrom(fd, st->rbuf->start,
108 buf_remaining_space(st->rbuf),
109 0, (struct sockaddr *)&from, &fromlen);
113 /* Check that the packet came from our poxy server;
114 we shouldn't be contacted directly by anybody else
115 (since they can trivially forge source addresses) */
116 if (!iaddr_equal(&from,&st->proxy)) {
117 Message(M_INFO,"udp: received packet that's not "
122 /* proxy protocol supports ipv4 transport only */
124 from.sa.sa_family=AF_INET;
125 memcpy(&from.sin.sin_addr,buf_unprepend(st->rbuf,4),4);
126 buf_unprepend(st->rbuf,2);
127 memcpy(&from.sin.sin_port,buf_unprepend(st->rbuf,2),2);
135 LIST_FOREACH(n, &st->notify, entry) {
136 if (n->fn(n->state, st->rbuf, &ca)) {
143 if (st->rbuf->size>12 /* prevents traffic amplification */
144 && ((msgtype=get_uint32(st->rbuf->start+8))
146 uint32_t source,dest;
147 /* Manufacture and send NAK packet */
148 source=get_uint32(st->rbuf->start); /* Us */
149 dest=get_uint32(st->rbuf->start+4); /* Them */
150 send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted");
154 BUF_ASSERT_FREE(st->rbuf);
162 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
164 struct udp *st=commst;
165 struct comm_notify_entry *n;
167 n=safe_malloc(sizeof(*n),"request_notify");
170 LIST_INSERT_HEAD(&st->notify, n, entry);
173 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
175 struct udp *st=commst;
176 struct comm_notify_entry *n, *t;
179 LIST_FOREACH_SAFE(n, &st->notify, entry, t) {
180 if (n->state==nst && n->fn==fn) {
181 LIST_REMOVE(n, entry);
187 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
188 const struct comm_addr *dest)
190 struct udp *st=commst;
194 sa=buf_prepend(buf,8);
195 if (dest->ia.sa.sa_family != AF_INET) {
197 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
198 iaddr_to_string(&dest->ia));
201 memcpy(sa,&dest->ia.sin.sin_addr,4);
203 memcpy(sa+6,&dest->ia.sin.sin_port,2);
204 sendto(st->socks[0].fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
206 buf_unprepend(buf,8);
209 bool_t allunsupported=True;
210 for (i=0; i<st->n_socks; i++) {
211 if (dest->ia.sa.sa_family != st->socks[i].addr.sa.sa_family)
212 /* no point even trying */
214 r=sendto(st->socks[i].fd, buf->start, buf->size, 0,
215 &dest->ia.sa, iaddr_socklen(&dest->ia));
217 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
218 /* who knows what that error means? */
219 allunsupported=False;
221 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
227 static void udp_make_socket(struct udp *st, struct udpsock *us)
229 const union iaddr *addr=&us->addr;
230 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
232 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
234 if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
235 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
236 st->loc.file,st->loc.line);
240 if (addr->sa.sa_family==AF_INET6) {
243 socklen_t optlen=sizeof(optval);
244 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
245 if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)",
246 st->loc.file,st->loc.line);
254 /* XXX this fork() and waitpid() business needs to be hidden
255 in some system-specific library functions. */
258 fatal_perror("udp_phase_hook: fork() for authbind");
261 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 st->loc.file,st->loc.line);
284 sprintf(portstr,"%04X",port);
285 argv[0]=st->authbind;
288 argv[3]=(char*)addrfam;
291 execvp(st->authbind,argv);
294 while (waitpid(c,&status,0)==-1) {
295 if (errno==EINTR) continue;
296 fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
298 if (WIFSIGNALED(status)) {
299 fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
300 st->loc.line, WTERMSIG(status));
302 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
303 fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
304 st->loc.line, WEXITSTATUS(status));
307 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
308 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
313 static void udp_phase_hook(void *sst, uint32_t new_phase)
317 for (i=0; i<st->n_socks; i++)
318 udp_make_socket(st,&st->socks[i]);
320 register_for_poll(st,udp_beforepoll,udp_afterpoll,"udp");
323 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
334 st=safe_malloc(sizeof(*st),"udp_apply(st)");
336 st->cl.description="udp";
339 st->cl.interface=&st->ops;
341 st->ops.request_notify=request_notify;
342 st->ops.release_notify=release_notify;
343 st->ops.sendmsg=udp_sendmsg;
344 st->ops.addr_to_string=addr_to_string;
346 LIST_INIT(&st->notify);
348 item=list_elem(args,0);
349 if (!item || item->type!=t_dict) {
350 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
354 int port=dict_read_number(d,"port",True,"udp",st->loc,0);
356 union iaddr defaultaddrs[] = {
358 { .sin6 = { .sin6_family=AF_INET6,
359 .sin6_port=htons(port),
360 .sin6_addr=IN6ADDR_ANY_INIT } },
362 { .sin = { .sin_family=AF_INET,
363 .sin_port=htons(port),
364 .sin_addr= { .s_addr=INADDR_ANY } } }
367 caddrl=dict_lookup(d,"address");
368 st->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
369 if (st->n_socks<=0 || st->n_socks>MAX_SOCKETS)
370 cfgfatal(st->loc,"udp","`address' must be 1..%d addresses",
373 for (i=0; i<st->n_socks; i++) {
374 struct udpsock *us=&st->socks[i];
376 if (!list_length(caddrl)) {
377 us->addr=defaultaddrs[i];
379 text2iaddr(list_elem(caddrl,i),port,
385 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
386 st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
387 l=dict_lookup(d,"proxy");
391 st->proxy.sa.sa_family=AF_INET;
393 if (!item || item->type!=t_string) {
394 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
396 a=string_item_to_ipaddr(item,"proxy");
397 st->proxy.sin.sin_addr.s_addr=htonl(a);
399 if (!item || item->type!=t_number) {
400 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
402 st->proxy.sin.sin_port=htons(item->data.number);
405 update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0);
407 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
409 return new_closure(&st->cl);
412 void udp_module(dict_t *dict)
414 add_closure(dict,"udp",udp_apply);