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