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