public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 4/6] ndp: Use struct assignment in preference to memcpy() for IPv6 addresses
Date: Tue, 12 Nov 2024 15:06:16 +1100	[thread overview]
Message-ID: <20241112040618.1804081-5-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20241112040618.1804081-1-david@gibson.dropbear.id.au>

There are a number of places we can simply assign IPv6 addresses about,
rather than the current mildly ugly memcpy().

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 ndp.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/ndp.c b/ndp.c
index e876c34..5fda0f5 100644
--- a/ndp.c
+++ b/ndp.c
@@ -158,7 +158,7 @@ struct ndp_ra {
 
 	unsigned char var[sizeof(struct opt_mtu) + sizeof(struct opt_rdnss) +
 			  sizeof(struct opt_dnssl)];
-} __attribute__((packed));
+} __attribute__((packed, aligned(__alignof__(struct in6_addr))));
 
 /**
  * struct ndp_ns - NDP Neighbor Solicitation (NS) message
@@ -168,7 +168,7 @@ struct ndp_ra {
 struct ndp_ns {
 	struct icmp6hdr ih;
 	struct in6_addr target_addr;
-} __attribute__((packed));
+} __attribute__((packed, aligned(__alignof__(struct in6_addr))));
 
 /**
  * ndp_send() - Send an NDP message
@@ -192,7 +192,7 @@ static void ndp_send(const struct ctx *c, const struct in6_addr *dst,
  * @addr:	IPv6 address to advertise
  */
 static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
-		   const void *addr)
+	    const struct in6_addr *addr)
 {
 	struct ndp_na na = {
 		.ih = {
@@ -202,6 +202,7 @@ static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
 			.icmp6_solicited	= 1,
 			.icmp6_override		= 1,
 		},
+		.target_addr = *addr,
 		.target_l2_addr = {
 			.header	= {
 				.type		= OPT_TARGET_L2_ADDR,
@@ -210,7 +211,6 @@ static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
 		}
 	};
 
-	memcpy(&na.target_addr, addr, sizeof(na.target_addr));
 	memcpy(na.target_l2_addr.mac, c->our_tap_mac, ETH_ALEN);
 
 	ndp_send(c, dst, &na, sizeof(na));
@@ -242,6 +242,7 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 			.valid_lifetime		= ~0U,
 			.pref_lifetime		= ~0U,
 		},
+		.prefix = c->ip6.addr,
 		.source_ll = {
 			.header = {
 				.type		= OPT_SRC_L2_ADDR,
@@ -251,8 +252,6 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 	};
 	unsigned char *ptr = NULL;
 
-	memcpy(&ra.prefix, &c->ip6.addr, sizeof(ra.prefix));
-
 	ptr = &ra.var[0];
 
 	if (c->mtu != -1) {
@@ -282,8 +281,7 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 				.lifetime		= ~0U,
 			};
 			for (i = 0; i < n; i++) {
-				memcpy(&rdnss->dns[i], &c->ip6.dns[i],
-				       sizeof(rdnss->dns[i]));
+				rdnss->dns[i] = c->ip6.dns[i];
 			}
 			ptr += offsetof(struct opt_rdnss, dns) +
 				i * sizeof(rdnss->dns[0]);
-- 
@@ -158,7 +158,7 @@ struct ndp_ra {
 
 	unsigned char var[sizeof(struct opt_mtu) + sizeof(struct opt_rdnss) +
 			  sizeof(struct opt_dnssl)];
-} __attribute__((packed));
+} __attribute__((packed, aligned(__alignof__(struct in6_addr))));
 
 /**
  * struct ndp_ns - NDP Neighbor Solicitation (NS) message
@@ -168,7 +168,7 @@ struct ndp_ra {
 struct ndp_ns {
 	struct icmp6hdr ih;
 	struct in6_addr target_addr;
-} __attribute__((packed));
+} __attribute__((packed, aligned(__alignof__(struct in6_addr))));
 
 /**
  * ndp_send() - Send an NDP message
@@ -192,7 +192,7 @@ static void ndp_send(const struct ctx *c, const struct in6_addr *dst,
  * @addr:	IPv6 address to advertise
  */
 static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
-		   const void *addr)
+	    const struct in6_addr *addr)
 {
 	struct ndp_na na = {
 		.ih = {
@@ -202,6 +202,7 @@ static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
 			.icmp6_solicited	= 1,
 			.icmp6_override		= 1,
 		},
+		.target_addr = *addr,
 		.target_l2_addr = {
 			.header	= {
 				.type		= OPT_TARGET_L2_ADDR,
@@ -210,7 +211,6 @@ static void ndp_na(const struct ctx *c, const struct in6_addr *dst,
 		}
 	};
 
-	memcpy(&na.target_addr, addr, sizeof(na.target_addr));
 	memcpy(na.target_l2_addr.mac, c->our_tap_mac, ETH_ALEN);
 
 	ndp_send(c, dst, &na, sizeof(na));
@@ -242,6 +242,7 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 			.valid_lifetime		= ~0U,
 			.pref_lifetime		= ~0U,
 		},
+		.prefix = c->ip6.addr,
 		.source_ll = {
 			.header = {
 				.type		= OPT_SRC_L2_ADDR,
@@ -251,8 +252,6 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 	};
 	unsigned char *ptr = NULL;
 
-	memcpy(&ra.prefix, &c->ip6.addr, sizeof(ra.prefix));
-
 	ptr = &ra.var[0];
 
 	if (c->mtu != -1) {
@@ -282,8 +281,7 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
 				.lifetime		= ~0U,
 			};
 			for (i = 0; i < n; i++) {
-				memcpy(&rdnss->dns[i], &c->ip6.dns[i],
-				       sizeof(rdnss->dns[i]));
+				rdnss->dns[i] = c->ip6.dns[i];
 			}
 			ptr += offsetof(struct opt_rdnss, dns) +
 				i * sizeof(rdnss->dns[0]);
-- 
2.47.0


  parent reply	other threads:[~2024-11-12  4:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-12  4:06 [PATCH 0/6] ndp: Unsolicited RAs David Gibson
2024-11-12  4:06 ` [PATCH 1/6] ndp: Remove redundant update to addr_seen David Gibson
2024-11-12  4:06 ` [PATCH 2/6] ndp: Add ndp_send() helper David Gibson
2024-11-12  4:06 ` [PATCH 3/6] ndp: Split out helpers for sending specific NDP message types David Gibson
2024-11-13  1:14   ` Stefano Brivio
2024-11-13  2:07     ` David Gibson
2024-11-12  4:06 ` David Gibson [this message]
2024-11-12  4:06 ` [PATCH 5/6] ndp: Make route lifetime a #define David Gibson
2024-11-12  4:06 ` [PATCH 6/6] ndp: Send unsolicited Router Advertisements David Gibson
2024-11-13  1:14   ` Stefano Brivio
2024-11-13  3:01     ` David Gibson
2024-11-13  8:18       ` Stefano Brivio

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=20241112040618.1804081-5-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).