xref: /freebsd/usr.bin/netstat/inet.c (revision 85b0f0f3259426e628f7ed0347a8206b78c14e8e)
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  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
306cc6f122SPhilippe Charnier #if 0
319b50d902SRodney W. Grimes #ifndef lint
3205ddff6eSPeter Wemm static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
339b50d902SRodney W. Grimes #endif /* not lint */
346cc6f122SPhilippe Charnier #endif
356cc6f122SPhilippe Charnier 
366cc6f122SPhilippe Charnier #include <sys/cdefs.h>
376cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
389b50d902SRodney W. Grimes 
399b50d902SRodney W. Grimes #include <sys/param.h>
40d8d89152SDavid Greenman #include <sys/queue.h>
41feda1a43SJohn Baldwin #include <sys/domain.h>
42f35a2092SMaksim Yevmenkin #include <sys/protosw.h>
439b50d902SRodney W. Grimes #include <sys/socket.h>
449b50d902SRodney W. Grimes #include <sys/socketvar.h>
454e00c309SGarrett Wollman #include <sys/sysctl.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <net/route.h>
4854fc657dSGeorge V. Neville-Neil #include <net/if_arp.h>
499b50d902SRodney W. Grimes #include <netinet/in.h>
509b50d902SRodney W. Grimes #include <netinet/in_systm.h>
519b50d902SRodney W. Grimes #include <netinet/ip.h>
52a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
53cfa1ca9dSYoshinobu Inoue #ifdef INET6
54cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
55cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
569b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
579b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
589b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
599b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
609b50d902SRodney W. Grimes #include <netinet/ip_var.h>
61c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
629b50d902SRodney W. Grimes #include <netinet/tcp.h>
639b50d902SRodney W. Grimes #include <netinet/tcpip.h>
649b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
659b50d902SRodney W. Grimes #define	TCPSTATES
669b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
679b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
689b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
699b50d902SRodney W. Grimes #include <netinet/tcp_debug.h>
709b50d902SRodney W. Grimes #include <netinet/udp.h>
719b50d902SRodney W. Grimes #include <netinet/udp_var.h>
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes #include <arpa/inet.h>
744f81ef50SGarrett Wollman #include <err.h>
754f81ef50SGarrett Wollman #include <errno.h>
76d121b556SBrian Somers #include <libutil.h>
779b50d902SRodney W. Grimes #include <netdb.h>
787b95a1ebSYaroslav Tykhiy #include <stdint.h>
799b50d902SRodney W. Grimes #include <stdio.h>
804f81ef50SGarrett Wollman #include <stdlib.h>
819b50d902SRodney W. Grimes #include <string.h>
829b50d902SRodney W. Grimes #include <unistd.h>
839b50d902SRodney W. Grimes #include "netstat.h"
849b50d902SRodney W. Grimes 
855e051718SAssar Westerlund char	*inetname(struct in_addr *);
86a01e3379SDavid Malone void	inetprint(struct in_addr *, int, const char *, int);
87cfa1ca9dSYoshinobu Inoue #ifdef INET6
88aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done;
89cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
909b50d902SRodney W. Grimes 
91feda1a43SJohn Baldwin static int
92321ae07fSPhilippe Charnier pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused)
93feda1a43SJohn Baldwin {
94feda1a43SJohn Baldwin 	const char *mibvar;
95feda1a43SJohn Baldwin 	char *buf;
96feda1a43SJohn Baldwin 	size_t len;
97feda1a43SJohn Baldwin 
98feda1a43SJohn Baldwin 	switch (proto) {
99feda1a43SJohn Baldwin 	case IPPROTO_TCP:
100feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
101feda1a43SJohn Baldwin 		break;
102feda1a43SJohn Baldwin 	case IPPROTO_UDP:
103feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
104feda1a43SJohn Baldwin 		break;
105feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
106feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
107feda1a43SJohn Baldwin 		break;
108feda1a43SJohn Baldwin 	default:
109feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
110feda1a43SJohn Baldwin 		break;
111feda1a43SJohn Baldwin 	}
112aa0a1e58SJeff Roberson 	if (strncmp(name, "sdp", 3) == 0)
113aa0a1e58SJeff Roberson 		mibvar = "net.inet.sdp.pcblist";
114feda1a43SJohn Baldwin 	len = 0;
115feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
116feda1a43SJohn Baldwin 		if (errno != ENOENT)
117feda1a43SJohn Baldwin 			warn("sysctl: %s", mibvar);
118feda1a43SJohn Baldwin 		return (0);
119feda1a43SJohn Baldwin 	}
120feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
121feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
122feda1a43SJohn Baldwin 		return (0);
123feda1a43SJohn Baldwin 	}
124feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
125feda1a43SJohn Baldwin 		warn("sysctl: %s", mibvar);
126feda1a43SJohn Baldwin 		free(buf);
127feda1a43SJohn Baldwin 		return (0);
128feda1a43SJohn Baldwin 	}
129feda1a43SJohn Baldwin 	*bufp = buf;
130feda1a43SJohn Baldwin 	return (1);
131feda1a43SJohn Baldwin }
132feda1a43SJohn Baldwin 
133feda1a43SJohn Baldwin /*
134feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
135feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
136feda1a43SJohn Baldwin  */
137feda1a43SJohn Baldwin static void
138feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
139feda1a43SJohn Baldwin {
140feda1a43SJohn Baldwin 	xsb->sb_cc = sb->sb_cc;
141feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
142feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
14349f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
14449f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
145feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
146feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
147feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
148feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
149feda1a43SJohn Baldwin }
150feda1a43SJohn Baldwin 
151feda1a43SJohn Baldwin int
152feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
153feda1a43SJohn Baldwin {
154feda1a43SJohn Baldwin 	struct protosw proto;
155feda1a43SJohn Baldwin 	struct domain domain;
156feda1a43SJohn Baldwin 
157feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
158feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
159feda1a43SJohn Baldwin 	xso->xso_so = so;
160feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
161feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
162feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
163feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
164feda1a43SJohn Baldwin 	xso->so_pcb = so->so_pcb;
165feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
166feda1a43SJohn Baldwin 		return (-1);
167feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
168feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
169feda1a43SJohn Baldwin 		return (-1);
170feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
171feda1a43SJohn Baldwin 	xso->so_qlen = so->so_qlen;
172feda1a43SJohn Baldwin 	xso->so_incqlen = so->so_incqlen;
173feda1a43SJohn Baldwin 	xso->so_qlimit = so->so_qlimit;
174feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
175feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
176feda1a43SJohn Baldwin 	xso->so_oobmark = so->so_oobmark;
177feda1a43SJohn Baldwin 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
178feda1a43SJohn Baldwin 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
179feda1a43SJohn Baldwin 	return (0);
180feda1a43SJohn Baldwin }
181feda1a43SJohn Baldwin 
182feda1a43SJohn Baldwin static int
183feda1a43SJohn Baldwin pcblist_kvm(u_long off, char **bufp, int istcp)
184feda1a43SJohn Baldwin {
185feda1a43SJohn Baldwin 	struct inpcbinfo pcbinfo;
186feda1a43SJohn Baldwin 	struct inpcbhead listhead;
187feda1a43SJohn Baldwin 	struct inpcb *inp;
188feda1a43SJohn Baldwin 	struct xinpcb xi;
189feda1a43SJohn Baldwin 	struct xinpgen xig;
190feda1a43SJohn Baldwin 	struct xtcpcb xt;
191feda1a43SJohn Baldwin 	struct socket so;
192feda1a43SJohn Baldwin 	struct xsocket *xso;
193feda1a43SJohn Baldwin 	char *buf, *p;
194feda1a43SJohn Baldwin 	size_t len;
195feda1a43SJohn Baldwin 
196feda1a43SJohn Baldwin 	if (off == 0)
197feda1a43SJohn Baldwin 		return (0);
198feda1a43SJohn Baldwin 	kread(off, &pcbinfo, sizeof(pcbinfo));
199feda1a43SJohn Baldwin 	if (istcp)
200feda1a43SJohn Baldwin 		len = 2 * sizeof(xig) +
201feda1a43SJohn Baldwin 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
202feda1a43SJohn Baldwin 		    sizeof(struct xtcpcb);
203feda1a43SJohn Baldwin 	else
204feda1a43SJohn Baldwin 		len = 2 * sizeof(xig) +
205feda1a43SJohn Baldwin 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
206feda1a43SJohn Baldwin 		    sizeof(struct xinpcb);
207feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
208feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
209feda1a43SJohn Baldwin 		return (0);
210feda1a43SJohn Baldwin 	}
211feda1a43SJohn Baldwin 	p = buf;
212feda1a43SJohn Baldwin 
213feda1a43SJohn Baldwin #define	COPYOUT(obj, size) do {						\
214feda1a43SJohn Baldwin 	if (len < (size)) {						\
215feda1a43SJohn Baldwin 		warnx("buffer size exceeded");				\
216feda1a43SJohn Baldwin 		goto fail;						\
217feda1a43SJohn Baldwin 	}								\
218feda1a43SJohn Baldwin 	bcopy((obj), p, (size));					\
219feda1a43SJohn Baldwin 	len -= (size);							\
220feda1a43SJohn Baldwin 	p += (size);							\
221feda1a43SJohn Baldwin } while (0)
222feda1a43SJohn Baldwin 
223feda1a43SJohn Baldwin #define	KREAD(off, buf, len) do {					\
224feda1a43SJohn Baldwin 	if (kread((uintptr_t)(off), (buf), (len)) != 0)			\
225feda1a43SJohn Baldwin 		goto fail;						\
226feda1a43SJohn Baldwin } while (0)
227feda1a43SJohn Baldwin 
228feda1a43SJohn Baldwin 	/* Write out header. */
229feda1a43SJohn Baldwin 	xig.xig_len = sizeof xig;
230feda1a43SJohn Baldwin 	xig.xig_count = pcbinfo.ipi_count;
231feda1a43SJohn Baldwin 	xig.xig_gen = pcbinfo.ipi_gencnt;
232feda1a43SJohn Baldwin 	xig.xig_sogen = 0;
233feda1a43SJohn Baldwin 	COPYOUT(&xig, sizeof xig);
234feda1a43SJohn Baldwin 
235feda1a43SJohn Baldwin 	/* Walk the PCB list. */
236feda1a43SJohn Baldwin 	xt.xt_len = sizeof xt;
237feda1a43SJohn Baldwin 	xi.xi_len = sizeof xi;
238feda1a43SJohn Baldwin 	if (istcp)
239feda1a43SJohn Baldwin 		xso = &xt.xt_socket;
240feda1a43SJohn Baldwin 	else
241feda1a43SJohn Baldwin 		xso = &xi.xi_socket;
242feda1a43SJohn Baldwin 	KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
243feda1a43SJohn Baldwin 	LIST_FOREACH(inp, &listhead, inp_list) {
244feda1a43SJohn Baldwin 		if (istcp) {
245feda1a43SJohn Baldwin 			KREAD(inp, &xt.xt_inp, sizeof(*inp));
246feda1a43SJohn Baldwin 			inp = &xt.xt_inp;
247feda1a43SJohn Baldwin 		} else {
248feda1a43SJohn Baldwin 			KREAD(inp, &xi.xi_inp, sizeof(*inp));
249feda1a43SJohn Baldwin 			inp = &xi.xi_inp;
250feda1a43SJohn Baldwin 		}
251feda1a43SJohn Baldwin 
252feda1a43SJohn Baldwin 		if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
253feda1a43SJohn Baldwin 			continue;
254feda1a43SJohn Baldwin 
255feda1a43SJohn Baldwin 		if (istcp) {
256feda1a43SJohn Baldwin 			if (inp->inp_ppcb == NULL)
257feda1a43SJohn Baldwin 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
258ad71fe3cSRobert Watson 			else if (inp->inp_flags & INP_TIMEWAIT) {
259feda1a43SJohn Baldwin 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
260feda1a43SJohn Baldwin 				xt.xt_tp.t_state = TCPS_TIME_WAIT;
261feda1a43SJohn Baldwin 			} else
262feda1a43SJohn Baldwin 				KREAD(inp->inp_ppcb, &xt.xt_tp,
263feda1a43SJohn Baldwin 				    sizeof xt.xt_tp);
264feda1a43SJohn Baldwin 		}
265feda1a43SJohn Baldwin 		if (inp->inp_socket) {
266feda1a43SJohn Baldwin 			KREAD(inp->inp_socket, &so, sizeof(so));
267feda1a43SJohn Baldwin 			if (sotoxsocket(&so, xso) != 0)
268feda1a43SJohn Baldwin 				goto fail;
269feda1a43SJohn Baldwin 		} else {
270feda1a43SJohn Baldwin 			bzero(xso, sizeof(*xso));
271feda1a43SJohn Baldwin 			if (istcp)
272feda1a43SJohn Baldwin 				xso->xso_protocol = IPPROTO_TCP;
273feda1a43SJohn Baldwin 		}
274feda1a43SJohn Baldwin 		if (istcp)
275feda1a43SJohn Baldwin 			COPYOUT(&xt, sizeof xt);
276feda1a43SJohn Baldwin 		else
277feda1a43SJohn Baldwin 			COPYOUT(&xi, sizeof xi);
278feda1a43SJohn Baldwin 	}
279feda1a43SJohn Baldwin 
280feda1a43SJohn Baldwin 	/* Reread the pcbinfo and write out the footer. */
281feda1a43SJohn Baldwin 	kread(off, &pcbinfo, sizeof(pcbinfo));
282feda1a43SJohn Baldwin 	xig.xig_count = pcbinfo.ipi_count;
283feda1a43SJohn Baldwin 	xig.xig_gen = pcbinfo.ipi_gencnt;
284feda1a43SJohn Baldwin 	COPYOUT(&xig, sizeof xig);
285feda1a43SJohn Baldwin 
286feda1a43SJohn Baldwin 	*bufp = buf;
287feda1a43SJohn Baldwin 	return (1);
288feda1a43SJohn Baldwin 
289feda1a43SJohn Baldwin fail:
290feda1a43SJohn Baldwin 	free(buf);
291feda1a43SJohn Baldwin 	return (0);
292feda1a43SJohn Baldwin #undef COPYOUT
293feda1a43SJohn Baldwin #undef KREAD
294feda1a43SJohn Baldwin }
295feda1a43SJohn Baldwin 
2969b50d902SRodney W. Grimes /*
2979b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
2989b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
2999b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
3009b50d902SRodney W. Grimes  * -a (all) flag is specified.
3019b50d902SRodney W. Grimes  */
3029b50d902SRodney W. Grimes void
303feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
3049b50d902SRodney W. Grimes {
3059b50d902SRodney W. Grimes 	int istcp;
3069b50d902SRodney W. Grimes 	static int first = 1;
3074f81ef50SGarrett Wollman 	char *buf;
308feda1a43SJohn Baldwin 	const char *vchar;
309a97a9922SJulian Elischer 	struct tcpcb *tp = NULL;
3104f81ef50SGarrett Wollman 	struct inpcb *inp;
3114f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
3124f81ef50SGarrett Wollman 	struct xsocket *so;
313b8614722SMike Silbersack 	struct xtcp_timer *timer;
3149b50d902SRodney W. Grimes 
3154f81ef50SGarrett Wollman 	istcp = 0;
3164f81ef50SGarrett Wollman 	switch (proto) {
3174f81ef50SGarrett Wollman 	case IPPROTO_TCP:
318cfa1ca9dSYoshinobu Inoue #ifdef INET6
319aa0a1e58SJeff Roberson 		if (strncmp(name, "sdp", 3) != 0) {
320cfa1ca9dSYoshinobu Inoue 			if (tcp_done != 0)
321cfa1ca9dSYoshinobu Inoue 				return;
322cfa1ca9dSYoshinobu Inoue 			else
323cfa1ca9dSYoshinobu Inoue 				tcp_done = 1;
324aa0a1e58SJeff Roberson 		} else {
325aa0a1e58SJeff Roberson 			if (sdp_done != 0)
326aa0a1e58SJeff Roberson 				return;
327aa0a1e58SJeff Roberson 			else
328aa0a1e58SJeff Roberson 				sdp_done = 1;
329aa0a1e58SJeff Roberson 		}
330cfa1ca9dSYoshinobu Inoue #endif
3314f81ef50SGarrett Wollman 		istcp = 1;
3324f81ef50SGarrett Wollman 		break;
3334f81ef50SGarrett Wollman 	case IPPROTO_UDP:
334cfa1ca9dSYoshinobu Inoue #ifdef INET6
335cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
336cfa1ca9dSYoshinobu Inoue 			return;
337cfa1ca9dSYoshinobu Inoue 		else
338cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
339cfa1ca9dSYoshinobu Inoue #endif
3404f81ef50SGarrett Wollman 		break;
3414f81ef50SGarrett Wollman 	}
342feda1a43SJohn Baldwin 	if (live) {
343aa0a1e58SJeff Roberson 		if (!pcblist_sysctl(proto, name, &buf, istcp))
3449b50d902SRodney W. Grimes 			return;
345feda1a43SJohn Baldwin 	} else {
346feda1a43SJohn Baldwin 		if (!pcblist_kvm(off, &buf, istcp))
3474f81ef50SGarrett Wollman 			return;
3484f81ef50SGarrett Wollman 	}
3494f81ef50SGarrett Wollman 
3504f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
3514f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
3524f81ef50SGarrett Wollman 	     xig->xig_len > sizeof(struct xinpgen);
3534f81ef50SGarrett Wollman 	     xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
3549b50d902SRodney W. Grimes 		if (istcp) {
355b8614722SMike Silbersack 			timer = &((struct xtcpcb *)xig)->xt_timer;
3564f81ef50SGarrett Wollman 			tp = &((struct xtcpcb *)xig)->xt_tp;
3574f81ef50SGarrett Wollman 			inp = &((struct xtcpcb *)xig)->xt_inp;
3584f81ef50SGarrett Wollman 			so = &((struct xtcpcb *)xig)->xt_socket;
3594f81ef50SGarrett Wollman 		} else {
3604f81ef50SGarrett Wollman 			inp = &((struct xinpcb *)xig)->xi_inp;
3614f81ef50SGarrett Wollman 			so = &((struct xinpcb *)xig)->xi_socket;
362aae09141SMike Silbersack 			timer = NULL;
3639b50d902SRodney W. Grimes 		}
3644f81ef50SGarrett Wollman 
3654f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
366feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
3674f81ef50SGarrett Wollman 			continue;
3684f81ef50SGarrett Wollman 
3694f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
3704f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
3714f81ef50SGarrett Wollman 			continue;
3724f81ef50SGarrett Wollman 
373a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
374cfa1ca9dSYoshinobu Inoue #ifdef INET6
375a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
376cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
377a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
378cfa1ca9dSYoshinobu Inoue #ifdef INET6
3793feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
380cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
381cfa1ca9dSYoshinobu Inoue 			))
382cfa1ca9dSYoshinobu Inoue 		    )
383cfa1ca9dSYoshinobu Inoue 			continue;
384cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
385cfa1ca9dSYoshinobu Inoue 		    (
3862b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
3872b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
388cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
389cfa1ca9dSYoshinobu Inoue #ifdef INET6
390a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
391cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
392cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
393a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
394cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
395cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
396cfa1ca9dSYoshinobu Inoue #ifdef INET6
397cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
398cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
399cfa1ca9dSYoshinobu Inoue #endif
400cfa1ca9dSYoshinobu Inoue 			  ))
401cfa1ca9dSYoshinobu Inoue 		     ))
4024f81ef50SGarrett Wollman 			continue;
4034f81ef50SGarrett Wollman 
4049b50d902SRodney W. Grimes 		if (first) {
405ac55add0SGuido van Rooij 			if (!Lflag) {
4069b50d902SRodney W. Grimes 				printf("Active Internet connections");
4079b50d902SRodney W. Grimes 				if (aflag)
4089b50d902SRodney W. Grimes 					printf(" (including servers)");
409ac55add0SGuido van Rooij 			} else
410ac55add0SGuido van Rooij 				printf(
411ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
4129b50d902SRodney W. Grimes 			putchar('\n');
4139b50d902SRodney W. Grimes 			if (Aflag)
414afc74015SRuslan Ermilov 				printf("%-*s ", 2 * (int)sizeof(void *), "Tcpcb");
415ac55add0SGuido van Rooij 			if (Lflag)
416080b7f49SDag-Erling Smørgrav 				printf((Aflag && !Wflag) ?
417afc74015SRuslan Ermilov 				    "%-5.5s %-14.14s %-18.18s" :
418afc74015SRuslan Ermilov 				    "%-5.5s %-14.14s %-22.22s",
419afc74015SRuslan Ermilov 				    "Proto", "Listen", "Local Address");
420afc74015SRuslan Ermilov 			else if (Tflag)
421afc74015SRuslan Ermilov 				printf((Aflag && !Wflag) ?
422afc74015SRuslan Ermilov 			    "%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %s" :
423afc74015SRuslan Ermilov 			    "%-5.5s %-6.6s %-6.6s %-6.6s %-22.22s %s",
424f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
42549f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
426afc74015SRuslan Ermilov 			else {
427afc74015SRuslan Ermilov 				printf((Aflag && !Wflag) ?
428afc74015SRuslan Ermilov 				       "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" :
429afc74015SRuslan Ermilov 				       "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s",
430afc74015SRuslan Ermilov 				       "Proto", "Recv-Q", "Send-Q",
431afc74015SRuslan Ermilov 				       "Local Address", "Foreign Address");
432*85b0f0f3SAdrian Chadd 				if (!xflag && !Rflag)
433afc74015SRuslan Ermilov 					printf(" (state)");
434afc74015SRuslan Ermilov 			}
435b8614722SMike Silbersack 			if (xflag) {
436b8614722SMike 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",
43794f138feSGeorge V. Neville-Neil 				       "R-MBUF", "S-MBUF", "R-CLUS",
43894f138feSGeorge V. Neville-Neil 				       "S-CLUS", "R-HIWA", "S-HIWA",
43994f138feSGeorge V. Neville-Neil 				       "R-LOWA", "S-LOWA", "R-BCNT",
440b8614722SMike Silbersack 				       "S-BCNT", "R-BMAX", "S-BMAX");
441afc74015SRuslan Ermilov 				printf(" %7.7s %7.7s %7.7s %7.7s %7.7s %7.7s",
442b8614722SMike Silbersack 				       "rexmt", "persist", "keep",
443afc74015SRuslan Ermilov 				       "2msl", "delack", "rcvtime");
444*85b0f0f3SAdrian Chadd 			} else if (Rflag) {
445*85b0f0f3SAdrian Chadd 				printf ("  %8.8s %5.5s",
446*85b0f0f3SAdrian Chadd 				    "flowid", "ftype");
44794f138feSGeorge V. Neville-Neil 			}
448afc74015SRuslan Ermilov 			putchar('\n');
4499b50d902SRodney W. Grimes 			first = 0;
4509b50d902SRodney W. Grimes 		}
451fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
452fb5d0fbdSRuslan Ermilov 			continue;
453e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
454e4ec3989SDag-Erling Smørgrav 			if (istcp)
455afc74015SRuslan Ermilov 				printf("%*lx ", 2 * (int)sizeof(void *), (u_long)inp->inp_ppcb);
456e4ec3989SDag-Erling Smørgrav 			else
457afc74015SRuslan Ermilov 				printf("%*lx ", 2 * (int)sizeof(void *), (u_long)so->so_pcb);
458e4ec3989SDag-Erling Smørgrav 		}
459cfa1ca9dSYoshinobu Inoue #ifdef INET6
460fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
4613feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4623feeb332SDavid E. O'Brien 			    "46" : "6 ";
463fc60ab7bSYoshinobu Inoue 		else
464cfa1ca9dSYoshinobu Inoue #endif
4653feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4663feeb332SDavid E. O'Brien 		    "4 " : "  ";
46709fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
46809fe6320SNavdeep Parhar 			printf("%-3.3s%-2.2s ", "toe", vchar);
46909fe6320SNavdeep Parhar 		else
470fb5d0fbdSRuslan Ermilov 			printf("%-3.3s%-2.2s ", name, vchar);
471fb5d0fbdSRuslan Ermilov 		if (Lflag) {
472a01e3379SDavid Malone 			char buf1[15];
473fc60ab7bSYoshinobu Inoue 
474a01e3379SDavid Malone 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
475fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
476a01e3379SDavid Malone 			printf("%-14.14s ", buf1);
477f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
478f5d34df5SGeorge V. Neville-Neil 			if (istcp)
479f5d34df5SGeorge V. Neville-Neil 				printf("%6u %6u %6u ", tp->t_sndrexmitpack,
480f5d34df5SGeorge V. Neville-Neil 				       tp->t_rcvoopack, tp->t_sndzerowin);
481fb5d0fbdSRuslan Ermilov 		} else {
482dd335a15SDavid E. O'Brien 			printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
483fc60ab7bSYoshinobu Inoue 		}
48465ea0024SAssar Westerlund 		if (numeric_port) {
485cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4864f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4878d612dd2SPoul-Henning Kamp 				    name, 1);
488ac55add0SGuido van Rooij 				if (!Lflag)
489ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
490ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 1);
491cfa1ca9dSYoshinobu Inoue 			}
492cfa1ca9dSYoshinobu Inoue #ifdef INET6
493cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
494cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
495cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
496ac55add0SGuido van Rooij 				if (!Lflag)
497cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
498cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
499cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
500cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5014f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
502cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
5034f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
5048d612dd2SPoul-Henning Kamp 				    name, 1);
505ac55add0SGuido van Rooij 				if (!Lflag)
506ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
507ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 0);
508cfa1ca9dSYoshinobu Inoue 			}
509cfa1ca9dSYoshinobu Inoue #ifdef INET6
510cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
511cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
512cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
513ac55add0SGuido van Rooij 				if (!Lflag)
514cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
515cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
516cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
517cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5188d612dd2SPoul-Henning Kamp 		} else {
519cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
5204f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
5218d612dd2SPoul-Henning Kamp 				    name, 0);
522ac55add0SGuido van Rooij 				if (!Lflag)
523ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
524ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
5253feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
526cfa1ca9dSYoshinobu Inoue 			}
527cfa1ca9dSYoshinobu Inoue #ifdef INET6
528cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
529cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
530cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
531ac55add0SGuido van Rooij 				if (!Lflag)
532cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
533cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
5343feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
535cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
536cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5378d612dd2SPoul-Henning Kamp 		}
53849f287f8SGeorge V. Neville-Neil 		if (xflag) {
53949f287f8SGeorge V. Neville-Neil 			printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u",
54049f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
54149f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
54249f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
54349f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
54449f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
54549f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
546aae09141SMike Silbersack 			if (timer != NULL)
547b8614722SMike Silbersack 				printf(" %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d",
548b8614722SMike Silbersack 				    timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10,
549b8614722SMike Silbersack 				    timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10,
550b8614722SMike Silbersack 				    timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10,
551b8614722SMike Silbersack 				    timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10,
552b8614722SMike Silbersack 				    timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10,
553b8614722SMike Silbersack 				    timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10);
554b8614722SMike Silbersack 		}
555*85b0f0f3SAdrian Chadd 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
5564f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5574f81ef50SGarrett Wollman 				printf("%d", tp->t_state);
5589a94a597SGarrett Wollman 			else {
5594f81ef50SGarrett Wollman 				printf("%s", tcpstates[tp->t_state]);
560513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5619a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
5624f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
5639a94a597SGarrett Wollman 					putchar('*');
564513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5659a94a597SGarrett Wollman 			}
5669b50d902SRodney W. Grimes 		}
567*85b0f0f3SAdrian Chadd 		if (Rflag) {
568*85b0f0f3SAdrian Chadd 			printf(" %08x %5d",
569*85b0f0f3SAdrian Chadd 			    inp->inp_flowid,
570*85b0f0f3SAdrian Chadd 			    inp->inp_flowtype);
571*85b0f0f3SAdrian Chadd 		}
5729b50d902SRodney W. Grimes 		putchar('\n');
5739b50d902SRodney W. Grimes 	}
5744f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5754f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
5764f81ef50SGarrett Wollman 			printf("Some %s sockets may have been deleted.\n",
5774f81ef50SGarrett Wollman 			    name);
5784f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
5794f81ef50SGarrett Wollman 			printf("Some %s sockets may have been created.\n",
5804f81ef50SGarrett Wollman 			    name);
5814f81ef50SGarrett Wollman 		} else {
5823feeb332SDavid E. O'Brien 			printf(
5833feeb332SDavid E. O'Brien 	"Some %s sockets may have been created or deleted.\n",
5844f81ef50SGarrett Wollman 			    name);
5854f81ef50SGarrett Wollman 		}
5864f81ef50SGarrett Wollman 	}
5874f81ef50SGarrett Wollman 	free(buf);
5889b50d902SRodney W. Grimes }
5899b50d902SRodney W. Grimes 
5909b50d902SRodney W. Grimes /*
5919b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5929b50d902SRodney W. Grimes  */
5939b50d902SRodney W. Grimes void
594feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5959b50d902SRodney W. Grimes {
596c73d99b5SRuslan Ermilov 	struct tcpstat tcpstat, zerostat;
5974f81ef50SGarrett Wollman 	size_t len = sizeof tcpstat;
5989b50d902SRodney W. Grimes 
599cfa1ca9dSYoshinobu Inoue #ifdef INET6
600cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
601cfa1ca9dSYoshinobu Inoue 		return;
602cfa1ca9dSYoshinobu Inoue 	else
603cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
604cfa1ca9dSYoshinobu Inoue #endif
605cfa1ca9dSYoshinobu Inoue 
606feda1a43SJohn Baldwin 	if (live) {
607feda1a43SJohn Baldwin 		if (zflag)
608feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
609feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
610feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
611feda1a43SJohn Baldwin 			warn("sysctl: net.inet.tcp.stats");
612feda1a43SJohn Baldwin 			return;
613feda1a43SJohn Baldwin 		}
6145da0521fSAndrey V. Elsukov 	} else
6155da0521fSAndrey V. Elsukov 		kread_counters(off, &tcpstat, len);
616feda1a43SJohn Baldwin 
6179b50d902SRodney W. Grimes 	printf ("%s:\n", name);
6189b50d902SRodney W. Grimes 
6199b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
6205923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
6215923c293SGleb Smirnoff 
62222694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
6235923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f)
6245923c293SGleb Smirnoff 
6259b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
6265923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6275923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
6285923c293SGleb Smirnoff 
62922694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
6305923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6315923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
6325923c293SGleb Smirnoff 
6339b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
6345923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
6359b50d902SRodney W. Grimes 
6365923c293SGleb Smirnoff 	p(tcps_sndtotal, "\t%ju packet%s sent\n");
6375923c293SGleb Smirnoff 	p2(tcps_sndpack,tcps_sndbyte, "\t\t%ju data packet%s (%ju byte%s)\n");
6389b50d902SRodney W. Grimes 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
6395923c293SGleb Smirnoff 	    "\t\t%ju data packet%s (%ju byte%s) retransmitted\n");
640d65bf08aSMatthew Dillon 	p(tcps_sndrexmitbad,
6415923c293SGleb Smirnoff 	    "\t\t%ju data packet%s unnecessarily retransmitted\n");
6425923c293SGleb Smirnoff 	p(tcps_mturesent, "\t\t%ju resend%s initiated by MTU discovery\n");
64322694ebaSBruce Evans 	p2a(tcps_sndacks, tcps_delack,
6445923c293SGleb Smirnoff 	    "\t\t%ju ack-only packet%s (%ju delayed)\n");
6455923c293SGleb Smirnoff 	p(tcps_sndurg, "\t\t%ju URG only packet%s\n");
6465923c293SGleb Smirnoff 	p(tcps_sndprobe, "\t\t%ju window probe packet%s\n");
6475923c293SGleb Smirnoff 	p(tcps_sndwinup, "\t\t%ju window update packet%s\n");
6485923c293SGleb Smirnoff 	p(tcps_sndctrl, "\t\t%ju control packet%s\n");
6495923c293SGleb Smirnoff 	p(tcps_rcvtotal, "\t%ju packet%s received\n");
6503feeb332SDavid E. O'Brien 	p2(tcps_rcvackpack, tcps_rcvackbyte,
6515923c293SGleb Smirnoff 	    "\t\t%ju ack%s (for %ju byte%s)\n");
6525923c293SGleb Smirnoff 	p(tcps_rcvdupack, "\t\t%ju duplicate ack%s\n");
6535923c293SGleb Smirnoff 	p(tcps_rcvacktoomuch, "\t\t%ju ack%s for unsent data\n");
6549b50d902SRodney W. Grimes 	p2(tcps_rcvpack, tcps_rcvbyte,
6555923c293SGleb Smirnoff 	    "\t\t%ju packet%s (%ju byte%s) received in-sequence\n");
6569b50d902SRodney W. Grimes 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
6575923c293SGleb Smirnoff 	    "\t\t%ju completely duplicate packet%s (%ju byte%s)\n");
6585923c293SGleb Smirnoff 	p(tcps_pawsdrop, "\t\t%ju old duplicate packet%s\n");
6599b50d902SRodney W. Grimes 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
6605923c293SGleb Smirnoff 	    "\t\t%ju packet%s with some dup. data (%ju byte%s duped)\n");
6619b50d902SRodney W. Grimes 	p2(tcps_rcvoopack, tcps_rcvoobyte,
6625923c293SGleb Smirnoff 	    "\t\t%ju out-of-order packet%s (%ju byte%s)\n");
6639b50d902SRodney W. Grimes 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
6645923c293SGleb Smirnoff 	    "\t\t%ju packet%s (%ju byte%s) of data after window\n");
6655923c293SGleb Smirnoff 	p(tcps_rcvwinprobe, "\t\t%ju window probe%s\n");
6665923c293SGleb Smirnoff 	p(tcps_rcvwinupd, "\t\t%ju window update packet%s\n");
6675923c293SGleb Smirnoff 	p(tcps_rcvafterclose, "\t\t%ju packet%s received after close\n");
6685923c293SGleb Smirnoff 	p(tcps_rcvbadsum, "\t\t%ju discarded for bad checksum%s\n");
6695923c293SGleb Smirnoff 	p(tcps_rcvbadoff, "\t\t%ju discarded for bad header offset field%s\n");
6705923c293SGleb Smirnoff 	p1a(tcps_rcvshort, "\t\t%ju discarded because packet too short\n");
671c669105dSGleb Smirnoff 	p1a(tcps_rcvreassfull,
672c669105dSGleb Smirnoff 	    "\t\t%ju discarded due to no space in reassembly queue\n");
6735923c293SGleb Smirnoff 	p(tcps_connattempt, "\t%ju connection request%s\n");
6745923c293SGleb Smirnoff 	p(tcps_accepts, "\t%ju connection accept%s\n");
6755923c293SGleb Smirnoff 	p(tcps_badsyn, "\t%ju bad connection attempt%s\n");
6765923c293SGleb Smirnoff 	p(tcps_listendrop, "\t%ju listen queue overflow%s\n");
6775923c293SGleb Smirnoff 	p(tcps_badrst, "\t%ju ignored RSTs in the window%s\n");
6785923c293SGleb Smirnoff 	p(tcps_connects, "\t%ju connection%s established (including accepts)\n");
6799b50d902SRodney W. Grimes 	p2(tcps_closed, tcps_drops,
6805923c293SGleb Smirnoff 	    "\t%ju connection%s closed (including %ju drop%s)\n");
6815923c293SGleb Smirnoff 	p(tcps_cachedrtt, "\t\t%ju connection%s updated cached RTT on close\n");
682861b1828SGarrett Wollman 	p(tcps_cachedrttvar,
6835923c293SGleb Smirnoff 	    "\t\t%ju connection%s updated cached RTT variance on close\n");
684861b1828SGarrett Wollman 	p(tcps_cachedssthresh,
6855923c293SGleb Smirnoff 	    "\t\t%ju connection%s updated cached ssthresh on close\n");
6865923c293SGleb Smirnoff 	p(tcps_conndrops, "\t%ju embryonic connection%s dropped\n");
6879b50d902SRodney W. Grimes 	p2(tcps_rttupdated, tcps_segstimed,
6885923c293SGleb Smirnoff 	    "\t%ju segment%s updated rtt (of %ju attempt%s)\n");
6895923c293SGleb Smirnoff 	p(tcps_rexmttimeo, "\t%ju retransmit timeout%s\n");
6905923c293SGleb Smirnoff 	p(tcps_timeoutdrop, "\t\t%ju connection%s dropped by rexmit timeout\n");
6915923c293SGleb Smirnoff 	p(tcps_persisttimeo, "\t%ju persist timeout%s\n");
6925923c293SGleb Smirnoff 	p(tcps_persistdrop, "\t\t%ju connection%s dropped by persist timeout\n");
6933feeb332SDavid E. O'Brien 	p(tcps_finwait2_drops,
6945923c293SGleb Smirnoff 	    "\t%ju Connection%s (fin_wait_2) dropped because of timeout\n");
6955923c293SGleb Smirnoff 	p(tcps_keeptimeo, "\t%ju keepalive timeout%s\n");
6965923c293SGleb Smirnoff 	p(tcps_keepprobe, "\t\t%ju keepalive probe%s sent\n");
6975923c293SGleb Smirnoff 	p(tcps_keepdrops, "\t\t%ju connection%s dropped by keepalive\n");
6985923c293SGleb Smirnoff 	p(tcps_predack, "\t%ju correct ACK header prediction%s\n");
6995923c293SGleb Smirnoff 	p(tcps_preddat, "\t%ju correct data packet header prediction%s\n");
70060a31b3aSJonathan Lemon 
7015923c293SGleb Smirnoff 	p3(tcps_sc_added, "\t%ju syncache entr%s added\n");
7025923c293SGleb Smirnoff 	p1a(tcps_sc_retransmitted, "\t\t%ju retransmitted\n");
7035923c293SGleb Smirnoff 	p1a(tcps_sc_dupsyn, "\t\t%ju dupsyn\n");
7045923c293SGleb Smirnoff 	p1a(tcps_sc_dropped, "\t\t%ju dropped\n");
7055923c293SGleb Smirnoff 	p1a(tcps_sc_completed, "\t\t%ju completed\n");
7065923c293SGleb Smirnoff 	p1a(tcps_sc_bucketoverflow, "\t\t%ju bucket overflow\n");
7075923c293SGleb Smirnoff 	p1a(tcps_sc_cacheoverflow, "\t\t%ju cache overflow\n");
7085923c293SGleb Smirnoff 	p1a(tcps_sc_reset, "\t\t%ju reset\n");
7095923c293SGleb Smirnoff 	p1a(tcps_sc_stale, "\t\t%ju stale\n");
7105923c293SGleb Smirnoff 	p1a(tcps_sc_aborted, "\t\t%ju aborted\n");
7115923c293SGleb Smirnoff 	p1a(tcps_sc_badack, "\t\t%ju badack\n");
7125923c293SGleb Smirnoff 	p1a(tcps_sc_unreach, "\t\t%ju unreach\n");
7135923c293SGleb Smirnoff 	p(tcps_sc_zonefail, "\t\t%ju zone failure%s\n");
7145923c293SGleb Smirnoff 	p(tcps_sc_sendcookie, "\t%ju cookie%s sent\n");
7155923c293SGleb Smirnoff 	p(tcps_sc_recvcookie, "\t%ju cookie%s received\n");
716b6101dafSPaul Saab 
7175923c293SGleb Smirnoff 	p3(tcps_hc_added, "\t%ju hostcache entr%s added\n");
7185923c293SGleb Smirnoff 	p1a(tcps_hc_bucketoverflow, "\t\t%ju bucket overflow\n");
719699ef999SRuslan Ermilov 
7205923c293SGleb Smirnoff 	p(tcps_sack_recovery_episode, "\t%ju SACK recovery episode%s\n");
721b6101dafSPaul Saab 	p(tcps_sack_rexmits,
7225923c293SGleb Smirnoff 	    "\t%ju segment rexmit%s in SACK recovery episodes\n");
723b6101dafSPaul Saab 	p(tcps_sack_rexmit_bytes,
7245923c293SGleb Smirnoff 	    "\t%ju byte rexmit%s in SACK recovery episodes\n");
725b6101dafSPaul Saab 	p(tcps_sack_rcv_blocks,
7265923c293SGleb Smirnoff 	    "\t%ju SACK option%s (SACK blocks) received\n");
7275923c293SGleb Smirnoff 	p(tcps_sack_send_blocks, "\t%ju SACK option%s (SACK blocks) sent\n");
7285923c293SGleb Smirnoff 	p1a(tcps_sack_sboverflow, "\t%ju SACK scoreboard overflow\n");
729b6101dafSPaul Saab 
7305923c293SGleb Smirnoff 	p(tcps_ecn_ce, "\t%ju packet%s with ECN CE bit set\n");
7315923c293SGleb Smirnoff 	p(tcps_ecn_ect0, "\t%ju packet%s with ECN ECT(0) bit set\n");
7325923c293SGleb Smirnoff 	p(tcps_ecn_ect1, "\t%ju packet%s with ECN ECT(1) bit set\n");
7335923c293SGleb Smirnoff 	p(tcps_ecn_shs, "\t%ju successful ECN handshake%s\n");
7345923c293SGleb Smirnoff 	p(tcps_ecn_rcwnd, "\t%ju time%s ECN reduced the congestion window\n");
735a4993b25SBjoern A. Zeeb 
736a4993b25SBjoern A. Zeeb 	p(tcps_sig_rcvgoodsig,
737a4993b25SBjoern A. Zeeb 	     "\t%ju packet%s with valid tcp-md5 signature received\n");
738a4993b25SBjoern A. Zeeb 	p(tcps_sig_rcvbadsig,
739a4993b25SBjoern A. Zeeb 	     "\t%ju packet%s with invalid tcp-md5 signature received\n");
740a4993b25SBjoern A. Zeeb 	p(tcps_sig_err_buildsig,
741a4993b25SBjoern A. Zeeb 	     "\t%ju packet%s with tcp-md5 signature mismatch\n");
742a4993b25SBjoern A. Zeeb 	p(tcps_sig_err_sigopt,
743a4993b25SBjoern A. Zeeb 	     "\t%ju packet%s with unexpected tcp-md5 signature received\n");
744a4993b25SBjoern A. Zeeb 	p(tcps_sig_err_nosigopt,
745a4993b25SBjoern A. Zeeb 	     "\t%ju packet%s without expected tcp-md5 signature received\n");
7469b50d902SRodney W. Grimes #undef p
74722694ebaSBruce Evans #undef p1a
7489b50d902SRodney W. Grimes #undef p2
74922694ebaSBruce Evans #undef p2a
7509b50d902SRodney W. Grimes #undef p3
7519b50d902SRodney W. Grimes }
7529b50d902SRodney W. Grimes 
7539b50d902SRodney W. Grimes /*
7549b50d902SRodney W. Grimes  * Dump UDP statistics structure.
7559b50d902SRodney W. Grimes  */
7569b50d902SRodney W. Grimes void
757feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
7589b50d902SRodney W. Grimes {
759c73d99b5SRuslan Ermilov 	struct udpstat udpstat, zerostat;
7604f81ef50SGarrett Wollman 	size_t len = sizeof udpstat;
761c80211e3SAndrey V. Elsukov 	uint64_t delivered;
7629b50d902SRodney W. Grimes 
763cfa1ca9dSYoshinobu Inoue #ifdef INET6
764cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
765cfa1ca9dSYoshinobu Inoue 		return;
766cfa1ca9dSYoshinobu Inoue 	else
767cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
768cfa1ca9dSYoshinobu Inoue #endif
769cfa1ca9dSYoshinobu Inoue 
770feda1a43SJohn Baldwin 	if (live) {
771feda1a43SJohn Baldwin 		if (zflag)
772feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
773feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
774feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
775feda1a43SJohn Baldwin 			warn("sysctl: net.inet.udp.stats");
776feda1a43SJohn Baldwin 			return;
777feda1a43SJohn Baldwin 		}
778feda1a43SJohn Baldwin 	} else
7795b7cb97cSAndrey V. Elsukov 		kread_counters(off, &udpstat, len);
780feda1a43SJohn Baldwin 
7819b50d902SRodney W. Grimes 	printf("%s:\n", name);
7829b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
783c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)udpstat.f, plural(udpstat.f))
78422694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
785c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)udpstat.f)
786c80211e3SAndrey V. Elsukov 	p(udps_ipackets, "datagram%s received\n");
787c80211e3SAndrey V. Elsukov 	p1a(udps_hdrops, "with incomplete header\n");
788c80211e3SAndrey V. Elsukov 	p1a(udps_badlen, "with bad data length field\n");
789c80211e3SAndrey V. Elsukov 	p1a(udps_badsum, "with bad checksum\n");
790c80211e3SAndrey V. Elsukov 	p1a(udps_nosum, "with no checksum\n");
791c80211e3SAndrey V. Elsukov 	p1a(udps_noport, "dropped due to no socket\n");
79222694ebaSBruce Evans 	p(udps_noportbcast,
793c80211e3SAndrey V. Elsukov 	    "broadcast/multicast datagram%s undelivered\n");
794c80211e3SAndrey V. Elsukov 	p1a(udps_fullsock, "dropped due to full socket buffers\n");
795c80211e3SAndrey V. Elsukov 	p1a(udpps_pcbhashmiss, "not for hashed pcb\n");
7969b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
7979b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
7989b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
7999b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
8009b50d902SRodney W. Grimes 		    udpstat.udps_noport -
8019b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
8029b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
8039b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
804c80211e3SAndrey V. Elsukov 		printf("\t%ju delivered\n", (uint64_t)delivered);
805c80211e3SAndrey V. Elsukov 	p(udps_opackets, "datagram%s output\n");
80671498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
80771498f30SBruce M Simpson 	p(udps_filtermcast,
808c80211e3SAndrey V. Elsukov 	    "time%s multicast source filter matched\n");
8099b50d902SRodney W. Grimes #undef p
81022694ebaSBruce Evans #undef p1a
8119b50d902SRodney W. Grimes }
8129b50d902SRodney W. Grimes 
8139b50d902SRodney W. Grimes /*
814a9771948SGleb Smirnoff  * Dump CARP statistics structure.
815a9771948SGleb Smirnoff  */
816a9771948SGleb Smirnoff void
817feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
818a9771948SGleb Smirnoff {
819a9771948SGleb Smirnoff 	struct carpstats carpstat, zerostat;
820a9771948SGleb Smirnoff 	size_t len = sizeof(struct carpstats);
821a9771948SGleb Smirnoff 
822feda1a43SJohn Baldwin 	if (live) {
823a9771948SGleb Smirnoff 		if (zflag)
824a9771948SGleb Smirnoff 			memset(&zerostat, 0, len);
825a9771948SGleb Smirnoff 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
826a9771948SGleb Smirnoff 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
82745125e14SRuslan Ermilov 			if (errno != ENOENT)
828a9771948SGleb Smirnoff 				warn("sysctl: net.inet.carp.stats");
829a9771948SGleb Smirnoff 			return;
830a9771948SGleb Smirnoff 		}
831feda1a43SJohn Baldwin 	} else {
832feda1a43SJohn Baldwin 		if (off == 0)
833feda1a43SJohn Baldwin 			return;
83469edf037SAndrey V. Elsukov 		kread_counters(off, &carpstat, len);
835feda1a43SJohn Baldwin 	}
836a9771948SGleb Smirnoff 
837a9771948SGleb Smirnoff 	printf("%s:\n", name);
838a9771948SGleb Smirnoff 
839a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
8407b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
841a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
8427b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f)
843a9771948SGleb Smirnoff 
8447b95a1ebSYaroslav Tykhiy 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
8457b95a1ebSYaroslav Tykhiy 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
8467b95a1ebSYaroslav Tykhiy 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
8477b95a1ebSYaroslav Tykhiy 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
8487b95a1ebSYaroslav Tykhiy 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
8497b95a1ebSYaroslav Tykhiy 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
8507b95a1ebSYaroslav Tykhiy 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
8517b95a1ebSYaroslav Tykhiy 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
8527b95a1ebSYaroslav Tykhiy 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
8537b95a1ebSYaroslav Tykhiy 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
8547b95a1ebSYaroslav Tykhiy 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
8557b95a1ebSYaroslav Tykhiy 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
8567b95a1ebSYaroslav Tykhiy 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
857a9771948SGleb Smirnoff #if notyet
858a9771948SGleb Smirnoff 	p(carps_ostates, "\t\t%s state update%s sent\n");
859a9771948SGleb Smirnoff #endif
860a9771948SGleb Smirnoff #undef p
861a9771948SGleb Smirnoff #undef p2
862a9771948SGleb Smirnoff }
863a9771948SGleb Smirnoff 
864a9771948SGleb Smirnoff /*
8659b50d902SRodney W. Grimes  * Dump IP statistics structure.
8669b50d902SRodney W. Grimes  */
8679b50d902SRodney W. Grimes void
868feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8699b50d902SRodney W. Grimes {
870c73d99b5SRuslan Ermilov 	struct ipstat ipstat, zerostat;
8714f81ef50SGarrett Wollman 	size_t len = sizeof ipstat;
8729b50d902SRodney W. Grimes 
873feda1a43SJohn Baldwin 	if (live) {
874c73d99b5SRuslan Ermilov 		if (zflag)
875c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
876c73d99b5SRuslan Ermilov 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
877c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
8784f81ef50SGarrett Wollman 			warn("sysctl: net.inet.ip.stats");
8799b50d902SRodney W. Grimes 			return;
8804f81ef50SGarrett Wollman 		}
8815da0521fSAndrey V. Elsukov 	} else
8825da0521fSAndrey V. Elsukov 		kread_counters(off, &ipstat, len);
8834f81ef50SGarrett Wollman 
8849b50d902SRodney W. Grimes 	printf("%s:\n", name);
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
8875923c293SGleb Smirnoff     printf(m, (uintmax_t )ipstat.f, plural(ipstat.f))
88822694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
8895923c293SGleb Smirnoff     printf(m, (uintmax_t )ipstat.f)
8909b50d902SRodney W. Grimes 
8915923c293SGleb Smirnoff 	p(ips_total, "\t%ju total packet%s received\n");
8925923c293SGleb Smirnoff 	p(ips_badsum, "\t%ju bad header checksum%s\n");
8935923c293SGleb Smirnoff 	p1a(ips_toosmall, "\t%ju with size smaller than minimum\n");
8945923c293SGleb Smirnoff 	p1a(ips_tooshort, "\t%ju with data size < data length\n");
8955923c293SGleb Smirnoff 	p1a(ips_toolong, "\t%ju with ip length > max ip packet size\n");
8965923c293SGleb Smirnoff 	p1a(ips_badhlen, "\t%ju with header length < data size\n");
8975923c293SGleb Smirnoff 	p1a(ips_badlen, "\t%ju with data length < header length\n");
8985923c293SGleb Smirnoff 	p1a(ips_badoptions, "\t%ju with bad options\n");
8995923c293SGleb Smirnoff 	p1a(ips_badvers, "\t%ju with incorrect version number\n");
9005923c293SGleb Smirnoff 	p(ips_fragments, "\t%ju fragment%s received\n");
9015923c293SGleb Smirnoff 	p(ips_fragdropped, "\t%ju fragment%s dropped (dup or out of space)\n");
9025923c293SGleb Smirnoff 	p(ips_fragtimeout, "\t%ju fragment%s dropped after timeout\n");
9035923c293SGleb Smirnoff 	p(ips_reassembled, "\t%ju packet%s reassembled ok\n");
9045923c293SGleb Smirnoff 	p(ips_delivered, "\t%ju packet%s for this host\n");
9055923c293SGleb Smirnoff 	p(ips_noproto, "\t%ju packet%s for unknown/unsupported protocol\n");
9065923c293SGleb Smirnoff 	p(ips_forward, "\t%ju packet%s forwarded");
9075923c293SGleb Smirnoff 	p(ips_fastforward, " (%ju packet%s fast forwarded)");
908958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
909958d6f7fSPierre Beyssac 		putchar('\n');
9105923c293SGleb Smirnoff 	p(ips_cantforward, "\t%ju packet%s not forwardable\n");
911a5969f5fSGarrett Wollman 	p(ips_notmember,
9125923c293SGleb Smirnoff 	    "\t%ju packet%s received for unknown multicast group\n");
9135923c293SGleb Smirnoff 	p(ips_redirectsent, "\t%ju redirect%s sent\n");
9145923c293SGleb Smirnoff 	p(ips_localout, "\t%ju packet%s sent from this host\n");
9155923c293SGleb Smirnoff 	p(ips_rawout, "\t%ju packet%s sent with fabricated ip header\n");
916a5969f5fSGarrett Wollman 	p(ips_odropped,
9175923c293SGleb Smirnoff 	    "\t%ju output packet%s dropped due to no bufs, etc.\n");
9185923c293SGleb Smirnoff 	p(ips_noroute, "\t%ju output packet%s discarded due to no route\n");
9195923c293SGleb Smirnoff 	p(ips_fragmented, "\t%ju output datagram%s fragmented\n");
9205923c293SGleb Smirnoff 	p(ips_ofragments, "\t%ju fragment%s created\n");
9215923c293SGleb Smirnoff 	p(ips_cantfrag, "\t%ju datagram%s that can't be fragmented\n");
9225923c293SGleb Smirnoff 	p(ips_nogif, "\t%ju tunneling packet%s that can't find gif\n");
9235923c293SGleb Smirnoff 	p(ips_badaddr, "\t%ju datagram%s with bad address in header\n");
9249b50d902SRodney W. Grimes #undef p
92522694ebaSBruce Evans #undef p1a
9269b50d902SRodney W. Grimes }
9279b50d902SRodney W. Grimes 
92854fc657dSGeorge V. Neville-Neil /*
92954fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
93054fc657dSGeorge V. Neville-Neil  */
93154fc657dSGeorge V. Neville-Neil void
93254fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
93354fc657dSGeorge V. Neville-Neil {
93454fc657dSGeorge V. Neville-Neil 	struct arpstat arpstat, zerostat;
93554fc657dSGeorge V. Neville-Neil 	size_t len = sizeof(arpstat);
93654fc657dSGeorge V. Neville-Neil 
93754fc657dSGeorge V. Neville-Neil 	if (live) {
93854fc657dSGeorge V. Neville-Neil 		if (zflag)
93954fc657dSGeorge V. Neville-Neil 			memset(&zerostat, 0, len);
94054fc657dSGeorge V. Neville-Neil 		if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
94154fc657dSGeorge V. Neville-Neil 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
94254fc657dSGeorge V. Neville-Neil 			warn("sysctl: net.link.ether.arp.stats");
94354fc657dSGeorge V. Neville-Neil 			return;
94454fc657dSGeorge V. Neville-Neil 		}
94554fc657dSGeorge V. Neville-Neil 	} else
9465b7cb97cSAndrey V. Elsukov 		kread_counters(off, &arpstat, len);
94754fc657dSGeorge V. Neville-Neil 
94854fc657dSGeorge V. Neville-Neil 	printf("%s:\n", name);
94954fc657dSGeorge V. Neville-Neil 
95054fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
951c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)arpstat.f, plural(arpstat.f))
95254fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
953c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
95454fc657dSGeorge V. Neville-Neil 
955c80211e3SAndrey V. Elsukov 	p(txrequests, "ARP request%s sent\n");
956c80211e3SAndrey V. Elsukov 	p2(txreplies, "ARP repl%s sent\n");
957c80211e3SAndrey V. Elsukov 	p(rxrequests, "ARP request%s received\n");
958c80211e3SAndrey V. Elsukov 	p2(rxreplies, "ARP repl%s received\n");
959c80211e3SAndrey V. Elsukov 	p(received, "ARP packet%s received\n");
960c80211e3SAndrey V. Elsukov 	p(dropped, "total packet%s dropped due to no ARP entry\n");
961c80211e3SAndrey V. Elsukov 	p(timeouts, "ARP entry%s timed out\n");
962c80211e3SAndrey V. Elsukov 	p(dupips, "Duplicate IP%s seen\n");
96354fc657dSGeorge V. Neville-Neil #undef p
96454fc657dSGeorge V. Neville-Neil #undef p2
96554fc657dSGeorge V. Neville-Neil }
96654fc657dSGeorge V. Neville-Neil 
96754fc657dSGeorge V. Neville-Neil 
96854fc657dSGeorge V. Neville-Neil 
9694063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
9704063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
9719b50d902SRodney W. Grimes 	"#1",
9729b50d902SRodney W. Grimes 	"#2",
9734063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
9744063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
9754063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
9769b50d902SRodney W. Grimes 	"#6",
9779b50d902SRodney W. Grimes 	"#7",
9784063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
9794063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
9804063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
9814063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
9824063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
9834063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
9844063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
9854063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
9864063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
9874063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
9884063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
9894063583aSMaxim Konovalov 	"#19",
9904063583aSMaxim Konovalov 	"#20",
9914063583aSMaxim Konovalov 	"#21",
9924063583aSMaxim Konovalov 	"#22",
9934063583aSMaxim Konovalov 	"#23",
9944063583aSMaxim Konovalov 	"#24",
9954063583aSMaxim Konovalov 	"#25",
9964063583aSMaxim Konovalov 	"#26",
9974063583aSMaxim Konovalov 	"#27",
9984063583aSMaxim Konovalov 	"#28",
9994063583aSMaxim Konovalov 	"#29",
10004063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
10014063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
10024063583aSMaxim Konovalov 	"mobile host redirect",
10034063583aSMaxim Konovalov 	"IPv6 where-are-you",
10044063583aSMaxim Konovalov 	"IPv6 i-am-here",
10054063583aSMaxim Konovalov 	"mobile registration req",
10064063583aSMaxim Konovalov 	"mobile registration reply",
10074063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
10084063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
10094063583aSMaxim Konovalov 	"icmp SKIP",
10104063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
10119b50d902SRodney W. Grimes };
10129b50d902SRodney W. Grimes 
10139b50d902SRodney W. Grimes /*
10149b50d902SRodney W. Grimes  * Dump ICMP statistics.
10159b50d902SRodney W. Grimes  */
10169b50d902SRodney W. Grimes void
1017feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
10189b50d902SRodney W. Grimes {
1019c73d99b5SRuslan Ermilov 	struct icmpstat icmpstat, zerostat;
10204e00c309SGarrett Wollman 	int i, first;
10214e00c309SGarrett Wollman 	size_t len;
10229b50d902SRodney W. Grimes 
10234e00c309SGarrett Wollman 	len = sizeof icmpstat;
1024feda1a43SJohn Baldwin 	if (live) {
1025c73d99b5SRuslan Ermilov 		if (zflag)
1026c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
1027feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
1028c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1029c73d99b5SRuslan Ermilov 			warn("sysctl: net.inet.icmp.stats");
1030c73d99b5SRuslan Ermilov 			return;
1031c73d99b5SRuslan Ermilov 		}
1032feda1a43SJohn Baldwin 	} else
10335b7cb97cSAndrey V. Elsukov 		kread_counters(off, &icmpstat, len);
10344e00c309SGarrett Wollman 
10359b50d902SRodney W. Grimes 	printf("%s:\n", name);
10369b50d902SRodney W. Grimes 
10379b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
10389b50d902SRodney W. Grimes     printf(m, icmpstat.f, plural(icmpstat.f))
103922694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
104022694ebaSBruce Evans     printf(m, icmpstat.f)
1041bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1042bd714208SRuslan Ermilov     printf(m, icmpstat.f, plurales(icmpstat.f))
10439b50d902SRodney W. Grimes 
10447d56c0eeSAlexander Langer 	p(icps_error, "\t%lu call%s to icmp_error\n");
10459b50d902SRodney W. Grimes 	p(icps_oldicmp,
1046f99a4046SMike Makonnen 	    "\t%lu error%s not generated in response to an icmp message\n");
10479b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10489b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
10499b50d902SRodney W. Grimes 			if (first) {
10509b50d902SRodney W. Grimes 				printf("\tOutput histogram:\n");
10519b50d902SRodney W. Grimes 				first = 0;
10529b50d902SRodney W. Grimes 			}
10534063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10547d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10559b50d902SRodney W. Grimes 					icmpstat.icps_outhist[i]);
10564063583aSMaxim Konovalov 			else
10574063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10584063583aSMaxim Konovalov 					icmpstat.icps_outhist[i]);
10599b50d902SRodney W. Grimes 		}
10607d56c0eeSAlexander Langer 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
1061bc215f59SDavid E. O'Brien 	p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
1062bc215f59SDavid E. O'Brien 	p(icps_checksum, "\t%lu message%s with bad checksum\n");
10637d56c0eeSAlexander Langer 	p(icps_badlen, "\t%lu message%s with bad length\n");
106422694ebaSBruce Evans 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
106522694ebaSBruce Evans 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
10669b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10679b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
10689b50d902SRodney W. Grimes 			if (first) {
10699b50d902SRodney W. Grimes 				printf("\tInput histogram:\n");
10709b50d902SRodney W. Grimes 				first = 0;
10719b50d902SRodney W. Grimes 			}
10724063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10737d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10749b50d902SRodney W. Grimes 				    icmpstat.icps_inhist[i]);
10754063583aSMaxim Konovalov 			else
10764063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10774063583aSMaxim Konovalov 				    icmpstat.icps_inhist[i]);
10789b50d902SRodney W. Grimes 		}
10797d56c0eeSAlexander Langer 	p(icps_reflect, "\t%lu message response%s generated\n");
1080bd714208SRuslan Ermilov 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
10810237ca7bSRuslan Ermilov 	p(icps_noroute, "\t%lu no return route%s\n");
10829b50d902SRodney W. Grimes #undef p
108322694ebaSBruce Evans #undef p1a
1084bd714208SRuslan Ermilov #undef p2
1085feda1a43SJohn Baldwin 	if (live) {
10864e00c309SGarrett Wollman 		len = sizeof i;
1087feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1088feda1a43SJohn Baldwin 		    0)
10894e00c309SGarrett Wollman 			return;
10904e00c309SGarrett Wollman 		printf("\tICMP address mask responses are %sabled\n",
10914e00c309SGarrett Wollman 		    i ? "en" : "dis");
10929b50d902SRodney W. Grimes 	}
1093feda1a43SJohn Baldwin }
10949b50d902SRodney W. Grimes 
1095d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
10969b50d902SRodney W. Grimes /*
1097d10910e6SBruce M Simpson  * Dump IGMP statistics structure (pre 8.x kernel).
10989b50d902SRodney W. Grimes  */
1099d10910e6SBruce M Simpson static void
110096c3073fSXin LI igmp_stats_live_old(const char *name)
11019b50d902SRodney W. Grimes {
1102d10910e6SBruce M Simpson 	struct oigmpstat oigmpstat, zerostat;
1103d10910e6SBruce M Simpson 	size_t len = sizeof(oigmpstat);
11049b50d902SRodney W. Grimes 
1105c73d99b5SRuslan Ermilov 	if (zflag)
1106c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
1107d10910e6SBruce M Simpson 	if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1108c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
11094f81ef50SGarrett Wollman 		warn("sysctl: net.inet.igmp.stats");
11109b50d902SRodney W. Grimes 		return;
11114f81ef50SGarrett Wollman 	}
11124f81ef50SGarrett Wollman 
11139b50d902SRodney W. Grimes 	printf("%s:\n", name);
11149b50d902SRodney W. Grimes 
1115d10910e6SBruce M Simpson #define	p(f, m) if (oigmpstat.f || sflag <= 1) \
1116d10910e6SBruce M Simpson     printf(m, oigmpstat.f, plural(oigmpstat.f))
1117d10910e6SBruce M Simpson #define	py(f, m) if (oigmpstat.f || sflag <= 1) \
1118d10910e6SBruce M Simpson     printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
11199b50d902SRodney W. Grimes 	p(igps_rcv_total, "\t%u message%s received\n");
11209b50d902SRodney W. Grimes 	p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
11219b50d902SRodney W. Grimes 	p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
11229b50d902SRodney W. Grimes 	py(igps_rcv_queries, "\t%u membership quer%s received\n");
11233feeb332SDavid E. O'Brien 	py(igps_rcv_badqueries,
11243feeb332SDavid E. O'Brien 	    "\t%u membership quer%s received with invalid field(s)\n");
11259b50d902SRodney W. Grimes 	p(igps_rcv_reports, "\t%u membership report%s received\n");
11263feeb332SDavid E. O'Brien 	p(igps_rcv_badreports,
11273feeb332SDavid E. O'Brien 	    "\t%u membership report%s received with invalid field(s)\n");
11283feeb332SDavid E. O'Brien 	p(igps_rcv_ourreports,
11293feeb332SDavid E. O'Brien "\t%u membership report%s received for groups to which we belong\n");
11309b50d902SRodney W. Grimes         p(igps_snd_reports, "\t%u membership report%s sent\n");
11319b50d902SRodney W. Grimes #undef p
11329b50d902SRodney W. Grimes #undef py
11339b50d902SRodney W. Grimes }
1134d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1135d10910e6SBruce M Simpson 
1136d10910e6SBruce M Simpson /*
1137d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1138d10910e6SBruce M Simpson  */
1139d10910e6SBruce M Simpson void
1140d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1141d10910e6SBruce M Simpson {
1142d10910e6SBruce M Simpson 	struct igmpstat igmpstat, zerostat;
1143d10910e6SBruce M Simpson 	size_t len;
1144d10910e6SBruce M Simpson 
1145d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
1146d10910e6SBruce M Simpson 	if (live) {
1147d10910e6SBruce M Simpson 		/*
1148d10910e6SBruce M Simpson 		 * Detect if we are being run against a pre-IGMPv3 kernel.
1149d10910e6SBruce M Simpson 		 * We cannot do this for a core file as the legacy
1150d10910e6SBruce M Simpson 		 * struct igmpstat has no size field, nor does it
1151d10910e6SBruce M Simpson 		 * export it in any readily-available symbols.
1152d10910e6SBruce M Simpson 		 */
1153d10910e6SBruce M Simpson 		len = 0;
1154d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1155d10910e6SBruce M Simpson 		    0) < 0) {
1156d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1157d10910e6SBruce M Simpson 			return;
1158d10910e6SBruce M Simpson 		}
1159d10910e6SBruce M Simpson 		if (len < sizeof(igmpstat)) {
116096c3073fSXin LI 			igmp_stats_live_old(name);
1161d10910e6SBruce M Simpson 			return;
1162d10910e6SBruce M Simpson 		}
1163d10910e6SBruce M Simpson 	}
1164d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1165d10910e6SBruce M Simpson 
1166d10910e6SBruce M Simpson 	len = sizeof(igmpstat);
1167d10910e6SBruce M Simpson 	if (live) {
1168d10910e6SBruce M Simpson 		if (zflag)
1169d10910e6SBruce M Simpson 			memset(&zerostat, 0, len);
1170d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1171d10910e6SBruce M Simpson 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1172d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1173d10910e6SBruce M Simpson 			return;
1174d10910e6SBruce M Simpson 		}
1175d10910e6SBruce M Simpson 	} else {
1176d10910e6SBruce M Simpson 		len = sizeof(igmpstat);
1177d10910e6SBruce M Simpson 		kread(off, &igmpstat, len);
1178d10910e6SBruce M Simpson 	}
1179d10910e6SBruce M Simpson 
1180d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1181d10910e6SBruce M Simpson 		warnx("%s: version mismatch (%d != %d)", __func__,
1182d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1183d10910e6SBruce M Simpson 	}
1184d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1185d10910e6SBruce M Simpson 		warnx("%s: size mismatch (%d != %d)", __func__,
1186d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1187d10910e6SBruce M Simpson 	}
1188d10910e6SBruce M Simpson 
1189d10910e6SBruce M Simpson 	printf("%s:\n", name);
1190d10910e6SBruce M Simpson 
1191d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1192d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1193d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1194d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1195d10910e6SBruce M Simpson 	p64(igps_rcv_total, "\t%ju message%s received\n");
1196d10910e6SBruce M Simpson 	p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1197d10910e6SBruce M Simpson 	p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1198d10910e6SBruce M Simpson 	p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1199d10910e6SBruce M Simpson 	py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1200d10910e6SBruce M Simpson 	py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1201d10910e6SBruce M Simpson 	py64(igps_rcv_badqueries,
1202d10910e6SBruce M Simpson 	    "\t%ju membership quer%s received with invalid field(s)\n");
1203d10910e6SBruce M Simpson 	py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1204d10910e6SBruce M Simpson 	py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1205d10910e6SBruce M Simpson 	py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1206d10910e6SBruce M Simpson 	py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1207d10910e6SBruce M Simpson 	p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1208d10910e6SBruce M Simpson 	p64(igps_rcv_badreports,
1209d10910e6SBruce M Simpson 	    "\t%ju membership report%s received with invalid field(s)\n");
1210d10910e6SBruce M Simpson 	p64(igps_rcv_ourreports,
1211d10910e6SBruce M Simpson "\t%ju membership report%s received for groups to which we belong\n");
1212d10910e6SBruce M Simpson         p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1213d10910e6SBruce M Simpson         p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1214d10910e6SBruce M Simpson #undef p64
1215d10910e6SBruce M Simpson #undef py64
1216d10910e6SBruce M Simpson }
12179b50d902SRodney W. Grimes 
12189b50d902SRodney W. Grimes /*
1219c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1220c7b9b5bbSJeffrey Hsu  */
1221c7b9b5bbSJeffrey Hsu void
1222feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1223feda1a43SJohn Baldwin     int proto __unused)
1224c7b9b5bbSJeffrey Hsu {
1225c7b9b5bbSJeffrey Hsu 	struct pimstat pimstat, zerostat;
1226c7b9b5bbSJeffrey Hsu 	size_t len = sizeof pimstat;
1227c7b9b5bbSJeffrey Hsu 
1228feda1a43SJohn Baldwin 	if (live) {
1229c7b9b5bbSJeffrey Hsu 		if (zflag)
1230c7b9b5bbSJeffrey Hsu 			memset(&zerostat, 0, len);
1231c7b9b5bbSJeffrey Hsu 		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1232c7b9b5bbSJeffrey Hsu 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
12336d7c0d2fSDag-Erling Smørgrav 			if (errno != ENOENT)
1234c7b9b5bbSJeffrey Hsu 				warn("sysctl: net.inet.pim.stats");
1235c7b9b5bbSJeffrey Hsu 			return;
1236c7b9b5bbSJeffrey Hsu 		}
1237feda1a43SJohn Baldwin 	} else {
1238feda1a43SJohn Baldwin 		if (off == 0)
1239feda1a43SJohn Baldwin 			return;
12405b7cb97cSAndrey V. Elsukov 		kread_counters(off, &pimstat, len);
1241feda1a43SJohn Baldwin 	}
1242c7b9b5bbSJeffrey Hsu 
1243c7b9b5bbSJeffrey Hsu 	printf("%s:\n", name);
1244c7b9b5bbSJeffrey Hsu 
1245c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
12467b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1247c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
12487b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
12497b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
12507b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
12517b95a1ebSYaroslav Tykhiy 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
12527b95a1ebSYaroslav Tykhiy         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
12537b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
12547b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
12557b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
12563feeb332SDavid E. O'Brien 	p(pims_rcv_registers_wrongiif,
12573feeb332SDavid E. O'Brien 	    "\t%ju data register message%s received on wrong iif\n");
12587b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
12597b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
12607b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1261c7b9b5bbSJeffrey Hsu #undef p
1262c7b9b5bbSJeffrey Hsu #undef py
1263c7b9b5bbSJeffrey Hsu }
1264c7b9b5bbSJeffrey Hsu 
1265c7b9b5bbSJeffrey Hsu /*
12669b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
12679b50d902SRodney W. Grimes  */
12689b50d902SRodney W. Grimes void
1269a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port)
12709b50d902SRodney W. Grimes {
12719b50d902SRodney W. Grimes 	struct servent *sp = 0;
12729b50d902SRodney W. Grimes 	char line[80], *cp;
1273cfa1ca9dSYoshinobu Inoue 	int width;
12749b50d902SRodney W. Grimes 
1275080b7f49SDag-Erling Smørgrav 	if (Wflag)
1276080b7f49SDag-Erling Smørgrav 	    sprintf(line, "%s.", inetname(in));
1277080b7f49SDag-Erling Smørgrav 	else
1278a01e3379SDavid Malone 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1279b3608ae1SEd Schouten 	cp = strchr(line, '\0');
1280a01e3379SDavid Malone 	if (!num_port && port)
12819b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
12829b50d902SRodney W. Grimes 	if (sp || port == 0)
12831ef69972SAndrey A. Chernov 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
12849b50d902SRodney W. Grimes 	else
12859b50d902SRodney W. Grimes 		sprintf(cp, "%d ", ntohs((u_short)port));
1286080b7f49SDag-Erling Smørgrav 	width = (Aflag && !Wflag) ? 18 : 22;
1287080b7f49SDag-Erling Smørgrav 	if (Wflag)
1288080b7f49SDag-Erling Smørgrav 	    printf("%-*s ", width, line);
1289080b7f49SDag-Erling Smørgrav 	else
1290cfa1ca9dSYoshinobu Inoue 	    printf("%-*.*s ", width, width, line);
12919b50d902SRodney W. Grimes }
12929b50d902SRodney W. Grimes 
12939b50d902SRodney W. Grimes /*
12949b50d902SRodney W. Grimes  * Construct an Internet address representation.
129565ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
12969b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
12979b50d902SRodney W. Grimes  */
12989b50d902SRodney W. Grimes char *
12995e051718SAssar Westerlund inetname(struct in_addr *inp)
13009b50d902SRodney W. Grimes {
1301a01e3379SDavid Malone 	char *cp;
1302d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
13039b50d902SRodney W. Grimes 	struct hostent *hp;
13049b50d902SRodney W. Grimes 	struct netent *np;
13059b50d902SRodney W. Grimes 
13069b50d902SRodney W. Grimes 	cp = 0;
130765ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
13089b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
13099b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
13109b50d902SRodney W. Grimes 
13119b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
13129b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
13139b50d902SRodney W. Grimes 			if (np)
13149b50d902SRodney W. Grimes 				cp = np->n_name;
13159b50d902SRodney W. Grimes 		}
13169b50d902SRodney W. Grimes 		if (cp == 0) {
13179b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
13189b50d902SRodney W. Grimes 			if (hp) {
13199b50d902SRodney W. Grimes 				cp = hp->h_name;
1320d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
13219b50d902SRodney W. Grimes 			}
13229b50d902SRodney W. Grimes 		}
13239b50d902SRodney W. Grimes 	}
13249b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
13259b50d902SRodney W. Grimes 		strcpy(line, "*");
13269a1f6729SWarner Losh 	else if (cp) {
13271c109628SXin LI 		strlcpy(line, cp, sizeof(line));
13289a1f6729SWarner Losh 	} else {
13299b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
133022694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
133122694ebaSBruce Evans 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
13329b50d902SRodney W. Grimes 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
13339b50d902SRodney W. Grimes 	}
13349b50d902SRodney W. Grimes 	return (line);
13359b50d902SRodney W. Grimes }
1336