blob: 79c322ec01b3588c5ad5c0045d4a5727e7325a2d [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.
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 */
Dan Fandrichf533ec82011-06-10 05:17:59 +020033
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Denys Vlasenkobb1dcc92010-02-02 12:45:38 +010035#include "inet_common.h"
Dan Fandrichf533ec82011-06-10 05:17:59 +020036#include <net/if.h>
37#include <net/if_arp.h>
38#ifdef HAVE_NET_ETHERNET_H
39# include <net/ethernet.h>
40#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000041
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000042#if ENABLE_FEATURE_HWIB
43/* #include <linux/if_infiniband.h> */
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020044# undef INFINIBAND_ALEN
45# define INFINIBAND_ALEN 20
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000046#endif
47
Denis Vlasenko1cc70222007-03-15 19:46:43 +000048#if ENABLE_FEATURE_IPV6
Glenn L McGrathb54a7482003-08-29 11:34:08 +000049# define HAVE_AFINET6 1
50#else
51# undef HAVE_AFINET6
52#endif
53
Eric Andersenf15d4da2001-03-06 00:48:59 +000054#define _PATH_PROCNET_DEV "/proc/net/dev"
Eric Andersen51b8bd62002-07-03 11:46:38 +000055#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
Eric Andersenf15d4da2001-03-06 00:48:59 +000056
Denis Vlasenkoaad49992006-11-22 02:12:07 +000057#ifdef HAVE_AFINET6
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020058# ifndef _LINUX_IN6_H
Eric Andersenf15d4da2001-03-06 00:48:59 +000059/*
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020060 * This is from linux/include/net/ipv6.h
Eric Andersenf15d4da2001-03-06 00:48:59 +000061 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000062struct in6_ifreq {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000063 struct in6_addr ifr6_addr;
Glenn L McGrathb54a7482003-08-29 11:34:08 +000064 uint32_t ifr6_prefixlen;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000065 unsigned int ifr6_ifindex;
Eric Andersenf15d4da2001-03-06 00:48:59 +000066};
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020067# endif
Denis Vlasenkoaad49992006-11-22 02:12:07 +000068#endif /* HAVE_AFINET6 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000069
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000070/* Defines for glibc2.0 users. */
71#ifndef SIOCSIFTXQLEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020072# define SIOCSIFTXQLEN 0x8943
73# define SIOCGIFTXQLEN 0x8942
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000074#endif
75
76/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
77#ifndef ifr_qlen
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020078# define ifr_qlen ifr_ifru.ifru_mtu
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000079#endif
80
81#ifndef HAVE_TXQUEUELEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020082# define HAVE_TXQUEUELEN 1
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000083#endif
84
85#ifndef IFF_DYNAMIC
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020086# define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000087#endif
88
Eric Andersenf15d4da2001-03-06 00:48:59 +000089/* Display an Internet socket address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000090static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
Eric Andersenf15d4da2001-03-06 00:48:59 +000091{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +000092 static char *buff; /* defaults to NULL */
Eric Andersenf15d4da2001-03-06 00:48:59 +000093
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000094 free(buff);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000095 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000096 return "[NONE SET]";
Denis Vlasenko6d9ea242007-06-19 11:12:46 +000097 buff = INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00);
Denis Vlasenkoaad49992006-11-22 02:12:07 +000098 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +000099}
100
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000101#ifdef UNUSED_AND_BUGGY
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000102static int INET_getsock(char *bufp, struct sockaddr *sap)
103{
104 char *sp = bufp, *bp;
105 unsigned int i;
106 unsigned val;
107 struct sockaddr_in *sock_in;
108
109 sock_in = (struct sockaddr_in *) sap;
110 sock_in->sin_family = AF_INET;
111 sock_in->sin_port = 0;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000112
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000113 val = 0;
114 bp = (char *) &val;
115 for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
116 *sp = toupper(*sp);
117
118 if ((unsigned)(*sp - 'A') <= 5)
119 bp[i] |= (int) (*sp - ('A' - 10));
120 else if (isdigit(*sp))
121 bp[i] |= (int) (*sp - '0');
122 else
123 return -1;
124
125 bp[i] <<= 4;
126 sp++;
127 *sp = toupper(*sp);
128
129 if ((unsigned)(*sp - 'A') <= 5)
130 bp[i] |= (int) (*sp - ('A' - 10));
131 else if (isdigit(*sp))
132 bp[i] |= (int) (*sp - '0');
133 else
134 return -1;
135
136 sp++;
137 }
138 sock_in->sin_addr.s_addr = htonl(val);
139
140 return (sp - bufp);
141}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000142#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000143
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000144static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000145{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000146 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
147/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000148 switch (type) {
149 case 1:
150 return (INET_getsock(bufp, sap));
151 case 256:
152 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
153 default:
154 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
155 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000156*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000157}
158
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000159static const struct aftype inet_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000160 .name = "inet",
161 .title = "DARPA Internet",
162 .af = AF_INET,
163 .alen = 4,
164 .sprint = INET_sprint,
165 .input = INET_input,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000166};
167
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000168#ifdef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000169
Eric Andersen51b8bd62002-07-03 11:46:38 +0000170/* Display an Internet socket address. */
171/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000172static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000173{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000174 static char *buff;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000175
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000176 free(buff);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000177 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000178 return "[NONE SET]";
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000179 buff = INET6_rresolve((struct sockaddr_in6 *) sap, numeric);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000180 return buff;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000181}
182
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000183#ifdef UNUSED
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000184static int INET6_getsock(char *bufp, struct sockaddr *sap)
185{
186 struct sockaddr_in6 *sin6;
187
188 sin6 = (struct sockaddr_in6 *) sap;
189 sin6->sin6_family = AF_INET6;
190 sin6->sin6_port = 0;
191
192 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
193 return -1;
194
195 return 16; /* ?;) */
196}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000197#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000198
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000199static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000200{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000201 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
202/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000203 switch (type) {
204 case 1:
205 return (INET6_getsock(bufp, sap));
206 default:
207 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
208 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000209*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000210}
211
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000212static const struct aftype inet6_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000213 .name = "inet6",
214 .title = "IPv6",
215 .af = AF_INET6,
216 .alen = sizeof(struct in6_addr),
217 .sprint = INET6_sprint,
218 .input = INET6_input,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000219};
220
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000221#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000222
Eric Andersenf15d4da2001-03-06 00:48:59 +0000223/* Display an UNSPEC address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000224static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000225{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000226 static char *buff;
227
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000228 char *pos;
229 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000230
Denis Vlasenko43d5d422008-05-15 19:44:46 +0000231 if (!buff)
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000232 buff = xmalloc(sizeof(struct sockaddr) * 3 + 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000233 pos = buff;
234 for (i = 0; i < sizeof(struct sockaddr); i++) {
235 /* careful -- not every libc's sprintf returns # bytes written */
236 sprintf(pos, "%02X-", (*ptr++ & 0377));
237 pos += 3;
238 }
239 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
240 *--pos = '\0';
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000241 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000242}
243
244/* Display an UNSPEC socket address. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000245static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000246{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000247 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000248 return "[NONE SET]";
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000249 return UNSPEC_print((unsigned char *)sap->sa_data);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000250}
251
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000252static const struct aftype unspec_aftype = {
253 .name = "unspec",
254 .title = "UNSPEC",
255 .af = AF_UNSPEC,
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000256 .alen = 0,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000257 .print = UNSPEC_print,
258 .sprint = UNSPEC_sprint,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000259};
260
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000261static const struct aftype *const aftypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000262 &inet_aftype,
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000263#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000264 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000265#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000266 &unspec_aftype,
267 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000268};
269
Eric Andersenf15d4da2001-03-06 00:48:59 +0000270/* Check our protocol family table for this family. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000271const struct aftype* FAST_FUNC get_aftype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000272{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000273 const struct aftype *const *afp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000274
275 afp = aftypes;
276 while (*afp != NULL) {
277 if (!strcmp((*afp)->name, name))
278 return (*afp);
279 afp++;
280 }
281 return NULL;
282}
283
284/* Check our protocol family table for this family. */
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000285static const struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000286{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000287 const struct aftype *const *afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000288
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000289 afp = aftypes;
290 while (*afp != NULL) {
291 if ((*afp)->af == af)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000292 return *afp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000293 afp++;
294 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000295 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000296}
297
Eric Andersenf15d4da2001-03-06 00:48:59 +0000298struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000299 unsigned long long rx_packets; /* total packets received */
300 unsigned long long tx_packets; /* total packets transmitted */
301 unsigned long long rx_bytes; /* total bytes received */
302 unsigned long long tx_bytes; /* total bytes transmitted */
303 unsigned long rx_errors; /* bad packets received */
304 unsigned long tx_errors; /* packet transmit problems */
305 unsigned long rx_dropped; /* no space in linux buffers */
306 unsigned long tx_dropped; /* no space available in linux */
307 unsigned long rx_multicast; /* multicast packets received */
308 unsigned long rx_compressed;
309 unsigned long tx_compressed;
310 unsigned long collisions;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000311
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000312 /* detailed rx_errors: */
313 unsigned long rx_length_errors;
314 unsigned long rx_over_errors; /* receiver ring buff overflow */
315 unsigned long rx_crc_errors; /* recved pkt with crc error */
316 unsigned long rx_frame_errors; /* recv'd frame alignment error */
317 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
318 unsigned long rx_missed_errors; /* receiver missed packet */
319 /* detailed tx_errors */
320 unsigned long tx_aborted_errors;
321 unsigned long tx_carrier_errors;
322 unsigned long tx_fifo_errors;
323 unsigned long tx_heartbeat_errors;
324 unsigned long tx_window_errors;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000325};
326
327struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000328 struct interface *next, *prev;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000329 char name[IFNAMSIZ]; /* interface name */
330 short type; /* if type */
331 short flags; /* various flags */
332 int metric; /* routing metric */
333 int mtu; /* MTU value */
334 int tx_queue_len; /* transmit queue length */
335 struct ifmap map; /* hardware setup */
336 struct sockaddr addr; /* IP address */
337 struct sockaddr dstaddr; /* P-P IP address */
338 struct sockaddr broadaddr; /* IP broadcast address */
339 struct sockaddr netmask; /* IP network mask */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000340 int has_ip;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000341 char hwaddr[32]; /* HW address */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000342 int statistics_valid;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000343 struct user_net_device_stats stats; /* statistics */
344 int keepalive; /* keepalive value for SLIP */
345 int outfill; /* outfill value for SLIP */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000346};
347
348
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000349smallint interface_opt_a; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000350
Eric Andersenf15d4da2001-03-06 00:48:59 +0000351static struct interface *int_list, *int_last;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000352
353
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000354#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000355/* like strcmp(), but knows about numbers */
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000356except that the freshly added calls to xatoul() brf on ethernet aliases with
357uClibc with e.g.: ife->name='lo' name='eth0:1'
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000358static int nstrcmp(const char *a, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000359{
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000360 const char *a_ptr = a;
361 const char *b_ptr = b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000362
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000363 while (*a == *b) {
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000364 if (*a == '\0') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000365 return 0;
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000366 }
367 if (!isdigit(*a) && isdigit(*(a+1))) {
368 a_ptr = a+1;
369 b_ptr = b+1;
370 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000371 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000372 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000373 }
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000374
375 if (isdigit(*a) && isdigit(*b)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000376 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000377 }
378 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000379}
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000380#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000381
382static struct interface *add_interface(char *name)
383{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000384 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000385
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000386 for (ife = int_last; ife; ife = ife->prev) {
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000387 int n = /*n*/strcmp(ife->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000388
389 if (n == 0)
390 return ife;
391 if (n < 0)
392 break;
393 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000394
395 new = xzalloc(sizeof(*new));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000396 strncpy_IFNAMSIZ(new->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000397 nextp = ife ? &ife->next : &int_list;
398 new->prev = ife;
399 new->next = *nextp;
400 if (new->next)
401 new->next->prev = new;
402 else
403 int_last = new;
404 *nextp = new;
405 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000406}
407
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000408static char *get_name(char *name, char *p)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000409{
Rob Landley9fe801e2006-06-20 21:13:29 +0000410 /* Extract <name> from nul-terminated p where p matches
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200411 * <name>: after leading whitespace.
412 * If match is not made, set name empty and return unchanged p
413 */
414 char *nameend;
415 char *namestart = skip_whitespace(p);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000416
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000417 nameend = namestart;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200418 while (*nameend && *nameend != ':' && !isspace(*nameend))
Eric Andersen9940e082004-08-12 16:52:00 +0000419 nameend++;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200420 if (*nameend == ':') {
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000421 if ((nameend - namestart) < IFNAMSIZ) {
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200422 memcpy(name, namestart, nameend - namestart);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000423 name[nameend - namestart] = '\0';
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200424 p = nameend;
Eric Andersen9940e082004-08-12 16:52:00 +0000425 } else {
426 /* Interface name too large */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000427 name[0] = '\0';
Eric Andersen9940e082004-08-12 16:52:00 +0000428 }
429 } else {
Rob Landley9fe801e2006-06-20 21:13:29 +0000430 /* trailing ':' not found - return empty */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000431 name[0] = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000432 }
Eric Andersen6fea7322004-08-26 21:45:21 +0000433 return p + 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000434}
435
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000436/* If scanf supports size qualifiers for %n conversions, then we can
437 * use a modified fmt that simply stores the position in the fields
438 * having no associated fields in the proc string. Of course, we need
439 * to zero them again when we're done. But that is smaller than the
440 * old approach of multiple scanf occurrences with large numbers of
441 * args. */
442
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000443/* static const char *const ss_fmt[] = { */
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000444/* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
445/* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
446/* "%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 +0000447/* }; */
448
449 /* Lie about the size of the int pointed to for %n. */
450#if INT_MAX == LONG_MAX
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000451static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000452 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
453 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
454 "%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 +0000455};
456#else
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000457static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000458 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
459 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
460 "%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 +0000461};
462
463#endif
464
465static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000466{
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000467 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
468
469 sscanf(bp, ss_fmt[procnetdev_vsn],
470 &ife->stats.rx_bytes, /* missing for 0 */
471 &ife->stats.rx_packets,
472 &ife->stats.rx_errors,
473 &ife->stats.rx_dropped,
474 &ife->stats.rx_fifo_errors,
475 &ife->stats.rx_frame_errors,
476 &ife->stats.rx_compressed, /* missing for <= 1 */
477 &ife->stats.rx_multicast, /* missing for <= 1 */
478 &ife->stats.tx_bytes, /* missing for 0 */
479 &ife->stats.tx_packets,
480 &ife->stats.tx_errors,
481 &ife->stats.tx_dropped,
482 &ife->stats.tx_fifo_errors,
483 &ife->stats.collisions,
484 &ife->stats.tx_carrier_errors,
485 &ife->stats.tx_compressed /* missing for <= 1 */
486 );
487
488 if (procnetdev_vsn <= 1) {
489 if (procnetdev_vsn == 0) {
490 ife->stats.rx_bytes = 0;
491 ife->stats.tx_bytes = 0;
492 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000493 ife->stats.rx_multicast = 0;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000494 ife->stats.rx_compressed = 0;
495 ife->stats.tx_compressed = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000496 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000497}
498
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000499static int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000500{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000501 if (strstr(buf, "compressed"))
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000502 return 2;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000503 if (strstr(buf, "bytes"))
504 return 1;
505 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000506}
507
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000508static int if_readconf(void)
509{
510 int numreqs = 30;
511 struct ifconf ifc;
512 struct ifreq *ifr;
513 int n, err = -1;
514 int skfd;
515
516 ifc.ifc_buf = NULL;
517
518 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
519 (as of 2.1.128) */
520 skfd = socket(AF_INET, SOCK_DGRAM, 0);
521 if (skfd < 0) {
522 bb_perror_msg("error: no inet socket available");
523 return -1;
524 }
525
526 for (;;) {
527 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
528 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
529
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000530 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000531 goto out;
532 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000533 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000534 /* assume it overflowed and try again */
535 numreqs += 10;
536 continue;
537 }
538 break;
539 }
540
541 ifr = ifc.ifc_req;
542 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
543 add_interface(ifr->ifr_name);
544 ifr++;
545 }
546 err = 0;
547
548 out:
549 close(skfd);
550 free(ifc.ifc_buf);
551 return err;
552}
553
Eric Andersenf15d4da2001-03-06 00:48:59 +0000554static int if_readlist_proc(char *target)
555{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000556 static smallint proc_read;
557
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000558 FILE *fh;
559 char buf[512];
560 struct interface *ife;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000561 int err, procnetdev_vsn;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000562
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000563 if (proc_read)
564 return 0;
565 if (!target)
566 proc_read = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000567
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000568 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000569 if (!fh) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000570 return if_readconf();
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000571 }
572 fgets(buf, sizeof buf, fh); /* eat line */
573 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000574
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000575 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000576
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000577 err = 0;
578 while (fgets(buf, sizeof buf, fh)) {
Eric Andersenf96675b2003-07-28 06:37:04 +0000579 char *s, name[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000580
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000581 s = get_name(name, buf);
582 ife = add_interface(name);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000583 get_dev_fields(s, ife, procnetdev_vsn);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000584 ife->statistics_valid = 1;
585 if (target && !strcmp(target, name))
586 break;
587 }
588 if (ferror(fh)) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000589 bb_perror_msg(_PATH_PROCNET_DEV);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000590 err = -1;
591 proc_read = 0;
592 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000593 fclose(fh);
594 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000595}
596
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000597static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000598{
Denis Vlasenkod0587ed2007-03-22 19:35:51 +0000599 int err = if_readlist_proc(NULL);
600 /* Needed in order to get ethN:M aliases */
601 if (!err)
602 err = if_readconf();
603 return err;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000604}
605
Eric Andersenf15d4da2001-03-06 00:48:59 +0000606/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000607static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000608{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000609 struct ifreq ifr;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000610 char *ifname = ife->name;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000611 int skfd;
612
613 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
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, SIOCGIFFLAGS, &ifr) < 0) {
617 close(skfd);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000618 return -1;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000619 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000620 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000621
Denis Vlasenko360d9662008-12-02 18:18:50 +0000622 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000623 memset(ife->hwaddr, 0, 32);
624 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000625 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000626
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000627 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000628
Denis Vlasenko360d9662008-12-02 18:18:50 +0000629 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000630 ife->metric = 0;
631 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000632 ife->metric = ifr.ifr_metric;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000633
Denis Vlasenko360d9662008-12-02 18:18:50 +0000634 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000635 ife->mtu = 0;
636 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000637 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000638
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000639 memset(&ife->map, 0, sizeof(struct ifmap));
Rob Landley93983042005-05-03 21:30:26 +0000640#ifdef SIOCGIFMAP
Denis Vlasenko360d9662008-12-02 18:18:50 +0000641 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Rob Landley93983042005-05-03 21:30:26 +0000642 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000643 ife->map = ifr.ifr_map;
Rob Landley93983042005-05-03 21:30:26 +0000644#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000645
646#ifdef HAVE_TXQUEUELEN
Denis Vlasenko360d9662008-12-02 18:18:50 +0000647 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000648 ife->tx_queue_len = -1; /* unknown value */
649 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000650 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000651#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000652 ife->tx_queue_len = -1; /* unknown value */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000653#endif
654
Denis Vlasenko360d9662008-12-02 18:18:50 +0000655 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000656 ifr.ifr_addr.sa_family = AF_INET;
657 memset(&ife->addr, 0, sizeof(struct sockaddr));
658 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
659 ife->has_ip = 1;
660 ife->addr = ifr.ifr_addr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000661 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000662 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
663 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
664 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000665
Denis Vlasenko360d9662008-12-02 18:18:50 +0000666 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000667 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
668 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
669 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000670
Denis Vlasenko360d9662008-12-02 18:18:50 +0000671 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000672 memset(&ife->netmask, 0, sizeof(struct sockaddr));
673 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
674 ife->netmask = ifr.ifr_netmask;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000675 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000676
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000677 close(skfd);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000678 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000679}
680
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000681static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000682{
683 if (if_fetch(ife) < 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000684 const char *errmsg;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000685
686 if (errno == ENODEV) {
687 /* Give better error message for this case. */
Rob Landley4e3aff32006-05-29 04:37:28 +0000688 errmsg = "Device not found";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000689 } else {
690 errmsg = strerror(errno);
691 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000692 bb_error_msg("%s: error fetching interface information: %s",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000693 ife->name, errmsg);
694 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000695 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000696 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000697}
698
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000699static const struct hwtype unspec_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000700 .name = "unspec",
701 .title = "UNSPEC",
702 .type = -1,
703 .print = UNSPEC_print
Eric Andersenf15d4da2001-03-06 00:48:59 +0000704};
705
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000706static const struct hwtype loop_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000707 .name = "loop",
708 .title = "Local Loopback",
709 .type = ARPHRD_LOOPBACK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000710};
711
Eric Andersenf15d4da2001-03-06 00:48:59 +0000712/* Display an Ethernet address in readable format. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000713static char* FAST_FUNC ether_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000714{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000715 static char *buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000716
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000717 free(buff);
718 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000719 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
720 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
721 );
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000722 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000723}
724
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000725static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap);
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000726
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000727static const struct hwtype ether_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000728 .name = "ether",
729 .title = "Ethernet",
730 .type = ARPHRD_ETHER,
731 .alen = ETH_ALEN,
732 .print = ether_print,
733 .input = ether_input
Eric Andersenf15d4da2001-03-06 00:48:59 +0000734};
735
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000736static unsigned hexchar2int(char c)
737{
738 if (isdigit(c))
739 return c - '0';
740 c &= ~0x20; /* a -> A */
741 if ((unsigned)(c - 'A') <= 5)
742 return c - ('A' - 10);
743 return ~0U;
744}
745
746/* Input an Ethernet address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000747static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000748{
749 unsigned char *ptr;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000750 char c;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000751 int i;
752 unsigned val;
753
754 sap->sa_family = ether_hwtype.type;
Denis Vlasenko731d3572007-02-02 01:16:33 +0000755 ptr = (unsigned char*) sap->sa_data;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000756
757 i = 0;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000758 while ((*bufp != '\0') && (i < ETH_ALEN)) {
759 val = hexchar2int(*bufp++) * 0x10;
760 if (val > 0xff) {
761 errno = EINVAL;
762 return -1;
763 }
764 c = *bufp;
765 if (c == ':' || c == 0)
766 val >>= 4;
767 else {
768 val |= hexchar2int(c);
769 if (val > 0xff) {
770 errno = EINVAL;
771 return -1;
772 }
773 }
774 if (c != 0)
775 bufp++;
776 *ptr++ = (unsigned char) val;
777 i++;
778
779 /* We might get a semicolon here - not required. */
780 if (*bufp == ':') {
781 bufp++;
782 }
783 }
784 return 0;
785}
786
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000787static const struct hwtype ppp_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000788 .name = "ppp",
789 .title = "Point-to-Point Protocol",
790 .type = ARPHRD_PPP
Eric Andersenf15d4da2001-03-06 00:48:59 +0000791};
792
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000793#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000794static const struct hwtype sit_hwtype = {
795 .name = "sit",
796 .title = "IPv6-in-IPv4",
797 .type = ARPHRD_SIT,
798 .print = UNSPEC_print,
799 .suppress_null_addr = 1
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000800};
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000801#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000802#if ENABLE_FEATURE_HWIB
803static const struct hwtype ib_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000804 .name = "infiniband",
805 .title = "InfiniBand",
806 .type = ARPHRD_INFINIBAND,
807 .alen = INFINIBAND_ALEN,
808 .print = UNSPEC_print,
809 .input = in_ib,
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000810};
811#endif
812
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000813
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000814static const struct hwtype *const hwtypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000815 &loop_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000816 &ether_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000817 &ppp_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000818 &unspec_hwtype,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000819#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000820 &sit_hwtype,
821#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000822#if ENABLE_FEATURE_HWIB
823 &ib_hwtype,
824#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000825 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000826};
827
Eric Andersenf15d4da2001-03-06 00:48:59 +0000828#ifdef IFF_PORTSEL
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000829static const char *const if_port_text[] = {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000830 /* Keep in step with <linux/netdevice.h> */
831 "unknown",
832 "10base2",
833 "10baseT",
834 "AUI",
835 "100baseT",
836 "100baseTX",
837 "100baseFX",
838 NULL
839};
840#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000841
842/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000843const struct hwtype* FAST_FUNC get_hwtype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000844{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000845 const struct hwtype *const *hwp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000846
847 hwp = hwtypes;
848 while (*hwp != NULL) {
849 if (!strcmp((*hwp)->name, name))
850 return (*hwp);
851 hwp++;
852 }
853 return NULL;
854}
855
856/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000857const struct hwtype* FAST_FUNC get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000858{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000859 const struct hwtype *const *hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000860
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000861 hwp = hwtypes;
862 while (*hwp != NULL) {
863 if ((*hwp)->type == type)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000864 return *hwp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000865 hwp++;
866 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000867 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000868}
869
870/* return 1 if address is all zeros */
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000871static int hw_null_address(const struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000872{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000873 int i;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000874 unsigned char *address = (unsigned char *) ap;
875
876 for (i = 0; i < hw->alen; i++)
877 if (address[i])
878 return 0;
879 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000880}
881
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000882static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000883
884static void print_bytes_scaled(unsigned long long ull, const char *end)
885{
886 unsigned long long int_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000887 const char *ext;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000888 unsigned int frac_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000889 int i;
890
891 frac_part = 0;
892 ext = TRext;
893 int_part = ull;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000894 i = 4;
895 do {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000896 if (int_part >= 1024) {
897 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
898 int_part /= 1024;
899 ext += 3; /* KiB, MiB, GiB, TiB */
900 }
901 --i;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000902 } while (i);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000903
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000904 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000905}
906
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000907
908#ifdef HAVE_AFINET6
909#define IPV6_ADDR_ANY 0x0000U
910
911#define IPV6_ADDR_UNICAST 0x0001U
912#define IPV6_ADDR_MULTICAST 0x0002U
913#define IPV6_ADDR_ANYCAST 0x0004U
914
915#define IPV6_ADDR_LOOPBACK 0x0010U
916#define IPV6_ADDR_LINKLOCAL 0x0020U
917#define IPV6_ADDR_SITELOCAL 0x0040U
918
919#define IPV6_ADDR_COMPATv4 0x0080U
920
921#define IPV6_ADDR_SCOPE_MASK 0x00f0U
922
923#define IPV6_ADDR_MAPPED 0x1000U
924#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
925
926
927static void ife_print6(struct interface *ptr)
928{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000929 FILE *f;
930 char addr6[40], devname[20];
931 struct sockaddr_in6 sap;
932 int plen, scope, dad_status, if_idx;
933 char addr6p[8][5];
934
935 f = fopen_for_read(_PATH_PROCNET_IFINET6);
936 if (f == NULL)
937 return;
938
939 while (fscanf
940 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
941 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
942 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
943 &dad_status, devname) != EOF
944 ) {
945 if (!strcmp(devname, ptr->name)) {
946 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
947 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
948 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
949 inet_pton(AF_INET6, addr6,
950 (struct sockaddr *) &sap.sin6_addr);
951 sap.sin6_family = AF_INET6;
952 printf(" inet6 addr: %s/%d",
953 INET6_sprint((struct sockaddr *) &sap, 1),
954 plen);
955 printf(" Scope:");
956 switch (scope & IPV6_ADDR_SCOPE_MASK) {
957 case 0:
958 puts("Global");
959 break;
960 case IPV6_ADDR_LINKLOCAL:
961 puts("Link");
962 break;
963 case IPV6_ADDR_SITELOCAL:
964 puts("Site");
965 break;
966 case IPV6_ADDR_COMPATv4:
967 puts("Compat");
968 break;
969 case IPV6_ADDR_LOOPBACK:
970 puts("Host");
971 break;
972 default:
973 puts("Unknown");
974 }
975 }
976 }
977 fclose(f);
978}
979#else
Denis Vlasenko00d84172008-11-24 07:34:42 +0000980#define ife_print6(a) ((void)0)
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000981#endif
982
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000983static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000984{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000985 const struct aftype *ap;
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000986 const struct hwtype *hw;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000987 int hf;
988 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000989
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000990 ap = get_afntype(ptr->addr.sa_family);
991 if (ap == NULL)
992 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000993
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000994 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000995
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000996 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
997 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000998
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000999 hw = get_hwntype(hf);
1000 if (hw == NULL)
1001 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001002
Denys Vlasenkoa85b66e2010-05-23 23:04:15 +02001003 printf("%-9s Link encap:%s ", ptr->name, hw->title);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001004 /* For some hardware types (eg Ash, ATM) we don't print the
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001005 hardware address if it's null. */
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001006 if (hw->print != NULL
1007 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
1008 ) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001009 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001010 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001011#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001012 if (ptr->flags & IFF_PORTSEL) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001013 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001014 if (ptr->flags & IFF_AUTOMEDIA)
Rob Landley4e3aff32006-05-29 04:37:28 +00001015 printf("(auto)");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001016 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001017#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001018 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001019
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001020 if (ptr->has_ip) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001021 printf(" %s addr:%s ", ap->name,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001022 ap->sprint(&ptr->addr, 1));
1023 if (ptr->flags & IFF_POINTOPOINT) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001024 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001025 }
1026 if (ptr->flags & IFF_BROADCAST) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001027 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001028 }
Rob Landley4e3aff32006-05-29 04:37:28 +00001029 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001030 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001031
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001032 ife_print6(ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001033
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001034 printf(" ");
1035 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001036
1037 if (ptr->flags == 0) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001038 printf("[NO FLAGS] ");
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001039 } else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001040 static const char ife_print_flags_strs[] ALIGN1 =
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001041 "UP\0"
1042 "BROADCAST\0"
1043 "DEBUG\0"
1044 "LOOPBACK\0"
1045 "POINTOPOINT\0"
1046 "NOTRAILERS\0"
1047 "RUNNING\0"
1048 "NOARP\0"
1049 "PROMISC\0"
1050 "ALLMULTI\0"
1051 "SLAVE\0"
1052 "MASTER\0"
1053 "MULTICAST\0"
1054#ifdef HAVE_DYNAMIC
1055 "DYNAMIC\0"
1056#endif
1057 ;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001058 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001059 IFF_UP,
1060 IFF_BROADCAST,
1061 IFF_DEBUG,
1062 IFF_LOOPBACK,
1063 IFF_POINTOPOINT,
1064 IFF_NOTRAILERS,
1065 IFF_RUNNING,
1066 IFF_NOARP,
1067 IFF_PROMISC,
1068 IFF_ALLMULTI,
1069 IFF_SLAVE,
1070 IFF_MASTER,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001071 IFF_MULTICAST
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001072#ifdef HAVE_DYNAMIC
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001073 ,IFF_DYNAMIC
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001074#endif
1075 };
1076 const unsigned short *mask = ife_print_flags_mask;
1077 const char *str = ife_print_flags_strs;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001078 do {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001079 if (ptr->flags & *mask) {
1080 printf("%s ", str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001081 }
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001082 mask++;
1083 str += strlen(str) + 1;
1084 } while (*str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001085 }
1086
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001087 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
Rob Landley4e3aff32006-05-29 04:37:28 +00001088 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001089#ifdef SIOCSKEEPALIVE
1090 if (ptr->outfill || ptr->keepalive)
Rob Landley4e3aff32006-05-29 04:37:28 +00001091 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001092#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001093 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001094
1095 /* If needed, display the interface statistics. */
1096
1097 if (ptr->statistics_valid) {
1098 /* XXX: statistics are currently only printed for the primary address,
1099 * not for the aliases, although strictly speaking they're shared
1100 * by all addresses.
1101 */
1102 printf(" ");
1103
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001104 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001105 ptr->stats.rx_packets, ptr->stats.rx_errors,
1106 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1107 ptr->stats.rx_frame_errors);
1108 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001109 printf(" compressed:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001110 ptr->stats.rx_compressed);
1111 printf(" ");
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001112 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001113 ptr->stats.tx_packets, ptr->stats.tx_errors,
1114 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1115 ptr->stats.tx_carrier_errors);
Bernhard Reutner-Fischer214744d2006-03-30 13:38:19 +00001116 printf(" collisions:%lu ", ptr->stats.collisions);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001117 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001118 printf("compressed:%lu ", ptr->stats.tx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001119 if (ptr->tx_queue_len != -1)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001120 printf("txqueuelen:%d ", ptr->tx_queue_len);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001121 printf("\n R");
1122 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1123 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001124 }
1125
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001126 if (ptr->map.irq || ptr->map.mem_start
1127 || ptr->map.dma || ptr->map.base_addr
1128 ) {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001129 printf(" ");
1130 if (ptr->map.irq)
Rob Landley4e3aff32006-05-29 04:37:28 +00001131 printf("Interrupt:%d ", ptr->map.irq);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001132 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001133 I/O maps */
Rob Landley4e3aff32006-05-29 04:37:28 +00001134 printf("Base address:0x%lx ",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001135 (unsigned long) ptr->map.base_addr);
1136 if (ptr->map.mem_start) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001137 printf("Memory:%lx-%lx ", ptr->map.mem_start,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001138 ptr->map.mem_end);
1139 }
1140 if (ptr->map.dma)
Rob Landley4e3aff32006-05-29 04:37:28 +00001141 printf("DMA chan:%x ", ptr->map.dma);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001142 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001143 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001144 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001145}
1146
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001147static int do_if_print(struct interface *ife) /*, int *opt_a)*/
Eric Andersenf15d4da2001-03-06 00:48:59 +00001148{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001149 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001150
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001151 res = do_if_fetch(ife);
1152 if (res >= 0) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001153 if ((ife->flags & IFF_UP) || interface_opt_a)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001154 ife_print(ife);
1155 }
1156 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001157}
1158
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001159static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001160{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001161 struct interface *ife = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001162
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001163 if (if_readlist_proc(name) < 0)
1164 return NULL;
1165 ife = add_interface(name);
1166 return ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001167}
1168
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001169#ifdef UNUSED
1170static int for_all_interfaces(int (*doit) (struct interface *, void *),
1171 void *cookie)
1172{
1173 struct interface *ife;
1174
1175 if (!int_list && (if_readlist() < 0))
1176 return -1;
1177 for (ife = int_list; ife; ife = ife->next) {
1178 int err = doit(ife, cookie);
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001179 if (err)
1180 return err;
1181 }
1182 return 0;
1183}
1184#endif
1185
Eric Andersenf15d4da2001-03-06 00:48:59 +00001186/* for ipv4 add/del modes */
1187static int if_print(char *ifname)
1188{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001189 struct interface *ife;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001190 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001191
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001192 if (!ifname) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001193 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1194 if (!int_list && (if_readlist() < 0))
1195 return -1;
1196 for (ife = int_list; ife; ife = ife->next) {
1197 int err = do_if_print(ife); /*, &interface_opt_a);*/
1198 if (err)
1199 return err;
1200 }
1201 return 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001202 }
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001203 ife = lookup_interface(ifname);
1204 res = do_if_fetch(ife);
1205 if (res >= 0)
1206 ife_print(ife);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001207 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001208}
1209
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001210#if ENABLE_FEATURE_HWIB
1211/* Input an Infiniband address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001212int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001213{
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001214 sap->sa_family = ib_hwtype.type;
Denys Vlasenko48363312010-04-04 15:29:32 +02001215//TODO: error check?
1216 hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN);
1217# ifdef HWIB_DEBUG
1218 fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data));
1219# endif
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001220 return 0;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001221}
1222#endif
1223
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001224int FAST_FUNC display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001225{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001226 int status;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001227
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001228 status = if_print(ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +00001229
1230 return (status < 0); /* status < 0 == 1 -- error */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001231}