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