[BACK]Return to inet.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/inet.c, Revision 1.157

1.157   ! bluhm       1: /*     $OpenBSD: inet.c,v 1.156 2017/03/07 23:35:06 jca Exp $  */
1.1       deraadt     2: /*     $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $        */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.65      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/queue.h>
1.150     guenther   34: #include <sys/select.h>
1.1       deraadt    35: #include <sys/socket.h>
                     36: #include <sys/socketvar.h>
1.114     bluhm      37: #include <sys/domain.h>
1.1       deraadt    38: #include <sys/protosw.h>
1.102     reyk       39: #include <sys/sysctl.h>
1.140     claudio    40: #define _KERNEL
                     41: #include <sys/file.h>
                     42: #undef _KERNEL
1.1       deraadt    43:
                     44: #include <net/route.h>
                     45: #include <netinet/in.h>
                     46: #include <netinet/ip.h>
                     47: #include <netinet/in_pcb.h>
                     48: #include <netinet/ip_icmp.h>
                     49: #include <netinet/icmp_var.h>
                     50: #include <netinet/igmp_var.h>
                     51: #include <netinet/ip_var.h>
                     52: #include <netinet/tcp.h>
                     53: #include <netinet/tcp_seq.h>
                     54: #define TCPSTATES
                     55: #include <netinet/tcp_fsm.h>
                     56: #include <netinet/tcp_timer.h>
                     57: #include <netinet/tcp_var.h>
                     58: #include <netinet/udp.h>
                     59: #include <netinet/udp_var.h>
1.30      angelos    60: #include <netinet/ip_ipsp.h>
1.10      angelos    61: #include <netinet/ip_ah.h>
                     62: #include <netinet/ip_esp.h>
1.45      angelos    63: #include <netinet/ip_ipip.h>
1.52      angelos    64: #include <netinet/ip_ipcomp.h>
1.34      angelos    65: #include <netinet/ip_ether.h>
1.69      mcbride    66: #include <netinet/ip_carp.h>
1.111     michele    67: #include <netinet/ip_divert.h>
1.75      mcbride    68: #include <net/if.h>
                     69: #include <net/pfvar.h>
                     70: #include <net/if_pfsync.h>
1.107     gollo      71: #include <net/if_pflow.h>
1.1       deraadt    72:
1.103     deraadt    73: #include <rpc/rpc.h>
                     74: #include <rpc/pmap_prot.h>
                     75: #include <rpc/pmap_clnt.h>
                     76:
1.1       deraadt    77: #include <arpa/inet.h>
1.110     chl        78: #include <err.h>
1.18      millert    79: #include <limits.h>
1.1       deraadt    80: #include <netdb.h>
                     81: #include <stdio.h>
                     82: #include <string.h>
                     83: #include <unistd.h>
1.14      dgregor    84: #include <stdlib.h>
1.103     deraadt    85: #include <errno.h>
1.1       deraadt    86: #include "netstat.h"
                     87:
                     88: struct inpcb inpcb;
                     89: struct tcpcb tcpcb;
                     90: struct socket sockb;
                     91:
1.56      millert    92: char   *inetname(struct in_addr *);
1.140     claudio    93: void   inetprint(struct in_addr *, in_port_t, const char *, int);
1.56      millert    94: char   *inet6name(struct in6_addr *);
1.137     bluhm      95: void   sosplice_dump(u_long);
1.114     bluhm      96: void   sockbuf_dump(struct sockbuf *, const char *);
                     97: void   protosw_dump(u_long, u_long);
                     98: void   domain_dump(u_long, u_long, short);
                     99: void   inpcb_dump(u_long, short, int);
                    100: void   tcpcb_dump(u_long);
1.152     bluhm     101: int    kf_comp(const void *, const void *);
1.1       deraadt   102:
1.140     claudio   103: int type_map[] = { -1, 2, 3, 1, 4, 5 };
                    104:
                    105: int
                    106: kf_comp(const void *a, const void *b)
                    107: {
                    108:        const struct kinfo_file *ka = a, *kb = b;
                    109:
                    110:        if (ka->so_family != kb->so_family) {
                    111:                /* AF_INET < AF_INET6 < AF_LOCAL */
                    112:                if (ka->so_family == AF_INET)
                    113:                        return (-1);
                    114:                if (ka->so_family == AF_LOCAL)
                    115:                        return (1);
                    116:                if (kb->so_family == AF_LOCAL)
                    117:                        return (-1);
                    118:                return (1);
                    119:        }
                    120:        if (ka->so_family == AF_LOCAL) {
                    121:                if (type_map[ka->so_type] < type_map[kb->so_type])
                    122:                        return (-1);
                    123:                if (type_map[ka->so_type] > type_map[kb->so_type])
                    124:                        return (1);
                    125:        } else if (ka->so_family == AF_INET || ka->so_family == AF_INET6) {
                    126:                if (ka->so_protocol < kb->so_protocol)
                    127:                        return (-1);
                    128:                if (ka->so_protocol > kb->so_protocol)
                    129:                        return (1);
                    130:                if (ka->so_type == SOCK_DGRAM || ka->so_type == SOCK_STREAM) {
                    131:                        /* order sockets by remote port desc */
                    132:                        if (ka->inp_fport > kb->inp_fport)
                    133:                                return (-1);
                    134:                        if (ka->inp_fport < kb->inp_fport)
                    135:                                return (1);
                    136:                } else if (ka->so_type == SOCK_RAW) {
                    137:                        if (ka->inp_proto > kb->inp_proto)
                    138:                                return (-1);
                    139:                        if (ka->inp_proto < kb->inp_proto)
                    140:                                return (1);
                    141:                }
                    142:        }
                    143:        return (0);
                    144: }
                    145:
                    146: void
                    147: protopr(kvm_t *kvmd, u_long pcbaddr, u_int tableid, int proto)
                    148: {
                    149:        struct kinfo_file *kf;
                    150:        int i, fcnt;
                    151:
                    152:        kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, DTYPE_SOCKET,
                    153:            sizeof(*kf), &fcnt);
                    154:        if (kf == NULL) {
                    155:                printf("Out of memory (file table).\n");
                    156:                return;
                    157:        }
                    158:
                    159:        /* sort sockets by AF and type */
                    160:        qsort(kf, fcnt, sizeof(*kf), kf_comp);
                    161:
                    162:        for (i = 0; i < fcnt; i++) {
                    163:                if (Pflag) {
                    164:                        switch (kf[i].so_family) {
                    165:                        case AF_INET:
                    166:                        case AF_INET6:
                    167:                                /*
                    168:                                 * XXX at the moment fstat returns the pointer
                    169:                                 * to the so_pcb or for tcp sockets the tcpcb
                    170:                                 * pointer (inp_ppcb) so check both.
                    171:                                 */
                    172:                                if (pcbaddr == kf[i].so_pcb) {
                    173:                                        inpcb_dump(pcbaddr, kf[i].so_protocol,
                    174:                                            kf[i].so_family);
                    175:                                        return;
                    176:                                } else if (pcbaddr == kf[i].inp_ppcb &&
                    177:                                    kf[i].so_protocol == IPPROTO_TCP) {
                    178:                                        tcpcb_dump(pcbaddr);
                    179:                                        return;
                    180:                                }
                    181:                                break;
                    182:                        case AF_UNIX:
                    183:                                if (pcbaddr == kf[i].so_pcb) {
                    184:                                        unpcb_dump(pcbaddr);
                    185:                                        return;
                    186:                                }
                    187:                                break;
                    188:                        }
                    189:                        continue;
                    190:                }
                    191:                if (kf[i].so_family == AF_LOCAL && (kf[i].so_pcb != 0 ||
                    192:                    kf[i].unp_path[0] != '\0'))
                    193:                        if ((af == AF_LOCAL || af == AF_UNSPEC) && !proto)
                    194:                                unixdomainpr(&kf[i]);
                    195:                if (kf[i].so_family == AF_INET && kf[i].so_pcb != 0 &&
                    196:                    kf[i].inp_rtableid == tableid)
                    197:                        if (af == AF_INET || af == AF_UNSPEC)
                    198:                                netdomainpr(&kf[i], proto);
                    199:                if (kf[i].so_family == AF_INET6 && kf[i].so_pcb != 0 &&
                    200:                    kf[i].inp_rtableid == tableid)
                    201:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    202:                                netdomainpr(&kf[i], proto);
                    203:        }
                    204: }
                    205:
1.1       deraadt   206: /*
                    207:  * Print a summary of connections related to an Internet
                    208:  * protocol.  For TCP, also give state of connection.
                    209:  * Listening processes (aflag) are suppressed unless the
                    210:  * -a (all) flag is specified.
                    211:  */
                    212: void
1.140     claudio   213: netdomainpr(struct kinfo_file *kf, int proto)
1.48      itojun    214: {
1.140     claudio   215:        static int af = 0, type = 0;
                    216:        struct in_addr laddr, faddr;
                    217:        struct in6_addr laddr6, faddr6;
                    218:        const char *name, *name6;
1.116     jsing     219:        int addrlen = 22;
1.140     claudio   220:        int isany = 0;
                    221:        int istcp = 0;
                    222:        int isip6 = 0;
                    223:
                    224:        /* XXX should fix kinfo_file instead but not now */
                    225:        if (kf->so_pcb == -1)
                    226:                kf->so_pcb = 0;
                    227:
                    228:        switch (proto) {
                    229:        case IPPROTO_TCP:
                    230:        case IPPROTO_UDP:
                    231:        case IPPROTO_DIVERT:
                    232:                if (kf->so_protocol != proto)
                    233:                        return;
                    234:                break;
                    235:        case IPPROTO_IPV4:
                    236:                if (kf->so_type != SOCK_RAW || kf->so_family != AF_INET)
                    237:                        return;
                    238:                break;
                    239:        case IPPROTO_IPV6:
                    240:                if (kf->so_type != SOCK_RAW || kf->so_family != AF_INET6)
                    241:                        return;
                    242:                break;
                    243:        }
1.1       deraadt   244:
1.140     claudio   245:        /* make in_addr6 access a bit easier */
                    246: #define s6_addr32 __u6_addr.__u6_addr32
                    247:        laddr.s_addr = kf->inp_laddru[0];
                    248:        laddr6.s6_addr32[0] = kf->inp_laddru[0];
                    249:        laddr6.s6_addr32[1] = kf->inp_laddru[1];
                    250:        laddr6.s6_addr32[2] = kf->inp_laddru[2];
                    251:        laddr6.s6_addr32[3] = kf->inp_laddru[3];
                    252:
                    253:        faddr.s_addr = kf->inp_faddru[0];
                    254:        faddr6.s6_addr32[0] = kf->inp_faddru[0];
                    255:        faddr6.s6_addr32[1] = kf->inp_faddru[1];
                    256:        faddr6.s6_addr32[2] = kf->inp_faddru[2];
                    257:        faddr6.s6_addr32[3] = kf->inp_faddru[3];
                    258: #undef s6_addr32
1.48      itojun    259:
1.140     claudio   260:        switch (kf->so_family) {
                    261:        case AF_INET:
                    262:                isany = faddr.s_addr == INADDR_ANY;
                    263:                break;
                    264:        case AF_INET6:
                    265:                isany = IN6_IS_ADDR_UNSPECIFIED(&faddr6);
                    266:                isip6 = 1;
                    267:                break;
                    268:        }
1.1       deraadt   269:
1.140     claudio   270:        switch (kf->so_protocol) {
                    271:        case IPPROTO_TCP:
                    272:                name = "tcp";
                    273:                name6 = "tcp6";
                    274:                istcp = 1;
                    275:                break;
                    276:        case IPPROTO_UDP:
                    277:                name = "udp";
                    278:                name6 = "udp6";
                    279:                break;
                    280:        case IPPROTO_DIVERT:
                    281:                name = "divert";
                    282:                name6 = "divert6";
                    283:                break;
                    284:        default:
                    285:                name = "ip";
                    286:                name6 = "ip6";
                    287:                break;
                    288:        }
1.119     mikeb     289:
1.140     claudio   290:        /* filter listening sockets out unless -a is set */
                    291:        if (!aflag && istcp && kf->t_state <= TCPS_LISTEN)
                    292:                return;
                    293:        else if (!aflag && isany)
                    294:                return;
1.114     bluhm     295:
1.140     claudio   296:        if (af != kf->so_family || type != kf->so_type) {
                    297:                af = kf->so_family;
                    298:                type = kf->so_type;
                    299:                printf("Active Internet connections");
                    300:                if (aflag)
                    301:                        printf(" (including servers)");
                    302:                putchar('\n');
1.38      itojun    303:                if (Aflag) {
1.140     claudio   304:                        addrlen = 18;
                    305:                        printf("%-*.*s ", PLEN, PLEN, "PCB");
1.38      itojun    306:                }
1.140     claudio   307:                printf("%-7.7s %-6.6s %-6.6s ",
                    308:                    "Proto", "Recv-Q", "Send-Q");
1.116     jsing     309:                if (Bflag && istcp)
1.140     claudio   310:                        printf("%-6.6s %-6.6s %-6.6s ",
                    311:                            "Recv-W", "Send-W", "Cgst-W");
                    312:                printf(" %-*.*s %-*.*s %s\n",
                    313:                    addrlen, addrlen, "Local Address",
                    314:                    addrlen, addrlen, "Foreign Address", "(state)");
                    315:        }
                    316:
                    317:        if (Aflag)
1.157   ! bluhm     318:                printf("%#*llx%s ", FAKE_PTR(kf->so_protocol == IPPROTO_TCP ?
        !           319:                    kf->inp_ppcb : kf->so_pcb));
1.140     claudio   320:
                    321:        printf("%-7.7s %6llu %6llu ",
                    322:            isip6 ? name6: name, kf->so_rcv_cc, kf->so_snd_cc);
                    323:        if (Bflag && istcp)
                    324:                printf("%6llu %6llu %6llu ", kf->t_rcv_wnd, kf->t_snd_wnd,
                    325:                    (kf->t_state == TCPS_ESTABLISHED) ?
                    326:                    kf->t_snd_cwnd : 0);
                    327:
                    328:        if (isip6) {
                    329:                inet6print(&laddr6, kf->inp_lport, name);
                    330:                inet6print(&faddr6, kf->inp_fport, name);
                    331:        } else {
                    332:                inetprint(&laddr, kf->inp_lport, name, 1);
                    333:                inetprint(&faddr, kf->inp_fport, name, 0);
                    334:        }
                    335:        if (istcp) {
                    336:                if (kf->t_state < 0 || kf->t_state >= TCP_NSTATES)
                    337:                        printf(" %d", kf->t_state);
                    338:                else
                    339:                        printf(" %s", tcpstates[kf->t_state]);
                    340:        } else if (kf->so_type == SOCK_RAW) {
                    341:                printf(" %u", kf->inp_proto);
1.1       deraadt   342:        }
1.140     claudio   343:        putchar('\n');
1.1       deraadt   344: }
                    345:
                    346: /*
                    347:  * Dump TCP statistics structure.
                    348:  */
                    349: void
1.103     deraadt   350: tcp_stats(char *name)
1.1       deraadt   351: {
                    352:        struct tcpstat tcpstat;
1.133     guenther  353:        int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_STATS };
1.103     deraadt   354:        size_t len = sizeof(tcpstat);
1.1       deraadt   355:
1.102     reyk      356:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    357:            &tcpstat, &len, NULL, 0) == -1) {
1.103     deraadt   358:                if (errno != ENOPROTOOPT)
1.124     guenther  359:                        warn("%s", name);
1.102     reyk      360:                return;
                    361:        }
                    362:
1.63      deraadt   363:        printf("%s:\n", name);
1.1       deraadt   364: #define        p(f, m) if (tcpstat.f || sflag <= 1) \
1.60      deraadt   365:        printf(m, tcpstat.f, plural(tcpstat.f))
1.38      itojun    366: #define        p1(f, m) if (tcpstat.f || sflag <= 1) \
1.60      deraadt   367:        printf(m, tcpstat.f)
1.1       deraadt   368: #define        p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
1.60      deraadt   369:        printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
1.38      itojun    370: #define        p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
1.60      deraadt   371:        printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
1.147     bluhm     372: #define        p2b(f1, f2, m) if (tcpstat.f1 || sflag <= 1) \
                    373:        printf(m, tcpstat.f1, tcpstat.f2)
                    374: #define        p2bys(f1, f2, m) if (tcpstat.f1 || sflag <= 1) \
                    375:        printf(m, tcpstat.f1, pluralys(tcpstat.f1), tcpstat.f2)
                    376: #define        pes(f, m) if (tcpstat.f || sflag <= 1) \
1.60      deraadt   377:        printf(m, tcpstat.f, plurales(tcpstat.f))
1.147     bluhm     378: #define        pys(f, m) if (tcpstat.f || sflag <= 1) \
                    379:        printf(m, tcpstat.f, pluralys(tcpstat.f))
1.1       deraadt   380:
1.38      itojun    381:        p(tcps_sndtotal, "\t%u packet%s sent\n");
1.1       deraadt   382:        p2(tcps_sndpack,tcps_sndbyte,
1.149     krw       383:            "\t\t%u data packet%s (%llu byte%s)\n");
1.1       deraadt   384:        p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
1.149     krw       385:            "\t\t%u data packet%s (%llu byte%s) retransmitted\n");
                    386:        p(tcps_sndrexmitfast, "\t\t%llu fast retransmitted packet%s\n");
1.38      itojun    387:        p2a(tcps_sndacks, tcps_delack,
1.60      deraadt   388:            "\t\t%u ack-only packet%s (%u delayed)\n");
1.38      itojun    389:        p(tcps_sndurg, "\t\t%u URG only packet%s\n");
                    390:        p(tcps_sndprobe, "\t\t%u window probe packet%s\n");
                    391:        p(tcps_sndwinup, "\t\t%u window update packet%s\n");
                    392:        p(tcps_sndctrl, "\t\t%u control packet%s\n");
1.131     henning   393:        p(tcps_outswcsum, "\t\t%u packet%s software-checksummed\n");
1.38      itojun    394:        p(tcps_rcvtotal, "\t%u packet%s received\n");
1.149     krw       395:        p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%u ack%s (for %llu byte%s)\n");
1.38      itojun    396:        p(tcps_rcvdupack, "\t\t%u duplicate ack%s\n");
                    397:        p(tcps_rcvacktoomuch, "\t\t%u ack%s for unsent data\n");
1.85      markus    398:        p(tcps_rcvacktooold, "\t\t%u ack%s for old data\n");
1.1       deraadt   399:        p2(tcps_rcvpack, tcps_rcvbyte,
1.148     krw       400:            "\t\t%u packet%s (%llu byte%s) received in-sequence\n");
1.1       deraadt   401:        p2(tcps_rcvduppack, tcps_rcvdupbyte,
1.149     krw       402:            "\t\t%u completely duplicate packet%s (%llu byte%s)\n");
1.38      itojun    403:        p(tcps_pawsdrop, "\t\t%u old duplicate packet%s\n");
1.1       deraadt   404:        p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
1.149     krw       405:            "\t\t%u packet%s with some duplicate data (%llu byte%s duplicated)\n");
1.1       deraadt   406:        p2(tcps_rcvoopack, tcps_rcvoobyte,
1.149     krw       407:            "\t\t%u out-of-order packet%s (%llu byte%s)\n");
1.1       deraadt   408:        p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
1.149     krw       409:            "\t\t%u packet%s (%llu byte%s) of data after window\n");
1.38      itojun    410:        p(tcps_rcvwinprobe, "\t\t%u window probe%s\n");
                    411:        p(tcps_rcvwinupd, "\t\t%u window update packet%s\n");
                    412:        p(tcps_rcvafterclose, "\t\t%u packet%s received after close\n");
                    413:        p(tcps_rcvbadsum, "\t\t%u discarded for bad checksum%s\n");
                    414:        p(tcps_rcvbadoff, "\t\t%u discarded for bad header offset field%s\n");
                    415:        p1(tcps_rcvshort, "\t\t%u discarded because packet too short\n");
1.58      miod      416:        p1(tcps_rcvnosec, "\t\t%u discarded for missing IPsec protection\n");
1.82      markus    417:        p1(tcps_rcvmemdrop, "\t\t%u discarded due to memory shortage\n");
1.131     henning   418:        p(tcps_inswcsum, "\t\t%u packet%s software-checksummed\n");
1.80      markus    419:        p(tcps_rcvbadsig, "\t\t%u bad/missing md5 checksum%s\n");
1.149     krw       420:        p(tcps_rcvgoodsig, "\t\t%llu good md5 checksum%s\n");
1.38      itojun    421:        p(tcps_connattempt, "\t%u connection request%s\n");
                    422:        p(tcps_accepts, "\t%u connection accept%s\n");
                    423:        p(tcps_connects, "\t%u connection%s established (including accepts)\n");
1.1       deraadt   424:        p2(tcps_closed, tcps_drops,
1.60      deraadt   425:            "\t%u connection%s closed (including %u drop%s)\n");
1.149     krw       426:        p(tcps_conndrained, "\t%llu connection%s drained\n");
1.38      itojun    427:        p(tcps_conndrops, "\t%u embryonic connection%s dropped\n");
1.1       deraadt   428:        p2(tcps_rttupdated, tcps_segstimed,
1.60      deraadt   429:            "\t%u segment%s updated rtt (of %u attempt%s)\n");
1.38      itojun    430:        p(tcps_rexmttimeo, "\t%u retransmit timeout%s\n");
                    431:        p(tcps_timeoutdrop, "\t\t%u connection%s dropped by rexmit timeout\n");
                    432:        p(tcps_persisttimeo, "\t%u persist timeout%s\n");
                    433:        p(tcps_keeptimeo, "\t%u keepalive timeout%s\n");
                    434:        p(tcps_keepprobe, "\t\t%u keepalive probe%s sent\n");
                    435:        p(tcps_keepdrops, "\t\t%u connection%s dropped by keepalive\n");
                    436:        p(tcps_predack, "\t%u correct ACK header prediction%s\n");
                    437:        p(tcps_preddat, "\t%u correct data packet header prediction%s\n");
1.147     bluhm     438:        pes(tcps_pcbhashmiss, "\t%u PCB cache miss%s\n");
1.146     bluhm     439:        p1(tcps_noport, "\t%u dropped due to no socket\n");
1.59      kjc       440:
                    441:        p(tcps_ecn_accepts, "\t%u ECN connection%s accepted\n");
                    442:        p(tcps_ecn_rcvece, "\t\t%u ECE packet%s received\n");
                    443:        p(tcps_ecn_rcvcwr, "\t\t%u CWR packet%s received\n");
                    444:        p(tcps_ecn_rcvce, "\t\t%u CE packet%s received\n");
                    445:        p(tcps_ecn_sndect, "\t\t%u ECT packet%s sent\n");
                    446:        p(tcps_ecn_sndece, "\t\t%u ECE packet%s sent\n");
                    447:        p(tcps_ecn_sndcwr, "\t\t%u CWR packet%s sent\n");
                    448:        p1(tcps_cwr_frecovery, "\t\t\tcwr by fastrecovery: %u\n");
                    449:        p1(tcps_cwr_timeout, "\t\t\tcwr by timeout: %u\n");
                    450:        p1(tcps_cwr_ecn, "\t\t\tcwr by ecn: %u\n");
1.78      markus    451:
                    452:        p(tcps_badsyn, "\t%u bad connection attempt%s\n");
1.139     yasuoka   453:        p(tcps_dropsyn, "\t%u SYN packet%s dropped due to queue or memory full\n");
1.149     krw       454:        pys(tcps_sc_added, "\t%llu SYN cache entr%s added\n");
                    455:        p(tcps_sc_collisions, "\t\t%llu hash collision%s\n");
                    456:        p1(tcps_sc_completed, "\t\t%llu completed\n");
                    457:        p1(tcps_sc_aborted, "\t\t%llu aborted (no space to build PCB)\n");
                    458:        p1(tcps_sc_timed_out, "\t\t%llu timed out\n");
                    459:        p1(tcps_sc_overflowed, "\t\t%llu dropped due to overflow\n");
                    460:        p1(tcps_sc_bucketoverflow, "\t\t%llu dropped due to bucket overflow\n");
                    461:        p1(tcps_sc_reset, "\t\t%llu dropped due to RST\n");
                    462:        p1(tcps_sc_unreach, "\t\t%llu dropped due to ICMP unreachable\n");
                    463:        p(tcps_sc_retransmitted, "\t%llu SYN,ACK%s retransmitted\n");
                    464:        p(tcps_sc_dupesyn, "\t%llu duplicate SYN%s received for entries "
1.147     bluhm     465:            "already in the cache\n");
1.149     krw       466:        p(tcps_sc_dropped, "\t%llu SYN%s dropped (no route or no space)\n");
                    467:        p(tcps_sc_seedrandom, "\t%llu SYN cache seed%s with new random\n");
                    468:        p1(tcps_sc_hash_size, "\t%llu hash bucket array size in current "
1.147     bluhm     469:            "SYN cache\n");
                    470:        p2bys(tcps_sc_entry_count, tcps_sc_entry_limit,
1.149     krw       471:            "\t%llu entr%s in current SYN cache, limit is %llu\n");
1.147     bluhm     472:        p2b(tcps_sc_bucket_maxlen, tcps_sc_bucket_limit,
1.149     krw       473:            "\t%llu longest bucket length in current SYN cache, limit is %llu\n");
                    474:        p(tcps_sc_uses_left, "\t%llu use%s of current SYN cache left\n");
1.95      markus    475:
1.149     krw       476:        p(tcps_sack_recovery_episode, "\t%llu SACK recovery episode%s\n");
1.95      markus    477:        p(tcps_sack_rexmits,
1.149     krw       478:                "\t\t%llu segment rexmit%s in SACK recovery episodes\n");
1.95      markus    479:        p(tcps_sack_rexmit_bytes,
1.149     krw       480:                "\t\t%llu byte rexmit%s in SACK recovery episodes\n");
1.95      markus    481:        p(tcps_sack_rcv_opts,
1.149     krw       482:                "\t%llu SACK option%s received\n");
                    483:        p(tcps_sack_snd_opts, "\t%llu SACK option%s sent\n");
1.59      kjc       484:
1.1       deraadt   485: #undef p
1.38      itojun    486: #undef p1
1.1       deraadt   487: #undef p2
1.38      itojun    488: #undef p2a
1.147     bluhm     489: #undef p2b
                    490: #undef p2bys
                    491: #undef pes
                    492: #undef pys
1.1       deraadt   493: }
                    494:
                    495: /*
                    496:  * Dump UDP statistics structure.
                    497:  */
                    498: void
1.103     deraadt   499: udp_stats(char *name)
1.1       deraadt   500: {
                    501:        struct udpstat udpstat;
                    502:        u_long delivered;
1.133     guenther  503:        int mib[] = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS };
1.103     deraadt   504:        size_t len = sizeof(udpstat);
1.1       deraadt   505:
1.102     reyk      506:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    507:            &udpstat, &len, NULL, 0) == -1) {
1.103     deraadt   508:                if (errno != ENOPROTOOPT)
1.124     guenther  509:                        warn("%s", name);
1.102     reyk      510:                return;
                    511:        }
                    512:
1.1       deraadt   513:        printf("%s:\n", name);
                    514: #define        p(f, m) if (udpstat.f || sflag <= 1) \
1.60      deraadt   515:        printf(m, udpstat.f, plural(udpstat.f))
1.38      itojun    516: #define        p1(f, m) if (udpstat.f || sflag <= 1) \
1.60      deraadt   517:        printf(m, udpstat.f)
                    518:
1.16      millert   519:        p(udps_ipackets, "\t%lu datagram%s received\n");
1.38      itojun    520:        p1(udps_hdrops, "\t%lu with incomplete header\n");
                    521:        p1(udps_badlen, "\t%lu with bad data length field\n");
                    522:        p1(udps_badsum, "\t%lu with bad checksum\n");
                    523:        p1(udps_nosum, "\t%lu with no checksum\n");
1.131     henning   524:        p(udps_inswcsum, "\t%lu input packet%s software-checksummed\n");
                    525:        p(udps_outswcsum, "\t%lu output packet%s software-checksummed\n");
1.38      itojun    526:        p1(udps_noport, "\t%lu dropped due to no socket\n");
1.16      millert   527:        p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
1.58      miod      528:        p1(udps_nosec, "\t%lu dropped due to missing IPsec protection\n");
1.38      itojun    529:        p1(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
1.60      deraadt   530:        delivered = udpstat.udps_ipackets - udpstat.udps_hdrops -
                    531:            udpstat.udps_badlen - udpstat.udps_badsum -
                    532:            udpstat.udps_noport - udpstat.udps_noportbcast -
                    533:            udpstat.udps_fullsock;
1.1       deraadt   534:        if (delivered || sflag <= 1)
1.16      millert   535:                printf("\t%lu delivered\n", delivered);
                    536:        p(udps_opackets, "\t%lu datagram%s output\n");
1.38      itojun    537:        p1(udps_pcbhashmiss, "\t%lu missed PCB cache\n");
1.1       deraadt   538: #undef p
1.38      itojun    539: #undef p1
1.1       deraadt   540: }
                    541:
                    542: /*
                    543:  * Dump IP statistics structure.
                    544:  */
                    545: void
1.103     deraadt   546: ip_stats(char *name)
1.1       deraadt   547: {
                    548:        struct ipstat ipstat;
1.133     guenther  549:        int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS };
1.109     mbalmer   550:        size_t len = sizeof(ipstat);
1.1       deraadt   551:
1.102     reyk      552:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    553:            &ipstat, &len, NULL, 0) == -1) {
1.103     deraadt   554:                if (errno != ENOPROTOOPT)
1.124     guenther  555:                        warn("%s", name);
1.102     reyk      556:                return;
                    557:        }
                    558:
1.1       deraadt   559:        printf("%s:\n", name);
                    560: #define        p(f, m) if (ipstat.f || sflag <= 1) \
1.60      deraadt   561:        printf(m, ipstat.f, plural(ipstat.f))
1.38      itojun    562: #define        p1(f, m) if (ipstat.f || sflag <= 1) \
1.60      deraadt   563:        printf(m, ipstat.f)
1.1       deraadt   564:
1.16      millert   565:        p(ips_total, "\t%lu total packet%s received\n");
                    566:        p(ips_badsum, "\t%lu bad header checksum%s\n");
1.38      itojun    567:        p1(ips_toosmall, "\t%lu with size smaller than minimum\n");
                    568:        p1(ips_tooshort, "\t%lu with data size < data length\n");
                    569:        p1(ips_badhlen, "\t%lu with header length < data size\n");
                    570:        p1(ips_badlen, "\t%lu with data length < header length\n");
                    571:        p1(ips_badoptions, "\t%lu with bad options\n");
                    572:        p1(ips_badvers, "\t%lu with incorrect version number\n");
1.16      millert   573:        p(ips_fragments, "\t%lu fragment%s received\n");
1.62      jsyn      574:        p(ips_fragdropped, "\t%lu fragment%s dropped (duplicates or out of space)\n");
1.16      millert   575:        p(ips_badfrags, "\t%lu malformed fragment%s dropped\n");
                    576:        p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
                    577:        p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
                    578:        p(ips_delivered, "\t%lu packet%s for this host\n");
                    579:        p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
                    580:        p(ips_forward, "\t%lu packet%s forwarded\n");
                    581:        p(ips_cantforward, "\t%lu packet%s not forwardable\n");
                    582:        p(ips_redirectsent, "\t%lu redirect%s sent\n");
                    583:        p(ips_localout, "\t%lu packet%s sent from this host\n");
                    584:        p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
                    585:        p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n");
                    586:        p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
                    587:        p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
                    588:        p(ips_ofragments, "\t%lu fragment%s created\n");
                    589:        p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
1.38      itojun    590:        p1(ips_rcvmemdrop, "\t%lu fragment floods\n");
1.42      itojun    591:        p(ips_toolong, "\t%lu packet%s with ip length > max ip packet size\n");
                    592:        p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
1.49      itojun    593:        p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
1.131     henning   594:        p(ips_inswcsum, "\t%lu input datagram%s software-checksummed\n");
                    595:        p(ips_outswcsum, "\t%lu output datagram%s software-checksummed\n");
1.97      mpf       596:        p(ips_notmember, "\t%lu multicast packet%s which we don't join\n");
1.111     michele   597: #undef p
                    598: #undef p1
                    599: }
                    600:
                    601: /*
                    602:  * Dump DIVERT statistics structure.
                    603:  */
                    604: void
                    605: div_stats(char *name)
                    606: {
                    607:        struct divstat divstat;
1.133     guenther  608:        int mib[] = { CTL_NET, PF_INET, IPPROTO_DIVERT, DIVERTCTL_STATS };
1.111     michele   609:        size_t len = sizeof(divstat);
                    610:
                    611:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    612:            &divstat, &len, NULL, 0) == -1) {
                    613:                if (errno != ENOPROTOOPT)
1.124     guenther  614:                        warn("%s", name);
1.111     michele   615:                return;
                    616:        }
                    617:
                    618:        printf("%s:\n", name);
                    619: #define        p(f, m) if (divstat.f || sflag <= 1) \
                    620:        printf(m, divstat.f, plural(divstat.f))
                    621: #define        p1(f, m) if (divstat.f || sflag <= 1) \
                    622:        printf(m, divstat.f)
                    623:        p(divs_ipackets, "\t%lu total packet%s received\n");
                    624:        p1(divs_noport, "\t%lu dropped due to no socket\n");
                    625:        p1(divs_fullsock, "\t%lu dropped due to full socket buffers\n");
                    626:        p(divs_opackets, "\t%lu packet%s output\n");
                    627:        p1(divs_errors, "\t%lu errors\n");
1.1       deraadt   628: #undef p
1.38      itojun    629: #undef p1
1.1       deraadt   630: }
                    631:
1.68      tedu      632: static char *icmpnames[ICMP_MAXTYPE + 1] = {
1.1       deraadt   633:        "echo reply",
                    634:        "#1",
                    635:        "#2",
                    636:        "destination unreachable",
                    637:        "source quench",
                    638:        "routing redirect",
                    639:        "#6",
                    640:        "#7",
                    641:        "echo",
1.9       deraadt   642:        "router advertisement",
                    643:        "router solicitation",
1.1       deraadt   644:        "time exceeded",
                    645:        "parameter problem",
                    646:        "time stamp",
                    647:        "time stamp reply",
                    648:        "information request",
                    649:        "information request reply",
                    650:        "address mask request",
                    651:        "address mask reply",
1.68      tedu      652:        "#19",
                    653:        "#20",
                    654:        "#21",
                    655:        "#22",
                    656:        "#23",
                    657:        "#24",
                    658:        "#25",
                    659:        "#26",
                    660:        "#27",
                    661:        "#28",
                    662:        "#29",
                    663:        "traceroute",
                    664:        "data conversion error",
                    665:        "mobile host redirect",
                    666:        "IPv6 where-are-you",
                    667:        "IPv6 i-am-here",
                    668:        "mobile registration request",
                    669:        "mobile registration reply",
                    670:        "#37",
                    671:        "#38",
                    672:        "SKIP",
                    673:        "Photuris",
1.1       deraadt   674: };
                    675:
                    676: /*
                    677:  * Dump ICMP statistics.
                    678:  */
                    679: void
1.103     deraadt   680: icmp_stats(char *name)
1.1       deraadt   681: {
                    682:        struct icmpstat icmpstat;
1.54      mpech     683:        int i, first;
1.133     guenther  684:        int mib[] = { CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS };
1.103     deraadt   685:        size_t len = sizeof(icmpstat);
1.102     reyk      686:
                    687:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    688:            &icmpstat, &len, NULL, 0) == -1) {
1.103     deraadt   689:                if (errno != ENOPROTOOPT)
1.124     guenther  690:                        warn("%s", name);
1.102     reyk      691:                return;
                    692:        }
                    693:
1.1       deraadt   694:        printf("%s:\n", name);
                    695: #define        p(f, m) if (icmpstat.f || sflag <= 1) \
1.60      deraadt   696:        printf(m, icmpstat.f, plural(icmpstat.f))
1.1       deraadt   697:
1.16      millert   698:        p(icps_error, "\t%lu call%s to icmp_error\n");
1.1       deraadt   699:        p(icps_oldicmp,
1.61      aaron     700:            "\t%lu error%s not generated because old message was icmp\n");
1.130     claudio   701:        p(icps_toofreq,
1.132     claudio   702:            "\t%lu error%s not generated because of rate limitation\n");
1.130     claudio   703:
1.1       deraadt   704:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    705:                if (icmpstat.icps_outhist[i] != 0) {
                    706:                        if (first) {
1.43      itojun    707:                                printf("\tOutput packet histogram:\n");
1.1       deraadt   708:                                first = 0;
                    709:                        }
1.68      tedu      710:                        if (icmpnames[i])
                    711:                                printf("\t\t%s:", icmpnames[i]);
                    712:                        else
                    713:                                printf("\t\t#%d:", i);
                    714:                        printf(" %lu\n", icmpstat.icps_outhist[i]);
1.1       deraadt   715:                }
1.16      millert   716:        p(icps_badcode, "\t%lu message%s with bad code fields\n");
                    717:        p(icps_tooshort, "\t%lu message%s < minimum length\n");
                    718:        p(icps_checksum, "\t%lu bad checksum%s\n");
                    719:        p(icps_badlen, "\t%lu message%s with bad length\n");
1.117     sthen     720:        p(icps_bmcastecho, "\t%lu echo request%s to broadcast/multicast "
                    721:            "rejected\n");
1.1       deraadt   722:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    723:                if (icmpstat.icps_inhist[i] != 0) {
                    724:                        if (first) {
1.43      itojun    725:                                printf("\tInput packet histogram:\n");
1.1       deraadt   726:                                first = 0;
                    727:                        }
1.68      tedu      728:                        if (icmpnames[i])
                    729:                                printf("\t\t%s:", icmpnames[i]);
                    730:                        else
                    731:                                printf("\t\t#%d:", i);
                    732:                        printf(" %lu\n", icmpstat.icps_inhist[i]);
1.1       deraadt   733:                }
1.16      millert   734:        p(icps_reflect, "\t%lu message response%s generated\n");
1.1       deraadt   735: #undef p
                    736: }
                    737:
                    738: /*
                    739:  * Dump IGMP statistics structure.
                    740:  */
                    741: void
1.103     deraadt   742: igmp_stats(char *name)
1.1       deraadt   743: {
                    744:        struct igmpstat igmpstat;
1.133     guenther  745:        int mib[] = { CTL_NET, PF_INET, IPPROTO_IGMP, IGMPCTL_STATS };
1.103     deraadt   746:        size_t len = sizeof(igmpstat);
1.1       deraadt   747:
1.103     deraadt   748:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    749:            &igmpstat, &len, NULL, 0) == -1) {
                    750:                if (errno != ENOPROTOOPT)
1.124     guenther  751:                        warn("%s", name);
1.1       deraadt   752:                return;
1.103     deraadt   753:        }
                    754:
1.1       deraadt   755:        printf("%s:\n", name);
                    756: #define        p(f, m) if (igmpstat.f || sflag <= 1) \
1.60      deraadt   757:        printf(m, igmpstat.f, plural(igmpstat.f))
1.1       deraadt   758: #define        py(f, m) if (igmpstat.f || sflag <= 1) \
1.60      deraadt   759:        printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
                    760:
1.16      millert   761:        p(igps_rcv_total, "\t%lu message%s received\n");
1.55      mickey    762:        p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
                    763:        p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
                    764:        py(igps_rcv_queries, "\t%lu membership quer%s received\n");
                    765:        py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
                    766:        p(igps_rcv_reports, "\t%lu membership report%s received\n");
                    767:        p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
                    768:        p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
                    769:        p(igps_snd_reports, "\t%lu membership report%s sent\n");
1.1       deraadt   770: #undef p
                    771: #undef py
                    772: }
                    773:
1.5       deraadt   774: struct rpcnams {
                    775:        struct rpcnams *next;
1.16      millert   776:        in_port_t port;
1.21      deraadt   777:        int       proto;
1.5       deraadt   778:        char    *rpcname;
                    779: };
                    780:
1.66      deraadt   781: static char *
1.63      deraadt   782: getrpcportnam(in_port_t port, int proto)
1.5       deraadt   783: {
                    784:        struct sockaddr_in server_addr;
1.54      mpech     785:        struct hostent *hp;
1.5       deraadt   786:        static struct pmaplist *head;
                    787:        int socket = RPC_ANYSOCK;
                    788:        struct timeval minutetimeout;
1.54      mpech     789:        CLIENT *client;
1.5       deraadt   790:        struct rpcent *rpc;
                    791:        static int first;
                    792:        static struct rpcnams *rpcn;
                    793:        struct rpcnams *n;
1.23      deraadt   794:        char num[20];
1.55      mickey    795:
1.5       deraadt   796:        if (first == 0) {
                    797:                first = 1;
1.93      jaredy    798:                memset(&server_addr, 0, sizeof server_addr);
1.5       deraadt   799:                server_addr.sin_family = AF_INET;
                    800:                if ((hp = gethostbyname("localhost")) != NULL)
1.7       deraadt   801:                        memmove((caddr_t)&server_addr.sin_addr, hp->h_addr,
1.5       deraadt   802:                            hp->h_length);
                    803:                else
                    804:                        (void) inet_aton("0.0.0.0", &server_addr.sin_addr);
                    805:
                    806:                minutetimeout.tv_sec = 60;
                    807:                minutetimeout.tv_usec = 0;
                    808:                server_addr.sin_port = htons(PMAPPORT);
                    809:                if ((client = clnttcp_create(&server_addr, PMAPPROG,
1.7       deraadt   810:                    PMAPVERS, &socket, 50, 500)) == NULL)
                    811:                        return (NULL);
1.5       deraadt   812:                if (clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL,
1.8       deraadt   813:                    xdr_pmaplist, &head, minutetimeout) != RPC_SUCCESS) {
1.7       deraadt   814:                        clnt_destroy(client);
                    815:                        return (NULL);
1.5       deraadt   816:                }
                    817:                for (; head != NULL; head = head->pml_next) {
1.144     deraadt   818:                        n = malloc(sizeof(struct rpcnams));
1.5       deraadt   819:                        if (n == NULL)
                    820:                                continue;
                    821:                        n->next = rpcn;
                    822:                        rpcn = n;
                    823:                        n->port = head->pml_map.pm_port;
1.21      deraadt   824:                        n->proto = head->pml_map.pm_prot;
1.5       deraadt   825:
                    826:                        rpc = getrpcbynumber(head->pml_map.pm_prog);
                    827:                        if (rpc)
                    828:                                n->rpcname = strdup(rpc->r_name);
                    829:                        else {
1.23      deraadt   830:                                snprintf(num, sizeof num, "%ld",
                    831:                                    head->pml_map.pm_prog);
1.5       deraadt   832:                                n->rpcname = strdup(num);
                    833:                        }
                    834:                }
                    835:                clnt_destroy(client);
                    836:        }
                    837:
                    838:        for (n = rpcn; n; n = n->next)
1.21      deraadt   839:                if (n->port == port && n->proto == proto)
1.5       deraadt   840:                        return (n->rpcname);
                    841:        return (NULL);
                    842: }
                    843:
1.1       deraadt   844: /*
                    845:  * Pretty print an Internet address (net address + port).
                    846:  * If the nflag was specified, use numbers instead of names.
                    847:  */
                    848: void
1.140     claudio   849: inetprint(struct in_addr *in, in_port_t port, const char *proto, int local)
1.1       deraadt   850: {
                    851:        struct servent *sp = 0;
1.5       deraadt   852:        char line[80], *cp, *nam;
1.1       deraadt   853:        int width;
                    854:
1.23      deraadt   855:        snprintf(line, sizeof line, "%.*s.", (Aflag && !nflag) ? 12 : 16,
                    856:            inetname(in));
1.4       millert   857:        cp = strchr(line, '\0');
1.1       deraadt   858:        if (!nflag && port)
                    859:                sp = getservbyport((int)port, proto);
                    860:        if (sp || port == 0)
1.23      deraadt   861:                snprintf(cp, line + sizeof line - cp, "%.8s",
                    862:                    sp ? sp->s_name : "*");
1.21      deraadt   863:        else if (local && !nflag && (nam = getrpcportnam(ntohs(port),
                    864:            (strcmp(proto, "tcp") == 0 ? IPPROTO_TCP : IPPROTO_UDP))))
1.23      deraadt   865:                snprintf(cp, line + sizeof line - cp, "%d[%.8s]",
                    866:                    ntohs(port), nam);
1.1       deraadt   867:        else
1.23      deraadt   868:                snprintf(cp, line + sizeof line - cp, "%d", ntohs(port));
1.1       deraadt   869:        width = Aflag ? 18 : 22;
                    870:        printf(" %-*.*s", width, width, line);
                    871: }
                    872:
                    873: /*
                    874:  * Construct an Internet address representation.
                    875:  * If the nflag has been supplied, give
                    876:  * numeric value, otherwise try for symbolic name.
                    877:  */
                    878: char *
1.63      deraadt   879: inetname(struct in_addr *inp)
1.1       deraadt   880: {
1.54      mpech     881:        char *cp;
1.1       deraadt   882:        static char line[50];
                    883:        struct hostent *hp;
1.138     deraadt   884:        static char domain[HOST_NAME_MAX+1];
1.1       deraadt   885:        static int first = 1;
                    886:
                    887:        if (first && !nflag) {
                    888:                first = 0;
1.57      mpech     889:                if (gethostname(domain, sizeof(domain)) == 0 &&
1.4       millert   890:                    (cp = strchr(domain, '.')))
1.60      deraadt   891:                        (void) strlcpy(domain, cp + 1, sizeof domain);
1.1       deraadt   892:                else
1.60      deraadt   893:                        domain[0] = '\0';
1.1       deraadt   894:        }
1.84      deraadt   895:        cp = NULL;
1.1       deraadt   896:        if (!nflag && inp->s_addr != INADDR_ANY) {
1.135     schwarze  897:                hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
                    898:                if (hp) {
                    899:                        if ((cp = strchr(hp->h_name, '.')) &&
                    900:                            !strcmp(cp + 1, domain))
                    901:                                *cp = '\0';
                    902:                        cp = hp->h_name;
1.1       deraadt   903:                }
                    904:        }
                    905:        if (inp->s_addr == INADDR_ANY)
1.23      deraadt   906:                snprintf(line, sizeof line, "*");
1.1       deraadt   907:        else if (cp)
1.23      deraadt   908:                snprintf(line, sizeof line, "%s", cp);
1.1       deraadt   909:        else {
                    910:                inp->s_addr = ntohl(inp->s_addr);
                    911: #define C(x)   ((x) & 0xff)
1.23      deraadt   912:                snprintf(line, sizeof line, "%u.%u.%u.%u",
                    913:                    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
                    914:                    C(inp->s_addr >> 8), C(inp->s_addr));
1.1       deraadt   915:        }
                    916:        return (line);
1.10      angelos   917: }
                    918:
                    919: /*
                    920:  * Dump AH statistics structure.
                    921:  */
                    922: void
1.103     deraadt   923: ah_stats(char *name)
1.10      angelos   924: {
1.55      mickey    925:        struct ahstat ahstat;
1.133     guenther  926:        int mib[] = { CTL_NET, PF_INET, IPPROTO_AH, AHCTL_STATS };
1.103     deraadt   927:        size_t len = sizeof(ahstat);
1.10      angelos   928:
1.103     deraadt   929:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    930:            &ahstat, &len, NULL, 0) == -1) {
                    931:                if (errno != ENOPROTOOPT)
1.124     guenther  932:                        warn("%s", name);
1.55      mickey    933:                return;
1.103     deraadt   934:        }
                    935:
1.55      mickey    936:        printf("%s:\n", name);
1.10      angelos   937: #define p(f, m) if (ahstat.f || sflag <= 1) \
1.60      deraadt   938:        printf(m, ahstat.f, plural(ahstat.f))
1.38      itojun    939: #define p1(f, m) if (ahstat.f || sflag <= 1) \
1.60      deraadt   940:        printf(m, ahstat.f)
1.10      angelos   941:
1.38      itojun    942:        p1(ahs_input, "\t%u input AH packets\n");
                    943:        p1(ahs_output, "\t%u output AH packets\n");
1.55      mickey    944:        p(ahs_nopf, "\t%u packet%s from unsupported protocol families\n");
                    945:        p(ahs_hdrops, "\t%u packet%s shorter than header shows\n");
                    946:        p(ahs_pdrops, "\t%u packet%s dropped due to policy\n");
                    947:        p(ahs_notdb, "\t%u packet%s for which no TDB was found\n");
                    948:        p(ahs_badkcr, "\t%u input packet%s that failed to be processed\n");
                    949:        p(ahs_badauth, "\t%u packet%s that failed verification received\n");
                    950:        p(ahs_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
                    951:        p(ahs_qfull, "\t%u packet%s were dropped due to full output queue\n");
                    952:        p(ahs_wrap, "\t%u packet%s where counter wrapping was detected\n");
                    953:        p(ahs_replay, "\t%u possibly replayed packet%s received\n");
                    954:        p(ahs_badauthl, "\t%u packet%s with bad authenticator length received\n");
1.62      jsyn      955:        p(ahs_invalid, "\t%u packet%s attempted to use an invalid TDB\n");
1.25      niklas    956:        p(ahs_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.46      angelos   957:        p(ahs_crypto, "\t%u packet%s that failed crypto processing\n");
1.154     bluhm     958:        p(ahs_outfail, "\t%u output packet%s could not be sent\n");
1.148     krw       959:        p(ahs_ibytes, "\t%llu input byte%s\n");
                    960:        p(ahs_obytes, "\t%llu output byte%s\n");
1.20      angelos   961:
1.34      angelos   962: #undef p
1.38      itojun    963: #undef p1
1.34      angelos   964: }
                    965:
                    966: /*
                    967:  * Dump etherip statistics structure.
                    968:  */
                    969: void
1.103     deraadt   970: etherip_stats(char *name)
1.34      angelos   971: {
1.55      mickey    972:        struct etheripstat etheripstat;
1.133     guenther  973:        int mib[] = { CTL_NET, PF_INET, IPPROTO_ETHERIP, ETHERIPCTL_STATS };
1.103     deraadt   974:        size_t len = sizeof(etheripstat);
1.34      angelos   975:
1.103     deraadt   976:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                    977:            &etheripstat, &len, NULL, 0) == -1) {
                    978:                if (errno != ENOPROTOOPT)
1.124     guenther  979:                        warn("%s", name);
1.55      mickey    980:                return;
1.103     deraadt   981:        }
                    982:
1.55      mickey    983:        printf("%s:\n", name);
1.34      angelos   984: #define p(f, m) if (etheripstat.f || sflag <= 1) \
1.60      deraadt   985:        printf(m, etheripstat.f, plural(etheripstat.f))
1.34      angelos   986:
1.156     jca       987:        p(etherips_hdrops, "\t%llu packet%s shorter than header shows\n");
                    988:        p(etherips_qfull, "\t%llu packet%s were dropped due to full output queue\n");
                    989:        p(etherips_noifdrops, "\t%llu packet%s were dropped because of no interface/bridge information\n");
                    990:        p(etherips_pdrops, "\t%llu packet%s dropped due to policy\n");
                    991:        p(etherips_adrops, "\t%llu packet%s dropped for other reasons\n");
                    992:        p(etherips_ipackets, "\t%llu input ethernet-in-IP packet%s\n");
                    993:        p(etherips_opackets, "\t%llu output ethernet-in-IP packet%s\n");
                    994:        p(etherips_ibytes, "\t%llu input byte%s\n");
                    995:        p(etherips_obytes, "\t%llu output byte%s\n");
1.10      angelos   996: #undef p
                    997: }
                    998:
                    999: /*
                   1000:  * Dump ESP statistics structure.
                   1001:  */
                   1002: void
1.103     deraadt  1003: esp_stats(char *name)
1.10      angelos  1004: {
1.55      mickey   1005:        struct espstat espstat;
1.133     guenther 1006:        int mib[] = { CTL_NET, PF_INET, IPPROTO_ESP, ESPCTL_STATS };
1.103     deraadt  1007:        size_t len = sizeof(espstat);
1.10      angelos  1008:
1.103     deraadt  1009:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                   1010:            &espstat, &len, NULL, 0) == -1) {
                   1011:                if (errno != ENOPROTOOPT)
1.124     guenther 1012:                        warn("%s", name);
1.55      mickey   1013:                return;
1.103     deraadt  1014:        }
                   1015:
1.55      mickey   1016:        printf("%s:\n", name);
1.10      angelos  1017: #define p(f, m) if (espstat.f || sflag <= 1) \
1.60      deraadt  1018:        printf(m, espstat.f, plural(espstat.f))
1.10      angelos  1019:
1.38      itojun   1020:        p(esps_input, "\t%u input ESP packet%s\n");
                   1021:        p(esps_output, "\t%u output ESP packet%s\n");
1.55      mickey   1022:        p(esps_nopf, "\t%u packet%s from unsupported protocol families\n");
                   1023:        p(esps_hdrops, "\t%u packet%s shorter than header shows\n");
                   1024:        p(esps_pdrops, "\t%u packet%s dropped due to policy\n");
                   1025:        p(esps_notdb, "\t%u packet%s for which no TDB was found\n");
                   1026:        p(esps_badkcr, "\t%u input packet%s that failed to be processed\n");
                   1027:        p(esps_badenc, "\t%u packet%s with bad encryption received\n");
                   1028:        p(esps_badauth, "\t%u packet%s that failed verification received\n");
                   1029:        p(esps_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
                   1030:        p(esps_qfull, "\t%u packet%s were dropped due to full output queue\n");
                   1031:        p(esps_wrap, "\t%u packet%s where counter wrapping was detected\n");
                   1032:        p(esps_replay, "\t%u possibly replayed packet%s received\n");
                   1033:        p(esps_badilen, "\t%u packet%s with bad payload size or padding received\n");
1.62      jsyn     1034:        p(esps_invalid, "\t%u packet%s attempted to use an invalid TDB\n");
1.25      niklas   1035:        p(esps_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.46      angelos  1036:        p(esps_crypto, "\t%u packet%s that failed crypto processing\n");
1.154     bluhm    1037:        p(esps_outfail, "\t%u output packet%s could not be sent\n");
1.74      markus   1038:        p(esps_udpencin, "\t%u input UDP encapsulated ESP packet%s\n");
                   1039:        p(esps_udpencout, "\t%u output UDP encapsulated ESP packet%s\n");
                   1040:        p(esps_udpinval, "\t%u UDP packet%s for non-encapsulating TDB received\n");
1.151     vgross   1041:        p(esps_udpneeded, "\t%u raw ESP packet%s for encapsulating TDB received\n");
1.148     krw      1042:        p(esps_ibytes, "\t%llu input byte%s\n");
                   1043:        p(esps_obytes, "\t%llu output byte%s\n");
1.10      angelos  1044:
                   1045: #undef p
                   1046: }
                   1047:
                   1048: /*
1.67      deraadt  1049:  * Dump IP-in-IP statistics structure.
1.10      angelos  1050:  */
                   1051: void
1.103     deraadt  1052: ipip_stats(char *name)
1.10      angelos  1053: {
1.55      mickey   1054:        struct ipipstat ipipstat;
1.133     guenther 1055:        int mib[] = { CTL_NET, PF_INET, IPPROTO_IPIP, IPIPCTL_STATS };
1.103     deraadt  1056:        size_t len = sizeof(ipipstat);
1.10      angelos  1057:
1.103     deraadt  1058:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                   1059:            &ipipstat, &len, NULL, 0) == -1) {
                   1060:                if (errno != ENOPROTOOPT)
1.124     guenther 1061:                        warn("%s", name);
1.55      mickey   1062:                return;
1.103     deraadt  1063:        }
                   1064:
1.55      mickey   1065:        printf("%s:\n", name);
1.45      angelos  1066: #define p(f, m) if (ipipstat.f || sflag <= 1) \
1.60      deraadt  1067:        printf(m, ipipstat.f, plural(ipipstat.f))
1.10      angelos  1068:
1.155     jca      1069:        p(ipips_ipackets, "\t%llu total input packet%s\n");
                   1070:        p(ipips_opackets, "\t%llu total output packet%s\n");
                   1071:        p(ipips_hdrops, "\t%llu packet%s shorter than header shows\n");
                   1072:        p(ipips_pdrops, "\t%llu packet%s dropped due to policy\n");
                   1073:        p(ipips_spoof, "\t%llu packet%s with possibly spoofed local addresses\n");
                   1074:        p(ipips_qfull, "\t%llu packet%s were dropped due to full output queue\n");
1.148     krw      1075:        p(ipips_ibytes, "\t%llu input byte%s\n");
                   1076:        p(ipips_obytes, "\t%llu output byte%s\n");
1.155     jca      1077:        p(ipips_family, "\t%llu protocol family mismatche%s\n");
                   1078:        p(ipips_unspec, "\t%llu attempt%s to use tunnel with unspecified endpoint(s)\n");
1.69      mcbride  1079: #undef p
                   1080: }
                   1081:
1.84      deraadt  1082: /*
1.69      mcbride  1083:  * Dump CARP statistics structure.
                   1084:  */
                   1085: void
1.103     deraadt  1086: carp_stats(char *name)
1.69      mcbride  1087: {
                   1088:        struct carpstats carpstat;
1.133     guenther 1089:        int mib[] = { CTL_NET, PF_INET, IPPROTO_CARP, CARPCTL_STATS };
1.103     deraadt  1090:        size_t len = sizeof(carpstat);
1.69      mcbride  1091:
1.103     deraadt  1092:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                   1093:            &carpstat, &len, NULL, 0) == -1) {
                   1094:                if (errno != ENOPROTOOPT)
1.124     guenther 1095:                        warn("%s", name);
1.69      mcbride  1096:                return;
1.103     deraadt  1097:        }
                   1098:
1.69      mcbride  1099:        printf("%s:\n", name);
                   1100: #define p(f, m) if (carpstat.f || sflag <= 1) \
                   1101:        printf(m, carpstat.f, plural(carpstat.f))
                   1102: #define p2(f, m) if (carpstat.f || sflag <= 1) \
                   1103:        printf(m, carpstat.f)
                   1104:
1.87      mcbride  1105:        p(carps_ipackets, "\t%llu packet%s received (IPv4)\n");
                   1106:        p(carps_ipackets6, "\t%llu packet%s received (IPv6)\n");
                   1107:        p(carps_badif, "\t\t%llu packet%s discarded for bad interface\n");
1.89      mcbride  1108:        p(carps_badttl, "\t\t%llu packet%s discarded for wrong TTL\n");
1.87      mcbride  1109:        p(carps_hdrops, "\t\t%llu packet%s shorter than header\n");
                   1110:        p(carps_badsum, "\t\t%llu discarded for bad checksum%s\n");
                   1111:        p(carps_badver, "\t\t%llu discarded packet%s with a bad version\n");
                   1112:        p2(carps_badlen, "\t\t%llu discarded because packet too short\n");
                   1113:        p2(carps_badauth, "\t\t%llu discarded for bad authentication\n");
1.106     mpf      1114:        p2(carps_badvhid, "\t\t%llu discarded for unknown vhid\n");
1.87      mcbride  1115:        p2(carps_badaddrs, "\t\t%llu discarded because of a bad address list\n");
                   1116:        p(carps_opackets, "\t%llu packet%s sent (IPv4)\n");
                   1117:        p(carps_opackets6, "\t%llu packet%s sent (IPv6)\n");
1.89      mcbride  1118:        p2(carps_onomem, "\t\t%llu send failed due to mbuf memory error\n");
1.101     joel     1119:        p(carps_preempt, "\t%llu transition%s to master\n");
1.51      jjbg     1120: #undef p
1.70      mcbride  1121: #undef p2
1.51      jjbg     1122: }
                   1123:
1.94      deraadt  1124: /*
1.75      mcbride  1125:  * Dump pfsync statistics structure.
                   1126:  */
                   1127: void
1.103     deraadt  1128: pfsync_stats(char *name)
1.75      mcbride  1129: {
                   1130:        struct pfsyncstats pfsyncstat;
1.133     guenther 1131:        int mib[] = { CTL_NET, PF_INET, IPPROTO_PFSYNC, PFSYNCCTL_STATS };
1.103     deraadt  1132:        size_t len = sizeof(pfsyncstat);
1.75      mcbride  1133:
1.103     deraadt  1134:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                   1135:            &pfsyncstat, &len, NULL, 0) == -1) {
                   1136:                if (errno != ENOPROTOOPT)
1.124     guenther 1137:                        warn("%s", name);
1.75      mcbride  1138:                return;
1.103     deraadt  1139:        }
                   1140:
1.75      mcbride  1141:        printf("%s:\n", name);
                   1142: #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
                   1143:        printf(m, pfsyncstat.f, plural(pfsyncstat.f))
                   1144: #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
                   1145:        printf(m, pfsyncstat.f)
                   1146:
1.86      mcbride  1147:        p(pfsyncs_ipackets, "\t%llu packet%s received (IPv4)\n");
                   1148:        p(pfsyncs_ipackets6, "\t%llu packet%s received (IPv6)\n");
                   1149:        p(pfsyncs_badif, "\t\t%llu packet%s discarded for bad interface\n");
                   1150:        p(pfsyncs_badttl, "\t\t%llu packet%s discarded for bad ttl\n");
                   1151:        p(pfsyncs_hdrops, "\t\t%llu packet%s shorter than header\n");
                   1152:        p(pfsyncs_badver, "\t\t%llu packet%s discarded for bad version\n");
                   1153:        p(pfsyncs_badauth, "\t\t%llu packet%s discarded for bad HMAC\n");
                   1154:        p(pfsyncs_badact,"\t\t%llu packet%s discarded for bad action\n");
                   1155:        p(pfsyncs_badlen, "\t\t%llu packet%s discarded for short packet\n");
                   1156:        p(pfsyncs_badval, "\t\t%llu state%s discarded for bad values\n");
                   1157:        p(pfsyncs_stale, "\t\t%llu stale state%s\n");
                   1158:        p(pfsyncs_badstate, "\t\t%llu failed state lookup/insert%s\n");
                   1159:        p(pfsyncs_opackets, "\t%llu packet%s sent (IPv4)\n");
                   1160:        p(pfsyncs_opackets6, "\t%llu packet%s sent (IPv6)\n");
                   1161:        p2(pfsyncs_onomem, "\t\t%llu send failed due to mbuf memory error\n");
                   1162:        p2(pfsyncs_oerrors, "\t\t%llu send error\n");
1.107     gollo    1163: #undef p
                   1164: #undef p2
                   1165: }
                   1166:
                   1167: /*
                   1168:  * Dump pflow statistics structure.
                   1169:  */
                   1170: void
                   1171: pflow_stats(char *name)
                   1172: {
                   1173:        struct pflowstats flowstats;
                   1174:        int mib[] = { CTL_NET, PF_PFLOW, NET_PFLOW_STATS };
                   1175:        size_t len = sizeof(struct pflowstats);
                   1176:
                   1177:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &flowstats, &len,
                   1178:            NULL, 0) == -1) {
                   1179:                if (errno != ENOPROTOOPT)
1.124     guenther 1180:                        warn("%s", name);
1.107     gollo    1181:                return;
                   1182:        }
                   1183:
                   1184:        printf("%s:\n", name);
                   1185: #define p(f, m) if (flowstats.f || sflag <= 1) \
                   1186:        printf(m, flowstats.f, plural(flowstats.f))
                   1187: #define p2(f, m) if (flowstats.f || sflag <= 1) \
                   1188:        printf(m, flowstats.f)
                   1189:
                   1190:        p(pflow_flows, "\t%llu flow%s sent\n");
                   1191:        p(pflow_packets, "\t%llu packet%s sent\n");
                   1192:        p2(pflow_onomem, "\t\t%llu send failed due to mbuf memory error\n");
                   1193:        p2(pflow_oerrors, "\t\t%llu send error\n");
1.75      mcbride  1194: #undef p
                   1195: #undef p2
                   1196: }
                   1197:
1.51      jjbg     1198: /*
                   1199:  * Dump IPCOMP statistics structure.
                   1200:  */
                   1201: void
1.103     deraadt  1202: ipcomp_stats(char *name)
1.51      jjbg     1203: {
                   1204:        struct ipcompstat ipcompstat;
1.133     guenther 1205:        int mib[] = { CTL_NET, PF_INET, IPPROTO_IPCOMP, IPCOMPCTL_STATS };
1.103     deraadt  1206:        size_t len = sizeof(ipcompstat);
1.51      jjbg     1207:
1.103     deraadt  1208:        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
                   1209:            &ipcompstat, &len, NULL, 0) == -1) {
                   1210:                if (errno != ENOPROTOOPT)
1.124     guenther 1211:                        warn("%s", name);
1.51      jjbg     1212:                return;
1.103     deraadt  1213:        }
                   1214:
1.51      jjbg     1215:        printf("%s:\n", name);
                   1216: #define p(f, m) if (ipcompstat.f || sflag <= 1) \
1.60      deraadt  1217:        printf(m, ipcompstat.f, plural(ipcompstat.f))
1.51      jjbg     1218:
                   1219:        p(ipcomps_input, "\t%u input IPCOMP packet%s\n");
                   1220:        p(ipcomps_output, "\t%u output IPCOMP packet%s\n");
                   1221:        p(ipcomps_nopf, "\t%u packet%s from unsupported protocol families\n");
                   1222:        p(ipcomps_hdrops, "\t%u packet%s shorter than header shows\n");
                   1223:        p(ipcomps_pdrops, "\t%u packet%s dropped due to policy\n");
                   1224:        p(ipcomps_notdb, "\t%u packet%s for which no TDB was found\n");
                   1225:        p(ipcomps_badkcr, "\t%u input packet%s that failed to be processed\n");
1.55      mickey   1226:        p(ipcomps_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
1.51      jjbg     1227:        p(ipcomps_qfull, "\t%u packet%s were dropped due to full output queue\n");
                   1228:        p(ipcomps_wrap, "\t%u packet%s where counter wrapping was detected\n");
1.62      jsyn     1229:        p(ipcomps_invalid, "\t%u packet%s attempted to use an invalid TDB\n");
1.51      jjbg     1230:        p(ipcomps_toobig, "\t%u packet%s got larger than max IP packet size\n");
                   1231:        p(ipcomps_crypto, "\t%u packet%s that failed (de)compression processing\n");
1.154     bluhm    1232:        p(ipcomps_outfail, "\t%u output packet%s could not be sent\n");
1.64      jason    1233:        p(ipcomps_minlen, "\t%u packet%s less than minimum compression length\n");
1.148     krw      1234:        p(ipcomps_ibytes, "\t%llu input byte%s\n");
                   1235:        p(ipcomps_obytes, "\t%llu output byte%s\n");
1.51      jjbg     1236:
1.10      angelos  1237: #undef p
1.1       deraadt  1238: }
1.96      markus   1239:
                   1240: /*
1.114     bluhm    1241:  * Dump the contents of a socket structure
                   1242:  */
                   1243: void
                   1244: socket_dump(u_long off)
                   1245: {
                   1246:        struct socket so;
                   1247:
                   1248:        if (off == 0)
                   1249:                return;
                   1250:        kread(off, &so, sizeof(so));
                   1251:
                   1252: #define        p(fmt, v, sep) printf(#v " " fmt sep, so.v);
1.140     claudio  1253: #define        pp(fmt, v, sep) printf(#v " " fmt sep, so.v);
                   1254:        printf("socket %#lx\n ", off);
1.124     guenther 1255:        p("%#.4x", so_type, "\n ");
                   1256:        p("%#.4x", so_options, "\n ");
1.114     bluhm    1257:        p("%d", so_linger, "\n ");
1.124     guenther 1258:        p("%#.4x", so_state, "\n ");
1.122     deraadt  1259:        pp("%p", so_pcb, ", ");
                   1260:        pp("%p", so_proto, ", ");
                   1261:        pp("%p", so_head, "\n ");
1.114     bluhm    1262:        p("%d", so_q0len, ", ");
                   1263:        p("%d", so_qlen, ", ");
                   1264:        p("%d", so_qlimit, "\n ");
                   1265:        p("%d", so_timeo, "\n ");
                   1266:        p("%u", so_error, "\n ");
                   1267:        p("%d", so_pgid, ", ");
                   1268:        p("%u", so_siguid, ", ");
                   1269:        p("%u", so_sigeuid, "\n ");
                   1270:        p("%lu", so_oobmark, "\n ");
1.137     bluhm    1271:        if (so.so_sp)
                   1272:                sosplice_dump((u_long)so.so_sp);
1.114     bluhm    1273:        sockbuf_dump(&so.so_rcv, "so_rcv");
                   1274:        sockbuf_dump(&so.so_snd, "so_snd");
                   1275:        p("%u", so_euid, ", ");
                   1276:        p("%u", so_ruid, ", ");
                   1277:        p("%u", so_egid, ", ");
                   1278:        p("%u", so_rgid, "\n ");
                   1279:        p("%d", so_cpid, "\n");
                   1280: #undef p
1.122     deraadt  1281: #undef pp
1.114     bluhm    1282:
                   1283:        protosw_dump((u_long)so.so_proto, (u_long)so.so_pcb);
1.137     bluhm    1284: }
                   1285:
                   1286: /*
                   1287:  * Dump the contents of a struct sosplice
                   1288:  */
                   1289: void
                   1290: sosplice_dump(u_long off)
                   1291: {
                   1292:        struct sosplice ssp;
                   1293:
                   1294:        if (off == 0)
                   1295:                return;
                   1296:        kread(off, &ssp, sizeof(ssp));
                   1297:
                   1298: #define        p(fmt, v, sep) printf(#v " " fmt sep, ssp.v);
                   1299: #define        pll(fmt, v, sep) printf(#v " " fmt sep, (long long) ssp.v);
1.140     claudio  1300: #define        pp(fmt, v, sep) printf(#v " " fmt sep, ssp.v);
1.137     bluhm    1301:        pp("%p", ssp_socket, ", ");
                   1302:        pp("%p", ssp_soback, "\n ");
                   1303:        p("%lld", ssp_len, ", ");
                   1304:        p("%lld", ssp_max, ", ");
                   1305:        pll("%lld", ssp_idletv.tv_sec, ", ");
                   1306:        p("%ld", ssp_idletv.tv_usec, "\n ");
                   1307: #undef p
                   1308: #undef pll
                   1309: #undef pp
1.114     bluhm    1310: }
                   1311:
                   1312: /*
                   1313:  * Dump the contents of a socket buffer
                   1314:  */
                   1315: void
                   1316: sockbuf_dump(struct sockbuf *sb, const char *name)
                   1317: {
                   1318: #define        p(fmt, v, sep) printf(#v " " fmt sep, sb->v);
                   1319:        printf("%s ", name);
                   1320:        p("%lu", sb_cc, ", ");
                   1321:        p("%lu", sb_datacc, ", ");
                   1322:        p("%lu", sb_hiwat, ", ");
                   1323:        p("%lu", sb_wat, "\n ");
                   1324:        printf("%s ", name);
                   1325:        p("%lu", sb_mbcnt, ", ");
                   1326:        p("%lu", sb_mbmax, ", ");
                   1327:        p("%ld", sb_lowat, "\n ");
                   1328:        printf("%s ", name);
1.124     guenther 1329:        p("%#.8x", sb_flagsintr, ", ");
                   1330:        p("%#.4x", sb_flags, ", ");
1.114     bluhm    1331:        p("%u", sb_timeo, "\n ");
                   1332: #undef p
                   1333: }
                   1334:
                   1335: /*
                   1336:  * Dump the contents of a protosw structure
                   1337:  */
                   1338: void
                   1339: protosw_dump(u_long off, u_long pcb)
                   1340: {
                   1341:        struct protosw proto;
                   1342:
                   1343:        if (off == 0)
                   1344:                return;
                   1345:        kread(off, &proto, sizeof(proto));
                   1346:
                   1347: #define        p(fmt, v, sep) printf(#v " " fmt sep, proto.v);
1.140     claudio  1348: #define        pp(fmt, v, sep) printf(#v " " fmt sep, proto.v);
                   1349:        printf("protosw %#lx\n ", off);
1.124     guenther 1350:        p("%#.4x", pr_type, "\n ");
1.122     deraadt  1351:        pp("%p", pr_domain, "\n ");
1.114     bluhm    1352:        p("%d", pr_protocol, "\n ");
1.124     guenther 1353:        p("%#.4x", pr_flags, "\n");
1.114     bluhm    1354: #undef p
1.122     deraadt  1355: #undef pp
1.114     bluhm    1356:
                   1357:        domain_dump((u_long)proto.pr_domain, pcb, proto.pr_protocol);
                   1358: }
                   1359:
                   1360: /*
                   1361:  * Dump the contents of a domain structure
                   1362:  */
                   1363: void
                   1364: domain_dump(u_long off, u_long pcb, short protocol)
                   1365: {
                   1366:        struct domain dom;
                   1367:        char name[256];
                   1368:
                   1369:        if (off == 0)
                   1370:                return;
                   1371:        kread(off, &dom, sizeof(dom));
                   1372:        kread((u_long)dom.dom_name, name, sizeof(name));
                   1373:
                   1374: #define        p(fmt, v, sep) printf(#v " " fmt sep, dom.v);
1.140     claudio  1375:        printf("domain %#lx\n ", off);
1.114     bluhm    1376:        p("%d", dom_family, "\n ");
1.124     guenther 1377:        printf("dom_name %.*s\n", (int)sizeof(name), name);
1.114     bluhm    1378: #undef p
                   1379: }
                   1380:
                   1381: /*
                   1382:  * Dump the contents of a internet PCB
                   1383:  */
                   1384: void
                   1385: inpcb_dump(u_long off, short protocol, int af)
                   1386: {
                   1387:        struct inpcb inp;
1.152     bluhm    1388:        char faddr[256], laddr[256], raddr[256];
1.114     bluhm    1389:
                   1390:        if (off == 0)
                   1391:                return;
                   1392:        kread(off, &inp, sizeof(inp));
1.140     claudio  1393:
                   1394:        if (vflag)
                   1395:                socket_dump((u_long)inp.inp_socket);
                   1396:
1.114     bluhm    1397:        switch (af) {
                   1398:        case AF_INET:
                   1399:                inet_ntop(af, &inp.inp_faddr, faddr, sizeof(faddr));
                   1400:                inet_ntop(af, &inp.inp_laddr, laddr, sizeof(laddr));
1.152     bluhm    1401:                inet_ntop(af, &((struct sockaddr_in *)
                   1402:                    (&inp.inp_route.ro_dst))->sin_addr, raddr, sizeof(raddr));
1.114     bluhm    1403:                break;
                   1404:        case AF_INET6:
                   1405:                inet_ntop(af, &inp.inp_faddr6, faddr, sizeof(faddr));
                   1406:                inet_ntop(af, &inp.inp_laddr6, laddr, sizeof(laddr));
1.152     bluhm    1407:                inet_ntop(af, &inp.inp_route6.ro_dst.sin6_addr,
                   1408:                    raddr, sizeof(raddr));
1.114     bluhm    1409:                break;
                   1410:        default:
                   1411:                faddr[0] = laddr[0] = '\0';
                   1412:        }
                   1413:
                   1414: #define        p(fmt, v, sep) printf(#v " " fmt sep, inp.v);
1.140     claudio  1415: #define        pp(fmt, v, sep) printf(#v " " fmt sep, inp.v);
                   1416:        printf("inpcb %#lx\n ", off);
1.122     deraadt  1417:        pp("%p", inp_table, "\n ");
1.114     bluhm    1418:        printf("inp_faddru %s, inp_laddru %s\n ", faddr, laddr);
                   1419:        HTONS(inp.inp_fport);
                   1420:        HTONS(inp.inp_lport);
                   1421:        p("%u", inp_fport, ", ");
                   1422:        p("%u", inp_lport, "\n ");
1.122     deraadt  1423:        pp("%p", inp_socket, ", ");
                   1424:        pp("%p", inp_ppcb, "\n ");
1.152     bluhm    1425:        pp("%p", inp_route.ro_rt, ", ");
                   1426:        printf("ro_dst %s\n ", raddr);
1.124     guenther 1427:        p("%#.8x", inp_flags, "\n ");
1.114     bluhm    1428:        p("%d", inp_hops, "\n ");
                   1429:        p("%u", inp_seclevel[0], ", ");
                   1430:        p("%u", inp_seclevel[1], ", ");
                   1431:        p("%u", inp_seclevel[2], ", ");
                   1432:        p("%u", inp_seclevel[3], "\n ");
                   1433:        p("%u", inp_ip_minttl, "\n ");
1.125     deraadt  1434:        p("%d", inp_cksum6, "\n ");
1.122     deraadt  1435:        pp("%p", inp_icmp6filt, "\n ");
                   1436:        pp("%p", inp_pf_sk, "\n ");
1.114     bluhm    1437:        p("%u", inp_rtableid, "\n ");
                   1438:        p("%d", inp_pipex, "\n");
                   1439: #undef p
1.122     deraadt  1440: #undef pp
1.114     bluhm    1441:
                   1442:        switch (protocol) {
                   1443:        case IPPROTO_TCP:
                   1444:                tcpcb_dump((u_long)inp.inp_ppcb);
                   1445:                break;
                   1446:        }
                   1447: }
                   1448:
                   1449: /*
                   1450:  * Dump the contents of a TCP PCB
1.96      markus   1451:  */
                   1452: void
1.114     bluhm    1453: tcpcb_dump(u_long off)
1.96      markus   1454: {
                   1455:        struct tcpcb tcpcb;
                   1456:
                   1457:        if (off == 0)
                   1458:                return;
                   1459:        kread(off, (char *)&tcpcb, sizeof (tcpcb));
                   1460:
                   1461: #define        p(fmt, v, sep) printf(#v " " fmt sep, tcpcb.v);
1.140     claudio  1462: #define        pp(fmt, v, sep) printf(#v " " fmt sep, tcpcb.v);
                   1463:        printf("tcpcb %#lx\n ", off);
1.122     deraadt  1464:        pp("%p", t_inpcb, "\n ");
1.96      markus   1465:        p("%d", t_state, "");
1.114     bluhm    1466:        if (tcpcb.t_state >= 0 && tcpcb.t_state < TCP_NSTATES)
1.96      markus   1467:                printf(" (%s)", tcpstates[tcpcb.t_state]);
1.114     bluhm    1468:        printf("\n ");
1.96      markus   1469:        p("%d", t_rxtshift, ", ");
                   1470:        p("%d", t_rxtcur, ", ");
1.114     bluhm    1471:        p("%d", t_dupacks, "\n ");
1.96      markus   1472:        p("%u", t_maxseg, ", ");
                   1473:        p("%u", t_maxopd, ", ");
1.114     bluhm    1474:        p("%u", t_peermss, "\n ");
1.96      markus   1475:        p("0x%x", t_flags, ", ");
1.114     bluhm    1476:        p("%u", t_force, "\n ");
                   1477:        p("%u", iss, "\n ");
1.96      markus   1478:        p("%u", snd_una, ", ");
                   1479:        p("%u", snd_nxt, ", ");
1.114     bluhm    1480:        p("%u", snd_up, "\n ");
1.96      markus   1481:        p("%u", snd_wl1, ", ");
                   1482:        p("%u", snd_wl2, ", ");
1.114     bluhm    1483:        p("%lu", snd_wnd, "\n ");
1.96      markus   1484:        p("%d", sack_enable, ", ");
                   1485:        p("%d", snd_numholes, ", ");
                   1486:        p("%u", snd_fack, ", ");
1.114     bluhm    1487:        p("%lu",snd_awnd, "\n ");
1.96      markus   1488:        p("%u", retran_data, ", ");
1.114     bluhm    1489:        p("%u", snd_last, "\n ");
                   1490:        p("%u", irs, "\n ");
1.96      markus   1491:        p("%u", rcv_nxt, ", ");
                   1492:        p("%u", rcv_up, ", ");
1.114     bluhm    1493:        p("%lu", rcv_wnd, "\n ");
                   1494:        p("%u", rcv_lastsack, "\n ");
                   1495:        p("%d", rcv_numsacks, "\n ");
1.96      markus   1496:        p("%u", rcv_adv, ", ");
1.114     bluhm    1497:        p("%u", snd_max, "\n ");
1.96      markus   1498:        p("%lu", snd_cwnd, ", ");
                   1499:        p("%lu", snd_ssthresh, ", ");
1.114     bluhm    1500:        p("%lu", max_sndwnd, "\n ");
1.96      markus   1501:        p("%u", t_rcvtime, ", ");
                   1502:        p("%u", t_rtttime, ", ");
1.114     bluhm    1503:        p("%u", t_rtseq, "\n ");
1.96      markus   1504:        p("%u", t_srtt, ", ");
                   1505:        p("%u", t_rttvar, ", ");
1.114     bluhm    1506:        p("%u", t_rttmin, "\n ");
1.96      markus   1507:        p("%u", t_oobflags, ", ");
1.114     bluhm    1508:        p("%u", t_iobc, "\n ");
                   1509:        p("%u", t_softerror, "\n ");
1.96      markus   1510:        p("%u", snd_scale, ", ");
                   1511:        p("%u", rcv_scale, ", ");
                   1512:        p("%u", request_r_scale, ", ");
1.114     bluhm    1513:        p("%u", requested_s_scale, "\n ");
1.96      markus   1514:        p("%u", ts_recent, ", ");
1.114     bluhm    1515:        p("%u", ts_recent_age, "\n ");
                   1516:        p("%u", last_ack_sent, "\n ");
1.98      markus   1517:        HTONS(tcpcb.t_pmtud_ip_len);
                   1518:        HTONS(tcpcb.t_pmtud_nextmtu);
                   1519:        p("%u", t_pmtud_mss_acked, ", ");
1.114     bluhm    1520:        p("%u", t_pmtud_mtu_sent, "\n ");
1.98      markus   1521:        p("%u", t_pmtud_nextmtu, ", ");
                   1522:        p("%u", t_pmtud_ip_len, ", ");
1.114     bluhm    1523:        p("%u", t_pmtud_ip_hl, "\n ");
                   1524:        p("%u", t_pmtud_th_seq, "\n ");
1.96      markus   1525:        p("%u", pf, "\n");
                   1526: #undef p
1.122     deraadt  1527: #undef pp
1.98      markus   1528: }