1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Copyright (c) 1983, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #if 0
34 #ifndef lint
35 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
36 #endif /* not lint */
37 #endif
38
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/time.h>
45
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
53 #ifdef PF
54 #include <net/pfvar.h>
55 #include <net/if_pfsync.h>
56 #endif
57
58 #include <err.h>
59 #include <errno.h>
60 #include <ifaddrs.h>
61 #include <libutil.h>
62 #ifdef INET6
63 #include <netdb.h>
64 #endif
65 #include <signal.h>
66 #include <stdbool.h>
67 #include <stdint.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <sysexits.h>
72 #include <unistd.h>
73 #include <libxo/xo.h>
74
75 #include "netstat.h"
76
77 static void sidewaysintpr(void);
78
79 #ifdef PF
80 static const char* pfsyncacts[] = {
81 /* PFSYNC_ACT_CLR */ "clear all request",
82 /* PFSYNC_ACT_INS */ "state insert",
83 /* PFSYNC_ACT_INS_ACK */ "state inserted ack",
84 /* PFSYNC_ACT_UPD */ "state update",
85 /* PFSYNC_ACT_UPD_C */ "compressed state update",
86 /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request",
87 /* PFSYNC_ACT_DEL */ "state delete",
88 /* PFSYNC_ACT_DEL_C */ "compressed state delete",
89 /* PFSYNC_ACT_INS_F */ "fragment insert",
90 /* PFSYNC_ACT_DEL_F */ "fragment delete",
91 /* PFSYNC_ACT_BUS */ "bulk update mark",
92 /* PFSYNC_ACT_TDB */ "TDB replay counter update",
93 /* PFSYNC_ACT_EOF */ "end of frame mark",
94 };
95
96 static const char* pfsyncacts_name[] = {
97 /* PFSYNC_ACT_CLR */ "clear-all-request",
98 /* PFSYNC_ACT_INS */ "state-insert",
99 /* PFSYNC_ACT_INS_ACK */ "state-inserted-ack",
100 /* PFSYNC_ACT_UPD */ "state-update",
101 /* PFSYNC_ACT_UPD_C */ "compressed-state-update",
102 /* PFSYNC_ACT_UPD_REQ */ "uncompressed-state-request",
103 /* PFSYNC_ACT_DEL */ "state-delete",
104 /* PFSYNC_ACT_DEL_C */ "compressed-state-delete",
105 /* PFSYNC_ACT_INS_F */ "fragment-insert",
106 /* PFSYNC_ACT_DEL_F */ "fragment-delete",
107 /* PFSYNC_ACT_BUS */ "bulk-update-mark",
108 /* PFSYNC_ACT_TDB */ "TDB-replay-counter-update",
109 /* PFSYNC_ACT_EOF */ "end-of-frame-mark",
110 };
111
112 static void
pfsync_acts_stats(const char * list,const char * desc,uint64_t * a)113 pfsync_acts_stats(const char *list, const char *desc, uint64_t *a)
114 {
115 int i;
116
117 xo_open_list(list);
118 for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) {
119 if (*a || sflag <= 1) {
120 xo_open_instance(list);
121 xo_emit("\t\t{e:name}{:count/%ju} {N:/%s%s %s}\n",
122 pfsyncacts_name[i], (uintmax_t)(*a),
123 pfsyncacts[i], plural(*a), desc);
124 xo_close_instance(list);
125 }
126 }
127 xo_close_list(list);
128 }
129
130 /*
131 * Dump pfsync statistics structure.
132 */
133 void
pfsync_stats(u_long off,const char * name,int af1 __unused,int proto __unused)134 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
135 {
136 struct pfsyncstats pfsyncstat;
137
138 if (fetch_stats("net.pfsync.stats", off, &pfsyncstat,
139 sizeof(pfsyncstat), kread) != 0)
140 return;
141
142 xo_emit("{T:/%s}:\n", name);
143 xo_open_container(name);
144
145 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
146 xo_emit(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
147
148 p(pfsyncs_ipackets, "\t{:received-inet-packets/%ju} "
149 "{N:/packet%s received (IPv4)}\n");
150 p(pfsyncs_ipackets6, "\t{:received-inet6-packets/%ju} "
151 "{N:/packet%s received (IPv6)}\n");
152 pfsync_acts_stats("input-histogram", "received",
153 &pfsyncstat.pfsyncs_iacts[0]);
154 p(pfsyncs_badif, "\t\t/{:dropped-bad-interface/%ju} "
155 "{N:/packet%s discarded for bad interface}\n");
156 p(pfsyncs_badttl, "\t\t{:dropped-bad-ttl/%ju} "
157 "{N:/packet%s discarded for bad ttl}\n");
158 p(pfsyncs_hdrops, "\t\t{:dropped-short-header/%ju} "
159 "{N:/packet%s shorter than header}\n");
160 p(pfsyncs_badver, "\t\t{:dropped-bad-version/%ju} "
161 "{N:/packet%s discarded for bad version}\n");
162 p(pfsyncs_badauth, "\t\t{:dropped-bad-auth/%ju} "
163 "{N:/packet%s discarded for bad HMAC}\n");
164 p(pfsyncs_badact,"\t\t{:dropped-bad-action/%ju} "
165 "{N:/packet%s discarded for bad action}\n");
166 p(pfsyncs_badlen, "\t\t{:dropped-short/%ju} "
167 "{N:/packet%s discarded for short packet}\n");
168 p(pfsyncs_badval, "\t\t{:dropped-bad-values/%ju} "
169 "{N:/state%s discarded for bad values}\n");
170 p(pfsyncs_stale, "\t\t{:dropped-stale-state/%ju} "
171 "{N:/stale state%s}\n");
172 p(pfsyncs_badstate, "\t\t{:dropped-failed-lookup/%ju} "
173 "{N:/failed state lookup\\/insert%s}\n");
174 p(pfsyncs_opackets, "\t{:sent-inet-packets/%ju} "
175 "{N:/packet%s sent (IPv4})\n");
176 p(pfsyncs_opackets6, "\t{:send-inet6-packets/%ju} "
177 "{N:/packet%s sent (IPv6})\n");
178 pfsync_acts_stats("output-histogram", "sent",
179 &pfsyncstat.pfsyncs_oacts[0]);
180 p(pfsyncs_onomem, "\t\t{:discarded-no-memory/%ju} "
181 "{N:/failure%s due to mbuf memory error}\n");
182 p(pfsyncs_oerrors, "\t\t{:send-errors/%ju} "
183 "{N:/send error%s}\n");
184 #undef p
185 xo_close_container(name);
186 }
187 #endif /* PF */
188
189 /*
190 * Display a formatted value, or a '-' in the same space.
191 */
192 static void
show_stat(const char * fmt,int width,const char * name,u_long value,short showvalue,int div1000)193 show_stat(const char *fmt, int width, const char *name,
194 u_long value, short showvalue, int div1000)
195 {
196 const char *lsep, *rsep;
197 char newfmt[64];
198
199 lsep = "";
200 if (strncmp(fmt, "LS", 2) == 0) {
201 lsep = " ";
202 fmt += 2;
203 }
204 rsep = " ";
205 if (strncmp(fmt, "NRS", 3) == 0) {
206 rsep = "";
207 fmt += 3;
208 }
209 if (showvalue == 0) {
210 /* Print just dash. */
211 xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep);
212 return;
213 }
214
215 /*
216 * XXX: workaround {P:} modifier can't be empty and doesn't seem to
217 * take args... so we need to conditionally include it in the format.
218 */
219 #define maybe_pad(pad) do { \
220 if (strlen(pad)) { \
221 snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad); \
222 xo_emit(newfmt); \
223 } \
224 } while (0)
225
226 if (hflag) {
227 char buf[5];
228
229 /* Format in human readable form. */
230 humanize_number(buf, sizeof(buf), (int64_t)value, "",
231 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | \
232 ((div1000) ? HN_DIVISOR_1000 : 0));
233 maybe_pad(lsep);
234 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width);
235 xo_emit(newfmt, buf);
236 maybe_pad(rsep);
237 } else {
238 /* Construct the format string. */
239 maybe_pad(lsep);
240 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}",
241 name, width, fmt);
242 xo_emit(newfmt, value);
243 maybe_pad(rsep);
244 }
245 }
246
247 /*
248 * Find next multiaddr for a given interface name.
249 */
250 static struct ifmaddrs *
next_ifma(struct ifmaddrs * ifma,const char * name,const sa_family_t family)251 next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
252 {
253
254 for(; ifma != NULL; ifma = ifma->ifma_next) {
255 struct sockaddr_dl *sdl;
256
257 sdl = (struct sockaddr_dl *)ifma->ifma_name;
258 if (ifma->ifma_addr->sa_family == family &&
259 strcmp(sdl->sdl_data, name) == 0)
260 break;
261 }
262
263 return (ifma);
264 }
265
266 enum process_op { MEASURE, EMIT };
267
268 static void
process_ifa_addr(enum process_op op,struct ifaddrs * ifa,int * max_net_len,int * max_addr_len,bool * network,bool * link)269 process_ifa_addr(enum process_op op, struct ifaddrs *ifa, int *max_net_len,
270 int *max_addr_len, bool *network, bool *link)
271 {
272 int net_len, addr_len;
273 const char *nn, *rn;
274
275 if (op == EMIT) {
276 net_len = *max_net_len;
277 addr_len = *max_addr_len;
278 }
279
280 switch (ifa->ifa_addr->sa_family) {
281 case AF_UNSPEC:
282 if (op == MEASURE) {
283 net_len = strlen("none");
284 addr_len = strlen("none");
285 } else {
286 xo_emit("{:network/%-*.*s} ", net_len, net_len,
287 "none");
288 xo_emit("{:address/%-*.*s} ", addr_len, addr_len,
289 "none");
290 }
291 break;
292 case AF_INET:
293 #ifdef INET6
294 case AF_INET6:
295 #endif /* INET6 */
296 nn = netname(ifa->ifa_addr, ifa->ifa_netmask);
297 rn = routename(ifa->ifa_addr, numeric_addr);
298 if (op == MEASURE) {
299 net_len = strlen(nn);
300 addr_len = strlen(rn);
301 } else {
302 xo_emit("{t:network/%-*s} ", net_len, nn);
303 xo_emit("{t:address/%-*s} ", addr_len, rn);
304 }
305
306 if (network != NULL)
307 *network = true;
308 break;
309 case AF_LINK:
310 {
311 struct sockaddr_dl *sdl;
312 char linknum[sizeof("<Link#32767>")];
313
314 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
315 snprintf(linknum, sizeof(linknum), "<Link#%d>", sdl->sdl_index);
316 if (op == MEASURE) {
317 net_len = strlen(linknum);
318 if (sdl->sdl_nlen == 0 &&
319 sdl->sdl_alen == 0 &&
320 sdl->sdl_slen == 0)
321 addr_len = 1;
322 else
323 addr_len = strlen(routename(ifa->ifa_addr, 1));
324 } else {
325 xo_emit("{t:network/%-*.*s} ", net_len, net_len,
326 linknum);
327 if (sdl->sdl_nlen == 0 &&
328 sdl->sdl_alen == 0 &&
329 sdl->sdl_slen == 0)
330 xo_emit("{P:/%*s} ", addr_len, "");
331 else
332 xo_emit("{t:address/%-*.*s} ", addr_len,
333 addr_len, routename(ifa->ifa_addr, 1));
334 }
335 if (link != NULL)
336 *link = true;
337 break;
338 }
339 }
340
341 if (op == MEASURE) {
342 if (net_len > *max_net_len)
343 *max_net_len = net_len;
344 if (addr_len > *max_addr_len)
345 *max_addr_len = addr_len;
346 }
347 }
348
349 static int
max_num_len(int max_len,u_long num)350 max_num_len(int max_len, u_long num)
351 {
352 int len = 2; /* include space */
353
354 for (; num > 10; len++)
355 num /= 10;
356 return (MAX(max_len, len));
357 }
358
359 /*
360 * Print a description of the network interfaces.
361 */
362 void
intpr(void (* pfunc)(char *),int af)363 intpr(void (*pfunc)(char *), int af)
364 {
365 struct ifaddrs *ifap, *ifa;
366 struct ifmaddrs *ifmap, *ifma;
367 u_int ifn_len_max = 5, ifn_len;
368 u_int net_len = strlen("Network "), addr_len = strlen("Address ");
369 u_int npkt_len = 8, nbyte_len = 10, nerr_len = 5;
370
371 if (interval)
372 return sidewaysintpr();
373
374 if (getifaddrs(&ifap) != 0)
375 err(EX_OSERR, "getifaddrs");
376 if (aflag && getifmaddrs(&ifmap) != 0)
377 err(EX_OSERR, "getifmaddrs");
378
379 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
380 if (interface != NULL &&
381 strcmp(ifa->ifa_name, interface) != 0)
382 continue;
383 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
384 continue;
385 ifn_len = strlen(ifa->ifa_name);
386 if ((ifa->ifa_flags & IFF_UP) == 0)
387 ++ifn_len;
388 ifn_len_max = MAX(ifn_len_max, ifn_len);
389 process_ifa_addr(MEASURE, ifa, &net_len, &addr_len,
390 NULL, NULL);
391
392 #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
393 if (!hflag) {
394 npkt_len = max_num_len(npkt_len, IFA_STAT(ipackets));
395 npkt_len = max_num_len(npkt_len, IFA_STAT(opackets));
396 nerr_len = max_num_len(nerr_len, IFA_STAT(ierrors));
397 nerr_len = max_num_len(nerr_len, IFA_STAT(iqdrops));
398 nerr_len = max_num_len(nerr_len, IFA_STAT(collisions));
399 if (dflag)
400 nerr_len = max_num_len(nerr_len,
401 IFA_STAT(oqdrops));
402 if (bflag) {
403 nbyte_len = max_num_len(nbyte_len,
404 IFA_STAT(ibytes));
405 nbyte_len = max_num_len(nbyte_len,
406 IFA_STAT(obytes));
407 }
408 }
409 }
410
411 xo_open_list("interface");
412 if (!pfunc) {
413 xo_emit("{T:/%-*.*s}", ifn_len_max, ifn_len_max, "Name");
414 xo_emit(" {T:/%5.5s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
415 "{T:/%*.*s} {T:/%*.*s}",
416 "Mtu", net_len, net_len, "Network", addr_len, addr_len,
417 "Address", npkt_len, npkt_len, "Ipkts",
418 nerr_len, nerr_len, "Ierrs", nerr_len, nerr_len, "Idrop");
419 if (bflag)
420 xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Ibytes");
421 xo_emit(" {T:/%*.*s} {T:/%*.*s}", npkt_len, npkt_len, "Opkts",
422 nerr_len, nerr_len, "Oerrs");
423 if (bflag)
424 xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Obytes");
425 xo_emit(" {T:/%*s}", nerr_len, "Coll");
426 if (dflag)
427 xo_emit(" {T:/%*.*s}", nerr_len, nerr_len, "Drop");
428 xo_emit("\n");
429 }
430
431 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
432 bool network = false, link = false;
433 char *name, *xname, buf[IFNAMSIZ+1];
434
435 if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
436 continue;
437
438 name = ifa->ifa_name;
439
440 if (pfunc) {
441
442 (*pfunc)(name);
443
444 /*
445 * Skip all ifaddrs belonging to same interface.
446 */
447 while(ifa->ifa_next != NULL &&
448 (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
449 ifa = ifa->ifa_next;
450 }
451 continue;
452 }
453
454 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
455 continue;
456
457 xo_open_instance("interface");
458
459 if ((ifa->ifa_flags & IFF_UP) == 0) {
460 xname = stpcpy(buf, name);
461 *xname++ = '*';
462 *xname = '\0';
463 xname = buf;
464 } else
465 xname = name;
466
467 xo_emit("{d:/%-*.*s}{etk:name}{eq:flags/0x%x}",
468 ifn_len_max, ifn_len_max, xname, name, ifa->ifa_flags);
469
470 #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
471 show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa), 0);
472 #undef IFA_MTU
473
474 process_ifa_addr(EMIT, ifa, &net_len, &addr_len,
475 &network, &link);
476
477 show_stat("lu", npkt_len, "received-packets",
478 IFA_STAT(ipackets), link|network, 1);
479 show_stat("lu", nerr_len, "received-errors", IFA_STAT(ierrors),
480 link, 1);
481 show_stat("lu", nerr_len, "dropped-packets", IFA_STAT(iqdrops),
482 link, 1);
483 if (bflag)
484 show_stat("lu", nbyte_len, "received-bytes",
485 IFA_STAT(ibytes), link|network, 0);
486 show_stat("lu", npkt_len, "sent-packets", IFA_STAT(opackets),
487 link|network, 1);
488 show_stat("lu", nerr_len, "send-errors", IFA_STAT(oerrors),
489 link, 1);
490 if (bflag)
491 show_stat("lu", nbyte_len, "sent-bytes",
492 IFA_STAT(obytes), link|network, 0);
493 show_stat("NRSlu", nerr_len, "collisions", IFA_STAT(collisions),
494 link, 1);
495 if (dflag)
496 show_stat("LSlu", nerr_len, "dropped-packets",
497 IFA_STAT(oqdrops), link, 1);
498 xo_emit("\n");
499
500 if (!aflag) {
501 xo_close_instance("interface");
502 continue;
503 }
504
505 /*
506 * Print family's multicast addresses.
507 */
508 xo_open_list("multicast-address");
509 for (ifma = next_ifma(ifmap, ifa->ifa_name,
510 ifa->ifa_addr->sa_family);
511 ifma != NULL;
512 ifma = next_ifma(ifma, ifa->ifa_name,
513 ifa->ifa_addr->sa_family)) {
514 const char *fmt = NULL;
515
516 xo_open_instance("multicast-address");
517 switch (ifma->ifma_addr->sa_family) {
518 case AF_LINK:
519 {
520 struct sockaddr_dl *sdl;
521
522 sdl = (struct sockaddr_dl *)ifma->ifma_addr;
523 if (sdl->sdl_type != IFT_ETHER &&
524 sdl->sdl_type != IFT_FDDI)
525 break;
526 }
527 /* FALLTHROUGH */
528 case AF_INET:
529 #ifdef INET6
530 case AF_INET6:
531 #endif /* INET6 */
532 fmt = routename(ifma->ifma_addr, numeric_addr);
533 break;
534 }
535 if (fmt) {
536 if (Wflag)
537 xo_emit("{P:/%27s }"
538 "{t:address/%-17s/}", "", fmt);
539 else
540 xo_emit("{P:/%25s }"
541 "{t:address/%-17.17s/}", "", fmt);
542 if (ifma->ifma_addr->sa_family == AF_LINK) {
543 xo_emit(" {:received-packets/%8lu}",
544 IFA_STAT(imcasts));
545 xo_emit("{P:/%*s}", bflag? 17 : 6, "");
546 xo_emit(" {:sent-packets/%8lu}",
547 IFA_STAT(omcasts));
548 }
549 xo_emit("\n");
550 }
551 xo_close_instance("multicast-address");
552 ifma = ifma->ifma_next;
553 }
554 xo_close_list("multicast-address");
555 xo_close_instance("interface");
556 }
557 xo_close_list("interface");
558
559 freeifaddrs(ifap);
560 if (aflag)
561 freeifmaddrs(ifmap);
562 }
563
564 struct iftot {
565 u_long ift_ip; /* input packets */
566 u_long ift_ie; /* input errors */
567 u_long ift_id; /* input drops */
568 u_long ift_op; /* output packets */
569 u_long ift_oe; /* output errors */
570 u_long ift_od; /* output drops */
571 u_long ift_co; /* collisions */
572 u_long ift_ib; /* input bytes */
573 u_long ift_ob; /* output bytes */
574 };
575
576 /*
577 * Obtain stats for interface(s).
578 */
579 static void
fill_iftot(struct iftot * st)580 fill_iftot(struct iftot *st)
581 {
582 struct ifaddrs *ifap, *ifa;
583 bool found = false;
584
585 if (getifaddrs(&ifap) != 0)
586 xo_err(EX_OSERR, "getifaddrs");
587
588 bzero(st, sizeof(*st));
589
590 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
591 if (ifa->ifa_addr->sa_family != AF_LINK)
592 continue;
593 if (interface) {
594 if (strcmp(ifa->ifa_name, interface) == 0)
595 found = true;
596 else
597 continue;
598 }
599
600 st->ift_ip += IFA_STAT(ipackets);
601 st->ift_ie += IFA_STAT(ierrors);
602 st->ift_id += IFA_STAT(iqdrops);
603 st->ift_ib += IFA_STAT(ibytes);
604 st->ift_op += IFA_STAT(opackets);
605 st->ift_oe += IFA_STAT(oerrors);
606 st->ift_od += IFA_STAT(oqdrops);
607 st->ift_ob += IFA_STAT(obytes);
608 st->ift_co += IFA_STAT(collisions);
609 }
610
611 if (interface && found == false)
612 xo_err(EX_DATAERR, "interface %s not found", interface);
613
614 freeifaddrs(ifap);
615 }
616
617 /*
618 * Set a flag to indicate that a signal from the periodic itimer has been
619 * caught.
620 */
621 static sig_atomic_t signalled;
622 static void
catchalarm(int signo __unused)623 catchalarm(int signo __unused)
624 {
625 signalled = true;
626 }
627
628 /*
629 * Print a running summary of interface statistics.
630 * Repeat display every interval seconds, showing statistics
631 * collected over that interval. Assumes that interval is non-zero.
632 * First line printed at top of screen is always cumulative.
633 */
634 static void
sidewaysintpr(void)635 sidewaysintpr(void)
636 {
637 struct iftot ift[2], *new, *old;
638 struct itimerval interval_it;
639 int oldmask, line;
640
641 new = &ift[0];
642 old = &ift[1];
643 fill_iftot(old);
644
645 (void)signal(SIGALRM, catchalarm);
646 signalled = false;
647 interval_it.it_interval.tv_sec = interval;
648 interval_it.it_interval.tv_usec = 0;
649 interval_it.it_value = interval_it.it_interval;
650 setitimer(ITIMER_REAL, &interval_it, NULL);
651 xo_open_list("interface-statistics");
652
653 banner:
654 xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input",
655 interface != NULL ? interface : "(Total)", "output");
656 xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} "
657 "{T:/%10s} {T:/%5s}",
658 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
659 "colls");
660 if (dflag)
661 xo_emit(" {T:/%5.5s}", "drops");
662 xo_emit("\n");
663 xo_flush();
664 line = 0;
665
666 loop:
667 if ((noutputs != 0) && (--noutputs == 0)) {
668 xo_close_list("interface-statistics");
669 return;
670 }
671 oldmask = sigblock(sigmask(SIGALRM));
672 while (!signalled)
673 sigpause(0);
674 signalled = false;
675 sigsetmask(oldmask);
676 line++;
677
678 fill_iftot(new);
679
680 xo_open_instance("stats");
681 show_stat("lu", 10, "received-packets",
682 new->ift_ip - old->ift_ip, 1, 1);
683 show_stat("lu", 5, "received-errors",
684 new->ift_ie - old->ift_ie, 1, 1);
685 show_stat("lu", 5, "dropped-packets",
686 new->ift_id - old->ift_id, 1, 1);
687 show_stat("lu", 10, "received-bytes",
688 new->ift_ib - old->ift_ib, 1, 0);
689 show_stat("lu", 10, "sent-packets",
690 new->ift_op - old->ift_op, 1, 1);
691 show_stat("lu", 5, "send-errors",
692 new->ift_oe - old->ift_oe, 1, 1);
693 show_stat("lu", 10, "sent-bytes",
694 new->ift_ob - old->ift_ob, 1, 0);
695 show_stat("NRSlu", 5, "collisions",
696 new->ift_co - old->ift_co, 1, 1);
697 if (dflag)
698 show_stat("LSlu", 5, "dropped-packets",
699 new->ift_od - old->ift_od, 1, 1);
700 xo_close_instance("stats");
701 xo_emit("\n");
702 xo_flush();
703
704 if (new == &ift[0]) {
705 new = &ift[1];
706 old = &ift[0];
707 } else {
708 new = &ift[0];
709 old = &ift[1];
710 }
711
712 if (line == 21)
713 goto banner;
714 else
715 goto loop;
716
717 /* NOTREACHED */
718 }
719