blob: e5723b42830511c466fc193c912a1076846abae6 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf15d4da2001-03-06 00:48:59 +00002/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 * Added print_bytes_scaled function to reduce code size.
9 * Added some (potentially) missing defines.
10 * Improved display support for -a and for a named interface.
11 *
12 * -----------------------------------------------------------
13 *
Eric Andersenf15d4da2001-03-06 00:48:59 +000014 * ifconfig This file contains an implementation of the command
15 * that either displays or sets the characteristics of
16 * one or more of the system's networking interfaces.
17 *
Eric Andersenf15d4da2001-03-06 00:48:59 +000018 *
19 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
20 * and others. Copyright 1993 MicroWalt Corporation
21 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020022 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf15d4da2001-03-06 00:48:59 +000023 *
24 * Patched to support 'add' and 'del' keywords for INET(4) addresses
25 * by Mrs. Brisby <mrs.brisby@nimh.org>
26 *
27 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
28 * - gettext instead of catgets for i18n
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029 * 10/1998 - Andi Kleen. Use interface list primitives.
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010030 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
Eric Andersenf15d4da2001-03-06 00:48:59 +000031 * (default AF was wrong)
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000032 */
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{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000092 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000093 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +020094 return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00));
Eric Andersenf15d4da2001-03-06 00:48:59 +000095}
96
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000097#ifdef UNUSED_AND_BUGGY
Denis Vlasenkofa85b862007-01-07 01:24:12 +000098static int INET_getsock(char *bufp, struct sockaddr *sap)
99{
100 char *sp = bufp, *bp;
101 unsigned int i;
102 unsigned val;
103 struct sockaddr_in *sock_in;
104
105 sock_in = (struct sockaddr_in *) sap;
106 sock_in->sin_family = AF_INET;
107 sock_in->sin_port = 0;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000108
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000109 val = 0;
110 bp = (char *) &val;
111 for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
112 *sp = toupper(*sp);
113
114 if ((unsigned)(*sp - 'A') <= 5)
115 bp[i] |= (int) (*sp - ('A' - 10));
116 else if (isdigit(*sp))
117 bp[i] |= (int) (*sp - '0');
118 else
119 return -1;
120
121 bp[i] <<= 4;
122 sp++;
123 *sp = toupper(*sp);
124
125 if ((unsigned)(*sp - 'A') <= 5)
126 bp[i] |= (int) (*sp - ('A' - 10));
127 else if (isdigit(*sp))
128 bp[i] |= (int) (*sp - '0');
129 else
130 return -1;
131
132 sp++;
133 }
134 sock_in->sin_addr.s_addr = htonl(val);
135
136 return (sp - bufp);
137}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000138#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000139
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000140static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000141{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000142 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
143/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000144 switch (type) {
145 case 1:
146 return (INET_getsock(bufp, sap));
147 case 256:
148 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
149 default:
150 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
151 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000152*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000153}
154
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000155static const struct aftype inet_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000156 .name = "inet",
157 .title = "DARPA Internet",
158 .af = AF_INET,
159 .alen = 4,
160 .sprint = INET_sprint,
161 .input = INET_input,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000162};
163
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000164#ifdef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000165
Eric Andersen51b8bd62002-07-03 11:46:38 +0000166/* Display an Internet socket address. */
167/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000168static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000169{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000170 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000171 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200172 return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000173}
174
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000175#ifdef UNUSED
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000176static int INET6_getsock(char *bufp, struct sockaddr *sap)
177{
178 struct sockaddr_in6 *sin6;
179
180 sin6 = (struct sockaddr_in6 *) sap;
181 sin6->sin6_family = AF_INET6;
182 sin6->sin6_port = 0;
183
184 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
185 return -1;
186
187 return 16; /* ?;) */
188}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000189#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000190
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000191static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000192{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000193 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
194/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000195 switch (type) {
196 case 1:
197 return (INET6_getsock(bufp, sap));
198 default:
199 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
200 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000201*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000202}
203
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000204static const struct aftype inet6_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000205 .name = "inet6",
206 .title = "IPv6",
207 .af = AF_INET6,
208 .alen = sizeof(struct in6_addr),
209 .sprint = INET6_sprint,
210 .input = INET6_input,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000211};
212
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000213#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000214
Eric Andersenf15d4da2001-03-06 00:48:59 +0000215/* Display an UNSPEC address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000216static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000217{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200218 char *buff;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000219 char *pos;
220 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000221
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200222 buff = auto_string(xmalloc(sizeof(struct sockaddr) * 3 + 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000223 pos = buff;
224 for (i = 0; i < sizeof(struct sockaddr); i++) {
225 /* careful -- not every libc's sprintf returns # bytes written */
226 sprintf(pos, "%02X-", (*ptr++ & 0377));
227 pos += 3;
228 }
229 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
230 *--pos = '\0';
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000231 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000232}
233
234/* Display an UNSPEC socket address. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000235static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000236{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000237 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000238 return "[NONE SET]";
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000239 return UNSPEC_print((unsigned char *)sap->sa_data);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000240}
241
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000242static const struct aftype unspec_aftype = {
243 .name = "unspec",
244 .title = "UNSPEC",
245 .af = AF_UNSPEC,
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000246 .alen = 0,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000247 .print = UNSPEC_print,
248 .sprint = UNSPEC_sprint,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000249};
250
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000251static const struct aftype *const aftypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000252 &inet_aftype,
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000253#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000254 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000255#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000256 &unspec_aftype,
257 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000258};
259
Eric Andersenf15d4da2001-03-06 00:48:59 +0000260/* Check our protocol family table for this family. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000261const struct aftype* FAST_FUNC get_aftype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000262{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000263 const struct aftype *const *afp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000264
265 afp = aftypes;
266 while (*afp != NULL) {
267 if (!strcmp((*afp)->name, name))
268 return (*afp);
269 afp++;
270 }
271 return NULL;
272}
273
274/* Check our protocol family table for this family. */
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000275static const struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000276{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000277 const struct aftype *const *afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000278
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000279 afp = aftypes;
280 while (*afp != NULL) {
281 if ((*afp)->af == af)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000282 return *afp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000283 afp++;
284 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000285 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000286}
287
Eric Andersenf15d4da2001-03-06 00:48:59 +0000288struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000289 unsigned long long rx_packets; /* total packets received */
290 unsigned long long tx_packets; /* total packets transmitted */
291 unsigned long long rx_bytes; /* total bytes received */
292 unsigned long long tx_bytes; /* total bytes transmitted */
293 unsigned long rx_errors; /* bad packets received */
294 unsigned long tx_errors; /* packet transmit problems */
295 unsigned long rx_dropped; /* no space in linux buffers */
296 unsigned long tx_dropped; /* no space available in linux */
297 unsigned long rx_multicast; /* multicast packets received */
298 unsigned long rx_compressed;
299 unsigned long tx_compressed;
300 unsigned long collisions;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000301
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000302 /* detailed rx_errors: */
303 unsigned long rx_length_errors;
304 unsigned long rx_over_errors; /* receiver ring buff overflow */
305 unsigned long rx_crc_errors; /* recved pkt with crc error */
306 unsigned long rx_frame_errors; /* recv'd frame alignment error */
307 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
308 unsigned long rx_missed_errors; /* receiver missed packet */
309 /* detailed tx_errors */
310 unsigned long tx_aborted_errors;
311 unsigned long tx_carrier_errors;
312 unsigned long tx_fifo_errors;
313 unsigned long tx_heartbeat_errors;
314 unsigned long tx_window_errors;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000315};
316
317struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000318 struct interface *next, *prev;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000319 char name[IFNAMSIZ]; /* interface name */
320 short type; /* if type */
321 short flags; /* various flags */
322 int metric; /* routing metric */
323 int mtu; /* MTU value */
324 int tx_queue_len; /* transmit queue length */
325 struct ifmap map; /* hardware setup */
326 struct sockaddr addr; /* IP address */
327 struct sockaddr dstaddr; /* P-P IP address */
328 struct sockaddr broadaddr; /* IP broadcast address */
329 struct sockaddr netmask; /* IP network mask */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000330 int has_ip;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000331 char hwaddr[32]; /* HW address */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000332 int statistics_valid;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000333 struct user_net_device_stats stats; /* statistics */
334 int keepalive; /* keepalive value for SLIP */
335 int outfill; /* outfill value for SLIP */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000336};
337
338
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000339smallint interface_opt_a; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000340
Eric Andersenf15d4da2001-03-06 00:48:59 +0000341static struct interface *int_list, *int_last;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000342
343
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000344#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000345/* like strcmp(), but knows about numbers */
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000346except that the freshly added calls to xatoul() brf on ethernet aliases with
347uClibc with e.g.: ife->name='lo' name='eth0:1'
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000348static int nstrcmp(const char *a, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000349{
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000350 const char *a_ptr = a;
351 const char *b_ptr = b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000352
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000353 while (*a == *b) {
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000354 if (*a == '\0') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000355 return 0;
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000356 }
357 if (!isdigit(*a) && isdigit(*(a+1))) {
358 a_ptr = a+1;
359 b_ptr = b+1;
360 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000361 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000362 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000363 }
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000364
365 if (isdigit(*a) && isdigit(*b)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000366 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000367 }
368 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000369}
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000370#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000371
372static struct interface *add_interface(char *name)
373{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000374 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000375
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000376 for (ife = int_last; ife; ife = ife->prev) {
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000377 int n = /*n*/strcmp(ife->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000378
379 if (n == 0)
380 return ife;
381 if (n < 0)
382 break;
383 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000384
385 new = xzalloc(sizeof(*new));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000386 strncpy_IFNAMSIZ(new->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000387 nextp = ife ? &ife->next : &int_list;
388 new->prev = ife;
389 new->next = *nextp;
390 if (new->next)
391 new->next->prev = new;
392 else
393 int_last = new;
394 *nextp = new;
395 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000396}
397
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000398static char *get_name(char *name, char *p)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000399{
Rob Landley9fe801e2006-06-20 21:13:29 +0000400 /* Extract <name> from nul-terminated p where p matches
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200401 * <name>: after leading whitespace.
402 * If match is not made, set name empty and return unchanged p
403 */
404 char *nameend;
405 char *namestart = skip_whitespace(p);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000406
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000407 nameend = namestart;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200408 while (*nameend && *nameend != ':' && !isspace(*nameend))
Eric Andersen9940e082004-08-12 16:52:00 +0000409 nameend++;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200410 if (*nameend == ':') {
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000411 if ((nameend - namestart) < IFNAMSIZ) {
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200412 memcpy(name, namestart, nameend - namestart);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000413 name[nameend - namestart] = '\0';
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200414 p = nameend;
Eric Andersen9940e082004-08-12 16:52:00 +0000415 } else {
416 /* Interface name too large */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000417 name[0] = '\0';
Eric Andersen9940e082004-08-12 16:52:00 +0000418 }
419 } else {
Rob Landley9fe801e2006-06-20 21:13:29 +0000420 /* trailing ':' not found - return empty */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000421 name[0] = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000422 }
Eric Andersen6fea7322004-08-26 21:45:21 +0000423 return p + 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000424}
425
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000426/* If scanf supports size qualifiers for %n conversions, then we can
427 * use a modified fmt that simply stores the position in the fields
428 * having no associated fields in the proc string. Of course, we need
429 * to zero them again when we're done. But that is smaller than the
430 * old approach of multiple scanf occurrences with large numbers of
431 * args. */
432
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000433/* static const char *const ss_fmt[] = { */
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000434/* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
435/* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
436/* "%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 +0000437/* }; */
438
439 /* Lie about the size of the int pointed to for %n. */
440#if INT_MAX == LONG_MAX
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000441static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000442 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
443 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
444 "%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 +0000445};
446#else
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000447static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000448 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
449 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
450 "%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 +0000451};
452
453#endif
454
455static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000456{
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000457 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
458
459 sscanf(bp, ss_fmt[procnetdev_vsn],
460 &ife->stats.rx_bytes, /* missing for 0 */
461 &ife->stats.rx_packets,
462 &ife->stats.rx_errors,
463 &ife->stats.rx_dropped,
464 &ife->stats.rx_fifo_errors,
465 &ife->stats.rx_frame_errors,
466 &ife->stats.rx_compressed, /* missing for <= 1 */
467 &ife->stats.rx_multicast, /* missing for <= 1 */
468 &ife->stats.tx_bytes, /* missing for 0 */
469 &ife->stats.tx_packets,
470 &ife->stats.tx_errors,
471 &ife->stats.tx_dropped,
472 &ife->stats.tx_fifo_errors,
473 &ife->stats.collisions,
474 &ife->stats.tx_carrier_errors,
475 &ife->stats.tx_compressed /* missing for <= 1 */
476 );
477
478 if (procnetdev_vsn <= 1) {
479 if (procnetdev_vsn == 0) {
480 ife->stats.rx_bytes = 0;
481 ife->stats.tx_bytes = 0;
482 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000483 ife->stats.rx_multicast = 0;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000484 ife->stats.rx_compressed = 0;
485 ife->stats.tx_compressed = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000486 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000487}
488
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000489static int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000490{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000491 if (strstr(buf, "compressed"))
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000492 return 2;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000493 if (strstr(buf, "bytes"))
494 return 1;
495 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000496}
497
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000498static int if_readconf(void)
499{
500 int numreqs = 30;
501 struct ifconf ifc;
502 struct ifreq *ifr;
503 int n, err = -1;
504 int skfd;
505
506 ifc.ifc_buf = NULL;
507
508 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
509 (as of 2.1.128) */
510 skfd = socket(AF_INET, SOCK_DGRAM, 0);
511 if (skfd < 0) {
512 bb_perror_msg("error: no inet socket available");
513 return -1;
514 }
515
516 for (;;) {
517 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
518 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
519
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000520 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000521 goto out;
522 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000523 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000524 /* assume it overflowed and try again */
525 numreqs += 10;
526 continue;
527 }
528 break;
529 }
530
531 ifr = ifc.ifc_req;
532 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
533 add_interface(ifr->ifr_name);
534 ifr++;
535 }
536 err = 0;
537
538 out:
539 close(skfd);
540 free(ifc.ifc_buf);
541 return err;
542}
543
Eric Andersenf15d4da2001-03-06 00:48:59 +0000544static int if_readlist_proc(char *target)
545{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000546 static smallint proc_read;
547
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000548 FILE *fh;
549 char buf[512];
550 struct interface *ife;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000551 int err, procnetdev_vsn;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000552
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000553 if (proc_read)
554 return 0;
555 if (!target)
556 proc_read = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000557
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000558 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000559 if (!fh) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000560 return if_readconf();
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000561 }
562 fgets(buf, sizeof buf, fh); /* eat line */
563 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000564
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000565 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000566
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000567 err = 0;
568 while (fgets(buf, sizeof buf, fh)) {
Eric Andersenf96675b2003-07-28 06:37:04 +0000569 char *s, name[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000570
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000571 s = get_name(name, buf);
572 ife = add_interface(name);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000573 get_dev_fields(s, ife, procnetdev_vsn);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000574 ife->statistics_valid = 1;
575 if (target && !strcmp(target, name))
576 break;
577 }
578 if (ferror(fh)) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000579 bb_perror_msg(_PATH_PROCNET_DEV);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000580 err = -1;
581 proc_read = 0;
582 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000583 fclose(fh);
584 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000585}
586
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000587static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000588{
Denis Vlasenkod0587ed2007-03-22 19:35:51 +0000589 int err = if_readlist_proc(NULL);
590 /* Needed in order to get ethN:M aliases */
591 if (!err)
592 err = if_readconf();
593 return err;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000594}
595
Eric Andersenf15d4da2001-03-06 00:48:59 +0000596/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000597static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000598{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000599 struct ifreq ifr;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000600 char *ifname = ife->name;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000601 int skfd;
602
603 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000604
Denis Vlasenko360d9662008-12-02 18:18:50 +0000605 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000606 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
607 close(skfd);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000608 return -1;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000609 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000610 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000611
Denis Vlasenko360d9662008-12-02 18:18:50 +0000612 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000613 memset(ife->hwaddr, 0, 32);
614 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000615 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000616
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000617 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000618
Denis Vlasenko360d9662008-12-02 18:18:50 +0000619 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000620 ife->metric = 0;
621 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000622 ife->metric = ifr.ifr_metric;
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 ife->mtu = 0;
626 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000627 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000628
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000629 memset(&ife->map, 0, sizeof(struct ifmap));
Rob Landley93983042005-05-03 21:30:26 +0000630#ifdef SIOCGIFMAP
Denis Vlasenko360d9662008-12-02 18:18:50 +0000631 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Rob Landley93983042005-05-03 21:30:26 +0000632 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000633 ife->map = ifr.ifr_map;
Rob Landley93983042005-05-03 21:30:26 +0000634#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000635
636#ifdef HAVE_TXQUEUELEN
Denis Vlasenko360d9662008-12-02 18:18:50 +0000637 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000638 ife->tx_queue_len = -1; /* unknown value */
639 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000640 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000641#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000642 ife->tx_queue_len = -1; /* unknown value */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000643#endif
644
Denis Vlasenko360d9662008-12-02 18:18:50 +0000645 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000646 ifr.ifr_addr.sa_family = AF_INET;
647 memset(&ife->addr, 0, sizeof(struct sockaddr));
648 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
649 ife->has_ip = 1;
650 ife->addr = ifr.ifr_addr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000651 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000652 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
653 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
654 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000655
Denis Vlasenko360d9662008-12-02 18:18:50 +0000656 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000657 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
658 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
659 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000660
Denis Vlasenko360d9662008-12-02 18:18:50 +0000661 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000662 memset(&ife->netmask, 0, sizeof(struct sockaddr));
663 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
664 ife->netmask = ifr.ifr_netmask;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000665 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000666
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000667 close(skfd);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000668 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000669}
670
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000671static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000672{
673 if (if_fetch(ife) < 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000674 const char *errmsg;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000675
676 if (errno == ENODEV) {
677 /* Give better error message for this case. */
Rob Landley4e3aff32006-05-29 04:37:28 +0000678 errmsg = "Device not found";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000679 } else {
680 errmsg = strerror(errno);
681 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000682 bb_error_msg("%s: error fetching interface information: %s",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000683 ife->name, errmsg);
684 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000685 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000686 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000687}
688
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000689static const struct hwtype unspec_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000690 .name = "unspec",
691 .title = "UNSPEC",
692 .type = -1,
693 .print = UNSPEC_print
Eric Andersenf15d4da2001-03-06 00:48:59 +0000694};
695
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000696static const struct hwtype loop_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000697 .name = "loop",
698 .title = "Local Loopback",
699 .type = ARPHRD_LOOPBACK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000700};
701
Eric Andersenf15d4da2001-03-06 00:48:59 +0000702/* Display an Ethernet address in readable format. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000703static char* FAST_FUNC ether_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000704{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200705 char *buff;
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000706 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000707 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
708 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
709 );
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200710 return auto_string(buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000711}
712
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000713static const struct hwtype ether_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000714 .name = "ether",
715 .title = "Ethernet",
716 .type = ARPHRD_ETHER,
717 .alen = ETH_ALEN,
718 .print = ether_print,
Bartosz Golaszewskic19be752013-07-25 04:39:04 +0200719 .input = in_ether
Eric Andersenf15d4da2001-03-06 00:48:59 +0000720};
721
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000722static const struct hwtype ppp_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000723 .name = "ppp",
724 .title = "Point-to-Point Protocol",
725 .type = ARPHRD_PPP
Eric Andersenf15d4da2001-03-06 00:48:59 +0000726};
727
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000728#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000729static const struct hwtype sit_hwtype = {
730 .name = "sit",
731 .title = "IPv6-in-IPv4",
732 .type = ARPHRD_SIT,
733 .print = UNSPEC_print,
734 .suppress_null_addr = 1
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000735};
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000736#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000737#if ENABLE_FEATURE_HWIB
738static const struct hwtype ib_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000739 .name = "infiniband",
740 .title = "InfiniBand",
741 .type = ARPHRD_INFINIBAND,
742 .alen = INFINIBAND_ALEN,
743 .print = UNSPEC_print,
744 .input = in_ib,
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000745};
746#endif
747
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000748
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000749static const struct hwtype *const hwtypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000750 &loop_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000751 &ether_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000752 &ppp_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000753 &unspec_hwtype,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000754#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000755 &sit_hwtype,
756#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000757#if ENABLE_FEATURE_HWIB
758 &ib_hwtype,
759#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000760 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000761};
762
Eric Andersenf15d4da2001-03-06 00:48:59 +0000763#ifdef IFF_PORTSEL
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000764static const char *const if_port_text[] = {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000765 /* Keep in step with <linux/netdevice.h> */
766 "unknown",
767 "10base2",
768 "10baseT",
769 "AUI",
770 "100baseT",
771 "100baseTX",
772 "100baseFX",
773 NULL
774};
775#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000776
777/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000778const struct hwtype* FAST_FUNC get_hwtype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000779{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000780 const struct hwtype *const *hwp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000781
782 hwp = hwtypes;
783 while (*hwp != NULL) {
784 if (!strcmp((*hwp)->name, name))
785 return (*hwp);
786 hwp++;
787 }
788 return NULL;
789}
790
791/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000792const struct hwtype* FAST_FUNC get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000793{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000794 const struct hwtype *const *hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000795
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000796 hwp = hwtypes;
797 while (*hwp != NULL) {
798 if ((*hwp)->type == type)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000799 return *hwp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000800 hwp++;
801 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000802 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000803}
804
805/* return 1 if address is all zeros */
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000806static int hw_null_address(const struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000807{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000808 int i;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000809 unsigned char *address = (unsigned char *) ap;
810
811 for (i = 0; i < hw->alen; i++)
812 if (address[i])
813 return 0;
814 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000815}
816
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000817static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000818
819static void print_bytes_scaled(unsigned long long ull, const char *end)
820{
821 unsigned long long int_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000822 const char *ext;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000823 unsigned int frac_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000824 int i;
825
826 frac_part = 0;
827 ext = TRext;
828 int_part = ull;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000829 i = 4;
830 do {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000831 if (int_part >= 1024) {
832 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
833 int_part /= 1024;
834 ext += 3; /* KiB, MiB, GiB, TiB */
835 }
836 --i;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000837 } while (i);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000838
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000839 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000840}
841
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000842
843#ifdef HAVE_AFINET6
844#define IPV6_ADDR_ANY 0x0000U
845
846#define IPV6_ADDR_UNICAST 0x0001U
847#define IPV6_ADDR_MULTICAST 0x0002U
848#define IPV6_ADDR_ANYCAST 0x0004U
849
850#define IPV6_ADDR_LOOPBACK 0x0010U
851#define IPV6_ADDR_LINKLOCAL 0x0020U
852#define IPV6_ADDR_SITELOCAL 0x0040U
853
854#define IPV6_ADDR_COMPATv4 0x0080U
855
856#define IPV6_ADDR_SCOPE_MASK 0x00f0U
857
858#define IPV6_ADDR_MAPPED 0x1000U
859#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
860
861
862static void ife_print6(struct interface *ptr)
863{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000864 FILE *f;
Nicolas Dichtel6761d7d2013-08-12 17:09:59 +0200865 char addr6[40], devname[21];
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000866 struct sockaddr_in6 sap;
867 int plen, scope, dad_status, if_idx;
868 char addr6p[8][5];
869
870 f = fopen_for_read(_PATH_PROCNET_IFINET6);
871 if (f == NULL)
872 return;
873
874 while (fscanf
875 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
876 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
877 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
878 &dad_status, devname) != EOF
879 ) {
880 if (!strcmp(devname, ptr->name)) {
881 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
882 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
883 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
Felix Fietkauf2c043a2016-01-18 12:07:35 +0100884 memset(&sap, 0, sizeof(sap));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000885 inet_pton(AF_INET6, addr6,
886 (struct sockaddr *) &sap.sin6_addr);
887 sap.sin6_family = AF_INET6;
888 printf(" inet6 addr: %s/%d",
Denys Vlasenko69675782013-01-14 01:34:48 +0100889 INET6_sprint((struct sockaddr *) &sap, 1),
890 plen);
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000891 printf(" Scope:");
892 switch (scope & IPV6_ADDR_SCOPE_MASK) {
893 case 0:
894 puts("Global");
895 break;
896 case IPV6_ADDR_LINKLOCAL:
897 puts("Link");
898 break;
899 case IPV6_ADDR_SITELOCAL:
900 puts("Site");
901 break;
902 case IPV6_ADDR_COMPATv4:
903 puts("Compat");
904 break;
905 case IPV6_ADDR_LOOPBACK:
906 puts("Host");
907 break;
908 default:
909 puts("Unknown");
910 }
911 }
912 }
913 fclose(f);
914}
915#else
Denis Vlasenko00d84172008-11-24 07:34:42 +0000916#define ife_print6(a) ((void)0)
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000917#endif
918
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000919static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000920{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000921 const struct aftype *ap;
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000922 const struct hwtype *hw;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000923 int hf;
924 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000925
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000926 ap = get_afntype(ptr->addr.sa_family);
927 if (ap == NULL)
928 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000929
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000930 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000931
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000932 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
933 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000934
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000935 hw = get_hwntype(hf);
936 if (hw == NULL)
937 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000938
Denys Vlasenkoa85b66e2010-05-23 23:04:15 +0200939 printf("%-9s Link encap:%s ", ptr->name, hw->title);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000940 /* For some hardware types (eg Ash, ATM) we don't print the
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000941 hardware address if it's null. */
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000942 if (hw->print != NULL
943 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
944 ) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000945 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000946 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000947#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000948 if (ptr->flags & IFF_PORTSEL) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000949 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000950 if (ptr->flags & IFF_AUTOMEDIA)
Rob Landley4e3aff32006-05-29 04:37:28 +0000951 printf("(auto)");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000952 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000953#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000954 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000955
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000956 if (ptr->has_ip) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000957 printf(" %s addr:%s ", ap->name,
Denys Vlasenko69675782013-01-14 01:34:48 +0100958 ap->sprint(&ptr->addr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000959 if (ptr->flags & IFF_POINTOPOINT) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000960 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000961 }
962 if (ptr->flags & IFF_BROADCAST) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000963 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000964 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000965 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000966 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000967
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000968 ife_print6(ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000969
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000970 printf(" ");
971 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000972
973 if (ptr->flags == 0) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000974 printf("[NO FLAGS] ");
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000975 } else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000976 static const char ife_print_flags_strs[] ALIGN1 =
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000977 "UP\0"
978 "BROADCAST\0"
979 "DEBUG\0"
980 "LOOPBACK\0"
981 "POINTOPOINT\0"
982 "NOTRAILERS\0"
983 "RUNNING\0"
984 "NOARP\0"
985 "PROMISC\0"
986 "ALLMULTI\0"
987 "SLAVE\0"
988 "MASTER\0"
989 "MULTICAST\0"
990#ifdef HAVE_DYNAMIC
991 "DYNAMIC\0"
992#endif
993 ;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000994 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000995 IFF_UP,
996 IFF_BROADCAST,
997 IFF_DEBUG,
998 IFF_LOOPBACK,
999 IFF_POINTOPOINT,
1000 IFF_NOTRAILERS,
1001 IFF_RUNNING,
1002 IFF_NOARP,
1003 IFF_PROMISC,
1004 IFF_ALLMULTI,
1005 IFF_SLAVE,
1006 IFF_MASTER,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001007 IFF_MULTICAST
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001008#ifdef HAVE_DYNAMIC
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001009 ,IFF_DYNAMIC
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001010#endif
1011 };
1012 const unsigned short *mask = ife_print_flags_mask;
1013 const char *str = ife_print_flags_strs;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001014 do {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001015 if (ptr->flags & *mask) {
1016 printf("%s ", str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001017 }
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001018 mask++;
1019 str += strlen(str) + 1;
1020 } while (*str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001021 }
1022
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001023 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
Rob Landley4e3aff32006-05-29 04:37:28 +00001024 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001025#ifdef SIOCSKEEPALIVE
1026 if (ptr->outfill || ptr->keepalive)
Rob Landley4e3aff32006-05-29 04:37:28 +00001027 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001028#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001029 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001030
1031 /* If needed, display the interface statistics. */
1032
1033 if (ptr->statistics_valid) {
1034 /* XXX: statistics are currently only printed for the primary address,
1035 * not for the aliases, although strictly speaking they're shared
1036 * by all addresses.
1037 */
1038 printf(" ");
1039
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001040 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001041 ptr->stats.rx_packets, ptr->stats.rx_errors,
1042 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1043 ptr->stats.rx_frame_errors);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001044 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001045 printf(" compressed:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001046 ptr->stats.rx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001047 printf(" ");
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001048 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001049 ptr->stats.tx_packets, ptr->stats.tx_errors,
1050 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1051 ptr->stats.tx_carrier_errors);
Bernhard Reutner-Fischer214744d2006-03-30 13:38:19 +00001052 printf(" collisions:%lu ", ptr->stats.collisions);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001053 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001054 printf("compressed:%lu ", ptr->stats.tx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001055 if (ptr->tx_queue_len != -1)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001056 printf("txqueuelen:%d ", ptr->tx_queue_len);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001057 printf("\n R");
1058 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1059 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001060 }
1061
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001062 if (ptr->map.irq || ptr->map.mem_start
1063 || ptr->map.dma || ptr->map.base_addr
1064 ) {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001065 printf(" ");
1066 if (ptr->map.irq)
Rob Landley4e3aff32006-05-29 04:37:28 +00001067 printf("Interrupt:%d ", ptr->map.irq);
Denys Vlasenko69675782013-01-14 01:34:48 +01001068 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */
Rob Landley4e3aff32006-05-29 04:37:28 +00001069 printf("Base address:0x%lx ",
Denys Vlasenko69675782013-01-14 01:34:48 +01001070 (unsigned long) ptr->map.base_addr);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001071 if (ptr->map.mem_start) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001072 printf("Memory:%lx-%lx ", ptr->map.mem_start,
Denys Vlasenko69675782013-01-14 01:34:48 +01001073 ptr->map.mem_end);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001074 }
1075 if (ptr->map.dma)
Rob Landley4e3aff32006-05-29 04:37:28 +00001076 printf("DMA chan:%x ", ptr->map.dma);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001077 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001078 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001079 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001080}
1081
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001082static int do_if_print(struct interface *ife) /*, int *opt_a)*/
Eric Andersenf15d4da2001-03-06 00:48:59 +00001083{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001084 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001085
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001086 res = do_if_fetch(ife);
1087 if (res >= 0) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001088 if ((ife->flags & IFF_UP) || interface_opt_a)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001089 ife_print(ife);
1090 }
1091 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001092}
1093
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001094static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001095{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001096 struct interface *ife = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001097
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001098 if (if_readlist_proc(name) < 0)
1099 return NULL;
1100 ife = add_interface(name);
1101 return ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001102}
1103
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001104#ifdef UNUSED
1105static int for_all_interfaces(int (*doit) (struct interface *, void *),
Denys Vlasenko69675782013-01-14 01:34:48 +01001106 void *cookie)
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001107{
1108 struct interface *ife;
1109
1110 if (!int_list && (if_readlist() < 0))
1111 return -1;
1112 for (ife = int_list; ife; ife = ife->next) {
1113 int err = doit(ife, cookie);
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001114 if (err)
1115 return err;
1116 }
1117 return 0;
1118}
1119#endif
1120
Eric Andersenf15d4da2001-03-06 00:48:59 +00001121/* for ipv4 add/del modes */
1122static int if_print(char *ifname)
1123{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001124 struct interface *ife;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001125 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001126
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001127 if (!ifname) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001128 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1129 if (!int_list && (if_readlist() < 0))
1130 return -1;
1131 for (ife = int_list; ife; ife = ife->next) {
1132 int err = do_if_print(ife); /*, &interface_opt_a);*/
1133 if (err)
1134 return err;
1135 }
1136 return 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001137 }
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001138 ife = lookup_interface(ifname);
1139 res = do_if_fetch(ife);
1140 if (res >= 0)
1141 ife_print(ife);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001142 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001143}
1144
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001145#if ENABLE_FEATURE_HWIB
1146/* Input an Infiniband address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001147int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001148{
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001149 sap->sa_family = ib_hwtype.type;
Denys Vlasenko48363312010-04-04 15:29:32 +02001150//TODO: error check?
1151 hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN);
1152# ifdef HWIB_DEBUG
1153 fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data));
1154# endif
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001155 return 0;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001156}
1157#endif
1158
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001159int FAST_FUNC display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001160{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001161 int status;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001162
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001163 status = if_print(ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +00001164
1165 return (status < 0); /* status < 0 == 1 -- error */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001166}