From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 25D285A02FF for ; Fri, 3 May 2024 03:11:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1714698697; bh=sQ3g1KFMX67BpvQtA8xTUe00tcXmPE5s2AuLdNC+dOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TLWqDiLDGN7qrdAT8k/K6Gyh35WRVqnbFp8S5xPwHtP5bZicvlyeOpNt39FAW9gS8 L0kEhJRgFPCFq9ropS8JMaIEnw7IofPnMyZQ8T0YhC72eD1D8gqvD4Bch4EYuCYz78 yYM1hIu346+JQffINjY2wx3of3F5lNduoPerYcB+y5U4FM3aIKALD/8HQKlv9KfaC8 CFiaYi7WHOYOCmu/bJYCNKxaESm1yFvO4ViqyfZbLGc/LQ4mZbzw4lfWiE+tHA7dlL wc+HIzlc7Kky4tLnrGaoOH4g2TdPRYOvHXxOqwhRewjQ8YibKY8OmL3NZLKhz8DaCu A1/Q48F/G/9Pw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4VVt6x5bFpz4xM1; Fri, 3 May 2024 11:11:37 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v4 06/16] tcp, tcp_splice: Construct sockaddrs for connect() from flowside Date: Fri, 3 May 2024 11:11:25 +1000 Message-ID: <20240503011135.2924437-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503011135.2924437-1-david@gibson.dropbear.id.au> References: <20240503011135.2924437-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: C5RVLC36GFOG7TQ2ZFCQ64RASEBM5IPQ X-Message-ID-Hash: C5RVLC36GFOG7TQ2ZFCQ64RASEBM5IPQ X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: In tcp_conn_from_tap() we currently generate the sockaddr we need to connect, and from it we fill in the sock flowside information. To make flowsides to be the primary source of truth, reverse this. We use a new sockaddr_from_inany() helper to build the sockaddr from the flowside. We also rearrange things a bit in a way that's now more natural. Similarly tcp_splice_connect() is given parameters from which it constructs a sockaddr in parallel to the address already in the flowside. Construct the sockaddr from the flowside instead. Signed-off-by: David Gibson --- inany.h | 28 +++++++++++++++++++++++++ tcp.c | 59 +++++++++++++++++++--------------------------------- tcp_splice.c | 36 ++++++++------------------------ 3 files changed, 58 insertions(+), 65 deletions(-) diff --git a/inany.h b/inany.h index c0228a1..6135f26 100644 --- a/inany.h +++ b/inany.h @@ -183,4 +183,32 @@ static inline void inany_siphash_feed(struct siphash_state *state, const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size); +/** sockaddr_from_inany - Construct a sockaddr from an inany + * @sa: Pointer to sockaddr to fill in + * @sl: Updated to relevant of length of initialised @sa + * @addr: IPv[46] address + * @port: Port (host byte order) + * @scope: Scope ID (ignored for IPv4 addresses) + */ +static inline void sockaddr_from_inany(union sockaddr_inany *sa, socklen_t *sl, + const union inany_addr *addr, + in_port_t port, uint32_t scope) +{ + const struct in_addr *v4 = inany_v4(addr); + + if (v4) { + sa->sa_family = AF_INET; + sa->sa4.sin_addr = *v4; + sa->sa4.sin_port = htons(port); + *sl = sizeof(sa->sa4); + } else { + sa->sa_family = AF_INET6; + sa->sa6.sin6_addr = addr->a6; + sa->sa6.sin6_port = htons(port); + sa->sa6.sin6_scope_id = scope; + sa->sa6.sin6_flowinfo = 0; + *sl = sizeof(sa->sa6); + } +} + #endif /* INANY_H */ diff --git a/tcp.c b/tcp.c index e669b18..9ba2b07 100644 --- a/tcp.c +++ b/tcp.c @@ -1929,19 +1929,9 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af, { in_port_t srcport = ntohs(th->source); in_port_t dstport = ntohs(th->dest); - struct sockaddr_in addr4 = { - .sin_family = AF_INET, - .sin_port = htons(dstport), - .sin_addr = *(struct in_addr *)daddr, - }; - struct sockaddr_in6 addr6 = { - .sin6_family = AF_INET6, - .sin6_port = htons(dstport), - .sin6_addr = *(struct in6_addr *)daddr, - }; struct flowside *tapside, *sockside; - const struct sockaddr *sa; struct tcp_tap_conn *conn; + union sockaddr_inany sa; union flow *flow; int s = -1, mss; socklen_t sl; @@ -1966,17 +1956,23 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af, goto cancel; } - if ((s = tcp_conn_sock(c, af)) < 0) - goto cancel; + sockside->pif = PIF_HOST; + sockside->eaddr = tapside->faddr; + sockside->eport = tapside->fport; if (!c->no_map_gw) { - if (af == AF_INET && IN4_ARE_ADDR_EQUAL(daddr, &c->ip4.gw)) - addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - if (af == AF_INET6 && IN6_ARE_ADDR_EQUAL(daddr, &c->ip6.gw)) - addr6.sin6_addr = in6addr_loopback; + struct in_addr *v4 = inany_v4(&sockside->eaddr); + + if (v4 && IN4_ARE_ADDR_EQUAL(v4, &c->ip4.gw)) + *v4 = in4addr_loopback; + if (IN6_ARE_ADDR_EQUAL(&sockside->eaddr, &c->ip6.gw)) + sockside->eaddr.a6 = in6addr_loopback; } - if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr)) { + if ((s = tcp_conn_sock(c, af)) < 0) + goto cancel; + + if (IN6_IS_ADDR_LINKLOCAL(&sockside->eaddr)) { struct sockaddr_in6 addr6_ll = { .sin6_family = AF_INET6, .sin6_addr = c->ip6.addr_ll, @@ -1984,6 +1980,8 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af, }; if (bind(s, (struct sockaddr *)&addr6_ll, sizeof(addr6_ll))) goto cancel; + } else if (!inany_is_loopback(&sockside->eaddr)) { + tcp_bind_outbound(c, s, af); } conn = FLOW_START(flow, FLOW_TCP, tcp, TAPSIDE); @@ -2006,19 +2004,6 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af, if (!(conn->wnd_from_tap = (htons(th->window) >> conn->ws_from_tap))) conn->wnd_from_tap = 1; - sockside->pif = PIF_HOST; - sockside->eport = dstport; - - if (af == AF_INET) { - inany_from_af(&sockside->eaddr, AF_INET, &addr4.sin_addr); - sa = (struct sockaddr *)&addr4; - sl = sizeof(addr4); - } else { - inany_from_af(&sockside->eaddr, AF_INET6, &addr6.sin6_addr); - sa = (struct sockaddr *)&addr6; - sl = sizeof(addr6); - } - conn->seq_init_from_tap = ntohl(th->seq); conn->seq_from_tap = conn->seq_init_from_tap + 1; conn->seq_ack_to_tap = conn->seq_from_tap; @@ -2028,19 +2013,17 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af, tcp_hash_insert(c, conn); - if (!bind(s, sa, sl)) { + sockaddr_from_inany(&sa, &sl, &sockside->eaddr, sockside->eport, + c->ifi6); + + if (!bind(s, &sa.sa, sl)) { tcp_rst(c, conn); /* Nobody is listening then */ return; } if (errno != EADDRNOTAVAIL && errno != EACCES) conn_flag(c, conn, LOCAL); - if ((af == AF_INET && !IN4_IS_ADDR_LOOPBACK(&addr4.sin_addr)) || - (af == AF_INET6 && !IN6_IS_ADDR_LOOPBACK(&addr6.sin6_addr) && - !IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr))) - tcp_bind_outbound(c, s, af); - - if (connect(s, sa, sl)) { + if (connect(s, &sa.sa, sl)) { if (errno != EINPROGRESS) { tcp_rst(c, conn); return; diff --git a/tcp_splice.c b/tcp_splice.c index 462ed0c..aa04a9b 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -321,31 +321,19 @@ static int tcp_splice_connect_finish(const struct ctx *c, * tcp_splice_connect() - Create and connect socket for new spliced connection * @c: Execution context * @conn: Connection pointer - * @af: Address family - * @pif: pif on which to create socket - * @port: Destination port, host order * * Return: 0 for connect() succeeded or in progress, negative value on error */ -static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn, - sa_family_t af, uint8_t pif, in_port_t port) +static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn) { - struct sockaddr_in6 addr6 = { - .sin6_family = AF_INET6, - .sin6_port = htons(port), - .sin6_addr = IN6ADDR_LOOPBACK_INIT, - }; - struct sockaddr_in addr4 = { - .sin_family = AF_INET, - .sin_port = htons(port), - .sin_addr = IN4ADDR_LOOPBACK_INIT, - }; - const struct sockaddr *sa; + const struct flowside *side1 = &conn->f.side[1]; + sa_family_t af = inany_v4(&side1->eaddr) ? AF_INET : AF_INET6; + union sockaddr_inany sa; socklen_t sl; - if (pif == PIF_HOST) + if (side1->pif == PIF_HOST) conn->s[1] = tcp_conn_sock(c, af); - else if (pif == PIF_SPLICE) + else if (side1->pif == PIF_SPLICE) conn->s[1] = tcp_conn_sock_ns(c, af); else ASSERT(0); @@ -359,15 +347,9 @@ static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn, conn->s[1]); } - if (CONN_V6(conn)) { - sa = (struct sockaddr *)&addr6; - sl = sizeof(addr6); - } else { - sa = (struct sockaddr *)&addr4; - sl = sizeof(addr4); - } + sockaddr_from_inany(&sa, &sl, &side1->eaddr, side1->eport, 0); - if (connect(conn->s[1], sa, sl)) { + if (connect(conn->s[1], &sa.sa, sl)) { if (errno != EINPROGRESS) { flow_trace(conn, "Couldn't connect socket for splice: %s", strerror(errno)); @@ -486,7 +468,7 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, in_port_t dstport, if (setsockopt(s0, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) flow_trace(conn, "failed to set TCP_QUICKACK on %i", s0); - if (tcp_splice_connect(c, conn, af, pif1, dstport)) + if (tcp_splice_connect(c, conn)) conn_flag(c, conn, CLOSING); return true; -- 2.44.0