xref: /freebsd/usr.bin/netstat/inet.c (revision f5d34df525d0214cd98141d75e2ca7d89887d44c)
165475bc8SDavid E. O'Brien /*-
205ddff6eSPeter Wemm  * Copyright (c) 1983, 1988, 1993, 1995
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
346cc6f122SPhilippe Charnier #if 0
359b50d902SRodney W. Grimes #ifndef lint
3605ddff6eSPeter Wemm static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
379b50d902SRodney W. Grimes #endif /* not lint */
386cc6f122SPhilippe Charnier #endif
396cc6f122SPhilippe Charnier 
406cc6f122SPhilippe Charnier #include <sys/cdefs.h>
416cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <sys/param.h>
44d8d89152SDavid Greenman #include <sys/queue.h>
45feda1a43SJohn Baldwin #include <sys/domain.h>
46f35a2092SMaksim Yevmenkin #include <sys/protosw.h>
479b50d902SRodney W. Grimes #include <sys/socket.h>
489b50d902SRodney W. Grimes #include <sys/socketvar.h>
494e00c309SGarrett Wollman #include <sys/sysctl.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <net/route.h>
5254fc657dSGeorge V. Neville-Neil #include <net/if_arp.h>
539b50d902SRodney W. Grimes #include <netinet/in.h>
549b50d902SRodney W. Grimes #include <netinet/in_systm.h>
559b50d902SRodney W. Grimes #include <netinet/ip.h>
56a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
57cfa1ca9dSYoshinobu Inoue #ifdef INET6
58cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
59cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
609b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
619b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
629b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
639b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
649b50d902SRodney W. Grimes #include <netinet/ip_var.h>
65c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
669b50d902SRodney W. Grimes #include <netinet/tcp.h>
679b50d902SRodney W. Grimes #include <netinet/tcpip.h>
689b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
699b50d902SRodney W. Grimes #define	TCPSTATES
709b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
719b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
729b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
739b50d902SRodney W. Grimes #include <netinet/tcp_debug.h>
749b50d902SRodney W. Grimes #include <netinet/udp.h>
759b50d902SRodney W. Grimes #include <netinet/udp_var.h>
769b50d902SRodney W. Grimes 
779b50d902SRodney W. Grimes #include <arpa/inet.h>
784f81ef50SGarrett Wollman #include <err.h>
794f81ef50SGarrett Wollman #include <errno.h>
80d121b556SBrian Somers #include <libutil.h>
819b50d902SRodney W. Grimes #include <netdb.h>
827b95a1ebSYaroslav Tykhiy #include <stdint.h>
839b50d902SRodney W. Grimes #include <stdio.h>
844f81ef50SGarrett Wollman #include <stdlib.h>
859b50d902SRodney W. Grimes #include <string.h>
869b50d902SRodney W. Grimes #include <unistd.h>
879b50d902SRodney W. Grimes #include "netstat.h"
889b50d902SRodney W. Grimes 
895e051718SAssar Westerlund char	*inetname(struct in_addr *);
90a01e3379SDavid Malone void	inetprint(struct in_addr *, int, const char *, int);
91cfa1ca9dSYoshinobu Inoue #ifdef INET6
92cfa1ca9dSYoshinobu Inoue static int udp_done, tcp_done;
93cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
949b50d902SRodney W. Grimes 
95feda1a43SJohn Baldwin static int
96feda1a43SJohn Baldwin pcblist_sysctl(int proto, char **bufp, int istcp)
97feda1a43SJohn Baldwin {
98feda1a43SJohn Baldwin 	const char *mibvar;
99feda1a43SJohn Baldwin 	char *buf;
100feda1a43SJohn Baldwin 	size_t len;
101feda1a43SJohn Baldwin 
102feda1a43SJohn Baldwin 	switch (proto) {
103feda1a43SJohn Baldwin 	case IPPROTO_TCP:
104feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
105feda1a43SJohn Baldwin 		break;
106feda1a43SJohn Baldwin 	case IPPROTO_UDP:
107feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
108feda1a43SJohn Baldwin 		break;
109feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
110feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
111feda1a43SJohn Baldwin 		break;
112feda1a43SJohn Baldwin 	default:
113feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
114feda1a43SJohn Baldwin 		break;
115feda1a43SJohn Baldwin 	}
116feda1a43SJohn Baldwin 
117feda1a43SJohn Baldwin 	len = 0;
118feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
119feda1a43SJohn Baldwin 		if (errno != ENOENT)
120feda1a43SJohn Baldwin 			warn("sysctl: %s", mibvar);
121feda1a43SJohn Baldwin 		return (0);
122feda1a43SJohn Baldwin 	}
123feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
124feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
125feda1a43SJohn Baldwin 		return (0);
126feda1a43SJohn Baldwin 	}
127feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
128feda1a43SJohn Baldwin 		warn("sysctl: %s", mibvar);
129feda1a43SJohn Baldwin 		free(buf);
130feda1a43SJohn Baldwin 		return (0);
131feda1a43SJohn Baldwin 	}
132feda1a43SJohn Baldwin 	*bufp = buf;
133feda1a43SJohn Baldwin 	return (1);
134feda1a43SJohn Baldwin }
135feda1a43SJohn Baldwin 
136feda1a43SJohn Baldwin /*
137feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
138feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
139feda1a43SJohn Baldwin  */
140feda1a43SJohn Baldwin static void
141feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
142feda1a43SJohn Baldwin {
143feda1a43SJohn Baldwin 	xsb->sb_cc = sb->sb_cc;
144feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
145feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
14649f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
14749f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
148feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
149feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
150feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
151feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
152feda1a43SJohn Baldwin }
153feda1a43SJohn Baldwin 
154feda1a43SJohn Baldwin int
155feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
156feda1a43SJohn Baldwin {
157feda1a43SJohn Baldwin 	struct protosw proto;
158feda1a43SJohn Baldwin 	struct domain domain;
159feda1a43SJohn Baldwin 
160feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
161feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
162feda1a43SJohn Baldwin 	xso->xso_so = so;
163feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
164feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
165feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
166feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
167feda1a43SJohn Baldwin 	xso->so_pcb = so->so_pcb;
168feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
169feda1a43SJohn Baldwin 		return (-1);
170feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
171feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
172feda1a43SJohn Baldwin 		return (-1);
173feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
174feda1a43SJohn Baldwin 	xso->so_qlen = so->so_qlen;
175feda1a43SJohn Baldwin 	xso->so_incqlen = so->so_incqlen;
176feda1a43SJohn Baldwin 	xso->so_qlimit = so->so_qlimit;
177feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
178feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
179feda1a43SJohn Baldwin 	xso->so_oobmark = so->so_oobmark;
180feda1a43SJohn Baldwin 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
181feda1a43SJohn Baldwin 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
182feda1a43SJohn Baldwin 	return (0);
183feda1a43SJohn Baldwin }
184feda1a43SJohn Baldwin 
185feda1a43SJohn Baldwin static int
186feda1a43SJohn Baldwin pcblist_kvm(u_long off, char **bufp, int istcp)
187feda1a43SJohn Baldwin {
188feda1a43SJohn Baldwin 	struct inpcbinfo pcbinfo;
189feda1a43SJohn Baldwin 	struct inpcbhead listhead;
190feda1a43SJohn Baldwin 	struct inpcb *inp;
191feda1a43SJohn Baldwin 	struct xinpcb xi;
192feda1a43SJohn Baldwin 	struct xinpgen xig;
193feda1a43SJohn Baldwin 	struct xtcpcb xt;
194feda1a43SJohn Baldwin 	struct socket so;
195feda1a43SJohn Baldwin 	struct xsocket *xso;
196feda1a43SJohn Baldwin 	char *buf, *p;
197feda1a43SJohn Baldwin 	size_t len;
198feda1a43SJohn Baldwin 
199feda1a43SJohn Baldwin 	if (off == 0)
200feda1a43SJohn Baldwin 		return (0);
201feda1a43SJohn Baldwin 	kread(off, &pcbinfo, sizeof(pcbinfo));
202feda1a43SJohn Baldwin 	if (istcp)
203feda1a43SJohn Baldwin 		len = 2 * sizeof(xig) +
204feda1a43SJohn Baldwin 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
205feda1a43SJohn Baldwin 		    sizeof(struct xtcpcb);
206feda1a43SJohn Baldwin 	else
207feda1a43SJohn Baldwin 		len = 2 * sizeof(xig) +
208feda1a43SJohn Baldwin 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
209feda1a43SJohn Baldwin 		    sizeof(struct xinpcb);
210feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
211feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
212feda1a43SJohn Baldwin 		return (0);
213feda1a43SJohn Baldwin 	}
214feda1a43SJohn Baldwin 	p = buf;
215feda1a43SJohn Baldwin 
216feda1a43SJohn Baldwin #define	COPYOUT(obj, size) do {						\
217feda1a43SJohn Baldwin 	if (len < (size)) {						\
218feda1a43SJohn Baldwin 		warnx("buffer size exceeded");				\
219feda1a43SJohn Baldwin 		goto fail;						\
220feda1a43SJohn Baldwin 	}								\
221feda1a43SJohn Baldwin 	bcopy((obj), p, (size));					\
222feda1a43SJohn Baldwin 	len -= (size);							\
223feda1a43SJohn Baldwin 	p += (size);							\
224feda1a43SJohn Baldwin } while (0)
225feda1a43SJohn Baldwin 
226feda1a43SJohn Baldwin #define	KREAD(off, buf, len) do {					\
227feda1a43SJohn Baldwin 	if (kread((uintptr_t)(off), (buf), (len)) != 0)			\
228feda1a43SJohn Baldwin 		goto fail;						\
229feda1a43SJohn Baldwin } while (0)
230feda1a43SJohn Baldwin 
231feda1a43SJohn Baldwin 	/* Write out header. */
232feda1a43SJohn Baldwin 	xig.xig_len = sizeof xig;
233feda1a43SJohn Baldwin 	xig.xig_count = pcbinfo.ipi_count;
234feda1a43SJohn Baldwin 	xig.xig_gen = pcbinfo.ipi_gencnt;
235feda1a43SJohn Baldwin 	xig.xig_sogen = 0;
236feda1a43SJohn Baldwin 	COPYOUT(&xig, sizeof xig);
237feda1a43SJohn Baldwin 
238feda1a43SJohn Baldwin 	/* Walk the PCB list. */
239feda1a43SJohn Baldwin 	xt.xt_len = sizeof xt;
240feda1a43SJohn Baldwin 	xi.xi_len = sizeof xi;
241feda1a43SJohn Baldwin 	if (istcp)
242feda1a43SJohn Baldwin 		xso = &xt.xt_socket;
243feda1a43SJohn Baldwin 	else
244feda1a43SJohn Baldwin 		xso = &xi.xi_socket;
245feda1a43SJohn Baldwin 	KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
246feda1a43SJohn Baldwin 	LIST_FOREACH(inp, &listhead, inp_list) {
247feda1a43SJohn Baldwin 		if (istcp) {
248feda1a43SJohn Baldwin 			KREAD(inp, &xt.xt_inp, sizeof(*inp));
249feda1a43SJohn Baldwin 			inp = &xt.xt_inp;
250feda1a43SJohn Baldwin 		} else {
251feda1a43SJohn Baldwin 			KREAD(inp, &xi.xi_inp, sizeof(*inp));
252feda1a43SJohn Baldwin 			inp = &xi.xi_inp;
253feda1a43SJohn Baldwin 		}
254feda1a43SJohn Baldwin 
255feda1a43SJohn Baldwin 		if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
256feda1a43SJohn Baldwin 			continue;
257feda1a43SJohn Baldwin 
258feda1a43SJohn Baldwin 		if (istcp) {
259feda1a43SJohn Baldwin 			if (inp->inp_ppcb == NULL)
260feda1a43SJohn Baldwin 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
261ad71fe3cSRobert Watson 			else if (inp->inp_flags & INP_TIMEWAIT) {
262feda1a43SJohn Baldwin 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
263feda1a43SJohn Baldwin 				xt.xt_tp.t_state = TCPS_TIME_WAIT;
264feda1a43SJohn Baldwin 			} else
265feda1a43SJohn Baldwin 				KREAD(inp->inp_ppcb, &xt.xt_tp,
266feda1a43SJohn Baldwin 				    sizeof xt.xt_tp);
267feda1a43SJohn Baldwin 		}
268feda1a43SJohn Baldwin 		if (inp->inp_socket) {
269feda1a43SJohn Baldwin 			KREAD(inp->inp_socket, &so, sizeof(so));
270feda1a43SJohn Baldwin 			if (sotoxsocket(&so, xso) != 0)
271feda1a43SJohn Baldwin 				goto fail;
272feda1a43SJohn Baldwin 		} else {
273feda1a43SJohn Baldwin 			bzero(xso, sizeof(*xso));
274feda1a43SJohn Baldwin 			if (istcp)
275feda1a43SJohn Baldwin 				xso->xso_protocol = IPPROTO_TCP;
276feda1a43SJohn Baldwin 		}
277feda1a43SJohn Baldwin 		if (istcp)
278feda1a43SJohn Baldwin 			COPYOUT(&xt, sizeof xt);
279feda1a43SJohn Baldwin 		else
280feda1a43SJohn Baldwin 			COPYOUT(&xi, sizeof xi);
281feda1a43SJohn Baldwin 	}
282feda1a43SJohn Baldwin 
283feda1a43SJohn Baldwin 	/* Reread the pcbinfo and write out the footer. */
284feda1a43SJohn Baldwin 	kread(off, &pcbinfo, sizeof(pcbinfo));
285feda1a43SJohn Baldwin 	xig.xig_count = pcbinfo.ipi_count;
286feda1a43SJohn Baldwin 	xig.xig_gen = pcbinfo.ipi_gencnt;
287feda1a43SJohn Baldwin 	COPYOUT(&xig, sizeof xig);
288feda1a43SJohn Baldwin 
289feda1a43SJohn Baldwin 	*bufp = buf;
290feda1a43SJohn Baldwin 	return (1);
291feda1a43SJohn Baldwin 
292feda1a43SJohn Baldwin fail:
293feda1a43SJohn Baldwin 	free(buf);
294feda1a43SJohn Baldwin 	return (0);
295feda1a43SJohn Baldwin #undef COPYOUT
296feda1a43SJohn Baldwin #undef KREAD
297feda1a43SJohn Baldwin }
298feda1a43SJohn Baldwin 
2999b50d902SRodney W. Grimes /*
3009b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
3019b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
3029b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
3039b50d902SRodney W. Grimes  * -a (all) flag is specified.
3049b50d902SRodney W. Grimes  */
3059b50d902SRodney W. Grimes void
306feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
3079b50d902SRodney W. Grimes {
3089b50d902SRodney W. Grimes 	int istcp;
3099b50d902SRodney W. Grimes 	static int first = 1;
3104f81ef50SGarrett Wollman 	char *buf;
311feda1a43SJohn Baldwin 	const char *vchar;
312a97a9922SJulian Elischer 	struct tcpcb *tp = NULL;
3134f81ef50SGarrett Wollman 	struct inpcb *inp;
3144f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
3154f81ef50SGarrett Wollman 	struct xsocket *so;
316b8614722SMike Silbersack 	struct xtcp_timer *timer;
3179b50d902SRodney W. Grimes 
3184f81ef50SGarrett Wollman 	istcp = 0;
3194f81ef50SGarrett Wollman 	switch (proto) {
3204f81ef50SGarrett Wollman 	case IPPROTO_TCP:
321cfa1ca9dSYoshinobu Inoue #ifdef INET6
322cfa1ca9dSYoshinobu Inoue 		if (tcp_done != 0)
323cfa1ca9dSYoshinobu Inoue 			return;
324cfa1ca9dSYoshinobu Inoue 		else
325cfa1ca9dSYoshinobu Inoue 			tcp_done = 1;
326cfa1ca9dSYoshinobu Inoue #endif
3274f81ef50SGarrett Wollman 		istcp = 1;
3284f81ef50SGarrett Wollman 		break;
3294f81ef50SGarrett Wollman 	case IPPROTO_UDP:
330cfa1ca9dSYoshinobu Inoue #ifdef INET6
331cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
332cfa1ca9dSYoshinobu Inoue 			return;
333cfa1ca9dSYoshinobu Inoue 		else
334cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
335cfa1ca9dSYoshinobu Inoue #endif
3364f81ef50SGarrett Wollman 		break;
3374f81ef50SGarrett Wollman 	}
338feda1a43SJohn Baldwin 	if (live) {
339feda1a43SJohn Baldwin 		if (!pcblist_sysctl(proto, &buf, istcp))
3409b50d902SRodney W. Grimes 			return;
341feda1a43SJohn Baldwin 	} else {
342feda1a43SJohn Baldwin 		if (!pcblist_kvm(off, &buf, istcp))
3434f81ef50SGarrett Wollman 			return;
3444f81ef50SGarrett Wollman 	}
3454f81ef50SGarrett Wollman 
3464f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
3474f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
3484f81ef50SGarrett Wollman 	     xig->xig_len > sizeof(struct xinpgen);
3494f81ef50SGarrett Wollman 	     xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
3509b50d902SRodney W. Grimes 		if (istcp) {
351b8614722SMike Silbersack 			timer = &((struct xtcpcb *)xig)->xt_timer;
3524f81ef50SGarrett Wollman 			tp = &((struct xtcpcb *)xig)->xt_tp;
3534f81ef50SGarrett Wollman 			inp = &((struct xtcpcb *)xig)->xt_inp;
3544f81ef50SGarrett Wollman 			so = &((struct xtcpcb *)xig)->xt_socket;
3554f81ef50SGarrett Wollman 		} else {
3564f81ef50SGarrett Wollman 			inp = &((struct xinpcb *)xig)->xi_inp;
3574f81ef50SGarrett Wollman 			so = &((struct xinpcb *)xig)->xi_socket;
358aae09141SMike Silbersack 			timer = NULL;
3599b50d902SRodney W. Grimes 		}
3604f81ef50SGarrett Wollman 
3614f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
362feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
3634f81ef50SGarrett Wollman 			continue;
3644f81ef50SGarrett Wollman 
3654f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
3664f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
3674f81ef50SGarrett Wollman 			continue;
3684f81ef50SGarrett Wollman 
369a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
370cfa1ca9dSYoshinobu Inoue #ifdef INET6
371a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
372cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
373a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
374cfa1ca9dSYoshinobu Inoue #ifdef INET6
3753feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
376cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
377cfa1ca9dSYoshinobu Inoue 			))
378cfa1ca9dSYoshinobu Inoue 		    )
379cfa1ca9dSYoshinobu Inoue 			continue;
380cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
381cfa1ca9dSYoshinobu Inoue 		    (
3822b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
3832b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
384cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
385cfa1ca9dSYoshinobu Inoue #ifdef INET6
386a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
387cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
388cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
389a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
390cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
391cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
392cfa1ca9dSYoshinobu Inoue #ifdef INET6
393cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
394cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
395cfa1ca9dSYoshinobu Inoue #endif
396cfa1ca9dSYoshinobu Inoue 			  ))
397cfa1ca9dSYoshinobu Inoue 		     ))
3984f81ef50SGarrett Wollman 			continue;
3994f81ef50SGarrett Wollman 
4009b50d902SRodney W. Grimes 		if (first) {
401ac55add0SGuido van Rooij 			if (!Lflag) {
4029b50d902SRodney W. Grimes 				printf("Active Internet connections");
4039b50d902SRodney W. Grimes 				if (aflag)
4049b50d902SRodney W. Grimes 					printf(" (including servers)");
405ac55add0SGuido van Rooij 			} else
406ac55add0SGuido van Rooij 				printf(
407ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
4089b50d902SRodney W. Grimes 			putchar('\n');
4099b50d902SRodney W. Grimes 			if (Aflag)
410612d2129SAndre Oppermann 				printf("%-8.8s ", "Tcpcb");
411ac55add0SGuido van Rooij 			if (Lflag)
412fb5d0fbdSRuslan Ermilov 				printf("%-5.5s %-14.14s %-22.22s\n",
413fb5d0fbdSRuslan Ermilov 				    "Proto", "Listen", "Local Address");
414*f5d34df5SGeorge V. Neville-Neil 			if (Tflag)
415080b7f49SDag-Erling Smørgrav 				printf((Aflag && !Wflag) ?
416*f5d34df5SGeorge V. Neville-Neil 			    "%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %s\n" :
417*f5d34df5SGeorge V. Neville-Neil 			    "%-5.5s %-6.6s %-6.6s %-6.6s %-22.22s %s\n",
418*f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
41949f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
420b8614722SMike Silbersack 			if (xflag) {
421b8614722SMike Silbersack 				printf("%-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s ",
42294f138feSGeorge V. Neville-Neil 				       "R-MBUF", "S-MBUF", "R-CLUS",
42394f138feSGeorge V. Neville-Neil 				       "S-CLUS", "R-HIWA", "S-HIWA",
42494f138feSGeorge V. Neville-Neil 				       "R-LOWA", "S-LOWA", "R-BCNT",
425b8614722SMike Silbersack 				       "S-BCNT", "R-BMAX", "S-BMAX");
426b8614722SMike Silbersack 				printf("%7.7s %7.7s %7.7s %7.7s %7.7s %7.7s %s\n",
427b8614722SMike Silbersack 				       "rexmt", "persist", "keep",
428b8614722SMike Silbersack 				       "2msl", "delack", "rcvtime",
429ac55add0SGuido van Rooij 				       "(state)");
43094f138feSGeorge V. Neville-Neil 			}
431*f5d34df5SGeorge V. Neville-Neil 			if (!xflag && !Tflag)
432*f5d34df5SGeorge V. Neville-Neil 				printf((Aflag && !Wflag) ?
433*f5d34df5SGeorge V. Neville-Neil 				       "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s" :
434*f5d34df5SGeorge V. Neville-Neil 				       "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s",
435*f5d34df5SGeorge V. Neville-Neil 				       "Proto", "Recv-Q", "Send-Q",
436*f5d34df5SGeorge V. Neville-Neil 				       "Local Address", "Foreign Address");
437*f5d34df5SGeorge V. Neville-Neil 
4389b50d902SRodney W. Grimes 			first = 0;
4399b50d902SRodney W. Grimes 		}
440fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
441fb5d0fbdSRuslan Ermilov 			continue;
442e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
443e4ec3989SDag-Erling Smørgrav 			if (istcp)
444e4ec3989SDag-Erling Smørgrav 				printf("%8lx ", (u_long)inp->inp_ppcb);
445e4ec3989SDag-Erling Smørgrav 			else
4464f81ef50SGarrett Wollman 				printf("%8lx ", (u_long)so->so_pcb);
447e4ec3989SDag-Erling Smørgrav 		}
448cfa1ca9dSYoshinobu Inoue #ifdef INET6
449fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
4503feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4513feeb332SDavid E. O'Brien 			    "46" : "6 ";
452fc60ab7bSYoshinobu Inoue 		else
453cfa1ca9dSYoshinobu Inoue #endif
4543feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4553feeb332SDavid E. O'Brien 		    "4 " : "  ";
456fb5d0fbdSRuslan Ermilov 		printf("%-3.3s%-2.2s ", name, vchar);
457fb5d0fbdSRuslan Ermilov 		if (Lflag) {
458a01e3379SDavid Malone 			char buf1[15];
459fc60ab7bSYoshinobu Inoue 
460a01e3379SDavid Malone 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
461fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
462a01e3379SDavid Malone 			printf("%-14.14s ", buf1);
463*f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
464*f5d34df5SGeorge V. Neville-Neil 			if (istcp)
465*f5d34df5SGeorge V. Neville-Neil 				printf("%6u %6u %6u ", tp->t_sndrexmitpack,
466*f5d34df5SGeorge V. Neville-Neil 				       tp->t_rcvoopack, tp->t_sndzerowin);
467fb5d0fbdSRuslan Ermilov 		} else {
468dd335a15SDavid E. O'Brien 			printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
469fc60ab7bSYoshinobu Inoue 		}
47065ea0024SAssar Westerlund 		if (numeric_port) {
471cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4724f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4738d612dd2SPoul-Henning Kamp 				    name, 1);
474ac55add0SGuido van Rooij 				if (!Lflag)
475ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
476ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 1);
477cfa1ca9dSYoshinobu Inoue 			}
478cfa1ca9dSYoshinobu Inoue #ifdef INET6
479cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
480cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
481cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
482ac55add0SGuido van Rooij 				if (!Lflag)
483cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
484cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
485cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
486cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4874f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
488cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4894f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4908d612dd2SPoul-Henning Kamp 				    name, 1);
491ac55add0SGuido van Rooij 				if (!Lflag)
492ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
493ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 0);
494cfa1ca9dSYoshinobu Inoue 			}
495cfa1ca9dSYoshinobu Inoue #ifdef INET6
496cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
497cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
498cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
499ac55add0SGuido van Rooij 				if (!Lflag)
500cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
501cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
502cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
503cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5048d612dd2SPoul-Henning Kamp 		} else {
505cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
5064f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
5078d612dd2SPoul-Henning Kamp 				    name, 0);
508ac55add0SGuido van Rooij 				if (!Lflag)
509ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
510ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
5113feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
512cfa1ca9dSYoshinobu Inoue 			}
513cfa1ca9dSYoshinobu Inoue #ifdef INET6
514cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
515cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
516cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
517ac55add0SGuido van Rooij 				if (!Lflag)
518cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
519cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
5203feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
521cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
522cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5238d612dd2SPoul-Henning Kamp 		}
52449f287f8SGeorge V. Neville-Neil 		if (xflag) {
52549f287f8SGeorge V. Neville-Neil 			if (Lflag)
52649f287f8SGeorge V. Neville-Neil 				printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
52749f287f8SGeorge V. Neville-Neil 				       " ",
52849f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
52949f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
53049f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
53149f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
53249f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
53349f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
534b8614722SMike Silbersack 			else {
53549f287f8SGeorge V. Neville-Neil 				printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
53649f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
53749f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
53849f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
53949f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
54049f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
54149f287f8SGeorge V. Neville-Neil 				       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
542aae09141SMike Silbersack 				if (timer != NULL)
543b8614722SMike Silbersack 					printf("%4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d ",
544b8614722SMike Silbersack 					    timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10,
545b8614722SMike Silbersack 					    timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10,
546b8614722SMike Silbersack 					    timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10,
547b8614722SMike Silbersack 					    timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10,
548b8614722SMike Silbersack 					    timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10,
549b8614722SMike Silbersack 					    timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10);
550b8614722SMike Silbersack 			}
55149f287f8SGeorge V. Neville-Neil 		}
552*f5d34df5SGeorge V. Neville-Neil 		if (istcp && !Lflag && !xflag && !Tflag) {
5534f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5544f81ef50SGarrett Wollman 				printf("%d", tp->t_state);
5559a94a597SGarrett Wollman 			else {
5564f81ef50SGarrett Wollman 				printf("%s", tcpstates[tp->t_state]);
557513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5589a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
5594f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
5609a94a597SGarrett Wollman 					putchar('*');
561513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5629a94a597SGarrett Wollman 			}
5639b50d902SRodney W. Grimes 		}
5649b50d902SRodney W. Grimes 		putchar('\n');
5659b50d902SRodney W. Grimes 	}
5664f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5674f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
5684f81ef50SGarrett Wollman 			printf("Some %s sockets may have been deleted.\n",
5694f81ef50SGarrett Wollman 			    name);
5704f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
5714f81ef50SGarrett Wollman 			printf("Some %s sockets may have been created.\n",
5724f81ef50SGarrett Wollman 			    name);
5734f81ef50SGarrett Wollman 		} else {
5743feeb332SDavid E. O'Brien 			printf(
5753feeb332SDavid E. O'Brien 	"Some %s sockets may have been created or deleted.\n",
5764f81ef50SGarrett Wollman 			    name);
5774f81ef50SGarrett Wollman 		}
5784f81ef50SGarrett Wollman 	}
5794f81ef50SGarrett Wollman 	free(buf);
5809b50d902SRodney W. Grimes }
5819b50d902SRodney W. Grimes 
5829b50d902SRodney W. Grimes /*
5839b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5849b50d902SRodney W. Grimes  */
5859b50d902SRodney W. Grimes void
586feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5879b50d902SRodney W. Grimes {
588c73d99b5SRuslan Ermilov 	struct tcpstat tcpstat, zerostat;
5894f81ef50SGarrett Wollman 	size_t len = sizeof tcpstat;
5909b50d902SRodney W. Grimes 
591cfa1ca9dSYoshinobu Inoue #ifdef INET6
592cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
593cfa1ca9dSYoshinobu Inoue 		return;
594cfa1ca9dSYoshinobu Inoue 	else
595cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
596cfa1ca9dSYoshinobu Inoue #endif
597cfa1ca9dSYoshinobu Inoue 
598feda1a43SJohn Baldwin 	if (live) {
599feda1a43SJohn Baldwin 		if (zflag)
600feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
601feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
602feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
603feda1a43SJohn Baldwin 			warn("sysctl: net.inet.tcp.stats");
604feda1a43SJohn Baldwin 			return;
605feda1a43SJohn Baldwin 		}
606feda1a43SJohn Baldwin 	} else
607feda1a43SJohn Baldwin 		kread(off, &tcpstat, len);
608feda1a43SJohn Baldwin 
6099b50d902SRodney W. Grimes 	printf ("%s:\n", name);
6109b50d902SRodney W. Grimes 
6119b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1) \
6129b50d902SRodney W. Grimes     printf(m, tcpstat.f, plural(tcpstat.f))
61322694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1) \
61422694ebaSBruce Evans     printf(m, tcpstat.f)
6159b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
6169b50d902SRodney W. Grimes     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
61722694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
61822694ebaSBruce Evans     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
6199b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
6200decbf9dSRuslan Ermilov     printf(m, tcpstat.f, pluralies(tcpstat.f))
6219b50d902SRodney W. Grimes 
622e1fb4daaSPaul Traina 	p(tcps_sndtotal, "\t%lu packet%s sent\n");
6233feeb332SDavid E. O'Brien 	p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
6249b50d902SRodney W. Grimes 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
625e1fb4daaSPaul Traina 	    "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
626d65bf08aSMatthew Dillon 	p(tcps_sndrexmitbad,
627d65bf08aSMatthew Dillon 	    "\t\t%lu data packet%s unnecessarily retransmitted\n");
628e1fb4daaSPaul Traina 	p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
62922694ebaSBruce Evans 	p2a(tcps_sndacks, tcps_delack,
630e1fb4daaSPaul Traina 	    "\t\t%lu ack-only packet%s (%lu delayed)\n");
631e1fb4daaSPaul Traina 	p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
632e1fb4daaSPaul Traina 	p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
633e1fb4daaSPaul Traina 	p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
634e1fb4daaSPaul Traina 	p(tcps_sndctrl, "\t\t%lu control packet%s\n");
635e1fb4daaSPaul Traina 	p(tcps_rcvtotal, "\t%lu packet%s received\n");
6363feeb332SDavid E. O'Brien 	p2(tcps_rcvackpack, tcps_rcvackbyte,
6373feeb332SDavid E. O'Brien 	    "\t\t%lu ack%s (for %lu byte%s)\n");
638e1fb4daaSPaul Traina 	p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
639e1fb4daaSPaul Traina 	p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
6409b50d902SRodney W. Grimes 	p2(tcps_rcvpack, tcps_rcvbyte,
641e1fb4daaSPaul Traina 	    "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
6429b50d902SRodney W. Grimes 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
643e1fb4daaSPaul Traina 	    "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
644e1fb4daaSPaul Traina 	p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
6459b50d902SRodney W. Grimes 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
646e1fb4daaSPaul Traina 	    "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
6479b50d902SRodney W. Grimes 	p2(tcps_rcvoopack, tcps_rcvoobyte,
648e1fb4daaSPaul Traina 	    "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
6499b50d902SRodney W. Grimes 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
650e1fb4daaSPaul Traina 	    "\t\t%lu packet%s (%lu byte%s) of data after window\n");
651e1fb4daaSPaul Traina 	p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
652e1fb4daaSPaul Traina 	p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
653e1fb4daaSPaul Traina 	p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
654e1fb4daaSPaul Traina 	p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
655e1fb4daaSPaul Traina 	p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
65622694ebaSBruce Evans 	p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
65760e15662SWojciech A. Koszek 	p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
658e1fb4daaSPaul Traina 	p(tcps_connattempt, "\t%lu connection request%s\n");
659e1fb4daaSPaul Traina 	p(tcps_accepts, "\t%lu connection accept%s\n");
660e1fb4daaSPaul Traina 	p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
661e1fb4daaSPaul Traina 	p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
66201d3e1c0SRuslan Ermilov 	p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
663e1fb4daaSPaul Traina 	p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
6649b50d902SRodney W. Grimes 	p2(tcps_closed, tcps_drops,
665e1fb4daaSPaul Traina 	    "\t%lu connection%s closed (including %lu drop%s)\n");
666e1fb4daaSPaul Traina 	p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
667861b1828SGarrett Wollman 	p(tcps_cachedrttvar,
668e1fb4daaSPaul Traina 	    "\t\t%lu connection%s updated cached RTT variance on close\n");
669861b1828SGarrett Wollman 	p(tcps_cachedssthresh,
670e1fb4daaSPaul Traina 	    "\t\t%lu connection%s updated cached ssthresh on close\n");
671e1fb4daaSPaul Traina 	p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
6729b50d902SRodney W. Grimes 	p2(tcps_rttupdated, tcps_segstimed,
673e1fb4daaSPaul Traina 	    "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
674e1fb4daaSPaul Traina 	p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
675e1fb4daaSPaul Traina 	p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
676e1fb4daaSPaul Traina 	p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
677e1fb4daaSPaul Traina 	p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
6783feeb332SDavid E. O'Brien 	p(tcps_finwait2_drops,
6793feeb332SDavid E. O'Brien 	    "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
680e1fb4daaSPaul Traina 	p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
681e1fb4daaSPaul Traina 	p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
682e1fb4daaSPaul Traina 	p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
683e1fb4daaSPaul Traina 	p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
684e1fb4daaSPaul Traina 	p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
68560a31b3aSJonathan Lemon 
6860decbf9dSRuslan Ermilov 	p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
687a01e3379SDavid Malone 	p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
688a01e3379SDavid Malone 	p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
689a01e3379SDavid Malone 	p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
690a01e3379SDavid Malone 	p1a(tcps_sc_completed, "\t\t%lu completed\n");
691a01e3379SDavid Malone 	p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
692a01e3379SDavid Malone 	p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
693a01e3379SDavid Malone 	p1a(tcps_sc_reset, "\t\t%lu reset\n");
694a01e3379SDavid Malone 	p1a(tcps_sc_stale, "\t\t%lu stale\n");
695a01e3379SDavid Malone 	p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
696a01e3379SDavid Malone 	p1a(tcps_sc_badack, "\t\t%lu badack\n");
697a01e3379SDavid Malone 	p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
698a01e3379SDavid Malone 	p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
699a01e3379SDavid Malone 	p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
700a01e3379SDavid Malone 	p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
701b6101dafSPaul Saab 
702699ef999SRuslan Ermilov 	p(tcps_hc_added, "\t%lu hostcache entrie%s added\n");
703699ef999SRuslan Ermilov 	p1a(tcps_hc_bucketoverflow, "\t\t%lu bucket overflow\n");
704699ef999SRuslan Ermilov 
705b6101dafSPaul Saab 	p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
706b6101dafSPaul Saab 	p(tcps_sack_rexmits,
707b6101dafSPaul Saab 	    "\t%lu segment rexmit%s in SACK recovery episodes\n");
708b6101dafSPaul Saab 	p(tcps_sack_rexmit_bytes,
709b6101dafSPaul Saab 	    "\t%lu byte rexmit%s in SACK recovery episodes\n");
710b6101dafSPaul Saab 	p(tcps_sack_rcv_blocks,
711b6101dafSPaul Saab 	    "\t%lu SACK option%s (SACK blocks) received\n");
712b6101dafSPaul Saab 	p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
713e891d82bSPaul Saab 	p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
714b6101dafSPaul Saab 
7154816ba93SRui Paulo 	p(tcps_ecn_ce, "\t%lu packet%s with ECN CE bit set\n");
7164816ba93SRui Paulo 	p(tcps_ecn_ect0, "\t%lu packet%s with ECN ECT(0) bit set\n");
7174816ba93SRui Paulo 	p(tcps_ecn_ect1, "\t%lu packet%s with ECN ECT(1) bit set\n");
7184816ba93SRui Paulo 	p(tcps_ecn_shs, "\t%lu successful ECN handshake%s\n");
7194816ba93SRui Paulo 	p(tcps_ecn_rcwnd, "\t%lu time%s ECN reduced the congestion window\n");
7209b50d902SRodney W. Grimes #undef p
72122694ebaSBruce Evans #undef p1a
7229b50d902SRodney W. Grimes #undef p2
72322694ebaSBruce Evans #undef p2a
7249b50d902SRodney W. Grimes #undef p3
7259b50d902SRodney W. Grimes }
7269b50d902SRodney W. Grimes 
7279b50d902SRodney W. Grimes /*
7289b50d902SRodney W. Grimes  * Dump UDP statistics structure.
7299b50d902SRodney W. Grimes  */
7309b50d902SRodney W. Grimes void
731feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
7329b50d902SRodney W. Grimes {
733c73d99b5SRuslan Ermilov 	struct udpstat udpstat, zerostat;
7344f81ef50SGarrett Wollman 	size_t len = sizeof udpstat;
7359b50d902SRodney W. Grimes 	u_long delivered;
7369b50d902SRodney W. Grimes 
737cfa1ca9dSYoshinobu Inoue #ifdef INET6
738cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
739cfa1ca9dSYoshinobu Inoue 		return;
740cfa1ca9dSYoshinobu Inoue 	else
741cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
742cfa1ca9dSYoshinobu Inoue #endif
743cfa1ca9dSYoshinobu Inoue 
744feda1a43SJohn Baldwin 	if (live) {
745feda1a43SJohn Baldwin 		if (zflag)
746feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
747feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
748feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
749feda1a43SJohn Baldwin 			warn("sysctl: net.inet.udp.stats");
750feda1a43SJohn Baldwin 			return;
751feda1a43SJohn Baldwin 		}
752feda1a43SJohn Baldwin 	} else
753feda1a43SJohn Baldwin 		kread(off, &udpstat, len);
754feda1a43SJohn Baldwin 
7559b50d902SRodney W. Grimes 	printf("%s:\n", name);
7569b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
7579b50d902SRodney W. Grimes     printf(m, udpstat.f, plural(udpstat.f))
75822694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
75922694ebaSBruce Evans     printf(m, udpstat.f)
7607d56c0eeSAlexander Langer 	p(udps_ipackets, "\t%lu datagram%s received\n");
76122694ebaSBruce Evans 	p1a(udps_hdrops, "\t%lu with incomplete header\n");
76222694ebaSBruce Evans 	p1a(udps_badlen, "\t%lu with bad data length field\n");
76322694ebaSBruce Evans 	p1a(udps_badsum, "\t%lu with bad checksum\n");
764fb9aaba0SRuslan Ermilov 	p1a(udps_nosum, "\t%lu with no checksum\n");
76522694ebaSBruce Evans 	p1a(udps_noport, "\t%lu dropped due to no socket\n");
76622694ebaSBruce Evans 	p(udps_noportbcast,
76771498f30SBruce M Simpson 	    "\t%lu broadcast/multicast datagram%s undelivered\n");
76822694ebaSBruce Evans 	p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
76922694ebaSBruce Evans 	p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
7709b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
7719b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
7729b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
7739b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
7749b50d902SRodney W. Grimes 		    udpstat.udps_noport -
7759b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
7769b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
7779b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
7787d56c0eeSAlexander Langer 		printf("\t%lu delivered\n", delivered);
7797d56c0eeSAlexander Langer 	p(udps_opackets, "\t%lu datagram%s output\n");
78071498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
78171498f30SBruce M Simpson 	p(udps_filtermcast,
78271498f30SBruce M Simpson 	    "\t%lu time%s multicast source filter matched\n");
7839b50d902SRodney W. Grimes #undef p
78422694ebaSBruce Evans #undef p1a
7859b50d902SRodney W. Grimes }
7869b50d902SRodney W. Grimes 
7879b50d902SRodney W. Grimes /*
788a9771948SGleb Smirnoff  * Dump CARP statistics structure.
789a9771948SGleb Smirnoff  */
790a9771948SGleb Smirnoff void
791feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
792a9771948SGleb Smirnoff {
793a9771948SGleb Smirnoff 	struct carpstats carpstat, zerostat;
794a9771948SGleb Smirnoff 	size_t len = sizeof(struct carpstats);
795a9771948SGleb Smirnoff 
796feda1a43SJohn Baldwin 	if (live) {
797a9771948SGleb Smirnoff 		if (zflag)
798a9771948SGleb Smirnoff 			memset(&zerostat, 0, len);
799a9771948SGleb Smirnoff 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
800a9771948SGleb Smirnoff 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
80145125e14SRuslan Ermilov 			if (errno != ENOENT)
802a9771948SGleb Smirnoff 				warn("sysctl: net.inet.carp.stats");
803a9771948SGleb Smirnoff 			return;
804a9771948SGleb Smirnoff 		}
805feda1a43SJohn Baldwin 	} else {
806feda1a43SJohn Baldwin 		if (off == 0)
807feda1a43SJohn Baldwin 			return;
808feda1a43SJohn Baldwin 		kread(off, &carpstat, len);
809feda1a43SJohn Baldwin 	}
810a9771948SGleb Smirnoff 
811a9771948SGleb Smirnoff 	printf("%s:\n", name);
812a9771948SGleb Smirnoff 
813a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
8147b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
815a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
8167b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f)
817a9771948SGleb Smirnoff 
8187b95a1ebSYaroslav Tykhiy 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
8197b95a1ebSYaroslav Tykhiy 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
8207b95a1ebSYaroslav Tykhiy 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
8217b95a1ebSYaroslav Tykhiy 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
8227b95a1ebSYaroslav Tykhiy 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
8237b95a1ebSYaroslav Tykhiy 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
8247b95a1ebSYaroslav Tykhiy 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
8257b95a1ebSYaroslav Tykhiy 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
8267b95a1ebSYaroslav Tykhiy 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
8277b95a1ebSYaroslav Tykhiy 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
8287b95a1ebSYaroslav Tykhiy 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
8297b95a1ebSYaroslav Tykhiy 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
8307b95a1ebSYaroslav Tykhiy 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
831a9771948SGleb Smirnoff #if notyet
832a9771948SGleb Smirnoff 	p(carps_ostates, "\t\t%s state update%s sent\n");
833a9771948SGleb Smirnoff #endif
834a9771948SGleb Smirnoff #undef p
835a9771948SGleb Smirnoff #undef p2
836a9771948SGleb Smirnoff }
837a9771948SGleb Smirnoff 
838a9771948SGleb Smirnoff /*
8399b50d902SRodney W. Grimes  * Dump IP statistics structure.
8409b50d902SRodney W. Grimes  */
8419b50d902SRodney W. Grimes void
842feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8439b50d902SRodney W. Grimes {
844c73d99b5SRuslan Ermilov 	struct ipstat ipstat, zerostat;
8454f81ef50SGarrett Wollman 	size_t len = sizeof ipstat;
8469b50d902SRodney W. Grimes 
847feda1a43SJohn Baldwin 	if (live) {
848c73d99b5SRuslan Ermilov 		if (zflag)
849c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
850c73d99b5SRuslan Ermilov 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
851c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
8524f81ef50SGarrett Wollman 			warn("sysctl: net.inet.ip.stats");
8539b50d902SRodney W. Grimes 			return;
8544f81ef50SGarrett Wollman 		}
855feda1a43SJohn Baldwin 	} else
856feda1a43SJohn Baldwin 		kread(off, &ipstat, len);
8574f81ef50SGarrett Wollman 
8589b50d902SRodney W. Grimes 	printf("%s:\n", name);
8599b50d902SRodney W. Grimes 
8609b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
8619b50d902SRodney W. Grimes     printf(m, ipstat.f, plural(ipstat.f))
86222694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
86322694ebaSBruce Evans     printf(m, ipstat.f)
8649b50d902SRodney W. Grimes 
8657d56c0eeSAlexander Langer 	p(ips_total, "\t%lu total packet%s received\n");
8667d56c0eeSAlexander Langer 	p(ips_badsum, "\t%lu bad header checksum%s\n");
86722694ebaSBruce Evans 	p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
86822694ebaSBruce Evans 	p1a(ips_tooshort, "\t%lu with data size < data length\n");
869cfa1ca9dSYoshinobu Inoue 	p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
87022694ebaSBruce Evans 	p1a(ips_badhlen, "\t%lu with header length < data size\n");
87122694ebaSBruce Evans 	p1a(ips_badlen, "\t%lu with data length < header length\n");
87222694ebaSBruce Evans 	p1a(ips_badoptions, "\t%lu with bad options\n");
87322694ebaSBruce Evans 	p1a(ips_badvers, "\t%lu with incorrect version number\n");
8747d56c0eeSAlexander Langer 	p(ips_fragments, "\t%lu fragment%s received\n");
8757d56c0eeSAlexander Langer 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
8767d56c0eeSAlexander Langer 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
8777d56c0eeSAlexander Langer 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
8787d56c0eeSAlexander Langer 	p(ips_delivered, "\t%lu packet%s for this host\n");
8797d56c0eeSAlexander Langer 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
880958d6f7fSPierre Beyssac 	p(ips_forward, "\t%lu packet%s forwarded");
881958d6f7fSPierre Beyssac 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
882958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
883958d6f7fSPierre Beyssac 		putchar('\n');
8847d56c0eeSAlexander Langer 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
885a5969f5fSGarrett Wollman 	p(ips_notmember,
886a5969f5fSGarrett Wollman 	    "\t%lu packet%s received for unknown multicast group\n");
8877d56c0eeSAlexander Langer 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
8887d56c0eeSAlexander Langer 	p(ips_localout, "\t%lu packet%s sent from this host\n");
8897d56c0eeSAlexander Langer 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
890a5969f5fSGarrett Wollman 	p(ips_odropped,
891a5969f5fSGarrett Wollman 	    "\t%lu output packet%s dropped due to no bufs, etc.\n");
8927d56c0eeSAlexander Langer 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
8937d56c0eeSAlexander Langer 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
8947d56c0eeSAlexander Langer 	p(ips_ofragments, "\t%lu fragment%s created\n");
8957d56c0eeSAlexander Langer 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
896cfa1ca9dSYoshinobu Inoue 	p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
89733841545SHajimu UMEMOTO 	p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
8989b50d902SRodney W. Grimes #undef p
89922694ebaSBruce Evans #undef p1a
9009b50d902SRodney W. Grimes }
9019b50d902SRodney W. Grimes 
90254fc657dSGeorge V. Neville-Neil /*
90354fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
90454fc657dSGeorge V. Neville-Neil  */
90554fc657dSGeorge V. Neville-Neil void
90654fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
90754fc657dSGeorge V. Neville-Neil {
90854fc657dSGeorge V. Neville-Neil 	struct arpstat arpstat, zerostat;
90954fc657dSGeorge V. Neville-Neil 	size_t len = sizeof(arpstat);
91054fc657dSGeorge V. Neville-Neil 
91154fc657dSGeorge V. Neville-Neil 	if (live) {
91254fc657dSGeorge V. Neville-Neil 		if (zflag)
91354fc657dSGeorge V. Neville-Neil 			memset(&zerostat, 0, len);
91454fc657dSGeorge V. Neville-Neil 		if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
91554fc657dSGeorge V. Neville-Neil 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
91654fc657dSGeorge V. Neville-Neil 			warn("sysctl: net.link.ether.arp.stats");
91754fc657dSGeorge V. Neville-Neil 			return;
91854fc657dSGeorge V. Neville-Neil 		}
91954fc657dSGeorge V. Neville-Neil 	} else
92054fc657dSGeorge V. Neville-Neil 		kread(off, &arpstat, len);
92154fc657dSGeorge V. Neville-Neil 
92254fc657dSGeorge V. Neville-Neil 	printf("%s:\n", name);
92354fc657dSGeorge V. Neville-Neil 
92454fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
92554fc657dSGeorge V. Neville-Neil     printf(m, arpstat.f, plural(arpstat.f))
92654fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
92754fc657dSGeorge V. Neville-Neil     printf(m, arpstat.f, pluralies(arpstat.f))
92854fc657dSGeorge V. Neville-Neil 
92954fc657dSGeorge V. Neville-Neil 	p(txrequests, "\t%lu ARP request%s sent\n");
93054fc657dSGeorge V. Neville-Neil 	p2(txreplies, "\t%lu ARP repl%s sent\n");
93154fc657dSGeorge V. Neville-Neil 	p(rxrequests, "\t%lu ARP request%s received\n");
93254fc657dSGeorge V. Neville-Neil 	p2(rxreplies, "\t%lu ARP repl%s received\n");
93354fc657dSGeorge V. Neville-Neil 	p(received, "\t%lu ARP packet%s received\n");
93454fc657dSGeorge V. Neville-Neil 	p(dropped, "\t%lu total packet%s dropped due to no ARP entry\n");
93554fc657dSGeorge V. Neville-Neil 	p(timeouts, "\t%lu ARP entry%s timed out\n");
93654fc657dSGeorge V. Neville-Neil 	p(dupips, "\t%lu Duplicate IP%s seen\n");
93754fc657dSGeorge V. Neville-Neil #undef p
93854fc657dSGeorge V. Neville-Neil #undef p2
93954fc657dSGeorge V. Neville-Neil }
94054fc657dSGeorge V. Neville-Neil 
94154fc657dSGeorge V. Neville-Neil 
94254fc657dSGeorge V. Neville-Neil 
9434063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
9444063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
9459b50d902SRodney W. Grimes 	"#1",
9469b50d902SRodney W. Grimes 	"#2",
9474063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
9484063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
9494063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
9509b50d902SRodney W. Grimes 	"#6",
9519b50d902SRodney W. Grimes 	"#7",
9524063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
9534063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
9544063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
9554063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
9564063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
9574063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
9584063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
9594063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
9604063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
9614063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
9624063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
9634063583aSMaxim Konovalov 	"#19",
9644063583aSMaxim Konovalov 	"#20",
9654063583aSMaxim Konovalov 	"#21",
9664063583aSMaxim Konovalov 	"#22",
9674063583aSMaxim Konovalov 	"#23",
9684063583aSMaxim Konovalov 	"#24",
9694063583aSMaxim Konovalov 	"#25",
9704063583aSMaxim Konovalov 	"#26",
9714063583aSMaxim Konovalov 	"#27",
9724063583aSMaxim Konovalov 	"#28",
9734063583aSMaxim Konovalov 	"#29",
9744063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
9754063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
9764063583aSMaxim Konovalov 	"mobile host redirect",
9774063583aSMaxim Konovalov 	"IPv6 where-are-you",
9784063583aSMaxim Konovalov 	"IPv6 i-am-here",
9794063583aSMaxim Konovalov 	"mobile registration req",
9804063583aSMaxim Konovalov 	"mobile registration reply",
9814063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
9824063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
9834063583aSMaxim Konovalov 	"icmp SKIP",
9844063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
9859b50d902SRodney W. Grimes };
9869b50d902SRodney W. Grimes 
9879b50d902SRodney W. Grimes /*
9889b50d902SRodney W. Grimes  * Dump ICMP statistics.
9899b50d902SRodney W. Grimes  */
9909b50d902SRodney W. Grimes void
991feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9929b50d902SRodney W. Grimes {
993c73d99b5SRuslan Ermilov 	struct icmpstat icmpstat, zerostat;
9944e00c309SGarrett Wollman 	int i, first;
9954e00c309SGarrett Wollman 	size_t len;
9969b50d902SRodney W. Grimes 
9974e00c309SGarrett Wollman 	len = sizeof icmpstat;
998feda1a43SJohn Baldwin 	if (live) {
999c73d99b5SRuslan Ermilov 		if (zflag)
1000c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
1001feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
1002c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1003c73d99b5SRuslan Ermilov 			warn("sysctl: net.inet.icmp.stats");
1004c73d99b5SRuslan Ermilov 			return;
1005c73d99b5SRuslan Ermilov 		}
1006feda1a43SJohn Baldwin 	} else
1007feda1a43SJohn Baldwin 		kread(off, &icmpstat, len);
10084e00c309SGarrett Wollman 
10099b50d902SRodney W. Grimes 	printf("%s:\n", name);
10109b50d902SRodney W. Grimes 
10119b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
10129b50d902SRodney W. Grimes     printf(m, icmpstat.f, plural(icmpstat.f))
101322694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
101422694ebaSBruce Evans     printf(m, icmpstat.f)
1015bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1016bd714208SRuslan Ermilov     printf(m, icmpstat.f, plurales(icmpstat.f))
10179b50d902SRodney W. Grimes 
10187d56c0eeSAlexander Langer 	p(icps_error, "\t%lu call%s to icmp_error\n");
10199b50d902SRodney W. Grimes 	p(icps_oldicmp,
1020f99a4046SMike Makonnen 	    "\t%lu error%s not generated in response to an icmp message\n");
10219b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10229b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
10239b50d902SRodney W. Grimes 			if (first) {
10249b50d902SRodney W. Grimes 				printf("\tOutput histogram:\n");
10259b50d902SRodney W. Grimes 				first = 0;
10269b50d902SRodney W. Grimes 			}
10274063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10287d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10299b50d902SRodney W. Grimes 					icmpstat.icps_outhist[i]);
10304063583aSMaxim Konovalov 			else
10314063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10324063583aSMaxim Konovalov 					icmpstat.icps_outhist[i]);
10339b50d902SRodney W. Grimes 		}
10347d56c0eeSAlexander Langer 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
1035bc215f59SDavid E. O'Brien 	p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
1036bc215f59SDavid E. O'Brien 	p(icps_checksum, "\t%lu message%s with bad checksum\n");
10377d56c0eeSAlexander Langer 	p(icps_badlen, "\t%lu message%s with bad length\n");
103822694ebaSBruce Evans 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
103922694ebaSBruce Evans 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
10409b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10419b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
10429b50d902SRodney W. Grimes 			if (first) {
10439b50d902SRodney W. Grimes 				printf("\tInput histogram:\n");
10449b50d902SRodney W. Grimes 				first = 0;
10459b50d902SRodney W. Grimes 			}
10464063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10477d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10489b50d902SRodney W. Grimes 				    icmpstat.icps_inhist[i]);
10494063583aSMaxim Konovalov 			else
10504063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10514063583aSMaxim Konovalov 				    icmpstat.icps_inhist[i]);
10529b50d902SRodney W. Grimes 		}
10537d56c0eeSAlexander Langer 	p(icps_reflect, "\t%lu message response%s generated\n");
1054bd714208SRuslan Ermilov 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
10550237ca7bSRuslan Ermilov 	p(icps_noroute, "\t%lu no return route%s\n");
10569b50d902SRodney W. Grimes #undef p
105722694ebaSBruce Evans #undef p1a
1058bd714208SRuslan Ermilov #undef p2
1059feda1a43SJohn Baldwin 	if (live) {
10604e00c309SGarrett Wollman 		len = sizeof i;
1061feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1062feda1a43SJohn Baldwin 		    0)
10634e00c309SGarrett Wollman 			return;
10644e00c309SGarrett Wollman 		printf("\tICMP address mask responses are %sabled\n",
10654e00c309SGarrett Wollman 		    i ? "en" : "dis");
10669b50d902SRodney W. Grimes 	}
1067feda1a43SJohn Baldwin }
10689b50d902SRodney W. Grimes 
1069d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
10709b50d902SRodney W. Grimes /*
1071d10910e6SBruce M Simpson  * Dump IGMP statistics structure (pre 8.x kernel).
10729b50d902SRodney W. Grimes  */
1073d10910e6SBruce M Simpson static void
1074d10910e6SBruce M Simpson igmp_stats_live_old(u_long off, const char *name)
10759b50d902SRodney W. Grimes {
1076d10910e6SBruce M Simpson 	struct oigmpstat oigmpstat, zerostat;
1077d10910e6SBruce M Simpson 	size_t len = sizeof(oigmpstat);
10789b50d902SRodney W. Grimes 
1079c73d99b5SRuslan Ermilov 	if (zflag)
1080c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
1081d10910e6SBruce M Simpson 	if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1082c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
10834f81ef50SGarrett Wollman 		warn("sysctl: net.inet.igmp.stats");
10849b50d902SRodney W. Grimes 		return;
10854f81ef50SGarrett Wollman 	}
10864f81ef50SGarrett Wollman 
10879b50d902SRodney W. Grimes 	printf("%s:\n", name);
10889b50d902SRodney W. Grimes 
1089d10910e6SBruce M Simpson #define	p(f, m) if (oigmpstat.f || sflag <= 1) \
1090d10910e6SBruce M Simpson     printf(m, oigmpstat.f, plural(oigmpstat.f))
1091d10910e6SBruce M Simpson #define	py(f, m) if (oigmpstat.f || sflag <= 1) \
1092d10910e6SBruce M Simpson     printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
10939b50d902SRodney W. Grimes 	p(igps_rcv_total, "\t%u message%s received\n");
10949b50d902SRodney W. Grimes 	p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
10959b50d902SRodney W. Grimes 	p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
10969b50d902SRodney W. Grimes 	py(igps_rcv_queries, "\t%u membership quer%s received\n");
10973feeb332SDavid E. O'Brien 	py(igps_rcv_badqueries,
10983feeb332SDavid E. O'Brien 	    "\t%u membership quer%s received with invalid field(s)\n");
10999b50d902SRodney W. Grimes 	p(igps_rcv_reports, "\t%u membership report%s received\n");
11003feeb332SDavid E. O'Brien 	p(igps_rcv_badreports,
11013feeb332SDavid E. O'Brien 	    "\t%u membership report%s received with invalid field(s)\n");
11023feeb332SDavid E. O'Brien 	p(igps_rcv_ourreports,
11033feeb332SDavid E. O'Brien "\t%u membership report%s received for groups to which we belong\n");
11049b50d902SRodney W. Grimes         p(igps_snd_reports, "\t%u membership report%s sent\n");
11059b50d902SRodney W. Grimes #undef p
11069b50d902SRodney W. Grimes #undef py
11079b50d902SRodney W. Grimes }
1108d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1109d10910e6SBruce M Simpson 
1110d10910e6SBruce M Simpson /*
1111d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1112d10910e6SBruce M Simpson  */
1113d10910e6SBruce M Simpson void
1114d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1115d10910e6SBruce M Simpson {
1116d10910e6SBruce M Simpson 	struct igmpstat igmpstat, zerostat;
1117d10910e6SBruce M Simpson 	size_t len;
1118d10910e6SBruce M Simpson 
1119d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
1120d10910e6SBruce M Simpson 	if (live) {
1121d10910e6SBruce M Simpson 		/*
1122d10910e6SBruce M Simpson 		 * Detect if we are being run against a pre-IGMPv3 kernel.
1123d10910e6SBruce M Simpson 		 * We cannot do this for a core file as the legacy
1124d10910e6SBruce M Simpson 		 * struct igmpstat has no size field, nor does it
1125d10910e6SBruce M Simpson 		 * export it in any readily-available symbols.
1126d10910e6SBruce M Simpson 		 */
1127d10910e6SBruce M Simpson 		len = 0;
1128d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1129d10910e6SBruce M Simpson 		    0) < 0) {
1130d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1131d10910e6SBruce M Simpson 			return;
1132d10910e6SBruce M Simpson 		}
1133d10910e6SBruce M Simpson 		if (len < sizeof(igmpstat)) {
1134d10910e6SBruce M Simpson 			igmp_stats_live_old(off, name);
1135d10910e6SBruce M Simpson 			return;
1136d10910e6SBruce M Simpson 		}
1137d10910e6SBruce M Simpson 	}
1138d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1139d10910e6SBruce M Simpson 
1140d10910e6SBruce M Simpson 	len = sizeof(igmpstat);
1141d10910e6SBruce M Simpson 	if (live) {
1142d10910e6SBruce M Simpson 		if (zflag)
1143d10910e6SBruce M Simpson 			memset(&zerostat, 0, len);
1144d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1145d10910e6SBruce M Simpson 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1146d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1147d10910e6SBruce M Simpson 			return;
1148d10910e6SBruce M Simpson 		}
1149d10910e6SBruce M Simpson 	} else {
1150d10910e6SBruce M Simpson 		len = sizeof(igmpstat);
1151d10910e6SBruce M Simpson 		kread(off, &igmpstat, len);
1152d10910e6SBruce M Simpson 	}
1153d10910e6SBruce M Simpson 
1154d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1155d10910e6SBruce M Simpson 		warnx("%s: version mismatch (%d != %d)", __func__,
1156d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1157d10910e6SBruce M Simpson 	}
1158d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1159d10910e6SBruce M Simpson 		warnx("%s: size mismatch (%d != %d)", __func__,
1160d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1161d10910e6SBruce M Simpson 	}
1162d10910e6SBruce M Simpson 
1163d10910e6SBruce M Simpson 	printf("%s:\n", name);
1164d10910e6SBruce M Simpson 
1165d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1166d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1167d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1168d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1169d10910e6SBruce M Simpson 	p64(igps_rcv_total, "\t%ju message%s received\n");
1170d10910e6SBruce M Simpson 	p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1171d10910e6SBruce M Simpson 	p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1172d10910e6SBruce M Simpson 	p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1173d10910e6SBruce M Simpson 	py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1174d10910e6SBruce M Simpson 	py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1175d10910e6SBruce M Simpson 	py64(igps_rcv_badqueries,
1176d10910e6SBruce M Simpson 	    "\t%ju membership quer%s received with invalid field(s)\n");
1177d10910e6SBruce M Simpson 	py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1178d10910e6SBruce M Simpson 	py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1179d10910e6SBruce M Simpson 	py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1180d10910e6SBruce M Simpson 	py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1181d10910e6SBruce M Simpson 	p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1182d10910e6SBruce M Simpson 	p64(igps_rcv_badreports,
1183d10910e6SBruce M Simpson 	    "\t%ju membership report%s received with invalid field(s)\n");
1184d10910e6SBruce M Simpson 	p64(igps_rcv_ourreports,
1185d10910e6SBruce M Simpson "\t%ju membership report%s received for groups to which we belong\n");
1186d10910e6SBruce M Simpson         p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1187d10910e6SBruce M Simpson         p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1188d10910e6SBruce M Simpson #undef p64
1189d10910e6SBruce M Simpson #undef py64
1190d10910e6SBruce M Simpson }
11919b50d902SRodney W. Grimes 
11929b50d902SRodney W. Grimes /*
1193c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1194c7b9b5bbSJeffrey Hsu  */
1195c7b9b5bbSJeffrey Hsu void
1196feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1197feda1a43SJohn Baldwin     int proto __unused)
1198c7b9b5bbSJeffrey Hsu {
1199c7b9b5bbSJeffrey Hsu 	struct pimstat pimstat, zerostat;
1200c7b9b5bbSJeffrey Hsu 	size_t len = sizeof pimstat;
1201c7b9b5bbSJeffrey Hsu 
1202feda1a43SJohn Baldwin 	if (live) {
1203c7b9b5bbSJeffrey Hsu 		if (zflag)
1204c7b9b5bbSJeffrey Hsu 			memset(&zerostat, 0, len);
1205c7b9b5bbSJeffrey Hsu 		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1206c7b9b5bbSJeffrey Hsu 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
12076d7c0d2fSDag-Erling Smørgrav 			if (errno != ENOENT)
1208c7b9b5bbSJeffrey Hsu 				warn("sysctl: net.inet.pim.stats");
1209c7b9b5bbSJeffrey Hsu 			return;
1210c7b9b5bbSJeffrey Hsu 		}
1211feda1a43SJohn Baldwin 	} else {
1212feda1a43SJohn Baldwin 		if (off == 0)
1213feda1a43SJohn Baldwin 			return;
1214feda1a43SJohn Baldwin 		kread(off, &pimstat, len);
1215feda1a43SJohn Baldwin 	}
1216c7b9b5bbSJeffrey Hsu 
1217c7b9b5bbSJeffrey Hsu 	printf("%s:\n", name);
1218c7b9b5bbSJeffrey Hsu 
1219c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
12207b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1221c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
12227b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
12237b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
12247b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
12257b95a1ebSYaroslav Tykhiy 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
12267b95a1ebSYaroslav Tykhiy         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
12277b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
12287b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
12297b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
12303feeb332SDavid E. O'Brien 	p(pims_rcv_registers_wrongiif,
12313feeb332SDavid E. O'Brien 	    "\t%ju data register message%s received on wrong iif\n");
12327b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
12337b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
12347b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1235c7b9b5bbSJeffrey Hsu #undef p
1236c7b9b5bbSJeffrey Hsu #undef py
1237c7b9b5bbSJeffrey Hsu }
1238c7b9b5bbSJeffrey Hsu 
1239c7b9b5bbSJeffrey Hsu /*
12409b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
12419b50d902SRodney W. Grimes  */
12429b50d902SRodney W. Grimes void
1243a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port)
12449b50d902SRodney W. Grimes {
12459b50d902SRodney W. Grimes 	struct servent *sp = 0;
12469b50d902SRodney W. Grimes 	char line[80], *cp;
1247cfa1ca9dSYoshinobu Inoue 	int width;
12489b50d902SRodney W. Grimes 
1249080b7f49SDag-Erling Smørgrav 	if (Wflag)
1250080b7f49SDag-Erling Smørgrav 	    sprintf(line, "%s.", inetname(in));
1251080b7f49SDag-Erling Smørgrav 	else
1252a01e3379SDavid Malone 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
12539b50d902SRodney W. Grimes 	cp = index(line, '\0');
1254a01e3379SDavid Malone 	if (!num_port && port)
12559b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
12569b50d902SRodney W. Grimes 	if (sp || port == 0)
12571ef69972SAndrey A. Chernov 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
12589b50d902SRodney W. Grimes 	else
12599b50d902SRodney W. Grimes 		sprintf(cp, "%d ", ntohs((u_short)port));
1260080b7f49SDag-Erling Smørgrav 	width = (Aflag && !Wflag) ? 18 : 22;
1261080b7f49SDag-Erling Smørgrav 	if (Wflag)
1262080b7f49SDag-Erling Smørgrav 	    printf("%-*s ", width, line);
1263080b7f49SDag-Erling Smørgrav 	else
1264cfa1ca9dSYoshinobu Inoue 	    printf("%-*.*s ", width, width, line);
12659b50d902SRodney W. Grimes }
12669b50d902SRodney W. Grimes 
12679b50d902SRodney W. Grimes /*
12689b50d902SRodney W. Grimes  * Construct an Internet address representation.
126965ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
12709b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
12719b50d902SRodney W. Grimes  */
12729b50d902SRodney W. Grimes char *
12735e051718SAssar Westerlund inetname(struct in_addr *inp)
12749b50d902SRodney W. Grimes {
1275a01e3379SDavid Malone 	char *cp;
1276d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
12779b50d902SRodney W. Grimes 	struct hostent *hp;
12789b50d902SRodney W. Grimes 	struct netent *np;
12799b50d902SRodney W. Grimes 
12809b50d902SRodney W. Grimes 	cp = 0;
128165ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
12829b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
12839b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
12849b50d902SRodney W. Grimes 
12859b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
12869b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
12879b50d902SRodney W. Grimes 			if (np)
12889b50d902SRodney W. Grimes 				cp = np->n_name;
12899b50d902SRodney W. Grimes 		}
12909b50d902SRodney W. Grimes 		if (cp == 0) {
12919b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
12929b50d902SRodney W. Grimes 			if (hp) {
12939b50d902SRodney W. Grimes 				cp = hp->h_name;
1294d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
12959b50d902SRodney W. Grimes 			}
12969b50d902SRodney W. Grimes 		}
12979b50d902SRodney W. Grimes 	}
12989b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
12999b50d902SRodney W. Grimes 		strcpy(line, "*");
13009a1f6729SWarner Losh 	else if (cp) {
13011c109628SXin LI 		strlcpy(line, cp, sizeof(line));
13029a1f6729SWarner Losh 	} else {
13039b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
130422694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
130522694ebaSBruce Evans 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
13069b50d902SRodney W. Grimes 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
13079b50d902SRodney W. Grimes 	}
13089b50d902SRodney W. Grimes 	return (line);
13099b50d902SRodney W. Grimes }
1310