Home | History | Annotate | Line # | Download | only in route
rtutil.c revision 1.5
      1  1.5       roy /*	$NetBSD: rtutil.c,v 1.5 2015/02/26 09:56:11 roy Exp $	*/
      2  1.1  christos /*	$OpenBSD: show.c,v 1.1 2006/05/27 19:16:37 claudio Exp $	*/
      3  1.1  christos 
      4  1.1  christos /*
      5  1.1  christos  * Copyright (c) 1983, 1988, 1993
      6  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     17  1.1  christos  *    may be used to endorse or promote products derived from this software
     18  1.1  christos  *    without specific prior written permission.
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1  christos  * SUCH DAMAGE.
     31  1.1  christos  */
     32  1.1  christos 
     33  1.1  christos #include <sys/param.h>
     34  1.1  christos #include <sys/protosw.h>
     35  1.1  christos #include <sys/socket.h>
     36  1.1  christos #include <sys/mbuf.h>
     37  1.1  christos #include <sys/sysctl.h>
     38  1.1  christos 
     39  1.1  christos #include <net/if.h>
     40  1.1  christos #include <net/if_dl.h>
     41  1.1  christos #include <net/if_types.h>
     42  1.1  christos #include <net/pfvar.h>
     43  1.1  christos #include <net/pfkeyv2.h>
     44  1.1  christos #include <net/route.h>
     45  1.1  christos #include <netinet/in.h>
     46  1.1  christos #include <netinet/if_ether.h>
     47  1.1  christos #include <netatalk/at.h>
     48  1.1  christos #include <netmpls/mpls.h>
     49  1.1  christos #include <arpa/inet.h>
     50  1.1  christos 
     51  1.1  christos #include <err.h>
     52  1.1  christos #include <errno.h>
     53  1.1  christos #include <netdb.h>
     54  1.1  christos #include <stdio.h>
     55  1.1  christos #include <stddef.h>
     56  1.1  christos #include <stdlib.h>
     57  1.1  christos #include <string.h>
     58  1.1  christos #include <unistd.h>
     59  1.1  christos 
     60  1.1  christos #include "prog_ops.h"
     61  1.1  christos #include "rtutil.h"
     62  1.1  christos 
     63  1.1  christos 
     64  1.1  christos #define PLEN    (LONG_BIT / 4 + 2)
     65  1.1  christos #define PFKEYV2_CHUNK sizeof(u_int64_t)
     66  1.1  christos static char *link_print(const struct sockaddr *);
     67  1.1  christos 
     68  1.1  christos /*
     69  1.1  christos  * Definitions for showing gateway flags.
     70  1.1  christos  */
     71  1.1  christos struct bits {
     72  1.1  christos 	int	b_mask;
     73  1.1  christos 	char	b_val;
     74  1.1  christos };
     75  1.1  christos static const struct bits bits[] = {
     76  1.1  christos 	{ RTF_UP,	'U' },
     77  1.1  christos 	{ RTF_GATEWAY,	'G' },
     78  1.1  christos 	{ RTF_HOST,	'H' },
     79  1.1  christos 	{ RTF_REJECT,	'R' },
     80  1.1  christos 	{ RTF_BLACKHOLE, 'B' },
     81  1.1  christos 	{ RTF_DYNAMIC,	'D' },
     82  1.1  christos 	{ RTF_MODIFIED,	'M' },
     83  1.1  christos 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     84  1.1  christos 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     85  1.1  christos 	{ RTF_CLONING,	'C' },
     86  1.1  christos 	{ RTF_XRESOLVE,	'X' },
     87  1.1  christos 	{ RTF_LLINFO,	'L' },
     88  1.1  christos 	{ RTF_STATIC,	'S' },
     89  1.1  christos 	{ RTF_PROTO1,	'1' },
     90  1.1  christos 	{ RTF_PROTO2,	'2' },
     91  1.1  christos 	/* { RTF_PROTO3,	'3' }, */
     92  1.1  christos 	{ RTF_CLONED,	'c' },
     93  1.1  christos 	/* { RTF_JUMBO,	'J' }, */
     94  1.1  christos 	{ RTF_ANNOUNCE,	'p' },
     95  1.5       roy 	{ RTF_LOCAL, 'l'},
     96  1.1  christos 	{ 0, 0 }
     97  1.1  christos };
     98  1.1  christos 
     99  1.1  christos #ifndef SMALL
    100  1.1  christos static void p_tag(const struct sockaddr *sa);
    101  1.1  christos #endif
    102  1.1  christos static void p_rtentry(struct rt_msghdr *, int, int);
    103  1.1  christos 
    104  1.1  christos /*
    105  1.1  christos  * Print routing tables.
    106  1.1  christos  */
    107  1.1  christos void
    108  1.1  christos p_rttables(int paf, int flags, int pflags, int interesting)
    109  1.1  christos {
    110  1.1  christos 	struct rt_msghdr *rtm;
    111  1.1  christos 	char *buf = NULL, *next, *lim = NULL;
    112  1.1  christos 	size_t needed;
    113  1.1  christos 	int mib[6];
    114  1.1  christos 	struct sockaddr *sa;
    115  1.1  christos 
    116  1.1  christos 	mib[0] = CTL_NET;
    117  1.1  christos 	mib[1] = PF_ROUTE;
    118  1.1  christos 	mib[2] = 0;
    119  1.1  christos 	mib[3] = paf;
    120  1.1  christos 	mib[4] = NET_RT_DUMP;
    121  1.1  christos 	mib[5] = 0;
    122  1.1  christos 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    123  1.1  christos 		err(1, "route-sysctl-estimate");
    124  1.1  christos 	if (needed > 0) {
    125  1.1  christos 		if ((buf = malloc(needed)) == 0)
    126  1.1  christos 			err(1, NULL);
    127  1.1  christos 		if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    128  1.1  christos 			err(1, "sysctl of routing table");
    129  1.1  christos 		lim = buf + needed;
    130  1.1  christos 	}
    131  1.1  christos 
    132  1.1  christos 	printf("Routing tables\n");
    133  1.1  christos 
    134  1.1  christos 	if (buf) {
    135  1.1  christos 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    136  1.1  christos 			rtm = (struct rt_msghdr *)next;
    137  1.1  christos 			sa = (struct sockaddr *)(rtm + 1);
    138  1.1  christos 			if ((rtm->rtm_flags & pflags) != pflags)
    139  1.1  christos 				continue;
    140  1.1  christos 			if (paf != AF_UNSPEC && sa->sa_family != paf)
    141  1.1  christos 				continue;
    142  1.1  christos 			p_rtentry(rtm, flags, interesting);
    143  1.1  christos 		}
    144  1.1  christos 		free(buf);
    145  1.1  christos 		buf = NULL;
    146  1.1  christos 	}
    147  1.1  christos 
    148  1.1  christos 	if (paf != 0 && paf != PF_KEY)
    149  1.1  christos 		return;
    150  1.1  christos 
    151  1.1  christos #if 0 /* XXX-elad */
    152  1.1  christos 	mib[0] = CTL_NET;
    153  1.1  christos 	mib[1] = PF_KEY;
    154  1.1  christos 	mib[2] = PF_KEY_V2;
    155  1.1  christos 	mib[3] = NET_KEY_SPD_DUMP;
    156  1.1  christos 	mib[4] = mib[5] = 0;
    157  1.1  christos 
    158  1.1  christos 	if (prog_sysctl(mib, 4, NULL, &needed, NULL, 0) == -1) {
    159  1.1  christos 		if (errno == ENOPROTOOPT)
    160  1.1  christos 			return;
    161  1.1  christos 		err(1, "spd-sysctl-estimate");
    162  1.1  christos 	}
    163  1.1  christos 	if (needed > 0) {
    164  1.1  christos 		if ((buf = malloc(needed)) == 0)
    165  1.1  christos 			err(1, NULL);
    166  1.1  christos 		if (prog_sysctl(mib, 4, buf, &needed, NULL, 0) == -1)
    167  1.1  christos 			err(1,"sysctl of spd");
    168  1.1  christos 		lim = buf + needed;
    169  1.1  christos 	}
    170  1.1  christos 
    171  1.1  christos 	if (buf) {
    172  1.1  christos 		printf("\nEncap:\n");
    173  1.1  christos 
    174  1.1  christos 		for (next = buf; next < lim; next += msg->sadb_msg_len *
    175  1.1  christos 		    PFKEYV2_CHUNK) {
    176  1.1  christos 			msg = (struct sadb_msg *)next;
    177  1.1  christos 			if (msg->sadb_msg_len == 0)
    178  1.1  christos 				break;
    179  1.1  christos 			p_pfkentry(msg);
    180  1.1  christos 		}
    181  1.1  christos 		free(buf);
    182  1.1  christos 		buf = NULL;
    183  1.1  christos 	}
    184  1.1  christos #endif /* 0 */
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos /*
    188  1.1  christos  * column widths; each followed by one space
    189  1.1  christos  * width of destination/gateway column
    190  1.2  christos  * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 = 34
    191  1.2  christos  * strlen("aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh") == 39
    192  1.1  christos  */
    193  1.1  christos #ifndef INET6
    194  1.1  christos #define	WID_DST(af)	18	/* width of destination column */
    195  1.1  christos #define	WID_GW(af)	18	/* width of gateway column */
    196  1.1  christos #else
    197  1.2  christos #define	WID_DST(af)	((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 39 : 18) : 18)
    198  1.1  christos #define	WID_GW(af)	((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 30 : 18) : 18)
    199  1.1  christos #endif
    200  1.1  christos 
    201  1.1  christos /*
    202  1.1  christos  * Print header for routing table columns.
    203  1.1  christos  */
    204  1.1  christos void
    205  1.1  christos p_rthdr(int paf, int flags)
    206  1.1  christos {
    207  1.1  christos #ifndef SMALL
    208  1.1  christos 	if (flags & RT_AFLAG)
    209  1.1  christos 		printf("%-*.*s ", PLEN, PLEN, "Address");
    210  1.1  christos 	if (paf == PF_KEY) {
    211  1.1  christos 		printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
    212  1.1  christos 		    "Source", "Port", "Destination",
    213  1.1  christos 		    "Port", "Proto", "SA(Address/Proto/Type/Direction)");
    214  1.1  christos 		return;
    215  1.1  christos 	}
    216  1.1  christos 	if (flags & RT_TFLAG) {
    217  1.1  christos 	    printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %7.7s"
    218  1.1  christos 		" %s\n", WID_DST(paf), WID_DST(paf), "Destination",
    219  1.1  christos 		WID_GW(paf), WID_GW(paf), "Gateway",
    220  1.1  christos 		"Flags", "Refs", "Use", "Mtu", "Tag", "Interface");
    221  1.1  christos 	    return;
    222  1.1  christos 	}
    223  1.1  christos #endif
    224  1.1  christos #ifndef SMALL
    225  1.1  christos 	printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %s\n",
    226  1.1  christos 	    WID_DST(paf), WID_DST(paf), "Destination",
    227  1.1  christos 	    WID_GW(paf), WID_GW(paf), "Gateway",
    228  1.1  christos 	    "Flags", "Refs", "Use", "Mtu", "Interface");
    229  1.1  christos #else
    230  1.1  christos 	printf("%-*.*s %-*.*s %-6.6s\n",
    231  1.1  christos 	    WID_DST(paf), WID_DST(paf), "Destination",
    232  1.1  christos 	    WID_GW(paf), WID_GW(paf), "Gateway",
    233  1.1  christos 	    "Flags");
    234  1.1  christos #endif
    235  1.1  christos }
    236  1.1  christos 
    237  1.1  christos static void
    238  1.1  christos get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
    239  1.1  christos {
    240  1.1  christos 	int	i;
    241  1.1  christos 
    242  1.1  christos 	for (i = 0; i < RTAX_MAX; i++) {
    243  1.1  christos 		if (addrs & (1 << i)) {
    244  1.1  christos 			rti_info[i] = sa;
    245  1.1  christos 			sa = (struct sockaddr *)((char *)(sa) +
    246  1.1  christos 			    RT_ROUNDUP(sa->sa_len));
    247  1.1  christos 		} else
    248  1.1  christos 			rti_info[i] = NULL;
    249  1.1  christos 	}
    250  1.1  christos }
    251  1.1  christos 
    252  1.1  christos /*
    253  1.1  christos  * Print a routing table entry.
    254  1.1  christos  */
    255  1.1  christos static void
    256  1.1  christos p_rtentry(struct rt_msghdr *rtm, int flags, int interesting)
    257  1.1  christos {
    258  1.1  christos 	static int	 old_af = -1;
    259  1.1  christos 	struct sockaddr	*sa = (struct sockaddr *)(rtm + 1);
    260  1.1  christos 	struct sockaddr	*mask, *rti_info[RTAX_MAX];
    261  1.1  christos #ifndef SMALL
    262  1.1  christos 	char		 ifbuf[IF_NAMESIZE];
    263  1.1  christos #endif
    264  1.1  christos 
    265  1.3  christos 	if ((flags & RT_LFLAG) && (rtm->rtm_flags & RTF_LLINFO))
    266  1.3  christos 		return;
    267  1.3  christos 
    268  1.1  christos 	if (old_af != sa->sa_family) {
    269  1.1  christos 		old_af = sa->sa_family;
    270  1.1  christos 		p_family(sa->sa_family);
    271  1.1  christos 		p_rthdr(sa->sa_family, flags);
    272  1.1  christos 	}
    273  1.1  christos 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    274  1.1  christos 
    275  1.1  christos 	mask = rti_info[RTAX_NETMASK];
    276  1.1  christos 	if ((sa = rti_info[RTAX_DST]) == NULL)
    277  1.1  christos 		return;
    278  1.1  christos 
    279  1.1  christos 	p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family), flags);
    280  1.1  christos 	p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
    281  1.1  christos 	    WID_GW(sa->sa_family), flags);
    282  1.1  christos 	p_flags(rtm->rtm_flags & interesting);
    283  1.1  christos #if 0 /* XXX-elad */
    284  1.1  christos 	printf("%6d %8"PRId64" ", (int)rtm->rtm_rmx.rmx_refcnt,
    285  1.1  christos 	    rtm->rtm_rmx.rmx_pksent);
    286  1.1  christos #else
    287  1.1  christos 	printf("%6s %8s ", "-", "-");
    288  1.1  christos #endif
    289  1.1  christos #ifndef SMALL
    290  1.1  christos 	if (rtm->rtm_rmx.rmx_mtu)
    291  1.1  christos 		printf("%6"PRId64, rtm->rtm_rmx.rmx_mtu);
    292  1.1  christos 	else
    293  1.1  christos 		printf("%6s", "-");
    294  1.1  christos 	putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
    295  1.1  christos 	if (flags & RT_TFLAG)
    296  1.1  christos 		p_tag(rti_info[RTAX_TAG]);
    297  1.1  christos 	printf(" %.16s", if_indextoname(rtm->rtm_index, ifbuf));
    298  1.1  christos 	putchar('\n');
    299  1.1  christos 	if (flags & RT_VFLAG)
    300  1.1  christos 		p_rtrmx(&rtm->rtm_rmx);
    301  1.1  christos #endif
    302  1.1  christos }
    303  1.1  christos 
    304  1.1  christos /*
    305  1.1  christos  * Print address family header before a section of the routing table.
    306  1.1  christos  */
    307  1.1  christos void
    308  1.1  christos p_family(int paf)
    309  1.1  christos {
    310  1.1  christos 	const char *afname;
    311  1.1  christos 
    312  1.1  christos 	switch (paf) {
    313  1.1  christos 	case AF_INET:
    314  1.1  christos 		afname = "Internet";
    315  1.1  christos 		break;
    316  1.1  christos #ifdef INET6
    317  1.1  christos 	case AF_INET6:
    318  1.1  christos 		afname = "Internet6";
    319  1.1  christos 		break;
    320  1.1  christos #endif
    321  1.1  christos 	case PF_KEY:
    322  1.1  christos 		afname = "Encap";
    323  1.1  christos 		break;
    324  1.1  christos 	case AF_APPLETALK:
    325  1.1  christos 		afname = "AppleTalk";
    326  1.1  christos 		break;
    327  1.1  christos #ifndef SMALL
    328  1.1  christos 	case AF_MPLS:
    329  1.1  christos 		afname = "MPLS";
    330  1.1  christos 		break;
    331  1.1  christos #endif
    332  1.1  christos 	default:
    333  1.1  christos 		afname = NULL;
    334  1.1  christos 		break;
    335  1.1  christos 	}
    336  1.1  christos 	if (afname)
    337  1.1  christos 		printf("\n%s:\n", afname);
    338  1.1  christos 	else
    339  1.1  christos 		printf("\nProtocol Family %d:\n", paf);
    340  1.1  christos }
    341  1.1  christos 
    342  1.1  christos void
    343  1.1  christos p_sockaddr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags,
    344  1.1  christos     int width, int flags)
    345  1.1  christos {
    346  1.1  christos 	char *cp;
    347  1.1  christos 
    348  1.1  christos 	switch (sa->sa_family) {
    349  1.1  christos #ifdef INET6
    350  1.1  christos 	case AF_INET6:
    351  1.1  christos 	    {
    352  1.1  christos 		struct sockaddr_in6 sa6 = *(const struct sockaddr_in6 *)sa;
    353  1.1  christos 
    354  1.1  christos 		inet6_getscopeid(&sa6, INET6_IS_ADDR_LINKLOCAL|
    355  1.1  christos 		    INET6_IS_ADDR_MC_LINKLOCAL);
    356  1.1  christos 		if (rflags & RTF_HOST)
    357  1.1  christos 			cp = routename((const struct sockaddr *)&sa6, flags);
    358  1.1  christos 		else
    359  1.1  christos 			cp = netname((const struct sockaddr *)&sa6, mask, flags);
    360  1.1  christos 		break;
    361  1.1  christos 	    }
    362  1.1  christos #endif
    363  1.1  christos 	default:
    364  1.1  christos 		if ((rflags & RTF_HOST) || mask == NULL)
    365  1.1  christos 			cp = routename(sa, flags);
    366  1.1  christos 		else
    367  1.1  christos 			cp = netname(sa, mask, flags);
    368  1.1  christos 		break;
    369  1.1  christos 	}
    370  1.1  christos 	if (width < 0)
    371  1.1  christos 		printf("%s", cp);
    372  1.1  christos 	else {
    373  1.1  christos 		if (flags & RT_NFLAG)
    374  1.1  christos 			printf("%-*s ", width, cp);
    375  1.1  christos 		else
    376  1.1  christos 			printf("%-*.*s ", width, width, cp);
    377  1.1  christos 	}
    378  1.1  christos }
    379  1.1  christos 
    380  1.1  christos void
    381  1.1  christos p_flags(int f)
    382  1.1  christos {
    383  1.1  christos 	char name[33], *flags;
    384  1.1  christos 	const struct bits *p = bits;
    385  1.1  christos 
    386  1.1  christos 	for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
    387  1.1  christos 		if (p->b_mask & f)
    388  1.1  christos 			*flags++ = p->b_val;
    389  1.1  christos 	*flags = '\0';
    390  1.1  christos 	printf("%-6.6s ", name);
    391  1.1  christos }
    392  1.1  christos 
    393  1.1  christos #ifndef SMALL
    394  1.1  christos void
    395  1.1  christos p_rtrmx(const struct rt_metrics *rmx)
    396  1.1  christos {
    397  1.1  christos 	printf("\texpire   %10"PRId64"%c  recvpipe %10"PRIu64"%c  "
    398  1.1  christos 	    "sendpipe %10"PRIu64"%c\n",
    399  1.1  christos 	    (int64_t)rmx->rmx_expire,
    400  1.1  christos 	    (rmx->rmx_locks & RTV_EXPIRE) ? 'L' : ' ', rmx->rmx_recvpipe,
    401  1.1  christos 	    (rmx->rmx_locks & RTV_RPIPE) ? 'L' : ' ', rmx->rmx_sendpipe,
    402  1.1  christos 	    (rmx->rmx_locks & RTV_SPIPE) ? 'L' : ' ');
    403  1.1  christos 	printf("\tssthresh %10"PRIu64"%c  rtt      %10"PRIu64"%c  "
    404  1.1  christos 	    "rttvar   %10"PRIu64"%c\n", rmx->rmx_ssthresh,
    405  1.1  christos 	    (rmx->rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
    406  1.1  christos 	    rmx->rmx_rtt, (rmx->rmx_locks & RTV_RTT) ? 'L' : ' ',
    407  1.1  christos 	    rmx->rmx_rttvar, (rmx->rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
    408  1.1  christos 	printf("\thopcount %10"PRIu64"%c\n",
    409  1.1  christos 	    rmx->rmx_hopcount, (rmx->rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
    410  1.1  christos }
    411  1.1  christos 
    412  1.1  christos static void
    413  1.1  christos p_tag(const struct sockaddr *sa)
    414  1.1  christos {
    415  1.1  christos 	char *line;
    416  1.1  christos 
    417  1.1  christos 	if (sa == NULL || sa->sa_family != AF_MPLS) {
    418  1.1  christos 		printf("%7s", "-");
    419  1.1  christos 		return;
    420  1.1  christos 	}
    421  1.1  christos 	line = mpls_ntoa(sa);
    422  1.1  christos 	if (strlen(line) < 7)
    423  1.1  christos 		printf("%7s", line);
    424  1.1  christos 	else
    425  1.1  christos 		printf("%s", line);
    426  1.1  christos }
    427  1.1  christos #endif
    428  1.1  christos 
    429  1.1  christos static char line[MAXHOSTNAMELEN];
    430  1.1  christos static char domain[MAXHOSTNAMELEN];
    431  1.1  christos 
    432  1.1  christos char *
    433  1.1  christos routename(const struct sockaddr *sa, int flags)
    434  1.1  christos {
    435  1.1  christos 	char *cp = NULL;
    436  1.1  christos 	static int first = 1;
    437  1.1  christos 
    438  1.1  christos 	if (first) {
    439  1.1  christos 		first = 0;
    440  1.1  christos 		if (gethostname(domain, sizeof(domain)) == 0 &&
    441  1.1  christos 		    (cp = strchr(domain, '.')))
    442  1.1  christos 			(void)strlcpy(domain, cp + 1, sizeof(domain));
    443  1.1  christos 		else
    444  1.1  christos 			domain[0] = '\0';
    445  1.1  christos 		cp = NULL;
    446  1.1  christos 	}
    447  1.1  christos 
    448  1.1  christos 	if (sa->sa_len == 0) {
    449  1.1  christos 		(void)strlcpy(line, "default", sizeof(line));
    450  1.1  christos 		return (line);
    451  1.1  christos 	}
    452  1.1  christos 
    453  1.1  christos 	switch (sa->sa_family) {
    454  1.1  christos 	case AF_INET:
    455  1.1  christos 		return routename4(
    456  1.1  christos 		    ((const struct sockaddr_in *)sa)->sin_addr.s_addr,
    457  1.1  christos 		    flags);
    458  1.1  christos #ifdef INET6
    459  1.1  christos 	case AF_INET6:
    460  1.1  christos 	    {
    461  1.1  christos 		struct sockaddr_in6 sin6;
    462  1.1  christos 
    463  1.1  christos 		memset(&sin6, 0, sizeof(sin6));
    464  1.1  christos 		memcpy(&sin6, sa, sa->sa_len);
    465  1.1  christos 		sin6.sin6_len = sizeof(struct sockaddr_in6);
    466  1.1  christos 		sin6.sin6_family = AF_INET6;
    467  1.1  christos 		if (sa->sa_len == sizeof(struct sockaddr_in6))
    468  1.1  christos 			inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
    469  1.1  christos 			    INET6_IS_ADDR_MC_LINKLOCAL);
    470  1.1  christos 		return routename6(&sin6, flags);
    471  1.1  christos 	    }
    472  1.1  christos #endif
    473  1.1  christos 	case AF_LINK:
    474  1.1  christos 		return link_print(sa);
    475  1.1  christos 
    476  1.1  christos #ifndef SMALL
    477  1.1  christos 	case AF_MPLS:
    478  1.1  christos 		return mpls_ntoa(sa);
    479  1.1  christos 
    480  1.1  christos 	case AF_APPLETALK:
    481  1.1  christos 		(void)snprintf(line, sizeof(line), "atalk %d.%d",
    482  1.1  christos 		    ((const struct sockaddr_at *)sa)->sat_addr.s_net,
    483  1.1  christos 		    ((const struct sockaddr_at *)sa)->sat_addr.s_node);
    484  1.1  christos 		break;
    485  1.1  christos #endif
    486  1.1  christos 
    487  1.1  christos #if 0 /* XXX-elad */
    488  1.1  christos 	case AF_UNSPEC:
    489  1.1  christos 		if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
    490  1.1  christos 			static char name[RTLABEL_LEN];
    491  1.1  christos 			struct sockaddr_rtlabel *sr;
    492  1.1  christos 
    493  1.1  christos 			sr = (struct sockaddr_rtlabel *)sa;
    494  1.1  christos 			strlcpy(name, sr->sr_label, sizeof(name));
    495  1.1  christos 			return (name);
    496  1.1  christos 		}
    497  1.1  christos 		/* FALLTHROUGH */
    498  1.1  christos #endif
    499  1.1  christos 	default:
    500  1.1  christos 		(void)snprintf(line, sizeof(line), "(%d) %s",
    501  1.1  christos 		    sa->sa_family, any_ntoa(sa));
    502  1.1  christos 		break;
    503  1.1  christos 	}
    504  1.1  christos 	return (line);
    505  1.1  christos }
    506  1.1  christos 
    507  1.1  christos char *
    508  1.1  christos routename4(in_addr_t in, int flags)
    509  1.1  christos {
    510  1.1  christos 	const char	*cp = NULL;
    511  1.1  christos 	struct in_addr	 ina;
    512  1.1  christos 	struct hostent	*hp;
    513  1.1  christos 
    514  1.1  christos 	if (in == INADDR_ANY)
    515  1.1  christos 		cp = "default";
    516  1.1  christos 	if (!cp && (flags & RT_NFLAG) == 0) {
    517  1.1  christos 		if ((hp = gethostbyaddr((char *)&in,
    518  1.1  christos 		    sizeof(in), AF_INET)) != NULL) {
    519  1.1  christos 			char *p;
    520  1.1  christos 			if ((p = strchr(hp->h_name, '.')) &&
    521  1.1  christos 			    !strcmp(p + 1, domain))
    522  1.1  christos 				*p = '\0';
    523  1.1  christos 			cp = hp->h_name;
    524  1.1  christos 		}
    525  1.1  christos 	}
    526  1.1  christos 	ina.s_addr = in;
    527  1.1  christos 	strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
    528  1.1  christos 
    529  1.1  christos 	return (line);
    530  1.1  christos }
    531  1.1  christos 
    532  1.1  christos #ifdef INET6
    533  1.1  christos char *
    534  1.1  christos routename6(const struct sockaddr_in6 *sin6, int flags)
    535  1.1  christos {
    536  1.1  christos 	int	 niflags = 0;
    537  1.1  christos 
    538  1.1  christos 	if ((flags & RT_NFLAG))
    539  1.1  christos 		niflags |= NI_NUMERICHOST;
    540  1.1  christos 	else
    541  1.1  christos 		niflags |= NI_NOFQDN;
    542  1.1  christos 
    543  1.1  christos 	if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
    544  1.1  christos 	    line, sizeof(line), NULL, 0, niflags) != 0)
    545  1.1  christos 		strncpy(line, "invalid", sizeof(line));
    546  1.1  christos 
    547  1.1  christos 	return (line);
    548  1.1  christos }
    549  1.1  christos #endif
    550  1.1  christos 
    551  1.1  christos /*
    552  1.1  christos  * Return the name of the network whose address is given.
    553  1.1  christos  * The address is assumed to be that of a net or subnet, not a host.
    554  1.1  christos  */
    555  1.1  christos char *
    556  1.4  christos netname4(const struct sockaddr_in* sa4, const struct sockaddr_in *mask, int flags)
    557  1.1  christos {
    558  1.1  christos 	const char *cp = NULL;
    559  1.1  christos 	struct netent *np = NULL;
    560  1.1  christos 	int mbits;
    561  1.4  christos 	in_addr_t in = sa4->sin_addr.s_addr;
    562  1.4  christos 
    563  1.4  christos 	if (mask) {
    564  1.4  christos 		in_addr_t m = mask->sin_addr.s_addr ;
    565  1.4  christos 		m = ntohl(m);
    566  1.4  christos 		mbits = m ? 33 - ffs(m) : 0;
    567  1.4  christos 	} else
    568  1.4  christos 		mbits = 0;
    569  1.1  christos 
    570  1.1  christos 	in = ntohl(in);
    571  1.4  christos 	if (in == INADDR_ANY && !mbits)
    572  1.4  christos 		cp = "default";
    573  1.4  christos 	else if (!(flags & RT_NFLAG) && in != INADDR_ANY) {
    574  1.1  christos 		if ((np = getnetbyaddr(in, AF_INET)) != NULL)
    575  1.1  christos 			cp = np->n_name;
    576  1.1  christos 	}
    577  1.1  christos 	if (cp)
    578  1.1  christos 		strlcpy(line, cp, sizeof(line));
    579  1.1  christos #define C(x)	((x) & 0xff)
    580  1.1  christos 	else if (mbits < 9)
    581  1.1  christos 		snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
    582  1.1  christos 	else if (mbits < 17)
    583  1.1  christos 		snprintf(line, sizeof(line), "%u.%u/%d",
    584  1.1  christos 		    C(in >> 24) , C(in >> 16), mbits);
    585  1.1  christos 	else if (mbits < 25)
    586  1.1  christos 		snprintf(line, sizeof(line), "%u.%u.%u/%d",
    587  1.1  christos 		    C(in >> 24), C(in >> 16), C(in >> 8), mbits);
    588  1.1  christos 	else
    589  1.1  christos 		snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
    590  1.1  christos 		    C(in >> 16), C(in >> 8), C(in), mbits);
    591  1.1  christos #undef C
    592  1.4  christos 	return line;
    593  1.1  christos }
    594  1.1  christos 
    595  1.1  christos #ifdef INET6
    596  1.1  christos char *
    597  1.1  christos netname6(const struct sockaddr_in6 *sa6, const struct sockaddr_in6 *mask, int flags)
    598  1.1  christos {
    599  1.1  christos 	struct sockaddr_in6 sin6;
    600  1.1  christos 	const u_char *p;
    601  1.1  christos 	int masklen, final = 0, illegal = 0;
    602  1.1  christos 	int i, lim, flag, error;
    603  1.1  christos 	char hbuf[NI_MAXHOST];
    604  1.1  christos 
    605  1.1  christos 	sin6 = *sa6;
    606  1.1  christos 
    607  1.1  christos 	flag = 0;
    608  1.1  christos 	masklen = 0;
    609  1.1  christos 	if (mask) {
    610  1.1  christos 		lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
    611  1.1  christos 		if (lim < 0)
    612  1.1  christos 			lim = 0;
    613  1.1  christos 		else if (lim > (int)sizeof(struct in6_addr))
    614  1.1  christos 			lim = sizeof(struct in6_addr);
    615  1.1  christos 		for (p = (const u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
    616  1.1  christos 			if (final && *p) {
    617  1.1  christos 				illegal++;
    618  1.1  christos 				sin6.sin6_addr.s6_addr[i++] = 0x00;
    619  1.1  christos 				continue;
    620  1.1  christos 			}
    621  1.1  christos 
    622  1.1  christos 			switch (*p & 0xff) {
    623  1.1  christos 			case 0xff:
    624  1.1  christos 				masklen += 8;
    625  1.1  christos 				break;
    626  1.1  christos 			case 0xfe:
    627  1.1  christos 				masklen += 7;
    628  1.1  christos 				final++;
    629  1.1  christos 				break;
    630  1.1  christos 			case 0xfc:
    631  1.1  christos 				masklen += 6;
    632  1.1  christos 				final++;
    633  1.1  christos 				break;
    634  1.1  christos 			case 0xf8:
    635  1.1  christos 				masklen += 5;
    636  1.1  christos 				final++;
    637  1.1  christos 				break;
    638  1.1  christos 			case 0xf0:
    639  1.1  christos 				masklen += 4;
    640  1.1  christos 				final++;
    641  1.1  christos 				break;
    642  1.1  christos 			case 0xe0:
    643  1.1  christos 				masklen += 3;
    644  1.1  christos 				final++;
    645  1.1  christos 				break;
    646  1.1  christos 			case 0xc0:
    647  1.1  christos 				masklen += 2;
    648  1.1  christos 				final++;
    649  1.1  christos 				break;
    650  1.1  christos 			case 0x80:
    651  1.1  christos 				masklen += 1;
    652  1.1  christos 				final++;
    653  1.1  christos 				break;
    654  1.1  christos 			case 0x00:
    655  1.1  christos 				final++;
    656  1.1  christos 				break;
    657  1.1  christos 			default:
    658  1.1  christos 				final++;
    659  1.1  christos 				illegal++;
    660  1.1  christos 				break;
    661  1.1  christos 			}
    662  1.1  christos 
    663  1.1  christos 			if (!illegal)
    664  1.1  christos 				sin6.sin6_addr.s6_addr[i++] &= *p;
    665  1.1  christos 			else
    666  1.1  christos 				sin6.sin6_addr.s6_addr[i++] = 0x00;
    667  1.1  christos 		}
    668  1.1  christos 		while (i < (int)sizeof(struct in6_addr))
    669  1.1  christos 			sin6.sin6_addr.s6_addr[i++] = 0x00;
    670  1.1  christos 	} else
    671  1.1  christos 		masklen = 128;
    672  1.1  christos 
    673  1.1  christos 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
    674  1.1  christos 		snprintf(line, sizeof(line), "default");
    675  1.1  christos 		return (line);
    676  1.1  christos 	}
    677  1.1  christos 
    678  1.1  christos 	if (illegal)
    679  1.1  christos 		warnx("illegal prefixlen");
    680  1.1  christos 
    681  1.1  christos 	if (flags & RT_NFLAG)
    682  1.1  christos 		flag |= NI_NUMERICHOST;
    683  1.1  christos 	error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    684  1.1  christos 	    hbuf, sizeof(hbuf), NULL, 0, flag);
    685  1.1  christos 	if (error)
    686  1.1  christos 		snprintf(hbuf, sizeof(hbuf), "invalid");
    687  1.1  christos 
    688  1.1  christos 	snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
    689  1.1  christos 	return (line);
    690  1.1  christos }
    691  1.1  christos #endif
    692  1.1  christos 
    693  1.1  christos /*
    694  1.1  christos  * Return the name of the network whose address is given.
    695  1.1  christos  * The address is assumed to be that of a net or subnet, not a host.
    696  1.1  christos  */
    697  1.1  christos char *
    698  1.1  christos netname(const struct sockaddr *sa, const struct sockaddr *mask, int flags)
    699  1.1  christos {
    700  1.1  christos 	switch (sa->sa_family) {
    701  1.1  christos 
    702  1.1  christos 	case AF_INET:
    703  1.4  christos 		return netname4((const struct sockaddr_in *)sa,
    704  1.4  christos 		    (const struct sockaddr_in *)mask, flags);
    705  1.1  christos #ifdef INET6
    706  1.1  christos 	case AF_INET6:
    707  1.1  christos 		return netname6((const struct sockaddr_in6 *)sa,
    708  1.1  christos 		    (const struct sockaddr_in6 *)mask, flags);
    709  1.1  christos #endif
    710  1.1  christos 	case AF_LINK:
    711  1.1  christos 		return link_print(sa);
    712  1.1  christos 	default:
    713  1.1  christos 		snprintf(line, sizeof(line), "af %d: %s",
    714  1.1  christos 		    sa->sa_family, any_ntoa(sa));
    715  1.1  christos 		break;
    716  1.1  christos 	}
    717  1.1  christos 	return (line);
    718  1.1  christos }
    719  1.1  christos 
    720  1.1  christos static const char hexlist[] = "0123456789abcdef";
    721  1.1  christos 
    722  1.1  christos char *
    723  1.1  christos any_ntoa(const struct sockaddr *sa)
    724  1.1  christos {
    725  1.1  christos 	static char obuf[240];
    726  1.1  christos 	const char *in = sa->sa_data;
    727  1.1  christos 	char *out = obuf;
    728  1.1  christos 	int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
    729  1.1  christos 
    730  1.1  christos 	*out++ = 'Q';
    731  1.1  christos 	do {
    732  1.1  christos 		*out++ = hexlist[(*in >> 4) & 15];
    733  1.1  christos 		*out++ = hexlist[(*in++)    & 15];
    734  1.1  christos 		*out++ = '.';
    735  1.1  christos 	} while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
    736  1.1  christos 	out[-1] = '\0';
    737  1.1  christos 	return (obuf);
    738  1.1  christos }
    739  1.1  christos 
    740  1.1  christos static char *
    741  1.1  christos link_print(const struct sockaddr *sa)
    742  1.1  christos {
    743  1.1  christos 	const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)sa;
    744  1.1  christos 	const u_char *lla = (const u_char *)sdl->sdl_data + sdl->sdl_nlen;
    745  1.1  christos 
    746  1.1  christos 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
    747  1.1  christos 	    sdl->sdl_slen == 0) {
    748  1.1  christos 		(void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
    749  1.1  christos 		return (line);
    750  1.1  christos 	}
    751  1.1  christos 	switch (sdl->sdl_type) {
    752  1.1  christos 	case IFT_ETHER:
    753  1.1  christos 	case IFT_CARP:
    754  1.1  christos 		return ether_ntoa((const struct ether_addr *)lla);
    755  1.1  christos 	default:
    756  1.1  christos 		return link_ntoa(sdl);
    757  1.1  christos 	}
    758  1.1  christos }
    759  1.1  christos 
    760  1.1  christos #ifndef SMALL
    761  1.1  christos char *
    762  1.1  christos mpls_ntoa(const struct sockaddr *sa)
    763  1.1  christos {
    764  1.1  christos 	static char obuf[16];
    765  1.1  christos 	size_t olen;
    766  1.1  christos 	const union mpls_shim *pms;
    767  1.1  christos 	union mpls_shim ms;
    768  1.1  christos 	int psize = sizeof(struct sockaddr_mpls);
    769  1.1  christos 
    770  1.1  christos 	pms = &((const struct sockaddr_mpls*)sa)->smpls_addr;
    771  1.1  christos 	ms.s_addr = ntohl(pms->s_addr);
    772  1.1  christos 
    773  1.1  christos 	snprintf(obuf, sizeof(obuf), "%u", ms.shim.label);
    774  1.1  christos 
    775  1.1  christos 	while(psize < sa->sa_len) {
    776  1.1  christos 		pms++;
    777  1.1  christos 		ms.s_addr = ntohl(pms->s_addr);
    778  1.1  christos 		olen = strlen(obuf);
    779  1.1  christos 		snprintf(obuf + olen, sizeof(obuf) - olen, ",%u",
    780  1.1  christos 		    ms.shim.label);
    781  1.1  christos 		psize+=sizeof(ms);
    782  1.1  christos 	}
    783  1.1  christos 	return obuf;
    784  1.1  christos }
    785  1.1  christos #endif
    786  1.1  christos 
    787  1.1  christos void
    788  1.1  christos p_addr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags, int flags)
    789  1.1  christos {
    790  1.1  christos 	p_sockaddr(sa, mask, rflags, WID_DST(sa->sa_family), flags);
    791  1.1  christos }
    792  1.1  christos 
    793  1.1  christos void
    794  1.1  christos p_gwaddr(const struct sockaddr *sa, int gwaf, int flags)
    795  1.1  christos {
    796  1.1  christos 	p_sockaddr(sa, 0, RTF_HOST, WID_GW(gwaf), flags);
    797  1.1  christos }
    798