Annotation of src/usr.bin/netstat/if.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: if.c,v 1.13 1997/06/29 21:46:00 millert Exp $ */
1.6 deraadt 2: /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
40: #else
1.14 ! millert 41: static char *rcsid = "$OpenBSD: if.c,v 1.13 1997/06/29 21:46:00 millert Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
44:
45: #include <sys/types.h>
46: #include <sys/protosw.h>
47: #include <sys/socket.h>
48:
49: #include <net/if.h>
50: #include <net/if_dl.h>
1.5 deraadt 51: #include <net/if_types.h>
1.1 deraadt 52: #include <netinet/in.h>
53: #include <netinet/in_var.h>
1.11 millert 54: #include <netinet/if_ether.h>
1.1 deraadt 55: #include <netns/ns.h>
56: #include <netns/ns_if.h>
1.8 mickey 57: #include <netipx/ipx.h>
58: #include <netipx/ipx_if.h>
1.1 deraadt 59: #include <netiso/iso.h>
60: #include <netiso/iso_var.h>
61: #include <arpa/inet.h>
62:
1.13 millert 63: #include <limits.h>
1.1 deraadt 64: #include <signal.h>
65: #include <stdio.h>
66: #include <string.h>
67: #include <unistd.h>
68:
69: #include "netstat.h"
70:
71: #define YES 1
72: #define NO 0
73:
74: static void sidewaysintpr __P((u_int, u_long));
75: static void catchalarm __P((int));
76:
77: /*
78: * Print a description of the network interfaces.
1.3 deraadt 79: * NOTE: ifnetaddr is the location of the kernel global "ifnet",
80: * which is a TAILQ_HEAD.
1.1 deraadt 81: */
82: void
83: intpr(interval, ifnetaddr)
84: int interval;
85: u_long ifnetaddr;
86: {
87: struct ifnet ifnet;
88: union {
89: struct ifaddr ifa;
90: struct in_ifaddr in;
91: struct ns_ifaddr ns;
1.8 mickey 92: struct ipx_ifaddr ipx;
1.1 deraadt 93: struct iso_ifaddr iso;
94: } ifaddr;
95: u_long ifaddraddr;
96: struct sockaddr *sa;
1.3 deraadt 97: struct ifnet_head ifhead; /* TAILQ_HEAD */
98: char name[IFNAMSIZ];
1.1 deraadt 99:
100: if (ifnetaddr == 0) {
101: printf("ifnet: symbol not defined\n");
102: return;
103: }
104: if (interval) {
105: sidewaysintpr((unsigned)interval, ifnetaddr);
106: return;
107: }
1.3 deraadt 108:
109: /*
110: * Find the pointer to the first ifnet structure. Replace
111: * the pointer to the TAILQ_HEAD with the actual pointer
112: * to the first list element.
113: */
114: if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 115: return;
1.3 deraadt 116: ifnetaddr = (u_long)ifhead.tqh_first;
117:
1.2 deraadt 118: printf("%-5.5s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s",
1.1 deraadt 119: "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
120: "Opkts", "Oerrs");
121: printf(" %5s", "Coll");
122: if (tflag)
123: printf(" %s", "Time");
124: if (dflag)
125: printf(" %s", "Drop");
126: putchar('\n');
127: ifaddraddr = 0;
128: while (ifnetaddr || ifaddraddr) {
129: struct sockaddr_in *sin;
130: register char *cp;
131: int n, m;
132:
133: if (ifaddraddr == 0) {
1.3 deraadt 134: if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1.1 deraadt 135: return;
1.3 deraadt 136: bcopy(ifnet.if_xname, name, IFNAMSIZ);
137: name[IFNAMSIZ - 1] = '\0'; /* sanity */
1.1 deraadt 138: ifnetaddr = (u_long)ifnet.if_list.tqe_next;
1.3 deraadt 139: if (interface != 0 && strcmp(name, interface) != 0)
1.1 deraadt 140: continue;
1.10 millert 141: cp = strchr(name, '\0');
1.1 deraadt 142: if ((ifnet.if_flags & IFF_UP) == 0)
143: *cp++ = '*';
144: *cp = '\0';
145: ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
146: }
1.11 millert 147: printf("%-5.5s %-5ld ", name, ifnet.if_mtu);
1.1 deraadt 148: if (ifaddraddr == 0) {
149: printf("%-11.11s ", "none");
150: printf("%-15.15s ", "none");
151: } else {
152: if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
153: ifaddraddr = 0;
154: continue;
155: }
156: #define CP(x) ((char *)(x))
157: cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
158: CP(&ifaddr); sa = (struct sockaddr *)cp;
159: switch (sa->sa_family) {
160: case AF_UNSPEC:
161: printf("%-11.11s ", "none");
162: printf("%-17.17s ", "none");
163: break;
164: case AF_INET:
165: sin = (struct sockaddr_in *)sa;
166: #ifdef notdef
167: /* can't use inet_makeaddr because kernel
168: * keeps nets unshifted.
169: */
170: in = inet_makeaddr(ifaddr.in.ia_subnet,
171: INADDR_ANY);
172: printf("%-11.11s ", netname(in.s_addr,
173: ifaddr.in.ia_subnetmask));
174: #else
175: printf("%-11.11s ",
176: netname(ifaddr.in.ia_subnet,
177: ifaddr.in.ia_subnetmask));
178: #endif
179: printf("%-17.17s ",
180: routename(sin->sin_addr.s_addr));
181:
182: if (aflag) {
183: u_long multiaddr;
184: struct in_multi inm;
185:
186: multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
187: while (multiaddr != 0) {
188: kread(multiaddr, (char *)&inm,
189: sizeof inm);
1.14 ! millert 190: printf("\n%23s %-17.17s ", "",
1.1 deraadt 191: routename(inm.inm_addr.s_addr));
192: multiaddr = (u_long)inm.inm_list.le_next;
193: }
1.8 mickey 194: }
195: break;
196: case AF_IPX:
197: {
198: struct sockaddr_ipx *sipx =
199: (struct sockaddr_ipx *)sa;
200: u_long net;
201: char netnum[8];
202:
203: *(union ipx_net *) &net = sipx->sipx_addr.ipx_net;
1.12 millert 204: sprintf(netnum, "%xH", ntohl(net));
1.8 mickey 205: upHex(netnum);
206: printf("ipx:%-8s", netnum);
207: printf("%-17s ",
208: ipx_phost((struct sockaddr *)sipx));
1.1 deraadt 209: }
210: break;
211: case AF_NS:
212: {
213: struct sockaddr_ns *sns =
214: (struct sockaddr_ns *)sa;
215: u_long net;
216: char netnum[8];
217:
218: *(union ns_net *) &net = sns->sns_addr.x_net;
1.12 millert 219: sprintf(netnum, "%xH", ntohl(net));
1.1 deraadt 220: upHex(netnum);
221: printf("ns:%-8s ", netnum);
222: printf("%-17s ",
223: ns_phost((struct sockaddr *)sns));
224: }
225: break;
226: case AF_LINK:
227: {
228: struct sockaddr_dl *sdl =
229: (struct sockaddr_dl *)sa;
1.5 deraadt 230: m = printf("%-11.11s ", "<Link>");
231: if (sdl->sdl_type == IFT_ETHER ||
232: sdl->sdl_type == IFT_FDDI)
233: printf("%-17.17s ",
1.11 millert 234: ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1.5 deraadt 235: else {
236: cp = (char *)LLADDR(sdl);
237: n = sdl->sdl_alen;
238: goto hexprint;
239: }
1.1 deraadt 240: }
1.5 deraadt 241: break;
1.1 deraadt 242: default:
243: m = printf("(%d)", sa->sa_family);
244: for (cp = sa->sa_len + (char *)sa;
245: --cp > sa->sa_data && (*cp == 0);) {}
246: n = cp - sa->sa_data + 1;
247: cp = sa->sa_data;
248: hexprint:
249: while (--n >= 0)
250: m += printf("%x%c", *cp++ & 0xff,
251: n > 0 ? '.' : ' ');
252: m = 30 - m;
253: while (m-- > 0)
254: putchar(' ');
255: break;
256: }
257: ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
258: }
1.11 millert 259: printf("%8ld %5ld %8ld %5ld %5ld",
1.1 deraadt 260: ifnet.if_ipackets, ifnet.if_ierrors,
261: ifnet.if_opackets, ifnet.if_oerrors,
262: ifnet.if_collisions);
263: if (tflag)
264: printf(" %3d", ifnet.if_timer);
265: if (dflag)
266: printf(" %3d", ifnet.if_snd.ifq_drops);
267: putchar('\n');
268: }
269: }
270:
1.4 deraadt 271: #define MAXIF 100
1.1 deraadt 272: struct iftot {
1.3 deraadt 273: char ift_name[IFNAMSIZ]; /* interface name */
1.1 deraadt 274: int ift_ip; /* input packets */
275: int ift_ie; /* input errors */
276: int ift_op; /* output packets */
277: int ift_oe; /* output errors */
278: int ift_co; /* collisions */
279: int ift_dr; /* drops */
280: } iftot[MAXIF];
281:
282: u_char signalled; /* set if alarm goes off "early" */
283:
284: /*
285: * Print a running summary of interface statistics.
286: * Repeat display every interval seconds, showing statistics
287: * collected over that interval. Assumes that interval is non-zero.
288: * First line printed at top of screen is always cumulative.
289: */
290: static void
291: sidewaysintpr(interval, off)
292: unsigned interval;
293: u_long off;
294: {
295: struct ifnet ifnet;
296: u_long firstifnet;
297: register struct iftot *ip, *total;
298: register int line;
299: struct iftot *lastif, *sum, *interesting;
1.3 deraadt 300: struct ifnet_head ifhead; /* TAILQ_HEAD */
1.1 deraadt 301: int oldmask;
302:
1.3 deraadt 303: /*
304: * Find the pointer to the first ifnet structure. Replace
305: * the pointer to the TAILQ_HEAD with the actual pointer
306: * to the first list element.
307: */
308: if (kread(off, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 309: return;
1.3 deraadt 310: firstifnet = (u_long)ifhead.tqh_first;
311:
1.1 deraadt 312: lastif = iftot;
313: sum = iftot + MAXIF - 1;
314: total = sum - 1;
1.4 deraadt 315: interesting = (interface == NULL) ? iftot : NULL;
1.1 deraadt 316: for (off = firstifnet, ip = iftot; off;) {
317: if (kread(off, (char *)&ifnet, sizeof ifnet))
318: break;
1.6 deraadt 319: bzero(ip->ift_name, sizeof(ip->ift_name));
320: snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname);
321: if (interface && strcmp(ifnet.if_xname, interface) == 0)
1.1 deraadt 322: interesting = ip;
323: ip++;
324: if (ip >= iftot + MAXIF - 2)
325: break;
326: off = (u_long)ifnet.if_list.tqe_next;
1.4 deraadt 327: }
328: if (interesting == NULL) {
329: fprintf(stderr, "%s: %s: unknown interface\n",
330: __progname, interface);
331: exit(1);
1.1 deraadt 332: }
333: lastif = ip;
334:
335: (void)signal(SIGALRM, catchalarm);
336: signalled = NO;
337: (void)alarm(interval);
338: banner:
339: printf(" input %-6.6s output ", interesting->ift_name);
340: if (lastif - iftot > 0) {
341: if (dflag)
342: printf(" ");
343: printf(" input (Total) output");
344: }
345: for (ip = iftot; ip < iftot + MAXIF; ip++) {
346: ip->ift_ip = 0;
347: ip->ift_ie = 0;
348: ip->ift_op = 0;
349: ip->ift_oe = 0;
350: ip->ift_co = 0;
351: ip->ift_dr = 0;
352: }
353: putchar('\n');
354: printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
355: "packets", "errs", "packets", "errs", "colls");
356: if (dflag)
357: printf("%5.5s ", "drops");
358: if (lastif - iftot > 0)
359: printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
360: "packets", "errs", "packets", "errs", "colls");
361: if (dflag)
362: printf(" %5.5s", "drops");
363: putchar('\n');
364: fflush(stdout);
365: line = 0;
366: loop:
367: sum->ift_ip = 0;
368: sum->ift_ie = 0;
369: sum->ift_op = 0;
370: sum->ift_oe = 0;
371: sum->ift_co = 0;
372: sum->ift_dr = 0;
373: for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
374: if (kread(off, (char *)&ifnet, sizeof ifnet)) {
375: off = 0;
376: continue;
377: }
378: if (ip == interesting) {
1.11 millert 379: printf("%8ld %5ld %8ld %5ld %5ld",
1.1 deraadt 380: ifnet.if_ipackets - ip->ift_ip,
381: ifnet.if_ierrors - ip->ift_ie,
382: ifnet.if_opackets - ip->ift_op,
383: ifnet.if_oerrors - ip->ift_oe,
384: ifnet.if_collisions - ip->ift_co);
385: if (dflag)
386: printf(" %5d",
387: ifnet.if_snd.ifq_drops - ip->ift_dr);
388: }
389: ip->ift_ip = ifnet.if_ipackets;
390: ip->ift_ie = ifnet.if_ierrors;
391: ip->ift_op = ifnet.if_opackets;
392: ip->ift_oe = ifnet.if_oerrors;
393: ip->ift_co = ifnet.if_collisions;
394: ip->ift_dr = ifnet.if_snd.ifq_drops;
395: sum->ift_ip += ip->ift_ip;
396: sum->ift_ie += ip->ift_ie;
397: sum->ift_op += ip->ift_op;
398: sum->ift_oe += ip->ift_oe;
399: sum->ift_co += ip->ift_co;
400: sum->ift_dr += ip->ift_dr;
401: off = (u_long)ifnet.if_list.tqe_next;
402: }
403: if (lastif - iftot > 0) {
404: printf(" %8d %5d %8d %5d %5d",
405: sum->ift_ip - total->ift_ip,
406: sum->ift_ie - total->ift_ie,
407: sum->ift_op - total->ift_op,
408: sum->ift_oe - total->ift_oe,
409: sum->ift_co - total->ift_co);
410: if (dflag)
411: printf(" %5d", sum->ift_dr - total->ift_dr);
412: }
413: *total = *sum;
414: putchar('\n');
415: fflush(stdout);
416: line++;
417: oldmask = sigblock(sigmask(SIGALRM));
418: if (! signalled) {
419: sigpause(0);
420: }
421: sigsetmask(oldmask);
422: signalled = NO;
423: (void)alarm(interval);
424: if (line == 21)
425: goto banner;
426: goto loop;
427: /*NOTREACHED*/
428: }
429:
430: /*
431: * Called if an interval expires before sidewaysintpr has completed a loop.
432: * Sets a flag to not wait for the alarm.
433: */
434: static void
435: catchalarm(signo)
436: int signo;
437: {
438: signalled = YES;
439: }