2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
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.
12 * -----------------------------------------------------------
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.
18 * Version: $Id: interface.c,v 1.8 2002/07/03 11:46:36 andersen Exp $
20 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
21 * and others. Copyright 1993 MicroWalt Corporation
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.
29 * Patched to support 'add' and 'del' keywords for INET(4) addresses
30 * by Mrs. Brisby <mrs.brisby@nimh.org>
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)
39 /* #define KEEP_UNUSED */
55 #if CONFIG_FEATURE_IPV6
56 #define HAVE_AFINET6 1
61 * Device Hardware types.
64 #define HAVE_HWETHER 1
69 #include "inet_common.h"
77 #include <sys/ioctl.h>
79 #include <net/if_arp.h>
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))
88 static int procnetdev_vsn = 1;
90 /* Ugh. But libc5 doesn't provide POSIX types. */
91 #include <asm/types.h>
95 #include <linux/if_slip.h>
102 * This is in linux/include/net/ipv6.h.
106 struct in6_addr ifr6_addr;
107 __u32 ifr6_prefixlen;
108 unsigned int ifr6_ifindex;
113 #endif /* HAVE_AFINET6 */
116 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
117 #include <netipx/ipx.h>
123 /* Defines for glibc2.0 users. */
124 #ifndef SIOCSIFTXQLEN
125 #define SIOCSIFTXQLEN 0x8943
126 #define SIOCGIFTXQLEN 0x8942
129 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
131 #define ifr_qlen ifr_ifru.ifru_mtu
134 #ifndef HAVE_TXQUEUELEN
135 #define HAVE_TXQUEUELEN 1
139 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
142 /* This structure defines protocol families and their handlers. */
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);
156 int (*getmask) (char *src, struct sockaddr * mask, char *name);
162 static struct aftype *aftypes[];
171 static int flag_ax25;
177 static int flag_netrom;
179 static int flag_inet;
181 static int flag_inet6;
184 static int flag_econet;
187 static int flag_x25 = 0;
194 static struct aftrans_t {
202 "ax25", "ax25", &flag_ax25
206 "ip", "inet", &flag_inet
210 "ip6", "inet6", &flag_inet6
215 "ipx", "ipx", &flag_ipx
220 "appletalk", "ddp", &flag_ddp
225 "netrom", "netrom", &flag_netrom
229 "inet", "inet", &flag_inet
233 "inet6", "inet6", &flag_inet6
238 "ddp", "ddp", &flag_ddp
242 "unix", "unix", &flag_unx
245 "tcpip", "inet", &flag_inet
249 "econet", "ec", &flag_econet
254 "x25", "x25", &flag_x25
259 "ash", "ash", &flag_ash
267 static char afname[256] = "";
268 #endif /* KEEP_UNUSED */
272 /* Display a UNIX domain address. */
273 static char *UNIX_print(unsigned char *ptr)
279 /* Display a UNIX domain address. */
280 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
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));
290 static struct aftype unix_aftype =
292 "unix", "UNIX Domain", AF_UNIX, 0,
293 UNIX_print, UNIX_sprint, NULL, NULL,
298 #endif /* HAVE_AFUNIX */
303 static void INET_reserror(char *text)
308 /* Display an Internet socket address. */
309 static char *INET_print(unsigned char *ptr)
311 return (inet_ntoa((*(struct in_addr *) ptr)));
313 #endif /* KEEP_UNUSED */
315 /* Display an Internet socket address. */
316 static char *INET_sprint(struct sockaddr *sap, int numeric)
318 static char buff[128];
320 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
321 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
323 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
324 numeric, 0xffffff00) != 0)
331 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
332 unsigned int netmask)
334 static char buff[128];
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)
344 static int INET_getsock(char *bufp, struct sockaddr *sap)
346 char *sp = bufp, *bp;
349 struct sockaddr_in *sin;
351 sin = (struct sockaddr_in *) sap;
352 sin->sin_family = AF_INET;
357 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
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');
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');
380 sin->sin_addr.s_addr = htonl(val);
385 static int INET_input(int type, char *bufp, struct sockaddr *sap)
389 return (INET_getsock(bufp, sap));
391 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
393 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
397 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
399 struct sockaddr_in *mask = (struct sockaddr_in *) m;
403 if ((slash = strchr(adr, '/')) == NULL)
407 prefix = strtoul(slash, &end, 0);
412 sprintf(name, "/%d", prefix);
414 mask->sin_family = AF_INET;
415 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
418 #endif /* KEEP_UNUSED */
420 static struct aftype inet_aftype =
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 */,
431 #endif /* HAVE_AFINET */
436 static void INET6_reserror(char *text)
441 /* Display an Internet socket address. */
442 static char *INET6_print(unsigned char *ptr)
444 static char name[80];
446 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
449 #endif /* KEEP_UNUSED */
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)
455 static char buff[128];
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));
465 static int INET6_getsock(char *bufp, struct sockaddr *sap)
467 struct sockaddr_in6 *sin6;
469 sin6 = (struct sockaddr_in6 *) sap;
470 sin6->sin6_family = AF_INET6;
473 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
479 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
483 return (INET6_getsock(bufp, sap));
485 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
488 #endif /* KEEP_UNUSED */
490 static struct aftype inet6_aftype =
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 */,
501 #endif /* HAVE_AFINET6 */
503 /* Display an UNSPEC address. */
504 static char *UNSPEC_print(unsigned char *ptr)
506 static char buff[sizeof(struct sockaddr)*3+1];
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));
516 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
521 /* Display an UNSPEC socket address. */
522 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
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));
531 static struct aftype unspec_aftype =
533 "unspec", "UNSPEC", AF_UNSPEC, 0,
534 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
538 static struct aftype *aftypes[] =
578 static short sVafinit = 0;
582 unspec_aftype.title = _("UNSPEC");
584 unix_aftype.title = _("UNIX Domain");
587 inet_aftype.title = _("DARPA Internet");
590 inet6_aftype.title = _("IPv6");
593 ax25_aftype.title = _("AMPR AX.25");
596 netrom_aftype.title = _("AMPR NET/ROM");
599 ipx_aftype.title = _("Novell IPX");
602 ddp_aftype.title = _("Appletalk DDP");
605 ec_aftype.title = _("Econet");
608 x25_aftype.title = _("CCITT X.25");
611 rose_aftype.title = _("AMPR ROSE");
614 ash_aftype.title = _("Ash");
619 static int aftrans_opt(const char *arg)
621 struct aftrans_t *paft;
625 safe_strncpy(buf, arg, sizeof(buf));
631 tmp2 = strchr(tmp1, ',');
637 for (paft = aftrans; paft->alias; paft++) {
638 if (strcmp(tmp1, paft->alias))
640 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
641 fprintf(stderr, _("Too much address family arguments.\n"));
648 strcat(afname, paft->name);
652 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
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)
667 strcpy(afname, dflt);
669 if (!(tmp = strrchr(argv0, '/')))
670 tmp = argv0; /* no slash?! */
674 if (!(buf = strdup(tmp)))
677 if (strlen(tool) >= strlen(tmp)) {
681 tmp = buf + (strlen(tmp) - strlen(tool));
683 if (strcmp(tmp, tool) != 0) {
688 if ((tmp = strchr(buf, '_')))
692 if (aftrans_opt(buf))
698 /* Check our protocol family table for this family. */
699 static struct aftype *get_aftype(const char *name)
706 #endif /* KEEP_UNUSED */
709 while (*afp != NULL) {
710 if (!strcmp((*afp)->name, name))
714 if (strchr(name, ','))
715 fprintf(stderr, _("Please don't supply more than one address family.\n"));
718 #endif /* KEEP_UNUSED */
720 /* Check our protocol family table for this family. */
721 static struct aftype *get_afntype(int af)
728 #endif /* KEEP_UNUSED */
731 while (*afp != NULL) {
732 if ((*afp)->af == af)
739 /* Check our protocol family table for this family and return its socket */
740 static int get_socket_for_af(int af)
747 #endif /* KEEP_UNUSED */
750 while (*afp != NULL) {
751 if ((*afp)->af == af)
759 /* type: 0=all, 1=getroute */
760 static void print_aflist(int type) {
768 #endif /* KEEP_UNUSED */
771 while (*afp != NULL) {
772 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
775 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
776 txt = (*afp)->name; if (!txt) txt = "..";
777 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
781 fprintf(stderr,"\n");
783 #endif /* KEEP_UNUSED */
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;
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;
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 */
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 */
849 int interface_opt_a = 0; /* show all interfaces */
852 static int opt_i = 0; /* show the statistics */
853 static int opt_v = 0; /* debugging output flag */
855 static int addr_family = 0; /* currently selected AF */
856 #endif /* KEEP_UNUSED */
858 static struct interface *int_list, *int_last;
859 static int skfd = -1; /* generic raw socket desc. */
862 static int sockets_open(int family)
866 static int force = -1;
870 if (get_kernel_revision() < KRELEASE(2, 1, 0))
872 if (access("/proc/net", R_OK))
875 for (aft = aftypes; *aft; aft++) {
876 struct aftype *af = *aft;
877 int type = SOCK_DGRAM;
878 if (af->af == AF_UNSPEC)
880 if (family && family != af->af)
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))
892 if (af->af == AF_NETROM)
893 type = SOCK_SEQPACKET;
896 if (af->af == AF_X25)
897 type = SOCK_SEQPACKET;
899 af->fd = socket(af->af, type, 0);
904 fprintf(stderr, _("No usable address families found.\n"));
908 /* like strcmp(), but knows about numbers */
909 static int nstrcmp(const char *astr, const char *b)
911 const char *a = astr;
932 return atoi(a) > atoi(b) ? 1 : -1;
937 static struct interface *add_interface(char *name)
939 struct interface *ife, **nextp, *new;
941 for (ife = int_last; ife; ife = ife->prev) {
942 int n = nstrcmp(ife->name, name);
949 safe_strncpy(new->name, name, IFNAMSIZ);
950 nextp = ife ? &ife->next : &int_list;
954 new->next->prev = new;
962 static int if_readconf(void)
970 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
972 skfd2 = get_socket_for_af(AF_INET);
974 fprintf(stderr, _("warning: no inet socket available: %s\n"),
976 /* Try to soldier on with whatever socket we can get hold of. */
977 skfd2 = sockets_open(0);
984 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
985 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
987 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
988 perror("SIOCGIFCONF");
991 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
992 /* assume it overflowed and try again */
1000 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1001 add_interface(ifr->ifr_name);
1011 static char *get_name(char *name, char *p)
1018 if (*p == ':') { /* could be an alias */
1019 char *dot = p, *dotname = name;
1023 if (*p != ':') { /* it wasn't, backup */
1038 static int get_dev_fields(char *bp, struct interface *ife)
1040 switch (procnetdev_vsn) {
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,
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);
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,
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;
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,
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;
1102 static inline int procnetdev_version(char *buf)
1104 if (strstr(buf, "compressed"))
1106 if (strstr(buf, "bytes"))
1111 static int if_readlist_proc(char *target)
1113 static int proc_read;
1116 struct interface *ife;
1124 fh = fopen(_PATH_PROCNET_DEV, "r");
1126 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1127 _PATH_PROCNET_DEV, strerror(errno));
1128 return if_readconf();
1130 fgets(buf, sizeof buf, fh); /* eat line */
1131 fgets(buf, sizeof buf, fh);
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 */
1142 "compressed", "%lu",
1151 "compressed", "%lu",
1156 procnetdev_vsn = procnetdev_version(buf);
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))
1170 perror(_PATH_PROCNET_DEV);
1182 static int if_readlist(void)
1184 int err = if_readlist_proc(NULL);
1186 err = if_readconf();
1190 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1192 struct interface *ife;
1194 if (!int_list && (if_readlist() < 0))
1196 for (ife = int_list; ife; ife = ife->next) {
1197 int err = doit(ife, cookie);
1204 /* Support for fetching an IPX address */
1207 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1209 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1210 return ioctl(sock, SIOCGIFADDR, ifr);
1215 /* Fetch the interface configuration from the kernel. */
1216 static int if_fetch(struct interface *ife)
1220 char *ifname = ife->name;
1222 strcpy(ifr.ifr_name, ifname);
1223 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1225 ife->flags = ifr.ifr_flags;
1227 strcpy(ifr.ifr_name, ifname);
1228 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1229 memset(ife->hwaddr, 0, 32);
1231 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1233 ife->type = ifr.ifr_hwaddr.sa_family;
1235 strcpy(ifr.ifr_name, ifname);
1236 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1239 ife->metric = ifr.ifr_metric;
1241 strcpy(ifr.ifr_name, ifname);
1242 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1245 ife->mtu = ifr.ifr_mtu;
1248 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1249 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1250 ife->type == ARPHRD_ADAPT) {
1252 strcpy(ifr.ifr_name, ifname);
1253 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1256 ife->outfill = (unsigned int) ifr.ifr_data;
1258 #ifdef SIOCGKEEPALIVE
1259 strcpy(ifr.ifr_name, ifname);
1260 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1263 ife->keepalive = (unsigned int) ifr.ifr_data;
1268 strcpy(ifr.ifr_name, ifname);
1269 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1270 memset(&ife->map, 0, sizeof(struct ifmap));
1272 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1274 strcpy(ifr.ifr_name, ifname);
1275 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1276 memset(&ife->map, 0, sizeof(struct ifmap));
1278 ife->map = ifr.ifr_map;
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 */
1285 ife->tx_queue_len = ifr.ifr_qlen;
1287 ife->tx_queue_len = -1; /* unknown value */
1292 fd = get_socket_for_af(AF_INET);
1294 strcpy(ifr.ifr_name, ifname);
1295 ifr.ifr_addr.sa_family = AF_INET;
1296 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
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));
1303 ife->dstaddr = ifr.ifr_dstaddr;
1305 strcpy(ifr.ifr_name, ifname);
1306 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1307 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1309 ife->broadaddr = ifr.ifr_broadaddr;
1311 strcpy(ifr.ifr_name, ifname);
1312 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1313 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1315 ife->netmask = ifr.ifr_netmask;
1317 memset(&ife->addr, 0, sizeof(struct sockaddr));
1322 /* DDP address maybe ? */
1323 fd = get_socket_for_af(AF_APPLETALK);
1325 strcpy(ifr.ifr_name, ifname);
1326 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1327 ife->ddpaddr = ifr.ifr_addr;
1334 /* Look for IPX addresses with all framing types */
1335 fd = get_socket_for_af(AF_IPX);
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;
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;
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;
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;
1361 /* Econet address maybe? */
1362 fd = get_socket_for_af(AF_ECONET);
1364 strcpy(ifr.ifr_name, ifname);
1365 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1366 ife->ecaddr = ifr.ifr_addr;
1367 ife->has_econet = 1;
1376 static int do_if_fetch(struct interface *ife)
1378 if (if_fetch(ife) < 0) {
1380 if (errno == ENODEV) {
1381 /* Give better error message for this case. */
1382 errmsg = _("Device not found");
1384 errmsg = strerror(errno);
1386 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1393 /* This structure defines hardware protocols and their handlers. */
1399 char *(*print) (unsigned char *);
1400 int (*input) (char *, struct sockaddr *);
1401 int (*activate) (int fd);
1402 int suppress_null_addr;
1405 static struct hwtype unspec_hwtype =
1407 "unspec", "UNSPEC", -1, 0,
1408 UNSPEC_print, NULL, NULL
1411 static struct hwtype loop_hwtype =
1413 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1418 #include <net/if_arp.h>
1419 #include <linux/if_ether.h>
1421 static struct hwtype ether_hwtype;
1423 /* Display an Ethernet address in readable format. */
1424 static char *pr_ether(unsigned char *ptr)
1426 static char buff[64];
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)
1436 /* Input an Ethernet address and convert to binary. */
1437 static int in_ether(char *bufp, struct sockaddr *sap)
1444 sap->sa_family = ether_hwtype.type;
1449 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1454 else if (c >= 'a' && c <= 'f')
1456 else if (c >= 'A' && c <= 'F')
1460 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
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)
1477 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1484 *ptr++ = (unsigned char) (val & 0377);
1487 /* We might get a semicolon here - not required. */
1489 if (i == ETH_ALEN) {
1491 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1500 /* That's it. Any trailing junk? */
1501 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1503 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1509 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1514 #endif /* KEEP_UNUSED */
1517 static struct hwtype ether_hwtype =
1519 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1520 pr_ether, NULL /* UNUSED in_ether */, NULL
1524 #endif /* HAVE_HWETHER */
1529 #include <net/if_arp.h>
1532 /* Start the PPP encapsulation on the file descriptor. */
1533 static int do_ppp(int fd)
1535 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1538 #endif /* KEEP_UNUSED */
1540 static struct hwtype ppp_hwtype =
1542 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1543 NULL, NULL, NULL /* UNUSED do_ppp */, 0
1547 #endif /* HAVE_PPP */
1549 static struct hwtype *hwtypes[] =
1572 #ifdef ARPHRD_IEEE802_TR
1625 static short sVhwinit = 0;
1627 static void hwinit()
1629 loop_hwtype.title = _("Local Loopback");
1630 unspec_hwtype.title = _("UNSPEC");
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");
1639 ether_hwtype.title = _("Ethernet");
1642 ash_hwtype.title = _("Ash");
1645 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1648 hippi_hwtype.title = _("HIPPI");
1651 ax25_hwtype.title = _("AMPR AX.25");
1654 rose_hwtype.title = _("AMPR ROSE");
1657 netrom_hwtype.title = _("AMPR NET/ROM");
1660 x25_hwtype.title = _("generic X.25");
1663 tunnel_hwtype.title = _("IPIP Tunnel");
1666 ppp_hwtype.title = _("Point-to-Point Protocol");
1669 hdlc_hwtype.title = _("(Cisco)-HDLC");
1670 lapb_hwtype.title = _("LAPB");
1673 arcnet_hwtype.title = _("ARCnet");
1676 dlci_hwtype.title = _("Frame Relay DLCI");
1677 frad_hwtype.title = _("Frame Relay Access Device");
1680 sit_hwtype.title = _("IPv6-in-IPv4");
1683 irda_hwtype.title = _("IrLAP");
1686 tr_hwtype.title = _("16/4 Mbps Token Ring");
1687 #ifdef ARPHRD_IEEE802_TR
1688 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1692 ec_hwtype.title = _("Econet");
1696 #endif /* KEEP_UNUSED */
1699 static const char *if_port_text[][4] =
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},
1713 /* Check our hardware type table for this type. */
1714 static struct hwtype *get_hwntype(int type)
1716 struct hwtype **hwp;
1721 #endif /* KEEP_UNUSED */
1724 while (*hwp != NULL) {
1725 if ((*hwp)->type == type)
1732 /* return 1 if address is all zeros */
1733 static int hw_null_address(struct hwtype *hw, void *ap)
1736 unsigned char *address = (unsigned char *)ap;
1737 for (i = 0; i < hw->alen; i++)
1743 static const char TRext[] = "\0\0k\0M";
1745 static void print_bytes_scaled(unsigned long long ull, const char *end)
1747 unsigned long long int_part;
1748 unsigned long frac_part;
1755 for (i=0 ; i<2 ; i++) {
1756 if (int_part >= 1024) {
1757 frac_part = ((int_part % 1024) * 10) / 1024;
1759 ext += 2; /* Kb, Mb */
1763 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1766 static void ife_print(struct interface *ptr)
1771 int can_compress = 0;
1774 static struct aftype *ipxtype = NULL;
1777 static struct aftype *ectype = NULL;
1780 static struct aftype *ddptype = NULL;
1784 char addr6[40], devname[20];
1785 struct sockaddr_in6 sap;
1786 int plen, scope, dad_status, if_idx;
1790 ap = get_afntype(ptr->addr.sa_family);
1792 ap = get_afntype(0);
1796 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1799 hw = get_hwntype(hf);
1801 hw = get_hwntype(-1);
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));
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)"));
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));
1825 if (ptr->flags & IFF_BROADCAST) {
1826 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1828 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1834 #define IPV6_ADDR_ANY 0x0000U
1836 #define IPV6_ADDR_UNICAST 0x0001U
1837 #define IPV6_ADDR_MULTICAST 0x0002U
1838 #define IPV6_ADDR_ANYCAST 0x0004U
1840 #define IPV6_ADDR_LOOPBACK 0x0010U
1841 #define IPV6_ADDR_LINKLOCAL 0x0020U
1842 #define IPV6_ADDR_SITELOCAL 0x0040U
1844 #define IPV6_ADDR_COMPATv4 0x0080U
1846 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1848 #define IPV6_ADDR_MAPPED 0x1000U
1849 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
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) {
1867 printf(_("Global"));
1869 case IPV6_ADDR_LINKLOCAL:
1872 case IPV6_ADDR_SITELOCAL:
1875 case IPV6_ADDR_COMPATv4:
1876 printf(_("Compat"));
1878 case IPV6_ADDR_LOOPBACK:
1882 printf(_("Unknown"));
1892 if (ipxtype == NULL)
1893 ipxtype = get_afntype(AF_IPX);
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));
1912 if (ddptype == NULL)
1913 ddptype = get_afntype(AF_APPLETALK);
1914 if (ddptype != NULL) {
1916 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
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));
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)
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 "));
1960 if (ptr->flags & IFF_DYNAMIC)
1961 printf(_("DYNAMIC "));
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);
1973 /* If needed, display the interface statistics. */
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
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);
1987 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
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);
1995 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1996 if (ptr->tx_queue_len != -1)
1997 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1999 print_bytes_scaled(ptr->stats.rx_bytes, " T");
2000 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2004 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2005 ptr->map.base_addr)) {
2008 printf(_("Interrupt:%d "), ptr->map.irq);
2009 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
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);
2016 printf(_("DMA chan:%x "), ptr->map.dma);
2023 static int do_if_print(struct interface *ife, void *cookie)
2025 int *opt_a = (int *) cookie;
2028 res = do_if_fetch(ife);
2030 if ((ife->flags & IFF_UP) || *opt_a)
2036 static struct interface *lookup_interface(char *name)
2038 struct interface *ife = NULL;
2040 if (if_readlist_proc(name) < 0)
2042 ife = add_interface(name);
2046 /* for ipv4 add/del modes */
2047 static int if_print(char *ifname)
2052 res = for_all_interfaces(do_if_print, &interface_opt_a);
2054 struct interface *ife;
2056 ife = lookup_interface(ifname);
2057 res = do_if_fetch(ife);
2064 int display_interfaces(char *ifname)
2068 /* Create a channel to the NET kernel. */
2069 if ((skfd = sockets_open(0)) < 0) {
2070 perror_msg_and_die("socket");
2073 /* Do we have to show the current setup? */
2074 status = if_print(ifname);