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.2 2001/03/06 20:54:43 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 KRELEASE(maj,min,patch) ((maj) * 10000 + (min)*1000 + (patch))
78 int procnetdev_vsn = 1;
81 /* Ugh. But libc5 doesn't provide POSIX types. */
82 #include <asm/types.h>
86 #include <linux/if_slip.h>
93 * This is in linux/include/net/ipv6.h.
97 struct in6_addr ifr6_addr;
99 unsigned int ifr6_ifindex;
104 #endif /* HAVE_AFINET6 */
107 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
108 #include <netipx/ipx.h>
114 #include "net-support.h"
115 #include "pathnames.h"
118 #include "interface.h"
123 /* This structure defines protocol families and their handlers. */
129 char *(*print) (unsigned char *);
130 char *(*sprint) (struct sockaddr *, int numeric);
131 int (*input) (int type, char *bufp, struct sockaddr *);
132 void (*herror) (char *text);
133 int (*rprint) (int options);
134 int (*rinput) (int typ, int ext, char **argv);
137 int (*getmask) (char *src, struct sockaddr * mask, char *name);
143 extern struct aftype *aftypes[];
163 "ax25", "ax25", &flag_ax25
166 "ip", "inet", &flag_inet
169 "ip6", "inet6", &flag_inet6
172 "ipx", "ipx", &flag_ipx
175 "appletalk", "ddp", &flag_ddp
178 "netrom", "netrom", &flag_netrom
181 "inet", "inet", &flag_inet
184 "inet6", "inet6", &flag_inet6
187 "ddp", "ddp", &flag_ddp
190 "unix", "unix", &flag_unx
193 "tcpip", "inet", &flag_inet
196 "econet", "ec", &flag_econet
199 "x25", "x25", &flag_x25
202 "ash", "ash", &flag_ash
209 char afname[256] = "";
213 /* Display a UNIX domain address. */
214 static char *UNIX_print(unsigned char *ptr)
220 /* Display a UNIX domain address. */
221 static char *UNIX_sprint(struct sockaddr *sap, int numeric)
225 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
226 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
227 return (UNIX_print(sap->sa_data));
231 struct aftype unix_aftype =
233 "unix", NULL, /*"UNIX Domain", */ AF_UNIX, 0,
234 UNIX_print, UNIX_sprint, NULL, NULL,
239 #endif /* HAVE_AFUNIX */
243 extern int h_errno; /* some netdb.h versions don't export this */
247 struct sockaddr_in addr;
253 static struct addr *INET_nn = NULL; /* addr-to-name cache */
255 static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
261 sin->sin_family = AF_INET;
264 /* Default is special, meaning 0.0.0.0. */
265 if (!strcmp(name, "default")) {
266 sin->sin_addr.s_addr = INADDR_ANY;
269 /* Look to see if it's a dotted quad. */
270 if (inet_aton(name, &sin->sin_addr)) {
273 /* If we expect this to be a hostname, try hostname database first */
275 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
278 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
279 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
280 sizeof(struct in_addr));
283 /* Try the NETWORKS database to see if this is a known network. */
285 fprintf (stderr, "getnetbyname (%s)\n", name);
287 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
288 sin->sin_addr.s_addr = htonl(np->n_net);
292 /* Don't try again */
298 _res.options |= RES_DEBUG;
302 fprintf (stderr, "gethostbyname (%s)\n", name);
304 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
308 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
309 sizeof(struct in_addr));
315 /* numeric: & 0x8000: default instead of *,
316 * & 0x4000: host instead of net,
317 * & 0x0fff: don't resolve
319 static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
320 int numeric, unsigned int netmask)
325 unsigned long ad, host_ad;
329 if (sin->sin_family != AF_INET) {
331 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), sin->sin_family);
333 errno = EAFNOSUPPORT;
336 ad = (unsigned long) sin->sin_addr.s_addr;
338 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
340 if (ad == INADDR_ANY) {
341 if ((numeric & 0x0FFF) == 0) {
342 if (numeric & 0x8000)
343 safe_strncpy(name, "default", len);
345 safe_strncpy(name, "*", len);
349 if (numeric & 0x0FFF) {
350 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
354 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
361 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
362 safe_strncpy(name, pn->name, len);
364 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
376 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
378 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
380 safe_strncpy(name, ent->h_name, len);
383 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
385 np = getnetbyaddr(host_ad, AF_INET);
387 safe_strncpy(name, np->n_name, len);
389 if ((ent == NULL) && (np == NULL))
390 safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
391 pn = (struct addr *) xmalloc(sizeof(struct addr));
395 pn->name = (char *) xmalloc(strlen(name) + 1);
396 strcpy(pn->name, name);
403 static void INET_reserror(char *text)
409 /* Display an Internet socket address. */
410 static char *INET_print(unsigned char *ptr)
412 return (inet_ntoa((*(struct in_addr *) ptr)));
416 /* Display an Internet socket address. */
417 static char *INET_sprint(struct sockaddr *sap, int numeric)
419 static char buff[128];
421 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
422 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
424 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
425 numeric, 0xffffff00) != 0)
431 char *INET_sprintmask(struct sockaddr *sap, int numeric,
432 unsigned int netmask)
434 static char buff[128];
436 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
437 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
438 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
439 numeric, netmask) != 0)
445 static int INET_getsock(char *bufp, struct sockaddr *sap)
447 char *sp = bufp, *bp;
450 struct sockaddr_in *sin;
452 sin = (struct sockaddr_in *) sap;
453 sin->sin_family = AF_INET;
458 for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
461 if ((*sp >= 'A') && (*sp <= 'F'))
462 bp[i] |= (int) (*sp - 'A') + 10;
463 else if ((*sp >= '0') && (*sp <= '9'))
464 bp[i] |= (int) (*sp - '0');
472 if ((*sp >= 'A') && (*sp <= 'F'))
473 bp[i] |= (int) (*sp - 'A') + 10;
474 else if ((*sp >= '0') && (*sp <= '9'))
475 bp[i] |= (int) (*sp - '0');
481 sin->sin_addr.s_addr = htonl(val);
486 static int INET_input(int type, char *bufp, struct sockaddr *sap)
490 return (INET_getsock(bufp, sap));
492 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
494 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
498 static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
500 struct sockaddr_in *mask = (struct sockaddr_in *) m;
504 if ((slash = strchr(adr, '/')) == NULL)
508 prefix = strtoul(slash, &end, 0);
513 sprintf(name, "/%d", prefix);
515 mask->sin_family = AF_INET;
516 mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
521 struct aftype inet_aftype =
523 "inet", NULL, /*"DARPA Internet", */ AF_INET, sizeof(unsigned long),
524 INET_print, INET_sprint, INET_input, INET_reserror,
525 NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
531 #endif /* HAVE_AFINET */
533 /* Display an UNSPEC address. */
534 static char *UNSPEC_print(unsigned char *ptr)
536 static char buff[64];
541 for (i = 0; i < sizeof(struct sockaddr); i++) {
542 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
544 buff[strlen(buff) - 1] = '\0';
548 /* Display an UNSPEC socket address. */
549 static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
553 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
554 return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
555 return (UNSPEC_print(sap->sa_data));
558 struct aftype unspec_aftype =
560 "unspec", NULL, /*"UNSPEC", */ AF_UNSPEC, 0,
561 UNSPEC_print, UNSPEC_sprint, NULL, NULL,
565 static short sVafinit = 0;
567 struct aftype *aftypes[] =
608 unspec_aftype.title = _("UNSPEC");
610 unix_aftype.title = _("UNIX Domain");
613 inet_aftype.title = _("DARPA Internet");
616 inet6_aftype.title = _("IPv6");
619 ax25_aftype.title = _("AMPR AX.25");
622 netrom_aftype.title = _("AMPR NET/ROM");
625 ipx_aftype.title = _("Novell IPX");
628 ddp_aftype.title = _("Appletalk DDP");
631 ec_aftype.title = _("Econet");
634 x25_aftype.title = _("CCITT X.25");
637 rose_aftype.title = _("AMPR ROSE");
640 ash_aftype.title = _("Ash");
645 int aftrans_opt(const char *arg)
647 struct aftrans_t *paft;
651 safe_strncpy(buf, arg, sizeof(buf));
657 tmp2 = index(tmp1, ',');
663 for (paft = aftrans; paft->alias; paft++) {
664 if (strcmp(tmp1, paft->alias))
666 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
667 fprintf(stderr, _("Too much address family arguments.\n"));
674 strcat(afname, paft->name);
678 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
687 /* set the default AF list from the program name or a constant value */
688 void aftrans_def(char *tool, char *argv0, char *dflt)
693 strcpy(afname, dflt);
695 if (!(tmp = strrchr(argv0, '/')))
696 tmp = argv0; /* no slash?! */
700 if (!(buf = strdup(tmp)))
703 if (strlen(tool) >= strlen(tmp)) {
707 tmp = buf + (strlen(tmp) - strlen(tool));
709 if (strcmp(tmp, tool) != 0) {
714 if ((tmp = strchr(buf, '_')))
718 if (aftrans_opt(buf))
725 /* Check our protocol family table for this family. */
726 struct aftype *get_aftype(const char *name)
734 while (*afp != NULL) {
735 if (!strcmp((*afp)->name, name))
739 if (index(name, ','))
740 fprintf(stderr, _("Please don't supply more than one address family.\n"));
745 /* Check our protocol family table for this family. */
746 struct aftype *get_afntype(int af)
754 while (*afp != NULL) {
755 if ((*afp)->af == af)
762 /* Check our protocol family table for this family and return its socket */
763 int get_socket_for_af(int af)
771 while (*afp != NULL) {
772 if ((*afp)->af == af)
779 /* type: 0=all, 1=getroute */
780 void print_aflist(int type) {
789 while (*afp != NULL) {
790 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
793 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
794 txt = (*afp)->name; if (!txt) txt = "..";
795 fprintf(stderr,"%s (%s) ",txt,(*afp)->title);
799 fprintf(stderr,"\n");
802 struct user_net_device_stats {
803 unsigned long long rx_packets; /* total packets received */
804 unsigned long long tx_packets; /* total packets transmitted */
805 unsigned long long rx_bytes; /* total bytes received */
806 unsigned long long tx_bytes; /* total bytes transmitted */
807 unsigned long rx_errors; /* bad packets received */
808 unsigned long tx_errors; /* packet transmit problems */
809 unsigned long rx_dropped; /* no space in linux buffers */
810 unsigned long tx_dropped; /* no space available in linux */
811 unsigned long rx_multicast; /* multicast packets received */
812 unsigned long rx_compressed;
813 unsigned long tx_compressed;
814 unsigned long collisions;
816 /* detailed rx_errors: */
817 unsigned long rx_length_errors;
818 unsigned long rx_over_errors; /* receiver ring buff overflow */
819 unsigned long rx_crc_errors; /* recved pkt with crc error */
820 unsigned long rx_frame_errors; /* recv'd frame alignment error */
821 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
822 unsigned long rx_missed_errors; /* receiver missed packet */
823 /* detailed tx_errors */
824 unsigned long tx_aborted_errors;
825 unsigned long tx_carrier_errors;
826 unsigned long tx_fifo_errors;
827 unsigned long tx_heartbeat_errors;
828 unsigned long tx_window_errors;
832 struct interface *next, *prev;
833 char name[IFNAMSIZ]; /* interface name */
834 short type; /* if type */
835 short flags; /* various flags */
836 int metric; /* routing metric */
837 int mtu; /* MTU value */
838 int tx_queue_len; /* transmit queue length */
839 struct ifmap map; /* hardware setup */
840 struct sockaddr addr; /* IP address */
841 struct sockaddr dstaddr; /* P-P IP address */
842 struct sockaddr broadaddr; /* IP broadcast address */
843 struct sockaddr netmask; /* IP network mask */
844 struct sockaddr ipxaddr_bb; /* IPX network address */
845 struct sockaddr ipxaddr_sn; /* IPX network address */
846 struct sockaddr ipxaddr_e3; /* IPX network address */
847 struct sockaddr ipxaddr_e2; /* IPX network address */
848 struct sockaddr ddpaddr; /* Appletalk DDP address */
849 struct sockaddr ecaddr; /* Econet address */
858 char hwaddr[32]; /* HW address */
859 int statistics_valid;
860 struct user_net_device_stats stats; /* statistics */
861 int keepalive; /* keepalive value for SLIP */
862 int outfill; /* outfill value for SLIP */
866 int opt_a = 0; /* show all interfaces */
867 int opt_i = 0; /* show the statistics */
868 int opt_v = 0; /* debugging output flag */
870 int addr_family = 0; /* currently selected AF */
871 static struct interface *int_list, *int_last;
872 int skfd = -1; /* generic raw socket desc. */
875 int sockets_open(int family)
879 static int force = -1;
883 if (get_kernel_revision() < KRELEASE(2, 1, 0))
885 if (access("/proc/net", R_OK))
888 for (aft = aftypes; *aft; aft++) {
889 struct aftype *af = *aft;
890 int type = SOCK_DGRAM;
891 if (af->af == AF_UNSPEC)
893 if (family && family != af->af)
899 /* Check some /proc file first to not stress kmod */
900 if (!family && !force && af->flag_file) {
901 if (access(af->flag_file, R_OK))
905 if (af->af == AF_NETROM)
906 type = SOCK_SEQPACKET;
909 if (af->af == AF_X25)
910 type = SOCK_SEQPACKET;
912 af->fd = socket(af->af, type, 0);
917 fprintf(stderr, _("No usable address families found.\n"));
921 /* like strcmp(), but knows about numbers */
922 int nstrcmp(const char *astr, const char *b)
924 const char *a = astr;
945 return atoi(a) > atoi(b) ? 1 : -1;
950 static struct interface *add_interface(char *name)
952 struct interface *ife, **nextp, *new;
954 for (ife = int_last; ife; ife = ife->prev) {
955 int n = nstrcmp(ife->name, name);
962 safe_strncpy(new->name, name, IFNAMSIZ);
963 nextp = ife ? &ife->next : &int_list;
967 new->next->prev = new;
975 static int if_readconf(void)
983 /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
985 skfd = get_socket_for_af(AF_INET);
987 fprintf(stderr, _("warning: no inet socket available: %s\n"),
989 /* Try to soldier on with whatever socket we can get hold of. */
990 skfd = sockets_open(0);
997 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
998 ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
1000 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
1001 perror("SIOCGIFCONF");
1004 if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
1005 /* assume it overflowed and try again */
1013 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
1014 add_interface(ifr->ifr_name);
1024 static char *get_name(char *name, char *p)
1031 if (*p == ':') { /* could be an alias */
1032 char *dot = p, *dotname = name;
1036 if (*p != ':') { /* it wasn't, backup */
1051 static int get_dev_fields(char *bp, struct interface *ife)
1053 switch (procnetdev_vsn) {
1056 "%llu %llu %lu %lu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu %lu",
1057 &ife->stats.rx_bytes,
1058 &ife->stats.rx_packets,
1059 &ife->stats.rx_errors,
1060 &ife->stats.rx_dropped,
1061 &ife->stats.rx_fifo_errors,
1062 &ife->stats.rx_frame_errors,
1063 &ife->stats.rx_compressed,
1064 &ife->stats.rx_multicast,
1066 &ife->stats.tx_bytes,
1067 &ife->stats.tx_packets,
1068 &ife->stats.tx_errors,
1069 &ife->stats.tx_dropped,
1070 &ife->stats.tx_fifo_errors,
1071 &ife->stats.collisions,
1072 &ife->stats.tx_carrier_errors,
1073 &ife->stats.tx_compressed);
1076 sscanf(bp, "%llu %llu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu",
1077 &ife->stats.rx_bytes,
1078 &ife->stats.rx_packets,
1079 &ife->stats.rx_errors,
1080 &ife->stats.rx_dropped,
1081 &ife->stats.rx_fifo_errors,
1082 &ife->stats.rx_frame_errors,
1084 &ife->stats.tx_bytes,
1085 &ife->stats.tx_packets,
1086 &ife->stats.tx_errors,
1087 &ife->stats.tx_dropped,
1088 &ife->stats.tx_fifo_errors,
1089 &ife->stats.collisions,
1090 &ife->stats.tx_carrier_errors);
1091 ife->stats.rx_multicast = 0;
1094 sscanf(bp, "%llu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu",
1095 &ife->stats.rx_packets,
1096 &ife->stats.rx_errors,
1097 &ife->stats.rx_dropped,
1098 &ife->stats.rx_fifo_errors,
1099 &ife->stats.rx_frame_errors,
1101 &ife->stats.tx_packets,
1102 &ife->stats.tx_errors,
1103 &ife->stats.tx_dropped,
1104 &ife->stats.tx_fifo_errors,
1105 &ife->stats.collisions,
1106 &ife->stats.tx_carrier_errors);
1107 ife->stats.rx_bytes = 0;
1108 ife->stats.tx_bytes = 0;
1109 ife->stats.rx_multicast = 0;
1115 static int procnetdev_version(char *buf)
1117 if (strstr(buf, "compressed"))
1119 if (strstr(buf, "bytes"))
1124 static int if_readlist_proc(char *target)
1126 static int proc_read;
1129 struct interface *ife;
1137 fh = fopen(_PATH_PROCNET_DEV, "r");
1139 fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"),
1140 _PATH_PROCNET_DEV, strerror(errno));
1141 return if_readconf();
1143 fgets(buf, sizeof buf, fh); /* eat line */
1144 fgets(buf, sizeof buf, fh);
1146 #if 0 /* pretty, but can't cope with missing fields */
1147 fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh,
1148 "face", "", /* parsed separately */
1155 "compressed", "%lu",
1164 "compressed", "%lu",
1169 procnetdev_vsn = procnetdev_version(buf);
1173 while (fgets(buf, sizeof buf, fh)) {
1174 char *s, name[IFNAMSIZ];
1175 s = get_name(name, buf);
1176 ife = add_interface(name);
1177 get_dev_fields(s, ife);
1178 ife->statistics_valid = 1;
1179 if (target && !strcmp(target,name))
1183 perror(_PATH_PROCNET_DEV);
1195 int if_readlist(void)
1197 int err = if_readlist_proc(NULL);
1199 err = if_readconf();
1203 int for_all_interfaces(int (*doit) (struct interface *, void *), void *cookie)
1205 struct interface *ife;
1207 if (!int_list && (if_readlist() < 0))
1209 for (ife = int_list; ife; ife = ife->next) {
1210 int err = doit(ife, cookie);
1217 /* Support for fetching an IPX address */
1220 static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
1222 ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
1223 return ioctl(sock, SIOCGIFADDR, ifr);
1228 /* Fetch the interface configuration from the kernel. */
1229 int if_fetch(struct interface *ife)
1233 char *ifname = ife->name;
1235 strcpy(ifr.ifr_name, ifname);
1236 if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
1238 ife->flags = ifr.ifr_flags;
1240 strcpy(ifr.ifr_name, ifname);
1241 if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
1242 memset(ife->hwaddr, 0, 32);
1244 memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
1246 ife->type = ifr.ifr_hwaddr.sa_family;
1248 strcpy(ifr.ifr_name, ifname);
1249 if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
1252 ife->metric = ifr.ifr_metric;
1254 strcpy(ifr.ifr_name, ifname);
1255 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
1258 ife->mtu = ifr.ifr_mtu;
1261 if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
1262 ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
1263 ife->type == ARPHRD_ADAPT) {
1265 strcpy(ifr.ifr_name, ifname);
1266 if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
1269 ife->outfill = (unsigned int) ifr.ifr_data;
1271 #ifdef SIOCGKEEPALIVE
1272 strcpy(ifr.ifr_name, ifname);
1273 if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
1276 ife->keepalive = (unsigned int) ifr.ifr_data;
1281 strcpy(ifr.ifr_name, ifname);
1282 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1283 memset(&ife->map, 0, sizeof(struct ifmap));
1285 memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap));
1287 strcpy(ifr.ifr_name, ifname);
1288 if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0)
1289 memset(&ife->map, 0, sizeof(struct ifmap));
1291 ife->map = ifr.ifr_map;
1293 #ifdef HAVE_TXQUEUELEN
1294 strcpy(ifr.ifr_name, ifname);
1295 if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
1296 ife->tx_queue_len = -1; /* unknown value */
1298 ife->tx_queue_len = ifr.ifr_qlen;
1300 ife->tx_queue_len = -1; /* unknown value */
1305 fd = get_socket_for_af(AF_INET);
1307 strcpy(ifr.ifr_name, ifname);
1308 ifr.ifr_addr.sa_family = AF_INET;
1309 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1311 ife->addr = ifr.ifr_addr;
1312 strcpy(ifr.ifr_name, ifname);
1313 if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
1314 memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
1316 ife->dstaddr = ifr.ifr_dstaddr;
1318 strcpy(ifr.ifr_name, ifname);
1319 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
1320 memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
1322 ife->broadaddr = ifr.ifr_broadaddr;
1324 strcpy(ifr.ifr_name, ifname);
1325 if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
1326 memset(&ife->netmask, 0, sizeof(struct sockaddr));
1328 ife->netmask = ifr.ifr_netmask;
1330 memset(&ife->addr, 0, sizeof(struct sockaddr));
1335 /* DDP address maybe ? */
1336 fd = get_socket_for_af(AF_APPLETALK);
1338 strcpy(ifr.ifr_name, ifname);
1339 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1340 ife->ddpaddr = ifr.ifr_addr;
1347 /* Look for IPX addresses with all framing types */
1348 fd = get_socket_for_af(AF_IPX);
1350 strcpy(ifr.ifr_name, ifname);
1351 if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
1352 ife->has_ipx_bb = 1;
1353 ife->ipxaddr_bb = ifr.ifr_addr;
1355 strcpy(ifr.ifr_name, ifname);
1356 if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
1357 ife->has_ipx_sn = 1;
1358 ife->ipxaddr_sn = ifr.ifr_addr;
1360 strcpy(ifr.ifr_name, ifname);
1361 if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
1362 ife->has_ipx_e3 = 1;
1363 ife->ipxaddr_e3 = ifr.ifr_addr;
1365 strcpy(ifr.ifr_name, ifname);
1366 if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
1367 ife->has_ipx_e2 = 1;
1368 ife->ipxaddr_e2 = ifr.ifr_addr;
1374 /* Econet address maybe? */
1375 fd = get_socket_for_af(AF_ECONET);
1377 strcpy(ifr.ifr_name, ifname);
1378 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
1379 ife->ecaddr = ifr.ifr_addr;
1380 ife->has_econet = 1;
1389 int do_if_fetch(struct interface *ife)
1391 if (if_fetch(ife) < 0) {
1393 if (errno == ENODEV) {
1394 /* Give better error message for this case. */
1395 errmsg = _("Device not found");
1397 errmsg = strerror(errno);
1399 fprintf(stderr, _("%s: error fetching interface information: %s\n"),
1406 /* This structure defines hardware protocols and their handlers. */
1412 char *(*print) (unsigned char *);
1413 int (*input) (char *, struct sockaddr *);
1414 int (*activate) (int fd);
1415 int suppress_null_addr;
1418 /* Display an UNSPEC address. */
1419 static char *pr_unspec(unsigned char *ptr)
1421 static char buff[64];
1426 for (i = 0; i < sizeof(struct sockaddr); i++) {
1427 pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
1429 buff[strlen(buff) - 1] = '\0';
1433 struct hwtype unspec_hwtype =
1435 "unspec", NULL, /*"UNSPEC", */ -1, 0,
1436 pr_unspec, NULL, NULL
1439 struct hwtype loop_hwtype =
1441 "loop", NULL, /*"Local Loopback", */ ARPHRD_LOOPBACK, 0,
1446 #include <net/if_arp.h>
1447 #include <linux/if_ether.h>
1449 extern struct hwtype ether_hwtype;
1451 /* Display an Ethernet address in readable format. */
1452 static char *pr_ether(unsigned char *ptr)
1454 static char buff[64];
1456 snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
1457 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
1458 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
1464 /* Input an Ethernet address and convert to binary. */
1465 static int in_ether(char *bufp, struct sockaddr *sap)
1472 sap->sa_family = ether_hwtype.type;
1477 while ((*bufp != '\0') && (i < ETH_ALEN)) {
1482 else if (c >= 'a' && c <= 'f')
1484 else if (c >= 'A' && c <= 'F')
1488 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1497 else if (c >= 'a' && c <= 'f')
1498 val |= c - 'a' + 10;
1499 else if (c >= 'A' && c <= 'F')
1500 val |= c - 'A' + 10;
1501 else if (c == ':' || c == 0)
1505 fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
1512 *ptr++ = (unsigned char) (val & 0377);
1515 /* We might get a semicolon here - not required. */
1517 if (i == ETH_ALEN) {
1519 fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
1528 /* That's it. Any trailing junk? */
1529 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1531 fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
1537 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
1544 struct hwtype ether_hwtype =
1546 "ether", NULL, /*"10Mbps Ethernet", */ ARPHRD_ETHER, ETH_ALEN,
1547 pr_ether, in_ether, NULL
1551 #endif /* HAVE_HWETHER */
1556 #include <net/if_arp.h>
1558 /* Start the PPP encapsulation on the file descriptor. */
1559 static int do_ppp(int fd)
1561 fprintf(stderr, _("You cannot start PPP with this program.\n"));
1566 struct hwtype ppp_hwtype =
1568 "ppp", NULL, /*"Point-Point Protocol", */ ARPHRD_PPP, 0,
1569 NULL, NULL, do_ppp, 0
1573 #endif /* HAVE_PPP */
1575 static struct hwtype *hwtypes[] =
1598 #ifdef ARPHRD_IEEE802_TR
1650 static short sVhwinit = 0;
1654 loop_hwtype.title = _("Local Loopback");
1655 unspec_hwtype.title = _("UNSPEC");
1657 slip_hwtype.title = _("Serial Line IP");
1658 cslip_hwtype.title = _("VJ Serial Line IP");
1659 slip6_hwtype.title = _("6-bit Serial Line IP");
1660 cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
1661 adaptive_hwtype.title = _("Adaptive Serial Line IP");
1664 ether_hwtype.title = _("Ethernet");
1667 ash_hwtype.title = _("Ash");
1670 fddi_hwtype.title = _("Fiber Distributed Data Interface");
1673 hippi_hwtype.title = _("HIPPI");
1676 ax25_hwtype.title = _("AMPR AX.25");
1679 rose_hwtype.title = _("AMPR ROSE");
1682 netrom_hwtype.title = _("AMPR NET/ROM");
1685 x25_hwtype.title = _("generic X.25");
1688 tunnel_hwtype.title = _("IPIP Tunnel");
1691 ppp_hwtype.title = _("Point-to-Point Protocol");
1694 hdlc_hwtype.title = _("(Cisco)-HDLC");
1695 lapb_hwtype.title = _("LAPB");
1698 arcnet_hwtype.title = _("ARCnet");
1701 dlci_hwtype.title = _("Frame Relay DLCI");
1702 frad_hwtype.title = _("Frame Relay Access Device");
1705 sit_hwtype.title = _("IPv6-in-IPv4");
1708 irda_hwtype.title = _("IrLAP");
1711 tr_hwtype.title = _("16/4 Mbps Token Ring");
1712 #ifdef ARPHRD_IEEE802_TR
1713 tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ;
1717 ec_hwtype.title = _("Econet");
1723 const char *if_port_text[][4] =
1725 /* Keep in step with <linux/netdevice.h> */
1726 {"unknown", NULL, NULL, NULL},
1727 {"10base2", "bnc", "coax", NULL},
1728 {"10baseT", "utp", "tpe", NULL},
1729 {"AUI", "thick", "db15", NULL},
1730 {"100baseT", NULL, NULL, NULL},
1731 {"100baseTX", NULL, NULL, NULL},
1732 {"100baseFX", NULL, NULL, NULL},
1733 {NULL, NULL, NULL, NULL},
1737 /* Check our hardware type table for this type. */
1738 struct hwtype *get_hwntype(int type)
1740 struct hwtype **hwp;
1746 while (*hwp != NULL) {
1747 if ((*hwp)->type == type)
1754 /* return 1 if address is all zeros */
1755 int hw_null_address(struct hwtype *hw, void *ap)
1758 unsigned char *address = (unsigned char *)ap;
1759 for (i = 0; i < hw->alen; i++)
1765 void ife_print(struct interface *ptr)
1770 int can_compress = 0;
1771 unsigned long long rx, tx, short_rx, short_tx;
1776 static struct aftype *ipxtype = NULL;
1779 static struct aftype *ectype = NULL;
1782 static struct aftype *ddptype = NULL;
1786 char addr6[40], devname[20];
1787 struct sockaddr_in6 sap;
1788 int plen, scope, dad_status, if_idx;
1789 extern struct aftype inet6_aftype;
1793 ap = get_afntype(ptr->addr.sa_family);
1795 ap = get_afntype(0);
1799 if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
1802 hw = get_hwntype(hf);
1804 hw = get_hwntype(-1);
1806 printf(_("%-9.9s Link encap:%s "), ptr->name, hw->title);
1807 /* For some hardware types (eg Ash, ATM) we don't print the
1808 hardware address if it's null. */
1809 if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&
1810 hw->suppress_null_addr)))
1811 printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
1813 if (ptr->flags & IFF_PORTSEL) {
1814 printf(_("Media:%s"), if_port_text[ptr->map.port][0]);
1815 if (ptr->flags & IFF_AUTOMEDIA)
1816 printf(_("(auto)"));
1823 printf(_(" %s addr:%s "), ap->name,
1824 ap->sprint(&ptr->addr, 1));
1825 if (ptr->flags & IFF_POINTOPOINT) {
1826 printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
1828 if (ptr->flags & IFF_BROADCAST) {
1829 printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
1831 printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
1836 /* FIXME: should be integrated into interface.c. */
1838 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1839 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
1840 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1841 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1842 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1843 if (!strcmp(devname, ptr->name)) {
1844 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1845 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1846 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1847 inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);
1848 printf(_(" inet6 addr: %s/%d"),
1849 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);
1850 printf(_(" Scope:"));
1853 printf(_("Global"));
1855 case IPV6_ADDR_LINKLOCAL:
1858 case IPV6_ADDR_SITELOCAL:
1861 case IPV6_ADDR_COMPATv4:
1862 printf(_("Compat"));
1864 case IPV6_ADDR_LOOPBACK:
1868 printf(_("Unknown"));
1878 if (ipxtype == NULL)
1879 ipxtype = get_afntype(AF_IPX);
1881 if (ipxtype != NULL) {
1882 if (ptr->has_ipx_bb)
1883 printf(_(" IPX/Ethernet II addr:%s\n"),
1884 ipxtype->sprint(&ptr->ipxaddr_bb, 1));
1885 if (ptr->has_ipx_sn)
1886 printf(_(" IPX/Ethernet SNAP addr:%s\n"),
1887 ipxtype->sprint(&ptr->ipxaddr_sn, 1));
1888 if (ptr->has_ipx_e2)
1889 printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
1890 ipxtype->sprint(&ptr->ipxaddr_e2, 1));
1891 if (ptr->has_ipx_e3)
1892 printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
1893 ipxtype->sprint(&ptr->ipxaddr_e3, 1));
1898 if (ddptype == NULL)
1899 ddptype = get_afntype(AF_APPLETALK);
1900 if (ddptype != NULL) {
1902 printf(_(" EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));
1908 ectype = get_afntype(AF_ECONET);
1909 if (ectype != NULL) {
1910 if (ptr->has_econet)
1911 printf(_(" econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));
1916 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
1917 if (ptr->flags == 0)
1918 printf(_("[NO FLAGS] "));
1919 if (ptr->flags & IFF_UP)
1921 if (ptr->flags & IFF_BROADCAST)
1922 printf(_("BROADCAST "));
1923 if (ptr->flags & IFF_DEBUG)
1924 printf(_("DEBUG "));
1925 if (ptr->flags & IFF_LOOPBACK)
1926 printf(_("LOOPBACK "));
1927 if (ptr->flags & IFF_POINTOPOINT)
1928 printf(_("POINTOPOINT "));
1929 if (ptr->flags & IFF_NOTRAILERS)
1930 printf(_("NOTRAILERS "));
1931 if (ptr->flags & IFF_RUNNING)
1932 printf(_("RUNNING "));
1933 if (ptr->flags & IFF_NOARP)
1934 printf(_("NOARP "));
1935 if (ptr->flags & IFF_PROMISC)
1936 printf(_("PROMISC "));
1937 if (ptr->flags & IFF_ALLMULTI)
1938 printf(_("ALLMULTI "));
1939 if (ptr->flags & IFF_SLAVE)
1940 printf(_("SLAVE "));
1941 if (ptr->flags & IFF_MASTER)
1942 printf(_("MASTER "));
1943 if (ptr->flags & IFF_MULTICAST)
1944 printf(_("MULTICAST "));
1946 if (ptr->flags & IFF_DYNAMIC)
1947 printf(_("DYNAMIC "));
1949 /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
1950 printf(_(" MTU:%d Metric:%d"),
1951 ptr->mtu, ptr->metric ? ptr->metric : 1);
1952 #ifdef SIOCSKEEPALIVE
1953 if (ptr->outfill || ptr->keepalive)
1954 printf(_(" Outfill:%d Keepalive:%d"),
1955 ptr->outfill, ptr->keepalive);
1959 /* If needed, display the interface statistics. */
1961 if (ptr->statistics_valid) {
1962 /* XXX: statistics are currently only printed for the primary address,
1963 * not for the aliases, although strictly speaking they're shared
1968 printf(_("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
1969 ptr->stats.rx_packets, ptr->stats.rx_errors,
1970 ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
1971 ptr->stats.rx_frame_errors);
1973 printf(_(" compressed:%lu\n"), ptr->stats.rx_compressed);
1975 rx = ptr->stats.rx_bytes;
1976 tx = ptr->stats.tx_bytes;
1979 if (rx > 1048576) { short_rx /= 1048576; strcpy(Rext, "Mb"); }
1980 else if (rx > 1024) { short_rx /= 1024; strcpy(Rext, "Kb"); }
1981 if (tx > 1048576) { short_tx /= 1048576; strcpy(Text, "Mb"); }
1982 else if (tx > 1024) { short_tx /= 1024; strcpy(Text, "Kb"); }
1985 printf(_("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
1986 ptr->stats.tx_packets, ptr->stats.tx_errors,
1987 ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
1988 ptr->stats.tx_carrier_errors);
1989 printf(_(" collisions:%lu "), ptr->stats.collisions);
1991 printf(_("compressed:%lu "), ptr->stats.tx_compressed);
1992 if (ptr->tx_queue_len != -1)
1993 printf(_("txqueuelen:%d "), ptr->tx_queue_len);
1995 printf(_("RX bytes:%llu (%lu.%lu %s) TX bytes:%llu (%lu.%lu %s)\n"),
1996 rx, (unsigned long)(short_rx / 10),
1997 (unsigned long)(short_rx % 10), Rext,
1998 tx, (unsigned long)(short_tx / 10),
1999 (unsigned long)(short_tx % 10), Text);
2002 if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
2003 ptr->map.base_addr)) {
2006 printf(_("Interrupt:%d "), ptr->map.irq);
2007 if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
2009 printf(_("Base address:0x%x "), ptr->map.base_addr);
2010 if (ptr->map.mem_start) {
2011 printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);
2014 printf(_("DMA chan:%x "), ptr->map.dma);
2021 int do_if_print(struct interface *ife, void *cookie)
2023 int *opt_a = (int *) cookie;
2026 res = do_if_fetch(ife);
2028 if ((ife->flags & IFF_UP) || *opt_a)
2034 struct interface *lookup_interface(char *name)
2036 struct interface *ife = NULL;
2038 if (if_readlist_proc(name) < 0)
2040 ife = add_interface(name);
2044 /* for ipv4 add/del modes */
2045 static int if_print(char *ifname)
2050 res = for_all_interfaces(do_if_print, &opt_a);
2052 struct interface *ife;
2054 ife = lookup_interface(ifname);
2055 res = do_if_fetch(ife);
2062 int display_interfaces(void)
2066 /* Create a channel to the NET kernel. */
2067 if ((skfd = sockets_open(0)) < 0) {
2072 /* Do we have to show the current setup? */
2073 status = if_print((char *) NULL);