public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Jon Maloy <jmaloy@redhat.com>
Cc: sbrivio@redhat.com, dgibson@redhat.com, passt-dev@passt.top
Subject: Re: [PATCH v4 1/9] netlink: add function to extract MAC addresses from NDP/ARP table
Date: Thu, 21 Aug 2025 10:57:21 +1000	[thread overview]
Message-ID: <aKZu8Tkf0NYqP1PD@zatzit> (raw)
In-Reply-To: <20250820031005.2725591-2-jmaloy@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 4167 bytes --]

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=120
> 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.
> 
> We add this feature here.
> 
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> ---
>  netlink.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  netlink.h |  2 ++
>  2 files changed, 81 insertions(+)
> 
> 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;
>  }
>  
> +/**
> + * nl_neigh_mac_get() - Get neighbor MAC address from the kernel neigh table
> + * @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 = 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 = ifi;
> +	req.rta.rta_type = NDA_DST;
> +
> +	if (ip) {
> +		req.ndm.ndm_family = AF_INET;
> +		iplen = sizeof(struct in_addr);
> +	} else {
> +		req.ndm.ndm_family = AF_INET6;
> +		ip = &addr;
> +		iplen = sizeof(struct in6_addr);
> +	}
> +
> +	req.rta.rta_len = RTA_LENGTH(iplen);
> +	memcpy(RTA_DATA(&req.rta), ip, iplen);
> +	msglen = NLMSG_ALIGN(sizeof(req.nlh) + sizeof(req.ndm) + RTA_LENGTH(iplen));
> +	seq = 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 = NLMSG_DATA(nh);
> +		struct rtattr *rta = (struct rtattr *)(ndm + 1);
> +		const uint8_t *lladdr = NULL;
> +		size_t na = RTM_PAYLOAD(nh);
> +		const void *dst = NULL;
> +		size_t lladdr_len = 0;
> +		size_t dstlen = 0;
> +
> +		for (; RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
> +			switch (rta->rta_type) {
> +			case NDA_DST:
> +				dst = RTA_DATA(rta);
> +				dstlen = RTA_PAYLOAD(rta);
> +				break;
> +			case NDA_LLADDR:
> +				lladdr = RTA_DATA(rta);
> +				lladdr_len = RTA_PAYLOAD(rta);
> +				break;
> +			default:
> +				break;
> +			}
> +		}
> +
> +		if (dst && dstlen == (size_t)iplen && memcmp(dst, ip, iplen) == 0) {
> +			/* Only copy Ethernet-style addresses; leave unchanged otherwise */
> +			if (lladdr && lladdr_len == 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 interface
>   * @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);

-- 
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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2025-08-21  0:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-20  3:09 [PATCH v4 0/9] Use true MAC address of LAN local remote hosts Jon Maloy
2025-08-20  3:09 ` [PATCH v4 1/9] netlink: add function to extract MAC addresses from NDP/ARP table Jon Maloy
2025-08-21  0:57   ` David Gibson [this message]
2025-08-20  3:09 ` [PATCH v4 2/9] arp/ndp: respond with true MAC address of LAN local remote hosts Jon Maloy
2025-08-21  1:18   ` David Gibson
2025-08-20  3:09 ` [PATCH v4 3/9] flow: add MAC address of LAN local remote hosts to flow Jon Maloy
2025-08-21  1:28   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 4/9] udp: forward external source MAC address through tap interface Jon Maloy
2025-08-21  1:32   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 5/9] tcp: " Jon Maloy
2025-08-21  1:37   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 6/9] tap: change signature of function tap_push_l2h() Jon Maloy
2025-08-21  1:39   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 7/9] tcp: make tcp_rst_no_conn() respond with correct MAC address Jon Maloy
2025-08-21  1:46   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 8/9] icmp: let icmp use mac address from flowside structure Jon Maloy
2025-08-21  1:51   ` David Gibson
2025-08-20  3:10 ` [PATCH v4 9/9] fwd: Added cache table for ARP/NDP contents Jon Maloy
2025-08-21  2:03   ` David Gibson
2025-08-21 10:53     ` Stefano Brivio
2025-08-25  1:48       ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aKZu8Tkf0NYqP1PD@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=dgibson@redhat.com \
    --cc=jmaloy@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).