comm: Break out common code in comm
[secnet] / udp.c
CommitLineData
2fe58dfd
SE
1/* UDP send/receive module for secnet */
2
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
6 * yet).
7 *
2fe58dfd
SE
8 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
10
8689b3a9 11#include "secnet.h"
2fe58dfd
SE
12#include <stdio.h>
13#include <unistd.h>
14#include <fcntl.h>
15#include <string.h>
16#include <errno.h>
2fe58dfd 17#include <sys/socket.h>
b2a56f7c 18#include <sys/wait.h>
5edf478f
IJ
19#include <netinet/in.h>
20#include <arpa/inet.h>
2fe58dfd 21#include "util.h"
dd9209d1 22#include "magic.h"
794f2398 23#include "unaligned.h"
ff05a229 24#include "ipaddr.h"
136740e6 25#include "magic.h"
54d5ef00 26#include "comm-common.h"
2fe58dfd
SE
27
28static beforepoll_fn udp_beforepoll;
29static afterpoll_fn udp_afterpoll;
2fe58dfd
SE
30static comm_sendmsg_fn udp_sendmsg;
31
7d31df0e 32#define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
08b62a6c
IJ
33
34struct udpsock {
35 union iaddr addr;
36 int fd;
37};
38
2fe58dfd 39struct udp {
54d5ef00 40 struct commcommon cc;
08b62a6c 41 int n_socks;
7d31df0e 42 struct udpsock socks[UDP_MAX_SOCKETS];
b2a56f7c 43 string_t authbind;
ff05a229 44 bool_t use_proxy;
a32d56fb 45 union iaddr proxy;
2fe58dfd
SE
46};
47
08b62a6c
IJ
48/*
49 * Re comm_addr.ix: This field allows us to note in the comm_addr
50 * which socket an incoming packet was received on. This is required
51 * for conveniently logging the actual source of a packet. But the ix
52 * does not formally form part of the address: it is not used when
53 * sending, nor when comparing two comm_addrs.
54 *
55 * The special value -1 means that the comm_addr was constructed by
56 * another module in secnet (eg the resolver), rather than being a
57 * description of the source of an incoming packet.
58 */
59
54d5ef00
IJ
60static const char *udp_addr_to_string(void *commst, const struct comm_addr *ca)
61{
5edf478f 62 struct udp *st=commst;
7d31df0e 63 struct udp *socks=st; /* for now */
5edf478f 64 static char sbuf[100];
08b62a6c 65 int ix=ca->ix>=0 ? ca->ix : 0;
5edf478f 66
7d31df0e 67 assert(ix>=0 && ix<socks->n_socks);
08b62a6c 68 snprintf(sbuf, sizeof(sbuf), "udp:%s%s-%s",
7d31df0e 69 iaddr_to_string(&socks->socks[ix].addr),
08b62a6c
IJ
70 ca->ix<0 ? "&" : "",
71 iaddr_to_string(&ca->ia));
5edf478f
IJ
72 return sbuf;
73}
74
2fe58dfd 75static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
90a39563 76 int *timeout_io)
2fe58dfd 77{
08b62a6c 78 int i;
2fe58dfd 79 struct udp *st=state;
7d31df0e
IJ
80 struct udp *socks=st; /* for now */
81 BEFOREPOLL_WANT_FDS(socks->n_socks);
82 for (i=0; i<socks->n_socks; i++) {
83 fds[i].fd=socks->socks[i].fd;
08b62a6c
IJ
84 fds[i].events=POLLIN;
85 }
2fe58dfd
SE
86 return 0;
87}
88
90a39563 89static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
2fe58dfd
SE
90{
91 struct udp *st=state;
54d5ef00 92 struct commcommon *cc=&st->cc;
7d31df0e 93 struct udp *socks=st; /* for now */
7d31df0e 94 struct udp *uc=st; /* for now */
a32d56fb 95 union iaddr from;
b1a0f651 96 socklen_t fromlen;
2fe58dfd
SE
97 bool_t done;
98 int rv;
08b62a6c 99 int i;
2fe58dfd 100
08b62a6c
IJ
101 for (i=0; i<st->n_socks; i++) {
102 if (i>=nfds) continue;
103 if (!(fds[i].revents & POLLIN)) continue;
7d31df0e
IJ
104 assert(fds[i].fd == socks->socks[i].fd);
105 int fd=socks->socks[i].fd;
2fe58dfd
SE
106 do {
107 fromlen=sizeof(from);
7d31df0e
IJ
108 BUF_ASSERT_FREE(cc->rbuf);
109 BUF_ALLOC(cc->rbuf,"udp_afterpoll");
110 buffer_init(cc->rbuf,calculate_max_start_pad());
111 rv=recvfrom(fd, cc->rbuf->start,
112 buf_remaining_space(cc->rbuf),
a32d56fb 113 0, &from.sa, &fromlen);
2fe58dfd 114 if (rv>0) {
7d31df0e
IJ
115 cc->rbuf->size=rv;
116 if (uc->use_proxy) {
ff05a229
SE
117 /* Check that the packet came from our poxy server;
118 we shouldn't be contacted directly by anybody else
119 (since they can trivially forge source addresses) */
7d31df0e 120 if (!iaddr_equal(&from,&uc->proxy)) {
ff05a229
SE
121 Message(M_INFO,"udp: received packet that's not "
122 "from the proxy\n");
7d31df0e 123 BUF_FREE(cc->rbuf);
ff05a229
SE
124 continue;
125 }
a32d56fb
IJ
126 /* proxy protocol supports ipv4 transport only */
127 from.sa.sa_family=AF_INET;
c1ddd026 128 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_addr,4);
7d31df0e 129 buf_unprepend(cc->rbuf,2);
c1ddd026 130 BUF_GET_BYTES(unprepend,cc->rbuf,&from.sin.sin_port,2);
ff05a229 131 }
8534d602 132 struct comm_addr ca;
7d31df0e 133 ca.comm=&cc->ops;
a32d56fb 134 ca.ia=from;
08b62a6c 135 ca.ix=i;
54d5ef00 136 done=comm_notify(&cc->notify, cc->rbuf, &ca);
2fe58dfd 137 if (!done) {
bf28fc73 138 uint32_t msgtype;
7d31df0e
IJ
139 if (cc->rbuf->size>12 /* prevents traffic amplification */
140 && ((msgtype=get_uint32(cc->rbuf->start+8))
bf28fc73
IJ
141 != LABEL_NAK)) {
142 uint32_t source,dest;
143 /* Manufacture and send NAK packet */
7d31df0e
IJ
144 source=get_uint32(cc->rbuf->start); /* Us */
145 dest=get_uint32(cc->rbuf->start+4); /* Them */
146 send_nak(&ca,source,dest,msgtype,cc->rbuf,"unwanted");
bf28fc73 147 }
7d31df0e 148 BUF_FREE(cc->rbuf);
2fe58dfd 149 }
7d31df0e 150 BUF_ASSERT_FREE(cc->rbuf);
2fe58dfd 151 } else {
7d31df0e 152 BUF_FREE(cc->rbuf);
2fe58dfd
SE
153 }
154 } while (rv>=0);
155 }
156}
157
2fe58dfd 158static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
a15faeb2 159 const struct comm_addr *dest)
2fe58dfd
SE
160{
161 struct udp *st=commst;
7d31df0e
IJ
162 struct udp *uc=st; /* for now */
163 struct udp *socks=st; /* for now */
ff05a229 164 uint8_t *sa;
2fe58dfd 165
7d31df0e 166 if (uc->use_proxy) {
3abd18e8 167 sa=buf_prepend(buf,8);
a32d56fb
IJ
168 if (dest->ia.sa.sa_family != AF_INET) {
169 Message(M_INFO,
170 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
171 iaddr_to_string(&dest->ia));
172 return False;
173 }
174 memcpy(sa,&dest->ia.sin.sin_addr,4);
ff05a229 175 memset(sa+4,0,4);
a32d56fb 176 memcpy(sa+6,&dest->ia.sin.sin_port,2);
7d31df0e
IJ
177 sendto(socks->socks[0].fd,sa,buf->size+8,0,&uc->proxy.sa,
178 iaddr_socklen(&uc->proxy));
3abd18e8 179 buf_unprepend(buf,8);
ff05a229 180 } else {
08b62a6c
IJ
181 int i,r;
182 bool_t allunsupported=True;
7d31df0e
IJ
183 for (i=0; i<socks->n_socks; i++) {
184 if (dest->ia.sa.sa_family != socks->socks[i].addr.sa.sa_family)
08b62a6c
IJ
185 /* no point even trying */
186 continue;
7d31df0e 187 r=sendto(socks->socks[i].fd, buf->start, buf->size, 0,
08b62a6c
IJ
188 &dest->ia.sa, iaddr_socklen(&dest->ia));
189 if (r>=0) return True;
190 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
191 /* who knows what that error means? */
192 allunsupported=False;
193 }
194 return !allunsupported; /* see doc for comm_sendmsg_fn in secnet.h */
ff05a229 195 }
2fe58dfd
SE
196
197 return True;
198}
199
08b62a6c 200static void udp_make_socket(struct udp *st, struct udpsock *us)
baa06aeb 201{
f164f167 202 const union iaddr *addr=&us->addr;
54d5ef00 203 struct commcommon *cc=&st->cc; /* for now */
7d31df0e 204 struct udp *uc=st; /* for now */
08b62a6c 205 us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
f164f167 206 if (us->fd<0) {
7d31df0e 207 fatal_perror("udp (%s:%d): socket",cc->loc.file,cc->loc.line);
baa06aeb 208 }
f164f167 209 if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
baa06aeb 210 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
7d31df0e 211 cc->loc.file,cc->loc.line);
baa06aeb 212 }
f164f167 213 setcloexec(us->fd);
08b62a6c
IJ
214#ifdef CONFIG_IPV6
215 if (addr->sa.sa_family==AF_INET6) {
216 int r;
217 int optval=1;
218 socklen_t optlen=sizeof(optval);
219 r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
220 if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)",
7d31df0e 221 cc->loc.file,cc->loc.line);
08b62a6c
IJ
222 }
223#endif
baa06aeb 224
7d31df0e 225 if (uc->authbind) {
b2a56f7c
SE
226 pid_t c;
227 int status;
228
229 /* XXX this fork() and waitpid() business needs to be hidden
230 in some system-specific library functions. */
231 c=fork();
232 if (c==-1) {
233 fatal_perror("udp_phase_hook: fork() for authbind");
234 }
235 if (c==0) {
3ff31eda
IJ
236 char *argv[5], addrstr[33], portstr[5];
237 const char *addrfam;
238 int port;
f164f167 239 switch (addr->sa.sa_family) {
a32d56fb 240 case AF_INET:
f164f167 241 sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
3ff31eda
IJ
242 port=addr->sin.sin_port;
243 addrfam=NULL;
a32d56fb 244 break;
3ff31eda
IJ
245#ifdef CONFIG_IPV6
246 case AF_INET6: {
247 int i;
248 for (i=0; i<16; i++)
249 sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
250 port=addr->sin6.sin6_port;
251 addrfam="6";
252 break;
253 }
254#endif /*CONFIG_IPV6*/
a32d56fb
IJ
255 default:
256 fatal("udp (%s:%d): unsupported address family for authbind",
7d31df0e 257 cc->loc.file,cc->loc.line);
a32d56fb 258 }
3ff31eda 259 sprintf(portstr,"%04X",port);
7d31df0e 260 argv[0]=uc->authbind;
3b83c932
SE
261 argv[1]=addrstr;
262 argv[2]=portstr;
3ff31eda
IJ
263 argv[3]=(char*)addrfam;
264 argv[4]=NULL;
f164f167 265 dup2(us->fd,0);
7d31df0e 266 execvp(uc->authbind,argv);
3b83c932 267 _exit(255);
b2a56f7c 268 }
3b83c932
SE
269 while (waitpid(c,&status,0)==-1) {
270 if (errno==EINTR) continue;
7d31df0e 271 fatal_perror("udp (%s:%d): authbind",cc->loc.file,cc->loc.line);
b2a56f7c 272 }
3b83c932 273 if (WIFSIGNALED(status)) {
7d31df0e
IJ
274 fatal("udp (%s:%d): authbind died on signal %d",cc->loc.file,
275 cc->loc.line, WTERMSIG(status));
3b83c932
SE
276 }
277 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
7d31df0e
IJ
278 fatal("udp (%s:%d): authbind died with status %d",cc->loc.file,
279 cc->loc.line, WEXITSTATUS(status));
3b83c932 280 }
b2a56f7c 281 } else {
08b62a6c 282 if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
7d31df0e 283 fatal_perror("udp (%s:%d): bind",cc->loc.file,cc->loc.line);
b2a56f7c 284 }
baa06aeb 285 }
f164f167 286}
baa06aeb 287
f164f167
IJ
288static void udp_phase_hook(void *sst, uint32_t new_phase)
289{
290 struct udp *st=sst;
7d31df0e 291 struct udp *socks=st; /* for now */
08b62a6c 292 int i;
7d31df0e
IJ
293 for (i=0; i<socks->n_socks; i++)
294 udp_make_socket(st,&socks->socks[i]);
08b62a6c 295
32fc582f 296 register_for_poll(st,udp_beforepoll,udp_afterpoll,"udp");
baa06aeb
SE
297}
298
2fe58dfd
SE
299static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
300 list_t *args)
301{
302 struct udp *st;
08b62a6c 303 list_t *caddrl;
ff05a229
SE
304 list_t *l;
305 uint32_t a;
08b62a6c 306 int i;
2fe58dfd 307
54d5ef00
IJ
308 COMM_APPLY(st,&st->cc,udp_,"udp",loc);
309 struct commcommon *cc=&st->cc; /* for now */
7d31df0e
IJ
310 struct udp *uc=st; /* for now */
311 struct udp *socks=st; /* for now */
54d5ef00 312
7d31df0e 313 uc->use_proxy=False;
2fe58dfd 314
54d5ef00 315 COMM_APPLY_STANDARD(st,&st->cc,"udp",args);
2fe58dfd 316
7d31df0e 317 int port=dict_read_number(d,"port",True,"udp",cc->loc,0);
08b62a6c
IJ
318
319 union iaddr defaultaddrs[] = {
320#ifdef CONFIG_IPV6
321 { .sin6 = { .sin6_family=AF_INET6,
322 .sin6_port=htons(port),
323 .sin6_addr=IN6ADDR_ANY_INIT } },
324#endif
325 { .sin = { .sin_family=AF_INET,
326 .sin_port=htons(port),
327 .sin_addr= { .s_addr=INADDR_ANY } } }
328 };
329
330 caddrl=dict_lookup(d,"address");
7d31df0e
IJ
331 socks->n_socks=caddrl ? list_length(caddrl) : (int)ARRAY_SIZE(defaultaddrs);
332 if (socks->n_socks<=0 || socks->n_socks>UDP_MAX_SOCKETS)
333 cfgfatal(cc->loc,"udp","`address' must be 1..%d addresses",
334 UDP_MAX_SOCKETS);
08b62a6c 335
7d31df0e
IJ
336 for (i=0; i<socks->n_socks; i++) {
337 struct udpsock *us=&socks->socks[i];
08b62a6c
IJ
338 if (!list_length(caddrl)) {
339 us->addr=defaultaddrs[i];
340 } else {
341 string_item_to_iaddr(list_elem(caddrl,i),port,&us->addr,"udp");
342 }
343 us->fd=-1;
344 }
345
7d31df0e 346 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
ff05a229
SE
347 l=dict_lookup(d,"proxy");
348 if (l) {
7d31df0e
IJ
349 uc->use_proxy=True;
350 uc->proxy.sa.sa_family=AF_INET;
c649eafe
IJ
351 item=list_elem(l,0);
352 if (!item || item->type!=t_string) {
7d31df0e 353 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
ff05a229 354 }
c649eafe 355 a=string_item_to_ipaddr(item,"proxy");
7d31df0e 356 uc->proxy.sin.sin_addr.s_addr=htonl(a);
c649eafe
IJ
357 item=list_elem(l,1);
358 if (!item || item->type!=t_number) {
7d31df0e 359 cfgfatal(cc->loc,"udp","proxy must supply ""addr"",port\n");
ff05a229 360 }
7d31df0e 361 uc->proxy.sin.sin_port=htons(item->data.number);
ff05a229 362 }
2fe58dfd 363
7d31df0e 364 update_max_start_pad(&comm_max_start_pad, uc->use_proxy ? 8 : 0);
3abd18e8 365
baa06aeb 366 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
2fe58dfd 367
7d31df0e 368 return new_closure(&cc->cl);
2fe58dfd
SE
369}
370
2fe58dfd
SE
371void udp_module(dict_t *dict)
372{
373 add_closure(dict,"udp",udp_apply);
374}