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];
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 "!userv*","!sl*","!tap*","!tun*", "*", 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 void dump_ppml(struct polypath *st, const char *orgl)
114 struct interf *interf;
116 lg_perror(LG,M_DEBUG,0, "polypath monitor line `%s'",orgl);
117 LIST_FOREACH(interf, &st->interfs, entry) {
118 lg_perror(LG,M_DEBUG,0, " polypath interface `%s', nsocks=%d",
119 interf->name, interf->socks.n_socks);
121 for (i=0; i<interf->socks.n_socks; i++) {
122 struct udpsock *us=&interf->socks.socks[i];
123 lg_perror(LG,M_DEBUG,0, " polypath sock fd=%d addr=%s",
124 us->fd, iaddr_to_string(&us->addr));
130 static void bad(struct polypath *st, char *l, char *undospace,
131 const char *m, int ev)
135 lg_perror(LG,M_WARNING,ev,
136 "error processing polypath state change: %s"
137 " (while processing `%s')",
141 static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen)
144 assert(prefixlen<=32);
145 uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1);
146 assert(!(prefix & ~mask));
147 return (word & mask) == prefix;
150 static void polypath_process_monitor_line(struct polypath *st, char *orgl)
152 struct interf *interf=0;
153 struct udpcommon *uc=&st->uc;
156 struct udpsock *us=0;
160 #define BAD(m) do{ bad(st,orgl,undospace,m,0); goto out; }while(0)
161 #define BADE(m,ev) do{ bad(st,orgl,undospace,m,ev); goto out; }while(0)
165 if (c=='+') add=True;
166 else if (c=='-') add=False;
171 if (c=='4') proto=AF_INET;
172 else if (c=='6') proto=AF_INET6;
173 else BAD("bad proto");
175 char *space=strchr(l,' ');
176 if (!space) BAD("no first space");
177 const char *ifname=space+1;
179 space=strchr(ifname,' ');
180 if (!space) BAD("no second space");
183 const char *ifaddr=space;
187 socklen_t salen=sizeof(ia);
188 int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly,
191 if (r) BADE("adns_text2addr",r);
192 if (ia.sa.sa_family!=proto) BAD("address family mismatch");
194 #define DONT(m) do{ \
196 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
200 if (!ifname_wanted(st,st->uc.cc.loc,ifname))
201 DONT("unwanted interface name");
203 switch (ia.sa.sa_family) {
205 const struct in6_addr *i6=&ia.sin6.sin6_addr;
206 #define DONTKIND(X,x) \
207 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " #x)
208 DONTKIND(UNSPECIFIED,unspecified);
209 DONTKIND(MULTICAST ,multicast );
210 DONTKIND(LINKLOCAL ,linklocal );
211 DONTKIND(SITELOCAL ,sitelocal );
212 DONTKIND(V4MAPPED ,v4mapped );
213 if (!st->permit_loopback)
214 DONTKIND(LOOPBACK ,loopback );
216 #define DONTMASK(w7x,w6x,prefixlen,m) \
217 if (matches32(get_uint32(i6->s6_addr), \
218 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
220 DONT("IPv6 address is " m)
221 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
222 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
227 const uint32_t i4=htonl(ia.sin.sin_addr.s_addr);
228 if (i4==INADDR_ANY) DONT("IPv4 address is any/unspecified");
229 if (i4==INADDR_BROADCAST) DONT("IPv4 address is all hosts broadcast");
230 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
231 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
232 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
233 DONT("IPv4 address is " m); \
235 DONTMASK(169,254,0,0, 16, "link local");
236 DONTMASK(224, 0,0,0, 4, "multicast");
237 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
238 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
239 if (!st->permit_loopback)
240 DONTMASK(127, 0,0,0, 8, "loopback");
250 LIST_FOREACH(interf,&st->interfs,entry) {
251 if (!strcmp(interf->name,ifname))
256 if (n_ifs==st->max_interfs) BAD("too many interfaces");
257 interf=malloc(sizeof(*interf));
258 if (!interf) BADE("malloc for new interface",errno);
260 interf->socks.n_socks=0;
261 LIST_INSERT_HEAD(&st->interfs,interf,entry);
262 udp_socks_register(&st->uc,&interf->socks);
263 interf->name=strdup(ifname);
264 if (!interf->name) BADE("strdup interface name",errno);
268 if (interf->socks.n_socks == UDP_MAX_SOCKETS)
269 BAD("too many addresses on this interface");
270 struct udpsock *us=&interf->socks.socks[interf->socks.n_socks];
272 memcpy(&us->addr,&ia,sizeof(us->addr));
273 bool_t ok=udp_make_socket(&st->uc,us,M_WARNING);
274 if (!ok) BAD("unable to set up socket");
275 r=setsockopt(us->fd,SOL_SOCKET,SO_BINDTODEVICE,
276 ifname,strlen(ifname)+1);
277 if (r) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno);
278 interf->socks.n_socks++;
279 us=0; /* do not destroy this socket during `out' */
280 lg_perror(LG,M_INFO,0,"using %s [%s]",ifname,ifaddr);
283 for (i=0;i<interf->socks.n_socks;i++)
284 if (!memcmp(&interf->socks.socks[i].addr,&ia,sizeof(ia)))
285 goto address_remove_found;
286 BAD("address to remove not found");
287 address_remove_found:
288 lg_perror(LG,M_INFO,0,"removed %s [%s]",ifname,ifaddr);
289 udp_destroy_socket(&st->uc,&interf->socks.socks[i]);
290 interf->socks.socks[i]=
291 interf->socks.socks[--interf->socks.n_socks];
296 udp_destroy_socket(uc,us);
297 if (interf && !interf->socks.n_socks) {
298 udp_socks_deregister(&st->uc,&interf->socks);
299 LIST_REMOVE(interf,entry);
311 static void polypath_afterpoll(void *state, struct pollfd *fds, int nfds)
313 struct polypath *st=state;
314 enum async_linebuf_result alr;
320 while ((alr=async_linebuf_read(fds,&st->lbuf,&emsg)) == async_linebuf_ok)
321 polypath_process_monitor_line(st,st->lbuf.base);
323 if (alr==async_linebuf_nothing)
326 assert(st->monitor_pid);
328 pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG);
329 if (gotpid==st->monitor_pid)
330 lg_exitstatus(LG,M_FATAL,status,"interface monitor");
332 lg_perror(LG,M_ERR,errno,"unable to reap interface monitor");
336 if (alr==async_linebuf_eof)
337 lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor");
339 lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg);
341 assert(!"not reached");
344 static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf,
345 const struct comm_addr *dest)
347 struct polypath *st=commst;
348 struct interf *interf;
349 bool_t allreasonable=True;
350 int af=dest->ia.sa.sa_family;
352 LIST_FOREACH(interf,&st->interfs,entry) {
354 bool_t attempted=False, reasonable=False;
355 for (i=0; i<interf->socks.n_socks; i++) {
356 struct udpsock *us=&interf->socks.socks[i];
357 if (af != us->addr.sa.sa_family)
360 int r=sendto(us->fd,buf->start,buf->size,
361 0,&dest->ia.sa,iaddr_socklen(&dest->ia));
362 udp_sock_experienced(0,&st->uc, interf->name,us,
368 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
370 lg_perror(LG,M_DEBUG,errno,"xmit %"PRIu32" bytes to %s",
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_getresources(void *sst, uint32_t newphase)
385 struct polypath *st=sst;
388 assert(!st->monitor_pid);
389 assert(st->monitor_fd<0);
393 st->monitor_pid=fork();
394 if (!st->monitor_pid) {
396 execvp(st->monitor_command[0],(char**)st->monitor_command);
397 fprintf(stderr,"secnet: cannot execute %s: %s\n",
398 st->monitor_command[0], strerror(errno));
401 if (st->monitor_pid<0)
402 fatal_perror("%s: failed to fork for interface monitor",
403 st->uc.cc.cl.description);
406 st->monitor_fd=pfds[0];
407 setnonblock(st->monitor_fd);
409 register_for_poll(st,polypath_beforepoll,polypath_afterpoll,"polypath");
412 static void polypath_phase_shutdown(void *sst, uint32_t newphase)
414 struct polypath *st=sst;
416 kill(SIGTERM,st->monitor_pid);
419 static list_t *polypath_apply(closure_t *self, struct cloc loc,
420 dict_t *context, list_t *args)
424 COMM_APPLY(st,&st->uc.cc,polypath_,"polypath",loc);
425 COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args);
426 UDP_APPLY_STANDARD(st,&st->uc,"polypath");
428 struct udpcommon *uc=&st->uc;
429 struct commcommon *cc=&uc->cc;
431 uc->authbind=dict_read_string(d,"authbind",False,"udp",cc->loc);
432 st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3);
434 st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath",
436 ifname_wanted(st,st->uc.cc.loc,"dummy"); /* walks and checks */
438 st->monitor_command=dict_read_string_array(d,"monitor-command",False,
439 "polypath",cc->loc, default_monitor_command);
440 if (!st->monitor_command[0])
441 cfgfatal(loc,"tun","no polypath interface monitor-command"
442 " (polypath unsupported on this platform?)\n");
444 st->permit_loopback=dict_read_bool(d,"permit-loopback",False,
445 "polypath",cc->loc,False);
447 LIST_INIT(&st->interfs);
448 buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100);
449 BUF_ALLOC(&st->lbuf,"polypath lbuf");
454 add_hook(PHASE_GETRESOURCES,polypath_phase_getresources,st);
455 add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st);
457 return new_closure(&cc->cl);
460 #endif /* CONFIG_IPV6 */
462 void polypath_module(dict_t *dict)
465 add_closure(dict,"polypath",polypath_apply);
466 #endif /* CONFIG_IPV6 */