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