blob: 8eb03a61c30f2b79ff439549648848be2dd73623 [file] [log] [blame]
Eric Andersenf15d4da2001-03-06 00:48:59 +00001/*
2 * ifconfig This file contains an implementation of the command
3 * that either displays or sets the characteristics of
4 * one or more of the system's networking interfaces.
5 *
Eric Andersen8b113f92001-06-01 21:47:15 +00006 * Version: $Id: interface.c,v 1.3 2001/06/01 21:47:15 andersen Exp $
Eric Andersenf15d4da2001-03-06 00:48:59 +00007 *
8 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
9 * and others. Copyright 1993 MicroWalt Corporation
10 *
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * Patched to support 'add' and 'del' keywords for INET(4) addresses
18 * by Mrs. Brisby <mrs.brisby@nimh.org>
19 *
20 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21 * - gettext instead of catgets for i18n
22 * 10/1998 - Andi Kleen. Use interface list primitives.
23 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
24 * (default AF was wrong)
25 * stolen from net-tools-1.59 and stripped down for busybox by
26 * Erik Andersen <andersee@debian.org>
27 */
28
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000029/*
30 * Heavily modified by Manuel Novoa III Mar 12, 2001
31 *
32 * Pruned unused code using KEEP_UNUSED define.
33 * Added print_bytes_scaled function to reduce code size.
34 * Added some (potentially) missing defines.
35 * Improved display support for -a and for a named interface.
36 */
37
38/* #define KEEP_UNUSED */
39
Eric Andersenf15d4da2001-03-06 00:48:59 +000040/*
41 *
42 * Protocol Families.
43 *
44 */
45#define HAVE_AFINET 1
46#undef HAVE_AFINET6
47#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
54/*
55 *
56 * Device Hardware types.
57 *
58 */
59#define HAVE_HWETHER 1
60#define HAVE_HWPPP 1
61#undef HAVE_HWSLIP
62
63
64#include <features.h>
65#include <sys/types.h>
66#include <sys/socket.h>
67#include <sys/ioctl.h>
68#include <netinet/in.h>
69#include <net/if.h>
70#include <net/if_arp.h>
71#include <stdio.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <ctype.h>
75#include <stdlib.h>
76#include <string.h>
77#include <unistd.h>
78#include <netdb.h>
79#include <netinet/in.h>
80#include <arpa/inet.h>
81#include <arpa/nameser.h>
Glenn L McGrath6b8c5502001-05-05 03:19:12 +000082#include "libbb.h"
Eric Andersenf15d4da2001-03-06 00:48:59 +000083
84#define _(x) x
85#define _PATH_PROCNET_DEV "/proc/net/dev"
86#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
91
92/* Ugh. But libc5 doesn't provide POSIX types. */
93#include <asm/types.h>
94
95
96#ifdef HAVE_HWSLIP
97#include <linux/if_slip.h>
98#endif
99
100#if HAVE_AFINET6
101
102#ifndef _LINUX_IN6_H
103/*
104 * This is in linux/include/net/ipv6.h.
105 */
106
107struct in6_ifreq {
108 struct in6_addr ifr6_addr;
109 __u32 ifr6_prefixlen;
110 unsigned int ifr6_ifindex;
111};
112
113#endif
114
115#endif /* HAVE_AFINET6 */
116
117#if HAVE_AFIPX
Eric Andersenb6b519b2001-04-09 23:52:18 +0000118#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000119#include <netipx/ipx.h>
120#else
121#include "ipx.h"
122#endif
123#endif
124#if 0
125#include "net-support.h"
126#include "pathnames.h"
127#include "version.h"
128#include "../intl.h"
129#include "interface.h"
130#include "sockets.h"
131#include "util.h"
132#endif
133
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000134/* Defines for glibc2.0 users. */
135#ifndef SIOCSIFTXQLEN
136#define SIOCSIFTXQLEN 0x8943
137#define SIOCGIFTXQLEN 0x8942
138#endif
139
140/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
141#ifndef ifr_qlen
142#define ifr_qlen ifr_ifru.ifru_mtu
143#endif
144
145#ifndef HAVE_TXQUEUELEN
146#define HAVE_TXQUEUELEN 1
147#endif
148
149#ifndef IFF_DYNAMIC
150#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
151#endif
152
Eric Andersenf15d4da2001-03-06 00:48:59 +0000153/* This structure defines protocol families and their handlers. */
154struct aftype {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000155 const char *name;
156 const char *title;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000157 int af;
158 int alen;
159 char *(*print) (unsigned char *);
160 char *(*sprint) (struct sockaddr *, int numeric);
161 int (*input) (int type, char *bufp, struct sockaddr *);
162 void (*herror) (char *text);
163 int (*rprint) (int options);
164 int (*rinput) (int typ, int ext, char **argv);
165
166 /* may modify src */
167 int (*getmask) (char *src, struct sockaddr * mask, char *name);
168
169 int fd;
170 char *flag_file;
171};
172
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000173static struct aftype *aftypes[];
174
175#ifdef KEEP_UNUSED
176
177static int flag_unx;
Eric Andersen8b113f92001-06-01 21:47:15 +0000178#ifdef HAVE_AFIPX
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000179static int flag_ipx;
Eric Andersen8b113f92001-06-01 21:47:15 +0000180#endif
181#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000182static int flag_ax25;
Eric Andersen8b113f92001-06-01 21:47:15 +0000183#endif
184#ifdef HAVE_AFATALK
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000185static int flag_ddp;
Eric Andersen8b113f92001-06-01 21:47:15 +0000186#endif
187#ifdef HAVE_AFNETROM
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000188static int flag_netrom;
Eric Andersen8b113f92001-06-01 21:47:15 +0000189#endif
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000190static int flag_inet;
Eric Andersen8b113f92001-06-01 21:47:15 +0000191#ifdef HAVE_AFINET6
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000192static int flag_inet6;
Eric Andersen8b113f92001-06-01 21:47:15 +0000193#endif
194#ifdef HAVE_AFECONET
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000195static int flag_econet;
Eric Andersen8b113f92001-06-01 21:47:15 +0000196#endif
197#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000198static int flag_x25 = 0;
Eric Andersen8b113f92001-06-01 21:47:15 +0000199#endif
200#ifdef HAVE_AFASH
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000201static int flag_ash;
Eric Andersen8b113f92001-06-01 21:47:15 +0000202#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000203
204
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000205static struct aftrans_t {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000206 char *alias;
207 char *name;
208 int *flag;
209} aftrans[] = {
210
Eric Andersen8b113f92001-06-01 21:47:15 +0000211#ifdef HAVE_AFX25
Eric Andersenf15d4da2001-03-06 00:48:59 +0000212 {
213 "ax25", "ax25", &flag_ax25
214 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000215#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000216 {
217 "ip", "inet", &flag_inet
218 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000219#ifdef HAVE_AFINET6
Eric Andersenf15d4da2001-03-06 00:48:59 +0000220 {
221 "ip6", "inet6", &flag_inet6
222 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000223#endif
224#ifdef HAVE_AFIPX
Eric Andersenf15d4da2001-03-06 00:48:59 +0000225 {
226 "ipx", "ipx", &flag_ipx
227 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000228#endif
229#ifdef HAVE_AFATALK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000230 {
231 "appletalk", "ddp", &flag_ddp
232 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000233#endif
234#ifdef HAVE_AFNETROM
Eric Andersenf15d4da2001-03-06 00:48:59 +0000235 {
236 "netrom", "netrom", &flag_netrom
237 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000238#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000239 {
240 "inet", "inet", &flag_inet
241 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000242#ifdef HAVE_AFINET6
Eric Andersenf15d4da2001-03-06 00:48:59 +0000243 {
244 "inet6", "inet6", &flag_inet6
245 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000246#endif
247#ifdef HAVE_AFATALK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000248 {
249 "ddp", "ddp", &flag_ddp
250 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000251#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000252 {
253 "unix", "unix", &flag_unx
254 },
255 {
256 "tcpip", "inet", &flag_inet
257 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000258#ifdef HAVE_AFECONET
Eric Andersenf15d4da2001-03-06 00:48:59 +0000259 {
260 "econet", "ec", &flag_econet
261 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000262#endif
263#ifdef HAVE_AFX25
Eric Andersenf15d4da2001-03-06 00:48:59 +0000264 {
265 "x25", "x25", &flag_x25
266 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000267#endif
268#ifdef HAVE_AFASH
Eric Andersenf15d4da2001-03-06 00:48:59 +0000269 {
270 "ash", "ash", &flag_ash
271 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000272#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000273 {
274 0, 0, 0
275 }
276};
277
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000278static char afname[256] = "";
279#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000280
281#if HAVE_AFUNIX
282
283/* Display a UNIX domain address. */
284static char *UNIX_print(unsigned char *ptr)
285{
286 return (ptr);
287}
288
289
290/* Display a UNIX domain address. */
291static char *UNIX_sprint(struct sockaddr *sap, int numeric)
292{
293 static char buf[64];
294
295 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
296 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
297 return (UNIX_print(sap->sa_data));
298}
299
300
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000301static struct aftype unix_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000302{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000303 "unix", "UNIX Domain", AF_UNIX, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000304 UNIX_print, UNIX_sprint, NULL, NULL,
305 NULL, NULL, NULL,
306 -1,
307 "/proc/net/unix"
308};
309#endif /* HAVE_AFUNIX */
310
311#if HAVE_AFINET
312
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000313#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000314extern int h_errno; /* some netdb.h versions don't export this */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000315#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000316
317/* cache */
318struct addr {
319 struct sockaddr_in addr;
320 char *name;
321 int host;
322 struct addr *next;
323};
324
325static struct addr *INET_nn = NULL; /* addr-to-name cache */
326
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000327#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000328static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
329{
330 struct hostent *hp;
331 struct netent *np;
332
333 /* Grmpf. -FvK */
334 sin->sin_family = AF_INET;
335 sin->sin_port = 0;
336
337 /* Default is special, meaning 0.0.0.0. */
338 if (!strcmp(name, "default")) {
339 sin->sin_addr.s_addr = INADDR_ANY;
340 return (1);
341 }
342 /* Look to see if it's a dotted quad. */
343 if (inet_aton(name, &sin->sin_addr)) {
344 return 0;
345 }
346 /* If we expect this to be a hostname, try hostname database first */
347#ifdef DEBUG
348 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
349#endif
350 if (hostfirst &&
351 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
352 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
353 sizeof(struct in_addr));
354 return 0;
355 }
356 /* Try the NETWORKS database to see if this is a known network. */
357#ifdef DEBUG
358 fprintf (stderr, "getnetbyname (%s)\n", name);
359#endif
360 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
361 sin->sin_addr.s_addr = htonl(np->n_net);
362 return 1;
363 }
364 if (hostfirst) {
365 /* Don't try again */
366 errno = h_errno;
367 return -1;
368 }
369#ifdef DEBUG
370 res_init();
371 _res.options |= RES_DEBUG;
372#endif
373
374#ifdef DEBUG
375 fprintf (stderr, "gethostbyname (%s)\n", name);
376#endif
377 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
378 errno = h_errno;
379 return -1;
380 }
381 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
382 sizeof(struct in_addr));
383
384 return 0;
385}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000386#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000387
388/* numeric: & 0x8000: default instead of *,
389 * & 0x4000: host instead of net,
390 * & 0x0fff: don't resolve
391 */
Eric Andersen46cd74b2001-04-19 16:55:27 +0000392static int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000393 int numeric, unsigned int netmask)
394{
395 struct hostent *ent;
396 struct netent *np;
397 struct addr *pn;
398 unsigned long ad, host_ad;
399 int host = 0;
400
401 /* Grmpf. -FvK */
Eric Andersen46cd74b2001-04-19 16:55:27 +0000402 if (s_in->sin_family != AF_INET) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000403#ifdef DEBUG
Eric Andersen46cd74b2001-04-19 16:55:27 +0000404 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000405#endif
406 errno = EAFNOSUPPORT;
407 return (-1);
408 }
Eric Andersen46cd74b2001-04-19 16:55:27 +0000409 ad = (unsigned long) s_in->sin_addr.s_addr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000410#ifdef DEBUG
411 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
412#endif
413 if (ad == INADDR_ANY) {
414 if ((numeric & 0x0FFF) == 0) {
415 if (numeric & 0x8000)
416 safe_strncpy(name, "default", len);
417 else
418 safe_strncpy(name, "*", len);
419 return (0);
420 }
421 }
422 if (numeric & 0x0FFF) {
Eric Andersen46cd74b2001-04-19 16:55:27 +0000423 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000424 return (0);
425 }
426
427 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
428 host = 1;
429#if 0
430 INET_nn = NULL;
431#endif
432 pn = INET_nn;
433 while (pn != NULL) {
434 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
435 safe_strncpy(name, pn->name, len);
436#ifdef DEBUG
437 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
438#endif
439 return (0);
440 }
441 pn = pn->next;
442 }
443
444 host_ad = ntohl(ad);
445 np = NULL;
446 ent = NULL;
447 if (host) {
448#ifdef DEBUG
449 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
450#endif
451 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
452 if (ent != NULL)
453 safe_strncpy(name, ent->h_name, len);
454 } else {
455#ifdef DEBUG
456 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
457#endif
458 np = getnetbyaddr(host_ad, AF_INET);
459 if (np != NULL)
460 safe_strncpy(name, np->n_name, len);
461 }
462 if ((ent == NULL) && (np == NULL))
Eric Andersen46cd74b2001-04-19 16:55:27 +0000463 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Eric Andersen0f430e32001-03-06 20:54:43 +0000464 pn = (struct addr *) xmalloc(sizeof(struct addr));
Eric Andersen46cd74b2001-04-19 16:55:27 +0000465 pn->addr = *s_in;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000466 pn->next = INET_nn;
467 pn->host = host;
Eric Andersen0f430e32001-03-06 20:54:43 +0000468 pn->name = (char *) xmalloc(strlen(name) + 1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000469 strcpy(pn->name, name);
470 INET_nn = pn;
471
472 return (0);
473}
474
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000475#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000476static void INET_reserror(char *text)
477{
478 herror(text);
479}
480
Eric Andersenf15d4da2001-03-06 00:48:59 +0000481/* Display an Internet socket address. */
482static char *INET_print(unsigned char *ptr)
483{
484 return (inet_ntoa((*(struct in_addr *) ptr)));
485}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000486#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000487
488/* Display an Internet socket address. */
489static char *INET_sprint(struct sockaddr *sap, int numeric)
490{
491 static char buff[128];
492
493 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
494 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
495
496 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
497 numeric, 0xffffff00) != 0)
498 return (NULL);
499
500 return (buff);
501}
502
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000503#ifdef KEEP_UNUSED
504static char *INET_sprintmask(struct sockaddr *sap, int numeric,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000505 unsigned int netmask)
506{
507 static char buff[128];
508
509 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
510 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
511 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
512 numeric, netmask) != 0)
513 return (NULL);
514 return (buff);
515}
516
Eric Andersenf15d4da2001-03-06 00:48:59 +0000517static int INET_getsock(char *bufp, struct sockaddr *sap)
518{
519 char *sp = bufp, *bp;
520 unsigned int i;
521 unsigned val;
522 struct sockaddr_in *sin;
523
524 sin = (struct sockaddr_in *) sap;
525 sin->sin_family = AF_INET;
526 sin->sin_port = 0;
527
528 val = 0;
529 bp = (char *) &val;
530 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
531 *sp = toupper(*sp);
532
533 if ((*sp >= 'A') && (*sp <= 'F'))
534 bp[i] |= (int) (*sp - 'A') + 10;
535 else if ((*sp >= '0') && (*sp <= '9'))
536 bp[i] |= (int) (*sp - '0');
537 else
538 return (-1);
539
540 bp[i] <<= 4;
541 sp++;
542 *sp = toupper(*sp);
543
544 if ((*sp >= 'A') && (*sp <= 'F'))
545 bp[i] |= (int) (*sp - 'A') + 10;
546 else if ((*sp >= '0') && (*sp <= '9'))
547 bp[i] |= (int) (*sp - '0');
548 else
549 return (-1);
550
551 sp++;
552 }
553 sin->sin_addr.s_addr = htonl(val);
554
555 return (sp - bufp);
556}
557
558static int INET_input(int type, char *bufp, struct sockaddr *sap)
559{
560 switch (type) {
561 case 1:
562 return (INET_getsock(bufp, sap));
563 case 256:
564 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
565 default:
566 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
567 }
568}
569
570static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
571{
572 struct sockaddr_in *mask = (struct sockaddr_in *) m;
573 char *slash, *end;
574 int prefix;
575
576 if ((slash = strchr(adr, '/')) == NULL)
577 return 0;
578
579 *slash++ = '\0';
580 prefix = strtoul(slash, &end, 0);
581 if (*end != '\0')
582 return -1;
583
584 if (name) {
585 sprintf(name, "/%d", prefix);
586 }
587 mask->sin_family = AF_INET;
588 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
589 return 1;
590}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000591#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000592
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000593static struct aftype inet_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000594{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000595 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
596 NULL /* UNUSED INET_print */, INET_sprint,
597 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000598 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000599 NULL /* UNUSED INET_getnetmask */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000600 -1,
601 NULL
602};
603
604#endif /* HAVE_AFINET */
605
606/* Display an UNSPEC address. */
607static char *UNSPEC_print(unsigned char *ptr)
608{
Eric Andersen46cd74b2001-04-19 16:55:27 +0000609 static char buff[sizeof(struct sockaddr)*3+1];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000610 char *pos;
611 unsigned int i;
612
613 pos = buff;
614 for (i = 0; i < sizeof(struct sockaddr); i++) {
Eric Andersen46cd74b2001-04-19 16:55:27 +0000615 /* careful -- not every libc's sprintf returns # bytes written */
616 sprintf(pos, "%02X-", (*ptr++ & 0377));
617 pos += 3;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000618 }
Eric Andersen46cd74b2001-04-19 16:55:27 +0000619 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
620 *--pos = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000621 return (buff);
622}
623
624/* Display an UNSPEC socket address. */
625static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
626{
627 static char buf[64];
628
629 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
630 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
631 return (UNSPEC_print(sap->sa_data));
632}
633
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000634static struct aftype unspec_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000635{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000636 "unspec", "UNSPEC", AF_UNSPEC, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000637 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
638 NULL,
639};
640
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000641static struct aftype *aftypes[] =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000642{
643#if HAVE_AFUNIX
644 &unix_aftype,
645#endif
646#if HAVE_AFINET
647 &inet_aftype,
648#endif
649#if HAVE_AFINET6
650 &inet6_aftype,
651#endif
652#if HAVE_AFAX25
653 &ax25_aftype,
654#endif
655#if HAVE_AFNETROM
656 &netrom_aftype,
657#endif
658#if HAVE_AFROSE
659 &rose_aftype,
660#endif
661#if HAVE_AFIPX
662 &ipx_aftype,
663#endif
664#if HAVE_AFATALK
665 &ddp_aftype,
666#endif
667#if HAVE_AFECONET
668 &ec_aftype,
669#endif
670#if HAVE_AFASH
671 &ash_aftype,
672#endif
673#if HAVE_AFX25
674 &x25_aftype,
675#endif
676 &unspec_aftype,
677 NULL
678};
679
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000680#ifdef KEEP_UNUSED
681static short sVafinit = 0;
682
683static void afinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +0000684{
685 unspec_aftype.title = _("UNSPEC");
686#if HAVE_AFUNIX
687 unix_aftype.title = _("UNIX Domain");
688#endif
689#if HAVE_AFINET
690 inet_aftype.title = _("DARPA Internet");
691#endif
692#if HAVE_AFINET6
693 inet6_aftype.title = _("IPv6");
694#endif
695#if HAVE_AFAX25
696 ax25_aftype.title = _("AMPR AX.25");
697#endif
698#if HAVE_AFNETROM
699 netrom_aftype.title = _("AMPR NET/ROM");
700#endif
701#if HAVE_AFIPX
702 ipx_aftype.title = _("Novell IPX");
703#endif
704#if HAVE_AFATALK
705 ddp_aftype.title = _("Appletalk DDP");
706#endif
707#if HAVE_AFECONET
708 ec_aftype.title = _("Econet");
709#endif
710#if HAVE_AFX25
711 x25_aftype.title = _("CCITT X.25");
712#endif
713#if HAVE_AFROSE
714 rose_aftype.title = _("AMPR ROSE");
715#endif
716#if HAVE_AFASH
717 ash_aftype.title = _("Ash");
718#endif
719 sVafinit = 1;
720}
721
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000722static int aftrans_opt(const char *arg)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000723{
724 struct aftrans_t *paft;
725 char *tmp1, *tmp2;
726 char buf[256];
727
728 safe_strncpy(buf, arg, sizeof(buf));
729
730 tmp1 = buf;
731
732 while (tmp1) {
733
Eric Andersen8b113f92001-06-01 21:47:15 +0000734 tmp2 = strchr(tmp1, ',');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000735
736 if (tmp2)
737 *(tmp2++) = '\0';
738
739 paft = aftrans;
740 for (paft = aftrans; paft->alias; paft++) {
741 if (strcmp(tmp1, paft->alias))
742 continue;
743 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
744 fprintf(stderr, _("Too much address family arguments.\n"));
745 return (0);
746 }
747 if (paft->flag)
748 (*paft->flag)++;
749 if (afname[0])
750 strcat(afname, ",");
751 strcat(afname, paft->name);
752 break;
753 }
754 if (!paft->alias) {
755 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
756 return (1);
757 }
758 tmp1 = tmp2;
759 }
760
761 return (0);
762}
763
764/* set the default AF list from the program name or a constant value */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000765static void aftrans_def(char *tool, char *argv0, char *dflt)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000766{
767 char *tmp;
768 char *buf;
769
770 strcpy(afname, dflt);
771
772 if (!(tmp = strrchr(argv0, '/')))
773 tmp = argv0; /* no slash?! */
774 else
775 tmp++;
776
777 if (!(buf = strdup(tmp)))
778 return;
779
780 if (strlen(tool) >= strlen(tmp)) {
781 free(buf);
782 return;
783 }
784 tmp = buf + (strlen(tmp) - strlen(tool));
785
786 if (strcmp(tmp, tool) != 0) {
787 free(buf);
788 return;
789 }
790 *tmp = '\0';
791 if ((tmp = strchr(buf, '_')))
792 *tmp = '\0';
793
794 afname[0] = '\0';
795 if (aftrans_opt(buf))
796 strcpy(afname, buf);
797
798 free(buf);
799}
800
Eric Andersenf15d4da2001-03-06 00:48:59 +0000801/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000802static struct aftype *get_aftype(const char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000803{
804 struct aftype **afp;
805
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000806#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000807 if (!sVafinit)
808 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000809#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000810
811 afp = aftypes;
812 while (*afp != NULL) {
813 if (!strcmp((*afp)->name, name))
814 return (*afp);
815 afp++;
816 }
Eric Andersen8b113f92001-06-01 21:47:15 +0000817 if (strchr(name, ','))
Eric Andersenf15d4da2001-03-06 00:48:59 +0000818 fprintf(stderr, _("Please don't supply more than one address family.\n"));
819 return (NULL);
820}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000821#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000822
823/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000824static struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000825{
826 struct aftype **afp;
827
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000828#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000829 if (!sVafinit)
830 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000831#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000832
833 afp = aftypes;
834 while (*afp != NULL) {
835 if ((*afp)->af == af)
836 return (*afp);
837 afp++;
838 }
839 return (NULL);
840}
841
842/* Check our protocol family table for this family and return its socket */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000843static int get_socket_for_af(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000844{
845 struct aftype **afp;
846
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000847#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000848 if (!sVafinit)
849 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000850#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000851
852 afp = aftypes;
853 while (*afp != NULL) {
854 if ((*afp)->af == af)
855 return (*afp)->fd;
856 afp++;
857 }
858 return -1;
859}
860
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000861#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000862/* type: 0=all, 1=getroute */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000863static void print_aflist(int type) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000864 int count = 0;
865 char * txt;
866 struct aftype **afp;
867
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000868#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000869 if (!sVafinit)
870 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000871#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000872
873 afp = aftypes;
874 while (*afp != NULL) {
875 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
876 afp++; continue;
877 }
878 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
879 txt = (*afp)->name; if (!txt) txt = "..";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000880 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000881 count++;
882 afp++;
883 }
884 fprintf(stderr,"\n");
885}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000886#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000887
888struct user_net_device_stats {
889 unsigned long long rx_packets; /* total packets received */
890 unsigned long long tx_packets; /* total packets transmitted */
891 unsigned long long rx_bytes; /* total bytes received */
892 unsigned long long tx_bytes; /* total bytes transmitted */
893 unsigned long rx_errors; /* bad packets received */
894 unsigned long tx_errors; /* packet transmit problems */
895 unsigned long rx_dropped; /* no space in linux buffers */
896 unsigned long tx_dropped; /* no space available in linux */
897 unsigned long rx_multicast; /* multicast packets received */
898 unsigned long rx_compressed;
899 unsigned long tx_compressed;
900 unsigned long collisions;
901
902 /* detailed rx_errors: */
903 unsigned long rx_length_errors;
904 unsigned long rx_over_errors; /* receiver ring buff overflow */
905 unsigned long rx_crc_errors; /* recved pkt with crc error */
906 unsigned long rx_frame_errors; /* recv'd frame alignment error */
907 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
908 unsigned long rx_missed_errors; /* receiver missed packet */
909 /* detailed tx_errors */
910 unsigned long tx_aborted_errors;
911 unsigned long tx_carrier_errors;
912 unsigned long tx_fifo_errors;
913 unsigned long tx_heartbeat_errors;
914 unsigned long tx_window_errors;
915};
916
917struct interface {
918 struct interface *next, *prev;
919 char name[IFNAMSIZ]; /* interface name */
920 short type; /* if type */
921 short flags; /* various flags */
922 int metric; /* routing metric */
923 int mtu; /* MTU value */
924 int tx_queue_len; /* transmit queue length */
925 struct ifmap map; /* hardware setup */
926 struct sockaddr addr; /* IP address */
927 struct sockaddr dstaddr; /* P-P IP address */
928 struct sockaddr broadaddr; /* IP broadcast address */
929 struct sockaddr netmask; /* IP network mask */
930 struct sockaddr ipxaddr_bb; /* IPX network address */
931 struct sockaddr ipxaddr_sn; /* IPX network address */
932 struct sockaddr ipxaddr_e3; /* IPX network address */
933 struct sockaddr ipxaddr_e2; /* IPX network address */
934 struct sockaddr ddpaddr; /* Appletalk DDP address */
935 struct sockaddr ecaddr; /* Econet address */
936 int has_ip;
937 int has_ipx_bb;
938 int has_ipx_sn;
939 int has_ipx_e3;
940 int has_ipx_e2;
941 int has_ax25;
942 int has_ddp;
943 int has_econet;
944 char hwaddr[32]; /* HW address */
945 int statistics_valid;
946 struct user_net_device_stats stats; /* statistics */
947 int keepalive; /* keepalive value for SLIP */
948 int outfill; /* outfill value for SLIP */
949};
950
951
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000952int interface_opt_a = 0; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000953
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000954#ifdef KEEP_UNUSED
955static int opt_i = 0; /* show the statistics */
956static int opt_v = 0; /* debugging output flag */
957
958static int addr_family = 0; /* currently selected AF */
959#endif /* KEEP_UNUSED */
960
Eric Andersenf15d4da2001-03-06 00:48:59 +0000961static struct interface *int_list, *int_last;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000962static int skfd = -1; /* generic raw socket desc. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000963
964
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000965static int sockets_open(int family)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000966{
967 struct aftype **aft;
968 int sfd = -1;
969 static int force = -1;
970
971 if (force < 0) {
972 force = 0;
973 if (get_kernel_revision() < KRELEASE(2, 1, 0))
974 force = 1;
975 if (access("/proc/net", R_OK))
976 force = 1;
977 }
978 for (aft = aftypes; *aft; aft++) {
979 struct aftype *af = *aft;
980 int type = SOCK_DGRAM;
981 if (af->af == AF_UNSPEC)
982 continue;
983 if (family && family != af->af)
984 continue;
985 if (af->fd != -1) {
986 sfd = af->fd;
987 continue;
988 }
989 /* Check some /proc file first to not stress kmod */
990 if (!family && !force && af->flag_file) {
991 if (access(af->flag_file, R_OK))
992 continue;
993 }
994#if HAVE_AFNETROM
995 if (af->af == AF_NETROM)
996 type = SOCK_SEQPACKET;
997#endif
998#if HAVE_AFX25
999 if (af->af == AF_X25)
1000 type = SOCK_SEQPACKET;
1001#endif
1002 af->fd = socket(af->af, type, 0);
1003 if (af->fd >= 0)
1004 sfd = af->fd;
1005 }
1006 if (sfd < 0)
1007 fprintf(stderr, _("No usable address families found.\n"));
1008 return sfd;
1009}
1010
1011/* like strcmp(), but knows about numbers */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001012static int nstrcmp(const char *astr, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001013{
1014 const char *a = astr;
1015
1016 while (*a == *b) {
1017 if (*a == '\0')
1018 return 0;
1019 a++;
1020 b++;
1021 }
1022 if (isdigit(*a)) {
1023 if (!isdigit(*b))
1024 return -1;
1025 while (a > astr) {
1026 a--;
1027 if (!isdigit(*a)) {
1028 a++;
1029 break;
1030 }
1031 if (!isdigit(*b))
1032 return -1;
1033 b--;
1034 }
1035 return atoi(a) > atoi(b) ? 1 : -1;
1036 }
1037 return *a - *b;
1038}
1039
1040static struct interface *add_interface(char *name)
1041{
1042 struct interface *ife, **nextp, *new;
1043
1044 for (ife = int_last; ife; ife = ife->prev) {
1045 int n = nstrcmp(ife->name, name);
1046 if (n == 0)
1047 return ife;
1048 if (n < 0)
1049 break;
1050 }
1051 new(new);
1052 safe_strncpy(new->name, name, IFNAMSIZ);
1053 nextp = ife ? &ife->next : &int_list;
1054 new->prev = ife;
1055 new->next = *nextp;
1056 if (new->next)
1057 new->next->prev = new;
1058 else
1059 int_last = new;
1060 *nextp = new;
1061 return new;
1062}
1063
1064
1065static int if_readconf(void)
1066{
1067 int numreqs = 30;
1068 struct ifconf ifc;
1069 struct ifreq *ifr;
1070 int n, err = -1;
Eric Andersen46cd74b2001-04-19 16:55:27 +00001071 /* XXX Should this re-use the global skfd? */
1072 int skfd2;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001073
1074 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1075 (as of 2.1.128) */
Eric Andersen46cd74b2001-04-19 16:55:27 +00001076 skfd2 = get_socket_for_af(AF_INET);
1077 if (skfd2 < 0) {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001078 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1079 strerror(errno));
1080 /* Try to soldier on with whatever socket we can get hold of. */
Eric Andersen46cd74b2001-04-19 16:55:27 +00001081 skfd2 = sockets_open(0);
1082 if (skfd2 < 0)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001083 return -1;
1084 }
1085
1086 ifc.ifc_buf = NULL;
1087 for (;;) {
1088 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1089 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1090
Eric Andersen46cd74b2001-04-19 16:55:27 +00001091 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001092 perror("SIOCGIFCONF");
1093 goto out;
1094 }
1095 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1096 /* assume it overflowed and try again */
1097 numreqs += 10;
1098 continue;
1099 }
1100 break;
1101 }
1102
1103 ifr = ifc.ifc_req;
1104 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1105 add_interface(ifr->ifr_name);
1106 ifr++;
1107 }
1108 err = 0;
1109
1110out:
1111 free(ifc.ifc_buf);
1112 return err;
1113}
1114
1115static char *get_name(char *name, char *p)
1116{
1117 while (isspace(*p))
1118 p++;
1119 while (*p) {
1120 if (isspace(*p))
1121 break;
1122 if (*p == ':') { /* could be an alias */
1123 char *dot = p, *dotname = name;
1124 *name++ = *p++;
1125 while (isdigit(*p))
1126 *name++ = *p++;
1127 if (*p != ':') { /* it wasn't, backup */
1128 p = dot;
1129 name = dotname;
1130 }
1131 if (*p == '\0')
1132 return NULL;
1133 p++;
1134 break;
1135 }
1136 *name++ = *p++;
1137 }
1138 *name++ = '\0';
1139 return p;
1140}
1141
1142static int get_dev_fields(char *bp, struct interface *ife)
1143{
1144 switch (procnetdev_vsn) {
1145 case 3:
1146 sscanf(bp,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001147 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001148 &ife->stats.rx_bytes,
1149 &ife->stats.rx_packets,
1150 &ife->stats.rx_errors,
1151 &ife->stats.rx_dropped,
1152 &ife->stats.rx_fifo_errors,
1153 &ife->stats.rx_frame_errors,
1154 &ife->stats.rx_compressed,
1155 &ife->stats.rx_multicast,
1156
1157 &ife->stats.tx_bytes,
1158 &ife->stats.tx_packets,
1159 &ife->stats.tx_errors,
1160 &ife->stats.tx_dropped,
1161 &ife->stats.tx_fifo_errors,
1162 &ife->stats.collisions,
1163 &ife->stats.tx_carrier_errors,
1164 &ife->stats.tx_compressed);
1165 break;
1166 case 2:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001167 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001168 &ife->stats.rx_bytes,
1169 &ife->stats.rx_packets,
1170 &ife->stats.rx_errors,
1171 &ife->stats.rx_dropped,
1172 &ife->stats.rx_fifo_errors,
1173 &ife->stats.rx_frame_errors,
1174
1175 &ife->stats.tx_bytes,
1176 &ife->stats.tx_packets,
1177 &ife->stats.tx_errors,
1178 &ife->stats.tx_dropped,
1179 &ife->stats.tx_fifo_errors,
1180 &ife->stats.collisions,
1181 &ife->stats.tx_carrier_errors);
1182 ife->stats.rx_multicast = 0;
1183 break;
1184 case 1:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001185 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001186 &ife->stats.rx_packets,
1187 &ife->stats.rx_errors,
1188 &ife->stats.rx_dropped,
1189 &ife->stats.rx_fifo_errors,
1190 &ife->stats.rx_frame_errors,
1191
1192 &ife->stats.tx_packets,
1193 &ife->stats.tx_errors,
1194 &ife->stats.tx_dropped,
1195 &ife->stats.tx_fifo_errors,
1196 &ife->stats.collisions,
1197 &ife->stats.tx_carrier_errors);
1198 ife->stats.rx_bytes = 0;
1199 ife->stats.tx_bytes = 0;
1200 ife->stats.rx_multicast = 0;
1201 break;
1202 }
1203 return 0;
1204}
1205
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001206static inline int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001207{
1208 if (strstr(buf, "compressed"))
1209 return 3;
1210 if (strstr(buf, "bytes"))
1211 return 2;
1212 return 1;
1213}
1214
1215static int if_readlist_proc(char *target)
1216{
1217 static int proc_read;
1218 FILE *fh;
1219 char buf[512];
1220 struct interface *ife;
1221 int err;
1222
1223 if (proc_read)
1224 return 0;
1225 if (!target)
1226 proc_read = 1;
1227
1228 fh = fopen(_PATH_PROCNET_DEV, "r");
1229 if (!fh) {
1230 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1231 _PATH_PROCNET_DEV, strerror(errno));
1232 return if_readconf();
1233 }
1234 fgets(buf, sizeof buf, fh); /* eat line */
1235 fgets(buf, sizeof buf, fh);
1236
1237#if 0 /* pretty, but can't cope with missing fields */
1238 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1239 "face", "", /* parsed separately */
1240 "bytes", "%lu",
1241 "packets", "%lu",
1242 "errs", "%lu",
1243 "drop", "%lu",
1244 "fifo", "%lu",
1245 "frame", "%lu",
1246 "compressed", "%lu",
1247 "multicast", "%lu",
1248 "bytes", "%lu",
1249 "packets", "%lu",
1250 "errs", "%lu",
1251 "drop", "%lu",
1252 "fifo", "%lu",
1253 "colls", "%lu",
1254 "carrier", "%lu",
1255 "compressed", "%lu",
1256 NULL);
1257 if (!fmt)
1258 return -1;
1259#else
1260 procnetdev_vsn = procnetdev_version(buf);
1261#endif
1262
1263 err = 0;
1264 while (fgets(buf, sizeof buf, fh)) {
1265 char *s, name[IFNAMSIZ];
1266 s = get_name(name, buf);
1267 ife = add_interface(name);
1268 get_dev_fields(s, ife);
1269 ife->statistics_valid = 1;
1270 if (target && !strcmp(target,name))
1271 break;
1272 }
1273 if (ferror(fh)) {
1274 perror(_PATH_PROCNET_DEV);
1275 err = -1;
1276 proc_read = 0;
1277 }
1278
1279#if 0
1280 free(fmt);
1281#endif
1282 fclose(fh);
1283 return err;
1284}
1285
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001286static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001287{
1288 int err = if_readlist_proc(NULL);
1289 if (!err)
1290 err = if_readconf();
1291 return err;
1292}
1293
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001294static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001295{
1296 struct interface *ife;
1297
1298 if (!int_list && (if_readlist() < 0))
1299 return -1;
1300 for (ife = int_list; ife; ife = ife->next) {
1301 int err = doit(ife, cookie);
1302 if (err)
1303 return err;
1304 }
1305 return 0;
1306}
1307
1308/* Support for fetching an IPX address */
1309
1310#if HAVE_AFIPX
1311static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1312{
1313 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1314 return ioctl(sock, SIOCGIFADDR, ifr);
1315}
1316#endif
1317
1318
1319/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001320static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001321{
1322 struct ifreq ifr;
1323 int fd;
1324 char *ifname = ife->name;
1325
1326 strcpy(ifr.ifr_name, ifname);
1327 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1328 return (-1);
1329 ife->flags = ifr.ifr_flags;
1330
1331 strcpy(ifr.ifr_name, ifname);
1332 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1333 memset(ife->hwaddr, 0, 32);
1334 else
1335 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1336
1337 ife->type = ifr.ifr_hwaddr.sa_family;
1338
1339 strcpy(ifr.ifr_name, ifname);
1340 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1341 ife->metric = 0;
1342 else
1343 ife->metric = ifr.ifr_metric;
1344
1345 strcpy(ifr.ifr_name, ifname);
1346 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1347 ife->mtu = 0;
1348 else
1349 ife->mtu = ifr.ifr_mtu;
1350
1351#ifdef HAVE_HWSLIP
1352 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1353 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1354 ife->type == ARPHRD_ADAPT) {
1355#ifdef SIOCGOUTFILL
1356 strcpy(ifr.ifr_name, ifname);
1357 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1358 ife->outfill = 0;
1359 else
1360 ife->outfill = (unsigned int) ifr.ifr_data;
1361#endif
1362#ifdef SIOCGKEEPALIVE
1363 strcpy(ifr.ifr_name, ifname);
1364 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1365 ife->keepalive = 0;
1366 else
1367 ife->keepalive = (unsigned int) ifr.ifr_data;
1368#endif
1369 }
1370#endif
1371
1372 strcpy(ifr.ifr_name, ifname);
1373 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1374 memset(&ife->map, 0, sizeof(struct ifmap));
1375 else
1376 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1377
1378 strcpy(ifr.ifr_name, ifname);
1379 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1380 memset(&ife->map, 0, sizeof(struct ifmap));
1381 else
1382 ife->map = ifr.ifr_map;
1383
1384#ifdef HAVE_TXQUEUELEN
1385 strcpy(ifr.ifr_name, ifname);
1386 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1387 ife->tx_queue_len = -1; /* unknown value */
1388 else
1389 ife->tx_queue_len = ifr.ifr_qlen;
1390#else
1391 ife->tx_queue_len = -1; /* unknown value */
1392#endif
1393
1394#if HAVE_AFINET
1395 /* IPv4 address? */
1396 fd = get_socket_for_af(AF_INET);
1397 if (fd >= 0) {
1398 strcpy(ifr.ifr_name, ifname);
1399 ifr.ifr_addr.sa_family = AF_INET;
1400 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1401 ife->has_ip = 1;
1402 ife->addr = ifr.ifr_addr;
1403 strcpy(ifr.ifr_name, ifname);
1404 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1405 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1406 else
1407 ife->dstaddr = ifr.ifr_dstaddr;
1408
1409 strcpy(ifr.ifr_name, ifname);
1410 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1411 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1412 else
1413 ife->broadaddr = ifr.ifr_broadaddr;
1414
1415 strcpy(ifr.ifr_name, ifname);
1416 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1417 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1418 else
1419 ife->netmask = ifr.ifr_netmask;
1420 } else
1421 memset(&ife->addr, 0, sizeof(struct sockaddr));
1422 }
1423#endif
1424
1425#if HAVE_AFATALK
1426 /* DDP address maybe ? */
1427 fd = get_socket_for_af(AF_APPLETALK);
1428 if (fd >= 0) {
1429 strcpy(ifr.ifr_name, ifname);
1430 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1431 ife->ddpaddr = ifr.ifr_addr;
1432 ife->has_ddp = 1;
1433 }
1434 }
1435#endif
1436
1437#if HAVE_AFIPX
1438 /* Look for IPX addresses with all framing types */
1439 fd = get_socket_for_af(AF_IPX);
1440 if (fd >= 0) {
1441 strcpy(ifr.ifr_name, ifname);
1442 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1443 ife->has_ipx_bb = 1;
1444 ife->ipxaddr_bb = ifr.ifr_addr;
1445 }
1446 strcpy(ifr.ifr_name, ifname);
1447 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1448 ife->has_ipx_sn = 1;
1449 ife->ipxaddr_sn = ifr.ifr_addr;
1450 }
1451 strcpy(ifr.ifr_name, ifname);
1452 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1453 ife->has_ipx_e3 = 1;
1454 ife->ipxaddr_e3 = ifr.ifr_addr;
1455 }
1456 strcpy(ifr.ifr_name, ifname);
1457 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1458 ife->has_ipx_e2 = 1;
1459 ife->ipxaddr_e2 = ifr.ifr_addr;
1460 }
1461 }
1462#endif
1463
1464#if HAVE_AFECONET
1465 /* Econet address maybe? */
1466 fd = get_socket_for_af(AF_ECONET);
1467 if (fd >= 0) {
1468 strcpy(ifr.ifr_name, ifname);
1469 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1470 ife->ecaddr = ifr.ifr_addr;
1471 ife->has_econet = 1;
1472 }
1473 }
1474#endif
1475
1476 return 0;
1477}
1478
1479
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001480static int do_if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001481{
1482 if (if_fetch(ife) < 0) {
1483 char *errmsg;
1484 if (errno == ENODEV) {
1485 /* Give better error message for this case. */
1486 errmsg = _("Device not found");
1487 } else {
1488 errmsg = strerror(errno);
1489 }
1490 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1491 ife->name, errmsg);
1492 return -1;
1493 }
1494 return 0;
1495}
1496
1497/* This structure defines hardware protocols and their handlers. */
1498struct hwtype {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001499 const char *name;
1500 const char *title;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001501 int type;
1502 int alen;
1503 char *(*print) (unsigned char *);
1504 int (*input) (char *, struct sockaddr *);
1505 int (*activate) (int fd);
1506 int suppress_null_addr;
1507};
1508
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001509static struct hwtype unspec_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001510{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001511 "unspec", "UNSPEC", -1, 0,
Eric Andersen46cd74b2001-04-19 16:55:27 +00001512 UNSPEC_print, NULL, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001513};
1514
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001515static struct hwtype loop_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001516{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001517 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001518 NULL, NULL, NULL
1519};
1520
1521#if HAVE_HWETHER
1522#include <net/if_arp.h>
1523#include <linux/if_ether.h>
1524
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001525static struct hwtype ether_hwtype;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001526
1527/* Display an Ethernet address in readable format. */
1528static char *pr_ether(unsigned char *ptr)
1529{
1530 static char buff[64];
1531
1532 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1533 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1534 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1535 );
1536 return (buff);
1537}
1538
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001539#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001540/* Input an Ethernet address and convert to binary. */
1541static int in_ether(char *bufp, struct sockaddr *sap)
1542{
1543 unsigned char *ptr;
1544 char c, *orig;
1545 int i;
1546 unsigned val;
1547
1548 sap->sa_family = ether_hwtype.type;
1549 ptr = sap->sa_data;
1550
1551 i = 0;
1552 orig = bufp;
1553 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1554 val = 0;
1555 c = *bufp++;
1556 if (isdigit(c))
1557 val = c - '0';
1558 else if (c >= 'a' && c <= 'f')
1559 val = c - 'a' + 10;
1560 else if (c >= 'A' && c <= 'F')
1561 val = c - 'A' + 10;
1562 else {
1563#ifdef DEBUG
1564 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1565#endif
1566 errno = EINVAL;
1567 return (-1);
1568 }
1569 val <<= 4;
1570 c = *bufp;
1571 if (isdigit(c))
1572 val |= c - '0';
1573 else if (c >= 'a' && c <= 'f')
1574 val |= c - 'a' + 10;
1575 else if (c >= 'A' && c <= 'F')
1576 val |= c - 'A' + 10;
1577 else if (c == ':' || c == 0)
1578 val >>= 4;
1579 else {
1580#ifdef DEBUG
1581 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1582#endif
1583 errno = EINVAL;
1584 return (-1);
1585 }
1586 if (c != 0)
1587 bufp++;
1588 *ptr++ = (unsigned char) (val & 0377);
1589 i++;
1590
1591 /* We might get a semicolon here - not required. */
1592 if (*bufp == ':') {
1593 if (i == ETH_ALEN) {
1594#ifdef DEBUG
1595 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1596 orig)
1597#endif
1598 ; /* nothing */
1599 }
1600 bufp++;
1601 }
1602 }
1603
1604 /* That's it. Any trailing junk? */
1605 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1606#ifdef DEBUG
1607 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1608 errno = EINVAL;
1609 return (-1);
1610#endif
1611 }
1612#ifdef DEBUG
1613 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1614#endif
1615
1616 return (0);
1617}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001618#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001619
1620
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001621static struct hwtype ether_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001622{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001623 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1624 pr_ether, NULL /* UNUSED in_ether */, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001625};
1626
1627
1628#endif /* HAVE_HWETHER */
1629
1630
1631#if HAVE_HWPPP
1632
1633#include <net/if_arp.h>
1634
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001635#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001636/* Start the PPP encapsulation on the file descriptor. */
1637static int do_ppp(int fd)
1638{
1639 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1640 return -1;
1641}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001642#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001643
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001644static struct hwtype ppp_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001645{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001646 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1647 NULL, NULL, NULL /* UNUSED do_ppp */, 0
Eric Andersenf15d4da2001-03-06 00:48:59 +00001648};
1649
1650
1651#endif /* HAVE_PPP */
1652
1653static struct hwtype *hwtypes[] =
1654{
1655
1656 &loop_hwtype,
1657
1658#if HAVE_HWSLIP
1659 &slip_hwtype,
1660 &cslip_hwtype,
1661 &slip6_hwtype,
1662 &cslip6_hwtype,
1663 &adaptive_hwtype,
1664#endif
1665#if HAVE_HWSTRIP
1666 &strip_hwtype,
1667#endif
1668#if HAVE_HWASH
1669 &ash_hwtype,
1670#endif
1671#if HAVE_HWETHER
1672 &ether_hwtype,
1673#endif
1674#if HAVE_HWTR
1675 &tr_hwtype,
1676#ifdef ARPHRD_IEEE802_TR
1677 &tr_hwtype1,
1678#endif
1679#endif
1680#if HAVE_HWAX25
1681 &ax25_hwtype,
1682#endif
1683#if HAVE_HWNETROM
1684 &netrom_hwtype,
1685#endif
1686#if HAVE_HWROSE
1687 &rose_hwtype,
1688#endif
1689#if HAVE_HWTUNNEL
1690 &tunnel_hwtype,
1691#endif
1692#if HAVE_HWPPP
1693 &ppp_hwtype,
1694#endif
1695#if HAVE_HWHDLCLAPB
1696 &hdlc_hwtype,
1697 &lapb_hwtype,
1698#endif
1699#if HAVE_HWARC
1700 &arcnet_hwtype,
1701#endif
1702#if HAVE_HWFR
1703 &dlci_hwtype,
1704 &frad_hwtype,
1705#endif
1706#if HAVE_HWSIT
1707 &sit_hwtype,
1708#endif
1709#if HAVE_HWFDDI
1710 &fddi_hwtype,
1711#endif
1712#if HAVE_HWHIPPI
1713 &hippi_hwtype,
1714#endif
1715#if HAVE_HWIRDA
1716 &irda_hwtype,
1717#endif
1718#if HAVE_HWEC
1719 &ec_hwtype,
1720#endif
1721#if HAVE_HWX25
1722 &x25_hwtype,
1723#endif
1724 &unspec_hwtype,
1725 NULL
1726};
1727
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001728#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001729static short sVhwinit = 0;
1730
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001731static void hwinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +00001732{
1733 loop_hwtype.title = _("Local Loopback");
1734 unspec_hwtype.title = _("UNSPEC");
1735#if HAVE_HWSLIP
1736 slip_hwtype.title = _("Serial Line IP");
1737 cslip_hwtype.title = _("VJ Serial Line IP");
1738 slip6_hwtype.title = _("6-bit Serial Line IP");
1739 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1740 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1741#endif
1742#if HAVE_HWETHER
1743 ether_hwtype.title = _("Ethernet");
1744#endif
1745#if HAVE_HWASH
1746 ash_hwtype.title = _("Ash");
1747#endif
1748#if HAVE_HWFDDI
1749 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1750#endif
1751#if HAVE_HWHIPPI
1752 hippi_hwtype.title = _("HIPPI");
1753#endif
1754#if HAVE_HWAX25
1755 ax25_hwtype.title = _("AMPR AX.25");
1756#endif
1757#if HAVE_HWROSE
1758 rose_hwtype.title = _("AMPR ROSE");
1759#endif
1760#if HAVE_HWNETROM
1761 netrom_hwtype.title = _("AMPR NET/ROM");
1762#endif
1763#if HAVE_HWX25
1764 x25_hwtype.title = _("generic X.25");
1765#endif
1766#if HAVE_HWTUNNEL
1767 tunnel_hwtype.title = _("IPIP Tunnel");
1768#endif
1769#if HAVE_HWPPP
1770 ppp_hwtype.title = _("Point-to-Point Protocol");
1771#endif
1772#if HAVE_HWHDLCLAPB
1773 hdlc_hwtype.title = _("(Cisco)-HDLC");
1774 lapb_hwtype.title = _("LAPB");
1775#endif
1776#if HAVE_HWARC
1777 arcnet_hwtype.title = _("ARCnet");
1778#endif
1779#if HAVE_HWFR
1780 dlci_hwtype.title = _("Frame Relay DLCI");
1781 frad_hwtype.title = _("Frame Relay Access Device");
1782#endif
1783#if HAVE_HWSIT
1784 sit_hwtype.title = _("IPv6-in-IPv4");
1785#endif
1786#if HAVE_HWIRDA
1787 irda_hwtype.title = _("IrLAP");
1788#endif
1789#if HAVE_HWTR
1790 tr_hwtype.title = _("16/4 Mbps Token Ring");
1791#ifdef ARPHRD_IEEE802_TR
1792 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1793#endif
1794#endif
1795#if HAVE_HWEC
1796 ec_hwtype.title = _("Econet");
1797#endif
1798 sVhwinit = 1;
1799}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001800#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001801
1802#ifdef IFF_PORTSEL
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001803static const char *if_port_text[][4] =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001804{
1805 /* Keep in step with <linux/netdevice.h> */
1806 {"unknown", NULL, NULL, NULL},
1807 {"10base2", "bnc", "coax", NULL},
1808 {"10baseT", "utp", "tpe", NULL},
1809 {"AUI", "thick", "db15", NULL},
1810 {"100baseT", NULL, NULL, NULL},
1811 {"100baseTX", NULL, NULL, NULL},
1812 {"100baseFX", NULL, NULL, NULL},
1813 {NULL, NULL, NULL, NULL},
1814};
1815#endif
1816
1817/* Check our hardware type table for this type. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001818static struct hwtype *get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001819{
1820 struct hwtype **hwp;
1821
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001822#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001823 if (!sVhwinit)
1824 hwinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001825#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001826
1827 hwp = hwtypes;
1828 while (*hwp != NULL) {
1829 if ((*hwp)->type == type)
1830 return (*hwp);
1831 hwp++;
1832 }
1833 return (NULL);
1834}
1835
1836/* return 1 if address is all zeros */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001837static int hw_null_address(struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001838{
1839 unsigned int i;
1840 unsigned char *address = (unsigned char *)ap;
1841 for (i = 0; i < hw->alen; i++)
1842 if (address[i])
1843 return 0;
1844 return 1;
1845}
1846
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001847static const char TRext[] = "\0\0k\0M";
1848
1849static void print_bytes_scaled(unsigned long long ull, const char *end)
1850{
1851 unsigned long long int_part;
1852 unsigned long frac_part;
1853 const char *ext;
1854 int i;
1855
1856 frac_part = 0;
1857 ext = TRext;
1858 int_part = ull;
1859 for (i=0 ; i<2 ; i++) {
1860 if (int_part >= 1024) {
1861 frac_part = ((int_part % 1024) * 10) / 1024;
1862 int_part /= 1024;
1863 ext += 2; /* Kb, Mb */
1864 }
1865 }
1866
1867 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1868}
1869
1870static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001871{
1872 struct aftype *ap;
1873 struct hwtype *hw;
1874 int hf;
1875 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001876
1877#if HAVE_AFIPX
1878 static struct aftype *ipxtype = NULL;
1879#endif
1880#if HAVE_AFECONET
1881 static struct aftype *ectype = NULL;
1882#endif
1883#if HAVE_AFATALK
1884 static struct aftype *ddptype = NULL;
1885#endif
1886#if HAVE_AFINET6
1887 FILE *f;
1888 char addr6[40], devname[20];
1889 struct sockaddr_in6 sap;
1890 int plen, scope, dad_status, if_idx;
1891 extern struct aftype inet6_aftype;
1892 char addr6p[8][5];
1893#endif
1894
1895 ap = get_afntype(ptr->addr.sa_family);
1896 if (ap == NULL)
1897 ap = get_afntype(0);
1898
1899 hf = ptr->type;
1900
1901 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1902 can_compress = 1;
1903
1904 hw = get_hwntype(hf);
1905 if (hw == NULL)
1906 hw = get_hwntype(-1);
1907
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001908 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001909 /* For some hardware types (eg Ash, ATM) we don't print the
1910 hardware address if it's null. */
1911 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1912 hw->suppress_null_addr)))
1913 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1914#ifdef IFF_PORTSEL
1915 if (ptr->flags & IFF_PORTSEL) {
1916 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1917 if (ptr->flags & IFF_AUTOMEDIA)
1918 printf(_("(auto)"));
1919 }
1920#endif
1921 printf("\n");
1922
1923#if HAVE_AFINET
1924 if (ptr->has_ip) {
1925 printf(_(" %s addr:%s "), ap->name,
1926 ap->sprint(&ptr->addr, 1));
1927 if (ptr->flags & IFF_POINTOPOINT) {
1928 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1929 }
1930 if (ptr->flags & IFF_BROADCAST) {
1931 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1932 }
1933 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1934 }
1935#endif
1936
1937#if HAVE_AFINET6
1938 /* FIXME: should be integrated into interface.c. */
1939
1940 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1941 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1942 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1943 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1944 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1945 if (!strcmp(devname, ptr->name)) {
1946 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1947 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1948 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1949 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1950 printf(_(" inet6 addr: %s/%d"),
1951 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1952 printf(_(" Scope:"));
1953 switch (scope) {
1954 case 0:
1955 printf(_("Global"));
1956 break;
1957 case IPV6_ADDR_LINKLOCAL:
1958 printf(_("Link"));
1959 break;
1960 case IPV6_ADDR_SITELOCAL:
1961 printf(_("Site"));
1962 break;
1963 case IPV6_ADDR_COMPATv4:
1964 printf(_("Compat"));
1965 break;
1966 case IPV6_ADDR_LOOPBACK:
1967 printf(_("Host"));
1968 break;
1969 default:
1970 printf(_("Unknown"));
1971 }
1972 printf("\n");
1973 }
1974 }
1975 fclose(f);
1976 }
1977#endif
1978
1979#if HAVE_AFIPX
1980 if (ipxtype == NULL)
1981 ipxtype = get_afntype(AF_IPX);
1982
1983 if (ipxtype != NULL) {
1984 if (ptr->has_ipx_bb)
1985 printf(_(" IPX/Ethernet II addr:%s\n"),
1986 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1987 if (ptr->has_ipx_sn)
1988 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1989 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1990 if (ptr->has_ipx_e2)
1991 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1992 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1993 if (ptr->has_ipx_e3)
1994 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1995 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1996 }
1997#endif
1998
1999#if HAVE_AFATALK
2000 if (ddptype == NULL)
2001 ddptype = get_afntype(AF_APPLETALK);
2002 if (ddptype != NULL) {
2003 if (ptr->has_ddp)
2004 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
2005 }
2006#endif
2007
2008#if HAVE_AFECONET
2009 if (ectype == NULL)
2010 ectype = get_afntype(AF_ECONET);
2011 if (ectype != NULL) {
2012 if (ptr->has_econet)
2013 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
2014 }
2015#endif
2016
2017 printf(" ");
2018 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
2019 if (ptr->flags == 0)
2020 printf(_("[NO FLAGS] "));
2021 if (ptr->flags & IFF_UP)
2022 printf(_("UP "));
2023 if (ptr->flags & IFF_BROADCAST)
2024 printf(_("BROADCAST "));
2025 if (ptr->flags & IFF_DEBUG)
2026 printf(_("DEBUG "));
2027 if (ptr->flags & IFF_LOOPBACK)
2028 printf(_("LOOPBACK "));
2029 if (ptr->flags & IFF_POINTOPOINT)
2030 printf(_("POINTOPOINT "));
2031 if (ptr->flags & IFF_NOTRAILERS)
2032 printf(_("NOTRAILERS "));
2033 if (ptr->flags & IFF_RUNNING)
2034 printf(_("RUNNING "));
2035 if (ptr->flags & IFF_NOARP)
2036 printf(_("NOARP "));
2037 if (ptr->flags & IFF_PROMISC)
2038 printf(_("PROMISC "));
2039 if (ptr->flags & IFF_ALLMULTI)
2040 printf(_("ALLMULTI "));
2041 if (ptr->flags & IFF_SLAVE)
2042 printf(_("SLAVE "));
2043 if (ptr->flags & IFF_MASTER)
2044 printf(_("MASTER "));
2045 if (ptr->flags & IFF_MULTICAST)
2046 printf(_("MULTICAST "));
2047#ifdef HAVE_DYNAMIC
2048 if (ptr->flags & IFF_DYNAMIC)
2049 printf(_("DYNAMIC "));
2050#endif
2051 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2052 printf(_(" MTU:%d Metric:%d"),
2053 ptr->mtu, ptr->metric ? ptr->metric : 1);
2054#ifdef SIOCSKEEPALIVE
2055 if (ptr->outfill || ptr->keepalive)
2056 printf(_(" Outfill:%d Keepalive:%d"),
2057 ptr->outfill, ptr->keepalive);
2058#endif
2059 printf("\n");
2060
2061 /* If needed, display the interface statistics. */
2062
2063 if (ptr->statistics_valid) {
2064 /* XXX: statistics are currently only printed for the primary address,
2065 * not for the aliases, although strictly speaking they're shared
2066 * by all addresses.
2067 */
2068 printf(" ");
2069
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002070 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002071 ptr->stats.rx_packets, ptr->stats.rx_errors,
2072 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2073 ptr->stats.rx_frame_errors);
2074 if (can_compress)
2075 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002076 printf(" ");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002077 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002078 ptr->stats.tx_packets, ptr->stats.tx_errors,
2079 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2080 ptr->stats.tx_carrier_errors);
2081 printf(_(" collisions:%lu "), ptr->stats.collisions);
2082 if (can_compress)
2083 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2084 if (ptr->tx_queue_len != -1)
2085 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002086 printf("\n R");
2087 print_bytes_scaled(ptr->stats.rx_bytes, " T");
Manuel Novoa III 0e0883e2001-03-15 15:37:48 +00002088 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002089
Eric Andersenf15d4da2001-03-06 00:48:59 +00002090 }
2091
2092 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2093 ptr->map.base_addr)) {
2094 printf(" ");
2095 if (ptr->map.irq)
2096 printf(_("Interrupt:%d "), ptr->map.irq);
2097 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2098 I/O maps */
2099 printf(_("Base address:0x%x "), ptr->map.base_addr);
2100 if (ptr->map.mem_start) {
2101 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2102 }
2103 if (ptr->map.dma)
2104 printf(_("DMA chan:%x "), ptr->map.dma);
2105 printf("\n");
2106 }
2107 printf("\n");
2108}
2109
2110
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002111static int do_if_print(struct interface *ife, void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002112{
2113 int *opt_a = (int *) cookie;
2114 int res;
2115
2116 res = do_if_fetch(ife);
2117 if (res >= 0) {
2118 if ((ife->flags & IFF_UP) || *opt_a)
2119 ife_print(ife);
2120 }
2121 return res;
2122}
2123
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002124static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002125{
2126 struct interface *ife = NULL;
2127
2128 if (if_readlist_proc(name) < 0)
2129 return NULL;
2130 ife = add_interface(name);
2131 return ife;
2132}
2133
2134/* for ipv4 add/del modes */
2135static int if_print(char *ifname)
2136{
2137 int res;
2138
2139 if (!ifname) {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002140 res = for_all_interfaces(do_if_print, &interface_opt_a);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002141 } else {
2142 struct interface *ife;
2143
2144 ife = lookup_interface(ifname);
2145 res = do_if_fetch(ife);
2146 if (res >= 0)
2147 ife_print(ife);
2148 }
2149 return res;
2150}
2151
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002152int display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002153{
2154 int status;
2155
2156 /* Create a channel to the NET kernel. */
2157 if ((skfd = sockets_open(0)) < 0) {
Manuel Novoa III 78f57462001-03-10 02:00:54 +00002158 perror_msg_and_die("socket");
Eric Andersenf15d4da2001-03-06 00:48:59 +00002159 }
2160
2161 /* Do we have to show the current setup? */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002162 status = if_print(ifname);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002163 close(skfd);
2164 exit(status < 0);
2165}