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