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[] = {
49 static const char *polypath_addr_to_string(void *commst,
50 const struct comm_addr *ca)
52 static char sbuf[100];
54 snprintf(sbuf, sizeof(sbuf), "polypath:%s",
55 iaddr_to_string(&ca->ia));
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. */
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);
82 static bool_t ifname_wanted(struct polypath *st, struct cloc loc,
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))
87 if (want) /* last pattern was positive, do not search default */
89 if (!st->permit_loopback &&
90 ifname_search_pats(st,loc,ifname,&want, default_loopback_ifname_pats))
92 if (ifname_search_pats(st,loc,ifname,&want, default_ifname_pats))
97 static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
100 struct polypath *st=state;
101 BEFOREPOLL_WANT_FDS(1);
102 fds[0].fd=st->monitor_fd;
103 fds[0].events=POLLIN;
107 static void dump_ppml(struct polypath *st, const char *orgl)
109 #ifdef POLYPATH_DEBUG
110 struct interf *interf;
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);
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));
126 static void bad(struct polypath *st, char *l, char *undospace,
127 const char *m, int ev)
131 lg_perror(LG,M_WARNING,ev,
132 "error processing polypath state change: %s"
133 " (while processing `%s')",
137 static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen)
140 assert(prefixlen<=32);
141 uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1);
142 assert(!(prefix & ~mask));
143 return (word & mask) == prefix;
146 static void polypath_process_monitor_line(struct polypath *st, char *orgl)
148 struct interf *interf=0;
149 struct udpcommon *uc=&st->uc;
152 struct udpsock *us=0;
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)
161 if (c=='+') add=True;
162 else if (c=='-') add=False;
167 if (c=='4') proto=AF_INET;
168 else if (c=='6') proto=AF_INET6;
169 else BAD("bad proto");
171 char *space=strchr(l,' ');
172 if (!space) BAD("no first space");
173 const char *ifname=space+1;
175 space=strchr(ifname,' ');
176 if (!space) BAD("no second space");
177 const char *ifaddr=space+1;
183 socklen_t salen=sizeof(ia);
184 int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly,
187 if (r) BADE("adns_text2addr",r);
188 if (ia.sa.sa_family!=proto) BAD("address family mismatch");
190 #define DONT(m) do{ \
192 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
196 if (!ifname_wanted(st,st->uc.cc.loc,ifname))
197 DONT("unwanted interface name");
199 switch (ia.sa.sa_family) {
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" );
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, \
216 DONT("IPv6 address is " m)
217 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
218 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
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); \
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");
246 LIST_FOREACH(interf,&st->interfs,entry) {
247 if (!strcmp(interf->name,ifname))
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);
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);
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];
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);
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];
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);
308 static void polypath_afterpoll(void *state, struct pollfd *fds, int nfds)
310 struct polypath *st=state;
311 enum async_linebuf_result alr;
317 while ((alr=async_linebuf_read(fds,&st->lbuf,&emsg)) == async_linebuf_ok)
318 polypath_process_monitor_line(st,st->lbuf.base);
320 if (alr==async_linebuf_nothing)
323 assert(st->monitor_pid);
325 pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG);
326 if (gotpid==st->monitor_pid) {
328 lg_exitstatus(LG,M_FATAL,status,"interface monitor");
330 lg_perror(LG,M_ERR,errno,"unable to reap interface monitor");
334 if (alr==async_linebuf_eof)
335 lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor");
337 lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg);
339 assert(!"not reached");
342 /* Actual udp packet sending work */
343 static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf,
344 const struct comm_addr *dest)
346 struct polypath *st=commst;
347 struct interf *interf;
348 bool_t allreasonable=True;
349 int af=dest->ia.sa.sa_family;
351 LIST_FOREACH(interf,&st->interfs,entry) {
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)
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,
367 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
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));
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;
380 return allreasonable;
383 static void polypath_phase_startmonitor(void *sst, uint32_t newphase)
385 struct polypath *st=sst;
388 assert(!st->monitor_pid);
389 assert(st->monitor_fd<0);
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));
403 fatal_perror("%s: failed to fork for interface monitor",
404 st->uc.cc.cl.description);
408 st->monitor_fd=pfds[0];
409 setnonblock(st->monitor_fd);
411 register_for_poll(st,polypath_beforepoll,polypath_afterpoll,"polypath");
414 static void polypath_phase_shutdown(void *sst, uint32_t newphase)
416 struct polypath *st=sst;
417 if (st->monitor_pid) {
418 assert(st->monitor_pid>0);
419 kill(st->monitor_pid,SIGTERM);
423 static list_t *polypath_apply(closure_t *self, struct cloc loc,
424 dict_t *context, list_t *args)
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");
432 struct udpcommon *uc=&st->uc;
433 struct commcommon *cc=&uc->cc;
435 st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3);
437 st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath",
439 st->permit_loopback=0; /* ifname_wanted reads this */
440 ifname_wanted(st,st->uc.cc.loc," "); /* try to check each pattern */
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");
448 st->permit_loopback=dict_read_bool(d,"permit-loopback",False,
449 "polypath",cc->loc,False);
451 LIST_INIT(&st->interfs);
452 buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100);
453 BUF_ALLOC(&st->lbuf,"polypath lbuf");
458 add_hook(PHASE_RUN, polypath_phase_startmonitor,st);
459 add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st);
461 return new_closure(&cc->cl);
464 #endif /* CONFIG_IPV6 */
466 void polypath_module(dict_t *dict)
469 add_closure(dict,"polypath",polypath_apply);
470 #endif /* CONFIG_IPV6 */