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

Annotation of src/usr.bin/netstat/if.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: if.c,v 1.11 1997/06/29 20:17:58 millert Exp $ */
1.6       deraadt     2: /*     $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $      */
1.1       deraadt     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: @(#)if.c 8.2 (Berkeley) 2/21/94";
                     40: #else
1.12    ! millert    41: static char *rcsid = "$OpenBSD: if.c,v 1.11 1997/06/29 20:17:58 millert Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/types.h>
                     46: #include <sys/protosw.h>
                     47: #include <sys/socket.h>
                     48:
                     49: #include <net/if.h>
                     50: #include <net/if_dl.h>
1.5       deraadt    51: #include <net/if_types.h>
1.1       deraadt    52: #include <netinet/in.h>
                     53: #include <netinet/in_var.h>
1.11      millert    54: #include <netinet/if_ether.h>
1.1       deraadt    55: #include <netns/ns.h>
                     56: #include <netns/ns_if.h>
1.8       mickey     57: #include <netipx/ipx.h>
                     58: #include <netipx/ipx_if.h>
1.1       deraadt    59: #include <netiso/iso.h>
                     60: #include <netiso/iso_var.h>
                     61: #include <arpa/inet.h>
                     62:
                     63: #include <signal.h>
                     64: #include <stdio.h>
                     65: #include <string.h>
                     66: #include <unistd.h>
                     67:
                     68: #include "netstat.h"
                     69:
                     70: #define        YES     1
                     71: #define        NO      0
                     72:
                     73: static void sidewaysintpr __P((u_int, u_long));
                     74: static void catchalarm __P((int));
                     75:
                     76: /*
                     77:  * Print a description of the network interfaces.
1.3       deraadt    78:  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
                     79:  * which is a TAILQ_HEAD.
1.1       deraadt    80:  */
                     81: void
                     82: intpr(interval, ifnetaddr)
                     83:        int interval;
                     84:        u_long ifnetaddr;
                     85: {
                     86:        struct ifnet ifnet;
                     87:        union {
                     88:                struct ifaddr ifa;
                     89:                struct in_ifaddr in;
                     90:                struct ns_ifaddr ns;
1.8       mickey     91:                struct ipx_ifaddr ipx;
1.1       deraadt    92:                struct iso_ifaddr iso;
                     93:        } ifaddr;
                     94:        u_long ifaddraddr;
                     95:        struct sockaddr *sa;
1.3       deraadt    96:        struct ifnet_head ifhead;       /* TAILQ_HEAD */
                     97:        char name[IFNAMSIZ];
1.1       deraadt    98:
                     99:        if (ifnetaddr == 0) {
                    100:                printf("ifnet: symbol not defined\n");
                    101:                return;
                    102:        }
                    103:        if (interval) {
                    104:                sidewaysintpr((unsigned)interval, ifnetaddr);
                    105:                return;
                    106:        }
1.3       deraadt   107:
                    108:        /*
                    109:         * Find the pointer to the first ifnet structure.  Replace
                    110:         * the pointer to the TAILQ_HEAD with the actual pointer
                    111:         * to the first list element.
                    112:         */
                    113:        if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
1.1       deraadt   114:                return;
1.3       deraadt   115:        ifnetaddr = (u_long)ifhead.tqh_first;
                    116:
1.2       deraadt   117:        printf("%-5.5s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s",
1.1       deraadt   118:                "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
                    119:                "Opkts", "Oerrs");
                    120:        printf(" %5s", "Coll");
                    121:        if (tflag)
                    122:                printf(" %s", "Time");
                    123:        if (dflag)
                    124:                printf(" %s", "Drop");
                    125:        putchar('\n');
                    126:        ifaddraddr = 0;
                    127:        while (ifnetaddr || ifaddraddr) {
                    128:                struct sockaddr_in *sin;
                    129:                register char *cp;
                    130:                int n, m;
                    131:
                    132:                if (ifaddraddr == 0) {
1.3       deraadt   133:                        if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1.1       deraadt   134:                                return;
1.3       deraadt   135:                        bcopy(ifnet.if_xname, name, IFNAMSIZ);
                    136:                        name[IFNAMSIZ - 1] = '\0';      /* sanity */
1.1       deraadt   137:                        ifnetaddr = (u_long)ifnet.if_list.tqe_next;
1.3       deraadt   138:                        if (interface != 0 && strcmp(name, interface) != 0)
1.1       deraadt   139:                                continue;
1.10      millert   140:                        cp = strchr(name, '\0');
1.1       deraadt   141:                        if ((ifnet.if_flags & IFF_UP) == 0)
                    142:                                *cp++ = '*';
                    143:                        *cp = '\0';
                    144:                        ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
                    145:                }
1.11      millert   146:                printf("%-5.5s %-5ld ", name, ifnet.if_mtu);
1.1       deraadt   147:                if (ifaddraddr == 0) {
                    148:                        printf("%-11.11s ", "none");
                    149:                        printf("%-15.15s ", "none");
                    150:                } else {
                    151:                        if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
                    152:                                ifaddraddr = 0;
                    153:                                continue;
                    154:                        }
                    155: #define CP(x) ((char *)(x))
                    156:                        cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
                    157:                                CP(&ifaddr); sa = (struct sockaddr *)cp;
                    158:                        switch (sa->sa_family) {
                    159:                        case AF_UNSPEC:
                    160:                                printf("%-11.11s ", "none");
                    161:                                printf("%-17.17s ", "none");
                    162:                                break;
                    163:                        case AF_INET:
                    164:                                sin = (struct sockaddr_in *)sa;
                    165: #ifdef notdef
                    166:                                /* can't use inet_makeaddr because kernel
                    167:                                 * keeps nets unshifted.
                    168:                                 */
                    169:                                in = inet_makeaddr(ifaddr.in.ia_subnet,
                    170:                                        INADDR_ANY);
                    171:                                printf("%-11.11s ", netname(in.s_addr,
                    172:                                    ifaddr.in.ia_subnetmask));
                    173: #else
                    174:                                printf("%-11.11s ",
                    175:                                    netname(ifaddr.in.ia_subnet,
                    176:                                    ifaddr.in.ia_subnetmask));
                    177: #endif
                    178:                                printf("%-17.17s ",
                    179:                                    routename(sin->sin_addr.s_addr));
                    180:
                    181:                                if (aflag) {
                    182:                                        u_long multiaddr;
                    183:                                        struct in_multi inm;
                    184:
                    185:                                        multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
                    186:                                        while (multiaddr != 0) {
                    187:                                                kread(multiaddr, (char *)&inm,
                    188:                                                    sizeof inm);
                    189:                                                printf("\n%23s %-15.15s ", "",
                    190:                                                    routename(inm.inm_addr.s_addr));
                    191:                                                multiaddr = (u_long)inm.inm_list.le_next;
                    192:                                        }
1.8       mickey    193:                                }
                    194:                                break;
                    195:                        case AF_IPX:
                    196:                                {
                    197:                                struct sockaddr_ipx *sipx =
                    198:                                        (struct sockaddr_ipx *)sa;
                    199:                                u_long net;
                    200:                                char netnum[8];
                    201:
                    202:                                *(union ipx_net *) &net = sipx->sipx_addr.ipx_net;
1.12    ! millert   203:                                sprintf(netnum, "%xH", ntohl(net));
1.8       mickey    204:                                upHex(netnum);
                    205:                                printf("ipx:%-8s", netnum);
                    206:                                printf("%-17s ",
                    207:                                    ipx_phost((struct sockaddr *)sipx));
1.1       deraadt   208:                                }
                    209:                                break;
                    210:                        case AF_NS:
                    211:                                {
                    212:                                struct sockaddr_ns *sns =
                    213:                                        (struct sockaddr_ns *)sa;
                    214:                                u_long net;
                    215:                                char netnum[8];
                    216:
                    217:                                *(union ns_net *) &net = sns->sns_addr.x_net;
1.12    ! millert   218:                                sprintf(netnum, "%xH", ntohl(net));
1.1       deraadt   219:                                upHex(netnum);
                    220:                                printf("ns:%-8s ", netnum);
                    221:                                printf("%-17s ",
                    222:                                    ns_phost((struct sockaddr *)sns));
                    223:                                }
                    224:                                break;
                    225:                        case AF_LINK:
                    226:                                {
                    227:                                struct sockaddr_dl *sdl =
                    228:                                        (struct sockaddr_dl *)sa;
1.5       deraadt   229:                                m = printf("%-11.11s ", "<Link>");
                    230:                                if (sdl->sdl_type == IFT_ETHER ||
                    231:                                    sdl->sdl_type == IFT_FDDI)
                    232:                                        printf("%-17.17s ",
1.11      millert   233:                                            ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1.5       deraadt   234:                                else {
                    235:                                        cp = (char *)LLADDR(sdl);
                    236:                                        n = sdl->sdl_alen;
                    237:                                        goto hexprint;
                    238:                                }
1.1       deraadt   239:                                }
1.5       deraadt   240:                                break;
1.1       deraadt   241:                        default:
                    242:                                m = printf("(%d)", sa->sa_family);
                    243:                                for (cp = sa->sa_len + (char *)sa;
                    244:                                        --cp > sa->sa_data && (*cp == 0);) {}
                    245:                                n = cp - sa->sa_data + 1;
                    246:                                cp = sa->sa_data;
                    247:                        hexprint:
                    248:                                while (--n >= 0)
                    249:                                        m += printf("%x%c", *cp++ & 0xff,
                    250:                                                    n > 0 ? '.' : ' ');
                    251:                                m = 30 - m;
                    252:                                while (m-- > 0)
                    253:                                        putchar(' ');
                    254:                                break;
                    255:                        }
                    256:                        ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
                    257:                }
1.11      millert   258:                printf("%8ld %5ld %8ld %5ld %5ld",
1.1       deraadt   259:                    ifnet.if_ipackets, ifnet.if_ierrors,
                    260:                    ifnet.if_opackets, ifnet.if_oerrors,
                    261:                    ifnet.if_collisions);
                    262:                if (tflag)
                    263:                        printf(" %3d", ifnet.if_timer);
                    264:                if (dflag)
                    265:                        printf(" %3d", ifnet.if_snd.ifq_drops);
                    266:                putchar('\n');
                    267:        }
                    268: }
                    269:
1.4       deraadt   270: #define        MAXIF   100
1.1       deraadt   271: struct iftot {
1.3       deraadt   272:        char    ift_name[IFNAMSIZ];     /* interface name */
1.1       deraadt   273:        int     ift_ip;                 /* input packets */
                    274:        int     ift_ie;                 /* input errors */
                    275:        int     ift_op;                 /* output packets */
                    276:        int     ift_oe;                 /* output errors */
                    277:        int     ift_co;                 /* collisions */
                    278:        int     ift_dr;                 /* drops */
                    279: } iftot[MAXIF];
                    280:
                    281: u_char signalled;                      /* set if alarm goes off "early" */
                    282:
                    283: /*
                    284:  * Print a running summary of interface statistics.
                    285:  * Repeat display every interval seconds, showing statistics
                    286:  * collected over that interval.  Assumes that interval is non-zero.
                    287:  * First line printed at top of screen is always cumulative.
                    288:  */
                    289: static void
                    290: sidewaysintpr(interval, off)
                    291:        unsigned interval;
                    292:        u_long off;
                    293: {
                    294:        struct ifnet ifnet;
                    295:        u_long firstifnet;
                    296:        register struct iftot *ip, *total;
                    297:        register int line;
                    298:        struct iftot *lastif, *sum, *interesting;
1.3       deraadt   299:        struct ifnet_head ifhead;       /* TAILQ_HEAD */
1.1       deraadt   300:        int oldmask;
                    301:
1.3       deraadt   302:        /*
                    303:         * Find the pointer to the first ifnet structure.  Replace
                    304:         * the pointer to the TAILQ_HEAD with the actual pointer
                    305:         * to the first list element.
                    306:         */
                    307:        if (kread(off, (char *)&ifhead, sizeof ifhead))
1.1       deraadt   308:                return;
1.3       deraadt   309:        firstifnet = (u_long)ifhead.tqh_first;
                    310:
1.1       deraadt   311:        lastif = iftot;
                    312:        sum = iftot + MAXIF - 1;
                    313:        total = sum - 1;
1.4       deraadt   314:        interesting = (interface == NULL) ? iftot : NULL;
1.1       deraadt   315:        for (off = firstifnet, ip = iftot; off;) {
                    316:                if (kread(off, (char *)&ifnet, sizeof ifnet))
                    317:                        break;
1.6       deraadt   318:                bzero(ip->ift_name, sizeof(ip->ift_name));
                    319:                snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname);
                    320:                if (interface && strcmp(ifnet.if_xname, interface) == 0)
1.1       deraadt   321:                        interesting = ip;
                    322:                ip++;
                    323:                if (ip >= iftot + MAXIF - 2)
                    324:                        break;
                    325:                off = (u_long)ifnet.if_list.tqe_next;
1.4       deraadt   326:        }
                    327:        if (interesting == NULL) {
                    328:                fprintf(stderr, "%s: %s: unknown interface\n",
                    329:                    __progname, interface);
                    330:                exit(1);
1.1       deraadt   331:        }
                    332:        lastif = ip;
                    333:
                    334:        (void)signal(SIGALRM, catchalarm);
                    335:        signalled = NO;
                    336:        (void)alarm(interval);
                    337: banner:
                    338:        printf("   input    %-6.6s    output       ", interesting->ift_name);
                    339:        if (lastif - iftot > 0) {
                    340:                if (dflag)
                    341:                        printf("      ");
                    342:                printf("     input   (Total)    output");
                    343:        }
                    344:        for (ip = iftot; ip < iftot + MAXIF; ip++) {
                    345:                ip->ift_ip = 0;
                    346:                ip->ift_ie = 0;
                    347:                ip->ift_op = 0;
                    348:                ip->ift_oe = 0;
                    349:                ip->ift_co = 0;
                    350:                ip->ift_dr = 0;
                    351:        }
                    352:        putchar('\n');
                    353:        printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
                    354:                "packets", "errs", "packets", "errs", "colls");
                    355:        if (dflag)
                    356:                printf("%5.5s ", "drops");
                    357:        if (lastif - iftot > 0)
                    358:                printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
                    359:                        "packets", "errs", "packets", "errs", "colls");
                    360:        if (dflag)
                    361:                printf(" %5.5s", "drops");
                    362:        putchar('\n');
                    363:        fflush(stdout);
                    364:        line = 0;
                    365: loop:
                    366:        sum->ift_ip = 0;
                    367:        sum->ift_ie = 0;
                    368:        sum->ift_op = 0;
                    369:        sum->ift_oe = 0;
                    370:        sum->ift_co = 0;
                    371:        sum->ift_dr = 0;
                    372:        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
                    373:                if (kread(off, (char *)&ifnet, sizeof ifnet)) {
                    374:                        off = 0;
                    375:                        continue;
                    376:                }
                    377:                if (ip == interesting) {
1.11      millert   378:                        printf("%8ld %5ld %8ld %5ld %5ld",
1.1       deraadt   379:                                ifnet.if_ipackets - ip->ift_ip,
                    380:                                ifnet.if_ierrors - ip->ift_ie,
                    381:                                ifnet.if_opackets - ip->ift_op,
                    382:                                ifnet.if_oerrors - ip->ift_oe,
                    383:                                ifnet.if_collisions - ip->ift_co);
                    384:                        if (dflag)
                    385:                                printf(" %5d",
                    386:                                    ifnet.if_snd.ifq_drops - ip->ift_dr);
                    387:                }
                    388:                ip->ift_ip = ifnet.if_ipackets;
                    389:                ip->ift_ie = ifnet.if_ierrors;
                    390:                ip->ift_op = ifnet.if_opackets;
                    391:                ip->ift_oe = ifnet.if_oerrors;
                    392:                ip->ift_co = ifnet.if_collisions;
                    393:                ip->ift_dr = ifnet.if_snd.ifq_drops;
                    394:                sum->ift_ip += ip->ift_ip;
                    395:                sum->ift_ie += ip->ift_ie;
                    396:                sum->ift_op += ip->ift_op;
                    397:                sum->ift_oe += ip->ift_oe;
                    398:                sum->ift_co += ip->ift_co;
                    399:                sum->ift_dr += ip->ift_dr;
                    400:                off = (u_long)ifnet.if_list.tqe_next;
                    401:        }
                    402:        if (lastif - iftot > 0) {
                    403:                printf("  %8d %5d %8d %5d %5d",
                    404:                        sum->ift_ip - total->ift_ip,
                    405:                        sum->ift_ie - total->ift_ie,
                    406:                        sum->ift_op - total->ift_op,
                    407:                        sum->ift_oe - total->ift_oe,
                    408:                        sum->ift_co - total->ift_co);
                    409:                if (dflag)
                    410:                        printf(" %5d", sum->ift_dr - total->ift_dr);
                    411:        }
                    412:        *total = *sum;
                    413:        putchar('\n');
                    414:        fflush(stdout);
                    415:        line++;
                    416:        oldmask = sigblock(sigmask(SIGALRM));
                    417:        if (! signalled) {
                    418:                sigpause(0);
                    419:        }
                    420:        sigsetmask(oldmask);
                    421:        signalled = NO;
                    422:        (void)alarm(interval);
                    423:        if (line == 21)
                    424:                goto banner;
                    425:        goto loop;
                    426:        /*NOTREACHED*/
                    427: }
                    428:
                    429: /*
                    430:  * Called if an interval expires before sidewaysintpr has completed a loop.
                    431:  * Sets a flag to not wait for the alarm.
                    432:  */
                    433: static void
                    434: catchalarm(signo)
                    435:        int signo;
                    436: {
                    437:        signalled = YES;
                    438: }