2 * ifconfig This file contains an implementation of the command
3 * that either displays or sets the characteristics of
4 * one or more of the system's networking interfaces.
6 * Version: $Id: interface.c,v 1.1 2001/04/19 16:55:27 andersen Exp $
8 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
9 * and others. Copyright 1993 MicroWalt Corporation
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * Patched to support 'add' and 'del' keywords for INET(4) addresses
18 * by Mrs. Brisby <mrs.brisby@nimh.org>
20 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21 * - gettext instead of catgets for i18n
22 * 10/1998 - Andi Kleen. Use interface list primitives.
23 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
24 * (default AF was wrong)
25 * stolen from net-tools-1.59 and stripped down for busybox by
26 * Erik Andersen <andersee@debian.org>
30 * Heavily modified by Manuel Novoa III Mar 12, 2001
32 * Pruned unused code using KEEP_UNUSED define.
33 * Added print_bytes_scaled function to reduce code size.
34 * Added some (potentially) missing defines.
35 * Improved display support for -a and for a named interface.
38 /* #define KEEP_UNUSED */
57 * Device Hardware types.
60 #define HAVE_HWETHER 1
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
69 #include <netinet/in.h>
71 #include <net/if_arp.h>
80 #include <netinet/in.h>
81 #include <arpa/inet.h>
82 #include <arpa/nameser.h>
85 #define _PATH_PROCNET_DEV "/proc/net/dev"
86 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
87 #define KRELEASE(maj,min,patch) ((maj) * 10000 + (min)*1000 + (patch))
89 static int procnetdev_vsn = 1;
92 /* Ugh. But libc5 doesn't provide POSIX types. */
93 #include <asm/types.h>
97 #include <linux/if_slip.h>
104 * This is in linux/include/net/ipv6.h.
108 struct in6_addr ifr6_addr;
109 __u32 ifr6_prefixlen;
110 unsigned int ifr6_ifindex;
115 #endif /* HAVE_AFINET6 */
118 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
119 #include <netipx/ipx.h>
125 #include "net-support.h"
126 #include "pathnames.h"
129 #include "interface.h"
134 /* Defines for glibc2.0 users. */
135 #ifndef SIOCSIFTXQLEN
136 #define SIOCSIFTXQLEN 0x8943
137 #define SIOCGIFTXQLEN 0x8942
140 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
142 #define ifr_qlen ifr_ifru.ifru_mtu
145 #ifndef HAVE_TXQUEUELEN
146 #define HAVE_TXQUEUELEN 1
150 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
153 /* This structure defines protocol families and their handlers. */
159 char *(*print) (unsigned char *);
160 char *(*sprint) (struct sockaddr *, int numeric);
161 int (*input) (int type, char *bufp, struct sockaddr *);
162 void (*herror) (char *text);
163 int (*rprint) (int options);
164 int (*rinput) (int typ, int ext, char **argv);
167 int (*getmask) (char *src, struct sockaddr * mask, char *name);
173 static struct aftype *aftypes[];
179 static int flag_ax25;
181 static int flag_netrom;
182 static int flag_inet;
183 static int flag_inet6;
184 static int flag_econet;
185 static int flag_x25 = 0;
189 static struct aftrans_t {
196 "ax25", "ax25", &flag_ax25
199 "ip", "inet", &flag_inet
202 "ip6", "inet6", &flag_inet6
205 "ipx", "ipx", &flag_ipx
208 "appletalk", "ddp", &flag_ddp
211 "netrom", "netrom", &flag_netrom
214 "inet", "inet", &flag_inet
217 "inet6", "inet6", &flag_inet6
220 "ddp", "ddp", &flag_ddp
223 "unix", "unix", &flag_unx
226 "tcpip", "inet", &flag_inet
229 "econet", "ec", &flag_econet
232 "x25", "x25", &flag_x25
235 "ash", "ash", &flag_ash
242 static char afname[256] = "";
243 #endif /* KEEP_UNUSED */
247 /* Display a UNIX domain address. */
248 static char *UNIX_print(unsigned char *ptr)
254 /* Display a UNIX domain address. */
255 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
259 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
260 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
261 return (UNIX_print(sap->sa_data));
265 static struct aftype unix_aftype =
267 "unix", "UNIX Domain", AF_UNIX, 0,
268 UNIX_print, UNIX_sprint, NULL, NULL,
273 #endif /* HAVE_AFUNIX */
278 extern int h_errno; /* some netdb.h versions don't export this */
283 struct sockaddr_in addr;
289 static struct addr *INET_nn = NULL; /* addr-to-name cache */
292 static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
298 sin->sin_family = AF_INET;
301 /* Default is special, meaning 0.0.0.0. */
302 if (!strcmp(name, "default")) {
303 sin->sin_addr.s_addr = INADDR_ANY;
306 /* Look to see if it's a dotted quad. */
307 if (inet_aton(name, &sin->sin_addr)) {
310 /* If we expect this to be a hostname, try hostname database first */
312 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
315 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
316 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
317 sizeof(struct in_addr));
320 /* Try the NETWORKS database to see if this is a known network. */
322 fprintf (stderr, "getnetbyname (%s)\n", name);
324 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
325 sin->sin_addr.s_addr = htonl(np->n_net);
329 /* Don't try again */
335 _res.options |= RES_DEBUG;
339 fprintf (stderr, "gethostbyname (%s)\n", name);
341 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
345 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
346 sizeof(struct in_addr));
350 #endif /* KEEP_UNUSED */
352 /* numeric: & 0x8000: default instead of *,
353 * & 0x4000: host instead of net,
354 * & 0x0fff: don't resolve
356 static int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
357 int numeric, unsigned int netmask)
362 unsigned long ad, host_ad;
366 if (s_in->sin_family != AF_INET) {
368 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
370 errno = EAFNOSUPPORT;
373 ad = (unsigned long) s_in->sin_addr.s_addr;
375 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
377 if (ad == INADDR_ANY) {
378 if ((numeric & 0x0FFF) == 0) {
379 if (numeric & 0x8000)
380 safe_strncpy(name, "default", len);
382 safe_strncpy(name, "*", len);
386 if (numeric & 0x0FFF) {
387 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
391 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
398 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
399 safe_strncpy(name, pn->name, len);
401 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
413 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
415 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
417 safe_strncpy(name, ent->h_name, len);
420 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
422 np = getnetbyaddr(host_ad, AF_INET);
424 safe_strncpy(name, np->n_name, len);
426 if ((ent == NULL) && (np == NULL))
427 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
428 pn = (struct addr *) xmalloc(sizeof(struct addr));
432 pn->name = (char *) xmalloc(strlen(name) + 1);
433 strcpy(pn->name, name);
440 static void INET_reserror(char *text)
445 /* Display an Internet socket address. */
446 static char *INET_print(unsigned char *ptr)
448 return (inet_ntoa((*(struct in_addr *) ptr)));
450 #endif /* KEEP_UNUSED */
452 /* Display an Internet socket address. */
453 static char *INET_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));
460 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
461 numeric, 0xffffff00) != 0)
468 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
469 unsigned int netmask)
471 static char buff[128];
473 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
474 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
475 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
476 numeric, netmask) != 0)
481 static int INET_getsock(char *bufp, struct sockaddr *sap)
483 char *sp = bufp, *bp;
486 struct sockaddr_in *sin;
488 sin = (struct sockaddr_in *) sap;
489 sin->sin_family = AF_INET;
494 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
497 if ((*sp >= 'A') && (*sp <= 'F'))
498 bp[i] |= (int) (*sp - 'A') + 10;
499 else if ((*sp >= '0') && (*sp <= '9'))
500 bp[i] |= (int) (*sp - '0');
508 if ((*sp >= 'A') && (*sp <= 'F'))
509 bp[i] |= (int) (*sp - 'A') + 10;
510 else if ((*sp >= '0') && (*sp <= '9'))
511 bp[i] |= (int) (*sp - '0');
517 sin->sin_addr.s_addr = htonl(val);
522 static int INET_input(int type, char *bufp, struct sockaddr *sap)
526 return (INET_getsock(bufp, sap));
528 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
530 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
534 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
536 struct sockaddr_in *mask = (struct sockaddr_in *) m;
540 if ((slash = strchr(adr, '/')) == NULL)
544 prefix = strtoul(slash, &end, 0);
549 sprintf(name, "/%d", prefix);
551 mask->sin_family = AF_INET;
552 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
555 #endif /* KEEP_UNUSED */
557 static struct aftype inet_aftype =
559 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
560 NULL /* UNUSED INET_print */, INET_sprint,
561 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
562 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
563 NULL /* UNUSED INET_getnetmask */,
568 #endif /* HAVE_AFINET */
570 /* Display an UNSPEC address. */
571 static char *UNSPEC_print(unsigned char *ptr)
573 static char buff[sizeof(struct sockaddr)*3+1];
578 for (i = 0; i < sizeof(struct sockaddr); i++) {
579 /* careful -- not every libc's sprintf returns # bytes written */
580 sprintf(pos, "%02X-", (*ptr++ & 0377));
583 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
588 /* Display an UNSPEC socket address. */
589 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
593 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
594 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
595 return (UNSPEC_print(sap->sa_data));
598 static struct aftype unspec_aftype =
600 "unspec", "UNSPEC", AF_UNSPEC, 0,
601 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
605 static struct aftype *aftypes[] =
645 static short sVafinit = 0;
649 unspec_aftype.title = _("UNSPEC");
651 unix_aftype.title = _("UNIX Domain");
654 inet_aftype.title = _("DARPA Internet");
657 inet6_aftype.title = _("IPv6");
660 ax25_aftype.title = _("AMPR AX.25");
663 netrom_aftype.title = _("AMPR NET/ROM");
666 ipx_aftype.title = _("Novell IPX");
669 ddp_aftype.title = _("Appletalk DDP");
672 ec_aftype.title = _("Econet");
675 x25_aftype.title = _("CCITT X.25");
678 rose_aftype.title = _("AMPR ROSE");
681 ash_aftype.title = _("Ash");
686 static int aftrans_opt(const char *arg)
688 struct aftrans_t *paft;
692 safe_strncpy(buf, arg, sizeof(buf));
698 tmp2 = index(tmp1, ',');
704 for (paft = aftrans; paft->alias; paft++) {
705 if (strcmp(tmp1, paft->alias))
707 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
708 fprintf(stderr, _("Too much address family arguments.\n"));
715 strcat(afname, paft->name);
719 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
728 /* set the default AF list from the program name or a constant value */
729 static void aftrans_def(char *tool, char *argv0, char *dflt)
734 strcpy(afname, dflt);
736 if (!(tmp = strrchr(argv0, '/')))
737 tmp = argv0; /* no slash?! */
741 if (!(buf = strdup(tmp)))
744 if (strlen(tool) >= strlen(tmp)) {
748 tmp = buf + (strlen(tmp) - strlen(tool));
750 if (strcmp(tmp, tool) != 0) {
755 if ((tmp = strchr(buf, '_')))
759 if (aftrans_opt(buf))
765 /* Check our protocol family table for this family. */
766 static struct aftype *get_aftype(const char *name)
773 #endif /* KEEP_UNUSED */
776 while (*afp != NULL) {
777 if (!strcmp((*afp)->name, name))
781 if (index(name, ','))
782 fprintf(stderr, _("Please don't supply more than one address family.\n"));
785 #endif /* KEEP_UNUSED */
787 /* Check our protocol family table for this family. */
788 static struct aftype *get_afntype(int af)
795 #endif /* KEEP_UNUSED */
798 while (*afp != NULL) {
799 if ((*afp)->af == af)
806 /* Check our protocol family table for this family and return its socket */
807 static int get_socket_for_af(int af)
814 #endif /* KEEP_UNUSED */
817 while (*afp != NULL) {
818 if ((*afp)->af == af)
826 /* type: 0=all, 1=getroute */
827 static void print_aflist(int type) {
835 #endif /* KEEP_UNUSED */
838 while (*afp != NULL) {
839 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
842 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
843 txt = (*afp)->name; if (!txt) txt = "..";
844 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
848 fprintf(stderr,"\n");
850 #endif /* KEEP_UNUSED */
852 struct user_net_device_stats {
853 unsigned long long rx_packets; /* total packets received */
854 unsigned long long tx_packets; /* total packets transmitted */
855 unsigned long long rx_bytes; /* total bytes received */
856 unsigned long long tx_bytes; /* total bytes transmitted */
857 unsigned long rx_errors; /* bad packets received */
858 unsigned long tx_errors; /* packet transmit problems */
859 unsigned long rx_dropped; /* no space in linux buffers */
860 unsigned long tx_dropped; /* no space available in linux */
861 unsigned long rx_multicast; /* multicast packets received */
862 unsigned long rx_compressed;
863 unsigned long tx_compressed;
864 unsigned long collisions;
866 /* detailed rx_errors: */
867 unsigned long rx_length_errors;
868 unsigned long rx_over_errors; /* receiver ring buff overflow */
869 unsigned long rx_crc_errors; /* recved pkt with crc error */
870 unsigned long rx_frame_errors; /* recv'd frame alignment error */
871 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
872 unsigned long rx_missed_errors; /* receiver missed packet */
873 /* detailed tx_errors */
874 unsigned long tx_aborted_errors;
875 unsigned long tx_carrier_errors;
876 unsigned long tx_fifo_errors;
877 unsigned long tx_heartbeat_errors;
878 unsigned long tx_window_errors;
882 struct interface *next, *prev;
883 char name[IFNAMSIZ]; /* interface name */
884 short type; /* if type */
885 short flags; /* various flags */
886 int metric; /* routing metric */
887 int mtu; /* MTU value */
888 int tx_queue_len; /* transmit queue length */
889 struct ifmap map; /* hardware setup */
890 struct sockaddr addr; /* IP address */
891 struct sockaddr dstaddr; /* P-P IP address */
892 struct sockaddr broadaddr; /* IP broadcast address */
893 struct sockaddr netmask; /* IP network mask */
894 struct sockaddr ipxaddr_bb; /* IPX network address */
895 struct sockaddr ipxaddr_sn; /* IPX network address */
896 struct sockaddr ipxaddr_e3; /* IPX network address */
897 struct sockaddr ipxaddr_e2; /* IPX network address */
898 struct sockaddr ddpaddr; /* Appletalk DDP address */
899 struct sockaddr ecaddr; /* Econet address */
908 char hwaddr[32]; /* HW address */
909 int statistics_valid;
910 struct user_net_device_stats stats; /* statistics */
911 int keepalive; /* keepalive value for SLIP */
912 int outfill; /* outfill value for SLIP */
916 int interface_opt_a = 0; /* show all interfaces */
919 static int opt_i = 0; /* show the statistics */
920 static int opt_v = 0; /* debugging output flag */
922 static int addr_family = 0; /* currently selected AF */
923 #endif /* KEEP_UNUSED */
925 static struct interface *int_list, *int_last;
926 static int skfd = -1; /* generic raw socket desc. */
929 static int sockets_open(int family)
933 static int force = -1;
937 if (get_kernel_revision() < KRELEASE(2, 1, 0))
939 if (access("/proc/net", R_OK))
942 for (aft = aftypes; *aft; aft++) {
943 struct aftype *af = *aft;
944 int type = SOCK_DGRAM;
945 if (af->af == AF_UNSPEC)
947 if (family && family != af->af)
953 /* Check some /proc file first to not stress kmod */
954 if (!family && !force && af->flag_file) {
955 if (access(af->flag_file, R_OK))
959 if (af->af == AF_NETROM)
960 type = SOCK_SEQPACKET;
963 if (af->af == AF_X25)
964 type = SOCK_SEQPACKET;
966 af->fd = socket(af->af, type, 0);
971 fprintf(stderr, _("No usable address families found.\n"));
975 /* like strcmp(), but knows about numbers */
976 static int nstrcmp(const char *astr, const char *b)
978 const char *a = astr;
999 return atoi(a) > atoi(b) ? 1 : -1;
1004 static struct interface *add_interface(char *name)
1006 struct interface *ife, **nextp, *new;
1008 for (ife = int_last; ife; ife = ife->prev) {
1009 int n = nstrcmp(ife->name, name);
1016 safe_strncpy(new->name, name, IFNAMSIZ);
1017 nextp = ife ? &ife->next : &int_list;
1021 new->next->prev = new;
1029 static int if_readconf(void)
1035 /* XXX Should this re-use the global skfd? */
1038 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1040 skfd2 = get_socket_for_af(AF_INET);
1042 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1044 /* Try to soldier on with whatever socket we can get hold of. */
1045 skfd2 = sockets_open(0);
1052 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1053 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1055 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
1056 perror("SIOCGIFCONF");
1059 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1060 /* assume it overflowed and try again */
1068 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1069 add_interface(ifr->ifr_name);
1079 static char *get_name(char *name, char *p)
1086 if (*p == ':') { /* could be an alias */
1087 char *dot = p, *dotname = name;
1091 if (*p != ':') { /* it wasn't, backup */
1106 static int get_dev_fields(char *bp, struct interface *ife)
1108 switch (procnetdev_vsn) {
1111 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1112 &ife->stats.rx_bytes,
1113 &ife->stats.rx_packets,
1114 &ife->stats.rx_errors,
1115 &ife->stats.rx_dropped,
1116 &ife->stats.rx_fifo_errors,
1117 &ife->stats.rx_frame_errors,
1118 &ife->stats.rx_compressed,
1119 &ife->stats.rx_multicast,
1121 &ife->stats.tx_bytes,
1122 &ife->stats.tx_packets,
1123 &ife->stats.tx_errors,
1124 &ife->stats.tx_dropped,
1125 &ife->stats.tx_fifo_errors,
1126 &ife->stats.collisions,
1127 &ife->stats.tx_carrier_errors,
1128 &ife->stats.tx_compressed);
1131 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1132 &ife->stats.rx_bytes,
1133 &ife->stats.rx_packets,
1134 &ife->stats.rx_errors,
1135 &ife->stats.rx_dropped,
1136 &ife->stats.rx_fifo_errors,
1137 &ife->stats.rx_frame_errors,
1139 &ife->stats.tx_bytes,
1140 &ife->stats.tx_packets,
1141 &ife->stats.tx_errors,
1142 &ife->stats.tx_dropped,
1143 &ife->stats.tx_fifo_errors,
1144 &ife->stats.collisions,
1145 &ife->stats.tx_carrier_errors);
1146 ife->stats.rx_multicast = 0;
1149 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1150 &ife->stats.rx_packets,
1151 &ife->stats.rx_errors,
1152 &ife->stats.rx_dropped,
1153 &ife->stats.rx_fifo_errors,
1154 &ife->stats.rx_frame_errors,
1156 &ife->stats.tx_packets,
1157 &ife->stats.tx_errors,
1158 &ife->stats.tx_dropped,
1159 &ife->stats.tx_fifo_errors,
1160 &ife->stats.collisions,
1161 &ife->stats.tx_carrier_errors);
1162 ife->stats.rx_bytes = 0;
1163 ife->stats.tx_bytes = 0;
1164 ife->stats.rx_multicast = 0;
1170 static inline int procnetdev_version(char *buf)
1172 if (strstr(buf, "compressed"))
1174 if (strstr(buf, "bytes"))
1179 static int if_readlist_proc(char *target)
1181 static int proc_read;
1184 struct interface *ife;
1192 fh = fopen(_PATH_PROCNET_DEV, "r");
1194 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1195 _PATH_PROCNET_DEV, strerror(errno));
1196 return if_readconf();
1198 fgets(buf, sizeof buf, fh); /* eat line */
1199 fgets(buf, sizeof buf, fh);
1201 #if 0 /* pretty, but can't cope with missing fields */
1202 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1203 "face", "", /* parsed separately */
1210 "compressed", "%lu",
1219 "compressed", "%lu",
1224 procnetdev_vsn = procnetdev_version(buf);
1228 while (fgets(buf, sizeof buf, fh)) {
1229 char *s, name[IFNAMSIZ];
1230 s = get_name(name, buf);
1231 ife = add_interface(name);
1232 get_dev_fields(s, ife);
1233 ife->statistics_valid = 1;
1234 if (target && !strcmp(target,name))
1238 perror(_PATH_PROCNET_DEV);
1250 static int if_readlist(void)
1252 int err = if_readlist_proc(NULL);
1254 err = if_readconf();
1258 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1260 struct interface *ife;
1262 if (!int_list && (if_readlist() < 0))
1264 for (ife = int_list; ife; ife = ife->next) {
1265 int err = doit(ife, cookie);
1272 /* Support for fetching an IPX address */
1275 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1277 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1278 return ioctl(sock, SIOCGIFADDR, ifr);
1283 /* Fetch the interface configuration from the kernel. */
1284 static int if_fetch(struct interface *ife)
1288 char *ifname = ife->name;
1290 strcpy(ifr.ifr_name, ifname);
1291 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1293 ife->flags = ifr.ifr_flags;
1295 strcpy(ifr.ifr_name, ifname);
1296 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1297 memset(ife->hwaddr, 0, 32);
1299 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1301 ife->type = ifr.ifr_hwaddr.sa_family;
1303 strcpy(ifr.ifr_name, ifname);
1304 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1307 ife->metric = ifr.ifr_metric;
1309 strcpy(ifr.ifr_name, ifname);
1310 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1313 ife->mtu = ifr.ifr_mtu;
1316 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1317 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1318 ife->type == ARPHRD_ADAPT) {
1320 strcpy(ifr.ifr_name, ifname);
1321 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1324 ife->outfill = (unsigned int) ifr.ifr_data;
1326 #ifdef SIOCGKEEPALIVE
1327 strcpy(ifr.ifr_name, ifname);
1328 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1331 ife->keepalive = (unsigned int) ifr.ifr_data;
1336 strcpy(ifr.ifr_name, ifname);
1337 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1338 memset(&ife->map, 0, sizeof(struct ifmap));
1340 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1342 strcpy(ifr.ifr_name, ifname);
1343 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1344 memset(&ife->map, 0, sizeof(struct ifmap));
1346 ife->map = ifr.ifr_map;
1348 #ifdef HAVE_TXQUEUELEN
1349 strcpy(ifr.ifr_name, ifname);
1350 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1351 ife->tx_queue_len = -1; /* unknown value */
1353 ife->tx_queue_len = ifr.ifr_qlen;
1355 ife->tx_queue_len = -1; /* unknown value */
1360 fd = get_socket_for_af(AF_INET);
1362 strcpy(ifr.ifr_name, ifname);
1363 ifr.ifr_addr.sa_family = AF_INET;
1364 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1366 ife->addr = ifr.ifr_addr;
1367 strcpy(ifr.ifr_name, ifname);
1368 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1369 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1371 ife->dstaddr = ifr.ifr_dstaddr;
1373 strcpy(ifr.ifr_name, ifname);
1374 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1375 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1377 ife->broadaddr = ifr.ifr_broadaddr;
1379 strcpy(ifr.ifr_name, ifname);
1380 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1381 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1383 ife->netmask = ifr.ifr_netmask;
1385 memset(&ife->addr, 0, sizeof(struct sockaddr));
1390 /* DDP address maybe ? */
1391 fd = get_socket_for_af(AF_APPLETALK);
1393 strcpy(ifr.ifr_name, ifname);
1394 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1395 ife->ddpaddr = ifr.ifr_addr;
1402 /* Look for IPX addresses with all framing types */
1403 fd = get_socket_for_af(AF_IPX);
1405 strcpy(ifr.ifr_name, ifname);
1406 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1407 ife->has_ipx_bb = 1;
1408 ife->ipxaddr_bb = ifr.ifr_addr;
1410 strcpy(ifr.ifr_name, ifname);
1411 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1412 ife->has_ipx_sn = 1;
1413 ife->ipxaddr_sn = ifr.ifr_addr;
1415 strcpy(ifr.ifr_name, ifname);
1416 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1417 ife->has_ipx_e3 = 1;
1418 ife->ipxaddr_e3 = ifr.ifr_addr;
1420 strcpy(ifr.ifr_name, ifname);
1421 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1422 ife->has_ipx_e2 = 1;
1423 ife->ipxaddr_e2 = ifr.ifr_addr;
1429 /* Econet address maybe? */
1430 fd = get_socket_for_af(AF_ECONET);
1432 strcpy(ifr.ifr_name, ifname);
1433 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1434 ife->ecaddr = ifr.ifr_addr;
1435 ife->has_econet = 1;
1444 static int do_if_fetch(struct interface *ife)
1446 if (if_fetch(ife) < 0) {
1448 if (errno == ENODEV) {
1449 /* Give better error message for this case. */
1450 errmsg = _("Device not found");
1452 errmsg = strerror(errno);
1454 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1461 /* This structure defines hardware protocols and their handlers. */
1467 char *(*print) (unsigned char *);
1468 int (*input) (char *, struct sockaddr *);
1469 int (*activate) (int fd);
1470 int suppress_null_addr;
1473 static struct hwtype unspec_hwtype =
1475 "unspec", "UNSPEC", -1, 0,
1476 UNSPEC_print, NULL, NULL
1479 static struct hwtype loop_hwtype =
1481 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1486 #include <net/if_arp.h>
1487 #include <linux/if_ether.h>
1489 static struct hwtype ether_hwtype;
1491 /* Display an Ethernet address in readable format. */
1492 static char *pr_ether(unsigned char *ptr)
1494 static char buff[64];
1496 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1497 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1498 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1504 /* Input an Ethernet address and convert to binary. */
1505 static int in_ether(char *bufp, struct sockaddr *sap)
1512 sap->sa_family = ether_hwtype.type;
1517 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1522 else if (c >= 'a' && c <= 'f')
1524 else if (c >= 'A' && c <= 'F')
1528 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1537 else if (c >= 'a' && c <= 'f')
1538 val |= c - 'a' + 10;
1539 else if (c >= 'A' && c <= 'F')
1540 val |= c - 'A' + 10;
1541 else if (c == ':' || c == 0)
1545 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1552 *ptr++ = (unsigned char) (val & 0377);
1555 /* We might get a semicolon here - not required. */
1557 if (i == ETH_ALEN) {
1559 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1568 /* That's it. Any trailing junk? */
1569 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1571 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1577 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1582 #endif /* KEEP_UNUSED */
1585 static struct hwtype ether_hwtype =
1587 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1588 pr_ether, NULL /* UNUSED in_ether */, NULL
1592 #endif /* HAVE_HWETHER */
1597 #include <net/if_arp.h>
1600 /* Start the PPP encapsulation on the file descriptor. */
1601 static int do_ppp(int fd)
1603 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1606 #endif /* KEEP_UNUSED */
1608 static struct hwtype ppp_hwtype =
1610 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1611 NULL, NULL, NULL /* UNUSED do_ppp */, 0
1615 #endif /* HAVE_PPP */
1617 static struct hwtype *hwtypes[] =
1640 #ifdef ARPHRD_IEEE802_TR
1693 static short sVhwinit = 0;
1695 static void hwinit()
1697 loop_hwtype.title = _("Local Loopback");
1698 unspec_hwtype.title = _("UNSPEC");
1700 slip_hwtype.title = _("Serial Line IP");
1701 cslip_hwtype.title = _("VJ Serial Line IP");
1702 slip6_hwtype.title = _("6-bit Serial Line IP");
1703 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1704 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1707 ether_hwtype.title = _("Ethernet");
1710 ash_hwtype.title = _("Ash");
1713 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1716 hippi_hwtype.title = _("HIPPI");
1719 ax25_hwtype.title = _("AMPR AX.25");
1722 rose_hwtype.title = _("AMPR ROSE");
1725 netrom_hwtype.title = _("AMPR NET/ROM");
1728 x25_hwtype.title = _("generic X.25");
1731 tunnel_hwtype.title = _("IPIP Tunnel");
1734 ppp_hwtype.title = _("Point-to-Point Protocol");
1737 hdlc_hwtype.title = _("(Cisco)-HDLC");
1738 lapb_hwtype.title = _("LAPB");
1741 arcnet_hwtype.title = _("ARCnet");
1744 dlci_hwtype.title = _("Frame Relay DLCI");
1745 frad_hwtype.title = _("Frame Relay Access Device");
1748 sit_hwtype.title = _("IPv6-in-IPv4");
1751 irda_hwtype.title = _("IrLAP");
1754 tr_hwtype.title = _("16/4 Mbps Token Ring");
1755 #ifdef ARPHRD_IEEE802_TR
1756 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1760 ec_hwtype.title = _("Econet");
1764 #endif /* KEEP_UNUSED */
1767 static const char *if_port_text[][4] =
1769 /* Keep in step with <linux/netdevice.h> */
1770 {"unknown", NULL, NULL, NULL},
1771 {"10base2", "bnc", "coax", NULL},
1772 {"10baseT", "utp", "tpe", NULL},
1773 {"AUI", "thick", "db15", NULL},
1774 {"100baseT", NULL, NULL, NULL},
1775 {"100baseTX", NULL, NULL, NULL},
1776 {"100baseFX", NULL, NULL, NULL},
1777 {NULL, NULL, NULL, NULL},
1781 /* Check our hardware type table for this type. */
1782 static struct hwtype *get_hwntype(int type)
1784 struct hwtype **hwp;
1789 #endif /* KEEP_UNUSED */
1792 while (*hwp != NULL) {
1793 if ((*hwp)->type == type)
1800 /* return 1 if address is all zeros */
1801 static int hw_null_address(struct hwtype *hw, void *ap)
1804 unsigned char *address = (unsigned char *)ap;
1805 for (i = 0; i < hw->alen; i++)
1811 static const char TRext[] = "\0\0k\0M";
1813 static void print_bytes_scaled(unsigned long long ull, const char *end)
1815 unsigned long long int_part;
1816 unsigned long frac_part;
1823 for (i=0 ; i<2 ; i++) {
1824 if (int_part >= 1024) {
1825 frac_part = ((int_part % 1024) * 10) / 1024;
1827 ext += 2; /* Kb, Mb */
1831 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1834 static void ife_print(struct interface *ptr)
1839 int can_compress = 0;
1842 static struct aftype *ipxtype = NULL;
1845 static struct aftype *ectype = NULL;
1848 static struct aftype *ddptype = NULL;
1852 char addr6[40], devname[20];
1853 struct sockaddr_in6 sap;
1854 int plen, scope, dad_status, if_idx;
1855 extern struct aftype inet6_aftype;
1859 ap = get_afntype(ptr->addr.sa_family);
1861 ap = get_afntype(0);
1865 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1868 hw = get_hwntype(hf);
1870 hw = get_hwntype(-1);
1872 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1873 /* For some hardware types (eg Ash, ATM) we don't print the
1874 hardware address if it's null. */
1875 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1876 hw->suppress_null_addr)))
1877 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1879 if (ptr->flags & IFF_PORTSEL) {
1880 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1881 if (ptr->flags & IFF_AUTOMEDIA)
1882 printf(_("(auto)"));
1889 printf(_(" %s addr:%s "), ap->name,
1890 ap->sprint(&ptr->addr, 1));
1891 if (ptr->flags & IFF_POINTOPOINT) {
1892 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1894 if (ptr->flags & IFF_BROADCAST) {
1895 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1897 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1902 /* FIXME: should be integrated into interface.c. */
1904 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1905 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1906 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1907 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1908 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1909 if (!strcmp(devname, ptr->name)) {
1910 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1911 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1912 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1913 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1914 printf(_(" inet6 addr: %s/%d"),
1915 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1916 printf(_(" Scope:"));
1919 printf(_("Global"));
1921 case IPV6_ADDR_LINKLOCAL:
1924 case IPV6_ADDR_SITELOCAL:
1927 case IPV6_ADDR_COMPATv4:
1928 printf(_("Compat"));
1930 case IPV6_ADDR_LOOPBACK:
1934 printf(_("Unknown"));
1944 if (ipxtype == NULL)
1945 ipxtype = get_afntype(AF_IPX);
1947 if (ipxtype != NULL) {
1948 if (ptr->has_ipx_bb)
1949 printf(_(" IPX/Ethernet II addr:%s\n"),
1950 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1951 if (ptr->has_ipx_sn)
1952 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1953 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1954 if (ptr->has_ipx_e2)
1955 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1956 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1957 if (ptr->has_ipx_e3)
1958 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1959 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1964 if (ddptype == NULL)
1965 ddptype = get_afntype(AF_APPLETALK);
1966 if (ddptype != NULL) {
1968 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
1974 ectype = get_afntype(AF_ECONET);
1975 if (ectype != NULL) {
1976 if (ptr->has_econet)
1977 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
1982 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1983 if (ptr->flags == 0)
1984 printf(_("[NO FLAGS] "));
1985 if (ptr->flags & IFF_UP)
1987 if (ptr->flags & IFF_BROADCAST)
1988 printf(_("BROADCAST "));
1989 if (ptr->flags & IFF_DEBUG)
1990 printf(_("DEBUG "));
1991 if (ptr->flags & IFF_LOOPBACK)
1992 printf(_("LOOPBACK "));
1993 if (ptr->flags & IFF_POINTOPOINT)
1994 printf(_("POINTOPOINT "));
1995 if (ptr->flags & IFF_NOTRAILERS)
1996 printf(_("NOTRAILERS "));
1997 if (ptr->flags & IFF_RUNNING)
1998 printf(_("RUNNING "));
1999 if (ptr->flags & IFF_NOARP)
2000 printf(_("NOARP "));
2001 if (ptr->flags & IFF_PROMISC)
2002 printf(_("PROMISC "));
2003 if (ptr->flags & IFF_ALLMULTI)
2004 printf(_("ALLMULTI "));
2005 if (ptr->flags & IFF_SLAVE)
2006 printf(_("SLAVE "));
2007 if (ptr->flags & IFF_MASTER)
2008 printf(_("MASTER "));
2009 if (ptr->flags & IFF_MULTICAST)
2010 printf(_("MULTICAST "));
2012 if (ptr->flags & IFF_DYNAMIC)
2013 printf(_("DYNAMIC "));
2015 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2016 printf(_(" MTU:%d Metric:%d"),
2017 ptr->mtu, ptr->metric ? ptr->metric : 1);
2018 #ifdef SIOCSKEEPALIVE
2019 if (ptr->outfill || ptr->keepalive)
2020 printf(_(" Outfill:%d Keepalive:%d"),
2021 ptr->outfill, ptr->keepalive);
2025 /* If needed, display the interface statistics. */
2027 if (ptr->statistics_valid) {
2028 /* XXX: statistics are currently only printed for the primary address,
2029 * not for the aliases, although strictly speaking they're shared
2034 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
2035 ptr->stats.rx_packets, ptr->stats.rx_errors,
2036 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2037 ptr->stats.rx_frame_errors);
2039 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
2041 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
2042 ptr->stats.tx_packets, ptr->stats.tx_errors,
2043 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2044 ptr->stats.tx_carrier_errors);
2045 printf(_(" collisions:%lu "), ptr->stats.collisions);
2047 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2048 if (ptr->tx_queue_len != -1)
2049 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
2051 print_bytes_scaled(ptr->stats.rx_bytes, " T");
2052 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2056 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2057 ptr->map.base_addr)) {
2060 printf(_("Interrupt:%d "), ptr->map.irq);
2061 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2063 printf(_("Base address:0x%x "), ptr->map.base_addr);
2064 if (ptr->map.mem_start) {
2065 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2068 printf(_("DMA chan:%x "), ptr->map.dma);
2075 static int do_if_print(struct interface *ife, void *cookie)
2077 int *opt_a = (int *) cookie;
2080 res = do_if_fetch(ife);
2082 if ((ife->flags & IFF_UP) || *opt_a)
2088 static struct interface *lookup_interface(char *name)
2090 struct interface *ife = NULL;
2092 if (if_readlist_proc(name) < 0)
2094 ife = add_interface(name);
2098 /* for ipv4 add/del modes */
2099 static int if_print(char *ifname)
2104 res = for_all_interfaces(do_if_print, &interface_opt_a);
2106 struct interface *ife;
2108 ife = lookup_interface(ifname);
2109 res = do_if_fetch(ife);
2116 int display_interfaces(char *ifname)
2120 /* Create a channel to the NET kernel. */
2121 if ((skfd = sockets_open(0)) < 0) {
2122 perror_msg_and_die("socket");
2125 /* Do we have to show the current setup? */
2126 status = if_print(ifname);