Home | History | Annotate | Line # | Download | only in netstat
unix.c revision 1.30
      1  1.30    plunky /*	$NetBSD: unix.c,v 1.30 2010/04/22 14:37:06 plunky 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.30    plunky __RCSID("$NetBSD: unix.c,v 1.30 2010/04/22 14:37:06 plunky 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.12       jtc #define _KERNEL
     56   1.1       cgd #include <sys/file.h>
     57  1.30    plunky #undef _KERNEL
     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.10   mycroft 
     68  1.24      elad static	void unixdomainprhdr(void);
     69  1.24      elad static	void unixdomainpr0(u_long, u_long, u_long, u_long, u_long, u_long,
     70  1.24      elad 			   u_long, u_long, u_long, struct sockaddr_un *, int);
     71  1.28        ad static	void unixdomainpr(struct socket *, void *);
     72  1.10   mycroft 
     73  1.10   mycroft static struct	file *file, *fileNFILE;
     74  1.25       mrg static int	ns_nfiles;
     75  1.10   mycroft extern	kvm_t *kvmd;
     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.24      elad 	       "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn", "Refs",
     83  1.24      elad 	       "Nextref");
     84   1.1       cgd }
     85   1.1       cgd 
     86  1.29     lukem static	const char *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.24      elad 	       so_pcb, socktype[so_type], rcvq, sndq, inode, conn, refs,
     96  1.24      elad 	       nextref);
     97  1.24      elad 	if (addr || remote)
     98  1.24      elad 		printf((remote ? " -> %.*s" : " %.*s"),
     99  1.24      elad 		       (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
    100  1.24      elad 		       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.1       cgd unixdomainpr(so, soaddr)
    106  1.17     lukem 	struct socket *so;
    107  1.28        ad 	void *soaddr;
    108   1.1       cgd {
    109  1.24      elad 	struct unpcb unp, runp;
    110  1.24      elad 	struct sockaddr_un sun, rsun;
    111   1.1       cgd 	static int first = 1;
    112  1.24      elad 	int remote = 0;
    113   1.1       cgd 
    114  1.14   mycroft 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
    115   1.1       cgd 		return;
    116  1.14   mycroft 	if (unp.unp_addr)
    117  1.14   mycroft 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
    118  1.14   mycroft 			unp.unp_addr = 0;
    119  1.24      elad 
    120  1.24      elad 	if (!unp.unp_addr) {
    121  1.24      elad 		memset(&rsun, 0, sizeof(rsun));
    122  1.24      elad 		if (unp.unp_conn &&
    123  1.24      elad 		    kread((u_long)unp.unp_conn, (char *)&runp, sizeof (runp)) == 0 &&
    124  1.24      elad 		    runp.unp_addr &&
    125  1.24      elad 		    kread((u_long)runp.unp_addr, (char *)&rsun, sizeof (rsun)) == 0 &&
    126  1.24      elad 		    rsun.sun_path[0] != '\0')
    127  1.24      elad 			remote = 1;
    128  1.24      elad 	}
    129  1.24      elad 
    130   1.1       cgd 	if (first) {
    131  1.24      elad 		unixdomainprhdr();
    132   1.1       cgd 		first = 0;
    133   1.1       cgd 	}
    134  1.24      elad 
    135  1.24      elad 	unixdomainpr0((u_long)so->so_pcb, so->so_type, so->so_rcv.sb_cc,
    136  1.24      elad 		      so->so_snd.sb_cc, (u_long)unp.unp_vnode,
    137  1.24      elad 		      (u_long)unp.unp_conn, (u_long)unp.unp_refs,
    138  1.24      elad 		      (u_long)unp.unp_nextref, (u_long)unp.unp_addr,
    139  1.24      elad 		      remote ? &rsun : &sun, remote);
    140  1.24      elad }
    141  1.24      elad 
    142  1.24      elad void
    143  1.24      elad unixpr(off)
    144  1.24      elad 	u_long	off;
    145  1.24      elad {
    146  1.24      elad 	struct file *fp;
    147  1.24      elad 	struct socket sock, *so = &sock;
    148  1.24      elad 	char *filebuf;
    149  1.24      elad 	struct protosw *unixsw = (struct protosw *)off;
    150  1.24      elad 
    151  1.24      elad 	if (use_sysctl) {
    152  1.24      elad 		struct kinfo_pcb *pcblist;
    153  1.24      elad 		int mib[8];
    154  1.24      elad 		size_t namelen = 0, size = 0, i;
    155  1.29     lukem 		const char *mibname = "net.local.stream.pcblist";
    156  1.24      elad 		static int first = 1;
    157  1.24      elad 		int done = 0;
    158  1.24      elad 
    159  1.24      elad  again:
    160  1.24      elad 		memset(mib, 0, sizeof(mib));
    161  1.24      elad 
    162  1.24      elad 		if (sysctlnametomib(mibname, mib,
    163  1.24      elad 				    &namelen) == -1)
    164  1.27      elad 			err(1, "sysctlnametomib: %s", mibname);
    165  1.24      elad 
    166  1.24      elad 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size,
    167  1.24      elad 			   NULL, 0) == -1)
    168  1.24      elad 			err(1, "sysctl (query)");
    169  1.24      elad 
    170  1.26  liamjfoy 		if ((pcblist = malloc(size)) == NULL)
    171  1.26  liamjfoy 			err(1, "malloc");
    172  1.24      elad 		memset(pcblist, 0, size);
    173  1.24      elad 
    174  1.24      elad 		mib[6] = sizeof(*pcblist);
    175  1.24      elad 		mib[7] = size / sizeof(*pcblist);
    176  1.24      elad 
    177  1.24      elad 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist,
    178  1.24      elad 			   &size, NULL, 0) == -1)
    179  1.24      elad 			err(1, "sysctl (copy)");
    180  1.24      elad 
    181  1.24      elad 		for (i = 0; i < size / sizeof(*pcblist); i++) {
    182  1.24      elad 			struct kinfo_pcb *ki = &pcblist[i];
    183  1.24      elad 			struct sockaddr_un *sun;
    184  1.24      elad 			int remote = 0;
    185  1.24      elad 
    186  1.24      elad 			if (first) {
    187  1.24      elad 				unixdomainprhdr();
    188  1.24      elad 				first = 0;
    189  1.24      elad 			}
    190  1.24      elad 
    191  1.24      elad 			sun = (struct sockaddr_un *)&ki->ki_dst;
    192  1.24      elad 			if (sun->sun_path[0] != '\0') {
    193  1.24      elad 				remote = 1;
    194  1.24      elad 			} else {
    195  1.24      elad 				sun = (struct sockaddr_un *)&ki->ki_src;
    196  1.24      elad 			}
    197  1.24      elad 
    198  1.24      elad 			unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
    199  1.24      elad 				      ki->ki_rcvq, ki->ki_sndq,
    200  1.24      elad 				      ki->ki_vnode, ki->ki_conn, ki->ki_refs,
    201  1.24      elad 				      ki->ki_nextref, ki->ki_sockaddr, sun, remote);
    202  1.24      elad 		}
    203  1.24      elad 
    204  1.24      elad 		free(pcblist);
    205  1.24      elad 
    206  1.24      elad 		if (!done && mibname) {
    207  1.24      elad 			mibname = "net.local.dgram.pcblist";
    208  1.24      elad 			done = 1;
    209  1.24      elad 			goto again;
    210  1.24      elad 		}
    211  1.24      elad 	} else {
    212  1.25       mrg 		filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &ns_nfiles);
    213  1.24      elad 		if (filebuf == 0) {
    214  1.24      elad 			printf("file table read error: %s", kvm_geterr(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.24      elad 			if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
    226  1.24      elad 				if (so->so_pcb)
    227  1.24      elad 					unixdomainpr(so, fp->f_data);
    228  1.24      elad 		}
    229  1.24      elad 	}
    230   1.1       cgd }
    231