5800e0f6a099b39fa9f8c05190299021407ca85d
[oweals/busybox.git] / libbb / interface.c
1 /*
2  * 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  *
14  * 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  *
18  * Version:     $Id: interface.c,v 1.8 2002/07/03 11:46:36 andersen Exp $
19  *
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)
37  */
38
39 /* #define KEEP_UNUSED */
40
41 /* 
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
53 #undef HAVE_AFASH
54
55 #if CONFIG_FEATURE_IPV6
56 #define HAVE_AFINET6 1
57 #endif
58
59 /* 
60  * 
61  * Device Hardware types.
62  * 
63  */
64 #define HAVE_HWETHER    1
65 #define HAVE_HWPPP      1
66 #undef HAVE_HWSLIP
67
68
69 #include "inet_common.h"
70 #include <stdio.h>
71 #include <errno.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <fcntl.h>
76 #include <ctype.h>
77 #include <sys/ioctl.h>
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include "libbb.h"
81
82 #define _(x) x
83 #define _PATH_PROCNET_DEV               "/proc/net/dev"
84 #define _PATH_PROCNET_IFINET6           "/proc/net/if_inet6"
85 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
86 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
87
88 static int procnetdev_vsn = 1;
89
90 /* Ugh.  But libc5 doesn't provide POSIX types.  */
91 #include <asm/types.h>
92
93
94 #ifdef HAVE_HWSLIP
95 #include <linux/if_slip.h>
96 #endif
97
98 #if HAVE_AFINET6
99
100 #ifndef _LINUX_IN6_H
101 /*
102  *    This is in linux/include/net/ipv6.h.
103  */
104
105 struct in6_ifreq {
106     struct in6_addr ifr6_addr;
107     __u32 ifr6_prefixlen;
108     unsigned int ifr6_ifindex;
109 };
110
111 #endif
112
113 #endif                          /* HAVE_AFINET6 */
114
115 #if HAVE_AFIPX
116 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
117 #include <netipx/ipx.h>
118 #else
119 #include "ipx.h"
120 #endif
121 #endif
122
123 /* Defines for glibc2.0 users. */
124 #ifndef SIOCSIFTXQLEN
125 #define SIOCSIFTXQLEN      0x8943
126 #define SIOCGIFTXQLEN      0x8942
127 #endif
128
129 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
130 #ifndef ifr_qlen
131 #define ifr_qlen        ifr_ifru.ifru_mtu
132 #endif
133
134 #ifndef HAVE_TXQUEUELEN
135 #define HAVE_TXQUEUELEN 1
136 #endif
137
138 #ifndef IFF_DYNAMIC
139 #define IFF_DYNAMIC     0x8000  /* dialup device with changing addresses */
140 #endif
141
142 /* This structure defines protocol families and their handlers. */
143 struct aftype {
144     const char *name;
145     const char *title;
146     int af;
147     int alen;
148     char *(*print) (unsigned char *);
149     char *(*sprint) (struct sockaddr *, int numeric);
150     int (*input) (int type, char *bufp, struct sockaddr *);
151     void (*herror) (char *text);
152     int (*rprint) (int options);
153     int (*rinput) (int typ, int ext, char **argv);
154
155     /* may modify src */
156     int (*getmask) (char *src, struct sockaddr * mask, char *name);
157
158     int fd;
159     char *flag_file;
160 };
161
162 static struct aftype *aftypes[];
163
164 #ifdef KEEP_UNUSED
165
166 static int flag_unx;
167 #ifdef HAVE_AFIPX
168 static int flag_ipx;
169 #endif
170 #ifdef HAVE_AFX25
171 static int flag_ax25;
172 #endif
173 #ifdef HAVE_AFATALK
174 static int flag_ddp;
175 #endif
176 #ifdef HAVE_AFNETROM
177 static int flag_netrom;
178 #endif
179 static int flag_inet;
180 #ifdef HAVE_AFINET6
181 static int flag_inet6;
182 #endif
183 #ifdef HAVE_AFECONET
184 static int flag_econet;
185 #endif
186 #ifdef HAVE_AFX25
187 static int flag_x25 = 0;
188 #endif
189 #ifdef HAVE_AFASH
190 static int flag_ash;
191 #endif
192
193
194 static struct aftrans_t {
195     char *alias;
196     char *name;
197     int *flag;
198 } aftrans[] = {
199
200 #ifdef HAVE_AFX25
201     {
202         "ax25", "ax25", &flag_ax25
203     },
204 #endif
205     {
206         "ip", "inet", &flag_inet
207     },
208 #ifdef HAVE_AFINET6
209     {
210         "ip6", "inet6", &flag_inet6
211     },
212 #endif
213 #ifdef HAVE_AFIPX
214     {
215         "ipx", "ipx", &flag_ipx
216     },
217 #endif
218 #ifdef HAVE_AFATALK
219     {
220         "appletalk", "ddp", &flag_ddp
221     },
222 #endif
223 #ifdef HAVE_AFNETROM
224     {
225         "netrom", "netrom", &flag_netrom
226     },
227 #endif
228     {
229         "inet", "inet", &flag_inet
230     },
231 #ifdef HAVE_AFINET6
232     {
233         "inet6", "inet6", &flag_inet6
234     },
235 #endif
236 #ifdef HAVE_AFATALK
237     {
238         "ddp", "ddp", &flag_ddp
239     },
240 #endif
241     {
242         "unix", "unix", &flag_unx
243     },
244     {
245         "tcpip", "inet", &flag_inet
246     },
247 #ifdef HAVE_AFECONET
248     {
249         "econet", "ec", &flag_econet
250     },
251 #endif
252 #ifdef HAVE_AFX25
253     {
254         "x25", "x25", &flag_x25
255     },
256 #endif
257 #ifdef HAVE_AFASH
258     {
259         "ash", "ash", &flag_ash
260     },
261 #endif
262     {
263         0, 0, 0
264     }
265 };
266
267 static char afname[256] = "";
268 #endif /* KEEP_UNUSED */
269
270 #if HAVE_AFUNIX
271
272 /* Display a UNIX domain address. */
273 static char *UNIX_print(unsigned char *ptr)
274 {
275     return (ptr);
276 }
277
278
279 /* Display a UNIX domain address. */
280 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
281 {
282     static char buf[64];
283
284     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
285         return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
286     return (UNIX_print(sap->sa_data));
287 }
288
289
290 static struct aftype unix_aftype =
291 {
292     "unix", "UNIX Domain", AF_UNIX, 0,
293     UNIX_print, UNIX_sprint, NULL, NULL,
294     NULL, NULL, NULL,
295     -1,
296     "/proc/net/unix"
297 };
298 #endif                          /* HAVE_AFUNIX */
299
300 #if HAVE_AFINET
301
302 #ifdef KEEP_UNUSED
303 static void INET_reserror(char *text)
304 {
305     herror(text);
306 }
307
308 /* Display an Internet socket address. */
309 static char *INET_print(unsigned char *ptr)
310 {
311     return (inet_ntoa((*(struct in_addr *) ptr)));
312 }
313 #endif /* KEEP_UNUSED */
314
315 /* Display an Internet socket address. */
316 static char *INET_sprint(struct sockaddr *sap, int numeric)
317 {
318     static char buff[128];
319
320     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
321         return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
322
323     if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, 
324                       numeric, 0xffffff00) != 0)
325         return (NULL);
326
327     return (buff);
328 }
329
330 #ifdef KEEP_UNUSED
331 static char *INET_sprintmask(struct sockaddr *sap, int numeric, 
332                       unsigned int netmask)
333 {
334     static char buff[128];
335
336     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
337         return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
338     if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, 
339                       numeric, netmask) != 0)
340         return (NULL);
341     return (buff);
342 }
343
344 static int INET_getsock(char *bufp, struct sockaddr *sap)
345 {
346     char *sp = bufp, *bp;
347     unsigned int i;
348     unsigned val;
349     struct sockaddr_in *sin;
350
351     sin = (struct sockaddr_in *) sap;
352     sin->sin_family = AF_INET;
353     sin->sin_port = 0;
354
355     val = 0;
356     bp = (char *) &val;
357     for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
358         *sp = toupper(*sp);
359
360         if ((*sp >= 'A') && (*sp <= 'F'))
361             bp[i] |= (int) (*sp - 'A') + 10;
362         else if ((*sp >= '0') && (*sp <= '9'))
363             bp[i] |= (int) (*sp - '0');
364         else
365             return (-1);
366
367         bp[i] <<= 4;
368         sp++;
369         *sp = toupper(*sp);
370
371         if ((*sp >= 'A') && (*sp <= 'F'))
372             bp[i] |= (int) (*sp - 'A') + 10;
373         else if ((*sp >= '0') && (*sp <= '9'))
374             bp[i] |= (int) (*sp - '0');
375         else
376             return (-1);
377
378         sp++;
379     }
380     sin->sin_addr.s_addr = htonl(val);
381
382     return (sp - bufp);
383 }
384
385 static int INET_input(int type, char *bufp, struct sockaddr *sap)
386 {
387     switch (type) {
388     case 1:
389         return (INET_getsock(bufp, sap));
390     case 256:
391         return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
392     default:
393         return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
394     }
395 }
396
397 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
398 {
399     struct sockaddr_in *mask = (struct sockaddr_in *) m;
400     char *slash, *end;
401     int prefix;
402
403     if ((slash = strchr(adr, '/')) == NULL)
404         return 0;
405
406     *slash++ = '\0';
407     prefix = strtoul(slash, &end, 0);
408     if (*end != '\0')
409         return -1;
410
411     if (name) {
412         sprintf(name, "/%d", prefix);
413     }
414     mask->sin_family = AF_INET;
415     mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
416     return 1;
417 }
418 #endif /* KEEP_UNUSED */
419
420 static struct aftype inet_aftype =
421 {
422     "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
423     NULL /* UNUSED INET_print */, INET_sprint,
424         NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
425     NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
426     NULL /* UNUSED INET_getnetmask */,
427     -1,
428     NULL
429 };
430
431 #endif                          /* HAVE_AFINET */
432
433 #if HAVE_AFINET6
434
435 #ifdef KEEP_UNUSED
436 static void INET6_reserror(char *text)
437 {
438     herror(text);
439 }
440
441 /* Display an Internet socket address. */
442 static char *INET6_print(unsigned char *ptr)
443 {
444     static char name[80];
445
446     inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
447     return name;
448 }
449 #endif /* KEEP_UNUSED */
450
451 /* Display an Internet socket address. */
452 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
453 static char *INET6_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     if (INET6_rresolve(buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
460         return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
461     return (buff);
462 }
463
464 #ifdef KEEP_UNUSED
465 static int INET6_getsock(char *bufp, struct sockaddr *sap)
466 {
467     struct sockaddr_in6 *sin6;
468
469     sin6 = (struct sockaddr_in6 *) sap;
470     sin6->sin6_family = AF_INET6;
471     sin6->sin6_port = 0;
472
473     if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
474         return (-1);
475
476     return 16;                  /* ?;) */
477 }
478
479 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
480 {
481     switch (type) {
482     case 1:
483         return (INET6_getsock(bufp, sap));
484     default:
485         return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
486     }
487 }
488 #endif /* KEEP_UNUSED */
489
490 static struct aftype inet6_aftype =
491 {
492     "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
493     NULL /* UNUSED INET6_print */, INET6_sprint,
494         NULL /* UNUSED INET6_input */, NULL /* UNUSED INET6_reserror */,
495     NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
496     NULL /* UNUSED INET6_getnetmask */,
497     -1,
498     NULL
499 };
500
501 #endif                          /* HAVE_AFINET6 */
502
503 /* Display an UNSPEC address. */
504 static char *UNSPEC_print(unsigned char *ptr)
505 {
506     static char buff[sizeof(struct sockaddr)*3+1];
507     char *pos;
508     unsigned int i;
509
510     pos = buff;
511     for (i = 0; i < sizeof(struct sockaddr); i++) {
512         /* careful -- not every libc's sprintf returns # bytes written */
513         sprintf(pos, "%02X-", (*ptr++ & 0377));
514         pos += 3;
515     }
516     /* Erase trailing "-".  Works as long as sizeof(struct sockaddr) != 0 */
517     *--pos = '\0';
518     return (buff);
519 }
520
521 /* Display an UNSPEC socket address. */
522 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
523 {
524     static char buf[64];
525
526     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
527         return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
528     return (UNSPEC_print(sap->sa_data));
529 }
530
531 static struct aftype unspec_aftype =
532 {
533     "unspec", "UNSPEC", AF_UNSPEC, 0,
534     UNSPEC_print, UNSPEC_sprint, NULL, NULL,
535     NULL,
536 };
537
538 static struct aftype *aftypes[] =
539 {
540 #if HAVE_AFUNIX
541     &unix_aftype,
542 #endif
543 #if HAVE_AFINET
544     &inet_aftype,
545 #endif
546 #if HAVE_AFINET6
547     &inet6_aftype,
548 #endif
549 #if HAVE_AFAX25
550     &ax25_aftype,
551 #endif
552 #if HAVE_AFNETROM
553     &netrom_aftype,
554 #endif
555 #if HAVE_AFROSE
556     &rose_aftype,
557 #endif
558 #if HAVE_AFIPX
559     &ipx_aftype,
560 #endif
561 #if HAVE_AFATALK
562     &ddp_aftype,
563 #endif
564 #if HAVE_AFECONET
565     &ec_aftype,
566 #endif
567 #if HAVE_AFASH
568     &ash_aftype,
569 #endif
570 #if HAVE_AFX25
571     &x25_aftype,
572 #endif
573     &unspec_aftype,
574     NULL
575 };
576
577 #ifdef KEEP_UNUSED
578 static short sVafinit = 0;
579
580 static void afinit()
581 {
582     unspec_aftype.title = _("UNSPEC");
583 #if HAVE_AFUNIX
584     unix_aftype.title = _("UNIX Domain");
585 #endif
586 #if HAVE_AFINET
587     inet_aftype.title = _("DARPA Internet");
588 #endif
589 #if HAVE_AFINET6
590     inet6_aftype.title = _("IPv6");
591 #endif
592 #if HAVE_AFAX25
593     ax25_aftype.title = _("AMPR AX.25");
594 #endif
595 #if HAVE_AFNETROM
596     netrom_aftype.title = _("AMPR NET/ROM");
597 #endif
598 #if HAVE_AFIPX
599     ipx_aftype.title = _("Novell IPX");
600 #endif
601 #if HAVE_AFATALK
602     ddp_aftype.title = _("Appletalk DDP");
603 #endif
604 #if HAVE_AFECONET
605     ec_aftype.title = _("Econet");
606 #endif
607 #if HAVE_AFX25
608     x25_aftype.title = _("CCITT X.25");
609 #endif
610 #if HAVE_AFROSE
611     rose_aftype.title = _("AMPR ROSE");
612 #endif
613 #if HAVE_AFASH
614     ash_aftype.title = _("Ash");
615 #endif  
616     sVafinit = 1;
617 }
618
619 static int aftrans_opt(const char *arg)
620 {
621     struct aftrans_t *paft;
622     char *tmp1, *tmp2;
623     char buf[256];
624
625     safe_strncpy(buf, arg, sizeof(buf));
626
627     tmp1 = buf;
628
629     while (tmp1) {
630
631         tmp2 = strchr(tmp1, ',');
632
633         if (tmp2)
634             *(tmp2++) = '\0';
635
636         paft = aftrans;
637         for (paft = aftrans; paft->alias; paft++) {
638             if (strcmp(tmp1, paft->alias))
639                 continue;
640             if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
641                 fprintf(stderr, _("Too much address family arguments.\n"));
642                 return (0);
643             }
644             if (paft->flag)
645                 (*paft->flag)++;
646             if (afname[0])
647                 strcat(afname, ",");
648             strcat(afname, paft->name);
649             break;
650         }
651         if (!paft->alias) {
652             fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
653             return (1);
654         }
655         tmp1 = tmp2;
656     }
657
658     return (0);
659 }
660
661 /* set the default AF list from the program name or a constant value    */
662 static void aftrans_def(char *tool, char *argv0, char *dflt)
663 {
664     char *tmp;
665     char *buf;
666
667     strcpy(afname, dflt);
668
669     if (!(tmp = strrchr(argv0, '/')))
670         tmp = argv0;            /* no slash?! */
671     else
672         tmp++;
673
674     if (!(buf = strdup(tmp)))
675         return;
676
677     if (strlen(tool) >= strlen(tmp)) {
678         free(buf);
679         return;
680     }
681     tmp = buf + (strlen(tmp) - strlen(tool));
682
683     if (strcmp(tmp, tool) != 0) {
684         free(buf);
685         return;
686     }
687     *tmp = '\0';
688     if ((tmp = strchr(buf, '_')))
689         *tmp = '\0';
690
691     afname[0] = '\0';
692     if (aftrans_opt(buf))
693         strcpy(afname, buf);
694
695     free(buf);
696 }
697
698 /* Check our protocol family table for this family. */
699 static struct aftype *get_aftype(const char *name)
700 {
701     struct aftype **afp;
702
703 #ifdef KEEP_UNUSED
704     if (!sVafinit)
705         afinit();
706 #endif /* KEEP_UNUSED */
707
708     afp = aftypes;
709     while (*afp != NULL) {
710         if (!strcmp((*afp)->name, name))
711             return (*afp);
712         afp++;
713     }
714     if (strchr(name, ','))
715         fprintf(stderr, _("Please don't supply more than one address family.\n"));
716     return (NULL);
717 }
718 #endif /* KEEP_UNUSED */
719
720 /* Check our protocol family table for this family. */
721 static struct aftype *get_afntype(int af)
722 {
723     struct aftype **afp;
724
725 #ifdef KEEP_UNUSED
726     if (!sVafinit)
727         afinit();
728 #endif /* KEEP_UNUSED */
729
730     afp = aftypes;
731     while (*afp != NULL) {
732         if ((*afp)->af == af)
733             return (*afp);
734         afp++;
735     }
736     return (NULL);
737 }
738
739 /* Check our protocol family table for this family and return its socket */
740 static int get_socket_for_af(int af)
741 {
742     struct aftype **afp;
743
744 #ifdef KEEP_UNUSED
745     if (!sVafinit)
746         afinit();
747 #endif /* KEEP_UNUSED */
748
749     afp = aftypes;
750     while (*afp != NULL) {
751         if ((*afp)->af == af)
752             return (*afp)->fd;
753         afp++;
754     }
755     return -1;
756 }
757
758 #ifdef KEEP_UNUSED
759 /* type: 0=all, 1=getroute */
760 static void print_aflist(int type) {
761     int count = 0;
762     char * txt;
763     struct aftype **afp;
764
765 #ifdef KEEP_UNUSED
766     if (!sVafinit)
767         afinit();
768 #endif /* KEEP_UNUSED */
769
770     afp = aftypes;
771     while (*afp != NULL) {
772         if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
773                 afp++; continue;
774         }
775         if ((count % 3) == 0) fprintf(stderr,count?"\n    ":"    "); 
776         txt = (*afp)->name; if (!txt) txt = "..";
777         fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
778         count++;
779         afp++;
780     }
781     fprintf(stderr,"\n");
782 }
783 #endif /* KEEP_UNUSED */
784
785 struct user_net_device_stats {
786     unsigned long long rx_packets;      /* total packets received       */
787     unsigned long long tx_packets;      /* total packets transmitted    */
788     unsigned long long rx_bytes;        /* total bytes received         */
789     unsigned long long tx_bytes;        /* total bytes transmitted      */
790     unsigned long rx_errors;    /* bad packets received         */
791     unsigned long tx_errors;    /* packet transmit problems     */
792     unsigned long rx_dropped;   /* no space in linux buffers    */
793     unsigned long tx_dropped;   /* no space available in linux  */
794     unsigned long rx_multicast; /* multicast packets received   */
795     unsigned long rx_compressed;
796     unsigned long tx_compressed;
797     unsigned long collisions;
798
799     /* detailed rx_errors: */
800     unsigned long rx_length_errors;
801     unsigned long rx_over_errors;       /* receiver ring buff overflow  */
802     unsigned long rx_crc_errors;        /* recved pkt with crc error    */
803     unsigned long rx_frame_errors;      /* recv'd frame alignment error */
804     unsigned long rx_fifo_errors;       /* recv'r fifo overrun          */
805     unsigned long rx_missed_errors;     /* receiver missed packet     */
806     /* detailed tx_errors */
807     unsigned long tx_aborted_errors;
808     unsigned long tx_carrier_errors;
809     unsigned long tx_fifo_errors;
810     unsigned long tx_heartbeat_errors;
811     unsigned long tx_window_errors;
812 };
813
814 struct interface {
815     struct interface *next, *prev; 
816     char name[IFNAMSIZ];        /* interface name        */
817     short type;                 /* if type               */
818     short flags;                /* various flags         */
819     int metric;                 /* routing metric        */
820     int mtu;                    /* MTU value             */
821     int tx_queue_len;           /* transmit queue length */
822     struct ifmap map;           /* hardware setup        */
823     struct sockaddr addr;       /* IP address            */
824     struct sockaddr dstaddr;    /* P-P IP address        */
825     struct sockaddr broadaddr;  /* IP broadcast address  */
826     struct sockaddr netmask;    /* IP network mask       */
827     struct sockaddr ipxaddr_bb; /* IPX network address   */
828     struct sockaddr ipxaddr_sn; /* IPX network address   */
829     struct sockaddr ipxaddr_e3; /* IPX network address   */
830     struct sockaddr ipxaddr_e2; /* IPX network address   */
831     struct sockaddr ddpaddr;    /* Appletalk DDP address */
832     struct sockaddr ecaddr;     /* Econet address        */
833     int has_ip;
834     int has_ipx_bb;
835     int has_ipx_sn;
836     int has_ipx_e3;
837     int has_ipx_e2;
838     int has_ax25;
839     int has_ddp;
840     int has_econet;
841     char hwaddr[32];            /* HW address            */
842     int statistics_valid;
843     struct user_net_device_stats stats;         /* statistics            */
844     int keepalive;              /* keepalive value for SLIP */
845     int outfill;                /* outfill value for SLIP */
846 };
847
848
849 int interface_opt_a = 0;                /* show all interfaces          */
850
851 #ifdef KEEP_UNUSED
852 static int opt_i = 0;                   /* show the statistics          */
853 static int opt_v = 0;                   /* debugging output flag        */
854
855 static int addr_family = 0;             /* currently selected AF        */
856 #endif /* KEEP_UNUSED */
857
858 static struct interface *int_list, *int_last;
859 static int skfd = -1;                   /* generic raw socket desc.     */
860
861
862 static int sockets_open(int family)
863 {
864     struct aftype **aft;
865     int sfd = -1;
866     static int force = -1;
867
868     if (force < 0) {
869         force = 0;
870         if (get_kernel_revision() < KRELEASE(2, 1, 0))
871             force = 1;
872         if (access("/proc/net", R_OK))
873             force = 1;
874     }
875     for (aft = aftypes; *aft; aft++) {
876         struct aftype *af = *aft;
877         int type = SOCK_DGRAM;
878         if (af->af == AF_UNSPEC)
879             continue;
880         if (family && family != af->af)
881             continue;
882         if (af->fd != -1) {
883             sfd = af->fd;
884             continue;
885         }
886         /* Check some /proc file first to not stress kmod */
887         if (!family && !force && af->flag_file) {
888             if (access(af->flag_file, R_OK))
889                 continue;
890         }
891 #if HAVE_AFNETROM
892         if (af->af == AF_NETROM)
893             type = SOCK_SEQPACKET;
894 #endif
895 #if HAVE_AFX25
896        if (af->af == AF_X25)
897            type = SOCK_SEQPACKET;
898 #endif
899         af->fd = socket(af->af, type, 0);
900         if (af->fd >= 0)
901             sfd = af->fd;
902     }
903     if (sfd < 0)
904         fprintf(stderr, _("No usable address families found.\n"));
905     return sfd;
906 }
907
908 /* like strcmp(), but knows about numbers */
909 static int nstrcmp(const char *astr, const char *b)
910 {
911     const char *a = astr;
912
913     while (*a == *b) {
914         if (*a == '\0')
915             return 0;
916         a++;
917         b++;
918     }
919     if (isdigit(*a)) {
920         if (!isdigit(*b))
921             return -1;
922         while (a > astr) {
923             a--;
924             if (!isdigit(*a)) {
925                 a++;
926                 break;
927             }
928             if (!isdigit(*b))
929                 return -1;
930             b--;
931         }
932         return atoi(a) > atoi(b) ? 1 : -1;
933     }
934     return *a - *b;
935 }
936
937 static struct interface *add_interface(char *name)
938 {
939     struct interface *ife, **nextp, *new;
940
941     for (ife = int_last; ife; ife = ife->prev) {
942             int n = nstrcmp(ife->name, name); 
943             if (n == 0) 
944                     return ife; 
945             if (n < 0) 
946                     break; 
947     }
948     new(new); 
949     safe_strncpy(new->name, name, IFNAMSIZ); 
950     nextp = ife ? &ife->next : &int_list;
951     new->prev = ife;
952     new->next = *nextp; 
953     if (new->next) 
954             new->next->prev = new; 
955     else
956             int_last = new; 
957     *nextp = new; 
958     return new; 
959 }
960
961
962 static int if_readconf(void)
963 {
964     int numreqs = 30;
965     struct ifconf ifc;
966     struct ifreq *ifr;
967     int n, err = -1;
968     int skfd2;
969
970     /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
971        (as of 2.1.128) */ 
972     skfd2 = get_socket_for_af(AF_INET);
973     if (skfd2 < 0) {
974         fprintf(stderr, _("warning: no inet socket available: %s\n"),
975                 strerror(errno));
976         /* Try to soldier on with whatever socket we can get hold of.  */
977         skfd2 = sockets_open(0);
978         if (skfd2 < 0)
979             return -1;
980     }
981
982     ifc.ifc_buf = NULL;
983     for (;;) {
984         ifc.ifc_len = sizeof(struct ifreq) * numreqs;
985         ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
986
987         if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
988             perror("SIOCGIFCONF");
989             goto out;
990         }
991         if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
992             /* assume it overflowed and try again */
993             numreqs += 10;
994             continue;
995         }
996         break;
997     }
998
999     ifr = ifc.ifc_req;
1000     for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1001         add_interface(ifr->ifr_name);
1002         ifr++;
1003     }
1004     err = 0;
1005
1006 out:
1007     free(ifc.ifc_buf);
1008     return err;
1009 }
1010
1011 static char *get_name(char *name, char *p)
1012 {
1013     while (isspace(*p))
1014         p++;
1015     while (*p) {
1016         if (isspace(*p))
1017             break;
1018         if (*p == ':') {        /* could be an alias */
1019             char *dot = p, *dotname = name;
1020             *name++ = *p++;
1021             while (isdigit(*p))
1022                 *name++ = *p++;
1023             if (*p != ':') {    /* it wasn't, backup */
1024                 p = dot;
1025                 name = dotname;
1026             }
1027             if (*p == '\0')
1028                 return NULL;
1029             p++;
1030             break;
1031         }
1032         *name++ = *p++;
1033     }
1034     *name++ = '\0';
1035     return p;
1036 }
1037
1038 static int get_dev_fields(char *bp, struct interface *ife)
1039 {
1040     switch (procnetdev_vsn) {
1041     case 3:
1042         sscanf(bp,
1043         "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1044                &ife->stats.rx_bytes,
1045                &ife->stats.rx_packets,
1046                &ife->stats.rx_errors,
1047                &ife->stats.rx_dropped,
1048                &ife->stats.rx_fifo_errors,
1049                &ife->stats.rx_frame_errors,
1050                &ife->stats.rx_compressed,
1051                &ife->stats.rx_multicast,
1052
1053                &ife->stats.tx_bytes,
1054                &ife->stats.tx_packets,
1055                &ife->stats.tx_errors,
1056                &ife->stats.tx_dropped,
1057                &ife->stats.tx_fifo_errors,
1058                &ife->stats.collisions,
1059                &ife->stats.tx_carrier_errors,
1060                &ife->stats.tx_compressed);
1061         break;
1062     case 2:
1063         sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1064                &ife->stats.rx_bytes,
1065                &ife->stats.rx_packets,
1066                &ife->stats.rx_errors,
1067                &ife->stats.rx_dropped,
1068                &ife->stats.rx_fifo_errors,
1069                &ife->stats.rx_frame_errors,
1070
1071                &ife->stats.tx_bytes,
1072                &ife->stats.tx_packets,
1073                &ife->stats.tx_errors,
1074                &ife->stats.tx_dropped,
1075                &ife->stats.tx_fifo_errors,
1076                &ife->stats.collisions,
1077                &ife->stats.tx_carrier_errors);
1078         ife->stats.rx_multicast = 0;
1079         break;
1080     case 1:
1081         sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1082                &ife->stats.rx_packets,
1083                &ife->stats.rx_errors,
1084                &ife->stats.rx_dropped,
1085                &ife->stats.rx_fifo_errors,
1086                &ife->stats.rx_frame_errors,
1087
1088                &ife->stats.tx_packets,
1089                &ife->stats.tx_errors,
1090                &ife->stats.tx_dropped,
1091                &ife->stats.tx_fifo_errors,
1092                &ife->stats.collisions,
1093                &ife->stats.tx_carrier_errors);
1094         ife->stats.rx_bytes = 0;
1095         ife->stats.tx_bytes = 0;
1096         ife->stats.rx_multicast = 0;
1097         break;
1098     }
1099     return 0;
1100 }
1101
1102 static inline int procnetdev_version(char *buf)
1103 {
1104     if (strstr(buf, "compressed"))
1105         return 3;
1106     if (strstr(buf, "bytes"))
1107         return 2;
1108     return 1;
1109 }
1110
1111 static int if_readlist_proc(char *target)
1112 {
1113     static int proc_read; 
1114     FILE *fh;
1115     char buf[512];
1116     struct interface *ife;
1117     int err;
1118
1119     if (proc_read) 
1120             return 0; 
1121     if (!target) 
1122             proc_read = 1;
1123
1124     fh = fopen(_PATH_PROCNET_DEV, "r");
1125     if (!fh) {
1126                 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1127                         _PATH_PROCNET_DEV, strerror(errno)); 
1128                 return if_readconf();
1129         }       
1130     fgets(buf, sizeof buf, fh); /* eat line */
1131     fgets(buf, sizeof buf, fh);
1132
1133 #if 0                           /* pretty, but can't cope with missing fields */
1134     fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1135                        "face", "",      /* parsed separately */
1136                        "bytes", "%lu",
1137                        "packets", "%lu",
1138                        "errs", "%lu",
1139                        "drop", "%lu",
1140                        "fifo", "%lu",
1141                        "frame", "%lu",
1142                        "compressed", "%lu",
1143                        "multicast", "%lu",
1144                        "bytes", "%lu",
1145                        "packets", "%lu",
1146                        "errs", "%lu",
1147                        "drop", "%lu",
1148                        "fifo", "%lu",
1149                        "colls", "%lu",
1150                        "carrier", "%lu",
1151                        "compressed", "%lu",
1152                        NULL);
1153     if (!fmt)
1154         return -1;
1155 #else
1156     procnetdev_vsn = procnetdev_version(buf);
1157 #endif
1158
1159     err = 0;
1160     while (fgets(buf, sizeof buf, fh)) {
1161         char *s, name[IFNAMSIZ];
1162         s = get_name(name, buf);    
1163         ife = add_interface(name);
1164         get_dev_fields(s, ife);
1165         ife->statistics_valid = 1;
1166         if (target && !strcmp(target,name))
1167                 break;
1168     }
1169     if (ferror(fh)) {
1170         perror(_PATH_PROCNET_DEV);
1171         err = -1;
1172         proc_read = 0; 
1173     }
1174
1175 #if 0
1176     free(fmt);
1177 #endif
1178     fclose(fh);
1179     return err;
1180 }
1181
1182 static int if_readlist(void) 
1183
1184     int err = if_readlist_proc(NULL); 
1185     if (!err)
1186             err = if_readconf();
1187     return err;
1188
1189
1190 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1191 {
1192     struct interface *ife;
1193
1194     if (!int_list && (if_readlist() < 0))
1195         return -1;
1196     for (ife = int_list; ife; ife = ife->next) {
1197         int err = doit(ife, cookie);
1198         if (err)
1199             return err;
1200     }
1201     return 0;
1202 }
1203
1204 /* Support for fetching an IPX address */
1205
1206 #if HAVE_AFIPX
1207 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1208 {
1209     ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1210     return ioctl(sock, SIOCGIFADDR, ifr);
1211 }
1212 #endif
1213
1214
1215 /* Fetch the interface configuration from the kernel. */
1216 static int if_fetch(struct interface *ife)
1217 {
1218     struct ifreq ifr;
1219     int fd;
1220     char *ifname = ife->name; 
1221
1222     strcpy(ifr.ifr_name, ifname);
1223     if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1224         return (-1);
1225     ife->flags = ifr.ifr_flags;
1226
1227     strcpy(ifr.ifr_name, ifname);
1228     if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1229         memset(ife->hwaddr, 0, 32);
1230     else
1231         memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1232
1233     ife->type = ifr.ifr_hwaddr.sa_family;
1234
1235     strcpy(ifr.ifr_name, ifname);
1236     if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1237         ife->metric = 0;
1238     else
1239         ife->metric = ifr.ifr_metric;
1240
1241     strcpy(ifr.ifr_name, ifname);
1242     if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1243         ife->mtu = 0;
1244     else
1245         ife->mtu = ifr.ifr_mtu;
1246
1247 #ifdef HAVE_HWSLIP
1248     if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1249         ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1250         ife->type == ARPHRD_ADAPT) {
1251 #ifdef SIOCGOUTFILL
1252         strcpy(ifr.ifr_name, ifname);
1253         if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1254             ife->outfill = 0;
1255         else
1256             ife->outfill = (unsigned int) ifr.ifr_data;
1257 #endif
1258 #ifdef SIOCGKEEPALIVE
1259         strcpy(ifr.ifr_name, ifname);
1260         if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1261             ife->keepalive = 0;
1262         else
1263             ife->keepalive = (unsigned int) ifr.ifr_data;
1264 #endif
1265     }
1266 #endif
1267
1268     strcpy(ifr.ifr_name, ifname);
1269     if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1270         memset(&ife->map, 0, sizeof(struct ifmap));
1271     else
1272         memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1273
1274     strcpy(ifr.ifr_name, ifname);
1275     if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1276         memset(&ife->map, 0, sizeof(struct ifmap));
1277     else
1278         ife->map = ifr.ifr_map;
1279
1280 #ifdef HAVE_TXQUEUELEN
1281     strcpy(ifr.ifr_name, ifname);
1282     if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1283         ife->tx_queue_len = -1; /* unknown value */
1284     else
1285         ife->tx_queue_len = ifr.ifr_qlen;
1286 #else
1287     ife->tx_queue_len = -1;     /* unknown value */
1288 #endif
1289
1290 #if HAVE_AFINET
1291     /* IPv4 address? */
1292     fd = get_socket_for_af(AF_INET);
1293     if (fd >= 0) {
1294         strcpy(ifr.ifr_name, ifname);
1295         ifr.ifr_addr.sa_family = AF_INET;
1296         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1297             ife->has_ip = 1;
1298             ife->addr = ifr.ifr_addr;
1299             strcpy(ifr.ifr_name, ifname);
1300             if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1301                 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1302             else
1303                 ife->dstaddr = ifr.ifr_dstaddr;
1304
1305             strcpy(ifr.ifr_name, ifname);
1306             if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1307                 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1308             else
1309                 ife->broadaddr = ifr.ifr_broadaddr;
1310
1311             strcpy(ifr.ifr_name, ifname);
1312             if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1313                 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1314             else
1315                 ife->netmask = ifr.ifr_netmask;
1316         } else
1317             memset(&ife->addr, 0, sizeof(struct sockaddr));
1318     }
1319 #endif
1320
1321 #if HAVE_AFATALK
1322     /* DDP address maybe ? */
1323     fd = get_socket_for_af(AF_APPLETALK);
1324     if (fd >= 0) {
1325         strcpy(ifr.ifr_name, ifname);
1326         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1327             ife->ddpaddr = ifr.ifr_addr;
1328             ife->has_ddp = 1;
1329         }
1330     }
1331 #endif
1332
1333 #if HAVE_AFIPX
1334     /* Look for IPX addresses with all framing types */
1335     fd = get_socket_for_af(AF_IPX);
1336     if (fd >= 0) {
1337         strcpy(ifr.ifr_name, ifname);
1338         if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1339             ife->has_ipx_bb = 1;
1340             ife->ipxaddr_bb = ifr.ifr_addr;
1341         }
1342         strcpy(ifr.ifr_name, ifname);
1343         if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1344             ife->has_ipx_sn = 1;
1345             ife->ipxaddr_sn = ifr.ifr_addr;
1346         }
1347         strcpy(ifr.ifr_name, ifname);
1348         if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1349             ife->has_ipx_e3 = 1;
1350             ife->ipxaddr_e3 = ifr.ifr_addr;
1351         }
1352         strcpy(ifr.ifr_name, ifname);
1353         if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1354             ife->has_ipx_e2 = 1;
1355             ife->ipxaddr_e2 = ifr.ifr_addr;
1356         }
1357     }
1358 #endif
1359
1360 #if HAVE_AFECONET
1361     /* Econet address maybe? */
1362     fd = get_socket_for_af(AF_ECONET);
1363     if (fd >= 0) {
1364         strcpy(ifr.ifr_name, ifname);
1365         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1366             ife->ecaddr = ifr.ifr_addr;
1367             ife->has_econet = 1;
1368         }
1369     }
1370 #endif
1371
1372     return 0;
1373 }
1374
1375
1376 static int do_if_fetch(struct interface *ife)
1377
1378     if (if_fetch(ife) < 0) {
1379         char *errmsg; 
1380         if (errno == ENODEV) { 
1381             /* Give better error message for this case. */ 
1382             errmsg = _("Device not found"); 
1383         } else { 
1384             errmsg = strerror(errno); 
1385         }
1386         fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1387                 ife->name, errmsg);
1388         return -1;
1389     }
1390     return 0; 
1391 }
1392
1393 /* This structure defines hardware protocols and their handlers. */
1394 struct hwtype {
1395     const char *name;
1396     const char *title;
1397     int type;
1398     int alen;
1399     char *(*print) (unsigned char *);
1400     int (*input) (char *, struct sockaddr *);
1401     int (*activate) (int fd);
1402     int suppress_null_addr;
1403 };
1404
1405 static struct hwtype unspec_hwtype =
1406 {
1407     "unspec", "UNSPEC", -1, 0,
1408     UNSPEC_print, NULL, NULL
1409 };
1410
1411 static struct hwtype loop_hwtype =
1412 {
1413     "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1414     NULL, NULL, NULL
1415 };
1416
1417 #if HAVE_HWETHER
1418 #include <net/if_arp.h>
1419 #include <linux/if_ether.h>
1420
1421 static struct hwtype ether_hwtype;
1422
1423 /* Display an Ethernet address in readable format. */
1424 static char *pr_ether(unsigned char *ptr)
1425 {
1426     static char buff[64];
1427
1428     snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1429              (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1430              (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1431         );
1432     return (buff);
1433 }
1434
1435 #ifdef KEEP_UNUSED
1436 /* Input an Ethernet address and convert to binary. */
1437 static int in_ether(char *bufp, struct sockaddr *sap)
1438 {
1439     unsigned char *ptr;
1440     char c, *orig;
1441     int i;
1442     unsigned val;
1443
1444     sap->sa_family = ether_hwtype.type;
1445     ptr = sap->sa_data;
1446
1447     i = 0;
1448     orig = bufp;
1449     while ((*bufp != '\0') && (i < ETH_ALEN)) {
1450         val = 0;
1451         c = *bufp++;
1452         if (isdigit(c))
1453             val = c - '0';
1454         else if (c >= 'a' && c <= 'f')
1455             val = c - 'a' + 10;
1456         else if (c >= 'A' && c <= 'F')
1457             val = c - 'A' + 10;
1458         else {
1459 #ifdef DEBUG
1460             fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1461 #endif
1462             errno = EINVAL;
1463             return (-1);
1464         }
1465         val <<= 4;
1466         c = *bufp;
1467         if (isdigit(c))
1468             val |= c - '0';
1469         else if (c >= 'a' && c <= 'f')
1470             val |= c - 'a' + 10;
1471         else if (c >= 'A' && c <= 'F')
1472             val |= c - 'A' + 10;
1473         else if (c == ':' || c == 0)
1474             val >>= 4;
1475         else {
1476 #ifdef DEBUG
1477             fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1478 #endif
1479             errno = EINVAL;
1480             return (-1);
1481         }
1482         if (c != 0)
1483             bufp++;
1484         *ptr++ = (unsigned char) (val & 0377);
1485         i++;
1486
1487         /* We might get a semicolon here - not required. */
1488         if (*bufp == ':') {
1489             if (i == ETH_ALEN) {
1490 #ifdef DEBUG
1491                 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1492                         orig)
1493 #endif
1494                     ;           /* nothing */
1495             }
1496             bufp++;
1497         }
1498     }
1499
1500     /* That's it.  Any trailing junk? */
1501     if ((i == ETH_ALEN) && (*bufp != '\0')) {
1502 #ifdef DEBUG
1503         fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1504         errno = EINVAL;
1505         return (-1);
1506 #endif
1507     }
1508 #ifdef DEBUG
1509     fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1510 #endif
1511
1512     return (0);
1513 }
1514 #endif /* KEEP_UNUSED */
1515
1516
1517 static struct hwtype ether_hwtype =
1518 {
1519     "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1520     pr_ether, NULL /* UNUSED in_ether */, NULL
1521 };
1522
1523
1524 #endif                          /* HAVE_HWETHER */
1525
1526
1527 #if HAVE_HWPPP
1528
1529 #include <net/if_arp.h>
1530
1531 #ifdef KEEP_UNUSED
1532 /* Start the PPP encapsulation on the file descriptor. */
1533 static int do_ppp(int fd)
1534 {
1535     fprintf(stderr, _("You cannot start PPP with this program.\n"));
1536     return -1;
1537 }
1538 #endif /* KEEP_UNUSED */
1539
1540 static struct hwtype ppp_hwtype =
1541 {
1542     "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1543     NULL, NULL, NULL /* UNUSED do_ppp */, 0
1544 };
1545
1546
1547 #endif                          /* HAVE_PPP */
1548
1549 static struct hwtype *hwtypes[] =
1550 {
1551
1552     &loop_hwtype,
1553
1554 #if HAVE_HWSLIP
1555     &slip_hwtype,
1556     &cslip_hwtype,
1557     &slip6_hwtype,
1558     &cslip6_hwtype,
1559     &adaptive_hwtype,
1560 #endif
1561 #if HAVE_HWSTRIP
1562     &strip_hwtype,
1563 #endif
1564 #if HAVE_HWASH
1565     &ash_hwtype,
1566 #endif
1567 #if HAVE_HWETHER
1568     &ether_hwtype,
1569 #endif
1570 #if HAVE_HWTR
1571     &tr_hwtype,
1572 #ifdef ARPHRD_IEEE802_TR
1573     &tr_hwtype1, 
1574 #endif
1575 #endif
1576 #if HAVE_HWAX25
1577     &ax25_hwtype,
1578 #endif
1579 #if HAVE_HWNETROM
1580     &netrom_hwtype,
1581 #endif
1582 #if HAVE_HWROSE
1583     &rose_hwtype,
1584 #endif
1585 #if HAVE_HWTUNNEL
1586     &tunnel_hwtype,
1587 #endif
1588 #if HAVE_HWPPP
1589     &ppp_hwtype,
1590 #endif
1591 #if HAVE_HWHDLCLAPB
1592     &hdlc_hwtype,
1593     &lapb_hwtype,
1594 #endif
1595 #if HAVE_HWARC
1596     &arcnet_hwtype,
1597 #endif
1598 #if HAVE_HWFR
1599     &dlci_hwtype,
1600     &frad_hwtype,
1601 #endif
1602 #if HAVE_HWSIT
1603     &sit_hwtype,
1604 #endif
1605 #if HAVE_HWFDDI
1606     &fddi_hwtype,
1607 #endif
1608 #if HAVE_HWHIPPI
1609     &hippi_hwtype,
1610 #endif
1611 #if HAVE_HWIRDA
1612     &irda_hwtype,
1613 #endif
1614 #if HAVE_HWEC
1615     &ec_hwtype,
1616 #endif
1617 #if HAVE_HWX25
1618     &x25_hwtype,
1619 #endif
1620     &unspec_hwtype,
1621     NULL
1622 };
1623
1624 #ifdef KEEP_UNUSED
1625 static short sVhwinit = 0;
1626
1627 static void hwinit()
1628 {
1629     loop_hwtype.title = _("Local Loopback");
1630     unspec_hwtype.title = _("UNSPEC");
1631 #if HAVE_HWSLIP
1632     slip_hwtype.title = _("Serial Line IP");
1633     cslip_hwtype.title = _("VJ Serial Line IP");
1634     slip6_hwtype.title = _("6-bit Serial Line IP");
1635     cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1636     adaptive_hwtype.title = _("Adaptive Serial Line IP");
1637 #endif
1638 #if HAVE_HWETHER
1639     ether_hwtype.title = _("Ethernet");
1640 #endif
1641 #if HAVE_HWASH
1642     ash_hwtype.title = _("Ash");
1643 #endif
1644 #if HAVE_HWFDDI
1645     fddi_hwtype.title = _("Fiber Distributed Data Interface");
1646 #endif
1647 #if HAVE_HWHIPPI
1648     hippi_hwtype.title = _("HIPPI");
1649 #endif
1650 #if HAVE_HWAX25
1651     ax25_hwtype.title = _("AMPR AX.25");
1652 #endif
1653 #if HAVE_HWROSE
1654     rose_hwtype.title = _("AMPR ROSE");
1655 #endif
1656 #if HAVE_HWNETROM
1657     netrom_hwtype.title = _("AMPR NET/ROM");
1658 #endif
1659 #if HAVE_HWX25
1660     x25_hwtype.title = _("generic X.25");
1661 #endif
1662 #if HAVE_HWTUNNEL
1663     tunnel_hwtype.title = _("IPIP Tunnel");
1664 #endif
1665 #if HAVE_HWPPP
1666     ppp_hwtype.title = _("Point-to-Point Protocol");
1667 #endif
1668 #if HAVE_HWHDLCLAPB
1669     hdlc_hwtype.title = _("(Cisco)-HDLC");
1670     lapb_hwtype.title = _("LAPB");
1671 #endif
1672 #if HAVE_HWARC
1673     arcnet_hwtype.title = _("ARCnet");
1674 #endif
1675 #if HAVE_HWFR
1676     dlci_hwtype.title = _("Frame Relay DLCI");
1677     frad_hwtype.title = _("Frame Relay Access Device");
1678 #endif
1679 #if HAVE_HWSIT
1680     sit_hwtype.title = _("IPv6-in-IPv4");
1681 #endif
1682 #if HAVE_HWIRDA
1683     irda_hwtype.title = _("IrLAP");
1684 #endif
1685 #if HAVE_HWTR
1686     tr_hwtype.title = _("16/4 Mbps Token Ring");
1687 #ifdef ARPHRD_IEEE802_TR
1688     tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ; 
1689 #endif
1690 #endif
1691 #if HAVE_HWEC
1692     ec_hwtype.title = _("Econet");
1693 #endif
1694     sVhwinit = 1;
1695 }
1696 #endif /* KEEP_UNUSED */
1697
1698 #ifdef IFF_PORTSEL
1699 static const char *if_port_text[][4] =
1700 {
1701     /* Keep in step with <linux/netdevice.h> */
1702     {"unknown", NULL, NULL, NULL},
1703     {"10base2", "bnc", "coax", NULL},
1704     {"10baseT", "utp", "tpe", NULL},
1705     {"AUI", "thick", "db15", NULL},
1706     {"100baseT", NULL, NULL, NULL},
1707     {"100baseTX", NULL, NULL, NULL},
1708     {"100baseFX", NULL, NULL, NULL},
1709     {NULL, NULL, NULL, NULL},
1710 };
1711 #endif
1712
1713 /* Check our hardware type table for this type. */
1714 static struct hwtype *get_hwntype(int type)
1715 {
1716     struct hwtype **hwp;
1717
1718 #ifdef KEEP_UNUSED
1719     if (!sVhwinit)
1720         hwinit();
1721 #endif /* KEEP_UNUSED */
1722
1723     hwp = hwtypes;
1724     while (*hwp != NULL) {
1725         if ((*hwp)->type == type)
1726             return (*hwp);
1727         hwp++;
1728     }
1729     return (NULL);
1730 }
1731
1732 /* return 1 if address is all zeros */
1733 static int hw_null_address(struct hwtype *hw, void *ap)
1734 {
1735     unsigned int i;
1736     unsigned char *address = (unsigned char *)ap;
1737     for (i = 0; i < hw->alen; i++)
1738         if (address[i])
1739             return 0;
1740     return 1;
1741 }
1742
1743 static const char TRext[] = "\0\0k\0M";
1744
1745 static void print_bytes_scaled(unsigned long long ull, const char *end)
1746 {
1747         unsigned long long int_part;
1748         unsigned long frac_part;
1749         const char *ext;
1750         int i;
1751
1752         frac_part = 0;
1753         ext = TRext;
1754         int_part = ull;
1755         for (i=0 ; i<2 ; i++) {
1756                 if (int_part >= 1024) {
1757                         frac_part = ((int_part % 1024) * 10) / 1024;
1758                         int_part /= 1024;
1759                         ext += 2;                       /* Kb, Mb */
1760                 }
1761         }
1762
1763         printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1764 }
1765
1766 static void ife_print(struct interface *ptr)
1767 {
1768     struct aftype *ap;
1769     struct hwtype *hw;
1770     int hf;
1771     int can_compress = 0;
1772
1773 #if HAVE_AFIPX
1774     static struct aftype *ipxtype = NULL;
1775 #endif
1776 #if HAVE_AFECONET
1777     static struct aftype *ectype = NULL;
1778 #endif
1779 #if HAVE_AFATALK
1780     static struct aftype *ddptype = NULL;
1781 #endif
1782 #if HAVE_AFINET6
1783     FILE *f;
1784     char addr6[40], devname[20];
1785     struct sockaddr_in6 sap;
1786     int plen, scope, dad_status, if_idx;
1787     char addr6p[8][5];
1788 #endif
1789
1790     ap = get_afntype(ptr->addr.sa_family);
1791     if (ap == NULL)
1792         ap = get_afntype(0);
1793
1794     hf = ptr->type;
1795
1796     if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1797         can_compress = 1;
1798
1799     hw = get_hwntype(hf);
1800     if (hw == NULL)
1801         hw = get_hwntype(-1);
1802
1803     printf(_("%-9.9s Link encap:%s  "), ptr->name, _(hw->title));
1804     /* For some hardware types (eg Ash, ATM) we don't print the 
1805        hardware address if it's null.  */
1806     if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1807                                   hw->suppress_null_addr)))
1808         printf(_("HWaddr %s  "), hw->print(ptr->hwaddr));
1809 #ifdef IFF_PORTSEL
1810     if (ptr->flags & IFF_PORTSEL) {
1811         printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1812         if (ptr->flags & IFF_AUTOMEDIA)
1813             printf(_("(auto)"));
1814     }
1815 #endif
1816     printf("\n");
1817
1818 #if HAVE_AFINET
1819     if (ptr->has_ip) {
1820         printf(_("          %s addr:%s "), ap->name,
1821                ap->sprint(&ptr->addr, 1));
1822         if (ptr->flags & IFF_POINTOPOINT) {
1823             printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1824         }
1825         if (ptr->flags & IFF_BROADCAST) {
1826             printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1827         }
1828         printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1829     }
1830 #endif
1831
1832 #if HAVE_AFINET6
1833
1834 #define IPV6_ADDR_ANY           0x0000U
1835
1836 #define IPV6_ADDR_UNICAST       0x0001U
1837 #define IPV6_ADDR_MULTICAST     0x0002U
1838 #define IPV6_ADDR_ANYCAST       0x0004U
1839
1840 #define IPV6_ADDR_LOOPBACK      0x0010U
1841 #define IPV6_ADDR_LINKLOCAL     0x0020U
1842 #define IPV6_ADDR_SITELOCAL     0x0040U
1843   
1844 #define IPV6_ADDR_COMPATv4      0x0080U
1845     
1846 #define IPV6_ADDR_SCOPE_MASK    0x00f0U
1847     
1848 #define IPV6_ADDR_MAPPED        0x1000U
1849 #define IPV6_ADDR_RESERVED      0x2000U         /* reserved address space */
1850     
1851     if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1852         while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1853                       addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1854                       addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1855                   &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1856             if (!strcmp(devname, ptr->name)) {
1857                 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1858                         addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1859                         addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1860                 inet_pton(AF_INET6, addr6, (struct sockaddr *) &sap.sin6_addr);
1861                 sap.sin6_family=AF_INET6;
1862                 printf(_("          inet6 addr: %s/%d"),
1863                  inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1864                 printf(_(" Scope:"));
1865                 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1866                 case 0:
1867                     printf(_("Global"));
1868                     break;
1869                 case IPV6_ADDR_LINKLOCAL:
1870                     printf(_("Link"));
1871                     break;
1872                 case IPV6_ADDR_SITELOCAL:
1873                     printf(_("Site"));
1874                     break;
1875                 case IPV6_ADDR_COMPATv4:
1876                     printf(_("Compat"));
1877                     break;
1878                 case IPV6_ADDR_LOOPBACK:
1879                     printf(_("Host"));
1880                     break;
1881                 default:
1882                     printf(_("Unknown"));
1883                 }
1884                 printf("\n");
1885             }
1886         }
1887         fclose(f);
1888     }
1889 #endif
1890
1891 #if HAVE_AFIPX
1892     if (ipxtype == NULL)
1893         ipxtype = get_afntype(AF_IPX);
1894
1895     if (ipxtype != NULL) {
1896         if (ptr->has_ipx_bb)
1897             printf(_("          IPX/Ethernet II addr:%s\n"),
1898                    ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1899         if (ptr->has_ipx_sn)
1900             printf(_("          IPX/Ethernet SNAP addr:%s\n"),
1901                    ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1902         if (ptr->has_ipx_e2)
1903             printf(_("          IPX/Ethernet 802.2 addr:%s\n"),
1904                    ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1905         if (ptr->has_ipx_e3)
1906             printf(_("          IPX/Ethernet 802.3 addr:%s\n"),
1907                    ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1908     }
1909 #endif
1910
1911 #if HAVE_AFATALK
1912     if (ddptype == NULL)
1913         ddptype = get_afntype(AF_APPLETALK);
1914     if (ddptype != NULL) {
1915         if (ptr->has_ddp)
1916             printf(_("          EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
1917     }
1918 #endif
1919
1920 #if HAVE_AFECONET
1921     if (ectype == NULL)
1922         ectype = get_afntype(AF_ECONET);
1923     if (ectype != NULL) {
1924         if (ptr->has_econet)
1925             printf(_("          econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
1926     }
1927 #endif
1928
1929     printf("          ");
1930     /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1931     if (ptr->flags == 0)
1932         printf(_("[NO FLAGS] "));
1933     if (ptr->flags & IFF_UP)
1934         printf(_("UP "));
1935     if (ptr->flags & IFF_BROADCAST)
1936         printf(_("BROADCAST "));
1937     if (ptr->flags & IFF_DEBUG)
1938         printf(_("DEBUG "));
1939     if (ptr->flags & IFF_LOOPBACK)
1940         printf(_("LOOPBACK "));
1941     if (ptr->flags & IFF_POINTOPOINT)
1942         printf(_("POINTOPOINT "));
1943     if (ptr->flags & IFF_NOTRAILERS)
1944         printf(_("NOTRAILERS "));
1945     if (ptr->flags & IFF_RUNNING)
1946         printf(_("RUNNING "));
1947     if (ptr->flags & IFF_NOARP)
1948         printf(_("NOARP "));
1949     if (ptr->flags & IFF_PROMISC)
1950         printf(_("PROMISC "));
1951     if (ptr->flags & IFF_ALLMULTI)
1952         printf(_("ALLMULTI "));
1953     if (ptr->flags & IFF_SLAVE)
1954         printf(_("SLAVE "));
1955     if (ptr->flags & IFF_MASTER)
1956         printf(_("MASTER "));
1957     if (ptr->flags & IFF_MULTICAST)
1958         printf(_("MULTICAST "));
1959 #ifdef HAVE_DYNAMIC
1960     if (ptr->flags & IFF_DYNAMIC)
1961         printf(_("DYNAMIC "));
1962 #endif
1963     /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1964     printf(_(" MTU:%d  Metric:%d"),
1965            ptr->mtu, ptr->metric ? ptr->metric : 1);
1966 #ifdef SIOCSKEEPALIVE
1967     if (ptr->outfill || ptr->keepalive)
1968         printf(_("  Outfill:%d  Keepalive:%d"),
1969                ptr->outfill, ptr->keepalive);
1970 #endif
1971     printf("\n");
1972
1973     /* If needed, display the interface statistics. */
1974
1975     if (ptr->statistics_valid) {
1976         /* XXX: statistics are currently only printed for the primary address,
1977          *      not for the aliases, although strictly speaking they're shared
1978          *      by all addresses.
1979          */
1980         printf("          ");
1981
1982         printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1983                ptr->stats.rx_packets, ptr->stats.rx_errors,
1984                ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1985                ptr->stats.rx_frame_errors);
1986         if (can_compress)
1987             printf(_("             compressed:%lu\n"), ptr->stats.rx_compressed);
1988         printf("          ");
1989         printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1990                ptr->stats.tx_packets, ptr->stats.tx_errors,
1991                ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1992                ptr->stats.tx_carrier_errors);
1993         printf(_("          collisions:%lu "), ptr->stats.collisions);
1994         if (can_compress)
1995             printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1996         if (ptr->tx_queue_len != -1)
1997             printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1998         printf("\n          R");
1999         print_bytes_scaled(ptr->stats.rx_bytes, "  T");
2000         print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2001
2002     }
2003
2004     if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2005          ptr->map.base_addr)) {
2006         printf("          ");
2007         if (ptr->map.irq)
2008             printf(_("Interrupt:%d "), ptr->map.irq);
2009         if (ptr->map.base_addr >= 0x100)        /* Only print devices using it for 
2010                                                    I/O maps */
2011             printf(_("Base address:0x%lx "), (unsigned long)ptr->map.base_addr);
2012         if (ptr->map.mem_start) {
2013             printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2014         }
2015         if (ptr->map.dma)
2016             printf(_("DMA chan:%x "), ptr->map.dma);
2017         printf("\n");
2018     }
2019     printf("\n");
2020 }
2021
2022
2023 static int do_if_print(struct interface *ife, void *cookie)
2024 {
2025     int *opt_a = (int *) cookie;
2026     int res; 
2027
2028     res = do_if_fetch(ife); 
2029     if (res >= 0) {   
2030         if ((ife->flags & IFF_UP) || *opt_a)
2031             ife_print(ife);
2032     }
2033     return res;
2034 }
2035
2036 static struct interface *lookup_interface(char *name)
2037 {
2038     struct interface *ife = NULL;
2039
2040     if (if_readlist_proc(name) < 0) 
2041             return NULL; 
2042     ife = add_interface(name); 
2043     return ife;
2044 }
2045
2046 /* for ipv4 add/del modes */
2047 static int if_print(char *ifname)
2048 {
2049     int res;
2050
2051     if (!ifname) {
2052         res = for_all_interfaces(do_if_print, &interface_opt_a);
2053     } else {
2054         struct interface *ife;
2055
2056         ife = lookup_interface(ifname);
2057         res = do_if_fetch(ife); 
2058         if (res >= 0) 
2059             ife_print(ife);
2060     }
2061     return res; 
2062 }
2063
2064 int display_interfaces(char *ifname)
2065 {
2066     int status;
2067
2068     /* Create a channel to the NET kernel. */
2069     if ((skfd = sockets_open(0)) < 0) {
2070                 perror_msg_and_die("socket");
2071     }
2072
2073     /* Do we have to show the current setup? */
2074     status = if_print(ifname);
2075     close(skfd);
2076     exit(status < 0);
2077 }