f13ef1b8b2eddfc9b38104468245cdf32eb66c8d
[oweals/busybox.git] / networking / nameif.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * nameif.c - Naming Interfaces based on MAC address for busybox.
4  *
5  * Written 2000 by Andi Kleen.
6  * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
7  *                      Glenn McGrath <bug1@iinet.net.au>
8  *
9  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10  */
11
12 #include "busybox.h"
13
14 #include <sys/syslog.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <net/if.h>
21 #include <netinet/ether.h>
22
23
24 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
25 #ifndef IF_NAMESIZE
26 #  ifdef IFNAMSIZ
27 #    define IF_NAMESIZE IFNAMSIZ
28 #  else
29 #    define IF_NAMESIZE 16
30 #  endif
31 #endif
32
33 /* take from linux/sockios.h */
34 #define SIOCSIFNAME     0x8923  /* set interface name */
35
36 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
37 #define ETH_ALEN        6
38
39 #ifndef ifr_newname
40 #define ifr_newname ifr_ifru.ifru_slave
41 #endif
42
43 typedef struct mactable_s {
44         struct mactable_s *next;
45         struct mactable_s *prev;
46         char *ifname;
47         struct ether_addr *mac;
48 } mactable_t;
49
50 static unsigned long flags;
51
52 static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
53
54 static void serror(const char *s, ...)
55 {
56         va_list ap;
57
58         va_start(ap, s);
59
60         if (flags & 1) {
61                 openlog(bb_applet_name, 0, LOG_LOCAL0);
62                 vsyslog(LOG_ERR, s, ap);
63                 closelog();
64         } else {
65                 bb_verror_msg(s, ap);
66                 putc('\n', stderr);
67         }
68         va_end(ap);
69
70         exit(EXIT_FAILURE);
71 }
72
73 /* Check ascii str_macaddr, convert and copy to *mac */
74 static struct ether_addr *cc_macaddr(const char *str_macaddr)
75 {
76         struct ether_addr *lmac, *mac;
77
78         lmac = ether_aton(str_macaddr);
79         if (lmac == NULL)
80                 serror("cannot parse MAC %s", str_macaddr);
81         mac = xmalloc(ETH_ALEN);
82         memcpy(mac, lmac, ETH_ALEN);
83
84         return mac;
85 }
86
87 int nameif_main(int argc, char **argv)
88 {
89         mactable_t *clist = NULL;
90         FILE *ifh;
91         const char *fname = "/etc/mactab";
92         char *line;
93         int ctl_sk;
94         int if_index = 1;
95         mactable_t *ch;
96
97         flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
98
99         if ((argc - optind) & 1)
100                 bb_show_usage();
101
102         if (optind < argc) {
103                 char **a = argv + optind;
104
105                 while (*a) {
106
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++);
112                         if (clist)
113                                 clist->prev = ch;
114                         ch->next = clist;
115                         clist = ch;
116                 }
117         } else {
118                 ifh = bb_xfopen(fname, "r");
119
120                 while ((line = bb_get_line_from_file(ifh)) != NULL) {
121                         char *line_ptr;
122                         size_t name_length;
123
124                         line_ptr = line + strspn(line, " \t");
125                         if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
126                                 free(line);
127                                 continue;
128                         }
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);
139                         if (clist)
140                                 clist->prev = ch;
141                         ch->next = clist;
142                         clist = ch;
143                         free(line);
144                 }
145                 fclose(ifh);
146         }
147
148         if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
149                 serror("socket: %m");
150
151         while (clist) {
152                 struct ifreq ifr;
153
154                 memset(&ifr, 0, sizeof(struct ifreq));
155                 if_index++;
156                 ifr.ifr_ifindex = if_index;
157
158                 /* Get ifname by index or die */
159                 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
160                         break;
161
162                 /* Has this device hwaddr? */
163                 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
164                         continue;
165
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))
169                                 break;
170
171                 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
172                 if (ch == NULL)
173                         continue;
174
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);
179
180                 /* Remove list entry of renamed interface */
181                 if (ch->prev != NULL) {
182                         (ch->prev)->next = ch->next;
183                 } else {
184                         clist = ch->next;
185                 }
186                 if (ch->next != NULL)
187                         (ch->next)->prev = ch->prev;
188                 if (ENABLE_FEATURE_CLEAN_UP) {
189                         free(ch->ifname);
190                         free(ch->mac);
191                         free(ch);
192                 }
193         }
194
195         return 0;
196 }