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>
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
15 #include <netinet/ether.h>
16 #include <linux/sockios.h>
18 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
21 # define IF_NAMESIZE IFNAMSIZ
23 # define IF_NAMESIZE 16
27 /* take from linux/sockios.h */
28 #define SIOCSIFNAME 0x8923 /* set interface name */
30 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
34 #define ifr_newname ifr_ifru.ifru_slave
37 typedef struct ethtable_s {
38 struct ethtable_s *next;
39 struct ethtable_s *prev;
41 struct ether_addr *mac;
42 #if ENABLE_FEATURE_NAMEIF_EXTENDED
48 #if ENABLE_FEATURE_NAMEIF_EXTENDED
49 /* Cut'n'paste from ethtool.h */
50 #define ETHTOOL_BUSINFO_LEN 32
51 /* these strings are set to whatever the driver author decides... */
52 struct ethtool_drvinfo {
54 char driver[32]; /* driver short name, "tulip", "eepro100" */
55 char version[32]; /* driver version string */
56 char fw_version[32]; /* firmware version string, if applicable */
57 char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
58 /* For PCI devices, use pci_dev->slot_name. */
61 uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
62 uint32_t testinfo_len;
63 uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
64 uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
66 #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
70 static void nameif_parse_selector(ethtable_t *ch, char *selector)
72 struct ether_addr *lmac;
73 #if ENABLE_FEATURE_NAMEIF_EXTENDED
74 int found_selector = 0;
79 selector = skip_whitespace(selector);
80 #if ENABLE_FEATURE_NAMEIF_EXTENDED
81 if (*selector == '\0')
83 /* Search for the end .... */
84 next = skip_non_whitespace(selector);
87 /* Check for selectors, mac= is assumed */
88 if (strncmp(selector, "bus=", 4) == 0) {
89 ch->bus_info = xstrdup(selector + 4);
91 } else if (strncmp(selector, "driver=", 7) == 0) {
92 ch->driver = xstrdup(selector + 7);
96 lmac = ether_aton(selector + (strncmp(selector, "mac=", 4) == 0 ? 4 : 0));
97 /* Check ascii selector, convert and copy to *mac */
99 bb_error_msg_and_die("cannot parse %s", selector);
100 ch->mac = xmalloc(ETH_ALEN);
101 memcpy(ch->mac, lmac, ETH_ALEN);
102 #if ENABLE_FEATURE_NAMEIF_EXTENDED
107 if (found_selector == 0)
108 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
112 static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
115 if (strlen(ifname) >= IF_NAMESIZE)
116 bb_error_msg_and_die("interface name '%s' too long", ifname);
117 ch = xzalloc(sizeof(*ch));
119 nameif_parse_selector(ch, selector);
126 int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
127 int nameif_main(int argc, char **argv)
129 ethtable_t *clist = NULL;
131 const char *fname = "/etc/mactab";
138 if (1 & getopt32(argv, "sc:", &fname)) {
139 openlog(applet_name, 0, LOG_LOCAL0);
140 logmode = LOGMODE_SYSLOG;
150 char *ifname = xstrdup(*argv++);
151 prepend_new_eth_table(&clist, ifname, *argv++);
154 ifh = xfopen(fname, "r");
155 while ((line = xmalloc_fgets(ifh)) != NULL) {
158 line_ptr = skip_whitespace(line);
159 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
163 next = skip_non_whitespace(line_ptr);
166 prepend_new_eth_table(&clist, line_ptr, next);
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 *skip_non_whitespace(line_ptr) = '\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) {
242 if (ENABLE_FEATURE_CLEAN_UP) {