unix.c revision 1.31 1 1.31 pooka /* $NetBSD: unix.c,v 1.31 2010/12/13 21:15:30 pooka 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.31 pooka __RCSID("$NetBSD: unix.c,v 1.31 2010/12/13 21:15:30 pooka 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.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.10 mycroft extern kvm_t *kvmd;
77 1.1 cgd
78 1.24 elad static void
79 1.24 elad unixdomainprhdr(void)
80 1.1 cgd {
81 1.24 elad printf("Active UNIX domain sockets\n");
82 1.24 elad printf("%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
83 1.24 elad "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn", "Refs",
84 1.24 elad "Nextref");
85 1.1 cgd }
86 1.1 cgd
87 1.29 lukem static const char *socktype[] =
88 1.1 cgd { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
89 1.1 cgd
90 1.10 mycroft static void
91 1.24 elad unixdomainpr0(u_long so_pcb, u_long so_type, u_long rcvq, u_long sndq,
92 1.24 elad u_long inode, u_long conn, u_long refs, u_long nextref,
93 1.24 elad u_long addr, struct sockaddr_un *sun, int remote)
94 1.24 elad {
95 1.24 elad printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
96 1.24 elad so_pcb, socktype[so_type], rcvq, sndq, inode, conn, refs,
97 1.24 elad nextref);
98 1.24 elad if (addr || remote)
99 1.24 elad printf((remote ? " -> %.*s" : " %.*s"),
100 1.24 elad (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
101 1.24 elad sun->sun_path);
102 1.24 elad putchar('\n');
103 1.24 elad }
104 1.24 elad
105 1.24 elad static void
106 1.1 cgd unixdomainpr(so, soaddr)
107 1.17 lukem struct socket *so;
108 1.28 ad void *soaddr;
109 1.1 cgd {
110 1.24 elad struct unpcb unp, runp;
111 1.24 elad struct sockaddr_un sun, rsun;
112 1.1 cgd static int first = 1;
113 1.24 elad int remote = 0;
114 1.1 cgd
115 1.14 mycroft if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
116 1.1 cgd return;
117 1.14 mycroft if (unp.unp_addr)
118 1.14 mycroft if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
119 1.14 mycroft unp.unp_addr = 0;
120 1.24 elad
121 1.24 elad if (!unp.unp_addr) {
122 1.24 elad memset(&rsun, 0, sizeof(rsun));
123 1.24 elad if (unp.unp_conn &&
124 1.24 elad kread((u_long)unp.unp_conn, (char *)&runp, sizeof (runp)) == 0 &&
125 1.24 elad runp.unp_addr &&
126 1.24 elad kread((u_long)runp.unp_addr, (char *)&rsun, sizeof (rsun)) == 0 &&
127 1.24 elad rsun.sun_path[0] != '\0')
128 1.24 elad remote = 1;
129 1.24 elad }
130 1.24 elad
131 1.1 cgd if (first) {
132 1.24 elad unixdomainprhdr();
133 1.1 cgd first = 0;
134 1.1 cgd }
135 1.24 elad
136 1.24 elad unixdomainpr0((u_long)so->so_pcb, so->so_type, so->so_rcv.sb_cc,
137 1.24 elad so->so_snd.sb_cc, (u_long)unp.unp_vnode,
138 1.24 elad (u_long)unp.unp_conn, (u_long)unp.unp_refs,
139 1.24 elad (u_long)unp.unp_nextref, (u_long)unp.unp_addr,
140 1.24 elad remote ? &rsun : &sun, remote);
141 1.24 elad }
142 1.24 elad
143 1.24 elad void
144 1.24 elad unixpr(off)
145 1.24 elad u_long off;
146 1.24 elad {
147 1.24 elad struct file *fp;
148 1.24 elad struct socket sock, *so = &sock;
149 1.24 elad char *filebuf;
150 1.24 elad struct protosw *unixsw = (struct protosw *)off;
151 1.24 elad
152 1.24 elad if (use_sysctl) {
153 1.24 elad struct kinfo_pcb *pcblist;
154 1.24 elad int mib[8];
155 1.24 elad size_t namelen = 0, size = 0, i;
156 1.29 lukem const char *mibname = "net.local.stream.pcblist";
157 1.24 elad static int first = 1;
158 1.24 elad int done = 0;
159 1.24 elad
160 1.24 elad again:
161 1.24 elad memset(mib, 0, sizeof(mib));
162 1.24 elad
163 1.24 elad if (sysctlnametomib(mibname, mib,
164 1.24 elad &namelen) == -1)
165 1.27 elad err(1, "sysctlnametomib: %s", mibname);
166 1.24 elad
167 1.31 pooka if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib),
168 1.31 pooka NULL, &size, NULL, 0) == -1)
169 1.24 elad err(1, "sysctl (query)");
170 1.24 elad
171 1.26 liamjfoy if ((pcblist = malloc(size)) == NULL)
172 1.26 liamjfoy err(1, "malloc");
173 1.24 elad memset(pcblist, 0, size);
174 1.24 elad
175 1.24 elad mib[6] = sizeof(*pcblist);
176 1.24 elad mib[7] = size / sizeof(*pcblist);
177 1.24 elad
178 1.31 pooka if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist,
179 1.24 elad &size, NULL, 0) == -1)
180 1.24 elad err(1, "sysctl (copy)");
181 1.24 elad
182 1.24 elad for (i = 0; i < size / sizeof(*pcblist); i++) {
183 1.24 elad struct kinfo_pcb *ki = &pcblist[i];
184 1.24 elad struct sockaddr_un *sun;
185 1.24 elad int remote = 0;
186 1.24 elad
187 1.24 elad if (first) {
188 1.24 elad unixdomainprhdr();
189 1.24 elad first = 0;
190 1.24 elad }
191 1.24 elad
192 1.24 elad sun = (struct sockaddr_un *)&ki->ki_dst;
193 1.24 elad if (sun->sun_path[0] != '\0') {
194 1.24 elad remote = 1;
195 1.24 elad } else {
196 1.24 elad sun = (struct sockaddr_un *)&ki->ki_src;
197 1.24 elad }
198 1.24 elad
199 1.24 elad unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
200 1.24 elad ki->ki_rcvq, ki->ki_sndq,
201 1.24 elad ki->ki_vnode, ki->ki_conn, ki->ki_refs,
202 1.24 elad ki->ki_nextref, ki->ki_sockaddr, sun, remote);
203 1.24 elad }
204 1.24 elad
205 1.24 elad free(pcblist);
206 1.24 elad
207 1.24 elad if (!done && mibname) {
208 1.24 elad mibname = "net.local.dgram.pcblist";
209 1.24 elad done = 1;
210 1.24 elad goto again;
211 1.24 elad }
212 1.24 elad } else {
213 1.25 mrg filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &ns_nfiles);
214 1.24 elad if (filebuf == 0) {
215 1.24 elad printf("file table read error: %s", kvm_geterr(kvmd));
216 1.24 elad return;
217 1.24 elad }
218 1.24 elad file = (struct file *)(filebuf + sizeof(fp));
219 1.25 mrg fileNFILE = file + ns_nfiles;
220 1.24 elad for (fp = file; fp < fileNFILE; fp++) {
221 1.24 elad if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
222 1.24 elad continue;
223 1.24 elad if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
224 1.24 elad continue;
225 1.24 elad /* kludge */
226 1.24 elad if (so->so_proto >= unixsw && 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