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

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