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.23 2004/07/23 01:49:46 bug1 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
85 #define _PATH_PROCNET_DEV "/proc/net/dev"
86 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
87 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
88 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
91 #include <net/if_slip.h>
98 * This is in linux/include/net/ipv6.h.
102 struct in6_addr ifr6_addr;
103 uint32_t ifr6_prefixlen;
104 unsigned int ifr6_ifindex;
109 #endif /* HAVE_AFINET6 */
112 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
113 #include <netipx/ipx.h>
119 /* Defines for glibc2.0 users. */
120 #ifndef SIOCSIFTXQLEN
121 #define SIOCSIFTXQLEN 0x8943
122 #define SIOCGIFTXQLEN 0x8942
125 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
127 #define ifr_qlen ifr_ifru.ifru_mtu
130 #ifndef HAVE_TXQUEUELEN
131 #define HAVE_TXQUEUELEN 1
135 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
138 /* This structure defines protocol families and their handlers. */
144 char *(*print) (unsigned char *);
145 char *(*sprint) (struct sockaddr *, int numeric);
146 int (*input) (int type, char *bufp, struct sockaddr *);
147 void (*herror) (char *text);
148 int (*rprint) (int options);
149 int (*rinput) (int typ, int ext, char **argv);
152 int (*getmask) (char *src, struct sockaddr * mask, char *name);
166 static int flag_ax25;
172 static int flag_netrom;
174 static int flag_inet;
177 static int flag_inet6;
180 static int flag_econet;
183 static int flag_x25 = 0;
190 static struct aftrans_t {
198 "ax25", "ax25", &flag_ax25},
201 "ip", "inet", &flag_inet},
204 "ip6", "inet6", &flag_inet6},
208 "ipx", "ipx", &flag_ipx},
212 "appletalk", "ddp", &flag_ddp},
216 "netrom", "netrom", &flag_netrom},
219 "inet", "inet", &flag_inet},
222 "inet6", "inet6", &flag_inet6},
226 "ddp", "ddp", &flag_ddp},
229 "unix", "unix", &flag_unx}, {
230 "tcpip", "inet", &flag_inet},
233 "econet", "ec", &flag_econet},
237 "x25", "x25", &flag_x25},
241 "ash", "ash", &flag_ash},
247 static char afname[256] = "";
248 #endif /* KEEP_UNUSED */
252 /* Display a UNIX domain address. */
253 static char *UNIX_print(unsigned char *ptr)
259 /* Display a UNIX domain address. */
260 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
264 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
265 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
266 return (UNIX_print(sap->sa_data));
270 static struct aftype unix_aftype = {
271 "unix", "UNIX Domain", AF_UNIX, 0,
272 UNIX_print, UNIX_sprint, NULL, NULL,
277 #endif /* HAVE_AFUNIX */
282 static void INET_reserror(char *text)
287 /* Display an Internet socket address. */
288 static char *INET_print(unsigned char *ptr)
290 return (inet_ntoa((*(struct in_addr *) ptr)));
292 #endif /* KEEP_UNUSED */
294 /* Display an Internet socket address. */
295 static char *INET_sprint(struct sockaddr *sap, int numeric)
297 static char buff[128];
299 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
300 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
302 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
303 numeric, 0xffffff00) != 0)
310 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
311 unsigned int netmask)
313 static char buff[128];
315 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
316 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
317 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
318 numeric, netmask) != 0)
323 static int INET_getsock(char *bufp, struct sockaddr *sap)
325 char *sp = bufp, *bp;
328 struct sockaddr_in *sin;
330 sin = (struct sockaddr_in *) sap;
331 sin->sin_family = AF_INET;
336 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
339 if ((*sp >= 'A') && (*sp <= 'F'))
340 bp[i] |= (int) (*sp - 'A') + 10;
341 else if ((*sp >= '0') && (*sp <= '9'))
342 bp[i] |= (int) (*sp - '0');
350 if ((*sp >= 'A') && (*sp <= 'F'))
351 bp[i] |= (int) (*sp - 'A') + 10;
352 else if ((*sp >= '0') && (*sp <= '9'))
353 bp[i] |= (int) (*sp - '0');
359 sin->sin_addr.s_addr = htonl(val);
364 static int INET_input(int type, char *bufp, struct sockaddr *sap)
368 return (INET_getsock(bufp, sap));
370 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
372 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
376 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
378 struct sockaddr_in *mask = (struct sockaddr_in *) m;
382 if ((slash = strchr(adr, '/')) == NULL)
386 prefix = strtoul(slash, &end, 0);
391 sprintf(name, "/%d", prefix);
393 mask->sin_family = AF_INET;
394 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
397 #endif /* KEEP_UNUSED */
399 static struct aftype inet_aftype = {
400 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
401 NULL /* UNUSED INET_print */ , INET_sprint,
402 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
403 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
404 NULL /* UNUSED INET_getnetmask */ ,
409 #endif /* HAVE_AFINET */
414 static void INET6_reserror(char *text)
419 /* Display an Internet socket address. */
420 static char *INET6_print(unsigned char *ptr)
422 static char name[80];
424 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
427 #endif /* KEEP_UNUSED */
429 /* Display an Internet socket address. */
430 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
431 static char *INET6_sprint(struct sockaddr *sap, int numeric)
433 static char buff[128];
435 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
436 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
438 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
439 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
444 static int INET6_getsock(char *bufp, struct sockaddr *sap)
446 struct sockaddr_in6 *sin6;
448 sin6 = (struct sockaddr_in6 *) sap;
449 sin6->sin6_family = AF_INET6;
452 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
458 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
462 return (INET6_getsock(bufp, sap));
464 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
467 #endif /* KEEP_UNUSED */
469 static struct aftype inet6_aftype = {
470 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
471 NULL /* UNUSED INET6_print */ , INET6_sprint,
472 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
473 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
474 NULL /* UNUSED INET6_getnetmask */ ,
479 #endif /* HAVE_AFINET6 */
481 /* Display an UNSPEC address. */
482 static char *UNSPEC_print(unsigned char *ptr)
484 static char buff[sizeof(struct sockaddr) * 3 + 1];
489 for (i = 0; i < sizeof(struct sockaddr); i++) {
490 /* careful -- not every libc's sprintf returns # bytes written */
491 sprintf(pos, "%02X-", (*ptr++ & 0377));
494 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
499 /* Display an UNSPEC socket address. */
500 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
504 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
505 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
506 return (UNSPEC_print(sap->sa_data));
509 static struct aftype unspec_aftype = {
510 "unspec", "UNSPEC", AF_UNSPEC, 0,
511 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
515 static struct aftype *aftypes[] = {
554 static short sVafinit = 0;
558 unspec_aftype.title = _("UNSPEC");
560 unix_aftype.title = _("UNIX Domain");
563 inet_aftype.title = _("DARPA Internet");
566 inet6_aftype.title = _("IPv6");
569 ax25_aftype.title = _("AMPR AX.25");
572 netrom_aftype.title = _("AMPR NET/ROM");
575 ipx_aftype.title = _("Novell IPX");
578 ddp_aftype.title = _("Appletalk DDP");
581 ec_aftype.title = _("Econet");
584 x25_aftype.title = _("CCITT X.25");
587 rose_aftype.title = _("AMPR ROSE");
590 ash_aftype.title = _("Ash");
595 static int aftrans_opt(const char *arg)
597 struct aftrans_t *paft;
601 safe_strncpy(buf, arg, sizeof(buf));
607 tmp2 = strchr(tmp1, ',');
613 for (paft = aftrans; paft->alias; paft++) {
614 if (strcmp(tmp1, paft->alias))
616 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
617 bb_error_msg(_("Too many address family arguments."));
624 strcat(afname, paft->name);
628 bb_error_msg(_("Unknown address family `%s'."), tmp1);
637 /* set the default AF list from the program name or a constant value */
638 static void aftrans_def(char *tool, char *argv0, char *dflt)
643 strcpy(afname, dflt);
645 if (!(tmp = strrchr(argv0, '/')))
646 tmp = argv0; /* no slash?! */
650 if (!(buf = strdup(tmp)))
653 if (strlen(tool) >= strlen(tmp)) {
657 tmp = buf + (strlen(tmp) - strlen(tool));
659 if (strcmp(tmp, tool) != 0) {
664 if ((tmp = strchr(buf, '_')))
668 if (aftrans_opt(buf))
674 /* Check our protocol family table for this family. */
675 static struct aftype *get_aftype(const char *name)
682 #endif /* KEEP_UNUSED */
685 while (*afp != NULL) {
686 if (!strcmp((*afp)->name, name))
690 if (strchr(name, ','))
691 bb_error_msg(_("Please don't supply more than one address family."));
694 #endif /* KEEP_UNUSED */
696 /* Check our protocol family table for this family. */
697 static struct aftype *get_afntype(int af)
704 #endif /* KEEP_UNUSED */
707 while (*afp != NULL) {
708 if ((*afp)->af == af)
715 /* Check our protocol family table for this family and return its socket */
716 static int get_socket_for_af(int af)
723 #endif /* KEEP_UNUSED */
726 while (*afp != NULL) {
727 if ((*afp)->af == af)
735 /* type: 0=all, 1=getroute */
736 static void print_aflist(int type)
745 #endif /* KEEP_UNUSED */
748 while (*afp != NULL) {
749 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
753 if ((count % 3) == 0)
754 fprintf(stderr, count ? "\n " : " ");
758 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
762 fprintf(stderr, "\n");
764 #endif /* KEEP_UNUSED */
766 struct user_net_device_stats {
767 unsigned long long rx_packets; /* total packets received */
768 unsigned long long tx_packets; /* total packets transmitted */
769 unsigned long long rx_bytes; /* total bytes received */
770 unsigned long long tx_bytes; /* total bytes transmitted */
771 unsigned long rx_errors; /* bad packets received */
772 unsigned long tx_errors; /* packet transmit problems */
773 unsigned long rx_dropped; /* no space in linux buffers */
774 unsigned long tx_dropped; /* no space available in linux */
775 unsigned long rx_multicast; /* multicast packets received */
776 unsigned long rx_compressed;
777 unsigned long tx_compressed;
778 unsigned long collisions;
780 /* detailed rx_errors: */
781 unsigned long rx_length_errors;
782 unsigned long rx_over_errors; /* receiver ring buff overflow */
783 unsigned long rx_crc_errors; /* recved pkt with crc error */
784 unsigned long rx_frame_errors; /* recv'd frame alignment error */
785 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
786 unsigned long rx_missed_errors; /* receiver missed packet */
787 /* detailed tx_errors */
788 unsigned long tx_aborted_errors;
789 unsigned long tx_carrier_errors;
790 unsigned long tx_fifo_errors;
791 unsigned long tx_heartbeat_errors;
792 unsigned long tx_window_errors;
796 struct interface *next, *prev;
797 char name[IFNAMSIZ]; /* interface name */
798 short type; /* if type */
799 short flags; /* various flags */
800 int metric; /* routing metric */
801 int mtu; /* MTU value */
802 int tx_queue_len; /* transmit queue length */
803 struct ifmap map; /* hardware setup */
804 struct sockaddr addr; /* IP address */
805 struct sockaddr dstaddr; /* P-P IP address */
806 struct sockaddr broadaddr; /* IP broadcast address */
807 struct sockaddr netmask; /* IP network mask */
808 struct sockaddr ipxaddr_bb; /* IPX network address */
809 struct sockaddr ipxaddr_sn; /* IPX network address */
810 struct sockaddr ipxaddr_e3; /* IPX network address */
811 struct sockaddr ipxaddr_e2; /* IPX network address */
812 struct sockaddr ddpaddr; /* Appletalk DDP address */
813 struct sockaddr ecaddr; /* Econet address */
822 char hwaddr[32]; /* HW address */
823 int statistics_valid;
824 struct user_net_device_stats stats; /* statistics */
825 int keepalive; /* keepalive value for SLIP */
826 int outfill; /* outfill value for SLIP */
830 int interface_opt_a = 0; /* show all interfaces */
833 static int opt_i = 0; /* show the statistics */
834 static int opt_v = 0; /* debugging output flag */
836 static int addr_family = 0; /* currently selected AF */
837 #endif /* KEEP_UNUSED */
839 static struct interface *int_list, *int_last;
840 static int skfd = -1; /* generic raw socket desc. */
843 static int sockets_open(int family)
847 static int force = -1;
851 if (get_kernel_revision() < KRELEASE(2, 1, 0))
853 if (access("/proc/net", R_OK))
856 for (aft = aftypes; *aft; aft++) {
857 struct aftype *af = *aft;
858 int type = SOCK_DGRAM;
860 if (af->af == AF_UNSPEC)
862 if (family && family != af->af)
868 /* Check some /proc file first to not stress kmod */
869 if (!family && !force && af->flag_file) {
870 if (access(af->flag_file, R_OK))
874 if (af->af == AF_NETROM)
875 type = SOCK_SEQPACKET;
878 if (af->af == AF_X25)
879 type = SOCK_SEQPACKET;
881 af->fd = socket(af->af, type, 0);
886 bb_error_msg(_("No usable address families found."));
891 /* like strcmp(), but knows about numbers */
892 static int nstrcmp(const char *a, const char *b)
894 const char *a_ptr = a;
895 const char *b_ptr = b;
901 if (!isdigit(*a) && isdigit(*(a+1))) {
909 if (isdigit(*a) && isdigit(*b)) {
910 return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1;
915 static struct interface *add_interface(char *name)
917 struct interface *ife, **nextp, *new;
919 for (ife = int_last; ife; ife = ife->prev) {
920 int n = nstrcmp(ife->name, name);
928 safe_strncpy(new->name, name, IFNAMSIZ);
929 nextp = ife ? &ife->next : &int_list;
933 new->next->prev = new;
941 static int if_readconf(void)
949 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
951 skfd2 = get_socket_for_af(AF_INET);
953 bb_perror_msg(("warning: no inet socket available"));
954 /* Try to soldier on with whatever socket we can get hold of. */
955 skfd2 = sockets_open(0);
962 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
963 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
965 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
966 perror("SIOCGIFCONF");
969 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
970 /* assume it overflowed and try again */
978 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
979 add_interface(ifr->ifr_name);
989 static char *get_name(char *name, char *p)
996 if (*p == ':') { /* could be an alias */
997 char *dot = p, *dotname = name;
1002 if (*p != ':') { /* it wasn't, backup */
1017 /* If scanf supports size qualifiers for %n conversions, then we can
1018 * use a modified fmt that simply stores the position in the fields
1019 * having no associated fields in the proc string. Of course, we need
1020 * to zero them again when we're done. But that is smaller than the
1021 * old approach of multiple scanf occurrences with large numbers of
1024 /* static const char *ss_fmt[] = { */
1025 /* "%Ln%Lu%lu%lu%lu%lu%ln%ln%Ln%Lu%lu%lu%lu%lu%lu", */
1026 /* "%Lu%Lu%lu%lu%lu%lu%ln%ln%Lu%Lu%lu%lu%lu%lu%lu", */
1027 /* "%Lu%Lu%lu%lu%lu%lu%lu%lu%Lu%Lu%lu%lu%lu%lu%lu%lu" */
1030 /* Lie about the size of the int pointed to for %n. */
1031 #if INT_MAX == LONG_MAX
1032 static const char *ss_fmt[] = {
1033 "%n%Lu%u%u%u%u%n%n%n%Lu%u%u%u%u%u",
1034 "%Lu%Lu%u%u%u%u%n%n%Lu%Lu%u%u%u%u%u",
1035 "%Lu%Lu%u%u%u%u%u%u%Lu%Lu%u%u%u%u%u%u"
1038 static const char *ss_fmt[] = {
1039 "%n%Lu%lu%lu%lu%lu%n%n%n%Lu%lu%lu%lu%lu%lu",
1040 "%Lu%Lu%lu%lu%lu%lu%n%n%Lu%Lu%lu%lu%lu%lu%lu",
1041 "%Lu%Lu%lu%lu%lu%lu%lu%lu%Lu%Lu%lu%lu%lu%lu%lu%lu"
1046 static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
1048 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
1050 sscanf(bp, ss_fmt[procnetdev_vsn],
1051 &ife->stats.rx_bytes, /* missing for 0 */
1052 &ife->stats.rx_packets,
1053 &ife->stats.rx_errors,
1054 &ife->stats.rx_dropped,
1055 &ife->stats.rx_fifo_errors,
1056 &ife->stats.rx_frame_errors,
1057 &ife->stats.rx_compressed, /* missing for <= 1 */
1058 &ife->stats.rx_multicast, /* missing for <= 1 */
1059 &ife->stats.tx_bytes, /* missing for 0 */
1060 &ife->stats.tx_packets,
1061 &ife->stats.tx_errors,
1062 &ife->stats.tx_dropped,
1063 &ife->stats.tx_fifo_errors,
1064 &ife->stats.collisions,
1065 &ife->stats.tx_carrier_errors,
1066 &ife->stats.tx_compressed /* missing for <= 1 */
1069 if (procnetdev_vsn <= 1) {
1070 if (procnetdev_vsn == 0) {
1071 ife->stats.rx_bytes = 0;
1072 ife->stats.tx_bytes = 0;
1074 ife->stats.rx_multicast = 0;
1075 ife->stats.rx_compressed = 0;
1076 ife->stats.tx_compressed = 0;
1080 static inline int procnetdev_version(char *buf)
1082 if (strstr(buf, "compressed"))
1084 if (strstr(buf, "bytes"))
1089 static int if_readlist_proc(char *target)
1091 static int proc_read;
1094 struct interface *ife;
1095 int err, procnetdev_vsn;
1102 fh = fopen(_PATH_PROCNET_DEV, "r");
1104 bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
1105 return if_readconf();
1107 fgets(buf, sizeof buf, fh); /* eat line */
1108 fgets(buf, sizeof buf, fh);
1110 procnetdev_vsn = procnetdev_version(buf);
1113 while (fgets(buf, sizeof buf, fh)) {
1116 s = get_name(name, buf);
1117 ife = add_interface(name);
1118 get_dev_fields(s, ife, procnetdev_vsn);
1119 ife->statistics_valid = 1;
1120 if (target && !strcmp(target, name))
1124 perror(_PATH_PROCNET_DEV);
1132 static int if_readlist(void)
1134 int err = if_readlist_proc(NULL);
1137 err = if_readconf();
1141 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1144 struct interface *ife;
1146 if (!int_list && (if_readlist() < 0))
1148 for (ife = int_list; ife; ife = ife->next) {
1149 int err = doit(ife, cookie);
1157 /* Support for fetching an IPX address */
1160 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1162 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1163 return ioctl(sock, SIOCGIFADDR, ifr);
1168 /* Fetch the interface configuration from the kernel. */
1169 static int if_fetch(struct interface *ife)
1173 char *ifname = ife->name;
1175 strcpy(ifr.ifr_name, ifname);
1176 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1178 ife->flags = ifr.ifr_flags;
1180 strcpy(ifr.ifr_name, ifname);
1181 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1182 memset(ife->hwaddr, 0, 32);
1184 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1186 ife->type = ifr.ifr_hwaddr.sa_family;
1188 strcpy(ifr.ifr_name, ifname);
1189 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1192 ife->metric = ifr.ifr_metric;
1194 strcpy(ifr.ifr_name, ifname);
1195 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1198 ife->mtu = ifr.ifr_mtu;
1201 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1202 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1203 ife->type == ARPHRD_ADAPT) {
1205 strcpy(ifr.ifr_name, ifname);
1206 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1209 ife->outfill = (unsigned int) ifr.ifr_data;
1211 #ifdef SIOCGKEEPALIVE
1212 strcpy(ifr.ifr_name, ifname);
1213 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1216 ife->keepalive = (unsigned int) ifr.ifr_data;
1221 strcpy(ifr.ifr_name, ifname);
1222 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1223 memset(&ife->map, 0, sizeof(struct ifmap));
1225 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1227 strcpy(ifr.ifr_name, ifname);
1228 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1229 memset(&ife->map, 0, sizeof(struct ifmap));
1231 ife->map = ifr.ifr_map;
1233 #ifdef HAVE_TXQUEUELEN
1234 strcpy(ifr.ifr_name, ifname);
1235 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1236 ife->tx_queue_len = -1; /* unknown value */
1238 ife->tx_queue_len = ifr.ifr_qlen;
1240 ife->tx_queue_len = -1; /* unknown value */
1245 fd = get_socket_for_af(AF_INET);
1247 strcpy(ifr.ifr_name, ifname);
1248 ifr.ifr_addr.sa_family = AF_INET;
1249 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1251 ife->addr = ifr.ifr_addr;
1252 strcpy(ifr.ifr_name, ifname);
1253 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1254 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1256 ife->dstaddr = ifr.ifr_dstaddr;
1258 strcpy(ifr.ifr_name, ifname);
1259 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1260 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1262 ife->broadaddr = ifr.ifr_broadaddr;
1264 strcpy(ifr.ifr_name, ifname);
1265 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1266 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1268 ife->netmask = ifr.ifr_netmask;
1270 memset(&ife->addr, 0, sizeof(struct sockaddr));
1275 /* DDP address maybe ? */
1276 fd = get_socket_for_af(AF_APPLETALK);
1278 strcpy(ifr.ifr_name, ifname);
1279 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1280 ife->ddpaddr = ifr.ifr_addr;
1287 /* Look for IPX addresses with all framing types */
1288 fd = get_socket_for_af(AF_IPX);
1290 strcpy(ifr.ifr_name, ifname);
1291 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1292 ife->has_ipx_bb = 1;
1293 ife->ipxaddr_bb = ifr.ifr_addr;
1295 strcpy(ifr.ifr_name, ifname);
1296 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1297 ife->has_ipx_sn = 1;
1298 ife->ipxaddr_sn = ifr.ifr_addr;
1300 strcpy(ifr.ifr_name, ifname);
1301 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1302 ife->has_ipx_e3 = 1;
1303 ife->ipxaddr_e3 = ifr.ifr_addr;
1305 strcpy(ifr.ifr_name, ifname);
1306 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1307 ife->has_ipx_e2 = 1;
1308 ife->ipxaddr_e2 = ifr.ifr_addr;
1314 /* Econet address maybe? */
1315 fd = get_socket_for_af(AF_ECONET);
1317 strcpy(ifr.ifr_name, ifname);
1318 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1319 ife->ecaddr = ifr.ifr_addr;
1320 ife->has_econet = 1;
1329 static int do_if_fetch(struct interface *ife)
1331 if (if_fetch(ife) < 0) {
1334 if (errno == ENODEV) {
1335 /* Give better error message for this case. */
1336 errmsg = _("Device not found");
1338 errmsg = strerror(errno);
1340 bb_error_msg(_("%s: error fetching interface information: %s\n"),
1347 /* This structure defines hardware protocols and their handlers. */
1353 char *(*print) (unsigned char *);
1354 int (*input) (char *, struct sockaddr *);
1355 int (*activate) (int fd);
1356 int suppress_null_addr;
1359 static struct hwtype unspec_hwtype = {
1360 "unspec", "UNSPEC", -1, 0,
1361 UNSPEC_print, NULL, NULL
1364 static struct hwtype loop_hwtype = {
1365 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1370 #include <net/if_arp.h>
1372 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
1373 #include <net/ethernet.h>
1375 #include <linux/if_ether.h>
1378 /* Display an Ethernet address in readable format. */
1379 static char *pr_ether(unsigned char *ptr)
1381 static char buff[64];
1383 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1384 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1385 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1391 /* Input an Ethernet address and convert to binary. */
1392 static int in_ether(char *bufp, struct sockaddr *sap)
1399 sap->sa_family = ether_hwtype.type;
1404 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1409 else if (c >= 'a' && c <= 'f')
1411 else if (c >= 'A' && c <= 'F')
1415 bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
1424 else if (c >= 'a' && c <= 'f')
1425 val |= c - 'a' + 10;
1426 else if (c >= 'A' && c <= 'F')
1427 val |= c - 'A' + 10;
1428 else if (c == ':' || c == 0)
1432 bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
1439 *ptr++ = (unsigned char) (val & 0377);
1442 /* We might get a semicolon here - not required. */
1445 if (i == ETH_ALEN) {
1446 bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
1454 /* That's it. Any trailing junk? */
1455 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1456 bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
1460 bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
1465 #endif /* KEEP_UNUSED */
1468 static struct hwtype ether_hwtype = {
1469 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1470 pr_ether, NULL /* UNUSED in_ether */ , NULL
1474 #endif /* HAVE_HWETHER */
1479 #include <net/if_arp.h>
1482 /* Start the PPP encapsulation on the file descriptor. */
1483 static int do_ppp(int fd)
1485 bb_error_msg(_("You cannot start PPP with this program."));
1488 #endif /* KEEP_UNUSED */
1490 static struct hwtype ppp_hwtype = {
1491 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1492 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
1496 #endif /* HAVE_PPP */
1498 static struct hwtype *hwtypes[] = {
1520 #ifdef ARPHRD_IEEE802_TR
1573 static short sVhwinit = 0;
1575 static void hwinit()
1577 loop_hwtype.title = _("Local Loopback");
1578 unspec_hwtype.title = _("UNSPEC");
1580 slip_hwtype.title = _("Serial Line IP");
1581 cslip_hwtype.title = _("VJ Serial Line IP");
1582 slip6_hwtype.title = _("6-bit Serial Line IP");
1583 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1584 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1587 ether_hwtype.title = _("Ethernet");
1590 ash_hwtype.title = _("Ash");
1593 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1596 hippi_hwtype.title = _("HIPPI");
1599 ax25_hwtype.title = _("AMPR AX.25");
1602 rose_hwtype.title = _("AMPR ROSE");
1605 netrom_hwtype.title = _("AMPR NET/ROM");
1608 x25_hwtype.title = _("generic X.25");
1611 tunnel_hwtype.title = _("IPIP Tunnel");
1614 ppp_hwtype.title = _("Point-to-Point Protocol");
1617 hdlc_hwtype.title = _("(Cisco)-HDLC");
1618 lapb_hwtype.title = _("LAPB");
1621 arcnet_hwtype.title = _("ARCnet");
1624 dlci_hwtype.title = _("Frame Relay DLCI");
1625 frad_hwtype.title = _("Frame Relay Access Device");
1628 sit_hwtype.title = _("IPv6-in-IPv4");
1631 irda_hwtype.title = _("IrLAP");
1634 tr_hwtype.title = _("16/4 Mbps Token Ring");
1635 #ifdef ARPHRD_IEEE802_TR
1636 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
1640 ec_hwtype.title = _("Econet");
1644 #endif /* KEEP_UNUSED */
1648 static const char * const if_port_text[][4] = {
1649 /* Keep in step with <linux/netdevice.h> */
1650 {"unknown", NULL, NULL, NULL},
1651 {"10base2", "bnc", "coax", NULL},
1652 {"10baseT", "utp", "tpe", NULL},
1653 {"AUI", "thick", "db15", NULL},
1654 {"100baseT", NULL, NULL, NULL},
1655 {"100baseTX", NULL, NULL, NULL},
1656 {"100baseFX", NULL, NULL, NULL},
1657 {NULL, NULL, NULL, NULL},
1660 static const char * const if_port_text[] = {
1661 /* Keep in step with <linux/netdevice.h> */
1674 /* Check our hardware type table for this type. */
1675 static struct hwtype *get_hwntype(int type)
1677 struct hwtype **hwp;
1682 #endif /* KEEP_UNUSED */
1685 while (*hwp != NULL) {
1686 if ((*hwp)->type == type)
1693 /* return 1 if address is all zeros */
1694 static int hw_null_address(struct hwtype *hw, void *ap)
1697 unsigned char *address = (unsigned char *) ap;
1699 for (i = 0; i < hw->alen; i++)
1705 static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti";
1707 static void print_bytes_scaled(unsigned long long ull, const char *end)
1709 unsigned long long int_part;
1711 unsigned int frac_part;
1720 /* This does correct rounding and is a little larger. But it
1721 * uses KiB as the smallest displayed unit. */
1722 if ((int_part < (1024*1024 - 51)) || !--i) {
1724 int_part += 51; /* 1024*.05 = 51.2 */
1725 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1728 ext += 3; /* KiB, MiB, GiB, TiB */
1730 if (int_part >= 1024) {
1731 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
1733 ext += 3; /* KiB, MiB, GiB, TiB */
1739 printf("X bytes:%Lu (%Lu.%u %sB)%s", ull, int_part, frac_part, ext, end);
1742 static const char * const ife_print_flags_strs[] = {
1761 static const unsigned short ife_print_flags_mask[] = {
1781 static void ife_print(struct interface *ptr)
1786 int can_compress = 0;
1789 static struct aftype *ipxtype = NULL;
1792 static struct aftype *ectype = NULL;
1795 static struct aftype *ddptype = NULL;
1799 char addr6[40], devname[20];
1800 struct sockaddr_in6 sap;
1801 int plen, scope, dad_status, if_idx;
1805 ap = get_afntype(ptr->addr.sa_family);
1807 ap = get_afntype(0);
1811 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1814 hw = get_hwntype(hf);
1816 hw = get_hwntype(-1);
1818 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1819 /* For some hardware types (eg Ash, ATM) we don't print the
1820 hardware address if it's null. */
1821 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1822 hw->suppress_null_addr)))
1823 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1825 if (ptr->flags & IFF_PORTSEL) {
1826 printf(_("Media:%s"), if_port_text[ptr->map.port] /* [0] */);
1827 if (ptr->flags & IFF_AUTOMEDIA)
1828 printf(_("(auto)"));
1835 printf(_(" %s addr:%s "), ap->name,
1836 ap->sprint(&ptr->addr, 1));
1837 if (ptr->flags & IFF_POINTOPOINT) {
1838 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1840 if (ptr->flags & IFF_BROADCAST) {
1841 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1843 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1849 #define IPV6_ADDR_ANY 0x0000U
1851 #define IPV6_ADDR_UNICAST 0x0001U
1852 #define IPV6_ADDR_MULTICAST 0x0002U
1853 #define IPV6_ADDR_ANYCAST 0x0004U
1855 #define IPV6_ADDR_LOOPBACK 0x0010U
1856 #define IPV6_ADDR_LINKLOCAL 0x0020U
1857 #define IPV6_ADDR_SITELOCAL 0x0040U
1859 #define IPV6_ADDR_COMPATv4 0x0080U
1861 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1863 #define IPV6_ADDR_MAPPED 0x1000U
1864 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1866 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1868 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1869 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1870 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1871 &dad_status, devname) != EOF) {
1872 if (!strcmp(devname, ptr->name)) {
1873 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1874 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1875 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1876 inet_pton(AF_INET6, addr6,
1877 (struct sockaddr *) &sap.sin6_addr);
1878 sap.sin6_family = AF_INET6;
1879 printf(_(" inet6 addr: %s/%d"),
1880 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1882 printf(_(" Scope:"));
1883 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1885 printf(_("Global"));
1887 case IPV6_ADDR_LINKLOCAL:
1890 case IPV6_ADDR_SITELOCAL:
1893 case IPV6_ADDR_COMPATv4:
1894 printf(_("Compat"));
1896 case IPV6_ADDR_LOOPBACK:
1900 printf(_("Unknown"));
1910 if (ipxtype == NULL)
1911 ipxtype = get_afntype(AF_IPX);
1913 if (ipxtype != NULL) {
1914 if (ptr->has_ipx_bb)
1915 printf(_(" IPX/Ethernet II addr:%s\n"),
1916 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1917 if (ptr->has_ipx_sn)
1918 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1919 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1920 if (ptr->has_ipx_e2)
1921 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1922 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1923 if (ptr->has_ipx_e3)
1924 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1925 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1930 if (ddptype == NULL)
1931 ddptype = get_afntype(AF_APPLETALK);
1932 if (ddptype != NULL) {
1934 printf(_(" EtherTalk Phase 2 addr:%s\n"),
1935 ddptype->sprint(&ptr->ddpaddr, 1));
1941 ectype = get_afntype(AF_ECONET);
1942 if (ectype != NULL) {
1943 if (ptr->has_econet)
1944 printf(_(" econet addr:%s\n"),
1945 ectype->sprint(&ptr->ecaddr, 1));
1950 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1952 if (ptr->flags == 0) {
1953 printf(_("[NO FLAGS] "));
1957 if (ptr->flags & ife_print_flags_mask[i]) {
1958 printf(_(ife_print_flags_strs[i]));
1960 } while (ife_print_flags_mask[++i]);
1963 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1964 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1965 #ifdef SIOCSKEEPALIVE
1966 if (ptr->outfill || ptr->keepalive)
1967 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1971 /* If needed, display the interface statistics. */
1973 if (ptr->statistics_valid) {
1974 /* XXX: statistics are currently only printed for the primary address,
1975 * not for the aliases, although strictly speaking they're shared
1980 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1981 ptr->stats.rx_packets, ptr->stats.rx_errors,
1982 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1983 ptr->stats.rx_frame_errors);
1985 printf(_(" compressed:%lu\n"),
1986 ptr->stats.rx_compressed);
1988 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1989 ptr->stats.tx_packets, ptr->stats.tx_errors,
1990 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1991 ptr->stats.tx_carrier_errors);
1992 printf(_(" collisions:%lu "), ptr->stats.collisions);
1994 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1995 if (ptr->tx_queue_len != -1)
1996 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1998 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1999 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2003 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2004 ptr->map.base_addr)) {
2007 printf(_("Interrupt:%d "), ptr->map.irq);
2008 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2010 printf(_("Base address:0x%lx "),
2011 (unsigned long) ptr->map.base_addr);
2012 if (ptr->map.mem_start) {
2013 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
2017 printf(_("DMA chan:%x "), ptr->map.dma);
2024 static int do_if_print(struct interface *ife, void *cookie)
2026 int *opt_a = (int *) cookie;
2029 res = do_if_fetch(ife);
2031 if ((ife->flags & IFF_UP) || *opt_a)
2037 static struct interface *lookup_interface(char *name)
2039 struct interface *ife = NULL;
2041 if (if_readlist_proc(name) < 0)
2043 ife = add_interface(name);
2047 /* for ipv4 add/del modes */
2048 static int if_print(char *ifname)
2053 res = for_all_interfaces(do_if_print, &interface_opt_a);
2055 struct interface *ife;
2057 ife = lookup_interface(ifname);
2058 res = do_if_fetch(ife);
2065 int display_interfaces(char *ifname)
2069 /* Create a channel to the NET kernel. */
2070 if ((skfd = sockets_open(0)) < 0) {
2071 bb_perror_msg_and_die("socket");
2074 /* Do we have to show the current setup? */
2075 status = if_print(ifname);