2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersen@codepoet.org>
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 * Added print_bytes_scaled function to reduce code size.
8 * Added some (potentially) missing defines.
9 * Improved display support for -a and for a named interface.
11 * -----------------------------------------------------------
13 * ifconfig This file contains an implementation of the command
14 * that either displays or sets the characteristics of
15 * one or more of the system's networking interfaces.
17 * Version: $Id: interface.c,v 1.25 2004/08/26 21:45:21 andersen Exp $
19 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
20 * and others. Copyright 1993 MicroWalt Corporation
22 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
24 * Patched to support 'add' and 'del' keywords for INET(4) addresses
25 * by Mrs. Brisby <mrs.brisby@nimh.org>
27 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
28 * - gettext instead of catgets for i18n
29 * 10/1998 - Andi Kleen. Use interface list primitives.
30 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
31 * (default AF was wrong)
43 * Device Hardware types.
46 #define HAVE_HWETHER 1
50 #include "inet_common.h"
58 #include <sys/ioctl.h>
59 #include <sys/types.h>
61 #include <net/if_arp.h>
64 #ifdef CONFIG_FEATURE_IPV6
65 # define HAVE_AFINET6 1
70 #define _PATH_PROCNET_DEV "/proc/net/dev"
71 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
77 * This is in linux/include/net/ipv6.h.
81 struct in6_addr ifr6_addr;
82 uint32_t ifr6_prefixlen;
83 unsigned int ifr6_ifindex;
88 #endif /* HAVE_AFINET6 */
90 /* Defines for glibc2.0 users. */
92 #define SIOCSIFTXQLEN 0x8943
93 #define SIOCGIFTXQLEN 0x8942
96 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
98 #define ifr_qlen ifr_ifru.ifru_mtu
101 #ifndef HAVE_TXQUEUELEN
102 #define HAVE_TXQUEUELEN 1
106 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
109 /* This structure defines protocol families and their handlers. */
115 char *(*print) (unsigned char *);
116 char *(*sprint) (struct sockaddr *, int numeric);
117 int (*input) (int type, char *bufp, struct sockaddr *);
118 void (*herror) (char *text);
119 int (*rprint) (int options);
120 int (*rinput) (int typ, int ext, char **argv);
123 int (*getmask) (char *src, struct sockaddr * mask, char *name);
131 /* Display a UNIX domain address. */
132 static char *UNIX_print(unsigned char *ptr)
138 /* Display a UNIX domain address. */
139 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
143 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
144 return safe_strncpy(buf, "[NONE SET]", sizeof(buf));
145 return (UNIX_print(sap->sa_data));
149 static struct aftype unix_aftype = {
150 "unix", "UNIX Domain", AF_UNIX, 0,
151 UNIX_print, UNIX_sprint, NULL, NULL,
156 #endif /* HAVE_AFUNIX */
160 /* Display an Internet socket address. */
161 static char *INET_sprint(struct sockaddr *sap, int numeric)
163 static char buff[128];
165 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
166 return safe_strncpy(buff, "[NONE SET]", sizeof(buff));
168 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
169 numeric, 0xffffff00) != 0)
175 static struct aftype inet_aftype = {
176 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
177 NULL /* UNUSED INET_print */ , INET_sprint,
178 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
179 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
180 NULL /* UNUSED INET_getnetmask */ ,
185 #endif /* HAVE_AFINET */
189 /* Display an Internet socket address. */
190 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
191 static char *INET6_sprint(struct sockaddr *sap, int numeric)
193 static char buff[128];
195 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
196 return safe_strncpy(buff, "[NONE SET]", sizeof(buff));
198 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
199 return safe_strncpy(buff, "[UNKNOWN]", sizeof(buff));
203 static struct aftype inet6_aftype = {
204 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
205 NULL /* UNUSED INET6_print */ , INET6_sprint,
206 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
207 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
208 NULL /* UNUSED INET6_getnetmask */ ,
213 #endif /* HAVE_AFINET6 */
215 /* Display an UNSPEC address. */
216 static char *UNSPEC_print(unsigned char *ptr)
218 static char buff[sizeof(struct sockaddr) * 3 + 1];
223 for (i = 0; i < sizeof(struct sockaddr); i++) {
224 /* careful -- not every libc's sprintf returns # bytes written */
225 sprintf(pos, "%02X-", (*ptr++ & 0377));
228 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
233 /* Display an UNSPEC socket address. */
234 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
238 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
239 return safe_strncpy(buf, "[NONE SET]", sizeof(buf));
240 return (UNSPEC_print((unsigned char *)sap->sa_data));
243 static struct aftype unspec_aftype = {
244 "unspec", "UNSPEC", AF_UNSPEC, 0,
245 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
249 static struct aftype * const aftypes[] = {
263 /* Check our protocol family table for this family. */
264 static struct aftype *get_afntype(int af)
266 struct aftype * const *afp;
269 while (*afp != NULL) {
270 if ((*afp)->af == af)
277 /* Check our protocol family table for this family and return its socket */
278 static int get_socket_for_af(int af)
280 struct aftype * const *afp;
283 while (*afp != NULL) {
284 if ((*afp)->af == af)
291 struct user_net_device_stats {
292 unsigned long long rx_packets; /* total packets received */
293 unsigned long long tx_packets; /* total packets transmitted */
294 unsigned long long rx_bytes; /* total bytes received */
295 unsigned long long tx_bytes; /* total bytes transmitted */
296 unsigned long rx_errors; /* bad packets received */
297 unsigned long tx_errors; /* packet transmit problems */
298 unsigned long rx_dropped; /* no space in linux buffers */
299 unsigned long tx_dropped; /* no space available in linux */
300 unsigned long rx_multicast; /* multicast packets received */
301 unsigned long rx_compressed;
302 unsigned long tx_compressed;
303 unsigned long collisions;
305 /* detailed rx_errors: */
306 unsigned long rx_length_errors;
307 unsigned long rx_over_errors; /* receiver ring buff overflow */
308 unsigned long rx_crc_errors; /* recved pkt with crc error */
309 unsigned long rx_frame_errors; /* recv'd frame alignment error */
310 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
311 unsigned long rx_missed_errors; /* receiver missed packet */
312 /* detailed tx_errors */
313 unsigned long tx_aborted_errors;
314 unsigned long tx_carrier_errors;
315 unsigned long tx_fifo_errors;
316 unsigned long tx_heartbeat_errors;
317 unsigned long tx_window_errors;
321 struct interface *next, *prev;
322 char name[IFNAMSIZ]; /* interface name */
323 short type; /* if type */
324 short flags; /* various flags */
325 int metric; /* routing metric */
326 int mtu; /* MTU value */
327 int tx_queue_len; /* transmit queue length */
328 struct ifmap map; /* hardware setup */
329 struct sockaddr addr; /* IP address */
330 struct sockaddr dstaddr; /* P-P IP address */
331 struct sockaddr broadaddr; /* IP broadcast address */
332 struct sockaddr netmask; /* IP network mask */
334 char hwaddr[32]; /* HW address */
335 int statistics_valid;
336 struct user_net_device_stats stats; /* statistics */
337 int keepalive; /* keepalive value for SLIP */
338 int outfill; /* outfill value for SLIP */
342 int interface_opt_a = 0; /* show all interfaces */
344 static struct interface *int_list, *int_last;
345 static int skfd = -1; /* generic raw socket desc. */
348 static int sockets_open(int family)
350 struct aftype * const *aft;
352 static int force = -1;
356 if (get_linux_version_code() < KERNEL_VERSION(2,1,0))
358 if (access("/proc/net", R_OK))
361 for (aft = aftypes; *aft; aft++) {
362 struct aftype *af = *aft;
363 int type = SOCK_DGRAM;
365 if (af->af == AF_UNSPEC)
367 if (family && family != af->af)
373 /* Check some /proc file first to not stress kmod */
374 if (!family && !force && af->flag_file) {
375 if (access(af->flag_file, R_OK))
378 af->fd = socket(af->af, type, 0);
383 bb_error_msg("No usable address families found.");
388 #ifdef CONFIG_FEATURE_CLEAN_UP
389 static void sockets_close(void)
391 struct aftype * const *aft;
392 for (aft = aftypes; *aft != NULL; aft++) {
393 struct aftype *af = *aft;
402 /* like strcmp(), but knows about numbers */
403 static int nstrcmp(const char *a, const char *b)
405 const char *a_ptr = a;
406 const char *b_ptr = b;
412 if (!isdigit(*a) && isdigit(*(a+1))) {
420 if (isdigit(*a) && isdigit(*b)) {
421 return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1;
426 static struct interface *add_interface(char *name)
428 struct interface *ife, **nextp, *new;
430 for (ife = int_last; ife; ife = ife->prev) {
431 int n = nstrcmp(ife->name, name);
439 new = xzalloc(sizeof(*new));
440 safe_strncpy(new->name, name, IFNAMSIZ);
441 nextp = ife ? &ife->next : &int_list;
445 new->next->prev = new;
453 static int if_readconf(void)
461 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
463 skfd2 = get_socket_for_af(AF_INET);
465 bb_perror_msg(("warning: no inet socket available"));
466 /* Try to soldier on with whatever socket we can get hold of. */
467 skfd2 = sockets_open(0);
474 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
475 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
477 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
478 perror("SIOCGIFCONF");
481 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
482 /* assume it overflowed and try again */
490 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
491 add_interface(ifr->ifr_name);
501 static char *get_name(char *name, char *p)
503 /* Extract <name>[:<alias>] from nul-terminated p where p matches
504 <name>[:<alias>]: after leading whitespace.
505 If match is not made, set name empty and return unchanged p */
506 int namestart=0, nameend=0, aliasend;
507 while (isspace(p[namestart]))
510 while (p[nameend] && p[nameend]!=':' && !isspace(p[nameend]))
512 if (p[nameend]==':') {
514 while (p[aliasend] && isdigit(p[aliasend]))
516 if (p[aliasend]==':') {
519 if ((nameend-namestart)<IFNAMSIZ) {
520 memcpy(name,&p[namestart],nameend-namestart);
521 name[nameend-namestart]='\0';
524 /* Interface name too large */
528 /* first ':' not found - return empty */
534 /* If scanf supports size qualifiers for %n conversions, then we can
535 * use a modified fmt that simply stores the position in the fields
536 * having no associated fields in the proc string. Of course, we need
537 * to zero them again when we're done. But that is smaller than the
538 * old approach of multiple scanf occurrences with large numbers of
541 /* static const char * const ss_fmt[] = { */
542 /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
543 /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
544 /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
547 /* Lie about the size of the int pointed to for %n. */
548 #if INT_MAX == LONG_MAX
549 static const char * const ss_fmt[] = {
550 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
551 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
552 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
555 static const char * const ss_fmt[] = {
556 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
557 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
558 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
563 static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
565 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
567 sscanf(bp, ss_fmt[procnetdev_vsn],
568 &ife->stats.rx_bytes, /* missing for 0 */
569 &ife->stats.rx_packets,
570 &ife->stats.rx_errors,
571 &ife->stats.rx_dropped,
572 &ife->stats.rx_fifo_errors,
573 &ife->stats.rx_frame_errors,
574 &ife->stats.rx_compressed, /* missing for <= 1 */
575 &ife->stats.rx_multicast, /* missing for <= 1 */
576 &ife->stats.tx_bytes, /* missing for 0 */
577 &ife->stats.tx_packets,
578 &ife->stats.tx_errors,
579 &ife->stats.tx_dropped,
580 &ife->stats.tx_fifo_errors,
581 &ife->stats.collisions,
582 &ife->stats.tx_carrier_errors,
583 &ife->stats.tx_compressed /* missing for <= 1 */
586 if (procnetdev_vsn <= 1) {
587 if (procnetdev_vsn == 0) {
588 ife->stats.rx_bytes = 0;
589 ife->stats.tx_bytes = 0;
591 ife->stats.rx_multicast = 0;
592 ife->stats.rx_compressed = 0;
593 ife->stats.tx_compressed = 0;
597 static inline int procnetdev_version(char *buf)
599 if (strstr(buf, "compressed"))
601 if (strstr(buf, "bytes"))
606 static int if_readlist_proc(char *target)
608 static int proc_read;
611 struct interface *ife;
612 int err, procnetdev_vsn;
619 fh = fopen(_PATH_PROCNET_DEV, "r");
621 bb_perror_msg("Warning: cannot open %s. Limited output.", _PATH_PROCNET_DEV);
622 return if_readconf();
624 fgets(buf, sizeof buf, fh); /* eat line */
625 fgets(buf, sizeof buf, fh);
627 procnetdev_vsn = procnetdev_version(buf);
630 while (fgets(buf, sizeof buf, fh)) {
633 s = get_name(name, buf);
634 ife = add_interface(name);
635 get_dev_fields(s, ife, procnetdev_vsn);
636 ife->statistics_valid = 1;
637 if (target && !strcmp(target, name))
641 perror(_PATH_PROCNET_DEV);
649 static int if_readlist(void)
651 int err = if_readlist_proc(NULL);
658 static int for_all_interfaces(int (*doit) (struct interface *, void *),
661 struct interface *ife;
663 if (!int_list && (if_readlist() < 0))
665 for (ife = int_list; ife; ife = ife->next) {
666 int err = doit(ife, cookie);
674 /* Fetch the interface configuration from the kernel. */
675 static int if_fetch(struct interface *ife)
679 char *ifname = ife->name;
681 strcpy(ifr.ifr_name, ifname);
682 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
684 ife->flags = ifr.ifr_flags;
686 strcpy(ifr.ifr_name, ifname);
687 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
688 memset(ife->hwaddr, 0, 32);
690 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
692 ife->type = ifr.ifr_hwaddr.sa_family;
694 strcpy(ifr.ifr_name, ifname);
695 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
698 ife->metric = ifr.ifr_metric;
700 strcpy(ifr.ifr_name, ifname);
701 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
704 ife->mtu = ifr.ifr_mtu;
707 strcpy(ifr.ifr_name, ifname);
708 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
709 ife->map = ifr.ifr_map;
712 memset(&ife->map, 0, sizeof(struct ifmap));
714 #ifdef HAVE_TXQUEUELEN
715 strcpy(ifr.ifr_name, ifname);
716 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
717 ife->tx_queue_len = -1; /* unknown value */
719 ife->tx_queue_len = ifr.ifr_qlen;
721 ife->tx_queue_len = -1; /* unknown value */
726 fd = get_socket_for_af(AF_INET);
728 strcpy(ifr.ifr_name, ifname);
729 ifr.ifr_addr.sa_family = AF_INET;
730 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
732 ife->addr = ifr.ifr_addr;
733 strcpy(ifr.ifr_name, ifname);
734 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
735 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
737 ife->dstaddr = ifr.ifr_dstaddr;
739 strcpy(ifr.ifr_name, ifname);
740 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
741 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
743 ife->broadaddr = ifr.ifr_broadaddr;
745 strcpy(ifr.ifr_name, ifname);
746 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
747 memset(&ife->netmask, 0, sizeof(struct sockaddr));
749 ife->netmask = ifr.ifr_netmask;
751 memset(&ife->addr, 0, sizeof(struct sockaddr));
759 static int do_if_fetch(struct interface *ife)
761 if (if_fetch(ife) < 0) {
764 if (errno == ENODEV) {
765 /* Give better error message for this case. */
766 errmsg = "Device not found";
768 errmsg = strerror(errno);
770 bb_error_msg("%s: error fetching interface information: %s",
777 /* This structure defines hardware protocols and their handlers. */
779 const char * const name;
783 char *(*print) (unsigned char *);
784 int (*input) (char *, struct sockaddr *);
785 int (*activate) (int fd);
786 int suppress_null_addr;
789 static const struct hwtype unspec_hwtype = {
790 "unspec", "UNSPEC", -1, 0,
791 UNSPEC_print, NULL, NULL
794 static const struct hwtype loop_hwtype = {
795 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
800 #include <net/if_arp.h>
802 #if (__GLIBC__ >=2 && __GLIBC_MINOR >= 1) || defined(_NEWLIB_VERSION)
803 #include <net/ethernet.h>
805 #include <linux/if_ether.h>
808 /* Display an Ethernet address in readable format. */
809 static char *pr_ether(unsigned char *ptr)
811 static char buff[64];
813 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
814 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
815 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
820 static const struct hwtype ether_hwtype = {
821 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
822 pr_ether, NULL /* UNUSED in_ether */ , NULL
826 #endif /* HAVE_HWETHER */
831 #include <net/if_arp.h>
833 static const struct hwtype ppp_hwtype = {
834 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
835 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
839 #endif /* HAVE_PPP */
841 static const struct hwtype * const hwtypes[] = {
862 static const char * const if_port_text[] = {
863 /* Keep in step with <linux/netdevice.h> */
875 /* Check our hardware type table for this type. */
876 static const struct hwtype *get_hwntype(int type)
878 const struct hwtype * const *hwp;
881 while (*hwp != NULL) {
882 if ((*hwp)->type == type)
889 /* return 1 if address is all zeros */
890 static int hw_null_address(const struct hwtype *hw, void *ap)
893 unsigned char *address = (unsigned char *) ap;
895 for (i = 0; i < hw->alen; i++)
901 static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti";
903 static void print_bytes_scaled(unsigned long long ull, const char *end)
905 unsigned long long int_part;
907 unsigned int frac_part;
915 if (int_part >= 1024) {
916 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
918 ext += 3; /* KiB, MiB, GiB, TiB */
923 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
926 static const char * const ife_print_flags_strs[] = {
945 static const unsigned short ife_print_flags_mask[] = {
965 static void ife_print(struct interface *ptr)
968 const struct hwtype *hw;
970 int can_compress = 0;
974 char addr6[40], devname[20];
975 struct sockaddr_in6 sap;
976 int plen, scope, dad_status, if_idx;
980 ap = get_afntype(ptr->addr.sa_family);
986 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
989 hw = get_hwntype(hf);
991 hw = get_hwntype(-1);
993 printf("%-9.9s Link encap:%s ", ptr->name, hw->title);
994 /* For some hardware types (eg Ash, ATM) we don't print the
995 hardware address if it's null. */
996 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
997 hw->suppress_null_addr)))
998 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
1000 if (ptr->flags & IFF_PORTSEL) {
1001 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
1002 if (ptr->flags & IFF_AUTOMEDIA)
1010 printf(" %s addr:%s ", ap->name,
1011 ap->sprint(&ptr->addr, 1));
1012 if (ptr->flags & IFF_POINTOPOINT) {
1013 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
1015 if (ptr->flags & IFF_BROADCAST) {
1016 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
1018 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
1024 #define IPV6_ADDR_ANY 0x0000U
1026 #define IPV6_ADDR_UNICAST 0x0001U
1027 #define IPV6_ADDR_MULTICAST 0x0002U
1028 #define IPV6_ADDR_ANYCAST 0x0004U
1030 #define IPV6_ADDR_LOOPBACK 0x0010U
1031 #define IPV6_ADDR_LINKLOCAL 0x0020U
1032 #define IPV6_ADDR_SITELOCAL 0x0040U
1034 #define IPV6_ADDR_COMPATv4 0x0080U
1036 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1038 #define IPV6_ADDR_MAPPED 0x1000U
1039 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1041 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1043 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1044 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1045 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1046 &dad_status, devname) != EOF) {
1047 if (!strcmp(devname, ptr->name)) {
1048 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1049 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1050 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1051 inet_pton(AF_INET6, addr6,
1052 (struct sockaddr *) &sap.sin6_addr);
1053 sap.sin6_family = AF_INET6;
1054 printf(" inet6 addr: %s/%d",
1055 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1058 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1062 case IPV6_ADDR_LINKLOCAL:
1065 case IPV6_ADDR_SITELOCAL:
1068 case IPV6_ADDR_COMPATv4:
1071 case IPV6_ADDR_LOOPBACK:
1085 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1087 if (ptr->flags == 0) {
1088 printf("[NO FLAGS] ");
1092 if (ptr->flags & ife_print_flags_mask[i]) {
1093 printf(ife_print_flags_strs[i]);
1095 } while (ife_print_flags_mask[++i]);
1098 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1099 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
1100 #ifdef SIOCSKEEPALIVE
1101 if (ptr->outfill || ptr->keepalive)
1102 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
1106 /* If needed, display the interface statistics. */
1108 if (ptr->statistics_valid) {
1109 /* XXX: statistics are currently only printed for the primary address,
1110 * not for the aliases, although strictly speaking they're shared
1115 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
1116 ptr->stats.rx_packets, ptr->stats.rx_errors,
1117 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1118 ptr->stats.rx_frame_errors);
1120 printf(" compressed:%lu\n",
1121 ptr->stats.rx_compressed);
1123 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
1124 ptr->stats.tx_packets, ptr->stats.tx_errors,
1125 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1126 ptr->stats.tx_carrier_errors);
1127 printf(" collisions:%lu ", ptr->stats.collisions);
1129 printf("compressed:%lu ", ptr->stats.tx_compressed);
1130 if (ptr->tx_queue_len != -1)
1131 printf("txqueuelen:%d ", ptr->tx_queue_len);
1133 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1134 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1138 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1139 ptr->map.base_addr)) {
1142 printf("Interrupt:%d ", ptr->map.irq);
1143 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
1145 printf("Base address:0x%lx ",
1146 (unsigned long) ptr->map.base_addr);
1147 if (ptr->map.mem_start) {
1148 printf("Memory:%lx-%lx ", ptr->map.mem_start,
1152 printf("DMA chan:%x ", ptr->map.dma);
1159 static int do_if_print(struct interface *ife, void *cookie)
1161 int *opt_a = (int *) cookie;
1164 res = do_if_fetch(ife);
1166 if ((ife->flags & IFF_UP) || *opt_a)
1172 static struct interface *lookup_interface(char *name)
1174 struct interface *ife = NULL;
1176 if (if_readlist_proc(name) < 0)
1178 ife = add_interface(name);
1182 /* for ipv4 add/del modes */
1183 static int if_print(char *ifname)
1188 res = for_all_interfaces(do_if_print, &interface_opt_a);
1190 struct interface *ife;
1192 ife = lookup_interface(ifname);
1193 res = do_if_fetch(ife);
1200 int display_interfaces(char *ifname);
1201 int display_interfaces(char *ifname)
1205 /* Create a channel to the NET kernel. */
1206 if ((skfd = sockets_open(0)) < 0) {
1207 bb_perror_msg_and_die("socket");
1210 /* Do we have to show the current setup? */
1211 status = if_print(ifname);
1212 #ifdef CONFIG_FEATURE_CLEAN_UP