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

1.41    ! angelos     1: /*     $OpenBSD: inet.c,v 1.40 1999/12/09 08:55:16 angelos 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "from: @(#)inet.c       8.4 (Berkeley) 4/20/94";
                     40: #else
1.41    ! angelos    41: static char *rcsid = "$OpenBSD: inet.c,v 1.40 1999/12/09 08:55:16 angelos Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/queue.h>
                     47: #include <sys/socket.h>
                     48: #include <sys/socketvar.h>
                     49: #include <sys/mbuf.h>
                     50: #include <sys/protosw.h>
                     51:
                     52: #include <net/route.h>
                     53: #include <netinet/in.h>
                     54: #include <netinet/in_systm.h>
                     55: #include <netinet/ip.h>
                     56: #include <netinet/in_pcb.h>
                     57: #include <netinet/ip_icmp.h>
                     58: #include <netinet/icmp_var.h>
                     59: #include <netinet/igmp_var.h>
                     60: #include <netinet/ip_var.h>
                     61: #include <netinet/tcp.h>
                     62: #include <netinet/tcpip.h>
                     63: #include <netinet/tcp_seq.h>
                     64: #define TCPSTATES
                     65: #include <netinet/tcp_fsm.h>
                     66: #include <netinet/tcp_timer.h>
                     67: #include <netinet/tcp_var.h>
                     68: #include <netinet/tcp_debug.h>
                     69: #include <netinet/udp.h>
                     70: #include <netinet/udp_var.h>
1.30      angelos    71: #include <netinet/ip_ipsp.h>
1.10      angelos    72: #include <netinet/ip_ah.h>
                     73: #include <netinet/ip_esp.h>
                     74: #include <netinet/ip_ip4.h>
1.34      angelos    75: #include <netinet/ip_ether.h>
1.1       deraadt    76:
                     77: #include <arpa/inet.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.1       deraadt    84: #include "netstat.h"
                     85:
1.5       deraadt    86: #include <rpc/rpc.h>
                     87: #include <rpc/pmap_prot.h>
                     88: #include <rpc/pmap_clnt.h>
                     89:
1.1       deraadt    90: struct inpcb inpcb;
                     91: struct tcpcb tcpcb;
                     92: struct socket sockb;
                     93:
                     94: char   *inetname __P((struct in_addr *));
1.5       deraadt    95: void   inetprint __P((struct in_addr *, int, char *, int));
1.38      itojun     96: #ifdef INET6
                     97: char   *inet6name __P((struct in6_addr *));
                     98: void   inet6print __P((struct in6_addr *, int, char *, int));
                     99: #endif
1.1       deraadt   100:
                    101: /*
                    102:  * Print a summary of connections related to an Internet
                    103:  * protocol.  For TCP, also give state of connection.
                    104:  * Listening processes (aflag) are suppressed unless the
                    105:  * -a (all) flag is specified.
                    106:  */
                    107: void
                    108: protopr(off, name)
                    109:        u_long off;
                    110:        char *name;
                    111: {
                    112:        struct inpcbtable table;
                    113:        register struct inpcb *head, *next, *prev;
                    114:        struct inpcb inpcb;
                    115:        int istcp;
                    116:        static int first = 1;
1.38      itojun    117:        char *name0;
                    118:        char namebuf[20];
1.1       deraadt   119:
1.38      itojun    120:        name0 = name;
1.1       deraadt   121:        if (off == 0)
                    122:                return;
                    123:        istcp = strcmp(name, "tcp") == 0;
                    124:        kread(off, (char *)&table, sizeof table);
                    125:        prev = head =
                    126:            (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
                    127:        next = table.inpt_queue.cqh_first;
                    128:
                    129:        while (next != head) {
                    130:                kread((u_long)next, (char *)&inpcb, sizeof inpcb);
                    131:                if (inpcb.inp_queue.cqe_prev != prev) {
                    132:                        printf("???\n");
                    133:                        break;
                    134:                }
                    135:                prev = next;
                    136:                next = inpcb.inp_queue.cqe_next;
                    137:
                    138:                if (!aflag &&
                    139:                    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
                    140:                        continue;
                    141:                kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
                    142:                if (istcp) {
                    143:                        kread((u_long)inpcb.inp_ppcb,
                    144:                            (char *)&tcpcb, sizeof (tcpcb));
                    145:                }
                    146:                if (first) {
                    147:                        printf("Active Internet connections");
                    148:                        if (aflag)
                    149:                                printf(" (including servers)");
                    150:                        putchar('\n');
                    151:                        if (Aflag)
1.29      deraadt   152:                                printf("%-*.*s %-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n",
1.18      millert   153:                                    PLEN, PLEN, "PCB", "Proto", "Recv-Q",
1.29      deraadt   154:                                    "Send-Q", "Local Address",
                    155:                                    "Foreign Address", "(state)");
1.18      millert   156:                        else
                    157:                                printf("%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
                    158:                                    "Proto", "Recv-Q", "Send-Q",
                    159:                                    "Local Address", "Foreign Address",
                    160:                                    "(state)");
1.1       deraadt   161:                        first = 0;
                    162:                }
1.38      itojun    163:                if (Aflag) {
1.1       deraadt   164:                        if (istcp)
1.18      millert   165:                                printf("%*p ", PLEN, inpcb.inp_ppcb);
1.1       deraadt   166:                        else
1.18      millert   167:                                printf("%*p ", PLEN, prev);
1.38      itojun    168:                }
                    169: #ifdef INET6
                    170:                if (inpcb.inp_flags & INP_IPV6) {
                    171:                        strcpy(namebuf, name0);
                    172:                        strcat(namebuf, "6");
                    173:                        name = namebuf;
                    174:                } else
                    175:                        name = name0;
                    176: #endif
1.16      millert   177:                printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
1.1       deraadt   178:                        sockb.so_snd.sb_cc);
1.38      itojun    179: #ifdef INET6
                    180:                if (inpcb.inp_flags & INP_IPV6) {
                    181:                        inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport,
                    182:                                name, 1);
                    183:                        inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport,
                    184:                                name, 0);
                    185:                } else
                    186: #endif
                    187:                {
                    188:                        inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
                    189:                                name, 1);
                    190:                        inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
                    191:                                name, 0);
                    192:                }
1.1       deraadt   193:                if (istcp) {
                    194:                        if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
                    195:                                printf(" %d", tcpcb.t_state);
                    196:                        else
                    197:                                printf(" %s", tcpstates[tcpcb.t_state]);
                    198:                }
                    199:                putchar('\n');
                    200:        }
                    201: }
                    202:
                    203: /*
                    204:  * Dump TCP statistics structure.
                    205:  */
                    206: void
                    207: tcp_stats(off, name)
                    208:        u_long off;
                    209:        char *name;
                    210: {
                    211:        struct tcpstat tcpstat;
                    212:
                    213:        if (off == 0)
                    214:                return;
                    215:        printf ("%s:\n", name);
                    216:        kread(off, (char *)&tcpstat, sizeof (tcpstat));
                    217:
                    218: #define        p(f, m) if (tcpstat.f || sflag <= 1) \
                    219:     printf(m, tcpstat.f, plural(tcpstat.f))
1.38      itojun    220: #define        p1(f, m) if (tcpstat.f || sflag <= 1) \
                    221:     printf(m, tcpstat.f)
1.1       deraadt   222: #define        p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
                    223:     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
1.38      itojun    224: #define        p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
                    225:     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
1.1       deraadt   226: #define        p3(f, m) if (tcpstat.f || sflag <= 1) \
                    227:     printf(m, tcpstat.f, plurales(tcpstat.f))
                    228:
1.38      itojun    229:        p(tcps_sndtotal, "\t%u packet%s sent\n");
1.1       deraadt   230:        p2(tcps_sndpack,tcps_sndbyte,
1.38      itojun    231:                "\t\t%u data packet%s (%qd byte%s)\n");
1.1       deraadt   232:        p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
1.38      itojun    233:                "\t\t%u data packet%s (%qd byte%s) retransmitted\n");
1.26      provos    234:        p(tcps_sndrexmitfast, "\t\t%qd fast retransmitted packet%s\n");
1.38      itojun    235:        p2a(tcps_sndacks, tcps_delack,
                    236:                "\t\t%u ack-only packet%s (%u delayed)\n");
                    237:        p(tcps_sndurg, "\t\t%u URG only packet%s\n");
                    238:        p(tcps_sndprobe, "\t\t%u window probe packet%s\n");
                    239:        p(tcps_sndwinup, "\t\t%u window update packet%s\n");
                    240:        p(tcps_sndctrl, "\t\t%u control packet%s\n");
                    241:        p(tcps_rcvtotal, "\t%u packet%s received\n");
                    242:        p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%u ack%s (for %qd byte%s)\n");
                    243:        p(tcps_rcvdupack, "\t\t%u duplicate ack%s\n");
                    244:        p(tcps_rcvacktoomuch, "\t\t%u ack%s for unsent data\n");
1.1       deraadt   245:        p2(tcps_rcvpack, tcps_rcvbyte,
1.38      itojun    246:                "\t\t%u packet%s (%qu byte%s) received in-sequence\n");
1.1       deraadt   247:        p2(tcps_rcvduppack, tcps_rcvdupbyte,
1.38      itojun    248:                "\t\t%u completely duplicate packet%s (%qd byte%s)\n");
                    249:        p(tcps_pawsdrop, "\t\t%u old duplicate packet%s\n");
1.1       deraadt   250:        p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
1.38      itojun    251:                "\t\t%u packet%s with some dup. data (%qd byte%s duped)\n");
1.1       deraadt   252:        p2(tcps_rcvoopack, tcps_rcvoobyte,
1.38      itojun    253:                "\t\t%u out-of-order packet%s (%qd byte%s)\n");
1.1       deraadt   254:        p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
1.38      itojun    255:                "\t\t%u packet%s (%qd byte%s) of data after window\n");
                    256:        p(tcps_rcvwinprobe, "\t\t%u window probe%s\n");
                    257:        p(tcps_rcvwinupd, "\t\t%u window update packet%s\n");
                    258:        p(tcps_rcvafterclose, "\t\t%u packet%s received after close\n");
                    259:        p(tcps_rcvbadsum, "\t\t%u discarded for bad checksum%s\n");
                    260:        p(tcps_rcvbadoff, "\t\t%u discarded for bad header offset field%s\n");
                    261:        p1(tcps_rcvshort, "\t\t%u discarded because packet too short\n");
                    262:        p1(tcps_rcvnosec, "\t\t%u discarded for missing IPSec protection\n");
                    263:        p(tcps_connattempt, "\t%u connection request%s\n");
                    264:        p(tcps_accepts, "\t%u connection accept%s\n");
                    265:        p(tcps_connects, "\t%u connection%s established (including accepts)\n");
1.1       deraadt   266:        p2(tcps_closed, tcps_drops,
1.38      itojun    267:                "\t%u connection%s closed (including %u drop%s)\n");
                    268:        p(tcps_conndrops, "\t%u embryonic connection%s dropped\n");
1.1       deraadt   269:        p2(tcps_rttupdated, tcps_segstimed,
1.38      itojun    270:                "\t%u segment%s updated rtt (of %u attempt%s)\n");
                    271:        p(tcps_rexmttimeo, "\t%u retransmit timeout%s\n");
                    272:        p(tcps_timeoutdrop, "\t\t%u connection%s dropped by rexmit timeout\n");
                    273:        p(tcps_persisttimeo, "\t%u persist timeout%s\n");
                    274:        p(tcps_keeptimeo, "\t%u keepalive timeout%s\n");
                    275:        p(tcps_keepprobe, "\t\t%u keepalive probe%s sent\n");
                    276:        p(tcps_keepdrops, "\t\t%u connection%s dropped by keepalive\n");
                    277:        p(tcps_predack, "\t%u correct ACK header prediction%s\n");
                    278:        p(tcps_preddat, "\t%u correct data packet header prediction%s\n");
                    279:        p3(tcps_pcbhashmiss, "\t%u PCB cache miss%s\n");
                    280:        p(tcps_badsyn, "\t%u SYN packet%s received with same src/dst address/port\n");
1.1       deraadt   281: #undef p
1.38      itojun    282: #undef p1
1.1       deraadt   283: #undef p2
1.38      itojun    284: #undef p2a
1.1       deraadt   285: #undef p3
                    286: }
                    287:
                    288: /*
                    289:  * Dump UDP statistics structure.
                    290:  */
                    291: void
                    292: udp_stats(off, name)
                    293:        u_long off;
                    294:        char *name;
                    295: {
                    296:        struct udpstat udpstat;
                    297:        u_long delivered;
                    298:
                    299:        if (off == 0)
                    300:                return;
                    301:        kread(off, (char *)&udpstat, sizeof (udpstat));
                    302:        printf("%s:\n", name);
                    303: #define        p(f, m) if (udpstat.f || sflag <= 1) \
                    304:     printf(m, udpstat.f, plural(udpstat.f))
1.38      itojun    305: #define        p1(f, m) if (udpstat.f || sflag <= 1) \
                    306:     printf(m, udpstat.f)
1.16      millert   307:        p(udps_ipackets, "\t%lu datagram%s received\n");
1.38      itojun    308:        p1(udps_hdrops, "\t%lu with incomplete header\n");
                    309:        p1(udps_badlen, "\t%lu with bad data length field\n");
                    310:        p1(udps_badsum, "\t%lu with bad checksum\n");
                    311:        p1(udps_nosum, "\t%lu with no checksum\n");
                    312:        p1(udps_noport, "\t%lu dropped due to no socket\n");
1.16      millert   313:        p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
1.38      itojun    314:        p1(udps_nosec, "\t%lu dropped due to missing IPSec protection\n");
                    315:        p1(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
1.1       deraadt   316:        delivered = udpstat.udps_ipackets -
                    317:                    udpstat.udps_hdrops -
                    318:                    udpstat.udps_badlen -
                    319:                    udpstat.udps_badsum -
                    320:                    udpstat.udps_noport -
                    321:                    udpstat.udps_noportbcast -
                    322:                    udpstat.udps_fullsock;
                    323:        if (delivered || sflag <= 1)
1.16      millert   324:                printf("\t%lu delivered\n", delivered);
                    325:        p(udps_opackets, "\t%lu datagram%s output\n");
1.38      itojun    326:        p1(udps_pcbhashmiss, "\t%lu missed PCB cache\n");
1.1       deraadt   327: #undef p
1.38      itojun    328: #undef p1
1.1       deraadt   329: }
                    330:
                    331: /*
                    332:  * Dump IP statistics structure.
                    333:  */
                    334: void
                    335: ip_stats(off, name)
                    336:        u_long off;
                    337:        char *name;
                    338: {
                    339:        struct ipstat ipstat;
                    340:
                    341:        if (off == 0)
                    342:                return;
                    343:        kread(off, (char *)&ipstat, sizeof (ipstat));
                    344:        printf("%s:\n", name);
                    345:
                    346: #define        p(f, m) if (ipstat.f || sflag <= 1) \
                    347:     printf(m, ipstat.f, plural(ipstat.f))
1.38      itojun    348: #define        p1(f, m) if (ipstat.f || sflag <= 1) \
                    349:     printf(m, ipstat.f)
1.1       deraadt   350:
1.16      millert   351:        p(ips_total, "\t%lu total packet%s received\n");
                    352:        p(ips_badsum, "\t%lu bad header checksum%s\n");
1.38      itojun    353:        p1(ips_toosmall, "\t%lu with size smaller than minimum\n");
                    354:        p1(ips_tooshort, "\t%lu with data size < data length\n");
                    355:        p1(ips_badhlen, "\t%lu with header length < data size\n");
                    356:        p1(ips_badlen, "\t%lu with data length < header length\n");
                    357:        p1(ips_badoptions, "\t%lu with bad options\n");
                    358:        p1(ips_badvers, "\t%lu with incorrect version number\n");
1.16      millert   359:        p(ips_fragments, "\t%lu fragment%s received\n");
                    360:        p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
                    361:        p(ips_badfrags, "\t%lu malformed fragment%s dropped\n");
                    362:        p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
                    363:        p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
                    364:        p(ips_delivered, "\t%lu packet%s for this host\n");
                    365:        p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
                    366:        p(ips_forward, "\t%lu packet%s forwarded\n");
                    367:        p(ips_cantforward, "\t%lu packet%s not forwardable\n");
                    368:        p(ips_redirectsent, "\t%lu redirect%s sent\n");
                    369:        p(ips_localout, "\t%lu packet%s sent from this host\n");
                    370:        p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
                    371:        p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n");
                    372:        p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
                    373:        p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
                    374:        p(ips_ofragments, "\t%lu fragment%s created\n");
                    375:        p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
1.38      itojun    376:        p1(ips_rcvmemdrop, "\t%lu fragment floods\n");
1.1       deraadt   377: #undef p
1.38      itojun    378: #undef p1
1.1       deraadt   379: }
                    380:
                    381: static char *icmpnames[] = {
                    382:        "echo reply",
                    383:        "#1",
                    384:        "#2",
                    385:        "destination unreachable",
                    386:        "source quench",
                    387:        "routing redirect",
                    388:        "#6",
                    389:        "#7",
                    390:        "echo",
1.9       deraadt   391:        "router advertisement",
                    392:        "router solicitation",
1.1       deraadt   393:        "time exceeded",
                    394:        "parameter problem",
                    395:        "time stamp",
                    396:        "time stamp reply",
                    397:        "information request",
                    398:        "information request reply",
                    399:        "address mask request",
                    400:        "address mask reply",
                    401: };
                    402:
                    403: /*
                    404:  * Dump ICMP statistics.
                    405:  */
                    406: void
                    407: icmp_stats(off, name)
                    408:        u_long off;
                    409:        char *name;
                    410: {
                    411:        struct icmpstat icmpstat;
                    412:        register int i, first;
                    413:
                    414:        if (off == 0)
                    415:                return;
                    416:        kread(off, (char *)&icmpstat, sizeof (icmpstat));
                    417:        printf("%s:\n", name);
                    418:
                    419: #define        p(f, m) if (icmpstat.f || sflag <= 1) \
                    420:     printf(m, icmpstat.f, plural(icmpstat.f))
                    421:
1.16      millert   422:        p(icps_error, "\t%lu call%s to icmp_error\n");
1.1       deraadt   423:        p(icps_oldicmp,
1.16      millert   424:            "\t%lu error%s not generated 'cuz old message was icmp\n");
1.1       deraadt   425:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    426:                if (icmpstat.icps_outhist[i] != 0) {
                    427:                        if (first) {
                    428:                                printf("\tOutput histogram:\n");
                    429:                                first = 0;
                    430:                        }
1.16      millert   431:                        printf("\t\t%s: %lu\n", icmpnames[i],
1.1       deraadt   432:                                icmpstat.icps_outhist[i]);
                    433:                }
1.16      millert   434:        p(icps_badcode, "\t%lu message%s with bad code fields\n");
                    435:        p(icps_tooshort, "\t%lu message%s < minimum length\n");
                    436:        p(icps_checksum, "\t%lu bad checksum%s\n");
                    437:        p(icps_badlen, "\t%lu message%s with bad length\n");
1.1       deraadt   438:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    439:                if (icmpstat.icps_inhist[i] != 0) {
                    440:                        if (first) {
                    441:                                printf("\tInput histogram:\n");
                    442:                                first = 0;
                    443:                        }
1.16      millert   444:                        printf("\t\t%s: %lu\n", icmpnames[i],
1.1       deraadt   445:                                icmpstat.icps_inhist[i]);
                    446:                }
1.16      millert   447:        p(icps_reflect, "\t%lu message response%s generated\n");
1.1       deraadt   448: #undef p
                    449: }
                    450:
                    451: /*
                    452:  * Dump IGMP statistics structure.
                    453:  */
                    454: void
                    455: igmp_stats(off, name)
                    456:        u_long off;
                    457:        char *name;
                    458: {
                    459:        struct igmpstat igmpstat;
                    460:
                    461:        if (off == 0)
                    462:                return;
                    463:        kread(off, (char *)&igmpstat, sizeof (igmpstat));
                    464:        printf("%s:\n", name);
                    465:
                    466: #define        p(f, m) if (igmpstat.f || sflag <= 1) \
                    467:     printf(m, igmpstat.f, plural(igmpstat.f))
                    468: #define        py(f, m) if (igmpstat.f || sflag <= 1) \
                    469:     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
1.16      millert   470:        p(igps_rcv_total, "\t%lu message%s received\n");
                    471:         p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
                    472:         p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
                    473:         py(igps_rcv_queries, "\t%lu membership quer%s received\n");
                    474:         py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
                    475:         p(igps_rcv_reports, "\t%lu membership report%s received\n");
                    476:         p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
                    477:         p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
                    478:         p(igps_snd_reports, "\t%lu membership report%s sent\n");
1.1       deraadt   479: #undef p
                    480: #undef py
                    481: }
                    482:
1.5       deraadt   483: struct rpcnams {
                    484:        struct rpcnams *next;
1.16      millert   485:        in_port_t port;
1.21      deraadt   486:        int       proto;
1.5       deraadt   487:        char    *rpcname;
                    488: };
                    489:
                    490: char *
1.21      deraadt   491: getrpcportnam(port, proto)
1.16      millert   492:        in_port_t port;
1.21      deraadt   493:        int proto;
1.5       deraadt   494: {
                    495:        struct sockaddr_in server_addr;
                    496:        register struct hostent *hp;
                    497:        static struct pmaplist *head;
                    498:        int socket = RPC_ANYSOCK;
                    499:        struct timeval minutetimeout;
                    500:        register CLIENT *client;
                    501:        struct rpcent *rpc;
                    502:        static int first;
                    503:        static struct rpcnams *rpcn;
                    504:        struct rpcnams *n;
1.23      deraadt   505:        char num[20];
1.5       deraadt   506:
                    507:        if (first == 0) {
                    508:                first = 1;
1.7       deraadt   509:                memset((char *)&server_addr, 0, sizeof server_addr);
1.5       deraadt   510:                server_addr.sin_family = AF_INET;
                    511:                if ((hp = gethostbyname("localhost")) != NULL)
1.7       deraadt   512:                        memmove((caddr_t)&server_addr.sin_addr, hp->h_addr,
1.5       deraadt   513:                            hp->h_length);
                    514:                else
                    515:                        (void) inet_aton("0.0.0.0", &server_addr.sin_addr);
                    516:
                    517:                minutetimeout.tv_sec = 60;
                    518:                minutetimeout.tv_usec = 0;
                    519:                server_addr.sin_port = htons(PMAPPORT);
                    520:                if ((client = clnttcp_create(&server_addr, PMAPPROG,
1.7       deraadt   521:                    PMAPVERS, &socket, 50, 500)) == NULL)
                    522:                        return (NULL);
1.5       deraadt   523:                if (clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL,
1.8       deraadt   524:                    xdr_pmaplist, &head, minutetimeout) != RPC_SUCCESS) {
1.7       deraadt   525:                        clnt_destroy(client);
                    526:                        return (NULL);
1.5       deraadt   527:                }
                    528:                for (; head != NULL; head = head->pml_next) {
                    529:                        n = (struct rpcnams *)malloc(sizeof(struct rpcnams));
                    530:                        if (n == NULL)
                    531:                                continue;
                    532:                        n->next = rpcn;
                    533:                        rpcn = n;
                    534:                        n->port = head->pml_map.pm_port;
1.21      deraadt   535:                        n->proto = head->pml_map.pm_prot;
1.5       deraadt   536:
                    537:                        rpc = getrpcbynumber(head->pml_map.pm_prog);
                    538:                        if (rpc)
                    539:                                n->rpcname = strdup(rpc->r_name);
                    540:                        else {
1.23      deraadt   541:                                snprintf(num, sizeof num, "%ld",
                    542:                                    head->pml_map.pm_prog);
1.5       deraadt   543:                                n->rpcname = strdup(num);
                    544:                        }
                    545:                }
                    546:                clnt_destroy(client);
                    547:        }
                    548:
                    549:        for (n = rpcn; n; n = n->next)
1.21      deraadt   550:                if (n->port == port && n->proto == proto)
1.5       deraadt   551:                        return (n->rpcname);
                    552:        return (NULL);
                    553: }
                    554:
1.1       deraadt   555: /*
                    556:  * Pretty print an Internet address (net address + port).
                    557:  * If the nflag was specified, use numbers instead of names.
                    558:  */
                    559: void
1.5       deraadt   560: inetprint(in, port, proto, local)
1.1       deraadt   561:        register struct in_addr *in;
1.16      millert   562:        in_port_t port;
1.1       deraadt   563:        char *proto;
1.5       deraadt   564:        int local;
1.1       deraadt   565: {
                    566:        struct servent *sp = 0;
1.5       deraadt   567:        char line[80], *cp, *nam;
1.1       deraadt   568:        int width;
                    569:
1.23      deraadt   570:        snprintf(line, sizeof line, "%.*s.", (Aflag && !nflag) ? 12 : 16,
                    571:            inetname(in));
1.4       millert   572:        cp = strchr(line, '\0');
1.1       deraadt   573:        if (!nflag && port)
                    574:                sp = getservbyport((int)port, proto);
                    575:        if (sp || port == 0)
1.23      deraadt   576:                snprintf(cp, line + sizeof line - cp, "%.8s",
                    577:                    sp ? sp->s_name : "*");
1.21      deraadt   578:        else if (local && !nflag && (nam = getrpcportnam(ntohs(port),
                    579:            (strcmp(proto, "tcp") == 0 ? IPPROTO_TCP : IPPROTO_UDP))))
1.23      deraadt   580:                snprintf(cp, line + sizeof line - cp, "%d[%.8s]",
                    581:                    ntohs(port), nam);
1.1       deraadt   582:        else
1.23      deraadt   583:                snprintf(cp, line + sizeof line - cp, "%d", ntohs(port));
1.1       deraadt   584:        width = Aflag ? 18 : 22;
                    585:        printf(" %-*.*s", width, width, line);
                    586: }
                    587:
                    588: /*
                    589:  * Construct an Internet address representation.
                    590:  * If the nflag has been supplied, give
                    591:  * numeric value, otherwise try for symbolic name.
                    592:  */
                    593: char *
                    594: inetname(inp)
                    595:        struct in_addr *inp;
                    596: {
                    597:        register char *cp;
                    598:        static char line[50];
                    599:        struct hostent *hp;
                    600:        struct netent *np;
                    601:        static char domain[MAXHOSTNAMELEN + 1];
                    602:        static int first = 1;
                    603:
                    604:        if (first && !nflag) {
                    605:                first = 0;
                    606:                if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1.4       millert   607:                    (cp = strchr(domain, '.')))
1.1       deraadt   608:                        (void) strcpy(domain, cp + 1);
                    609:                else
                    610:                        domain[0] = 0;
                    611:        }
                    612:        cp = 0;
                    613:        if (!nflag && inp->s_addr != INADDR_ANY) {
                    614:                int net = inet_netof(*inp);
                    615:                int lna = inet_lnaof(*inp);
                    616:
                    617:                if (lna == INADDR_ANY) {
                    618:                        np = getnetbyaddr(net, AF_INET);
                    619:                        if (np)
                    620:                                cp = np->n_name;
                    621:                }
                    622:                if (cp == 0) {
                    623:                        hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
                    624:                        if (hp) {
1.4       millert   625:                                if ((cp = strchr(hp->h_name, '.')) &&
1.1       deraadt   626:                                    !strcmp(cp + 1, domain))
                    627:                                        *cp = 0;
                    628:                                cp = hp->h_name;
                    629:                        }
                    630:                }
                    631:        }
                    632:        if (inp->s_addr == INADDR_ANY)
1.23      deraadt   633:                snprintf(line, sizeof line, "*");
1.1       deraadt   634:        else if (cp)
1.23      deraadt   635:                snprintf(line, sizeof line, "%s", cp);
1.1       deraadt   636:        else {
                    637:                inp->s_addr = ntohl(inp->s_addr);
                    638: #define C(x)   ((x) & 0xff)
1.23      deraadt   639:                snprintf(line, sizeof line, "%u.%u.%u.%u",
                    640:                    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
                    641:                    C(inp->s_addr >> 8), C(inp->s_addr));
1.1       deraadt   642:        }
                    643:        return (line);
1.10      angelos   644: }
                    645:
                    646: /*
                    647:  * Dump AH statistics structure.
                    648:  */
                    649: void
                    650: ah_stats(off, name)
                    651:         u_long off;
                    652:         char *name;
                    653: {
                    654:         struct ahstat ahstat;
                    655:
                    656:         if (off == 0)
                    657:                 return;
                    658:         kread(off, (char *)&ahstat, sizeof (ahstat));
                    659:         printf("%s:\n", name);
                    660:
                    661: #define p(f, m) if (ahstat.f || sflag <= 1) \
                    662:     printf(m, ahstat.f, plural(ahstat.f))
1.38      itojun    663: #define p1(f, m) if (ahstat.f || sflag <= 1) \
                    664:     printf(m, ahstat.f)
1.10      angelos   665:
1.38      itojun    666:        p1(ahs_input, "\t%u input AH packets\n");
                    667:        p1(ahs_output, "\t%u output AH packets\n");
1.10      angelos   668:         p(ahs_hdrops, "\t%u packet%s shorter than header shows\n");
1.33      niklas    669:         p(ahs_pdrops, "\t%u packet%s dropped due to policy\n");
1.10      angelos   670:         p(ahs_notdb, "\t%u packet%s for which no TDB was found\n");
                    671:         p(ahs_badkcr, "\t%u input packet%s that failed to be processed\n");
                    672:         p(ahs_badauth, "\t%u packet%s that failed verification received\n");
                    673:         p(ahs_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
1.19      provos    674:         p(ahs_qfull, "\t%u packet%s were dropped due to full output queue\n");
1.10      angelos   675:         p(ahs_wrap, "\t%u packet%s where counter wrapping was detected\n");
                    676:         p(ahs_replay, "\t%u possibly replayed packet%s received\n");
                    677:         p(ahs_badauthl, "\t%u packet%s with bad authenticator length received\n");
1.15      provos    678:        p(ahs_invalid, "\t%u packet%s attempted to use an invalid tdb\n");
1.25      niklas    679:        p(ahs_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.20      angelos   680:        p(ahs_ibytes, "\t%qu input byte%s\n");
                    681:        p(ahs_obytes, "\t%qu output byte%s\n");
                    682:
1.34      angelos   683: #undef p
1.38      itojun    684: #undef p1
1.34      angelos   685: }
                    686:
                    687: /*
                    688:  * Dump etherip statistics structure.
                    689:  */
                    690: void
                    691: etherip_stats(off, name)
                    692:        u_long off;
                    693:        char *name;
                    694: {
                    695:         struct etheripstat etheripstat;
                    696:
                    697:
                    698:         if (off == 0)
                    699:                 return;
                    700:         kread(off, (char *)&etheripstat, sizeof (etheripstat));
                    701:         printf("%s:\n", name);
                    702:
                    703: #define p(f, m) if (etheripstat.f || sflag <= 1) \
                    704:     printf(m, etheripstat.f, plural(etheripstat.f))
                    705:
                    706:
                    707:         p(etherip_hdrops, "\t%u packet%s shorter than header shows\n");
                    708:         p(etherip_qfull, "\t%u packet%s were dropped due to full output queue\n");
                    709:        p(etherip_noifdrops, "\t%u packet%s were dropped because of no interface/bridge information\n");
                    710:         p(etherip_pdrops, "\t%u packet%s dropped due to policy\n");
                    711:         p(etherip_adrops, "\t%u packet%s dropped for other reasons\n");
                    712:        p(etherip_ipackets, "\t%u input ethernet-in-IP packets\n");
                    713:        p(etherip_opackets, "\t%u output ethernet-in-IP packets\n");
                    714:        p(etherip_ibytes, "\t%qu input byte%s\n");
                    715:        p(etherip_obytes, "\t%qu output byte%s\n");
1.10      angelos   716: #undef p
                    717: }
                    718:
                    719: /*
                    720:  * Dump ESP statistics structure.
                    721:  */
                    722: void
                    723: esp_stats(off, name)
                    724:         u_long off;
                    725:         char *name;
                    726: {
                    727:         struct espstat espstat;
                    728:
1.20      angelos   729:
1.10      angelos   730:         if (off == 0)
                    731:                 return;
                    732:         kread(off, (char *)&espstat, sizeof (espstat));
                    733:         printf("%s:\n", name);
                    734:
                    735: #define p(f, m) if (espstat.f || sflag <= 1) \
                    736:     printf(m, espstat.f, plural(espstat.f))
                    737:
1.38      itojun    738:        p(esps_input, "\t%u input ESP packet%s\n");
                    739:        p(esps_output, "\t%u output ESP packet%s\n");
1.10      angelos   740:         p(esps_hdrops, "\t%u packet%s shorter than header shows\n");
1.33      niklas    741:         p(esps_pdrops, "\t%u packet%s dropped due to policy\n");
1.10      angelos   742:         p(esps_notdb, "\t%u packet%s for which no TDB was found\n");
                    743:         p(esps_badkcr, "\t%u input packet%s that failed to be processed\n");
1.37      niklas    744:         p(esps_badenc, "\t%u packet%s with bad encryption received\n");
1.10      angelos   745:         p(esps_badauth, "\t%u packet%s that failed verification received\n");
                    746:         p(esps_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
1.19      provos    747:         p(esps_qfull, "\t%u packet%s were dropped due to full output queue\n");
1.10      angelos   748:         p(esps_wrap, "\t%u packet%s where counter wrapping was detected\n");
                    749:         p(esps_replay, "\t%u possibly replayed packet%s received\n");
                    750:         p(esps_badilen, "\t%u packet%s with payload not a multiple of 8 received\n");
1.15      provos    751:        p(esps_invalid, "\t%u packet%s attempted to use an invalid tdb\n");
1.25      niklas    752:        p(esps_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.20      angelos   753:        p(esps_ibytes, "\t%qu input byte%s\n");
                    754:        p(esps_obytes, "\t%qu output byte%s\n");
1.10      angelos   755:
                    756: #undef p
                    757: }
                    758:
                    759: /*
                    760:  * Dump ESP statistics structure.
                    761:  */
                    762: void
                    763: ip4_stats(off, name)
                    764:         u_long off;
                    765:         char *name;
                    766: {
                    767:         struct ip4stat ip4stat;
                    768:
                    769:         if (off == 0)
                    770:                 return;
                    771:         kread(off, (char *)&ip4stat, sizeof (ip4stat));
                    772:         printf("%s:\n", name);
                    773:
                    774: #define p(f, m) if (ip4stat.f || sflag <= 1) \
                    775:     printf(m, ip4stat.f, plural(ip4stat.f))
                    776:
                    777:         p(ip4s_ipackets, "\t%u total input packet%s\n");
1.11      angelos   778:         p(ip4s_opackets, "\t%u total output packet%s\n");
1.10      angelos   779:         p(ip4s_hdrops, "\t%u packet%s shorter than header shows\n");
1.32      niklas    780:         p(ip4s_pdrops, "\t%u packet%s dropped due to policy\n");
                    781:         p(ip4s_spoof, "\t%u packet%s with possibly spoofed local addresses\n");
1.19      provos    782:         p(ip4s_qfull, "\t%u packet%s were dropped due to full output queue\n");
1.20      angelos   783:        p(ip4s_ibytes, "\t%qu input byte%s\n");
                    784:        p(ip4s_obytes, "\t%qu output byte%s\n");
1.40      angelos   785:        p(ip4s_family, "\t%u protocol family mismatches\n");
1.41    ! angelos   786:        p(ip4s_unspec, "\t%u attempts to use tunnel with unspecified endpoint(s)\n");
1.10      angelos   787: #undef p
1.1       deraadt   788: }