Skip to contentSkip to conversation

rename ppp interface

PATCH Arvin Schnell 4 participants 7 messages Jul 16, 2004 – Aug 1, 2004 PPP protocol drivers and compressors

lore
Share & tools
lore
#0 lore raw

Hi,

as you might know with the linux kernel 2.6 it is possible to rename network interfaces. I have made a small patch to allow the pppd to rename it is ppp interface. The patch is only to demonstrate the issue - it is far from complete (no option, no documentation, pppstats needs adaption). If there is willingness to include this feature in the mainline I can make a complete patch.

Besides that, does anybody see problems with renaming of ppp interfaces?

ciao Arvin

Signature · 4 lines
Dipl.-Phys. Arvin Schnell
SuSE Linux AG
Research & Development
email: [email protected]
pppd/main.c renamed from pppd/main.c.orig +4 -1
@@ -777,8 +777,11 @@
 set_ifunit(iskey)
     int iskey;
 {
-    info("Using interface %s%d", PPP_DRV_NAME, ifunit);
+    if (1)
+	slprintf(ifname, sizeof(ifname), "modem");
+    else
     slprintf(ifname, sizeof(ifname), "%s%d", PPP_DRV_NAME, ifunit);
+    info("Using interface %s", ifname);
     script_setenv("IFNAME", ifname, iskey);
     if (iskey) {
 	create_pidfile(getpid());	/* write pid to file */
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +11 -0
@@ -647,6 +647,17 @@
 	}
 	if (x < 0)
 		error("Couldn't create new ppp unit: %m");
+
+	if (1) {
+		struct ifreq ifr;
+		memset(&ifr, 0, sizeof(struct ifreq));
+		strcpy(ifr.ifr_name, "ppp0");
+		strcpy(ifr.ifr_newname, "modem");
+		x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
+		if (x < 0)
+		    error("Couldn't rename interface %s to %s: %m", "ppp0", "modem");
+	}
+
 	return x;
 }
 
#1 lore raw

I'm certainly interested in the patch; in the PPTP projects we regularly get "change the interface name" as a wishlist request. Can you tell me which kernel version range supports the ioctl?

I'm not qualified to judge any other impacts of the change.

Signature · 3 lines
James Cameron                         http://quozl.netrek.org/
HP Open Source, Volunteer             http://opensource.hp.com/
PPTP Client Project, Release Engineer http://pptpclient.sourceforge.net/
#2 lore raw
In reply to Arvin Schnell
Quoted 3 lines · James Cameron

I'm certainly interested in the patch; in the PPTP projects we regularly get "change the interface name" as a wishlist request. Can you tell me which kernel version range supports the ioctl?

I think the ioctl was added for kernel 2.6, but I might be wrong. Likely the code should be placed in ifdef's.

Quoted 1 line

I'm not qualified to judge any other impacts of the change.

The only problem I found is when you try to rename several interfaces to pppN. Also I didn't test multilink.

Attached is an enhanced patch.

ciao Arvin

Signature · 4 lines
Dipl.-Phys. Arvin Schnell
SuSE Linux AG
Research & Development
email: [email protected]
pppd/main.c renamed from pppd/main.c.orig +5 -9
@@ -98,7 +98,7 @@
 static const char rcsid[] = RCSID;
 
 /* interface vars */
-char ifname[32];		/* Interface name */
+char ifname[MAXIFNAMELEN];	/* Interface name */
 int ifunit;			/* Interface unit number */
 
 struct channel *the_channel;
@@ -261,13 +261,6 @@
     NULL
 };
 
-/*
- * If PPP_DRV_NAME is not defined, use the default "ppp" as the device name.
- */
-#if !defined(PPP_DRV_NAME)
-#define PPP_DRV_NAME	"ppp"
-#endif /* !defined(PPP_DRV_NAME) */
-
 int
 main(argc, argv)
     int argc;
@@ -777,8 +770,11 @@
 set_ifunit(iskey)
     int iskey;
 {
-    info("Using interface %s%d", PPP_DRV_NAME, ifunit);
+    if (req_ifname[0] != '\0')
+	slprintf(ifname, sizeof(ifname), req_ifname);
+    else
     slprintf(ifname, sizeof(ifname), "%s%d", PPP_DRV_NAME, ifunit);
+    info("Using interface %s", ifname);
     script_setenv("IFNAME", ifname, iskey);
     if (iskey) {
 	create_pidfile(getpid());	/* write pid to file */
pppd/options.c renamed from pppd/options.c.orig +5 -0
@@ -102,6 +102,7 @@
 bool	tune_kernel;		/* may alter kernel settings */
 int	connect_delay = 1000;	/* wait this many ms after connect script */
 int	req_unit = -1;		/* requested interface unit */
+char	req_ifname[MAXIFNAMELEN];	/* requested interface name */
 bool	multilink = 0;		/* Enable multilink operation */
 char	*bundle_name = NULL;	/* bundle name for multilink */
 bool	dump_options;		/* print out option values */
@@ -259,6 +260,10 @@
       "PPP interface unit number to use if possible",
       OPT_PRIO | OPT_LLIMIT, 0, 0 },
 
+    { "ifname", o_string, req_ifname,
+      "Set PPP interface name",
+      OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXIFNAMELEN },
+
     { "dump", o_bool, &dump_options,
       "Print out option values after parsing all options", 1 },
     { "dryrun", o_bool, &dryrun,
pppd/pppd.8 renamed from pppd/pppd.8.orig +6 -1
@@ -1020,7 +1020,12 @@
 .TP
 .B unit \fInum
 Sets the ppp unit number (for a ppp0 or ppp1 etc interface name) for outbound
-connections.
+connections.  If the unit is already in use a dynamically allocated will be
+used.
+.TP
+.B ifname \fIstring
+Set the ppp interface name for outbound connections.  A failure to set the
+name will terminate the pppd.
 .TP
 .B updetach
 With this option, pppd will detach from its controlling terminal once
pppd/pppd.h renamed from pppd/pppd.h.orig +11 -0
@@ -80,6 +80,16 @@
 #define MAXARGS		1	/* max # args to a command */
 #define MAXNAMELEN	256	/* max length of hostname or name for auth */
 #define MAXSECRETLEN	256	/* max length of password or secret */
+#define MAXIFNAMELEN	32	/* max length of interface name; or use IFNAMSIZ, can we
+				   always include net/if.h? */
+
+/*
+ * If PPP_DRV_NAME is not defined, use the default "ppp" as the device name.
+ * Where should PPP_DRV_NAME come from? Do we include it here?
+ */
+#if !defined(PPP_DRV_NAME)
+#define PPP_DRV_NAME	"ppp"
+#endif /* !defined(PPP_DRV_NAME) */
 
 /*
  * Option descriptor structure.
@@ -304,6 +314,7 @@
 extern int	connect_delay;	/* Time to delay after connect script */
 extern int	max_data_rate;	/* max bytes/sec through charshunt */
 extern int	req_unit;	/* interface unit number to use */
+extern char	req_ifname[MAXIFNAMELEN];	/* interface name to use */
 extern bool	multilink;	/* enable multilink operation */
 extern bool	noendpoint;	/* don't send or accept endpt. discrim. */
 extern char	*bundle_name;	/* bundle name for multilink */
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +15 -0
@@ -649,6 +649,21 @@
 	}
 	if (x < 0)
 		error("Couldn't create new ppp unit: %m");
+
+	if (x == 0 && req_ifname[0] != '\0') {
+		struct ifreq ifr;
+		char t[MAXIFNAMELEN];
+		memset(&ifr, 0, sizeof(struct ifreq));
+		slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
+		strncpy(ifr.ifr_name, t, IF_NAMESIZE);
+		strncpy(ifr.ifr_newname, req_ifname, IF_NAMESIZE);
+		x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
+		if (x < 0)
+		    error("Couldn't rename interface %s to %s: %m", t, req_ifname);
+		else
+		    info("Renamed interface %s to %s", t, req_ifname);
+	}
+
 	return x;
 }
 
#3 lore raw
In reply to Arvin Schnell
Quoted 3 lines · James Cameron

I'm certainly interested in the patch; in the PPTP projects we regularly get "change the interface name" as a wishlist request. Can you tell me which kernel version range supports the ioctl?

The SIOCSIFNAME ioctl is supported for ages. Linux `nameif' program that uses it is very old too. At least, linux 2.2 does support it. All after all, error() in patch may be changed into warn().

/mjt

#4 lore raw
In reply to Arvin Schnell

Arvin Schnell wrote:
[]

pppd/options.c

Quoted 5 lines
pppd/options.c renamed from pppd/options.c.orig +3 -0
+    { "ifname", o_string, req_ifname,
+      "Set PPP interface name",
+      OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXIFNAMELEN },

Is there any reason to use new option instead of existing "linkname" option? Currently, linkname is used only for informational purposes (to be passed into scripts, and to create /var/run/ppp-$linkname.pid file). Existing option seems to be better, but it may lead to unexpected problems, namely, users who use it expects to see interface named pppN (with current version), but with this change things may change too.

Quoted 7 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +1 -0
@@ -649,6 +649,21 @@
 	}
 	if (x < 0)
 		error("Couldn't create new ppp unit: %m");
+

pppd/sys-linux.c line 662

Quoted 10 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +10 -0
+	if (x = 0 && req_ifname[0] != '\0') {
+		struct ifreq ifr;
+		char t[MAXIFNAMELEN];
+		memset(&ifr, 0, sizeof(struct ifreq));
+		slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
+		strncpy(ifr.ifr_name, t, IF_NAMESIZE);
+		strncpy(ifr.ifr_newname, req_ifname, IF_NAMESIZE);
+		x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
+		if (x < 0)
+		    error("Couldn't rename interface %s to %s: %m", t, req_ifname);

I'd suggest to change this into warn().

pppd/sys-linux.c line 664

Quoted 2 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +2 -0
+		else
+		    info("Renamed interface %s to %s", t, req_ifname);

Is such a verbosity really necessary? Pppd is already too noizy...

/mjt

#5 lore raw
In reply to Arvin Schnell
Quoted 16 lines · Michael Tokarev

Arvin Schnell wrote:
[]

pppd/options.c

Quoted 5 lines
pppd/options.c renamed from pppd/options.c.orig +3 -0
+    { "ifname", o_string, req_ifname,
+      "Set PPP interface name",
+      OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXIFNAMELEN },

Is there any reason to use new option instead of existing "linkname" option? Currently, linkname is used only for informational purposes (to be passed into scripts, and to create /var/run/ppp-$linkname.pid file). Existing option seems to be better, but it may lead to unexpected problems, namely, users who use it expects to see interface named pppN (with current version), but with this change things may change too.

In the end the "linkname" option could be used but in a patch I prefer to not change the existing behaviour.

Quoted 12 lines
Quoted 12 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +6 -0
@@ -649,6 +649,21 @@
 	}
 	if (x < 0)
 		error("Couldn't create new ppp unit: %m");
+
+	if (x = 0 && req_ifname[0] != '\0') {
+		struct ifreq ifr;
+		char t[MAXIFNAMELEN];
+		memset(&ifr, 0, sizeof(struct ifreq));
+		slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
Quoted 8 lines

pppd/sys-linux.c line 663

Quoted 6 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +5 -0
+		strncpy(ifr.ifr_name, t, IF_NAMESIZE);
+		strncpy(ifr.ifr_newname, req_ifname, IF_NAMESIZE);
+		x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
+		if (x < 0)
+		    error("Couldn't rename interface %s to %s: %m", t, 
 req_ifname);

I'd suggest to change this into warn().

The goal is to get persistent interface names. I suppose some people will prefer a failure instead of a random interface name to prevent a interface not covert by the firewall.

Anyway, if it's changed to warn the code in set_ifunit must also be changed.

Quoted 4 lines

pppd/sys-linux.c line 665

Quoted 2 lines
pppd/sys-linux.c renamed from pppd/sys-linux.c.orig +2 -0
+		else
+		    info("Renamed interface %s to %s", t, req_ifname);

Is such a verbosity really necessary? Pppd is already too noizy...

It was useful during development but can of course be removed. There's already the "using interface xxx" message.

ciao Arvin

Signature · 4 lines
Dipl.-Phys. Arvin Schnell
SUSE Linux AG
Research & Development
email: [email protected]