blob: 78719edac899ee20e205ad9a72d7040fa67f567b [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath77c60e52003-01-16 11:37:57 +00002/*
Glenn L McGrathf03c9332002-12-13 00:01:44 +00003 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Written 2000 by Andi Kleen.
Glenn L McGrathf03c9332002-12-13 00:01:44 +00006 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
Denis Vlasenko3f5fdc72007-10-14 04:55:59 +00007 * Glenn McGrath
Denis Vlasenkob3f39f02008-04-10 02:03:21 +00008 * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
Glenn L McGrathf03c9332002-12-13 00:01:44 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathf03c9332002-12-13 00:01:44 +000011 */
12
Phil Sutter293a8f22011-03-06 19:42:51 +010013//config:config NAMEIF
14//config: bool "nameif"
15//config: default y
16//config: select PLATFORM_LINUX
17//config: select FEATURE_SYSLOG
18//config: help
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:
26//config: # Comment
27//config: new_interface_name XX:XX:XX:XX:XX:XX
28//config:
29//config:config FEATURE_NAMEIF_EXTENDED
30//config: bool "Extended nameif"
31//config: default y
32//config: depends on NAMEIF
33//config: help
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
42
43//usage:#define nameif_trivial_usage
44//usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
45//usage: "[-s] [-c FILE] [IFNAME HWADDR]..."
46//usage: )
47//usage: IF_FEATURE_NAMEIF_EXTENDED(
48//usage: "[-s] [-c FILE] [IFNAME SELECTOR]..."
49//usage: )
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."
54//usage: )
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"
62//usage: )
63//usage: "\n"
64//usage: "\nOptions:"
65//usage: "\n -c FILE Configuration file (default: /etc/mactab)"
66//usage: "\n -s Log to syslog"
67//usage:
68//usage:#define nameif_example_usage
69//usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
70//usage: " or\n"
71//usage: "$ nameif -c /etc/my_mactab_file\n"
72
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000073#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000074#include <syslog.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000075#include <net/if.h>
76#include <netinet/ether.h>
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000077#include <linux/sockios.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000078
Denis Vlasenko01eaee92008-04-21 02:21:45 +000079#ifndef IFNAMSIZ
80#define IFNAMSIZ 16
Eric Andersen40ea66c2003-07-05 08:00:17 +000081#endif
82
Denis Vlasenko01eaee92008-04-21 02:21:45 +000083/* Taken from linux/sockios.h */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020084#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000085
Eric Andersenaff114c2004-04-14 17:51:38 +000086/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020087#define ETH_ALEN 6
Glenn L McGrathf03c9332002-12-13 00:01:44 +000088
89#ifndef ifr_newname
90#define ifr_newname ifr_ifru.ifru_slave
91#endif
92
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000093typedef struct ethtable_s {
94 struct ethtable_s *next;
95 struct ethtable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000096 char *ifname;
97 struct ether_addr *mac;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000098#if ENABLE_FEATURE_NAMEIF_EXTENDED
99 char *bus_info;
100 char *driver;
Phil Sutter293a8f22011-03-06 19:42:51 +0100101 int32_t phy_address;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000102#endif
103} ethtable_t;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000104
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000105#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... */
109struct ethtool_drvinfo {
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000110 uint32_t cmd;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000111 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. */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000115 /* For PCI devices, use pci_dev->slot_name. */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000116 char reserved1[32];
117 char reserved2[16];
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000118 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) */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000122};
Phil Sutter293a8f22011-03-06 19:42:51 +0100123
124struct ethtool_cmd {
125 __u32 cmd;
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 */
131 __u8 phy_address;
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 */
136 __u16 speed_hi;
137 __u16 reserved2;
138 __u32 reserved[3];
139};
140
141#define ETHTOOL_GSET 0x00000001 /* Get settings. */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000142#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
143#endif
144
145
146static void nameif_parse_selector(ethtable_t *ch, char *selector)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000147{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000148 struct ether_addr *lmac;
149#if ENABLE_FEATURE_NAMEIF_EXTENDED
150 int found_selector = 0;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000151
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000152 while (*selector) {
153 char *next;
154#endif
155 selector = skip_whitespace(selector);
156#if ENABLE_FEATURE_NAMEIF_EXTENDED
Phil Sutter293a8f22011-03-06 19:42:51 +0100157 ch->phy_address = -1;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000158 if (*selector == '\0')
159 break;
160 /* Search for the end .... */
161 next = skip_non_whitespace(selector);
162 if (*next)
163 *next++ = '\0';
164 /* Check for selectors, mac= is assumed */
165 if (strncmp(selector, "bus=", 4) == 0) {
166 ch->bus_info = xstrdup(selector + 4);
167 found_selector++;
168 } else if (strncmp(selector, "driver=", 7) == 0) {
169 ch->driver = xstrdup(selector + 7);
170 found_selector++;
Phil Sutter293a8f22011-03-06 19:42:51 +0100171 } else if (strncmp(selector, "phyaddr=", 8) == 0) {
172 ch->phy_address = xatoi_positive(selector + 8);
173 found_selector++;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000174 } else {
175#endif
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000176 lmac = xmalloc(ETH_ALEN);
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100177 ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000178 if (ch->mac == NULL)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100179 bb_error_msg_and_die("can't parse %s", selector);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000180#if ENABLE_FEATURE_NAMEIF_EXTENDED
181 found_selector++;
182 };
183 selector = next;
184 }
185 if (found_selector == 0)
186 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
187#endif
188}
Glenn L McGrath688cf012002-12-17 12:43:43 +0000189
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000190static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
191{
192 ethtable_t *ch;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000193 if (strlen(ifname) >= IFNAMSIZ)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000194 bb_error_msg_and_die("interface name '%s' too long", ifname);
195 ch = xzalloc(sizeof(*ch));
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000196 ch->ifname = xstrdup(ifname);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000197 nameif_parse_selector(ch, selector);
198 ch->next = *clist;
199 if (*clist)
200 (*clist)->prev = ch;
201 *clist = ch;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000202}
203
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000204#if ENABLE_FEATURE_CLEAN_UP
205static void delete_eth_table(ethtable_t *ch)
206{
207 free(ch->ifname);
208#if ENABLE_FEATURE_NAMEIF_EXTENDED
209 free(ch->bus_info);
210 free(ch->driver);
211#endif
212 free(ch->mac);
213 free(ch);
214};
215#else
216void delete_eth_table(ethtable_t *ch);
217#endif
218
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000219int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Phil Sutter2adc0e62011-03-06 19:15:30 +0100220int nameif_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000221{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000222 ethtable_t *clist = NULL;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000223 const char *fname = "/etc/mactab";
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000224 int ctl_sk;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000225 ethtable_t *ch;
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000226 parser_t *parser;
227 char *token[2];
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000228
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000229 if (1 & getopt32(argv, "sc:", &fname)) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000230 openlog(applet_name, 0, LOG_LOCAL0);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000231 /* 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;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000234 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000235 argv += optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000236
Phil Sutter2adc0e62011-03-06 19:15:30 +0100237 if (argv[0]) {
238 do {
239 if (!argv[1])
240 bb_show_usage();
241 prepend_new_eth_table(&clist, argv[0], argv[1]);
242 argv += 2;
243 } while (*argv);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000244 } else {
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000245 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]);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000248 config_close(parser);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000249 }
250
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000251 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000252 parser = config_open2("/proc/net/dev", xfopen_for_read);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000253
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000254 while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000255 struct ifreq ifr;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000256#if ENABLE_FEATURE_NAMEIF_EXTENDED
257 struct ethtool_drvinfo drvinfo;
Phil Sutter293a8f22011-03-06 19:42:51 +0100258 struct ethtool_cmd eth_settings;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000259#endif
Phil Sutter2adc0e62011-03-06 19:15:30 +0100260 if (parser->lineno <= 2)
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000261 continue; /* Skip the first two lines */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000262
263 /* Find the current interface name and copy it to ifr.ifr_name */
Rob Landley855f1e12006-01-15 02:20:06 +0000264 memset(&ifr, 0, sizeof(struct ifreq));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000265 strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000266
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000267#if ENABLE_FEATURE_NAMEIF_EXTENDED
Phil Sutter293a8f22011-03-06 19:42:51 +0100268 /* Check for phy address */
269 memset(&eth_settings, 0, sizeof(eth_settings));
270 eth_settings.cmd = ETHTOOL_GSET;
271 ifr.ifr_data = (caddr_t) &eth_settings;
272 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
273
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000274 /* Check for driver etc. */
Phil Sutter293a8f22011-03-06 19:42:51 +0100275 memset(&drvinfo, 0, sizeof(drvinfo));
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000276 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);
280#endif
281 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000282
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000283 /* 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)
287 continue;
288 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
289 continue;
Phil Sutter293a8f22011-03-06 19:42:51 +0100290 if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
291 continue;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000292#endif
293 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
294 continue;
295 /* if we came here, all selectors have matched */
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000296 break;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000297 }
298 /* Nothing found for current interface */
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000299 if (!ch)
300 continue;
301
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000302 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,
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100305 "can't change ifname %s to %s",
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000306 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000307 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000308 /* Remove list entry of renamed interface */
309 if (ch->prev != NULL)
310 ch->prev->next = ch->next;
311 else
312 clist = ch->next;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000313 if (ch->next != NULL)
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000314 ch->next->prev = ch->prev;
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000315 if (ENABLE_FEATURE_CLEAN_UP)
316 delete_eth_table(ch);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000317 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000318 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000319 for (ch = clist; ch; ch = ch->next)
320 delete_eth_table(ch);
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000321 config_close(parser);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000322 };
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000323
324 return 0;
325}