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).
6 * Bjorn Wesen, Axis Communications AB
9 * Authors of the original ifconfig was:
10 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
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.
18 * $Id: ifconfig.c,v 1.16 2001/11/10 11:22:43 andersen Exp $
23 * Heavily modified by Manuel Novoa III Mar 6, 2001
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.
29 * Still missing: media, tunnel.
34 #include <string.h> // strcmp and friends
35 #include <ctype.h> // isdigit and friends
36 #include <stddef.h> /* offsetof */
37 #include <sys/ioctl.h>
38 #include <net/if_arp.h>
39 #include <netinet/in.h>
40 #include <linux/if_ether.h>
42 #include "inet_common.h"
45 #ifdef CONFIG_FEATURE_IFCONFIG_SLIP
46 #include <linux/if_slip.h>
49 /* I don't know if this is needed for busybox or not. Anyone? */
50 #define QUESTIONABLE_ALIAS_CASE
53 /* Defines for glibc2.0 users. */
55 #define SIOCSIFTXQLEN 0x8943
56 #define SIOCGIFTXQLEN 0x8942
59 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
61 #define ifr_qlen ifr_ifru.ifru_mtu
65 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
69 * Here are the bit masks for the "flags" member of struct options below.
70 * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
71 * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
80 #define M_MASK (M_CLR | M_SET | M_ARG)
81 #define N_MASK (N_CLR | N_SET | N_ARG)
82 #define SET_MASK (N_SET | M_SET)
83 #define CLR_MASK (N_CLR | M_CLR)
84 #define SET_CLR_MASK (SET_MASK | CLR_MASK)
85 #define ARG_MASK (M_ARG | N_ARG)
88 * Here are the bit masks for the "arg_flags" member of struct options below.
95 * 02 HOST_COPY in_ether
96 * 03 HOST_COPY INET_resolve
98 #define A_CAST_TYPE 0x03
101 * 00 not a map type (mem_start, io_addr, irq)
102 * 04 memstart (unsigned long)
103 * 08 io_addr (unsigned short)
104 * 0C irq (unsigned char)
106 #define A_MAP_TYPE 0x0C
107 #define A_ARG_REQ 0x10 /* Set if an arg is required. */
108 #define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
109 #define A_SET_AFTER 0x40 /* Set a flag at the end. */
110 #define A_COLON_CHK 0x80 /* Is this needed? See below. */
113 * These defines are for dealing with the A_CAST_TYPE field.
115 #define A_CAST_CHAR_PTR 0x01
116 #define A_CAST_RESOLVE 0x01
117 #define A_CAST_HOST_COPY 0x02
118 #define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
119 #define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
122 * These defines are for dealing with the A_MAP_TYPE field.
124 #define A_MAP_ULONG 0x04 /* memstart */
125 #define A_MAP_USHORT 0x08 /* io_addr */
126 #define A_MAP_UCHAR 0x0C /* irq */
129 * Define the bit masks signifying which operations to perform for each arg.
132 #define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
133 #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
134 #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
135 #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
136 #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
137 #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
138 #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
139 #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
140 #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
141 #define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
142 #define ARG_POINTOPOINT (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
143 #define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
144 #define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
145 #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
149 * Set up the tables. Warning! They must have corresponding order!
154 unsigned short selector;
155 unsigned short ifr_offset;
160 const unsigned char flags;
161 const unsigned char arg_flags;
162 const unsigned short selector;
165 #define ifreq_offsetof(x) offsetof(struct ifreq, x)
167 static const struct arg1opt Arg1Opt[] = {
168 {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
169 {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
170 {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
171 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
172 {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
173 {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
174 #ifdef CONFIG_FEATURE_IFCONFIG_HW
175 {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
177 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
178 #ifdef SIOCSKEEPALIVE
179 {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
182 {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
184 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
185 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
186 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
187 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
189 /* Last entry if for unmatched (possibly hostname) arg. */
190 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
193 static const struct options OptArray[] = {
194 {"metric", N_ARG, ARG_METRIC, 0},
195 {"mtu", N_ARG, ARG_MTU, 0},
196 {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
197 {"dstaddr", N_ARG, ARG_DSTADDR, 0},
198 {"netmask", N_ARG, ARG_NETMASK, 0},
199 {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
200 #ifdef CONFIG_FEATURE_IFCONFIG_HW
201 {"hw", N_ARG, ARG_HW, 0},
203 {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
204 #ifdef SIOCSKEEPALIVE
205 {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
208 {"outfill", N_ARG, ARG_OUTFILL, 0},
210 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
211 {"mem_start", N_ARG, ARG_MEM_START, 0},
212 {"io_addr", N_ARG, ARG_IO_ADDR, 0},
213 {"irq", N_ARG, ARG_IRQ, 0},
215 {"arp", N_CLR | M_SET, 0, IFF_NOARP},
216 {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
217 {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
218 {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
219 {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
220 {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
221 {"up", N_SET , 0, (IFF_UP | IFF_RUNNING)},
222 {"down", N_CLR , 0, IFF_UP},
223 { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
227 * A couple of prototypes.
230 #ifdef CONFIG_FEATURE_IFCONFIG_HW
231 static int in_ether(char *bufp, struct sockaddr *sap);
234 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
235 extern int interface_opt_a;
236 extern int display_interfaces(char *ifname);
243 int ifconfig_main(int argc, char **argv)
246 struct sockaddr_in sai;
247 #ifdef CONFIG_FEATURE_IFCONFIG_HW
250 const struct arg1opt *a1op;
251 const struct options *op;
252 int sockfd; /* socket fd we use to manipulate stuff with */
258 unsigned char did_flags;
267 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
268 if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
276 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
277 return display_interfaces(argc ? *argv : NULL);
279 error_msg_and_die( "ifconfig was not compiled with interface status display support.");
283 /* Create a channel to the NET kernel. */
284 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
285 perror_msg_and_die("socket");
288 /* get interface name */
289 safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
291 /* Process the remaining arguments. */
292 while (*++argv != (char *) NULL) {
295 if (*p == '-') { /* If the arg starts with '-'... */
296 ++p; /* advance past it and */
297 mask = M_MASK; /* set the appropriate mask. */
299 for (op = OptArray ; op->name ; op++) { /* Find table entry. */
300 if (strcmp(p,op->name) == 0) { /* If name matches... */
301 if ((mask &= op->flags)) { /* set the mask and go. */
304 /* If we get here, there was a valid arg with an */
305 /* invalid '-' prefix. */
311 /* We fell through, so treat as possible hostname. */
312 a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
313 mask = op->arg_flags;
317 if (mask & ARG_MASK) {
318 mask = op->arg_flags;
319 a1op = Arg1Opt + (op - OptArray);
320 if (mask & A_NETMASK & did_flags) {
323 if (*++argv == NULL) {
324 if (mask & A_ARG_REQ) {
328 mask &= A_SET_AFTER; /* just for broadcast */
330 } else { /* got an arg so process it */
332 did_flags |= (mask & A_NETMASK);
333 if (mask & A_CAST_HOST_COPY) {
334 #ifdef CONFIG_FEATURE_IFCONFIG_HW
335 if (mask & A_CAST_RESOLVE) {
337 safe_strncpy(host, *argv, (sizeof host));
338 sai.sin_family = AF_INET;
340 if (!strcmp(host, bb_INET_default)) {
341 /* Default is special, meaning 0.0.0.0. */
342 sai.sin_addr.s_addr = INADDR_ANY;
343 } else if (inet_aton(host, &sai.sin_addr) == 0) {
344 /* It's not a dotted quad. */
349 #ifdef CONFIG_FEATURE_IFCONFIG_HW
350 } else { /* A_CAST_HOST_COPY_IN_ETHER */
351 /* This is the "hw" arg case. */
352 if (strcmp("ether", *argv) || (*++argv == NULL)) {
355 safe_strncpy(host, *argv, (sizeof host));
356 if (in_ether(host, &sa)) {
357 error_msg("invalid hw-addr %s", host);
364 memcpy((((char *)(&ifr)) + a1op->ifr_offset),
365 p, sizeof(struct sockaddr));
367 unsigned int i = strtoul(*argv,NULL,0);
368 p = ((char *)(&ifr)) + a1op->ifr_offset;
369 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
370 if (mask & A_MAP_TYPE) {
371 if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
375 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
376 *((unsigned char *) p) = i;
377 } else if (mask & A_MAP_USHORT) {
378 *((unsigned short *) p) = i;
380 *((unsigned long *) p) = i;
384 if (mask & A_CAST_CHAR_PTR) {
385 *((caddr_t *) p) = (caddr_t) i;
386 } else { /* A_CAST_INT */
391 if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
397 #ifdef QUESTIONABLE_ALIAS_CASE
398 if (mask & A_COLON_CHK) {
400 * Don't do the set_flag() if the address is an alias with
401 * a - at the end, since it's deleted already! - Roman
403 * Should really use regex.h here, not sure though how well
404 * it'll go with the cross-platform support etc.
407 short int found_colon = 0;
408 for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
414 if (found_colon && *(ptr - 1) == '-') {
420 if (!(mask & A_SET_AFTER)) {
426 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
427 perror("SIOCGIFFLAGS");
430 selector = op->selector;
431 if (mask & SET_MASK) {
432 ifr.ifr_flags |= selector;
434 ifr.ifr_flags &= ~selector;
436 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
437 perror("SIOCSIFFLAGS");
442 } /* end of while-loop */
447 #ifdef CONFIG_FEATURE_IFCONFIG_HW
448 /* Input an Ethernet address and convert to binary. */
450 in_ether(char *bufp, struct sockaddr *sap)
457 sap->sa_family = ARPHRD_ETHER;
460 for (i = 0 ; i < ETH_ALEN ; i++) {
463 /* We might get a semicolon here - not required. */
464 if (i && (*bufp == ':')) {
468 for (j=0 ; j<2 ; j++) {
470 if (c >= '0' && c <= '9') {
472 } else if (c >= 'a' && c <= 'f') {
474 } else if (c >= 'A' && c <= 'F') {
476 } else if (j && (c == ':' || c == 0)) {
488 return (int) (*bufp); /* Error if we don't end at end of string. */