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