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