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