xref: /freebsd/usr.bin/netstat/unix.c (revision 963b7ccd3b045db7404a023760379bece1756504)
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
196963b7ccdSRobert Watson unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off,
197963b7ccdSRobert Watson     u_long sphead_off)
198feda1a43SJohn Baldwin {
199feda1a43SJohn Baldwin 	char 	*buf;
200feda1a43SJohn Baldwin 	int	ret, type;
201feda1a43SJohn Baldwin 	struct	xsocket *so;
202feda1a43SJohn Baldwin 	struct	xunpgen *xug, *oxug;
203feda1a43SJohn Baldwin 	struct	xunpcb *xunp;
204963b7ccdSRobert Watson 	u_long	head_off;
205feda1a43SJohn Baldwin 
206feda1a43SJohn Baldwin 	for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) {
207feda1a43SJohn Baldwin 		if (live)
208feda1a43SJohn Baldwin 			ret = pcblist_sysctl(type, &buf);
209963b7ccdSRobert Watson 		else {
210963b7ccdSRobert Watson 			head_off = 0;
211963b7ccdSRobert Watson 			switch (type) {
212963b7ccdSRobert Watson 			case SOCK_STREAM:
213963b7ccdSRobert Watson 				head_off = shead_off;
214963b7ccdSRobert Watson 				break;
215963b7ccdSRobert Watson 
216963b7ccdSRobert Watson 			case SOCK_DGRAM:
217963b7ccdSRobert Watson 				head_off = dhead_off;
218963b7ccdSRobert Watson 				break;
219963b7ccdSRobert Watson 
220963b7ccdSRobert Watson 			case SOCK_SEQPACKET:
221963b7ccdSRobert Watson 				head_off = sphead_off;
222963b7ccdSRobert Watson 				break;
223963b7ccdSRobert Watson 			}
224963b7ccdSRobert Watson 			ret = pcblist_kvm(count_off, gencnt_off, head_off,
225963b7ccdSRobert Watson 			    &buf);
226963b7ccdSRobert Watson 		}
227feda1a43SJohn Baldwin 		if (ret == -1)
228feda1a43SJohn Baldwin 			continue;
229feda1a43SJohn Baldwin 		if (ret < 0)
230feda1a43SJohn Baldwin 			return;
2314f81ef50SGarrett Wollman 
2324f81ef50SGarrett Wollman 		oxug = xug = (struct xunpgen *)buf;
2334f81ef50SGarrett Wollman 		for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
2344f81ef50SGarrett Wollman 		     xug->xug_len > sizeof(struct xunpgen);
2354f81ef50SGarrett Wollman 		     xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
2364f81ef50SGarrett Wollman 			xunp = (struct xunpcb *)xug;
2374f81ef50SGarrett Wollman 			so = &xunp->xu_socket;
2384f81ef50SGarrett Wollman 
2394f81ef50SGarrett Wollman 			/* Ignore PCBs which were freed during copyout. */
2404f81ef50SGarrett Wollman 			if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
2414f81ef50SGarrett Wollman 				continue;
2424f81ef50SGarrett Wollman 			unixdomainpr(xunp, so);
2434f81ef50SGarrett Wollman 		}
2444f81ef50SGarrett Wollman 		if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
2454f81ef50SGarrett Wollman 			if (oxug->xug_count > xug->xug_count) {
2464f81ef50SGarrett Wollman 				printf("Some %s sockets may have been deleted.\n",
2474f81ef50SGarrett Wollman 				       socktype[type]);
2484f81ef50SGarrett Wollman 			} else if (oxug->xug_count < xug->xug_count) {
2494f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created.\n",
2504f81ef50SGarrett Wollman 			       socktype[type]);
2514f81ef50SGarrett Wollman 			} else {
2524f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created or deleted",
2534f81ef50SGarrett Wollman 			       socktype[type]);
2544f81ef50SGarrett Wollman 			}
2554f81ef50SGarrett Wollman 		}
2564f81ef50SGarrett Wollman 		free(buf);
2574f81ef50SGarrett Wollman 	}
2584f81ef50SGarrett Wollman }
2594f81ef50SGarrett Wollman 
2604f81ef50SGarrett Wollman static void
2615e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
2624f81ef50SGarrett Wollman {
2634f81ef50SGarrett Wollman 	struct unpcb *unp;
2644f81ef50SGarrett Wollman 	struct sockaddr_un *sa;
2659b50d902SRodney W. Grimes 	static int first = 1;
266f1c0a78dSMaxim Konovalov 	char buf1[15];
2679b50d902SRodney W. Grimes 
2684f81ef50SGarrett Wollman 	unp = &xunp->xu_unp;
2694f81ef50SGarrett Wollman 	if (unp->unp_addr)
2704f81ef50SGarrett Wollman 		sa = &xunp->xu_addr;
2714f81ef50SGarrett Wollman 	else
272e4bb0b9aSGarrett Wollman 		sa = (struct sockaddr_un *)0;
2734f81ef50SGarrett Wollman 
274f1c0a78dSMaxim Konovalov 	if (first && !Lflag) {
2759b50d902SRodney W. Grimes 		printf("Active UNIX domain sockets\n");
2769b50d902SRodney W. Grimes 		printf(
2779b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
2789b50d902SRodney W. Grimes 		    "Address", "Type", "Recv-Q", "Send-Q",
2799b50d902SRodney W. Grimes 		    "Inode", "Conn", "Refs", "Nextref");
2809b50d902SRodney W. Grimes 		first = 0;
2819b50d902SRodney W. Grimes 	}
282f1c0a78dSMaxim Konovalov 
283f1c0a78dSMaxim Konovalov 	if (Lflag && so->so_qlimit == 0)
284f1c0a78dSMaxim Konovalov 		return;
285f1c0a78dSMaxim Konovalov 
286f1c0a78dSMaxim Konovalov 	if (Lflag) {
287f1c0a78dSMaxim Konovalov 		snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
288f1c0a78dSMaxim Konovalov 		    so->so_incqlen, so->so_qlimit);
289f1c0a78dSMaxim Konovalov 		printf("unix  %-14.14s", buf1);
290f1c0a78dSMaxim Konovalov 	} else {
291f824b518SJohn Polstra 		printf("%8lx %-6.6s %6u %6u %8lx %8lx %8lx %8lx",
2924f81ef50SGarrett Wollman 		    (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
293f1c0a78dSMaxim Konovalov 		    so->so_snd.sb_cc, (long)unp->unp_vnode, (long)unp->unp_conn,
294f1c0a78dSMaxim Konovalov 		    (long)LIST_FIRST(&unp->unp_refs),
295f1c0a78dSMaxim Konovalov 		    (long)LIST_NEXT(unp, unp_reflink));
296f1c0a78dSMaxim Konovalov 	}
297e4bb0b9aSGarrett Wollman 	if (sa)
298bc6ee716SAndrey A. Chernov 		printf(" %.*s",
29922694ebaSBruce Evans 		    (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)),
300bc6ee716SAndrey A. Chernov 		    sa->sun_path);
3019b50d902SRodney W. Grimes 	putchar('\n');
3029b50d902SRodney W. Grimes }
303