polypath: Provide polypath comm module
[secnet] / polypath.c
1 /* polypath
2 * send/receive module for secnet
3 * for multi-route setups */
4
5 #include "secnet.h"
6 #include "util.h"
7 #include "unaligned.h"
8 #include "comm-common.h"
9
10 #include <adns.h>
11 #include <ctype.h>
12
13 #ifdef CONFIG_IPV6
14
15 static comm_sendmsg_fn polypath_sendmsg;
16
17 struct interf {
18 char *name; /* from malloc */
19 struct udpsocks socks;
20 bool_t experienced_xmit_noaf[MAX_AF+1];
21 LIST_ENTRY(interf) entry;
22 };
23
24 struct polypath {
25 struct udpcommon uc;
26 int max_interfs;
27 const char *const *ifname_pats;
28 const char *const *monitor_command;
29 bool_t permit_loopback;
30 LIST_HEAD(,interf) interfs;
31 struct buffer_if lbuf;
32 int monitor_fd;
33 pid_t monitor_pid;
34 };
35
36 #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc
37
38 static const char *const default_loopback_ifname_pats[] = {
39 "!lo", 0
40 };
41 static const char *const default_ifname_pats[] = {
42 "!tun*","!tap*","!sl*","!userv*", "*", 0
43 };
44
45 static const char *const default_monitor_command[] = {
46 0
47 };
48
49 static const char *polypath_addr_to_string(void *commst,
50 const struct comm_addr *ca)
51 {
52 static char sbuf[100];
53
54 snprintf(sbuf, sizeof(sbuf), "polypath:%s",
55 iaddr_to_string(&ca->ia));
56 return sbuf;
57 }
58
59 static bool_t ifname_search_pats(struct polypath *st, struct cloc loc,
60 const char *ifname, bool_t *want_io,
61 const char *const *pats) {
62 /* Returns True iff we found a list entry, in which case *want_io
63 * is set to the sense of that entry. Otherwise *want_io is set
64 * to the sense of the last entry, or unchanged if there were no pats. */
65 if (!pats)
66 return False;
67 const char *const *pati;
68 for (pati=pats; *pati; pati++) {
69 const char *pat=*pati;
70 if (*pat=='!') { *want_io=False; pat++; }
71 else if (*pat=='+') { *want_io=True; pat++; }
72 else if (*pat=='*' || isalnum((unsigned char)*pat)) { *want_io=True; }
73 else cfgfatal(loc,"polypath","invalid interface name pattern `%s'",pat);
74 int match=fnmatch(pat,ifname,0);
75 if (match==0) return True;
76 if (match!=FNM_NOMATCH)
77 cfgfatal(loc,"polypath","fnmatch failed! (pattern `%s')",pat);
78 }
79 return False;
80 }
81
82 static bool_t ifname_wanted(struct polypath *st, struct cloc loc,
83 const char *ifname) {
84 bool_t want=False; /* pretend an empty cfg ends with !<doesn'tmatch> */
85 if (ifname_search_pats(st,loc,ifname,&want, st->ifname_pats))
86 return want;
87 if (want) /* last pattern was positive, do not search default */
88 return False;
89 if (!st->permit_loopback &&
90 ifname_search_pats(st,loc,ifname,&want, default_loopback_ifname_pats))
91 return want;
92 if (ifname_search_pats(st,loc,ifname,&want, default_ifname_pats))
93 return want;
94 abort();
95 }
96
97 static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
98 int *timeout_io)
99 {
100 struct polypath *st=state;
101 BEFOREPOLL_WANT_FDS(1);
102 fds[0].fd=st->monitor_fd;
103 fds[0].events=POLLIN;
104 return 0;
105 }
106
107 static void dump_ppml(struct polypath *st, const char *orgl)
108 {
109 #ifdef POLYPATH_DEBUG
110 struct interf *interf;
111 if (orgl)
112 lg_perror(LG,M_DEBUG,0, "polypath monitor line `%s'",orgl);
113 LIST_FOREACH(interf, &st->interfs, entry) {
114 lg_perror(LG,M_DEBUG,0, " polypath interface `%s', nsocks=%d",
115 interf->name, interf->socks.n_socks);
116 int i;
117 for (i=0; i<interf->socks.n_socks; i++) {
118 struct udpsock *us=&interf->socks.socks[i];
119 lg_perror(LG,M_DEBUG,0, " polypath sock fd=%d addr=%s",
120 us->fd, iaddr_to_string(&us->addr));
121 }
122 }
123 #endif
124 }
125
126 static void bad(struct polypath *st, char *l, char *undospace,
127 const char *m, int ev)
128 {
129 if (undospace)
130 *undospace=' ';
131 lg_perror(LG,M_WARNING,ev,
132 "error processing polypath state change: %s"
133 " (while processing `%s')",
134 m,l);
135 }
136
137 static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen)
138 {
139 assert(prefixlen>0);
140 assert(prefixlen<=32);
141 uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1);
142 assert(!(prefix & ~mask));
143 return (word & mask) == prefix;
144 }
145
146 static void polypath_process_monitor_line(struct polypath *st, char *orgl)
147 {
148 struct interf *interf=0;
149 struct udpcommon *uc=&st->uc;
150 char *undospace=0;
151 char *l=orgl;
152 struct udpsock *us=0;
153
154 dump_ppml(st,orgl);
155
156 #define BAD(m) do{ bad(st,orgl,undospace,m,0); goto out; }while(0)
157 #define BADE(m,ev) do{ bad(st,orgl,undospace,m,ev); goto out; }while(0)
158
159 bool_t add;
160 int c=*l++;
161 if (c=='+') add=True;
162 else if (c=='-') add=False;
163 else BAD("bad +/-");
164
165 int proto;
166 c=*l++;
167 if (c=='4') proto=AF_INET;
168 else if (c=='6') proto=AF_INET6;
169 else BAD("bad proto");
170
171 char *space=strchr(l,' ');
172 if (!space) BAD("no first space");
173 const char *ifname=space+1;
174
175 space=strchr(ifname,' ');
176 if (!space) BAD("no second space");
177 const char *ifaddr=space+1;
178 *space=0;
179 undospace=space;
180
181 union iaddr ia;
182 FILLZERO(ia);
183 socklen_t salen=sizeof(ia);
184 int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly,
185 &ia.sa, &salen);
186 assert(r!=ENOSPC);
187 if (r) BADE("adns_text2addr",r);
188 if (ia.sa.sa_family!=proto) BAD("address family mismatch");
189
190 #define DONT(m) do{ \
191 if (add) \
192 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
193 goto out; \
194 }while(0)
195
196 if (!ifname_wanted(st,st->uc.cc.loc,ifname))
197 DONT("unwanted interface name");
198
199 switch (ia.sa.sa_family) {
200 case AF_INET6: {
201 const struct in6_addr *i6=&ia.sin6.sin6_addr;
202 #define DONTKIND(X,m) \
203 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m)
204 DONTKIND(UNSPECIFIED, "unspecified");
205 DONTKIND(MULTICAST , "multicast" );
206 DONTKIND(LINKLOCAL , "link local" );
207 DONTKIND(SITELOCAL , "site local" );
208 DONTKIND(V4MAPPED , "v4-mapped" );
209 if (!st->permit_loopback)
210 DONTKIND(LOOPBACK , "loopback" );
211 #undef DONTKIND
212 #define DONTMASK(w7x,w6x,prefixlen,m) \
213 if (matches32(get_uint32(i6->s6_addr), \
214 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
215 prefixlen)) \
216 DONT("IPv6 address is " m)
217 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
218 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
219 #undef DONTMASK
220 break;
221 }
222 case AF_INET: {
223 const uint32_t i4=htonl(ia.sin.sin_addr.s_addr);
224 if (i4==INADDR_ANY) DONT("IPv4 address is any/unspecified");
225 if (i4==INADDR_BROADCAST) DONT("IPv4 address is all hosts broadcast");
226 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
227 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
228 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
229 DONT("IPv4 address is " m); \
230 }while(0)
231 DONTMASK(169,254,0,0, 16, "link local");
232 DONTMASK(224, 0,0,0, 4, "multicast");
233 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
234 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
235 if (!st->permit_loopback)
236 DONTMASK(127, 0,0,0, 8, "loopback");
237 #undef DONTMASK
238 break;
239 }
240 default:
241 abort();
242 }
243
244 /* OK, process it */
245 int n_ifs=0;
246 LIST_FOREACH(interf,&st->interfs,entry) {
247 if (!strcmp(interf->name,ifname))
248 goto found_interf;
249 n_ifs++;
250 }
251 /* not found */
252 if (n_ifs==st->max_interfs) BAD("too many interfaces");
253 interf=malloc(sizeof(*interf));
254 if (!interf) BADE("malloc for new interface",errno);
255 interf->name=0;
256 interf->socks.n_socks=0;
257 FILLZERO(interf->experienced_xmit_noaf);
258 LIST_INSERT_HEAD(&st->interfs,interf,entry);
259 udp_socks_register(&st->uc,&interf->socks);
260 interf->name=strdup(ifname);
261 if (!interf->name) BADE("strdup interface name",errno);
262 found_interf:
263
264 if (add) {
265 if (interf->socks.n_socks == UDP_MAX_SOCKETS)
266 BAD("too many addresses on this interface");
267 struct udpsock *us=&interf->socks.socks[interf->socks.n_socks];
268 us->fd=-1;
269 COPY_OBJ(us->addr,ia);
270 bool_t ok=udp_make_socket(&st->uc,us,M_WARNING);
271 if (!ok) BAD("unable to set up socket");
272 r=setsockopt(us->fd,SOL_SOCKET,SO_BINDTODEVICE,
273 ifname,strlen(ifname)+1);
274 if (r) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno);
275 interf->socks.n_socks++;
276 us=0; /* do not destroy this socket during `out' */
277 lg_perror(LG,M_INFO,0,"using %s %s",ifname,ifaddr);
278 } else {
279 int i;
280 for (i=0; i<interf->socks.n_socks; i++)
281 if (!memcmp(&interf->socks.socks[i].addr,&ia,sizeof(ia)))
282 goto address_remove_found;
283 BAD("address to remove not found");
284 address_remove_found:
285 lg_perror(LG,M_INFO,0,"removed %s %s",ifname,ifaddr);
286 udp_destroy_socket(&st->uc,&interf->socks.socks[i]);
287 interf->socks.socks[i]=
288 interf->socks.socks[--interf->socks.n_socks];
289 }
290
291 out:
292 if (us)
293 udp_destroy_socket(uc,us);
294 if (interf && !interf->socks.n_socks) {
295 udp_socks_deregister(&st->uc,&interf->socks);
296 LIST_REMOVE(interf,entry);
297 free(interf->name);
298 free(interf);
299 }
300
301 dump_ppml(st,0);
302
303 #undef BAD
304 #undef BADE
305 #undef DONT
306 }
307
308 static void polypath_afterpoll(void *state, struct pollfd *fds, int nfds)
309 {
310 struct polypath *st=state;
311 enum async_linebuf_result alr;
312 const char *emsg;
313 int status;
314
315 if (nfds<1) return;
316
317 while ((alr=async_linebuf_read(fds,&st->lbuf,&emsg)) == async_linebuf_ok)
318 polypath_process_monitor_line(st,st->lbuf.base);
319
320 if (alr==async_linebuf_nothing)
321 return;
322
323 assert(st->monitor_pid);
324
325 pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG);
326 if (gotpid==st->monitor_pid) {
327 st->monitor_pid=0;
328 lg_exitstatus(LG,M_FATAL,status,"interface monitor");
329 } else if (gotpid<0)
330 lg_perror(LG,M_ERR,errno,"unable to reap interface monitor");
331 else
332 assert(gotpid==0);
333
334 if (alr==async_linebuf_eof)
335 lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor");
336 else
337 lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg);
338
339 assert(!"not reached");
340 }
341
342 /* Actual udp packet sending work */
343 static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf,
344 const struct comm_addr *dest)
345 {
346 struct polypath *st=commst;
347 struct interf *interf;
348 bool_t allreasonable=True;
349 int af=dest->ia.sa.sa_family;
350
351 LIST_FOREACH(interf,&st->interfs,entry) {
352 int i;
353 bool_t attempted=False, reasonable=False;
354 for (i=0; i<interf->socks.n_socks; i++) {
355 struct udpsock *us=&interf->socks.socks[i];
356 if (af != us->addr.sa.sa_family)
357 continue;
358 attempted=True;
359 int r=sendto(us->fd,buf->start,buf->size,
360 0,&dest->ia.sa,iaddr_socklen(&dest->ia));
361 udp_sock_experienced(0,&st->uc, interf->name,us,
362 1,af, r,errno);
363 if (r>=0) {
364 reasonable=True;
365 break;
366 }
367 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
368 reasonable=True;
369 lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s",
370 interf->name,iaddr_to_string(&us->addr),
371 buf->size,iaddr_to_string(&dest->ia));
372 }
373 if (!attempted)
374 if (!interf->experienced_xmit_noaf[af]++)
375 lg_perror(LG,M_WARNING,0,
376 "%s has no suitable address to transmit %s",
377 interf->name, af_name(af));
378 allreasonable *= reasonable;
379 }
380 return allreasonable;
381 }
382
383 static void polypath_phase_startmonitor(void *sst, uint32_t newphase)
384 {
385 struct polypath *st=sst;
386 int pfds[2];
387
388 assert(!st->monitor_pid);
389 assert(st->monitor_fd<0);
390
391 pipe_cloexec(pfds);
392
393 pid_t pid=fork();
394 if (!pid) {
395 afterfork();
396 dup2(pfds[1],1);
397 execvp(st->monitor_command[0],(char**)st->monitor_command);
398 fprintf(stderr,"secnet: cannot execute %s: %s\n",
399 st->monitor_command[0], strerror(errno));
400 exit(-1);
401 }
402 if (pid<0)
403 fatal_perror("%s: failed to fork for interface monitor",
404 st->uc.cc.cl.description);
405
406 close(pfds[1]);
407 st->monitor_pid=pid;
408 st->monitor_fd=pfds[0];
409 setnonblock(st->monitor_fd);
410
411 register_for_poll(st,polypath_beforepoll,polypath_afterpoll,"polypath");
412 }
413
414 static void polypath_phase_shutdown(void *sst, uint32_t newphase)
415 {
416 struct polypath *st=sst;
417 if (st->monitor_pid) {
418 assert(st->monitor_pid>0);
419 kill(st->monitor_pid,SIGTERM);
420 }
421 }
422
423 static list_t *polypath_apply(closure_t *self, struct cloc loc,
424 dict_t *context, list_t *args)
425 {
426 struct polypath *st;
427
428 COMM_APPLY(st,&st->uc.cc,polypath_,"polypath",loc);
429 COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args);
430 UDP_APPLY_STANDARD(st,&st->uc,"polypath");
431
432 struct udpcommon *uc=&st->uc;
433 struct commcommon *cc=&uc->cc;
434
435 st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3);
436
437 st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath",
438 cc->loc,0);
439 st->permit_loopback=0; /* ifname_wanted reads this */
440 ifname_wanted(st,st->uc.cc.loc," "); /* try to check each pattern */
441
442 st->monitor_command=dict_read_string_array(d,"monitor-command",False,
443 "polypath",cc->loc, default_monitor_command);
444 if (!st->monitor_command[0])
445 cfgfatal(loc,"polypath","no polypath interface monitor-command"
446 " (polypath unsupported on this platform?)\n");
447
448 st->permit_loopback=dict_read_bool(d,"permit-loopback",False,
449 "polypath",cc->loc,False);
450
451 LIST_INIT(&st->interfs);
452 buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100);
453 BUF_ALLOC(&st->lbuf,"polypath lbuf");
454
455 st->monitor_fd=-1;
456 st->monitor_pid=0;
457
458 add_hook(PHASE_RUN, polypath_phase_startmonitor,st);
459 add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st);
460
461 return new_closure(&cc->cl);
462 }
463
464 #endif /* CONFIG_IPV6 */
465
466 void polypath_module(dict_t *dict)
467 {
468 #ifdef CONFIG_IPV6
469 add_closure(dict,"polypath",polypath_apply);
470 #endif /* CONFIG_IPV6 */
471 }