19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 305d422d6aSPhilippe Charnier #if 0 316cc6f122SPhilippe Charnier #ifndef lint 329b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93"; 339b50d902SRodney W. Grimes #endif /* not lint */ 346cc6f122SPhilippe Charnier #endif 356cc6f122SPhilippe Charnier 366cc6f122SPhilippe Charnier #include <sys/cdefs.h> 376cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 389b50d902SRodney W. Grimes 399b50d902SRodney W. Grimes /* 409b50d902SRodney W. Grimes * Display protocol blocks in the unix domain. 419b50d902SRodney W. Grimes */ 429b50d902SRodney W. Grimes #include <sys/param.h> 43d8d89152SDavid Greenman #include <sys/queue.h> 449b50d902SRodney W. Grimes #include <sys/protosw.h> 459b50d902SRodney W. Grimes #include <sys/socket.h> 469b50d902SRodney W. Grimes #include <sys/socketvar.h> 479b50d902SRodney W. Grimes #include <sys/mbuf.h> 489b50d902SRodney W. Grimes #include <sys/sysctl.h> 499b50d902SRodney W. Grimes #include <sys/un.h> 509b50d902SRodney W. Grimes #include <sys/unpcb.h> 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <netinet/in.h> 539b50d902SRodney W. Grimes 544f81ef50SGarrett Wollman #include <errno.h> 55c6620a13SPoul-Henning Kamp #include <err.h> 568ad9b1a3SGarrett Wollman #include <stddef.h> 577b95a1ebSYaroslav Tykhiy #include <stdint.h> 589b50d902SRodney W. Grimes #include <stdio.h> 599b50d902SRodney W. Grimes #include <stdlib.h> 60*ade9ccfeSMarcel Moolenaar #include <stdbool.h> 61feda1a43SJohn Baldwin #include <strings.h> 624f81ef50SGarrett Wollman #include <kvm.h> 63*ade9ccfeSMarcel Moolenaar #include <libxo/xo.h> 649b50d902SRodney W. Grimes #include "netstat.h" 659b50d902SRodney W. Grimes 665e051718SAssar Westerlund static void unixdomainpr(struct xunpcb *, struct xsocket *); 679b50d902SRodney W. Grimes 684f81ef50SGarrett Wollman static const char *const socktype[] = 699b50d902SRodney W. Grimes { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" }; 709b50d902SRodney W. Grimes 71feda1a43SJohn Baldwin static int 72feda1a43SJohn Baldwin pcblist_sysctl(int type, char **bufp) 739b50d902SRodney W. Grimes { 744f81ef50SGarrett Wollman char *buf; 754f81ef50SGarrett Wollman size_t len; 764f81ef50SGarrett Wollman char mibvar[sizeof "net.local.seqpacket.pcblist"]; 774f81ef50SGarrett Wollman 784f81ef50SGarrett Wollman sprintf(mibvar, "net.local.%s.pcblist", socktype[type]); 794f81ef50SGarrett Wollman 804f81ef50SGarrett Wollman len = 0; 814f81ef50SGarrett Wollman if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { 824f81ef50SGarrett Wollman if (errno != ENOENT) 83*ade9ccfeSMarcel Moolenaar xo_warn("sysctl: %s", mibvar); 84feda1a43SJohn Baldwin return (-1); 854f81ef50SGarrett Wollman } 864f81ef50SGarrett Wollman if ((buf = malloc(len)) == 0) { 87*ade9ccfeSMarcel Moolenaar xo_warnx("malloc %lu bytes", (u_long)len); 88feda1a43SJohn Baldwin return (-2); 894f81ef50SGarrett Wollman } 904f81ef50SGarrett Wollman if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { 91*ade9ccfeSMarcel Moolenaar xo_warn("sysctl: %s", mibvar); 924f81ef50SGarrett Wollman free(buf); 93feda1a43SJohn Baldwin return (-2); 944f81ef50SGarrett Wollman } 95feda1a43SJohn Baldwin *bufp = buf; 96feda1a43SJohn Baldwin return (0); 97feda1a43SJohn Baldwin } 98feda1a43SJohn Baldwin 99feda1a43SJohn Baldwin static int 100feda1a43SJohn Baldwin pcblist_kvm(u_long count_off, u_long gencnt_off, u_long head_off, char **bufp) 101feda1a43SJohn Baldwin { 102feda1a43SJohn Baldwin struct unp_head head; 103feda1a43SJohn Baldwin struct unpcb *unp, unp_conn; 104feda1a43SJohn Baldwin u_char sun_len; 105feda1a43SJohn Baldwin struct socket so; 106feda1a43SJohn Baldwin struct xunpgen xug; 107feda1a43SJohn Baldwin struct xunpcb xu; 108feda1a43SJohn Baldwin unp_gen_t unp_gencnt; 109feda1a43SJohn Baldwin u_int unp_count; 110feda1a43SJohn Baldwin char *buf, *p; 111feda1a43SJohn Baldwin size_t len; 112feda1a43SJohn Baldwin 113feda1a43SJohn Baldwin if (count_off == 0 || gencnt_off == 0) 114feda1a43SJohn Baldwin return (-2); 115feda1a43SJohn Baldwin if (head_off == 0) 116feda1a43SJohn Baldwin return (-1); 117feda1a43SJohn Baldwin kread(count_off, &unp_count, sizeof(unp_count)); 118feda1a43SJohn Baldwin len = 2 * sizeof(xug) + (unp_count + unp_count / 8) * sizeof(xu); 119feda1a43SJohn Baldwin if ((buf = malloc(len)) == 0) { 120*ade9ccfeSMarcel Moolenaar xo_warnx("malloc %lu bytes", (u_long)len); 121feda1a43SJohn Baldwin return (-2); 122feda1a43SJohn Baldwin } 123feda1a43SJohn Baldwin p = buf; 124feda1a43SJohn Baldwin 125feda1a43SJohn Baldwin #define COPYOUT(obj, size) do { \ 126feda1a43SJohn Baldwin if (len < (size)) { \ 127*ade9ccfeSMarcel Moolenaar xo_warnx("buffer size exceeded"); \ 128feda1a43SJohn Baldwin goto fail; \ 129feda1a43SJohn Baldwin } \ 130feda1a43SJohn Baldwin bcopy((obj), p, (size)); \ 131feda1a43SJohn Baldwin len -= (size); \ 132feda1a43SJohn Baldwin p += (size); \ 133feda1a43SJohn Baldwin } while (0) 134feda1a43SJohn Baldwin 135feda1a43SJohn Baldwin #define KREAD(off, buf, len) do { \ 136feda1a43SJohn Baldwin if (kread((uintptr_t)(off), (buf), (len)) != 0) \ 137feda1a43SJohn Baldwin goto fail; \ 138feda1a43SJohn Baldwin } while (0) 139feda1a43SJohn Baldwin 140feda1a43SJohn Baldwin /* Write out header. */ 141feda1a43SJohn Baldwin kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt)); 142feda1a43SJohn Baldwin xug.xug_len = sizeof xug; 143feda1a43SJohn Baldwin xug.xug_count = unp_count; 144feda1a43SJohn Baldwin xug.xug_gen = unp_gencnt; 145feda1a43SJohn Baldwin xug.xug_sogen = 0; 146feda1a43SJohn Baldwin COPYOUT(&xug, sizeof xug); 147feda1a43SJohn Baldwin 148feda1a43SJohn Baldwin /* Walk the PCB list. */ 149feda1a43SJohn Baldwin xu.xu_len = sizeof xu; 150feda1a43SJohn Baldwin KREAD(head_off, &head, sizeof(head)); 151feda1a43SJohn Baldwin LIST_FOREACH(unp, &head, unp_link) { 152feda1a43SJohn Baldwin xu.xu_unpp = unp; 153feda1a43SJohn Baldwin KREAD(unp, &xu.xu_unp, sizeof (*unp)); 154feda1a43SJohn Baldwin unp = &xu.xu_unp; 155feda1a43SJohn Baldwin 156feda1a43SJohn Baldwin if (unp->unp_gencnt > unp_gencnt) 157feda1a43SJohn Baldwin continue; 158feda1a43SJohn Baldwin if (unp->unp_addr != NULL) { 159feda1a43SJohn Baldwin KREAD(unp->unp_addr, &sun_len, sizeof(sun_len)); 160feda1a43SJohn Baldwin KREAD(unp->unp_addr, &xu.xu_addr, sun_len); 161feda1a43SJohn Baldwin } 162feda1a43SJohn Baldwin if (unp->unp_conn != NULL) { 163feda1a43SJohn Baldwin KREAD(unp->unp_conn, &unp_conn, sizeof(unp_conn)); 164feda1a43SJohn Baldwin if (unp_conn.unp_addr != NULL) { 165feda1a43SJohn Baldwin KREAD(unp_conn.unp_addr, &sun_len, 166feda1a43SJohn Baldwin sizeof(sun_len)); 167feda1a43SJohn Baldwin KREAD(unp_conn.unp_addr, &xu.xu_caddr, sun_len); 168feda1a43SJohn Baldwin } 169feda1a43SJohn Baldwin } 170feda1a43SJohn Baldwin KREAD(unp->unp_socket, &so, sizeof(so)); 171feda1a43SJohn Baldwin if (sotoxsocket(&so, &xu.xu_socket) != 0) 172feda1a43SJohn Baldwin goto fail; 173feda1a43SJohn Baldwin COPYOUT(&xu, sizeof(xu)); 174feda1a43SJohn Baldwin } 175feda1a43SJohn Baldwin 176feda1a43SJohn Baldwin /* Reread the counts and write the footer. */ 177feda1a43SJohn Baldwin kread(count_off, &unp_count, sizeof(unp_count)); 178feda1a43SJohn Baldwin kread(gencnt_off, &unp_gencnt, sizeof(unp_gencnt)); 179feda1a43SJohn Baldwin xug.xug_count = unp_count; 180feda1a43SJohn Baldwin xug.xug_gen = unp_gencnt; 181feda1a43SJohn Baldwin COPYOUT(&xug, sizeof xug); 182feda1a43SJohn Baldwin 183feda1a43SJohn Baldwin *bufp = buf; 184feda1a43SJohn Baldwin return (0); 185feda1a43SJohn Baldwin 186feda1a43SJohn Baldwin fail: 187feda1a43SJohn Baldwin free(buf); 188feda1a43SJohn Baldwin return (-1); 189feda1a43SJohn Baldwin #undef COPYOUT 190feda1a43SJohn Baldwin #undef KREAD 191feda1a43SJohn Baldwin } 192feda1a43SJohn Baldwin 193feda1a43SJohn Baldwin void 194963b7ccdSRobert Watson unixpr(u_long count_off, u_long gencnt_off, u_long dhead_off, u_long shead_off, 195*ade9ccfeSMarcel Moolenaar u_long sphead_off, bool *first) 196feda1a43SJohn Baldwin { 197feda1a43SJohn Baldwin char *buf; 198feda1a43SJohn Baldwin int ret, type; 199feda1a43SJohn Baldwin struct xsocket *so; 200feda1a43SJohn Baldwin struct xunpgen *xug, *oxug; 201feda1a43SJohn Baldwin struct xunpcb *xunp; 202963b7ccdSRobert Watson u_long head_off; 203feda1a43SJohn Baldwin 204321ae07fSPhilippe Charnier buf = NULL; 205feda1a43SJohn Baldwin for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) { 206feda1a43SJohn Baldwin if (live) 207feda1a43SJohn Baldwin ret = pcblist_sysctl(type, &buf); 208963b7ccdSRobert Watson else { 209963b7ccdSRobert Watson head_off = 0; 210963b7ccdSRobert Watson switch (type) { 211963b7ccdSRobert Watson case SOCK_STREAM: 212963b7ccdSRobert Watson head_off = shead_off; 213963b7ccdSRobert Watson break; 214963b7ccdSRobert Watson 215963b7ccdSRobert Watson case SOCK_DGRAM: 216963b7ccdSRobert Watson head_off = dhead_off; 217963b7ccdSRobert Watson break; 218963b7ccdSRobert Watson 219963b7ccdSRobert Watson case SOCK_SEQPACKET: 220963b7ccdSRobert Watson head_off = sphead_off; 221963b7ccdSRobert Watson break; 222963b7ccdSRobert Watson } 223963b7ccdSRobert Watson ret = pcblist_kvm(count_off, gencnt_off, head_off, 224963b7ccdSRobert Watson &buf); 225963b7ccdSRobert Watson } 226feda1a43SJohn Baldwin if (ret == -1) 227feda1a43SJohn Baldwin continue; 228feda1a43SJohn Baldwin if (ret < 0) 229feda1a43SJohn Baldwin return; 2304f81ef50SGarrett Wollman 2314f81ef50SGarrett Wollman oxug = xug = (struct xunpgen *)buf; 2324f81ef50SGarrett Wollman for (xug = (struct xunpgen *)((char *)xug + xug->xug_len); 2334f81ef50SGarrett Wollman xug->xug_len > sizeof(struct xunpgen); 2344f81ef50SGarrett Wollman xug = (struct xunpgen *)((char *)xug + xug->xug_len)) { 2354f81ef50SGarrett Wollman xunp = (struct xunpcb *)xug; 2364f81ef50SGarrett Wollman so = &xunp->xu_socket; 2374f81ef50SGarrett Wollman 2384f81ef50SGarrett Wollman /* Ignore PCBs which were freed during copyout. */ 2394f81ef50SGarrett Wollman if (xunp->xu_unp.unp_gencnt > oxug->xug_gen) 2404f81ef50SGarrett Wollman continue; 241*ade9ccfeSMarcel Moolenaar if (*first) { 242*ade9ccfeSMarcel Moolenaar xo_open_list("socket"); 243*ade9ccfeSMarcel Moolenaar *first = false; 244*ade9ccfeSMarcel Moolenaar } 245*ade9ccfeSMarcel Moolenaar xo_open_instance("socket"); 2464f81ef50SGarrett Wollman unixdomainpr(xunp, so); 247*ade9ccfeSMarcel Moolenaar xo_close_instance("socket"); 2484f81ef50SGarrett Wollman } 2494f81ef50SGarrett Wollman if (xug != oxug && xug->xug_gen != oxug->xug_gen) { 2504f81ef50SGarrett Wollman if (oxug->xug_count > xug->xug_count) { 251*ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 252*ade9ccfeSMarcel Moolenaar "been {:action/deleted}.\n", 2534f81ef50SGarrett Wollman socktype[type]); 2544f81ef50SGarrett Wollman } else if (oxug->xug_count < xug->xug_count) { 255*ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 256*ade9ccfeSMarcel Moolenaar "been {:action/created}.\n", 2574f81ef50SGarrett Wollman socktype[type]); 2584f81ef50SGarrett Wollman } else { 259*ade9ccfeSMarcel Moolenaar xo_emit("Some {:type/%s} sockets may have " 260*ade9ccfeSMarcel Moolenaar "been {:action/created or deleted}", 2614f81ef50SGarrett Wollman socktype[type]); 2624f81ef50SGarrett Wollman } 2634f81ef50SGarrett Wollman } 2644f81ef50SGarrett Wollman free(buf); 2654f81ef50SGarrett Wollman } 2664f81ef50SGarrett Wollman } 2674f81ef50SGarrett Wollman 2684f81ef50SGarrett Wollman static void 2695e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so) 2704f81ef50SGarrett Wollman { 2714f81ef50SGarrett Wollman struct unpcb *unp; 2724f81ef50SGarrett Wollman struct sockaddr_un *sa; 2739b50d902SRodney W. Grimes static int first = 1; 274f1c0a78dSMaxim Konovalov char buf1[15]; 275*ade9ccfeSMarcel Moolenaar static const char *titles[2] = { 276*ade9ccfeSMarcel Moolenaar "{T:/%-8.8s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%8.8s} " 277*ade9ccfeSMarcel Moolenaar "{T:/%8.8s} {T:/%8.8s} {T:/%8.8s} {T:Addr}\n", 278*ade9ccfeSMarcel Moolenaar "{T:/%-16.16s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%16.16s} " 279*ade9ccfeSMarcel Moolenaar "{T:/%16.16s} {T:/%16.16s} {T:/%16.16s} {T:Addr}\n" 280*ade9ccfeSMarcel Moolenaar }; 281*ade9ccfeSMarcel Moolenaar static const char *format[2] = { 282*ade9ccfeSMarcel Moolenaar "{q:address/%8lx} {t:type/%-6.6s} " 283*ade9ccfeSMarcel Moolenaar "{:receive-bytes-waiting/%6u} " 284*ade9ccfeSMarcel Moolenaar "{:send-bytes-waiting/%6u} " 285*ade9ccfeSMarcel Moolenaar "{q:vnode/%8lx} {q:connection/%8lx} " 286*ade9ccfeSMarcel Moolenaar "{q:first-reference/%8lx} {q:next-reference/%8lx}", 287*ade9ccfeSMarcel Moolenaar "{q:address/%16lx} {t:type/%-6.6s} " 288*ade9ccfeSMarcel Moolenaar "{:receive-bytes-waiting/%6u} " 289*ade9ccfeSMarcel Moolenaar "{:send-bytes-waiting/%6u} " 290*ade9ccfeSMarcel Moolenaar "{q:vnode/%16lx} {q:connection/%16lx} " 291*ade9ccfeSMarcel Moolenaar "{q:first-reference/%16lx} {q:next-reference/%16lx}" 292*ade9ccfeSMarcel Moolenaar }; 293*ade9ccfeSMarcel Moolenaar int fmt = (sizeof(void *) == 8) ? 1 : 0; 2949b50d902SRodney W. Grimes 2954f81ef50SGarrett Wollman unp = &xunp->xu_unp; 2964f81ef50SGarrett Wollman if (unp->unp_addr) 2974f81ef50SGarrett Wollman sa = &xunp->xu_addr; 2984f81ef50SGarrett Wollman else 299e4bb0b9aSGarrett Wollman sa = (struct sockaddr_un *)0; 3004f81ef50SGarrett Wollman 301f1c0a78dSMaxim Konovalov if (first && !Lflag) { 302*ade9ccfeSMarcel Moolenaar xo_emit("{T:Active UNIX domain sockets}\n"); 303*ade9ccfeSMarcel Moolenaar xo_emit(titles[fmt], 3049b50d902SRodney W. Grimes "Address", "Type", "Recv-Q", "Send-Q", 3059b50d902SRodney W. Grimes "Inode", "Conn", "Refs", "Nextref"); 3069b50d902SRodney W. Grimes first = 0; 3079b50d902SRodney W. Grimes } 308f1c0a78dSMaxim Konovalov 309f1c0a78dSMaxim Konovalov if (Lflag && so->so_qlimit == 0) 310f1c0a78dSMaxim Konovalov return; 311f1c0a78dSMaxim Konovalov 312f1c0a78dSMaxim Konovalov if (Lflag) { 313f1c0a78dSMaxim Konovalov snprintf(buf1, 15, "%d/%d/%d", so->so_qlen, 314f1c0a78dSMaxim Konovalov so->so_incqlen, so->so_qlimit); 315*ade9ccfeSMarcel Moolenaar xo_emit("unix {d:socket/%-14.14s}{e:queue-length/%d}" 316*ade9ccfeSMarcel Moolenaar "{e:incomplete-queue-length/%d}{e:queue-limit/%d}", 317*ade9ccfeSMarcel Moolenaar buf1, so->so_qlen, so->so_incqlen, so->so_qlimit); 318f1c0a78dSMaxim Konovalov } else { 319*ade9ccfeSMarcel Moolenaar xo_emit(format[fmt], 3204f81ef50SGarrett Wollman (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc, 3210f9d0a73SGleb Smirnoff so->so_snd.sb_cc, (long)unp->unp_vnode, 3220f9d0a73SGleb Smirnoff (long)unp->unp_conn, 323f1c0a78dSMaxim Konovalov (long)LIST_FIRST(&unp->unp_refs), 324f1c0a78dSMaxim Konovalov (long)LIST_NEXT(unp, unp_reflink)); 325f1c0a78dSMaxim Konovalov } 326e4bb0b9aSGarrett Wollman if (sa) 327*ade9ccfeSMarcel Moolenaar xo_emit(" {:path/%.*s}", 32822694ebaSBruce Evans (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)), 329bc6ee716SAndrey A. Chernov sa->sun_path); 330*ade9ccfeSMarcel Moolenaar xo_emit("\n"); 3319b50d902SRodney W. Grimes } 332