]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.bin/netstat/if.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.bin / netstat / if.c
1 /*-
2  * Copyright (c) 1983, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)if.c        8.3 (Berkeley) 4/28/95";
33 #endif /* not lint */
34 #endif
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/types.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/time.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/ethernet.h>
51 #include <net/pfvar.h>
52 #include <net/if_pfsync.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netipx/ipx.h>
56 #include <netipx/ipx_if.h>
57 #include <arpa/inet.h>
58
59 #include <err.h>
60 #include <errno.h>
61 #include <libutil.h>
62 #ifdef INET6
63 #include <netdb.h>
64 #endif
65 #include <signal.h>
66 #include <stdint.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71
72 #include "netstat.h"
73
74 #define YES     1
75 #define NO      0
76
77 static void sidewaysintpr(int, u_long);
78 static void catchalarm(int);
79
80 #ifdef INET6
81 static char addr_buf[NI_MAXHOST];               /* for getnameinfo() */
82 #endif
83
84 /*
85  * Dump pfsync statistics structure.
86  */
87 void
88 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
89 {
90         struct pfsyncstats pfsyncstat, zerostat;
91         size_t len = sizeof(struct pfsyncstats);
92
93         if (live) {
94                 if (zflag)
95                         memset(&zerostat, 0, len);
96                 if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
97                     zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
98                         if (errno != ENOENT)
99                                 warn("sysctl: net.inet.pfsync.stats");
100                         return;
101                 }
102         } else
103                 kread(off, &pfsyncstat, len);
104
105         printf("%s:\n", name);
106
107 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
108         printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
109 #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
110         printf(m, (uintmax_t)pfsyncstat.f)
111
112         p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
113         p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
114         p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
115         p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
116         p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
117         p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
118         p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
119         p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
120         p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
121         p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
122         p(pfsyncs_stale, "\t\t%ju stale state%s\n");
123         p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
124         p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
125         p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
126         p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n");
127         p2(pfsyncs_oerrors, "\t\t%ju send error\n");
128 #undef p
129 #undef p2
130 }
131
132 /*
133  * Display a formatted value, or a '-' in the same space.
134  */
135 static void
136 show_stat(const char *fmt, int width, u_long value, short showvalue)
137 {
138         const char *lsep, *rsep;
139         char newfmt[32];
140
141         lsep = "";
142         if (strncmp(fmt, "LS", 2) == 0) {
143                 lsep = " ";
144                 fmt += 2;
145         }
146         rsep = " ";
147         if (strncmp(fmt, "NRS", 3) == 0) {
148                 rsep = "";
149                 fmt += 3;
150         }
151         if (showvalue == 0) {
152                 /* Print just dash. */
153                 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
154                 printf(newfmt, "-");
155                 return;
156         }
157
158         if (hflag) {
159                 char buf[5];
160
161                 /* Format in human readable form. */
162                 humanize_number(buf, sizeof(buf), (int64_t)value, "",
163                     HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
164                 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
165                 printf(newfmt, buf);
166         } else {
167                 /* Construct the format string. */
168                 sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
169                 printf(newfmt, value);
170         }
171 }
172
173 /*
174  * Print a description of the network interfaces.
175  */
176 void
177 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
178 {
179         struct ifnet ifnet;
180         struct ifnethead ifnethead;
181         union {
182                 struct ifaddr ifa;
183                 struct in_ifaddr in;
184 #ifdef INET6
185                 struct in6_ifaddr in6;
186 #endif
187                 struct ipx_ifaddr ipx;
188         } ifaddr;
189         u_long ifaddraddr;
190         u_long ifaddrfound;
191         u_long ifnetfound;
192         u_long opackets;
193         u_long ipackets;
194         u_long obytes;
195         u_long ibytes;
196         u_long omcasts;
197         u_long imcasts;
198         u_long oerrors;
199         u_long ierrors;
200         u_long idrops;
201         u_long collisions;
202         int drops;
203         struct sockaddr *sa = NULL;
204         char name[IFNAMSIZ];
205         short network_layer;
206         short link_layer;
207
208         if (ifnetaddr == 0) {
209                 printf("ifnet: symbol not defined\n");
210                 return;
211         }
212         if (interval1) {
213                 sidewaysintpr(interval1, ifnetaddr);
214                 return;
215         }
216         if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
217                 return;
218         ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
219         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
220                 return;
221
222         if (!pfunc) {
223                 if (Wflag)
224                         printf("%-7.7s", "Name");
225                 else
226                         printf("%-5.5s", "Name");
227                 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
228                     "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
229                 if (bflag)
230                         printf(" %10.10s","Ibytes");
231                 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
232                 if (bflag)
233                         printf(" %10.10s","Obytes");
234                 printf(" %5s", "Coll");
235                 if (dflag)
236                         printf(" %s", "Drop");
237                 putchar('\n');
238         }
239         ifaddraddr = 0;
240         while (ifnetaddr || ifaddraddr) {
241                 struct sockaddr_in *sockin;
242 #ifdef INET6
243                 struct sockaddr_in6 *sockin6;
244 #endif
245                 char *cp;
246                 int n, m;
247
248                 network_layer = 0;
249                 link_layer = 0;
250
251                 if (ifaddraddr == 0) {
252                         ifnetfound = ifnetaddr;
253                         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
254                                 return;
255                         strlcpy(name, ifnet.if_xname, sizeof(name));
256                         ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
257                         if (interface != 0 && strcmp(name, interface) != 0)
258                                 continue;
259                         cp = index(name, '\0');
260
261                         if (pfunc) {
262                                 (*pfunc)(name);
263                                 continue;
264                         }
265
266                         if ((ifnet.if_flags&IFF_UP) == 0)
267                                 *cp++ = '*';
268                         *cp = '\0';
269                         ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
270                 }
271                 ifaddrfound = ifaddraddr;
272
273                 /*
274                  * Get the interface stats.  These may get
275                  * overriden below on a per-interface basis.
276                  */
277                 opackets = ifnet.if_opackets;
278                 ipackets = ifnet.if_ipackets;
279                 obytes = ifnet.if_obytes;
280                 ibytes = ifnet.if_ibytes;
281                 omcasts = ifnet.if_omcasts;
282                 imcasts = ifnet.if_imcasts;
283                 oerrors = ifnet.if_oerrors;
284                 ierrors = ifnet.if_ierrors;
285                 idrops = ifnet.if_iqdrops;
286                 collisions = ifnet.if_collisions;
287                 drops = ifnet.if_snd.ifq_drops;
288
289                 if (ifaddraddr == 0) {
290                         if (Wflag)
291                                 printf("%-7.7s", name);
292                         else
293                                 printf("%-5.5s", name);
294                         printf(" %5lu ", ifnet.if_mtu);
295                         printf("%-13.13s ", "none");
296                         printf("%-17.17s ", "none");
297                 } else {
298                         if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
299                             != 0) {
300                                 ifaddraddr = 0;
301                                 continue;
302                         }
303 #define CP(x) ((char *)(x))
304                         cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
305                                 CP(&ifaddr);
306                         sa = (struct sockaddr *)cp;
307                         if (af != AF_UNSPEC && sa->sa_family != af) {
308                                 ifaddraddr =
309                                     (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
310                                 continue;
311                         }
312                         if (Wflag)
313                                 printf("%-7.7s", name);
314                         else
315                                 printf("%-5.5s", name);
316                         printf(" %5lu ", ifnet.if_mtu);
317                         switch (sa->sa_family) {
318                         case AF_UNSPEC:
319                                 printf("%-13.13s ", "none");
320                                 printf("%-15.15s ", "none");
321                                 break;
322                         case AF_INET:
323                                 sockin = (struct sockaddr_in *)sa;
324 #ifdef notdef
325                                 /* can't use inet_makeaddr because kernel
326                                  * keeps nets unshifted.
327                                  */
328                                 in = inet_makeaddr(ifaddr.in.ia_subnet,
329                                         INADDR_ANY);
330                                 printf("%-13.13s ", netname(in.s_addr,
331                                     ifaddr.in.ia_subnetmask));
332 #else
333                                 printf("%-13.13s ",
334                                     netname(htonl(ifaddr.in.ia_subnet),
335                                     ifaddr.in.ia_subnetmask));
336 #endif
337                                 printf("%-17.17s ",
338                                     routename(sockin->sin_addr.s_addr));
339
340                                 network_layer = 1;
341                                 break;
342 #ifdef INET6
343                         case AF_INET6:
344                                 sockin6 = (struct sockaddr_in6 *)sa;
345                                 in6_fillscopeid(&ifaddr.in6.ia_addr);
346                                 printf("%-13.13s ",
347                                        netname6(&ifaddr.in6.ia_addr,
348                                                 &ifaddr.in6.ia_prefixmask.sin6_addr));
349                                 in6_fillscopeid(sockin6);
350                                 getnameinfo(sa, sa->sa_len, addr_buf,
351                                     sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
352                                 printf("%-17.17s ", addr_buf);
353
354                                 network_layer = 1;
355                                 break;
356 #endif /*INET6*/
357                         case AF_IPX:
358                                 {
359                                 struct sockaddr_ipx *sipx =
360                                         (struct sockaddr_ipx *)sa;
361                                 u_long net;
362                                 char netnum[10];
363
364                                 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
365                                 sprintf(netnum, "%lx", (u_long)ntohl(net));
366                                 printf("ipx:%-8s  ", netnum);
367 /*                              printf("ipx:%-8s ", netname(net, 0L)); */
368                                 printf("%-17s ",
369                                     ipx_phost((struct sockaddr *)sipx));
370                                 }
371
372                                 network_layer = 1;
373                                 break;
374
375                         case AF_APPLETALK:
376                                 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
377                                 printf("%-11.11s  ",atalk_print(sa,0x0b) );
378                                 break;
379                         case AF_LINK:
380                                 {
381                                 struct sockaddr_dl *sdl =
382                                         (struct sockaddr_dl *)sa;
383                                 char linknum[10];
384                                 cp = (char *)LLADDR(sdl);
385                                 n = sdl->sdl_alen;
386                                 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
387                                 m = printf("%-13.13s ", linknum);
388                                 }
389                                 goto hexprint;
390                         default:
391                                 m = printf("(%d)", sa->sa_family);
392                                 for (cp = sa->sa_len + (char *)sa;
393                                         --cp > sa->sa_data && (*cp == 0);) {}
394                                 n = cp - sa->sa_data + 1;
395                                 cp = sa->sa_data;
396                         hexprint:
397                                 while ((--n >= 0) && (m < 30))
398                                         m += printf("%02x%c", *cp++ & 0xff,
399                                                     n > 0 ? ':' : ' ');
400                                 m = 32 - m;
401                                 while (m-- > 0)
402                                         putchar(' ');
403
404                                 link_layer = 1;
405                                 break;
406                         }
407
408                         /*
409                          * Fixup the statistics for interfaces that
410                          * update stats for their network addresses
411                          */
412                         if (network_layer) {
413                                 opackets = ifaddr.in.ia_ifa.if_opackets;
414                                 ipackets = ifaddr.in.ia_ifa.if_ipackets;
415                                 obytes = ifaddr.in.ia_ifa.if_obytes;
416                                 ibytes = ifaddr.in.ia_ifa.if_ibytes;
417                         }
418
419                         ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
420                 }
421
422                 show_stat("lu", 8, ipackets, link_layer|network_layer);
423                 show_stat("lu", 5, ierrors, link_layer);
424                 show_stat("lu", 5, idrops, link_layer);
425                 if (bflag)
426                         show_stat("lu", 10, ibytes, link_layer|network_layer);
427
428                 show_stat("lu", 8, opackets, link_layer|network_layer);
429                 show_stat("lu", 5, oerrors, link_layer);
430                 if (bflag)
431                         show_stat("lu", 10, obytes, link_layer|network_layer);
432
433                 show_stat("NRSlu", 5, collisions, link_layer);
434                 if (dflag)
435                         show_stat("LSd", 4, drops, link_layer);
436                 putchar('\n');
437
438                 if (aflag && ifaddrfound) {
439                         /*
440                          * Print family's multicast addresses
441                          */
442                         struct ifmultiaddr *multiaddr;
443                         struct ifmultiaddr ifma;
444                         union {
445                                 struct sockaddr sa;
446                                 struct sockaddr_in in;
447 #ifdef INET6
448                                 struct sockaddr_in6 in6;
449 #endif /* INET6 */
450                                 struct sockaddr_dl dl;
451                         } msa;
452                         const char *fmt;
453
454                         TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
455                                 if (kread((u_long)multiaddr, (char *)&ifma,
456                                           sizeof ifma) != 0)
457                                         break;
458                                 multiaddr = &ifma;
459                                 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
460                                           sizeof msa) != 0)
461                                         break;
462                                 if (msa.sa.sa_family != sa->sa_family)
463                                         continue;
464
465                                 fmt = 0;
466                                 switch (msa.sa.sa_family) {
467                                 case AF_INET:
468                                         fmt = routename(msa.in.sin_addr.s_addr);
469                                         break;
470 #ifdef INET6
471                                 case AF_INET6:
472                                         in6_fillscopeid(&msa.in6);
473                                         getnameinfo(&msa.sa, msa.sa.sa_len,
474                                             addr_buf, sizeof(addr_buf), 0, 0,
475                                             NI_NUMERICHOST);
476                                         printf("%*s %-19.19s(refs: %d)\n",
477                                                Wflag ? 27 : 25, "",
478                                                addr_buf, ifma.ifma_refcount);
479                                         break;
480 #endif /* INET6 */
481                                 case AF_LINK:
482                                         switch (msa.dl.sdl_type) {
483                                         case IFT_ETHER:
484                                         case IFT_FDDI:
485                                                 fmt = ether_ntoa(
486                                                         (struct ether_addr *)
487                                                         LLADDR(&msa.dl));
488                                                 break;
489                                         }
490                                         break;
491                                 }
492                                 if (fmt) {
493                                         printf("%*s %-17.17s",
494                                             Wflag ? 27 : 25, "", fmt);
495                                         if (msa.sa.sa_family == AF_LINK) {
496                                                 printf(" %8lu", imcasts);
497                                                 printf("%*s",
498                                                     bflag ? 17 : 6, "");
499                                                 printf(" %8lu", omcasts);
500                                         }
501                                         putchar('\n');
502                                 }
503                         }
504                 }
505         }
506 }
507
508 struct  iftot {
509         SLIST_ENTRY(iftot) chain;
510         char    ift_name[IFNAMSIZ];     /* interface name */
511         u_long  ift_ip;                 /* input packets */
512         u_long  ift_ie;                 /* input errors */
513         u_long  ift_id;                 /* input drops */
514         u_long  ift_op;                 /* output packets */
515         u_long  ift_oe;                 /* output errors */
516         u_long  ift_co;                 /* collisions */
517         u_int   ift_dr;                 /* drops */
518         u_long  ift_ib;                 /* input bytes */
519         u_long  ift_ob;                 /* output bytes */
520 };
521
522 u_char  signalled;                      /* set if alarm goes off "early" */
523
524 /*
525  * Print a running summary of interface statistics.
526  * Repeat display every interval1 seconds, showing statistics
527  * collected over that interval.  Assumes that interval1 is non-zero.
528  * First line printed at top of screen is always cumulative.
529  * XXX - should be rewritten to use ifmib(4).
530  */
531 static void
532 sidewaysintpr(int interval1, u_long off)
533 {
534         struct ifnet ifnet;
535         u_long firstifnet;
536         struct ifnethead ifnethead;
537         struct itimerval interval_it;
538         struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
539         int line;
540         int oldmask, first;
541         u_long interesting_off;
542
543         if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
544                 return;
545         firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
546
547         if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
548                 printf("malloc failed\n");
549                 exit(1);
550         }
551         memset(iftot, 0, sizeof(struct iftot));
552
553         interesting = NULL;
554         interesting_off = 0;
555         for (off = firstifnet, ip = iftot; off;) {
556                 char name[IFNAMSIZ];
557
558                 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
559                         break;
560                 strlcpy(name, ifnet.if_xname, sizeof(name));
561                 if (interface && strcmp(name, interface) == 0) {
562                         interesting = ip;
563                         interesting_off = off;
564                 }
565                 snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);
566                 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
567                         printf("malloc failed\n");
568                         exit(1);
569                 }
570                 memset(ipn, 0, sizeof(struct iftot));
571                 SLIST_NEXT(ip, chain) = ipn;
572                 ip = ipn;
573                 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
574         }
575         if (interface && interesting == NULL)
576                 errx(1, "%s: unknown interface", interface);
577         if ((total = malloc(sizeof(struct iftot))) == NULL) {
578                 printf("malloc failed\n");
579                 exit(1);
580         }
581         memset(total, 0, sizeof(struct iftot));
582         if ((sum = malloc(sizeof(struct iftot))) == NULL) {
583                 printf("malloc failed\n");
584                 exit(1);
585         }
586         memset(sum, 0, sizeof(struct iftot));
587
588         (void)signal(SIGALRM, catchalarm);
589         signalled = NO;
590         interval_it.it_interval.tv_sec = interval1;
591         interval_it.it_interval.tv_usec = 0;
592         interval_it.it_value = interval_it.it_interval;
593         setitimer(ITIMER_REAL, &interval_it, NULL);
594         first = 1;
595 banner:
596         printf("%17s %14s %16s", "input",
597             interesting ? interesting->ift_name : "(Total)", "output");
598         putchar('\n');
599         printf("%10s %5s %5s %10s %10s %5s %10s %5s",
600             "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
601             "colls");
602         if (dflag)
603                 printf(" %5.5s", "drops");
604         putchar('\n');
605         fflush(stdout);
606         line = 0;
607 loop:
608         if (interesting != NULL) {
609                 ip = interesting;
610                 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
611                         printf("???\n");
612                         exit(1);
613                 };
614                 if (!first) {
615                         show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
616                         show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
617                         show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
618                         show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
619                         show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
620                         show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
621                         show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
622                         show_stat("NRSlu", 5,
623                             ifnet.if_collisions - ip->ift_co, 1);
624                         if (dflag)
625                                 show_stat("LSu", 5,
626                                     ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
627                 }
628                 ip->ift_ip = ifnet.if_ipackets;
629                 ip->ift_ie = ifnet.if_ierrors;
630                 ip->ift_id = ifnet.if_iqdrops;
631                 ip->ift_ib = ifnet.if_ibytes;
632                 ip->ift_op = ifnet.if_opackets;
633                 ip->ift_oe = ifnet.if_oerrors;
634                 ip->ift_ob = ifnet.if_obytes;
635                 ip->ift_co = ifnet.if_collisions;
636                 ip->ift_dr = ifnet.if_snd.ifq_drops;
637         } else {
638                 sum->ift_ip = 0;
639                 sum->ift_ie = 0;
640                 sum->ift_id = 0;
641                 sum->ift_ib = 0;
642                 sum->ift_op = 0;
643                 sum->ift_oe = 0;
644                 sum->ift_ob = 0;
645                 sum->ift_co = 0;
646                 sum->ift_dr = 0;
647                 for (off = firstifnet, ip = iftot;
648                      off && SLIST_NEXT(ip, chain) != NULL;
649                      ip = SLIST_NEXT(ip, chain)) {
650                         if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
651                                 off = 0;
652                                 continue;
653                         }
654                         sum->ift_ip += ifnet.if_ipackets;
655                         sum->ift_ie += ifnet.if_ierrors;
656                         sum->ift_id += ifnet.if_iqdrops;
657                         sum->ift_ib += ifnet.if_ibytes;
658                         sum->ift_op += ifnet.if_opackets;
659                         sum->ift_oe += ifnet.if_oerrors;
660                         sum->ift_ob += ifnet.if_obytes;
661                         sum->ift_co += ifnet.if_collisions;
662                         sum->ift_dr += ifnet.if_snd.ifq_drops;
663                         off = (u_long)TAILQ_NEXT(&ifnet, if_link);
664                 }
665                 if (!first) {
666                         show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
667                         show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
668                         show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
669                         show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
670                         show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
671                         show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
672                         show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
673                         show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
674                         if (dflag)
675                                 show_stat("LSu", 5,
676                                     sum->ift_dr - total->ift_dr, 1);
677                 }
678                 *total = *sum;
679         }
680         if (!first)
681                 putchar('\n');
682         fflush(stdout);
683         if ((noutputs != 0) && (--noutputs == 0))
684                 exit(0);
685         oldmask = sigblock(sigmask(SIGALRM));
686         while (!signalled)
687                 sigpause(0);
688         signalled = NO;
689         sigsetmask(oldmask);
690         line++;
691         first = 0;
692         if (line == 21)
693                 goto banner;
694         else
695                 goto loop;
696         /*NOTREACHED*/
697 }
698
699 /*
700  * Set a flag to indicate that a signal from the periodic itimer has been
701  * caught.
702  */
703 static void
704 catchalarm(int signo __unused)
705 {
706         signalled = YES;
707 }