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>
7 * Glenn McGrath <bug1@iinet.net.au>
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
15 #include <netinet/ether.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 mactable_s {
38 struct mactable_s *next;
39 struct mactable_s *prev;
41 struct ether_addr *mac;
44 /* Check ascii str_macaddr, convert and copy to *mac */
45 static struct ether_addr *cc_macaddr(const char *str_macaddr)
47 struct ether_addr *lmac, *mac;
49 lmac = ether_aton(str_macaddr);
51 bb_error_msg_and_die("cannot parse MAC %s", str_macaddr);
52 mac = xmalloc(ETH_ALEN);
53 memcpy(mac, lmac, ETH_ALEN);
58 int nameif_main(int argc, char **argv)
60 mactable_t *clist = NULL;
62 const char *fname = "/etc/mactab";
68 if (1 & getopt32(argc, argv, "sc:", &fname)) {
69 openlog(applet_name, 0, LOG_LOCAL0);
70 logmode = LOGMODE_SYSLOG;
73 if ((argc - optind) & 1)
77 char **a = argv + optind;
80 if (strlen(*a) > IF_NAMESIZE)
81 bb_error_msg_and_die("interface name `%s' "
83 ch = xzalloc(sizeof(mactable_t));
84 ch->ifname = xstrdup(*a++);
85 ch->mac = cc_macaddr(*a++);
92 ifh = xfopen(fname, "r");
94 while ((line = bb_get_line_from_file(ifh)) != NULL) {
98 line_ptr = line + strspn(line, " \t");
99 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
103 name_length = strcspn(line_ptr, " \t");
104 ch = xzalloc(sizeof(mactable_t));
105 ch->ifname = xstrndup(line_ptr, name_length);
106 if (name_length > IF_NAMESIZE)
107 bb_error_msg_and_die("interface name `%s' "
108 "too long", ch->ifname);
109 line_ptr += name_length;
110 line_ptr += strspn(line_ptr, " \t");
111 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
112 line_ptr[name_length] = '\0';
113 ch->mac = cc_macaddr(line_ptr);
123 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
128 memset(&ifr, 0, sizeof(struct ifreq));
130 ifr.ifr_ifindex = if_index;
132 /* Get ifname by index or die */
133 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
136 /* Has this device hwaddr? */
137 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
140 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
141 for (ch = clist; ch; ch = ch->next)
142 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
145 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
149 strcpy(ifr.ifr_newname, ch->ifname);
150 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
151 bb_perror_msg_and_die("cannot change ifname %s to %s",
152 ifr.ifr_name, ch->ifname);
154 /* Remove list entry of renamed interface */
155 if (ch->prev != NULL) {
156 (ch->prev)->next = ch->next;
160 if (ch->next != NULL)
161 (ch->next)->prev = ch->prev;
162 if (ENABLE_FEATURE_CLEAN_UP) {