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=202504 header.b=bSyVKhXK; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 42EE45A061F for ; Tue, 15 Apr 2025 09:16:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202504; t=1744701386; bh=m+wtySvbfYn/W3hFEK6BeKb4y3ZnLsHjBkUXWeGQDLU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bSyVKhXK06Bwu2s3wumTxaAvtlOiUtdsblzzp+Wf32ilTYQq71tab4XYH4bstYREr S+D1wFpZo7Tf7jYhzFZELsIVaLHqXbUisTW5+Zlr4u1lTBkpqRJCaPxL/zBgsvPjuc eJ4GjP8DhNOA/PXh0JWOh45UPeQ6EWbI0Fe9aJ17cdQorKRc3eX29enUifukrhJOYd RltLGx/8Aqkqeq99fOYWFh3oKlYogmR7W1aockNgufyjaAKigfQrmehbARJQKraAgG eZW/TcLmCw6K8sIL70iHabMBRHV6wnuAxc/BczIpVD5QSDb52lTCP2f3FmR8SRWxMs eIdhNppT8peHQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ZcFnk5Lp5z4xND; Tue, 15 Apr 2025 17:16:26 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 5/7] udp: Add udp_pktinfo() helper Date: Tue, 15 Apr 2025 17:16:22 +1000 Message-ID: <20250415071624.2618589-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250415071624.2618589-1-david@gibson.dropbear.id.au> References: <20250415071624.2618589-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DXH4NCUT5PTA3D6G4GSTVTESIU4W52Z3 X-Message-ID-Hash: DXH4NCUT5PTA3D6G4GSTVTESIU4W52Z3 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: Jon Maloy , 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: Currently we open code parsing the control message for IP_PKTINFO in udp_peek_addr(). We have an upcoming case where we want to parse PKTINFO in another place, so split this out into a helper function. While we're there, make the parsing a bit more robust: scan all cmsgs to look for the one we want, rather than assuming there's only one. Signed-off-by: David Gibson --- udp.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/udp.c b/udp.c index 0bec499d..352ab83b 100644 --- a/udp.c +++ b/udp.c @@ -464,6 +464,41 @@ static void udp_send_tap_icmp6(const struct ctx *c, tap_icmp6_send(c, saddr, eaddr, &msg, msglen); } +/** + * udp_pktinfo() - Retreive packet destination address from cmsg + * @msg: msghdr into which message has been received + * @dst: (Local) destination address of message in @mh (output) + * + * Return: 0 on success, -1 if the information was missing (@dst is set to + * inany_any6). + */ +static int udp_pktinfo(struct msghdr *msg, union inany_addr *dst) +{ + struct cmsghdr *hdr; + + for (hdr = CMSG_FIRSTHDR(msg); hdr; hdr = CMSG_NXTHDR(msg, hdr)) { + if (hdr->cmsg_level == IPPROTO_IP && + hdr->cmsg_type == IP_PKTINFO) { + const struct in_pktinfo *i4 = (void *)CMSG_DATA(hdr); + + *dst = inany_from_v4(i4->ipi_addr); + return 0; + } + + if (hdr->cmsg_level == IPPROTO_IPV6 && + hdr->cmsg_type == IPV6_PKTINFO) { + const struct in6_pktinfo *i6 = (void *)CMSG_DATA(hdr); + + dst->a6 = i6->ipi6_addr; + return 0; + } + } + + err("Missing PKTINFO cmsg on datagram"); + *dst = inany_any6; + return -1; +} + /** * udp_sock_recverr() - Receive and clear an error from a socket * @c: Execution context @@ -607,7 +642,6 @@ static int udp_peek_addr(int s, union sockaddr_inany *src, union inany_addr *dst) { char sastr[SOCKADDR_STRLEN], dstr[INANY_ADDRSTRLEN]; - const struct cmsghdr *hdr; char cmsg[PKTINFO_SPACE]; struct msghdr msg = { .msg_name = src, @@ -624,21 +658,7 @@ static int udp_peek_addr(int s, union sockaddr_inany *src, return -errno; } - hdr = CMSG_FIRSTHDR(&msg); - if (hdr && hdr->cmsg_level == IPPROTO_IP && - hdr->cmsg_type == IP_PKTINFO) { - const struct in_pktinfo *info4 = (void *)CMSG_DATA(hdr); - - *dst = inany_from_v4(info4->ipi_addr); - } else if (hdr && hdr->cmsg_level == IPPROTO_IPV6 && - hdr->cmsg_type == IPV6_PKTINFO) { - const struct in6_pktinfo *info6 = (void *)CMSG_DATA(hdr); - - dst->a6 = info6->ipi6_addr; - } else { - debug("Unexpected cmsg on UDP datagram"); - *dst = inany_any6; - } + udp_pktinfo(&msg, dst); trace("Peeked UDP datagram: %s -> %s", sockaddr_ntop(src, sastr, sizeof(sastr)), -- 2.49.0