[BusyBox] nameif applet for 0.61.pre

Nick Fedchik fnm at fusion.ukrsat.com
Tue Dec 10 07:01:04 UTC 2002


Hi All!

I made new applet - nameif, it used to rename default network interface
names to user-defined names.
The applet patch is in the attach.
Tested and applied to 0.61.pre from cvs.
I tested the applet on my 6-NICs box using new interface names, defined
in the /etc/mactab file.


-- 
Nick Fedchik <fnm at fusion.ukrsat.com>
FNM3-RIPE (-UANIC), UkrSat ISP
-------------- next part --------------
diff -urN busybox/include/applets.h busybox.nameif/include/applets.h
--- busybox/include/applets.h	2002-12-02 01:04:04.000000000 +0200
+++ busybox.nameif/include/applets.h	2002-12-09 12:33:02.000000000 +0200
@@ -368,6 +368,9 @@
 #ifdef CONFIG_MV
 	APPLET(mv, mv_main, _BB_DIR_BIN, _BB_SUID_NEVER)
 #endif
+#ifdef CONFIG_NAMEIF
+	APPLET(nameif, nameif_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
+#endif
 #ifdef CONFIG_NC
 	APPLET(nc, nc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
 #endif
diff -urN busybox/include/usage.h busybox.nameif/include/usage.h
--- busybox/include/usage.h	2002-12-05 22:56:31.000000000 +0200
+++ busybox.nameif/include/usage.h	2002-12-10 15:24:29.000000000 +0200
@@ -1449,6 +1449,19 @@
 #define mv_example_usage \
 	"$ mv /tmp/foo /bin/bar\n" 
 
+#define nameif_trivial_usage \
+	"[OPTIONS] [{IFNAME MACADDR}]"
+#define nameif_full_usage \
+		"Nameif renaming network interface while it in the down state.\n\n" \
+		"Options:\n" \
+		"\t-c FILE\t\tUse another configuration file (default is /etc/mactab)\n" \
+		"\t-s\t\tUse syslog (LOCAL0 facility).\n" \
+		"\tIFNAME MACADDR\tnew_interface_name interface_macaddress\n" 
+#define nameif_example_usage \
+		"$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n" \
+		" or\n" \
+		"$ nameif -c /etc/my_mactab_file\n" \
+
 #define nc_trivial_usage \
 	"[OPTIONS] [IP] [port]" 
 #define nc_full_usage \
diff -urN busybox/networking/Config.in busybox.nameif/networking/Config.in
--- busybox/networking/Config.in	2002-12-07 01:11:30.000000000 +0200
+++ busybox.nameif/networking/Config.in	2002-12-10 15:02:52.000000000 +0200
@@ -17,6 +17,18 @@
 	help
 	  Please submit a patch to add help text for this item.
 
+config CONFIG_NAMEIF
+	bool "nameif"
+	default n
+	help
+	  Naming Interfaces based on MAC address. 
+	  Renamed interfaces MUST be in the down state.
+	  It is possible to use file (default: /etc/mactab)
+	  with list of new interface names and MACs.
+	  File format:
+	  new_if_name	XX:XX:XX:XX:XX:XX
+	  Maximum interface name length: IF_NAMESIZE = 16
+
 config CONFIG_IFCONFIG
 	bool "ifconfig"
 	default n
diff -urN busybox/networking/Makefile.in busybox.nameif/networking/Makefile.in
--- busybox/networking/Makefile.in	2002-12-02 01:04:05.000000000 +0200
+++ busybox.nameif/networking/Makefile.in	2002-12-09 11:32:42.000000000 +0200
@@ -32,6 +32,7 @@
 NETWORKING-$(CONFIG_IPLINK)		+= iplink.o
 NETWORKING-$(CONFIG_IPROUTE)	+= iproute.o
 NETWORKING-$(CONFIG_IPTUNNEL)	+= iptunnel.o
+NETWORKING-$(CONFIG_NAMEIF)	+= nameif.o
 NETWORKING-$(CONFIG_NC)		    += nc.o
 NETWORKING-$(CONFIG_NETSTAT)	+= netstat.o
 NETWORKING-$(CONFIG_NSLOOKUP)	+= nslookup.o
diff -urN busybox/networking/nameif.c busybox.nameif/networking/nameif.c
--- busybox/networking/nameif.c	1970-01-01 03:00:00.000000000 +0300
+++ busybox.nameif/networking/nameif.c	2002-12-10 15:06:14.000000000 +0200
@@ -0,0 +1,251 @@
+/* 
+ * nameif.c - Naming Interfaces based on MAC address for busybox.
+ *
+ * Writen 2000 by Andi Kleen.
+ * Busybox port 2002 by Nick Fedchik <nick at fedchik.org.ua>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307 USA
+ *
+ */
+
+#include <stdio.h>
+#include <getopt.h>
+#include <sys/syslog.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <net/if.h>
+#include <netinet/ether.h>
+#include <linux/sockios.h>
+#include <errno.h>
+#include "busybox.h"
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+struct mactable {
+	struct mactable *next, **pprev;
+	char ifname[IFNAMSIZ + 1];
+	struct ether_addr mac;
+};
+
+struct mactable *clist;
+struct mactable *ch = NULL;
+struct ether_addr *mac = NULL;
+
+const char default_conf[] = "/etc/mactab";
+const char *fname = default_conf;
+
+int use_syslog = 0;
+int ctl_sk = -1;
+
+struct option opts[] = {
+	{"syslog", 0, NULL, 's'},
+	{"configfile", 1, NULL, 'c'},
+	{NULL},
+};
+
+#define EXIT_ON_ERROR(...) { (use_syslog) ?  syslog(LOG_ERR, __VA_ARGS__) : fprintf(stderr, __VA_ARGS__ ); exit(1); }
+
+#ifndef ifr_newname
+#define ifr_newname ifr_ifru.ifru_slave
+#endif
+int rename_interface(char *oldname, char *newname)
+{
+	struct ifreq ifr;
+
+	if (ctl_sk < 0)
+		ctl_sk = socket(PF_INET, SOCK_DGRAM, 0);
+	memset(&ifr, 0, sizeof(struct ifreq));
+	strcpy(ifr.ifr_name, oldname);
+	strcpy(ifr.ifr_newname, newname);
+	return ioctl(ctl_sk, SIOCSIFNAME, &ifr);
+}
+
+int getmacbyifname(char *ifname, struct ether_addr *macaddr)
+{
+	int rval;
+	struct ifreq ifr;
+
+	if (ctl_sk < 0)
+		ctl_sk = socket(PF_INET, SOCK_DGRAM, 0);
+	memset(&ifr, 0, sizeof(struct ifreq));
+	strcpy(ifr.ifr_name, ifname);
+	rval = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
+	memcpy(macaddr, ifr.ifr_hwaddr.sa_data, 6);
+	return rval;
+}
+
+void readconf(void)
+{
+	char *line = NULL;
+	size_t linelen = 0;
+	int linenum = 1;
+	FILE *ifh;
+	char *p;
+	int n;
+
+	ifh = fopen(fname, "r");
+	if (!ifh)
+		EXIT_ON_ERROR("opening configuration file %s: %s", fname,
+					  strerror(errno));
+
+	while (getdelim(&line, &linelen, '\n', ifh) > 0) {
+		ch = xmalloc(sizeof(struct mactable));
+
+		if ((p = strchr(line, '#')) != NULL)
+			*p = '\0';
+		p = line;
+		while (isspace(*p))
+			++p;
+		if (*p == '\0')
+			continue;
+
+		n = strcspn(p, " \t");
+		if (n > IFNAMSIZ)
+			EXIT_ON_ERROR("interface name too long at line %d", linenum);
+		memcpy(ch->ifname, p, n);
+		ch->ifname[n] = 0;
+		if (strchr(ch->ifname, ':'))
+			EXIT_ON_ERROR
+				("alias device %s at %d probably has no mac",
+				 ch->ifname, linenum);
+		p += n;
+		p += strspn(p, " \t");
+		n = strspn(p, "0123456789ABCDEFabcdef:");
+		p[n] = 0;
+
+		mac = ether_aton(p);
+		if (mac == NULL)
+			EXIT_ON_ERROR("invalid mac addres %s\n", p);
+		memcpy(&ch->mac, mac, 6);
+
+		if (clist)
+			clist->pprev = &ch->next;
+		ch->next = clist;
+		ch->pprev = &clist;
+		clist = ch;
+		linenum++;
+	}
+	fclose(ifh);
+}
+
+
+int nameif_main(int argc, char **argv)
+{
+	FILE *ifh;
+	char *p;
+	int n;
+	int linenum;
+	char *line = NULL;
+	size_t linelen = 0;
+	int c;
+
+// Parse args
+	while ((c = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) {
+		switch (c) {
+		case 'c':
+			fname = optarg;
+			break;
+		case 's':
+			use_syslog = 1;
+			break;
+		default:
+			show_usage();
+			break;
+		}
+	}
+
+// Open log socket
+	if (use_syslog)
+		openlog("nameif", 0, LOG_LOCAL0);
+
+// rest of params: ifname macaddr
+	while (optind < argc) {
+		ch = xmalloc(sizeof(struct mactable));
+		if (ch == NULL)
+			EXIT_ON_ERROR("Unable to malloc memory for ch\n");
+		if ((argc - optind) & 1)
+			show_usage();
+		if (strlen(argv[optind]) + 1 > IFNAMSIZ)
+			EXIT_ON_ERROR("interface name `%s' too long", argv[optind]);
+
+		if (strchr(argv[optind], ':'))
+			EXIT_ON_ERROR("alias device %s probably has no mac",
+						  argv[optind]);
+		strcpy(ch->ifname, argv[optind]);
+		optind++;
+		mac = ether_aton(argv[optind]);
+		if (mac == NULL)
+			EXIT_ON_ERROR("invalid mac addres %s\n", argv[optind]);
+		memcpy(&ch->mac, mac, 6);
+		if (clist)
+			clist->pprev = &ch->next;
+		ch->next = clist;
+		ch->pprev = &clist;
+		clist = ch;
+		optind++;
+	}
+	if (!clist || fname != default_conf)
+		readconf();
+	ifh = fopen("/proc/net/dev", "r");
+	if (!ifh)
+		EXIT_ON_ERROR("open of /proc/net/dev: %s", strerror(errno));
+	linenum = 0;
+	while (getdelim(&line, &linelen, '\n', ifh) > 0) {
+		ch = malloc(sizeof(struct mactable));
+		if (linenum++ < 2)
+			continue;
+		p = line;
+		while (isspace(*p))
+			++p;
+		n = strcspn(p, ": \t");
+		p[n] = 0;
+		if (n > IFNAMSIZ - 1)
+			EXIT_ON_ERROR("interface name `%s' too long", p);
+		if (getmacbyifname(p, mac) < 0)
+			continue;
+		for (ch = clist; ch; ch = ch->next)
+			if (!memcmp(&ch->mac, mac, 6))
+				break;
+		if (!ch)
+			continue;
+		*ch->pprev = ch->next;
+		if (strcmp(p, ch->ifname)) {
+			if (rename_interface(p, ch->ifname) < 0)
+				EXIT_ON_ERROR
+					("cannot change interface name from %s to %s: %s", p,
+					 ch->ifname, strerror(errno));
+		}
+		free(ch);
+	}
+	fclose(ifh);
+
+// Cleanup list
+	while (clist) {
+		ch = clist;
+		clist = clist->next;
+		free(ch);
+	}
+	if (use_syslog)
+		closelog();
+	return 0;
+}


More information about the busybox mailing list