xref: /freebsd/usr.bin/netstat/unix.c (revision bc6ee716d5670df8b5bfc96f41ee8ac8fbfec695)
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 
349b50d902SRodney W. Grimes #ifndef lint
355d422d6aSPhilippe Charnier #if 0
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
375d422d6aSPhilippe Charnier #endif
385d422d6aSPhilippe Charnier static const char rcsid[] =
39bc6ee716SAndrey A. Chernov 	"$Id: unix.c,v 1.6 1998/05/15 20:19:21 wollman Exp $";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes /*
439b50d902SRodney W. Grimes  * Display protocol blocks in the unix domain.
449b50d902SRodney W. Grimes  */
459b50d902SRodney W. Grimes #include <sys/param.h>
46d8d89152SDavid Greenman #include <sys/queue.h>
479b50d902SRodney W. Grimes #include <sys/protosw.h>
489b50d902SRodney W. Grimes #include <sys/socket.h>
499b50d902SRodney W. Grimes #include <sys/socketvar.h>
509b50d902SRodney W. Grimes #include <sys/mbuf.h>
519b50d902SRodney W. Grimes #include <sys/sysctl.h>
529b50d902SRodney W. Grimes #include <sys/un.h>
539b50d902SRodney W. Grimes #include <sys/unpcb.h>
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #include <netinet/in.h>
569b50d902SRodney W. Grimes 
574f81ef50SGarrett Wollman #include <errno.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
604f81ef50SGarrett Wollman #include <kvm.h>
619b50d902SRodney W. Grimes #include "netstat.h"
629b50d902SRodney W. Grimes 
634f81ef50SGarrett Wollman static	void unixdomainpr __P((struct xunpcb *, struct xsocket *));
649b50d902SRodney W. Grimes 
654f81ef50SGarrett Wollman static	const char *const socktype[] =
669b50d902SRodney W. Grimes     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
679b50d902SRodney W. Grimes 
684f81ef50SGarrett Wollman void
694f81ef50SGarrett Wollman unixpr()
709b50d902SRodney W. Grimes {
714f81ef50SGarrett Wollman 	char 	*buf;
724f81ef50SGarrett Wollman 	int	type;
734f81ef50SGarrett Wollman 	size_t	len;
744f81ef50SGarrett Wollman 	struct	xsocket *so;
754f81ef50SGarrett Wollman 	struct	xunpgen *xug, *oxug;
764f81ef50SGarrett Wollman 	struct	xunpcb *xunp;
774f81ef50SGarrett Wollman 	char mibvar[sizeof "net.local.seqpacket.pcblist"];
784f81ef50SGarrett Wollman 
794f81ef50SGarrett Wollman 	for (type = SOCK_STREAM; type < SOCK_SEQPACKET; type++) {
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);
864f81ef50SGarrett Wollman 			continue;
874f81ef50SGarrett Wollman 		}
884f81ef50SGarrett Wollman 		if ((buf = malloc(len)) == 0) {
894f81ef50SGarrett Wollman 			warn("malloc %lu bytes", (u_long)len);
904f81ef50SGarrett Wollman 			return;
914f81ef50SGarrett Wollman 		}
924f81ef50SGarrett Wollman 		if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
934f81ef50SGarrett Wollman 			warn("sysctl: %s", mibvar);
944f81ef50SGarrett Wollman 			free(buf);
954f81ef50SGarrett Wollman 			return;
964f81ef50SGarrett Wollman 		}
974f81ef50SGarrett Wollman 
984f81ef50SGarrett Wollman 		oxug = xug = (struct xunpgen *)buf;
994f81ef50SGarrett Wollman 		for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
1004f81ef50SGarrett Wollman 		     xug->xug_len > sizeof(struct xunpgen);
1014f81ef50SGarrett Wollman 		     xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
1024f81ef50SGarrett Wollman 			xunp = (struct xunpcb *)xug;
1034f81ef50SGarrett Wollman 			so = &xunp->xu_socket;
1044f81ef50SGarrett Wollman 
1054f81ef50SGarrett Wollman 			/* Ignore PCBs which were freed during copyout. */
1064f81ef50SGarrett Wollman 			if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
1074f81ef50SGarrett Wollman 				continue;
1084f81ef50SGarrett Wollman 			unixdomainpr(xunp, so);
1094f81ef50SGarrett Wollman 		}
1104f81ef50SGarrett Wollman 		if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
1114f81ef50SGarrett Wollman 			if (oxug->xug_count > xug->xug_count) {
1124f81ef50SGarrett Wollman 				printf("Some %s sockets may have been deleted.\n",
1134f81ef50SGarrett Wollman 				       socktype[type]);
1144f81ef50SGarrett Wollman 			} else if (oxug->xug_count < xug->xug_count) {
1154f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created.\n",
1164f81ef50SGarrett Wollman 			       socktype[type]);
1174f81ef50SGarrett Wollman 			} else {
1184f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created or deleted",
1194f81ef50SGarrett Wollman 			       socktype[type]);
1204f81ef50SGarrett Wollman 			}
1214f81ef50SGarrett Wollman 		}
1224f81ef50SGarrett Wollman 		free(buf);
1234f81ef50SGarrett Wollman 	}
1244f81ef50SGarrett Wollman }
1254f81ef50SGarrett Wollman 
1264f81ef50SGarrett Wollman static void
1274f81ef50SGarrett Wollman unixdomainpr(xunp, so)
1284f81ef50SGarrett Wollman 	struct xunpcb *xunp;
1294f81ef50SGarrett Wollman 	struct xsocket *so;
1304f81ef50SGarrett Wollman {
1314f81ef50SGarrett Wollman 	struct unpcb *unp;
1324f81ef50SGarrett Wollman 	struct sockaddr_un *sa;
1339b50d902SRodney W. Grimes 	static int first = 1;
134bc6ee716SAndrey A. Chernov #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
1359b50d902SRodney W. Grimes 
1364f81ef50SGarrett Wollman 	unp = &xunp->xu_unp;
1374f81ef50SGarrett Wollman 	if (unp->unp_addr)
1384f81ef50SGarrett Wollman 		sa = &xunp->xu_addr;
1394f81ef50SGarrett Wollman 	else
140e4bb0b9aSGarrett Wollman 		sa = (struct sockaddr_un *)0;
1414f81ef50SGarrett Wollman 
1429b50d902SRodney W. Grimes 	if (first) {
1439b50d902SRodney W. Grimes 		printf("Active UNIX domain sockets\n");
1449b50d902SRodney W. Grimes 		printf(
1459b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1469b50d902SRodney W. Grimes 		    "Address", "Type", "Recv-Q", "Send-Q",
1479b50d902SRodney W. Grimes 		    "Inode", "Conn", "Refs", "Nextref");
1489b50d902SRodney W. Grimes 		first = 0;
1499b50d902SRodney W. Grimes 	}
1504f81ef50SGarrett Wollman 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
1514f81ef50SGarrett Wollman 	       (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
1524f81ef50SGarrett Wollman 	       so->so_snd.sb_cc,
1534f81ef50SGarrett Wollman 	       (long)unp->unp_vnode, (long)unp->unp_conn,
1544f81ef50SGarrett Wollman 	       (long)unp->unp_refs.lh_first, (long)unp->unp_reflink.le_next);
155e4bb0b9aSGarrett Wollman 	if (sa)
156bc6ee716SAndrey A. Chernov 		printf(" %.*s",
157bc6ee716SAndrey A. Chernov 		    sa->sun_len - offsetof(struct sockaddr_un, sun_path),
158bc6ee716SAndrey A. Chernov 		    sa->sun_path);
1599b50d902SRodney W. Grimes 	putchar('\n');
1609b50d902SRodney W. Grimes }
161