165475bc8SDavid E. O'Brien /*- 205ddff6eSPeter Wemm * Copyright (c) 1983, 1988, 1993, 1995 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 306cc6f122SPhilippe Charnier #if 0 319b50d902SRodney W. Grimes #ifndef lint 3205ddff6eSPeter Wemm static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95"; 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 #include <sys/param.h> 40d8d89152SDavid Greenman #include <sys/queue.h> 41feda1a43SJohn Baldwin #include <sys/domain.h> 42f35a2092SMaksim Yevmenkin #include <sys/protosw.h> 439b50d902SRodney W. Grimes #include <sys/socket.h> 449b50d902SRodney W. Grimes #include <sys/socketvar.h> 454e00c309SGarrett Wollman #include <sys/sysctl.h> 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <net/route.h> 4854fc657dSGeorge V. Neville-Neil #include <net/if_arp.h> 499b50d902SRodney W. Grimes #include <netinet/in.h> 509b50d902SRodney W. Grimes #include <netinet/in_systm.h> 519b50d902SRodney W. Grimes #include <netinet/ip.h> 52a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 53cfa1ca9dSYoshinobu Inoue #ifdef INET6 54cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 55cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 569b50d902SRodney W. Grimes #include <netinet/in_pcb.h> 579b50d902SRodney W. Grimes #include <netinet/ip_icmp.h> 589b50d902SRodney W. Grimes #include <netinet/icmp_var.h> 599b50d902SRodney W. Grimes #include <netinet/igmp_var.h> 609b50d902SRodney W. Grimes #include <netinet/ip_var.h> 61c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h> 629b50d902SRodney W. Grimes #include <netinet/tcp.h> 639b50d902SRodney W. Grimes #include <netinet/tcpip.h> 649b50d902SRodney W. Grimes #include <netinet/tcp_seq.h> 659b50d902SRodney W. Grimes #define TCPSTATES 669b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h> 679b50d902SRodney W. Grimes #include <netinet/tcp_timer.h> 689b50d902SRodney W. Grimes #include <netinet/tcp_var.h> 699b50d902SRodney W. Grimes #include <netinet/tcp_debug.h> 709b50d902SRodney W. Grimes #include <netinet/udp.h> 719b50d902SRodney W. Grimes #include <netinet/udp_var.h> 729b50d902SRodney W. Grimes 739b50d902SRodney W. Grimes #include <arpa/inet.h> 744f81ef50SGarrett Wollman #include <err.h> 754f81ef50SGarrett Wollman #include <errno.h> 76d121b556SBrian Somers #include <libutil.h> 779b50d902SRodney W. Grimes #include <netdb.h> 787b95a1ebSYaroslav Tykhiy #include <stdint.h> 799b50d902SRodney W. Grimes #include <stdio.h> 804f81ef50SGarrett Wollman #include <stdlib.h> 819b50d902SRodney W. Grimes #include <string.h> 829b50d902SRodney W. Grimes #include <unistd.h> 839b50d902SRodney W. Grimes #include "netstat.h" 849b50d902SRodney W. Grimes 855e051718SAssar Westerlund char *inetname(struct in_addr *); 86a01e3379SDavid Malone void inetprint(struct in_addr *, int, const char *, int); 87cfa1ca9dSYoshinobu Inoue #ifdef INET6 88aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done; 89cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 909b50d902SRodney W. Grimes 91feda1a43SJohn Baldwin static int 92321ae07fSPhilippe Charnier pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused) 93feda1a43SJohn Baldwin { 94feda1a43SJohn Baldwin const char *mibvar; 95feda1a43SJohn Baldwin char *buf; 96feda1a43SJohn Baldwin size_t len; 97feda1a43SJohn Baldwin 98feda1a43SJohn Baldwin switch (proto) { 99feda1a43SJohn Baldwin case IPPROTO_TCP: 100feda1a43SJohn Baldwin mibvar = "net.inet.tcp.pcblist"; 101feda1a43SJohn Baldwin break; 102feda1a43SJohn Baldwin case IPPROTO_UDP: 103feda1a43SJohn Baldwin mibvar = "net.inet.udp.pcblist"; 104feda1a43SJohn Baldwin break; 105feda1a43SJohn Baldwin case IPPROTO_DIVERT: 106feda1a43SJohn Baldwin mibvar = "net.inet.divert.pcblist"; 107feda1a43SJohn Baldwin break; 108feda1a43SJohn Baldwin default: 109feda1a43SJohn Baldwin mibvar = "net.inet.raw.pcblist"; 110feda1a43SJohn Baldwin break; 111feda1a43SJohn Baldwin } 112aa0a1e58SJeff Roberson if (strncmp(name, "sdp", 3) == 0) 113aa0a1e58SJeff Roberson mibvar = "net.inet.sdp.pcblist"; 114feda1a43SJohn Baldwin len = 0; 115feda1a43SJohn Baldwin if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { 116feda1a43SJohn Baldwin if (errno != ENOENT) 117feda1a43SJohn Baldwin warn("sysctl: %s", mibvar); 118feda1a43SJohn Baldwin return (0); 119feda1a43SJohn Baldwin } 120feda1a43SJohn Baldwin if ((buf = malloc(len)) == 0) { 121feda1a43SJohn Baldwin warnx("malloc %lu bytes", (u_long)len); 122feda1a43SJohn Baldwin return (0); 123feda1a43SJohn Baldwin } 124feda1a43SJohn Baldwin if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { 125feda1a43SJohn Baldwin warn("sysctl: %s", mibvar); 126feda1a43SJohn Baldwin free(buf); 127feda1a43SJohn Baldwin return (0); 128feda1a43SJohn Baldwin } 129feda1a43SJohn Baldwin *bufp = buf; 130feda1a43SJohn Baldwin return (1); 131feda1a43SJohn Baldwin } 132feda1a43SJohn Baldwin 133feda1a43SJohn Baldwin /* 134feda1a43SJohn Baldwin * Copied directly from uipc_socket2.c. We leave out some fields that are in 135feda1a43SJohn Baldwin * nested structures that aren't used to avoid extra work. 136feda1a43SJohn Baldwin */ 137feda1a43SJohn Baldwin static void 138feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 139feda1a43SJohn Baldwin { 140feda1a43SJohn Baldwin xsb->sb_cc = sb->sb_cc; 141feda1a43SJohn Baldwin xsb->sb_hiwat = sb->sb_hiwat; 142feda1a43SJohn Baldwin xsb->sb_mbcnt = sb->sb_mbcnt; 14349f287f8SGeorge V. Neville-Neil xsb->sb_mcnt = sb->sb_mcnt; 14449f287f8SGeorge V. Neville-Neil xsb->sb_ccnt = sb->sb_ccnt; 145feda1a43SJohn Baldwin xsb->sb_mbmax = sb->sb_mbmax; 146feda1a43SJohn Baldwin xsb->sb_lowat = sb->sb_lowat; 147feda1a43SJohn Baldwin xsb->sb_flags = sb->sb_flags; 148feda1a43SJohn Baldwin xsb->sb_timeo = sb->sb_timeo; 149feda1a43SJohn Baldwin } 150feda1a43SJohn Baldwin 151feda1a43SJohn Baldwin int 152feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso) 153feda1a43SJohn Baldwin { 154feda1a43SJohn Baldwin struct protosw proto; 155feda1a43SJohn Baldwin struct domain domain; 156feda1a43SJohn Baldwin 157feda1a43SJohn Baldwin bzero(xso, sizeof *xso); 158feda1a43SJohn Baldwin xso->xso_len = sizeof *xso; 159feda1a43SJohn Baldwin xso->xso_so = so; 160feda1a43SJohn Baldwin xso->so_type = so->so_type; 161feda1a43SJohn Baldwin xso->so_options = so->so_options; 162feda1a43SJohn Baldwin xso->so_linger = so->so_linger; 163feda1a43SJohn Baldwin xso->so_state = so->so_state; 164feda1a43SJohn Baldwin xso->so_pcb = so->so_pcb; 165feda1a43SJohn Baldwin if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0) 166feda1a43SJohn Baldwin return (-1); 167feda1a43SJohn Baldwin xso->xso_protocol = proto.pr_protocol; 168feda1a43SJohn Baldwin if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0) 169feda1a43SJohn Baldwin return (-1); 170feda1a43SJohn Baldwin xso->xso_family = domain.dom_family; 171feda1a43SJohn Baldwin xso->so_qlen = so->so_qlen; 172feda1a43SJohn Baldwin xso->so_incqlen = so->so_incqlen; 173feda1a43SJohn Baldwin xso->so_qlimit = so->so_qlimit; 174feda1a43SJohn Baldwin xso->so_timeo = so->so_timeo; 175feda1a43SJohn Baldwin xso->so_error = so->so_error; 176feda1a43SJohn Baldwin xso->so_oobmark = so->so_oobmark; 177feda1a43SJohn Baldwin sbtoxsockbuf(&so->so_snd, &xso->so_snd); 178feda1a43SJohn Baldwin sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 179feda1a43SJohn Baldwin return (0); 180feda1a43SJohn Baldwin } 181feda1a43SJohn Baldwin 182feda1a43SJohn Baldwin static int 183feda1a43SJohn Baldwin pcblist_kvm(u_long off, char **bufp, int istcp) 184feda1a43SJohn Baldwin { 185feda1a43SJohn Baldwin struct inpcbinfo pcbinfo; 186feda1a43SJohn Baldwin struct inpcbhead listhead; 187feda1a43SJohn Baldwin struct inpcb *inp; 188feda1a43SJohn Baldwin struct xinpcb xi; 189feda1a43SJohn Baldwin struct xinpgen xig; 190feda1a43SJohn Baldwin struct xtcpcb xt; 191feda1a43SJohn Baldwin struct socket so; 192feda1a43SJohn Baldwin struct xsocket *xso; 193feda1a43SJohn Baldwin char *buf, *p; 194feda1a43SJohn Baldwin size_t len; 195feda1a43SJohn Baldwin 196feda1a43SJohn Baldwin if (off == 0) 197feda1a43SJohn Baldwin return (0); 198feda1a43SJohn Baldwin kread(off, &pcbinfo, sizeof(pcbinfo)); 199feda1a43SJohn Baldwin if (istcp) 200feda1a43SJohn Baldwin len = 2 * sizeof(xig) + 201feda1a43SJohn Baldwin (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) * 202feda1a43SJohn Baldwin sizeof(struct xtcpcb); 203feda1a43SJohn Baldwin else 204feda1a43SJohn Baldwin len = 2 * sizeof(xig) + 205feda1a43SJohn Baldwin (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) * 206feda1a43SJohn Baldwin sizeof(struct xinpcb); 207feda1a43SJohn Baldwin if ((buf = malloc(len)) == 0) { 208feda1a43SJohn Baldwin warnx("malloc %lu bytes", (u_long)len); 209feda1a43SJohn Baldwin return (0); 210feda1a43SJohn Baldwin } 211feda1a43SJohn Baldwin p = buf; 212feda1a43SJohn Baldwin 213feda1a43SJohn Baldwin #define COPYOUT(obj, size) do { \ 214feda1a43SJohn Baldwin if (len < (size)) { \ 215feda1a43SJohn Baldwin warnx("buffer size exceeded"); \ 216feda1a43SJohn Baldwin goto fail; \ 217feda1a43SJohn Baldwin } \ 218feda1a43SJohn Baldwin bcopy((obj), p, (size)); \ 219feda1a43SJohn Baldwin len -= (size); \ 220feda1a43SJohn Baldwin p += (size); \ 221feda1a43SJohn Baldwin } while (0) 222feda1a43SJohn Baldwin 223feda1a43SJohn Baldwin #define KREAD(off, buf, len) do { \ 224feda1a43SJohn Baldwin if (kread((uintptr_t)(off), (buf), (len)) != 0) \ 225feda1a43SJohn Baldwin goto fail; \ 226feda1a43SJohn Baldwin } while (0) 227feda1a43SJohn Baldwin 228feda1a43SJohn Baldwin /* Write out header. */ 229feda1a43SJohn Baldwin xig.xig_len = sizeof xig; 230feda1a43SJohn Baldwin xig.xig_count = pcbinfo.ipi_count; 231feda1a43SJohn Baldwin xig.xig_gen = pcbinfo.ipi_gencnt; 232feda1a43SJohn Baldwin xig.xig_sogen = 0; 233feda1a43SJohn Baldwin COPYOUT(&xig, sizeof xig); 234feda1a43SJohn Baldwin 235feda1a43SJohn Baldwin /* Walk the PCB list. */ 236feda1a43SJohn Baldwin xt.xt_len = sizeof xt; 237feda1a43SJohn Baldwin xi.xi_len = sizeof xi; 238feda1a43SJohn Baldwin if (istcp) 239feda1a43SJohn Baldwin xso = &xt.xt_socket; 240feda1a43SJohn Baldwin else 241feda1a43SJohn Baldwin xso = &xi.xi_socket; 242feda1a43SJohn Baldwin KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead)); 243feda1a43SJohn Baldwin LIST_FOREACH(inp, &listhead, inp_list) { 244feda1a43SJohn Baldwin if (istcp) { 245feda1a43SJohn Baldwin KREAD(inp, &xt.xt_inp, sizeof(*inp)); 246feda1a43SJohn Baldwin inp = &xt.xt_inp; 247feda1a43SJohn Baldwin } else { 248feda1a43SJohn Baldwin KREAD(inp, &xi.xi_inp, sizeof(*inp)); 249feda1a43SJohn Baldwin inp = &xi.xi_inp; 250feda1a43SJohn Baldwin } 251feda1a43SJohn Baldwin 252feda1a43SJohn Baldwin if (inp->inp_gencnt > pcbinfo.ipi_gencnt) 253feda1a43SJohn Baldwin continue; 254feda1a43SJohn Baldwin 255feda1a43SJohn Baldwin if (istcp) { 256feda1a43SJohn Baldwin if (inp->inp_ppcb == NULL) 257feda1a43SJohn Baldwin bzero(&xt.xt_tp, sizeof xt.xt_tp); 258ad71fe3cSRobert Watson else if (inp->inp_flags & INP_TIMEWAIT) { 259feda1a43SJohn Baldwin bzero(&xt.xt_tp, sizeof xt.xt_tp); 260feda1a43SJohn Baldwin xt.xt_tp.t_state = TCPS_TIME_WAIT; 261feda1a43SJohn Baldwin } else 262feda1a43SJohn Baldwin KREAD(inp->inp_ppcb, &xt.xt_tp, 263feda1a43SJohn Baldwin sizeof xt.xt_tp); 264feda1a43SJohn Baldwin } 265feda1a43SJohn Baldwin if (inp->inp_socket) { 266feda1a43SJohn Baldwin KREAD(inp->inp_socket, &so, sizeof(so)); 267feda1a43SJohn Baldwin if (sotoxsocket(&so, xso) != 0) 268feda1a43SJohn Baldwin goto fail; 269feda1a43SJohn Baldwin } else { 270feda1a43SJohn Baldwin bzero(xso, sizeof(*xso)); 271feda1a43SJohn Baldwin if (istcp) 272feda1a43SJohn Baldwin xso->xso_protocol = IPPROTO_TCP; 273feda1a43SJohn Baldwin } 274feda1a43SJohn Baldwin if (istcp) 275feda1a43SJohn Baldwin COPYOUT(&xt, sizeof xt); 276feda1a43SJohn Baldwin else 277feda1a43SJohn Baldwin COPYOUT(&xi, sizeof xi); 278feda1a43SJohn Baldwin } 279feda1a43SJohn Baldwin 280feda1a43SJohn Baldwin /* Reread the pcbinfo and write out the footer. */ 281feda1a43SJohn Baldwin kread(off, &pcbinfo, sizeof(pcbinfo)); 282feda1a43SJohn Baldwin xig.xig_count = pcbinfo.ipi_count; 283feda1a43SJohn Baldwin xig.xig_gen = pcbinfo.ipi_gencnt; 284feda1a43SJohn Baldwin COPYOUT(&xig, sizeof xig); 285feda1a43SJohn Baldwin 286feda1a43SJohn Baldwin *bufp = buf; 287feda1a43SJohn Baldwin return (1); 288feda1a43SJohn Baldwin 289feda1a43SJohn Baldwin fail: 290feda1a43SJohn Baldwin free(buf); 291feda1a43SJohn Baldwin return (0); 292feda1a43SJohn Baldwin #undef COPYOUT 293feda1a43SJohn Baldwin #undef KREAD 294feda1a43SJohn Baldwin } 295feda1a43SJohn Baldwin 2969b50d902SRodney W. Grimes /* 2979b50d902SRodney W. Grimes * Print a summary of connections related to an Internet 2989b50d902SRodney W. Grimes * protocol. For TCP, also give state of connection. 2999b50d902SRodney W. Grimes * Listening processes (aflag) are suppressed unless the 3009b50d902SRodney W. Grimes * -a (all) flag is specified. 3019b50d902SRodney W. Grimes */ 3029b50d902SRodney W. Grimes void 303feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto) 3049b50d902SRodney W. Grimes { 3059b50d902SRodney W. Grimes int istcp; 3069b50d902SRodney W. Grimes static int first = 1; 3074f81ef50SGarrett Wollman char *buf; 308feda1a43SJohn Baldwin const char *vchar; 309a97a9922SJulian Elischer struct tcpcb *tp = NULL; 3104f81ef50SGarrett Wollman struct inpcb *inp; 3114f81ef50SGarrett Wollman struct xinpgen *xig, *oxig; 3124f81ef50SGarrett Wollman struct xsocket *so; 313b8614722SMike Silbersack struct xtcp_timer *timer; 3149b50d902SRodney W. Grimes 3154f81ef50SGarrett Wollman istcp = 0; 3164f81ef50SGarrett Wollman switch (proto) { 3174f81ef50SGarrett Wollman case IPPROTO_TCP: 318cfa1ca9dSYoshinobu Inoue #ifdef INET6 319aa0a1e58SJeff Roberson if (strncmp(name, "sdp", 3) != 0) { 320cfa1ca9dSYoshinobu Inoue if (tcp_done != 0) 321cfa1ca9dSYoshinobu Inoue return; 322cfa1ca9dSYoshinobu Inoue else 323cfa1ca9dSYoshinobu Inoue tcp_done = 1; 324aa0a1e58SJeff Roberson } else { 325aa0a1e58SJeff Roberson if (sdp_done != 0) 326aa0a1e58SJeff Roberson return; 327aa0a1e58SJeff Roberson else 328aa0a1e58SJeff Roberson sdp_done = 1; 329aa0a1e58SJeff Roberson } 330cfa1ca9dSYoshinobu Inoue #endif 3314f81ef50SGarrett Wollman istcp = 1; 3324f81ef50SGarrett Wollman break; 3334f81ef50SGarrett Wollman case IPPROTO_UDP: 334cfa1ca9dSYoshinobu Inoue #ifdef INET6 335cfa1ca9dSYoshinobu Inoue if (udp_done != 0) 336cfa1ca9dSYoshinobu Inoue return; 337cfa1ca9dSYoshinobu Inoue else 338cfa1ca9dSYoshinobu Inoue udp_done = 1; 339cfa1ca9dSYoshinobu Inoue #endif 3404f81ef50SGarrett Wollman break; 3414f81ef50SGarrett Wollman } 342feda1a43SJohn Baldwin if (live) { 343aa0a1e58SJeff Roberson if (!pcblist_sysctl(proto, name, &buf, istcp)) 3449b50d902SRodney W. Grimes return; 345feda1a43SJohn Baldwin } else { 346feda1a43SJohn Baldwin if (!pcblist_kvm(off, &buf, istcp)) 3474f81ef50SGarrett Wollman return; 3484f81ef50SGarrett Wollman } 3494f81ef50SGarrett Wollman 3504f81ef50SGarrett Wollman oxig = xig = (struct xinpgen *)buf; 3514f81ef50SGarrett Wollman for (xig = (struct xinpgen *)((char *)xig + xig->xig_len); 3524f81ef50SGarrett Wollman xig->xig_len > sizeof(struct xinpgen); 3534f81ef50SGarrett Wollman xig = (struct xinpgen *)((char *)xig + xig->xig_len)) { 3549b50d902SRodney W. Grimes if (istcp) { 355b8614722SMike Silbersack timer = &((struct xtcpcb *)xig)->xt_timer; 3564f81ef50SGarrett Wollman tp = &((struct xtcpcb *)xig)->xt_tp; 3574f81ef50SGarrett Wollman inp = &((struct xtcpcb *)xig)->xt_inp; 3584f81ef50SGarrett Wollman so = &((struct xtcpcb *)xig)->xt_socket; 3594f81ef50SGarrett Wollman } else { 3604f81ef50SGarrett Wollman inp = &((struct xinpcb *)xig)->xi_inp; 3614f81ef50SGarrett Wollman so = &((struct xinpcb *)xig)->xi_socket; 362aae09141SMike Silbersack timer = NULL; 3639b50d902SRodney W. Grimes } 3644f81ef50SGarrett Wollman 3654f81ef50SGarrett Wollman /* Ignore sockets for protocols other than the desired one. */ 366feda1a43SJohn Baldwin if (so->xso_protocol != proto) 3674f81ef50SGarrett Wollman continue; 3684f81ef50SGarrett Wollman 3694f81ef50SGarrett Wollman /* Ignore PCBs which were freed during copyout. */ 3704f81ef50SGarrett Wollman if (inp->inp_gencnt > oxig->xig_gen) 3714f81ef50SGarrett Wollman continue; 3724f81ef50SGarrett Wollman 373a01e3379SDavid Malone if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0) 374cfa1ca9dSYoshinobu Inoue #ifdef INET6 375a01e3379SDavid Malone || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0) 376cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 377a01e3379SDavid Malone || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0 378cfa1ca9dSYoshinobu Inoue #ifdef INET6 3793feeb332SDavid E. O'Brien && (inp->inp_vflag & INP_IPV6) == 0 380cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 381cfa1ca9dSYoshinobu Inoue )) 382cfa1ca9dSYoshinobu Inoue ) 383cfa1ca9dSYoshinobu Inoue continue; 384cfa1ca9dSYoshinobu Inoue if (!aflag && 385cfa1ca9dSYoshinobu Inoue ( 3862b286cedSBruce M Simpson (istcp && tp->t_state == TCPS_LISTEN) 3872b286cedSBruce M Simpson || (af1 == AF_INET && 388cfa1ca9dSYoshinobu Inoue inet_lnaof(inp->inp_laddr) == INADDR_ANY) 389cfa1ca9dSYoshinobu Inoue #ifdef INET6 390a01e3379SDavid Malone || (af1 == AF_INET6 && 391cfa1ca9dSYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 392cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 393a01e3379SDavid Malone || (af1 == AF_UNSPEC && 394cfa1ca9dSYoshinobu Inoue (((inp->inp_vflag & INP_IPV4) != 0 && 395cfa1ca9dSYoshinobu Inoue inet_lnaof(inp->inp_laddr) == INADDR_ANY) 396cfa1ca9dSYoshinobu Inoue #ifdef INET6 397cfa1ca9dSYoshinobu Inoue || ((inp->inp_vflag & INP_IPV6) != 0 && 398cfa1ca9dSYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 399cfa1ca9dSYoshinobu Inoue #endif 400cfa1ca9dSYoshinobu Inoue )) 401cfa1ca9dSYoshinobu Inoue )) 4024f81ef50SGarrett Wollman continue; 4034f81ef50SGarrett Wollman 4049b50d902SRodney W. Grimes if (first) { 405ac55add0SGuido van Rooij if (!Lflag) { 4069b50d902SRodney W. Grimes printf("Active Internet connections"); 4079b50d902SRodney W. Grimes if (aflag) 4089b50d902SRodney W. Grimes printf(" (including servers)"); 409ac55add0SGuido van Rooij } else 410ac55add0SGuido van Rooij printf( 411ac55add0SGuido van Rooij "Current listen queue sizes (qlen/incqlen/maxqlen)"); 4129b50d902SRodney W. Grimes putchar('\n'); 4139b50d902SRodney W. Grimes if (Aflag) 414afc74015SRuslan Ermilov printf("%-*s ", 2 * (int)sizeof(void *), "Tcpcb"); 415ac55add0SGuido van Rooij if (Lflag) 416080b7f49SDag-Erling Smørgrav printf((Aflag && !Wflag) ? 417afc74015SRuslan Ermilov "%-5.5s %-14.14s %-18.18s" : 418afc74015SRuslan Ermilov "%-5.5s %-14.14s %-22.22s", 419afc74015SRuslan Ermilov "Proto", "Listen", "Local Address"); 420afc74015SRuslan Ermilov else if (Tflag) 421afc74015SRuslan Ermilov printf((Aflag && !Wflag) ? 422afc74015SRuslan Ermilov "%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %s" : 423afc74015SRuslan Ermilov "%-5.5s %-6.6s %-6.6s %-6.6s %-22.22s %s", 424f5d34df5SGeorge V. Neville-Neil "Proto", "Rexmit", "OOORcv", "0-win", 42549f287f8SGeorge V. Neville-Neil "Local Address", "Foreign Address"); 426afc74015SRuslan Ermilov else { 427afc74015SRuslan Ermilov printf((Aflag && !Wflag) ? 428afc74015SRuslan Ermilov "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" : 429afc74015SRuslan Ermilov "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s", 430afc74015SRuslan Ermilov "Proto", "Recv-Q", "Send-Q", 431afc74015SRuslan Ermilov "Local Address", "Foreign Address"); 432afc74015SRuslan Ermilov if (!xflag) 433afc74015SRuslan Ermilov printf(" (state)"); 434afc74015SRuslan Ermilov } 435b8614722SMike Silbersack if (xflag) { 436b8614722SMike Silbersack printf(" %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s", 43794f138feSGeorge V. Neville-Neil "R-MBUF", "S-MBUF", "R-CLUS", 43894f138feSGeorge V. Neville-Neil "S-CLUS", "R-HIWA", "S-HIWA", 43994f138feSGeorge V. Neville-Neil "R-LOWA", "S-LOWA", "R-BCNT", 440b8614722SMike Silbersack "S-BCNT", "R-BMAX", "S-BMAX"); 441afc74015SRuslan Ermilov printf(" %7.7s %7.7s %7.7s %7.7s %7.7s %7.7s", 442b8614722SMike Silbersack "rexmt", "persist", "keep", 443afc74015SRuslan Ermilov "2msl", "delack", "rcvtime"); 44494f138feSGeorge V. Neville-Neil } 445afc74015SRuslan Ermilov putchar('\n'); 4469b50d902SRodney W. Grimes first = 0; 4479b50d902SRodney W. Grimes } 448fb5d0fbdSRuslan Ermilov if (Lflag && so->so_qlimit == 0) 449fb5d0fbdSRuslan Ermilov continue; 450e4ec3989SDag-Erling Smørgrav if (Aflag) { 451e4ec3989SDag-Erling Smørgrav if (istcp) 452afc74015SRuslan Ermilov printf("%*lx ", 2 * (int)sizeof(void *), (u_long)inp->inp_ppcb); 453e4ec3989SDag-Erling Smørgrav else 454afc74015SRuslan Ermilov printf("%*lx ", 2 * (int)sizeof(void *), (u_long)so->so_pcb); 455e4ec3989SDag-Erling Smørgrav } 456cfa1ca9dSYoshinobu Inoue #ifdef INET6 457fc60ab7bSYoshinobu Inoue if ((inp->inp_vflag & INP_IPV6) != 0) 4583feeb332SDavid E. O'Brien vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? 4593feeb332SDavid E. O'Brien "46" : "6 "; 460fc60ab7bSYoshinobu Inoue else 461cfa1ca9dSYoshinobu Inoue #endif 4623feeb332SDavid E. O'Brien vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? 4633feeb332SDavid E. O'Brien "4 " : " "; 46409fe6320SNavdeep Parhar if (istcp && (tp->t_flags & TF_TOE) != 0) 46509fe6320SNavdeep Parhar printf("%-3.3s%-2.2s ", "toe", vchar); 46609fe6320SNavdeep Parhar else 467fb5d0fbdSRuslan Ermilov printf("%-3.3s%-2.2s ", name, vchar); 468fb5d0fbdSRuslan Ermilov if (Lflag) { 469a01e3379SDavid Malone char buf1[15]; 470fc60ab7bSYoshinobu Inoue 471a01e3379SDavid Malone snprintf(buf1, 15, "%d/%d/%d", so->so_qlen, 472fb5d0fbdSRuslan Ermilov so->so_incqlen, so->so_qlimit); 473a01e3379SDavid Malone printf("%-14.14s ", buf1); 474f5d34df5SGeorge V. Neville-Neil } else if (Tflag) { 475f5d34df5SGeorge V. Neville-Neil if (istcp) 476f5d34df5SGeorge V. Neville-Neil printf("%6u %6u %6u ", tp->t_sndrexmitpack, 477f5d34df5SGeorge V. Neville-Neil tp->t_rcvoopack, tp->t_sndzerowin); 478fb5d0fbdSRuslan Ermilov } else { 479dd335a15SDavid E. O'Brien printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc); 480fc60ab7bSYoshinobu Inoue } 48165ea0024SAssar Westerlund if (numeric_port) { 482cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV4) { 4834f81ef50SGarrett Wollman inetprint(&inp->inp_laddr, (int)inp->inp_lport, 4848d612dd2SPoul-Henning Kamp name, 1); 485ac55add0SGuido van Rooij if (!Lflag) 486ac55add0SGuido van Rooij inetprint(&inp->inp_faddr, 487ac55add0SGuido van Rooij (int)inp->inp_fport, name, 1); 488cfa1ca9dSYoshinobu Inoue } 489cfa1ca9dSYoshinobu Inoue #ifdef INET6 490cfa1ca9dSYoshinobu Inoue else if (inp->inp_vflag & INP_IPV6) { 491cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_laddr, 492cfa1ca9dSYoshinobu Inoue (int)inp->inp_lport, name, 1); 493ac55add0SGuido van Rooij if (!Lflag) 494cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_faddr, 495cfa1ca9dSYoshinobu Inoue (int)inp->inp_fport, name, 1); 496cfa1ca9dSYoshinobu Inoue } /* else nothing printed now */ 497cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 4984f81ef50SGarrett Wollman } else if (inp->inp_flags & INP_ANONPORT) { 499cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV4) { 5004f81ef50SGarrett Wollman inetprint(&inp->inp_laddr, (int)inp->inp_lport, 5018d612dd2SPoul-Henning Kamp name, 1); 502ac55add0SGuido van Rooij if (!Lflag) 503ac55add0SGuido van Rooij inetprint(&inp->inp_faddr, 504ac55add0SGuido van Rooij (int)inp->inp_fport, name, 0); 505cfa1ca9dSYoshinobu Inoue } 506cfa1ca9dSYoshinobu Inoue #ifdef INET6 507cfa1ca9dSYoshinobu Inoue else if (inp->inp_vflag & INP_IPV6) { 508cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_laddr, 509cfa1ca9dSYoshinobu Inoue (int)inp->inp_lport, name, 1); 510ac55add0SGuido van Rooij if (!Lflag) 511cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_faddr, 512cfa1ca9dSYoshinobu Inoue (int)inp->inp_fport, name, 0); 513cfa1ca9dSYoshinobu Inoue } /* else nothing printed now */ 514cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 5158d612dd2SPoul-Henning Kamp } else { 516cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV4) { 5174f81ef50SGarrett Wollman inetprint(&inp->inp_laddr, (int)inp->inp_lport, 5188d612dd2SPoul-Henning Kamp name, 0); 519ac55add0SGuido van Rooij if (!Lflag) 520ac55add0SGuido van Rooij inetprint(&inp->inp_faddr, 521ac55add0SGuido van Rooij (int)inp->inp_fport, name, 5223feeb332SDavid E. O'Brien inp->inp_lport != inp->inp_fport); 523cfa1ca9dSYoshinobu Inoue } 524cfa1ca9dSYoshinobu Inoue #ifdef INET6 525cfa1ca9dSYoshinobu Inoue else if (inp->inp_vflag & INP_IPV6) { 526cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_laddr, 527cfa1ca9dSYoshinobu Inoue (int)inp->inp_lport, name, 0); 528ac55add0SGuido van Rooij if (!Lflag) 529cfa1ca9dSYoshinobu Inoue inet6print(&inp->in6p_faddr, 530cfa1ca9dSYoshinobu Inoue (int)inp->inp_fport, name, 5313feeb332SDavid E. O'Brien inp->inp_lport != inp->inp_fport); 532cfa1ca9dSYoshinobu Inoue } /* else nothing printed now */ 533cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 5348d612dd2SPoul-Henning Kamp } 53549f287f8SGeorge V. Neville-Neil if (xflag) { 53649f287f8SGeorge V. Neville-Neil printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u", 53749f287f8SGeorge V. Neville-Neil so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt, 53849f287f8SGeorge V. Neville-Neil so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt, 53949f287f8SGeorge V. Neville-Neil so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat, 54049f287f8SGeorge V. Neville-Neil so->so_rcv.sb_lowat, so->so_snd.sb_lowat, 54149f287f8SGeorge V. Neville-Neil so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt, 54249f287f8SGeorge V. Neville-Neil so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax); 543aae09141SMike Silbersack if (timer != NULL) 544b8614722SMike Silbersack printf(" %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d", 545b8614722SMike Silbersack timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10, 546b8614722SMike Silbersack timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10, 547b8614722SMike Silbersack timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10, 548b8614722SMike Silbersack timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10, 549b8614722SMike Silbersack timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10, 550b8614722SMike Silbersack timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10); 551b8614722SMike Silbersack } 552f5d34df5SGeorge V. Neville-Neil if (istcp && !Lflag && !xflag && !Tflag) { 5534f81ef50SGarrett Wollman if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES) 5544f81ef50SGarrett Wollman printf("%d", tp->t_state); 5559a94a597SGarrett Wollman else { 5564f81ef50SGarrett Wollman printf("%s", tcpstates[tp->t_state]); 557513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN) 5589a94a597SGarrett Wollman /* Show T/TCP `hidden state' */ 5594f81ef50SGarrett Wollman if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) 5609a94a597SGarrett Wollman putchar('*'); 561513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */ 5629a94a597SGarrett Wollman } 5639b50d902SRodney W. Grimes } 5649b50d902SRodney W. Grimes putchar('\n'); 5659b50d902SRodney W. Grimes } 5664f81ef50SGarrett Wollman if (xig != oxig && xig->xig_gen != oxig->xig_gen) { 5674f81ef50SGarrett Wollman if (oxig->xig_count > xig->xig_count) { 5684f81ef50SGarrett Wollman printf("Some %s sockets may have been deleted.\n", 5694f81ef50SGarrett Wollman name); 5704f81ef50SGarrett Wollman } else if (oxig->xig_count < xig->xig_count) { 5714f81ef50SGarrett Wollman printf("Some %s sockets may have been created.\n", 5724f81ef50SGarrett Wollman name); 5734f81ef50SGarrett Wollman } else { 5743feeb332SDavid E. O'Brien printf( 5753feeb332SDavid E. O'Brien "Some %s sockets may have been created or deleted.\n", 5764f81ef50SGarrett Wollman name); 5774f81ef50SGarrett Wollman } 5784f81ef50SGarrett Wollman } 5794f81ef50SGarrett Wollman free(buf); 5809b50d902SRodney W. Grimes } 5819b50d902SRodney W. Grimes 5829b50d902SRodney W. Grimes /* 5839b50d902SRodney W. Grimes * Dump TCP statistics structure. 5849b50d902SRodney W. Grimes */ 5859b50d902SRodney W. Grimes void 586feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 5879b50d902SRodney W. Grimes { 588c73d99b5SRuslan Ermilov struct tcpstat tcpstat, zerostat; 5894f81ef50SGarrett Wollman size_t len = sizeof tcpstat; 5909b50d902SRodney W. Grimes 591cfa1ca9dSYoshinobu Inoue #ifdef INET6 592cfa1ca9dSYoshinobu Inoue if (tcp_done != 0) 593cfa1ca9dSYoshinobu Inoue return; 594cfa1ca9dSYoshinobu Inoue else 595cfa1ca9dSYoshinobu Inoue tcp_done = 1; 596cfa1ca9dSYoshinobu Inoue #endif 597cfa1ca9dSYoshinobu Inoue 598feda1a43SJohn Baldwin if (live) { 599feda1a43SJohn Baldwin if (zflag) 600feda1a43SJohn Baldwin memset(&zerostat, 0, len); 601feda1a43SJohn Baldwin if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, 602feda1a43SJohn Baldwin zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 603feda1a43SJohn Baldwin warn("sysctl: net.inet.tcp.stats"); 604feda1a43SJohn Baldwin return; 605feda1a43SJohn Baldwin } 6065da0521fSAndrey V. Elsukov } else 6075da0521fSAndrey V. Elsukov kread_counters(off, &tcpstat, len); 608feda1a43SJohn Baldwin 6099b50d902SRodney W. Grimes printf ("%s:\n", name); 6109b50d902SRodney W. Grimes 6119b50d902SRodney W. Grimes #define p(f, m) if (tcpstat.f || sflag <= 1) \ 6125923c293SGleb Smirnoff printf(m, (uintmax_t )tcpstat.f, plural(tcpstat.f)) 6135923c293SGleb Smirnoff 61422694ebaSBruce Evans #define p1a(f, m) if (tcpstat.f || sflag <= 1) \ 6155923c293SGleb Smirnoff printf(m, (uintmax_t )tcpstat.f) 6165923c293SGleb Smirnoff 6179b50d902SRodney W. Grimes #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \ 6185923c293SGleb Smirnoff printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \ 6195923c293SGleb Smirnoff (uintmax_t )tcpstat.f2, plural(tcpstat.f2)) 6205923c293SGleb Smirnoff 62122694ebaSBruce Evans #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \ 6225923c293SGleb Smirnoff printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \ 6235923c293SGleb Smirnoff (uintmax_t )tcpstat.f2) 6245923c293SGleb Smirnoff 6259b50d902SRodney W. Grimes #define p3(f, m) if (tcpstat.f || sflag <= 1) \ 6265923c293SGleb Smirnoff printf(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f)) 6279b50d902SRodney W. Grimes 6285923c293SGleb Smirnoff p(tcps_sndtotal, "\t%ju packet%s sent\n"); 6295923c293SGleb Smirnoff p2(tcps_sndpack,tcps_sndbyte, "\t\t%ju data packet%s (%ju byte%s)\n"); 6309b50d902SRodney W. Grimes p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, 6315923c293SGleb Smirnoff "\t\t%ju data packet%s (%ju byte%s) retransmitted\n"); 632d65bf08aSMatthew Dillon p(tcps_sndrexmitbad, 6335923c293SGleb Smirnoff "\t\t%ju data packet%s unnecessarily retransmitted\n"); 6345923c293SGleb Smirnoff p(tcps_mturesent, "\t\t%ju resend%s initiated by MTU discovery\n"); 63522694ebaSBruce Evans p2a(tcps_sndacks, tcps_delack, 6365923c293SGleb Smirnoff "\t\t%ju ack-only packet%s (%ju delayed)\n"); 6375923c293SGleb Smirnoff p(tcps_sndurg, "\t\t%ju URG only packet%s\n"); 6385923c293SGleb Smirnoff p(tcps_sndprobe, "\t\t%ju window probe packet%s\n"); 6395923c293SGleb Smirnoff p(tcps_sndwinup, "\t\t%ju window update packet%s\n"); 6405923c293SGleb Smirnoff p(tcps_sndctrl, "\t\t%ju control packet%s\n"); 6415923c293SGleb Smirnoff p(tcps_rcvtotal, "\t%ju packet%s received\n"); 6423feeb332SDavid E. O'Brien p2(tcps_rcvackpack, tcps_rcvackbyte, 6435923c293SGleb Smirnoff "\t\t%ju ack%s (for %ju byte%s)\n"); 6445923c293SGleb Smirnoff p(tcps_rcvdupack, "\t\t%ju duplicate ack%s\n"); 6455923c293SGleb Smirnoff p(tcps_rcvacktoomuch, "\t\t%ju ack%s for unsent data\n"); 6469b50d902SRodney W. Grimes p2(tcps_rcvpack, tcps_rcvbyte, 6475923c293SGleb Smirnoff "\t\t%ju packet%s (%ju byte%s) received in-sequence\n"); 6489b50d902SRodney W. Grimes p2(tcps_rcvduppack, tcps_rcvdupbyte, 6495923c293SGleb Smirnoff "\t\t%ju completely duplicate packet%s (%ju byte%s)\n"); 6505923c293SGleb Smirnoff p(tcps_pawsdrop, "\t\t%ju old duplicate packet%s\n"); 6519b50d902SRodney W. Grimes p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, 6525923c293SGleb Smirnoff "\t\t%ju packet%s with some dup. data (%ju byte%s duped)\n"); 6539b50d902SRodney W. Grimes p2(tcps_rcvoopack, tcps_rcvoobyte, 6545923c293SGleb Smirnoff "\t\t%ju out-of-order packet%s (%ju byte%s)\n"); 6559b50d902SRodney W. Grimes p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, 6565923c293SGleb Smirnoff "\t\t%ju packet%s (%ju byte%s) of data after window\n"); 6575923c293SGleb Smirnoff p(tcps_rcvwinprobe, "\t\t%ju window probe%s\n"); 6585923c293SGleb Smirnoff p(tcps_rcvwinupd, "\t\t%ju window update packet%s\n"); 6595923c293SGleb Smirnoff p(tcps_rcvafterclose, "\t\t%ju packet%s received after close\n"); 6605923c293SGleb Smirnoff p(tcps_rcvbadsum, "\t\t%ju discarded for bad checksum%s\n"); 6615923c293SGleb Smirnoff p(tcps_rcvbadoff, "\t\t%ju discarded for bad header offset field%s\n"); 6625923c293SGleb Smirnoff p1a(tcps_rcvshort, "\t\t%ju discarded because packet too short\n"); 6635923c293SGleb Smirnoff p1a(tcps_rcvmemdrop, "\t\t%ju discarded due to memory problems\n"); 6645923c293SGleb Smirnoff p(tcps_connattempt, "\t%ju connection request%s\n"); 6655923c293SGleb Smirnoff p(tcps_accepts, "\t%ju connection accept%s\n"); 6665923c293SGleb Smirnoff p(tcps_badsyn, "\t%ju bad connection attempt%s\n"); 6675923c293SGleb Smirnoff p(tcps_listendrop, "\t%ju listen queue overflow%s\n"); 6685923c293SGleb Smirnoff p(tcps_badrst, "\t%ju ignored RSTs in the window%s\n"); 6695923c293SGleb Smirnoff p(tcps_connects, "\t%ju connection%s established (including accepts)\n"); 6709b50d902SRodney W. Grimes p2(tcps_closed, tcps_drops, 6715923c293SGleb Smirnoff "\t%ju connection%s closed (including %ju drop%s)\n"); 6725923c293SGleb Smirnoff p(tcps_cachedrtt, "\t\t%ju connection%s updated cached RTT on close\n"); 673861b1828SGarrett Wollman p(tcps_cachedrttvar, 6745923c293SGleb Smirnoff "\t\t%ju connection%s updated cached RTT variance on close\n"); 675861b1828SGarrett Wollman p(tcps_cachedssthresh, 6765923c293SGleb Smirnoff "\t\t%ju connection%s updated cached ssthresh on close\n"); 6775923c293SGleb Smirnoff p(tcps_conndrops, "\t%ju embryonic connection%s dropped\n"); 6789b50d902SRodney W. Grimes p2(tcps_rttupdated, tcps_segstimed, 6795923c293SGleb Smirnoff "\t%ju segment%s updated rtt (of %ju attempt%s)\n"); 6805923c293SGleb Smirnoff p(tcps_rexmttimeo, "\t%ju retransmit timeout%s\n"); 6815923c293SGleb Smirnoff p(tcps_timeoutdrop, "\t\t%ju connection%s dropped by rexmit timeout\n"); 6825923c293SGleb Smirnoff p(tcps_persisttimeo, "\t%ju persist timeout%s\n"); 6835923c293SGleb Smirnoff p(tcps_persistdrop, "\t\t%ju connection%s dropped by persist timeout\n"); 6843feeb332SDavid E. O'Brien p(tcps_finwait2_drops, 6855923c293SGleb Smirnoff "\t%ju Connection%s (fin_wait_2) dropped because of timeout\n"); 6865923c293SGleb Smirnoff p(tcps_keeptimeo, "\t%ju keepalive timeout%s\n"); 6875923c293SGleb Smirnoff p(tcps_keepprobe, "\t\t%ju keepalive probe%s sent\n"); 6885923c293SGleb Smirnoff p(tcps_keepdrops, "\t\t%ju connection%s dropped by keepalive\n"); 6895923c293SGleb Smirnoff p(tcps_predack, "\t%ju correct ACK header prediction%s\n"); 6905923c293SGleb Smirnoff p(tcps_preddat, "\t%ju correct data packet header prediction%s\n"); 69160a31b3aSJonathan Lemon 6925923c293SGleb Smirnoff p3(tcps_sc_added, "\t%ju syncache entr%s added\n"); 6935923c293SGleb Smirnoff p1a(tcps_sc_retransmitted, "\t\t%ju retransmitted\n"); 6945923c293SGleb Smirnoff p1a(tcps_sc_dupsyn, "\t\t%ju dupsyn\n"); 6955923c293SGleb Smirnoff p1a(tcps_sc_dropped, "\t\t%ju dropped\n"); 6965923c293SGleb Smirnoff p1a(tcps_sc_completed, "\t\t%ju completed\n"); 6975923c293SGleb Smirnoff p1a(tcps_sc_bucketoverflow, "\t\t%ju bucket overflow\n"); 6985923c293SGleb Smirnoff p1a(tcps_sc_cacheoverflow, "\t\t%ju cache overflow\n"); 6995923c293SGleb Smirnoff p1a(tcps_sc_reset, "\t\t%ju reset\n"); 7005923c293SGleb Smirnoff p1a(tcps_sc_stale, "\t\t%ju stale\n"); 7015923c293SGleb Smirnoff p1a(tcps_sc_aborted, "\t\t%ju aborted\n"); 7025923c293SGleb Smirnoff p1a(tcps_sc_badack, "\t\t%ju badack\n"); 7035923c293SGleb Smirnoff p1a(tcps_sc_unreach, "\t\t%ju unreach\n"); 7045923c293SGleb Smirnoff p(tcps_sc_zonefail, "\t\t%ju zone failure%s\n"); 7055923c293SGleb Smirnoff p(tcps_sc_sendcookie, "\t%ju cookie%s sent\n"); 7065923c293SGleb Smirnoff p(tcps_sc_recvcookie, "\t%ju cookie%s received\n"); 707b6101dafSPaul Saab 7085923c293SGleb Smirnoff p3(tcps_hc_added, "\t%ju hostcache entr%s added\n"); 7095923c293SGleb Smirnoff p1a(tcps_hc_bucketoverflow, "\t\t%ju bucket overflow\n"); 710699ef999SRuslan Ermilov 7115923c293SGleb Smirnoff p(tcps_sack_recovery_episode, "\t%ju SACK recovery episode%s\n"); 712b6101dafSPaul Saab p(tcps_sack_rexmits, 7135923c293SGleb Smirnoff "\t%ju segment rexmit%s in SACK recovery episodes\n"); 714b6101dafSPaul Saab p(tcps_sack_rexmit_bytes, 7155923c293SGleb Smirnoff "\t%ju byte rexmit%s in SACK recovery episodes\n"); 716b6101dafSPaul Saab p(tcps_sack_rcv_blocks, 7175923c293SGleb Smirnoff "\t%ju SACK option%s (SACK blocks) received\n"); 7185923c293SGleb Smirnoff p(tcps_sack_send_blocks, "\t%ju SACK option%s (SACK blocks) sent\n"); 7195923c293SGleb Smirnoff p1a(tcps_sack_sboverflow, "\t%ju SACK scoreboard overflow\n"); 720b6101dafSPaul Saab 7215923c293SGleb Smirnoff p(tcps_ecn_ce, "\t%ju packet%s with ECN CE bit set\n"); 7225923c293SGleb Smirnoff p(tcps_ecn_ect0, "\t%ju packet%s with ECN ECT(0) bit set\n"); 7235923c293SGleb Smirnoff p(tcps_ecn_ect1, "\t%ju packet%s with ECN ECT(1) bit set\n"); 7245923c293SGleb Smirnoff p(tcps_ecn_shs, "\t%ju successful ECN handshake%s\n"); 7255923c293SGleb Smirnoff p(tcps_ecn_rcwnd, "\t%ju time%s ECN reduced the congestion window\n"); 726*a4993b25SBjoern A. Zeeb 727*a4993b25SBjoern A. Zeeb p(tcps_sig_rcvgoodsig, 728*a4993b25SBjoern A. Zeeb "\t%ju packet%s with valid tcp-md5 signature received\n"); 729*a4993b25SBjoern A. Zeeb p(tcps_sig_rcvbadsig, 730*a4993b25SBjoern A. Zeeb "\t%ju packet%s with invalid tcp-md5 signature received\n"); 731*a4993b25SBjoern A. Zeeb p(tcps_sig_err_buildsig, 732*a4993b25SBjoern A. Zeeb "\t%ju packet%s with tcp-md5 signature mismatch\n"); 733*a4993b25SBjoern A. Zeeb p(tcps_sig_err_sigopt, 734*a4993b25SBjoern A. Zeeb "\t%ju packet%s with unexpected tcp-md5 signature received\n"); 735*a4993b25SBjoern A. Zeeb p(tcps_sig_err_nosigopt, 736*a4993b25SBjoern A. Zeeb "\t%ju packet%s without expected tcp-md5 signature received\n"); 7379b50d902SRodney W. Grimes #undef p 73822694ebaSBruce Evans #undef p1a 7399b50d902SRodney W. Grimes #undef p2 74022694ebaSBruce Evans #undef p2a 7419b50d902SRodney W. Grimes #undef p3 7429b50d902SRodney W. Grimes } 7439b50d902SRodney W. Grimes 7449b50d902SRodney W. Grimes /* 7459b50d902SRodney W. Grimes * Dump UDP statistics structure. 7469b50d902SRodney W. Grimes */ 7479b50d902SRodney W. Grimes void 748feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 7499b50d902SRodney W. Grimes { 750c73d99b5SRuslan Ermilov struct udpstat udpstat, zerostat; 7514f81ef50SGarrett Wollman size_t len = sizeof udpstat; 752c80211e3SAndrey V. Elsukov uint64_t delivered; 7539b50d902SRodney W. Grimes 754cfa1ca9dSYoshinobu Inoue #ifdef INET6 755cfa1ca9dSYoshinobu Inoue if (udp_done != 0) 756cfa1ca9dSYoshinobu Inoue return; 757cfa1ca9dSYoshinobu Inoue else 758cfa1ca9dSYoshinobu Inoue udp_done = 1; 759cfa1ca9dSYoshinobu Inoue #endif 760cfa1ca9dSYoshinobu Inoue 761feda1a43SJohn Baldwin if (live) { 762feda1a43SJohn Baldwin if (zflag) 763feda1a43SJohn Baldwin memset(&zerostat, 0, len); 764feda1a43SJohn Baldwin if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, 765feda1a43SJohn Baldwin zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 766feda1a43SJohn Baldwin warn("sysctl: net.inet.udp.stats"); 767feda1a43SJohn Baldwin return; 768feda1a43SJohn Baldwin } 769feda1a43SJohn Baldwin } else 7705b7cb97cSAndrey V. Elsukov kread_counters(off, &udpstat, len); 771feda1a43SJohn Baldwin 7729b50d902SRodney W. Grimes printf("%s:\n", name); 7739b50d902SRodney W. Grimes #define p(f, m) if (udpstat.f || sflag <= 1) \ 774c80211e3SAndrey V. Elsukov printf("\t%ju " m, (uintmax_t)udpstat.f, plural(udpstat.f)) 77522694ebaSBruce Evans #define p1a(f, m) if (udpstat.f || sflag <= 1) \ 776c80211e3SAndrey V. Elsukov printf("\t%ju " m, (uintmax_t)udpstat.f) 777c80211e3SAndrey V. Elsukov p(udps_ipackets, "datagram%s received\n"); 778c80211e3SAndrey V. Elsukov p1a(udps_hdrops, "with incomplete header\n"); 779c80211e3SAndrey V. Elsukov p1a(udps_badlen, "with bad data length field\n"); 780c80211e3SAndrey V. Elsukov p1a(udps_badsum, "with bad checksum\n"); 781c80211e3SAndrey V. Elsukov p1a(udps_nosum, "with no checksum\n"); 782c80211e3SAndrey V. Elsukov p1a(udps_noport, "dropped due to no socket\n"); 78322694ebaSBruce Evans p(udps_noportbcast, 784c80211e3SAndrey V. Elsukov "broadcast/multicast datagram%s undelivered\n"); 785c80211e3SAndrey V. Elsukov p1a(udps_fullsock, "dropped due to full socket buffers\n"); 786c80211e3SAndrey V. Elsukov p1a(udpps_pcbhashmiss, "not for hashed pcb\n"); 7879b50d902SRodney W. Grimes delivered = udpstat.udps_ipackets - 7889b50d902SRodney W. Grimes udpstat.udps_hdrops - 7899b50d902SRodney W. Grimes udpstat.udps_badlen - 7909b50d902SRodney W. Grimes udpstat.udps_badsum - 7919b50d902SRodney W. Grimes udpstat.udps_noport - 7929b50d902SRodney W. Grimes udpstat.udps_noportbcast - 7939b50d902SRodney W. Grimes udpstat.udps_fullsock; 7949b50d902SRodney W. Grimes if (delivered || sflag <= 1) 795c80211e3SAndrey V. Elsukov printf("\t%ju delivered\n", (uint64_t)delivered); 796c80211e3SAndrey V. Elsukov p(udps_opackets, "datagram%s output\n"); 79771498f30SBruce M Simpson /* the next statistic is cumulative in udps_noportbcast */ 79871498f30SBruce M Simpson p(udps_filtermcast, 799c80211e3SAndrey V. Elsukov "time%s multicast source filter matched\n"); 8009b50d902SRodney W. Grimes #undef p 80122694ebaSBruce Evans #undef p1a 8029b50d902SRodney W. Grimes } 8039b50d902SRodney W. Grimes 8049b50d902SRodney W. Grimes /* 805a9771948SGleb Smirnoff * Dump CARP statistics structure. 806a9771948SGleb Smirnoff */ 807a9771948SGleb Smirnoff void 808feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 809a9771948SGleb Smirnoff { 810a9771948SGleb Smirnoff struct carpstats carpstat, zerostat; 811a9771948SGleb Smirnoff size_t len = sizeof(struct carpstats); 812a9771948SGleb Smirnoff 813feda1a43SJohn Baldwin if (live) { 814a9771948SGleb Smirnoff if (zflag) 815a9771948SGleb Smirnoff memset(&zerostat, 0, len); 816a9771948SGleb Smirnoff if (sysctlbyname("net.inet.carp.stats", &carpstat, &len, 817a9771948SGleb Smirnoff zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 81845125e14SRuslan Ermilov if (errno != ENOENT) 819a9771948SGleb Smirnoff warn("sysctl: net.inet.carp.stats"); 820a9771948SGleb Smirnoff return; 821a9771948SGleb Smirnoff } 822feda1a43SJohn Baldwin } else { 823feda1a43SJohn Baldwin if (off == 0) 824feda1a43SJohn Baldwin return; 82569edf037SAndrey V. Elsukov kread_counters(off, &carpstat, len); 826feda1a43SJohn Baldwin } 827a9771948SGleb Smirnoff 828a9771948SGleb Smirnoff printf("%s:\n", name); 829a9771948SGleb Smirnoff 830a9771948SGleb Smirnoff #define p(f, m) if (carpstat.f || sflag <= 1) \ 8317b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)carpstat.f, plural(carpstat.f)) 832a9771948SGleb Smirnoff #define p2(f, m) if (carpstat.f || sflag <= 1) \ 8337b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)carpstat.f) 834a9771948SGleb Smirnoff 8357b95a1ebSYaroslav Tykhiy p(carps_ipackets, "\t%ju packet%s received (IPv4)\n"); 8367b95a1ebSYaroslav Tykhiy p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n"); 8377b95a1ebSYaroslav Tykhiy p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n"); 8387b95a1ebSYaroslav Tykhiy p(carps_hdrops, "\t\t%ju packet%s shorter than header\n"); 8397b95a1ebSYaroslav Tykhiy p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n"); 8407b95a1ebSYaroslav Tykhiy p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n"); 8417b95a1ebSYaroslav Tykhiy p2(carps_badlen, "\t\t%ju discarded because packet too short\n"); 8427b95a1ebSYaroslav Tykhiy p2(carps_badauth, "\t\t%ju discarded for bad authentication\n"); 8437b95a1ebSYaroslav Tykhiy p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n"); 8447b95a1ebSYaroslav Tykhiy p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n"); 8457b95a1ebSYaroslav Tykhiy p(carps_opackets, "\t%ju packet%s sent (IPv4)\n"); 8467b95a1ebSYaroslav Tykhiy p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n"); 8477b95a1ebSYaroslav Tykhiy p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n"); 848a9771948SGleb Smirnoff #if notyet 849a9771948SGleb Smirnoff p(carps_ostates, "\t\t%s state update%s sent\n"); 850a9771948SGleb Smirnoff #endif 851a9771948SGleb Smirnoff #undef p 852a9771948SGleb Smirnoff #undef p2 853a9771948SGleb Smirnoff } 854a9771948SGleb Smirnoff 855a9771948SGleb Smirnoff /* 8569b50d902SRodney W. Grimes * Dump IP statistics structure. 8579b50d902SRodney W. Grimes */ 8589b50d902SRodney W. Grimes void 859feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 8609b50d902SRodney W. Grimes { 861c73d99b5SRuslan Ermilov struct ipstat ipstat, zerostat; 8624f81ef50SGarrett Wollman size_t len = sizeof ipstat; 8639b50d902SRodney W. Grimes 864feda1a43SJohn Baldwin if (live) { 865c73d99b5SRuslan Ermilov if (zflag) 866c73d99b5SRuslan Ermilov memset(&zerostat, 0, len); 867c73d99b5SRuslan Ermilov if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, 868c73d99b5SRuslan Ermilov zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 8694f81ef50SGarrett Wollman warn("sysctl: net.inet.ip.stats"); 8709b50d902SRodney W. Grimes return; 8714f81ef50SGarrett Wollman } 8725da0521fSAndrey V. Elsukov } else 8735da0521fSAndrey V. Elsukov kread_counters(off, &ipstat, len); 8744f81ef50SGarrett Wollman 8759b50d902SRodney W. Grimes printf("%s:\n", name); 8769b50d902SRodney W. Grimes 8779b50d902SRodney W. Grimes #define p(f, m) if (ipstat.f || sflag <= 1) \ 8785923c293SGleb Smirnoff printf(m, (uintmax_t )ipstat.f, plural(ipstat.f)) 87922694ebaSBruce Evans #define p1a(f, m) if (ipstat.f || sflag <= 1) \ 8805923c293SGleb Smirnoff printf(m, (uintmax_t )ipstat.f) 8819b50d902SRodney W. Grimes 8825923c293SGleb Smirnoff p(ips_total, "\t%ju total packet%s received\n"); 8835923c293SGleb Smirnoff p(ips_badsum, "\t%ju bad header checksum%s\n"); 8845923c293SGleb Smirnoff p1a(ips_toosmall, "\t%ju with size smaller than minimum\n"); 8855923c293SGleb Smirnoff p1a(ips_tooshort, "\t%ju with data size < data length\n"); 8865923c293SGleb Smirnoff p1a(ips_toolong, "\t%ju with ip length > max ip packet size\n"); 8875923c293SGleb Smirnoff p1a(ips_badhlen, "\t%ju with header length < data size\n"); 8885923c293SGleb Smirnoff p1a(ips_badlen, "\t%ju with data length < header length\n"); 8895923c293SGleb Smirnoff p1a(ips_badoptions, "\t%ju with bad options\n"); 8905923c293SGleb Smirnoff p1a(ips_badvers, "\t%ju with incorrect version number\n"); 8915923c293SGleb Smirnoff p(ips_fragments, "\t%ju fragment%s received\n"); 8925923c293SGleb Smirnoff p(ips_fragdropped, "\t%ju fragment%s dropped (dup or out of space)\n"); 8935923c293SGleb Smirnoff p(ips_fragtimeout, "\t%ju fragment%s dropped after timeout\n"); 8945923c293SGleb Smirnoff p(ips_reassembled, "\t%ju packet%s reassembled ok\n"); 8955923c293SGleb Smirnoff p(ips_delivered, "\t%ju packet%s for this host\n"); 8965923c293SGleb Smirnoff p(ips_noproto, "\t%ju packet%s for unknown/unsupported protocol\n"); 8975923c293SGleb Smirnoff p(ips_forward, "\t%ju packet%s forwarded"); 8985923c293SGleb Smirnoff p(ips_fastforward, " (%ju packet%s fast forwarded)"); 899958d6f7fSPierre Beyssac if (ipstat.ips_forward || sflag <= 1) 900958d6f7fSPierre Beyssac putchar('\n'); 9015923c293SGleb Smirnoff p(ips_cantforward, "\t%ju packet%s not forwardable\n"); 902a5969f5fSGarrett Wollman p(ips_notmember, 9035923c293SGleb Smirnoff "\t%ju packet%s received for unknown multicast group\n"); 9045923c293SGleb Smirnoff p(ips_redirectsent, "\t%ju redirect%s sent\n"); 9055923c293SGleb Smirnoff p(ips_localout, "\t%ju packet%s sent from this host\n"); 9065923c293SGleb Smirnoff p(ips_rawout, "\t%ju packet%s sent with fabricated ip header\n"); 907a5969f5fSGarrett Wollman p(ips_odropped, 9085923c293SGleb Smirnoff "\t%ju output packet%s dropped due to no bufs, etc.\n"); 9095923c293SGleb Smirnoff p(ips_noroute, "\t%ju output packet%s discarded due to no route\n"); 9105923c293SGleb Smirnoff p(ips_fragmented, "\t%ju output datagram%s fragmented\n"); 9115923c293SGleb Smirnoff p(ips_ofragments, "\t%ju fragment%s created\n"); 9125923c293SGleb Smirnoff p(ips_cantfrag, "\t%ju datagram%s that can't be fragmented\n"); 9135923c293SGleb Smirnoff p(ips_nogif, "\t%ju tunneling packet%s that can't find gif\n"); 9145923c293SGleb Smirnoff p(ips_badaddr, "\t%ju datagram%s with bad address in header\n"); 9159b50d902SRodney W. Grimes #undef p 91622694ebaSBruce Evans #undef p1a 9179b50d902SRodney W. Grimes } 9189b50d902SRodney W. Grimes 91954fc657dSGeorge V. Neville-Neil /* 92054fc657dSGeorge V. Neville-Neil * Dump ARP statistics structure. 92154fc657dSGeorge V. Neville-Neil */ 92254fc657dSGeorge V. Neville-Neil void 92354fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 92454fc657dSGeorge V. Neville-Neil { 92554fc657dSGeorge V. Neville-Neil struct arpstat arpstat, zerostat; 92654fc657dSGeorge V. Neville-Neil size_t len = sizeof(arpstat); 92754fc657dSGeorge V. Neville-Neil 92854fc657dSGeorge V. Neville-Neil if (live) { 92954fc657dSGeorge V. Neville-Neil if (zflag) 93054fc657dSGeorge V. Neville-Neil memset(&zerostat, 0, len); 93154fc657dSGeorge V. Neville-Neil if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 93254fc657dSGeorge V. Neville-Neil zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 93354fc657dSGeorge V. Neville-Neil warn("sysctl: net.link.ether.arp.stats"); 93454fc657dSGeorge V. Neville-Neil return; 93554fc657dSGeorge V. Neville-Neil } 93654fc657dSGeorge V. Neville-Neil } else 9375b7cb97cSAndrey V. Elsukov kread_counters(off, &arpstat, len); 93854fc657dSGeorge V. Neville-Neil 93954fc657dSGeorge V. Neville-Neil printf("%s:\n", name); 94054fc657dSGeorge V. Neville-Neil 94154fc657dSGeorge V. Neville-Neil #define p(f, m) if (arpstat.f || sflag <= 1) \ 942c80211e3SAndrey V. Elsukov printf("\t%ju " m, (uintmax_t)arpstat.f, plural(arpstat.f)) 94354fc657dSGeorge V. Neville-Neil #define p2(f, m) if (arpstat.f || sflag <= 1) \ 944c80211e3SAndrey V. Elsukov printf("\t%ju " m, (uintmax_t)arpstat.f, pluralies(arpstat.f)) 94554fc657dSGeorge V. Neville-Neil 946c80211e3SAndrey V. Elsukov p(txrequests, "ARP request%s sent\n"); 947c80211e3SAndrey V. Elsukov p2(txreplies, "ARP repl%s sent\n"); 948c80211e3SAndrey V. Elsukov p(rxrequests, "ARP request%s received\n"); 949c80211e3SAndrey V. Elsukov p2(rxreplies, "ARP repl%s received\n"); 950c80211e3SAndrey V. Elsukov p(received, "ARP packet%s received\n"); 951c80211e3SAndrey V. Elsukov p(dropped, "total packet%s dropped due to no ARP entry\n"); 952c80211e3SAndrey V. Elsukov p(timeouts, "ARP entry%s timed out\n"); 953c80211e3SAndrey V. Elsukov p(dupips, "Duplicate IP%s seen\n"); 95454fc657dSGeorge V. Neville-Neil #undef p 95554fc657dSGeorge V. Neville-Neil #undef p2 95654fc657dSGeorge V. Neville-Neil } 95754fc657dSGeorge V. Neville-Neil 95854fc657dSGeorge V. Neville-Neil 95954fc657dSGeorge V. Neville-Neil 9604063583aSMaxim Konovalov static const char *icmpnames[ICMP_MAXTYPE + 1] = { 9614063583aSMaxim Konovalov "echo reply", /* RFC 792 */ 9629b50d902SRodney W. Grimes "#1", 9639b50d902SRodney W. Grimes "#2", 9644063583aSMaxim Konovalov "destination unreachable", /* RFC 792 */ 9654063583aSMaxim Konovalov "source quench", /* RFC 792 */ 9664063583aSMaxim Konovalov "routing redirect", /* RFC 792 */ 9679b50d902SRodney W. Grimes "#6", 9689b50d902SRodney W. Grimes "#7", 9694063583aSMaxim Konovalov "echo", /* RFC 792 */ 9704063583aSMaxim Konovalov "router advertisement", /* RFC 1256 */ 9714063583aSMaxim Konovalov "router solicitation", /* RFC 1256 */ 9724063583aSMaxim Konovalov "time exceeded", /* RFC 792 */ 9734063583aSMaxim Konovalov "parameter problem", /* RFC 792 */ 9744063583aSMaxim Konovalov "time stamp", /* RFC 792 */ 9754063583aSMaxim Konovalov "time stamp reply", /* RFC 792 */ 9764063583aSMaxim Konovalov "information request", /* RFC 792 */ 9774063583aSMaxim Konovalov "information request reply", /* RFC 792 */ 9784063583aSMaxim Konovalov "address mask request", /* RFC 950 */ 9794063583aSMaxim Konovalov "address mask reply", /* RFC 950 */ 9804063583aSMaxim Konovalov "#19", 9814063583aSMaxim Konovalov "#20", 9824063583aSMaxim Konovalov "#21", 9834063583aSMaxim Konovalov "#22", 9844063583aSMaxim Konovalov "#23", 9854063583aSMaxim Konovalov "#24", 9864063583aSMaxim Konovalov "#25", 9874063583aSMaxim Konovalov "#26", 9884063583aSMaxim Konovalov "#27", 9894063583aSMaxim Konovalov "#28", 9904063583aSMaxim Konovalov "#29", 9914063583aSMaxim Konovalov "icmp traceroute", /* RFC 1393 */ 9924063583aSMaxim Konovalov "datagram conversion error", /* RFC 1475 */ 9934063583aSMaxim Konovalov "mobile host redirect", 9944063583aSMaxim Konovalov "IPv6 where-are-you", 9954063583aSMaxim Konovalov "IPv6 i-am-here", 9964063583aSMaxim Konovalov "mobile registration req", 9974063583aSMaxim Konovalov "mobile registration reply", 9984063583aSMaxim Konovalov "domain name request", /* RFC 1788 */ 9994063583aSMaxim Konovalov "domain name reply", /* RFC 1788 */ 10004063583aSMaxim Konovalov "icmp SKIP", 10014063583aSMaxim Konovalov "icmp photuris", /* RFC 2521 */ 10029b50d902SRodney W. Grimes }; 10039b50d902SRodney W. Grimes 10049b50d902SRodney W. Grimes /* 10059b50d902SRodney W. Grimes * Dump ICMP statistics. 10069b50d902SRodney W. Grimes */ 10079b50d902SRodney W. Grimes void 1008feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 10099b50d902SRodney W. Grimes { 1010c73d99b5SRuslan Ermilov struct icmpstat icmpstat, zerostat; 10114e00c309SGarrett Wollman int i, first; 10124e00c309SGarrett Wollman size_t len; 10139b50d902SRodney W. Grimes 10144e00c309SGarrett Wollman len = sizeof icmpstat; 1015feda1a43SJohn Baldwin if (live) { 1016c73d99b5SRuslan Ermilov if (zflag) 1017c73d99b5SRuslan Ermilov memset(&zerostat, 0, len); 1018feda1a43SJohn Baldwin if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, 1019c73d99b5SRuslan Ermilov zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1020c73d99b5SRuslan Ermilov warn("sysctl: net.inet.icmp.stats"); 1021c73d99b5SRuslan Ermilov return; 1022c73d99b5SRuslan Ermilov } 1023feda1a43SJohn Baldwin } else 10245b7cb97cSAndrey V. Elsukov kread_counters(off, &icmpstat, len); 10254e00c309SGarrett Wollman 10269b50d902SRodney W. Grimes printf("%s:\n", name); 10279b50d902SRodney W. Grimes 10289b50d902SRodney W. Grimes #define p(f, m) if (icmpstat.f || sflag <= 1) \ 10299b50d902SRodney W. Grimes printf(m, icmpstat.f, plural(icmpstat.f)) 103022694ebaSBruce Evans #define p1a(f, m) if (icmpstat.f || sflag <= 1) \ 103122694ebaSBruce Evans printf(m, icmpstat.f) 1032bd714208SRuslan Ermilov #define p2(f, m) if (icmpstat.f || sflag <= 1) \ 1033bd714208SRuslan Ermilov printf(m, icmpstat.f, plurales(icmpstat.f)) 10349b50d902SRodney W. Grimes 10357d56c0eeSAlexander Langer p(icps_error, "\t%lu call%s to icmp_error\n"); 10369b50d902SRodney W. Grimes p(icps_oldicmp, 1037f99a4046SMike Makonnen "\t%lu error%s not generated in response to an icmp message\n"); 10389b50d902SRodney W. Grimes for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 10399b50d902SRodney W. Grimes if (icmpstat.icps_outhist[i] != 0) { 10409b50d902SRodney W. Grimes if (first) { 10419b50d902SRodney W. Grimes printf("\tOutput histogram:\n"); 10429b50d902SRodney W. Grimes first = 0; 10439b50d902SRodney W. Grimes } 10444063583aSMaxim Konovalov if (icmpnames[i] != NULL) 10457d56c0eeSAlexander Langer printf("\t\t%s: %lu\n", icmpnames[i], 10469b50d902SRodney W. Grimes icmpstat.icps_outhist[i]); 10474063583aSMaxim Konovalov else 10484063583aSMaxim Konovalov printf("\t\tunknown ICMP #%d: %lu\n", i, 10494063583aSMaxim Konovalov icmpstat.icps_outhist[i]); 10509b50d902SRodney W. Grimes } 10517d56c0eeSAlexander Langer p(icps_badcode, "\t%lu message%s with bad code fields\n"); 1052bc215f59SDavid E. O'Brien p(icps_tooshort, "\t%lu message%s less than the minimum length\n"); 1053bc215f59SDavid E. O'Brien p(icps_checksum, "\t%lu message%s with bad checksum\n"); 10547d56c0eeSAlexander Langer p(icps_badlen, "\t%lu message%s with bad length\n"); 105522694ebaSBruce Evans p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n"); 105622694ebaSBruce Evans p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n"); 10579b50d902SRodney W. Grimes for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 10589b50d902SRodney W. Grimes if (icmpstat.icps_inhist[i] != 0) { 10599b50d902SRodney W. Grimes if (first) { 10609b50d902SRodney W. Grimes printf("\tInput histogram:\n"); 10619b50d902SRodney W. Grimes first = 0; 10629b50d902SRodney W. Grimes } 10634063583aSMaxim Konovalov if (icmpnames[i] != NULL) 10647d56c0eeSAlexander Langer printf("\t\t%s: %lu\n", icmpnames[i], 10659b50d902SRodney W. Grimes icmpstat.icps_inhist[i]); 10664063583aSMaxim Konovalov else 10674063583aSMaxim Konovalov printf("\t\tunknown ICMP #%d: %lu\n", i, 10684063583aSMaxim Konovalov icmpstat.icps_inhist[i]); 10699b50d902SRodney W. Grimes } 10707d56c0eeSAlexander Langer p(icps_reflect, "\t%lu message response%s generated\n"); 1071bd714208SRuslan Ermilov p2(icps_badaddr, "\t%lu invalid return address%s\n"); 10720237ca7bSRuslan Ermilov p(icps_noroute, "\t%lu no return route%s\n"); 10739b50d902SRodney W. Grimes #undef p 107422694ebaSBruce Evans #undef p1a 1075bd714208SRuslan Ermilov #undef p2 1076feda1a43SJohn Baldwin if (live) { 10774e00c309SGarrett Wollman len = sizeof i; 1078feda1a43SJohn Baldwin if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) < 1079feda1a43SJohn Baldwin 0) 10804e00c309SGarrett Wollman return; 10814e00c309SGarrett Wollman printf("\tICMP address mask responses are %sabled\n", 10824e00c309SGarrett Wollman i ? "en" : "dis"); 10839b50d902SRodney W. Grimes } 1084feda1a43SJohn Baldwin } 10859b50d902SRodney W. Grimes 1086d10910e6SBruce M Simpson #ifndef BURN_BRIDGES 10879b50d902SRodney W. Grimes /* 1088d10910e6SBruce M Simpson * Dump IGMP statistics structure (pre 8.x kernel). 10899b50d902SRodney W. Grimes */ 1090d10910e6SBruce M Simpson static void 109196c3073fSXin LI igmp_stats_live_old(const char *name) 10929b50d902SRodney W. Grimes { 1093d10910e6SBruce M Simpson struct oigmpstat oigmpstat, zerostat; 1094d10910e6SBruce M Simpson size_t len = sizeof(oigmpstat); 10959b50d902SRodney W. Grimes 1096c73d99b5SRuslan Ermilov if (zflag) 1097c73d99b5SRuslan Ermilov memset(&zerostat, 0, len); 1098d10910e6SBruce M Simpson if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len, 1099c73d99b5SRuslan Ermilov zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 11004f81ef50SGarrett Wollman warn("sysctl: net.inet.igmp.stats"); 11019b50d902SRodney W. Grimes return; 11024f81ef50SGarrett Wollman } 11034f81ef50SGarrett Wollman 11049b50d902SRodney W. Grimes printf("%s:\n", name); 11059b50d902SRodney W. Grimes 1106d10910e6SBruce M Simpson #define p(f, m) if (oigmpstat.f || sflag <= 1) \ 1107d10910e6SBruce M Simpson printf(m, oigmpstat.f, plural(oigmpstat.f)) 1108d10910e6SBruce M Simpson #define py(f, m) if (oigmpstat.f || sflag <= 1) \ 1109d10910e6SBruce M Simpson printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y") 11109b50d902SRodney W. Grimes p(igps_rcv_total, "\t%u message%s received\n"); 11119b50d902SRodney W. Grimes p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n"); 11129b50d902SRodney W. Grimes p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n"); 11139b50d902SRodney W. Grimes py(igps_rcv_queries, "\t%u membership quer%s received\n"); 11143feeb332SDavid E. O'Brien py(igps_rcv_badqueries, 11153feeb332SDavid E. O'Brien "\t%u membership quer%s received with invalid field(s)\n"); 11169b50d902SRodney W. Grimes p(igps_rcv_reports, "\t%u membership report%s received\n"); 11173feeb332SDavid E. O'Brien p(igps_rcv_badreports, 11183feeb332SDavid E. O'Brien "\t%u membership report%s received with invalid field(s)\n"); 11193feeb332SDavid E. O'Brien p(igps_rcv_ourreports, 11203feeb332SDavid E. O'Brien "\t%u membership report%s received for groups to which we belong\n"); 11219b50d902SRodney W. Grimes p(igps_snd_reports, "\t%u membership report%s sent\n"); 11229b50d902SRodney W. Grimes #undef p 11239b50d902SRodney W. Grimes #undef py 11249b50d902SRodney W. Grimes } 1125d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */ 1126d10910e6SBruce M Simpson 1127d10910e6SBruce M Simpson /* 1128d10910e6SBruce M Simpson * Dump IGMP statistics structure. 1129d10910e6SBruce M Simpson */ 1130d10910e6SBruce M Simpson void 1131d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 1132d10910e6SBruce M Simpson { 1133d10910e6SBruce M Simpson struct igmpstat igmpstat, zerostat; 1134d10910e6SBruce M Simpson size_t len; 1135d10910e6SBruce M Simpson 1136d10910e6SBruce M Simpson #ifndef BURN_BRIDGES 1137d10910e6SBruce M Simpson if (live) { 1138d10910e6SBruce M Simpson /* 1139d10910e6SBruce M Simpson * Detect if we are being run against a pre-IGMPv3 kernel. 1140d10910e6SBruce M Simpson * We cannot do this for a core file as the legacy 1141d10910e6SBruce M Simpson * struct igmpstat has no size field, nor does it 1142d10910e6SBruce M Simpson * export it in any readily-available symbols. 1143d10910e6SBruce M Simpson */ 1144d10910e6SBruce M Simpson len = 0; 1145d10910e6SBruce M Simpson if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL, 1146d10910e6SBruce M Simpson 0) < 0) { 1147d10910e6SBruce M Simpson warn("sysctl: net.inet.igmp.stats"); 1148d10910e6SBruce M Simpson return; 1149d10910e6SBruce M Simpson } 1150d10910e6SBruce M Simpson if (len < sizeof(igmpstat)) { 115196c3073fSXin LI igmp_stats_live_old(name); 1152d10910e6SBruce M Simpson return; 1153d10910e6SBruce M Simpson } 1154d10910e6SBruce M Simpson } 1155d10910e6SBruce M Simpson #endif /* !BURN_BRIDGES */ 1156d10910e6SBruce M Simpson 1157d10910e6SBruce M Simpson len = sizeof(igmpstat); 1158d10910e6SBruce M Simpson if (live) { 1159d10910e6SBruce M Simpson if (zflag) 1160d10910e6SBruce M Simpson memset(&zerostat, 0, len); 1161d10910e6SBruce M Simpson if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, 1162d10910e6SBruce M Simpson zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1163d10910e6SBruce M Simpson warn("sysctl: net.inet.igmp.stats"); 1164d10910e6SBruce M Simpson return; 1165d10910e6SBruce M Simpson } 1166d10910e6SBruce M Simpson } else { 1167d10910e6SBruce M Simpson len = sizeof(igmpstat); 1168d10910e6SBruce M Simpson kread(off, &igmpstat, len); 1169d10910e6SBruce M Simpson } 1170d10910e6SBruce M Simpson 1171d10910e6SBruce M Simpson if (igmpstat.igps_version != IGPS_VERSION_3) { 1172d10910e6SBruce M Simpson warnx("%s: version mismatch (%d != %d)", __func__, 1173d10910e6SBruce M Simpson igmpstat.igps_version, IGPS_VERSION_3); 1174d10910e6SBruce M Simpson } 1175d10910e6SBruce M Simpson if (igmpstat.igps_len != IGPS_VERSION3_LEN) { 1176d10910e6SBruce M Simpson warnx("%s: size mismatch (%d != %d)", __func__, 1177d10910e6SBruce M Simpson igmpstat.igps_len, IGPS_VERSION3_LEN); 1178d10910e6SBruce M Simpson } 1179d10910e6SBruce M Simpson 1180d10910e6SBruce M Simpson printf("%s:\n", name); 1181d10910e6SBruce M Simpson 1182d10910e6SBruce M Simpson #define p64(f, m) if (igmpstat.f || sflag <= 1) \ 1183d10910e6SBruce M Simpson printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f)) 1184d10910e6SBruce M Simpson #define py64(f, m) if (igmpstat.f || sflag <= 1) \ 1185d10910e6SBruce M Simpson printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f)) 1186d10910e6SBruce M Simpson p64(igps_rcv_total, "\t%ju message%s received\n"); 1187d10910e6SBruce M Simpson p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n"); 1188d10910e6SBruce M Simpson p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n"); 1189d10910e6SBruce M Simpson p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n"); 1190d10910e6SBruce M Simpson py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n"); 1191d10910e6SBruce M Simpson py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n"); 1192d10910e6SBruce M Simpson py64(igps_rcv_badqueries, 1193d10910e6SBruce M Simpson "\t%ju membership quer%s received with invalid field(s)\n"); 1194d10910e6SBruce M Simpson py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n"); 1195d10910e6SBruce M Simpson py64(igps_rcv_group_queries, "\t%ju group quer%s received\n"); 1196d10910e6SBruce M Simpson py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n"); 1197d10910e6SBruce M Simpson py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n"); 1198d10910e6SBruce M Simpson p64(igps_rcv_reports, "\t%ju membership report%s received\n"); 1199d10910e6SBruce M Simpson p64(igps_rcv_badreports, 1200d10910e6SBruce M Simpson "\t%ju membership report%s received with invalid field(s)\n"); 1201d10910e6SBruce M Simpson p64(igps_rcv_ourreports, 1202d10910e6SBruce M Simpson "\t%ju membership report%s received for groups to which we belong\n"); 1203d10910e6SBruce M Simpson p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n"); 1204d10910e6SBruce M Simpson p64(igps_snd_reports, "\t%ju membership report%s sent\n"); 1205d10910e6SBruce M Simpson #undef p64 1206d10910e6SBruce M Simpson #undef py64 1207d10910e6SBruce M Simpson } 12089b50d902SRodney W. Grimes 12099b50d902SRodney W. Grimes /* 1210c7b9b5bbSJeffrey Hsu * Dump PIM statistics structure. 1211c7b9b5bbSJeffrey Hsu */ 1212c7b9b5bbSJeffrey Hsu void 1213feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused, 1214feda1a43SJohn Baldwin int proto __unused) 1215c7b9b5bbSJeffrey Hsu { 1216c7b9b5bbSJeffrey Hsu struct pimstat pimstat, zerostat; 1217c7b9b5bbSJeffrey Hsu size_t len = sizeof pimstat; 1218c7b9b5bbSJeffrey Hsu 1219feda1a43SJohn Baldwin if (live) { 1220c7b9b5bbSJeffrey Hsu if (zflag) 1221c7b9b5bbSJeffrey Hsu memset(&zerostat, 0, len); 1222c7b9b5bbSJeffrey Hsu if (sysctlbyname("net.inet.pim.stats", &pimstat, &len, 1223c7b9b5bbSJeffrey Hsu zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 12246d7c0d2fSDag-Erling Smørgrav if (errno != ENOENT) 1225c7b9b5bbSJeffrey Hsu warn("sysctl: net.inet.pim.stats"); 1226c7b9b5bbSJeffrey Hsu return; 1227c7b9b5bbSJeffrey Hsu } 1228feda1a43SJohn Baldwin } else { 1229feda1a43SJohn Baldwin if (off == 0) 1230feda1a43SJohn Baldwin return; 12315b7cb97cSAndrey V. Elsukov kread_counters(off, &pimstat, len); 1232feda1a43SJohn Baldwin } 1233c7b9b5bbSJeffrey Hsu 1234c7b9b5bbSJeffrey Hsu printf("%s:\n", name); 1235c7b9b5bbSJeffrey Hsu 1236c7b9b5bbSJeffrey Hsu #define p(f, m) if (pimstat.f || sflag <= 1) \ 12377b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)pimstat.f, plural(pimstat.f)) 1238c7b9b5bbSJeffrey Hsu #define py(f, m) if (pimstat.f || sflag <= 1) \ 12397b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y") 12407b95a1ebSYaroslav Tykhiy p(pims_rcv_total_msgs, "\t%ju message%s received\n"); 12417b95a1ebSYaroslav Tykhiy p(pims_rcv_total_bytes, "\t%ju byte%s received\n"); 12427b95a1ebSYaroslav Tykhiy p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n"); 12437b95a1ebSYaroslav Tykhiy p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n"); 12447b95a1ebSYaroslav Tykhiy p(pims_rcv_badversion, "\t%ju message%s received with bad version\n"); 12457b95a1ebSYaroslav Tykhiy p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n"); 12467b95a1ebSYaroslav Tykhiy p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n"); 12473feeb332SDavid E. O'Brien p(pims_rcv_registers_wrongiif, 12483feeb332SDavid E. O'Brien "\t%ju data register message%s received on wrong iif\n"); 12497b95a1ebSYaroslav Tykhiy p(pims_rcv_badregisters, "\t%ju bad register%s received\n"); 12507b95a1ebSYaroslav Tykhiy p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n"); 12517b95a1ebSYaroslav Tykhiy p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n"); 1252c7b9b5bbSJeffrey Hsu #undef p 1253c7b9b5bbSJeffrey Hsu #undef py 1254c7b9b5bbSJeffrey Hsu } 1255c7b9b5bbSJeffrey Hsu 1256c7b9b5bbSJeffrey Hsu /* 12579b50d902SRodney W. Grimes * Pretty print an Internet address (net address + port). 12589b50d902SRodney W. Grimes */ 12599b50d902SRodney W. Grimes void 1260a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port) 12619b50d902SRodney W. Grimes { 12629b50d902SRodney W. Grimes struct servent *sp = 0; 12639b50d902SRodney W. Grimes char line[80], *cp; 1264cfa1ca9dSYoshinobu Inoue int width; 12659b50d902SRodney W. Grimes 1266080b7f49SDag-Erling Smørgrav if (Wflag) 1267080b7f49SDag-Erling Smørgrav sprintf(line, "%s.", inetname(in)); 1268080b7f49SDag-Erling Smørgrav else 1269a01e3379SDavid Malone sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in)); 1270b3608ae1SEd Schouten cp = strchr(line, '\0'); 1271a01e3379SDavid Malone if (!num_port && port) 12729b50d902SRodney W. Grimes sp = getservbyport((int)port, proto); 12739b50d902SRodney W. Grimes if (sp || port == 0) 12741ef69972SAndrey A. Chernov sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); 12759b50d902SRodney W. Grimes else 12769b50d902SRodney W. Grimes sprintf(cp, "%d ", ntohs((u_short)port)); 1277080b7f49SDag-Erling Smørgrav width = (Aflag && !Wflag) ? 18 : 22; 1278080b7f49SDag-Erling Smørgrav if (Wflag) 1279080b7f49SDag-Erling Smørgrav printf("%-*s ", width, line); 1280080b7f49SDag-Erling Smørgrav else 1281cfa1ca9dSYoshinobu Inoue printf("%-*.*s ", width, width, line); 12829b50d902SRodney W. Grimes } 12839b50d902SRodney W. Grimes 12849b50d902SRodney W. Grimes /* 12859b50d902SRodney W. Grimes * Construct an Internet address representation. 128665ea0024SAssar Westerlund * If numeric_addr has been supplied, give 12879b50d902SRodney W. Grimes * numeric value, otherwise try for symbolic name. 12889b50d902SRodney W. Grimes */ 12899b50d902SRodney W. Grimes char * 12905e051718SAssar Westerlund inetname(struct in_addr *inp) 12919b50d902SRodney W. Grimes { 1292a01e3379SDavid Malone char *cp; 1293d121b556SBrian Somers static char line[MAXHOSTNAMELEN]; 12949b50d902SRodney W. Grimes struct hostent *hp; 12959b50d902SRodney W. Grimes struct netent *np; 12969b50d902SRodney W. Grimes 12979b50d902SRodney W. Grimes cp = 0; 129865ea0024SAssar Westerlund if (!numeric_addr && inp->s_addr != INADDR_ANY) { 12999b50d902SRodney W. Grimes int net = inet_netof(*inp); 13009b50d902SRodney W. Grimes int lna = inet_lnaof(*inp); 13019b50d902SRodney W. Grimes 13029b50d902SRodney W. Grimes if (lna == INADDR_ANY) { 13039b50d902SRodney W. Grimes np = getnetbyaddr(net, AF_INET); 13049b50d902SRodney W. Grimes if (np) 13059b50d902SRodney W. Grimes cp = np->n_name; 13069b50d902SRodney W. Grimes } 13079b50d902SRodney W. Grimes if (cp == 0) { 13089b50d902SRodney W. Grimes hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); 13099b50d902SRodney W. Grimes if (hp) { 13109b50d902SRodney W. Grimes cp = hp->h_name; 1311d121b556SBrian Somers trimdomain(cp, strlen(cp)); 13129b50d902SRodney W. Grimes } 13139b50d902SRodney W. Grimes } 13149b50d902SRodney W. Grimes } 13159b50d902SRodney W. Grimes if (inp->s_addr == INADDR_ANY) 13169b50d902SRodney W. Grimes strcpy(line, "*"); 13179a1f6729SWarner Losh else if (cp) { 13181c109628SXin LI strlcpy(line, cp, sizeof(line)); 13199a1f6729SWarner Losh } else { 13209b50d902SRodney W. Grimes inp->s_addr = ntohl(inp->s_addr); 132122694ebaSBruce Evans #define C(x) ((u_int)((x) & 0xff)) 132222694ebaSBruce Evans sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24), 13239b50d902SRodney W. Grimes C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); 13249b50d902SRodney W. Grimes } 13259b50d902SRodney W. Grimes return (line); 13269b50d902SRodney W. Grimes } 1327