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 the GPL v2 or later, see the file LICENSE in this tarball.
16 #include <netinet/ether.h>
17 #include <linux/sockios.h>
23 /* Taken from linux/sockios.h */
24 #define SIOCSIFNAME 0x8923 /* set interface name */
26 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
30 #define ifr_newname ifr_ifru.ifru_slave
33 typedef struct ethtable_s {
34 struct ethtable_s *next;
35 struct ethtable_s *prev;
37 struct ether_addr *mac;
38 #if ENABLE_FEATURE_NAMEIF_EXTENDED
44 #if ENABLE_FEATURE_NAMEIF_EXTENDED
45 /* Cut'n'paste from ethtool.h */
46 #define ETHTOOL_BUSINFO_LEN 32
47 /* these strings are set to whatever the driver author decides... */
48 struct ethtool_drvinfo {
50 char driver[32]; /* driver short name, "tulip", "eepro100" */
51 char version[32]; /* driver version string */
52 char fw_version[32]; /* firmware version string, if applicable */
53 char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
54 /* For PCI devices, use pci_dev->slot_name. */
57 uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
58 uint32_t testinfo_len;
59 uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
60 uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
62 #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
66 static void nameif_parse_selector(ethtable_t *ch, char *selector)
68 struct ether_addr *lmac;
69 #if ENABLE_FEATURE_NAMEIF_EXTENDED
70 int found_selector = 0;
75 selector = skip_whitespace(selector);
76 #if ENABLE_FEATURE_NAMEIF_EXTENDED
77 if (*selector == '\0')
79 /* Search for the end .... */
80 next = skip_non_whitespace(selector);
83 /* Check for selectors, mac= is assumed */
84 if (strncmp(selector, "bus=", 4) == 0) {
85 ch->bus_info = xstrdup(selector + 4);
87 } else if (strncmp(selector, "driver=", 7) == 0) {
88 ch->driver = xstrdup(selector + 7);
92 lmac = xmalloc(ETH_ALEN);
93 ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) ? 0 : 4), lmac);
95 bb_error_msg_and_die("cannot parse %s", selector);
96 #if ENABLE_FEATURE_NAMEIF_EXTENDED
101 if (found_selector == 0)
102 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
106 static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
109 if (strlen(ifname) >= IFNAMSIZ)
110 bb_error_msg_and_die("interface name '%s' too long", ifname);
111 ch = xzalloc(sizeof(*ch));
112 ch->ifname = xstrdup(ifname);
113 nameif_parse_selector(ch, selector);
120 #if ENABLE_FEATURE_CLEAN_UP
121 static void delete_eth_table(ethtable_t *ch)
124 #if ENABLE_FEATURE_NAMEIF_EXTENDED
132 void delete_eth_table(ethtable_t *ch);
135 int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
136 int nameif_main(int argc, char **argv)
138 ethtable_t *clist = NULL;
140 const char *fname = "/etc/mactab";
147 if (1 & getopt32(argv, "sc:", &fname)) {
148 openlog(applet_name, 0, LOG_LOCAL0);
149 logmode = LOGMODE_SYSLOG;
159 char *ifname = xstrdup(*argv++);
160 prepend_new_eth_table(&clist, ifname, *argv++);
163 struct parser_t *parser = config_open(fname);
166 while (config_read(parser, tokens, 2, 2, "# \t", 0))
167 prepend_new_eth_table(&clist, tokens[0], tokens[1]);
168 config_close(parser);
172 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
173 ifh = xfopen("/proc/net/dev", "r");
178 #if ENABLE_FEATURE_NAMEIF_EXTENDED
179 struct ethtool_drvinfo drvinfo;
182 line = xmalloc_fgets(ifh);
184 break; /* Seems like we're done */
186 goto next_line; /* Skip the first two lines */
188 /* Find the current interface name and copy it to ifr.ifr_name */
189 line_ptr = skip_whitespace(line);
190 *strpbrk(line_ptr, " \t\n:") = '\0';
192 memset(&ifr, 0, sizeof(struct ifreq));
193 strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
195 #if ENABLE_FEATURE_NAMEIF_EXTENDED
196 /* Check for driver etc. */
197 memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo));
198 drvinfo.cmd = ETHTOOL_GDRVINFO;
199 ifr.ifr_data = (caddr_t) &drvinfo;
200 /* Get driver and businfo first, so we have it in drvinfo */
201 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
203 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
205 /* Search the list for a matching device */
206 for (ch = clist; ch; ch = ch->next) {
207 #if ENABLE_FEATURE_NAMEIF_EXTENDED
208 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
210 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
213 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
215 /* if we came here, all selectors have matched */
218 /* Nothing found for current interface */
221 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
222 strcpy(ifr.ifr_newname, ch->ifname);
223 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
224 "cannot change ifname %s to %s",
225 ifr.ifr_name, ch->ifname);
227 /* Remove list entry of renamed interface */
228 if (ch->prev != NULL)
229 ch->prev->next = ch->next;
232 if (ch->next != NULL)
233 ch->next->prev = ch->prev;
234 if (ENABLE_FEATURE_CLEAN_UP)
235 delete_eth_table(ch);
239 if (ENABLE_FEATURE_CLEAN_UP) {
240 for (ch = clist; ch; ch = ch->next)
241 delete_eth_table(ch);