File 0005-Add-support-for-interface-rename-in-nameif.patch of Package net-tools

From 2f92b974dd8f6c0885e060b53254d4470d06235f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Chv=C3=A1tal?= <tchvatal@suse.com>
Date: Sat, 17 Jun 2017 23:07:04 +0200
Subject: [PATCH 5/7] Add support for interface rename in nameif

---
 man/en_US/nameif.8 | 43 ++++++++++++++++++++++++--------------
 nameif.c           | 61 ++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 70 insertions(+), 34 deletions(-)

diff --git a/man/en_US/nameif.8 b/man/en_US/nameif.8
index 195304d..ccea694 100644
--- a/man/en_US/nameif.8
+++ b/man/en_US/nameif.8
@@ -4,26 +4,37 @@ nameif \- name network interfaces based on MAC addresses
 .SH SYNOPSIS
 .B "nameif [\-c configfile] [\-s]"
 .br
-.B "nameif [\-c configfile] [\-s] {interface macaddress}"
+.B "nameif [\-c configfile] [\-s] [interface macaddress]"
+.br
+.B "nameif [-c configfile] [-r] [newifname oldifname]"
 .SH DESCRIPTION
-.B nameif 
-renames network interfaces based on mac addresses. When no arguments are
-given 
+.B nameif
+renames network interfaces based on mac addresses or interface names.
+When no arguments are given
 .I /etc/mactab
-is read. Each line  of it contains an interface name and a Ethernet MAC 
-address. Comments are allowed starting with #. 
+is read. Each line  of it contains an interface name and a Ethernet MAC
+address. Comments are allowed starting with #.
 Otherwise the interfaces specified on the command line are processed.
 .I nameif
-looks for the interface with the given MAC address and renames it to the
-name given.
-
-When the 
-.I \-s
-argument is given all error messages go to the syslog.
-
-When the 
-.I \-c 
-argument is given with a file name that file is read instead of /etc/mactab.
+looks for the interface with the given MAC address or old interface name
+and renames it to the name given.
+.SH OPTIONS
+.TP
+.B "[-s|--syslog]"
+Log all error messages to syslog.
+.TP
+.B "[-r|--rename]"
+Rename the interface given by
+.I oldifname
+to the new name
+.I newifname
+without consulting any macaddress.
+.TP
+.B "[-c|--config-file configfile]"
+Read
+.I configfile
+instead of
+.I /etc/mactab.
 
 .SH NOTES
 .I nameif
diff --git a/nameif.c b/nameif.c
index b280e59..bdc1dfa 100644
--- a/nameif.c
+++ b/nameif.c
@@ -28,6 +28,7 @@
 const char default_conf[] = "/etc/mactab";
 const char *fname = default_conf;
 int use_syslog;
+int do_rename;
 int ctl_sk = -1;
 
 void err(char *msg)
@@ -114,7 +115,8 @@ int getmac(char *name, unsigned char *mac)
 struct change {
 	struct change *next;
 	int found;
-	char ifname[IFNAMSIZ+1];
+	unsigned char ifname_old[IFNAMSIZ+1];
+	unsigned char ifname_new[IFNAMSIZ+1];
 	unsigned char mac[6];
 };
 struct change *clist;
@@ -128,13 +130,28 @@ struct change *lookupmac(unsigned char *mac)
 	return NULL;
 }
 
+struct change *lookupifname(unsigned char *ifname_old)
+{
+	struct change *ch;
+	for (ch = clist;ch;ch = ch->next)
+		if (!strcmp(ch->ifname_old, ifname_old))
+			return ch;
+	return NULL;
+}
+
 int addchange(char *p, struct change *ch, char *pos)
 {
-	if (strchr(ch->ifname, ':'))
-		warning(_("alias device %s at %s probably has no mac"),
-			ch->ifname, pos);
-	if (parsemac(p,ch->mac) < 0)
-		complain(_("cannot parse MAC `%s' at %s"), p, pos);
+	if (do_rename) {
+		if (strlen(p)+1>IFNAMSIZ)
+			complain(_("interface name `%s' too long"), p);
+		strcpy(ch->ifname_old, p);
+	} else {
+		if (strchr(ch->ifname_new, ':'))
+			warning(_("alias device %s at %s probably has no mac"),
+				ch->ifname_new, pos);
+		if (parsemac(p,ch->mac) < 0)
+			complain(_("cannot parse MAC `%s' at %s"), p, pos);
+	}
 	ch->next = clist;
 	clist = ch;
 	return 0;
@@ -173,8 +190,8 @@ void readconf(void)
 		if (n > IFNAMSIZ-1)
 			complain(_("interface name too long at line %d"), line);
 		ch = xmalloc(sizeof(struct change));
-		memcpy(ch->ifname, p, n);
-		ch->ifname[n] = 0;
+		memcpy(ch->ifname_new, p, n);
+		ch->ifname_new[n] = 0;
 		p += n;
 		p += strspn(p, " \t");
 		n = strspn(p, "0123456789ABCDEFabcdef:");
@@ -187,6 +204,7 @@ void readconf(void)
 
 struct option lopt[] = {
 	{"syslog", 0, NULL, 's' },
+	{"rename", 0, NULL, 'r' },
 	{"config-file", 1, NULL, 'c' },
 	{"help", 0, NULL, '?' },
 	{NULL},
@@ -194,7 +212,7 @@ struct option lopt[] = {
 
 void usage(void)
 {
-	fprintf(stderr, _("usage: nameif [-c configurationfile] [-s] {ifname macaddress}\n"));
+	fprintf(stderr, _("usage: nameif [-c configurationfile] [-s] [-r] {ifname macaddress|oldifname}\n"));
 	exit(E_USAGE);
 }
 
@@ -209,7 +227,7 @@ int main(int ac, char **av)
 	int ret = 0;
 
 	for (;;) {
-		int c = getopt_long(ac,av,"c:s",lopt,NULL);
+		int c = getopt_long(ac,av,"c:sr",lopt,NULL);
 		if (c == -1) break;
 		switch (c) {
 		default:
@@ -221,6 +239,9 @@ int main(int ac, char **av)
 		case 's':
 			use_syslog = 1;
 			break;
+		case 'r':
+			do_rename = 1;
+			break;
 		}
 	}
 
@@ -235,7 +256,7 @@ int main(int ac, char **av)
 			usage();
 		if (strlen(av[optind])+1>IFNAMSIZ)
 			complain(_("interface name `%s' too long"), av[optind]);
-		safe_strncpy(ch->ifname, av[optind], sizeof(ch->ifname));
+		safe_strncpy(ch->ifname_new, av[optind], sizeof(ch->ifname_new));
 		optind++;
 		sprintf(pos,_("argument %d"),optind);
 		addchange(av[optind], ch, pos);
@@ -266,18 +287,22 @@ int main(int ac, char **av)
 		if (n > IFNAMSIZ-1)
 			complain(_("interface name `%s' too long"), p);
 
-		if (getmac(p, mac) < 0)
-			continue;
+		if (do_rename) {
+			ch = lookupifname(p);
+		} else {
+			if (getmac(p, mac) < 0)
+				continue;
 
-		ch = lookupmac(mac);
+			ch = lookupmac(mac);
+		}
 		if (!ch)
 			continue;
 
 		ch->found = 1;
-		if (strcmp(p, ch->ifname)) {
-			if (setname(p, ch->ifname) < 0)
+		if (strcmp(p, ch->ifname_new)) {
+			if (setname(p, ch->ifname_new) < 0)
 				complain(_("cannot change name of %s to %s: %s"),
-						p, ch->ifname, strerror(errno));
+					 p, ch->ifname_new, strerror(errno));
 		}
 	}
 	fclose(ifh);
@@ -286,7 +311,7 @@ int main(int ac, char **av)
 		struct change *ch = clist;
 		clist = clist->next;
 		if (!ch->found){
-			warning(_("interface '%s' not found"), ch->ifname);
+			complain(_("interface '%s' not found"), ch->ifname_new);
 			ret = 1;
 		}
 		free(ch);
-- 
2.13.1
openSUSE Build Service is sponsored by