blob: ff7970321f8501ce67168a9ee74334da437bda06 [file] [log] [blame]
Eric Andersenf15d4da2001-03-06 00:48:59 +00001/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
4 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
7 * Pruned unused code using KEEP_UNUSED define.
8 * 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 *
Glenn L McGrath642f2892002-11-28 10:20:45 +000018 * Version: $Id: interface.c,v 1.12 2002/11/28 10:20:45 bug1 Exp $
Eric Andersenf15d4da2001-03-06 00:48:59 +000019 *
20 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
21 * and others. Copyright 1993 MicroWalt Corporation
22 *
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
28 *
29 * Patched to support 'add' and 'del' keywords for INET(4) addresses
30 * by Mrs. Brisby <mrs.brisby@nimh.org>
31 *
32 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
33 * - gettext instead of catgets for i18n
34 * 10/1998 - Andi Kleen. Use interface list primitives.
35 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
36 * (default AF was wrong)
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000037 */
38
39/* #define KEEP_UNUSED */
40
Eric Andersenf15d4da2001-03-06 00:48:59 +000041/*
42 *
43 * Protocol Families.
44 *
45 */
46#define HAVE_AFINET 1
Eric Andersenf15d4da2001-03-06 00:48:59 +000047#undef HAVE_AFIPX
48#undef HAVE_AFATALK
49#undef HAVE_AFNETROM
50#undef HAVE_AFX25
51#undef HAVE_AFECONET
Eric Andersen8b113f92001-06-01 21:47:15 +000052#undef HAVE_AFASH
Eric Andersenf15d4da2001-03-06 00:48:59 +000053
Glenn L McGrath8ae4cab2002-11-26 09:02:06 +000054#ifdef CONFIG_FEATURE_IPV6
55# define HAVE_AFINET6 1
56#else
57# undef HAVE_AFINET6
Eric Andersen51b8bd62002-07-03 11:46:38 +000058#endif
59
Eric Andersenf15d4da2001-03-06 00:48:59 +000060/*
61 *
62 * Device Hardware types.
63 *
64 */
65#define HAVE_HWETHER 1
66#define HAVE_HWPPP 1
67#undef HAVE_HWSLIP
68
69
Eric Andersencd8c4362001-11-10 11:22:46 +000070#include "inet_common.h"
Eric Andersenf15d4da2001-03-06 00:48:59 +000071#include <stdio.h>
72#include <errno.h>
Eric Andersenf15d4da2001-03-06 00:48:59 +000073#include <stdlib.h>
74#include <string.h>
75#include <unistd.h>
Eric Andersencd8c4362001-11-10 11:22:46 +000076#include <fcntl.h>
77#include <ctype.h>
78#include <sys/ioctl.h>
79#include <net/if.h>
80#include <net/if_arp.h>
Glenn L McGrath6b8c5502001-05-05 03:19:12 +000081#include "libbb.h"
Eric Andersenf15d4da2001-03-06 00:48:59 +000082
83#define _(x) x
84#define _PATH_PROCNET_DEV "/proc/net/dev"
Eric Andersen51b8bd62002-07-03 11:46:38 +000085#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
Eric Andersenf15d4da2001-03-06 00:48:59 +000086#define new(p) ((p) = xcalloc(1,sizeof(*(p))))
Eric Andersen8b113f92001-06-01 21:47:15 +000087#define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
Eric Andersenf15d4da2001-03-06 00:48:59 +000088
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000089static int procnetdev_vsn = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +000090
Eric Andersenf15d4da2001-03-06 00:48:59 +000091/* Ugh. But libc5 doesn't provide POSIX types. */
92#include <asm/types.h>
93
94
95#ifdef HAVE_HWSLIP
96#include <linux/if_slip.h>
97#endif
98
99#if HAVE_AFINET6
100
101#ifndef _LINUX_IN6_H
102/*
103 * This is in linux/include/net/ipv6.h.
104 */
105
106struct in6_ifreq {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000107 struct in6_addr ifr6_addr;
108 __u32 ifr6_prefixlen;
109 unsigned int ifr6_ifindex;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000110};
111
112#endif
113
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000114#endif /* HAVE_AFINET6 */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000115
116#if HAVE_AFIPX
Eric Andersenb6b519b2001-04-09 23:52:18 +0000117#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000118#include <netipx/ipx.h>
119#else
120#include "ipx.h"
121#endif
122#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000123
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000124/* Defines for glibc2.0 users. */
125#ifndef SIOCSIFTXQLEN
126#define SIOCSIFTXQLEN 0x8943
127#define SIOCGIFTXQLEN 0x8942
128#endif
129
130/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
131#ifndef ifr_qlen
132#define ifr_qlen ifr_ifru.ifru_mtu
133#endif
134
135#ifndef HAVE_TXQUEUELEN
136#define HAVE_TXQUEUELEN 1
137#endif
138
139#ifndef IFF_DYNAMIC
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000140#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000141#endif
142
Eric Andersenf15d4da2001-03-06 00:48:59 +0000143/* This structure defines protocol families and their handlers. */
144struct aftype {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000145 const char *name;
146 const char *title;
147 int af;
148 int alen;
149 char *(*print) (unsigned char *);
150 char *(*sprint) (struct sockaddr *, int numeric);
151 int (*input) (int type, char *bufp, struct sockaddr *);
152 void (*herror) (char *text);
153 int (*rprint) (int options);
154 int (*rinput) (int typ, int ext, char **argv);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000155
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000156 /* may modify src */
157 int (*getmask) (char *src, struct sockaddr * mask, char *name);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000158
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000159 int fd;
160 char *flag_file;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000161};
162
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000163#ifdef KEEP_UNUSED
164
165static int flag_unx;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000166
Eric Andersen8b113f92001-06-01 21:47:15 +0000167#ifdef HAVE_AFIPX
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000168static int flag_ipx;
Eric Andersen8b113f92001-06-01 21:47:15 +0000169#endif
170#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000171static int flag_ax25;
Eric Andersen8b113f92001-06-01 21:47:15 +0000172#endif
173#ifdef HAVE_AFATALK
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000174static int flag_ddp;
Eric Andersen8b113f92001-06-01 21:47:15 +0000175#endif
176#ifdef HAVE_AFNETROM
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000177static int flag_netrom;
Eric Andersen8b113f92001-06-01 21:47:15 +0000178#endif
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000179static int flag_inet;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000180
Eric Andersen8b113f92001-06-01 21:47:15 +0000181#ifdef HAVE_AFINET6
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000182static int flag_inet6;
Eric Andersen8b113f92001-06-01 21:47:15 +0000183#endif
184#ifdef HAVE_AFECONET
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000185static int flag_econet;
Eric Andersen8b113f92001-06-01 21:47:15 +0000186#endif
187#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000188static int flag_x25 = 0;
Eric Andersen8b113f92001-06-01 21:47:15 +0000189#endif
190#ifdef HAVE_AFASH
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000191static int flag_ash;
Eric Andersen8b113f92001-06-01 21:47:15 +0000192#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000193
194
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000195static struct aftrans_t {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000196 char *alias;
197 char *name;
198 int *flag;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000199} aftrans[] = {
200
Eric Andersen8b113f92001-06-01 21:47:15 +0000201#ifdef HAVE_AFX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000202 {
203 "ax25", "ax25", &flag_ax25},
Eric Andersen8b113f92001-06-01 21:47:15 +0000204#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000205 {
206 "ip", "inet", &flag_inet},
Eric Andersen8b113f92001-06-01 21:47:15 +0000207#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000208 {
209 "ip6", "inet6", &flag_inet6},
Eric Andersen8b113f92001-06-01 21:47:15 +0000210#endif
211#ifdef HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000212 {
213 "ipx", "ipx", &flag_ipx},
Eric Andersen8b113f92001-06-01 21:47:15 +0000214#endif
215#ifdef HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000216 {
217 "appletalk", "ddp", &flag_ddp},
Eric Andersen8b113f92001-06-01 21:47:15 +0000218#endif
219#ifdef HAVE_AFNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000220 {
221 "netrom", "netrom", &flag_netrom},
Eric Andersen8b113f92001-06-01 21:47:15 +0000222#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000223 {
224 "inet", "inet", &flag_inet},
Eric Andersen8b113f92001-06-01 21:47:15 +0000225#ifdef HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000226 {
227 "inet6", "inet6", &flag_inet6},
Eric Andersen8b113f92001-06-01 21:47:15 +0000228#endif
229#ifdef HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000230 {
231 "ddp", "ddp", &flag_ddp},
Eric Andersen8b113f92001-06-01 21:47:15 +0000232#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000233 {
234 "unix", "unix", &flag_unx}, {
235 "tcpip", "inet", &flag_inet},
Eric Andersen8b113f92001-06-01 21:47:15 +0000236#ifdef HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000237 {
238 "econet", "ec", &flag_econet},
Eric Andersen8b113f92001-06-01 21:47:15 +0000239#endif
240#ifdef HAVE_AFX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000241 {
242 "x25", "x25", &flag_x25},
Eric Andersen8b113f92001-06-01 21:47:15 +0000243#endif
244#ifdef HAVE_AFASH
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000245 {
246 "ash", "ash", &flag_ash},
Eric Andersen8b113f92001-06-01 21:47:15 +0000247#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000248 {
249 0, 0, 0}
Eric Andersenf15d4da2001-03-06 00:48:59 +0000250};
251
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000252static char afname[256] = "";
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000253#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000254
255#if HAVE_AFUNIX
256
257/* Display a UNIX domain address. */
258static char *UNIX_print(unsigned char *ptr)
259{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000260 return (ptr);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000261}
262
263
264/* Display a UNIX domain address. */
265static char *UNIX_sprint(struct sockaddr *sap, int numeric)
266{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000267 static char buf[64];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000268
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000269 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
270 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
271 return (UNIX_print(sap->sa_data));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000272}
273
274
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000275static struct aftype unix_aftype = {
276 "unix", "UNIX Domain", AF_UNIX, 0,
277 UNIX_print, UNIX_sprint, NULL, NULL,
278 NULL, NULL, NULL,
279 -1,
280 "/proc/net/unix"
Eric Andersenf15d4da2001-03-06 00:48:59 +0000281};
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000282#endif /* HAVE_AFUNIX */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000283
284#if HAVE_AFINET
285
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000286#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000287static void INET_reserror(char *text)
288{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000289 herror(text);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000290}
291
Eric Andersenf15d4da2001-03-06 00:48:59 +0000292/* Display an Internet socket address. */
293static char *INET_print(unsigned char *ptr)
294{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000295 return (inet_ntoa((*(struct in_addr *) ptr)));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000296}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000297#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000298
299/* Display an Internet socket address. */
300static char *INET_sprint(struct sockaddr *sap, int numeric)
301{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000302 static char buff[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000303
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000304 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
305 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000306
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000307 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
308 numeric, 0xffffff00) != 0)
309 return (NULL);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000310
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000311 return (buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000312}
313
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000314#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000315static char *INET_sprintmask(struct sockaddr *sap, int numeric,
316 unsigned int netmask)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000317{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000318 static char buff[128];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000319
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000320 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
321 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
322 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
323 numeric, netmask) != 0)
324 return (NULL);
325 return (buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000326}
327
Eric Andersenf15d4da2001-03-06 00:48:59 +0000328static int INET_getsock(char *bufp, struct sockaddr *sap)
329{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000330 char *sp = bufp, *bp;
331 unsigned int i;
332 unsigned val;
333 struct sockaddr_in *sin;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000334
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000335 sin = (struct sockaddr_in *) sap;
336 sin->sin_family = AF_INET;
337 sin->sin_port = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000338
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000339 val = 0;
340 bp = (char *) &val;
341 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
342 *sp = toupper(*sp);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000343
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000344 if ((*sp >= 'A') && (*sp <= 'F'))
345 bp[i] |= (int) (*sp - 'A') + 10;
346 else if ((*sp >= '0') && (*sp <= '9'))
347 bp[i] |= (int) (*sp - '0');
348 else
349 return (-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000350
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000351 bp[i] <<= 4;
352 sp++;
353 *sp = toupper(*sp);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000354
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000355 if ((*sp >= 'A') && (*sp <= 'F'))
356 bp[i] |= (int) (*sp - 'A') + 10;
357 else if ((*sp >= '0') && (*sp <= '9'))
358 bp[i] |= (int) (*sp - '0');
359 else
360 return (-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000361
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000362 sp++;
363 }
364 sin->sin_addr.s_addr = htonl(val);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000365
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000366 return (sp - bufp);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000367}
368
369static int INET_input(int type, char *bufp, struct sockaddr *sap)
370{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000371 switch (type) {
372 case 1:
373 return (INET_getsock(bufp, sap));
374 case 256:
375 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
376 default:
377 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
378 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000379}
380
381static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
382{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000383 struct sockaddr_in *mask = (struct sockaddr_in *) m;
384 char *slash, *end;
385 int prefix;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000386
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000387 if ((slash = strchr(adr, '/')) == NULL)
388 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000389
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000390 *slash++ = '\0';
391 prefix = strtoul(slash, &end, 0);
392 if (*end != '\0')
393 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000394
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000395 if (name) {
396 sprintf(name, "/%d", prefix);
397 }
398 mask->sin_family = AF_INET;
399 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
400 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000401}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000402#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000403
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000404static struct aftype inet_aftype = {
405 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
406 NULL /* UNUSED INET_print */ , INET_sprint,
407 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
408 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
409 NULL /* UNUSED INET_getnetmask */ ,
410 -1,
411 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000412};
413
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000414#endif /* HAVE_AFINET */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000415
Eric Andersen51b8bd62002-07-03 11:46:38 +0000416#if HAVE_AFINET6
417
418#ifdef KEEP_UNUSED
419static void INET6_reserror(char *text)
420{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000421 herror(text);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000422}
423
424/* Display an Internet socket address. */
425static char *INET6_print(unsigned char *ptr)
426{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000427 static char name[80];
Eric Andersen51b8bd62002-07-03 11:46:38 +0000428
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000429 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
430 return name;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000431}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000432#endif /* KEEP_UNUSED */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000433
434/* Display an Internet socket address. */
435/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
436static char *INET6_sprint(struct sockaddr *sap, int numeric)
437{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000438 static char buff[128];
Eric Andersen51b8bd62002-07-03 11:46:38 +0000439
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000440 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
441 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
442 if (INET6_rresolve
443 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
444 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
445 return (buff);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000446}
447
448#ifdef KEEP_UNUSED
449static int INET6_getsock(char *bufp, struct sockaddr *sap)
450{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000451 struct sockaddr_in6 *sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000452
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000453 sin6 = (struct sockaddr_in6 *) sap;
454 sin6->sin6_family = AF_INET6;
455 sin6->sin6_port = 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000456
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000457 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
458 return (-1);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000459
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000460 return 16; /* ?;) */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000461}
462
463static int INET6_input(int type, char *bufp, struct sockaddr *sap)
464{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000465 switch (type) {
466 case 1:
467 return (INET6_getsock(bufp, sap));
468 default:
469 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
470 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000471}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000472#endif /* KEEP_UNUSED */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000473
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000474static struct aftype inet6_aftype = {
475 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
476 NULL /* UNUSED INET6_print */ , INET6_sprint,
477 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
478 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
479 NULL /* UNUSED INET6_getnetmask */ ,
480 -1,
481 NULL
Eric Andersen51b8bd62002-07-03 11:46:38 +0000482};
483
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000484#endif /* HAVE_AFINET6 */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000485
Eric Andersenf15d4da2001-03-06 00:48:59 +0000486/* Display an UNSPEC address. */
487static char *UNSPEC_print(unsigned char *ptr)
488{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000489 static char buff[sizeof(struct sockaddr) * 3 + 1];
490 char *pos;
491 unsigned int i;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000492
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000493 pos = buff;
494 for (i = 0; i < sizeof(struct sockaddr); i++) {
495 /* careful -- not every libc's sprintf returns # bytes written */
496 sprintf(pos, "%02X-", (*ptr++ & 0377));
497 pos += 3;
498 }
499 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
500 *--pos = '\0';
501 return (buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000502}
503
504/* Display an UNSPEC socket address. */
505static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
506{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000507 static char buf[64];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000508
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000509 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
510 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
511 return (UNSPEC_print(sap->sa_data));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000512}
513
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000514static struct aftype unspec_aftype = {
515 "unspec", "UNSPEC", AF_UNSPEC, 0,
516 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
517 NULL,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000518};
519
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000520static struct aftype *aftypes[] = {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000521#if HAVE_AFUNIX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000522 &unix_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000523#endif
524#if HAVE_AFINET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000525 &inet_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000526#endif
527#if HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000528 &inet6_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000529#endif
530#if HAVE_AFAX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000531 &ax25_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000532#endif
533#if HAVE_AFNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000534 &netrom_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000535#endif
536#if HAVE_AFROSE
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000537 &rose_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000538#endif
539#if HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000540 &ipx_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000541#endif
542#if HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000543 &ddp_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000544#endif
545#if HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000546 &ec_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000547#endif
548#if HAVE_AFASH
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000549 &ash_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000550#endif
551#if HAVE_AFX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000552 &x25_aftype,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000553#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000554 &unspec_aftype,
555 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +0000556};
557
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000558#ifdef KEEP_UNUSED
559static short sVafinit = 0;
560
561static void afinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +0000562{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000563 unspec_aftype.title = _("UNSPEC");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000564#if HAVE_AFUNIX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000565 unix_aftype.title = _("UNIX Domain");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000566#endif
567#if HAVE_AFINET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000568 inet_aftype.title = _("DARPA Internet");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000569#endif
570#if HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000571 inet6_aftype.title = _("IPv6");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000572#endif
573#if HAVE_AFAX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000574 ax25_aftype.title = _("AMPR AX.25");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000575#endif
576#if HAVE_AFNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000577 netrom_aftype.title = _("AMPR NET/ROM");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000578#endif
579#if HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000580 ipx_aftype.title = _("Novell IPX");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000581#endif
582#if HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000583 ddp_aftype.title = _("Appletalk DDP");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000584#endif
585#if HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000586 ec_aftype.title = _("Econet");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000587#endif
588#if HAVE_AFX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000589 x25_aftype.title = _("CCITT X.25");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000590#endif
591#if HAVE_AFROSE
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000592 rose_aftype.title = _("AMPR ROSE");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000593#endif
594#if HAVE_AFASH
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000595 ash_aftype.title = _("Ash");
596#endif
597 sVafinit = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000598}
599
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000600static int aftrans_opt(const char *arg)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000601{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000602 struct aftrans_t *paft;
603 char *tmp1, *tmp2;
604 char buf[256];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000605
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000606 safe_strncpy(buf, arg, sizeof(buf));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000607
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000608 tmp1 = buf;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000609
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000610 while (tmp1) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000611
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000612 tmp2 = strchr(tmp1, ',');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000613
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000614 if (tmp2)
615 *(tmp2++) = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000616
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000617 paft = aftrans;
618 for (paft = aftrans; paft->alias; paft++) {
619 if (strcmp(tmp1, paft->alias))
620 continue;
621 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
Glenn L McGrath642f2892002-11-28 10:20:45 +0000622 error_msg(_("Too many address family arguments."));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000623 return (0);
624 }
625 if (paft->flag)
626 (*paft->flag)++;
627 if (afname[0])
628 strcat(afname, ",");
629 strcat(afname, paft->name);
630 break;
631 }
632 if (!paft->alias) {
Glenn L McGrath642f2892002-11-28 10:20:45 +0000633 error_msg(_("Unknown address family `%s'."), tmp1);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000634 return (1);
635 }
636 tmp1 = tmp2;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000637 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000638
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000639 return (0);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000640}
641
642/* set the default AF list from the program name or a constant value */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000643static void aftrans_def(char *tool, char *argv0, char *dflt)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000644{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000645 char *tmp;
646 char *buf;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000647
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000648 strcpy(afname, dflt);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000649
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000650 if (!(tmp = strrchr(argv0, '/')))
651 tmp = argv0; /* no slash?! */
652 else
653 tmp++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000654
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000655 if (!(buf = strdup(tmp)))
656 return;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000657
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000658 if (strlen(tool) >= strlen(tmp)) {
659 free(buf);
660 return;
661 }
662 tmp = buf + (strlen(tmp) - strlen(tool));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000663
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000664 if (strcmp(tmp, tool) != 0) {
665 free(buf);
666 return;
667 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000668 *tmp = '\0';
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000669 if ((tmp = strchr(buf, '_')))
670 *tmp = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000671
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000672 afname[0] = '\0';
673 if (aftrans_opt(buf))
674 strcpy(afname, buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000675
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000676 free(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000677}
678
Eric Andersenf15d4da2001-03-06 00:48:59 +0000679/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000680static struct aftype *get_aftype(const char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000681{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000682 struct aftype **afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000683
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000684#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000685 if (!sVafinit)
686 afinit();
687#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000688
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000689 afp = aftypes;
690 while (*afp != NULL) {
691 if (!strcmp((*afp)->name, name))
692 return (*afp);
693 afp++;
694 }
695 if (strchr(name, ','))
Glenn L McGrath642f2892002-11-28 10:20:45 +0000696 error_msg(_("Please don't supply more than one address family."));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000697 return (NULL);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000698}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000699#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000700
701/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000702static struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000703{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000704 struct aftype **afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000705
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000706#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000707 if (!sVafinit)
708 afinit();
709#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000710
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000711 afp = aftypes;
712 while (*afp != NULL) {
713 if ((*afp)->af == af)
714 return (*afp);
715 afp++;
716 }
717 return (NULL);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000718}
719
720/* Check our protocol family table for this family and return its socket */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000721static int get_socket_for_af(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000722{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000723 struct aftype **afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000724
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000725#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000726 if (!sVafinit)
727 afinit();
728#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000729
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000730 afp = aftypes;
731 while (*afp != NULL) {
732 if ((*afp)->af == af)
733 return (*afp)->fd;
734 afp++;
735 }
736 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000737}
738
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000739#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000740/* type: 0=all, 1=getroute */
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000741static void print_aflist(int type)
742{
743 int count = 0;
744 char *txt;
745 struct aftype **afp;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000746
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000747#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000748 if (!sVafinit)
749 afinit();
750#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000751
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000752 afp = aftypes;
753 while (*afp != NULL) {
754 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
755 afp++;
756 continue;
757 }
758 if ((count % 3) == 0)
759 fprintf(stderr, count ? "\n " : " ");
760 txt = (*afp)->name;
761 if (!txt)
762 txt = "..";
763 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
764 count++;
765 afp++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000766 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000767 fprintf(stderr, "\n");
Eric Andersenf15d4da2001-03-06 00:48:59 +0000768}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000769#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000770
771struct user_net_device_stats {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000772 unsigned long long rx_packets; /* total packets received */
773 unsigned long long tx_packets; /* total packets transmitted */
774 unsigned long long rx_bytes; /* total bytes received */
775 unsigned long long tx_bytes; /* total bytes transmitted */
776 unsigned long rx_errors; /* bad packets received */
777 unsigned long tx_errors; /* packet transmit problems */
778 unsigned long rx_dropped; /* no space in linux buffers */
779 unsigned long tx_dropped; /* no space available in linux */
780 unsigned long rx_multicast; /* multicast packets received */
781 unsigned long rx_compressed;
782 unsigned long tx_compressed;
783 unsigned long collisions;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000784
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000785 /* detailed rx_errors: */
786 unsigned long rx_length_errors;
787 unsigned long rx_over_errors; /* receiver ring buff overflow */
788 unsigned long rx_crc_errors; /* recved pkt with crc error */
789 unsigned long rx_frame_errors; /* recv'd frame alignment error */
790 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
791 unsigned long rx_missed_errors; /* receiver missed packet */
792 /* detailed tx_errors */
793 unsigned long tx_aborted_errors;
794 unsigned long tx_carrier_errors;
795 unsigned long tx_fifo_errors;
796 unsigned long tx_heartbeat_errors;
797 unsigned long tx_window_errors;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000798};
799
800struct interface {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000801 struct interface *next, *prev;
802 char name[IFNAMSIZ]; /* interface name */
803 short type; /* if type */
804 short flags; /* various flags */
805 int metric; /* routing metric */
806 int mtu; /* MTU value */
807 int tx_queue_len; /* transmit queue length */
808 struct ifmap map; /* hardware setup */
809 struct sockaddr addr; /* IP address */
810 struct sockaddr dstaddr; /* P-P IP address */
811 struct sockaddr broadaddr; /* IP broadcast address */
812 struct sockaddr netmask; /* IP network mask */
813 struct sockaddr ipxaddr_bb; /* IPX network address */
814 struct sockaddr ipxaddr_sn; /* IPX network address */
815 struct sockaddr ipxaddr_e3; /* IPX network address */
816 struct sockaddr ipxaddr_e2; /* IPX network address */
817 struct sockaddr ddpaddr; /* Appletalk DDP address */
818 struct sockaddr ecaddr; /* Econet address */
819 int has_ip;
820 int has_ipx_bb;
821 int has_ipx_sn;
822 int has_ipx_e3;
823 int has_ipx_e2;
824 int has_ax25;
825 int has_ddp;
826 int has_econet;
827 char hwaddr[32]; /* HW address */
828 int statistics_valid;
829 struct user_net_device_stats stats; /* statistics */
830 int keepalive; /* keepalive value for SLIP */
831 int outfill; /* outfill value for SLIP */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000832};
833
834
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000835int interface_opt_a = 0; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000836
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000837#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000838static int opt_i = 0; /* show the statistics */
839static int opt_v = 0; /* debugging output flag */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000840
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000841static int addr_family = 0; /* currently selected AF */
842#endif /* KEEP_UNUSED */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000843
Eric Andersenf15d4da2001-03-06 00:48:59 +0000844static struct interface *int_list, *int_last;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000845static int skfd = -1; /* generic raw socket desc. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000846
847
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000848static int sockets_open(int family)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000849{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000850 struct aftype **aft;
851 int sfd = -1;
852 static int force = -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000853
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000854 if (force < 0) {
855 force = 0;
856 if (get_kernel_revision() < KRELEASE(2, 1, 0))
857 force = 1;
858 if (access("/proc/net", R_OK))
859 force = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000860 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000861 for (aft = aftypes; *aft; aft++) {
862 struct aftype *af = *aft;
863 int type = SOCK_DGRAM;
864
865 if (af->af == AF_UNSPEC)
866 continue;
867 if (family && family != af->af)
868 continue;
869 if (af->fd != -1) {
870 sfd = af->fd;
871 continue;
872 }
873 /* Check some /proc file first to not stress kmod */
874 if (!family && !force && af->flag_file) {
875 if (access(af->flag_file, R_OK))
876 continue;
877 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000878#if HAVE_AFNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000879 if (af->af == AF_NETROM)
880 type = SOCK_SEQPACKET;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000881#endif
882#if HAVE_AFX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000883 if (af->af == AF_X25)
884 type = SOCK_SEQPACKET;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000885#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000886 af->fd = socket(af->af, type, 0);
887 if (af->fd >= 0)
888 sfd = af->fd;
889 }
Glenn L McGrath642f2892002-11-28 10:20:45 +0000890 if (sfd < 0) {
891 error_msg(_("No usable address families found."));
892 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000893 return sfd;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000894}
895
896/* like strcmp(), but knows about numbers */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000897static int nstrcmp(const char *astr, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000898{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000899 const char *a = astr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000900
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000901 while (*a == *b) {
902 if (*a == '\0')
903 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000904 a++;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000905 b++;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000906 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000907 if (isdigit(*a)) {
908 if (!isdigit(*b))
909 return -1;
910 while (a > astr) {
911 a--;
912 if (!isdigit(*a)) {
913 a++;
914 break;
915 }
916 if (!isdigit(*b))
917 return -1;
918 b--;
919 }
920 return atoi(a) > atoi(b) ? 1 : -1;
921 }
922 return *a - *b;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000923}
924
925static struct interface *add_interface(char *name)
926{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000927 struct interface *ife, **nextp, *new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000928
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000929 for (ife = int_last; ife; ife = ife->prev) {
930 int n = nstrcmp(ife->name, name);
931
932 if (n == 0)
933 return ife;
934 if (n < 0)
935 break;
936 }
937 new(new);
938 safe_strncpy(new->name, name, IFNAMSIZ);
939 nextp = ife ? &ife->next : &int_list;
940 new->prev = ife;
941 new->next = *nextp;
942 if (new->next)
943 new->next->prev = new;
944 else
945 int_last = new;
946 *nextp = new;
947 return new;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000948}
949
950
951static int if_readconf(void)
952{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000953 int numreqs = 30;
954 struct ifconf ifc;
955 struct ifreq *ifr;
956 int n, err = -1;
957 int skfd2;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000958
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000959 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
960 (as of 2.1.128) */
961 skfd2 = get_socket_for_af(AF_INET);
962 if (skfd2 < 0) {
Glenn L McGrath642f2892002-11-28 10:20:45 +0000963 perror_msg(("warning: no inet socket available: %s\n"));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000964 /* Try to soldier on with whatever socket we can get hold of. */
965 skfd2 = sockets_open(0);
966 if (skfd2 < 0)
967 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000968 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000969
970 ifc.ifc_buf = NULL;
971 for (;;) {
972 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
973 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
974
975 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
976 perror("SIOCGIFCONF");
977 goto out;
978 }
979 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
980 /* assume it overflowed and try again */
981 numreqs += 10;
982 continue;
983 }
984 break;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000985 }
Eric Andersenf15d4da2001-03-06 00:48:59 +0000986
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000987 ifr = ifc.ifc_req;
988 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
989 add_interface(ifr->ifr_name);
990 ifr++;
991 }
992 err = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000993
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +0000994 out:
995 free(ifc.ifc_buf);
996 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000997}
998
999static char *get_name(char *name, char *p)
1000{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001001 while (isspace(*p))
1002 p++;
1003 while (*p) {
1004 if (isspace(*p))
1005 break;
1006 if (*p == ':') { /* could be an alias */
1007 char *dot = p, *dotname = name;
1008
1009 *name++ = *p++;
1010 while (isdigit(*p))
1011 *name++ = *p++;
1012 if (*p != ':') { /* it wasn't, backup */
1013 p = dot;
1014 name = dotname;
1015 }
1016 if (*p == '\0')
1017 return NULL;
1018 p++;
1019 break;
1020 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001021 *name++ = *p++;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001022 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001023 *name++ = '\0';
1024 return p;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001025}
1026
1027static int get_dev_fields(char *bp, struct interface *ife)
1028{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001029 switch (procnetdev_vsn) {
1030 case 3:
1031 sscanf(bp,
1032 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1033 &ife->stats.rx_bytes,
1034 &ife->stats.rx_packets,
1035 &ife->stats.rx_errors,
1036 &ife->stats.rx_dropped,
1037 &ife->stats.rx_fifo_errors,
1038 &ife->stats.rx_frame_errors,
1039 &ife->stats.rx_compressed,
1040 &ife->stats.rx_multicast,
1041 &ife->stats.tx_bytes,
1042 &ife->stats.tx_packets,
1043 &ife->stats.tx_errors,
1044 &ife->stats.tx_dropped,
1045 &ife->stats.tx_fifo_errors,
1046 &ife->stats.collisions,
1047 &ife->stats.tx_carrier_errors, &ife->stats.tx_compressed);
1048 break;
1049 case 2:
1050 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1051 &ife->stats.rx_bytes,
1052 &ife->stats.rx_packets,
1053 &ife->stats.rx_errors,
1054 &ife->stats.rx_dropped,
1055 &ife->stats.rx_fifo_errors,
1056 &ife->stats.rx_frame_errors,
1057 &ife->stats.tx_bytes,
1058 &ife->stats.tx_packets,
1059 &ife->stats.tx_errors,
1060 &ife->stats.tx_dropped,
1061 &ife->stats.tx_fifo_errors,
1062 &ife->stats.collisions, &ife->stats.tx_carrier_errors);
1063 ife->stats.rx_multicast = 0;
1064 break;
1065 case 1:
1066 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1067 &ife->stats.rx_packets,
1068 &ife->stats.rx_errors,
1069 &ife->stats.rx_dropped,
1070 &ife->stats.rx_fifo_errors,
1071 &ife->stats.rx_frame_errors,
1072 &ife->stats.tx_packets,
1073 &ife->stats.tx_errors,
1074 &ife->stats.tx_dropped,
1075 &ife->stats.tx_fifo_errors,
1076 &ife->stats.collisions, &ife->stats.tx_carrier_errors);
1077 ife->stats.rx_bytes = 0;
1078 ife->stats.tx_bytes = 0;
1079 ife->stats.rx_multicast = 0;
1080 break;
1081 }
1082 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001083}
1084
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001085static inline int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001086{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001087 if (strstr(buf, "compressed"))
1088 return 3;
1089 if (strstr(buf, "bytes"))
1090 return 2;
1091 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001092}
1093
1094static int if_readlist_proc(char *target)
1095{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001096 static int proc_read;
1097 FILE *fh;
1098 char buf[512];
1099 struct interface *ife;
1100 int err;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001101
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001102 if (proc_read)
1103 return 0;
1104 if (!target)
1105 proc_read = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001106
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001107 fh = fopen(_PATH_PROCNET_DEV, "r");
1108 if (!fh) {
Glenn L McGrath642f2892002-11-28 10:20:45 +00001109 perror_msg(_("Warning: cannot open %s (%s). Limited output.\n"), _PATH_PROCNET_DEV);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001110 return if_readconf();
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001111 }
1112 fgets(buf, sizeof buf, fh); /* eat line */
1113 fgets(buf, sizeof buf, fh);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001114
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001115#if 0 /* pretty, but can't cope with missing fields */
1116 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh, "face", "", /* parsed separately */
1117 "bytes", "%lu",
1118 "packets", "%lu",
1119 "errs", "%lu",
1120 "drop", "%lu",
1121 "fifo", "%lu",
1122 "frame", "%lu",
1123 "compressed", "%lu",
1124 "multicast", "%lu",
1125 "bytes", "%lu",
1126 "packets", "%lu",
1127 "errs", "%lu",
1128 "drop", "%lu",
1129 "fifo", "%lu",
1130 "colls", "%lu",
1131 "carrier", "%lu", "compressed", "%lu", NULL);
1132 if (!fmt)
1133 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001134#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001135 procnetdev_vsn = procnetdev_version(buf);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001136#endif
1137
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001138 err = 0;
1139 while (fgets(buf, sizeof buf, fh)) {
1140 char *s, name[IFNAMSIZ];
Eric Andersenf15d4da2001-03-06 00:48:59 +00001141
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001142 s = get_name(name, buf);
1143 ife = add_interface(name);
1144 get_dev_fields(s, ife);
1145 ife->statistics_valid = 1;
1146 if (target && !strcmp(target, name))
1147 break;
1148 }
1149 if (ferror(fh)) {
1150 perror(_PATH_PROCNET_DEV);
1151 err = -1;
1152 proc_read = 0;
1153 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001154#if 0
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001155 free(fmt);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001156#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001157 fclose(fh);
1158 return err;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001159}
1160
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001161static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001162{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001163 int err = if_readlist_proc(NULL);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001164
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001165 if (!err)
1166 err = if_readconf();
1167 return err;
1168}
1169
1170static int for_all_interfaces(int (*doit) (struct interface *, void *),
1171 void *cookie)
1172{
1173 struct interface *ife;
1174
1175 if (!int_list && (if_readlist() < 0))
1176 return -1;
1177 for (ife = int_list; ife; ife = ife->next) {
1178 int err = doit(ife, cookie);
1179
1180 if (err)
1181 return err;
1182 }
1183 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001184}
1185
1186/* Support for fetching an IPX address */
1187
1188#if HAVE_AFIPX
1189static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1190{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001191 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1192 return ioctl(sock, SIOCGIFADDR, ifr);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001193}
1194#endif
1195
1196
1197/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001198static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001199{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001200 struct ifreq ifr;
1201 int fd;
1202 char *ifname = ife->name;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001203
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001204 strcpy(ifr.ifr_name, ifname);
1205 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1206 return (-1);
1207 ife->flags = ifr.ifr_flags;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001208
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001209 strcpy(ifr.ifr_name, ifname);
1210 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1211 memset(ife->hwaddr, 0, 32);
1212 else
1213 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001214
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001215 ife->type = ifr.ifr_hwaddr.sa_family;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001216
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001217 strcpy(ifr.ifr_name, ifname);
1218 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1219 ife->metric = 0;
1220 else
1221 ife->metric = ifr.ifr_metric;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001222
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001223 strcpy(ifr.ifr_name, ifname);
1224 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1225 ife->mtu = 0;
1226 else
1227 ife->mtu = ifr.ifr_mtu;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001228
1229#ifdef HAVE_HWSLIP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001230 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1231 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1232 ife->type == ARPHRD_ADAPT) {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001233#ifdef SIOCGOUTFILL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001234 strcpy(ifr.ifr_name, ifname);
1235 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1236 ife->outfill = 0;
1237 else
1238 ife->outfill = (unsigned int) ifr.ifr_data;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001239#endif
1240#ifdef SIOCGKEEPALIVE
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001241 strcpy(ifr.ifr_name, ifname);
1242 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1243 ife->keepalive = 0;
1244 else
1245 ife->keepalive = (unsigned int) ifr.ifr_data;
1246#endif
1247 }
1248#endif
1249
Eric Andersenf15d4da2001-03-06 00:48:59 +00001250 strcpy(ifr.ifr_name, ifname);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001251 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1252 memset(&ife->map, 0, sizeof(struct ifmap));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001253 else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001254 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001255
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001256 strcpy(ifr.ifr_name, ifname);
1257 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1258 memset(&ife->map, 0, sizeof(struct ifmap));
1259 else
1260 ife->map = ifr.ifr_map;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001261
1262#ifdef HAVE_TXQUEUELEN
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001263 strcpy(ifr.ifr_name, ifname);
1264 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1265 ife->tx_queue_len = -1; /* unknown value */
1266 else
1267 ife->tx_queue_len = ifr.ifr_qlen;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001268#else
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001269 ife->tx_queue_len = -1; /* unknown value */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001270#endif
1271
1272#if HAVE_AFINET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001273 /* IPv4 address? */
1274 fd = get_socket_for_af(AF_INET);
1275 if (fd >= 0) {
1276 strcpy(ifr.ifr_name, ifname);
1277 ifr.ifr_addr.sa_family = AF_INET;
1278 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1279 ife->has_ip = 1;
1280 ife->addr = ifr.ifr_addr;
1281 strcpy(ifr.ifr_name, ifname);
1282 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1283 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1284 else
1285 ife->dstaddr = ifr.ifr_dstaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001286
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001287 strcpy(ifr.ifr_name, ifname);
1288 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1289 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1290 else
1291 ife->broadaddr = ifr.ifr_broadaddr;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001292
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001293 strcpy(ifr.ifr_name, ifname);
1294 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1295 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1296 else
1297 ife->netmask = ifr.ifr_netmask;
1298 } else
1299 memset(&ife->addr, 0, sizeof(struct sockaddr));
1300 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001301#endif
1302
1303#if HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001304 /* DDP address maybe ? */
1305 fd = get_socket_for_af(AF_APPLETALK);
1306 if (fd >= 0) {
1307 strcpy(ifr.ifr_name, ifname);
1308 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1309 ife->ddpaddr = ifr.ifr_addr;
1310 ife->has_ddp = 1;
1311 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001312 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001313#endif
1314
1315#if HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001316 /* Look for IPX addresses with all framing types */
1317 fd = get_socket_for_af(AF_IPX);
1318 if (fd >= 0) {
1319 strcpy(ifr.ifr_name, ifname);
1320 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1321 ife->has_ipx_bb = 1;
1322 ife->ipxaddr_bb = ifr.ifr_addr;
1323 }
1324 strcpy(ifr.ifr_name, ifname);
1325 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1326 ife->has_ipx_sn = 1;
1327 ife->ipxaddr_sn = ifr.ifr_addr;
1328 }
1329 strcpy(ifr.ifr_name, ifname);
1330 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1331 ife->has_ipx_e3 = 1;
1332 ife->ipxaddr_e3 = ifr.ifr_addr;
1333 }
1334 strcpy(ifr.ifr_name, ifname);
1335 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1336 ife->has_ipx_e2 = 1;
1337 ife->ipxaddr_e2 = ifr.ifr_addr;
1338 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001339 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001340#endif
1341
1342#if HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001343 /* Econet address maybe? */
1344 fd = get_socket_for_af(AF_ECONET);
1345 if (fd >= 0) {
1346 strcpy(ifr.ifr_name, ifname);
1347 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1348 ife->ecaddr = ifr.ifr_addr;
1349 ife->has_econet = 1;
1350 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001351 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001352#endif
1353
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001354 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001355}
1356
1357
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001358static int do_if_fetch(struct interface *ife)
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001359{
1360 if (if_fetch(ife) < 0) {
1361 char *errmsg;
1362
1363 if (errno == ENODEV) {
1364 /* Give better error message for this case. */
1365 errmsg = _("Device not found");
1366 } else {
1367 errmsg = strerror(errno);
1368 }
Glenn L McGrath642f2892002-11-28 10:20:45 +00001369 error_msg(_("%s: error fetching interface information: %s\n"),
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001370 ife->name, errmsg);
1371 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001372 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001373 return 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001374}
1375
1376/* This structure defines hardware protocols and their handlers. */
1377struct hwtype {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001378 const char *name;
1379 const char *title;
1380 int type;
1381 int alen;
1382 char *(*print) (unsigned char *);
1383 int (*input) (char *, struct sockaddr *);
1384 int (*activate) (int fd);
1385 int suppress_null_addr;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001386};
1387
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001388static struct hwtype unspec_hwtype = {
1389 "unspec", "UNSPEC", -1, 0,
1390 UNSPEC_print, NULL, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001391};
1392
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001393static struct hwtype loop_hwtype = {
1394 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1395 NULL, NULL, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001396};
1397
1398#if HAVE_HWETHER
1399#include <net/if_arp.h>
1400#include <linux/if_ether.h>
1401
Eric Andersenf15d4da2001-03-06 00:48:59 +00001402/* Display an Ethernet address in readable format. */
1403static char *pr_ether(unsigned char *ptr)
1404{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001405 static char buff[64];
Eric Andersenf15d4da2001-03-06 00:48:59 +00001406
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001407 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1408 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1409 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1410 );
1411 return (buff);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001412}
1413
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001414#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001415/* Input an Ethernet address and convert to binary. */
1416static int in_ether(char *bufp, struct sockaddr *sap)
1417{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001418 unsigned char *ptr;
1419 char c, *orig;
1420 int i;
1421 unsigned val;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001422
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001423 sap->sa_family = ether_hwtype.type;
1424 ptr = sap->sa_data;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001425
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001426 i = 0;
1427 orig = bufp;
1428 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1429 val = 0;
1430 c = *bufp++;
1431 if (isdigit(c))
1432 val = c - '0';
1433 else if (c >= 'a' && c <= 'f')
1434 val = c - 'a' + 10;
1435 else if (c >= 'A' && c <= 'F')
1436 val = c - 'A' + 10;
1437 else {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001438#ifdef DEBUG
Glenn L McGrath642f2892002-11-28 10:20:45 +00001439 error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001440#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001441 errno = EINVAL;
1442 return (-1);
1443 }
1444 val <<= 4;
1445 c = *bufp;
1446 if (isdigit(c))
1447 val |= c - '0';
1448 else if (c >= 'a' && c <= 'f')
1449 val |= c - 'a' + 10;
1450 else if (c >= 'A' && c <= 'F')
1451 val |= c - 'A' + 10;
1452 else if (c == ':' || c == 0)
1453 val >>= 4;
1454 else {
1455#ifdef DEBUG
Glenn L McGrath642f2892002-11-28 10:20:45 +00001456 error_msg(_("in_ether(%s): invalid ether address!"), orig);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001457#endif
1458 errno = EINVAL;
1459 return (-1);
1460 }
1461 if (c != 0)
1462 bufp++;
1463 *ptr++ = (unsigned char) (val & 0377);
1464 i++;
1465
1466 /* We might get a semicolon here - not required. */
1467 if (*bufp == ':') {
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001468#ifdef DEBUG
Glenn L McGrath642f2892002-11-28 10:20:45 +00001469 if (i == ETH_ALEN) {
1470 error_msg(_("in_ether(%s): trailing : ignored!"), orig);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001471 }
Glenn L McGrath642f2892002-11-28 10:20:45 +00001472#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001473 bufp++;
1474 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001475 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001476
Glenn L McGrath642f2892002-11-28 10:20:45 +00001477#ifdef DEBUG
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001478 /* That's it. Any trailing junk? */
1479 if ((i == ETH_ALEN) && (*bufp != '\0')) {
Glenn L McGrath642f2892002-11-28 10:20:45 +00001480 error_msg(_("in_ether(%s): trailing junk!"), orig);
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001481 errno = EINVAL;
1482 return (-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001483 }
Glenn L McGrath642f2892002-11-28 10:20:45 +00001484 error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001485#endif
1486
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001487 return (0);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001488}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001489#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001490
1491
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001492static struct hwtype ether_hwtype = {
1493 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1494 pr_ether, NULL /* UNUSED in_ether */ , NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001495};
1496
1497
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001498#endif /* HAVE_HWETHER */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001499
1500
1501#if HAVE_HWPPP
1502
1503#include <net/if_arp.h>
1504
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001505#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001506/* Start the PPP encapsulation on the file descriptor. */
1507static int do_ppp(int fd)
1508{
Glenn L McGrath642f2892002-11-28 10:20:45 +00001509 error_msg(_("You cannot start PPP with this program."));
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001510 return -1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001511}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001512#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001513
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001514static struct hwtype ppp_hwtype = {
1515 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1516 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
Eric Andersenf15d4da2001-03-06 00:48:59 +00001517};
1518
1519
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001520#endif /* HAVE_PPP */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001521
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001522static struct hwtype *hwtypes[] = {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001523
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001524 &loop_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001525
1526#if HAVE_HWSLIP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001527 &slip_hwtype,
1528 &cslip_hwtype,
1529 &slip6_hwtype,
1530 &cslip6_hwtype,
1531 &adaptive_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001532#endif
1533#if HAVE_HWSTRIP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001534 &strip_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001535#endif
1536#if HAVE_HWASH
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001537 &ash_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001538#endif
1539#if HAVE_HWETHER
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001540 &ether_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001541#endif
1542#if HAVE_HWTR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001543 &tr_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001544#ifdef ARPHRD_IEEE802_TR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001545 &tr_hwtype1,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001546#endif
1547#endif
1548#if HAVE_HWAX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001549 &ax25_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001550#endif
1551#if HAVE_HWNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001552 &netrom_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001553#endif
1554#if HAVE_HWROSE
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001555 &rose_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001556#endif
1557#if HAVE_HWTUNNEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001558 &tunnel_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001559#endif
1560#if HAVE_HWPPP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001561 &ppp_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001562#endif
1563#if HAVE_HWHDLCLAPB
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001564 &hdlc_hwtype,
1565 &lapb_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001566#endif
1567#if HAVE_HWARC
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001568 &arcnet_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001569#endif
1570#if HAVE_HWFR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001571 &dlci_hwtype,
1572 &frad_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001573#endif
1574#if HAVE_HWSIT
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001575 &sit_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001576#endif
1577#if HAVE_HWFDDI
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001578 &fddi_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001579#endif
1580#if HAVE_HWHIPPI
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001581 &hippi_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001582#endif
1583#if HAVE_HWIRDA
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001584 &irda_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001585#endif
1586#if HAVE_HWEC
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001587 &ec_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001588#endif
1589#if HAVE_HWX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001590 &x25_hwtype,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001591#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001592 &unspec_hwtype,
1593 NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001594};
1595
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001596#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001597static short sVhwinit = 0;
1598
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001599static void hwinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +00001600{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001601 loop_hwtype.title = _("Local Loopback");
1602 unspec_hwtype.title = _("UNSPEC");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001603#if HAVE_HWSLIP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001604 slip_hwtype.title = _("Serial Line IP");
1605 cslip_hwtype.title = _("VJ Serial Line IP");
1606 slip6_hwtype.title = _("6-bit Serial Line IP");
1607 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1608 adaptive_hwtype.title = _("Adaptive Serial Line IP");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001609#endif
1610#if HAVE_HWETHER
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001611 ether_hwtype.title = _("Ethernet");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001612#endif
1613#if HAVE_HWASH
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001614 ash_hwtype.title = _("Ash");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001615#endif
1616#if HAVE_HWFDDI
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001617 fddi_hwtype.title = _("Fiber Distributed Data Interface");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001618#endif
1619#if HAVE_HWHIPPI
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001620 hippi_hwtype.title = _("HIPPI");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001621#endif
1622#if HAVE_HWAX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001623 ax25_hwtype.title = _("AMPR AX.25");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001624#endif
1625#if HAVE_HWROSE
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001626 rose_hwtype.title = _("AMPR ROSE");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001627#endif
1628#if HAVE_HWNETROM
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001629 netrom_hwtype.title = _("AMPR NET/ROM");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001630#endif
1631#if HAVE_HWX25
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001632 x25_hwtype.title = _("generic X.25");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001633#endif
1634#if HAVE_HWTUNNEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001635 tunnel_hwtype.title = _("IPIP Tunnel");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001636#endif
1637#if HAVE_HWPPP
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001638 ppp_hwtype.title = _("Point-to-Point Protocol");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001639#endif
1640#if HAVE_HWHDLCLAPB
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001641 hdlc_hwtype.title = _("(Cisco)-HDLC");
1642 lapb_hwtype.title = _("LAPB");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001643#endif
1644#if HAVE_HWARC
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001645 arcnet_hwtype.title = _("ARCnet");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001646#endif
1647#if HAVE_HWFR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001648 dlci_hwtype.title = _("Frame Relay DLCI");
1649 frad_hwtype.title = _("Frame Relay Access Device");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001650#endif
1651#if HAVE_HWSIT
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001652 sit_hwtype.title = _("IPv6-in-IPv4");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001653#endif
1654#if HAVE_HWIRDA
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001655 irda_hwtype.title = _("IrLAP");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001656#endif
1657#if HAVE_HWTR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001658 tr_hwtype.title = _("16/4 Mbps Token Ring");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001659#ifdef ARPHRD_IEEE802_TR
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001660 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001661#endif
1662#endif
1663#if HAVE_HWEC
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001664 ec_hwtype.title = _("Econet");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001665#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001666 sVhwinit = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001667}
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001668#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001669
1670#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001671static const char *if_port_text[][4] = {
1672 /* Keep in step with <linux/netdevice.h> */
1673 {"unknown", NULL, NULL, NULL},
1674 {"10base2", "bnc", "coax", NULL},
1675 {"10baseT", "utp", "tpe", NULL},
1676 {"AUI", "thick", "db15", NULL},
1677 {"100baseT", NULL, NULL, NULL},
1678 {"100baseTX", NULL, NULL, NULL},
1679 {"100baseFX", NULL, NULL, NULL},
1680 {NULL, NULL, NULL, NULL},
Eric Andersenf15d4da2001-03-06 00:48:59 +00001681};
1682#endif
1683
1684/* Check our hardware type table for this type. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001685static struct hwtype *get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001686{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001687 struct hwtype **hwp;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001688
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001689#ifdef KEEP_UNUSED
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001690 if (!sVhwinit)
1691 hwinit();
1692#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001693
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001694 hwp = hwtypes;
1695 while (*hwp != NULL) {
1696 if ((*hwp)->type == type)
1697 return (*hwp);
1698 hwp++;
1699 }
1700 return (NULL);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001701}
1702
1703/* return 1 if address is all zeros */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001704static int hw_null_address(struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001705{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001706 unsigned int i;
1707 unsigned char *address = (unsigned char *) ap;
1708
1709 for (i = 0; i < hw->alen; i++)
1710 if (address[i])
1711 return 0;
1712 return 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001713}
1714
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001715static const char TRext[] = "\0\0k\0M";
1716
1717static void print_bytes_scaled(unsigned long long ull, const char *end)
1718{
1719 unsigned long long int_part;
1720 unsigned long frac_part;
1721 const char *ext;
1722 int i;
1723
1724 frac_part = 0;
1725 ext = TRext;
1726 int_part = ull;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001727 for (i = 0; i < 2; i++) {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001728 if (int_part >= 1024) {
1729 frac_part = ((int_part % 1024) * 10) / 1024;
1730 int_part /= 1024;
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001731 ext += 2; /* Kb, Mb */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001732 }
1733 }
1734
Eric Andersen7365c582002-09-17 06:36:56 +00001735 printf("X bytes:%Lu (%Lu.%lu %siB)%s", ull, int_part, frac_part, ext, end);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001736}
1737
1738static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001739{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001740 struct aftype *ap;
1741 struct hwtype *hw;
1742 int hf;
1743 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001744
1745#if HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001746 static struct aftype *ipxtype = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001747#endif
1748#if HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001749 static struct aftype *ectype = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001750#endif
1751#if HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001752 static struct aftype *ddptype = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001753#endif
1754#if HAVE_AFINET6
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001755 FILE *f;
1756 char addr6[40], devname[20];
1757 struct sockaddr_in6 sap;
1758 int plen, scope, dad_status, if_idx;
1759 char addr6p[8][5];
Eric Andersenf15d4da2001-03-06 00:48:59 +00001760#endif
1761
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001762 ap = get_afntype(ptr->addr.sa_family);
1763 if (ap == NULL)
1764 ap = get_afntype(0);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001765
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001766 hf = ptr->type;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001767
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001768 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1769 can_compress = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001770
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001771 hw = get_hwntype(hf);
1772 if (hw == NULL)
1773 hw = get_hwntype(-1);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001774
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001775 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1776 /* For some hardware types (eg Ash, ATM) we don't print the
1777 hardware address if it's null. */
1778 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1779 hw->suppress_null_addr)))
1780 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001781#ifdef IFF_PORTSEL
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001782 if (ptr->flags & IFF_PORTSEL) {
1783 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1784 if (ptr->flags & IFF_AUTOMEDIA)
1785 printf(_("(auto)"));
1786 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001787#endif
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001788 printf("\n");
Eric Andersenf15d4da2001-03-06 00:48:59 +00001789
1790#if HAVE_AFINET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001791 if (ptr->has_ip) {
1792 printf(_(" %s addr:%s "), ap->name,
1793 ap->sprint(&ptr->addr, 1));
1794 if (ptr->flags & IFF_POINTOPOINT) {
1795 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1796 }
1797 if (ptr->flags & IFF_BROADCAST) {
1798 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1799 }
1800 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001801 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001802#endif
1803
1804#if HAVE_AFINET6
Eric Andersenf15d4da2001-03-06 00:48:59 +00001805
Eric Andersen51b8bd62002-07-03 11:46:38 +00001806#define IPV6_ADDR_ANY 0x0000U
1807
1808#define IPV6_ADDR_UNICAST 0x0001U
1809#define IPV6_ADDR_MULTICAST 0x0002U
1810#define IPV6_ADDR_ANYCAST 0x0004U
1811
1812#define IPV6_ADDR_LOOPBACK 0x0010U
1813#define IPV6_ADDR_LINKLOCAL 0x0020U
1814#define IPV6_ADDR_SITELOCAL 0x0040U
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001815
Eric Andersen51b8bd62002-07-03 11:46:38 +00001816#define IPV6_ADDR_COMPATv4 0x0080U
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001817
Eric Andersen51b8bd62002-07-03 11:46:38 +00001818#define IPV6_ADDR_SCOPE_MASK 0x00f0U
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001819
Eric Andersen51b8bd62002-07-03 11:46:38 +00001820#define IPV6_ADDR_MAPPED 0x1000U
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001821#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1822
1823 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1824 while (fscanf
1825 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1826 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1827 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1828 &dad_status, devname) != EOF) {
1829 if (!strcmp(devname, ptr->name)) {
1830 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1831 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1832 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1833 inet_pton(AF_INET6, addr6,
1834 (struct sockaddr *) &sap.sin6_addr);
1835 sap.sin6_family = AF_INET6;
1836 printf(_(" inet6 addr: %s/%d"),
1837 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1838 plen);
1839 printf(_(" Scope:"));
1840 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1841 case 0:
1842 printf(_("Global"));
1843 break;
1844 case IPV6_ADDR_LINKLOCAL:
1845 printf(_("Link"));
1846 break;
1847 case IPV6_ADDR_SITELOCAL:
1848 printf(_("Site"));
1849 break;
1850 case IPV6_ADDR_COMPATv4:
1851 printf(_("Compat"));
1852 break;
1853 case IPV6_ADDR_LOOPBACK:
1854 printf(_("Host"));
1855 break;
1856 default:
1857 printf(_("Unknown"));
1858 }
1859 printf("\n");
1860 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001861 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001862 fclose(f);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001863 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001864#endif
1865
1866#if HAVE_AFIPX
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001867 if (ipxtype == NULL)
1868 ipxtype = get_afntype(AF_IPX);
Eric Andersenf15d4da2001-03-06 00:48:59 +00001869
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001870 if (ipxtype != NULL) {
1871 if (ptr->has_ipx_bb)
1872 printf(_(" IPX/Ethernet II addr:%s\n"),
1873 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1874 if (ptr->has_ipx_sn)
1875 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1876 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1877 if (ptr->has_ipx_e2)
1878 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1879 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1880 if (ptr->has_ipx_e3)
1881 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1882 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1883 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001884#endif
1885
1886#if HAVE_AFATALK
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001887 if (ddptype == NULL)
1888 ddptype = get_afntype(AF_APPLETALK);
1889 if (ddptype != NULL) {
1890 if (ptr->has_ddp)
1891 printf(_(" EtherTalk Phase 2 addr:%s\n"),
1892 ddptype->sprint(&ptr->ddpaddr, 1));
1893 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00001894#endif
1895
1896#if HAVE_AFECONET
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001897 if (ectype == NULL)
1898 ectype = get_afntype(AF_ECONET);
1899 if (ectype != NULL) {
1900 if (ptr->has_econet)
1901 printf(_(" econet addr:%s\n"),
1902 ectype->sprint(&ptr->ecaddr, 1));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001903 }
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001904#endif
1905
1906 printf(" ");
1907 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1908 if (ptr->flags == 0)
1909 printf(_("[NO FLAGS] "));
1910 if (ptr->flags & IFF_UP)
1911 printf(_("UP "));
1912 if (ptr->flags & IFF_BROADCAST)
1913 printf(_("BROADCAST "));
1914 if (ptr->flags & IFF_DEBUG)
1915 printf(_("DEBUG "));
1916 if (ptr->flags & IFF_LOOPBACK)
1917 printf(_("LOOPBACK "));
1918 if (ptr->flags & IFF_POINTOPOINT)
1919 printf(_("POINTOPOINT "));
1920 if (ptr->flags & IFF_NOTRAILERS)
1921 printf(_("NOTRAILERS "));
1922 if (ptr->flags & IFF_RUNNING)
1923 printf(_("RUNNING "));
1924 if (ptr->flags & IFF_NOARP)
1925 printf(_("NOARP "));
1926 if (ptr->flags & IFF_PROMISC)
1927 printf(_("PROMISC "));
1928 if (ptr->flags & IFF_ALLMULTI)
1929 printf(_("ALLMULTI "));
1930 if (ptr->flags & IFF_SLAVE)
1931 printf(_("SLAVE "));
1932 if (ptr->flags & IFF_MASTER)
1933 printf(_("MASTER "));
1934 if (ptr->flags & IFF_MULTICAST)
1935 printf(_("MULTICAST "));
1936#ifdef HAVE_DYNAMIC
1937 if (ptr->flags & IFF_DYNAMIC)
1938 printf(_("DYNAMIC "));
1939#endif
1940 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1941 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1942#ifdef SIOCSKEEPALIVE
1943 if (ptr->outfill || ptr->keepalive)
1944 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1945#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +00001946 printf("\n");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00001947
1948 /* If needed, display the interface statistics. */
1949
1950 if (ptr->statistics_valid) {
1951 /* XXX: statistics are currently only printed for the primary address,
1952 * not for the aliases, although strictly speaking they're shared
1953 * by all addresses.
1954 */
1955 printf(" ");
1956
1957 printf(_
1958 ("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1959 ptr->stats.rx_packets, ptr->stats.rx_errors,
1960 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1961 ptr->stats.rx_frame_errors);
1962 if (can_compress)
1963 printf(_(" compressed:%lu\n"),
1964 ptr->stats.rx_compressed);
1965 printf(" ");
1966 printf(_
1967 ("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1968 ptr->stats.tx_packets, ptr->stats.tx_errors,
1969 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1970 ptr->stats.tx_carrier_errors);
1971 printf(_(" collisions:%lu "), ptr->stats.collisions);
1972 if (can_compress)
1973 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1974 if (ptr->tx_queue_len != -1)
1975 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1976 printf("\n R");
1977 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1978 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1979
1980 }
1981
1982 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1983 ptr->map.base_addr)) {
1984 printf(" ");
1985 if (ptr->map.irq)
1986 printf(_("Interrupt:%d "), ptr->map.irq);
1987 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
1988 I/O maps */
1989 printf(_("Base address:0x%lx "),
1990 (unsigned long) ptr->map.base_addr);
1991 if (ptr->map.mem_start) {
1992 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
1993 ptr->map.mem_end);
1994 }
1995 if (ptr->map.dma)
1996 printf(_("DMA chan:%x "), ptr->map.dma);
1997 printf("\n");
1998 }
1999 printf("\n");
Eric Andersenf15d4da2001-03-06 00:48:59 +00002000}
2001
2002
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002003static int do_if_print(struct interface *ife, void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002004{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002005 int *opt_a = (int *) cookie;
2006 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002007
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002008 res = do_if_fetch(ife);
2009 if (res >= 0) {
2010 if ((ife->flags & IFF_UP) || *opt_a)
2011 ife_print(ife);
2012 }
2013 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002014}
2015
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002016static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002017{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002018 struct interface *ife = NULL;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002019
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002020 if (if_readlist_proc(name) < 0)
2021 return NULL;
2022 ife = add_interface(name);
2023 return ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002024}
2025
2026/* for ipv4 add/del modes */
2027static int if_print(char *ifname)
2028{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002029 int res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002030
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002031 if (!ifname) {
2032 res = for_all_interfaces(do_if_print, &interface_opt_a);
2033 } else {
2034 struct interface *ife;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002035
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002036 ife = lookup_interface(ifname);
2037 res = do_if_fetch(ife);
2038 if (res >= 0)
2039 ife_print(ife);
2040 }
2041 return res;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002042}
2043
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002044int display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002045{
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002046 int status;
Eric Andersenf15d4da2001-03-06 00:48:59 +00002047
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002048 /* Create a channel to the NET kernel. */
2049 if ((skfd = sockets_open(0)) < 0) {
Manuel Novoa III 78f57462001-03-10 02:00:54 +00002050 perror_msg_and_die("socket");
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002051 }
Eric Andersenf15d4da2001-03-06 00:48:59 +00002052
Glenn L McGrath1ed9dd92002-08-22 19:35:36 +00002053 /* Do we have to show the current setup? */
2054 status = if_print(ifname);
2055 close(skfd);
2056 exit(status < 0);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002057}