2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2003-01-16 17:07:57 +05:30
|
|
|
/*
|
2002-12-13 05:31:44 +05:30
|
|
|
* nameif.c - Naming Interfaces based on MAC address for busybox.
|
|
|
|
*
|
2004-04-14 23:21:38 +05:30
|
|
|
* Written 2000 by Andi Kleen.
|
2002-12-13 05:31:44 +05:30
|
|
|
* Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
|
2007-10-14 10:25:59 +05:30
|
|
|
* Glenn McGrath
|
2008-04-10 07:33:21 +05:30
|
|
|
* Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
|
2002-12-13 05:31:44 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-12-13 05:31:44 +05:30
|
|
|
*/
|
|
|
|
|
2011-03-07 00:12:51 +05:30
|
|
|
//config:config NAMEIF
|
|
|
|
//config: bool "nameif"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: select FEATURE_SYSLOG
|
|
|
|
//config: help
|
|
|
|
//config: nameif is used to rename network interface by its MAC address.
|
|
|
|
//config: Renamed interfaces MUST be in the down state.
|
|
|
|
//config: It is possible to use a file (default: /etc/mactab)
|
|
|
|
//config: with list of new interface names and MACs.
|
|
|
|
//config: Maximum interface name length: IFNAMSIZ = 16
|
|
|
|
//config: File fields are separated by space or tab.
|
|
|
|
//config: File format:
|
|
|
|
//config: # Comment
|
|
|
|
//config: new_interface_name XX:XX:XX:XX:XX:XX
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_NAMEIF_EXTENDED
|
|
|
|
//config: bool "Extended nameif"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on NAMEIF
|
|
|
|
//config: help
|
|
|
|
//config: This extends the nameif syntax to support the bus_info, driver,
|
|
|
|
//config: phyaddr selectors. The syntax is compatible to the normal nameif.
|
|
|
|
//config: File format:
|
|
|
|
//config: new_interface_name driver=asix bus=usb-0000:00:08.2-3
|
|
|
|
//config: new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
|
|
|
|
//config: new_interface_name phy_address=2 00:80:C8:38:91:B5
|
|
|
|
//config: new_interface_name mac=00:80:C8:38:91:B5
|
|
|
|
//config: new_interface_name 00:80:C8:38:91:B5
|
|
|
|
|
|
|
|
//usage:#define nameif_trivial_usage
|
|
|
|
//usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
|
|
|
|
//usage: "[-s] [-c FILE] [IFNAME HWADDR]..."
|
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_NAMEIF_EXTENDED(
|
|
|
|
//usage: "[-s] [-c FILE] [IFNAME SELECTOR]..."
|
|
|
|
//usage: )
|
|
|
|
//usage:#define nameif_full_usage "\n\n"
|
|
|
|
//usage: "Rename network interface while it in the down state."
|
|
|
|
//usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
|
|
|
|
//usage: "\nThe device with address HWADDR is renamed to IFACE."
|
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_NAMEIF_EXTENDED(
|
|
|
|
//usage: "\nThe device matched by SELECTOR is renamed to IFACE."
|
|
|
|
//usage: "\nSELECTOR can be a combination of:"
|
|
|
|
//usage: "\n driver=STRING"
|
|
|
|
//usage: "\n bus=STRING"
|
|
|
|
//usage: "\n phy_address=NUM"
|
|
|
|
//usage: "\n [mac=]XX:XX:XX:XX:XX:XX"
|
|
|
|
//usage: )
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n -c FILE Configuration file (default: /etc/mactab)"
|
|
|
|
//usage: "\n -s Log to syslog"
|
|
|
|
//usage:
|
|
|
|
//usage:#define nameif_example_usage
|
|
|
|
//usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
|
|
|
|
//usage: " or\n"
|
|
|
|
//usage: "$ nameif -c /etc/my_mactab_file\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include <syslog.h>
|
2002-12-13 05:31:44 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/ether.h>
|
2007-12-24 19:39:19 +05:30
|
|
|
#include <linux/sockios.h>
|
2002-12-13 05:31:44 +05:30
|
|
|
|
2008-04-21 07:51:45 +05:30
|
|
|
#ifndef IFNAMSIZ
|
|
|
|
#define IFNAMSIZ 16
|
2003-07-05 13:30:17 +05:30
|
|
|
#endif
|
|
|
|
|
2008-04-21 07:51:45 +05:30
|
|
|
/* Taken from linux/sockios.h */
|
2010-10-29 15:16:52 +05:30
|
|
|
#define SIOCSIFNAME 0x8923 /* set interface name */
|
2003-01-14 14:24:08 +05:30
|
|
|
|
2004-04-14 23:21:38 +05:30
|
|
|
/* Octets in one Ethernet addr, from <linux/if_ether.h> */
|
2010-10-29 15:16:52 +05:30
|
|
|
#define ETH_ALEN 6
|
2002-12-13 05:31:44 +05:30
|
|
|
|
|
|
|
#ifndef ifr_newname
|
|
|
|
#define ifr_newname ifr_ifru.ifru_slave
|
|
|
|
#endif
|
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
typedef struct ethtable_s {
|
|
|
|
struct ethtable_s *next;
|
|
|
|
struct ethtable_s *prev;
|
2002-12-13 05:31:44 +05:30
|
|
|
char *ifname;
|
|
|
|
struct ether_addr *mac;
|
2007-12-24 19:39:19 +05:30
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
char *bus_info;
|
|
|
|
char *driver;
|
2011-03-07 00:12:51 +05:30
|
|
|
int32_t phy_address;
|
2007-12-24 19:39:19 +05:30
|
|
|
#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 22:34:42 +05:30
|
|
|
uint32_t cmd;
|
2007-12-24 19:39:19 +05:30
|
|
|
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. */
|
2008-01-07 21:43:14 +05:30
|
|
|
/* For PCI devices, use pci_dev->slot_name. */
|
2007-12-24 19:39:19 +05:30
|
|
|
char reserved1[32];
|
|
|
|
char reserved2[16];
|
2007-12-28 22:34:42 +05:30
|
|
|
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 19:39:19 +05:30
|
|
|
};
|
2011-03-07 00:12:51 +05:30
|
|
|
|
|
|
|
struct ethtool_cmd {
|
|
|
|
__u32 cmd;
|
|
|
|
__u32 supported; /* Features this interface supports */
|
|
|
|
__u32 advertising; /* Features this interface advertises */
|
|
|
|
__u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */
|
|
|
|
__u8 duplex; /* Duplex, half or full */
|
|
|
|
__u8 port; /* Which connector port */
|
|
|
|
__u8 phy_address;
|
|
|
|
__u8 transceiver; /* Which transceiver to use */
|
|
|
|
__u8 autoneg; /* Enable or disable autonegotiation */
|
|
|
|
__u32 maxtxpkt; /* Tx pkts before generating tx int */
|
|
|
|
__u32 maxrxpkt; /* Rx pkts before generating rx int */
|
|
|
|
__u16 speed_hi;
|
|
|
|
__u16 reserved2;
|
|
|
|
__u32 reserved[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ETHTOOL_GSET 0x00000001 /* Get settings. */
|
2007-12-24 19:39:19 +05:30
|
|
|
#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
|
|
|
|
#endif
|
|
|
|
|
2002-12-13 05:31:44 +05:30
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
static void nameif_parse_selector(ethtable_t *ch, char *selector)
|
2002-12-17 18:13:43 +05:30
|
|
|
{
|
2007-12-24 19:39:19 +05:30
|
|
|
struct ether_addr *lmac;
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
int found_selector = 0;
|
2002-12-17 18:13:43 +05:30
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
while (*selector) {
|
|
|
|
char *next;
|
|
|
|
#endif
|
|
|
|
selector = skip_whitespace(selector);
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
2011-03-07 00:12:51 +05:30
|
|
|
ch->phy_address = -1;
|
2007-12-24 19:39:19 +05:30
|
|
|
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++;
|
2011-03-07 00:12:51 +05:30
|
|
|
} else if (strncmp(selector, "phyaddr=", 8) == 0) {
|
|
|
|
ch->phy_address = xatoi_positive(selector + 8);
|
|
|
|
found_selector++;
|
2007-12-24 19:39:19 +05:30
|
|
|
} else {
|
|
|
|
#endif
|
2008-04-10 07:33:21 +05:30
|
|
|
lmac = xmalloc(ETH_ALEN);
|
2010-03-23 20:13:08 +05:30
|
|
|
ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
|
2008-04-10 07:33:21 +05:30
|
|
|
if (ch->mac == NULL)
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_error_msg_and_die("can't parse %s", selector);
|
2007-12-24 19:39:19 +05:30
|
|
|
#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 18:13:43 +05:30
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
|
|
|
|
{
|
|
|
|
ethtable_t *ch;
|
2008-04-21 07:51:45 +05:30
|
|
|
if (strlen(ifname) >= IFNAMSIZ)
|
2007-12-24 19:39:19 +05:30
|
|
|
bb_error_msg_and_die("interface name '%s' too long", ifname);
|
|
|
|
ch = xzalloc(sizeof(*ch));
|
2008-04-10 07:33:21 +05:30
|
|
|
ch->ifname = xstrdup(ifname);
|
2007-12-24 19:39:19 +05:30
|
|
|
nameif_parse_selector(ch, selector);
|
|
|
|
ch->next = *clist;
|
|
|
|
if (*clist)
|
|
|
|
(*clist)->prev = ch;
|
|
|
|
*clist = ch;
|
2002-12-17 18:13:43 +05:30
|
|
|
}
|
|
|
|
|
2008-04-10 07:33:21 +05:30
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
|
|
|
static void delete_eth_table(ethtable_t *ch)
|
|
|
|
{
|
|
|
|
free(ch->ifname);
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
free(ch->bus_info);
|
|
|
|
free(ch->driver);
|
|
|
|
#endif
|
|
|
|
free(ch->mac);
|
|
|
|
free(ch);
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
void delete_eth_table(ethtable_t *ch);
|
|
|
|
#endif
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2011-03-06 23:45:30 +05:30
|
|
|
int nameif_main(int argc UNUSED_PARAM, char **argv)
|
2002-12-13 05:31:44 +05:30
|
|
|
{
|
2007-12-24 19:39:19 +05:30
|
|
|
ethtable_t *clist = NULL;
|
2003-01-16 17:07:57 +05:30
|
|
|
const char *fname = "/etc/mactab";
|
|
|
|
int ctl_sk;
|
2007-12-24 19:39:19 +05:30
|
|
|
ethtable_t *ch;
|
2008-08-01 08:02:23 +05:30
|
|
|
parser_t *parser;
|
|
|
|
char *token[2];
|
2002-12-13 05:31:44 +05:30
|
|
|
|
2007-08-18 21:02:12 +05:30
|
|
|
if (1 & getopt32(argv, "sc:", &fname)) {
|
2006-10-04 02:30:43 +05:30
|
|
|
openlog(applet_name, 0, LOG_LOCAL0);
|
2009-03-11 20:10:00 +05:30
|
|
|
/* Why not just "="? I assume logging to stderr
|
|
|
|
* can't hurt. 2>/dev/null if you don't like it: */
|
|
|
|
logmode |= LOGMODE_SYSLOG;
|
2006-09-07 00:06:50 +05:30
|
|
|
}
|
2007-12-24 19:39:19 +05:30
|
|
|
argv += optind;
|
2002-12-13 05:31:44 +05:30
|
|
|
|
2011-03-06 23:45:30 +05:30
|
|
|
if (argv[0]) {
|
|
|
|
do {
|
|
|
|
if (!argv[1])
|
|
|
|
bb_show_usage();
|
|
|
|
prepend_new_eth_table(&clist, argv[0], argv[1]);
|
|
|
|
argv += 2;
|
|
|
|
} while (*argv);
|
2002-12-13 05:31:44 +05:30
|
|
|
} else {
|
2008-08-01 08:02:23 +05:30
|
|
|
parser = config_open(fname);
|
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
|
|
|
|
prepend_new_eth_table(&clist, token[0], token[1]);
|
2008-07-27 04:38:31 +05:30
|
|
|
config_close(parser);
|
2002-12-13 05:31:44 +05:30
|
|
|
}
|
|
|
|
|
2006-09-07 00:06:50 +05:30
|
|
|
ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
|
2008-08-01 08:02:23 +05:30
|
|
|
parser = config_open2("/proc/net/dev", xfopen_for_read);
|
2002-12-17 18:13:43 +05:30
|
|
|
|
2008-08-01 08:02:23 +05:30
|
|
|
while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
|
2002-12-13 05:31:44 +05:30
|
|
|
struct ifreq ifr;
|
2007-12-24 19:39:19 +05:30
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
|
|
|
struct ethtool_drvinfo drvinfo;
|
2011-03-07 00:12:51 +05:30
|
|
|
struct ethtool_cmd eth_settings;
|
2007-12-24 19:39:19 +05:30
|
|
|
#endif
|
2011-03-06 23:45:30 +05:30
|
|
|
if (parser->lineno <= 2)
|
2008-08-01 08:02:23 +05:30
|
|
|
continue; /* Skip the first two lines */
|
2002-12-17 18:13:43 +05:30
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
/* Find the current interface name and copy it to ifr.ifr_name */
|
|
|
|
memset(&ifr, 0, sizeof(struct ifreq));
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
|
2007-12-24 19:39:19 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
2011-03-07 00:12:51 +05:30
|
|
|
/* Check for phy address */
|
|
|
|
memset(ð_settings, 0, sizeof(eth_settings));
|
|
|
|
eth_settings.cmd = ETHTOOL_GSET;
|
|
|
|
ifr.ifr_data = (caddr_t) ð_settings;
|
|
|
|
ioctl(ctl_sk, SIOCETHTOOL, &ifr);
|
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
/* Check for driver etc. */
|
2011-03-07 00:12:51 +05:30
|
|
|
memset(&drvinfo, 0, sizeof(drvinfo));
|
2007-12-24 19:39:19 +05:30
|
|
|
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 14:32:16 +05:30
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
/* 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;
|
2011-03-07 00:12:51 +05:30
|
|
|
if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
|
|
|
|
continue;
|
2007-12-24 19:39:19 +05:30
|
|
|
#endif
|
|
|
|
if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
|
|
|
|
continue;
|
|
|
|
/* if we came here, all selectors have matched */
|
2008-08-01 08:02:23 +05:30
|
|
|
break;
|
2007-12-24 19:39:19 +05:30
|
|
|
}
|
|
|
|
/* Nothing found for current interface */
|
2008-08-01 08:02:23 +05:30
|
|
|
if (!ch)
|
|
|
|
continue;
|
|
|
|
|
2007-12-24 19:39:19 +05:30
|
|
|
if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
|
|
|
|
strcpy(ifr.ifr_newname, ch->ifname);
|
|
|
|
ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
|
2009-11-13 13:38:27 +05:30
|
|
|
"can't change ifname %s to %s",
|
2007-07-15 03:37:14 +05:30
|
|
|
ifr.ifr_name, ch->ifname);
|
2007-12-24 19:39:19 +05:30
|
|
|
}
|
2002-12-17 18:13:43 +05:30
|
|
|
/* Remove list entry of renamed interface */
|
2007-12-24 19:39:19 +05:30
|
|
|
if (ch->prev != NULL)
|
|
|
|
ch->prev->next = ch->next;
|
|
|
|
else
|
2002-12-17 18:13:43 +05:30
|
|
|
clist = ch->next;
|
|
|
|
if (ch->next != NULL)
|
2008-08-01 08:02:23 +05:30
|
|
|
ch->next->prev = ch->prev;
|
2008-04-10 07:33:21 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
delete_eth_table(ch);
|
2002-12-13 05:31:44 +05:30
|
|
|
}
|
2007-12-24 19:39:19 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
2008-04-10 07:33:21 +05:30
|
|
|
for (ch = clist; ch; ch = ch->next)
|
|
|
|
delete_eth_table(ch);
|
2008-08-01 08:02:23 +05:30
|
|
|
config_close(parser);
|
2007-12-24 19:39:19 +05:30
|
|
|
};
|
2002-12-13 05:31:44 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|