xref: /freebsd/usr.bin/netstat/inet.c (revision c80211e3cf59276683fe85db8142f09fbc97e2cc)
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");
432afc74015SRuslan Ermilov 				if (!xflag)
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");
44494f138feSGeorge V. Neville-Neil 			}
445afc74015SRuslan Ermilov 			putchar('\n');
4469b50d902SRodney W. Grimes 			first = 0;
4479b50d902SRodney W. Grimes 		}
448fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
449fb5d0fbdSRuslan Ermilov 			continue;
450e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
451e4ec3989SDag-Erling Smørgrav 			if (istcp)
452afc74015SRuslan Ermilov 				printf("%*lx ", 2 * (int)sizeof(void *), (u_long)inp->inp_ppcb);
453e4ec3989SDag-Erling Smørgrav 			else
454afc74015SRuslan Ermilov 				printf("%*lx ", 2 * (int)sizeof(void *), (u_long)so->so_pcb);
455e4ec3989SDag-Erling Smørgrav 		}
456cfa1ca9dSYoshinobu Inoue #ifdef INET6
457fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
4583feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4593feeb332SDavid E. O'Brien 			    "46" : "6 ";
460fc60ab7bSYoshinobu Inoue 		else
461cfa1ca9dSYoshinobu Inoue #endif
4623feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
4633feeb332SDavid E. O'Brien 		    "4 " : "  ";
46409fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
46509fe6320SNavdeep Parhar 			printf("%-3.3s%-2.2s ", "toe", vchar);
46609fe6320SNavdeep Parhar 		else
467fb5d0fbdSRuslan Ermilov 			printf("%-3.3s%-2.2s ", name, vchar);
468fb5d0fbdSRuslan Ermilov 		if (Lflag) {
469a01e3379SDavid Malone 			char buf1[15];
470fc60ab7bSYoshinobu Inoue 
471a01e3379SDavid Malone 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
472fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
473a01e3379SDavid Malone 			printf("%-14.14s ", buf1);
474f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
475f5d34df5SGeorge V. Neville-Neil 			if (istcp)
476f5d34df5SGeorge V. Neville-Neil 				printf("%6u %6u %6u ", tp->t_sndrexmitpack,
477f5d34df5SGeorge V. Neville-Neil 				       tp->t_rcvoopack, tp->t_sndzerowin);
478fb5d0fbdSRuslan Ermilov 		} else {
479dd335a15SDavid E. O'Brien 			printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
480fc60ab7bSYoshinobu Inoue 		}
48165ea0024SAssar Westerlund 		if (numeric_port) {
482cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
4834f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
4848d612dd2SPoul-Henning Kamp 				    name, 1);
485ac55add0SGuido van Rooij 				if (!Lflag)
486ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
487ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 1);
488cfa1ca9dSYoshinobu Inoue 			}
489cfa1ca9dSYoshinobu Inoue #ifdef INET6
490cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
491cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
492cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
493ac55add0SGuido van Rooij 				if (!Lflag)
494cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
495cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
496cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
497cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4984f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
499cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
5004f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
5018d612dd2SPoul-Henning Kamp 				    name, 1);
502ac55add0SGuido van Rooij 				if (!Lflag)
503ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
504ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name, 0);
505cfa1ca9dSYoshinobu Inoue 			}
506cfa1ca9dSYoshinobu Inoue #ifdef INET6
507cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
508cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
509cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
510ac55add0SGuido van Rooij 				if (!Lflag)
511cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
512cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
513cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
514cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5158d612dd2SPoul-Henning Kamp 		} else {
516cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
5174f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
5188d612dd2SPoul-Henning Kamp 				    name, 0);
519ac55add0SGuido van Rooij 				if (!Lflag)
520ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
521ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
5223feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
523cfa1ca9dSYoshinobu Inoue 			}
524cfa1ca9dSYoshinobu Inoue #ifdef INET6
525cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
526cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
527cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
528ac55add0SGuido van Rooij 				if (!Lflag)
529cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
530cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
5313feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
532cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
533cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
5348d612dd2SPoul-Henning Kamp 		}
53549f287f8SGeorge V. Neville-Neil 		if (xflag) {
53649f287f8SGeorge V. Neville-Neil 			printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u",
53749f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
53849f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
53949f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
54049f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
54149f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
54249f287f8SGeorge V. Neville-Neil 			       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
543aae09141SMike Silbersack 			if (timer != NULL)
544b8614722SMike Silbersack 				printf(" %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d",
545b8614722SMike Silbersack 				    timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10,
546b8614722SMike Silbersack 				    timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10,
547b8614722SMike Silbersack 				    timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10,
548b8614722SMike Silbersack 				    timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10,
549b8614722SMike Silbersack 				    timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10,
550b8614722SMike Silbersack 				    timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10);
551b8614722SMike Silbersack 		}
552f5d34df5SGeorge V. Neville-Neil 		if (istcp && !Lflag && !xflag && !Tflag) {
5534f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5544f81ef50SGarrett Wollman 				printf("%d", tp->t_state);
5559a94a597SGarrett Wollman 			else {
5564f81ef50SGarrett Wollman 				printf("%s", tcpstates[tp->t_state]);
557513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5589a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
5594f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
5609a94a597SGarrett Wollman 					putchar('*');
561513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5629a94a597SGarrett Wollman 			}
5639b50d902SRodney W. Grimes 		}
5649b50d902SRodney W. Grimes 		putchar('\n');
5659b50d902SRodney W. Grimes 	}
5664f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5674f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
5684f81ef50SGarrett Wollman 			printf("Some %s sockets may have been deleted.\n",
5694f81ef50SGarrett Wollman 			    name);
5704f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
5714f81ef50SGarrett Wollman 			printf("Some %s sockets may have been created.\n",
5724f81ef50SGarrett Wollman 			    name);
5734f81ef50SGarrett Wollman 		} else {
5743feeb332SDavid E. O'Brien 			printf(
5753feeb332SDavid E. O'Brien 	"Some %s sockets may have been created or deleted.\n",
5764f81ef50SGarrett Wollman 			    name);
5774f81ef50SGarrett Wollman 		}
5784f81ef50SGarrett Wollman 	}
5794f81ef50SGarrett Wollman 	free(buf);
5809b50d902SRodney W. Grimes }
5819b50d902SRodney W. Grimes 
5829b50d902SRodney W. Grimes /*
5839b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5849b50d902SRodney W. Grimes  */
5859b50d902SRodney W. Grimes void
586feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5879b50d902SRodney W. Grimes {
588c73d99b5SRuslan Ermilov 	struct tcpstat tcpstat, zerostat;
5894f81ef50SGarrett Wollman 	size_t len = sizeof tcpstat;
5909b50d902SRodney W. Grimes 
591cfa1ca9dSYoshinobu Inoue #ifdef INET6
592cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
593cfa1ca9dSYoshinobu Inoue 		return;
594cfa1ca9dSYoshinobu Inoue 	else
595cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
596cfa1ca9dSYoshinobu Inoue #endif
597cfa1ca9dSYoshinobu Inoue 
598feda1a43SJohn Baldwin 	if (live) {
599feda1a43SJohn Baldwin 		if (zflag)
600feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
601feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
602feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
603feda1a43SJohn Baldwin 			warn("sysctl: net.inet.tcp.stats");
604feda1a43SJohn Baldwin 			return;
605feda1a43SJohn Baldwin 		}
60629dde48dSGleb Smirnoff 	} else {
60729dde48dSGleb Smirnoff 		u_long tcpstat_p[sizeof(struct tcpstat)/sizeof(uint64_t)];
60829dde48dSGleb Smirnoff 
60929dde48dSGleb Smirnoff 		kread(off, &tcpstat_p, sizeof(tcpstat_p));
61029dde48dSGleb Smirnoff 		kread_counters(tcpstat_p, (uint64_t *)&tcpstat,
61129dde48dSGleb Smirnoff 		    sizeof(struct tcpstat)/sizeof(uint64_t));
61229dde48dSGleb Smirnoff 	}
613feda1a43SJohn Baldwin 
6149b50d902SRodney W. Grimes 	printf ("%s:\n", name);
6159b50d902SRodney W. Grimes 
6169b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
6175923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
6185923c293SGleb Smirnoff 
61922694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
6205923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f)
6215923c293SGleb Smirnoff 
6229b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
6235923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6245923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
6255923c293SGleb Smirnoff 
62622694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
6275923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6285923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
6295923c293SGleb Smirnoff 
6309b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
6315923c293SGleb Smirnoff 	printf(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
6329b50d902SRodney W. Grimes 
6335923c293SGleb Smirnoff 	p(tcps_sndtotal, "\t%ju packet%s sent\n");
6345923c293SGleb Smirnoff 	p2(tcps_sndpack,tcps_sndbyte, "\t\t%ju data packet%s (%ju byte%s)\n");
6359b50d902SRodney W. Grimes 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
6365923c293SGleb Smirnoff 	    "\t\t%ju data packet%s (%ju byte%s) retransmitted\n");
637d65bf08aSMatthew Dillon 	p(tcps_sndrexmitbad,
6385923c293SGleb Smirnoff 	    "\t\t%ju data packet%s unnecessarily retransmitted\n");
6395923c293SGleb Smirnoff 	p(tcps_mturesent, "\t\t%ju resend%s initiated by MTU discovery\n");
64022694ebaSBruce Evans 	p2a(tcps_sndacks, tcps_delack,
6415923c293SGleb Smirnoff 	    "\t\t%ju ack-only packet%s (%ju delayed)\n");
6425923c293SGleb Smirnoff 	p(tcps_sndurg, "\t\t%ju URG only packet%s\n");
6435923c293SGleb Smirnoff 	p(tcps_sndprobe, "\t\t%ju window probe packet%s\n");
6445923c293SGleb Smirnoff 	p(tcps_sndwinup, "\t\t%ju window update packet%s\n");
6455923c293SGleb Smirnoff 	p(tcps_sndctrl, "\t\t%ju control packet%s\n");
6465923c293SGleb Smirnoff 	p(tcps_rcvtotal, "\t%ju packet%s received\n");
6473feeb332SDavid E. O'Brien 	p2(tcps_rcvackpack, tcps_rcvackbyte,
6485923c293SGleb Smirnoff 	    "\t\t%ju ack%s (for %ju byte%s)\n");
6495923c293SGleb Smirnoff 	p(tcps_rcvdupack, "\t\t%ju duplicate ack%s\n");
6505923c293SGleb Smirnoff 	p(tcps_rcvacktoomuch, "\t\t%ju ack%s for unsent data\n");
6519b50d902SRodney W. Grimes 	p2(tcps_rcvpack, tcps_rcvbyte,
6525923c293SGleb Smirnoff 	    "\t\t%ju packet%s (%ju byte%s) received in-sequence\n");
6539b50d902SRodney W. Grimes 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
6545923c293SGleb Smirnoff 	    "\t\t%ju completely duplicate packet%s (%ju byte%s)\n");
6555923c293SGleb Smirnoff 	p(tcps_pawsdrop, "\t\t%ju old duplicate packet%s\n");
6569b50d902SRodney W. Grimes 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
6575923c293SGleb Smirnoff 	    "\t\t%ju packet%s with some dup. data (%ju byte%s duped)\n");
6589b50d902SRodney W. Grimes 	p2(tcps_rcvoopack, tcps_rcvoobyte,
6595923c293SGleb Smirnoff 	    "\t\t%ju out-of-order packet%s (%ju byte%s)\n");
6609b50d902SRodney W. Grimes 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
6615923c293SGleb Smirnoff 	    "\t\t%ju packet%s (%ju byte%s) of data after window\n");
6625923c293SGleb Smirnoff 	p(tcps_rcvwinprobe, "\t\t%ju window probe%s\n");
6635923c293SGleb Smirnoff 	p(tcps_rcvwinupd, "\t\t%ju window update packet%s\n");
6645923c293SGleb Smirnoff 	p(tcps_rcvafterclose, "\t\t%ju packet%s received after close\n");
6655923c293SGleb Smirnoff 	p(tcps_rcvbadsum, "\t\t%ju discarded for bad checksum%s\n");
6665923c293SGleb Smirnoff 	p(tcps_rcvbadoff, "\t\t%ju discarded for bad header offset field%s\n");
6675923c293SGleb Smirnoff 	p1a(tcps_rcvshort, "\t\t%ju discarded because packet too short\n");
6685923c293SGleb Smirnoff 	p1a(tcps_rcvmemdrop, "\t\t%ju discarded due to memory problems\n");
6695923c293SGleb Smirnoff 	p(tcps_connattempt, "\t%ju connection request%s\n");
6705923c293SGleb Smirnoff 	p(tcps_accepts, "\t%ju connection accept%s\n");
6715923c293SGleb Smirnoff 	p(tcps_badsyn, "\t%ju bad connection attempt%s\n");
6725923c293SGleb Smirnoff 	p(tcps_listendrop, "\t%ju listen queue overflow%s\n");
6735923c293SGleb Smirnoff 	p(tcps_badrst, "\t%ju ignored RSTs in the window%s\n");
6745923c293SGleb Smirnoff 	p(tcps_connects, "\t%ju connection%s established (including accepts)\n");
6759b50d902SRodney W. Grimes 	p2(tcps_closed, tcps_drops,
6765923c293SGleb Smirnoff 	    "\t%ju connection%s closed (including %ju drop%s)\n");
6775923c293SGleb Smirnoff 	p(tcps_cachedrtt, "\t\t%ju connection%s updated cached RTT on close\n");
678861b1828SGarrett Wollman 	p(tcps_cachedrttvar,
6795923c293SGleb Smirnoff 	    "\t\t%ju connection%s updated cached RTT variance on close\n");
680861b1828SGarrett Wollman 	p(tcps_cachedssthresh,
6815923c293SGleb Smirnoff 	    "\t\t%ju connection%s updated cached ssthresh on close\n");
6825923c293SGleb Smirnoff 	p(tcps_conndrops, "\t%ju embryonic connection%s dropped\n");
6839b50d902SRodney W. Grimes 	p2(tcps_rttupdated, tcps_segstimed,
6845923c293SGleb Smirnoff 	    "\t%ju segment%s updated rtt (of %ju attempt%s)\n");
6855923c293SGleb Smirnoff 	p(tcps_rexmttimeo, "\t%ju retransmit timeout%s\n");
6865923c293SGleb Smirnoff 	p(tcps_timeoutdrop, "\t\t%ju connection%s dropped by rexmit timeout\n");
6875923c293SGleb Smirnoff 	p(tcps_persisttimeo, "\t%ju persist timeout%s\n");
6885923c293SGleb Smirnoff 	p(tcps_persistdrop, "\t\t%ju connection%s dropped by persist timeout\n");
6893feeb332SDavid E. O'Brien 	p(tcps_finwait2_drops,
6905923c293SGleb Smirnoff 	    "\t%ju Connection%s (fin_wait_2) dropped because of timeout\n");
6915923c293SGleb Smirnoff 	p(tcps_keeptimeo, "\t%ju keepalive timeout%s\n");
6925923c293SGleb Smirnoff 	p(tcps_keepprobe, "\t\t%ju keepalive probe%s sent\n");
6935923c293SGleb Smirnoff 	p(tcps_keepdrops, "\t\t%ju connection%s dropped by keepalive\n");
6945923c293SGleb Smirnoff 	p(tcps_predack, "\t%ju correct ACK header prediction%s\n");
6955923c293SGleb Smirnoff 	p(tcps_preddat, "\t%ju correct data packet header prediction%s\n");
69660a31b3aSJonathan Lemon 
6975923c293SGleb Smirnoff 	p3(tcps_sc_added, "\t%ju syncache entr%s added\n");
6985923c293SGleb Smirnoff 	p1a(tcps_sc_retransmitted, "\t\t%ju retransmitted\n");
6995923c293SGleb Smirnoff 	p1a(tcps_sc_dupsyn, "\t\t%ju dupsyn\n");
7005923c293SGleb Smirnoff 	p1a(tcps_sc_dropped, "\t\t%ju dropped\n");
7015923c293SGleb Smirnoff 	p1a(tcps_sc_completed, "\t\t%ju completed\n");
7025923c293SGleb Smirnoff 	p1a(tcps_sc_bucketoverflow, "\t\t%ju bucket overflow\n");
7035923c293SGleb Smirnoff 	p1a(tcps_sc_cacheoverflow, "\t\t%ju cache overflow\n");
7045923c293SGleb Smirnoff 	p1a(tcps_sc_reset, "\t\t%ju reset\n");
7055923c293SGleb Smirnoff 	p1a(tcps_sc_stale, "\t\t%ju stale\n");
7065923c293SGleb Smirnoff 	p1a(tcps_sc_aborted, "\t\t%ju aborted\n");
7075923c293SGleb Smirnoff 	p1a(tcps_sc_badack, "\t\t%ju badack\n");
7085923c293SGleb Smirnoff 	p1a(tcps_sc_unreach, "\t\t%ju unreach\n");
7095923c293SGleb Smirnoff 	p(tcps_sc_zonefail, "\t\t%ju zone failure%s\n");
7105923c293SGleb Smirnoff 	p(tcps_sc_sendcookie, "\t%ju cookie%s sent\n");
7115923c293SGleb Smirnoff 	p(tcps_sc_recvcookie, "\t%ju cookie%s received\n");
712b6101dafSPaul Saab 
7135923c293SGleb Smirnoff 	p3(tcps_hc_added, "\t%ju hostcache entr%s added\n");
7145923c293SGleb Smirnoff 	p1a(tcps_hc_bucketoverflow, "\t\t%ju bucket overflow\n");
715699ef999SRuslan Ermilov 
7165923c293SGleb Smirnoff 	p(tcps_sack_recovery_episode, "\t%ju SACK recovery episode%s\n");
717b6101dafSPaul Saab 	p(tcps_sack_rexmits,
7185923c293SGleb Smirnoff 	    "\t%ju segment rexmit%s in SACK recovery episodes\n");
719b6101dafSPaul Saab 	p(tcps_sack_rexmit_bytes,
7205923c293SGleb Smirnoff 	    "\t%ju byte rexmit%s in SACK recovery episodes\n");
721b6101dafSPaul Saab 	p(tcps_sack_rcv_blocks,
7225923c293SGleb Smirnoff 	    "\t%ju SACK option%s (SACK blocks) received\n");
7235923c293SGleb Smirnoff 	p(tcps_sack_send_blocks, "\t%ju SACK option%s (SACK blocks) sent\n");
7245923c293SGleb Smirnoff 	p1a(tcps_sack_sboverflow, "\t%ju SACK scoreboard overflow\n");
725b6101dafSPaul Saab 
7265923c293SGleb Smirnoff 	p(tcps_ecn_ce, "\t%ju packet%s with ECN CE bit set\n");
7275923c293SGleb Smirnoff 	p(tcps_ecn_ect0, "\t%ju packet%s with ECN ECT(0) bit set\n");
7285923c293SGleb Smirnoff 	p(tcps_ecn_ect1, "\t%ju packet%s with ECN ECT(1) bit set\n");
7295923c293SGleb Smirnoff 	p(tcps_ecn_shs, "\t%ju successful ECN handshake%s\n");
7305923c293SGleb Smirnoff 	p(tcps_ecn_rcwnd, "\t%ju time%s ECN reduced the congestion window\n");
7319b50d902SRodney W. Grimes #undef p
73222694ebaSBruce Evans #undef p1a
7339b50d902SRodney W. Grimes #undef p2
73422694ebaSBruce Evans #undef p2a
7359b50d902SRodney W. Grimes #undef p3
7369b50d902SRodney W. Grimes }
7379b50d902SRodney W. Grimes 
7389b50d902SRodney W. Grimes /*
7399b50d902SRodney W. Grimes  * Dump UDP statistics structure.
7409b50d902SRodney W. Grimes  */
7419b50d902SRodney W. Grimes void
742feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
7439b50d902SRodney W. Grimes {
744c73d99b5SRuslan Ermilov 	struct udpstat udpstat, zerostat;
7454f81ef50SGarrett Wollman 	size_t len = sizeof udpstat;
746*c80211e3SAndrey V. Elsukov 	uint64_t delivered;
7479b50d902SRodney W. Grimes 
748cfa1ca9dSYoshinobu Inoue #ifdef INET6
749cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
750cfa1ca9dSYoshinobu Inoue 		return;
751cfa1ca9dSYoshinobu Inoue 	else
752cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
753cfa1ca9dSYoshinobu Inoue #endif
754cfa1ca9dSYoshinobu Inoue 
755feda1a43SJohn Baldwin 	if (live) {
756feda1a43SJohn Baldwin 		if (zflag)
757feda1a43SJohn Baldwin 			memset(&zerostat, 0, len);
758feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
759feda1a43SJohn Baldwin 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
760feda1a43SJohn Baldwin 			warn("sysctl: net.inet.udp.stats");
761feda1a43SJohn Baldwin 			return;
762feda1a43SJohn Baldwin 		}
763feda1a43SJohn Baldwin 	} else
764feda1a43SJohn Baldwin 		kread(off, &udpstat, len);
765feda1a43SJohn Baldwin 
7669b50d902SRodney W. Grimes 	printf("%s:\n", name);
7679b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
768*c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)udpstat.f, plural(udpstat.f))
76922694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
770*c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)udpstat.f)
771*c80211e3SAndrey V. Elsukov 	p(udps_ipackets, "datagram%s received\n");
772*c80211e3SAndrey V. Elsukov 	p1a(udps_hdrops, "with incomplete header\n");
773*c80211e3SAndrey V. Elsukov 	p1a(udps_badlen, "with bad data length field\n");
774*c80211e3SAndrey V. Elsukov 	p1a(udps_badsum, "with bad checksum\n");
775*c80211e3SAndrey V. Elsukov 	p1a(udps_nosum, "with no checksum\n");
776*c80211e3SAndrey V. Elsukov 	p1a(udps_noport, "dropped due to no socket\n");
77722694ebaSBruce Evans 	p(udps_noportbcast,
778*c80211e3SAndrey V. Elsukov 	    "broadcast/multicast datagram%s undelivered\n");
779*c80211e3SAndrey V. Elsukov 	p1a(udps_fullsock, "dropped due to full socket buffers\n");
780*c80211e3SAndrey V. Elsukov 	p1a(udpps_pcbhashmiss, "not for hashed pcb\n");
7819b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
7829b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
7839b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
7849b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
7859b50d902SRodney W. Grimes 		    udpstat.udps_noport -
7869b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
7879b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
7889b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
789*c80211e3SAndrey V. Elsukov 		printf("\t%ju delivered\n", (uint64_t)delivered);
790*c80211e3SAndrey V. Elsukov 	p(udps_opackets, "datagram%s output\n");
79171498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
79271498f30SBruce M Simpson 	p(udps_filtermcast,
793*c80211e3SAndrey V. Elsukov 	    "time%s multicast source filter matched\n");
7949b50d902SRodney W. Grimes #undef p
79522694ebaSBruce Evans #undef p1a
7969b50d902SRodney W. Grimes }
7979b50d902SRodney W. Grimes 
7989b50d902SRodney W. Grimes /*
799a9771948SGleb Smirnoff  * Dump CARP statistics structure.
800a9771948SGleb Smirnoff  */
801a9771948SGleb Smirnoff void
802feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
803a9771948SGleb Smirnoff {
804a9771948SGleb Smirnoff 	struct carpstats carpstat, zerostat;
805a9771948SGleb Smirnoff 	size_t len = sizeof(struct carpstats);
806a9771948SGleb Smirnoff 
807feda1a43SJohn Baldwin 	if (live) {
808a9771948SGleb Smirnoff 		if (zflag)
809a9771948SGleb Smirnoff 			memset(&zerostat, 0, len);
810a9771948SGleb Smirnoff 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
811a9771948SGleb Smirnoff 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
81245125e14SRuslan Ermilov 			if (errno != ENOENT)
813a9771948SGleb Smirnoff 				warn("sysctl: net.inet.carp.stats");
814a9771948SGleb Smirnoff 			return;
815a9771948SGleb Smirnoff 		}
816feda1a43SJohn Baldwin 	} else {
817feda1a43SJohn Baldwin 		if (off == 0)
818feda1a43SJohn Baldwin 			return;
819feda1a43SJohn Baldwin 		kread(off, &carpstat, len);
820feda1a43SJohn Baldwin 	}
821a9771948SGleb Smirnoff 
822a9771948SGleb Smirnoff 	printf("%s:\n", name);
823a9771948SGleb Smirnoff 
824a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
8257b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
826a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
8277b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f)
828a9771948SGleb Smirnoff 
8297b95a1ebSYaroslav Tykhiy 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
8307b95a1ebSYaroslav Tykhiy 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
8317b95a1ebSYaroslav Tykhiy 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
8327b95a1ebSYaroslav Tykhiy 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
8337b95a1ebSYaroslav Tykhiy 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
8347b95a1ebSYaroslav Tykhiy 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
8357b95a1ebSYaroslav Tykhiy 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
8367b95a1ebSYaroslav Tykhiy 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
8377b95a1ebSYaroslav Tykhiy 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
8387b95a1ebSYaroslav Tykhiy 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
8397b95a1ebSYaroslav Tykhiy 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
8407b95a1ebSYaroslav Tykhiy 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
8417b95a1ebSYaroslav Tykhiy 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
842a9771948SGleb Smirnoff #if notyet
843a9771948SGleb Smirnoff 	p(carps_ostates, "\t\t%s state update%s sent\n");
844a9771948SGleb Smirnoff #endif
845a9771948SGleb Smirnoff #undef p
846a9771948SGleb Smirnoff #undef p2
847a9771948SGleb Smirnoff }
848a9771948SGleb Smirnoff 
849a9771948SGleb Smirnoff /*
8509b50d902SRodney W. Grimes  * Dump IP statistics structure.
8519b50d902SRodney W. Grimes  */
8529b50d902SRodney W. Grimes void
853feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8549b50d902SRodney W. Grimes {
855c73d99b5SRuslan Ermilov 	struct ipstat ipstat, zerostat;
8564f81ef50SGarrett Wollman 	size_t len = sizeof ipstat;
8579b50d902SRodney W. Grimes 
858feda1a43SJohn Baldwin 	if (live) {
859c73d99b5SRuslan Ermilov 		if (zflag)
860c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
861c73d99b5SRuslan Ermilov 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
862c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
8634f81ef50SGarrett Wollman 			warn("sysctl: net.inet.ip.stats");
8649b50d902SRodney W. Grimes 			return;
8654f81ef50SGarrett Wollman 		}
86629dde48dSGleb Smirnoff 	} else {
86729dde48dSGleb Smirnoff 		u_long ipstat_p[sizeof(struct ipstat)/sizeof(uint64_t)];
86829dde48dSGleb Smirnoff 
86929dde48dSGleb Smirnoff 		kread(off, &ipstat_p, sizeof(ipstat_p));
87029dde48dSGleb Smirnoff 		kread_counters(ipstat_p, (uint64_t *)&ipstat,
87129dde48dSGleb Smirnoff 		    sizeof(struct ipstat)/sizeof(uint64_t));
87229dde48dSGleb Smirnoff 	}
8734f81ef50SGarrett Wollman 
8749b50d902SRodney W. Grimes 	printf("%s:\n", name);
8759b50d902SRodney W. Grimes 
8769b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
8775923c293SGleb Smirnoff     printf(m, (uintmax_t )ipstat.f, plural(ipstat.f))
87822694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
8795923c293SGleb Smirnoff     printf(m, (uintmax_t )ipstat.f)
8809b50d902SRodney W. Grimes 
8815923c293SGleb Smirnoff 	p(ips_total, "\t%ju total packet%s received\n");
8825923c293SGleb Smirnoff 	p(ips_badsum, "\t%ju bad header checksum%s\n");
8835923c293SGleb Smirnoff 	p1a(ips_toosmall, "\t%ju with size smaller than minimum\n");
8845923c293SGleb Smirnoff 	p1a(ips_tooshort, "\t%ju with data size < data length\n");
8855923c293SGleb Smirnoff 	p1a(ips_toolong, "\t%ju with ip length > max ip packet size\n");
8865923c293SGleb Smirnoff 	p1a(ips_badhlen, "\t%ju with header length < data size\n");
8875923c293SGleb Smirnoff 	p1a(ips_badlen, "\t%ju with data length < header length\n");
8885923c293SGleb Smirnoff 	p1a(ips_badoptions, "\t%ju with bad options\n");
8895923c293SGleb Smirnoff 	p1a(ips_badvers, "\t%ju with incorrect version number\n");
8905923c293SGleb Smirnoff 	p(ips_fragments, "\t%ju fragment%s received\n");
8915923c293SGleb Smirnoff 	p(ips_fragdropped, "\t%ju fragment%s dropped (dup or out of space)\n");
8925923c293SGleb Smirnoff 	p(ips_fragtimeout, "\t%ju fragment%s dropped after timeout\n");
8935923c293SGleb Smirnoff 	p(ips_reassembled, "\t%ju packet%s reassembled ok\n");
8945923c293SGleb Smirnoff 	p(ips_delivered, "\t%ju packet%s for this host\n");
8955923c293SGleb Smirnoff 	p(ips_noproto, "\t%ju packet%s for unknown/unsupported protocol\n");
8965923c293SGleb Smirnoff 	p(ips_forward, "\t%ju packet%s forwarded");
8975923c293SGleb Smirnoff 	p(ips_fastforward, " (%ju packet%s fast forwarded)");
898958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
899958d6f7fSPierre Beyssac 		putchar('\n');
9005923c293SGleb Smirnoff 	p(ips_cantforward, "\t%ju packet%s not forwardable\n");
901a5969f5fSGarrett Wollman 	p(ips_notmember,
9025923c293SGleb Smirnoff 	    "\t%ju packet%s received for unknown multicast group\n");
9035923c293SGleb Smirnoff 	p(ips_redirectsent, "\t%ju redirect%s sent\n");
9045923c293SGleb Smirnoff 	p(ips_localout, "\t%ju packet%s sent from this host\n");
9055923c293SGleb Smirnoff 	p(ips_rawout, "\t%ju packet%s sent with fabricated ip header\n");
906a5969f5fSGarrett Wollman 	p(ips_odropped,
9075923c293SGleb Smirnoff 	    "\t%ju output packet%s dropped due to no bufs, etc.\n");
9085923c293SGleb Smirnoff 	p(ips_noroute, "\t%ju output packet%s discarded due to no route\n");
9095923c293SGleb Smirnoff 	p(ips_fragmented, "\t%ju output datagram%s fragmented\n");
9105923c293SGleb Smirnoff 	p(ips_ofragments, "\t%ju fragment%s created\n");
9115923c293SGleb Smirnoff 	p(ips_cantfrag, "\t%ju datagram%s that can't be fragmented\n");
9125923c293SGleb Smirnoff 	p(ips_nogif, "\t%ju tunneling packet%s that can't find gif\n");
9135923c293SGleb Smirnoff 	p(ips_badaddr, "\t%ju datagram%s with bad address in header\n");
9149b50d902SRodney W. Grimes #undef p
91522694ebaSBruce Evans #undef p1a
9169b50d902SRodney W. Grimes }
9179b50d902SRodney W. Grimes 
91854fc657dSGeorge V. Neville-Neil /*
91954fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
92054fc657dSGeorge V. Neville-Neil  */
92154fc657dSGeorge V. Neville-Neil void
92254fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
92354fc657dSGeorge V. Neville-Neil {
92454fc657dSGeorge V. Neville-Neil 	struct arpstat arpstat, zerostat;
92554fc657dSGeorge V. Neville-Neil 	size_t len = sizeof(arpstat);
92654fc657dSGeorge V. Neville-Neil 
92754fc657dSGeorge V. Neville-Neil 	if (live) {
92854fc657dSGeorge V. Neville-Neil 		if (zflag)
92954fc657dSGeorge V. Neville-Neil 			memset(&zerostat, 0, len);
93054fc657dSGeorge V. Neville-Neil 		if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
93154fc657dSGeorge V. Neville-Neil 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
93254fc657dSGeorge V. Neville-Neil 			warn("sysctl: net.link.ether.arp.stats");
93354fc657dSGeorge V. Neville-Neil 			return;
93454fc657dSGeorge V. Neville-Neil 		}
93554fc657dSGeorge V. Neville-Neil 	} else
93654fc657dSGeorge V. Neville-Neil 		kread(off, &arpstat, len);
93754fc657dSGeorge V. Neville-Neil 
93854fc657dSGeorge V. Neville-Neil 	printf("%s:\n", name);
93954fc657dSGeorge V. Neville-Neil 
94054fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
941*c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)arpstat.f, plural(arpstat.f))
94254fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
943*c80211e3SAndrey V. Elsukov     printf("\t%ju " m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
94454fc657dSGeorge V. Neville-Neil 
945*c80211e3SAndrey V. Elsukov 	p(txrequests, "ARP request%s sent\n");
946*c80211e3SAndrey V. Elsukov 	p2(txreplies, "ARP repl%s sent\n");
947*c80211e3SAndrey V. Elsukov 	p(rxrequests, "ARP request%s received\n");
948*c80211e3SAndrey V. Elsukov 	p2(rxreplies, "ARP repl%s received\n");
949*c80211e3SAndrey V. Elsukov 	p(received, "ARP packet%s received\n");
950*c80211e3SAndrey V. Elsukov 	p(dropped, "total packet%s dropped due to no ARP entry\n");
951*c80211e3SAndrey V. Elsukov 	p(timeouts, "ARP entry%s timed out\n");
952*c80211e3SAndrey V. Elsukov 	p(dupips, "Duplicate IP%s seen\n");
95354fc657dSGeorge V. Neville-Neil #undef p
95454fc657dSGeorge V. Neville-Neil #undef p2
95554fc657dSGeorge V. Neville-Neil }
95654fc657dSGeorge V. Neville-Neil 
95754fc657dSGeorge V. Neville-Neil 
95854fc657dSGeorge V. Neville-Neil 
9594063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
9604063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
9619b50d902SRodney W. Grimes 	"#1",
9629b50d902SRodney W. Grimes 	"#2",
9634063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
9644063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
9654063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
9669b50d902SRodney W. Grimes 	"#6",
9679b50d902SRodney W. Grimes 	"#7",
9684063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
9694063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
9704063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
9714063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
9724063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
9734063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
9744063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
9754063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
9764063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
9774063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
9784063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
9794063583aSMaxim Konovalov 	"#19",
9804063583aSMaxim Konovalov 	"#20",
9814063583aSMaxim Konovalov 	"#21",
9824063583aSMaxim Konovalov 	"#22",
9834063583aSMaxim Konovalov 	"#23",
9844063583aSMaxim Konovalov 	"#24",
9854063583aSMaxim Konovalov 	"#25",
9864063583aSMaxim Konovalov 	"#26",
9874063583aSMaxim Konovalov 	"#27",
9884063583aSMaxim Konovalov 	"#28",
9894063583aSMaxim Konovalov 	"#29",
9904063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
9914063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
9924063583aSMaxim Konovalov 	"mobile host redirect",
9934063583aSMaxim Konovalov 	"IPv6 where-are-you",
9944063583aSMaxim Konovalov 	"IPv6 i-am-here",
9954063583aSMaxim Konovalov 	"mobile registration req",
9964063583aSMaxim Konovalov 	"mobile registration reply",
9974063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
9984063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
9994063583aSMaxim Konovalov 	"icmp SKIP",
10004063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
10019b50d902SRodney W. Grimes };
10029b50d902SRodney W. Grimes 
10039b50d902SRodney W. Grimes /*
10049b50d902SRodney W. Grimes  * Dump ICMP statistics.
10059b50d902SRodney W. Grimes  */
10069b50d902SRodney W. Grimes void
1007feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
10089b50d902SRodney W. Grimes {
1009c73d99b5SRuslan Ermilov 	struct icmpstat icmpstat, zerostat;
10104e00c309SGarrett Wollman 	int i, first;
10114e00c309SGarrett Wollman 	size_t len;
10129b50d902SRodney W. Grimes 
10134e00c309SGarrett Wollman 	len = sizeof icmpstat;
1014feda1a43SJohn Baldwin 	if (live) {
1015c73d99b5SRuslan Ermilov 		if (zflag)
1016c73d99b5SRuslan Ermilov 			memset(&zerostat, 0, len);
1017feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
1018c73d99b5SRuslan Ermilov 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1019c73d99b5SRuslan Ermilov 			warn("sysctl: net.inet.icmp.stats");
1020c73d99b5SRuslan Ermilov 			return;
1021c73d99b5SRuslan Ermilov 		}
1022feda1a43SJohn Baldwin 	} else
1023feda1a43SJohn Baldwin 		kread(off, &icmpstat, len);
10244e00c309SGarrett Wollman 
10259b50d902SRodney W. Grimes 	printf("%s:\n", name);
10269b50d902SRodney W. Grimes 
10279b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
10289b50d902SRodney W. Grimes     printf(m, icmpstat.f, plural(icmpstat.f))
102922694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
103022694ebaSBruce Evans     printf(m, icmpstat.f)
1031bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1032bd714208SRuslan Ermilov     printf(m, icmpstat.f, plurales(icmpstat.f))
10339b50d902SRodney W. Grimes 
10347d56c0eeSAlexander Langer 	p(icps_error, "\t%lu call%s to icmp_error\n");
10359b50d902SRodney W. Grimes 	p(icps_oldicmp,
1036f99a4046SMike Makonnen 	    "\t%lu error%s not generated in response to an icmp message\n");
10379b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10389b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
10399b50d902SRodney W. Grimes 			if (first) {
10409b50d902SRodney W. Grimes 				printf("\tOutput histogram:\n");
10419b50d902SRodney W. Grimes 				first = 0;
10429b50d902SRodney W. Grimes 			}
10434063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10447d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10459b50d902SRodney W. Grimes 					icmpstat.icps_outhist[i]);
10464063583aSMaxim Konovalov 			else
10474063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10484063583aSMaxim Konovalov 					icmpstat.icps_outhist[i]);
10499b50d902SRodney W. Grimes 		}
10507d56c0eeSAlexander Langer 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
1051bc215f59SDavid E. O'Brien 	p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
1052bc215f59SDavid E. O'Brien 	p(icps_checksum, "\t%lu message%s with bad checksum\n");
10537d56c0eeSAlexander Langer 	p(icps_badlen, "\t%lu message%s with bad length\n");
105422694ebaSBruce Evans 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
105522694ebaSBruce Evans 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
10569b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
10579b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
10589b50d902SRodney W. Grimes 			if (first) {
10599b50d902SRodney W. Grimes 				printf("\tInput histogram:\n");
10609b50d902SRodney W. Grimes 				first = 0;
10619b50d902SRodney W. Grimes 			}
10624063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
10637d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
10649b50d902SRodney W. Grimes 				    icmpstat.icps_inhist[i]);
10654063583aSMaxim Konovalov 			else
10664063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
10674063583aSMaxim Konovalov 				    icmpstat.icps_inhist[i]);
10689b50d902SRodney W. Grimes 		}
10697d56c0eeSAlexander Langer 	p(icps_reflect, "\t%lu message response%s generated\n");
1070bd714208SRuslan Ermilov 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
10710237ca7bSRuslan Ermilov 	p(icps_noroute, "\t%lu no return route%s\n");
10729b50d902SRodney W. Grimes #undef p
107322694ebaSBruce Evans #undef p1a
1074bd714208SRuslan Ermilov #undef p2
1075feda1a43SJohn Baldwin 	if (live) {
10764e00c309SGarrett Wollman 		len = sizeof i;
1077feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1078feda1a43SJohn Baldwin 		    0)
10794e00c309SGarrett Wollman 			return;
10804e00c309SGarrett Wollman 		printf("\tICMP address mask responses are %sabled\n",
10814e00c309SGarrett Wollman 		    i ? "en" : "dis");
10829b50d902SRodney W. Grimes 	}
1083feda1a43SJohn Baldwin }
10849b50d902SRodney W. Grimes 
1085d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
10869b50d902SRodney W. Grimes /*
1087d10910e6SBruce M Simpson  * Dump IGMP statistics structure (pre 8.x kernel).
10889b50d902SRodney W. Grimes  */
1089d10910e6SBruce M Simpson static void
109096c3073fSXin LI igmp_stats_live_old(const char *name)
10919b50d902SRodney W. Grimes {
1092d10910e6SBruce M Simpson 	struct oigmpstat oigmpstat, zerostat;
1093d10910e6SBruce M Simpson 	size_t len = sizeof(oigmpstat);
10949b50d902SRodney W. Grimes 
1095c73d99b5SRuslan Ermilov 	if (zflag)
1096c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
1097d10910e6SBruce M Simpson 	if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1098c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
10994f81ef50SGarrett Wollman 		warn("sysctl: net.inet.igmp.stats");
11009b50d902SRodney W. Grimes 		return;
11014f81ef50SGarrett Wollman 	}
11024f81ef50SGarrett Wollman 
11039b50d902SRodney W. Grimes 	printf("%s:\n", name);
11049b50d902SRodney W. Grimes 
1105d10910e6SBruce M Simpson #define	p(f, m) if (oigmpstat.f || sflag <= 1) \
1106d10910e6SBruce M Simpson     printf(m, oigmpstat.f, plural(oigmpstat.f))
1107d10910e6SBruce M Simpson #define	py(f, m) if (oigmpstat.f || sflag <= 1) \
1108d10910e6SBruce M Simpson     printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
11099b50d902SRodney W. Grimes 	p(igps_rcv_total, "\t%u message%s received\n");
11109b50d902SRodney W. Grimes 	p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
11119b50d902SRodney W. Grimes 	p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
11129b50d902SRodney W. Grimes 	py(igps_rcv_queries, "\t%u membership quer%s received\n");
11133feeb332SDavid E. O'Brien 	py(igps_rcv_badqueries,
11143feeb332SDavid E. O'Brien 	    "\t%u membership quer%s received with invalid field(s)\n");
11159b50d902SRodney W. Grimes 	p(igps_rcv_reports, "\t%u membership report%s received\n");
11163feeb332SDavid E. O'Brien 	p(igps_rcv_badreports,
11173feeb332SDavid E. O'Brien 	    "\t%u membership report%s received with invalid field(s)\n");
11183feeb332SDavid E. O'Brien 	p(igps_rcv_ourreports,
11193feeb332SDavid E. O'Brien "\t%u membership report%s received for groups to which we belong\n");
11209b50d902SRodney W. Grimes         p(igps_snd_reports, "\t%u membership report%s sent\n");
11219b50d902SRodney W. Grimes #undef p
11229b50d902SRodney W. Grimes #undef py
11239b50d902SRodney W. Grimes }
1124d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1125d10910e6SBruce M Simpson 
1126d10910e6SBruce M Simpson /*
1127d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1128d10910e6SBruce M Simpson  */
1129d10910e6SBruce M Simpson void
1130d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1131d10910e6SBruce M Simpson {
1132d10910e6SBruce M Simpson 	struct igmpstat igmpstat, zerostat;
1133d10910e6SBruce M Simpson 	size_t len;
1134d10910e6SBruce M Simpson 
1135d10910e6SBruce M Simpson #ifndef BURN_BRIDGES
1136d10910e6SBruce M Simpson 	if (live) {
1137d10910e6SBruce M Simpson 		/*
1138d10910e6SBruce M Simpson 		 * Detect if we are being run against a pre-IGMPv3 kernel.
1139d10910e6SBruce M Simpson 		 * We cannot do this for a core file as the legacy
1140d10910e6SBruce M Simpson 		 * struct igmpstat has no size field, nor does it
1141d10910e6SBruce M Simpson 		 * export it in any readily-available symbols.
1142d10910e6SBruce M Simpson 		 */
1143d10910e6SBruce M Simpson 		len = 0;
1144d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1145d10910e6SBruce M Simpson 		    0) < 0) {
1146d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1147d10910e6SBruce M Simpson 			return;
1148d10910e6SBruce M Simpson 		}
1149d10910e6SBruce M Simpson 		if (len < sizeof(igmpstat)) {
115096c3073fSXin LI 			igmp_stats_live_old(name);
1151d10910e6SBruce M Simpson 			return;
1152d10910e6SBruce M Simpson 		}
1153d10910e6SBruce M Simpson 	}
1154d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */
1155d10910e6SBruce M Simpson 
1156d10910e6SBruce M Simpson 	len = sizeof(igmpstat);
1157d10910e6SBruce M Simpson 	if (live) {
1158d10910e6SBruce M Simpson 		if (zflag)
1159d10910e6SBruce M Simpson 			memset(&zerostat, 0, len);
1160d10910e6SBruce M Simpson 		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1161d10910e6SBruce M Simpson 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1162d10910e6SBruce M Simpson 			warn("sysctl: net.inet.igmp.stats");
1163d10910e6SBruce M Simpson 			return;
1164d10910e6SBruce M Simpson 		}
1165d10910e6SBruce M Simpson 	} else {
1166d10910e6SBruce M Simpson 		len = sizeof(igmpstat);
1167d10910e6SBruce M Simpson 		kread(off, &igmpstat, len);
1168d10910e6SBruce M Simpson 	}
1169d10910e6SBruce M Simpson 
1170d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1171d10910e6SBruce M Simpson 		warnx("%s: version mismatch (%d != %d)", __func__,
1172d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1173d10910e6SBruce M Simpson 	}
1174d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1175d10910e6SBruce M Simpson 		warnx("%s: size mismatch (%d != %d)", __func__,
1176d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1177d10910e6SBruce M Simpson 	}
1178d10910e6SBruce M Simpson 
1179d10910e6SBruce M Simpson 	printf("%s:\n", name);
1180d10910e6SBruce M Simpson 
1181d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1182d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1183d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1184d10910e6SBruce M Simpson     printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1185d10910e6SBruce M Simpson 	p64(igps_rcv_total, "\t%ju message%s received\n");
1186d10910e6SBruce M Simpson 	p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1187d10910e6SBruce M Simpson 	p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1188d10910e6SBruce M Simpson 	p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1189d10910e6SBruce M Simpson 	py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1190d10910e6SBruce M Simpson 	py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1191d10910e6SBruce M Simpson 	py64(igps_rcv_badqueries,
1192d10910e6SBruce M Simpson 	    "\t%ju membership quer%s received with invalid field(s)\n");
1193d10910e6SBruce M Simpson 	py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1194d10910e6SBruce M Simpson 	py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1195d10910e6SBruce M Simpson 	py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1196d10910e6SBruce M Simpson 	py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1197d10910e6SBruce M Simpson 	p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1198d10910e6SBruce M Simpson 	p64(igps_rcv_badreports,
1199d10910e6SBruce M Simpson 	    "\t%ju membership report%s received with invalid field(s)\n");
1200d10910e6SBruce M Simpson 	p64(igps_rcv_ourreports,
1201d10910e6SBruce M Simpson "\t%ju membership report%s received for groups to which we belong\n");
1202d10910e6SBruce M Simpson         p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1203d10910e6SBruce M Simpson         p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1204d10910e6SBruce M Simpson #undef p64
1205d10910e6SBruce M Simpson #undef py64
1206d10910e6SBruce M Simpson }
12079b50d902SRodney W. Grimes 
12089b50d902SRodney W. Grimes /*
1209c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1210c7b9b5bbSJeffrey Hsu  */
1211c7b9b5bbSJeffrey Hsu void
1212feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1213feda1a43SJohn Baldwin     int proto __unused)
1214c7b9b5bbSJeffrey Hsu {
1215c7b9b5bbSJeffrey Hsu 	struct pimstat pimstat, zerostat;
1216c7b9b5bbSJeffrey Hsu 	size_t len = sizeof pimstat;
1217c7b9b5bbSJeffrey Hsu 
1218feda1a43SJohn Baldwin 	if (live) {
1219c7b9b5bbSJeffrey Hsu 		if (zflag)
1220c7b9b5bbSJeffrey Hsu 			memset(&zerostat, 0, len);
1221c7b9b5bbSJeffrey Hsu 		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1222c7b9b5bbSJeffrey Hsu 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
12236d7c0d2fSDag-Erling Smørgrav 			if (errno != ENOENT)
1224c7b9b5bbSJeffrey Hsu 				warn("sysctl: net.inet.pim.stats");
1225c7b9b5bbSJeffrey Hsu 			return;
1226c7b9b5bbSJeffrey Hsu 		}
1227feda1a43SJohn Baldwin 	} else {
1228feda1a43SJohn Baldwin 		if (off == 0)
1229feda1a43SJohn Baldwin 			return;
1230feda1a43SJohn Baldwin 		kread(off, &pimstat, len);
1231feda1a43SJohn Baldwin 	}
1232c7b9b5bbSJeffrey Hsu 
1233c7b9b5bbSJeffrey Hsu 	printf("%s:\n", name);
1234c7b9b5bbSJeffrey Hsu 
1235c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
12367b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1237c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
12387b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
12397b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
12407b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
12417b95a1ebSYaroslav Tykhiy 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
12427b95a1ebSYaroslav Tykhiy         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
12437b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
12447b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
12457b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
12463feeb332SDavid E. O'Brien 	p(pims_rcv_registers_wrongiif,
12473feeb332SDavid E. O'Brien 	    "\t%ju data register message%s received on wrong iif\n");
12487b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
12497b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
12507b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1251c7b9b5bbSJeffrey Hsu #undef p
1252c7b9b5bbSJeffrey Hsu #undef py
1253c7b9b5bbSJeffrey Hsu }
1254c7b9b5bbSJeffrey Hsu 
1255c7b9b5bbSJeffrey Hsu /*
12569b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
12579b50d902SRodney W. Grimes  */
12589b50d902SRodney W. Grimes void
1259a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port)
12609b50d902SRodney W. Grimes {
12619b50d902SRodney W. Grimes 	struct servent *sp = 0;
12629b50d902SRodney W. Grimes 	char line[80], *cp;
1263cfa1ca9dSYoshinobu Inoue 	int width;
12649b50d902SRodney W. Grimes 
1265080b7f49SDag-Erling Smørgrav 	if (Wflag)
1266080b7f49SDag-Erling Smørgrav 	    sprintf(line, "%s.", inetname(in));
1267080b7f49SDag-Erling Smørgrav 	else
1268a01e3379SDavid Malone 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1269b3608ae1SEd Schouten 	cp = strchr(line, '\0');
1270a01e3379SDavid Malone 	if (!num_port && port)
12719b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
12729b50d902SRodney W. Grimes 	if (sp || port == 0)
12731ef69972SAndrey A. Chernov 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
12749b50d902SRodney W. Grimes 	else
12759b50d902SRodney W. Grimes 		sprintf(cp, "%d ", ntohs((u_short)port));
1276080b7f49SDag-Erling Smørgrav 	width = (Aflag && !Wflag) ? 18 : 22;
1277080b7f49SDag-Erling Smørgrav 	if (Wflag)
1278080b7f49SDag-Erling Smørgrav 	    printf("%-*s ", width, line);
1279080b7f49SDag-Erling Smørgrav 	else
1280cfa1ca9dSYoshinobu Inoue 	    printf("%-*.*s ", width, width, line);
12819b50d902SRodney W. Grimes }
12829b50d902SRodney W. Grimes 
12839b50d902SRodney W. Grimes /*
12849b50d902SRodney W. Grimes  * Construct an Internet address representation.
128565ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
12869b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
12879b50d902SRodney W. Grimes  */
12889b50d902SRodney W. Grimes char *
12895e051718SAssar Westerlund inetname(struct in_addr *inp)
12909b50d902SRodney W. Grimes {
1291a01e3379SDavid Malone 	char *cp;
1292d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
12939b50d902SRodney W. Grimes 	struct hostent *hp;
12949b50d902SRodney W. Grimes 	struct netent *np;
12959b50d902SRodney W. Grimes 
12969b50d902SRodney W. Grimes 	cp = 0;
129765ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
12989b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
12999b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
13009b50d902SRodney W. Grimes 
13019b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
13029b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
13039b50d902SRodney W. Grimes 			if (np)
13049b50d902SRodney W. Grimes 				cp = np->n_name;
13059b50d902SRodney W. Grimes 		}
13069b50d902SRodney W. Grimes 		if (cp == 0) {
13079b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
13089b50d902SRodney W. Grimes 			if (hp) {
13099b50d902SRodney W. Grimes 				cp = hp->h_name;
1310d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
13119b50d902SRodney W. Grimes 			}
13129b50d902SRodney W. Grimes 		}
13139b50d902SRodney W. Grimes 	}
13149b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
13159b50d902SRodney W. Grimes 		strcpy(line, "*");
13169a1f6729SWarner Losh 	else if (cp) {
13171c109628SXin LI 		strlcpy(line, cp, sizeof(line));
13189a1f6729SWarner Losh 	} else {
13199b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
132022694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
132122694ebaSBruce Evans 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
13229b50d902SRodney W. Grimes 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
13239b50d902SRodney W. Grimes 	}
13249b50d902SRodney W. Grimes 	return (line);
13259b50d902SRodney W. Grimes }
1326