19b50d902SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 325d422d6aSPhilippe Charnier #if 0 336cc6f122SPhilippe Charnier #ifndef lint 349b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93"; 359b50d902SRodney W. Grimes #endif /* not lint */ 366cc6f122SPhilippe Charnier #endif 376cc6f122SPhilippe Charnier 386cc6f122SPhilippe Charnier #include <sys/cdefs.h> 396cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 409b50d902SRodney W. Grimes 419b50d902SRodney W. Grimes /* 429b50d902SRodney W. Grimes * Display protocol blocks in the unix domain. 439b50d902SRodney W. Grimes */ 449b50d902SRodney W. Grimes #include <sys/param.h> 45d8d89152SDavid Greenman #include <sys/queue.h> 469b50d902SRodney W. Grimes #include <sys/protosw.h> 479b50d902SRodney W. Grimes #include <sys/socket.h> 480e229f34SGleb Smirnoff #define _WANT_SOCKET 499b50d902SRodney W. Grimes #include <sys/socketvar.h> 509b50d902SRodney W. Grimes #include <sys/mbuf.h> 519b50d902SRodney W. Grimes #include <sys/sysctl.h> 529b50d902SRodney W. Grimes #include <sys/un.h> 530e229f34SGleb Smirnoff #define _WANT_UNPCB 549b50d902SRodney W. Grimes #include <sys/unpcb.h> 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes #include <netinet/in.h> 579b50d902SRodney W. Grimes 584f81ef50SGarrett Wollman #include <errno.h> 59c6620a13SPoul-Henning Kamp #include <err.h> 608ad9b1a3SGarrett Wollman #include <stddef.h> 617b95a1ebSYaroslav Tykhiy #include <stdint.h> 629b50d902SRodney W. Grimes #include <stdio.h> 639b50d902SRodney W. Grimes #include <stdlib.h> 64ade9ccfeSMarcel Moolenaar #include <stdbool.h> 65feda1a43SJohn Baldwin #include <strings.h> 664f81ef50SGarrett Wollman #include <kvm.h> 67ade9ccfeSMarcel Moolenaar #include <libxo/xo.h> 689b50d902SRodney W. Grimes #include "netstat.h" 699b50d902SRodney W. Grimes 705e051718SAssar Westerlund static void unixdomainpr(struct xunpcb *, struct xsocket *); 719b50d902SRodney W. Grimes 724f81ef50SGarrett Wollman static const char *const socktype[] = 739b50d902SRodney W. Grimes { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" }; 749b50d902SRodney W. Grimes 75feda1a43SJohn Baldwin static int 76feda1a43SJohn Baldwin pcblist_sysctl(int type, char **bufp) 779b50d902SRodney W. Grimes { 784f81ef50SGarrett Wollman char *buf; 794f81ef50SGarrett Wollman size_t len; 804f81ef50SGarrett Wollman char mibvar[sizeof "net.local.seqpacket.pcblist"]; 814f81ef50SGarrett Wollman 82f193c8ceSXin LI snprintf(mibvar, sizeof(mibvar), "net.local.%s.pcblist", socktype[type]); 834f81ef50SGarrett Wollman 844f81ef50SGarrett Wollman len = 0; 854f81ef50SGarrett Wollman if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { 864f81ef50SGarrett Wollman if (errno != ENOENT) 87ade9ccfeSMarcel Moolenaar xo_warn("sysctl: %s", mibvar); 88feda1a43SJohn Baldwin return (-1); 894f81ef50SGarrett Wollman } 90ef1cb629SMarcelo Araujo if ((buf = malloc(len)) == NULL) { 91ade9ccfeSMarcel Moolenaar xo_warnx("malloc %lu bytes", (u_long)len); 92feda1a43SJohn Baldwin return (-2); 934f81ef50SGarrett Wollman } 944f81ef50SGarrett Wollman if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { 95ade9ccfeSMarcel Moolenaar xo_warn("sysctl: %s", mibvar); 964f81ef50SGarrett Wollman free(buf); 97feda1a43SJohn Baldwin return (-2); 984f81ef50SGarrett Wollman } 99feda1a43SJohn Baldwin *bufp = buf; 100feda1a43SJohn Baldwin return (0); 101feda1a43SJohn Baldwin } 102feda1a43SJohn Baldwin 103feda1a43SJohn Baldwin static int 104feda1a43SJohn Baldwin pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp) 105feda1a43SJohn Baldwin { 106feda1a43SJohn Baldwin struct unp_head head; 1070e229f34SGleb Smirnoff struct unpcb *unp, unp0, unp_conn; 108feda1a43SJohn Baldwin u_char sun_len; 109feda1a43SJohn Baldwin struct socket so; 110feda1a43SJohn Baldwin struct xunpgen xug; 111feda1a43SJohn Baldwin struct xunpcb xu; 112feda1a43SJohn Baldwin unp_gen_t unp_gencnt; 113feda1a43SJohn Baldwin u_int unp_count; 114feda1a43SJohn Baldwin char *buf, *p; 115feda1a43SJohn Baldwin size_t len; 116feda1a43SJohn Baldwin 117feda1a43SJohn Baldwin if (count_off == 0 || gencnt_off == 0) 118feda1a43SJohn Baldwin return (-2); 119feda1a43SJohn Baldwin if (head_off == 0) 120feda1a43SJohn Baldwin return (-1); 121feda1a43SJohn Baldwin kread(count_off, &unp_count, sizeof(unp_count)); 122feda1a43SJohn Baldwin len = 2 * sizeof(xug) + (unp_count + unp_count / 8) * sizeof(xu); 123ef1cb629SMarcelo Araujo if ((buf = malloc(len)) == NULL) { 124ade9ccfeSMarcel Moolenaar xo_warnx("malloc %lu bytes", (u_long)len); 125feda1a43SJohn Baldwin return (-2); 126feda1a43SJohn Baldwin } 127feda1a43SJohn Baldwin p = buf; 128feda1a43SJohn Baldwin 129feda1a43SJohn Baldwin #define COPYOUT(obj, size) do { \ 130feda1a43SJohn Baldwin if (len < (size)) { \ 131ade9ccfeSMarcel Moolenaar xo_warnx("buffer size exceeded"); \ 132feda1a43SJohn Baldwin goto fail; \ 133feda1a43SJohn Baldwin } \ 134feda1a43SJohn Baldwin bcopy((obj), p, (size)); \ 135feda1a43SJohn Baldwin len -= (size); \ 136feda1a43SJohn Baldwin p += (size); \ 137feda1a43SJohn Baldwin } while (0) 138feda1a43SJohn Baldwin 139feda1a43SJohn Baldwin #define KREAD(off, buf, len) do { \ 140feda1a43SJohn Baldwin if (kread((uintptr_t)(off), (buf), (len)) != 0) \ 141feda1a43SJohn Baldwin goto fail; \ 142feda1a43SJohn Baldwin } while (0) 143feda1a43SJohn Baldwin 144feda1a43SJohn Baldwin /* Write out header. */ 145feda1a43SJohn Baldwin kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt)); 146feda1a43SJohn Baldwin xug.xug_len = sizeof xug; 147feda1a43SJohn Baldwin xug.xug_count = unp_count; 148feda1a43SJohn Baldwin xug.xug_gen = unp_gencnt; 149feda1a43SJohn Baldwin xug.xug_sogen = 0; 150feda1a43SJohn Baldwin COPYOUT(&xug, sizeof xug); 151feda1a43SJohn Baldwin 152feda1a43SJohn Baldwin /* Walk the PCB list. */ 153feda1a43SJohn Baldwin xu.xu_len = sizeof xu; 154feda1a43SJohn Baldwin KREAD(head_off, &head, sizeof(head)); 155feda1a43SJohn Baldwin LIST_FOREACH(unp, &head, unp_link) { 156*3a20f06aSBrooks Davis xu.xu_unpp = (uintptr_t)unp; 1570e229f34SGleb Smirnoff KREAD(unp, &unp0, sizeof (*unp)); 1580e229f34SGleb Smirnoff unp = &unp0; 159feda1a43SJohn Baldwin 160feda1a43SJohn Baldwin if (unp->unp_gencnt > unp_gencnt) 161feda1a43SJohn Baldwin continue; 162feda1a43SJohn Baldwin if (unp->unp_addr != NULL) { 163feda1a43SJohn Baldwin KREAD(unp->unp_addr, &sun_len, sizeof(sun_len)); 164feda1a43SJohn Baldwin KREAD(unp->unp_addr, &xu.xu_addr, sun_len); 165feda1a43SJohn Baldwin } 166feda1a43SJohn Baldwin if (unp->unp_conn != NULL) { 167feda1a43SJohn Baldwin KREAD(unp->unp_conn, &unp_conn, sizeof(unp_conn)); 168feda1a43SJohn Baldwin if (unp_conn.unp_addr != NULL) { 169feda1a43SJohn Baldwin KREAD(unp_conn.unp_addr, &sun_len, 170feda1a43SJohn Baldwin sizeof(sun_len)); 171feda1a43SJohn Baldwin KREAD(unp_conn.unp_addr, &xu.xu_caddr, sun_len); 172feda1a43SJohn Baldwin } 173feda1a43SJohn Baldwin } 174feda1a43SJohn Baldwin KREAD(unp->unp_socket, &so, sizeof(so)); 175feda1a43SJohn Baldwin if (sotoxsocket(&so, &xu.xu_socket) != 0) 176feda1a43SJohn Baldwin goto fail; 177feda1a43SJohn Baldwin COPYOUT(&xu, sizeof(xu)); 178feda1a43SJohn Baldwin } 179feda1a43SJohn Baldwin 180feda1a43SJohn Baldwin /* Reread the counts and write the footer. */ 181feda1a43SJohn Baldwin kread(count_off, &unp_count, sizeof(unp_count)); 182feda1a43SJohn Baldwin kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt)); 183feda1a43SJohn Baldwin xug.xug_count = unp_count; 184feda1a43SJohn Baldwin xug.xug_gen = unp_gencnt; 185feda1a43SJohn Baldwin COPYOUT(&xug, sizeof xug); 186feda1a43SJohn Baldwin 187feda1a43SJohn Baldwin *bufp = buf; 188feda1a43SJohn Baldwin return (0); 189feda1a43SJohn Baldwin 190feda1a43SJohn Baldwin fail: 191feda1a43SJohn Baldwin free(buf); 192feda1a43SJohn Baldwin return (-1); 193feda1a43SJohn Baldwin #undef COPYOUT 194feda1a43SJohn Baldwin #undef KREAD 195feda1a43SJohn Baldwin } 196feda1a43SJohn Baldwin 197feda1a43SJohn Baldwin void 198963b7ccdSRobert Watson unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off, 199ade9ccfeSMarcel Moolenaar u_long sphead_off, bool *first) 200feda1a43SJohn Baldwin { 201feda1a43SJohn Baldwin char *buf; 202feda1a43SJohn Baldwin int ret, type; 203feda1a43SJohn Baldwin struct xsocket *so; 204feda1a43SJohn Baldwin struct xunpgen *xug, *oxug; 205feda1a43SJohn Baldwin struct xunpcb *xunp; 206963b7ccdSRobert Watson u_long head_off; 207feda1a43SJohn Baldwin 208321ae07fSPhilippe Charnier buf = NULL; 209feda1a43SJohn Baldwin for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) { 210feda1a43SJohn Baldwin if (live) 211feda1a43SJohn Baldwin ret = pcblist_sysctl(type, &buf); 212963b7ccdSRobert Watson else { 213963b7ccdSRobert Watson head_off = 0; 214963b7ccdSRobert Watson switch (type) { 215963b7ccdSRobert Watson case SOCK_STREAM: 216963b7ccdSRobert Watson head_off = shead_off; 217963b7ccdSRobert Watson break; 218963b7ccdSRobert Watson 219963b7ccdSRobert Watson case SOCK_DGRAM: 220963b7ccdSRobert Watson head_off = dhead_off; 221963b7ccdSRobert Watson break; 222963b7ccdSRobert Watson 223963b7ccdSRobert Watson case SOCK_SEQPACKET: 224963b7ccdSRobert Watson head_off = sphead_off; 225963b7ccdSRobert Watson break; 226963b7ccdSRobert Watson } 227963b7ccdSRobert Watson ret = pcblist_kvm(count_off, gencnt_off, head_off, 228963b7ccdSRobert Watson &buf); 229963b7ccdSRobert Watson } 230feda1a43SJohn Baldwin if (ret == -1) 231feda1a43SJohn Baldwin continue; 232feda1a43SJohn Baldwin if (ret < 0) 233feda1a43SJohn Baldwin return; 2344f81ef50SGarrett Wollman 2354f81ef50SGarrett Wollman oxug = xug = (struct xunpgen *)buf; 2364f81ef50SGarrett Wollman for (xug = (struct xunpgen *)((char *)xug + xug->xug_len); 2374f81ef50SGarrett Wollman xug->xug_len > sizeof(struct xunpgen); 2384f81ef50SGarrett Wollman xug = (struct xunpgen *)((char *)xug + xug->xug_len)) { 2394f81ef50SGarrett Wollman xunp = (struct xunpcb *)xug; 2404f81ef50SGarrett Wollman so = &xunp->xu_socket; 2414f81ef50SGarrett Wollman 2424f81ef50SGarrett Wollman /* Ignore PCBs which were freed during copyout. */ 2430e229f34SGleb Smirnoff if (xunp->unp_gencnt > oxug->xug_gen) 2444f81ef50SGarrett Wollman continue; 245ade9ccfeSMarcel Moolenaar if (*first) { 246ade9ccfeSMarcel Moolenaar xo_open_list("socket"); 247ade9ccfeSMarcel Moolenaar *first = false; 248ade9ccfeSMarcel Moolenaar } 249ade9ccfeSMarcel Moolenaar xo_open_instance("socket"); 2504f81ef50SGarrett Wollman unixdomainpr(xunp, so); 251ade9ccfeSMarcel Moolenaar xo_close_instance("socket"); 2524f81ef50SGarrett Wollman } 2534f81ef50SGarrett Wollman if (xug != oxug && xug->xug_gen != oxug->xug_gen) { 2544f81ef50SGarrett Wollman if (oxug->xug_count > xug->xug_count) { 255ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 256ade9ccfeSMarcel Moolenaar "been {:action/deleted}.\n", 2574f81ef50SGarrett Wollman socktype[type]); 2584f81ef50SGarrett Wollman } else if (oxug->xug_count < xug->xug_count) { 259ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 260ade9ccfeSMarcel Moolenaar "been {:action/created}.\n", 2614f81ef50SGarrett Wollman socktype[type]); 2624f81ef50SGarrett Wollman } else { 263ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 264ade9ccfeSMarcel Moolenaar "been {:action/created or deleted}", 2654f81ef50SGarrett Wollman socktype[type]); 2664f81ef50SGarrett Wollman } 2674f81ef50SGarrett Wollman } 2684f81ef50SGarrett Wollman free(buf); 2694f81ef50SGarrett Wollman } 2704f81ef50SGarrett Wollman } 2714f81ef50SGarrett Wollman 2724f81ef50SGarrett Wollman static void 2735e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so) 2744f81ef50SGarrett Wollman { 2754f81ef50SGarrett Wollman struct sockaddr_un *sa; 2769b50d902SRodney W. Grimes static int first = 1; 2777325dfbbSAlfred Perlstein char buf1[33]; 278ade9ccfeSMarcel Moolenaar static const char *titles[2] = { 279ade9ccfeSMarcel Moolenaar "{T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%8.8s} " 280ade9ccfeSMarcel Moolenaar "{T:/%8.8s} {T:/%8.8s} {T:/%8.8s} {T:Addr}\n", 281ade9ccfeSMarcel Moolenaar "{T:/%-16.16s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%16.16s} " 282ade9ccfeSMarcel Moolenaar "{T:/%16.16s} {T:/%16.16s} {T:/%16.16s} {T:Addr}\n" 283ade9ccfeSMarcel Moolenaar }; 284ade9ccfeSMarcel Moolenaar static const char *format[2] = { 285ade9ccfeSMarcel Moolenaar "{q:address/%8lx} {t:type/%-6.6s} " 286ade9ccfeSMarcel Moolenaar "{:receive-bytes-waiting/%6u} " 287ade9ccfeSMarcel Moolenaar "{:send-bytes-waiting/%6u} " 288ade9ccfeSMarcel Moolenaar "{q:vnode/%8lx} {q:connection/%8lx} " 289ade9ccfeSMarcel Moolenaar "{q:first-reference/%8lx} {q:next-reference/%8lx}", 290ade9ccfeSMarcel Moolenaar "{q:address/%16lx} {t:type/%-6.6s} " 291ade9ccfeSMarcel Moolenaar "{:receive-bytes-waiting/%6u} " 292ade9ccfeSMarcel Moolenaar "{:send-bytes-waiting/%6u} " 293ade9ccfeSMarcel Moolenaar "{q:vnode/%16lx} {q:connection/%16lx} " 294ade9ccfeSMarcel Moolenaar "{q:first-reference/%16lx} {q:next-reference/%16lx}" 295ade9ccfeSMarcel Moolenaar }; 296ade9ccfeSMarcel Moolenaar int fmt = (sizeof(void *) == 8) ? 1 : 0; 2979b50d902SRodney W. Grimes 2980e229f34SGleb Smirnoff sa = (xunp->xu_addr.sun_family == AF_UNIX) ? &xunp->xu_addr : NULL; 2994f81ef50SGarrett Wollman 300f1c0a78dSMaxim Konovalov if (first && !Lflag) { 301ade9ccfeSMarcel Moolenaar xo_emit("{T:Active UNIX domain sockets}\n"); 302ade9ccfeSMarcel Moolenaar xo_emit(titles[fmt], 3039b50d902SRodney W. Grimes "Address", "Type", "Recv-Q", "Send-Q", 3049b50d902SRodney W. Grimes "Inode", "Conn", "Refs", "Nextref"); 3059b50d902SRodney W. Grimes first = 0; 3069b50d902SRodney W. Grimes } 307f1c0a78dSMaxim Konovalov 308f1c0a78dSMaxim Konovalov if (Lflag && so->so_qlimit == 0) 309f1c0a78dSMaxim Konovalov return; 310f1c0a78dSMaxim Konovalov 311f1c0a78dSMaxim Konovalov if (Lflag) { 3127325dfbbSAlfred Perlstein snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen, 313f1c0a78dSMaxim Konovalov so->so_incqlen, so->so_qlimit); 3147325dfbbSAlfred Perlstein xo_emit("unix {d:socket/%-32.32s}{e:queue-length/%u}" 3157325dfbbSAlfred Perlstein "{e:incomplete-queue-length/%u}{e:queue-limit/%u}", 316ade9ccfeSMarcel Moolenaar buf1, so->so_qlen, so->so_incqlen, so->so_qlimit); 317f1c0a78dSMaxim Konovalov } else { 318ade9ccfeSMarcel Moolenaar xo_emit(format[fmt], 3194f81ef50SGarrett Wollman (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc, 3200e229f34SGleb Smirnoff so->so_snd.sb_cc, (long)xunp->unp_vnode, 3210e229f34SGleb Smirnoff (long)xunp->unp_conn, (long)xunp->xu_firstref, 3220e229f34SGleb Smirnoff (long)xunp->xu_nextref); 323f1c0a78dSMaxim Konovalov } 324e4bb0b9aSGarrett Wollman if (sa) 325ade9ccfeSMarcel Moolenaar xo_emit(" {:path/%.*s}", 32622694ebaSBruce Evans (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)), 327bc6ee716SAndrey A. Chernov sa->sun_path); 328ade9ccfeSMarcel Moolenaar xo_emit("\n"); 3299b50d902SRodney W. Grimes } 330