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.
14 #include <sys/syslog.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
21 #include <netinet/ether.h>
24 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
27 # define IF_NAMESIZE IFNAMSIZ
29 # define IF_NAMESIZE 16
33 /* take from linux/sockios.h */
34 #define SIOCSIFNAME 0x8923 /* set interface name */
36 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
40 #define ifr_newname ifr_ifru.ifru_slave
43 typedef struct mactable_s {
44 struct mactable_s *next;
45 struct mactable_s *prev;
47 struct ether_addr *mac;
50 static unsigned long flags;
52 static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
54 static void serror(const char *s, ...)
61 openlog(bb_applet_name, 0, LOG_LOCAL0);
62 vsyslog(LOG_ERR, s, ap);
73 /* Check ascii str_macaddr, convert and copy to *mac */
74 static struct ether_addr *cc_macaddr(const char *str_macaddr)
76 struct ether_addr *lmac, *mac;
78 lmac = ether_aton(str_macaddr);
80 serror("cannot parse MAC %s", str_macaddr);
81 mac = xmalloc(ETH_ALEN);
82 memcpy(mac, lmac, ETH_ALEN);
87 int nameif_main(int argc, char **argv)
89 mactable_t *clist = NULL;
91 const char *fname = "/etc/mactab";
97 flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
99 if ((argc - optind) & 1)
103 char **a = argv + optind;
107 if (strlen(*a) > IF_NAMESIZE)
108 serror("interface name `%s' too long", *a);
109 ch = xzalloc(sizeof(mactable_t));
110 ch->ifname = bb_xstrdup(*a++);
111 ch->mac = cc_macaddr(*a++);
118 ifh = bb_xfopen(fname, "r");
120 while ((line = bb_get_line_from_file(ifh)) != NULL) {
124 line_ptr = line + strspn(line, " \t");
125 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
129 name_length = strcspn(line_ptr, " \t");
130 ch = xzalloc(sizeof(mactable_t));
131 ch->ifname = bb_xstrndup(line_ptr, name_length);
132 if (name_length > IF_NAMESIZE)
133 serror("interface name `%s' too long", ch->ifname);
134 line_ptr += name_length;
135 line_ptr += strspn(line_ptr, " \t");
136 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
137 line_ptr[name_length] = '\0';
138 ch->mac = cc_macaddr(line_ptr);
148 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
149 serror("socket: %m");
154 memset(&ifr, 0, sizeof(struct ifreq));
156 ifr.ifr_ifindex = if_index;
158 /* Get ifname by index or die */
159 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
162 /* Has this device hwaddr? */
163 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
166 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
167 for (ch = clist; ch; ch = ch->next)
168 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
171 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
175 strcpy(ifr.ifr_newname, ch->ifname);
176 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
177 serror("cannot change ifname %s to %s: %m",
178 ifr.ifr_name, ch->ifname);
180 /* Remove list entry of renamed interface */
181 if (ch->prev != NULL) {
182 (ch->prev)->next = ch->next;
186 if (ch->next != NULL)
187 (ch->next)->prev = ch->prev;
188 if (ENABLE_FEATURE_CLEAN_UP) {