NetBSD Problem Report #8566

Received: (qmail 3149 invoked from network); 6 Oct 1999 11:24:38 -0000
Message-Id: <199910060629.CAA00429@groo-the-wanderer.waterside.net>
Date: Wed, 6 Oct 1999 02:29:23 -0400 (EDT)
From: rafal@mediaone.net
Reply-To: rafal@netbsd.org
To: gnats-bugs@gnats.netbsd.org
Subject: [rkb] Make 'arp(8)' output interface associated with each ARP entry
X-Send-Pr-Version: 3.95

>Number:         8566
>Category:       bin
>Synopsis:       an option to print the interface name for ARP entries is added
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    rafal
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Oct 06 04:35:00 +0000 1999
>Closed-Date:    Thu Nov 07 07:21:05 +0000 2002
>Last-Modified:  Thu Nov 07 07:21:05 +0000 2002
>Originator:     Rafal Boni
>Release:        -current as of 10/4
>Organization:
	...me organized? Ha!
>Environment:
System: NetBSD groo-the-wanderer.waterside.net 1.4K NetBSD 1.4K (GROO) #2: Mon Aug 30 07:10:25 EDT 1999 root@groo-the-wanderer.waterside.net:/usr/src/sys/arch/i386/compile/GROO i386


>Description:
	arp(8) does not print which interface an ARP entry is associated with.
	On machines multihomed machines, it's often useful to know which 
	interface the ARP entry is associated with (for debugging network
	topologies, etc.)

>How-To-Repeat:
	Run arp.  See that no interface is printed.
>Fix:

Index: arp.c
===================================================================
RCS file: /cvsroot/basesrc/usr.sbin/arp/arp.c,v
retrieving revision 1.23
diff -b -u -r1.23 arp.c
--- arp.c	1998/02/10 03:45:06	1.23
+++ arp.c	1999/10/06 11:17:56
@@ -58,6 +58,7 @@
 #include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/sysctl.h>
+#include <sys/ioctl.h>

 #include <net/if.h>
 #include <net/if_dl.h>
@@ -81,6 +82,7 @@
 int	delete __P((const char *, const char *));
 void	dump __P((u_long));
 void	sdl_print __P((const struct sockaddr_dl *));
+int	getifname __P((u_int16_t, char *));
 int	atosdl __P((const char *s, struct sockaddr_dl *sdl));
 int	file __P((char *));
 void	get __P((const char *));
@@ -94,7 +96,11 @@
 static int pid;
 static int nflag, vflag;
 static int s = -1;
+static int is = -1;

+static struct ifconf ifc;
+static char ifconfbuf[8192];
+
 int	delete __P((const char *, const char *));
 void	dump __P((u_long));
 void	ether_print __P((const u_char *));
@@ -390,6 +396,7 @@
 {
 	int mib[6];
 	size_t needed;
+	char ifname[IF_NAMESIZE];
 	char *host, *lim, *buf, *next;
 	struct rt_msghdr *rtm;
 	struct sockaddr_inarp *sin;
@@ -438,6 +445,12 @@
 			sdl_print(sdl);
 		else
 			(void)printf("(incomplete)");
+
+		if (sdl->sdl_index) {
+			if (getifname(sdl->sdl_index, ifname) == 0)
+				printf(" on %s", ifname);
+		}
+
 		if (rtm->rtm_rmx.rmx_expire == 0)
 			(void)printf(" permanent");
 		if (sin->sin_other & SIN_PROXY)
@@ -607,4 +620,58 @@
 	}
 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
 	return (0);
+}
+
+int
+getifname(ifindex, ifname)
+	u_int16_t ifindex;
+	char* ifname;
+{
+	int i, idx, siz;
+	char ifrbuf[8192];
+	struct ifreq ifreq, *ifr;
+	const struct sockaddr_dl *sdl = NULL;
+
+	if (is < 0) {
+		is = socket(PF_INET, SOCK_DGRAM, 0);
+		if (is < 0)
+			err(1, "socket");
+
+		ifc.ifc_len = sizeof(ifconfbuf);
+		ifc.ifc_buf = ifconfbuf;
+
+		if (ioctl(is, SIOCGIFCONF, &ifc) < 0) {
+			close(is);
+			err(1, "SIOCGIFCONF");
+			is = -1;
+		}
+	}
+
+	ifr = ifc.ifc_req;
+	ifreq.ifr_name[0] = '\0';
+	for (i = 0, idx = 0; i < ifc.ifc_len; ) {
+		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
+		siz = sizeof(ifr->ifr_name) +
+			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
+				? ifr->ifr_addr.sa_len
+				: sizeof(struct sockaddr));
+		i += siz;
+		/* avoid alignment issue */
+		if (sizeof(ifrbuf) < siz)
+			errx(1, "ifr too big");
+
+		memcpy(ifrbuf, ifr, siz);
+		ifr = (struct ifreq *)ifrbuf;
+
+		if (ifr->ifr_addr.sa_family != AF_LINK)
+			continue;
+
+		sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
+		if (sdl && sdl->sdl_index == ifindex) {
+			(void) strncpy(ifname, ifr->ifr_name, IF_NAMESIZE);
+			return 0;
+		}
+	}
+
+	return -1;
 }
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: fair 
State-Changed-When: Mon Mar 19 11:49:51 PST 2001 
State-Changed-Why:  
A modified version of your patch has been committed; pull up requests for 
both NetBSD 1.4 and NetBSD 1.5 have been submitted. If this is acceptable, 
we can close this PR. 

From: Rafal Boni <rafal@mediaone.net>
To: fair@netbsd.org
Cc:  
Subject: Re: bin/8566 
Date: Mon, 19 Mar 2001 16:08:12 -0500

 In message <20010319195216.24035.qmail@mail.netbsd.org>, you write: 

 -> Synopsis: an option to print the interface name for ARP entries is added
 -> 
 -> State-Changed-From-To: open->feedback
 -> State-Changed-By: fair
 -> State-Changed-When: Mon Mar 19 11:49:51 PST 2001
 -> State-Changed-Why: 
 -> A modified version of your patch has been committed; pull up requests for
 -> both NetBSD 1.4 and NetBSD 1.5 have been submitted. If this is acceptable,
 -> we can close this PR.

 I'll check this out and send feedback to gnats as soon as I get the updated
 copy and give it a whirl... I had actually all but forgotten this patch, but
 a while ago had meant to update it to use getifaddrs().  I'm not sure if you
 did or even if it's possible to do that and have it work on 1.4.x (not sure
 if getifaddrs() made it there).

 Thanks!
 --rafal

 ----
 Rafal Boni                                                  rafal@mediaone.net

From: "Erik E. Fair" <fair@clock.org>
To: Rafal Boni <rafal@mediaone.net>
Cc: NetBSD GNATS Problem Report Tracking System <gnats-bugs@gnats.netbsd.org>
Subject: Re: bin/8566
Date: Mon, 19 Mar 2001 18:12:44 -0800

 I did not update it for the new API; in fact, I undid "getprogname" 
 and one other thing. However, it's easy to convert back in the next 
 patch, which we would not pull up to 1.5 or 1.4...

 	Erik <fair@clock.org>
Responsible-Changed-From-To: bin-bug-people->fair 
Responsible-Changed-By: fair 
Responsible-Changed-When: Tue Mar 20 16:16:06 PST 2001 
Responsible-Changed-Why:  
a bit more effort is required, so I'll assign it to myself to keep it on the 
front burner. 

From: Rafal Boni <rafal@mediaone.net>
To: "Erik E. Fair" <fair@clock.org>
Cc: NetBSD GNATS Problem Report Tracking System <gnats-bugs@gnats.netbsd.org>
Subject: Re: bin/8566
Date: Wed, 05 Dec 2001 19:56:09 -0500

 What's been submitted is functionally OK for my needs, though the pullups
 seem to not have made it to the 1.5 branch yet (I was waiting for that to
 send `yes it's OK' feedback).

 I'll look at updating the -current arp for getifaddrs() API sometime soon
 too.

 THanks!
 --rafal

 ----
 Rafal Boni                                                   rafal@mediaone.net
Responsible-Changed-From-To: fair->rafal 
Responsible-Changed-By: fair 
Responsible-Changed-When: Fri Jan 4 18:05:30 PST 2002 
Responsible-Changed-Why:  
I'm reassigning this to Rafal Boni to get it off my plate, and to remind him 
that he was going to update the arp command for the new getifaddrs() API. 
This PR can be closed after 1.6 (or "Fred") is released because then the 
-current code will be in release. 
State-Changed-From-To: feedback->closed 
State-Changed-By: rafal 
State-Changed-When: Wed Nov 6 23:19:32 PST 2002 
State-Changed-Why:  
1.6 has been released, so per Erik's comment I'm closing this; I've also  
updated arp(8) to use getifaddrs() API and will check that in if I don't 
get any reasons not to from tech-net@ 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.