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.17 2003/07/22 08:56:46 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 */
54 #ifdef CONFIG_FEATURE_IPV6
55 # define HAVE_AFINET6 1
62 * Device Hardware types.
65 #define HAVE_HWETHER 1
70 #include "inet_common.h"
78 #include <sys/ioctl.h>
79 #include <sys/types.h>
81 #include <net/if_arp.h>
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))
90 static int procnetdev_vsn = 1;
93 #include <net/if_slip.h>
100 * This is in linux/include/net/ipv6.h.
104 struct in6_addr ifr6_addr;
105 __u32 ifr6_prefixlen;
106 unsigned int ifr6_ifindex;
111 #endif /* HAVE_AFINET6 */
114 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
115 #include <netipx/ipx.h>
121 /* Defines for glibc2.0 users. */
122 #ifndef SIOCSIFTXQLEN
123 #define SIOCSIFTXQLEN 0x8943
124 #define SIOCGIFTXQLEN 0x8942
127 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
129 #define ifr_qlen ifr_ifru.ifru_mtu
132 #ifndef HAVE_TXQUEUELEN
133 #define HAVE_TXQUEUELEN 1
137 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
140 /* This structure defines protocol families and their handlers. */
146 char *(*print) (unsigned char *);
147 char *(*sprint) (struct sockaddr *, int numeric);
148 int (*input) (int type, char *bufp, struct sockaddr *);
149 void (*herror) (char *text);
150 int (*rprint) (int options);
151 int (*rinput) (int typ, int ext, char **argv);
154 int (*getmask) (char *src, struct sockaddr * mask, char *name);
168 static int flag_ax25;
174 static int flag_netrom;
176 static int flag_inet;
179 static int flag_inet6;
182 static int flag_econet;
185 static int flag_x25 = 0;
192 static struct aftrans_t {
200 "ax25", "ax25", &flag_ax25},
203 "ip", "inet", &flag_inet},
206 "ip6", "inet6", &flag_inet6},
210 "ipx", "ipx", &flag_ipx},
214 "appletalk", "ddp", &flag_ddp},
218 "netrom", "netrom", &flag_netrom},
221 "inet", "inet", &flag_inet},
224 "inet6", "inet6", &flag_inet6},
228 "ddp", "ddp", &flag_ddp},
231 "unix", "unix", &flag_unx}, {
232 "tcpip", "inet", &flag_inet},
235 "econet", "ec", &flag_econet},
239 "x25", "x25", &flag_x25},
243 "ash", "ash", &flag_ash},
249 static char afname[256] = "";
250 #endif /* KEEP_UNUSED */
254 /* Display a UNIX domain address. */
255 static char *UNIX_print(unsigned char *ptr)
261 /* Display a UNIX domain address. */
262 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
266 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
267 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
268 return (UNIX_print(sap->sa_data));
272 static struct aftype unix_aftype = {
273 "unix", "UNIX Domain", AF_UNIX, 0,
274 UNIX_print, UNIX_sprint, NULL, NULL,
279 #endif /* HAVE_AFUNIX */
284 static void INET_reserror(char *text)
289 /* Display an Internet socket address. */
290 static char *INET_print(unsigned char *ptr)
292 return (inet_ntoa((*(struct in_addr *) ptr)));
294 #endif /* KEEP_UNUSED */
296 /* Display an Internet socket address. */
297 static char *INET_sprint(struct sockaddr *sap, int numeric)
299 static char buff[128];
301 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
302 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
304 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
305 numeric, 0xffffff00) != 0)
312 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
313 unsigned int netmask)
315 static char buff[128];
317 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
318 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
319 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
320 numeric, netmask) != 0)
325 static int INET_getsock(char *bufp, struct sockaddr *sap)
327 char *sp = bufp, *bp;
330 struct sockaddr_in *sin;
332 sin = (struct sockaddr_in *) sap;
333 sin->sin_family = AF_INET;
338 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
341 if ((*sp >= 'A') && (*sp <= 'F'))
342 bp[i] |= (int) (*sp - 'A') + 10;
343 else if ((*sp >= '0') && (*sp <= '9'))
344 bp[i] |= (int) (*sp - '0');
352 if ((*sp >= 'A') && (*sp <= 'F'))
353 bp[i] |= (int) (*sp - 'A') + 10;
354 else if ((*sp >= '0') && (*sp <= '9'))
355 bp[i] |= (int) (*sp - '0');
361 sin->sin_addr.s_addr = htonl(val);
366 static int INET_input(int type, char *bufp, struct sockaddr *sap)
370 return (INET_getsock(bufp, sap));
372 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
374 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
378 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
380 struct sockaddr_in *mask = (struct sockaddr_in *) m;
384 if ((slash = strchr(adr, '/')) == NULL)
388 prefix = strtoul(slash, &end, 0);
393 sprintf(name, "/%d", prefix);
395 mask->sin_family = AF_INET;
396 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
399 #endif /* KEEP_UNUSED */
401 static struct aftype inet_aftype = {
402 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
403 NULL /* UNUSED INET_print */ , INET_sprint,
404 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
405 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
406 NULL /* UNUSED INET_getnetmask */ ,
411 #endif /* HAVE_AFINET */
416 static void INET6_reserror(char *text)
421 /* Display an Internet socket address. */
422 static char *INET6_print(unsigned char *ptr)
424 static char name[80];
426 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
429 #endif /* KEEP_UNUSED */
431 /* Display an Internet socket address. */
432 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
433 static char *INET6_sprint(struct sockaddr *sap, int numeric)
435 static char buff[128];
437 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
438 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
440 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
441 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
446 static int INET6_getsock(char *bufp, struct sockaddr *sap)
448 struct sockaddr_in6 *sin6;
450 sin6 = (struct sockaddr_in6 *) sap;
451 sin6->sin6_family = AF_INET6;
454 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
460 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
464 return (INET6_getsock(bufp, sap));
466 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
469 #endif /* KEEP_UNUSED */
471 static struct aftype inet6_aftype = {
472 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
473 NULL /* UNUSED INET6_print */ , INET6_sprint,
474 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
475 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
476 NULL /* UNUSED INET6_getnetmask */ ,
481 #endif /* HAVE_AFINET6 */
483 /* Display an UNSPEC address. */
484 static char *UNSPEC_print(unsigned char *ptr)
486 static char buff[sizeof(struct sockaddr) * 3 + 1];
491 for (i = 0; i < sizeof(struct sockaddr); i++) {
492 /* careful -- not every libc's sprintf returns # bytes written */
493 sprintf(pos, "%02X-", (*ptr++ & 0377));
496 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
501 /* Display an UNSPEC socket address. */
502 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
506 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
507 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
508 return (UNSPEC_print(sap->sa_data));
511 static struct aftype unspec_aftype = {
512 "unspec", "UNSPEC", AF_UNSPEC, 0,
513 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
517 static struct aftype *aftypes[] = {
556 static short sVafinit = 0;
560 unspec_aftype.title = _("UNSPEC");
562 unix_aftype.title = _("UNIX Domain");
565 inet_aftype.title = _("DARPA Internet");
568 inet6_aftype.title = _("IPv6");
571 ax25_aftype.title = _("AMPR AX.25");
574 netrom_aftype.title = _("AMPR NET/ROM");
577 ipx_aftype.title = _("Novell IPX");
580 ddp_aftype.title = _("Appletalk DDP");
583 ec_aftype.title = _("Econet");
586 x25_aftype.title = _("CCITT X.25");
589 rose_aftype.title = _("AMPR ROSE");
592 ash_aftype.title = _("Ash");
597 static int aftrans_opt(const char *arg)
599 struct aftrans_t *paft;
603 safe_strncpy(buf, arg, sizeof(buf));
609 tmp2 = strchr(tmp1, ',');
615 for (paft = aftrans; paft->alias; paft++) {
616 if (strcmp(tmp1, paft->alias))
618 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
619 bb_error_msg(_("Too many address family arguments."));
626 strcat(afname, paft->name);
630 bb_error_msg(_("Unknown address family `%s'."), tmp1);
639 /* set the default AF list from the program name or a constant value */
640 static void aftrans_def(char *tool, char *argv0, char *dflt)
645 strcpy(afname, dflt);
647 if (!(tmp = strrchr(argv0, '/')))
648 tmp = argv0; /* no slash?! */
652 if (!(buf = strdup(tmp)))
655 if (strlen(tool) >= strlen(tmp)) {
659 tmp = buf + (strlen(tmp) - strlen(tool));
661 if (strcmp(tmp, tool) != 0) {
666 if ((tmp = strchr(buf, '_')))
670 if (aftrans_opt(buf))
676 /* Check our protocol family table for this family. */
677 static struct aftype *get_aftype(const char *name)
684 #endif /* KEEP_UNUSED */
687 while (*afp != NULL) {
688 if (!strcmp((*afp)->name, name))
692 if (strchr(name, ','))
693 bb_error_msg(_("Please don't supply more than one address family."));
696 #endif /* KEEP_UNUSED */
698 /* Check our protocol family table for this family. */
699 static struct aftype *get_afntype(int af)
706 #endif /* KEEP_UNUSED */
709 while (*afp != NULL) {
710 if ((*afp)->af == af)
717 /* Check our protocol family table for this family and return its socket */
718 static int get_socket_for_af(int af)
725 #endif /* KEEP_UNUSED */
728 while (*afp != NULL) {
729 if ((*afp)->af == af)
737 /* type: 0=all, 1=getroute */
738 static void print_aflist(int type)
747 #endif /* KEEP_UNUSED */
750 while (*afp != NULL) {
751 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
755 if ((count % 3) == 0)
756 fprintf(stderr, count ? "\n " : " ");
760 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
764 fprintf(stderr, "\n");
766 #endif /* KEEP_UNUSED */
768 struct user_net_device_stats {
769 unsigned long long rx_packets; /* total packets received */
770 unsigned long long tx_packets; /* total packets transmitted */
771 unsigned long long rx_bytes; /* total bytes received */
772 unsigned long long tx_bytes; /* total bytes transmitted */
773 unsigned long rx_errors; /* bad packets received */
774 unsigned long tx_errors; /* packet transmit problems */
775 unsigned long rx_dropped; /* no space in linux buffers */
776 unsigned long tx_dropped; /* no space available in linux */
777 unsigned long rx_multicast; /* multicast packets received */
778 unsigned long rx_compressed;
779 unsigned long tx_compressed;
780 unsigned long collisions;
782 /* detailed rx_errors: */
783 unsigned long rx_length_errors;
784 unsigned long rx_over_errors; /* receiver ring buff overflow */
785 unsigned long rx_crc_errors; /* recved pkt with crc error */
786 unsigned long rx_frame_errors; /* recv'd frame alignment error */
787 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
788 unsigned long rx_missed_errors; /* receiver missed packet */
789 /* detailed tx_errors */
790 unsigned long tx_aborted_errors;
791 unsigned long tx_carrier_errors;
792 unsigned long tx_fifo_errors;
793 unsigned long tx_heartbeat_errors;
794 unsigned long tx_window_errors;
798 struct interface *next, *prev;
799 char name[IFNAMSIZ]; /* interface name */
800 short type; /* if type */
801 short flags; /* various flags */
802 int metric; /* routing metric */
803 int mtu; /* MTU value */
804 int tx_queue_len; /* transmit queue length */
805 struct ifmap map; /* hardware setup */
806 struct sockaddr addr; /* IP address */
807 struct sockaddr dstaddr; /* P-P IP address */
808 struct sockaddr broadaddr; /* IP broadcast address */
809 struct sockaddr netmask; /* IP network mask */
810 struct sockaddr ipxaddr_bb; /* IPX network address */
811 struct sockaddr ipxaddr_sn; /* IPX network address */
812 struct sockaddr ipxaddr_e3; /* IPX network address */
813 struct sockaddr ipxaddr_e2; /* IPX network address */
814 struct sockaddr ddpaddr; /* Appletalk DDP address */
815 struct sockaddr ecaddr; /* Econet address */
824 char hwaddr[32]; /* HW address */
825 int statistics_valid;
826 struct user_net_device_stats stats; /* statistics */
827 int keepalive; /* keepalive value for SLIP */
828 int outfill; /* outfill value for SLIP */
832 int interface_opt_a = 0; /* show all interfaces */
835 static int opt_i = 0; /* show the statistics */
836 static int opt_v = 0; /* debugging output flag */
838 static int addr_family = 0; /* currently selected AF */
839 #endif /* KEEP_UNUSED */
841 static struct interface *int_list, *int_last;
842 static int skfd = -1; /* generic raw socket desc. */
845 static int sockets_open(int family)
849 static int force = -1;
853 if (get_kernel_revision() < KRELEASE(2, 1, 0))
855 if (access("/proc/net", R_OK))
858 for (aft = aftypes; *aft; aft++) {
859 struct aftype *af = *aft;
860 int type = SOCK_DGRAM;
862 if (af->af == AF_UNSPEC)
864 if (family && family != af->af)
870 /* Check some /proc file first to not stress kmod */
871 if (!family && !force && af->flag_file) {
872 if (access(af->flag_file, R_OK))
876 if (af->af == AF_NETROM)
877 type = SOCK_SEQPACKET;
880 if (af->af == AF_X25)
881 type = SOCK_SEQPACKET;
883 af->fd = socket(af->af, type, 0);
888 bb_error_msg(_("No usable address families found."));
893 /* like strcmp(), but knows about numbers */
894 static int nstrcmp(const char *astr, const char *b)
896 const char *a = astr;
917 return atoi(a) > atoi(b) ? 1 : -1;
922 static struct interface *add_interface(char *name)
924 struct interface *ife, **nextp, *new;
926 for (ife = int_last; ife; ife = ife->prev) {
927 int n = nstrcmp(ife->name, name);
935 safe_strncpy(new->name, name, IFNAMSIZ);
936 nextp = ife ? &ife->next : &int_list;
940 new->next->prev = new;
948 static int if_readconf(void)
956 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
958 skfd2 = get_socket_for_af(AF_INET);
960 bb_perror_msg(("warning: no inet socket available"));
961 /* Try to soldier on with whatever socket we can get hold of. */
962 skfd2 = sockets_open(0);
969 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
970 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
972 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
973 perror("SIOCGIFCONF");
976 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
977 /* assume it overflowed and try again */
985 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
986 add_interface(ifr->ifr_name);
996 static char *get_name(char *name, char *p)
1003 if (*p == ':') { /* could be an alias */
1004 char *dot = p, *dotname = name;
1009 if (*p != ':') { /* it wasn't, backup */
1024 static int get_dev_fields(char *bp, struct interface *ife)
1026 switch (procnetdev_vsn) {
1029 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1030 &ife->stats.rx_bytes,
1031 &ife->stats.rx_packets,
1032 &ife->stats.rx_errors,
1033 &ife->stats.rx_dropped,
1034 &ife->stats.rx_fifo_errors,
1035 &ife->stats.rx_frame_errors,
1036 &ife->stats.rx_compressed,
1037 &ife->stats.rx_multicast,
1038 &ife->stats.tx_bytes,
1039 &ife->stats.tx_packets,
1040 &ife->stats.tx_errors,
1041 &ife->stats.tx_dropped,
1042 &ife->stats.tx_fifo_errors,
1043 &ife->stats.collisions,
1044 &ife->stats.tx_carrier_errors, &ife->stats.tx_compressed);
1047 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1048 &ife->stats.rx_bytes,
1049 &ife->stats.rx_packets,
1050 &ife->stats.rx_errors,
1051 &ife->stats.rx_dropped,
1052 &ife->stats.rx_fifo_errors,
1053 &ife->stats.rx_frame_errors,
1054 &ife->stats.tx_bytes,
1055 &ife->stats.tx_packets,
1056 &ife->stats.tx_errors,
1057 &ife->stats.tx_dropped,
1058 &ife->stats.tx_fifo_errors,
1059 &ife->stats.collisions, &ife->stats.tx_carrier_errors);
1060 ife->stats.rx_multicast = 0;
1063 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1064 &ife->stats.rx_packets,
1065 &ife->stats.rx_errors,
1066 &ife->stats.rx_dropped,
1067 &ife->stats.rx_fifo_errors,
1068 &ife->stats.rx_frame_errors,
1069 &ife->stats.tx_packets,
1070 &ife->stats.tx_errors,
1071 &ife->stats.tx_dropped,
1072 &ife->stats.tx_fifo_errors,
1073 &ife->stats.collisions, &ife->stats.tx_carrier_errors);
1074 ife->stats.rx_bytes = 0;
1075 ife->stats.tx_bytes = 0;
1076 ife->stats.rx_multicast = 0;
1082 static inline int procnetdev_version(char *buf)
1084 if (strstr(buf, "compressed"))
1086 if (strstr(buf, "bytes"))
1091 static int if_readlist_proc(char *target)
1093 static int proc_read;
1096 struct interface *ife;
1104 fh = fopen(_PATH_PROCNET_DEV, "r");
1106 bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
1107 return if_readconf();
1109 fgets(buf, sizeof buf, fh); /* eat line */
1110 fgets(buf, sizeof buf, fh);
1112 #if 0 /* pretty, but can't cope with missing fields */
1113 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh, "face", "", /* parsed separately */
1120 "compressed", "%lu",
1128 "carrier", "%lu", "compressed", "%lu", NULL);
1132 procnetdev_vsn = procnetdev_version(buf);
1136 while (fgets(buf, sizeof buf, fh)) {
1137 char *s, name[IFNAMSIZ];
1139 s = get_name(name, buf);
1140 ife = add_interface(name);
1141 get_dev_fields(s, ife);
1142 ife->statistics_valid = 1;
1143 if (target && !strcmp(target, name))
1147 perror(_PATH_PROCNET_DEV);
1158 static int if_readlist(void)
1160 int err = if_readlist_proc(NULL);
1163 err = if_readconf();
1167 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1170 struct interface *ife;
1172 if (!int_list && (if_readlist() < 0))
1174 for (ife = int_list; ife; ife = ife->next) {
1175 int err = doit(ife, cookie);
1183 /* Support for fetching an IPX address */
1186 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1188 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1189 return ioctl(sock, SIOCGIFADDR, ifr);
1194 /* Fetch the interface configuration from the kernel. */
1195 static int if_fetch(struct interface *ife)
1199 char *ifname = ife->name;
1201 strcpy(ifr.ifr_name, ifname);
1202 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1204 ife->flags = ifr.ifr_flags;
1206 strcpy(ifr.ifr_name, ifname);
1207 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1208 memset(ife->hwaddr, 0, 32);
1210 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1212 ife->type = ifr.ifr_hwaddr.sa_family;
1214 strcpy(ifr.ifr_name, ifname);
1215 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1218 ife->metric = ifr.ifr_metric;
1220 strcpy(ifr.ifr_name, ifname);
1221 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1224 ife->mtu = ifr.ifr_mtu;
1227 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1228 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1229 ife->type == ARPHRD_ADAPT) {
1231 strcpy(ifr.ifr_name, ifname);
1232 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1235 ife->outfill = (unsigned int) ifr.ifr_data;
1237 #ifdef SIOCGKEEPALIVE
1238 strcpy(ifr.ifr_name, ifname);
1239 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1242 ife->keepalive = (unsigned int) ifr.ifr_data;
1247 strcpy(ifr.ifr_name, ifname);
1248 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1249 memset(&ife->map, 0, sizeof(struct ifmap));
1251 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1253 strcpy(ifr.ifr_name, ifname);
1254 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1255 memset(&ife->map, 0, sizeof(struct ifmap));
1257 ife->map = ifr.ifr_map;
1259 #ifdef HAVE_TXQUEUELEN
1260 strcpy(ifr.ifr_name, ifname);
1261 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1262 ife->tx_queue_len = -1; /* unknown value */
1264 ife->tx_queue_len = ifr.ifr_qlen;
1266 ife->tx_queue_len = -1; /* unknown value */
1271 fd = get_socket_for_af(AF_INET);
1273 strcpy(ifr.ifr_name, ifname);
1274 ifr.ifr_addr.sa_family = AF_INET;
1275 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1277 ife->addr = ifr.ifr_addr;
1278 strcpy(ifr.ifr_name, ifname);
1279 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1280 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1282 ife->dstaddr = ifr.ifr_dstaddr;
1284 strcpy(ifr.ifr_name, ifname);
1285 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1286 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1288 ife->broadaddr = ifr.ifr_broadaddr;
1290 strcpy(ifr.ifr_name, ifname);
1291 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1292 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1294 ife->netmask = ifr.ifr_netmask;
1296 memset(&ife->addr, 0, sizeof(struct sockaddr));
1301 /* DDP address maybe ? */
1302 fd = get_socket_for_af(AF_APPLETALK);
1304 strcpy(ifr.ifr_name, ifname);
1305 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1306 ife->ddpaddr = ifr.ifr_addr;
1313 /* Look for IPX addresses with all framing types */
1314 fd = get_socket_for_af(AF_IPX);
1316 strcpy(ifr.ifr_name, ifname);
1317 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1318 ife->has_ipx_bb = 1;
1319 ife->ipxaddr_bb = ifr.ifr_addr;
1321 strcpy(ifr.ifr_name, ifname);
1322 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1323 ife->has_ipx_sn = 1;
1324 ife->ipxaddr_sn = ifr.ifr_addr;
1326 strcpy(ifr.ifr_name, ifname);
1327 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1328 ife->has_ipx_e3 = 1;
1329 ife->ipxaddr_e3 = ifr.ifr_addr;
1331 strcpy(ifr.ifr_name, ifname);
1332 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1333 ife->has_ipx_e2 = 1;
1334 ife->ipxaddr_e2 = ifr.ifr_addr;
1340 /* Econet address maybe? */
1341 fd = get_socket_for_af(AF_ECONET);
1343 strcpy(ifr.ifr_name, ifname);
1344 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1345 ife->ecaddr = ifr.ifr_addr;
1346 ife->has_econet = 1;
1355 static int do_if_fetch(struct interface *ife)
1357 if (if_fetch(ife) < 0) {
1360 if (errno == ENODEV) {
1361 /* Give better error message for this case. */
1362 errmsg = _("Device not found");
1364 errmsg = strerror(errno);
1366 bb_error_msg(_("%s: error fetching interface information: %s\n"),
1373 /* This structure defines hardware protocols and their handlers. */
1379 char *(*print) (unsigned char *);
1380 int (*input) (char *, struct sockaddr *);
1381 int (*activate) (int fd);
1382 int suppress_null_addr;
1385 static struct hwtype unspec_hwtype = {
1386 "unspec", "UNSPEC", -1, 0,
1387 UNSPEC_print, NULL, NULL
1390 static struct hwtype loop_hwtype = {
1391 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1396 #include <net/if_arp.h>
1398 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
1399 #include <net/ethernet.h>
1401 #include <linux/if_ether.h>
1404 /* Display an Ethernet address in readable format. */
1405 static char *pr_ether(unsigned char *ptr)
1407 static char buff[64];
1409 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1410 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1411 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1417 /* Input an Ethernet address and convert to binary. */
1418 static int in_ether(char *bufp, struct sockaddr *sap)
1425 sap->sa_family = ether_hwtype.type;
1430 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1435 else if (c >= 'a' && c <= 'f')
1437 else if (c >= 'A' && c <= 'F')
1441 bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
1450 else if (c >= 'a' && c <= 'f')
1451 val |= c - 'a' + 10;
1452 else if (c >= 'A' && c <= 'F')
1453 val |= c - 'A' + 10;
1454 else if (c == ':' || c == 0)
1458 bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
1465 *ptr++ = (unsigned char) (val & 0377);
1468 /* We might get a semicolon here - not required. */
1471 if (i == ETH_ALEN) {
1472 bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
1480 /* That's it. Any trailing junk? */
1481 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1482 bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
1486 bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
1491 #endif /* KEEP_UNUSED */
1494 static struct hwtype ether_hwtype = {
1495 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1496 pr_ether, NULL /* UNUSED in_ether */ , NULL
1500 #endif /* HAVE_HWETHER */
1505 #include <net/if_arp.h>
1508 /* Start the PPP encapsulation on the file descriptor. */
1509 static int do_ppp(int fd)
1511 bb_error_msg(_("You cannot start PPP with this program."));
1514 #endif /* KEEP_UNUSED */
1516 static struct hwtype ppp_hwtype = {
1517 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1518 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
1522 #endif /* HAVE_PPP */
1524 static struct hwtype *hwtypes[] = {
1546 #ifdef ARPHRD_IEEE802_TR
1599 static short sVhwinit = 0;
1601 static void hwinit()
1603 loop_hwtype.title = _("Local Loopback");
1604 unspec_hwtype.title = _("UNSPEC");
1606 slip_hwtype.title = _("Serial Line IP");
1607 cslip_hwtype.title = _("VJ Serial Line IP");
1608 slip6_hwtype.title = _("6-bit Serial Line IP");
1609 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1610 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1613 ether_hwtype.title = _("Ethernet");
1616 ash_hwtype.title = _("Ash");
1619 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1622 hippi_hwtype.title = _("HIPPI");
1625 ax25_hwtype.title = _("AMPR AX.25");
1628 rose_hwtype.title = _("AMPR ROSE");
1631 netrom_hwtype.title = _("AMPR NET/ROM");
1634 x25_hwtype.title = _("generic X.25");
1637 tunnel_hwtype.title = _("IPIP Tunnel");
1640 ppp_hwtype.title = _("Point-to-Point Protocol");
1643 hdlc_hwtype.title = _("(Cisco)-HDLC");
1644 lapb_hwtype.title = _("LAPB");
1647 arcnet_hwtype.title = _("ARCnet");
1650 dlci_hwtype.title = _("Frame Relay DLCI");
1651 frad_hwtype.title = _("Frame Relay Access Device");
1654 sit_hwtype.title = _("IPv6-in-IPv4");
1657 irda_hwtype.title = _("IrLAP");
1660 tr_hwtype.title = _("16/4 Mbps Token Ring");
1661 #ifdef ARPHRD_IEEE802_TR
1662 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
1666 ec_hwtype.title = _("Econet");
1670 #endif /* KEEP_UNUSED */
1673 static const char *if_port_text[][4] = {
1674 /* Keep in step with <linux/netdevice.h> */
1675 {"unknown", NULL, NULL, NULL},
1676 {"10base2", "bnc", "coax", NULL},
1677 {"10baseT", "utp", "tpe", NULL},
1678 {"AUI", "thick", "db15", NULL},
1679 {"100baseT", NULL, NULL, NULL},
1680 {"100baseTX", NULL, NULL, NULL},
1681 {"100baseFX", NULL, NULL, NULL},
1682 {NULL, NULL, NULL, NULL},
1686 /* Check our hardware type table for this type. */
1687 static struct hwtype *get_hwntype(int type)
1689 struct hwtype **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(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\0k\0M";
1719 static void print_bytes_scaled(unsigned long long ull, const char *end)
1721 unsigned long long int_part;
1722 unsigned long frac_part;
1729 for (i = 0; i < 2; i++) {
1730 if (int_part >= 1024) {
1731 frac_part = ((int_part % 1024) * 10) / 1024;
1733 ext += 2; /* Kb, Mb */
1737 printf("X bytes:%Lu (%Lu.%lu %siB)%s", ull, int_part, frac_part, ext, end);
1740 static void ife_print(struct interface *ptr)
1745 int can_compress = 0;
1748 static struct aftype *ipxtype = NULL;
1751 static struct aftype *ectype = NULL;
1754 static struct aftype *ddptype = NULL;
1758 char addr6[40], devname[20];
1759 struct sockaddr_in6 sap;
1760 int plen, scope, dad_status, if_idx;
1764 ap = get_afntype(ptr->addr.sa_family);
1766 ap = get_afntype(0);
1770 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1773 hw = get_hwntype(hf);
1775 hw = get_hwntype(-1);
1777 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1778 /* For some hardware types (eg Ash, ATM) we don't print the
1779 hardware address if it's null. */
1780 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1781 hw->suppress_null_addr)))
1782 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1784 if (ptr->flags & IFF_PORTSEL) {
1785 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1786 if (ptr->flags & IFF_AUTOMEDIA)
1787 printf(_("(auto)"));
1794 printf(_(" %s addr:%s "), ap->name,
1795 ap->sprint(&ptr->addr, 1));
1796 if (ptr->flags & IFF_POINTOPOINT) {
1797 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1799 if (ptr->flags & IFF_BROADCAST) {
1800 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1802 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1808 #define IPV6_ADDR_ANY 0x0000U
1810 #define IPV6_ADDR_UNICAST 0x0001U
1811 #define IPV6_ADDR_MULTICAST 0x0002U
1812 #define IPV6_ADDR_ANYCAST 0x0004U
1814 #define IPV6_ADDR_LOOPBACK 0x0010U
1815 #define IPV6_ADDR_LINKLOCAL 0x0020U
1816 #define IPV6_ADDR_SITELOCAL 0x0040U
1818 #define IPV6_ADDR_COMPATv4 0x0080U
1820 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1822 #define IPV6_ADDR_MAPPED 0x1000U
1823 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1825 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1827 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1828 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1829 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1830 &dad_status, devname) != EOF) {
1831 if (!strcmp(devname, ptr->name)) {
1832 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1833 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1834 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1835 inet_pton(AF_INET6, addr6,
1836 (struct sockaddr *) &sap.sin6_addr);
1837 sap.sin6_family = AF_INET6;
1838 printf(_(" inet6 addr: %s/%d"),
1839 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1841 printf(_(" Scope:"));
1842 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1844 printf(_("Global"));
1846 case IPV6_ADDR_LINKLOCAL:
1849 case IPV6_ADDR_SITELOCAL:
1852 case IPV6_ADDR_COMPATv4:
1853 printf(_("Compat"));
1855 case IPV6_ADDR_LOOPBACK:
1859 printf(_("Unknown"));
1869 if (ipxtype == NULL)
1870 ipxtype = get_afntype(AF_IPX);
1872 if (ipxtype != NULL) {
1873 if (ptr->has_ipx_bb)
1874 printf(_(" IPX/Ethernet II addr:%s\n"),
1875 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1876 if (ptr->has_ipx_sn)
1877 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1878 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1879 if (ptr->has_ipx_e2)
1880 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1881 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1882 if (ptr->has_ipx_e3)
1883 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1884 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1889 if (ddptype == NULL)
1890 ddptype = get_afntype(AF_APPLETALK);
1891 if (ddptype != NULL) {
1893 printf(_(" EtherTalk Phase 2 addr:%s\n"),
1894 ddptype->sprint(&ptr->ddpaddr, 1));
1900 ectype = get_afntype(AF_ECONET);
1901 if (ectype != NULL) {
1902 if (ptr->has_econet)
1903 printf(_(" econet addr:%s\n"),
1904 ectype->sprint(&ptr->ecaddr, 1));
1909 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1910 if (ptr->flags == 0)
1911 printf(_("[NO FLAGS] "));
1912 if (ptr->flags & IFF_UP)
1914 if (ptr->flags & IFF_BROADCAST)
1915 printf(_("BROADCAST "));
1916 if (ptr->flags & IFF_DEBUG)
1917 printf(_("DEBUG "));
1918 if (ptr->flags & IFF_LOOPBACK)
1919 printf(_("LOOPBACK "));
1920 if (ptr->flags & IFF_POINTOPOINT)
1921 printf(_("POINTOPOINT "));
1922 if (ptr->flags & IFF_NOTRAILERS)
1923 printf(_("NOTRAILERS "));
1924 if (ptr->flags & IFF_RUNNING)
1925 printf(_("RUNNING "));
1926 if (ptr->flags & IFF_NOARP)
1927 printf(_("NOARP "));
1928 if (ptr->flags & IFF_PROMISC)
1929 printf(_("PROMISC "));
1930 if (ptr->flags & IFF_ALLMULTI)
1931 printf(_("ALLMULTI "));
1932 if (ptr->flags & IFF_SLAVE)
1933 printf(_("SLAVE "));
1934 if (ptr->flags & IFF_MASTER)
1935 printf(_("MASTER "));
1936 if (ptr->flags & IFF_MULTICAST)
1937 printf(_("MULTICAST "));
1939 if (ptr->flags & IFF_DYNAMIC)
1940 printf(_("DYNAMIC "));
1942 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1943 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1944 #ifdef SIOCSKEEPALIVE
1945 if (ptr->outfill || ptr->keepalive)
1946 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1950 /* If needed, display the interface statistics. */
1952 if (ptr->statistics_valid) {
1953 /* XXX: statistics are currently only printed for the primary address,
1954 * not for the aliases, although strictly speaking they're shared
1960 ("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1961 ptr->stats.rx_packets, ptr->stats.rx_errors,
1962 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1963 ptr->stats.rx_frame_errors);
1965 printf(_(" compressed:%lu\n"),
1966 ptr->stats.rx_compressed);
1969 ("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1970 ptr->stats.tx_packets, ptr->stats.tx_errors,
1971 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1972 ptr->stats.tx_carrier_errors);
1973 printf(_(" collisions:%lu "), ptr->stats.collisions);
1975 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1976 if (ptr->tx_queue_len != -1)
1977 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1979 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1980 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1984 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1985 ptr->map.base_addr)) {
1988 printf(_("Interrupt:%d "), ptr->map.irq);
1989 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
1991 printf(_("Base address:0x%lx "),
1992 (unsigned long) ptr->map.base_addr);
1993 if (ptr->map.mem_start) {
1994 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
1998 printf(_("DMA chan:%x "), ptr->map.dma);
2005 static int do_if_print(struct interface *ife, void *cookie)
2007 int *opt_a = (int *) cookie;
2010 res = do_if_fetch(ife);
2012 if ((ife->flags & IFF_UP) || *opt_a)
2018 static struct interface *lookup_interface(char *name)
2020 struct interface *ife = NULL;
2022 if (if_readlist_proc(name) < 0)
2024 ife = add_interface(name);
2028 /* for ipv4 add/del modes */
2029 static int if_print(char *ifname)
2034 res = for_all_interfaces(do_if_print, &interface_opt_a);
2036 struct interface *ife;
2038 ife = lookup_interface(ifname);
2039 res = do_if_fetch(ife);
2046 int display_interfaces(char *ifname)
2050 /* Create a channel to the NET kernel. */
2051 if ((skfd = sockets_open(0)) < 0) {
2052 bb_perror_msg_and_die("socket");
2055 /* Do we have to show the current setup? */
2056 status = if_print(ifname);