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 */
56 * Device Hardware types.
59 #define HAVE_HWETHER 1
64 #include "inet_common.h"
72 #include <sys/ioctl.h>
73 #include <sys/types.h>
75 #include <net/if_arp.h>
78 #ifdef CONFIG_FEATURE_IPV6
79 # define HAVE_AFINET6 1
84 #define _PATH_PROCNET_DEV "/proc/net/dev"
85 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
86 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
87 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
90 #include <net/if_slip.h>
97 * This is in linux/include/net/ipv6.h.
101 struct in6_addr ifr6_addr;
102 uint32_t ifr6_prefixlen;
103 unsigned int ifr6_ifindex;
108 #endif /* HAVE_AFINET6 */
111 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
112 #include <netipx/ipx.h>
116 /* Defines for glibc2.0 users. */
117 #ifndef SIOCSIFTXQLEN
118 #define SIOCSIFTXQLEN 0x8943
119 #define SIOCGIFTXQLEN 0x8942
122 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
124 #define ifr_qlen ifr_ifru.ifru_mtu
127 #ifndef HAVE_TXQUEUELEN
128 #define HAVE_TXQUEUELEN 1
132 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
135 /* This structure defines protocol families and their handlers. */
141 char *(*print) (unsigned char *);
142 char *(*sprint) (struct sockaddr *, int numeric);
143 int (*input) (int type, char *bufp, struct sockaddr *);
144 void (*herror) (char *text);
145 int (*rprint) (int options);
146 int (*rinput) (int typ, int ext, char **argv);
149 int (*getmask) (char *src, struct sockaddr * mask, char *name);
163 static int flag_ax25;
169 static int flag_netrom;
171 static int flag_inet;
174 static int flag_inet6;
177 static int flag_econet;
180 static int flag_x25 = 0;
187 static struct aftrans_t {
195 "ax25", "ax25", &flag_ax25},
198 "ip", "inet", &flag_inet},
201 "ip6", "inet6", &flag_inet6},
205 "ipx", "ipx", &flag_ipx},
209 "appletalk", "ddp", &flag_ddp},
213 "netrom", "netrom", &flag_netrom},
216 "inet", "inet", &flag_inet},
219 "inet6", "inet6", &flag_inet6},
223 "ddp", "ddp", &flag_ddp},
226 "unix", "unix", &flag_unx}, {
227 "tcpip", "inet", &flag_inet},
230 "econet", "ec", &flag_econet},
234 "x25", "x25", &flag_x25},
238 "ash", "ash", &flag_ash},
244 static char afname[256] = "";
245 #endif /* KEEP_UNUSED */
249 /* Display a UNIX domain address. */
250 static char *UNIX_print(unsigned char *ptr)
256 /* Display a UNIX domain address. */
257 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
261 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
262 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
263 return (UNIX_print(sap->sa_data));
267 static struct aftype unix_aftype = {
268 "unix", "UNIX Domain", AF_UNIX, 0,
269 UNIX_print, UNIX_sprint, NULL, NULL,
274 #endif /* HAVE_AFUNIX */
279 static void INET_reserror(char *text)
284 /* Display an Internet socket address. */
285 static char *INET_print(unsigned char *ptr)
287 return (inet_ntoa((*(struct in_addr *) ptr)));
289 #endif /* KEEP_UNUSED */
291 /* Display an Internet socket address. */
292 static char *INET_sprint(struct sockaddr *sap, int numeric)
294 static char buff[128];
296 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
297 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
299 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
300 numeric, 0xffffff00) != 0)
307 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
308 unsigned int netmask)
310 static char buff[128];
312 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
313 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
314 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
315 numeric, netmask) != 0)
320 static int INET_getsock(char *bufp, struct sockaddr *sap)
322 char *sp = bufp, *bp;
325 struct sockaddr_in *sin;
327 sin = (struct sockaddr_in *) sap;
328 sin->sin_family = AF_INET;
333 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
336 if ((*sp >= 'A') && (*sp <= 'F'))
337 bp[i] |= (int) (*sp - 'A') + 10;
338 else if ((*sp >= '0') && (*sp <= '9'))
339 bp[i] |= (int) (*sp - '0');
347 if ((*sp >= 'A') && (*sp <= 'F'))
348 bp[i] |= (int) (*sp - 'A') + 10;
349 else if ((*sp >= '0') && (*sp <= '9'))
350 bp[i] |= (int) (*sp - '0');
356 sin->sin_addr.s_addr = htonl(val);
361 static int INET_input(int type, char *bufp, struct sockaddr *sap)
365 return (INET_getsock(bufp, sap));
367 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
369 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
373 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
375 struct sockaddr_in *mask = (struct sockaddr_in *) m;
379 if ((slash = strchr(adr, '/')) == NULL)
383 prefix = strtoul(slash, &end, 0);
388 sprintf(name, "/%d", prefix);
390 mask->sin_family = AF_INET;
391 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
394 #endif /* KEEP_UNUSED */
396 static struct aftype inet_aftype = {
397 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
398 NULL /* UNUSED INET_print */ , INET_sprint,
399 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
400 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
401 NULL /* UNUSED INET_getnetmask */ ,
406 #endif /* HAVE_AFINET */
411 static void INET6_reserror(char *text)
416 /* Display an Internet socket address. */
417 static char *INET6_print(unsigned char *ptr)
419 static char name[80];
421 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
424 #endif /* KEEP_UNUSED */
426 /* Display an Internet socket address. */
427 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
428 static char *INET6_sprint(struct sockaddr *sap, int numeric)
430 static char buff[128];
432 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
433 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
435 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
436 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
441 static int INET6_getsock(char *bufp, struct sockaddr *sap)
443 struct sockaddr_in6 *sin6;
445 sin6 = (struct sockaddr_in6 *) sap;
446 sin6->sin6_family = AF_INET6;
449 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
455 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
459 return (INET6_getsock(bufp, sap));
461 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
464 #endif /* KEEP_UNUSED */
466 static struct aftype inet6_aftype = {
467 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
468 NULL /* UNUSED INET6_print */ , INET6_sprint,
469 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
470 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
471 NULL /* UNUSED INET6_getnetmask */ ,
476 #endif /* HAVE_AFINET6 */
478 /* Display an UNSPEC address. */
479 static char *UNSPEC_print(unsigned char *ptr)
481 static char buff[sizeof(struct sockaddr) * 3 + 1];
486 for (i = 0; i < sizeof(struct sockaddr); i++) {
487 /* careful -- not every libc's sprintf returns # bytes written */
488 sprintf(pos, "%02X-", (*ptr++ & 0377));
491 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
496 /* Display an UNSPEC socket address. */
497 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
501 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
502 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
503 return (UNSPEC_print((unsigned char *)sap->sa_data));
506 static struct aftype unspec_aftype = {
507 "unspec", "UNSPEC", AF_UNSPEC, 0,
508 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
512 static struct aftype * const aftypes[] = {
551 static short sVafinit = 0;
555 unspec_aftype.title = _("UNSPEC");
557 unix_aftype.title = _("UNIX Domain");
560 inet_aftype.title = _("DARPA Internet");
563 inet6_aftype.title = _("IPv6");
566 ax25_aftype.title = _("AMPR AX.25");
569 netrom_aftype.title = _("AMPR NET/ROM");
572 ipx_aftype.title = _("Novell IPX");
575 ddp_aftype.title = _("Appletalk DDP");
578 ec_aftype.title = _("Econet");
581 x25_aftype.title = _("CCITT X.25");
584 rose_aftype.title = _("AMPR ROSE");
587 ash_aftype.title = _("Ash");
592 static int aftrans_opt(const char *arg)
594 struct aftrans_t *paft;
598 safe_strncpy(buf, arg, sizeof(buf));
604 tmp2 = strchr(tmp1, ',');
610 for (paft = aftrans; paft->alias; paft++) {
611 if (strcmp(tmp1, paft->alias))
613 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
614 bb_error_msg(_("Too many address family arguments."));
621 strcat(afname, paft->name);
625 bb_error_msg(_("Unknown address family `%s'."), tmp1);
634 /* set the default AF list from the program name or a constant value */
635 static void aftrans_def(char *tool, char *argv0, char *dflt)
640 strcpy(afname, dflt);
642 if (!(tmp = strrchr(argv0, '/')))
643 tmp = argv0; /* no slash?! */
647 if (!(buf = strdup(tmp)))
650 if (strlen(tool) >= strlen(tmp)) {
654 tmp = buf + (strlen(tmp) - strlen(tool));
656 if (strcmp(tmp, tool) != 0) {
661 if ((tmp = strchr(buf, '_')))
665 if (aftrans_opt(buf))
671 /* Check our protocol family table for this family. */
672 static struct aftype *get_aftype(const char *name)
674 struct aftype * const *afp;
679 #endif /* KEEP_UNUSED */
682 while (*afp != NULL) {
683 if (!strcmp((*afp)->name, name))
687 if (strchr(name, ','))
688 bb_error_msg(_("Please don't supply more than one address family."));
691 #endif /* KEEP_UNUSED */
693 /* Check our protocol family table for this family. */
694 static struct aftype *get_afntype(int af)
696 struct aftype * const *afp;
701 #endif /* KEEP_UNUSED */
704 while (*afp != NULL) {
705 if ((*afp)->af == af)
712 /* Check our protocol family table for this family and return its socket */
713 static int get_socket_for_af(int af)
715 struct aftype * const *afp;
720 #endif /* KEEP_UNUSED */
723 while (*afp != NULL) {
724 if ((*afp)->af == af)
732 /* type: 0=all, 1=getroute */
733 static void print_aflist(int type)
737 struct aftype * const *afp;
742 #endif /* KEEP_UNUSED */
745 while (*afp != NULL) {
746 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
750 if ((count % 3) == 0)
751 fprintf(stderr, count ? "\n " : " ");
755 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
759 fprintf(stderr, "\n");
761 #endif /* KEEP_UNUSED */
763 struct user_net_device_stats {
764 unsigned long long rx_packets; /* total packets received */
765 unsigned long long tx_packets; /* total packets transmitted */
766 unsigned long long rx_bytes; /* total bytes received */
767 unsigned long long tx_bytes; /* total bytes transmitted */
768 unsigned long rx_errors; /* bad packets received */
769 unsigned long tx_errors; /* packet transmit problems */
770 unsigned long rx_dropped; /* no space in linux buffers */
771 unsigned long tx_dropped; /* no space available in linux */
772 unsigned long rx_multicast; /* multicast packets received */
773 unsigned long rx_compressed;
774 unsigned long tx_compressed;
775 unsigned long collisions;
777 /* detailed rx_errors: */
778 unsigned long rx_length_errors;
779 unsigned long rx_over_errors; /* receiver ring buff overflow */
780 unsigned long rx_crc_errors; /* recved pkt with crc error */
781 unsigned long rx_frame_errors; /* recv'd frame alignment error */
782 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
783 unsigned long rx_missed_errors; /* receiver missed packet */
784 /* detailed tx_errors */
785 unsigned long tx_aborted_errors;
786 unsigned long tx_carrier_errors;
787 unsigned long tx_fifo_errors;
788 unsigned long tx_heartbeat_errors;
789 unsigned long tx_window_errors;
793 struct interface *next, *prev;
794 char name[IFNAMSIZ]; /* interface name */
795 short type; /* if type */
796 short flags; /* various flags */
797 int metric; /* routing metric */
798 int mtu; /* MTU value */
799 int tx_queue_len; /* transmit queue length */
800 struct ifmap map; /* hardware setup */
801 struct sockaddr addr; /* IP address */
802 struct sockaddr dstaddr; /* P-P IP address */
803 struct sockaddr broadaddr; /* IP broadcast address */
804 struct sockaddr netmask; /* IP network mask */
805 struct sockaddr ipxaddr_bb; /* IPX network address */
806 struct sockaddr ipxaddr_sn; /* IPX network address */
807 struct sockaddr ipxaddr_e3; /* IPX network address */
808 struct sockaddr ipxaddr_e2; /* IPX network address */
809 struct sockaddr ddpaddr; /* Appletalk DDP address */
810 struct sockaddr ecaddr; /* Econet address */
819 char hwaddr[32]; /* HW address */
820 int statistics_valid;
821 struct user_net_device_stats stats; /* statistics */
822 int keepalive; /* keepalive value for SLIP */
823 int outfill; /* outfill value for SLIP */
827 int interface_opt_a = 0; /* show all interfaces */
830 static int opt_i = 0; /* show the statistics */
831 static int opt_v = 0; /* debugging output flag */
833 static int addr_family = 0; /* currently selected AF */
834 #endif /* KEEP_UNUSED */
836 static struct interface *int_list, *int_last;
837 static int skfd = -1; /* generic raw socket desc. */
840 static int sockets_open(int family)
842 struct aftype * const *aft;
844 static int force = -1;
848 if (get_kernel_revision() < KRELEASE(2, 1, 0))
850 if (access("/proc/net", R_OK))
853 for (aft = aftypes; *aft; aft++) {
854 struct aftype *af = *aft;
855 int type = SOCK_DGRAM;
857 if (af->af == AF_UNSPEC)
859 if (family && family != af->af)
865 /* Check some /proc file first to not stress kmod */
866 if (!family && !force && af->flag_file) {
867 if (access(af->flag_file, R_OK))
871 if (af->af == AF_NETROM)
872 type = SOCK_SEQPACKET;
875 if (af->af == AF_X25)
876 type = SOCK_SEQPACKET;
878 af->fd = socket(af->af, type, 0);
883 bb_error_msg(_("No usable address families found."));
888 #ifdef CONFIG_FEATURE_CLEAN_UP
889 static void sockets_close(void)
891 struct aftype * const *aft;
892 for (aft = aftypes; *aft != NULL; aft++) {
893 struct aftype *af = *aft;
902 /* like strcmp(), but knows about numbers */
903 static int nstrcmp(const char *a, const char *b)
905 const char *a_ptr = a;
906 const char *b_ptr = b;
912 if (!isdigit(*a) && isdigit(*(a+1))) {
920 if (isdigit(*a) && isdigit(*b)) {
921 return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1;
926 static struct interface *add_interface(char *name)
928 struct interface *ife, **nextp, *new;
930 for (ife = int_last; ife; ife = ife->prev) {
931 int n = nstrcmp(ife->name, name);
939 safe_strncpy(new->name, name, IFNAMSIZ);
940 nextp = ife ? &ife->next : &int_list;
944 new->next->prev = new;
952 static int if_readconf(void)
960 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
962 skfd2 = get_socket_for_af(AF_INET);
964 bb_perror_msg(("warning: no inet socket available"));
965 /* Try to soldier on with whatever socket we can get hold of. */
966 skfd2 = sockets_open(0);
973 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
974 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
976 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
977 perror("SIOCGIFCONF");
980 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
981 /* assume it overflowed and try again */
989 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
990 add_interface(ifr->ifr_name);
1000 static char *get_name(char *name, char *p)
1002 /* Extract <name>[:<alias>] from nul-terminated p where p matches
1003 <name>[:<alias>]: after leading whitespace.
1004 If match is not made, set name empty and return unchanged p */
1005 int namestart=0, nameend=0, aliasend;
1006 while (isspace(p[namestart]))
1009 while (p[nameend] && p[nameend]!=':' && !isspace(p[nameend]))
1011 if (p[nameend]==':') {
1013 while (p[aliasend] && isdigit(p[aliasend]))
1015 if (p[aliasend]==':') {
1018 if ((nameend-namestart)<IFNAMSIZ) {
1019 memcpy(name,&p[namestart],nameend-namestart);
1020 name[nameend-namestart]='\0';
1023 /* Interface name too large */
1027 /* first ':' not found - return empty */
1033 /* If scanf supports size qualifiers for %n conversions, then we can
1034 * use a modified fmt that simply stores the position in the fields
1035 * having no associated fields in the proc string. Of course, we need
1036 * to zero them again when we're done. But that is smaller than the
1037 * old approach of multiple scanf occurrences with large numbers of
1040 /* static const char * const ss_fmt[] = { */
1041 /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
1042 /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
1043 /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
1046 /* Lie about the size of the int pointed to for %n. */
1047 #if INT_MAX == LONG_MAX
1048 static const char * const ss_fmt[] = {
1049 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
1050 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
1051 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
1054 static const char * const ss_fmt[] = {
1055 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
1056 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
1057 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
1062 static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
1064 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
1066 sscanf(bp, ss_fmt[procnetdev_vsn],
1067 &ife->stats.rx_bytes, /* missing for 0 */
1068 &ife->stats.rx_packets,
1069 &ife->stats.rx_errors,
1070 &ife->stats.rx_dropped,
1071 &ife->stats.rx_fifo_errors,
1072 &ife->stats.rx_frame_errors,
1073 &ife->stats.rx_compressed, /* missing for <= 1 */
1074 &ife->stats.rx_multicast, /* missing for <= 1 */
1075 &ife->stats.tx_bytes, /* missing for 0 */
1076 &ife->stats.tx_packets,
1077 &ife->stats.tx_errors,
1078 &ife->stats.tx_dropped,
1079 &ife->stats.tx_fifo_errors,
1080 &ife->stats.collisions,
1081 &ife->stats.tx_carrier_errors,
1082 &ife->stats.tx_compressed /* missing for <= 1 */
1085 if (procnetdev_vsn <= 1) {
1086 if (procnetdev_vsn == 0) {
1087 ife->stats.rx_bytes = 0;
1088 ife->stats.tx_bytes = 0;
1090 ife->stats.rx_multicast = 0;
1091 ife->stats.rx_compressed = 0;
1092 ife->stats.tx_compressed = 0;
1096 static inline int procnetdev_version(char *buf)
1098 if (strstr(buf, "compressed"))
1100 if (strstr(buf, "bytes"))
1105 static int if_readlist_proc(char *target)
1107 static int proc_read;
1110 struct interface *ife;
1111 int err, procnetdev_vsn;
1118 fh = fopen(_PATH_PROCNET_DEV, "r");
1120 bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
1121 return if_readconf();
1123 fgets(buf, sizeof buf, fh); /* eat line */
1124 fgets(buf, sizeof buf, fh);
1126 procnetdev_vsn = procnetdev_version(buf);
1129 while (fgets(buf, sizeof buf, fh)) {
1132 s = get_name(name, buf);
1133 ife = add_interface(name);
1134 get_dev_fields(s, ife, procnetdev_vsn);
1135 ife->statistics_valid = 1;
1136 if (target && !strcmp(target, name))
1140 perror(_PATH_PROCNET_DEV);
1148 static int if_readlist(void)
1150 int err = if_readlist_proc(NULL);
1153 err = if_readconf();
1157 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1160 struct interface *ife;
1162 if (!int_list && (if_readlist() < 0))
1164 for (ife = int_list; ife; ife = ife->next) {
1165 int err = doit(ife, cookie);
1173 /* Support for fetching an IPX address */
1176 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1178 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1179 return ioctl(sock, SIOCGIFADDR, ifr);
1184 /* Fetch the interface configuration from the kernel. */
1185 static int if_fetch(struct interface *ife)
1189 char *ifname = ife->name;
1191 strcpy(ifr.ifr_name, ifname);
1192 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1194 ife->flags = ifr.ifr_flags;
1196 strcpy(ifr.ifr_name, ifname);
1197 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1198 memset(ife->hwaddr, 0, 32);
1200 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1202 ife->type = ifr.ifr_hwaddr.sa_family;
1204 strcpy(ifr.ifr_name, ifname);
1205 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1208 ife->metric = ifr.ifr_metric;
1210 strcpy(ifr.ifr_name, ifname);
1211 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1214 ife->mtu = ifr.ifr_mtu;
1217 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1218 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1219 ife->type == ARPHRD_ADAPT) {
1221 strcpy(ifr.ifr_name, ifname);
1222 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1225 ife->outfill = (unsigned int) ifr.ifr_data;
1227 #ifdef SIOCGKEEPALIVE
1228 strcpy(ifr.ifr_name, ifname);
1229 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1232 ife->keepalive = (unsigned int) ifr.ifr_data;
1238 strcpy(ifr.ifr_name, ifname);
1239 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
1240 ife->map = ifr.ifr_map;
1243 memset(&ife->map, 0, sizeof(struct ifmap));
1245 #ifdef HAVE_TXQUEUELEN
1246 strcpy(ifr.ifr_name, ifname);
1247 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1248 ife->tx_queue_len = -1; /* unknown value */
1250 ife->tx_queue_len = ifr.ifr_qlen;
1252 ife->tx_queue_len = -1; /* unknown value */
1257 fd = get_socket_for_af(AF_INET);
1259 strcpy(ifr.ifr_name, ifname);
1260 ifr.ifr_addr.sa_family = AF_INET;
1261 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1263 ife->addr = ifr.ifr_addr;
1264 strcpy(ifr.ifr_name, ifname);
1265 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1266 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1268 ife->dstaddr = ifr.ifr_dstaddr;
1270 strcpy(ifr.ifr_name, ifname);
1271 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1272 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1274 ife->broadaddr = ifr.ifr_broadaddr;
1276 strcpy(ifr.ifr_name, ifname);
1277 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1278 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1280 ife->netmask = ifr.ifr_netmask;
1282 memset(&ife->addr, 0, sizeof(struct sockaddr));
1287 /* DDP address maybe ? */
1288 fd = get_socket_for_af(AF_APPLETALK);
1290 strcpy(ifr.ifr_name, ifname);
1291 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1292 ife->ddpaddr = ifr.ifr_addr;
1299 /* Look for IPX addresses with all framing types */
1300 fd = get_socket_for_af(AF_IPX);
1302 strcpy(ifr.ifr_name, ifname);
1303 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1304 ife->has_ipx_bb = 1;
1305 ife->ipxaddr_bb = ifr.ifr_addr;
1307 strcpy(ifr.ifr_name, ifname);
1308 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1309 ife->has_ipx_sn = 1;
1310 ife->ipxaddr_sn = ifr.ifr_addr;
1312 strcpy(ifr.ifr_name, ifname);
1313 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1314 ife->has_ipx_e3 = 1;
1315 ife->ipxaddr_e3 = ifr.ifr_addr;
1317 strcpy(ifr.ifr_name, ifname);
1318 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1319 ife->has_ipx_e2 = 1;
1320 ife->ipxaddr_e2 = ifr.ifr_addr;
1326 /* Econet address maybe? */
1327 fd = get_socket_for_af(AF_ECONET);
1329 strcpy(ifr.ifr_name, ifname);
1330 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1331 ife->ecaddr = ifr.ifr_addr;
1332 ife->has_econet = 1;
1341 static int do_if_fetch(struct interface *ife)
1343 if (if_fetch(ife) < 0) {
1346 if (errno == ENODEV) {
1347 /* Give better error message for this case. */
1348 errmsg = _("Device not found");
1350 errmsg = strerror(errno);
1352 bb_error_msg(_("%s: error fetching interface information: %s"),
1359 /* This structure defines hardware protocols and their handlers. */
1361 const char * const name;
1365 char *(*print) (unsigned char *);
1366 int (*input) (char *, struct sockaddr *);
1367 int (*activate) (int fd);
1368 int suppress_null_addr;
1371 static const struct hwtype unspec_hwtype = {
1372 "unspec", "UNSPEC", -1, 0,
1373 UNSPEC_print, NULL, NULL
1376 static const struct hwtype loop_hwtype = {
1377 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1382 #include <net/if_arp.h>
1384 #if (__GLIBC__ >=2 && __GLIBC_MINOR >= 1) || defined(_NEWLIB_VERSION)
1385 #include <net/ethernet.h>
1387 #include <linux/if_ether.h>
1390 /* Display an Ethernet address in readable format. */
1391 static char *pr_ether(unsigned char *ptr)
1393 static char buff[64];
1395 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1396 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1397 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1403 /* Input an Ethernet address and convert to binary. */
1404 static int in_ether(char *bufp, struct sockaddr *sap)
1411 sap->sa_family = ether_hwtype.type;
1416 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1421 else if (c >= 'a' && c <= 'f')
1423 else if (c >= 'A' && c <= 'F')
1427 bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
1436 else if (c >= 'a' && c <= 'f')
1437 val |= c - 'a' + 10;
1438 else if (c >= 'A' && c <= 'F')
1439 val |= c - 'A' + 10;
1440 else if (c == ':' || c == 0)
1444 bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
1451 *ptr++ = (unsigned char) (val & 0377);
1454 /* We might get a semicolon here - not required. */
1457 if (i == ETH_ALEN) {
1458 bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
1466 /* That's it. Any trailing junk? */
1467 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1468 bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
1472 bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
1477 #endif /* KEEP_UNUSED */
1480 static const struct hwtype ether_hwtype = {
1481 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1482 pr_ether, NULL /* UNUSED in_ether */ , NULL
1486 #endif /* HAVE_HWETHER */
1491 #include <net/if_arp.h>
1494 /* Start the PPP encapsulation on the file descriptor. */
1495 static int do_ppp(int fd)
1497 bb_error_msg(_("You cannot start PPP with this program."));
1500 #endif /* KEEP_UNUSED */
1502 static const struct hwtype ppp_hwtype = {
1503 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1504 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
1508 #endif /* HAVE_PPP */
1510 static const struct hwtype * const hwtypes[] = {
1532 #ifdef ARPHRD_IEEE802_TR
1585 static short sVhwinit = 0;
1587 static void hwinit()
1589 loop_hwtype.title = _("Local Loopback");
1590 unspec_hwtype.title = _("UNSPEC");
1592 slip_hwtype.title = _("Serial Line IP");
1593 cslip_hwtype.title = _("VJ Serial Line IP");
1594 slip6_hwtype.title = _("6-bit Serial Line IP");
1595 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1596 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1599 ether_hwtype.title = _("Ethernet");
1602 ash_hwtype.title = _("Ash");
1605 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1608 hippi_hwtype.title = _("HIPPI");
1611 ax25_hwtype.title = _("AMPR AX.25");
1614 rose_hwtype.title = _("AMPR ROSE");
1617 netrom_hwtype.title = _("AMPR NET/ROM");
1620 x25_hwtype.title = _("generic X.25");
1623 tunnel_hwtype.title = _("IPIP Tunnel");
1626 ppp_hwtype.title = _("Point-to-Point Protocol");
1629 hdlc_hwtype.title = _("(Cisco)-HDLC");
1630 lapb_hwtype.title = _("LAPB");
1633 arcnet_hwtype.title = _("ARCnet");
1636 dlci_hwtype.title = _("Frame Relay DLCI");
1637 frad_hwtype.title = _("Frame Relay Access Device");
1640 sit_hwtype.title = _("IPv6-in-IPv4");
1643 irda_hwtype.title = _("IrLAP");
1646 tr_hwtype.title = _("16/4 Mbps Token Ring");
1647 #ifdef ARPHRD_IEEE802_TR
1648 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
1652 ec_hwtype.title = _("Econet");
1656 #endif /* KEEP_UNUSED */
1660 static const char * const if_port_text[][4] = {
1661 /* Keep in step with <linux/netdevice.h> */
1662 {"unknown", NULL, NULL, NULL},
1663 {"10base2", "bnc", "coax", NULL},
1664 {"10baseT", "utp", "tpe", NULL},
1665 {"AUI", "thick", "db15", NULL},
1666 {"100baseT", NULL, NULL, NULL},
1667 {"100baseTX", NULL, NULL, NULL},
1668 {"100baseFX", NULL, NULL, NULL},
1669 {NULL, NULL, NULL, NULL},
1672 static const char * const if_port_text[] = {
1673 /* Keep in step with <linux/netdevice.h> */
1686 /* Check our hardware type table for this type. */
1687 static const struct hwtype *get_hwntype(int type)
1689 const struct hwtype * const *hwp;
1694 #endif /* KEEP_UNUSED */
1697 while (*hwp != NULL) {
1698 if ((*hwp)->type == type)
1705 /* return 1 if address is all zeros */
1706 static int hw_null_address(const struct hwtype *hw, void *ap)
1709 unsigned char *address = (unsigned char *) ap;
1711 for (i = 0; i < hw->alen; i++)
1717 static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti";
1719 static void print_bytes_scaled(unsigned long long ull, const char *end)
1721 unsigned long long int_part;
1723 unsigned int frac_part;
1732 /* This does correct rounding and is a little larger. But it
1733 * uses KiB as the smallest displayed unit. */
1734 if ((int_part < (1024*1024 - 51)) || !--i) {
1736 int_part += 51; /* 1024*.05 = 51.2 */
1737 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1740 ext += 3; /* KiB, MiB, GiB, TiB */
1742 if (int_part >= 1024) {
1743 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1745 ext += 3; /* KiB, MiB, GiB, TiB */
1751 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
1754 static const char * const ife_print_flags_strs[] = {
1773 static const unsigned short ife_print_flags_mask[] = {
1793 static void ife_print(struct interface *ptr)
1796 const struct hwtype *hw;
1798 int can_compress = 0;
1801 static struct aftype *ipxtype = NULL;
1804 static struct aftype *ectype = NULL;
1807 static struct aftype *ddptype = NULL;
1811 char addr6[40], devname[20];
1812 struct sockaddr_in6 sap;
1813 int plen, scope, dad_status, if_idx;
1817 ap = get_afntype(ptr->addr.sa_family);
1819 ap = get_afntype(0);
1823 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1826 hw = get_hwntype(hf);
1828 hw = get_hwntype(-1);
1830 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1831 /* For some hardware types (eg Ash, ATM) we don't print the
1832 hardware address if it's null. */
1833 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1834 hw->suppress_null_addr)))
1835 printf(_("HWaddr %s "), hw->print((unsigned char *)ptr->hwaddr));
1837 if (ptr->flags & IFF_PORTSEL) {
1838 printf(_("Media:%s"), if_port_text[ptr->map.port] /* [0] */);
1839 if (ptr->flags & IFF_AUTOMEDIA)
1840 printf(_("(auto)"));
1847 printf(_(" %s addr:%s "), ap->name,
1848 ap->sprint(&ptr->addr, 1));
1849 if (ptr->flags & IFF_POINTOPOINT) {
1850 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1852 if (ptr->flags & IFF_BROADCAST) {
1853 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1855 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1861 #define IPV6_ADDR_ANY 0x0000U
1863 #define IPV6_ADDR_UNICAST 0x0001U
1864 #define IPV6_ADDR_MULTICAST 0x0002U
1865 #define IPV6_ADDR_ANYCAST 0x0004U
1867 #define IPV6_ADDR_LOOPBACK 0x0010U
1868 #define IPV6_ADDR_LINKLOCAL 0x0020U
1869 #define IPV6_ADDR_SITELOCAL 0x0040U
1871 #define IPV6_ADDR_COMPATv4 0x0080U
1873 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1875 #define IPV6_ADDR_MAPPED 0x1000U
1876 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1878 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1880 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1881 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1882 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1883 &dad_status, devname) != EOF) {
1884 if (!strcmp(devname, ptr->name)) {
1885 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1886 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1887 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1888 inet_pton(AF_INET6, addr6,
1889 (struct sockaddr *) &sap.sin6_addr);
1890 sap.sin6_family = AF_INET6;
1891 printf(_(" inet6 addr: %s/%d"),
1892 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1894 printf(_(" Scope:"));
1895 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1897 printf(_("Global"));
1899 case IPV6_ADDR_LINKLOCAL:
1902 case IPV6_ADDR_SITELOCAL:
1905 case IPV6_ADDR_COMPATv4:
1906 printf(_("Compat"));
1908 case IPV6_ADDR_LOOPBACK:
1912 printf(_("Unknown"));
1922 if (ipxtype == NULL)
1923 ipxtype = get_afntype(AF_IPX);
1925 if (ipxtype != NULL) {
1926 if (ptr->has_ipx_bb)
1927 printf(_(" IPX/Ethernet II addr:%s\n"),
1928 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1929 if (ptr->has_ipx_sn)
1930 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1931 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1932 if (ptr->has_ipx_e2)
1933 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1934 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1935 if (ptr->has_ipx_e3)
1936 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1937 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1942 if (ddptype == NULL)
1943 ddptype = get_afntype(AF_APPLETALK);
1944 if (ddptype != NULL) {
1946 printf(_(" EtherTalk Phase 2 addr:%s\n"),
1947 ddptype->sprint(&ptr->ddpaddr, 1));
1953 ectype = get_afntype(AF_ECONET);
1954 if (ectype != NULL) {
1955 if (ptr->has_econet)
1956 printf(_(" econet addr:%s\n"),
1957 ectype->sprint(&ptr->ecaddr, 1));
1962 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1964 if (ptr->flags == 0) {
1965 printf(_("[NO FLAGS] "));
1969 if (ptr->flags & ife_print_flags_mask[i]) {
1970 printf(_(ife_print_flags_strs[i]));
1972 } while (ife_print_flags_mask[++i]);
1975 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1976 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1977 #ifdef SIOCSKEEPALIVE
1978 if (ptr->outfill || ptr->keepalive)
1979 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1983 /* If needed, display the interface statistics. */
1985 if (ptr->statistics_valid) {
1986 /* XXX: statistics are currently only printed for the primary address,
1987 * not for the aliases, although strictly speaking they're shared
1992 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
1993 ptr->stats.rx_packets, ptr->stats.rx_errors,
1994 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1995 ptr->stats.rx_frame_errors);
1997 printf(" compressed:%lu\n",
1998 ptr->stats.rx_compressed);
2000 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
2001 ptr->stats.tx_packets, ptr->stats.tx_errors,
2002 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2003 ptr->stats.tx_carrier_errors);
2004 printf(" collisions:%lu ", ptr->stats.collisions);
2006 printf("compressed:%lu ", ptr->stats.tx_compressed);
2007 if (ptr->tx_queue_len != -1)
2008 printf("txqueuelen:%d ", ptr->tx_queue_len);
2010 print_bytes_scaled(ptr->stats.rx_bytes, " T");
2011 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2015 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2016 ptr->map.base_addr)) {
2019 printf(_("Interrupt:%d "), ptr->map.irq);
2020 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2022 printf(_("Base address:0x%lx "),
2023 (unsigned long) ptr->map.base_addr);
2024 if (ptr->map.mem_start) {
2025 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
2029 printf(_("DMA chan:%x "), ptr->map.dma);
2036 static int do_if_print(struct interface *ife, void *cookie)
2038 int *opt_a = (int *) cookie;
2041 res = do_if_fetch(ife);
2043 if ((ife->flags & IFF_UP) || *opt_a)
2049 static struct interface *lookup_interface(char *name)
2051 struct interface *ife = NULL;
2053 if (if_readlist_proc(name) < 0)
2055 ife = add_interface(name);
2059 /* for ipv4 add/del modes */
2060 static int if_print(char *ifname)
2065 res = for_all_interfaces(do_if_print, &interface_opt_a);
2067 struct interface *ife;
2069 ife = lookup_interface(ifname);
2070 res = do_if_fetch(ife);
2077 int display_interfaces(char *ifname)
2081 /* Create a channel to the NET kernel. */
2082 if ((skfd = sockets_open(0)) < 0) {
2083 bb_perror_msg_and_die("socket");
2086 /* Do we have to show the current setup? */
2087 status = if_print(ifname);
2088 #ifdef CONFIG_FEATURE_CLEAN_UP