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