unix.c revision 1.24 1 1.24 elad /* $NetBSD: unix.c,v 1.24 2005/09/04 18:59:57 elad 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.24 elad __RCSID("$NetBSD: unix.c,v 1.24 2005/09/04 18:59:57 elad 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.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/protosw.h>
46 1.1 cgd #include <sys/socket.h>
47 1.1 cgd #include <sys/socketvar.h>
48 1.1 cgd #include <sys/mbuf.h>
49 1.10 mycroft #include <sys/sysctl.h>
50 1.1 cgd #include <sys/un.h>
51 1.1 cgd #include <sys/unpcb.h>
52 1.12 jtc #define _KERNEL
53 1.1 cgd struct uio;
54 1.10 mycroft struct proc;
55 1.1 cgd #include <sys/file.h>
56 1.1 cgd
57 1.10 mycroft #include <netinet/in.h>
58 1.10 mycroft
59 1.10 mycroft #include <stdio.h>
60 1.10 mycroft #include <stdlib.h>
61 1.24 elad #include <string.h>
62 1.11 deraadt #include <kvm.h>
63 1.24 elad #include <err.h>
64 1.10 mycroft #include "netstat.h"
65 1.10 mycroft
66 1.24 elad static void unixdomainprhdr(void);
67 1.24 elad static void unixdomainpr0(u_long, u_long, u_long, u_long, u_long, u_long,
68 1.24 elad u_long, u_long, u_long, struct sockaddr_un *, int);
69 1.24 elad static void unixdomainpr(struct socket *, caddr_t);
70 1.10 mycroft
71 1.10 mycroft static struct file *file, *fileNFILE;
72 1.10 mycroft static int nfiles;
73 1.10 mycroft extern kvm_t *kvmd;
74 1.1 cgd
75 1.24 elad static void
76 1.24 elad unixdomainprhdr(void)
77 1.1 cgd {
78 1.24 elad printf("Active UNIX domain sockets\n");
79 1.24 elad printf("%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
80 1.24 elad "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn", "Refs",
81 1.24 elad "Nextref");
82 1.1 cgd }
83 1.1 cgd
84 1.1 cgd static char *socktype[] =
85 1.1 cgd { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
86 1.1 cgd
87 1.10 mycroft static void
88 1.24 elad unixdomainpr0(u_long so_pcb, u_long so_type, u_long rcvq, u_long sndq,
89 1.24 elad u_long inode, u_long conn, u_long refs, u_long nextref,
90 1.24 elad u_long addr, struct sockaddr_un *sun, int remote)
91 1.24 elad {
92 1.24 elad printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
93 1.24 elad so_pcb, socktype[so_type], rcvq, sndq, inode, conn, refs,
94 1.24 elad nextref);
95 1.24 elad if (addr || remote)
96 1.24 elad printf((remote ? " -> %.*s" : " %.*s"),
97 1.24 elad (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
98 1.24 elad sun->sun_path);
99 1.24 elad putchar('\n');
100 1.24 elad }
101 1.24 elad
102 1.24 elad static void
103 1.1 cgd unixdomainpr(so, soaddr)
104 1.17 lukem struct socket *so;
105 1.1 cgd caddr_t 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.24 elad unixpr(off)
142 1.24 elad u_long off;
143 1.24 elad {
144 1.24 elad struct file *fp;
145 1.24 elad struct socket sock, *so = &sock;
146 1.24 elad char *filebuf;
147 1.24 elad struct protosw *unixsw = (struct protosw *)off;
148 1.24 elad
149 1.24 elad if (use_sysctl) {
150 1.24 elad struct kinfo_pcb *pcblist;
151 1.24 elad int mib[8];
152 1.24 elad size_t namelen = 0, size = 0, i;
153 1.24 elad char *mibname = "net.local.stream.pcblist";
154 1.24 elad static int first = 1;
155 1.24 elad int done = 0;
156 1.24 elad
157 1.24 elad again:
158 1.24 elad memset(mib, 0, sizeof(mib));
159 1.24 elad
160 1.24 elad if (sysctlnametomib(mibname, mib,
161 1.24 elad &namelen) == -1)
162 1.24 elad err(1, "sysctlnametomib");
163 1.24 elad
164 1.24 elad if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size,
165 1.24 elad NULL, 0) == -1)
166 1.24 elad err(1, "sysctl (query)");
167 1.24 elad
168 1.24 elad pcblist = malloc(size);
169 1.24 elad memset(pcblist, 0, size);
170 1.24 elad
171 1.24 elad mib[6] = sizeof(*pcblist);
172 1.24 elad mib[7] = size / sizeof(*pcblist);
173 1.24 elad
174 1.24 elad if (sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist,
175 1.24 elad &size, NULL, 0) == -1)
176 1.24 elad err(1, "sysctl (copy)");
177 1.24 elad
178 1.24 elad for (i = 0; i < size / sizeof(*pcblist); i++) {
179 1.24 elad struct kinfo_pcb *ki = &pcblist[i];
180 1.24 elad struct sockaddr_un *sun;
181 1.24 elad int remote = 0;
182 1.24 elad
183 1.24 elad if (first) {
184 1.24 elad unixdomainprhdr();
185 1.24 elad first = 0;
186 1.24 elad }
187 1.24 elad
188 1.24 elad sun = (struct sockaddr_un *)&ki->ki_dst;
189 1.24 elad if (sun->sun_path[0] != '\0') {
190 1.24 elad remote = 1;
191 1.24 elad } else {
192 1.24 elad sun = (struct sockaddr_un *)&ki->ki_src;
193 1.24 elad }
194 1.24 elad
195 1.24 elad unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
196 1.24 elad ki->ki_rcvq, ki->ki_sndq,
197 1.24 elad ki->ki_vnode, ki->ki_conn, ki->ki_refs,
198 1.24 elad ki->ki_nextref, ki->ki_sockaddr, sun, remote);
199 1.24 elad }
200 1.24 elad
201 1.24 elad free(pcblist);
202 1.24 elad
203 1.24 elad if (!done && mibname) {
204 1.24 elad mibname = "net.local.dgram.pcblist";
205 1.24 elad done = 1;
206 1.24 elad goto again;
207 1.24 elad }
208 1.24 elad } else {
209 1.24 elad filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
210 1.24 elad if (filebuf == 0) {
211 1.24 elad printf("file table read error: %s", kvm_geterr(kvmd));
212 1.24 elad return;
213 1.24 elad }
214 1.24 elad file = (struct file *)(filebuf + sizeof(fp));
215 1.24 elad fileNFILE = file + nfiles;
216 1.24 elad for (fp = file; fp < fileNFILE; fp++) {
217 1.24 elad if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
218 1.24 elad continue;
219 1.24 elad if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
220 1.24 elad continue;
221 1.24 elad /* kludge */
222 1.24 elad if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
223 1.24 elad if (so->so_pcb)
224 1.24 elad unixdomainpr(so, fp->f_data);
225 1.24 elad }
226 1.24 elad }
227 1.1 cgd }
228