[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.175

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