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

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