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.7 2001/11/10 11:22:46 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 */
57 * Device Hardware types.
60 #define HAVE_HWETHER 1
65 #include "inet_common.h"
73 #include <sys/ioctl.h>
75 #include <net/if_arp.h>
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))
83 static int procnetdev_vsn = 1;
85 /* Ugh. But libc5 doesn't provide POSIX types. */
86 #include <asm/types.h>
90 #include <linux/if_slip.h>
97 * This is in linux/include/net/ipv6.h.
101 struct in6_addr ifr6_addr;
102 __u32 ifr6_prefixlen;
103 unsigned int ifr6_ifindex;
108 #endif /* HAVE_AFINET6 */
111 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
112 #include <netipx/ipx.h>
118 /* Defines for glibc2.0 users. */
119 #ifndef SIOCSIFTXQLEN
120 #define SIOCSIFTXQLEN 0x8943
121 #define SIOCGIFTXQLEN 0x8942
124 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
126 #define ifr_qlen ifr_ifru.ifru_mtu
129 #ifndef HAVE_TXQUEUELEN
130 #define HAVE_TXQUEUELEN 1
134 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
137 /* This structure defines protocol families and their handlers. */
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);
151 int (*getmask) (char *src, struct sockaddr * mask, char *name);
157 static struct aftype *aftypes[];
166 static int flag_ax25;
172 static int flag_netrom;
174 static int flag_inet;
176 static int flag_inet6;
179 static int flag_econet;
182 static int flag_x25 = 0;
189 static struct aftrans_t {
197 "ax25", "ax25", &flag_ax25
201 "ip", "inet", &flag_inet
205 "ip6", "inet6", &flag_inet6
210 "ipx", "ipx", &flag_ipx
215 "appletalk", "ddp", &flag_ddp
220 "netrom", "netrom", &flag_netrom
224 "inet", "inet", &flag_inet
228 "inet6", "inet6", &flag_inet6
233 "ddp", "ddp", &flag_ddp
237 "unix", "unix", &flag_unx
240 "tcpip", "inet", &flag_inet
244 "econet", "ec", &flag_econet
249 "x25", "x25", &flag_x25
254 "ash", "ash", &flag_ash
262 static char afname[256] = "";
263 #endif /* KEEP_UNUSED */
267 /* Display a UNIX domain address. */
268 static char *UNIX_print(unsigned char *ptr)
274 /* Display a UNIX domain address. */
275 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
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));
285 static struct aftype unix_aftype =
287 "unix", "UNIX Domain", AF_UNIX, 0,
288 UNIX_print, UNIX_sprint, NULL, NULL,
293 #endif /* HAVE_AFUNIX */
298 static void INET_reserror(char *text)
303 /* Display an Internet socket address. */
304 static char *INET_print(unsigned char *ptr)
306 return (inet_ntoa((*(struct in_addr *) ptr)));
308 #endif /* KEEP_UNUSED */
310 /* Display an Internet socket address. */
311 static char *INET_sprint(struct sockaddr *sap, int numeric)
313 static char buff[128];
315 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
316 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
318 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
319 numeric, 0xffffff00) != 0)
326 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
327 unsigned int netmask)
329 static char buff[128];
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)
339 static int INET_getsock(char *bufp, struct sockaddr *sap)
341 char *sp = bufp, *bp;
344 struct sockaddr_in *sin;
346 sin = (struct sockaddr_in *) sap;
347 sin->sin_family = AF_INET;
352 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
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');
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');
375 sin->sin_addr.s_addr = htonl(val);
380 static int INET_input(int type, char *bufp, struct sockaddr *sap)
384 return (INET_getsock(bufp, sap));
386 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
388 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
392 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
394 struct sockaddr_in *mask = (struct sockaddr_in *) m;
398 if ((slash = strchr(adr, '/')) == NULL)
402 prefix = strtoul(slash, &end, 0);
407 sprintf(name, "/%d", prefix);
409 mask->sin_family = AF_INET;
410 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
413 #endif /* KEEP_UNUSED */
415 static struct aftype inet_aftype =
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 */,
426 #endif /* HAVE_AFINET */
428 /* Display an UNSPEC address. */
429 static char *UNSPEC_print(unsigned char *ptr)
431 static char buff[sizeof(struct sockaddr)*3+1];
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));
441 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
446 /* Display an UNSPEC socket address. */
447 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
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));
456 static struct aftype unspec_aftype =
458 "unspec", "UNSPEC", AF_UNSPEC, 0,
459 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
463 static struct aftype *aftypes[] =
503 static short sVafinit = 0;
507 unspec_aftype.title = _("UNSPEC");
509 unix_aftype.title = _("UNIX Domain");
512 inet_aftype.title = _("DARPA Internet");
515 inet6_aftype.title = _("IPv6");
518 ax25_aftype.title = _("AMPR AX.25");
521 netrom_aftype.title = _("AMPR NET/ROM");
524 ipx_aftype.title = _("Novell IPX");
527 ddp_aftype.title = _("Appletalk DDP");
530 ec_aftype.title = _("Econet");
533 x25_aftype.title = _("CCITT X.25");
536 rose_aftype.title = _("AMPR ROSE");
539 ash_aftype.title = _("Ash");
544 static int aftrans_opt(const char *arg)
546 struct aftrans_t *paft;
550 safe_strncpy(buf, arg, sizeof(buf));
556 tmp2 = strchr(tmp1, ',');
562 for (paft = aftrans; paft->alias; paft++) {
563 if (strcmp(tmp1, paft->alias))
565 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
566 fprintf(stderr, _("Too much address family arguments.\n"));
573 strcat(afname, paft->name);
577 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
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)
592 strcpy(afname, dflt);
594 if (!(tmp = strrchr(argv0, '/')))
595 tmp = argv0; /* no slash?! */
599 if (!(buf = strdup(tmp)))
602 if (strlen(tool) >= strlen(tmp)) {
606 tmp = buf + (strlen(tmp) - strlen(tool));
608 if (strcmp(tmp, tool) != 0) {
613 if ((tmp = strchr(buf, '_')))
617 if (aftrans_opt(buf))
623 /* Check our protocol family table for this family. */
624 static struct aftype *get_aftype(const char *name)
631 #endif /* KEEP_UNUSED */
634 while (*afp != NULL) {
635 if (!strcmp((*afp)->name, name))
639 if (strchr(name, ','))
640 fprintf(stderr, _("Please don't supply more than one address family.\n"));
643 #endif /* KEEP_UNUSED */
645 /* Check our protocol family table for this family. */
646 static struct aftype *get_afntype(int af)
653 #endif /* KEEP_UNUSED */
656 while (*afp != NULL) {
657 if ((*afp)->af == af)
664 /* Check our protocol family table for this family and return its socket */
665 static int get_socket_for_af(int af)
672 #endif /* KEEP_UNUSED */
675 while (*afp != NULL) {
676 if ((*afp)->af == af)
684 /* type: 0=all, 1=getroute */
685 static void print_aflist(int type) {
693 #endif /* KEEP_UNUSED */
696 while (*afp != NULL) {
697 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
700 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
701 txt = (*afp)->name; if (!txt) txt = "..";
702 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
706 fprintf(stderr,"\n");
708 #endif /* KEEP_UNUSED */
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;
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;
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 */
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 */
774 int interface_opt_a = 0; /* show all interfaces */
777 static int opt_i = 0; /* show the statistics */
778 static int opt_v = 0; /* debugging output flag */
780 static int addr_family = 0; /* currently selected AF */
781 #endif /* KEEP_UNUSED */
783 static struct interface *int_list, *int_last;
784 static int skfd = -1; /* generic raw socket desc. */
787 static int sockets_open(int family)
791 static int force = -1;
795 if (get_kernel_revision() < KRELEASE(2, 1, 0))
797 if (access("/proc/net", R_OK))
800 for (aft = aftypes; *aft; aft++) {
801 struct aftype *af = *aft;
802 int type = SOCK_DGRAM;
803 if (af->af == AF_UNSPEC)
805 if (family && family != af->af)
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))
817 if (af->af == AF_NETROM)
818 type = SOCK_SEQPACKET;
821 if (af->af == AF_X25)
822 type = SOCK_SEQPACKET;
824 af->fd = socket(af->af, type, 0);
829 fprintf(stderr, _("No usable address families found.\n"));
833 /* like strcmp(), but knows about numbers */
834 static int nstrcmp(const char *astr, const char *b)
836 const char *a = astr;
857 return atoi(a) > atoi(b) ? 1 : -1;
862 static struct interface *add_interface(char *name)
864 struct interface *ife, **nextp, *new;
866 for (ife = int_last; ife; ife = ife->prev) {
867 int n = nstrcmp(ife->name, name);
874 safe_strncpy(new->name, name, IFNAMSIZ);
875 nextp = ife ? &ife->next : &int_list;
879 new->next->prev = new;
887 static int if_readconf(void)
895 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
897 skfd2 = get_socket_for_af(AF_INET);
899 fprintf(stderr, _("warning: no inet socket available: %s\n"),
901 /* Try to soldier on with whatever socket we can get hold of. */
902 skfd2 = sockets_open(0);
909 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
910 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
912 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
913 perror("SIOCGIFCONF");
916 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
917 /* assume it overflowed and try again */
925 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
926 add_interface(ifr->ifr_name);
936 static char *get_name(char *name, char *p)
943 if (*p == ':') { /* could be an alias */
944 char *dot = p, *dotname = name;
948 if (*p != ':') { /* it wasn't, backup */
963 static int get_dev_fields(char *bp, struct interface *ife)
965 switch (procnetdev_vsn) {
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,
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);
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,
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;
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,
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;
1027 static inline int procnetdev_version(char *buf)
1029 if (strstr(buf, "compressed"))
1031 if (strstr(buf, "bytes"))
1036 static int if_readlist_proc(char *target)
1038 static int proc_read;
1041 struct interface *ife;
1049 fh = fopen(_PATH_PROCNET_DEV, "r");
1051 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1052 _PATH_PROCNET_DEV, strerror(errno));
1053 return if_readconf();
1055 fgets(buf, sizeof buf, fh); /* eat line */
1056 fgets(buf, sizeof buf, fh);
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 */
1067 "compressed", "%lu",
1076 "compressed", "%lu",
1081 procnetdev_vsn = procnetdev_version(buf);
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))
1095 perror(_PATH_PROCNET_DEV);
1107 static int if_readlist(void)
1109 int err = if_readlist_proc(NULL);
1111 err = if_readconf();
1115 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1117 struct interface *ife;
1119 if (!int_list && (if_readlist() < 0))
1121 for (ife = int_list; ife; ife = ife->next) {
1122 int err = doit(ife, cookie);
1129 /* Support for fetching an IPX address */
1132 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1134 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1135 return ioctl(sock, SIOCGIFADDR, ifr);
1140 /* Fetch the interface configuration from the kernel. */
1141 static int if_fetch(struct interface *ife)
1145 char *ifname = ife->name;
1147 strcpy(ifr.ifr_name, ifname);
1148 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1150 ife->flags = ifr.ifr_flags;
1152 strcpy(ifr.ifr_name, ifname);
1153 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1154 memset(ife->hwaddr, 0, 32);
1156 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1158 ife->type = ifr.ifr_hwaddr.sa_family;
1160 strcpy(ifr.ifr_name, ifname);
1161 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1164 ife->metric = ifr.ifr_metric;
1166 strcpy(ifr.ifr_name, ifname);
1167 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1170 ife->mtu = ifr.ifr_mtu;
1173 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1174 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1175 ife->type == ARPHRD_ADAPT) {
1177 strcpy(ifr.ifr_name, ifname);
1178 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1181 ife->outfill = (unsigned int) ifr.ifr_data;
1183 #ifdef SIOCGKEEPALIVE
1184 strcpy(ifr.ifr_name, ifname);
1185 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1188 ife->keepalive = (unsigned int) ifr.ifr_data;
1193 strcpy(ifr.ifr_name, ifname);
1194 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1195 memset(&ife->map, 0, sizeof(struct ifmap));
1197 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1199 strcpy(ifr.ifr_name, ifname);
1200 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1201 memset(&ife->map, 0, sizeof(struct ifmap));
1203 ife->map = ifr.ifr_map;
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 */
1210 ife->tx_queue_len = ifr.ifr_qlen;
1212 ife->tx_queue_len = -1; /* unknown value */
1217 fd = get_socket_for_af(AF_INET);
1219 strcpy(ifr.ifr_name, ifname);
1220 ifr.ifr_addr.sa_family = AF_INET;
1221 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
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));
1228 ife->dstaddr = ifr.ifr_dstaddr;
1230 strcpy(ifr.ifr_name, ifname);
1231 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1232 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1234 ife->broadaddr = ifr.ifr_broadaddr;
1236 strcpy(ifr.ifr_name, ifname);
1237 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1238 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1240 ife->netmask = ifr.ifr_netmask;
1242 memset(&ife->addr, 0, sizeof(struct sockaddr));
1247 /* DDP address maybe ? */
1248 fd = get_socket_for_af(AF_APPLETALK);
1250 strcpy(ifr.ifr_name, ifname);
1251 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1252 ife->ddpaddr = ifr.ifr_addr;
1259 /* Look for IPX addresses with all framing types */
1260 fd = get_socket_for_af(AF_IPX);
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;
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;
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;
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;
1286 /* Econet address maybe? */
1287 fd = get_socket_for_af(AF_ECONET);
1289 strcpy(ifr.ifr_name, ifname);
1290 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1291 ife->ecaddr = ifr.ifr_addr;
1292 ife->has_econet = 1;
1301 static int do_if_fetch(struct interface *ife)
1303 if (if_fetch(ife) < 0) {
1305 if (errno == ENODEV) {
1306 /* Give better error message for this case. */
1307 errmsg = _("Device not found");
1309 errmsg = strerror(errno);
1311 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1318 /* This structure defines hardware protocols and their handlers. */
1324 char *(*print) (unsigned char *);
1325 int (*input) (char *, struct sockaddr *);
1326 int (*activate) (int fd);
1327 int suppress_null_addr;
1330 static struct hwtype unspec_hwtype =
1332 "unspec", "UNSPEC", -1, 0,
1333 UNSPEC_print, NULL, NULL
1336 static struct hwtype loop_hwtype =
1338 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1343 #include <net/if_arp.h>
1344 #include <linux/if_ether.h>
1346 static struct hwtype ether_hwtype;
1348 /* Display an Ethernet address in readable format. */
1349 static char *pr_ether(unsigned char *ptr)
1351 static char buff[64];
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)
1361 /* Input an Ethernet address and convert to binary. */
1362 static int in_ether(char *bufp, struct sockaddr *sap)
1369 sap->sa_family = ether_hwtype.type;
1374 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1379 else if (c >= 'a' && c <= 'f')
1381 else if (c >= 'A' && c <= 'F')
1385 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
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)
1402 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1409 *ptr++ = (unsigned char) (val & 0377);
1412 /* We might get a semicolon here - not required. */
1414 if (i == ETH_ALEN) {
1416 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1425 /* That's it. Any trailing junk? */
1426 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1428 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1434 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1439 #endif /* KEEP_UNUSED */
1442 static struct hwtype ether_hwtype =
1444 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1445 pr_ether, NULL /* UNUSED in_ether */, NULL
1449 #endif /* HAVE_HWETHER */
1454 #include <net/if_arp.h>
1457 /* Start the PPP encapsulation on the file descriptor. */
1458 static int do_ppp(int fd)
1460 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1463 #endif /* KEEP_UNUSED */
1465 static struct hwtype ppp_hwtype =
1467 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1468 NULL, NULL, NULL /* UNUSED do_ppp */, 0
1472 #endif /* HAVE_PPP */
1474 static struct hwtype *hwtypes[] =
1497 #ifdef ARPHRD_IEEE802_TR
1550 static short sVhwinit = 0;
1552 static void hwinit()
1554 loop_hwtype.title = _("Local Loopback");
1555 unspec_hwtype.title = _("UNSPEC");
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");
1564 ether_hwtype.title = _("Ethernet");
1567 ash_hwtype.title = _("Ash");
1570 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1573 hippi_hwtype.title = _("HIPPI");
1576 ax25_hwtype.title = _("AMPR AX.25");
1579 rose_hwtype.title = _("AMPR ROSE");
1582 netrom_hwtype.title = _("AMPR NET/ROM");
1585 x25_hwtype.title = _("generic X.25");
1588 tunnel_hwtype.title = _("IPIP Tunnel");
1591 ppp_hwtype.title = _("Point-to-Point Protocol");
1594 hdlc_hwtype.title = _("(Cisco)-HDLC");
1595 lapb_hwtype.title = _("LAPB");
1598 arcnet_hwtype.title = _("ARCnet");
1601 dlci_hwtype.title = _("Frame Relay DLCI");
1602 frad_hwtype.title = _("Frame Relay Access Device");
1605 sit_hwtype.title = _("IPv6-in-IPv4");
1608 irda_hwtype.title = _("IrLAP");
1611 tr_hwtype.title = _("16/4 Mbps Token Ring");
1612 #ifdef ARPHRD_IEEE802_TR
1613 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1617 ec_hwtype.title = _("Econet");
1621 #endif /* KEEP_UNUSED */
1624 static const char *if_port_text[][4] =
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},
1638 /* Check our hardware type table for this type. */
1639 static struct hwtype *get_hwntype(int type)
1641 struct hwtype **hwp;
1646 #endif /* KEEP_UNUSED */
1649 while (*hwp != NULL) {
1650 if ((*hwp)->type == type)
1657 /* return 1 if address is all zeros */
1658 static int hw_null_address(struct hwtype *hw, void *ap)
1661 unsigned char *address = (unsigned char *)ap;
1662 for (i = 0; i < hw->alen; i++)
1668 static const char TRext[] = "\0\0k\0M";
1670 static void print_bytes_scaled(unsigned long long ull, const char *end)
1672 unsigned long long int_part;
1673 unsigned long frac_part;
1680 for (i=0 ; i<2 ; i++) {
1681 if (int_part >= 1024) {
1682 frac_part = ((int_part % 1024) * 10) / 1024;
1684 ext += 2; /* Kb, Mb */
1688 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1691 static void ife_print(struct interface *ptr)
1696 int can_compress = 0;
1699 static struct aftype *ipxtype = NULL;
1702 static struct aftype *ectype = NULL;
1705 static struct aftype *ddptype = NULL;
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;
1716 ap = get_afntype(ptr->addr.sa_family);
1718 ap = get_afntype(0);
1722 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1725 hw = get_hwntype(hf);
1727 hw = get_hwntype(-1);
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));
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)"));
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));
1751 if (ptr->flags & IFF_BROADCAST) {
1752 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1754 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1759 /* FIXME: should be integrated into interface.c. */
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:"));
1776 printf(_("Global"));
1778 case IPV6_ADDR_LINKLOCAL:
1781 case IPV6_ADDR_SITELOCAL:
1784 case IPV6_ADDR_COMPATv4:
1785 printf(_("Compat"));
1787 case IPV6_ADDR_LOOPBACK:
1791 printf(_("Unknown"));
1801 if (ipxtype == NULL)
1802 ipxtype = get_afntype(AF_IPX);
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));
1821 if (ddptype == NULL)
1822 ddptype = get_afntype(AF_APPLETALK);
1823 if (ddptype != NULL) {
1825 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
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));
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)
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 "));
1869 if (ptr->flags & IFF_DYNAMIC)
1870 printf(_("DYNAMIC "));
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);
1882 /* If needed, display the interface statistics. */
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
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);
1896 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
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);
1904 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1905 if (ptr->tx_queue_len != -1)
1906 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1908 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1909 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1913 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1914 ptr->map.base_addr)) {
1917 printf(_("Interrupt:%d "), ptr->map.irq);
1918 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
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);
1925 printf(_("DMA chan:%x "), ptr->map.dma);
1932 static int do_if_print(struct interface *ife, void *cookie)
1934 int *opt_a = (int *) cookie;
1937 res = do_if_fetch(ife);
1939 if ((ife->flags & IFF_UP) || *opt_a)
1945 static struct interface *lookup_interface(char *name)
1947 struct interface *ife = NULL;
1949 if (if_readlist_proc(name) < 0)
1951 ife = add_interface(name);
1955 /* for ipv4 add/del modes */
1956 static int if_print(char *ifname)
1961 res = for_all_interfaces(do_if_print, &interface_opt_a);
1963 struct interface *ife;
1965 ife = lookup_interface(ifname);
1966 res = do_if_fetch(ife);
1973 int display_interfaces(char *ifname)
1977 /* Create a channel to the NET kernel. */
1978 if ((skfd = sockets_open(0)) < 0) {
1979 perror_msg_and_die("socket");
1982 /* Do we have to show the current setup? */
1983 status = if_print(ifname);