public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: Anders Ingemann <anders@ingemann.de>,
	David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/3] tap: Move tap4_handler() / tap6_handler() adddress checks to own function
Date: Tue, 24 Nov 2026 03:40:10 +0100	[thread overview]
Message-ID: <20261124024012.547787-2-sbrivio@redhat.com> (raw)
In-Reply-To: <20261124024012.547787-1-sbrivio@redhat.com>

This avoids some duplication and makes upcoming patches less ugly.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 tap.c | 60 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 25 deletions(-)

diff --git a/tap.c b/tap.c
index 100afc8..78030f3 100644
--- a/tap.c
+++ b/tap.c
@@ -660,6 +660,39 @@ static void tap_packet_debug(const struct iphdr *iph,
 	}
 }
 
+/**
+ * tap_filter_address() - Filter packets based on source and destination address
+ * @af:		Address family, AF_INET or AF_INET6
+ * @saddr:	Source address
+ * @daddr:	Destination address
+ *
+ * Return: true if packet must be dropped, false otherwise
+ */
+static bool tap_filter_address(sa_family_t af,
+			       const void *saddr, const void *daddr)
+{
+	/* The scope of sstr and dstr could be in theory reduced into the
+	 * conditional block debug() expands to, but it's awkward and
+	 * unreadable, so ignore this warning.
+	 */
+	/* cppcheck-suppress [variableScope,unmatchedSuppression] */
+	char sstr[INANY_ADDRSTRLEN], dstr[INANY_ADDRSTRLEN];
+
+	if (af == AF_INET &&
+	    !IN4_IS_ADDR_LOOPBACK(saddr) && !IN4_IS_ADDR_LOOPBACK(daddr))
+		return false;
+
+	if (af == AF_INET6 &&
+	    !IN6_IS_ADDR_LOOPBACK(saddr) && !IN6_IS_ADDR_LOOPBACK(daddr))
+		return false;
+
+	debug("Invalid address on tap interface: %s -> %s",
+	      inet_ntop(af, saddr, sstr, sizeof(sstr)),
+	      inet_ntop(af, daddr, dstr, sizeof(dstr)));
+
+	return true;
+}
+
 /**
  * tap4_is_fragment() - Determine if a packet is an IP fragment
  * @iph:	IPv4 header (length already validated)
@@ -741,20 +774,8 @@ resume:
 
 		l4len = htons(iph->tot_len) - hlen;
 
-		if (IN4_IS_ADDR_LOOPBACK(&iph->saddr) ||
-		    IN4_IS_ADDR_LOOPBACK(&iph->daddr)) {
-			/* The scope of sstr and dstr could be in theory reduced
-			 * into the conditional block debug() expands to, but
-			 * it's awkward and unreadable, so ignore this warning.
-			 */
-			/* cppcheck-suppress [variableScope,unmatchedSuppression] */
-			char sstr[INET_ADDRSTRLEN], dstr[INET_ADDRSTRLEN];
-
-			debug("Loopback address on tap interface: %s -> %s",
-			      inet_ntop(AF_INET, &iph->saddr, sstr, sizeof(sstr)),
-			      inet_ntop(AF_INET, &iph->daddr, dstr, sizeof(dstr)));
+		if (tap_filter_address(AF_INET, &iph->saddr, &iph->daddr))
 			continue;
-		}
 
 		if (!c->ip4.addr_fixed &&
 		    iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr)
@@ -983,19 +1004,8 @@ resume:
 		if (!ipv6_l4hdr(&data, &proto, &l4len))
 			continue;
 
-		if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) {
-			/* The scope of sstr and dstr could be in theory reduced
-			 * into the conditional block debug() expands to, but
-			 * it's awkward and unreadable, so ignore this warning.
-			 */
-			/* cppcheck-suppress [variableScope,unmatchedSuppression] */
-			char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN];
-
-			debug("Loopback address on tap interface: %s -> %s",
-			      inet_ntop(AF_INET6, saddr, sstr, sizeof(sstr)),
-			      inet_ntop(AF_INET6, daddr, dstr, sizeof(dstr)));
+		if (tap_filter_address(AF_INET6, saddr, daddr))
 			continue;
-		}
 
 		if (IN6_IS_ADDR_LINKLOCAL(saddr)) {
 			c->ip6.addr_ll_seen = *saddr;
-- 
2.43.0


  reply	other threads:[~2026-11-24  2:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  2:40 [PATCH 0/3] Throttle messages for invalid addresses, drop v4-mapped packets Stefano Brivio
2026-11-24  2:40 ` Stefano Brivio [this message]
2026-11-24  2:40 ` [PATCH 2/3] tap: Rate limit messages about filtered guest-side addresses Stefano Brivio
2026-11-24  2:40 ` [PATCH 3/3] tap: Drop IPv6 packets with outbound v4-mapped addresses as well Stefano Brivio
2026-11-24  2:59   ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124024012.547787-2-sbrivio@redhat.com \
    --to=sbrivio@redhat.com \
    --cc=anders@ingemann.de \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).