[BACK]Return to ns.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/ns.c, Revision 1.3

1.3     ! millert     1: /*     $OpenBSD: ns.c,v 1.2 1996/06/26 05:37:25 deraadt Exp $  */
1.1       deraadt     2: /*     $NetBSD: ns.c,v 1.8 1995/10/03 21:42:46 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: @(#)ns.c 8.1 (Berkeley) 6/6/93";
                     40: #else
1.3     ! millert    41: static char *rcsid = "$OpenBSD: ns.c,v 1.2 1996/06/26 05:37:25 deraadt Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/socket.h>
                     47: #include <sys/socketvar.h>
                     48: #include <sys/mbuf.h>
                     49: #include <sys/protosw.h>
                     50:
                     51: #include <net/route.h>
                     52: #include <net/if.h>
                     53:
                     54: #include <netinet/tcp_fsm.h>
                     55:
                     56: #include <netns/ns.h>
                     57: #include <netns/ns_pcb.h>
                     58: #include <netns/idp.h>
                     59: #include <netns/idp_var.h>
                     60: #include <netns/ns_error.h>
                     61: #include <netns/sp.h>
                     62: #include <netns/spidp.h>
                     63: #include <netns/spp_timer.h>
                     64: #include <netns/spp_var.h>
                     65: #define SANAMES
                     66: #include <netns/spp_debug.h>
                     67:
                     68: #include <nlist.h>
                     69: #include <errno.h>
                     70: #include <stdio.h>
                     71: #include <string.h>
                     72: #include "netstat.h"
                     73:
                     74: struct nspcb nspcb;
                     75: struct sppcb sppcb;
                     76: struct socket sockb;
                     77:
                     78: static char *ns_prpr __P((struct ns_addr *));
                     79: static void ns_erputil __P((int, int));
                     80:
                     81: static int first = 1;
                     82:
                     83: /*
                     84:  * Print a summary of connections related to a Network Systems
                     85:  * protocol.  For SPP, also give state of connection.
                     86:  * Listening processes (aflag) are suppressed unless the
                     87:  * -a (all) flag is specified.
                     88:  */
                     89:
                     90: void
                     91: nsprotopr(off, name)
                     92:        u_long off;
                     93:        char *name;
                     94: {
                     95:        struct nspcb cb;
                     96:        register struct nspcb *prev, *next;
                     97:        int isspp;
                     98:
                     99:        if (off == 0)
                    100:                return;
                    101:        isspp = strcmp(name, "spp") == 0;
                    102:        kread(off, (char *)&cb, sizeof (struct nspcb));
                    103:        nspcb = cb;
                    104:        prev = (struct nspcb *)off;
                    105:        if (nspcb.nsp_next == (struct nspcb *)off)
                    106:                return;
                    107:        for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
                    108:                u_long ppcb;
                    109:
                    110:                next = nspcb.nsp_next;
                    111:                kread((u_long)next, (char *)&nspcb, sizeof (nspcb));
                    112:                if (nspcb.nsp_prev != prev) {
                    113:                        printf("???\n");
                    114:                        break;
                    115:                }
                    116:                if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
                    117:                        continue;
                    118:                }
                    119:                kread((u_long)nspcb.nsp_socket,
                    120:                                (char *)&sockb, sizeof (sockb));
                    121:                ppcb = (u_long) nspcb.nsp_pcb;
                    122:                if (ppcb) {
                    123:                        if (isspp) {
                    124:                                kread(ppcb, (char *)&sppcb, sizeof (sppcb));
                    125:                        } else continue;
                    126:                } else
                    127:                        if (isspp) continue;
                    128:                if (first) {
                    129:                        printf("Active NS connections");
                    130:                        if (aflag)
                    131:                                printf(" (including servers)");
                    132:                        putchar('\n');
                    133:                        if (Aflag)
                    134:                                printf("%-8.8s ", "PCB");
                    135:                        printf(Aflag ?
                    136:                                "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
                    137:                                "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
                    138:                                "Proto", "Recv-Q", "Send-Q",
                    139:                                "Local Address", "Foreign Address", "(state)");
                    140:                        first = 0;
                    141:                }
                    142:                if (Aflag)
1.3     ! millert   143:                        printf("%8lx ", ppcb);
        !           144:                printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
1.1       deraadt   145:                        sockb.so_snd.sb_cc);
                    146:                printf("  %-22.22s", ns_prpr(&nspcb.nsp_laddr));
                    147:                printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
                    148:                if (isspp) {
                    149:                        extern char *tcpstates[];
                    150:                        if (sppcb.s_state >= TCP_NSTATES)
                    151:                                printf(" %d", sppcb.s_state);
                    152:                        else
                    153:                                printf(" %s", tcpstates[sppcb.s_state]);
                    154:                }
                    155:                putchar('\n');
                    156:                prev = next;
                    157:        }
                    158: }
                    159: #define ANY(x,y,z) \
1.3     ! millert   160:        ((x) ? printf("\t%ld %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
1.1       deraadt   161:
                    162: /*
                    163:  * Dump SPP statistics structure.
                    164:  */
                    165: void
                    166: spp_stats(off, name)
                    167:        u_long off;
                    168:        char *name;
                    169: {
                    170:        struct spp_istat spp_istat;
                    171: #define sppstat spp_istat.newstats
                    172:
                    173:        if (off == 0)
                    174:                return;
                    175:        kread(off, (char *)&spp_istat, sizeof (spp_istat));
                    176:        printf("%s:\n", name);
1.3     ! millert   177:        ANY((long)spp_istat.nonucn, "connection",
        !           178:            " dropped due to no new sockets ");
        !           179:        ANY((long)spp_istat.gonawy, "connection",
        !           180:            " terminated due to our end dying");
        !           181:        ANY((long)spp_istat.nonucn, "connection",
1.1       deraadt   182:            " dropped due to inability to connect");
1.3     ! millert   183:        ANY((long)spp_istat.noconn, "connection",
1.1       deraadt   184:            " dropped due to inability to connect");
1.3     ! millert   185:        ANY((long)spp_istat.notme, "connection",
1.1       deraadt   186:            " incompleted due to mismatched id's");
1.3     ! millert   187:        ANY((long)spp_istat.wrncon, "connection",
        !           188:            " dropped due to mismatched id's");
        !           189:        ANY((long)spp_istat.bdreas, "packet", " dropped out of sequence");
        !           190:        ANY((long)spp_istat.lstdup, "packet",
        !           191:            " duplicating the highest packet");
        !           192:        ANY((long)spp_istat.notyet, "packet",
        !           193:            " refused as exceeding allocation");
1.1       deraadt   194:        ANY(sppstat.spps_connattempt, "connection", " initiated");
                    195:        ANY(sppstat.spps_accepts, "connection", " accepted");
                    196:        ANY(sppstat.spps_connects, "connection", " established");
                    197:        ANY(sppstat.spps_drops, "connection", " dropped");
                    198:        ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
                    199:        ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
                    200:        ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
                    201:        ANY(sppstat.spps_rttupdated, "time", " we got rtt");
                    202:        ANY(sppstat.spps_delack, "delayed ack", " sent");
                    203:        ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
                    204:        ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
                    205:        ANY(sppstat.spps_persisttimeo, "persist timeout", "");
                    206:        ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
                    207:        ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
                    208:        ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
                    209:        ANY(sppstat.spps_sndtotal, "total packet", " sent");
                    210:        ANY(sppstat.spps_sndpack, "data packet", " sent");
                    211:        ANY(sppstat.spps_sndbyte, "data byte", " sent");
                    212:        ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
                    213:        ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
                    214:        ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
                    215:        ANY(sppstat.spps_sndprobe, "window probe", " sent");
                    216:        ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
                    217:        ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
                    218:        ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
                    219:        ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
                    220:        ANY(sppstat.spps_rcvtotal, "total packet", " received");
                    221:        ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
                    222:        ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
                    223:        ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
                    224:        ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
                    225:        ANY(sppstat.spps_rcvshort, "packet", " received too short");
                    226:        ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
                    227:        ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
                    228:        ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
                    229:        ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
                    230:        ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
                    231:        ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
                    232:        ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
                    233:        ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
                    234:        ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
                    235:        ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
                    236:        ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
                    237:        ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
                    238:        ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
                    239:        ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
                    240:        ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
                    241: }
                    242: #undef ANY
                    243: #define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
                    244:
                    245: /*
                    246:  * Dump IDP statistics structure.
                    247:  */
                    248: void
                    249: idp_stats(off, name)
                    250:        u_long off;
                    251:        char *name;
                    252: {
                    253:        struct idpstat idpstat;
                    254:
                    255:        if (off == 0)
                    256:                return;
                    257:        kread(off, (char *)&idpstat, sizeof (idpstat));
                    258:        printf("%s:\n", name);
                    259:        ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
                    260:        ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
                    261:        ANY(idpstat.idps_badsum, "packet", " with bad checksums");
                    262: }
                    263:
                    264: static struct {
                    265:        u_short code;
                    266:        char *name;
                    267:        char *where;
                    268: } ns_errnames[] = {
                    269:        {0, "Unspecified Error", " at Destination"},
                    270:        {1, "Bad Checksum", " at Destination"},
                    271:        {2, "No Listener", " at Socket"},
                    272:        {3, "Packet", " Refused due to lack of space at Destination"},
                    273:        {01000, "Unspecified Error", " while gatewayed"},
                    274:        {01001, "Bad Checksum", " while gatewayed"},
                    275:        {01002, "Packet", " forwarded too many times"},
                    276:        {01003, "Packet", " too large to be forwarded"},
                    277:        {-1, 0, 0},
                    278: };
                    279:
                    280: /*
                    281:  * Dump NS Error statistics structure.
                    282:  */
                    283: /*ARGSUSED*/
                    284: void
                    285: nserr_stats(off, name)
                    286:        u_long off;
                    287:        char *name;
                    288: {
                    289:        struct ns_errstat ns_errstat;
                    290:        register int j;
                    291:        register int histoprint = 1;
                    292:        int z;
                    293:
                    294:        if (off == 0)
                    295:                return;
                    296:        kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
                    297:        printf("NS error statistics:\n");
                    298:        ANY(ns_errstat.ns_es_error, "call", " to ns_error");
                    299:        ANY(ns_errstat.ns_es_oldshort, "error",
                    300:                " ignored due to insufficient addressing");
                    301:        ANY(ns_errstat.ns_es_oldns_err, "error request",
                    302:                " in response to error packets");
                    303:        ANY(ns_errstat.ns_es_tooshort, "error packet",
                    304:                " received incomplete");
                    305:        ANY(ns_errstat.ns_es_badcode, "error packet",
                    306:                " received of unknown type");
                    307:        for(j = 0; j < NS_ERR_MAX; j ++) {
                    308:                z = ns_errstat.ns_es_outhist[j];
                    309:                if (z && histoprint) {
                    310:                        printf("Output Error Histogram:\n");
                    311:                        histoprint = 0;
                    312:                }
                    313:                ns_erputil(z, ns_errstat.ns_es_codes[j]);
                    314:
                    315:        }
                    316:        histoprint = 1;
                    317:        for(j = 0; j < NS_ERR_MAX; j ++) {
                    318:                z = ns_errstat.ns_es_inhist[j];
                    319:                if (z && histoprint) {
                    320:                        printf("Input Error Histogram:\n");
                    321:                        histoprint = 0;
                    322:                }
                    323:                ns_erputil(z, ns_errstat.ns_es_codes[j]);
                    324:        }
                    325: }
                    326:
                    327: static void
                    328: ns_erputil(z, c)
                    329:        int z, c;
                    330: {
                    331:        int j;
                    332:        char codebuf[30];
                    333:        char *name, *where;
                    334:
                    335:        for(j = 0;; j ++) {
                    336:                if ((name = ns_errnames[j].name) == 0)
                    337:                        break;
                    338:                if (ns_errnames[j].code == c)
                    339:                        break;
                    340:        }
                    341:        if (name == 0)  {
                    342:                if (c > 01000)
                    343:                        where = "in transit";
                    344:                else
                    345:                        where = "at destination";
                    346:                sprintf(codebuf, "Unknown XNS error code 0%o", c);
                    347:                name = codebuf;
                    348:        } else
                    349:                where =  ns_errnames[j].where;
                    350:        ANY(z, name, where);
                    351: }
                    352:
                    353: static struct sockaddr_ns ssns = {AF_NS};
                    354:
                    355: static
                    356: char *ns_prpr(x)
                    357:        struct ns_addr *x;
                    358: {
                    359:        struct sockaddr_ns *sns = &ssns;
                    360:
                    361:        sns->sns_addr = *x;
                    362:        return(ns_print((struct sockaddr *)sns));
                    363: }