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