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

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