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.4 2001/07/19 22:28:02 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 */
56 * Device Hardware types.
59 #define HAVE_HWETHER 1
65 #include <sys/types.h>
66 #include <sys/socket.h>
67 #include <sys/ioctl.h>
68 #include <netinet/in.h>
70 #include <net/if_arp.h>
79 #include <netinet/in.h>
80 #include <arpa/inet.h>
82 #include <arpa/nameser.h>
87 #define _PATH_PROCNET_DEV "/proc/net/dev"
88 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
89 #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
91 static int procnetdev_vsn = 1;
94 /* Ugh. But libc5 doesn't provide POSIX types. */
95 #include <asm/types.h>
99 #include <linux/if_slip.h>
106 * This is in linux/include/net/ipv6.h.
110 struct in6_addr ifr6_addr;
111 __u32 ifr6_prefixlen;
112 unsigned int ifr6_ifindex;
117 #endif /* HAVE_AFINET6 */
120 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
121 #include <netipx/ipx.h>
127 #include "net-support.h"
128 #include "pathnames.h"
131 #include "interface.h"
136 /* Defines for glibc2.0 users. */
137 #ifndef SIOCSIFTXQLEN
138 #define SIOCSIFTXQLEN 0x8943
139 #define SIOCGIFTXQLEN 0x8942
142 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
144 #define ifr_qlen ifr_ifru.ifru_mtu
147 #ifndef HAVE_TXQUEUELEN
148 #define HAVE_TXQUEUELEN 1
152 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
155 /* This structure defines protocol families and their handlers. */
161 char *(*print) (unsigned char *);
162 char *(*sprint) (struct sockaddr *, int numeric);
163 int (*input) (int type, char *bufp, struct sockaddr *);
164 void (*herror) (char *text);
165 int (*rprint) (int options);
166 int (*rinput) (int typ, int ext, char **argv);
169 int (*getmask) (char *src, struct sockaddr * mask, char *name);
175 static struct aftype *aftypes[];
184 static int flag_ax25;
190 static int flag_netrom;
192 static int flag_inet;
194 static int flag_inet6;
197 static int flag_econet;
200 static int flag_x25 = 0;
207 static struct aftrans_t {
215 "ax25", "ax25", &flag_ax25
219 "ip", "inet", &flag_inet
223 "ip6", "inet6", &flag_inet6
228 "ipx", "ipx", &flag_ipx
233 "appletalk", "ddp", &flag_ddp
238 "netrom", "netrom", &flag_netrom
242 "inet", "inet", &flag_inet
246 "inet6", "inet6", &flag_inet6
251 "ddp", "ddp", &flag_ddp
255 "unix", "unix", &flag_unx
258 "tcpip", "inet", &flag_inet
262 "econet", "ec", &flag_econet
267 "x25", "x25", &flag_x25
272 "ash", "ash", &flag_ash
280 static char afname[256] = "";
281 #endif /* KEEP_UNUSED */
285 /* Display a UNIX domain address. */
286 static char *UNIX_print(unsigned char *ptr)
292 /* Display a UNIX domain address. */
293 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
297 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
298 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
299 return (UNIX_print(sap->sa_data));
303 static struct aftype unix_aftype =
305 "unix", "UNIX Domain", AF_UNIX, 0,
306 UNIX_print, UNIX_sprint, NULL, NULL,
311 #endif /* HAVE_AFUNIX */
316 extern int h_errno; /* some netdb.h versions don't export this */
321 struct sockaddr_in addr;
327 static struct addr *INET_nn = NULL; /* addr-to-name cache */
330 static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
336 sin->sin_family = AF_INET;
339 /* Default is special, meaning 0.0.0.0. */
340 if (!strcmp(name, "default")) {
341 sin->sin_addr.s_addr = INADDR_ANY;
344 /* Look to see if it's a dotted quad. */
345 if (inet_aton(name, &sin->sin_addr)) {
348 /* If we expect this to be a hostname, try hostname database first */
350 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
353 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
354 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
355 sizeof(struct in_addr));
358 /* Try the NETWORKS database to see if this is a known network. */
360 fprintf (stderr, "getnetbyname (%s)\n", name);
362 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
363 sin->sin_addr.s_addr = htonl(np->n_net);
367 /* Don't try again */
373 _res.options |= RES_DEBUG;
377 fprintf (stderr, "gethostbyname (%s)\n", name);
379 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
383 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
384 sizeof(struct in_addr));
388 #endif /* KEEP_UNUSED */
390 /* numeric: & 0x8000: default instead of *,
391 * & 0x4000: host instead of net,
392 * & 0x0fff: don't resolve
394 static int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
395 int numeric, unsigned int netmask)
400 unsigned long ad, host_ad;
404 if (s_in->sin_family != AF_INET) {
406 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
408 errno = EAFNOSUPPORT;
411 ad = (unsigned long) s_in->sin_addr.s_addr;
413 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
415 if (ad == INADDR_ANY) {
416 if ((numeric & 0x0FFF) == 0) {
417 if (numeric & 0x8000)
418 safe_strncpy(name, "default", len);
420 safe_strncpy(name, "*", len);
424 if (numeric & 0x0FFF) {
425 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
429 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
436 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
437 safe_strncpy(name, pn->name, len);
439 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
451 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
453 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
455 safe_strncpy(name, ent->h_name, len);
458 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
461 np = getnetbyaddr(host_ad, AF_INET);
463 safe_strncpy(name, np->n_name, len);
466 if ((ent == NULL) && (np == NULL))
467 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
468 pn = (struct addr *) xmalloc(sizeof(struct addr));
472 pn->name = (char *) xmalloc(strlen(name) + 1);
473 strcpy(pn->name, name);
480 static void INET_reserror(char *text)
485 /* Display an Internet socket address. */
486 static char *INET_print(unsigned char *ptr)
488 return (inet_ntoa((*(struct in_addr *) ptr)));
490 #endif /* KEEP_UNUSED */
492 /* Display an Internet socket address. */
493 static char *INET_sprint(struct sockaddr *sap, int numeric)
495 static char buff[128];
497 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
498 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
500 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
501 numeric, 0xffffff00) != 0)
508 static char *INET_sprintmask(struct sockaddr *sap, int numeric,
509 unsigned int netmask)
511 static char buff[128];
513 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
514 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
515 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
516 numeric, netmask) != 0)
521 static int INET_getsock(char *bufp, struct sockaddr *sap)
523 char *sp = bufp, *bp;
526 struct sockaddr_in *sin;
528 sin = (struct sockaddr_in *) sap;
529 sin->sin_family = AF_INET;
534 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
537 if ((*sp >= 'A') && (*sp <= 'F'))
538 bp[i] |= (int) (*sp - 'A') + 10;
539 else if ((*sp >= '0') && (*sp <= '9'))
540 bp[i] |= (int) (*sp - '0');
548 if ((*sp >= 'A') && (*sp <= 'F'))
549 bp[i] |= (int) (*sp - 'A') + 10;
550 else if ((*sp >= '0') && (*sp <= '9'))
551 bp[i] |= (int) (*sp - '0');
557 sin->sin_addr.s_addr = htonl(val);
562 static int INET_input(int type, char *bufp, struct sockaddr *sap)
566 return (INET_getsock(bufp, sap));
568 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
570 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
574 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
576 struct sockaddr_in *mask = (struct sockaddr_in *) m;
580 if ((slash = strchr(adr, '/')) == NULL)
584 prefix = strtoul(slash, &end, 0);
589 sprintf(name, "/%d", prefix);
591 mask->sin_family = AF_INET;
592 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
595 #endif /* KEEP_UNUSED */
597 static struct aftype inet_aftype =
599 "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
600 NULL /* UNUSED INET_print */, INET_sprint,
601 NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */,
602 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
603 NULL /* UNUSED INET_getnetmask */,
608 #endif /* HAVE_AFINET */
610 /* Display an UNSPEC address. */
611 static char *UNSPEC_print(unsigned char *ptr)
613 static char buff[sizeof(struct sockaddr)*3+1];
618 for (i = 0; i < sizeof(struct sockaddr); i++) {
619 /* careful -- not every libc's sprintf returns # bytes written */
620 sprintf(pos, "%02X-", (*ptr++ & 0377));
623 /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
628 /* Display an UNSPEC socket address. */
629 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
633 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
634 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
635 return (UNSPEC_print(sap->sa_data));
638 static struct aftype unspec_aftype =
640 "unspec", "UNSPEC", AF_UNSPEC, 0,
641 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
645 static struct aftype *aftypes[] =
685 static short sVafinit = 0;
689 unspec_aftype.title = _("UNSPEC");
691 unix_aftype.title = _("UNIX Domain");
694 inet_aftype.title = _("DARPA Internet");
697 inet6_aftype.title = _("IPv6");
700 ax25_aftype.title = _("AMPR AX.25");
703 netrom_aftype.title = _("AMPR NET/ROM");
706 ipx_aftype.title = _("Novell IPX");
709 ddp_aftype.title = _("Appletalk DDP");
712 ec_aftype.title = _("Econet");
715 x25_aftype.title = _("CCITT X.25");
718 rose_aftype.title = _("AMPR ROSE");
721 ash_aftype.title = _("Ash");
726 static int aftrans_opt(const char *arg)
728 struct aftrans_t *paft;
732 safe_strncpy(buf, arg, sizeof(buf));
738 tmp2 = strchr(tmp1, ',');
744 for (paft = aftrans; paft->alias; paft++) {
745 if (strcmp(tmp1, paft->alias))
747 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
748 fprintf(stderr, _("Too much address family arguments.\n"));
755 strcat(afname, paft->name);
759 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
768 /* set the default AF list from the program name or a constant value */
769 static void aftrans_def(char *tool, char *argv0, char *dflt)
774 strcpy(afname, dflt);
776 if (!(tmp = strrchr(argv0, '/')))
777 tmp = argv0; /* no slash?! */
781 if (!(buf = strdup(tmp)))
784 if (strlen(tool) >= strlen(tmp)) {
788 tmp = buf + (strlen(tmp) - strlen(tool));
790 if (strcmp(tmp, tool) != 0) {
795 if ((tmp = strchr(buf, '_')))
799 if (aftrans_opt(buf))
805 /* Check our protocol family table for this family. */
806 static struct aftype *get_aftype(const char *name)
813 #endif /* KEEP_UNUSED */
816 while (*afp != NULL) {
817 if (!strcmp((*afp)->name, name))
821 if (strchr(name, ','))
822 fprintf(stderr, _("Please don't supply more than one address family.\n"));
825 #endif /* KEEP_UNUSED */
827 /* Check our protocol family table for this family. */
828 static struct aftype *get_afntype(int af)
835 #endif /* KEEP_UNUSED */
838 while (*afp != NULL) {
839 if ((*afp)->af == af)
846 /* Check our protocol family table for this family and return its socket */
847 static int get_socket_for_af(int af)
854 #endif /* KEEP_UNUSED */
857 while (*afp != NULL) {
858 if ((*afp)->af == af)
866 /* type: 0=all, 1=getroute */
867 static void print_aflist(int type) {
875 #endif /* KEEP_UNUSED */
878 while (*afp != NULL) {
879 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
882 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
883 txt = (*afp)->name; if (!txt) txt = "..";
884 fprintf(stderr,"%s (%s) ",txt,_((*afp)->title));
888 fprintf(stderr,"\n");
890 #endif /* KEEP_UNUSED */
892 struct user_net_device_stats {
893 unsigned long long rx_packets; /* total packets received */
894 unsigned long long tx_packets; /* total packets transmitted */
895 unsigned long long rx_bytes; /* total bytes received */
896 unsigned long long tx_bytes; /* total bytes transmitted */
897 unsigned long rx_errors; /* bad packets received */
898 unsigned long tx_errors; /* packet transmit problems */
899 unsigned long rx_dropped; /* no space in linux buffers */
900 unsigned long tx_dropped; /* no space available in linux */
901 unsigned long rx_multicast; /* multicast packets received */
902 unsigned long rx_compressed;
903 unsigned long tx_compressed;
904 unsigned long collisions;
906 /* detailed rx_errors: */
907 unsigned long rx_length_errors;
908 unsigned long rx_over_errors; /* receiver ring buff overflow */
909 unsigned long rx_crc_errors; /* recved pkt with crc error */
910 unsigned long rx_frame_errors; /* recv'd frame alignment error */
911 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
912 unsigned long rx_missed_errors; /* receiver missed packet */
913 /* detailed tx_errors */
914 unsigned long tx_aborted_errors;
915 unsigned long tx_carrier_errors;
916 unsigned long tx_fifo_errors;
917 unsigned long tx_heartbeat_errors;
918 unsigned long tx_window_errors;
922 struct interface *next, *prev;
923 char name[IFNAMSIZ]; /* interface name */
924 short type; /* if type */
925 short flags; /* various flags */
926 int metric; /* routing metric */
927 int mtu; /* MTU value */
928 int tx_queue_len; /* transmit queue length */
929 struct ifmap map; /* hardware setup */
930 struct sockaddr addr; /* IP address */
931 struct sockaddr dstaddr; /* P-P IP address */
932 struct sockaddr broadaddr; /* IP broadcast address */
933 struct sockaddr netmask; /* IP network mask */
934 struct sockaddr ipxaddr_bb; /* IPX network address */
935 struct sockaddr ipxaddr_sn; /* IPX network address */
936 struct sockaddr ipxaddr_e3; /* IPX network address */
937 struct sockaddr ipxaddr_e2; /* IPX network address */
938 struct sockaddr ddpaddr; /* Appletalk DDP address */
939 struct sockaddr ecaddr; /* Econet address */
948 char hwaddr[32]; /* HW address */
949 int statistics_valid;
950 struct user_net_device_stats stats; /* statistics */
951 int keepalive; /* keepalive value for SLIP */
952 int outfill; /* outfill value for SLIP */
956 int interface_opt_a = 0; /* show all interfaces */
959 static int opt_i = 0; /* show the statistics */
960 static int opt_v = 0; /* debugging output flag */
962 static int addr_family = 0; /* currently selected AF */
963 #endif /* KEEP_UNUSED */
965 static struct interface *int_list, *int_last;
966 static int skfd = -1; /* generic raw socket desc. */
969 static int sockets_open(int family)
973 static int force = -1;
977 if (get_kernel_revision() < KRELEASE(2, 1, 0))
979 if (access("/proc/net", R_OK))
982 for (aft = aftypes; *aft; aft++) {
983 struct aftype *af = *aft;
984 int type = SOCK_DGRAM;
985 if (af->af == AF_UNSPEC)
987 if (family && family != af->af)
993 /* Check some /proc file first to not stress kmod */
994 if (!family && !force && af->flag_file) {
995 if (access(af->flag_file, R_OK))
999 if (af->af == AF_NETROM)
1000 type = SOCK_SEQPACKET;
1003 if (af->af == AF_X25)
1004 type = SOCK_SEQPACKET;
1006 af->fd = socket(af->af, type, 0);
1011 fprintf(stderr, _("No usable address families found.\n"));
1015 /* like strcmp(), but knows about numbers */
1016 static int nstrcmp(const char *astr, const char *b)
1018 const char *a = astr;
1039 return atoi(a) > atoi(b) ? 1 : -1;
1044 static struct interface *add_interface(char *name)
1046 struct interface *ife, **nextp, *new;
1048 for (ife = int_last; ife; ife = ife->prev) {
1049 int n = nstrcmp(ife->name, name);
1056 safe_strncpy(new->name, name, IFNAMSIZ);
1057 nextp = ife ? &ife->next : &int_list;
1061 new->next->prev = new;
1069 static int if_readconf(void)
1077 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
1079 skfd2 = get_socket_for_af(AF_INET);
1081 fprintf(stderr, _("warning: no inet socket available: %s\n"),
1083 /* Try to soldier on with whatever socket we can get hold of. */
1084 skfd2 = sockets_open(0);
1091 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1092 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1094 if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
1095 perror("SIOCGIFCONF");
1098 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1099 /* assume it overflowed and try again */
1107 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1108 add_interface(ifr->ifr_name);
1118 static char *get_name(char *name, char *p)
1125 if (*p == ':') { /* could be an alias */
1126 char *dot = p, *dotname = name;
1130 if (*p != ':') { /* it wasn't, backup */
1145 static int get_dev_fields(char *bp, struct interface *ife)
1147 switch (procnetdev_vsn) {
1150 "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu",
1151 &ife->stats.rx_bytes,
1152 &ife->stats.rx_packets,
1153 &ife->stats.rx_errors,
1154 &ife->stats.rx_dropped,
1155 &ife->stats.rx_fifo_errors,
1156 &ife->stats.rx_frame_errors,
1157 &ife->stats.rx_compressed,
1158 &ife->stats.rx_multicast,
1160 &ife->stats.tx_bytes,
1161 &ife->stats.tx_packets,
1162 &ife->stats.tx_errors,
1163 &ife->stats.tx_dropped,
1164 &ife->stats.tx_fifo_errors,
1165 &ife->stats.collisions,
1166 &ife->stats.tx_carrier_errors,
1167 &ife->stats.tx_compressed);
1170 sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu",
1171 &ife->stats.rx_bytes,
1172 &ife->stats.rx_packets,
1173 &ife->stats.rx_errors,
1174 &ife->stats.rx_dropped,
1175 &ife->stats.rx_fifo_errors,
1176 &ife->stats.rx_frame_errors,
1178 &ife->stats.tx_bytes,
1179 &ife->stats.tx_packets,
1180 &ife->stats.tx_errors,
1181 &ife->stats.tx_dropped,
1182 &ife->stats.tx_fifo_errors,
1183 &ife->stats.collisions,
1184 &ife->stats.tx_carrier_errors);
1185 ife->stats.rx_multicast = 0;
1188 sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu",
1189 &ife->stats.rx_packets,
1190 &ife->stats.rx_errors,
1191 &ife->stats.rx_dropped,
1192 &ife->stats.rx_fifo_errors,
1193 &ife->stats.rx_frame_errors,
1195 &ife->stats.tx_packets,
1196 &ife->stats.tx_errors,
1197 &ife->stats.tx_dropped,
1198 &ife->stats.tx_fifo_errors,
1199 &ife->stats.collisions,
1200 &ife->stats.tx_carrier_errors);
1201 ife->stats.rx_bytes = 0;
1202 ife->stats.tx_bytes = 0;
1203 ife->stats.rx_multicast = 0;
1209 static inline int procnetdev_version(char *buf)
1211 if (strstr(buf, "compressed"))
1213 if (strstr(buf, "bytes"))
1218 static int if_readlist_proc(char *target)
1220 static int proc_read;
1223 struct interface *ife;
1231 fh = fopen(_PATH_PROCNET_DEV, "r");
1233 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1234 _PATH_PROCNET_DEV, strerror(errno));
1235 return if_readconf();
1237 fgets(buf, sizeof buf, fh); /* eat line */
1238 fgets(buf, sizeof buf, fh);
1240 #if 0 /* pretty, but can't cope with missing fields */
1241 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1242 "face", "", /* parsed separately */
1249 "compressed", "%lu",
1258 "compressed", "%lu",
1263 procnetdev_vsn = procnetdev_version(buf);
1267 while (fgets(buf, sizeof buf, fh)) {
1268 char *s, name[IFNAMSIZ];
1269 s = get_name(name, buf);
1270 ife = add_interface(name);
1271 get_dev_fields(s, ife);
1272 ife->statistics_valid = 1;
1273 if (target && !strcmp(target,name))
1277 perror(_PATH_PROCNET_DEV);
1289 static int if_readlist(void)
1291 int err = if_readlist_proc(NULL);
1293 err = if_readconf();
1297 static int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1299 struct interface *ife;
1301 if (!int_list && (if_readlist() < 0))
1303 for (ife = int_list; ife; ife = ife->next) {
1304 int err = doit(ife, cookie);
1311 /* Support for fetching an IPX address */
1314 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1316 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1317 return ioctl(sock, SIOCGIFADDR, ifr);
1322 /* Fetch the interface configuration from the kernel. */
1323 static int if_fetch(struct interface *ife)
1327 char *ifname = ife->name;
1329 strcpy(ifr.ifr_name, ifname);
1330 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1332 ife->flags = ifr.ifr_flags;
1334 strcpy(ifr.ifr_name, ifname);
1335 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1336 memset(ife->hwaddr, 0, 32);
1338 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1340 ife->type = ifr.ifr_hwaddr.sa_family;
1342 strcpy(ifr.ifr_name, ifname);
1343 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1346 ife->metric = ifr.ifr_metric;
1348 strcpy(ifr.ifr_name, ifname);
1349 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1352 ife->mtu = ifr.ifr_mtu;
1355 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1356 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1357 ife->type == ARPHRD_ADAPT) {
1359 strcpy(ifr.ifr_name, ifname);
1360 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1363 ife->outfill = (unsigned int) ifr.ifr_data;
1365 #ifdef SIOCGKEEPALIVE
1366 strcpy(ifr.ifr_name, ifname);
1367 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1370 ife->keepalive = (unsigned int) ifr.ifr_data;
1375 strcpy(ifr.ifr_name, ifname);
1376 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1377 memset(&ife->map, 0, sizeof(struct ifmap));
1379 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1381 strcpy(ifr.ifr_name, ifname);
1382 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1383 memset(&ife->map, 0, sizeof(struct ifmap));
1385 ife->map = ifr.ifr_map;
1387 #ifdef HAVE_TXQUEUELEN
1388 strcpy(ifr.ifr_name, ifname);
1389 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1390 ife->tx_queue_len = -1; /* unknown value */
1392 ife->tx_queue_len = ifr.ifr_qlen;
1394 ife->tx_queue_len = -1; /* unknown value */
1399 fd = get_socket_for_af(AF_INET);
1401 strcpy(ifr.ifr_name, ifname);
1402 ifr.ifr_addr.sa_family = AF_INET;
1403 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1405 ife->addr = ifr.ifr_addr;
1406 strcpy(ifr.ifr_name, ifname);
1407 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1408 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1410 ife->dstaddr = ifr.ifr_dstaddr;
1412 strcpy(ifr.ifr_name, ifname);
1413 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1414 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1416 ife->broadaddr = ifr.ifr_broadaddr;
1418 strcpy(ifr.ifr_name, ifname);
1419 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1420 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1422 ife->netmask = ifr.ifr_netmask;
1424 memset(&ife->addr, 0, sizeof(struct sockaddr));
1429 /* DDP address maybe ? */
1430 fd = get_socket_for_af(AF_APPLETALK);
1432 strcpy(ifr.ifr_name, ifname);
1433 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1434 ife->ddpaddr = ifr.ifr_addr;
1441 /* Look for IPX addresses with all framing types */
1442 fd = get_socket_for_af(AF_IPX);
1444 strcpy(ifr.ifr_name, ifname);
1445 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1446 ife->has_ipx_bb = 1;
1447 ife->ipxaddr_bb = ifr.ifr_addr;
1449 strcpy(ifr.ifr_name, ifname);
1450 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1451 ife->has_ipx_sn = 1;
1452 ife->ipxaddr_sn = ifr.ifr_addr;
1454 strcpy(ifr.ifr_name, ifname);
1455 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1456 ife->has_ipx_e3 = 1;
1457 ife->ipxaddr_e3 = ifr.ifr_addr;
1459 strcpy(ifr.ifr_name, ifname);
1460 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1461 ife->has_ipx_e2 = 1;
1462 ife->ipxaddr_e2 = ifr.ifr_addr;
1468 /* Econet address maybe? */
1469 fd = get_socket_for_af(AF_ECONET);
1471 strcpy(ifr.ifr_name, ifname);
1472 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1473 ife->ecaddr = ifr.ifr_addr;
1474 ife->has_econet = 1;
1483 static int do_if_fetch(struct interface *ife)
1485 if (if_fetch(ife) < 0) {
1487 if (errno == ENODEV) {
1488 /* Give better error message for this case. */
1489 errmsg = _("Device not found");
1491 errmsg = strerror(errno);
1493 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1500 /* This structure defines hardware protocols and their handlers. */
1506 char *(*print) (unsigned char *);
1507 int (*input) (char *, struct sockaddr *);
1508 int (*activate) (int fd);
1509 int suppress_null_addr;
1512 static struct hwtype unspec_hwtype =
1514 "unspec", "UNSPEC", -1, 0,
1515 UNSPEC_print, NULL, NULL
1518 static struct hwtype loop_hwtype =
1520 "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
1525 #include <net/if_arp.h>
1526 #include <linux/if_ether.h>
1528 static struct hwtype ether_hwtype;
1530 /* Display an Ethernet address in readable format. */
1531 static char *pr_ether(unsigned char *ptr)
1533 static char buff[64];
1535 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1536 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1537 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1543 /* Input an Ethernet address and convert to binary. */
1544 static int in_ether(char *bufp, struct sockaddr *sap)
1551 sap->sa_family = ether_hwtype.type;
1556 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1561 else if (c >= 'a' && c <= 'f')
1563 else if (c >= 'A' && c <= 'F')
1567 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1576 else if (c >= 'a' && c <= 'f')
1577 val |= c - 'a' + 10;
1578 else if (c >= 'A' && c <= 'F')
1579 val |= c - 'A' + 10;
1580 else if (c == ':' || c == 0)
1584 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1591 *ptr++ = (unsigned char) (val & 0377);
1594 /* We might get a semicolon here - not required. */
1596 if (i == ETH_ALEN) {
1598 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1607 /* That's it. Any trailing junk? */
1608 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1610 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1616 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1621 #endif /* KEEP_UNUSED */
1624 static struct hwtype ether_hwtype =
1626 "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
1627 pr_ether, NULL /* UNUSED in_ether */, NULL
1631 #endif /* HAVE_HWETHER */
1636 #include <net/if_arp.h>
1639 /* Start the PPP encapsulation on the file descriptor. */
1640 static int do_ppp(int fd)
1642 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1645 #endif /* KEEP_UNUSED */
1647 static struct hwtype ppp_hwtype =
1649 "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
1650 NULL, NULL, NULL /* UNUSED do_ppp */, 0
1654 #endif /* HAVE_PPP */
1656 static struct hwtype *hwtypes[] =
1679 #ifdef ARPHRD_IEEE802_TR
1732 static short sVhwinit = 0;
1734 static void hwinit()
1736 loop_hwtype.title = _("Local Loopback");
1737 unspec_hwtype.title = _("UNSPEC");
1739 slip_hwtype.title = _("Serial Line IP");
1740 cslip_hwtype.title = _("VJ Serial Line IP");
1741 slip6_hwtype.title = _("6-bit Serial Line IP");
1742 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1743 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1746 ether_hwtype.title = _("Ethernet");
1749 ash_hwtype.title = _("Ash");
1752 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1755 hippi_hwtype.title = _("HIPPI");
1758 ax25_hwtype.title = _("AMPR AX.25");
1761 rose_hwtype.title = _("AMPR ROSE");
1764 netrom_hwtype.title = _("AMPR NET/ROM");
1767 x25_hwtype.title = _("generic X.25");
1770 tunnel_hwtype.title = _("IPIP Tunnel");
1773 ppp_hwtype.title = _("Point-to-Point Protocol");
1776 hdlc_hwtype.title = _("(Cisco)-HDLC");
1777 lapb_hwtype.title = _("LAPB");
1780 arcnet_hwtype.title = _("ARCnet");
1783 dlci_hwtype.title = _("Frame Relay DLCI");
1784 frad_hwtype.title = _("Frame Relay Access Device");
1787 sit_hwtype.title = _("IPv6-in-IPv4");
1790 irda_hwtype.title = _("IrLAP");
1793 tr_hwtype.title = _("16/4 Mbps Token Ring");
1794 #ifdef ARPHRD_IEEE802_TR
1795 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1799 ec_hwtype.title = _("Econet");
1803 #endif /* KEEP_UNUSED */
1806 static const char *if_port_text[][4] =
1808 /* Keep in step with <linux/netdevice.h> */
1809 {"unknown", NULL, NULL, NULL},
1810 {"10base2", "bnc", "coax", NULL},
1811 {"10baseT", "utp", "tpe", NULL},
1812 {"AUI", "thick", "db15", NULL},
1813 {"100baseT", NULL, NULL, NULL},
1814 {"100baseTX", NULL, NULL, NULL},
1815 {"100baseFX", NULL, NULL, NULL},
1816 {NULL, NULL, NULL, NULL},
1820 /* Check our hardware type table for this type. */
1821 static struct hwtype *get_hwntype(int type)
1823 struct hwtype **hwp;
1828 #endif /* KEEP_UNUSED */
1831 while (*hwp != NULL) {
1832 if ((*hwp)->type == type)
1839 /* return 1 if address is all zeros */
1840 static int hw_null_address(struct hwtype *hw, void *ap)
1843 unsigned char *address = (unsigned char *)ap;
1844 for (i = 0; i < hw->alen; i++)
1850 static const char TRext[] = "\0\0k\0M";
1852 static void print_bytes_scaled(unsigned long long ull, const char *end)
1854 unsigned long long int_part;
1855 unsigned long frac_part;
1862 for (i=0 ; i<2 ; i++) {
1863 if (int_part >= 1024) {
1864 frac_part = ((int_part % 1024) * 10) / 1024;
1866 ext += 2; /* Kb, Mb */
1870 printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);
1873 static void ife_print(struct interface *ptr)
1878 int can_compress = 0;
1881 static struct aftype *ipxtype = NULL;
1884 static struct aftype *ectype = NULL;
1887 static struct aftype *ddptype = NULL;
1891 char addr6[40], devname[20];
1892 struct sockaddr_in6 sap;
1893 int plen, scope, dad_status, if_idx;
1894 extern struct aftype inet6_aftype;
1898 ap = get_afntype(ptr->addr.sa_family);
1900 ap = get_afntype(0);
1904 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1907 hw = get_hwntype(hf);
1909 hw = get_hwntype(-1);
1911 printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
1912 /* For some hardware types (eg Ash, ATM) we don't print the
1913 hardware address if it's null. */
1914 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1915 hw->suppress_null_addr)))
1916 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1918 if (ptr->flags & IFF_PORTSEL) {
1919 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1920 if (ptr->flags & IFF_AUTOMEDIA)
1921 printf(_("(auto)"));
1928 printf(_(" %s addr:%s "), ap->name,
1929 ap->sprint(&ptr->addr, 1));
1930 if (ptr->flags & IFF_POINTOPOINT) {
1931 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1933 if (ptr->flags & IFF_BROADCAST) {
1934 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1936 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1941 /* FIXME: should be integrated into interface.c. */
1943 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1944 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1945 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1946 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1947 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1948 if (!strcmp(devname, ptr->name)) {
1949 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1950 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1951 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1952 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1953 printf(_(" inet6 addr: %s/%d"),
1954 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1955 printf(_(" Scope:"));
1958 printf(_("Global"));
1960 case IPV6_ADDR_LINKLOCAL:
1963 case IPV6_ADDR_SITELOCAL:
1966 case IPV6_ADDR_COMPATv4:
1967 printf(_("Compat"));
1969 case IPV6_ADDR_LOOPBACK:
1973 printf(_("Unknown"));
1983 if (ipxtype == NULL)
1984 ipxtype = get_afntype(AF_IPX);
1986 if (ipxtype != NULL) {
1987 if (ptr->has_ipx_bb)
1988 printf(_(" IPX/Ethernet II addr:%s\n"),
1989 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1990 if (ptr->has_ipx_sn)
1991 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1992 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1993 if (ptr->has_ipx_e2)
1994 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1995 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1996 if (ptr->has_ipx_e3)
1997 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1998 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
2003 if (ddptype == NULL)
2004 ddptype = get_afntype(AF_APPLETALK);
2005 if (ddptype != NULL) {
2007 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
2013 ectype = get_afntype(AF_ECONET);
2014 if (ectype != NULL) {
2015 if (ptr->has_econet)
2016 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
2021 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
2022 if (ptr->flags == 0)
2023 printf(_("[NO FLAGS] "));
2024 if (ptr->flags & IFF_UP)
2026 if (ptr->flags & IFF_BROADCAST)
2027 printf(_("BROADCAST "));
2028 if (ptr->flags & IFF_DEBUG)
2029 printf(_("DEBUG "));
2030 if (ptr->flags & IFF_LOOPBACK)
2031 printf(_("LOOPBACK "));
2032 if (ptr->flags & IFF_POINTOPOINT)
2033 printf(_("POINTOPOINT "));
2034 if (ptr->flags & IFF_NOTRAILERS)
2035 printf(_("NOTRAILERS "));
2036 if (ptr->flags & IFF_RUNNING)
2037 printf(_("RUNNING "));
2038 if (ptr->flags & IFF_NOARP)
2039 printf(_("NOARP "));
2040 if (ptr->flags & IFF_PROMISC)
2041 printf(_("PROMISC "));
2042 if (ptr->flags & IFF_ALLMULTI)
2043 printf(_("ALLMULTI "));
2044 if (ptr->flags & IFF_SLAVE)
2045 printf(_("SLAVE "));
2046 if (ptr->flags & IFF_MASTER)
2047 printf(_("MASTER "));
2048 if (ptr->flags & IFF_MULTICAST)
2049 printf(_("MULTICAST "));
2051 if (ptr->flags & IFF_DYNAMIC)
2052 printf(_("DYNAMIC "));
2054 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
2055 printf(_(" MTU:%d Metric:%d"),
2056 ptr->mtu, ptr->metric ? ptr->metric : 1);
2057 #ifdef SIOCSKEEPALIVE
2058 if (ptr->outfill || ptr->keepalive)
2059 printf(_(" Outfill:%d Keepalive:%d"),
2060 ptr->outfill, ptr->keepalive);
2064 /* If needed, display the interface statistics. */
2066 if (ptr->statistics_valid) {
2067 /* XXX: statistics are currently only printed for the primary address,
2068 * not for the aliases, although strictly speaking they're shared
2073 printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
2074 ptr->stats.rx_packets, ptr->stats.rx_errors,
2075 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
2076 ptr->stats.rx_frame_errors);
2078 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
2080 printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
2081 ptr->stats.tx_packets, ptr->stats.tx_errors,
2082 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
2083 ptr->stats.tx_carrier_errors);
2084 printf(_(" collisions:%lu "), ptr->stats.collisions);
2086 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
2087 if (ptr->tx_queue_len != -1)
2088 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
2090 print_bytes_scaled(ptr->stats.rx_bytes, " T");
2091 print_bytes_scaled(ptr->stats.tx_bytes, "\n");
2095 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2096 ptr->map.base_addr)) {
2099 printf(_("Interrupt:%d "), ptr->map.irq);
2100 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2102 printf(_("Base address:0x%x "), ptr->map.base_addr);
2103 if (ptr->map.mem_start) {
2104 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2107 printf(_("DMA chan:%x "), ptr->map.dma);
2114 static int do_if_print(struct interface *ife, void *cookie)
2116 int *opt_a = (int *) cookie;
2119 res = do_if_fetch(ife);
2121 if ((ife->flags & IFF_UP) || *opt_a)
2127 static struct interface *lookup_interface(char *name)
2129 struct interface *ife = NULL;
2131 if (if_readlist_proc(name) < 0)
2133 ife = add_interface(name);
2137 /* for ipv4 add/del modes */
2138 static int if_print(char *ifname)
2143 res = for_all_interfaces(do_if_print, &interface_opt_a);
2145 struct interface *ife;
2147 ife = lookup_interface(ifname);
2148 res = do_if_fetch(ife);
2155 int display_interfaces(char *ifname)
2159 /* Create a channel to the NET kernel. */
2160 if ((skfd = sockets_open(0)) < 0) {
2161 perror_msg_and_die("socket");
2164 /* Do we have to show the current setup? */
2165 status = if_print(ifname);