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_general
;
49 struct interf_list interfs_dedicated
;
50 struct buffer_if lbuf
;
53 int privsep_incoming_fd
;
54 int privsep_ipcsock_fd
;
57 struct comm_clientinfo
{
58 union iaddr dedicated
; /* might be AF_UNSPEC */
61 static void polypath_phase_shutdown(void *sst
, uint32_t newphase
);
63 #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc
65 static const char *const default_loopback_ifname_pats
[] = {
68 static const char *const default_ifname_pats
[] = {
69 "!tun*","!tap*","!sl*","!userv*", "@hippo*", "*", 0
72 static const char *const default_monitor_command
[] = {
74 DATAROOTDIR
"/secnet/" "polypath-interface-monitor-linux", 0
80 static const char *polypath_addr_to_string(void *commst
,
81 const struct comm_addr
*ca
)
83 static char sbuf
[100];
85 snprintf(sbuf
, sizeof(sbuf
), "polypath:%s",
86 iaddr_to_string(&ca
->ia
));
90 static bool_t
ifname_search_pats(struct polypath
*st
, struct cloc loc
,
91 const char *ifname
, char *want_io
,
92 const char *const *pats
) {
93 /* Returns True iff we found a list entry, in which case *want_io
94 * is set to the sense of that entry. Otherwise *want_io is set
95 * to the sense of the last entry, or unchanged if there were no pats. */
98 const char *const *pati
;
99 for (pati
=pats
; *pati
; pati
++) {
100 const char *pat
=*pati
;
101 if (*pat
=='!' || *pat
=='+' || *pat
=='@') { *want_io
=*pat
; pat
++; }
102 else if (*pat
=='*' || isalnum((unsigned char)*pat
)) { *want_io
='+'; }
103 else cfgfatal(loc
,"polypath","invalid interface name pattern `%s'",pat
);
104 int match
=fnmatch(pat
,ifname
,0);
105 if (match
==0) return True
;
106 if (match
!=FNM_NOMATCH
)
107 cfgfatal(loc
,"polypath","fnmatch failed! (pattern `%s')",pat
);
112 static char ifname_wanted(struct polypath
*st
, struct cloc loc
,
113 const char *ifname
) {
114 char want
='!'; /* pretend an empty cfg ends with !<doesn'tmatch> */
115 if (ifname_search_pats(st
,loc
,ifname
,&want
, st
->ifname_pats
))
117 if (want
!='!') /* last pattern was positive, do not search default */
119 if (!st
->permit_loopback
&&
120 ifname_search_pats(st
,loc
,ifname
,&want
, default_loopback_ifname_pats
))
122 if (ifname_search_pats(st
,loc
,ifname
,&want
, default_ifname_pats
))
127 static struct comm_clientinfo
*polypath_clientinfo(void *state
,
128 dict_t
*dict
, struct cloc cloc
) {
129 struct comm_clientinfo
*clientinfo
;
132 FILLZERO(*clientinfo
);
133 clientinfo
->dedicated
.sa
.sa_family
=AF_UNSPEC
;
135 item_t
*item
= dict_find_item(dict
,"dedicated-interface-addr",
136 False
,"polypath",cloc
);
137 if (item
) string_item_to_iaddr(item
,0,&clientinfo
->dedicated
,
142 static int polypath_beforepoll(void *state
, struct pollfd
*fds
, int *nfds_io
,
145 struct polypath
*st
=state
;
146 BEFOREPOLL_WANT_FDS(1);
147 fds
[0].fd
=st
->monitor_fd
;
148 fds
[0].events
=POLLIN
;
152 static inline bool_t
matches32(uint32_t word
, uint32_t prefix
, int prefixlen
)
155 assert(prefixlen
<=32);
156 uint32_t mask
= ~(((uint32_t)1 << (32-prefixlen
)) - 1);
157 assert(!(prefix
& ~mask
));
158 return (word
& mask
) == prefix
;
161 /* These macros expect
162 * bad_fn_type *const bad;
167 #define BAD(m) do{ bad(st,badctx,M_WARNING,m,0); goto out; }while(0)
168 #define BADE(m,ev) do{ bad(st,badctx,M_WARNING,m,ev); goto out; }while(0)
169 typedef void bad_fn_type(struct polypath
*st
, void *badctx
,
170 int mclass
, const char* m
, int ev
);
172 typedef void polypath_ppml_callback_type(struct polypath
*st
,
173 bad_fn_type
*bad
, void *badctx
,
174 bool_t add
, char want
,
175 const char *ifname
, const char *ifaddr
,
176 const union iaddr
*ia
, int fd
/* -1 if none yet */);
178 struct ppml_bad_ctx
{
183 static void ppml_bad(struct polypath
*st
, void *badctx
,
184 int mclass
, const char *m
, int ev
)
186 struct ppml_bad_ctx
*bc
=badctx
;
188 *(bc
->undospace
)=' ';
189 lg_perror(LG
,mclass
,ev
,
190 "error processing polypath state change: %s"
191 " (while processing `%s')",
195 static void polypath_process_monitor_line(struct polypath
*st
, char *orgl
,
196 polypath_ppml_callback_type
*callback
)
197 /* always calls callback with fd==-1 */
199 struct udpcommon
*uc
=&st
->uc
;
201 bad_fn_type (*const bad
)=ppml_bad
;
202 struct ppml_bad_ctx badctx
[1]={{
209 if (c
=='+') add
=True
;
210 else if (c
=='-') add
=False
;
215 if (c
=='4') proto
=AF_INET
;
216 else if (c
=='6') proto
=AF_INET6
;
217 else BAD("bad proto");
219 char *space
=strchr(l
,' ');
220 if (!space
) BAD("no first space");
221 const char *ifname
=space
+1;
223 space
=strchr(ifname
,' ');
224 if (!space
) BAD("no second space");
225 const char *ifaddr
=space
+1;
227 badctx
->undospace
=space
;
231 socklen_t salen
=sizeof(ia
);
232 int r
=adns_text2addr(ifaddr
,uc
->port
, adns_qf_addrlit_ipv4_quadonly
,
235 if (r
) BADE("adns_text2addr",r
);
236 if (ia
.sa
.sa_family
!=proto
) BAD("address family mismatch");
238 #define DONT(m) do{ \
240 lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \
244 char want
=ifname_wanted(st
,st
->uc
.cc
.loc
,ifname
);
245 if (want
=='!') DONT("unwanted interface name");
247 switch (ia
.sa
.sa_family
) {
249 const struct in6_addr
*i6
=&ia
.sin6
.sin6_addr
;
250 #define DONTKIND(X,m) \
251 if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m)
252 DONTKIND(UNSPECIFIED
, "unspecified");
253 DONTKIND(MULTICAST
, "multicast" );
254 DONTKIND(LINKLOCAL
, "link local" );
255 DONTKIND(SITELOCAL
, "site local" );
256 DONTKIND(V4MAPPED
, "v4-mapped" );
257 if (!st
->permit_loopback
)
258 DONTKIND(LOOPBACK
, "loopback" );
260 #define DONTMASK(w7x,w6x,prefixlen,m) \
261 if (matches32(get_uint32(i6->s6_addr), \
262 ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \
264 DONT("IPv6 address is " m)
265 DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)");
266 DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)");
267 DONTMASK(fc00
, 0, 7, "Uniqe Local unicast (RFC4193)");
272 const uint32_t i4
=htonl(ia
.sin
.sin_addr
.s_addr
);
273 if (i4
==INADDR_ANY
) DONT("IPv4 address is any/unspecified");
274 if (i4
==INADDR_BROADCAST
) DONT("IPv4 address is all hosts broadcast");
275 #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \
276 const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \
277 if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \
278 DONT("IPv4 address is " m); \
280 DONTMASK(169,254,0,0, 16, "link local");
281 DONTMASK(224, 0,0,0, 4, "multicast");
282 DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)");
283 DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)");
284 if (!st
->permit_loopback
)
285 DONTMASK(127, 0,0,0, 8, "loopback");
296 callback(st
, bad
,badctx
, add
,want
, ifname
,ifaddr
,&ia
,-1);
301 static void dump_pria(struct polypath
*st
, struct interf_list
*interfs
,
302 const char *ifname
, char want
)
304 #ifdef POLYPATH_DEBUG
305 struct interf
*interf
;
307 lg_perror(LG
,M_DEBUG
,0, "polypath record ifaddr `%s' (%c)",
309 LIST_FOREACH(interf
, interfs
, entry
) {
310 lg_perror(LG
,M_DEBUG
,0, " polypath interface `%s', nsocks=%d",
311 interf
->name
, interf
->socks
.n_socks
);
313 for (i
=0; i
<interf
->socks
.n_socks
; i
++) {
314 struct udpsock
*us
=&interf
->socks
.socks
[i
];
315 lg_perror(LG
,M_DEBUG
,0, " polypath sock fd=%d addr=%s",
316 us
->fd
, iaddr_to_string(&us
->addr
));
322 static bool_t
polypath_make_socket(struct polypath
*st
,
323 bad_fn_type
*bad
, void *badctx
,
324 struct udpsock
*us
, const char *ifname
)
325 /* on error exit has called bad; might leave us->fd as -1 */
329 bool_t ok
=udp_make_socket(&st
->uc
,us
,M_WARNING
);
330 if (!ok
) BAD("unable to set up socket");
331 int r
=setsockopt(us
->fd
,SOL_SOCKET
,SO_BINDTODEVICE
,
332 ifname
,strlen(ifname
)+1);
333 if (r
) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno
);
340 static void polypath_record_ifaddr(struct polypath
*st
,
341 bad_fn_type
*bad
, void *badctx
,
342 bool_t add
, char want
,
345 const union iaddr
*ia
, int fd
)
347 struct udpcommon
*uc
=&st
->uc
;
348 struct interf
*interf
=0;
350 struct udpsock
*us
=0;
352 struct interf_list
*interfs
;
354 case '+': interfs
=&st
->interfs_general
; max_interfs
=st
->max_interfs
; break;
355 case '@': interfs
=&st
->interfs_dedicated
; max_interfs
=INT_MAX
;
356 default: fatal("polypath: got bad want (%#x, %s)", want
, ifname
);
359 dump_pria(st
,interfs
,ifname
,want
);
362 LIST_FOREACH(interf
,interfs
,entry
) {
363 if (!strcmp(interf
->name
,ifname
))
368 if (n_ifs
==max_interfs
) BAD("too many interfaces");
369 interf
=malloc(sizeof(*interf
));
370 if (!interf
) BADE("malloc for new interface",errno
);
372 interf
->socks
.n_socks
=0;
373 FILLZERO(interf
->experienced_xmit_noaf
);
374 LIST_INSERT_HEAD(interfs
,interf
,entry
);
375 interf
->name
=strdup(ifname
);
376 udp_socks_register(&st
->uc
,&interf
->socks
,interf
->name
);
377 if (!interf
->name
) BADE("strdup interface name",errno
);
381 if (interf
->socks
.n_socks
== UDP_MAX_SOCKETS
)
382 BAD("too many addresses on this interface");
383 struct udpsock
*us
=&interf
->socks
.socks
[interf
->socks
.n_socks
];
385 COPY_OBJ(us
->addr
,*ia
);
387 bool_t ok
=polypath_make_socket(st
,bad
,badctx
, us
,ifname
);
390 bool_t ok
=udp_import_socket(uc
,us
,M_WARNING
,fd
);
394 interf
->socks
.n_socks
++;
395 lg_perror(LG
,M_INFO
,0,"using %s %s",ifname
,
396 iaddr_to_string(&us
->addr
));
397 us
=0; /* do not destroy this socket during `out' */
400 for (i
=0; i
<interf
->socks
.n_socks
; i
++)
401 if (iaddr_equal(&interf
->socks
.socks
[i
].addr
,ia
,True
))
402 goto address_remove_found
;
403 bad(st
,badctx
,M_DEBUG
,"address to remove not found",0);
405 address_remove_found
:
406 lg_perror(LG
,M_INFO
,0,"removed %s %s",ifname
,
407 iaddr_to_string(&interf
->socks
.socks
[i
].addr
));
408 udp_destroy_socket(&st
->uc
,&interf
->socks
.socks
[i
]);
409 interf
->socks
.socks
[i
]=
410 interf
->socks
.socks
[--interf
->socks
.n_socks
];
415 udp_destroy_socket(uc
,us
);
418 if (interf
&& !interf
->socks
.n_socks
) {
419 udp_socks_deregister(&st
->uc
,&interf
->socks
);
420 LIST_REMOVE(interf
,entry
);
425 dump_pria(st
,interfs
,0,0);
428 static void subproc_problem(struct polypath
*st
,
429 enum async_linebuf_result alr
, const char *emsg
)
432 assert(st
->monitor_pid
);
434 pid_t gotpid
=waitpid(st
->monitor_pid
,&status
,WNOHANG
);
435 if (gotpid
==st
->monitor_pid
) {
437 lg_exitstatus(LG
,M_FATAL
,status
,"interface monitor");
439 lg_perror(LG
,M_ERR
,errno
,"unable to reap interface monitor");
443 if (alr
==async_linebuf_eof
)
444 lg_perror(LG
,M_FATAL
,0,"unexpected EOF from interface monitor");
446 lg_perror(LG
,M_FATAL
,0,"bad output from interface monitor: %s",emsg
);
447 assert(!"not reached");
450 /* Used in non-privsep case, and in privsep child */
451 static void afterpoll_monitor(struct polypath
*st
, struct pollfd
*fd
,
452 polypath_ppml_callback_type
*callback
)
454 enum async_linebuf_result alr
;
457 while ((alr
=async_linebuf_read(fd
,&st
->lbuf
,&emsg
)) == async_linebuf_ok
)
458 polypath_process_monitor_line(st
,st
->lbuf
.base
,callback
);
460 if (alr
==async_linebuf_nothing
)
463 subproc_problem(st
,alr
,emsg
);
466 /* Used in non-privsep case only - glue for secnet main loop */
467 static void polypath_afterpoll_monitor(void *state
, struct pollfd
*fds
,
470 struct polypath
*st
=state
;
472 afterpoll_monitor(st
,fds
,polypath_record_ifaddr
);
475 /* Actual udp packet sending work */
477 static void polypath_sendmsg_interf(struct polypath
*st
,
478 struct interf
*interf
,
479 struct buffer_if
*buf
,
480 const struct comm_addr
*dest
,
481 const union iaddr
*dedicated
,
482 bool_t
*allreasonable
)
485 int af
=dest
->ia
.sa
.sa_family
;
486 bool_t wanted
=False
, attempted
=False
, reasonable
=False
;
488 for (i
=0; i
<interf
->socks
.n_socks
; i
++) {
489 struct udpsock
*us
=&interf
->socks
.socks
[i
];
490 if (dedicated
&& !iaddr_equal(dedicated
, &us
->addr
, True
))
493 if (af
!= us
->addr
.sa
.sa_family
)
496 int r
=sendto(us
->fd
,buf
->start
,buf
->size
,
497 0,&dest
->ia
.sa
,iaddr_socklen(&dest
->ia
));
498 udp_sock_experienced(0,&st
->uc
,&interf
->socks
,us
,
499 &dest
->ia
,af
, r
,errno
);
504 if (!(errno
==EAFNOSUPPORT
|| errno
==ENETUNREACH
))
506 lg_perror(LG
,M_DEBUG
,errno
,"%s [%s] xmit %"PRIu32
" bytes to %s",
507 interf
->name
,iaddr_to_string(&us
->addr
),
508 buf
->size
,iaddr_to_string(&dest
->ia
));
514 if (!interf
->experienced_xmit_noaf
[af
]++)
515 lg_perror(LG
,M_WARNING
,0,
516 "%s has no suitable address to transmit %s",
517 interf
->name
, af_name(af
));
519 *allreasonable
*= reasonable
;
522 static bool_t
polypath_sendmsg(void *commst
, struct buffer_if
*buf
,
523 const struct comm_addr
*dest
,
524 struct comm_clientinfo
*clientinfo
)
526 struct polypath
*st
=commst
;
527 struct interf
*interf
;
528 bool_t allreasonable
=True
;
530 LIST_FOREACH(interf
,&st
->interfs_general
,entry
) {
531 polypath_sendmsg_interf(st
,interf
,buf
,dest
,
534 if (clientinfo
&& clientinfo
->dedicated
.sa
.sa_family
!= AF_UNSPEC
) {
535 LIST_FOREACH(interf
,&st
->interfs_dedicated
,entry
) {
536 polypath_sendmsg_interf(st
,interf
,buf
,dest
,
537 &clientinfo
->dedicated
, &allreasonable
);
540 return allreasonable
;
543 /* Non-privsep: called in (sole) child. Privsep: in grandchild. */
544 static void child_monitor(struct polypath
*st
, int childfd
)
547 execvp(st
->monitor_command
[0],(char**)st
->monitor_command
);
548 fprintf(stderr
,"secnet: cannot execute %s: %s\n",
549 st
->monitor_command
[0], strerror(errno
));
553 /* General utility function. */
554 static void start_subproc(struct polypath
*st
, void (*make_fdpair
)(int[2]),
555 void (*child
)(struct polypath
*st
, int childfd
),
560 assert(!st
->monitor_pid
);
561 assert(st
->monitor_fd
<0);
573 fatal_perror("%s: failed to fork for interface monitoring",
574 st
->uc
.cc
.cl
.description
);
578 st
->monitor_fd
=pfds
[0];
579 setnonblock(st
->monitor_fd
);
581 lg_perror(LG
,M_NOTICE
,0, "%s: spawning %s [pid %ld]",
582 st
->uc
.cc
.cl
.description
, desc
, (long)st
->monitor_pid
);
585 /* Non-privsep only: glue for forking the monitor, from the main loop */
586 static void polypath_phase_startmonitor(void *sst
, uint32_t newphase
)
588 struct polypath
*st
=sst
;
589 start_subproc(st
,pipe_cloexec
,child_monitor
,
590 "interface monitor (no privsep)");
591 register_for_poll(st
,polypath_beforepoll
,
592 polypath_afterpoll_monitor
,"polypath");
595 /*----- Privsep-only: -----*/
598 * We use two subprocesses, a child and a grandchild. These are
599 * forked before secnet drops privilege.
601 * The grandchild is the same interface monitor helper script as used
602 * in the non-privsep case. But its lines are read by the child
603 * instead of by the main secnet. The child is responsible for
604 * creating the actual socket (binding it, etc.). Each socket is
605 * passed to secnet proper via fd passing, along with a data message
606 * describing the interface name and address. The child does not
607 * retain a list of current interfaces and addresses - it trusts the
608 * interface monitor to get that right. secnet proper still maintains
609 * that data structure.
611 * The result is that much of the non-privsep code can be reused, but
612 * just plumbed together differently.
614 * The child does not retain the socket after passing it on.
615 * Interface removals are handled similarly but without any fd.
617 * The result is that the configuration's limits on which interfaces
618 * and ports secnet may use are enforced by the privileged child.
621 struct privsep_mdata
{
625 char want
; /* `+' or `@' */
628 static void papp_bad(struct polypath
*st
, void *badctx
,
629 int mclass
, const char *m
, int ev
)
631 const struct privsep_mdata
*mdata
=(const void*)st
->lbuf
.start
;
632 const char *addr_str
=badctx
;
634 lg_perror(LG
,mclass
,ev
,
635 "error processing polypath address change %s %s [%s]: %s",
636 mdata
->add ?
"+" : "-",
637 mdata
->ifname
, addr_str
, m
);
640 static void polypath_afterpoll_privsep(void *state
, struct pollfd
*fds
,
642 /* In secnet proper; receives messages from child. */
644 struct polypath
*st
=state
;
648 int revents
=fds
[0].revents
;
650 const char *badbit
=pollbadbit(revents
);
651 if (badbit
) subproc_problem(st
,async_linebuf_broken
,badbit
);
653 if (!(revents
& POLLIN
)) return;
656 if (st
->lbuf
.size
==sizeof(struct privsep_mdata
)) {
657 const struct privsep_mdata
*mdata
=(const void*)st
->lbuf
.start
;
658 if (mdata
->add
&& st
->privsep_incoming_fd
<0)
659 fatal("polypath (privsep): got add message data but no fd");
660 if (!mdata
->add
&& st
->privsep_incoming_fd
>=0)
661 fatal("polypath (privsep): got remove message data with fd");
662 if (!memchr(mdata
->ifname
,0,sizeof(mdata
->ifname
)))
663 fatal("polypath (privsep): got ifname with no terminating nul");
664 int af
=mdata
->ia
.sa
.sa_family
;
665 if (!(af
==AF_INET6
|| af
==AF_INET
))
666 fatal("polypath (privsep): got message data but bad AF %d",af
);
667 const char *addr_str
=iaddr_to_string(&mdata
->ia
);
668 polypath_record_ifaddr(st
,papp_bad
,(void*)addr_str
,
669 mdata
->add
,mdata
->want
,
670 mdata
->ifname
,addr_str
,
671 &mdata
->ia
, st
->privsep_incoming_fd
);
672 st
->privsep_incoming_fd
=-1;
677 size_t cmsgdatalen
=sizeof(fd
);
678 char cmsg_control_buf
[CMSG_SPACE(cmsgdatalen
)];
685 iov
.iov_base
=st
->lbuf
.start
+st
->lbuf
.size
;
686 iov
.iov_len
=sizeof(struct privsep_mdata
)-st
->lbuf
.size
;
688 if (st
->privsep_incoming_fd
<0) {
689 msg
.msg_control
=cmsg_control_buf
;
690 msg
.msg_controllen
=sizeof(cmsg_control_buf
);
693 ssize_t got
=recvmsg(st
->monitor_fd
,&msg
,0);
695 if (errno
==EINTR
) continue;
696 if (iswouldblock(errno
)) break;
697 fatal_perror("polypath (privsep): recvmsg failed");
700 subproc_problem(st
,async_linebuf_eof
,0);
704 if (msg
.msg_controllen
) {
705 size_t cmsgdatalen
=sizeof(st
->privsep_incoming_fd
);
706 struct cmsghdr
*h
=CMSG_FIRSTHDR(&msg
);
707 if (!(st
->privsep_incoming_fd
==-1 &&
709 h
->cmsg_level
==SOL_SOCKET
&&
710 h
->cmsg_type
==SCM_RIGHTS
&&
711 h
->cmsg_len
==CMSG_LEN(cmsgdatalen
) &&
712 !CMSG_NXTHDR(&msg
,h
)))
713 subproc_problem(st
,async_linebuf_broken
,"bad cmsg");
714 memcpy(&st
->privsep_incoming_fd
,CMSG_DATA(h
),cmsgdatalen
);
715 assert(st
->privsep_incoming_fd
>=0);
721 static void privsep_handle_ifaddr(struct polypath
*st
,
722 bad_fn_type
*bad
, void *badctx
,
723 bool_t add
, char want
,
726 const union iaddr
*ia
, int fd_dummy
)
727 /* In child: handles discovered wanted interfaces, making sockets
728 and sending them to secnet proper. */
732 struct udpsock us
={ .fd
=-1 };
733 size_t cmsgdatalen
=sizeof(us
.fd
);
734 char cmsg_control_buf
[CMSG_SPACE(cmsgdatalen
)];
736 assert(fd_dummy
==-1);
738 struct privsep_mdata mdata
;
742 size_t l
=strlen(ifname
);
743 if (l
>=sizeof(mdata
.ifname
)) BAD("interface name too long");
744 strcpy(mdata
.ifname
,ifname
);
747 COPY_OBJ(mdata
.ia
,*ia
);
750 iov
.iov_len
=sizeof(mdata
);
757 COPY_OBJ(us
.addr
,*ia
);
758 bool_t ok
=polypath_make_socket(st
,bad
,badctx
,&us
,ifname
);
761 msg
.msg_control
=cmsg_control_buf
;
762 msg
.msg_controllen
=sizeof(cmsg_control_buf
);
764 struct cmsghdr
*h
=CMSG_FIRSTHDR(&msg
);
765 h
->cmsg_level
=SOL_SOCKET
;
766 h
->cmsg_type
=SCM_RIGHTS
;
767 h
->cmsg_len
=CMSG_LEN(cmsgdatalen
);
768 memcpy(CMSG_DATA(h
),&us
.fd
,cmsgdatalen
);
771 while (iov
.iov_len
) {
772 ssize_t got
=sendmsg(st
->privsep_ipcsock_fd
,&msg
,0);
774 if (errno
!=EINTR
) fatal_perror("polypath privsep sendmsg");
778 assert((size_t)got
<=iov
.iov_len
);
780 iov
.iov_base
=(char*)iov
.iov_base
+got
;
783 msg
.msg_controllen
=0;
787 if (us
.fd
>=0) close(us
.fd
);
790 static void child_privsep(struct polypath
*st
, int ipcsockfd
)
791 /* Privsep child main loop. */
793 struct pollfd fds
[2];
795 enter_phase(PHASE_CHILDPERSIST
);
797 st
->privsep_ipcsock_fd
=ipcsockfd
;
798 start_subproc(st
,pipe_cloexec
,child_monitor
,
799 "interface monitor (grandchild)");
802 int r
=polypath_beforepoll(st
,fds
,&nfds
,0);
806 fds
[1].fd
=st
->privsep_ipcsock_fd
;
807 fds
[1].events
=POLLIN
;
809 r
=poll(fds
,ARRAY_SIZE(fds
),-1);
812 if (errno
==EINTR
) continue;
813 fatal_perror("polypath privsep poll");
815 if (fds
[1].revents
) {
816 if (fds
[1].revents
& (POLLHUP
|POLLIN
)) {
817 polypath_phase_shutdown(st
,PHASE_SHUTDOWN
);
820 fatal("polypath privsep poll parent socket revents=%#x",
823 if (fds
[0].revents
& POLLNVAL
)
824 fatal("polypath privsep poll child socket POLLNVAL");
825 afterpoll_monitor(st
,fds
,privsep_handle_ifaddr
);
829 static void privsep_socketpair(int *fd
)
831 int r
=socketpair(AF_UNIX
,SOCK_STREAM
,0,fd
);
832 if (r
) fatal_perror("socketpair(AF_UNIX,SOCK_STREAM,,)");
837 static void polypath_phase_startprivsep(void *sst
, uint32_t newphase
)
839 struct polypath
*st
=sst
;
841 if (!will_droppriv()) {
842 add_hook(PHASE_RUN
, polypath_phase_startmonitor
,st
);
846 start_subproc(st
,privsep_socketpair
,child_privsep
,
847 "socket generator (privsep interface handler)");
850 buffer_destroy(&st
->lbuf
);
851 buffer_new(&st
->lbuf
,sizeof(struct privsep_mdata
));
852 BUF_ALLOC(&st
->lbuf
,"polypath mdata buf");
853 st
->privsep_incoming_fd
=-1;
855 register_for_poll(st
,polypath_beforepoll
,
856 polypath_afterpoll_privsep
,"polypath");
859 static void polypath_phase_shutdown(void *sst
, uint32_t newphase
)
861 struct polypath
*st
=sst
;
862 if (st
->monitor_pid
) {
863 assert(st
->monitor_pid
>0);
864 kill(st
->monitor_pid
,SIGTERM
);
868 static void polypath_phase_childpersist(void *sst
, uint32_t newphase
)
870 struct polypath
*st
=sst
;
871 struct interf
*interf
;
873 LIST_FOREACH(interf
,&st
->interfs_general
,entry
)
874 udp_socks_childpersist(&st
->uc
,&interf
->socks
);
875 LIST_FOREACH(interf
,&st
->interfs_dedicated
,entry
)
876 udp_socks_childpersist(&st
->uc
,&interf
->socks
);
882 /*----- generic closure and module setup -----*/
884 static list_t
*polypath_apply(closure_t
*self
, struct cloc loc
,
885 dict_t
*context
, list_t
*args
)
889 COMM_APPLY(st
,&st
->uc
.cc
,polypath_
,"polypath",loc
);
890 COMM_APPLY_STANDARD(st
,&st
->uc
.cc
,"polypath",args
);
891 UDP_APPLY_STANDARD(st
,&st
->uc
,"polypath");
893 st
->uc
.cc
.ops
.clientinfo
= polypath_clientinfo
;
895 struct udpcommon
*uc
=&st
->uc
;
896 struct commcommon
*cc
=&uc
->cc
;
898 st
->max_interfs
=dict_read_number(d
,"max-interfaces",False
,"polypath",loc
,3);
900 st
->ifname_pats
=dict_read_string_array(d
,"interfaces",False
,"polypath",
902 st
->permit_loopback
=0; /* ifname_wanted reads this */
903 ifname_wanted(st
,st
->uc
.cc
.loc
," "); /* try to check each pattern */
905 st
->monitor_command
=dict_read_string_array(d
,"monitor-command",False
,
906 "polypath",cc
->loc
, default_monitor_command
);
907 if (!st
->monitor_command
[0])
908 cfgfatal(loc
,"polypath","no polypath interface monitor-command"
909 " (polypath unsupported on this platform?)\n");
911 st
->permit_loopback
=dict_read_bool(d
,"permit-loopback",False
,
912 "polypath",cc
->loc
,False
);
914 LIST_INIT(&st
->interfs_general
);
915 LIST_INIT(&st
->interfs_dedicated
);
916 buffer_new(&st
->lbuf
,ADNS_ADDR2TEXT_BUFLEN
+100);
917 BUF_ALLOC(&st
->lbuf
,"polypath lbuf");
922 add_hook(PHASE_GETRESOURCES
, polypath_phase_startprivsep
,st
);
924 add_hook(PHASE_SHUTDOWN
, polypath_phase_shutdown
, st
);
925 add_hook(PHASE_CHILDPERSIST
,polypath_phase_childpersist
,st
);
927 return new_closure(&cc
->cl
);
930 #endif /* CONFIG_IPV6 */
932 void polypath_module(dict_t
*dict
)
935 add_closure(dict
,"polypath",polypath_apply
);
936 #endif /* CONFIG_IPV6 */