From 2c255a85f1a94490d53a0d50a11a7292322fdebb Mon Sep 17 00:00:00 2001 From: delphij Date: Fri, 26 Feb 2010 00:54:47 +0000 Subject: MFC 203052: Add interface description capability as inspired by OpenBSD. Thanks for rwatson@, jhb@, brooks@ and others for feedback to the old implementation! Sponsored by: iXsystems, Inc. --- sbin/ifconfig/ifconfig.8 | 12 +++++++++- sbin/ifconfig/ifconfig.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) (limited to 'sbin') diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index c372a08..32e8fa3 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -28,7 +28,7 @@ .\" From: @(#)ifconfig.8 8.3 (Berkeley) 1/5/94 .\" $FreeBSD$ .\" -.Dd December 7, 2009 +.Dd January 26, 2010 .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 , Cm descr 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 , Cm -descr +Clear the interface description. .It Cm down Mark an interface .Dq down . @@ -2493,6 +2499,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 diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index f05374c..ec5e403 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -44,7 +44,6 @@ static const char rcsid[] = #include #include #include -#include #include #include #include @@ -83,6 +82,8 @@ static const char rcsid[] = struct ifreq ifr; char name[IFNAMSIZ]; +char *descr = NULL; +size_t descrlen = 64; int setaddr; int setmask; int doalias; @@ -822,6 +823,40 @@ setifname(const char *val, int dummy __unused, int s, free(newname); } +/* ARGSUSED */ +static void +setifdescr(const char *val, int dummy __unused, int s, + const struct afswtch *afp) +{ + char *newdescr; + + ifr.ifr_buffer.length = strlen(val) + 1; + if (ifr.ifr_buffer.length == 1) { + ifr.ifr_buffer.buffer = newdescr = NULL; + ifr.ifr_buffer.length = 0; + } else { + newdescr = strdup(val); + ifr.ifr_buffer.buffer = newdescr; + if (newdescr == NULL) { + warn("no memory to set ifdescr"); + return; + } + } + + if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) + warn("ioctl (set 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 +901,25 @@ status(const struct afswtch *afp, const struct sockaddr_dl *sdl, printf(" mtu %d", ifr.ifr_mtu); putchar('\n'); + for (;;) { + if ((descr = reallocf(descr, descrlen)) != NULL) { + ifr.ifr_buffer.buffer = descr; + ifr.ifr_buffer.length = descrlen; + if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { + if (strlen(descr) > 0) + printf("\tdescription: %s\n", descr); + break; + } else if (errno == ENAMETOOLONG) + descrlen = ifr.ifr_buffer.length; + else + break; + } else { + warn("unable to allocate memory for interface" + "description"); + break; + } + }; + if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { if (ifr.ifr_curcap != 0) { printb("\toptions", ifr.ifr_curcap, IFCAPBITS); @@ -1035,6 +1089,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), -- cgit v1.1