blob: 6b6c0944a6d13367684e61f996056ef140e73a24 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf15d4da2001-03-06 00:48:59 +00002/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 * 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 Andersenf15d4da2001-03-06 00:48:59 +000014 * 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 Andersenf15d4da2001-03-06 00:48:59 +000018 *
19 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
20 * and others. Copyright 1993 MicroWalt Corporation
21 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020022 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf15d4da2001-03-06 00:48:59 +000023 *
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 Andersenc7bda1c2004-03-15 08:29:22 +000029 * 10/1998 - Andi Kleen. Use interface list primitives.
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010030 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
Eric Andersenf15d4da2001-03-06 00:48:59 +000031 * (default AF was wrong)
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000032 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Denys Vlasenkobb1dcc92010-02-02 12:45:38 +010034#include "inet_common.h"
Dan Fandrichf533ec82011-06-10 05:17:59 +020035#include <net/if.h>
36#include <net/if_arp.h>
37#ifdef HAVE_NET_ETHERNET_H
38# include <net/ethernet.h>
39#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000040
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000041#if ENABLE_FEATURE_HWIB
42/* #include <linux/if_infiniband.h> */
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020043# undef INFINIBAND_ALEN
44# define INFINIBAND_ALEN 20
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000045#endif
46
Denis Vlasenko1cc70222007-03-15 19:46:43 +000047#if ENABLE_FEATURE_IPV6
Glenn L McGrathb54a7482003-08-29 11:34:08 +000048# define HAVE_AFINET6 1
49#else
50# undef HAVE_AFINET6
51#endif
52
Eric Andersenf15d4da2001-03-06 00:48:59 +000053#define _PATH_PROCNET_DEV "/proc/net/dev"
Eric Andersen51b8bd62002-07-03 11:46:38 +000054#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
Eric Andersenf15d4da2001-03-06 00:48:59 +000055
Denis Vlasenkoaad49992006-11-22 02:12:07 +000056#ifdef HAVE_AFINET6
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020057# ifndef _LINUX_IN6_H
Eric Andersenf15d4da2001-03-06 00:48:59 +000058/*
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020059 * This is from linux/include/net/ipv6.h
Eric Andersenf15d4da2001-03-06 00:48:59 +000060 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000061struct in6_ifreq {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000062 struct in6_addr ifr6_addr;
Glenn L McGrathb54a7482003-08-29 11:34:08 +000063 uint32_t ifr6_prefixlen;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000064 unsigned int ifr6_ifindex;
Eric Andersenf15d4da2001-03-06 00:48:59 +000065};
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020066# endif
Denis Vlasenkoaad49992006-11-22 02:12:07 +000067#endif /* HAVE_AFINET6 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000068
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000069/* Defines for glibc2.0 users. */
70#ifndef SIOCSIFTXQLEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020071# define SIOCSIFTXQLEN 0x8943
72# define SIOCGIFTXQLEN 0x8942
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000073#endif
74
75/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
76#ifndef ifr_qlen
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020077# define ifr_qlen ifr_ifru.ifru_mtu
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000078#endif
79
80#ifndef HAVE_TXQUEUELEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020081# define HAVE_TXQUEUELEN 1
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000082#endif
83
84#ifndef IFF_DYNAMIC
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020085# define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000086#endif
87
Eric Andersenf15d4da2001-03-06 00:48:59 +000088/* Display an Internet socket address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000089static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
Eric Andersenf15d4da2001-03-06 00:48:59 +000090{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000091 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000092 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +020093 return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00));
Eric Andersenf15d4da2001-03-06 00:48:59 +000094}
95
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000096#ifdef UNUSED_AND_BUGGY
Denis Vlasenkofa85b862007-01-07 01:24:12 +000097static 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 Vlasenkof7996f32007-01-11 17:20:00 +0000107
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000108 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 Vlasenko7f2527e2007-03-14 22:11:20 +0000137#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000138
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000139static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000140{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000141 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
142/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000143 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 Vlasenko7f2527e2007-03-14 22:11:20 +0000151*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000152}
153
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000154static const struct aftype inet_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000155 .name = "inet",
156 .title = "DARPA Internet",
157 .af = AF_INET,
158 .alen = 4,
159 .sprint = INET_sprint,
160 .input = INET_input,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000161};
162
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000163#ifdef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000164
Eric Andersen51b8bd62002-07-03 11:46:38 +0000165/* Display an Internet socket address. */
166/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000167static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000168{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000169 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000170 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200171 return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000172}
173
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000174#ifdef UNUSED
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000175static 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 Vlasenko7f2527e2007-03-14 22:11:20 +0000188#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000189
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000190static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000191{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000192 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
193/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000194 switch (type) {
195 case 1:
196 return (INET6_getsock(bufp, sap));
197 default:
198 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
199 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000200*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000201}
202
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000203static const struct aftype inet6_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000204 .name = "inet6",
205 .title = "IPv6",
206 .af = AF_INET6,
207 .alen = sizeof(struct in6_addr),
208 .sprint = INET6_sprint,
209 .input = INET6_input,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000210};
211
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000212#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000213
Eric Andersenf15d4da2001-03-06 00:48:59 +0000214/* Display an UNSPEC address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000215static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000216{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200217 char *buff;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000218 char *pos;
219 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000220
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200221 buff = auto_string(xmalloc(sizeof(struct sockaddr) * 3 + 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000222 pos = buff;
223 for (i = 0; i < sizeof(struct sockaddr); i++) {
224 /* careful -- not every libc's sprintf returns # bytes written */
Denys Vlasenkob5a03962017-09-12 17:54:28 +0200225 sprintf(pos, "%02X-", *ptr++);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000226 pos += 3;
227 }
228 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
229 *--pos = '\0';
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000230 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000231}
232
233/* Display an UNSPEC socket address. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000234static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000235{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000236 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000237 return "[NONE SET]";
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000238 return UNSPEC_print((unsigned char *)sap->sa_data);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000239}
240
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000241static const struct aftype unspec_aftype = {
242 .name = "unspec",
243 .title = "UNSPEC",
244 .af = AF_UNSPEC,
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000245 .alen = 0,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000246 .print = UNSPEC_print,
247 .sprint = UNSPEC_sprint,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000248};
249
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000250static const struct aftype *const aftypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000251 &inet_aftype,
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000252#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000253 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000254#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000255 &unspec_aftype,
256 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000257};
258
Eric Andersenf15d4da2001-03-06 00:48:59 +0000259/* Check our protocol family table for this family. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000260const struct aftype* FAST_FUNC get_aftype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000261{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000262 const struct aftype *const *afp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000263
264 afp = aftypes;
265 while (*afp != NULL) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100266 if (strcmp((*afp)->name, name) == 0)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000267 return (*afp);
268 afp++;
269 }
270 return NULL;
271}
272
273/* Check our protocol family table for this family. */
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000274static const struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000275{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000276 const struct aftype *const *afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000277
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000278 afp = aftypes;
279 while (*afp != NULL) {
280 if ((*afp)->af == af)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000281 return *afp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000282 afp++;
283 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000284 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000285}
286
Eric Andersenf15d4da2001-03-06 00:48:59 +0000287struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000288 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 Andersenf15d4da2001-03-06 00:48:59 +0000300
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000301 /* 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 Andersenf15d4da2001-03-06 00:48:59 +0000314};
315
316struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000317 struct interface *next, *prev;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000318 char name[IFNAMSIZ]; /* interface name */
319 short type; /* if type */
320 short flags; /* various flags */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100321 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 Vlasenko01eaee92008-04-21 02:21:45 +0000325 int metric; /* routing metric */
326 int mtu; /* MTU value */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000327 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 Vlasenko01eaee92008-04-21 02:21:45 +0000332 char hwaddr[32]; /* HW address */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100333#define LAST_TO_ZERO hwaddr
334
335 smallint has_ip;
336 smallint statistics_valid;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000337 struct user_net_device_stats stats; /* statistics */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100338#if 0 /* UNUSED */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000339 int keepalive; /* keepalive value for SLIP */
340 int outfill; /* outfill value for SLIP */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100341#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000342};
343
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100344struct iface_list {
345 struct interface *int_list, *int_last;
346};
Eric Andersenf15d4da2001-03-06 00:48:59 +0000347
348
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000349#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000350/* like strcmp(), but knows about numbers */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100351except that the freshly added calls to xatoul() barf on ethernet aliases with
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000352uClibc with e.g.: ife->name='lo' name='eth0:1'
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000353static int nstrcmp(const char *a, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000354{
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000355 const char *a_ptr = a;
356 const char *b_ptr = b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000357
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000358 while (*a == *b) {
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000359 if (*a == '\0') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000360 return 0;
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000361 }
362 if (!isdigit(*a) && isdigit(*(a+1))) {
363 a_ptr = a+1;
364 b_ptr = b+1;
365 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000366 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000367 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000368 }
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000369
370 if (isdigit(*a) && isdigit(*b)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000371 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000372 }
373 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000374}
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000375#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000376
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100377static struct interface *add_interface(struct iface_list *ilist, char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000378{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000379 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000380
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100381 for (ife = ilist->int_last; ife; ife = ife->prev) {
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000382 int n = /*n*/strcmp(ife->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000383
384 if (n == 0)
385 return ife;
386 if (n < 0)
387 break;
388 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000389
390 new = xzalloc(sizeof(*new));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000391 strncpy_IFNAMSIZ(new->name, name);
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100392
393 nextp = ife ? &ife->next : &ilist->int_list;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000394 new->prev = ife;
395 new->next = *nextp;
396 if (new->next)
397 new->next->prev = new;
398 else
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100399 ilist->int_last = new;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000400 *nextp = new;
401 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000402}
403
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100404static char *get_name(char name[IFNAMSIZ], char *p)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000405{
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100406 /* 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 Vlasenko6935ec92009-10-22 19:42:26 +0200408 */
409 char *nameend;
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100410 char *namestart;
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000411
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100412 nameend = namestart = skip_whitespace(p);
413
414 for (;;) {
415 if ((nameend - namestart) >= IFNAMSIZ)
416 break; /* interface name too large - return "" */
417 if (*nameend == ':') {
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200418 memcpy(name, namestart, nameend - namestart);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000419 name[nameend - namestart] = '\0';
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100420 return nameend + 1;
Eric Andersen9940e082004-08-12 16:52:00 +0000421 }
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100422 nameend++;
423 /* isspace, NUL, any control char? */
424 if ((unsigned char)*nameend <= (unsigned char)' ')
425 break; /* trailing ':' not found - return "" */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000426 }
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100427 name[0] = '\0';
428 return p;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000429}
430
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000431/* 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 Vlasenko1cc70222007-03-15 19:46:43 +0000438/* static const char *const ss_fmt[] = { */
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000439/* "%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 b1d5b742003-08-02 00:04:18 +0000442/* }; */
443
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100444/* 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 b1d5b742003-08-02 00:04:18 +0000448#if INT_MAX == LONG_MAX
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100449static const char *const ss_fmt[] ALIGN_PTR = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000450 "%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 b1d5b742003-08-02 00:04:18 +0000453};
454#else
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100455static const char *const ss_fmt[] ALIGN_PTR = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000456 "%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 b1d5b742003-08-02 00:04:18 +0000459};
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000460#endif
461
462static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000463{
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000464 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
465
466 sscanf(bp, ss_fmt[procnetdev_vsn],
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100467 &ife->stats.rx_bytes, /* missing for v0 */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000468 &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 Vlasenko8a5299f2018-03-05 16:56:16 +0100473 &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 b1d5b742003-08-02 00:04:18 +0000476 &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 Vlasenko8a5299f2018-03-05 16:56:16 +0100482 &ife->stats.tx_compressed /* missing for v0, v1 */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000483 );
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 McGrath1ed9dd92002-08-22 19:35:36 +0000490 ife->stats.rx_multicast = 0;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000491 ife->stats.rx_compressed = 0;
492 ife->stats.tx_compressed = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000493 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000494}
495
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000496static int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000497{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000498 if (strstr(buf, "compressed"))
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000499 return 2;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000500 if (strstr(buf, "bytes"))
501 return 1;
502 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000503}
504
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100505static void if_readconf(struct iface_list *ilist)
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000506{
507 int numreqs = 30;
508 struct ifconf ifc;
509 struct ifreq *ifr;
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100510 int n;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000511 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 Vlasenko8a5299f2018-03-05 16:56:16 +0100517 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000518
519 for (;;) {
520 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
521 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
522
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100523 xioctl(skfd, SIOCGIFCONF, &ifc);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000524 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000525 /* 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 Vlasenkod51ba0b2018-03-05 18:28:04 +0100534 add_interface(ilist, ifr->ifr_name);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000535 ifr++;
536 }
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000537
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000538 close(skfd);
539 free(ifc.ifc_buf);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000540}
541
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100542static int if_readlist_proc(struct iface_list *ilist, char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000543{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000544 FILE *fh;
545 char buf[512];
546 struct interface *ife;
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100547 int procnetdev_vsn;
548 int ret;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000549
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000550 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000551 if (!fh) {
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100552 return 0; /* "not found" */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000553 }
554 fgets(buf, sizeof buf, fh); /* eat line */
555 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000556
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000557 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000558
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100559 ret = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000560 while (fgets(buf, sizeof buf, fh)) {
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100561 char *s, name[IFNAMSIZ];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000562
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000563 s = get_name(name, buf);
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100564 ife = add_interface(ilist, name);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000565 get_dev_fields(s, ife, procnetdev_vsn);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000566 ife->statistics_valid = 1;
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100567 if (ifname && strcmp(ifname, name) == 0) {
568 ret = 1; /* found */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000569 break;
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100570 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000571 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000572 fclose(fh);
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100573 return ret;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000574}
575
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100576static void if_readlist(struct iface_list *ilist, char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000577{
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100578 int found = if_readlist_proc(ilist, ifname);
Denis Vlasenkod0587ed2007-03-22 19:35:51 +0000579 /* Needed in order to get ethN:M aliases */
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +0100580 if (!found)
581 if_readconf(ilist);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000582}
583
Eric Andersenf15d4da2001-03-06 00:48:59 +0000584/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000585static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000586{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000587 struct ifreq ifr;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000588 char *ifname = ife->name;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000589 int skfd;
590
591 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000592
Denis Vlasenko360d9662008-12-02 18:18:50 +0000593 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000594 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
595 close(skfd);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000596 return -1;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000597 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000598 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000599
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100600 /* 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 Vlasenko360d9662008-12-02 18:18:50 +0000608 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000609 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000610 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000611
Denys Vlasenko8a5299f2018-03-05 16:56:16 +0100612//er.... why this _isnt_ inside if()?
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000613 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000614
Denis Vlasenko360d9662008-12-02 18:18:50 +0000615 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000616 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000617 ife->metric = ifr.ifr_metric;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000618
Denis Vlasenko360d9662008-12-02 18:18:50 +0000619 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000620 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000621 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000622
Rob Landley93983042005-05-03 21:30:26 +0000623#ifdef SIOCGIFMAP
Denis Vlasenko360d9662008-12-02 18:18:50 +0000624 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Rob Landley93983042005-05-03 21:30:26 +0000625 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000626 ife->map = ifr.ifr_map;
Rob Landley93983042005-05-03 21:30:26 +0000627#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000628
629#ifdef HAVE_TXQUEUELEN
Denis Vlasenko360d9662008-12-02 18:18:50 +0000630 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000631 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000632 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000633#endif
634
Denis Vlasenko360d9662008-12-02 18:18:50 +0000635 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000636 ifr.ifr_addr.sa_family = AF_INET;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000637 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
638 ife->has_ip = 1;
639 ife->addr = ifr.ifr_addr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000640 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000641 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
642 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000643
Denis Vlasenko360d9662008-12-02 18:18:50 +0000644 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000645 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
646 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000647
Denis Vlasenko360d9662008-12-02 18:18:50 +0000648 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000649 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
650 ife->netmask = ifr.ifr_netmask;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000651 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000652
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000653 close(skfd);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000654 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000655}
656
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000657static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000658{
659 if (if_fetch(ife) < 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000660 const char *errmsg;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000661
662 if (errno == ENODEV) {
663 /* Give better error message for this case. */
Rob Landley4e3aff32006-05-29 04:37:28 +0000664 errmsg = "Device not found";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000665 } else {
666 errmsg = strerror(errno);
667 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000668 bb_error_msg("%s: error fetching interface information: %s",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000669 ife->name, errmsg);
670 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000671 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000672 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000673}
674
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000675static const struct hwtype unspec_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000676 .name = "unspec",
677 .title = "UNSPEC",
678 .type = -1,
679 .print = UNSPEC_print
Eric Andersenf15d4da2001-03-06 00:48:59 +0000680};
681
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000682static const struct hwtype loop_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000683 .name = "loop",
684 .title = "Local Loopback",
685 .type = ARPHRD_LOOPBACK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000686};
687
Eric Andersenf15d4da2001-03-06 00:48:59 +0000688/* Display an Ethernet address in readable format. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000689static char* FAST_FUNC ether_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000690{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200691 char *buff;
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000692 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
Denys Vlasenkob5a03962017-09-12 17:54:28 +0200693 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]
694 );
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200695 return auto_string(buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000696}
697
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000698static const struct hwtype ether_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000699 .name = "ether",
700 .title = "Ethernet",
701 .type = ARPHRD_ETHER,
702 .alen = ETH_ALEN,
703 .print = ether_print,
Bartosz Golaszewskic19be752013-07-25 04:39:04 +0200704 .input = in_ether
Eric Andersenf15d4da2001-03-06 00:48:59 +0000705};
706
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000707static const struct hwtype ppp_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000708 .name = "ppp",
709 .title = "Point-to-Point Protocol",
710 .type = ARPHRD_PPP
Eric Andersenf15d4da2001-03-06 00:48:59 +0000711};
712
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000713#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000714static 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 Vlasenkob71c6682007-07-21 15:08:09 +0000720};
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000721#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000722#if ENABLE_FEATURE_HWIB
723static const struct hwtype ib_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000724 .name = "infiniband",
725 .title = "InfiniBand",
726 .type = ARPHRD_INFINIBAND,
727 .alen = INFINIBAND_ALEN,
728 .print = UNSPEC_print,
729 .input = in_ib,
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000730};
731#endif
732
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000733
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100734static const struct hwtype *const hwtypes[] ALIGN_PTR = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000735 &loop_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000736 &ether_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000737 &ppp_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000738 &unspec_hwtype,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000739#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000740 &sit_hwtype,
741#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000742#if ENABLE_FEATURE_HWIB
743 &ib_hwtype,
744#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000745 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000746};
747
Eric Andersenf15d4da2001-03-06 00:48:59 +0000748#ifdef IFF_PORTSEL
Denys Vlasenko965b7952020-11-30 13:03:03 +0100749static const char *const if_port_text[] ALIGN_PTR = {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000750 /* 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 Andersenf15d4da2001-03-06 00:48:59 +0000761
762/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000763const struct hwtype* FAST_FUNC get_hwtype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000764{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000765 const struct hwtype *const *hwp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000766
767 hwp = hwtypes;
768 while (*hwp != NULL) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100769 if (strcmp((*hwp)->name, name) == 0)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000770 return (*hwp);
771 hwp++;
772 }
773 return NULL;
774}
775
776/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000777const struct hwtype* FAST_FUNC get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000778{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000779 const struct hwtype *const *hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000780
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000781 hwp = hwtypes;
782 while (*hwp != NULL) {
783 if ((*hwp)->type == type)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000784 return *hwp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000785 hwp++;
786 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000787 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000788}
789
790/* return 1 if address is all zeros */
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000791static int hw_null_address(const struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000792{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000793 int i;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000794 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 Andersenf15d4da2001-03-06 00:48:59 +0000800}
801
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000802static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000803
804static void print_bytes_scaled(unsigned long long ull, const char *end)
805{
806 unsigned long long int_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000807 const char *ext;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000808 unsigned int frac_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000809 int i;
810
811 frac_part = 0;
812 ext = TRext;
813 int_part = ull;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000814 i = 4;
815 do {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000816 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 b1d5b742003-08-02 00:04:18 +0000822 } while (i);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000823
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000824 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000825}
826
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000827
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
847static void ife_print6(struct interface *ptr)
848{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000849 FILE *f;
Nicolas Dichtel6761d7d2013-08-12 17:09:59 +0200850 char addr6[40], devname[21];
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000851 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 Vlasenko1d3a04a2016-11-28 01:22:57 +0100865 if (strcmp(devname, ptr->name) == 0) {
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000866 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 Fietkauf2c043a2016-01-18 12:07:35 +0100869 memset(&sap, 0, sizeof(sap));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000870 inet_pton(AF_INET6, addr6,
871 (struct sockaddr *) &sap.sin6_addr);
872 sap.sin6_family = AF_INET6;
873 printf(" inet6 addr: %s/%d",
Denys Vlasenko69675782013-01-14 01:34:48 +0100874 INET6_sprint((struct sockaddr *) &sap, 1),
875 plen);
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000876 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 Vlasenko00d84172008-11-24 07:34:42 +0000901#define ife_print6(a) ((void)0)
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000902#endif
903
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000904static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000905{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000906 const struct aftype *ap;
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000907 const struct hwtype *hw;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000908 int hf;
909 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000910
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000911 ap = get_afntype(ptr->addr.sa_family);
912 if (ap == NULL)
913 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000914
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000915 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000916
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000917 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
918 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000919
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000920 hw = get_hwntype(hf);
921 if (hw == NULL)
922 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000923
Denys Vlasenkoa85b66e2010-05-23 23:04:15 +0200924 printf("%-9s Link encap:%s ", ptr->name, hw->title);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000925 /* For some hardware types (eg Ash, ATM) we don't print the
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000926 hardware address if it's null. */
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000927 if (hw->print != NULL
928 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
929 ) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000930 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000931 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000932#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000933 if (ptr->flags & IFF_PORTSEL) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000934 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000935 if (ptr->flags & IFF_AUTOMEDIA)
Rob Landley4e3aff32006-05-29 04:37:28 +0000936 printf("(auto)");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000937 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000938#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000939 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000940
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000941 if (ptr->has_ip) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000942 printf(" %s addr:%s ", ap->name,
Denys Vlasenko69675782013-01-14 01:34:48 +0100943 ap->sprint(&ptr->addr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000944 if (ptr->flags & IFF_POINTOPOINT) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000945 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000946 }
947 if (ptr->flags & IFF_BROADCAST) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000948 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000949 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000950 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000951 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000952
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000953 ife_print6(ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000954
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000955 printf(" ");
956 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000957
958 if (ptr->flags == 0) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000959 printf("[NO FLAGS] ");
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000960 } else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000961 static const char ife_print_flags_strs[] ALIGN1 =
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000962 "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 Vlasenko6ca409e2007-08-12 20:58:27 +0000979 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000980 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 Vlasenko6ca409e2007-08-12 20:58:27 +0000992 IFF_MULTICAST
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000993#ifdef HAVE_DYNAMIC
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000994 ,IFF_DYNAMIC
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000995#endif
996 };
997 const unsigned short *mask = ife_print_flags_mask;
998 const char *str = ife_print_flags_strs;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000999 do {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001000 if (ptr->flags & *mask) {
1001 printf("%s ", str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001002 }
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001003 mask++;
1004 str += strlen(str) + 1;
1005 } while (*str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001006 }
1007
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001008 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
Rob Landley4e3aff32006-05-29 04:37:28 +00001009 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
Denys Vlasenko8a5299f2018-03-05 16:56:16 +01001010#if 0
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001011#ifdef SIOCSKEEPALIVE
1012 if (ptr->outfill || ptr->keepalive)
Rob Landley4e3aff32006-05-29 04:37:28 +00001013 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001014#endif
Denys Vlasenko8a5299f2018-03-05 16:56:16 +01001015#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001016 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001017
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-Fischerd409c3a2006-03-29 22:34:47 +00001027 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001028 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 McGrath1ed9dd92002-08-22 19:35:36 +00001031 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001032 printf(" compressed:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001033 ptr->stats.rx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001034 printf(" ");
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001035 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001036 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-Fischer214744d2006-03-30 13:38:19 +00001039 printf(" collisions:%lu ", ptr->stats.collisions);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001040 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001041 printf("compressed:%lu ", ptr->stats.tx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001042 if (ptr->tx_queue_len != -1)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001043 printf("txqueuelen:%d ", ptr->tx_queue_len);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001044 printf("\n R");
1045 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1046 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001047 }
1048
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001049 if (ptr->map.irq || ptr->map.mem_start
1050 || ptr->map.dma || ptr->map.base_addr
1051 ) {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001052 printf(" ");
1053 if (ptr->map.irq)
Rob Landley4e3aff32006-05-29 04:37:28 +00001054 printf("Interrupt:%d ", ptr->map.irq);
Denys Vlasenko69675782013-01-14 01:34:48 +01001055 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */
Rob Landley4e3aff32006-05-29 04:37:28 +00001056 printf("Base address:0x%lx ",
Denys Vlasenko69675782013-01-14 01:34:48 +01001057 (unsigned long) ptr->map.base_addr);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001058 if (ptr->map.mem_start) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001059 printf("Memory:%lx-%lx ", ptr->map.mem_start,
Denys Vlasenko69675782013-01-14 01:34:48 +01001060 ptr->map.mem_end);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001061 }
1062 if (ptr->map.dma)
Rob Landley4e3aff32006-05-29 04:37:28 +00001063 printf("DMA chan:%x ", ptr->map.dma);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001064 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001065 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001066 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001067}
1068
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001069static int do_if_print(struct interface *ife, int show_downed_too)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001070{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001071 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001072
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001073 res = do_if_fetch(ife);
1074 if (res >= 0) {
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001075 if ((ife->flags & IFF_UP) || show_downed_too)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001076 ife_print(ife);
1077 }
1078 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001079}
1080
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001081int FAST_FUNC display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001082{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001083 struct interface *ife;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001084 int res;
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +01001085 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 Andersenf15d4da2001-03-06 00:48:59 +00001090
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001091 if (!ifname || ifname == IFNAME_SHOW_DOWNED_TOO) {
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +01001092 for (ife = ilist.int_list; ife; ife = ife->next) {
1093
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001094 BUILD_BUG_ON((int)(intptr_t)IFNAME_SHOW_DOWNED_TOO != 1);
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +01001095
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001096 res = do_if_print(ife, (int)(intptr_t)ifname);
1097 if (res < 0)
1098 goto ret;
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001099 }
1100 return 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001101 }
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001102
Denys Vlasenkod51ba0b2018-03-05 18:28:04 +01001103 ife = add_interface(&ilist, ifname);
1104 res = do_if_print(ife, /*show_downed_too:*/ 1);
Denys Vlasenko82ec8942018-03-05 17:46:17 +01001105 ret:
1106 return (res < 0); /* status < 0 == 1 -- error */
Denys Vlasenko8a5299f2018-03-05 16:56:16 +01001107}
1108
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001109#if ENABLE_FEATURE_HWIB
1110/* Input an Infiniband address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001111int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001112{
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001113 sap->sa_family = ib_hwtype.type;
Denys Vlasenko48363312010-04-04 15:29:32 +02001114//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 Vlasenko3b162ec2008-06-04 07:59:51 +00001119 return 0;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001120}
1121#endif