ndp: Use tap_icmp6_send() helper

We send ICMPv6 packets to the guest from both icmp.c and from ndp.c.  The
case in ndp() manually constructs L2 and IPv6 headers, unlike the version
in icmp.c which uses the tap_icmp6_send() helper from tap.c  Now that we've
broaded the parameters of tap_icmp6_send() we can use it in ndp() as well
saving some duplicated logic.

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 2022-10-19 11:43:55 +11:00 committed by Stefano Brivio
parent cb1edae3b5
commit db07804d26

21
ndp.c
View file

@ -47,6 +47,7 @@
*/ */
int ndp(struct ctx *c, const struct icmp6hdr *ih, const struct in6_addr *saddr) int ndp(struct ctx *c, const struct icmp6hdr *ih, const struct in6_addr *saddr)
{ {
const struct in6_addr *rsaddr; /* src addr for reply */
char buf[BUFSIZ] = { 0 }; char buf[BUFSIZ] = { 0 };
struct ipv6hdr *ip6hr; struct ipv6hdr *ip6hr;
struct icmp6hdr *ihr; struct icmp6hdr *ihr;
@ -180,26 +181,12 @@ dns_done:
else else
c->ip6.addr_seen = *saddr; c->ip6.addr_seen = *saddr;
ip6hr->daddr = *saddr;
if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw))
ip6hr->saddr = c->ip6.gw; rsaddr = &c->ip6.gw;
else else
ip6hr->saddr = c->ip6.addr_ll; rsaddr = &c->ip6.addr_ll;
ip6hr->payload_len = htons(sizeof(*ihr) + len); tap_icmp6_send(c, rsaddr, saddr, ihr, len + sizeof(*ihr));
csum_icmp6(ihr, &ip6hr->saddr, &ip6hr->daddr, ihr + 1, len);
ip6hr->version = 6;
ip6hr->nexthdr = IPPROTO_ICMPV6;
ip6hr->hop_limit = 255;
len += sizeof(*ehr) + sizeof(*ip6hr) + sizeof(*ihr);
memcpy(ehr->h_dest, c->mac_guest, ETH_ALEN);
memcpy(ehr->h_source, c->mac, ETH_ALEN);
ehr->h_proto = htons(ETH_P_IPV6);
if (tap_send(c, ehr, len) < 0)
perror("NDP: send");
return 1; return 1;
} }