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=202508 header.b=F8+kxiIa; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 363F15A0279 for ; Thu, 21 Aug 2025 02:57:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202508; t=1755737844; bh=bhMZEwQaEZ952/r6LJNmxSX0EpjKomQJwHHE2287/B8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=F8+kxiIaiq1z4xcM9gf3rriVcGVcsjJOhkg0z7c0mybYLIDnFfKsaIT3xbImmUdNJ q6E0mwobyzpqGBQJhuqAp9gZi05b75FSyyBqKRyV293A4wcL5o0peNt6Cz29MtcKLE yTUYO+Okp6BHPp9iLUfkAiWInJgKP4n1XJUGQsiZmvzQ4WN0axmnloNCdv1my8QHh/ fVJDyL5W3KJIlV+KyURCmfQLgyyeiyU7KyguM2EL+KRp0tM9fLYqaJa3yPtrWACPTZ 1HD4OQmi2hpZ+DA3OmhFVnlvy12y/jj0/+ln/bCHWcEz7KPbp3q3Bl7J+ydnIZu+DM mTwVqcYhkgxzQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4c6lKJ0f3Kz4wcn; Thu, 21 Aug 2025 10:57:24 +1000 (AEST) Date: Thu, 21 Aug 2025 10:57:21 +1000 From: David Gibson To: Jon Maloy Subject: Re: [PATCH v4 1/9] netlink: add function to extract MAC addresses from NDP/ARP table Message-ID: References: <20250820031005.2725591-1-jmaloy@redhat.com> <20250820031005.2725591-2-jmaloy@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="9y9LlMVquLKVoMeK" Content-Disposition: inline In-Reply-To: <20250820031005.2725591-2-jmaloy@redhat.com> Message-ID-Hash: YFMIY7RNNLKLE6EOJ4BN6EIS2M5UGGQC X-Message-ID-Hash: YFMIY7RNNLKLE6EOJ4BN6EIS2M5UGGQC 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: sbrivio@redhat.com, dgibson@redhat.com, passt-dev@passt.top 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: --9y9LlMVquLKVoMeK Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Aug 19, 2025 at 11:09:57PM -0400, Jon Maloy wrote: > The solution to bug https://bugs.passt.top/show_bug.cgi?id=3D120 > requires the ability to translate from an IP address to its > corresponding MAC address in cases where those are present in > the ARP/NDP table. >=20 > We add this feature here. >=20 > Signed-off-by: Jon Maloy > --- > netlink.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ > netlink.h | 2 ++ > 2 files changed, 81 insertions(+) >=20 > diff --git a/netlink.c b/netlink.c > index 8f82e73..cf7debc 100644 > --- a/netlink.c > +++ b/netlink.c > @@ -800,6 +800,85 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t= af, > return status; > } > =20 > +/** > + * nl_neigh_mac_get() - Get neighbor MAC address from the kernel neigh t= able > + * @s: Netlink socket fd > + * @addr: IPv4 or IPv6 address > + * @ifi: Interface index > + * @mac: Buffer for Ethernet MAC, left unchanged if not found/usable > + * > + * Return: <0 on netlink error; 0 otherwise (drains all replies for @seq= ). > + */ > +int nl_neigh_mac_get(int s, const union inany_addr *addr, > + int ifi, unsigned char *mac) > +{ > + const void *ip =3D inany_v4(addr); > + struct req_t { > + struct nlmsghdr nlh; > + struct ndmsg ndm; > + struct rtattr rta; > + char ip[RTA_ALIGN(sizeof(struct in6_addr))]; > + } req; > + struct nlmsghdr *nh; > + char buf[NLBUFSIZ]; > + ssize_t status; > + uint32_t seq; > + int msglen; > + int iplen; > + > + memset(&req, 0, sizeof(req)); > + req.ndm.ndm_ifindex =3D ifi; > + req.rta.rta_type =3D NDA_DST; > + > + if (ip) { > + req.ndm.ndm_family =3D AF_INET; > + iplen =3D sizeof(struct in_addr); > + } else { > + req.ndm.ndm_family =3D AF_INET6; > + ip =3D &addr; > + iplen =3D sizeof(struct in6_addr); > + } > + > + req.rta.rta_len =3D RTA_LENGTH(iplen); > + memcpy(RTA_DATA(&req.rta), ip, iplen); > + msglen =3D NLMSG_ALIGN(sizeof(req.nlh) + sizeof(req.ndm) + RTA_LENGTH(i= plen)); > + seq =3D nl_send(s, &req, RTM_GETNEIGH, 0, msglen); > + > + /* Drain all RTM_NEWNEIGH replies for this seq */ > + nl_foreach_oftype(nh, status, s, buf, seq, RTM_NEWNEIGH) { > + struct ndmsg *ndm =3D NLMSG_DATA(nh); > + struct rtattr *rta =3D (struct rtattr *)(ndm + 1); > + const uint8_t *lladdr =3D NULL; > + size_t na =3D RTM_PAYLOAD(nh); > + const void *dst =3D NULL; > + size_t lladdr_len =3D 0; > + size_t dstlen =3D 0; > + > + for (; RTA_OK(rta, na); rta =3D RTA_NEXT(rta, na)) { > + switch (rta->rta_type) { > + case NDA_DST: > + dst =3D RTA_DATA(rta); > + dstlen =3D RTA_PAYLOAD(rta); > + break; > + case NDA_LLADDR: > + lladdr =3D RTA_DATA(rta); > + lladdr_len =3D RTA_PAYLOAD(rta); > + break; > + default: > + break; > + } > + } > + > + if (dst && dstlen =3D=3D (size_t)iplen && memcmp(dst, ip, iplen) =3D= =3D 0) { > + /* Only copy Ethernet-style addresses; leave unchanged otherwise */ > + if (lladdr && lladdr_len =3D=3D ETH_ALEN) You need to return an error code in the case that the MAC is missing or doesn't have the required form; otherwise you'll silently return garbage in @mac. > + memcpy(mac, lladdr, ETH_ALEN); > + } > + } > + > + return status; > +} > + > /** > * nl_addr_get_ll() - Get first IPv6 link-local address for a given inte= rface > * @s: Netlink socket > diff --git a/netlink.h b/netlink.h > index b51e99c..026c64f 100644 > --- a/netlink.h > +++ b/netlink.h > @@ -17,6 +17,8 @@ int nl_route_dup(int s_src, unsigned int ifi_src, > int s_dst, unsigned int ifi_dst, sa_family_t af); > int nl_addr_get(int s, unsigned int ifi, sa_family_t af, > void *addr, int *prefix_len, void *addr_l); > +int nl_neigh_mac_get(int s, const union inany_addr *addr, int ifi, > + unsigned char *mac); > int nl_addr_set(int s, unsigned int ifi, sa_family_t af, > const void *addr, int prefix_len); > int nl_addr_get_ll(int s, unsigned int ifi, struct in6_addr *addr); --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --9y9LlMVquLKVoMeK Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmimbvAACgkQzQJF27ox 2GdusRAAjo+elq2OhryFj6/kFgweWpaCh8YAOYjnWf7zPpG1NTF6dyZyF9QS8K2v eQp+vbKEFNfgdZk9xklER0lnx3qepce7cKbZ0qhPNnmRR76bY9rWnwwQkEdhe3WV fYX2rya05eWC0HTjtx2AAcMHt7inFUEM2rXGpbjzZ9RK8lhkYTxhHWZMIqaNwdSB A00dBgaWwU9xKbh5t1YFUCwBYGjV7W+xZRmjBwiU8sSQWvUJ7DuzUWUR0dmyrmj5 bqgH6h0UX9ZoOTe3kL88kBqTEd83ST2AhfsJRC9joqkl+eYI6YUjoUq0Ozt9pCIy MJK24BFXAnWxaaQGx5OlxSizVSDaNJQhQJStrUmg2tg6dT2TX+t+pK/qfTVs3kOX EH1JMnVXxwYAN5aKeAFdJ2NF6MkximUy1/az0dzVaYlCzM4FtL/ugUM5U4g9nOUi Mid8zVzOlQO56r8tHQUdtVPqEI3svddwjj26AQlo+GFjt4FWKx4aE7bkMoc4oY9s UeWDEi6ALfvwQhJ3jQFonnyWi1LSS7j03m4YDKhRRsX3hXU0YmXhD0FUh4bqwX1X ifJDbSqSQ969uyMVnRFRyin6RCWhWJMLPIZN3irQvdQS16GSAJ6EMvBiyt1r98vA EVO6FfU1EAVhMgKpXXtD/3X6kOGl4NDc+PoqQjMFHgQa9Y6KoQ8= =OabK -----END PGP SIGNATURE----- --9y9LlMVquLKVoMeK--