Home | History | Annotate | Line # | Download | only in netstat
route.c revision 1.32
      1  1.32        pk /*	$NetBSD: route.c,v 1.32 1998/12/05 13:14:04 pk Exp $	*/
      2  1.14   thorpej 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1983, 1988, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.23     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.14   thorpej #if 0
     39  1.14   thorpej static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     40  1.14   thorpej #else
     41  1.32        pk __RCSID("$NetBSD: route.c,v 1.32 1998/12/05 13:14:04 pk Exp $");
     42  1.14   thorpej #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46  1.10   mycroft #include <sys/protosw.h>
     47   1.1       cgd #include <sys/socket.h>
     48   1.1       cgd #include <sys/mbuf.h>
     49  1.30       mrg #include <sys/un.h>
     50   1.1       cgd 
     51   1.1       cgd #include <net/if.h>
     52  1.10   mycroft #include <net/if_dl.h>
     53  1.10   mycroft #include <net/if_types.h>
     54  1.11       jtc #define _KERNEL
     55   1.1       cgd #include <net/route.h>
     56  1.11       jtc #undef _KERNEL
     57   1.1       cgd #include <netinet/in.h>
     58  1.21  christos #include <netatalk/at.h>
     59  1.30       mrg #include <netiso/iso.h>
     60   1.1       cgd 
     61   1.1       cgd #include <netns/ns.h>
     62   1.1       cgd 
     63  1.10   mycroft #include <sys/sysctl.h>
     64   1.4      paul 
     65  1.29       mrg #include <err.h>
     66   1.1       cgd #include <netdb.h>
     67   1.1       cgd #include <stdio.h>
     68  1.10   mycroft #include <stdlib.h>
     69   1.1       cgd #include <string.h>
     70  1.10   mycroft #include <unistd.h>
     71  1.29       mrg 
     72  1.10   mycroft #include "netstat.h"
     73   1.1       cgd 
     74  1.10   mycroft #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
     75   1.1       cgd 
     76   1.1       cgd /*
     77   1.1       cgd  * Definitions for showing gateway flags.
     78   1.1       cgd  */
     79   1.1       cgd struct bits {
     80   1.1       cgd 	short	b_mask;
     81   1.1       cgd 	char	b_val;
     82   1.1       cgd } bits[] = {
     83   1.1       cgd 	{ RTF_UP,	'U' },
     84   1.1       cgd 	{ RTF_GATEWAY,	'G' },
     85   1.1       cgd 	{ RTF_HOST,	'H' },
     86  1.10   mycroft 	{ RTF_REJECT,	'R' },
     87   1.1       cgd 	{ RTF_DYNAMIC,	'D' },
     88   1.1       cgd 	{ RTF_MODIFIED,	'M' },
     89  1.10   mycroft 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     90  1.10   mycroft 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     91   1.1       cgd 	{ RTF_CLONING,	'C' },
     92   1.1       cgd 	{ RTF_XRESOLVE,	'X' },
     93   1.1       cgd 	{ RTF_LLINFO,	'L' },
     94  1.10   mycroft 	{ RTF_STATIC,	'S' },
     95  1.10   mycroft 	{ RTF_PROTO1,	'1' },
     96  1.10   mycroft 	{ RTF_PROTO2,	'2' },
     97   1.1       cgd 	{ 0 }
     98   1.1       cgd };
     99   1.1       cgd 
    100  1.30       mrg /*
    101  1.30       mrg  * XXX we put all of the sockaddr types in here to force the alignment
    102  1.30       mrg  * to be correct.
    103  1.30       mrg  */
    104  1.10   mycroft static union {
    105  1.10   mycroft 	struct	sockaddr u_sa;
    106  1.30       mrg 	struct	sockaddr_in u_in;
    107  1.30       mrg 	struct	sockaddr_un u_un;
    108  1.30       mrg 	struct	sockaddr_iso u_iso;
    109  1.30       mrg 	struct	sockaddr_at u_at;
    110  1.30       mrg 	struct	sockaddr_dl u_dl;
    111  1.30       mrg 	struct	sockaddr_ns u_ns;
    112  1.10   mycroft 	u_short	u_data[128];
    113  1.10   mycroft } pt_u;
    114  1.10   mycroft 
    115  1.10   mycroft int	do_rtent = 0;
    116  1.10   mycroft struct	rtentry rtentry;
    117  1.10   mycroft struct	radix_node rnode;
    118  1.10   mycroft struct	radix_mask rmask;
    119  1.10   mycroft 
    120  1.10   mycroft int	NewTree = 0;
    121  1.10   mycroft 
    122  1.10   mycroft static struct sockaddr *kgetsa __P((struct sockaddr *));
    123  1.10   mycroft static void p_tree __P((struct radix_node *));
    124  1.21  christos static void p_rtnode __P((void));
    125  1.21  christos static void ntreestuff __P((void));
    126  1.10   mycroft static void np_rtentry __P((struct rt_msghdr *));
    127  1.16    mellon static void p_sockaddr __P((const struct sockaddr *,
    128  1.16    mellon 			    const struct sockaddr *, int, int));
    129  1.10   mycroft static void p_flags __P((int, char *));
    130  1.10   mycroft static void p_rtentry __P((struct rtentry *));
    131  1.21  christos static void ntreestuff __P((void));
    132  1.21  christos static u_long forgemask __P((u_long));
    133  1.29       mrg static void domask __P((char *, size_t, u_long, u_long));
    134   1.3       cgd 
    135   1.3       cgd /*
    136   1.1       cgd  * Print routing tables.
    137   1.1       cgd  */
    138  1.10   mycroft void
    139  1.10   mycroft routepr(rtree)
    140  1.10   mycroft 	u_long rtree;
    141   1.1       cgd {
    142  1.10   mycroft 	struct radix_node_head *rnh, head;
    143  1.10   mycroft 	int i;
    144   1.1       cgd 
    145   1.1       cgd 	printf("Routing tables\n");
    146  1.10   mycroft 
    147  1.10   mycroft 	if (Aflag == 0 && NewTree)
    148  1.10   mycroft 		ntreestuff();
    149  1.10   mycroft 	else {
    150  1.10   mycroft 		if (rtree == 0) {
    151  1.10   mycroft 			printf("rt_tables: symbol not in namelist\n");
    152  1.10   mycroft 			return;
    153  1.10   mycroft 		}
    154  1.10   mycroft 
    155  1.10   mycroft 		kget(rtree, rt_tables);
    156  1.10   mycroft 		for (i = 0; i <= AF_MAX; i++) {
    157  1.10   mycroft 			if ((rnh = rt_tables[i]) == 0)
    158  1.10   mycroft 				continue;
    159  1.10   mycroft 			kget(rnh, head);
    160  1.10   mycroft 			if (i == AF_UNSPEC) {
    161  1.10   mycroft 				if (Aflag && af == 0) {
    162  1.10   mycroft 					printf("Netmasks:\n");
    163  1.10   mycroft 					p_tree(head.rnh_treetop);
    164  1.10   mycroft 				}
    165  1.10   mycroft 			} else if (af == AF_UNSPEC || af == i) {
    166  1.10   mycroft 				pr_family(i);
    167  1.10   mycroft 				do_rtent = 1;
    168  1.10   mycroft 				pr_rthdr();
    169  1.10   mycroft 				p_tree(head.rnh_treetop);
    170  1.10   mycroft 			}
    171   1.1       cgd 		}
    172   1.1       cgd 	}
    173   1.1       cgd }
    174   1.1       cgd 
    175  1.10   mycroft /*
    176  1.10   mycroft  * Print address family header before a section of the routing table.
    177  1.10   mycroft  */
    178  1.10   mycroft void
    179  1.10   mycroft pr_family(af)
    180  1.10   mycroft 	int af;
    181   1.4      paul {
    182  1.10   mycroft 	char *afname;
    183   1.4      paul 
    184  1.10   mycroft 	switch (af) {
    185   1.4      paul 	case AF_INET:
    186  1.10   mycroft 		afname = "Internet";
    187  1.10   mycroft 		break;
    188   1.4      paul 	case AF_NS:
    189  1.10   mycroft 		afname = "XNS";
    190  1.10   mycroft 		break;
    191   1.4      paul 	case AF_ISO:
    192  1.10   mycroft 		afname = "ISO";
    193   1.4      paul 		break;
    194  1.21  christos 	case AF_APPLETALK:
    195  1.21  christos 		afname = "AppleTalk";
    196  1.21  christos 		break;
    197  1.10   mycroft 	case AF_CCITT:
    198  1.10   mycroft 		afname = "X.25";
    199   1.4      paul 		break;
    200   1.4      paul 	default:
    201  1.10   mycroft 		afname = NULL;
    202  1.10   mycroft 		break;
    203   1.4      paul 	}
    204  1.10   mycroft 	if (afname)
    205  1.10   mycroft 		printf("\n%s:\n", afname);
    206  1.10   mycroft 	else
    207  1.10   mycroft 		printf("\nProtocol Family %d:\n", af);
    208   1.4      paul }
    209   1.4      paul 
    210  1.10   mycroft /* column widths; each followed by one space */
    211  1.22  christos #define	WID_DST		18	/* width of destination column */
    212  1.10   mycroft #define	WID_GW		18	/* width of gateway column */
    213   1.4      paul 
    214  1.10   mycroft /*
    215  1.10   mycroft  * Print header for routing table columns.
    216  1.10   mycroft  */
    217  1.10   mycroft void
    218  1.10   mycroft pr_rthdr()
    219   1.1       cgd {
    220   1.1       cgd 
    221  1.10   mycroft 	if (Aflag)
    222  1.10   mycroft 		printf("%-8.8s ","Address");
    223  1.13   thorpej 	printf("%-*.*s %-*.*s %-6.6s  %6.6s%8.8s %6.6s  %s\n",
    224  1.10   mycroft 		WID_DST, WID_DST, "Destination",
    225  1.10   mycroft 		WID_GW, WID_GW, "Gateway",
    226  1.13   thorpej 		"Flags", "Refs", "Use", "Mtu", "Interface");
    227   1.1       cgd }
    228   1.1       cgd 
    229  1.10   mycroft static struct sockaddr *
    230   1.1       cgd kgetsa(dst)
    231  1.23     lukem 	struct sockaddr *dst;
    232   1.1       cgd {
    233  1.10   mycroft 
    234   1.1       cgd 	kget(dst, pt_u.u_sa);
    235  1.10   mycroft 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
    236  1.10   mycroft 		kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
    237   1.1       cgd 	return (&pt_u.u_sa);
    238   1.1       cgd }
    239   1.1       cgd 
    240  1.10   mycroft static void
    241   1.1       cgd p_tree(rn)
    242  1.10   mycroft 	struct radix_node *rn;
    243   1.1       cgd {
    244   1.1       cgd 
    245   1.1       cgd again:
    246   1.1       cgd 	kget(rn, rnode);
    247   1.1       cgd 	if (rnode.rn_b < 0) {
    248   1.1       cgd 		if (Aflag)
    249  1.21  christos 			printf("%-8.8lx ", (u_long) rn);
    250  1.10   mycroft 		if (rnode.rn_flags & RNF_ROOT) {
    251  1.10   mycroft 			if (Aflag)
    252  1.10   mycroft 				printf("(root node)%s",
    253   1.1       cgd 				    rnode.rn_dupedkey ? " =>\n" : "\n");
    254  1.10   mycroft 		} else if (do_rtent) {
    255   1.1       cgd 			kget(rn, rtentry);
    256   1.1       cgd 			p_rtentry(&rtentry);
    257   1.1       cgd 			if (Aflag)
    258   1.1       cgd 				p_rtnode();
    259   1.1       cgd 		} else {
    260   1.1       cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
    261  1.16    mellon 			    NULL, 0, 44);
    262   1.1       cgd 			putchar('\n');
    263   1.1       cgd 		}
    264  1.21  christos 		if ((rn = rnode.rn_dupedkey) != NULL)
    265   1.1       cgd 			goto again;
    266   1.1       cgd 	} else {
    267   1.1       cgd 		if (Aflag && do_rtent) {
    268  1.21  christos 			printf("%-8.8lx ", (u_long) rn);
    269   1.1       cgd 			p_rtnode();
    270   1.1       cgd 		}
    271   1.1       cgd 		rn = rnode.rn_r;
    272   1.1       cgd 		p_tree(rnode.rn_l);
    273   1.1       cgd 		p_tree(rn);
    274   1.1       cgd 	}
    275   1.1       cgd }
    276   1.1       cgd 
    277  1.10   mycroft static void
    278   1.1       cgd p_rtnode()
    279   1.1       cgd {
    280  1.10   mycroft 	struct radix_mask *rm = rnode.rn_mklist;
    281  1.29       mrg 	char	nbuf[20];
    282   1.1       cgd 
    283   1.1       cgd 	if (rnode.rn_b < 0) {
    284   1.1       cgd 		if (rnode.rn_mask) {
    285   1.1       cgd 			printf("\t  mask ");
    286   1.1       cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
    287  1.16    mellon 				    NULL, 0, -1);
    288   1.1       cgd 		} else if (rm == 0)
    289   1.1       cgd 			return;
    290   1.1       cgd 	} else {
    291  1.29       mrg 		(void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
    292  1.21  christos 		printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
    293  1.21  christos 		    (u_long) rnode.rn_r);
    294   1.1       cgd 	}
    295   1.1       cgd 	while (rm) {
    296   1.1       cgd 		kget(rm, rmask);
    297  1.29       mrg 		(void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
    298  1.21  christos 		printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
    299  1.21  christos 		    -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
    300  1.16    mellon 		if (rmask.rm_flags & RNF_NORMAL) {
    301  1.16    mellon 			struct radix_node rnode_aux;
    302  1.16    mellon 			printf(" <normal>, ");
    303  1.16    mellon 			kget(rmask.rm_leaf, rnode_aux);
    304  1.16    mellon 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
    305  1.16    mellon 				    NULL, 0, -1);
    306  1.16    mellon 		} else
    307  1.29       mrg 			p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
    308  1.29       mrg 			    NULL, 0, -1);
    309   1.1       cgd 		putchar('}');
    310  1.21  christos 		if ((rm = rmask.rm_mklist) != NULL)
    311   1.1       cgd 			printf(" ->");
    312   1.1       cgd 	}
    313   1.1       cgd 	putchar('\n');
    314   1.1       cgd }
    315   1.1       cgd 
    316  1.10   mycroft static void
    317   1.1       cgd ntreestuff()
    318   1.1       cgd {
    319  1.10   mycroft 	size_t needed;
    320  1.10   mycroft 	int mib[6];
    321   1.1       cgd 	char *buf, *next, *lim;
    322  1.23     lukem 	struct rt_msghdr *rtm;
    323   1.1       cgd 
    324  1.29       mrg 	mib[0] = CTL_NET;
    325  1.29       mrg 	mib[1] = PF_ROUTE;
    326  1.29       mrg 	mib[2] = 0;
    327  1.29       mrg 	mib[3] = 0;
    328  1.29       mrg 	mib[4] = NET_RT_DUMP;
    329  1.29       mrg 	mib[5] = 0;
    330  1.29       mrg 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    331  1.29       mrg 		err(1, "route sysctl estimate");
    332   1.1       cgd 	if ((buf = malloc(needed)) == 0)
    333  1.29       mrg 		errx(1, "out of space");
    334  1.29       mrg 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    335  1.29       mrg 		err(1, "sysctl of routing table");
    336   1.1       cgd 	lim  = buf + needed;
    337   1.1       cgd 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    338   1.1       cgd 		rtm = (struct rt_msghdr *)next;
    339   1.1       cgd 		np_rtentry(rtm);
    340   1.1       cgd 	}
    341   1.1       cgd }
    342   1.1       cgd 
    343  1.10   mycroft static void
    344   1.1       cgd np_rtentry(rtm)
    345  1.23     lukem 	struct rt_msghdr *rtm;
    346   1.1       cgd {
    347  1.23     lukem 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    348  1.10   mycroft #ifdef notdef
    349  1.10   mycroft 	static int masks_done, banner_printed;
    350  1.10   mycroft #endif
    351  1.10   mycroft 	static int old_af;
    352   1.1       cgd 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    353   1.1       cgd 
    354   1.1       cgd #ifdef notdef
    355   1.1       cgd 	/* for the moment, netmasks are skipped over */
    356   1.1       cgd 	if (!banner_printed) {
    357   1.1       cgd 		printf("Netmasks:\n");
    358   1.1       cgd 		banner_printed = 1;
    359   1.1       cgd 	}
    360   1.1       cgd 	if (masks_done == 0) {
    361   1.1       cgd 		if (rtm->rtm_addrs != RTA_DST ) {
    362   1.1       cgd 			masks_done = 1;
    363   1.1       cgd 			af = sa->sa_family;
    364   1.1       cgd 		}
    365   1.1       cgd 	} else
    366   1.1       cgd #endif
    367   1.1       cgd 		af = sa->sa_family;
    368   1.1       cgd 	if (af != old_af) {
    369  1.10   mycroft 		pr_family(af);
    370   1.1       cgd 		old_af = af;
    371   1.1       cgd 	}
    372   1.1       cgd 	if (rtm->rtm_addrs == RTA_DST)
    373  1.16    mellon 		p_sockaddr(sa, NULL, 0, 36);
    374   1.1       cgd 	else {
    375  1.16    mellon 		p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
    376   1.1       cgd 		if (sa->sa_len == 0)
    377   1.1       cgd 			sa->sa_len = sizeof(long);
    378   1.1       cgd 		sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
    379  1.16    mellon 		p_sockaddr(sa, NULL, 0, 18);
    380   1.1       cgd 	}
    381   1.1       cgd 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
    382   1.1       cgd 	putchar('\n');
    383   1.1       cgd }
    384   1.1       cgd 
    385  1.10   mycroft static void
    386  1.16    mellon p_sockaddr(sa, mask, flags, width)
    387  1.16    mellon 	const struct sockaddr *sa, *mask;
    388  1.10   mycroft 	int flags, width;
    389   1.1       cgd {
    390  1.10   mycroft 	char workbuf[128], *cplim;
    391  1.23     lukem 	char *cp = workbuf;
    392   1.1       cgd 
    393   1.1       cgd 	switch(sa->sa_family) {
    394   1.1       cgd 	case AF_INET:
    395   1.1       cgd 	    {
    396  1.23     lukem 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    397   1.1       cgd 
    398  1.16    mellon 		if (sin->sin_addr.s_addr == INADDR_ANY)
    399  1.16    mellon 			cp = "default";
    400  1.16    mellon 		else if (flags & RTF_HOST)
    401  1.16    mellon 			cp = routename(sin->sin_addr.s_addr);
    402  1.16    mellon 		else if (mask)
    403  1.16    mellon 			cp = netname(sin->sin_addr.s_addr,
    404  1.19   mycroft 			    ((struct sockaddr_in *)mask)->sin_addr.s_addr);
    405  1.16    mellon 		else
    406  1.16    mellon 			cp = netname(sin->sin_addr.s_addr, INADDR_ANY);
    407  1.10   mycroft 		break;
    408   1.1       cgd 	    }
    409   1.1       cgd 
    410  1.21  christos 	case AF_APPLETALK:
    411  1.21  christos 	case 0:
    412  1.21  christos 	    {
    413  1.21  christos 		if (!(flags & RTF_HOST) && mask)
    414  1.21  christos 			cp = atalk_print2(sa,mask,11);
    415  1.21  christos 		else
    416  1.21  christos 			cp = atalk_print(sa,11);
    417  1.21  christos 		break;
    418  1.21  christos 	    }
    419   1.1       cgd 	case AF_NS:
    420  1.18    mellon 		cp = ns_print((struct sockaddr *)sa);
    421   1.4      paul 		break;
    422   1.4      paul 
    423   1.4      paul 	case AF_LINK:
    424  1.10   mycroft 	    {
    425  1.23     lukem 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
    426  1.10   mycroft 
    427  1.10   mycroft 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
    428  1.10   mycroft 		    sdl->sdl_slen == 0)
    429  1.29       mrg 			(void)snprintf(workbuf, sizeof workbuf, "link#%d",
    430  1.29       mrg 			    sdl->sdl_index);
    431  1.10   mycroft 		else switch (sdl->sdl_type) {
    432  1.20   thorpej 		case IFT_FDDI:
    433  1.10   mycroft 		case IFT_ETHER:
    434  1.10   mycroft 		    {
    435  1.23     lukem 			int i;
    436  1.23     lukem 			u_char *lla = (u_char *)sdl->sdl_data +
    437  1.10   mycroft 			    sdl->sdl_nlen;
    438  1.10   mycroft 
    439  1.10   mycroft 			cplim = "";
    440  1.10   mycroft 			for (i = 0; i < sdl->sdl_alen; i++, lla++) {
    441  1.29       mrg 				/* XXX */
    442  1.20   thorpej 				cp += sprintf(cp, "%s%02x", cplim, *lla);
    443  1.10   mycroft 				cplim = ":";
    444  1.10   mycroft 			}
    445  1.10   mycroft 			cp = workbuf;
    446  1.10   mycroft 			break;
    447  1.10   mycroft 		    }
    448  1.10   mycroft 		default:
    449  1.10   mycroft 			cp = link_ntoa(sdl);
    450  1.10   mycroft 			break;
    451  1.10   mycroft 		}
    452   1.4      paul 		break;
    453  1.10   mycroft 	    }
    454   1.1       cgd 
    455   1.1       cgd 	default:
    456   1.1       cgd 	    {
    457  1.23     lukem 		u_char *s = (u_char *)sa->sa_data, *slim;
    458   1.1       cgd 
    459  1.10   mycroft 		slim =  sa->sa_len + (u_char *) sa;
    460   1.1       cgd 		cplim = cp + sizeof(workbuf) - 6;
    461   1.1       cgd 		cp += sprintf(cp, "(%d)", sa->sa_family);
    462   1.7       cgd 		while (s < slim && cp < cplim) {
    463   1.7       cgd 			cp += sprintf(cp, " %02x", *s++);
    464   1.7       cgd 			if (s < slim)
    465  1.10   mycroft 			    cp += sprintf(cp, "%02x", *s++);
    466   1.7       cgd 		}
    467   1.1       cgd 		cp = workbuf;
    468   1.1       cgd 	    }
    469   1.1       cgd 	}
    470   1.1       cgd 	if (width < 0 )
    471   1.1       cgd 		printf("%s ", cp);
    472   1.1       cgd 	else {
    473   1.1       cgd 		if (nflag)
    474   1.1       cgd 			printf("%-*s ", width, cp);
    475   1.1       cgd 		else
    476   1.1       cgd 			printf("%-*.*s ", width, width, cp);
    477   1.1       cgd 	}
    478   1.1       cgd }
    479   1.1       cgd 
    480  1.10   mycroft static void
    481   1.1       cgd p_flags(f, format)
    482  1.23     lukem 	int f;
    483   1.4      paul 	char *format;
    484   1.4      paul {
    485   1.4      paul 	char name[33], *flags;
    486  1.23     lukem 	struct bits *p = bits;
    487   1.4      paul 
    488  1.29       mrg 	for (flags = name; p->b_mask && flags - name < sizeof(name); p++)
    489   1.4      paul 		if (p->b_mask & f)
    490   1.4      paul 			*flags++ = p->b_val;
    491   1.4      paul 	*flags = '\0';
    492   1.4      paul 	printf(format, name);
    493   1.4      paul }
    494   1.4      paul 
    495  1.10   mycroft static void
    496   1.4      paul p_rtentry(rt)
    497  1.23     lukem 	struct rtentry *rt;
    498   1.4      paul {
    499  1.10   mycroft 	static struct ifnet ifnet, *lastif;
    500  1.16    mellon 	struct sockaddr *sa, addr, mask;
    501   1.4      paul 
    502  1.16    mellon 	if (!(sa = kgetsa(rt_key(rt))))
    503  1.23     lukem 		memset(&addr, 0, sizeof addr);
    504  1.16    mellon 	else
    505  1.16    mellon 		addr = *sa;
    506  1.16    mellon 	if (!rt_mask(rt) || !(sa = kgetsa(rt_mask(rt))))
    507  1.23     lukem 		memset(&mask, 0, sizeof mask);
    508  1.16    mellon 	else
    509  1.16    mellon 		mask = *sa;
    510  1.16    mellon 	p_sockaddr(&addr, &mask, rt->rt_flags, WID_DST);
    511  1.16    mellon 	p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, WID_GW);
    512   1.4      paul 	p_flags(rt->rt_flags, "%-6.6s ");
    513  1.27       kml 	printf("%6d %8lu ", rt->rt_refcnt, rt->rt_use);
    514  1.13   thorpej 	if (rt->rt_rmx.rmx_mtu)
    515  1.27       kml 		printf("%6lu", rt->rt_rmx.rmx_mtu);
    516  1.13   thorpej 	else
    517  1.26       kml 		printf("%6s", "-");
    518  1.26       kml 	putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
    519  1.10   mycroft 	if (rt->rt_ifp) {
    520  1.10   mycroft 		if (rt->rt_ifp != lastif) {
    521  1.10   mycroft 			kget(rt->rt_ifp, ifnet);
    522  1.10   mycroft 			lastif = rt->rt_ifp;
    523  1.10   mycroft 		}
    524  1.15   thorpej 		printf(" %.16s%s", ifnet.if_xname,
    525  1.10   mycroft 			rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
    526   1.1       cgd 	}
    527  1.10   mycroft 	putchar('\n');
    528   1.1       cgd }
    529   1.1       cgd 
    530   1.1       cgd char *
    531   1.1       cgd routename(in)
    532  1.12   mycroft 	u_int32_t in;
    533   1.1       cgd {
    534  1.23     lukem 	char *cp;
    535   1.1       cgd 	static char line[MAXHOSTNAMELEN + 1];
    536   1.1       cgd 	struct hostent *hp;
    537   1.1       cgd 	static char domain[MAXHOSTNAMELEN + 1];
    538   1.1       cgd 	static int first = 1;
    539   1.1       cgd 
    540   1.1       cgd 	if (first) {
    541   1.1       cgd 		first = 0;
    542  1.28       mrg 		if (gethostname(domain, MAXHOSTNAMELEN) == 0) {
    543  1.28       mrg 			domain[sizeof(domain) - 1] = '\0';
    544  1.28       mrg 			if ((cp = strchr(domain, '.')))
    545  1.28       mrg 				(void)strcpy(domain, cp + 1);
    546  1.28       mrg 			else
    547  1.28       mrg 				domain[0] = 0;
    548  1.28       mrg 		} else
    549   1.1       cgd 			domain[0] = 0;
    550   1.1       cgd 	}
    551   1.1       cgd 	cp = 0;
    552   1.1       cgd 	if (!nflag) {
    553   1.1       cgd 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
    554   1.1       cgd 			AF_INET);
    555   1.1       cgd 		if (hp) {
    556  1.23     lukem 			if ((cp = strchr(hp->h_name, '.')) &&
    557   1.1       cgd 			    !strcmp(cp + 1, domain))
    558   1.1       cgd 				*cp = 0;
    559   1.1       cgd 			cp = hp->h_name;
    560   1.1       cgd 		}
    561   1.1       cgd 	}
    562  1.29       mrg 	if (cp) {
    563   1.1       cgd 		strncpy(line, cp, sizeof(line) - 1);
    564  1.29       mrg 		line[sizeof(line) - 1] = '\0';
    565  1.29       mrg 	} else {
    566   1.1       cgd #define C(x)	((x) & 0xff)
    567  1.10   mycroft 		in = ntohl(in);
    568  1.29       mrg 		snprintf(line, sizeof line, "%u.%u.%u.%u",
    569  1.10   mycroft 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
    570   1.1       cgd 	}
    571   1.1       cgd 	return (line);
    572   1.1       cgd }
    573   1.1       cgd 
    574  1.16    mellon static u_long
    575  1.16    mellon forgemask(a)
    576  1.16    mellon 	u_long a;
    577  1.16    mellon {
    578  1.16    mellon 	u_long m;
    579  1.16    mellon 
    580  1.16    mellon 	if (IN_CLASSA(a))
    581  1.16    mellon 		m = IN_CLASSA_NET;
    582  1.16    mellon 	else if (IN_CLASSB(a))
    583  1.16    mellon 		m = IN_CLASSB_NET;
    584  1.16    mellon 	else
    585  1.16    mellon 		m = IN_CLASSC_NET;
    586  1.16    mellon 	return (m);
    587  1.16    mellon }
    588  1.16    mellon 
    589  1.16    mellon static void
    590  1.29       mrg domask(dst, dlen, addr, mask)
    591  1.16    mellon 	char *dst;
    592  1.29       mrg 	size_t dlen;
    593  1.16    mellon 	u_long addr, mask;
    594  1.16    mellon {
    595  1.23     lukem 	int b, i;
    596  1.16    mellon 
    597  1.16    mellon 	if (!mask || (forgemask(addr) == mask)) {
    598  1.16    mellon 		*dst = '\0';
    599  1.16    mellon 		return;
    600  1.16    mellon 	}
    601  1.16    mellon 	i = 0;
    602  1.16    mellon 	for (b = 0; b < 32; b++)
    603  1.16    mellon 		if (mask & (1 << b)) {
    604  1.23     lukem 			int bb;
    605  1.16    mellon 
    606  1.16    mellon 			i = b;
    607  1.16    mellon 			for (bb = b+1; bb < 32; bb++)
    608  1.16    mellon 				if (!(mask & (1 << bb))) {
    609  1.16    mellon 					i = -1;	/* noncontig */
    610  1.16    mellon 					break;
    611  1.16    mellon 				}
    612  1.16    mellon 			break;
    613  1.16    mellon 		}
    614  1.16    mellon 	if (i == -1)
    615  1.29       mrg 		(void)snprintf(dst, dlen, "&0x%lx", mask);
    616  1.16    mellon 	else
    617  1.29       mrg 		(void)snprintf(dst, dlen, "/%d", 32-i);
    618  1.16    mellon }
    619  1.16    mellon 
    620   1.1       cgd /*
    621   1.1       cgd  * Return the name of the network whose address is given.
    622   1.1       cgd  * The address is assumed to be that of a net or subnet, not a host.
    623   1.1       cgd  */
    624   1.1       cgd char *
    625   1.1       cgd netname(in, mask)
    626  1.12   mycroft 	u_int32_t in, mask;
    627   1.1       cgd {
    628   1.1       cgd 	char *cp = 0;
    629  1.16    mellon 	static char line[MAXHOSTNAMELEN + 4];
    630   1.1       cgd 	struct netent *np = 0;
    631  1.16    mellon 	u_int32_t net, omask;
    632  1.23     lukem 	u_int32_t i;
    633   1.1       cgd 	int subnetshift;
    634   1.1       cgd 
    635  1.16    mellon 	i = ntohl(in);
    636  1.19   mycroft 	omask = mask = ntohl(mask);
    637  1.16    mellon 	if (!nflag && i != INADDR_ANY) {
    638  1.12   mycroft 		if (mask == INADDR_ANY) {
    639  1.16    mellon 			switch (mask = forgemask(i)) {
    640  1.16    mellon 			case IN_CLASSA_NET:
    641   1.1       cgd 				subnetshift = 8;
    642  1.16    mellon 				break;
    643  1.16    mellon 			case IN_CLASSB_NET:
    644   1.1       cgd 				subnetshift = 8;
    645  1.16    mellon 				break;
    646  1.16    mellon 			case IN_CLASSC_NET:
    647   1.1       cgd 				subnetshift = 4;
    648  1.16    mellon 				break;
    649  1.16    mellon 			default:
    650  1.16    mellon 				abort();
    651   1.1       cgd 			}
    652   1.1       cgd 			/*
    653   1.1       cgd 			 * If there are more bits than the standard mask
    654   1.1       cgd 			 * would suggest, subnets must be in use.
    655   1.1       cgd 			 * Guess at the subnet mask, assuming reasonable
    656   1.1       cgd 			 * width subnet fields.
    657   1.1       cgd 			 */
    658  1.16    mellon 			while (i &~ mask)
    659   1.1       cgd 				mask = (long)mask >> subnetshift;
    660   1.1       cgd 		}
    661  1.16    mellon 		net = i & mask;
    662  1.32        pk 		/*
    663  1.32        pk 		 * Note: shift the hosts bits out in octet units, since
    664  1.32        pk 		 * not all versions of getnetbyaddr() do this for us (e.g.
    665  1.32        pk 		 * the current `etc/networks' parser).
    666  1.32        pk 		 */
    667  1.32        pk 		while ((mask & 0xff) == 0)
    668  1.32        pk 			mask >>= 8, net >>= 8;
    669   1.1       cgd 		np = getnetbyaddr(net, AF_INET);
    670   1.1       cgd 		if (np)
    671   1.1       cgd 			cp = np->n_name;
    672  1.10   mycroft 	}
    673   1.1       cgd 	if (cp)
    674   1.1       cgd 		strncpy(line, cp, sizeof(line) - 1);
    675  1.16    mellon 	else if ((i & 0xffffff) == 0)
    676  1.29       mrg 		(void)snprintf(line, sizeof line, "%u", C(i >> 24));
    677  1.16    mellon 	else if ((i & 0xffff) == 0)
    678  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u", C(i >> 24)
    679  1.29       mrg 		    , C(i >> 16));
    680  1.16    mellon 	else if ((i & 0xff) == 0)
    681  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u", C(i >> 24),
    682  1.29       mrg 		    C(i >> 16), C(i >> 8));
    683   1.1       cgd 	else
    684  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u", C(i >> 24),
    685  1.16    mellon 			C(i >> 16), C(i >> 8), C(i));
    686  1.29       mrg 	domask(line + strlen(line), sizeof(line) - strlen(line), i, omask);
    687   1.1       cgd 	return (line);
    688   1.1       cgd }
    689   1.1       cgd 
    690   1.1       cgd /*
    691   1.1       cgd  * Print routing statistics
    692   1.1       cgd  */
    693  1.10   mycroft void
    694   1.1       cgd rt_stats(off)
    695   1.9       cgd 	u_long off;
    696   1.1       cgd {
    697   1.1       cgd 	struct rtstat rtstat;
    698   1.1       cgd 
    699   1.1       cgd 	if (off == 0) {
    700   1.1       cgd 		printf("rtstat: symbol not in namelist\n");
    701   1.1       cgd 		return;
    702   1.1       cgd 	}
    703  1.10   mycroft 	kread(off, (char *)&rtstat, sizeof (rtstat));
    704   1.1       cgd 	printf("routing:\n");
    705   1.1       cgd 	printf("\t%u bad routing redirect%s\n",
    706   1.1       cgd 		rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
    707   1.1       cgd 	printf("\t%u dynamically created route%s\n",
    708   1.1       cgd 		rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
    709   1.1       cgd 	printf("\t%u new gateway%s due to redirects\n",
    710   1.1       cgd 		rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
    711   1.1       cgd 	printf("\t%u destination%s found unreachable\n",
    712   1.1       cgd 		rtstat.rts_unreach, plural(rtstat.rts_unreach));
    713   1.1       cgd 	printf("\t%u use%s of a wildcard route\n",
    714   1.1       cgd 		rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
    715   1.1       cgd }
    716   1.1       cgd short ns_nullh[] = {0,0,0};
    717   1.1       cgd short ns_bh[] = {-1,-1,-1};
    718   1.1       cgd 
    719   1.1       cgd char *
    720  1.10   mycroft ns_print(sa)
    721  1.17    mellon 	struct sockaddr *sa;
    722   1.1       cgd {
    723  1.23     lukem 	struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
    724   1.1       cgd 	struct ns_addr work;
    725  1.29       mrg 	union {
    726  1.29       mrg 		union	ns_net net_e;
    727  1.29       mrg 		u_long	long_e;
    728  1.29       mrg 	} net;
    729   1.1       cgd 	u_short port;
    730   1.1       cgd 	static char mybuf[50], cport[10], chost[25];
    731   1.1       cgd 	char *host = "";
    732  1.23     lukem 	char *p;
    733  1.23     lukem 	u_char *q;
    734   1.1       cgd 
    735   1.1       cgd 	work = sns->sns_addr;
    736   1.1       cgd 	port = ntohs(work.x_port);
    737   1.1       cgd 	work.x_port = 0;
    738   1.1       cgd 	net.net_e  = work.x_net;
    739   1.1       cgd 	if (ns_nullhost(work) && net.long_e == 0) {
    740   1.1       cgd 		if (port ) {
    741  1.29       mrg 			(void)snprintf(mybuf, sizeof mybuf, "*.%xH", port);
    742   1.1       cgd 			upHex(mybuf);
    743   1.1       cgd 		} else
    744  1.29       mrg 			(void)snprintf(mybuf, sizeof mybuf, "*.*");
    745   1.1       cgd 		return (mybuf);
    746   1.1       cgd 	}
    747   1.1       cgd 
    748  1.23     lukem 	if (memcmp(ns_bh, work.x_host.c_host, 6) == 0) {
    749   1.1       cgd 		host = "any";
    750  1.23     lukem 	} else if (memcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
    751   1.1       cgd 		host = "*";
    752   1.1       cgd 	} else {
    753   1.1       cgd 		q = work.x_host.c_host;
    754  1.29       mrg 		(void)snprintf(chost, sizeof chost, "%02x%02x%02x%02x%02x%02xH",
    755   1.1       cgd 			q[0], q[1], q[2], q[3], q[4], q[5]);
    756  1.10   mycroft 		for (p = chost; *p == '0' && p < chost + 12; p++)
    757  1.10   mycroft 			continue;
    758   1.1       cgd 		host = p;
    759   1.1       cgd 	}
    760   1.1       cgd 	if (port)
    761  1.29       mrg 		(void)snprintf(cport, sizeof cport, ".%xH", htons(port));
    762   1.1       cgd 	else
    763   1.1       cgd 		*cport = 0;
    764   1.1       cgd 
    765  1.29       mrg 	(void)snprintf(mybuf, sizeof mybuf, "%xH.%s%s", (int)ntohl(net.long_e),
    766  1.29       mrg 	    host, cport);
    767   1.1       cgd 	upHex(mybuf);
    768  1.29       mrg 	return (mybuf);
    769   1.1       cgd }
    770   1.1       cgd 
    771   1.1       cgd char *
    772  1.10   mycroft ns_phost(sa)
    773  1.10   mycroft 	struct sockaddr *sa;
    774   1.1       cgd {
    775  1.23     lukem 	struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
    776   1.1       cgd 	struct sockaddr_ns work;
    777   1.1       cgd 	static union ns_net ns_zeronet;
    778   1.1       cgd 	char *p;
    779  1.10   mycroft 
    780   1.1       cgd 	work = *sns;
    781   1.1       cgd 	work.sns_addr.x_port = 0;
    782   1.1       cgd 	work.sns_addr.x_net = ns_zeronet;
    783   1.1       cgd 
    784  1.10   mycroft 	p = ns_print((struct sockaddr *)&work);
    785  1.29       mrg 	if (strncmp("0H.", p, 3) == 0)
    786  1.29       mrg 		p += 3;
    787  1.29       mrg 	return (p);
    788   1.1       cgd }
    789  1.10   mycroft 
    790  1.10   mycroft void
    791   1.1       cgd upHex(p0)
    792  1.10   mycroft 	char *p0;
    793   1.1       cgd {
    794  1.23     lukem 	char *p = p0;
    795   1.1       cgd 
    796  1.29       mrg 	for (; *p; p++)
    797  1.29       mrg 		switch (*p) {
    798  1.29       mrg 		case 'a':
    799  1.29       mrg 		case 'b':
    800  1.29       mrg 		case 'c':
    801  1.29       mrg 		case 'd':
    802  1.29       mrg 		case 'e':
    803  1.29       mrg 		case 'f':
    804  1.29       mrg 			*p += ('A' - 'a');
    805  1.29       mrg 		}
    806   1.1       cgd }
    807