2 * send/receive module for secnet
3 * for multi-route setups */
8 #include "comm-common.h"
15 static comm_sendmsg_fn polypath_sendmsg;
18 char *name; /* from malloc */
19 struct udpsocks socks;
20 bool_t experienced_xmit_noaf[MAX_AF+1];
21 LIST_ENTRY(interf) entry;
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;
36 #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc
38 static const char *const default_loopback_ifname_pats[] = {
41 static const char *const default_ifname_pats[] = {
42 "!tun*","!tap*","!sl*","!userv*", "*", 0
45 static const char *const default_monitor_command[] = {
47 DATAROOTDIR "/secnet/" "polypath-interface-monitor-linux", 0
53 static const char *polypath_addr_to_string(void *commst,
54 const struct comm_addr *ca)
56 static char sbuf[100];
58 snprintf(sbuf, sizeof(sbuf), "polypath:%s",
59 iaddr_to_string(&ca->ia));
63 static bool_t ifname_search_pats(struct polypath *st, struct cloc loc,
64 const char *ifname, bool_t *want_io,
65 const char *const *pats) {
66 /* Returns True iff we found a list entry, in which case *want_io
67 * is set to the sense of that entry. Otherwise *want_io is set
68 * to the sense of the last entry, or unchanged if there were no pats. */
71 const char *const *pati;
72 for (pati=pats; *pati; pati++) {
73 const char *pat=*pati;
74 if (*pat=='!') { *want_io=False; pat++; }
75 else if (*pat=='+') { *want_io=True; pat++; }
76 else if (*pat=='*' || isalnum((unsigned char)*pat)) { *want_io=True; }
77 else cfgfatal(loc,"polypath","invalid interface name pattern `%s'",pat);
78 int match=fnmatch(pat,ifname,0);
79 if (match==0) return True;
80 if (match!=FNM_NOMATCH)
81 cfgfatal(loc,"polypath","fnmatch failed! (pattern `%s')",pat);
86 static bool_t ifname_wanted(struct polypath *st, struct cloc loc,
88 bool_t want=False; /* pretend an empty cfg ends with !<doesn'tmatch> */
89 if (ifname_search_pats(st,loc,ifname,&want, st->ifname_pats))
91 if (want) /* last pattern was positive, do not search default */
93 if (!st->permit_loopback &&
94 ifname_search_pats(st,loc,ifname,&want, default_loopback_ifname_pats))
96 if (ifname_search_pats(st,loc,ifname,&want, default_ifname_pats))
101 static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
104 struct polypath *st=state;
105 BEFOREPOLL_WANT_FDS(1);
106 fds[0].fd=st->monitor_fd;
107 fds[0].events=POLLIN;
111 static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen)
114 assert(prefixlen<=32);
115 uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1);
116 assert(!(prefix & ~mask));
117 return (word & mask) == prefix;
120 /* These macros expect
121 * bad_fn_type *const bad;
126 #define BAD(m) do{ bad(st,badctx,m,0); goto out; }while(0)
127 #define BADE(m,ev) do{ bad(st,badctx,m,ev); goto out; }while(0)
128 typedef void bad_fn_type(struct polypath *st, void *badctx,
129 const char* m, int ev);
131 typedef void polypath_ppml_callback_type(struct polypath *st,
132 bad_fn_type *bad, void *badctx,
133 bool_t add, const char *ifname, const char *ifaddr,
134 const union iaddr *ia, int fd /* -1 if none yet */);
136 struct ppml_bad_ctx {
141 static void ppml_bad(struct polypath *st, void *badctx, const char *m, int ev)
143 struct ppml_bad_ctx *bc=badctx;
145 *(bc->undospace)=' ';
146 lg_perror(LG,M_WARNING,ev,
147 "error processing polypath state change: %s"
148 " (while processing `%s')",
152 static void polypath_process_monitor_line(struct polypath *st, char *orgl,
153 polypath_ppml_callback_type *callback)
154 /* always calls callback with fd==-1 */
156 struct udpcommon *uc=&st->uc;
158 bad_fn_type (*const bad)=ppml_bad;
159 struct ppml_bad_ctx badctx[1]={{
166 if (c=='+') add=True;
167 else if (c=='-') add=False;
172 if (c=='4') proto=AF_INET;
173 else if (c=='6') proto=AF_INET6;
174 else BAD("bad proto");
176 char *space=strchr(l,' ');
177 if (!space) BAD("no first space");
178 const char *ifname=space+1;
180 space=strchr(ifname,' ');
181 if (!space) BAD("no second space");
182 const char *ifaddr=space+1;
184 badctx->undospace=space;
188 socklen_t salen=sizeof(ia);
189 int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly,
192 if (r) BADE("adns_text2addr",r);
193 if (ia.sa.sa_family!=proto) BAD("address family mismatch");
195 #define DONT(m) do{ \
197 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
201 if (!ifname_wanted(st,st->uc.cc.loc,ifname))
202 DONT("unwanted interface name");
204 switch (ia.sa.sa_family) {
206 const struct in6_addr *i6=&ia.sin6.sin6_addr;
207 #define DONTKIND(X,m) \
208 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m)
209 DONTKIND(UNSPECIFIED, "unspecified");
210 DONTKIND(MULTICAST , "multicast" );
211 DONTKIND(LINKLOCAL , "link local" );
212 DONTKIND(SITELOCAL , "site local" );
213 DONTKIND(V4MAPPED , "v4-mapped" );
214 if (!st->permit_loopback)
215 DONTKIND(LOOPBACK , "loopback" );
217 #define DONTMASK(w7x,w6x,prefixlen,m) \
218 if (matches32(get_uint32(i6->s6_addr), \
219 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
221 DONT("IPv6 address is " m)
222 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
223 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
228 const uint32_t i4=htonl(ia.sin.sin_addr.s_addr);
229 if (i4==INADDR_ANY) DONT("IPv4 address is any/unspecified");
230 if (i4==INADDR_BROADCAST) DONT("IPv4 address is all hosts broadcast");
231 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
232 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
233 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
234 DONT("IPv4 address is " m); \
236 DONTMASK(169,254,0,0, 16, "link local");
237 DONTMASK(224, 0,0,0, 4, "multicast");
238 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
239 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
240 if (!st->permit_loopback)
241 DONTMASK(127, 0,0,0, 8, "loopback");
252 callback(st, bad,badctx, add,ifname,ifaddr,&ia,-1);
257 static void dump_pria(struct polypath *st, const char *ifname)
259 #ifdef POLYPATH_DEBUG
260 struct interf *interf;
262 lg_perror(LG,M_DEBUG,0, "polypath record ifaddr `%s'",ifname);
263 LIST_FOREACH(interf, &st->interfs, entry) {
264 lg_perror(LG,M_DEBUG,0, " polypath interface `%s', nsocks=%d",
265 interf->name, interf->socks.n_socks);
267 for (i=0; i<interf->socks.n_socks; i++) {
268 struct udpsock *us=&interf->socks.socks[i];
269 lg_perror(LG,M_DEBUG,0, " polypath sock fd=%d addr=%s",
270 us->fd, iaddr_to_string(&us->addr));
276 static bool_t polypath_make_socket(struct polypath *st,
277 bad_fn_type *bad, void *badctx,
278 struct udpsock *us, const char *ifname)
279 /* on error exit has called bad; might leave us->fd as -1 */
283 bool_t ok=udp_make_socket(&st->uc,us,M_WARNING);
284 if (!ok) BAD("unable to set up socket");
285 int r=setsockopt(us->fd,SOL_SOCKET,SO_BINDTODEVICE,
286 ifname,strlen(ifname)+1);
287 if (r) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno);
294 static void polypath_record_ifaddr(struct polypath *st,
295 bad_fn_type *bad, void *badctx,
296 bool_t add, const char *ifname,
298 const union iaddr *ia, int fd)
300 struct udpcommon *uc=&st->uc;
301 struct interf *interf=0;
302 struct udpsock *us=0;
304 dump_pria(st,ifname);
307 LIST_FOREACH(interf,&st->interfs,entry) {
308 if (!strcmp(interf->name,ifname))
313 if (n_ifs==st->max_interfs) BAD("too many interfaces");
314 interf=malloc(sizeof(*interf));
315 if (!interf) BADE("malloc for new interface",errno);
317 interf->socks.n_socks=0;
318 FILLZERO(interf->experienced_xmit_noaf);
319 LIST_INSERT_HEAD(&st->interfs,interf,entry);
320 udp_socks_register(&st->uc,&interf->socks);
321 interf->name=strdup(ifname);
322 if (!interf->name) BADE("strdup interface name",errno);
326 if (interf->socks.n_socks == UDP_MAX_SOCKETS)
327 BAD("too many addresses on this interface");
328 struct udpsock *us=&interf->socks.socks[interf->socks.n_socks];
330 COPY_OBJ(us->addr,*ia);
332 bool_t ok=polypath_make_socket(st,bad,badctx, us,ifname);
335 FILLZERO(us->experienced);
339 interf->socks.n_socks++;
340 us=0; /* do not destroy this socket during `out' */
341 lg_perror(LG,M_INFO,0,"using %s %s",ifname,ifaddr);
344 for (i=0; i<interf->socks.n_socks; i++)
345 if (!memcmp(&interf->socks.socks[i].addr,ia,sizeof(*ia)))
346 goto address_remove_found;
347 BAD("address to remove not found");
348 address_remove_found:
349 lg_perror(LG,M_INFO,0,"removed %s %s",ifname,ifaddr);
350 udp_destroy_socket(&st->uc,&interf->socks.socks[i]);
351 interf->socks.socks[i]=
352 interf->socks.socks[--interf->socks.n_socks];
357 udp_destroy_socket(uc,us);
360 if (interf && !interf->socks.n_socks) {
361 udp_socks_deregister(&st->uc,&interf->socks);
362 LIST_REMOVE(interf,entry);
370 static void polypath_afterpoll(void *state, struct pollfd *fds, int nfds)
372 struct polypath *st=state;
373 enum async_linebuf_result alr;
379 while ((alr=async_linebuf_read(fds,&st->lbuf,&emsg)) == async_linebuf_ok)
380 polypath_process_monitor_line(st,st->lbuf.base,
381 polypath_record_ifaddr);
383 if (alr==async_linebuf_nothing)
386 assert(st->monitor_pid);
388 pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG);
389 if (gotpid==st->monitor_pid) {
391 lg_exitstatus(LG,M_FATAL,status,"interface monitor");
393 lg_perror(LG,M_ERR,errno,"unable to reap interface monitor");
397 if (alr==async_linebuf_eof)
398 lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor");
400 lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg);
402 assert(!"not reached");
405 /* Actual udp packet sending work */
406 static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf,
407 const struct comm_addr *dest)
409 struct polypath *st=commst;
410 struct interf *interf;
411 bool_t allreasonable=True;
412 int af=dest->ia.sa.sa_family;
414 LIST_FOREACH(interf,&st->interfs,entry) {
416 bool_t attempted=False, reasonable=False;
417 for (i=0; i<interf->socks.n_socks; i++) {
418 struct udpsock *us=&interf->socks.socks[i];
419 if (af != us->addr.sa.sa_family)
422 int r=sendto(us->fd,buf->start,buf->size,
423 0,&dest->ia.sa,iaddr_socklen(&dest->ia));
424 udp_sock_experienced(0,&st->uc, interf->name,us,
430 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
432 lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s",
433 interf->name,iaddr_to_string(&us->addr),
434 buf->size,iaddr_to_string(&dest->ia));
437 if (!interf->experienced_xmit_noaf[af]++)
438 lg_perror(LG,M_WARNING,0,
439 "%s has no suitable address to transmit %s",
440 interf->name, af_name(af));
441 allreasonable *= reasonable;
443 return allreasonable;
446 static void polypath_phase_startmonitor(void *sst, uint32_t newphase)
448 struct polypath *st=sst;
451 assert(!st->monitor_pid);
452 assert(st->monitor_fd<0);
460 execvp(st->monitor_command[0],(char**)st->monitor_command);
461 fprintf(stderr,"secnet: cannot execute %s: %s\n",
462 st->monitor_command[0], strerror(errno));
466 fatal_perror("%s: failed to fork for interface monitor",
467 st->uc.cc.cl.description);
471 st->monitor_fd=pfds[0];
472 setnonblock(st->monitor_fd);
474 register_for_poll(st,polypath_beforepoll,polypath_afterpoll,"polypath");
477 static void polypath_phase_shutdown(void *sst, uint32_t newphase)
479 struct polypath *st=sst;
480 if (st->monitor_pid) {
481 assert(st->monitor_pid>0);
482 kill(st->monitor_pid,SIGTERM);
489 static list_t *polypath_apply(closure_t *self, struct cloc loc,
490 dict_t *context, list_t *args)
494 COMM_APPLY(st,&st->uc.cc,polypath_,"polypath",loc);
495 COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args);
496 UDP_APPLY_STANDARD(st,&st->uc,"polypath");
498 struct udpcommon *uc=&st->uc;
499 struct commcommon *cc=&uc->cc;
501 st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3);
503 st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath",
505 st->permit_loopback=0; /* ifname_wanted reads this */
506 ifname_wanted(st,st->uc.cc.loc," "); /* try to check each pattern */
508 st->monitor_command=dict_read_string_array(d,"monitor-command",False,
509 "polypath",cc->loc, default_monitor_command);
510 if (!st->monitor_command[0])
511 cfgfatal(loc,"polypath","no polypath interface monitor-command"
512 " (polypath unsupported on this platform?)\n");
514 st->permit_loopback=dict_read_bool(d,"permit-loopback",False,
515 "polypath",cc->loc,False);
517 LIST_INIT(&st->interfs);
518 buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100);
519 BUF_ALLOC(&st->lbuf,"polypath lbuf");
524 add_hook(PHASE_RUN, polypath_phase_startmonitor,st);
525 add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st);
527 return new_closure(&cc->cl);
530 #endif /* CONFIG_IPV6 */
532 void polypath_module(dict_t *dict)
535 add_closure(dict,"polypath",polypath_apply);
536 #endif /* CONFIG_IPV6 */