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

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