Deleted Added
full compact
inet.c (175077) inet.c (179027)
1/*-
2 * Copyright (c) 1983, 1988, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
37#endif /* not lint */
38#endif
39
40#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1983, 1988, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
37#endif /* not lint */
38#endif
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/usr.bin/netstat/inet.c 175077 2008-01-04 03:09:28Z obrien $");
41__FBSDID("$FreeBSD: head/usr.bin/netstat/inet.c 179027 2008-05-15 20:18:44Z gnn $");
42
43#include <sys/param.h>
44#include <sys/queue.h>
45#include <sys/domain.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/sysctl.h>
49#include <sys/protosw.h>
50
51#include <net/route.h>
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#include <netinet/ip_carp.h>
56#ifdef INET6
57#include <netinet/ip6.h>
58#endif /* INET6 */
59#include <netinet/in_pcb.h>
60#include <netinet/ip_icmp.h>
61#include <netinet/icmp_var.h>
62#include <netinet/igmp_var.h>
63#include <netinet/ip_var.h>
64#include <netinet/pim_var.h>
65#include <netinet/tcp.h>
66#include <netinet/tcpip.h>
67#include <netinet/tcp_seq.h>
68#define TCPSTATES
69#include <netinet/tcp_fsm.h>
70#include <netinet/tcp_timer.h>
71#include <netinet/tcp_var.h>
72#include <netinet/tcp_debug.h>
73#include <netinet/udp.h>
74#include <netinet/udp_var.h>
75
76#include <arpa/inet.h>
77#include <err.h>
78#include <errno.h>
79#include <libutil.h>
80#include <netdb.h>
81#include <stdint.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <unistd.h>
86#include "netstat.h"
87
88char *inetname(struct in_addr *);
89void inetprint(struct in_addr *, int, const char *, int);
90#ifdef INET6
91static int udp_done, tcp_done;
92#endif /* INET6 */
93
94static int
95pcblist_sysctl(int proto, char **bufp, int istcp)
96{
97 const char *mibvar;
98 char *buf;
99 size_t len;
100
101 switch (proto) {
102 case IPPROTO_TCP:
103 mibvar = "net.inet.tcp.pcblist";
104 break;
105 case IPPROTO_UDP:
106 mibvar = "net.inet.udp.pcblist";
107 break;
108 case IPPROTO_DIVERT:
109 mibvar = "net.inet.divert.pcblist";
110 break;
111 default:
112 mibvar = "net.inet.raw.pcblist";
113 break;
114 }
115
116 len = 0;
117 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118 if (errno != ENOENT)
119 warn("sysctl: %s", mibvar);
120 return (0);
121 }
122 if ((buf = malloc(len)) == 0) {
123 warnx("malloc %lu bytes", (u_long)len);
124 return (0);
125 }
126 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 warn("sysctl: %s", mibvar);
128 free(buf);
129 return (0);
130 }
131 *bufp = buf;
132 return (1);
133}
134
135/*
136 * Copied directly from uipc_socket2.c. We leave out some fields that are in
137 * nested structures that aren't used to avoid extra work.
138 */
139static void
140sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141{
142 xsb->sb_cc = sb->sb_cc;
143 xsb->sb_hiwat = sb->sb_hiwat;
144 xsb->sb_mbcnt = sb->sb_mbcnt;
42
43#include <sys/param.h>
44#include <sys/queue.h>
45#include <sys/domain.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/sysctl.h>
49#include <sys/protosw.h>
50
51#include <net/route.h>
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#include <netinet/ip_carp.h>
56#ifdef INET6
57#include <netinet/ip6.h>
58#endif /* INET6 */
59#include <netinet/in_pcb.h>
60#include <netinet/ip_icmp.h>
61#include <netinet/icmp_var.h>
62#include <netinet/igmp_var.h>
63#include <netinet/ip_var.h>
64#include <netinet/pim_var.h>
65#include <netinet/tcp.h>
66#include <netinet/tcpip.h>
67#include <netinet/tcp_seq.h>
68#define TCPSTATES
69#include <netinet/tcp_fsm.h>
70#include <netinet/tcp_timer.h>
71#include <netinet/tcp_var.h>
72#include <netinet/tcp_debug.h>
73#include <netinet/udp.h>
74#include <netinet/udp_var.h>
75
76#include <arpa/inet.h>
77#include <err.h>
78#include <errno.h>
79#include <libutil.h>
80#include <netdb.h>
81#include <stdint.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <unistd.h>
86#include "netstat.h"
87
88char *inetname(struct in_addr *);
89void inetprint(struct in_addr *, int, const char *, int);
90#ifdef INET6
91static int udp_done, tcp_done;
92#endif /* INET6 */
93
94static int
95pcblist_sysctl(int proto, char **bufp, int istcp)
96{
97 const char *mibvar;
98 char *buf;
99 size_t len;
100
101 switch (proto) {
102 case IPPROTO_TCP:
103 mibvar = "net.inet.tcp.pcblist";
104 break;
105 case IPPROTO_UDP:
106 mibvar = "net.inet.udp.pcblist";
107 break;
108 case IPPROTO_DIVERT:
109 mibvar = "net.inet.divert.pcblist";
110 break;
111 default:
112 mibvar = "net.inet.raw.pcblist";
113 break;
114 }
115
116 len = 0;
117 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118 if (errno != ENOENT)
119 warn("sysctl: %s", mibvar);
120 return (0);
121 }
122 if ((buf = malloc(len)) == 0) {
123 warnx("malloc %lu bytes", (u_long)len);
124 return (0);
125 }
126 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 warn("sysctl: %s", mibvar);
128 free(buf);
129 return (0);
130 }
131 *bufp = buf;
132 return (1);
133}
134
135/*
136 * Copied directly from uipc_socket2.c. We leave out some fields that are in
137 * nested structures that aren't used to avoid extra work.
138 */
139static void
140sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141{
142 xsb->sb_cc = sb->sb_cc;
143 xsb->sb_hiwat = sb->sb_hiwat;
144 xsb->sb_mbcnt = sb->sb_mbcnt;
145 xsb->sb_mcnt = sb->sb_mcnt;
146 xsb->sb_ccnt = sb->sb_ccnt;
145 xsb->sb_mbmax = sb->sb_mbmax;
146 xsb->sb_lowat = sb->sb_lowat;
147 xsb->sb_flags = sb->sb_flags;
148 xsb->sb_timeo = sb->sb_timeo;
149}
150
151int
152sotoxsocket(struct socket *so, struct xsocket *xso)
153{
154 struct protosw proto;
155 struct domain domain;
156
157 bzero(xso, sizeof *xso);
158 xso->xso_len = sizeof *xso;
159 xso->xso_so = so;
160 xso->so_type = so->so_type;
161 xso->so_options = so->so_options;
162 xso->so_linger = so->so_linger;
163 xso->so_state = so->so_state;
164 xso->so_pcb = so->so_pcb;
165 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
166 return (-1);
167 xso->xso_protocol = proto.pr_protocol;
168 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
169 return (-1);
170 xso->xso_family = domain.dom_family;
171 xso->so_qlen = so->so_qlen;
172 xso->so_incqlen = so->so_incqlen;
173 xso->so_qlimit = so->so_qlimit;
174 xso->so_timeo = so->so_timeo;
175 xso->so_error = so->so_error;
176 xso->so_oobmark = so->so_oobmark;
177 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
178 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
179 return (0);
180}
181
182static int
183pcblist_kvm(u_long off, char **bufp, int istcp)
184{
185 struct inpcbinfo pcbinfo;
186 struct inpcbhead listhead;
187 struct inpcb *inp;
188 struct xinpcb xi;
189 struct xinpgen xig;
190 struct xtcpcb xt;
191 struct socket so;
192 struct xsocket *xso;
193 char *buf, *p;
194 size_t len;
195
196 if (off == 0)
197 return (0);
198 kread(off, &pcbinfo, sizeof(pcbinfo));
199 if (istcp)
200 len = 2 * sizeof(xig) +
201 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
202 sizeof(struct xtcpcb);
203 else
204 len = 2 * sizeof(xig) +
205 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
206 sizeof(struct xinpcb);
207 if ((buf = malloc(len)) == 0) {
208 warnx("malloc %lu bytes", (u_long)len);
209 return (0);
210 }
211 p = buf;
212
213#define COPYOUT(obj, size) do { \
214 if (len < (size)) { \
215 warnx("buffer size exceeded"); \
216 goto fail; \
217 } \
218 bcopy((obj), p, (size)); \
219 len -= (size); \
220 p += (size); \
221} while (0)
222
223#define KREAD(off, buf, len) do { \
224 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
225 goto fail; \
226} while (0)
227
228 /* Write out header. */
229 xig.xig_len = sizeof xig;
230 xig.xig_count = pcbinfo.ipi_count;
231 xig.xig_gen = pcbinfo.ipi_gencnt;
232 xig.xig_sogen = 0;
233 COPYOUT(&xig, sizeof xig);
234
235 /* Walk the PCB list. */
236 xt.xt_len = sizeof xt;
237 xi.xi_len = sizeof xi;
238 if (istcp)
239 xso = &xt.xt_socket;
240 else
241 xso = &xi.xi_socket;
242 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
243 LIST_FOREACH(inp, &listhead, inp_list) {
244 if (istcp) {
245 KREAD(inp, &xt.xt_inp, sizeof(*inp));
246 inp = &xt.xt_inp;
247 } else {
248 KREAD(inp, &xi.xi_inp, sizeof(*inp));
249 inp = &xi.xi_inp;
250 }
251
252 if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
253 continue;
254
255 if (istcp) {
256 if (inp->inp_ppcb == NULL)
257 bzero(&xt.xt_tp, sizeof xt.xt_tp);
258 else if (inp->inp_vflag & INP_TIMEWAIT) {
259 bzero(&xt.xt_tp, sizeof xt.xt_tp);
260 xt.xt_tp.t_state = TCPS_TIME_WAIT;
261 } else
262 KREAD(inp->inp_ppcb, &xt.xt_tp,
263 sizeof xt.xt_tp);
264 }
265 if (inp->inp_socket) {
266 KREAD(inp->inp_socket, &so, sizeof(so));
267 if (sotoxsocket(&so, xso) != 0)
268 goto fail;
269 } else {
270 bzero(xso, sizeof(*xso));
271 if (istcp)
272 xso->xso_protocol = IPPROTO_TCP;
273 }
274 if (istcp)
275 COPYOUT(&xt, sizeof xt);
276 else
277 COPYOUT(&xi, sizeof xi);
278 }
279
280 /* Reread the pcbinfo and write out the footer. */
281 kread(off, &pcbinfo, sizeof(pcbinfo));
282 xig.xig_count = pcbinfo.ipi_count;
283 xig.xig_gen = pcbinfo.ipi_gencnt;
284 COPYOUT(&xig, sizeof xig);
285
286 *bufp = buf;
287 return (1);
288
289fail:
290 free(buf);
291 return (0);
292#undef COPYOUT
293#undef KREAD
294}
295
296/*
297 * Print a summary of connections related to an Internet
298 * protocol. For TCP, also give state of connection.
299 * Listening processes (aflag) are suppressed unless the
300 * -a (all) flag is specified.
301 */
302void
303protopr(u_long off, const char *name, int af1, int proto)
304{
305 int istcp;
306 static int first = 1;
307 char *buf;
308 const char *vchar;
309 struct tcpcb *tp = NULL;
310 struct inpcb *inp;
311 struct xinpgen *xig, *oxig;
312 struct xsocket *so;
313
314 istcp = 0;
315 switch (proto) {
316 case IPPROTO_TCP:
317#ifdef INET6
318 if (tcp_done != 0)
319 return;
320 else
321 tcp_done = 1;
322#endif
323 istcp = 1;
324 break;
325 case IPPROTO_UDP:
326#ifdef INET6
327 if (udp_done != 0)
328 return;
329 else
330 udp_done = 1;
331#endif
332 break;
333 }
334 if (live) {
335 if (!pcblist_sysctl(proto, &buf, istcp))
336 return;
337 } else {
338 if (!pcblist_kvm(off, &buf, istcp))
339 return;
340 }
341
342 oxig = xig = (struct xinpgen *)buf;
343 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
344 xig->xig_len > sizeof(struct xinpgen);
345 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
346 if (istcp) {
347 tp = &((struct xtcpcb *)xig)->xt_tp;
348 inp = &((struct xtcpcb *)xig)->xt_inp;
349 so = &((struct xtcpcb *)xig)->xt_socket;
350 } else {
351 inp = &((struct xinpcb *)xig)->xi_inp;
352 so = &((struct xinpcb *)xig)->xi_socket;
353 }
354
355 /* Ignore sockets for protocols other than the desired one. */
356 if (so->xso_protocol != proto)
357 continue;
358
359 /* Ignore PCBs which were freed during copyout. */
360 if (inp->inp_gencnt > oxig->xig_gen)
361 continue;
362
363 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
364#ifdef INET6
365 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
366#endif /* INET6 */
367 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
368#ifdef INET6
369 && (inp->inp_vflag & INP_IPV6) == 0
370#endif /* INET6 */
371 ))
372 )
373 continue;
374 if (!aflag &&
375 (
376 (istcp && tp->t_state == TCPS_LISTEN)
377 || (af1 == AF_INET &&
378 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
379#ifdef INET6
380 || (af1 == AF_INET6 &&
381 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
382#endif /* INET6 */
383 || (af1 == AF_UNSPEC &&
384 (((inp->inp_vflag & INP_IPV4) != 0 &&
385 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
386#ifdef INET6
387 || ((inp->inp_vflag & INP_IPV6) != 0 &&
388 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
389#endif
390 ))
391 ))
392 continue;
393
394 if (first) {
395 if (!Lflag) {
396 printf("Active Internet connections");
397 if (aflag)
398 printf(" (including servers)");
399 } else
400 printf(
401 "Current listen queue sizes (qlen/incqlen/maxqlen)");
402 putchar('\n');
403 if (Aflag)
404 printf("%-8.8s ", "Tcpcb");
405 if (Lflag)
406 printf("%-5.5s %-14.14s %-22.22s\n",
407 "Proto", "Listen", "Local Address");
147 xsb->sb_mbmax = sb->sb_mbmax;
148 xsb->sb_lowat = sb->sb_lowat;
149 xsb->sb_flags = sb->sb_flags;
150 xsb->sb_timeo = sb->sb_timeo;
151}
152
153int
154sotoxsocket(struct socket *so, struct xsocket *xso)
155{
156 struct protosw proto;
157 struct domain domain;
158
159 bzero(xso, sizeof *xso);
160 xso->xso_len = sizeof *xso;
161 xso->xso_so = so;
162 xso->so_type = so->so_type;
163 xso->so_options = so->so_options;
164 xso->so_linger = so->so_linger;
165 xso->so_state = so->so_state;
166 xso->so_pcb = so->so_pcb;
167 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
168 return (-1);
169 xso->xso_protocol = proto.pr_protocol;
170 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
171 return (-1);
172 xso->xso_family = domain.dom_family;
173 xso->so_qlen = so->so_qlen;
174 xso->so_incqlen = so->so_incqlen;
175 xso->so_qlimit = so->so_qlimit;
176 xso->so_timeo = so->so_timeo;
177 xso->so_error = so->so_error;
178 xso->so_oobmark = so->so_oobmark;
179 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
180 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
181 return (0);
182}
183
184static int
185pcblist_kvm(u_long off, char **bufp, int istcp)
186{
187 struct inpcbinfo pcbinfo;
188 struct inpcbhead listhead;
189 struct inpcb *inp;
190 struct xinpcb xi;
191 struct xinpgen xig;
192 struct xtcpcb xt;
193 struct socket so;
194 struct xsocket *xso;
195 char *buf, *p;
196 size_t len;
197
198 if (off == 0)
199 return (0);
200 kread(off, &pcbinfo, sizeof(pcbinfo));
201 if (istcp)
202 len = 2 * sizeof(xig) +
203 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
204 sizeof(struct xtcpcb);
205 else
206 len = 2 * sizeof(xig) +
207 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
208 sizeof(struct xinpcb);
209 if ((buf = malloc(len)) == 0) {
210 warnx("malloc %lu bytes", (u_long)len);
211 return (0);
212 }
213 p = buf;
214
215#define COPYOUT(obj, size) do { \
216 if (len < (size)) { \
217 warnx("buffer size exceeded"); \
218 goto fail; \
219 } \
220 bcopy((obj), p, (size)); \
221 len -= (size); \
222 p += (size); \
223} while (0)
224
225#define KREAD(off, buf, len) do { \
226 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
227 goto fail; \
228} while (0)
229
230 /* Write out header. */
231 xig.xig_len = sizeof xig;
232 xig.xig_count = pcbinfo.ipi_count;
233 xig.xig_gen = pcbinfo.ipi_gencnt;
234 xig.xig_sogen = 0;
235 COPYOUT(&xig, sizeof xig);
236
237 /* Walk the PCB list. */
238 xt.xt_len = sizeof xt;
239 xi.xi_len = sizeof xi;
240 if (istcp)
241 xso = &xt.xt_socket;
242 else
243 xso = &xi.xi_socket;
244 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
245 LIST_FOREACH(inp, &listhead, inp_list) {
246 if (istcp) {
247 KREAD(inp, &xt.xt_inp, sizeof(*inp));
248 inp = &xt.xt_inp;
249 } else {
250 KREAD(inp, &xi.xi_inp, sizeof(*inp));
251 inp = &xi.xi_inp;
252 }
253
254 if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
255 continue;
256
257 if (istcp) {
258 if (inp->inp_ppcb == NULL)
259 bzero(&xt.xt_tp, sizeof xt.xt_tp);
260 else if (inp->inp_vflag & INP_TIMEWAIT) {
261 bzero(&xt.xt_tp, sizeof xt.xt_tp);
262 xt.xt_tp.t_state = TCPS_TIME_WAIT;
263 } else
264 KREAD(inp->inp_ppcb, &xt.xt_tp,
265 sizeof xt.xt_tp);
266 }
267 if (inp->inp_socket) {
268 KREAD(inp->inp_socket, &so, sizeof(so));
269 if (sotoxsocket(&so, xso) != 0)
270 goto fail;
271 } else {
272 bzero(xso, sizeof(*xso));
273 if (istcp)
274 xso->xso_protocol = IPPROTO_TCP;
275 }
276 if (istcp)
277 COPYOUT(&xt, sizeof xt);
278 else
279 COPYOUT(&xi, sizeof xi);
280 }
281
282 /* Reread the pcbinfo and write out the footer. */
283 kread(off, &pcbinfo, sizeof(pcbinfo));
284 xig.xig_count = pcbinfo.ipi_count;
285 xig.xig_gen = pcbinfo.ipi_gencnt;
286 COPYOUT(&xig, sizeof xig);
287
288 *bufp = buf;
289 return (1);
290
291fail:
292 free(buf);
293 return (0);
294#undef COPYOUT
295#undef KREAD
296}
297
298/*
299 * Print a summary of connections related to an Internet
300 * protocol. For TCP, also give state of connection.
301 * Listening processes (aflag) are suppressed unless the
302 * -a (all) flag is specified.
303 */
304void
305protopr(u_long off, const char *name, int af1, int proto)
306{
307 int istcp;
308 static int first = 1;
309 char *buf;
310 const char *vchar;
311 struct tcpcb *tp = NULL;
312 struct inpcb *inp;
313 struct xinpgen *xig, *oxig;
314 struct xsocket *so;
315
316 istcp = 0;
317 switch (proto) {
318 case IPPROTO_TCP:
319#ifdef INET6
320 if (tcp_done != 0)
321 return;
322 else
323 tcp_done = 1;
324#endif
325 istcp = 1;
326 break;
327 case IPPROTO_UDP:
328#ifdef INET6
329 if (udp_done != 0)
330 return;
331 else
332 udp_done = 1;
333#endif
334 break;
335 }
336 if (live) {
337 if (!pcblist_sysctl(proto, &buf, istcp))
338 return;
339 } else {
340 if (!pcblist_kvm(off, &buf, istcp))
341 return;
342 }
343
344 oxig = xig = (struct xinpgen *)buf;
345 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
346 xig->xig_len > sizeof(struct xinpgen);
347 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
348 if (istcp) {
349 tp = &((struct xtcpcb *)xig)->xt_tp;
350 inp = &((struct xtcpcb *)xig)->xt_inp;
351 so = &((struct xtcpcb *)xig)->xt_socket;
352 } else {
353 inp = &((struct xinpcb *)xig)->xi_inp;
354 so = &((struct xinpcb *)xig)->xi_socket;
355 }
356
357 /* Ignore sockets for protocols other than the desired one. */
358 if (so->xso_protocol != proto)
359 continue;
360
361 /* Ignore PCBs which were freed during copyout. */
362 if (inp->inp_gencnt > oxig->xig_gen)
363 continue;
364
365 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
366#ifdef INET6
367 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
368#endif /* INET6 */
369 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
370#ifdef INET6
371 && (inp->inp_vflag & INP_IPV6) == 0
372#endif /* INET6 */
373 ))
374 )
375 continue;
376 if (!aflag &&
377 (
378 (istcp && tp->t_state == TCPS_LISTEN)
379 || (af1 == AF_INET &&
380 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
381#ifdef INET6
382 || (af1 == AF_INET6 &&
383 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
384#endif /* INET6 */
385 || (af1 == AF_UNSPEC &&
386 (((inp->inp_vflag & INP_IPV4) != 0 &&
387 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
388#ifdef INET6
389 || ((inp->inp_vflag & INP_IPV6) != 0 &&
390 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
391#endif
392 ))
393 ))
394 continue;
395
396 if (first) {
397 if (!Lflag) {
398 printf("Active Internet connections");
399 if (aflag)
400 printf(" (including servers)");
401 } else
402 printf(
403 "Current listen queue sizes (qlen/incqlen/maxqlen)");
404 putchar('\n');
405 if (Aflag)
406 printf("%-8.8s ", "Tcpcb");
407 if (Lflag)
408 printf("%-5.5s %-14.14s %-22.22s\n",
409 "Proto", "Listen", "Local Address");
410 printf((Aflag && !Wflag) ?
411 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" :
412 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s",
413 "Proto", "Recv-Q", "Send-Q",
414 "Local Address", "Foreign Address");
415 if (xflag)
416 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 %s\n",
417 "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
418 "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
419 "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX",
420 "(state)");
408 else
421 else
409 printf((Aflag && !Wflag) ?
410 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
411 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
412 "Proto", "Recv-Q", "Send-Q",
413 "Local Address", "Foreign Address",
414 "(state)");
422 printf("(state)\n");
415 first = 0;
416 }
417 if (Lflag && so->so_qlimit == 0)
418 continue;
419 if (Aflag) {
420 if (istcp)
421 printf("%8lx ", (u_long)inp->inp_ppcb);
422 else
423 printf("%8lx ", (u_long)so->so_pcb);
424 }
425#ifdef INET6
426 if ((inp->inp_vflag & INP_IPV6) != 0)
427 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
428 "46" : "6 ";
429 else
430#endif
431 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
432 "4 " : " ";
433 printf("%-3.3s%-2.2s ", name, vchar);
434 if (Lflag) {
435 char buf1[15];
436
437 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
438 so->so_incqlen, so->so_qlimit);
439 printf("%-14.14s ", buf1);
440 } else {
423 first = 0;
424 }
425 if (Lflag && so->so_qlimit == 0)
426 continue;
427 if (Aflag) {
428 if (istcp)
429 printf("%8lx ", (u_long)inp->inp_ppcb);
430 else
431 printf("%8lx ", (u_long)so->so_pcb);
432 }
433#ifdef INET6
434 if ((inp->inp_vflag & INP_IPV6) != 0)
435 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
436 "46" : "6 ";
437 else
438#endif
439 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
440 "4 " : " ";
441 printf("%-3.3s%-2.2s ", name, vchar);
442 if (Lflag) {
443 char buf1[15];
444
445 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
446 so->so_incqlen, so->so_qlimit);
447 printf("%-14.14s ", buf1);
448 } else {
441 printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
449 printf("%6u %6u ",
450 so->so_rcv.sb_cc, so->so_snd.sb_cc);
442 }
443 if (numeric_port) {
444 if (inp->inp_vflag & INP_IPV4) {
445 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
446 name, 1);
447 if (!Lflag)
448 inetprint(&inp->inp_faddr,
449 (int)inp->inp_fport, name, 1);
450 }
451#ifdef INET6
452 else if (inp->inp_vflag & INP_IPV6) {
453 inet6print(&inp->in6p_laddr,
454 (int)inp->inp_lport, name, 1);
455 if (!Lflag)
456 inet6print(&inp->in6p_faddr,
457 (int)inp->inp_fport, name, 1);
458 } /* else nothing printed now */
459#endif /* INET6 */
460 } else if (inp->inp_flags & INP_ANONPORT) {
461 if (inp->inp_vflag & INP_IPV4) {
462 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
463 name, 1);
464 if (!Lflag)
465 inetprint(&inp->inp_faddr,
466 (int)inp->inp_fport, name, 0);
467 }
468#ifdef INET6
469 else if (inp->inp_vflag & INP_IPV6) {
470 inet6print(&inp->in6p_laddr,
471 (int)inp->inp_lport, name, 1);
472 if (!Lflag)
473 inet6print(&inp->in6p_faddr,
474 (int)inp->inp_fport, name, 0);
475 } /* else nothing printed now */
476#endif /* INET6 */
477 } else {
478 if (inp->inp_vflag & INP_IPV4) {
479 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
480 name, 0);
481 if (!Lflag)
482 inetprint(&inp->inp_faddr,
483 (int)inp->inp_fport, name,
484 inp->inp_lport != inp->inp_fport);
485 }
486#ifdef INET6
487 else if (inp->inp_vflag & INP_IPV6) {
488 inet6print(&inp->in6p_laddr,
489 (int)inp->inp_lport, name, 0);
490 if (!Lflag)
491 inet6print(&inp->in6p_faddr,
492 (int)inp->inp_fport, name,
493 inp->inp_lport != inp->inp_fport);
494 } /* else nothing printed now */
495#endif /* INET6 */
496 }
451 }
452 if (numeric_port) {
453 if (inp->inp_vflag & INP_IPV4) {
454 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
455 name, 1);
456 if (!Lflag)
457 inetprint(&inp->inp_faddr,
458 (int)inp->inp_fport, name, 1);
459 }
460#ifdef INET6
461 else if (inp->inp_vflag & INP_IPV6) {
462 inet6print(&inp->in6p_laddr,
463 (int)inp->inp_lport, name, 1);
464 if (!Lflag)
465 inet6print(&inp->in6p_faddr,
466 (int)inp->inp_fport, name, 1);
467 } /* else nothing printed now */
468#endif /* INET6 */
469 } else if (inp->inp_flags & INP_ANONPORT) {
470 if (inp->inp_vflag & INP_IPV4) {
471 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
472 name, 1);
473 if (!Lflag)
474 inetprint(&inp->inp_faddr,
475 (int)inp->inp_fport, name, 0);
476 }
477#ifdef INET6
478 else if (inp->inp_vflag & INP_IPV6) {
479 inet6print(&inp->in6p_laddr,
480 (int)inp->inp_lport, name, 1);
481 if (!Lflag)
482 inet6print(&inp->in6p_faddr,
483 (int)inp->inp_fport, name, 0);
484 } /* else nothing printed now */
485#endif /* INET6 */
486 } else {
487 if (inp->inp_vflag & INP_IPV4) {
488 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
489 name, 0);
490 if (!Lflag)
491 inetprint(&inp->inp_faddr,
492 (int)inp->inp_fport, name,
493 inp->inp_lport != inp->inp_fport);
494 }
495#ifdef INET6
496 else if (inp->inp_vflag & INP_IPV6) {
497 inet6print(&inp->in6p_laddr,
498 (int)inp->inp_lport, name, 0);
499 if (!Lflag)
500 inet6print(&inp->in6p_faddr,
501 (int)inp->inp_fport, name,
502 inp->inp_lport != inp->inp_fport);
503 } /* else nothing printed now */
504#endif /* INET6 */
505 }
506 if (xflag) {
507 if (Lflag)
508 printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
509 " ",
510 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
511 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
512 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
513 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
514 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
515 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
516 else
517 printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
518 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
519 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
520 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
521 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
522 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
523 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
524 }
497 if (istcp && !Lflag) {
498 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
499 printf("%d", tp->t_state);
525 if (istcp && !Lflag) {
526 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
527 printf("%d", tp->t_state);
500 else {
528 else {
501 printf("%s", tcpstates[tp->t_state]);
502#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
503 /* Show T/TCP `hidden state' */
504 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
505 putchar('*');
506#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
507 }
529 printf("%s", tcpstates[tp->t_state]);
530#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
531 /* Show T/TCP `hidden state' */
532 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
533 putchar('*');
534#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
535 }
508 }
536 }
509 putchar('\n');
510 }
511 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
512 if (oxig->xig_count > xig->xig_count) {
513 printf("Some %s sockets may have been deleted.\n",
514 name);
515 } else if (oxig->xig_count < xig->xig_count) {
516 printf("Some %s sockets may have been created.\n",
517 name);
518 } else {
519 printf(
520 "Some %s sockets may have been created or deleted.\n",
521 name);
522 }
523 }
524 free(buf);
525}
526
527/*
528 * Dump TCP statistics structure.
529 */
530void
531tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
532{
533 struct tcpstat tcpstat, zerostat;
534 size_t len = sizeof tcpstat;
535
536#ifdef INET6
537 if (tcp_done != 0)
538 return;
539 else
540 tcp_done = 1;
541#endif
542
543 if (live) {
544 if (zflag)
545 memset(&zerostat, 0, len);
546 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
547 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
548 warn("sysctl: net.inet.tcp.stats");
549 return;
550 }
551 } else
552 kread(off, &tcpstat, len);
553
554 printf ("%s:\n", name);
555
556#define p(f, m) if (tcpstat.f || sflag <= 1) \
557 printf(m, tcpstat.f, plural(tcpstat.f))
558#define p1a(f, m) if (tcpstat.f || sflag <= 1) \
559 printf(m, tcpstat.f)
560#define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
561 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
562#define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
563 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
564#define p3(f, m) if (tcpstat.f || sflag <= 1) \
565 printf(m, tcpstat.f, pluralies(tcpstat.f))
566
567 p(tcps_sndtotal, "\t%lu packet%s sent\n");
568 p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
569 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
570 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
571 p(tcps_sndrexmitbad,
572 "\t\t%lu data packet%s unnecessarily retransmitted\n");
573 p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
574 p2a(tcps_sndacks, tcps_delack,
575 "\t\t%lu ack-only packet%s (%lu delayed)\n");
576 p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
577 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
578 p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
579 p(tcps_sndctrl, "\t\t%lu control packet%s\n");
580 p(tcps_rcvtotal, "\t%lu packet%s received\n");
581 p2(tcps_rcvackpack, tcps_rcvackbyte,
582 "\t\t%lu ack%s (for %lu byte%s)\n");
583 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
584 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
585 p2(tcps_rcvpack, tcps_rcvbyte,
586 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
587 p2(tcps_rcvduppack, tcps_rcvdupbyte,
588 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
589 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
590 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
591 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
592 p2(tcps_rcvoopack, tcps_rcvoobyte,
593 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
594 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
595 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
596 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
597 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
598 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
599 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
600 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
601 p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
602 p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
603 p(tcps_connattempt, "\t%lu connection request%s\n");
604 p(tcps_accepts, "\t%lu connection accept%s\n");
605 p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
606 p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
607 p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
608 p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
609 p2(tcps_closed, tcps_drops,
610 "\t%lu connection%s closed (including %lu drop%s)\n");
611 p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
612 p(tcps_cachedrttvar,
613 "\t\t%lu connection%s updated cached RTT variance on close\n");
614 p(tcps_cachedssthresh,
615 "\t\t%lu connection%s updated cached ssthresh on close\n");
616 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
617 p2(tcps_rttupdated, tcps_segstimed,
618 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
619 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
620 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
621 p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
622 p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
623 p(tcps_finwait2_drops,
624 "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
625 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
626 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
627 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
628 p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
629 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
630
631 p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
632 p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
633 p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
634 p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
635 p1a(tcps_sc_completed, "\t\t%lu completed\n");
636 p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
637 p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
638 p1a(tcps_sc_reset, "\t\t%lu reset\n");
639 p1a(tcps_sc_stale, "\t\t%lu stale\n");
640 p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
641 p1a(tcps_sc_badack, "\t\t%lu badack\n");
642 p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
643 p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
644 p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
645 p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
646
647 p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
648 p(tcps_sack_rexmits,
649 "\t%lu segment rexmit%s in SACK recovery episodes\n");
650 p(tcps_sack_rexmit_bytes,
651 "\t%lu byte rexmit%s in SACK recovery episodes\n");
652 p(tcps_sack_rcv_blocks,
653 "\t%lu SACK option%s (SACK blocks) received\n");
654 p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
655 p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
656
657#undef p
658#undef p1a
659#undef p2
660#undef p2a
661#undef p3
662}
663
664/*
665 * Dump UDP statistics structure.
666 */
667void
668udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
669{
670 struct udpstat udpstat, zerostat;
671 size_t len = sizeof udpstat;
672 u_long delivered;
673
674#ifdef INET6
675 if (udp_done != 0)
676 return;
677 else
678 udp_done = 1;
679#endif
680
681 if (live) {
682 if (zflag)
683 memset(&zerostat, 0, len);
684 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
685 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
686 warn("sysctl: net.inet.udp.stats");
687 return;
688 }
689 } else
690 kread(off, &udpstat, len);
691
692 printf("%s:\n", name);
693#define p(f, m) if (udpstat.f || sflag <= 1) \
694 printf(m, udpstat.f, plural(udpstat.f))
695#define p1a(f, m) if (udpstat.f || sflag <= 1) \
696 printf(m, udpstat.f)
697 p(udps_ipackets, "\t%lu datagram%s received\n");
698 p1a(udps_hdrops, "\t%lu with incomplete header\n");
699 p1a(udps_badlen, "\t%lu with bad data length field\n");
700 p1a(udps_badsum, "\t%lu with bad checksum\n");
701 p1a(udps_nosum, "\t%lu with no checksum\n");
702 p1a(udps_noport, "\t%lu dropped due to no socket\n");
703 p(udps_noportbcast,
704 "\t%lu broadcast/multicast datagram%s undelivered\n");
705 p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
706 p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
707 delivered = udpstat.udps_ipackets -
708 udpstat.udps_hdrops -
709 udpstat.udps_badlen -
710 udpstat.udps_badsum -
711 udpstat.udps_noport -
712 udpstat.udps_noportbcast -
713 udpstat.udps_fullsock;
714 if (delivered || sflag <= 1)
715 printf("\t%lu delivered\n", delivered);
716 p(udps_opackets, "\t%lu datagram%s output\n");
717 /* the next statistic is cumulative in udps_noportbcast */
718 p(udps_filtermcast,
719 "\t%lu time%s multicast source filter matched\n");
720#undef p
721#undef p1a
722}
723
724/*
725 * Dump CARP statistics structure.
726 */
727void
728carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
729{
730 struct carpstats carpstat, zerostat;
731 size_t len = sizeof(struct carpstats);
732
733 if (live) {
734 if (zflag)
735 memset(&zerostat, 0, len);
736 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
737 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
738 if (errno != ENOENT)
739 warn("sysctl: net.inet.carp.stats");
740 return;
741 }
742 } else {
743 if (off == 0)
744 return;
745 kread(off, &carpstat, len);
746 }
747
748 printf("%s:\n", name);
749
750#define p(f, m) if (carpstat.f || sflag <= 1) \
751 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
752#define p2(f, m) if (carpstat.f || sflag <= 1) \
753 printf(m, (uintmax_t)carpstat.f)
754
755 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
756 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
757 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
758 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
759 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
760 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
761 p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
762 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
763 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
764 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
765 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
766 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
767 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
768#if notyet
769 p(carps_ostates, "\t\t%s state update%s sent\n");
770#endif
771#undef p
772#undef p2
773}
774
775/*
776 * Dump IP statistics structure.
777 */
778void
779ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
780{
781 struct ipstat ipstat, zerostat;
782 size_t len = sizeof ipstat;
783
784 if (live) {
785 if (zflag)
786 memset(&zerostat, 0, len);
787 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
788 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
789 warn("sysctl: net.inet.ip.stats");
790 return;
791 }
792 } else
793 kread(off, &ipstat, len);
794
795 printf("%s:\n", name);
796
797#define p(f, m) if (ipstat.f || sflag <= 1) \
798 printf(m, ipstat.f, plural(ipstat.f))
799#define p1a(f, m) if (ipstat.f || sflag <= 1) \
800 printf(m, ipstat.f)
801
802 p(ips_total, "\t%lu total packet%s received\n");
803 p(ips_badsum, "\t%lu bad header checksum%s\n");
804 p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
805 p1a(ips_tooshort, "\t%lu with data size < data length\n");
806 p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
807 p1a(ips_badhlen, "\t%lu with header length < data size\n");
808 p1a(ips_badlen, "\t%lu with data length < header length\n");
809 p1a(ips_badoptions, "\t%lu with bad options\n");
810 p1a(ips_badvers, "\t%lu with incorrect version number\n");
811 p(ips_fragments, "\t%lu fragment%s received\n");
812 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
813 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
814 p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
815 p(ips_delivered, "\t%lu packet%s for this host\n");
816 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
817 p(ips_forward, "\t%lu packet%s forwarded");
818 p(ips_fastforward, " (%lu packet%s fast forwarded)");
819 if (ipstat.ips_forward || sflag <= 1)
820 putchar('\n');
821 p(ips_cantforward, "\t%lu packet%s not forwardable\n");
822 p(ips_notmember,
823 "\t%lu packet%s received for unknown multicast group\n");
824 p(ips_redirectsent, "\t%lu redirect%s sent\n");
825 p(ips_localout, "\t%lu packet%s sent from this host\n");
826 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
827 p(ips_odropped,
828 "\t%lu output packet%s dropped due to no bufs, etc.\n");
829 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
830 p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
831 p(ips_ofragments, "\t%lu fragment%s created\n");
832 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
833 p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
834 p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
835#undef p
836#undef p1a
837}
838
839static const char *icmpnames[ICMP_MAXTYPE + 1] = {
840 "echo reply", /* RFC 792 */
841 "#1",
842 "#2",
843 "destination unreachable", /* RFC 792 */
844 "source quench", /* RFC 792 */
845 "routing redirect", /* RFC 792 */
846 "#6",
847 "#7",
848 "echo", /* RFC 792 */
849 "router advertisement", /* RFC 1256 */
850 "router solicitation", /* RFC 1256 */
851 "time exceeded", /* RFC 792 */
852 "parameter problem", /* RFC 792 */
853 "time stamp", /* RFC 792 */
854 "time stamp reply", /* RFC 792 */
855 "information request", /* RFC 792 */
856 "information request reply", /* RFC 792 */
857 "address mask request", /* RFC 950 */
858 "address mask reply", /* RFC 950 */
859 "#19",
860 "#20",
861 "#21",
862 "#22",
863 "#23",
864 "#24",
865 "#25",
866 "#26",
867 "#27",
868 "#28",
869 "#29",
870 "icmp traceroute", /* RFC 1393 */
871 "datagram conversion error", /* RFC 1475 */
872 "mobile host redirect",
873 "IPv6 where-are-you",
874 "IPv6 i-am-here",
875 "mobile registration req",
876 "mobile registration reply",
877 "domain name request", /* RFC 1788 */
878 "domain name reply", /* RFC 1788 */
879 "icmp SKIP",
880 "icmp photuris", /* RFC 2521 */
881};
882
883/*
884 * Dump ICMP statistics.
885 */
886void
887icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
888{
889 struct icmpstat icmpstat, zerostat;
890 int i, first;
891 size_t len;
892
893 len = sizeof icmpstat;
894 if (live) {
895 if (zflag)
896 memset(&zerostat, 0, len);
897 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
898 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
899 warn("sysctl: net.inet.icmp.stats");
900 return;
901 }
902 } else
903 kread(off, &icmpstat, len);
904
905 printf("%s:\n", name);
906
907#define p(f, m) if (icmpstat.f || sflag <= 1) \
908 printf(m, icmpstat.f, plural(icmpstat.f))
909#define p1a(f, m) if (icmpstat.f || sflag <= 1) \
910 printf(m, icmpstat.f)
911#define p2(f, m) if (icmpstat.f || sflag <= 1) \
912 printf(m, icmpstat.f, plurales(icmpstat.f))
913
914 p(icps_error, "\t%lu call%s to icmp_error\n");
915 p(icps_oldicmp,
916 "\t%lu error%s not generated in response to an icmp message\n");
917 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
918 if (icmpstat.icps_outhist[i] != 0) {
919 if (first) {
920 printf("\tOutput histogram:\n");
921 first = 0;
922 }
923 if (icmpnames[i] != NULL)
924 printf("\t\t%s: %lu\n", icmpnames[i],
925 icmpstat.icps_outhist[i]);
926 else
927 printf("\t\tunknown ICMP #%d: %lu\n", i,
928 icmpstat.icps_outhist[i]);
929 }
930 p(icps_badcode, "\t%lu message%s with bad code fields\n");
931 p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
932 p(icps_checksum, "\t%lu message%s with bad checksum\n");
933 p(icps_badlen, "\t%lu message%s with bad length\n");
934 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
935 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
936 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
937 if (icmpstat.icps_inhist[i] != 0) {
938 if (first) {
939 printf("\tInput histogram:\n");
940 first = 0;
941 }
942 if (icmpnames[i] != NULL)
943 printf("\t\t%s: %lu\n", icmpnames[i],
944 icmpstat.icps_inhist[i]);
945 else
946 printf("\t\tunknown ICMP #%d: %lu\n", i,
947 icmpstat.icps_inhist[i]);
948 }
949 p(icps_reflect, "\t%lu message response%s generated\n");
950 p2(icps_badaddr, "\t%lu invalid return address%s\n");
951 p(icps_noroute, "\t%lu no return route%s\n");
952#undef p
953#undef p1a
954#undef p2
955 if (live) {
956 len = sizeof i;
957 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
958 0)
959 return;
960 printf("\tICMP address mask responses are %sabled\n",
961 i ? "en" : "dis");
962 }
963}
964
965/*
966 * Dump IGMP statistics structure.
967 */
968void
969igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
970{
971 struct igmpstat igmpstat, zerostat;
972 size_t len = sizeof igmpstat;
973
974 if (live) {
975 if (zflag)
976 memset(&zerostat, 0, len);
977 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
978 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
979 warn("sysctl: net.inet.igmp.stats");
980 return;
981 }
982 } else
983 kread(off, &igmpstat, len);
984
985 printf("%s:\n", name);
986
987#define p(f, m) if (igmpstat.f || sflag <= 1) \
988 printf(m, igmpstat.f, plural(igmpstat.f))
989#define py(f, m) if (igmpstat.f || sflag <= 1) \
990 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
991 p(igps_rcv_total, "\t%u message%s received\n");
992 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
993 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
994 py(igps_rcv_queries, "\t%u membership quer%s received\n");
995 py(igps_rcv_badqueries,
996 "\t%u membership quer%s received with invalid field(s)\n");
997 p(igps_rcv_reports, "\t%u membership report%s received\n");
998 p(igps_rcv_badreports,
999 "\t%u membership report%s received with invalid field(s)\n");
1000 p(igps_rcv_ourreports,
1001"\t%u membership report%s received for groups to which we belong\n");
1002 p(igps_snd_reports, "\t%u membership report%s sent\n");
1003#undef p
1004#undef py
1005}
1006
1007/*
1008 * Dump PIM statistics structure.
1009 */
1010void
1011pim_stats(u_long off __unused, const char *name, int af1 __unused,
1012 int proto __unused)
1013{
1014 struct pimstat pimstat, zerostat;
1015 size_t len = sizeof pimstat;
1016
1017 if (live) {
1018 if (zflag)
1019 memset(&zerostat, 0, len);
1020 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1021 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1022 if (errno != ENOENT)
1023 warn("sysctl: net.inet.pim.stats");
1024 return;
1025 }
1026 } else {
1027 if (off == 0)
1028 return;
1029 kread(off, &pimstat, len);
1030 }
1031
1032 printf("%s:\n", name);
1033
1034#define p(f, m) if (pimstat.f || sflag <= 1) \
1035 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1036#define py(f, m) if (pimstat.f || sflag <= 1) \
1037 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1038 p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1039 p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1040 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1041 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1042 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1043 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1044 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1045 p(pims_rcv_registers_wrongiif,
1046 "\t%ju data register message%s received on wrong iif\n");
1047 p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1048 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1049 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1050#undef p
1051#undef py
1052}
1053
1054/*
1055 * Pretty print an Internet address (net address + port).
1056 */
1057void
1058inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1059{
1060 struct servent *sp = 0;
1061 char line[80], *cp;
1062 int width;
1063
1064 if (Wflag)
1065 sprintf(line, "%s.", inetname(in));
1066 else
1067 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1068 cp = index(line, '\0');
1069 if (!num_port && port)
1070 sp = getservbyport((int)port, proto);
1071 if (sp || port == 0)
1072 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1073 else
1074 sprintf(cp, "%d ", ntohs((u_short)port));
1075 width = (Aflag && !Wflag) ? 18 : 22;
1076 if (Wflag)
1077 printf("%-*s ", width, line);
1078 else
1079 printf("%-*.*s ", width, width, line);
1080}
1081
1082/*
1083 * Construct an Internet address representation.
1084 * If numeric_addr has been supplied, give
1085 * numeric value, otherwise try for symbolic name.
1086 */
1087char *
1088inetname(struct in_addr *inp)
1089{
1090 char *cp;
1091 static char line[MAXHOSTNAMELEN];
1092 struct hostent *hp;
1093 struct netent *np;
1094
1095 cp = 0;
1096 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1097 int net = inet_netof(*inp);
1098 int lna = inet_lnaof(*inp);
1099
1100 if (lna == INADDR_ANY) {
1101 np = getnetbyaddr(net, AF_INET);
1102 if (np)
1103 cp = np->n_name;
1104 }
1105 if (cp == 0) {
1106 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1107 if (hp) {
1108 cp = hp->h_name;
1109 trimdomain(cp, strlen(cp));
1110 }
1111 }
1112 }
1113 if (inp->s_addr == INADDR_ANY)
1114 strcpy(line, "*");
1115 else if (cp) {
1116 strncpy(line, cp, sizeof(line) - 1);
1117 line[sizeof(line) - 1] = '\0';
1118 } else {
1119 inp->s_addr = ntohl(inp->s_addr);
1120#define C(x) ((u_int)((x) & 0xff))
1121 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1122 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
1123 }
1124 return (line);
1125}
537 putchar('\n');
538 }
539 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
540 if (oxig->xig_count > xig->xig_count) {
541 printf("Some %s sockets may have been deleted.\n",
542 name);
543 } else if (oxig->xig_count < xig->xig_count) {
544 printf("Some %s sockets may have been created.\n",
545 name);
546 } else {
547 printf(
548 "Some %s sockets may have been created or deleted.\n",
549 name);
550 }
551 }
552 free(buf);
553}
554
555/*
556 * Dump TCP statistics structure.
557 */
558void
559tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
560{
561 struct tcpstat tcpstat, zerostat;
562 size_t len = sizeof tcpstat;
563
564#ifdef INET6
565 if (tcp_done != 0)
566 return;
567 else
568 tcp_done = 1;
569#endif
570
571 if (live) {
572 if (zflag)
573 memset(&zerostat, 0, len);
574 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
575 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
576 warn("sysctl: net.inet.tcp.stats");
577 return;
578 }
579 } else
580 kread(off, &tcpstat, len);
581
582 printf ("%s:\n", name);
583
584#define p(f, m) if (tcpstat.f || sflag <= 1) \
585 printf(m, tcpstat.f, plural(tcpstat.f))
586#define p1a(f, m) if (tcpstat.f || sflag <= 1) \
587 printf(m, tcpstat.f)
588#define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
589 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
590#define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
591 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
592#define p3(f, m) if (tcpstat.f || sflag <= 1) \
593 printf(m, tcpstat.f, pluralies(tcpstat.f))
594
595 p(tcps_sndtotal, "\t%lu packet%s sent\n");
596 p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
597 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
598 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
599 p(tcps_sndrexmitbad,
600 "\t\t%lu data packet%s unnecessarily retransmitted\n");
601 p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
602 p2a(tcps_sndacks, tcps_delack,
603 "\t\t%lu ack-only packet%s (%lu delayed)\n");
604 p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
605 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
606 p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
607 p(tcps_sndctrl, "\t\t%lu control packet%s\n");
608 p(tcps_rcvtotal, "\t%lu packet%s received\n");
609 p2(tcps_rcvackpack, tcps_rcvackbyte,
610 "\t\t%lu ack%s (for %lu byte%s)\n");
611 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
612 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
613 p2(tcps_rcvpack, tcps_rcvbyte,
614 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
615 p2(tcps_rcvduppack, tcps_rcvdupbyte,
616 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
617 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
618 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
619 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
620 p2(tcps_rcvoopack, tcps_rcvoobyte,
621 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
622 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
623 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
624 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
625 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
626 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
627 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
628 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
629 p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
630 p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
631 p(tcps_connattempt, "\t%lu connection request%s\n");
632 p(tcps_accepts, "\t%lu connection accept%s\n");
633 p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
634 p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
635 p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
636 p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
637 p2(tcps_closed, tcps_drops,
638 "\t%lu connection%s closed (including %lu drop%s)\n");
639 p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
640 p(tcps_cachedrttvar,
641 "\t\t%lu connection%s updated cached RTT variance on close\n");
642 p(tcps_cachedssthresh,
643 "\t\t%lu connection%s updated cached ssthresh on close\n");
644 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
645 p2(tcps_rttupdated, tcps_segstimed,
646 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
647 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
648 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
649 p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
650 p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
651 p(tcps_finwait2_drops,
652 "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
653 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
654 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
655 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
656 p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
657 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
658
659 p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
660 p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
661 p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
662 p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
663 p1a(tcps_sc_completed, "\t\t%lu completed\n");
664 p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
665 p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
666 p1a(tcps_sc_reset, "\t\t%lu reset\n");
667 p1a(tcps_sc_stale, "\t\t%lu stale\n");
668 p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
669 p1a(tcps_sc_badack, "\t\t%lu badack\n");
670 p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
671 p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
672 p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
673 p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
674
675 p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
676 p(tcps_sack_rexmits,
677 "\t%lu segment rexmit%s in SACK recovery episodes\n");
678 p(tcps_sack_rexmit_bytes,
679 "\t%lu byte rexmit%s in SACK recovery episodes\n");
680 p(tcps_sack_rcv_blocks,
681 "\t%lu SACK option%s (SACK blocks) received\n");
682 p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
683 p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
684
685#undef p
686#undef p1a
687#undef p2
688#undef p2a
689#undef p3
690}
691
692/*
693 * Dump UDP statistics structure.
694 */
695void
696udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
697{
698 struct udpstat udpstat, zerostat;
699 size_t len = sizeof udpstat;
700 u_long delivered;
701
702#ifdef INET6
703 if (udp_done != 0)
704 return;
705 else
706 udp_done = 1;
707#endif
708
709 if (live) {
710 if (zflag)
711 memset(&zerostat, 0, len);
712 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
713 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
714 warn("sysctl: net.inet.udp.stats");
715 return;
716 }
717 } else
718 kread(off, &udpstat, len);
719
720 printf("%s:\n", name);
721#define p(f, m) if (udpstat.f || sflag <= 1) \
722 printf(m, udpstat.f, plural(udpstat.f))
723#define p1a(f, m) if (udpstat.f || sflag <= 1) \
724 printf(m, udpstat.f)
725 p(udps_ipackets, "\t%lu datagram%s received\n");
726 p1a(udps_hdrops, "\t%lu with incomplete header\n");
727 p1a(udps_badlen, "\t%lu with bad data length field\n");
728 p1a(udps_badsum, "\t%lu with bad checksum\n");
729 p1a(udps_nosum, "\t%lu with no checksum\n");
730 p1a(udps_noport, "\t%lu dropped due to no socket\n");
731 p(udps_noportbcast,
732 "\t%lu broadcast/multicast datagram%s undelivered\n");
733 p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
734 p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
735 delivered = udpstat.udps_ipackets -
736 udpstat.udps_hdrops -
737 udpstat.udps_badlen -
738 udpstat.udps_badsum -
739 udpstat.udps_noport -
740 udpstat.udps_noportbcast -
741 udpstat.udps_fullsock;
742 if (delivered || sflag <= 1)
743 printf("\t%lu delivered\n", delivered);
744 p(udps_opackets, "\t%lu datagram%s output\n");
745 /* the next statistic is cumulative in udps_noportbcast */
746 p(udps_filtermcast,
747 "\t%lu time%s multicast source filter matched\n");
748#undef p
749#undef p1a
750}
751
752/*
753 * Dump CARP statistics structure.
754 */
755void
756carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
757{
758 struct carpstats carpstat, zerostat;
759 size_t len = sizeof(struct carpstats);
760
761 if (live) {
762 if (zflag)
763 memset(&zerostat, 0, len);
764 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
765 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
766 if (errno != ENOENT)
767 warn("sysctl: net.inet.carp.stats");
768 return;
769 }
770 } else {
771 if (off == 0)
772 return;
773 kread(off, &carpstat, len);
774 }
775
776 printf("%s:\n", name);
777
778#define p(f, m) if (carpstat.f || sflag <= 1) \
779 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
780#define p2(f, m) if (carpstat.f || sflag <= 1) \
781 printf(m, (uintmax_t)carpstat.f)
782
783 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
784 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
785 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
786 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
787 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
788 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
789 p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
790 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
791 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
792 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
793 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
794 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
795 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
796#if notyet
797 p(carps_ostates, "\t\t%s state update%s sent\n");
798#endif
799#undef p
800#undef p2
801}
802
803/*
804 * Dump IP statistics structure.
805 */
806void
807ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
808{
809 struct ipstat ipstat, zerostat;
810 size_t len = sizeof ipstat;
811
812 if (live) {
813 if (zflag)
814 memset(&zerostat, 0, len);
815 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
816 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
817 warn("sysctl: net.inet.ip.stats");
818 return;
819 }
820 } else
821 kread(off, &ipstat, len);
822
823 printf("%s:\n", name);
824
825#define p(f, m) if (ipstat.f || sflag <= 1) \
826 printf(m, ipstat.f, plural(ipstat.f))
827#define p1a(f, m) if (ipstat.f || sflag <= 1) \
828 printf(m, ipstat.f)
829
830 p(ips_total, "\t%lu total packet%s received\n");
831 p(ips_badsum, "\t%lu bad header checksum%s\n");
832 p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
833 p1a(ips_tooshort, "\t%lu with data size < data length\n");
834 p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
835 p1a(ips_badhlen, "\t%lu with header length < data size\n");
836 p1a(ips_badlen, "\t%lu with data length < header length\n");
837 p1a(ips_badoptions, "\t%lu with bad options\n");
838 p1a(ips_badvers, "\t%lu with incorrect version number\n");
839 p(ips_fragments, "\t%lu fragment%s received\n");
840 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
841 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
842 p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
843 p(ips_delivered, "\t%lu packet%s for this host\n");
844 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
845 p(ips_forward, "\t%lu packet%s forwarded");
846 p(ips_fastforward, " (%lu packet%s fast forwarded)");
847 if (ipstat.ips_forward || sflag <= 1)
848 putchar('\n');
849 p(ips_cantforward, "\t%lu packet%s not forwardable\n");
850 p(ips_notmember,
851 "\t%lu packet%s received for unknown multicast group\n");
852 p(ips_redirectsent, "\t%lu redirect%s sent\n");
853 p(ips_localout, "\t%lu packet%s sent from this host\n");
854 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
855 p(ips_odropped,
856 "\t%lu output packet%s dropped due to no bufs, etc.\n");
857 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
858 p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
859 p(ips_ofragments, "\t%lu fragment%s created\n");
860 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
861 p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
862 p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
863#undef p
864#undef p1a
865}
866
867static const char *icmpnames[ICMP_MAXTYPE + 1] = {
868 "echo reply", /* RFC 792 */
869 "#1",
870 "#2",
871 "destination unreachable", /* RFC 792 */
872 "source quench", /* RFC 792 */
873 "routing redirect", /* RFC 792 */
874 "#6",
875 "#7",
876 "echo", /* RFC 792 */
877 "router advertisement", /* RFC 1256 */
878 "router solicitation", /* RFC 1256 */
879 "time exceeded", /* RFC 792 */
880 "parameter problem", /* RFC 792 */
881 "time stamp", /* RFC 792 */
882 "time stamp reply", /* RFC 792 */
883 "information request", /* RFC 792 */
884 "information request reply", /* RFC 792 */
885 "address mask request", /* RFC 950 */
886 "address mask reply", /* RFC 950 */
887 "#19",
888 "#20",
889 "#21",
890 "#22",
891 "#23",
892 "#24",
893 "#25",
894 "#26",
895 "#27",
896 "#28",
897 "#29",
898 "icmp traceroute", /* RFC 1393 */
899 "datagram conversion error", /* RFC 1475 */
900 "mobile host redirect",
901 "IPv6 where-are-you",
902 "IPv6 i-am-here",
903 "mobile registration req",
904 "mobile registration reply",
905 "domain name request", /* RFC 1788 */
906 "domain name reply", /* RFC 1788 */
907 "icmp SKIP",
908 "icmp photuris", /* RFC 2521 */
909};
910
911/*
912 * Dump ICMP statistics.
913 */
914void
915icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
916{
917 struct icmpstat icmpstat, zerostat;
918 int i, first;
919 size_t len;
920
921 len = sizeof icmpstat;
922 if (live) {
923 if (zflag)
924 memset(&zerostat, 0, len);
925 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
926 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
927 warn("sysctl: net.inet.icmp.stats");
928 return;
929 }
930 } else
931 kread(off, &icmpstat, len);
932
933 printf("%s:\n", name);
934
935#define p(f, m) if (icmpstat.f || sflag <= 1) \
936 printf(m, icmpstat.f, plural(icmpstat.f))
937#define p1a(f, m) if (icmpstat.f || sflag <= 1) \
938 printf(m, icmpstat.f)
939#define p2(f, m) if (icmpstat.f || sflag <= 1) \
940 printf(m, icmpstat.f, plurales(icmpstat.f))
941
942 p(icps_error, "\t%lu call%s to icmp_error\n");
943 p(icps_oldicmp,
944 "\t%lu error%s not generated in response to an icmp message\n");
945 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
946 if (icmpstat.icps_outhist[i] != 0) {
947 if (first) {
948 printf("\tOutput histogram:\n");
949 first = 0;
950 }
951 if (icmpnames[i] != NULL)
952 printf("\t\t%s: %lu\n", icmpnames[i],
953 icmpstat.icps_outhist[i]);
954 else
955 printf("\t\tunknown ICMP #%d: %lu\n", i,
956 icmpstat.icps_outhist[i]);
957 }
958 p(icps_badcode, "\t%lu message%s with bad code fields\n");
959 p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
960 p(icps_checksum, "\t%lu message%s with bad checksum\n");
961 p(icps_badlen, "\t%lu message%s with bad length\n");
962 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
963 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
964 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
965 if (icmpstat.icps_inhist[i] != 0) {
966 if (first) {
967 printf("\tInput histogram:\n");
968 first = 0;
969 }
970 if (icmpnames[i] != NULL)
971 printf("\t\t%s: %lu\n", icmpnames[i],
972 icmpstat.icps_inhist[i]);
973 else
974 printf("\t\tunknown ICMP #%d: %lu\n", i,
975 icmpstat.icps_inhist[i]);
976 }
977 p(icps_reflect, "\t%lu message response%s generated\n");
978 p2(icps_badaddr, "\t%lu invalid return address%s\n");
979 p(icps_noroute, "\t%lu no return route%s\n");
980#undef p
981#undef p1a
982#undef p2
983 if (live) {
984 len = sizeof i;
985 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
986 0)
987 return;
988 printf("\tICMP address mask responses are %sabled\n",
989 i ? "en" : "dis");
990 }
991}
992
993/*
994 * Dump IGMP statistics structure.
995 */
996void
997igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
998{
999 struct igmpstat igmpstat, zerostat;
1000 size_t len = sizeof igmpstat;
1001
1002 if (live) {
1003 if (zflag)
1004 memset(&zerostat, 0, len);
1005 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1006 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1007 warn("sysctl: net.inet.igmp.stats");
1008 return;
1009 }
1010 } else
1011 kread(off, &igmpstat, len);
1012
1013 printf("%s:\n", name);
1014
1015#define p(f, m) if (igmpstat.f || sflag <= 1) \
1016 printf(m, igmpstat.f, plural(igmpstat.f))
1017#define py(f, m) if (igmpstat.f || sflag <= 1) \
1018 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
1019 p(igps_rcv_total, "\t%u message%s received\n");
1020 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
1021 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
1022 py(igps_rcv_queries, "\t%u membership quer%s received\n");
1023 py(igps_rcv_badqueries,
1024 "\t%u membership quer%s received with invalid field(s)\n");
1025 p(igps_rcv_reports, "\t%u membership report%s received\n");
1026 p(igps_rcv_badreports,
1027 "\t%u membership report%s received with invalid field(s)\n");
1028 p(igps_rcv_ourreports,
1029"\t%u membership report%s received for groups to which we belong\n");
1030 p(igps_snd_reports, "\t%u membership report%s sent\n");
1031#undef p
1032#undef py
1033}
1034
1035/*
1036 * Dump PIM statistics structure.
1037 */
1038void
1039pim_stats(u_long off __unused, const char *name, int af1 __unused,
1040 int proto __unused)
1041{
1042 struct pimstat pimstat, zerostat;
1043 size_t len = sizeof pimstat;
1044
1045 if (live) {
1046 if (zflag)
1047 memset(&zerostat, 0, len);
1048 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1049 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1050 if (errno != ENOENT)
1051 warn("sysctl: net.inet.pim.stats");
1052 return;
1053 }
1054 } else {
1055 if (off == 0)
1056 return;
1057 kread(off, &pimstat, len);
1058 }
1059
1060 printf("%s:\n", name);
1061
1062#define p(f, m) if (pimstat.f || sflag <= 1) \
1063 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1064#define py(f, m) if (pimstat.f || sflag <= 1) \
1065 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1066 p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1067 p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1068 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1069 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1070 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1071 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1072 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1073 p(pims_rcv_registers_wrongiif,
1074 "\t%ju data register message%s received on wrong iif\n");
1075 p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1076 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1077 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1078#undef p
1079#undef py
1080}
1081
1082/*
1083 * Pretty print an Internet address (net address + port).
1084 */
1085void
1086inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1087{
1088 struct servent *sp = 0;
1089 char line[80], *cp;
1090 int width;
1091
1092 if (Wflag)
1093 sprintf(line, "%s.", inetname(in));
1094 else
1095 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1096 cp = index(line, '\0');
1097 if (!num_port && port)
1098 sp = getservbyport((int)port, proto);
1099 if (sp || port == 0)
1100 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1101 else
1102 sprintf(cp, "%d ", ntohs((u_short)port));
1103 width = (Aflag && !Wflag) ? 18 : 22;
1104 if (Wflag)
1105 printf("%-*s ", width, line);
1106 else
1107 printf("%-*.*s ", width, width, line);
1108}
1109
1110/*
1111 * Construct an Internet address representation.
1112 * If numeric_addr has been supplied, give
1113 * numeric value, otherwise try for symbolic name.
1114 */
1115char *
1116inetname(struct in_addr *inp)
1117{
1118 char *cp;
1119 static char line[MAXHOSTNAMELEN];
1120 struct hostent *hp;
1121 struct netent *np;
1122
1123 cp = 0;
1124 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1125 int net = inet_netof(*inp);
1126 int lna = inet_lnaof(*inp);
1127
1128 if (lna == INADDR_ANY) {
1129 np = getnetbyaddr(net, AF_INET);
1130 if (np)
1131 cp = np->n_name;
1132 }
1133 if (cp == 0) {
1134 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1135 if (hp) {
1136 cp = hp->h_name;
1137 trimdomain(cp, strlen(cp));
1138 }
1139 }
1140 }
1141 if (inp->s_addr == INADDR_ANY)
1142 strcpy(line, "*");
1143 else if (cp) {
1144 strncpy(line, cp, sizeof(line) - 1);
1145 line[sizeof(line) - 1] = '\0';
1146 } else {
1147 inp->s_addr = ntohl(inp->s_addr);
1148#define C(x) ((u_int)((x) & 0xff))
1149 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1150 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
1151 }
1152 return (line);
1153}