2 * send/receive module for secnet
3 * for multi-route setups */
5 * This file is part of secnet.
6 * See README for full list of copyright holders.
8 * secnet is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version d of the License, or
11 * (at your option) any later version.
13 * secnet is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * version 3 along with secnet; if not, see
20 * https://www.gnu.org/licenses/gpl.html.
25 #include "unaligned.h"
26 #include "comm-common.h"
33 static comm_sendmsg_fn polypath_sendmsg;
36 char *name; /* from malloc */
37 struct udpsocks socks;
38 bool_t experienced_xmit_noaf[MAX_AF+1];
39 LIST_ENTRY(interf) entry;
45 const char *const *ifname_pats;
46 const char *const *monitor_command;
47 bool_t permit_loopback;
48 LIST_HEAD(interf_list, interf) interfs;
49 struct buffer_if lbuf;
52 int privsep_incoming_fd;
53 int privsep_ipcsock_fd;
56 static void polypath_phase_shutdown(void *sst, uint32_t newphase);
58 #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc
60 static const char *const default_loopback_ifname_pats[] = {
63 static const char *const default_ifname_pats[] = {
64 "!tun*","!tap*","!sl*","!userv*", "*", 0
67 static const char *const default_monitor_command[] = {
69 DATAROOTDIR "/secnet/" "polypath-interface-monitor-linux", 0
75 static const char *polypath_addr_to_string(void *commst,
76 const struct comm_addr *ca)
78 static char sbuf[100];
80 snprintf(sbuf, sizeof(sbuf), "polypath:%s",
81 iaddr_to_string(&ca->ia));
85 static bool_t ifname_search_pats(struct polypath *st, struct cloc loc,
86 const char *ifname, char *want_io,
87 const char *const *pats) {
88 /* Returns True iff we found a list entry, in which case *want_io
89 * is set to the sense of that entry. Otherwise *want_io is set
90 * to the sense of the last entry, or unchanged if there were no pats. */
93 const char *const *pati;
94 for (pati=pats; *pati; pati++) {
95 const char *pat=*pati;
96 if (*pat=='!' || *pat=='+') { *want_io=*pat; pat++; }
97 else if (*pat=='*' || isalnum((unsigned char)*pat)) { *want_io='+'; }
98 else cfgfatal(loc,"polypath","invalid interface name pattern `%s'",pat);
99 int match=fnmatch(pat,ifname,0);
100 if (match==0) return True;
101 if (match!=FNM_NOMATCH)
102 cfgfatal(loc,"polypath","fnmatch failed! (pattern `%s')",pat);
107 static char ifname_wanted(struct polypath *st, struct cloc loc,
108 const char *ifname) {
109 char want='!'; /* pretend an empty cfg ends with !<doesn'tmatch> */
110 if (ifname_search_pats(st,loc,ifname,&want, st->ifname_pats))
112 if (want!='!') /* last pattern was positive, do not search default */
114 if (!st->permit_loopback &&
115 ifname_search_pats(st,loc,ifname,&want, default_loopback_ifname_pats))
117 if (ifname_search_pats(st,loc,ifname,&want, default_ifname_pats))
122 static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
125 struct polypath *st=state;
126 BEFOREPOLL_WANT_FDS(1);
127 fds[0].fd=st->monitor_fd;
128 fds[0].events=POLLIN;
132 static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen)
135 assert(prefixlen<=32);
136 uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1);
137 assert(!(prefix & ~mask));
138 return (word & mask) == prefix;
141 /* These macros expect
142 * bad_fn_type *const bad;
147 #define BAD(m) do{ bad(st,badctx,M_WARNING,m,0); goto out; }while(0)
148 #define BADE(m,ev) do{ bad(st,badctx,M_WARNING,m,ev); goto out; }while(0)
149 typedef void bad_fn_type(struct polypath *st, void *badctx,
150 int mclass, const char* m, int ev);
152 typedef void polypath_ppml_callback_type(struct polypath *st,
153 bad_fn_type *bad, void *badctx,
154 bool_t add, char want,
155 const char *ifname, const char *ifaddr,
156 const union iaddr *ia, int fd /* -1 if none yet */);
158 struct ppml_bad_ctx {
163 static void ppml_bad(struct polypath *st, void *badctx,
164 int mclass, const char *m, int ev)
166 struct ppml_bad_ctx *bc=badctx;
168 *(bc->undospace)=' ';
169 lg_perror(LG,mclass,ev,
170 "error processing polypath state change: %s"
171 " (while processing `%s')",
175 static void polypath_process_monitor_line(struct polypath *st, char *orgl,
176 polypath_ppml_callback_type *callback)
177 /* always calls callback with fd==-1 */
179 struct udpcommon *uc=&st->uc;
181 bad_fn_type (*const bad)=ppml_bad;
182 struct ppml_bad_ctx badctx[1]={{
189 if (c=='+') add=True;
190 else if (c=='-') add=False;
195 if (c=='4') proto=AF_INET;
196 else if (c=='6') proto=AF_INET6;
197 else BAD("bad proto");
199 char *space=strchr(l,' ');
200 if (!space) BAD("no first space");
201 const char *ifname=space+1;
203 space=strchr(ifname,' ');
204 if (!space) BAD("no second space");
205 const char *ifaddr=space+1;
207 badctx->undospace=space;
211 socklen_t salen=sizeof(ia);
212 int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly,
215 if (r) BADE("adns_text2addr",r);
216 if (ia.sa.sa_family!=proto) BAD("address family mismatch");
218 #define DONT(m) do{ \
220 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
224 char want=ifname_wanted(st,st->uc.cc.loc,ifname);
225 if (want=='!') DONT("unwanted interface name");
227 switch (ia.sa.sa_family) {
229 const struct in6_addr *i6=&ia.sin6.sin6_addr;
230 #define DONTKIND(X,m) \
231 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m)
232 DONTKIND(UNSPECIFIED, "unspecified");
233 DONTKIND(MULTICAST , "multicast" );
234 DONTKIND(LINKLOCAL , "link local" );
235 DONTKIND(SITELOCAL , "site local" );
236 DONTKIND(V4MAPPED , "v4-mapped" );
237 if (!st->permit_loopback)
238 DONTKIND(LOOPBACK , "loopback" );
240 #define DONTMASK(w7x,w6x,prefixlen,m) \
241 if (matches32(get_uint32(i6->s6_addr), \
242 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
244 DONT("IPv6 address is " m)
245 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
246 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
247 DONTMASK(fc00, 0, 7, "Uniqe Local unicast (RFC4193)");
252 const uint32_t i4=htonl(ia.sin.sin_addr.s_addr);
253 if (i4==INADDR_ANY) DONT("IPv4 address is any/unspecified");
254 if (i4==INADDR_BROADCAST) DONT("IPv4 address is all hosts broadcast");
255 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
256 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
257 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
258 DONT("IPv4 address is " m); \
260 DONTMASK(169,254,0,0, 16, "link local");
261 DONTMASK(224, 0,0,0, 4, "multicast");
262 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
263 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
264 if (!st->permit_loopback)
265 DONTMASK(127, 0,0,0, 8, "loopback");
276 callback(st, bad,badctx, add,want, ifname,ifaddr,&ia,-1);
281 static void dump_pria(struct polypath *st, struct interf_list *interfs,
282 const char *ifname, char want)
284 #ifdef POLYPATH_DEBUG
285 struct interf *interf;
287 lg_perror(LG,M_DEBUG,0, "polypath record ifaddr `%s' (%c)",
289 LIST_FOREACH(interf, interfs, entry) {
290 lg_perror(LG,M_DEBUG,0, " polypath interface `%s', nsocks=%d",
291 interf->name, interf->socks.n_socks);
293 for (i=0; i<interf->socks.n_socks; i++) {
294 struct udpsock *us=&interf->socks.socks[i];
295 lg_perror(LG,M_DEBUG,0, " polypath sock fd=%d addr=%s",
296 us->fd, iaddr_to_string(&us->addr));
302 static bool_t polypath_make_socket(struct polypath *st,
303 bad_fn_type *bad, void *badctx,
304 struct udpsock *us, const char *ifname)
305 /* on error exit has called bad; might leave us->fd as -1 */
309 bool_t ok=udp_make_socket(&st->uc,us,M_WARNING);
310 if (!ok) BAD("unable to set up socket");
311 int r=setsockopt(us->fd,SOL_SOCKET,SO_BINDTODEVICE,
312 ifname,strlen(ifname)+1);
313 if (r) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno);
320 static void polypath_record_ifaddr(struct polypath *st,
321 bad_fn_type *bad, void *badctx,
322 bool_t add, char want,
325 const union iaddr *ia, int fd)
327 struct udpcommon *uc=&st->uc;
328 struct interf *interf=0;
330 struct udpsock *us=0;
332 struct interf_list *interfs;
334 case '+': interfs=&st->interfs; max_interfs=st->max_interfs;
335 default: fatal("polypath: got bad want (%#x, %s)", want, ifname);
338 dump_pria(st,interfs,ifname,want);
341 LIST_FOREACH(interf,interfs,entry) {
342 if (!strcmp(interf->name,ifname))
347 if (n_ifs==max_interfs) BAD("too many interfaces");
348 interf=malloc(sizeof(*interf));
349 if (!interf) BADE("malloc for new interface",errno);
351 interf->socks.n_socks=0;
352 FILLZERO(interf->experienced_xmit_noaf);
353 LIST_INSERT_HEAD(interfs,interf,entry);
354 interf->name=strdup(ifname);
355 udp_socks_register(&st->uc,&interf->socks,interf->name);
356 if (!interf->name) BADE("strdup interface name",errno);
360 if (interf->socks.n_socks == UDP_MAX_SOCKETS)
361 BAD("too many addresses on this interface");
362 struct udpsock *us=&interf->socks.socks[interf->socks.n_socks];
364 COPY_OBJ(us->addr,*ia);
366 bool_t ok=polypath_make_socket(st,bad,badctx, us,ifname);
369 bool_t ok=udp_import_socket(uc,us,M_WARNING,fd);
373 interf->socks.n_socks++;
374 lg_perror(LG,M_INFO,0,"using %s %s",ifname,
375 iaddr_to_string(&us->addr));
376 us=0; /* do not destroy this socket during `out' */
379 for (i=0; i<interf->socks.n_socks; i++)
380 if (iaddr_equal(&interf->socks.socks[i].addr,ia,True))
381 goto address_remove_found;
382 bad(st,badctx,M_DEBUG,"address to remove not found",0);
384 address_remove_found:
385 lg_perror(LG,M_INFO,0,"removed %s %s",ifname,
386 iaddr_to_string(&interf->socks.socks[i].addr));
387 udp_destroy_socket(&st->uc,&interf->socks.socks[i]);
388 interf->socks.socks[i]=
389 interf->socks.socks[--interf->socks.n_socks];
394 udp_destroy_socket(uc,us);
397 if (interf && !interf->socks.n_socks) {
398 udp_socks_deregister(&st->uc,&interf->socks);
399 LIST_REMOVE(interf,entry);
404 dump_pria(st,interfs,0,0);
407 static void subproc_problem(struct polypath *st,
408 enum async_linebuf_result alr, const char *emsg)
411 assert(st->monitor_pid);
413 pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG);
414 if (gotpid==st->monitor_pid) {
416 lg_exitstatus(LG,M_FATAL,status,"interface monitor");
418 lg_perror(LG,M_ERR,errno,"unable to reap interface monitor");
422 if (alr==async_linebuf_eof)
423 lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor");
425 lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg);
426 assert(!"not reached");
429 /* Used in non-privsep case, and in privsep child */
430 static void afterpoll_monitor(struct polypath *st, struct pollfd *fd,
431 polypath_ppml_callback_type *callback)
433 enum async_linebuf_result alr;
436 while ((alr=async_linebuf_read(fd,&st->lbuf,&emsg)) == async_linebuf_ok)
437 polypath_process_monitor_line(st,st->lbuf.base,callback);
439 if (alr==async_linebuf_nothing)
442 subproc_problem(st,alr,emsg);
445 /* Used in non-privsep case only - glue for secnet main loop */
446 static void polypath_afterpoll_monitor(void *state, struct pollfd *fds,
449 struct polypath *st=state;
451 afterpoll_monitor(st,fds,polypath_record_ifaddr);
454 /* Actual udp packet sending work */
455 static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf,
456 const struct comm_addr *dest,
457 struct comm_clientinfo *clientinfo)
459 struct polypath *st=commst;
460 struct interf *interf;
461 bool_t allreasonable=True;
462 int af=dest->ia.sa.sa_family;
464 LIST_FOREACH(interf,&st->interfs,entry) {
466 bool_t attempted=False, reasonable=False;
467 for (i=0; i<interf->socks.n_socks; i++) {
468 struct udpsock *us=&interf->socks.socks[i];
469 if (af != us->addr.sa.sa_family)
472 int r=sendto(us->fd,buf->start,buf->size,
473 0,&dest->ia.sa,iaddr_socklen(&dest->ia));
474 udp_sock_experienced(0,&st->uc,&interf->socks,us,
475 &dest->ia,af, r,errno);
480 if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH))
482 lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s",
483 interf->name,iaddr_to_string(&us->addr),
484 buf->size,iaddr_to_string(&dest->ia));
487 if (!interf->experienced_xmit_noaf[af]++)
488 lg_perror(LG,M_WARNING,0,
489 "%s has no suitable address to transmit %s",
490 interf->name, af_name(af));
491 allreasonable *= reasonable;
493 return allreasonable;
496 /* Non-privsep: called in (sole) child. Privsep: in grandchild. */
497 static void child_monitor(struct polypath *st, int childfd)
500 execvp(st->monitor_command[0],(char**)st->monitor_command);
501 fprintf(stderr,"secnet: cannot execute %s: %s\n",
502 st->monitor_command[0], strerror(errno));
506 /* General utility function. */
507 static void start_subproc(struct polypath *st, void (*make_fdpair)(int[2]),
508 void (*child)(struct polypath *st, int childfd),
513 assert(!st->monitor_pid);
514 assert(st->monitor_fd<0);
526 fatal_perror("%s: failed to fork for interface monitoring",
527 st->uc.cc.cl.description);
531 st->monitor_fd=pfds[0];
532 setnonblock(st->monitor_fd);
534 lg_perror(LG,M_NOTICE,0, "%s: spawning %s [pid %ld]",
535 st->uc.cc.cl.description, desc, (long)st->monitor_pid);
538 /* Non-privsep only: glue for forking the monitor, from the main loop */
539 static void polypath_phase_startmonitor(void *sst, uint32_t newphase)
541 struct polypath *st=sst;
542 start_subproc(st,pipe_cloexec,child_monitor,
543 "interface monitor (no privsep)");
544 register_for_poll(st,polypath_beforepoll,
545 polypath_afterpoll_monitor,"polypath");
548 /*----- Privsep-only: -----*/
551 * We use two subprocesses, a child and a grandchild. These are
552 * forked before secnet drops privilege.
554 * The grandchild is the same interface monitor helper script as used
555 * in the non-privsep case. But its lines are read by the child
556 * instead of by the main secnet. The child is responsible for
557 * creating the actual socket (binding it, etc.). Each socket is
558 * passed to secnet proper via fd passing, along with a data message
559 * describing the interface name and address. The child does not
560 * retain a list of current interfaces and addresses - it trusts the
561 * interface monitor to get that right. secnet proper still maintains
562 * that data structure.
564 * The result is that much of the non-privsep code can be reused, but
565 * just plumbed together differently.
567 * The child does not retain the socket after passing it on.
568 * Interface removals are handled similarly but without any fd.
570 * The result is that the configuration's limits on which interfaces
571 * and ports secnet may use are enforced by the privileged child.
574 struct privsep_mdata {
578 char want; /* `+', for now */
581 static void papp_bad(struct polypath *st, void *badctx,
582 int mclass, const char *m, int ev)
584 const struct privsep_mdata *mdata=(const void*)st->lbuf.start;
585 const char *addr_str=badctx;
587 lg_perror(LG,mclass,ev,
588 "error processing polypath address change %s %s [%s]: %s",
589 mdata->add ? "+" : "-",
590 mdata->ifname, addr_str, m);
593 static void polypath_afterpoll_privsep(void *state, struct pollfd *fds,
595 /* In secnet proper; receives messages from child. */
597 struct polypath *st=state;
601 int revents=fds[0].revents;
603 const char *badbit=pollbadbit(revents);
604 if (badbit) subproc_problem(st,async_linebuf_broken,badbit);
606 if (!(revents & POLLIN)) return;
609 if (st->lbuf.size==sizeof(struct privsep_mdata)) {
610 const struct privsep_mdata *mdata=(const void*)st->lbuf.start;
611 if (mdata->add && st->privsep_incoming_fd<0)
612 fatal("polypath (privsep): got add message data but no fd");
613 if (!mdata->add && st->privsep_incoming_fd>=0)
614 fatal("polypath (privsep): got remove message data with fd");
615 if (!memchr(mdata->ifname,0,sizeof(mdata->ifname)))
616 fatal("polypath (privsep): got ifname with no terminating nul");
617 int af=mdata->ia.sa.sa_family;
618 if (!(af==AF_INET6 || af==AF_INET))
619 fatal("polypath (privsep): got message data but bad AF %d",af);
620 const char *addr_str=iaddr_to_string(&mdata->ia);
621 polypath_record_ifaddr(st,papp_bad,(void*)addr_str,
622 mdata->add,mdata->want,
623 mdata->ifname,addr_str,
624 &mdata->ia, st->privsep_incoming_fd);
625 st->privsep_incoming_fd=-1;
630 size_t cmsgdatalen=sizeof(fd);
631 char cmsg_control_buf[CMSG_SPACE(cmsgdatalen)];
638 iov.iov_base=st->lbuf.start+st->lbuf.size;
639 iov.iov_len=sizeof(struct privsep_mdata)-st->lbuf.size;
641 if (st->privsep_incoming_fd<0) {
642 msg.msg_control=cmsg_control_buf;
643 msg.msg_controllen=sizeof(cmsg_control_buf);
646 ssize_t got=recvmsg(st->monitor_fd,&msg,0);
648 if (errno==EINTR) continue;
649 if (iswouldblock(errno)) break;
650 fatal_perror("polypath (privsep): recvmsg failed");
653 subproc_problem(st,async_linebuf_eof,0);
657 if (msg.msg_controllen) {
658 size_t cmsgdatalen=sizeof(st->privsep_incoming_fd);
659 struct cmsghdr *h=CMSG_FIRSTHDR(&msg);
660 if (!(st->privsep_incoming_fd==-1 &&
662 h->cmsg_level==SOL_SOCKET &&
663 h->cmsg_type==SCM_RIGHTS &&
664 h->cmsg_len==CMSG_LEN(cmsgdatalen) &&
665 !CMSG_NXTHDR(&msg,h)))
666 subproc_problem(st,async_linebuf_broken,"bad cmsg");
667 memcpy(&st->privsep_incoming_fd,CMSG_DATA(h),cmsgdatalen);
668 assert(st->privsep_incoming_fd>=0);
674 static void privsep_handle_ifaddr(struct polypath *st,
675 bad_fn_type *bad, void *badctx,
676 bool_t add, char want,
679 const union iaddr *ia, int fd_dummy)
680 /* In child: handles discovered wanted interfaces, making sockets
681 and sending them to secnet proper. */
685 struct udpsock us={ .fd=-1 };
686 size_t cmsgdatalen=sizeof(us.fd);
687 char cmsg_control_buf[CMSG_SPACE(cmsgdatalen)];
689 assert(fd_dummy==-1);
691 struct privsep_mdata mdata;
695 size_t l=strlen(ifname);
696 if (l>=sizeof(mdata.ifname)) BAD("interface name too long");
697 strcpy(mdata.ifname,ifname);
700 COPY_OBJ(mdata.ia,*ia);
703 iov.iov_len =sizeof(mdata);
710 COPY_OBJ(us.addr,*ia);
711 bool_t ok=polypath_make_socket(st,bad,badctx,&us,ifname);
714 msg.msg_control=cmsg_control_buf;
715 msg.msg_controllen=sizeof(cmsg_control_buf);
717 struct cmsghdr *h=CMSG_FIRSTHDR(&msg);
718 h->cmsg_level=SOL_SOCKET;
719 h->cmsg_type =SCM_RIGHTS;
720 h->cmsg_len =CMSG_LEN(cmsgdatalen);
721 memcpy(CMSG_DATA(h),&us.fd,cmsgdatalen);
724 while (iov.iov_len) {
725 ssize_t got=sendmsg(st->privsep_ipcsock_fd,&msg,0);
727 if (errno!=EINTR) fatal_perror("polypath privsep sendmsg");
731 assert((size_t)got<=iov.iov_len);
733 iov.iov_base=(char*)iov.iov_base+got;
736 msg.msg_controllen=0;
740 if (us.fd>=0) close(us.fd);
743 static void child_privsep(struct polypath *st, int ipcsockfd)
744 /* Privsep child main loop. */
746 struct pollfd fds[2];
748 enter_phase(PHASE_CHILDPERSIST);
750 st->privsep_ipcsock_fd=ipcsockfd;
751 start_subproc(st,pipe_cloexec,child_monitor,
752 "interface monitor (grandchild)");
755 int r=polypath_beforepoll(st,fds,&nfds,0);
759 fds[1].fd=st->privsep_ipcsock_fd;
760 fds[1].events=POLLIN;
762 r=poll(fds,ARRAY_SIZE(fds),-1);
765 if (errno==EINTR) continue;
766 fatal_perror("polypath privsep poll");
768 if (fds[1].revents) {
769 if (fds[1].revents & (POLLHUP|POLLIN)) {
770 polypath_phase_shutdown(st,PHASE_SHUTDOWN);
773 fatal("polypath privsep poll parent socket revents=%#x",
776 if (fds[0].revents & POLLNVAL)
777 fatal("polypath privsep poll child socket POLLNVAL");
778 afterpoll_monitor(st,fds,privsep_handle_ifaddr);
782 static void privsep_socketpair(int *fd)
784 int r=socketpair(AF_UNIX,SOCK_STREAM,0,fd);
785 if (r) fatal_perror("socketpair(AF_UNIX,SOCK_STREAM,,)");
790 static void polypath_phase_startprivsep(void *sst, uint32_t newphase)
792 struct polypath *st=sst;
794 if (!will_droppriv()) {
795 add_hook(PHASE_RUN, polypath_phase_startmonitor,st);
799 start_subproc(st,privsep_socketpair,child_privsep,
800 "socket generator (privsep interface handler)");
803 buffer_destroy(&st->lbuf);
804 buffer_new(&st->lbuf,sizeof(struct privsep_mdata));
805 BUF_ALLOC(&st->lbuf,"polypath mdata buf");
806 st->privsep_incoming_fd=-1;
808 register_for_poll(st,polypath_beforepoll,
809 polypath_afterpoll_privsep,"polypath");
812 static void polypath_phase_shutdown(void *sst, uint32_t newphase)
814 struct polypath *st=sst;
815 if (st->monitor_pid) {
816 assert(st->monitor_pid>0);
817 kill(st->monitor_pid,SIGTERM);
821 static void polypath_phase_childpersist(void *sst, uint32_t newphase)
823 struct polypath *st=sst;
824 struct interf *interf;
826 LIST_FOREACH(interf,&st->interfs,entry)
827 udp_socks_childpersist(&st->uc,&interf->socks);
833 /*----- generic closure and module setup -----*/
835 static list_t *polypath_apply(closure_t *self, struct cloc loc,
836 dict_t *context, list_t *args)
840 COMM_APPLY(st,&st->uc.cc,polypath_,"polypath",loc);
841 COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args);
842 UDP_APPLY_STANDARD(st,&st->uc,"polypath");
844 struct udpcommon *uc=&st->uc;
845 struct commcommon *cc=&uc->cc;
847 st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3);
849 st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath",
851 st->permit_loopback=0; /* ifname_wanted reads this */
852 ifname_wanted(st,st->uc.cc.loc," "); /* try to check each pattern */
854 st->monitor_command=dict_read_string_array(d,"monitor-command",False,
855 "polypath",cc->loc, default_monitor_command);
856 if (!st->monitor_command[0])
857 cfgfatal(loc,"polypath","no polypath interface monitor-command"
858 " (polypath unsupported on this platform?)\n");
860 st->permit_loopback=dict_read_bool(d,"permit-loopback",False,
861 "polypath",cc->loc,False);
863 LIST_INIT(&st->interfs);
864 buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100);
865 BUF_ALLOC(&st->lbuf,"polypath lbuf");
870 add_hook(PHASE_GETRESOURCES, polypath_phase_startprivsep,st);
872 add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st);
873 add_hook(PHASE_CHILDPERSIST,polypath_phase_childpersist,st);
875 return new_closure(&cc->cl);
878 #endif /* CONFIG_IPV6 */
880 void polypath_module(dict_t *dict)
883 add_closure(dict,"polypath",polypath_apply);
884 #endif /* CONFIG_IPV6 */