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