165475bc8SDavid E. O'Brien /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4526b18aaSGleb Smirnoff * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
59b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993
69b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
79b50d902SRodney W. Grimes *
89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes * are met:
119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes * without specific prior written permission.
199b50d902SRodney W. Grimes *
209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes * SUCH DAMAGE.
319b50d902SRodney W. Grimes */
329b50d902SRodney W. Grimes
33f9b3502cSEnji Cooper #include <sys/param.h>
349b50d902SRodney W. Grimes #include <sys/protosw.h>
359b50d902SRodney W. Grimes #include <sys/socket.h>
36feda1a43SJohn Baldwin #include <sys/socketvar.h>
37628d2ac1SGarrett Wollman #include <sys/time.h>
389b50d902SRodney W. Grimes
399b50d902SRodney W. Grimes #include <net/if.h>
409b50d902SRodney W. Grimes #include <net/if_dl.h>
41f6719675SBill Fenner #include <net/if_types.h>
4275fb8770SGarrett Wollman #include <net/ethernet.h>
439b50d902SRodney W. Grimes #include <netinet/in.h>
449b50d902SRodney W. Grimes #include <netinet/in_var.h>
459b50d902SRodney W. Grimes #include <arpa/inet.h>
463e4d5cd3SGleb Smirnoff #ifdef PF
473e4d5cd3SGleb Smirnoff #include <net/pfvar.h>
485dea523bSKristof Provost #include <net/pflow.h>
493e4d5cd3SGleb Smirnoff #include <net/if_pfsync.h>
503e4d5cd3SGleb Smirnoff #endif
519b50d902SRodney W. Grimes
522e37c5a3SMax Laier #include <errno.h>
5384c1edcbSGleb Smirnoff #include <ifaddrs.h>
54c2dfd19fSGleb Smirnoff #include <libutil.h>
55cd05232aSHajimu UMEMOTO #ifdef INET6
56cd05232aSHajimu UMEMOTO #include <netdb.h>
57cd05232aSHajimu UMEMOTO #endif
589b50d902SRodney W. Grimes #include <signal.h>
5984c1edcbSGleb Smirnoff #include <stdbool.h>
607b95a1ebSYaroslav Tykhiy #include <stdint.h>
619b50d902SRodney W. Grimes #include <stdio.h>
62591c194aSGuido van Rooij #include <stdlib.h>
639b50d902SRodney W. Grimes #include <string.h>
6484c1edcbSGleb Smirnoff #include <sysexits.h>
65821df508SXin LI #include <unistd.h>
66ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
679b50d902SRodney W. Grimes
689b50d902SRodney W. Grimes #include "netstat.h"
699b50d902SRodney W. Grimes
7010d5269fSHiroki Sato static void sidewaysintpr(void);
71cfa1ca9dSYoshinobu Inoue
723e4d5cd3SGleb Smirnoff #ifdef PF
73d6d3f01eSGleb Smirnoff static const char* pfsyncacts[] = {
74d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_CLR */ "clear all request",
754bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_INS_1301 */ "13.1 state insert",
76d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_INS_ACK */ "state inserted ack",
774bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_UPD_1301 */ "13.1 state update",
78d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_UPD_C */ "compressed state update",
79d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request",
80d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL */ "state delete",
81d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL_C */ "compressed state delete",
82d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_INS_F */ "fragment insert",
83d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL_F */ "fragment delete",
84d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_BUS */ "bulk update mark",
85d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_TDB */ "TDB replay counter update",
86d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_EOF */ "end of frame mark",
874bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_INS_1400 */ "state insert",
884bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_UPD_1400 */ "state update",
89d6d3f01eSGleb Smirnoff };
90d6d3f01eSGleb Smirnoff
91ade9ccfeSMarcel Moolenaar static const char* pfsyncacts_name[] = {
92ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_CLR */ "clear-all-request",
934bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_INS_1301 */ "state-insert-1301",
94ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_INS_ACK */ "state-inserted-ack",
954bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_UPD_1301 */ "state-update-1301",
96ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_UPD_C */ "compressed-state-update",
97ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_UPD_REQ */ "uncompressed-state-request",
98ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_DEL */ "state-delete",
99ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_DEL_C */ "compressed-state-delete",
100ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_INS_F */ "fragment-insert",
101ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_DEL_F */ "fragment-delete",
102ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_BUS */ "bulk-update-mark",
103ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_TDB */ "TDB-replay-counter-update",
104ade9ccfeSMarcel Moolenaar /* PFSYNC_ACT_EOF */ "end-of-frame-mark",
1054bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_INS_1400 */ "state-insert",
1064bf98559SKajetan Staszkiewicz /* PFSYNC_ACT_UPD_1400 */ "state-update",
107ade9ccfeSMarcel Moolenaar };
108ade9ccfeSMarcel Moolenaar
109d6d3f01eSGleb Smirnoff static void
pfsync_acts_stats(const char * list,const char * desc,uint64_t * a)110ade9ccfeSMarcel Moolenaar pfsync_acts_stats(const char *list, const char *desc, uint64_t *a)
111d6d3f01eSGleb Smirnoff {
112d6d3f01eSGleb Smirnoff int i;
113d6d3f01eSGleb Smirnoff
114ade9ccfeSMarcel Moolenaar xo_open_list(list);
115ade9ccfeSMarcel Moolenaar for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) {
116ade9ccfeSMarcel Moolenaar if (*a || sflag <= 1) {
117ade9ccfeSMarcel Moolenaar xo_open_instance(list);
118ade9ccfeSMarcel Moolenaar xo_emit("\t\t{e:name}{:count/%ju} {N:/%s%s %s}\n",
119ade9ccfeSMarcel Moolenaar pfsyncacts_name[i], (uintmax_t)(*a),
120ade9ccfeSMarcel Moolenaar pfsyncacts[i], plural(*a), desc);
121ade9ccfeSMarcel Moolenaar xo_close_instance(list);
122ade9ccfeSMarcel Moolenaar }
123ade9ccfeSMarcel Moolenaar }
124ade9ccfeSMarcel Moolenaar xo_close_list(list);
125d6d3f01eSGleb Smirnoff }
126d6d3f01eSGleb Smirnoff
1272e37c5a3SMax Laier /*
1282e37c5a3SMax Laier * Dump pfsync statistics structure.
1292e37c5a3SMax Laier */
1302e37c5a3SMax Laier void
pfsync_stats(u_long off,const char * name,int af1 __unused,int proto __unused)131feda1a43SJohn Baldwin pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1322e37c5a3SMax Laier {
1339eddb899SMark Johnston struct pfsyncstats pfsyncstat;
134445f17bbSJosef Karthauser
1359eddb899SMark Johnston if (fetch_stats("net.pfsync.stats", off, &pfsyncstat,
1369eddb899SMark Johnston sizeof(pfsyncstat), kread) != 0)
1372e37c5a3SMax Laier return;
138445f17bbSJosef Karthauser
139ade9ccfeSMarcel Moolenaar xo_emit("{T:/%s}:\n", name);
140ade9ccfeSMarcel Moolenaar xo_open_container(name);
1412e37c5a3SMax Laier
1422e37c5a3SMax Laier #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
143ade9ccfeSMarcel Moolenaar xo_emit(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
1442e37c5a3SMax Laier
145ade9ccfeSMarcel Moolenaar p(pfsyncs_ipackets, "\t{:received-inet-packets/%ju} "
146ade9ccfeSMarcel Moolenaar "{N:/packet%s received (IPv4)}\n");
147ade9ccfeSMarcel Moolenaar p(pfsyncs_ipackets6, "\t{:received-inet6-packets/%ju} "
148ade9ccfeSMarcel Moolenaar "{N:/packet%s received (IPv6)}\n");
149ade9ccfeSMarcel Moolenaar pfsync_acts_stats("input-histogram", "received",
150d6d3f01eSGleb Smirnoff &pfsyncstat.pfsyncs_iacts[0]);
151cd9e5d43SKajetan Staszkiewicz p(pfsyncs_badif, "\t\t{:dropped-bad-interface/%ju} "
152ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for bad interface}\n");
153ade9ccfeSMarcel Moolenaar p(pfsyncs_badttl, "\t\t{:dropped-bad-ttl/%ju} "
154ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for bad ttl}\n");
155ade9ccfeSMarcel Moolenaar p(pfsyncs_hdrops, "\t\t{:dropped-short-header/%ju} "
156ade9ccfeSMarcel Moolenaar "{N:/packet%s shorter than header}\n");
157ade9ccfeSMarcel Moolenaar p(pfsyncs_badver, "\t\t{:dropped-bad-version/%ju} "
158ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for bad version}\n");
159ade9ccfeSMarcel Moolenaar p(pfsyncs_badauth, "\t\t{:dropped-bad-auth/%ju} "
160ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for bad HMAC}\n");
161ade9ccfeSMarcel Moolenaar p(pfsyncs_badact,"\t\t{:dropped-bad-action/%ju} "
162ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for bad action}\n");
163ade9ccfeSMarcel Moolenaar p(pfsyncs_badlen, "\t\t{:dropped-short/%ju} "
164ade9ccfeSMarcel Moolenaar "{N:/packet%s discarded for short packet}\n");
165ade9ccfeSMarcel Moolenaar p(pfsyncs_badval, "\t\t{:dropped-bad-values/%ju} "
166ade9ccfeSMarcel Moolenaar "{N:/state%s discarded for bad values}\n");
167ade9ccfeSMarcel Moolenaar p(pfsyncs_stale, "\t\t{:dropped-stale-state/%ju} "
168ade9ccfeSMarcel Moolenaar "{N:/stale state%s}\n");
169ade9ccfeSMarcel Moolenaar p(pfsyncs_badstate, "\t\t{:dropped-failed-lookup/%ju} "
170ade9ccfeSMarcel Moolenaar "{N:/failed state lookup\\/insert%s}\n");
171ade9ccfeSMarcel Moolenaar p(pfsyncs_opackets, "\t{:sent-inet-packets/%ju} "
172ade9ccfeSMarcel Moolenaar "{N:/packet%s sent (IPv4})\n");
173ade9ccfeSMarcel Moolenaar p(pfsyncs_opackets6, "\t{:send-inet6-packets/%ju} "
174ade9ccfeSMarcel Moolenaar "{N:/packet%s sent (IPv6})\n");
175ade9ccfeSMarcel Moolenaar pfsync_acts_stats("output-histogram", "sent",
176d6d3f01eSGleb Smirnoff &pfsyncstat.pfsyncs_oacts[0]);
177ade9ccfeSMarcel Moolenaar p(pfsyncs_onomem, "\t\t{:discarded-no-memory/%ju} "
178ade9ccfeSMarcel Moolenaar "{N:/failure%s due to mbuf memory error}\n");
179ade9ccfeSMarcel Moolenaar p(pfsyncs_oerrors, "\t\t{:send-errors/%ju} "
180ade9ccfeSMarcel Moolenaar "{N:/send error%s}\n");
1812e37c5a3SMax Laier #undef p
182ade9ccfeSMarcel Moolenaar xo_close_container(name);
1832e37c5a3SMax Laier }
1845dea523bSKristof Provost
1855dea523bSKristof Provost void
pflow_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1865dea523bSKristof Provost pflow_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1875dea523bSKristof Provost {
1885dea523bSKristof Provost struct pflowstats pflowstat;
1895dea523bSKristof Provost
1905dea523bSKristof Provost if (fetch_stats("net.pflow.stats", off, &pflowstat,
1915dea523bSKristof Provost sizeof(pflowstat), kread) != 0)
1925dea523bSKristof Provost return;
1935dea523bSKristof Provost
1945dea523bSKristof Provost xo_emit("{T:/%s}:\n", name);
1955dea523bSKristof Provost xo_open_container(name);
1965dea523bSKristof Provost
1975dea523bSKristof Provost #define p(f, m) if (pflowstat.f || sflag <= 1) \
1985dea523bSKristof Provost xo_emit(m, (uintmax_t)pflowstat.f, plural(pflowstat.f))
1995dea523bSKristof Provost
2005dea523bSKristof Provost p(pflow_flows, "\t{:flows/%ju} {N:/flow%s sent}\n");
2015dea523bSKristof Provost p(pflow_packets, "\t{:packets/%ju} {N:/packet%s sent}\n");
2025dea523bSKristof Provost p(pflow_onomem, "\t{:nomem/%ju} "
2035dea523bSKristof Provost "{N:/send failed due to mbuf memory error}\n");
2045dea523bSKristof Provost p(pflow_oerrors, "\t{:send-error/%ju} {N:/send error}\n");
2055dea523bSKristof Provost #undef p
2065dea523bSKristof Provost
2075dea523bSKristof Provost xo_close_container(name);
2085dea523bSKristof Provost }
2093e4d5cd3SGleb Smirnoff #endif /* PF */
210445f17bbSJosef Karthauser
211445f17bbSJosef Karthauser /*
212445f17bbSJosef Karthauser * Display a formatted value, or a '-' in the same space.
213445f17bbSJosef Karthauser */
2145e051718SAssar Westerlund static void
show_stat(const char * fmt,int width,const char * name,u_long value,short showvalue,int div1000)215ade9ccfeSMarcel Moolenaar show_stat(const char *fmt, int width, const char *name,
216a261e361SHiroki Sato u_long value, short showvalue, int div1000)
217445f17bbSJosef Karthauser {
2181f575ce8SBruce Evans const char *lsep, *rsep;
219ade9ccfeSMarcel Moolenaar char newfmt[64];
220445f17bbSJosef Karthauser
2211f575ce8SBruce Evans lsep = "";
2221f575ce8SBruce Evans if (strncmp(fmt, "LS", 2) == 0) {
2231f575ce8SBruce Evans lsep = " ";
2241f575ce8SBruce Evans fmt += 2;
2251f575ce8SBruce Evans }
2261f575ce8SBruce Evans rsep = " ";
2271f575ce8SBruce Evans if (strncmp(fmt, "NRS", 3) == 0) {
2281f575ce8SBruce Evans rsep = "";
2291f575ce8SBruce Evans fmt += 3;
2301f575ce8SBruce Evans }
231c2dfd19fSGleb Smirnoff if (showvalue == 0) {
232c2dfd19fSGleb Smirnoff /* Print just dash. */
233ade9ccfeSMarcel Moolenaar xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep);
234c2dfd19fSGleb Smirnoff return;
235445f17bbSJosef Karthauser }
236445f17bbSJosef Karthauser
237ade9ccfeSMarcel Moolenaar /*
238ade9ccfeSMarcel Moolenaar * XXX: workaround {P:} modifier can't be empty and doesn't seem to
239ade9ccfeSMarcel Moolenaar * take args... so we need to conditionally include it in the format.
240ade9ccfeSMarcel Moolenaar */
241ade9ccfeSMarcel Moolenaar #define maybe_pad(pad) do { \
242ade9ccfeSMarcel Moolenaar if (strlen(pad)) { \
243ade9ccfeSMarcel Moolenaar snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad); \
244ade9ccfeSMarcel Moolenaar xo_emit(newfmt); \
245ade9ccfeSMarcel Moolenaar } \
246ade9ccfeSMarcel Moolenaar } while (0)
247ade9ccfeSMarcel Moolenaar
248c2dfd19fSGleb Smirnoff if (hflag) {
249c2dfd19fSGleb Smirnoff char buf[5];
250445f17bbSJosef Karthauser
251c2dfd19fSGleb Smirnoff /* Format in human readable form. */
252c2dfd19fSGleb Smirnoff humanize_number(buf, sizeof(buf), (int64_t)value, "",
253a261e361SHiroki Sato HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | \
254a261e361SHiroki Sato ((div1000) ? HN_DIVISOR_1000 : 0));
255ade9ccfeSMarcel Moolenaar maybe_pad(lsep);
256ade9ccfeSMarcel Moolenaar snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width);
257ade9ccfeSMarcel Moolenaar xo_emit(newfmt, buf);
258ade9ccfeSMarcel Moolenaar maybe_pad(rsep);
259c2dfd19fSGleb Smirnoff } else {
260c2dfd19fSGleb Smirnoff /* Construct the format string. */
261ade9ccfeSMarcel Moolenaar maybe_pad(lsep);
262ade9ccfeSMarcel Moolenaar snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}",
263ade9ccfeSMarcel Moolenaar name, width, fmt);
264ade9ccfeSMarcel Moolenaar xo_emit(newfmt, value);
265ade9ccfeSMarcel Moolenaar maybe_pad(rsep);
266c2dfd19fSGleb Smirnoff }
267c2dfd19fSGleb Smirnoff }
268445f17bbSJosef Karthauser
2699b50d902SRodney W. Grimes /*
27084c1edcbSGleb Smirnoff * Find next multiaddr for a given interface name.
27184c1edcbSGleb Smirnoff */
27284c1edcbSGleb Smirnoff static struct ifmaddrs *
next_ifma(struct ifmaddrs * ifma,const char * name,const sa_family_t family)27384c1edcbSGleb Smirnoff next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
27484c1edcbSGleb Smirnoff {
27584c1edcbSGleb Smirnoff
27684c1edcbSGleb Smirnoff for(; ifma != NULL; ifma = ifma->ifma_next) {
27784c1edcbSGleb Smirnoff struct sockaddr_dl *sdl;
27884c1edcbSGleb Smirnoff
27984c1edcbSGleb Smirnoff sdl = (struct sockaddr_dl *)ifma->ifma_name;
28084c1edcbSGleb Smirnoff if (ifma->ifma_addr->sa_family == family &&
28184c1edcbSGleb Smirnoff strcmp(sdl->sdl_data, name) == 0)
28284c1edcbSGleb Smirnoff break;
28384c1edcbSGleb Smirnoff }
28484c1edcbSGleb Smirnoff
28584c1edcbSGleb Smirnoff return (ifma);
28684c1edcbSGleb Smirnoff }
28784c1edcbSGleb Smirnoff
288372e3d56SMike Karels enum process_op { MEASURE, EMIT };
289372e3d56SMike Karels
290372e3d56SMike Karels static void
process_ifa_addr(enum process_op op,struct ifaddrs * ifa,int * max_net_len,int * max_addr_len,bool * network,bool * link)291372e3d56SMike Karels process_ifa_addr(enum process_op op, struct ifaddrs *ifa, int *max_net_len,
292372e3d56SMike Karels int *max_addr_len, bool *network, bool *link)
293372e3d56SMike Karels {
294372e3d56SMike Karels int net_len, addr_len;
295372e3d56SMike Karels const char *nn, *rn;
296372e3d56SMike Karels
297372e3d56SMike Karels if (op == EMIT) {
298372e3d56SMike Karels net_len = *max_net_len;
299372e3d56SMike Karels addr_len = *max_addr_len;
300372e3d56SMike Karels }
301372e3d56SMike Karels
302372e3d56SMike Karels switch (ifa->ifa_addr->sa_family) {
303372e3d56SMike Karels case AF_UNSPEC:
304372e3d56SMike Karels if (op == MEASURE) {
305372e3d56SMike Karels net_len = strlen("none");
306372e3d56SMike Karels addr_len = strlen("none");
307372e3d56SMike Karels } else {
308372e3d56SMike Karels xo_emit("{:network/%-*.*s} ", net_len, net_len,
309372e3d56SMike Karels "none");
310372e3d56SMike Karels xo_emit("{:address/%-*.*s} ", addr_len, addr_len,
311372e3d56SMike Karels "none");
312372e3d56SMike Karels }
313372e3d56SMike Karels break;
314372e3d56SMike Karels case AF_INET:
315372e3d56SMike Karels #ifdef INET6
316372e3d56SMike Karels case AF_INET6:
317372e3d56SMike Karels #endif /* INET6 */
318372e3d56SMike Karels nn = netname(ifa->ifa_addr, ifa->ifa_netmask);
319372e3d56SMike Karels rn = routename(ifa->ifa_addr, numeric_addr);
320372e3d56SMike Karels if (op == MEASURE) {
321372e3d56SMike Karels net_len = strlen(nn);
322372e3d56SMike Karels addr_len = strlen(rn);
323372e3d56SMike Karels } else {
324372e3d56SMike Karels xo_emit("{t:network/%-*s} ", net_len, nn);
325372e3d56SMike Karels xo_emit("{t:address/%-*s} ", addr_len, rn);
326372e3d56SMike Karels }
327372e3d56SMike Karels
328372e3d56SMike Karels if (network != NULL)
329372e3d56SMike Karels *network = true;
330372e3d56SMike Karels break;
331372e3d56SMike Karels case AF_LINK:
332372e3d56SMike Karels {
333372e3d56SMike Karels struct sockaddr_dl *sdl;
334372e3d56SMike Karels char linknum[sizeof("<Link#32767>")];
335372e3d56SMike Karels
336372e3d56SMike Karels sdl = (struct sockaddr_dl *)ifa->ifa_addr;
337372e3d56SMike Karels snprintf(linknum, sizeof(linknum), "<Link#%d>", sdl->sdl_index);
338372e3d56SMike Karels if (op == MEASURE) {
339372e3d56SMike Karels net_len = strlen(linknum);
340372e3d56SMike Karels if (sdl->sdl_nlen == 0 &&
341372e3d56SMike Karels sdl->sdl_alen == 0 &&
342372e3d56SMike Karels sdl->sdl_slen == 0)
343372e3d56SMike Karels addr_len = 1;
344372e3d56SMike Karels else
345372e3d56SMike Karels addr_len = strlen(routename(ifa->ifa_addr, 1));
346372e3d56SMike Karels } else {
347372e3d56SMike Karels xo_emit("{t:network/%-*.*s} ", net_len, net_len,
348372e3d56SMike Karels linknum);
349372e3d56SMike Karels if (sdl->sdl_nlen == 0 &&
350372e3d56SMike Karels sdl->sdl_alen == 0 &&
351372e3d56SMike Karels sdl->sdl_slen == 0)
352372e3d56SMike Karels xo_emit("{P:/%*s} ", addr_len, "");
353372e3d56SMike Karels else
354372e3d56SMike Karels xo_emit("{t:address/%-*.*s} ", addr_len,
355372e3d56SMike Karels addr_len, routename(ifa->ifa_addr, 1));
356372e3d56SMike Karels }
357372e3d56SMike Karels if (link != NULL)
358372e3d56SMike Karels *link = true;
359372e3d56SMike Karels break;
360372e3d56SMike Karels }
361372e3d56SMike Karels }
362372e3d56SMike Karels
363372e3d56SMike Karels if (op == MEASURE) {
364372e3d56SMike Karels if (net_len > *max_net_len)
365372e3d56SMike Karels *max_net_len = net_len;
366372e3d56SMike Karels if (addr_len > *max_addr_len)
367372e3d56SMike Karels *max_addr_len = addr_len;
368372e3d56SMike Karels }
369372e3d56SMike Karels }
370372e3d56SMike Karels
371372e3d56SMike Karels static int
max_num_len(int max_len,u_long num)372372e3d56SMike Karels max_num_len(int max_len, u_long num)
373372e3d56SMike Karels {
374372e3d56SMike Karels int len = 2; /* include space */
375372e3d56SMike Karels
376372e3d56SMike Karels for (; num > 10; len++)
377372e3d56SMike Karels num /= 10;
378372e3d56SMike Karels return (MAX(max_len, len));
379372e3d56SMike Karels }
380372e3d56SMike Karels
38184c1edcbSGleb Smirnoff /*
3829b50d902SRodney W. Grimes * Print a description of the network interfaces.
3839b50d902SRodney W. Grimes */
3849b50d902SRodney W. Grimes void
intpr(void (* pfunc)(char *),int af)38510d5269fSHiroki Sato intpr(void (*pfunc)(char *), int af)
3869b50d902SRodney W. Grimes {
38784c1edcbSGleb Smirnoff struct ifaddrs *ifap, *ifa;
38884c1edcbSGleb Smirnoff struct ifmaddrs *ifmap, *ifma;
389bd4fcecaSHajimu UMEMOTO u_int ifn_len_max = 5, ifn_len;
390372e3d56SMike Karels u_int net_len = strlen("Network "), addr_len = strlen("Address ");
391372e3d56SMike Karels u_int npkt_len = 8, nbyte_len = 10, nerr_len = 5;
3929b50d902SRodney W. Grimes
39384c1edcbSGleb Smirnoff if (interval)
39410d5269fSHiroki Sato return sidewaysintpr();
39584c1edcbSGleb Smirnoff
39684c1edcbSGleb Smirnoff if (getifaddrs(&ifap) != 0)
397*95968ea7SYan-Hao Wang xo_err(EX_OSERR, "getifaddrs");
39884c1edcbSGleb Smirnoff if (aflag && getifmaddrs(&ifmap) != 0)
399*95968ea7SYan-Hao Wang xo_err(EX_OSERR, "getifmaddrs");
4000e27dc05SGarrett Wollman
401bd4fcecaSHajimu UMEMOTO for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
402bd4fcecaSHajimu UMEMOTO if (interface != NULL &&
403bd4fcecaSHajimu UMEMOTO strcmp(ifa->ifa_name, interface) != 0)
404bd4fcecaSHajimu UMEMOTO continue;
405bd4fcecaSHajimu UMEMOTO if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
406bd4fcecaSHajimu UMEMOTO continue;
407bd4fcecaSHajimu UMEMOTO ifn_len = strlen(ifa->ifa_name);
408bd4fcecaSHajimu UMEMOTO if ((ifa->ifa_flags & IFF_UP) == 0)
409bd4fcecaSHajimu UMEMOTO ++ifn_len;
410bd4fcecaSHajimu UMEMOTO ifn_len_max = MAX(ifn_len_max, ifn_len);
411372e3d56SMike Karels process_ifa_addr(MEASURE, ifa, &net_len, &addr_len,
412372e3d56SMike Karels NULL, NULL);
413372e3d56SMike Karels
414372e3d56SMike Karels #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
415372e3d56SMike Karels if (!hflag) {
416372e3d56SMike Karels npkt_len = max_num_len(npkt_len, IFA_STAT(ipackets));
417372e3d56SMike Karels npkt_len = max_num_len(npkt_len, IFA_STAT(opackets));
418372e3d56SMike Karels nerr_len = max_num_len(nerr_len, IFA_STAT(ierrors));
419372e3d56SMike Karels nerr_len = max_num_len(nerr_len, IFA_STAT(iqdrops));
420372e3d56SMike Karels nerr_len = max_num_len(nerr_len, IFA_STAT(collisions));
421372e3d56SMike Karels if (dflag)
422372e3d56SMike Karels nerr_len = max_num_len(nerr_len,
423372e3d56SMike Karels IFA_STAT(oqdrops));
424372e3d56SMike Karels if (bflag) {
425372e3d56SMike Karels nbyte_len = max_num_len(nbyte_len,
426372e3d56SMike Karels IFA_STAT(ibytes));
427372e3d56SMike Karels nbyte_len = max_num_len(nbyte_len,
428372e3d56SMike Karels IFA_STAT(obytes));
429bd4fcecaSHajimu UMEMOTO }
430372e3d56SMike Karels }
431bd4fcecaSHajimu UMEMOTO }
432bd4fcecaSHajimu UMEMOTO
433ade9ccfeSMarcel Moolenaar xo_open_list("interface");
434cf5e44f8SRuslan Ermilov if (!pfunc) {
43586a59895SHajimu UMEMOTO xo_emit("{T:/%-*.*s}", ifn_len_max, ifn_len_max, "Name");
436372e3d56SMike Karels xo_emit(" {T:/%5.5s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
437372e3d56SMike Karels "{T:/%*.*s} {T:/%*.*s}",
438cca052c6SHajimu UMEMOTO "Mtu", net_len, net_len, "Network", addr_len, addr_len,
439372e3d56SMike Karels "Address", npkt_len, npkt_len, "Ipkts",
440372e3d56SMike Karels nerr_len, nerr_len, "Ierrs", nerr_len, nerr_len, "Idrop");
441e1e293a5SDavid Greenman if (bflag)
442372e3d56SMike Karels xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Ibytes");
443372e3d56SMike Karels xo_emit(" {T:/%*.*s} {T:/%*.*s}", npkt_len, npkt_len, "Opkts",
444372e3d56SMike Karels nerr_len, nerr_len, "Oerrs");
445e1e293a5SDavid Greenman if (bflag)
446372e3d56SMike Karels xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Obytes");
447372e3d56SMike Karels xo_emit(" {T:/%*s}", nerr_len, "Coll");
4489b50d902SRodney W. Grimes if (dflag)
449372e3d56SMike Karels xo_emit(" {T:/%*.*s}", nerr_len, nerr_len, "Drop");
450ade9ccfeSMarcel Moolenaar xo_emit("\n");
451cfa1ca9dSYoshinobu Inoue }
4529b50d902SRodney W. Grimes
45384c1edcbSGleb Smirnoff for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
45484c1edcbSGleb Smirnoff bool network = false, link = false;
455e42e1fccSGleb Smirnoff char *name, *xname, buf[IFNAMSIZ+1];
456445f17bbSJosef Karthauser
45784c1edcbSGleb Smirnoff if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
4589b50d902SRodney W. Grimes continue;
459cfa1ca9dSYoshinobu Inoue
46084c1edcbSGleb Smirnoff name = ifa->ifa_name;
461ade9ccfeSMarcel Moolenaar
462ade9ccfeSMarcel Moolenaar if (pfunc) {
463ade9ccfeSMarcel Moolenaar
46484c1edcbSGleb Smirnoff (*pfunc)(name);
4655da9f8faSJosef Karthauser
4665da9f8faSJosef Karthauser /*
46784c1edcbSGleb Smirnoff * Skip all ifaddrs belonging to same interface.
4685da9f8faSJosef Karthauser */
46984c1edcbSGleb Smirnoff while(ifa->ifa_next != NULL &&
47084c1edcbSGleb Smirnoff (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
47184c1edcbSGleb Smirnoff ifa = ifa->ifa_next;
47284c1edcbSGleb Smirnoff }
47384c1edcbSGleb Smirnoff continue;
47484c1edcbSGleb Smirnoff }
4755da9f8faSJosef Karthauser
47684c1edcbSGleb Smirnoff if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
4779b50d902SRodney W. Grimes continue;
47884c1edcbSGleb Smirnoff
479ade9ccfeSMarcel Moolenaar xo_open_instance("interface");
480ade9ccfeSMarcel Moolenaar
481e42e1fccSGleb Smirnoff if ((ifa->ifa_flags & IFF_UP) == 0) {
482e42e1fccSGleb Smirnoff xname = stpcpy(buf, name);
483e42e1fccSGleb Smirnoff *xname++ = '*';
484e42e1fccSGleb Smirnoff *xname = '\0';
485e42e1fccSGleb Smirnoff xname = buf;
486e42e1fccSGleb Smirnoff } else
487e42e1fccSGleb Smirnoff xname = name;
488e42e1fccSGleb Smirnoff
489b1a302a5SHajimu UMEMOTO xo_emit("{d:/%-*.*s}{etk:name}{eq:flags/0x%x}",
490b1a302a5SHajimu UMEMOTO ifn_len_max, ifn_len_max, xname, name, ifa->ifa_flags);
49184c1edcbSGleb Smirnoff
49284c1edcbSGleb Smirnoff #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
493a261e361SHiroki Sato show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa), 0);
49484c1edcbSGleb Smirnoff #undef IFA_MTU
49584c1edcbSGleb Smirnoff
496372e3d56SMike Karels process_ifa_addr(EMIT, ifa, &net_len, &addr_len,
497372e3d56SMike Karels &network, &link);
49810d5269fSHiroki Sato
499372e3d56SMike Karels show_stat("lu", npkt_len, "received-packets",
500372e3d56SMike Karels IFA_STAT(ipackets), link|network, 1);
501372e3d56SMike Karels show_stat("lu", nerr_len, "received-errors", IFA_STAT(ierrors),
502a261e361SHiroki Sato link, 1);
503372e3d56SMike Karels show_stat("lu", nerr_len, "dropped-packets", IFA_STAT(iqdrops),
504a261e361SHiroki Sato link, 1);
5057c23a867SGleb Smirnoff if (bflag)
506372e3d56SMike Karels show_stat("lu", nbyte_len, "received-bytes",
507372e3d56SMike Karels IFA_STAT(ibytes), link|network, 0);
508372e3d56SMike Karels show_stat("lu", npkt_len, "sent-packets", IFA_STAT(opackets),
509a261e361SHiroki Sato link|network, 1);
510372e3d56SMike Karels show_stat("lu", nerr_len, "send-errors", IFA_STAT(oerrors),
511372e3d56SMike Karels link, 1);
5127c23a867SGleb Smirnoff if (bflag)
513372e3d56SMike Karels show_stat("lu", nbyte_len, "sent-bytes",
514372e3d56SMike Karels IFA_STAT(obytes), link|network, 0);
515372e3d56SMike Karels show_stat("NRSlu", nerr_len, "collisions", IFA_STAT(collisions),
516a261e361SHiroki Sato link, 1);
51755fb7d68SGleb Smirnoff if (dflag)
518d00827a9SWarner Losh show_stat("LSlu", nerr_len, "dropped-packets",
519a261e361SHiroki Sato IFA_STAT(oqdrops), link, 1);
520ade9ccfeSMarcel Moolenaar xo_emit("\n");
5211f575ce8SBruce Evans
522ade9ccfeSMarcel Moolenaar if (!aflag) {
523ade9ccfeSMarcel Moolenaar xo_close_instance("interface");
52475fb8770SGarrett Wollman continue;
525ade9ccfeSMarcel Moolenaar }
52675fb8770SGarrett Wollman
52784c1edcbSGleb Smirnoff /*
52884c1edcbSGleb Smirnoff * Print family's multicast addresses.
52984c1edcbSGleb Smirnoff */
530ade9ccfeSMarcel Moolenaar xo_open_list("multicast-address");
53184c1edcbSGleb Smirnoff for (ifma = next_ifma(ifmap, ifa->ifa_name,
53284c1edcbSGleb Smirnoff ifa->ifa_addr->sa_family);
53384c1edcbSGleb Smirnoff ifma != NULL;
53484c1edcbSGleb Smirnoff ifma = next_ifma(ifma, ifa->ifa_name,
53584c1edcbSGleb Smirnoff ifa->ifa_addr->sa_family)) {
53684c1edcbSGleb Smirnoff const char *fmt = NULL;
53784c1edcbSGleb Smirnoff
538ade9ccfeSMarcel Moolenaar xo_open_instance("multicast-address");
53984c1edcbSGleb Smirnoff switch (ifma->ifma_addr->sa_family) {
540f6719675SBill Fenner case AF_LINK:
54184c1edcbSGleb Smirnoff {
54284c1edcbSGleb Smirnoff struct sockaddr_dl *sdl;
54384c1edcbSGleb Smirnoff
54484c1edcbSGleb Smirnoff sdl = (struct sockaddr_dl *)ifma->ifma_addr;
54510d5269fSHiroki Sato if (sdl->sdl_type != IFT_ETHER &&
54610d5269fSHiroki Sato sdl->sdl_type != IFT_FDDI)
54775fb8770SGarrett Wollman break;
548f6719675SBill Fenner }
54910d5269fSHiroki Sato /* FALLTHROUGH */
55010d5269fSHiroki Sato case AF_INET:
55110d5269fSHiroki Sato #ifdef INET6
55210d5269fSHiroki Sato case AF_INET6:
55310d5269fSHiroki Sato #endif /* INET6 */
55410d5269fSHiroki Sato fmt = routename(ifma->ifma_addr, numeric_addr);
555f6719675SBill Fenner break;
556f6719675SBill Fenner }
55784c1edcbSGleb Smirnoff if (fmt) {
55810d5269fSHiroki Sato if (Wflag)
55910d5269fSHiroki Sato xo_emit("{P:/%27s }"
56010d5269fSHiroki Sato "{t:address/%-17s/}", "", fmt);
56110d5269fSHiroki Sato else
56210d5269fSHiroki Sato xo_emit("{P:/%25s }"
56310d5269fSHiroki Sato "{t:address/%-17.17s/}", "", fmt);
56484c1edcbSGleb Smirnoff if (ifma->ifma_addr->sa_family == AF_LINK) {
565ade9ccfeSMarcel Moolenaar xo_emit(" {:received-packets/%8lu}",
566ade9ccfeSMarcel Moolenaar IFA_STAT(imcasts));
567ade9ccfeSMarcel Moolenaar xo_emit("{P:/%*s}", bflag? 17 : 6, "");
568ade9ccfeSMarcel Moolenaar xo_emit(" {:sent-packets/%8lu}",
569ade9ccfeSMarcel Moolenaar IFA_STAT(omcasts));
57084c1edcbSGleb Smirnoff }
571ade9ccfeSMarcel Moolenaar xo_emit("\n");
57284c1edcbSGleb Smirnoff }
573ade9ccfeSMarcel Moolenaar xo_close_instance("multicast-address");
57484c1edcbSGleb Smirnoff ifma = ifma->ifma_next;
57584c1edcbSGleb Smirnoff }
576ade9ccfeSMarcel Moolenaar xo_close_list("multicast-address");
577ade9ccfeSMarcel Moolenaar xo_close_instance("interface");
57884c1edcbSGleb Smirnoff }
579ade9ccfeSMarcel Moolenaar xo_close_list("interface");
58084c1edcbSGleb Smirnoff
58184c1edcbSGleb Smirnoff freeifaddrs(ifap);
58284c1edcbSGleb Smirnoff if (aflag)
58384c1edcbSGleb Smirnoff freeifmaddrs(ifmap);
58484c1edcbSGleb Smirnoff }
58584c1edcbSGleb Smirnoff
5869b50d902SRodney W. Grimes struct iftot {
587890bc2a5SDoug Rabson u_long ift_ip; /* input packets */
588890bc2a5SDoug Rabson u_long ift_ie; /* input errors */
589d72dc9a7SAttilio Rao u_long ift_id; /* input drops */
590890bc2a5SDoug Rabson u_long ift_op; /* output packets */
591890bc2a5SDoug Rabson u_long ift_oe; /* output errors */
59255fb7d68SGleb Smirnoff u_long ift_od; /* output drops */
593890bc2a5SDoug Rabson u_long ift_co; /* collisions */
594890bc2a5SDoug Rabson u_long ift_ib; /* input bytes */
595890bc2a5SDoug Rabson u_long ift_ob; /* output bytes */
596591c194aSGuido van Rooij };
5979b50d902SRodney W. Grimes
59884c1edcbSGleb Smirnoff /*
59984c1edcbSGleb Smirnoff * Obtain stats for interface(s).
60084c1edcbSGleb Smirnoff */
60184c1edcbSGleb Smirnoff static void
fill_iftot(struct iftot * st)60284c1edcbSGleb Smirnoff fill_iftot(struct iftot *st)
60384c1edcbSGleb Smirnoff {
60484c1edcbSGleb Smirnoff struct ifaddrs *ifap, *ifa;
60584c1edcbSGleb Smirnoff bool found = false;
60684c1edcbSGleb Smirnoff
60784c1edcbSGleb Smirnoff if (getifaddrs(&ifap) != 0)
608ade9ccfeSMarcel Moolenaar xo_err(EX_OSERR, "getifaddrs");
60984c1edcbSGleb Smirnoff
61084c1edcbSGleb Smirnoff bzero(st, sizeof(*st));
61184c1edcbSGleb Smirnoff
61284c1edcbSGleb Smirnoff for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
61384c1edcbSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_LINK)
61484c1edcbSGleb Smirnoff continue;
61584c1edcbSGleb Smirnoff if (interface) {
61684c1edcbSGleb Smirnoff if (strcmp(ifa->ifa_name, interface) == 0)
61784c1edcbSGleb Smirnoff found = true;
61884c1edcbSGleb Smirnoff else
61984c1edcbSGleb Smirnoff continue;
62084c1edcbSGleb Smirnoff }
62184c1edcbSGleb Smirnoff
62284c1edcbSGleb Smirnoff st->ift_ip += IFA_STAT(ipackets);
62384c1edcbSGleb Smirnoff st->ift_ie += IFA_STAT(ierrors);
62484c1edcbSGleb Smirnoff st->ift_id += IFA_STAT(iqdrops);
62584c1edcbSGleb Smirnoff st->ift_ib += IFA_STAT(ibytes);
62684c1edcbSGleb Smirnoff st->ift_op += IFA_STAT(opackets);
62784c1edcbSGleb Smirnoff st->ift_oe += IFA_STAT(oerrors);
62855fb7d68SGleb Smirnoff st->ift_od += IFA_STAT(oqdrops);
62984c1edcbSGleb Smirnoff st->ift_ob += IFA_STAT(obytes);
63084c1edcbSGleb Smirnoff st->ift_co += IFA_STAT(collisions);
63184c1edcbSGleb Smirnoff }
63284c1edcbSGleb Smirnoff
63384c1edcbSGleb Smirnoff if (interface && found == false)
634ade9ccfeSMarcel Moolenaar xo_err(EX_DATAERR, "interface %s not found", interface);
63584c1edcbSGleb Smirnoff
63684c1edcbSGleb Smirnoff freeifaddrs(ifap);
63784c1edcbSGleb Smirnoff }
63884c1edcbSGleb Smirnoff
63984c1edcbSGleb Smirnoff /*
64084c1edcbSGleb Smirnoff * Set a flag to indicate that a signal from the periodic itimer has been
64184c1edcbSGleb Smirnoff * caught.
64284c1edcbSGleb Smirnoff */
64384c1edcbSGleb Smirnoff static sig_atomic_t signalled;
64484c1edcbSGleb Smirnoff static void
catchalarm(int signo __unused)64584c1edcbSGleb Smirnoff catchalarm(int signo __unused)
64684c1edcbSGleb Smirnoff {
64784c1edcbSGleb Smirnoff signalled = true;
64884c1edcbSGleb Smirnoff }
6499b50d902SRodney W. Grimes
6509b50d902SRodney W. Grimes /*
6519b50d902SRodney W. Grimes * Print a running summary of interface statistics.
65284c1edcbSGleb Smirnoff * Repeat display every interval seconds, showing statistics
65384c1edcbSGleb Smirnoff * collected over that interval. Assumes that interval is non-zero.
6549b50d902SRodney W. Grimes * First line printed at top of screen is always cumulative.
6559b50d902SRodney W. Grimes */
6569b50d902SRodney W. Grimes static void
sidewaysintpr(void)65710d5269fSHiroki Sato sidewaysintpr(void)
6589b50d902SRodney W. Grimes {
65984c1edcbSGleb Smirnoff struct iftot ift[2], *new, *old;
66093547b07SBruce Evans struct itimerval interval_it;
66184c1edcbSGleb Smirnoff int oldmask, line;
6629b50d902SRodney W. Grimes
66384c1edcbSGleb Smirnoff new = &ift[0];
66484c1edcbSGleb Smirnoff old = &ift[1];
66584c1edcbSGleb Smirnoff fill_iftot(old);
666591c194aSGuido van Rooij
6679b50d902SRodney W. Grimes (void)signal(SIGALRM, catchalarm);
66884c1edcbSGleb Smirnoff signalled = false;
66984c1edcbSGleb Smirnoff interval_it.it_interval.tv_sec = interval;
67093547b07SBruce Evans interval_it.it_interval.tv_usec = 0;
67193547b07SBruce Evans interval_it.it_value = interval_it.it_interval;
67293547b07SBruce Evans setitimer(ITIMER_REAL, &interval_it, NULL);
673ade9ccfeSMarcel Moolenaar xo_open_list("interface-statistics");
67484c1edcbSGleb Smirnoff
6750b87c915SDavid Greenman banner:
676ade9ccfeSMarcel Moolenaar xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input",
67784c1edcbSGleb Smirnoff interface != NULL ? interface : "(Total)", "output");
678ade9ccfeSMarcel Moolenaar xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} "
679ade9ccfeSMarcel Moolenaar "{T:/%10s} {T:/%5s}",
680d72dc9a7SAttilio Rao "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
681d72dc9a7SAttilio Rao "colls");
6829b50d902SRodney W. Grimes if (dflag)
683ade9ccfeSMarcel Moolenaar xo_emit(" {T:/%5.5s}", "drops");
684ade9ccfeSMarcel Moolenaar xo_emit("\n");
685ade9ccfeSMarcel Moolenaar xo_flush();
6869b50d902SRodney W. Grimes line = 0;
68784c1edcbSGleb Smirnoff
6889b50d902SRodney W. Grimes loop:
689ade9ccfeSMarcel Moolenaar if ((noutputs != 0) && (--noutputs == 0)) {
690ade9ccfeSMarcel Moolenaar xo_close_list("interface-statistics");
691ade9ccfeSMarcel Moolenaar return;
692ade9ccfeSMarcel Moolenaar }
6939b50d902SRodney W. Grimes oldmask = sigblock(sigmask(SIGALRM));
69493547b07SBruce Evans while (!signalled)
6959b50d902SRodney W. Grimes sigpause(0);
69684c1edcbSGleb Smirnoff signalled = false;
69793547b07SBruce Evans sigsetmask(oldmask);
6980b87c915SDavid Greenman line++;
69984c1edcbSGleb Smirnoff
70084c1edcbSGleb Smirnoff fill_iftot(new);
70184c1edcbSGleb Smirnoff
702ade9ccfeSMarcel Moolenaar xo_open_instance("stats");
703ade9ccfeSMarcel Moolenaar show_stat("lu", 10, "received-packets",
704a261e361SHiroki Sato new->ift_ip - old->ift_ip, 1, 1);
705ade9ccfeSMarcel Moolenaar show_stat("lu", 5, "received-errors",
706a261e361SHiroki Sato new->ift_ie - old->ift_ie, 1, 1);
707ade9ccfeSMarcel Moolenaar show_stat("lu", 5, "dropped-packets",
708a261e361SHiroki Sato new->ift_id - old->ift_id, 1, 1);
709ade9ccfeSMarcel Moolenaar show_stat("lu", 10, "received-bytes",
710a261e361SHiroki Sato new->ift_ib - old->ift_ib, 1, 0);
711ade9ccfeSMarcel Moolenaar show_stat("lu", 10, "sent-packets",
712a261e361SHiroki Sato new->ift_op - old->ift_op, 1, 1);
713ade9ccfeSMarcel Moolenaar show_stat("lu", 5, "send-errors",
714a261e361SHiroki Sato new->ift_oe - old->ift_oe, 1, 1);
715ade9ccfeSMarcel Moolenaar show_stat("lu", 10, "sent-bytes",
716a261e361SHiroki Sato new->ift_ob - old->ift_ob, 1, 0);
717ade9ccfeSMarcel Moolenaar show_stat("NRSlu", 5, "collisions",
718a261e361SHiroki Sato new->ift_co - old->ift_co, 1, 1);
71955fb7d68SGleb Smirnoff if (dflag)
720d00827a9SWarner Losh show_stat("LSlu", 5, "dropped-packets",
721a261e361SHiroki Sato new->ift_od - old->ift_od, 1, 1);
722ade9ccfeSMarcel Moolenaar xo_close_instance("stats");
723ade9ccfeSMarcel Moolenaar xo_emit("\n");
724ade9ccfeSMarcel Moolenaar xo_flush();
72584c1edcbSGleb Smirnoff
72684c1edcbSGleb Smirnoff if (new == &ift[0]) {
72784c1edcbSGleb Smirnoff new = &ift[1];
72884c1edcbSGleb Smirnoff old = &ift[0];
72984c1edcbSGleb Smirnoff } else {
73084c1edcbSGleb Smirnoff new = &ift[0];
73184c1edcbSGleb Smirnoff old = &ift[1];
73284c1edcbSGleb Smirnoff }
73384c1edcbSGleb Smirnoff
7349b50d902SRodney W. Grimes if (line == 21)
7359b50d902SRodney W. Grimes goto banner;
7360b87c915SDavid Greenman else
7379b50d902SRodney W. Grimes goto loop;
7389b50d902SRodney W. Grimes
73984c1edcbSGleb Smirnoff /* NOTREACHED */
7409b50d902SRodney W. Grimes }
741