public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, dgibson@redhat.com, jmaloy@redhat.com,
	passt-dev@passt.top
Subject: [1/4] netlink: Add function to extract mac addresses from arp table
Date: Fri,  6 Jun 2025 21:11:48 -0400	[thread overview]
Message-ID: <20250607011151.3290866-2-jmaloy@redhat.com> (raw)
In-Reply-To: <20250607011151.3290866-1-jmaloy@redhat.com>

The solution to bug #120 requires the ability to translate from
an IP address to its corresponding MAC address in cases where
those are present in the ARP/NTP table.

We add this feature here.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
 netlink.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 netlink.h |  1 +
 2 files changed, 60 insertions(+)

diff --git a/netlink.c b/netlink.c
index ee9325a..625b60c 100644
--- a/netlink.c
+++ b/netlink.c
@@ -800,6 +800,65 @@ 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
+ * @mac:	Array to place the returned MAC address
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int nl_mac_get(int s, const union inany_addr *addr, unsigned char *mac)
+{
+	struct nlmsghdr *nlh;
+	char buf[NLBUFSIZ];
+	const void *ip;
+	ssize_t status;
+	uint32_t seq;
+	int alen;
+	struct {
+		struct nlmsghdr nlh;
+		struct ndmsg ndm;
+	} req;
+
+	if (IN6_IS_ADDR_V4MAPPED(&addr->a6)) {
+		ip = &addr->v4mapped.a4;
+		alen = sizeof(struct in_addr);
+		req.ndm.ndm_family = AF_INET;
+	} else {
+		ip = &addr->a6;
+		alen = sizeof(struct in6_addr);
+		req.ndm.ndm_family = AF_INET6;
+	}
+
+	seq = nl_send(s, &req, RTM_GETNEIGH, NLM_F_DUMP, sizeof(req));
+	nl_foreach_oftype(nlh, status, s, buf, seq, RTM_NEWNEIGH) {
+		struct ndmsg *ndm = NLMSG_DATA(nlh);
+		struct rtattr *attr = (struct rtattr *)(ndm + 1);
+		int attrlen = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ndm));
+		unsigned char *lladdr = NULL;
+		void *neigh_ip = NULL;
+
+		for (; RTA_OK(attr, attrlen); attr = RTA_NEXT(attr, attrlen)) {
+			if (attr->rta_type == NDA_DST)
+				neigh_ip = RTA_DATA(attr);
+			else if (attr->rta_type == NDA_LLADDR)
+				lladdr = RTA_DATA(attr);
+		}
+
+		if (!neigh_ip || !lladdr)
+			continue;
+
+		if (!memcmp(neigh_ip, ip, alen)) {
+			memcpy(mac, lladdr, ETH_ALEN);
+			return 0;
+		}
+	}
+	memcpy(mac, MAC_OUR_LAA, 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..2f674d7 100644
--- a/netlink.h
+++ b/netlink.h
@@ -17,6 +17,7 @@ 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, 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);
-- 
@@ -17,6 +17,7 @@ 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, 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);
-- 
2.48.1


  reply	other threads:[~2025-06-07  1:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-07  1:11 [0/4] use true mac address of LAN local remote hosts Jon Maloy
2025-06-07  1:11 ` Jon Maloy [this message]
2025-06-07  1:11 ` [2/4] arp: respond with " Jon Maloy
2025-06-07  1:11 ` [3/4] udp: forward external source mac address through tap interface Jon Maloy
2025-06-07  1:11 ` [4/4] tcp: " Jon Maloy

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=20250607011151.3290866-2-jmaloy@redhat.com \
    --to=jmaloy@redhat.com \
    --cc=dgibson@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).