Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Aug 2009 03:44:56 -0700
From:      Xin LI <delphij@delphij.net>
To:        freebsd-net@freebsd.org
Subject:   RFC: interface description
Message-ID:  <4A83EEA8.5080202@delphij.net>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------090505000704070004090000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi guys,

While playing with some OpenBSD installation I found that they have an
interesting feature - adding description to a NIC.  This is useful for
system administrators to "tag" the interface, also, the ladvd program
has a feature to use the SIOCSIFDESCR ioctl to document the remote CDP
peer like:

em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	ether 00:11:22:33:44:55
	description: connected to myrouter.home (CDP)
	[...]

The attached patch ported the feature to FreeBSD.

Cheers,
- --
Xin LI <delphij@delphij.net>	http://www.delphij.net/
FreeBSD - The Power to Serve!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (FreeBSD)

iEYEARECAAYFAkqD7qgACgkQi+vbBBjt66CF+QCeO6INwh3S1T/LvhIUTjZ/Ix4H
zQkAniftv+SQ+irEcnItGHTbLH0HyUez
=cEsJ
-----END PGP SIGNATURE-----

--------------090505000704070004090000
Content-Type: text/plain;
 name="ifdescr.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ifdescr.diff"

Index: sbin/ifconfig/ifconfig.8
===================================================================
--- sbin/ifconfig/ifconfig.8	(revision 196163)
+++ sbin/ifconfig/ifconfig.8	(working copy)
@@ -28,7 +28,7 @@
 .\"     From: @(#)ifconfig.8	8.3 (Berkeley) 1/5/94
 .\" $FreeBSD$
 .\"
-.Dd July 8, 2009
+.Dd September 14, 2009
 .Dt IFCONFIG 8
 .Os
 .Sh NAME
@@ -258,6 +258,12 @@ Disable permanently promiscuous mode.
 Another name for the
 .Fl alias
 parameter.
+.It Cm description Ar value
+Specify a description of the interface.
+This can be used to label interfaces in situations where they may
+otherwise be difficult to distinguish.
+.It Cm -description
+Clear the interface description.
 .It Cm down
 Mark an interface
 .Dq down .
@@ -2443,6 +2449,10 @@ Configure the interface
 to use 100baseTX, full duplex Ethernet media options:
 .Dl # ifconfig xl0 media 100baseTX mediaopt full-duplex
 .Pp
+Label the em0 interface as an uplink:
+.Pp
+.Dl # ifconfig em0 description \&"Uplink to Gigabit Switch 2\&"
+.Pp
 Create the software network interface
 .Li gif1 :
 .Dl # ifconfig gif1 create
Index: sbin/ifconfig/ifconfig.c
===================================================================
--- sbin/ifconfig/ifconfig.c	(revision 196163)
+++ sbin/ifconfig/ifconfig.c	(working copy)
@@ -83,6 +83,7 @@ static const char rcsid[] =
 struct	ifreq ifr;
 
 char	name[IFNAMSIZ];
+char	descr[IFDESCRSIZE];
 int	setaddr;
 int	setmask;
 int	doalias;
@@ -822,6 +823,36 @@ setifname(const char *val, int dummy __unused, int
 	free(newname);
 }
 
+/* ARGSUSED */
+static void
+setifdescr(const char *val, int dummy __unused, int s, 
+    const struct afswtch *afp)
+{
+	char *newdescr;
+
+	newdescr = strdup(val);
+	if (newdescr == NULL) {
+		warn("no memory to set ifdescr");
+		return;
+	}
+	ifr.ifr_data = newdescr;
+	if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
+		warn("ioctl (set descr)");
+		free(newdescr);
+		return;
+	}
+	strlcpy(descr, newdescr, sizeof(descr));
+	free(newdescr);
+}
+
+/* ARGSUSED */
+static void
+unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
+{
+
+	setifdescr("", 0, s, 0);
+}
+
 #define	IFFBITS \
 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
@@ -866,6 +897,11 @@ status(const struct afswtch *afp, const struct soc
 		printf(" mtu %d", ifr.ifr_mtu);
 	putchar('\n');
 
+	ifr.ifr_data = (caddr_t)&descr;
+	if (ioctl(s, SIOCGIFDESCR, &ifr) == 0 &&
+	    strlen(ifr.ifr_data) > 0)
+		printf("\tdescription: %s\n", ifr.ifr_data);
+
 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
 		if (ifr.ifr_curcap != 0) {
 			printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
@@ -1035,6 +1071,10 @@ static struct cmd basic_cmds[] = {
 	DEF_CMD("-arp",		IFF_NOARP,	setifflags),
 	DEF_CMD("debug",	IFF_DEBUG,	setifflags),
 	DEF_CMD("-debug",	-IFF_DEBUG,	setifflags),
+	DEF_CMD_ARG("description",		setifdescr),
+	DEF_CMD_ARG("descr",			setifdescr),
+	DEF_CMD("-description",	0,		unsetifdescr),
+	DEF_CMD("-descr",	0,		unsetifdescr),
 	DEF_CMD("promisc",	IFF_PPROMISC,	setifflags),
 	DEF_CMD("-promisc",	-IFF_PPROMISC,	setifflags),
 	DEF_CMD("add",		IFF_UP,		notealias),
Index: share/man/man4/netintro.4
===================================================================
--- share/man/man4/netintro.4	(revision 196163)
+++ share/man/man4/netintro.4	(working copy)
@@ -32,7 +32,7 @@
 .\"     @(#)netintro.4	8.2 (Berkeley) 11/30/93
 .\" $FreeBSD$
 .\"
-.Dd June 18, 2004
+.Dd September 15, 2009
 .Dt NETINTRO 4
 .Os
 .Sh NAME
@@ -277,6 +277,15 @@ and
 fields of the
 .Vt ifreq
 structure, respectively.
+.It Dv SIOCGIFDESCR Fa "struct ifreq *"
+Get the interface description, returned in the
+.Va ifru_data
+field.
+.It Dv SIOCSIFDESCR Fa "struct ifreq *"
+Set the interface description to the value of the
+.Va ifru_data
+field, limited to the size of
+.Dv IFDESCRSIZE .
 .It Dv SIOCSIFFLAGS
 Set interface flags field.
 If the interface is marked down,
Index: sys/kern/kern_jail.c
===================================================================
--- sys/kern/kern_jail.c	(revision 196163)
+++ sys/kern/kern_jail.c	(working copy)
@@ -3437,6 +3437,7 @@ prison_priv_check(struct ucred *cred, int priv)
 	case PRIV_NET_SETIFMETRIC:
 	case PRIV_NET_SETIFPHYS:
 	case PRIV_NET_SETIFMAC:
+	case PRIV_NET_SETIFDESCR:
 	case PRIV_NET_ADDMULTI:
 	case PRIV_NET_DELMULTI:
 	case PRIV_NET_HWIOCTL:
Index: sys/net/if.c
===================================================================
--- sys/net/if.c	(revision 196163)
+++ sys/net/if.c	(working copy)
@@ -1927,6 +1927,7 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t d
 	int new_flags, temp_flags;
 	size_t namelen, onamelen;
 	char new_name[IFNAMSIZ];
+	char ifdescrbuf[IFDESCRSIZE];
 	struct ifaddr *ifa;
 	struct sockaddr_dl *sdl;
 
@@ -1965,6 +1966,25 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t d
 		ifr->ifr_phys = ifp->if_physical;
 		break;
 
+	case SIOCGIFDESCR:
+		bzero(ifdescrbuf, sizeof(ifdescrbuf));
+		strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE);
+		error = copyout(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE);
+		break;
+
+	case SIOCSIFDESCR:
+		error = priv_check(td, PRIV_NET_SETIFDESCR);
+		if (error)
+			return (error);
+		error = copyinstr(ifr->ifr_data, ifdescrbuf,
+			IFDESCRSIZE, NULL);
+		if (error == 0) {
+			bzero(ifp->if_description, IFDESCRSIZE);
+			strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE);
+			getmicrotime(&ifp->if_lastchange);
+		}
+		break;
+
 	case SIOCSIFFLAGS:
 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
 		if (error)
Index: sys/net/if.h
===================================================================
--- sys/net/if.h	(revision 196163)
+++ sys/net/if.h	(working copy)
@@ -60,6 +60,10 @@ struct ifnet;
 #define		IFNAMSIZ	IF_NAMESIZE
 #define		IF_MAXUNIT	0x7fff	/* historical value */
 #endif
+
+/* Length of interface description, including terminating '\0'. */
+#define		IFDESCRSIZE	64
+
 #if __BSD_VISIBLE
 
 /*
Index: sys/net/if_var.h
===================================================================
--- sys/net/if_var.h	(revision 196163)
+++ sys/net/if_var.h	(working copy)
@@ -197,6 +197,7 @@ struct ifnet {
 	void	*if_pf_kif;
 	void	*if_lagg;		/* lagg glue */
 	u_char	 if_alloctype;		/* if_type at time of allocation */
+	char	if_description[IFDESCRSIZE];	/* interface description */
 
 	/*
 	 * Spare fields are added so that we can modify sensitive data
Index: sys/sys/priv.h
===================================================================
--- sys/sys/priv.h	(revision 196163)
+++ sys/sys/priv.h	(working copy)
@@ -335,6 +335,7 @@
 #define	PRIV_NET_LAGG		415	/* Administer lagg interface. */
 #define	PRIV_NET_GIF		416	/* Administer gif interface. */
 #define	PRIV_NET_SETIFVNET	417	/* Move interface to vnet. */
+#define	PRIV_NET_SETIFDESCR	418	/* Set interface description. */
 
 /*
  * 802.11-related privileges.

--------------090505000704070004090000--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4A83EEA8.5080202>