xref: /freebsd/usr.bin/sockstat/main.c (revision d888317796190bec350aea3701b8aed3bfdad4c8)
1ec92e61fSAlan Somers /*-
2ec92e61fSAlan Somers  * SPDX-License-Identifier: BSD-2-Clause
3ec92e61fSAlan Somers  *
4ec92e61fSAlan Somers  * Copyright (c) 2002 Dag-Erling Smørgrav
5ec92e61fSAlan Somers  * All rights reserved.
6ec92e61fSAlan Somers  *
7ec92e61fSAlan Somers  * Redistribution and use in source and binary forms, with or without
8ec92e61fSAlan Somers  * modification, are permitted provided that the following conditions
9ec92e61fSAlan Somers  * are met:
10ec92e61fSAlan Somers  * 1. Redistributions of source code must retain the above copyright
11ec92e61fSAlan Somers  *    notice, this list of conditions and the following disclaimer
12ec92e61fSAlan Somers  *    in this position and unchanged.
13ec92e61fSAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
14ec92e61fSAlan Somers  *    notice, this list of conditions and the following disclaimer in the
15ec92e61fSAlan Somers  *    documentation and/or other materials provided with the distribution.
16ec92e61fSAlan Somers  * 3. The name of the author may not be used to endorse or promote products
17ec92e61fSAlan Somers  *    derived from this software without specific prior written permission.
18ec92e61fSAlan Somers  *
19ec92e61fSAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20ec92e61fSAlan Somers  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21ec92e61fSAlan Somers  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22ec92e61fSAlan Somers  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23ec92e61fSAlan Somers  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24ec92e61fSAlan Somers  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25ec92e61fSAlan Somers  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26ec92e61fSAlan Somers  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27ec92e61fSAlan Somers  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28ec92e61fSAlan Somers  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29ec92e61fSAlan Somers  */
30ec92e61fSAlan Somers 
31ec92e61fSAlan Somers #include <sys/param.h>
32ec92e61fSAlan Somers #include <sys/file.h>
33ec92e61fSAlan Somers #include <sys/socket.h>
34ec92e61fSAlan Somers #include <sys/socketvar.h>
35ec92e61fSAlan Somers #include <sys/sysctl.h>
36ec92e61fSAlan Somers #include <sys/jail.h>
37ec92e61fSAlan Somers #include <sys/user.h>
38ec92e61fSAlan Somers #include <sys/queue.h>
39ec92e61fSAlan Somers #include <sys/tree.h>
40ec92e61fSAlan Somers 
41ec92e61fSAlan Somers #include <sys/un.h>
42ec92e61fSAlan Somers #include <sys/unpcb.h>
43ec92e61fSAlan Somers 
44ec92e61fSAlan Somers #include <net/route.h>
45ec92e61fSAlan Somers 
46ec92e61fSAlan Somers #include <netinet/in.h>
47ec92e61fSAlan Somers #include <netinet/in_pcb.h>
48ec92e61fSAlan Somers #include <netinet/sctp.h>
49ec92e61fSAlan Somers #include <netinet/tcp.h>
50ec92e61fSAlan Somers #define TCPSTATES /* load state names */
51ec92e61fSAlan Somers #include <netinet/tcp_fsm.h>
52ec92e61fSAlan Somers #include <netinet/tcp_seq.h>
53ec92e61fSAlan Somers #include <netinet/tcp_var.h>
54ec92e61fSAlan Somers #include <arpa/inet.h>
55ec92e61fSAlan Somers 
56ec92e61fSAlan Somers #include <capsicum_helpers.h>
57ec92e61fSAlan Somers #include <errno.h>
58ec92e61fSAlan Somers #include <inttypes.h>
59ec92e61fSAlan Somers #include <jail.h>
60ec92e61fSAlan Somers #include <netdb.h>
61ec92e61fSAlan Somers #include <pwd.h>
62ec92e61fSAlan Somers #include <stdarg.h>
63ec92e61fSAlan Somers #include <stdbool.h>
64ec92e61fSAlan Somers #include <stdio.h>
65ec92e61fSAlan Somers #include <stdlib.h>
66ec92e61fSAlan Somers #include <string.h>
67ec92e61fSAlan Somers #include <unistd.h>
68ec92e61fSAlan Somers #include <libxo/xo.h>
69ec92e61fSAlan Somers 
70ec92e61fSAlan Somers #include <libcasper.h>
71ec92e61fSAlan Somers #include <casper/cap_net.h>
72ec92e61fSAlan Somers #include <casper/cap_netdb.h>
73ec92e61fSAlan Somers #include <casper/cap_pwd.h>
74ec92e61fSAlan Somers #include <casper/cap_sysctl.h>
75ec92e61fSAlan Somers 
76*d8883177SAlan Somers #include "sockstat.h"
77*d8883177SAlan Somers 
78ec92e61fSAlan Somers #define SOCKSTAT_XO_VERSION "1"
79ec92e61fSAlan Somers #define	sstosin(ss)	((struct sockaddr_in *)(ss))
80ec92e61fSAlan Somers #define	sstosin6(ss)	((struct sockaddr_in6 *)(ss))
81ec92e61fSAlan Somers #define	sstosun(ss)	((struct sockaddr_un *)(ss))
82ec92e61fSAlan Somers #define	sstosa(ss)	((struct sockaddr *)(ss))
83ec92e61fSAlan Somers 
84ec92e61fSAlan Somers static bool	 opt_4;		/* Show IPv4 sockets */
85ec92e61fSAlan Somers static bool	 opt_6;		/* Show IPv6 sockets */
86ec92e61fSAlan Somers static bool	 opt_A;		/* Show kernel address of pcb */
87ec92e61fSAlan Somers static bool	 opt_C;		/* Show congestion control */
88ec92e61fSAlan Somers static bool	 opt_c;		/* Show connected sockets */
89ec92e61fSAlan Somers static bool	 opt_f;		/* Show FIB numbers */
90ec92e61fSAlan Somers static bool	 opt_I;		/* Show spliced socket addresses */
91ec92e61fSAlan Somers static bool	 opt_i;		/* Show inp_gencnt */
92ec92e61fSAlan Somers static int	 opt_j;		/* Show specified jail */
93ec92e61fSAlan Somers static bool	 opt_L;		/* Don't show IPv4 or IPv6 loopback sockets */
94ec92e61fSAlan Somers static bool	 opt_l;		/* Show listening sockets */
95ec92e61fSAlan Somers static bool	 opt_n;		/* Don't resolve UIDs to user names */
96ec92e61fSAlan Somers static bool	 opt_q;		/* Don't show header */
97ec92e61fSAlan Somers static bool	 opt_S;		/* Show protocol stack if applicable */
98ec92e61fSAlan Somers static bool	 opt_s;		/* Show protocol state if applicable */
99ec92e61fSAlan Somers static bool	 opt_U;		/* Show remote UDP encapsulation port number */
100ec92e61fSAlan Somers static bool	 opt_u;		/* Show Unix domain sockets */
101ec92e61fSAlan Somers static u_int	 opt_v;		/* Verbose mode */
102ec92e61fSAlan Somers static bool	 opt_w;		/* Automatically size the columns */
103ec92e61fSAlan Somers static bool	 is_xo_style_encoding;
104ec92e61fSAlan Somers 
105ec92e61fSAlan Somers /*
106ec92e61fSAlan Somers  * Default protocols to use if no -P was defined.
107ec92e61fSAlan Somers  */
108ec92e61fSAlan Somers static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
109ec92e61fSAlan Somers static size_t	   default_numprotos = nitems(default_protos);
110ec92e61fSAlan Somers 
111ec92e61fSAlan Somers static int	*protos;	/* protocols to use */
112ec92e61fSAlan Somers static size_t	 numprotos;	/* allocated size of protos[] */
113ec92e61fSAlan Somers 
114ec92e61fSAlan Somers struct addr {
115ec92e61fSAlan Somers 	union {
116ec92e61fSAlan Somers 		struct sockaddr_storage address;
117ec92e61fSAlan Somers 		struct {	/* unix(4) faddr */
118ec92e61fSAlan Somers 			kvaddr_t conn;
119ec92e61fSAlan Somers 			kvaddr_t firstref;
120ec92e61fSAlan Somers 			kvaddr_t nextref;
121ec92e61fSAlan Somers 		};
122ec92e61fSAlan Somers 	};
123ec92e61fSAlan Somers 	unsigned int encaps_port;
124ec92e61fSAlan Somers 	int state;
125ec92e61fSAlan Somers 	struct addr *next;
126ec92e61fSAlan Somers };
127ec92e61fSAlan Somers 
128ec92e61fSAlan Somers struct sock {
129ec92e61fSAlan Somers 	union {
130ec92e61fSAlan Somers 		RB_ENTRY(sock) socket_tree;	/* tree of pcbs with socket */
131ec92e61fSAlan Somers 		SLIST_ENTRY(sock) socket_list;	/* list of pcbs w/o socket */
132ec92e61fSAlan Somers 	};
133ec92e61fSAlan Somers 	RB_ENTRY(sock) pcb_tree;
134ec92e61fSAlan Somers 	kvaddr_t socket;
135ec92e61fSAlan Somers 	kvaddr_t pcb;
136ec92e61fSAlan Somers 	kvaddr_t splice_socket;
137ec92e61fSAlan Somers 	uint64_t inp_gencnt;
138ec92e61fSAlan Somers 	int shown;
139ec92e61fSAlan Somers 	int vflag;
140ec92e61fSAlan Somers 	int family;
141ec92e61fSAlan Somers 	int proto;
142ec92e61fSAlan Somers 	int state;
143ec92e61fSAlan Somers 	int fibnum;
144ec92e61fSAlan Somers 	const char *protoname;
145ec92e61fSAlan Somers 	char stack[TCP_FUNCTION_NAME_LEN_MAX];
146ec92e61fSAlan Somers 	char cc[TCP_CA_NAME_MAX];
147ec92e61fSAlan Somers 	struct addr *laddr;
148ec92e61fSAlan Somers 	struct addr *faddr;
149ec92e61fSAlan Somers };
150ec92e61fSAlan Somers 
151ec92e61fSAlan Somers static RB_HEAD(socks_t, sock) socks = RB_INITIALIZER(&socks);
152ec92e61fSAlan Somers static int64_t
socket_compare(const struct sock * a,const struct sock * b)153ec92e61fSAlan Somers socket_compare(const struct sock *a, const struct sock *b)
154ec92e61fSAlan Somers {
155ec92e61fSAlan Somers 	return ((int64_t)(a->socket/2 - b->socket/2));
156ec92e61fSAlan Somers }
157ec92e61fSAlan Somers RB_GENERATE_STATIC(socks_t, sock, socket_tree, socket_compare);
158ec92e61fSAlan Somers 
159ec92e61fSAlan Somers static RB_HEAD(pcbs_t, sock) pcbs = RB_INITIALIZER(&pcbs);
160ec92e61fSAlan Somers static int64_t
pcb_compare(const struct sock * a,const struct sock * b)161ec92e61fSAlan Somers pcb_compare(const struct sock *a, const struct sock *b)
162ec92e61fSAlan Somers {
163ec92e61fSAlan Somers         return ((int64_t)(a->pcb/2 - b->pcb/2));
164ec92e61fSAlan Somers }
165ec92e61fSAlan Somers RB_GENERATE_STATIC(pcbs_t, sock, pcb_tree, pcb_compare);
166ec92e61fSAlan Somers 
167ec92e61fSAlan Somers static SLIST_HEAD(, sock) nosocks = SLIST_HEAD_INITIALIZER(&nosocks);
168ec92e61fSAlan Somers 
169ec92e61fSAlan Somers struct file {
170ec92e61fSAlan Somers 	RB_ENTRY(file)	file_tree;
171ec92e61fSAlan Somers 	kvaddr_t	xf_data;
172ec92e61fSAlan Somers 	pid_t	xf_pid;
173ec92e61fSAlan Somers 	uid_t	xf_uid;
174ec92e61fSAlan Somers 	int	xf_fd;
175ec92e61fSAlan Somers };
176ec92e61fSAlan Somers 
177ec92e61fSAlan Somers static RB_HEAD(files_t, file) ftree = RB_INITIALIZER(&ftree);
178ec92e61fSAlan Somers static int64_t
file_compare(const struct file * a,const struct file * b)179ec92e61fSAlan Somers file_compare(const struct file *a, const struct file *b)
180ec92e61fSAlan Somers {
181ec92e61fSAlan Somers 	return ((int64_t)(a->xf_data/2 - b->xf_data/2));
182ec92e61fSAlan Somers }
183ec92e61fSAlan Somers RB_GENERATE_STATIC(files_t, file, file_tree, file_compare);
184ec92e61fSAlan Somers 
185ec92e61fSAlan Somers static struct file *files;
186ec92e61fSAlan Somers static int nfiles;
187ec92e61fSAlan Somers 
188ec92e61fSAlan Somers static cap_channel_t *capnet;
189ec92e61fSAlan Somers static cap_channel_t *capnetdb;
190ec92e61fSAlan Somers static cap_channel_t *capsysctl;
191ec92e61fSAlan Somers static cap_channel_t *cappwd;
192ec92e61fSAlan Somers 
193ec92e61fSAlan Somers static bool
_check_ksize(size_t received_size,size_t expected_size,const char * struct_name)194ec92e61fSAlan Somers _check_ksize(size_t received_size, size_t expected_size, const char *struct_name)
195ec92e61fSAlan Somers {
196ec92e61fSAlan Somers 	if (received_size != expected_size) {
197ec92e61fSAlan Somers 		xo_warnx("%s size mismatch: expected %zd, received %zd",
198ec92e61fSAlan Somers 		    struct_name, expected_size, received_size);
199ec92e61fSAlan Somers 		return false;
200ec92e61fSAlan Somers 	}
201ec92e61fSAlan Somers 	return true;
202ec92e61fSAlan Somers }
203ec92e61fSAlan Somers #define check_ksize(_sz, _struct)	(_check_ksize(_sz, sizeof(_struct), #_struct))
204ec92e61fSAlan Somers 
205ec92e61fSAlan Somers static void
_enforce_ksize(size_t received_size,size_t expected_size,const char * struct_name)206ec92e61fSAlan Somers _enforce_ksize(size_t received_size, size_t expected_size, const char *struct_name)
207ec92e61fSAlan Somers {
208ec92e61fSAlan Somers 	if (received_size != expected_size) {
209ec92e61fSAlan Somers 		xo_errx(1, "fatal: struct %s size mismatch: expected %zd, received %zd",
210ec92e61fSAlan Somers 		    struct_name, expected_size, received_size);
211ec92e61fSAlan Somers 	}
212ec92e61fSAlan Somers }
213ec92e61fSAlan Somers #define enforce_ksize(_sz, _struct)	(_enforce_ksize(_sz, sizeof(_struct), #_struct))
214ec92e61fSAlan Somers 
215ec92e61fSAlan Somers static int
get_proto_type(const char * proto)216ec92e61fSAlan Somers get_proto_type(const char *proto)
217ec92e61fSAlan Somers {
218ec92e61fSAlan Somers 	struct protoent *pent;
219ec92e61fSAlan Somers 
220ec92e61fSAlan Somers 	if (strlen(proto) == 0)
221ec92e61fSAlan Somers 		return (0);
222ec92e61fSAlan Somers 	if (capnetdb != NULL)
223ec92e61fSAlan Somers 		pent = cap_getprotobyname(capnetdb, proto);
224ec92e61fSAlan Somers 	else
225ec92e61fSAlan Somers 		pent = getprotobyname(proto);
226ec92e61fSAlan Somers 	if (pent == NULL) {
227ec92e61fSAlan Somers 		xo_warn("cap_getprotobyname");
228ec92e61fSAlan Somers 		return (-1);
229ec92e61fSAlan Somers 	}
230ec92e61fSAlan Somers 	return (pent->p_proto);
231ec92e61fSAlan Somers }
232ec92e61fSAlan Somers 
233ec92e61fSAlan Somers static void
init_protos(int num)234ec92e61fSAlan Somers init_protos(int num)
235ec92e61fSAlan Somers {
236ec92e61fSAlan Somers 	int proto_count = 0;
237ec92e61fSAlan Somers 
238ec92e61fSAlan Somers 	if (num > 0) {
239ec92e61fSAlan Somers 		proto_count = num;
240ec92e61fSAlan Somers 	} else {
241ec92e61fSAlan Somers 		/* Find the maximum number of possible protocols. */
242ec92e61fSAlan Somers 		while (getprotoent() != NULL)
243ec92e61fSAlan Somers 			proto_count++;
244ec92e61fSAlan Somers 		endprotoent();
245ec92e61fSAlan Somers 	}
246ec92e61fSAlan Somers 
247ec92e61fSAlan Somers 	if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
248ec92e61fSAlan Somers 		xo_err(1, "malloc");
249ec92e61fSAlan Somers 	numprotos = proto_count;
250ec92e61fSAlan Somers }
251ec92e61fSAlan Somers 
252ec92e61fSAlan Somers static int
parse_protos(char * protospec)253ec92e61fSAlan Somers parse_protos(char *protospec)
254ec92e61fSAlan Somers {
255ec92e61fSAlan Somers 	char *prot;
256ec92e61fSAlan Somers 	int proto_type, proto_index;
257ec92e61fSAlan Somers 
258ec92e61fSAlan Somers 	if (protospec == NULL)
259ec92e61fSAlan Somers 		return (-1);
260ec92e61fSAlan Somers 
261ec92e61fSAlan Somers 	init_protos(0);
262ec92e61fSAlan Somers 	proto_index = 0;
263ec92e61fSAlan Somers 	while ((prot = strsep(&protospec, ",")) != NULL) {
264ec92e61fSAlan Somers 		if (strlen(prot) == 0)
265ec92e61fSAlan Somers 			continue;
266ec92e61fSAlan Somers 		proto_type = get_proto_type(prot);
267ec92e61fSAlan Somers 		if (proto_type != -1)
268ec92e61fSAlan Somers 			protos[proto_index++] = proto_type;
269ec92e61fSAlan Somers 	}
270ec92e61fSAlan Somers 	numprotos = proto_index;
271ec92e61fSAlan Somers 	return (proto_index);
272ec92e61fSAlan Somers }
273ec92e61fSAlan Somers 
274ec92e61fSAlan Somers static void
sockaddr(struct sockaddr_storage * ss,int af,void * addr,int port)275ec92e61fSAlan Somers sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
276ec92e61fSAlan Somers {
277ec92e61fSAlan Somers 	struct sockaddr_in *sin4;
278ec92e61fSAlan Somers 	struct sockaddr_in6 *sin6;
279ec92e61fSAlan Somers 
280ec92e61fSAlan Somers 	bzero(ss, sizeof(*ss));
281ec92e61fSAlan Somers 	switch (af) {
282ec92e61fSAlan Somers 	case AF_INET:
283ec92e61fSAlan Somers 		sin4 = sstosin(ss);
284ec92e61fSAlan Somers 		sin4->sin_len = sizeof(*sin4);
285ec92e61fSAlan Somers 		sin4->sin_family = af;
286ec92e61fSAlan Somers 		sin4->sin_port = port;
287ec92e61fSAlan Somers 		sin4->sin_addr = *(struct in_addr *)addr;
288ec92e61fSAlan Somers 		break;
289ec92e61fSAlan Somers 	case AF_INET6:
290ec92e61fSAlan Somers 		sin6 = sstosin6(ss);
291ec92e61fSAlan Somers 		sin6->sin6_len = sizeof(*sin6);
292ec92e61fSAlan Somers 		sin6->sin6_family = af;
293ec92e61fSAlan Somers 		sin6->sin6_port = port;
294ec92e61fSAlan Somers 		sin6->sin6_addr = *(struct in6_addr *)addr;
295ec92e61fSAlan Somers #define	s6_addr16	__u6_addr.__u6_addr16
296ec92e61fSAlan Somers 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
297ec92e61fSAlan Somers 			sin6->sin6_scope_id =
298ec92e61fSAlan Somers 			    ntohs(sin6->sin6_addr.s6_addr16[1]);
299ec92e61fSAlan Somers 			sin6->sin6_addr.s6_addr16[1] = 0;
300ec92e61fSAlan Somers 		}
301ec92e61fSAlan Somers 		break;
302ec92e61fSAlan Somers 	default:
303ec92e61fSAlan Somers 		abort();
304ec92e61fSAlan Somers 	}
305ec92e61fSAlan Somers }
306ec92e61fSAlan Somers 
307ec92e61fSAlan Somers static void
free_socket(struct sock * sock)308ec92e61fSAlan Somers free_socket(struct sock *sock)
309ec92e61fSAlan Somers {
310ec92e61fSAlan Somers 	struct addr *cur, *next;
311ec92e61fSAlan Somers 
312ec92e61fSAlan Somers 	cur = sock->laddr;
313ec92e61fSAlan Somers 	while (cur != NULL) {
314ec92e61fSAlan Somers 		next = cur->next;
315ec92e61fSAlan Somers 		free(cur);
316ec92e61fSAlan Somers 		cur = next;
317ec92e61fSAlan Somers 	}
318ec92e61fSAlan Somers 	cur = sock->faddr;
319ec92e61fSAlan Somers 	while (cur != NULL) {
320ec92e61fSAlan Somers 		next = cur->next;
321ec92e61fSAlan Somers 		free(cur);
322ec92e61fSAlan Somers 		cur = next;
323ec92e61fSAlan Somers 	}
324ec92e61fSAlan Somers 	free(sock);
325ec92e61fSAlan Somers }
326ec92e61fSAlan Somers 
327ec92e61fSAlan Somers static void
gather_sctp(void)328ec92e61fSAlan Somers gather_sctp(void)
329ec92e61fSAlan Somers {
330ec92e61fSAlan Somers 	struct sock *sock;
331ec92e61fSAlan Somers 	struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
332ec92e61fSAlan Somers 	struct xsctp_inpcb *xinpcb;
333ec92e61fSAlan Somers 	struct xsctp_tcb *xstcb;
334ec92e61fSAlan Somers 	struct xsctp_raddr *xraddr;
335ec92e61fSAlan Somers 	struct xsctp_laddr *xladdr;
336ec92e61fSAlan Somers 	const char *varname;
337ec92e61fSAlan Somers 	size_t len, offset;
338ec92e61fSAlan Somers 	char *buf;
339ec92e61fSAlan Somers 	int vflag;
340ec92e61fSAlan Somers 	int no_stcb, local_all_loopback, foreign_all_loopback;
341ec92e61fSAlan Somers 
342ec92e61fSAlan Somers 	vflag = 0;
343ec92e61fSAlan Somers 	if (opt_4)
344ec92e61fSAlan Somers 		vflag |= INP_IPV4;
345ec92e61fSAlan Somers 	if (opt_6)
346ec92e61fSAlan Somers 		vflag |= INP_IPV6;
347ec92e61fSAlan Somers 
348ec92e61fSAlan Somers 	varname = "net.inet.sctp.assoclist";
349ec92e61fSAlan Somers 	if (cap_sysctlbyname(capsysctl, varname, 0, &len, 0, 0) < 0) {
350ec92e61fSAlan Somers 		if (errno != ENOENT)
351ec92e61fSAlan Somers 			xo_err(1, "cap_sysctlbyname()");
352ec92e61fSAlan Somers 		return;
353ec92e61fSAlan Somers 	}
354ec92e61fSAlan Somers 	if ((buf = (char *)malloc(len)) == NULL) {
355ec92e61fSAlan Somers 		xo_err(1, "malloc()");
356ec92e61fSAlan Somers 		return;
357ec92e61fSAlan Somers 	}
358ec92e61fSAlan Somers 	if (cap_sysctlbyname(capsysctl, varname, buf, &len, 0, 0) < 0) {
359ec92e61fSAlan Somers 		xo_err(1, "cap_sysctlbyname()");
360ec92e61fSAlan Somers 		free(buf);
361ec92e61fSAlan Somers 		return;
362ec92e61fSAlan Somers 	}
363ec92e61fSAlan Somers 	xinpcb = (struct xsctp_inpcb *)(void *)buf;
364ec92e61fSAlan Somers 	offset = sizeof(struct xsctp_inpcb);
365ec92e61fSAlan Somers 	while ((offset < len) && (xinpcb->last == 0)) {
366ec92e61fSAlan Somers 		if ((sock = calloc(1, sizeof *sock)) == NULL)
367ec92e61fSAlan Somers 			xo_err(1, "malloc()");
368ec92e61fSAlan Somers 		sock->socket = xinpcb->socket;
369ec92e61fSAlan Somers 		sock->proto = IPPROTO_SCTP;
370ec92e61fSAlan Somers 		sock->protoname = "sctp";
371ec92e61fSAlan Somers 		if (xinpcb->maxqlen == 0)
372ec92e61fSAlan Somers 			sock->state = SCTP_CLOSED;
373ec92e61fSAlan Somers 		else
374ec92e61fSAlan Somers 			sock->state = SCTP_LISTEN;
375ec92e61fSAlan Somers 		if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
376ec92e61fSAlan Somers 			sock->family = AF_INET6;
377ec92e61fSAlan Somers 			/*
378ec92e61fSAlan Somers 			 * Currently there is no way to distinguish between
379ec92e61fSAlan Somers 			 * IPv6 only sockets or dual family sockets.
380ec92e61fSAlan Somers 			 * So mark it as dual socket.
381ec92e61fSAlan Somers 			 */
382ec92e61fSAlan Somers 			sock->vflag = INP_IPV6 | INP_IPV4;
383ec92e61fSAlan Somers 		} else {
384ec92e61fSAlan Somers 			sock->family = AF_INET;
385ec92e61fSAlan Somers 			sock->vflag = INP_IPV4;
386ec92e61fSAlan Somers 		}
387ec92e61fSAlan Somers 		prev_laddr = NULL;
388ec92e61fSAlan Somers 		local_all_loopback = 1;
389ec92e61fSAlan Somers 		while (offset < len) {
390ec92e61fSAlan Somers 			xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
391ec92e61fSAlan Somers 			offset += sizeof(struct xsctp_laddr);
392ec92e61fSAlan Somers 			if (xladdr->last == 1)
393ec92e61fSAlan Somers 				break;
394ec92e61fSAlan Somers 			if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
395ec92e61fSAlan Somers 				xo_err(1, "malloc()");
396ec92e61fSAlan Somers 			switch (xladdr->address.sa.sa_family) {
397ec92e61fSAlan Somers 			case AF_INET:
398ec92e61fSAlan Somers #define	__IN_IS_ADDR_LOOPBACK(pina) \
399ec92e61fSAlan Somers 	((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
400ec92e61fSAlan Somers 				if (!__IN_IS_ADDR_LOOPBACK(
401ec92e61fSAlan Somers 				    &xladdr->address.sin.sin_addr))
402ec92e61fSAlan Somers 					local_all_loopback = 0;
403ec92e61fSAlan Somers #undef	__IN_IS_ADDR_LOOPBACK
404ec92e61fSAlan Somers 				sockaddr(&laddr->address, AF_INET,
405ec92e61fSAlan Somers 				    &xladdr->address.sin.sin_addr,
406ec92e61fSAlan Somers 				    htons(xinpcb->local_port));
407ec92e61fSAlan Somers 				break;
408ec92e61fSAlan Somers 			case AF_INET6:
409ec92e61fSAlan Somers 				if (!IN6_IS_ADDR_LOOPBACK(
410ec92e61fSAlan Somers 				    &xladdr->address.sin6.sin6_addr))
411ec92e61fSAlan Somers 					local_all_loopback = 0;
412ec92e61fSAlan Somers 				sockaddr(&laddr->address, AF_INET6,
413ec92e61fSAlan Somers 				    &xladdr->address.sin6.sin6_addr,
414ec92e61fSAlan Somers 				    htons(xinpcb->local_port));
415ec92e61fSAlan Somers 				break;
416ec92e61fSAlan Somers 			default:
417ec92e61fSAlan Somers 				xo_errx(1, "address family %d not supported",
418ec92e61fSAlan Somers 				    xladdr->address.sa.sa_family);
419ec92e61fSAlan Somers 			}
420ec92e61fSAlan Somers 			laddr->next = NULL;
421ec92e61fSAlan Somers 			if (prev_laddr == NULL)
422ec92e61fSAlan Somers 				sock->laddr = laddr;
423ec92e61fSAlan Somers 			else
424ec92e61fSAlan Somers 				prev_laddr->next = laddr;
425ec92e61fSAlan Somers 			prev_laddr = laddr;
426ec92e61fSAlan Somers 		}
427ec92e61fSAlan Somers 		if (sock->laddr == NULL) {
428ec92e61fSAlan Somers 			if ((sock->laddr =
429ec92e61fSAlan Somers 			    calloc(1, sizeof(struct addr))) == NULL)
430ec92e61fSAlan Somers 				xo_err(1, "malloc()");
431ec92e61fSAlan Somers 			sock->laddr->address.ss_family = sock->family;
432ec92e61fSAlan Somers 			if (sock->family == AF_INET)
433ec92e61fSAlan Somers 				sock->laddr->address.ss_len =
434ec92e61fSAlan Somers 				    sizeof(struct sockaddr_in);
435ec92e61fSAlan Somers 			else
436ec92e61fSAlan Somers 				sock->laddr->address.ss_len =
437ec92e61fSAlan Somers 				    sizeof(struct sockaddr_in6);
438ec92e61fSAlan Somers 			local_all_loopback = 0;
439ec92e61fSAlan Somers 		}
440ec92e61fSAlan Somers 		if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
441ec92e61fSAlan Somers 			xo_err(1, "malloc()");
442ec92e61fSAlan Somers 		sock->faddr->address.ss_family = sock->family;
443ec92e61fSAlan Somers 		if (sock->family == AF_INET)
444ec92e61fSAlan Somers 			sock->faddr->address.ss_len =
445ec92e61fSAlan Somers 			    sizeof(struct sockaddr_in);
446ec92e61fSAlan Somers 		else
447ec92e61fSAlan Somers 			sock->faddr->address.ss_len =
448ec92e61fSAlan Somers 			    sizeof(struct sockaddr_in6);
449ec92e61fSAlan Somers 		no_stcb = 1;
450ec92e61fSAlan Somers 		while (offset < len) {
451ec92e61fSAlan Somers 			xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
452ec92e61fSAlan Somers 			offset += sizeof(struct xsctp_tcb);
453ec92e61fSAlan Somers 			if (no_stcb) {
454ec92e61fSAlan Somers 				if (opt_l && (sock->vflag & vflag) &&
455ec92e61fSAlan Somers 				    (!opt_L || !local_all_loopback) &&
456ec92e61fSAlan Somers 				    ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
457ec92e61fSAlan Somers 				     (xstcb->last == 1))) {
458ec92e61fSAlan Somers 					RB_INSERT(socks_t, &socks, sock);
459ec92e61fSAlan Somers 				} else {
460ec92e61fSAlan Somers 					free_socket(sock);
461ec92e61fSAlan Somers 				}
462ec92e61fSAlan Somers 			}
463ec92e61fSAlan Somers 			if (xstcb->last == 1)
464ec92e61fSAlan Somers 				break;
465ec92e61fSAlan Somers 			no_stcb = 0;
466ec92e61fSAlan Somers 			if (opt_c) {
467ec92e61fSAlan Somers 				if ((sock = calloc(1, sizeof *sock)) == NULL)
468ec92e61fSAlan Somers 					xo_err(1, "malloc()");
469ec92e61fSAlan Somers 				sock->socket = xinpcb->socket;
470ec92e61fSAlan Somers 				sock->proto = IPPROTO_SCTP;
471ec92e61fSAlan Somers 				sock->protoname = "sctp";
472ec92e61fSAlan Somers 				sock->state = (int)xstcb->state;
473ec92e61fSAlan Somers 				if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
474ec92e61fSAlan Somers 					sock->family = AF_INET6;
475ec92e61fSAlan Somers 				/*
476ec92e61fSAlan Somers 				 * Currently there is no way to distinguish
477ec92e61fSAlan Somers 				 * between IPv6 only sockets or dual family
478ec92e61fSAlan Somers 				 *  sockets. So mark it as dual socket.
479ec92e61fSAlan Somers 				 */
480ec92e61fSAlan Somers 					sock->vflag = INP_IPV6 | INP_IPV4;
481ec92e61fSAlan Somers 				} else {
482ec92e61fSAlan Somers 					sock->family = AF_INET;
483ec92e61fSAlan Somers 					sock->vflag = INP_IPV4;
484ec92e61fSAlan Somers 				}
485ec92e61fSAlan Somers 			}
486ec92e61fSAlan Somers 			prev_laddr = NULL;
487ec92e61fSAlan Somers 			local_all_loopback = 1;
488ec92e61fSAlan Somers 			while (offset < len) {
489ec92e61fSAlan Somers 				xladdr = (struct xsctp_laddr *)(void *)(buf +
490ec92e61fSAlan Somers 				    offset);
491ec92e61fSAlan Somers 				offset += sizeof(struct xsctp_laddr);
492ec92e61fSAlan Somers 				if (xladdr->last == 1)
493ec92e61fSAlan Somers 					break;
494ec92e61fSAlan Somers 				if (!opt_c)
495ec92e61fSAlan Somers 					continue;
496ec92e61fSAlan Somers 				laddr = calloc(1, sizeof(struct addr));
497ec92e61fSAlan Somers 				if (laddr == NULL)
498ec92e61fSAlan Somers 					xo_err(1, "malloc()");
499ec92e61fSAlan Somers 				switch (xladdr->address.sa.sa_family) {
500ec92e61fSAlan Somers 				case AF_INET:
501ec92e61fSAlan Somers #define	__IN_IS_ADDR_LOOPBACK(pina) \
502ec92e61fSAlan Somers 	((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
503ec92e61fSAlan Somers 					if (!__IN_IS_ADDR_LOOPBACK(
504ec92e61fSAlan Somers 					    &xladdr->address.sin.sin_addr))
505ec92e61fSAlan Somers 						local_all_loopback = 0;
506ec92e61fSAlan Somers #undef	__IN_IS_ADDR_LOOPBACK
507ec92e61fSAlan Somers 					sockaddr(&laddr->address, AF_INET,
508ec92e61fSAlan Somers 					    &xladdr->address.sin.sin_addr,
509ec92e61fSAlan Somers 					    htons(xstcb->local_port));
510ec92e61fSAlan Somers 					break;
511ec92e61fSAlan Somers 				case AF_INET6:
512ec92e61fSAlan Somers 					if (!IN6_IS_ADDR_LOOPBACK(
513ec92e61fSAlan Somers 					    &xladdr->address.sin6.sin6_addr))
514ec92e61fSAlan Somers 						local_all_loopback = 0;
515ec92e61fSAlan Somers 					sockaddr(&laddr->address, AF_INET6,
516ec92e61fSAlan Somers 					    &xladdr->address.sin6.sin6_addr,
517ec92e61fSAlan Somers 					    htons(xstcb->local_port));
518ec92e61fSAlan Somers 					break;
519ec92e61fSAlan Somers 				default:
520ec92e61fSAlan Somers 					xo_errx(1,
521ec92e61fSAlan Somers 					    "address family %d not supported",
522ec92e61fSAlan Somers 					    xladdr->address.sa.sa_family);
523ec92e61fSAlan Somers 				}
524ec92e61fSAlan Somers 				laddr->next = NULL;
525ec92e61fSAlan Somers 				if (prev_laddr == NULL)
526ec92e61fSAlan Somers 					sock->laddr = laddr;
527ec92e61fSAlan Somers 				else
528ec92e61fSAlan Somers 					prev_laddr->next = laddr;
529ec92e61fSAlan Somers 				prev_laddr = laddr;
530ec92e61fSAlan Somers 			}
531ec92e61fSAlan Somers 			prev_faddr = NULL;
532ec92e61fSAlan Somers 			foreign_all_loopback = 1;
533ec92e61fSAlan Somers 			while (offset < len) {
534ec92e61fSAlan Somers 				xraddr = (struct xsctp_raddr *)(void *)(buf +
535ec92e61fSAlan Somers 				    offset);
536ec92e61fSAlan Somers 				offset += sizeof(struct xsctp_raddr);
537ec92e61fSAlan Somers 				if (xraddr->last == 1)
538ec92e61fSAlan Somers 					break;
539ec92e61fSAlan Somers 				if (!opt_c)
540ec92e61fSAlan Somers 					continue;
541ec92e61fSAlan Somers 				faddr = calloc(1, sizeof(struct addr));
542ec92e61fSAlan Somers 				if (faddr == NULL)
543ec92e61fSAlan Somers 					xo_err(1, "malloc()");
544ec92e61fSAlan Somers 				switch (xraddr->address.sa.sa_family) {
545ec92e61fSAlan Somers 				case AF_INET:
546ec92e61fSAlan Somers #define	__IN_IS_ADDR_LOOPBACK(pina) \
547ec92e61fSAlan Somers 	((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
548ec92e61fSAlan Somers 					if (!__IN_IS_ADDR_LOOPBACK(
549ec92e61fSAlan Somers 					    &xraddr->address.sin.sin_addr))
550ec92e61fSAlan Somers 						foreign_all_loopback = 0;
551ec92e61fSAlan Somers #undef	__IN_IS_ADDR_LOOPBACK
552ec92e61fSAlan Somers 					sockaddr(&faddr->address, AF_INET,
553ec92e61fSAlan Somers 					    &xraddr->address.sin.sin_addr,
554ec92e61fSAlan Somers 					    htons(xstcb->remote_port));
555ec92e61fSAlan Somers 					break;
556ec92e61fSAlan Somers 				case AF_INET6:
557ec92e61fSAlan Somers 					if (!IN6_IS_ADDR_LOOPBACK(
558ec92e61fSAlan Somers 					    &xraddr->address.sin6.sin6_addr))
559ec92e61fSAlan Somers 						foreign_all_loopback = 0;
560ec92e61fSAlan Somers 					sockaddr(&faddr->address, AF_INET6,
561ec92e61fSAlan Somers 					    &xraddr->address.sin6.sin6_addr,
562ec92e61fSAlan Somers 					    htons(xstcb->remote_port));
563ec92e61fSAlan Somers 					break;
564ec92e61fSAlan Somers 				default:
565ec92e61fSAlan Somers 					xo_errx(1,
566ec92e61fSAlan Somers 					    "address family %d not supported",
567ec92e61fSAlan Somers 					    xraddr->address.sa.sa_family);
568ec92e61fSAlan Somers 				}
569ec92e61fSAlan Somers 				faddr->encaps_port = xraddr->encaps_port;
570ec92e61fSAlan Somers 				faddr->state = xraddr->state;
571ec92e61fSAlan Somers 				faddr->next = NULL;
572ec92e61fSAlan Somers 				if (prev_faddr == NULL)
573ec92e61fSAlan Somers 					sock->faddr = faddr;
574ec92e61fSAlan Somers 				else
575ec92e61fSAlan Somers 					prev_faddr->next = faddr;
576ec92e61fSAlan Somers 				prev_faddr = faddr;
577ec92e61fSAlan Somers 			}
578ec92e61fSAlan Somers 			if (opt_c) {
579ec92e61fSAlan Somers 				if ((sock->vflag & vflag) &&
580ec92e61fSAlan Somers 				    (!opt_L ||
581ec92e61fSAlan Somers 				     !(local_all_loopback ||
582ec92e61fSAlan Somers 				     foreign_all_loopback))) {
583ec92e61fSAlan Somers 					RB_INSERT(socks_t, &socks, sock);
584ec92e61fSAlan Somers 				} else {
585ec92e61fSAlan Somers 					free_socket(sock);
586ec92e61fSAlan Somers 				}
587ec92e61fSAlan Somers 			}
588ec92e61fSAlan Somers 		}
589ec92e61fSAlan Somers 		xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
590ec92e61fSAlan Somers 		offset += sizeof(struct xsctp_inpcb);
591ec92e61fSAlan Somers 	}
592ec92e61fSAlan Somers 	free(buf);
593ec92e61fSAlan Somers }
594ec92e61fSAlan Somers 
595ec92e61fSAlan Somers static void
gather_inet(int proto)596ec92e61fSAlan Somers gather_inet(int proto)
597ec92e61fSAlan Somers {
598ec92e61fSAlan Somers 	struct xinpgen *xig, *exig;
599ec92e61fSAlan Somers 	struct xinpcb *xip;
600ec92e61fSAlan Somers 	struct xtcpcb *xtp = NULL;
601ec92e61fSAlan Somers 	struct xsocket *so;
602ec92e61fSAlan Somers 	struct sock *sock;
603ec92e61fSAlan Somers 	struct addr *laddr, *faddr;
604ec92e61fSAlan Somers 	const char *varname, *protoname;
605ec92e61fSAlan Somers 	size_t len, bufsize;
606ec92e61fSAlan Somers 	void *buf;
607ec92e61fSAlan Somers 	int retry, vflag;
608ec92e61fSAlan Somers 
609ec92e61fSAlan Somers 	vflag = 0;
610ec92e61fSAlan Somers 	if (opt_4)
611ec92e61fSAlan Somers 		vflag |= INP_IPV4;
612ec92e61fSAlan Somers 	if (opt_6)
613ec92e61fSAlan Somers 		vflag |= INP_IPV6;
614ec92e61fSAlan Somers 
615ec92e61fSAlan Somers 	switch (proto) {
616ec92e61fSAlan Somers 	case IPPROTO_TCP:
617ec92e61fSAlan Somers 		varname = "net.inet.tcp.pcblist";
618ec92e61fSAlan Somers 		protoname = "tcp";
619ec92e61fSAlan Somers 		break;
620ec92e61fSAlan Somers 	case IPPROTO_UDP:
621ec92e61fSAlan Somers 		varname = "net.inet.udp.pcblist";
622ec92e61fSAlan Somers 		protoname = "udp";
623ec92e61fSAlan Somers 		break;
624ec92e61fSAlan Somers 	case IPPROTO_DIVERT:
625ec92e61fSAlan Somers 		varname = "net.inet.divert.pcblist";
626ec92e61fSAlan Somers 		protoname = "div";
627ec92e61fSAlan Somers 		break;
628ec92e61fSAlan Somers 	default:
629ec92e61fSAlan Somers 		xo_errx(1, "protocol %d not supported", proto);
630ec92e61fSAlan Somers 	}
631ec92e61fSAlan Somers 
632ec92e61fSAlan Somers 	buf = NULL;
633ec92e61fSAlan Somers 	bufsize = 8192;
634ec92e61fSAlan Somers 	retry = 5;
635ec92e61fSAlan Somers 	do {
636ec92e61fSAlan Somers 		for (;;) {
637ec92e61fSAlan Somers 			if ((buf = realloc(buf, bufsize)) == NULL)
638ec92e61fSAlan Somers 				xo_err(1, "realloc()");
639ec92e61fSAlan Somers 			len = bufsize;
640ec92e61fSAlan Somers 			if (cap_sysctlbyname(capsysctl, varname, buf, &len,
641ec92e61fSAlan Somers 			    NULL, 0) == 0)
642ec92e61fSAlan Somers 				break;
643ec92e61fSAlan Somers 			if (errno == ENOENT)
644ec92e61fSAlan Somers 				goto out;
645ec92e61fSAlan Somers 			if (errno != ENOMEM || len != bufsize)
646ec92e61fSAlan Somers 				xo_err(1, "cap_sysctlbyname()");
647ec92e61fSAlan Somers 			bufsize *= 2;
648ec92e61fSAlan Somers 		}
649ec92e61fSAlan Somers 		xig = (struct xinpgen *)buf;
650ec92e61fSAlan Somers 		exig = (struct xinpgen *)(void *)
651ec92e61fSAlan Somers 		    ((char *)buf + len - sizeof *exig);
652ec92e61fSAlan Somers 		enforce_ksize(xig->xig_len, struct xinpgen);
653ec92e61fSAlan Somers 		enforce_ksize(exig->xig_len, struct xinpgen);
654ec92e61fSAlan Somers 	} while (xig->xig_gen != exig->xig_gen && retry--);
655ec92e61fSAlan Somers 
656ec92e61fSAlan Somers 	if (xig->xig_gen != exig->xig_gen && opt_v)
657ec92e61fSAlan Somers 		xo_warnx("warning: data may be inconsistent");
658ec92e61fSAlan Somers 
659ec92e61fSAlan Somers 	for (;;) {
660ec92e61fSAlan Somers 		xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
661ec92e61fSAlan Somers 		if (xig >= exig)
662ec92e61fSAlan Somers 			break;
663ec92e61fSAlan Somers 		switch (proto) {
664ec92e61fSAlan Somers 		case IPPROTO_TCP:
665ec92e61fSAlan Somers 			xtp = (struct xtcpcb *)xig;
666ec92e61fSAlan Somers 			xip = &xtp->xt_inp;
667ec92e61fSAlan Somers 			if (!check_ksize(xtp->xt_len, struct xtcpcb))
668ec92e61fSAlan Somers 				goto out;
669ec92e61fSAlan Somers 			protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
670ec92e61fSAlan Somers 			break;
671ec92e61fSAlan Somers 		case IPPROTO_UDP:
672ec92e61fSAlan Somers 		case IPPROTO_DIVERT:
673ec92e61fSAlan Somers 			xip = (struct xinpcb *)xig;
674ec92e61fSAlan Somers 			if (!check_ksize(xip->xi_len, struct xinpcb))
675ec92e61fSAlan Somers 				goto out;
676ec92e61fSAlan Somers 			break;
677ec92e61fSAlan Somers 		default:
678ec92e61fSAlan Somers 			xo_errx(1, "protocol %d not supported", proto);
679ec92e61fSAlan Somers 		}
680ec92e61fSAlan Somers 		so = &xip->xi_socket;
681ec92e61fSAlan Somers 		if ((xip->inp_vflag & vflag) == 0)
682ec92e61fSAlan Somers 			continue;
683ec92e61fSAlan Somers 		if (xip->inp_vflag & INP_IPV4) {
684ec92e61fSAlan Somers 			if ((xip->inp_fport == 0 && !opt_l) ||
685ec92e61fSAlan Somers 			    (xip->inp_fport != 0 && !opt_c))
686ec92e61fSAlan Somers 				continue;
687ec92e61fSAlan Somers #define	__IN_IS_ADDR_LOOPBACK(pina) \
688ec92e61fSAlan Somers 	((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
689ec92e61fSAlan Somers 			if (opt_L &&
690ec92e61fSAlan Somers 			    (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
691ec92e61fSAlan Somers 			     __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
692ec92e61fSAlan Somers 				continue;
693ec92e61fSAlan Somers #undef	__IN_IS_ADDR_LOOPBACK
694ec92e61fSAlan Somers 		} else if (xip->inp_vflag & INP_IPV6) {
695ec92e61fSAlan Somers 			if ((xip->inp_fport == 0 && !opt_l) ||
696ec92e61fSAlan Somers 			    (xip->inp_fport != 0 && !opt_c))
697ec92e61fSAlan Somers 				continue;
698ec92e61fSAlan Somers 			if (opt_L &&
699ec92e61fSAlan Somers 			    (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
700ec92e61fSAlan Somers 			     IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
701ec92e61fSAlan Somers 				continue;
702ec92e61fSAlan Somers 		} else {
703ec92e61fSAlan Somers 			if (opt_v)
704ec92e61fSAlan Somers 				xo_warnx("invalid vflag 0x%x", xip->inp_vflag);
705ec92e61fSAlan Somers 			continue;
706ec92e61fSAlan Somers 		}
707ec92e61fSAlan Somers 		if ((sock = calloc(1, sizeof(*sock))) == NULL)
708ec92e61fSAlan Somers 			xo_err(1, "malloc()");
709ec92e61fSAlan Somers 		if ((laddr = calloc(1, sizeof *laddr)) == NULL)
710ec92e61fSAlan Somers 			xo_err(1, "malloc()");
711ec92e61fSAlan Somers 		if ((faddr = calloc(1, sizeof *faddr)) == NULL)
712ec92e61fSAlan Somers 			xo_err(1, "malloc()");
713ec92e61fSAlan Somers 		sock->socket = so->xso_so;
714ec92e61fSAlan Somers 		sock->pcb = so->so_pcb;
715ec92e61fSAlan Somers 		sock->splice_socket = so->so_splice_so;
716ec92e61fSAlan Somers 		sock->proto = proto;
717ec92e61fSAlan Somers 		sock->inp_gencnt = xip->inp_gencnt;
718ec92e61fSAlan Somers 		sock->fibnum = so->so_fibnum;
719ec92e61fSAlan Somers 		if (xip->inp_vflag & INP_IPV4) {
720ec92e61fSAlan Somers 			sock->family = AF_INET;
721ec92e61fSAlan Somers 			sockaddr(&laddr->address, sock->family,
722ec92e61fSAlan Somers 			    &xip->inp_laddr, xip->inp_lport);
723ec92e61fSAlan Somers 			sockaddr(&faddr->address, sock->family,
724ec92e61fSAlan Somers 			    &xip->inp_faddr, xip->inp_fport);
725ec92e61fSAlan Somers 		} else if (xip->inp_vflag & INP_IPV6) {
726ec92e61fSAlan Somers 			sock->family = AF_INET6;
727ec92e61fSAlan Somers 			sockaddr(&laddr->address, sock->family,
728ec92e61fSAlan Somers 			    &xip->in6p_laddr, xip->inp_lport);
729ec92e61fSAlan Somers 			sockaddr(&faddr->address, sock->family,
730ec92e61fSAlan Somers 			    &xip->in6p_faddr, xip->inp_fport);
731ec92e61fSAlan Somers 		}
732ec92e61fSAlan Somers 		if (proto == IPPROTO_TCP)
733ec92e61fSAlan Somers 			faddr->encaps_port = xtp->xt_encaps_port;
734ec92e61fSAlan Somers 		laddr->next = NULL;
735ec92e61fSAlan Somers 		faddr->next = NULL;
736ec92e61fSAlan Somers 		sock->laddr = laddr;
737ec92e61fSAlan Somers 		sock->faddr = faddr;
738ec92e61fSAlan Somers 		sock->vflag = xip->inp_vflag;
739ec92e61fSAlan Somers 		if (proto == IPPROTO_TCP) {
740ec92e61fSAlan Somers 			sock->state = xtp->t_state;
741ec92e61fSAlan Somers 			memcpy(sock->stack, xtp->xt_stack,
742ec92e61fSAlan Somers 			    TCP_FUNCTION_NAME_LEN_MAX);
743ec92e61fSAlan Somers 			memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX);
744ec92e61fSAlan Somers 		}
745ec92e61fSAlan Somers 		sock->protoname = protoname;
746ec92e61fSAlan Somers 		if (sock->socket != 0)
747ec92e61fSAlan Somers 			RB_INSERT(socks_t, &socks, sock);
748ec92e61fSAlan Somers 		else
749ec92e61fSAlan Somers 			SLIST_INSERT_HEAD(&nosocks, sock, socket_list);
750ec92e61fSAlan Somers 	}
751ec92e61fSAlan Somers out:
752ec92e61fSAlan Somers 	free(buf);
753ec92e61fSAlan Somers }
754ec92e61fSAlan Somers 
755ec92e61fSAlan Somers static void
gather_unix(int proto)756ec92e61fSAlan Somers gather_unix(int proto)
757ec92e61fSAlan Somers {
758ec92e61fSAlan Somers 	struct xunpgen *xug, *exug;
759ec92e61fSAlan Somers 	struct xunpcb *xup;
760ec92e61fSAlan Somers 	struct sock *sock;
761ec92e61fSAlan Somers 	struct addr *laddr, *faddr;
762ec92e61fSAlan Somers 	const char *varname, *protoname;
763ec92e61fSAlan Somers 	size_t len, bufsize;
764ec92e61fSAlan Somers 	void *buf;
765ec92e61fSAlan Somers 	int retry;
766ec92e61fSAlan Somers 
767ec92e61fSAlan Somers 	switch (proto) {
768ec92e61fSAlan Somers 	case SOCK_STREAM:
769ec92e61fSAlan Somers 		varname = "net.local.stream.pcblist";
770ec92e61fSAlan Somers 		protoname = "stream";
771ec92e61fSAlan Somers 		break;
772ec92e61fSAlan Somers 	case SOCK_DGRAM:
773ec92e61fSAlan Somers 		varname = "net.local.dgram.pcblist";
774ec92e61fSAlan Somers 		protoname = "dgram";
775ec92e61fSAlan Somers 		break;
776ec92e61fSAlan Somers 	case SOCK_SEQPACKET:
777ec92e61fSAlan Somers 		varname = "net.local.seqpacket.pcblist";
778ec92e61fSAlan Somers 		protoname = is_xo_style_encoding ? "seqpacket" : "seqpack";
779ec92e61fSAlan Somers 		break;
780ec92e61fSAlan Somers 	default:
781ec92e61fSAlan Somers 		abort();
782ec92e61fSAlan Somers 	}
783ec92e61fSAlan Somers 	buf = NULL;
784ec92e61fSAlan Somers 	bufsize = 8192;
785ec92e61fSAlan Somers 	retry = 5;
786ec92e61fSAlan Somers 	do {
787ec92e61fSAlan Somers 		for (;;) {
788ec92e61fSAlan Somers 			if ((buf = realloc(buf, bufsize)) == NULL)
789ec92e61fSAlan Somers 				xo_err(1, "realloc()");
790ec92e61fSAlan Somers 			len = bufsize;
791ec92e61fSAlan Somers 			if (cap_sysctlbyname(capsysctl, varname, buf, &len,
792ec92e61fSAlan Somers 			    NULL, 0) == 0)
793ec92e61fSAlan Somers 				break;
794ec92e61fSAlan Somers 			if (errno != ENOMEM || len != bufsize)
795ec92e61fSAlan Somers 				xo_err(1, "cap_sysctlbyname()");
796ec92e61fSAlan Somers 			bufsize *= 2;
797ec92e61fSAlan Somers 		}
798ec92e61fSAlan Somers 		xug = (struct xunpgen *)buf;
799ec92e61fSAlan Somers 		exug = (struct xunpgen *)(void *)
800ec92e61fSAlan Somers 		    ((char *)buf + len - sizeof(*exug));
801ec92e61fSAlan Somers 		if (!check_ksize(xug->xug_len, struct xunpgen) ||
802ec92e61fSAlan Somers 		    !check_ksize(exug->xug_len, struct xunpgen))
803ec92e61fSAlan Somers 			goto out;
804ec92e61fSAlan Somers 	} while (xug->xug_gen != exug->xug_gen && retry--);
805ec92e61fSAlan Somers 
806ec92e61fSAlan Somers 	if (xug->xug_gen != exug->xug_gen && opt_v)
807ec92e61fSAlan Somers 		xo_warnx("warning: data may be inconsistent");
808ec92e61fSAlan Somers 
809ec92e61fSAlan Somers 	for (;;) {
810ec92e61fSAlan Somers 		xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
811ec92e61fSAlan Somers 		if (xug >= exug)
812ec92e61fSAlan Somers 			break;
813ec92e61fSAlan Somers 		xup = (struct xunpcb *)xug;
814ec92e61fSAlan Somers 		if (!check_ksize(xup->xu_len, struct xunpcb))
815ec92e61fSAlan Somers 			goto out;
816ec92e61fSAlan Somers 		if ((xup->unp_conn == 0 && !opt_l) ||
817ec92e61fSAlan Somers 		    (xup->unp_conn != 0 && !opt_c))
818ec92e61fSAlan Somers 			continue;
819ec92e61fSAlan Somers 		if ((sock = calloc(1, sizeof(*sock))) == NULL)
820ec92e61fSAlan Somers 			xo_err(1, "malloc()");
821ec92e61fSAlan Somers 		if ((laddr = calloc(1, sizeof *laddr)) == NULL)
822ec92e61fSAlan Somers 			xo_err(1, "malloc()");
823ec92e61fSAlan Somers 		if ((faddr = calloc(1, sizeof *faddr)) == NULL)
824ec92e61fSAlan Somers 			xo_err(1, "malloc()");
825ec92e61fSAlan Somers 		sock->socket = xup->xu_socket.xso_so;
826ec92e61fSAlan Somers 		sock->pcb = xup->xu_unpp;
827ec92e61fSAlan Somers 		sock->proto = proto;
828ec92e61fSAlan Somers 		sock->family = AF_UNIX;
829ec92e61fSAlan Somers 		sock->protoname = protoname;
830ec92e61fSAlan Somers 		if (xup->xu_addr.sun_family == AF_UNIX)
831ec92e61fSAlan Somers 			laddr->address =
832ec92e61fSAlan Somers 			    *(struct sockaddr_storage *)(void *)&xup->xu_addr;
833ec92e61fSAlan Somers 		faddr->conn = xup->unp_conn;
834ec92e61fSAlan Somers 		faddr->firstref = xup->xu_firstref;
835ec92e61fSAlan Somers 		faddr->nextref = xup->xu_nextref;
836ec92e61fSAlan Somers 		laddr->next = NULL;
837ec92e61fSAlan Somers 		faddr->next = NULL;
838ec92e61fSAlan Somers 		sock->laddr = laddr;
839ec92e61fSAlan Somers 		sock->faddr = faddr;
840ec92e61fSAlan Somers 		RB_INSERT(socks_t, &socks, sock);
841ec92e61fSAlan Somers 		RB_INSERT(pcbs_t, &pcbs, sock);
842ec92e61fSAlan Somers 	}
843ec92e61fSAlan Somers out:
844ec92e61fSAlan Somers 	free(buf);
845ec92e61fSAlan Somers }
846ec92e61fSAlan Somers 
847ec92e61fSAlan Somers static void
getfiles(void)848ec92e61fSAlan Somers getfiles(void)
849ec92e61fSAlan Somers {
850ec92e61fSAlan Somers 	struct xfile *xfiles;
851ec92e61fSAlan Somers 	size_t len, olen;
852ec92e61fSAlan Somers 
853ec92e61fSAlan Somers 	olen = len = sizeof(*xfiles);
854ec92e61fSAlan Somers 	if ((xfiles = malloc(len)) == NULL)
855ec92e61fSAlan Somers 		xo_err(1, "malloc()");
856ec92e61fSAlan Somers 	while (cap_sysctlbyname(capsysctl, "kern.file", xfiles, &len, 0, 0)
857ec92e61fSAlan Somers 	    == -1) {
858ec92e61fSAlan Somers 		if (errno != ENOMEM || len != olen)
859ec92e61fSAlan Somers 			xo_err(1, "cap_sysctlbyname()");
860ec92e61fSAlan Somers 		olen = len *= 2;
861ec92e61fSAlan Somers 		if ((xfiles = realloc(xfiles, len)) == NULL)
862ec92e61fSAlan Somers 			xo_err(1, "realloc()");
863ec92e61fSAlan Somers 	}
864ec92e61fSAlan Somers 	if (len > 0)
865ec92e61fSAlan Somers 		enforce_ksize(xfiles->xf_size, struct xfile);
866ec92e61fSAlan Somers 	nfiles = len / sizeof(*xfiles);
867ec92e61fSAlan Somers 
868ec92e61fSAlan Somers 	if ((files = malloc(nfiles * sizeof(struct file))) == NULL)
869ec92e61fSAlan Somers 		xo_err(1, "malloc()");
870ec92e61fSAlan Somers 
871ec92e61fSAlan Somers 	for (int i = 0; i < nfiles; i++) {
872ec92e61fSAlan Somers 		files[i].xf_data = xfiles[i].xf_data;
873ec92e61fSAlan Somers 		files[i].xf_pid = xfiles[i].xf_pid;
874ec92e61fSAlan Somers 		files[i].xf_uid = xfiles[i].xf_uid;
875ec92e61fSAlan Somers 		files[i].xf_fd = xfiles[i].xf_fd;
876ec92e61fSAlan Somers 		RB_INSERT(files_t, &ftree, &files[i]);
877ec92e61fSAlan Somers 	}
878ec92e61fSAlan Somers 
879ec92e61fSAlan Somers 	free(xfiles);
880ec92e61fSAlan Somers }
881ec92e61fSAlan Somers 
882ec92e61fSAlan Somers static int
formataddr(struct sockaddr_storage * ss,char * buf,size_t bufsize)883ec92e61fSAlan Somers formataddr(struct sockaddr_storage *ss, char *buf, size_t bufsize)
884ec92e61fSAlan Somers {
885ec92e61fSAlan Somers 	struct sockaddr_un *sun;
886ec92e61fSAlan Somers 	char addrstr[NI_MAXHOST] = { '\0', '\0' };
887ec92e61fSAlan Somers 	int error, off, port = 0;
888ec92e61fSAlan Somers 
889ec92e61fSAlan Somers 	switch (ss->ss_family) {
890ec92e61fSAlan Somers 	case AF_INET:
891ec92e61fSAlan Somers 		if (sstosin(ss)->sin_addr.s_addr == INADDR_ANY)
892ec92e61fSAlan Somers 			addrstr[0] = '*';
893ec92e61fSAlan Somers 		port = ntohs(sstosin(ss)->sin_port);
894ec92e61fSAlan Somers 		break;
895ec92e61fSAlan Somers 	case AF_INET6:
896ec92e61fSAlan Somers 		if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
897ec92e61fSAlan Somers 			addrstr[0] = '*';
898ec92e61fSAlan Somers 		port = ntohs(sstosin6(ss)->sin6_port);
899ec92e61fSAlan Somers 		break;
900ec92e61fSAlan Somers 	case AF_UNIX:
901ec92e61fSAlan Somers 		sun = sstosun(ss);
902ec92e61fSAlan Somers 		off = (int)((char *)&sun->sun_path - (char *)sun);
903ec92e61fSAlan Somers 		if (is_xo_style_encoding) {
904ec92e61fSAlan Somers 			xo_emit("{:path/%.*s}", sun->sun_len - off,
905ec92e61fSAlan Somers 				sun->sun_path);
906ec92e61fSAlan Somers 			return 0;
907ec92e61fSAlan Somers 		}
908ec92e61fSAlan Somers 		return snprintf(buf, bufsize, "%.*s",
909ec92e61fSAlan Somers 				sun->sun_len - off, sun->sun_path);
910ec92e61fSAlan Somers 	}
911ec92e61fSAlan Somers 	if (addrstr[0] == '\0') {
912ec92e61fSAlan Somers 		error = cap_getnameinfo(capnet, sstosa(ss), ss->ss_len,
913ec92e61fSAlan Somers 		    addrstr, sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
914ec92e61fSAlan Somers 		if (error)
915ec92e61fSAlan Somers 			xo_errx(1, "cap_getnameinfo()");
916ec92e61fSAlan Somers 	}
917ec92e61fSAlan Somers 	if (is_xo_style_encoding) {
918ec92e61fSAlan Somers 		xo_emit("{:address/%s}", addrstr);
919ec92e61fSAlan Somers 		xo_emit("{:port/%d}", port);
920ec92e61fSAlan Somers 		return 0;
921ec92e61fSAlan Somers 	}
922ec92e61fSAlan Somers 	if (port == 0)
923ec92e61fSAlan Somers 		return snprintf(buf, bufsize, "%s:*", addrstr);
924ec92e61fSAlan Somers 	return snprintf(buf, bufsize, "%s:%d", addrstr, port);
925ec92e61fSAlan Somers }
926ec92e61fSAlan Somers 
927ec92e61fSAlan Somers static const char *
getprocname(pid_t pid)928ec92e61fSAlan Somers getprocname(pid_t pid)
929ec92e61fSAlan Somers {
930ec92e61fSAlan Somers 	static struct kinfo_proc proc;
931ec92e61fSAlan Somers 	size_t len;
932ec92e61fSAlan Somers 	int mib[4];
933ec92e61fSAlan Somers 
934ec92e61fSAlan Somers 	mib[0] = CTL_KERN;
935ec92e61fSAlan Somers 	mib[1] = KERN_PROC;
936ec92e61fSAlan Somers 	mib[2] = KERN_PROC_PID;
937ec92e61fSAlan Somers 	mib[3] = (int)pid;
938ec92e61fSAlan Somers 	len = sizeof(proc);
939ec92e61fSAlan Somers 	if (cap_sysctl(capsysctl, mib, nitems(mib), &proc, &len, NULL, 0)
940ec92e61fSAlan Somers 	    == -1) {
941ec92e61fSAlan Somers 		/* Do not warn if the process exits before we get its name. */
942ec92e61fSAlan Somers 		if (errno != ESRCH)
943ec92e61fSAlan Somers 			xo_warn("cap_sysctl()");
944ec92e61fSAlan Somers 		return ("??");
945ec92e61fSAlan Somers 	}
946ec92e61fSAlan Somers 	return (proc.ki_comm);
947ec92e61fSAlan Somers }
948ec92e61fSAlan Somers 
949ec92e61fSAlan Somers static int
getprocjid(pid_t pid)950ec92e61fSAlan Somers getprocjid(pid_t pid)
951ec92e61fSAlan Somers {
952ec92e61fSAlan Somers 	static struct kinfo_proc proc;
953ec92e61fSAlan Somers 	size_t len;
954ec92e61fSAlan Somers 	int mib[4];
955ec92e61fSAlan Somers 
956ec92e61fSAlan Somers 	mib[0] = CTL_KERN;
957ec92e61fSAlan Somers 	mib[1] = KERN_PROC;
958ec92e61fSAlan Somers 	mib[2] = KERN_PROC_PID;
959ec92e61fSAlan Somers 	mib[3] = (int)pid;
960ec92e61fSAlan Somers 	len = sizeof(proc);
961ec92e61fSAlan Somers 	if (cap_sysctl(capsysctl, mib, nitems(mib), &proc, &len, NULL, 0)
962ec92e61fSAlan Somers 	    == -1) {
963ec92e61fSAlan Somers 		/* Do not warn if the process exits before we get its jid. */
964ec92e61fSAlan Somers 		if (errno != ESRCH)
965ec92e61fSAlan Somers 			xo_warn("cap_sysctl()");
966ec92e61fSAlan Somers 		return (-1);
967ec92e61fSAlan Somers 	}
968ec92e61fSAlan Somers 	return (proc.ki_jid);
969ec92e61fSAlan Somers }
970ec92e61fSAlan Somers 
971ec92e61fSAlan Somers static int
check_ports(struct sock * s)972ec92e61fSAlan Somers check_ports(struct sock *s)
973ec92e61fSAlan Somers {
974ec92e61fSAlan Somers 	int port;
975ec92e61fSAlan Somers 	struct addr *addr;
976ec92e61fSAlan Somers 
977ec92e61fSAlan Somers 	if (ports == NULL)
978ec92e61fSAlan Somers 		return (1);
979ec92e61fSAlan Somers 	if ((s->family != AF_INET) && (s->family != AF_INET6))
980ec92e61fSAlan Somers 		return (1);
981ec92e61fSAlan Somers 	for (addr = s->laddr; addr != NULL; addr = addr->next) {
982ec92e61fSAlan Somers 		if (s->family == AF_INET)
983ec92e61fSAlan Somers 			port = ntohs(sstosin(&addr->address)->sin_port);
984ec92e61fSAlan Somers 		else
985ec92e61fSAlan Somers 			port = ntohs(sstosin6(&addr->address)->sin6_port);
986ec92e61fSAlan Somers 		if (CHK_PORT(port))
987ec92e61fSAlan Somers 			return (1);
988ec92e61fSAlan Somers 	}
989ec92e61fSAlan Somers 	for (addr = s->faddr; addr != NULL; addr = addr->next) {
990ec92e61fSAlan Somers 		if (s->family == AF_INET)
991ec92e61fSAlan Somers 			port = ntohs(sstosin(&addr->address)->sin_port);
992ec92e61fSAlan Somers 		else
993ec92e61fSAlan Somers 			port = ntohs(sstosin6(&addr->address)->sin6_port);
994ec92e61fSAlan Somers 		if (CHK_PORT(port))
995ec92e61fSAlan Somers 			return (1);
996ec92e61fSAlan Somers 	}
997ec92e61fSAlan Somers 	return (0);
998ec92e61fSAlan Somers }
999ec92e61fSAlan Somers 
1000ec92e61fSAlan Somers static const char *
sctp_conn_state(int state)1001ec92e61fSAlan Somers sctp_conn_state(int state)
1002ec92e61fSAlan Somers {
1003ec92e61fSAlan Somers 	switch (state) {
1004ec92e61fSAlan Somers 	case SCTP_CLOSED:
1005ec92e61fSAlan Somers 		return "CLOSED";
1006ec92e61fSAlan Somers 		break;
1007ec92e61fSAlan Somers 	case SCTP_BOUND:
1008ec92e61fSAlan Somers 		return "BOUND";
1009ec92e61fSAlan Somers 		break;
1010ec92e61fSAlan Somers 	case SCTP_LISTEN:
1011ec92e61fSAlan Somers 		return "LISTEN";
1012ec92e61fSAlan Somers 		break;
1013ec92e61fSAlan Somers 	case SCTP_COOKIE_WAIT:
1014ec92e61fSAlan Somers 		return "COOKIE_WAIT";
1015ec92e61fSAlan Somers 		break;
1016ec92e61fSAlan Somers 	case SCTP_COOKIE_ECHOED:
1017ec92e61fSAlan Somers 		return "COOKIE_ECHOED";
1018ec92e61fSAlan Somers 		break;
1019ec92e61fSAlan Somers 	case SCTP_ESTABLISHED:
1020ec92e61fSAlan Somers 		return "ESTABLISHED";
1021ec92e61fSAlan Somers 		break;
1022ec92e61fSAlan Somers 	case SCTP_SHUTDOWN_SENT:
1023ec92e61fSAlan Somers 		return "SHUTDOWN_SENT";
1024ec92e61fSAlan Somers 		break;
1025ec92e61fSAlan Somers 	case SCTP_SHUTDOWN_RECEIVED:
1026ec92e61fSAlan Somers 		return "SHUTDOWN_RECEIVED";
1027ec92e61fSAlan Somers 		break;
1028ec92e61fSAlan Somers 	case SCTP_SHUTDOWN_ACK_SENT:
1029ec92e61fSAlan Somers 		return "SHUTDOWN_ACK_SENT";
1030ec92e61fSAlan Somers 		break;
1031ec92e61fSAlan Somers 	case SCTP_SHUTDOWN_PENDING:
1032ec92e61fSAlan Somers 		return "SHUTDOWN_PENDING";
1033ec92e61fSAlan Somers 		break;
1034ec92e61fSAlan Somers 	default:
1035ec92e61fSAlan Somers 		return "UNKNOWN";
1036ec92e61fSAlan Somers 		break;
1037ec92e61fSAlan Somers 	}
1038ec92e61fSAlan Somers }
1039ec92e61fSAlan Somers 
1040ec92e61fSAlan Somers static const char *
sctp_path_state(int state)1041ec92e61fSAlan Somers sctp_path_state(int state)
1042ec92e61fSAlan Somers {
1043ec92e61fSAlan Somers 	switch (state) {
1044ec92e61fSAlan Somers 	case SCTP_UNCONFIRMED:
1045ec92e61fSAlan Somers 		return "UNCONFIRMED";
1046ec92e61fSAlan Somers 		break;
1047ec92e61fSAlan Somers 	case SCTP_ACTIVE:
1048ec92e61fSAlan Somers 		return "ACTIVE";
1049ec92e61fSAlan Somers 		break;
1050ec92e61fSAlan Somers 	case SCTP_INACTIVE:
1051ec92e61fSAlan Somers 		return "INACTIVE";
1052ec92e61fSAlan Somers 		break;
1053ec92e61fSAlan Somers 	default:
1054ec92e61fSAlan Somers 		return "UNKNOWN";
1055ec92e61fSAlan Somers 		break;
1056ec92e61fSAlan Somers 	}
1057ec92e61fSAlan Somers }
1058ec92e61fSAlan Somers 
1059ec92e61fSAlan Somers static int
format_unix_faddr(struct addr * faddr,char * buf,size_t bufsize)1060ec92e61fSAlan Somers format_unix_faddr(struct addr *faddr, char *buf, size_t bufsize) {
1061ec92e61fSAlan Somers 	#define SAFEBUF  (buf == NULL ? NULL : buf + pos)
1062ec92e61fSAlan Somers 	#define SAFESIZE (buf == NULL ? 0 : bufsize - pos)
1063ec92e61fSAlan Somers 
1064ec92e61fSAlan Somers 	size_t pos = 0;
1065ec92e61fSAlan Somers 	if (faddr->conn != 0) {
1066ec92e61fSAlan Somers 		/* Remote peer we connect(2) to, if any. */
1067ec92e61fSAlan Somers 		struct sock *p;
1068ec92e61fSAlan Somers 		if (!is_xo_style_encoding)
1069ec92e61fSAlan Somers 			pos += strlcpy(SAFEBUF, "-> ", SAFESIZE);
1070ec92e61fSAlan Somers 		p = RB_FIND(pcbs_t, &pcbs,
1071ec92e61fSAlan Somers 			&(struct sock){ .pcb = faddr->conn });
1072ec92e61fSAlan Somers 		if (__predict_false(p == NULL) && !is_xo_style_encoding) {
1073ec92e61fSAlan Somers 			/* XXGL: can this happen at all? */
1074ec92e61fSAlan Somers 			pos += snprintf(SAFEBUF, SAFESIZE, "??");
1075ec92e61fSAlan Somers 		} else if (p->laddr->address.ss_len == 0) {
1076ec92e61fSAlan Somers 			struct file *f;
1077ec92e61fSAlan Somers 			f = RB_FIND(files_t, &ftree,
1078ec92e61fSAlan Somers 				&(struct file){ .xf_data =
1079ec92e61fSAlan Somers 				p->socket });
1080ec92e61fSAlan Somers 			if (f != NULL) {
1081ec92e61fSAlan Somers 				if (!is_xo_style_encoding) {
1082ec92e61fSAlan Somers 					pos += snprintf(SAFEBUF, SAFESIZE,
1083ec92e61fSAlan Somers 						"[%lu %d]", (u_long)f->xf_pid,
1084ec92e61fSAlan Somers 						f->xf_fd);
1085ec92e61fSAlan Somers 				} else {
1086ec92e61fSAlan Somers 					xo_open_list("connections");
1087ec92e61fSAlan Somers 					xo_open_instance("connections");
1088ec92e61fSAlan Somers 					xo_emit("{:pid/%lu}", (u_long)f->xf_pid);
1089ec92e61fSAlan Somers 					xo_emit("{:fd/%d}", f->xf_fd);
1090ec92e61fSAlan Somers 					xo_close_instance("connections");
1091ec92e61fSAlan Somers 					xo_close_list("connections");
1092ec92e61fSAlan Somers 				}
1093ec92e61fSAlan Somers 			}
1094ec92e61fSAlan Somers 		} else
1095ec92e61fSAlan Somers 			pos += formataddr(&p->laddr->address,
1096ec92e61fSAlan Somers 				SAFEBUF, SAFESIZE);
1097ec92e61fSAlan Somers 	} else if (faddr->firstref != 0) {
1098ec92e61fSAlan Somers 		/* Remote peer(s) connect(2)ed to us, if any. */
1099ec92e61fSAlan Somers 		struct sock *p;
1100ec92e61fSAlan Somers 		struct file *f;
1101ec92e61fSAlan Somers 		kvaddr_t ref = faddr->firstref;
1102ec92e61fSAlan Somers 		bool fref = true;
1103ec92e61fSAlan Somers 
1104ec92e61fSAlan Somers 		if (!is_xo_style_encoding)
1105ec92e61fSAlan Somers 			pos += snprintf(SAFEBUF, SAFESIZE, " <- ");
1106ec92e61fSAlan Somers 		xo_open_list("connections");
1107ec92e61fSAlan Somers 		while ((p = RB_FIND(pcbs_t, &pcbs,
1108ec92e61fSAlan Somers 			&(struct sock){ .pcb = ref })) != 0) {
1109ec92e61fSAlan Somers 			f = RB_FIND(files_t, &ftree,
1110ec92e61fSAlan Somers 				&(struct file){ .xf_data = p->socket });
1111ec92e61fSAlan Somers 			if (f != NULL) {
1112ec92e61fSAlan Somers 				if (!is_xo_style_encoding) {
1113ec92e61fSAlan Somers 					pos += snprintf(SAFEBUF, SAFESIZE,
1114ec92e61fSAlan Somers 						"%s[%lu %d]", fref ? "" : ",",
1115ec92e61fSAlan Somers 						(u_long)f->xf_pid, f->xf_fd);
1116ec92e61fSAlan Somers 				} else {
1117ec92e61fSAlan Somers 					xo_open_instance("connections");
1118ec92e61fSAlan Somers 					xo_emit("{:pid/%lu}", (u_long)f->xf_pid);
1119ec92e61fSAlan Somers 					xo_emit("{:fd/%d}", f->xf_fd);
1120ec92e61fSAlan Somers 					xo_close_instance("connections");
1121ec92e61fSAlan Somers 				}
1122ec92e61fSAlan Somers 			}
1123ec92e61fSAlan Somers 			ref = p->faddr->nextref;
1124ec92e61fSAlan Somers 			fref = false;
1125ec92e61fSAlan Somers 		}
1126ec92e61fSAlan Somers 		xo_close_list("connections");
1127ec92e61fSAlan Somers 	}
1128ec92e61fSAlan Somers 	return pos;
1129ec92e61fSAlan Somers }
1130ec92e61fSAlan Somers 
1131ec92e61fSAlan Somers struct col_widths {
1132ec92e61fSAlan Somers 	int user;
1133ec92e61fSAlan Somers 	int command;
1134ec92e61fSAlan Somers 	int pid;
1135ec92e61fSAlan Somers 	int fd;
1136ec92e61fSAlan Somers 	int proto;
1137ec92e61fSAlan Somers 	int local_addr;
1138ec92e61fSAlan Somers 	int foreign_addr;
1139ec92e61fSAlan Somers 	int pcb_kva;
1140ec92e61fSAlan Somers 	int fib;
1141ec92e61fSAlan Somers 	int splice_address;
1142ec92e61fSAlan Somers 	int inp_gencnt;
1143ec92e61fSAlan Somers 	int encaps;
1144ec92e61fSAlan Somers 	int path_state;
1145ec92e61fSAlan Somers 	int conn_state;
1146ec92e61fSAlan Somers 	int stack;
1147ec92e61fSAlan Somers 	int cc;
1148ec92e61fSAlan Somers };
1149ec92e61fSAlan Somers 
1150ec92e61fSAlan Somers static void
calculate_sock_column_widths(struct col_widths * cw,struct sock * s)1151ec92e61fSAlan Somers calculate_sock_column_widths(struct col_widths *cw, struct sock *s)
1152ec92e61fSAlan Somers {
1153ec92e61fSAlan Somers 	struct addr *laddr, *faddr;
1154ec92e61fSAlan Somers 	bool first = true;
1155ec92e61fSAlan Somers 	int len = 0;
1156ec92e61fSAlan Somers 	laddr = s->laddr;
1157ec92e61fSAlan Somers 	faddr = s->faddr;
1158ec92e61fSAlan Somers 	first = true;
1159ec92e61fSAlan Somers 
1160ec92e61fSAlan Somers 	len = strlen(s->protoname);
1161ec92e61fSAlan Somers 	if (s->vflag & (INP_IPV4 | INP_IPV6))
1162ec92e61fSAlan Somers 		len += 1;
1163ec92e61fSAlan Somers 	cw->proto = MAX(cw->proto, len);
1164ec92e61fSAlan Somers 
1165ec92e61fSAlan Somers 	while (laddr != NULL || faddr != NULL) {
1166ec92e61fSAlan Somers 		if (opt_w && s->family == AF_UNIX) {
1167ec92e61fSAlan Somers 			if ((laddr == NULL) || (faddr == NULL))
1168ec92e61fSAlan Somers 				xo_errx(1, "laddr = %p or faddr = %p is NULL",
1169ec92e61fSAlan Somers 					(void *)laddr, (void *)faddr);
1170ec92e61fSAlan Somers 			if (laddr->address.ss_len > 0)
1171ec92e61fSAlan Somers 				len = formataddr(&laddr->address, NULL, 0);
1172ec92e61fSAlan Somers 			cw->local_addr = MAX(cw->local_addr, len);
1173ec92e61fSAlan Somers 			len = format_unix_faddr(faddr, NULL, 0);
1174ec92e61fSAlan Somers 			cw->foreign_addr = MAX(cw->foreign_addr, len);
1175ec92e61fSAlan Somers 		} else if (opt_w) {
1176ec92e61fSAlan Somers 			if (laddr != NULL) {
1177ec92e61fSAlan Somers 				len = formataddr(&laddr->address, NULL, 0);
1178ec92e61fSAlan Somers 				cw->local_addr = MAX(cw->local_addr, len);
1179ec92e61fSAlan Somers 			}
1180ec92e61fSAlan Somers 			if (faddr != NULL) {
1181ec92e61fSAlan Somers 				len = formataddr(&faddr->address, NULL, 0);
1182ec92e61fSAlan Somers 				cw->foreign_addr = MAX(cw->foreign_addr, len);
1183ec92e61fSAlan Somers 			}
1184ec92e61fSAlan Somers 		}
1185ec92e61fSAlan Somers 		if (opt_f) {
1186ec92e61fSAlan Somers 			len = snprintf(NULL, 0, "%d", s->fibnum);
1187ec92e61fSAlan Somers 			cw->fib = MAX(cw->fib, len);
1188ec92e61fSAlan Somers 		}
1189ec92e61fSAlan Somers 		if (opt_I) {
1190ec92e61fSAlan Somers 			if (s->splice_socket != 0) {
1191ec92e61fSAlan Somers 				struct sock *sp;
1192ec92e61fSAlan Somers 
1193ec92e61fSAlan Somers 				sp = RB_FIND(socks_t, &socks, &(struct sock)
1194ec92e61fSAlan Somers 					{ .socket = s->splice_socket });
1195ec92e61fSAlan Somers 				if (sp != NULL) {
1196ec92e61fSAlan Somers 					len = formataddr(&sp->laddr->address,
1197ec92e61fSAlan Somers 						 NULL, 0);
1198ec92e61fSAlan Somers 					cw->splice_address = MAX(
1199ec92e61fSAlan Somers 						cw->splice_address, len);
1200ec92e61fSAlan Somers 				}
1201ec92e61fSAlan Somers 			}
1202ec92e61fSAlan Somers 		}
1203ec92e61fSAlan Somers 		if (opt_i) {
1204ec92e61fSAlan Somers 			if (s->proto == IPPROTO_TCP || s->proto == IPPROTO_UDP)
1205ec92e61fSAlan Somers 			{
1206ec92e61fSAlan Somers 				len = snprintf(NULL, 0,
1207ec92e61fSAlan Somers 					"%" PRIu64, s->inp_gencnt);
1208ec92e61fSAlan Somers 				cw->inp_gencnt = MAX(cw->inp_gencnt, len);
1209ec92e61fSAlan Somers 			}
1210ec92e61fSAlan Somers 		}
1211ec92e61fSAlan Somers 		if (opt_U) {
1212ec92e61fSAlan Somers 			if (faddr != NULL &&
1213ec92e61fSAlan Somers 				((s->proto == IPPROTO_SCTP &&
1214ec92e61fSAlan Somers 					s->state != SCTP_CLOSED &&
1215ec92e61fSAlan Somers 					s->state != SCTP_BOUND &&
1216ec92e61fSAlan Somers 					s->state != SCTP_LISTEN) ||
1217ec92e61fSAlan Somers 					(s->proto == IPPROTO_TCP &&
1218ec92e61fSAlan Somers 					s->state != TCPS_CLOSED &&
1219ec92e61fSAlan Somers 					s->state != TCPS_LISTEN))) {
1220ec92e61fSAlan Somers 				len = snprintf(NULL, 0, "%u",
1221ec92e61fSAlan Somers 					ntohs(faddr->encaps_port));
1222ec92e61fSAlan Somers 				cw->encaps = MAX(cw->encaps, len);
1223ec92e61fSAlan Somers 			}
1224ec92e61fSAlan Somers 		}
1225ec92e61fSAlan Somers 		if (opt_s) {
1226ec92e61fSAlan Somers 			if (faddr != NULL &&
1227ec92e61fSAlan Somers 				s->proto == IPPROTO_SCTP &&
1228ec92e61fSAlan Somers 				s->state != SCTP_CLOSED &&
1229ec92e61fSAlan Somers 				s->state != SCTP_BOUND &&
1230ec92e61fSAlan Somers 				s->state != SCTP_LISTEN) {
1231ec92e61fSAlan Somers 				len = strlen(sctp_path_state(faddr->state));
1232ec92e61fSAlan Somers 				cw->path_state = MAX(cw->path_state, len);
1233ec92e61fSAlan Somers 			}
1234ec92e61fSAlan Somers 		}
1235ec92e61fSAlan Somers 		if (first) {
1236ec92e61fSAlan Somers 			if (opt_s) {
1237ec92e61fSAlan Somers 				if (s->proto == IPPROTO_SCTP ||
1238ec92e61fSAlan Somers 					s->proto == IPPROTO_TCP) {
1239ec92e61fSAlan Somers 					switch (s->proto) {
1240ec92e61fSAlan Somers 					case IPPROTO_SCTP:
1241ec92e61fSAlan Somers 						len = strlen(
1242ec92e61fSAlan Somers 						    sctp_conn_state(s->state));
1243ec92e61fSAlan Somers 						cw->conn_state = MAX(
1244ec92e61fSAlan Somers 							cw->conn_state, len);
1245ec92e61fSAlan Somers 						break;
1246ec92e61fSAlan Somers 					case IPPROTO_TCP:
1247ec92e61fSAlan Somers 						if (s->state >= 0 &&
1248ec92e61fSAlan Somers 						    s->state < TCP_NSTATES) {
1249ec92e61fSAlan Somers 						    len = strlen(
1250ec92e61fSAlan Somers 							tcpstates[s->state]);
1251ec92e61fSAlan Somers 						    cw->conn_state = MAX(
1252ec92e61fSAlan Somers 							cw->conn_state, len);
1253ec92e61fSAlan Somers 						}
1254ec92e61fSAlan Somers 						break;
1255ec92e61fSAlan Somers 					}
1256ec92e61fSAlan Somers 				}
1257ec92e61fSAlan Somers 			}
1258ec92e61fSAlan Somers 			if (opt_S && s->proto == IPPROTO_TCP) {
1259ec92e61fSAlan Somers 				len = strlen(s->stack);
1260ec92e61fSAlan Somers 				cw->stack = MAX(cw->stack, len);
1261ec92e61fSAlan Somers 			}
1262ec92e61fSAlan Somers 			if (opt_C && s->proto == IPPROTO_TCP) {
1263ec92e61fSAlan Somers 				len = strlen(s->cc);
1264ec92e61fSAlan Somers 				cw->cc = MAX(cw->cc, len);
1265ec92e61fSAlan Somers 			}
1266ec92e61fSAlan Somers 		}
1267ec92e61fSAlan Somers 		if (laddr != NULL)
1268ec92e61fSAlan Somers 			laddr = laddr->next;
1269ec92e61fSAlan Somers 		if (faddr != NULL)
1270ec92e61fSAlan Somers 			faddr = faddr->next;
1271ec92e61fSAlan Somers 		first = false;
1272ec92e61fSAlan Somers 	}
1273ec92e61fSAlan Somers }
1274ec92e61fSAlan Somers 
1275ec92e61fSAlan Somers static void
calculate_column_widths(struct col_widths * cw)1276ec92e61fSAlan Somers calculate_column_widths(struct col_widths *cw)
1277ec92e61fSAlan Somers {
1278ec92e61fSAlan Somers 	int n, len;
1279ec92e61fSAlan Somers 	struct file *xf;
1280ec92e61fSAlan Somers 	struct sock *s;
1281ec92e61fSAlan Somers 	struct passwd *pwd;
1282ec92e61fSAlan Somers 
1283ec92e61fSAlan Somers 	cap_setpassent(cappwd, 1);
1284ec92e61fSAlan Somers 	for (xf = files, n = 0; n < nfiles; ++n, ++xf) {
1285ec92e61fSAlan Somers 		if (xf->xf_data == 0)
1286ec92e61fSAlan Somers 			continue;
1287ec92e61fSAlan Somers 		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1288ec92e61fSAlan Somers 			continue;
1289ec92e61fSAlan Somers 		s = RB_FIND(socks_t, &socks,
1290ec92e61fSAlan Somers 			&(struct sock){ .socket = xf->xf_data});
1291ec92e61fSAlan Somers 		if (s == NULL || (!check_ports(s)))
1292ec92e61fSAlan Somers 			continue;
1293ec92e61fSAlan Somers 		s->shown = 1;
1294ec92e61fSAlan Somers 		if (opt_n ||
1295ec92e61fSAlan Somers 			(pwd = cap_getpwuid(cappwd, xf->xf_uid)) == NULL)
1296ec92e61fSAlan Somers 			len = snprintf(NULL, 0, "%lu", (u_long)xf->xf_uid);
1297ec92e61fSAlan Somers 		else
1298ec92e61fSAlan Somers 			len = snprintf(NULL, 0, "%s", pwd->pw_name);
1299ec92e61fSAlan Somers 		cw->user = MAX(cw->user, len);
1300ec92e61fSAlan Somers 		len = snprintf(NULL, 0, "%lu", (u_long)xf->xf_pid);
1301ec92e61fSAlan Somers 		cw->pid = MAX(cw->pid, len);
1302ec92e61fSAlan Somers 		len = snprintf(NULL, 0, "%d", xf->xf_fd);
1303ec92e61fSAlan Somers 		cw->fd = MAX(cw->fd, len);
1304ec92e61fSAlan Somers 
1305ec92e61fSAlan Somers 		calculate_sock_column_widths(cw, s);
1306ec92e61fSAlan Somers 	}
1307ec92e61fSAlan Somers 	if (opt_j >= 0)
1308ec92e61fSAlan Somers 		return;
1309ec92e61fSAlan Somers 	SLIST_FOREACH(s, &nosocks, socket_list) {
1310ec92e61fSAlan Somers 		if (!check_ports(s))
1311ec92e61fSAlan Somers 			continue;
1312ec92e61fSAlan Somers 		calculate_sock_column_widths(cw, s);
1313ec92e61fSAlan Somers 	}
1314ec92e61fSAlan Somers 	RB_FOREACH(s, socks_t, &socks) {
1315ec92e61fSAlan Somers 		if (s->shown)
1316ec92e61fSAlan Somers 			continue;
1317ec92e61fSAlan Somers 		if (!check_ports(s))
1318ec92e61fSAlan Somers 			continue;
1319ec92e61fSAlan Somers 		calculate_sock_column_widths(cw, s);
1320ec92e61fSAlan Somers 	}
1321ec92e61fSAlan Somers }
1322ec92e61fSAlan Somers 
1323ec92e61fSAlan Somers static void
display_sock(struct sock * s,struct col_widths * cw,char * buf,size_t bufsize)1324ec92e61fSAlan Somers display_sock(struct sock *s, struct col_widths *cw, char *buf, size_t bufsize)
1325ec92e61fSAlan Somers {
1326ec92e61fSAlan Somers 	struct addr *laddr, *faddr;
1327ec92e61fSAlan Somers 	bool first;
1328ec92e61fSAlan Somers 	laddr = s->laddr;
1329ec92e61fSAlan Somers 	faddr = s->faddr;
1330ec92e61fSAlan Somers 	first = true;
1331ec92e61fSAlan Somers 
1332ec92e61fSAlan Somers 	snprintf(buf, bufsize, "%s%s%s",
1333ec92e61fSAlan Somers 		s->protoname,
1334ec92e61fSAlan Somers 		s->vflag & INP_IPV4 ? "4" : "",
1335ec92e61fSAlan Somers 		s->vflag & INP_IPV6 ? "6" : "");
1336ec92e61fSAlan Somers 	xo_emit(" {:proto/%-*s}", cw->proto, buf);
1337ec92e61fSAlan Somers 	while (laddr != NULL || faddr != NULL) {
1338ec92e61fSAlan Somers 		if (s->family == AF_UNIX) {
1339ec92e61fSAlan Somers 			if ((laddr == NULL) || (faddr == NULL))
1340ec92e61fSAlan Somers 				xo_errx(1, "laddr = %p or faddr = %p is NULL",
1341ec92e61fSAlan Somers 					(void *)laddr, (void *)faddr);
1342ec92e61fSAlan Somers 			if (laddr->address.ss_len > 0) {
1343ec92e61fSAlan Somers 				xo_open_container("local");
1344ec92e61fSAlan Somers 				formataddr(&laddr->address, buf, bufsize);
1345ec92e61fSAlan Somers 				if (!is_xo_style_encoding) {
1346ec92e61fSAlan Somers 					xo_emit(" {:local-address/%-*.*s}",
1347ec92e61fSAlan Somers 						cw->local_addr, cw->local_addr,
1348ec92e61fSAlan Somers 						buf);
1349ec92e61fSAlan Somers 				}
1350ec92e61fSAlan Somers 				xo_close_container("local");
1351ec92e61fSAlan Somers 			} else if (laddr->address.ss_len == 0 &&
1352ec92e61fSAlan Somers 				faddr->conn == 0 && !is_xo_style_encoding) {
1353ec92e61fSAlan Somers 				xo_emit(" {:local-address/%-*.*s}",
1354ec92e61fSAlan Somers 					cw->local_addr,	cw->local_addr,
1355ec92e61fSAlan Somers 					"(not connected)");
1356ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding) {
1357ec92e61fSAlan Somers 				xo_emit(" {:local-address/%-*.*s}",
1358ec92e61fSAlan Somers 					cw->local_addr, cw->local_addr, "??");
1359ec92e61fSAlan Somers 			}
1360ec92e61fSAlan Somers 			if (faddr->conn != 0 || faddr->firstref != 0) {
1361ec92e61fSAlan Somers 				xo_open_container("foreign");
1362ec92e61fSAlan Somers 				int len = format_unix_faddr(faddr, buf,
1363ec92e61fSAlan Somers 						bufsize);
1364ec92e61fSAlan Somers 				if (len == 0 && !is_xo_style_encoding)
1365ec92e61fSAlan Somers 					xo_emit(" {:foreign-address/%-*s}",
1366ec92e61fSAlan Somers 						cw->foreign_addr, "??");
1367ec92e61fSAlan Somers 				else if (!is_xo_style_encoding)
1368ec92e61fSAlan Somers 					xo_emit(" {:foreign-address/%-*.*s}",
1369ec92e61fSAlan Somers 						cw->foreign_addr,
1370ec92e61fSAlan Somers 						cw->foreign_addr, buf);
1371ec92e61fSAlan Somers 				xo_close_container("foreign");
1372ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1373ec92e61fSAlan Somers 				xo_emit(" {:foreign-address/%-*s}",
1374ec92e61fSAlan Somers 					cw->foreign_addr, "??");
1375ec92e61fSAlan Somers 		} else {
1376ec92e61fSAlan Somers 			if (laddr != NULL) {
1377ec92e61fSAlan Somers 				xo_open_container("local");
1378ec92e61fSAlan Somers 				formataddr(&laddr->address, buf, bufsize);
1379ec92e61fSAlan Somers 				if (!is_xo_style_encoding) {
1380ec92e61fSAlan Somers 					xo_emit(" {:local-address/%-*.*s}",
1381ec92e61fSAlan Somers 						cw->local_addr, cw->local_addr,
1382ec92e61fSAlan Somers 						buf);
1383ec92e61fSAlan Somers 				}
1384ec92e61fSAlan Somers 				xo_close_container("local");
1385ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1386ec92e61fSAlan Somers 				xo_emit(" {:local-address/%-*.*s}",
1387ec92e61fSAlan Somers 					cw->local_addr, cw->local_addr, "??");
1388ec92e61fSAlan Somers 			if (faddr != NULL) {
1389ec92e61fSAlan Somers 				xo_open_container("foreign");
1390ec92e61fSAlan Somers 				formataddr(&faddr->address, buf, bufsize);
1391ec92e61fSAlan Somers 				if (!is_xo_style_encoding) {
1392ec92e61fSAlan Somers 					xo_emit(" {:foreign-address/%-*.*s}",
1393ec92e61fSAlan Somers 						cw->foreign_addr,
1394ec92e61fSAlan Somers 						cw->foreign_addr, buf);
1395ec92e61fSAlan Somers 				}
1396ec92e61fSAlan Somers 				xo_close_container("foreign");
1397ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding) {
1398ec92e61fSAlan Somers 				xo_emit(" {:foreign-address/%-*.*s}",
1399ec92e61fSAlan Somers 					cw->foreign_addr, cw->foreign_addr,
1400ec92e61fSAlan Somers 					"??");
1401ec92e61fSAlan Somers 			}
1402ec92e61fSAlan Somers 		}
1403ec92e61fSAlan Somers 		if (opt_A) {
1404ec92e61fSAlan Somers 			snprintf(buf, bufsize, "%#*" PRIx64,
1405ec92e61fSAlan Somers 				cw->pcb_kva, s->pcb);
1406ec92e61fSAlan Somers 			xo_emit(" {:pcb-kva/%s}", buf);
1407ec92e61fSAlan Somers 		}
1408ec92e61fSAlan Somers 		if (opt_f)
1409ec92e61fSAlan Somers 			xo_emit(" {:fib/%*d}", cw->fib, s->fibnum);
1410ec92e61fSAlan Somers 		if (opt_I) {
1411ec92e61fSAlan Somers 			if (s->splice_socket != 0) {
1412ec92e61fSAlan Somers 				struct sock *sp;
1413ec92e61fSAlan Somers 				sp = RB_FIND(socks_t, &socks, &(struct sock)
1414ec92e61fSAlan Somers 					{ .socket = s->splice_socket });
1415ec92e61fSAlan Somers 				if (sp != NULL) {
1416ec92e61fSAlan Somers 					xo_open_container("splice");
1417ec92e61fSAlan Somers 					formataddr(&sp->laddr->address,
1418ec92e61fSAlan Somers 								buf, bufsize);
1419ec92e61fSAlan Somers 					xo_close_container("splice");
1420ec92e61fSAlan Somers 				} else if (!is_xo_style_encoding)
1421ec92e61fSAlan Somers 					strlcpy(buf, "??", bufsize);
1422ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1423ec92e61fSAlan Somers 				strlcpy(buf, "??", bufsize);
1424ec92e61fSAlan Somers 			if (!is_xo_style_encoding)
1425ec92e61fSAlan Somers 				xo_emit(" {:splice-address/%-*s}",
1426ec92e61fSAlan Somers 					cw->splice_address, buf);
1427ec92e61fSAlan Somers 		}
1428ec92e61fSAlan Somers 		if (opt_i) {
1429ec92e61fSAlan Somers 			if (s->proto == IPPROTO_TCP || s->proto == IPPROTO_UDP)
1430ec92e61fSAlan Somers 			{
1431ec92e61fSAlan Somers 				snprintf(buf, bufsize, "%" PRIu64,
1432ec92e61fSAlan Somers 					s->inp_gencnt);
1433ec92e61fSAlan Somers 				xo_emit(" {:id/%*s}", cw->inp_gencnt, buf);
1434ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1435ec92e61fSAlan Somers 				xo_emit(" {:id/%*s}", cw->inp_gencnt, "??");
1436ec92e61fSAlan Somers 		}
1437ec92e61fSAlan Somers 		if (opt_U) {
1438ec92e61fSAlan Somers 			if (faddr != NULL &&
1439ec92e61fSAlan Somers 				((s->proto == IPPROTO_SCTP &&
1440ec92e61fSAlan Somers 					s->state != SCTP_CLOSED &&
1441ec92e61fSAlan Somers 					s->state != SCTP_BOUND &&
1442ec92e61fSAlan Somers 					s->state != SCTP_LISTEN) ||
1443ec92e61fSAlan Somers 					(s->proto == IPPROTO_TCP &&
1444ec92e61fSAlan Somers 					s->state != TCPS_CLOSED &&
1445ec92e61fSAlan Somers 					s->state != TCPS_LISTEN))) {
1446ec92e61fSAlan Somers 				xo_emit(" {:encaps/%*u}", cw->encaps,
1447ec92e61fSAlan Somers 					ntohs(faddr->encaps_port));
1448ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1449ec92e61fSAlan Somers 				xo_emit(" {:encaps/%*s}", cw->encaps, "??");
1450ec92e61fSAlan Somers 		}
1451ec92e61fSAlan Somers 		if (opt_s) {
1452ec92e61fSAlan Somers 			if (faddr != NULL &&
1453ec92e61fSAlan Somers 				s->proto == IPPROTO_SCTP &&
1454ec92e61fSAlan Somers 				s->state != SCTP_CLOSED &&
1455ec92e61fSAlan Somers 				s->state != SCTP_BOUND &&
1456ec92e61fSAlan Somers 				s->state != SCTP_LISTEN) {
1457ec92e61fSAlan Somers 				xo_emit(" {:path-state/%-*s}", cw->path_state,
1458ec92e61fSAlan Somers 					sctp_path_state(faddr->state));
1459ec92e61fSAlan Somers 			} else if (!is_xo_style_encoding)
1460ec92e61fSAlan Somers 				xo_emit(" {:path-state/%-*s}", cw->path_state,
1461ec92e61fSAlan Somers 					"??");
1462ec92e61fSAlan Somers 		}
1463ec92e61fSAlan Somers 		if (first) {
1464ec92e61fSAlan Somers 			if (opt_s) {
1465ec92e61fSAlan Somers 				if (s->proto == IPPROTO_SCTP ||
1466ec92e61fSAlan Somers 				    s->proto == IPPROTO_TCP) {
1467ec92e61fSAlan Somers 					switch (s->proto) {
1468ec92e61fSAlan Somers 					case IPPROTO_SCTP:
1469ec92e61fSAlan Somers 						xo_emit(" {:conn-state/%-*s}",
1470ec92e61fSAlan Somers 							cw->conn_state,
1471ec92e61fSAlan Somers 							sctp_conn_state(s->state));
1472ec92e61fSAlan Somers 						break;
1473ec92e61fSAlan Somers 					case IPPROTO_TCP:
1474ec92e61fSAlan Somers 						if (s->state >= 0 &&
1475ec92e61fSAlan Somers 							s->state < TCP_NSTATES)
1476ec92e61fSAlan Somers 							xo_emit(" {:conn-state/%-*s}",
1477ec92e61fSAlan Somers 								cw->conn_state,
1478ec92e61fSAlan Somers 								tcpstates[s->state]);
1479ec92e61fSAlan Somers 						else if (!is_xo_style_encoding)
1480ec92e61fSAlan Somers 							xo_emit(" {:conn-state/%-*s}",
1481ec92e61fSAlan Somers 								cw->conn_state, "??");
1482ec92e61fSAlan Somers 						break;
1483ec92e61fSAlan Somers 					}
1484ec92e61fSAlan Somers 				} else if (!is_xo_style_encoding)
1485ec92e61fSAlan Somers 					xo_emit(" {:conn-state/%-*s}",
1486ec92e61fSAlan Somers 						cw->conn_state, "??");
1487ec92e61fSAlan Somers 			}
1488ec92e61fSAlan Somers 			if (opt_S) {
1489ec92e61fSAlan Somers 				if (s->proto == IPPROTO_TCP)
1490ec92e61fSAlan Somers 					xo_emit(" {:stack/%-*s}",
1491ec92e61fSAlan Somers 						cw->stack, s->stack);
1492ec92e61fSAlan Somers 				else if (!is_xo_style_encoding)
1493ec92e61fSAlan Somers 					xo_emit(" {:stack/%-*s}",
1494ec92e61fSAlan Somers 						cw->stack, "??");
1495ec92e61fSAlan Somers 			}
1496ec92e61fSAlan Somers 			if (opt_C) {
1497ec92e61fSAlan Somers 				if (s->proto == IPPROTO_TCP)
1498ec92e61fSAlan Somers 					xo_emit(" {:cc/%-*s}", cw->cc, s->cc);
1499ec92e61fSAlan Somers 				else if (!is_xo_style_encoding)
1500ec92e61fSAlan Somers 					xo_emit(" {:cc/%-*s}", cw->cc, "??");
1501ec92e61fSAlan Somers 			}
1502ec92e61fSAlan Somers 		}
1503ec92e61fSAlan Somers 		if (laddr != NULL)
1504ec92e61fSAlan Somers 			laddr = laddr->next;
1505ec92e61fSAlan Somers 		if (faddr != NULL)
1506ec92e61fSAlan Somers 			faddr = faddr->next;
1507ec92e61fSAlan Somers 		if (!is_xo_style_encoding && (laddr != NULL || faddr != NULL))
1508ec92e61fSAlan Somers 			xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}"
1509ec92e61fSAlan Somers 				" {:fd/%*s}", cw->user, "??", cw->command, "??",
1510ec92e61fSAlan Somers 				cw->pid, "??", cw->fd, "??");
1511ec92e61fSAlan Somers 		first = false;
1512ec92e61fSAlan Somers 	}
1513ec92e61fSAlan Somers 	xo_emit("\n");
1514ec92e61fSAlan Somers }
1515ec92e61fSAlan Somers 
1516ec92e61fSAlan Somers static void
display(void)1517ec92e61fSAlan Somers display(void)
1518ec92e61fSAlan Somers {
1519ec92e61fSAlan Somers 	struct passwd *pwd;
1520ec92e61fSAlan Somers 	struct file *xf;
1521ec92e61fSAlan Somers 	struct sock *s;
1522ec92e61fSAlan Somers 	int n;
1523ec92e61fSAlan Somers 	struct col_widths cw;
1524ec92e61fSAlan Somers 	const size_t bufsize = 512;
1525ec92e61fSAlan Somers 	void *buf;
1526ec92e61fSAlan Somers 	if ((buf = (char *)malloc(bufsize)) == NULL) {
1527ec92e61fSAlan Somers 		xo_err(1, "malloc()");
1528ec92e61fSAlan Somers 		return;
1529ec92e61fSAlan Somers 	}
1530ec92e61fSAlan Somers 
1531ec92e61fSAlan Somers 	if (!is_xo_style_encoding) {
1532ec92e61fSAlan Somers 		cw = (struct col_widths) {
1533ec92e61fSAlan Somers 			.user = strlen("USER"),
1534ec92e61fSAlan Somers 			.command = 10,
1535ec92e61fSAlan Somers 			.pid = strlen("PID"),
1536ec92e61fSAlan Somers 			.fd = strlen("FD"),
1537ec92e61fSAlan Somers 			.proto = strlen("PROTO"),
1538ec92e61fSAlan Somers 			.local_addr = opt_w ? strlen("LOCAL ADDRESS") : 21,
1539ec92e61fSAlan Somers 			.foreign_addr = opt_w ? strlen("FOREIGN ADDRESS") : 21,
1540ec92e61fSAlan Somers 			.pcb_kva = 18,
1541ec92e61fSAlan Somers 			.fib = strlen("FIB"),
1542ec92e61fSAlan Somers 			.splice_address = strlen("SPLICE ADDRESS"),
1543ec92e61fSAlan Somers 			.inp_gencnt = strlen("ID"),
1544ec92e61fSAlan Somers 			.encaps = strlen("ENCAPS"),
1545ec92e61fSAlan Somers 			.path_state = strlen("PATH STATE"),
1546ec92e61fSAlan Somers 			.conn_state = strlen("CONN STATE"),
1547ec92e61fSAlan Somers 			.stack = strlen("STACK"),
1548ec92e61fSAlan Somers 			.cc = strlen("CC"),
1549ec92e61fSAlan Somers 		};
1550ec92e61fSAlan Somers 		calculate_column_widths(&cw);
1551ec92e61fSAlan Somers 	} else
1552ec92e61fSAlan Somers 		memset(&cw, 0, sizeof(cw));
1553ec92e61fSAlan Somers 
1554ec92e61fSAlan Somers 	xo_set_version(SOCKSTAT_XO_VERSION);
1555ec92e61fSAlan Somers 	xo_open_container("sockstat");
1556ec92e61fSAlan Somers 	xo_open_list("socket");
1557ec92e61fSAlan Somers 	if (!opt_q) {
1558ec92e61fSAlan Somers 		xo_emit("{T:/%-*s} {T:/%-*s} {T:/%*s} {T:/%*s} {T:/%-*s} "
1559ec92e61fSAlan Somers 			"{T:/%-*s} {T:/%-*s}", cw.user, "USER", cw.command,
1560ec92e61fSAlan Somers 			"COMMAND", cw.pid, "PID", cw.fd, "FD", cw.proto,
1561ec92e61fSAlan Somers 			"PROTO", cw.local_addr, "LOCAL ADDRESS",
1562ec92e61fSAlan Somers 			cw.foreign_addr, "FOREIGN ADDRESS");
1563ec92e61fSAlan Somers 		if (opt_A)
1564ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.pcb_kva, "PCB KVA");
1565ec92e61fSAlan Somers 		if (opt_f)
1566ec92e61fSAlan Somers 			/* RT_MAXFIBS is 65535. */
1567ec92e61fSAlan Somers 			xo_emit(" {T:/%*s}", cw.fib, "FIB");
1568ec92e61fSAlan Somers 		if (opt_I)
1569ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.splice_address,
1570ec92e61fSAlan Somers 				"SPLICE ADDRESS");
1571ec92e61fSAlan Somers 		if (opt_i)
1572ec92e61fSAlan Somers 			xo_emit(" {T:/%*s}", cw.inp_gencnt, "ID");
1573ec92e61fSAlan Somers 		if (opt_U)
1574ec92e61fSAlan Somers 			xo_emit(" {T:/%*s}", cw.encaps, "ENCAPS");
1575ec92e61fSAlan Somers 		if (opt_s) {
1576ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.path_state, "PATH STATE");
1577ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.conn_state, "CONN STATE");
1578ec92e61fSAlan Somers 		}
1579ec92e61fSAlan Somers 		if (opt_S)
1580ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.stack, "STACK");
1581ec92e61fSAlan Somers 		if (opt_C)
1582ec92e61fSAlan Somers 			xo_emit(" {T:/%-*s}", cw.cc, "CC");
1583ec92e61fSAlan Somers 		xo_emit("\n");
1584ec92e61fSAlan Somers 	}
1585ec92e61fSAlan Somers 	cap_setpassent(cappwd, 1);
1586ec92e61fSAlan Somers 	for (xf = files, n = 0; n < nfiles; ++n, ++xf) {
1587ec92e61fSAlan Somers 		if (xf->xf_data == 0)
1588ec92e61fSAlan Somers 			continue;
1589ec92e61fSAlan Somers 		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1590ec92e61fSAlan Somers 			continue;
1591ec92e61fSAlan Somers 		s = RB_FIND(socks_t, &socks,
1592ec92e61fSAlan Somers 			&(struct sock){ .socket = xf->xf_data});
1593ec92e61fSAlan Somers 		if (s != NULL && check_ports(s)) {
1594ec92e61fSAlan Somers 			xo_open_instance("socket");
1595ec92e61fSAlan Somers 			s->shown = 1;
1596ec92e61fSAlan Somers 			if (opt_n ||
1597ec92e61fSAlan Somers 			    (pwd = cap_getpwuid(cappwd, xf->xf_uid)) == NULL)
1598ec92e61fSAlan Somers 				xo_emit("{:user/%-*lu}", cw.user,
1599ec92e61fSAlan Somers 					(u_long)xf->xf_uid);
1600ec92e61fSAlan Somers 			else
1601ec92e61fSAlan Somers 				xo_emit("{:user/%-*s}", cw.user, pwd->pw_name);
1602ec92e61fSAlan Somers 			if (!is_xo_style_encoding)
1603ec92e61fSAlan Somers 				xo_emit(" {:command/%-*.10s}", cw.command,
1604ec92e61fSAlan Somers 					getprocname(xf->xf_pid));
1605ec92e61fSAlan Somers 			else
1606ec92e61fSAlan Somers 				xo_emit(" {:command/%-*s}", cw.command,
1607ec92e61fSAlan Somers 					getprocname(xf->xf_pid));
1608ec92e61fSAlan Somers 			xo_emit(" {:pid/%*lu}", cw.pid, (u_long)xf->xf_pid);
1609ec92e61fSAlan Somers 			xo_emit(" {:fd/%*d}", cw.fd, xf->xf_fd);
1610ec92e61fSAlan Somers 			display_sock(s, &cw, buf, bufsize);
1611ec92e61fSAlan Somers 			xo_close_instance("socket");
1612ec92e61fSAlan Somers 		}
1613ec92e61fSAlan Somers 	}
1614ec92e61fSAlan Somers 	if (opt_j >= 0)
1615ec92e61fSAlan Somers 		return;
1616ec92e61fSAlan Somers 	SLIST_FOREACH(s, &nosocks, socket_list) {
1617ec92e61fSAlan Somers 		if (!check_ports(s))
1618ec92e61fSAlan Somers 			continue;
1619ec92e61fSAlan Somers 		xo_open_instance("socket");
1620ec92e61fSAlan Somers 		if (!is_xo_style_encoding)
1621ec92e61fSAlan Somers 			xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}"
1622ec92e61fSAlan Somers 				" {:fd/%*s}", cw.user, "??", cw.command, "??",
1623ec92e61fSAlan Somers 				cw.pid, "??", cw.fd, "??");
1624ec92e61fSAlan Somers 		display_sock(s, &cw, buf, bufsize);
1625ec92e61fSAlan Somers 		xo_close_instance("socket");
1626ec92e61fSAlan Somers 	}
1627ec92e61fSAlan Somers 	RB_FOREACH(s, socks_t, &socks) {
1628ec92e61fSAlan Somers 		if (s->shown)
1629ec92e61fSAlan Somers 			continue;
1630ec92e61fSAlan Somers 		if (!check_ports(s))
1631ec92e61fSAlan Somers 			continue;
1632ec92e61fSAlan Somers 		xo_open_instance("socket");
1633ec92e61fSAlan Somers 		if (!is_xo_style_encoding)
1634ec92e61fSAlan Somers 			xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}"
1635ec92e61fSAlan Somers 				" {:fd/%*s}", cw.user, "??", cw.command, "??",
1636ec92e61fSAlan Somers 				cw.pid, "??", cw.fd, "??");
1637ec92e61fSAlan Somers 		display_sock(s, &cw, buf, bufsize);
1638ec92e61fSAlan Somers 		xo_close_instance("socket");
1639ec92e61fSAlan Somers 	}
1640ec92e61fSAlan Somers 	xo_close_list("socket");
1641ec92e61fSAlan Somers 	xo_close_container("sockstat");
1642ec92e61fSAlan Somers 	if (xo_finish() < 0)
1643ec92e61fSAlan Somers 		xo_err(1, "stdout");
1644ec92e61fSAlan Somers 	free(buf);
1645ec92e61fSAlan Somers 	cap_endpwent(cappwd);
1646ec92e61fSAlan Somers }
1647ec92e61fSAlan Somers 
1648ec92e61fSAlan Somers static int
set_default_protos(void)1649ec92e61fSAlan Somers set_default_protos(void)
1650ec92e61fSAlan Somers {
1651ec92e61fSAlan Somers 	struct protoent *prot;
1652ec92e61fSAlan Somers 	const char *pname;
1653ec92e61fSAlan Somers 	size_t pindex;
1654ec92e61fSAlan Somers 
1655ec92e61fSAlan Somers 	init_protos(default_numprotos);
1656ec92e61fSAlan Somers 
1657ec92e61fSAlan Somers 	for (pindex = 0; pindex < default_numprotos; pindex++) {
1658ec92e61fSAlan Somers 		pname = default_protos[pindex];
1659ec92e61fSAlan Somers 		prot = cap_getprotobyname(capnetdb, pname);
1660ec92e61fSAlan Somers 		if (prot == NULL)
1661ec92e61fSAlan Somers 			xo_err(1, "cap_getprotobyname: %s", pname);
1662ec92e61fSAlan Somers 		protos[pindex] = prot->p_proto;
1663ec92e61fSAlan Somers 	}
1664ec92e61fSAlan Somers 	numprotos = pindex;
1665ec92e61fSAlan Somers 	return (pindex);
1666ec92e61fSAlan Somers }
1667ec92e61fSAlan Somers 
1668ec92e61fSAlan Somers /*
1669ec92e61fSAlan Somers  * Return the vnet property of the jail, or -1 on error.
1670ec92e61fSAlan Somers  */
1671ec92e61fSAlan Somers static int
jail_getvnet(int jid)1672ec92e61fSAlan Somers jail_getvnet(int jid)
1673ec92e61fSAlan Somers {
1674ec92e61fSAlan Somers 	struct iovec jiov[6];
1675ec92e61fSAlan Somers 	int vnet;
1676ec92e61fSAlan Somers 	size_t len = sizeof(vnet);
1677ec92e61fSAlan Somers 
1678ec92e61fSAlan Somers 	if (sysctlbyname("kern.features.vimage", &vnet, &len, NULL, 0) != 0)
1679ec92e61fSAlan Somers 		return (0);
1680ec92e61fSAlan Somers 
1681ec92e61fSAlan Somers 	vnet = -1;
1682ec92e61fSAlan Somers 	jiov[0].iov_base = __DECONST(char *, "jid");
1683ec92e61fSAlan Somers 	jiov[0].iov_len = sizeof("jid");
1684ec92e61fSAlan Somers 	jiov[1].iov_base = &jid;
1685ec92e61fSAlan Somers 	jiov[1].iov_len = sizeof(jid);
1686ec92e61fSAlan Somers 	jiov[2].iov_base = __DECONST(char *, "vnet");
1687ec92e61fSAlan Somers 	jiov[2].iov_len = sizeof("vnet");
1688ec92e61fSAlan Somers 	jiov[3].iov_base = &vnet;
1689ec92e61fSAlan Somers 	jiov[3].iov_len = sizeof(vnet);
1690ec92e61fSAlan Somers 	jiov[4].iov_base = __DECONST(char *, "errmsg");
1691ec92e61fSAlan Somers 	jiov[4].iov_len = sizeof("errmsg");
1692ec92e61fSAlan Somers 	jiov[5].iov_base = jail_errmsg;
1693ec92e61fSAlan Somers 	jiov[5].iov_len = JAIL_ERRMSGLEN;
1694ec92e61fSAlan Somers 	jail_errmsg[0] = '\0';
1695ec92e61fSAlan Somers 	if (jail_get(jiov, nitems(jiov), 0) < 0) {
1696ec92e61fSAlan Somers 		if (!jail_errmsg[0])
1697ec92e61fSAlan Somers 			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1698ec92e61fSAlan Somers 			    "jail_get: %s", strerror(errno));
1699ec92e61fSAlan Somers 		return (-1);
1700ec92e61fSAlan Somers 	}
1701ec92e61fSAlan Somers 	return (vnet);
1702ec92e61fSAlan Somers }
1703ec92e61fSAlan Somers 
1704ec92e61fSAlan Somers static void
usage(void)1705ec92e61fSAlan Somers usage(void)
1706ec92e61fSAlan Somers {
1707ec92e61fSAlan Somers 	xo_error(
1708ec92e61fSAlan Somers "usage: sockstat [--libxo ...] [-46ACcfIiLlnqSsUuvw] [-j jid] [-p ports]\n"
1709ec92e61fSAlan Somers "                [-P protocols]\n");
1710ec92e61fSAlan Somers 	exit(1);
1711ec92e61fSAlan Somers }
1712ec92e61fSAlan Somers 
1713ec92e61fSAlan Somers int
main(int argc,char * argv[])1714ec92e61fSAlan Somers main(int argc, char *argv[])
1715ec92e61fSAlan Somers {
1716ec92e61fSAlan Somers 	cap_channel_t *capcas;
1717ec92e61fSAlan Somers 	cap_net_limit_t *limit;
1718ec92e61fSAlan Somers 	const char *pwdcmds[] = { "setpassent", "getpwuid" };
1719ec92e61fSAlan Somers 	const char *pwdfields[] = { "pw_name" };
1720ec92e61fSAlan Somers 	int protos_defined = -1;
1721*d8883177SAlan Somers 	int o, i, err;
1722ec92e61fSAlan Somers 
1723ec92e61fSAlan Somers 	argc = xo_parse_args(argc, argv);
1724ec92e61fSAlan Somers 	if (argc < 0)
1725ec92e61fSAlan Somers 		exit(1);
1726ec92e61fSAlan Somers 	if (xo_get_style(NULL) != XO_STYLE_TEXT &&
1727ec92e61fSAlan Somers 		xo_get_style(NULL) != XO_STYLE_HTML)
1728ec92e61fSAlan Somers 		is_xo_style_encoding = true;
1729ec92e61fSAlan Somers 	opt_j = -1;
1730ec92e61fSAlan Somers 	while ((o = getopt(argc, argv, "46ACcfIij:Llnp:P:qSsUuvw")) != -1)
1731ec92e61fSAlan Somers 		switch (o) {
1732ec92e61fSAlan Somers 		case '4':
1733ec92e61fSAlan Somers 			opt_4 = true;
1734ec92e61fSAlan Somers 			break;
1735ec92e61fSAlan Somers 		case '6':
1736ec92e61fSAlan Somers 			opt_6 = true;
1737ec92e61fSAlan Somers 			break;
1738ec92e61fSAlan Somers 		case 'A':
1739ec92e61fSAlan Somers 			opt_A = true;
1740ec92e61fSAlan Somers 			break;
1741ec92e61fSAlan Somers 		case 'C':
1742ec92e61fSAlan Somers 			opt_C = true;
1743ec92e61fSAlan Somers 			break;
1744ec92e61fSAlan Somers 		case 'c':
1745ec92e61fSAlan Somers 			opt_c = true;
1746ec92e61fSAlan Somers 			break;
1747ec92e61fSAlan Somers 		case 'f':
1748ec92e61fSAlan Somers 			opt_f = true;
1749ec92e61fSAlan Somers 			break;
1750ec92e61fSAlan Somers 		case 'I':
1751ec92e61fSAlan Somers 			opt_I = true;
1752ec92e61fSAlan Somers 			break;
1753ec92e61fSAlan Somers 		case 'i':
1754ec92e61fSAlan Somers 			opt_i = true;
1755ec92e61fSAlan Somers 			break;
1756ec92e61fSAlan Somers 		case 'j':
1757ec92e61fSAlan Somers 			opt_j = jail_getid(optarg);
1758ec92e61fSAlan Somers 			if (opt_j < 0)
1759ec92e61fSAlan Somers 				xo_errx(1, "jail_getid: %s", jail_errmsg);
1760ec92e61fSAlan Somers 			break;
1761ec92e61fSAlan Somers 		case 'L':
1762ec92e61fSAlan Somers 			opt_L = true;
1763ec92e61fSAlan Somers 			break;
1764ec92e61fSAlan Somers 		case 'l':
1765ec92e61fSAlan Somers 			opt_l = true;
1766ec92e61fSAlan Somers 			break;
1767ec92e61fSAlan Somers 		case 'n':
1768ec92e61fSAlan Somers 			opt_n = true;
1769ec92e61fSAlan Somers 			break;
1770ec92e61fSAlan Somers 		case 'p':
1771*d8883177SAlan Somers 			err = parse_ports(optarg);
1772*d8883177SAlan Somers 			switch (err) {
1773*d8883177SAlan Somers 			case EINVAL:
1774*d8883177SAlan Somers 				xo_errx(1, "syntax error in port range");
1775*d8883177SAlan Somers 				break;
1776*d8883177SAlan Somers 			case ERANGE:
1777*d8883177SAlan Somers 				xo_errx(1, "invalid port number");
1778*d8883177SAlan Somers 				break;
1779*d8883177SAlan Somers 			}
1780ec92e61fSAlan Somers 			break;
1781ec92e61fSAlan Somers 		case 'P':
1782ec92e61fSAlan Somers 			protos_defined = parse_protos(optarg);
1783ec92e61fSAlan Somers 			break;
1784ec92e61fSAlan Somers 		case 'q':
1785ec92e61fSAlan Somers 			opt_q = true;
1786ec92e61fSAlan Somers 			break;
1787ec92e61fSAlan Somers 		case 'S':
1788ec92e61fSAlan Somers 			opt_S = true;
1789ec92e61fSAlan Somers 			break;
1790ec92e61fSAlan Somers 		case 's':
1791ec92e61fSAlan Somers 			opt_s = true;
1792ec92e61fSAlan Somers 			break;
1793ec92e61fSAlan Somers 		case 'U':
1794ec92e61fSAlan Somers 			opt_U = true;
1795ec92e61fSAlan Somers 			break;
1796ec92e61fSAlan Somers 		case 'u':
1797ec92e61fSAlan Somers 			opt_u = true;
1798ec92e61fSAlan Somers 			break;
1799ec92e61fSAlan Somers 		case 'v':
1800ec92e61fSAlan Somers 			++opt_v;
1801ec92e61fSAlan Somers 			break;
1802ec92e61fSAlan Somers 		case 'w':
1803ec92e61fSAlan Somers 			opt_w = true;
1804ec92e61fSAlan Somers 			break;
1805ec92e61fSAlan Somers 		default:
1806ec92e61fSAlan Somers 			usage();
1807ec92e61fSAlan Somers 		}
1808ec92e61fSAlan Somers 
1809ec92e61fSAlan Somers 	argc -= optind;
1810ec92e61fSAlan Somers 	argv += optind;
1811ec92e61fSAlan Somers 
1812ec92e61fSAlan Somers 	if (argc > 0)
1813ec92e61fSAlan Somers 		usage();
1814ec92e61fSAlan Somers 
1815ec92e61fSAlan Somers 	if (opt_j > 0) {
1816ec92e61fSAlan Somers 		switch (jail_getvnet(opt_j)) {
1817ec92e61fSAlan Somers 		case -1:
1818ec92e61fSAlan Somers 			xo_errx(2, "jail_getvnet: %s", jail_errmsg);
1819ec92e61fSAlan Somers 		case JAIL_SYS_NEW:
1820ec92e61fSAlan Somers 			if (jail_attach(opt_j) < 0)
1821ec92e61fSAlan Somers 				xo_err(3, "jail_attach()");
1822ec92e61fSAlan Somers 			/* Set back to -1 for normal output in vnet jail. */
1823ec92e61fSAlan Somers 			opt_j = -1;
1824ec92e61fSAlan Somers 			break;
1825ec92e61fSAlan Somers 		default:
1826ec92e61fSAlan Somers 			break;
1827ec92e61fSAlan Somers 		}
1828ec92e61fSAlan Somers 	}
1829ec92e61fSAlan Somers 
1830ec92e61fSAlan Somers 	capcas = cap_init();
1831ec92e61fSAlan Somers 	if (capcas == NULL)
1832ec92e61fSAlan Somers 		xo_err(1, "Unable to contact Casper");
1833ec92e61fSAlan Somers 	if (caph_enter_casper() < 0)
1834ec92e61fSAlan Somers 		xo_err(1, "Unable to enter capability mode");
1835ec92e61fSAlan Somers 	capnet = cap_service_open(capcas, "system.net");
1836ec92e61fSAlan Somers 	if (capnet == NULL)
1837ec92e61fSAlan Somers 		xo_err(1, "Unable to open system.net service");
1838ec92e61fSAlan Somers 	capnetdb = cap_service_open(capcas, "system.netdb");
1839ec92e61fSAlan Somers 	if (capnetdb == NULL)
1840ec92e61fSAlan Somers 		xo_err(1, "Unable to open system.netdb service");
1841ec92e61fSAlan Somers 	capsysctl = cap_service_open(capcas, "system.sysctl");
1842ec92e61fSAlan Somers 	if (capsysctl == NULL)
1843ec92e61fSAlan Somers 		xo_err(1, "Unable to open system.sysctl service");
1844ec92e61fSAlan Somers 	cappwd = cap_service_open(capcas, "system.pwd");
1845ec92e61fSAlan Somers 	if (cappwd == NULL)
1846ec92e61fSAlan Somers 		xo_err(1, "Unable to open system.pwd service");
1847ec92e61fSAlan Somers 	cap_close(capcas);
1848ec92e61fSAlan Somers 	limit = cap_net_limit_init(capnet, CAPNET_ADDR2NAME);
1849ec92e61fSAlan Somers 	if (limit == NULL)
1850ec92e61fSAlan Somers 		xo_err(1, "Unable to init cap_net limits");
1851ec92e61fSAlan Somers 	if (cap_net_limit(limit) < 0)
1852ec92e61fSAlan Somers 		xo_err(1, "Unable to apply limits");
1853ec92e61fSAlan Somers 	if (cap_pwd_limit_cmds(cappwd, pwdcmds, nitems(pwdcmds)) < 0)
1854ec92e61fSAlan Somers 		xo_err(1, "Unable to apply pwd commands limits");
1855ec92e61fSAlan Somers 	if (cap_pwd_limit_fields(cappwd, pwdfields, nitems(pwdfields)) < 0)
1856ec92e61fSAlan Somers 		xo_err(1, "Unable to apply pwd commands limits");
1857ec92e61fSAlan Somers 
1858ec92e61fSAlan Somers 	if ((!opt_4 && !opt_6) && protos_defined != -1)
1859ec92e61fSAlan Somers 		opt_4 = opt_6 = true;
1860ec92e61fSAlan Somers 	if (!opt_4 && !opt_6 && !opt_u)
1861ec92e61fSAlan Somers 		opt_4 = opt_6 = opt_u = true;
1862ec92e61fSAlan Somers 	if ((opt_4 || opt_6) && protos_defined == -1)
1863ec92e61fSAlan Somers 		protos_defined = set_default_protos();
1864ec92e61fSAlan Somers 	if (!opt_c && !opt_l)
1865ec92e61fSAlan Somers 		opt_c = opt_l = true;
1866ec92e61fSAlan Somers 
1867ec92e61fSAlan Somers 	if (opt_4 || opt_6) {
1868ec92e61fSAlan Somers 		for (i = 0; i < protos_defined; i++)
1869ec92e61fSAlan Somers 			if (protos[i] == IPPROTO_SCTP)
1870ec92e61fSAlan Somers 				gather_sctp();
1871ec92e61fSAlan Somers 			else
1872ec92e61fSAlan Somers 				gather_inet(protos[i]);
1873ec92e61fSAlan Somers 	}
1874ec92e61fSAlan Somers 
1875ec92e61fSAlan Somers 	if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1876ec92e61fSAlan Somers 		gather_unix(SOCK_STREAM);
1877ec92e61fSAlan Somers 		gather_unix(SOCK_DGRAM);
1878ec92e61fSAlan Somers 		gather_unix(SOCK_SEQPACKET);
1879ec92e61fSAlan Somers 	}
1880ec92e61fSAlan Somers 	getfiles();
1881ec92e61fSAlan Somers 	display();
1882ec92e61fSAlan Somers 	exit(0);
1883ec92e61fSAlan Somers }
1884