unix.c revision 1.33 1 1.33 manu /* $NetBSD: unix.c,v 1.33 2011/05/29 04:45:08 manu 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.33 manu __RCSID("$NetBSD: unix.c,v 1.33 2011/05/29 04:45:08 manu 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.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.33 manu const char *mibnames[] = {
156 1.33 manu "net.local.stream.pcblist",
157 1.33 manu "net.local.dgram.pcblist",
158 1.33 manu "net.local.seqpacket.pcblist",
159 1.33 manu NULL,
160 1.33 manu };
161 1.33 manu const char **mibname;
162 1.24 elad static int first = 1;
163 1.24 elad
164 1.33 manu for (mibname = mibnames; *mibname; mibname++) {
165 1.33 manu memset(mib, 0, sizeof(mib));
166 1.24 elad
167 1.33 manu if (sysctlnametomib(*mibname, mib,
168 1.33 manu &namelen) == -1)
169 1.33 manu err(1, "sysctlnametomib: %s", *mibname);
170 1.33 manu
171 1.33 manu if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib),
172 1.33 manu NULL, &size, NULL, 0) == -1)
173 1.33 manu err(1, "sysctl (query)");
174 1.33 manu
175 1.33 manu if ((pcblist = malloc(size)) == NULL)
176 1.33 manu err(1, "malloc");
177 1.33 manu memset(pcblist, 0, size);
178 1.33 manu
179 1.33 manu mib[6] = sizeof(*pcblist);
180 1.33 manu mib[7] = size / sizeof(*pcblist);
181 1.33 manu
182 1.33 manu if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib),
183 1.33 manu pcblist, &size, NULL, 0) == -1)
184 1.33 manu err(1, "sysctl (copy)");
185 1.33 manu
186 1.33 manu for (i = 0; i < size / sizeof(*pcblist); i++) {
187 1.33 manu struct kinfo_pcb *ki = &pcblist[i];
188 1.33 manu struct sockaddr_un *sun;
189 1.33 manu int remote = 0;
190 1.33 manu
191 1.33 manu if (first) {
192 1.33 manu unixdomainprhdr();
193 1.33 manu first = 0;
194 1.33 manu }
195 1.33 manu
196 1.33 manu sun = (struct sockaddr_un *)&ki->ki_dst;
197 1.33 manu if (sun->sun_path[0] != '\0') {
198 1.33 manu remote = 1;
199 1.33 manu } else {
200 1.33 manu sun = (struct sockaddr_un *)&ki->ki_src;
201 1.33 manu }
202 1.33 manu
203 1.33 manu unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
204 1.33 manu ki->ki_rcvq, ki->ki_sndq,
205 1.33 manu ki->ki_vnode, ki->ki_conn,
206 1.33 manu ki->ki_refs, ki->ki_nextref,
207 1.33 manu ki->ki_sockaddr, sun, remote);
208 1.24 elad }
209 1.24 elad
210 1.33 manu free(pcblist);
211 1.24 elad }
212 1.24 elad
213 1.24 elad } else {
214 1.33 manu filebuf = (char *)kvm_getfiles(get_kvmd(), KERN_FILE,
215 1.33 manu 0, &ns_nfiles);
216 1.24 elad if (filebuf == 0) {
217 1.33 manu printf("file table read error: %s",
218 1.33 manu kvm_geterr(get_kvmd()));
219 1.24 elad return;
220 1.24 elad }
221 1.24 elad file = (struct file *)(filebuf + sizeof(fp));
222 1.25 mrg fileNFILE = file + ns_nfiles;
223 1.24 elad for (fp = file; fp < fileNFILE; fp++) {
224 1.24 elad if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
225 1.24 elad continue;
226 1.24 elad if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
227 1.24 elad continue;
228 1.24 elad /* kludge */
229 1.24 elad if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
230 1.24 elad if (so->so_pcb)
231 1.24 elad unixdomainpr(so, fp->f_data);
232 1.24 elad }
233 1.24 elad }
234 1.1 cgd }
235