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 #include <sys/cdefs.h>
33 /*
34 * Display protocol blocks in the unix domain.
35 */
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #define _WANT_SOCKET
41 #include <sys/socketvar.h>
42 #include <sys/mbuf.h>
43 #include <sys/sysctl.h>
44 #include <sys/un.h>
45 #define _WANT_UNPCB
46 #include <sys/unpcb.h>
47
48 #include <netinet/in.h>
49
50 #include <errno.h>
51 #include <stddef.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <stdbool.h>
56 #include <strings.h>
57 #include <kvm.h>
58 #include <libxo/xo.h>
59 #include "netstat.h"
60
61 static void unixdomainpr(struct xunpcb *, struct xsocket *);
62
63 static const char *const socktype[] =
64 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
65
66 static int
pcblist_sysctl(int type,char ** bufp)67 pcblist_sysctl(int type, char **bufp)
68 {
69 char *buf;
70 size_t len;
71 char mibvar[sizeof "net.local.seqpacket.pcblist"];
72
73 snprintf(mibvar, sizeof(mibvar), "net.local.%s.pcblist", socktype[type]);
74
75 len = 0;
76 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
77 if (errno != ENOENT)
78 xo_warn("sysctl: %s", mibvar);
79 return (-1);
80 }
81 if ((buf = malloc(len)) == NULL) {
82 xo_warnx("malloc %lu bytes", (u_long)len);
83 return (-2);
84 }
85 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
86 xo_warn("sysctl: %s", mibvar);
87 free(buf);
88 return (-2);
89 }
90 *bufp = buf;
91 return (0);
92 }
93
94 static int
pcblist_kvm(u_long count_off,u_long gencnt_off,u_long head_off,char ** bufp)95 pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp)
96 {
97 struct unp_head head;
98 struct unpcb *unp, unp0, unp_conn;
99 u_char sun_len;
100 struct socket so;
101 struct xunpgen xug;
102 struct xunpcb xu;
103 unp_gen_t unp_gencnt;
104 u_int unp_count;
105 char *buf, *p;
106 size_t len;
107
108 if (count_off == 0 || gencnt_off == 0)
109 return (-2);
110 if (head_off == 0)
111 return (-1);
112 kread(count_off, &unp_count, sizeof(unp_count));
113 len = 2 * sizeof(xug) + (unp_count + unp_count / 8) * sizeof(xu);
114 if ((buf = malloc(len)) == NULL) {
115 xo_warnx("malloc %lu bytes", (u_long)len);
116 return (-2);
117 }
118 p = buf;
119
120 #define COPYOUT(obj, size) do { \
121 if (len < (size)) { \
122 xo_warnx("buffer size exceeded"); \
123 goto fail; \
124 } \
125 bcopy((obj), p, (size)); \
126 len -= (size); \
127 p += (size); \
128 } while (0)
129
130 #define KREAD(off, buf, len) do { \
131 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
132 goto fail; \
133 } while (0)
134
135 /* Write out header. */
136 kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
137 xug.xug_len = sizeof xug;
138 xug.xug_count = unp_count;
139 xug.xug_gen = unp_gencnt;
140 xug.xug_sogen = 0;
141 COPYOUT(&xug, sizeof xug);
142
143 /* Walk the PCB list. */
144 xu.xu_len = sizeof xu;
145 KREAD(head_off, &head, sizeof(head));
146 LIST_FOREACH(unp, &head, unp_link) {
147 xu.xu_unpp = (uintptr_t)unp;
148 KREAD(unp, &unp0, sizeof (*unp));
149 unp = &unp0;
150
151 if (unp->unp_gencnt > unp_gencnt)
152 continue;
153 if (unp->unp_addr != NULL) {
154 KREAD(unp->unp_addr, &sun_len, sizeof(sun_len));
155 KREAD(unp->unp_addr, &xu.xu_addr, sun_len);
156 }
157 if (unp->unp_conn != NULL) {
158 KREAD(unp->unp_conn, &unp_conn, sizeof(unp_conn));
159 if (unp_conn.unp_addr != NULL) {
160 KREAD(unp_conn.unp_addr, &sun_len,
161 sizeof(sun_len));
162 KREAD(unp_conn.unp_addr, &xu.xu_caddr, sun_len);
163 }
164 }
165 KREAD(unp->unp_socket, &so, sizeof(so));
166 if (sotoxsocket(&so, &xu.xu_socket) != 0)
167 goto fail;
168 COPYOUT(&xu, sizeof(xu));
169 }
170
171 /* Reread the counts and write the footer. */
172 kread(count_off, &unp_count, sizeof(unp_count));
173 kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt));
174 xug.xug_count = unp_count;
175 xug.xug_gen = unp_gencnt;
176 COPYOUT(&xug, sizeof xug);
177
178 *bufp = buf;
179 return (0);
180
181 fail:
182 free(buf);
183 return (-1);
184 #undef COPYOUT
185 #undef KREAD
186 }
187
188 void
unixpr(u_long count_off,u_long gencnt_off,u_long dhead_off,u_long shead_off,u_long sphead_off,bool * first)189 unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off,
190 u_long sphead_off, bool *first)
191 {
192 char *buf;
193 int ret, type;
194 struct xsocket *so;
195 struct xunpgen *xug, *oxug;
196 struct xunpcb *xunp;
197 u_long head_off;
198
199 buf = NULL;
200 for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) {
201 if (live)
202 ret = pcblist_sysctl(type, &buf);
203 else {
204 head_off = 0;
205 switch (type) {
206 case SOCK_STREAM:
207 head_off = shead_off;
208 break;
209
210 case SOCK_DGRAM:
211 head_off = dhead_off;
212 break;
213
214 case SOCK_SEQPACKET:
215 head_off = sphead_off;
216 break;
217 }
218 ret = pcblist_kvm(count_off, gencnt_off, head_off,
219 &buf);
220 }
221 if (ret == -1)
222 continue;
223 if (ret < 0)
224 return;
225
226 oxug = xug = (struct xunpgen *)buf;
227 for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
228 xug->xug_len > sizeof(struct xunpgen);
229 xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
230 xunp = (struct xunpcb *)xug;
231 so = &xunp->xu_socket;
232
233 /* Ignore PCBs which were freed during copyout. */
234 if (xunp->unp_gencnt > oxug->xug_gen)
235 continue;
236 if (*first) {
237 xo_open_list("socket");
238 *first = false;
239 }
240 xo_open_instance("socket");
241 unixdomainpr(xunp, so);
242 xo_close_instance("socket");
243 }
244 if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
245 if (oxug->xug_count > xug->xug_count) {
246 xo_emit("Some {:type/%s} sockets may have "
247 "been {:action/deleted}.\n",
248 socktype[type]);
249 } else if (oxug->xug_count < xug->xug_count) {
250 xo_emit("Some {:type/%s} sockets may have "
251 "been {:action/created}.\n",
252 socktype[type]);
253 } else {
254 xo_emit("Some {:type/%s} sockets may have "
255 "been {:action/created or deleted}",
256 socktype[type]);
257 }
258 }
259 free(buf);
260 }
261 }
262
263 static void
unixdomainpr(struct xunpcb * xunp,struct xsocket * so)264 unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
265 {
266 struct sockaddr_un *sa;
267 static int first = 1;
268 char buf1[33];
269 static const char *titles[2] = {
270 "{T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%8.8s} "
271 "{T:/%8.8s} {T:/%8.8s} {T:/%8.8s} {T:Addr}\n",
272 "{T:/%-16.16s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%16.16s} "
273 "{T:/%16.16s} {T:/%16.16s} {T:/%16.16s} {T:Addr}\n"
274 };
275 static const char *format[2] = {
276 "{q:address/%8lx} {t:type/%-6.6s} "
277 "{:receive-bytes-waiting/%6u} "
278 "{:send-bytes-waiting/%6u} "
279 "{q:vnode/%8lx} {q:connection/%8lx} "
280 "{q:first-reference/%8lx} {q:next-reference/%8lx}",
281 "{q:address/%16lx} {t:type/%-6.6s} "
282 "{:receive-bytes-waiting/%6u} "
283 "{:send-bytes-waiting/%6u} "
284 "{q:vnode/%16lx} {q:connection/%16lx} "
285 "{q:first-reference/%16lx} {q:next-reference/%16lx}"
286 };
287 int fmt = (sizeof(void *) == 8) ? 1 : 0;
288
289 sa = (xunp->xu_addr.sun_family == AF_UNIX) ? &xunp->xu_addr : NULL;
290
291 if (first && !Lflag) {
292 xo_emit("{T:Active UNIX domain sockets}\n");
293 xo_emit(titles[fmt],
294 "Address", "Type", "Recv-Q", "Send-Q",
295 "Inode", "Conn", "Refs", "Nextref");
296 first = 0;
297 }
298
299 if (Lflag && so->so_qlimit == 0)
300 return;
301
302 if (Lflag) {
303 snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
304 so->so_incqlen, so->so_qlimit);
305 xo_emit("unix {d:socket/%-32.32s}{e:queue-length/%u}"
306 "{e:incomplete-queue-length/%u}{e:queue-limit/%u}",
307 buf1, so->so_qlen, so->so_incqlen, so->so_qlimit);
308 } else {
309 xo_emit(format[fmt],
310 (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
311 so->so_snd.sb_cc, (long)xunp->unp_vnode,
312 (long)xunp->unp_conn, (long)xunp->xu_firstref,
313 (long)xunp->xu_nextref);
314 }
315 if (sa)
316 xo_emit(" {:path/%.*s}",
317 (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)),
318 sa->sun_path);
319 xo_emit("\n");
320 }
321