diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-13 00:01:44 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-13 00:01:44 +0000 |
commit | f03c933e473760cb3776aee8283ecb3e4bddf097 (patch) | |
tree | ba401fac3566fc280a93c7933e4c33767acd99ee | |
parent | 9130c8584783a4449b01cce2a780add080f23dd9 (diff) | |
download | busybox-w32-f03c933e473760cb3776aee8283ecb3e4bddf097.tar.gz busybox-w32-f03c933e473760cb3776aee8283ecb3e4bddf097.tar.bz2 busybox-w32-f03c933e473760cb3776aee8283ecb3e4bddf097.zip |
New applet, nameif by Nick Fedchik and myself.
-rw-r--r-- | include/applets.h | 3 | ||||
-rw-r--r-- | include/usage.h | 13 | ||||
-rw-r--r-- | networking/Config.in | 14 | ||||
-rw-r--r-- | networking/Makefile.in | 1 | ||||
-rw-r--r-- | networking/nameif.c | 217 |
5 files changed, 248 insertions, 0 deletions
diff --git a/include/applets.h b/include/applets.h index 9257fc940..bd2053039 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -368,6 +368,9 @@ | |||
368 | #ifdef CONFIG_MV | 368 | #ifdef CONFIG_MV |
369 | APPLET(mv, mv_main, _BB_DIR_BIN, _BB_SUID_NEVER) | 369 | APPLET(mv, mv_main, _BB_DIR_BIN, _BB_SUID_NEVER) |
370 | #endif | 370 | #endif |
371 | #ifdef CONFIG_NAMEIF | ||
372 | APPLET(nameif, nameif_main, _BB_DIR_SBIN, _BB_SUID_NEVER) | ||
373 | #endif | ||
371 | #ifdef CONFIG_NC | 374 | #ifdef CONFIG_NC |
372 | APPLET(nc, nc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) | 375 | APPLET(nc, nc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) |
373 | #endif | 376 | #endif |
diff --git a/include/usage.h b/include/usage.h index f484ee9ef..d3c1e5072 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -1453,6 +1453,19 @@ | |||
1453 | #define mv_example_usage \ | 1453 | #define mv_example_usage \ |
1454 | "$ mv /tmp/foo /bin/bar\n" | 1454 | "$ mv /tmp/foo /bin/bar\n" |
1455 | 1455 | ||
1456 | #define nameif_trivial_usage \ | ||
1457 | "[OPTIONS] [{IFNAME MACADDR}]" | ||
1458 | #define nameif_full_usage \ | ||
1459 | "Nameif renaming network interface while it in the down state.\n\n" \ | ||
1460 | "Options:\n" \ | ||
1461 | "\t-c FILE\t\tUse another configuration file (default is /etc/mactab)\n" \ | ||
1462 | "\t-s\t\tUse syslog (LOCAL0 facility).\n" \ | ||
1463 | "\tIFNAME MACADDR\tnew_interface_name interface_mac_address\n" | ||
1464 | #define nameif_example_usage \ | ||
1465 | "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n" \ | ||
1466 | " or\n" \ | ||
1467 | "$ nameif -c /etc/my_mactab_file\n" \ | ||
1468 | |||
1456 | #define nc_trivial_usage \ | 1469 | #define nc_trivial_usage \ |
1457 | "[OPTIONS] [IP] [port]" | 1470 | "[OPTIONS] [IP] [port]" |
1458 | #define nc_full_usage \ | 1471 | #define nc_full_usage \ |
diff --git a/networking/Config.in b/networking/Config.in index 92467b748..b4b9462c3 100644 --- a/networking/Config.in +++ b/networking/Config.in | |||
@@ -192,6 +192,20 @@ config CONFIG_IPTUNNEL | |||
192 | help | 192 | help |
193 | Please submit a patch to add help text for this item. | 193 | Please submit a patch to add help text for this item. |
194 | 194 | ||
195 | config CONFIG_NAMEIF | ||
196 | bool "nameif" | ||
197 | default n | ||
198 | help | ||
199 | nameif used to rename network interface by its MAC address. | ||
200 | Renamed interfaces MUST be in the down state. | ||
201 | It is possible to use file (default: /etc/mactab) | ||
202 | with list of new interface names and MACs. | ||
203 | Maximum interface name length: IF_NAMESIZE = 16 | ||
204 | File fields are sepatated by space or tab. | ||
205 | File format: | ||
206 | # Comment | ||
207 | new_interface_name XX:XX:XX:XX:XX:XX | ||
208 | |||
195 | config CONFIG_NC | 209 | config CONFIG_NC |
196 | bool "nc" | 210 | bool "nc" |
197 | default n | 211 | default n |
diff --git a/networking/Makefile.in b/networking/Makefile.in index fc6a3b7e0..e72b00194 100644 --- a/networking/Makefile.in +++ b/networking/Makefile.in | |||
@@ -32,6 +32,7 @@ NETWORKING-$(CONFIG_IPADDR) += ipaddr.o | |||
32 | NETWORKING-$(CONFIG_IPLINK) += iplink.o | 32 | NETWORKING-$(CONFIG_IPLINK) += iplink.o |
33 | NETWORKING-$(CONFIG_IPROUTE) += iproute.o | 33 | NETWORKING-$(CONFIG_IPROUTE) += iproute.o |
34 | NETWORKING-$(CONFIG_IPTUNNEL) += iptunnel.o | 34 | NETWORKING-$(CONFIG_IPTUNNEL) += iptunnel.o |
35 | NETWORKING-$(CONFIG_NAMEIF) += nameif.o | ||
35 | NETWORKING-$(CONFIG_NC) += nc.o | 36 | NETWORKING-$(CONFIG_NC) += nc.o |
36 | NETWORKING-$(CONFIG_NETSTAT) += netstat.o | 37 | NETWORKING-$(CONFIG_NETSTAT) += netstat.o |
37 | NETWORKING-$(CONFIG_NSLOOKUP) += nslookup.o | 38 | NETWORKING-$(CONFIG_NSLOOKUP) += nslookup.o |
diff --git a/networking/nameif.c b/networking/nameif.c new file mode 100644 index 000000000..1edd64111 --- /dev/null +++ b/networking/nameif.c | |||
@@ -0,0 +1,217 @@ | |||
1 | /* | ||
2 | * nameif.c - Naming Interfaces based on MAC address for busybox. | ||
3 | * | ||
4 | * Writen 2000 by Andi Kleen. | ||
5 | * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua> | ||
6 | * Glenn McGrath <bug1@optushome.com.au> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
21 | * 02111-1307 USA | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include <sys/syslog.h> | ||
26 | #include <sys/socket.h> | ||
27 | #include <sys/ioctl.h> | ||
28 | |||
29 | #include <errno.h> | ||
30 | #include <getopt.h> | ||
31 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | #include <net/if.h> | ||
34 | #include <netinet/ether.h> | ||
35 | |||
36 | #include "busybox.h" | ||
37 | |||
38 | /* set interface name, from <linux/sockios.h> */ | ||
39 | #define SIOCSIFNAME 0x8923 | ||
40 | /* Octets in one ethernet addr, from <linux/if_ether.h> */ | ||
41 | #define ETH_ALEN 6 | ||
42 | |||
43 | #ifndef ifr_newname | ||
44 | #define ifr_newname ifr_ifru.ifru_slave | ||
45 | #endif | ||
46 | |||
47 | typedef struct mactable_s { | ||
48 | struct mactable_s *next; | ||
49 | struct mactable_s **pprev; | ||
50 | char *ifname; | ||
51 | struct ether_addr *mac; | ||
52 | } mactable_t; | ||
53 | |||
54 | static void serror_msg_and_die(const char use_syslog, const char *s, ...) | ||
55 | { | ||
56 | va_list ap; | ||
57 | |||
58 | va_start(ap, s); | ||
59 | |||
60 | if (use_syslog) { | ||
61 | openlog("nameif", 0, LOG_LOCAL0); | ||
62 | syslog(LOG_ERR, s, ap); | ||
63 | closelog(); | ||
64 | } else { | ||
65 | vfprintf(stderr, s, ap); | ||
66 | putc('\n', stderr); | ||
67 | } | ||
68 | |||
69 | va_end(ap); | ||
70 | |||
71 | exit(EXIT_FAILURE); | ||
72 | } | ||
73 | |||
74 | int nameif_main(int argc, char **argv) | ||
75 | { | ||
76 | mactable_t *clist = NULL; | ||
77 | FILE *ifh; | ||
78 | char *fname = "/etc/mactab"; | ||
79 | char *line; | ||
80 | unsigned short linenum = 0; | ||
81 | unsigned char use_syslog = 0; | ||
82 | int ctl_sk = -1; | ||
83 | int opt; | ||
84 | |||
85 | static struct option opts[] = { | ||
86 | {"syslog", 0, NULL, 's'}, | ||
87 | {"configfile", 1, NULL, 'c'}, | ||
88 | {NULL}, | ||
89 | }; | ||
90 | |||
91 | while ((opt = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) { | ||
92 | switch (opt) { | ||
93 | case 'c': | ||
94 | fname = optarg; | ||
95 | break; | ||
96 | case 's': | ||
97 | use_syslog = 1; | ||
98 | break; | ||
99 | default: | ||
100 | show_usage(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | if ((argc - optind) & 1) { | ||
105 | show_usage(); | ||
106 | } | ||
107 | |||
108 | if (optind < argc) { | ||
109 | while (optind < argc) { | ||
110 | struct ether_addr *mac; | ||
111 | mactable_t *ch; | ||
112 | |||
113 | if (strlen(argv[optind]) > IF_NAMESIZE) { | ||
114 | serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]); | ||
115 | } | ||
116 | optind++; | ||
117 | mac = ether_aton(argv[optind]); | ||
118 | if (mac == NULL) { | ||
119 | serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]); | ||
120 | } | ||
121 | ch = xcalloc(1, sizeof(mactable_t)); | ||
122 | ch->ifname = strdup(argv[optind - 1]); | ||
123 | ch->mac = xcalloc(1, ETH_ALEN); | ||
124 | memcpy(ch->mac, &mac, ETH_ALEN); | ||
125 | optind++; | ||
126 | if (clist) | ||
127 | clist->pprev = &ch->next; | ||
128 | ch->next = clist; | ||
129 | ch->pprev = &clist; | ||
130 | clist = ch; | ||
131 | } | ||
132 | } else { | ||
133 | ifh = xfopen(fname, "r"); | ||
134 | |||
135 | while ((line = get_line_from_file(ifh)) != NULL) { | ||
136 | struct ether_addr *mac; | ||
137 | mactable_t *ch; | ||
138 | char *line_ptr; | ||
139 | unsigned short name_length; | ||
140 | |||
141 | line_ptr = line + strspn(line, " \t"); | ||
142 | if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) | ||
143 | continue; | ||
144 | name_length = strcspn(line_ptr, " \t"); | ||
145 | if (name_length > IF_NAMESIZE) { | ||
146 | serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]); | ||
147 | } | ||
148 | ch = xcalloc(1, sizeof(mactable_t)); | ||
149 | ch->ifname = strndup(line_ptr, name_length); | ||
150 | line_ptr += name_length; | ||
151 | line_ptr += strspn(line_ptr, " \t"); | ||
152 | name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); | ||
153 | line_ptr[name_length] = '\0'; | ||
154 | mac = ether_aton(line_ptr); | ||
155 | if (mac == NULL) { | ||
156 | serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]); | ||
157 | } | ||
158 | ch->mac = xcalloc(1, ETH_ALEN); | ||
159 | memcpy(ch->mac, mac, ETH_ALEN); | ||
160 | if (clist) | ||
161 | clist->pprev = &ch->next; | ||
162 | ch->next = clist; | ||
163 | ch->pprev = &clist; | ||
164 | clist = ch; | ||
165 | free(line); | ||
166 | } | ||
167 | fclose(ifh); | ||
168 | } | ||
169 | |||
170 | ifh = xfopen("/proc/net/dev", "r"); | ||
171 | while ((line = get_line_from_file(ifh)) != NULL) { | ||
172 | char *line_ptr; | ||
173 | unsigned short iface_name_length; | ||
174 | struct ifreq ifr; | ||
175 | mactable_t *ch = NULL; | ||
176 | |||
177 | linenum++; | ||
178 | if (linenum < 3) | ||
179 | continue; | ||
180 | line_ptr = line + strspn(line, " \t"); | ||
181 | if (line_ptr[0] == '\n') | ||
182 | continue; | ||
183 | iface_name_length = strcspn(line_ptr, ":"); | ||
184 | if (ctl_sk < 0) | ||
185 | ctl_sk = socket(PF_INET, SOCK_DGRAM, 0); | ||
186 | memset(&ifr, 0, sizeof(struct ifreq)); | ||
187 | strncpy(ifr.ifr_name, line_ptr, iface_name_length); | ||
188 | if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr) < 0) { | ||
189 | serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno)); | ||
190 | } | ||
191 | for (ch = clist; ch; ch = ch->next) | ||
192 | if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN)) | ||
193 | break; | ||
194 | if (ch == NULL) { | ||
195 | continue; | ||
196 | } | ||
197 | strcpy(ifr.ifr_newname, ch->ifname); | ||
198 | |||
199 | if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) {; | ||
200 | serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno)); | ||
201 | } | ||
202 | *ch->pprev = ch->next; | ||
203 | free(ch); | ||
204 | free(line); | ||
205 | } | ||
206 | fclose(ifh); | ||
207 | |||
208 | while (clist) { | ||
209 | mactable_t *ch; | ||
210 | |||
211 | ch = clist; | ||
212 | clist = clist->next; | ||
213 | free(ch); | ||
214 | } | ||
215 | |||
216 | return 0; | ||
217 | } | ||