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

Annotation of src/usr.bin/netstat/main.c, Revision 1.117

1.117   ! remi        1: /*     $OpenBSD: main.c,v 1.116 2019/04/28 17:59:51 mpi Exp $  */
1.2       deraadt     2: /*     $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     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.36      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:
1.103     deraadt    33: #include <sys/types.h>
1.1       deraadt    34: #include <sys/protosw.h>
                     35: #include <sys/socket.h>
1.86      claudio    36: #include <sys/sysctl.h>
1.1       deraadt    37:
1.68      claudio    38: #include <net/route.h>
1.1       deraadt    39: #include <netinet/in.h>
                     40:
                     41: #include <ctype.h>
1.61      djm        42: #include <err.h>
1.1       deraadt    43: #include <errno.h>
1.96      guenther   44: #include <fcntl.h>
1.1       deraadt    45: #include <kvm.h>
                     46: #include <limits.h>
                     47: #include <netdb.h>
                     48: #include <nlist.h>
                     49: #include <paths.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
                     53: #include <unistd.h>
                     54: #include "netstat.h"
                     55:
                     56: struct nlist nl[] = {
1.116     mpi        57: #define N_AFMAP                0
                     58:        { "_afmap"},
                     59: #define N_AF2IDX       1
                     60:        { "_af2idx" },
                     61: #define N_AF2IDXMAX    2
                     62:        { "_af2idx_max" },
1.71      deraadt    63:
1.87      bluhm      64:        { "" }
1.1       deraadt    65: };
                     66:
                     67: struct protox {
1.87      bluhm      68:        void    (*pr_stats)(char *);    /* statistics printing routine */
                     69:        char    *pr_name;               /* well-known name */
1.106     claudio    70:        int     pr_proto;               /* protocol number */
1.1       deraadt    71: } protox[] = {
1.106     claudio    72:        { ip_stats,     "ip",   IPPROTO_IPV4 },
                     73:        { icmp_stats,   "icmp", 0 },
                     74:        { igmp_stats,   "igmp", 0 },
                     75:        { ipip_stats,   "ipencap", 0 },
                     76:        { tcp_stats,    "tcp",  IPPROTO_TCP },
                     77:        { udp_stats,    "udp",  IPPROTO_UDP },
1.113     mpi        78:        { ipsec_stats,  "ipsec", 0 },
1.106     claudio    79:        { esp_stats,    "esp", 0 },
                     80:        { ah_stats,     "ah", 0 },
                     81:        { etherip_stats,"etherip", 0 },
                     82:        { ipcomp_stats, "ipcomp", 0 },
                     83:        { carp_stats,   "carp", 0 },
                     84:        { pfsync_stats, "pfsync", 0 },
                     85:        { div_stats,    "divert", IPPROTO_DIVERT },
                     86:        { pflow_stats,  "pflow", 0 },
                     87:        { NULL,         NULL, 0 }
1.1       deraadt    88: };
                     89:
1.19      itojun     90: struct protox ip6protox[] = {
1.106     claudio    91:        { ip6_stats,    "ip6", IPPROTO_IPV6 },
                     92:        { div6_stats,   "divert6", IPPROTO_DIVERT },
                     93:        { icmp6_stats,  "icmp6", 0 },
                     94:        { rip6_stats,   "rip6", 0 },
                     95:        { NULL,         NULL, 0 }
1.19      itojun     96: };
                     97:
1.34      deraadt    98: struct protox *protoprotox[] = {
1.89      henning    99:        protox, ip6protox, NULL
1.34      deraadt   100: };
1.1       deraadt   101:
1.30      millert   102: static void usage(void);
                    103: static struct protox *name2protox(char *);
                    104: static struct protox *knownname(char *);
1.116     mpi       105: void gettable(u_int);
1.86      claudio   106:
1.1       deraadt   107: kvm_t *kvmd;
                    108:
                    109: int
1.34      deraadt   110: main(int argc, char *argv[])
1.1       deraadt   111: {
                    112:        extern char *optarg;
                    113:        extern int optind;
1.68      claudio   114:        const char *errstr;
1.28      mpech     115:        struct protox *tp = NULL; /* for printing cblocks & stats */
1.1       deraadt   116:        int ch;
1.59      markus    117:        char *nlistf = NULL, *memf = NULL, *ep;
1.1       deraadt   118:        char buf[_POSIX2_LINE_MAX];
1.59      markus    119:        u_long pcbaddr = 0;
1.91      mikeb     120:        u_int tableid;
1.86      claudio   121:        int Tflag = 0;
1.82      tedu      122:        int repeatcount = 0;
1.106     claudio   123:        int proto = 0;
1.109     mpi       124:        int need_nlist, kvm_flags = O_RDONLY;
1.95      deraadt   125:
1.1       deraadt   126:        af = AF_UNSPEC;
1.91      mikeb     127:        tableid = getrtable();
1.1       deraadt   128:
1.88      jsing     129:        while ((ch = getopt(argc, argv,
1.117   ! remi      130:            "AaBbc:deFf:ghI:iLlM:mN:np:P:qRrsT:tuvW:w:")) != -1)
1.31      deraadt   131:                switch (ch) {
1.1       deraadt   132:                case 'A':
                    133:                        Aflag = 1;
                    134:                        break;
                    135:                case 'a':
                    136:                        aflag = 1;
1.24      camield   137:                        break;
1.88      jsing     138:                case 'B':
                    139:                        Bflag = 1;
                    140:                        break;
1.24      camield   141:                case 'b':
                    142:                        bflag = 1;
1.1       deraadt   143:                        break;
1.82      tedu      144:                case 'c':
                    145:                        repeatcount = strtonum(optarg, 1, INT_MAX, &errstr);
1.100     tedu      146:                        if (errstr)
                    147:                                errx(1, "count is %s", errstr);
1.82      tedu      148:                        break;
1.1       deraadt   149:                case 'd':
1.114     dlg       150:                        dflag = IF_SHOW_DROP;
                    151:                        break;
                    152:                case 'e':
                    153:                        dflag = IF_SHOW_ERRS;
1.64      pyr       154:                        break;
                    155:                case 'F':
                    156:                        Fflag = 1;
1.1       deraadt   157:                        break;
                    158:                case 'f':
1.4       mickey    159:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   160:                                af = AF_INET;
1.19      itojun    161:                        else if (strcmp(optarg, "inet6") == 0)
                    162:                                af = AF_INET6;
1.7       kstailey  163:                        else if (strcmp(optarg, "local") == 0)
                    164:                                af = AF_LOCAL;
1.1       deraadt   165:                        else if (strcmp(optarg, "unix") == 0)
                    166:                                af = AF_UNIX;
1.75      claudio   167:                        else if (strcmp(optarg, "mpls") == 0)
                    168:                                af = AF_MPLS;
1.1       deraadt   169:                        else {
                    170:                                (void)fprintf(stderr,
                    171:                                    "%s: %s: unknown address family\n",
1.2       deraadt   172:                                    __progname, optarg);
1.1       deraadt   173:                                exit(1);
                    174:                        }
                    175:                        break;
                    176:                case 'g':
                    177:                        gflag = 1;
1.93      tedu      178:                        break;
                    179:                case 'h':
                    180:                        hflag = 1;
1.1       deraadt   181:                        break;
1.2       deraadt   182:                case 'I':
1.1       deraadt   183:                        iflag = 1;
1.2       deraadt   184:                        interface = optarg;
1.1       deraadt   185:                        break;
                    186:                case 'i':
                    187:                        iflag = 1;
                    188:                        break;
1.19      itojun    189:                case 'l':
                    190:                        lflag = 1;
                    191:                        break;
1.1       deraadt   192:                case 'M':
                    193:                        memf = optarg;
                    194:                        break;
                    195:                case 'm':
                    196:                        mflag = 1;
                    197:                        break;
                    198:                case 'N':
                    199:                        nlistf = optarg;
                    200:                        break;
                    201:                case 'n':
                    202:                        nflag = 1;
                    203:                        break;
                    204:                case 'p':
                    205:                        if ((tp = name2protox(optarg)) == NULL) {
                    206:                                (void)fprintf(stderr,
1.42      jmc       207:                                    "%s: %s: unknown protocol\n",
1.2       deraadt   208:                                    __progname, optarg);
1.1       deraadt   209:                                exit(1);
                    210:                        }
                    211:                        pflag = 1;
1.27      brian     212:                        break;
1.59      markus    213:                case 'P':
                    214:                        errno = 0;
                    215:                        pcbaddr = strtoul(optarg, &ep, 16);
                    216:                        if (optarg[0] == '\0' || *ep != '\0' ||
                    217:                            errno == ERANGE) {
                    218:                                (void)fprintf(stderr,
                    219:                                    "%s: %s: invalid PCB address\n",
                    220:                                    __progname, optarg);
                    221:                                exit(1);
                    222:                        }
                    223:                        Pflag = 1;
                    224:                        break;
1.27      brian     225:                case 'q':
                    226:                        qflag = 1;
1.1       deraadt   227:                        break;
1.117   ! remi      228:                case 'R':
        !           229:                        Rflag = 1;
        !           230:                        break;
1.1       deraadt   231:                case 'r':
                    232:                        rflag = 1;
1.46      cedric    233:                        break;
1.1       deraadt   234:                case 's':
                    235:                        ++sflag;
                    236:                        break;
1.68      claudio   237:                case 'T':
1.116     mpi       238:                        tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
                    239:                        if (errstr)
                    240:                                errx(1, "invalid table id: %s", errstr);
1.86      claudio   241:                        Tflag = 1;
1.68      claudio   242:                        break;
1.1       deraadt   243:                case 't':
                    244:                        tflag = 1;
                    245:                        break;
                    246:                case 'u':
                    247:                        af = AF_UNIX;
1.13      peter     248:                        break;
                    249:                case 'v':
                    250:                        vflag = 1;
1.1       deraadt   251:                        break;
1.56      reyk      252:                case 'W':
                    253:                        Wflag = 1;
                    254:                        interface = optarg;
                    255:                        break;
1.1       deraadt   256:                case 'w':
1.100     tedu      257:                        interval = strtonum(optarg, 1, INT_MAX, &errstr);
                    258:                        if (errstr)
                    259:                                errx(1, "interval is %s", errstr);
1.1       deraadt   260:                        iflag = 1;
                    261:                        break;
                    262:                case '?':
                    263:                default:
                    264:                        usage();
                    265:                }
                    266:        argv += optind;
                    267:        argc -= optind;
                    268:
1.108     tedu      269:        if (argc) {
                    270:                interval = strtonum(*argv, 1, INT_MAX, &errstr);
                    271:                if (errstr)
                    272:                        errx(1, "interval is %s", errstr);
                    273:                ++argv;
                    274:                --argc;
                    275:                iflag = 1;
1.84      lum       276:        }
1.107     tedu      277:        if (argc)
                    278:                usage();
1.84      lum       279:
1.56      reyk      280:        /*
1.106     claudio   281:         * Show per-interface statistics which don't need access to
                    282:         * kernel memory (they're using IOCTLs)
1.33      deraadt   283:         */
1.106     claudio   284:        if (Wflag) {
                    285:                if (interface == NULL)
                    286:                        usage();
                    287:                net80211_ifstats(interface);
                    288:                exit(0);
1.33      deraadt   289:        }
1.61      djm       290:
1.1       deraadt   291:        if (mflag) {
1.70      deraadt   292:                mbpr();
1.1       deraadt   293:                exit(0);
                    294:        }
                    295:        if (iflag) {
1.82      tedu      296:                intpr(interval, repeatcount);
1.1       deraadt   297:                exit(0);
                    298:        }
1.106     claudio   299:        if (sflag) {
                    300:                if (rflag) {
1.73      claudio   301:                        rt_stats();
1.106     claudio   302:                } else if (gflag) {
1.19      itojun    303:                        if (af == AF_INET || af == AF_UNSPEC)
1.71      deraadt   304:                                mrt_stats();
1.19      itojun    305:                        if (af == AF_INET6 || af == AF_UNSPEC)
1.71      deraadt   306:                                mrt6_stats();
1.106     claudio   307:                } else if (pflag && tp->pr_name) {
                    308:                        (*tp->pr_stats)(tp->pr_name);
1.70      deraadt   309:                } else {
1.19      itojun    310:                        if (af == AF_INET || af == AF_UNSPEC)
1.106     claudio   311:                                for (tp = protox; tp->pr_name; tp++)
                    312:                                        (*tp->pr_stats)(tp->pr_name);
1.19      itojun    313:                        if (af == AF_INET6 || af == AF_UNSPEC)
1.106     claudio   314:                                for (tp = ip6protox; tp->pr_name; tp++)
                    315:                                        (*tp->pr_stats)(tp->pr_name);
1.19      itojun    316:                }
1.1       deraadt   317:                exit(0);
                    318:        }
1.106     claudio   319:        if (gflag) {
                    320:                if (af == AF_INET || af == AF_UNSPEC)
                    321:                        mroutepr();
                    322:                if (af == AF_INET6 || af == AF_UNSPEC)
                    323:                        mroute6pr();
1.117   ! remi      324:                exit(0);
        !           325:        }
        !           326:
        !           327:        if (Rflag) {
        !           328:                rdomainpr();
1.106     claudio   329:                exit(0);
                    330:        }
                    331:
                    332:        /*
                    333:         * The remaining code may need kvm so lets try to open it.
                    334:         * -r and -P are the only bits left that actually can use this.
                    335:         */
1.109     mpi       336:        need_nlist = (nlistf != NULL) || (memf != NULL) || (Aflag && rflag);
                    337:        if (!need_nlist && !Pflag)
                    338:                kvm_flags |= KVM_NO_FILES;
1.106     claudio   339:
1.109     mpi       340:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, kvm_flags, buf)) == NULL)
1.106     claudio   341:                errx(1, "kvm_openfiles: %s", buf);
                    342:
                    343:        if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) {
                    344:                if (nlistf)
                    345:                        errx(1, "%s: no namelist", nlistf);
                    346:                else
                    347:                        errx(1, "no namelist");
1.76      gollo     348:        }
1.106     claudio   349:
1.116     mpi       350:        if (!need_nlist && Tflag)
                    351:                gettable(tableid);
                    352:
1.106     claudio   353:        if (rflag) {
                    354:                if (Aflag || nlistf != NULL || memf != NULL)
1.116     mpi       355:                        routepr(nl[N_AFMAP].n_value, nl[N_AF2IDX].n_value,
                    356:                            nl[N_AF2IDXMAX].n_value, tableid);
1.106     claudio   357:                else
                    358:                        p_rttables(af, tableid);
                    359:                exit(0);
1.1       deraadt   360:        }
                    361:
1.106     claudio   362:        if (pflag) {
                    363:                if (tp->pr_proto == 0)
                    364:                        errx(1, "no protocol handler for protocol %s",
                    365:                            tp->pr_name);
                    366:                else
                    367:                        proto = tp->pr_proto;
1.1       deraadt   368:        }
1.106     claudio   369:
                    370:        protopr(kvmd, pcbaddr, tableid, proto);
                    371:        exit(0);
1.1       deraadt   372: }
                    373:
                    374: /*
                    375:  * Read kernel memory, return 0 on success.
                    376:  */
                    377: int
1.53      jaredy    378: kread(u_long addr, void *buf, int size)
1.1       deraadt   379: {
                    380:
                    381:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   382:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   383:                    kvm_geterr(kvmd));
                    384:                return (-1);
                    385:        }
                    386:        return (0);
                    387: }
                    388:
                    389: char *
1.77      claudio   390: plural(u_int64_t n)
1.1       deraadt   391: {
                    392:        return (n != 1 ? "s" : "");
                    393: }
                    394:
                    395: char *
1.77      claudio   396: plurales(u_int64_t n)
1.1       deraadt   397: {
                    398:        return (n != 1 ? "es" : "");
1.110     bluhm     399: }
                    400:
                    401: char *
                    402: pluralys(u_int64_t n)
                    403: {
                    404:        return (n != 1 ? "ies" : "y");
1.1       deraadt   405: }
                    406:
                    407: /*
                    408:  * Find the protox for the given "well-known" name.
                    409:  */
                    410: static struct protox *
1.34      deraadt   411: knownname(char *name)
1.1       deraadt   412: {
                    413:        struct protox **tpp, *tp;
                    414:
                    415:        for (tpp = protoprotox; *tpp; tpp++)
                    416:                for (tp = *tpp; tp->pr_name; tp++)
                    417:                        if (strcmp(tp->pr_name, name) == 0)
                    418:                                return (tp);
                    419:        return (NULL);
                    420: }
                    421:
                    422: /*
                    423:  * Find the protox corresponding to name.
                    424:  */
                    425: static struct protox *
1.34      deraadt   426: name2protox(char *name)
1.1       deraadt   427: {
                    428:        struct protox *tp;
                    429:        char **alias;                   /* alias from p->aliases */
                    430:        struct protoent *p;
                    431:
                    432:        /*
                    433:         * Try to find the name in the list of "well-known" names. If that
                    434:         * fails, check if name is an alias for an Internet protocol.
                    435:         */
1.11      millert   436:        if ((tp = knownname(name)))
1.1       deraadt   437:                return (tp);
                    438:
                    439:        setprotoent(1);                 /* make protocol lookup cheaper */
1.11      millert   440:        while ((p = getprotoent())) {
1.1       deraadt   441:                /* assert: name not same as p->name */
                    442:                for (alias = p->p_aliases; *alias; alias++)
                    443:                        if (strcmp(name, *alias) == 0) {
                    444:                                endprotoent();
                    445:                                return (knownname(p->p_name));
                    446:                        }
                    447:        }
                    448:        endprotoent();
                    449:        return (NULL);
                    450: }
                    451:
                    452: static void
1.34      deraadt   453: usage(void)
1.1       deraadt   454: {
                    455:        (void)fprintf(stderr,
1.112     benno     456:            "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n"
1.115     jmc       457:            "       %s [-bdeFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
                    458:            "               [-T rtable]\n"
                    459:            "       %s [-bdehn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n"
1.87      bluhm     460:            "       %s [-v] [-M core] [-N system] -P pcbaddr\n"
1.60      jaredy    461:            "       %s [-s] [-M core] [-N system] [-p protocol]\n"
                    462:            "       %s [-a] [-f address_family] [-i | -I interface]\n"
                    463:            "       %s [-W interface]\n",
                    464:            __progname, __progname, __progname, __progname,
                    465:            __progname, __progname, __progname);
1.1       deraadt   466:        exit(1);
                    467: }
1.86      claudio   468:
1.116     mpi       469: void
                    470: gettable(u_int tableid)
1.86      claudio   471: {
                    472:        struct rt_tableinfo info;
                    473:        int mib[6];
                    474:        size_t len;
                    475:
                    476:        mib[0] = CTL_NET;
1.101     guenther  477:        mib[1] = PF_ROUTE;
1.86      claudio   478:        mib[2] = 0;
                    479:        mib[3] = 0;
                    480:        mib[4] = NET_RT_TABLE;
                    481:        mib[5] = tableid;
                    482:
                    483:        len = sizeof(info);
                    484:        if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
1.97      deraadt   485:                err(1, "routing table %d", tableid);
1.86      claudio   486: }