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=gcx33ChC; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 53D055A061D for ; Tue, 12 Nov 2024 05:06:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1731384378; bh=Jv7p7KZCltLaP91PeZNM1NM079ncOwBaJRg85jxH3cc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gcx33ChCipot0jwb0x7T6gX4ua2cRllQhOCtEt4blI/zMNqikl5e+nyASicZPNVyi YhLsGjDWenl1n9OADnHABcFE/AzCidgAb75uUgoCRjwVjHu5VcDdQqpNMP7lWMSFDB mGTunfuXhktb26cO4VgD8FrC1bkwjm9U1/fXLNdrQP22XXxCtikCRSXcR3dTXz9Y2C 5qjYpTmiaZMGJt8km1tQSGaEK54T4UQ4i3qyIWEDstaPChi7UpEr4mxKF+LlgV+Qte YzZeX2KlTy6unVpfQkP6WZRKoX27uIDd4xqrHPdI0Wzd4DEW1Of+nRaq0wA7RHtoUw 34P/BzfOktesw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XnXsQ28hyz4xCV; Tue, 12 Nov 2024 15:06:18 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 2/6] ndp: Add ndp_send() helper Date: Tue, 12 Nov 2024 15:06:14 +1100 Message-ID: <20241112040618.1804081-3-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: QBAZIPGRSJZFXVCMT3672JAHS33DRILN X-Message-ID-Hash: QBAZIPGRSJZFXVCMT3672JAHS33DRILN 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: 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 --- ndp.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/ndp.c b/ndp.c index ab80898..fa1b67a 100644 --- a/ndp.c +++ b/ndp.c @@ -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; -- 2.47.0