1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93";
35 #endif /* not lint */
36 #endif
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42 * Display protocol blocks in the unix domain.
43 */
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #define _WANT_SOCKET
49 #include <sys/socketvar.h>
50 #include <sys/mbuf.h>
51 #include <sys/sysctl.h>
52 #include <sys/un.h>
53 #define _WANT_UNPCB
54 #include <sys/unpcb.h>
55
56 #include <netinet/in.h>
57
58 #include <errno.h>
59 #include <err.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <stdbool.h>
65 #include <strings.h>
66 #ifndef FSTACK
67 #include <kvm.h>
68 #endif
69 #include <libxo/xo.h>
70 #include "netstat.h"
71
72 static void unixdomainpr(struct xunpcb *, struct xsocket *);
73
74 static const char *const socktype[] =
75 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
76
77 static int
pcblist_sysctl(int type,char ** bufp)78 pcblist_sysctl(int type, char **bufp)
79 {
80 char *buf;
81 size_t len;
82 char mibvar[sizeof "net.local.seqpacket.pcblist"];
83
84 snprintf(mibvar, sizeof(mibvar), "net.local.%s.pcblist", socktype[type]);
85
86 len = 0;
87 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
88 if (errno != ENOENT)
89 xo_warn("sysctl: %s", mibvar);
90 return (-1);
91 }
92 if ((buf = malloc(len)) == NULL) {
93 xo_warnx("malloc %lu bytes", (u_long)len);
94 return (-2);
95 }
96 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
97 xo_warn("sysctl: %s", mibvar);
98 free(buf);
99 return (-2);
100 }
101 *bufp = buf;
102 return (0);
103 }
104
105 #ifndef FSTACK
106 static int
pcblist_kvm(u_long count_off,u_long gencnt_off,u_long head_off,char ** bufp)107 pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp)
108 {
109 struct unp_head head;
110 struct unpcb *unp, unp0, unp_conn;
111 u_char sun_len;
112 struct socket so;
113 struct xunpgen xug;
114 struct xunpcb xu;
115 unp_gen_t unp_gencnt;
116 u_int unp_count;
117 char *buf, *p;
118 size_t len;
119
120 if (count_off == 0 || gencnt_off == 0)
121 return (-2);
122 if (head_off == 0)
123 return (-1);
124 kread(count_off, &unp_count, sizeof(unp_count));
125 len = 2 * sizeof(xug) + (unp_count + unp_count / 8) * sizeof(xu);
126 if ((buf = malloc(len)) == NULL) {
127 xo_warnx("malloc %lu bytes", (u_long)len);
128 return (-2);
129 }
130 p = buf;
131
132 #define COPYOUT(obj, size) do { \
133 if (len < (size)) { \
134 xo_warnx("buffer size exceeded"); \
135 goto fail; \
136 } \
137 bcopy((obj), p, (size)); \
138 len -= (size); \
139 p += (size); \
140 } while (0)
141
142 #define KREAD(off, buf, len) do { \
143 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
144 goto fail; \
145 } while (0)
146
147 /* Write out header. */
148 kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
149 xug.xug_len = sizeof xug;
150 xug.xug_count = unp_count;
151 xug.xug_gen = unp_gencnt;
152 xug.xug_sogen = 0;
153 COPYOUT(&xug, sizeof xug);
154
155 /* Walk the PCB list. */
156 xu.xu_len = sizeof xu;
157 KREAD(head_off, &head, sizeof(head));
158 LIST_FOREACH(unp, &head, unp_link) {
159 xu.xu_unpp = (uintptr_t)unp;
160 KREAD(unp, &unp0, sizeof (*unp));
161 unp = &unp0;
162
163 if (unp->unp_gencnt > unp_gencnt)
164 continue;
165 if (unp->unp_addr != NULL) {
166 KREAD(unp->unp_addr, &sun_len, sizeof(sun_len));
167 KREAD(unp->unp_addr, &xu.xu_addr, sun_len);
168 }
169 if (unp->unp_conn != NULL) {
170 KREAD(unp->unp_conn, &unp_conn, sizeof(unp_conn));
171 if (unp_conn.unp_addr != NULL) {
172 KREAD(unp_conn.unp_addr, &sun_len,
173 sizeof(sun_len));
174 KREAD(unp_conn.unp_addr, &xu.xu_caddr, sun_len);
175 }
176 }
177 KREAD(unp->unp_socket, &so, sizeof(so));
178 if (sotoxsocket(&so, &xu.xu_socket) != 0)
179 goto fail;
180 COPYOUT(&xu, sizeof(xu));
181 }
182
183 /* Reread the counts and write the footer. */
184 kread(count_off, &unp_count, sizeof(unp_count));
185 kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
186 xug.xug_count = unp_count;
187 xug.xug_gen = unp_gencnt;
188 COPYOUT(&xug, sizeof xug);
189
190 *bufp = buf;
191 return (0);
192
193 fail:
194 free(buf);
195 return (-1);
196 #undef COPYOUT
197 #undef KREAD
198 }
199 #endif
200
201 void
unixpr(u_long count_off,u_long gencnt_off,u_long dhead_off,u_long shead_off,u_long sphead_off,bool * first)202 unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off,
203 u_long sphead_off, bool *first)
204 {
205 char *buf;
206 int ret, type;
207 struct xsocket *so;
208 struct xunpgen *xug, *oxug;
209 struct xunpcb *xunp;
210 #ifndef FSTACK
211 u_long head_off;
212 #endif
213
214 buf = NULL;
215 for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) {
216 if (live)
217 ret = pcblist_sysctl(type, &buf);
218 else {
219 #ifndef FSTACK
220 head_off = 0;
221 switch (type) {
222 case SOCK_STREAM:
223 head_off = shead_off;
224 break;
225
226 case SOCK_DGRAM:
227 head_off = dhead_off;
228 break;
229
230 case SOCK_SEQPACKET:
231 head_off = sphead_off;
232 break;
233 }
234 ret = pcblist_kvm(count_off, gencnt_off, head_off,
235 &buf);
236 #endif
237 }
238 if (ret == -1)
239 continue;
240 if (ret < 0)
241 return;
242
243 oxug = xug = (struct xunpgen *)buf;
244 for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
245 xug->xug_len > sizeof(struct xunpgen);
246 xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
247 xunp = (struct xunpcb *)xug;
248 so = &xunp->xu_socket;
249
250 /* Ignore PCBs which were freed during copyout. */
251 if (xunp->unp_gencnt > oxug->xug_gen)
252 continue;
253 if (*first) {
254 xo_open_list("socket");
255 *first = false;
256 }
257 xo_open_instance("socket");
258 unixdomainpr(xunp, so);
259 xo_close_instance("socket");
260 }
261 if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
262 if (oxug->xug_count > xug->xug_count) {
263 xo_emit("Some {:type/%s} sockets may have "
264 "been {:action/deleted}.\n",
265 socktype[type]);
266 } else if (oxug->xug_count < xug->xug_count) {
267 xo_emit("Some {:type/%s} sockets may have "
268 "been {:action/created}.\n",
269 socktype[type]);
270 } else {
271 xo_emit("Some {:type/%s} sockets may have "
272 "been {:action/created or deleted}",
273 socktype[type]);
274 }
275 }
276 free(buf);
277 }
278 }
279
280 static void
unixdomainpr(struct xunpcb * xunp,struct xsocket * so)281 unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
282 {
283 struct sockaddr_un *sa;
284 static int first = 1;
285 char buf1[33];
286 static const char *titles[2] = {
287 "{T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%8.8s} "
288 "{T:/%8.8s} {T:/%8.8s} {T:/%8.8s} {T:Addr}\n",
289 "{T:/%-16.16s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%16.16s} "
290 "{T:/%16.16s} {T:/%16.16s} {T:/%16.16s} {T:Addr}\n"
291 };
292 static const char *format[2] = {
293 "{q:address/%8lx} {t:type/%-6.6s} "
294 "{:receive-bytes-waiting/%6u} "
295 "{:send-bytes-waiting/%6u} "
296 "{q:vnode/%8lx} {q:connection/%8lx} "
297 "{q:first-reference/%8lx} {q:next-reference/%8lx}",
298 "{q:address/%16lx} {t:type/%-6.6s} "
299 "{:receive-bytes-waiting/%6u} "
300 "{:send-bytes-waiting/%6u} "
301 "{q:vnode/%16lx} {q:connection/%16lx} "
302 "{q:first-reference/%16lx} {q:next-reference/%16lx}"
303 };
304 int fmt = (sizeof(void *) == 8) ? 1 : 0;
305
306 sa = (xunp->xu_addr.sun_family == AF_UNIX) ? &xunp->xu_addr : NULL;
307
308 if (first && !Lflag) {
309 xo_emit("{T:Active UNIX domain sockets}\n");
310 xo_emit(titles[fmt],
311 "Address", "Type", "Recv-Q", "Send-Q",
312 "Inode", "Conn", "Refs", "Nextref");
313 first = 0;
314 }
315
316 if (Lflag && so->so_qlimit == 0)
317 return;
318
319 if (Lflag) {
320 snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
321 so->so_incqlen, so->so_qlimit);
322 xo_emit("unix {d:socket/%-32.32s}{e:queue-length/%u}"
323 "{e:incomplete-queue-length/%u}{e:queue-limit/%u}",
324 buf1, so->so_qlen, so->so_incqlen, so->so_qlimit);
325 } else {
326 xo_emit(format[fmt],
327 (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
328 so->so_snd.sb_cc, (long)xunp->unp_vnode,
329 (long)xunp->unp_conn, (long)xunp->xu_firstref,
330 (long)xunp->xu_nextref);
331 }
332 if (sa)
333 xo_emit(" {:path/%.*s}",
334 (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)),
335 sa->sun_path);
336 xo_emit("\n");
337 }
338