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

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