xref: /freebsd/usr.bin/netstat/unix.c (revision f1c0a78d995be6c104328276eccf5abc564cde61)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
345d422d6aSPhilippe Charnier #if 0
356cc6f122SPhilippe Charnier #ifndef lint
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
379b50d902SRodney W. Grimes #endif /* not lint */
386cc6f122SPhilippe Charnier #endif
396cc6f122SPhilippe Charnier 
406cc6f122SPhilippe Charnier #include <sys/cdefs.h>
416cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes /*
449b50d902SRodney W. Grimes  * Display protocol blocks in the unix domain.
459b50d902SRodney W. Grimes  */
469b50d902SRodney W. Grimes #include <sys/param.h>
47d8d89152SDavid Greenman #include <sys/queue.h>
489b50d902SRodney W. Grimes #include <sys/protosw.h>
499b50d902SRodney W. Grimes #include <sys/socket.h>
509b50d902SRodney W. Grimes #include <sys/socketvar.h>
519b50d902SRodney W. Grimes #include <sys/mbuf.h>
529b50d902SRodney W. Grimes #include <sys/sysctl.h>
539b50d902SRodney W. Grimes #include <sys/un.h>
549b50d902SRodney W. Grimes #include <sys/unpcb.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes #include <netinet/in.h>
579b50d902SRodney W. Grimes 
584f81ef50SGarrett Wollman #include <errno.h>
59c6620a13SPoul-Henning Kamp #include <err.h>
608ad9b1a3SGarrett Wollman #include <stddef.h>
617b95a1ebSYaroslav Tykhiy #include <stdint.h>
629b50d902SRodney W. Grimes #include <stdio.h>
639b50d902SRodney W. Grimes #include <stdlib.h>
64feda1a43SJohn Baldwin #include <strings.h>
654f81ef50SGarrett Wollman #include <kvm.h>
669b50d902SRodney W. Grimes #include "netstat.h"
679b50d902SRodney W. Grimes 
685e051718SAssar Westerlund static	void unixdomainpr(struct xunpcb *, struct xsocket *);
699b50d902SRodney W. Grimes 
704f81ef50SGarrett Wollman static	const char *const socktype[] =
719b50d902SRodney W. Grimes     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
729b50d902SRodney W. Grimes 
73feda1a43SJohn Baldwin static int
74feda1a43SJohn Baldwin pcblist_sysctl(int type, char **bufp)
759b50d902SRodney W. Grimes {
764f81ef50SGarrett Wollman 	char 	*buf;
774f81ef50SGarrett Wollman 	size_t	len;
784f81ef50SGarrett Wollman 	char mibvar[sizeof "net.local.seqpacket.pcblist"];
794f81ef50SGarrett Wollman 
804f81ef50SGarrett Wollman 	sprintf(mibvar, "net.local.%s.pcblist", socktype[type]);
814f81ef50SGarrett Wollman 
824f81ef50SGarrett Wollman 	len = 0;
834f81ef50SGarrett Wollman 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
844f81ef50SGarrett Wollman 		if (errno != ENOENT)
854f81ef50SGarrett Wollman 			warn("sysctl: %s", mibvar);
86feda1a43SJohn Baldwin 		return (-1);
874f81ef50SGarrett Wollman 	}
884f81ef50SGarrett Wollman 	if ((buf = malloc(len)) == 0) {
896cc6f122SPhilippe Charnier 		warnx("malloc %lu bytes", (u_long)len);
90feda1a43SJohn Baldwin 		return (-2);
914f81ef50SGarrett Wollman 	}
924f81ef50SGarrett Wollman 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
934f81ef50SGarrett Wollman 		warn("sysctl: %s", mibvar);
944f81ef50SGarrett Wollman 		free(buf);
95feda1a43SJohn Baldwin 		return (-2);
964f81ef50SGarrett Wollman 	}
97feda1a43SJohn Baldwin 	*bufp = buf;
98feda1a43SJohn Baldwin 	return (0);
99feda1a43SJohn Baldwin }
100feda1a43SJohn Baldwin 
101feda1a43SJohn Baldwin static int
102feda1a43SJohn Baldwin pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp)
103feda1a43SJohn Baldwin {
104feda1a43SJohn Baldwin 	struct unp_head head;
105feda1a43SJohn Baldwin 	struct unpcb *unp, unp_conn;
106feda1a43SJohn Baldwin 	u_char sun_len;
107feda1a43SJohn Baldwin 	struct socket so;
108feda1a43SJohn Baldwin 	struct xunpgen xug;
109feda1a43SJohn Baldwin 	struct xunpcb xu;
110feda1a43SJohn Baldwin 	unp_gen_t unp_gencnt;
111feda1a43SJohn Baldwin 	u_int	unp_count;
112feda1a43SJohn Baldwin 	char 	*buf, *p;
113feda1a43SJohn Baldwin 	size_t	len;
114feda1a43SJohn Baldwin 
115feda1a43SJohn Baldwin 	if (count_off == 0 || gencnt_off == 0)
116feda1a43SJohn Baldwin 		return (-2);
117feda1a43SJohn Baldwin 	if (head_off == 0)
118feda1a43SJohn Baldwin 		return (-1);
119feda1a43SJohn Baldwin 	kread(count_off, &unp_count, sizeof(unp_count));
120feda1a43SJohn Baldwin 	len = 2 * sizeof(xug) + (unp_count + unp_count / 8) * sizeof(xu);
121feda1a43SJohn Baldwin 	if ((buf = malloc(len)) == 0) {
122feda1a43SJohn Baldwin 		warnx("malloc %lu bytes", (u_long)len);
123feda1a43SJohn Baldwin 		return (-2);
124feda1a43SJohn Baldwin 	}
125feda1a43SJohn Baldwin 	p = buf;
126feda1a43SJohn Baldwin 
127feda1a43SJohn Baldwin #define	COPYOUT(obj, size) do {						\
128feda1a43SJohn Baldwin 	if (len < (size)) {						\
129feda1a43SJohn Baldwin 		warnx("buffer size exceeded");				\
130feda1a43SJohn Baldwin 		goto fail;						\
131feda1a43SJohn Baldwin 	}								\
132feda1a43SJohn Baldwin 	bcopy((obj), p, (size));					\
133feda1a43SJohn Baldwin 	len -= (size);							\
134feda1a43SJohn Baldwin 	p += (size);							\
135feda1a43SJohn Baldwin } while (0)
136feda1a43SJohn Baldwin 
137feda1a43SJohn Baldwin #define	KREAD(off, buf, len) do {					\
138feda1a43SJohn Baldwin 	if (kread((uintptr_t)(off), (buf), (len)) != 0)			\
139feda1a43SJohn Baldwin 		goto fail;						\
140feda1a43SJohn Baldwin } while (0)
141feda1a43SJohn Baldwin 
142feda1a43SJohn Baldwin 	/* Write out header. */
143feda1a43SJohn Baldwin 	kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
144feda1a43SJohn Baldwin 	xug.xug_len = sizeof xug;
145feda1a43SJohn Baldwin 	xug.xug_count = unp_count;
146feda1a43SJohn Baldwin 	xug.xug_gen = unp_gencnt;
147feda1a43SJohn Baldwin 	xug.xug_sogen = 0;
148feda1a43SJohn Baldwin 	COPYOUT(&xug, sizeof xug);
149feda1a43SJohn Baldwin 
150feda1a43SJohn Baldwin 	/* Walk the PCB list. */
151feda1a43SJohn Baldwin 	xu.xu_len = sizeof xu;
152feda1a43SJohn Baldwin 	KREAD(head_off, &head, sizeof(head));
153feda1a43SJohn Baldwin 	LIST_FOREACH(unp, &head, unp_link) {
154feda1a43SJohn Baldwin 		xu.xu_unpp = unp;
155feda1a43SJohn Baldwin 		KREAD(unp, &xu.xu_unp, sizeof (*unp));
156feda1a43SJohn Baldwin 		unp = &xu.xu_unp;
157feda1a43SJohn Baldwin 
158feda1a43SJohn Baldwin 		if (unp->unp_gencnt > unp_gencnt)
159feda1a43SJohn Baldwin 			continue;
160feda1a43SJohn Baldwin 		if (unp->unp_addr != NULL) {
161feda1a43SJohn Baldwin 			KREAD(unp->unp_addr, &sun_len, sizeof(sun_len));
162feda1a43SJohn Baldwin 			KREAD(unp->unp_addr, &xu.xu_addr, sun_len);
163feda1a43SJohn Baldwin 		}
164feda1a43SJohn Baldwin 		if (unp->unp_conn != NULL) {
165feda1a43SJohn Baldwin 			KREAD(unp->unp_conn, &unp_conn, sizeof(unp_conn));
166feda1a43SJohn Baldwin 			if (unp_conn.unp_addr != NULL) {
167feda1a43SJohn Baldwin 				KREAD(unp_conn.unp_addr, &sun_len,
168feda1a43SJohn Baldwin 				    sizeof(sun_len));
169feda1a43SJohn Baldwin 				KREAD(unp_conn.unp_addr, &xu.xu_caddr, sun_len);
170feda1a43SJohn Baldwin 			}
171feda1a43SJohn Baldwin 		}
172feda1a43SJohn Baldwin 		KREAD(unp->unp_socket, &so, sizeof(so));
173feda1a43SJohn Baldwin 		if (sotoxsocket(&so, &xu.xu_socket) != 0)
174feda1a43SJohn Baldwin 			goto fail;
175feda1a43SJohn Baldwin 		COPYOUT(&xu, sizeof(xu));
176feda1a43SJohn Baldwin 	}
177feda1a43SJohn Baldwin 
178feda1a43SJohn Baldwin 	/* Reread the counts and write the footer. */
179feda1a43SJohn Baldwin 	kread(count_off, &unp_count, sizeof(unp_count));
180feda1a43SJohn Baldwin 	kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
181feda1a43SJohn Baldwin 	xug.xug_count = unp_count;
182feda1a43SJohn Baldwin 	xug.xug_gen = unp_gencnt;
183feda1a43SJohn Baldwin 	COPYOUT(&xug, sizeof xug);
184feda1a43SJohn Baldwin 
185feda1a43SJohn Baldwin 	*bufp = buf;
186feda1a43SJohn Baldwin 	return (0);
187feda1a43SJohn Baldwin 
188feda1a43SJohn Baldwin fail:
189feda1a43SJohn Baldwin 	free(buf);
190feda1a43SJohn Baldwin 	return (-1);
191feda1a43SJohn Baldwin #undef COPYOUT
192feda1a43SJohn Baldwin #undef KREAD
193feda1a43SJohn Baldwin }
194feda1a43SJohn Baldwin 
195feda1a43SJohn Baldwin void
196feda1a43SJohn Baldwin unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off)
197feda1a43SJohn Baldwin {
198feda1a43SJohn Baldwin 	char 	*buf;
199feda1a43SJohn Baldwin 	int	ret, type;
200feda1a43SJohn Baldwin 	struct	xsocket *so;
201feda1a43SJohn Baldwin 	struct	xunpgen *xug, *oxug;
202feda1a43SJohn Baldwin 	struct	xunpcb *xunp;
203feda1a43SJohn Baldwin 
204feda1a43SJohn Baldwin 	for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) {
205feda1a43SJohn Baldwin 		if (live)
206feda1a43SJohn Baldwin 			ret = pcblist_sysctl(type, &buf);
207feda1a43SJohn Baldwin 		else
208feda1a43SJohn Baldwin 			ret = pcblist_kvm(count_off, gencnt_off,
209feda1a43SJohn Baldwin 			    type == SOCK_STREAM ? shead_off :
210feda1a43SJohn Baldwin 			    (type == SOCK_DGRAM ? dhead_off : 0), &buf);
211feda1a43SJohn Baldwin 		if (ret == -1)
212feda1a43SJohn Baldwin 			continue;
213feda1a43SJohn Baldwin 		if (ret < 0)
214feda1a43SJohn Baldwin 			return;
2154f81ef50SGarrett Wollman 
2164f81ef50SGarrett Wollman 		oxug = xug = (struct xunpgen *)buf;
2174f81ef50SGarrett Wollman 		for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
2184f81ef50SGarrett Wollman 		     xug->xug_len > sizeof(struct xunpgen);
2194f81ef50SGarrett Wollman 		     xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
2204f81ef50SGarrett Wollman 			xunp = (struct xunpcb *)xug;
2214f81ef50SGarrett Wollman 			so = &xunp->xu_socket;
2224f81ef50SGarrett Wollman 
2234f81ef50SGarrett Wollman 			/* Ignore PCBs which were freed during copyout. */
2244f81ef50SGarrett Wollman 			if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
2254f81ef50SGarrett Wollman 				continue;
2264f81ef50SGarrett Wollman 			unixdomainpr(xunp, so);
2274f81ef50SGarrett Wollman 		}
2284f81ef50SGarrett Wollman 		if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
2294f81ef50SGarrett Wollman 			if (oxug->xug_count > xug->xug_count) {
2304f81ef50SGarrett Wollman 				printf("Some %s sockets may have been deleted.\n",
2314f81ef50SGarrett Wollman 				       socktype[type]);
2324f81ef50SGarrett Wollman 			} else if (oxug->xug_count < xug->xug_count) {
2334f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created.\n",
2344f81ef50SGarrett Wollman 			       socktype[type]);
2354f81ef50SGarrett Wollman 			} else {
2364f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created or deleted",
2374f81ef50SGarrett Wollman 			       socktype[type]);
2384f81ef50SGarrett Wollman 			}
2394f81ef50SGarrett Wollman 		}
2404f81ef50SGarrett Wollman 		free(buf);
2414f81ef50SGarrett Wollman 	}
2424f81ef50SGarrett Wollman }
2434f81ef50SGarrett Wollman 
2444f81ef50SGarrett Wollman static void
2455e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
2464f81ef50SGarrett Wollman {
2474f81ef50SGarrett Wollman 	struct unpcb *unp;
2484f81ef50SGarrett Wollman 	struct sockaddr_un *sa;
2499b50d902SRodney W. Grimes 	static int first = 1;
250f1c0a78dSMaxim Konovalov 	char buf1[15];
2519b50d902SRodney W. Grimes 
2524f81ef50SGarrett Wollman 	unp = &xunp->xu_unp;
2534f81ef50SGarrett Wollman 	if (unp->unp_addr)
2544f81ef50SGarrett Wollman 		sa = &xunp->xu_addr;
2554f81ef50SGarrett Wollman 	else
256e4bb0b9aSGarrett Wollman 		sa = (struct sockaddr_un *)0;
2574f81ef50SGarrett Wollman 
258f1c0a78dSMaxim Konovalov 	if (first && !Lflag) {
2599b50d902SRodney W. Grimes 		printf("Active UNIX domain sockets\n");
2609b50d902SRodney W. Grimes 		printf(
2619b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
2629b50d902SRodney W. Grimes 		    "Address", "Type", "Recv-Q", "Send-Q",
2639b50d902SRodney W. Grimes 		    "Inode", "Conn", "Refs", "Nextref");
2649b50d902SRodney W. Grimes 		first = 0;
2659b50d902SRodney W. Grimes 	}
266f1c0a78dSMaxim Konovalov 
267f1c0a78dSMaxim Konovalov 	if (Lflag && so->so_qlimit == 0)
268f1c0a78dSMaxim Konovalov 		return;
269f1c0a78dSMaxim Konovalov 
270f1c0a78dSMaxim Konovalov 	if (Lflag) {
271f1c0a78dSMaxim Konovalov 		snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
272f1c0a78dSMaxim Konovalov 		    so->so_incqlen, so->so_qlimit);
273f1c0a78dSMaxim Konovalov 		printf("unix  %-14.14s", buf1);
274f1c0a78dSMaxim Konovalov 	} else {
275f824b518SJohn Polstra 		printf("%8lx %-6.6s %6u %6u %8lx %8lx %8lx %8lx",
2764f81ef50SGarrett Wollman 		    (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
277f1c0a78dSMaxim Konovalov 		    so->so_snd.sb_cc, (long)unp->unp_vnode, (long)unp->unp_conn,
278f1c0a78dSMaxim Konovalov 		    (long)LIST_FIRST(&unp->unp_refs),
279f1c0a78dSMaxim Konovalov 		    (long)LIST_NEXT(unp, unp_reflink));
280f1c0a78dSMaxim Konovalov 	}
281e4bb0b9aSGarrett Wollman 	if (sa)
282bc6ee716SAndrey A. Chernov 		printf(" %.*s",
28322694ebaSBruce Evans 		    (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)),
284bc6ee716SAndrey A. Chernov 		    sa->sun_path);
2859b50d902SRodney W. Grimes 	putchar('\n');
2869b50d902SRodney W. Grimes }
287