blob: f342ceedefa54033d27789b210971dea6ece0d10 [file] [log] [blame]
Eric Andersenec455952001-02-14 08:11:27 +00001/* 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 *
Manuel Novoa III df351d62001-03-08 22:57:00 +000018 * $Id: ifconfig.c,v 1.5 2001/03/08 22:57:00 mjn3 Exp $
Eric Andersenec455952001-02-14 08:11:27 +000019 *
20 */
21
Manuel Novoa III df351d62001-03-08 22:57:00 +000022/*
23 * Heavily modified by Manuel Novoa III Mar 6, 2001
24 *
25 * From initial port to busybox, removed most of the redundancy by
26 * converting to a table-driven approach. Added several (optional)
27 * args missing from initial port.
28 *
29 * Still missing: media.
30 */
31
Eric Andersenec455952001-02-14 08:11:27 +000032#include <stdio.h>
33#include <stdlib.h>
Eric Andersenec455952001-02-14 08:11:27 +000034#include <string.h> // strcmp and friends
35#include <ctype.h> // isdigit and friends
Manuel Novoa III df351d62001-03-08 22:57:00 +000036#include <stddef.h> /* offsetof */
37#include <sys/types.h>
Eric Andersenec455952001-02-14 08:11:27 +000038#include <sys/socket.h>
39#include <sys/ioctl.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <net/if.h>
43#include <net/if_arp.h>
44#include <linux/if_ether.h>
Manuel Novoa III df351d62001-03-08 22:57:00 +000045#include "busybox.h"
Eric Andersenec455952001-02-14 08:11:27 +000046
Manuel Novoa III df351d62001-03-08 22:57:00 +000047#ifdef BB_FEATURE_IFCONFIG_SLIP
48#include <linux/if_slip.h>
Eric Andersenf15d4da2001-03-06 00:48:59 +000049#endif
50
Manuel Novoa III df351d62001-03-08 22:57:00 +000051/* I don't know if this is needed for busybox or not. Anyone? */
52#define QUESTIONABLE_ALIAS_CASE
Eric Andersenec455952001-02-14 08:11:27 +000053
54
Manuel Novoa III df351d62001-03-08 22:57:00 +000055/* Defines for glibc2.0 users. */
56#ifndef SIOCSIFTXQLEN
57#define SIOCSIFTXQLEN 0x8943
58#define SIOCGIFTXQLEN 0x8942
59#endif
Eric Andersenec455952001-02-14 08:11:27 +000060
Manuel Novoa III df351d62001-03-08 22:57:00 +000061/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
62#ifndef ifr_qlen
63#define ifr_qlen ifr_ifru.ifru_mtu
64#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000065
Manuel Novoa III df351d62001-03-08 22:57:00 +000066#ifndef IFF_DYNAMIC
67#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
68#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000069
Manuel Novoa III df351d62001-03-08 22:57:00 +000070/*
71 * Here are the bit masks for the "flags" member of struct options below.
72 * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
73 * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
Eric Andersenf15d4da2001-03-06 00:48:59 +000074 */
Manuel Novoa III df351d62001-03-08 22:57:00 +000075#define N_CLR 0x01
76#define M_CLR 0x02
77#define N_SET 0x04
78#define M_SET 0x08
79#define N_ARG 0x10
80#define M_ARG 0x20
81
82#define M_MASK (M_CLR | M_SET | M_ARG)
83#define N_MASK (N_CLR | N_SET | N_ARG)
84#define SET_MASK (N_SET | M_SET)
85#define CLR_MASK (N_CLR | M_CLR)
86#define SET_CLR_MASK (SET_MASK | CLR_MASK)
87#define ARG_MASK (M_ARG | N_ARG)
88
89/*
90 * Here are the bit masks for the "arg_flags" member of struct options below.
91 */
92
93/*
94 * cast type:
95 * 00 int
96 * 01 char *
97 * 02 HOST_COPY in_ether
98 * 03 HOST_COPY INET_resolve
99 */
100#define A_CAST_TYPE 0x03
101/*
102 * map type:
103 * 00 not a map type (mem_start, io_addr, irq)
104 * 04 memstart (unsigned long)
105 * 08 io_addr (unsigned short)
106 * 0C irq (unsigned char)
107 */
108#define A_MAP_TYPE 0x0C
109#define A_ARG_REQ 0x10 /* Set if an arg is required. */
110#define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
111#define A_SET_AFTER 0x40 /* Set a flag at the end. */
112#define A_COLON_CHK 0x80 /* Is this needed? See below. */
113
114/*
115 * These defines are for dealing with the A_CAST_TYPE field.
116 */
117#define A_CAST_CHAR_PTR 0x01
118#define A_CAST_RESOLVE 0x01
119#define A_CAST_HOST_COPY 0x02
120#define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
121#define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
122
123/*
124 * These defines are for dealing with the A_MAP_TYPE field.
125 */
126#define A_MAP_ULONG 0x04 /* memstart */
127#define A_MAP_USHORT 0x08 /* io_addr */
128#define A_MAP_UCHAR 0x0C /* irq */
129
130/*
131 * Define the bit masks signifying which operations to perform for each arg.
132 */
133
134#define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
135#define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
136#define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
137#define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
138#define ARG_IO_ADDR (A_ARG_REQ | A_MAP_USHORT)
139#define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
140#define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
141#define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
142#define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
143#define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
144#define ARG_POINTOPOINT (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
145#define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
146#define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
147#define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
148
149
150/*
151 * Set up the tables. Warning! They must have corresponding order!
152 */
153
154struct arg1opt {
155 const char *name;
156 unsigned short selector;
157 unsigned short ifr_offset;
158};
159
160struct options {
161 const char *name;
162 const unsigned char flags;
163 const unsigned char arg_flags;
164 const unsigned short selector;
165};
166
167#define ifreq_offsetof(x) offsetof(struct ifreq, x)
168
169static const struct arg1opt Arg1Opt[] = {
170 {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
171 {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
172 {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
173 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
174 {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
175 {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
176#ifdef BB_FEATURE_IFCONFIG_HW
177 {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
178#endif
179 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000180#ifdef SIOCSKEEPALIVE
Manuel Novoa III df351d62001-03-08 22:57:00 +0000181 {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000182#endif
183#ifdef SIOCSOUTFILL
Manuel Novoa III df351d62001-03-08 22:57:00 +0000184 {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000185#endif
Manuel Novoa III df351d62001-03-08 22:57:00 +0000186#ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
187 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
188 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
189 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
190#endif
191 /* Last entry if for unmatched (possibly hostname) arg. */
192 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000193};
194
Manuel Novoa III df351d62001-03-08 22:57:00 +0000195static const struct options OptArray[] = {
196 {"metric", N_ARG, ARG_METRIC, 0},
197 {"mtu", N_ARG, ARG_MTU, 0},
198 {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
199 {"dstaddr", N_ARG, ARG_DSTADDR, 0},
200 {"netmask", N_ARG, ARG_NETMASK, 0},
201 {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
202#ifdef BB_FEATURE_IFCONFIG_HW
203 {"hw", N_ARG, ARG_HW, 0},
204#endif
205 {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
206#ifdef SIOCSKEEPALIVE
207 {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
208#endif
209#ifdef SIOCSOUTFILL
210 {"outfill", N_ARG, ARG_OUTFILL, 0},
211#endif
212#ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
213 {"mem_start", N_ARG, ARG_MEM_START, 0},
214 {"io_addr", N_ARG, ARG_IO_ADDR, 0},
215 {"irq", N_ARG, ARG_IRQ, 0},
216#endif
217 {"arp", N_CLR | M_SET, 0, IFF_NOARP},
218 {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
219 {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
220 {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
221 {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
222 {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
223 {"up", N_SET , 0, (IFF_UP | IFF_RUNNING)},
224 {"down", N_CLR , 0, IFF_UP},
225 { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
226};
Eric Andersenf15d4da2001-03-06 00:48:59 +0000227
Manuel Novoa III df351d62001-03-08 22:57:00 +0000228/*
229 * A couple of prototypes.
230 */
Eric Andersenec455952001-02-14 08:11:27 +0000231
Manuel Novoa III df351d62001-03-08 22:57:00 +0000232#ifdef BB_FEATURE_IFCONFIG_HW
233static int in_ether(char *bufp, struct sockaddr *sap);
234#endif
235
236#ifdef BB_FEATURE_IFCONFIG_STATUS
237extern int display_interfaces(void);
238#endif
239
240/*
241 * Our main function.
242 */
243
244int ifconfig_main(int argc, char **argv)
Eric Andersenec455952001-02-14 08:11:27 +0000245{
Manuel Novoa III df351d62001-03-08 22:57:00 +0000246 struct ifreq ifr;
247 struct sockaddr_in sai;
248#ifdef BB_FEATURE_IFCONFIG_HW
249 struct sockaddr sa;
250#endif
251 const struct arg1opt *a1op;
252 const struct options *op;
253 int sockfd; /* socket fd we use to manipulate stuff with */
254 int goterr;
255 int selector;
256 char *p;
257 char host[128];
258 unsigned char mask;
259 unsigned char did_flags;
Eric Andersenec455952001-02-14 08:11:27 +0000260
Manuel Novoa III df351d62001-03-08 22:57:00 +0000261 goterr = 0;
262 did_flags = 0;
263
264 if(argc < 2) {
265#ifdef BB_FEATURE_IFCONFIG_STATUS
266 return(display_interfaces());
267#else
268 show_usage();
269#endif
Eric Andersenec455952001-02-14 08:11:27 +0000270 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000271
272 /* Create a channel to the NET kernel. */
273 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
274 perror("socket");
275 exit(1);
Eric Andersenec455952001-02-14 08:11:27 +0000276 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000277
278 /* skip argv[0] */
279 argc--;
280 argv++;
281
282 /* get interface name */
283 safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
284
285 /* Process the remaining arguments. */
286 while (*++argv != (char *) NULL) {
287 p = *argv;
288 mask = N_MASK;
289 if (*p == '-') { /* If the arg starts with '-'... */
290 ++p; /* advance past it and */
291 mask = M_MASK; /* set the appropriate mask. */
292 }
293 for (op = OptArray ; op->name ; op++) { /* Find table entry. */
294 if (!strcmp(p,op->name)) { /* If name matches... */
295 if ((mask &= op->flags)) { /* set the mask and go. */
296 goto FOUND_ARG;;
297 }
298 /* If we get here, there was a valid arg with an */
299 /* invalid '-' prefix. */
300 ++goterr;
301 goto LOOP;
302 }
303 }
304
305 /* We fell through, so treat as possible hostname. */
306 a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
307 mask = op->arg_flags;
308 goto HOSTNAME;
309
310 FOUND_ARG:
311 if (mask & ARG_MASK) {
312 mask = op->arg_flags;
313 a1op = Arg1Opt + (op - OptArray);
314 if (mask & A_NETMASK & did_flags) {
315 show_usage();
316 }
317 if (*++argv == NULL) {
318 if (mask & A_ARG_REQ) {
319 show_usage();
320 } else {
321 --argv;
322 mask &= A_SET_AFTER; /* just for broadcast */
323 }
324 } else { /* got an arg so process it */
325 HOSTNAME:
326 did_flags |= (mask & A_NETMASK);
327 if (mask & A_CAST_HOST_COPY) {
328#ifdef BB_FEATURE_IFCONFIG_HW
329 if (mask & A_CAST_RESOLVE) {
330#endif
331 safe_strncpy(host, *argv, (sizeof host));
332 sai.sin_family = AF_INET;
333 sai.sin_port = 0;
334 if (!strcmp(host, "default")) {
335 /* Default is special, meaning 0.0.0.0. */
336 sai.sin_addr.s_addr = INADDR_ANY;
337 } else if (inet_aton(host, &sai.sin_addr) == 0) {
338 /* It's not a dotted quad. */
339 ++goterr;
340 continue;
341 }
342 p = (char *) &sai;
343#ifdef BB_FEATURE_IFCONFIG_HW
344 } else { /* A_CAST_HOST_COPY_IN_ETHER */
345 /* This is the "hw" arg case. */
346 if (strcmp("ether", *argv) || (*++argv == NULL)) {
347 show_usage();
348 }
349 safe_strncpy(host, *argv, (sizeof host));
350 if (in_ether(host, &sa)) {
351 fprintf(stderr, "invalid hw-addr %s\n", host);
352 ++goterr;
353 continue;
354 }
355 p = (char *) &sa;
356 }
357#endif
358 memcpy((((char *)(&ifr)) + a1op->ifr_offset),
359 p, sizeof(struct sockaddr));
360 } else {
361 unsigned int i = strtoul(*argv,NULL,0);
362 p = ((char *)(&ifr)) + a1op->ifr_offset;
363#ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
364 if (mask & A_MAP_TYPE) {
365 if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
366 ++goterr;
367 continue;
368 }
369 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
370 *((unsigned char *) p) = i;
371 } else if (mask & A_MAP_USHORT) {
372 *((unsigned short *) p) = i;
373 } else {
374 *((unsigned long *) p) = i;
375 }
376 } else
377#endif
378 if (mask & A_CAST_CHAR_PTR) {
379 *((caddr_t *) p) = (caddr_t) i;
380 } else { /* A_CAST_INT */
381 *((int *) p) = i;
382 }
383 }
384
385 if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
386 perror(a1op->name);
387 ++goterr;
388 continue;
389 }
390
391#ifdef QUESTIONABLE_ALIAS_CASE
392 if (mask & A_COLON_CHK) {
393 /*
394 * Don't do the set_flag() if the address is an alias with
395 * a - at the end, since it's deleted already! - Roman
396 *
397 * Should really use regex.h here, not sure though how well
398 * it'll go with the cross-platform support etc.
399 */
400 char *ptr;
401 short int found_colon = 0;
402 for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
403 if (*ptr == ':') {
404 found_colon++;
405 }
406 }
407
408 if (found_colon && *(ptr - 1) == '-') {
409 continue;
410 }
411 }
412#endif
413 }
414 if (!(mask & A_SET_AFTER)) {
415 continue;
416 }
417 mask = N_SET;
418 }
419
420 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
421 perror("SIOCGIFFLAGS");
422 ++goterr;
423 } else {
424 selector = op->selector;
425 if (mask & SET_MASK) {
426 ifr.ifr_flags |= selector;
427 } else {
428 ifr.ifr_flags &= ~selector;
429 }
430 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
431 perror("SIOCSIFFLAGS");
432 ++goterr;
433 }
434 }
435 LOOP:
436 } /* end of while-loop */
437
438 return goterr;
Eric Andersenec455952001-02-14 08:11:27 +0000439}
440
Manuel Novoa III df351d62001-03-08 22:57:00 +0000441#ifdef BB_FEATURE_IFCONFIG_HW
Eric Andersenec455952001-02-14 08:11:27 +0000442/* Input an Ethernet address and convert to binary. */
443static int
444in_ether(char *bufp, struct sockaddr *sap)
445{
446 unsigned char *ptr;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000447 int i, j;
448 unsigned char val;
449 unsigned char c;
Eric Andersenec455952001-02-14 08:11:27 +0000450
451 sap->sa_family = ARPHRD_ETHER;
452 ptr = sap->sa_data;
453
Manuel Novoa III df351d62001-03-08 22:57:00 +0000454 for (i = 0 ; i < ETH_ALEN ; i++) {
Eric Andersenec455952001-02-14 08:11:27 +0000455 val = 0;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000456
457 /* We might get a semicolon here - not required. */
458 if (i && (*bufp == ':')) {
Eric Andersenec455952001-02-14 08:11:27 +0000459 bufp++;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000460 }
461
462 for (j=0 ; j<2 ; j++) {
463 c = *bufp;
464 if (c >= '0' && c <= '9') {
465 c -= '0';
466 } else if (c >= 'a' && c <= 'f') {
467 c -= ('a' + 10);
468 } else if (c >= 'A' && c <= 'F') {
469 c -= ('A' + 10);
470 } else if (j && (c == ':' || c == 0)) {
471 break;
472 } else {
473 return -1;
474 }
475 ++bufp;
476 val <<= 4;
477 val += c;
478 }
479 *ptr++ = val;
Eric Andersenec455952001-02-14 08:11:27 +0000480 }
481
Manuel Novoa III df351d62001-03-08 22:57:00 +0000482 return (int) (*bufp); /* Error if we don't end at end of string. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000483}
484#endif