"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * stolen from net-tools-1.59 and stripped down for busybox by |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 4 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * |
| 6 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
| 7 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 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. |
| 11 | * |
| 12 | * ----------------------------------------------------------- |
| 13 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 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. |
| 17 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 18 | * |
| 19 | * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> |
| 20 | * and others. Copyright 1993 MicroWalt Corporation |
| 21 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 22 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 23 | * |
| 24 | * Patched to support 'add' and 'del' keywords for INET(4) addresses |
| 25 | * by Mrs. Brisby <mrs.brisby@nimh.org> |
| 26 | * |
| 27 | * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 28 | * - gettext instead of catgets for i18n |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 29 | * 10/1998 - Andi Kleen. Use interface list primitives. |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 30 | * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 31 | * (default AF was wrong) |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 32 | */ |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Denys Vlasenko | bb1dcc9 | 2010-02-02 12:45:38 +0100 | [diff] [blame] | 35 | #include "inet_common.h" |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 36 | #include <net/if.h> |
| 37 | #include <net/if_arp.h> |
| 38 | #ifdef HAVE_NET_ETHERNET_H |
| 39 | # include <net/ethernet.h> |
| 40 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 41 | |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 42 | #if ENABLE_FEATURE_HWIB |
| 43 | /* #include <linux/if_infiniband.h> */ |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 44 | # undef INFINIBAND_ALEN |
| 45 | # define INFINIBAND_ALEN 20 |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 48 | #if ENABLE_FEATURE_IPV6 |
Glenn L McGrath | b54a748 | 2003-08-29 11:34:08 +0000 | [diff] [blame] | 49 | # define HAVE_AFINET6 1 |
| 50 | #else |
| 51 | # undef HAVE_AFINET6 |
| 52 | #endif |
| 53 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 54 | #define _PATH_PROCNET_DEV "/proc/net/dev" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 55 | #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 57 | #ifdef HAVE_AFINET6 |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 58 | # ifndef _LINUX_IN6_H |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 59 | /* |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 60 | * This is from linux/include/net/ipv6.h |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 61 | */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 62 | struct in6_ifreq { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 63 | struct in6_addr ifr6_addr; |
Glenn L McGrath | b54a748 | 2003-08-29 11:34:08 +0000 | [diff] [blame] | 64 | uint32_t ifr6_prefixlen; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 65 | unsigned int ifr6_ifindex; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 66 | }; |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 67 | # endif |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 68 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 69 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 70 | /* Defines for glibc2.0 users. */ |
| 71 | #ifndef SIOCSIFTXQLEN |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 72 | # define SIOCSIFTXQLEN 0x8943 |
| 73 | # define SIOCGIFTXQLEN 0x8942 |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */ |
| 77 | #ifndef ifr_qlen |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 78 | # define ifr_qlen ifr_ifru.ifru_mtu |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | #ifndef HAVE_TXQUEUELEN |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 82 | # define HAVE_TXQUEUELEN 1 |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | #ifndef IFF_DYNAMIC |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 86 | # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 89 | /* Display an Internet socket address. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 90 | static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 91 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 92 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 93 | return "[NONE SET]"; |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 94 | return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 97 | #ifdef UNUSED_AND_BUGGY |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 98 | static int INET_getsock(char *bufp, struct sockaddr *sap) |
| 99 | { |
| 100 | char *sp = bufp, *bp; |
| 101 | unsigned int i; |
| 102 | unsigned val; |
| 103 | struct sockaddr_in *sock_in; |
| 104 | |
| 105 | sock_in = (struct sockaddr_in *) sap; |
| 106 | sock_in->sin_family = AF_INET; |
| 107 | sock_in->sin_port = 0; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 109 | val = 0; |
| 110 | bp = (char *) &val; |
| 111 | for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) { |
| 112 | *sp = toupper(*sp); |
| 113 | |
| 114 | if ((unsigned)(*sp - 'A') <= 5) |
| 115 | bp[i] |= (int) (*sp - ('A' - 10)); |
| 116 | else if (isdigit(*sp)) |
| 117 | bp[i] |= (int) (*sp - '0'); |
| 118 | else |
| 119 | return -1; |
| 120 | |
| 121 | bp[i] <<= 4; |
| 122 | sp++; |
| 123 | *sp = toupper(*sp); |
| 124 | |
| 125 | if ((unsigned)(*sp - 'A') <= 5) |
| 126 | bp[i] |= (int) (*sp - ('A' - 10)); |
| 127 | else if (isdigit(*sp)) |
| 128 | bp[i] |= (int) (*sp - '0'); |
| 129 | else |
| 130 | return -1; |
| 131 | |
| 132 | sp++; |
| 133 | } |
| 134 | sock_in->sin_addr.s_addr = htonl(val); |
| 135 | |
| 136 | return (sp - bufp); |
| 137 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 138 | #endif |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 140 | static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 141 | { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 142 | return INET_resolve(bufp, (struct sockaddr_in *) sap, 0); |
| 143 | /* |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 144 | switch (type) { |
| 145 | case 1: |
| 146 | return (INET_getsock(bufp, sap)); |
| 147 | case 256: |
| 148 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1)); |
| 149 | default: |
| 150 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0)); |
| 151 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 152 | */ |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 155 | static const struct aftype inet_aftype = { |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 156 | .name = "inet", |
| 157 | .title = "DARPA Internet", |
| 158 | .af = AF_INET, |
| 159 | .alen = 4, |
| 160 | .sprint = INET_sprint, |
| 161 | .input = INET_input, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 164 | #ifdef HAVE_AFINET6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 165 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 166 | /* Display an Internet socket address. */ |
| 167 | /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 168 | static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 169 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 170 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 171 | return "[NONE SET]"; |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 172 | return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric)); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 175 | #ifdef UNUSED |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 176 | static int INET6_getsock(char *bufp, struct sockaddr *sap) |
| 177 | { |
| 178 | struct sockaddr_in6 *sin6; |
| 179 | |
| 180 | sin6 = (struct sockaddr_in6 *) sap; |
| 181 | sin6->sin6_family = AF_INET6; |
| 182 | sin6->sin6_port = 0; |
| 183 | |
| 184 | if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0) |
| 185 | return -1; |
| 186 | |
| 187 | return 16; /* ?;) */ |
| 188 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 189 | #endif |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 191 | static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 192 | { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 193 | return INET6_resolve(bufp, (struct sockaddr_in6 *) sap); |
| 194 | /* |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 195 | switch (type) { |
| 196 | case 1: |
| 197 | return (INET6_getsock(bufp, sap)); |
| 198 | default: |
| 199 | return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap)); |
| 200 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 201 | */ |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 204 | static const struct aftype inet6_aftype = { |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 205 | .name = "inet6", |
| 206 | .title = "IPv6", |
| 207 | .af = AF_INET6, |
| 208 | .alen = sizeof(struct in6_addr), |
| 209 | .sprint = INET6_sprint, |
| 210 | .input = INET6_input, |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 213 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 214 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 215 | /* Display an UNSPEC address. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 216 | static char* FAST_FUNC UNSPEC_print(unsigned char *ptr) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 217 | { |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 218 | char *buff; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 219 | char *pos; |
| 220 | unsigned int i; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 221 | |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 222 | buff = auto_string(xmalloc(sizeof(struct sockaddr) * 3 + 1)); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 223 | pos = buff; |
| 224 | for (i = 0; i < sizeof(struct sockaddr); i++) { |
| 225 | /* careful -- not every libc's sprintf returns # bytes written */ |
Denys Vlasenko | b5a0396 | 2017-09-12 17:54:28 +0200 | [diff] [blame] | 226 | sprintf(pos, "%02X-", *ptr++); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 227 | pos += 3; |
| 228 | } |
| 229 | /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */ |
| 230 | *--pos = '\0'; |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 231 | return buff; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Display an UNSPEC socket address. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 235 | static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 236 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 237 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 238 | return "[NONE SET]"; |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 239 | return UNSPEC_print((unsigned char *)sap->sa_data); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 242 | static const struct aftype unspec_aftype = { |
| 243 | .name = "unspec", |
| 244 | .title = "UNSPEC", |
| 245 | .af = AF_UNSPEC, |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 246 | .alen = 0, |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 247 | .print = UNSPEC_print, |
| 248 | .sprint = UNSPEC_sprint, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 251 | static const struct aftype *const aftypes[] = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 252 | &inet_aftype, |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 253 | #ifdef HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 254 | &inet6_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 255 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 256 | &unspec_aftype, |
| 257 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 260 | /* Check our protocol family table for this family. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 261 | const struct aftype* FAST_FUNC get_aftype(const char *name) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 262 | { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 263 | const struct aftype *const *afp; |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 264 | |
| 265 | afp = aftypes; |
| 266 | while (*afp != NULL) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 267 | if (strcmp((*afp)->name, name) == 0) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 268 | return (*afp); |
| 269 | afp++; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | /* Check our protocol family table for this family. */ |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 275 | static const struct aftype *get_afntype(int af) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 276 | { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 277 | const struct aftype *const *afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 278 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 279 | afp = aftypes; |
| 280 | while (*afp != NULL) { |
| 281 | if ((*afp)->af == af) |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 282 | return *afp; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 283 | afp++; |
| 284 | } |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 285 | return NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 288 | struct user_net_device_stats { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 289 | unsigned long long rx_packets; /* total packets received */ |
| 290 | unsigned long long tx_packets; /* total packets transmitted */ |
| 291 | unsigned long long rx_bytes; /* total bytes received */ |
| 292 | unsigned long long tx_bytes; /* total bytes transmitted */ |
| 293 | unsigned long rx_errors; /* bad packets received */ |
| 294 | unsigned long tx_errors; /* packet transmit problems */ |
| 295 | unsigned long rx_dropped; /* no space in linux buffers */ |
| 296 | unsigned long tx_dropped; /* no space available in linux */ |
| 297 | unsigned long rx_multicast; /* multicast packets received */ |
| 298 | unsigned long rx_compressed; |
| 299 | unsigned long tx_compressed; |
| 300 | unsigned long collisions; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 301 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 302 | /* detailed rx_errors: */ |
| 303 | unsigned long rx_length_errors; |
| 304 | unsigned long rx_over_errors; /* receiver ring buff overflow */ |
| 305 | unsigned long rx_crc_errors; /* recved pkt with crc error */ |
| 306 | unsigned long rx_frame_errors; /* recv'd frame alignment error */ |
| 307 | unsigned long rx_fifo_errors; /* recv'r fifo overrun */ |
| 308 | unsigned long rx_missed_errors; /* receiver missed packet */ |
| 309 | /* detailed tx_errors */ |
| 310 | unsigned long tx_aborted_errors; |
| 311 | unsigned long tx_carrier_errors; |
| 312 | unsigned long tx_fifo_errors; |
| 313 | unsigned long tx_heartbeat_errors; |
| 314 | unsigned long tx_window_errors; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | struct interface { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 318 | struct interface *next, *prev; |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 319 | char name[IFNAMSIZ]; /* interface name */ |
| 320 | short type; /* if type */ |
| 321 | short flags; /* various flags */ |
| 322 | int metric; /* routing metric */ |
| 323 | int mtu; /* MTU value */ |
| 324 | int tx_queue_len; /* transmit queue length */ |
| 325 | struct ifmap map; /* hardware setup */ |
| 326 | struct sockaddr addr; /* IP address */ |
| 327 | struct sockaddr dstaddr; /* P-P IP address */ |
| 328 | struct sockaddr broadaddr; /* IP broadcast address */ |
| 329 | struct sockaddr netmask; /* IP network mask */ |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 330 | int has_ip; |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 331 | char hwaddr[32]; /* HW address */ |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 332 | int statistics_valid; |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 333 | struct user_net_device_stats stats; /* statistics */ |
| 334 | int keepalive; /* keepalive value for SLIP */ |
| 335 | int outfill; /* outfill value for SLIP */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 339 | smallint interface_opt_a; /* show all interfaces */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 340 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 341 | static struct interface *int_list, *int_last; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 342 | |
| 343 | |
Bernhard Reutner-Fischer | 57d83ff | 2006-12-07 12:25:17 +0000 | [diff] [blame] | 344 | #if 0 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 345 | /* like strcmp(), but knows about numbers */ |
Bernhard Reutner-Fischer | 57d83ff | 2006-12-07 12:25:17 +0000 | [diff] [blame] | 346 | except that the freshly added calls to xatoul() brf on ethernet aliases with |
| 347 | uClibc with e.g.: ife->name='lo' name='eth0:1' |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 348 | static int nstrcmp(const char *a, const char *b) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 349 | { |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 350 | const char *a_ptr = a; |
| 351 | const char *b_ptr = b; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 352 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 353 | while (*a == *b) { |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 354 | if (*a == '\0') { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 355 | return 0; |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 356 | } |
| 357 | if (!isdigit(*a) && isdigit(*(a+1))) { |
| 358 | a_ptr = a+1; |
| 359 | b_ptr = b+1; |
| 360 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 361 | a++; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 362 | b++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 363 | } |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 364 | |
| 365 | if (isdigit(*a) && isdigit(*b)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 366 | return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 367 | } |
| 368 | return *a - *b; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 369 | } |
Bernhard Reutner-Fischer | 57d83ff | 2006-12-07 12:25:17 +0000 | [diff] [blame] | 370 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 371 | |
| 372 | static struct interface *add_interface(char *name) |
| 373 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 374 | struct interface *ife, **nextp, *new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 375 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 376 | for (ife = int_last; ife; ife = ife->prev) { |
Bernhard Reutner-Fischer | 57d83ff | 2006-12-07 12:25:17 +0000 | [diff] [blame] | 377 | int n = /*n*/strcmp(ife->name, name); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 378 | |
| 379 | if (n == 0) |
| 380 | return ife; |
| 381 | if (n < 0) |
| 382 | break; |
| 383 | } |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 384 | |
| 385 | new = xzalloc(sizeof(*new)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 386 | strncpy_IFNAMSIZ(new->name, name); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 387 | nextp = ife ? &ife->next : &int_list; |
| 388 | new->prev = ife; |
| 389 | new->next = *nextp; |
| 390 | if (new->next) |
| 391 | new->next->prev = new; |
| 392 | else |
| 393 | int_last = new; |
| 394 | *nextp = new; |
| 395 | return new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 398 | static char *get_name(char *name, char *p) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 399 | { |
Rob Landley | 9fe801e | 2006-06-20 21:13:29 +0000 | [diff] [blame] | 400 | /* Extract <name> from nul-terminated p where p matches |
Denys Vlasenko | 6935ec9 | 2009-10-22 19:42:26 +0200 | [diff] [blame] | 401 | * <name>: after leading whitespace. |
| 402 | * If match is not made, set name empty and return unchanged p |
| 403 | */ |
| 404 | char *nameend; |
| 405 | char *namestart = skip_whitespace(p); |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 406 | |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 407 | nameend = namestart; |
Denys Vlasenko | 6935ec9 | 2009-10-22 19:42:26 +0200 | [diff] [blame] | 408 | while (*nameend && *nameend != ':' && !isspace(*nameend)) |
Eric Andersen | 9940e08 | 2004-08-12 16:52:00 +0000 | [diff] [blame] | 409 | nameend++; |
Denys Vlasenko | 6935ec9 | 2009-10-22 19:42:26 +0200 | [diff] [blame] | 410 | if (*nameend == ':') { |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 411 | if ((nameend - namestart) < IFNAMSIZ) { |
Denys Vlasenko | 6935ec9 | 2009-10-22 19:42:26 +0200 | [diff] [blame] | 412 | memcpy(name, namestart, nameend - namestart); |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 413 | name[nameend - namestart] = '\0'; |
Denys Vlasenko | 6935ec9 | 2009-10-22 19:42:26 +0200 | [diff] [blame] | 414 | p = nameend; |
Eric Andersen | 9940e08 | 2004-08-12 16:52:00 +0000 | [diff] [blame] | 415 | } else { |
| 416 | /* Interface name too large */ |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 417 | name[0] = '\0'; |
Eric Andersen | 9940e08 | 2004-08-12 16:52:00 +0000 | [diff] [blame] | 418 | } |
| 419 | } else { |
Rob Landley | 9fe801e | 2006-06-20 21:13:29 +0000 | [diff] [blame] | 420 | /* trailing ':' not found - return empty */ |
Denis Vlasenko | 11c23d7 | 2007-10-11 19:53:10 +0000 | [diff] [blame] | 421 | name[0] = '\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 422 | } |
Eric Andersen | 6fea732 | 2004-08-26 21:45:21 +0000 | [diff] [blame] | 423 | return p + 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 426 | /* If scanf supports size qualifiers for %n conversions, then we can |
| 427 | * use a modified fmt that simply stores the position in the fields |
| 428 | * having no associated fields in the proc string. Of course, we need |
| 429 | * to zero them again when we're done. But that is smaller than the |
| 430 | * old approach of multiple scanf occurrences with large numbers of |
| 431 | * args. */ |
| 432 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 433 | /* static const char *const ss_fmt[] = { */ |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 434 | /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */ |
| 435 | /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */ |
| 436 | /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */ |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 437 | /* }; */ |
| 438 | |
| 439 | /* Lie about the size of the int pointed to for %n. */ |
| 440 | #if INT_MAX == LONG_MAX |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 441 | static const char *const ss_fmt[] = { |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 442 | "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", |
| 443 | "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u", |
| 444 | "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u" |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 445 | }; |
| 446 | #else |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 447 | static const char *const ss_fmt[] = { |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 448 | "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu", |
| 449 | "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu", |
| 450 | "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | #endif |
| 454 | |
| 455 | static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 456 | { |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 457 | memset(&ife->stats, 0, sizeof(struct user_net_device_stats)); |
| 458 | |
| 459 | sscanf(bp, ss_fmt[procnetdev_vsn], |
| 460 | &ife->stats.rx_bytes, /* missing for 0 */ |
| 461 | &ife->stats.rx_packets, |
| 462 | &ife->stats.rx_errors, |
| 463 | &ife->stats.rx_dropped, |
| 464 | &ife->stats.rx_fifo_errors, |
| 465 | &ife->stats.rx_frame_errors, |
| 466 | &ife->stats.rx_compressed, /* missing for <= 1 */ |
| 467 | &ife->stats.rx_multicast, /* missing for <= 1 */ |
| 468 | &ife->stats.tx_bytes, /* missing for 0 */ |
| 469 | &ife->stats.tx_packets, |
| 470 | &ife->stats.tx_errors, |
| 471 | &ife->stats.tx_dropped, |
| 472 | &ife->stats.tx_fifo_errors, |
| 473 | &ife->stats.collisions, |
| 474 | &ife->stats.tx_carrier_errors, |
| 475 | &ife->stats.tx_compressed /* missing for <= 1 */ |
| 476 | ); |
| 477 | |
| 478 | if (procnetdev_vsn <= 1) { |
| 479 | if (procnetdev_vsn == 0) { |
| 480 | ife->stats.rx_bytes = 0; |
| 481 | ife->stats.tx_bytes = 0; |
| 482 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 483 | ife->stats.rx_multicast = 0; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 484 | ife->stats.rx_compressed = 0; |
| 485 | ife->stats.tx_compressed = 0; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 486 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 489 | static int procnetdev_version(char *buf) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 490 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 491 | if (strstr(buf, "compressed")) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 492 | return 2; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 493 | if (strstr(buf, "bytes")) |
| 494 | return 1; |
| 495 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 498 | static int if_readconf(void) |
| 499 | { |
| 500 | int numreqs = 30; |
| 501 | struct ifconf ifc; |
| 502 | struct ifreq *ifr; |
| 503 | int n, err = -1; |
| 504 | int skfd; |
| 505 | |
| 506 | ifc.ifc_buf = NULL; |
| 507 | |
| 508 | /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets |
| 509 | (as of 2.1.128) */ |
| 510 | skfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 511 | if (skfd < 0) { |
| 512 | bb_perror_msg("error: no inet socket available"); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | for (;;) { |
| 517 | ifc.ifc_len = sizeof(struct ifreq) * numreqs; |
| 518 | ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); |
| 519 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 520 | if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 521 | goto out; |
| 522 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 523 | if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 524 | /* assume it overflowed and try again */ |
| 525 | numreqs += 10; |
| 526 | continue; |
| 527 | } |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | ifr = ifc.ifc_req; |
| 532 | for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { |
| 533 | add_interface(ifr->ifr_name); |
| 534 | ifr++; |
| 535 | } |
| 536 | err = 0; |
| 537 | |
| 538 | out: |
| 539 | close(skfd); |
| 540 | free(ifc.ifc_buf); |
| 541 | return err; |
| 542 | } |
| 543 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 544 | static int if_readlist_proc(char *target) |
| 545 | { |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 546 | static smallint proc_read; |
| 547 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 548 | FILE *fh; |
| 549 | char buf[512]; |
| 550 | struct interface *ife; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 551 | int err, procnetdev_vsn; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 552 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 553 | if (proc_read) |
| 554 | return 0; |
| 555 | if (!target) |
| 556 | proc_read = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 557 | |
Denis Vlasenko | f90ab18 | 2008-03-20 21:19:35 +0000 | [diff] [blame] | 558 | fh = fopen_or_warn(_PATH_PROCNET_DEV, "r"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 559 | if (!fh) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 560 | return if_readconf(); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 561 | } |
| 562 | fgets(buf, sizeof buf, fh); /* eat line */ |
| 563 | fgets(buf, sizeof buf, fh); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 564 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 565 | procnetdev_vsn = procnetdev_version(buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 566 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 567 | err = 0; |
| 568 | while (fgets(buf, sizeof buf, fh)) { |
Eric Andersen | f96675b | 2003-07-28 06:37:04 +0000 | [diff] [blame] | 569 | char *s, name[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 570 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 571 | s = get_name(name, buf); |
| 572 | ife = add_interface(name); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 573 | get_dev_fields(s, ife, procnetdev_vsn); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 574 | ife->statistics_valid = 1; |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 575 | if (target && strcmp(target, name) == 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | if (ferror(fh)) { |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 579 | bb_perror_msg(_PATH_PROCNET_DEV); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 580 | err = -1; |
| 581 | proc_read = 0; |
| 582 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 583 | fclose(fh); |
| 584 | return err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 587 | static int if_readlist(void) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 588 | { |
Denis Vlasenko | d0587ed | 2007-03-22 19:35:51 +0000 | [diff] [blame] | 589 | int err = if_readlist_proc(NULL); |
| 590 | /* Needed in order to get ethN:M aliases */ |
| 591 | if (!err) |
| 592 | err = if_readconf(); |
| 593 | return err; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 596 | /* Fetch the interface configuration from the kernel. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 597 | static int if_fetch(struct interface *ife) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 598 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 599 | struct ifreq ifr; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 600 | char *ifname = ife->name; |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 601 | int skfd; |
| 602 | |
| 603 | skfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 604 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 605 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 606 | if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 607 | close(skfd); |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 608 | return -1; |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 609 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 610 | ife->flags = ifr.ifr_flags; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 611 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 612 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 613 | memset(ife->hwaddr, 0, 32); |
| 614 | if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 615 | memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 616 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 617 | ife->type = ifr.ifr_hwaddr.sa_family; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 618 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 619 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 620 | ife->metric = 0; |
| 621 | if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 622 | ife->metric = ifr.ifr_metric; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 623 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 624 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 625 | ife->mtu = 0; |
| 626 | if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 627 | ife->mtu = ifr.ifr_mtu; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 628 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 629 | memset(&ife->map, 0, sizeof(struct ifmap)); |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 630 | #ifdef SIOCGIFMAP |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 631 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 632 | if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 633 | ife->map = ifr.ifr_map; |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 634 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 635 | |
| 636 | #ifdef HAVE_TXQUEUELEN |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 637 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 638 | ife->tx_queue_len = -1; /* unknown value */ |
| 639 | if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 640 | ife->tx_queue_len = ifr.ifr_qlen; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 641 | #else |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 642 | ife->tx_queue_len = -1; /* unknown value */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 643 | #endif |
| 644 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 645 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 646 | ifr.ifr_addr.sa_family = AF_INET; |
| 647 | memset(&ife->addr, 0, sizeof(struct sockaddr)); |
| 648 | if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) { |
| 649 | ife->has_ip = 1; |
| 650 | ife->addr = ifr.ifr_addr; |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 651 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 652 | memset(&ife->dstaddr, 0, sizeof(struct sockaddr)); |
| 653 | if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0) |
| 654 | ife->dstaddr = ifr.ifr_dstaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 655 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 656 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 657 | memset(&ife->broadaddr, 0, sizeof(struct sockaddr)); |
| 658 | if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0) |
| 659 | ife->broadaddr = ifr.ifr_broadaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 660 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 661 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 662 | memset(&ife->netmask, 0, sizeof(struct sockaddr)); |
| 663 | if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0) |
| 664 | ife->netmask = ifr.ifr_netmask; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 665 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 666 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 667 | close(skfd); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 668 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 671 | static int do_if_fetch(struct interface *ife) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 672 | { |
| 673 | if (if_fetch(ife) < 0) { |
Denis Vlasenko | 322661d | 2007-01-29 23:43:52 +0000 | [diff] [blame] | 674 | const char *errmsg; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 675 | |
| 676 | if (errno == ENODEV) { |
| 677 | /* Give better error message for this case. */ |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 678 | errmsg = "Device not found"; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 679 | } else { |
| 680 | errmsg = strerror(errno); |
| 681 | } |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 682 | bb_error_msg("%s: error fetching interface information: %s", |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 683 | ife->name, errmsg); |
| 684 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 685 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 686 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 687 | } |
| 688 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 689 | static const struct hwtype unspec_hwtype = { |
"Robert P. J. Day" | 4137dd7 | 2006-06-26 21:54:57 +0000 | [diff] [blame] | 690 | .name = "unspec", |
| 691 | .title = "UNSPEC", |
| 692 | .type = -1, |
| 693 | .print = UNSPEC_print |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 694 | }; |
| 695 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 696 | static const struct hwtype loop_hwtype = { |
"Robert P. J. Day" | 4137dd7 | 2006-06-26 21:54:57 +0000 | [diff] [blame] | 697 | .name = "loop", |
| 698 | .title = "Local Loopback", |
| 699 | .type = ARPHRD_LOOPBACK |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 700 | }; |
| 701 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 702 | /* Display an Ethernet address in readable format. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 703 | static char* FAST_FUNC ether_print(unsigned char *ptr) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 704 | { |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 705 | char *buff; |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 706 | buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X", |
Denys Vlasenko | b5a0396 | 2017-09-12 17:54:28 +0200 | [diff] [blame] | 707 | ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5] |
| 708 | ); |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 709 | return auto_string(buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 712 | static const struct hwtype ether_hwtype = { |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 713 | .name = "ether", |
| 714 | .title = "Ethernet", |
| 715 | .type = ARPHRD_ETHER, |
| 716 | .alen = ETH_ALEN, |
| 717 | .print = ether_print, |
Bartosz Golaszewski | c19be75 | 2013-07-25 04:39:04 +0200 | [diff] [blame] | 718 | .input = in_ether |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 719 | }; |
| 720 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 721 | static const struct hwtype ppp_hwtype = { |
"Robert P. J. Day" | 4137dd7 | 2006-06-26 21:54:57 +0000 | [diff] [blame] | 722 | .name = "ppp", |
| 723 | .title = "Point-to-Point Protocol", |
| 724 | .type = ARPHRD_PPP |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 725 | }; |
| 726 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 727 | #if ENABLE_FEATURE_IPV6 |
"Robert P. J. Day" | 21302c2 | 2006-06-26 22:03:43 +0000 | [diff] [blame] | 728 | static const struct hwtype sit_hwtype = { |
| 729 | .name = "sit", |
| 730 | .title = "IPv6-in-IPv4", |
| 731 | .type = ARPHRD_SIT, |
| 732 | .print = UNSPEC_print, |
| 733 | .suppress_null_addr = 1 |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 734 | }; |
"Robert P. J. Day" | 21302c2 | 2006-06-26 22:03:43 +0000 | [diff] [blame] | 735 | #endif |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 736 | #if ENABLE_FEATURE_HWIB |
| 737 | static const struct hwtype ib_hwtype = { |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 738 | .name = "infiniband", |
| 739 | .title = "InfiniBand", |
| 740 | .type = ARPHRD_INFINIBAND, |
| 741 | .alen = INFINIBAND_ALEN, |
| 742 | .print = UNSPEC_print, |
| 743 | .input = in_ib, |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 744 | }; |
| 745 | #endif |
| 746 | |
"Robert P. J. Day" | 21302c2 | 2006-06-26 22:03:43 +0000 | [diff] [blame] | 747 | |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 748 | static const struct hwtype *const hwtypes[] = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 749 | &loop_hwtype, |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 750 | ðer_hwtype, |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 751 | &ppp_hwtype, |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 752 | &unspec_hwtype, |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 753 | #if ENABLE_FEATURE_IPV6 |
"Robert P. J. Day" | 21302c2 | 2006-06-26 22:03:43 +0000 | [diff] [blame] | 754 | &sit_hwtype, |
| 755 | #endif |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 756 | #if ENABLE_FEATURE_HWIB |
| 757 | &ib_hwtype, |
| 758 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 759 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 760 | }; |
| 761 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 762 | #ifdef IFF_PORTSEL |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 763 | static const char *const if_port_text[] = { |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 764 | /* Keep in step with <linux/netdevice.h> */ |
| 765 | "unknown", |
| 766 | "10base2", |
| 767 | "10baseT", |
| 768 | "AUI", |
| 769 | "100baseT", |
| 770 | "100baseTX", |
| 771 | "100baseFX", |
| 772 | NULL |
| 773 | }; |
| 774 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 775 | |
| 776 | /* Check our hardware type table for this type. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 777 | const struct hwtype* FAST_FUNC get_hwtype(const char *name) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 778 | { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 779 | const struct hwtype *const *hwp; |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 780 | |
| 781 | hwp = hwtypes; |
| 782 | while (*hwp != NULL) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 783 | if (strcmp((*hwp)->name, name) == 0) |
Denis Vlasenko | fa85b86 | 2007-01-07 01:24:12 +0000 | [diff] [blame] | 784 | return (*hwp); |
| 785 | hwp++; |
| 786 | } |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | /* Check our hardware type table for this type. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 791 | const struct hwtype* FAST_FUNC get_hwntype(int type) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 792 | { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 793 | const struct hwtype *const *hwp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 794 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 795 | hwp = hwtypes; |
| 796 | while (*hwp != NULL) { |
| 797 | if ((*hwp)->type == type) |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 798 | return *hwp; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 799 | hwp++; |
| 800 | } |
Denis Vlasenko | aad4999 | 2006-11-22 02:12:07 +0000 | [diff] [blame] | 801 | return NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* return 1 if address is all zeros */ |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 805 | static int hw_null_address(const struct hwtype *hw, void *ap) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 806 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 807 | int i; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 808 | unsigned char *address = (unsigned char *) ap; |
| 809 | |
| 810 | for (i = 0; i < hw->alen; i++) |
| 811 | if (address[i]) |
| 812 | return 0; |
| 813 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 816 | static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti"; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 817 | |
| 818 | static void print_bytes_scaled(unsigned long long ull, const char *end) |
| 819 | { |
| 820 | unsigned long long int_part; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 821 | const char *ext; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 822 | unsigned int frac_part; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 823 | int i; |
| 824 | |
| 825 | frac_part = 0; |
| 826 | ext = TRext; |
| 827 | int_part = ull; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 828 | i = 4; |
| 829 | do { |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 830 | if (int_part >= 1024) { |
| 831 | frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024; |
| 832 | int_part /= 1024; |
| 833 | ext += 3; /* KiB, MiB, GiB, TiB */ |
| 834 | } |
| 835 | --i; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 836 | } while (i); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 837 | |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 838 | printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 841 | |
| 842 | #ifdef HAVE_AFINET6 |
| 843 | #define IPV6_ADDR_ANY 0x0000U |
| 844 | |
| 845 | #define IPV6_ADDR_UNICAST 0x0001U |
| 846 | #define IPV6_ADDR_MULTICAST 0x0002U |
| 847 | #define IPV6_ADDR_ANYCAST 0x0004U |
| 848 | |
| 849 | #define IPV6_ADDR_LOOPBACK 0x0010U |
| 850 | #define IPV6_ADDR_LINKLOCAL 0x0020U |
| 851 | #define IPV6_ADDR_SITELOCAL 0x0040U |
| 852 | |
| 853 | #define IPV6_ADDR_COMPATv4 0x0080U |
| 854 | |
| 855 | #define IPV6_ADDR_SCOPE_MASK 0x00f0U |
| 856 | |
| 857 | #define IPV6_ADDR_MAPPED 0x1000U |
| 858 | #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */ |
| 859 | |
| 860 | |
| 861 | static void ife_print6(struct interface *ptr) |
| 862 | { |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 863 | FILE *f; |
Nicolas Dichtel | 6761d7d | 2013-08-12 17:09:59 +0200 | [diff] [blame] | 864 | char addr6[40], devname[21]; |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 865 | struct sockaddr_in6 sap; |
| 866 | int plen, scope, dad_status, if_idx; |
| 867 | char addr6p[8][5]; |
| 868 | |
| 869 | f = fopen_for_read(_PATH_PROCNET_IFINET6); |
| 870 | if (f == NULL) |
| 871 | return; |
| 872 | |
| 873 | while (fscanf |
| 874 | (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n", |
| 875 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], |
| 876 | addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope, |
| 877 | &dad_status, devname) != EOF |
| 878 | ) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 879 | if (strcmp(devname, ptr->name) == 0) { |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 880 | sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s", |
| 881 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
| 882 | addr6p[4], addr6p[5], addr6p[6], addr6p[7]); |
Felix Fietkau | f2c043a | 2016-01-18 12:07:35 +0100 | [diff] [blame] | 883 | memset(&sap, 0, sizeof(sap)); |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 884 | inet_pton(AF_INET6, addr6, |
| 885 | (struct sockaddr *) &sap.sin6_addr); |
| 886 | sap.sin6_family = AF_INET6; |
| 887 | printf(" inet6 addr: %s/%d", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 888 | INET6_sprint((struct sockaddr *) &sap, 1), |
| 889 | plen); |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 890 | printf(" Scope:"); |
| 891 | switch (scope & IPV6_ADDR_SCOPE_MASK) { |
| 892 | case 0: |
| 893 | puts("Global"); |
| 894 | break; |
| 895 | case IPV6_ADDR_LINKLOCAL: |
| 896 | puts("Link"); |
| 897 | break; |
| 898 | case IPV6_ADDR_SITELOCAL: |
| 899 | puts("Site"); |
| 900 | break; |
| 901 | case IPV6_ADDR_COMPATv4: |
| 902 | puts("Compat"); |
| 903 | break; |
| 904 | case IPV6_ADDR_LOOPBACK: |
| 905 | puts("Host"); |
| 906 | break; |
| 907 | default: |
| 908 | puts("Unknown"); |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | fclose(f); |
| 913 | } |
| 914 | #else |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 915 | #define ife_print6(a) ((void)0) |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 916 | #endif |
| 917 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 918 | static void ife_print(struct interface *ptr) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 919 | { |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 920 | const struct aftype *ap; |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 921 | const struct hwtype *hw; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 922 | int hf; |
| 923 | int can_compress = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 924 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 925 | ap = get_afntype(ptr->addr.sa_family); |
| 926 | if (ap == NULL) |
| 927 | ap = get_afntype(0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 928 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 929 | hf = ptr->type; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 930 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 931 | if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6) |
| 932 | can_compress = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 933 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 934 | hw = get_hwntype(hf); |
| 935 | if (hw == NULL) |
| 936 | hw = get_hwntype(-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 937 | |
Denys Vlasenko | a85b66e | 2010-05-23 23:04:15 +0200 | [diff] [blame] | 938 | printf("%-9s Link encap:%s ", ptr->name, hw->title); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 939 | /* For some hardware types (eg Ash, ATM) we don't print the |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 940 | hardware address if it's null. */ |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 941 | if (hw->print != NULL |
| 942 | && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr) |
| 943 | ) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 944 | printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr)); |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 945 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 946 | #ifdef IFF_PORTSEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 947 | if (ptr->flags & IFF_PORTSEL) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 948 | printf("Media:%s", if_port_text[ptr->map.port] /* [0] */); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 949 | if (ptr->flags & IFF_AUTOMEDIA) |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 950 | printf("(auto)"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 951 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 952 | #endif |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 953 | bb_putchar('\n'); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 954 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 955 | if (ptr->has_ip) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 956 | printf(" %s addr:%s ", ap->name, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 957 | ap->sprint(&ptr->addr, 1)); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 958 | if (ptr->flags & IFF_POINTOPOINT) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 959 | printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1)); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 960 | } |
| 961 | if (ptr->flags & IFF_BROADCAST) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 962 | printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1)); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 963 | } |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 964 | printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 965 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 966 | |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 967 | ife_print6(ptr); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 968 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 969 | printf(" "); |
| 970 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */ |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 971 | |
| 972 | if (ptr->flags == 0) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 973 | printf("[NO FLAGS] "); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 974 | } else { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 975 | static const char ife_print_flags_strs[] ALIGN1 = |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 976 | "UP\0" |
| 977 | "BROADCAST\0" |
| 978 | "DEBUG\0" |
| 979 | "LOOPBACK\0" |
| 980 | "POINTOPOINT\0" |
| 981 | "NOTRAILERS\0" |
| 982 | "RUNNING\0" |
| 983 | "NOARP\0" |
| 984 | "PROMISC\0" |
| 985 | "ALLMULTI\0" |
| 986 | "SLAVE\0" |
| 987 | "MASTER\0" |
| 988 | "MULTICAST\0" |
| 989 | #ifdef HAVE_DYNAMIC |
| 990 | "DYNAMIC\0" |
| 991 | #endif |
| 992 | ; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 993 | static const unsigned short ife_print_flags_mask[] ALIGN2 = { |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 994 | IFF_UP, |
| 995 | IFF_BROADCAST, |
| 996 | IFF_DEBUG, |
| 997 | IFF_LOOPBACK, |
| 998 | IFF_POINTOPOINT, |
| 999 | IFF_NOTRAILERS, |
| 1000 | IFF_RUNNING, |
| 1001 | IFF_NOARP, |
| 1002 | IFF_PROMISC, |
| 1003 | IFF_ALLMULTI, |
| 1004 | IFF_SLAVE, |
| 1005 | IFF_MASTER, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1006 | IFF_MULTICAST |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 1007 | #ifdef HAVE_DYNAMIC |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1008 | ,IFF_DYNAMIC |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 1009 | #endif |
| 1010 | }; |
| 1011 | const unsigned short *mask = ife_print_flags_mask; |
| 1012 | const char *str = ife_print_flags_strs; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1013 | do { |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 1014 | if (ptr->flags & *mask) { |
| 1015 | printf("%s ", str); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1016 | } |
Denis Vlasenko | 6b74b02 | 2007-07-29 14:17:48 +0000 | [diff] [blame] | 1017 | mask++; |
| 1018 | str += strlen(str) + 1; |
| 1019 | } while (*str); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1022 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */ |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1023 | printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1024 | #ifdef SIOCSKEEPALIVE |
| 1025 | if (ptr->outfill || ptr->keepalive) |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1026 | printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1027 | #endif |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1028 | bb_putchar('\n'); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* If needed, display the interface statistics. */ |
| 1031 | |
| 1032 | if (ptr->statistics_valid) { |
| 1033 | /* XXX: statistics are currently only printed for the primary address, |
| 1034 | * not for the aliases, although strictly speaking they're shared |
| 1035 | * by all addresses. |
| 1036 | */ |
| 1037 | printf(" "); |
| 1038 | |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1039 | printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1040 | ptr->stats.rx_packets, ptr->stats.rx_errors, |
| 1041 | ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors, |
| 1042 | ptr->stats.rx_frame_errors); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1043 | if (can_compress) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1044 | printf(" compressed:%lu\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1045 | ptr->stats.rx_compressed); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1046 | printf(" "); |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1047 | printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1048 | ptr->stats.tx_packets, ptr->stats.tx_errors, |
| 1049 | ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors, |
| 1050 | ptr->stats.tx_carrier_errors); |
Bernhard Reutner-Fischer | 214744d | 2006-03-30 13:38:19 +0000 | [diff] [blame] | 1051 | printf(" collisions:%lu ", ptr->stats.collisions); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1052 | if (can_compress) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1053 | printf("compressed:%lu ", ptr->stats.tx_compressed); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1054 | if (ptr->tx_queue_len != -1) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1055 | printf("txqueuelen:%d ", ptr->tx_queue_len); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1056 | printf("\n R"); |
| 1057 | print_bytes_scaled(ptr->stats.rx_bytes, " T"); |
| 1058 | print_bytes_scaled(ptr->stats.tx_bytes, "\n"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 1061 | if (ptr->map.irq || ptr->map.mem_start |
| 1062 | || ptr->map.dma || ptr->map.base_addr |
| 1063 | ) { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1064 | printf(" "); |
| 1065 | if (ptr->map.irq) |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1066 | printf("Interrupt:%d ", ptr->map.irq); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1067 | if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */ |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1068 | printf("Base address:0x%lx ", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1069 | (unsigned long) ptr->map.base_addr); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1070 | if (ptr->map.mem_start) { |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1071 | printf("Memory:%lx-%lx ", ptr->map.mem_start, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1072 | ptr->map.mem_end); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (ptr->map.dma) |
Rob Landley | 4e3aff3 | 2006-05-29 04:37:28 +0000 | [diff] [blame] | 1075 | printf("DMA chan:%x ", ptr->map.dma); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1076 | bb_putchar('\n'); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1077 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1078 | bb_putchar('\n'); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1081 | static int do_if_print(struct interface *ife) /*, int *opt_a)*/ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1082 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1083 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1084 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1085 | res = do_if_fetch(ife); |
| 1086 | if (res >= 0) { |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1087 | if ((ife->flags & IFF_UP) || interface_opt_a) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1088 | ife_print(ife); |
| 1089 | } |
| 1090 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1093 | static struct interface *lookup_interface(char *name) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1094 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1095 | struct interface *ife = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1096 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1097 | if (if_readlist_proc(name) < 0) |
| 1098 | return NULL; |
| 1099 | ife = add_interface(name); |
| 1100 | return ife; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1103 | #ifdef UNUSED |
| 1104 | static int for_all_interfaces(int (*doit) (struct interface *, void *), |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1105 | void *cookie) |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1106 | { |
| 1107 | struct interface *ife; |
| 1108 | |
| 1109 | if (!int_list && (if_readlist() < 0)) |
| 1110 | return -1; |
| 1111 | for (ife = int_list; ife; ife = ife->next) { |
| 1112 | int err = doit(ife, cookie); |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1113 | if (err) |
| 1114 | return err; |
| 1115 | } |
| 1116 | return 0; |
| 1117 | } |
| 1118 | #endif |
| 1119 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1120 | /* for ipv4 add/del modes */ |
| 1121 | static int if_print(char *ifname) |
| 1122 | { |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1123 | struct interface *ife; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1124 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1125 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1126 | if (!ifname) { |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1127 | /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/ |
| 1128 | if (!int_list && (if_readlist() < 0)) |
| 1129 | return -1; |
| 1130 | for (ife = int_list; ife; ife = ife->next) { |
| 1131 | int err = do_if_print(ife); /*, &interface_opt_a);*/ |
| 1132 | if (err) |
| 1133 | return err; |
| 1134 | } |
| 1135 | return 0; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1136 | } |
Denis Vlasenko | 1b16bda | 2007-06-19 11:10:02 +0000 | [diff] [blame] | 1137 | ife = lookup_interface(ifname); |
| 1138 | res = do_if_fetch(ife); |
| 1139 | if (res >= 0) |
| 1140 | ife_print(ife); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1141 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 1144 | #if ENABLE_FEATURE_HWIB |
| 1145 | /* Input an Infiniband address and convert to binary. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 1146 | int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap) |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 1147 | { |
Denis Vlasenko | 3b162ec | 2008-06-04 07:59:51 +0000 | [diff] [blame] | 1148 | sap->sa_family = ib_hwtype.type; |
Denys Vlasenko | 4836331 | 2010-04-04 15:29:32 +0200 | [diff] [blame] | 1149 | //TODO: error check? |
| 1150 | hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN); |
| 1151 | # ifdef HWIB_DEBUG |
| 1152 | fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data)); |
| 1153 | # endif |
Denis Vlasenko | 3b162ec | 2008-06-04 07:59:51 +0000 | [diff] [blame] | 1154 | return 0; |
Bernhard Reutner-Fischer | f3b778a | 2008-05-16 16:10:31 +0000 | [diff] [blame] | 1155 | } |
| 1156 | #endif |
| 1157 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 1158 | int FAST_FUNC display_interfaces(char *ifname) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1159 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1160 | int status; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1161 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1162 | status = if_print(ifname); |
Denis Vlasenko | 1cc7022 | 2007-03-15 19:46:43 +0000 | [diff] [blame] | 1163 | |
| 1164 | return (status < 0); /* status < 0 == 1 -- error */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1165 | } |