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 <netipx/ipx.h>
55 #include <netipx/ipx_if.h>
56 #include <arpa/inet.h>
58 #include <net/pfvar.h>
59 #include <net/if_pfsync.h>
80 static void sidewaysintpr(int);
83 static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */
87 static const char* pfsyncacts[] = {
88 /* PFSYNC_ACT_CLR */ "clear all request",
89 /* PFSYNC_ACT_INS */ "state insert",
90 /* PFSYNC_ACT_INS_ACK */ "state inserted ack",
91 /* PFSYNC_ACT_UPD */ "state update",
92 /* PFSYNC_ACT_UPD_C */ "compressed state update",
93 /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request",
94 /* PFSYNC_ACT_DEL */ "state delete",
95 /* PFSYNC_ACT_DEL_C */ "compressed state delete",
96 /* PFSYNC_ACT_INS_F */ "fragment insert",
97 /* PFSYNC_ACT_DEL_F */ "fragment delete",
98 /* PFSYNC_ACT_BUS */ "bulk update mark",
99 /* PFSYNC_ACT_TDB */ "TDB replay counter update",
100 /* PFSYNC_ACT_EOF */ "end of frame mark",
104 pfsync_acts_stats(const char *fmt, uint64_t *a)
108 for (i = 0; i < PFSYNC_ACT_MAX; i++, a++)
109 if (*a || sflag <= 1)
110 printf(fmt, *a, pfsyncacts[i], plural(*a));
114 * Dump pfsync statistics structure.
117 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
119 struct pfsyncstats pfsyncstat, zerostat;
120 size_t len = sizeof(struct pfsyncstats);
124 memset(&zerostat, 0, len);
125 if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len,
126 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
128 warn("sysctl: net.pfsync.stats");
132 kread(off, &pfsyncstat, len);
134 printf("%s:\n", name);
136 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
137 printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
139 p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
140 p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
141 pfsync_acts_stats("\t %ju %s%s received\n",
142 &pfsyncstat.pfsyncs_iacts[0]);
143 p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
144 p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
145 p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
146 p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
147 p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
148 p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
149 p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
150 p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
151 p(pfsyncs_stale, "\t\t%ju stale state%s\n");
152 p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
153 p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
154 p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
155 pfsync_acts_stats("\t %ju %s%s sent\n",
156 &pfsyncstat.pfsyncs_oacts[0]);
157 p(pfsyncs_onomem, "\t\t%ju failure%s due to mbuf memory error\n");
158 p(pfsyncs_oerrors, "\t\t%ju send error%s\n");
164 * Display a formatted value, or a '-' in the same space.
167 show_stat(const char *fmt, int width, u_long value, short showvalue)
169 const char *lsep, *rsep;
173 if (strncmp(fmt, "LS", 2) == 0) {
178 if (strncmp(fmt, "NRS", 3) == 0) {
182 if (showvalue == 0) {
183 /* Print just dash. */
184 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
192 /* Format in human readable form. */
193 humanize_number(buf, sizeof(buf), (int64_t)value, "",
194 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
195 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
198 /* Construct the format string. */
199 sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
200 printf(newfmt, value);
205 * Find next multiaddr for a given interface name.
207 static struct ifmaddrs *
208 next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
211 for(; ifma != NULL; ifma = ifma->ifma_next) {
212 struct sockaddr_dl *sdl;
214 sdl = (struct sockaddr_dl *)ifma->ifma_name;
215 if (ifma->ifma_addr->sa_family == family &&
216 strcmp(sdl->sdl_data, name) == 0)
224 * Print a description of the network interfaces.
227 intpr(int interval, void (*pfunc)(char *))
229 struct ifaddrs *ifap, *ifa;
230 struct ifmaddrs *ifmap, *ifma;
233 return sidewaysintpr(interval);
235 if (getifaddrs(&ifap) != 0)
236 err(EX_OSERR, "getifaddrs");
237 if (aflag && getifmaddrs(&ifmap) != 0)
238 err(EX_OSERR, "getifmaddrs");
242 printf("%-7.7s", "Name");
244 printf("%-5.5s", "Name");
245 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
246 "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
248 printf(" %10.10s","Ibytes");
249 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
251 printf(" %10.10s","Obytes");
252 printf(" %5s", "Coll");
254 printf(" %s", "Drop");
258 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
259 bool network = false, link = false;
261 if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
267 name = ifa->ifa_name;
271 * Skip all ifaddrs belonging to same interface.
273 while(ifa->ifa_next != NULL &&
274 (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
280 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
284 printf("%-7.7s", ifa->ifa_name);
286 printf("%-5.5s", ifa->ifa_name);
288 #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
289 show_stat("lu", 6, IFA_MTU(ifa), IFA_MTU(ifa));
292 switch (ifa->ifa_addr->sa_family) {
294 printf("%-13.13s ", "none");
295 printf("%-15.15s ", "none");
299 struct sockaddr_in *sin, *mask;
301 sin = (struct sockaddr_in *)ifa->ifa_addr;
302 mask = (struct sockaddr_in *)ifa->ifa_netmask;
303 printf("%-13.13s ", netname(sin->sin_addr.s_addr,
304 mask->sin_addr.s_addr));
306 routename(sin->sin_addr.s_addr));
314 struct sockaddr_in6 *sin6, *mask;
316 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
317 mask = (struct sockaddr_in6 *)ifa->ifa_netmask;
319 printf("%-13.13s ", netname6(sin6, &mask->sin6_addr));
320 getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
321 addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
322 printf("%-17.17s ", addr_buf);
330 struct sockaddr_ipx *sipx;
334 sipx = (struct sockaddr_ipx *)ifa->ifa_addr;
335 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
337 sprintf(netnum, "%lx", (u_long)ntohl(net));
338 printf("ipx:%-8s ", netnum);
339 printf("%-17s ", ipx_phost((struct sockaddr *)sipx));
345 printf("atalk:%-12.12s ",
346 atalk_print(ifa->ifa_addr, 0x10));
348 atalk_print(ifa->ifa_addr, 0x0b));
352 struct sockaddr_dl *sdl;
353 char *cp, linknum[10];
356 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
357 cp = (char *)LLADDR(sdl);
359 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
360 m = printf("%-13.13s ", linknum);
362 while ((--n >= 0) && (m < 30))
363 m += printf("%02x%c", *cp++ & 0xff,
374 #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
375 show_stat("lu", 8, IFA_STAT(ipackets), link|network);
376 show_stat("lu", 5, IFA_STAT(ierrors), link);
377 show_stat("lu", 5, IFA_STAT(iqdrops), link);
379 show_stat("lu", 10, IFA_STAT(ibytes), link|network);
380 show_stat("lu", 8, IFA_STAT(opackets), link|network);
381 show_stat("lu", 5, IFA_STAT(oerrors), link);
383 show_stat("lu", 10, IFA_STAT(obytes), link|network);
384 show_stat("NRSlu", 5, IFA_STAT(collisions), link);
385 /* XXXGL: output queue drops */
392 * Print family's multicast addresses.
394 for (ifma = next_ifma(ifmap, ifa->ifa_name,
395 ifa->ifa_addr->sa_family);
397 ifma = next_ifma(ifma, ifa->ifa_name,
398 ifa->ifa_addr->sa_family)) {
399 const char *fmt = NULL;
401 switch (ifma->ifma_addr->sa_family) {
404 struct sockaddr_in *sin;
406 sin = (struct sockaddr_in *)ifma->ifma_addr;
407 fmt = routename(sin->sin_addr.s_addr);
413 /* in6_fillscopeid(&msa.in6); */
414 getnameinfo(ifma->ifma_addr,
415 ifma->ifma_addr->sa_len, addr_buf,
416 sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
418 Wflag ? 27 : 25, "", addr_buf);
423 struct sockaddr_dl *sdl;
425 sdl = (struct sockaddr_dl *)ifma->ifma_addr;
426 switch (sdl->sdl_type) {
430 (struct ether_addr *)LLADDR(sdl));
438 printf("%*s %-17.17s",
439 Wflag ? 27 : 25, "", fmt);
440 if (ifma->ifma_addr->sa_family == AF_LINK) {
441 printf(" %8lu", IFA_STAT(imcasts));
442 printf("%*s", bflag ? 17 : 6, "");
443 printf(" %8lu", IFA_STAT(omcasts));
448 ifma = ifma->ifma_next;
458 u_long ift_ip; /* input packets */
459 u_long ift_ie; /* input errors */
460 u_long ift_id; /* input drops */
461 u_long ift_op; /* output packets */
462 u_long ift_oe; /* output errors */
463 u_long ift_co; /* collisions */
464 u_long ift_ib; /* input bytes */
465 u_long ift_ob; /* output bytes */
469 * Obtain stats for interface(s).
472 fill_iftot(struct iftot *st)
474 struct ifaddrs *ifap, *ifa;
477 if (getifaddrs(&ifap) != 0)
478 err(EX_OSERR, "getifaddrs");
480 bzero(st, sizeof(*st));
482 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
483 if (ifa->ifa_addr->sa_family != AF_LINK)
486 if (strcmp(ifa->ifa_name, interface) == 0)
492 st->ift_ip += IFA_STAT(ipackets);
493 st->ift_ie += IFA_STAT(ierrors);
494 st->ift_id += IFA_STAT(iqdrops);
495 st->ift_ib += IFA_STAT(ibytes);
496 st->ift_op += IFA_STAT(opackets);
497 st->ift_oe += IFA_STAT(oerrors);
498 st->ift_ob += IFA_STAT(obytes);
499 st->ift_co += IFA_STAT(collisions);
502 if (interface && found == false)
503 err(EX_DATAERR, "interface %s not found", interface);
509 * Set a flag to indicate that a signal from the periodic itimer has been
512 static sig_atomic_t signalled;
514 catchalarm(int signo __unused)
520 * Print a running summary of interface statistics.
521 * Repeat display every interval seconds, showing statistics
522 * collected over that interval. Assumes that interval is non-zero.
523 * First line printed at top of screen is always cumulative.
526 sidewaysintpr(int interval)
528 struct iftot ift[2], *new, *old;
529 struct itimerval interval_it;
536 (void)signal(SIGALRM, catchalarm);
538 interval_it.it_interval.tv_sec = interval;
539 interval_it.it_interval.tv_usec = 0;
540 interval_it.it_value = interval_it.it_interval;
541 setitimer(ITIMER_REAL, &interval_it, NULL);
544 printf("%17s %14s %16s", "input",
545 interface != NULL ? interface : "(Total)", "output");
547 printf("%10s %5s %5s %10s %10s %5s %10s %5s",
548 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
551 printf(" %5.5s", "drops");
557 if ((noutputs != 0) && (--noutputs == 0))
559 oldmask = sigblock(sigmask(SIGALRM));
568 show_stat("lu", 10, new->ift_ip - old->ift_ip, 1);
569 show_stat("lu", 5, new->ift_ie - old->ift_ie, 1);
570 show_stat("lu", 5, new->ift_id - old->ift_id, 1);
571 show_stat("lu", 10, new->ift_ib - old->ift_ib, 1);
572 show_stat("lu", 10, new->ift_op - old->ift_op, 1);
573 show_stat("lu", 5, new->ift_oe - old->ift_oe, 1);
574 show_stat("lu", 10, new->ift_ob - old->ift_ob, 1);
575 show_stat("NRSlu", 5, new->ift_co - old->ift_co, 1);
576 /* XXXGL: output queue drops */
580 if (new == &ift[0]) {