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 * Pruned unused code using KEEP_UNUSED define.
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.
18 * Version: $Id: interface.c,v 1.25 2004/08/26 21:45:21 andersen Exp $
20 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
21 * and others. Copyright 1993 MicroWalt Corporation
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * Patched to support 'add' and 'del' keywords for INET(4) addresses
30 * by Mrs. Brisby <mrs.brisby@nimh.org>
32 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
33 * - gettext instead of catgets for i18n
34 * 10/1998 - Andi Kleen. Use interface list primitives.
35 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
36 * (default AF was wrong)
39 /* #define KEEP_UNUSED */
50 * Device Hardware types.
53 #define HAVE_HWETHER 1
58 #include "inet_common.h"
66 #include <sys/ioctl.h>
67 #include <sys/types.h>
69 #include <net/if_arp.h>
72 #ifdef CONFIG_FEATURE_IPV6
73 # define HAVE_AFINET6 1
78 #define _PATH_PROCNET_DEV "/proc/net/dev"
79 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
80 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
81 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
84 #include <net/if_slip.h>
91 * This is in linux/include/net/ipv6.h.
95 struct in6_addr ifr6_addr;
96 uint32_t ifr6_prefixlen;
97 unsigned int ifr6_ifindex;
102 #endif /* HAVE_AFINET6 */
104 /* Defines for glibc2.0 users. */
105 #ifndef SIOCSIFTXQLEN
106 #define SIOCSIFTXQLEN 0x8943
107 #define SIOCGIFTXQLEN 0x8942
110 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
112 #define ifr_qlen ifr_ifru.ifru_mtu
115 #ifndef HAVE_TXQUEUELEN
116 #define HAVE_TXQUEUELEN 1
120 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
123 /* This structure defines protocol families and their handlers. */
129 char *(*print) (unsigned char *);
130 char *(*sprint) (struct sockaddr *, int numeric);
131 int (*input) (int type, char *bufp, struct sockaddr *);
132 void (*herror) (char *text);
133 int (*rprint) (int options);
134 int (*rinput) (int typ, int ext, char **argv);
137 int (*getmask) (char *src, struct sockaddr * mask, char *name);
146 static int flag_inet;
148 static struct aftrans_t {
155 "ip", "inet", &flag_inet},
158 "ip6", "inet6", &flag_inet6},
161 "inet", "inet", &flag_inet},
164 "inet6", "inet6", &flag_inet6},
167 "unix", "unix", &flag_unx}, {
168 "tcpip", "inet", &flag_inet},
173 static char afname[256] = "";
174 #endif /* KEEP_UNUSED */
178 /* Display a UNIX domain address. */
179 static char *UNIX_print(unsigned char *ptr)
185 /* Display a UNIX domain address. */
186 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
190 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
191 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
192 return (UNIX_print(sap->sa_data));
196 static struct aftype unix_aftype = {
197 "unix", "UNIX Domain", AF_UNIX, 0,
198 UNIX_print, UNIX_sprint, NULL, NULL,
203 #endif /* HAVE_AFUNIX */
208 static void INET_reserror(char *text)
213 /* Display an Internet socket address. */
214 static char *INET_print(unsigned char *ptr)
216 return (inet_ntoa((*(struct in_addr *) ptr)));
218 #endif /* KEEP_UNUSED */
220 /* Display an Internet socket address. */
221 static char *INET_sprint(struct sockaddr *sap, int numeric)
223 static char buff[128];
225 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
226 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
228 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
229 numeric, 0xffffff00) != 0)
236 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
237 unsigned int netmask)
239 static char buff[128];
241 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
242 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
243 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
244 numeric, netmask) != 0)
249 static int INET_getsock(char *bufp, struct sockaddr *sap)
251 char *sp = bufp, *bp;
254 struct sockaddr_in *sin;
256 sin = (struct sockaddr_in *) sap;
257 sin->sin_family = AF_INET;
262 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
265 if ((*sp >= 'A') && (*sp <= 'F'))
266 bp[i] |= (int) (*sp - 'A') + 10;
267 else if ((*sp >= '0') && (*sp <= '9'))
268 bp[i] |= (int) (*sp - '0');
276 if ((*sp >= 'A') && (*sp <= 'F'))
277 bp[i] |= (int) (*sp - 'A') + 10;
278 else if ((*sp >= '0') && (*sp <= '9'))
279 bp[i] |= (int) (*sp - '0');
285 sin->sin_addr.s_addr = htonl(val);
290 static int INET_input(int type, char *bufp, struct sockaddr *sap)
294 return (INET_getsock(bufp, sap));
296 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
298 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
302 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
304 struct sockaddr_in *mask = (struct sockaddr_in *) m;
308 if ((slash = strchr(adr, '/')) == NULL)
312 prefix = strtoul(slash, &end, 0);
317 sprintf(name, "/%d", prefix);
319 mask->sin_family = AF_INET;
320 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
323 #endif /* KEEP_UNUSED */
325 static struct aftype inet_aftype = {
326 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
327 NULL /* UNUSED INET_print */ , INET_sprint,
328 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
329 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
330 NULL /* UNUSED INET_getnetmask */ ,
335 #endif /* HAVE_AFINET */
340 static void INET6_reserror(char *text)
345 /* Display an Internet socket address. */
346 static char *INET6_print(unsigned char *ptr)
348 static char name[80];
350 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
353 #endif /* KEEP_UNUSED */
355 /* Display an Internet socket address. */
356 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
357 static char *INET6_sprint(struct sockaddr *sap, int numeric)
359 static char buff[128];
361 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
362 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
364 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
365 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
370 static int INET6_getsock(char *bufp, struct sockaddr *sap)
372 struct sockaddr_in6 *sin6;
374 sin6 = (struct sockaddr_in6 *) sap;
375 sin6->sin6_family = AF_INET6;
378 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
384 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
388 return (INET6_getsock(bufp, sap));
390 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
393 #endif /* KEEP_UNUSED */
395 static struct aftype inet6_aftype = {
396 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
397 NULL /* UNUSED INET6_print */ , INET6_sprint,
398 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
399 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
400 NULL /* UNUSED INET6_getnetmask */ ,
405 #endif /* HAVE_AFINET6 */
407 /* Display an UNSPEC address. */
408 static char *UNSPEC_print(unsigned char *ptr)
410 static char buff[sizeof(struct sockaddr) * 3 + 1];
415 for (i = 0; i < sizeof(struct sockaddr); i++) {
416 /* careful -- not every libc's sprintf returns # bytes written */
417 sprintf(pos, "%02X-", (*ptr++ & 0377));
420 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
425 /* Display an UNSPEC socket address. */
426 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
430 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
431 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
432 return (UNSPEC_print((unsigned char *)sap->sa_data));
435 static struct aftype unspec_aftype = {
436 "unspec", "UNSPEC", AF_UNSPEC, 0,
437 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
441 static struct aftype * const aftypes[] = {
456 static short sVafinit = 0;
460 unspec_aftype.title = _("UNSPEC");
462 unix_aftype.title = _("UNIX Domain");
465 inet_aftype.title = _("DARPA Internet");
468 inet6_aftype.title = _("IPv6");
473 static int aftrans_opt(const char *arg)
475 struct aftrans_t *paft;
479 safe_strncpy(buf, arg, sizeof(buf));
485 tmp2 = strchr(tmp1, ',');
491 for (paft = aftrans; paft->alias; paft++) {
492 if (strcmp(tmp1, paft->alias))
494 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
495 bb_error_msg(_("Too many address family arguments."));
502 strcat(afname, paft->name);
506 bb_error_msg(_("Unknown address family `%s'."), tmp1);
515 /* set the default AF list from the program name or a constant value */
516 static void aftrans_def(char *tool, char *argv0, char *dflt)
521 strcpy(afname, dflt);
523 if (!(tmp = strrchr(argv0, '/')))
524 tmp = argv0; /* no slash?! */
528 if (!(buf = strdup(tmp)))
531 if (strlen(tool) >= strlen(tmp)) {
535 tmp = buf + (strlen(tmp) - strlen(tool));
537 if (strcmp(tmp, tool) != 0) {
542 if ((tmp = strchr(buf, '_')))
546 if (aftrans_opt(buf))
552 /* Check our protocol family table for this family. */
553 static struct aftype *get_aftype(const char *name)
555 struct aftype * const *afp;
560 #endif /* KEEP_UNUSED */
563 while (*afp != NULL) {
564 if (!strcmp((*afp)->name, name))
568 if (strchr(name, ','))
569 bb_error_msg(_("Please don't supply more than one address family."));
572 #endif /* KEEP_UNUSED */
574 /* Check our protocol family table for this family. */
575 static struct aftype *get_afntype(int af)
577 struct aftype * const *afp;
582 #endif /* KEEP_UNUSED */
585 while (*afp != NULL) {
586 if ((*afp)->af == af)
593 /* Check our protocol family table for this family and return its socket */
594 static int get_socket_for_af(int af)
596 struct aftype * const *afp;
601 #endif /* KEEP_UNUSED */
604 while (*afp != NULL) {
605 if ((*afp)->af == af)
613 /* type: 0=all, 1=getroute */
614 static void print_aflist(int type)
618 struct aftype * const *afp;
623 #endif /* KEEP_UNUSED */
626 while (*afp != NULL) {
627 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
631 if ((count % 3) == 0)
632 fprintf(stderr, count ? "\n " : " ");
636 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
640 fprintf(stderr, "\n");
642 #endif /* KEEP_UNUSED */
644 struct user_net_device_stats {
645 unsigned long long rx_packets; /* total packets received */
646 unsigned long long tx_packets; /* total packets transmitted */
647 unsigned long long rx_bytes; /* total bytes received */
648 unsigned long long tx_bytes; /* total bytes transmitted */
649 unsigned long rx_errors; /* bad packets received */
650 unsigned long tx_errors; /* packet transmit problems */
651 unsigned long rx_dropped; /* no space in linux buffers */
652 unsigned long tx_dropped; /* no space available in linux */
653 unsigned long rx_multicast; /* multicast packets received */
654 unsigned long rx_compressed;
655 unsigned long tx_compressed;
656 unsigned long collisions;
658 /* detailed rx_errors: */
659 unsigned long rx_length_errors;
660 unsigned long rx_over_errors; /* receiver ring buff overflow */
661 unsigned long rx_crc_errors; /* recved pkt with crc error */
662 unsigned long rx_frame_errors; /* recv'd frame alignment error */
663 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
664 unsigned long rx_missed_errors; /* receiver missed packet */
665 /* detailed tx_errors */
666 unsigned long tx_aborted_errors;
667 unsigned long tx_carrier_errors;
668 unsigned long tx_fifo_errors;
669 unsigned long tx_heartbeat_errors;
670 unsigned long tx_window_errors;
674 struct interface *next, *prev;
675 char name[IFNAMSIZ]; /* interface name */
676 short type; /* if type */
677 short flags; /* various flags */
678 int metric; /* routing metric */
679 int mtu; /* MTU value */
680 int tx_queue_len; /* transmit queue length */
681 struct ifmap map; /* hardware setup */
682 struct sockaddr addr; /* IP address */
683 struct sockaddr dstaddr; /* P-P IP address */
684 struct sockaddr broadaddr; /* IP broadcast address */
685 struct sockaddr netmask; /* IP network mask */
687 char hwaddr[32]; /* HW address */
688 int statistics_valid;
689 struct user_net_device_stats stats; /* statistics */
690 int keepalive; /* keepalive value for SLIP */
691 int outfill; /* outfill value for SLIP */
695 int interface_opt_a = 0; /* show all interfaces */
698 static int opt_i = 0; /* show the statistics */
699 static int opt_v = 0; /* debugging output flag */
701 static int addr_family = 0; /* currently selected AF */
702 #endif /* KEEP_UNUSED */
704 static struct interface *int_list, *int_last;
705 static int skfd = -1; /* generic raw socket desc. */
708 static int sockets_open(int family)
710 struct aftype * const *aft;
712 static int force = -1;
716 if (get_kernel_revision() < KRELEASE(2, 1, 0))
718 if (access("/proc/net", R_OK))
721 for (aft = aftypes; *aft; aft++) {
722 struct aftype *af = *aft;
723 int type = SOCK_DGRAM;
725 if (af->af == AF_UNSPEC)
727 if (family && family != af->af)
733 /* Check some /proc file first to not stress kmod */
734 if (!family && !force && af->flag_file) {
735 if (access(af->flag_file, R_OK))
738 af->fd = socket(af->af, type, 0);
743 bb_error_msg(_("No usable address families found."));
748 #ifdef CONFIG_FEATURE_CLEAN_UP
749 static void sockets_close(void)
751 struct aftype * const *aft;
752 for (aft = aftypes; *aft != NULL; aft++) {
753 struct aftype *af = *aft;
762 /* like strcmp(), but knows about numbers */
763 static int nstrcmp(const char *a, const char *b)
765 const char *a_ptr = a;
766 const char *b_ptr = b;
772 if (!isdigit(*a) && isdigit(*(a+1))) {
780 if (isdigit(*a) && isdigit(*b)) {
781 return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1;
786 static struct interface *add_interface(char *name)
788 struct interface *ife, **nextp, *new;
790 for (ife = int_last; ife; ife = ife->prev) {
791 int n = nstrcmp(ife->name, name);
799 safe_strncpy(new->name, name, IFNAMSIZ);
800 nextp = ife ? &ife->next : &int_list;
804 new->next->prev = new;
812 static int if_readconf(void)
820 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
822 skfd2 = get_socket_for_af(AF_INET);
824 bb_perror_msg(("warning: no inet socket available"));
825 /* Try to soldier on with whatever socket we can get hold of. */
826 skfd2 = sockets_open(0);
833 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
834 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
836 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
837 perror("SIOCGIFCONF");
840 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
841 /* assume it overflowed and try again */
849 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
850 add_interface(ifr->ifr_name);
860 static char *get_name(char *name, char *p)
862 /* Extract <name>[:<alias>] from nul-terminated p where p matches
863 <name>[:<alias>]: after leading whitespace.
864 If match is not made, set name empty and return unchanged p */
865 int namestart=0, nameend=0, aliasend;
866 while (isspace(p[namestart]))
869 while (p[nameend] && p[nameend]!=':' && !isspace(p[nameend]))
871 if (p[nameend]==':') {
873 while (p[aliasend] && isdigit(p[aliasend]))
875 if (p[aliasend]==':') {
878 if ((nameend-namestart)<IFNAMSIZ) {
879 memcpy(name,&p[namestart],nameend-namestart);
880 name[nameend-namestart]='\0';
883 /* Interface name too large */
887 /* first ':' not found - return empty */
893 /* If scanf supports size qualifiers for %n conversions, then we can
894 * use a modified fmt that simply stores the position in the fields
895 * having no associated fields in the proc string. Of course, we need
896 * to zero them again when we're done. But that is smaller than the
897 * old approach of multiple scanf occurrences with large numbers of
900 /* static const char * const ss_fmt[] = { */
901 /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
902 /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
903 /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
906 /* Lie about the size of the int pointed to for %n. */
907 #if INT_MAX == LONG_MAX
908 static const char * const ss_fmt[] = {
909 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
910 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
911 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
914 static const char * const ss_fmt[] = {
915 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
916 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
917 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
922 static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
924 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
926 sscanf(bp, ss_fmt[procnetdev_vsn],
927 &ife->stats.rx_bytes, /* missing for 0 */
928 &ife->stats.rx_packets,
929 &ife->stats.rx_errors,
930 &ife->stats.rx_dropped,
931 &ife->stats.rx_fifo_errors,
932 &ife->stats.rx_frame_errors,
933 &ife->stats.rx_compressed, /* missing for <= 1 */
934 &ife->stats.rx_multicast, /* missing for <= 1 */
935 &ife->stats.tx_bytes, /* missing for 0 */
936 &ife->stats.tx_packets,
937 &ife->stats.tx_errors,
938 &ife->stats.tx_dropped,
939 &ife->stats.tx_fifo_errors,
940 &ife->stats.collisions,
941 &ife->stats.tx_carrier_errors,
942 &ife->stats.tx_compressed /* missing for <= 1 */
945 if (procnetdev_vsn <= 1) {
946 if (procnetdev_vsn == 0) {
947 ife->stats.rx_bytes = 0;
948 ife->stats.tx_bytes = 0;
950 ife->stats.rx_multicast = 0;
951 ife->stats.rx_compressed = 0;
952 ife->stats.tx_compressed = 0;
956 static inline int procnetdev_version(char *buf)
958 if (strstr(buf, "compressed"))
960 if (strstr(buf, "bytes"))
965 static int if_readlist_proc(char *target)
967 static int proc_read;
970 struct interface *ife;
971 int err, procnetdev_vsn;
978 fh = fopen(_PATH_PROCNET_DEV, "r");
980 bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
981 return if_readconf();
983 fgets(buf, sizeof buf, fh); /* eat line */
984 fgets(buf, sizeof buf, fh);
986 procnetdev_vsn = procnetdev_version(buf);
989 while (fgets(buf, sizeof buf, fh)) {
992 s = get_name(name, buf);
993 ife = add_interface(name);
994 get_dev_fields(s, ife, procnetdev_vsn);
995 ife->statistics_valid = 1;
996 if (target && !strcmp(target, name))
1000 perror(_PATH_PROCNET_DEV);
1008 static int if_readlist(void)
1010 int err = if_readlist_proc(NULL);
1013 err = if_readconf();
1017 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1020 struct interface *ife;
1022 if (!int_list && (if_readlist() < 0))
1024 for (ife = int_list; ife; ife = ife->next) {
1025 int err = doit(ife, cookie);
1033 /* Fetch the interface configuration from the kernel. */
1034 static int if_fetch(struct interface *ife)
1038 char *ifname = ife->name;
1040 strcpy(ifr.ifr_name, ifname);
1041 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1043 ife->flags = ifr.ifr_flags;
1045 strcpy(ifr.ifr_name, ifname);
1046 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1047 memset(ife->hwaddr, 0, 32);
1049 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1051 ife->type = ifr.ifr_hwaddr.sa_family;
1053 strcpy(ifr.ifr_name, ifname);
1054 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1057 ife->metric = ifr.ifr_metric;
1059 strcpy(ifr.ifr_name, ifname);
1060 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1063 ife->mtu = ifr.ifr_mtu;
1066 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1067 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1068 ife->type == ARPHRD_ADAPT) {
1070 strcpy(ifr.ifr_name, ifname);
1071 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1074 ife->outfill = (unsigned int) ifr.ifr_data;
1076 #ifdef SIOCGKEEPALIVE
1077 strcpy(ifr.ifr_name, ifname);
1078 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1081 ife->keepalive = (unsigned int) ifr.ifr_data;
1087 strcpy(ifr.ifr_name, ifname);
1088 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
1089 ife->map = ifr.ifr_map;
1092 memset(&ife->map, 0, sizeof(struct ifmap));
1094 #ifdef HAVE_TXQUEUELEN
1095 strcpy(ifr.ifr_name, ifname);
1096 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1097 ife->tx_queue_len = -1; /* unknown value */
1099 ife->tx_queue_len = ifr.ifr_qlen;
1101 ife->tx_queue_len = -1; /* unknown value */
1106 fd = get_socket_for_af(AF_INET);
1108 strcpy(ifr.ifr_name, ifname);
1109 ifr.ifr_addr.sa_family = AF_INET;
1110 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1112 ife->addr = ifr.ifr_addr;
1113 strcpy(ifr.ifr_name, ifname);
1114 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1115 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1117 ife->dstaddr = ifr.ifr_dstaddr;
1119 strcpy(ifr.ifr_name, ifname);
1120 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1121 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1123 ife->broadaddr = ifr.ifr_broadaddr;
1125 strcpy(ifr.ifr_name, ifname);
1126 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1127 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1129 ife->netmask = ifr.ifr_netmask;
1131 memset(&ife->addr, 0, sizeof(struct sockaddr));
1139 static int do_if_fetch(struct interface *ife)
1141 if (if_fetch(ife) < 0) {
1144 if (errno == ENODEV) {
1145 /* Give better error message for this case. */
1146 errmsg = _("Device not found");
1148 errmsg = strerror(errno);
1150 bb_error_msg(_("%s: error fetching interface information: %s"),
1157 /* This structure defines hardware protocols and their handlers. */
1159 const char * const name;
1163 char *(*print) (unsigned char *);
1164 int (*input) (char *, struct sockaddr *);
1165 int (*activate) (int fd);
1166 int suppress_null_addr;
1169 static const struct hwtype unspec_hwtype = {
1170 "unspec", "UNSPEC", -1, 0,
1171 UNSPEC_print, NULL, NULL
1174 static const struct hwtype loop_hwtype = {
1175 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1180 #include <net/if_arp.h>
1182 #if (__GLIBC__ >=2 && __GLIBC_MINOR >= 1) || defined(_NEWLIB_VERSION)
1183 #include <net/ethernet.h>
1185 #include <linux/if_ether.h>
1188 /* Display an Ethernet address in readable format. */
1189 static char *pr_ether(unsigned char *ptr)
1191 static char buff[64];
1193 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1194 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1195 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1201 /* Input an Ethernet address and convert to binary. */
1202 static int in_ether(char *bufp, struct sockaddr *sap)
1209 sap->sa_family = ether_hwtype.type;
1214 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1219 else if (c >= 'a' && c <= 'f')
1221 else if (c >= 'A' && c <= 'F')
1225 bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
1234 else if (c >= 'a' && c <= 'f')
1235 val |= c - 'a' + 10;
1236 else if (c >= 'A' && c <= 'F')
1237 val |= c - 'A' + 10;
1238 else if (c == ':' || c == 0)
1242 bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
1249 *ptr++ = (unsigned char) (val & 0377);
1252 /* We might get a semicolon here - not required. */
1255 if (i == ETH_ALEN) {
1256 bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
1264 /* That's it. Any trailing junk? */
1265 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1266 bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
1270 bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
1275 #endif /* KEEP_UNUSED */
1278 static const struct hwtype ether_hwtype = {
1279 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1280 pr_ether, NULL /* UNUSED in_ether */ , NULL
1284 #endif /* HAVE_HWETHER */
1289 #include <net/if_arp.h>
1292 /* Start the PPP encapsulation on the file descriptor. */
1293 static int do_ppp(int fd)
1295 bb_error_msg(_("You cannot start PPP with this program."));
1298 #endif /* KEEP_UNUSED */
1300 static const struct hwtype ppp_hwtype = {
1301 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1302 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
1306 #endif /* HAVE_PPP */
1308 static const struct hwtype * const hwtypes[] = {
1336 static short sVhwinit = 0;
1338 static void hwinit()
1340 loop_hwtype.title = _("Local Loopback");
1341 unspec_hwtype.title = _("UNSPEC");
1343 slip_hwtype.title = _("Serial Line IP");
1344 cslip_hwtype.title = _("VJ Serial Line IP");
1345 slip6_hwtype.title = _("6-bit Serial Line IP");
1346 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1347 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1350 ether_hwtype.title = _("Ethernet");
1353 tunnel_hwtype.title = _("IPIP Tunnel");
1356 ppp_hwtype.title = _("Point-to-Point Protocol");
1360 #endif /* KEEP_UNUSED */
1364 static const char * const if_port_text[][4] = {
1365 /* Keep in step with <linux/netdevice.h> */
1366 {"unknown", NULL, NULL, NULL},
1367 {"10base2", "bnc", "coax", NULL},
1368 {"10baseT", "utp", "tpe", NULL},
1369 {"AUI", "thick", "db15", NULL},
1370 {"100baseT", NULL, NULL, NULL},
1371 {"100baseTX", NULL, NULL, NULL},
1372 {"100baseFX", NULL, NULL, NULL},
1373 {NULL, NULL, NULL, NULL},
1376 static const char * const if_port_text[] = {
1377 /* Keep in step with <linux/netdevice.h> */
1390 /* Check our hardware type table for this type. */
1391 static const struct hwtype *get_hwntype(int type)
1393 const struct hwtype * const *hwp;
1398 #endif /* KEEP_UNUSED */
1401 while (*hwp != NULL) {
1402 if ((*hwp)->type == type)
1409 /* return 1 if address is all zeros */
1410 static int hw_null_address(const struct hwtype *hw, void *ap)
1413 unsigned char *address = (unsigned char *) ap;
1415 for (i = 0; i < hw->alen; i++)
1421 static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti";
1423 static void print_bytes_scaled(unsigned long long ull, const char *end)
1425 unsigned long long int_part;
1427 unsigned int frac_part;
1436 /* This does correct rounding and is a little larger. But it
1437 * uses KiB as the smallest displayed unit. */
1438 if ((int_part < (1024*1024 - 51)) || !--i) {
1440 int_part += 51; /* 1024*.05 = 51.2 */
1441 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1444 ext += 3; /* KiB, MiB, GiB, TiB */
1446 if (int_part >= 1024) {
1447 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1449 ext += 3; /* KiB, MiB, GiB, TiB */
1455 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
1458 static const char * const ife_print_flags_strs[] = {
1477 static const unsigned short ife_print_flags_mask[] = {
1497 static void ife_print(struct interface *ptr)
1500 const struct hwtype *hw;
1502 int can_compress = 0;
1506 char addr6[40], devname[20];
1507 struct sockaddr_in6 sap;
1508 int plen, scope, dad_status, if_idx;
1512 ap = get_afntype(ptr->addr.sa_family);
1514 ap = get_afntype(0);
1518 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1521 hw = get_hwntype(hf);
1523 hw = get_hwntype(-1);
1525 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1526 /* For some hardware types (eg Ash, ATM) we don't print the
1527 hardware address if it's null. */
1528 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1529 hw->suppress_null_addr)))
1530 printf(_("HWaddr %s "), hw->print((unsigned char *)ptr->hwaddr));
1532 if (ptr->flags & IFF_PORTSEL) {
1533 printf(_("Media:%s"), if_port_text[ptr->map.port] /* [0] */);
1534 if (ptr->flags & IFF_AUTOMEDIA)
1535 printf(_("(auto)"));
1542 printf(_(" %s addr:%s "), ap->name,
1543 ap->sprint(&ptr->addr, 1));
1544 if (ptr->flags & IFF_POINTOPOINT) {
1545 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1547 if (ptr->flags & IFF_BROADCAST) {
1548 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1550 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1556 #define IPV6_ADDR_ANY 0x0000U
1558 #define IPV6_ADDR_UNICAST 0x0001U
1559 #define IPV6_ADDR_MULTICAST 0x0002U
1560 #define IPV6_ADDR_ANYCAST 0x0004U
1562 #define IPV6_ADDR_LOOPBACK 0x0010U
1563 #define IPV6_ADDR_LINKLOCAL 0x0020U
1564 #define IPV6_ADDR_SITELOCAL 0x0040U
1566 #define IPV6_ADDR_COMPATv4 0x0080U
1568 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1570 #define IPV6_ADDR_MAPPED 0x1000U
1571 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1573 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1575 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1576 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1577 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1578 &dad_status, devname) != EOF) {
1579 if (!strcmp(devname, ptr->name)) {
1580 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1581 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1582 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1583 inet_pton(AF_INET6, addr6,
1584 (struct sockaddr *) &sap.sin6_addr);
1585 sap.sin6_family = AF_INET6;
1586 printf(_(" inet6 addr: %s/%d"),
1587 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1589 printf(_(" Scope:"));
1590 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1592 printf(_("Global"));
1594 case IPV6_ADDR_LINKLOCAL:
1597 case IPV6_ADDR_SITELOCAL:
1600 case IPV6_ADDR_COMPATv4:
1601 printf(_("Compat"));
1603 case IPV6_ADDR_LOOPBACK:
1607 printf(_("Unknown"));
1617 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1619 if (ptr->flags == 0) {
1620 printf(_("[NO FLAGS] "));
1624 if (ptr->flags & ife_print_flags_mask[i]) {
1625 printf(_(ife_print_flags_strs[i]));
1627 } while (ife_print_flags_mask[++i]);
1630 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1631 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1632 #ifdef SIOCSKEEPALIVE
1633 if (ptr->outfill || ptr->keepalive)
1634 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1638 /* If needed, display the interface statistics. */
1640 if (ptr->statistics_valid) {
1641 /* XXX: statistics are currently only printed for the primary address,
1642 * not for the aliases, although strictly speaking they're shared
1647 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
1648 ptr->stats.rx_packets, ptr->stats.rx_errors,
1649 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1650 ptr->stats.rx_frame_errors);
1652 printf(" compressed:%lu\n",
1653 ptr->stats.rx_compressed);
1655 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
1656 ptr->stats.tx_packets, ptr->stats.tx_errors,
1657 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1658 ptr->stats.tx_carrier_errors);
1659 printf(" collisions:%lu ", ptr->stats.collisions);
1661 printf("compressed:%lu ", ptr->stats.tx_compressed);
1662 if (ptr->tx_queue_len != -1)
1663 printf("txqueuelen:%d ", ptr->tx_queue_len);
1665 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1666 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1670 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1671 ptr->map.base_addr)) {
1674 printf(_("Interrupt:%d "), ptr->map.irq);
1675 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
1677 printf(_("Base address:0x%lx "),
1678 (unsigned long) ptr->map.base_addr);
1679 if (ptr->map.mem_start) {
1680 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
1684 printf(_("DMA chan:%x "), ptr->map.dma);
1691 static int do_if_print(struct interface *ife, void *cookie)
1693 int *opt_a = (int *) cookie;
1696 res = do_if_fetch(ife);
1698 if ((ife->flags & IFF_UP) || *opt_a)
1704 static struct interface *lookup_interface(char *name)
1706 struct interface *ife = NULL;
1708 if (if_readlist_proc(name) < 0)
1710 ife = add_interface(name);
1714 /* for ipv4 add/del modes */
1715 static int if_print(char *ifname)
1720 res = for_all_interfaces(do_if_print, &interface_opt_a);
1722 struct interface *ife;
1724 ife = lookup_interface(ifname);
1725 res = do_if_fetch(ife);
1732 int display_interfaces(char *ifname);
1733 int display_interfaces(char *ifname)
1737 /* Create a channel to the NET kernel. */
1738 if ((skfd = sockets_open(0)) < 0) {
1739 bb_perror_msg_and_die("socket");
1742 /* Do we have to show the current setup? */
1743 status = if_print(ifname);
1744 #ifdef CONFIG_FEATURE_CLEAN_UP