xref: /freebsd/usr.bin/netstat/sctp.c (revision acc1a9ef8333c798c210fa94be6af4d5fe2dd794)
1 /*-
2  * Copyright (c) 2001-2007, by Weongyo Jeong. All rights reserved.
3  * Copyright (c) 2011, by Michael Tuexen. 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 are met:
7  *
8  * a) Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * b) Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the distribution.
14  *
15  * c) Neither the name of Cisco Systems, Inc. nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)sctp.c	0.1 (Berkeley) 4/18/2007";
35 #endif /* not lint */
36 #endif
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/protosw.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/sctp.h>
51 #include <netinet/sctp_constants.h>
52 #include <arpa/inet.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <libutil.h>
57 #include <netdb.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdbool.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include "netstat.h"
65 #include <libxo/xo.h>
66 
67 #ifdef SCTP
68 
69 static void sctp_statesprint(uint32_t state);
70 
71 #define	NETSTAT_SCTP_STATES_CLOSED		0x0
72 #define	NETSTAT_SCTP_STATES_BOUND		0x1
73 #define	NETSTAT_SCTP_STATES_LISTEN		0x2
74 #define	NETSTAT_SCTP_STATES_COOKIE_WAIT		0x3
75 #define	NETSTAT_SCTP_STATES_COOKIE_ECHOED	0x4
76 #define	NETSTAT_SCTP_STATES_ESTABLISHED		0x5
77 #define	NETSTAT_SCTP_STATES_SHUTDOWN_SENT	0x6
78 #define	NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED	0x7
79 #define	NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT	0x8
80 #define	NETSTAT_SCTP_STATES_SHUTDOWN_PENDING	0x9
81 
82 static const char *sctpstates[] = {
83 	"CLOSED",
84 	"BOUND",
85 	"LISTEN",
86 	"COOKIE_WAIT",
87 	"COOKIE_ECHOED",
88 	"ESTABLISHED",
89 	"SHUTDOWN_SENT",
90 	"SHUTDOWN_RECEIVED",
91 	"SHUTDOWN_ACK_SENT",
92 	"SHUTDOWN_PENDING"
93 };
94 
95 static LIST_HEAD(xladdr_list, xladdr_entry) xladdr_head;
96 struct xladdr_entry {
97 	struct xsctp_laddr *xladdr;
98 	LIST_ENTRY(xladdr_entry) xladdr_entries;
99 };
100 
101 static LIST_HEAD(xraddr_list, xraddr_entry) xraddr_head;
102 struct xraddr_entry {
103 	struct xsctp_raddr *xraddr;
104 	LIST_ENTRY(xraddr_entry) xraddr_entries;
105 };
106 
107 /*
108  * Construct an Internet address representation.
109  * If numeric_addr has been supplied, give
110  * numeric value, otherwise try for symbolic name.
111  */
112 #ifdef INET
113 static char *
114 inetname(struct in_addr *inp)
115 {
116 	char *cp;
117 	static char line[MAXHOSTNAMELEN];
118 	struct hostent *hp;
119 	struct netent *np;
120 
121 	cp = 0;
122 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
123 		int net = inet_netof(*inp);
124 		int lna = inet_lnaof(*inp);
125 
126 		if (lna == INADDR_ANY) {
127 			np = getnetbyaddr(net, AF_INET);
128 			if (np)
129 				cp = np->n_name;
130 		}
131 		if (cp == 0) {
132 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
133 			if (hp) {
134 				cp = hp->h_name;
135 				trimdomain(cp, strlen(cp));
136 			}
137 		}
138 	}
139 	if (inp->s_addr == INADDR_ANY)
140 		strcpy(line, "*");
141 	else if (cp) {
142 		strlcpy(line, cp, sizeof(line));
143 	} else {
144 		inp->s_addr = ntohl(inp->s_addr);
145 #define	C(x)	((u_int)((x) & 0xff))
146 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
147 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
148 		inp->s_addr = htonl(inp->s_addr);
149 	}
150 	return (line);
151 }
152 #endif
153 
154 #ifdef INET6
155 static char ntop_buf[INET6_ADDRSTRLEN];
156 
157 static char *
158 inet6name(struct in6_addr *in6p)
159 {
160 	char *cp;
161 	static char line[50];
162 	struct hostent *hp;
163 	static char domain[MAXHOSTNAMELEN];
164 	static int first = 1;
165 
166 	if (first && !numeric_addr) {
167 		first = 0;
168 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
169 		    (cp = strchr(domain, '.')))
170 			(void) strcpy(domain, cp + 1);
171 		else
172 			domain[0] = 0;
173 	}
174 	cp = 0;
175 	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
176 		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
177 		if (hp) {
178 			if ((cp = strchr(hp->h_name, '.')) &&
179 			    !strcmp(cp + 1, domain))
180 				*cp = 0;
181 			cp = hp->h_name;
182 		}
183 	}
184 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
185 		strcpy(line, "*");
186 	else if (cp)
187 		strcpy(line, cp);
188 	else
189 		sprintf(line, "%s",
190 			inet_ntop(AF_INET6, (void *)in6p, ntop_buf,
191 				sizeof(ntop_buf)));
192 	return (line);
193 }
194 #endif
195 
196 static void
197 sctp_print_address(const char *container, union sctp_sockstore *address,
198     int port, int num_port)
199 {
200 	struct servent *sp = 0;
201 	char line[80], *cp;
202 	int width;
203 
204 	if (container)
205 		xo_open_container(container);
206 
207 	switch (address->sa.sa_family) {
208 #ifdef INET
209 	case AF_INET:
210 		sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr));
211 		break;
212 #endif
213 #ifdef INET6
214 	case AF_INET6:
215 		sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr));
216 		break;
217 #endif
218 	default:
219 		sprintf(line, "%.*s.", Wflag ? 39 : 16, "");
220 		break;
221 	}
222 	cp = strchr(line, '\0');
223 	if (!num_port && port)
224 		sp = getservbyport((int)port, "sctp");
225 	if (sp || port == 0)
226 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
227 	else
228 		sprintf(cp, "%d ", ntohs((u_short)port));
229 	width = Wflag ? 45 : 22;
230 	xo_emit("{d:target/%-*.*s} ", width, width, line);
231 
232 	int alen = cp - line - 1, plen = strlen(cp) - 1;
233 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
234 	    plen, cp);
235 
236 	if (container)
237 		xo_close_container(container);
238 }
239 
240 static int
241 sctp_skip_xinpcb_ifneed(char *buf, const size_t buflen, size_t *offset)
242 {
243 	int exist_tcb = 0;
244 	struct xsctp_tcb *xstcb;
245 	struct xsctp_raddr *xraddr;
246 	struct xsctp_laddr *xladdr;
247 
248 	while (*offset < buflen) {
249 		xladdr = (struct xsctp_laddr *)(buf + *offset);
250 		*offset += sizeof(struct xsctp_laddr);
251 		if (xladdr->last == 1)
252 			break;
253 	}
254 
255 	while (*offset < buflen) {
256 		xstcb = (struct xsctp_tcb *)(buf + *offset);
257 		*offset += sizeof(struct xsctp_tcb);
258 		if (xstcb->last == 1)
259 			break;
260 
261 		exist_tcb = 1;
262 
263 		while (*offset < buflen) {
264 			xladdr = (struct xsctp_laddr *)(buf + *offset);
265 			*offset += sizeof(struct xsctp_laddr);
266 			if (xladdr->last == 1)
267 				break;
268 		}
269 
270 		while (*offset < buflen) {
271 			xraddr = (struct xsctp_raddr *)(buf + *offset);
272 			*offset += sizeof(struct xsctp_raddr);
273 			if (xraddr->last == 1)
274 				break;
275 		}
276 	}
277 
278 	/*
279 	 * If Lflag is set, we don't care about the return value.
280 	 */
281 	if (Lflag)
282 		return 0;
283 
284 	return exist_tcb;
285 }
286 
287 static void
288 sctp_process_tcb(struct xsctp_tcb *xstcb,
289     char *buf, const size_t buflen, size_t *offset, int *indent)
290 {
291 	int i, xl_total = 0, xr_total = 0, x_max;
292 	struct xsctp_raddr *xraddr;
293 	struct xsctp_laddr *xladdr;
294 	struct xladdr_entry *prev_xl = NULL, *xl = NULL, *xl_tmp;
295 	struct xraddr_entry *prev_xr = NULL, *xr = NULL, *xr_tmp;
296 
297 	LIST_INIT(&xladdr_head);
298 	LIST_INIT(&xraddr_head);
299 
300 	/*
301 	 * Make `struct xladdr_list' list and `struct xraddr_list' list
302 	 * to handle the address flexibly.
303 	 */
304 	while (*offset < buflen) {
305 		xladdr = (struct xsctp_laddr *)(buf + *offset);
306 		*offset += sizeof(struct xsctp_laddr);
307 		if (xladdr->last == 1)
308 			break;
309 
310 		prev_xl = xl;
311 		xl = malloc(sizeof(struct xladdr_entry));
312 		if (xl == NULL) {
313 			xo_warnx("malloc %lu bytes",
314 			    (u_long)sizeof(struct xladdr_entry));
315 			goto out;
316 		}
317 		xl->xladdr = xladdr;
318 		if (prev_xl == NULL)
319 			LIST_INSERT_HEAD(&xladdr_head, xl, xladdr_entries);
320 		else
321 			LIST_INSERT_AFTER(prev_xl, xl, xladdr_entries);
322 		xl_total++;
323 	}
324 
325 	while (*offset < buflen) {
326 		xraddr = (struct xsctp_raddr *)(buf + *offset);
327 		*offset += sizeof(struct xsctp_raddr);
328 		if (xraddr->last == 1)
329 			break;
330 
331 		prev_xr = xr;
332 		xr = malloc(sizeof(struct xraddr_entry));
333 		if (xr == NULL) {
334 			xo_warnx("malloc %lu bytes",
335 			    (u_long)sizeof(struct xraddr_entry));
336 			goto out;
337 		}
338 		xr->xraddr = xraddr;
339 		if (prev_xr == NULL)
340 			LIST_INSERT_HEAD(&xraddr_head, xr, xraddr_entries);
341 		else
342 			LIST_INSERT_AFTER(prev_xr, xr, xraddr_entries);
343 		xr_total++;
344 	}
345 
346 	/*
347 	 * Let's print the address infos.
348 	 */
349 	xo_open_list("address");
350 	xl = LIST_FIRST(&xladdr_head);
351 	xr = LIST_FIRST(&xraddr_head);
352 	x_max = (xl_total > xr_total) ? xl_total : xr_total;
353 	for (i = 0; i < x_max; i++) {
354 		xo_open_instance("address");
355 
356 		if (((*indent == 0) && i > 0) || *indent > 0)
357 			xo_emit("{P:/%-12s} ", " ");
358 
359 		if (xl != NULL) {
360 			sctp_print_address("local", &(xl->xladdr->address),
361 			    htons(xstcb->local_port), numeric_port);
362 		} else {
363 			if (Wflag) {
364 				xo_emit("{P:/%-45s} ", " ");
365 			} else {
366 				xo_emit("{P:/%-22s} ", " ");
367 			}
368 		}
369 
370 		if (xr != NULL && !Lflag) {
371 			sctp_print_address("remote", &(xr->xraddr->address),
372 			    htons(xstcb->remote_port), numeric_port);
373 		}
374 
375 		if (xl != NULL)
376 			xl = LIST_NEXT(xl, xladdr_entries);
377 		if (xr != NULL)
378 			xr = LIST_NEXT(xr, xraddr_entries);
379 
380 		if (i == 0 && !Lflag)
381 			sctp_statesprint(xstcb->state);
382 
383 		if (i < x_max)
384 			xo_emit("\n");
385 		xo_close_instance("address");
386 	}
387 
388 out:
389 	/*
390 	 * Free the list which be used to handle the address.
391 	 */
392 	xl = LIST_FIRST(&xladdr_head);
393 	while (xl != NULL) {
394 		xl_tmp = LIST_NEXT(xl, xladdr_entries);
395 		free(xl);
396 		xl = xl_tmp;
397 	}
398 
399 	xr = LIST_FIRST(&xraddr_head);
400 	while (xr != NULL) {
401 		xr_tmp = LIST_NEXT(xr, xraddr_entries);
402 		free(xr);
403 		xr = xr_tmp;
404 	}
405 }
406 
407 static void
408 sctp_process_inpcb(struct xsctp_inpcb *xinpcb,
409     char *buf, const size_t buflen, size_t *offset)
410 {
411 	int indent = 0, xladdr_total = 0, is_listening = 0;
412 	static int first = 1;
413 	const char *tname, *pname;
414 	struct xsctp_tcb *xstcb;
415 	struct xsctp_laddr *xladdr;
416 	size_t offset_laddr;
417 	int process_closed;
418 
419 	if (xinpcb->maxqlen > 0)
420 		is_listening = 1;
421 
422 	if (first) {
423 		if (!Lflag) {
424 			xo_emit("Active SCTP associations");
425 			if (aflag)
426 				xo_emit(" (including servers)");
427 		} else
428 			xo_emit("Current listen queue sizes (qlen/maxqlen)");
429 		xo_emit("\n");
430 		if (Lflag)
431 			xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-8.8s} "
432 			    "{T:/%-22.22s}\n",
433 			    "Proto", "Type", "Listen", "Local Address");
434 		else
435 			if (Wflag)
436 				xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-45.45s} "
437 				    "{T:/%-45.45s} {T:/%s}\n",
438 				    "Proto", "Type",
439 				    "Local Address", "Foreign Address",
440 				    "(state)");
441 			else
442 				xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-22.22s} "
443 				    "{T:/%-22.22s} {T:/%s}\n",
444 				    "Proto", "Type",
445 				    "Local Address", "Foreign Address",
446 				    "(state)");
447 		first = 0;
448 	}
449 	xladdr = (struct xsctp_laddr *)(buf + *offset);
450 	if (Lflag && !is_listening) {
451 		sctp_skip_xinpcb_ifneed(buf, buflen, offset);
452 		return;
453 	}
454 
455 	if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
456 		/* Can't distinguish between sctp46 and sctp6 */
457 		pname = "sctp46";
458 	} else {
459 		pname = "sctp4";
460 	}
461 
462 	if (xinpcb->flags & SCTP_PCB_FLAGS_TCPTYPE)
463 		tname = "1to1";
464 	else if (xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE)
465 		tname = "1toN";
466 	else
467 		tname = "????";
468 
469 	if (Lflag) {
470 		char buf1[22];
471 
472 		snprintf(buf1, sizeof buf1, "%u/%u",
473 		    xinpcb->qlen, xinpcb->maxqlen);
474 		xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
475 		    pname, tname);
476 		xo_emit("{d:queues/%-8.8s}{e:queue-len/%hu}"
477 		    "{e:max-queue-len/%hu} ",
478 		    buf1, xinpcb->qlen, xinpcb->maxqlen);
479 	}
480 
481 	offset_laddr = *offset;
482 	process_closed = 0;
483 
484 	xo_open_list("local-address");
485 retry:
486 	while (*offset < buflen) {
487 		xladdr = (struct xsctp_laddr *)(buf + *offset);
488 		*offset += sizeof(struct xsctp_laddr);
489 		if (xladdr->last) {
490 			if (aflag && !Lflag && (xladdr_total == 0) && process_closed) {
491 				xo_open_instance("local-address");
492 
493 				xo_emit("{:protocol/%-6.6s/%s} "
494 				    "{:type/%-5.5s/%s} ", pname, tname);
495 				if (Wflag) {
496 					xo_emit("{P:/%-91.91s/%s} "
497 					    "{:state/CLOSED}", " ");
498 				} else {
499 					xo_emit("{P:/%-45.45s/%s} "
500 					    "{:state/CLOSED}", " ");
501 				}
502 				xo_close_instance("local-address");
503 			}
504 			if (process_closed || is_listening) {
505 				xo_emit("\n");
506 			}
507 			break;
508 		}
509 
510 		if (!Lflag && !is_listening && !process_closed)
511 			continue;
512 
513 		xo_open_instance("local-address");
514 
515 		if (xladdr_total == 0) {
516 			xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
517 			    pname, tname);
518 		} else {
519 			xo_emit("\n");
520 			xo_emit(Lflag ? "{P:/%-21.21s} " : "{P:/%-12.12s} ",
521 			    " ");
522 		}
523 		sctp_print_address("local", &(xladdr->address),
524 		    htons(xinpcb->local_port), numeric_port);
525 		if (aflag && !Lflag && xladdr_total == 0) {
526 			if (Wflag) {
527 				if (process_closed) {
528 					xo_emit("{P:/%-45.45s} "
529 					    "{:state/CLOSED}", " ");
530 				} else {
531 					xo_emit("{P:/%-45.45s} "
532 					    "{:state:LISTEN}", " ");
533 				}
534 			} else {
535 				if (process_closed) {
536 					xo_emit("{P:/%-22.22s} "
537 					    "{:state/CLOSED}", " ");
538 				} else {
539 					xo_emit("{P:/%-22.22s} "
540 					    "{:state/LISTEN}", " ");
541 				}
542 			}
543 		}
544 		xladdr_total++;
545 		xo_close_instance("local-address");
546 	}
547 
548 	xstcb = (struct xsctp_tcb *)(buf + *offset);
549 	*offset += sizeof(struct xsctp_tcb);
550 	if (aflag && (xladdr_total == 0) && xstcb->last && !process_closed) {
551 		process_closed = 1;
552 		*offset = offset_laddr;
553 		goto retry;
554 	}
555 	while (xstcb->last == 0 && *offset < buflen) {
556 		xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
557 		    pname, tname);
558 		sctp_process_tcb(xstcb, buf, buflen, offset, &indent);
559 		indent++;
560 		xstcb = (struct xsctp_tcb *)(buf + *offset);
561 		*offset += sizeof(struct xsctp_tcb);
562 	}
563 
564 	xo_close_list("local-address");
565 }
566 
567 /*
568  * Print a summary of SCTP connections related to an Internet
569  * protocol.
570  */
571 void
572 sctp_protopr(u_long off __unused,
573     const char *name __unused, int af1 __unused, int proto)
574 {
575 	char *buf;
576 	const char *mibvar = "net.inet.sctp.assoclist";
577 	size_t offset = 0;
578 	size_t len = 0;
579 	struct xsctp_inpcb *xinpcb;
580 
581 	if (proto != IPPROTO_SCTP)
582 		return;
583 
584 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
585 		if (errno != ENOENT)
586 			xo_warn("sysctl: %s", mibvar);
587 		return;
588 	}
589 	if ((buf = malloc(len)) == 0) {
590 		xo_warnx("malloc %lu bytes", (u_long)len);
591 		return;
592 	}
593 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
594 		xo_warn("sysctl: %s", mibvar);
595 		free(buf);
596 		return;
597 	}
598 
599 	xinpcb = (struct xsctp_inpcb *)(buf + offset);
600 	offset += sizeof(struct xsctp_inpcb);
601 	while (xinpcb->last == 0 && offset < len) {
602 		sctp_process_inpcb(xinpcb, buf, (const size_t)len,
603 		    &offset);
604 
605 		xinpcb = (struct xsctp_inpcb *)(buf + offset);
606 		offset += sizeof(struct xsctp_inpcb);
607 	}
608 
609 	free(buf);
610 }
611 
612 static void
613 sctp_statesprint(uint32_t state)
614 {
615 	int idx;
616 
617 	switch (state) {
618 	case SCTP_CLOSED:
619 		idx = NETSTAT_SCTP_STATES_CLOSED;
620 		break;
621 	case SCTP_BOUND:
622 		idx = NETSTAT_SCTP_STATES_BOUND;
623 		break;
624 	case SCTP_LISTEN:
625 		idx = NETSTAT_SCTP_STATES_LISTEN;
626 		break;
627 	case SCTP_COOKIE_WAIT:
628 		idx = NETSTAT_SCTP_STATES_COOKIE_WAIT;
629 		break;
630 	case SCTP_COOKIE_ECHOED:
631 		idx = NETSTAT_SCTP_STATES_COOKIE_ECHOED;
632 		break;
633 	case SCTP_ESTABLISHED:
634 		idx = NETSTAT_SCTP_STATES_ESTABLISHED;
635 		break;
636 	case SCTP_SHUTDOWN_SENT:
637 		idx = NETSTAT_SCTP_STATES_SHUTDOWN_SENT;
638 		break;
639 	case SCTP_SHUTDOWN_RECEIVED:
640 		idx = NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED;
641 		break;
642 	case SCTP_SHUTDOWN_ACK_SENT:
643 		idx = NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT;
644 		break;
645 	case SCTP_SHUTDOWN_PENDING:
646 		idx = NETSTAT_SCTP_STATES_SHUTDOWN_PENDING;
647 		break;
648 	default:
649 		xo_emit("UNKNOWN {:state/0x%08x}", state);
650 		return;
651 	}
652 
653 	xo_emit("{:state/%s}", sctpstates[idx]);
654 }
655 
656 /*
657  * Dump SCTP statistics structure.
658  */
659 void
660 sctp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
661 {
662 	struct sctpstat sctpstat;
663 
664 	if (fetch_stats("net.inet.sctp.stats", off, &sctpstat,
665 	    sizeof(sctpstat), kread) != 0)
666 		return;
667 
668 	xo_open_container(name);
669 	xo_emit("{T:/%s}:\n", name);
670 
671 #define	p(f, m) if (sctpstat.f || sflag <= 1) \
672 	xo_emit(m, (uintmax_t)sctpstat.f, plural(sctpstat.f))
673 #define	p1a(f, m) if (sctpstat.f || sflag <= 1) \
674 	xo_emit(m, (uintmax_t)sctpstat.f)
675 
676 	/*
677 	 * input statistics
678 	 */
679 	p(sctps_recvpackets, "\t{:received-packets/%ju} "
680 	    "{N:/input packet%s}\n");
681 	p(sctps_recvdatagrams, "\t\t{:received-datagrams/%ju} "
682 	    "{N:/datagram%s}\n");
683 	p(sctps_recvpktwithdata, "\t\t{:received-with-data/%ju} "
684 	    "{N:/packet%s that had data}\n");
685 	p(sctps_recvsacks, "\t\t{:received-sack-chunks/%ju} "
686 	    "{N:/input SACK chunk%s}\n");
687 	p(sctps_recvdata, "\t\t{:received-data-chunks/%ju} "
688 	    "{N:/input DATA chunk%s}\n");
689 	p(sctps_recvdupdata, "\t\t{:received-duplicate-data-chunks/%ju} "
690 	    "{N:/duplicate DATA chunk%s}\n");
691 	p(sctps_recvheartbeat, "\t\t{:received-hb-chunks/%ju} "
692 	    "{N:/input HB chunk%s}\n");
693 	p(sctps_recvheartbeatack, "\t\t{:received-hb-ack-chunks/%ju} "
694 	    "{N:/HB-ACK chunk%s}\n");
695 	p(sctps_recvecne, "\t\t{:received-ecne-chunks/%ju} "
696 	    "{N:/input ECNE chunk%s}\n");
697 	p(sctps_recvauth, "\t\t{:received-auth-chunks/%ju} "
698 	    "{N:/input AUTH chunk%s}\n");
699 	p(sctps_recvauthmissing, "\t\t{:dropped-missing-auth/%ju} "
700 	    "{N:/chunk%s missing AUTH}\n");
701 	p(sctps_recvivalhmacid, "\t\t{:dropped-invalid-hmac/%ju} "
702 	    "{N:/invalid HMAC id%s received}\n");
703 	p(sctps_recvivalkeyid, "\t\t{:dropped-invalid-secret/%ju} "
704 	    "{N:/invalid secret id%s received}\n");
705 	p1a(sctps_recvauthfailed, "\t\t{:dropped-auth-failed/%ju} "
706 	    "{N:/auth failed}\n");
707 	p1a(sctps_recvexpress, "\t\t{:received-fast-path/%ju} "
708 	    "{N:/fast path receives all one chunk}\n");
709 	p1a(sctps_recvexpressm, "\t\t{:receives-fast-path-multipart/%ju} "
710 	    "{N:/fast path multi-part data}\n");
711 
712 	/*
713 	 * output statistics
714 	 */
715 	p(sctps_sendpackets, "\t{:sent-packets/%ju} "
716 	    "{N:/output packet%s}\n");
717 	p(sctps_sendsacks, "\t\t{:sent-sacks/%ju} "
718 	    "{N:/output SACK%s}\n");
719 	p(sctps_senddata, "\t\t{:sent-data-chunks/%ju} "
720 	    "{N:/output DATA chunk%s}\n");
721 	p(sctps_sendretransdata, "\t\t{:sent-retransmitted-data-chunks/%ju} "
722 	    "{N:/retransmitted DATA chunk%s}\n");
723 	p(sctps_sendfastretrans, "\t\t"
724 	    "{:sent-fast-retransmitted-data-chunks/%ju} "
725 	    "{N:/fast retransmitted DATA chunk%s}\n");
726 	p(sctps_sendmultfastretrans, "\t\t"
727 	    "{:sent-fast-retransmitted-data-chunk-multiple-times/%ju} "
728 	    "{N:/FR'%s that happened more than once to same chunk}\n");
729 	p(sctps_sendheartbeat, "\t\t{:sent-hb-chunks/%ju} "
730 	    "{N:/output HB chunk%s}\n");
731 	p(sctps_sendecne, "\t\t{:sent-ecne-chunks/%ju} "
732 	    "{N:/output ECNE chunk%s}\n");
733 	p(sctps_sendauth, "\t\t{:sent-auth-chunks/%ju} "
734 	    "{N:/output AUTH chunk%s}\n");
735 	p1a(sctps_senderrors, "\t\t{:send-errors/%ju} "
736 	    "{N:/ip_output error counter}\n");
737 
738 	/*
739 	 * PCKDROPREP statistics
740 	 */
741 	xo_emit("\t{T:Packet drop statistics}:\n");
742 	xo_open_container("drop-statistics");
743 	p1a(sctps_pdrpfmbox, "\t\t{:middle-box/%ju} "
744 	    "{N:/from middle box}\n");
745 	p1a(sctps_pdrpfehos, "\t\t{:end-host/%ju} "
746 	    "{N:/from end host}\n");
747 	p1a(sctps_pdrpmbda, "\t\t{:with-data/%ju} "
748 	    "{N:/with data}\n");
749 	p1a(sctps_pdrpmbct, "\t\t{:non-data/%ju} "
750 	    "{N:/non-data, non-endhost}\n");
751 	p1a(sctps_pdrpbwrpt, "\t\t{:non-endhost/%ju} "
752 	    "{N:/non-endhost, bandwidth rep only}\n");
753 	p1a(sctps_pdrpcrupt, "\t\t{:short-header/%ju} "
754 	    "{N:/not enough for chunk header}\n");
755 	p1a(sctps_pdrpnedat, "\t\t{:short-data/%ju} "
756 	    "{N:/not enough data to confirm}\n");
757 	p1a(sctps_pdrppdbrk, "\t\t{:chunk-break/%ju} "
758 	    "{N:/where process_chunk_drop said break}\n");
759 	p1a(sctps_pdrptsnnf, "\t\t{:tsn-not-found/%ju} "
760 	    "{N:/failed to find TSN}\n");
761 	p1a(sctps_pdrpdnfnd, "\t\t{:reverse-tsn/%ju} "
762 	    "{N:/attempt reverse TSN lookup}\n");
763 	p1a(sctps_pdrpdiwnp, "\t\t{:confirmed-zero-window/%ju} "
764 	    "{N:/e-host confirms zero-rwnd}\n");
765 	p1a(sctps_pdrpdizrw, "\t\t{:middle-box-no-space/%ju} "
766 	    "{N:/midbox confirms no space}\n");
767 	p1a(sctps_pdrpbadd, "\t\t{:bad-data/%ju} "
768 	    "{N:/data did not match TSN}\n");
769 	p(sctps_pdrpmark, "\t\t{:tsn-marked-fast-retransmission/%ju} "
770 	    "{N:/TSN'%s marked for Fast Retran}\n");
771 	xo_close_container("drop-statistics");
772 
773 	/*
774 	 * Timeouts
775 	 */
776 	xo_emit("\t{T:Timeouts}:\n");
777 	xo_open_container("timeouts");
778 	p(sctps_timoiterator, "\t\t{:iterator/%ju} "
779 	    "{N:/iterator timer%s fired}\n");
780 	p(sctps_timodata, "\t\t{:t3-data/%ju} "
781 	    "{N:/T3 data time out%s}\n");
782 	p(sctps_timowindowprobe, "\t\t{:window-probe/%ju} "
783 	    "{N:/window probe (T3) timer%s fired}\n");
784 	p(sctps_timoinit, "\t\t{:init-timer/%ju} "
785 	    "{N:/INIT timer%s fired}\n");
786 	p(sctps_timosack, "\t\t{:sack-timer/%ju} "
787 	    "{N:/sack timer%s fired}\n");
788 	p(sctps_timoshutdown, "\t\t{:shutdown-timer/%ju} "
789 	    "{N:/shutdown timer%s fired}\n");
790 	p(sctps_timoheartbeat, "\t\t{:heartbeat-timer/%ju} "
791 	    "{N:/heartbeat timer%s fired}\n");
792 	p1a(sctps_timocookie, "\t\t{:cookie-timer/%ju} "
793 	    "{N:/a cookie timeout fired}\n");
794 	p1a(sctps_timosecret, "\t\t{:endpoint-changed-cookie/%ju} "
795 	    "{N:/an endpoint changed its cook}ie"
796 	    "secret\n");
797 	p(sctps_timopathmtu, "\t\t{:pmtu-timer/%ju} "
798 	    "{N:/PMTU timer%s fired}\n");
799 	p(sctps_timoshutdownack, "\t\t{:shutdown-timer/%ju} "
800 	    "{N:/shutdown ack timer%s fired}\n");
801 	p(sctps_timoshutdownguard, "\t\t{:shutdown-guard-timer/%ju} "
802 	    "{N:/shutdown guard timer%s fired}\n");
803 	p(sctps_timostrmrst, "\t\t{:stream-reset-timer/%ju} "
804 	    "{N:/stream reset timer%s fired}\n");
805 	p(sctps_timoearlyfr, "\t\t{:early-fast-retransmission-timer/%ju} "
806 	    "{N:/early FR timer%s fired}\n");
807 	p1a(sctps_timoasconf, "\t\t{:asconf-timer/%ju} "
808 	    "{N:/an asconf timer fired}\n");
809 	p1a(sctps_timoautoclose, "\t\t{:auto-close-timer/%ju} "
810 	    "{N:/auto close timer fired}\n");
811 	p(sctps_timoassockill, "\t\t{:asoc-free-timer/%ju} "
812 	    "{N:/asoc free timer%s expired}\n");
813 	p(sctps_timoinpkill, "\t\t{:input-free-timer/%ju} "
814 	    "{N:/inp free timer%s expired}\n");
815 	xo_close_container("timeouts");
816 
817 #if 0
818 	/*
819 	 * Early fast retransmission counters
820 	 */
821 	p(sctps_earlyfrstart, "\t%ju TODO:sctps_earlyfrstart\n");
822 	p(sctps_earlyfrstop, "\t%ju TODO:sctps_earlyfrstop\n");
823 	p(sctps_earlyfrmrkretrans, "\t%ju TODO:sctps_earlyfrmrkretrans\n");
824 	p(sctps_earlyfrstpout, "\t%ju TODO:sctps_earlyfrstpout\n");
825 	p(sctps_earlyfrstpidsck1, "\t%ju TODO:sctps_earlyfrstpidsck1\n");
826 	p(sctps_earlyfrstpidsck2, "\t%ju TODO:sctps_earlyfrstpidsck2\n");
827 	p(sctps_earlyfrstpidsck3, "\t%ju TODO:sctps_earlyfrstpidsck3\n");
828 	p(sctps_earlyfrstpidsck4, "\t%ju TODO:sctps_earlyfrstpidsck4\n");
829 	p(sctps_earlyfrstrid, "\t%ju TODO:sctps_earlyfrstrid\n");
830 	p(sctps_earlyfrstrout, "\t%ju TODO:sctps_earlyfrstrout\n");
831 	p(sctps_earlyfrstrtmr, "\t%ju TODO:sctps_earlyfrstrtmr\n");
832 #endif
833 
834 	/*
835 	 * Others
836 	 */
837 	p1a(sctps_hdrops, "\t{:dropped-too-short/%ju} "
838 	    "{N:/packet shorter than header}\n");
839 	p1a(sctps_badsum, "\t{:dropped-bad-checksum/%ju} "
840 	    "{N:/checksum error}\n");
841 	p1a(sctps_noport, "\t{:dropped-no-endpoint/%ju} "
842 	    "{N:/no endpoint for port}\n");
843 	p1a(sctps_badvtag, "\t{:dropped-bad-v-tag/%ju} "
844 	    "{N:/bad v-tag}\n");
845 	p1a(sctps_badsid, "\t{:dropped-bad-sid/%ju} "
846 	    "{N:/bad SID}\n");
847 	p1a(sctps_nomem, "\t{:dropped-no-memory/%ju} "
848 	    "{N:/no memory}\n");
849 	p1a(sctps_fastretransinrtt, "\t{:multiple-fast-retransmits-in-rtt/%ju} "
850 	    "{N:/number of multiple FR in a RT}T window\n");
851 #if 0
852 	p(sctps_markedretrans, "\t%ju TODO:sctps_markedretrans\n");
853 #endif
854 	p1a(sctps_naglesent, "\t{:rfc813-sent/%ju} "
855 	    "{N:/RFC813 allowed sending}\n");
856 	p1a(sctps_naglequeued, "\t{:rfc813-queued/%ju} "
857 	    "{N:/RFC813 does not allow sending}\n");
858 	p1a(sctps_maxburstqueued, "\t{:max-burst-queued/%ju} "
859 	    "{N:/times max burst prohibited sending}\n");
860 	p1a(sctps_ifnomemqueued, "\t{:no-memory-in-interface/%ju} "
861 	    "{N:/look ahead tells us no memory in interface}\n");
862 	p(sctps_windowprobed, "\t{:sent-window-probes/%ju} "
863 	    "{N:/number%s of window probes sent}\n");
864 	p(sctps_lowlevelerr, "\t{:low-level-err/%ju} "
865 	    "{N:/time%s an output error to clamp down on next user send}\n");
866 	p(sctps_lowlevelerrusr, "\t{:low-level-user-error/%ju} "
867 	    "{N:/time%s sctp_senderrors were caused from a user}\n");
868 	p(sctps_datadropchklmt, "\t{:dropped-chunk-limit/%ju} "
869 	    "{N:/number of in data drop%s due to chunk limit reached}\n");
870 	p(sctps_datadroprwnd, "\t{:dropped-rwnd-limit/%ju} "
871 	    "{N:/number of in data drop%s due to rwnd limit reached}\n");
872 	p(sctps_ecnereducedcwnd, "\t{:ecn-reduced-cwnd/%ju} "
873 	    "{N:/time%s a ECN reduced the cwnd}\n");
874 	p1a(sctps_vtagexpress, "\t{:v-tag-express-lookup/%ju} "
875 	    "{N:/used express lookup via vtag}\n");
876 	p1a(sctps_vtagbogus, "\t{:v-tag-collision/%ju} "
877 	    "{N:/collision in express lookup}\n");
878 	p(sctps_primary_randry, "\t{:sender-ran-dry/%ju} "
879 	    "{N:/time%s the sender ran dry of user data on primary}\n");
880 	p1a(sctps_cmt_randry, "\t{:cmt-ran-dry/%ju} "
881 	    "{N:/same for above}\n");
882 	p(sctps_slowpath_sack, "\t{:slow-path-sack/%ju} "
883 	    "{N:/sack%s the slow way}\n");
884 	p(sctps_wu_sacks_sent, "\t{:sent-window-update-only-sack/%ju} "
885 	    "{N:/window update only sack%s sent}\n");
886 	p(sctps_sends_with_flags, "\t{:sent-with-sinfo/%ju} "
887 	    "{N:/send%s with sinfo_flags !=0}\n");
888 	p(sctps_sends_with_unord, "\t{:sent-with-unordered/%ju} "
889 	    "{N:/unordered send%s}\n");
890 	p(sctps_sends_with_eof, "\t{:sent-with-eof/%ju} "
891 	    "{N:/send%s with EOF flag set}\n");
892 	p(sctps_sends_with_abort, "\t{:sent-with-abort/%ju} "
893 	    "{N:/send%s with ABORT flag set}\n");
894 	p(sctps_protocol_drain_calls, "\t{:protocol-drain-called/%ju} "
895 	    "{N:/time%s protocol drain called}\n");
896 	p(sctps_protocol_drains_done, "\t{:protocol-drain/%ju} "
897 	    "{N:/time%s we did a protocol drain}\n");
898 	p(sctps_read_peeks, "\t{:read-with-peek/%ju} "
899 	    "{N:/time%s recv was called with peek}\n");
900 	p(sctps_cached_chk, "\t{:cached-chunks/%ju} "
901 	    "{N:/cached chunk%s used}\n");
902 	p1a(sctps_cached_strmoq, "\t{:cached-output-queue-used/%ju} "
903 	    "{N:/cached stream oq's used}\n");
904 	p(sctps_left_abandon, "\t{:messages-abandoned/%ju} "
905 	    "{N:/unread message%s abandonded by close}\n");
906 	p1a(sctps_send_burst_avoid, "\t{:send-burst-avoidance/%ju} "
907 	    "{N:/send burst avoidance, already max burst inflight to net}\n");
908 	p1a(sctps_send_cwnd_avoid, "\t{:send-cwnd-avoidance/%ju} "
909 	    "{N:/send cwnd full avoidance, already max burst inflight "
910 	    "to net}\n");
911 	p(sctps_fwdtsn_map_over, "\t{:tsn-map-overruns/%ju} "
912 	   "{N:/number of map array over-run%s via fwd-tsn's}\n");
913 
914 #undef p
915 #undef p1a
916 	xo_close_container(name);
917 }
918 
919 #endif /* SCTP */
920