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. 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 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/domain.h>
33 #define _WANT_PROTOSW
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #define _WANT_SOCKET
37 #include <sys/socketvar.h>
38 #include <sys/sysctl.h>
39
40 #include <net/route.h>
41 #include <net/if_arp.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_carp.h>
46 #ifdef INET6
47 #include <netinet/ip6.h>
48 #endif /* INET6 */
49 #include <netinet/in_pcb.h>
50 #include <netinet/ip_icmp.h>
51 #include <netinet/icmp_var.h>
52 #include <netinet/igmp_var.h>
53 #include <netinet/ip_divert.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/pim_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcpip.h>
58 #include <netinet/tcp_seq.h>
59 #define TCPSTATES
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64
65 #include <arpa/inet.h>
66 #include <errno.h>
67 #include <libutil.h>
68 #include <netdb.h>
69 #include <stdint.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <stdbool.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <libxo/xo.h>
76 #include "netstat.h"
77 #include "nl_defs.h"
78
79 #define max(a, b) (((a) > (b)) ? (a) : (b))
80
81 #ifdef INET
82 static void inetprint(const char *, struct in_addr *, int, const char *, int,
83 const int);
84 #endif
85 #ifdef INET6
86 static int udp_done, udplite_done, tcp_done, sdp_done;
87 #endif /* INET6 */
88
89 static int
pcblist_sysctl(int proto,const char * name,char ** bufp)90 pcblist_sysctl(int proto, const char *name, char **bufp)
91 {
92 const char *mibvar;
93 char *buf;
94 size_t len;
95
96 switch (proto) {
97 case IPPROTO_TCP:
98 mibvar = "net.inet.tcp.pcblist";
99 break;
100 case IPPROTO_UDP:
101 mibvar = "net.inet.udp.pcblist";
102 break;
103 case IPPROTO_UDPLITE:
104 mibvar = "net.inet.udplite.pcblist";
105 break;
106 default:
107 mibvar = "net.inet.raw.pcblist";
108 break;
109 }
110 if (strncmp(name, "sdp", 3) == 0)
111 mibvar = "net.inet.sdp.pcblist";
112 else if (strncmp(name, "divert", 6) == 0)
113 mibvar = "net.inet.divert.pcblist";
114 len = 0;
115 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
116 if (errno != ENOENT)
117 xo_warn("sysctl: %s", mibvar);
118 return (0);
119 }
120 if ((buf = malloc(len)) == NULL) {
121 xo_warnx("malloc %lu bytes", (u_long)len);
122 return (0);
123 }
124 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
125 xo_warn("sysctl: %s", mibvar);
126 free(buf);
127 return (0);
128 }
129 *bufp = buf;
130 return (1);
131 }
132
133 /*
134 * Copied directly from uipc_socket2.c. We leave out some fields that are in
135 * nested structures that aren't used to avoid extra work.
136 */
137 static void
sbtoxsockbuf(struct sockbuf * sb,struct xsockbuf * xsb)138 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
139 {
140 xsb->sb_cc = sb->sb_ccc;
141 xsb->sb_hiwat = sb->sb_hiwat;
142 xsb->sb_mbcnt = sb->sb_mbcnt;
143 xsb->sb_mbmax = sb->sb_mbmax;
144 xsb->sb_lowat = sb->sb_lowat;
145 xsb->sb_flags = sb->sb_flags;
146 xsb->sb_timeo = sb->sb_timeo;
147 }
148
149 int
sotoxsocket(struct socket * so,struct xsocket * xso)150 sotoxsocket(struct socket *so, struct xsocket *xso)
151 {
152 struct protosw proto;
153 struct domain domain;
154
155 bzero(xso, sizeof *xso);
156 xso->xso_len = sizeof *xso;
157 xso->xso_so = (uintptr_t)so;
158 xso->so_type = so->so_type;
159 xso->so_options = so->so_options;
160 xso->so_linger = so->so_linger;
161 xso->so_state = so->so_state;
162 xso->so_pcb = (uintptr_t)so->so_pcb;
163 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
164 return (-1);
165 xso->xso_protocol = proto.pr_protocol;
166 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
167 return (-1);
168 xso->xso_family = domain.dom_family;
169 xso->so_timeo = so->so_timeo;
170 xso->so_error = so->so_error;
171 if ((so->so_options & SO_ACCEPTCONN) != 0) {
172 xso->so_qlen = so->sol_qlen;
173 xso->so_incqlen = so->sol_incqlen;
174 xso->so_qlimit = so->sol_qlimit;
175 } else {
176 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
177 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
178 xso->so_oobmark = so->so_oobmark;
179 }
180 return (0);
181 }
182
183 /*
184 * Print a summary of connections related to an Internet
185 * protocol. For TCP, also give state of connection.
186 * Listening processes (aflag) are suppressed unless the
187 * -a (all) flag is specified.
188 */
189 void
protopr(u_long off,const char * name,int af1,int proto)190 protopr(u_long off, const char *name, int af1, int proto)
191 {
192 static int first = 1;
193 int istcp;
194 char *buf;
195 const char *vchar;
196 struct xtcpcb *tp;
197 struct xinpcb *inp;
198 struct xinpgen *xig, *oxig;
199 struct xsocket *so;
200 int fnamelen, cnamelen;
201
202 istcp = 0;
203 switch (proto) {
204 case IPPROTO_TCP:
205 #ifdef INET6
206 if (strncmp(name, "sdp", 3) != 0) {
207 if (tcp_done != 0)
208 return;
209 else
210 tcp_done = 1;
211 } else {
212 if (sdp_done != 0)
213 return;
214 else
215 sdp_done = 1;
216 }
217 #endif
218 istcp = 1;
219 break;
220 case IPPROTO_UDP:
221 #ifdef INET6
222 if (udp_done != 0)
223 return;
224 else
225 udp_done = 1;
226 #endif
227 break;
228 case IPPROTO_UDPLITE:
229 #ifdef INET6
230 if (udplite_done != 0)
231 return;
232 else
233 udplite_done = 1;
234 #endif
235 break;
236 }
237
238 if (!pcblist_sysctl(proto, name, &buf))
239 return;
240 if (istcp && (cflag || Cflag)) {
241 fnamelen = strlen("Stack");
242 cnamelen = strlen("CC");
243 oxig = xig = (struct xinpgen *)buf;
244 for (xig = (struct xinpgen*)((char *)xig + xig->xig_len);
245 xig->xig_len > sizeof(struct xinpgen);
246 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
247 tp = (struct xtcpcb *)xig;
248 inp = &tp->xt_inp;
249 if (inp->inp_gencnt > oxig->xig_gen)
250 continue;
251 so = &inp->xi_socket;
252 if (so->xso_protocol != proto)
253 continue;
254 fnamelen = max(fnamelen, (int)strlen(tp->xt_stack));
255 cnamelen = max(cnamelen, (int)strlen(tp->xt_cc));
256 }
257 }
258
259 oxig = xig = (struct xinpgen *)buf;
260 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
261 xig->xig_len > sizeof(struct xinpgen);
262 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
263 if (istcp) {
264 tp = (struct xtcpcb *)xig;
265 inp = &tp->xt_inp;
266 } else {
267 inp = (struct xinpcb *)xig;
268 }
269 so = &inp->xi_socket;
270
271 /* Ignore sockets for protocols other than the desired one. */
272 if (proto != 0 && so->xso_protocol != proto)
273 continue;
274
275 /* Ignore PCBs which were freed during copyout. */
276 if (inp->inp_gencnt > oxig->xig_gen)
277 continue;
278
279 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
280 #ifdef INET6
281 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
282 #endif /* INET6 */
283 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
284 #ifdef INET6
285 && (inp->inp_vflag & INP_IPV6) == 0
286 #endif /* INET6 */
287 ))
288 )
289 continue;
290 if (!aflag &&
291 (
292 (istcp && tp->t_state == TCPS_LISTEN)
293 || (af1 == AF_INET &&
294 inp->inp_laddr.s_addr == INADDR_ANY)
295 #ifdef INET6
296 || (af1 == AF_INET6 &&
297 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
298 #endif /* INET6 */
299 || (af1 == AF_UNSPEC &&
300 (((inp->inp_vflag & INP_IPV4) != 0 &&
301 inp->inp_laddr.s_addr == INADDR_ANY)
302 #ifdef INET6
303 || ((inp->inp_vflag & INP_IPV6) != 0 &&
304 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
305 #endif
306 ))
307 ))
308 continue;
309
310 if (first) {
311 if (!Lflag) {
312 xo_emit("Active Internet connections");
313 if (aflag)
314 xo_emit(" (including servers)");
315 } else
316 xo_emit(
317 "Current listen queue sizes (qlen/incqlen/maxqlen)");
318 xo_emit("\n");
319 if (Aflag)
320 xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
321 "Tcpcb");
322 if (Lflag)
323 xo_emit((Aflag && !Wflag) ?
324 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
325 ((!Wflag || af1 == AF_INET) ?
326 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
327 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
328 "Proto", "Listen", "Local Address");
329 else if (Tflag)
330 xo_emit((Aflag && !Wflag) ?
331 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
332 ((!Wflag || af1 == AF_INET) ?
333 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
334 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
335 "Proto", "Rexmit", "OOORcv", "0-win",
336 "Local Address", "Foreign Address");
337 else {
338 xo_emit((Aflag && !Wflag) ?
339 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
340 ((!Wflag || af1 == AF_INET) ?
341 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
342 "{T:/%-9.9s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
343 "Proto", "Recv-Q", "Send-Q",
344 "Local Address", "Foreign Address");
345 if (!xflag && !Rflag)
346 xo_emit(" {T:/%-11.11s}", "(state)");
347 }
348 if (xflag) {
349 xo_emit(" {T:/%-6.6s} {T:/%-6.6s} "
350 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
351 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
352 "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
353 "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
354 xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
355 "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
356 "rexmt", "persist", "keep", "2msl",
357 "delack", "rcvtime");
358 } else if (Rflag) {
359 xo_emit(" {T:/%8.8s} {T:/%5.5s}",
360 "flowid", "ftype");
361 }
362 if (cflag) {
363 xo_emit(" {T:/%-*.*s}",
364 fnamelen, fnamelen, "Stack");
365 }
366 if (Cflag)
367 xo_emit(" {T:/%-*.*s} {T:/%10.10s}"
368 " {T:/%10.10s} {T:/%5.5s}"
369 " {T:/%3.3s}", cnamelen,
370 cnamelen, "CC",
371 "cwin",
372 "ssthresh",
373 "MSS",
374 "ECN");
375 if (Pflag)
376 xo_emit(" {T:/%s}", "Log ID");
377 xo_emit("\n");
378 first = 0;
379 }
380 if (Lflag && so->so_qlimit == 0)
381 continue;
382 xo_open_instance("socket");
383 if (Aflag)
384 xo_emit("{q:address/%*lx} ", 2 * (int)sizeof(void *),
385 (u_long)so->so_pcb);
386 #ifdef INET6
387 if ((inp->inp_vflag & INP_IPV6) != 0)
388 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
389 "46" : "6";
390 else
391 #endif
392 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
393 "4" : "";
394 if (istcp && (tp->t_flags & TF_TOE) != 0)
395 xo_emit("{:protocol/%-3.3s%-6.6s/%s%s} ", "toe", vchar);
396 else {
397 int len;
398
399 len = max (2, 9 - strlen(name));
400 xo_emit("{:protocol/%.7s%-*.*s/%s%s} ", name, len, len,
401 vchar);
402 }
403 if (Lflag) {
404 char buf1[33];
405
406 snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
407 so->so_incqlen, so->so_qlimit);
408 xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
409 } else if (Tflag) {
410 if (istcp)
411 xo_emit("{:sent-retransmit-packets/%6u} "
412 "{:received-out-of-order-packets/%6u} "
413 "{:sent-zero-window/%6u} ",
414 tp->t_sndrexmitpack, tp->t_rcvoopack,
415 tp->t_sndzerowin);
416 else
417 xo_emit("{P:/%21s}", "");
418 } else {
419 xo_emit("{:receive-bytes-waiting/%6u} "
420 "{:send-bytes-waiting/%6u} ",
421 so->so_rcv.sb_cc, so->so_snd.sb_cc);
422 }
423 if (numeric_port) {
424 #ifdef INET
425 if (inp->inp_vflag & INP_IPV4) {
426 inetprint("local", &inp->inp_laddr,
427 (int)inp->inp_lport, name, 1, af1);
428 if (!Lflag)
429 inetprint("remote", &inp->inp_faddr,
430 (int)inp->inp_fport, name, 1, af1);
431 }
432 #endif
433 #if defined(INET) && defined(INET6)
434 else
435 #endif
436 #ifdef INET6
437 if (inp->inp_vflag & INP_IPV6) {
438 inet6print("local", &inp->in6p_laddr,
439 (int)inp->inp_lport, name, 1);
440 if (!Lflag)
441 inet6print("remote", &inp->in6p_faddr,
442 (int)inp->inp_fport, name, 1);
443 } /* else nothing printed now */
444 #endif /* INET6 */
445 } else if (inp->inp_flags & INP_ANONPORT) {
446 #ifdef INET
447 if (inp->inp_vflag & INP_IPV4) {
448 inetprint("local", &inp->inp_laddr,
449 (int)inp->inp_lport, name, 1, af1);
450 if (!Lflag)
451 inetprint("remote", &inp->inp_faddr,
452 (int)inp->inp_fport, name, 0, af1);
453 }
454 #endif
455 #if defined(INET) && defined(INET6)
456 else
457 #endif
458 #ifdef INET6
459 if (inp->inp_vflag & INP_IPV6) {
460 inet6print("local", &inp->in6p_laddr,
461 (int)inp->inp_lport, name, 1);
462 if (!Lflag)
463 inet6print("remote", &inp->in6p_faddr,
464 (int)inp->inp_fport, name, 0);
465 } /* else nothing printed now */
466 #endif /* INET6 */
467 } else {
468 #ifdef INET
469 if (inp->inp_vflag & INP_IPV4) {
470 inetprint("local", &inp->inp_laddr,
471 (int)inp->inp_lport, name, 0, af1);
472 if (!Lflag)
473 inetprint("remote", &inp->inp_faddr,
474 (int)inp->inp_fport, name,
475 inp->inp_lport != inp->inp_fport,
476 af1);
477 }
478 #endif
479 #if defined(INET) && defined(INET6)
480 else
481 #endif
482 #ifdef INET6
483 if (inp->inp_vflag & INP_IPV6) {
484 inet6print("local", &inp->in6p_laddr,
485 (int)inp->inp_lport, name, 0);
486 if (!Lflag)
487 inet6print("remote", &inp->in6p_faddr,
488 (int)inp->inp_fport, name,
489 inp->inp_lport != inp->inp_fport);
490 } /* else nothing printed now */
491 #endif /* INET6 */
492 }
493 if (xflag) {
494 xo_emit("{:receive-high-water/%6u} "
495 "{:send-high-water/%6u} "
496 "{:receive-low-water/%6u} {:send-low-water/%6u} "
497 "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
498 "{:receive-mbuf-bytes-max/%6u} "
499 "{:send-mbuf-bytes-max/%6u}",
500 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
501 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
502 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
503 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
504 if (istcp)
505 xo_emit(" {:retransmit-timer/%4d.%02d} "
506 "{:persist-timer/%4d.%02d} "
507 "{:keepalive-timer/%4d.%02d} "
508 "{:msl2-timer/%4d.%02d} "
509 "{:delay-ack-timer/%4d.%02d} "
510 "{:inactivity-timer/%4d.%02d}",
511 tp->tt_rexmt / 1000,
512 (tp->tt_rexmt % 1000) / 10,
513 tp->tt_persist / 1000,
514 (tp->tt_persist % 1000) / 10,
515 tp->tt_keep / 1000,
516 (tp->tt_keep % 1000) / 10,
517 tp->tt_2msl / 1000,
518 (tp->tt_2msl % 1000) / 10,
519 tp->tt_delack / 1000,
520 (tp->tt_delack % 1000) / 10,
521 tp->t_rcvtime / 1000,
522 (tp->t_rcvtime % 1000) / 10);
523 }
524 if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
525 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
526 xo_emit("{:tcp-state/%-11d}", tp->t_state);
527 else {
528 xo_emit("{:tcp-state/%-11s}",
529 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 xo_emit("{:need-syn-or-fin/*}");
534 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
535 }
536 }
537 if (Rflag) {
538 /* XXX: is this right Alfred */
539 xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
540 inp->inp_flowid,
541 inp->inp_flowtype);
542 }
543 if (istcp) {
544 if (cflag)
545 xo_emit(" {:stack/%-*.*s}",
546
547 fnamelen, fnamelen, tp->xt_stack);
548 if (Cflag)
549 xo_emit(" {:cc/%-*.*s}"
550 " {:snd-cwnd/%10lu}"
551 " {:snd-ssthresh/%10lu}"
552 " {:t-maxseg/%5u} {:ecn/%3s}",
553 cnamelen, cnamelen, tp->xt_cc,
554 tp->t_snd_cwnd, tp->t_snd_ssthresh,
555 tp->t_maxseg,
556 (tp->t_state >= TCPS_ESTABLISHED ?
557 (tp->xt_ecn > 0 ?
558 (tp->xt_ecn == 1 ?
559 "ecn" : "ace")
560 : "off")
561 : "n/a"));
562 if (Pflag)
563 xo_emit(" {:log-id/%s}",
564 tp->xt_logid[0] == '\0' ?
565 "-" : tp->xt_logid);
566 }
567 xo_emit("\n");
568 xo_close_instance("socket");
569 }
570 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
571 if (oxig->xig_count > xig->xig_count) {
572 xo_emit("Some {d:lost/%s} sockets may have been "
573 "deleted.\n", name);
574 } else if (oxig->xig_count < xig->xig_count) {
575 xo_emit("Some {d:created/%s} sockets may have been "
576 "created.\n", name);
577 } else {
578 xo_emit("Some {d:changed/%s} sockets may have been "
579 "created or deleted.\n", name);
580 }
581 }
582 free(buf);
583 }
584
585 /*
586 * Dump TCP statistics structure.
587 */
588 void
tcp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)589 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
590 {
591 struct tcpstat tcpstat;
592 uint64_t tcps_states[TCP_NSTATES];
593
594 #ifdef INET6
595 if (tcp_done != 0)
596 return;
597 else
598 tcp_done = 1;
599 #endif
600
601 if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
602 sizeof(tcpstat), kread_counters) != 0)
603 return;
604
605 if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
606 &tcps_states, sizeof(tcps_states), kread_counters) != 0)
607 return;
608
609 xo_open_container("tcp");
610 xo_emit("{T:/%s}:\n", name);
611
612 #define p(f, m) if (tcpstat.f || sflag <= 1) \
613 xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
614 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
615 xo_emit(m, (uintmax_t )tcpstat.f)
616 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
617 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
618 (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
619 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
620 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
621 (uintmax_t )tcpstat.f2)
622 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
623 xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
624
625 p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
626 p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
627 "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
628 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
629 "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
630 "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
631 "{N:retransmitted}\n");
632 p(tcps_sndrexmitbad, "\t\t"
633 "{:sent-unnecessary-retransmitted-packets/%ju} "
634 "{N:/data packet%s unnecessarily retransmitted}\n");
635 p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
636 "{N:/resend%s initiated by MTU discovery}\n");
637 p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
638 "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
639 "{N:delayed})\n");
640 p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
641 "{N:/URG only packet%s}\n");
642 p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
643 "{N:/window probe packet%s}\n");
644 p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
645 "{N:/window update packet%s}\n");
646 p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
647 "{N:/control packet%s}\n");
648 p(tcps_rcvtotal, "\t{:received-packets/%ju} "
649 "{N:/packet%s received}\n");
650 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
651 "{:received-ack-packets/%ju} {N:/ack%s} "
652 "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
653 p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
654 "{N:/duplicate ack%s}\n");
655 p(tcps_tunneled_pkts, "\t\t{:received-udp-tunneled-pkts/%ju} "
656 "{N:/UDP tunneled pkt%s}\n");
657 p(tcps_tunneled_errs, "\t\t{:received-bad-udp-tunneled-pkts/%ju} "
658 "{N:/UDP tunneled pkt cnt with error%s}\n");
659 p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-data-not-yet-sent/%ju} "
660 "{N:/ack%s for data not yet sent}\n");
661 p(tcps_rcvghostack, "\t\t{:received-acks-for-data-never-been-sent/%ju} "
662 "{N:/ack%s for data never been sent (ghost acks)}\n");
663 p(tcps_rcvacktooold, "\t\t{:received-acks-for-data-being-too-old/%ju} "
664 "{N:/ack%s for data being too old}\n");
665 p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
666 "{:received-in-sequence-packets/%ju} {N:/packet%s} "
667 "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
668 "{N:received in-sequence}\n");
669 p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
670 "{:received-completely-duplicate-packets/%ju} "
671 "{N:/completely duplicate packet%s} "
672 "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
673 p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
674 "{N:/old duplicate packet%s}\n");
675 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
676 "{:received-some-duplicate-packets/%ju} "
677 "{N:/packet%s with some dup. data} "
678 "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
679 p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
680 "{N:/out-of-order packet%s} "
681 "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
682 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
683 "{:received-after-window-packets/%ju} {N:/packet%s} "
684 "({:received-after-window-bytes/%ju} {N:/byte%s}) "
685 "{N:of data after window}\n");
686 p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
687 "{N:/window probe%s}\n");
688 p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
689 "{N:/window update packet%s}\n");
690 p(tcps_dsack_count, "\t\t{:received-with-dsack-packets/%ju} "
691 "{N:/packet%s received with dsack}\n");
692 p(tcps_dsack_bytes, "\t\t{:received-with-dsack-bytes/%ju} "
693 "{N:/dsack byte%s received (no TLP involved)}\n");
694 p(tcps_dsack_tlp_bytes, "\t\t{:received-with-dsack-bytes-tlp/%ju} "
695 "{N:/dsack byte%s received (TLP responsible)}\n");
696 p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
697 "{N:/packet%s received after close}\n");
698 p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
699 "{N:/discarded for bad checksum%s}\n");
700 p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
701 "{N:/discarded for bad header offset field%s}\n");
702 p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
703 "{N:discarded because packet too short}\n");
704 p1a(tcps_rcvreassfull, "\t\t{:discard-reassembly-queue-full/%ju} "
705 "{N:discarded due to full reassembly queue}\n");
706 p(tcps_connattempt, "\t{:connection-requests/%ju} "
707 "{N:/connection request%s}\n");
708 p(tcps_accepts, "\t{:connections-accepts/%ju} "
709 "{N:/connection accept%s}\n");
710 p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
711 "{N:/bad connection attempt%s}\n");
712 p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
713 "{N:/listen queue overflow%s}\n");
714 p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
715 "{N:/ignored RSTs in the window%s}\n");
716 p(tcps_connects, "\t{:connections-established/%ju} "
717 "{N:/connection%s established (including accepts)}\n");
718 p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
719 "{N:/time%s used RTT from hostcache}\n");
720 p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
721 "{N:/time%s used RTT variance from hostcache}\n");
722 p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
723 "{N:/time%s used slow-start threshold from hostcache}\n");
724 p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
725 "{N:/connection%s closed (including} "
726 "{:connection-drops/%ju} {N:/drop%s})\n");
727 p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
728 "{N:/connection%s updated cached RTT on close}\n");
729 p(tcps_cachedrttvar, "\t\t"
730 "{:connections-updated-variance-on-close/%ju} "
731 "{N:/connection%s updated cached RTT variance on close}\n");
732 p(tcps_cachedssthresh, "\t\t"
733 "{:connections-updated-ssthresh-on-close/%ju} "
734 "{N:/connection%s updated cached ssthresh on close}\n");
735 p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
736 "{N:/embryonic connection%s dropped}\n");
737 p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
738 "{N:/segment%s updated rtt (of} "
739 "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
740 p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
741 "{N:/retransmit timeout%s}\n");
742 p(tcps_timeoutdrop, "\t\t"
743 "{:connections-dropped-by-retransmit-timeout/%ju} "
744 "{N:/connection%s dropped by rexmit timeout}\n");
745 p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
746 "{N:/persist timeout%s}\n");
747 p(tcps_persistdrop, "\t\t"
748 "{:connections-dropped-by-persist-timeout/%ju} "
749 "{N:/connection%s dropped by persist timeout}\n");
750 p(tcps_finwait2_drops, "\t"
751 "{:connections-dropped-by-finwait2-timeout/%ju} "
752 "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
753 p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
754 "{N:/keepalive timeout%s}\n");
755 p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
756 "{N:/keepalive probe%s sent}\n");
757 p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
758 "{N:/connection%s dropped by keepalive}\n");
759 p(tcps_progdrops, "\t{:connections-dropped-due-to-progress-time/%ju} "
760 "{N:/connection%s dropped due to exceeding progress time}\n");
761 p(tcps_predack, "\t{:ack-header-predictions/%ju} "
762 "{N:/correct ACK header prediction%s}\n");
763 p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
764 "{N:/correct data packet header prediction%s}\n");
765
766 xo_open_container("syncache");
767
768 p3(tcps_sc_added, "\t{:entries-added/%ju} "
769 "{N:/syncache entr%s added}\n");
770 p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
771 "{N:/retransmitted}\n");
772 p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
773 p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
774 p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
775 p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
776 "{N:/bucket overflow}\n");
777 p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
778 "{N:/cache overflow}\n");
779 p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
780 p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
781 p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
782 p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
783 p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
784 p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
785
786 xo_close_container("syncache");
787
788 xo_open_container("syncookies");
789
790 p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
791 p(tcps_sc_recvcookie, "\t\t{:received-cookies/%ju} "
792 "{N:/cookie%s received}\n");
793 p(tcps_sc_spurcookie, "\t\t{:spurious-cookies/%ju} "
794 "{N:/spurious cookie%s rejected}\n");
795 p(tcps_sc_failcookie, "\t\t{:failed-cookies/%ju} "
796 "{N:/failed cookie%s rejected}\n");
797
798 xo_close_container("syncookies");
799
800 xo_open_container("hostcache");
801
802 p3(tcps_hc_added, "\t{:entries-added/%ju} "
803 "{N:/hostcache entr%s added}\n");
804 p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
805 "{N:/bucket overflow}\n");
806
807 xo_close_container("hostcache");
808
809 xo_open_container("sack");
810
811 p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
812 "{N:/SACK recovery episode%s}\n");
813 p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
814 "{N:/segment rexmit%s in SACK recovery episodes}\n");
815 p(tcps_sack_rexmits_tso, "\t{:tso-chunk-retransmits/%ju} "
816 "{N:/tso chunk rexmit%s in SACK recovery episodes}\n");
817 p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
818 "{N:/byte rexmit%s in SACK recovery episodes}\n");
819 p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
820 "{N:/SACK option%s (SACK blocks) received}\n");
821 p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
822 "{N:/SACK option%s (SACK blocks) sent}\n");
823 p(tcps_sack_lostrexmt, "\t{:lost-retransmissions/%ju} "
824 "{N:/SACK retransmission%s lost}\n");
825 p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
826 "{N:/SACK scoreboard overflow}\n");
827
828 xo_close_container("sack");
829 xo_open_container("ecn");
830
831 p(tcps_ecn_rcvce, "\t{:received-ce-packets/%ju} "
832 "{N:/packet%s received with ECN CE bit set}\n");
833 p(tcps_ecn_rcvect0, "\t{:received-ect0-packets/%ju} "
834 "{N:/packet%s received with ECN ECT(0) bit set}\n");
835 p(tcps_ecn_rcvect1, "\t{:received-ect1-packets/%ju} "
836 "{N:/packet%s received with ECN ECT(1) bit set}\n");
837 p(tcps_ecn_sndect0, "\t{:sent-ect0-packets/%ju} "
838 "{N:/packet%s sent with ECN ECT(0) bit set}\n");
839 p(tcps_ecn_sndect1, "\t{:sent-ect1-packets/%ju} "
840 "{N:/packet%s sent with ECN ECT(1) bit set}\n");
841 p(tcps_ecn_shs, "\t{:handshakes/%ju} "
842 "{N:/successful ECN handshake%s}\n");
843 p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
844 "{N:/time%s ECN reduced the congestion window}\n");
845
846 p(tcps_ace_nect, "\t{:ace-nonect-syn/%ju} "
847 "{N:/ACE SYN packet%s with Non-ECT}\n");
848 p(tcps_ace_ect0, "\t{:ace-ect0-syn/%ju} "
849 "{N:/ACE SYN packet%s with ECT0}\n");
850 p(tcps_ace_ect1, "\t{:ace-ect1-syn/%ju} "
851 "{N:/ACE SYN packet%s with ECT1}\n");
852 p(tcps_ace_ce, "\t{:ace-ce-syn/%ju} "
853 "{N:/ACE SYN packet%s with CE}\n");
854
855 xo_close_container("ecn");
856 xo_open_container("tcp-signature");
857 p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
858 "{N:/packet%s with matching signature received}\n");
859 p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
860 "{N:/packet%s with bad signature received}\n");
861 p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
862 "{N:/time%s failed to make signature due to no SA}\n");
863 p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
864 "{N:/time%s unexpected signature received}\n");
865 p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
866 "{N:/time%s no signature provided by segment}\n");
867
868 xo_close_container("tcp-signature");
869 xo_open_container("pmtud");
870
871 p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
872 "{N:/Path MTU discovery black hole detection activation%s}\n");
873 p(tcps_pmtud_blackhole_activated_min_mss,
874 "\t{:pmtud-activated-min-mss/%ju} "
875 "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
876 p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
877 "{N:/Path MTU discovery black hole detection failure%s}\n");
878
879 xo_close_container("pmtud");
880 xo_open_container("tw");
881
882 p(tcps_tw_responds, "\t{:tw_responds/%ju} "
883 "{N:/time%s connection in TIME-WAIT responded with ACK}\n");
884 p(tcps_tw_recycles, "\t{:tw_recycles/%ju} "
885 "{N:/time%s connection in TIME-WAIT was actively recycled}\n");
886 p(tcps_tw_resets, "\t{:tw_resets/%ju} "
887 "{N:/time%s connection in TIME-WAIT responded with RST}\n");
888
889 xo_close_container("tw");
890 #undef p
891 #undef p1a
892 #undef p2
893 #undef p2a
894 #undef p3
895
896 xo_open_container("TCP connection count by state");
897 xo_emit("{T:/TCP connection count by state}:\n");
898 for (int i = 0; i < TCP_NSTATES; i++) {
899 /*
900 * XXXGL: is there a way in libxo to use %s
901 * in the "content string" of a format
902 * string? I failed to do that, that's why
903 * a temporary buffer is used to construct
904 * format string for xo_emit().
905 */
906 char fmtbuf[80];
907
908 if (sflag > 1 && tcps_states[i] == 0)
909 continue;
910 snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
911 "{Np:/connection ,connections} in %s state\n",
912 tcpstates[i], tcpstates[i]);
913 xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
914 }
915 xo_close_container("TCP connection count by state");
916
917 xo_close_container("tcp");
918 }
919
920 /*
921 * Dump UDP statistics structure.
922 */
923 void
udp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)924 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
925 {
926 struct udpstat udpstat;
927 uint64_t delivered, noportbmcast;
928
929 #ifdef INET6
930 if (udp_done != 0)
931 return;
932 else
933 udp_done = 1;
934 #endif
935
936 if (fetch_stats("net.inet.udp.stats", off, &udpstat,
937 sizeof(udpstat), kread_counters) != 0)
938 return;
939
940 xo_open_container("udp");
941 xo_emit("{T:/%s}:\n", name);
942
943 #define p(f, m) if (udpstat.f || sflag <= 1) \
944 xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
945 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
946 xo_emit("\t" m, (uintmax_t)udpstat.f)
947
948 p(udps_ipackets, "{:received-datagrams/%ju} "
949 "{N:/datagram%s received}\n");
950 p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
951 "{N:/with incomplete header}\n");
952 p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
953 "{N:/with bad data length field}\n");
954 p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
955 "{N:/with bad checksum}\n");
956 p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
957 "{N:/with no checksum}\n");
958 p1a(udps_noport, "{:dropped-no-socket/%ju} "
959 "{N:/dropped due to no socket}\n");
960 noportbmcast = udpstat.udps_noportmcast + udpstat.udps_noportbcast;
961 if (noportbmcast || sflag <= 1)
962 xo_emit("\t{:dropped-broadcast-multicast/%ju} "
963 "{N:/broadcast\\/multicast datagram%s undelivered}\n",
964 (uintmax_t)noportbmcast, plural(noportbmcast));
965 p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
966 "{N:/dropped due to full socket buffers}\n");
967 p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
968 "{N:/not for hashed pcb}\n");
969 delivered = udpstat.udps_ipackets -
970 udpstat.udps_hdrops -
971 udpstat.udps_badlen -
972 udpstat.udps_badsum -
973 udpstat.udps_noport -
974 udpstat.udps_fullsock;
975 if (delivered || sflag <= 1)
976 xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
977 (uintmax_t)delivered);
978 p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
979 /* the next statistic is cumulative in udps_noportbcast */
980 p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
981 "{N:/time%s multicast source filter matched}\n");
982 #undef p
983 #undef p1a
984 xo_close_container("udp");
985 }
986
987 /*
988 * Dump CARP statistics structure.
989 */
990 void
carp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)991 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
992 {
993 struct carpstats carpstat;
994
995 if (fetch_stats("net.inet.carp.stats", off, &carpstat,
996 sizeof(carpstat), kread_counters) != 0)
997 return;
998
999 xo_open_container(name);
1000 xo_emit("{T:/%s}:\n", name);
1001
1002 #define p(f, m) if (carpstat.f || sflag <= 1) \
1003 xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
1004 #define p2(f, m) if (carpstat.f || sflag <= 1) \
1005 xo_emit(m, (uintmax_t)carpstat.f)
1006
1007 p(carps_ipackets, "\t{:received-inet-packets/%ju} "
1008 "{N:/packet%s received (IPv4)}\n");
1009 p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
1010 "{N:/packet%s received (IPv6)}\n");
1011 p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
1012 "{N:/packet%s discarded for wrong TTL}\n");
1013 p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
1014 "{N:/packet%s shorter than header}\n");
1015 p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
1016 "{N:/discarded for bad checksum%s}\n");
1017 p(carps_badver, "\t\t{:dropped-bad-version/%ju} "
1018 "{N:/discarded packet%s with a bad version}\n");
1019 p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
1020 "{N:/discarded because packet too short}\n");
1021 p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
1022 "{N:/discarded for bad authentication}\n");
1023 p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
1024 "{N:/discarded for bad vhid}\n");
1025 p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
1026 "{N:/discarded because of a bad address list}\n");
1027 p(carps_opackets, "\t{:sent-inet-packets/%ju} "
1028 "{N:/packet%s sent (IPv4)}\n");
1029 p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
1030 "{N:/packet%s sent (IPv6)}\n");
1031 p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
1032 "{N:/send failed due to mbuf memory error}\n");
1033 #if notyet
1034 p(carps_ostates, "\t\t{:send-state-updates/%s} "
1035 "{N:/state update%s sent}\n");
1036 #endif
1037 #undef p
1038 #undef p2
1039 xo_close_container(name);
1040 }
1041
1042 /*
1043 * Dump IP statistics structure.
1044 */
1045 void
ip_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1046 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1047 {
1048 struct ipstat ipstat;
1049
1050 if (fetch_stats("net.inet.ip.stats", off, &ipstat,
1051 sizeof(ipstat), kread_counters) != 0)
1052 return;
1053
1054 xo_open_container(name);
1055 xo_emit("{T:/%s}:\n", name);
1056
1057 #define p(f, m) if (ipstat.f || sflag <= 1) \
1058 xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
1059 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
1060 xo_emit(m, (uintmax_t )ipstat.f)
1061
1062 p(ips_total, "\t{:received-packets/%ju} "
1063 "{N:/total packet%s received}\n");
1064 p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1065 "{N:/bad header checksum%s}\n");
1066 p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1067 "{N:/with size smaller than minimum}\n");
1068 p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1069 "{N:/with data size < data length}\n");
1070 p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1071 "{N:/with ip length > max ip packet size}\n");
1072 p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1073 "{N:/with header length < data size}\n");
1074 p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1075 "{N:/with data length < header length}\n");
1076 p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1077 "{N:/with bad options}\n");
1078 p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1079 "{N:/with incorrect version number}\n");
1080 p(ips_fragments, "\t{:received-fragments/%ju} "
1081 "{N:/fragment%s received}\n");
1082 p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1083 "{N:/fragment%s dropped (dup or out of space)}\n");
1084 p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1085 "{N:/fragment%s dropped after timeout}\n");
1086 p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1087 "{N:/packet%s reassembled ok}\n");
1088 p(ips_delivered, "\t{:received-local-packets/%ju} "
1089 "{N:/packet%s for this host}\n");
1090 p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1091 "{N:/packet%s for unknown\\/unsupported protocol}\n");
1092 p(ips_forward, "\t{:forwarded-packets/%ju} "
1093 "{N:/packet%s forwarded}");
1094 p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1095 "{N:/packet%s fast forwarded})");
1096 if (ipstat.ips_forward || sflag <= 1)
1097 xo_emit("\n");
1098 p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1099 "{N:/packet%s not forwardable}\n");
1100 p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1101 "{N:/packet%s received for unknown multicast group}\n");
1102 p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1103 "{N:/redirect%s sent}\n");
1104 p(ips_localout, "\t{:sent-packets/%ju} "
1105 "{N:/packet%s sent from this host}\n");
1106 p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1107 "{N:/packet%s sent with fabricated ip header}\n");
1108 p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1109 "{N:/output packet%s dropped due to no bufs, etc.}\n");
1110 p(ips_noroute, "\t{:discard-no-route/%ju} "
1111 "{N:/output packet%s discarded due to no route}\n");
1112 p(ips_fragmented, "\t{:sent-fragments/%ju} "
1113 "{N:/output datagram%s fragmented}\n");
1114 p(ips_ofragments, "\t{:fragments-created/%ju} "
1115 "{N:/fragment%s created}\n");
1116 p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1117 "{N:/datagram%s that can't be fragmented}\n");
1118 p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1119 "{N:/tunneling packet%s that can't find gif}\n");
1120 p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1121 "{N:/datagram%s with bad address in header}\n");
1122 #undef p
1123 #undef p1a
1124 xo_close_container(name);
1125 }
1126
1127 /*
1128 * Dump ARP statistics structure.
1129 */
1130 void
arp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1131 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1132 {
1133 struct arpstat arpstat;
1134
1135 if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1136 sizeof(arpstat), kread_counters) != 0)
1137 return;
1138
1139 xo_open_container(name);
1140 xo_emit("{T:/%s}:\n", name);
1141
1142 #define p(f, m) if (arpstat.f || sflag <= 1) \
1143 xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1144 #define p2(f, m) if (arpstat.f || sflag <= 1) \
1145 xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1146
1147 p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1148 p(txerrors, "{:sent-failures/%ju} {N:/ARP request%s failed to sent}\n");
1149 p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1150 p(rxrequests, "{:received-requests/%ju} "
1151 "{N:/ARP request%s received}\n");
1152 p2(rxreplies, "{:received-replies/%ju} "
1153 "{N:/ARP repl%s received}\n");
1154 p(received, "{:received-packets/%ju} "
1155 "{N:/ARP packet%s received}\n");
1156 p(dropped, "{:dropped-no-entry/%ju} "
1157 "{N:/total packet%s dropped due to no ARP entry}\n");
1158 p(timeouts, "{:entries-timeout/%ju} "
1159 "{N:/ARP entry%s timed out}\n");
1160 p(dupips, "{:dropped-duplicate-address/%ju} "
1161 "{N:/Duplicate IP%s seen}\n");
1162 #undef p
1163 #undef p2
1164 xo_close_container(name);
1165 }
1166
1167
1168
1169 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
1170 "echo reply", /* RFC 792 */
1171 "#1",
1172 "#2",
1173 "destination unreachable", /* RFC 792 */
1174 "source quench", /* RFC 792 */
1175 "routing redirect", /* RFC 792 */
1176 "#6",
1177 "#7",
1178 "echo", /* RFC 792 */
1179 "router advertisement", /* RFC 1256 */
1180 "router solicitation", /* RFC 1256 */
1181 "time exceeded", /* RFC 792 */
1182 "parameter problem", /* RFC 792 */
1183 "time stamp", /* RFC 792 */
1184 "time stamp reply", /* RFC 792 */
1185 "information request", /* RFC 792 */
1186 "information request reply", /* RFC 792 */
1187 "address mask request", /* RFC 950 */
1188 "address mask reply", /* RFC 950 */
1189 "#19",
1190 "#20",
1191 "#21",
1192 "#22",
1193 "#23",
1194 "#24",
1195 "#25",
1196 "#26",
1197 "#27",
1198 "#28",
1199 "#29",
1200 "icmp traceroute", /* RFC 1393 */
1201 "datagram conversion error", /* RFC 1475 */
1202 "mobile host redirect",
1203 "IPv6 where-are-you",
1204 "IPv6 i-am-here",
1205 "mobile registration req",
1206 "mobile registration reply",
1207 "domain name request", /* RFC 1788 */
1208 "domain name reply", /* RFC 1788 */
1209 "icmp SKIP",
1210 "icmp photuris", /* RFC 2521 */
1211 };
1212
1213 /*
1214 * Dump ICMP statistics.
1215 */
1216 void
icmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1217 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1218 {
1219 struct icmpstat icmpstat;
1220 size_t len;
1221 int i, first;
1222
1223 if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1224 sizeof(icmpstat), kread_counters) != 0)
1225 return;
1226
1227 xo_open_container(name);
1228 xo_emit("{T:/%s}:\n", name);
1229
1230 #define p(f, m) if (icmpstat.f || sflag <= 1) \
1231 xo_emit(m, icmpstat.f, plural(icmpstat.f))
1232 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
1233 xo_emit(m, icmpstat.f)
1234 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
1235 xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1236
1237 p(icps_error, "\t{:icmp-calls/%lu} "
1238 "{N:/call%s to icmp_error}\n");
1239 p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1240 "{N:/error%s not generated in response to an icmp message}\n");
1241
1242 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1243 if (icmpstat.icps_outhist[i] != 0) {
1244 if (first) {
1245 xo_open_list("output-histogram");
1246 xo_emit("\tOutput histogram:\n");
1247 first = 0;
1248 }
1249 xo_open_instance("output-histogram");
1250 if (icmpnames[i] != NULL)
1251 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1252 icmpnames[i], icmpstat.icps_outhist[i]);
1253 else
1254 xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1255 "{:count/%lu}\n",
1256 i, icmpstat.icps_outhist[i]);
1257 xo_close_instance("output-histogram");
1258 }
1259 }
1260 if (!first)
1261 xo_close_list("output-histogram");
1262
1263 p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1264 "{N:/message%s with bad code fields}\n");
1265 p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1266 "{N:/message%s less than the minimum length}\n");
1267 p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1268 "{N:/message%s with bad checksum}\n");
1269 p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1270 "{N:/message%s with bad length}\n");
1271 p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1272 "{N:/multicast echo requests ignored}\n");
1273 p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1274 "{N:/multicast timestamp requests ignored}\n");
1275
1276 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1277 if (icmpstat.icps_inhist[i] != 0) {
1278 if (first) {
1279 xo_open_list("input-histogram");
1280 xo_emit("\tInput histogram:\n");
1281 first = 0;
1282 }
1283 xo_open_instance("input-histogram");
1284 if (icmpnames[i] != NULL)
1285 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1286 icmpnames[i],
1287 icmpstat.icps_inhist[i]);
1288 else
1289 xo_emit(
1290 "\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1291 i, icmpstat.icps_inhist[i]);
1292 xo_close_instance("input-histogram");
1293 }
1294 }
1295 if (!first)
1296 xo_close_list("input-histogram");
1297
1298 p(icps_reflect, "\t{:sent-packets/%lu} "
1299 "{N:/message response%s generated}\n");
1300 p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1301 "{N:/invalid return address%s}\n");
1302 p(icps_noroute, "\t{:discard-no-route/%lu} "
1303 "{N:/no return route%s}\n");
1304 #undef p
1305 #undef p1a
1306 #undef p2
1307 if (live) {
1308 len = sizeof i;
1309 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1310 0)
1311 return;
1312 xo_emit("\tICMP address mask responses are "
1313 "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1314 }
1315
1316 xo_close_container(name);
1317 }
1318
1319 /*
1320 * Dump IGMP statistics structure.
1321 */
1322 void
igmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1323 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1324 {
1325 struct igmpstat igmpstat;
1326 int error, zflag0;
1327
1328 if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1329 sizeof(igmpstat), kread) != 0)
1330 return;
1331 /*
1332 * Reread net.inet.igmp.stats when zflag == 1.
1333 * This is because this MIB contains version number and
1334 * length of the structure which are not set when clearing
1335 * the counters.
1336 */
1337 zflag0 = zflag;
1338 if (zflag) {
1339 zflag = 0;
1340 error = fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1341 sizeof(igmpstat), kread);
1342 zflag = zflag0;
1343 if (error)
1344 return;
1345 }
1346
1347 if (igmpstat.igps_version != IGPS_VERSION_3) {
1348 xo_warnx("%s: version mismatch (%d != %d)", __func__,
1349 igmpstat.igps_version, IGPS_VERSION_3);
1350 return;
1351 }
1352 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1353 xo_warnx("%s: size mismatch (%d != %d)", __func__,
1354 igmpstat.igps_len, IGPS_VERSION3_LEN);
1355 return;
1356 }
1357
1358 xo_open_container(name);
1359 xo_emit("{T:/%s}:\n", name);
1360
1361 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1362 xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1363 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1364 xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1365
1366 p64(igps_rcv_total, "\t{:received-messages/%ju} "
1367 "{N:/message%s received}\n");
1368 p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1369 "{N:/message%s received with too few bytes}\n");
1370 p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1371 "{N:/message%s received with wrong TTL}\n");
1372 p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1373 "{N:/message%s received with bad checksum}\n");
1374 py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1375 "{N:/V1\\/V2 membership quer%s received}\n");
1376 py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1377 "{N:/V3 membership quer%s received}\n");
1378 py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1379 "{N:/membership quer%s received with invalid field(s)}\n");
1380 py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1381 "{N:/general quer%s received}\n");
1382 py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1383 "{N:/group quer%s received}\n");
1384 py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1385 "{N:/group-source quer%s received}\n");
1386 py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1387 "{N:/group-source quer%s dropped}\n");
1388 p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1389 "{N:/membership report%s received}\n");
1390 p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1391 "{N:/membership report%s received with invalid field(s)}\n");
1392 p64(igps_rcv_ourreports, "\t"
1393 "{:received-membership-reports-matching/%ju} "
1394 "{N:/membership report%s received for groups to which we belong}"
1395 "\n");
1396 p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1397 "{N:/V3 report%s received without Router Alert}\n");
1398 p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1399 "{N:/membership report%s sent}\n");
1400 #undef p64
1401 #undef py64
1402 xo_close_container(name);
1403 }
1404
1405 /*
1406 * Dump PIM statistics structure.
1407 */
1408 void
pim_stats(u_long off __unused,const char * name,int af1 __unused,int proto __unused)1409 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1410 int proto __unused)
1411 {
1412 struct pimstat pimstat;
1413
1414 if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1415 sizeof(pimstat), kread_counters) != 0)
1416 return;
1417
1418 xo_open_container(name);
1419 xo_emit("{T:/%s}:\n", name);
1420
1421 #define p(f, m) if (pimstat.f || sflag <= 1) \
1422 xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1423 #define py(f, m) if (pimstat.f || sflag <= 1) \
1424 xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1425
1426 p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1427 "{N:/message%s received}\n");
1428 p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1429 "{N:/byte%s received}\n");
1430 p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1431 "{N:/message%s received with too few bytes}\n");
1432 p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1433 "{N:/message%s received with bad checksum}\n");
1434 p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1435 "{N:/message%s received with bad version}\n");
1436 p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1437 "{N:/data register message%s received}\n");
1438 p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1439 "{N:/data register byte%s received}\n");
1440 p(pims_rcv_registers_wrongiif, "\t"
1441 "{:received-data-register-wrong-interface/%ju} "
1442 "{N:/data register message%s received on wrong iif}\n");
1443 p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1444 "{N:/bad register%s received}\n");
1445 p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1446 "{N:/data register message%s sent}\n");
1447 p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1448 "{N:/data register byte%s sent}\n");
1449 #undef p
1450 #undef py
1451 xo_close_container(name);
1452 }
1453
1454 /*
1455 * Dump divert(4) statistics structure.
1456 */
1457 void
divert_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1458 divert_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1459 {
1460 struct divstat divstat;
1461
1462 if (fetch_stats("net.inet.divert.stats", off, &divstat,
1463 sizeof(divstat), kread_counters) != 0)
1464 return;
1465
1466 xo_open_container(name);
1467 xo_emit("{T:/%s}:\n", name);
1468
1469 #define p(f, m) if (divstat.f || sflag <= 1) \
1470 xo_emit(m, (uintmax_t)divstat.f, plural(divstat.f))
1471
1472 p(div_diverted, "\t{:diverted-packets/%ju} "
1473 "{N:/packet%s successfully diverted to userland}\n");
1474 p(div_noport, "\t{:noport-fails/%ju} "
1475 "{N:/packet%s failed to divert due to no socket bound at port}\n");
1476 p(div_outbound, "\t{:outbound-packets/%ju} "
1477 "{N:/packet%s successfully re-injected as outbound}\n");
1478 p(div_inbound, "\t{:inbound-packets/%ju} "
1479 "{N:/packet%s successfully re-injected as inbound}\n");
1480 #undef p
1481 xo_close_container(name);
1482 }
1483
1484 #ifdef INET
1485 /*
1486 * Pretty print an Internet address (net address + port).
1487 */
1488 static void
inetprint(const char * container,struct in_addr * in,int port,const char * proto,int num_port,const int af1)1489 inetprint(const char *container, struct in_addr *in, int port,
1490 const char *proto, int num_port, const int af1)
1491 {
1492 struct servent *sp = 0;
1493 char line[80], *cp;
1494 int width;
1495 size_t alen, plen;
1496
1497 if (container)
1498 xo_open_container(container);
1499
1500 if (Wflag)
1501 snprintf(line, sizeof(line), "%s.", inetname(in));
1502 else
1503 snprintf(line, sizeof(line), "%.*s.",
1504 (Aflag && !num_port) ? 12 : 16, inetname(in));
1505 alen = strlen(line);
1506 cp = line + alen;
1507 if (!num_port && port)
1508 sp = getservbyport((int)port, proto);
1509 if (sp || port == 0)
1510 snprintf(cp, sizeof(line) - alen,
1511 "%.15s ", sp ? sp->s_name : "*");
1512 else
1513 snprintf(cp, sizeof(line) - alen,
1514 "%d ", ntohs((u_short)port));
1515 width = (Aflag && !Wflag) ? 18 :
1516 ((!Wflag || af1 == AF_INET) ? 22 : 45);
1517 if (Wflag)
1518 xo_emit("{d:target/%-*s} ", width, line);
1519 else
1520 xo_emit("{d:target/%-*.*s} ", width, width, line);
1521
1522 plen = strlen(cp) - 1;
1523 alen--;
1524 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1525 plen, cp);
1526
1527 if (container)
1528 xo_close_container(container);
1529 }
1530
1531 /*
1532 * Construct an Internet address representation.
1533 * If numeric_addr has been supplied, give
1534 * numeric value, otherwise try for symbolic name.
1535 */
1536 char *
inetname(struct in_addr * inp)1537 inetname(struct in_addr *inp)
1538 {
1539 char *cp;
1540 static char line[MAXHOSTNAMELEN];
1541 struct hostent *hp;
1542
1543 cp = 0;
1544 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1545 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1546 if (hp) {
1547 cp = hp->h_name;
1548 trimdomain(cp, strlen(cp));
1549 }
1550 }
1551 if (inp->s_addr == INADDR_ANY)
1552 strcpy(line, "*");
1553 else if (cp) {
1554 strlcpy(line, cp, sizeof(line));
1555 } else {
1556 inp->s_addr = ntohl(inp->s_addr);
1557 #define C(x) ((u_int)((x) & 0xff))
1558 snprintf(line, sizeof(line), "%u.%u.%u.%u",
1559 C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1560 C(inp->s_addr >> 8), C(inp->s_addr));
1561 }
1562 return (line);
1563 }
1564 #endif
1565