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