From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id B0FC05A027E for ; Tue, 22 Aug 2023 04:11:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1692670296; bh=5kvbrIr0A+2KItqzhxzYo/6i1953ee5IAa74rJvmrV8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ND7DfWQ5vIauRzbOJen4IaDZ2tMwhipZCGfrjpD7ZbgA12S31zdCEygpc1QoSBm8S SKE0irrUDcT+3HdhDKEI9GW48sBXChOvLBSj6trv0ARv2bbuOL0V360osahO+QI0Q1 AW/J4cyf3jw7wpH2GcxqiMHw3KdhsFmLKj62MYhc= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RVCWr3036z4x2F; Tue, 22 Aug 2023 12:11:36 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v3 6/9] tcp, udp: Don't pre-fill IPv4 destination address in headers Date: Tue, 22 Aug 2023 12:11:27 +1000 Message-ID: <20230822021130.450542-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230822021130.450542-1-david@gibson.dropbear.id.au> References: <20230822021130.450542-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WBAOHSR4BEVWNEYG3OODRZB25YZIT2ZB X-Message-ID-Hash: WBAOHSR4BEVWNEYG3OODRZB25YZIT2ZB 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: Because packets sent on the tap interface will always be going to the guest/namespace, we more-or-less know what address they'll be going to. So we pre-fill this destination address in our header buffers for IPv4. We can't do the same for IPv6 because we could need either the global or link-local address for the guest. In future we're going to want more flexibility for the destination address, so this pre-filling will get in the way. Change the flow so we always fill in the IPv4 destination address for each packet, rather than prefilling it from proto_update_l2_buf(). In fact for TCP we already redundantly filled the destination for each packet anyway. Signed-off-by: David Gibson --- passt.c | 10 ++++------ passt.h | 4 ++-- pasta.c | 2 +- tap.c | 8 +++----- tcp.c | 8 +------- tcp.h | 3 +-- udp.c | 9 ++------- udp.h | 3 +-- 8 files changed, 15 insertions(+), 32 deletions(-) diff --git a/passt.c b/passt.c index ca0acc6..8ddd9b3 100644 --- a/passt.c +++ b/passt.c @@ -117,13 +117,11 @@ static void timer_init(struct ctx *c, const struct timespec *now) * proto_update_l2_buf() - Update scatter-gather L2 buffers in protocol handlers * @eth_d: Ethernet destination address, NULL if unchanged * @eth_s: Ethernet source address, NULL if unchanged - * @ip_da: Pointer to IPv4 destination address, NULL if unchanged */ -void proto_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da) +void proto_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s) { - tcp_update_l2_buf(eth_d, eth_s, ip_da); - udp_update_l2_buf(eth_d, eth_s, ip_da); + tcp_update_l2_buf(eth_d, eth_s); + udp_update_l2_buf(eth_d, eth_s); } /** @@ -247,7 +245,7 @@ int main(int argc, char **argv) if (!c.no_icmp) icmp_init(); - proto_update_l2_buf(c.mac_guest, c.mac, &c.ip4.addr); + proto_update_l2_buf(c.mac_guest, c.mac); if (c.ifi4 && !c.no_dhcp) dhcp_init(); diff --git a/passt.h b/passt.h index 0500ff0..282bd1a 100644 --- a/passt.h +++ b/passt.h @@ -303,7 +303,7 @@ struct ctx { int low_rmem; }; -void proto_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da); +void proto_update_l2_buf(const unsigned char *eth_d, + const unsigned char *eth_s); #endif /* PASST_H */ diff --git a/pasta.c b/pasta.c index dbe8e8c..9bc26d5 100644 --- a/pasta.c +++ b/pasta.c @@ -353,7 +353,7 @@ void pasta_ns_conf(struct ctx *c) } } - proto_update_l2_buf(c->mac_guest, NULL, NULL); + proto_update_l2_buf(c->mac_guest, NULL); } /** diff --git a/tap.c b/tap.c index 4ed63f1..ee79be0 100644 --- a/tap.c +++ b/tap.c @@ -625,10 +625,8 @@ resume: l4_len = l3_len - hlen; - if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr) { + if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr) c->ip4.addr_seen.s_addr = iph->saddr; - proto_update_l2_buf(NULL, NULL, &c->ip4.addr_seen); - } l4h = packet_get(in, i, sizeof(*eh) + hlen, l4_len, NULL); if (!l4h) @@ -969,7 +967,7 @@ redo: if (memcmp(c->mac_guest, eh->h_source, ETH_ALEN)) { memcpy(c->mac_guest, eh->h_source, ETH_ALEN); - proto_update_l2_buf(c->mac_guest, NULL, NULL); + proto_update_l2_buf(c->mac_guest, NULL); } switch (ntohs(eh->h_proto)) { @@ -1030,7 +1028,7 @@ restart: if (memcmp(c->mac_guest, eh->h_source, ETH_ALEN)) { memcpy(c->mac_guest, eh->h_source, ETH_ALEN); - proto_update_l2_buf(c->mac_guest, NULL, NULL); + proto_update_l2_buf(c->mac_guest, NULL); } switch (ntohs(eh->h_proto)) { diff --git a/tcp.c b/tcp.c index c52ea2b..87f443a 100644 --- a/tcp.c +++ b/tcp.c @@ -997,10 +997,8 @@ static void tcp_update_check_tcp6(struct tcp6_l2_buf_t *buf) * tcp_update_l2_buf() - Update L2 buffers with Ethernet and IPv4 addresses * @eth_d: Ethernet destination address, NULL if unchanged * @eth_s: Ethernet source address, NULL if unchanged - * @ip_da: Pointer to IPv4 destination address, NULL if unchanged */ -void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da) +void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s) { int i; @@ -1014,10 +1012,6 @@ void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, tap_update_mac(&b6->taph, eth_d, eth_s); tap_update_mac(&b4f->taph, eth_d, eth_s); tap_update_mac(&b6f->taph, eth_d, eth_s); - - if (ip_da) { - b4f->iph.daddr = b4->iph.daddr = ip_da->s_addr; - } } } diff --git a/tcp.h b/tcp.h index 3454d9a..1608d58 100644 --- a/tcp.h +++ b/tcp.h @@ -26,8 +26,7 @@ void tcp_timer(struct ctx *c, const struct timespec *ts); void tcp_defer_handler(struct ctx *c); void tcp_sock_set_bufsize(const struct ctx *c, int s); -void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da); +void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s); /** * union tcp_epoll_ref - epoll reference portion for TCP connections diff --git a/udp.c b/udp.c index 6bb515a..f4ed660 100644 --- a/udp.c +++ b/udp.c @@ -276,10 +276,8 @@ static void udp_update_check4(struct udp4_l2_buf_t *buf) * udp_update_l2_buf() - Update L2 buffers with Ethernet and IPv4 addresses * @eth_d: Ethernet destination address, NULL if unchanged * @eth_s: Ethernet source address, NULL if unchanged - * @ip_da: Pointer to IPv4 destination address, NULL if unchanged */ -void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da) +void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s) { int i; @@ -289,10 +287,6 @@ void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, tap_update_mac(&b4->taph, eth_d, eth_s); tap_update_mac(&b6->taph, eth_d, eth_s); - - if (ip_da) { - b4->iph.daddr = ip_da->s_addr; - } } } @@ -578,6 +572,7 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport, ip_len = udp4_l2_mh_sock[n].msg_len + sizeof(b->iph) + sizeof(b->uh); b->iph.tot_len = htons(ip_len); + b->iph.daddr = c->ip4.addr_seen.s_addr; src_port = ntohs(b->s_in.sin_port); diff --git a/udp.h b/udp.h index f9d4459..f553de2 100644 --- a/udp.h +++ b/udp.h @@ -16,8 +16,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af, const void *addr, const char *ifname, in_port_t port); int udp_init(struct ctx *c); void udp_timer(struct ctx *c, const struct timespec *ts); -void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, - const struct in_addr *ip_da); +void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s); /** * union udp_epoll_ref - epoll reference portion for TCP connections -- 2.41.0