2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2003-01-16 11:37:57 +00:00
|
|
|
/*
|
2002-12-13 00:01:44 +00:00
|
|
|
* nameif.c - Naming Interfaces based on MAC address for busybox.
|
|
|
|
*
|
2004-04-14 17:51:38 +00:00
|
|
|
* Written 2000 by Andi Kleen.
|
2002-12-13 00:01:44 +00:00
|
|
|
* Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
|
2007-10-14 04:55:59 +00:00
|
|
|
* Glenn McGrath
|
2002-12-13 00:01:44 +00:00
|
|
|
*
|
2006-01-22 22:55:11 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-12-13 00:01:44 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-08-03 15:41:12 +00:00
|
|
|
#include <syslog.h>
|
2002-12-13 00:01:44 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/ether.h>
|
2007-12-24 14:09:19 +00:00
|
|
|
#include <linux/sockios.h>
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2003-07-05 08:00:17 +00:00
|
|
|
/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
|
|
|
|
#ifndef IF_NAMESIZE
|
|
|
|
# ifdef IFNAMSIZ
|
|
|
|
# define IF_NAMESIZE IFNAMSIZ
|
|
|
|
# else
|
|
|
|
# define IF_NAMESIZE 16
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2003-01-14 08:54:08 +00:00
|
|
|
/* take from linux/sockios.h */
|
2003-01-19 13:34:21 +00:00
|
|
|
#define SIOCSIFNAME 0x8923 /* set interface name */
|
2003-01-14 08:54:08 +00:00
|
|
|
|
2004-04-14 17:51:38 +00:00
|
|
|
/* Octets in one Ethernet addr, from <linux/if_ether.h> */
|
2002-12-13 00:01:44 +00:00
|
|
|
#define ETH_ALEN 6
|
|
|
|
|
|
|
|
#ifndef ifr_newname
|
|
|
|
#define ifr_newname ifr_ifru.ifru_slave
|
|
|
|
#endif
|
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
typedef struct ethtable_s {
|
|
|
|
struct ethtable_s *next;
|
|
|
|
struct ethtable_s *prev;
|
2002-12-13 00:01:44 +00:00
|
|
|
char *ifname;
|
|
|
|
struct ether_addr *mac;
|
2007-12-24 14:09:19 +00:00
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
char *bus_info;
|
|
|
|
char *driver;
|
|
|
|
#endif
|
|
|
|
} ethtable_t;
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
/* Cut'n'paste from ethtool.h */
|
|
|
|
#define ETHTOOL_BUSINFO_LEN 32
|
|
|
|
/* these strings are set to whatever the driver author decides... */
|
|
|
|
struct ethtool_drvinfo {
|
2007-12-28 17:04:42 +00:00
|
|
|
uint32_t cmd;
|
2007-12-24 14:09:19 +00:00
|
|
|
char driver[32]; /* driver short name, "tulip", "eepro100" */
|
|
|
|
char version[32]; /* driver version string */
|
|
|
|
char fw_version[32]; /* firmware version string, if applicable */
|
|
|
|
char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
|
|
|
|
/* For PCI devices, use pci_dev->slot_name. */
|
|
|
|
char reserved1[32];
|
|
|
|
char reserved2[16];
|
2007-12-28 17:04:42 +00:00
|
|
|
uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
|
|
|
|
uint32_t testinfo_len;
|
|
|
|
uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
|
|
|
|
uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
|
2007-12-24 14:09:19 +00:00
|
|
|
};
|
|
|
|
#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
|
|
|
|
#endif
|
|
|
|
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
static void nameif_parse_selector(ethtable_t *ch, char *selector)
|
2002-12-17 12:43:43 +00:00
|
|
|
{
|
2007-12-24 14:09:19 +00:00
|
|
|
struct ether_addr *lmac;
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
int found_selector = 0;
|
2002-12-17 12:43:43 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
while (*selector) {
|
|
|
|
char *next;
|
|
|
|
#endif
|
|
|
|
selector = skip_whitespace(selector);
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
if (*selector == '\0')
|
|
|
|
break;
|
|
|
|
/* Search for the end .... */
|
|
|
|
next = skip_non_whitespace(selector);
|
|
|
|
if (*next)
|
|
|
|
*next++ = '\0';
|
|
|
|
/* Check for selectors, mac= is assumed */
|
|
|
|
if (strncmp(selector, "bus=", 4) == 0) {
|
|
|
|
ch->bus_info = xstrdup(selector + 4);
|
|
|
|
found_selector++;
|
|
|
|
} else if (strncmp(selector, "driver=", 7) == 0) {
|
|
|
|
ch->driver = xstrdup(selector + 7);
|
|
|
|
found_selector++;
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
lmac = ether_aton(selector + (strncmp(selector, "mac=", 4) == 0 ? 4 : 0));
|
|
|
|
/* Check ascii selector, convert and copy to *mac */
|
|
|
|
if (lmac == NULL)
|
|
|
|
bb_error_msg_and_die("cannot parse %s", selector);
|
|
|
|
ch->mac = xmalloc(ETH_ALEN);
|
|
|
|
memcpy(ch->mac, lmac, ETH_ALEN);
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
found_selector++;
|
|
|
|
};
|
|
|
|
selector = next;
|
|
|
|
}
|
|
|
|
if (found_selector == 0)
|
|
|
|
bb_error_msg_and_die("no selectors found for %s", ch->ifname);
|
|
|
|
#endif
|
|
|
|
}
|
2002-12-17 12:43:43 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
|
|
|
|
{
|
|
|
|
ethtable_t *ch;
|
|
|
|
if (strlen(ifname) >= IF_NAMESIZE)
|
|
|
|
bb_error_msg_and_die("interface name '%s' too long", ifname);
|
|
|
|
ch = xzalloc(sizeof(*ch));
|
|
|
|
ch->ifname = ifname;
|
|
|
|
nameif_parse_selector(ch, selector);
|
|
|
|
ch->next = *clist;
|
|
|
|
if (*clist)
|
|
|
|
(*clist)->prev = ch;
|
|
|
|
*clist = ch;
|
2002-12-17 12:43:43 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2002-12-13 00:01:44 +00:00
|
|
|
int nameif_main(int argc, char **argv)
|
|
|
|
{
|
2007-12-24 14:09:19 +00:00
|
|
|
ethtable_t *clist = NULL;
|
2002-12-13 00:01:44 +00:00
|
|
|
FILE *ifh;
|
2003-01-16 11:37:57 +00:00
|
|
|
const char *fname = "/etc/mactab";
|
2002-12-13 00:01:44 +00:00
|
|
|
char *line;
|
2007-12-24 14:09:19 +00:00
|
|
|
char *line_ptr;
|
|
|
|
int linenum;
|
2003-01-16 11:37:57 +00:00
|
|
|
int ctl_sk;
|
2007-12-24 14:09:19 +00:00
|
|
|
ethtable_t *ch;
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-08-18 15:32:12 +00:00
|
|
|
if (1 & getopt32(argv, "sc:", &fname)) {
|
2006-10-03 21:00:43 +00:00
|
|
|
openlog(applet_name, 0, LOG_LOCAL0);
|
2006-09-06 18:36:50 +00:00
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
2007-12-24 14:09:19 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
if (argc & 1)
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_show_usage();
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
if (argc) {
|
|
|
|
while (*argv) {
|
|
|
|
char *ifname = xstrdup(*argv++);
|
|
|
|
prepend_new_eth_table(&clist, ifname, *argv++);
|
2002-12-13 00:01:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-08-03 15:41:12 +00:00
|
|
|
ifh = xfopen(fname, "r");
|
2006-10-12 22:43:20 +00:00
|
|
|
while ((line = xmalloc_fgets(ifh)) != NULL) {
|
2007-12-24 14:09:19 +00:00
|
|
|
char *next;
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
line_ptr = skip_whitespace(line);
|
2006-01-15 09:29:41 +00:00
|
|
|
if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
|
|
|
|
free(line);
|
2002-12-13 00:01:44 +00:00
|
|
|
continue;
|
2006-01-15 09:29:41 +00:00
|
|
|
}
|
2007-12-24 14:09:19 +00:00
|
|
|
next = skip_non_whitespace(line_ptr);
|
|
|
|
if (*next)
|
|
|
|
*next++ = '\0';
|
|
|
|
prepend_new_eth_table(&clist, line_ptr, next);
|
2002-12-13 00:01:44 +00:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
fclose(ifh);
|
|
|
|
}
|
|
|
|
|
2006-09-06 18:36:50 +00:00
|
|
|
ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
|
2007-12-24 14:09:19 +00:00
|
|
|
ifh = xfopen("/proc/net/dev", "r");
|
2002-12-17 12:43:43 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
linenum = 0;
|
2002-12-17 12:43:43 +00:00
|
|
|
while (clist) {
|
2002-12-13 00:01:44 +00:00
|
|
|
struct ifreq ifr;
|
2007-12-24 14:09:19 +00:00
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
struct ethtool_drvinfo drvinfo;
|
|
|
|
#endif
|
2002-12-13 00:01:44 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
line = xmalloc_fgets(ifh);
|
|
|
|
if (line == NULL)
|
|
|
|
break; /* Seems like we're done */
|
|
|
|
if (linenum++ < 2 )
|
|
|
|
goto next_line; /* Skip the first two lines */
|
2002-12-17 12:43:43 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
/* Find the current interface name and copy it to ifr.ifr_name */
|
|
|
|
line_ptr = skip_whitespace(line);
|
|
|
|
*skip_non_whitespace(line_ptr) = '\0';
|
2002-12-17 12:43:43 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
memset(&ifr, 0, sizeof(struct ifreq));
|
|
|
|
strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
/* Check for driver etc. */
|
|
|
|
memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo));
|
|
|
|
drvinfo.cmd = ETHTOOL_GDRVINFO;
|
|
|
|
ifr.ifr_data = (caddr_t) &drvinfo;
|
|
|
|
/* Get driver and businfo first, so we have it in drvinfo */
|
|
|
|
ioctl(ctl_sk, SIOCETHTOOL, &ifr);
|
|
|
|
#endif
|
|
|
|
ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
|
2002-12-13 09:02:16 +00:00
|
|
|
|
2007-12-24 14:09:19 +00:00
|
|
|
/* Search the list for a matching device */
|
|
|
|
for (ch = clist; ch; ch = ch->next) {
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
|
|
|
|
continue;
|
|
|
|
if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
|
|
|
|
continue;
|
|
|
|
/* if we came here, all selectors have matched */
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
/* Nothing found for current interface */
|
|
|
|
goto next_line;
|
|
|
|
found:
|
|
|
|
if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
|
|
|
|
strcpy(ifr.ifr_newname, ch->ifname);
|
|
|
|
ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
|
2007-07-14 22:07:14 +00:00
|
|
|
"cannot change ifname %s to %s",
|
|
|
|
ifr.ifr_name, ch->ifname);
|
2007-12-24 14:09:19 +00:00
|
|
|
}
|
2002-12-17 12:43:43 +00:00
|
|
|
/* Remove list entry of renamed interface */
|
2007-12-24 14:09:19 +00:00
|
|
|
if (ch->prev != NULL)
|
|
|
|
ch->prev->next = ch->next;
|
|
|
|
else
|
2002-12-17 12:43:43 +00:00
|
|
|
clist = ch->next;
|
|
|
|
if (ch->next != NULL)
|
2007-12-24 14:09:19 +00:00
|
|
|
ch->next->prev = ch->prev;
|
2006-05-04 19:52:28 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free(ch->ifname);
|
|
|
|
free(ch->mac);
|
|
|
|
free(ch);
|
|
|
|
}
|
2007-12-24 14:09:19 +00:00
|
|
|
next_line:
|
|
|
|
free(line);
|
2002-12-13 00:01:44 +00:00
|
|
|
}
|
2007-12-24 14:09:19 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
fclose(ifh);
|
|
|
|
};
|
2002-12-13 00:01:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|