1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74#include "libbb.h"
75#include <syslog.h>
76#include <net/if.h>
77#include <netinet/ether.h>
78#include <linux/sockios.h>
79
80#ifndef IFNAMSIZ
81#define IFNAMSIZ 16
82#endif
83
84
85#define SIOCSIFNAME 0x8923
86
87
88#define ETH_ALEN 6
89
90#ifndef ifr_newname
91#define ifr_newname ifr_ifru.ifru_slave
92#endif
93
94typedef struct ethtable_s {
95 struct ethtable_s *next;
96 struct ethtable_s *prev;
97 char *ifname;
98 struct ether_addr *mac;
99#if ENABLE_FEATURE_NAMEIF_EXTENDED
100 char *bus_info;
101 char *driver;
102 int32_t phy_address;
103#endif
104} ethtable_t;
105
106#if ENABLE_FEATURE_NAMEIF_EXTENDED
107
108#define ETHTOOL_BUSINFO_LEN 32
109
110struct ethtool_drvinfo {
111 uint32_t cmd;
112 char driver[32];
113 char version[32];
114 char fw_version[32];
115 char bus_info[ETHTOOL_BUSINFO_LEN];
116
117 char reserved1[32];
118 char reserved2[16];
119 uint32_t n_stats;
120 uint32_t testinfo_len;
121 uint32_t eedump_len;
122 uint32_t regdump_len;
123};
124
125struct ethtool_cmd {
126 uint32_t cmd;
127 uint32_t supported;
128 uint32_t advertising;
129 uint16_t speed;
130 uint8_t duplex;
131 uint8_t port;
132 uint8_t phy_address;
133 uint8_t transceiver;
134 uint8_t autoneg;
135 uint32_t maxtxpkt;
136 uint32_t maxrxpkt;
137 uint16_t speed_hi;
138 uint16_t reserved2;
139 uint32_t reserved[3];
140};
141
142#define ETHTOOL_GSET 0x00000001
143#define ETHTOOL_GDRVINFO 0x00000003
144#endif
145
146
147static void nameif_parse_selector(ethtable_t *ch, char *selector)
148{
149 struct ether_addr *lmac;
150#if ENABLE_FEATURE_NAMEIF_EXTENDED
151 int found_selector = 0;
152
153 while (*selector) {
154 char *next;
155#endif
156 selector = skip_whitespace(selector);
157#if ENABLE_FEATURE_NAMEIF_EXTENDED
158 ch->phy_address = -1;
159 if (*selector == '\0')
160 break;
161
162 next = skip_non_whitespace(selector);
163 if (*next)
164 *next++ = '\0';
165
166 if (is_prefixed_with(selector, "bus=")) {
167 ch->bus_info = xstrdup(selector + 4);
168 found_selector++;
169 } else if (is_prefixed_with(selector, "driver=")) {
170 ch->driver = xstrdup(selector + 7);
171 found_selector++;
172 } else if (is_prefixed_with(selector, "phyaddr=")) {
173 ch->phy_address = xatoi_positive(selector + 8);
174 found_selector++;
175 } else {
176#endif
177 lmac = xmalloc(ETH_ALEN);
178 ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac);
179 if (ch->mac == NULL)
180 bb_error_msg_and_die("can't parse %s", selector);
181#if ENABLE_FEATURE_NAMEIF_EXTENDED
182 found_selector++;
183 };
184 selector = next;
185 }
186 if (found_selector == 0)
187 bb_error_msg_and_die("no selectors found for %s", ch->ifname);
188#endif
189}
190
191static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
192{
193 ethtable_t *ch;
194 if (strlen(ifname) >= IFNAMSIZ)
195 bb_error_msg_and_die("interface name '%s' too long", ifname);
196 ch = xzalloc(sizeof(*ch));
197 ch->ifname = xstrdup(ifname);
198 nameif_parse_selector(ch, selector);
199 ch->next = *clist;
200 if (*clist)
201 (*clist)->prev = ch;
202 *clist = ch;
203}
204
205#if ENABLE_FEATURE_CLEAN_UP
206static void delete_eth_table(ethtable_t *ch)
207{
208 free(ch->ifname);
209#if ENABLE_FEATURE_NAMEIF_EXTENDED
210 free(ch->bus_info);
211 free(ch->driver);
212#endif
213 free(ch->mac);
214 free(ch);
215};
216#else
217void delete_eth_table(ethtable_t *ch);
218#endif
219
220int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
221int nameif_main(int argc UNUSED_PARAM, char **argv)
222{
223 ethtable_t *clist = NULL;
224 const char *fname = "/etc/mactab";
225 int ctl_sk;
226 ethtable_t *ch;
227 parser_t *parser;
228 char *token[2];
229
230 if (1 & getopt32(argv, "sc:", &fname)) {
231 openlog(applet_name, 0, LOG_LOCAL0);
232
233
234 logmode |= LOGMODE_SYSLOG;
235 }
236 argv += optind;
237
238 if (argv[0]) {
239 do {
240 if (!argv[1])
241 bb_show_usage();
242 prepend_new_eth_table(&clist, argv[0], argv[1]);
243 argv += 2;
244 } while (*argv);
245 } else {
246 parser = config_open(fname);
247 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
248 prepend_new_eth_table(&clist, token[0], token[1]);
249 config_close(parser);
250 }
251
252 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
253 parser = config_open2("/proc/net/dev", xfopen_for_read);
254
255 while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
256 struct ifreq ifr;
257#if ENABLE_FEATURE_NAMEIF_EXTENDED
258 struct ethtool_drvinfo drvinfo;
259 struct ethtool_cmd eth_settings;
260#endif
261 if (parser->lineno <= 2)
262 continue;
263
264
265 memset(&ifr, 0, sizeof(struct ifreq));
266 strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
267
268#if ENABLE_FEATURE_NAMEIF_EXTENDED
269
270 memset(ð_settings, 0, sizeof(eth_settings));
271 eth_settings.cmd = ETHTOOL_GSET;
272 ifr.ifr_data = (caddr_t) ð_settings;
273 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
274
275
276 memset(&drvinfo, 0, sizeof(drvinfo));
277 drvinfo.cmd = ETHTOOL_GDRVINFO;
278 ifr.ifr_data = (caddr_t) &drvinfo;
279
280 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
281#endif
282 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
283
284
285 for (ch = clist; ch; ch = ch->next) {
286#if ENABLE_FEATURE_NAMEIF_EXTENDED
287 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
288 continue;
289 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
290 continue;
291 if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
292 continue;
293#endif
294 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
295 continue;
296
297 goto found;
298 }
299
300 continue;
301 found:
302 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
303 strcpy(ifr.ifr_newname, ch->ifname);
304 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
305 "can't change ifname %s to %s",
306 ifr.ifr_name, ch->ifname);
307 }
308
309 if (ch->prev != NULL)
310 ch->prev->next = ch->next;
311 else
312 clist = ch->next;
313 if (ch->next != NULL)
314 ch->next->prev = ch->prev;
315 if (ENABLE_FEATURE_CLEAN_UP)
316 delete_eth_table(ch);
317 }
318
319 if (ENABLE_FEATURE_CLEAN_UP) {
320 ethtable_t *next;
321 for (ch = clist; ch; ch = next) {
322 next = ch->next;
323 delete_eth_table(ch);
324 }
325 config_close(parser);
326 };
327
328 return 0;
329}
330