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