1 /* vi: set sw=4 ts=4: */
3 * stolen from net-tools-1.59 and stripped down for busybox by
4 * Erik Andersen <andersen@codepoet.org>
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
8 * Added print_bytes_scaled function to reduce code size.
9 * Added some (potentially) missing defines.
10 * Improved display support for -a and for a named interface.
12 * -----------------------------------------------------------
14 * ifconfig This file contains an implementation of the command
15 * that either displays or sets the characteristics of
16 * one or more of the system's networking interfaces.
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 source tree.
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)
34 #include "inet_common.h"
36 #include <net/if_arp.h>
37 #ifdef HAVE_NET_ETHERNET_H
38 # include <net/ethernet.h>
41 #if ENABLE_FEATURE_HWIB
42 /* #include <linux/if_infiniband.h> */
43 # undef INFINIBAND_ALEN
44 # define INFINIBAND_ALEN 20
47 #if ENABLE_FEATURE_IPV6
48 # define HAVE_AFINET6 1
53 #define _PATH_PROCNET_DEV "/proc/net/dev"
54 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
59 * This is from linux/include/net/ipv6.h
62 struct in6_addr ifr6_addr;
63 uint32_t ifr6_prefixlen;
64 unsigned int ifr6_ifindex;
67 #endif /* HAVE_AFINET6 */
69 /* Defines for glibc2.0 users. */
71 # define SIOCSIFTXQLEN 0x8943
72 # define SIOCGIFTXQLEN 0x8942
75 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
77 # define ifr_qlen ifr_ifru.ifru_mtu
80 #ifndef HAVE_TXQUEUELEN
81 # define HAVE_TXQUEUELEN 1
85 # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
88 /* Display an Internet socket address. */
89 static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
91 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
93 return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00));
96 #ifdef UNUSED_AND_BUGGY
97 static int INET_getsock(char *bufp, struct sockaddr *sap)
102 struct sockaddr_in *sock_in;
104 sock_in = (struct sockaddr_in *) sap;
105 sock_in->sin_family = AF_INET;
106 sock_in->sin_port = 0;
110 for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
113 if ((unsigned)(*sp - 'A') <= 5)
114 bp[i] |= (int) (*sp - ('A' - 10));
115 else if (isdigit(*sp))
116 bp[i] |= (int) (*sp - '0');
124 if ((unsigned)(*sp - 'A') <= 5)
125 bp[i] |= (int) (*sp - ('A' - 10));
126 else if (isdigit(*sp))
127 bp[i] |= (int) (*sp - '0');
133 sock_in->sin_addr.s_addr = htonl(val);
139 static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
141 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
145 return (INET_getsock(bufp, sap));
147 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
149 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
154 static const struct aftype inet_aftype = {
156 .title = "DARPA Internet",
159 .sprint = INET_sprint,
165 /* Display an Internet socket address. */
166 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
167 static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
169 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
171 return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric));
175 static int INET6_getsock(char *bufp, struct sockaddr *sap)
177 struct sockaddr_in6 *sin6;
179 sin6 = (struct sockaddr_in6 *) sap;
180 sin6->sin6_family = AF_INET6;
183 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
190 static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
192 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
196 return (INET6_getsock(bufp, sap));
198 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
203 static const struct aftype inet6_aftype = {
207 .alen = sizeof(struct in6_addr),
208 .sprint = INET6_sprint,
209 .input = INET6_input,
212 #endif /* HAVE_AFINET6 */
214 /* Display an UNSPEC address. */
215 static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
221 buff = auto_string(xmalloc(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++);
228 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
233 /* Display an UNSPEC socket address. */
234 static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
236 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
238 return UNSPEC_print((unsigned char *)sap->sa_data);
241 static const struct aftype unspec_aftype = {
246 .print = UNSPEC_print,
247 .sprint = UNSPEC_sprint,
250 static const struct aftype *const aftypes[] = {
259 /* Check our protocol family table for this family. */
260 const struct aftype* FAST_FUNC get_aftype(const char *name)
262 const struct aftype *const *afp;
265 while (*afp != NULL) {
266 if (strcmp((*afp)->name, name) == 0)
273 /* Check our protocol family table for this family. */
274 static const struct aftype *get_afntype(int af)
276 const struct aftype *const *afp;
279 while (*afp != NULL) {
280 if ((*afp)->af == af)
287 struct user_net_device_stats {
288 unsigned long long rx_packets; /* total packets received */
289 unsigned long long tx_packets; /* total packets transmitted */
290 unsigned long long rx_bytes; /* total bytes received */
291 unsigned long long tx_bytes; /* total bytes transmitted */
292 unsigned long rx_errors; /* bad packets received */
293 unsigned long tx_errors; /* packet transmit problems */
294 unsigned long rx_dropped; /* no space in linux buffers */
295 unsigned long tx_dropped; /* no space available in linux */
296 unsigned long rx_multicast; /* multicast packets received */
297 unsigned long rx_compressed;
298 unsigned long tx_compressed;
299 unsigned long collisions;
301 /* detailed rx_errors: */
302 unsigned long rx_length_errors;
303 unsigned long rx_over_errors; /* receiver ring buff overflow */
304 unsigned long rx_crc_errors; /* recved pkt with crc error */
305 unsigned long rx_frame_errors; /* recv'd frame alignment error */
306 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
307 unsigned long rx_missed_errors; /* receiver missed packet */
308 /* detailed tx_errors */
309 unsigned long tx_aborted_errors;
310 unsigned long tx_carrier_errors;
311 unsigned long tx_fifo_errors;
312 unsigned long tx_heartbeat_errors;
313 unsigned long tx_window_errors;
317 struct interface *next, *prev;
318 char name[IFNAMSIZ]; /* interface name */
319 short type; /* if type */
320 short flags; /* various flags */
321 int metric; /* routing metric */
322 int mtu; /* MTU value */
323 int tx_queue_len; /* transmit queue length */
324 struct ifmap map; /* hardware setup */
325 struct sockaddr addr; /* IP address */
326 struct sockaddr dstaddr; /* P-P IP address */
327 struct sockaddr broadaddr; /* IP broadcast address */
328 struct sockaddr netmask; /* IP network mask */
330 char hwaddr[32]; /* HW address */
331 int statistics_valid;
332 struct user_net_device_stats stats; /* statistics */
333 int keepalive; /* keepalive value for SLIP */
334 int outfill; /* outfill value for SLIP */
338 smallint interface_opt_a; /* show all interfaces */
340 static struct interface *int_list, *int_last;
344 /* like strcmp(), but knows about numbers */
345 except that the freshly added calls to xatoul() brf on ethernet aliases with
346 uClibc with e.g.: ife->name='lo' name='eth0:1'
347 static int nstrcmp(const char *a, const char *b)
349 const char *a_ptr = a;
350 const char *b_ptr = b;
356 if (!isdigit(*a) && isdigit(*(a+1))) {
364 if (isdigit(*a) && isdigit(*b)) {
365 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
371 static struct interface *add_interface(char *name)
373 struct interface *ife, **nextp, *new;
375 for (ife = int_last; ife; ife = ife->prev) {
376 int n = /*n*/strcmp(ife->name, name);
384 new = xzalloc(sizeof(*new));
385 strncpy_IFNAMSIZ(new->name, name);
386 nextp = ife ? &ife->next : &int_list;
390 new->next->prev = new;
397 static char *get_name(char *name, char *p)
399 /* Extract <name> from nul-terminated p where p matches
400 * <name>: after leading whitespace.
401 * If match is not made, set name empty and return unchanged p
404 char *namestart = skip_whitespace(p);
407 while (*nameend && *nameend != ':' && !isspace(*nameend))
409 if (*nameend == ':') {
410 if ((nameend - namestart) < IFNAMSIZ) {
411 memcpy(name, namestart, nameend - namestart);
412 name[nameend - namestart] = '\0';
415 /* Interface name too large */
419 /* trailing ':' not found - return empty */
425 /* If scanf supports size qualifiers for %n conversions, then we can
426 * use a modified fmt that simply stores the position in the fields
427 * having no associated fields in the proc string. Of course, we need
428 * to zero them again when we're done. But that is smaller than the
429 * old approach of multiple scanf occurrences with large numbers of
432 /* static const char *const ss_fmt[] = { */
433 /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
434 /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
435 /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
438 /* Lie about the size of the int pointed to for %n. */
439 #if INT_MAX == LONG_MAX
440 static const char *const ss_fmt[] = {
441 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
442 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
443 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
446 static const char *const ss_fmt[] = {
447 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
448 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
449 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
454 static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
456 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
458 sscanf(bp, ss_fmt[procnetdev_vsn],
459 &ife->stats.rx_bytes, /* missing for 0 */
460 &ife->stats.rx_packets,
461 &ife->stats.rx_errors,
462 &ife->stats.rx_dropped,
463 &ife->stats.rx_fifo_errors,
464 &ife->stats.rx_frame_errors,
465 &ife->stats.rx_compressed, /* missing for <= 1 */
466 &ife->stats.rx_multicast, /* missing for <= 1 */
467 &ife->stats.tx_bytes, /* missing for 0 */
468 &ife->stats.tx_packets,
469 &ife->stats.tx_errors,
470 &ife->stats.tx_dropped,
471 &ife->stats.tx_fifo_errors,
472 &ife->stats.collisions,
473 &ife->stats.tx_carrier_errors,
474 &ife->stats.tx_compressed /* missing for <= 1 */
477 if (procnetdev_vsn <= 1) {
478 if (procnetdev_vsn == 0) {
479 ife->stats.rx_bytes = 0;
480 ife->stats.tx_bytes = 0;
482 ife->stats.rx_multicast = 0;
483 ife->stats.rx_compressed = 0;
484 ife->stats.tx_compressed = 0;
488 static int procnetdev_version(char *buf)
490 if (strstr(buf, "compressed"))
492 if (strstr(buf, "bytes"))
497 static int if_readconf(void)
507 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
509 skfd = socket(AF_INET, SOCK_DGRAM, 0);
511 bb_perror_msg("error: no inet socket available");
516 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
517 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
519 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
522 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
523 /* assume it overflowed and try again */
531 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
532 add_interface(ifr->ifr_name);
543 static int if_readlist_proc(char *target)
545 static smallint proc_read;
549 struct interface *ife;
550 int err, procnetdev_vsn;
557 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
559 return if_readconf();
561 fgets(buf, sizeof buf, fh); /* eat line */
562 fgets(buf, sizeof buf, fh);
564 procnetdev_vsn = procnetdev_version(buf);
567 while (fgets(buf, sizeof buf, fh)) {
570 s = get_name(name, buf);
571 ife = add_interface(name);
572 get_dev_fields(s, ife, procnetdev_vsn);
573 ife->statistics_valid = 1;
574 if (target && strcmp(target, name) == 0)
578 bb_perror_msg(_PATH_PROCNET_DEV);
586 static int if_readlist(void)
588 int err = if_readlist_proc(NULL);
589 /* Needed in order to get ethN:M aliases */
595 /* Fetch the interface configuration from the kernel. */
596 static int if_fetch(struct interface *ife)
599 char *ifname = ife->name;
602 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
604 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
605 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
609 ife->flags = ifr.ifr_flags;
611 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
612 memset(ife->hwaddr, 0, 32);
613 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
614 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
616 ife->type = ifr.ifr_hwaddr.sa_family;
618 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
620 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
621 ife->metric = ifr.ifr_metric;
623 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
625 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
626 ife->mtu = ifr.ifr_mtu;
628 memset(&ife->map, 0, sizeof(struct ifmap));
630 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
631 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
632 ife->map = ifr.ifr_map;
635 #ifdef HAVE_TXQUEUELEN
636 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
637 ife->tx_queue_len = -1; /* unknown value */
638 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
639 ife->tx_queue_len = ifr.ifr_qlen;
641 ife->tx_queue_len = -1; /* unknown value */
644 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
645 ifr.ifr_addr.sa_family = AF_INET;
646 memset(&ife->addr, 0, sizeof(struct sockaddr));
647 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
649 ife->addr = ifr.ifr_addr;
650 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
651 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
652 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
653 ife->dstaddr = ifr.ifr_dstaddr;
655 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
656 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
657 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
658 ife->broadaddr = ifr.ifr_broadaddr;
660 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
661 memset(&ife->netmask, 0, sizeof(struct sockaddr));
662 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
663 ife->netmask = ifr.ifr_netmask;
670 static int do_if_fetch(struct interface *ife)
672 if (if_fetch(ife) < 0) {
675 if (errno == ENODEV) {
676 /* Give better error message for this case. */
677 errmsg = "Device not found";
679 errmsg = strerror(errno);
681 bb_error_msg("%s: error fetching interface information: %s",
688 static const struct hwtype unspec_hwtype = {
692 .print = UNSPEC_print
695 static const struct hwtype loop_hwtype = {
697 .title = "Local Loopback",
698 .type = ARPHRD_LOOPBACK
701 /* Display an Ethernet address in readable format. */
702 static char* FAST_FUNC ether_print(unsigned char *ptr)
705 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
706 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]
708 return auto_string(buff);
711 static const struct hwtype ether_hwtype = {
714 .type = ARPHRD_ETHER,
716 .print = ether_print,
720 static const struct hwtype ppp_hwtype = {
722 .title = "Point-to-Point Protocol",
726 #if ENABLE_FEATURE_IPV6
727 static const struct hwtype sit_hwtype = {
729 .title = "IPv6-in-IPv4",
731 .print = UNSPEC_print,
732 .suppress_null_addr = 1
735 #if ENABLE_FEATURE_HWIB
736 static const struct hwtype ib_hwtype = {
737 .name = "infiniband",
738 .title = "InfiniBand",
739 .type = ARPHRD_INFINIBAND,
740 .alen = INFINIBAND_ALEN,
741 .print = UNSPEC_print,
747 static const struct hwtype *const hwtypes[] = {
752 #if ENABLE_FEATURE_IPV6
755 #if ENABLE_FEATURE_HWIB
762 static const char *const if_port_text[] = {
763 /* Keep in step with <linux/netdevice.h> */
775 /* Check our hardware type table for this type. */
776 const struct hwtype* FAST_FUNC get_hwtype(const char *name)
778 const struct hwtype *const *hwp;
781 while (*hwp != NULL) {
782 if (strcmp((*hwp)->name, name) == 0)
789 /* Check our hardware type table for this type. */
790 const struct hwtype* FAST_FUNC get_hwntype(int type)
792 const struct hwtype *const *hwp;
795 while (*hwp != NULL) {
796 if ((*hwp)->type == type)
803 /* return 1 if address is all zeros */
804 static int hw_null_address(const struct hwtype *hw, void *ap)
807 unsigned char *address = (unsigned char *) ap;
809 for (i = 0; i < hw->alen; i++)
815 static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
817 static void print_bytes_scaled(unsigned long long ull, const char *end)
819 unsigned long long int_part;
821 unsigned int frac_part;
829 if (int_part >= 1024) {
830 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
832 ext += 3; /* KiB, MiB, GiB, TiB */
837 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
842 #define IPV6_ADDR_ANY 0x0000U
844 #define IPV6_ADDR_UNICAST 0x0001U
845 #define IPV6_ADDR_MULTICAST 0x0002U
846 #define IPV6_ADDR_ANYCAST 0x0004U
848 #define IPV6_ADDR_LOOPBACK 0x0010U
849 #define IPV6_ADDR_LINKLOCAL 0x0020U
850 #define IPV6_ADDR_SITELOCAL 0x0040U
852 #define IPV6_ADDR_COMPATv4 0x0080U
854 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
856 #define IPV6_ADDR_MAPPED 0x1000U
857 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
860 static void ife_print6(struct interface *ptr)
863 char addr6[40], devname[21];
864 struct sockaddr_in6 sap;
865 int plen, scope, dad_status, if_idx;
868 f = fopen_for_read(_PATH_PROCNET_IFINET6);
873 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
874 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
875 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
876 &dad_status, devname) != EOF
878 if (strcmp(devname, ptr->name) == 0) {
879 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
880 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
881 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
882 memset(&sap, 0, sizeof(sap));
883 inet_pton(AF_INET6, addr6,
884 (struct sockaddr *) &sap.sin6_addr);
885 sap.sin6_family = AF_INET6;
886 printf(" inet6 addr: %s/%d",
887 INET6_sprint((struct sockaddr *) &sap, 1),
890 switch (scope & IPV6_ADDR_SCOPE_MASK) {
894 case IPV6_ADDR_LINKLOCAL:
897 case IPV6_ADDR_SITELOCAL:
900 case IPV6_ADDR_COMPATv4:
903 case IPV6_ADDR_LOOPBACK:
914 #define ife_print6(a) ((void)0)
917 static void ife_print(struct interface *ptr)
919 const struct aftype *ap;
920 const struct hwtype *hw;
922 int can_compress = 0;
924 ap = get_afntype(ptr->addr.sa_family);
930 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
933 hw = get_hwntype(hf);
935 hw = get_hwntype(-1);
937 printf("%-9s Link encap:%s ", ptr->name, hw->title);
938 /* For some hardware types (eg Ash, ATM) we don't print the
939 hardware address if it's null. */
940 if (hw->print != NULL
941 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
943 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
946 if (ptr->flags & IFF_PORTSEL) {
947 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
948 if (ptr->flags & IFF_AUTOMEDIA)
955 printf(" %s addr:%s ", ap->name,
956 ap->sprint(&ptr->addr, 1));
957 if (ptr->flags & IFF_POINTOPOINT) {
958 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
960 if (ptr->flags & IFF_BROADCAST) {
961 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
963 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
969 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
971 if (ptr->flags == 0) {
972 printf("[NO FLAGS] ");
974 static const char ife_print_flags_strs[] ALIGN1 =
992 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
1010 const unsigned short *mask = ife_print_flags_mask;
1011 const char *str = ife_print_flags_strs;
1013 if (ptr->flags & *mask) {
1017 str += strlen(str) + 1;
1021 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1022 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
1023 #ifdef SIOCSKEEPALIVE
1024 if (ptr->outfill || ptr->keepalive)
1025 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
1029 /* If needed, display the interface statistics. */
1031 if (ptr->statistics_valid) {
1032 /* XXX: statistics are currently only printed for the primary address,
1033 * not for the aliases, although strictly speaking they're shared
1038 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
1039 ptr->stats.rx_packets, ptr->stats.rx_errors,
1040 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1041 ptr->stats.rx_frame_errors);
1043 printf(" compressed:%lu\n",
1044 ptr->stats.rx_compressed);
1046 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
1047 ptr->stats.tx_packets, ptr->stats.tx_errors,
1048 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1049 ptr->stats.tx_carrier_errors);
1050 printf(" collisions:%lu ", ptr->stats.collisions);
1052 printf("compressed:%lu ", ptr->stats.tx_compressed);
1053 if (ptr->tx_queue_len != -1)
1054 printf("txqueuelen:%d ", ptr->tx_queue_len);
1056 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1057 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1060 if (ptr->map.irq || ptr->map.mem_start
1061 || ptr->map.dma || ptr->map.base_addr
1065 printf("Interrupt:%d ", ptr->map.irq);
1066 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */
1067 printf("Base address:0x%lx ",
1068 (unsigned long) ptr->map.base_addr);
1069 if (ptr->map.mem_start) {
1070 printf("Memory:%lx-%lx ", ptr->map.mem_start,
1074 printf("DMA chan:%x ", ptr->map.dma);
1080 static int do_if_print(struct interface *ife) /*, int *opt_a)*/
1084 res = do_if_fetch(ife);
1086 if ((ife->flags & IFF_UP) || interface_opt_a)
1092 static struct interface *lookup_interface(char *name)
1094 struct interface *ife = NULL;
1096 if (if_readlist_proc(name) < 0)
1098 ife = add_interface(name);
1103 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1106 struct interface *ife;
1108 if (!int_list && (if_readlist() < 0))
1110 for (ife = int_list; ife; ife = ife->next) {
1111 int err = doit(ife, cookie);
1119 /* for ipv4 add/del modes */
1120 static int if_print(char *ifname)
1122 struct interface *ife;
1126 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1127 if (!int_list && (if_readlist() < 0))
1129 for (ife = int_list; ife; ife = ife->next) {
1130 int err = do_if_print(ife); /*, &interface_opt_a);*/
1136 ife = lookup_interface(ifname);
1137 res = do_if_fetch(ife);
1143 #if ENABLE_FEATURE_HWIB
1144 /* Input an Infiniband address and convert to binary. */
1145 int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
1147 sap->sa_family = ib_hwtype.type;
1148 //TODO: error check?
1149 hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN);
1151 fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data));
1157 int FAST_FUNC display_interfaces(char *ifname)
1161 status = if_print(ifname);
1163 return (status < 0); /* status < 0 == 1 -- error */