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

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