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