2 * Copyright 1996 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/tools/tools/ifinfo/ifinfo.c,v 1.5.2.1 2000/09/13 19:55:30 wollman Exp $
31 #include <sys/types.h>
32 #include <sys/socket.h> /* for PF_LINK */
33 #include <sys/sysctl.h>
44 #include <net/if_types.h>
45 #include <net/if_mib.h>
49 static void printit(const struct ifmibdata *);
50 static const char *iftype(int);
51 static const char *ifphys(int, int);
52 static int isit(int, char **, const char *);
53 static printfcn findlink(int);
56 usage(const char *argv0)
58 fprintf(stderr, "%s: usage:\n\t%s [-l]\n", argv0, argv0);
63 main(int argc, char **argv)
65 int i, maxifno, retval;
66 struct ifmibdata ifmd;
75 while ((c = getopt(argc, argv, "l")) != -1) {
89 name[2] = NETLINK_GENERIC;
90 name[3] = IFMIB_SYSTEM;
91 name[4] = IFMIB_IFCOUNT;
94 if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
95 err(EX_OSERR, "sysctl(net.link.generic.system.ifcount)");
97 for (i = 1; i <= maxifno; i++) {
99 name[3] = IFMIB_IFDATA;
101 name[5] = IFDATA_GENERAL;
102 if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0)
103 err(EX_OSERR, "sysctl(net.link.ifdata.%d.general)",
106 if (!isit(argc - optind, argv + optind, ifmd.ifmd_name))
109 if (dolink && (pf = findlink(ifmd.ifmd_data.ifi_type))) {
110 name[5] = IFDATA_LINKSPECIFIC;
111 if (sysctl(name, 6, 0, &linkmiblen, 0, 0) < 0)
113 "sysctl(net.link.ifdata.%d.linkspec) size",
115 linkmib = malloc(linkmiblen);
117 err(EX_OSERR, "malloc(%lu)",
119 if (sysctl(name, 6, linkmib, &linkmiblen, 0, 0) < 0)
121 "sysctl(net.link.ifdata.%d.linkspec)",
123 pf(linkmib, linkmiblen);
133 printit(const struct ifmibdata *ifmd)
135 printf("Interface %.*s:\n", IFNAMSIZ, ifmd->ifmd_name);
136 printf("\tflags: %x\n", ifmd->ifmd_flags);
137 printf("\tpromiscuous listeners: %d\n", ifmd->ifmd_pcount);
138 printf("\tsend queue length: %d\n", ifmd->ifmd_snd_len);
139 printf("\tsend queue max length: %d\n", ifmd->ifmd_snd_maxlen);
140 printf("\tsend queue drops: %d\n", ifmd->ifmd_snd_drops);
141 printf("\ttype: %s\n", iftype(ifmd->ifmd_data.ifi_type));
142 printf("\tphysical: %s\n", ifphys(ifmd->ifmd_data.ifi_type,
143 ifmd->ifmd_data.ifi_physical));
144 printf("\taddress length: %d\n", ifmd->ifmd_data.ifi_addrlen);
145 printf("\theader length: %d\n", ifmd->ifmd_data.ifi_hdrlen);
146 printf("\treceive quota: %d\n", ifmd->ifmd_data.ifi_recvquota);
147 printf("\ttransmit quota: %d\n", ifmd->ifmd_data.ifi_xmitquota);
148 printf("\tmtu: %lu\n", ifmd->ifmd_data.ifi_mtu);
149 printf("\tmetric: %lu\n", ifmd->ifmd_data.ifi_metric);
150 printf("\tline rate: %lu bit/s\n", ifmd->ifmd_data.ifi_baudrate);
151 printf("\tpackets received: %lu\n", ifmd->ifmd_data.ifi_ipackets);
152 printf("\tinput errors: %lu\n", ifmd->ifmd_data.ifi_ierrors);
153 printf("\tpackets transmitted: %lu\n", ifmd->ifmd_data.ifi_opackets);
154 printf("\toutput errors: %lu\n", ifmd->ifmd_data.ifi_oerrors);
155 printf("\tcollisions: %lu\n", ifmd->ifmd_data.ifi_collisions);
156 printf("\tbytes received: %lu\n", ifmd->ifmd_data.ifi_ibytes);
157 printf("\tbytes transmitted: %lu\n", ifmd->ifmd_data.ifi_obytes);
158 printf("\tmulticasts received: %lu\n", ifmd->ifmd_data.ifi_imcasts);
159 printf("\tmulticasts transmitted: %lu\n", ifmd->ifmd_data.ifi_omcasts);
160 printf("\tinput queue drops: %lu\n", ifmd->ifmd_data.ifi_iqdrops);
161 printf("\tpackets for unknown protocol: %lu\n",
162 ifmd->ifmd_data.ifi_noproto);
164 printf("\treceive timing: %lu usec\n", ifmd->ifmd_data.ifi_recvtiming);
165 printf("\ttransmit timing: %lu usec\n",
166 ifmd->ifmd_data.ifi_xmittiming);
170 static const char *const if_types[] = {
178 "ISO 8802-3 CSMA/CD",
179 "ISO 8802-4 Token Bus",
180 "ISO 8802-5 Token Ring",
181 "ISO 8802-6 DQDB MAN",
197 "Experimental Ethernet",
200 "Ultra Technologies",
205 "Parallel printer port",
223 "SMDS InterCarrier Interface",
224 "Proprietary virtual interface",
225 "Proprietary multiplexing",
226 "Generic tunnel interface",
227 "IPv6-to-IPv4 TCP relay capturing interface",
228 "6to4 tunnel interface"
230 #define NIFTYPES ((sizeof if_types)/(sizeof if_types[0]))
235 static char buf[256];
237 if (type <= 0 || type >= NIFTYPES) {
238 sprintf(buf, "unknown type %d", type);
242 return if_types[type];
246 ifphys(int type, int phys)
248 static char buf[256];
250 sprintf(buf, "unknown physical %d", phys);
255 isit(int argc, char **argv, const char *name)
259 for (argc = 0; argv[argc]; argc++) {
260 if (strncmp(argv[argc], name, IFNAMSIZ) == 0)