2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sysctl.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/ethernet.h>
51 #include <net/pfvar.h>
52 #include <net/if_pfsync.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netipx/ipx.h>
56 #include <netipx/ipx_if.h>
57 #include <arpa/inet.h>
77 static void sidewaysintpr(int, u_long);
78 static void catchalarm(int);
81 static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */
84 static const char* pfsyncacts[] = {
85 /* PFSYNC_ACT_CLR */ "clear all request",
86 /* PFSYNC_ACT_INS */ "state insert",
87 /* PFSYNC_ACT_INS_ACK */ "state inserted ack",
88 /* PFSYNC_ACT_UPD */ "state update",
89 /* PFSYNC_ACT_UPD_C */ "compressed state update",
90 /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request",
91 /* PFSYNC_ACT_DEL */ "state delete",
92 /* PFSYNC_ACT_DEL_C */ "compressed state delete",
93 /* PFSYNC_ACT_INS_F */ "fragment insert",
94 /* PFSYNC_ACT_DEL_F */ "fragment delete",
95 /* PFSYNC_ACT_BUS */ "bulk update mark",
96 /* PFSYNC_ACT_TDB */ "TDB replay counter update",
97 /* PFSYNC_ACT_EOF */ "end of frame mark",
101 pfsync_acts_stats(const char *fmt, uint64_t *a)
105 for (i = 0; i < PFSYNC_ACT_MAX; i++, a++)
106 if (*a || sflag <= 1)
107 printf(fmt, *a, pfsyncacts[i], plural(*a));
111 * Dump pfsync statistics structure.
114 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
116 struct pfsyncstats pfsyncstat, zerostat;
117 size_t len = sizeof(struct pfsyncstats);
121 memset(&zerostat, 0, len);
122 if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len,
123 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
125 warn("sysctl: net.pfsync.stats");
129 kread(off, &pfsyncstat, len);
131 printf("%s:\n", name);
133 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
134 printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
136 p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
137 p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
138 pfsync_acts_stats("\t %ju %s%s received\n",
139 &pfsyncstat.pfsyncs_iacts[0]);
140 p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
141 p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
142 p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
143 p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
144 p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
145 p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
146 p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
147 p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
148 p(pfsyncs_stale, "\t\t%ju stale state%s\n");
149 p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
150 p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
151 p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
152 pfsync_acts_stats("\t %ju %s%s sent\n",
153 &pfsyncstat.pfsyncs_oacts[0]);
154 p(pfsyncs_onomem, "\t\t%ju failure%s due to mbuf memory error\n");
155 p(pfsyncs_oerrors, "\t\t%ju send error%s\n");
160 * Display a formatted value, or a '-' in the same space.
163 show_stat(const char *fmt, int width, u_long value, short showvalue)
165 const char *lsep, *rsep;
169 if (strncmp(fmt, "LS", 2) == 0) {
174 if (strncmp(fmt, "NRS", 3) == 0) {
178 if (showvalue == 0) {
179 /* Print just dash. */
180 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
188 /* Format in human readable form. */
189 humanize_number(buf, sizeof(buf), (int64_t)value, "",
190 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
191 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
194 /* Construct the format string. */
195 sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
196 printf(newfmt, value);
201 * Print a description of the network interfaces.
204 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
207 struct ifnethead ifnethead;
212 struct in6_ifaddr in6;
214 struct ipx_ifaddr ipx;
229 struct sockaddr *sa = NULL;
234 if (ifnetaddr == 0) {
235 printf("ifnet: symbol not defined\n");
239 sidewaysintpr(interval1, ifnetaddr);
242 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
244 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
245 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
250 printf("%-7.7s", "Name");
252 printf("%-5.5s", "Name");
253 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
254 "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
256 printf(" %10.10s","Ibytes");
257 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
259 printf(" %10.10s","Obytes");
260 printf(" %5s", "Coll");
262 printf(" %s", "Drop");
266 while (ifnetaddr || ifaddraddr) {
267 struct sockaddr_in *sockin;
269 struct sockaddr_in6 *sockin6;
277 if (ifaddraddr == 0) {
278 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
280 strlcpy(name, ifnet.if_xname, sizeof(name));
281 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
282 if (interface != 0 && strcmp(name, interface) != 0)
284 cp = strchr(name, '\0');
291 if ((ifnet.if_flags&IFF_UP) == 0)
294 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
296 ifaddrfound = ifaddraddr;
299 * Get the interface stats. These may get
300 * overriden below on a per-interface basis.
302 opackets = ifnet.if_opackets;
303 ipackets = ifnet.if_ipackets;
304 obytes = ifnet.if_obytes;
305 ibytes = ifnet.if_ibytes;
306 omcasts = ifnet.if_omcasts;
307 imcasts = ifnet.if_imcasts;
308 oerrors = ifnet.if_oerrors;
309 ierrors = ifnet.if_ierrors;
310 idrops = ifnet.if_iqdrops;
311 collisions = ifnet.if_collisions;
312 drops = ifnet.if_snd.ifq_drops;
314 if (ifaddraddr == 0) {
316 printf("%-7.7s", name);
318 printf("%-5.5s", name);
319 printf(" %5lu ", ifnet.if_mtu);
320 printf("%-13.13s ", "none");
321 printf("%-17.17s ", "none");
323 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
328 #define CP(x) ((char *)(x))
329 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
331 sa = (struct sockaddr *)cp;
332 if (af != AF_UNSPEC && sa->sa_family != af) {
334 (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
338 printf("%-7.7s", name);
340 printf("%-5.5s", name);
341 printf(" %5lu ", ifnet.if_mtu);
342 switch (sa->sa_family) {
344 printf("%-13.13s ", "none");
345 printf("%-15.15s ", "none");
348 sockin = (struct sockaddr_in *)sa;
350 /* can't use inet_makeaddr because kernel
351 * keeps nets unshifted.
353 in = inet_makeaddr(ifaddr.in.ia_subnet,
355 printf("%-13.13s ", netname(in.s_addr,
356 ifaddr.in.ia_subnetmask));
359 netname(htonl(ifaddr.in.ia_subnet),
360 ifaddr.in.ia_subnetmask));
363 routename(sockin->sin_addr.s_addr));
369 sockin6 = (struct sockaddr_in6 *)sa;
370 in6_fillscopeid(&ifaddr.in6.ia_addr);
372 netname6(&ifaddr.in6.ia_addr,
373 &ifaddr.in6.ia_prefixmask.sin6_addr));
374 in6_fillscopeid(sockin6);
375 getnameinfo(sa, sa->sa_len, addr_buf,
376 sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
377 printf("%-17.17s ", addr_buf);
384 struct sockaddr_ipx *sipx =
385 (struct sockaddr_ipx *)sa;
389 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
390 sprintf(netnum, "%lx", (u_long)ntohl(net));
391 printf("ipx:%-8s ", netnum);
392 /* printf("ipx:%-8s ", netname(net, 0L)); */
394 ipx_phost((struct sockaddr *)sipx));
401 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
402 printf("%-11.11s ",atalk_print(sa,0x0b) );
406 struct sockaddr_dl *sdl =
407 (struct sockaddr_dl *)sa;
409 cp = (char *)LLADDR(sdl);
411 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
412 m = printf("%-13.13s ", linknum);
416 m = printf("(%d)", sa->sa_family);
417 for (cp = sa->sa_len + (char *)sa;
418 --cp > sa->sa_data && (*cp == 0);) {}
419 n = cp - sa->sa_data + 1;
422 while ((--n >= 0) && (m < 30))
423 m += printf("%02x%c", *cp++ & 0xff,
434 * Fixup the statistics for interfaces that
435 * update stats for their network addresses
438 opackets = ifaddr.in.ia_ifa.if_opackets;
439 ipackets = ifaddr.in.ia_ifa.if_ipackets;
440 obytes = ifaddr.in.ia_ifa.if_obytes;
441 ibytes = ifaddr.in.ia_ifa.if_ibytes;
444 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
447 show_stat("lu", 8, ipackets, link_layer|network_layer);
448 show_stat("lu", 5, ierrors, link_layer);
449 show_stat("lu", 5, idrops, link_layer);
451 show_stat("lu", 10, ibytes, link_layer|network_layer);
453 show_stat("lu", 8, opackets, link_layer|network_layer);
454 show_stat("lu", 5, oerrors, link_layer);
456 show_stat("lu", 10, obytes, link_layer|network_layer);
458 show_stat("NRSlu", 5, collisions, link_layer);
460 show_stat("LSd", 4, drops, link_layer);
463 if (aflag && ifaddrfound) {
465 * Print family's multicast addresses
467 struct ifmultiaddr *multiaddr;
468 struct ifmultiaddr ifma;
471 struct sockaddr_in in;
473 struct sockaddr_in6 in6;
475 struct sockaddr_dl dl;
479 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
480 if (kread((u_long)multiaddr, (char *)&ifma,
484 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
487 if (msa.sa.sa_family != sa->sa_family)
491 switch (msa.sa.sa_family) {
493 fmt = routename(msa.in.sin_addr.s_addr);
497 in6_fillscopeid(&msa.in6);
498 getnameinfo(&msa.sa, msa.sa.sa_len,
499 addr_buf, sizeof(addr_buf), 0, 0,
501 printf("%*s %-19.19s(refs: %d)\n",
503 addr_buf, ifma.ifma_refcount);
507 switch (msa.dl.sdl_type) {
511 (struct ether_addr *)
518 printf("%*s %-17.17s",
519 Wflag ? 27 : 25, "", fmt);
520 if (msa.sa.sa_family == AF_LINK) {
521 printf(" %8lu", imcasts);
524 printf(" %8lu", omcasts);
534 SLIST_ENTRY(iftot) chain;
535 char ift_name[IFNAMSIZ]; /* interface name */
536 u_long ift_ip; /* input packets */
537 u_long ift_ie; /* input errors */
538 u_long ift_id; /* input drops */
539 u_long ift_op; /* output packets */
540 u_long ift_oe; /* output errors */
541 u_long ift_co; /* collisions */
542 u_int ift_dr; /* drops */
543 u_long ift_ib; /* input bytes */
544 u_long ift_ob; /* output bytes */
547 u_char signalled; /* set if alarm goes off "early" */
550 * Print a running summary of interface statistics.
551 * Repeat display every interval1 seconds, showing statistics
552 * collected over that interval. Assumes that interval1 is non-zero.
553 * First line printed at top of screen is always cumulative.
554 * XXX - should be rewritten to use ifmib(4).
557 sidewaysintpr(int interval1, u_long off)
561 struct ifnethead ifnethead;
562 struct itimerval interval_it;
563 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
566 u_long interesting_off;
568 if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
570 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
572 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
573 printf("malloc failed\n");
576 memset(iftot, 0, sizeof(struct iftot));
580 for (off = firstifnet, ip = iftot; off;) {
583 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
585 strlcpy(name, ifnet.if_xname, sizeof(name));
586 if (interface && strcmp(name, interface) == 0) {
588 interesting_off = off;
590 snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);
591 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
592 printf("malloc failed\n");
595 memset(ipn, 0, sizeof(struct iftot));
596 SLIST_NEXT(ip, chain) = ipn;
598 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
600 if (interface && interesting == NULL)
601 errx(1, "%s: unknown interface", interface);
602 if ((total = malloc(sizeof(struct iftot))) == NULL) {
603 printf("malloc failed\n");
606 memset(total, 0, sizeof(struct iftot));
607 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
608 printf("malloc failed\n");
611 memset(sum, 0, sizeof(struct iftot));
613 (void)signal(SIGALRM, catchalarm);
615 interval_it.it_interval.tv_sec = interval1;
616 interval_it.it_interval.tv_usec = 0;
617 interval_it.it_value = interval_it.it_interval;
618 setitimer(ITIMER_REAL, &interval_it, NULL);
621 printf("%17s %14s %16s", "input",
622 interesting ? interesting->ift_name : "(Total)", "output");
624 printf("%10s %5s %5s %10s %10s %5s %10s %5s",
625 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
628 printf(" %5.5s", "drops");
633 if (interesting != NULL) {
635 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
640 show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
641 show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
642 show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
643 show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
644 show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
645 show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
646 show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
647 show_stat("NRSlu", 5,
648 ifnet.if_collisions - ip->ift_co, 1);
651 ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
653 ip->ift_ip = ifnet.if_ipackets;
654 ip->ift_ie = ifnet.if_ierrors;
655 ip->ift_id = ifnet.if_iqdrops;
656 ip->ift_ib = ifnet.if_ibytes;
657 ip->ift_op = ifnet.if_opackets;
658 ip->ift_oe = ifnet.if_oerrors;
659 ip->ift_ob = ifnet.if_obytes;
660 ip->ift_co = ifnet.if_collisions;
661 ip->ift_dr = ifnet.if_snd.ifq_drops;
672 for (off = firstifnet, ip = iftot;
673 off && SLIST_NEXT(ip, chain) != NULL;
674 ip = SLIST_NEXT(ip, chain)) {
675 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
679 sum->ift_ip += ifnet.if_ipackets;
680 sum->ift_ie += ifnet.if_ierrors;
681 sum->ift_id += ifnet.if_iqdrops;
682 sum->ift_ib += ifnet.if_ibytes;
683 sum->ift_op += ifnet.if_opackets;
684 sum->ift_oe += ifnet.if_oerrors;
685 sum->ift_ob += ifnet.if_obytes;
686 sum->ift_co += ifnet.if_collisions;
687 sum->ift_dr += ifnet.if_snd.ifq_drops;
688 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
691 show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
692 show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
693 show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
694 show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
695 show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
696 show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
697 show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
698 show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
701 sum->ift_dr - total->ift_dr, 1);
708 if ((noutputs != 0) && (--noutputs == 0))
710 oldmask = sigblock(sigmask(SIGALRM));
725 * Set a flag to indicate that a signal from the periodic itimer has been
729 catchalarm(int signo __unused)