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