aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-02-14 08:11:27 +0000
committerEric Andersen <andersen@codepoet.org>2001-02-14 08:11:27 +0000
commitec455953008d08f588fe0c0bd9df4278cd188315 (patch)
treeece4d8fefb69365210a9e623df07b41086cd5613
parent88a5ceb450b576558b7d34e0baadb204dd329fb8 (diff)
downloadbusybox-w32-ec455953008d08f588fe0c0bd9df4278cd188315.tar.gz
busybox-w32-ec455953008d08f588fe0c0bd9df4278cd188315.tar.bz2
busybox-w32-ec455953008d08f588fe0c0bd9df4278cd188315.zip
Add in ifconfig and route
-rw-r--r--Config.h2
-rw-r--r--applets.h6
-rw-r--r--applets/usage.c29
-rw-r--r--busybox.h1
-rw-r--r--ifconfig.c476
-rw-r--r--include/applets.h6
-rw-r--r--include/busybox.h1
-rw-r--r--networking/ifconfig.c476
-rw-r--r--networking/route.c372
-rw-r--r--route.c372
-rw-r--r--usage.c29
-rw-r--r--utility.c8
12 files changed, 1778 insertions, 0 deletions
diff --git a/Config.h b/Config.h
index c9e675093..38a9559fa 100644
--- a/Config.h
+++ b/Config.h
@@ -47,6 +47,7 @@
47//#define BB_HOSTID 47//#define BB_HOSTID
48//#define BB_HOSTNAME 48//#define BB_HOSTNAME
49#define BB_ID 49#define BB_ID
50//#define BB_IFCONFIG
50#define BB_INIT 51#define BB_INIT
51//#define BB_INSMOD 52//#define BB_INSMOD
52#define BB_KILL 53#define BB_KILL
@@ -87,6 +88,7 @@
87#define BB_RM 88#define BB_RM
88#define BB_RMDIR 89#define BB_RMDIR
89//#define BB_RMMOD 90//#define BB_RMMOD
91//#define BB_ROUTE
90//#define BB_RPMUNPACK 92//#define BB_RPMUNPACK
91#define BB_SED 93#define BB_SED
92//#define BB_SETKEYCODES 94//#define BB_SETKEYCODES
diff --git a/applets.h b/applets.h
index 4bd8b79e4..9aa65dac0 100644
--- a/applets.h
+++ b/applets.h
@@ -158,6 +158,9 @@ const struct BB_applet applets[] = {
158#ifdef BB_ID 158#ifdef BB_ID
159 APPLET("id", id_main, _BB_DIR_USR_BIN, id_usage) 159 APPLET("id", id_main, _BB_DIR_USR_BIN, id_usage)
160#endif 160#endif
161#ifdef BB_IFCONFIG
162 APPLET("ifconfig", ifconfig_main, _BB_DIR_SBIN, ifconfig_usage)
163#endif
161#ifdef BB_INIT 164#ifdef BB_INIT
162 APPLET_NOUSAGE("init", init_main, _BB_DIR_SBIN) 165 APPLET_NOUSAGE("init", init_main, _BB_DIR_SBIN)
163#endif 166#endif
@@ -284,6 +287,9 @@ const struct BB_applet applets[] = {
284#ifdef BB_RMMOD 287#ifdef BB_RMMOD
285 APPLET("rmmod", rmmod_main, _BB_DIR_SBIN, rmmod_usage) 288 APPLET("rmmod", rmmod_main, _BB_DIR_SBIN, rmmod_usage)
286#endif 289#endif
290#ifdef BB_ROUTE
291 APPLET("route", route_main, _BB_DIR_USR_BIN, route_usage)
292#endif
287#ifdef BB_RPMUNPACK 293#ifdef BB_RPMUNPACK
288 APPLET("rpmunpack", rpmunpack_main, _BB_DIR_USR_BIN, rpmunpack_usage) 294 APPLET("rpmunpack", rpmunpack_main, _BB_DIR_USR_BIN, rpmunpack_usage)
289#endif 295#endif
diff --git a/applets/usage.c b/applets/usage.c
index 13107c6e0..bdd4d3d83 100644
--- a/applets/usage.c
+++ b/applets/usage.c
@@ -556,6 +556,26 @@ const char id_usage[] =
556 ; 556 ;
557#endif 557#endif
558 558
559#if defined BB_IFCONFIG
560const char ifconfig_usage[] =
561 "ifconfig [-a] [-i] [-v] <interface> [<address>]"
562#ifndef BB_FEATURE_TRIVIAL_HELP
563 "\n\nconfigure a network interface\n\n"
564 "Options:\n"
565 " [[-]broadcast [<address>]] [[-]pointopoint [<address>]]\n"
566 " [netmask <address>] [dstaddr <address>] [tunnel <adress>]\n"
567#ifdef SIOCSKEEPALIVE
568 " [outfill <NN>] [keepalive <NN>]\n"
569#endif
570 " [hw ether <address>] [metric <NN>] [mtu <NN>]\n"
571 " [[-]trailers] [[-]arp] [[-]allmulti]\n"
572 " [multicast] [[-]promisc]\n"
573 " [mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]\n"
574 " [up|down] ..."
575#endif
576 ;
577#endif
578
559#if defined BB_INSMOD 579#if defined BB_INSMOD
560const char insmod_usage[] = 580const char insmod_usage[] =
561 "insmod [OPTION]... MODULE [symbol=value]..." 581 "insmod [OPTION]... MODULE [symbol=value]..."
@@ -1144,6 +1164,15 @@ const char rmmod_usage[] =
1144 ; 1164 ;
1145#endif 1165#endif
1146 1166
1167#if defined BB_ROUTE
1168const char route_usage[] =
1169 "route [{add|del|flush}]"
1170#ifndef BB_FEATURE_TRIVIAL_HELP
1171 "\n\nEdit the kernel's routing tables"
1172#endif
1173 ;
1174#endif
1175
1147#if defined BB_RPMUNPACK 1176#if defined BB_RPMUNPACK
1148const char rpmunpack_usage[] = 1177const char rpmunpack_usage[] =
1149 "rpmunpack < package.rpm | gunzip | cpio -idmuv" 1178 "rpmunpack < package.rpm | gunzip | cpio -idmuv"
diff --git a/busybox.h b/busybox.h
index 101e65989..39580b548 100644
--- a/busybox.h
+++ b/busybox.h
@@ -161,6 +161,7 @@ extern void *xcalloc(size_t nmemb, size_t size);
161extern char *xstrdup (const char *s); 161extern char *xstrdup (const char *s);
162#endif 162#endif
163extern char *xstrndup (const char *s, int n); 163extern char *xstrndup (const char *s, int n);
164extern char * safe_strncpy(char *dst, const char *src, size_t size);
164 165
165struct suffix_mult { 166struct suffix_mult {
166 char *suffix; 167 char *suffix;
diff --git a/ifconfig.c b/ifconfig.c
new file mode 100644
index 000000000..2134f8a48
--- /dev/null
+++ b/ifconfig.c
@@ -0,0 +1,476 @@
1/* ifconfig
2 *
3 * Similar to the standard Unix ifconfig, but with only the necessary
4 * parts for AF_INET, and without any printing of if info (for now).
5 *
6 * Bjorn Wesen, Axis Communications AB
7 *
8 *
9 * Authors of the original ifconfig was:
10 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * $Id: ifconfig.c,v 1.1 2001/02/14 08:11:27 andersen Exp $
19 *
20 */
21
22#include "busybox.h"
23#include <sys/types.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h> // strcmp and friends
28#include <ctype.h> // isdigit and friends
29#include <sys/socket.h>
30#include <sys/ioctl.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <net/if.h>
34#include <net/if_arp.h>
35#include <linux/if_ether.h>
36
37static int sockfd; /* socket fd we use to manipulate stuff with */
38
39/* print usage and exit */
40
41#define _(x) x
42
43/* Set a certain interface flag. */
44static int
45set_flag(char *ifname, short flag)
46{
47 struct ifreq ifr;
48
49 strcpy(ifr.ifr_name, ifname);
50 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
51 perror("SIOCGIFFLAGS");
52 return (-1);
53 }
54 strcpy(ifr.ifr_name, ifname);
55 ifr.ifr_flags |= flag;
56 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
57 perror("SIOCSIFFLAGS");
58 return -1;
59 }
60 return (0);
61}
62
63
64/* Clear a certain interface flag. */
65static int
66clr_flag(char *ifname, short flag)
67{
68 struct ifreq ifr;
69
70 strcpy(ifr.ifr_name, ifname);
71 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
72 perror("SIOCGIFFLAGS");
73 return -1;
74 }
75 strcpy(ifr.ifr_name, ifname);
76 ifr.ifr_flags &= ~flag;
77 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
78 perror("SIOCSIFFLAGS");
79 return -1;
80 }
81 return (0);
82}
83
84/* resolve XXX.YYY.ZZZ.QQQ -> binary */
85
86static int
87INET_resolve(char *name, struct sockaddr_in *sin)
88{
89 sin->sin_family = AF_INET;
90 sin->sin_port = 0;
91
92 /* Default is special, meaning 0.0.0.0. */
93 if (!strcmp(name, "default")) {
94 sin->sin_addr.s_addr = INADDR_ANY;
95 return (1);
96 }
97 /* Look to see if it's a dotted quad. */
98 if (inet_aton(name, &sin->sin_addr)) {
99 return 0;
100 }
101 /* guess not.. */
102 return -1;
103}
104
105/* Input an Ethernet address and convert to binary. */
106static int
107in_ether(char *bufp, struct sockaddr *sap)
108{
109 unsigned char *ptr;
110 char c, *orig;
111 int i;
112 unsigned val;
113
114 sap->sa_family = ARPHRD_ETHER;
115 ptr = sap->sa_data;
116
117 i = 0;
118 orig = bufp;
119 while ((*bufp != '\0') && (i < ETH_ALEN)) {
120 val = 0;
121 c = *bufp++;
122 if (isdigit(c))
123 val = c - '0';
124 else if (c >= 'a' && c <= 'f')
125 val = c - 'a' + 10;
126 else if (c >= 'A' && c <= 'F')
127 val = c - 'A' + 10;
128 else {
129#ifdef DEBUG
130 fprintf(stderr,
131 _("in_ether(%s): invalid ether address!\n"),
132 orig);
133#endif
134 errno = EINVAL;
135 return (-1);
136 }
137 val <<= 4;
138 c = *bufp;
139 if (isdigit(c))
140 val |= c - '0';
141 else if (c >= 'a' && c <= 'f')
142 val |= c - 'a' + 10;
143 else if (c >= 'A' && c <= 'F')
144 val |= c - 'A' + 10;
145 else if (c == ':' || c == 0)
146 val >>= 4;
147 else {
148#ifdef DEBUG
149 fprintf(stderr,
150 _("in_ether(%s): invalid ether address!\n"),
151 orig);
152#endif
153 errno = EINVAL;
154 return (-1);
155 }
156 if (c != 0)
157 bufp++;
158 *ptr++ = (unsigned char) (val & 0377);
159 i++;
160
161 /* We might get a semicolon here - not required. */
162 if (*bufp == ':')
163 bufp++;
164
165 }
166
167 if(i != ETH_ALEN) {
168 errno = EINVAL;
169 return -1;
170 }
171
172 return 0;
173}
174
175int ifconfig_main(int argc, char **argv)
176{
177 struct ifreq ifr;
178 struct sockaddr_in sa;
179 struct sockaddr sa2;
180 char **spp;
181 int goterr = 0;
182 int r, didnetmask = 0;
183 char host[128];
184
185 if(argc < 2) {
186 usage(ifconfig_usage);
187 }
188
189 /* Create a channel to the NET kernel. */
190 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
191 perror("socket");
192 exit(1);
193 }
194
195 /* skip argv[0] */
196
197 argc--;
198 argv++;
199
200 spp = argv;
201
202 /* get interface name */
203
204 safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
205
206 /* Process the remaining arguments. */
207 while (*spp != (char *) NULL) {
208 if (!strcmp(*spp, "arp")) {
209 goterr |= clr_flag(ifr.ifr_name, IFF_NOARP);
210 spp++;
211 continue;
212 }
213 if (!strcmp(*spp, "-arp")) {
214 goterr |= set_flag(ifr.ifr_name, IFF_NOARP);
215 spp++;
216 continue;
217 }
218
219 if (!strcmp(*spp, "trailers")) {
220 goterr |= clr_flag(ifr.ifr_name, IFF_NOTRAILERS);
221 spp++;
222 continue;
223 }
224 if (!strcmp(*spp, "-trailers")) {
225 goterr |= set_flag(ifr.ifr_name, IFF_NOTRAILERS);
226 spp++;
227 continue;
228 }
229 if (!strcmp(*spp, "promisc")) {
230 goterr |= set_flag(ifr.ifr_name, IFF_PROMISC);
231 spp++;
232 continue;
233 }
234 if (!strcmp(*spp, "-promisc")) {
235 goterr |= clr_flag(ifr.ifr_name, IFF_PROMISC);
236 spp++;
237 continue;
238 }
239 if (!strcmp(*spp, "multicast")) {
240 goterr |= set_flag(ifr.ifr_name, IFF_MULTICAST);
241 spp++;
242 continue;
243 }
244 if (!strcmp(*spp, "-multicast")) {
245 goterr |= clr_flag(ifr.ifr_name, IFF_MULTICAST);
246 spp++;
247 continue;
248 }
249 if (!strcmp(*spp, "allmulti")) {
250 goterr |= set_flag(ifr.ifr_name, IFF_ALLMULTI);
251 spp++;
252 continue;
253 }
254 if (!strcmp(*spp, "-allmulti")) {
255 goterr |= clr_flag(ifr.ifr_name, IFF_ALLMULTI);
256 spp++;
257 continue;
258 }
259 if (!strcmp(*spp, "up")) {
260 goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));
261 spp++;
262 continue;
263 }
264 if (!strcmp(*spp, "down")) {
265 goterr |= clr_flag(ifr.ifr_name, IFF_UP);
266 spp++;
267 continue;
268 }
269
270 if (!strcmp(*spp, "metric")) {
271 if (*++spp == NULL)
272 usage(ifconfig_usage);
273 ifr.ifr_metric = atoi(*spp);
274 if (ioctl(sockfd, SIOCSIFMETRIC, &ifr) < 0) {
275 fprintf(stderr, "SIOCSIFMETRIC: %s\n", strerror(errno));
276 goterr++;
277 }
278 spp++;
279 continue;
280 }
281 if (!strcmp(*spp, "mtu")) {
282 if (*++spp == NULL)
283 usage(ifconfig_usage);
284 ifr.ifr_mtu = atoi(*spp);
285 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
286 fprintf(stderr, "SIOCSIFMTU: %s\n", strerror(errno));
287 goterr++;
288 }
289 spp++;
290 continue;
291 }
292#ifdef SIOCSKEEPALIVE
293 if (!strcmp(*spp, "keepalive")) {
294 if (*++spp == NULL)
295 usage(ifconfig_usage);
296 ifr.ifr_data = (caddr_t) atoi(*spp);
297 if (ioctl(sockfd, SIOCSKEEPALIVE, &ifr) < 0) {
298 fprintf(stderr, "SIOCSKEEPALIVE: %s\n", strerror(errno));
299 goterr++;
300 }
301 spp++;
302 continue;
303 }
304#endif
305
306#ifdef SIOCSOUTFILL
307 if (!strcmp(*spp, "outfill")) {
308 if (*++spp == NULL)
309 usage(ifconfig_usage);
310 ifr.ifr_data = (caddr_t) atoi(*spp);
311 if (ioctl(sockfd, SIOCSOUTFILL, &ifr) < 0) {
312 fprintf(stderr, "SIOCSOUTFILL: %s\n", strerror(errno));
313 goterr++;
314 }
315 spp++;
316 continue;
317 }
318#endif
319
320 if (!strcmp(*spp, "-broadcast")) {
321 goterr |= clr_flag(ifr.ifr_name, IFF_BROADCAST);
322 spp++;
323 continue;
324 }
325 if (!strcmp(*spp, "broadcast")) {
326 if (*++spp != NULL) {
327 safe_strncpy(host, *spp, (sizeof host));
328 if (INET_resolve(host, &sa) < 0) {
329 goterr++;
330 spp++;
331 continue;
332 }
333 memcpy((char *) &ifr.ifr_broadaddr,
334 (char *) &sa,
335 sizeof(struct sockaddr));
336 if (ioctl(sockfd, SIOCSIFBRDADDR, &ifr) < 0) {
337 perror("SIOCSIFBRDADDR");
338 goterr++;
339 }
340 spp++;
341 }
342 goterr |= set_flag(ifr.ifr_name, IFF_BROADCAST);
343 continue;
344 }
345 if (!strcmp(*spp, "dstaddr")) {
346 if (*++spp == NULL)
347 usage(ifconfig_usage);
348 safe_strncpy(host, *spp, (sizeof host));
349 if (INET_resolve(host, &sa) < 0) {
350 goterr++;
351 spp++;
352 continue;
353 }
354 memcpy((char *) &ifr.ifr_dstaddr, (char *) &sa,
355 sizeof(struct sockaddr));
356 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
357 fprintf(stderr, "SIOCSIFDSTADDR: %s\n",
358 strerror(errno));
359 goterr++;
360 }
361 spp++;
362 continue;
363 }
364 if (!strcmp(*spp, "netmask")) {
365 if (*++spp == NULL || didnetmask)
366 usage(ifconfig_usage);
367 safe_strncpy(host, *spp, (sizeof host));
368 if (INET_resolve(host, &sa) < 0) {
369 goterr++;
370 spp++;
371 continue;
372 }
373 didnetmask++;
374 memcpy((char *) &ifr.ifr_netmask, (char *) &sa,
375 sizeof(struct sockaddr));
376 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
377 perror("SIOCSIFNETMASK");
378 goterr++;
379 }
380 spp++;
381 continue;
382 }
383
384 if (!strcmp(*spp, "-pointopoint")) {
385 goterr |= clr_flag(ifr.ifr_name, IFF_POINTOPOINT);
386 spp++;
387 continue;
388 }
389 if (!strcmp(*spp, "pointopoint")) {
390 if (*(spp + 1) != NULL) {
391 spp++;
392 safe_strncpy(host, *spp, (sizeof host));
393 if (INET_resolve(host, &sa)) {
394 goterr++;
395 spp++;
396 continue;
397 }
398 memcpy((char *) &ifr.ifr_dstaddr, (char *) &sa,
399 sizeof(struct sockaddr));
400 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
401 perror("SIOCSIFDSTADDR");
402 goterr++;
403 }
404 }
405 goterr |= set_flag(ifr.ifr_name, IFF_POINTOPOINT);
406 spp++;
407 continue;
408 };
409
410 if (!strcmp(*spp, "hw")) {
411 if (*++spp == NULL || strcmp("ether", *spp)) {
412 usage(ifconfig_usage);
413 }
414
415 if (*++spp == NULL) {
416 /* silently ignore it if no address */
417 continue;
418 }
419
420 safe_strncpy(host, *spp, (sizeof host));
421 if (in_ether(host, &sa2) < 0) {
422 fprintf(stderr, "invalid hw-addr %s\n", host);
423 goterr++;
424 spp++;
425 continue;
426 }
427 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sa2,
428 sizeof(struct sockaddr));
429 if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) {
430 perror("SIOCSIFHWADDR");
431 goterr++;
432 }
433 spp++;
434 continue;
435 }
436
437 /* If the next argument is a valid hostname, assume OK. */
438 safe_strncpy(host, *spp, (sizeof host));
439
440 if (INET_resolve(host, &sa) < 0) {
441 usage(ifconfig_usage);
442 }
443 memcpy((char *) &ifr.ifr_addr,
444 (char *) &sa, sizeof(struct sockaddr));
445
446 r = ioctl(sockfd, SIOCSIFADDR, &ifr);
447
448 if (r < 0) {
449 perror("SIOCSIFADDR");
450 goterr++;
451 }
452
453 /*
454 * Don't do the set_flag() if the address is an alias with a - at the
455 * end, since it's deleted already! - Roman
456 *
457 * Should really use regex.h here, not sure though how well it'll go
458 * with the cross-platform support etc.
459 */
460 {
461 char *ptr;
462 short int found_colon = 0;
463 for (ptr = ifr.ifr_name; *ptr; ptr++ )
464 if (*ptr == ':') found_colon++;
465
466 if (!(found_colon && *(ptr - 1) == '-'))
467 goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));
468 }
469
470 spp++;
471
472 } /* end of while-loop */
473
474 exit(0);
475}
476
diff --git a/include/applets.h b/include/applets.h
index 4bd8b79e4..9aa65dac0 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -158,6 +158,9 @@ const struct BB_applet applets[] = {
158#ifdef BB_ID 158#ifdef BB_ID
159 APPLET("id", id_main, _BB_DIR_USR_BIN, id_usage) 159 APPLET("id", id_main, _BB_DIR_USR_BIN, id_usage)
160#endif 160#endif
161#ifdef BB_IFCONFIG
162 APPLET("ifconfig", ifconfig_main, _BB_DIR_SBIN, ifconfig_usage)
163#endif
161#ifdef BB_INIT 164#ifdef BB_INIT
162 APPLET_NOUSAGE("init", init_main, _BB_DIR_SBIN) 165 APPLET_NOUSAGE("init", init_main, _BB_DIR_SBIN)
163#endif 166#endif
@@ -284,6 +287,9 @@ const struct BB_applet applets[] = {
284#ifdef BB_RMMOD 287#ifdef BB_RMMOD
285 APPLET("rmmod", rmmod_main, _BB_DIR_SBIN, rmmod_usage) 288 APPLET("rmmod", rmmod_main, _BB_DIR_SBIN, rmmod_usage)
286#endif 289#endif
290#ifdef BB_ROUTE
291 APPLET("route", route_main, _BB_DIR_USR_BIN, route_usage)
292#endif
287#ifdef BB_RPMUNPACK 293#ifdef BB_RPMUNPACK
288 APPLET("rpmunpack", rpmunpack_main, _BB_DIR_USR_BIN, rpmunpack_usage) 294 APPLET("rpmunpack", rpmunpack_main, _BB_DIR_USR_BIN, rpmunpack_usage)
289#endif 295#endif
diff --git a/include/busybox.h b/include/busybox.h
index 101e65989..39580b548 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -161,6 +161,7 @@ extern void *xcalloc(size_t nmemb, size_t size);
161extern char *xstrdup (const char *s); 161extern char *xstrdup (const char *s);
162#endif 162#endif
163extern char *xstrndup (const char *s, int n); 163extern char *xstrndup (const char *s, int n);
164extern char * safe_strncpy(char *dst, const char *src, size_t size);
164 165
165struct suffix_mult { 166struct suffix_mult {
166 char *suffix; 167 char *suffix;
diff --git a/networking/ifconfig.c b/networking/ifconfig.c
new file mode 100644
index 000000000..2134f8a48
--- /dev/null
+++ b/networking/ifconfig.c
@@ -0,0 +1,476 @@
1/* ifconfig
2 *
3 * Similar to the standard Unix ifconfig, but with only the necessary
4 * parts for AF_INET, and without any printing of if info (for now).
5 *
6 * Bjorn Wesen, Axis Communications AB
7 *
8 *
9 * Authors of the original ifconfig was:
10 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * $Id: ifconfig.c,v 1.1 2001/02/14 08:11:27 andersen Exp $
19 *
20 */
21
22#include "busybox.h"
23#include <sys/types.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h> // strcmp and friends
28#include <ctype.h> // isdigit and friends
29#include <sys/socket.h>
30#include <sys/ioctl.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <net/if.h>
34#include <net/if_arp.h>
35#include <linux/if_ether.h>
36
37static int sockfd; /* socket fd we use to manipulate stuff with */
38
39/* print usage and exit */
40
41#define _(x) x
42
43/* Set a certain interface flag. */
44static int
45set_flag(char *ifname, short flag)
46{
47 struct ifreq ifr;
48
49 strcpy(ifr.ifr_name, ifname);
50 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
51 perror("SIOCGIFFLAGS");
52 return (-1);
53 }
54 strcpy(ifr.ifr_name, ifname);
55 ifr.ifr_flags |= flag;
56 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
57 perror("SIOCSIFFLAGS");
58 return -1;
59 }
60 return (0);
61}
62
63
64/* Clear a certain interface flag. */
65static int
66clr_flag(char *ifname, short flag)
67{
68 struct ifreq ifr;
69
70 strcpy(ifr.ifr_name, ifname);
71 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
72 perror("SIOCGIFFLAGS");
73 return -1;
74 }
75 strcpy(ifr.ifr_name, ifname);
76 ifr.ifr_flags &= ~flag;
77 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
78 perror("SIOCSIFFLAGS");
79 return -1;
80 }
81 return (0);
82}
83
84/* resolve XXX.YYY.ZZZ.QQQ -> binary */
85
86static int
87INET_resolve(char *name, struct sockaddr_in *sin)
88{
89 sin->sin_family = AF_INET;
90 sin->sin_port = 0;
91
92 /* Default is special, meaning 0.0.0.0. */
93 if (!strcmp(name, "default")) {
94 sin->sin_addr.s_addr = INADDR_ANY;
95 return (1);
96 }
97 /* Look to see if it's a dotted quad. */
98 if (inet_aton(name, &sin->sin_addr)) {
99 return 0;
100 }
101 /* guess not.. */
102 return -1;
103}
104
105/* Input an Ethernet address and convert to binary. */
106static int
107in_ether(char *bufp, struct sockaddr *sap)
108{
109 unsigned char *ptr;
110 char c, *orig;
111 int i;
112 unsigned val;
113
114 sap->sa_family = ARPHRD_ETHER;
115 ptr = sap->sa_data;
116
117 i = 0;
118 orig = bufp;
119 while ((*bufp != '\0') && (i < ETH_ALEN)) {
120 val = 0;
121 c = *bufp++;
122 if (isdigit(c))
123 val = c - '0';
124 else if (c >= 'a' && c <= 'f')
125 val = c - 'a' + 10;
126 else if (c >= 'A' && c <= 'F')
127 val = c - 'A' + 10;
128 else {
129#ifdef DEBUG
130 fprintf(stderr,
131 _("in_ether(%s): invalid ether address!\n"),
132 orig);
133#endif
134 errno = EINVAL;
135 return (-1);
136 }
137 val <<= 4;
138 c = *bufp;
139 if (isdigit(c))
140 val |= c - '0';
141 else if (c >= 'a' && c <= 'f')
142 val |= c - 'a' + 10;
143 else if (c >= 'A' && c <= 'F')
144 val |= c - 'A' + 10;
145 else if (c == ':' || c == 0)
146 val >>= 4;
147 else {
148#ifdef DEBUG
149 fprintf(stderr,
150 _("in_ether(%s): invalid ether address!\n"),
151 orig);
152#endif
153 errno = EINVAL;
154 return (-1);
155 }
156 if (c != 0)
157 bufp++;
158 *ptr++ = (unsigned char) (val & 0377);
159 i++;
160
161 /* We might get a semicolon here - not required. */
162 if (*bufp == ':')
163 bufp++;
164
165 }
166
167 if(i != ETH_ALEN) {
168 errno = EINVAL;
169 return -1;
170 }
171
172 return 0;
173}
174
175int ifconfig_main(int argc, char **argv)
176{
177 struct ifreq ifr;
178 struct sockaddr_in sa;
179 struct sockaddr sa2;
180 char **spp;
181 int goterr = 0;
182 int r, didnetmask = 0;
183 char host[128];
184
185 if(argc < 2) {
186 usage(ifconfig_usage);
187 }
188
189 /* Create a channel to the NET kernel. */
190 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
191 perror("socket");
192 exit(1);
193 }
194
195 /* skip argv[0] */
196
197 argc--;
198 argv++;
199
200 spp = argv;
201
202 /* get interface name */
203
204 safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
205
206 /* Process the remaining arguments. */
207 while (*spp != (char *) NULL) {
208 if (!strcmp(*spp, "arp")) {
209 goterr |= clr_flag(ifr.ifr_name, IFF_NOARP);
210 spp++;
211 continue;
212 }
213 if (!strcmp(*spp, "-arp")) {
214 goterr |= set_flag(ifr.ifr_name, IFF_NOARP);
215 spp++;
216 continue;
217 }
218
219 if (!strcmp(*spp, "trailers")) {
220 goterr |= clr_flag(ifr.ifr_name, IFF_NOTRAILERS);
221 spp++;
222 continue;
223 }
224 if (!strcmp(*spp, "-trailers")) {
225 goterr |= set_flag(ifr.ifr_name, IFF_NOTRAILERS);
226 spp++;
227 continue;
228 }
229 if (!strcmp(*spp, "promisc")) {
230 goterr |= set_flag(ifr.ifr_name, IFF_PROMISC);
231 spp++;
232 continue;
233 }
234 if (!strcmp(*spp, "-promisc")) {
235 goterr |= clr_flag(ifr.ifr_name, IFF_PROMISC);
236 spp++;
237 continue;
238 }
239 if (!strcmp(*spp, "multicast")) {
240 goterr |= set_flag(ifr.ifr_name, IFF_MULTICAST);
241 spp++;
242 continue;
243 }
244 if (!strcmp(*spp, "-multicast")) {
245 goterr |= clr_flag(ifr.ifr_name, IFF_MULTICAST);
246 spp++;
247 continue;
248 }
249 if (!strcmp(*spp, "allmulti")) {
250 goterr |= set_flag(ifr.ifr_name, IFF_ALLMULTI);
251 spp++;
252 continue;
253 }
254 if (!strcmp(*spp, "-allmulti")) {
255 goterr |= clr_flag(ifr.ifr_name, IFF_ALLMULTI);
256 spp++;
257 continue;
258 }
259 if (!strcmp(*spp, "up")) {
260 goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));
261 spp++;
262 continue;
263 }
264 if (!strcmp(*spp, "down")) {
265 goterr |= clr_flag(ifr.ifr_name, IFF_UP);
266 spp++;
267 continue;
268 }
269
270 if (!strcmp(*spp, "metric")) {
271 if (*++spp == NULL)
272 usage(ifconfig_usage);
273 ifr.ifr_metric = atoi(*spp);
274 if (ioctl(sockfd, SIOCSIFMETRIC, &ifr) < 0) {
275 fprintf(stderr, "SIOCSIFMETRIC: %s\n", strerror(errno));
276 goterr++;
277 }
278 spp++;
279 continue;
280 }
281 if (!strcmp(*spp, "mtu")) {
282 if (*++spp == NULL)
283 usage(ifconfig_usage);
284 ifr.ifr_mtu = atoi(*spp);
285 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
286 fprintf(stderr, "SIOCSIFMTU: %s\n", strerror(errno));
287 goterr++;
288 }
289 spp++;
290 continue;
291 }
292#ifdef SIOCSKEEPALIVE
293 if (!strcmp(*spp, "keepalive")) {
294 if (*++spp == NULL)
295 usage(ifconfig_usage);
296 ifr.ifr_data = (caddr_t) atoi(*spp);
297 if (ioctl(sockfd, SIOCSKEEPALIVE, &ifr) < 0) {
298 fprintf(stderr, "SIOCSKEEPALIVE: %s\n", strerror(errno));
299 goterr++;
300 }
301 spp++;
302 continue;
303 }
304#endif
305
306#ifdef SIOCSOUTFILL
307 if (!strcmp(*spp, "outfill")) {
308 if (*++spp == NULL)
309 usage(ifconfig_usage);
310 ifr.ifr_data = (caddr_t) atoi(*spp);
311 if (ioctl(sockfd, SIOCSOUTFILL, &ifr) < 0) {
312 fprintf(stderr, "SIOCSOUTFILL: %s\n", strerror(errno));
313 goterr++;
314 }
315 spp++;
316 continue;
317 }
318#endif
319
320 if (!strcmp(*spp, "-broadcast")) {
321 goterr |= clr_flag(ifr.ifr_name, IFF_BROADCAST);
322 spp++;
323 continue;
324 }
325 if (!strcmp(*spp, "broadcast")) {
326 if (*++spp != NULL) {
327 safe_strncpy(host, *spp, (sizeof host));
328 if (INET_resolve(host, &sa) < 0) {
329 goterr++;
330 spp++;
331 continue;
332 }
333 memcpy((char *) &ifr.ifr_broadaddr,
334 (char *) &sa,
335 sizeof(struct sockaddr));
336 if (ioctl(sockfd, SIOCSIFBRDADDR, &ifr) < 0) {
337 perror("SIOCSIFBRDADDR");
338 goterr++;
339 }
340 spp++;
341 }
342 goterr |= set_flag(ifr.ifr_name, IFF_BROADCAST);
343 continue;
344 }
345 if (!strcmp(*spp, "dstaddr")) {
346 if (*++spp == NULL)
347 usage(ifconfig_usage);
348 safe_strncpy(host, *spp, (sizeof host));
349 if (INET_resolve(host, &sa) < 0) {
350 goterr++;
351 spp++;
352 continue;
353 }
354 memcpy((char *) &ifr.ifr_dstaddr, (char *) &sa,
355 sizeof(struct sockaddr));
356 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
357 fprintf(stderr, "SIOCSIFDSTADDR: %s\n",
358 strerror(errno));
359 goterr++;
360 }
361 spp++;
362 continue;
363 }
364 if (!strcmp(*spp, "netmask")) {
365 if (*++spp == NULL || didnetmask)
366 usage(ifconfig_usage);
367 safe_strncpy(host, *spp, (sizeof host));
368 if (INET_resolve(host, &sa) < 0) {
369 goterr++;
370 spp++;
371 continue;
372 }
373 didnetmask++;
374 memcpy((char *) &ifr.ifr_netmask, (char *) &sa,
375 sizeof(struct sockaddr));
376 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
377 perror("SIOCSIFNETMASK");
378 goterr++;
379 }
380 spp++;
381 continue;
382 }
383
384 if (!strcmp(*spp, "-pointopoint")) {
385 goterr |= clr_flag(ifr.ifr_name, IFF_POINTOPOINT);
386 spp++;
387 continue;
388 }
389 if (!strcmp(*spp, "pointopoint")) {
390 if (*(spp + 1) != NULL) {
391 spp++;
392 safe_strncpy(host, *spp, (sizeof host));
393 if (INET_resolve(host, &sa)) {
394 goterr++;
395 spp++;
396 continue;
397 }
398 memcpy((char *) &ifr.ifr_dstaddr, (char *) &sa,
399 sizeof(struct sockaddr));
400 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
401 perror("SIOCSIFDSTADDR");
402 goterr++;
403 }
404 }
405 goterr |= set_flag(ifr.ifr_name, IFF_POINTOPOINT);
406 spp++;
407 continue;
408 };
409
410 if (!strcmp(*spp, "hw")) {
411 if (*++spp == NULL || strcmp("ether", *spp)) {
412 usage(ifconfig_usage);
413 }
414
415 if (*++spp == NULL) {
416 /* silently ignore it if no address */
417 continue;
418 }
419
420 safe_strncpy(host, *spp, (sizeof host));
421 if (in_ether(host, &sa2) < 0) {
422 fprintf(stderr, "invalid hw-addr %s\n", host);
423 goterr++;
424 spp++;
425 continue;
426 }
427 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sa2,
428 sizeof(struct sockaddr));
429 if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) {
430 perror("SIOCSIFHWADDR");
431 goterr++;
432 }
433 spp++;
434 continue;
435 }
436
437 /* If the next argument is a valid hostname, assume OK. */
438 safe_strncpy(host, *spp, (sizeof host));
439
440 if (INET_resolve(host, &sa) < 0) {
441 usage(ifconfig_usage);
442 }
443 memcpy((char *) &ifr.ifr_addr,
444 (char *) &sa, sizeof(struct sockaddr));
445
446 r = ioctl(sockfd, SIOCSIFADDR, &ifr);
447
448 if (r < 0) {
449 perror("SIOCSIFADDR");
450 goterr++;
451 }
452
453 /*
454 * Don't do the set_flag() if the address is an alias with a - at the
455 * end, since it's deleted already! - Roman
456 *
457 * Should really use regex.h here, not sure though how well it'll go
458 * with the cross-platform support etc.
459 */
460 {
461 char *ptr;
462 short int found_colon = 0;
463 for (ptr = ifr.ifr_name; *ptr; ptr++ )
464 if (*ptr == ':') found_colon++;
465
466 if (!(found_colon && *(ptr - 1) == '-'))
467 goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));
468 }
469
470 spp++;
471
472 } /* end of while-loop */
473
474 exit(0);
475}
476
diff --git a/networking/route.c b/networking/route.c
new file mode 100644
index 000000000..76b2306fd
--- /dev/null
+++ b/networking/route.c
@@ -0,0 +1,372 @@
1/* route
2 *
3 * Similar to the standard Unix route, but with only the necessary
4 * parts for AF_INET
5 *
6 * Bjorn Wesen, Axis Communications AB
7 *
8 * Author of the original route:
9 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10 * (derived from FvK's 'route.c 1.70 01/04/94')
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * $Id: route.c,v 1.1 2001/02/14 08:11:27 andersen Exp $
19 *
20 */
21
22#include "busybox.h"
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <net/route.h>
27#include <linux/param.h> // HZ
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <stdio.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <stdlib.h>
34#include <string.h>
35#include <getopt.h>
36#include <unistd.h>
37#include <ctype.h>
38
39#define _(x) x
40
41#define RTACTION_ADD 1
42#define RTACTION_DEL 2
43#define RTACTION_HELP 3
44#define RTACTION_FLUSH 4
45#define RTACTION_SHOW 5
46
47#define E_NOTFOUND 8
48#define E_SOCK 7
49#define E_LOOKUP 6
50#define E_VERSION 5
51#define E_USAGE 4
52#define E_OPTERR 3
53#define E_INTERN 2
54#define E_NOSUPP 1
55
56/* resolve XXX.YYY.ZZZ.QQQ -> binary */
57
58static int
59INET_resolve(char *name, struct sockaddr *sa)
60{
61 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
62
63 sin->sin_family = AF_INET;
64 sin->sin_port = 0;
65
66 /* Default is special, meaning 0.0.0.0. */
67 if (!strcmp(name, "default")) {
68 sin->sin_addr.s_addr = INADDR_ANY;
69 return (1);
70 }
71 /* Look to see if it's a dotted quad. */
72 if (inet_aton(name, &sin->sin_addr)) {
73 return 0;
74 }
75 /* guess not.. */
76 return -1;
77}
78
79#if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */
80#define HAVE_NEW_ADDRT 1
81#endif
82#ifdef RTF_IRTT /* route */
83#define HAVE_RTF_IRTT 1
84#endif
85#ifdef RTF_REJECT /* route */
86#define HAVE_RTF_REJECT 1
87#endif
88
89#if HAVE_NEW_ADDRT
90#define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr)
91#define full_mask(x) (x)
92#else
93#define mask_in_addr(x) ((x).rt_genmask)
94#define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr)
95#endif
96
97/* add or delete a route depending on action */
98
99static int
100INET_setroute(int action, int options, char **args)
101{
102 struct rtentry rt;
103 char target[128], gateway[128] = "NONE", netmask[128] = "default";
104 int xflag, isnet;
105 int skfd;
106
107 xflag = 0;
108
109 if (!strcmp(*args, "-net")) {
110 xflag = 1;
111 args++;
112 } else if (!strcmp(*args, "-host")) {
113 xflag = 2;
114 args++;
115 }
116 if (*args == NULL)
117 usage(route_usage);
118
119 safe_strncpy(target, *args++, (sizeof target));
120
121 /* Clean out the RTREQ structure. */
122 memset((char *) &rt, 0, sizeof(struct rtentry));
123
124
125 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
126 fprintf(stderr, "cant resolve %s\n", target);
127 return (1);
128 }
129
130 switch (xflag) {
131 case 1:
132 isnet = 1;
133 break;
134
135 case 2:
136 isnet = 0;
137 break;
138
139 default:
140 break;
141 }
142
143 /* Fill in the other fields. */
144 rt.rt_flags = (RTF_UP | RTF_HOST);
145 if (isnet)
146 rt.rt_flags &= ~RTF_HOST;
147
148 while (*args) {
149 if (!strcmp(*args, "metric")) {
150 int metric;
151
152 args++;
153 if (!*args || !isdigit(**args))
154 usage(route_usage);
155 metric = atoi(*args);
156#if HAVE_NEW_ADDRT
157 rt.rt_metric = metric + 1;
158#else
159 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)");
160#endif
161 args++;
162 continue;
163 }
164
165 if (!strcmp(*args, "netmask")) {
166 struct sockaddr mask;
167
168 args++;
169 if (!*args || mask_in_addr(rt))
170 usage(route_usage);
171 safe_strncpy(netmask, *args, (sizeof netmask));
172 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
173 fprintf(stderr, "cant resolve netmask %s\n", netmask);
174 return (E_LOOKUP);
175 }
176 rt.rt_genmask = full_mask(mask);
177 args++;
178 continue;
179 }
180
181 if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) {
182 args++;
183 if (!*args)
184 usage(route_usage);
185 if (rt.rt_flags & RTF_GATEWAY)
186 usage(route_usage);
187 safe_strncpy(gateway, *args, (sizeof gateway));
188 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
189 fprintf(stderr, "cant resolve gw %s\n", gateway);
190 return (E_LOOKUP);
191 }
192 if (isnet) {
193 fprintf(stderr,
194 _("route: %s: cannot use a NETWORK as gateway!\n"),
195 gateway);
196 return (E_OPTERR);
197 }
198 rt.rt_flags |= RTF_GATEWAY;
199 args++;
200 continue;
201 }
202
203 if (!strcmp(*args, "mss")) {
204 args++;
205 rt.rt_flags |= RTF_MSS;
206 if (!*args)
207 usage(route_usage);
208 rt.rt_mss = atoi(*args);
209 args++;
210 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
211 fprintf(stderr, _("route: Invalid MSS.\n"));
212 return (E_OPTERR);
213 }
214 continue;
215 }
216
217 if (!strcmp(*args, "window")) {
218 args++;
219 if (!*args)
220 usage(route_usage);
221 rt.rt_flags |= RTF_WINDOW;
222 rt.rt_window = atoi(*args);
223 args++;
224 if (rt.rt_window < 128) {
225 fprintf(stderr, _("route: Invalid window.\n"));
226 return (E_OPTERR);
227 }
228 continue;
229 }
230
231 if (!strcmp(*args, "irtt")) {
232 args++;
233 if (!*args)
234 usage(route_usage);
235 args++;
236#if HAVE_RTF_IRTT
237 rt.rt_flags |= RTF_IRTT;
238 rt.rt_irtt = atoi(*(args - 1));
239 rt.rt_irtt *= (HZ / 100); /* FIXME */
240#if 0 /* FIXME: do we need to check anything of this? */
241 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
242 fprintf(stderr, _("route: Invalid initial rtt.\n"));
243 return (E_OPTERR);
244 }
245#endif
246#else
247 ENOSUPP("inet_setroute", "RTF_IRTT");
248#endif
249 continue;
250 }
251
252 if (!strcmp(*args, "reject")) {
253 args++;
254#if HAVE_RTF_REJECT
255 rt.rt_flags |= RTF_REJECT;
256#else
257 ENOSUPP("inet_setroute", "RTF_REJECT");
258#endif
259 continue;
260 }
261 if (!strcmp(*args, "mod")) {
262 args++;
263 rt.rt_flags |= RTF_MODIFIED;
264 continue;
265 }
266 if (!strcmp(*args, "dyn")) {
267 args++;
268 rt.rt_flags |= RTF_DYNAMIC;
269 continue;
270 }
271 if (!strcmp(*args, "reinstate")) {
272 args++;
273 rt.rt_flags |= RTF_REINSTATE;
274 continue;
275 }
276 if (!strcmp(*args, "device") || !strcmp(*args, "dev")) {
277 args++;
278 if (rt.rt_dev || *args == NULL)
279 usage(route_usage);
280 rt.rt_dev = *args++;
281 continue;
282 }
283 /* nothing matches */
284 if (!rt.rt_dev) {
285 rt.rt_dev = *args++;
286 if (*args)
287 usage(route_usage); /* must be last to catch typos */
288 } else
289 usage(route_usage);
290 }
291
292#if HAVE_RTF_REJECT
293 if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev)
294 rt.rt_dev = "lo";
295#endif
296
297 /* sanity checks.. */
298 if (mask_in_addr(rt)) {
299 unsigned long mask = mask_in_addr(rt);
300 mask = ~ntohl(mask);
301 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
302 fprintf(stderr,
303 _("route: netmask %.8x doesn't make sense with host route\n"),
304 (unsigned int)mask);
305 return (E_OPTERR);
306 }
307 if (mask & (mask + 1)) {
308 fprintf(stderr, _("route: bogus netmask %s\n"), netmask);
309 return (E_OPTERR);
310 }
311 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
312 if (mask & ~mask_in_addr(rt)) {
313 fprintf(stderr, _("route: netmask doesn't match route address\n"));
314 return (E_OPTERR);
315 }
316 }
317 /* Fill out netmask if still unset */
318 if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST)
319 mask_in_addr(rt) = 0xffffffff;
320
321 /* Create a socket to the INET kernel. */
322 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
323 perror("socket");
324 return (E_SOCK);
325 }
326 /* Tell the kernel to accept this route. */
327 if (action == RTACTION_DEL) {
328 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
329 perror("SIOCDELRT");
330 close(skfd);
331 return (E_SOCK);
332 }
333 } else {
334 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
335 perror("SIOCADDRT");
336 close(skfd);
337 return (E_SOCK);
338 }
339 }
340
341 /* Close the socket. */
342 (void) close(skfd);
343 return (0);
344}
345
346int route_main(int argc, char **argv)
347{
348 int what = 0;
349
350 argc--;
351 argv++;
352
353 if (*argv == NULL) {
354 //displayroutes();
355 fprintf(stderr, "print routes is not implemented yet\n");
356 usage(route_usage);
357 } else {
358 /* check verb */
359 if (!strcmp(*argv, "add"))
360 what = RTACTION_ADD;
361 else if (!strcmp(*argv, "del") || !strcmp(*argv, "delete"))
362 what = RTACTION_DEL;
363 else if (!strcmp(*argv, "flush"))
364 what = RTACTION_FLUSH;
365 else
366 usage(route_usage);
367 }
368
369 INET_setroute(what, 0, ++argv);
370
371 exit(0);
372}
diff --git a/route.c b/route.c
new file mode 100644
index 000000000..76b2306fd
--- /dev/null
+++ b/route.c
@@ -0,0 +1,372 @@
1/* route
2 *
3 * Similar to the standard Unix route, but with only the necessary
4 * parts for AF_INET
5 *
6 * Bjorn Wesen, Axis Communications AB
7 *
8 * Author of the original route:
9 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10 * (derived from FvK's 'route.c 1.70 01/04/94')
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * $Id: route.c,v 1.1 2001/02/14 08:11:27 andersen Exp $
19 *
20 */
21
22#include "busybox.h"
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <net/route.h>
27#include <linux/param.h> // HZ
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <stdio.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <stdlib.h>
34#include <string.h>
35#include <getopt.h>
36#include <unistd.h>
37#include <ctype.h>
38
39#define _(x) x
40
41#define RTACTION_ADD 1
42#define RTACTION_DEL 2
43#define RTACTION_HELP 3
44#define RTACTION_FLUSH 4
45#define RTACTION_SHOW 5
46
47#define E_NOTFOUND 8
48#define E_SOCK 7
49#define E_LOOKUP 6
50#define E_VERSION 5
51#define E_USAGE 4
52#define E_OPTERR 3
53#define E_INTERN 2
54#define E_NOSUPP 1
55
56/* resolve XXX.YYY.ZZZ.QQQ -> binary */
57
58static int
59INET_resolve(char *name, struct sockaddr *sa)
60{
61 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
62
63 sin->sin_family = AF_INET;
64 sin->sin_port = 0;
65
66 /* Default is special, meaning 0.0.0.0. */
67 if (!strcmp(name, "default")) {
68 sin->sin_addr.s_addr = INADDR_ANY;
69 return (1);
70 }
71 /* Look to see if it's a dotted quad. */
72 if (inet_aton(name, &sin->sin_addr)) {
73 return 0;
74 }
75 /* guess not.. */
76 return -1;
77}
78
79#if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */
80#define HAVE_NEW_ADDRT 1
81#endif
82#ifdef RTF_IRTT /* route */
83#define HAVE_RTF_IRTT 1
84#endif
85#ifdef RTF_REJECT /* route */
86#define HAVE_RTF_REJECT 1
87#endif
88
89#if HAVE_NEW_ADDRT
90#define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr)
91#define full_mask(x) (x)
92#else
93#define mask_in_addr(x) ((x).rt_genmask)
94#define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr)
95#endif
96
97/* add or delete a route depending on action */
98
99static int
100INET_setroute(int action, int options, char **args)
101{
102 struct rtentry rt;
103 char target[128], gateway[128] = "NONE", netmask[128] = "default";
104 int xflag, isnet;
105 int skfd;
106
107 xflag = 0;
108
109 if (!strcmp(*args, "-net")) {
110 xflag = 1;
111 args++;
112 } else if (!strcmp(*args, "-host")) {
113 xflag = 2;
114 args++;
115 }
116 if (*args == NULL)
117 usage(route_usage);
118
119 safe_strncpy(target, *args++, (sizeof target));
120
121 /* Clean out the RTREQ structure. */
122 memset((char *) &rt, 0, sizeof(struct rtentry));
123
124
125 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
126 fprintf(stderr, "cant resolve %s\n", target);
127 return (1);
128 }
129
130 switch (xflag) {
131 case 1:
132 isnet = 1;
133 break;
134
135 case 2:
136 isnet = 0;
137 break;
138
139 default:
140 break;
141 }
142
143 /* Fill in the other fields. */
144 rt.rt_flags = (RTF_UP | RTF_HOST);
145 if (isnet)
146 rt.rt_flags &= ~RTF_HOST;
147
148 while (*args) {
149 if (!strcmp(*args, "metric")) {
150 int metric;
151
152 args++;
153 if (!*args || !isdigit(**args))
154 usage(route_usage);
155 metric = atoi(*args);
156#if HAVE_NEW_ADDRT
157 rt.rt_metric = metric + 1;
158#else
159 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)");
160#endif
161 args++;
162 continue;
163 }
164
165 if (!strcmp(*args, "netmask")) {
166 struct sockaddr mask;
167
168 args++;
169 if (!*args || mask_in_addr(rt))
170 usage(route_usage);
171 safe_strncpy(netmask, *args, (sizeof netmask));
172 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
173 fprintf(stderr, "cant resolve netmask %s\n", netmask);
174 return (E_LOOKUP);
175 }
176 rt.rt_genmask = full_mask(mask);
177 args++;
178 continue;
179 }
180
181 if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) {
182 args++;
183 if (!*args)
184 usage(route_usage);
185 if (rt.rt_flags & RTF_GATEWAY)
186 usage(route_usage);
187 safe_strncpy(gateway, *args, (sizeof gateway));
188 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
189 fprintf(stderr, "cant resolve gw %s\n", gateway);
190 return (E_LOOKUP);
191 }
192 if (isnet) {
193 fprintf(stderr,
194 _("route: %s: cannot use a NETWORK as gateway!\n"),
195 gateway);
196 return (E_OPTERR);
197 }
198 rt.rt_flags |= RTF_GATEWAY;
199 args++;
200 continue;
201 }
202
203 if (!strcmp(*args, "mss")) {
204 args++;
205 rt.rt_flags |= RTF_MSS;
206 if (!*args)
207 usage(route_usage);
208 rt.rt_mss = atoi(*args);
209 args++;
210 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
211 fprintf(stderr, _("route: Invalid MSS.\n"));
212 return (E_OPTERR);
213 }
214 continue;
215 }
216
217 if (!strcmp(*args, "window")) {
218 args++;
219 if (!*args)
220 usage(route_usage);
221 rt.rt_flags |= RTF_WINDOW;
222 rt.rt_window = atoi(*args);
223 args++;
224 if (rt.rt_window < 128) {
225 fprintf(stderr, _("route: Invalid window.\n"));
226 return (E_OPTERR);
227 }
228 continue;
229 }
230
231 if (!strcmp(*args, "irtt")) {
232 args++;
233 if (!*args)
234 usage(route_usage);
235 args++;
236#if HAVE_RTF_IRTT
237 rt.rt_flags |= RTF_IRTT;
238 rt.rt_irtt = atoi(*(args - 1));
239 rt.rt_irtt *= (HZ / 100); /* FIXME */
240#if 0 /* FIXME: do we need to check anything of this? */
241 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
242 fprintf(stderr, _("route: Invalid initial rtt.\n"));
243 return (E_OPTERR);
244 }
245#endif
246#else
247 ENOSUPP("inet_setroute", "RTF_IRTT");
248#endif
249 continue;
250 }
251
252 if (!strcmp(*args, "reject")) {
253 args++;
254#if HAVE_RTF_REJECT
255 rt.rt_flags |= RTF_REJECT;
256#else
257 ENOSUPP("inet_setroute", "RTF_REJECT");
258#endif
259 continue;
260 }
261 if (!strcmp(*args, "mod")) {
262 args++;
263 rt.rt_flags |= RTF_MODIFIED;
264 continue;
265 }
266 if (!strcmp(*args, "dyn")) {
267 args++;
268 rt.rt_flags |= RTF_DYNAMIC;
269 continue;
270 }
271 if (!strcmp(*args, "reinstate")) {
272 args++;
273 rt.rt_flags |= RTF_REINSTATE;
274 continue;
275 }
276 if (!strcmp(*args, "device") || !strcmp(*args, "dev")) {
277 args++;
278 if (rt.rt_dev || *args == NULL)
279 usage(route_usage);
280 rt.rt_dev = *args++;
281 continue;
282 }
283 /* nothing matches */
284 if (!rt.rt_dev) {
285 rt.rt_dev = *args++;
286 if (*args)
287 usage(route_usage); /* must be last to catch typos */
288 } else
289 usage(route_usage);
290 }
291
292#if HAVE_RTF_REJECT
293 if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev)
294 rt.rt_dev = "lo";
295#endif
296
297 /* sanity checks.. */
298 if (mask_in_addr(rt)) {
299 unsigned long mask = mask_in_addr(rt);
300 mask = ~ntohl(mask);
301 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
302 fprintf(stderr,
303 _("route: netmask %.8x doesn't make sense with host route\n"),
304 (unsigned int)mask);
305 return (E_OPTERR);
306 }
307 if (mask & (mask + 1)) {
308 fprintf(stderr, _("route: bogus netmask %s\n"), netmask);
309 return (E_OPTERR);
310 }
311 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
312 if (mask & ~mask_in_addr(rt)) {
313 fprintf(stderr, _("route: netmask doesn't match route address\n"));
314 return (E_OPTERR);
315 }
316 }
317 /* Fill out netmask if still unset */
318 if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST)
319 mask_in_addr(rt) = 0xffffffff;
320
321 /* Create a socket to the INET kernel. */
322 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
323 perror("socket");
324 return (E_SOCK);
325 }
326 /* Tell the kernel to accept this route. */
327 if (action == RTACTION_DEL) {
328 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
329 perror("SIOCDELRT");
330 close(skfd);
331 return (E_SOCK);
332 }
333 } else {
334 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
335 perror("SIOCADDRT");
336 close(skfd);
337 return (E_SOCK);
338 }
339 }
340
341 /* Close the socket. */
342 (void) close(skfd);
343 return (0);
344}
345
346int route_main(int argc, char **argv)
347{
348 int what = 0;
349
350 argc--;
351 argv++;
352
353 if (*argv == NULL) {
354 //displayroutes();
355 fprintf(stderr, "print routes is not implemented yet\n");
356 usage(route_usage);
357 } else {
358 /* check verb */
359 if (!strcmp(*argv, "add"))
360 what = RTACTION_ADD;
361 else if (!strcmp(*argv, "del") || !strcmp(*argv, "delete"))
362 what = RTACTION_DEL;
363 else if (!strcmp(*argv, "flush"))
364 what = RTACTION_FLUSH;
365 else
366 usage(route_usage);
367 }
368
369 INET_setroute(what, 0, ++argv);
370
371 exit(0);
372}
diff --git a/usage.c b/usage.c
index 13107c6e0..bdd4d3d83 100644
--- a/usage.c
+++ b/usage.c
@@ -556,6 +556,26 @@ const char id_usage[] =
556 ; 556 ;
557#endif 557#endif
558 558
559#if defined BB_IFCONFIG
560const char ifconfig_usage[] =
561 "ifconfig [-a] [-i] [-v] <interface> [<address>]"
562#ifndef BB_FEATURE_TRIVIAL_HELP
563 "\n\nconfigure a network interface\n\n"
564 "Options:\n"
565 " [[-]broadcast [<address>]] [[-]pointopoint [<address>]]\n"
566 " [netmask <address>] [dstaddr <address>] [tunnel <adress>]\n"
567#ifdef SIOCSKEEPALIVE
568 " [outfill <NN>] [keepalive <NN>]\n"
569#endif
570 " [hw ether <address>] [metric <NN>] [mtu <NN>]\n"
571 " [[-]trailers] [[-]arp] [[-]allmulti]\n"
572 " [multicast] [[-]promisc]\n"
573 " [mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]\n"
574 " [up|down] ..."
575#endif
576 ;
577#endif
578
559#if defined BB_INSMOD 579#if defined BB_INSMOD
560const char insmod_usage[] = 580const char insmod_usage[] =
561 "insmod [OPTION]... MODULE [symbol=value]..." 581 "insmod [OPTION]... MODULE [symbol=value]..."
@@ -1144,6 +1164,15 @@ const char rmmod_usage[] =
1144 ; 1164 ;
1145#endif 1165#endif
1146 1166
1167#if defined BB_ROUTE
1168const char route_usage[] =
1169 "route [{add|del|flush}]"
1170#ifndef BB_FEATURE_TRIVIAL_HELP
1171 "\n\nEdit the kernel's routing tables"
1172#endif
1173 ;
1174#endif
1175
1147#if defined BB_RPMUNPACK 1176#if defined BB_RPMUNPACK
1148const char rpmunpack_usage[] = 1177const char rpmunpack_usage[] =
1149 "rpmunpack < package.rpm | gunzip | cpio -idmuv" 1178 "rpmunpack < package.rpm | gunzip | cpio -idmuv"
diff --git a/utility.c b/utility.c
index f3c184e73..368dfc5fe 100644
--- a/utility.c
+++ b/utility.c
@@ -1377,6 +1377,14 @@ extern char * xstrndup (const char *s, int n) {
1377} 1377}
1378#endif 1378#endif
1379 1379
1380#if defined BB_IFCONFIG || defined BB_ROUTE
1381/* Like strncpy but make sure the resulting string is always 0 terminated. */
1382extern char * safe_strncpy(char *dst, const char *src, size_t size)
1383{
1384 dst[size-1] = '\0';
1385 return strncpy(dst, src, size-1);
1386}
1387#endif
1380 1388
1381#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) 1389#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1382extern int vdprintf(int d, const char *format, va_list ap) 1390extern int vdprintf(int d, const char *format, va_list ap)