blob: 66e042688ae1537e7aa720f16812ba158d6616c1 [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 */
Phil Sutter293a8f22011-03-06 19:42:51 +010012//config:config NAMEIF
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "nameif (6.6 kb)"
Phil Sutter293a8f22011-03-06 19:42:51 +010014//config: default y
Phil Sutter293a8f22011-03-06 19:42:51 +010015//config: select FEATURE_SYSLOG
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: nameif is used to rename network interface by its MAC address.
18//config: Renamed interfaces MUST be in the down state.
19//config: It is possible to use a file (default: /etc/mactab)
20//config: with list of new interface names and MACs.
21//config: Maximum interface name length: IFNAMSIZ = 16
22//config: File fields are separated by space or tab.
23//config: File format:
24//config: # Comment
25//config: new_interface_name XX:XX:XX:XX:XX:XX
Phil Sutter293a8f22011-03-06 19:42:51 +010026//config:
27//config:config FEATURE_NAMEIF_EXTENDED
28//config: bool "Extended nameif"
29//config: default y
30//config: depends on NAMEIF
31//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020032//config: This extends the nameif syntax to support the bus_info, driver,
33//config: phyaddr selectors. The syntax is compatible to the normal nameif.
34//config: File format:
35//config: new_interface_name driver=asix bus=usb-0000:00:08.2-3
36//config: new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
37//config: new_interface_name phy_address=2 00:80:C8:38:91:B5
38//config: new_interface_name mac=00:80:C8:38:91:B5
39//config: new_interface_name 00:80:C8:38:91:B5
Phil Sutter293a8f22011-03-06 19:42:51 +010040
Denys Vlasenkoa759b222017-08-06 14:15:24 +020041//applet:IF_NAMEIF(APPLET_NOEXEC(nameif, nameif, BB_DIR_SBIN, BB_SUID_DROP, nameif))
Denys Vlasenko47367e12016-11-23 09:05:14 +010042
43//kbuild:lib-$(CONFIG_NAMEIF) += nameif.o
44
Phil Sutter293a8f22011-03-06 19:42:51 +010045//usage:#define nameif_trivial_usage
46//usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
47//usage: "[-s] [-c FILE] [IFNAME HWADDR]..."
48//usage: )
49//usage: IF_FEATURE_NAMEIF_EXTENDED(
50//usage: "[-s] [-c FILE] [IFNAME SELECTOR]..."
51//usage: )
52//usage:#define nameif_full_usage "\n\n"
53//usage: "Rename network interface while it in the down state."
54//usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020055//usage: "\nThe device with address HWADDR is renamed to IFNAME."
Phil Sutter293a8f22011-03-06 19:42:51 +010056//usage: )
57//usage: IF_FEATURE_NAMEIF_EXTENDED(
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020058//usage: "\nThe device matched by SELECTOR is renamed to IFNAME."
Phil Sutter293a8f22011-03-06 19:42:51 +010059//usage: "\nSELECTOR can be a combination of:"
60//usage: "\n driver=STRING"
61//usage: "\n bus=STRING"
62//usage: "\n phy_address=NUM"
63//usage: "\n [mac=]XX:XX:XX:XX:XX:XX"
64//usage: )
65//usage: "\n"
Phil Sutter293a8f22011-03-06 19:42:51 +010066//usage: "\n -c FILE Configuration file (default: /etc/mactab)"
67//usage: "\n -s Log to syslog"
68//usage:
69//usage:#define nameif_example_usage
70//usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
71//usage: " or\n"
72//usage: "$ nameif -c /etc/my_mactab_file\n"
73
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000074#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000075#include <syslog.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000076#include <net/if.h>
77#include <netinet/ether.h>
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000078#include <linux/sockios.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000079
Denis Vlasenko01eaee92008-04-21 02:21:45 +000080#ifndef IFNAMSIZ
81#define IFNAMSIZ 16
Eric Andersen40ea66c2003-07-05 08:00:17 +000082#endif
83
Denis Vlasenko01eaee92008-04-21 02:21:45 +000084/* Taken from linux/sockios.h */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020085#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000086
Eric Andersenaff114c2004-04-14 17:51:38 +000087/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020088#define ETH_ALEN 6
Glenn L McGrathf03c9332002-12-13 00:01:44 +000089
90#ifndef ifr_newname
91#define ifr_newname ifr_ifru.ifru_slave
92#endif
93
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000094typedef struct ethtable_s {
95 struct ethtable_s *next;
96 struct ethtable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000097 char *ifname;
98 struct ether_addr *mac;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000099#if ENABLE_FEATURE_NAMEIF_EXTENDED
100 char *bus_info;
101 char *driver;
Phil Sutter293a8f22011-03-06 19:42:51 +0100102 int32_t phy_address;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000103#endif
104} ethtable_t;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000105
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000106#if ENABLE_FEATURE_NAMEIF_EXTENDED
107/* Cut'n'paste from ethtool.h */
108#define ETHTOOL_BUSINFO_LEN 32
109/* these strings are set to whatever the driver author decides... */
110struct ethtool_drvinfo {
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000111 uint32_t cmd;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000112 char driver[32]; /* driver short name, "tulip", "eepro100" */
113 char version[32]; /* driver version string */
114 char fw_version[32]; /* firmware version string, if applicable */
115 char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000116 /* For PCI devices, use pci_dev->slot_name. */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000117 char reserved1[32];
118 char reserved2[16];
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000119 uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
120 uint32_t testinfo_len;
121 uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
122 uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000123};
Phil Sutter293a8f22011-03-06 19:42:51 +0100124
125struct ethtool_cmd {
Daniel Fandrich6295d272011-06-09 15:44:44 -0700126 uint32_t cmd;
127 uint32_t supported; /* Features this interface supports */
128 uint32_t advertising; /* Features this interface advertises */
129 uint16_t speed; /* The forced speed, 10Mb, 100Mb, gigabit */
130 uint8_t duplex; /* Duplex, half or full */
131 uint8_t port; /* Which connector port */
132 uint8_t phy_address;
133 uint8_t transceiver; /* Which transceiver to use */
134 uint8_t autoneg; /* Enable or disable autonegotiation */
135 uint32_t maxtxpkt; /* Tx pkts before generating tx int */
136 uint32_t maxrxpkt; /* Rx pkts before generating rx int */
137 uint16_t speed_hi;
138 uint16_t reserved2;
139 uint32_t reserved[3];
Phil Sutter293a8f22011-03-06 19:42:51 +0100140};
141
142#define ETHTOOL_GSET 0x00000001 /* Get settings. */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000143#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
144#endif
145
146
147static void nameif_parse_selector(ethtable_t *ch, char *selector)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000148{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000149 struct ether_addr *lmac;
150#if ENABLE_FEATURE_NAMEIF_EXTENDED
151 int found_selector = 0;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000152
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000153 while (*selector) {
154 char *next;
155#endif
156 selector = skip_whitespace(selector);
157#if ENABLE_FEATURE_NAMEIF_EXTENDED
Phil Sutter293a8f22011-03-06 19:42:51 +0100158 ch->phy_address = -1;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000159 if (*selector == '\0')
160 break;
161 /* Search for the end .... */
162 next = skip_non_whitespace(selector);
163 if (*next)
164 *next++ = '\0';
165 /* Check for selectors, mac= is assumed */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100166 if (is_prefixed_with(selector, "bus=")) {
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000167 ch->bus_info = xstrdup(selector + 4);
168 found_selector++;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100169 } else if (is_prefixed_with(selector, "driver=")) {
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000170 ch->driver = xstrdup(selector + 7);
171 found_selector++;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100172 } else if (is_prefixed_with(selector, "phyaddr=")) {
Phil Sutter293a8f22011-03-06 19:42:51 +0100173 ch->phy_address = xatoi_positive(selector + 8);
174 found_selector++;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000175 } else {
176#endif
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000177 lmac = xmalloc(ETH_ALEN);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100178 ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac);
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000179 if (ch->mac == NULL)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100180 bb_error_msg_and_die("can't parse %s", selector);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000181#if ENABLE_FEATURE_NAMEIF_EXTENDED
182 found_selector++;
183 };
184 selector = next;
185 }
186 if (found_selector == 0)
187 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
188#endif
189}
Glenn L McGrath688cf012002-12-17 12:43:43 +0000190
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000191static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
192{
193 ethtable_t *ch;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000194 if (strlen(ifname) >= IFNAMSIZ)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000195 bb_error_msg_and_die("interface name '%s' too long", ifname);
196 ch = xzalloc(sizeof(*ch));
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000197 ch->ifname = xstrdup(ifname);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000198 nameif_parse_selector(ch, selector);
199 ch->next = *clist;
200 if (*clist)
201 (*clist)->prev = ch;
202 *clist = ch;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000203}
204
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000205#if ENABLE_FEATURE_CLEAN_UP
206static void delete_eth_table(ethtable_t *ch)
207{
208 free(ch->ifname);
209#if ENABLE_FEATURE_NAMEIF_EXTENDED
210 free(ch->bus_info);
211 free(ch->driver);
212#endif
213 free(ch->mac);
214 free(ch);
215};
216#else
217void delete_eth_table(ethtable_t *ch);
218#endif
219
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000220int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Phil Sutter2adc0e62011-03-06 19:15:30 +0100221int nameif_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000222{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000223 ethtable_t *clist = NULL;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000224 const char *fname = "/etc/mactab";
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000225 int ctl_sk;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000226 ethtable_t *ch;
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000227 parser_t *parser;
228 char *token[2];
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000229
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000230 if (1 & getopt32(argv, "sc:", &fname)) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000231 openlog(applet_name, 0, LOG_LOCAL0);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000232 /* Why not just "="? I assume logging to stderr
233 * can't hurt. 2>/dev/null if you don't like it: */
234 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000235 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000236 argv += optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000237
Phil Sutter2adc0e62011-03-06 19:15:30 +0100238 if (argv[0]) {
239 do {
240 if (!argv[1])
241 bb_show_usage();
242 prepend_new_eth_table(&clist, argv[0], argv[1]);
243 argv += 2;
244 } while (*argv);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000245 } else {
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000246 parser = config_open(fname);
247 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
248 prepend_new_eth_table(&clist, token[0], token[1]);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000249 config_close(parser);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000250 }
251
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000252 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000253 parser = config_open2("/proc/net/dev", xfopen_for_read);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000254
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000255 while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000256 struct ifreq ifr;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000257#if ENABLE_FEATURE_NAMEIF_EXTENDED
258 struct ethtool_drvinfo drvinfo;
Phil Sutter293a8f22011-03-06 19:42:51 +0100259 struct ethtool_cmd eth_settings;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000260#endif
Phil Sutter2adc0e62011-03-06 19:15:30 +0100261 if (parser->lineno <= 2)
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000262 continue; /* Skip the first two lines */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000263
264 /* Find the current interface name and copy it to ifr.ifr_name */
Rob Landley855f1e12006-01-15 02:20:06 +0000265 memset(&ifr, 0, sizeof(struct ifreq));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000266 strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000267
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000268#if ENABLE_FEATURE_NAMEIF_EXTENDED
Phil Sutter293a8f22011-03-06 19:42:51 +0100269 /* Check for phy address */
270 memset(&eth_settings, 0, sizeof(eth_settings));
271 eth_settings.cmd = ETHTOOL_GSET;
272 ifr.ifr_data = (caddr_t) &eth_settings;
273 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
274
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000275 /* Check for driver etc. */
Phil Sutter293a8f22011-03-06 19:42:51 +0100276 memset(&drvinfo, 0, sizeof(drvinfo));
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000277 drvinfo.cmd = ETHTOOL_GDRVINFO;
278 ifr.ifr_data = (caddr_t) &drvinfo;
279 /* Get driver and businfo first, so we have it in drvinfo */
280 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
281#endif
282 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000283
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000284 /* Search the list for a matching device */
285 for (ch = clist; ch; ch = ch->next) {
286#if ENABLE_FEATURE_NAMEIF_EXTENDED
287 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
288 continue;
289 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
290 continue;
Phil Sutter293a8f22011-03-06 19:42:51 +0100291 if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
292 continue;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000293#endif
294 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
295 continue;
296 /* if we came here, all selectors have matched */
Thomas De Schampheleirebc0ffc02013-02-28 10:31:54 +0100297 goto found;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000298 }
299 /* Nothing found for current interface */
Thomas De Schampheleirebc0ffc02013-02-28 10:31:54 +0100300 continue;
301 found:
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);
Thomas De Schampheleirebc0ffc02013-02-28 10:31:54 +0100317 } /* while */
318
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000319 if (ENABLE_FEATURE_CLEAN_UP) {
Thomas De Schampheleirebc0ffc02013-02-28 10:31:54 +0100320 ethtable_t *next;
321 for (ch = clist; ch; ch = next) {
322 next = ch->next;
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000323 delete_eth_table(ch);
Thomas De Schampheleirebc0ffc02013-02-28 10:31:54 +0100324 }
Denis Vlasenkocfe29362008-08-01 02:32:23 +0000325 config_close(parser);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000326 };
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000327
328 return 0;
329}