From owner-freebsd-net@FreeBSD.ORG Sat Aug 15 00:32:38 2009 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1B565106568B; Sat, 15 Aug 2009 00:32:38 +0000 (UTC) (envelope-from delphij@delphij.net) Received: from tarsier.delphij.net (delphij-pt.tunnel.tserv2.fmt.ipv6.he.net [IPv6:2001:470:1f03:2c9::2]) by mx1.freebsd.org (Postfix) with ESMTP id D36C28FC5B; Sat, 15 Aug 2009 00:32:36 +0000 (UTC) Received: from tarsier.geekcn.org (tarsier.geekcn.org [211.166.10.233]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by tarsier.delphij.net (Postfix) with ESMTPS id C5C765C025; Sat, 15 Aug 2009 08:32:35 +0800 (CST) Received: from localhost (tarsier.geekcn.org [211.166.10.233]) by tarsier.geekcn.org (Postfix) with ESMTP id 8A3A955CDC3B; Sat, 15 Aug 2009 08:32:30 +0800 (CST) X-Virus-Scanned: amavisd-new at geekcn.org Received: from tarsier.geekcn.org ([211.166.10.233]) by localhost (mail.geekcn.org [211.166.10.233]) (amavisd-new, port 10024) with ESMTP id xtKur-2chUx8; Sat, 15 Aug 2009 08:31:36 +0800 (CST) Received: from charlie.delphij.net (adsl-76-237-33-62.dsl.pltn13.sbcglobal.net [76.237.33.62]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by tarsier.geekcn.org (Postfix) with ESMTPSA id 5810155CDC5B; Sat, 15 Aug 2009 08:31:28 +0800 (CST) DomainKey-Signature: a=rsa-sha1; s=default; d=delphij.net; c=nofws; q=dns; h=message-id:date:from:reply-to:organization:user-agent: mime-version:to:cc:subject:references:in-reply-to: x-enigmail-version:openpgp:content-type; b=RdV63K2kmX8Gvf2lqRIYRG6l6H3OtV2NGFehrUz07N8rbG0BI4d3i+Z/07aCDlvY8 j8RUlUmZ1INh8eT4RpgQg== Message-ID: <4A8601CE.5030205@delphij.net> Date: Fri, 14 Aug 2009 17:31:10 -0700 From: Xin LI Organization: The FreeBSD Project User-Agent: Thunderbird 2.0.0.22 (X11/20090803) MIME-Version: 1.0 To: Brooks Davis References: <4A83EEA8.5080202@delphij.net> <20090813182918.S93661@maildrop.int.zabbadoz.net> <20090814193303.GA21941@lor.one-eyed-alien.net> In-Reply-To: <20090814193303.GA21941@lor.one-eyed-alien.net> X-Enigmail-Version: 0.96.0 OpenPGP: id=18EDEBA0; url=http://www.delphij.net/delphij.asc Content-Type: multipart/mixed; boundary="------------070600020208080409030004" Cc: "Li, Qing" , d@delphij.net, freebsd-net@freebsd.org, Qing Li , "Andrey V. Elsukov" , Julian Elischer , "Bjoern A. Zeeb" Subject: [Take 2] Re: RFC: interface description X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: d@delphij.net List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 Aug 2009 00:32:38 -0000 This is a multi-part message in MIME format. --------------070600020208080409030004 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, guys, Here is a patch that implements the functionality in userland (as setifdescription/getifdescription functions in libutil); with this I think we can also provide an option that some kernel feature (like Qing Li said it might be useful for embedded systems, but of course the kernel feature would require more careful design) being used without modifying programs. This version uses if_dname plus if_dunit as distinguished name, and a Berkeley DB (hash, /etc/ifdescr.db) to store the information. In order to obtain if_dname and if_duint I had to create a new ioctl, as we don't seem to expose these two fields in an KBI-stable manner in the past. I have not took a look at bsnmp yet but I'll take a look at it to see if we have some better ways to distinguish the interface name. Cheers, - -- Xin LI http://www.delphij.net/ FreeBSD - The Power to Serve! -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.12 (FreeBSD) iEYEARECAAYFAkqGAc0ACgkQi+vbBBjt66D82QCeMV3G+2FepN5asaxvAwpc0qKS 1poAoIhQ719JdYPE7sq+lseTtszr++o0 =ph72 -----END PGP SIGNATURE----- --------------070600020208080409030004 Content-Type: text/plain; name="ifdescr-userland.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ifdescr-userland.diff" Index: sbin/ifconfig/ifconfig.c =================================================================== --- sbin/ifconfig/ifconfig.c (revision 196234) +++ sbin/ifconfig/ifconfig.c (working copy) @@ -63,6 +63,7 @@ static const char rcsid[] = #include #include +#include #include #include #include @@ -822,6 +823,24 @@ 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 __unused) +{ + + setifdescription(s, &ifr, val); +} + +/* ARGSUSED */ +static void +unsetifdescr(const char *val __unused, int dummy __unused, int s, + const struct afswtch *afp __unused) +{ + + setifdescription(s, &ifr, NULL); +} + #define IFFBITS \ "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ @@ -843,6 +862,7 @@ status(const struct afswtch *afp, const struct soc struct ifaddrs *ift; int allfamilies, s; struct ifstat ifs; + char *description = NULL; if (afp == NULL) { allfamilies = 1; @@ -866,6 +886,11 @@ status(const struct afswtch *afp, const struct soc printf(" mtu %d", ifr.ifr_mtu); putchar('\n'); + if (getifdescription(s, &ifr, &description) == 0 && description != NULL) + printf("\tdescription: %s\n", description); + free(description); + description = NULL; + if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { if (ifr.ifr_curcap != 0) { printb("\toptions", ifr.ifr_curcap, IFCAPBITS); @@ -1035,6 +1060,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: sbin/ifconfig/Makefile =================================================================== --- sbin/ifconfig/Makefile (revision 196234) +++ sbin/ifconfig/Makefile (working copy) @@ -27,8 +27,8 @@ SRCS+= ifgre.c # GRE keys etc SRCS+= ifgif.c # GIF reversed header workaround SRCS+= ifieee80211.c regdomain.c # SIOC[GS]IEEE80211 support -DPADD+= ${LIBBSDXML} ${LIBSBUF} ${LIBJAIL} -LDADD+= -lbsdxml -ljail -lsbuf +DPADD+= ${LIBBSDXML} ${LIBSBUF} ${LIBJAIL} ${LIBUTIL} +LDADD+= -lbsdxml -ljail -lsbuf -lutil SRCS+= ifcarp.c # SIOC[GS]VH support SRCS+= ifgroup.c # ... Index: lib/libutil/Makefile =================================================================== --- lib/libutil/Makefile (revision 196234) +++ lib/libutil/Makefile (working copy) @@ -10,11 +10,12 @@ SHLIB_MAJOR= 8 SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \ + if_description.c \ login.c login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \ stub.c trimdomain.c uucplock.c -INCS= libutil.h login_cap.h +INCS= if_description.h libutil.h login_cap.h WARNS?= 6 @@ -31,7 +32,7 @@ MAN+= kld.3 login.3 login_auth.3 login_tty.3 logou _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \ pidfile.3 flopen.3 expand_number.3 hexdump.3 \ - kinfo_getfile.3 kinfo_getvmmap.3 + kinfo_getfile.3 kinfo_getvmmap.3 if_description.3 MAN+= login.conf.5 auth.conf.5 MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 @@ -59,5 +60,6 @@ MLINKS+=pidfile.3 pidfile_open.3 \ pidfile.3 pidfile_write.3 \ pidfile.3 pidfile_close.3 \ pidfile.3 pidfile_remove.3 +MLINKS+= if_description.3 setifdescription.3 if_description.3 getifdescription.3 .include Index: lib/libutil/if_description.3 =================================================================== --- lib/libutil/if_description.3 (revision 0) +++ lib/libutil/if_description.3 (revision 0) @@ -0,0 +1,90 @@ +.\"- +.\" Copyright (c) 2009 Xin LI +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd September 15, 2009 +.Os +.Dt IFSETDESCRIPTION 3 +.Sh NAME +.Nm ifsetdescription , +.Nm ifgetdescription +.Nd ifnet description utility functions +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In if_description.h +.Ft int +.Fn setifdescription "int s" "struct ifreq *ifr" "const char *val" +.Ft int +.Fn getifdescription "int s" "struct ifreq *ifr" "char **val" +.Sh DESCRIPTION +These functions manipulates auxiliary interface description facility +from userland. +.Pp +The +.Fn setifdescription +function store the description +.Fa val +on the socket +.Fa s +referencing the interface and a struct ifreq pointer +.Fa ifr +that is already filled in with appropriate request information. +If +.Fa val +is NULL or is a pointer to empty string, +the interface description is removed. +.Pp +The +.Fn getifdescription +function reads and fills the description information into +.Fa *val +from the socket +.Fa s +with a struct ifreq pointer +.Fa ifr +that is already filled in with appropriate request information. +The previous +.Fa *val +will be freed and reallocated if it is not NULL. +.Sh RETURN VALUES +.Rv -std +.Sh SEE ALSO +.Xr netintro 4 , +.Sh HISTORY +The +.Fn ifsetdescription +and +.Fn ifgetdescrition +functions first appeared in +.Fx 9.0 . +.Sh AUTHORS +The +.Fn ifsetdescription +and +.Fn ifgetdescription +functions and this manual page were written by +.An Xin LI Aq delphij@FreeBSD.org . Property changes on: lib/libutil/if_description.3 ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: lib/libutil/if_description.c =================================================================== --- lib/libutil/if_description.c (revision 0) +++ lib/libutil/if_description.c (revision 0) @@ -0,0 +1,107 @@ +/*- + * Copyright (c) 2009 Xin LI + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +setifdescription(int s, struct ifreq *ifr, const char *val) +{ + DB *res; + DBT key, entry; + char dname[IFNAMSIZ]; + + bzero(dname, sizeof(dname)); + ifr->ifr_data = (caddr_t)dname; + + errno = ioctl(s, SIOCGIFDNAME, ifr); + if (errno == 0) { + res = dbopen(_PATH_IFDESCR_DB, + O_RDWR|O_CREAT|O_EXLOCK, + S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, + DB_HASH, NULL); + if (res != NULL) { + key.data = dname; + key.size = strlen(dname); + + if (val == NULL || strlen(val) == 0) { + (res->del)(res, &key, 0); + } else { + entry.data = __DECONST(void *, val); + entry.size = strlen(val) + 1; + (res->put)(res, &key, &entry, 0); + } + (res->close)(res); + return 0; + } + } + + return -1; +} + +int +getifdescription(int s, struct ifreq *ifr, char **val) +{ + DB *res; + DBT key, entry; + char dname[IFNAMSIZ]; + + bzero(dname, sizeof(dname)); + ifr->ifr_data = (caddr_t)dname; + + free(*val); + *val = NULL; + + errno = ioctl(s, SIOCGIFDNAME, ifr); + if (errno == 0) { + res = dbopen(_PATH_IFDESCR_DB, + O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL); + if (res != NULL) { + key.data = dname; + key.size = strlen(dname); + + if ((res->get)(res, &key, &entry, 0) == 0) { + *val = malloc(entry.size); + strlcpy(*val, entry.data, entry.size); + } + (res->close)(res); + } + } + return 0; +} + Property changes on: lib/libutil/if_description.c ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: lib/libutil/if_description.h =================================================================== --- lib/libutil/if_description.h (revision 0) +++ lib/libutil/if_description.h (revision 0) @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2009 Xin LI + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _IF_DESCRIPTION_H_ +#define _IF_DESCRIPTION_H_ + +#define _PATH_IFDESCR_DB "/etc/ifdescr.db" + +int setifdescription(int s, struct ifreq *ifr, const char *val); +int getifdescription(int s, struct ifreq *ifr, char **val); + +#endif /* _IF_DESCRIPTION_H_ */ Property changes on: lib/libutil/if_description.h ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: sys/net/if.c =================================================================== --- sys/net/if.c (revision 196234) +++ 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 dname[IFNAMSIZ]; struct ifaddr *ifa; struct sockaddr_dl *sdl; @@ -1965,6 +1966,18 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t d ifr->ifr_phys = ifp->if_physical; break; + case SIOCGIFDNAME: + bzero(dname, sizeof(dname)); + if (ifp->if_dunit != IF_DUNIT_NONE) + snprintf(dname, IFNAMSIZ, "%s%d", ifp->if_dname, + ifp->if_dunit); + else + strlcpy(dname, ifp->if_dname, sizeof(dname)); + error = copyout(dname, ifr->ifr_data, sizeof(dname)); + if (error) + return (error); + break; + case SIOCSIFFLAGS: error = priv_check(td, PRIV_NET_SETIFFLAGS); if (error) Index: sys/sys/sockio.h =================================================================== --- sys/sys/sockio.h (revision 196234) +++ sys/sys/sockio.h (working copy) @@ -82,6 +82,7 @@ #define SIOCGIFMAC _IOWR('i', 38, struct ifreq) /* get IF MAC label */ #define SIOCSIFMAC _IOW('i', 39, struct ifreq) /* set IF MAC label */ #define SIOCSIFNAME _IOW('i', 40, struct ifreq) /* set IF name */ +#define SIOCGIFDNAME _IOWR('i', 41, struct ifreq) /* get IF dname */ #define SIOCADDMULTI _IOW('i', 49, struct ifreq) /* add m'cast addr */ #define SIOCDELMULTI _IOW('i', 50, struct ifreq) /* del m'cast addr */ --------------070600020208080409030004--