1
0
mirror of https://passt.top/passt synced 2024-12-22 13:45:32 +00:00

ndp: Add ndp_send() helper

ndp() has a conditional on message type generating the reply message, then
a tiny amount of common code, then another conditional to send the reply
with slightly different parameters.  We can make this a bit neater by
making a helper function for sending the reply, and call it from each of
the different message type paths.

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:04 +11:00 committed by Stefano Brivio
parent 71f228d04b
commit 4e47167035

32
ndp.c
View File

@ -170,6 +170,21 @@ struct ndp_ns {
struct in6_addr target_addr;
} __attribute__((packed));
/**
* ndp_send() - Send an NDP message
* @c: Execution context
* @dst: IPv6 address to send the message to
* @buf: ICMPv6 header + message payload
* @l4len: Length of message, including ICMPv6 header
*/
static void ndp_send(const struct ctx *c, const struct in6_addr *dst,
const void *buf, size_t l4len)
{
const struct in6_addr *src = &c->ip6.our_tap_ll;
tap_icmp6_send(c, src, dst, buf, l4len);
}
/**
* ndp() - Check for NDP solicitations, reply as needed
* @c: Execution context
@ -223,9 +238,6 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
},
},
};
const struct in6_addr *rsaddr; /* src addr for reply */
unsigned char *ptr = NULL;
size_t dlen;
if (ih->icmp6_type < RS || ih->icmp6_type > NA)
return 0;
@ -249,7 +261,9 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
sizeof(na.target_addr));
memcpy(na.target_l2_addr.mac, c->our_tap_mac, ETH_ALEN);
ndp_send(c, saddr, &na, sizeof(struct ndp_na));
} else if (ih->icmp6_type == RS) {
unsigned char *ptr = NULL;
size_t dns_s_len = 0;
int i, n;
@ -332,18 +346,8 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
dns_done:
memcpy(&ra.source_ll.mac, c->our_tap_mac, ETH_ALEN);
} else {
return 1;
}
rsaddr = &c->ip6.our_tap_ll;
if (ih->icmp6_type == NS) {
dlen = sizeof(struct ndp_na);
tap_icmp6_send(c, rsaddr, saddr, &na, dlen);
} else if (ih->icmp6_type == RS) {
dlen = ptr - (unsigned char *)&ra;
tap_icmp6_send(c, rsaddr, saddr, &ra, dlen);
ndp_send(c, saddr, &ra, ptr - (unsigned char *)&ra);
}
return 1;