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

Annotation of src/usr.bin/netstat/route.c, Revision 1.108

1.108   ! deraadt     1: /*     $OpenBSD: route.c,v 1.107 2021/03/17 09:03:51 claudio Exp $     */
1.2       deraadt     2: /*     $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 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.
1.53      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.95      deraadt    33: #include <sys/types.h>
1.1       deraadt    34: #include <sys/protosw.h>
                     35: #include <sys/socket.h>
                     36:
                     37: #include <net/if.h>
                     38: #include <net/if_types.h>
1.107     claudio    39: #define _KERNEL
1.1       deraadt    40: #include <net/route.h>
1.107     claudio    41: #undef _KERNEL
1.96      deraadt    42: #include <netinet/ip_ipsp.h>
1.1       deraadt    43: #include <netinet/in.h>
1.13      millert    44: #include <arpa/inet.h>
1.1       deraadt    45:
                     46: #include <sys/sysctl.h>
                     47:
1.61      otto       48: #include <err.h>
1.15      millert    49: #include <limits.h>
1.1       deraadt    50: #include <netdb.h>
                     51: #include <stdio.h>
                     52: #include <stdlib.h>
                     53: #include <string.h>
                     54: #include <unistd.h>
1.105     remi       55: #include <ifaddrs.h>
1.9       angelos    56:
1.1       deraadt    57: #include "netstat.h"
                     58:
                     59: static union {
1.6       deraadt    60:        struct          sockaddr u_sa;
                     61:        u_int32_t       u_data[64];
1.29      deraadt    62:        int             u_dummy;        /* force word-alignment */
1.1       deraadt    63: } pt_u;
                     64:
                     65: struct rtentry rtentry;
                     66:
1.45      millert    67: static struct sockaddr *kgetsa(struct sockaddr *);
1.102     mpi        68: static struct sockaddr *plentosa(sa_family_t, int, struct sockaddr *);
                     69: static struct art_node *getdefault(struct art_table *);
                     70: static void p_table(struct art_table *);
                     71: static void p_artnode(struct art_node *);
1.72      claudio    72: static void p_krtentry(struct rtentry *);
1.1       deraadt    73:
                     74: /*
                     75:  * Print routing tables.
                     76:  */
                     77: void
1.102     mpi        78: routepr(u_long afmap, u_long af2idx, u_long af2idx_max, u_int tableid)
1.1       deraadt    79: {
1.102     mpi        80:        struct art_root ar;
                     81:        struct art_node *node;
                     82:        struct srp *afm_head, *afm;
                     83:        struct {
                     84:                unsigned int    limit;
                     85:                void          **tbl;
                     86:        } map;
                     87:        void **tbl;
                     88:        int i;
                     89:        uint8_t af2i[AF_MAX+1];
                     90:        uint8_t af2i_max;
1.1       deraadt    91:
                     92:        printf("Routing tables\n");
                     93:
1.102     mpi        94:        if (afmap == 0 || af2idx == 0 || af2idx_max == 0) {
                     95:                printf("symbol not in namelist\n");
1.72      claudio    96:                return;
                     97:        }
1.1       deraadt    98:
1.102     mpi        99:        kread(afmap, &afm_head, sizeof(afm_head));
                    100:        kread(af2idx, af2i, sizeof(af2i));
                    101:        kread(af2idx_max, &af2i_max, sizeof(af2i_max));
1.73      claudio   102:
1.102     mpi       103:        if ((afm = calloc(af2i_max + 1, sizeof(*afm))) == NULL)
1.73      claudio   104:                err(1, NULL);
                    105:
1.102     mpi       106:        kread((u_long)afm_head, afm, (af2i_max + 1) * sizeof(*afm));
1.73      claudio   107:
1.102     mpi       108:        for (i = 1; i <= AF_MAX; i++) {
                    109:                if (af != AF_UNSPEC && af != i)
                    110:                        continue;
                    111:                if (af2i[i] == 0 || afm[af2i[i]].ref == NULL)
1.73      claudio   112:                        continue;
1.102     mpi       113:
                    114:                kread((u_long)afm[af2i[i]].ref, &map, sizeof(map));
                    115:                if (tableid >= map.limit)
1.73      claudio   116:                        continue;
1.102     mpi       117:
                    118:                if ((tbl = calloc(map.limit, sizeof(*tbl))) == NULL)
                    119:                        err(1, NULL);
                    120:
                    121:                kread((u_long)map.tbl, tbl, map.limit * sizeof(*tbl));
                    122:                if (tbl[tableid] == NULL)
1.73      claudio   123:                        continue;
1.102     mpi       124:
                    125:                kread((u_long)tbl[tableid], &ar, sizeof(ar));
                    126:
                    127:                free(tbl);
                    128:
                    129:                if (ar.ar_root.ref == NULL)
                    130:                        continue;
                    131:
                    132:                pr_family(i);
                    133:                pr_rthdr(i, Aflag);
                    134:
                    135:                node = getdefault(ar.ar_root.ref);
                    136:                if (node != NULL)
                    137:                        p_artnode(node);
                    138:
                    139:                p_table(ar.ar_root.ref);
1.1       deraadt   140:        }
1.102     mpi       141:
                    142:        free(afm);
1.1       deraadt   143: }
                    144:
                    145: static struct sockaddr *
1.50      deraadt   146: kgetsa(struct sockaddr *dst)
1.1       deraadt   147: {
                    148:
1.83      deraadt   149:        kread((u_long)dst, &pt_u.u_sa, sizeof(pt_u.u_sa));
1.1       deraadt   150:        if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
1.67      jaredy    151:                kread((u_long)dst, pt_u.u_data, pt_u.u_sa.sa_len);
1.1       deraadt   152:        return (&pt_u.u_sa);
                    153: }
                    154:
1.102     mpi       155: static struct sockaddr *
                    156: plentosa(sa_family_t af, int plen, struct sockaddr *sa_mask)
1.1       deraadt   157: {
1.102     mpi       158:        struct sockaddr_in *sin = (struct sockaddr_in *)sa_mask;
                    159:        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa_mask;
                    160:        uint8_t *p;
                    161:        int i;
                    162:
                    163:        if (plen < 0)
                    164:                return (NULL);
                    165:
                    166:        memset(sa_mask, 0, sizeof(struct sockaddr_storage));
                    167:
                    168:        switch (af) {
                    169:        case AF_INET:
                    170:                if (plen > 32)
                    171:                        return (NULL);
                    172:                sin->sin_family = AF_INET;
                    173:                sin->sin_len = sizeof(struct sockaddr_in);
                    174:                memset(&sin->sin_addr, 0, sizeof(sin->sin_addr));
                    175:                p = (uint8_t *)&sin->sin_addr;
                    176:                break;
                    177:        case AF_INET6:
                    178:                if (plen > 128)
                    179:                        return (NULL);
                    180:                sin6->sin6_family = AF_INET6;
                    181:                sin6->sin6_len = sizeof(struct sockaddr_in6);
                    182:                memset(&sin6->sin6_addr.s6_addr, 0, sizeof(sin6->sin6_addr.s6_addr));
                    183:                p = sin6->sin6_addr.s6_addr;
                    184:                break;
                    185:        default:
                    186:                return (NULL);
                    187:        }
1.1       deraadt   188:
1.102     mpi       189:        for (i = 0; i < plen / 8; i++)
                    190:                p[i] = 0xff;
                    191:        if (plen % 8)
                    192:                p[i] = (0xff00 >> (plen % 8)) & 0xff;
                    193:
                    194:        return (sa_mask);
1.1       deraadt   195: }
                    196:
1.102     mpi       197: static struct art_node *
                    198: getdefault(struct art_table *at)
1.49      deraadt   199: {
1.102     mpi       200:        struct art_node *node;
                    201:        struct art_table table;
                    202:        union {
                    203:                struct srp               node;
                    204:                unsigned long            count;
                    205:        } *heap;
                    206:
                    207:        kread((u_long)at, &table, sizeof(table));
                    208:        heap = calloc(1, AT_HEAPSIZE(table.at_bits));
                    209:        kread((u_long)table.at_heap, heap, AT_HEAPSIZE(table.at_bits));
                    210:
                    211:        node = heap[1].node.ref;
                    212:
                    213:        free(heap);
                    214:
                    215:        return (node);
1.49      deraadt   216: }
                    217:
1.1       deraadt   218: static void
1.102     mpi       219: p_table(struct art_table *at)
1.1       deraadt   220: {
1.102     mpi       221:        struct art_node *next, *node;
                    222:        struct art_table *nat, table;
                    223:        union {
                    224:                struct srp               node;
                    225:                unsigned long            count;
                    226:        } *heap;
                    227:        int i, j;
                    228:
                    229:        kread((u_long)at, &table, sizeof(table));
                    230:        heap = calloc(1, AT_HEAPSIZE(table.at_bits));
                    231:        kread((u_long)table.at_heap, heap, AT_HEAPSIZE(table.at_bits));
                    232:
                    233:        for (j = 1; j < table.at_minfringe; j += 2) {
                    234:                for (i = (j > 2) ? j : 2; i < table.at_minfringe; i <<= 1) {
                    235:                        next = heap[i >> 1].node.ref;
                    236:                        node = heap[i].node.ref;
                    237:                        if (node != NULL && node != next)
                    238:                                p_artnode(node);
                    239:                }
                    240:        }
                    241:
                    242:        for (i = table.at_minfringe; i < table.at_minfringe << 1; i++) {
                    243:                next = heap[i >> 1].node.ref;
                    244:                node = heap[i].node.ref;
                    245:                if (!ISLEAF(node)) {
                    246:                        nat = SUBTABLE(node);
                    247:                        node = getdefault(nat);
                    248:                } else
                    249:                        nat = NULL;
                    250:
                    251:                if (node != NULL && node != next)
                    252:                        p_artnode(node);
1.1       deraadt   253:
1.102     mpi       254:                if (nat != NULL)
                    255:                        p_table(nat);
1.1       deraadt   256:        }
1.49      deraadt   257:
1.102     mpi       258:        free(heap);
                    259: }
1.49      deraadt   260:
1.102     mpi       261: static void
                    262: p_artnode(struct art_node *an)
                    263: {
                    264:        struct art_node node;
                    265:        struct rtentry *rt;
                    266:
                    267:        kread((u_long)an, &node, sizeof(node));
                    268:        rt = node.an_rtlist.sl_head.ref;
                    269:
                    270:        while (rt != NULL) {
                    271:                kread((u_long)rt, &rtentry, sizeof(rtentry));
                    272:                if (Aflag)
                    273:                        printf("%-16p ", rt);
                    274:                p_krtentry(&rtentry);
                    275:                rt = rtentry.rt_next.se_next.ref;
1.1       deraadt   276:        }
                    277: }
                    278:
                    279: static void
1.72      claudio   280: p_krtentry(struct rtentry *rt)
1.1       deraadt   281: {
1.30      itojun    282:        struct sockaddr_storage sock1, sock2;
                    283:        struct sockaddr *sa = (struct sockaddr *)&sock1;
                    284:        struct sockaddr *mask = (struct sockaddr *)&sock2;
1.46      deraadt   285:
1.21      peter     286:        bcopy(kgetsa(rt_key(rt)), sa, sizeof(struct sockaddr));
1.30      itojun    287:        if (sa->sa_len > sizeof(struct sockaddr))
                    288:                bcopy(kgetsa(rt_key(rt)), sa, sa->sa_len);
1.21      peter     289:
1.26      angelos   290:        if (sa->sa_family == PF_KEY) {
1.97      reyk      291:                /* Ignore PF_KEY entries */
1.10      deraadt   292:                return;
1.9       angelos   293:        }
1.21      peter     294:
1.102     mpi       295:        mask = plentosa(sa->sa_family, rt_plen(rt), mask);
1.46      deraadt   296:
1.72      claudio   297:        p_addr(sa, mask, rt->rt_flags);
                    298:        p_gwaddr(kgetsa(rt->rt_gateway), sa->sa_family);
1.1       deraadt   299:        p_flags(rt->rt_flags, "%-6.6s ");
1.102     mpi       300:        printf("%5u %8lld ", rt->rt_refcnt - 1, rt->rt_use);
1.1       deraadt   301:        if (rt->rt_rmx.rmx_mtu)
1.79      claudio   302:                printf("%5u ", rt->rt_rmx.rmx_mtu);
1.1       deraadt   303:        else
1.79      claudio   304:                printf("%5s ", "-");
1.39      provos    305:        putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
1.102     mpi       306:        printf("  %2d", rt->rt_priority & RTP_MASK);
1.72      claudio   307:
1.102     mpi       308:        if (rt->rt_ifidx != 0)
                    309:                printf(" if%d", rt->rt_ifidx);
1.1       deraadt   310:        putchar('\n');
1.63      claudio   311:        if (vflag)
1.91      bluhm     312:                printf("\texpire   %10lld%c\n",
                    313:                    (long long)rt->rt_rmx.rmx_expire,
1.63      claudio   314:                    (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ');
1.1       deraadt   315: }
                    316:
                    317: /*
                    318:  * Print routing statistics
                    319:  */
                    320: void
1.78      claudio   321: rt_stats(void)
1.1       deraadt   322: {
                    323:        struct rtstat rtstat;
1.72      claudio   324:        int mib[6];
                    325:        size_t size;
1.76      deraadt   326:
1.78      claudio   327:        mib[0] = CTL_NET;
                    328:        mib[1] = PF_ROUTE;
                    329:        mib[2] = 0;
                    330:        mib[3] = 0;
                    331:        mib[4] = NET_RT_STATS;
                    332:        mib[5] = 0;
                    333:        size = sizeof (rtstat);
                    334:
1.104     deraadt   335:        if (sysctl(mib, 6, &rtstat, &size, NULL, 0) == -1) {
1.78      claudio   336:                perror("sysctl of routing table statistics");
                    337:                exit(1);
                    338:        }
1.72      claudio   339:
1.1       deraadt   340:        printf("routing:\n");
                    341:        printf("\t%u bad routing redirect%s\n",
1.46      deraadt   342:            rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
1.1       deraadt   343:        printf("\t%u dynamically created route%s\n",
1.46      deraadt   344:            rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
1.1       deraadt   345:        printf("\t%u new gateway%s due to redirects\n",
1.46      deraadt   346:            rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
1.1       deraadt   347:        printf("\t%u destination%s found unreachable\n",
1.46      deraadt   348:            rtstat.rts_unreach, plural(rtstat.rts_unreach));
1.1       deraadt   349:        printf("\t%u use%s of a wildcard route\n",
1.46      deraadt   350:            rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
1.105     remi      351: }
                    352:
                    353: /*
                    354:  * Print rdomain and rtable summary
                    355:  */
                    356:
                    357: void
                    358: rdomainpr(void)
                    359: {
                    360:        struct if_data           *ifd;
                    361:        struct ifaddrs           *ifap, *ifa;
                    362:        struct rt_tableinfo       info;
                    363:
                    364:        int      rtt_dom[RT_TABLEID_MAX+1];
                    365:        int      rdom_rttcnt[RT_TABLEID_MAX+1] = { };
                    366:        int      mib[6], rdom, rtt;
                    367:        size_t   len;
                    368:        char    *old, *rdom_if[RT_TABLEID_MAX+1] = { };
                    369:
                    370:        getifaddrs(&ifap);
                    371:        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1.106     benno     372:                if (ifa->ifa_addr == NULL ||
                    373:                    ifa->ifa_addr->sa_family != AF_LINK)
1.105     remi      374:                        continue;
                    375:                ifd = ifa->ifa_data;
                    376:                if (rdom_if[ifd->ifi_rdomain] == NULL) {
                    377:                        if (asprintf(&rdom_if[ifd->ifi_rdomain], "%s",
                    378:                            ifa->ifa_name) == -1)
                    379:                                exit(1);
                    380:                } else {
                    381:                        old = rdom_if[ifd->ifi_rdomain];
                    382:                        if (asprintf(&rdom_if[ifd->ifi_rdomain], "%s %s",
                    383:                            old, ifa->ifa_name) == -1)
                    384:                                exit(1);
                    385:                        free(old);
                    386:                }
                    387:        }
                    388:        freeifaddrs(ifap);
                    389:
                    390:        mib[0] = CTL_NET;
                    391:        mib[1] = PF_ROUTE;
                    392:        mib[2] = 0;
                    393:        mib[3] = 0;
                    394:        mib[4] = NET_RT_TABLE;
                    395:
                    396:        len = sizeof(info);
                    397:        for (rtt = 0; rtt <= RT_TABLEID_MAX; rtt++) {
                    398:                mib[5] = rtt;
                    399:                if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
                    400:                        rtt_dom[rtt] = -1;
                    401:                else {
                    402:                        rtt_dom[rtt] = info.rti_domainid;
                    403:                        rdom_rttcnt[info.rti_domainid]++;
                    404:                }
                    405:        }
                    406:
                    407:        for (rdom = 0; rdom <= RT_TABLEID_MAX; rdom++) {
                    408:                if (rdom_if[rdom] == NULL)
                    409:                        continue;
                    410:                printf("Rdomain %i\n", rdom);
                    411:                printf("  Interface%s %s\n",
                    412:                    (strchr(rdom_if[rdom], ' ') == NULL) ? ":" : "s:",
                    413:                    rdom_if[rdom]);
                    414:                printf("  Routing table%s",
                    415:                    (rdom_rttcnt[rdom] == 1) ? ":" : "s:");
                    416:                for (rtt = 0; rtt <= RT_TABLEID_MAX; rtt++) {
                    417:                        if (rtt_dom[rtt] == rdom)
                    418:                                printf(" %i", rtt);
                    419:                }
                    420:                printf("\n\n");
                    421:                free(rdom_if[rdom]);
                    422:        }
1.1       deraadt   423: }