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