blob: b59a61de4d4903aa757c1582b73ec4d241aa95a2 [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 *
Rob Landleye84f4342006-06-03 21:23:20 +000022 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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.
30 * 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 */
33
Eric Andersencd8c4362001-11-10 11:22:46 +000034#include <net/if.h>
35#include <net/if_arp.h>
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020036#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
37# include <net/ethernet.h>
38#else
39# include <linux/if_ether.h>
40#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Denys Vlasenkobb1dcc92010-02-02 12:45:38 +010042#include "inet_common.h"
Eric Andersenf15d4da2001-03-06 00:48:59 +000043
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000044#if ENABLE_FEATURE_HWIB
45/* #include <linux/if_infiniband.h> */
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020046# undef INFINIBAND_ALEN
47# define INFINIBAND_ALEN 20
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000048#endif
49
Denis Vlasenko1cc70222007-03-15 19:46:43 +000050#if ENABLE_FEATURE_IPV6
Glenn L McGrathb54a7482003-08-29 11:34:08 +000051# define HAVE_AFINET6 1
52#else
53# undef HAVE_AFINET6
54#endif
55
Eric Andersenf15d4da2001-03-06 00:48:59 +000056#define _PATH_PROCNET_DEV "/proc/net/dev"
Eric Andersen51b8bd62002-07-03 11:46:38 +000057#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
Eric Andersenf15d4da2001-03-06 00:48:59 +000058
Denis Vlasenkoaad49992006-11-22 02:12:07 +000059#ifdef HAVE_AFINET6
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020060# ifndef _LINUX_IN6_H
Eric Andersenf15d4da2001-03-06 00:48:59 +000061/*
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020062 * This is from linux/include/net/ipv6.h
Eric Andersenf15d4da2001-03-06 00:48:59 +000063 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000064struct in6_ifreq {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000065 struct in6_addr ifr6_addr;
Glenn L McGrathb54a7482003-08-29 11:34:08 +000066 uint32_t ifr6_prefixlen;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000067 unsigned int ifr6_ifindex;
Eric Andersenf15d4da2001-03-06 00:48:59 +000068};
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020069# endif
Denis Vlasenkoaad49992006-11-22 02:12:07 +000070#endif /* HAVE_AFINET6 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000071
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000072/* Defines for glibc2.0 users. */
73#ifndef SIOCSIFTXQLEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020074# define SIOCSIFTXQLEN 0x8943
75# define SIOCGIFTXQLEN 0x8942
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000076#endif
77
78/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
79#ifndef ifr_qlen
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020080# define ifr_qlen ifr_ifru.ifru_mtu
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000081#endif
82
83#ifndef HAVE_TXQUEUELEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020084# define HAVE_TXQUEUELEN 1
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000085#endif
86
87#ifndef IFF_DYNAMIC
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020088# define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000089#endif
90
Eric Andersenf15d4da2001-03-06 00:48:59 +000091/* Display an Internet socket address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000092static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
Eric Andersenf15d4da2001-03-06 00:48:59 +000093{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +000094 static char *buff; /* defaults to NULL */
Eric Andersenf15d4da2001-03-06 00:48:59 +000095
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000096 free(buff);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000097 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000098 return "[NONE SET]";
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000099 buff = INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000100 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000101}
102
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000103#ifdef UNUSED_AND_BUGGY
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000104static int INET_getsock(char *bufp, struct sockaddr *sap)
105{
106 char *sp = bufp, *bp;
107 unsigned int i;
108 unsigned val;
109 struct sockaddr_in *sock_in;
110
111 sock_in = (struct sockaddr_in *) sap;
112 sock_in->sin_family = AF_INET;
113 sock_in->sin_port = 0;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000114
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000115 val = 0;
116 bp = (char *) &val;
117 for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
118 *sp = toupper(*sp);
119
120 if ((unsigned)(*sp - 'A') <= 5)
121 bp[i] |= (int) (*sp - ('A' - 10));
122 else if (isdigit(*sp))
123 bp[i] |= (int) (*sp - '0');
124 else
125 return -1;
126
127 bp[i] <<= 4;
128 sp++;
129 *sp = toupper(*sp);
130
131 if ((unsigned)(*sp - 'A') <= 5)
132 bp[i] |= (int) (*sp - ('A' - 10));
133 else if (isdigit(*sp))
134 bp[i] |= (int) (*sp - '0');
135 else
136 return -1;
137
138 sp++;
139 }
140 sock_in->sin_addr.s_addr = htonl(val);
141
142 return (sp - bufp);
143}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000144#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000145
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000146static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000147{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000148 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
149/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000150 switch (type) {
151 case 1:
152 return (INET_getsock(bufp, sap));
153 case 256:
154 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
155 default:
156 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
157 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000158*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000159}
160
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000161static const struct aftype inet_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000162 .name = "inet",
163 .title = "DARPA Internet",
164 .af = AF_INET,
165 .alen = 4,
166 .sprint = INET_sprint,
167 .input = INET_input,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000168};
169
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000170#ifdef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000171
Eric Andersen51b8bd62002-07-03 11:46:38 +0000172/* Display an Internet socket address. */
173/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000174static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000175{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000176 static char *buff;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000177
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000178 free(buff);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000179 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000180 return "[NONE SET]";
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000181 buff = INET6_rresolve((struct sockaddr_in6 *) sap, numeric);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000182 return buff;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000183}
184
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000185#ifdef UNUSED
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000186static int INET6_getsock(char *bufp, struct sockaddr *sap)
187{
188 struct sockaddr_in6 *sin6;
189
190 sin6 = (struct sockaddr_in6 *) sap;
191 sin6->sin6_family = AF_INET6;
192 sin6->sin6_port = 0;
193
194 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
195 return -1;
196
197 return 16; /* ?;) */
198}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000199#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000200
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000201static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000202{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000203 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
204/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000205 switch (type) {
206 case 1:
207 return (INET6_getsock(bufp, sap));
208 default:
209 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
210 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000211*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000212}
213
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000214static const struct aftype inet6_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000215 .name = "inet6",
216 .title = "IPv6",
217 .af = AF_INET6,
218 .alen = sizeof(struct in6_addr),
219 .sprint = INET6_sprint,
220 .input = INET6_input,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000221};
222
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000223#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000224
Eric Andersenf15d4da2001-03-06 00:48:59 +0000225/* Display an UNSPEC address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000226static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000227{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000228 static char *buff;
229
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000230 char *pos;
231 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000232
Denis Vlasenko43d5d422008-05-15 19:44:46 +0000233 if (!buff)
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000234 buff = xmalloc(sizeof(struct sockaddr) * 3 + 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000235 pos = buff;
236 for (i = 0; i < sizeof(struct sockaddr); i++) {
237 /* careful -- not every libc's sprintf returns # bytes written */
238 sprintf(pos, "%02X-", (*ptr++ & 0377));
239 pos += 3;
240 }
241 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
242 *--pos = '\0';
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000243 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000244}
245
246/* Display an UNSPEC socket address. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000247static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000248{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000249 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000250 return "[NONE SET]";
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000251 return UNSPEC_print((unsigned char *)sap->sa_data);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000252}
253
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000254static const struct aftype unspec_aftype = {
255 .name = "unspec",
256 .title = "UNSPEC",
257 .af = AF_UNSPEC,
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000258 .alen = 0,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000259 .print = UNSPEC_print,
260 .sprint = UNSPEC_sprint,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000261};
262
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000263static const struct aftype *const aftypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000264 &inet_aftype,
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000265#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000266 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000267#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000268 &unspec_aftype,
269 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000270};
271
Eric Andersenf15d4da2001-03-06 00:48:59 +0000272/* Check our protocol family table for this family. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000273const struct aftype* FAST_FUNC get_aftype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000274{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000275 const struct aftype *const *afp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000276
277 afp = aftypes;
278 while (*afp != NULL) {
279 if (!strcmp((*afp)->name, name))
280 return (*afp);
281 afp++;
282 }
283 return NULL;
284}
285
286/* Check our protocol family table for this family. */
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000287static const struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000288{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000289 const struct aftype *const *afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000290
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000291 afp = aftypes;
292 while (*afp != NULL) {
293 if ((*afp)->af == af)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000294 return *afp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000295 afp++;
296 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000297 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000298}
299
Eric Andersenf15d4da2001-03-06 00:48:59 +0000300struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000301 unsigned long long rx_packets; /* total packets received */
302 unsigned long long tx_packets; /* total packets transmitted */
303 unsigned long long rx_bytes; /* total bytes received */
304 unsigned long long tx_bytes; /* total bytes transmitted */
305 unsigned long rx_errors; /* bad packets received */
306 unsigned long tx_errors; /* packet transmit problems */
307 unsigned long rx_dropped; /* no space in linux buffers */
308 unsigned long tx_dropped; /* no space available in linux */
309 unsigned long rx_multicast; /* multicast packets received */
310 unsigned long rx_compressed;
311 unsigned long tx_compressed;
312 unsigned long collisions;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000313
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000314 /* detailed rx_errors: */
315 unsigned long rx_length_errors;
316 unsigned long rx_over_errors; /* receiver ring buff overflow */
317 unsigned long rx_crc_errors; /* recved pkt with crc error */
318 unsigned long rx_frame_errors; /* recv'd frame alignment error */
319 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
320 unsigned long rx_missed_errors; /* receiver missed packet */
321 /* detailed tx_errors */
322 unsigned long tx_aborted_errors;
323 unsigned long tx_carrier_errors;
324 unsigned long tx_fifo_errors;
325 unsigned long tx_heartbeat_errors;
326 unsigned long tx_window_errors;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000327};
328
329struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000330 struct interface *next, *prev;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000331 char name[IFNAMSIZ]; /* interface name */
332 short type; /* if type */
333 short flags; /* various flags */
334 int metric; /* routing metric */
335 int mtu; /* MTU value */
336 int tx_queue_len; /* transmit queue length */
337 struct ifmap map; /* hardware setup */
338 struct sockaddr addr; /* IP address */
339 struct sockaddr dstaddr; /* P-P IP address */
340 struct sockaddr broadaddr; /* IP broadcast address */
341 struct sockaddr netmask; /* IP network mask */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000342 int has_ip;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000343 char hwaddr[32]; /* HW address */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000344 int statistics_valid;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000345 struct user_net_device_stats stats; /* statistics */
346 int keepalive; /* keepalive value for SLIP */
347 int outfill; /* outfill value for SLIP */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000348};
349
350
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000351smallint interface_opt_a; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000352
Eric Andersenf15d4da2001-03-06 00:48:59 +0000353static struct interface *int_list, *int_last;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000354
355
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000356#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000357/* like strcmp(), but knows about numbers */
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000358except that the freshly added calls to xatoul() brf on ethernet aliases with
359uClibc with e.g.: ife->name='lo' name='eth0:1'
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000360static int nstrcmp(const char *a, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000361{
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000362 const char *a_ptr = a;
363 const char *b_ptr = b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000364
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000365 while (*a == *b) {
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000366 if (*a == '\0') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000367 return 0;
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000368 }
369 if (!isdigit(*a) && isdigit(*(a+1))) {
370 a_ptr = a+1;
371 b_ptr = b+1;
372 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000373 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000374 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000375 }
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000376
377 if (isdigit(*a) && isdigit(*b)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000378 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000379 }
380 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000381}
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000382#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000383
384static struct interface *add_interface(char *name)
385{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000386 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000387
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000388 for (ife = int_last; ife; ife = ife->prev) {
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000389 int n = /*n*/strcmp(ife->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000390
391 if (n == 0)
392 return ife;
393 if (n < 0)
394 break;
395 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000396
397 new = xzalloc(sizeof(*new));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000398 strncpy_IFNAMSIZ(new->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000399 nextp = ife ? &ife->next : &int_list;
400 new->prev = ife;
401 new->next = *nextp;
402 if (new->next)
403 new->next->prev = new;
404 else
405 int_last = new;
406 *nextp = new;
407 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000408}
409
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000410static char *get_name(char *name, char *p)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000411{
Rob Landley9fe801e2006-06-20 21:13:29 +0000412 /* Extract <name> from nul-terminated p where p matches
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200413 * <name>: after leading whitespace.
414 * If match is not made, set name empty and return unchanged p
415 */
416 char *nameend;
417 char *namestart = skip_whitespace(p);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000418
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000419 nameend = namestart;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200420 while (*nameend && *nameend != ':' && !isspace(*nameend))
Eric Andersen9940e082004-08-12 16:52:00 +0000421 nameend++;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200422 if (*nameend == ':') {
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000423 if ((nameend - namestart) < IFNAMSIZ) {
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200424 memcpy(name, namestart, nameend - namestart);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000425 name[nameend - namestart] = '\0';
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200426 p = nameend;
Eric Andersen9940e082004-08-12 16:52:00 +0000427 } else {
428 /* Interface name too large */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000429 name[0] = '\0';
Eric Andersen9940e082004-08-12 16:52:00 +0000430 }
431 } else {
Rob Landley9fe801e2006-06-20 21:13:29 +0000432 /* trailing ':' not found - return empty */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000433 name[0] = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000434 }
Eric Andersen6fea7322004-08-26 21:45:21 +0000435 return p + 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000436}
437
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000438/* If scanf supports size qualifiers for %n conversions, then we can
439 * use a modified fmt that simply stores the position in the fields
440 * having no associated fields in the proc string. Of course, we need
441 * to zero them again when we're done. But that is smaller than the
442 * old approach of multiple scanf occurrences with large numbers of
443 * args. */
444
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000445/* static const char *const ss_fmt[] = { */
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000446/* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
447/* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
448/* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000449/* }; */
450
451 /* Lie about the size of the int pointed to for %n. */
452#if INT_MAX == LONG_MAX
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000453static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000454 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
455 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
456 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000457};
458#else
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000459static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000460 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
461 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
462 "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000463};
464
465#endif
466
467static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000468{
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000469 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
470
471 sscanf(bp, ss_fmt[procnetdev_vsn],
472 &ife->stats.rx_bytes, /* missing for 0 */
473 &ife->stats.rx_packets,
474 &ife->stats.rx_errors,
475 &ife->stats.rx_dropped,
476 &ife->stats.rx_fifo_errors,
477 &ife->stats.rx_frame_errors,
478 &ife->stats.rx_compressed, /* missing for <= 1 */
479 &ife->stats.rx_multicast, /* missing for <= 1 */
480 &ife->stats.tx_bytes, /* missing for 0 */
481 &ife->stats.tx_packets,
482 &ife->stats.tx_errors,
483 &ife->stats.tx_dropped,
484 &ife->stats.tx_fifo_errors,
485 &ife->stats.collisions,
486 &ife->stats.tx_carrier_errors,
487 &ife->stats.tx_compressed /* missing for <= 1 */
488 );
489
490 if (procnetdev_vsn <= 1) {
491 if (procnetdev_vsn == 0) {
492 ife->stats.rx_bytes = 0;
493 ife->stats.tx_bytes = 0;
494 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000495 ife->stats.rx_multicast = 0;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000496 ife->stats.rx_compressed = 0;
497 ife->stats.tx_compressed = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000498 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000499}
500
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000501static int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000502{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000503 if (strstr(buf, "compressed"))
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000504 return 2;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000505 if (strstr(buf, "bytes"))
506 return 1;
507 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000508}
509
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000510static int if_readconf(void)
511{
512 int numreqs = 30;
513 struct ifconf ifc;
514 struct ifreq *ifr;
515 int n, err = -1;
516 int skfd;
517
518 ifc.ifc_buf = NULL;
519
520 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
521 (as of 2.1.128) */
522 skfd = socket(AF_INET, SOCK_DGRAM, 0);
523 if (skfd < 0) {
524 bb_perror_msg("error: no inet socket available");
525 return -1;
526 }
527
528 for (;;) {
529 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
530 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
531
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000532 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000533 goto out;
534 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000535 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000536 /* assume it overflowed and try again */
537 numreqs += 10;
538 continue;
539 }
540 break;
541 }
542
543 ifr = ifc.ifc_req;
544 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
545 add_interface(ifr->ifr_name);
546 ifr++;
547 }
548 err = 0;
549
550 out:
551 close(skfd);
552 free(ifc.ifc_buf);
553 return err;
554}
555
Eric Andersenf15d4da2001-03-06 00:48:59 +0000556static int if_readlist_proc(char *target)
557{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000558 static smallint proc_read;
559
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000560 FILE *fh;
561 char buf[512];
562 struct interface *ife;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000563 int err, procnetdev_vsn;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000564
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000565 if (proc_read)
566 return 0;
567 if (!target)
568 proc_read = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000569
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000570 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000571 if (!fh) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000572 return if_readconf();
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000573 }
574 fgets(buf, sizeof buf, fh); /* eat line */
575 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000576
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000577 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000578
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000579 err = 0;
580 while (fgets(buf, sizeof buf, fh)) {
Eric Andersenf96675b2003-07-28 06:37:04 +0000581 char *s, name[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000582
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000583 s = get_name(name, buf);
584 ife = add_interface(name);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000585 get_dev_fields(s, ife, procnetdev_vsn);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000586 ife->statistics_valid = 1;
587 if (target && !strcmp(target, name))
588 break;
589 }
590 if (ferror(fh)) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000591 bb_perror_msg(_PATH_PROCNET_DEV);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000592 err = -1;
593 proc_read = 0;
594 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000595 fclose(fh);
596 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000597}
598
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000599static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000600{
Denis Vlasenkod0587ed2007-03-22 19:35:51 +0000601 int err = if_readlist_proc(NULL);
602 /* Needed in order to get ethN:M aliases */
603 if (!err)
604 err = if_readconf();
605 return err;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000606}
607
Eric Andersenf15d4da2001-03-06 00:48:59 +0000608/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000609static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000610{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000611 struct ifreq ifr;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000612 char *ifname = ife->name;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000613 int skfd;
614
615 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000616
Denis Vlasenko360d9662008-12-02 18:18:50 +0000617 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000618 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
619 close(skfd);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000620 return -1;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000621 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000622 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000623
Denis Vlasenko360d9662008-12-02 18:18:50 +0000624 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000625 memset(ife->hwaddr, 0, 32);
626 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000627 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000628
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000629 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000630
Denis Vlasenko360d9662008-12-02 18:18:50 +0000631 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000632 ife->metric = 0;
633 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000634 ife->metric = ifr.ifr_metric;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000635
Denis Vlasenko360d9662008-12-02 18:18:50 +0000636 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000637 ife->mtu = 0;
638 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000639 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000640
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000641 memset(&ife->map, 0, sizeof(struct ifmap));
Rob Landley93983042005-05-03 21:30:26 +0000642#ifdef SIOCGIFMAP
Denis Vlasenko360d9662008-12-02 18:18:50 +0000643 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Rob Landley93983042005-05-03 21:30:26 +0000644 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000645 ife->map = ifr.ifr_map;
Rob Landley93983042005-05-03 21:30:26 +0000646#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000647
648#ifdef HAVE_TXQUEUELEN
Denis Vlasenko360d9662008-12-02 18:18:50 +0000649 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000650 ife->tx_queue_len = -1; /* unknown value */
651 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000652 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000653#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000654 ife->tx_queue_len = -1; /* unknown value */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000655#endif
656
Denis Vlasenko360d9662008-12-02 18:18:50 +0000657 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000658 ifr.ifr_addr.sa_family = AF_INET;
659 memset(&ife->addr, 0, sizeof(struct sockaddr));
660 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
661 ife->has_ip = 1;
662 ife->addr = ifr.ifr_addr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000663 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000664 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
665 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
666 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000667
Denis Vlasenko360d9662008-12-02 18:18:50 +0000668 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000669 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
670 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
671 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000672
Denis Vlasenko360d9662008-12-02 18:18:50 +0000673 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000674 memset(&ife->netmask, 0, sizeof(struct sockaddr));
675 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
676 ife->netmask = ifr.ifr_netmask;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000677 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000678
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000679 close(skfd);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000680 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000681}
682
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000683static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000684{
685 if (if_fetch(ife) < 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000686 const char *errmsg;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000687
688 if (errno == ENODEV) {
689 /* Give better error message for this case. */
Rob Landley4e3aff32006-05-29 04:37:28 +0000690 errmsg = "Device not found";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000691 } else {
692 errmsg = strerror(errno);
693 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000694 bb_error_msg("%s: error fetching interface information: %s",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000695 ife->name, errmsg);
696 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000697 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000698 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000699}
700
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000701static const struct hwtype unspec_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000702 .name = "unspec",
703 .title = "UNSPEC",
704 .type = -1,
705 .print = UNSPEC_print
Eric Andersenf15d4da2001-03-06 00:48:59 +0000706};
707
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000708static const struct hwtype loop_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000709 .name = "loop",
710 .title = "Local Loopback",
711 .type = ARPHRD_LOOPBACK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000712};
713
Eric Andersenf15d4da2001-03-06 00:48:59 +0000714/* Display an Ethernet address in readable format. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000715static char* FAST_FUNC ether_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000716{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000717 static char *buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000718
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000719 free(buff);
720 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000721 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
722 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
723 );
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000724 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000725}
726
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000727static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap);
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000728
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000729static const struct hwtype ether_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000730 .name = "ether",
731 .title = "Ethernet",
732 .type = ARPHRD_ETHER,
733 .alen = ETH_ALEN,
734 .print = ether_print,
735 .input = ether_input
Eric Andersenf15d4da2001-03-06 00:48:59 +0000736};
737
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000738static unsigned hexchar2int(char c)
739{
740 if (isdigit(c))
741 return c - '0';
742 c &= ~0x20; /* a -> A */
743 if ((unsigned)(c - 'A') <= 5)
744 return c - ('A' - 10);
745 return ~0U;
746}
747
748/* Input an Ethernet address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000749static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000750{
751 unsigned char *ptr;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000752 char c;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000753 int i;
754 unsigned val;
755
756 sap->sa_family = ether_hwtype.type;
Denis Vlasenko731d3572007-02-02 01:16:33 +0000757 ptr = (unsigned char*) sap->sa_data;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000758
759 i = 0;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000760 while ((*bufp != '\0') && (i < ETH_ALEN)) {
761 val = hexchar2int(*bufp++) * 0x10;
762 if (val > 0xff) {
763 errno = EINVAL;
764 return -1;
765 }
766 c = *bufp;
767 if (c == ':' || c == 0)
768 val >>= 4;
769 else {
770 val |= hexchar2int(c);
771 if (val > 0xff) {
772 errno = EINVAL;
773 return -1;
774 }
775 }
776 if (c != 0)
777 bufp++;
778 *ptr++ = (unsigned char) val;
779 i++;
780
781 /* We might get a semicolon here - not required. */
782 if (*bufp == ':') {
783 bufp++;
784 }
785 }
786 return 0;
787}
788
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000789static const struct hwtype ppp_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000790 .name = "ppp",
791 .title = "Point-to-Point Protocol",
792 .type = ARPHRD_PPP
Eric Andersenf15d4da2001-03-06 00:48:59 +0000793};
794
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000795#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000796static const struct hwtype sit_hwtype = {
797 .name = "sit",
798 .title = "IPv6-in-IPv4",
799 .type = ARPHRD_SIT,
800 .print = UNSPEC_print,
801 .suppress_null_addr = 1
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000802};
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000803#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000804#if ENABLE_FEATURE_HWIB
805static const struct hwtype ib_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000806 .name = "infiniband",
807 .title = "InfiniBand",
808 .type = ARPHRD_INFINIBAND,
809 .alen = INFINIBAND_ALEN,
810 .print = UNSPEC_print,
811 .input = in_ib,
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000812};
813#endif
814
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000815
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000816static const struct hwtype *const hwtypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000817 &loop_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000818 &ether_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000819 &ppp_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000820 &unspec_hwtype,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000821#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000822 &sit_hwtype,
823#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000824#if ENABLE_FEATURE_HWIB
825 &ib_hwtype,
826#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000827 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000828};
829
Eric Andersenf15d4da2001-03-06 00:48:59 +0000830#ifdef IFF_PORTSEL
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000831static const char *const if_port_text[] = {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000832 /* Keep in step with <linux/netdevice.h> */
833 "unknown",
834 "10base2",
835 "10baseT",
836 "AUI",
837 "100baseT",
838 "100baseTX",
839 "100baseFX",
840 NULL
841};
842#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000843
844/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000845const struct hwtype* FAST_FUNC get_hwtype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000846{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000847 const struct hwtype *const *hwp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000848
849 hwp = hwtypes;
850 while (*hwp != NULL) {
851 if (!strcmp((*hwp)->name, name))
852 return (*hwp);
853 hwp++;
854 }
855 return NULL;
856}
857
858/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000859const struct hwtype* FAST_FUNC get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000860{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000861 const struct hwtype *const *hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000862
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000863 hwp = hwtypes;
864 while (*hwp != NULL) {
865 if ((*hwp)->type == type)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000866 return *hwp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000867 hwp++;
868 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000869 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000870}
871
872/* return 1 if address is all zeros */
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000873static int hw_null_address(const struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000874{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000875 int i;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000876 unsigned char *address = (unsigned char *) ap;
877
878 for (i = 0; i < hw->alen; i++)
879 if (address[i])
880 return 0;
881 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000882}
883
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000884static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000885
886static void print_bytes_scaled(unsigned long long ull, const char *end)
887{
888 unsigned long long int_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000889 const char *ext;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000890 unsigned int frac_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000891 int i;
892
893 frac_part = 0;
894 ext = TRext;
895 int_part = ull;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000896 i = 4;
897 do {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000898 if (int_part >= 1024) {
899 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
900 int_part /= 1024;
901 ext += 3; /* KiB, MiB, GiB, TiB */
902 }
903 --i;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000904 } while (i);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000905
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000906 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000907}
908
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000909
910#ifdef HAVE_AFINET6
911#define IPV6_ADDR_ANY 0x0000U
912
913#define IPV6_ADDR_UNICAST 0x0001U
914#define IPV6_ADDR_MULTICAST 0x0002U
915#define IPV6_ADDR_ANYCAST 0x0004U
916
917#define IPV6_ADDR_LOOPBACK 0x0010U
918#define IPV6_ADDR_LINKLOCAL 0x0020U
919#define IPV6_ADDR_SITELOCAL 0x0040U
920
921#define IPV6_ADDR_COMPATv4 0x0080U
922
923#define IPV6_ADDR_SCOPE_MASK 0x00f0U
924
925#define IPV6_ADDR_MAPPED 0x1000U
926#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
927
928
929static void ife_print6(struct interface *ptr)
930{
931
932 FILE *f;
933 char addr6[40], devname[20];
934 struct sockaddr_in6 sap;
935 int plen, scope, dad_status, if_idx;
936 char addr6p[8][5];
937
938 f = fopen_for_read(_PATH_PROCNET_IFINET6);
939 if (f == NULL)
940 return;
941
942 while (fscanf
943 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
944 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
945 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
946 &dad_status, devname) != EOF
947 ) {
948 if (!strcmp(devname, ptr->name)) {
949 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
950 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
951 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
952 inet_pton(AF_INET6, addr6,
953 (struct sockaddr *) &sap.sin6_addr);
954 sap.sin6_family = AF_INET6;
955 printf(" inet6 addr: %s/%d",
956 INET6_sprint((struct sockaddr *) &sap, 1),
957 plen);
958 printf(" Scope:");
959 switch (scope & IPV6_ADDR_SCOPE_MASK) {
960 case 0:
961 puts("Global");
962 break;
963 case IPV6_ADDR_LINKLOCAL:
964 puts("Link");
965 break;
966 case IPV6_ADDR_SITELOCAL:
967 puts("Site");
968 break;
969 case IPV6_ADDR_COMPATv4:
970 puts("Compat");
971 break;
972 case IPV6_ADDR_LOOPBACK:
973 puts("Host");
974 break;
975 default:
976 puts("Unknown");
977 }
978 }
979 }
980 fclose(f);
981}
982#else
Denis Vlasenko00d84172008-11-24 07:34:42 +0000983#define ife_print6(a) ((void)0)
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000984#endif
985
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000986static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000987{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000988 const struct aftype *ap;
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000989 const struct hwtype *hw;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000990 int hf;
991 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000992
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000993 ap = get_afntype(ptr->addr.sa_family);
994 if (ap == NULL)
995 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000996
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000997 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000998
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000999 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1000 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001001
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001002 hw = get_hwntype(hf);
1003 if (hw == NULL)
1004 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001005
Rob Landley4e3aff32006-05-29 04:37:28 +00001006 printf("%-9.9s Link encap:%s ", ptr->name, hw->title);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001007 /* For some hardware types (eg Ash, ATM) we don't print the
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001008 hardware address if it's null. */
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001009 if (hw->print != NULL
1010 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
1011 ) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001012 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001013 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001014#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001015 if (ptr->flags & IFF_PORTSEL) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001016 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001017 if (ptr->flags & IFF_AUTOMEDIA)
Rob Landley4e3aff32006-05-29 04:37:28 +00001018 printf("(auto)");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001019 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001020#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001021 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001022
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001023 if (ptr->has_ip) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001024 printf(" %s addr:%s ", ap->name,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001025 ap->sprint(&ptr->addr, 1));
1026 if (ptr->flags & IFF_POINTOPOINT) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001027 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001028 }
1029 if (ptr->flags & IFF_BROADCAST) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001030 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001031 }
Rob Landley4e3aff32006-05-29 04:37:28 +00001032 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001033 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001034
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001035 ife_print6(ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001036
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001037 printf(" ");
1038 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001039
1040 if (ptr->flags == 0) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001041 printf("[NO FLAGS] ");
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001042 } else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001043 static const char ife_print_flags_strs[] ALIGN1 =
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001044 "UP\0"
1045 "BROADCAST\0"
1046 "DEBUG\0"
1047 "LOOPBACK\0"
1048 "POINTOPOINT\0"
1049 "NOTRAILERS\0"
1050 "RUNNING\0"
1051 "NOARP\0"
1052 "PROMISC\0"
1053 "ALLMULTI\0"
1054 "SLAVE\0"
1055 "MASTER\0"
1056 "MULTICAST\0"
1057#ifdef HAVE_DYNAMIC
1058 "DYNAMIC\0"
1059#endif
1060 ;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001061 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001062 IFF_UP,
1063 IFF_BROADCAST,
1064 IFF_DEBUG,
1065 IFF_LOOPBACK,
1066 IFF_POINTOPOINT,
1067 IFF_NOTRAILERS,
1068 IFF_RUNNING,
1069 IFF_NOARP,
1070 IFF_PROMISC,
1071 IFF_ALLMULTI,
1072 IFF_SLAVE,
1073 IFF_MASTER,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001074 IFF_MULTICAST
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001075#ifdef HAVE_DYNAMIC
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001076 ,IFF_DYNAMIC
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001077#endif
1078 };
1079 const unsigned short *mask = ife_print_flags_mask;
1080 const char *str = ife_print_flags_strs;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001081 do {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001082 if (ptr->flags & *mask) {
1083 printf("%s ", str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001084 }
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001085 mask++;
1086 str += strlen(str) + 1;
1087 } while (*str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001088 }
1089
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001090 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
Rob Landley4e3aff32006-05-29 04:37:28 +00001091 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001092#ifdef SIOCSKEEPALIVE
1093 if (ptr->outfill || ptr->keepalive)
Rob Landley4e3aff32006-05-29 04:37:28 +00001094 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001095#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001096 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001097
1098 /* If needed, display the interface statistics. */
1099
1100 if (ptr->statistics_valid) {
1101 /* XXX: statistics are currently only printed for the primary address,
1102 * not for the aliases, although strictly speaking they're shared
1103 * by all addresses.
1104 */
1105 printf(" ");
1106
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001107 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001108 ptr->stats.rx_packets, ptr->stats.rx_errors,
1109 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1110 ptr->stats.rx_frame_errors);
1111 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001112 printf(" compressed:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001113 ptr->stats.rx_compressed);
1114 printf(" ");
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001115 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001116 ptr->stats.tx_packets, ptr->stats.tx_errors,
1117 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1118 ptr->stats.tx_carrier_errors);
Bernhard Reutner-Fischer214744d2006-03-30 13:38:19 +00001119 printf(" collisions:%lu ", ptr->stats.collisions);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001120 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001121 printf("compressed:%lu ", ptr->stats.tx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001122 if (ptr->tx_queue_len != -1)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001123 printf("txqueuelen:%d ", ptr->tx_queue_len);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001124 printf("\n R");
1125 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1126 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001127 }
1128
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001129 if (ptr->map.irq || ptr->map.mem_start
1130 || ptr->map.dma || ptr->map.base_addr
1131 ) {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001132 printf(" ");
1133 if (ptr->map.irq)
Rob Landley4e3aff32006-05-29 04:37:28 +00001134 printf("Interrupt:%d ", ptr->map.irq);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001135 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001136 I/O maps */
Rob Landley4e3aff32006-05-29 04:37:28 +00001137 printf("Base address:0x%lx ",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001138 (unsigned long) ptr->map.base_addr);
1139 if (ptr->map.mem_start) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001140 printf("Memory:%lx-%lx ", ptr->map.mem_start,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001141 ptr->map.mem_end);
1142 }
1143 if (ptr->map.dma)
Rob Landley4e3aff32006-05-29 04:37:28 +00001144 printf("DMA chan:%x ", ptr->map.dma);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001145 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001146 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001147 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001148}
1149
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001150static int do_if_print(struct interface *ife) /*, int *opt_a)*/
Eric Andersenf15d4da2001-03-06 00:48:59 +00001151{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001152 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001153
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001154 res = do_if_fetch(ife);
1155 if (res >= 0) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001156 if ((ife->flags & IFF_UP) || interface_opt_a)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001157 ife_print(ife);
1158 }
1159 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001160}
1161
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001162static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001163{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001164 struct interface *ife = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001165
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001166 if (if_readlist_proc(name) < 0)
1167 return NULL;
1168 ife = add_interface(name);
1169 return ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001170}
1171
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001172#ifdef UNUSED
1173static int for_all_interfaces(int (*doit) (struct interface *, void *),
1174 void *cookie)
1175{
1176 struct interface *ife;
1177
1178 if (!int_list && (if_readlist() < 0))
1179 return -1;
1180 for (ife = int_list; ife; ife = ife->next) {
1181 int err = doit(ife, cookie);
1182
1183 if (err)
1184 return err;
1185 }
1186 return 0;
1187}
1188#endif
1189
Eric Andersenf15d4da2001-03-06 00:48:59 +00001190/* for ipv4 add/del modes */
1191static int if_print(char *ifname)
1192{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001193 struct interface *ife;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001194 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001195
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001196 if (!ifname) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001197 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1198 if (!int_list && (if_readlist() < 0))
1199 return -1;
1200 for (ife = int_list; ife; ife = ife->next) {
1201 int err = do_if_print(ife); /*, &interface_opt_a);*/
1202 if (err)
1203 return err;
1204 }
1205 return 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001206 }
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001207 ife = lookup_interface(ifname);
1208 res = do_if_fetch(ife);
1209 if (res >= 0)
1210 ife_print(ife);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001211 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001212}
1213
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001214#if ENABLE_FEATURE_HWIB
1215/* Input an Infiniband address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001216int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001217{
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001218 unsigned char *ptr;
1219 char c;
1220 const char *orig;
1221 int i;
1222 unsigned val;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001223
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001224 sap->sa_family = ib_hwtype.type;
1225 ptr = (unsigned char *) sap->sa_data;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001226
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001227 i = 0;
1228 orig = bufp;
1229 while ((*bufp != '\0') && (i < INFINIBAND_ALEN)) {
1230 val = 0;
1231 c = *bufp++;
1232 if (isdigit(c))
1233 val = c - '0';
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02001234 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
1235 val = (c|0x20) - ('a' - 10);
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001236 else {
1237 errno = EINVAL;
1238 return -1;
1239 }
1240 val <<= 4;
1241 c = *bufp;
1242 if (isdigit(c))
1243 val |= c - '0';
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02001244 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
1245 val |= (c|0x20) - ('a' - 10);
1246 else if (c == ':' || c == '\0')
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001247 val >>= 4;
1248 else {
1249 errno = EINVAL;
1250 return -1;
1251 }
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02001252 if (c != '\0')
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001253 bufp++;
1254 *ptr++ = (unsigned char) (val & 0377);
1255 i++;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001256
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001257 /* We might get a semicolon here - not required. */
1258 if (*bufp == ':') {
1259 bufp++;
1260 }
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001261 }
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001262#ifdef DEBUG
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001263 fprintf(stderr, "in_ib(%s): %s\n", orig, UNSPEC_print(sap->sa_data));
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001264#endif
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001265 return 0;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001266}
1267#endif
1268
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001269int FAST_FUNC display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001270{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001271 int status;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001272
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001273 status = if_print(ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +00001274
1275 return (status < 0); /* status < 0 == 1 -- error */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001276}