Every flow in the flow table now has space for the the addresses as seen by both the host and guest side. We already fill that information in for regular "tap" TCP connections, now do it for spliced connections too. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- tcp.c | 20 ++++++++++---------- tcp_splice.c | 31 +++++++++++++++++-------------- tcp_splice.h | 6 ++---- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/tcp.c b/tcp.c index 1835b86..cd5bffe 100644 --- a/tcp.c +++ b/tcp.c @@ -2731,22 +2731,16 @@ static void tcp_snat_inbound(const struct ctx *c, union inany_addr *addr) * @dstport: Destination port for connection (host side) * @flow: flow to initialise * @s: Accepted socket - * @sa: Peer socket address (from accept()) * @now: Current timestamp */ static void tcp_tap_conn_from_sock(struct ctx *c, in_port_t dstport, union flow *flow, int s, - const union sockaddr_inany *sa, const struct timespec *now) { - struct flowside *sockside = &flow->f.side[SOCKSIDE]; + const struct flowside *sockside = &flow->f.side[SOCKSIDE]; struct flowside *tapside = &flow->f.side[TAPSIDE]; struct tcp_tap_conn *conn; - sockside->pif = PIF_HOST; - inany_from_sockaddr(&sockside->eaddr, &sockside->eport, sa); - sockside->fport = dstport; - tapside->pif = PIF_TAP; tapside->faddr = sockside->eaddr; tapside->fport = sockside->eport; @@ -2792,16 +2786,23 @@ void tcp_listen_handler(struct ctx *c, union epoll_ref ref, { union sockaddr_inany sa; socklen_t sl = sizeof(sa); + struct flowside *side0; union flow *flow; int s; if (c->no_tcp || !(flow = flow_alloc())) return; + side0 = &flow->f.side[0]; + s = accept4(ref.fd, &sa.sa, &sl, SOCK_NONBLOCK); if (s < 0) goto cancel; + side0->pif = ref.tcp_listen.pif; + inany_from_sockaddr(&side0->eaddr, &side0->eport, &sa); + side0->fport = ref.tcp_listen.port; + if (sa.sa_family == AF_INET) { const struct in_addr *addr = &sa.sa4.sin_addr; in_port_t port = sa.sa4.sin_port; @@ -2829,11 +2830,10 @@ void tcp_listen_handler(struct ctx *c, union epoll_ref ref, } } - if (tcp_splice_conn_from_sock(c, ref.tcp_listen.pif, - ref.tcp_listen.port, flow, s, &sa)) + if (tcp_splice_conn_from_sock(c, ref.tcp_listen.port, flow, s)) return; - tcp_tap_conn_from_sock(c, ref.tcp_listen.port, flow, s, &sa, now); + tcp_tap_conn_from_sock(c, ref.tcp_listen.port, flow, s, now); return; cancel: diff --git a/tcp_splice.c b/tcp_splice.c index 42b7be0..462ed0c 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -414,42 +414,38 @@ static int tcp_conn_sock_ns(const struct ctx *c, sa_family_t af) /** * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection * @c: Execution context - * @pif0: pif id of side 0 * @dstport: Side 0 destination port of connection * @flow: flow to initialise * @s0: Accepted (side 0) socket - * @sa: Peer address of connection * * Return: true if able to create a spliced connection, false otherwise * #syscalls:pasta setsockopt */ -bool tcp_splice_conn_from_sock(const struct ctx *c, - uint8_t pif0, in_port_t dstport, - union flow *flow, int s0, - const union sockaddr_inany *sa) +bool tcp_splice_conn_from_sock(const struct ctx *c, in_port_t dstport, + union flow *flow, int s0) { + const struct flowside *side0 = &flow->f.side[0]; + const union inany_addr *src = &side0->eaddr; + struct flowside *side1 = &flow->f.side[1]; struct tcp_splice_conn *conn; - union inany_addr src; - in_port_t srcport; sa_family_t af; uint8_t pif1; if (c->mode != MODE_PASTA) return false; - inany_from_sockaddr(&src, &srcport, sa); - af = inany_v4(&src) ? AF_INET : AF_INET6; + af = inany_v4(src) ? AF_INET : AF_INET6; - switch (pif0) { + switch (side0->pif) { case PIF_SPLICE: - if (!inany_is_loopback(&src)) { + if (!inany_is_loopback(src)) { char str[INANY_ADDRSTRLEN]; /* We can't use flow_err() etc. because we haven't set * the flow type yet */ warn("Bad source address %s for splice, closing", - inany_ntop(&src, str, sizeof(str))); + inany_ntop(src, str, sizeof(str))); /* We *don't* want to fall back to tap */ flow_alloc_cancel(flow); @@ -461,7 +457,7 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, break; case PIF_HOST: - if (!inany_is_loopback(&src)) + if (!inany_is_loopback(src)) return false; pif1 = PIF_SPLICE; @@ -472,6 +468,13 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, return false; } + if (af == AF_INET) + flowside_from_af(side1, pif1, AF_INET, NULL, 0, + &in4addr_loopback, dstport); + else + flowside_from_af(side1, pif1, AF_INET6, NULL, 0, + &in6addr_loopback, dstport); + conn = FLOW_START(flow, FLOW_TCP_SPLICE, tcp_splice, 0); conn->flags = af == AF_INET ? 0 : SPLICE_V6; diff --git a/tcp_splice.h b/tcp_splice.h index ed8f0c5..e523c7e 100644 --- a/tcp_splice.h +++ b/tcp_splice.h @@ -11,10 +11,8 @@ union sockaddr_inany; void tcp_splice_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events); -bool tcp_splice_conn_from_sock(const struct ctx *c, - uint8_t pif0, in_port_t dstport, - union flow *flow, int s0, - const union sockaddr_inany *sa); +bool tcp_splice_conn_from_sock(const struct ctx *c, in_port_t dstport, + union flow *flow, int s0); void tcp_splice_init(struct ctx *c); #endif /* TCP_SPLICE_H */ -- 2.44.0