sctp.c revision 293307
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2001-2007, by Weongyo Jeong. All rights reserved.
31541Srgrimes * Copyright (c) 2011, by Michael Tuexen. All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions are met:
71541Srgrimes *
81541Srgrimes * a) Redistributions of source code must retain the above copyright notice,
91541Srgrimes *   this list of conditions and the following disclaimer.
101541Srgrimes *
111541Srgrimes * b) Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in
131541Srgrimes *   the documentation and/or other materials provided with the distribution.
141541Srgrimes *
151541Srgrimes * c) Neither the name of Cisco Systems, Inc. nor the names of its
161541Srgrimes *    contributors may be used to endorse or promote products derived
171541Srgrimes *    from this software without specific prior written permission.
181541Srgrimes *
191541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
201541Srgrimes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
211541Srgrimes * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
231541Srgrimes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241541Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251541Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261541Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271541Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281541Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
291541Srgrimes * THE POSSIBILITY OF SUCH DAMAGE.
301541Srgrimes */
311541Srgrimes
321541Srgrimes#if 0
3314478Shsu#ifndef lint
3429683Sgibbsstatic char sccsid[] = "@(#)sctp.c	0.1 (Berkeley) 4/18/2007";
351541Srgrimes#endif /* not lint */
361541Srgrimes#endif
3718444Sbde
385052Sbde#include <sys/cdefs.h>
392165Spaul__FBSDID("$FreeBSD: stable/10/usr.bin/netstat/sctp.c 293307 2016-01-07 07:21:37Z markj $");
4018444Sbde
4118444Sbde#include <sys/param.h>
4218444Sbde#include <sys/queue.h>
4318429Sbde#include <sys/types.h>
4418429Sbde#include <sys/socket.h>
451541Srgrimes#include <sys/socketvar.h>
461541Srgrimes#include <sys/sysctl.h>
471541Srgrimes#include <sys/protosw.h>
481541Srgrimes
491541Srgrimes#include <netinet/in.h>
501541Srgrimes#include <netinet/sctp.h>
511541Srgrimes#include <netinet/sctp_constants.h>
521541Srgrimes#include <arpa/inet.h>
531541Srgrimes
541541Srgrimes#include <err.h>
551541Srgrimes#include <errno.h>
561541Srgrimes#include <libutil.h>
571541Srgrimes#include <netdb.h>
581541Srgrimes#include <stdint.h>
594027Sphk#include <stdio.h>
601541Srgrimes#include <stdlib.h>
611541Srgrimes#include <string.h>
621541Srgrimes#include <unistd.h>
631541Srgrimes#include "netstat.h"
641541Srgrimes
651541Srgrimes#ifdef SCTP
661541Srgrimes
671541Srgrimesstatic void sctp_statesprint(uint32_t state);
681541Srgrimes
691541Srgrimes#define	NETSTAT_SCTP_STATES_CLOSED		0x0
701541Srgrimes#define	NETSTAT_SCTP_STATES_BOUND		0x1
711541Srgrimes#define	NETSTAT_SCTP_STATES_LISTEN		0x2
7214478Shsu#define	NETSTAT_SCTP_STATES_COOKIE_WAIT		0x3
731541Srgrimes#define	NETSTAT_SCTP_STATES_COOKIE_ECHOED	0x4
741541Srgrimes#define	NETSTAT_SCTP_STATES_ESTABLISHED		0x5
751541Srgrimes#define	NETSTAT_SCTP_STATES_SHUTDOWN_SENT	0x6
761541Srgrimes#define	NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED	0x7
775052Sbde#define	NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT	0x8
785052Sbde#define	NETSTAT_SCTP_STATES_SHUTDOWN_PENDING	0x9
795052Sbde
801541Srgrimesconst char *sctpstates[] = {
811541Srgrimes	"CLOSED",
821541Srgrimes	"BOUND",
8314478Shsu	"LISTEN",
8414478Shsu	"COOKIE_WAIT",
8514478Shsu	"COOKIE_ECHOED",
8614478Shsu	"ESTABLISHED",
8714478Shsu	"SHUTDOWN_SENT",
8814478Shsu	"SHUTDOWN_RECEIVED",
891541Srgrimes	"SHUTDOWN_ACK_SENT",
901541Srgrimes	"SHUTDOWN_PENDING"
911541Srgrimes};
921541Srgrimes
931541SrgrimesLIST_HEAD(xladdr_list, xladdr_entry) xladdr_head;
941541Srgrimesstruct xladdr_entry {
951541Srgrimes	struct xsctp_laddr *xladdr;
961541Srgrimes	LIST_ENTRY(xladdr_entry) xladdr_entries;
9714478Shsu};
981541Srgrimes
991541SrgrimesLIST_HEAD(xraddr_list, xraddr_entry) xraddr_head;
1008876Srgrimesstruct xraddr_entry {
1011541Srgrimes        struct xsctp_raddr *xraddr;
1021541Srgrimes        LIST_ENTRY(xraddr_entry) xraddr_entries;
1031541Srgrimes};
1048876Srgrimes
1058876Srgrimes/*
1061541Srgrimes * Construct an Internet address representation.
1071541Srgrimes * If numeric_addr has been supplied, give
1081541Srgrimes * numeric value, otherwise try for symbolic name.
1091541Srgrimes */
11014478Shsu#ifdef INET
1111541Srgrimesstatic char *
11214478Shsuinetname(struct in_addr *inp)
11314478Shsu{
11414478Shsu	char *cp;
11514478Shsu	static char line[MAXHOSTNAMELEN];
11614478Shsu	struct hostent *hp;
11714478Shsu	struct netent *np;
11814478Shsu
1191541Srgrimes	cp = 0;
12014478Shsu	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
12114478Shsu		int net = inet_netof(*inp);
12214478Shsu		int lna = inet_lnaof(*inp);
12314478Shsu
1241541Srgrimes		if (lna == INADDR_ANY) {
12514478Shsu			np = getnetbyaddr(net, AF_INET);
12614478Shsu			if (np)
1271541Srgrimes				cp = np->n_name;
12814478Shsu		}
12914478Shsu		if (cp == 0) {
1301541Srgrimes			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
13114478Shsu			if (hp) {
1321541Srgrimes				cp = hp->h_name;
1331541Srgrimes				trimdomain(cp, strlen(cp));
1341541Srgrimes			}
1351541Srgrimes		}
1361541Srgrimes	}
13714478Shsu	if (inp->s_addr == INADDR_ANY)
13814478Shsu		strcpy(line, "*");
13914478Shsu	else if (cp) {
14014478Shsu		strlcpy(line, cp, sizeof(line));
14114478Shsu	} else {
14214478Shsu		inp->s_addr = ntohl(inp->s_addr);
14314478Shsu#define	C(x)	((u_int)((x) & 0xff))
14414478Shsu		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
14514478Shsu		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
14614478Shsu		inp->s_addr = htonl(inp->s_addr);
14714478Shsu	}
14814478Shsu	return (line);
1491541Srgrimes}
15014478Shsu#endif
15114478Shsu
15214478Shsu#ifdef INET6
15314478Shsustatic char ntop_buf[INET6_ADDRSTRLEN];
15414478Shsu
15514478Shsustatic char *
15614478Shsuinet6name(struct in6_addr *in6p)
1571541Srgrimes{
15814478Shsu	char *cp;
1591541Srgrimes	static char line[50];
16014478Shsu	struct hostent *hp;
16114478Shsu	static char domain[MAXHOSTNAMELEN];
16214478Shsu	static int first = 1;
1631541Srgrimes
1641541Srgrimes	if (first && !numeric_addr) {
16514478Shsu		first = 0;
16614478Shsu		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
16714478Shsu		    (cp = strchr(domain, '.')))
1681541Srgrimes			(void) strcpy(domain, cp + 1);
16914478Shsu		else
17014478Shsu			domain[0] = 0;
17114478Shsu	}
17214478Shsu	cp = 0;
17314478Shsu	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1741541Srgrimes		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
17514478Shsu		if (hp) {
17614478Shsu			if ((cp = strchr(hp->h_name, '.')) &&
1771541Srgrimes			    !strcmp(cp + 1, domain))
1781541Srgrimes				*cp = 0;
1791541Srgrimes			cp = hp->h_name;
1801541Srgrimes		}
1811541Srgrimes	}
1821541Srgrimes	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1831541Srgrimes		strcpy(line, "*");
1841541Srgrimes	else if (cp)
1851541Srgrimes		strcpy(line, cp);
1861541Srgrimes	else
1871541Srgrimes		sprintf(line, "%s",
1881541Srgrimes			inet_ntop(AF_INET6, (void *)in6p, ntop_buf,
1891541Srgrimes				sizeof(ntop_buf)));
1901541Srgrimes	return (line);
1911541Srgrimes}
1921541Srgrimes#endif
1931541Srgrimes
1941541Srgrimesstatic void
1951541Srgrimessctp_print_address(union sctp_sockstore *address, int port, int num_port)
1961541Srgrimes{
1971541Srgrimes	struct servent *sp = 0;
1981541Srgrimes	char line[80], *cp;
1991541Srgrimes	int width;
2001541Srgrimes
2011541Srgrimes	switch (address->sa.sa_family) {
2021541Srgrimes#ifdef INET
2031541Srgrimes	case AF_INET:
2041541Srgrimes		sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr));
20515571Sasami		break;
2061541Srgrimes#endif
20716363Sasami#ifdef INET6
20816363Sasami	case AF_INET6:
20916363Sasami		sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr));
21016363Sasami		break;
2111541Srgrimes#endif
2121541Srgrimes	default:
2131541Srgrimes		sprintf(line, "%.*s.", Wflag ? 39 : 16, "");
2141541Srgrimes		break;
2151541Srgrimes	}
2161541Srgrimes	cp = strchr(line, '\0');
2171541Srgrimes	if (!num_port && port)
2181541Srgrimes		sp = getservbyport((int)port, "sctp");
2191541Srgrimes	if (sp || port == 0)
2201541Srgrimes		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
2211541Srgrimes	else
2221541Srgrimes		sprintf(cp, "%d ", ntohs((u_short)port));
2231541Srgrimes	width = Wflag ? 45 : 22;
22415571Sasami	printf("%-*.*s ", width, width, line);
22514478Shsu}
2261541Srgrimes
2271541Srgrimesstatic int
2281541Srgrimessctp_skip_xinpcb_ifneed(char *buf, const size_t buflen, size_t *offset)
2291541Srgrimes{
2301541Srgrimes	int exist_tcb = 0;
2311541Srgrimes	struct xsctp_tcb *xstcb;
2321541Srgrimes	struct xsctp_raddr *xraddr;
2331541Srgrimes	struct xsctp_laddr *xladdr;
2341541Srgrimes
2351541Srgrimes	while (*offset < buflen) {
2361541Srgrimes		xladdr = (struct xsctp_laddr *)(buf + *offset);
2371541Srgrimes		*offset += sizeof(struct xsctp_laddr);
2381541Srgrimes		if (xladdr->last == 1)
2391541Srgrimes			break;
2401541Srgrimes	}
2411541Srgrimes
2421541Srgrimes	while (*offset < buflen) {
2431541Srgrimes		xstcb = (struct xsctp_tcb *)(buf + *offset);
2441541Srgrimes		*offset += sizeof(struct xsctp_tcb);
2451541Srgrimes		if (xstcb->last == 1)
2461541Srgrimes			break;
2471541Srgrimes
2481541Srgrimes		exist_tcb = 1;
2491541Srgrimes
2501541Srgrimes		while (*offset < buflen) {
2511541Srgrimes			xladdr = (struct xsctp_laddr *)(buf + *offset);
2521541Srgrimes			*offset += sizeof(struct xsctp_laddr);
2531541Srgrimes			if (xladdr->last == 1)
2541541Srgrimes				break;
2551541Srgrimes		}
2561541Srgrimes
2571541Srgrimes		while (*offset < buflen) {
2581541Srgrimes			xraddr = (struct xsctp_raddr *)(buf + *offset);
2591541Srgrimes			*offset += sizeof(struct xsctp_raddr);
2601541Srgrimes			if (xraddr->last == 1)
2611541Srgrimes				break;
2621541Srgrimes		}
2631541Srgrimes	}
2641541Srgrimes
2651541Srgrimes	/*
26614478Shsu	 * If Lflag is set, we don't care about the return value.
2671541Srgrimes	 */
2681541Srgrimes	if (Lflag)
2691541Srgrimes		return 0;
2701541Srgrimes
2711541Srgrimes	return exist_tcb;
2721541Srgrimes}
2731541Srgrimes
2741541Srgrimesstatic void
2751541Srgrimessctp_process_tcb(struct xsctp_tcb *xstcb,
2761541Srgrimes    char *buf, const size_t buflen, size_t *offset, int *indent)
2771541Srgrimes{
2781541Srgrimes	int i, xl_total = 0, xr_total = 0, x_max;
2791541Srgrimes	struct xsctp_raddr *xraddr;
2801541Srgrimes	struct xsctp_laddr *xladdr;
2811541Srgrimes	struct xladdr_entry *prev_xl = NULL, *xl = NULL, *xl_tmp;
2821541Srgrimes	struct xraddr_entry *prev_xr = NULL, *xr = NULL, *xr_tmp;
2831541Srgrimes
2841541Srgrimes	LIST_INIT(&xladdr_head);
2851541Srgrimes	LIST_INIT(&xraddr_head);
2861541Srgrimes
2871541Srgrimes	/*
2881541Srgrimes	 * Make `struct xladdr_list' list and `struct xraddr_list' list
2891541Srgrimes	 * to handle the address flexibly.
2901541Srgrimes	 */
2911541Srgrimes	while (*offset < buflen) {
2921541Srgrimes		xladdr = (struct xsctp_laddr *)(buf + *offset);
2931541Srgrimes		*offset += sizeof(struct xsctp_laddr);
2941541Srgrimes		if (xladdr->last == 1)
2951541Srgrimes			break;
2961541Srgrimes
2971541Srgrimes		prev_xl = xl;
2981541Srgrimes		xl = malloc(sizeof(struct xladdr_entry));
2991541Srgrimes		if (xl == NULL) {
3001541Srgrimes			warnx("malloc %lu bytes",
3011541Srgrimes			    (u_long)sizeof(struct xladdr_entry));
30214478Shsu			goto out;
30314478Shsu		}
3041541Srgrimes		xl->xladdr = xladdr;
3051541Srgrimes		if (prev_xl == NULL)
3061541Srgrimes			LIST_INSERT_HEAD(&xladdr_head, xl, xladdr_entries);
3071541Srgrimes		else
3081541Srgrimes			LIST_INSERT_AFTER(prev_xl, xl, xladdr_entries);
3091541Srgrimes		xl_total++;
3101541Srgrimes	}
3111541Srgrimes
3121541Srgrimes	while (*offset < buflen) {
3131541Srgrimes		xraddr = (struct xsctp_raddr *)(buf + *offset);
31414478Shsu		*offset += sizeof(struct xsctp_raddr);
31514478Shsu		if (xraddr->last == 1)
3161541Srgrimes			break;
3171541Srgrimes
3181541Srgrimes		prev_xr = xr;
3191541Srgrimes		xr = malloc(sizeof(struct xraddr_entry));
3201541Srgrimes		if (xr == NULL) {
3211541Srgrimes			warnx("malloc %lu bytes",
3221549Srgrimes			    (u_long)sizeof(struct xraddr_entry));
3231549Srgrimes			goto out;
32416363Sasami		}
32516363Sasami		xr->xraddr = xraddr;
32616363Sasami		if (prev_xr == NULL)
32716363Sasami			LIST_INSERT_HEAD(&xraddr_head, xr, xraddr_entries);
32816363Sasami		else
32916363Sasami			LIST_INSERT_AFTER(prev_xr, xr, xraddr_entries);
33016363Sasami		xr_total++;
33116363Sasami	}
33216363Sasami
33316363Sasami	/*
33416363Sasami	 * Let's print the address infos.
33516363Sasami	 */
33616363Sasami	xl = LIST_FIRST(&xladdr_head);
33716363Sasami	xr = LIST_FIRST(&xraddr_head);
33816363Sasami	x_max = (xl_total > xr_total) ? xl_total : xr_total;
33916363Sasami	for (i = 0; i < x_max; i++) {
34016363Sasami		if (((*indent == 0) && i > 0) || *indent > 0)
34116363Sasami			printf("%-12s ", " ");
34216363Sasami
34316363Sasami		if (xl != NULL) {
34416363Sasami			sctp_print_address(&(xl->xladdr->address),
34516363Sasami			    htons(xstcb->local_port), numeric_port);
34616363Sasami		} else {
34716363Sasami			if (Wflag) {
34816363Sasami				printf("%-45s ", " ");
34916363Sasami			} else {
35016363Sasami				printf("%-22s ", " ");
35116363Sasami			}
3521549Srgrimes		}
3531549Srgrimes
3541549Srgrimes		if (xr != NULL && !Lflag) {
3553742Spaul			sctp_print_address(&(xr->xraddr->address),
3561549Srgrimes			    htons(xstcb->remote_port), numeric_port);
3571549Srgrimes		}
3581549Srgrimes
3591549Srgrimes		if (xl != NULL)
3601549Srgrimes			xl = LIST_NEXT(xl, xladdr_entries);
3611549Srgrimes		if (xr != NULL)
3621549Srgrimes			xr = LIST_NEXT(xr, xraddr_entries);
3631549Srgrimes
3641549Srgrimes		if (i == 0 && !Lflag)
3651549Srgrimes			sctp_statesprint(xstcb->state);
3661549Srgrimes
3671549Srgrimes		if (i < x_max)
3681549Srgrimes			putchar('\n');
36916363Sasami	}
3701549Srgrimes
3711549Srgrimesout:
3721549Srgrimes	/*
3731549Srgrimes	 * Free the list which be used to handle the address.
3741541Srgrimes	 */
3751541Srgrimes	xl = LIST_FIRST(&xladdr_head);
3761541Srgrimes	while (xl != NULL) {
3771541Srgrimes		xl_tmp = LIST_NEXT(xl, xladdr_entries);
3781541Srgrimes		free(xl);
3791541Srgrimes		xl = xl_tmp;
3801541Srgrimes	}
3811541Srgrimes
3821541Srgrimes	xr = LIST_FIRST(&xraddr_head);
3831541Srgrimes	while (xr != NULL) {
3841541Srgrimes		xr_tmp = LIST_NEXT(xr, xraddr_entries);
3851541Srgrimes		free(xr);
3861541Srgrimes		xr = xr_tmp;
3871541Srgrimes	}
3881541Srgrimes}
3891541Srgrimes
3901541Srgrimesstatic void
3911541Srgrimessctp_process_inpcb(struct xsctp_inpcb *xinpcb,
3928552Sdg    char *buf, const size_t buflen, size_t *offset)
3931541Srgrimes{
3945052Sbde	int indent = 0, xladdr_total = 0, is_listening = 0;
3955052Sbde	static int first = 1;
3965052Sbde	const char *tname, *pname;
3975052Sbde	struct xsctp_tcb *xstcb;
3985052Sbde	struct xsctp_laddr *xladdr;
3995052Sbde	size_t offset_laddr;
4008876Srgrimes	int process_closed;
4015052Sbde
4025052Sbde	if (xinpcb->maxqlen > 0)
4035052Sbde		is_listening = 1;
4048193Sjulian
4058193Sjulian	if (first) {
4068193Sjulian		if (!Lflag) {
4078193Sjulian			printf("Active SCTP associations");
4088193Sjulian			if (aflag)
4098193Sjulian				printf(" (including servers)");
4108193Sjulian		} else
41121986Sdg			printf("Current listen queue sizes (qlen/maxqlen)");
4128193Sjulian		putchar('\n');
4138193Sjulian		if (Lflag)
4145052Sbde			printf("%-6.6s %-5.5s %-8.8s %-22.22s\n",
4155052Sbde			    "Proto", "Type", "Listen", "Local Address");
4165052Sbde		else
4175052Sbde			if (Wflag)
4185052Sbde				printf("%-6.6s %-5.5s %-45.45s %-45.45s %s\n",
4195052Sbde				    "Proto", "Type",
42021986Sdg				    "Local Address", "Foreign Address",
42121986Sdg				    "(state)");
4221541Srgrimes			else
4233940Sjkh				printf("%-6.6s %-5.5s %-22.22s %-22.22s %s\n",
4244462Sbde				    "Proto", "Type",
42512478Sbde				    "Local Address", "Foreign Address",
42612478Sbde				    "(state)");
4277090Sbde		first = 0;
4287090Sbde	}
4295052Sbde	xladdr = (struct xsctp_laddr *)(buf + *offset);
4305052Sbde	if (Lflag && !is_listening) {
4313940Sjkh		sctp_skip_xinpcb_ifneed(buf, buflen, offset);
4325052Sbde		return;
43318429Sbde	}
43410823Sbde
43529683Sgibbs	if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
4365052Sbde		/* Can't distinguish between sctp46 and sctp6 */
4375052Sbde		pname = "sctp46";
43818429Sbde	} else {
4395052Sbde		pname = "sctp4";
4403940Sjkh	}
4415052Sbde
4425052Sbde	if (xinpcb->flags & SCTP_PCB_FLAGS_TCPTYPE)
4435052Sbde		tname = "1to1";
4445052Sbde	else if (xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE)
44518444Sbde		tname = "1toN";
4461541Srgrimes	else
4471541Srgrimes		tname = "????";
4481541Srgrimes
4491541Srgrimes	if (Lflag) {
4502165Spaul		char buf1[9];
4515052Sbde
452		snprintf(buf1, 9, "%hu/%hu", xinpcb->qlen, xinpcb->maxqlen);
453		printf("%-6.6s %-5.5s ", pname, tname);
454		printf("%-8.8s ", buf1);
455	}
456
457	offset_laddr = *offset;
458	process_closed = 0;
459retry:
460	while (*offset < buflen) {
461		xladdr = (struct xsctp_laddr *)(buf + *offset);
462		*offset += sizeof(struct xsctp_laddr);
463		if (xladdr->last) {
464			if (aflag && !Lflag && (xladdr_total == 0) && process_closed) {
465				printf("%-6.6s %-5.5s ", pname, tname);
466				if (Wflag) {
467					printf("%-91.91s CLOSED", " ");
468				} else {
469					printf("%-45.45s CLOSED", " ");
470				}
471			}
472			if (process_closed || is_listening) {
473				putchar('\n');
474			}
475			break;
476		}
477
478		if (!Lflag && !is_listening && !process_closed)
479			continue;
480
481		if (xladdr_total == 0) {
482			printf("%-6.6s %-5.5s ", pname, tname);
483		} else {
484			putchar('\n');
485			printf((Lflag) ?
486			    "%-21.21s " : "%-12.12s ", " ");
487		}
488		sctp_print_address(&(xladdr->address),
489		    htons(xinpcb->local_port), numeric_port);
490		if (aflag && !Lflag && xladdr_total == 0) {
491			if (Wflag) {
492				if (process_closed) {
493					printf("%-45.45s CLOSED", " ");
494				} else {
495					printf("%-45.45s LISTEN", " ");
496				}
497			} else {
498				if (process_closed) {
499					printf("%-22.22s CLOSED", " ");
500				} else {
501					printf("%-22.22s LISTEN", " ");
502				}
503			}
504		}
505		xladdr_total++;
506	}
507
508	xstcb = (struct xsctp_tcb *)(buf + *offset);
509	*offset += sizeof(struct xsctp_tcb);
510	if (aflag && (xladdr_total == 0) && xstcb->last && !process_closed) {
511		process_closed = 1;
512		*offset = offset_laddr;
513		goto retry;
514	}
515	while (xstcb->last == 0 && *offset < buflen) {
516		printf("%-6.6s %-5.5s ", pname, tname);
517		sctp_process_tcb(xstcb, buf, buflen, offset, &indent);
518		indent++;
519		xstcb = (struct xsctp_tcb *)(buf + *offset);
520		*offset += sizeof(struct xsctp_tcb);
521	}
522}
523
524/*
525 * Print a summary of SCTP connections related to an Internet
526 * protocol.
527 */
528void
529sctp_protopr(u_long off __unused,
530    const char *name __unused, int af1 __unused, int proto)
531{
532	char *buf;
533	const char *mibvar = "net.inet.sctp.assoclist";
534	size_t offset = 0;
535	size_t len = 0;
536	struct xsctp_inpcb *xinpcb;
537
538	if (proto != IPPROTO_SCTP)
539		return;
540
541	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
542		if (errno != ENOENT)
543			warn("sysctl: %s", mibvar);
544		return;
545	}
546	if ((buf = malloc(len)) == 0) {
547		warnx("malloc %lu bytes", (u_long)len);
548		return;
549	}
550	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
551		warn("sysctl: %s", mibvar);
552		free(buf);
553		return;
554	}
555
556	xinpcb = (struct xsctp_inpcb *)(buf + offset);
557	offset += sizeof(struct xsctp_inpcb);
558	while (xinpcb->last == 0 && offset < len) {
559		sctp_process_inpcb(xinpcb, buf, (const size_t)len,
560		    &offset);
561
562		xinpcb = (struct xsctp_inpcb *)(buf + offset);
563		offset += sizeof(struct xsctp_inpcb);
564	}
565
566	free(buf);
567}
568
569static void
570sctp_statesprint(uint32_t state)
571{
572	int idx;
573
574	switch (state) {
575	case SCTP_STATE_COOKIE_WAIT:
576		idx = NETSTAT_SCTP_STATES_COOKIE_WAIT;
577		break;
578	case SCTP_STATE_COOKIE_ECHOED:
579		idx = NETSTAT_SCTP_STATES_COOKIE_ECHOED;
580		break;
581	case SCTP_STATE_OPEN:
582		idx = NETSTAT_SCTP_STATES_ESTABLISHED;
583		break;
584	case SCTP_STATE_SHUTDOWN_SENT:
585		idx = NETSTAT_SCTP_STATES_SHUTDOWN_SENT;
586		break;
587	case SCTP_STATE_SHUTDOWN_RECEIVED:
588		idx = NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED;
589		break;
590	case SCTP_STATE_SHUTDOWN_ACK_SENT:
591		idx = NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT;
592		break;
593	case SCTP_STATE_SHUTDOWN_PENDING:
594		idx = NETSTAT_SCTP_STATES_SHUTDOWN_PENDING;
595		break;
596	default:
597		printf("UNKNOWN 0x%08x", state);
598		return;
599	}
600
601	printf("%s", sctpstates[idx]);
602}
603
604/*
605 * Dump SCTP statistics structure.
606 */
607void
608sctp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
609{
610	struct sctpstat sctpstat;
611
612	if (fetch_stats("net.inet.sctp.stats", off, &sctpstat,
613	    sizeof(sctpstat), kread) != 0)
614		return;
615
616	printf ("%s:\n", name);
617
618#define	p(f, m) if (sctpstat.f || sflag <= 1) \
619    printf(m, (uintmax_t)sctpstat.f, plural(sctpstat.f))
620#define	p1a(f, m) if (sctpstat.f || sflag <= 1) \
621    printf(m, (uintmax_t)sctpstat.f)
622
623	/*
624	 * input statistics
625	 */
626	p(sctps_recvpackets, "\t%ju input packet%s\n");
627	p(sctps_recvdatagrams, "\t\t%ju datagram%s\n");
628	p(sctps_recvpktwithdata, "\t\t%ju packet%s that had data\n");
629	p(sctps_recvsacks, "\t\t%ju input SACK chunk%s\n");
630	p(sctps_recvdata, "\t\t%ju input DATA chunk%s\n");
631	p(sctps_recvdupdata, "\t\t%ju duplicate DATA chunk%s\n");
632	p(sctps_recvheartbeat, "\t\t%ju input HB chunk%s\n");
633	p(sctps_recvheartbeatack, "\t\t%ju HB-ACK chunk%s\n");
634	p(sctps_recvecne, "\t\t%ju input ECNE chunk%s\n");
635	p(sctps_recvauth, "\t\t%ju input AUTH chunk%s\n");
636	p(sctps_recvauthmissing, "\t\t%ju chunk%s missing AUTH\n");
637	p(sctps_recvivalhmacid, "\t\t%ju invalid HMAC id%s received\n");
638	p(sctps_recvivalkeyid, "\t\t%ju invalid secret id%s received\n");
639	p1a(sctps_recvauthfailed, "\t\t%ju auth failed\n");
640	p1a(sctps_recvexpress, "\t\t%ju fast path receives all one chunk\n");
641	p1a(sctps_recvexpressm, "\t\t%ju fast path multi-part data\n");
642
643	/*
644	 * output statistics
645	 */
646	p(sctps_sendpackets, "\t%ju output packet%s\n");
647	p(sctps_sendsacks, "\t\t%ju output SACK%s\n");
648	p(sctps_senddata, "\t\t%ju output DATA chunk%s\n");
649	p(sctps_sendretransdata, "\t\t%ju retransmitted DATA chunk%s\n");
650	p(sctps_sendfastretrans, "\t\t%ju fast retransmitted DATA chunk%s\n");
651	p(sctps_sendmultfastretrans, "\t\t%ju FR'%s that happened more "
652	    "than once to same chunk\n");
653	p(sctps_sendheartbeat, "\t\t%ju output HB chunk%s\n");
654	p(sctps_sendecne, "\t\t%ju output ECNE chunk%s\n");
655	p(sctps_sendauth, "\t\t%ju output AUTH chunk%s\n");
656	p1a(sctps_senderrors, "\t\t%ju ip_output error counter\n");
657
658	/*
659	 * PCKDROPREP statistics
660	 */
661	printf("\tPacket drop statistics:\n");
662	p1a(sctps_pdrpfmbox, "\t\t%ju from middle box\n");
663	p1a(sctps_pdrpfehos, "\t\t%ju from end host\n");
664	p1a(sctps_pdrpmbda, "\t\t%ju with data\n");
665	p1a(sctps_pdrpmbct, "\t\t%ju non-data, non-endhost\n");
666	p1a(sctps_pdrpbwrpt, "\t\t%ju non-endhost, bandwidth rep only\n");
667	p1a(sctps_pdrpcrupt, "\t\t%ju not enough for chunk header\n");
668	p1a(sctps_pdrpnedat, "\t\t%ju not enough data to confirm\n");
669	p1a(sctps_pdrppdbrk, "\t\t%ju where process_chunk_drop said break\n");
670	p1a(sctps_pdrptsnnf, "\t\t%ju failed to find TSN\n");
671	p1a(sctps_pdrpdnfnd, "\t\t%ju attempt reverse TSN lookup\n");
672	p1a(sctps_pdrpdiwnp, "\t\t%ju e-host confirms zero-rwnd\n");
673	p1a(sctps_pdrpdizrw, "\t\t%ju midbox confirms no space\n");
674	p1a(sctps_pdrpbadd, "\t\t%ju data did not match TSN\n");
675	p(sctps_pdrpmark, "\t\t%ju TSN'%s marked for Fast Retran\n");
676
677	/*
678	 * Timeouts
679	 */
680	printf("\tTimeouts:\n");
681	p(sctps_timoiterator, "\t\t%ju iterator timer%s fired\n");
682	p(sctps_timodata, "\t\t%ju T3 data time out%s\n");
683	p(sctps_timowindowprobe, "\t\t%ju window probe (T3) timer%s fired\n");
684	p(sctps_timoinit, "\t\t%ju INIT timer%s fired\n");
685	p(sctps_timosack, "\t\t%ju sack timer%s fired\n");
686	p(sctps_timoshutdown, "\t\t%ju shutdown timer%s fired\n");
687	p(sctps_timoheartbeat, "\t\t%ju heartbeat timer%s fired\n");
688	p1a(sctps_timocookie, "\t\t%ju a cookie timeout fired\n");
689	p1a(sctps_timosecret, "\t\t%ju an endpoint changed its cookie"
690	    "secret\n");
691	p(sctps_timopathmtu, "\t\t%ju PMTU timer%s fired\n");
692	p(sctps_timoshutdownack, "\t\t%ju shutdown ack timer%s fired\n");
693	p(sctps_timoshutdownguard, "\t\t%ju shutdown guard timer%s fired\n");
694	p(sctps_timostrmrst, "\t\t%ju stream reset timer%s fired\n");
695	p(sctps_timoearlyfr, "\t\t%ju early FR timer%s fired\n");
696	p1a(sctps_timoasconf, "\t\t%ju an asconf timer fired\n");
697	p1a(sctps_timoautoclose, "\t\t%ju auto close timer fired\n");
698	p(sctps_timoassockill, "\t\t%ju asoc free timer%s expired\n");
699	p(sctps_timoinpkill, "\t\t%ju inp free timer%s expired\n");
700
701#if 0
702	/*
703	 * Early fast retransmission counters
704	 */
705	p(sctps_earlyfrstart, "\t%ju TODO:sctps_earlyfrstart\n");
706	p(sctps_earlyfrstop, "\t%ju TODO:sctps_earlyfrstop\n");
707	p(sctps_earlyfrmrkretrans, "\t%ju TODO:sctps_earlyfrmrkretrans\n");
708	p(sctps_earlyfrstpout, "\t%ju TODO:sctps_earlyfrstpout\n");
709	p(sctps_earlyfrstpidsck1, "\t%ju TODO:sctps_earlyfrstpidsck1\n");
710	p(sctps_earlyfrstpidsck2, "\t%ju TODO:sctps_earlyfrstpidsck2\n");
711	p(sctps_earlyfrstpidsck3, "\t%ju TODO:sctps_earlyfrstpidsck3\n");
712	p(sctps_earlyfrstpidsck4, "\t%ju TODO:sctps_earlyfrstpidsck4\n");
713	p(sctps_earlyfrstrid, "\t%ju TODO:sctps_earlyfrstrid\n");
714	p(sctps_earlyfrstrout, "\t%ju TODO:sctps_earlyfrstrout\n");
715	p(sctps_earlyfrstrtmr, "\t%ju TODO:sctps_earlyfrstrtmr\n");
716#endif
717
718	/*
719	 * Others
720	 */
721	p1a(sctps_hdrops, "\t%ju packet shorter than header\n");
722	p1a(sctps_badsum, "\t%ju checksum error\n");
723	p1a(sctps_noport, "\t%ju no endpoint for port\n");
724	p1a(sctps_badvtag, "\t%ju bad v-tag\n");
725	p1a(sctps_badsid, "\t%ju bad SID\n");
726	p1a(sctps_nomem, "\t%ju no memory\n");
727	p1a(sctps_fastretransinrtt, "\t%ju number of multiple FR in a RTT "
728	    "window\n");
729#if 0
730	p(sctps_markedretrans, "\t%ju TODO:sctps_markedretrans\n");
731#endif
732	p1a(sctps_naglesent, "\t%ju RFC813 allowed sending\n");
733	p1a(sctps_naglequeued, "\t%ju RFC813 does not allow sending\n");
734	p1a(sctps_maxburstqueued, "\t%ju times max burst prohibited sending\n");
735	p1a(sctps_ifnomemqueued, "\t%ju look ahead tells us no memory in "
736	    "interface\n");
737	p(sctps_windowprobed, "\t%ju number%s of window probes sent\n");
738	p(sctps_lowlevelerr, "\t%ju time%s an output error to clamp "
739	    "down on next user send\n");
740	p(sctps_lowlevelerrusr, "\t%ju time%s sctp_senderrors were "
741	    "caused from a user\n");
742	p(sctps_datadropchklmt, "\t%ju number of in data drop%s due to "
743	    "chunk limit reached\n");
744	p(sctps_datadroprwnd, "\t%ju number of in data drop%s due to rwnd "
745	    "limit reached\n");
746	p(sctps_ecnereducedcwnd, "\t%ju time%s a ECN reduced "
747	    "the cwnd\n");
748	p1a(sctps_vtagexpress, "\t%ju used express lookup via vtag\n");
749	p1a(sctps_vtagbogus, "\t%ju collision in express lookup\n");
750	p(sctps_primary_randry, "\t%ju time%s the sender ran dry "
751	    "of user data on primary\n");
752	p1a(sctps_cmt_randry, "\t%ju same for above\n");
753	p(sctps_slowpath_sack, "\t%ju sack%s the slow way\n");
754	p(sctps_wu_sacks_sent, "\t%ju window update only sack%s sent\n");
755	p(sctps_sends_with_flags, "\t%ju send%s with sinfo_flags !=0\n");
756	p(sctps_sends_with_unord, "\t%ju unordered send%s\n");
757	p(sctps_sends_with_eof, "\t%ju send%s with EOF flag set\n");
758	p(sctps_sends_with_abort, "\t%ju send%s with ABORT flag set\n");
759	p(sctps_protocol_drain_calls, "\t%ju time%s protocol drain called\n");
760	p(sctps_protocol_drains_done, "\t%ju time%s we did a protocol "
761	    "drain\n");
762	p(sctps_read_peeks, "\t%ju time%s recv was called with peek\n");
763	p(sctps_cached_chk, "\t%ju cached chunk%s used\n");
764	p1a(sctps_cached_strmoq, "\t%ju cached stream oq's used\n");
765	p(sctps_left_abandon, "\t%ju unread message%s abandonded by close\n");
766	p1a(sctps_send_burst_avoid, "\t%ju send burst avoidance, already "
767	    "max burst inflight to net\n");
768	p1a(sctps_send_cwnd_avoid, "\t%ju send cwnd full avoidance, already "
769	    "max burst inflight to net\n");
770	p(sctps_fwdtsn_map_over, "\t%ju number of map array over-run%s via "
771	    "fwd-tsn's\n");
772
773#undef p
774#undef p1a
775}
776
777#endif /* SCTP */
778