2 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 * Written 2000 by Andi Kleen.
5 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
6 * Glenn McGrath <bug1@iinet.net.au>
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 #include <sys/syslog.h>
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
19 #include <netinet/ether.h>
23 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
26 # define IF_NAMESIZE IFNAMSIZ
28 # define IF_NAMESIZE 16
32 /* take from linux/sockios.h */
33 #define SIOCSIFNAME 0x8923 /* set interface name */
35 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
39 #define ifr_newname ifr_ifru.ifru_slave
42 typedef struct mactable_s {
43 struct mactable_s *next;
44 struct mactable_s *prev;
46 struct ether_addr *mac;
49 static unsigned long flags;
51 static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
53 static void serror(const char *s, ...)
60 openlog(bb_applet_name, 0, LOG_LOCAL0);
61 vsyslog(LOG_ERR, s, ap);
72 /* Check ascii str_macaddr, convert and copy to *mac */
73 static struct ether_addr *cc_macaddr(const char *str_macaddr)
75 struct ether_addr *lmac, *mac;
77 lmac = ether_aton(str_macaddr);
79 serror("cannot parse MAC %s", str_macaddr);
80 mac = xmalloc(ETH_ALEN);
81 memcpy(mac, lmac, ETH_ALEN);
86 int nameif_main(int argc, char **argv)
88 mactable_t *clist = NULL;
90 const char *fname = "/etc/mactab";
96 flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
98 if ((argc - optind) & 1)
102 char **a = argv + optind;
106 if (strlen(*a) > IF_NAMESIZE)
107 serror("interface name `%s' too long", *a);
108 ch = xcalloc(1, sizeof(mactable_t));
109 ch->ifname = bb_xstrdup(*a++);
110 ch->mac = cc_macaddr(*a++);
117 ifh = bb_xfopen(fname, "r");
119 while ((line = bb_get_line_from_file(ifh)) != NULL) {
123 line_ptr = line + strspn(line, " \t");
124 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
128 name_length = strcspn(line_ptr, " \t");
129 ch = xcalloc(1, sizeof(mactable_t));
130 ch->ifname = bb_xstrndup(line_ptr, name_length);
131 if (name_length > IF_NAMESIZE)
132 serror("interface name `%s' too long", ch->ifname);
133 line_ptr += name_length;
134 line_ptr += strspn(line_ptr, " \t");
135 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
136 line_ptr[name_length] = '\0';
137 ch->mac = cc_macaddr(line_ptr);
147 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
148 serror("socket: %m");
153 memset(&ifr, 0, sizeof(struct ifreq));
155 ifr.ifr_ifindex = if_index;
157 /* Get ifname by index or die */
158 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
161 /* Has this device hwaddr? */
162 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
165 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
166 for (ch = clist; ch; ch = ch->next)
167 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
170 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
174 strcpy(ifr.ifr_newname, ch->ifname);
175 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
176 serror("cannot change ifname %s to %s: %m",
177 ifr.ifr_name, ch->ifname);
179 /* Remove list entry of renamed interface */
180 if (ch->prev != NULL) {
181 (ch->prev)->next = ch->next;
185 if (ch->next != NULL)
186 (ch->next)->prev = ch->prev;
187 #ifdef CONFIG_FEATURE_CLEAN_UP