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 v3 1/8] netlink: add function to extract MAC addresses from NDP/ARP table
Date: Tue, 22 Jul 2025 10:53:33 +1000	[thread overview]
Message-ID: <aH7hDTKh3OWlbueT@zatzit> (raw)
In-Reply-To: <20250629171348.86323-2-jmaloy@redhat.com>

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

On Sun, Jun 29, 2025 at 01:13:40PM -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>
> 
> ---
> v3: - Added an attribute contianing NDA_DST to sent message, so
>       that we let the kernel do the filtering of the IP address
>       and return only one entry.
>     - Added interface index to the call signature. Since the only
>       interface we know is the template interface, this limits
>       the number of hosts that will be seen as 'network segment
>       local' from a PASST viewpoint.
> ---
>  netlink.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  netlink.h |  2 ++
>  2 files changed, 64 insertions(+)
> 
> diff --git a/netlink.c b/netlink.c
> index ee9325a..16bb995 100644
> --- a/netlink.c
> +++ b/netlink.c
> @@ -800,6 +800,68 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t af,
>  	return status;
>  }
>  
> +/**
> + * nl_mac_get() - Get MAC address corresponding to given IP address
> + * @s:		Netlink socket
> + * @addr:	IPv4 or IPv6 address
> + * @ifi:	Interface index
> + * @mac:	Array to place the returned MAC address
> + *
> + * Return: 0 if found or not in table, negative error code on failure.
> + *         Leaves MAC array unchanged if no match found
> + */
> +int nl_mac_get(int s, const union inany_addr *addr, int ifi, unsigned char *mac)

Looking at the name alone, I'd think this got the local MAC, rather
than a neighbour MAC.  Maybe rename to nl_neigh_mac_get() or even just
nl_neigh_get()?

> +{
> +	struct req_t {
> +		struct nlmsghdr nlh;
> +		struct ndmsg ndm;
> +		struct rtattr rta;
> +		char ip[RTA_ALIGN(sizeof(struct in6_addr))];
> +	} req = {
> +		.ndm.ndm_ifindex = ifi,
> +		.rta.rta_type = NDA_DST
> +	};
> +	struct nlmsghdr *nh;
> +	char buf[NLBUFSIZ];
> +	const void *ip;
> +	ssize_t status;
> +	uint32_t seq;
> +	int msglen;
> +	int iplen;
> +
> +	if (inany_v4(addr)) {
> +		ip = &addr->v4mapped.a4;

I generally try to avoid reaching into inany internal fields,
particularly the v4 ones.  You could instead use:

	if ((ip = inany_v4(addr))) {
		...

> +		iplen = sizeof(struct in_addr);
> +		req.ndm.ndm_family = AF_INET;
> +	} else {
> +		ip = &addr->a6;

and here just ip = &addr.

> +		iplen = sizeof(struct in6_addr);
> +		req.ndm.ndm_family = AF_INET6;
> +	}
> +	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);
> +	nl_foreach_oftype(nh, status, s, buf, seq, RTM_NEWNEIGH) {
> +		struct ndmsg *ndm = NLMSG_DATA(nh);
> +		struct rtattr *rta = (struct rtattr *)(ndm + 1);
> +		size_t na = RTM_PAYLOAD(nh);
> +		bool found = false;
> +
> +		for (; RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
> +			if (rta->rta_type == NDA_DST) {
> +				if (memcmp(RTA_DATA(rta), ip, iplen) == 0)
> +					found = true;

Do you need this logic now that you're using the kernel filtering?  If
you do, is it guaranteed that the NDA_DST attribute will come before
the NDA_LLADDR attribute?  This code won't work if they're reversed.

> +			} else if (rta->rta_type == NDA_LLADDR && found) {
> +				memcpy(mac, RTA_DATA(rta), ETH_ALEN);

It's unusual, but the host interface could be something other than
ethernet, so you need to check the type/length of the host link level
address, in case it's not an ethernet style MAC address.

> +			}
> +		}
> +	}
> +
> +	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..51ba49f 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_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-07-22  1:44 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-29 17:13 [PATCH v3 0/8] use true MAC address of LAN local remote hosts Jon Maloy
2025-06-29 17:13 ` [PATCH v3 1/8] netlink: add function to extract MAC addresses from NDP/ARP table Jon Maloy
2025-07-22  0:53   ` David Gibson [this message]
2025-06-29 17:13 ` [PATCH v3 2/8] arp/ndp: respond with true MAC address of LAN local remote hosts Jon Maloy
2025-07-22  1:55   ` David Gibson
2025-06-29 17:13 ` [PATCH v3 3/8] flow: add MAC address of LAN local remote hosts to flow Jon Maloy
2025-07-22  2:12   ` David Gibson
2025-07-22  2:33     ` David Gibson
2025-06-29 17:13 ` [PATCH v3 4/8] udp: forward external source MAC address through tap interface Jon Maloy
2025-07-22  2:19   ` David Gibson
2025-06-29 17:13 ` [PATCH v3 5/8] tcp: " Jon Maloy
2025-07-22  2:29   ` David Gibson
2025-06-29 17:13 ` [PATCH v3 6/8] tap: change signature of function tap_push_l2h() Jon Maloy
2025-07-22  2:36   ` David Gibson
2025-06-29 17:13 ` [PATCH v3 7/8] tcp: make tcp_rst_no_conn() respond with correct MAC address Jon Maloy
2025-07-22  2:39   ` David Gibson
2025-06-29 17:13 ` [PATCH v3 8/8] icmp: let icmp use mac address from flowside structure Jon Maloy
2025-07-22  2:44   ` 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=aH7hDTKh3OWlbueT@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).