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

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