bin/151400: [patch] ppp(8) new commands: iface name and iface descr

Aragon Gouveia aragon at phat.za.net
Mon Oct 11 21:10:04 UTC 2010


>Number:         151400
>Category:       bin
>Synopsis:       [patch] ppp(8) new commands: iface name and iface descr
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 11 21:10:04 UTC 2010
>Closed-Date:
>Last-Modified:
>Originator:     Aragon Gouveia
>Release:        8.1-STABLE
>Organization:
>Environment:
FreeBSD soek.geek.sh 8.1-STABLE FreeBSD 8.1-STABLE #0: Sun Oct 10 11:43:28 SAST 2010     toor at igor.geek.sh:/usr/obj/nanobsd.soek/i386/usr/src/sys/SOEK  i386

>Description:
FreeBSD has supported network interface renaming for some time now.  More recently it's been possible to attach textual descriptions to network interfaces too.  Both of these are very useful, but unavailable to PPP interfaces.  I've written a patch for ppp(8) which allows it to rename the tun(4) interfaces that it clones and assign textual descriptions to them too.

The patch adds two new commands:

iface name <name>
iface descr <descr>

The patch also makes a small change to how ppp(8) destroys interfaces at exit. Instead of just dealiasing interfaces and leaving them behind, they are now destroyed in the same manner "ifconfig destroy" works. I believe this is the only correct way of enabling interface renaming without causing ppp to break when it is restarted.

Written for and tested on 8.1-STABLE.  Please note this PR supersedes bin/137379 so please could that old PR be closed.

Please consider committing this patch. :)

>How-To-Repeat:

>Fix:


Patch attached with submission follows:

--- usr.sbin/ppp/bundle.c.orig	2009-08-03 10:13:06.000000000 +0200
+++ usr.sbin/ppp/bundle.c	2010-08-15 04:10:54.000000000 +0200
@@ -758,7 +758,7 @@ bundle_Create(const char *prefix, int ty
     return NULL;
   }
 
-  log_SetTun(bundle.unit);
+  log_SetTun(bundle.unit, NULL);
 
   ifname = strrchr(bundle.dev.Name, '/');
   if (ifname == NULL)
@@ -849,7 +849,7 @@ bundle_Create(const char *prefix, int ty
   bundle.links = datalink_Create("deflink", &bundle, type);
   if (bundle.links == NULL) {
     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
-    iface_Destroy(bundle.iface);
+    iface_Free(bundle.iface);
     bundle.iface = NULL;
     close(bundle.dev.fd);
     return NULL;
--- usr.sbin/ppp/command.c.orig	2009-08-03 10:13:06.000000000 +0200
+++ usr.sbin/ppp/command.c	2010-08-15 05:28:36.000000000 +0200
@@ -184,6 +184,7 @@ static int DeleteCommand(struct cmdargs 
 static int NegotiateCommand(struct cmdargs const *);
 static int ClearCommand(struct cmdargs const *);
 static int RunListCommand(struct cmdargs const *);
+static int IfaceNameCommand(struct cmdargs const *arg);
 static int IfaceAddCommand(struct cmdargs const *);
 static int IfaceDeleteCommand(struct cmdargs const *);
 static int IfaceClearCommand(struct cmdargs const *);
@@ -823,6 +824,10 @@ static struct cmdtab const IfaceCommands
    "Delete iface address", "iface delete addr", (void *)1},
   {NULL, "delete!", IfaceDeleteCommand, LOCAL_AUTH,
    "Delete iface address", "iface delete addr", (void *)1},
+  {"name", NULL, IfaceNameCommand, LOCAL_AUTH,
+    "Set iface name", "iface name name", NULL},
+  {"descr", NULL, iface_Descr, LOCAL_AUTH,
+    "Set iface description", "iface descr description", NULL},
   {"show", NULL, iface_Show, LOCAL_AUTH,
    "Show iface address(es)", "iface show", NULL},
   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
@@ -3176,6 +3181,22 @@ RunListCommand(struct cmdargs const *arg
 }
 
 static int
+IfaceNameCommand(struct cmdargs const *arg)
+{
+  int n = arg->argn;
+
+  if (arg->argc != n + 1) {
+    return -1;
+  }
+
+  if (!iface_Name(arg->bundle->iface, arg->argv[n]))
+    return 1;
+
+  log_SetTun(arg->bundle->unit, arg->bundle->iface->name);
+  return 0;
+}
+
+static int
 IfaceAddCommand(struct cmdargs const *arg)
 {
   struct ncpaddr peer, addr;
--- usr.sbin/ppp/iface.c.orig	2009-08-03 10:13:06.000000000 +0200
+++ usr.sbin/ppp/iface.c	2010-08-15 05:25:27.000000000 +0200
@@ -369,6 +369,100 @@ iface_addr_Add(const char *name, struct 
   return res != -1;
 }
 
+int
+iface_Name(struct iface *iface, const char *name)
+{
+  struct ifreq ifr;
+  int s;
+  char *newname;
+
+  if ((newname = strdup(name)) == NULL) {
+    log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
+    return 0;
+  }
+
+  if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
+    log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
+    free(newname);
+    return 0;
+  }
+
+  strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+  ifr.ifr_data = (caddr_t)newname;
+  if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
+    log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
+               name, newname, strerror(errno));
+    free(newname);
+    return 0;
+  }
+
+  free(iface->name);
+  iface->name = newname;
+  return 1;
+}
+
+int
+iface_Descr (struct cmdargs const *arg)
+{
+  struct ifreq ifr;
+  struct iface *iface;
+  size_t sz, len;
+  int s, n, ifdescr_maxlen;
+  char *descr;
+
+  sz = sizeof(int);
+  if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
+    log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
+    return 1;
+  }
+
+  if (ifdescr_maxlen < 1) {
+    log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
+    return 1;
+  }
+
+  sz = sizeof(char)*ifdescr_maxlen;
+  if ((descr = malloc(sz)) == NULL) {
+    log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
+    return 1;
+  }
+
+  *descr = '\0';
+  n = arg->argn;
+  len = strlcat(descr, arg->argv[n++], sz);
+  while (n < arg->argc) {
+    if ((len = strlcat(descr, " ", sz)) >= sz
+    || (len = strlcat(descr, arg->argv[n], sz)) >= sz)
+      break;
+    ++n;
+  }
+  if (len >= sz) {
+    log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n", (ifdescr_maxlen-1));
+    free(descr);
+    return 1;
+  }
+
+  if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
+    log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
+    free(descr);
+    return 1;
+  }
+
+  iface = arg->bundle->iface;
+  strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+  ifr.ifr_buffer.length = strlen(descr) + 1;
+  ifr.ifr_buffer.buffer = descr;
+  if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
+    log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
+               descr, strerror(errno));
+    free(descr);
+    return 1;
+  }
+
+  if (iface->descr) free(iface->descr);
+  iface->descr = descr;
+  return 0;
+}
 
 void
 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
@@ -608,18 +702,40 @@ iface_ClearFlags(const char *ifname, int
 }
 
 void
+iface_Free(struct iface *iface)
+{
+    free(iface->name);
+    free(iface->descr);
+    free(iface->addr);
+    free(iface);
+}
+
+void
 iface_Destroy(struct iface *iface)
 {
+  struct ifreq ifr;
+  int s;
   /*
-   * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
-   * if that's what the user wants.  It's better to leave the interface
-   * allocated so that existing connections can continue to work.
+   * Old behaviour used to leave interface created, but this
+   * conflicts with interface renaming.  When ppp is started
+   * it tries to open an unopened interface by /dev/tunX
+   * control device and then assumes the interface name
+   * corresponds to the control device name.  If an interface
+   * is renamed the tun(4) control device name won't correspond
+   * to the interface name anymore, so it must be destroyed
+   * when ppp is done with it.
    */
 
   if (iface != NULL) {
-    free(iface->name);
-    free(iface->addr);
-    free(iface);
+    if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
+      log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
+    } else {
+      strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+      if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
+        log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
+               iface->name, strerror(errno));
+    }
+    iface_Free(iface);
   }
 }
 
@@ -661,7 +777,7 @@ iface_Show(struct cmdargs const *arg)
 
   current = iface_Create(iface->name);
   flags = iface->flags = current->flags;
-  iface_Destroy(current);
+  iface_Free(current);
 
   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
--- usr.sbin/ppp/iface.h.orig	2009-08-03 10:13:06.000000000 +0200
+++ usr.sbin/ppp/iface.h	2010-08-15 04:22:20.000000000 +0200
@@ -36,6 +36,7 @@ struct iface_addr {
 
 struct iface {
   char *name;			/* Interface name (malloc'd) */
+  char *descr;			/* Interface description (malloc'd) */
   int index;			/* Interface index */
   int flags;			/* Interface flags (IFF_*) */
   unsigned long mtu;		/* struct tuninfo MTU */
@@ -55,11 +56,14 @@ struct iface {
 
 extern struct iface *iface_Create(const char *name);
 extern void iface_Clear(struct iface *, struct ncp *, int, int);
+extern int iface_Name(struct iface *, const char *);
+extern int iface_Descr(struct cmdargs const *);
 extern int iface_Add(struct iface *, struct ncp *, const struct ncprange *,
                      const struct ncpaddr *, int);
 extern int iface_Delete(struct iface *, struct ncp *, const struct ncpaddr *);
 extern int iface_Show(struct cmdargs const *);
 extern int iface_SetFlags(const char *, int);
 extern int iface_ClearFlags(const char *, int);
+extern void iface_Free(struct iface *);
 extern void iface_Destroy(struct iface *);
 extern void iface_ParseHdr(struct ifa_msghdr *, struct sockaddr *[RTAX_MAX]);
--- usr.sbin/ppp/log.c.orig	2010-08-15 03:56:43.000000000 +0200
+++ usr.sbin/ppp/log.c	2010-08-15 04:23:55.000000000 +0200
@@ -75,6 +75,7 @@ static const char *const LogNames[] = {
 static u_long LogMask = MSK(LogPHASE);
 static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
 static int LogTunno = -1;
+static char *LogIfaceName = NULL;
 static struct prompt *promptlist;	/* Where to log local stuff */
 struct prompt *log_PromptContext;
 int log_PromptListChanged;
@@ -296,9 +297,10 @@ log_Open(const char *Name)
 }
 
 void
-log_SetTun(int tunno)
+log_SetTun(int tunno, char *ifaceName)
 {
   LogTunno = tunno;
+  LogIfaceName = ifaceName;
 }
 
 void
@@ -306,6 +308,7 @@ log_Close()
 {
   closelog();
   LogTunno = -1;
+  LogIfaceName = NULL;
 }
 
 void
@@ -320,8 +323,13 @@ log_Printf(int lev, const char *fmt,...)
     va_start(ap, fmt);
     if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
       if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
-        snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
+        if (LogIfaceName)
+          snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
+	         LogTunno, LogIfaceName, log_Name(lev), fmt);
+        else
+          snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
 	         LogTunno, log_Name(lev), fmt);
+          
       else
         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
 
@@ -338,8 +346,13 @@ log_Printf(int lev, const char *fmt,...)
     if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
         (lev != LogWARN || !log_PromptContext)) {
       if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
-        snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
+        if (LogIfaceName)
+          snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
+	         LogTunno, LogIfaceName, log_Name(lev), fmt);
+        else
+          snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
 	         LogTunno, log_Name(lev), fmt);
+          
       else
         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
       vsyslog(syslogLevel(lev), nfmt, ap);
--- usr.sbin/ppp/log.h.orig	2010-08-15 03:57:48.000000000 +0200
+++ usr.sbin/ppp/log.h	2010-08-15 03:58:44.000000000 +0200
@@ -76,7 +76,7 @@ extern void log_DiscardAllLocal(u_long *
 extern int log_IsKept(int);
 extern int log_IsKeptLocal(int, u_long);
 extern void log_Open(const char *);
-extern void log_SetTun(int);
+extern void log_SetTun(int, char *);
 extern void log_Close(void);
 #ifdef __GNUC__
 extern void log_Printf(int, const char *,...)
--- usr.sbin/ppp/main.c.orig	2009-08-27 09:07:38.000000000 +0200
+++ usr.sbin/ppp/main.c	2010-08-14 22:37:41.000000000 +0200
@@ -386,11 +386,6 @@ main(int argc, char **argv)
 
   /* NOTE:  We may now have changed argv[1] via a ``set proctitle'' */
 
-  if (prompt) {
-    prompt->bundle = bundle;	/* couldn't do it earlier */
-    if (!sw.quiet)
-      prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
-  }
   SignalBundle = bundle;
   bundle->NatEnabled = sw.nat;
   if (sw.nat)
@@ -430,6 +425,12 @@ main(int argc, char **argv)
     AbortProgram(EX_START);
   }
 
+  if (prompt) {
+    prompt->bundle = bundle;	/* couldn't do it earlier */
+    if (!sw.quiet)
+      prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
+  } 
+
   if (sw.mode != PHYS_INTERACTIVE) {
     if (sw.mode != PHYS_DIRECT) {
       if (!sw.fg) {
--- usr.sbin/ppp/ppp.8.m4.orig	2009-08-29 06:15:37.000000000 +0200
+++ usr.sbin/ppp/ppp.8.m4	2010-08-15 03:24:25.000000000 +0200
@@ -3924,6 +3924,13 @@ If the
 .Dq !\&
 is used, no error is given if the address is not currently assigned to
 the interface (and no deletion takes place).
+.It iface name Ar name
+Renames tun interface to
+.Ar name .
+.It iface descr Ar description
+Sets tun interface description to
+.Ar description .
+Useful if you have many interfaces on your system.
 .It iface show
 Shows the current state and current addresses for the interface.
 It is much the same as running


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list