1
0
mirror of https://passt.top/passt synced 2024-12-22 05:35:23 +00:00

ndp: Use struct assignment in preference to memcpy() for IPv6 addresses

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>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-11-14 14:33:06 +11:00 committed by Stefano Brivio
parent cbc83e14df
commit 36c070e6e3

14
ndp.c
View File

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