2 * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 1983, 1988, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <arpa/inet.h>
56 #include <net/pfvar.h>
57 #include <net/if_pfsync.h>
79 static void sidewaysintpr(int);
82 static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */
86 static const char* pfsyncacts[] = {
87 /* PFSYNC_ACT_CLR */ "clear all request",
88 /* PFSYNC_ACT_INS */ "state insert",
89 /* PFSYNC_ACT_INS_ACK */ "state inserted ack",
90 /* PFSYNC_ACT_UPD */ "state update",
91 /* PFSYNC_ACT_UPD_C */ "compressed state update",
92 /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request",
93 /* PFSYNC_ACT_DEL */ "state delete",
94 /* PFSYNC_ACT_DEL_C */ "compressed state delete",
95 /* PFSYNC_ACT_INS_F */ "fragment insert",
96 /* PFSYNC_ACT_DEL_F */ "fragment delete",
97 /* PFSYNC_ACT_BUS */ "bulk update mark",
98 /* PFSYNC_ACT_TDB */ "TDB replay counter update",
99 /* PFSYNC_ACT_EOF */ "end of frame mark",
102 static const char* pfsyncacts_name[] = {
103 /* PFSYNC_ACT_CLR */ "clear-all-request",
104 /* PFSYNC_ACT_INS */ "state-insert",
105 /* PFSYNC_ACT_INS_ACK */ "state-inserted-ack",
106 /* PFSYNC_ACT_UPD */ "state-update",
107 /* PFSYNC_ACT_UPD_C */ "compressed-state-update",
108 /* PFSYNC_ACT_UPD_REQ */ "uncompressed-state-request",
109 /* PFSYNC_ACT_DEL */ "state-delete",
110 /* PFSYNC_ACT_DEL_C */ "compressed-state-delete",
111 /* PFSYNC_ACT_INS_F */ "fragment-insert",
112 /* PFSYNC_ACT_DEL_F */ "fragment-delete",
113 /* PFSYNC_ACT_BUS */ "bulk-update-mark",
114 /* PFSYNC_ACT_TDB */ "TDB-replay-counter-update",
115 /* PFSYNC_ACT_EOF */ "end-of-frame-mark",
119 pfsync_acts_stats(const char *list, const char *desc, uint64_t *a)
124 for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) {
125 if (*a || sflag <= 1) {
126 xo_open_instance(list);
127 xo_emit("\t\t{e:name}{:count/%ju} {N:/%s%s %s}\n",
128 pfsyncacts_name[i], (uintmax_t)(*a),
129 pfsyncacts[i], plural(*a), desc);
130 xo_close_instance(list);
137 * Dump pfsync statistics structure.
140 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
142 struct pfsyncstats pfsyncstat, zerostat;
143 size_t len = sizeof(struct pfsyncstats);
147 memset(&zerostat, 0, len);
148 if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len,
149 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
151 warn("sysctl: net.pfsync.stats");
155 kread(off, &pfsyncstat, len);
157 xo_emit("{T:/%s}:\n", name);
158 xo_open_container(name);
160 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
161 xo_emit(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
163 p(pfsyncs_ipackets, "\t{:received-inet-packets/%ju} "
164 "{N:/packet%s received (IPv4)}\n");
165 p(pfsyncs_ipackets6, "\t{:received-inet6-packets/%ju} "
166 "{N:/packet%s received (IPv6)}\n");
167 pfsync_acts_stats("input-histogram", "received",
168 &pfsyncstat.pfsyncs_iacts[0]);
169 p(pfsyncs_badif, "\t\t/{:dropped-bad-interface/%ju} "
170 "{N:/packet%s discarded for bad interface}\n");
171 p(pfsyncs_badttl, "\t\t{:dropped-bad-ttl/%ju} "
172 "{N:/packet%s discarded for bad ttl}\n");
173 p(pfsyncs_hdrops, "\t\t{:dropped-short-header/%ju} "
174 "{N:/packet%s shorter than header}\n");
175 p(pfsyncs_badver, "\t\t{:dropped-bad-version/%ju} "
176 "{N:/packet%s discarded for bad version}\n");
177 p(pfsyncs_badauth, "\t\t{:dropped-bad-auth/%ju} "
178 "{N:/packet%s discarded for bad HMAC}\n");
179 p(pfsyncs_badact,"\t\t{:dropped-bad-action/%ju} "
180 "{N:/packet%s discarded for bad action}\n");
181 p(pfsyncs_badlen, "\t\t{:dropped-short/%ju} "
182 "{N:/packet%s discarded for short packet}\n");
183 p(pfsyncs_badval, "\t\t{:dropped-bad-values/%ju} "
184 "{N:/state%s discarded for bad values}\n");
185 p(pfsyncs_stale, "\t\t{:dropped-stale-state/%ju} "
186 "{N:/stale state%s}\n");
187 p(pfsyncs_badstate, "\t\t{:dropped-failed-lookup/%ju} "
188 "{N:/failed state lookup\\/insert%s}\n");
189 p(pfsyncs_opackets, "\t{:sent-inet-packets/%ju} "
190 "{N:/packet%s sent (IPv4})\n");
191 p(pfsyncs_opackets6, "\t{:send-inet6-packets/%ju} "
192 "{N:/packet%s sent (IPv6})\n");
193 pfsync_acts_stats("output-histogram", "sent",
194 &pfsyncstat.pfsyncs_oacts[0]);
195 p(pfsyncs_onomem, "\t\t{:discarded-no-memory/%ju} "
196 "{N:/failure%s due to mbuf memory error}\n");
197 p(pfsyncs_oerrors, "\t\t{:send-errors/%ju} "
198 "{N:/send error%s}\n");
200 xo_close_container(name);
205 * Display a formatted value, or a '-' in the same space.
208 show_stat(const char *fmt, int width, const char *name,
209 u_long value, short showvalue)
211 const char *lsep, *rsep;
215 if (strncmp(fmt, "LS", 2) == 0) {
220 if (strncmp(fmt, "NRS", 3) == 0) {
224 if (showvalue == 0) {
225 /* Print just dash. */
226 xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep);
231 * XXX: workaround {P:} modifier can't be empty and doesn't seem to
232 * take args... so we need to conditionally include it in the format.
234 #define maybe_pad(pad) do { \
236 snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad); \
244 /* Format in human readable form. */
245 humanize_number(buf, sizeof(buf), (int64_t)value, "",
246 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
248 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width);
249 xo_emit(newfmt, buf);
252 /* Construct the format string. */
254 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}",
256 xo_emit(newfmt, value);
262 * Find next multiaddr for a given interface name.
264 static struct ifmaddrs *
265 next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
268 for(; ifma != NULL; ifma = ifma->ifma_next) {
269 struct sockaddr_dl *sdl;
271 sdl = (struct sockaddr_dl *)ifma->ifma_name;
272 if (ifma->ifma_addr->sa_family == family &&
273 strcmp(sdl->sdl_data, name) == 0)
281 * Print a description of the network interfaces.
284 intpr(int interval, void (*pfunc)(char *), int af)
286 struct ifaddrs *ifap, *ifa;
287 struct ifmaddrs *ifmap, *ifma;
290 return sidewaysintpr(interval);
292 if (getifaddrs(&ifap) != 0)
293 err(EX_OSERR, "getifaddrs");
294 if (aflag && getifmaddrs(&ifmap) != 0)
295 err(EX_OSERR, "getifmaddrs");
297 xo_open_list("interface");
300 xo_emit("{T:/%-7.7s}", "Name");
302 xo_emit("{T:/%-5.5s}", "Name");
303 xo_emit(" {T:/%5.5s} {T:/%-13.13s} {T:/%-17.17s} {T:/%8.8s} "
304 "{T:/%5.5s} {T:/%5.5s}",
305 "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
307 xo_emit(" {T:/%10.10s}","Ibytes");
308 xo_emit(" {T:/%8.8s} {T:/%5.5s}", "Opkts", "Oerrs");
310 xo_emit(" {T:/%10.10s}","Obytes");
311 xo_emit(" {T:/%5s}", "Coll");
313 xo_emit(" {T:/%s}", "Drop");
317 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
318 bool network = false, link = false;
321 if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
324 name = ifa->ifa_name;
331 * Skip all ifaddrs belonging to same interface.
333 while(ifa->ifa_next != NULL &&
334 (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
340 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
343 xo_open_instance("interface");
346 xo_emit("{tk:name/%-7.7s}", name);
348 xo_emit("{tk:name/%-5.5s}", name);
350 #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
351 show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa));
354 switch (ifa->ifa_addr->sa_family) {
356 xo_emit("{:network/%-13.13s} ", "none");
357 xo_emit("{:address/%-15.15s} ", "none");
361 struct sockaddr_in *sin, *mask;
363 sin = (struct sockaddr_in *)ifa->ifa_addr;
364 mask = (struct sockaddr_in *)ifa->ifa_netmask;
365 xo_emit("{t:network/%-13.13s} ",
366 netname(sin->sin_addr.s_addr,
367 mask->sin_addr.s_addr));
368 xo_emit("{t:address/%-17.17s} ",
369 routename(sin->sin_addr.s_addr));
377 struct sockaddr_in6 *sin6, *mask;
379 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
380 mask = (struct sockaddr_in6 *)ifa->ifa_netmask;
382 xo_emit("{t:network/%-13.13s} ",
383 netname6(sin6, &mask->sin6_addr));
384 getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
385 addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
386 xo_emit("{t:address/%-17.17s} ", addr_buf);
394 struct sockaddr_dl *sdl;
395 char *cp, linknum[10];
400 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
401 cp = (char *)LLADDR(sdl);
403 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
404 xo_emit("{t:network/%-13.13s} ", linknum);
407 while ((--n >= 0) && (z < len)) {
408 snprintf(buf + z, len - z, "%02x%c",
409 *cp++ & 0xff, n > 0 ? ':' : ' ');
413 xo_emit("{:address/%*s}", 32 - z, buf);
421 #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
422 show_stat("lu", 8, "received-packets", IFA_STAT(ipackets),
424 show_stat("lu", 5, "received-errors", IFA_STAT(ierrors), link);
425 show_stat("lu", 5, "dropped-packet", IFA_STAT(iqdrops), link);
427 show_stat("lu", 10, "received-bytes", IFA_STAT(ibytes),
429 show_stat("lu", 8, "sent-packets", IFA_STAT(opackets),
431 show_stat("lu", 5, "send-errors", IFA_STAT(oerrors), link);
433 show_stat("lu", 10, "sent-bytes", IFA_STAT(obytes),
435 show_stat("NRSlu", 5, "collisions", IFA_STAT(collisions), link);
437 show_stat("LSlu", 5, "dropped-packets",
438 IFA_STAT(oqdrops), link);
442 xo_close_instance("interface");
447 * Print family's multicast addresses.
449 xo_open_list("multicast-address");
450 for (ifma = next_ifma(ifmap, ifa->ifa_name,
451 ifa->ifa_addr->sa_family);
453 ifma = next_ifma(ifma, ifa->ifa_name,
454 ifa->ifa_addr->sa_family)) {
455 const char *fmt = NULL;
457 xo_open_instance("multicast-address");
458 switch (ifma->ifma_addr->sa_family) {
461 struct sockaddr_in *sin;
463 sin = (struct sockaddr_in *)ifma->ifma_addr;
464 fmt = routename(sin->sin_addr.s_addr);
470 /* in6_fillscopeid(&msa.in6); */
471 getnameinfo(ifma->ifma_addr,
472 ifma->ifma_addr->sa_len, addr_buf,
473 sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
474 xo_emit("{P:/%*s }{t:address/%-19.19s}",
475 Wflag ? 27 : 25, "", addr_buf);
480 struct sockaddr_dl *sdl;
482 sdl = (struct sockaddr_dl *)ifma->ifma_addr;
483 switch (sdl->sdl_type) {
487 (struct ether_addr *)LLADDR(sdl));
495 xo_emit("{P:/%*s }{t:address/%-17.17s/}",
496 Wflag ? 27 : 25, "", fmt);
497 if (ifma->ifma_addr->sa_family == AF_LINK) {
498 xo_emit(" {:received-packets/%8lu}",
500 xo_emit("{P:/%*s}", bflag? 17 : 6, "");
501 xo_emit(" {:sent-packets/%8lu}",
506 xo_close_instance("multicast-address");
507 ifma = ifma->ifma_next;
509 xo_close_list("multicast-address");
510 xo_close_instance("interface");
512 xo_close_list("interface");
520 u_long ift_ip; /* input packets */
521 u_long ift_ie; /* input errors */
522 u_long ift_id; /* input drops */
523 u_long ift_op; /* output packets */
524 u_long ift_oe; /* output errors */
525 u_long ift_od; /* output drops */
526 u_long ift_co; /* collisions */
527 u_long ift_ib; /* input bytes */
528 u_long ift_ob; /* output bytes */
532 * Obtain stats for interface(s).
535 fill_iftot(struct iftot *st)
537 struct ifaddrs *ifap, *ifa;
540 if (getifaddrs(&ifap) != 0)
541 xo_err(EX_OSERR, "getifaddrs");
543 bzero(st, sizeof(*st));
545 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
546 if (ifa->ifa_addr->sa_family != AF_LINK)
549 if (strcmp(ifa->ifa_name, interface) == 0)
555 st->ift_ip += IFA_STAT(ipackets);
556 st->ift_ie += IFA_STAT(ierrors);
557 st->ift_id += IFA_STAT(iqdrops);
558 st->ift_ib += IFA_STAT(ibytes);
559 st->ift_op += IFA_STAT(opackets);
560 st->ift_oe += IFA_STAT(oerrors);
561 st->ift_od += IFA_STAT(oqdrops);
562 st->ift_ob += IFA_STAT(obytes);
563 st->ift_co += IFA_STAT(collisions);
566 if (interface && found == false)
567 xo_err(EX_DATAERR, "interface %s not found", interface);
573 * Set a flag to indicate that a signal from the periodic itimer has been
576 static sig_atomic_t signalled;
578 catchalarm(int signo __unused)
584 * Print a running summary of interface statistics.
585 * Repeat display every interval seconds, showing statistics
586 * collected over that interval. Assumes that interval is non-zero.
587 * First line printed at top of screen is always cumulative.
590 sidewaysintpr(int interval)
592 struct iftot ift[2], *new, *old;
593 struct itimerval interval_it;
600 (void)signal(SIGALRM, catchalarm);
602 interval_it.it_interval.tv_sec = interval;
603 interval_it.it_interval.tv_usec = 0;
604 interval_it.it_value = interval_it.it_interval;
605 setitimer(ITIMER_REAL, &interval_it, NULL);
606 xo_open_list("interface-statistics");
609 xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input",
610 interface != NULL ? interface : "(Total)", "output");
611 xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} "
612 "{T:/%10s} {T:/%5s}",
613 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
616 xo_emit(" {T:/%5.5s}", "drops");
622 if ((noutputs != 0) && (--noutputs == 0)) {
623 xo_close_list("interface-statistics");
626 oldmask = sigblock(sigmask(SIGALRM));
635 xo_open_instance("stats");
636 show_stat("lu", 10, "received-packets",
637 new->ift_ip - old->ift_ip, 1);
638 show_stat("lu", 5, "received-errors",
639 new->ift_ie - old->ift_ie, 1);
640 show_stat("lu", 5, "dropped-packets",
641 new->ift_id - old->ift_id, 1);
642 show_stat("lu", 10, "received-bytes",
643 new->ift_ib - old->ift_ib, 1);
644 show_stat("lu", 10, "sent-packets",
645 new->ift_op - old->ift_op, 1);
646 show_stat("lu", 5, "send-errors",
647 new->ift_oe - old->ift_oe, 1);
648 show_stat("lu", 10, "sent-bytes",
649 new->ift_ob - old->ift_ob, 1);
650 show_stat("NRSlu", 5, "collisions",
651 new->ift_co - old->ift_co, 1);
653 show_stat("LSlu", 5, "dropped-packets",
654 new->ift_od - old->ift_od, 1);
655 xo_close_instance("stats");
659 if (new == &ift[0]) {