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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/ethernet.h>
55 #include <net/pfvar.h>
56 #include <net/if_pfsync.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netipx/ipx.h>
60 #include <netipx/ipx_if.h>
61 #include <arpa/inet.h>
81 static void sidewaysintpr(int, u_long);
82 static void catchalarm(int);
85 static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */
89 * Dump pfsync statistics structure.
92 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
94 struct pfsyncstats pfsyncstat, zerostat;
95 size_t len = sizeof(struct pfsyncstats);
99 memset(&zerostat, 0, len);
100 if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
101 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
103 warn("sysctl: net.inet.pfsync.stats");
107 kread(off, &pfsyncstat, len);
109 printf("%s:\n", name);
111 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
112 printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
113 #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
114 printf(m, (uintmax_t)pfsyncstat.f)
116 p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
117 p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
118 p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
119 p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
120 p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
121 p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
122 p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
123 p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
124 p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
125 p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
126 p(pfsyncs_stale, "\t\t%ju stale state%s\n");
127 p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
128 p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
129 p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
130 p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n");
131 p2(pfsyncs_oerrors, "\t\t%ju send error\n");
137 * Display a formatted value, or a '-' in the same space.
140 show_stat(const char *fmt, int width, u_long value, short showvalue)
142 const char *lsep, *rsep;
146 if (strncmp(fmt, "LS", 2) == 0) {
151 if (strncmp(fmt, "NRS", 3) == 0) {
155 if (showvalue == 0) {
156 /* Print just dash. */
157 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
165 /* Format in human readable form. */
166 humanize_number(buf, sizeof(buf), (int64_t)value, "",
167 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
168 sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
171 /* Construct the format string. */
172 sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
173 printf(newfmt, value);
178 * Print a description of the network interfaces.
181 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
184 struct ifnethead ifnethead;
189 struct in6_ifaddr in6;
191 struct ipx_ifaddr ipx;
207 struct sockaddr *sa = NULL;
212 if (ifnetaddr == 0) {
213 printf("ifnet: symbol not defined\n");
217 sidewaysintpr(interval1, ifnetaddr);
220 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
222 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
223 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
228 printf("%-7.7s", "Name");
230 printf("%-5.5s", "Name");
231 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
232 "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
234 printf(" %10.10s","Ibytes");
235 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
237 printf(" %10.10s","Obytes");
238 printf(" %5s", "Coll");
240 printf(" %s", "Time");
242 printf(" %s", "Drop");
246 while (ifnetaddr || ifaddraddr) {
247 struct sockaddr_in *sockin;
249 struct sockaddr_in6 *sockin6;
257 if (ifaddraddr == 0) {
258 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
260 strlcpy(name, ifnet.if_xname, sizeof(name));
261 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
262 if (interface != 0 && strcmp(name, interface) != 0)
264 cp = index(name, '\0');
271 if ((ifnet.if_flags&IFF_UP) == 0)
274 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
276 ifaddrfound = ifaddraddr;
279 * Get the interface stats. These may get
280 * overriden below on a per-interface basis.
282 opackets = ifnet.if_opackets;
283 ipackets = ifnet.if_ipackets;
284 obytes = ifnet.if_obytes;
285 ibytes = ifnet.if_ibytes;
286 omcasts = ifnet.if_omcasts;
287 imcasts = ifnet.if_imcasts;
288 oerrors = ifnet.if_oerrors;
289 ierrors = ifnet.if_ierrors;
290 idrops = ifnet.if_iqdrops;
291 collisions = ifnet.if_collisions;
292 timer = ifnet.if_timer;
293 drops = ifnet.if_snd.ifq_drops;
295 if (ifaddraddr == 0) {
297 printf("%-7.7s", name);
299 printf("%-5.5s", name);
300 printf(" %5lu ", ifnet.if_mtu);
301 printf("%-13.13s ", "none");
302 printf("%-17.17s ", "none");
304 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
309 #define CP(x) ((char *)(x))
310 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
312 sa = (struct sockaddr *)cp;
313 if (af != AF_UNSPEC && sa->sa_family != af) {
315 (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
319 printf("%-7.7s", name);
321 printf("%-5.5s", name);
322 printf(" %5lu ", ifnet.if_mtu);
323 switch (sa->sa_family) {
325 printf("%-13.13s ", "none");
326 printf("%-15.15s ", "none");
329 sockin = (struct sockaddr_in *)sa;
331 /* can't use inet_makeaddr because kernel
332 * keeps nets unshifted.
334 in = inet_makeaddr(ifaddr.in.ia_subnet,
336 printf("%-13.13s ", netname(in.s_addr,
337 ifaddr.in.ia_subnetmask));
340 netname(htonl(ifaddr.in.ia_subnet),
341 ifaddr.in.ia_subnetmask));
344 routename(sockin->sin_addr.s_addr));
350 sockin6 = (struct sockaddr_in6 *)sa;
351 in6_fillscopeid(&ifaddr.in6.ia_addr);
353 netname6(&ifaddr.in6.ia_addr,
354 &ifaddr.in6.ia_prefixmask.sin6_addr));
355 in6_fillscopeid(sockin6);
356 getnameinfo(sa, sa->sa_len, addr_buf,
357 sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
358 printf("%-17.17s ", addr_buf);
365 struct sockaddr_ipx *sipx =
366 (struct sockaddr_ipx *)sa;
370 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
371 sprintf(netnum, "%lx", (u_long)ntohl(net));
372 printf("ipx:%-8s ", netnum);
373 /* printf("ipx:%-8s ", netname(net, 0L)); */
375 ipx_phost((struct sockaddr *)sipx));
382 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
383 printf("%-11.11s ",atalk_print(sa,0x0b) );
387 struct sockaddr_dl *sdl =
388 (struct sockaddr_dl *)sa;
390 cp = (char *)LLADDR(sdl);
392 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
393 m = printf("%-13.13s ", linknum);
397 m = printf("(%d)", sa->sa_family);
398 for (cp = sa->sa_len + (char *)sa;
399 --cp > sa->sa_data && (*cp == 0);) {}
400 n = cp - sa->sa_data + 1;
403 while ((--n >= 0) && (m < 30))
404 m += printf("%02x%c", *cp++ & 0xff,
415 * Fixup the statistics for interfaces that
416 * update stats for their network addresses
419 opackets = ifaddr.in.ia_ifa.if_opackets;
420 ipackets = ifaddr.in.ia_ifa.if_ipackets;
421 obytes = ifaddr.in.ia_ifa.if_obytes;
422 ibytes = ifaddr.in.ia_ifa.if_ibytes;
425 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
428 show_stat("lu", 8, ipackets, link_layer|network_layer);
429 show_stat("lu", 5, ierrors, link_layer);
430 show_stat("lu", 5, idrops, link_layer);
432 show_stat("lu", 10, ibytes, link_layer|network_layer);
434 show_stat("lu", 8, opackets, link_layer|network_layer);
435 show_stat("lu", 5, oerrors, link_layer);
437 show_stat("lu", 10, obytes, link_layer|network_layer);
439 show_stat("NRSlu", 5, collisions, link_layer);
441 show_stat("LSd", 4, timer, link_layer);
443 show_stat("LSd", 4, drops, link_layer);
446 if (aflag && ifaddrfound) {
448 * Print family's multicast addresses
450 struct ifmultiaddr *multiaddr;
451 struct ifmultiaddr ifma;
454 struct sockaddr_in in;
456 struct sockaddr_in6 in6;
458 struct sockaddr_dl dl;
462 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
463 if (kread((u_long)multiaddr, (char *)&ifma,
467 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
470 if (msa.sa.sa_family != sa->sa_family)
474 switch (msa.sa.sa_family) {
476 fmt = routename(msa.in.sin_addr.s_addr);
480 in6_fillscopeid(&msa.in6);
481 getnameinfo(&msa.sa, msa.sa.sa_len,
482 addr_buf, sizeof(addr_buf), 0, 0,
484 printf("%*s %-19.19s(refs: %d)\n",
486 addr_buf, ifma.ifma_refcount);
490 switch (msa.dl.sdl_type) {
494 (struct ether_addr *)
501 printf("%*s %-17.17s",
502 Wflag ? 27 : 25, "", fmt);
503 if (msa.sa.sa_family == AF_LINK) {
504 printf(" %8lu", imcasts);
507 printf(" %8lu", omcasts);
517 SLIST_ENTRY(iftot) chain;
518 char ift_name[IFNAMSIZ]; /* interface name */
519 u_long ift_ip; /* input packets */
520 u_long ift_ie; /* input errors */
521 u_long ift_id; /* input drops */
522 u_long ift_op; /* output packets */
523 u_long ift_oe; /* output errors */
524 u_long ift_co; /* collisions */
525 u_int ift_dr; /* drops */
526 u_long ift_ib; /* input bytes */
527 u_long ift_ob; /* output bytes */
530 u_char signalled; /* set if alarm goes off "early" */
533 * Print a running summary of interface statistics.
534 * Repeat display every interval1 seconds, showing statistics
535 * collected over that interval. Assumes that interval1 is non-zero.
536 * First line printed at top of screen is always cumulative.
537 * XXX - should be rewritten to use ifmib(4).
540 sidewaysintpr(int interval1, u_long off)
544 struct ifnethead ifnethead;
545 struct itimerval interval_it;
546 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
549 u_long interesting_off;
551 if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
553 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
555 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
556 printf("malloc failed\n");
559 memset(iftot, 0, sizeof(struct iftot));
563 for (off = firstifnet, ip = iftot; off;) {
566 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
568 strlcpy(name, ifnet.if_xname, sizeof(name));
569 if (interface && strcmp(name, interface) == 0) {
571 interesting_off = off;
573 snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
574 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
575 printf("malloc failed\n");
578 memset(ipn, 0, sizeof(struct iftot));
579 SLIST_NEXT(ip, chain) = ipn;
581 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
583 if (interface && interesting == NULL)
584 errx(1, "%s: unknown interface", interface);
585 if ((total = malloc(sizeof(struct iftot))) == NULL) {
586 printf("malloc failed\n");
589 memset(total, 0, sizeof(struct iftot));
590 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
591 printf("malloc failed\n");
594 memset(sum, 0, sizeof(struct iftot));
596 (void)signal(SIGALRM, catchalarm);
598 interval_it.it_interval.tv_sec = interval1;
599 interval_it.it_interval.tv_usec = 0;
600 interval_it.it_value = interval_it.it_interval;
601 setitimer(ITIMER_REAL, &interval_it, NULL);
604 printf("%17s %14s %16s", "input",
605 interesting ? interesting->ift_name : "(Total)", "output");
607 printf("%10s %5s %5s %10s %10s %5s %10s %5s",
608 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
611 printf(" %5.5s", "drops");
616 if (interesting != NULL) {
618 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
623 show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
624 show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
625 show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
626 show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
627 show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
628 show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
629 show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
630 show_stat("NRSlu", 5,
631 ifnet.if_collisions - ip->ift_co, 1);
634 ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
636 ip->ift_ip = ifnet.if_ipackets;
637 ip->ift_ie = ifnet.if_ierrors;
638 ip->ift_id = ifnet.if_iqdrops;
639 ip->ift_ib = ifnet.if_ibytes;
640 ip->ift_op = ifnet.if_opackets;
641 ip->ift_oe = ifnet.if_oerrors;
642 ip->ift_ob = ifnet.if_obytes;
643 ip->ift_co = ifnet.if_collisions;
644 ip->ift_dr = ifnet.if_snd.ifq_drops;
655 for (off = firstifnet, ip = iftot;
656 off && SLIST_NEXT(ip, chain) != NULL;
657 ip = SLIST_NEXT(ip, chain)) {
658 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
662 sum->ift_ip += ifnet.if_ipackets;
663 sum->ift_ie += ifnet.if_ierrors;
664 sum->ift_id += ifnet.if_iqdrops;
665 sum->ift_ib += ifnet.if_ibytes;
666 sum->ift_op += ifnet.if_opackets;
667 sum->ift_oe += ifnet.if_oerrors;
668 sum->ift_ob += ifnet.if_obytes;
669 sum->ift_co += ifnet.if_collisions;
670 sum->ift_dr += ifnet.if_snd.ifq_drops;
671 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
674 show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
675 show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
676 show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
677 show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
678 show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
679 show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
680 show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
681 show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
684 sum->ift_dr - total->ift_dr, 1);
691 if ((noutputs != 0) && (--noutputs == 0))
693 oldmask = sigblock(sigmask(SIGALRM));
708 * Set a flag to indicate that a signal from the periodic itimer has been
712 catchalarm(int signo __unused)