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
;
34 int privsep_incoming_fd
;
35 int privsep_ipcsock_fd
;
38 static void polypath_phase_shutdown(void *sst
, uint32_t newphase
);
40 #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc
42 static const char *const default_loopback_ifname_pats
[] = {
45 static const char *const default_ifname_pats
[] = {
46 "!tun*","!tap*","!sl*","!userv*", "*", 0
49 static const char *const default_monitor_command
[] = {
51 DATAROOTDIR
"/secnet/" "polypath-interface-monitor-linux", 0
57 static const char *polypath_addr_to_string(void *commst
,
58 const struct comm_addr
*ca
)
60 static char sbuf
[100];
62 snprintf(sbuf
, sizeof(sbuf
), "polypath:%s",
63 iaddr_to_string(&ca
->ia
));
67 static bool_t
ifname_search_pats(struct polypath
*st
, struct cloc loc
,
68 const char *ifname
, bool_t
*want_io
,
69 const char *const *pats
) {
70 /* Returns True iff we found a list entry, in which case *want_io
71 * is set to the sense of that entry. Otherwise *want_io is set
72 * to the sense of the last entry, or unchanged if there were no pats. */
75 const char *const *pati
;
76 for (pati
=pats
; *pati
; pati
++) {
77 const char *pat
=*pati
;
78 if (*pat
=='!') { *want_io
=False
; pat
++; }
79 else if (*pat
=='+') { *want_io
=True
; pat
++; }
80 else if (*pat
=='*' || isalnum((unsigned char)*pat
)) { *want_io
=True
; }
81 else cfgfatal(loc
,"polypath","invalid interface name pattern `%s'",pat
);
82 int match
=fnmatch(pat
,ifname
,0);
83 if (match
==0) return True
;
84 if (match
!=FNM_NOMATCH
)
85 cfgfatal(loc
,"polypath","fnmatch failed! (pattern `%s')",pat
);
90 static bool_t
ifname_wanted(struct polypath
*st
, struct cloc loc
,
92 bool_t want
=False
; /* pretend an empty cfg ends with !<doesn'tmatch> */
93 if (ifname_search_pats(st
,loc
,ifname
,&want
, st
->ifname_pats
))
95 if (want
) /* last pattern was positive, do not search default */
97 if (!st
->permit_loopback
&&
98 ifname_search_pats(st
,loc
,ifname
,&want
, default_loopback_ifname_pats
))
100 if (ifname_search_pats(st
,loc
,ifname
,&want
, default_ifname_pats
))
105 static int polypath_beforepoll(void *state
, struct pollfd
*fds
, int *nfds_io
,
108 struct polypath
*st
=state
;
109 BEFOREPOLL_WANT_FDS(1);
110 fds
[0].fd
=st
->monitor_fd
;
111 fds
[0].events
=POLLIN
;
115 static inline bool_t
matches32(uint32_t word
, uint32_t prefix
, int prefixlen
)
118 assert(prefixlen
<=32);
119 uint32_t mask
= ~(((uint32_t)1 << (32-prefixlen
)) - 1);
120 assert(!(prefix
& ~mask
));
121 return (word
& mask
) == prefix
;
124 /* These macros expect
125 * bad_fn_type *const bad;
130 #define BAD(m) do{ bad(st,badctx,M_WARNING,m,0); goto out; }while(0)
131 #define BADE(m,ev) do{ bad(st,badctx,M_WARNING,m,ev); goto out; }while(0)
132 typedef void bad_fn_type(struct polypath
*st
, void *badctx
,
133 int mclass
, const char* m
, int ev
);
135 typedef void polypath_ppml_callback_type(struct polypath
*st
,
136 bad_fn_type
*bad
, void *badctx
,
137 bool_t add
, const char *ifname
, const char *ifaddr
,
138 const union iaddr
*ia
, int fd
/* -1 if none yet */);
140 struct ppml_bad_ctx
{
145 static void ppml_bad(struct polypath
*st
, void *badctx
,
146 int mclass
, const char *m
, int ev
)
148 struct ppml_bad_ctx
*bc
=badctx
;
150 *(bc
->undospace
)=' ';
151 lg_perror(LG
,mclass
,ev
,
152 "error processing polypath state change: %s"
153 " (while processing `%s')",
157 static void polypath_process_monitor_line(struct polypath
*st
, char *orgl
,
158 polypath_ppml_callback_type
*callback
)
159 /* always calls callback with fd==-1 */
161 struct udpcommon
*uc
=&st
->uc
;
163 bad_fn_type (*const bad
)=ppml_bad
;
164 struct ppml_bad_ctx badctx
[1]={{
171 if (c
=='+') add
=True
;
172 else if (c
=='-') add
=False
;
177 if (c
=='4') proto
=AF_INET
;
178 else if (c
=='6') proto
=AF_INET6
;
179 else BAD("bad proto");
181 char *space
=strchr(l
,' ');
182 if (!space
) BAD("no first space");
183 const char *ifname
=space
+1;
185 space
=strchr(ifname
,' ');
186 if (!space
) BAD("no second space");
187 const char *ifaddr
=space
+1;
189 badctx
->undospace
=space
;
193 socklen_t salen
=sizeof(ia
);
194 int r
=adns_text2addr(ifaddr
,uc
->port
, adns_qf_addrlit_ipv4_quadonly
,
197 if (r
) BADE("adns_text2addr",r
);
198 if (ia
.sa
.sa_family
!=proto
) BAD("address family mismatch");
200 #define DONT(m) do{ \
202 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
206 if (!ifname_wanted(st
,st
->uc
.cc
.loc
,ifname
))
207 DONT("unwanted interface name");
209 switch (ia
.sa
.sa_family
) {
211 const struct in6_addr
*i6
=&ia
.sin6
.sin6_addr
;
212 #define DONTKIND(X,m) \
213 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m)
214 DONTKIND(UNSPECIFIED
, "unspecified");
215 DONTKIND(MULTICAST
, "multicast" );
216 DONTKIND(LINKLOCAL
, "link local" );
217 DONTKIND(SITELOCAL
, "site local" );
218 DONTKIND(V4MAPPED
, "v4-mapped" );
219 if (!st
->permit_loopback
)
220 DONTKIND(LOOPBACK
, "loopback" );
222 #define DONTMASK(w7x,w6x,prefixlen,m) \
223 if (matches32(get_uint32(i6->s6_addr), \
224 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
226 DONT("IPv6 address is " m)
227 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
228 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
229 DONTMASK(fc00
, 0, 7, "Uniqe Local unicast (RFC4193)");
234 const uint32_t i4
=htonl(ia
.sin
.sin_addr
.s_addr
);
235 if (i4
==INADDR_ANY
) DONT("IPv4 address is any/unspecified");
236 if (i4
==INADDR_BROADCAST
) DONT("IPv4 address is all hosts broadcast");
237 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
238 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
239 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
240 DONT("IPv4 address is " m); \
242 DONTMASK(169,254,0,0, 16, "link local");
243 DONTMASK(224, 0,0,0, 4, "multicast");
244 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
245 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
246 if (!st
->permit_loopback
)
247 DONTMASK(127, 0,0,0, 8, "loopback");
258 callback(st
, bad
,badctx
, add
,ifname
,ifaddr
,&ia
,-1);
263 static void dump_pria(struct polypath
*st
, const char *ifname
)
265 #ifdef POLYPATH_DEBUG
266 struct interf
*interf
;
268 lg_perror(LG
,M_DEBUG
,0, "polypath record ifaddr `%s'",ifname
);
269 LIST_FOREACH(interf
, &st
->interfs
, entry
) {
270 lg_perror(LG
,M_DEBUG
,0, " polypath interface `%s', nsocks=%d",
271 interf
->name
, interf
->socks
.n_socks
);
273 for (i
=0; i
<interf
->socks
.n_socks
; i
++) {
274 struct udpsock
*us
=&interf
->socks
.socks
[i
];
275 lg_perror(LG
,M_DEBUG
,0, " polypath sock fd=%d addr=%s",
276 us
->fd
, iaddr_to_string(&us
->addr
));
282 static bool_t
polypath_make_socket(struct polypath
*st
,
283 bad_fn_type
*bad
, void *badctx
,
284 struct udpsock
*us
, const char *ifname
)
285 /* on error exit has called bad; might leave us->fd as -1 */
289 bool_t ok
=udp_make_socket(&st
->uc
,us
,M_WARNING
);
290 if (!ok
) BAD("unable to set up socket");
291 int r
=setsockopt(us
->fd
,SOL_SOCKET
,SO_BINDTODEVICE
,
292 ifname
,strlen(ifname
)+1);
293 if (r
) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno
);
300 static void polypath_record_ifaddr(struct polypath
*st
,
301 bad_fn_type
*bad
, void *badctx
,
302 bool_t add
, const char *ifname
,
304 const union iaddr
*ia
, int fd
)
306 struct udpcommon
*uc
=&st
->uc
;
307 struct interf
*interf
=0;
308 struct udpsock
*us
=0;
310 dump_pria(st
,ifname
);
313 LIST_FOREACH(interf
,&st
->interfs
,entry
) {
314 if (!strcmp(interf
->name
,ifname
))
319 if (n_ifs
==st
->max_interfs
) BAD("too many interfaces");
320 interf
=malloc(sizeof(*interf
));
321 if (!interf
) BADE("malloc for new interface",errno
);
323 interf
->socks
.n_socks
=0;
324 FILLZERO(interf
->experienced_xmit_noaf
);
325 LIST_INSERT_HEAD(&st
->interfs
,interf
,entry
);
326 interf
->name
=strdup(ifname
);
327 udp_socks_register(&st
->uc
,&interf
->socks
,interf
->name
);
328 if (!interf
->name
) BADE("strdup interface name",errno
);
332 if (interf
->socks
.n_socks
== UDP_MAX_SOCKETS
)
333 BAD("too many addresses on this interface");
334 struct udpsock
*us
=&interf
->socks
.socks
[interf
->socks
.n_socks
];
336 COPY_OBJ(us
->addr
,*ia
);
338 bool_t ok
=polypath_make_socket(st
,bad
,badctx
, us
,ifname
);
341 bool_t ok
=udp_import_socket(uc
,us
,M_WARNING
,fd
);
345 interf
->socks
.n_socks
++;
346 lg_perror(LG
,M_INFO
,0,"using %s %s",ifname
,
347 iaddr_to_string(&us
->addr
));
348 us
=0; /* do not destroy this socket during `out' */
351 for (i
=0; i
<interf
->socks
.n_socks
; i
++)
352 if (iaddr_equal(&interf
->socks
.socks
[i
].addr
,ia
,True
))
353 goto address_remove_found
;
354 BAD("address to remove not found");
355 address_remove_found
:
356 lg_perror(LG
,M_INFO
,0,"removed %s %s",ifname
,
357 iaddr_to_string(&interf
->socks
.socks
[i
].addr
));
358 udp_destroy_socket(&st
->uc
,&interf
->socks
.socks
[i
]);
359 interf
->socks
.socks
[i
]=
360 interf
->socks
.socks
[--interf
->socks
.n_socks
];
365 udp_destroy_socket(uc
,us
);
368 if (interf
&& !interf
->socks
.n_socks
) {
369 udp_socks_deregister(&st
->uc
,&interf
->socks
);
370 LIST_REMOVE(interf
,entry
);
378 static void subproc_problem(struct polypath
*st
,
379 enum async_linebuf_result alr
, const char *emsg
)
382 assert(st
->monitor_pid
);
384 pid_t gotpid
=waitpid(st
->monitor_pid
,&status
,WNOHANG
);
385 if (gotpid
==st
->monitor_pid
) {
387 lg_exitstatus(LG
,M_FATAL
,status
,"interface monitor");
389 lg_perror(LG
,M_ERR
,errno
,"unable to reap interface monitor");
393 if (alr
==async_linebuf_eof
)
394 lg_perror(LG
,M_FATAL
,0,"unexpected EOF from interface monitor");
396 lg_perror(LG
,M_FATAL
,0,"bad output from interface monitor: %s",emsg
);
397 assert(!"not reached");
400 /* Used in non-privsep case, and in privsep child */
401 static void afterpoll_monitor(struct polypath
*st
, struct pollfd
*fd
,
402 polypath_ppml_callback_type
*callback
)
404 enum async_linebuf_result alr
;
407 while ((alr
=async_linebuf_read(fd
,&st
->lbuf
,&emsg
)) == async_linebuf_ok
)
408 polypath_process_monitor_line(st
,st
->lbuf
.base
,callback
);
410 if (alr
==async_linebuf_nothing
)
413 subproc_problem(st
,alr
,emsg
);
416 /* Used in non-privsep case only - glue for secnet main loop */
417 static void polypath_afterpoll_monitor(void *state
, struct pollfd
*fds
,
420 struct polypath
*st
=state
;
422 afterpoll_monitor(st
,fds
,polypath_record_ifaddr
);
425 /* Actual udp packet sending work */
426 static bool_t
polypath_sendmsg(void *commst
, struct buffer_if
*buf
,
427 const struct comm_addr
*dest
)
429 struct polypath
*st
=commst
;
430 struct interf
*interf
;
431 bool_t allreasonable
=True
;
432 int af
=dest
->ia
.sa
.sa_family
;
434 LIST_FOREACH(interf
,&st
->interfs
,entry
) {
436 bool_t attempted
=False
, reasonable
=False
;
437 for (i
=0; i
<interf
->socks
.n_socks
; i
++) {
438 struct udpsock
*us
=&interf
->socks
.socks
[i
];
439 if (af
!= us
->addr
.sa
.sa_family
)
442 int r
=sendto(us
->fd
,buf
->start
,buf
->size
,
443 0,&dest
->ia
.sa
,iaddr_socklen(&dest
->ia
));
444 udp_sock_experienced(0,&st
->uc
,&interf
->socks
,us
,
445 &dest
->ia
,af
, r
,errno
);
450 if (!(errno
==EAFNOSUPPORT
|| errno
==ENETUNREACH
))
452 lg_perror(LG
,M_DEBUG
,errno
,"%s [%s] xmit %"PRIu32
" bytes to %s",
453 interf
->name
,iaddr_to_string(&us
->addr
),
454 buf
->size
,iaddr_to_string(&dest
->ia
));
457 if (!interf
->experienced_xmit_noaf
[af
]++)
458 lg_perror(LG
,M_WARNING
,0,
459 "%s has no suitable address to transmit %s",
460 interf
->name
, af_name(af
));
461 allreasonable
*= reasonable
;
463 return allreasonable
;
466 /* Non-privsep: called in (sole) child. Privsep: in grandchild. */
467 static void child_monitor(struct polypath
*st
, int childfd
)
470 execvp(st
->monitor_command
[0],(char**)st
->monitor_command
);
471 fprintf(stderr
,"secnet: cannot execute %s: %s\n",
472 st
->monitor_command
[0], strerror(errno
));
476 /* General utility function. */
477 static void start_subproc(struct polypath
*st
, void (*make_fdpair
)(int[2]),
478 void (*child
)(struct polypath
*st
, int childfd
),
483 assert(!st
->monitor_pid
);
484 assert(st
->monitor_fd
<0);
496 fatal_perror("%s: failed to fork for interface monitoring",
497 st
->uc
.cc
.cl
.description
);
501 st
->monitor_fd
=pfds
[0];
502 setnonblock(st
->monitor_fd
);
504 lg_perror(LG
,M_NOTICE
,0, "%s: spawning %s [pid %ld]",
505 st
->uc
.cc
.cl
.description
, desc
, (long)st
->monitor_pid
);
508 /* Non-privsep only: glue for forking the monitor, from the main loop */
509 static void polypath_phase_startmonitor(void *sst
, uint32_t newphase
)
511 struct polypath
*st
=sst
;
512 start_subproc(st
,pipe_cloexec
,child_monitor
,
513 "interface monitor (no privsep)");
514 register_for_poll(st
,polypath_beforepoll
,
515 polypath_afterpoll_monitor
,"polypath");
518 /*----- Privsep-only: -----*/
521 * We use two subprocesses, a child and a grandchild. These are
522 * forked before secnet drops privilege.
524 * The grandchild is the same interface monitor helper script as used
525 * in the non-privsep case. But its lines are read by the child
526 * instead of by the main secnet. The child is responsible for
527 * creating the actual socket (binding it, etc.). Each socket is
528 * passed to secnet proper via fd passing, along with a data message
529 * describing the interface name and address. The child does not
530 * retain a list of current interfaces and addresses - it trusts the
531 * interface monitor to get that right. secnet proper still maintains
532 * that data structure.
534 * The result is that much of the non-privsep code can be reused, but
535 * just plumbed together differently.
537 * The child does not retain the socket after passing it on.
538 * Interface removals are handled similarly but without any fd.
540 * The result is that the configuration's limits on which interfaces
541 * and ports secnet may use are enforced by the privileged child.
544 struct privsep_mdata
{
550 static void papp_bad(struct polypath
*st
, void *badctx
,
551 int mclass
, const char *m
, int ev
)
553 const struct privsep_mdata
*mdata
=(const void*)st
->lbuf
.start
;
554 const char *addr_str
=badctx
;
556 lg_perror(LG
,mclass
,ev
,
557 "error processing polypath address change %s %s [%s]: %s",
558 mdata
->add ?
"+" : "-",
559 mdata
->ifname
, addr_str
, m
);
562 static void polypath_afterpoll_privsep(void *state
, struct pollfd
*fds
,
564 /* In secnet proper; receives messages from child. */
566 struct polypath
*st
=state
;
570 int revents
=fds
[0].revents
;
572 const char *badbit
=pollbadbit(revents
);
573 if (badbit
) subproc_problem(st
,async_linebuf_broken
,badbit
);
575 if (!(revents
& POLLIN
)) return;
578 if (st
->lbuf
.size
==sizeof(struct privsep_mdata
)) {
579 const struct privsep_mdata
*mdata
=(const void*)st
->lbuf
.start
;
580 if (mdata
->add
&& st
->privsep_incoming_fd
<0)
581 fatal("polypath (privsep): got add message data but no fd");
582 if (!mdata
->add
&& st
->privsep_incoming_fd
>=0)
583 fatal("polypath (privsep): got remove message data with fd");
584 if (!memchr(mdata
->ifname
,0,sizeof(mdata
->ifname
)))
585 fatal("polypath (privsep): got ifname with no terminating nul");
586 int af
=mdata
->ia
.sa
.sa_family
;
587 if (!(af
==AF_INET6
|| af
==AF_INET
))
588 fatal("polypath (privsep): got message data but bad AF %d",af
);
589 const char *addr_str
=iaddr_to_string(&mdata
->ia
);
590 polypath_record_ifaddr(st
,papp_bad
,(void*)addr_str
,
591 mdata
->add
,mdata
->ifname
,addr_str
,
592 &mdata
->ia
, st
->privsep_incoming_fd
);
593 st
->privsep_incoming_fd
=-1;
598 size_t cmsgdatalen
=sizeof(fd
);
599 char cmsg_control_buf
[CMSG_SPACE(cmsgdatalen
)];
606 iov
.iov_base
=st
->lbuf
.start
+st
->lbuf
.size
;
607 iov
.iov_len
=sizeof(struct privsep_mdata
)-st
->lbuf
.size
;
609 if (st
->privsep_incoming_fd
<0) {
610 msg
.msg_control
=cmsg_control_buf
;
611 msg
.msg_controllen
=sizeof(cmsg_control_buf
);
614 ssize_t got
=recvmsg(st
->monitor_fd
,&msg
,0);
616 if (errno
==EINTR
) continue;
617 if (iswouldblock(errno
)) break;
618 fatal_perror("polypath (privsep): recvmsg failed");
621 subproc_problem(st
,async_linebuf_eof
,0);
625 if (msg
.msg_controllen
) {
626 size_t cmsgdatalen
=sizeof(st
->privsep_incoming_fd
);
627 struct cmsghdr
*h
=CMSG_FIRSTHDR(&msg
);
628 if (!(st
->privsep_incoming_fd
==-1 &&
630 h
->cmsg_level
==SOL_SOCKET
&&
631 h
->cmsg_type
==SCM_RIGHTS
&&
632 h
->cmsg_len
==CMSG_LEN(cmsgdatalen
) &&
633 !CMSG_NXTHDR(&msg
,h
)))
634 subproc_problem(st
,async_linebuf_broken
,"bad cmsg");
635 memcpy(&st
->privsep_incoming_fd
,CMSG_DATA(h
),cmsgdatalen
);
636 assert(st
->privsep_incoming_fd
>=0);
642 static void privsep_handle_ifaddr(struct polypath
*st
,
643 bad_fn_type
*bad
, void *badctx
,
644 bool_t add
, const char *ifname
,
646 const union iaddr
*ia
, int fd_dummy
)
647 /* In child: handles discovered wanted interfaces, making sockets
648 and sending them to secnet proper. */
652 struct udpsock us
={ .fd
=-1 };
653 size_t cmsgdatalen
=sizeof(us
.fd
);
654 char cmsg_control_buf
[CMSG_SPACE(cmsgdatalen
)];
656 assert(fd_dummy
==-1);
658 struct privsep_mdata mdata
;
662 size_t l
=strlen(ifname
);
663 if (l
>=sizeof(mdata
.ifname
)) BAD("interface name too long");
664 strcpy(mdata
.ifname
,ifname
);
666 COPY_OBJ(mdata
.ia
,*ia
);
669 iov
.iov_len
=sizeof(mdata
);
676 COPY_OBJ(us
.addr
,*ia
);
677 bool_t ok
=polypath_make_socket(st
,bad
,badctx
,&us
,ifname
);
680 msg
.msg_control
=cmsg_control_buf
;
681 msg
.msg_controllen
=sizeof(cmsg_control_buf
);
683 struct cmsghdr
*h
=CMSG_FIRSTHDR(&msg
);
684 h
->cmsg_level
=SOL_SOCKET
;
685 h
->cmsg_type
=SCM_RIGHTS
;
686 h
->cmsg_len
=CMSG_LEN(cmsgdatalen
);
687 memcpy(CMSG_DATA(h
),&us
.fd
,cmsgdatalen
);
690 while (iov
.iov_len
) {
691 ssize_t got
=sendmsg(st
->privsep_ipcsock_fd
,&msg
,0);
693 if (errno
!=EINTR
) fatal_perror("polypath privsep sendmsg");
697 assert((size_t)got
<=iov
.iov_len
);
699 iov
.iov_base
=(char*)iov
.iov_base
+got
;
702 msg
.msg_controllen
=0;
706 if (us
.fd
>=0) close(us
.fd
);
709 static void child_privsep(struct polypath
*st
, int ipcsockfd
)
710 /* Privsep child main loop. */
712 struct pollfd fds
[2];
714 enter_phase(PHASE_CHILDPERSIST
);
716 st
->privsep_ipcsock_fd
=ipcsockfd
;
717 start_subproc(st
,pipe_cloexec
,child_monitor
,
718 "interface monitor (grandchild)");
721 int r
=polypath_beforepoll(st
,fds
,&nfds
,0);
725 fds
[1].fd
=st
->privsep_ipcsock_fd
;
726 fds
[1].events
=POLLIN
;
728 r
=poll(fds
,ARRAY_SIZE(fds
),-1);
731 if (errno
==EINTR
) continue;
732 fatal_perror("polypath privsep poll");
734 if (fds
[1].revents
) {
735 if (fds
[1].revents
& (POLLHUP
|POLLIN
)) {
736 polypath_phase_shutdown(st
,PHASE_SHUTDOWN
);
739 fatal("polypath privsep poll parent socket revents=%#x",
742 if (fds
[0].revents
& POLLNVAL
)
743 fatal("polypath privsep poll child socket POLLNVAL");
744 afterpoll_monitor(st
,fds
,privsep_handle_ifaddr
);
748 static void privsep_socketpair(int *fd
)
750 int r
=socketpair(AF_UNIX
,SOCK_STREAM
,0,fd
);
751 if (r
) fatal_perror("socketpair(AF_UNIX,SOCK_STREAM,,)");
756 static void polypath_phase_startprivsep(void *sst
, uint32_t newphase
)
758 struct polypath
*st
=sst
;
760 if (!will_droppriv()) {
761 add_hook(PHASE_RUN
, polypath_phase_startmonitor
,st
);
765 start_subproc(st
,privsep_socketpair
,child_privsep
,
766 "socket generator (privsep interface handler)");
769 buffer_destroy(&st
->lbuf
);
770 buffer_new(&st
->lbuf
,sizeof(struct privsep_mdata
));
771 BUF_ALLOC(&st
->lbuf
,"polypath mdata buf");
772 st
->privsep_incoming_fd
=-1;
774 register_for_poll(st
,polypath_beforepoll
,
775 polypath_afterpoll_privsep
,"polypath");
778 static void polypath_phase_shutdown(void *sst
, uint32_t newphase
)
780 struct polypath
*st
=sst
;
781 if (st
->monitor_pid
) {
782 assert(st
->monitor_pid
>0);
783 kill(st
->monitor_pid
,SIGTERM
);
787 static void polypath_phase_childpersist(void *sst
, uint32_t newphase
)
789 struct polypath
*st
=sst
;
790 struct interf
*interf
;
792 LIST_FOREACH(interf
,&st
->interfs
,entry
)
793 udp_socks_childpersist(&st
->uc
,&interf
->socks
);
799 /*----- generic closure and module setup -----*/
801 static list_t
*polypath_apply(closure_t
*self
, struct cloc loc
,
802 dict_t
*context
, list_t
*args
)
806 COMM_APPLY(st
,&st
->uc
.cc
,polypath_
,"polypath",loc
);
807 COMM_APPLY_STANDARD(st
,&st
->uc
.cc
,"polypath",args
);
808 UDP_APPLY_STANDARD(st
,&st
->uc
,"polypath");
810 struct udpcommon
*uc
=&st
->uc
;
811 struct commcommon
*cc
=&uc
->cc
;
813 st
->max_interfs
=dict_read_number(d
,"max-interfaces",False
,"polypath",loc
,3);
815 st
->ifname_pats
=dict_read_string_array(d
,"interfaces",False
,"polypath",
817 st
->permit_loopback
=0; /* ifname_wanted reads this */
818 ifname_wanted(st
,st
->uc
.cc
.loc
," "); /* try to check each pattern */
820 st
->monitor_command
=dict_read_string_array(d
,"monitor-command",False
,
821 "polypath",cc
->loc
, default_monitor_command
);
822 if (!st
->monitor_command
[0])
823 cfgfatal(loc
,"polypath","no polypath interface monitor-command"
824 " (polypath unsupported on this platform?)\n");
826 st
->permit_loopback
=dict_read_bool(d
,"permit-loopback",False
,
827 "polypath",cc
->loc
,False
);
829 LIST_INIT(&st
->interfs
);
830 buffer_new(&st
->lbuf
,ADNS_ADDR2TEXT_BUFLEN
+100);
831 BUF_ALLOC(&st
->lbuf
,"polypath lbuf");
836 add_hook(PHASE_GETRESOURCES
, polypath_phase_startprivsep
,st
);
838 add_hook(PHASE_SHUTDOWN
, polypath_phase_shutdown
, st
);
839 add_hook(PHASE_CHILDPERSIST
,polypath_phase_childpersist
,st
);
841 return new_closure(&cc
->cl
);
844 #endif /* CONFIG_IPV6 */
846 void polypath_module(dict_t
*dict
)
849 add_closure(dict
,"polypath",polypath_apply
);
850 #endif /* CONFIG_IPV6 */