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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
33 #endif /* not lint */
34 #endif
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46
47 #include <net/route.h>
48 #include <net/if_arp.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_carp.h>
53 #ifdef INET6
54 #include <netinet/ip6.h>
55 #endif /* INET6 */
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/igmp_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/pim_var.h>
62 #include <netinet/tcp.h>
63 #include <netinet/tcpip.h>
64 #include <netinet/tcp_seq.h>
65 #define TCPSTATES
66 #include <netinet/tcp_fsm.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71
72 #include <arpa/inet.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <libutil.h>
76 #include <netdb.h>
77 #include <stdint.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <stdbool.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <libxo/xo.h>
84 #include "netstat.h"
85
86 char *inetname(struct in_addr *);
87 void inetprint(const char *, struct in_addr *, int, const char *, int,
88 const int);
89 #ifdef INET6
90 static int udp_done, tcp_done, sdp_done;
91 #endif /* INET6 */
92
93 static int
pcblist_sysctl(int proto,const char * name,char ** bufp,int istcp __unused)94 pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused)
95 {
96 const char *mibvar;
97 char *buf;
98 size_t len;
99
100 switch (proto) {
101 case IPPROTO_TCP:
102 mibvar = "net.inet.tcp.pcblist";
103 break;
104 case IPPROTO_UDP:
105 mibvar = "net.inet.udp.pcblist";
106 break;
107 case IPPROTO_DIVERT:
108 mibvar = "net.inet.divert.pcblist";
109 break;
110 default:
111 mibvar = "net.inet.raw.pcblist";
112 break;
113 }
114 if (strncmp(name, "sdp", 3) == 0)
115 mibvar = "net.inet.sdp.pcblist";
116 len = 0;
117 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118 if (errno != ENOENT)
119 xo_warn("sysctl: %s", mibvar);
120 return (0);
121 }
122 if ((buf = malloc(len)) == 0) {
123 xo_warnx("malloc %lu bytes", (u_long)len);
124 return (0);
125 }
126 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 xo_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 */
139 static void
sbtoxsockbuf(struct sockbuf * sb,struct xsockbuf * xsb)140 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141 {
142 xsb->sb_cc = sb->sb_ccc;
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;
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
153 int
sotoxsocket(struct socket * so,struct xsocket * xso)154 sotoxsocket(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
184 static int
pcblist_kvm(u_long off,char ** bufp,int istcp)185 pcblist_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 xo_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 xo_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_flags & 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
291 fail:
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 */
304 void
protopr(u_long off,const char * name,int af1,int proto)305 protopr(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 char *algo;
312 struct tcpcb *tp = NULL;
313 struct inpcb *inp;
314 struct xinpgen *xig, *oxig;
315 struct xsocket *so;
316 struct xtcp_timer *timer;
317
318 istcp = 0;
319 switch (proto) {
320 case IPPROTO_TCP:
321 #ifdef INET6
322 if (strncmp(name, "sdp", 3) != 0) {
323 if (tcp_done != 0)
324 return;
325 else
326 tcp_done = 1;
327 } else {
328 if (sdp_done != 0)
329 return;
330 else
331 sdp_done = 1;
332 }
333 #endif
334 istcp = 1;
335 break;
336 case IPPROTO_UDP:
337 #ifdef INET6
338 if (udp_done != 0)
339 return;
340 else
341 udp_done = 1;
342 #endif
343 break;
344 }
345 if (live) {
346 if (!pcblist_sysctl(proto, name, &buf, istcp))
347 return;
348 } else {
349 if (!pcblist_kvm(off, &buf, istcp))
350 return;
351 }
352
353 oxig = xig = (struct xinpgen *)buf;
354 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
355 xig->xig_len > sizeof(struct xinpgen);
356 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
357 if (istcp) {
358 timer = &((struct xtcpcb *)xig)->xt_timer;
359 tp = &((struct xtcpcb *)xig)->xt_tp;
360 inp = &((struct xtcpcb *)xig)->xt_inp;
361 so = &((struct xtcpcb *)xig)->xt_socket;
362 algo = ((struct xtcpcb *)xig)->xt_cc_name;
363 } else {
364 inp = &((struct xinpcb *)xig)->xi_inp;
365 so = &((struct xinpcb *)xig)->xi_socket;
366 timer = NULL;
367 algo = NULL;
368 }
369
370 /* Ignore sockets for protocols other than the desired one. */
371 if (so->xso_protocol != proto)
372 continue;
373
374 /* Ignore PCBs which were freed during copyout. */
375 if (inp->inp_gencnt > oxig->xig_gen)
376 continue;
377
378 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
379 #ifdef INET6
380 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
381 #endif /* INET6 */
382 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
383 #ifdef INET6
384 && (inp->inp_vflag & INP_IPV6) == 0
385 #endif /* INET6 */
386 ))
387 )
388 continue;
389 if (!aflag &&
390 (
391 (istcp && tp->t_state == TCPS_LISTEN)
392 || (af1 == AF_INET &&
393 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
394 #ifdef INET6
395 || (af1 == AF_INET6 &&
396 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
397 #endif /* INET6 */
398 || (af1 == AF_UNSPEC &&
399 (((inp->inp_vflag & INP_IPV4) != 0 &&
400 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
401 #ifdef INET6
402 || ((inp->inp_vflag & INP_IPV6) != 0 &&
403 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
404 #endif
405 ))
406 ))
407 continue;
408
409 if (first) {
410 if (!Lflag) {
411 xo_emit("Active Internet connections");
412 if (aflag)
413 xo_emit(" (including servers)");
414 } else
415 xo_emit(
416 "Current listen queue sizes (qlen/incqlen/maxqlen)");
417 xo_emit("\n");
418 if (Aflag)
419 xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
420 "Tcpcb");
421 if (Lflag)
422 xo_emit((Aflag && !Wflag) ?
423 "{T:/%-5.5s} {T:/%-14.14s} {T:/%-18.18s}" :
424 ((!Wflag || af1 == AF_INET) ?
425 "{T:/%-5.5s} {T:/%-14.14s} {T:/%-22.22s}" :
426 "{T:/%-5.5s} {T:/%-14.14s} {T:/%-45.45s}"),
427 "Proto", "Listen", "Local Address");
428 else if (Tflag)
429 xo_emit((Aflag && !Wflag) ?
430 "{T:/%-5.5s} {T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
431 ((!Wflag || af1 == AF_INET) ?
432 "{T:/%-5.5s} {T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
433 "{T:/%-5.5s} {T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
434 "Proto", "CC Alg", "Rexmit", "OOORcv", "0-win",
435 "Local Address", "Foreign Address");
436 else {
437 xo_emit((Aflag && !Wflag) ?
438 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
439 ((!Wflag || af1 == AF_INET) ?
440 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
441 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
442 "Proto", "Recv-Q", "Send-Q",
443 "Local Address", "Foreign Address");
444 if (!xflag && !Rflag)
445 xo_emit(" (state)");
446 }
447 if (xflag) {
448 xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
449 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
450 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
451 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
452 "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
453 "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
454 "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
455 xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
456 "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
457 "rexmt", "persist", "keep", "2msl",
458 "delack", "rcvtime");
459 } else if (Rflag) {
460 xo_emit(" {T:/%8.8s} {T:/%5.5s}",
461 "flowid", "ftype");
462 }
463 xo_emit("\n");
464 first = 0;
465 }
466 if (Lflag && so->so_qlimit == 0)
467 continue;
468 xo_open_instance("socket");
469 if (Aflag) {
470 if (istcp)
471 xo_emit("{q:address/%*lx} ",
472 2 * (int)sizeof(void *),
473 (u_long)inp->inp_ppcb);
474 else
475 xo_emit("{q:adddress/%*lx} ",
476 2 * (int)sizeof(void *),
477 (u_long)so->so_pcb);
478 }
479 #ifdef INET6
480 if ((inp->inp_vflag & INP_IPV6) != 0)
481 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
482 "46" : "6";
483 else
484 #endif
485 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
486 "4" : "";
487 if (istcp && (tp->t_flags & TF_TOE) != 0)
488 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
489 else
490 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
491 if (istcp && Tflag)
492 xo_emit("{:cc/%-8.8s} ", algo);
493 if (Lflag) {
494 char buf1[15];
495
496 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
497 so->so_incqlen, so->so_qlimit);
498 xo_emit("{:listen-queue-sizes/%-14.14s} ", buf1);
499 } else if (Tflag) {
500 if (istcp)
501 xo_emit("{:sent-retransmit-packets/%6u} "
502 "{:received-out-of-order-packets/%6u} "
503 "{:sent-zero-window/%6u} ",
504 tp->t_sndrexmitpack, tp->t_rcvoopack,
505 tp->t_sndzerowin);
506 else
507 xo_emit("{P:/%21s}", "");
508 } else {
509 xo_emit("{:receive-bytes-waiting/%6u} "
510 "{:send-bytes-waiting/%6u} ",
511 so->so_rcv.sb_cc, so->so_snd.sb_cc);
512 }
513 if (numeric_port) {
514 if (inp->inp_vflag & INP_IPV4) {
515 inetprint("local", &inp->inp_laddr,
516 (int)inp->inp_lport, name, 1, af1);
517 if (!Lflag)
518 inetprint("remote", &inp->inp_faddr,
519 (int)inp->inp_fport, name, 1, af1);
520 }
521 #ifdef INET6
522 else if (inp->inp_vflag & INP_IPV6) {
523 inet6print("local", &inp->in6p_laddr,
524 (int)inp->inp_lport, name, 1);
525 if (!Lflag)
526 inet6print("remote", &inp->in6p_faddr,
527 (int)inp->inp_fport, name, 1);
528 } /* else nothing printed now */
529 #endif /* INET6 */
530 } else if (inp->inp_flags & INP_ANONPORT) {
531 if (inp->inp_vflag & INP_IPV4) {
532 inetprint("local", &inp->inp_laddr,
533 (int)inp->inp_lport, name, 1, af1);
534 if (!Lflag)
535 inetprint("remote", &inp->inp_faddr,
536 (int)inp->inp_fport, name, 0, af1);
537 }
538 #ifdef INET6
539 else if (inp->inp_vflag & INP_IPV6) {
540 inet6print("local", &inp->in6p_laddr,
541 (int)inp->inp_lport, name, 1);
542 if (!Lflag)
543 inet6print("remote", &inp->in6p_faddr,
544 (int)inp->inp_fport, name, 0);
545 } /* else nothing printed now */
546 #endif /* INET6 */
547 } else {
548 if (inp->inp_vflag & INP_IPV4) {
549 inetprint("local", &inp->inp_laddr,
550 (int)inp->inp_lport, name, 0, af1);
551 if (!Lflag)
552 inetprint("remote", &inp->inp_faddr,
553 (int)inp->inp_fport, name,
554 inp->inp_lport != inp->inp_fport,
555 af1);
556 }
557 #ifdef INET6
558 else if (inp->inp_vflag & INP_IPV6) {
559 inet6print("local", &inp->in6p_laddr,
560 (int)inp->inp_lport, name, 0);
561 if (!Lflag)
562 inet6print("remote", &inp->in6p_faddr,
563 (int)inp->inp_fport, name,
564 inp->inp_lport != inp->inp_fport);
565 } /* else nothing printed now */
566 #endif /* INET6 */
567 }
568 if (xflag) {
569 xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
570 "{:receive-clusters/%6u} {:send-clusters/%6u} "
571 "{:receive-high-water/%6u} {:send-high-water/%6u} "
572 "{:receive-low-water/%6u} {:send-low-water/%6u} "
573 "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
574 "{:receive-mbuf-bytes-max/%6u} "
575 "{:send-mbuf-bytes-max/%6u}",
576 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
577 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
578 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
579 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
580 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
581 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
582 if (timer != NULL)
583 xo_emit(" {:retransmit-timer/%4d.%02d} "
584 "{:persist-timer/%4d.%02d} "
585 "{:keepalive-timer/%4d.%02d} "
586 "{:msl2-timer/%4d.%02d} "
587 "{:delay-ack-timer/%4d.%02d} "
588 "{:inactivity-timer/%4d.%02d}",
589 timer->tt_rexmt / 1000,
590 (timer->tt_rexmt % 1000) / 10,
591 timer->tt_persist / 1000,
592 (timer->tt_persist % 1000) / 10,
593 timer->tt_keep / 1000,
594 (timer->tt_keep % 1000) / 10,
595 timer->tt_2msl / 1000,
596 (timer->tt_2msl % 1000) / 10,
597 timer->tt_delack / 1000,
598 (timer->tt_delack % 1000) / 10,
599 timer->t_rcvtime / 1000,
600 (timer->t_rcvtime % 1000) / 10);
601 }
602 if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
603 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
604 xo_emit("{:tcp-state/%d}", tp->t_state);
605 else {
606 xo_emit("{:tcp-state/%s}",
607 tcpstates[tp->t_state]);
608 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
609 /* Show T/TCP `hidden state' */
610 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
611 xo_emit("{:need-syn-or-fin/*}");
612 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
613 }
614 }
615 if (Rflag) {
616 /* XXX: is this right Alfred */
617 xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
618 inp->inp_flowid,
619 inp->inp_flowtype);
620 }
621 xo_emit("\n");
622 xo_close_instance("socket");
623 }
624 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
625 if (oxig->xig_count > xig->xig_count) {
626 xo_emit("Some {d:lost/%s} sockets may have been "
627 "deleted.\n", name);
628 } else if (oxig->xig_count < xig->xig_count) {
629 xo_emit("Some {d:created/%s} sockets may have been "
630 "created.\n", name);
631 } else {
632 xo_emit("Some {d:changed/%s} sockets may have been "
633 "created or deleted.\n", name);
634 }
635 }
636 free(buf);
637 }
638
639 /*
640 * Dump TCP statistics structure.
641 */
642 void
tcp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)643 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
644 {
645 struct tcpstat tcpstat;
646
647 #ifdef INET6
648 if (tcp_done != 0)
649 return;
650 else
651 tcp_done = 1;
652 #endif
653
654 if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
655 sizeof(tcpstat), kread_counters) != 0)
656 return;
657
658 xo_open_container("tcp");
659 xo_emit("{T:/%s}:\n", name);
660
661 #define p(f, m) if (tcpstat.f || sflag <= 1) \
662 xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
663 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
664 xo_emit(m, (uintmax_t )tcpstat.f)
665 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
666 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
667 (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
668 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
669 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
670 (uintmax_t )tcpstat.f2)
671 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
672 xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
673
674 p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
675 p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
676 "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
677 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
678 "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
679 "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
680 "{N:retransmitted}\n");
681 p(tcps_sndrexmitbad, "\t\t"
682 "{:sent-unnecessary-retransmitted-packets/%ju} "
683 "{N:/data packet%s unnecessarily retransmitted}\n");
684 p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
685 "{N:/resend%s initiated by MTU discovery}\n");
686 p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
687 "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
688 "{N:delayed})\n");
689 p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
690 "{N:/URG only packet%s}\n");
691 p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
692 "{N:/window probe packet%s}\n");
693 p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
694 "{N:/window update packet%s}\n");
695 p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
696 "{N:/control packet%s}\n");
697 p(tcps_rcvtotal, "\t{:received-packets/%ju} "
698 "{N:/packet%s received}\n");
699 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
700 "{:received-ack-packets/%ju} {N:/ack%s} "
701 "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
702 p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
703 "{N:/duplicate ack%s}\n");
704 p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
705 "{N:/ack%s for unsent data}\n");
706 p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
707 "{:received-in-sequence-packets/%ju} {N:/packet%s} "
708 "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
709 "{N:received in-sequence}\n");
710 p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
711 "{:received-completely-duplicate-packets/%ju} "
712 "{N:/completely duplicate packet%s} "
713 "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
714 p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
715 "{N:/old duplicate packet%s}\n");
716 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
717 "{:received-some-duplicate-packets/%ju} "
718 "{N:/packet%s with some dup. data} "
719 "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
720 p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
721 "{N:/out-of-order packet%s} "
722 "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
723 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
724 "{:received-after-window-packets/%ju} {N:/packet%s} "
725 "({:received-after-window-bytes/%ju} {N:/byte%s}) "
726 "{N:of data after window}\n");
727 p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
728 "{N:/window probe%s}\n");
729 p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
730 "{N:/window update packet%s}\n");
731 p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
732 "{N:/packet%s received after close}\n");
733 p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
734 "{N:/discarded for bad checksum%s}\n");
735 p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
736 "{N:/discarded for bad header offset field%s}\n");
737 p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
738 "{N:discarded because packet too short}\n");
739 p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
740 "{N:discarded due to memory problems}\n");
741 p(tcps_connattempt, "\t{:connection-requests/%ju} "
742 "{N:/connection request%s}\n");
743 p(tcps_accepts, "\t{:connections-accepts/%ju} "
744 "{N:/connection accept%s}\n");
745 p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
746 "{N:/bad connection attempt%s}\n");
747 p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
748 "{N:/listen queue overflow%s}\n");
749 p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
750 "{N:/ignored RSTs in the window%s}\n");
751 p(tcps_connects, "\t{:connections-established/%ju} "
752 "{N:/connection%s established (including accepts)}\n");
753 p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
754 "{N:/connection%s closed (including} "
755 "{:connection-drops/%ju} {N:/drop%s})\n");
756 p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
757 "{N:/connection%s updated cached RTT on close}\n");
758 p(tcps_cachedrttvar, "\t\t"
759 "{:connections-updated-variance-on-close/%ju} "
760 "{N:/connection%s updated cached RTT variance on close}\n");
761 p(tcps_cachedssthresh, "\t\t"
762 "{:connections-updated-ssthresh-on-close/%ju} "
763 "{N:/connection%s updated cached ssthresh on close}\n");
764 p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
765 "{N:/embryonic connection%s dropped}\n");
766 p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
767 "{N:/segment%s updated rtt (of} "
768 "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
769 p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
770 "{N:/retransmit timeout%s}\n");
771 p(tcps_timeoutdrop, "\t\t"
772 "{:connections-dropped-by-retransmit-timeout/%ju} "
773 "{N:/connection%s dropped by rexmit timeout}\n");
774 p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
775 "{N:/persist timeout%s}\n");
776 p(tcps_persistdrop, "\t\t"
777 "{:connections-dropped-by-persist-timeout/%ju} "
778 "{N:/connection%s dropped by persist timeout}\n");
779 p(tcps_finwait2_drops, "\t"
780 "{:connections-dropped-by-finwait2-timeout/%ju} "
781 "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
782 p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
783 "{N:/keepalive timeout%s}\n");
784 p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
785 "{N:/keepalive probe%s sent}\n");
786 p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
787 "{N:/connection%s dropped by keepalive}\n");
788 p(tcps_predack, "\t{:ack-header-predictions/%ju} "
789 "{N:/correct ACK header prediction%s}\n");
790 p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
791 "{N:/correct data packet header prediction%s}\n");
792
793 xo_open_container("syncache");
794
795 p3(tcps_sc_added, "\t{:entries-added/%ju} "
796 "{N:/syncache entr%s added}\n");
797 p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
798 "{N:/retransmitted}\n");
799 p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
800 p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
801 p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
802 p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
803 "{N:/bucket overflow}\n");
804 p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
805 "{N:/cache overflow}\n");
806 p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
807 p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
808 p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
809 p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
810 p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
811 p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
812 p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
813 p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
814 "{N:/cookie%s received}\n");
815
816 xo_close_container("syncache");
817
818 xo_open_container("hostcache");
819
820 p3(tcps_hc_added, "\t{:entries-added/%ju} "
821 "{N:/hostcache entr%s added}\n");
822 p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
823 "{N:/bucket overflow}\n");
824
825 xo_close_container("hostcache");
826
827 xo_open_container("sack");
828
829 p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
830 "{N:/SACK recovery episode%s}\n");
831 p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
832 "{N:/segment rexmit%s in SACK recovery episodes}\n");
833 p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
834 "{N:/byte rexmit%s in SACK recovery episodes}\n");
835 p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
836 "{N:/SACK option%s (SACK blocks) received}\n");
837 p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
838 "{N:/SACK option%s (SACK blocks) sent}\n");
839 p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
840 "{N:/SACK scoreboard overflow}\n");
841
842 xo_close_container("sack");
843 xo_open_container("ecn");
844
845 p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
846 "{N:/packet%s with ECN CE bit set}\n");
847 p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
848 "{N:/packet%s with ECN ECT(0) bit set}\n");
849 p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
850 "{N:/packet%s with ECN ECT(1) bit set}\n");
851 p(tcps_ecn_shs, "\t{:handshakes/%ju} "
852 "{N:/successful ECN handshake%s}\n");
853 p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
854 "{N:/time%s ECN reduced the congestion window}\n");
855 #undef p
856 #undef p1a
857 #undef p2
858 #undef p2a
859 #undef p3
860 xo_close_container("ecn");
861 xo_close_container("tcp");
862 }
863
864 /*
865 * Dump UDP statistics structure.
866 */
867 void
udp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)868 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
869 {
870 struct udpstat udpstat;
871 uint64_t delivered;
872
873 #ifdef INET6
874 if (udp_done != 0)
875 return;
876 else
877 udp_done = 1;
878 #endif
879
880 if (fetch_stats("net.inet.udp.stats", off, &udpstat,
881 sizeof(udpstat), kread_counters) != 0)
882 return;
883
884 xo_open_container("udp");
885 xo_emit("{T:/%s}:\n", name);
886
887 #define p(f, m) if (udpstat.f || sflag <= 1) \
888 xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
889 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
890 xo_emit("\t" m, (uintmax_t)udpstat.f)
891
892 p(udps_ipackets, "{:received-datagrams/%ju} "
893 "{N:/datagram%s received}\n");
894 p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
895 "{N:/with incomplete header}\n");
896 p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
897 "{N:/with bad data length field}\n");
898 p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
899 "{N:/with bad checksum}\n");
900 p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
901 "{N:/with no checksum}\n");
902 p1a(udps_noport, "{:dropped-no-socket/%ju} "
903 "{N:/dropped due to no socket}\n");
904 p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
905 "{N:/broadcast\\/multicast datagram%s undelivered}\n");
906 p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
907 "{N:/dropped due to full socket buffers}\n");
908 p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
909 "{N:/not for hashed pcb}\n");
910 delivered = udpstat.udps_ipackets -
911 udpstat.udps_hdrops -
912 udpstat.udps_badlen -
913 udpstat.udps_badsum -
914 udpstat.udps_noport -
915 udpstat.udps_noportbcast -
916 udpstat.udps_fullsock;
917 if (delivered || sflag <= 1)
918 xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
919 (uint64_t)delivered);
920 p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
921 /* the next statistic is cumulative in udps_noportbcast */
922 p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
923 "{N:/time%s multicast source filter matched}\n");
924 #undef p
925 #undef p1a
926 xo_close_container("udp");
927 }
928
929 /*
930 * Dump CARP statistics structure.
931 */
932 void
carp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)933 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
934 {
935 struct carpstats carpstat;
936
937 if (fetch_stats("net.inet.carp.stats", off, &carpstat,
938 sizeof(carpstat), kread_counters) != 0)
939 return;
940
941 xo_open_container(name);
942 xo_emit("{T:/%s}:\n", name);
943
944 #define p(f, m) if (carpstat.f || sflag <= 1) \
945 xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
946 #define p2(f, m) if (carpstat.f || sflag <= 1) \
947 xo_emit(m, (uintmax_t)carpstat.f)
948
949 p(carps_ipackets, "\t{:received-inet-packets/%ju} "
950 "{N:/packet%s received (IPv4)}\n");
951 p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
952 "{N:/packet%s received (IPv6)}\n");
953 p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
954 "{N:/packet%s discarded for wrong TTL}\n");
955 p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
956 "{N:/packet%s shorter than header}\n");
957 p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
958 "{N:/discarded for bad checksum%s}\n");
959 p(carps_badver, "\t\t{:dropped-bad-version/%ju} "
960 "{N:/discarded packet%s with a bad version}\n");
961 p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
962 "{N:/discarded because packet too short}\n");
963 p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
964 "{N:/discarded for bad authentication}\n");
965 p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
966 "{N:/discarded for bad vhid}\n");
967 p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
968 "{N:/discarded because of a bad address list}\n");
969 p(carps_opackets, "\t{:sent-inet-packets/%ju} "
970 "{N:/packet%s sent (IPv4)}\n");
971 p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
972 "{N:/packet%s sent (IPv6)}\n");
973 p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
974 "{N:/send failed due to mbuf memory error}\n");
975 #if notyet
976 p(carps_ostates, "\t\t{:send-state-updates/%s} "
977 "{N:/state update%s sent}\n");
978 #endif
979 #undef p
980 #undef p2
981 xo_close_container(name);
982 }
983
984 /*
985 * Dump IP statistics structure.
986 */
987 void
ip_stats(u_long off,const char * name,int af1 __unused,int proto __unused)988 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
989 {
990 struct ipstat ipstat;
991
992 if (fetch_stats("net.inet.ip.stats", off, &ipstat,
993 sizeof(ipstat), kread_counters) != 0)
994 return;
995
996 xo_open_container(name);
997 xo_emit("{T:/%s}:\n", name);
998
999 #define p(f, m) if (ipstat.f || sflag <= 1) \
1000 xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
1001 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
1002 xo_emit(m, (uintmax_t )ipstat.f)
1003
1004 p(ips_total, "\t{:received-packets/%ju} "
1005 "{N:/total packet%s received}\n");
1006 p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1007 "{N:/bad header checksum%s}\n");
1008 p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1009 "{N:/with size smaller than minimum}\n");
1010 p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1011 "{N:/with data size < data length}\n");
1012 p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1013 "{N:/with ip length > max ip packet size}\n");
1014 p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1015 "{N:/with header length < data size}\n");
1016 p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1017 "{N:/with data length < header length}\n");
1018 p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1019 "{N:/with bad options}\n");
1020 p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1021 "{N:/with incorrect version number}\n");
1022 p(ips_fragments, "\t{:received-fragments/%ju} "
1023 "{N:/fragment%s received}\n");
1024 p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1025 "{N:/fragment%s dropped (dup or out of space)}\n");
1026 p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1027 "{N:/fragment%s dropped after timeout}\n");
1028 p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1029 "{N:/packet%s reassembled ok}\n");
1030 p(ips_delivered, "\t{:received-local-packets/%ju} "
1031 "{N:/packet%s for this host}\n");
1032 p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1033 "{N:/packet%s for unknown\\/unsupported protocol}\n");
1034 p(ips_forward, "\t{:forwarded-packets/%ju} "
1035 "{N:/packet%s forwarded}");
1036 p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1037 "{N:/packet%s fast forwarded})");
1038 if (ipstat.ips_forward || sflag <= 1)
1039 xo_emit("\n");
1040 p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1041 "{N:/packet%s not forwardable}\n");
1042 p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1043 "{N:/packet%s received for unknown multicast group}\n");
1044 p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1045 "{N:/redirect%s sent}\n");
1046 p(ips_localout, "\t{:sent-packets/%ju} "
1047 "{N:/packet%s sent from this host}\n");
1048 p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1049 "{N:/packet%s sent with fabricated ip header}\n");
1050 p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1051 "{N:/output packet%s dropped due to no bufs, etc.}\n");
1052 p(ips_noroute, "\t{:discard-no-route/%ju} "
1053 "{N:/output packet%s discarded due to no route}\n");
1054 p(ips_fragmented, "\t{:sent-fragments/%ju} "
1055 "{N:/output datagram%s fragmented}\n");
1056 p(ips_ofragments, "\t{:fragments-created/%ju} "
1057 "{N:/fragment%s created}\n");
1058 p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1059 "{N:/datagram%s that can't be fragmented}\n");
1060 p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1061 "{N:/tunneling packet%s that can't find gif}\n");
1062 p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1063 "{N:/datagram%s with bad address in header}\n");
1064 #undef p
1065 #undef p1a
1066 xo_close_container(name);
1067 }
1068
1069 /*
1070 * Dump ARP statistics structure.
1071 */
1072 void
arp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1073 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1074 {
1075 struct arpstat arpstat;
1076
1077 if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1078 sizeof(arpstat), kread_counters) != 0)
1079 return;
1080
1081 xo_open_container(name);
1082 xo_emit("{T:/%s}:\n", name);
1083
1084 #define p(f, m) if (arpstat.f || sflag <= 1) \
1085 xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1086 #define p2(f, m) if (arpstat.f || sflag <= 1) \
1087 xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1088
1089 p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1090 p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1091 p(rxrequests, "{:received-requests/%ju} "
1092 "{N:/ARP request%s received}\n");
1093 p2(rxreplies, "{:received-replies/%ju} "
1094 "{N:/ARP repl%s received}\n");
1095 p(received, "{:received-packers/%ju} "
1096 "{N:/ARP packet%s received}\n");
1097 p(dropped, "{:dropped-no-entry/%ju} "
1098 "{N:/total packet%s dropped due to no ARP entry}\n");
1099 p(timeouts, "{:entries-timeout/%ju} "
1100 "{N:/ARP entry%s timed out}\n");
1101 p(dupips, "{:dropped-duplicate-address/%ju} "
1102 "{N:/Duplicate IP%s seen}\n");
1103 #undef p
1104 #undef p2
1105 xo_close_container(name);
1106 }
1107
1108
1109
1110 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
1111 "echo reply", /* RFC 792 */
1112 "#1",
1113 "#2",
1114 "destination unreachable", /* RFC 792 */
1115 "source quench", /* RFC 792 */
1116 "routing redirect", /* RFC 792 */
1117 "#6",
1118 "#7",
1119 "echo", /* RFC 792 */
1120 "router advertisement", /* RFC 1256 */
1121 "router solicitation", /* RFC 1256 */
1122 "time exceeded", /* RFC 792 */
1123 "parameter problem", /* RFC 792 */
1124 "time stamp", /* RFC 792 */
1125 "time stamp reply", /* RFC 792 */
1126 "information request", /* RFC 792 */
1127 "information request reply", /* RFC 792 */
1128 "address mask request", /* RFC 950 */
1129 "address mask reply", /* RFC 950 */
1130 "#19",
1131 "#20",
1132 "#21",
1133 "#22",
1134 "#23",
1135 "#24",
1136 "#25",
1137 "#26",
1138 "#27",
1139 "#28",
1140 "#29",
1141 "icmp traceroute", /* RFC 1393 */
1142 "datagram conversion error", /* RFC 1475 */
1143 "mobile host redirect",
1144 "IPv6 where-are-you",
1145 "IPv6 i-am-here",
1146 "mobile registration req",
1147 "mobile registration reply",
1148 "domain name request", /* RFC 1788 */
1149 "domain name reply", /* RFC 1788 */
1150 "icmp SKIP",
1151 "icmp photuris", /* RFC 2521 */
1152 };
1153
1154 /*
1155 * Dump ICMP statistics.
1156 */
1157 void
icmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1158 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1159 {
1160 struct icmpstat icmpstat;
1161 size_t len;
1162 int i, first;
1163
1164 if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1165 sizeof(icmpstat), kread_counters) != 0)
1166 return;
1167
1168 xo_open_container(name);
1169 xo_emit("{T:/%s}:\n", name);
1170
1171 #define p(f, m) if (icmpstat.f || sflag <= 1) \
1172 xo_emit(m, icmpstat.f, plural(icmpstat.f))
1173 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
1174 xo_emit(m, icmpstat.f)
1175 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
1176 xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1177
1178 p(icps_error, "\t{:icmp-calls/%lu} "
1179 "{N:/call%s to icmp_error}\n");
1180 p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1181 "{N:/error%s not generated in response to an icmp message}\n");
1182
1183 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1184 if (icmpstat.icps_outhist[i] != 0) {
1185 if (first) {
1186 xo_open_list("output-histogram");
1187 xo_emit("\tOutput histogram:\n");
1188 first = 0;
1189 }
1190 xo_open_instance("output-histogram");
1191 if (icmpnames[i] != NULL)
1192 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1193 icmpnames[i], icmpstat.icps_outhist[i]);
1194 else
1195 xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1196 "{:count/%lu}\n",
1197 i, icmpstat.icps_outhist[i]);
1198 xo_close_instance("output-histogram");
1199 }
1200 }
1201 if (!first)
1202 xo_close_list("output-histogram");
1203
1204 p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1205 "{N:/message%s with bad code fields}\n");
1206 p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1207 "{N:/message%s less than the minimum length}\n");
1208 p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1209 "{N:/message%s with bad checksum}\n");
1210 p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1211 "{N:/message%s with bad length}\n");
1212 p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1213 "{N:/multicast echo requests ignored}\n");
1214 p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1215 "{N:/multicast timestamp requests ignored}\n");
1216
1217 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1218 if (icmpstat.icps_inhist[i] != 0) {
1219 if (first) {
1220 xo_open_list("input-histogram");
1221 xo_emit("\tInput histogram:\n");
1222 first = 0;
1223 }
1224 xo_open_instance("input-histogram");
1225 if (icmpnames[i] != NULL)
1226 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1227 icmpnames[i],
1228 icmpstat.icps_inhist[i]);
1229 else
1230 xo_emit(
1231 "\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1232 i, icmpstat.icps_inhist[i]);
1233 xo_close_instance("input-histogram");
1234 }
1235 }
1236 if (!first)
1237 xo_close_list("input-histogram");
1238
1239 p(icps_reflect, "\t{:sent-packets/%lu} "
1240 "{N:/message response%s generated}\n");
1241 p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1242 "{N:/invalid return address%s}\n");
1243 p(icps_noroute, "\t{:discard-no-route/%lu} "
1244 "{N:/no return route%s}\n");
1245 #undef p
1246 #undef p1a
1247 #undef p2
1248 if (live) {
1249 len = sizeof i;
1250 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1251 0)
1252 return;
1253 xo_emit("\tICMP address mask responses are "
1254 "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1255 }
1256
1257 xo_close_container(name);
1258 }
1259
1260 /*
1261 * Dump IGMP statistics structure.
1262 */
1263 void
igmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1264 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1265 {
1266 struct igmpstat igmpstat;
1267
1268 if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1269 sizeof(igmpstat), kread) != 0)
1270 return;
1271
1272 if (igmpstat.igps_version != IGPS_VERSION_3) {
1273 xo_warnx("%s: version mismatch (%d != %d)", __func__,
1274 igmpstat.igps_version, IGPS_VERSION_3);
1275 }
1276 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1277 xo_warnx("%s: size mismatch (%d != %d)", __func__,
1278 igmpstat.igps_len, IGPS_VERSION3_LEN);
1279 }
1280
1281 xo_open_container(name);
1282 xo_emit("{T:/%s}:\n", name);
1283
1284 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1285 xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1286 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1287 xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1288
1289 p64(igps_rcv_total, "\t{:received-messages/%ju} "
1290 "{N:/message%s received}\n");
1291 p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1292 "{N:/message%s received with too few bytes}\n");
1293 p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1294 "{N:/message%s received with wrong TTL}\n");
1295 p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1296 "{N:/message%s received with bad checksum}\n");
1297 py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1298 "{N:/V1\\/V2 membership quer%s received}\n");
1299 py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1300 "{N:/V3 membership quer%s received}\n");
1301 py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1302 "{N:/membership quer%s received with invalid field(s)}\n");
1303 py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1304 "{N:/general quer%s received}\n");
1305 py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1306 "{N:/group quer%s received}\n");
1307 py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1308 "{N:/group-source quer%s received}\n");
1309 py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1310 "{N:/group-source quer%s dropped}\n");
1311 p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1312 "{N:/membership report%s received}\n");
1313 p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1314 "{N:/membership report%s received with invalid field(s)}\n");
1315 p64(igps_rcv_ourreports, "\t"
1316 "{:received-membership-reports-matching/%ju} "
1317 "{N:/membership report%s received for groups to which we belong}"
1318 "\n");
1319 p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1320 "{N:/V3 report%s received without Router Alert}\n");
1321 p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1322 "{N:/membership report%s sent}\n");
1323 #undef p64
1324 #undef py64
1325 xo_close_container(name);
1326 }
1327
1328 /*
1329 * Dump PIM statistics structure.
1330 */
1331 void
pim_stats(u_long off __unused,const char * name,int af1 __unused,int proto __unused)1332 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1333 int proto __unused)
1334 {
1335 struct pimstat pimstat;
1336
1337 if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1338 sizeof(pimstat), kread_counters) != 0)
1339 return;
1340
1341 xo_open_container(name);
1342 xo_emit("{T:/%s}:\n", name);
1343
1344 #define p(f, m) if (pimstat.f || sflag <= 1) \
1345 xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1346 #define py(f, m) if (pimstat.f || sflag <= 1) \
1347 xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1348
1349 p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1350 "{N:/message%s received}\n");
1351 p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1352 "{N:/byte%s received}\n");
1353 p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1354 "{N:/message%s received with too few bytes}\n");
1355 p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1356 "{N:/message%s received with bad checksum}\n");
1357 p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1358 "{N:/message%s received with bad version}\n");
1359 p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1360 "{N:/data register message%s received}\n");
1361 p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1362 "{N:/data register byte%s received}\n");
1363 p(pims_rcv_registers_wrongiif, "\t"
1364 "{:received-data-register-wrong-interface/%ju} "
1365 "{N:/data register message%s received on wrong iif}\n");
1366 p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1367 "{N:/bad register%s received}\n");
1368 p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1369 "{N:/data register message%s sent}\n");
1370 p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1371 "{N:/data register byte%s sent}\n");
1372 #undef p
1373 #undef py
1374 xo_close_container(name);
1375 }
1376
1377 /*
1378 * Pretty print an Internet address (net address + port).
1379 */
1380 void
inetprint(const char * container,struct in_addr * in,int port,const char * proto,int num_port,const int af1)1381 inetprint(const char *container, struct in_addr *in, int port,
1382 const char *proto, int num_port, const int af1)
1383 {
1384 struct servent *sp = 0;
1385 char line[80], *cp;
1386 int width;
1387
1388 if (container)
1389 xo_open_container(container);
1390
1391 if (Wflag)
1392 sprintf(line, "%s.", inetname(in));
1393 else
1394 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1395 cp = strchr(line, '\0');
1396 if (!num_port && port)
1397 sp = getservbyport((int)port, proto);
1398 if (sp || port == 0)
1399 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1400 else
1401 sprintf(cp, "%d ", ntohs((u_short)port));
1402 width = (Aflag && !Wflag) ? 18 :
1403 ((!Wflag || af1 == AF_INET) ? 22 : 45);
1404 if (Wflag)
1405 xo_emit("{d:target/%-*s} ", width, line);
1406 else
1407 xo_emit("{d:target/%-*.*s} ", width, width, line);
1408
1409 int alen = cp - line - 1, plen = strlen(cp) - 1;
1410 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1411 plen, cp);
1412
1413 if (container)
1414 xo_close_container(container);
1415 }
1416
1417 /*
1418 * Construct an Internet address representation.
1419 * If numeric_addr has been supplied, give
1420 * numeric value, otherwise try for symbolic name.
1421 */
1422 char *
inetname(struct in_addr * inp)1423 inetname(struct in_addr *inp)
1424 {
1425 char *cp;
1426 static char line[MAXHOSTNAMELEN];
1427 struct hostent *hp;
1428 struct netent *np;
1429
1430 cp = 0;
1431 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1432 int net = inet_netof(*inp);
1433 int lna = inet_lnaof(*inp);
1434
1435 if (lna == INADDR_ANY) {
1436 np = getnetbyaddr(net, AF_INET);
1437 if (np)
1438 cp = np->n_name;
1439 }
1440 if (cp == 0) {
1441 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1442 if (hp) {
1443 cp = hp->h_name;
1444 trimdomain(cp, strlen(cp));
1445 }
1446 }
1447 }
1448 if (inp->s_addr == INADDR_ANY)
1449 strcpy(line, "*");
1450 else if (cp) {
1451 strlcpy(line, cp, sizeof(line));
1452 } else {
1453 inp->s_addr = ntohl(inp->s_addr);
1454 #define C(x) ((u_int)((x) & 0xff))
1455 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1456 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
1457 }
1458 return (line);
1459 }
1460