From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202410 header.b=bfmnLsNV; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 1E80C5A061D for ; Tue, 12 Nov 2024 05:06:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1731384378; bh=Lp+hceGy2O5WPRKfygkftcI4jLmn1chqaBWCtmxyHLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bfmnLsNVO4r77MtYwZ436XreDA1Zpo4r9WKWHKvgiLXbqmV5N6MCIkFYlZPB+Gp6V kCqJcLYJQjnzMcWkmAHkpdc+bjrT/sAL+hw3Rp5yGzYUfYCf9QqhAskgXZQC7p2wbi O2Vi8uNupZqWJk5x1CIxe5c9ohLCbZS5GdK7mpWQl46f4+gU4mkuED8vsHKkKhczFn QGOazQGjbEj/bpjiQQio22pLLbhI58CAXjzPIytobOc+EoqMZymQCm/OywGdIW7Zpc yWwss24btOB5HlmWZiOdL8NhpdTK6yXiRVm532YT0PpUq4jhtHOfS6ogL1fvdaZkh0 Gse4b0GGBt6xQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XnXsQ2Lw2z4xPy; Tue, 12 Nov 2024 15:06:18 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 4/6] ndp: Use struct assignment in preference to memcpy() for IPv6 addresses Date: Tue, 12 Nov 2024 15:06:16 +1100 Message-ID: <20241112040618.1804081-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241112040618.1804081-1-david@gibson.dropbear.id.au> References: <20241112040618.1804081-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WLSQGA7N3LRITAQRFRYQ7KLYZAJJXNFZ X-Message-ID-Hash: WLSQGA7N3LRITAQRFRYQ7KLYZAJJXNFZ 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: 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 --- 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]); -- 2.47.0