blob: 680216a57da44eee7833af3e525baaccfeb2248d [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 *
Eric Andersen8e392922001-10-27 03:28:53 +000018 * Version: $Id: interface.c,v 1.6 2001/10/27 03:28:19 andersen 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
47#undef HAVE_AFINET6
48#undef HAVE_AFIPX
49#undef HAVE_AFATALK
50#undef HAVE_AFNETROM
51#undef HAVE_AFX25
52#undef HAVE_AFECONET
Eric Andersen8b113f92001-06-01 21:47:15 +000053#undef HAVE_AFASH
Eric Andersenf15d4da2001-03-06 00:48:59 +000054
55/*
56 *
57 * Device Hardware types.
58 *
59 */
60#define HAVE_HWETHER 1
61#define HAVE_HWPPP 1
62#undef HAVE_HWSLIP
63
64
65#include <features.h>
66#include <sys/types.h>
67#include <sys/socket.h>
68#include <sys/ioctl.h>
69#include <netinet/in.h>
70#include <net/if.h>
71#include <net/if_arp.h>
72#include <stdio.h>
73#include <errno.h>
74#include <fcntl.h>
75#include <ctype.h>
76#include <stdlib.h>
77#include <string.h>
78#include <unistd.h>
79#include <netdb.h>
80#include <netinet/in.h>
81#include <arpa/inet.h>
Eric Andersen20aab262001-07-19 22:28:02 +000082#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +000083#include <arpa/nameser.h>
Eric Andersen20aab262001-07-19 22:28:02 +000084#endif
Glenn L McGrath6b8c5502001-05-05 03:19:12 +000085#include "libbb.h"
Eric Andersenf15d4da2001-03-06 00:48:59 +000086
87#define _(x) x
88#define _PATH_PROCNET_DEV "/proc/net/dev"
89#define new(p) ((p) = xcalloc(1,sizeof(*(p))))
Eric Andersen8b113f92001-06-01 21:47:15 +000090#define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
Eric Andersenf15d4da2001-03-06 00:48:59 +000091
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000092static int procnetdev_vsn = 1;
Eric Andersenf15d4da2001-03-06 00:48:59 +000093
94
95/* Ugh. But libc5 doesn't provide POSIX types. */
96#include <asm/types.h>
97
98
99#ifdef HAVE_HWSLIP
100#include <linux/if_slip.h>
101#endif
102
103#if HAVE_AFINET6
104
105#ifndef _LINUX_IN6_H
106/*
107 * This is in linux/include/net/ipv6.h.
108 */
109
110struct in6_ifreq {
111 struct in6_addr ifr6_addr;
112 __u32 ifr6_prefixlen;
113 unsigned int ifr6_ifindex;
114};
115
116#endif
117
118#endif /* HAVE_AFINET6 */
119
120#if HAVE_AFIPX
Eric Andersenb6b519b2001-04-09 23:52:18 +0000121#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000122#include <netipx/ipx.h>
123#else
124#include "ipx.h"
125#endif
126#endif
127#if 0
128#include "net-support.h"
129#include "pathnames.h"
130#include "version.h"
131#include "../intl.h"
132#include "interface.h"
133#include "sockets.h"
134#include "util.h"
135#endif
136
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000137/* Defines for glibc2.0 users. */
138#ifndef SIOCSIFTXQLEN
139#define SIOCSIFTXQLEN 0x8943
140#define SIOCGIFTXQLEN 0x8942
141#endif
142
143/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
144#ifndef ifr_qlen
145#define ifr_qlen ifr_ifru.ifru_mtu
146#endif
147
148#ifndef HAVE_TXQUEUELEN
149#define HAVE_TXQUEUELEN 1
150#endif
151
152#ifndef IFF_DYNAMIC
153#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
154#endif
155
Eric Andersenf15d4da2001-03-06 00:48:59 +0000156/* This structure defines protocol families and their handlers. */
157struct aftype {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000158 const char *name;
159 const char *title;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000160 int af;
161 int alen;
162 char *(*print) (unsigned char *);
163 char *(*sprint) (struct sockaddr *, int numeric);
164 int (*input) (int type, char *bufp, struct sockaddr *);
165 void (*herror) (char *text);
166 int (*rprint) (int options);
167 int (*rinput) (int typ, int ext, char **argv);
168
169 /* may modify src */
170 int (*getmask) (char *src, struct sockaddr * mask, char *name);
171
172 int fd;
173 char *flag_file;
174};
175
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000176static struct aftype *aftypes[];
177
178#ifdef KEEP_UNUSED
179
180static int flag_unx;
Eric Andersen8b113f92001-06-01 21:47:15 +0000181#ifdef HAVE_AFIPX
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000182static int flag_ipx;
Eric Andersen8b113f92001-06-01 21:47:15 +0000183#endif
184#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000185static int flag_ax25;
Eric Andersen8b113f92001-06-01 21:47:15 +0000186#endif
187#ifdef HAVE_AFATALK
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000188static int flag_ddp;
Eric Andersen8b113f92001-06-01 21:47:15 +0000189#endif
190#ifdef HAVE_AFNETROM
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000191static int flag_netrom;
Eric Andersen8b113f92001-06-01 21:47:15 +0000192#endif
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000193static int flag_inet;
Eric Andersen8b113f92001-06-01 21:47:15 +0000194#ifdef HAVE_AFINET6
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000195static int flag_inet6;
Eric Andersen8b113f92001-06-01 21:47:15 +0000196#endif
197#ifdef HAVE_AFECONET
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000198static int flag_econet;
Eric Andersen8b113f92001-06-01 21:47:15 +0000199#endif
200#ifdef HAVE_AFX25
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000201static int flag_x25 = 0;
Eric Andersen8b113f92001-06-01 21:47:15 +0000202#endif
203#ifdef HAVE_AFASH
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000204static int flag_ash;
Eric Andersen8b113f92001-06-01 21:47:15 +0000205#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000206
207
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000208static struct aftrans_t {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000209 char *alias;
210 char *name;
211 int *flag;
212} aftrans[] = {
213
Eric Andersen8b113f92001-06-01 21:47:15 +0000214#ifdef HAVE_AFX25
Eric Andersenf15d4da2001-03-06 00:48:59 +0000215 {
216 "ax25", "ax25", &flag_ax25
217 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000218#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000219 {
220 "ip", "inet", &flag_inet
221 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000222#ifdef HAVE_AFINET6
Eric Andersenf15d4da2001-03-06 00:48:59 +0000223 {
224 "ip6", "inet6", &flag_inet6
225 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000226#endif
227#ifdef HAVE_AFIPX
Eric Andersenf15d4da2001-03-06 00:48:59 +0000228 {
229 "ipx", "ipx", &flag_ipx
230 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000231#endif
232#ifdef HAVE_AFATALK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000233 {
234 "appletalk", "ddp", &flag_ddp
235 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000236#endif
237#ifdef HAVE_AFNETROM
Eric Andersenf15d4da2001-03-06 00:48:59 +0000238 {
239 "netrom", "netrom", &flag_netrom
240 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000241#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000242 {
243 "inet", "inet", &flag_inet
244 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000245#ifdef HAVE_AFINET6
Eric Andersenf15d4da2001-03-06 00:48:59 +0000246 {
247 "inet6", "inet6", &flag_inet6
248 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000249#endif
250#ifdef HAVE_AFATALK
Eric Andersenf15d4da2001-03-06 00:48:59 +0000251 {
252 "ddp", "ddp", &flag_ddp
253 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000254#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000255 {
256 "unix", "unix", &flag_unx
257 },
258 {
259 "tcpip", "inet", &flag_inet
260 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000261#ifdef HAVE_AFECONET
Eric Andersenf15d4da2001-03-06 00:48:59 +0000262 {
263 "econet", "ec", &flag_econet
264 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000265#endif
266#ifdef HAVE_AFX25
Eric Andersenf15d4da2001-03-06 00:48:59 +0000267 {
268 "x25", "x25", &flag_x25
269 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000270#endif
271#ifdef HAVE_AFASH
Eric Andersenf15d4da2001-03-06 00:48:59 +0000272 {
273 "ash", "ash", &flag_ash
274 },
Eric Andersen8b113f92001-06-01 21:47:15 +0000275#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000276 {
277 0, 0, 0
278 }
279};
280
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000281static char afname[256] = "";
282#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000283
284#if HAVE_AFUNIX
285
286/* Display a UNIX domain address. */
287static char *UNIX_print(unsigned char *ptr)
288{
289 return (ptr);
290}
291
292
293/* Display a UNIX domain address. */
294static char *UNIX_sprint(struct sockaddr *sap, int numeric)
295{
296 static char buf[64];
297
298 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
299 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
300 return (UNIX_print(sap->sa_data));
301}
302
303
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000304static struct aftype unix_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000305{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000306 "unix", "UNIX Domain", AF_UNIX, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000307 UNIX_print, UNIX_sprint, NULL, NULL,
308 NULL, NULL, NULL,
309 -1,
310 "/proc/net/unix"
311};
312#endif /* HAVE_AFUNIX */
313
314#if HAVE_AFINET
315
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000316#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000317extern int h_errno; /* some netdb.h versions don't export this */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000318#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000319
320/* cache */
321struct addr {
322 struct sockaddr_in addr;
323 char *name;
324 int host;
325 struct addr *next;
326};
327
328static struct addr *INET_nn = NULL; /* addr-to-name cache */
329
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000330#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000331static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
332{
333 struct hostent *hp;
334 struct netent *np;
335
336 /* Grmpf. -FvK */
337 sin->sin_family = AF_INET;
338 sin->sin_port = 0;
339
340 /* Default is special, meaning 0.0.0.0. */
341 if (!strcmp(name, "default")) {
342 sin->sin_addr.s_addr = INADDR_ANY;
343 return (1);
344 }
345 /* Look to see if it's a dotted quad. */
346 if (inet_aton(name, &sin->sin_addr)) {
347 return 0;
348 }
349 /* If we expect this to be a hostname, try hostname database first */
350#ifdef DEBUG
351 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
352#endif
353 if (hostfirst &&
354 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
355 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
356 sizeof(struct in_addr));
357 return 0;
358 }
359 /* Try the NETWORKS database to see if this is a known network. */
360#ifdef DEBUG
361 fprintf (stderr, "getnetbyname (%s)\n", name);
362#endif
363 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
364 sin->sin_addr.s_addr = htonl(np->n_net);
365 return 1;
366 }
367 if (hostfirst) {
368 /* Don't try again */
369 errno = h_errno;
370 return -1;
371 }
372#ifdef DEBUG
373 res_init();
374 _res.options |= RES_DEBUG;
375#endif
376
377#ifdef DEBUG
378 fprintf (stderr, "gethostbyname (%s)\n", name);
379#endif
380 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
381 errno = h_errno;
382 return -1;
383 }
384 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
385 sizeof(struct in_addr));
386
387 return 0;
388}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000389#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000390
391/* numeric: & 0x8000: default instead of *,
392 * & 0x4000: host instead of net,
393 * & 0x0fff: don't resolve
394 */
Eric Andersen46cd74b2001-04-19 16:55:27 +0000395static int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000396 int numeric, unsigned int netmask)
397{
398 struct hostent *ent;
399 struct netent *np;
400 struct addr *pn;
401 unsigned long ad, host_ad;
402 int host = 0;
403
404 /* Grmpf. -FvK */
Eric Andersen46cd74b2001-04-19 16:55:27 +0000405 if (s_in->sin_family != AF_INET) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000406#ifdef DEBUG
Eric Andersen46cd74b2001-04-19 16:55:27 +0000407 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000408#endif
409 errno = EAFNOSUPPORT;
410 return (-1);
411 }
Eric Andersen46cd74b2001-04-19 16:55:27 +0000412 ad = (unsigned long) s_in->sin_addr.s_addr;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000413#ifdef DEBUG
414 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
415#endif
416 if (ad == INADDR_ANY) {
417 if ((numeric & 0x0FFF) == 0) {
418 if (numeric & 0x8000)
419 safe_strncpy(name, "default", len);
420 else
421 safe_strncpy(name, "*", len);
422 return (0);
423 }
424 }
425 if (numeric & 0x0FFF) {
Eric Andersen46cd74b2001-04-19 16:55:27 +0000426 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000427 return (0);
428 }
429
430 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
431 host = 1;
432#if 0
433 INET_nn = NULL;
434#endif
435 pn = INET_nn;
436 while (pn != NULL) {
437 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
438 safe_strncpy(name, pn->name, len);
439#ifdef DEBUG
440 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
441#endif
442 return (0);
443 }
444 pn = pn->next;
445 }
446
447 host_ad = ntohl(ad);
448 np = NULL;
449 ent = NULL;
450 if (host) {
451#ifdef DEBUG
452 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
453#endif
454 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
455 if (ent != NULL)
456 safe_strncpy(name, ent->h_name, len);
457 } else {
458#ifdef DEBUG
459 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
460#endif
Eric Andersen20aab262001-07-19 22:28:02 +0000461#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000462 np = getnetbyaddr(host_ad, AF_INET);
463 if (np != NULL)
464 safe_strncpy(name, np->n_name, len);
Eric Andersen20aab262001-07-19 22:28:02 +0000465#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000466 }
467 if ((ent == NULL) && (np == NULL))
Eric Andersen46cd74b2001-04-19 16:55:27 +0000468 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Eric Andersen0f430e32001-03-06 20:54:43 +0000469 pn = (struct addr *) xmalloc(sizeof(struct addr));
Eric Andersen46cd74b2001-04-19 16:55:27 +0000470 pn->addr = *s_in;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000471 pn->next = INET_nn;
472 pn->host = host;
Eric Andersen0f430e32001-03-06 20:54:43 +0000473 pn->name = (char *) xmalloc(strlen(name) + 1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000474 strcpy(pn->name, name);
475 INET_nn = pn;
476
477 return (0);
478}
479
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000480#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000481static void INET_reserror(char *text)
482{
483 herror(text);
484}
485
Eric Andersenf15d4da2001-03-06 00:48:59 +0000486/* Display an Internet socket address. */
487static char *INET_print(unsigned char *ptr)
488{
489 return (inet_ntoa((*(struct in_addr *) ptr)));
490}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000491#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000492
493/* Display an Internet socket address. */
494static char *INET_sprint(struct sockaddr *sap, int numeric)
495{
496 static char buff[128];
497
498 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
499 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
500
501 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
502 numeric, 0xffffff00) != 0)
503 return (NULL);
504
505 return (buff);
506}
507
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000508#ifdef KEEP_UNUSED
509static char *INET_sprintmask(struct sockaddr *sap, int numeric,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000510 unsigned int netmask)
511{
512 static char buff[128];
513
514 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
515 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
516 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
517 numeric, netmask) != 0)
518 return (NULL);
519 return (buff);
520}
521
Eric Andersenf15d4da2001-03-06 00:48:59 +0000522static int INET_getsock(char *bufp, struct sockaddr *sap)
523{
524 char *sp = bufp, *bp;
525 unsigned int i;
526 unsigned val;
527 struct sockaddr_in *sin;
528
529 sin = (struct sockaddr_in *) sap;
530 sin->sin_family = AF_INET;
531 sin->sin_port = 0;
532
533 val = 0;
534 bp = (char *) &val;
535 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
536 *sp = toupper(*sp);
537
538 if ((*sp >= 'A') && (*sp <= 'F'))
539 bp[i] |= (int) (*sp - 'A') + 10;
540 else if ((*sp >= '0') && (*sp <= '9'))
541 bp[i] |= (int) (*sp - '0');
542 else
543 return (-1);
544
545 bp[i] <<= 4;
546 sp++;
547 *sp = toupper(*sp);
548
549 if ((*sp >= 'A') && (*sp <= 'F'))
550 bp[i] |= (int) (*sp - 'A') + 10;
551 else if ((*sp >= '0') && (*sp <= '9'))
552 bp[i] |= (int) (*sp - '0');
553 else
554 return (-1);
555
556 sp++;
557 }
558 sin->sin_addr.s_addr = htonl(val);
559
560 return (sp - bufp);
561}
562
563static int INET_input(int type, char *bufp, struct sockaddr *sap)
564{
565 switch (type) {
566 case 1:
567 return (INET_getsock(bufp, sap));
568 case 256:
569 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
570 default:
571 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
572 }
573}
574
575static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
576{
577 struct sockaddr_in *mask = (struct sockaddr_in *) m;
578 char *slash, *end;
579 int prefix;
580
581 if ((slash = strchr(adr, '/')) == NULL)
582 return 0;
583
584 *slash++ = '\0';
585 prefix = strtoul(slash, &end, 0);
586 if (*end != '\0')
587 return -1;
588
589 if (name) {
590 sprintf(name, "/%d", prefix);
591 }
592 mask->sin_family = AF_INET;
593 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
594 return 1;
595}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000596#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000597
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000598static struct aftype inet_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000599{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000600 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
601 NULL /* UNUSED INET_print */, INET_sprint,
602 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000603 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000604 NULL /* UNUSED INET_getnetmask */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000605 -1,
606 NULL
607};
608
609#endif /* HAVE_AFINET */
610
611/* Display an UNSPEC address. */
612static char *UNSPEC_print(unsigned char *ptr)
613{
Eric Andersen46cd74b2001-04-19 16:55:27 +0000614 static char buff[sizeof(struct sockaddr)*3+1];
Eric Andersenf15d4da2001-03-06 00:48:59 +0000615 char *pos;
616 unsigned int i;
617
618 pos = buff;
619 for (i = 0; i < sizeof(struct sockaddr); i++) {
Eric Andersen46cd74b2001-04-19 16:55:27 +0000620 /* careful -- not every libc's sprintf returns # bytes written */
621 sprintf(pos, "%02X-", (*ptr++ & 0377));
622 pos += 3;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000623 }
Eric Andersen46cd74b2001-04-19 16:55:27 +0000624 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
625 *--pos = '\0';
Eric Andersenf15d4da2001-03-06 00:48:59 +0000626 return (buff);
627}
628
629/* Display an UNSPEC socket address. */
630static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
631{
632 static char buf[64];
633
634 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
635 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
636 return (UNSPEC_print(sap->sa_data));
637}
638
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000639static struct aftype unspec_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000640{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000641 "unspec", "UNSPEC", AF_UNSPEC, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000642 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
643 NULL,
644};
645
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000646static struct aftype *aftypes[] =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000647{
648#if HAVE_AFUNIX
649 &unix_aftype,
650#endif
651#if HAVE_AFINET
652 &inet_aftype,
653#endif
654#if HAVE_AFINET6
655 &inet6_aftype,
656#endif
657#if HAVE_AFAX25
658 &ax25_aftype,
659#endif
660#if HAVE_AFNETROM
661 &netrom_aftype,
662#endif
663#if HAVE_AFROSE
664 &rose_aftype,
665#endif
666#if HAVE_AFIPX
667 &ipx_aftype,
668#endif
669#if HAVE_AFATALK
670 &ddp_aftype,
671#endif
672#if HAVE_AFECONET
673 &ec_aftype,
674#endif
675#if HAVE_AFASH
676 &ash_aftype,
677#endif
678#if HAVE_AFX25
679 &x25_aftype,
680#endif
681 &unspec_aftype,
682 NULL
683};
684
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000685#ifdef KEEP_UNUSED
686static short sVafinit = 0;
687
688static void afinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +0000689{
690 unspec_aftype.title = _("UNSPEC");
691#if HAVE_AFUNIX
692 unix_aftype.title = _("UNIX Domain");
693#endif
694#if HAVE_AFINET
695 inet_aftype.title = _("DARPA Internet");
696#endif
697#if HAVE_AFINET6
698 inet6_aftype.title = _("IPv6");
699#endif
700#if HAVE_AFAX25
701 ax25_aftype.title = _("AMPR AX.25");
702#endif
703#if HAVE_AFNETROM
704 netrom_aftype.title = _("AMPR NET/ROM");
705#endif
706#if HAVE_AFIPX
707 ipx_aftype.title = _("Novell IPX");
708#endif
709#if HAVE_AFATALK
710 ddp_aftype.title = _("Appletalk DDP");
711#endif
712#if HAVE_AFECONET
713 ec_aftype.title = _("Econet");
714#endif
715#if HAVE_AFX25
716 x25_aftype.title = _("CCITT X.25");
717#endif
718#if HAVE_AFROSE
719 rose_aftype.title = _("AMPR ROSE");
720#endif
721#if HAVE_AFASH
722 ash_aftype.title = _("Ash");
723#endif
724 sVafinit = 1;
725}
726
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000727static int aftrans_opt(const char *arg)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000728{
729 struct aftrans_t *paft;
730 char *tmp1, *tmp2;
731 char buf[256];
732
733 safe_strncpy(buf, arg, sizeof(buf));
734
735 tmp1 = buf;
736
737 while (tmp1) {
738
Eric Andersen8b113f92001-06-01 21:47:15 +0000739 tmp2 = strchr(tmp1, ',');
Eric Andersenf15d4da2001-03-06 00:48:59 +0000740
741 if (tmp2)
742 *(tmp2++) = '\0';
743
744 paft = aftrans;
745 for (paft = aftrans; paft->alias; paft++) {
746 if (strcmp(tmp1, paft->alias))
747 continue;
748 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
749 fprintf(stderr, _("Too much address family arguments.\n"));
750 return (0);
751 }
752 if (paft->flag)
753 (*paft->flag)++;
754 if (afname[0])
755 strcat(afname, ",");
756 strcat(afname, paft->name);
757 break;
758 }
759 if (!paft->alias) {
760 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
761 return (1);
762 }
763 tmp1 = tmp2;
764 }
765
766 return (0);
767}
768
769/* set the default AF list from the program name or a constant value */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000770static void aftrans_def(char *tool, char *argv0, char *dflt)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000771{
772 char *tmp;
773 char *buf;
774
775 strcpy(afname, dflt);
776
777 if (!(tmp = strrchr(argv0, '/')))
778 tmp = argv0; /* no slash?! */
779 else
780 tmp++;
781
782 if (!(buf = strdup(tmp)))
783 return;
784
785 if (strlen(tool) >= strlen(tmp)) {
786 free(buf);
787 return;
788 }
789 tmp = buf + (strlen(tmp) - strlen(tool));
790
791 if (strcmp(tmp, tool) != 0) {
792 free(buf);
793 return;
794 }
795 *tmp = '\0';
796 if ((tmp = strchr(buf, '_')))
797 *tmp = '\0';
798
799 afname[0] = '\0';
800 if (aftrans_opt(buf))
801 strcpy(afname, buf);
802
803 free(buf);
804}
805
Eric Andersenf15d4da2001-03-06 00:48:59 +0000806/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000807static struct aftype *get_aftype(const char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000808{
809 struct aftype **afp;
810
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000811#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000812 if (!sVafinit)
813 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000814#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000815
816 afp = aftypes;
817 while (*afp != NULL) {
818 if (!strcmp((*afp)->name, name))
819 return (*afp);
820 afp++;
821 }
Eric Andersen8b113f92001-06-01 21:47:15 +0000822 if (strchr(name, ','))
Eric Andersenf15d4da2001-03-06 00:48:59 +0000823 fprintf(stderr, _("Please don't supply more than one address family.\n"));
824 return (NULL);
825}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000826#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000827
828/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000829static struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000830{
831 struct aftype **afp;
832
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000833#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000834 if (!sVafinit)
835 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000836#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000837
838 afp = aftypes;
839 while (*afp != NULL) {
840 if ((*afp)->af == af)
841 return (*afp);
842 afp++;
843 }
844 return (NULL);
845}
846
847/* Check our protocol family table for this family and return its socket */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000848static int get_socket_for_af(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000849{
850 struct aftype **afp;
851
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000852#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000853 if (!sVafinit)
854 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000855#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000856
857 afp = aftypes;
858 while (*afp != NULL) {
859 if ((*afp)->af == af)
860 return (*afp)->fd;
861 afp++;
862 }
863 return -1;
864}
865
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000866#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000867/* type: 0=all, 1=getroute */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000868static void print_aflist(int type) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000869 int count = 0;
870 char * txt;
871 struct aftype **afp;
872
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000873#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000874 if (!sVafinit)
875 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000876#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000877
878 afp = aftypes;
879 while (*afp != NULL) {
880 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
881 afp++; continue;
882 }
883 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
884 txt = (*afp)->name; if (!txt) txt = "..";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000885 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000886 count++;
887 afp++;
888 }
889 fprintf(stderr,"\n");
890}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000891#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000892
893struct user_net_device_stats {
894 unsigned long long rx_packets; /* total packets received */
895 unsigned long long tx_packets; /* total packets transmitted */
896 unsigned long long rx_bytes; /* total bytes received */
897 unsigned long long tx_bytes; /* total bytes transmitted */
898 unsigned long rx_errors; /* bad packets received */
899 unsigned long tx_errors; /* packet transmit problems */
900 unsigned long rx_dropped; /* no space in linux buffers */
901 unsigned long tx_dropped; /* no space available in linux */
902 unsigned long rx_multicast; /* multicast packets received */
903 unsigned long rx_compressed;
904 unsigned long tx_compressed;
905 unsigned long collisions;
906
907 /* detailed rx_errors: */
908 unsigned long rx_length_errors;
909 unsigned long rx_over_errors; /* receiver ring buff overflow */
910 unsigned long rx_crc_errors; /* recved pkt with crc error */
911 unsigned long rx_frame_errors; /* recv'd frame alignment error */
912 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
913 unsigned long rx_missed_errors; /* receiver missed packet */
914 /* detailed tx_errors */
915 unsigned long tx_aborted_errors;
916 unsigned long tx_carrier_errors;
917 unsigned long tx_fifo_errors;
918 unsigned long tx_heartbeat_errors;
919 unsigned long tx_window_errors;
920};
921
922struct interface {
923 struct interface *next, *prev;
924 char name[IFNAMSIZ]; /* interface name */
925 short type; /* if type */
926 short flags; /* various flags */
927 int metric; /* routing metric */
928 int mtu; /* MTU value */
929 int tx_queue_len; /* transmit queue length */
930 struct ifmap map; /* hardware setup */
931 struct sockaddr addr; /* IP address */
932 struct sockaddr dstaddr; /* P-P IP address */
933 struct sockaddr broadaddr; /* IP broadcast address */
934 struct sockaddr netmask; /* IP network mask */
935 struct sockaddr ipxaddr_bb; /* IPX network address */
936 struct sockaddr ipxaddr_sn; /* IPX network address */
937 struct sockaddr ipxaddr_e3; /* IPX network address */
938 struct sockaddr ipxaddr_e2; /* IPX network address */
939 struct sockaddr ddpaddr; /* Appletalk DDP address */
940 struct sockaddr ecaddr; /* Econet address */
941 int has_ip;
942 int has_ipx_bb;
943 int has_ipx_sn;
944 int has_ipx_e3;
945 int has_ipx_e2;
946 int has_ax25;
947 int has_ddp;
948 int has_econet;
949 char hwaddr[32]; /* HW address */
950 int statistics_valid;
951 struct user_net_device_stats stats; /* statistics */
952 int keepalive; /* keepalive value for SLIP */
953 int outfill; /* outfill value for SLIP */
954};
955
956
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000957int interface_opt_a = 0; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000958
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000959#ifdef KEEP_UNUSED
960static int opt_i = 0; /* show the statistics */
961static int opt_v = 0; /* debugging output flag */
962
963static int addr_family = 0; /* currently selected AF */
964#endif /* KEEP_UNUSED */
965
Eric Andersenf15d4da2001-03-06 00:48:59 +0000966static struct interface *int_list, *int_last;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000967static int skfd = -1; /* generic raw socket desc. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000968
969
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000970static int sockets_open(int family)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000971{
972 struct aftype **aft;
973 int sfd = -1;
974 static int force = -1;
975
976 if (force < 0) {
977 force = 0;
978 if (get_kernel_revision() < KRELEASE(2, 1, 0))
979 force = 1;
980 if (access("/proc/net", R_OK))
981 force = 1;
982 }
983 for (aft = aftypes; *aft; aft++) {
984 struct aftype *af = *aft;
985 int type = SOCK_DGRAM;
986 if (af->af == AF_UNSPEC)
987 continue;
988 if (family && family != af->af)
989 continue;
990 if (af->fd != -1) {
991 sfd = af->fd;
992 continue;
993 }
994 /* Check some /proc file first to not stress kmod */
995 if (!family && !force && af->flag_file) {
996 if (access(af->flag_file, R_OK))
997 continue;
998 }
999#if HAVE_AFNETROM
1000 if (af->af == AF_NETROM)
1001 type = SOCK_SEQPACKET;
1002#endif
1003#if HAVE_AFX25
1004 if (af->af == AF_X25)
1005 type = SOCK_SEQPACKET;
1006#endif
1007 af->fd = socket(af->af, type, 0);
1008 if (af->fd >= 0)
1009 sfd = af->fd;
1010 }
1011 if (sfd < 0)
1012 fprintf(stderr, _("No usable address families found.\n"));
1013 return sfd;
1014}
1015
1016/* like strcmp(), but knows about numbers */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001017static int nstrcmp(const char *astr, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001018{
1019 const char *a = astr;
1020
1021 while (*a == *b) {
1022 if (*a == '\0')
1023 return 0;
1024 a++;
1025 b++;
1026 }
1027 if (isdigit(*a)) {
1028 if (!isdigit(*b))
1029 return -1;
1030 while (a > astr) {
1031 a--;
1032 if (!isdigit(*a)) {
1033 a++;
1034 break;
1035 }
1036 if (!isdigit(*b))
1037 return -1;
1038 b--;
1039 }
1040 return atoi(a) > atoi(b) ? 1 : -1;
1041 }
1042 return *a - *b;
1043}
1044
1045static struct interface *add_interface(char *name)
1046{
1047 struct interface *ife, **nextp, *new;
1048
1049 for (ife = int_last; ife; ife = ife->prev) {
1050 int n = nstrcmp(ife->name, name);
1051 if (n == 0)
1052 return ife;
1053 if (n < 0)
1054 break;
1055 }
1056 new(new);
1057 safe_strncpy(new->name, name, IFNAMSIZ);
1058 nextp = ife ? &ife->next : &int_list;
1059 new->prev = ife;
1060 new->next = *nextp;
1061 if (new->next)
1062 new->next->prev = new;
1063 else
1064 int_last = new;
1065 *nextp = new;
1066 return new;
1067}
1068
1069
1070static int if_readconf(void)
1071{
1072 int numreqs = 30;
1073 struct ifconf ifc;
1074 struct ifreq *ifr;
1075 int n, err = -1;
Eric Andersen46cd74b2001-04-19 16:55:27 +00001076 int skfd2;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001077
1078 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1079 (as of 2.1.128) */
Eric Andersen46cd74b2001-04-19 16:55:27 +00001080 skfd2 = get_socket_for_af(AF_INET);
1081 if (skfd2 < 0) {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001082 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1083 strerror(errno));
1084 /* Try to soldier on with whatever socket we can get hold of. */
Eric Andersen46cd74b2001-04-19 16:55:27 +00001085 skfd2 = sockets_open(0);
1086 if (skfd2 < 0)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001087 return -1;
1088 }
1089
1090 ifc.ifc_buf = NULL;
1091 for (;;) {
1092 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1093 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1094
Eric Andersen46cd74b2001-04-19 16:55:27 +00001095 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
Eric Andersenf15d4da2001-03-06 00:48:59 +00001096 perror("SIOCGIFCONF");
1097 goto out;
1098 }
1099 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1100 /* assume it overflowed and try again */
1101 numreqs += 10;
1102 continue;
1103 }
1104 break;
1105 }
1106
1107 ifr = ifc.ifc_req;
1108 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1109 add_interface(ifr->ifr_name);
1110 ifr++;
1111 }
1112 err = 0;
1113
1114out:
1115 free(ifc.ifc_buf);
1116 return err;
1117}
1118
1119static char *get_name(char *name, char *p)
1120{
1121 while (isspace(*p))
1122 p++;
1123 while (*p) {
1124 if (isspace(*p))
1125 break;
1126 if (*p == ':') { /* could be an alias */
1127 char *dot = p, *dotname = name;
1128 *name++ = *p++;
1129 while (isdigit(*p))
1130 *name++ = *p++;
1131 if (*p != ':') { /* it wasn't, backup */
1132 p = dot;
1133 name = dotname;
1134 }
1135 if (*p == '\0')
1136 return NULL;
1137 p++;
1138 break;
1139 }
1140 *name++ = *p++;
1141 }
1142 *name++ = '\0';
1143 return p;
1144}
1145
1146static int get_dev_fields(char *bp, struct interface *ife)
1147{
1148 switch (procnetdev_vsn) {
1149 case 3:
1150 sscanf(bp,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001151 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001152 &ife->stats.rx_bytes,
1153 &ife->stats.rx_packets,
1154 &ife->stats.rx_errors,
1155 &ife->stats.rx_dropped,
1156 &ife->stats.rx_fifo_errors,
1157 &ife->stats.rx_frame_errors,
1158 &ife->stats.rx_compressed,
1159 &ife->stats.rx_multicast,
1160
1161 &ife->stats.tx_bytes,
1162 &ife->stats.tx_packets,
1163 &ife->stats.tx_errors,
1164 &ife->stats.tx_dropped,
1165 &ife->stats.tx_fifo_errors,
1166 &ife->stats.collisions,
1167 &ife->stats.tx_carrier_errors,
1168 &ife->stats.tx_compressed);
1169 break;
1170 case 2:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001171 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001172 &ife->stats.rx_bytes,
1173 &ife->stats.rx_packets,
1174 &ife->stats.rx_errors,
1175 &ife->stats.rx_dropped,
1176 &ife->stats.rx_fifo_errors,
1177 &ife->stats.rx_frame_errors,
1178
1179 &ife->stats.tx_bytes,
1180 &ife->stats.tx_packets,
1181 &ife->stats.tx_errors,
1182 &ife->stats.tx_dropped,
1183 &ife->stats.tx_fifo_errors,
1184 &ife->stats.collisions,
1185 &ife->stats.tx_carrier_errors);
1186 ife->stats.rx_multicast = 0;
1187 break;
1188 case 1:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001189 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001190 &ife->stats.rx_packets,
1191 &ife->stats.rx_errors,
1192 &ife->stats.rx_dropped,
1193 &ife->stats.rx_fifo_errors,
1194 &ife->stats.rx_frame_errors,
1195
1196 &ife->stats.tx_packets,
1197 &ife->stats.tx_errors,
1198 &ife->stats.tx_dropped,
1199 &ife->stats.tx_fifo_errors,
1200 &ife->stats.collisions,
1201 &ife->stats.tx_carrier_errors);
1202 ife->stats.rx_bytes = 0;
1203 ife->stats.tx_bytes = 0;
1204 ife->stats.rx_multicast = 0;
1205 break;
1206 }
1207 return 0;
1208}
1209
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001210static inline int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001211{
1212 if (strstr(buf, "compressed"))
1213 return 3;
1214 if (strstr(buf, "bytes"))
1215 return 2;
1216 return 1;
1217}
1218
1219static int if_readlist_proc(char *target)
1220{
1221 static int proc_read;
1222 FILE *fh;
1223 char buf[512];
1224 struct interface *ife;
1225 int err;
1226
1227 if (proc_read)
1228 return 0;
1229 if (!target)
1230 proc_read = 1;
1231
1232 fh = fopen(_PATH_PROCNET_DEV, "r");
1233 if (!fh) {
1234 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1235 _PATH_PROCNET_DEV, strerror(errno));
1236 return if_readconf();
1237 }
1238 fgets(buf, sizeof buf, fh); /* eat line */
1239 fgets(buf, sizeof buf, fh);
1240
1241#if 0 /* pretty, but can't cope with missing fields */
1242 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1243 "face", "", /* parsed separately */
1244 "bytes", "%lu",
1245 "packets", "%lu",
1246 "errs", "%lu",
1247 "drop", "%lu",
1248 "fifo", "%lu",
1249 "frame", "%lu",
1250 "compressed", "%lu",
1251 "multicast", "%lu",
1252 "bytes", "%lu",
1253 "packets", "%lu",
1254 "errs", "%lu",
1255 "drop", "%lu",
1256 "fifo", "%lu",
1257 "colls", "%lu",
1258 "carrier", "%lu",
1259 "compressed", "%lu",
1260 NULL);
1261 if (!fmt)
1262 return -1;
1263#else
1264 procnetdev_vsn = procnetdev_version(buf);
1265#endif
1266
1267 err = 0;
1268 while (fgets(buf, sizeof buf, fh)) {
1269 char *s, name[IFNAMSIZ];
1270 s = get_name(name, buf);
1271 ife = add_interface(name);
1272 get_dev_fields(s, ife);
1273 ife->statistics_valid = 1;
1274 if (target && !strcmp(target,name))
1275 break;
1276 }
1277 if (ferror(fh)) {
1278 perror(_PATH_PROCNET_DEV);
1279 err = -1;
1280 proc_read = 0;
1281 }
1282
1283#if 0
1284 free(fmt);
1285#endif
1286 fclose(fh);
1287 return err;
1288}
1289
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001290static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001291{
1292 int err = if_readlist_proc(NULL);
1293 if (!err)
1294 err = if_readconf();
1295 return err;
1296}
1297
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001298static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001299{
1300 struct interface *ife;
1301
1302 if (!int_list && (if_readlist() < 0))
1303 return -1;
1304 for (ife = int_list; ife; ife = ife->next) {
1305 int err = doit(ife, cookie);
1306 if (err)
1307 return err;
1308 }
1309 return 0;
1310}
1311
1312/* Support for fetching an IPX address */
1313
1314#if HAVE_AFIPX
1315static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1316{
1317 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1318 return ioctl(sock, SIOCGIFADDR, ifr);
1319}
1320#endif
1321
1322
1323/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001324static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001325{
1326 struct ifreq ifr;
1327 int fd;
1328 char *ifname = ife->name;
1329
1330 strcpy(ifr.ifr_name, ifname);
1331 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1332 return (-1);
1333 ife->flags = ifr.ifr_flags;
1334
1335 strcpy(ifr.ifr_name, ifname);
1336 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1337 memset(ife->hwaddr, 0, 32);
1338 else
1339 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1340
1341 ife->type = ifr.ifr_hwaddr.sa_family;
1342
1343 strcpy(ifr.ifr_name, ifname);
1344 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1345 ife->metric = 0;
1346 else
1347 ife->metric = ifr.ifr_metric;
1348
1349 strcpy(ifr.ifr_name, ifname);
1350 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1351 ife->mtu = 0;
1352 else
1353 ife->mtu = ifr.ifr_mtu;
1354
1355#ifdef HAVE_HWSLIP
1356 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1357 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1358 ife->type == ARPHRD_ADAPT) {
1359#ifdef SIOCGOUTFILL
1360 strcpy(ifr.ifr_name, ifname);
1361 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1362 ife->outfill = 0;
1363 else
1364 ife->outfill = (unsigned int) ifr.ifr_data;
1365#endif
1366#ifdef SIOCGKEEPALIVE
1367 strcpy(ifr.ifr_name, ifname);
1368 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1369 ife->keepalive = 0;
1370 else
1371 ife->keepalive = (unsigned int) ifr.ifr_data;
1372#endif
1373 }
1374#endif
1375
1376 strcpy(ifr.ifr_name, ifname);
1377 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1378 memset(&ife->map, 0, sizeof(struct ifmap));
1379 else
1380 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1381
1382 strcpy(ifr.ifr_name, ifname);
1383 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1384 memset(&ife->map, 0, sizeof(struct ifmap));
1385 else
1386 ife->map = ifr.ifr_map;
1387
1388#ifdef HAVE_TXQUEUELEN
1389 strcpy(ifr.ifr_name, ifname);
1390 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1391 ife->tx_queue_len = -1; /* unknown value */
1392 else
1393 ife->tx_queue_len = ifr.ifr_qlen;
1394#else
1395 ife->tx_queue_len = -1; /* unknown value */
1396#endif
1397
1398#if HAVE_AFINET
1399 /* IPv4 address? */
1400 fd = get_socket_for_af(AF_INET);
1401 if (fd >= 0) {
1402 strcpy(ifr.ifr_name, ifname);
1403 ifr.ifr_addr.sa_family = AF_INET;
1404 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1405 ife->has_ip = 1;
1406 ife->addr = ifr.ifr_addr;
1407 strcpy(ifr.ifr_name, ifname);
1408 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1409 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1410 else
1411 ife->dstaddr = ifr.ifr_dstaddr;
1412
1413 strcpy(ifr.ifr_name, ifname);
1414 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1415 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1416 else
1417 ife->broadaddr = ifr.ifr_broadaddr;
1418
1419 strcpy(ifr.ifr_name, ifname);
1420 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1421 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1422 else
1423 ife->netmask = ifr.ifr_netmask;
1424 } else
1425 memset(&ife->addr, 0, sizeof(struct sockaddr));
1426 }
1427#endif
1428
1429#if HAVE_AFATALK
1430 /* DDP address maybe ? */
1431 fd = get_socket_for_af(AF_APPLETALK);
1432 if (fd >= 0) {
1433 strcpy(ifr.ifr_name, ifname);
1434 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1435 ife->ddpaddr = ifr.ifr_addr;
1436 ife->has_ddp = 1;
1437 }
1438 }
1439#endif
1440
1441#if HAVE_AFIPX
1442 /* Look for IPX addresses with all framing types */
1443 fd = get_socket_for_af(AF_IPX);
1444 if (fd >= 0) {
1445 strcpy(ifr.ifr_name, ifname);
1446 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1447 ife->has_ipx_bb = 1;
1448 ife->ipxaddr_bb = ifr.ifr_addr;
1449 }
1450 strcpy(ifr.ifr_name, ifname);
1451 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1452 ife->has_ipx_sn = 1;
1453 ife->ipxaddr_sn = ifr.ifr_addr;
1454 }
1455 strcpy(ifr.ifr_name, ifname);
1456 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1457 ife->has_ipx_e3 = 1;
1458 ife->ipxaddr_e3 = ifr.ifr_addr;
1459 }
1460 strcpy(ifr.ifr_name, ifname);
1461 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1462 ife->has_ipx_e2 = 1;
1463 ife->ipxaddr_e2 = ifr.ifr_addr;
1464 }
1465 }
1466#endif
1467
1468#if HAVE_AFECONET
1469 /* Econet address maybe? */
1470 fd = get_socket_for_af(AF_ECONET);
1471 if (fd >= 0) {
1472 strcpy(ifr.ifr_name, ifname);
1473 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1474 ife->ecaddr = ifr.ifr_addr;
1475 ife->has_econet = 1;
1476 }
1477 }
1478#endif
1479
1480 return 0;
1481}
1482
1483
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001484static int do_if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001485{
1486 if (if_fetch(ife) < 0) {
1487 char *errmsg;
1488 if (errno == ENODEV) {
1489 /* Give better error message for this case. */
1490 errmsg = _("Device not found");
1491 } else {
1492 errmsg = strerror(errno);
1493 }
1494 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1495 ife->name, errmsg);
1496 return -1;
1497 }
1498 return 0;
1499}
1500
1501/* This structure defines hardware protocols and their handlers. */
1502struct hwtype {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001503 const char *name;
1504 const char *title;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001505 int type;
1506 int alen;
1507 char *(*print) (unsigned char *);
1508 int (*input) (char *, struct sockaddr *);
1509 int (*activate) (int fd);
1510 int suppress_null_addr;
1511};
1512
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001513static struct hwtype unspec_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001514{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001515 "unspec", "UNSPEC", -1, 0,
Eric Andersen46cd74b2001-04-19 16:55:27 +00001516 UNSPEC_print, NULL, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001517};
1518
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001519static struct hwtype loop_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001520{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001521 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001522 NULL, NULL, NULL
1523};
1524
1525#if HAVE_HWETHER
1526#include <net/if_arp.h>
1527#include <linux/if_ether.h>
1528
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001529static struct hwtype ether_hwtype;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001530
1531/* Display an Ethernet address in readable format. */
1532static char *pr_ether(unsigned char *ptr)
1533{
1534 static char buff[64];
1535
1536 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1537 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1538 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1539 );
1540 return (buff);
1541}
1542
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001543#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001544/* Input an Ethernet address and convert to binary. */
1545static int in_ether(char *bufp, struct sockaddr *sap)
1546{
1547 unsigned char *ptr;
1548 char c, *orig;
1549 int i;
1550 unsigned val;
1551
1552 sap->sa_family = ether_hwtype.type;
1553 ptr = sap->sa_data;
1554
1555 i = 0;
1556 orig = bufp;
1557 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1558 val = 0;
1559 c = *bufp++;
1560 if (isdigit(c))
1561 val = c - '0';
1562 else if (c >= 'a' && c <= 'f')
1563 val = c - 'a' + 10;
1564 else if (c >= 'A' && c <= 'F')
1565 val = c - 'A' + 10;
1566 else {
1567#ifdef DEBUG
1568 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1569#endif
1570 errno = EINVAL;
1571 return (-1);
1572 }
1573 val <<= 4;
1574 c = *bufp;
1575 if (isdigit(c))
1576 val |= c - '0';
1577 else if (c >= 'a' && c <= 'f')
1578 val |= c - 'a' + 10;
1579 else if (c >= 'A' && c <= 'F')
1580 val |= c - 'A' + 10;
1581 else if (c == ':' || c == 0)
1582 val >>= 4;
1583 else {
1584#ifdef DEBUG
1585 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1586#endif
1587 errno = EINVAL;
1588 return (-1);
1589 }
1590 if (c != 0)
1591 bufp++;
1592 *ptr++ = (unsigned char) (val & 0377);
1593 i++;
1594
1595 /* We might get a semicolon here - not required. */
1596 if (*bufp == ':') {
1597 if (i == ETH_ALEN) {
1598#ifdef DEBUG
1599 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1600 orig)
1601#endif
1602 ; /* nothing */
1603 }
1604 bufp++;
1605 }
1606 }
1607
1608 /* That's it. Any trailing junk? */
1609 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1610#ifdef DEBUG
1611 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1612 errno = EINVAL;
1613 return (-1);
1614#endif
1615 }
1616#ifdef DEBUG
1617 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1618#endif
1619
1620 return (0);
1621}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001622#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001623
1624
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001625static struct hwtype ether_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001626{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001627 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1628 pr_ether, NULL /* UNUSED in_ether */, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001629};
1630
1631
1632#endif /* HAVE_HWETHER */
1633
1634
1635#if HAVE_HWPPP
1636
1637#include <net/if_arp.h>
1638
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001639#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001640/* Start the PPP encapsulation on the file descriptor. */
1641static int do_ppp(int fd)
1642{
1643 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1644 return -1;
1645}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001646#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001647
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001648static struct hwtype ppp_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001649{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001650 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1651 NULL, NULL, NULL /* UNUSED do_ppp */, 0
Eric Andersenf15d4da2001-03-06 00:48:59 +00001652};
1653
1654
1655#endif /* HAVE_PPP */
1656
1657static struct hwtype *hwtypes[] =
1658{
1659
1660 &loop_hwtype,
1661
1662#if HAVE_HWSLIP
1663 &slip_hwtype,
1664 &cslip_hwtype,
1665 &slip6_hwtype,
1666 &cslip6_hwtype,
1667 &adaptive_hwtype,
1668#endif
1669#if HAVE_HWSTRIP
1670 &strip_hwtype,
1671#endif
1672#if HAVE_HWASH
1673 &ash_hwtype,
1674#endif
1675#if HAVE_HWETHER
1676 &ether_hwtype,
1677#endif
1678#if HAVE_HWTR
1679 &tr_hwtype,
1680#ifdef ARPHRD_IEEE802_TR
1681 &tr_hwtype1,
1682#endif
1683#endif
1684#if HAVE_HWAX25
1685 &ax25_hwtype,
1686#endif
1687#if HAVE_HWNETROM
1688 &netrom_hwtype,
1689#endif
1690#if HAVE_HWROSE
1691 &rose_hwtype,
1692#endif
1693#if HAVE_HWTUNNEL
1694 &tunnel_hwtype,
1695#endif
1696#if HAVE_HWPPP
1697 &ppp_hwtype,
1698#endif
1699#if HAVE_HWHDLCLAPB
1700 &hdlc_hwtype,
1701 &lapb_hwtype,
1702#endif
1703#if HAVE_HWARC
1704 &arcnet_hwtype,
1705#endif
1706#if HAVE_HWFR
1707 &dlci_hwtype,
1708 &frad_hwtype,
1709#endif
1710#if HAVE_HWSIT
1711 &sit_hwtype,
1712#endif
1713#if HAVE_HWFDDI
1714 &fddi_hwtype,
1715#endif
1716#if HAVE_HWHIPPI
1717 &hippi_hwtype,
1718#endif
1719#if HAVE_HWIRDA
1720 &irda_hwtype,
1721#endif
1722#if HAVE_HWEC
1723 &ec_hwtype,
1724#endif
1725#if HAVE_HWX25
1726 &x25_hwtype,
1727#endif
1728 &unspec_hwtype,
1729 NULL
1730};
1731
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001732#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001733static short sVhwinit = 0;
1734
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001735static void hwinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +00001736{
1737 loop_hwtype.title = _("Local Loopback");
1738 unspec_hwtype.title = _("UNSPEC");
1739#if HAVE_HWSLIP
1740 slip_hwtype.title = _("Serial Line IP");
1741 cslip_hwtype.title = _("VJ Serial Line IP");
1742 slip6_hwtype.title = _("6-bit Serial Line IP");
1743 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1744 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1745#endif
1746#if HAVE_HWETHER
1747 ether_hwtype.title = _("Ethernet");
1748#endif
1749#if HAVE_HWASH
1750 ash_hwtype.title = _("Ash");
1751#endif
1752#if HAVE_HWFDDI
1753 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1754#endif
1755#if HAVE_HWHIPPI
1756 hippi_hwtype.title = _("HIPPI");
1757#endif
1758#if HAVE_HWAX25
1759 ax25_hwtype.title = _("AMPR AX.25");
1760#endif
1761#if HAVE_HWROSE
1762 rose_hwtype.title = _("AMPR ROSE");
1763#endif
1764#if HAVE_HWNETROM
1765 netrom_hwtype.title = _("AMPR NET/ROM");
1766#endif
1767#if HAVE_HWX25
1768 x25_hwtype.title = _("generic X.25");
1769#endif
1770#if HAVE_HWTUNNEL
1771 tunnel_hwtype.title = _("IPIP Tunnel");
1772#endif
1773#if HAVE_HWPPP
1774 ppp_hwtype.title = _("Point-to-Point Protocol");
1775#endif
1776#if HAVE_HWHDLCLAPB
1777 hdlc_hwtype.title = _("(Cisco)-HDLC");
1778 lapb_hwtype.title = _("LAPB");
1779#endif
1780#if HAVE_HWARC
1781 arcnet_hwtype.title = _("ARCnet");
1782#endif
1783#if HAVE_HWFR
1784 dlci_hwtype.title = _("Frame Relay DLCI");
1785 frad_hwtype.title = _("Frame Relay Access Device");
1786#endif
1787#if HAVE_HWSIT
1788 sit_hwtype.title = _("IPv6-in-IPv4");
1789#endif
1790#if HAVE_HWIRDA
1791 irda_hwtype.title = _("IrLAP");
1792#endif
1793#if HAVE_HWTR
1794 tr_hwtype.title = _("16/4 Mbps Token Ring");
1795#ifdef ARPHRD_IEEE802_TR
1796 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1797#endif
1798#endif
1799#if HAVE_HWEC
1800 ec_hwtype.title = _("Econet");
1801#endif
1802 sVhwinit = 1;
1803}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001804#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001805
1806#ifdef IFF_PORTSEL
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001807static const char *if_port_text[][4] =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001808{
1809 /* Keep in step with <linux/netdevice.h> */
1810 {"unknown", NULL, NULL, NULL},
1811 {"10base2", "bnc", "coax", NULL},
1812 {"10baseT", "utp", "tpe", NULL},
1813 {"AUI", "thick", "db15", NULL},
1814 {"100baseT", NULL, NULL, NULL},
1815 {"100baseTX", NULL, NULL, NULL},
1816 {"100baseFX", NULL, NULL, NULL},
1817 {NULL, NULL, NULL, NULL},
1818};
1819#endif
1820
1821/* Check our hardware type table for this type. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001822static struct hwtype *get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001823{
1824 struct hwtype **hwp;
1825
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001826#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001827 if (!sVhwinit)
1828 hwinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001829#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001830
1831 hwp = hwtypes;
1832 while (*hwp != NULL) {
1833 if ((*hwp)->type == type)
1834 return (*hwp);
1835 hwp++;
1836 }
1837 return (NULL);
1838}
1839
1840/* return 1 if address is all zeros */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001841static int hw_null_address(struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001842{
1843 unsigned int i;
1844 unsigned char *address = (unsigned char *)ap;
1845 for (i = 0; i < hw->alen; i++)
1846 if (address[i])
1847 return 0;
1848 return 1;
1849}
1850
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001851static const char TRext[] = "\0\0k\0M";
1852
1853static void print_bytes_scaled(unsigned long long ull, const char *end)
1854{
1855 unsigned long long int_part;
1856 unsigned long frac_part;
1857 const char *ext;
1858 int i;
1859
1860 frac_part = 0;
1861 ext = TRext;
1862 int_part = ull;
1863 for (i=0 ; i<2 ; i++) {
1864 if (int_part >= 1024) {
1865 frac_part = ((int_part % 1024) * 10) / 1024;
1866 int_part /= 1024;
1867 ext += 2; /* Kb, Mb */
1868 }
1869 }
1870
1871 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1872}
1873
1874static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001875{
1876 struct aftype *ap;
1877 struct hwtype *hw;
1878 int hf;
1879 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001880
1881#if HAVE_AFIPX
1882 static struct aftype *ipxtype = NULL;
1883#endif
1884#if HAVE_AFECONET
1885 static struct aftype *ectype = NULL;
1886#endif
1887#if HAVE_AFATALK
1888 static struct aftype *ddptype = NULL;
1889#endif
1890#if HAVE_AFINET6
1891 FILE *f;
1892 char addr6[40], devname[20];
1893 struct sockaddr_in6 sap;
1894 int plen, scope, dad_status, if_idx;
1895 extern struct aftype inet6_aftype;
1896 char addr6p[8][5];
1897#endif
1898
1899 ap = get_afntype(ptr->addr.sa_family);
1900 if (ap == NULL)
1901 ap = get_afntype(0);
1902
1903 hf = ptr->type;
1904
1905 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1906 can_compress = 1;
1907
1908 hw = get_hwntype(hf);
1909 if (hw == NULL)
1910 hw = get_hwntype(-1);
1911
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001912 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001913 /* For some hardware types (eg Ash, ATM) we don't print the
1914 hardware address if it's null. */
1915 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1916 hw->suppress_null_addr)))
1917 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1918#ifdef IFF_PORTSEL
1919 if (ptr->flags & IFF_PORTSEL) {
1920 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1921 if (ptr->flags & IFF_AUTOMEDIA)
1922 printf(_("(auto)"));
1923 }
1924#endif
1925 printf("\n");
1926
1927#if HAVE_AFINET
1928 if (ptr->has_ip) {
1929 printf(_(" %s addr:%s "), ap->name,
1930 ap->sprint(&ptr->addr, 1));
1931 if (ptr->flags & IFF_POINTOPOINT) {
1932 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1933 }
1934 if (ptr->flags & IFF_BROADCAST) {
1935 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1936 }
1937 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1938 }
1939#endif
1940
1941#if HAVE_AFINET6
1942 /* FIXME: should be integrated into interface.c. */
1943
1944 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1945 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1946 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1947 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1948 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1949 if (!strcmp(devname, ptr->name)) {
1950 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1951 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1952 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1953 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1954 printf(_(" inet6 addr: %s/%d"),
1955 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1956 printf(_(" Scope:"));
1957 switch (scope) {
1958 case 0:
1959 printf(_("Global"));
1960 break;
1961 case IPV6_ADDR_LINKLOCAL:
1962 printf(_("Link"));
1963 break;
1964 case IPV6_ADDR_SITELOCAL:
1965 printf(_("Site"));
1966 break;
1967 case IPV6_ADDR_COMPATv4:
1968 printf(_("Compat"));
1969 break;
1970 case IPV6_ADDR_LOOPBACK:
1971 printf(_("Host"));
1972 break;
1973 default:
1974 printf(_("Unknown"));
1975 }
1976 printf("\n");
1977 }
1978 }
1979 fclose(f);
1980 }
1981#endif
1982
1983#if HAVE_AFIPX
1984 if (ipxtype == NULL)
1985 ipxtype = get_afntype(AF_IPX);
1986
1987 if (ipxtype != NULL) {
1988 if (ptr->has_ipx_bb)
1989 printf(_(" IPX/Ethernet II addr:%s\n"),
1990 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1991 if (ptr->has_ipx_sn)
1992 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1993 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1994 if (ptr->has_ipx_e2)
1995 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1996 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1997 if (ptr->has_ipx_e3)
1998 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1999 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
2000 }
2001#endif
2002
2003#if HAVE_AFATALK
2004 if (ddptype == NULL)
2005 ddptype = get_afntype(AF_APPLETALK);
2006 if (ddptype != NULL) {
2007 if (ptr->has_ddp)
2008 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
2009 }
2010#endif
2011
2012#if HAVE_AFECONET
2013 if (ectype == NULL)
2014 ectype = get_afntype(AF_ECONET);
2015 if (ectype != NULL) {
2016 if (ptr->has_econet)
2017 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
2018 }
2019#endif
2020
2021 printf(" ");
2022 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
2023 if (ptr->flags == 0)
2024 printf(_("[NO FLAGS] "));
2025 if (ptr->flags & IFF_UP)
2026 printf(_("UP "));
2027 if (ptr->flags & IFF_BROADCAST)
2028 printf(_("BROADCAST "));
2029 if (ptr->flags & IFF_DEBUG)
2030 printf(_("DEBUG "));
2031 if (ptr->flags & IFF_LOOPBACK)
2032 printf(_("LOOPBACK "));
2033 if (ptr->flags & IFF_POINTOPOINT)
2034 printf(_("POINTOPOINT "));
2035 if (ptr->flags & IFF_NOTRAILERS)
2036 printf(_("NOTRAILERS "));
2037 if (ptr->flags & IFF_RUNNING)
2038 printf(_("RUNNING "));
2039 if (ptr->flags & IFF_NOARP)
2040 printf(_("NOARP "));
2041 if (ptr->flags & IFF_PROMISC)
2042 printf(_("PROMISC "));
2043 if (ptr->flags & IFF_ALLMULTI)
2044 printf(_("ALLMULTI "));
2045 if (ptr->flags & IFF_SLAVE)
2046 printf(_("SLAVE "));
2047 if (ptr->flags & IFF_MASTER)
2048 printf(_("MASTER "));
2049 if (ptr->flags & IFF_MULTICAST)
2050 printf(_("MULTICAST "));
2051#ifdef HAVE_DYNAMIC
2052 if (ptr->flags & IFF_DYNAMIC)
2053 printf(_("DYNAMIC "));
2054#endif
2055 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2056 printf(_(" MTU:%d Metric:%d"),
2057 ptr->mtu, ptr->metric ? ptr->metric : 1);
2058#ifdef SIOCSKEEPALIVE
2059 if (ptr->outfill || ptr->keepalive)
2060 printf(_(" Outfill:%d Keepalive:%d"),
2061 ptr->outfill, ptr->keepalive);
2062#endif
2063 printf("\n");
2064
2065 /* If needed, display the interface statistics. */
2066
2067 if (ptr->statistics_valid) {
2068 /* XXX: statistics are currently only printed for the primary address,
2069 * not for the aliases, although strictly speaking they're shared
2070 * by all addresses.
2071 */
2072 printf(" ");
2073
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002074 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002075 ptr->stats.rx_packets, ptr->stats.rx_errors,
2076 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2077 ptr->stats.rx_frame_errors);
2078 if (can_compress)
2079 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002080 printf(" ");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002081 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002082 ptr->stats.tx_packets, ptr->stats.tx_errors,
2083 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2084 ptr->stats.tx_carrier_errors);
2085 printf(_(" collisions:%lu "), ptr->stats.collisions);
2086 if (can_compress)
2087 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2088 if (ptr->tx_queue_len != -1)
2089 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002090 printf("\n R");
2091 print_bytes_scaled(ptr->stats.rx_bytes, " T");
Manuel Novoa III 0e0883e2001-03-15 15:37:48 +00002092 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002093
Eric Andersenf15d4da2001-03-06 00:48:59 +00002094 }
2095
2096 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2097 ptr->map.base_addr)) {
2098 printf(" ");
2099 if (ptr->map.irq)
2100 printf(_("Interrupt:%d "), ptr->map.irq);
2101 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2102 I/O maps */
Eric Andersen8e392922001-10-27 03:28:53 +00002103 printf(_("Base address:0x%lx "), (unsigned long)ptr->map.base_addr);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002104 if (ptr->map.mem_start) {
2105 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2106 }
2107 if (ptr->map.dma)
2108 printf(_("DMA chan:%x "), ptr->map.dma);
2109 printf("\n");
2110 }
2111 printf("\n");
2112}
2113
2114
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002115static int do_if_print(struct interface *ife, void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002116{
2117 int *opt_a = (int *) cookie;
2118 int res;
2119
2120 res = do_if_fetch(ife);
2121 if (res >= 0) {
2122 if ((ife->flags & IFF_UP) || *opt_a)
2123 ife_print(ife);
2124 }
2125 return res;
2126}
2127
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002128static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002129{
2130 struct interface *ife = NULL;
2131
2132 if (if_readlist_proc(name) < 0)
2133 return NULL;
2134 ife = add_interface(name);
2135 return ife;
2136}
2137
2138/* for ipv4 add/del modes */
2139static int if_print(char *ifname)
2140{
2141 int res;
2142
2143 if (!ifname) {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002144 res = for_all_interfaces(do_if_print, &interface_opt_a);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002145 } else {
2146 struct interface *ife;
2147
2148 ife = lookup_interface(ifname);
2149 res = do_if_fetch(ife);
2150 if (res >= 0)
2151 ife_print(ife);
2152 }
2153 return res;
2154}
2155
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002156int display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002157{
2158 int status;
2159
2160 /* Create a channel to the NET kernel. */
2161 if ((skfd = sockets_open(0)) < 0) {
Manuel Novoa III 78f57462001-03-10 02:00:54 +00002162 perror_msg_and_die("socket");
Eric Andersenf15d4da2001-03-06 00:48:59 +00002163 }
2164
2165 /* Do we have to show the current setup? */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002166 status = if_print(ifname);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002167 close(skfd);
2168 exit(status < 0);
2169}