Initial public busybox upstream commit
[busybox4maemo] / 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
8  * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
9  *
10  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11  */
12
13 #include "libbb.h"
14 #include <syslog.h>
15 #include <net/if.h>
16 #include <netinet/ether.h>
17 #include <linux/sockios.h>
18
19 /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
20 #ifndef IF_NAMESIZE
21 #  ifdef IFNAMSIZ
22 #    define IF_NAMESIZE IFNAMSIZ
23 #  else
24 #    define IF_NAMESIZE 16
25 #  endif
26 #endif
27
28 /* take from linux/sockios.h */
29 #define SIOCSIFNAME     0x8923  /* set interface name */
30
31 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
32 #define ETH_ALEN        6
33
34 #ifndef ifr_newname
35 #define ifr_newname ifr_ifru.ifru_slave
36 #endif
37
38 typedef struct ethtable_s {
39         struct ethtable_s *next;
40         struct ethtable_s *prev;
41         char *ifname;
42         struct ether_addr *mac;
43 #if ENABLE_FEATURE_NAMEIF_EXTENDED
44         char *bus_info;
45         char *driver;
46 #endif
47 } ethtable_t;
48
49 #if ENABLE_FEATURE_NAMEIF_EXTENDED
50 /* Cut'n'paste from ethtool.h */
51 #define ETHTOOL_BUSINFO_LEN 32
52 /* these strings are set to whatever the driver author decides... */
53 struct ethtool_drvinfo {
54         uint32_t cmd;
55         char  driver[32]; /* driver short name, "tulip", "eepro100" */
56         char  version[32];  /* driver version string */
57         char  fw_version[32]; /* firmware version string, if applicable */
58         char  bus_info[ETHTOOL_BUSINFO_LEN];  /* Bus info for this IF. */
59         /* For PCI devices, use pci_dev->slot_name. */
60         char  reserved1[32];
61         char  reserved2[16];
62         uint32_t n_stats;  /* number of u64's from ETHTOOL_GSTATS */
63         uint32_t testinfo_len;
64         uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
65         uint32_t regdump_len;  /* Size of data from ETHTOOL_GREGS (bytes) */
66 };
67 #define ETHTOOL_GDRVINFO  0x00000003 /* Get driver info. */
68 #endif
69
70
71 static void nameif_parse_selector(ethtable_t *ch, char *selector)
72 {
73         struct ether_addr *lmac;
74 #if ENABLE_FEATURE_NAMEIF_EXTENDED
75         int found_selector = 0;
76
77         while (*selector) {
78                 char *next;
79 #endif
80                 selector = skip_whitespace(selector);
81 #if ENABLE_FEATURE_NAMEIF_EXTENDED
82                 if (*selector == '\0')
83                         break;
84                 /* Search for the end .... */
85                 next = skip_non_whitespace(selector);
86                 if (*next)
87                         *next++ = '\0';
88                 /* Check for selectors, mac= is assumed */
89                 if (strncmp(selector, "bus=", 4) == 0) {
90                         ch->bus_info = xstrdup(selector + 4);
91                         found_selector++;
92                 } else if (strncmp(selector, "driver=", 7) == 0) {
93                         ch->driver = xstrdup(selector + 7);
94                         found_selector++;
95                 } else {
96 #endif
97                         lmac = xmalloc(ETH_ALEN);
98                         ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) ? 0 : 4), lmac);
99                         if (ch->mac == NULL)
100                                 bb_error_msg_and_die("cannot parse %s", selector);
101 #if  ENABLE_FEATURE_NAMEIF_EXTENDED
102                         found_selector++;
103                 };
104                 selector = next;
105         }
106         if (found_selector == 0)
107                 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
108 #endif
109 }
110
111 static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
112 {
113         ethtable_t *ch;
114         if (strlen(ifname) >= IF_NAMESIZE)
115                 bb_error_msg_and_die("interface name '%s' too long", ifname);
116         ch = xzalloc(sizeof(*ch));
117         ch->ifname = xstrdup(ifname);
118         nameif_parse_selector(ch, selector);
119         ch->next = *clist;
120         if (*clist)
121                 (*clist)->prev = ch;
122         *clist = ch;
123 }
124
125 #if ENABLE_FEATURE_CLEAN_UP
126 static void delete_eth_table(ethtable_t *ch)
127 {
128         free(ch->ifname);
129 #if ENABLE_FEATURE_NAMEIF_EXTENDED
130         free(ch->bus_info);
131         free(ch->driver);
132 #endif
133         free(ch->mac);
134         free(ch);
135 };
136 #else
137 void delete_eth_table(ethtable_t *ch);
138 #endif
139
140 int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
141 int nameif_main(int argc, char **argv)
142 {
143         ethtable_t *clist = NULL;
144         FILE *ifh;
145         const char *fname = "/etc/mactab";
146         char *line;
147         char *line_ptr;
148         int linenum;
149         int ctl_sk;
150         ethtable_t *ch;
151
152         if (1 & getopt32(argv, "sc:", &fname)) {
153                 openlog(applet_name, 0, LOG_LOCAL0);
154                 logmode = LOGMODE_SYSLOG;
155         }
156         argc -= optind;
157         argv += optind;
158
159         if (argc & 1)
160                 bb_show_usage();
161
162         if (argc) {
163                 while (*argv) {
164                         char *ifname = xstrdup(*argv++);
165                         prepend_new_eth_table(&clist, ifname, *argv++);
166                 }
167         } else {
168                 ifh = xfopen(fname, "r");
169                 while ((line = xmalloc_fgets(ifh)) != NULL) {
170                         char *next;
171
172                         line_ptr = skip_whitespace(line);
173                         if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
174                                 goto read_next_line;
175                         next = skip_non_whitespace(line_ptr);
176                         if (*next)
177                                 *next++ = '\0';
178                         prepend_new_eth_table(&clist, line_ptr, next);
179                         read_next_line:
180                         free(line);
181                 }
182                 fclose(ifh);
183         }
184
185         ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
186         ifh = xfopen("/proc/net/dev", "r");
187
188         linenum = 0;
189         while (clist) {
190                 struct ifreq ifr;
191 #if  ENABLE_FEATURE_NAMEIF_EXTENDED
192                 struct ethtool_drvinfo drvinfo;
193 #endif
194
195                 line = xmalloc_fgets(ifh);
196                 if (line == NULL)
197                         break; /* Seems like we're done */
198                 if (linenum++ < 2 )
199                         goto next_line; /* Skip the first two lines */
200
201                 /* Find the current interface name and copy it to ifr.ifr_name */
202                 line_ptr = skip_whitespace(line);
203                 *strpbrk(line_ptr, " \t\n:") = '\0';
204
205                 memset(&ifr, 0, sizeof(struct ifreq));
206                 strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
207
208 #if ENABLE_FEATURE_NAMEIF_EXTENDED
209                 /* Check for driver etc. */
210                 memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo));
211                 drvinfo.cmd = ETHTOOL_GDRVINFO;
212                 ifr.ifr_data = (caddr_t) &drvinfo;
213                 /* Get driver and businfo first, so we have it in drvinfo */
214                 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
215 #endif
216                 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
217
218                 /* Search the list for a matching device */
219                 for (ch = clist; ch; ch = ch->next) {
220 #if ENABLE_FEATURE_NAMEIF_EXTENDED
221                         if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
222                                 continue;
223                         if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
224                                 continue;
225 #endif
226                         if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
227                                 continue;
228                         /* if we came here, all selectors have matched */
229                         goto found;
230                 }
231                 /* Nothing found for current interface */
232                 goto next_line;
233  found:
234                 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
235                         strcpy(ifr.ifr_newname, ch->ifname);
236                         ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
237                                         "cannot change ifname %s to %s",
238                                         ifr.ifr_name, ch->ifname);
239                 }
240                 /* Remove list entry of renamed interface */
241                 if (ch->prev != NULL)
242                         ch->prev->next = ch->next;
243                 else
244                         clist = ch->next;
245                 if (ch->next != NULL)
246                 ch->next->prev = ch->prev;
247                 if (ENABLE_FEATURE_CLEAN_UP)
248                         delete_eth_table(ch);
249  next_line:
250                 free(line);
251         }
252         if (ENABLE_FEATURE_CLEAN_UP) {
253                 for (ch = clist; ch; ch = ch->next)
254                         delete_eth_table(ch);
255                 fclose(ifh);
256         };
257
258         return 0;
259 }