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

1.45    ! angelos     1: /*     $OpenBSD: inet.c,v 1.44 2000/01/09 23:44:03 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.45    ! angelos    41: static char *rcsid = "$OpenBSD: inet.c,v 1.44 2000/01/09 23:44:03 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>
1.45    ! angelos    74: #include <netinet/ip_ipip.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.42      itojun    377:        p(ips_toolong, "\t%lu packet%s with ip length > max ip packet size\n");
                    378:        p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
1.1       deraadt   379: #undef p
1.38      itojun    380: #undef p1
1.1       deraadt   381: }
                    382:
                    383: static char *icmpnames[] = {
                    384:        "echo reply",
                    385:        "#1",
                    386:        "#2",
                    387:        "destination unreachable",
                    388:        "source quench",
                    389:        "routing redirect",
                    390:        "#6",
                    391:        "#7",
                    392:        "echo",
1.9       deraadt   393:        "router advertisement",
                    394:        "router solicitation",
1.1       deraadt   395:        "time exceeded",
                    396:        "parameter problem",
                    397:        "time stamp",
                    398:        "time stamp reply",
                    399:        "information request",
                    400:        "information request reply",
                    401:        "address mask request",
                    402:        "address mask reply",
                    403: };
                    404:
                    405: /*
                    406:  * Dump ICMP statistics.
                    407:  */
                    408: void
                    409: icmp_stats(off, name)
                    410:        u_long off;
                    411:        char *name;
                    412: {
                    413:        struct icmpstat icmpstat;
                    414:        register int i, first;
                    415:
                    416:        if (off == 0)
                    417:                return;
                    418:        kread(off, (char *)&icmpstat, sizeof (icmpstat));
                    419:        printf("%s:\n", name);
                    420:
                    421: #define        p(f, m) if (icmpstat.f || sflag <= 1) \
                    422:     printf(m, icmpstat.f, plural(icmpstat.f))
                    423:
1.16      millert   424:        p(icps_error, "\t%lu call%s to icmp_error\n");
1.1       deraadt   425:        p(icps_oldicmp,
1.16      millert   426:            "\t%lu error%s not generated 'cuz old message was icmp\n");
1.1       deraadt   427:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    428:                if (icmpstat.icps_outhist[i] != 0) {
                    429:                        if (first) {
1.43      itojun    430:                                printf("\tOutput packet histogram:\n");
1.1       deraadt   431:                                first = 0;
                    432:                        }
1.16      millert   433:                        printf("\t\t%s: %lu\n", icmpnames[i],
1.1       deraadt   434:                                icmpstat.icps_outhist[i]);
                    435:                }
1.16      millert   436:        p(icps_badcode, "\t%lu message%s with bad code fields\n");
                    437:        p(icps_tooshort, "\t%lu message%s < minimum length\n");
                    438:        p(icps_checksum, "\t%lu bad checksum%s\n");
                    439:        p(icps_badlen, "\t%lu message%s with bad length\n");
1.1       deraadt   440:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    441:                if (icmpstat.icps_inhist[i] != 0) {
                    442:                        if (first) {
1.43      itojun    443:                                printf("\tInput packet histogram:\n");
1.1       deraadt   444:                                first = 0;
                    445:                        }
1.16      millert   446:                        printf("\t\t%s: %lu\n", icmpnames[i],
1.1       deraadt   447:                                icmpstat.icps_inhist[i]);
                    448:                }
1.16      millert   449:        p(icps_reflect, "\t%lu message response%s generated\n");
1.1       deraadt   450: #undef p
                    451: }
                    452:
                    453: /*
                    454:  * Dump IGMP statistics structure.
                    455:  */
                    456: void
                    457: igmp_stats(off, name)
                    458:        u_long off;
                    459:        char *name;
                    460: {
                    461:        struct igmpstat igmpstat;
                    462:
                    463:        if (off == 0)
                    464:                return;
                    465:        kread(off, (char *)&igmpstat, sizeof (igmpstat));
                    466:        printf("%s:\n", name);
                    467:
                    468: #define        p(f, m) if (igmpstat.f || sflag <= 1) \
                    469:     printf(m, igmpstat.f, plural(igmpstat.f))
                    470: #define        py(f, m) if (igmpstat.f || sflag <= 1) \
                    471:     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
1.16      millert   472:        p(igps_rcv_total, "\t%lu message%s received\n");
                    473:         p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
                    474:         p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
                    475:         py(igps_rcv_queries, "\t%lu membership quer%s received\n");
                    476:         py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
                    477:         p(igps_rcv_reports, "\t%lu membership report%s received\n");
                    478:         p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
                    479:         p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
                    480:         p(igps_snd_reports, "\t%lu membership report%s sent\n");
1.1       deraadt   481: #undef p
                    482: #undef py
                    483: }
                    484:
1.5       deraadt   485: struct rpcnams {
                    486:        struct rpcnams *next;
1.16      millert   487:        in_port_t port;
1.21      deraadt   488:        int       proto;
1.5       deraadt   489:        char    *rpcname;
                    490: };
                    491:
                    492: char *
1.21      deraadt   493: getrpcportnam(port, proto)
1.16      millert   494:        in_port_t port;
1.21      deraadt   495:        int proto;
1.5       deraadt   496: {
                    497:        struct sockaddr_in server_addr;
                    498:        register struct hostent *hp;
                    499:        static struct pmaplist *head;
                    500:        int socket = RPC_ANYSOCK;
                    501:        struct timeval minutetimeout;
                    502:        register CLIENT *client;
                    503:        struct rpcent *rpc;
                    504:        static int first;
                    505:        static struct rpcnams *rpcn;
                    506:        struct rpcnams *n;
1.23      deraadt   507:        char num[20];
1.5       deraadt   508:
                    509:        if (first == 0) {
                    510:                first = 1;
1.7       deraadt   511:                memset((char *)&server_addr, 0, sizeof server_addr);
1.5       deraadt   512:                server_addr.sin_family = AF_INET;
                    513:                if ((hp = gethostbyname("localhost")) != NULL)
1.7       deraadt   514:                        memmove((caddr_t)&server_addr.sin_addr, hp->h_addr,
1.5       deraadt   515:                            hp->h_length);
                    516:                else
                    517:                        (void) inet_aton("0.0.0.0", &server_addr.sin_addr);
                    518:
                    519:                minutetimeout.tv_sec = 60;
                    520:                minutetimeout.tv_usec = 0;
                    521:                server_addr.sin_port = htons(PMAPPORT);
                    522:                if ((client = clnttcp_create(&server_addr, PMAPPROG,
1.7       deraadt   523:                    PMAPVERS, &socket, 50, 500)) == NULL)
                    524:                        return (NULL);
1.5       deraadt   525:                if (clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL,
1.8       deraadt   526:                    xdr_pmaplist, &head, minutetimeout) != RPC_SUCCESS) {
1.7       deraadt   527:                        clnt_destroy(client);
                    528:                        return (NULL);
1.5       deraadt   529:                }
                    530:                for (; head != NULL; head = head->pml_next) {
                    531:                        n = (struct rpcnams *)malloc(sizeof(struct rpcnams));
                    532:                        if (n == NULL)
                    533:                                continue;
                    534:                        n->next = rpcn;
                    535:                        rpcn = n;
                    536:                        n->port = head->pml_map.pm_port;
1.21      deraadt   537:                        n->proto = head->pml_map.pm_prot;
1.5       deraadt   538:
                    539:                        rpc = getrpcbynumber(head->pml_map.pm_prog);
                    540:                        if (rpc)
                    541:                                n->rpcname = strdup(rpc->r_name);
                    542:                        else {
1.23      deraadt   543:                                snprintf(num, sizeof num, "%ld",
                    544:                                    head->pml_map.pm_prog);
1.5       deraadt   545:                                n->rpcname = strdup(num);
                    546:                        }
                    547:                }
                    548:                clnt_destroy(client);
                    549:        }
                    550:
                    551:        for (n = rpcn; n; n = n->next)
1.21      deraadt   552:                if (n->port == port && n->proto == proto)
1.5       deraadt   553:                        return (n->rpcname);
                    554:        return (NULL);
                    555: }
                    556:
1.1       deraadt   557: /*
                    558:  * Pretty print an Internet address (net address + port).
                    559:  * If the nflag was specified, use numbers instead of names.
                    560:  */
                    561: void
1.5       deraadt   562: inetprint(in, port, proto, local)
1.1       deraadt   563:        register struct in_addr *in;
1.16      millert   564:        in_port_t port;
1.1       deraadt   565:        char *proto;
1.5       deraadt   566:        int local;
1.1       deraadt   567: {
                    568:        struct servent *sp = 0;
1.5       deraadt   569:        char line[80], *cp, *nam;
1.1       deraadt   570:        int width;
                    571:
1.23      deraadt   572:        snprintf(line, sizeof line, "%.*s.", (Aflag && !nflag) ? 12 : 16,
                    573:            inetname(in));
1.4       millert   574:        cp = strchr(line, '\0');
1.1       deraadt   575:        if (!nflag && port)
                    576:                sp = getservbyport((int)port, proto);
                    577:        if (sp || port == 0)
1.23      deraadt   578:                snprintf(cp, line + sizeof line - cp, "%.8s",
                    579:                    sp ? sp->s_name : "*");
1.21      deraadt   580:        else if (local && !nflag && (nam = getrpcportnam(ntohs(port),
                    581:            (strcmp(proto, "tcp") == 0 ? IPPROTO_TCP : IPPROTO_UDP))))
1.23      deraadt   582:                snprintf(cp, line + sizeof line - cp, "%d[%.8s]",
                    583:                    ntohs(port), nam);
1.1       deraadt   584:        else
1.23      deraadt   585:                snprintf(cp, line + sizeof line - cp, "%d", ntohs(port));
1.1       deraadt   586:        width = Aflag ? 18 : 22;
                    587:        printf(" %-*.*s", width, width, line);
                    588: }
                    589:
                    590: /*
                    591:  * Construct an Internet address representation.
                    592:  * If the nflag has been supplied, give
                    593:  * numeric value, otherwise try for symbolic name.
                    594:  */
                    595: char *
                    596: inetname(inp)
                    597:        struct in_addr *inp;
                    598: {
                    599:        register char *cp;
                    600:        static char line[50];
                    601:        struct hostent *hp;
                    602:        struct netent *np;
                    603:        static char domain[MAXHOSTNAMELEN + 1];
                    604:        static int first = 1;
                    605:
                    606:        if (first && !nflag) {
                    607:                first = 0;
                    608:                if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1.4       millert   609:                    (cp = strchr(domain, '.')))
1.1       deraadt   610:                        (void) strcpy(domain, cp + 1);
                    611:                else
                    612:                        domain[0] = 0;
                    613:        }
                    614:        cp = 0;
                    615:        if (!nflag && inp->s_addr != INADDR_ANY) {
                    616:                int net = inet_netof(*inp);
                    617:                int lna = inet_lnaof(*inp);
                    618:
                    619:                if (lna == INADDR_ANY) {
                    620:                        np = getnetbyaddr(net, AF_INET);
                    621:                        if (np)
                    622:                                cp = np->n_name;
                    623:                }
                    624:                if (cp == 0) {
                    625:                        hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
                    626:                        if (hp) {
1.4       millert   627:                                if ((cp = strchr(hp->h_name, '.')) &&
1.1       deraadt   628:                                    !strcmp(cp + 1, domain))
                    629:                                        *cp = 0;
                    630:                                cp = hp->h_name;
                    631:                        }
                    632:                }
                    633:        }
                    634:        if (inp->s_addr == INADDR_ANY)
1.23      deraadt   635:                snprintf(line, sizeof line, "*");
1.1       deraadt   636:        else if (cp)
1.23      deraadt   637:                snprintf(line, sizeof line, "%s", cp);
1.1       deraadt   638:        else {
                    639:                inp->s_addr = ntohl(inp->s_addr);
                    640: #define C(x)   ((x) & 0xff)
1.23      deraadt   641:                snprintf(line, sizeof line, "%u.%u.%u.%u",
                    642:                    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
                    643:                    C(inp->s_addr >> 8), C(inp->s_addr));
1.1       deraadt   644:        }
                    645:        return (line);
1.10      angelos   646: }
                    647:
                    648: /*
                    649:  * Dump AH statistics structure.
                    650:  */
                    651: void
                    652: ah_stats(off, name)
                    653:         u_long off;
                    654:         char *name;
                    655: {
                    656:         struct ahstat ahstat;
                    657:
                    658:         if (off == 0)
                    659:                 return;
                    660:         kread(off, (char *)&ahstat, sizeof (ahstat));
                    661:         printf("%s:\n", name);
                    662:
                    663: #define p(f, m) if (ahstat.f || sflag <= 1) \
                    664:     printf(m, ahstat.f, plural(ahstat.f))
1.38      itojun    665: #define p1(f, m) if (ahstat.f || sflag <= 1) \
                    666:     printf(m, ahstat.f)
1.10      angelos   667:
1.38      itojun    668:        p1(ahs_input, "\t%u input AH packets\n");
                    669:        p1(ahs_output, "\t%u output AH packets\n");
1.44      angelos   670:         p(ahs_nopf, "\t%u packet%s from unsupported protocol families\n");
1.10      angelos   671:         p(ahs_hdrops, "\t%u packet%s shorter than header shows\n");
1.33      niklas    672:         p(ahs_pdrops, "\t%u packet%s dropped due to policy\n");
1.10      angelos   673:         p(ahs_notdb, "\t%u packet%s for which no TDB was found\n");
                    674:         p(ahs_badkcr, "\t%u input packet%s that failed to be processed\n");
                    675:         p(ahs_badauth, "\t%u packet%s that failed verification received\n");
                    676:         p(ahs_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
1.19      provos    677:         p(ahs_qfull, "\t%u packet%s were dropped due to full output queue\n");
1.10      angelos   678:         p(ahs_wrap, "\t%u packet%s where counter wrapping was detected\n");
                    679:         p(ahs_replay, "\t%u possibly replayed packet%s received\n");
                    680:         p(ahs_badauthl, "\t%u packet%s with bad authenticator length received\n");
1.15      provos    681:        p(ahs_invalid, "\t%u packet%s attempted to use an invalid tdb\n");
1.25      niklas    682:        p(ahs_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.20      angelos   683:        p(ahs_ibytes, "\t%qu input byte%s\n");
                    684:        p(ahs_obytes, "\t%qu output byte%s\n");
                    685:
1.34      angelos   686: #undef p
1.38      itojun    687: #undef p1
1.34      angelos   688: }
                    689:
                    690: /*
                    691:  * Dump etherip statistics structure.
                    692:  */
                    693: void
                    694: etherip_stats(off, name)
                    695:        u_long off;
                    696:        char *name;
                    697: {
                    698:         struct etheripstat etheripstat;
                    699:
                    700:
                    701:         if (off == 0)
                    702:                 return;
                    703:         kread(off, (char *)&etheripstat, sizeof (etheripstat));
                    704:         printf("%s:\n", name);
                    705:
                    706: #define p(f, m) if (etheripstat.f || sflag <= 1) \
                    707:     printf(m, etheripstat.f, plural(etheripstat.f))
                    708:
                    709:
                    710:         p(etherip_hdrops, "\t%u packet%s shorter than header shows\n");
                    711:         p(etherip_qfull, "\t%u packet%s were dropped due to full output queue\n");
                    712:        p(etherip_noifdrops, "\t%u packet%s were dropped because of no interface/bridge information\n");
                    713:         p(etherip_pdrops, "\t%u packet%s dropped due to policy\n");
                    714:         p(etherip_adrops, "\t%u packet%s dropped for other reasons\n");
                    715:        p(etherip_ipackets, "\t%u input ethernet-in-IP packets\n");
                    716:        p(etherip_opackets, "\t%u output ethernet-in-IP packets\n");
                    717:        p(etherip_ibytes, "\t%qu input byte%s\n");
                    718:        p(etherip_obytes, "\t%qu output byte%s\n");
1.10      angelos   719: #undef p
                    720: }
                    721:
                    722: /*
                    723:  * Dump ESP statistics structure.
                    724:  */
                    725: void
                    726: esp_stats(off, name)
                    727:         u_long off;
                    728:         char *name;
                    729: {
                    730:         struct espstat espstat;
                    731:
1.20      angelos   732:
1.10      angelos   733:         if (off == 0)
                    734:                 return;
                    735:         kread(off, (char *)&espstat, sizeof (espstat));
                    736:         printf("%s:\n", name);
                    737:
                    738: #define p(f, m) if (espstat.f || sflag <= 1) \
                    739:     printf(m, espstat.f, plural(espstat.f))
                    740:
1.38      itojun    741:        p(esps_input, "\t%u input ESP packet%s\n");
                    742:        p(esps_output, "\t%u output ESP packet%s\n");
1.44      angelos   743:         p(esps_nopf, "\t%u packet%s from unsupported protocol families\n");
1.10      angelos   744:         p(esps_hdrops, "\t%u packet%s shorter than header shows\n");
1.33      niklas    745:         p(esps_pdrops, "\t%u packet%s dropped due to policy\n");
1.10      angelos   746:         p(esps_notdb, "\t%u packet%s for which no TDB was found\n");
                    747:         p(esps_badkcr, "\t%u input packet%s that failed to be processed\n");
1.37      niklas    748:         p(esps_badenc, "\t%u packet%s with bad encryption received\n");
1.10      angelos   749:         p(esps_badauth, "\t%u packet%s that failed verification received\n");
                    750:         p(esps_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
1.19      provos    751:         p(esps_qfull, "\t%u packet%s were dropped due to full output queue\n");
1.10      angelos   752:         p(esps_wrap, "\t%u packet%s where counter wrapping was detected\n");
                    753:         p(esps_replay, "\t%u possibly replayed packet%s received\n");
                    754:         p(esps_badilen, "\t%u packet%s with payload not a multiple of 8 received\n");
1.15      provos    755:        p(esps_invalid, "\t%u packet%s attempted to use an invalid tdb\n");
1.25      niklas    756:        p(esps_toobig, "\t%u packet%s got larger than max IP packet size\n");
1.20      angelos   757:        p(esps_ibytes, "\t%qu input byte%s\n");
                    758:        p(esps_obytes, "\t%qu output byte%s\n");
1.10      angelos   759:
                    760: #undef p
                    761: }
                    762:
                    763: /*
                    764:  * Dump ESP statistics structure.
                    765:  */
                    766: void
1.45    ! angelos   767: ipip_stats(off, name)
1.10      angelos   768:         u_long off;
                    769:         char *name;
                    770: {
1.45    ! angelos   771:         struct ipipstat ipipstat;
1.10      angelos   772:
                    773:         if (off == 0)
                    774:                 return;
1.45    ! angelos   775:         kread(off, (char *)&ipipstat, sizeof (ipipstat));
1.10      angelos   776:         printf("%s:\n", name);
                    777:
1.45    ! angelos   778: #define p(f, m) if (ipipstat.f || sflag <= 1) \
        !           779:     printf(m, ipipstat.f, plural(ipipstat.f))
1.10      angelos   780:
1.45    ! angelos   781:         p(ipips_ipackets, "\t%u total input packet%s\n");
        !           782:         p(ipips_opackets, "\t%u total output packet%s\n");
        !           783:         p(ipips_hdrops, "\t%u packet%s shorter than header shows\n");
        !           784:         p(ipips_pdrops, "\t%u packet%s dropped due to policy\n");
        !           785:         p(ipips_spoof, "\t%u packet%s with possibly spoofed local addresses\n");
        !           786:         p(ipips_qfull, "\t%u packet%s were dropped due to full output queue\n");
        !           787:        p(ipips_ibytes, "\t%qu input byte%s\n");
        !           788:        p(ipips_obytes, "\t%qu output byte%s\n");
        !           789:        p(ipips_family, "\t%u protocol family mismatches\n");
        !           790:        p(ipips_unspec, "\t%u attempts to use tunnel with unspecified endpoint(s)\n");
1.10      angelos   791: #undef p
1.1       deraadt   792: }