From db07804d26fbd0d350d4d1c90dd9a43c5e4d9f93 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 19 Oct 2022 11:43:55 +1100 Subject: 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 Signed-off-by: Stefano Brivio --- ndp.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'ndp.c') diff --git a/ndp.c b/ndp.c index f96b4b7..80e1f19 100644 --- a/ndp.c +++ b/ndp.c @@ -47,6 +47,7 @@ */ 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 }; struct ipv6hdr *ip6hr; struct icmp6hdr *ihr; @@ -180,26 +181,12 @@ dns_done: else c->ip6.addr_seen = *saddr; - ip6hr->daddr = *saddr; if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) - ip6hr->saddr = c->ip6.gw; + rsaddr = &c->ip6.gw; else - ip6hr->saddr = c->ip6.addr_ll; + rsaddr = &c->ip6.addr_ll; - ip6hr->payload_len = htons(sizeof(*ihr) + len); - 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"); + tap_icmp6_send(c, rsaddr, saddr, ihr, len + sizeof(*ihr)); return 1; } -- cgit v1.2.3