xref: /freebsd/usr.bin/netstat/route.c (revision 588ff6c0cc9aaf10ba19080d9f8acbd8be36abf3)
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 <stdint.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sysexits.h>
71 #include <unistd.h>
72 #include <err.h>
73 #include "netstat.h"
74 
75 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
76 
77 /*
78  * Definitions for showing gateway flags.
79  */
80 struct bits {
81 	u_long	b_mask;
82 	char	b_val;
83 } bits[] = {
84 	{ RTF_UP,	'U' },
85 	{ RTF_GATEWAY,	'G' },
86 	{ RTF_HOST,	'H' },
87 	{ RTF_REJECT,	'R' },
88 	{ RTF_DYNAMIC,	'D' },
89 	{ RTF_MODIFIED,	'M' },
90 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
91 	{ RTF_CLONING,	'C' },
92 	{ RTF_XRESOLVE,	'X' },
93 	{ RTF_LLINFO,	'L' },
94 	{ RTF_STATIC,	'S' },
95 	{ RTF_PROTO1,	'1' },
96 	{ RTF_PROTO2,	'2' },
97 	{ RTF_WASCLONED,'W' },
98 	{ RTF_PRCLONING,'c' },
99 	{ RTF_PROTO3,	'3' },
100 	{ RTF_BLACKHOLE,'B' },
101 	{ RTF_BROADCAST,'b' },
102 	{ 0 , 0 }
103 };
104 
105 typedef union {
106 	long	dummy;		/* Helps align structure. */
107 	struct	sockaddr u_sa;
108 	u_short	u_data[128];
109 } sa_u;
110 
111 static sa_u pt_u;
112 
113 int	do_rtent = 0;
114 struct	rtentry rtentry;
115 struct	radix_node rnode;
116 struct	radix_mask rmask;
117 struct	radix_node_head *rt_tables[AF_MAX+1];
118 
119 int	NewTree = 0;
120 
121 struct	timespec uptime;
122 
123 static struct sockaddr *kgetsa (struct sockaddr *);
124 static void size_cols (int ef, struct radix_node *rn);
125 static void size_cols_tree (struct radix_node *rn);
126 static void size_cols_rtentry (struct rtentry *rt);
127 static void p_tree (struct radix_node *);
128 static void p_rtnode (void);
129 static void ntreestuff (void);
130 static void np_rtentry (struct rt_msghdr *);
131 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
132 static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask,
133 				 int flags);
134 static void p_flags (int, const char *);
135 static const char *fmt_flags(int f);
136 static void p_rtentry (struct rtentry *);
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 		strlcpy(workbuf, ((struct sockaddr_ng *)sa)->sg_data,
644 		        sizeof(workbuf));
645 		cp = workbuf;
646 		break;
647 	    }
648 
649 	case AF_LINK:
650 	    {
651 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
652 
653 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
654 		    sdl->sdl_slen == 0) {
655 			(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
656 			cp = workbuf;
657 		} else
658 			switch (sdl->sdl_type) {
659 
660 			case IFT_ETHER:
661 			case IFT_L2VLAN:
662 				if (sdl->sdl_alen == ETHER_ADDR_LEN) {
663 					cp = ether_ntoa((struct ether_addr *)
664 					    (sdl->sdl_data + sdl->sdl_nlen));
665 					break;
666 				}
667 				/* FALLTHROUGH */
668 			default:
669 				cp = link_ntoa(sdl);
670 				break;
671 			}
672 		break;
673 	    }
674 
675 	default:
676 	    {
677 		u_char *s = (u_char *)sa->sa_data, *slim;
678 		char *cq, *cqlim;
679 
680 		cq = workbuf;
681 		slim =  sa->sa_len + (u_char *) sa;
682 		cqlim = cq + sizeof(workbuf) - 6;
683 		cq += sprintf(cq, "(%d)", sa->sa_family);
684 		while (s < slim && cq < cqlim) {
685 			cq += sprintf(cq, " %02x", *s++);
686 			if (s < slim)
687 			    cq += sprintf(cq, "%02x", *s++);
688 		}
689 		cp = workbuf;
690 	    }
691 	}
692 
693 	return (cp);
694 }
695 
696 static void
697 p_flags(int f, const char *format)
698 {
699 	printf(format, fmt_flags(f));
700 }
701 
702 static const char *
703 fmt_flags(int f)
704 {
705 	static char name[33];
706 	char *flags;
707 	struct bits *p = bits;
708 
709 	for (flags = name; p->b_mask; p++)
710 		if (p->b_mask & f)
711 			*flags++ = p->b_val;
712 	*flags = '\0';
713 	return (name);
714 }
715 
716 static void
717 p_rtentry(struct rtentry *rt)
718 {
719 	static struct ifnet ifnet, *lastif;
720 	struct rtentry parent;
721 	static char buffer[128];
722 	static char prettyname[128];
723 	struct sockaddr *sa;
724 	sa_u addr, mask;
725 
726 	/*
727 	 * Don't print protocol-cloned routes unless -a.
728 	 */
729 	if (rt->rt_flags & RTF_WASCLONED && !aflag) {
730 		kget(rt->rt_parent, parent);
731 		if (parent.rt_flags & RTF_PRCLONING)
732 			return;
733 	}
734 
735 	bzero(&addr, sizeof(addr));
736 	if ((sa = kgetsa(rt_key(rt))))
737 		bcopy(sa, &addr, sa->sa_len);
738 	bzero(&mask, sizeof(mask));
739 	if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
740 		bcopy(sa, &mask, sa->sa_len);
741 	p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst);
742 	p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw);
743 	snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags);
744 	p_flags(rt->rt_flags, buffer);
745 	if (addr.u_sa.sa_family == AF_INET || Wflag) {
746 		printf("%*ld %*lu ", wid_refs, rt->rt_refcnt,
747 				     wid_use, rt->rt_use);
748 		if (Wflag) {
749 			if (rt->rt_rmx.rmx_mtu != 0)
750 				printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu);
751 			else
752 				printf("%*s ", wid_mtu, "");
753 		}
754 	}
755 	if (rt->rt_ifp) {
756 		if (rt->rt_ifp != lastif) {
757 			kget(rt->rt_ifp, ifnet);
758 			lastif = rt->rt_ifp;
759 			strlcpy(prettyname, ifnet.if_xname, sizeof(prettyname));
760 		}
761 		printf("%*.*s", wid_if, wid_if, prettyname);
762 		if (rt->rt_rmx.rmx_expire) {
763 			time_t expire_time;
764 
765 			if ((expire_time =
766 			    rt->rt_rmx.rmx_expire - uptime.tv_sec) > 0)
767 				printf(" %*d", wid_expire, (int)expire_time);
768 		}
769 		if (rt->rt_nodes[0].rn_dupedkey)
770 			printf(" =>");
771 	}
772 	putchar('\n');
773 }
774 
775 char *
776 routename(u_long in)
777 {
778 	char *cp;
779 	static char line[MAXHOSTNAMELEN];
780 	struct hostent *hp;
781 
782 	cp = 0;
783 	if (!numeric_addr) {
784 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
785 			AF_INET);
786 		if (hp) {
787 			cp = hp->h_name;
788 			trimdomain(cp, strlen(cp));
789 		}
790 	}
791 	if (cp) {
792 		strncpy(line, cp, sizeof(line) - 1);
793 		line[sizeof(line) - 1] = '\0';
794 	} else {
795 #define C(x)	((x) & 0xff)
796 		in = ntohl(in);
797 		sprintf(line, "%lu.%lu.%lu.%lu",
798 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
799 	}
800 	return (line);
801 }
802 
803 #define	NSHIFT(m) (							\
804 	(m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :			\
805 	(m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :			\
806 	(m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :			\
807 	0)
808 
809 static void
810 domask(char *dst, u_long addr, u_long mask)
811 {
812 	int b, i;
813 
814 	if (mask == 0 || (!numeric_addr && NSHIFT(mask) != 0)) {
815 		*dst = '\0';
816 		return;
817 	}
818 	i = 0;
819 	for (b = 0; b < 32; b++)
820 		if (mask & (1 << b)) {
821 			int bb;
822 
823 			i = b;
824 			for (bb = b+1; bb < 32; bb++)
825 				if (!(mask & (1 << bb))) {
826 					i = -1;	/* noncontig */
827 					break;
828 				}
829 			break;
830 		}
831 	if (i == -1)
832 		sprintf(dst, "&0x%lx", mask);
833 	else
834 		sprintf(dst, "/%d", 32-i);
835 }
836 
837 /*
838  * Return the name of the network whose address is given.
839  * The address is assumed to be that of a net or subnet, not a host.
840  */
841 char *
842 netname(u_long in, u_long mask)
843 {
844 	char *cp = 0;
845 	static char line[MAXHOSTNAMELEN];
846 	struct netent *np = 0;
847 	u_long i;
848 
849 	i = ntohl(in);
850 	if (!numeric_addr && i) {
851 		np = getnetbyaddr(i >> NSHIFT(mask), AF_INET);
852 		if (np != NULL) {
853 			cp = np->n_name;
854 			trimdomain(cp, strlen(cp));
855 		}
856 	}
857 	if (cp != NULL) {
858 		strncpy(line, cp, sizeof(line) - 1);
859 		line[sizeof(line) - 1] = '\0';
860 	} else {
861 		inet_ntop(AF_INET, (char *)&in, line, sizeof(line) - 1);
862 	}
863 	domask(line + strlen(line), i, mask);
864 	return (line);
865 }
866 
867 #undef NSHIFT
868 
869 #ifdef INET6
870 const char *
871 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
872 {
873 	static char line[MAXHOSTNAMELEN];
874 	u_char *p = (u_char *)mask;
875 	u_char *lim;
876 	int masklen, illegal = 0, flag = 0;
877 
878 	if (mask) {
879 		for (masklen = 0, lim = p + 16; p < lim; p++) {
880 			switch (*p) {
881 			 case 0xff:
882 				 masklen += 8;
883 				 break;
884 			 case 0xfe:
885 				 masklen += 7;
886 				 break;
887 			 case 0xfc:
888 				 masklen += 6;
889 				 break;
890 			 case 0xf8:
891 				 masklen += 5;
892 				 break;
893 			 case 0xf0:
894 				 masklen += 4;
895 				 break;
896 			 case 0xe0:
897 				 masklen += 3;
898 				 break;
899 			 case 0xc0:
900 				 masklen += 2;
901 				 break;
902 			 case 0x80:
903 				 masklen += 1;
904 				 break;
905 			 case 0x00:
906 				 break;
907 			 default:
908 				 illegal ++;
909 				 break;
910 			}
911 		}
912 		if (illegal)
913 			fprintf(stderr, "illegal prefixlen\n");
914 	}
915 	else
916 		masklen = 128;
917 
918 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
919 		return("default");
920 
921 	if (numeric_addr)
922 		flag |= NI_NUMERICHOST;
923 	getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
924 		    NULL, 0, flag);
925 
926 	if (numeric_addr)
927 		sprintf(&line[strlen(line)], "/%d", masklen);
928 
929 	return line;
930 }
931 
932 char *
933 routename6(struct sockaddr_in6 *sa6)
934 {
935 	static char line[MAXHOSTNAMELEN];
936 	int flag = 0;
937 	/* use local variable for safety */
938 	struct sockaddr_in6 sa6_local;
939 
940 	sa6_local.sin6_family = AF_INET6;
941 	sa6_local.sin6_len = sizeof(sa6_local);
942 	sa6_local.sin6_addr = sa6->sin6_addr;
943 	sa6_local.sin6_scope_id = sa6->sin6_scope_id;
944 
945 	if (numeric_addr)
946 		flag |= NI_NUMERICHOST;
947 
948 	getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
949 		    line, sizeof(line), NULL, 0, flag);
950 
951 	return line;
952 }
953 #endif /*INET6*/
954 
955 /*
956  * Print routing statistics
957  */
958 void
959 rt_stats(u_long rtsaddr, u_long rttaddr)
960 {
961 	struct rtstat rtstat;
962 	int rttrash;
963 
964 	if (rtsaddr == 0) {
965 		printf("rtstat: symbol not in namelist\n");
966 		return;
967 	}
968 	if (rttaddr == 0) {
969 		printf("rttrash: symbol not in namelist\n");
970 		return;
971 	}
972 	kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
973 	kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
974 	printf("routing:\n");
975 
976 #define	p(f, m) if (rtstat.f || sflag <= 1) \
977 	printf(m, rtstat.f, plural(rtstat.f))
978 
979 	p(rts_badredirect, "\t%u bad routing redirect%s\n");
980 	p(rts_dynamic, "\t%u dynamically created route%s\n");
981 	p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
982 	p(rts_unreach, "\t%u destination%s found unreachable\n");
983 	p(rts_wildcard, "\t%u use%s of a wildcard route\n");
984 #undef p
985 
986 	if (rttrash || sflag <= 1)
987 		printf("\t%u route%s not in table but not freed\n",
988 		    rttrash, plural(rttrash));
989 }
990 
991 char *
992 ipx_print(struct sockaddr *sa)
993 {
994 	u_short port;
995 	struct servent *sp = 0;
996 	const char *net = "", *host = "";
997 	char *p;
998 	u_char *q;
999 	struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
1000 	static char mybuf[50];
1001 	char cport[10], chost[15], cnet[15];
1002 
1003 	port = ntohs(work.x_port);
1004 
1005 	if (ipx_nullnet(work) && ipx_nullhost(work)) {
1006 
1007 		if (port) {
1008 			if (sp)
1009 				sprintf(mybuf, "*.%s", sp->s_name);
1010 			else
1011 				sprintf(mybuf, "*.%x", port);
1012 		} else
1013 			sprintf(mybuf, "*.*");
1014 
1015 		return (mybuf);
1016 	}
1017 
1018 	if (ipx_wildnet(work))
1019 		net = "any";
1020 	else if (ipx_nullnet(work))
1021 		net = "*";
1022 	else {
1023 		q = work.x_net.c_net;
1024 		sprintf(cnet, "%02x%02x%02x%02x",
1025 			q[0], q[1], q[2], q[3]);
1026 		for (p = cnet; *p == '0' && p < cnet + 8; p++)
1027 			continue;
1028 		net = p;
1029 	}
1030 
1031 	if (ipx_wildhost(work))
1032 		host = "any";
1033 	else if (ipx_nullhost(work))
1034 		host = "*";
1035 	else {
1036 		q = work.x_host.c_host;
1037 		sprintf(chost, "%02x%02x%02x%02x%02x%02x",
1038 			q[0], q[1], q[2], q[3], q[4], q[5]);
1039 		for (p = chost; *p == '0' && p < chost + 12; p++)
1040 			continue;
1041 		host = p;
1042 	}
1043 
1044 	if (port) {
1045 		if (strcmp(host, "*") == 0)
1046 			host = "";
1047 		if (sp)
1048 			snprintf(cport, sizeof(cport),
1049 				"%s%s", *host ? "." : "", sp->s_name);
1050 		else
1051 			snprintf(cport, sizeof(cport),
1052 				"%s%x", *host ? "." : "", port);
1053 	} else
1054 		*cport = 0;
1055 
1056 	snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
1057 	return(mybuf);
1058 }
1059 
1060 char *
1061 ipx_phost(struct sockaddr *sa)
1062 {
1063 	struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
1064 	struct sockaddr_ipx work;
1065 	static union ipx_net ipx_zeronet;
1066 	char *p;
1067 	struct ipx_addr in;
1068 
1069 	work = *sipx;
1070 	in = work.sipx_addr;
1071 
1072 	work.sipx_addr.x_port = 0;
1073 	work.sipx_addr.x_net = ipx_zeronet;
1074 	p = ipx_print((struct sockaddr *)&work);
1075 	if (strncmp("*.", p, 2) == 0) p += 2;
1076 
1077 	return(p);
1078 }
1079 
1080 void
1081 upHex(char *p0)
1082 {
1083 	char *p = p0;
1084 
1085 	for (; *p; p++)
1086 		switch (*p) {
1087 
1088 		case 'a':
1089 		case 'b':
1090 		case 'c':
1091 		case 'd':
1092 		case 'e':
1093 		case 'f':
1094 			*p += ('A' - 'a');
1095 			break;
1096 		}
1097 }
1098