1 /* vi: set sw=4 ts=4: */
5 * Similar to the standard Unix ifconfig, but with only the necessary
6 * parts for AF_INET, and without any printing of if info (for now).
8 * Bjorn Wesen, Axis Communications AB
10 * Authors of the original ifconfig was:
11 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
16 * Heavily modified by Manuel Novoa III Mar 6, 2001
18 * From initial port to busybox, removed most of the redundancy by
19 * converting to a table-driven approach. Added several (optional)
20 * args missing from initial port.
22 * Still missing: media, tunnel.
25 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
27 //config:config IFCONFIG
28 //config: bool "ifconfig (12 kb)"
30 //config: select PLATFORM_LINUX
32 //config: Ifconfig is used to configure the kernel-resident network interfaces.
34 //config:config FEATURE_IFCONFIG_STATUS
35 //config: bool "Enable status reporting output (+7k)"
37 //config: depends on IFCONFIG
39 //config: If ifconfig is called with no arguments it will display the status
40 //config: of the currently active interfaces.
42 //config:config FEATURE_IFCONFIG_SLIP
43 //config: bool "Enable slip-specific options \"keepalive\" and \"outfill\""
45 //config: depends on IFCONFIG
47 //config: Allow "keepalive" and "outfill" support for SLIP. If you're not
48 //config: planning on using serial lines, leave this unchecked.
50 //config:config FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
51 //config: bool "Enable options \"mem_start\", \"io_addr\", and \"irq\""
53 //config: depends on IFCONFIG
55 //config: Allow the start address for shared memory, start address for I/O,
56 //config: and/or the interrupt line used by the specified device.
58 //config:config FEATURE_IFCONFIG_HW
59 //config: bool "Enable option \"hw\" (ether only)"
61 //config: depends on IFCONFIG
63 //config: Set the hardware address of this interface, if the device driver
64 //config: supports this operation. Currently, we only support the 'ether'
67 //config:config FEATURE_IFCONFIG_BROADCAST_PLUS
68 //config: bool "Set the broadcast automatically"
70 //config: depends on IFCONFIG
72 //config: Setting this will make ifconfig attempt to find the broadcast
73 //config: automatically if the value '+' is used.
75 //applet:IF_IFCONFIG(APPLET(ifconfig, BB_DIR_SBIN, BB_SUID_DROP))
77 //kbuild:lib-$(CONFIG_IFCONFIG) += ifconfig.o interface.o
79 //usage:#define ifconfig_trivial_usage
80 //usage: IF_FEATURE_IFCONFIG_STATUS("[-a]") " interface [address]"
81 //usage:#define ifconfig_full_usage "\n\n"
82 //usage: "Configure a network interface\n"
84 //usage: IF_FEATURE_IPV6(
85 //usage: " [add ADDRESS[/PREFIXLEN]]\n")
86 //usage: IF_FEATURE_IPV6(
87 //usage: " [del ADDRESS[/PREFIXLEN]]\n")
88 //usage: " [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]]\n"
89 //usage: " [netmask ADDRESS] [dstaddr ADDRESS]\n"
90 //usage: IF_FEATURE_IFCONFIG_SLIP(
91 //usage: " [outfill NN] [keepalive NN]\n")
92 //usage: " " IF_FEATURE_IFCONFIG_HW("[hw ether" IF_FEATURE_HWIB("|infiniband")" ADDRESS] ") "[metric NN] [mtu NN]\n"
93 //usage: " [[-]trailers] [[-]arp] [[-]allmulti]\n"
94 //usage: " [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic]\n"
95 //usage: IF_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ(
96 //usage: " [mem_start NN] [io_addr NN] [irq NN]\n")
97 //usage: " [up|down] ..."
100 #include "inet_common.h"
102 #include <net/if_arp.h>
103 #include <netinet/in.h>
104 #ifdef HAVE_NET_ETHERNET_H
105 # include <net/ethernet.h>
108 #if ENABLE_FEATURE_IFCONFIG_SLIP
109 # include <linux/if_slip.h>
112 /* I don't know if this is needed for busybox or not. Anyone? */
113 #define QUESTIONABLE_ALIAS_CASE
116 /* Defines for glibc2.0 users. */
117 #ifndef SIOCSIFTXQLEN
118 # define SIOCSIFTXQLEN 0x8943
119 # define SIOCGIFTXQLEN 0x8942
122 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
124 # define ifr_qlen ifr_ifru.ifru_mtu
128 # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
131 #if ENABLE_FEATURE_IPV6
133 struct in6_addr ifr6_addr;
134 uint32_t ifr6_prefixlen;
140 * Here are the bit masks for the "flags" member of struct options below.
141 * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
142 * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
151 #define M_MASK (M_CLR | M_SET | M_ARG)
152 #define N_MASK (N_CLR | N_SET | N_ARG)
153 #define SET_MASK (N_SET | M_SET)
154 #define CLR_MASK (N_CLR | M_CLR)
155 #define SET_CLR_MASK (SET_MASK | CLR_MASK)
156 #define ARG_MASK (M_ARG | N_ARG)
159 * Here are the bit masks for the "arg_flags" member of struct options below.
166 * 02 HOST_COPY in_ether
167 * 03 HOST_COPY INET_resolve
169 #define A_CAST_TYPE 0x03
172 * 00 not a map type (mem_start, io_addr, irq)
173 * 04 memstart (unsigned long)
174 * 08 io_addr (unsigned short)
175 * 0C irq (unsigned char)
177 #define A_MAP_TYPE 0x0C
178 #define A_ARG_REQ 0x10 /* Set if an arg is required. */
179 #define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
180 #define A_SET_AFTER 0x40 /* Set a flag at the end. */
181 #define A_COLON_CHK 0x80 /* Is this needed? See below. */
182 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
183 #define A_HOSTNAME 0x100 /* Set if it is ip addr. */
184 #define A_BROADCAST 0x200 /* Set if it is broadcast addr. */
187 #define A_BROADCAST 0
191 * These defines are for dealing with the A_CAST_TYPE field.
193 #define A_CAST_CHAR_PTR 0x01
194 #define A_CAST_RESOLVE 0x01
195 #define A_CAST_HOST_COPY 0x02
196 #define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
197 #define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
200 * These defines are for dealing with the A_MAP_TYPE field.
202 #define A_MAP_ULONG 0x04 /* memstart */
203 #define A_MAP_USHORT 0x08 /* io_addr */
204 #define A_MAP_UCHAR 0x0C /* irq */
207 * Define the bit masks signifying which operations to perform for each arg.
210 #define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
211 #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
212 #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
213 #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
214 #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
215 #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
216 #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
217 #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
218 #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_BROADCAST)
219 #define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
220 #define ARG_POINTOPOINT (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
221 #define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
222 #define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
223 #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK | A_HOSTNAME)
224 #define ARG_ADD_DEL (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
229 unsigned short selector;
230 unsigned short ifr_offset;
235 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
236 const unsigned int flags:6;
237 const unsigned int arg_flags:10;
239 const unsigned char flags;
240 const unsigned char arg_flags;
242 const unsigned short selector;
245 #define ifreq_offsetof(x) offsetof(struct ifreq, x)
248 * Set up the tables. Warning! They must have corresponding order!
251 static const struct arg1opt Arg1Opt[] = {
252 { "SIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric) },
253 { "SIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu) },
254 { "SIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen) },
255 { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
256 { "SIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask) },
257 { "SIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr) },
258 #if ENABLE_FEATURE_IFCONFIG_HW
259 { "SIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr) },
261 { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
262 #ifdef SIOCSKEEPALIVE
263 { "SKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data) },
266 { "SOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data) },
268 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
269 { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start) },
270 { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr) },
271 { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq) },
273 #if ENABLE_FEATURE_IPV6
274 { "SIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
275 { "DIFADDR", SIOCDIFADDR, ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
277 /* Last entry is for unmatched (assumed to be hostname/address) arg. */
278 { "SIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr) },
281 static const struct options OptArray[] = {
282 { "metric", N_ARG, ARG_METRIC, 0 },
283 { "mtu", N_ARG, ARG_MTU, 0 },
284 { "txqueuelen", N_ARG, ARG_TXQUEUELEN, 0 },
285 { "dstaddr", N_ARG, ARG_DSTADDR, 0 },
286 { "netmask", N_ARG, ARG_NETMASK, 0 },
287 { "broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST },
288 #if ENABLE_FEATURE_IFCONFIG_HW
289 { "hw", N_ARG, ARG_HW, 0 },
291 { "pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT },
292 #ifdef SIOCSKEEPALIVE
293 { "keepalive", N_ARG, ARG_KEEPALIVE, 0 },
296 { "outfill", N_ARG, ARG_OUTFILL, 0 },
298 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
299 { "mem_start", N_ARG, ARG_MEM_START, 0 },
300 { "io_addr", N_ARG, ARG_IO_ADDR, 0 },
301 { "irq", N_ARG, ARG_IRQ, 0 },
303 #if ENABLE_FEATURE_IPV6
304 { "add", N_ARG, ARG_ADD_DEL, 0 },
305 { "del", N_ARG, ARG_ADD_DEL, 0 },
307 { "arp", N_CLR | M_SET, 0, IFF_NOARP },
308 { "trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS },
309 { "promisc", N_SET | M_CLR, 0, IFF_PROMISC },
310 { "multicast", N_SET | M_CLR, 0, IFF_MULTICAST },
311 { "allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI },
312 { "dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC },
313 { "up", N_SET, 0, (IFF_UP | IFF_RUNNING) },
314 { "down", N_CLR, 0, IFF_UP },
315 { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING) }
318 int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
319 int ifconfig_main(int argc UNUSED_PARAM, char **argv)
322 struct sockaddr_in sai;
323 #if ENABLE_FEATURE_IFCONFIG_HW
326 const struct arg1opt *a1op;
327 const struct options *op;
328 int sockfd; /* socket fd we use to manipulate stuff with */
330 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
332 unsigned int did_flags;
333 unsigned int sai_hostname, sai_netmask;
336 unsigned char did_flags;
340 const char *host = NULL; /* make gcc happy */
343 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
351 #if ENABLE_FEATURE_IFCONFIG_STATUS
352 if (argv[0] && (argv[0][0] == '-' && argv[0][1] == 'a' && !argv[0][2])) {
358 if (!argv[0] || !argv[1]) { /* one or no args */
359 #if ENABLE_FEATURE_IFCONFIG_STATUS
360 return display_interfaces(argv[0] /* can be NULL */);
362 bb_error_msg_and_die("no support for status display");
366 /* Create a channel to the NET kernel. */
367 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
369 /* get interface name */
370 strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
372 /* Process the remaining arguments. */
373 while (*++argv != NULL) {
376 if (*p == '-') { /* If the arg starts with '-'... */
377 ++p; /* advance past it and */
378 mask = M_MASK; /* set the appropriate mask. */
380 for (op = OptArray; op->name; op++) { /* Find table entry. */
381 if (strcmp(p, op->name) == 0) { /* If name matches... */
383 if (mask) /* set the mask and go. */
385 /* If we get here, there was a valid arg with an */
386 /* invalid '-' prefix. */
387 bb_error_msg_and_die("bad: '%s'", p-1);
391 /* We fell through, so treat as possible hostname. */
392 a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1;
393 mask = op->arg_flags;
397 if (mask & ARG_MASK) {
398 mask = op->arg_flags;
399 if (mask & A_NETMASK & did_flags)
401 a1op = Arg1Opt + (op - OptArray);
402 if (*++argv == NULL) {
403 if (mask & A_ARG_REQ)
406 mask &= A_SET_AFTER; /* just for broadcast */
407 } else { /* got an arg so process it */
409 did_flags |= (mask & (A_NETMASK|A_HOSTNAME));
410 if (mask & A_CAST_HOST_COPY) {
411 #if ENABLE_FEATURE_IFCONFIG_HW
412 if (mask & A_CAST_RESOLVE) {
415 if (strcmp(host, "inet") == 0)
416 continue; /* compat stuff */
417 sai.sin_family = AF_INET;
419 if (strcmp(host, "default") == 0) {
420 /* Default is special, meaning 0.0.0.0. */
421 sai.sin_addr.s_addr = INADDR_ANY;
423 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
424 else if ((host[0] == '+' && !host[1])
425 && (mask & A_BROADCAST)
426 && (did_flags & (A_NETMASK|A_HOSTNAME)) == (A_NETMASK|A_HOSTNAME)
428 /* + is special, meaning broadcast is derived. */
429 sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
433 len_and_sockaddr *lsa;
434 #if ENABLE_FEATURE_IPV6
437 prefix = strchr(host, '/');
439 prefix_len = xatou_range(prefix + 1, 0, 128);
444 lsa = xhost2sockaddr(host, 0);
445 #if ENABLE_FEATURE_IPV6
446 if (lsa->u.sa.sa_family != AF_INET6 && prefix) {
447 /* TODO: we do not support "ifconfig eth0 up 1.2.3.4/17".
448 * For now, just make it fail instead of silently ignoring "/17" part:
453 if (lsa->u.sa.sa_family == AF_INET6) {
455 struct in6_ifreq ifr6;
457 sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
458 xioctl(sockfd6, SIOCGIFINDEX, &ifr);
459 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
460 ifr6.ifr6_prefixlen = prefix_len;
461 memcpy(&ifr6.ifr6_addr,
462 &lsa->u.sin6.sin6_addr,
463 sizeof(struct in6_addr));
464 ioctl_or_perror_and_die(sockfd6, a1op->selector, &ifr6, "SIOC%s", a1op->name);
465 if (ENABLE_FEATURE_CLEAN_UP)
470 sai.sin_addr = lsa->u.sin.sin_addr;
471 if (ENABLE_FEATURE_CLEAN_UP)
474 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
475 if (mask & A_HOSTNAME)
476 sai_hostname = sai.sin_addr.s_addr;
477 if (mask & A_NETMASK)
478 sai_netmask = sai.sin_addr.s_addr;
481 #if ENABLE_FEATURE_IFCONFIG_HW
482 } else { /* A_CAST_HOST_COPY_IN_ETHER */
483 /* This is the "hw" arg case. */
484 smalluint hw_class = index_in_substrings("ether\0"
485 IF_FEATURE_HWIB("infiniband\0"), *argv) + 1;
486 if (!hw_class || !*++argv)
489 if (hw_class == 1 ? in_ether(host, &sa) : in_ib(host, &sa))
490 bb_error_msg_and_die("invalid hw-addr %s", host);
494 memcpy( ((char *)&ifr) + a1op->ifr_offset,
495 p, sizeof(struct sockaddr));
497 /* FIXME: error check?? */
498 unsigned long i = strtoul(*argv, NULL, 0);
499 p = ((char *)&ifr) + a1op->ifr_offset;
500 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
501 if (mask & A_MAP_TYPE) {
502 xioctl(sockfd, SIOCGIFMAP, &ifr);
503 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR)
504 *(unsigned char *) p = i;
505 else if (mask & A_MAP_USHORT)
506 *(unsigned short *) p = i;
508 *(unsigned long *) p = i;
511 if (mask & A_CAST_CHAR_PTR)
512 *(caddr_t *) p = (caddr_t) i;
513 else /* A_CAST_INT */
517 ioctl_or_perror_and_die(sockfd, a1op->selector, &ifr, "SIOC%s", a1op->name);
518 #ifdef QUESTIONABLE_ALIAS_CASE
519 if (mask & A_COLON_CHK) {
521 * Don't do the set_flag() if the address is an alias with
522 * a '-' at the end, since it's deleted already! - Roman
524 * Should really use regex.h here, not sure though how well
525 * it'll go with the cross-platform support etc.
528 short int found_colon = 0;
529 for (ptr = ifr.ifr_name; *ptr; ptr++)
532 if (found_colon && ptr[-1] == '-')
537 if (!(mask & A_SET_AFTER))
540 } /* if (mask & ARG_MASK) */
542 xioctl(sockfd, SIOCGIFFLAGS, &ifr);
543 selector = op->selector;
545 ifr.ifr_flags |= selector;
547 ifr.ifr_flags &= ~selector;
548 xioctl(sockfd, SIOCSIFFLAGS, &ifr);
551 if (ENABLE_FEATURE_CLEAN_UP)