xref: /dragonfly/usr.bin/netstat/route.c (revision 9bb2a92d)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)route.c	8.6 (Berkeley) 4/28/95
34  * $FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.14 2002/07/17 02:22:22 kbyanc Exp $
35  * $DragonFly: src/usr.bin/netstat/route.c,v 1.5 2004/01/06 03:17:21 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/time.h>
42 
43 #include <net/ethernet.h>
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netipx/ipx.h>
52 #include <netatalk/at.h>
53 #include <netgraph/socket/ng_socket.h>
54 
55 #ifdef NS
56 #include <netns/ns.h>
57 #endif
58 
59 #include <sys/sysctl.h>
60 
61 #include <arpa/inet.h>
62 #include <libutil.h>
63 #include <netdb.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <err.h>
69 #include <time.h>
70 #include "netstat.h"
71 
72 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
73 
74 
75 /* alignment constraint for routing socket */
76 #define ROUNDUP(a) \
77        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
78 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
79 
80 /*
81  * Definitions for showing gateway flags.
82  */
83 struct bits {
84 	u_long	b_mask;
85 	char	b_val;
86 } bits[] = {
87 	{ RTF_UP,	'U' },
88 	{ RTF_GATEWAY,	'G' },
89 	{ RTF_HOST,	'H' },
90 	{ RTF_REJECT,	'R' },
91 	{ RTF_DYNAMIC,	'D' },
92 	{ RTF_MODIFIED,	'M' },
93 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
94 	{ RTF_CLONING,	'C' },
95 	{ RTF_XRESOLVE,	'X' },
96 	{ RTF_LLINFO,	'L' },
97 	{ RTF_STATIC,	'S' },
98 	{ RTF_PROTO1,	'1' },
99 	{ RTF_PROTO2,	'2' },
100 	{ RTF_WASCLONED,'W' },
101 	{ RTF_PRCLONING,'c' },
102 	{ RTF_PROTO3,	'3' },
103 	{ RTF_BLACKHOLE,'B' },
104 	{ RTF_BROADCAST,'b' },
105 	{ 0 }
106 };
107 
108 typedef union {
109 	long	dummy;		/* Helps align structure. */
110 	struct	sockaddr u_sa;
111 	u_short	u_data[128];
112 } sa_u;
113 
114 static sa_u pt_u;
115 
116 int	do_rtent = 0;
117 struct	rtentry rtentry;
118 struct	radix_node rnode;
119 struct	radix_mask rmask;
120 struct	radix_node_head *rt_tables[AF_MAX+1];
121 
122 int	NewTree = 0;
123 
124 static struct sockaddr *kgetsa (struct sockaddr *);
125 static void size_cols (int ef, struct radix_node *rn);
126 static void size_cols_tree (struct radix_node *rn);
127 static void size_cols_rtentry (struct rtentry *rt);
128 static void p_tree (struct radix_node *);
129 static void p_rtnode (void);
130 static void ntreestuff (void);
131 static void np_rtentry (struct rt_msghdr *);
132 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
133 static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
134 				 int flags);
135 static void p_flags (int, char *);
136 static const char *fmt_flags(int f);
137 static void p_rtentry (struct rtentry *);
138 static u_long forgemask (u_long);
139 static void domask (char *, u_long, u_long);
140 
141 /*
142  * Print routing tables.
143  */
144 void
145 routepr(u_long rtree)
146 {
147 	struct radix_node_head *rnh, head;
148 	int i;
149 
150 	printf("Routing tables\n");
151 
152 	if (Aflag == 0 && NewTree)
153 		ntreestuff();
154 	else {
155 		if (rtree == 0) {
156 			printf("rt_tables: symbol not in namelist\n");
157 			return;
158 		}
159 
160 		kget(rtree, rt_tables);
161 		for (i = 0; i <= AF_MAX; i++) {
162 			if ((rnh = rt_tables[i]) == 0)
163 				continue;
164 			kget(rnh, head);
165 			if (i == AF_UNSPEC) {
166 				if (Aflag && af == 0) {
167 					printf("Netmasks:\n");
168 					p_tree(head.rnh_treetop);
169 				}
170 			} else if (af == AF_UNSPEC || af == i) {
171 				size_cols(i, head.rnh_treetop);
172 				pr_family(i);
173 				do_rtent = 1;
174 				pr_rthdr(i);
175 				p_tree(head.rnh_treetop);
176 			}
177 		}
178 	}
179 }
180 
181 /*
182  * Print address family header before a section of the routing table.
183  */
184 void
185 pr_family(int af)
186 {
187 	char *afname;
188 
189 	switch (af) {
190 	case AF_INET:
191 		afname = "Internet";
192 		break;
193 #ifdef INET6
194 	case AF_INET6:
195 		afname = "Internet6";
196 		break;
197 #endif /*INET6*/
198 	case AF_IPX:
199 		afname = "IPX";
200 		break;
201 #ifdef NS
202 	case AF_NS:
203 		afname = "XNS";
204 		break;
205 #endif
206 	case AF_ISO:
207 		afname = "ISO";
208 		break;
209 	case AF_APPLETALK:
210 		afname = "AppleTalk";
211 		break;
212 	case AF_CCITT:
213 		afname = "X.25";
214 		break;
215 	case AF_NETGRAPH:
216 		afname = "Netgraph";
217 		break;
218 	default:
219 		afname = NULL;
220 		break;
221 	}
222 	if (afname)
223 		printf("\n%s:\n", afname);
224 	else
225 		printf("\nProtocol Family %d:\n", af);
226 }
227 
228 /* column widths; each followed by one space */
229 #ifndef INET6
230 #define	WID_DST_DEFAULT(af) 	18	/* width of destination column */
231 #define	WID_GW_DEFAULT(af)	18	/* width of gateway column */
232 #define	WID_IF_DEFAULT(af)	(Wflag ? 8 : 6)	/* width of netif column */
233 #else
234 #define	WID_DST_DEFAULT(af) \
235 	((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
236 #define	WID_GW_DEFAULT(af) \
237 	((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
238 #define	WID_IF_DEFAULT(af)	((af) == AF_INET6 ? 8 : (Wflag ? 8 :6))
239 #endif /*INET6*/
240 
241 static int wid_dst;
242 static int wid_gw;
243 static int wid_flags;
244 static int wid_refs;
245 static int wid_use;
246 static int wid_mtu;
247 static int wid_if;
248 static int wid_expire;
249 
250 static void
251 size_cols(int ef, struct radix_node *rn)
252 {
253 	wid_dst = WID_DST_DEFAULT(ef);
254 	wid_gw = WID_GW_DEFAULT(ef);
255 	wid_flags = 6;
256 	wid_refs = 6;
257 	wid_use = 8;
258 	wid_mtu = 6;
259 	wid_if = WID_IF_DEFAULT(ef);
260 	wid_expire = 6;
261 
262 	if (Wflag)
263 		size_cols_tree(rn);
264 }
265 
266 static void
267 size_cols_tree(struct radix_node *rn)
268 {
269 again:
270 	kget(rn, rnode);
271 	if (rnode.rn_bit < 0) {
272 		if ((rnode.rn_flags & RNF_ROOT) == 0) {
273 			kget(rn, rtentry);
274 			size_cols_rtentry(&rtentry);
275 		}
276 		if ((rn = rnode.rn_dupedkey))
277 			goto again;
278 	} else {
279 		rn = rnode.rn_right;
280 		size_cols_tree(rnode.rn_left);
281 		size_cols_tree(rn);
282 	}
283 }
284 
285 static void
286 size_cols_rtentry(struct rtentry *rt)
287 {
288 	static struct ifnet ifnet, *lastif;
289 	struct rtentry parent;
290 	static char buffer[100];
291 	const char *bp;
292 	struct sockaddr *sa;
293 	sa_u addr, mask;
294 	int len;
295 
296 	/*
297 	 * Don't print protocol-cloned routes unless -a.
298 	 */
299 	if (rt->rt_flags & RTF_WASCLONED && !aflag) {
300 		kget(rt->rt_parent, parent);
301 		if (parent.rt_flags & RTF_PRCLONING)
302 			return;
303 	}
304 
305 	bzero(&addr, sizeof(addr));
306 	if ((sa = kgetsa(rt_key(rt))))
307 		bcopy(sa, &addr, sa->sa_len);
308 	bzero(&mask, sizeof(mask));
309 	if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
310 		bcopy(sa, &mask, sa->sa_len);
311 	bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags);
312 	len = strlen(bp);
313 	wid_dst = MAX(len, wid_dst);
314 
315 	bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST);
316 	len = strlen(bp);
317 	wid_gw = MAX(len, wid_gw);
318 
319 	bp = fmt_flags(rt->rt_flags);
320 	len = strlen(bp);
321 	wid_flags = MAX(len, wid_flags);
322 
323 	if (addr.u_sa.sa_family == AF_INET || Wflag) {
324 		len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt);
325 		wid_refs = MAX(len, wid_refs);
326 		len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use);
327 		wid_use = MAX(len, wid_use);
328 		if (Wflag && rt->rt_rmx.rmx_mtu != 0) {
329 			len = snprintf(buffer, sizeof(buffer),
330 				       "%lu", rt->rt_rmx.rmx_mtu);
331 			wid_mtu = MAX(len, wid_mtu);
332 		}
333 	}
334 	if (rt->rt_ifp) {
335 		if (rt->rt_ifp != lastif) {
336 			kget(rt->rt_ifp, ifnet);
337 			lastif = rt->rt_ifp;
338 			len = strlen(ifnet.if_xname);
339 			wid_if = MAX(len, wid_if);
340 		}
341 		if (rt->rt_rmx.rmx_expire) {
342 			time_t expire_time;
343 
344 			if ((expire_time =
345 			    rt->rt_rmx.rmx_expire - time(NULL)) > 0) {
346 				snprintf(buffer, sizeof(buffer), "%d",
347 					 (int)expire_time);
348 				wid_expire = MAX(len, wid_expire);
349 			}
350 		}
351 	}
352 }
353 
354 
355 /*
356  * Print header for routing table columns.
357  */
358 void
359 pr_rthdr(int af)
360 {
361 
362 	if (Aflag)
363 		printf("%-8.8s ","Address");
364 	if (af == AF_INET || Wflag) {
365 		if (Wflag) {
366 			printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s\n",
367 				wid_dst,	wid_dst,	"Destination",
368 				wid_gw,		wid_gw,		"Gateway",
369 				wid_flags,	wid_flags,	"Flags",
370 				wid_refs,	wid_refs,	"Refs",
371 				wid_use,	wid_use,	"Use",
372 				wid_mtu,	wid_mtu,	"Mtu",
373 				wid_if,		wid_if,		"Netif",
374 				wid_expire,			"Expire");
375 		} else {
376 			printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n",
377 				wid_dst,	wid_dst,	"Destination",
378 				wid_gw,		wid_gw,		"Gateway",
379 				wid_flags,	wid_flags,	"Flags",
380 				wid_refs,	wid_refs,	"Refs",
381 				wid_use,	wid_use,	"Use",
382 				wid_if,		wid_if,		"Netif",
383 				wid_expire,			"Expire");
384 		}
385 	} else {
386 		printf("%-*.*s %-*.*s %-*.*s  %*.*s %*s\n",
387 			wid_dst,	wid_dst,	"Destination",
388 			wid_gw,		wid_gw,		"Gateway",
389 			wid_flags,	wid_flags,	"Flags",
390 			wid_if,		wid_if,		"Netif",
391 			wid_expire,			"Expire");
392 	}
393 }
394 
395 static struct sockaddr *
396 kgetsa(struct sockaddr *dst)
397 {
398 
399 	kget(dst, pt_u.u_sa);
400 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
401 		kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
402 	return (&pt_u.u_sa);
403 }
404 
405 static void
406 p_tree(struct radix_node *rn)
407 {
408 
409 again:
410 	kget(rn, rnode);
411 	if (rnode.rn_bit < 0) {
412 		if (Aflag)
413 			printf("%-8.8lx ", (u_long)rn);
414 		if (rnode.rn_flags & RNF_ROOT) {
415 			if (Aflag)
416 				printf("(root node)%s",
417 				    rnode.rn_dupedkey ? " =>\n" : "\n");
418 		} else if (do_rtent) {
419 			kget(rn, rtentry);
420 			p_rtentry(&rtentry);
421 			if (Aflag)
422 				p_rtnode();
423 		} else {
424 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
425 				   NULL, 0, 44);
426 			putchar('\n');
427 		}
428 		if ((rn = rnode.rn_dupedkey))
429 			goto again;
430 	} else {
431 		if (Aflag && do_rtent) {
432 			printf("%-8.8lx ", (u_long)rn);
433 			p_rtnode();
434 		}
435 		rn = rnode.rn_right;
436 		p_tree(rnode.rn_left);
437 		p_tree(rn);
438 	}
439 }
440 
441 char	nbuf[20];
442 
443 static void
444 p_rtnode(void)
445 {
446 	struct radix_mask *rm = rnode.rn_mklist;
447 
448 	if (rnode.rn_bit < 0) {
449 		if (rnode.rn_mask) {
450 			printf("\t  mask ");
451 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
452 				   NULL, 0, -1);
453 		} else if (rm == 0)
454 			return;
455 	} else {
456 		sprintf(nbuf, "(%d)", rnode.rn_bit);
457 		printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
458 	}
459 	while (rm) {
460 		kget(rm, rmask);
461 		sprintf(nbuf, " %d refs, ", rmask.rm_refs);
462 		printf(" mk = %8.8lx {(%d),%s",
463 			(u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
464 		if (rmask.rm_flags & RNF_NORMAL) {
465 			struct radix_node rnode_aux;
466 			printf(" <normal>, ");
467 			kget(rmask.rm_leaf, rnode_aux);
468 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
469 				    NULL, 0, -1);
470 		} else
471 		    p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
472 				NULL, 0, -1);
473 		putchar('}');
474 		if ((rm = rmask.rm_mklist))
475 			printf(" ->");
476 	}
477 	putchar('\n');
478 }
479 
480 static void
481 ntreestuff(void)
482 {
483 	size_t needed;
484 	int mib[6];
485 	char *buf, *next, *lim;
486 	register struct rt_msghdr *rtm;
487 
488 	mib[0] = CTL_NET;
489 	mib[1] = PF_ROUTE;
490 	mib[2] = 0;
491 	mib[3] = 0;
492 	mib[4] = NET_RT_DUMP;
493 	mib[5] = 0;
494 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
495 		err(1, "sysctl: net.route.0.0.dump estimate");
496 	}
497 
498 	if ((buf = malloc(needed)) == 0) {
499 		err(2, "malloc(%lu)", (unsigned long)needed);
500 	}
501 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
502 		err(1, "sysctl: net.route.0.0.dump");
503 	}
504 	lim  = buf + needed;
505 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
506 		rtm = (struct rt_msghdr *)next;
507 		np_rtentry(rtm);
508 	}
509 }
510 
511 static void
512 np_rtentry(struct rt_msghdr *rtm)
513 {
514 	register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
515 #ifdef notdef
516 	static int masks_done, banner_printed;
517 #endif
518 	static int old_af;
519 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
520 
521 #ifdef notdef
522 	/* for the moment, netmasks are skipped over */
523 	if (!banner_printed) {
524 		printf("Netmasks:\n");
525 		banner_printed = 1;
526 	}
527 	if (masks_done == 0) {
528 		if (rtm->rtm_addrs != RTA_DST ) {
529 			masks_done = 1;
530 			af = sa->sa_family;
531 		}
532 	} else
533 #endif
534 		af = sa->sa_family;
535 	if (af != old_af) {
536 		pr_family(af);
537 		old_af = af;
538 	}
539 	if (rtm->rtm_addrs == RTA_DST)
540 		p_sockaddr(sa, NULL, 0, 36);
541 	else {
542 		p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
543 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
544 		p_sockaddr(sa, NULL, 0, 18);
545 	}
546 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
547 	putchar('\n');
548 }
549 
550 static void
551 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
552 {
553 	const char *cp;
554 
555 	cp = fmt_sockaddr(sa, mask, flags);
556 
557 	if (width < 0 )
558 		printf("%s ", cp);
559 	else {
560 		if (numeric_addr)
561 			printf("%-*s ", width, cp);
562 		else
563 			printf("%-*.*s ", width, width, cp);
564 	}
565 }
566 
567 static const char *
568 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
569 {
570 	static char workbuf[128];
571 	char *cplim;
572 	char *cp = workbuf;
573 
574 	switch(sa->sa_family) {
575 	case AF_INET:
576 	    {
577 		register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
578 
579 		if ((sin->sin_addr.s_addr == INADDR_ANY) &&
580 			mask &&
581 			ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
582 				==0L)
583 				cp = "default" ;
584 		else if (flags & RTF_HOST)
585 			cp = routename(sin->sin_addr.s_addr);
586 		else if (mask)
587 			cp = netname(sin->sin_addr.s_addr,
588 				     ntohl(((struct sockaddr_in *)mask)
589 					   ->sin_addr.s_addr));
590 		else
591 			cp = netname(sin->sin_addr.s_addr, 0L);
592 		break;
593 	    }
594 
595 #ifdef INET6
596 	case AF_INET6:
597 	    {
598 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
599 		struct in6_addr *in6 = &sa6->sin6_addr;
600 
601 		/*
602 		 * XXX: This is a special workaround for KAME kernels.
603 		 * sin6_scope_id field of SA should be set in the future.
604 		 */
605 		if (IN6_IS_ADDR_LINKLOCAL(in6) ||
606 		    IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
607 		    /* XXX: override is ok? */
608 		    sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
609 		    *(u_short *)&in6->s6_addr[2] = 0;
610 		}
611 
612 		if (flags & RTF_HOST)
613 		    cp = routename6(sa6);
614 		else if (mask)
615 		    cp = netname6(sa6,
616 				  &((struct sockaddr_in6 *)mask)->sin6_addr);
617 		else {
618 		    cp = netname6(sa6, NULL);
619 		}
620 		break;
621 	    }
622 #endif /*INET6*/
623 
624 	case AF_IPX:
625 	    {
626 		struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
627 		if (ipx_nullnet(satoipx_addr(work)))
628 			cp = "default";
629 		else
630 			cp = ipx_print(sa);
631 		break;
632 	    }
633 	case AF_APPLETALK:
634 	    {
635 		if (!(flags & RTF_HOST) && mask)
636 			cp = atalk_print2(sa,mask,9);
637 		else
638 			cp = atalk_print(sa,11);
639 		break;
640 	    }
641 	case AF_NETGRAPH:
642 	    {
643 		printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
644 		break;
645 	    }
646 #ifdef NS
647 	case AF_NS:
648 		cp = ns_print(sa);
649 		break;
650 #endif
651 
652 	case AF_LINK:
653 	    {
654 		register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
655 
656 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
657 		    sdl->sdl_slen == 0)
658 			(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
659 		else
660 			switch (sdl->sdl_type) {
661 
662 			case IFT_ETHER:
663 				if (sdl->sdl_alen == ETHER_ADDR_LEN) {
664 					cp = ether_ntoa((struct ether_addr *)
665 					    (sdl->sdl_data + sdl->sdl_nlen));
666 					break;
667 				}
668 				/* FALLTHROUGH */
669 			default:
670 				cp = link_ntoa(sdl);
671 				break;
672 			}
673 		break;
674 	    }
675 
676 	default:
677 	    {
678 		register u_char *s = (u_char *)sa->sa_data, *slim;
679 
680 		slim =  sa->sa_len + (u_char *) sa;
681 		cplim = cp + sizeof(workbuf) - 6;
682 		cp += sprintf(cp, "(%d)", sa->sa_family);
683 		while (s < slim && cp < cplim) {
684 			cp += sprintf(cp, " %02x", *s++);
685 			if (s < slim)
686 			    cp += sprintf(cp, "%02x", *s++);
687 		}
688 		cp = workbuf;
689 	    }
690 	}
691 
692 	return (cp);
693 }
694 
695 static void
696 p_flags(int f, char *format)
697 {
698 	printf(format, fmt_flags(f));
699 }
700 
701 static const char *
702 fmt_flags(int f)
703 {
704 	static char name[33];
705 	char *flags;
706 	struct bits *p = bits;
707 
708 	for (flags = name; p->b_mask; p++)
709 		if (p->b_mask & f)
710 			*flags++ = p->b_val;
711 	*flags = '\0';
712 	return (name);
713 }
714 
715 static void
716 p_rtentry(struct rtentry *rt)
717 {
718 	static struct ifnet ifnet, *lastif;
719 	struct rtentry parent;
720 	static char buffer[128];
721 	static char prettyname[128];
722 	struct sockaddr *sa;
723 	sa_u addr, mask;
724 
725 	/*
726 	 * Don't print protocol-cloned routes unless -a.
727 	 */
728 	if (rt->rt_flags & RTF_WASCLONED && !aflag) {
729 		kget(rt->rt_parent, parent);
730 		if (parent.rt_flags & RTF_PRCLONING)
731 			return;
732 	}
733 
734 	bzero(&addr, sizeof(addr));
735 	if ((sa = kgetsa(rt_key(rt))))
736 		bcopy(sa, &addr, sa->sa_len);
737 	bzero(&mask, sizeof(mask));
738 	if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
739 		bcopy(sa, &mask, sa->sa_len);
740 	p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
741 	p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
742 	snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
743 	p_flags(rt->rt_flags, buffer);
744 	if (addr.u_sa.sa_family == AF_INET || Wflag) {
745 		printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
746 				     wid_use, rt->rt_use);
747 		if (Wflag) {
748 			if (rt->rt_rmx.rmx_mtu != 0)
749 				printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
750 			else
751 				printf("%*s ", wid_mtu, "");
752 		}
753 	}
754 	if (rt->rt_ifp) {
755 		if (rt->rt_ifp != lastif) {
756 			kget(rt->rt_ifp, ifnet);
757 			lastif = rt->rt_ifp;
758 			strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
759 		}
760 		printf("%*.*s", wid_if, wid_if, prettyname);
761 		if (rt->rt_rmx.rmx_expire) {
762 			time_t expire_time;
763 
764 			if ((expire_time =
765 			    rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
766 				printf(" %*d", wid_expire, (int)expire_time);
767 		}
768 		if (rt->rt_nodes[0].rn_dupedkey)
769 			printf(" =>");
770 	}
771 	putchar('\n');
772 }
773 
774 char *
775 routename(u_long in)
776 {
777 	register char *cp;
778 	static char line[MAXHOSTNAMELEN];
779 	struct hostent *hp;
780 
781 	cp = 0;
782 	if (!numeric_addr) {
783 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
784 			AF_INET);
785 		if (hp) {
786 			cp = hp->h_name;
787 			trimdomain(cp, strlen(cp));
788 		}
789 	}
790 	if (cp) {
791 		strncpy(line, cp, sizeof(line) - 1);
792 		line[sizeof(line) - 1] = '\0';
793 	} else {
794 #define C(x)	((x) & 0xff)
795 		in = ntohl(in);
796 		sprintf(line, "%lu.%lu.%lu.%lu",
797 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
798 	}
799 	return (line);
800 }
801 
802 static u_long
803 forgemask(u_long a)
804 {
805 	u_long m;
806 
807 	if (IN_CLASSA(a))
808 		m = IN_CLASSA_NET;
809 	else if (IN_CLASSB(a))
810 		m = IN_CLASSB_NET;
811 	else
812 		m = IN_CLASSC_NET;
813 	return (m);
814 }
815 
816 static void
817 domask(char *dst, u_long addr, u_long mask)
818 {
819 	register int b, i;
820 
821 	if (!mask || (forgemask(addr) == mask)) {
822 		*dst = '\0';
823 		return;
824 	}
825 	i = 0;
826 	for (b = 0; b < 32; b++)
827 		if (mask & (1 << b)) {
828 			register int bb;
829 
830 			i = b;
831 			for (bb = b+1; bb < 32; bb++)
832 				if (!(mask & (1 << bb))) {
833 					i = -1;	/* noncontig */
834 					break;
835 				}
836 			break;
837 		}
838 	if (i == -1)
839 		sprintf(dst, "&0x%lx", mask);
840 	else
841 		sprintf(dst, "/%d", 32-i);
842 }
843 
844 /*
845  * Return the name of the network whose address is given.
846  * The address is assumed to be that of a net or subnet, not a host.
847  */
848 char *
849 netname(u_long in, u_long mask)
850 {
851 	char *cp = 0;
852 	static char line[MAXHOSTNAMELEN];
853 	struct netent *np = 0;
854 	u_long dmask;
855 	register u_long i;
856 
857 #define	NSHIFT(m) (							\
858 	(m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :			\
859 	(m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :			\
860 	(m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :			\
861 	0)
862 
863 	i = ntohl(in);
864 	dmask = forgemask(i);
865 	if (!numeric_addr && i) {
866 		np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
867 		if (np == NULL && mask == 0)
868 			np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET);
869 		if (np != NULL) {
870 			cp = np->n_name;
871 			trimdomain(cp, strlen(cp));
872 		}
873 	}
874 #undef NSHIFT
875 	if (cp != NULL) {
876 		strncpy(line, cp, sizeof(line) - 1);
877 		line[sizeof(line) - 1] = '\0';
878 	} else {
879 		switch (dmask) {
880 		case IN_CLASSA_NET:
881 			if ((i & IN_CLASSA_HOST) == 0) {
882 				sprintf(line, "%lu", C(i >> 24));
883 				break;
884 			}
885 			/* FALLTHROUGH */
886 		case IN_CLASSB_NET:
887 			if ((i & IN_CLASSB_HOST) == 0) {
888 				sprintf(line, "%lu.%lu",
889 					C(i >> 24), C(i >> 16));
890 				break;
891 			}
892 			/* FALLTHROUGH */
893 		case IN_CLASSC_NET:
894 			if ((i & IN_CLASSC_HOST) == 0) {
895 				sprintf(line, "%lu.%lu.%lu",
896 					C(i >> 24), C(i >> 16), C(i >> 8));
897 				break;
898 			}
899 			/* FALLTHROUGH */
900 		default:
901 			sprintf(line, "%lu.%lu.%lu.%lu",
902 				C(i >> 24), C(i >> 16), C(i >> 8), C(i));
903 			break;
904 		}
905 	}
906 	domask(line + strlen(line), i, mask);
907 	return (line);
908 }
909 
910 #ifdef INET6
911 char *
912 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
913 {
914 	static char line[MAXHOSTNAMELEN];
915 	u_char *p = (u_char *)mask;
916 	u_char *lim;
917 	int masklen, illegal = 0, flag = NI_WITHSCOPEID;
918 
919 	if (mask) {
920 		for (masklen = 0, lim = p + 16; p < lim; p++) {
921 			switch (*p) {
922 			 case 0xff:
923 				 masklen += 8;
924 				 break;
925 			 case 0xfe:
926 				 masklen += 7;
927 				 break;
928 			 case 0xfc:
929 				 masklen += 6;
930 				 break;
931 			 case 0xf8:
932 				 masklen += 5;
933 				 break;
934 			 case 0xf0:
935 				 masklen += 4;
936 				 break;
937 			 case 0xe0:
938 				 masklen += 3;
939 				 break;
940 			 case 0xc0:
941 				 masklen += 2;
942 				 break;
943 			 case 0x80:
944 				 masklen += 1;
945 				 break;
946 			 case 0x00:
947 				 break;
948 			 default:
949 				 illegal ++;
950 				 break;
951 			}
952 		}
953 		if (illegal)
954 			fprintf(stderr, "illegal prefixlen\n");
955 	}
956 	else
957 		masklen = 128;
958 
959 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
960 		return("default");
961 
962 	if (numeric_addr)
963 		flag |= NI_NUMERICHOST;
964 	getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
965 		    NULL, 0, flag);
966 
967 	if (numeric_addr)
968 		sprintf(&line[strlen(line)], "/%d", masklen);
969 
970 	return line;
971 }
972 
973 char *
974 routename6(struct sockaddr_in6 *sa6)
975 {
976 	static char line[MAXHOSTNAMELEN];
977 	int flag = NI_WITHSCOPEID;
978 	/* use local variable for safety */
979 	struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),};
980 
981 	sa6_local.sin6_addr = sa6->sin6_addr;
982 	sa6_local.sin6_scope_id = sa6->sin6_scope_id;
983 
984 	if (numeric_addr)
985 		flag |= NI_NUMERICHOST;
986 
987 	getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
988 		    line, sizeof(line), NULL, 0, flag);
989 
990 	return line;
991 }
992 #endif /*INET6*/
993 
994 /*
995  * Print routing statistics
996  */
997 void
998 rt_stats(u_long rtsaddr, u_long rttaddr)
999 {
1000 	struct rtstat rtstat;
1001 	int rttrash;
1002 
1003 	if (rtsaddr == 0) {
1004 		printf("rtstat: symbol not in namelist\n");
1005 		return;
1006 	}
1007 	if (rttaddr == 0) {
1008 		printf("rttrash: symbol not in namelist\n");
1009 		return;
1010 	}
1011 	kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
1012 	kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
1013 	printf("routing:\n");
1014 
1015 #define	p(f, m) if (rtstat.f || sflag <= 1) \
1016 	printf(m, rtstat.f, plural(rtstat.f))
1017 
1018 	p(rts_badredirect, "\t%u bad routing redirect%s\n");
1019 	p(rts_dynamic, "\t%u dynamically created route%s\n");
1020 	p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
1021 	p(rts_unreach, "\t%u destination%s found unreachable\n");
1022 	p(rts_wildcard, "\t%u use%s of a wildcard route\n");
1023 #undef p
1024 
1025 	if (rttrash || sflag <= 1)
1026 		printf("\t%u route%s not in table but not freed\n",
1027 		    rttrash, plural(rttrash));
1028 }
1029 
1030 char *
1031 ipx_print(struct sockaddr *sa)
1032 {
1033 	u_short port;
1034 	struct servent *sp = 0;
1035 	char *net = "", *host = "";
1036 	register char *p;
1037 	register u_char *q;
1038 	struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
1039 	static char mybuf[50];
1040 	char cport[10], chost[15], cnet[15];
1041 
1042 	port = ntohs(work.x_port);
1043 
1044 	if (ipx_nullnet(work) && ipx_nullhost(work)) {
1045 
1046 		if (port) {
1047 			if (sp)
1048 				sprintf(mybuf, "*.%s", sp->s_name);
1049 			else
1050 				sprintf(mybuf, "*.%x", port);
1051 		} else
1052 			sprintf(mybuf, "*.*");
1053 
1054 		return (mybuf);
1055 	}
1056 
1057 	if (ipx_wildnet(work))
1058 		net = "any";
1059 	else if (ipx_nullnet(work))
1060 		net = "*";
1061 	else {
1062 		q = work.x_net.c_net;
1063 		sprintf(cnet, "%02x%02x%02x%02x",
1064 			q[0], q[1], q[2], q[3]);
1065 		for (p = cnet; *p == '0' && p < cnet + 8; p++)
1066 			continue;
1067 		net = p;
1068 	}
1069 
1070 	if (ipx_wildhost(work))
1071 		host = "any";
1072 	else if (ipx_nullhost(work))
1073 		host = "*";
1074 	else {
1075 		q = work.x_host.c_host;
1076 		sprintf(chost, "%02x%02x%02x%02x%02x%02x",
1077 			q[0], q[1], q[2], q[3], q[4], q[5]);
1078 		for (p = chost; *p == '0' && p < chost + 12; p++)
1079 			continue;
1080 		host = p;
1081 	}
1082 
1083 	if (port) {
1084 		if (strcmp(host, "*") == 0)
1085 			host = "";
1086 		if (sp)
1087 			snprintf(cport, sizeof(cport),
1088 				"%s%s", *host ? "." : "", sp->s_name);
1089 		else
1090 			snprintf(cport, sizeof(cport),
1091 				"%s%x", *host ? "." : "", port);
1092 	} else
1093 		*cport = 0;
1094 
1095 	snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
1096 	return(mybuf);
1097 }
1098 
1099 char *
1100 ipx_phost(struct sockaddr *sa)
1101 {
1102 	register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
1103 	struct sockaddr_ipx work;
1104 	static union ipx_net ipx_zeronet;
1105 	char *p;
1106 	struct ipx_addr in;
1107 
1108 	work = *sipx;
1109 	in = work.sipx_addr;
1110 
1111 	work.sipx_addr.x_port = 0;
1112 	work.sipx_addr.x_net = ipx_zeronet;
1113 	p = ipx_print((struct sockaddr *)&work);
1114 	if (strncmp("*.", p, 2) == 0) p += 2;
1115 
1116 	return(p);
1117 }
1118 
1119 #ifdef NS
1120 short ns_nullh[] = {0,0,0};
1121 short ns_bh[] = {-1,-1,-1};
1122 
1123 char *
1124 ns_print(struct sockaddr *sa)
1125 {
1126 	register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
1127 	struct ns_addr work;
1128 	union { union ns_net net_e; u_long long_e; } net;
1129 	u_short port;
1130 	static char mybuf[50], cport[10], chost[25];
1131 	char *host = "";
1132 	register char *p; register u_char *q;
1133 
1134 	work = sns->sns_addr;
1135 	port = ntohs(work.x_port);
1136 	work.x_port = 0;
1137 	net.net_e  = work.x_net;
1138 	if (ns_nullhost(work) && net.long_e == 0) {
1139 		if (port ) {
1140 			sprintf(mybuf, "*.%xH", port);
1141 			upHex(mybuf);
1142 		} else
1143 			sprintf(mybuf, "*.*");
1144 		return (mybuf);
1145 	}
1146 
1147 	if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
1148 		host = "any";
1149 	} else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
1150 		host = "*";
1151 	} else {
1152 		q = work.x_host.c_host;
1153 		sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
1154 			q[0], q[1], q[2], q[3], q[4], q[5]);
1155 		for (p = chost; *p == '0' && p < chost + 12; p++)
1156 			continue;
1157 		host = p;
1158 	}
1159 	if (port)
1160 		sprintf(cport, ".%xH", htons(port));
1161 	else
1162 		*cport = 0;
1163 
1164 	sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
1165 	upHex(mybuf);
1166 	return(mybuf);
1167 }
1168 
1169 char *
1170 ns_phost(struct sockaddr *sa)
1171 {
1172 	register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
1173 	struct sockaddr_ns work;
1174 	static union ns_net ns_zeronet;
1175 	char *p;
1176 
1177 	work = *sns;
1178 	work.sns_addr.x_port = 0;
1179 	work.sns_addr.x_net = ns_zeronet;
1180 
1181 	p = ns_print((struct sockaddr *)&work);
1182 	if (strncmp("0H.", p, 3) == 0)
1183 		p += 3;
1184 	return(p);
1185 }
1186 #endif
1187 
1188 void
1189 upHex(char *p0)
1190 {
1191 	register char *p = p0;
1192 
1193 	for (; *p; p++)
1194 		switch (*p) {
1195 
1196 		case 'a':
1197 		case 'b':
1198 		case 'c':
1199 		case 'd':
1200 		case 'e':
1201 		case 'f':
1202 			*p += ('A' - 'a');
1203 			break;
1204 		}
1205 }
1206