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

Annotation of src/usr.bin/netstat/show.c, Revision 1.5

1.5     ! pyr         1: /*     $OpenBSD: show.c,v 1.4 2006/12/29 10:09:09 claudio Exp $        */
1.1       claudio     2: /*     $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the University nor the names of its contributors
                     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:
                     33: #include <sys/param.h>
                     34: #include <sys/protosw.h>
                     35: #include <sys/socket.h>
                     36: #include <sys/mbuf.h>
                     37: #include <sys/sysctl.h>
                     38:
                     39: #include <net/if.h>
                     40: #include <net/if_dl.h>
                     41: #include <net/if_types.h>
                     42: #include <net/pfkeyv2.h>
                     43: #include <net/route.h>
                     44: #include <netinet/in.h>
                     45: #include <netipx/ipx.h>
                     46: #include <netinet/if_ether.h>
                     47: #include <netinet/ip_ipsp.h>
                     48: #include <arpa/inet.h>
                     49:
                     50: #include <err.h>
                     51: #include <errno.h>
                     52: #include <netdb.h>
                     53: #include <stdio.h>
                     54: #include <stddef.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
                     57: #include <unistd.h>
                     58:
                     59: #include "netstat.h"
                     60:
                     61: char   *any_ntoa(const struct sockaddr *);
                     62: char   *link_print(struct sockaddr *);
                     63:
                     64: #define ROUNDUP(a) \
                     65:        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
                     66: #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
                     67:
                     68: #define PFKEYV2_CHUNK sizeof(u_int64_t)
                     69:
                     70: /*
                     71:  * Definitions for showing gateway flags.
                     72:  */
                     73: struct bits {
                     74:        int     b_mask;
                     75:        char    b_val;
                     76: };
                     77: static const struct bits bits[] = {
                     78:        { RTF_UP,       'U' },
                     79:        { RTF_GATEWAY,  'G' },
                     80:        { RTF_HOST,     'H' },
                     81:        { RTF_REJECT,   'R' },
                     82:        { RTF_BLACKHOLE, 'B' },
                     83:        { RTF_DYNAMIC,  'D' },
                     84:        { RTF_MODIFIED, 'M' },
                     85:        { RTF_DONE,     'd' }, /* Completed -- for routing messages only */
                     86:        { RTF_MASK,     'm' }, /* Mask Present -- for routing messages only */
                     87:        { RTF_CLONING,  'C' },
                     88:        { RTF_XRESOLVE, 'X' },
                     89:        { RTF_LLINFO,   'L' },
                     90:        { RTF_STATIC,   'S' },
                     91:        { RTF_PROTO1,   '1' },
                     92:        { RTF_PROTO2,   '2' },
                     93:        { RTF_PROTO3,   '3' },
                     94:        { RTF_CLONED,   'c' },
                     95:        { RTF_JUMBO,    'J' },
                     96:        { 0 }
                     97: };
                     98:
                     99: void    pr_rthdr(int, int);
                    100: void    p_rtentry(struct rt_msghdr *);
                    101: void    p_pfkentry(struct sadb_msg *);
                    102: void    pr_family(int);
                    103: void    p_encap(struct sockaddr *, struct sockaddr *, int);
                    104: void    p_protocol(struct sadb_protocol *, struct sockaddr *, struct
                    105:            sadb_protocol *, int);
                    106: void    p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
                    107: void    p_flags(int, char *);
                    108: char   *routename4(in_addr_t);
                    109: char   *routename6(struct sockaddr_in6 *);
                    110: void    index_pfk(struct sadb_msg *, void **);
                    111:
                    112: /*
                    113:  * Print routing tables.
                    114:  */
                    115: void
                    116: p_rttables(int af)
                    117: {
                    118:        struct rt_msghdr *rtm;
                    119:        struct sadb_msg *msg;
                    120:        char *buf = NULL, *next, *lim = NULL;
                    121:        size_t needed;
                    122:        int mib[6];
                    123:        struct sockaddr *sa;
                    124:
                    125:        mib[0] = CTL_NET;
                    126:        mib[1] = PF_ROUTE;
                    127:        mib[2] = 0;
                    128:        mib[3] = af;
                    129:        mib[4] = NET_RT_DUMP;
                    130:        mib[5] = 0;
                    131:        if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
                    132:                err(1, "route-sysctl-estimate");
                    133:        if (needed > 0) {
                    134:                if ((buf = malloc(needed)) == 0)
                    135:                        err(1, NULL);
                    136:                if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
                    137:                        err(1, "sysctl of routing table");
                    138:                lim = buf + needed;
                    139:        }
                    140:
                    141:        printf("Routing tables\n");
                    142:
                    143:        if (buf) {
                    144:                for (next = buf; next < lim; next += rtm->rtm_msglen) {
                    145:                        rtm = (struct rt_msghdr *)next;
                    146:                        sa = (struct sockaddr *)(rtm + 1);
                    147:                        if (af != AF_UNSPEC && sa->sa_family != af)
                    148:                                continue;
                    149:                        p_rtentry(rtm);
                    150:                }
                    151:                free(buf);
                    152:                buf = NULL;
                    153:        }
                    154:
                    155:        if (af != 0 && af != PF_KEY)
                    156:                return;
                    157:
                    158:        mib[0] = CTL_NET;
                    159:        mib[1] = PF_KEY;
                    160:        mib[2] = PF_KEY_V2;
                    161:        mib[3] = NET_KEY_SPD_DUMP;
                    162:        mib[4] = mib[5] = 0;
                    163:
                    164:        if (sysctl(mib, 4, NULL, &needed, NULL, 0) == -1) {
                    165:                if (errno == ENOPROTOOPT)
                    166:                        return;
                    167:                err(1, "spd-sysctl-estimate");
                    168:        }
                    169:        if (needed > 0) {
                    170:                if ((buf = malloc(needed)) == 0)
                    171:                        err(1, NULL);
                    172:                if (sysctl(mib, 4, buf, &needed, NULL, 0) == -1)
                    173:                        err(1,"sysctl of spd");
                    174:                lim = buf + needed;
                    175:        }
                    176:
                    177:        if (buf) {
                    178:                printf("\nEncap:\n");
                    179:
                    180:                for (next = buf; next < lim; next += msg->sadb_msg_len *
                    181:                    PFKEYV2_CHUNK) {
                    182:                        msg = (struct sadb_msg *)next;
                    183:                        if (msg->sadb_msg_len == 0)
                    184:                                break;
                    185:                        p_pfkentry(msg);
                    186:                }
                    187:                free(buf);
                    188:                buf = NULL;
                    189:        }
                    190: }
                    191:
                    192: /*
                    193:  * column widths; each followed by one space
                    194:  * width of destination/gateway column
                    195:  * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
                    196:  */
                    197: #define        WID_DST(af)     ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
                    198: #define        WID_GW(af)      ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
                    199:
                    200: /*
                    201:  * Print header for routing table columns.
                    202:  */
                    203: void
                    204: pr_rthdr(int af, int Aflag)
                    205: {
                    206:        if (Aflag)
                    207:                printf("%-*.*s ", PLEN, PLEN, "Address");
                    208:        if (af != PF_KEY)
                    209:                printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s  %s\n",
                    210:                    WID_DST(af), WID_DST(af), "Destination",
                    211:                    WID_GW(af), WID_GW(af), "Gateway",
                    212:                    "Flags", "Refs", "Use", "Mtu", "Interface");
                    213:        else
                    214:                printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
                    215:                    "Source", "Port", "Destination",
                    216:                    "Port", "Proto", "SA(Address/Proto/Type/Direction)");
                    217: }
                    218:
                    219: static void
                    220: get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
                    221: {
                    222:        int     i;
                    223:
                    224:        for (i = 0; i < RTAX_MAX; i++) {
                    225:                if (addrs & (1 << i)) {
                    226:                        rti_info[i] = sa;
                    227:                        sa = (struct sockaddr *)((char *)(sa) +
                    228:                            ROUNDUP(sa->sa_len));
                    229:                } else
                    230:                        rti_info[i] = NULL;
                    231:        }
                    232: }
                    233:
                    234: /*
                    235:  * Print a routing table entry.
                    236:  */
                    237: void
                    238: p_rtentry(struct rt_msghdr *rtm)
                    239: {
                    240:        static int       old_af = -1;
                    241:        struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
                    242:        struct sockaddr *mask, *rti_info[RTAX_MAX];
                    243:        char             ifbuf[IF_NAMESIZE];
                    244:
                    245:
                    246:        if (sa->sa_family == AF_KEY)
                    247:                return;
                    248:
1.5     ! pyr       249:        get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
        !           250:        if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
        !           251:                return;
        !           252:        }
1.1       claudio   253:        if (old_af != sa->sa_family) {
                    254:                old_af = sa->sa_family;
                    255:                pr_family(sa->sa_family);
                    256:                pr_rthdr(sa->sa_family, 0);
                    257:        }
                    258:
                    259:        mask = rti_info[RTAX_NETMASK];
                    260:        if ((sa = rti_info[RTAX_DST]) == NULL)
                    261:                return;
                    262:
                    263:        p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family));
                    264:        p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
                    265:            WID_GW(sa->sa_family));
                    266:        p_flags(rtm->rtm_flags, "%-6.6s ");
                    267:        printf("%6d %8ld ", (int)rtm->rtm_rmx.rmx_refcnt,
                    268:            rtm->rtm_rmx.rmx_pksent);
                    269:        if (rtm->rtm_rmx.rmx_mtu)
                    270:                printf("%6ld ", rtm->rtm_rmx.rmx_mtu);
                    271:        else
                    272:                printf("%6s ", "-");
                    273:        putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
                    274:        printf(" %.16s", if_indextoname(rtm->rtm_index, ifbuf));
                    275:        putchar('\n');
                    276: }
                    277:
                    278: /*
                    279:  * Print a pfkey/encap entry.
                    280:  */
                    281: void
                    282: p_pfkentry(struct sadb_msg *msg)
                    283: {
                    284:        static int               old = 0;
                    285:        struct sadb_ext         *ext;
                    286:        struct sadb_address     *saddr;
                    287:        struct sadb_protocol    *sap, *saft;
                    288:        struct sockaddr         *sa, *mask;
                    289:        void                    *headers[SADB_EXT_MAX + 1];
                    290:
                    291:        if (!old) {
                    292:                pr_rthdr(PF_KEY, 0);
                    293:                old++;
                    294:        }
                    295:
                    296:        bzero(headers, sizeof(headers));
                    297:        index_pfk(msg, headers);
                    298:
                    299:        /* These are always set */
                    300:        saddr = headers[SADB_X_EXT_SRC_FLOW];
                    301:        sa = (struct sockaddr *)(saddr + 1);
                    302:        saddr = headers[SADB_X_EXT_SRC_MASK];
                    303:        mask = (struct sockaddr *)(saddr + 1);
                    304:        p_encap(sa, mask, WID_DST(sa->sa_family));
                    305:
                    306:        /* These are always set, too. */
                    307:        saddr = headers[SADB_X_EXT_DST_FLOW];
                    308:        sa = (struct sockaddr *)(saddr + 1);
                    309:        saddr = headers[SADB_X_EXT_DST_MASK];
                    310:        mask = (struct sockaddr *)(saddr + 1);
                    311:        p_encap(sa, mask, WID_DST(sa->sa_family));
                    312:
                    313:        /* Bypass and deny flows do not set SADB_EXT_ADDRESS_DST! */
                    314:        sap = headers[SADB_X_EXT_PROTOCOL];
                    315:        saft = headers[SADB_X_EXT_FLOW_TYPE];
                    316:        saddr = headers[SADB_EXT_ADDRESS_DST];
                    317:        if (saddr)
                    318:                sa = (struct sockaddr *)(saddr + 1);
                    319:        else
                    320:                sa = NULL;
                    321:        p_protocol(sap, sa, saft, msg->sadb_msg_satype);
                    322:
                    323:        printf("\n");
                    324: }
                    325:
                    326: /*
                    327:  * Print address family header before a section of the routing table.
                    328:  */
                    329: void
                    330: pr_family(int af)
                    331: {
                    332:        char *afname;
                    333:
                    334:        switch (af) {
                    335:        case AF_INET:
                    336:                afname = "Internet";
                    337:                break;
                    338:        case AF_INET6:
                    339:                afname = "Internet6";
                    340:                break;
                    341:        case AF_IPX:
                    342:                afname = "IPX";
                    343:                break;
                    344:        case PF_KEY:
                    345:                afname = "Encap";
                    346:                break;
                    347:        case AF_APPLETALK:
                    348:                afname = "AppleTalk";
                    349:                break;
                    350:        default:
                    351:                afname = NULL;
                    352:                break;
                    353:        }
                    354:        if (afname)
                    355:                printf("\n%s:\n", afname);
                    356:        else
                    357:                printf("\nProtocol Family %d:\n", af);
                    358: }
                    359:
                    360: void
                    361: p_addr(struct sockaddr *sa, struct sockaddr *mask, int flags)
                    362: {
                    363:        p_sockaddr(sa, mask, flags, WID_DST(sa->sa_family));
                    364: }
                    365:
                    366: void
                    367: p_gwaddr(struct sockaddr *sa, int af)
                    368: {
                    369:        p_sockaddr(sa, 0, RTF_HOST, WID_GW(af));
                    370: }
                    371:
                    372: void
                    373: p_encap(struct sockaddr *sa, struct sockaddr *mask, int width)
                    374: {
                    375:        char            *cp;
                    376:        unsigned short   port;
                    377:
1.2       todd      378:        if (mask)
                    379:                cp = netname(sa, mask);
                    380:        else
                    381:                cp = routename(sa);
1.1       claudio   382:        switch (sa->sa_family) {
1.2       todd      383:        case AF_INET6:
                    384:                port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
1.1       claudio   385:                break;
                    386:        default:
                    387:                port = ntohs(((struct sockaddr_in *)sa)->sin_port);
                    388:                break;
                    389:        }
                    390:        if (width < 0)
                    391:                printf("%s", cp);
                    392:        else {
                    393:                if (nflag)
                    394:                        printf("%-*s %-5u ", width, cp, port);
                    395:                else
                    396:                        printf("%-*.*s %-5u ", width, width, cp, port);
                    397:        }
                    398: }
                    399:
                    400: void
                    401: p_protocol(struct sadb_protocol *sap, struct sockaddr *sa, struct sadb_protocol
                    402:     *saft, int proto)
                    403: {
                    404:        printf("%-6u", sap->sadb_protocol_proto);
                    405:        if (sa)
                    406:                p_sockaddr(sa, NULL, 0, -1);
                    407:        else
                    408:                printf("none");
                    409:
                    410:        switch (proto) {
                    411:        case SADB_SATYPE_ESP:
                    412:                printf("/esp");
                    413:                break;
                    414:        case SADB_SATYPE_AH:
                    415:                printf("/ah");
                    416:                break;
                    417:        case SADB_X_SATYPE_IPCOMP:
                    418:                printf("/ipcomp");
                    419:                break;
                    420:        case SADB_X_SATYPE_IPIP:
                    421:                printf("/ipip");
                    422:                break;
                    423:        default:
                    424:                printf("/<unknown>");
                    425:        }
                    426:
                    427:        switch(saft->sadb_protocol_proto) {
                    428:        case SADB_X_FLOW_TYPE_USE:
                    429:                printf("/use");
                    430:                break;
                    431:        case SADB_X_FLOW_TYPE_REQUIRE:
                    432:                printf("/require");
                    433:                break;
                    434:        case SADB_X_FLOW_TYPE_ACQUIRE:
                    435:                printf("/acquire");
                    436:                break;
                    437:        case SADB_X_FLOW_TYPE_DENY:
                    438:                printf("/deny");
                    439:                break;
                    440:        case SADB_X_FLOW_TYPE_BYPASS:
                    441:                printf("/bypass");
                    442:                break;
                    443:        case SADB_X_FLOW_TYPE_DONTACQ:
                    444:                printf("/dontacq");
                    445:                break;
                    446:        default:
                    447:                printf("/<unknown type>");
                    448:        }
                    449:
                    450:        switch(saft->sadb_protocol_direction) {
                    451:        case IPSP_DIRECTION_IN:
                    452:                printf("/in");
                    453:                break;
                    454:        case IPSP_DIRECTION_OUT:
                    455:                printf("/out");
                    456:                break;
                    457:        default:
                    458:                printf("/<unknown>");
                    459:        }
                    460: }
                    461:
                    462: void
                    463: p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
                    464: {
                    465:        char *cp;
                    466:
                    467:        switch (sa->sa_family) {
                    468:        case AF_INET6:
                    469:            {
                    470:                struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
                    471:                struct in6_addr *in6 = &sa6->sin6_addr;
                    472:
                    473:                /*
                    474:                 * XXX: This is a special workaround for KAME kernels.
                    475:                 * sin6_scope_id field of SA should be set in the future.
                    476:                 */
                    477:                if (IN6_IS_ADDR_LINKLOCAL(in6) ||
1.3       itojun    478:                    IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
                    479:                    IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
1.1       claudio   480:                        /* XXX: override is ok? */
                    481:                        sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
                    482:                            &in6->s6_addr[2]);
                    483:                        *(u_short *)&in6->s6_addr[2] = 0;
                    484:                }
                    485:                if (flags & RTF_HOST)
                    486:                        cp = routename((struct sockaddr *)sa6);
                    487:                else
                    488:                        cp = netname((struct sockaddr *)sa6, mask);
                    489:                break;
                    490:            }
                    491:        default:
                    492:                if ((flags & RTF_HOST) || mask == NULL)
                    493:                        cp = routename(sa);
                    494:                else
                    495:                        cp = netname(sa, mask);
                    496:                break;
                    497:        }
                    498:        if (width < 0)
                    499:                printf("%s", cp);
                    500:        else {
                    501:                if (nflag)
                    502:                        printf("%-*s ", width, cp);
                    503:                else
                    504:                        printf("%-*.*s ", width, width, cp);
                    505:        }
                    506: }
                    507:
                    508: void
                    509: p_flags(int f, char *format)
                    510: {
                    511:        char name[33], *flags;
                    512:        const struct bits *p = bits;
                    513:
                    514:        for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
                    515:                if (p->b_mask & f)
                    516:                        *flags++ = p->b_val;
                    517:        *flags = '\0';
                    518:        printf(format, name);
                    519: }
                    520:
                    521: static char line[MAXHOSTNAMELEN];
                    522: static char domain[MAXHOSTNAMELEN];
                    523:
                    524: char *
                    525: routename(struct sockaddr *sa)
                    526: {
                    527:        char *cp = NULL;
                    528:        static int first = 1;
                    529:
                    530:        if (first) {
                    531:                first = 0;
                    532:                if (gethostname(domain, sizeof(domain)) == 0 &&
                    533:                    (cp = strchr(domain, '.')))
                    534:                        (void)strlcpy(domain, cp + 1, sizeof(domain));
                    535:                else
                    536:                        domain[0] = '\0';
                    537:                cp = NULL;
                    538:        }
                    539:
                    540:        if (sa->sa_len == 0) {
                    541:                (void)strlcpy(line, "default", sizeof(line));
                    542:                return (line);
                    543:        }
                    544:
                    545:        switch (sa->sa_family) {
                    546:        case AF_INET:
                    547:                return
                    548:                    (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
                    549:
                    550:        case AF_INET6:
                    551:            {
                    552:                struct sockaddr_in6 sin6;
                    553:
                    554:                memset(&sin6, 0, sizeof(sin6));
                    555:                memcpy(&sin6, sa, sa->sa_len);
                    556:                sin6.sin6_len = sizeof(struct sockaddr_in6);
                    557:                sin6.sin6_family = AF_INET6;
                    558:                if (sa->sa_len == sizeof(struct sockaddr_in6) &&
                    559:                    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3       itojun    560:                     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
                    561:                     IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1       claudio   562:                    sin6.sin6_scope_id == 0) {
                    563:                        sin6.sin6_scope_id =
                    564:                            ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
                    565:                        sin6.sin6_addr.s6_addr[2] = 0;
                    566:                        sin6.sin6_addr.s6_addr[3] = 0;
                    567:                }
                    568:                return (routename6(&sin6));
                    569:            }
                    570:
                    571:        case AF_IPX:
                    572:                return (ipx_print(sa));
                    573:
                    574:        case AF_LINK:
                    575:                return (link_print(sa));
                    576:
                    577:        case AF_UNSPEC:
                    578:                if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
                    579:                        static char name[RTLABEL_LEN];
                    580:                        struct sockaddr_rtlabel *sr;
                    581:
                    582:                        sr = (struct sockaddr_rtlabel *)sa;
                    583:                        strlcpy(name, sr->sr_label, sizeof(name));
                    584:                        return (name);
                    585:                }
                    586:                /* FALLTHROUGH */
                    587:        default:
                    588:                (void)snprintf(line, sizeof(line), "(%d) %s",
                    589:                    sa->sa_family, any_ntoa(sa));
                    590:                break;
                    591:        }
                    592:        return (line);
                    593: }
                    594:
                    595: char *
                    596: routename4(in_addr_t in)
                    597: {
                    598:        char            *cp = NULL;
                    599:        struct in_addr   ina;
                    600:        struct hostent  *hp;
                    601:
                    602:        if (in == INADDR_ANY)
                    603:                cp = "default";
                    604:        if (!cp && !nflag) {
                    605:                if ((hp = gethostbyaddr((char *)&in,
                    606:                    sizeof(in), AF_INET)) != NULL) {
                    607:                        if ((cp = strchr(hp->h_name, '.')) &&
                    608:                            !strcmp(cp + 1, domain))
                    609:                                *cp = '\0';
                    610:                        cp = hp->h_name;
                    611:                }
                    612:        }
                    613:        ina.s_addr = in;
                    614:        strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
                    615:
                    616:        return (line);
                    617: }
                    618:
                    619: char *
                    620: routename6(struct sockaddr_in6 *sin6)
                    621: {
                    622:        int      niflags = 0;
                    623:
                    624:        if (nflag)
                    625:                niflags |= NI_NUMERICHOST;
                    626:        else
                    627:                niflags |= NI_NOFQDN;
                    628:
                    629:        if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
                    630:            line, sizeof(line), NULL, 0, niflags) != 0)
                    631:                strncpy(line, "invalid", sizeof(line));
                    632:
                    633:        return (line);
                    634: }
                    635:
                    636: /*
                    637:  * Return the name of the network whose address is given.
                    638:  * The address is assumed to be that of a net or subnet, not a host.
                    639:  */
                    640: char *
                    641: netname4(in_addr_t in, in_addr_t mask)
                    642: {
                    643:        char *cp = NULL;
                    644:        struct netent *np = NULL;
                    645:        int mbits;
                    646:
                    647:        in = ntohl(in);
                    648:        mask = ntohl(mask);
                    649:        if (!nflag && in != INADDR_ANY) {
                    650:                if ((np = getnetbyaddr(in, AF_INET)) != NULL)
                    651:                        cp = np->n_name;
                    652:        }
                    653:        if (in == INADDR_ANY)
                    654:                cp = "default";
                    655:        mbits = mask ? 33 - ffs(mask) : 0;
                    656:        if (cp)
                    657:                strlcpy(line, cp, sizeof(line));
                    658: #define C(x)   ((x) & 0xff)
                    659:        else if (mbits < 9)
                    660:                snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
                    661:        else if (mbits < 17)
                    662:                snprintf(line, sizeof(line), "%u.%u/%d",
                    663:                    C(in >> 24) , C(in >> 16), mbits);
                    664:        else if (mbits < 25)
                    665:                snprintf(line, sizeof(line), "%u.%u.%u/%d",
                    666:                    C(in >> 24), C(in >> 16), C(in >> 8), mbits);
                    667:        else
                    668:                snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
                    669:                    C(in >> 16), C(in >> 8), C(in), mbits);
                    670: #undef C
                    671:        return (line);
                    672: }
                    673:
                    674: char *
                    675: netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
                    676: {
                    677:        struct sockaddr_in6 sin6;
                    678:        u_char *p;
                    679:        int masklen, final = 0, illegal = 0;
                    680:        int i, lim, flag, error;
                    681:        char hbuf[NI_MAXHOST];
                    682:
                    683:        sin6 = *sa6;
                    684:
                    685:        flag = 0;
                    686:        masklen = 0;
                    687:        if (mask) {
                    688:                lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1.4       claudio   689:                lim = lim < (int)sizeof(struct in6_addr) ?
1.1       claudio   690:                    lim : sizeof(struct in6_addr);
                    691:                for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
                    692:                        if (final && *p) {
                    693:                                illegal++;
                    694:                                sin6.sin6_addr.s6_addr[i++] = 0x00;
                    695:                                continue;
                    696:                        }
                    697:
                    698:                        switch (*p & 0xff) {
                    699:                        case 0xff:
                    700:                                masklen += 8;
                    701:                                break;
                    702:                        case 0xfe:
                    703:                                masklen += 7;
                    704:                                final++;
                    705:                                break;
                    706:                        case 0xfc:
                    707:                                masklen += 6;
                    708:                                final++;
                    709:                                break;
                    710:                        case 0xf8:
                    711:                                masklen += 5;
                    712:                                final++;
                    713:                                break;
                    714:                        case 0xf0:
                    715:                                masklen += 4;
                    716:                                final++;
                    717:                                break;
                    718:                        case 0xe0:
                    719:                                masklen += 3;
                    720:                                final++;
                    721:                                break;
                    722:                        case 0xc0:
                    723:                                masklen += 2;
                    724:                                final++;
                    725:                                break;
                    726:                        case 0x80:
                    727:                                masklen += 1;
                    728:                                final++;
                    729:                                break;
                    730:                        case 0x00:
                    731:                                final++;
                    732:                                break;
                    733:                        default:
                    734:                                final++;
                    735:                                illegal++;
                    736:                                break;
                    737:                        }
                    738:
                    739:                        if (!illegal)
                    740:                                sin6.sin6_addr.s6_addr[i++] &= *p;
                    741:                        else
                    742:                                sin6.sin6_addr.s6_addr[i++] = 0x00;
                    743:                }
                    744:                while (i < sizeof(struct in6_addr))
                    745:                        sin6.sin6_addr.s6_addr[i++] = 0x00;
                    746:        } else
                    747:                masklen = 128;
                    748:
                    749:        if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
                    750:                return ("default");
                    751:
                    752:        if (illegal)
                    753:                warnx("illegal prefixlen");
                    754:
                    755:        if (nflag)
                    756:                flag |= NI_NUMERICHOST;
                    757:        error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
                    758:            hbuf, sizeof(hbuf), NULL, 0, flag);
                    759:        if (error)
                    760:                snprintf(hbuf, sizeof(hbuf), "invalid");
                    761:
                    762:        snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
                    763:        return (line);
                    764: }
                    765:
                    766: /*
                    767:  * Return the name of the network whose address is given.
                    768:  * The address is assumed to be that of a net or subnet, not a host.
                    769:  */
                    770: char *
                    771: netname(struct sockaddr *sa, struct sockaddr *mask)
                    772: {
                    773:        switch (sa->sa_family) {
                    774:
                    775:        case AF_INET:
                    776:                return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
                    777:                    ((struct sockaddr_in *)mask)->sin_addr.s_addr);
                    778:        case AF_INET6:
                    779:                return netname6((struct sockaddr_in6 *)sa,
                    780:                    (struct sockaddr_in6 *)mask);
                    781:        case AF_IPX:
                    782:                return (ipx_print(sa));
                    783:        case AF_LINK:
                    784:                return (link_print(sa));
                    785:        default:
                    786:                snprintf(line, sizeof(line), "af %d: %s",
                    787:                    sa->sa_family, any_ntoa(sa));
                    788:                break;
                    789:        }
                    790:        return (line);
                    791: }
                    792:
                    793: static const char hexlist[] = "0123456789abcdef";
                    794:
                    795: char *
                    796: any_ntoa(const struct sockaddr *sa)
                    797: {
                    798:        static char obuf[240];
                    799:        const char *in = sa->sa_data;
                    800:        char *out = obuf;
                    801:        int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
                    802:
                    803:        *out++ = 'Q';
                    804:        do {
                    805:                *out++ = hexlist[(*in >> 4) & 15];
                    806:                *out++ = hexlist[(*in++)    & 15];
                    807:                *out++ = '.';
                    808:        } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
                    809:        out[-1] = '\0';
                    810:        return (obuf);
                    811: }
                    812:
                    813: short ipx_nullh[] = {0,0,0};
                    814: short ipx_bh[] = {-1,-1,-1};
                    815:
                    816: char *
                    817: ipx_print(struct sockaddr *sa)
                    818: {
                    819:        struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
                    820:        struct ipx_addr work;
                    821:        union {
                    822:                union ipx_net   net_e;
                    823:                u_int32_t       long_e;
                    824:        } net;
                    825:        u_short port;
                    826:        static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
                    827:        char *host = "";
                    828:        char *p;
                    829:        u_char *q;
                    830:
                    831:        work = sipx->sipx_addr;
                    832:        port = ntohs(work.ipx_port);
                    833:        work.ipx_port = 0;
                    834:        net.net_e = work.ipx_net;
                    835:        if (ipx_nullhost(work) && net.long_e == 0) {
                    836:                if (!port)
                    837:                        return ("*.*");
                    838:                (void)snprintf(mybuf, sizeof(mybuf), "*.0x%XH", port);
                    839:                return (mybuf);
                    840:        }
                    841:
                    842:        if (memcmp(ipx_bh, work.ipx_host.c_host, 6) == 0)
                    843:                host = "any";
                    844:        else if (memcmp(ipx_nullh, work.ipx_host.c_host, 6) == 0)
                    845:                host = "*";
                    846:        else {
                    847:                q = work.ipx_host.c_host;
                    848:                (void)snprintf(chost, sizeof(chost), "%02X%02X%02X%02X%02X%02XH",
                    849:                        q[0], q[1], q[2], q[3], q[4], q[5]);
                    850:                for (p = chost; *p == '0' && p < chost + 12; p++)
                    851:                        /* void */;
                    852:                host = p;
                    853:        }
                    854:        if (port)
                    855:                (void)snprintf(cport, sizeof(cport), ".%XH", htons(port));
                    856:        else
                    857:                *cport = '\0';
                    858:
                    859:        (void)snprintf(mybuf, sizeof(mybuf), "%XH.%s%s",
                    860:            ntohl(net.long_e), host, cport);
                    861:        return (mybuf);
                    862: }
                    863:
                    864: char *
                    865: link_print(struct sockaddr *sa)
                    866: {
                    867:        struct sockaddr_dl      *sdl = (struct sockaddr_dl *)sa;
                    868:        u_char                  *lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
                    869:
                    870:        if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
                    871:            sdl->sdl_slen == 0) {
                    872:                (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
                    873:                return (line);
                    874:        }
                    875:        switch (sdl->sdl_type) {
                    876:        case IFT_ETHER:
                    877:        case IFT_CARP:
                    878:                return (ether_ntoa((struct ether_addr *)lla));
                    879:        default:
                    880:                return (link_ntoa(sdl));
                    881:        }
                    882: }
                    883:
                    884: void
                    885: index_pfk(struct sadb_msg *msg, void **headers)
                    886: {
                    887:        struct sadb_ext *ext;
                    888:
                    889:        for (ext = (struct sadb_ext *)(msg + 1);
                    890:            (size_t)((u_int8_t *)ext - (u_int8_t *)msg) <
                    891:            msg->sadb_msg_len * PFKEYV2_CHUNK && ext->sadb_ext_len > 0;
                    892:            ext = (struct sadb_ext *)((u_int8_t *)ext +
                    893:            ext->sadb_ext_len * PFKEYV2_CHUNK)) {
                    894:                switch (ext->sadb_ext_type) {
                    895:                case SADB_EXT_ADDRESS_SRC:
                    896:                        headers[SADB_EXT_ADDRESS_SRC] = (void *)ext;
                    897:                        break;
                    898:                case SADB_EXT_ADDRESS_DST:
                    899:                        headers[SADB_EXT_ADDRESS_DST] = (void *)ext;
                    900:                        break;
                    901:                case SADB_X_EXT_PROTOCOL:
                    902:                        headers[SADB_X_EXT_PROTOCOL] = (void *)ext;
                    903:                        break;
                    904:                case SADB_X_EXT_SRC_FLOW:
                    905:                        headers[SADB_X_EXT_SRC_FLOW] = (void *)ext;
                    906:                        break;
                    907:                case SADB_X_EXT_DST_FLOW:
                    908:                        headers[SADB_X_EXT_DST_FLOW] = (void *)ext;
                    909:                        break;
                    910:                case SADB_X_EXT_SRC_MASK:
                    911:                        headers[SADB_X_EXT_SRC_MASK] = (void *)ext;
                    912:                        break;
                    913:                case SADB_X_EXT_DST_MASK:
                    914:                        headers[SADB_X_EXT_DST_MASK] = (void *)ext;
                    915:                        break;
                    916:                case SADB_X_EXT_FLOW_TYPE:
                    917:                        headers[SADB_X_EXT_FLOW_TYPE] = (void *)ext;
                    918:                default:
                    919:                        /* Ignore. */
                    920:                        break;
                    921:                }
                    922:        }
                    923: }