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.12 2001/08/10 06:02:23 mjn3 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/types.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
43 #include <net/if_arp.h>
44 #include <linux/if_ether.h>
47 #ifdef BB_FEATURE_IFCONFIG_SLIP
48 #include <linux/if_slip.h>
51 /* I don't know if this is needed for busybox or not. Anyone? */
52 #define QUESTIONABLE_ALIAS_CASE
55 /* Defines for glibc2.0 users. */
57 #define SIOCSIFTXQLEN 0x8943
58 #define SIOCGIFTXQLEN 0x8942
61 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
63 #define ifr_qlen ifr_ifru.ifru_mtu
67 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
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.
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)
90 * Here are the bit masks for the "arg_flags" member of struct options below.
97 * 02 HOST_COPY in_ether
98 * 03 HOST_COPY INET_resolve
100 #define A_CAST_TYPE 0x03
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)
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. */
115 * These defines are for dealing with the A_CAST_TYPE field.
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)
124 * These defines are for dealing with the A_MAP_TYPE field.
126 #define A_MAP_ULONG 0x04 /* memstart */
127 #define A_MAP_USHORT 0x08 /* io_addr */
128 #define A_MAP_UCHAR 0x0C /* irq */
131 * Define the bit masks signifying which operations to perform for each arg.
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)
151 * Set up the tables. Warning! They must have corresponding order!
156 unsigned short selector;
157 unsigned short ifr_offset;
162 const unsigned char flags;
163 const unsigned char arg_flags;
164 const unsigned short selector;
167 #define ifreq_offsetof(x) offsetof(struct ifreq, x)
169 static 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)},
179 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
180 #ifdef SIOCSKEEPALIVE
181 {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
184 {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
186 #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)},
191 /* Last entry if for unmatched (possibly hostname) arg. */
192 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
195 static 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},
205 {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
206 #ifdef SIOCSKEEPALIVE
207 {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
210 {"outfill", N_ARG, ARG_OUTFILL, 0},
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},
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)}
229 * A couple of prototypes.
232 #ifdef BB_FEATURE_IFCONFIG_HW
233 static int in_ether(char *bufp, struct sockaddr *sap);
236 #ifdef BB_FEATURE_IFCONFIG_STATUS
237 extern int interface_opt_a;
238 extern int display_interfaces(char *ifname);
245 int ifconfig_main(int argc, char **argv)
248 struct sockaddr_in sai;
249 #ifdef BB_FEATURE_IFCONFIG_HW
252 const struct arg1opt *a1op;
253 const struct options *op;
254 int sockfd; /* socket fd we use to manipulate stuff with */
260 unsigned char did_flags;
269 #ifdef BB_FEATURE_IFCONFIG_STATUS
270 if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
278 #ifdef BB_FEATURE_IFCONFIG_STATUS
279 return display_interfaces(argc ? *argv : NULL);
281 error_msg_and_die( "ifconfig was not compiled with interface status display support.");
285 /* Create a channel to the NET kernel. */
286 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
287 perror_msg_and_die("socket");
290 /* get interface name */
291 safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
293 /* Process the remaining arguments. */
294 while (*++argv != (char *) NULL) {
297 if (*p == '-') { /* If the arg starts with '-'... */
298 ++p; /* advance past it and */
299 mask = M_MASK; /* set the appropriate mask. */
301 for (op = OptArray ; op->name ; op++) { /* Find table entry. */
302 if (strcmp(p,op->name) == 0) { /* If name matches... */
303 if ((mask &= op->flags)) { /* set the mask and go. */
306 /* If we get here, there was a valid arg with an */
307 /* invalid '-' prefix. */
313 /* We fell through, so treat as possible hostname. */
314 a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
315 mask = op->arg_flags;
319 if (mask & ARG_MASK) {
320 mask = op->arg_flags;
321 a1op = Arg1Opt + (op - OptArray);
322 if (mask & A_NETMASK & did_flags) {
325 if (*++argv == NULL) {
326 if (mask & A_ARG_REQ) {
330 mask &= A_SET_AFTER; /* just for broadcast */
332 } else { /* got an arg so process it */
334 did_flags |= (mask & A_NETMASK);
335 if (mask & A_CAST_HOST_COPY) {
336 #ifdef BB_FEATURE_IFCONFIG_HW
337 if (mask & A_CAST_RESOLVE) {
339 safe_strncpy(host, *argv, (sizeof host));
340 sai.sin_family = AF_INET;
342 if (!strcmp(host, "default")) {
343 /* Default is special, meaning 0.0.0.0. */
344 sai.sin_addr.s_addr = INADDR_ANY;
345 } else if (inet_aton(host, &sai.sin_addr) == 0) {
346 /* It's not a dotted quad. */
351 #ifdef BB_FEATURE_IFCONFIG_HW
352 } else { /* A_CAST_HOST_COPY_IN_ETHER */
353 /* This is the "hw" arg case. */
354 if (strcmp("ether", *argv) || (*++argv == NULL)) {
357 safe_strncpy(host, *argv, (sizeof host));
358 if (in_ether(host, &sa)) {
359 fprintf(stderr, "invalid hw-addr %s\n", host);
366 memcpy((((char *)(&ifr)) + a1op->ifr_offset),
367 p, sizeof(struct sockaddr));
369 unsigned int i = strtoul(*argv,NULL,0);
370 p = ((char *)(&ifr)) + a1op->ifr_offset;
371 #ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
372 if (mask & A_MAP_TYPE) {
373 if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
377 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
378 *((unsigned char *) p) = i;
379 } else if (mask & A_MAP_USHORT) {
380 *((unsigned short *) p) = i;
382 *((unsigned long *) p) = i;
386 if (mask & A_CAST_CHAR_PTR) {
387 *((caddr_t *) p) = (caddr_t) i;
388 } else { /* A_CAST_INT */
393 if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
399 #ifdef QUESTIONABLE_ALIAS_CASE
400 if (mask & A_COLON_CHK) {
402 * Don't do the set_flag() if the address is an alias with
403 * a - at the end, since it's deleted already! - Roman
405 * Should really use regex.h here, not sure though how well
406 * it'll go with the cross-platform support etc.
409 short int found_colon = 0;
410 for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
416 if (found_colon && *(ptr - 1) == '-') {
422 if (!(mask & A_SET_AFTER)) {
428 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
429 perror("SIOCGIFFLAGS");
432 selector = op->selector;
433 if (mask & SET_MASK) {
434 ifr.ifr_flags |= selector;
436 ifr.ifr_flags &= ~selector;
438 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
439 perror("SIOCSIFFLAGS");
444 } /* end of while-loop */
449 #ifdef BB_FEATURE_IFCONFIG_HW
450 /* Input an Ethernet address and convert to binary. */
452 in_ether(char *bufp, struct sockaddr *sap)
459 sap->sa_family = ARPHRD_ETHER;
462 for (i = 0 ; i < ETH_ALEN ; i++) {
465 /* We might get a semicolon here - not required. */
466 if (i && (*bufp == ':')) {
470 for (j=0 ; j<2 ; j++) {
472 if (c >= '0' && c <= '9') {
474 } else if (c >= 'a' && c <= 'f') {
476 } else if (c >= 'A' && c <= 'F') {
478 } else if (j && (c == ':' || c == 0)) {
490 return (int) (*bufp); /* Error if we don't end at end of string. */