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;
208 struct sockaddr *sa = NULL;
213 if (ifnetaddr == 0) {
214 printf("ifnet: symbol not defined\n");
218 sidewaysintpr(interval1, ifnetaddr);
221 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
223 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
224 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
229 printf("%-7.7s", "Name");
231 printf("%-5.5s", "Name");
232 printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
233 "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
235 printf(" %10.10s","Ibytes");
236 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
238 printf(" %10.10s","Obytes");
239 printf(" %5s", "Coll");
241 printf(" %s", "Time");
243 printf(" %s", "Drop");
247 while (ifnetaddr || ifaddraddr) {
248 struct sockaddr_in *sockin;
250 struct sockaddr_in6 *sockin6;
258 if (ifaddraddr == 0) {
259 ifnetfound = ifnetaddr;
260 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
262 strlcpy(name, ifnet.if_xname, sizeof(name));
263 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
264 if (interface != 0 && strcmp(name, interface) != 0)
266 cp = index(name, '\0');
273 if ((ifnet.if_flags&IFF_UP) == 0)
276 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
278 ifaddrfound = ifaddraddr;
281 * Get the interface stats. These may get
282 * overriden below on a per-interface basis.
284 opackets = ifnet.if_opackets;
285 ipackets = ifnet.if_ipackets;
286 obytes = ifnet.if_obytes;
287 ibytes = ifnet.if_ibytes;
288 omcasts = ifnet.if_omcasts;
289 imcasts = ifnet.if_imcasts;
290 oerrors = ifnet.if_oerrors;
291 ierrors = ifnet.if_ierrors;
292 idrops = ifnet.if_iqdrops;
293 collisions = ifnet.if_collisions;
294 timer = ifnet.if_timer;
295 drops = ifnet.if_snd.ifq_drops;
297 if (ifaddraddr == 0) {
299 printf("%-7.7s", name);
301 printf("%-5.5s", name);
302 printf(" %5lu ", ifnet.if_mtu);
303 printf("%-13.13s ", "none");
304 printf("%-17.17s ", "none");
306 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
311 #define CP(x) ((char *)(x))
312 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
314 sa = (struct sockaddr *)cp;
315 if (af != AF_UNSPEC && sa->sa_family != af) {
317 (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
321 printf("%-7.7s", name);
323 printf("%-5.5s", name);
324 printf(" %5lu ", ifnet.if_mtu);
325 switch (sa->sa_family) {
327 printf("%-13.13s ", "none");
328 printf("%-15.15s ", "none");
331 sockin = (struct sockaddr_in *)sa;
333 /* can't use inet_makeaddr because kernel
334 * keeps nets unshifted.
336 in = inet_makeaddr(ifaddr.in.ia_subnet,
338 printf("%-13.13s ", netname(in.s_addr,
339 ifaddr.in.ia_subnetmask));
342 netname(htonl(ifaddr.in.ia_subnet),
343 ifaddr.in.ia_subnetmask));
346 routename(sockin->sin_addr.s_addr));
352 sockin6 = (struct sockaddr_in6 *)sa;
353 in6_fillscopeid(&ifaddr.in6.ia_addr);
355 netname6(&ifaddr.in6.ia_addr,
356 &ifaddr.in6.ia_prefixmask.sin6_addr));
357 in6_fillscopeid(sockin6);
358 getnameinfo(sa, sa->sa_len, addr_buf,
359 sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
360 printf("%-17.17s ", addr_buf);
367 struct sockaddr_ipx *sipx =
368 (struct sockaddr_ipx *)sa;
372 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
373 sprintf(netnum, "%lx", (u_long)ntohl(net));
374 printf("ipx:%-8s ", netnum);
375 /* printf("ipx:%-8s ", netname(net, 0L)); */
377 ipx_phost((struct sockaddr *)sipx));
384 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
385 printf("%-11.11s ",atalk_print(sa,0x0b) );
389 struct sockaddr_dl *sdl =
390 (struct sockaddr_dl *)sa;
392 cp = (char *)LLADDR(sdl);
394 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
395 m = printf("%-13.13s ", linknum);
399 m = printf("(%d)", sa->sa_family);
400 for (cp = sa->sa_len + (char *)sa;
401 --cp > sa->sa_data && (*cp == 0);) {}
402 n = cp - sa->sa_data + 1;
406 m += printf("%02x%c", *cp++ & 0xff,
417 * Fixup the statistics for interfaces that
418 * update stats for their network addresses
421 opackets = ifaddr.in.ia_ifa.if_opackets;
422 ipackets = ifaddr.in.ia_ifa.if_ipackets;
423 obytes = ifaddr.in.ia_ifa.if_obytes;
424 ibytes = ifaddr.in.ia_ifa.if_ibytes;
427 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
430 show_stat("lu", 8, ipackets, link_layer|network_layer);
431 show_stat("lu", 5, ierrors, link_layer);
432 show_stat("lu", 5, idrops, link_layer);
434 show_stat("lu", 10, ibytes, link_layer|network_layer);
436 show_stat("lu", 8, opackets, link_layer|network_layer);
437 show_stat("lu", 5, oerrors, link_layer);
439 show_stat("lu", 10, obytes, link_layer|network_layer);
441 show_stat("NRSlu", 5, collisions, link_layer);
443 show_stat("LSd", 4, timer, link_layer);
445 show_stat("LSd", 4, drops, link_layer);
448 if (aflag && ifaddrfound) {
450 * Print family's multicast addresses
452 struct ifmultiaddr *multiaddr;
453 struct ifmultiaddr ifma;
456 struct sockaddr_in in;
458 struct sockaddr_in6 in6;
460 struct sockaddr_dl dl;
464 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
465 if (kread((u_long)multiaddr, (char *)&ifma,
469 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
472 if (msa.sa.sa_family != sa->sa_family)
476 switch (msa.sa.sa_family) {
478 fmt = routename(msa.in.sin_addr.s_addr);
482 in6_fillscopeid(&msa.in6);
483 getnameinfo(&msa.sa, msa.sa.sa_len,
484 addr_buf, sizeof(addr_buf), 0, 0,
486 printf("%*s %-19.19s(refs: %d)\n",
488 addr_buf, ifma.ifma_refcount);
492 switch (msa.dl.sdl_type) {
496 (struct ether_addr *)
503 printf("%*s %-17.17s",
504 Wflag ? 27 : 25, "", fmt);
505 if (msa.sa.sa_family == AF_LINK) {
506 printf(" %8lu", imcasts);
509 printf(" %8lu", omcasts);
519 SLIST_ENTRY(iftot) chain;
520 char ift_name[IFNAMSIZ]; /* interface name */
521 u_long ift_ip; /* input packets */
522 u_long ift_ie; /* input errors */
523 u_long ift_id; /* input drops */
524 u_long ift_op; /* output packets */
525 u_long ift_oe; /* output errors */
526 u_long ift_co; /* collisions */
527 u_int ift_dr; /* drops */
528 u_long ift_ib; /* input bytes */
529 u_long ift_ob; /* output bytes */
532 u_char signalled; /* set if alarm goes off "early" */
535 * Print a running summary of interface statistics.
536 * Repeat display every interval1 seconds, showing statistics
537 * collected over that interval. Assumes that interval1 is non-zero.
538 * First line printed at top of screen is always cumulative.
539 * XXX - should be rewritten to use ifmib(4).
542 sidewaysintpr(int interval1, u_long off)
546 struct ifnethead ifnethead;
547 struct itimerval interval_it;
548 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
551 u_long interesting_off;
553 if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
555 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
557 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
558 printf("malloc failed\n");
561 memset(iftot, 0, sizeof(struct iftot));
565 for (off = firstifnet, ip = iftot; off;) {
568 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
570 strlcpy(name, ifnet.if_xname, sizeof(name));
571 if (interface && strcmp(name, interface) == 0) {
573 interesting_off = off;
575 snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
576 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
577 printf("malloc failed\n");
580 memset(ipn, 0, sizeof(struct iftot));
581 SLIST_NEXT(ip, chain) = ipn;
583 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
585 if (interface && interesting == NULL)
586 errx(1, "%s: unknown interface", interface);
587 if ((total = malloc(sizeof(struct iftot))) == NULL) {
588 printf("malloc failed\n");
591 memset(total, 0, sizeof(struct iftot));
592 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
593 printf("malloc failed\n");
596 memset(sum, 0, sizeof(struct iftot));
598 (void)signal(SIGALRM, catchalarm);
600 interval_it.it_interval.tv_sec = interval1;
601 interval_it.it_interval.tv_usec = 0;
602 interval_it.it_value = interval_it.it_interval;
603 setitimer(ITIMER_REAL, &interval_it, NULL);
606 printf("%17s %14s %16s", "input",
607 interesting ? interesting->ift_name : "(Total)", "output");
609 printf("%10s %5s %5s %10s %10s %5s %10s %5s",
610 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
613 printf(" %5.5s", "drops");
618 if (interesting != NULL) {
620 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
625 show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
626 show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
627 show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
628 show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
629 show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
630 show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
631 show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
632 show_stat("NRSlu", 5,
633 ifnet.if_collisions - ip->ift_co, 1);
636 ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
638 ip->ift_ip = ifnet.if_ipackets;
639 ip->ift_ie = ifnet.if_ierrors;
640 ip->ift_id = ifnet.if_iqdrops;
641 ip->ift_ib = ifnet.if_ibytes;
642 ip->ift_op = ifnet.if_opackets;
643 ip->ift_oe = ifnet.if_oerrors;
644 ip->ift_ob = ifnet.if_obytes;
645 ip->ift_co = ifnet.if_collisions;
646 ip->ift_dr = ifnet.if_snd.ifq_drops;
657 for (off = firstifnet, ip = iftot;
658 off && SLIST_NEXT(ip, chain) != NULL;
659 ip = SLIST_NEXT(ip, chain)) {
660 if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
664 sum->ift_ip += ifnet.if_ipackets;
665 sum->ift_ie += ifnet.if_ierrors;
666 sum->ift_id += ifnet.if_iqdrops;
667 sum->ift_ib += ifnet.if_ibytes;
668 sum->ift_op += ifnet.if_opackets;
669 sum->ift_oe += ifnet.if_oerrors;
670 sum->ift_ob += ifnet.if_obytes;
671 sum->ift_co += ifnet.if_collisions;
672 sum->ift_dr += ifnet.if_snd.ifq_drops;
673 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
676 show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
677 show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
678 show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
679 show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
680 show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
681 show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
682 show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
683 show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
686 sum->ift_dr - total->ift_dr, 1);
693 if ((noutputs != 0) && (--noutputs == 0))
695 oldmask = sigblock(sigmask(SIGALRM));
710 * Set a flag to indicate that a signal from the periodic itimer has been
714 catchalarm(int signo __unused)