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