xref: /freebsd/usr.bin/netstat/inet.c (revision 65475bc8e6c268f6054a4ec9cfdb277c2c58cac3)
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>
469b50d902SRodney W. Grimes #include <sys/socket.h>
479b50d902SRodney W. Grimes #include <sys/socketvar.h>
484e00c309SGarrett Wollman #include <sys/sysctl.h>
499b50d902SRodney W. Grimes #include <sys/protosw.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <net/route.h>
529b50d902SRodney W. Grimes #include <netinet/in.h>
539b50d902SRodney W. Grimes #include <netinet/in_systm.h>
549b50d902SRodney W. Grimes #include <netinet/ip.h>
55a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
56cfa1ca9dSYoshinobu Inoue #ifdef INET6
57cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
58cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
599b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
609b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
619b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
629b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
639b50d902SRodney W. Grimes #include <netinet/ip_var.h>
64c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
659b50d902SRodney W. Grimes #include <netinet/tcp.h>
669b50d902SRodney W. Grimes #include <netinet/tcpip.h>
679b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
689b50d902SRodney W. Grimes #define	TCPSTATES
699b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
709b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
719b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
729b50d902SRodney W. Grimes #include <netinet/tcp_debug.h>
739b50d902SRodney W. Grimes #include <netinet/udp.h>
749b50d902SRodney W. Grimes #include <netinet/udp_var.h>
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes #include <arpa/inet.h>
774f81ef50SGarrett Wollman #include <err.h>
784f81ef50SGarrett Wollman #include <errno.h>
79d121b556SBrian Somers #include <libutil.h>
809b50d902SRodney W. Grimes #include <netdb.h>
817b95a1ebSYaroslav Tykhiy #include <stdint.h>
829b50d902SRodney W. Grimes #include <stdio.h>
834f81ef50SGarrett Wollman #include <stdlib.h>
849b50d902SRodney W. Grimes #include <string.h>
859b50d902SRodney W. Grimes #include <unistd.h>
869b50d902SRodney W. Grimes #include "netstat.h"
879b50d902SRodney W. Grimes 
885e051718SAssar Westerlund char	*inetname(struct in_addr *);
89a01e3379SDavid Malone void	inetprint(struct in_addr *, int, const char *, int);
90cfa1ca9dSYoshinobu Inoue #ifdef INET6
91cfa1ca9dSYoshinobu Inoue static int udp_done, tcp_done;
92cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
939b50d902SRodney W. Grimes 
94feda1a43SJohn Baldwin static int
95feda1a43SJohn Baldwin pcblist_sysctl(int proto, char **bufp, int istcp)
96feda1a43SJohn Baldwin {
97feda1a43SJohn Baldwin 	const char *mibvar;
98feda1a43SJohn Baldwin 	char *buf;
99feda1a43SJohn Baldwin 	size_t len;
100feda1a43SJohn Baldwin 
101feda1a43SJohn Baldwin 	switch (proto) {
102feda1a43SJohn Baldwin 	case IPPROTO_TCP:
103feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
104feda1a43SJohn Baldwin 		break;
105feda1a43SJohn Baldwin 	case IPPROTO_UDP:
106feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
107feda1a43SJohn Baldwin 		break;
108feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
109feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
110feda1a43SJohn Baldwin 		break;
111feda1a43SJohn Baldwin 	default:
112feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
113feda1a43SJohn Baldwin 		break;
114feda1a43SJohn Baldwin 	}
115feda1a43SJohn Baldwin 
116feda1a43SJohn Baldwin 	len = 0;
117feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118feda1a43SJohn Baldwin 		if (errno != ENOENT)
119feda1a43SJohn Baldwin 			warn("sysctl: %s", mibvar);
120feda1a43SJohn Baldwin 		return (0);
121feda1a43SJohn Baldwin 	}
122feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
123feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
124feda1a43SJohn Baldwin 		return (0);
125feda1a43SJohn Baldwin 	}
126feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127feda1a43SJohn Baldwin 		warn("sysctl: %s", mibvar);
128feda1a43SJohn Baldwin 		free(buf);
129feda1a43SJohn Baldwin 		return (0);
130feda1a43SJohn Baldwin 	}
131feda1a43SJohn Baldwin 	*bufp = buf;
132feda1a43SJohn Baldwin 	return (1);
133feda1a43SJohn Baldwin }
134feda1a43SJohn Baldwin 
135feda1a43SJohn Baldwin /*
136feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
137feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
138feda1a43SJohn Baldwin  */
139feda1a43SJohn Baldwin static void
140feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141feda1a43SJohn Baldwin {
142feda1a43SJohn Baldwin 	xsb->sb_cc = sb->sb_cc;
143feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
144feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
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);
258feda1a43SJohn Baldwin 			else if (inp->inp_vflag & 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;
3139b50d902SRodney W. Grimes 
3144f81ef50SGarrett Wollman 	istcp = 0;
3154f81ef50SGarrett Wollman 	switch (proto) {
3164f81ef50SGarrett Wollman 	case IPPROTO_TCP:
317cfa1ca9dSYoshinobu Inoue #ifdef INET6
318cfa1ca9dSYoshinobu Inoue 		if (tcp_done != 0)
319cfa1ca9dSYoshinobu Inoue 			return;
320cfa1ca9dSYoshinobu Inoue 		else
321cfa1ca9dSYoshinobu Inoue 			tcp_done = 1;
322cfa1ca9dSYoshinobu Inoue #endif
3234f81ef50SGarrett Wollman 		istcp = 1;
3244f81ef50SGarrett Wollman 		break;
3254f81ef50SGarrett Wollman 	case IPPROTO_UDP:
326cfa1ca9dSYoshinobu Inoue #ifdef INET6
327cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
328cfa1ca9dSYoshinobu Inoue 			return;
329cfa1ca9dSYoshinobu Inoue 		else
330cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
331cfa1ca9dSYoshinobu Inoue #endif
3324f81ef50SGarrett Wollman 		break;
3334f81ef50SGarrett Wollman 	}
334feda1a43SJohn Baldwin 	if (live) {
335feda1a43SJohn Baldwin 		if (!pcblist_sysctl(proto, &buf, istcp))
3369b50d902SRodney W. Grimes 			return;
337feda1a43SJohn Baldwin 	} else {
338feda1a43SJohn Baldwin 		if (!pcblist_kvm(off, &buf, istcp))
3394f81ef50SGarrett Wollman 			return;
3404f81ef50SGarrett Wollman 	}
3414f81ef50SGarrett Wollman 
3424f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
3434f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
3444f81ef50SGarrett Wollman 	     xig->xig_len > sizeof(struct xinpgen);
3454f81ef50SGarrett Wollman 	     xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
3469b50d902SRodney W. Grimes 		if (istcp) {
3474f81ef50SGarrett Wollman 			tp = &((struct xtcpcb *)xig)->xt_tp;
3484f81ef50SGarrett Wollman 			inp = &((struct xtcpcb *)xig)->xt_inp;
3494f81ef50SGarrett Wollman 			so = &((struct xtcpcb *)xig)->xt_socket;
3504f81ef50SGarrett Wollman 		} else {
3514f81ef50SGarrett Wollman 			inp = &((struct xinpcb *)xig)->xi_inp;
3524f81ef50SGarrett Wollman 			so = &((struct xinpcb *)xig)->xi_socket;
3539b50d902SRodney W. Grimes 		}
3544f81ef50SGarrett Wollman 
3554f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
356feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
3574f81ef50SGarrett Wollman 			continue;
3584f81ef50SGarrett Wollman 
3594f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
3604f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
3614f81ef50SGarrett Wollman 			continue;
3624f81ef50SGarrett Wollman 
363a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
364cfa1ca9dSYoshinobu Inoue #ifdef INET6
365a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
366cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
367a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
368cfa1ca9dSYoshinobu Inoue #ifdef INET6
369cfa1ca9dSYoshinobu Inoue 					    && (inp->inp_vflag &
370cfa1ca9dSYoshinobu Inoue 						INP_IPV6) == 0
371cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
372cfa1ca9dSYoshinobu Inoue 			))
373cfa1ca9dSYoshinobu Inoue 		    )
374cfa1ca9dSYoshinobu Inoue 			continue;
375cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
376cfa1ca9dSYoshinobu Inoue 		    (
3772b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
3782b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
379cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
380cfa1ca9dSYoshinobu Inoue #ifdef INET6
381a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
382cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
383cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
384a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
385cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
386cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
387cfa1ca9dSYoshinobu Inoue #ifdef INET6
388cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
389cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
390cfa1ca9dSYoshinobu Inoue #endif
391cfa1ca9dSYoshinobu Inoue 			  ))
392cfa1ca9dSYoshinobu Inoue 		     ))
3934f81ef50SGarrett Wollman 			continue;
3944f81ef50SGarrett Wollman 
3959b50d902SRodney W. Grimes 		if (first) {
396ac55add0SGuido van Rooij 			if (!Lflag) {
3979b50d902SRodney W. Grimes 				printf("Active Internet connections");
3989b50d902SRodney W. Grimes 				if (aflag)
3999b50d902SRodney W. Grimes 					printf(" (including servers)");
400ac55add0SGuido van Rooij 			} else
401ac55add0SGuido van Rooij 				printf(
402ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
4039b50d902SRodney W. Grimes 			putchar('\n');
4049b50d902SRodney W. Grimes 			if (Aflag)
405612d2129SAndre Oppermann 				printf("%-8.8s ", "Tcpcb");
406ac55add0SGuido van Rooij 			if (Lflag)
407fb5d0fbdSRuslan Ermilov 				printf("%-5.5s %-14.14s %-22.22s\n",
408fb5d0fbdSRuslan Ermilov 					"Proto", "Listen", "Local Address");
409ac55add0SGuido van Rooij 			else
410080b7f49SDag-Erling Smørgrav 				printf((Aflag && !Wflag) ?
411cfa1ca9dSYoshinobu Inoue 		"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
412cfa1ca9dSYoshinobu Inoue 		"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
4139b50d902SRodney W. Grimes 					"Proto", "Recv-Q", "Send-Q",
414ac55add0SGuido van Rooij 					"Local Address", "Foreign Address",
415ac55add0SGuido van Rooij 					"(state)");
4169b50d902SRodney W. Grimes 			first = 0;
4179b50d902SRodney W. Grimes 		}
418fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
419fb5d0fbdSRuslan Ermilov 			continue;
420e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
421e4ec3989SDag-Erling Smørgrav 			if (istcp)
422e4ec3989SDag-Erling Smørgrav 				printf("%8lx ", (u_long)inp->inp_ppcb);
423e4ec3989SDag-Erling Smørgrav 			else
4244f81ef50SGarrett Wollman 				printf("%8lx ", (u_long)so->so_pcb);
425e4ec3989SDag-Erling Smørgrav 		}
426cfa1ca9dSYoshinobu Inoue #ifdef INET6
427fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
428fc60ab7bSYoshinobu Inoue 			vchar = ((inp->inp_vflag & INP_IPV4) != 0)
429fc60ab7bSYoshinobu Inoue 				? "46" : "6 ";
430fc60ab7bSYoshinobu Inoue 		else
431cfa1ca9dSYoshinobu Inoue #endif
432fc60ab7bSYoshinobu Inoue 		vchar = ((inp->inp_vflag & INP_IPV4) != 0)
433fc60ab7bSYoshinobu Inoue 				? "4 " : "  ";
434fb5d0fbdSRuslan Ermilov 		printf("%-3.3s%-2.2s ", name, vchar);
435fb5d0fbdSRuslan Ermilov 		if (Lflag) {
436a01e3379SDavid Malone 			char buf1[15];
437fc60ab7bSYoshinobu Inoue 
438a01e3379SDavid Malone 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
439fb5d0fbdSRuslan Ermilov 				 so->so_incqlen, so->so_qlimit);
440a01e3379SDavid Malone 			printf("%-14.14s ", buf1);
441fb5d0fbdSRuslan Ermilov 		} else {
442f824b518SJohn Polstra 			printf("%6u %6u  ",
443cfa1ca9dSYoshinobu Inoue 			       so->so_rcv.sb_cc,
4444f81ef50SGarrett Wollman 			       so->so_snd.sb_cc);
445fc60ab7bSYoshinobu Inoue 		}
44665ea0024SAssar Westerlund 		if (numeric_port) {
447cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4484f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4498d612dd2SPoul-Henning Kamp 					  name, 1);
450ac55add0SGuido van Rooij 				if (!Lflag)
451ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
452ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name, 1);
453cfa1ca9dSYoshinobu Inoue 			}
454cfa1ca9dSYoshinobu Inoue #ifdef INET6
455cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
456cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
457cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 1);
458ac55add0SGuido van Rooij 				if (!Lflag)
459cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
460cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name, 1);
461cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
462cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4634f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
464cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4654f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4668d612dd2SPoul-Henning Kamp 					  name, 1);
467ac55add0SGuido van Rooij 				if (!Lflag)
468ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
469ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name, 0);
470cfa1ca9dSYoshinobu Inoue 			}
471cfa1ca9dSYoshinobu Inoue #ifdef INET6
472cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
473cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
474cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 1);
475ac55add0SGuido van Rooij 				if (!Lflag)
476cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
477cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name, 0);
478cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
479cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4808d612dd2SPoul-Henning Kamp 		} else {
481cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4824f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4838d612dd2SPoul-Henning Kamp 					  name, 0);
484ac55add0SGuido van Rooij 				if (!Lflag)
485ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
486ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name,
487ac55add0SGuido van Rooij 						  inp->inp_lport !=
488ac55add0SGuido van Rooij 							inp->inp_fport);
489cfa1ca9dSYoshinobu Inoue 			}
490cfa1ca9dSYoshinobu Inoue #ifdef INET6
491cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
492cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
493cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 0);
494ac55add0SGuido van Rooij 				if (!Lflag)
495cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
496cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name,
497ac55add0SGuido van Rooij 						   inp->inp_lport !=
498ac55add0SGuido van Rooij 							inp->inp_fport);
499cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
500cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5018d612dd2SPoul-Henning Kamp 		}
502ac55add0SGuido van Rooij 		if (istcp && !Lflag) {
5034f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5044f81ef50SGarrett Wollman 				printf("%d", tp->t_state);
5059a94a597SGarrett Wollman                       else {
5064f81ef50SGarrett Wollman 				printf("%s", tcpstates[tp->t_state]);
507513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5089a94a597SGarrett Wollman                               /* Show T/TCP `hidden state' */
5094f81ef50SGarrett Wollman                               if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
5109a94a597SGarrett Wollman                                       putchar('*');
511513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5129a94a597SGarrett Wollman                       }
5139b50d902SRodney W. Grimes 		}
5149b50d902SRodney W. Grimes 		putchar('\n');
5159b50d902SRodney W. Grimes 	}
5164f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5174f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
5184f81ef50SGarrett Wollman 			printf("Some %s sockets may have been deleted.\n",
5194f81ef50SGarrett Wollman 			       name);
5204f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
5214f81ef50SGarrett Wollman 			printf("Some %s sockets may have been created.\n",
5224f81ef50SGarrett Wollman 			       name);
5234f81ef50SGarrett Wollman 		} else {
524eb6fe0d9SMax Khon 			printf("Some %s sockets may have been created or deleted.\n",
5254f81ef50SGarrett Wollman 			       name);
5264f81ef50SGarrett Wollman 		}
5274f81ef50SGarrett Wollman 	}
5284f81ef50SGarrett Wollman 	free(buf);
5299b50d902SRodney W. Grimes }
5309b50d902SRodney W. Grimes 
5319b50d902SRodney W. Grimes /*
5329b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5339b50d902SRodney W. Grimes  */
5349b50d902SRodney W. Grimes void
535feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5369b50d902SRodney W. Grimes {
537c73d99b5SRuslan Ermilov 	struct tcpstat tcpstat, zerostat;
5384f81ef50SGarrett Wollman 	size_t len = sizeof tcpstat;
5399b50d902SRodney W. Grimes 
540cfa1ca9dSYoshinobu Inoue #ifdef INET6
541cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
542cfa1ca9dSYoshinobu Inoue 		return;
543cfa1ca9dSYoshinobu Inoue 	else
544cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
545cfa1ca9dSYoshinobu Inoue #endif
546cfa1ca9dSYoshinobu Inoue 
547feda1a43SJohn Baldwin 	if (live) {
548feda1a43SJohn Baldwin 		if (zflag)
549feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
550feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
551feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
552feda1a43SJohn Baldwin 			warn("sysctl: net.inet.tcp.stats");
553feda1a43SJohn Baldwin 			return;
554feda1a43SJohn Baldwin 		}
555feda1a43SJohn Baldwin 	} else
556feda1a43SJohn Baldwin 		kread(off, &tcpstat, len);
557feda1a43SJohn Baldwin 
5589b50d902SRodney W. Grimes 	printf ("%s:\n", name);
5599b50d902SRodney W. Grimes 
5609b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1) \
5619b50d902SRodney W. Grimes     printf(m, tcpstat.f, plural(tcpstat.f))
56222694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1) \
56322694ebaSBruce Evans     printf(m, tcpstat.f)
5649b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
5659b50d902SRodney W. Grimes     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
56622694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
56722694ebaSBruce Evans     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
5689b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
5690decbf9dSRuslan Ermilov     printf(m, tcpstat.f, pluralies(tcpstat.f))
5709b50d902SRodney W. Grimes 
571e1fb4daaSPaul Traina 	p(tcps_sndtotal, "\t%lu packet%s sent\n");
5729b50d902SRodney W. Grimes 	p2(tcps_sndpack,tcps_sndbyte,
573e1fb4daaSPaul Traina 		"\t\t%lu data packet%s (%lu byte%s)\n");
5749b50d902SRodney W. Grimes 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
575e1fb4daaSPaul Traina 		"\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
576d65bf08aSMatthew Dillon 	p(tcps_sndrexmitbad,
577d65bf08aSMatthew Dillon 		"\t\t%lu data packet%s unnecessarily retransmitted\n");
578e1fb4daaSPaul Traina 	p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
57922694ebaSBruce Evans 	p2a(tcps_sndacks, tcps_delack,
580e1fb4daaSPaul Traina 		"\t\t%lu ack-only packet%s (%lu delayed)\n");
581e1fb4daaSPaul Traina 	p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
582e1fb4daaSPaul Traina 	p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
583e1fb4daaSPaul Traina 	p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
584e1fb4daaSPaul Traina 	p(tcps_sndctrl, "\t\t%lu control packet%s\n");
585e1fb4daaSPaul Traina 	p(tcps_rcvtotal, "\t%lu packet%s received\n");
586e1fb4daaSPaul Traina 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%lu ack%s (for %lu byte%s)\n");
587e1fb4daaSPaul Traina 	p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
588e1fb4daaSPaul Traina 	p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
5899b50d902SRodney W. Grimes 	p2(tcps_rcvpack, tcps_rcvbyte,
590e1fb4daaSPaul Traina 		"\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
5919b50d902SRodney W. Grimes 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
592e1fb4daaSPaul Traina 		"\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
593e1fb4daaSPaul Traina 	p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
5949b50d902SRodney W. Grimes 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
595e1fb4daaSPaul Traina 		"\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
5969b50d902SRodney W. Grimes 	p2(tcps_rcvoopack, tcps_rcvoobyte,
597e1fb4daaSPaul Traina 		"\t\t%lu out-of-order packet%s (%lu byte%s)\n");
5989b50d902SRodney W. Grimes 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
599e1fb4daaSPaul Traina 		"\t\t%lu packet%s (%lu byte%s) of data after window\n");
600e1fb4daaSPaul Traina 	p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
601e1fb4daaSPaul Traina 	p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
602e1fb4daaSPaul Traina 	p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
603e1fb4daaSPaul Traina 	p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
604e1fb4daaSPaul Traina 	p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
60522694ebaSBruce Evans 	p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
60660e15662SWojciech A. Koszek 	p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
607e1fb4daaSPaul Traina 	p(tcps_connattempt, "\t%lu connection request%s\n");
608e1fb4daaSPaul Traina 	p(tcps_accepts, "\t%lu connection accept%s\n");
609e1fb4daaSPaul Traina 	p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
610e1fb4daaSPaul Traina 	p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
61101d3e1c0SRuslan Ermilov 	p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
612e1fb4daaSPaul Traina 	p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
6139b50d902SRodney W. Grimes 	p2(tcps_closed, tcps_drops,
614e1fb4daaSPaul Traina 		"\t%lu connection%s closed (including %lu drop%s)\n");
615e1fb4daaSPaul Traina 	p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
616861b1828SGarrett Wollman 	p(tcps_cachedrttvar,
617e1fb4daaSPaul Traina 	  "\t\t%lu connection%s updated cached RTT variance on close\n");
618861b1828SGarrett Wollman 	p(tcps_cachedssthresh,
619e1fb4daaSPaul Traina 	  "\t\t%lu connection%s updated cached ssthresh on close\n");
620e1fb4daaSPaul Traina 	p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
6219b50d902SRodney W. Grimes 	p2(tcps_rttupdated, tcps_segstimed,
622e1fb4daaSPaul Traina 		"\t%lu segment%s updated rtt (of %lu attempt%s)\n");
623e1fb4daaSPaul Traina 	p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
624e1fb4daaSPaul Traina 	p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
625e1fb4daaSPaul Traina 	p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
626e1fb4daaSPaul Traina 	p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
6277c72af87SMohan Srinivasan 	p(tcps_finwait2_drops, "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
628e1fb4daaSPaul Traina 	p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
629e1fb4daaSPaul Traina 	p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
630e1fb4daaSPaul Traina 	p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
631e1fb4daaSPaul Traina 	p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
632e1fb4daaSPaul Traina 	p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
63360a31b3aSJonathan Lemon 
6340decbf9dSRuslan Ermilov 	p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
635a01e3379SDavid Malone 	p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
636a01e3379SDavid Malone 	p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
637a01e3379SDavid Malone 	p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
638a01e3379SDavid Malone 	p1a(tcps_sc_completed, "\t\t%lu completed\n");
639a01e3379SDavid Malone 	p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
640a01e3379SDavid Malone 	p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
641a01e3379SDavid Malone 	p1a(tcps_sc_reset, "\t\t%lu reset\n");
642a01e3379SDavid Malone 	p1a(tcps_sc_stale, "\t\t%lu stale\n");
643a01e3379SDavid Malone 	p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
644a01e3379SDavid Malone 	p1a(tcps_sc_badack, "\t\t%lu badack\n");
645a01e3379SDavid Malone 	p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
646a01e3379SDavid Malone 	p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
647a01e3379SDavid Malone 	p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
648a01e3379SDavid Malone 	p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
649b6101dafSPaul Saab 
650b6101dafSPaul Saab 	p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
651b6101dafSPaul Saab 	p(tcps_sack_rexmits,
652b6101dafSPaul Saab 		"\t%lu segment rexmit%s in SACK recovery episodes\n");
653b6101dafSPaul Saab 	p(tcps_sack_rexmit_bytes,
654b6101dafSPaul Saab 		"\t%lu byte rexmit%s in SACK recovery episodes\n");
655b6101dafSPaul Saab 	p(tcps_sack_rcv_blocks,
656b6101dafSPaul Saab 		"\t%lu SACK option%s (SACK blocks) received\n");
657b6101dafSPaul Saab 	p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
658e891d82bSPaul Saab 	p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
659b6101dafSPaul Saab 
6609b50d902SRodney W. Grimes #undef p
66122694ebaSBruce Evans #undef p1a
6629b50d902SRodney W. Grimes #undef p2
66322694ebaSBruce Evans #undef p2a
6649b50d902SRodney W. Grimes #undef p3
6659b50d902SRodney W. Grimes }
6669b50d902SRodney W. Grimes 
6679b50d902SRodney W. Grimes /*
6689b50d902SRodney W. Grimes  * Dump UDP statistics structure.
6699b50d902SRodney W. Grimes  */
6709b50d902SRodney W. Grimes void
671feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
6729b50d902SRodney W. Grimes {
673c73d99b5SRuslan Ermilov 	struct udpstat udpstat, zerostat;
6744f81ef50SGarrett Wollman 	size_t len = sizeof udpstat;
6759b50d902SRodney W. Grimes 	u_long delivered;
6769b50d902SRodney W. Grimes 
677cfa1ca9dSYoshinobu Inoue #ifdef INET6
678cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
679cfa1ca9dSYoshinobu Inoue 		return;
680cfa1ca9dSYoshinobu Inoue 	else
681cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
682cfa1ca9dSYoshinobu Inoue #endif
683cfa1ca9dSYoshinobu Inoue 
684feda1a43SJohn Baldwin 	if (live) {
685feda1a43SJohn Baldwin 		if (zflag)
686feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
687feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
688feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
689feda1a43SJohn Baldwin 			warn("sysctl: net.inet.udp.stats");
690feda1a43SJohn Baldwin 			return;
691feda1a43SJohn Baldwin 		}
692feda1a43SJohn Baldwin 	} else
693feda1a43SJohn Baldwin 		kread(off, &udpstat, len);
694feda1a43SJohn Baldwin 
6959b50d902SRodney W. Grimes 	printf("%s:\n", name);
6969b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
6979b50d902SRodney W. Grimes     printf(m, udpstat.f, plural(udpstat.f))
69822694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
69922694ebaSBruce Evans     printf(m, udpstat.f)
7007d56c0eeSAlexander Langer 	p(udps_ipackets, "\t%lu datagram%s received\n");
70122694ebaSBruce Evans 	p1a(udps_hdrops, "\t%lu with incomplete header\n");
70222694ebaSBruce Evans 	p1a(udps_badlen, "\t%lu with bad data length field\n");
70322694ebaSBruce Evans 	p1a(udps_badsum, "\t%lu with bad checksum\n");
704fb9aaba0SRuslan Ermilov 	p1a(udps_nosum, "\t%lu with no checksum\n");
70522694ebaSBruce Evans 	p1a(udps_noport, "\t%lu dropped due to no socket\n");
70622694ebaSBruce Evans 	p(udps_noportbcast,
70771498f30SBruce M Simpson 	    "\t%lu broadcast/multicast datagram%s undelivered\n");
70822694ebaSBruce Evans 	p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
70922694ebaSBruce Evans 	p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
7109b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
7119b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
7129b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
7139b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
7149b50d902SRodney W. Grimes 		    udpstat.udps_noport -
7159b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
7169b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
7179b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
7187d56c0eeSAlexander Langer 		printf("\t%lu delivered\n", delivered);
7197d56c0eeSAlexander Langer 	p(udps_opackets, "\t%lu datagram%s output\n");
72071498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
72171498f30SBruce M Simpson 	p(udps_filtermcast,
72271498f30SBruce M Simpson 	    "\t%lu time%s multicast source filter matched\n");
7239b50d902SRodney W. Grimes #undef p
72422694ebaSBruce Evans #undef p1a
7259b50d902SRodney W. Grimes }
7269b50d902SRodney W. Grimes 
7279b50d902SRodney W. Grimes /*
728a9771948SGleb Smirnoff  * Dump CARP statistics structure.
729a9771948SGleb Smirnoff  */
730a9771948SGleb Smirnoff void
731feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
732a9771948SGleb Smirnoff {
733a9771948SGleb Smirnoff 	struct carpstats carpstat, zerostat;
734a9771948SGleb Smirnoff 	size_t len = sizeof(struct carpstats);
735a9771948SGleb Smirnoff 
736feda1a43SJohn Baldwin 	if (live) {
737a9771948SGleb Smirnoff 		if (zflag)
738a9771948SGleb Smirnoff 			memset(&zerostat, 0, len);
739a9771948SGleb Smirnoff 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
740a9771948SGleb Smirnoff 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
74145125e14SRuslan Ermilov 			if (errno != ENOENT)
742a9771948SGleb Smirnoff 				warn("sysctl: net.inet.carp.stats");
743a9771948SGleb Smirnoff 			return;
744a9771948SGleb Smirnoff 		}
745feda1a43SJohn Baldwin 	} else {
746feda1a43SJohn Baldwin 		if (off == 0)
747feda1a43SJohn Baldwin 			return;
748feda1a43SJohn Baldwin 		kread(off, &carpstat, len);
749feda1a43SJohn Baldwin 	}
750a9771948SGleb Smirnoff 
751a9771948SGleb Smirnoff 	printf("%s:\n", name);
752a9771948SGleb Smirnoff 
753a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
7547b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
755a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
7567b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f)
757a9771948SGleb Smirnoff 
7587b95a1ebSYaroslav Tykhiy 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
7597b95a1ebSYaroslav Tykhiy 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
7607b95a1ebSYaroslav Tykhiy 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
7617b95a1ebSYaroslav Tykhiy 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
7627b95a1ebSYaroslav Tykhiy 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
7637b95a1ebSYaroslav Tykhiy 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
7647b95a1ebSYaroslav Tykhiy 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
7657b95a1ebSYaroslav Tykhiy 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
7667b95a1ebSYaroslav Tykhiy 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
7677b95a1ebSYaroslav Tykhiy 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
7687b95a1ebSYaroslav Tykhiy 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
7697b95a1ebSYaroslav Tykhiy 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
7707b95a1ebSYaroslav Tykhiy 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
771a9771948SGleb Smirnoff #if notyet
772a9771948SGleb Smirnoff 	p(carps_ostates, "\t\t%s state update%s sent\n");
773a9771948SGleb Smirnoff #endif
774a9771948SGleb Smirnoff #undef p
775a9771948SGleb Smirnoff #undef p2
776a9771948SGleb Smirnoff }
777a9771948SGleb Smirnoff 
778a9771948SGleb Smirnoff /*
7799b50d902SRodney W. Grimes  * Dump IP statistics structure.
7809b50d902SRodney W. Grimes  */
7819b50d902SRodney W. Grimes void
782feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
7839b50d902SRodney W. Grimes {
784c73d99b5SRuslan Ermilov 	struct ipstat ipstat, zerostat;
7854f81ef50SGarrett Wollman 	size_t len = sizeof ipstat;
7869b50d902SRodney W. Grimes 
787feda1a43SJohn Baldwin 	if (live) {
788c73d99b5SRuslan Ermilov 		if (zflag)
789c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
790c73d99b5SRuslan Ermilov 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
791c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
7924f81ef50SGarrett Wollman 			warn("sysctl: net.inet.ip.stats");
7939b50d902SRodney W. Grimes 			return;
7944f81ef50SGarrett Wollman 		}
795feda1a43SJohn Baldwin 	} else
796feda1a43SJohn Baldwin 		kread(off, &ipstat, len);
7974f81ef50SGarrett Wollman 
7989b50d902SRodney W. Grimes 	printf("%s:\n", name);
7999b50d902SRodney W. Grimes 
8009b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
8019b50d902SRodney W. Grimes     printf(m, ipstat.f, plural(ipstat.f))
80222694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
80322694ebaSBruce Evans     printf(m, ipstat.f)
8049b50d902SRodney W. Grimes 
8057d56c0eeSAlexander Langer 	p(ips_total, "\t%lu total packet%s received\n");
8067d56c0eeSAlexander Langer 	p(ips_badsum, "\t%lu bad header checksum%s\n");
80722694ebaSBruce Evans 	p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
80822694ebaSBruce Evans 	p1a(ips_tooshort, "\t%lu with data size < data length\n");
809cfa1ca9dSYoshinobu Inoue 	p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
81022694ebaSBruce Evans 	p1a(ips_badhlen, "\t%lu with header length < data size\n");
81122694ebaSBruce Evans 	p1a(ips_badlen, "\t%lu with data length < header length\n");
81222694ebaSBruce Evans 	p1a(ips_badoptions, "\t%lu with bad options\n");
81322694ebaSBruce Evans 	p1a(ips_badvers, "\t%lu with incorrect version number\n");
8147d56c0eeSAlexander Langer 	p(ips_fragments, "\t%lu fragment%s received\n");
8157d56c0eeSAlexander Langer 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
8167d56c0eeSAlexander Langer 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
8177d56c0eeSAlexander Langer 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
8187d56c0eeSAlexander Langer 	p(ips_delivered, "\t%lu packet%s for this host\n");
8197d56c0eeSAlexander Langer 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
820958d6f7fSPierre Beyssac 	p(ips_forward, "\t%lu packet%s forwarded");
821958d6f7fSPierre Beyssac 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
822958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
823958d6f7fSPierre Beyssac 		putchar('\n');
8247d56c0eeSAlexander Langer 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
825a5969f5fSGarrett Wollman 	p(ips_notmember,
826a5969f5fSGarrett Wollman 	  "\t%lu packet%s received for unknown multicast group\n");
8277d56c0eeSAlexander Langer 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
8287d56c0eeSAlexander Langer 	p(ips_localout, "\t%lu packet%s sent from this host\n");
8297d56c0eeSAlexander Langer 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
830a5969f5fSGarrett Wollman 	p(ips_odropped,
831a5969f5fSGarrett Wollman 	  "\t%lu output packet%s dropped due to no bufs, etc.\n");
8327d56c0eeSAlexander Langer 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
8337d56c0eeSAlexander Langer 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
8347d56c0eeSAlexander Langer 	p(ips_ofragments, "\t%lu fragment%s created\n");
8357d56c0eeSAlexander Langer 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
836cfa1ca9dSYoshinobu Inoue 	p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
83733841545SHajimu UMEMOTO 	p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
8389b50d902SRodney W. Grimes #undef p
83922694ebaSBruce Evans #undef p1a
8409b50d902SRodney W. Grimes }
8419b50d902SRodney W. Grimes 
8424063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
8434063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
8449b50d902SRodney W. Grimes 	"#1",
8459b50d902SRodney W. Grimes 	"#2",
8464063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
8474063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
8484063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
8499b50d902SRodney W. Grimes 	"#6",
8509b50d902SRodney W. Grimes 	"#7",
8514063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
8524063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
8534063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
8544063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
8554063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
8564063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
8574063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
8584063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
8594063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
8604063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
8614063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
8624063583aSMaxim Konovalov 	"#19",
8634063583aSMaxim Konovalov 	"#20",
8644063583aSMaxim Konovalov 	"#21",
8654063583aSMaxim Konovalov 	"#22",
8664063583aSMaxim Konovalov 	"#23",
8674063583aSMaxim Konovalov 	"#24",
8684063583aSMaxim Konovalov 	"#25",
8694063583aSMaxim Konovalov 	"#26",
8704063583aSMaxim Konovalov 	"#27",
8714063583aSMaxim Konovalov 	"#28",
8724063583aSMaxim Konovalov 	"#29",
8734063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
8744063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
8754063583aSMaxim Konovalov 	"mobile host redirect",
8764063583aSMaxim Konovalov 	"IPv6 where-are-you",
8774063583aSMaxim Konovalov 	"IPv6 i-am-here",
8784063583aSMaxim Konovalov 	"mobile registration req",
8794063583aSMaxim Konovalov 	"mobile registration reply",
8804063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
8814063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
8824063583aSMaxim Konovalov 	"icmp SKIP",
8834063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
8849b50d902SRodney W. Grimes };
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes /*
8879b50d902SRodney W. Grimes  * Dump ICMP statistics.
8889b50d902SRodney W. Grimes  */
8899b50d902SRodney W. Grimes void
890feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8919b50d902SRodney W. Grimes {
892c73d99b5SRuslan Ermilov 	struct icmpstat icmpstat, zerostat;
8934e00c309SGarrett Wollman 	int i, first;
8944e00c309SGarrett Wollman 	size_t len;
8959b50d902SRodney W. Grimes 
8964e00c309SGarrett Wollman 	len = sizeof icmpstat;
897feda1a43SJohn Baldwin 	if (live) {
898c73d99b5SRuslan Ermilov 		if (zflag)
899c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
900feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
901c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
902c73d99b5SRuslan Ermilov 			warn("sysctl: net.inet.icmp.stats");
903c73d99b5SRuslan Ermilov 			return;
904c73d99b5SRuslan Ermilov 		}
905feda1a43SJohn Baldwin 	} else
906feda1a43SJohn Baldwin 		kread(off, &icmpstat, len);
9074e00c309SGarrett Wollman 
9089b50d902SRodney W. Grimes 	printf("%s:\n", name);
9099b50d902SRodney W. Grimes 
9109b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
9119b50d902SRodney W. Grimes     printf(m, icmpstat.f, plural(icmpstat.f))
91222694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
91322694ebaSBruce Evans     printf(m, icmpstat.f)
914bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
915bd714208SRuslan Ermilov     printf(m, icmpstat.f, plurales(icmpstat.f))
9169b50d902SRodney W. Grimes 
9177d56c0eeSAlexander Langer 	p(icps_error, "\t%lu call%s to icmp_error\n");
9189b50d902SRodney W. Grimes 	p(icps_oldicmp,
919f99a4046SMike Makonnen 	    "\t%lu error%s not generated in response to an icmp message\n");
9209b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
9219b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
9229b50d902SRodney W. Grimes 			if (first) {
9239b50d902SRodney W. Grimes 				printf("\tOutput histogram:\n");
9249b50d902SRodney W. Grimes 				first = 0;
9259b50d902SRodney W. Grimes 			}
9264063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
9277d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
9289b50d902SRodney W. Grimes 					icmpstat.icps_outhist[i]);
9294063583aSMaxim Konovalov 			else
9304063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
9314063583aSMaxim Konovalov 					icmpstat.icps_outhist[i]);
9329b50d902SRodney W. Grimes 		}
9337d56c0eeSAlexander Langer 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
9347d56c0eeSAlexander Langer 	p(icps_tooshort, "\t%lu message%s < minimum length\n");
9357d56c0eeSAlexander Langer 	p(icps_checksum, "\t%lu bad checksum%s\n");
9367d56c0eeSAlexander Langer 	p(icps_badlen, "\t%lu message%s with bad length\n");
93722694ebaSBruce Evans 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
93822694ebaSBruce Evans 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
9399b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
9409b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
9419b50d902SRodney W. Grimes 			if (first) {
9429b50d902SRodney W. Grimes 				printf("\tInput histogram:\n");
9439b50d902SRodney W. Grimes 				first = 0;
9449b50d902SRodney W. Grimes 			}
9454063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
9467d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
9479b50d902SRodney W. Grimes 			 		icmpstat.icps_inhist[i]);
9484063583aSMaxim Konovalov 			else
9494063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
9504063583aSMaxim Konovalov 					icmpstat.icps_inhist[i]);
9519b50d902SRodney W. Grimes 		}
9527d56c0eeSAlexander Langer 	p(icps_reflect, "\t%lu message response%s generated\n");
953bd714208SRuslan Ermilov 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
9540237ca7bSRuslan Ermilov 	p(icps_noroute, "\t%lu no return route%s\n");
9559b50d902SRodney W. Grimes #undef p
95622694ebaSBruce Evans #undef p1a
957bd714208SRuslan Ermilov #undef p2
958feda1a43SJohn Baldwin 	if (live) {
9594e00c309SGarrett Wollman 		len = sizeof i;
960feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
961feda1a43SJohn Baldwin 		    0)
9624e00c309SGarrett Wollman 			return;
9634e00c309SGarrett Wollman 		printf("\tICMP address mask responses are %sabled\n",
9644e00c309SGarrett Wollman 		    i ? "en" : "dis");
9659b50d902SRodney W. Grimes 	}
966feda1a43SJohn Baldwin }
9679b50d902SRodney W. Grimes 
9689b50d902SRodney W. Grimes /*
9699b50d902SRodney W. Grimes  * Dump IGMP statistics structure.
9709b50d902SRodney W. Grimes  */
9719b50d902SRodney W. Grimes void
972feda1a43SJohn Baldwin igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9739b50d902SRodney W. Grimes {
974c73d99b5SRuslan Ermilov 	struct igmpstat igmpstat, zerostat;
9754f81ef50SGarrett Wollman 	size_t len = sizeof igmpstat;
9769b50d902SRodney W. Grimes 
977feda1a43SJohn Baldwin 	if (live) {
978c73d99b5SRuslan Ermilov 		if (zflag)
979c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
980c73d99b5SRuslan Ermilov 		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
981c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
9824f81ef50SGarrett Wollman 			warn("sysctl: net.inet.igmp.stats");
9839b50d902SRodney W. Grimes 			return;
9844f81ef50SGarrett Wollman 		}
985feda1a43SJohn Baldwin 	} else
986feda1a43SJohn Baldwin 		kread(off, &igmpstat, len);
9874f81ef50SGarrett Wollman 
9889b50d902SRodney W. Grimes 	printf("%s:\n", name);
9899b50d902SRodney W. Grimes 
9909b50d902SRodney W. Grimes #define	p(f, m) if (igmpstat.f || sflag <= 1) \
9919b50d902SRodney W. Grimes     printf(m, igmpstat.f, plural(igmpstat.f))
9929b50d902SRodney W. Grimes #define	py(f, m) if (igmpstat.f || sflag <= 1) \
9939b50d902SRodney W. Grimes     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
9949b50d902SRodney W. Grimes 	p(igps_rcv_total, "\t%u message%s received\n");
9959b50d902SRodney W. Grimes         p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
9969b50d902SRodney W. Grimes         p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
9979b50d902SRodney W. Grimes         py(igps_rcv_queries, "\t%u membership quer%s received\n");
9989b50d902SRodney W. Grimes         py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
9999b50d902SRodney W. Grimes         p(igps_rcv_reports, "\t%u membership report%s received\n");
10009b50d902SRodney W. Grimes         p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
10019b50d902SRodney W. Grimes         p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
10029b50d902SRodney W. Grimes         p(igps_snd_reports, "\t%u membership report%s sent\n");
10039b50d902SRodney W. Grimes #undef p
10049b50d902SRodney W. Grimes #undef py
10059b50d902SRodney W. Grimes }
10069b50d902SRodney W. Grimes 
10079b50d902SRodney W. Grimes /*
1008c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1009c7b9b5bbSJeffrey Hsu  */
1010c7b9b5bbSJeffrey Hsu void
1011feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1012feda1a43SJohn Baldwin     int proto __unused)
1013c7b9b5bbSJeffrey Hsu {
1014c7b9b5bbSJeffrey Hsu 	struct pimstat pimstat, zerostat;
1015c7b9b5bbSJeffrey Hsu 	size_t len = sizeof pimstat;
1016c7b9b5bbSJeffrey Hsu 
1017feda1a43SJohn Baldwin 	if (live) {
1018c7b9b5bbSJeffrey Hsu 		if (zflag)
1019c7b9b5bbSJeffrey Hsu 			memset(&zerostat, 0, len);
1020c7b9b5bbSJeffrey Hsu 		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1021c7b9b5bbSJeffrey Hsu 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
10226d7c0d2fSDag-Erling Smørgrav 			if (errno != ENOENT)
1023c7b9b5bbSJeffrey Hsu 				warn("sysctl: net.inet.pim.stats");
1024c7b9b5bbSJeffrey Hsu 			return;
1025c7b9b5bbSJeffrey Hsu 		}
1026feda1a43SJohn Baldwin 	} else {
1027feda1a43SJohn Baldwin 		if (off == 0)
1028feda1a43SJohn Baldwin 			return;
1029feda1a43SJohn Baldwin 		kread(off, &pimstat, len);
1030feda1a43SJohn Baldwin 	}
1031c7b9b5bbSJeffrey Hsu 
1032c7b9b5bbSJeffrey Hsu 	printf("%s:\n", name);
1033c7b9b5bbSJeffrey Hsu 
1034c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
10357b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1036c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
10377b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
10387b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
10397b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
10407b95a1ebSYaroslav Tykhiy 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
10417b95a1ebSYaroslav Tykhiy         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
10427b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
10437b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
10447b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
10457b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_wrongiif, "\t%ju data register message%s received on wrong iif\n");
10467b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
10477b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
10487b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1049c7b9b5bbSJeffrey Hsu #undef p
1050c7b9b5bbSJeffrey Hsu #undef py
1051c7b9b5bbSJeffrey Hsu }
1052c7b9b5bbSJeffrey Hsu 
1053c7b9b5bbSJeffrey Hsu /*
10549b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
10559b50d902SRodney W. Grimes  */
10569b50d902SRodney W. Grimes void
1057a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port)
10589b50d902SRodney W. Grimes {
10599b50d902SRodney W. Grimes 	struct servent *sp = 0;
10609b50d902SRodney W. Grimes 	char line[80], *cp;
1061cfa1ca9dSYoshinobu Inoue 	int width;
10629b50d902SRodney W. Grimes 
1063080b7f49SDag-Erling Smørgrav 	if (Wflag)
1064080b7f49SDag-Erling Smørgrav 	    sprintf(line, "%s.", inetname(in));
1065080b7f49SDag-Erling Smørgrav 	else
1066a01e3379SDavid Malone 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
10679b50d902SRodney W. Grimes 	cp = index(line, '\0');
1068a01e3379SDavid Malone 	if (!num_port && port)
10699b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
10709b50d902SRodney W. Grimes 	if (sp || port == 0)
10711ef69972SAndrey A. Chernov 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
10729b50d902SRodney W. Grimes 	else
10739b50d902SRodney W. Grimes 		sprintf(cp, "%d ", ntohs((u_short)port));
1074080b7f49SDag-Erling Smørgrav 	width = (Aflag && !Wflag) ? 18 : 22;
1075080b7f49SDag-Erling Smørgrav 	if (Wflag)
1076080b7f49SDag-Erling Smørgrav 	    printf("%-*s ", width, line);
1077080b7f49SDag-Erling Smørgrav 	else
1078cfa1ca9dSYoshinobu Inoue 	    printf("%-*.*s ", width, width, line);
10799b50d902SRodney W. Grimes }
10809b50d902SRodney W. Grimes 
10819b50d902SRodney W. Grimes /*
10829b50d902SRodney W. Grimes  * Construct an Internet address representation.
108365ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
10849b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
10859b50d902SRodney W. Grimes  */
10869b50d902SRodney W. Grimes char *
10875e051718SAssar Westerlund inetname(struct in_addr *inp)
10889b50d902SRodney W. Grimes {
1089a01e3379SDavid Malone 	char *cp;
1090d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
10919b50d902SRodney W. Grimes 	struct hostent *hp;
10929b50d902SRodney W. Grimes 	struct netent *np;
10939b50d902SRodney W. Grimes 
10949b50d902SRodney W. Grimes 	cp = 0;
109565ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
10969b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
10979b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
10989b50d902SRodney W. Grimes 
10999b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
11009b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
11019b50d902SRodney W. Grimes 			if (np)
11029b50d902SRodney W. Grimes 				cp = np->n_name;
11039b50d902SRodney W. Grimes 		}
11049b50d902SRodney W. Grimes 		if (cp == 0) {
11059b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
11069b50d902SRodney W. Grimes 			if (hp) {
11079b50d902SRodney W. Grimes 				cp = hp->h_name;
1108d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
11099b50d902SRodney W. Grimes 			}
11109b50d902SRodney W. Grimes 		}
11119b50d902SRodney W. Grimes 	}
11129b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
11139b50d902SRodney W. Grimes 		strcpy(line, "*");
11149a1f6729SWarner Losh 	else if (cp) {
11159a1f6729SWarner Losh 		strncpy(line, cp, sizeof(line) - 1);
11169a1f6729SWarner Losh 		line[sizeof(line) - 1] = '\0';
11179a1f6729SWarner Losh 	} else {
11189b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
111922694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
112022694ebaSBruce Evans 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
11219b50d902SRodney W. Grimes 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
11229b50d902SRodney W. Grimes 	}
11239b50d902SRodney W. Grimes 	return (line);
11249b50d902SRodney W. Grimes }
1125