xref: /freebsd/usr.bin/netstat/unix.c (revision d8d891521ea2d573f3f8d260c21aefd448df4125)
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
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes /*
399b50d902SRodney W. Grimes  * Display protocol blocks in the unix domain.
409b50d902SRodney W. Grimes  */
419b50d902SRodney W. Grimes #include <kvm.h>
429b50d902SRodney W. Grimes #include <sys/param.h>
43d8d89152SDavid Greenman #include <sys/queue.h>
449b50d902SRodney W. Grimes #include <sys/protosw.h>
459b50d902SRodney W. Grimes #include <sys/socket.h>
469b50d902SRodney W. Grimes #include <sys/socketvar.h>
479b50d902SRodney W. Grimes #include <sys/mbuf.h>
489b50d902SRodney W. Grimes #include <sys/sysctl.h>
499b50d902SRodney W. Grimes #include <sys/un.h>
509b50d902SRodney W. Grimes #include <sys/unpcb.h>
519b50d902SRodney W. Grimes #define KERNEL
529b50d902SRodney W. Grimes struct uio;
539b50d902SRodney W. Grimes struct proc;
549b50d902SRodney W. Grimes #include <sys/file.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes #include <netinet/in.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include "netstat.h"
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes static	void unixdomainpr __P((struct socket *, caddr_t));
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes static struct	file *file, *fileNFILE;
659b50d902SRodney W. Grimes static int	nfiles;
669b50d902SRodney W. Grimes extern	kvm_t *kvmd;
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes void
699b50d902SRodney W. Grimes unixpr(off)
709b50d902SRodney W. Grimes 	u_long	off;
719b50d902SRodney W. Grimes {
729b50d902SRodney W. Grimes 	register struct file *fp;
739b50d902SRodney W. Grimes 	struct socket sock, *so = &sock;
749b50d902SRodney W. Grimes 	char *filebuf;
759b50d902SRodney W. Grimes 	struct protosw *unixsw = (struct protosw *)off;
769b50d902SRodney W. Grimes 
779b50d902SRodney W. Grimes 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
789b50d902SRodney W. Grimes 	if (filebuf == 0) {
799b50d902SRodney W. Grimes 		printf("Out of memory (file table).\n");
809b50d902SRodney W. Grimes 		return;
819b50d902SRodney W. Grimes 	}
829b50d902SRodney W. Grimes 	file = (struct file *)(filebuf + sizeof(fp));
839b50d902SRodney W. Grimes 	fileNFILE = file + nfiles;
849b50d902SRodney W. Grimes 	for (fp = file; fp < fileNFILE; fp++) {
859b50d902SRodney W. Grimes 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
869b50d902SRodney W. Grimes 			continue;
879b50d902SRodney W. Grimes 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
889b50d902SRodney W. Grimes 			continue;
899b50d902SRodney W. Grimes 		/* kludge */
909b50d902SRodney W. Grimes 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
919b50d902SRodney W. Grimes 			if (so->so_pcb)
929b50d902SRodney W. Grimes 				unixdomainpr(so, fp->f_data);
939b50d902SRodney W. Grimes 	}
949b50d902SRodney W. Grimes }
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes static	char *socktype[] =
979b50d902SRodney W. Grimes     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
989b50d902SRodney W. Grimes 
999b50d902SRodney W. Grimes static void
1009b50d902SRodney W. Grimes unixdomainpr(so, soaddr)
1019b50d902SRodney W. Grimes 	register struct socket *so;
1029b50d902SRodney W. Grimes 	caddr_t soaddr;
1039b50d902SRodney W. Grimes {
1049b50d902SRodney W. Grimes 	struct unpcb unpcb, *unp = &unpcb;
1059b50d902SRodney W. Grimes 	struct mbuf mbuf, *m;
1069b50d902SRodney W. Grimes 	struct sockaddr_un *sa;
1079b50d902SRodney W. Grimes 	static int first = 1;
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
1109b50d902SRodney W. Grimes 		return;
1119b50d902SRodney W. Grimes 	if (unp->unp_addr) {
1129b50d902SRodney W. Grimes 		m = &mbuf;
1139b50d902SRodney W. Grimes 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
1149b50d902SRodney W. Grimes 			m = (struct mbuf *)0;
1159b50d902SRodney W. Grimes 		sa = (struct sockaddr_un *)(m->m_dat);
1169b50d902SRodney W. Grimes 	} else
1179b50d902SRodney W. Grimes 		m = (struct mbuf *)0;
1189b50d902SRodney W. Grimes 	if (first) {
1199b50d902SRodney W. Grimes 		printf("Active UNIX domain sockets\n");
1209b50d902SRodney W. Grimes 		printf(
1219b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1229b50d902SRodney W. Grimes 		    "Address", "Type", "Recv-Q", "Send-Q",
1239b50d902SRodney W. Grimes 		    "Inode", "Conn", "Refs", "Nextref");
1249b50d902SRodney W. Grimes 		first = 0;
1259b50d902SRodney W. Grimes 	}
1269b50d902SRodney W. Grimes 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
1279b50d902SRodney W. Grimes 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
1289b50d902SRodney W. Grimes 	    unp->unp_vnode, unp->unp_conn,
1299b50d902SRodney W. Grimes 	    unp->unp_refs, unp->unp_nextref);
1309b50d902SRodney W. Grimes 	if (m)
1319b50d902SRodney W. Grimes 		printf(" %.*s",
1329b50d902SRodney W. Grimes 		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
1339b50d902SRodney W. Grimes 		    sa->sun_path);
1349b50d902SRodney W. Grimes 	putchar('\n');
1359b50d902SRodney W. Grimes }
136