1 /* vi: set sw=4 ts=4: */
3 * nameif.c - Naming Interfaces based on MAC address for busybox.
5 * Written 2000 by Andi Kleen.
6 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
8 * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
10 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 //config:config NAMEIF
14 //config: bool "nameif"
16 //config: select PLATFORM_LINUX
17 //config: select FEATURE_SYSLOG
19 //config: nameif is used to rename network interface by its MAC address.
20 //config: Renamed interfaces MUST be in the down state.
21 //config: It is possible to use a file (default: /etc/mactab)
22 //config: with list of new interface names and MACs.
23 //config: Maximum interface name length: IFNAMSIZ = 16
24 //config: File fields are separated by space or tab.
25 //config: File format:
27 //config: new_interface_name XX:XX:XX:XX:XX:XX
29 //config:config FEATURE_NAMEIF_EXTENDED
30 //config: bool "Extended nameif"
32 //config: depends on NAMEIF
34 //config: This extends the nameif syntax to support the bus_info, driver,
35 //config: phyaddr selectors. The syntax is compatible to the normal nameif.
36 //config: File format:
37 //config: new_interface_name driver=asix bus=usb-0000:00:08.2-3
38 //config: new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
39 //config: new_interface_name phy_address=2 00:80:C8:38:91:B5
40 //config: new_interface_name mac=00:80:C8:38:91:B5
41 //config: new_interface_name 00:80:C8:38:91:B5
43 //usage:#define nameif_trivial_usage
44 //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
45 //usage: "[-s] [-c FILE] [IFNAME HWADDR]..."
47 //usage: IF_FEATURE_NAMEIF_EXTENDED(
48 //usage: "[-s] [-c FILE] [IFNAME SELECTOR]..."
50 //usage:#define nameif_full_usage "\n\n"
51 //usage: "Rename network interface while it in the down state."
52 //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
53 //usage: "\nThe device with address HWADDR is renamed to IFACE."
55 //usage: IF_FEATURE_NAMEIF_EXTENDED(
56 //usage: "\nThe device matched by SELECTOR is renamed to IFACE."
57 //usage: "\nSELECTOR can be a combination of:"
58 //usage: "\n driver=STRING"
59 //usage: "\n bus=STRING"
60 //usage: "\n phy_address=NUM"
61 //usage: "\n [mac=]XX:XX:XX:XX:XX:XX"
65 //usage: "\n -c FILE Configuration file (default: /etc/mactab)"
66 //usage: "\n -s Log to syslog"
68 //usage:#define nameif_example_usage
69 //usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
71 //usage: "$ nameif -c /etc/my_mactab_file\n"
76 #include <netinet/ether.h>
77 #include <linux/sockios.h>
83 /* Taken from linux/sockios.h */
84 #define SIOCSIFNAME 0x8923 /* set interface name */
86 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
90 #define ifr_newname ifr_ifru.ifru_slave
93 typedef struct ethtable_s {
94 struct ethtable_s *next;
95 struct ethtable_s *prev;
97 struct ether_addr *mac;
98 #if ENABLE_FEATURE_NAMEIF_EXTENDED
105 #if ENABLE_FEATURE_NAMEIF_EXTENDED
106 /* Cut'n'paste from ethtool.h */
107 #define ETHTOOL_BUSINFO_LEN 32
108 /* these strings are set to whatever the driver author decides... */
109 struct ethtool_drvinfo {
111 char driver[32]; /* driver short name, "tulip", "eepro100" */
112 char version[32]; /* driver version string */
113 char fw_version[32]; /* firmware version string, if applicable */
114 char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
115 /* For PCI devices, use pci_dev->slot_name. */
118 uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
119 uint32_t testinfo_len;
120 uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
121 uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
126 __u32 supported; /* Features this interface supports */
127 __u32 advertising; /* Features this interface advertises */
128 __u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */
129 __u8 duplex; /* Duplex, half or full */
130 __u8 port; /* Which connector port */
132 __u8 transceiver; /* Which transceiver to use */
133 __u8 autoneg; /* Enable or disable autonegotiation */
134 __u32 maxtxpkt; /* Tx pkts before generating tx int */
135 __u32 maxrxpkt; /* Rx pkts before generating rx int */
141 #define ETHTOOL_GSET 0x00000001 /* Get settings. */
142 #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
146 static void nameif_parse_selector(ethtable_t *ch, char *selector)
148 struct ether_addr *lmac;
149 #if ENABLE_FEATURE_NAMEIF_EXTENDED
150 int found_selector = 0;
155 selector = skip_whitespace(selector);
156 #if ENABLE_FEATURE_NAMEIF_EXTENDED
157 ch->phy_address = -1;
158 if (*selector == '\0')
160 /* Search for the end .... */
161 next = skip_non_whitespace(selector);
164 /* Check for selectors, mac= is assumed */
165 if (strncmp(selector, "bus=", 4) == 0) {
166 ch->bus_info = xstrdup(selector + 4);
168 } else if (strncmp(selector, "driver=", 7) == 0) {
169 ch->driver = xstrdup(selector + 7);
171 } else if (strncmp(selector, "phyaddr=", 8) == 0) {
172 ch->phy_address = xatoi_positive(selector + 8);
176 lmac = xmalloc(ETH_ALEN);
177 ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
179 bb_error_msg_and_die("can't parse %s", selector);
180 #if ENABLE_FEATURE_NAMEIF_EXTENDED
185 if (found_selector == 0)
186 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
190 static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
193 if (strlen(ifname) >= IFNAMSIZ)
194 bb_error_msg_and_die("interface name '%s' too long", ifname);
195 ch = xzalloc(sizeof(*ch));
196 ch->ifname = xstrdup(ifname);
197 nameif_parse_selector(ch, selector);
204 #if ENABLE_FEATURE_CLEAN_UP
205 static void delete_eth_table(ethtable_t *ch)
208 #if ENABLE_FEATURE_NAMEIF_EXTENDED
216 void delete_eth_table(ethtable_t *ch);
219 int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
220 int nameif_main(int argc UNUSED_PARAM, char **argv)
222 ethtable_t *clist = NULL;
223 const char *fname = "/etc/mactab";
229 if (1 & getopt32(argv, "sc:", &fname)) {
230 openlog(applet_name, 0, LOG_LOCAL0);
231 /* Why not just "="? I assume logging to stderr
232 * can't hurt. 2>/dev/null if you don't like it: */
233 logmode |= LOGMODE_SYSLOG;
241 prepend_new_eth_table(&clist, argv[0], argv[1]);
245 parser = config_open(fname);
246 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
247 prepend_new_eth_table(&clist, token[0], token[1]);
248 config_close(parser);
251 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
252 parser = config_open2("/proc/net/dev", xfopen_for_read);
254 while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
256 #if ENABLE_FEATURE_NAMEIF_EXTENDED
257 struct ethtool_drvinfo drvinfo;
258 struct ethtool_cmd eth_settings;
260 if (parser->lineno <= 2)
261 continue; /* Skip the first two lines */
263 /* Find the current interface name and copy it to ifr.ifr_name */
264 memset(&ifr, 0, sizeof(struct ifreq));
265 strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
267 #if ENABLE_FEATURE_NAMEIF_EXTENDED
268 /* Check for phy address */
269 memset(ð_settings, 0, sizeof(eth_settings));
270 eth_settings.cmd = ETHTOOL_GSET;
271 ifr.ifr_data = (caddr_t) ð_settings;
272 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
274 /* Check for driver etc. */
275 memset(&drvinfo, 0, sizeof(drvinfo));
276 drvinfo.cmd = ETHTOOL_GDRVINFO;
277 ifr.ifr_data = (caddr_t) &drvinfo;
278 /* Get driver and businfo first, so we have it in drvinfo */
279 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
281 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
283 /* Search the list for a matching device */
284 for (ch = clist; ch; ch = ch->next) {
285 #if ENABLE_FEATURE_NAMEIF_EXTENDED
286 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
288 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
290 if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
293 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
295 /* if we came here, all selectors have matched */
298 /* Nothing found for current interface */
302 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
303 strcpy(ifr.ifr_newname, ch->ifname);
304 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
305 "can't change ifname %s to %s",
306 ifr.ifr_name, ch->ifname);
308 /* Remove list entry of renamed interface */
309 if (ch->prev != NULL)
310 ch->prev->next = ch->next;
313 if (ch->next != NULL)
314 ch->next->prev = ch->prev;
315 if (ENABLE_FEATURE_CLEAN_UP)
316 delete_eth_table(ch);
318 if (ENABLE_FEATURE_CLEAN_UP) {
319 for (ch = clist; ch; ch = ch->next)
320 delete_eth_table(ch);
321 config_close(parser);