There are a number of reasons for reinventing printf(). Writing status to
[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 #include <syslog.h>
14 #include <net/if.h>
15 #include <netinet/ether.h>
16
17
18 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
19 #ifndef IF_NAMESIZE
20 #  ifdef IFNAMSIZ
21 #    define IF_NAMESIZE IFNAMSIZ
22 #  else
23 #    define IF_NAMESIZE 16
24 #  endif
25 #endif
26
27 /* take from linux/sockios.h */
28 #define SIOCSIFNAME     0x8923  /* set interface name */
29
30 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
31 #define ETH_ALEN        6
32
33 #ifndef ifr_newname
34 #define ifr_newname ifr_ifru.ifru_slave
35 #endif
36
37 typedef struct mactable_s {
38         struct mactable_s *next;
39         struct mactable_s *prev;
40         char *ifname;
41         struct ether_addr *mac;
42 } mactable_t;
43
44 static unsigned long flags;
45
46 static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
47
48 static void serror(const char *s, ...)
49 {
50         va_list ap;
51
52         va_start(ap, s);
53
54         if (flags & 1) {
55                 openlog(bb_applet_name, 0, LOG_LOCAL0);
56                 vsyslog(LOG_ERR, s, ap);
57                 closelog();
58         } else {
59                 bb_verror_msg(s, ap);
60                 putc('\n', stderr);
61         }
62         va_end(ap);
63
64         exit(EXIT_FAILURE);
65 }
66
67 /* Check ascii str_macaddr, convert and copy to *mac */
68 static struct ether_addr *cc_macaddr(const char *str_macaddr)
69 {
70         struct ether_addr *lmac, *mac;
71
72         lmac = ether_aton(str_macaddr);
73         if (lmac == NULL)
74                 serror("cannot parse MAC %s", str_macaddr);
75         mac = xmalloc(ETH_ALEN);
76         memcpy(mac, lmac, ETH_ALEN);
77
78         return mac;
79 }
80
81 int nameif_main(int argc, char **argv)
82 {
83         mactable_t *clist = NULL;
84         FILE *ifh;
85         const char *fname = "/etc/mactab";
86         char *line;
87         int ctl_sk;
88         int if_index = 1;
89         mactable_t *ch;
90
91         flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
92
93         if ((argc - optind) & 1)
94                 bb_show_usage();
95
96         if (optind < argc) {
97                 char **a = argv + optind;
98
99                 while (*a) {
100
101                         if (strlen(*a) > IF_NAMESIZE)
102                                 serror("interface name `%s' too long", *a);
103                         ch = xzalloc(sizeof(mactable_t));
104                         ch->ifname = xstrdup(*a++);
105                         ch->mac = cc_macaddr(*a++);
106                         if (clist)
107                                 clist->prev = ch;
108                         ch->next = clist;
109                         clist = ch;
110                 }
111         } else {
112                 ifh = xfopen(fname, "r");
113
114                 while ((line = bb_get_line_from_file(ifh)) != NULL) {
115                         char *line_ptr;
116                         size_t name_length;
117
118                         line_ptr = line + strspn(line, " \t");
119                         if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
120                                 free(line);
121                                 continue;
122                         }
123                         name_length = strcspn(line_ptr, " \t");
124                         ch = xzalloc(sizeof(mactable_t));
125                         ch->ifname = xstrndup(line_ptr, name_length);
126                         if (name_length > IF_NAMESIZE)
127                                 serror("interface name `%s' too long", ch->ifname);
128                         line_ptr += name_length;
129                         line_ptr += strspn(line_ptr, " \t");
130                         name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
131                         line_ptr[name_length] = '\0';
132                         ch->mac = cc_macaddr(line_ptr);
133                         if (clist)
134                                 clist->prev = ch;
135                         ch->next = clist;
136                         clist = ch;
137                         free(line);
138                 }
139                 fclose(ifh);
140         }
141
142         if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
143                 serror("socket: %m");
144
145         while (clist) {
146                 struct ifreq ifr;
147
148                 memset(&ifr, 0, sizeof(struct ifreq));
149                 if_index++;
150                 ifr.ifr_ifindex = if_index;
151
152                 /* Get ifname by index or die */
153                 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
154                         break;
155
156                 /* Has this device hwaddr? */
157                 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
158                         continue;
159
160                 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
161                 for (ch = clist; ch; ch = ch->next)
162                         if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
163                                 break;
164
165                 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
166                 if (ch == NULL)
167                         continue;
168
169                 strcpy(ifr.ifr_newname, ch->ifname);
170                 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
171                         serror("cannot change ifname %s to %s: %m",
172                                    ifr.ifr_name, ch->ifname);
173
174                 /* Remove list entry of renamed interface */
175                 if (ch->prev != NULL) {
176                         (ch->prev)->next = ch->next;
177                 } else {
178                         clist = ch->next;
179                 }
180                 if (ch->next != NULL)
181                         (ch->next)->prev = ch->prev;
182                 if (ENABLE_FEATURE_CLEAN_UP) {
183                         free(ch->ifname);
184                         free(ch->mac);
185                         free(ch);
186                 }
187         }
188
189         return 0;
190 }