X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/65d6d32e633f180dc77146ddcc4f6be58114fd19..86420bb75f19f628ffd2d8ff9964e59ed99e3187:/polypath.c diff --git a/polypath.c b/polypath.c index 859e3a5..1d73a0a 100644 --- a/polypath.c +++ b/polypath.c @@ -45,7 +45,8 @@ struct polypath { const char *const *ifname_pats; const char *const *monitor_command; bool_t permit_loopback; - LIST_HEAD(interf_list, interf) interfs; + LIST_HEAD(interf_list, interf) interfs_general; + struct interf_list interfs_dedicated; struct buffer_if lbuf; int monitor_fd; pid_t monitor_pid; @@ -53,6 +54,10 @@ struct polypath { int privsep_ipcsock_fd; }; +struct comm_clientinfo { + union iaddr dedicated; /* might be AF_UNSPEC */ +}; + static void polypath_phase_shutdown(void *sst, uint32_t newphase); #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc @@ -61,7 +66,7 @@ static const char *const default_loopback_ifname_pats[] = { "!lo", 0 }; static const char *const default_ifname_pats[] = { - "!tun*","!tap*","!sl*","!userv*", "*", 0 + "!tun*","!tap*","!sl*","!userv*", "@hippo*", "*", 0 }; static const char *const default_monitor_command[] = { @@ -119,6 +124,21 @@ static char ifname_wanted(struct polypath *st, struct cloc loc, abort(); } +static struct comm_clientinfo *polypath_clientinfo(void *state, + dict_t *dict, struct cloc cloc) { + struct comm_clientinfo *clientinfo; + + NEW(clientinfo); + FILLZERO(*clientinfo); + clientinfo->dedicated.sa.sa_family=AF_UNSPEC; + + item_t *item = dict_find_item(dict,"dedicated-interface-addr", + False,"polypath",cloc); + if (item) string_item_to_iaddr(item,0,&clientinfo->dedicated, + "polypath"); + return clientinfo; +} + static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io, int *timeout_io) { @@ -331,7 +351,8 @@ static void polypath_record_ifaddr(struct polypath *st, struct interf_list *interfs; switch (want) { - case '+': interfs=&st->interfs; max_interfs=st->max_interfs; + case '+': interfs=&st->interfs_general; max_interfs=st->max_interfs; + case '@': interfs=&st->interfs_dedicated; max_interfs=INT_MAX; default: fatal("polypath: got bad want (%#x, %s)", want, ifname); } @@ -452,6 +473,52 @@ static void polypath_afterpoll_monitor(void *state, struct pollfd *fds, } /* Actual udp packet sending work */ + +static void polypath_sendmsg_interf(struct polypath *st, + struct interf *interf, + struct buffer_if *buf, + const struct comm_addr *dest, + const union iaddr *dedicated, + bool_t *allreasonable) +{ + int i; + int af=dest->ia.sa.sa_family; + bool_t wanted=False, attempted=False, reasonable=False; + + for (i=0; isocks.n_socks; i++) { + struct udpsock *us=&interf->socks.socks[i]; + if (dedicated && !iaddr_equal(dedicated, &us->addr, True)) + continue; + wanted=True; + if (af != us->addr.sa.sa_family) + continue; + attempted=True; + int r=sendto(us->fd,buf->start,buf->size, + 0,&dest->ia.sa,iaddr_socklen(&dest->ia)); + udp_sock_experienced(0,&st->uc,&interf->socks,us, + &dest->ia,af, r,errno); + if (r>=0) { + reasonable=True; + break; + } + if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH)) + reasonable=True; + lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s", + interf->name,iaddr_to_string(&us->addr), + buf->size,iaddr_to_string(&dest->ia)); + } + if (!wanted) + return; + + if (!attempted) + if (!interf->experienced_xmit_noaf[af]++) + lg_perror(LG,M_WARNING,0, + "%s has no suitable address to transmit %s", + interf->name, af_name(af)); + + *allreasonable *= reasonable; +} + static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf, const struct comm_addr *dest, struct comm_clientinfo *clientinfo) @@ -459,36 +526,16 @@ static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf, struct polypath *st=commst; struct interf *interf; bool_t allreasonable=True; - int af=dest->ia.sa.sa_family; - LIST_FOREACH(interf,&st->interfs,entry) { - int i; - bool_t attempted=False, reasonable=False; - for (i=0; isocks.n_socks; i++) { - struct udpsock *us=&interf->socks.socks[i]; - if (af != us->addr.sa.sa_family) - continue; - attempted=True; - int r=sendto(us->fd,buf->start,buf->size, - 0,&dest->ia.sa,iaddr_socklen(&dest->ia)); - udp_sock_experienced(0,&st->uc,&interf->socks,us, - &dest->ia,af, r,errno); - if (r>=0) { - reasonable=True; - break; - } - if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH)) - reasonable=True; - lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s", - interf->name,iaddr_to_string(&us->addr), - buf->size,iaddr_to_string(&dest->ia)); + LIST_FOREACH(interf,&st->interfs_general,entry) { + polypath_sendmsg_interf(st,interf,buf,dest, + 0, &allreasonable); + } + if (clientinfo && clientinfo->dedicated.sa.sa_family != AF_UNSPEC) { + LIST_FOREACH(interf,&st->interfs_dedicated,entry) { + polypath_sendmsg_interf(st,interf,buf,dest, + &clientinfo->dedicated, &allreasonable); } - if (!attempted) - if (!interf->experienced_xmit_noaf[af]++) - lg_perror(LG,M_WARNING,0, - "%s has no suitable address to transmit %s", - interf->name, af_name(af)); - allreasonable *= reasonable; } return allreasonable; } @@ -575,7 +622,7 @@ struct privsep_mdata { bool_t add; char ifname[100]; union iaddr ia; - char want; /* `+', for now */ + char want; /* `+' or `@' */ }; static void papp_bad(struct polypath *st, void *badctx, @@ -823,7 +870,9 @@ static void polypath_phase_childpersist(void *sst, uint32_t newphase) struct polypath *st=sst; struct interf *interf; - LIST_FOREACH(interf,&st->interfs,entry) + LIST_FOREACH(interf,&st->interfs_general,entry) + udp_socks_childpersist(&st->uc,&interf->socks); + LIST_FOREACH(interf,&st->interfs_dedicated,entry) udp_socks_childpersist(&st->uc,&interf->socks); } @@ -841,6 +890,8 @@ static list_t *polypath_apply(closure_t *self, struct cloc loc, COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args); UDP_APPLY_STANDARD(st,&st->uc,"polypath"); + st->uc.cc.ops.clientinfo = polypath_clientinfo; + struct udpcommon *uc=&st->uc; struct commcommon *cc=&uc->cc; @@ -860,7 +911,8 @@ static list_t *polypath_apply(closure_t *self, struct cloc loc, st->permit_loopback=dict_read_bool(d,"permit-loopback",False, "polypath",cc->loc,False); - LIST_INIT(&st->interfs); + LIST_INIT(&st->interfs_general); + LIST_INIT(&st->interfs_dedicated); buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100); BUF_ALLOC(&st->lbuf,"polypath lbuf");