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>
19 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
22 # define IF_NAMESIZE IFNAMSIZ
24 # define IF_NAMESIZE 16
28 /* take from linux/sockios.h */
29 #define SIOCSIFNAME 0x8923 /* set interface name */
31 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
35 #define ifr_newname ifr_ifru.ifru_slave
38 typedef struct ethtable_s {
39 struct ethtable_s *next;
40 struct ethtable_s *prev;
42 struct ether_addr *mac;
43 #if ENABLE_FEATURE_NAMEIF_EXTENDED
49 #if ENABLE_FEATURE_NAMEIF_EXTENDED
50 /* Cut'n'paste from ethtool.h */
51 #define ETHTOOL_BUSINFO_LEN 32
52 /* these strings are set to whatever the driver author decides... */
53 struct ethtool_drvinfo {
55 char driver[32]; /* driver short name, "tulip", "eepro100" */
56 char version[32]; /* driver version string */
57 char fw_version[32]; /* firmware version string, if applicable */
58 char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
59 /* For PCI devices, use pci_dev->slot_name. */
62 uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
63 uint32_t testinfo_len;
64 uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
65 uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
67 #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
71 static void nameif_parse_selector(ethtable_t *ch, char *selector)
73 struct ether_addr *lmac;
74 #if ENABLE_FEATURE_NAMEIF_EXTENDED
75 int found_selector = 0;
80 selector = skip_whitespace(selector);
81 #if ENABLE_FEATURE_NAMEIF_EXTENDED
82 if (*selector == '\0')
84 /* Search for the end .... */
85 next = skip_non_whitespace(selector);
88 /* Check for selectors, mac= is assumed */
89 if (strncmp(selector, "bus=", 4) == 0) {
90 ch->bus_info = xstrdup(selector + 4);
92 } else if (strncmp(selector, "driver=", 7) == 0) {
93 ch->driver = xstrdup(selector + 7);
97 lmac = xmalloc(ETH_ALEN);
98 ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) ? 0 : 4), lmac);
100 bb_error_msg_and_die("cannot parse %s", selector);
101 #if ENABLE_FEATURE_NAMEIF_EXTENDED
106 if (found_selector == 0)
107 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
111 static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
114 if (strlen(ifname) >= IF_NAMESIZE)
115 bb_error_msg_and_die("interface name '%s' too long", ifname);
116 ch = xzalloc(sizeof(*ch));
117 ch->ifname = xstrdup(ifname);
118 nameif_parse_selector(ch, selector);
125 #if ENABLE_FEATURE_CLEAN_UP
126 static void delete_eth_table(ethtable_t *ch)
129 #if ENABLE_FEATURE_NAMEIF_EXTENDED
137 void delete_eth_table(ethtable_t *ch);
140 int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
141 int nameif_main(int argc, char **argv)
143 ethtable_t *clist = NULL;
145 const char *fname = "/etc/mactab";
152 if (1 & getopt32(argv, "sc:", &fname)) {
153 openlog(applet_name, 0, LOG_LOCAL0);
154 logmode = LOGMODE_SYSLOG;
164 char *ifname = xstrdup(*argv++);
165 prepend_new_eth_table(&clist, ifname, *argv++);
168 ifh = xfopen(fname, "r");
169 while ((line = xmalloc_fgets(ifh)) != NULL) {
172 line_ptr = skip_whitespace(line);
173 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
175 next = skip_non_whitespace(line_ptr);
178 prepend_new_eth_table(&clist, line_ptr, next);
185 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
186 ifh = xfopen("/proc/net/dev", "r");
191 #if ENABLE_FEATURE_NAMEIF_EXTENDED
192 struct ethtool_drvinfo drvinfo;
195 line = xmalloc_fgets(ifh);
197 break; /* Seems like we're done */
199 goto next_line; /* Skip the first two lines */
201 /* Find the current interface name and copy it to ifr.ifr_name */
202 line_ptr = skip_whitespace(line);
203 *strpbrk(line_ptr, " \t\n:") = '\0';
205 memset(&ifr, 0, sizeof(struct ifreq));
206 strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
208 #if ENABLE_FEATURE_NAMEIF_EXTENDED
209 /* Check for driver etc. */
210 memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo));
211 drvinfo.cmd = ETHTOOL_GDRVINFO;
212 ifr.ifr_data = (caddr_t) &drvinfo;
213 /* Get driver and businfo first, so we have it in drvinfo */
214 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
216 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
218 /* Search the list for a matching device */
219 for (ch = clist; ch; ch = ch->next) {
220 #if ENABLE_FEATURE_NAMEIF_EXTENDED
221 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
223 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
226 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
228 /* if we came here, all selectors have matched */
231 /* Nothing found for current interface */
234 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
235 strcpy(ifr.ifr_newname, ch->ifname);
236 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
237 "cannot change ifname %s to %s",
238 ifr.ifr_name, ch->ifname);
240 /* Remove list entry of renamed interface */
241 if (ch->prev != NULL)
242 ch->prev->next = ch->next;
245 if (ch->next != NULL)
246 ch->next->prev = ch->prev;
247 if (ENABLE_FEATURE_CLEAN_UP)
248 delete_eth_table(ch);
252 if (ENABLE_FEATURE_CLEAN_UP) {
253 for (ch = clist; ch; ch = ch->next)
254 delete_eth_table(ch);