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