2 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 * Writen 2000 by Andi Kleen.
5 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
6 * Glenn McGrath <bug1@optushome.com.au>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/syslog.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
34 #include <netinet/ether.h>
38 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
41 # define IF_NAMESIZE IFNAMSIZ
43 # define IF_NAMESIZE 16
47 /* take from linux/sockios.h */
48 #define SIOCSIFNAME 0x8923 /* set interface name */
50 /* Octets in one ethernet addr, from <linux/if_ether.h> */
54 #define ifr_newname ifr_ifru.ifru_slave
57 typedef struct mactable_s {
58 struct mactable_s *next;
59 struct mactable_s *prev;
61 struct ether_addr *mac;
64 static unsigned char use_syslog;
66 static void serror(const char *s, ...) __attribute__ ((noreturn));
68 static void serror(const char *s, ...)
75 openlog(bb_applet_name, 0, LOG_LOCAL0);
76 vsyslog(LOG_ERR, s, ap);
88 /* Check ascii str_macaddr, convert and copy to *mac */
89 struct ether_addr *cc_macaddr(char *str_macaddr)
91 struct ether_addr *lmac, *mac;
93 lmac = ether_aton(str_macaddr);
95 serror("cannot parse MAC %s", str_macaddr);
96 mac = xmalloc(ETH_ALEN);
97 memcpy(mac, lmac, ETH_ALEN);
102 int nameif_main(int argc, char **argv)
104 mactable_t *clist = NULL;
106 const char *fname = "/etc/mactab";
114 while ((opt = getopt(argc, argv, "c:s")) != -1) {
127 if ((argc - optind) & 1)
131 char **a = argv + optind;
135 if (strlen(*a) > IF_NAMESIZE)
136 serror("interface name `%s' too long", *a);
137 ch = xcalloc(1, sizeof(mactable_t));
138 ch->ifname = bb_xstrdup(*a++);
139 ch->mac = cc_macaddr(*a++);
146 ifh = bb_xfopen(fname, "r");
148 while ((line = bb_get_line_from_file(ifh)) != NULL) {
152 line_ptr = line + strspn(line, " \t");
153 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
155 name_length = strcspn(line_ptr, " \t");
156 ch = xcalloc(1, sizeof(mactable_t));
157 ch->ifname = bb_xstrndup(line_ptr, name_length);
158 if (name_length > IF_NAMESIZE)
159 serror("interface name `%s' too long", ch->ifname);
160 line_ptr += name_length;
161 line_ptr += strspn(line_ptr, " \t");
162 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
163 line_ptr[name_length] = '\0';
164 ch->mac = cc_macaddr(line_ptr);
174 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
175 serror("socket: %m");
180 bzero(&ifr, sizeof(struct ifreq));
182 ifr.ifr_ifindex = if_index;
184 /* Get ifname by index or die */
185 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
188 /* Has this device hwaddr? */
189 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
192 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
193 for (ch = clist; ch; ch = ch->next)
194 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
197 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
201 strcpy(ifr.ifr_newname, ch->ifname);
202 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
203 serror("cannot change ifname %s to %s: %m",
204 ifr.ifr_name, ch->ifname);
206 /* Remove list entry of renamed interface */
207 if (ch->prev != NULL) {
208 (ch->prev)->next = ch->next;
212 if (ch->next != NULL)
213 (ch->next)->prev = ch->prev;
214 #ifdef CONFIG_FEATURE_CLEAN_UP