blob: a80cd5524cf44bebacc5a500f5ea742e30fa6890 [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 *
Manuel Novoa III 0e0883e2001-03-15 15:37:48 +00006 * Version: $Id: interface.c,v 1.5 2001/03/15 15:37:48 mjn3 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#include "busybox.h"
41
42/*
43 *
44 * Protocol Families.
45 *
46 */
47#define HAVE_AFINET 1
48#undef HAVE_AFINET6
49#undef HAVE_AFIPX
50#undef HAVE_AFATALK
51#undef HAVE_AFNETROM
52#undef HAVE_AFX25
53#undef HAVE_AFECONET
54
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>
82#include <arpa/nameser.h>
83
84#define _(x) x
85#define _PATH_PROCNET_DEV "/proc/net/dev"
86#define new(p) ((p) = xcalloc(1,sizeof(*(p))))
Eric Andersenf15d4da2001-03-06 00:48:59 +000087#define KRELEASE(maj,min,patch) ((maj) * 10000 + (min)*1000 + (patch))
88
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
118#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
119#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;
178static int flag_ipx;
179static int flag_ax25;
180static int flag_ddp;
181static int flag_netrom;
182static int flag_inet;
183static int flag_inet6;
184static int flag_econet;
185static int flag_x25 = 0;
186static int flag_ash;
Eric Andersenf15d4da2001-03-06 00:48:59 +0000187
188
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000189static struct aftrans_t {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000190 char *alias;
191 char *name;
192 int *flag;
193} aftrans[] = {
194
195 {
196 "ax25", "ax25", &flag_ax25
197 },
198 {
199 "ip", "inet", &flag_inet
200 },
201 {
202 "ip6", "inet6", &flag_inet6
203 },
204 {
205 "ipx", "ipx", &flag_ipx
206 },
207 {
208 "appletalk", "ddp", &flag_ddp
209 },
210 {
211 "netrom", "netrom", &flag_netrom
212 },
213 {
214 "inet", "inet", &flag_inet
215 },
216 {
217 "inet6", "inet6", &flag_inet6
218 },
219 {
220 "ddp", "ddp", &flag_ddp
221 },
222 {
223 "unix", "unix", &flag_unx
224 },
225 {
226 "tcpip", "inet", &flag_inet
227 },
228 {
229 "econet", "ec", &flag_econet
230 },
231 {
232 "x25", "x25", &flag_x25
233 },
234 {
235 "ash", "ash", &flag_ash
236 },
237 {
238 0, 0, 0
239 }
240};
241
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000242static char afname[256] = "";
243#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000244
245#if HAVE_AFUNIX
246
247/* Display a UNIX domain address. */
248static char *UNIX_print(unsigned char *ptr)
249{
250 return (ptr);
251}
252
253
254/* Display a UNIX domain address. */
255static char *UNIX_sprint(struct sockaddr *sap, int numeric)
256{
257 static char buf[64];
258
259 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
260 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
261 return (UNIX_print(sap->sa_data));
262}
263
264
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000265static struct aftype unix_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000266{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000267 "unix", "UNIX Domain", AF_UNIX, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000268 UNIX_print, UNIX_sprint, NULL, NULL,
269 NULL, NULL, NULL,
270 -1,
271 "/proc/net/unix"
272};
273#endif /* HAVE_AFUNIX */
274
275#if HAVE_AFINET
276
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000277#if 0
Eric Andersenf15d4da2001-03-06 00:48:59 +0000278extern int h_errno; /* some netdb.h versions don't export this */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000279#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +0000280
281/* cache */
282struct addr {
283 struct sockaddr_in addr;
284 char *name;
285 int host;
286 struct addr *next;
287};
288
289static struct addr *INET_nn = NULL; /* addr-to-name cache */
290
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000291#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000292static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
293{
294 struct hostent *hp;
295 struct netent *np;
296
297 /* Grmpf. -FvK */
298 sin->sin_family = AF_INET;
299 sin->sin_port = 0;
300
301 /* Default is special, meaning 0.0.0.0. */
302 if (!strcmp(name, "default")) {
303 sin->sin_addr.s_addr = INADDR_ANY;
304 return (1);
305 }
306 /* Look to see if it's a dotted quad. */
307 if (inet_aton(name, &sin->sin_addr)) {
308 return 0;
309 }
310 /* If we expect this to be a hostname, try hostname database first */
311#ifdef DEBUG
312 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
313#endif
314 if (hostfirst &&
315 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
316 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
317 sizeof(struct in_addr));
318 return 0;
319 }
320 /* Try the NETWORKS database to see if this is a known network. */
321#ifdef DEBUG
322 fprintf (stderr, "getnetbyname (%s)\n", name);
323#endif
324 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
325 sin->sin_addr.s_addr = htonl(np->n_net);
326 return 1;
327 }
328 if (hostfirst) {
329 /* Don't try again */
330 errno = h_errno;
331 return -1;
332 }
333#ifdef DEBUG
334 res_init();
335 _res.options |= RES_DEBUG;
336#endif
337
338#ifdef DEBUG
339 fprintf (stderr, "gethostbyname (%s)\n", name);
340#endif
341 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
342 errno = h_errno;
343 return -1;
344 }
345 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
346 sizeof(struct in_addr));
347
348 return 0;
349}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000350#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000351
352/* numeric: & 0x8000: default instead of *,
353 * & 0x4000: host instead of net,
354 * & 0x0fff: don't resolve
355 */
356static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
357 int numeric, unsigned int netmask)
358{
359 struct hostent *ent;
360 struct netent *np;
361 struct addr *pn;
362 unsigned long ad, host_ad;
363 int host = 0;
364
365 /* Grmpf. -FvK */
366 if (sin->sin_family != AF_INET) {
367#ifdef DEBUG
368 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), sin->sin_family);
369#endif
370 errno = EAFNOSUPPORT;
371 return (-1);
372 }
373 ad = (unsigned long) sin->sin_addr.s_addr;
374#ifdef DEBUG
375 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
376#endif
377 if (ad == INADDR_ANY) {
378 if ((numeric & 0x0FFF) == 0) {
379 if (numeric & 0x8000)
380 safe_strncpy(name, "default", len);
381 else
382 safe_strncpy(name, "*", len);
383 return (0);
384 }
385 }
386 if (numeric & 0x0FFF) {
387 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
388 return (0);
389 }
390
391 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
392 host = 1;
393#if 0
394 INET_nn = NULL;
395#endif
396 pn = INET_nn;
397 while (pn != NULL) {
398 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
399 safe_strncpy(name, pn->name, len);
400#ifdef DEBUG
401 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
402#endif
403 return (0);
404 }
405 pn = pn->next;
406 }
407
408 host_ad = ntohl(ad);
409 np = NULL;
410 ent = NULL;
411 if (host) {
412#ifdef DEBUG
413 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
414#endif
415 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
416 if (ent != NULL)
417 safe_strncpy(name, ent->h_name, len);
418 } else {
419#ifdef DEBUG
420 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
421#endif
422 np = getnetbyaddr(host_ad, AF_INET);
423 if (np != NULL)
424 safe_strncpy(name, np->n_name, len);
425 }
426 if ((ent == NULL) && (np == NULL))
427 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
Eric Andersen0f430e32001-03-06 20:54:43 +0000428 pn = (struct addr *) xmalloc(sizeof(struct addr));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000429 pn->addr = *sin;
430 pn->next = INET_nn;
431 pn->host = host;
Eric Andersen0f430e32001-03-06 20:54:43 +0000432 pn->name = (char *) xmalloc(strlen(name) + 1);
Eric Andersenf15d4da2001-03-06 00:48:59 +0000433 strcpy(pn->name, name);
434 INET_nn = pn;
435
436 return (0);
437}
438
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000439#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000440static void INET_reserror(char *text)
441{
442 herror(text);
443}
444
Eric Andersenf15d4da2001-03-06 00:48:59 +0000445/* Display an Internet socket address. */
446static char *INET_print(unsigned char *ptr)
447{
448 return (inet_ntoa((*(struct in_addr *) ptr)));
449}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000450#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000451
452/* Display an Internet socket address. */
453static char *INET_sprint(struct sockaddr *sap, int numeric)
454{
455 static char buff[128];
456
457 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
458 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
459
460 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
461 numeric, 0xffffff00) != 0)
462 return (NULL);
463
464 return (buff);
465}
466
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000467#ifdef KEEP_UNUSED
468static char *INET_sprintmask(struct sockaddr *sap, int numeric,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000469 unsigned int netmask)
470{
471 static char buff[128];
472
473 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
474 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
475 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
476 numeric, netmask) != 0)
477 return (NULL);
478 return (buff);
479}
480
Eric Andersenf15d4da2001-03-06 00:48:59 +0000481static int INET_getsock(char *bufp, struct sockaddr *sap)
482{
483 char *sp = bufp, *bp;
484 unsigned int i;
485 unsigned val;
486 struct sockaddr_in *sin;
487
488 sin = (struct sockaddr_in *) sap;
489 sin->sin_family = AF_INET;
490 sin->sin_port = 0;
491
492 val = 0;
493 bp = (char *) &val;
494 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
495 *sp = toupper(*sp);
496
497 if ((*sp >= 'A') && (*sp <= 'F'))
498 bp[i] |= (int) (*sp - 'A') + 10;
499 else if ((*sp >= '0') && (*sp <= '9'))
500 bp[i] |= (int) (*sp - '0');
501 else
502 return (-1);
503
504 bp[i] <<= 4;
505 sp++;
506 *sp = toupper(*sp);
507
508 if ((*sp >= 'A') && (*sp <= 'F'))
509 bp[i] |= (int) (*sp - 'A') + 10;
510 else if ((*sp >= '0') && (*sp <= '9'))
511 bp[i] |= (int) (*sp - '0');
512 else
513 return (-1);
514
515 sp++;
516 }
517 sin->sin_addr.s_addr = htonl(val);
518
519 return (sp - bufp);
520}
521
522static int INET_input(int type, char *bufp, struct sockaddr *sap)
523{
524 switch (type) {
525 case 1:
526 return (INET_getsock(bufp, sap));
527 case 256:
528 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
529 default:
530 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
531 }
532}
533
534static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
535{
536 struct sockaddr_in *mask = (struct sockaddr_in *) m;
537 char *slash, *end;
538 int prefix;
539
540 if ((slash = strchr(adr, '/')) == NULL)
541 return 0;
542
543 *slash++ = '\0';
544 prefix = strtoul(slash, &end, 0);
545 if (*end != '\0')
546 return -1;
547
548 if (name) {
549 sprintf(name, "/%d", prefix);
550 }
551 mask->sin_family = AF_INET;
552 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
553 return 1;
554}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000555#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000556
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000557static struct aftype inet_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000558{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000559 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
560 NULL /* UNUSED INET_print */, INET_sprint,
561 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000562 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000563 NULL /* UNUSED INET_getnetmask */,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000564 -1,
565 NULL
566};
567
568#endif /* HAVE_AFINET */
569
570/* Display an UNSPEC address. */
571static char *UNSPEC_print(unsigned char *ptr)
572{
573 static char buff[64];
574 char *pos;
575 unsigned int i;
576
577 pos = buff;
578 for (i = 0; i < sizeof(struct sockaddr); i++) {
579 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
580 }
581 buff[strlen(buff) - 1] = '\0';
582 return (buff);
583}
584
585/* Display an UNSPEC socket address. */
586static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
587{
588 static char buf[64];
589
590 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
591 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
592 return (UNSPEC_print(sap->sa_data));
593}
594
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000595static struct aftype unspec_aftype =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000596{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000597 "unspec", "UNSPEC", AF_UNSPEC, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +0000598 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
599 NULL,
600};
601
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000602static struct aftype *aftypes[] =
Eric Andersenf15d4da2001-03-06 00:48:59 +0000603{
604#if HAVE_AFUNIX
605 &unix_aftype,
606#endif
607#if HAVE_AFINET
608 &inet_aftype,
609#endif
610#if HAVE_AFINET6
611 &inet6_aftype,
612#endif
613#if HAVE_AFAX25
614 &ax25_aftype,
615#endif
616#if HAVE_AFNETROM
617 &netrom_aftype,
618#endif
619#if HAVE_AFROSE
620 &rose_aftype,
621#endif
622#if HAVE_AFIPX
623 &ipx_aftype,
624#endif
625#if HAVE_AFATALK
626 &ddp_aftype,
627#endif
628#if HAVE_AFECONET
629 &ec_aftype,
630#endif
631#if HAVE_AFASH
632 &ash_aftype,
633#endif
634#if HAVE_AFX25
635 &x25_aftype,
636#endif
637 &unspec_aftype,
638 NULL
639};
640
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000641#ifdef KEEP_UNUSED
642static short sVafinit = 0;
643
644static void afinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +0000645{
646 unspec_aftype.title = _("UNSPEC");
647#if HAVE_AFUNIX
648 unix_aftype.title = _("UNIX Domain");
649#endif
650#if HAVE_AFINET
651 inet_aftype.title = _("DARPA Internet");
652#endif
653#if HAVE_AFINET6
654 inet6_aftype.title = _("IPv6");
655#endif
656#if HAVE_AFAX25
657 ax25_aftype.title = _("AMPR AX.25");
658#endif
659#if HAVE_AFNETROM
660 netrom_aftype.title = _("AMPR NET/ROM");
661#endif
662#if HAVE_AFIPX
663 ipx_aftype.title = _("Novell IPX");
664#endif
665#if HAVE_AFATALK
666 ddp_aftype.title = _("Appletalk DDP");
667#endif
668#if HAVE_AFECONET
669 ec_aftype.title = _("Econet");
670#endif
671#if HAVE_AFX25
672 x25_aftype.title = _("CCITT X.25");
673#endif
674#if HAVE_AFROSE
675 rose_aftype.title = _("AMPR ROSE");
676#endif
677#if HAVE_AFASH
678 ash_aftype.title = _("Ash");
679#endif
680 sVafinit = 1;
681}
682
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000683static int aftrans_opt(const char *arg)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000684{
685 struct aftrans_t *paft;
686 char *tmp1, *tmp2;
687 char buf[256];
688
689 safe_strncpy(buf, arg, sizeof(buf));
690
691 tmp1 = buf;
692
693 while (tmp1) {
694
695 tmp2 = index(tmp1, ',');
696
697 if (tmp2)
698 *(tmp2++) = '\0';
699
700 paft = aftrans;
701 for (paft = aftrans; paft->alias; paft++) {
702 if (strcmp(tmp1, paft->alias))
703 continue;
704 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
705 fprintf(stderr, _("Too much address family arguments.\n"));
706 return (0);
707 }
708 if (paft->flag)
709 (*paft->flag)++;
710 if (afname[0])
711 strcat(afname, ",");
712 strcat(afname, paft->name);
713 break;
714 }
715 if (!paft->alias) {
716 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
717 return (1);
718 }
719 tmp1 = tmp2;
720 }
721
722 return (0);
723}
724
725/* set the default AF list from the program name or a constant value */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000726static void aftrans_def(char *tool, char *argv0, char *dflt)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000727{
728 char *tmp;
729 char *buf;
730
731 strcpy(afname, dflt);
732
733 if (!(tmp = strrchr(argv0, '/')))
734 tmp = argv0; /* no slash?! */
735 else
736 tmp++;
737
738 if (!(buf = strdup(tmp)))
739 return;
740
741 if (strlen(tool) >= strlen(tmp)) {
742 free(buf);
743 return;
744 }
745 tmp = buf + (strlen(tmp) - strlen(tool));
746
747 if (strcmp(tmp, tool) != 0) {
748 free(buf);
749 return;
750 }
751 *tmp = '\0';
752 if ((tmp = strchr(buf, '_')))
753 *tmp = '\0';
754
755 afname[0] = '\0';
756 if (aftrans_opt(buf))
757 strcpy(afname, buf);
758
759 free(buf);
760}
761
Eric Andersenf15d4da2001-03-06 00:48:59 +0000762/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000763static struct aftype *get_aftype(const char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000764{
765 struct aftype **afp;
766
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000767#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000768 if (!sVafinit)
769 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000770#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000771
772 afp = aftypes;
773 while (*afp != NULL) {
774 if (!strcmp((*afp)->name, name))
775 return (*afp);
776 afp++;
777 }
778 if (index(name, ','))
779 fprintf(stderr, _("Please don't supply more than one address family.\n"));
780 return (NULL);
781}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000782#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000783
784/* Check our protocol family table for this family. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000785static struct aftype *get_afntype(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000786{
787 struct aftype **afp;
788
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000789#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000790 if (!sVafinit)
791 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000792#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000793
794 afp = aftypes;
795 while (*afp != NULL) {
796 if ((*afp)->af == af)
797 return (*afp);
798 afp++;
799 }
800 return (NULL);
801}
802
803/* Check our protocol family table for this family and return its socket */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000804static int get_socket_for_af(int af)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000805{
806 struct aftype **afp;
807
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000808#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000809 if (!sVafinit)
810 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000811#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000812
813 afp = aftypes;
814 while (*afp != NULL) {
815 if ((*afp)->af == af)
816 return (*afp)->fd;
817 afp++;
818 }
819 return -1;
820}
821
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000822#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000823/* type: 0=all, 1=getroute */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000824static void print_aflist(int type) {
Eric Andersenf15d4da2001-03-06 00:48:59 +0000825 int count = 0;
826 char * txt;
827 struct aftype **afp;
828
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000829#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +0000830 if (!sVafinit)
831 afinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000832#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000833
834 afp = aftypes;
835 while (*afp != NULL) {
836 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
837 afp++; continue;
838 }
839 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
840 txt = (*afp)->name; if (!txt) txt = "..";
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000841 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +0000842 count++;
843 afp++;
844 }
845 fprintf(stderr,"\n");
846}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000847#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000848
849struct user_net_device_stats {
850 unsigned long long rx_packets; /* total packets received */
851 unsigned long long tx_packets; /* total packets transmitted */
852 unsigned long long rx_bytes; /* total bytes received */
853 unsigned long long tx_bytes; /* total bytes transmitted */
854 unsigned long rx_errors; /* bad packets received */
855 unsigned long tx_errors; /* packet transmit problems */
856 unsigned long rx_dropped; /* no space in linux buffers */
857 unsigned long tx_dropped; /* no space available in linux */
858 unsigned long rx_multicast; /* multicast packets received */
859 unsigned long rx_compressed;
860 unsigned long tx_compressed;
861 unsigned long collisions;
862
863 /* detailed rx_errors: */
864 unsigned long rx_length_errors;
865 unsigned long rx_over_errors; /* receiver ring buff overflow */
866 unsigned long rx_crc_errors; /* recved pkt with crc error */
867 unsigned long rx_frame_errors; /* recv'd frame alignment error */
868 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
869 unsigned long rx_missed_errors; /* receiver missed packet */
870 /* detailed tx_errors */
871 unsigned long tx_aborted_errors;
872 unsigned long tx_carrier_errors;
873 unsigned long tx_fifo_errors;
874 unsigned long tx_heartbeat_errors;
875 unsigned long tx_window_errors;
876};
877
878struct interface {
879 struct interface *next, *prev;
880 char name[IFNAMSIZ]; /* interface name */
881 short type; /* if type */
882 short flags; /* various flags */
883 int metric; /* routing metric */
884 int mtu; /* MTU value */
885 int tx_queue_len; /* transmit queue length */
886 struct ifmap map; /* hardware setup */
887 struct sockaddr addr; /* IP address */
888 struct sockaddr dstaddr; /* P-P IP address */
889 struct sockaddr broadaddr; /* IP broadcast address */
890 struct sockaddr netmask; /* IP network mask */
891 struct sockaddr ipxaddr_bb; /* IPX network address */
892 struct sockaddr ipxaddr_sn; /* IPX network address */
893 struct sockaddr ipxaddr_e3; /* IPX network address */
894 struct sockaddr ipxaddr_e2; /* IPX network address */
895 struct sockaddr ddpaddr; /* Appletalk DDP address */
896 struct sockaddr ecaddr; /* Econet address */
897 int has_ip;
898 int has_ipx_bb;
899 int has_ipx_sn;
900 int has_ipx_e3;
901 int has_ipx_e2;
902 int has_ax25;
903 int has_ddp;
904 int has_econet;
905 char hwaddr[32]; /* HW address */
906 int statistics_valid;
907 struct user_net_device_stats stats; /* statistics */
908 int keepalive; /* keepalive value for SLIP */
909 int outfill; /* outfill value for SLIP */
910};
911
912
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000913int interface_opt_a = 0; /* show all interfaces */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000914
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000915#ifdef KEEP_UNUSED
916static int opt_i = 0; /* show the statistics */
917static int opt_v = 0; /* debugging output flag */
918
919static int addr_family = 0; /* currently selected AF */
920#endif /* KEEP_UNUSED */
921
Eric Andersenf15d4da2001-03-06 00:48:59 +0000922static struct interface *int_list, *int_last;
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000923static int skfd = -1; /* generic raw socket desc. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000924
925
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000926static int sockets_open(int family)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000927{
928 struct aftype **aft;
929 int sfd = -1;
930 static int force = -1;
931
932 if (force < 0) {
933 force = 0;
934 if (get_kernel_revision() < KRELEASE(2, 1, 0))
935 force = 1;
936 if (access("/proc/net", R_OK))
937 force = 1;
938 }
939 for (aft = aftypes; *aft; aft++) {
940 struct aftype *af = *aft;
941 int type = SOCK_DGRAM;
942 if (af->af == AF_UNSPEC)
943 continue;
944 if (family && family != af->af)
945 continue;
946 if (af->fd != -1) {
947 sfd = af->fd;
948 continue;
949 }
950 /* Check some /proc file first to not stress kmod */
951 if (!family && !force && af->flag_file) {
952 if (access(af->flag_file, R_OK))
953 continue;
954 }
955#if HAVE_AFNETROM
956 if (af->af == AF_NETROM)
957 type = SOCK_SEQPACKET;
958#endif
959#if HAVE_AFX25
960 if (af->af == AF_X25)
961 type = SOCK_SEQPACKET;
962#endif
963 af->fd = socket(af->af, type, 0);
964 if (af->fd >= 0)
965 sfd = af->fd;
966 }
967 if (sfd < 0)
968 fprintf(stderr, _("No usable address families found.\n"));
969 return sfd;
970}
971
972/* like strcmp(), but knows about numbers */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000973static int nstrcmp(const char *astr, const char *b)
Eric Andersenf15d4da2001-03-06 00:48:59 +0000974{
975 const char *a = astr;
976
977 while (*a == *b) {
978 if (*a == '\0')
979 return 0;
980 a++;
981 b++;
982 }
983 if (isdigit(*a)) {
984 if (!isdigit(*b))
985 return -1;
986 while (a > astr) {
987 a--;
988 if (!isdigit(*a)) {
989 a++;
990 break;
991 }
992 if (!isdigit(*b))
993 return -1;
994 b--;
995 }
996 return atoi(a) > atoi(b) ? 1 : -1;
997 }
998 return *a - *b;
999}
1000
1001static struct interface *add_interface(char *name)
1002{
1003 struct interface *ife, **nextp, *new;
1004
1005 for (ife = int_last; ife; ife = ife->prev) {
1006 int n = nstrcmp(ife->name, name);
1007 if (n == 0)
1008 return ife;
1009 if (n < 0)
1010 break;
1011 }
1012 new(new);
1013 safe_strncpy(new->name, name, IFNAMSIZ);
1014 nextp = ife ? &ife->next : &int_list;
1015 new->prev = ife;
1016 new->next = *nextp;
1017 if (new->next)
1018 new->next->prev = new;
1019 else
1020 int_last = new;
1021 *nextp = new;
1022 return new;
1023}
1024
1025
1026static int if_readconf(void)
1027{
1028 int numreqs = 30;
1029 struct ifconf ifc;
1030 struct ifreq *ifr;
1031 int n, err = -1;
1032 int skfd;
1033
1034 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1035 (as of 2.1.128) */
1036 skfd = get_socket_for_af(AF_INET);
1037 if (skfd < 0) {
1038 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1039 strerror(errno));
1040 /* Try to soldier on with whatever socket we can get hold of. */
1041 skfd = sockets_open(0);
1042 if (skfd < 0)
1043 return -1;
1044 }
1045
1046 ifc.ifc_buf = NULL;
1047 for (;;) {
1048 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1049 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1050
1051 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
1052 perror("SIOCGIFCONF");
1053 goto out;
1054 }
1055 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1056 /* assume it overflowed and try again */
1057 numreqs += 10;
1058 continue;
1059 }
1060 break;
1061 }
1062
1063 ifr = ifc.ifc_req;
1064 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1065 add_interface(ifr->ifr_name);
1066 ifr++;
1067 }
1068 err = 0;
1069
1070out:
1071 free(ifc.ifc_buf);
1072 return err;
1073}
1074
1075static char *get_name(char *name, char *p)
1076{
1077 while (isspace(*p))
1078 p++;
1079 while (*p) {
1080 if (isspace(*p))
1081 break;
1082 if (*p == ':') { /* could be an alias */
1083 char *dot = p, *dotname = name;
1084 *name++ = *p++;
1085 while (isdigit(*p))
1086 *name++ = *p++;
1087 if (*p != ':') { /* it wasn't, backup */
1088 p = dot;
1089 name = dotname;
1090 }
1091 if (*p == '\0')
1092 return NULL;
1093 p++;
1094 break;
1095 }
1096 *name++ = *p++;
1097 }
1098 *name++ = '\0';
1099 return p;
1100}
1101
1102static int get_dev_fields(char *bp, struct interface *ife)
1103{
1104 switch (procnetdev_vsn) {
1105 case 3:
1106 sscanf(bp,
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001107 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001108 &ife->stats.rx_bytes,
1109 &ife->stats.rx_packets,
1110 &ife->stats.rx_errors,
1111 &ife->stats.rx_dropped,
1112 &ife->stats.rx_fifo_errors,
1113 &ife->stats.rx_frame_errors,
1114 &ife->stats.rx_compressed,
1115 &ife->stats.rx_multicast,
1116
1117 &ife->stats.tx_bytes,
1118 &ife->stats.tx_packets,
1119 &ife->stats.tx_errors,
1120 &ife->stats.tx_dropped,
1121 &ife->stats.tx_fifo_errors,
1122 &ife->stats.collisions,
1123 &ife->stats.tx_carrier_errors,
1124 &ife->stats.tx_compressed);
1125 break;
1126 case 2:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001127 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001128 &ife->stats.rx_bytes,
1129 &ife->stats.rx_packets,
1130 &ife->stats.rx_errors,
1131 &ife->stats.rx_dropped,
1132 &ife->stats.rx_fifo_errors,
1133 &ife->stats.rx_frame_errors,
1134
1135 &ife->stats.tx_bytes,
1136 &ife->stats.tx_packets,
1137 &ife->stats.tx_errors,
1138 &ife->stats.tx_dropped,
1139 &ife->stats.tx_fifo_errors,
1140 &ife->stats.collisions,
1141 &ife->stats.tx_carrier_errors);
1142 ife->stats.rx_multicast = 0;
1143 break;
1144 case 1:
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001145 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
Eric Andersenf15d4da2001-03-06 00:48:59 +00001146 &ife->stats.rx_packets,
1147 &ife->stats.rx_errors,
1148 &ife->stats.rx_dropped,
1149 &ife->stats.rx_fifo_errors,
1150 &ife->stats.rx_frame_errors,
1151
1152 &ife->stats.tx_packets,
1153 &ife->stats.tx_errors,
1154 &ife->stats.tx_dropped,
1155 &ife->stats.tx_fifo_errors,
1156 &ife->stats.collisions,
1157 &ife->stats.tx_carrier_errors);
1158 ife->stats.rx_bytes = 0;
1159 ife->stats.tx_bytes = 0;
1160 ife->stats.rx_multicast = 0;
1161 break;
1162 }
1163 return 0;
1164}
1165
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001166static inline int procnetdev_version(char *buf)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001167{
1168 if (strstr(buf, "compressed"))
1169 return 3;
1170 if (strstr(buf, "bytes"))
1171 return 2;
1172 return 1;
1173}
1174
1175static int if_readlist_proc(char *target)
1176{
1177 static int proc_read;
1178 FILE *fh;
1179 char buf[512];
1180 struct interface *ife;
1181 int err;
1182
1183 if (proc_read)
1184 return 0;
1185 if (!target)
1186 proc_read = 1;
1187
1188 fh = fopen(_PATH_PROCNET_DEV, "r");
1189 if (!fh) {
1190 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1191 _PATH_PROCNET_DEV, strerror(errno));
1192 return if_readconf();
1193 }
1194 fgets(buf, sizeof buf, fh); /* eat line */
1195 fgets(buf, sizeof buf, fh);
1196
1197#if 0 /* pretty, but can't cope with missing fields */
1198 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1199 "face", "", /* parsed separately */
1200 "bytes", "%lu",
1201 "packets", "%lu",
1202 "errs", "%lu",
1203 "drop", "%lu",
1204 "fifo", "%lu",
1205 "frame", "%lu",
1206 "compressed", "%lu",
1207 "multicast", "%lu",
1208 "bytes", "%lu",
1209 "packets", "%lu",
1210 "errs", "%lu",
1211 "drop", "%lu",
1212 "fifo", "%lu",
1213 "colls", "%lu",
1214 "carrier", "%lu",
1215 "compressed", "%lu",
1216 NULL);
1217 if (!fmt)
1218 return -1;
1219#else
1220 procnetdev_vsn = procnetdev_version(buf);
1221#endif
1222
1223 err = 0;
1224 while (fgets(buf, sizeof buf, fh)) {
1225 char *s, name[IFNAMSIZ];
1226 s = get_name(name, buf);
1227 ife = add_interface(name);
1228 get_dev_fields(s, ife);
1229 ife->statistics_valid = 1;
1230 if (target && !strcmp(target,name))
1231 break;
1232 }
1233 if (ferror(fh)) {
1234 perror(_PATH_PROCNET_DEV);
1235 err = -1;
1236 proc_read = 0;
1237 }
1238
1239#if 0
1240 free(fmt);
1241#endif
1242 fclose(fh);
1243 return err;
1244}
1245
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001246static int if_readlist(void)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001247{
1248 int err = if_readlist_proc(NULL);
1249 if (!err)
1250 err = if_readconf();
1251 return err;
1252}
1253
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001254static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001255{
1256 struct interface *ife;
1257
1258 if (!int_list && (if_readlist() < 0))
1259 return -1;
1260 for (ife = int_list; ife; ife = ife->next) {
1261 int err = doit(ife, cookie);
1262 if (err)
1263 return err;
1264 }
1265 return 0;
1266}
1267
1268/* Support for fetching an IPX address */
1269
1270#if HAVE_AFIPX
1271static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1272{
1273 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1274 return ioctl(sock, SIOCGIFADDR, ifr);
1275}
1276#endif
1277
1278
1279/* Fetch the interface configuration from the kernel. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001280static int if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001281{
1282 struct ifreq ifr;
1283 int fd;
1284 char *ifname = ife->name;
1285
1286 strcpy(ifr.ifr_name, ifname);
1287 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1288 return (-1);
1289 ife->flags = ifr.ifr_flags;
1290
1291 strcpy(ifr.ifr_name, ifname);
1292 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1293 memset(ife->hwaddr, 0, 32);
1294 else
1295 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1296
1297 ife->type = ifr.ifr_hwaddr.sa_family;
1298
1299 strcpy(ifr.ifr_name, ifname);
1300 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1301 ife->metric = 0;
1302 else
1303 ife->metric = ifr.ifr_metric;
1304
1305 strcpy(ifr.ifr_name, ifname);
1306 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1307 ife->mtu = 0;
1308 else
1309 ife->mtu = ifr.ifr_mtu;
1310
1311#ifdef HAVE_HWSLIP
1312 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1313 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1314 ife->type == ARPHRD_ADAPT) {
1315#ifdef SIOCGOUTFILL
1316 strcpy(ifr.ifr_name, ifname);
1317 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1318 ife->outfill = 0;
1319 else
1320 ife->outfill = (unsigned int) ifr.ifr_data;
1321#endif
1322#ifdef SIOCGKEEPALIVE
1323 strcpy(ifr.ifr_name, ifname);
1324 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1325 ife->keepalive = 0;
1326 else
1327 ife->keepalive = (unsigned int) ifr.ifr_data;
1328#endif
1329 }
1330#endif
1331
1332 strcpy(ifr.ifr_name, ifname);
1333 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1334 memset(&ife->map, 0, sizeof(struct ifmap));
1335 else
1336 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1337
1338 strcpy(ifr.ifr_name, ifname);
1339 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1340 memset(&ife->map, 0, sizeof(struct ifmap));
1341 else
1342 ife->map = ifr.ifr_map;
1343
1344#ifdef HAVE_TXQUEUELEN
1345 strcpy(ifr.ifr_name, ifname);
1346 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1347 ife->tx_queue_len = -1; /* unknown value */
1348 else
1349 ife->tx_queue_len = ifr.ifr_qlen;
1350#else
1351 ife->tx_queue_len = -1; /* unknown value */
1352#endif
1353
1354#if HAVE_AFINET
1355 /* IPv4 address? */
1356 fd = get_socket_for_af(AF_INET);
1357 if (fd >= 0) {
1358 strcpy(ifr.ifr_name, ifname);
1359 ifr.ifr_addr.sa_family = AF_INET;
1360 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1361 ife->has_ip = 1;
1362 ife->addr = ifr.ifr_addr;
1363 strcpy(ifr.ifr_name, ifname);
1364 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1365 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1366 else
1367 ife->dstaddr = ifr.ifr_dstaddr;
1368
1369 strcpy(ifr.ifr_name, ifname);
1370 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1371 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1372 else
1373 ife->broadaddr = ifr.ifr_broadaddr;
1374
1375 strcpy(ifr.ifr_name, ifname);
1376 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1377 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1378 else
1379 ife->netmask = ifr.ifr_netmask;
1380 } else
1381 memset(&ife->addr, 0, sizeof(struct sockaddr));
1382 }
1383#endif
1384
1385#if HAVE_AFATALK
1386 /* DDP address maybe ? */
1387 fd = get_socket_for_af(AF_APPLETALK);
1388 if (fd >= 0) {
1389 strcpy(ifr.ifr_name, ifname);
1390 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1391 ife->ddpaddr = ifr.ifr_addr;
1392 ife->has_ddp = 1;
1393 }
1394 }
1395#endif
1396
1397#if HAVE_AFIPX
1398 /* Look for IPX addresses with all framing types */
1399 fd = get_socket_for_af(AF_IPX);
1400 if (fd >= 0) {
1401 strcpy(ifr.ifr_name, ifname);
1402 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1403 ife->has_ipx_bb = 1;
1404 ife->ipxaddr_bb = ifr.ifr_addr;
1405 }
1406 strcpy(ifr.ifr_name, ifname);
1407 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1408 ife->has_ipx_sn = 1;
1409 ife->ipxaddr_sn = ifr.ifr_addr;
1410 }
1411 strcpy(ifr.ifr_name, ifname);
1412 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1413 ife->has_ipx_e3 = 1;
1414 ife->ipxaddr_e3 = ifr.ifr_addr;
1415 }
1416 strcpy(ifr.ifr_name, ifname);
1417 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1418 ife->has_ipx_e2 = 1;
1419 ife->ipxaddr_e2 = ifr.ifr_addr;
1420 }
1421 }
1422#endif
1423
1424#if HAVE_AFECONET
1425 /* Econet address maybe? */
1426 fd = get_socket_for_af(AF_ECONET);
1427 if (fd >= 0) {
1428 strcpy(ifr.ifr_name, ifname);
1429 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1430 ife->ecaddr = ifr.ifr_addr;
1431 ife->has_econet = 1;
1432 }
1433 }
1434#endif
1435
1436 return 0;
1437}
1438
1439
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001440static int do_if_fetch(struct interface *ife)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001441{
1442 if (if_fetch(ife) < 0) {
1443 char *errmsg;
1444 if (errno == ENODEV) {
1445 /* Give better error message for this case. */
1446 errmsg = _("Device not found");
1447 } else {
1448 errmsg = strerror(errno);
1449 }
1450 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1451 ife->name, errmsg);
1452 return -1;
1453 }
1454 return 0;
1455}
1456
1457/* This structure defines hardware protocols and their handlers. */
1458struct hwtype {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001459 const char *name;
1460 const char *title;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001461 int type;
1462 int alen;
1463 char *(*print) (unsigned char *);
1464 int (*input) (char *, struct sockaddr *);
1465 int (*activate) (int fd);
1466 int suppress_null_addr;
1467};
1468
1469/* Display an UNSPEC address. */
1470static char *pr_unspec(unsigned char *ptr)
1471{
1472 static char buff[64];
1473 char *pos;
1474 unsigned int i;
1475
1476 pos = buff;
1477 for (i = 0; i < sizeof(struct sockaddr); i++) {
1478 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
1479 }
1480 buff[strlen(buff) - 1] = '\0';
1481 return (buff);
1482}
1483
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001484static struct hwtype unspec_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001485{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001486 "unspec", "UNSPEC", -1, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001487 pr_unspec, NULL, NULL
1488};
1489
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001490static struct hwtype loop_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001491{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001492 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
Eric Andersenf15d4da2001-03-06 00:48:59 +00001493 NULL, NULL, NULL
1494};
1495
1496#if HAVE_HWETHER
1497#include <net/if_arp.h>
1498#include <linux/if_ether.h>
1499
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001500static struct hwtype ether_hwtype;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001501
1502/* Display an Ethernet address in readable format. */
1503static char *pr_ether(unsigned char *ptr)
1504{
1505 static char buff[64];
1506
1507 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1508 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1509 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1510 );
1511 return (buff);
1512}
1513
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001514#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001515/* Input an Ethernet address and convert to binary. */
1516static int in_ether(char *bufp, struct sockaddr *sap)
1517{
1518 unsigned char *ptr;
1519 char c, *orig;
1520 int i;
1521 unsigned val;
1522
1523 sap->sa_family = ether_hwtype.type;
1524 ptr = sap->sa_data;
1525
1526 i = 0;
1527 orig = bufp;
1528 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1529 val = 0;
1530 c = *bufp++;
1531 if (isdigit(c))
1532 val = c - '0';
1533 else if (c >= 'a' && c <= 'f')
1534 val = c - 'a' + 10;
1535 else if (c >= 'A' && c <= 'F')
1536 val = c - 'A' + 10;
1537 else {
1538#ifdef DEBUG
1539 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1540#endif
1541 errno = EINVAL;
1542 return (-1);
1543 }
1544 val <<= 4;
1545 c = *bufp;
1546 if (isdigit(c))
1547 val |= c - '0';
1548 else if (c >= 'a' && c <= 'f')
1549 val |= c - 'a' + 10;
1550 else if (c >= 'A' && c <= 'F')
1551 val |= c - 'A' + 10;
1552 else if (c == ':' || c == 0)
1553 val >>= 4;
1554 else {
1555#ifdef DEBUG
1556 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1557#endif
1558 errno = EINVAL;
1559 return (-1);
1560 }
1561 if (c != 0)
1562 bufp++;
1563 *ptr++ = (unsigned char) (val & 0377);
1564 i++;
1565
1566 /* We might get a semicolon here - not required. */
1567 if (*bufp == ':') {
1568 if (i == ETH_ALEN) {
1569#ifdef DEBUG
1570 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1571 orig)
1572#endif
1573 ; /* nothing */
1574 }
1575 bufp++;
1576 }
1577 }
1578
1579 /* That's it. Any trailing junk? */
1580 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1581#ifdef DEBUG
1582 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1583 errno = EINVAL;
1584 return (-1);
1585#endif
1586 }
1587#ifdef DEBUG
1588 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1589#endif
1590
1591 return (0);
1592}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001593#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001594
1595
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001596static struct hwtype ether_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001597{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001598 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1599 pr_ether, NULL /* UNUSED in_ether */, NULL
Eric Andersenf15d4da2001-03-06 00:48:59 +00001600};
1601
1602
1603#endif /* HAVE_HWETHER */
1604
1605
1606#if HAVE_HWPPP
1607
1608#include <net/if_arp.h>
1609
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001610#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001611/* Start the PPP encapsulation on the file descriptor. */
1612static int do_ppp(int fd)
1613{
1614 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1615 return -1;
1616}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001617#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001618
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001619static struct hwtype ppp_hwtype =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001620{
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001621 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1622 NULL, NULL, NULL /* UNUSED do_ppp */, 0
Eric Andersenf15d4da2001-03-06 00:48:59 +00001623};
1624
1625
1626#endif /* HAVE_PPP */
1627
1628static struct hwtype *hwtypes[] =
1629{
1630
1631 &loop_hwtype,
1632
1633#if HAVE_HWSLIP
1634 &slip_hwtype,
1635 &cslip_hwtype,
1636 &slip6_hwtype,
1637 &cslip6_hwtype,
1638 &adaptive_hwtype,
1639#endif
1640#if HAVE_HWSTRIP
1641 &strip_hwtype,
1642#endif
1643#if HAVE_HWASH
1644 &ash_hwtype,
1645#endif
1646#if HAVE_HWETHER
1647 &ether_hwtype,
1648#endif
1649#if HAVE_HWTR
1650 &tr_hwtype,
1651#ifdef ARPHRD_IEEE802_TR
1652 &tr_hwtype1,
1653#endif
1654#endif
1655#if HAVE_HWAX25
1656 &ax25_hwtype,
1657#endif
1658#if HAVE_HWNETROM
1659 &netrom_hwtype,
1660#endif
1661#if HAVE_HWROSE
1662 &rose_hwtype,
1663#endif
1664#if HAVE_HWTUNNEL
1665 &tunnel_hwtype,
1666#endif
1667#if HAVE_HWPPP
1668 &ppp_hwtype,
1669#endif
1670#if HAVE_HWHDLCLAPB
1671 &hdlc_hwtype,
1672 &lapb_hwtype,
1673#endif
1674#if HAVE_HWARC
1675 &arcnet_hwtype,
1676#endif
1677#if HAVE_HWFR
1678 &dlci_hwtype,
1679 &frad_hwtype,
1680#endif
1681#if HAVE_HWSIT
1682 &sit_hwtype,
1683#endif
1684#if HAVE_HWFDDI
1685 &fddi_hwtype,
1686#endif
1687#if HAVE_HWHIPPI
1688 &hippi_hwtype,
1689#endif
1690#if HAVE_HWIRDA
1691 &irda_hwtype,
1692#endif
1693#if HAVE_HWEC
1694 &ec_hwtype,
1695#endif
1696#if HAVE_HWX25
1697 &x25_hwtype,
1698#endif
1699 &unspec_hwtype,
1700 NULL
1701};
1702
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001703#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001704static short sVhwinit = 0;
1705
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001706static void hwinit()
Eric Andersenf15d4da2001-03-06 00:48:59 +00001707{
1708 loop_hwtype.title = _("Local Loopback");
1709 unspec_hwtype.title = _("UNSPEC");
1710#if HAVE_HWSLIP
1711 slip_hwtype.title = _("Serial Line IP");
1712 cslip_hwtype.title = _("VJ Serial Line IP");
1713 slip6_hwtype.title = _("6-bit Serial Line IP");
1714 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1715 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1716#endif
1717#if HAVE_HWETHER
1718 ether_hwtype.title = _("Ethernet");
1719#endif
1720#if HAVE_HWASH
1721 ash_hwtype.title = _("Ash");
1722#endif
1723#if HAVE_HWFDDI
1724 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1725#endif
1726#if HAVE_HWHIPPI
1727 hippi_hwtype.title = _("HIPPI");
1728#endif
1729#if HAVE_HWAX25
1730 ax25_hwtype.title = _("AMPR AX.25");
1731#endif
1732#if HAVE_HWROSE
1733 rose_hwtype.title = _("AMPR ROSE");
1734#endif
1735#if HAVE_HWNETROM
1736 netrom_hwtype.title = _("AMPR NET/ROM");
1737#endif
1738#if HAVE_HWX25
1739 x25_hwtype.title = _("generic X.25");
1740#endif
1741#if HAVE_HWTUNNEL
1742 tunnel_hwtype.title = _("IPIP Tunnel");
1743#endif
1744#if HAVE_HWPPP
1745 ppp_hwtype.title = _("Point-to-Point Protocol");
1746#endif
1747#if HAVE_HWHDLCLAPB
1748 hdlc_hwtype.title = _("(Cisco)-HDLC");
1749 lapb_hwtype.title = _("LAPB");
1750#endif
1751#if HAVE_HWARC
1752 arcnet_hwtype.title = _("ARCnet");
1753#endif
1754#if HAVE_HWFR
1755 dlci_hwtype.title = _("Frame Relay DLCI");
1756 frad_hwtype.title = _("Frame Relay Access Device");
1757#endif
1758#if HAVE_HWSIT
1759 sit_hwtype.title = _("IPv6-in-IPv4");
1760#endif
1761#if HAVE_HWIRDA
1762 irda_hwtype.title = _("IrLAP");
1763#endif
1764#if HAVE_HWTR
1765 tr_hwtype.title = _("16/4 Mbps Token Ring");
1766#ifdef ARPHRD_IEEE802_TR
1767 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1768#endif
1769#endif
1770#if HAVE_HWEC
1771 ec_hwtype.title = _("Econet");
1772#endif
1773 sVhwinit = 1;
1774}
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001775#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001776
1777#ifdef IFF_PORTSEL
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001778static const char *if_port_text[][4] =
Eric Andersenf15d4da2001-03-06 00:48:59 +00001779{
1780 /* Keep in step with <linux/netdevice.h> */
1781 {"unknown", NULL, NULL, NULL},
1782 {"10base2", "bnc", "coax", NULL},
1783 {"10baseT", "utp", "tpe", NULL},
1784 {"AUI", "thick", "db15", NULL},
1785 {"100baseT", NULL, NULL, NULL},
1786 {"100baseTX", NULL, NULL, NULL},
1787 {"100baseFX", NULL, NULL, NULL},
1788 {NULL, NULL, NULL, NULL},
1789};
1790#endif
1791
1792/* Check our hardware type table for this type. */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001793static struct hwtype *get_hwntype(int type)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001794{
1795 struct hwtype **hwp;
1796
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001797#ifdef KEEP_UNUSED
Eric Andersenf15d4da2001-03-06 00:48:59 +00001798 if (!sVhwinit)
1799 hwinit();
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001800#endif /* KEEP_UNUSED */
Eric Andersenf15d4da2001-03-06 00:48:59 +00001801
1802 hwp = hwtypes;
1803 while (*hwp != NULL) {
1804 if ((*hwp)->type == type)
1805 return (*hwp);
1806 hwp++;
1807 }
1808 return (NULL);
1809}
1810
1811/* return 1 if address is all zeros */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001812static int hw_null_address(struct hwtype *hw, void *ap)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001813{
1814 unsigned int i;
1815 unsigned char *address = (unsigned char *)ap;
1816 for (i = 0; i < hw->alen; i++)
1817 if (address[i])
1818 return 0;
1819 return 1;
1820}
1821
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001822static const char TRext[] = "\0\0k\0M";
1823
1824static void print_bytes_scaled(unsigned long long ull, const char *end)
1825{
1826 unsigned long long int_part;
1827 unsigned long frac_part;
1828 const char *ext;
1829 int i;
1830
1831 frac_part = 0;
1832 ext = TRext;
1833 int_part = ull;
1834 for (i=0 ; i<2 ; i++) {
1835 if (int_part >= 1024) {
1836 frac_part = ((int_part % 1024) * 10) / 1024;
1837 int_part /= 1024;
1838 ext += 2; /* Kb, Mb */
1839 }
1840 }
1841
1842 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1843}
1844
1845static void ife_print(struct interface *ptr)
Eric Andersenf15d4da2001-03-06 00:48:59 +00001846{
1847 struct aftype *ap;
1848 struct hwtype *hw;
1849 int hf;
1850 int can_compress = 0;
Eric Andersenf15d4da2001-03-06 00:48:59 +00001851
1852#if HAVE_AFIPX
1853 static struct aftype *ipxtype = NULL;
1854#endif
1855#if HAVE_AFECONET
1856 static struct aftype *ectype = NULL;
1857#endif
1858#if HAVE_AFATALK
1859 static struct aftype *ddptype = NULL;
1860#endif
1861#if HAVE_AFINET6
1862 FILE *f;
1863 char addr6[40], devname[20];
1864 struct sockaddr_in6 sap;
1865 int plen, scope, dad_status, if_idx;
1866 extern struct aftype inet6_aftype;
1867 char addr6p[8][5];
1868#endif
1869
1870 ap = get_afntype(ptr->addr.sa_family);
1871 if (ap == NULL)
1872 ap = get_afntype(0);
1873
1874 hf = ptr->type;
1875
1876 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1877 can_compress = 1;
1878
1879 hw = get_hwntype(hf);
1880 if (hw == NULL)
1881 hw = get_hwntype(-1);
1882
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00001883 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
Eric Andersenf15d4da2001-03-06 00:48:59 +00001884 /* For some hardware types (eg Ash, ATM) we don't print the
1885 hardware address if it's null. */
1886 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1887 hw->suppress_null_addr)))
1888 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1889#ifdef IFF_PORTSEL
1890 if (ptr->flags & IFF_PORTSEL) {
1891 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1892 if (ptr->flags & IFF_AUTOMEDIA)
1893 printf(_("(auto)"));
1894 }
1895#endif
1896 printf("\n");
1897
1898#if HAVE_AFINET
1899 if (ptr->has_ip) {
1900 printf(_(" %s addr:%s "), ap->name,
1901 ap->sprint(&ptr->addr, 1));
1902 if (ptr->flags & IFF_POINTOPOINT) {
1903 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1904 }
1905 if (ptr->flags & IFF_BROADCAST) {
1906 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1907 }
1908 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1909 }
1910#endif
1911
1912#if HAVE_AFINET6
1913 /* FIXME: should be integrated into interface.c. */
1914
1915 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1916 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1917 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1918 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1919 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1920 if (!strcmp(devname, ptr->name)) {
1921 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1922 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1923 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1924 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1925 printf(_(" inet6 addr: %s/%d"),
1926 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1927 printf(_(" Scope:"));
1928 switch (scope) {
1929 case 0:
1930 printf(_("Global"));
1931 break;
1932 case IPV6_ADDR_LINKLOCAL:
1933 printf(_("Link"));
1934 break;
1935 case IPV6_ADDR_SITELOCAL:
1936 printf(_("Site"));
1937 break;
1938 case IPV6_ADDR_COMPATv4:
1939 printf(_("Compat"));
1940 break;
1941 case IPV6_ADDR_LOOPBACK:
1942 printf(_("Host"));
1943 break;
1944 default:
1945 printf(_("Unknown"));
1946 }
1947 printf("\n");
1948 }
1949 }
1950 fclose(f);
1951 }
1952#endif
1953
1954#if HAVE_AFIPX
1955 if (ipxtype == NULL)
1956 ipxtype = get_afntype(AF_IPX);
1957
1958 if (ipxtype != NULL) {
1959 if (ptr->has_ipx_bb)
1960 printf(_(" IPX/Ethernet II addr:%s\n"),
1961 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1962 if (ptr->has_ipx_sn)
1963 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1964 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1965 if (ptr->has_ipx_e2)
1966 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1967 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1968 if (ptr->has_ipx_e3)
1969 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1970 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1971 }
1972#endif
1973
1974#if HAVE_AFATALK
1975 if (ddptype == NULL)
1976 ddptype = get_afntype(AF_APPLETALK);
1977 if (ddptype != NULL) {
1978 if (ptr->has_ddp)
1979 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
1980 }
1981#endif
1982
1983#if HAVE_AFECONET
1984 if (ectype == NULL)
1985 ectype = get_afntype(AF_ECONET);
1986 if (ectype != NULL) {
1987 if (ptr->has_econet)
1988 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
1989 }
1990#endif
1991
1992 printf(" ");
1993 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1994 if (ptr->flags == 0)
1995 printf(_("[NO FLAGS] "));
1996 if (ptr->flags & IFF_UP)
1997 printf(_("UP "));
1998 if (ptr->flags & IFF_BROADCAST)
1999 printf(_("BROADCAST "));
2000 if (ptr->flags & IFF_DEBUG)
2001 printf(_("DEBUG "));
2002 if (ptr->flags & IFF_LOOPBACK)
2003 printf(_("LOOPBACK "));
2004 if (ptr->flags & IFF_POINTOPOINT)
2005 printf(_("POINTOPOINT "));
2006 if (ptr->flags & IFF_NOTRAILERS)
2007 printf(_("NOTRAILERS "));
2008 if (ptr->flags & IFF_RUNNING)
2009 printf(_("RUNNING "));
2010 if (ptr->flags & IFF_NOARP)
2011 printf(_("NOARP "));
2012 if (ptr->flags & IFF_PROMISC)
2013 printf(_("PROMISC "));
2014 if (ptr->flags & IFF_ALLMULTI)
2015 printf(_("ALLMULTI "));
2016 if (ptr->flags & IFF_SLAVE)
2017 printf(_("SLAVE "));
2018 if (ptr->flags & IFF_MASTER)
2019 printf(_("MASTER "));
2020 if (ptr->flags & IFF_MULTICAST)
2021 printf(_("MULTICAST "));
2022#ifdef HAVE_DYNAMIC
2023 if (ptr->flags & IFF_DYNAMIC)
2024 printf(_("DYNAMIC "));
2025#endif
2026 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2027 printf(_(" MTU:%d Metric:%d"),
2028 ptr->mtu, ptr->metric ? ptr->metric : 1);
2029#ifdef SIOCSKEEPALIVE
2030 if (ptr->outfill || ptr->keepalive)
2031 printf(_(" Outfill:%d Keepalive:%d"),
2032 ptr->outfill, ptr->keepalive);
2033#endif
2034 printf("\n");
2035
2036 /* If needed, display the interface statistics. */
2037
2038 if (ptr->statistics_valid) {
2039 /* XXX: statistics are currently only printed for the primary address,
2040 * not for the aliases, although strictly speaking they're shared
2041 * by all addresses.
2042 */
2043 printf(" ");
2044
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002045 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002046 ptr->stats.rx_packets, ptr->stats.rx_errors,
2047 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2048 ptr->stats.rx_frame_errors);
2049 if (can_compress)
2050 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002051 printf(" ");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002052 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
Eric Andersenf15d4da2001-03-06 00:48:59 +00002053 ptr->stats.tx_packets, ptr->stats.tx_errors,
2054 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2055 ptr->stats.tx_carrier_errors);
2056 printf(_(" collisions:%lu "), ptr->stats.collisions);
2057 if (can_compress)
2058 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2059 if (ptr->tx_queue_len != -1)
2060 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002061 printf("\n R");
2062 print_bytes_scaled(ptr->stats.rx_bytes, " T");
Manuel Novoa III 0e0883e2001-03-15 15:37:48 +00002063 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002064
Eric Andersenf15d4da2001-03-06 00:48:59 +00002065 }
2066
2067 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2068 ptr->map.base_addr)) {
2069 printf(" ");
2070 if (ptr->map.irq)
2071 printf(_("Interrupt:%d "), ptr->map.irq);
2072 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2073 I/O maps */
2074 printf(_("Base address:0x%x "), ptr->map.base_addr);
2075 if (ptr->map.mem_start) {
2076 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2077 }
2078 if (ptr->map.dma)
2079 printf(_("DMA chan:%x "), ptr->map.dma);
2080 printf("\n");
2081 }
2082 printf("\n");
2083}
2084
2085
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002086static int do_if_print(struct interface *ife, void *cookie)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002087{
2088 int *opt_a = (int *) cookie;
2089 int res;
2090
2091 res = do_if_fetch(ife);
2092 if (res >= 0) {
2093 if ((ife->flags & IFF_UP) || *opt_a)
2094 ife_print(ife);
2095 }
2096 return res;
2097}
2098
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002099static struct interface *lookup_interface(char *name)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002100{
2101 struct interface *ife = NULL;
2102
2103 if (if_readlist_proc(name) < 0)
2104 return NULL;
2105 ife = add_interface(name);
2106 return ife;
2107}
2108
2109/* for ipv4 add/del modes */
2110static int if_print(char *ifname)
2111{
2112 int res;
2113
2114 if (!ifname) {
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002115 res = for_all_interfaces(do_if_print, &interface_opt_a);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002116 } else {
2117 struct interface *ife;
2118
2119 ife = lookup_interface(ifname);
2120 res = do_if_fetch(ife);
2121 if (res >= 0)
2122 ife_print(ife);
2123 }
2124 return res;
2125}
2126
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002127int display_interfaces(char *ifname)
Eric Andersenf15d4da2001-03-06 00:48:59 +00002128{
2129 int status;
2130
2131 /* Create a channel to the NET kernel. */
2132 if ((skfd = sockets_open(0)) < 0) {
Manuel Novoa III 78f57462001-03-10 02:00:54 +00002133 perror_msg_and_die("socket");
Eric Andersenf15d4da2001-03-06 00:48:59 +00002134 }
2135
2136 /* Do we have to show the current setup? */
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +00002137 status = if_print(ifname);
Eric Andersenf15d4da2001-03-06 00:48:59 +00002138 close(skfd);
2139 exit(status < 0);
2140}