net-tools/nameif-rename.patch

203 lines
4.9 KiB
Diff

--- man/en_US/nameif.8
+++ man/en_US/nameif.8
@@ -4,26 +4,37 @@
.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
+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 #.
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
--- nameif.c
+++ nameif.c
@@ -26,6 +26,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)
@@ -118,7 +119,8 @@
struct change {
struct change *next,**pprev;
- char ifname[IFNAMSIZ+1];
+ char ifname_old[IFNAMSIZ+1];
+ char ifname_new[IFNAMSIZ+1];
unsigned char mac[6];
};
struct change *clist;
@@ -132,13 +134,28 @@
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);
+ }
if (clist)
clist->pprev = &ch->next;
ch->next = clist;
@@ -179,8 +196,8 @@
n = strcspn(p, " \t");
if (n > IFNAMSIZ)
complain(_("interface name too long at line %d"), line);
- 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:");
@@ -193,6 +210,7 @@
struct option lopt[] = {
{"syslog", 0, NULL, 's' },
+ {"rename", 0, NULL, 'r' },
{"config-file", 1, NULL, 'c' },
{"help", 0, NULL, '?' },
{NULL},
@@ -200,7 +218,7 @@
void usage(void)
{
- fprintf(stderr, _("usage: nameif [-c configurationfile] [-s] {ifname macaddress}"));
+ fprintf(stderr, _("usage: nameif [-c configurationfile] [-s] [-r] {ifname macaddress|oldifname}\n"));
exit(1);
}
@@ -214,7 +232,7 @@
size_t linel = 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:
@@ -226,6 +244,9 @@
case 's':
use_syslog = 1;
break;
+ case 'r':
+ do_rename = 1;
+ break;
}
}
@@ -240,7 +261,7 @@
usage();
if (strlen(av[optind])+1>IFNAMSIZ)
complain(_("interface name `%s' too long"), av[optind]);
- strcpy(ch->ifname, av[optind]);
+ strcpy(ch->ifname_new, av[optind]);
optind++;
sprintf(pos,_("argument %d"),optind);
addchange(av[optind], ch, pos);
@@ -271,18 +292,22 @@
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->pprev = ch->next;
- 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));
}
free(ch);
}
@@ -291,7 +316,7 @@
while (clist) {
struct change *ch = clist;
clist = clist->next;
- warning(_("interface '%s' not found"), ch->ifname);
+ complain(_("interface '%s' not found"), ch->ifname_new);
free(ch);
}