From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gwtdRrmM; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id CFE585A0285 for ; Sat, 07 Jun 2025 03:11:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749258717; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=J+u4epZlVn0Ka85Bt2s0uGLadxCu07rin62472aeCJQ=; b=gwtdRrmMxhXg3gfWW+wVjfgUbzQ5T54aG8iZ/M/g+ANNjUdweCkyQYcEKuNHOWtfvxx1Vl 0lZ+rSIOGIyTWkckH1TxHVR/1Tnn80cneFrIQJotd65D+npcQM3GZqrwAxA2XUo57kQeFf /m6sdauKgonf6ZTM7e77HLDhmqP8Auw= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-640-cFbr4b9gNraTpP6tsWGQog-1; Fri, 06 Jun 2025 21:11:56 -0400 X-MC-Unique: cFbr4b9gNraTpP6tsWGQog-1 X-Mimecast-MFC-AGG-ID: cFbr4b9gNraTpP6tsWGQog_1749258715 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 88E85180047F for ; Sat, 7 Jun 2025 01:11:55 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.80.87]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 64576180045C; Sat, 7 Jun 2025 01:11:54 +0000 (UTC) From: Jon Maloy 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 Message-ID: <20250607011151.3290866-2-jmaloy@redhat.com> In-Reply-To: <20250607011151.3290866-1-jmaloy@redhat.com> References: <20250607011151.3290866-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Rjk8wVj01sX2HvC8bnBfZcw89Nhp0hZz3FshPcdMPEA_1749258715 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: ERHBO525ZMIEFXKQNCYQ27Y525MWSGDK X-Message-ID-Hash: ERHBO525ZMIEFXKQNCYQ27Y525MWSGDK X-MailFrom: jmaloy@redhat.com 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 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: 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 --- 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); -- 2.48.1