blob: 89427f2f4ae770a61c571e077b8cd2b2465dd028 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf15d4da2001-03-06 00:48:59 +00002/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 * Added print_bytes_scaled function to reduce code size.
9 * Added some (potentially) missing defines.
10 * Improved display support for -a and for a named interface.
11 *
12 * -----------------------------------------------------------
13 *
Eric Andersenf15d4da2001-03-06 00:48:59 +000014 * ifconfig This file contains an implementation of the command
15 * that either displays or sets the characteristics of
16 * one or more of the system's networking interfaces.
17 *
Eric Andersenf15d4da2001-03-06 00:48:59 +000018 *
19 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
20 * and others. Copyright 1993 MicroWalt Corporation
21 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020022 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf15d4da2001-03-06 00:48:59 +000023 *
24 * Patched to support 'add' and 'del' keywords for INET(4) addresses
25 * by Mrs. Brisby <mrs.brisby@nimh.org>
26 *
27 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
28 * - gettext instead of catgets for i18n
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029 * 10/1998 - Andi Kleen. Use interface list primitives.
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010030 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
Eric Andersenf15d4da2001-03-06 00:48:59 +000031 * (default AF was wrong)
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000032 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Denys Vlasenkobb1dcc92010-02-02 12:45:38 +010034#include "inet_common.h"
Dan Fandrichf533ec82011-06-10 05:17:59 +020035#include <net/if.h>
36#include <net/if_arp.h>
37#ifdef HAVE_NET_ETHERNET_H
38# include <net/ethernet.h>
39#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000040
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000041#if ENABLE_FEATURE_HWIB
42/* #include <linux/if_infiniband.h> */
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020043# undef INFINIBAND_ALEN
44# define INFINIBAND_ALEN 20
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +000045#endif
46
Denis Vlasenko1cc70222007-03-15 19:46:43 +000047#if ENABLE_FEATURE_IPV6
Glenn L McGrathb54a7482003-08-29 11:34:08 +000048# define HAVE_AFINET6 1
49#else
50# undef HAVE_AFINET6
51#endif
52
Eric Andersenf15d4da2001-03-06 00:48:59 +000053#define _PATH_PROCNET_DEV "/proc/net/dev"
Eric Andersen51b8bd62002-07-03 11:46:38 +000054#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
Eric Andersenf15d4da2001-03-06 00:48:59 +000055
Denis Vlasenkoaad49992006-11-22 02:12:07 +000056#ifdef HAVE_AFINET6
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020057# ifndef _LINUX_IN6_H
Eric Andersenf15d4da2001-03-06 00:48:59 +000058/*
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020059 * This is from linux/include/net/ipv6.h
Eric Andersenf15d4da2001-03-06 00:48:59 +000060 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000061struct in6_ifreq {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000062 struct in6_addr ifr6_addr;
Glenn L McGrathb54a7482003-08-29 11:34:08 +000063 uint32_t ifr6_prefixlen;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000064 unsigned int ifr6_ifindex;
Eric Andersenf15d4da2001-03-06 00:48:59 +000065};
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020066# endif
Denis Vlasenkoaad49992006-11-22 02:12:07 +000067#endif /* HAVE_AFINET6 */
Eric Andersenf15d4da2001-03-06 00:48:59 +000068
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000069/* Defines for glibc2.0 users. */
70#ifndef SIOCSIFTXQLEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020071# define SIOCSIFTXQLEN 0x8943
72# define SIOCGIFTXQLEN 0x8942
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000073#endif
74
75/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
76#ifndef ifr_qlen
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020077# define ifr_qlen ifr_ifru.ifru_mtu
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000078#endif
79
80#ifndef HAVE_TXQUEUELEN
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020081# define HAVE_TXQUEUELEN 1
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000082#endif
83
84#ifndef IFF_DYNAMIC
Denys Vlasenko5342c3f2010-04-01 15:09:44 +020085# define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000086#endif
87
Eric Andersenf15d4da2001-03-06 00:48:59 +000088/* Display an Internet socket address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000089static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
Eric Andersenf15d4da2001-03-06 00:48:59 +000090{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +000091 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000092 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +020093 return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00));
Eric Andersenf15d4da2001-03-06 00:48:59 +000094}
95
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000096#ifdef UNUSED_AND_BUGGY
Denis Vlasenkofa85b862007-01-07 01:24:12 +000097static int INET_getsock(char *bufp, struct sockaddr *sap)
98{
99 char *sp = bufp, *bp;
100 unsigned int i;
101 unsigned val;
102 struct sockaddr_in *sock_in;
103
104 sock_in = (struct sockaddr_in *) sap;
105 sock_in->sin_family = AF_INET;
106 sock_in->sin_port = 0;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000107
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000108 val = 0;
109 bp = (char *) &val;
110 for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
111 *sp = toupper(*sp);
112
113 if ((unsigned)(*sp - 'A') <= 5)
114 bp[i] |= (int) (*sp - ('A' - 10));
115 else if (isdigit(*sp))
116 bp[i] |= (int) (*sp - '0');
117 else
118 return -1;
119
120 bp[i] <<= 4;
121 sp++;
122 *sp = toupper(*sp);
123
124 if ((unsigned)(*sp - 'A') <= 5)
125 bp[i] |= (int) (*sp - ('A' - 10));
126 else if (isdigit(*sp))
127 bp[i] |= (int) (*sp - '0');
128 else
129 return -1;
130
131 sp++;
132 }
133 sock_in->sin_addr.s_addr = htonl(val);
134
135 return (sp - bufp);
136}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000137#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000138
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000139static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000140{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000141 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
142/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000143 switch (type) {
144 case 1:
145 return (INET_getsock(bufp, sap));
146 case 256:
147 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
148 default:
149 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
150 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000151*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000152}
153
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000154static const struct aftype inet_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000155 .name = "inet",
156 .title = "DARPA Internet",
157 .af = AF_INET,
158 .alen = 4,
159 .sprint = INET_sprint,
160 .input = INET_input,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000161};
162
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000163#ifdef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000164
Eric Andersen51b8bd62002-07-03 11:46:38 +0000165/* Display an Internet socket address. */
166/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000167static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000168{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000169 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000170 return "[NONE SET]";
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200171 return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000172}
173
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000174#ifdef UNUSED
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000175static int INET6_getsock(char *bufp, struct sockaddr *sap)
176{
177 struct sockaddr_in6 *sin6;
178
179 sin6 = (struct sockaddr_in6 *) sap;
180 sin6->sin6_family = AF_INET6;
181 sin6->sin6_port = 0;
182
183 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
184 return -1;
185
186 return 16; /* ?;) */
187}
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000188#endif
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000189
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000190static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000191{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000192 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
193/*
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000194 switch (type) {
195 case 1:
196 return (INET6_getsock(bufp, sap));
197 default:
198 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
199 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000200*/
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000201}
202
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000203static const struct aftype inet6_aftype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000204 .name = "inet6",
205 .title = "IPv6",
206 .af = AF_INET6,
207 .alen = sizeof(struct in6_addr),
208 .sprint = INET6_sprint,
209 .input = INET6_input,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000210};
211
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000212#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000213
Eric Andersenf15d4da2001-03-06 00:48:59 +0000214/* Display an UNSPEC address. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000215static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000216{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200217 char *buff;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000218 char *pos;
219 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000220
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200221 buff = auto_string(xmalloc(sizeof(struct sockaddr) * 3 + 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000222 pos = buff;
223 for (i = 0; i < sizeof(struct sockaddr); i++) {
224 /* careful -- not every libc's sprintf returns # bytes written */
Denys Vlasenkob5a03962017-09-12 17:54:28 +0200225 sprintf(pos, "%02X-", *ptr++);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000226 pos += 3;
227 }
228 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
229 *--pos = '\0';
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000230 return buff;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000231}
232
233/* Display an UNSPEC socket address. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000234static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000235{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000236 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000237 return "[NONE SET]";
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000238 return UNSPEC_print((unsigned char *)sap->sa_data);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000239}
240
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000241static const struct aftype unspec_aftype = {
242 .name = "unspec",
243 .title = "UNSPEC",
244 .af = AF_UNSPEC,
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000245 .alen = 0,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000246 .print = UNSPEC_print,
247 .sprint = UNSPEC_sprint,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000248};
249
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000250static const struct aftype *const aftypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000251 &inet_aftype,
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000252#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000253 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000254#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000255 &unspec_aftype,
256 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000257};
258
Eric Andersenf15d4da2001-03-06 00:48:59 +0000259/* Check our protocol family table for this family. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000260const struct aftype* FAST_FUNC get_aftype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000261{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000262 const struct aftype *const *afp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000263
264 afp = aftypes;
265 while (*afp != NULL) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100266 if (strcmp((*afp)->name, name) == 0)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000267 return (*afp);
268 afp++;
269 }
270 return NULL;
271}
272
273/* Check our protocol family table for this family. */
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000274static const struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000275{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000276 const struct aftype *const *afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000277
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000278 afp = aftypes;
279 while (*afp != NULL) {
280 if ((*afp)->af == af)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000281 return *afp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000282 afp++;
283 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000284 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000285}
286
Eric Andersenf15d4da2001-03-06 00:48:59 +0000287struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000288 unsigned long long rx_packets; /* total packets received */
289 unsigned long long tx_packets; /* total packets transmitted */
290 unsigned long long rx_bytes; /* total bytes received */
291 unsigned long long tx_bytes; /* total bytes transmitted */
292 unsigned long rx_errors; /* bad packets received */
293 unsigned long tx_errors; /* packet transmit problems */
294 unsigned long rx_dropped; /* no space in linux buffers */
295 unsigned long tx_dropped; /* no space available in linux */
296 unsigned long rx_multicast; /* multicast packets received */
297 unsigned long rx_compressed;
298 unsigned long tx_compressed;
299 unsigned long collisions;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000300
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000301 /* detailed rx_errors: */
302 unsigned long rx_length_errors;
303 unsigned long rx_over_errors; /* receiver ring buff overflow */
304 unsigned long rx_crc_errors; /* recved pkt with crc error */
305 unsigned long rx_frame_errors; /* recv'd frame alignment error */
306 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
307 unsigned long rx_missed_errors; /* receiver missed packet */
308 /* detailed tx_errors */
309 unsigned long tx_aborted_errors;
310 unsigned long tx_carrier_errors;
311 unsigned long tx_fifo_errors;
312 unsigned long tx_heartbeat_errors;
313 unsigned long tx_window_errors;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000314};
315
316struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000317 struct interface *next, *prev;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000318 char name[IFNAMSIZ]; /* interface name */
319 short type; /* if type */
320 short flags; /* various flags */
321 int metric; /* routing metric */
322 int mtu; /* MTU value */
323 int tx_queue_len; /* transmit queue length */
324 struct ifmap map; /* hardware setup */
325 struct sockaddr addr; /* IP address */
326 struct sockaddr dstaddr; /* P-P IP address */
327 struct sockaddr broadaddr; /* IP broadcast address */
328 struct sockaddr netmask; /* IP network mask */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000329 int has_ip;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000330 char hwaddr[32]; /* HW address */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000331 int statistics_valid;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000332 struct user_net_device_stats stats; /* statistics */
333 int keepalive; /* keepalive value for SLIP */
334 int outfill; /* outfill value for SLIP */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000335};
336
337
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000338smallint interface_opt_a; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000339
Eric Andersenf15d4da2001-03-06 00:48:59 +0000340static struct interface *int_list, *int_last;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000341
342
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000343#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000344/* like strcmp(), but knows about numbers */
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000345except that the freshly added calls to xatoul() brf on ethernet aliases with
346uClibc with e.g.: ife->name='lo' name='eth0:1'
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000347static int nstrcmp(const char *a, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000348{
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000349 const char *a_ptr = a;
350 const char *b_ptr = b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000351
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000352 while (*a == *b) {
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000353 if (*a == '\0') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000354 return 0;
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000355 }
356 if (!isdigit(*a) && isdigit(*(a+1))) {
357 a_ptr = a+1;
358 b_ptr = b+1;
359 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000360 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000361 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000362 }
Glenn L McGrath2e99d432004-07-23 01:49:46 +0000363
364 if (isdigit(*a) && isdigit(*b)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000365 return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000366 }
367 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000368}
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000369#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000370
371static struct interface *add_interface(char *name)
372{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000373 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000374
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000375 for (ife = int_last; ife; ife = ife->prev) {
Bernhard Reutner-Fischer57d83ff2006-12-07 12:25:17 +0000376 int n = /*n*/strcmp(ife->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000377
378 if (n == 0)
379 return ife;
380 if (n < 0)
381 break;
382 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000383
384 new = xzalloc(sizeof(*new));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000385 strncpy_IFNAMSIZ(new->name, name);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000386 nextp = ife ? &ife->next : &int_list;
387 new->prev = ife;
388 new->next = *nextp;
389 if (new->next)
390 new->next->prev = new;
391 else
392 int_last = new;
393 *nextp = new;
394 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000395}
396
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000397static char *get_name(char *name, char *p)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000398{
Rob Landley9fe801e2006-06-20 21:13:29 +0000399 /* Extract <name> from nul-terminated p where p matches
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200400 * <name>: after leading whitespace.
401 * If match is not made, set name empty and return unchanged p
402 */
403 char *nameend;
404 char *namestart = skip_whitespace(p);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000405
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000406 nameend = namestart;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200407 while (*nameend && *nameend != ':' && !isspace(*nameend))
Eric Andersen9940e082004-08-12 16:52:00 +0000408 nameend++;
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200409 if (*nameend == ':') {
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000410 if ((nameend - namestart) < IFNAMSIZ) {
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200411 memcpy(name, namestart, nameend - namestart);
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000412 name[nameend - namestart] = '\0';
Denys Vlasenko6935ec92009-10-22 19:42:26 +0200413 p = nameend;
Eric Andersen9940e082004-08-12 16:52:00 +0000414 } else {
415 /* Interface name too large */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000416 name[0] = '\0';
Eric Andersen9940e082004-08-12 16:52:00 +0000417 }
418 } else {
Rob Landley9fe801e2006-06-20 21:13:29 +0000419 /* trailing ':' not found - return empty */
Denis Vlasenko11c23d72007-10-11 19:53:10 +0000420 name[0] = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000421 }
Eric Andersen6fea7322004-08-26 21:45:21 +0000422 return p + 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000423}
424
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000425/* If scanf supports size qualifiers for %n conversions, then we can
426 * use a modified fmt that simply stores the position in the fields
427 * having no associated fields in the proc string. Of course, we need
428 * to zero them again when we're done. But that is smaller than the
429 * old approach of multiple scanf occurrences with large numbers of
430 * args. */
431
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000432/* static const char *const ss_fmt[] = { */
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000433/* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
434/* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
435/* "%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 +0000436/* }; */
437
438 /* Lie about the size of the int pointed to for %n. */
439#if INT_MAX == LONG_MAX
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000440static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000441 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
442 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
443 "%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 +0000444};
445#else
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000446static const char *const ss_fmt[] = {
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000447 "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
448 "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
449 "%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 +0000450};
451
452#endif
453
454static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000455{
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000456 memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
457
458 sscanf(bp, ss_fmt[procnetdev_vsn],
459 &ife->stats.rx_bytes, /* missing for 0 */
460 &ife->stats.rx_packets,
461 &ife->stats.rx_errors,
462 &ife->stats.rx_dropped,
463 &ife->stats.rx_fifo_errors,
464 &ife->stats.rx_frame_errors,
465 &ife->stats.rx_compressed, /* missing for <= 1 */
466 &ife->stats.rx_multicast, /* missing for <= 1 */
467 &ife->stats.tx_bytes, /* missing for 0 */
468 &ife->stats.tx_packets,
469 &ife->stats.tx_errors,
470 &ife->stats.tx_dropped,
471 &ife->stats.tx_fifo_errors,
472 &ife->stats.collisions,
473 &ife->stats.tx_carrier_errors,
474 &ife->stats.tx_compressed /* missing for <= 1 */
475 );
476
477 if (procnetdev_vsn <= 1) {
478 if (procnetdev_vsn == 0) {
479 ife->stats.rx_bytes = 0;
480 ife->stats.tx_bytes = 0;
481 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000482 ife->stats.rx_multicast = 0;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000483 ife->stats.rx_compressed = 0;
484 ife->stats.tx_compressed = 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000485 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000486}
487
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000488static int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000489{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000490 if (strstr(buf, "compressed"))
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000491 return 2;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000492 if (strstr(buf, "bytes"))
493 return 1;
494 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000495}
496
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000497static int if_readconf(void)
498{
499 int numreqs = 30;
500 struct ifconf ifc;
501 struct ifreq *ifr;
502 int n, err = -1;
503 int skfd;
504
505 ifc.ifc_buf = NULL;
506
507 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
508 (as of 2.1.128) */
509 skfd = socket(AF_INET, SOCK_DGRAM, 0);
510 if (skfd < 0) {
511 bb_perror_msg("error: no inet socket available");
512 return -1;
513 }
514
515 for (;;) {
516 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
517 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
518
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000519 if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000520 goto out;
521 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000522 if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000523 /* assume it overflowed and try again */
524 numreqs += 10;
525 continue;
526 }
527 break;
528 }
529
530 ifr = ifc.ifc_req;
531 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
532 add_interface(ifr->ifr_name);
533 ifr++;
534 }
535 err = 0;
536
537 out:
538 close(skfd);
539 free(ifc.ifc_buf);
540 return err;
541}
542
Eric Andersenf15d4da2001-03-06 00:48:59 +0000543static int if_readlist_proc(char *target)
544{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000545 static smallint proc_read;
546
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000547 FILE *fh;
548 char buf[512];
549 struct interface *ife;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000550 int err, procnetdev_vsn;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000551
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000552 if (proc_read)
553 return 0;
554 if (!target)
555 proc_read = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000556
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000557 fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000558 if (!fh) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000559 return if_readconf();
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000560 }
561 fgets(buf, sizeof buf, fh); /* eat line */
562 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000563
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000564 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000565
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000566 err = 0;
567 while (fgets(buf, sizeof buf, fh)) {
Eric Andersenf96675b2003-07-28 06:37:04 +0000568 char *s, name[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000569
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000570 s = get_name(name, buf);
571 ife = add_interface(name);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000572 get_dev_fields(s, ife, procnetdev_vsn);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000573 ife->statistics_valid = 1;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100574 if (target && strcmp(target, name) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000575 break;
576 }
577 if (ferror(fh)) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000578 bb_perror_msg(_PATH_PROCNET_DEV);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000579 err = -1;
580 proc_read = 0;
581 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000582 fclose(fh);
583 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000584}
585
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000586static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000587{
Denis Vlasenkod0587ed2007-03-22 19:35:51 +0000588 int err = if_readlist_proc(NULL);
589 /* Needed in order to get ethN:M aliases */
590 if (!err)
591 err = if_readconf();
592 return err;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000593}
594
Eric Andersenf15d4da2001-03-06 00:48:59 +0000595/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000596static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000597{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000598 struct ifreq ifr;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000599 char *ifname = ife->name;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000600 int skfd;
601
602 skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000603
Denis Vlasenko360d9662008-12-02 18:18:50 +0000604 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000605 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
606 close(skfd);
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000607 return -1;
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000608 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000609 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000610
Denis Vlasenko360d9662008-12-02 18:18:50 +0000611 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000612 memset(ife->hwaddr, 0, 32);
613 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000614 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000615
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000616 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000617
Denis Vlasenko360d9662008-12-02 18:18:50 +0000618 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000619 ife->metric = 0;
620 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000621 ife->metric = ifr.ifr_metric;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000622
Denis Vlasenko360d9662008-12-02 18:18:50 +0000623 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000624 ife->mtu = 0;
625 if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000626 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000627
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000628 memset(&ife->map, 0, sizeof(struct ifmap));
Rob Landley93983042005-05-03 21:30:26 +0000629#ifdef SIOCGIFMAP
Denis Vlasenko360d9662008-12-02 18:18:50 +0000630 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Rob Landley93983042005-05-03 21:30:26 +0000631 if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000632 ife->map = ifr.ifr_map;
Rob Landley93983042005-05-03 21:30:26 +0000633#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000634
635#ifdef HAVE_TXQUEUELEN
Denis Vlasenko360d9662008-12-02 18:18:50 +0000636 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000637 ife->tx_queue_len = -1; /* unknown value */
638 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000639 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000640#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000641 ife->tx_queue_len = -1; /* unknown value */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000642#endif
643
Denis Vlasenko360d9662008-12-02 18:18:50 +0000644 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000645 ifr.ifr_addr.sa_family = AF_INET;
646 memset(&ife->addr, 0, sizeof(struct sockaddr));
647 if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
648 ife->has_ip = 1;
649 ife->addr = ifr.ifr_addr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000650 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000651 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
652 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
653 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000654
Denis Vlasenko360d9662008-12-02 18:18:50 +0000655 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000656 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
657 if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
658 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000659
Denis Vlasenko360d9662008-12-02 18:18:50 +0000660 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000661 memset(&ife->netmask, 0, sizeof(struct sockaddr));
662 if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
663 ife->netmask = ifr.ifr_netmask;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000664 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000665
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000666 close(skfd);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000667 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000668}
669
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000670static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000671{
672 if (if_fetch(ife) < 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000673 const char *errmsg;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000674
675 if (errno == ENODEV) {
676 /* Give better error message for this case. */
Rob Landley4e3aff32006-05-29 04:37:28 +0000677 errmsg = "Device not found";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000678 } else {
679 errmsg = strerror(errno);
680 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000681 bb_error_msg("%s: error fetching interface information: %s",
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000682 ife->name, errmsg);
683 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000684 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000685 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000686}
687
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000688static const struct hwtype unspec_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000689 .name = "unspec",
690 .title = "UNSPEC",
691 .type = -1,
692 .print = UNSPEC_print
Eric Andersenf15d4da2001-03-06 00:48:59 +0000693};
694
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000695static const struct hwtype loop_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000696 .name = "loop",
697 .title = "Local Loopback",
698 .type = ARPHRD_LOOPBACK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000699};
700
Eric Andersenf15d4da2001-03-06 00:48:59 +0000701/* Display an Ethernet address in readable format. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000702static char* FAST_FUNC ether_print(unsigned char *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000703{
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200704 char *buff;
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000705 buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
Denys Vlasenkob5a03962017-09-12 17:54:28 +0200706 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]
707 );
Denys Vlasenko02859aa2015-10-09 18:16:40 +0200708 return auto_string(buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000709}
710
Denis Vlasenko1b16bda2007-06-19 11:10:02 +0000711static const struct hwtype ether_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000712 .name = "ether",
713 .title = "Ethernet",
714 .type = ARPHRD_ETHER,
715 .alen = ETH_ALEN,
716 .print = ether_print,
Bartosz Golaszewskic19be752013-07-25 04:39:04 +0200717 .input = in_ether
Eric Andersenf15d4da2001-03-06 00:48:59 +0000718};
719
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000720static const struct hwtype ppp_hwtype = {
"Robert P. J. Day"4137dd72006-06-26 21:54:57 +0000721 .name = "ppp",
722 .title = "Point-to-Point Protocol",
723 .type = ARPHRD_PPP
Eric Andersenf15d4da2001-03-06 00:48:59 +0000724};
725
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000726#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000727static const struct hwtype sit_hwtype = {
728 .name = "sit",
729 .title = "IPv6-in-IPv4",
730 .type = ARPHRD_SIT,
731 .print = UNSPEC_print,
732 .suppress_null_addr = 1
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000733};
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000734#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000735#if ENABLE_FEATURE_HWIB
736static const struct hwtype ib_hwtype = {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000737 .name = "infiniband",
738 .title = "InfiniBand",
739 .type = ARPHRD_INFINIBAND,
740 .alen = INFINIBAND_ALEN,
741 .print = UNSPEC_print,
742 .input = in_ib,
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000743};
744#endif
745
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000746
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000747static const struct hwtype *const hwtypes[] = {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000748 &loop_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000749 &ether_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000750 &ppp_hwtype,
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000751 &unspec_hwtype,
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000752#if ENABLE_FEATURE_IPV6
"Robert P. J. Day"21302c22006-06-26 22:03:43 +0000753 &sit_hwtype,
754#endif
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +0000755#if ENABLE_FEATURE_HWIB
756 &ib_hwtype,
757#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000758 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000759};
760
Eric Andersenf15d4da2001-03-06 00:48:59 +0000761#ifdef IFF_PORTSEL
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000762static const char *const if_port_text[] = {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000763 /* Keep in step with <linux/netdevice.h> */
764 "unknown",
765 "10base2",
766 "10baseT",
767 "AUI",
768 "100baseT",
769 "100baseTX",
770 "100baseFX",
771 NULL
772};
773#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000774
775/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000776const struct hwtype* FAST_FUNC get_hwtype(const char *name)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000777{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000778 const struct hwtype *const *hwp;
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000779
780 hwp = hwtypes;
781 while (*hwp != NULL) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100782 if (strcmp((*hwp)->name, name) == 0)
Denis Vlasenkofa85b862007-01-07 01:24:12 +0000783 return (*hwp);
784 hwp++;
785 }
786 return NULL;
787}
788
789/* Check our hardware type table for this type. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000790const struct hwtype* FAST_FUNC get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000791{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000792 const struct hwtype *const *hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000793
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000794 hwp = hwtypes;
795 while (*hwp != NULL) {
796 if ((*hwp)->type == type)
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000797 return *hwp;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000798 hwp++;
799 }
Denis Vlasenkoaad49992006-11-22 02:12:07 +0000800 return NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000801}
802
803/* return 1 if address is all zeros */
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000804static int hw_null_address(const struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000805{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000806 int i;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000807 unsigned char *address = (unsigned char *) ap;
808
809 for (i = 0; i < hw->alen; i++)
810 if (address[i])
811 return 0;
812 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000813}
814
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000815static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000816
817static void print_bytes_scaled(unsigned long long ull, const char *end)
818{
819 unsigned long long int_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000820 const char *ext;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000821 unsigned int frac_part;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000822 int i;
823
824 frac_part = 0;
825 ext = TRext;
826 int_part = ull;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000827 i = 4;
828 do {
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000829 if (int_part >= 1024) {
830 frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
831 int_part /= 1024;
832 ext += 3; /* KiB, MiB, GiB, TiB */
833 }
834 --i;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000835 } while (i);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000836
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000837 printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000838}
839
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000840
841#ifdef HAVE_AFINET6
842#define IPV6_ADDR_ANY 0x0000U
843
844#define IPV6_ADDR_UNICAST 0x0001U
845#define IPV6_ADDR_MULTICAST 0x0002U
846#define IPV6_ADDR_ANYCAST 0x0004U
847
848#define IPV6_ADDR_LOOPBACK 0x0010U
849#define IPV6_ADDR_LINKLOCAL 0x0020U
850#define IPV6_ADDR_SITELOCAL 0x0040U
851
852#define IPV6_ADDR_COMPATv4 0x0080U
853
854#define IPV6_ADDR_SCOPE_MASK 0x00f0U
855
856#define IPV6_ADDR_MAPPED 0x1000U
857#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
858
859
860static void ife_print6(struct interface *ptr)
861{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000862 FILE *f;
Nicolas Dichtel6761d7d2013-08-12 17:09:59 +0200863 char addr6[40], devname[21];
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000864 struct sockaddr_in6 sap;
865 int plen, scope, dad_status, if_idx;
866 char addr6p[8][5];
867
868 f = fopen_for_read(_PATH_PROCNET_IFINET6);
869 if (f == NULL)
870 return;
871
872 while (fscanf
873 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
874 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
875 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
876 &dad_status, devname) != EOF
877 ) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100878 if (strcmp(devname, ptr->name) == 0) {
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000879 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
880 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
881 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
Felix Fietkauf2c043a2016-01-18 12:07:35 +0100882 memset(&sap, 0, sizeof(sap));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000883 inet_pton(AF_INET6, addr6,
884 (struct sockaddr *) &sap.sin6_addr);
885 sap.sin6_family = AF_INET6;
886 printf(" inet6 addr: %s/%d",
Denys Vlasenko69675782013-01-14 01:34:48 +0100887 INET6_sprint((struct sockaddr *) &sap, 1),
888 plen);
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000889 printf(" Scope:");
890 switch (scope & IPV6_ADDR_SCOPE_MASK) {
891 case 0:
892 puts("Global");
893 break;
894 case IPV6_ADDR_LINKLOCAL:
895 puts("Link");
896 break;
897 case IPV6_ADDR_SITELOCAL:
898 puts("Site");
899 break;
900 case IPV6_ADDR_COMPATv4:
901 puts("Compat");
902 break;
903 case IPV6_ADDR_LOOPBACK:
904 puts("Host");
905 break;
906 default:
907 puts("Unknown");
908 }
909 }
910 }
911 fclose(f);
912}
913#else
Denis Vlasenko00d84172008-11-24 07:34:42 +0000914#define ife_print6(a) ((void)0)
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000915#endif
916
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000917static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000918{
Denis Vlasenko1cc70222007-03-15 19:46:43 +0000919 const struct aftype *ap;
"Vladimir N. Oleynik"0fa9ded2005-10-20 09:48:28 +0000920 const struct hwtype *hw;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000921 int hf;
922 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000923
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000924 ap = get_afntype(ptr->addr.sa_family);
925 if (ap == NULL)
926 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000927
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000928 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000929
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000930 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
931 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000932
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000933 hw = get_hwntype(hf);
934 if (hw == NULL)
935 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000936
Denys Vlasenkoa85b66e2010-05-23 23:04:15 +0200937 printf("%-9s Link encap:%s ", ptr->name, hw->title);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000938 /* For some hardware types (eg Ash, ATM) we don't print the
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000939 hardware address if it's null. */
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000940 if (hw->print != NULL
941 && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
942 ) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000943 printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000944 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000945#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000946 if (ptr->flags & IFF_PORTSEL) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000947 printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000948 if (ptr->flags & IFF_AUTOMEDIA)
Rob Landley4e3aff32006-05-29 04:37:28 +0000949 printf("(auto)");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000950 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000951#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000952 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000953
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000954 if (ptr->has_ip) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000955 printf(" %s addr:%s ", ap->name,
Denys Vlasenko69675782013-01-14 01:34:48 +0100956 ap->sprint(&ptr->addr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000957 if (ptr->flags & IFF_POINTOPOINT) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000958 printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000959 }
960 if (ptr->flags & IFF_BROADCAST) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000961 printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000962 }
Rob Landley4e3aff32006-05-29 04:37:28 +0000963 printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000964 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000965
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000966 ife_print6(ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000967
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000968 printf(" ");
969 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000970
971 if (ptr->flags == 0) {
Rob Landley4e3aff32006-05-29 04:37:28 +0000972 printf("[NO FLAGS] ");
Manuel Novoa III b1d5b742003-08-02 00:04:18 +0000973 } else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000974 static const char ife_print_flags_strs[] ALIGN1 =
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000975 "UP\0"
976 "BROADCAST\0"
977 "DEBUG\0"
978 "LOOPBACK\0"
979 "POINTOPOINT\0"
980 "NOTRAILERS\0"
981 "RUNNING\0"
982 "NOARP\0"
983 "PROMISC\0"
984 "ALLMULTI\0"
985 "SLAVE\0"
986 "MASTER\0"
987 "MULTICAST\0"
988#ifdef HAVE_DYNAMIC
989 "DYNAMIC\0"
990#endif
991 ;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000992 static const unsigned short ife_print_flags_mask[] ALIGN2 = {
Denis Vlasenko6b74b022007-07-29 14:17:48 +0000993 IFF_UP,
994 IFF_BROADCAST,
995 IFF_DEBUG,
996 IFF_LOOPBACK,
997 IFF_POINTOPOINT,
998 IFF_NOTRAILERS,
999 IFF_RUNNING,
1000 IFF_NOARP,
1001 IFF_PROMISC,
1002 IFF_ALLMULTI,
1003 IFF_SLAVE,
1004 IFF_MASTER,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001005 IFF_MULTICAST
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001006#ifdef HAVE_DYNAMIC
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001007 ,IFF_DYNAMIC
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001008#endif
1009 };
1010 const unsigned short *mask = ife_print_flags_mask;
1011 const char *str = ife_print_flags_strs;
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001012 do {
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001013 if (ptr->flags & *mask) {
1014 printf("%s ", str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001015 }
Denis Vlasenko6b74b022007-07-29 14:17:48 +00001016 mask++;
1017 str += strlen(str) + 1;
1018 } while (*str);
Manuel Novoa III b1d5b742003-08-02 00:04:18 +00001019 }
1020
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001021 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
Rob Landley4e3aff32006-05-29 04:37:28 +00001022 printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001023#ifdef SIOCSKEEPALIVE
1024 if (ptr->outfill || ptr->keepalive)
Rob Landley4e3aff32006-05-29 04:37:28 +00001025 printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001026#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +00001027 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001028
1029 /* If needed, display the interface statistics. */
1030
1031 if (ptr->statistics_valid) {
1032 /* XXX: statistics are currently only printed for the primary address,
1033 * not for the aliases, although strictly speaking they're shared
1034 * by all addresses.
1035 */
1036 printf(" ");
1037
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001038 printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001039 ptr->stats.rx_packets, ptr->stats.rx_errors,
1040 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1041 ptr->stats.rx_frame_errors);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001042 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001043 printf(" compressed:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001044 ptr->stats.rx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001045 printf(" ");
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001046 printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
Denys Vlasenko69675782013-01-14 01:34:48 +01001047 ptr->stats.tx_packets, ptr->stats.tx_errors,
1048 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1049 ptr->stats.tx_carrier_errors);
Bernhard Reutner-Fischer214744d2006-03-30 13:38:19 +00001050 printf(" collisions:%lu ", ptr->stats.collisions);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001051 if (can_compress)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001052 printf("compressed:%lu ", ptr->stats.tx_compressed);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001053 if (ptr->tx_queue_len != -1)
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +00001054 printf("txqueuelen:%d ", ptr->tx_queue_len);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001055 printf("\n R");
1056 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1057 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001058 }
1059
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +00001060 if (ptr->map.irq || ptr->map.mem_start
1061 || ptr->map.dma || ptr->map.base_addr
1062 ) {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001063 printf(" ");
1064 if (ptr->map.irq)
Rob Landley4e3aff32006-05-29 04:37:28 +00001065 printf("Interrupt:%d ", ptr->map.irq);
Denys Vlasenko69675782013-01-14 01:34:48 +01001066 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */
Rob Landley4e3aff32006-05-29 04:37:28 +00001067 printf("Base address:0x%lx ",
Denys Vlasenko69675782013-01-14 01:34:48 +01001068 (unsigned long) ptr->map.base_addr);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001069 if (ptr->map.mem_start) {
Rob Landley4e3aff32006-05-29 04:37:28 +00001070 printf("Memory:%lx-%lx ", ptr->map.mem_start,
Denys Vlasenko69675782013-01-14 01:34:48 +01001071 ptr->map.mem_end);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001072 }
1073 if (ptr->map.dma)
Rob Landley4e3aff32006-05-29 04:37:28 +00001074 printf("DMA chan:%x ", ptr->map.dma);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001075 bb_putchar('\n');
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001076 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001077 bb_putchar('\n');
Eric Andersenf15d4da2001-03-06 00:48:59 +00001078}
1079
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001080static int do_if_print(struct interface *ife) /*, int *opt_a)*/
Eric Andersenf15d4da2001-03-06 00:48:59 +00001081{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001082 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001083
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001084 res = do_if_fetch(ife);
1085 if (res >= 0) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001086 if ((ife->flags & IFF_UP) || interface_opt_a)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001087 ife_print(ife);
1088 }
1089 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001090}
1091
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001092static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001093{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001094 struct interface *ife = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001095
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001096 if (if_readlist_proc(name) < 0)
1097 return NULL;
1098 ife = add_interface(name);
1099 return ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001100}
1101
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001102#ifdef UNUSED
1103static int for_all_interfaces(int (*doit) (struct interface *, void *),
Denys Vlasenko69675782013-01-14 01:34:48 +01001104 void *cookie)
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001105{
1106 struct interface *ife;
1107
1108 if (!int_list && (if_readlist() < 0))
1109 return -1;
1110 for (ife = int_list; ife; ife = ife->next) {
1111 int err = doit(ife, cookie);
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001112 if (err)
1113 return err;
1114 }
1115 return 0;
1116}
1117#endif
1118
Eric Andersenf15d4da2001-03-06 00:48:59 +00001119/* for ipv4 add/del modes */
1120static int if_print(char *ifname)
1121{
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001122 struct interface *ife;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001123 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001124
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001125 if (!ifname) {
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001126 /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
1127 if (!int_list && (if_readlist() < 0))
1128 return -1;
1129 for (ife = int_list; ife; ife = ife->next) {
1130 int err = do_if_print(ife); /*, &interface_opt_a);*/
1131 if (err)
1132 return err;
1133 }
1134 return 0;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001135 }
Denis Vlasenko1b16bda2007-06-19 11:10:02 +00001136 ife = lookup_interface(ifname);
1137 res = do_if_fetch(ife);
1138 if (res >= 0)
1139 ife_print(ife);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001140 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001141}
1142
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001143#if ENABLE_FEATURE_HWIB
1144/* Input an Infiniband address and convert to binary. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001145int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001146{
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001147 sap->sa_family = ib_hwtype.type;
Denys Vlasenko48363312010-04-04 15:29:32 +02001148//TODO: error check?
1149 hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN);
1150# ifdef HWIB_DEBUG
1151 fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data));
1152# endif
Denis Vlasenko3b162ec2008-06-04 07:59:51 +00001153 return 0;
Bernhard Reutner-Fischerf3b778a2008-05-16 16:10:31 +00001154}
1155#endif
1156
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001157int FAST_FUNC display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001158{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001159 int status;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001160
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001161 status = if_print(ifname);
Denis Vlasenko1cc70222007-03-15 19:46:43 +00001162
1163 return (status < 0); /* status < 0 == 1 -- error */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001164}