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.5 2001/10/24 04:59:38 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
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>
83 #include <arpa/nameser.h>
88 #define _PATH_PROCNET_DEV "/proc/net/dev"
89 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
90 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
92 static int procnetdev_vsn = 1;
95 /* Ugh. But libc5 doesn't provide POSIX types. */
96 #include <asm/types.h>
100 #include <linux/if_slip.h>
107 * This is in linux/include/net/ipv6.h.
111 struct in6_addr ifr6_addr;
112 __u32 ifr6_prefixlen;
113 unsigned int ifr6_ifindex;
118 #endif /* HAVE_AFINET6 */
121 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
122 #include <netipx/ipx.h>
128 #include "net-support.h"
129 #include "pathnames.h"
132 #include "interface.h"
137 /* Defines for glibc2.0 users. */
138 #ifndef SIOCSIFTXQLEN
139 #define SIOCSIFTXQLEN 0x8943
140 #define SIOCGIFTXQLEN 0x8942
143 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
145 #define ifr_qlen ifr_ifru.ifru_mtu
148 #ifndef HAVE_TXQUEUELEN
149 #define HAVE_TXQUEUELEN 1
153 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
156 /* This structure defines protocol families and their handlers. */
162 char *(*print) (unsigned char *);
163 char *(*sprint) (struct sockaddr *, int numeric);
164 int (*input) (int type, char *bufp, struct sockaddr *);
165 void (*herror) (char *text);
166 int (*rprint) (int options);
167 int (*rinput) (int typ, int ext, char **argv);
170 int (*getmask) (char *src, struct sockaddr * mask, char *name);
176 static struct aftype *aftypes[];
185 static int flag_ax25;
191 static int flag_netrom;
193 static int flag_inet;
195 static int flag_inet6;
198 static int flag_econet;
201 static int flag_x25 = 0;
208 static struct aftrans_t {
216 "ax25", "ax25", &flag_ax25
220 "ip", "inet", &flag_inet
224 "ip6", "inet6", &flag_inet6
229 "ipx", "ipx", &flag_ipx
234 "appletalk", "ddp", &flag_ddp
239 "netrom", "netrom", &flag_netrom
243 "inet", "inet", &flag_inet
247 "inet6", "inet6", &flag_inet6
252 "ddp", "ddp", &flag_ddp
256 "unix", "unix", &flag_unx
259 "tcpip", "inet", &flag_inet
263 "econet", "ec", &flag_econet
268 "x25", "x25", &flag_x25
273 "ash", "ash", &flag_ash
281 static char afname[256] = "";
282 #endif /* KEEP_UNUSED */
286 /* Display a UNIX domain address. */
287 static char *UNIX_print(unsigned char *ptr)
293 /* Display a UNIX domain address. */
294 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
298 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
299 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
300 return (UNIX_print(sap->sa_data));
304 static struct aftype unix_aftype =
306 "unix", "UNIX Domain", AF_UNIX, 0,
307 UNIX_print, UNIX_sprint, NULL, NULL,
312 #endif /* HAVE_AFUNIX */
317 extern int h_errno; /* some netdb.h versions don't export this */
322 struct sockaddr_in addr;
328 static struct addr *INET_nn = NULL; /* addr-to-name cache */
331 static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
337 sin->sin_family = AF_INET;
340 /* Default is special, meaning 0.0.0.0. */
341 if (!strcmp(name, "default")) {
342 sin->sin_addr.s_addr = INADDR_ANY;
345 /* Look to see if it's a dotted quad. */
346 if (inet_aton(name, &sin->sin_addr)) {
349 /* If we expect this to be a hostname, try hostname database first */
351 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
354 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
355 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
356 sizeof(struct in_addr));
359 /* Try the NETWORKS database to see if this is a known network. */
361 fprintf (stderr, "getnetbyname (%s)\n", name);
363 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
364 sin->sin_addr.s_addr = htonl(np->n_net);
368 /* Don't try again */
374 _res.options |= RES_DEBUG;
378 fprintf (stderr, "gethostbyname (%s)\n", name);
380 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
384 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
385 sizeof(struct in_addr));
389 #endif /* KEEP_UNUSED */
391 /* numeric: & 0x8000: default instead of *,
392 * & 0x4000: host instead of net,
393 * & 0x0fff: don't resolve
395 static int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
396 int numeric, unsigned int netmask)
401 unsigned long ad, host_ad;
405 if (s_in->sin_family != AF_INET) {
407 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
409 errno = EAFNOSUPPORT;
412 ad = (unsigned long) s_in->sin_addr.s_addr;
414 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
416 if (ad == INADDR_ANY) {
417 if ((numeric & 0x0FFF) == 0) {
418 if (numeric & 0x8000)
419 safe_strncpy(name, "default", len);
421 safe_strncpy(name, "*", len);
425 if (numeric & 0x0FFF) {
426 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
430 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
437 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
438 safe_strncpy(name, pn->name, len);
440 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
452 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
454 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
456 safe_strncpy(name, ent->h_name, len);
459 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
462 np = getnetbyaddr(host_ad, AF_INET);
464 safe_strncpy(name, np->n_name, len);
467 if ((ent == NULL) && (np == NULL))
468 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
469 pn = (struct addr *) xmalloc(sizeof(struct addr));
473 pn->name = (char *) xmalloc(strlen(name) + 1);
474 strcpy(pn->name, name);
481 static void INET_reserror(char *text)
486 /* Display an Internet socket address. */
487 static char *INET_print(unsigned char *ptr)
489 return (inet_ntoa((*(struct in_addr *) ptr)));
491 #endif /* KEEP_UNUSED */
493 /* Display an Internet socket address. */
494 static char *INET_sprint(struct sockaddr *sap, int numeric)
496 static char buff[128];
498 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
499 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
501 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
502 numeric, 0xffffff00) != 0)
509 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
510 unsigned int netmask)
512 static char buff[128];
514 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
515 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
516 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
517 numeric, netmask) != 0)
522 static int INET_getsock(char *bufp, struct sockaddr *sap)
524 char *sp = bufp, *bp;
527 struct sockaddr_in *sin;
529 sin = (struct sockaddr_in *) sap;
530 sin->sin_family = AF_INET;
535 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
538 if ((*sp >= 'A') && (*sp <= 'F'))
539 bp[i] |= (int) (*sp - 'A') + 10;
540 else if ((*sp >= '0') && (*sp <= '9'))
541 bp[i] |= (int) (*sp - '0');
549 if ((*sp >= 'A') && (*sp <= 'F'))
550 bp[i] |= (int) (*sp - 'A') + 10;
551 else if ((*sp >= '0') && (*sp <= '9'))
552 bp[i] |= (int) (*sp - '0');
558 sin->sin_addr.s_addr = htonl(val);
563 static int INET_input(int type, char *bufp, struct sockaddr *sap)
567 return (INET_getsock(bufp, sap));
569 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
571 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
575 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
577 struct sockaddr_in *mask = (struct sockaddr_in *) m;
581 if ((slash = strchr(adr, '/')) == NULL)
585 prefix = strtoul(slash, &end, 0);
590 sprintf(name, "/%d", prefix);
592 mask->sin_family = AF_INET;
593 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
596 #endif /* KEEP_UNUSED */
598 static struct aftype inet_aftype =
600 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
601 NULL /* UNUSED INET_print */, INET_sprint,
602 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
603 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
604 NULL /* UNUSED INET_getnetmask */,
609 #endif /* HAVE_AFINET */
611 /* Display an UNSPEC address. */
612 static char *UNSPEC_print(unsigned char *ptr)
614 static char buff[sizeof(struct sockaddr)*3+1];
619 for (i = 0; i < sizeof(struct sockaddr); i++) {
620 /* careful -- not every libc's sprintf returns # bytes written */
621 sprintf(pos, "%02X-", (*ptr++ & 0377));
624 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
629 /* Display an UNSPEC socket address. */
630 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
634 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
635 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
636 return (UNSPEC_print(sap->sa_data));
639 static struct aftype unspec_aftype =
641 "unspec", "UNSPEC", AF_UNSPEC, 0,
642 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
646 static struct aftype *aftypes[] =
686 static short sVafinit = 0;
690 unspec_aftype.title = _("UNSPEC");
692 unix_aftype.title = _("UNIX Domain");
695 inet_aftype.title = _("DARPA Internet");
698 inet6_aftype.title = _("IPv6");
701 ax25_aftype.title = _("AMPR AX.25");
704 netrom_aftype.title = _("AMPR NET/ROM");
707 ipx_aftype.title = _("Novell IPX");
710 ddp_aftype.title = _("Appletalk DDP");
713 ec_aftype.title = _("Econet");
716 x25_aftype.title = _("CCITT X.25");
719 rose_aftype.title = _("AMPR ROSE");
722 ash_aftype.title = _("Ash");
727 static int aftrans_opt(const char *arg)
729 struct aftrans_t *paft;
733 safe_strncpy(buf, arg, sizeof(buf));
739 tmp2 = strchr(tmp1, ',');
745 for (paft = aftrans; paft->alias; paft++) {
746 if (strcmp(tmp1, paft->alias))
748 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
749 fprintf(stderr, _("Too much address family arguments.\n"));
756 strcat(afname, paft->name);
760 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
769 /* set the default AF list from the program name or a constant value */
770 static void aftrans_def(char *tool, char *argv0, char *dflt)
775 strcpy(afname, dflt);
777 if (!(tmp = strrchr(argv0, '/')))
778 tmp = argv0; /* no slash?! */
782 if (!(buf = strdup(tmp)))
785 if (strlen(tool) >= strlen(tmp)) {
789 tmp = buf + (strlen(tmp) - strlen(tool));
791 if (strcmp(tmp, tool) != 0) {
796 if ((tmp = strchr(buf, '_')))
800 if (aftrans_opt(buf))
806 /* Check our protocol family table for this family. */
807 static struct aftype *get_aftype(const char *name)
814 #endif /* KEEP_UNUSED */
817 while (*afp != NULL) {
818 if (!strcmp((*afp)->name, name))
822 if (strchr(name, ','))
823 fprintf(stderr, _("Please don't supply more than one address family.\n"));
826 #endif /* KEEP_UNUSED */
828 /* Check our protocol family table for this family. */
829 static struct aftype *get_afntype(int af)
836 #endif /* KEEP_UNUSED */
839 while (*afp != NULL) {
840 if ((*afp)->af == af)
847 /* Check our protocol family table for this family and return its socket */
848 static int get_socket_for_af(int af)
855 #endif /* KEEP_UNUSED */
858 while (*afp != NULL) {
859 if ((*afp)->af == af)
867 /* type: 0=all, 1=getroute */
868 static void print_aflist(int type) {
876 #endif /* KEEP_UNUSED */
879 while (*afp != NULL) {
880 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
883 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
884 txt = (*afp)->name; if (!txt) txt = "..";
885 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
889 fprintf(stderr,"\n");
891 #endif /* KEEP_UNUSED */
893 struct user_net_device_stats {
894 unsigned long long rx_packets; /* total packets received */
895 unsigned long long tx_packets; /* total packets transmitted */
896 unsigned long long rx_bytes; /* total bytes received */
897 unsigned long long tx_bytes; /* total bytes transmitted */
898 unsigned long rx_errors; /* bad packets received */
899 unsigned long tx_errors; /* packet transmit problems */
900 unsigned long rx_dropped; /* no space in linux buffers */
901 unsigned long tx_dropped; /* no space available in linux */
902 unsigned long rx_multicast; /* multicast packets received */
903 unsigned long rx_compressed;
904 unsigned long tx_compressed;
905 unsigned long collisions;
907 /* detailed rx_errors: */
908 unsigned long rx_length_errors;
909 unsigned long rx_over_errors; /* receiver ring buff overflow */
910 unsigned long rx_crc_errors; /* recved pkt with crc error */
911 unsigned long rx_frame_errors; /* recv'd frame alignment error */
912 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
913 unsigned long rx_missed_errors; /* receiver missed packet */
914 /* detailed tx_errors */
915 unsigned long tx_aborted_errors;
916 unsigned long tx_carrier_errors;
917 unsigned long tx_fifo_errors;
918 unsigned long tx_heartbeat_errors;
919 unsigned long tx_window_errors;
923 struct interface *next, *prev;
924 char name[IFNAMSIZ]; /* interface name */
925 short type; /* if type */
926 short flags; /* various flags */
927 int metric; /* routing metric */
928 int mtu; /* MTU value */
929 int tx_queue_len; /* transmit queue length */
930 struct ifmap map; /* hardware setup */
931 struct sockaddr addr; /* IP address */
932 struct sockaddr dstaddr; /* P-P IP address */
933 struct sockaddr broadaddr; /* IP broadcast address */
934 struct sockaddr netmask; /* IP network mask */
935 struct sockaddr ipxaddr_bb; /* IPX network address */
936 struct sockaddr ipxaddr_sn; /* IPX network address */
937 struct sockaddr ipxaddr_e3; /* IPX network address */
938 struct sockaddr ipxaddr_e2; /* IPX network address */
939 struct sockaddr ddpaddr; /* Appletalk DDP address */
940 struct sockaddr ecaddr; /* Econet address */
949 char hwaddr[32]; /* HW address */
950 int statistics_valid;
951 struct user_net_device_stats stats; /* statistics */
952 int keepalive; /* keepalive value for SLIP */
953 int outfill; /* outfill value for SLIP */
957 int interface_opt_a = 0; /* show all interfaces */
960 static int opt_i = 0; /* show the statistics */
961 static int opt_v = 0; /* debugging output flag */
963 static int addr_family = 0; /* currently selected AF */
964 #endif /* KEEP_UNUSED */
966 static struct interface *int_list, *int_last;
967 static int skfd = -1; /* generic raw socket desc. */
970 static int sockets_open(int family)
974 static int force = -1;
978 if (get_kernel_revision() < KRELEASE(2, 1, 0))
980 if (access("/proc/net", R_OK))
983 for (aft = aftypes; *aft; aft++) {
984 struct aftype *af = *aft;
985 int type = SOCK_DGRAM;
986 if (af->af == AF_UNSPEC)
988 if (family && family != af->af)
994 /* Check some /proc file first to not stress kmod */
995 if (!family && !force && af->flag_file) {
996 if (access(af->flag_file, R_OK))
1000 if (af->af == AF_NETROM)
1001 type = SOCK_SEQPACKET;
1004 if (af->af == AF_X25)
1005 type = SOCK_SEQPACKET;
1007 af->fd = socket(af->af, type, 0);
1012 fprintf(stderr, _("No usable address families found.\n"));
1016 /* like strcmp(), but knows about numbers */
1017 static int nstrcmp(const char *astr, const char *b)
1019 const char *a = astr;
1040 return atoi(a) > atoi(b) ? 1 : -1;
1045 static struct interface *add_interface(char *name)
1047 struct interface *ife, **nextp, *new;
1049 for (ife = int_last; ife; ife = ife->prev) {
1050 int n = nstrcmp(ife->name, name);
1057 safe_strncpy(new->name, name, IFNAMSIZ);
1058 nextp = ife ? &ife->next : &int_list;
1062 new->next->prev = new;
1070 static int if_readconf(void)
1078 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1080 skfd2 = get_socket_for_af(AF_INET);
1082 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1084 /* Try to soldier on with whatever socket we can get hold of. */
1085 skfd2 = sockets_open(0);
1092 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1093 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1095 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
1096 perror("SIOCGIFCONF");
1099 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1100 /* assume it overflowed and try again */
1108 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1109 add_interface(ifr->ifr_name);
1119 static char *get_name(char *name, char *p)
1126 if (*p == ':') { /* could be an alias */
1127 char *dot = p, *dotname = name;
1131 if (*p != ':') { /* it wasn't, backup */
1146 static int get_dev_fields(char *bp, struct interface *ife)
1148 switch (procnetdev_vsn) {
1151 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1152 &ife->stats.rx_bytes,
1153 &ife->stats.rx_packets,
1154 &ife->stats.rx_errors,
1155 &ife->stats.rx_dropped,
1156 &ife->stats.rx_fifo_errors,
1157 &ife->stats.rx_frame_errors,
1158 &ife->stats.rx_compressed,
1159 &ife->stats.rx_multicast,
1161 &ife->stats.tx_bytes,
1162 &ife->stats.tx_packets,
1163 &ife->stats.tx_errors,
1164 &ife->stats.tx_dropped,
1165 &ife->stats.tx_fifo_errors,
1166 &ife->stats.collisions,
1167 &ife->stats.tx_carrier_errors,
1168 &ife->stats.tx_compressed);
1171 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1172 &ife->stats.rx_bytes,
1173 &ife->stats.rx_packets,
1174 &ife->stats.rx_errors,
1175 &ife->stats.rx_dropped,
1176 &ife->stats.rx_fifo_errors,
1177 &ife->stats.rx_frame_errors,
1179 &ife->stats.tx_bytes,
1180 &ife->stats.tx_packets,
1181 &ife->stats.tx_errors,
1182 &ife->stats.tx_dropped,
1183 &ife->stats.tx_fifo_errors,
1184 &ife->stats.collisions,
1185 &ife->stats.tx_carrier_errors);
1186 ife->stats.rx_multicast = 0;
1189 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1190 &ife->stats.rx_packets,
1191 &ife->stats.rx_errors,
1192 &ife->stats.rx_dropped,
1193 &ife->stats.rx_fifo_errors,
1194 &ife->stats.rx_frame_errors,
1196 &ife->stats.tx_packets,
1197 &ife->stats.tx_errors,
1198 &ife->stats.tx_dropped,
1199 &ife->stats.tx_fifo_errors,
1200 &ife->stats.collisions,
1201 &ife->stats.tx_carrier_errors);
1202 ife->stats.rx_bytes = 0;
1203 ife->stats.tx_bytes = 0;
1204 ife->stats.rx_multicast = 0;
1210 static inline int procnetdev_version(char *buf)
1212 if (strstr(buf, "compressed"))
1214 if (strstr(buf, "bytes"))
1219 static int if_readlist_proc(char *target)
1221 static int proc_read;
1224 struct interface *ife;
1232 fh = fopen(_PATH_PROCNET_DEV, "r");
1234 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1235 _PATH_PROCNET_DEV, strerror(errno));
1236 return if_readconf();
1238 fgets(buf, sizeof buf, fh); /* eat line */
1239 fgets(buf, sizeof buf, fh);
1241 #if 0 /* pretty, but can't cope with missing fields */
1242 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1243 "face", "", /* parsed separately */
1250 "compressed", "%lu",
1259 "compressed", "%lu",
1264 procnetdev_vsn = procnetdev_version(buf);
1268 while (fgets(buf, sizeof buf, fh)) {
1269 char *s, name[IFNAMSIZ];
1270 s = get_name(name, buf);
1271 ife = add_interface(name);
1272 get_dev_fields(s, ife);
1273 ife->statistics_valid = 1;
1274 if (target && !strcmp(target,name))
1278 perror(_PATH_PROCNET_DEV);
1290 static int if_readlist(void)
1292 int err = if_readlist_proc(NULL);
1294 err = if_readconf();
1298 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1300 struct interface *ife;
1302 if (!int_list && (if_readlist() < 0))
1304 for (ife = int_list; ife; ife = ife->next) {
1305 int err = doit(ife, cookie);
1312 /* Support for fetching an IPX address */
1315 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1317 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1318 return ioctl(sock, SIOCGIFADDR, ifr);
1323 /* Fetch the interface configuration from the kernel. */
1324 static int if_fetch(struct interface *ife)
1328 char *ifname = ife->name;
1330 strcpy(ifr.ifr_name, ifname);
1331 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1333 ife->flags = ifr.ifr_flags;
1335 strcpy(ifr.ifr_name, ifname);
1336 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1337 memset(ife->hwaddr, 0, 32);
1339 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1341 ife->type = ifr.ifr_hwaddr.sa_family;
1343 strcpy(ifr.ifr_name, ifname);
1344 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1347 ife->metric = ifr.ifr_metric;
1349 strcpy(ifr.ifr_name, ifname);
1350 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1353 ife->mtu = ifr.ifr_mtu;
1356 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1357 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1358 ife->type == ARPHRD_ADAPT) {
1360 strcpy(ifr.ifr_name, ifname);
1361 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1364 ife->outfill = (unsigned int) ifr.ifr_data;
1366 #ifdef SIOCGKEEPALIVE
1367 strcpy(ifr.ifr_name, ifname);
1368 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1371 ife->keepalive = (unsigned int) ifr.ifr_data;
1376 strcpy(ifr.ifr_name, ifname);
1377 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1378 memset(&ife->map, 0, sizeof(struct ifmap));
1380 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1382 strcpy(ifr.ifr_name, ifname);
1383 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1384 memset(&ife->map, 0, sizeof(struct ifmap));
1386 ife->map = ifr.ifr_map;
1388 #ifdef HAVE_TXQUEUELEN
1389 strcpy(ifr.ifr_name, ifname);
1390 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1391 ife->tx_queue_len = -1; /* unknown value */
1393 ife->tx_queue_len = ifr.ifr_qlen;
1395 ife->tx_queue_len = -1; /* unknown value */
1400 fd = get_socket_for_af(AF_INET);
1402 strcpy(ifr.ifr_name, ifname);
1403 ifr.ifr_addr.sa_family = AF_INET;
1404 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1406 ife->addr = ifr.ifr_addr;
1407 strcpy(ifr.ifr_name, ifname);
1408 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1409 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1411 ife->dstaddr = ifr.ifr_dstaddr;
1413 strcpy(ifr.ifr_name, ifname);
1414 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1415 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1417 ife->broadaddr = ifr.ifr_broadaddr;
1419 strcpy(ifr.ifr_name, ifname);
1420 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1421 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1423 ife->netmask = ifr.ifr_netmask;
1425 memset(&ife->addr, 0, sizeof(struct sockaddr));
1430 /* DDP address maybe ? */
1431 fd = get_socket_for_af(AF_APPLETALK);
1433 strcpy(ifr.ifr_name, ifname);
1434 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1435 ife->ddpaddr = ifr.ifr_addr;
1442 /* Look for IPX addresses with all framing types */
1443 fd = get_socket_for_af(AF_IPX);
1445 strcpy(ifr.ifr_name, ifname);
1446 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1447 ife->has_ipx_bb = 1;
1448 ife->ipxaddr_bb = ifr.ifr_addr;
1450 strcpy(ifr.ifr_name, ifname);
1451 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1452 ife->has_ipx_sn = 1;
1453 ife->ipxaddr_sn = ifr.ifr_addr;
1455 strcpy(ifr.ifr_name, ifname);
1456 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1457 ife->has_ipx_e3 = 1;
1458 ife->ipxaddr_e3 = ifr.ifr_addr;
1460 strcpy(ifr.ifr_name, ifname);
1461 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1462 ife->has_ipx_e2 = 1;
1463 ife->ipxaddr_e2 = ifr.ifr_addr;
1469 /* Econet address maybe? */
1470 fd = get_socket_for_af(AF_ECONET);
1472 strcpy(ifr.ifr_name, ifname);
1473 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1474 ife->ecaddr = ifr.ifr_addr;
1475 ife->has_econet = 1;
1484 static int do_if_fetch(struct interface *ife)
1486 if (if_fetch(ife) < 0) {
1488 if (errno == ENODEV) {
1489 /* Give better error message for this case. */
1490 errmsg = _("Device not found");
1492 errmsg = strerror(errno);
1494 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1501 /* This structure defines hardware protocols and their handlers. */
1507 char *(*print) (unsigned char *);
1508 int (*input) (char *, struct sockaddr *);
1509 int (*activate) (int fd);
1510 int suppress_null_addr;
1513 static struct hwtype unspec_hwtype =
1515 "unspec", "UNSPEC", -1, 0,
1516 UNSPEC_print, NULL, NULL
1519 static struct hwtype loop_hwtype =
1521 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1526 #include <net/if_arp.h>
1527 #include <linux/if_ether.h>
1529 static struct hwtype ether_hwtype;
1531 /* Display an Ethernet address in readable format. */
1532 static char *pr_ether(unsigned char *ptr)
1534 static char buff[64];
1536 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1537 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1538 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1544 /* Input an Ethernet address and convert to binary. */
1545 static int in_ether(char *bufp, struct sockaddr *sap)
1552 sap->sa_family = ether_hwtype.type;
1557 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1562 else if (c >= 'a' && c <= 'f')
1564 else if (c >= 'A' && c <= 'F')
1568 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1577 else if (c >= 'a' && c <= 'f')
1578 val |= c - 'a' + 10;
1579 else if (c >= 'A' && c <= 'F')
1580 val |= c - 'A' + 10;
1581 else if (c == ':' || c == 0)
1585 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1592 *ptr++ = (unsigned char) (val & 0377);
1595 /* We might get a semicolon here - not required. */
1597 if (i == ETH_ALEN) {
1599 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1608 /* That's it. Any trailing junk? */
1609 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1611 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1617 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1622 #endif /* KEEP_UNUSED */
1625 static struct hwtype ether_hwtype =
1627 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1628 pr_ether, NULL /* UNUSED in_ether */, NULL
1632 #endif /* HAVE_HWETHER */
1637 #include <net/if_arp.h>
1640 /* Start the PPP encapsulation on the file descriptor. */
1641 static int do_ppp(int fd)
1643 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1646 #endif /* KEEP_UNUSED */
1648 static struct hwtype ppp_hwtype =
1650 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1651 NULL, NULL, NULL /* UNUSED do_ppp */, 0
1655 #endif /* HAVE_PPP */
1657 static struct hwtype *hwtypes[] =
1680 #ifdef ARPHRD_IEEE802_TR
1733 static short sVhwinit = 0;
1735 static void hwinit()
1737 loop_hwtype.title = _("Local Loopback");
1738 unspec_hwtype.title = _("UNSPEC");
1740 slip_hwtype.title = _("Serial Line IP");
1741 cslip_hwtype.title = _("VJ Serial Line IP");
1742 slip6_hwtype.title = _("6-bit Serial Line IP");
1743 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1744 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1747 ether_hwtype.title = _("Ethernet");
1750 ash_hwtype.title = _("Ash");
1753 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1756 hippi_hwtype.title = _("HIPPI");
1759 ax25_hwtype.title = _("AMPR AX.25");
1762 rose_hwtype.title = _("AMPR ROSE");
1765 netrom_hwtype.title = _("AMPR NET/ROM");
1768 x25_hwtype.title = _("generic X.25");
1771 tunnel_hwtype.title = _("IPIP Tunnel");
1774 ppp_hwtype.title = _("Point-to-Point Protocol");
1777 hdlc_hwtype.title = _("(Cisco)-HDLC");
1778 lapb_hwtype.title = _("LAPB");
1781 arcnet_hwtype.title = _("ARCnet");
1784 dlci_hwtype.title = _("Frame Relay DLCI");
1785 frad_hwtype.title = _("Frame Relay Access Device");
1788 sit_hwtype.title = _("IPv6-in-IPv4");
1791 irda_hwtype.title = _("IrLAP");
1794 tr_hwtype.title = _("16/4 Mbps Token Ring");
1795 #ifdef ARPHRD_IEEE802_TR
1796 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1800 ec_hwtype.title = _("Econet");
1804 #endif /* KEEP_UNUSED */
1807 static const char *if_port_text[][4] =
1809 /* Keep in step with <linux/netdevice.h> */
1810 {"unknown", NULL, NULL, NULL},
1811 {"10base2", "bnc", "coax", NULL},
1812 {"10baseT", "utp", "tpe", NULL},
1813 {"AUI", "thick", "db15", NULL},
1814 {"100baseT", NULL, NULL, NULL},
1815 {"100baseTX", NULL, NULL, NULL},
1816 {"100baseFX", NULL, NULL, NULL},
1817 {NULL, NULL, NULL, NULL},
1821 /* Check our hardware type table for this type. */
1822 static struct hwtype *get_hwntype(int type)
1824 struct hwtype **hwp;
1829 #endif /* KEEP_UNUSED */
1832 while (*hwp != NULL) {
1833 if ((*hwp)->type == type)
1840 /* return 1 if address is all zeros */
1841 static int hw_null_address(struct hwtype *hw, void *ap)
1844 unsigned char *address = (unsigned char *)ap;
1845 for (i = 0; i < hw->alen; i++)
1851 static const char TRext[] = "\0\0k\0M";
1853 static void print_bytes_scaled(unsigned long long ull, const char *end)
1855 unsigned long long int_part;
1856 unsigned long frac_part;
1863 for (i=0 ; i<2 ; i++) {
1864 if (int_part >= 1024) {
1865 frac_part = ((int_part % 1024) * 10) / 1024;
1867 ext += 2; /* Kb, Mb */
1871 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1874 static void ife_print(struct interface *ptr)
1879 int can_compress = 0;
1882 static struct aftype *ipxtype = NULL;
1885 static struct aftype *ectype = NULL;
1888 static struct aftype *ddptype = NULL;
1892 char addr6[40], devname[20];
1893 struct sockaddr_in6 sap;
1894 int plen, scope, dad_status, if_idx;
1895 extern struct aftype inet6_aftype;
1899 ap = get_afntype(ptr->addr.sa_family);
1901 ap = get_afntype(0);
1905 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1908 hw = get_hwntype(hf);
1910 hw = get_hwntype(-1);
1912 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1913 /* For some hardware types (eg Ash, ATM) we don't print the
1914 hardware address if it's null. */
1915 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1916 hw->suppress_null_addr)))
1917 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1919 if (ptr->flags & IFF_PORTSEL) {
1920 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1921 if (ptr->flags & IFF_AUTOMEDIA)
1922 printf(_("(auto)"));
1929 printf(_(" %s addr:%s "), ap->name,
1930 ap->sprint(&ptr->addr, 1));
1931 if (ptr->flags & IFF_POINTOPOINT) {
1932 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1934 if (ptr->flags & IFF_BROADCAST) {
1935 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1937 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1942 /* FIXME: should be integrated into interface.c. */
1944 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1945 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1946 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1947 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1948 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1949 if (!strcmp(devname, ptr->name)) {
1950 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1951 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1952 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1953 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1954 printf(_(" inet6 addr: %s/%d"),
1955 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1956 printf(_(" Scope:"));
1959 printf(_("Global"));
1961 case IPV6_ADDR_LINKLOCAL:
1964 case IPV6_ADDR_SITELOCAL:
1967 case IPV6_ADDR_COMPATv4:
1968 printf(_("Compat"));
1970 case IPV6_ADDR_LOOPBACK:
1974 printf(_("Unknown"));
1984 if (ipxtype == NULL)
1985 ipxtype = get_afntype(AF_IPX);
1987 if (ipxtype != NULL) {
1988 if (ptr->has_ipx_bb)
1989 printf(_(" IPX/Ethernet II addr:%s\n"),
1990 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1991 if (ptr->has_ipx_sn)
1992 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1993 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1994 if (ptr->has_ipx_e2)
1995 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1996 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1997 if (ptr->has_ipx_e3)
1998 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1999 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
2004 if (ddptype == NULL)
2005 ddptype = get_afntype(AF_APPLETALK);
2006 if (ddptype != NULL) {
2008 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
2014 ectype = get_afntype(AF_ECONET);
2015 if (ectype != NULL) {
2016 if (ptr->has_econet)
2017 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
2022 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
2023 if (ptr->flags == 0)
2024 printf(_("[NO FLAGS] "));
2025 if (ptr->flags & IFF_UP)
2027 if (ptr->flags & IFF_BROADCAST)
2028 printf(_("BROADCAST "));
2029 if (ptr->flags & IFF_DEBUG)
2030 printf(_("DEBUG "));
2031 if (ptr->flags & IFF_LOOPBACK)
2032 printf(_("LOOPBACK "));
2033 if (ptr->flags & IFF_POINTOPOINT)
2034 printf(_("POINTOPOINT "));
2035 if (ptr->flags & IFF_NOTRAILERS)
2036 printf(_("NOTRAILERS "));
2037 if (ptr->flags & IFF_RUNNING)
2038 printf(_("RUNNING "));
2039 if (ptr->flags & IFF_NOARP)
2040 printf(_("NOARP "));
2041 if (ptr->flags & IFF_PROMISC)
2042 printf(_("PROMISC "));
2043 if (ptr->flags & IFF_ALLMULTI)
2044 printf(_("ALLMULTI "));
2045 if (ptr->flags & IFF_SLAVE)
2046 printf(_("SLAVE "));
2047 if (ptr->flags & IFF_MASTER)
2048 printf(_("MASTER "));
2049 if (ptr->flags & IFF_MULTICAST)
2050 printf(_("MULTICAST "));
2052 if (ptr->flags & IFF_DYNAMIC)
2053 printf(_("DYNAMIC "));
2055 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2056 printf(_(" MTU:%d Metric:%d"),
2057 ptr->mtu, ptr->metric ? ptr->metric : 1);
2058 #ifdef SIOCSKEEPALIVE
2059 if (ptr->outfill || ptr->keepalive)
2060 printf(_(" Outfill:%d Keepalive:%d"),
2061 ptr->outfill, ptr->keepalive);
2065 /* If needed, display the interface statistics. */
2067 if (ptr->statistics_valid) {
2068 /* XXX: statistics are currently only printed for the primary address,
2069 * not for the aliases, although strictly speaking they're shared
2074 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
2075 ptr->stats.rx_packets, ptr->stats.rx_errors,
2076 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2077 ptr->stats.rx_frame_errors);
2079 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
2081 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
2082 ptr->stats.tx_packets, ptr->stats.tx_errors,
2083 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2084 ptr->stats.tx_carrier_errors);
2085 printf(_(" collisions:%lu "), ptr->stats.collisions);
2087 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2088 if (ptr->tx_queue_len != -1)
2089 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
2091 print_bytes_scaled(ptr->stats.rx_bytes, " T");
2092 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2096 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2097 ptr->map.base_addr)) {
2100 printf(_("Interrupt:%d "), ptr->map.irq);
2101 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2103 printf(_("Base address:0x%x "), ptr->map.base_addr);
2104 if (ptr->map.mem_start) {
2105 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2108 printf(_("DMA chan:%x "), ptr->map.dma);
2115 static int do_if_print(struct interface *ife, void *cookie)
2117 int *opt_a = (int *) cookie;
2120 res = do_if_fetch(ife);
2122 if ((ife->flags & IFF_UP) || *opt_a)
2128 static struct interface *lookup_interface(char *name)
2130 struct interface *ife = NULL;
2132 if (if_readlist_proc(name) < 0)
2134 ife = add_interface(name);
2138 /* for ipv4 add/del modes */
2139 static int if_print(char *ifname)
2144 res = for_all_interfaces(do_if_print, &interface_opt_a);
2146 struct interface *ife;
2148 ife = lookup_interface(ifname);
2149 res = do_if_fetch(ife);
2156 int display_interfaces(char *ifname)
2160 /* Create a channel to the NET kernel. */
2161 if ((skfd = sockets_open(0)) < 0) {
2162 perror_msg_and_die("socket");
2165 /* Do we have to show the current setup? */
2166 status = if_print(ifname);