Home | History | Annotate | Line # | Download | only in netstat
      1  1.37   msaitoh /*	$NetBSD: unix.c,v 1.37 2022/09/02 06:25:43 msaitoh Exp $	*/
      2  1.13   thorpej 
      3   1.1       cgd /*-
      4  1.10   mycroft  * Copyright (c) 1983, 1988, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.17     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.13   thorpej #if 0
     35  1.13   thorpej static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
     36  1.13   thorpej #else
     37  1.37   msaitoh __RCSID("$NetBSD: unix.c,v 1.37 2022/09/02 06:25:43 msaitoh Exp $");
     38  1.13   thorpej #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd /*
     42   1.1       cgd  * Display protocol blocks in the unix domain.
     43   1.1       cgd  */
     44  1.28        ad #define _KERNEL
     45  1.28        ad #include <sys/types.h>
     46  1.28        ad #undef _KERNEL
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/protosw.h>
     49   1.1       cgd #include <sys/socket.h>
     50   1.1       cgd #include <sys/socketvar.h>
     51   1.1       cgd #include <sys/mbuf.h>
     52  1.10   mycroft #include <sys/sysctl.h>
     53   1.1       cgd #include <sys/un.h>
     54   1.1       cgd #include <sys/unpcb.h>
     55  1.35  christos #define _KMEMUSER
     56   1.1       cgd #include <sys/file.h>
     57  1.35  christos #undef _KMEMUSER
     58   1.1       cgd 
     59  1.10   mycroft #include <netinet/in.h>
     60  1.10   mycroft 
     61  1.10   mycroft #include <stdio.h>
     62  1.10   mycroft #include <stdlib.h>
     63  1.24      elad #include <string.h>
     64  1.11   deraadt #include <kvm.h>
     65  1.24      elad #include <err.h>
     66  1.10   mycroft #include "netstat.h"
     67  1.31     pooka #include "prog_ops.h"
     68  1.10   mycroft 
     69  1.24      elad static	void unixdomainprhdr(void);
     70  1.24      elad static	void unixdomainpr0(u_long, u_long, u_long, u_long, u_long, u_long,
     71  1.24      elad 			   u_long, u_long, u_long, struct sockaddr_un *, int);
     72  1.28        ad static	void unixdomainpr(struct socket *, void *);
     73  1.10   mycroft 
     74  1.10   mycroft static struct	file *file, *fileNFILE;
     75  1.25       mrg static int	ns_nfiles;
     76   1.1       cgd 
     77  1.24      elad static void
     78  1.24      elad unixdomainprhdr(void)
     79   1.1       cgd {
     80  1.24      elad 	printf("Active UNIX domain sockets\n");
     81  1.24      elad 	printf("%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
     82  1.36   msaitoh 	    "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn", "Refs",
     83  1.36   msaitoh 	    "Nextref");
     84   1.1       cgd }
     85   1.1       cgd 
     86  1.34      matt static	const char * const socktype[] =
     87   1.1       cgd     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
     88   1.1       cgd 
     89  1.10   mycroft static void
     90  1.24      elad unixdomainpr0(u_long so_pcb, u_long so_type, u_long rcvq, u_long sndq,
     91  1.24      elad 	      u_long inode, u_long conn, u_long refs, u_long nextref,
     92  1.24      elad 	      u_long addr, struct sockaddr_un *sun, int remote)
     93  1.24      elad {
     94  1.24      elad 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
     95  1.36   msaitoh 	    so_pcb, socktype[so_type], rcvq, sndq, inode, conn, refs,
     96  1.36   msaitoh 	    nextref);
     97  1.24      elad 	if (addr || remote)
     98  1.24      elad 		printf((remote ? " -> %.*s" : " %.*s"),
     99  1.36   msaitoh 		    (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
    100  1.36   msaitoh 		    sun->sun_path);
    101  1.24      elad 	putchar('\n');
    102  1.24      elad }
    103  1.24      elad 
    104  1.24      elad static void
    105  1.34      matt unixdomainpr(struct socket *so, void *soaddr)
    106   1.1       cgd {
    107  1.24      elad 	struct unpcb unp, runp;
    108  1.24      elad 	struct sockaddr_un sun, rsun;
    109   1.1       cgd 	static int first = 1;
    110  1.24      elad 	int remote = 0;
    111   1.1       cgd 
    112  1.14   mycroft 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
    113   1.1       cgd 		return;
    114  1.14   mycroft 	if (unp.unp_addr)
    115  1.14   mycroft 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
    116  1.14   mycroft 			unp.unp_addr = 0;
    117  1.24      elad 
    118  1.24      elad 	if (!unp.unp_addr) {
    119  1.24      elad 		memset(&rsun, 0, sizeof(rsun));
    120  1.24      elad 		if (unp.unp_conn &&
    121  1.24      elad 		    kread((u_long)unp.unp_conn, (char *)&runp, sizeof (runp)) == 0 &&
    122  1.24      elad 		    runp.unp_addr &&
    123  1.24      elad 		    kread((u_long)runp.unp_addr, (char *)&rsun, sizeof (rsun)) == 0 &&
    124  1.24      elad 		    rsun.sun_path[0] != '\0')
    125  1.24      elad 			remote = 1;
    126  1.24      elad 	}
    127  1.24      elad 
    128   1.1       cgd 	if (first) {
    129  1.24      elad 		unixdomainprhdr();
    130   1.1       cgd 		first = 0;
    131   1.1       cgd 	}
    132  1.24      elad 
    133  1.24      elad 	unixdomainpr0((u_long)so->so_pcb, so->so_type, so->so_rcv.sb_cc,
    134  1.24      elad 		      so->so_snd.sb_cc, (u_long)unp.unp_vnode,
    135  1.24      elad 		      (u_long)unp.unp_conn, (u_long)unp.unp_refs,
    136  1.24      elad 		      (u_long)unp.unp_nextref, (u_long)unp.unp_addr,
    137  1.24      elad 		      remote ? &rsun : &sun, remote);
    138  1.24      elad }
    139  1.24      elad 
    140  1.24      elad void
    141  1.34      matt unixpr(u_long off)
    142  1.24      elad {
    143  1.24      elad 	struct file *fp;
    144  1.24      elad 	struct socket sock, *so = &sock;
    145  1.24      elad 	char *filebuf;
    146  1.24      elad 	struct protosw *unixsw = (struct protosw *)off;
    147  1.24      elad 
    148  1.24      elad 	if (use_sysctl) {
    149  1.24      elad 		struct kinfo_pcb *pcblist;
    150  1.24      elad 		int mib[8];
    151  1.24      elad 		size_t namelen = 0, size = 0, i;
    152  1.33      manu 		const char *mibnames[] = {
    153  1.33      manu 			"net.local.stream.pcblist",
    154  1.33      manu 			"net.local.dgram.pcblist",
    155  1.33      manu 			"net.local.seqpacket.pcblist",
    156  1.33      manu 			NULL,
    157  1.33      manu 		};
    158  1.33      manu 		const char **mibname;
    159  1.24      elad 		static int first = 1;
    160  1.24      elad 
    161  1.33      manu 		for (mibname = mibnames; *mibname; mibname++) {
    162  1.33      manu 			memset(mib, 0, sizeof(mib));
    163  1.24      elad 
    164  1.37   msaitoh 			if (sysctlnametomib(*mibname, mib, &namelen) == -1)
    165  1.33      manu 				err(1, "sysctlnametomib: %s", *mibname);
    166  1.33      manu 
    167  1.33      manu 			if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib),
    168  1.33      manu 			    NULL, &size, NULL, 0) == -1)
    169  1.33      manu 				err(1, "sysctl (query)");
    170  1.33      manu 
    171  1.33      manu 			if ((pcblist = malloc(size)) == NULL)
    172  1.33      manu 				err(1, "malloc");
    173  1.33      manu 			memset(pcblist, 0, size);
    174  1.33      manu 
    175  1.33      manu 			mib[6] = sizeof(*pcblist);
    176  1.33      manu 			mib[7] = size / sizeof(*pcblist);
    177  1.33      manu 
    178  1.36   msaitoh 			if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib),
    179  1.33      manu 					pcblist, &size, NULL, 0) == -1)
    180  1.33      manu 				err(1, "sysctl (copy)");
    181  1.33      manu 
    182  1.33      manu 			for (i = 0; i < size / sizeof(*pcblist); i++) {
    183  1.33      manu 				struct kinfo_pcb *ki = &pcblist[i];
    184  1.33      manu 				struct sockaddr_un *sun;
    185  1.33      manu 				int remote = 0;
    186  1.33      manu 
    187  1.33      manu 				if (first) {
    188  1.33      manu 					unixdomainprhdr();
    189  1.33      manu 					first = 0;
    190  1.33      manu 				}
    191  1.33      manu 
    192  1.33      manu 				sun = (struct sockaddr_un *)&ki->ki_dst;
    193  1.33      manu 				if (sun->sun_path[0] != '\0') {
    194  1.33      manu 					remote = 1;
    195  1.33      manu 				} else {
    196  1.33      manu 					sun = (struct sockaddr_un *)&ki->ki_src;
    197  1.33      manu 				}
    198  1.33      manu 
    199  1.36   msaitoh 				unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
    200  1.33      manu 					      ki->ki_rcvq, ki->ki_sndq,
    201  1.36   msaitoh 					      ki->ki_vnode, ki->ki_conn,
    202  1.36   msaitoh 					      ki->ki_refs, ki->ki_nextref,
    203  1.33      manu 					      ki->ki_sockaddr, sun, remote);
    204  1.24      elad 			}
    205  1.24      elad 
    206  1.33      manu 			free(pcblist);
    207  1.24      elad 		}
    208  1.24      elad 
    209  1.24      elad 	} else {
    210  1.36   msaitoh 		filebuf = (char *)kvm_getfiles(get_kvmd(), KERN_FILE,
    211  1.33      manu 					       0, &ns_nfiles);
    212  1.24      elad 		if (filebuf == 0) {
    213  1.36   msaitoh 			printf("file table read error: %s",
    214  1.37   msaitoh 			    kvm_geterr(get_kvmd()));
    215  1.24      elad 			return;
    216  1.24      elad 		}
    217  1.24      elad 		file = (struct file *)(filebuf + sizeof(fp));
    218  1.25       mrg 		fileNFILE = file + ns_nfiles;
    219  1.24      elad 		for (fp = file; fp < fileNFILE; fp++) {
    220  1.24      elad 			if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
    221  1.24      elad 				continue;
    222  1.24      elad 			if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
    223  1.24      elad 				continue;
    224  1.24      elad 			/* kludge */
    225  1.36   msaitoh 			if (so->so_proto >= unixsw &&
    226  1.36   msaitoh 			    so->so_proto <= unixsw + 2)
    227  1.24      elad 				if (so->so_pcb)
    228  1.24      elad 					unixdomainpr(so, fp->f_data);
    229  1.24      elad 		}
    230  1.24      elad 	}
    231   1.1       cgd }
    232