]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/netstat/if.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 idrops;
202         u_long collisions;
203         short timer;
204         int drops;
205         struct sockaddr *sa = NULL;
206         char name[IFNAMSIZ];
207         short network_layer;
208         short link_layer;
209
210         if (ifnetaddr == 0) {
211                 printf("ifnet: symbol not defined\n");
212                 return;
213         }
214         if (interval1) {
215                 sidewaysintpr(interval1, ifnetaddr);
216                 return;
217         }
218         if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
219                 return;
220         ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
221         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
222                 return;
223
224         if (!pfunc) {
225                 if (Wflag)
226                         printf("%-7.7s", "Name");
227                 else
228                         printf("%-5.5s", "Name");
229                 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
230                     "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
231                 if (bflag)
232                         printf(" %10.10s","Ibytes");
233                 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
234                 if (bflag)
235                         printf(" %10.10s","Obytes");
236                 printf(" %5s", "Coll");
237                 if (tflag)
238                         printf(" %s", "Time");
239                 if (dflag)
240                         printf(" %s", "Drop");
241                 putchar('\n');
242         }
243         ifaddraddr = 0;
244         while (ifnetaddr || ifaddraddr) {
245                 struct sockaddr_in *sockin;
246 #ifdef INET6
247                 struct sockaddr_in6 *sockin6;
248 #endif
249                 char *cp;
250                 int n, m;
251
252                 network_layer = 0;
253                 link_layer = 0;
254
255                 if (ifaddraddr == 0) {
256                         ifnetfound = ifnetaddr;
257                         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
258                                 return;
259                         strlcpy(name, ifnet.if_xname, sizeof(name));
260                         ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
261                         if (interface != 0 && strcmp(name, interface) != 0)
262                                 continue;
263                         cp = index(name, '\0');
264
265                         if (pfunc) {
266                                 (*pfunc)(name);
267                                 continue;
268                         }
269
270                         if ((ifnet.if_flags&IFF_UP) == 0)
271                                 *cp++ = '*';
272                         *cp = '\0';
273                         ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
274                 }
275                 ifaddrfound = ifaddraddr;
276
277                 /*
278                  * Get the interface stats.  These may get
279                  * overriden below on a per-interface basis.
280                  */
281                 opackets = ifnet.if_opackets;
282                 ipackets = ifnet.if_ipackets;
283                 obytes = ifnet.if_obytes;
284                 ibytes = ifnet.if_ibytes;
285                 omcasts = ifnet.if_omcasts;
286                 imcasts = ifnet.if_imcasts;
287                 oerrors = ifnet.if_oerrors;
288                 ierrors = ifnet.if_ierrors;
289                 idrops = ifnet.if_iqdrops;
290                 collisions = ifnet.if_collisions;
291                 timer = ifnet.if_timer;
292                 drops = ifnet.if_snd.ifq_drops;
293
294                 if (ifaddraddr == 0) {
295                         if (Wflag)
296                                 printf("%-7.7s", name);
297                         else
298                                 printf("%-5.5s", name);
299                         printf(" %5lu ", ifnet.if_mtu);
300                         printf("%-13.13s ", "none");
301                         printf("%-17.17s ", "none");
302                 } else {
303                         if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
304                             != 0) {
305                                 ifaddraddr = 0;
306                                 continue;
307                         }
308 #define CP(x) ((char *)(x))
309                         cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
310                                 CP(&ifaddr);
311                         sa = (struct sockaddr *)cp;
312                         if (af != AF_UNSPEC && sa->sa_family != af) {
313                                 ifaddraddr =
314                                     (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
315                                 continue;
316                         }
317                         if (Wflag)
318                                 printf("%-7.7s", name);
319                         else
320                                 printf("%-5.5s", name);
321                         printf(" %5lu ", ifnet.if_mtu);
322                         switch (sa->sa_family) {
323                         case AF_UNSPEC:
324                                 printf("%-13.13s ", "none");
325                                 printf("%-15.15s ", "none");
326                                 break;
327                         case AF_INET:
328                                 sockin = (struct sockaddr_in *)sa;
329 #ifdef notdef
330                                 /* can't use inet_makeaddr because kernel
331                                  * keeps nets unshifted.
332                                  */
333                                 in = inet_makeaddr(ifaddr.in.ia_subnet,
334                                         INADDR_ANY);
335                                 printf("%-13.13s ", netname(in.s_addr,
336                                     ifaddr.in.ia_subnetmask));
337 #else
338                                 printf("%-13.13s ",
339                                     netname(htonl(ifaddr.in.ia_subnet),
340                                     ifaddr.in.ia_subnetmask));
341 #endif
342                                 printf("%-17.17s ",
343                                     routename(sockin->sin_addr.s_addr));
344
345                                 network_layer = 1;
346                                 break;
347 #ifdef INET6
348                         case AF_INET6:
349                                 sockin6 = (struct sockaddr_in6 *)sa;
350                                 printf("%-13.13s ",
351                                        netname6(&ifaddr.in6.ia_addr,
352                                                 &ifaddr.in6.ia_prefixmask.sin6_addr));
353                                 printf("%-17.17s ",
354                                     inet_ntop(AF_INET6,
355                                         &sockin6->sin6_addr,
356                                         ntop_buf, sizeof(ntop_buf)));
357
358                                 network_layer = 1;
359                                 break;
360 #endif /*INET6*/
361                         case AF_IPX:
362                                 {
363                                 struct sockaddr_ipx *sipx =
364                                         (struct sockaddr_ipx *)sa;
365                                 u_long net;
366                                 char netnum[10];
367
368                                 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
369                                 sprintf(netnum, "%lx", (u_long)ntohl(net));
370                                 printf("ipx:%-8s  ", netnum);
371 /*                              printf("ipx:%-8s ", netname(net, 0L)); */
372                                 printf("%-17s ",
373                                     ipx_phost((struct sockaddr *)sipx));
374                                 }
375
376                                 network_layer = 1;
377                                 break;
378
379                         case AF_APPLETALK:
380                                 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
381                                 printf("%-11.11s  ",atalk_print(sa,0x0b) );
382                                 break;
383                         case AF_LINK:
384                                 {
385                                 struct sockaddr_dl *sdl =
386                                         (struct sockaddr_dl *)sa;
387                                 char linknum[10];
388                                 cp = (char *)LLADDR(sdl);
389                                 n = sdl->sdl_alen;
390                                 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
391                                 m = printf("%-13.13s ", linknum);
392                                 }
393                                 goto hexprint;
394                         default:
395                                 m = printf("(%d)", sa->sa_family);
396                                 for (cp = sa->sa_len + (char *)sa;
397                                         --cp > sa->sa_data && (*cp == 0);) {}
398                                 n = cp - sa->sa_data + 1;
399                                 cp = sa->sa_data;
400                         hexprint:
401                                 while (--n >= 0)
402                                         m += printf("%02x%c", *cp++ & 0xff,
403                                                     n > 0 ? ':' : ' ');
404                                 m = 32 - m;
405                                 while (m-- > 0)
406                                         putchar(' ');
407
408                                 link_layer = 1;
409                                 break;
410                         }
411
412                         /*
413                          * Fixup the statistics for interfaces that
414                          * update stats for their network addresses
415                          */
416                         if (network_layer) {
417                                 opackets = ifaddr.in.ia_ifa.if_opackets;
418                                 ipackets = ifaddr.in.ia_ifa.if_ipackets;
419                                 obytes = ifaddr.in.ia_ifa.if_obytes;
420                                 ibytes = ifaddr.in.ia_ifa.if_ibytes;
421                         }
422
423                         ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
424                 }
425
426                 show_stat("lu", 8, ipackets, link_layer|network_layer);
427                 show_stat("lu", 5, ierrors, link_layer);
428                 show_stat("lu", 5, idrops, link_layer);
429                 if (bflag)
430                         show_stat("lu", 10, ibytes, link_layer|network_layer);
431
432                 show_stat("lu", 8, opackets, link_layer|network_layer);
433                 show_stat("lu", 5, oerrors, link_layer);
434                 if (bflag)
435                         show_stat("lu", 10, obytes, link_layer|network_layer);
436
437                 show_stat("NRSlu", 5, collisions, link_layer);
438                 if (tflag)
439                         show_stat("LSd", 4, timer, link_layer);
440                 if (dflag)
441                         show_stat("LSd", 4, drops, link_layer);
442                 putchar('\n');
443
444                 if (aflag && ifaddrfound) {
445                         /*
446                          * Print family's multicast addresses
447                          */
448                         struct ifmultiaddr *multiaddr;
449                         struct ifmultiaddr ifma;
450                         union {
451                                 struct sockaddr sa;
452                                 struct sockaddr_in in;
453 #ifdef INET6
454                                 struct sockaddr_in6 in6;
455 #endif /* INET6 */
456                                 struct sockaddr_dl dl;
457                         } msa;
458                         const char *fmt;
459
460                         TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
461                                 if (kread((u_long)multiaddr, (char *)&ifma,
462                                           sizeof ifma) != 0)
463                                         break;
464                                 multiaddr = &ifma;
465                                 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
466                                           sizeof msa) != 0)
467                                         break;
468                                 if (msa.sa.sa_family != sa->sa_family)
469                                         continue;
470
471                                 fmt = 0;
472                                 switch (msa.sa.sa_family) {
473                                 case AF_INET:
474                                         fmt = routename(msa.in.sin_addr.s_addr);
475                                         break;
476 #ifdef INET6
477                                 case AF_INET6:
478                                         printf("%*s %-19.19s(refs: %d)\n",
479                                                Wflag ? 27 : 25, "",
480                                                inet_ntop(AF_INET6,
481                                                          &msa.in6.sin6_addr,
482                                                          ntop_buf,
483                                                          sizeof(ntop_buf)),
484                                                ifma.ifma_refcount);
485                                         break;
486 #endif /* INET6 */
487                                 case AF_LINK:
488                                         switch (msa.dl.sdl_type) {
489                                         case IFT_ETHER:
490                                         case IFT_FDDI:
491                                                 fmt = ether_ntoa(
492                                                         (struct ether_addr *)
493                                                         LLADDR(&msa.dl));
494                                                 break;
495                                         }
496                                         break;
497                                 }
498                                 if (fmt) {
499                                         printf("%*s %-17.17s",
500                                             Wflag ? 27 : 25, "", fmt);
501                                         if (msa.sa.sa_family == AF_LINK) {
502                                                 printf(" %8lu", imcasts);
503                                                 printf("%*s",
504                                                     bflag ? 17 : 6, "");
505                                                 printf(" %8lu", omcasts);
506                                         }
507                                         putchar('\n');
508                                 }
509                         }
510                 }
511         }
512 }
513
514 struct  iftot {
515         SLIST_ENTRY(iftot) chain;
516         char    ift_name[IFNAMSIZ];     /* interface name */
517         u_long  ift_ip;                 /* input packets */
518         u_long  ift_ie;                 /* input errors */
519         u_long  ift_id;                 /* input drops */
520         u_long  ift_op;                 /* output packets */
521         u_long  ift_oe;                 /* output errors */
522         u_long  ift_co;                 /* collisions */
523         u_int   ift_dr;                 /* drops */
524         u_long  ift_ib;                 /* input bytes */
525         u_long  ift_ob;                 /* output bytes */
526 };
527
528 u_char  signalled;                      /* set if alarm goes off "early" */
529
530 /*
531  * Print a running summary of interface statistics.
532  * Repeat display every interval1 seconds, showing statistics
533  * collected over that interval.  Assumes that interval1 is non-zero.
534  * First line printed at top of screen is always cumulative.
535  * XXX - should be rewritten to use ifmib(4).
536  */
537 static void
538 sidewaysintpr(int interval1, u_long off)
539 {
540         struct ifnet ifnet;
541         u_long firstifnet;
542         struct ifnethead ifnethead;
543         struct itimerval interval_it;
544         struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
545         int line;
546         int oldmask, first;
547         u_long interesting_off;
548
549         if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
550                 return;
551         firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
552
553         if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
554                 printf("malloc failed\n");
555                 exit(1);
556         }
557         memset(iftot, 0, sizeof(struct iftot));
558
559         interesting = NULL;
560         interesting_off = 0;
561         for (off = firstifnet, ip = iftot; off;) {
562                 char name[IFNAMSIZ];
563
564                 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
565                         break;
566                 strlcpy(name, ifnet.if_xname, sizeof(name));
567                 if (interface && strcmp(name, interface) == 0) {
568                         interesting = ip;
569                         interesting_off = off;
570                 }
571                 snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
572                 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
573                         printf("malloc failed\n");
574                         exit(1);
575                 }
576                 memset(ipn, 0, sizeof(struct iftot));
577                 SLIST_NEXT(ip, chain) = ipn;
578                 ip = ipn;
579                 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
580         }
581         if (interface && interesting == NULL)
582                 errx(1, "%s: unknown interface", interface);
583         if ((total = malloc(sizeof(struct iftot))) == NULL) {
584                 printf("malloc failed\n");
585                 exit(1);
586         }
587         memset(total, 0, sizeof(struct iftot));
588         if ((sum = malloc(sizeof(struct iftot))) == NULL) {
589                 printf("malloc failed\n");
590                 exit(1);
591         }
592         memset(sum, 0, sizeof(struct iftot));
593
594         (void)signal(SIGALRM, catchalarm);
595         signalled = NO;
596         interval_it.it_interval.tv_sec = interval1;
597         interval_it.it_interval.tv_usec = 0;
598         interval_it.it_value = interval_it.it_interval;
599         setitimer(ITIMER_REAL, &interval_it, NULL);
600         first = 1;
601 banner:
602         printf("%17s %14s %16s", "input",
603             interesting ? interesting->ift_name : "(Total)", "output");
604         putchar('\n');
605         printf("%10s %5s %5s %10s %10s %5s %10s %5s",
606             "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
607             "colls");
608         if (dflag)
609                 printf(" %5.5s", "drops");
610         putchar('\n');
611         fflush(stdout);
612         line = 0;
613 loop:
614         if (interesting != NULL) {
615                 ip = interesting;
616                 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
617                         printf("???\n");
618                         exit(1);
619                 };
620                 if (!first) {
621                         show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
622                         show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
623                         show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
624                         show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
625                         show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
626                         show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
627                         show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
628                         show_stat("NRSlu", 5,
629                             ifnet.if_collisions - ip->ift_co, 1);
630                         if (dflag)
631                                 show_stat("LSu", 5,
632                                     ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
633                 }
634                 ip->ift_ip = ifnet.if_ipackets;
635                 ip->ift_ie = ifnet.if_ierrors;
636                 ip->ift_ib = ifnet.if_ibytes;
637                 ip->ift_op = ifnet.if_opackets;
638                 ip->ift_oe = ifnet.if_oerrors;
639                 ip->ift_ob = ifnet.if_obytes;
640                 ip->ift_co = ifnet.if_collisions;
641                 ip->ift_dr = ifnet.if_snd.ifq_drops;
642         } else {
643                 sum->ift_ip = 0;
644                 sum->ift_ie = 0;
645                 sum->ift_id = 0;
646                 sum->ift_ib = 0;
647                 sum->ift_op = 0;
648                 sum->ift_oe = 0;
649                 sum->ift_ob = 0;
650                 sum->ift_co = 0;
651                 sum->ift_dr = 0;
652                 for (off = firstifnet, ip = iftot;
653                      off && SLIST_NEXT(ip, chain) != NULL;
654                      ip = SLIST_NEXT(ip, chain)) {
655                         if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
656                                 off = 0;
657                                 continue;
658                         }
659                         sum->ift_ip += ifnet.if_ipackets;
660                         sum->ift_ie += ifnet.if_ierrors;
661                         sum->ift_id += ifnet.if_iqdrops;
662                         sum->ift_ib += ifnet.if_ibytes;
663                         sum->ift_op += ifnet.if_opackets;
664                         sum->ift_oe += ifnet.if_oerrors;
665                         sum->ift_ob += ifnet.if_obytes;
666                         sum->ift_co += ifnet.if_collisions;
667                         sum->ift_dr += ifnet.if_snd.ifq_drops;
668                         off = (u_long)TAILQ_NEXT(&ifnet, if_link);
669                 }
670                 if (!first) {
671                         show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
672                         show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
673                         show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
674                         show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
675                         show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
676                         show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
677                         show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
678                         show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
679                         if (dflag)
680                                 show_stat("LSu", 5,
681                                     sum->ift_dr - total->ift_dr, 1);
682                 }
683                 *total = *sum;
684         }
685         if (!first)
686                 putchar('\n');
687         fflush(stdout);
688         if ((noutputs != 0) && (--noutputs == 0))
689                 exit(0);
690         oldmask = sigblock(sigmask(SIGALRM));
691         while (!signalled)
692                 sigpause(0);
693         signalled = NO;
694         sigsetmask(oldmask);
695         line++;
696         first = 0;
697         if (line == 21)
698                 goto banner;
699         else
700                 goto loop;
701         /*NOTREACHED*/
702 }
703
704 /*
705  * Set a flag to indicate that a signal from the periodic itimer has been
706  * caught.
707  */
708 static void
709 catchalarm(int signo __unused)
710 {
711         signalled = YES;
712 }