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