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.9 2002/08/22 19:35:36 bug1 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);
170 static int flag_ax25;
176 static int flag_netrom;
178 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},
205 "ip", "inet", &flag_inet},
208 "ip6", "inet6", &flag_inet6},
212 "ipx", "ipx", &flag_ipx},
216 "appletalk", "ddp", &flag_ddp},
220 "netrom", "netrom", &flag_netrom},
223 "inet", "inet", &flag_inet},
226 "inet6", "inet6", &flag_inet6},
230 "ddp", "ddp", &flag_ddp},
233 "unix", "unix", &flag_unx}, {
234 "tcpip", "inet", &flag_inet},
237 "econet", "ec", &flag_econet},
241 "x25", "x25", &flag_x25},
245 "ash", "ash", &flag_ash},
251 static char afname[256] = "";
252 #endif /* KEEP_UNUSED */
256 /* Display a UNIX domain address. */
257 static char *UNIX_print(unsigned char *ptr)
263 /* Display a UNIX domain address. */
264 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
268 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
269 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
270 return (UNIX_print(sap->sa_data));
274 static struct aftype unix_aftype = {
275 "unix", "UNIX Domain", AF_UNIX, 0,
276 UNIX_print, UNIX_sprint, NULL, NULL,
281 #endif /* HAVE_AFUNIX */
286 static void INET_reserror(char *text)
291 /* Display an Internet socket address. */
292 static char *INET_print(unsigned char *ptr)
294 return (inet_ntoa((*(struct in_addr *) ptr)));
296 #endif /* KEEP_UNUSED */
298 /* Display an Internet socket address. */
299 static char *INET_sprint(struct sockaddr *sap, int numeric)
301 static char buff[128];
303 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
304 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
306 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
307 numeric, 0xffffff00) != 0)
314 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
315 unsigned int netmask)
317 static char buff[128];
319 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
320 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
321 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
322 numeric, netmask) != 0)
327 static int INET_getsock(char *bufp, struct sockaddr *sap)
329 char *sp = bufp, *bp;
332 struct sockaddr_in *sin;
334 sin = (struct sockaddr_in *) sap;
335 sin->sin_family = AF_INET;
340 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
343 if ((*sp >= 'A') && (*sp <= 'F'))
344 bp[i] |= (int) (*sp - 'A') + 10;
345 else if ((*sp >= '0') && (*sp <= '9'))
346 bp[i] |= (int) (*sp - '0');
354 if ((*sp >= 'A') && (*sp <= 'F'))
355 bp[i] |= (int) (*sp - 'A') + 10;
356 else if ((*sp >= '0') && (*sp <= '9'))
357 bp[i] |= (int) (*sp - '0');
363 sin->sin_addr.s_addr = htonl(val);
368 static int INET_input(int type, char *bufp, struct sockaddr *sap)
372 return (INET_getsock(bufp, sap));
374 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
376 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
380 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
382 struct sockaddr_in *mask = (struct sockaddr_in *) m;
386 if ((slash = strchr(adr, '/')) == NULL)
390 prefix = strtoul(slash, &end, 0);
395 sprintf(name, "/%d", prefix);
397 mask->sin_family = AF_INET;
398 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
401 #endif /* KEEP_UNUSED */
403 static struct aftype inet_aftype = {
404 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
405 NULL /* UNUSED INET_print */ , INET_sprint,
406 NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
407 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
408 NULL /* UNUSED INET_getnetmask */ ,
413 #endif /* HAVE_AFINET */
418 static void INET6_reserror(char *text)
423 /* Display an Internet socket address. */
424 static char *INET6_print(unsigned char *ptr)
426 static char name[80];
428 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
431 #endif /* KEEP_UNUSED */
433 /* Display an Internet socket address. */
434 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
435 static char *INET6_sprint(struct sockaddr *sap, int numeric)
437 static char buff[128];
439 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
440 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
442 (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
443 return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
448 static int INET6_getsock(char *bufp, struct sockaddr *sap)
450 struct sockaddr_in6 *sin6;
452 sin6 = (struct sockaddr_in6 *) sap;
453 sin6->sin6_family = AF_INET6;
456 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
462 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
466 return (INET6_getsock(bufp, sap));
468 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
471 #endif /* KEEP_UNUSED */
473 static struct aftype inet6_aftype = {
474 "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
475 NULL /* UNUSED INET6_print */ , INET6_sprint,
476 NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
477 NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
478 NULL /* UNUSED INET6_getnetmask */ ,
483 #endif /* HAVE_AFINET6 */
485 /* Display an UNSPEC address. */
486 static char *UNSPEC_print(unsigned char *ptr)
488 static char buff[sizeof(struct sockaddr) * 3 + 1];
493 for (i = 0; i < sizeof(struct sockaddr); i++) {
494 /* careful -- not every libc's sprintf returns # bytes written */
495 sprintf(pos, "%02X-", (*ptr++ & 0377));
498 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
503 /* Display an UNSPEC socket address. */
504 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
508 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
509 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
510 return (UNSPEC_print(sap->sa_data));
513 static struct aftype unspec_aftype = {
514 "unspec", "UNSPEC", AF_UNSPEC, 0,
515 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
519 static struct aftype *aftypes[] = {
558 static short sVafinit = 0;
562 unspec_aftype.title = _("UNSPEC");
564 unix_aftype.title = _("UNIX Domain");
567 inet_aftype.title = _("DARPA Internet");
570 inet6_aftype.title = _("IPv6");
573 ax25_aftype.title = _("AMPR AX.25");
576 netrom_aftype.title = _("AMPR NET/ROM");
579 ipx_aftype.title = _("Novell IPX");
582 ddp_aftype.title = _("Appletalk DDP");
585 ec_aftype.title = _("Econet");
588 x25_aftype.title = _("CCITT X.25");
591 rose_aftype.title = _("AMPR ROSE");
594 ash_aftype.title = _("Ash");
599 static int aftrans_opt(const char *arg)
601 struct aftrans_t *paft;
605 safe_strncpy(buf, arg, sizeof(buf));
611 tmp2 = strchr(tmp1, ',');
617 for (paft = aftrans; paft->alias; paft++) {
618 if (strcmp(tmp1, paft->alias))
620 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
621 fprintf(stderr, _("Too much address family arguments.\n"));
628 strcat(afname, paft->name);
632 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
641 /* set the default AF list from the program name or a constant value */
642 static void aftrans_def(char *tool, char *argv0, char *dflt)
647 strcpy(afname, dflt);
649 if (!(tmp = strrchr(argv0, '/')))
650 tmp = argv0; /* no slash?! */
654 if (!(buf = strdup(tmp)))
657 if (strlen(tool) >= strlen(tmp)) {
661 tmp = buf + (strlen(tmp) - strlen(tool));
663 if (strcmp(tmp, tool) != 0) {
668 if ((tmp = strchr(buf, '_')))
672 if (aftrans_opt(buf))
678 /* Check our protocol family table for this family. */
679 static struct aftype *get_aftype(const char *name)
686 #endif /* KEEP_UNUSED */
689 while (*afp != NULL) {
690 if (!strcmp((*afp)->name, name))
694 if (strchr(name, ','))
696 _("Please don't supply more than one address family.\n"));
699 #endif /* KEEP_UNUSED */
701 /* Check our protocol family table for this family. */
702 static struct aftype *get_afntype(int af)
709 #endif /* KEEP_UNUSED */
712 while (*afp != NULL) {
713 if ((*afp)->af == af)
720 /* Check our protocol family table for this family and return its socket */
721 static int get_socket_for_af(int af)
728 #endif /* KEEP_UNUSED */
731 while (*afp != NULL) {
732 if ((*afp)->af == af)
740 /* type: 0=all, 1=getroute */
741 static void print_aflist(int type)
750 #endif /* KEEP_UNUSED */
753 while (*afp != NULL) {
754 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
758 if ((count % 3) == 0)
759 fprintf(stderr, count ? "\n " : " ");
763 fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
767 fprintf(stderr, "\n");
769 #endif /* KEEP_UNUSED */
771 struct user_net_device_stats {
772 unsigned long long rx_packets; /* total packets received */
773 unsigned long long tx_packets; /* total packets transmitted */
774 unsigned long long rx_bytes; /* total bytes received */
775 unsigned long long tx_bytes; /* total bytes transmitted */
776 unsigned long rx_errors; /* bad packets received */
777 unsigned long tx_errors; /* packet transmit problems */
778 unsigned long rx_dropped; /* no space in linux buffers */
779 unsigned long tx_dropped; /* no space available in linux */
780 unsigned long rx_multicast; /* multicast packets received */
781 unsigned long rx_compressed;
782 unsigned long tx_compressed;
783 unsigned long collisions;
785 /* detailed rx_errors: */
786 unsigned long rx_length_errors;
787 unsigned long rx_over_errors; /* receiver ring buff overflow */
788 unsigned long rx_crc_errors; /* recved pkt with crc error */
789 unsigned long rx_frame_errors; /* recv'd frame alignment error */
790 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
791 unsigned long rx_missed_errors; /* receiver missed packet */
792 /* detailed tx_errors */
793 unsigned long tx_aborted_errors;
794 unsigned long tx_carrier_errors;
795 unsigned long tx_fifo_errors;
796 unsigned long tx_heartbeat_errors;
797 unsigned long tx_window_errors;
801 struct interface *next, *prev;
802 char name[IFNAMSIZ]; /* interface name */
803 short type; /* if type */
804 short flags; /* various flags */
805 int metric; /* routing metric */
806 int mtu; /* MTU value */
807 int tx_queue_len; /* transmit queue length */
808 struct ifmap map; /* hardware setup */
809 struct sockaddr addr; /* IP address */
810 struct sockaddr dstaddr; /* P-P IP address */
811 struct sockaddr broadaddr; /* IP broadcast address */
812 struct sockaddr netmask; /* IP network mask */
813 struct sockaddr ipxaddr_bb; /* IPX network address */
814 struct sockaddr ipxaddr_sn; /* IPX network address */
815 struct sockaddr ipxaddr_e3; /* IPX network address */
816 struct sockaddr ipxaddr_e2; /* IPX network address */
817 struct sockaddr ddpaddr; /* Appletalk DDP address */
818 struct sockaddr ecaddr; /* Econet address */
827 char hwaddr[32]; /* HW address */
828 int statistics_valid;
829 struct user_net_device_stats stats; /* statistics */
830 int keepalive; /* keepalive value for SLIP */
831 int outfill; /* outfill value for SLIP */
835 int interface_opt_a = 0; /* show all interfaces */
838 static int opt_i = 0; /* show the statistics */
839 static int opt_v = 0; /* debugging output flag */
841 static int addr_family = 0; /* currently selected AF */
842 #endif /* KEEP_UNUSED */
844 static struct interface *int_list, *int_last;
845 static int skfd = -1; /* generic raw socket desc. */
848 static int sockets_open(int family)
852 static int force = -1;
856 if (get_kernel_revision() < KRELEASE(2, 1, 0))
858 if (access("/proc/net", R_OK))
861 for (aft = aftypes; *aft; aft++) {
862 struct aftype *af = *aft;
863 int type = SOCK_DGRAM;
865 if (af->af == AF_UNSPEC)
867 if (family && family != af->af)
873 /* Check some /proc file first to not stress kmod */
874 if (!family && !force && af->flag_file) {
875 if (access(af->flag_file, R_OK))
879 if (af->af == AF_NETROM)
880 type = SOCK_SEQPACKET;
883 if (af->af == AF_X25)
884 type = SOCK_SEQPACKET;
886 af->fd = socket(af->af, type, 0);
891 fprintf(stderr, _("No usable address families found.\n"));
895 /* like strcmp(), but knows about numbers */
896 static int nstrcmp(const char *astr, const char *b)
898 const char *a = astr;
919 return atoi(a) > atoi(b) ? 1 : -1;
924 static struct interface *add_interface(char *name)
926 struct interface *ife, **nextp, *new;
928 for (ife = int_last; ife; ife = ife->prev) {
929 int n = nstrcmp(ife->name, name);
937 safe_strncpy(new->name, name, IFNAMSIZ);
938 nextp = ife ? &ife->next : &int_list;
942 new->next->prev = new;
950 static int if_readconf(void)
958 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
960 skfd2 = get_socket_for_af(AF_INET);
962 fprintf(stderr, _("warning: no inet socket available: %s\n"),
964 /* Try to soldier on with whatever socket we can get hold of. */
965 skfd2 = sockets_open(0);
972 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
973 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
975 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
976 perror("SIOCGIFCONF");
979 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
980 /* assume it overflowed and try again */
988 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
989 add_interface(ifr->ifr_name);
999 static char *get_name(char *name, char *p)
1006 if (*p == ':') { /* could be an alias */
1007 char *dot = p, *dotname = name;
1012 if (*p != ':') { /* it wasn't, backup */
1027 static int get_dev_fields(char *bp, struct interface *ife)
1029 switch (procnetdev_vsn) {
1032 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1033 &ife->stats.rx_bytes,
1034 &ife->stats.rx_packets,
1035 &ife->stats.rx_errors,
1036 &ife->stats.rx_dropped,
1037 &ife->stats.rx_fifo_errors,
1038 &ife->stats.rx_frame_errors,
1039 &ife->stats.rx_compressed,
1040 &ife->stats.rx_multicast,
1041 &ife->stats.tx_bytes,
1042 &ife->stats.tx_packets,
1043 &ife->stats.tx_errors,
1044 &ife->stats.tx_dropped,
1045 &ife->stats.tx_fifo_errors,
1046 &ife->stats.collisions,
1047 &ife->stats.tx_carrier_errors, &ife->stats.tx_compressed);
1050 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1051 &ife->stats.rx_bytes,
1052 &ife->stats.rx_packets,
1053 &ife->stats.rx_errors,
1054 &ife->stats.rx_dropped,
1055 &ife->stats.rx_fifo_errors,
1056 &ife->stats.rx_frame_errors,
1057 &ife->stats.tx_bytes,
1058 &ife->stats.tx_packets,
1059 &ife->stats.tx_errors,
1060 &ife->stats.tx_dropped,
1061 &ife->stats.tx_fifo_errors,
1062 &ife->stats.collisions, &ife->stats.tx_carrier_errors);
1063 ife->stats.rx_multicast = 0;
1066 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1067 &ife->stats.rx_packets,
1068 &ife->stats.rx_errors,
1069 &ife->stats.rx_dropped,
1070 &ife->stats.rx_fifo_errors,
1071 &ife->stats.rx_frame_errors,
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, &ife->stats.tx_carrier_errors);
1077 ife->stats.rx_bytes = 0;
1078 ife->stats.tx_bytes = 0;
1079 ife->stats.rx_multicast = 0;
1085 static inline int procnetdev_version(char *buf)
1087 if (strstr(buf, "compressed"))
1089 if (strstr(buf, "bytes"))
1094 static int if_readlist_proc(char *target)
1096 static int proc_read;
1099 struct interface *ife;
1107 fh = fopen(_PATH_PROCNET_DEV, "r");
1109 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1110 _PATH_PROCNET_DEV, strerror(errno));
1111 return if_readconf();
1113 fgets(buf, sizeof buf, fh); /* eat line */
1114 fgets(buf, sizeof buf, fh);
1116 #if 0 /* pretty, but can't cope with missing fields */
1117 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh, "face", "", /* parsed separately */
1124 "compressed", "%lu",
1132 "carrier", "%lu", "compressed", "%lu", NULL);
1136 procnetdev_vsn = procnetdev_version(buf);
1140 while (fgets(buf, sizeof buf, fh)) {
1141 char *s, name[IFNAMSIZ];
1143 s = get_name(name, buf);
1144 ife = add_interface(name);
1145 get_dev_fields(s, ife);
1146 ife->statistics_valid = 1;
1147 if (target && !strcmp(target, name))
1151 perror(_PATH_PROCNET_DEV);
1162 static int if_readlist(void)
1164 int err = if_readlist_proc(NULL);
1167 err = if_readconf();
1171 static int for_all_interfaces(int (*doit) (struct interface *, void *),
1174 struct interface *ife;
1176 if (!int_list && (if_readlist() < 0))
1178 for (ife = int_list; ife; ife = ife->next) {
1179 int err = doit(ife, cookie);
1187 /* Support for fetching an IPX address */
1190 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1192 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1193 return ioctl(sock, SIOCGIFADDR, ifr);
1198 /* Fetch the interface configuration from the kernel. */
1199 static int if_fetch(struct interface *ife)
1203 char *ifname = ife->name;
1205 strcpy(ifr.ifr_name, ifname);
1206 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1208 ife->flags = ifr.ifr_flags;
1210 strcpy(ifr.ifr_name, ifname);
1211 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1212 memset(ife->hwaddr, 0, 32);
1214 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1216 ife->type = ifr.ifr_hwaddr.sa_family;
1218 strcpy(ifr.ifr_name, ifname);
1219 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1222 ife->metric = ifr.ifr_metric;
1224 strcpy(ifr.ifr_name, ifname);
1225 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1228 ife->mtu = ifr.ifr_mtu;
1231 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1232 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1233 ife->type == ARPHRD_ADAPT) {
1235 strcpy(ifr.ifr_name, ifname);
1236 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1239 ife->outfill = (unsigned int) ifr.ifr_data;
1241 #ifdef SIOCGKEEPALIVE
1242 strcpy(ifr.ifr_name, ifname);
1243 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1246 ife->keepalive = (unsigned int) ifr.ifr_data;
1251 strcpy(ifr.ifr_name, ifname);
1252 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1253 memset(&ife->map, 0, sizeof(struct ifmap));
1255 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1257 strcpy(ifr.ifr_name, ifname);
1258 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1259 memset(&ife->map, 0, sizeof(struct ifmap));
1261 ife->map = ifr.ifr_map;
1263 #ifdef HAVE_TXQUEUELEN
1264 strcpy(ifr.ifr_name, ifname);
1265 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1266 ife->tx_queue_len = -1; /* unknown value */
1268 ife->tx_queue_len = ifr.ifr_qlen;
1270 ife->tx_queue_len = -1; /* unknown value */
1275 fd = get_socket_for_af(AF_INET);
1277 strcpy(ifr.ifr_name, ifname);
1278 ifr.ifr_addr.sa_family = AF_INET;
1279 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1281 ife->addr = ifr.ifr_addr;
1282 strcpy(ifr.ifr_name, ifname);
1283 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1284 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1286 ife->dstaddr = ifr.ifr_dstaddr;
1288 strcpy(ifr.ifr_name, ifname);
1289 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1290 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1292 ife->broadaddr = ifr.ifr_broadaddr;
1294 strcpy(ifr.ifr_name, ifname);
1295 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1296 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1298 ife->netmask = ifr.ifr_netmask;
1300 memset(&ife->addr, 0, sizeof(struct sockaddr));
1305 /* DDP address maybe ? */
1306 fd = get_socket_for_af(AF_APPLETALK);
1308 strcpy(ifr.ifr_name, ifname);
1309 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1310 ife->ddpaddr = ifr.ifr_addr;
1317 /* Look for IPX addresses with all framing types */
1318 fd = get_socket_for_af(AF_IPX);
1320 strcpy(ifr.ifr_name, ifname);
1321 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1322 ife->has_ipx_bb = 1;
1323 ife->ipxaddr_bb = ifr.ifr_addr;
1325 strcpy(ifr.ifr_name, ifname);
1326 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1327 ife->has_ipx_sn = 1;
1328 ife->ipxaddr_sn = ifr.ifr_addr;
1330 strcpy(ifr.ifr_name, ifname);
1331 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1332 ife->has_ipx_e3 = 1;
1333 ife->ipxaddr_e3 = ifr.ifr_addr;
1335 strcpy(ifr.ifr_name, ifname);
1336 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1337 ife->has_ipx_e2 = 1;
1338 ife->ipxaddr_e2 = ifr.ifr_addr;
1344 /* Econet address maybe? */
1345 fd = get_socket_for_af(AF_ECONET);
1347 strcpy(ifr.ifr_name, ifname);
1348 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1349 ife->ecaddr = ifr.ifr_addr;
1350 ife->has_econet = 1;
1359 static int do_if_fetch(struct interface *ife)
1361 if (if_fetch(ife) < 0) {
1364 if (errno == ENODEV) {
1365 /* Give better error message for this case. */
1366 errmsg = _("Device not found");
1368 errmsg = strerror(errno);
1370 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1377 /* This structure defines hardware protocols and their handlers. */
1383 char *(*print) (unsigned char *);
1384 int (*input) (char *, struct sockaddr *);
1385 int (*activate) (int fd);
1386 int suppress_null_addr;
1389 static struct hwtype unspec_hwtype = {
1390 "unspec", "UNSPEC", -1, 0,
1391 UNSPEC_print, NULL, NULL
1394 static struct hwtype loop_hwtype = {
1395 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1400 #include <net/if_arp.h>
1401 #include <linux/if_ether.h>
1403 /* Display an Ethernet address in readable format. */
1404 static char *pr_ether(unsigned char *ptr)
1406 static char buff[64];
1408 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1409 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1410 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1416 /* Input an Ethernet address and convert to binary. */
1417 static int in_ether(char *bufp, struct sockaddr *sap)
1424 sap->sa_family = ether_hwtype.type;
1429 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1434 else if (c >= 'a' && c <= 'f')
1436 else if (c >= 'A' && c <= 'F')
1440 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"),
1450 else if (c >= 'a' && c <= 'f')
1451 val |= c - 'a' + 10;
1452 else if (c >= 'A' && c <= 'F')
1453 val |= c - 'A' + 10;
1454 else if (c == ':' || c == 0)
1458 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"),
1466 *ptr++ = (unsigned char) (val & 0377);
1469 /* We might get a semicolon here - not required. */
1471 if (i == ETH_ALEN) {
1473 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1482 /* That's it. Any trailing junk? */
1483 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1485 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1491 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1496 #endif /* KEEP_UNUSED */
1499 static struct hwtype ether_hwtype = {
1500 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1501 pr_ether, NULL /* UNUSED in_ether */ , NULL
1505 #endif /* HAVE_HWETHER */
1510 #include <net/if_arp.h>
1513 /* Start the PPP encapsulation on the file descriptor. */
1514 static int do_ppp(int fd)
1516 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1519 #endif /* KEEP_UNUSED */
1521 static struct hwtype ppp_hwtype = {
1522 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1523 NULL, NULL, NULL /* UNUSED do_ppp */ , 0
1527 #endif /* HAVE_PPP */
1529 static struct hwtype *hwtypes[] = {
1551 #ifdef ARPHRD_IEEE802_TR
1604 static short sVhwinit = 0;
1606 static void hwinit()
1608 loop_hwtype.title = _("Local Loopback");
1609 unspec_hwtype.title = _("UNSPEC");
1611 slip_hwtype.title = _("Serial Line IP");
1612 cslip_hwtype.title = _("VJ Serial Line IP");
1613 slip6_hwtype.title = _("6-bit Serial Line IP");
1614 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1615 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1618 ether_hwtype.title = _("Ethernet");
1621 ash_hwtype.title = _("Ash");
1624 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1627 hippi_hwtype.title = _("HIPPI");
1630 ax25_hwtype.title = _("AMPR AX.25");
1633 rose_hwtype.title = _("AMPR ROSE");
1636 netrom_hwtype.title = _("AMPR NET/ROM");
1639 x25_hwtype.title = _("generic X.25");
1642 tunnel_hwtype.title = _("IPIP Tunnel");
1645 ppp_hwtype.title = _("Point-to-Point Protocol");
1648 hdlc_hwtype.title = _("(Cisco)-HDLC");
1649 lapb_hwtype.title = _("LAPB");
1652 arcnet_hwtype.title = _("ARCnet");
1655 dlci_hwtype.title = _("Frame Relay DLCI");
1656 frad_hwtype.title = _("Frame Relay Access Device");
1659 sit_hwtype.title = _("IPv6-in-IPv4");
1662 irda_hwtype.title = _("IrLAP");
1665 tr_hwtype.title = _("16/4 Mbps Token Ring");
1666 #ifdef ARPHRD_IEEE802_TR
1667 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
1671 ec_hwtype.title = _("Econet");
1675 #endif /* KEEP_UNUSED */
1678 static const char *if_port_text[][4] = {
1679 /* Keep in step with <linux/netdevice.h> */
1680 {"unknown", NULL, NULL, NULL},
1681 {"10base2", "bnc", "coax", NULL},
1682 {"10baseT", "utp", "tpe", NULL},
1683 {"AUI", "thick", "db15", NULL},
1684 {"100baseT", NULL, NULL, NULL},
1685 {"100baseTX", NULL, NULL, NULL},
1686 {"100baseFX", NULL, NULL, NULL},
1687 {NULL, NULL, NULL, NULL},
1691 /* Check our hardware type table for this type. */
1692 static struct hwtype *get_hwntype(int type)
1694 struct hwtype **hwp;
1699 #endif /* KEEP_UNUSED */
1702 while (*hwp != NULL) {
1703 if ((*hwp)->type == type)
1710 /* return 1 if address is all zeros */
1711 static int hw_null_address(struct hwtype *hw, void *ap)
1714 unsigned char *address = (unsigned char *) ap;
1716 for (i = 0; i < hw->alen; i++)
1722 static const char TRext[] = "\0\0k\0M";
1724 static void print_bytes_scaled(unsigned long long ull, const char *end)
1726 unsigned long long int_part;
1727 unsigned long frac_part;
1734 for (i = 0; i < 2; i++) {
1735 if (int_part >= 1024) {
1736 frac_part = ((int_part % 1024) * 10) / 1024;
1738 ext += 2; /* Kb, Mb */
1742 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1745 static void ife_print(struct interface *ptr)
1750 int can_compress = 0;
1753 static struct aftype *ipxtype = NULL;
1756 static struct aftype *ectype = NULL;
1759 static struct aftype *ddptype = NULL;
1763 char addr6[40], devname[20];
1764 struct sockaddr_in6 sap;
1765 int plen, scope, dad_status, if_idx;
1769 ap = get_afntype(ptr->addr.sa_family);
1771 ap = get_afntype(0);
1775 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1778 hw = get_hwntype(hf);
1780 hw = get_hwntype(-1);
1782 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1783 /* For some hardware types (eg Ash, ATM) we don't print the
1784 hardware address if it's null. */
1785 if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
1786 hw->suppress_null_addr)))
1787 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1789 if (ptr->flags & IFF_PORTSEL) {
1790 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1791 if (ptr->flags & IFF_AUTOMEDIA)
1792 printf(_("(auto)"));
1799 printf(_(" %s addr:%s "), ap->name,
1800 ap->sprint(&ptr->addr, 1));
1801 if (ptr->flags & IFF_POINTOPOINT) {
1802 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1804 if (ptr->flags & IFF_BROADCAST) {
1805 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1807 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1813 #define IPV6_ADDR_ANY 0x0000U
1815 #define IPV6_ADDR_UNICAST 0x0001U
1816 #define IPV6_ADDR_MULTICAST 0x0002U
1817 #define IPV6_ADDR_ANYCAST 0x0004U
1819 #define IPV6_ADDR_LOOPBACK 0x0010U
1820 #define IPV6_ADDR_LINKLOCAL 0x0020U
1821 #define IPV6_ADDR_SITELOCAL 0x0040U
1823 #define IPV6_ADDR_COMPATv4 0x0080U
1825 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
1827 #define IPV6_ADDR_MAPPED 0x1000U
1828 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
1830 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1832 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1833 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
1834 addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
1835 &dad_status, devname) != EOF) {
1836 if (!strcmp(devname, ptr->name)) {
1837 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1838 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1839 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1840 inet_pton(AF_INET6, addr6,
1841 (struct sockaddr *) &sap.sin6_addr);
1842 sap.sin6_family = AF_INET6;
1843 printf(_(" inet6 addr: %s/%d"),
1844 inet6_aftype.sprint((struct sockaddr *) &sap, 1),
1846 printf(_(" Scope:"));
1847 switch (scope & IPV6_ADDR_SCOPE_MASK) {
1849 printf(_("Global"));
1851 case IPV6_ADDR_LINKLOCAL:
1854 case IPV6_ADDR_SITELOCAL:
1857 case IPV6_ADDR_COMPATv4:
1858 printf(_("Compat"));
1860 case IPV6_ADDR_LOOPBACK:
1864 printf(_("Unknown"));
1874 if (ipxtype == NULL)
1875 ipxtype = get_afntype(AF_IPX);
1877 if (ipxtype != NULL) {
1878 if (ptr->has_ipx_bb)
1879 printf(_(" IPX/Ethernet II addr:%s\n"),
1880 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1881 if (ptr->has_ipx_sn)
1882 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1883 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1884 if (ptr->has_ipx_e2)
1885 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1886 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1887 if (ptr->has_ipx_e3)
1888 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1889 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1894 if (ddptype == NULL)
1895 ddptype = get_afntype(AF_APPLETALK);
1896 if (ddptype != NULL) {
1898 printf(_(" EtherTalk Phase 2 addr:%s\n"),
1899 ddptype->sprint(&ptr->ddpaddr, 1));
1905 ectype = get_afntype(AF_ECONET);
1906 if (ectype != NULL) {
1907 if (ptr->has_econet)
1908 printf(_(" econet addr:%s\n"),
1909 ectype->sprint(&ptr->ecaddr, 1));
1914 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1915 if (ptr->flags == 0)
1916 printf(_("[NO FLAGS] "));
1917 if (ptr->flags & IFF_UP)
1919 if (ptr->flags & IFF_BROADCAST)
1920 printf(_("BROADCAST "));
1921 if (ptr->flags & IFF_DEBUG)
1922 printf(_("DEBUG "));
1923 if (ptr->flags & IFF_LOOPBACK)
1924 printf(_("LOOPBACK "));
1925 if (ptr->flags & IFF_POINTOPOINT)
1926 printf(_("POINTOPOINT "));
1927 if (ptr->flags & IFF_NOTRAILERS)
1928 printf(_("NOTRAILERS "));
1929 if (ptr->flags & IFF_RUNNING)
1930 printf(_("RUNNING "));
1931 if (ptr->flags & IFF_NOARP)
1932 printf(_("NOARP "));
1933 if (ptr->flags & IFF_PROMISC)
1934 printf(_("PROMISC "));
1935 if (ptr->flags & IFF_ALLMULTI)
1936 printf(_("ALLMULTI "));
1937 if (ptr->flags & IFF_SLAVE)
1938 printf(_("SLAVE "));
1939 if (ptr->flags & IFF_MASTER)
1940 printf(_("MASTER "));
1941 if (ptr->flags & IFF_MULTICAST)
1942 printf(_("MULTICAST "));
1944 if (ptr->flags & IFF_DYNAMIC)
1945 printf(_("DYNAMIC "));
1947 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1948 printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
1949 #ifdef SIOCSKEEPALIVE
1950 if (ptr->outfill || ptr->keepalive)
1951 printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
1955 /* If needed, display the interface statistics. */
1957 if (ptr->statistics_valid) {
1958 /* XXX: statistics are currently only printed for the primary address,
1959 * not for the aliases, although strictly speaking they're shared
1965 ("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1966 ptr->stats.rx_packets, ptr->stats.rx_errors,
1967 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1968 ptr->stats.rx_frame_errors);
1970 printf(_(" compressed:%lu\n"),
1971 ptr->stats.rx_compressed);
1974 ("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1975 ptr->stats.tx_packets, ptr->stats.tx_errors,
1976 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1977 ptr->stats.tx_carrier_errors);
1978 printf(_(" collisions:%lu "), ptr->stats.collisions);
1980 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1981 if (ptr->tx_queue_len != -1)
1982 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1984 print_bytes_scaled(ptr->stats.rx_bytes, " T");
1985 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
1989 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
1990 ptr->map.base_addr)) {
1993 printf(_("Interrupt:%d "), ptr->map.irq);
1994 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
1996 printf(_("Base address:0x%lx "),
1997 (unsigned long) ptr->map.base_addr);
1998 if (ptr->map.mem_start) {
1999 printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
2003 printf(_("DMA chan:%x "), ptr->map.dma);
2010 static int do_if_print(struct interface *ife, void *cookie)
2012 int *opt_a = (int *) cookie;
2015 res = do_if_fetch(ife);
2017 if ((ife->flags & IFF_UP) || *opt_a)
2023 static struct interface *lookup_interface(char *name)
2025 struct interface *ife = NULL;
2027 if (if_readlist_proc(name) < 0)
2029 ife = add_interface(name);
2033 /* for ipv4 add/del modes */
2034 static int if_print(char *ifname)
2039 res = for_all_interfaces(do_if_print, &interface_opt_a);
2041 struct interface *ife;
2043 ife = lookup_interface(ifname);
2044 res = do_if_fetch(ife);
2051 int display_interfaces(char *ifname)
2055 /* Create a channel to the NET kernel. */
2056 if ((skfd = sockets_open(0)) < 0) {
2057 perror_msg_and_die("socket");
2060 /* Do we have to show the current setup? */
2061 status = if_print(ifname);