public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: [PATCH 3/7] Initialize host side MAC when in IPv6 only mode
Date: Fri, 22 Jul 2022 15:31:14 +1000	[thread overview]
Message-ID: <20220722053118.1067459-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20220722053118.1067459-1-david@gibson.dropbear.id.au>

[-- Attachment #1: Type: text/plain, Size: 2437 bytes --]

When sending packets to the guest we need a source MAC address, which we
currently take from the host side interface we're using (though it's
basically arbitrary).  However if not given on the command line this MAC
is initialized in an IPv4 specific path, and will end up as
00:00:00:00:00:00 when running "passt 6".  The MAC address is also used
for IPv6 packets, though.

Interestingly, we largely seem to get away with using an all-zero MAC, but
it's probably not a good idea.  Make the IPv6 path pick the MAC address
from its interface if the IPv4 path hasn't already done so.

While we're there, use the existing MAC_IS_ZERO macro to make the code a
little clearer.

Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au>
---
 conf.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/conf.c b/conf.c
index cc08de3..bc8851f 100644
--- a/conf.c
+++ b/conf.c
@@ -672,7 +672,7 @@ static void conf_ip(struct ctx *c)
 
 		memcpy(&c->addr4_seen, &c->addr4, sizeof(c->addr4_seen));
 
-		if (!memcmp(c->mac, MAC_ZERO, ETH_ALEN))
+		if (MAC_IS_ZERO(c->mac))
 			nl_link(0, c->ifi4, c->mac, 0, 0);
 	}
 
@@ -691,17 +691,20 @@ static void conf_ip(struct ctx *c)
 
 		memcpy(&c->addr6_seen, &c->addr6, sizeof(c->addr6));
 		memcpy(&c->addr6_ll_seen, &c->addr6_ll, sizeof(c->addr6_ll));
+
+		if (MAC_IS_ZERO(c->mac))
+			nl_link(0, c->ifi6, c->mac, 0, 0);
 	}
 
-	if (!c->gw4 || !c->addr4 ||
-	    !memcmp(c->mac, ((uint8_t [ETH_ALEN]){ 0 }), ETH_ALEN))
+	if (!c->gw4 || !c->addr4 || MAC_IS_ZERO(c->mac))
 		v4 = IP_VERSION_DISABLED;
 	else
 		v4 = IP_VERSION_ENABLED;
 
 	if (IN6_IS_ADDR_UNSPECIFIED(&c->gw6) ||
 	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6) ||
-	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6_ll))
+	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6_ll) ||
+	    MAC_IS_ZERO(c->mac))
 		v6 = IP_VERSION_DISABLED;
 	else
 		v6 = IP_VERSION_ENABLED;
@@ -905,12 +908,12 @@ static void conf_print(const struct ctx *c)
 	if (c->mode == MODE_PASTA)
 		info("Namespace interface: %s", c->pasta_ifn);
 
-	if (c->v4) {
-		info("ARP:");
-		info("    address: %02x:%02x:%02x:%02x:%02x:%02x",
-		     c->mac[0], c->mac[1], c->mac[2],
-		     c->mac[3], c->mac[4], c->mac[5]);
+	info("MAC:");
+	info("    host: %02x:%02x:%02x:%02x:%02x:%02x",
+	     c->mac[0], c->mac[1], c->mac[2],
+	     c->mac[3], c->mac[4], c->mac[5]);
 
+	if (c->v4) {
 		if (!c->no_dhcp) {
 			info("DHCP:");
 			info("    assign: %s",
-- 
@@ -672,7 +672,7 @@ static void conf_ip(struct ctx *c)
 
 		memcpy(&c->addr4_seen, &c->addr4, sizeof(c->addr4_seen));
 
-		if (!memcmp(c->mac, MAC_ZERO, ETH_ALEN))
+		if (MAC_IS_ZERO(c->mac))
 			nl_link(0, c->ifi4, c->mac, 0, 0);
 	}
 
@@ -691,17 +691,20 @@ static void conf_ip(struct ctx *c)
 
 		memcpy(&c->addr6_seen, &c->addr6, sizeof(c->addr6));
 		memcpy(&c->addr6_ll_seen, &c->addr6_ll, sizeof(c->addr6_ll));
+
+		if (MAC_IS_ZERO(c->mac))
+			nl_link(0, c->ifi6, c->mac, 0, 0);
 	}
 
-	if (!c->gw4 || !c->addr4 ||
-	    !memcmp(c->mac, ((uint8_t [ETH_ALEN]){ 0 }), ETH_ALEN))
+	if (!c->gw4 || !c->addr4 || MAC_IS_ZERO(c->mac))
 		v4 = IP_VERSION_DISABLED;
 	else
 		v4 = IP_VERSION_ENABLED;
 
 	if (IN6_IS_ADDR_UNSPECIFIED(&c->gw6) ||
 	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6) ||
-	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6_ll))
+	    IN6_IS_ADDR_UNSPECIFIED(&c->addr6_ll) ||
+	    MAC_IS_ZERO(c->mac))
 		v6 = IP_VERSION_DISABLED;
 	else
 		v6 = IP_VERSION_ENABLED;
@@ -905,12 +908,12 @@ static void conf_print(const struct ctx *c)
 	if (c->mode == MODE_PASTA)
 		info("Namespace interface: %s", c->pasta_ifn);
 
-	if (c->v4) {
-		info("ARP:");
-		info("    address: %02x:%02x:%02x:%02x:%02x:%02x",
-		     c->mac[0], c->mac[1], c->mac[2],
-		     c->mac[3], c->mac[4], c->mac[5]);
+	info("MAC:");
+	info("    host: %02x:%02x:%02x:%02x:%02x:%02x",
+	     c->mac[0], c->mac[1], c->mac[2],
+	     c->mac[3], c->mac[4], c->mac[5]);
 
+	if (c->v4) {
 		if (!c->no_dhcp) {
 			info("DHCP:");
 			info("    assign: %s",
-- 
2.37.1


  parent reply	other threads:[~2022-07-22  5:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-22  5:31 [PATCH 0/7] Improved selection of external interface and IP configuration David Gibson
2022-07-22  5:31 ` [PATCH 1/7] Allow different external interfaces for IPv4 and IPv6 connectivity David Gibson
2022-07-22  5:31 ` [PATCH 2/7] Separately locate external interfaces for IPv4 and IPv6 David Gibson
2022-08-01 10:23   ` Stefano Brivio
2022-07-22  5:31 ` David Gibson [this message]
2022-07-22  5:31 ` [PATCH 4/7] Move passt mac_guest init to be more symmetric with pasta David Gibson
2022-07-22  5:31 ` [PATCH 5/7] Clarify semantics of c->v4 and c->v6 variables David Gibson
2022-08-01 10:24   ` Stefano Brivio
2022-07-22  5:31 ` [PATCH 6/7] Separate IPv4 and IPv6 configuration David Gibson
2022-08-01 10:24   ` Stefano Brivio
2022-07-22  5:31 ` [PATCH 7/7] Make substructures for IPv4 and IPv6 specific context information 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=20220722053118.1067459-4-david@gibson.dropbear.id.au \
    --to=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).