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