Index: sys/net/if.c =================================================================== RCS file: /cvs/src/sys/net/if.c,v retrieving revision 1.13 diff -u -p -u -r1.13 if.c --- sys/net/if.c 4 Mar 2004 10:29:23 -0000 1.13 +++ sys/net/if.c 15 Mar 2004 21:29:30 -0000 @@ -31,7 +31,7 @@ * SUCH DAMAGE. * * @(#)if.c 8.3 (Berkeley) 1/4/94 - * $FreeBSD: src/sys/net/if.c,v 1.85.2.23 2003/04/15 18:11:19 fjoe Exp $ + * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $ * $DragonFly: src/sys/net/if.c,v 1.13 2004/03/04 10:29:23 hsu Exp $ */ @@ -999,6 +999,10 @@ ifioctl(struct socket *so, u_long cmd, c int error; short oif_flags; int new_flags; + size_t namelen, onamelen; + char new_name[IFNAMSIZ]; + struct ifaddr *ifa; + struct sockaddr_dl *sdl; switch (cmd) { @@ -1090,6 +1094,48 @@ ifioctl(struct socket *so, u_long cmd, c return (EINVAL); (void) (*ifp->if_ioctl)(ifp, cmd, data); break; + + case SIOCSIFNAME: + error = suser(td); + if (error != 0) + return (error); + error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); + if (error != 0) + return (error); + if (new_name[0] == '\0') + return (EINVAL); + if (ifunit(new_name) != NULL) + return (EEXIST); + + /* Announce the departure of the interface. */ + rt_ifannouncemsg(ifp, IFAN_DEPARTURE); + + strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); + ifa = TAILQ_FIRST(&ifp->if_addrhead); + /* XXX IFA_LOCK(ifa); */ + sdl = (struct sockaddr_dl *)ifa->ifa_addr; + namelen = strlen(new_name); + onamelen = sdl->sdl_nlen; + /* + * Move the address if needed. This is safe because we + * allocate space for a name of length IFNAMSIZ when we + * create this in if_attach(). + */ + if (namelen != onamelen) { + bcopy(sdl->sdl_data + onamelen, + sdl->sdl_data + namelen, sdl->sdl_alen); + } + bcopy(new_name, sdl->sdl_data, namelen); + sdl->sdl_nlen = namelen; + sdl = (struct sockaddr_dl *)ifa->ifa_netmask; + bzero(sdl->sdl_data, onamelen); + while (namelen != 0) + sdl->sdl_data[--namelen] = 0xff; + /* XXX IFA_UNLOCK(ifa) */ + + /* Announce the return of the interface. */ + rt_ifannouncemsg(ifp, IFAN_ARRIVAL); + break; case SIOCSIFMETRIC: error = suser(td); Index: sys/sys/sockio.h =================================================================== RCS file: /cvs/src/sys/sys/sockio.h,v retrieving revision 1.2 diff -u -p -u -r1.2 sockio.h --- sys/sys/sockio.h 17 Jun 2003 04:28:58 -0000 1.2 +++ sys/sys/sockio.h 15 Mar 2004 21:29:33 -0000 @@ -80,6 +80,7 @@ #define SIOCDLIFADDR _IOW('i', 29, struct if_laddrreq) /* delete IF addr */ #define SIOCSIFCAP _IOW('i', 30, struct ifreq) /* set IF features */ #define SIOCGIFCAP _IOWR('i', 31, struct ifreq) /* get IF features */ +#define SIOCSIFNAME _IOW('i', 40, struct ifreq) /* set IF name */ #define SIOCADDMULTI _IOW('i', 49, struct ifreq) /* add m'cast addr */ #define SIOCDELMULTI _IOW('i', 50, struct ifreq) /* del m'cast addr */ Index: ifconfig/ifconfig.c =================================================================== RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v retrieving revision 1.6 diff -u -p -u -r1.6 ifconfig.c --- ifconfig/ifconfig.c 4 Feb 2004 17:39:59 -0000 1.6 +++ ifconfig/ifconfig.c 15 Mar 2004 21:30:40 -0000 @@ -32,7 +32,7 @@ * * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved. * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94 - * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.51.2.19 2003/01/28 11:02:56 fjoe Exp $ + * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.96 2004/02/27 06:43:14 kan Exp $ * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.6 2004/02/04 17:39:59 joerg Exp $ */ @@ -113,7 +113,7 @@ struct in6_aliasreq in6_addreq = struct sockaddr_in netmask; struct netrange at_nr; /* AppleTalk net range */ -char name[32]; +char name[IFNAMSIZ]; int flags; int setaddr; int setipdst; @@ -129,6 +129,7 @@ struct afswtch; int supmedia = 0; int listcloners = 0; +int printname = 0; /* Print the name of the created interface. */ #ifdef INET6 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ @@ -172,6 +173,7 @@ c_func setip6eui64; c_func setifipdst; c_func setifflags, setifmetric, setifmtu, setifcap; c_func clone_destroy; +c_func setifname; void clone_create(void); @@ -278,6 +280,7 @@ struct cmd { { "compress", IFF_LINK0, setifflags }, { "noicmp", IFF_LINK1, setifflags }, { "mtu", NEXTARG, setifmtu }, + { "name", NEXTARG, setifname }, { 0, 0, setifaddr }, { 0, 0, setifdstaddr }, }; @@ -523,7 +526,7 @@ main(int argc, char * const *argv) clone_create(); argc--, argv++; if (argc == 0) - exit(0); + goto end; } } @@ -591,6 +594,9 @@ main(int argc, char * const *argv) addrcount++; next += nextifm->ifm_msglen; } + strlcpy(name, sdl->sdl_data, + sizeof(name) <= sdl->sdl_nlen ? + sizeof(name) : sdl->sdl_nlen + 1); if (all || namesonly) { if (uponly) @@ -633,6 +639,9 @@ main(int argc, char * const *argv) if (namesonly && need_nl > 0) putchar('\n'); +end: + if (printname) + printf("%s\n", name); if (all == 0 && namesonly == 0 && foundit == 0) errx(1, "interface %s does not exist", name); @@ -1056,6 +1065,31 @@ setifmtu(const char *val, int dummy __un warn("ioctl (set mtu)"); } +void +setifname(const char *val, int dummy __unused, int s, + const struct afswtch *afp) +{ + char *newname; + + newname = strdup(val); + + ifr.ifr_data = newname; + if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { + warn("ioctl (set name)"); + free(newname); + return; + } + strlcpy(name, newname, sizeof(name)); + + free(newname); + + /* + * Even if we just created the interface, we don't need to print + * its name because we just nailed it down separately. + */ + printname = 0; +} + #define IFFBITS \ "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ @@ -1873,7 +1907,8 @@ ifmaybeload(char *name) cp = mstat.name; } /* already loaded? */ - if (!strcmp(ifkind, cp)) + if (strncmp(name, cp, strlen(cp)) == 0 || + strncmp(ifkind, cp, strlen(cp)) == 0) return; } } @@ -1939,8 +1974,13 @@ clone_create(void) if (ioctl(s, SIOCIFCREATE, &ifr) < 0) err(1, "SIOCIFCREATE"); + /* + * If we get a different name back then we put in, we probably + * want to print it out, but we might change our mind later so + * we just signal our intrest and leave the printout for later. + */ if (strcmp(name, ifr.ifr_name) != 0) { - printf("%s\n", ifr.ifr_name); + printname = 1; strlcpy(name, ifr.ifr_name, sizeof(name)); } @@ -1954,4 +1994,9 @@ clone_destroy(const char *val, int d, in (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) err(1, "SIOCIFDESTROY"); + /* + * If we create and destroy an interface in the same command, + * there isn't any reason to print it's name. + */ + printname = 0; } Index: ifconfig/ifconfig.h =================================================================== RCS file: /cvs/src/sbin/ifconfig/ifconfig.h,v retrieving revision 1.2 diff -u -p -u -r1.2 ifconfig.h --- ifconfig/ifconfig.h 17 Jun 2003 04:27:33 -0000 1.2 +++ ifconfig/ifconfig.h 15 Mar 2004 21:30:40 -0000 @@ -37,7 +37,7 @@ extern struct ifreq ifr; -extern char name[32]; /* name of interface */ +extern char name[IFNAMSIZ]; /* name of interface */ extern int allmedia; extern int supmedia; struct afswtch;