Annotation of src/usr.bin/netstat/if.c, Revision 1.21
1.21 ! mickey 1: /* $OpenBSD: if.c,v 1.20 2000/02/05 18:46:50 itojun 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.21 ! mickey 41: static char *rcsid = "$OpenBSD: if.c,v 1.20 2000/02/05 18:46:50 itojun 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;
1.18 itojun 91: #ifdef INET6
92: struct in6_ifaddr in6;
93: #endif
1.1 deraadt 94: struct ns_ifaddr ns;
1.8 mickey 95: struct ipx_ifaddr ipx;
1.1 deraadt 96: struct iso_ifaddr iso;
97: } ifaddr;
98: u_long ifaddraddr;
99: struct sockaddr *sa;
1.3 deraadt 100: struct ifnet_head ifhead; /* TAILQ_HEAD */
101: char name[IFNAMSIZ];
1.20 itojun 102: int n;
1.1 deraadt 103:
104: if (ifnetaddr == 0) {
105: printf("ifnet: symbol not defined\n");
106: return;
107: }
108: if (interval) {
109: sidewaysintpr((unsigned)interval, ifnetaddr);
110: return;
111: }
1.3 deraadt 112:
113: /*
114: * Find the pointer to the first ifnet structure. Replace
115: * the pointer to the TAILQ_HEAD with the actual pointer
116: * to the first list element.
117: */
118: if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 119: return;
1.3 deraadt 120: ifnetaddr = (u_long)ifhead.tqh_first;
121:
1.16 mickey 122: printf("%-7.7s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s",
1.1 deraadt 123: "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
124: "Opkts", "Oerrs");
125: printf(" %5s", "Coll");
126: if (tflag)
127: printf(" %s", "Time");
128: if (dflag)
129: printf(" %s", "Drop");
130: putchar('\n');
131: ifaddraddr = 0;
132: while (ifnetaddr || ifaddraddr) {
133: struct sockaddr_in *sin;
1.18 itojun 134: #ifdef INET6
135: struct sockaddr_in6 *sin6;
136: #endif
1.1 deraadt 137: register char *cp;
138: int n, m;
139:
140: if (ifaddraddr == 0) {
1.3 deraadt 141: if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1.1 deraadt 142: return;
1.3 deraadt 143: bcopy(ifnet.if_xname, name, IFNAMSIZ);
144: name[IFNAMSIZ - 1] = '\0'; /* sanity */
1.1 deraadt 145: ifnetaddr = (u_long)ifnet.if_list.tqe_next;
1.3 deraadt 146: if (interface != 0 && strcmp(name, interface) != 0)
1.1 deraadt 147: continue;
1.10 millert 148: cp = strchr(name, '\0');
1.1 deraadt 149: if ((ifnet.if_flags & IFF_UP) == 0)
150: *cp++ = '*';
151: *cp = '\0';
152: ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
153: }
1.16 mickey 154: printf("%-7.7s %-5ld ", name, ifnet.if_mtu);
1.1 deraadt 155: if (ifaddraddr == 0) {
156: printf("%-11.11s ", "none");
157: printf("%-15.15s ", "none");
158: } else {
159: if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
160: ifaddraddr = 0;
161: continue;
162: }
163: #define CP(x) ((char *)(x))
164: cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
165: CP(&ifaddr); sa = (struct sockaddr *)cp;
166: switch (sa->sa_family) {
167: case AF_UNSPEC:
168: printf("%-11.11s ", "none");
169: printf("%-17.17s ", "none");
170: break;
171: case AF_INET:
172: sin = (struct sockaddr_in *)sa;
173: #ifdef notdef
174: /* can't use inet_makeaddr because kernel
175: * keeps nets unshifted.
176: */
177: in = inet_makeaddr(ifaddr.in.ia_subnet,
178: INADDR_ANY);
1.20 itojun 179: cp = netname(in.s_addr,
180: ifaddr.in.ia_subnetmask);
1.1 deraadt 181: #else
1.20 itojun 182: cp = netname(ifaddr.in.ia_subnet,
183: ifaddr.in.ia_subnetmask);
1.1 deraadt 184: #endif
1.20 itojun 185: if (vflag)
186: n = strlen(cp) < 11 ? 11 : strlen(cp);
187: else
188: n = 11;
189: printf("%-*.*s ", n, n, cp);
190: cp = routename(sin->sin_addr.s_addr);
191: if (vflag)
192: n = strlen(cp) < 17 ? 17 : strlen(cp);
193: else
194: n = 17;
195: printf("%-*.*s ", n, n, cp);
1.1 deraadt 196:
197: if (aflag) {
198: u_long multiaddr;
199: struct in_multi inm;
200:
201: multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
202: while (multiaddr != 0) {
203: kread(multiaddr, (char *)&inm,
204: sizeof inm);
1.19 itojun 205: printf("\n%25s %-17.17s ", "",
1.1 deraadt 206: routename(inm.inm_addr.s_addr));
207: multiaddr = (u_long)inm.inm_list.le_next;
208: }
1.8 mickey 209: }
210: break;
1.18 itojun 211: #ifdef INET6
212: case AF_INET6:
213: sin6 = (struct sockaddr_in6 *)sa;
1.20 itojun 214: #ifdef KAME_SCOPEID
215: if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
216: sin6->sin6_scope_id =
217: ntohs(*(u_int16_t *)
218: &sin6->sin6_addr.s6_addr[2]);
219: /* too little width */
220: if (!vflag)
221: sin6->sin6_scope_id = 0;
222: sin6->sin6_addr.s6_addr[2] = 0;
223: sin6->sin6_addr.s6_addr[3] = 0;
224: }
225: #endif
226: cp = netname6(&ifaddr.in6.ia_addr,
227: &ifaddr.in6.ia_prefixmask.sin6_addr);
228: if (vflag)
229: n = strlen(cp) < 11 ? 11 : strlen(cp);
230: else
231: n = 11;
232: printf("%-*.*s ", n, n, cp);
233: cp = routename6(sin6);
234: if (vflag)
235: n = strlen(cp) < 17 ? 17 : strlen(cp);
236: else
237: n = 17;
238: printf("%-*.*s ", n, n, cp);
1.19 itojun 239: if (aflag) {
240: u_long multiaddr;
241: struct in6_multi inm;
242: char hbuf[INET6_ADDRSTRLEN];
243:
244: multiaddr = (u_long)ifaddr.in6.ia6_multiaddrs.lh_first;
245: while (multiaddr != 0) {
246: kread(multiaddr, (char *)&inm,
247: sizeof inm);
248: inet_ntop(AF_INET6, &inm.in6m_addr,
249: hbuf, sizeof(hbuf));
250: if (vflag)
251: n = strlen(hbuf) < 17 ? 17 : strlen(hbuf);
252: else
253: n = 17;
254: printf("\n%25s %-*.*s ", "",
255: n, n, hbuf);
256: multiaddr = (u_long)inm.in6m_entry.le_next;
257: }
258: }
1.18 itojun 259: break;
260: #endif
1.8 mickey 261: case AF_IPX:
262: {
263: struct sockaddr_ipx *sipx =
264: (struct sockaddr_ipx *)sa;
265: u_long net;
266: char netnum[8];
267:
1.17 deraadt 268: *(union ipx_net *)&net = sipx->sipx_addr.ipx_net;
269: snprintf(netnum, sizeof netnum, "%xH",
270: ntohl(net));
1.8 mickey 271: upHex(netnum);
272: printf("ipx:%-8s", netnum);
273: printf("%-17s ",
274: ipx_phost((struct sockaddr *)sipx));
1.1 deraadt 275: }
1.15 denny 276: break;
277: case AF_APPLETALK:
278: printf("atlk:%-12s",atalk_print(sa,0x10) );
279: printf("%-12s ",atalk_print(sa,0x0b) );
1.1 deraadt 280: break;
281: case AF_NS:
282: {
283: struct sockaddr_ns *sns =
284: (struct sockaddr_ns *)sa;
285: u_long net;
286: char netnum[8];
287:
1.17 deraadt 288: *(union ns_net *)&net = sns->sns_addr.x_net;
289: snprintf(netnum, sizeof netnum, "%xH",
290: ntohl(net));
1.1 deraadt 291: upHex(netnum);
292: printf("ns:%-8s ", netnum);
293: printf("%-17s ",
294: ns_phost((struct sockaddr *)sns));
295: }
296: break;
297: case AF_LINK:
298: {
299: struct sockaddr_dl *sdl =
300: (struct sockaddr_dl *)sa;
1.5 deraadt 301: m = printf("%-11.11s ", "<Link>");
302: if (sdl->sdl_type == IFT_ETHER ||
303: sdl->sdl_type == IFT_FDDI)
304: printf("%-17.17s ",
1.11 millert 305: ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1.5 deraadt 306: else {
307: cp = (char *)LLADDR(sdl);
308: n = sdl->sdl_alen;
309: goto hexprint;
310: }
1.1 deraadt 311: }
1.5 deraadt 312: break;
1.1 deraadt 313: default:
314: m = printf("(%d)", sa->sa_family);
315: for (cp = sa->sa_len + (char *)sa;
316: --cp > sa->sa_data && (*cp == 0);) {}
317: n = cp - sa->sa_data + 1;
318: cp = sa->sa_data;
319: hexprint:
320: while (--n >= 0)
321: m += printf("%x%c", *cp++ & 0xff,
322: n > 0 ? '.' : ' ');
323: m = 30 - m;
324: while (m-- > 0)
325: putchar(' ');
326: break;
327: }
328: ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
329: }
1.21 ! mickey 330: printf("%8lu %5lu %8lu %5lu %5lu",
1.1 deraadt 331: ifnet.if_ipackets, ifnet.if_ierrors,
332: ifnet.if_opackets, ifnet.if_oerrors,
333: ifnet.if_collisions);
334: if (tflag)
335: printf(" %3d", ifnet.if_timer);
336: if (dflag)
337: printf(" %3d", ifnet.if_snd.ifq_drops);
338: putchar('\n');
339: }
340: }
341:
1.4 deraadt 342: #define MAXIF 100
1.1 deraadt 343: struct iftot {
1.3 deraadt 344: char ift_name[IFNAMSIZ]; /* interface name */
1.1 deraadt 345: int ift_ip; /* input packets */
346: int ift_ie; /* input errors */
347: int ift_op; /* output packets */
348: int ift_oe; /* output errors */
349: int ift_co; /* collisions */
350: int ift_dr; /* drops */
351: } iftot[MAXIF];
352:
353: u_char signalled; /* set if alarm goes off "early" */
354:
355: /*
356: * Print a running summary of interface statistics.
357: * Repeat display every interval seconds, showing statistics
358: * collected over that interval. Assumes that interval is non-zero.
359: * First line printed at top of screen is always cumulative.
360: */
361: static void
362: sidewaysintpr(interval, off)
363: unsigned interval;
364: u_long off;
365: {
366: struct ifnet ifnet;
367: u_long firstifnet;
368: register struct iftot *ip, *total;
369: register int line;
370: struct iftot *lastif, *sum, *interesting;
1.3 deraadt 371: struct ifnet_head ifhead; /* TAILQ_HEAD */
1.1 deraadt 372: int oldmask;
373:
1.3 deraadt 374: /*
375: * Find the pointer to the first ifnet structure. Replace
376: * the pointer to the TAILQ_HEAD with the actual pointer
377: * to the first list element.
378: */
379: if (kread(off, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 380: return;
1.3 deraadt 381: firstifnet = (u_long)ifhead.tqh_first;
382:
1.1 deraadt 383: lastif = iftot;
384: sum = iftot + MAXIF - 1;
385: total = sum - 1;
1.4 deraadt 386: interesting = (interface == NULL) ? iftot : NULL;
1.1 deraadt 387: for (off = firstifnet, ip = iftot; off;) {
388: if (kread(off, (char *)&ifnet, sizeof ifnet))
389: break;
1.6 deraadt 390: bzero(ip->ift_name, sizeof(ip->ift_name));
391: snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname);
392: if (interface && strcmp(ifnet.if_xname, interface) == 0)
1.1 deraadt 393: interesting = ip;
394: ip++;
395: if (ip >= iftot + MAXIF - 2)
396: break;
397: off = (u_long)ifnet.if_list.tqe_next;
1.4 deraadt 398: }
399: if (interesting == NULL) {
400: fprintf(stderr, "%s: %s: unknown interface\n",
401: __progname, interface);
402: exit(1);
1.1 deraadt 403: }
404: lastif = ip;
405:
406: (void)signal(SIGALRM, catchalarm);
407: signalled = NO;
408: (void)alarm(interval);
409: banner:
410: printf(" input %-6.6s output ", interesting->ift_name);
411: if (lastif - iftot > 0) {
412: if (dflag)
413: printf(" ");
414: printf(" input (Total) output");
415: }
416: for (ip = iftot; ip < iftot + MAXIF; ip++) {
417: ip->ift_ip = 0;
418: ip->ift_ie = 0;
419: ip->ift_op = 0;
420: ip->ift_oe = 0;
421: ip->ift_co = 0;
422: ip->ift_dr = 0;
423: }
424: putchar('\n');
425: printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
426: "packets", "errs", "packets", "errs", "colls");
427: if (dflag)
428: printf("%5.5s ", "drops");
429: if (lastif - iftot > 0)
430: printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
431: "packets", "errs", "packets", "errs", "colls");
432: if (dflag)
433: printf(" %5.5s", "drops");
434: putchar('\n');
435: fflush(stdout);
436: line = 0;
437: loop:
438: sum->ift_ip = 0;
439: sum->ift_ie = 0;
440: sum->ift_op = 0;
441: sum->ift_oe = 0;
442: sum->ift_co = 0;
443: sum->ift_dr = 0;
444: for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
445: if (kread(off, (char *)&ifnet, sizeof ifnet)) {
446: off = 0;
447: continue;
448: }
449: if (ip == interesting) {
1.11 millert 450: printf("%8ld %5ld %8ld %5ld %5ld",
1.1 deraadt 451: ifnet.if_ipackets - ip->ift_ip,
452: ifnet.if_ierrors - ip->ift_ie,
453: ifnet.if_opackets - ip->ift_op,
454: ifnet.if_oerrors - ip->ift_oe,
455: ifnet.if_collisions - ip->ift_co);
456: if (dflag)
457: printf(" %5d",
458: ifnet.if_snd.ifq_drops - ip->ift_dr);
459: }
460: ip->ift_ip = ifnet.if_ipackets;
461: ip->ift_ie = ifnet.if_ierrors;
462: ip->ift_op = ifnet.if_opackets;
463: ip->ift_oe = ifnet.if_oerrors;
464: ip->ift_co = ifnet.if_collisions;
465: ip->ift_dr = ifnet.if_snd.ifq_drops;
466: sum->ift_ip += ip->ift_ip;
467: sum->ift_ie += ip->ift_ie;
468: sum->ift_op += ip->ift_op;
469: sum->ift_oe += ip->ift_oe;
470: sum->ift_co += ip->ift_co;
471: sum->ift_dr += ip->ift_dr;
472: off = (u_long)ifnet.if_list.tqe_next;
473: }
474: if (lastif - iftot > 0) {
475: printf(" %8d %5d %8d %5d %5d",
476: sum->ift_ip - total->ift_ip,
477: sum->ift_ie - total->ift_ie,
478: sum->ift_op - total->ift_op,
479: sum->ift_oe - total->ift_oe,
480: sum->ift_co - total->ift_co);
481: if (dflag)
482: printf(" %5d", sum->ift_dr - total->ift_dr);
483: }
484: *total = *sum;
485: putchar('\n');
486: fflush(stdout);
487: line++;
488: oldmask = sigblock(sigmask(SIGALRM));
489: if (! signalled) {
490: sigpause(0);
491: }
492: sigsetmask(oldmask);
493: signalled = NO;
494: (void)alarm(interval);
495: if (line == 21)
496: goto banner;
497: goto loop;
498: /*NOTREACHED*/
499: }
500:
501: /*
502: * Called if an interval expires before sidewaysintpr has completed a loop.
503: * Sets a flag to not wait for the alarm.
504: */
505: static void
506: catchalarm(signo)
507: int signo;
508: {
509: signalled = YES;
510: }