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/03/06 00:48:59 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>
46 * Device Hardware types.
49 #define HAVE_HWETHER 1
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
58 #include <netinet/in.h>
60 #include <net/if_arp.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <arpa/nameser.h>
74 #define _PATH_PROCNET_DEV "/proc/net/dev"
75 #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
76 #define xmalloc malloc
77 #define xcalloc calloc
78 #define xrealloc realloc
79 #define KRELEASE(maj,min,patch) ((maj) * 10000 + (min)*1000 + (patch))
81 int procnetdev_vsn = 1;
84 /* Ugh. But libc5 doesn't provide POSIX types. */
85 #include <asm/types.h>
89 #include <linux/if_slip.h>
96 * This is in linux/include/net/ipv6.h.
100 struct in6_addr ifr6_addr;
101 __u32 ifr6_prefixlen;
102 unsigned int ifr6_ifindex;
107 #endif /* HAVE_AFINET6 */
110 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
111 #include <netipx/ipx.h>
117 #include "net-support.h"
118 #include "pathnames.h"
121 #include "interface.h"
126 /* This structure defines protocol families and their handlers. */
132 char *(*print) (unsigned char *);
133 char *(*sprint) (struct sockaddr *, int numeric);
134 int (*input) (int type, char *bufp, struct sockaddr *);
135 void (*herror) (char *text);
136 int (*rprint) (int options);
137 int (*rinput) (int typ, int ext, char **argv);
140 int (*getmask) (char *src, struct sockaddr * mask, char *name);
146 extern struct aftype *aftypes[];
166 "ax25", "ax25", &flag_ax25
169 "ip", "inet", &flag_inet
172 "ip6", "inet6", &flag_inet6
175 "ipx", "ipx", &flag_ipx
178 "appletalk", "ddp", &flag_ddp
181 "netrom", "netrom", &flag_netrom
184 "inet", "inet", &flag_inet
187 "inet6", "inet6", &flag_inet6
190 "ddp", "ddp", &flag_ddp
193 "unix", "unix", &flag_unx
196 "tcpip", "inet", &flag_inet
199 "econet", "ec", &flag_econet
202 "x25", "x25", &flag_x25
205 "ash", "ash", &flag_ash
212 char afname[256] = "";
216 /* Display a UNIX domain address. */
217 static char *UNIX_print(unsigned char *ptr)
223 /* Display a UNIX domain address. */
224 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
228 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
229 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
230 return (UNIX_print(sap->sa_data));
234 struct aftype unix_aftype =
236 "unix", NULL, /*"UNIX Domain", */ AF_UNIX, 0,
237 UNIX_print, UNIX_sprint, NULL, NULL,
242 #endif /* HAVE_AFUNIX */
246 extern int h_errno; /* some netdb.h versions don't export this */
250 struct sockaddr_in addr;
256 static struct addr *INET_nn = NULL; /* addr-to-name cache */
258 static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
264 sin->sin_family = AF_INET;
267 /* Default is special, meaning 0.0.0.0. */
268 if (!strcmp(name, "default")) {
269 sin->sin_addr.s_addr = INADDR_ANY;
272 /* Look to see if it's a dotted quad. */
273 if (inet_aton(name, &sin->sin_addr)) {
276 /* If we expect this to be a hostname, try hostname database first */
278 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
281 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
282 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
283 sizeof(struct in_addr));
286 /* Try the NETWORKS database to see if this is a known network. */
288 fprintf (stderr, "getnetbyname (%s)\n", name);
290 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
291 sin->sin_addr.s_addr = htonl(np->n_net);
295 /* Don't try again */
301 _res.options |= RES_DEBUG;
305 fprintf (stderr, "gethostbyname (%s)\n", name);
307 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
311 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
312 sizeof(struct in_addr));
318 /* numeric: & 0x8000: default instead of *,
319 * & 0x4000: host instead of net,
320 * & 0x0fff: don't resolve
322 static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
323 int numeric, unsigned int netmask)
328 unsigned long ad, host_ad;
332 if (sin->sin_family != AF_INET) {
334 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), sin->sin_family);
336 errno = EAFNOSUPPORT;
339 ad = (unsigned long) sin->sin_addr.s_addr;
341 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
343 if (ad == INADDR_ANY) {
344 if ((numeric & 0x0FFF) == 0) {
345 if (numeric & 0x8000)
346 safe_strncpy(name, "default", len);
348 safe_strncpy(name, "*", len);
352 if (numeric & 0x0FFF) {
353 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
357 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
364 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
365 safe_strncpy(name, pn->name, len);
367 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
379 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
381 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
383 safe_strncpy(name, ent->h_name, len);
386 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
388 np = getnetbyaddr(host_ad, AF_INET);
390 safe_strncpy(name, np->n_name, len);
392 if ((ent == NULL) && (np == NULL))
393 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
394 pn = (struct addr *) malloc(sizeof(struct addr));
398 pn->name = (char *) malloc(strlen(name) + 1);
399 strcpy(pn->name, name);
406 static void INET_reserror(char *text)
412 /* Display an Internet socket address. */
413 static char *INET_print(unsigned char *ptr)
415 return (inet_ntoa((*(struct in_addr *) ptr)));
419 /* Display an Internet socket address. */
420 static char *INET_sprint(struct sockaddr *sap, int numeric)
422 static char buff[128];
424 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
425 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
427 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
428 numeric, 0xffffff00) != 0)
434 char *INET_sprintmask(struct sockaddr *sap, int numeric,
435 unsigned int netmask)
437 static char buff[128];
439 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
440 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
441 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
442 numeric, netmask) != 0)
448 static int INET_getsock(char *bufp, struct sockaddr *sap)
450 char *sp = bufp, *bp;
453 struct sockaddr_in *sin;
455 sin = (struct sockaddr_in *) sap;
456 sin->sin_family = AF_INET;
461 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
464 if ((*sp >= 'A') && (*sp <= 'F'))
465 bp[i] |= (int) (*sp - 'A') + 10;
466 else if ((*sp >= '0') && (*sp <= '9'))
467 bp[i] |= (int) (*sp - '0');
475 if ((*sp >= 'A') && (*sp <= 'F'))
476 bp[i] |= (int) (*sp - 'A') + 10;
477 else if ((*sp >= '0') && (*sp <= '9'))
478 bp[i] |= (int) (*sp - '0');
484 sin->sin_addr.s_addr = htonl(val);
489 static int INET_input(int type, char *bufp, struct sockaddr *sap)
493 return (INET_getsock(bufp, sap));
495 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
497 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
501 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
503 struct sockaddr_in *mask = (struct sockaddr_in *) m;
507 if ((slash = strchr(adr, '/')) == NULL)
511 prefix = strtoul(slash, &end, 0);
516 sprintf(name, "/%d", prefix);
518 mask->sin_family = AF_INET;
519 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
524 struct aftype inet_aftype =
526 "inet", NULL, /*"DARPA Internet", */ AF_INET, sizeof(unsigned long),
527 INET_print, INET_sprint, INET_input, INET_reserror,
528 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
534 #endif /* HAVE_AFINET */
536 /* Display an UNSPEC address. */
537 static char *UNSPEC_print(unsigned char *ptr)
539 static char buff[64];
544 for (i = 0; i < sizeof(struct sockaddr); i++) {
545 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
547 buff[strlen(buff) - 1] = '\0';
551 /* Display an UNSPEC socket address. */
552 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
556 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
557 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
558 return (UNSPEC_print(sap->sa_data));
561 struct aftype unspec_aftype =
563 "unspec", NULL, /*"UNSPEC", */ AF_UNSPEC, 0,
564 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
568 static short sVafinit = 0;
570 struct aftype *aftypes[] =
611 unspec_aftype.title = _("UNSPEC");
613 unix_aftype.title = _("UNIX Domain");
616 inet_aftype.title = _("DARPA Internet");
619 inet6_aftype.title = _("IPv6");
622 ax25_aftype.title = _("AMPR AX.25");
625 netrom_aftype.title = _("AMPR NET/ROM");
628 ipx_aftype.title = _("Novell IPX");
631 ddp_aftype.title = _("Appletalk DDP");
634 ec_aftype.title = _("Econet");
637 x25_aftype.title = _("CCITT X.25");
640 rose_aftype.title = _("AMPR ROSE");
643 ash_aftype.title = _("Ash");
648 int aftrans_opt(const char *arg)
650 struct aftrans_t *paft;
654 safe_strncpy(buf, arg, sizeof(buf));
660 tmp2 = index(tmp1, ',');
666 for (paft = aftrans; paft->alias; paft++) {
667 if (strcmp(tmp1, paft->alias))
669 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
670 fprintf(stderr, _("Too much address family arguments.\n"));
677 strcat(afname, paft->name);
681 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
690 /* set the default AF list from the program name or a constant value */
691 void aftrans_def(char *tool, char *argv0, char *dflt)
696 strcpy(afname, dflt);
698 if (!(tmp = strrchr(argv0, '/')))
699 tmp = argv0; /* no slash?! */
703 if (!(buf = strdup(tmp)))
706 if (strlen(tool) >= strlen(tmp)) {
710 tmp = buf + (strlen(tmp) - strlen(tool));
712 if (strcmp(tmp, tool) != 0) {
717 if ((tmp = strchr(buf, '_')))
721 if (aftrans_opt(buf))
728 /* Check our protocol family table for this family. */
729 struct aftype *get_aftype(const char *name)
737 while (*afp != NULL) {
738 if (!strcmp((*afp)->name, name))
742 if (index(name, ','))
743 fprintf(stderr, _("Please don't supply more than one address family.\n"));
748 /* Check our protocol family table for this family. */
749 struct aftype *get_afntype(int af)
757 while (*afp != NULL) {
758 if ((*afp)->af == af)
765 /* Check our protocol family table for this family and return its socket */
766 int get_socket_for_af(int af)
774 while (*afp != NULL) {
775 if ((*afp)->af == af)
782 /* type: 0=all, 1=getroute */
783 void print_aflist(int type) {
792 while (*afp != NULL) {
793 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
796 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
797 txt = (*afp)->name; if (!txt) txt = "..";
798 fprintf(stderr,"%s (%s) ",txt,(*afp)->title);
802 fprintf(stderr,"\n");
805 struct user_net_device_stats {
806 unsigned long long rx_packets; /* total packets received */
807 unsigned long long tx_packets; /* total packets transmitted */
808 unsigned long long rx_bytes; /* total bytes received */
809 unsigned long long tx_bytes; /* total bytes transmitted */
810 unsigned long rx_errors; /* bad packets received */
811 unsigned long tx_errors; /* packet transmit problems */
812 unsigned long rx_dropped; /* no space in linux buffers */
813 unsigned long tx_dropped; /* no space available in linux */
814 unsigned long rx_multicast; /* multicast packets received */
815 unsigned long rx_compressed;
816 unsigned long tx_compressed;
817 unsigned long collisions;
819 /* detailed rx_errors: */
820 unsigned long rx_length_errors;
821 unsigned long rx_over_errors; /* receiver ring buff overflow */
822 unsigned long rx_crc_errors; /* recved pkt with crc error */
823 unsigned long rx_frame_errors; /* recv'd frame alignment error */
824 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
825 unsigned long rx_missed_errors; /* receiver missed packet */
826 /* detailed tx_errors */
827 unsigned long tx_aborted_errors;
828 unsigned long tx_carrier_errors;
829 unsigned long tx_fifo_errors;
830 unsigned long tx_heartbeat_errors;
831 unsigned long tx_window_errors;
835 struct interface *next, *prev;
836 char name[IFNAMSIZ]; /* interface name */
837 short type; /* if type */
838 short flags; /* various flags */
839 int metric; /* routing metric */
840 int mtu; /* MTU value */
841 int tx_queue_len; /* transmit queue length */
842 struct ifmap map; /* hardware setup */
843 struct sockaddr addr; /* IP address */
844 struct sockaddr dstaddr; /* P-P IP address */
845 struct sockaddr broadaddr; /* IP broadcast address */
846 struct sockaddr netmask; /* IP network mask */
847 struct sockaddr ipxaddr_bb; /* IPX network address */
848 struct sockaddr ipxaddr_sn; /* IPX network address */
849 struct sockaddr ipxaddr_e3; /* IPX network address */
850 struct sockaddr ipxaddr_e2; /* IPX network address */
851 struct sockaddr ddpaddr; /* Appletalk DDP address */
852 struct sockaddr ecaddr; /* Econet address */
861 char hwaddr[32]; /* HW address */
862 int statistics_valid;
863 struct user_net_device_stats stats; /* statistics */
864 int keepalive; /* keepalive value for SLIP */
865 int outfill; /* outfill value for SLIP */
869 int opt_a = 0; /* show all interfaces */
870 int opt_i = 0; /* show the statistics */
871 int opt_v = 0; /* debugging output flag */
873 int addr_family = 0; /* currently selected AF */
874 static struct interface *int_list, *int_last;
875 int skfd = -1; /* generic raw socket desc. */
878 int sockets_open(int family)
882 static int force = -1;
886 if (get_kernel_revision() < KRELEASE(2, 1, 0))
888 if (access("/proc/net", R_OK))
891 for (aft = aftypes; *aft; aft++) {
892 struct aftype *af = *aft;
893 int type = SOCK_DGRAM;
894 if (af->af == AF_UNSPEC)
896 if (family && family != af->af)
902 /* Check some /proc file first to not stress kmod */
903 if (!family && !force && af->flag_file) {
904 if (access(af->flag_file, R_OK))
908 if (af->af == AF_NETROM)
909 type = SOCK_SEQPACKET;
912 if (af->af == AF_X25)
913 type = SOCK_SEQPACKET;
915 af->fd = socket(af->af, type, 0);
920 fprintf(stderr, _("No usable address families found.\n"));
924 /* like strcmp(), but knows about numbers */
925 int nstrcmp(const char *astr, const char *b)
927 const char *a = astr;
948 return atoi(a) > atoi(b) ? 1 : -1;
953 static struct interface *add_interface(char *name)
955 struct interface *ife, **nextp, *new;
957 for (ife = int_last; ife; ife = ife->prev) {
958 int n = nstrcmp(ife->name, name);
965 safe_strncpy(new->name, name, IFNAMSIZ);
966 nextp = ife ? &ife->next : &int_list;
970 new->next->prev = new;
978 static int if_readconf(void)
986 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
988 skfd = get_socket_for_af(AF_INET);
990 fprintf(stderr, _("warning: no inet socket available: %s\n"),
992 /* Try to soldier on with whatever socket we can get hold of. */
993 skfd = sockets_open(0);
1000 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
1001 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1003 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
1004 perror("SIOCGIFCONF");
1007 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1008 /* assume it overflowed and try again */
1016 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1017 add_interface(ifr->ifr_name);
1027 static char *get_name(char *name, char *p)
1034 if (*p == ':') { /* could be an alias */
1035 char *dot = p, *dotname = name;
1039 if (*p != ':') { /* it wasn't, backup */
1054 static int get_dev_fields(char *bp, struct interface *ife)
1056 switch (procnetdev_vsn) {
1059 "%llu %llu %lu %lu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu %lu",
1060 &ife->stats.rx_bytes,
1061 &ife->stats.rx_packets,
1062 &ife->stats.rx_errors,
1063 &ife->stats.rx_dropped,
1064 &ife->stats.rx_fifo_errors,
1065 &ife->stats.rx_frame_errors,
1066 &ife->stats.rx_compressed,
1067 &ife->stats.rx_multicast,
1069 &ife->stats.tx_bytes,
1070 &ife->stats.tx_packets,
1071 &ife->stats.tx_errors,
1072 &ife->stats.tx_dropped,
1073 &ife->stats.tx_fifo_errors,
1074 &ife->stats.collisions,
1075 &ife->stats.tx_carrier_errors,
1076 &ife->stats.tx_compressed);
1079 sscanf(bp, "%llu %llu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu",
1080 &ife->stats.rx_bytes,
1081 &ife->stats.rx_packets,
1082 &ife->stats.rx_errors,
1083 &ife->stats.rx_dropped,
1084 &ife->stats.rx_fifo_errors,
1085 &ife->stats.rx_frame_errors,
1087 &ife->stats.tx_bytes,
1088 &ife->stats.tx_packets,
1089 &ife->stats.tx_errors,
1090 &ife->stats.tx_dropped,
1091 &ife->stats.tx_fifo_errors,
1092 &ife->stats.collisions,
1093 &ife->stats.tx_carrier_errors);
1094 ife->stats.rx_multicast = 0;
1097 sscanf(bp, "%llu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu",
1098 &ife->stats.rx_packets,
1099 &ife->stats.rx_errors,
1100 &ife->stats.rx_dropped,
1101 &ife->stats.rx_fifo_errors,
1102 &ife->stats.rx_frame_errors,
1104 &ife->stats.tx_packets,
1105 &ife->stats.tx_errors,
1106 &ife->stats.tx_dropped,
1107 &ife->stats.tx_fifo_errors,
1108 &ife->stats.collisions,
1109 &ife->stats.tx_carrier_errors);
1110 ife->stats.rx_bytes = 0;
1111 ife->stats.tx_bytes = 0;
1112 ife->stats.rx_multicast = 0;
1118 static int procnetdev_version(char *buf)
1120 if (strstr(buf, "compressed"))
1122 if (strstr(buf, "bytes"))
1127 static int if_readlist_proc(char *target)
1129 static int proc_read;
1132 struct interface *ife;
1140 fh = fopen(_PATH_PROCNET_DEV, "r");
1142 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1143 _PATH_PROCNET_DEV, strerror(errno));
1144 return if_readconf();
1146 fgets(buf, sizeof buf, fh); /* eat line */
1147 fgets(buf, sizeof buf, fh);
1149 #if 0 /* pretty, but can't cope with missing fields */
1150 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1151 "face", "", /* parsed separately */
1158 "compressed", "%lu",
1167 "compressed", "%lu",
1172 procnetdev_vsn = procnetdev_version(buf);
1176 while (fgets(buf, sizeof buf, fh)) {
1177 char *s, name[IFNAMSIZ];
1178 s = get_name(name, buf);
1179 ife = add_interface(name);
1180 get_dev_fields(s, ife);
1181 ife->statistics_valid = 1;
1182 if (target && !strcmp(target,name))
1186 perror(_PATH_PROCNET_DEV);
1198 int if_readlist(void)
1200 int err = if_readlist_proc(NULL);
1202 err = if_readconf();
1206 int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1208 struct interface *ife;
1210 if (!int_list && (if_readlist() < 0))
1212 for (ife = int_list; ife; ife = ife->next) {
1213 int err = doit(ife, cookie);
1220 /* Support for fetching an IPX address */
1223 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1225 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1226 return ioctl(sock, SIOCGIFADDR, ifr);
1231 /* Fetch the interface configuration from the kernel. */
1232 int if_fetch(struct interface *ife)
1236 char *ifname = ife->name;
1238 strcpy(ifr.ifr_name, ifname);
1239 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1241 ife->flags = ifr.ifr_flags;
1243 strcpy(ifr.ifr_name, ifname);
1244 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1245 memset(ife->hwaddr, 0, 32);
1247 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1249 ife->type = ifr.ifr_hwaddr.sa_family;
1251 strcpy(ifr.ifr_name, ifname);
1252 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1255 ife->metric = ifr.ifr_metric;
1257 strcpy(ifr.ifr_name, ifname);
1258 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1261 ife->mtu = ifr.ifr_mtu;
1264 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1265 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1266 ife->type == ARPHRD_ADAPT) {
1268 strcpy(ifr.ifr_name, ifname);
1269 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1272 ife->outfill = (unsigned int) ifr.ifr_data;
1274 #ifdef SIOCGKEEPALIVE
1275 strcpy(ifr.ifr_name, ifname);
1276 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1279 ife->keepalive = (unsigned int) ifr.ifr_data;
1284 strcpy(ifr.ifr_name, ifname);
1285 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1286 memset(&ife->map, 0, sizeof(struct ifmap));
1288 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1290 strcpy(ifr.ifr_name, ifname);
1291 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1292 memset(&ife->map, 0, sizeof(struct ifmap));
1294 ife->map = ifr.ifr_map;
1296 #ifdef HAVE_TXQUEUELEN
1297 strcpy(ifr.ifr_name, ifname);
1298 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1299 ife->tx_queue_len = -1; /* unknown value */
1301 ife->tx_queue_len = ifr.ifr_qlen;
1303 ife->tx_queue_len = -1; /* unknown value */
1308 fd = get_socket_for_af(AF_INET);
1310 strcpy(ifr.ifr_name, ifname);
1311 ifr.ifr_addr.sa_family = AF_INET;
1312 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1314 ife->addr = ifr.ifr_addr;
1315 strcpy(ifr.ifr_name, ifname);
1316 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1317 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1319 ife->dstaddr = ifr.ifr_dstaddr;
1321 strcpy(ifr.ifr_name, ifname);
1322 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1323 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1325 ife->broadaddr = ifr.ifr_broadaddr;
1327 strcpy(ifr.ifr_name, ifname);
1328 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1329 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1331 ife->netmask = ifr.ifr_netmask;
1333 memset(&ife->addr, 0, sizeof(struct sockaddr));
1338 /* DDP address maybe ? */
1339 fd = get_socket_for_af(AF_APPLETALK);
1341 strcpy(ifr.ifr_name, ifname);
1342 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1343 ife->ddpaddr = ifr.ifr_addr;
1350 /* Look for IPX addresses with all framing types */
1351 fd = get_socket_for_af(AF_IPX);
1353 strcpy(ifr.ifr_name, ifname);
1354 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1355 ife->has_ipx_bb = 1;
1356 ife->ipxaddr_bb = ifr.ifr_addr;
1358 strcpy(ifr.ifr_name, ifname);
1359 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1360 ife->has_ipx_sn = 1;
1361 ife->ipxaddr_sn = ifr.ifr_addr;
1363 strcpy(ifr.ifr_name, ifname);
1364 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1365 ife->has_ipx_e3 = 1;
1366 ife->ipxaddr_e3 = ifr.ifr_addr;
1368 strcpy(ifr.ifr_name, ifname);
1369 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1370 ife->has_ipx_e2 = 1;
1371 ife->ipxaddr_e2 = ifr.ifr_addr;
1377 /* Econet address maybe? */
1378 fd = get_socket_for_af(AF_ECONET);
1380 strcpy(ifr.ifr_name, ifname);
1381 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1382 ife->ecaddr = ifr.ifr_addr;
1383 ife->has_econet = 1;
1392 int do_if_fetch(struct interface *ife)
1394 if (if_fetch(ife) < 0) {
1396 if (errno == ENODEV) {
1397 /* Give better error message for this case. */
1398 errmsg = _("Device not found");
1400 errmsg = strerror(errno);
1402 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1409 /* This structure defines hardware protocols and their handlers. */
1415 char *(*print) (unsigned char *);
1416 int (*input) (char *, struct sockaddr *);
1417 int (*activate) (int fd);
1418 int suppress_null_addr;
1421 /* Display an UNSPEC address. */
1422 static char *pr_unspec(unsigned char *ptr)
1424 static char buff[64];
1429 for (i = 0; i < sizeof(struct sockaddr); i++) {
1430 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
1432 buff[strlen(buff) - 1] = '\0';
1436 struct hwtype unspec_hwtype =
1438 "unspec", NULL, /*"UNSPEC", */ -1, 0,
1439 pr_unspec, NULL, NULL
1442 struct hwtype loop_hwtype =
1444 "loop", NULL, /*"Local Loopback", */ ARPHRD_LOOPBACK, 0,
1449 #include <net/if_arp.h>
1450 #include <linux/if_ether.h>
1452 extern struct hwtype ether_hwtype;
1454 /* Display an Ethernet address in readable format. */
1455 static char *pr_ether(unsigned char *ptr)
1457 static char buff[64];
1459 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1460 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1461 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1467 /* Input an Ethernet address and convert to binary. */
1468 static int in_ether(char *bufp, struct sockaddr *sap)
1475 sap->sa_family = ether_hwtype.type;
1480 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1485 else if (c >= 'a' && c <= 'f')
1487 else if (c >= 'A' && c <= 'F')
1491 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1500 else if (c >= 'a' && c <= 'f')
1501 val |= c - 'a' + 10;
1502 else if (c >= 'A' && c <= 'F')
1503 val |= c - 'A' + 10;
1504 else if (c == ':' || c == 0)
1508 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1515 *ptr++ = (unsigned char) (val & 0377);
1518 /* We might get a semicolon here - not required. */
1520 if (i == ETH_ALEN) {
1522 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1531 /* That's it. Any trailing junk? */
1532 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1534 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1540 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1547 struct hwtype ether_hwtype =
1549 "ether", NULL, /*"10Mbps Ethernet", */ ARPHRD_ETHER, ETH_ALEN,
1550 pr_ether, in_ether, NULL
1554 #endif /* HAVE_HWETHER */
1559 #include <net/if_arp.h>
1561 /* Start the PPP encapsulation on the file descriptor. */
1562 static int do_ppp(int fd)
1564 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1569 struct hwtype ppp_hwtype =
1571 "ppp", NULL, /*"Point-Point Protocol", */ ARPHRD_PPP, 0,
1572 NULL, NULL, do_ppp, 0
1576 #endif /* HAVE_PPP */
1578 static struct hwtype *hwtypes[] =
1601 #ifdef ARPHRD_IEEE802_TR
1653 static short sVhwinit = 0;
1657 loop_hwtype.title = _("Local Loopback");
1658 unspec_hwtype.title = _("UNSPEC");
1660 slip_hwtype.title = _("Serial Line IP");
1661 cslip_hwtype.title = _("VJ Serial Line IP");
1662 slip6_hwtype.title = _("6-bit Serial Line IP");
1663 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1664 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1667 ether_hwtype.title = _("Ethernet");
1670 ash_hwtype.title = _("Ash");
1673 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1676 hippi_hwtype.title = _("HIPPI");
1679 ax25_hwtype.title = _("AMPR AX.25");
1682 rose_hwtype.title = _("AMPR ROSE");
1685 netrom_hwtype.title = _("AMPR NET/ROM");
1688 x25_hwtype.title = _("generic X.25");
1691 tunnel_hwtype.title = _("IPIP Tunnel");
1694 ppp_hwtype.title = _("Point-to-Point Protocol");
1697 hdlc_hwtype.title = _("(Cisco)-HDLC");
1698 lapb_hwtype.title = _("LAPB");
1701 arcnet_hwtype.title = _("ARCnet");
1704 dlci_hwtype.title = _("Frame Relay DLCI");
1705 frad_hwtype.title = _("Frame Relay Access Device");
1708 sit_hwtype.title = _("IPv6-in-IPv4");
1711 irda_hwtype.title = _("IrLAP");
1714 tr_hwtype.title = _("16/4 Mbps Token Ring");
1715 #ifdef ARPHRD_IEEE802_TR
1716 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1720 ec_hwtype.title = _("Econet");
1726 const char *if_port_text[][4] =
1728 /* Keep in step with <linux/netdevice.h> */
1729 {"unknown", NULL, NULL, NULL},
1730 {"10base2", "bnc", "coax", NULL},
1731 {"10baseT", "utp", "tpe", NULL},
1732 {"AUI", "thick", "db15", NULL},
1733 {"100baseT", NULL, NULL, NULL},
1734 {"100baseTX", NULL, NULL, NULL},
1735 {"100baseFX", NULL, NULL, NULL},
1736 {NULL, NULL, NULL, NULL},
1740 /* Check our hardware type table for this type. */
1741 struct hwtype *get_hwntype(int type)
1743 struct hwtype **hwp;
1749 while (*hwp != NULL) {
1750 if ((*hwp)->type == type)
1757 /* return 1 if address is all zeros */
1758 int hw_null_address(struct hwtype *hw, void *ap)
1761 unsigned char *address = (unsigned char *)ap;
1762 for (i = 0; i < hw->alen; i++)
1768 void ife_print(struct interface *ptr)
1773 int can_compress = 0;
1774 unsigned long long rx, tx, short_rx, short_tx;
1779 static struct aftype *ipxtype = NULL;
1782 static struct aftype *ectype = NULL;
1785 static struct aftype *ddptype = NULL;
1789 char addr6[40], devname[20];
1790 struct sockaddr_in6 sap;
1791 int plen, scope, dad_status, if_idx;
1792 extern struct aftype inet6_aftype;
1796 ap = get_afntype(ptr->addr.sa_family);
1798 ap = get_afntype(0);
1802 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1805 hw = get_hwntype(hf);
1807 hw = get_hwntype(-1);
1809 printf(_("%-9.9s Link encap:%s "), ptr->name, hw->title);
1810 /* For some hardware types (eg Ash, ATM) we don't print the
1811 hardware address if it's null. */
1812 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1813 hw->suppress_null_addr)))
1814 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1816 if (ptr->flags & IFF_PORTSEL) {
1817 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1818 if (ptr->flags & IFF_AUTOMEDIA)
1819 printf(_("(auto)"));
1826 printf(_(" %s addr:%s "), ap->name,
1827 ap->sprint(&ptr->addr, 1));
1828 if (ptr->flags & IFF_POINTOPOINT) {
1829 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1831 if (ptr->flags & IFF_BROADCAST) {
1832 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1834 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1839 /* FIXME: should be integrated into interface.c. */
1841 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1842 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1843 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1844 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1845 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1846 if (!strcmp(devname, ptr->name)) {
1847 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1848 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1849 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1850 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1851 printf(_(" inet6 addr: %s/%d"),
1852 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1853 printf(_(" Scope:"));
1856 printf(_("Global"));
1858 case IPV6_ADDR_LINKLOCAL:
1861 case IPV6_ADDR_SITELOCAL:
1864 case IPV6_ADDR_COMPATv4:
1865 printf(_("Compat"));
1867 case IPV6_ADDR_LOOPBACK:
1871 printf(_("Unknown"));
1881 if (ipxtype == NULL)
1882 ipxtype = get_afntype(AF_IPX);
1884 if (ipxtype != NULL) {
1885 if (ptr->has_ipx_bb)
1886 printf(_(" IPX/Ethernet II addr:%s\n"),
1887 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1888 if (ptr->has_ipx_sn)
1889 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1890 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1891 if (ptr->has_ipx_e2)
1892 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1893 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1894 if (ptr->has_ipx_e3)
1895 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1896 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1901 if (ddptype == NULL)
1902 ddptype = get_afntype(AF_APPLETALK);
1903 if (ddptype != NULL) {
1905 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
1911 ectype = get_afntype(AF_ECONET);
1912 if (ectype != NULL) {
1913 if (ptr->has_econet)
1914 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
1919 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1920 if (ptr->flags == 0)
1921 printf(_("[NO FLAGS] "));
1922 if (ptr->flags & IFF_UP)
1924 if (ptr->flags & IFF_BROADCAST)
1925 printf(_("BROADCAST "));
1926 if (ptr->flags & IFF_DEBUG)
1927 printf(_("DEBUG "));
1928 if (ptr->flags & IFF_LOOPBACK)
1929 printf(_("LOOPBACK "));
1930 if (ptr->flags & IFF_POINTOPOINT)
1931 printf(_("POINTOPOINT "));
1932 if (ptr->flags & IFF_NOTRAILERS)
1933 printf(_("NOTRAILERS "));
1934 if (ptr->flags & IFF_RUNNING)
1935 printf(_("RUNNING "));
1936 if (ptr->flags & IFF_NOARP)
1937 printf(_("NOARP "));
1938 if (ptr->flags & IFF_PROMISC)
1939 printf(_("PROMISC "));
1940 if (ptr->flags & IFF_ALLMULTI)
1941 printf(_("ALLMULTI "));
1942 if (ptr->flags & IFF_SLAVE)
1943 printf(_("SLAVE "));
1944 if (ptr->flags & IFF_MASTER)
1945 printf(_("MASTER "));
1946 if (ptr->flags & IFF_MULTICAST)
1947 printf(_("MULTICAST "));
1949 if (ptr->flags & IFF_DYNAMIC)
1950 printf(_("DYNAMIC "));
1952 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1953 printf(_(" MTU:%d Metric:%d"),
1954 ptr->mtu, ptr->metric ? ptr->metric : 1);
1955 #ifdef SIOCSKEEPALIVE
1956 if (ptr->outfill || ptr->keepalive)
1957 printf(_(" Outfill:%d Keepalive:%d"),
1958 ptr->outfill, ptr->keepalive);
1962 /* If needed, display the interface statistics. */
1964 if (ptr->statistics_valid) {
1965 /* XXX: statistics are currently only printed for the primary address,
1966 * not for the aliases, although strictly speaking they're shared
1971 printf(_("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1972 ptr->stats.rx_packets, ptr->stats.rx_errors,
1973 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1974 ptr->stats.rx_frame_errors);
1976 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
1978 rx = ptr->stats.rx_bytes;
1979 tx = ptr->stats.tx_bytes;
1982 if (rx > 1048576) { short_rx /= 1048576; strcpy(Rext, "Mb"); }
1983 else if (rx > 1024) { short_rx /= 1024; strcpy(Rext, "Kb"); }
1984 if (tx > 1048576) { short_tx /= 1048576; strcpy(Text, "Mb"); }
1985 else if (tx > 1024) { short_tx /= 1024; strcpy(Text, "Kb"); }
1988 printf(_("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1989 ptr->stats.tx_packets, ptr->stats.tx_errors,
1990 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1991 ptr->stats.tx_carrier_errors);
1992 printf(_(" collisions:%lu "), ptr->stats.collisions);
1994 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1995 if (ptr->tx_queue_len != -1)
1996 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1998 printf(_("RX bytes:%llu (%lu.%lu %s) TX bytes:%llu (%lu.%lu %s)\n"),
1999 rx, (unsigned long)(short_rx / 10),
2000 (unsigned long)(short_rx % 10), Rext,
2001 tx, (unsigned long)(short_tx / 10),
2002 (unsigned long)(short_tx % 10), Text);
2005 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2006 ptr->map.base_addr)) {
2009 printf(_("Interrupt:%d "), ptr->map.irq);
2010 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2012 printf(_("Base address:0x%x "), ptr->map.base_addr);
2013 if (ptr->map.mem_start) {
2014 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2017 printf(_("DMA chan:%x "), ptr->map.dma);
2024 int do_if_print(struct interface *ife, void *cookie)
2026 int *opt_a = (int *) cookie;
2029 res = do_if_fetch(ife);
2031 if ((ife->flags & IFF_UP) || *opt_a)
2037 struct interface *lookup_interface(char *name)
2039 struct interface *ife = NULL;
2041 if (if_readlist_proc(name) < 0)
2043 ife = add_interface(name);
2047 /* for ipv4 add/del modes */
2048 static int if_print(char *ifname)
2053 res = for_all_interfaces(do_if_print, &opt_a);
2055 struct interface *ife;
2057 ife = lookup_interface(ifname);
2058 res = do_if_fetch(ife);
2065 int display_interfaces(void)
2069 /* Create a channel to the NET kernel. */
2070 if ((skfd = sockets_open(0)) < 0) {
2075 /* Do we have to show the current setup? */
2076 status = if_print((char *) NULL);