xref: /freebsd/usr.bin/netstat/route.c (revision 325151a3)
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/sysctl.h>
44 #include <sys/time.h>
45 
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netgraph/ng_socket.h>
54 
55 #include <arpa/inet.h>
56 #include <ifaddrs.h>
57 #include <libutil.h>
58 #include <netdb.h>
59 #include <stdint.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <stdbool.h>
63 #include <string.h>
64 #include <sysexits.h>
65 #include <unistd.h>
66 #include <err.h>
67 #include <libxo/xo.h>
68 #include "netstat.h"
69 #include "nl_defs.h"
70 
71 /*
72  * Definitions for showing gateway flags.
73  */
74 static struct bits {
75 	u_long	b_mask;
76 	char	b_val;
77 	const char *b_name;
78 } bits[] = {
79 	{ RTF_UP,	'U', "up" },
80 	{ RTF_GATEWAY,	'G', "gateway" },
81 	{ RTF_HOST,	'H', "host" },
82 	{ RTF_REJECT,	'R', "reject" },
83 	{ RTF_DYNAMIC,	'D', "dynamic" },
84 	{ RTF_MODIFIED,	'M', "modified" },
85 	{ RTF_DONE,	'd', "done" }, /* Completed -- for routing msgs only */
86 	{ RTF_XRESOLVE,	'X', "xresolve" },
87 	{ RTF_STATIC,	'S', "static" },
88 	{ RTF_PROTO1,	'1', "proto1" },
89 	{ RTF_PROTO2,	'2', "proto2" },
90 	{ RTF_PROTO3,	'3', "proto3" },
91 	{ RTF_BLACKHOLE,'B', "blackhole" },
92 	{ RTF_BROADCAST,'b', "broadcast" },
93 #ifdef RTF_LLINFO
94 	{ RTF_LLINFO,	'L', "llinfo" },
95 #endif
96 	{ 0 , 0, NULL }
97 };
98 
99 struct ifmap_entry {
100 	char ifname[IFNAMSIZ];
101 };
102 static struct ifmap_entry *ifmap;
103 static int ifmap_size;
104 static struct timespec uptime;
105 
106 static const char *netname4(in_addr_t, in_addr_t);
107 static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
108 static void p_rtable_sysctl(int, int);
109 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
110 static void p_sockaddr(const char *name, struct sockaddr *, struct sockaddr *,
111     int, int);
112 static const char *fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask,
113     int flags);
114 static void p_flags(int, const char *);
115 static const char *fmt_flags(int f);
116 static void domask(char *, in_addr_t, u_long);
117 
118 
119 /*
120  * Print routing tables.
121  */
122 void
123 routepr(int fibnum, int af)
124 {
125 	size_t intsize;
126 	int numfibs;
127 
128 	if (live == 0)
129 		return;
130 
131 	intsize = sizeof(int);
132 	if (fibnum == -1 &&
133 	    sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
134 		fibnum = 0;
135 	if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
136 		numfibs = 1;
137 	if (fibnum < 0 || fibnum > numfibs - 1)
138 		errx(EX_USAGE, "%d: invalid fib", fibnum);
139 	/*
140 	 * Since kernel & userland use different timebase
141 	 * (time_uptime vs time_second) and we are reading kernel memory
142 	 * directly we should do rt_expire --> expire_time conversion.
143 	 */
144 	if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
145 		err(EX_OSERR, "clock_gettime() failed");
146 
147 	xo_open_container("route-information");
148 	xo_emit("{T:Routing tables}");
149 	if (fibnum)
150 		xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
151 	xo_emit("\n");
152 	p_rtable_sysctl(fibnum, af);
153 	xo_close_container("route-information");
154 }
155 
156 
157 /*
158  * Print address family header before a section of the routing table.
159  */
160 void
161 pr_family(int af1)
162 {
163 	const char *afname;
164 
165 	switch (af1) {
166 	case AF_INET:
167 		afname = "Internet";
168 		break;
169 #ifdef INET6
170 	case AF_INET6:
171 		afname = "Internet6";
172 		break;
173 #endif /*INET6*/
174 	case AF_ISO:
175 		afname = "ISO";
176 		break;
177 	case AF_CCITT:
178 		afname = "X.25";
179 		break;
180 	case AF_NETGRAPH:
181 		afname = "Netgraph";
182 		break;
183 	default:
184 		afname = NULL;
185 		break;
186 	}
187 	if (afname)
188 		xo_emit("\n{k:address-family/%s}:\n", afname);
189 	else
190 		xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
191 }
192 
193 /* column widths; each followed by one space */
194 #ifndef INET6
195 #define	WID_DST_DEFAULT(af) 	18	/* width of destination column */
196 #define	WID_GW_DEFAULT(af)	18	/* width of gateway column */
197 #define	WID_IF_DEFAULT(af)	(Wflag ? 10 : 8) /* width of netif column */
198 #else
199 #define	WID_DST_DEFAULT(af) \
200 	((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
201 #define	WID_GW_DEFAULT(af) \
202 	((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
203 #define	WID_IF_DEFAULT(af)	((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
204 #endif /*INET6*/
205 
206 static int wid_dst;
207 static int wid_gw;
208 static int wid_flags;
209 static int wid_pksent;
210 static int wid_mtu;
211 static int wid_if;
212 static int wid_expire;
213 
214 /*
215  * Print header for routing table columns.
216  */
217 static void
218 pr_rthdr(int af1 __unused)
219 {
220 
221 	if (Wflag) {
222 		xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
223 		    "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
224 			wid_dst,	wid_dst,	"Destination",
225 			wid_gw,		wid_gw,		"Gateway",
226 			wid_flags,	wid_flags,	"Flags",
227 			wid_pksent,	wid_pksent,	"Use",
228 			wid_mtu,	wid_mtu,	"Mtu",
229 			wid_if,		wid_if,		"Netif",
230 			wid_expire,			"Expire");
231 	} else {
232 		xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
233 		    "{T:/%*s}\n",
234 			wid_dst,	wid_dst,	"Destination",
235 			wid_gw,		wid_gw,		"Gateway",
236 			wid_flags,	wid_flags,	"Flags",
237 			wid_if,		wid_if,		"Netif",
238 			wid_expire,			"Expire");
239 	}
240 }
241 
242 static void
243 p_rtable_sysctl(int fibnum, int af)
244 {
245 	size_t needed;
246 	int mib[7];
247 	char *buf, *next, *lim;
248 	struct rt_msghdr *rtm;
249 	struct sockaddr *sa;
250 	int fam = AF_UNSPEC, ifindex = 0, size;
251 	int need_table_close = false;
252 
253 	struct ifaddrs *ifap, *ifa;
254 	struct sockaddr_dl *sdl;
255 
256 	/*
257 	 * Retrieve interface list at first
258 	 * since we need #ifindex -> if_xname match
259 	 */
260 	if (getifaddrs(&ifap) != 0)
261 		err(EX_OSERR, "getifaddrs");
262 
263 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
264 
265 		if (ifa->ifa_addr->sa_family != AF_LINK)
266 			continue;
267 
268 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
269 		ifindex = sdl->sdl_index;
270 
271 		if (ifindex >= ifmap_size) {
272 			size = roundup(ifindex + 1, 32) *
273 			    sizeof(struct ifmap_entry);
274 			if ((ifmap = realloc(ifmap, size)) == NULL)
275 				errx(2, "realloc(%d) failed", size);
276 			memset(&ifmap[ifmap_size], 0,
277 			    size - ifmap_size *
278 			    sizeof(struct ifmap_entry));
279 
280 			ifmap_size = roundup(ifindex + 1, 32);
281 		}
282 
283 		if (*ifmap[ifindex].ifname != '\0')
284 			continue;
285 
286 		strlcpy(ifmap[ifindex].ifname, ifa->ifa_name, IFNAMSIZ);
287 	}
288 
289 	freeifaddrs(ifap);
290 
291 	mib[0] = CTL_NET;
292 	mib[1] = PF_ROUTE;
293 	mib[2] = 0;
294 	mib[3] = af;
295 	mib[4] = NET_RT_DUMP;
296 	mib[5] = 0;
297 	mib[6] = fibnum;
298 	if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
299 		err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
300 		    fibnum);
301 	if ((buf = malloc(needed)) == NULL)
302 		errx(2, "malloc(%lu)", (unsigned long)needed);
303 	if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
304 		err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
305 	lim  = buf + needed;
306 	xo_open_container("route-table");
307 	xo_open_list("rt-family");
308 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
309 		rtm = (struct rt_msghdr *)next;
310 		if (rtm->rtm_version != RTM_VERSION)
311 			continue;
312 		/*
313 		 * Peek inside header to determine AF
314 		 */
315 		sa = (struct sockaddr *)(rtm + 1);
316 		/* Only print family first time. */
317 		if (fam != sa->sa_family) {
318 			if (need_table_close) {
319 				xo_close_list("rt-entry");
320 				xo_close_instance("rt-family");
321 			}
322 			need_table_close = true;
323 
324 			fam = sa->sa_family;
325 			wid_dst = WID_DST_DEFAULT(fam);
326 			wid_gw = WID_GW_DEFAULT(fam);
327 			wid_flags = 6;
328 			wid_pksent = 8;
329 			wid_mtu = 6;
330 			wid_if = WID_IF_DEFAULT(fam);
331 			wid_expire = 6;
332 			xo_open_instance("rt-family");
333 			pr_family(fam);
334 			xo_open_list("rt-entry");
335 
336 			pr_rthdr(fam);
337 		}
338 		p_rtentry_sysctl("rt-entry", rtm);
339 	}
340 	if (need_table_close) {
341 		xo_close_list("rt-entry");
342 		xo_close_instance("rt-family");
343 	}
344 	xo_close_list("rt-family");
345 	xo_close_container("route-table");
346 	free(buf);
347 }
348 
349 static void
350 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
351 {
352 	struct sockaddr *sa, *addr[RTAX_MAX];
353 	char buffer[128];
354 	char prettyname[128];
355 	int i;
356 
357 	xo_open_instance(name);
358 	sa = (struct sockaddr *)(rtm + 1);
359 	for (i = 0; i < RTAX_MAX; i++) {
360 		if (rtm->rtm_addrs & (1 << i))
361 			addr[i] = sa;
362 		sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
363 	}
364 
365 	p_sockaddr("destination", addr[RTAX_DST], addr[RTAX_NETMASK],
366 	    rtm->rtm_flags, wid_dst);
367 	p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST, wid_gw);
368 	snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
369 	    wid_flags);
370 	p_flags(rtm->rtm_flags, buffer);
371 	if (Wflag) {
372 		xo_emit("{t:use/%*lu} ", wid_pksent, rtm->rtm_rmx.rmx_pksent);
373 
374 		if (rtm->rtm_rmx.rmx_mtu != 0)
375 			xo_emit("{t:mtu/%*lu} ", wid_mtu, rtm->rtm_rmx.rmx_mtu);
376 		else
377 			xo_emit("{P:/%*s} ", wid_mtu, "");
378 	}
379 
380 	memset(prettyname, 0, sizeof(prettyname));
381 	if (rtm->rtm_index < ifmap_size) {
382 		strlcpy(prettyname, ifmap[rtm->rtm_index].ifname,
383 		    sizeof(prettyname));
384 		if (*prettyname == '\0')
385 			strlcpy(prettyname, "---", sizeof(prettyname));
386 	}
387 
388 	xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, prettyname);
389 	if (rtm->rtm_rmx.rmx_expire) {
390 		time_t expire_time;
391 
392 		if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0)
393 			xo_emit(" {:expire-time/%*d}", wid_expire,
394 			    (int)expire_time);
395 	}
396 
397 	xo_emit("\n");
398 	xo_close_instance(name);
399 }
400 
401 static void
402 p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask,
403     int flags, int width)
404 {
405 	const char *cp;
406 	char buf[128];
407 
408 	cp = fmt_sockaddr(sa, mask, flags);
409 
410 	if (width < 0) {
411 		snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
412 		xo_emit(buf, cp);
413 	} else {
414 		if (Wflag != 0 || numeric_addr) {
415 			snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
416 			    -width, name);
417 			xo_emit(buf, cp);
418 		} else {
419 			snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
420 			    -width, name);
421 			xo_emit(buf, width, cp);
422 		}
423 	}
424 }
425 
426 static const char *
427 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
428 {
429 	static char buf[128];
430 	const char *cp;
431 
432 	if (sa == NULL)
433 		return ("null");
434 
435 	switch(sa->sa_family) {
436 #ifdef INET6
437 	case AF_INET6:
438 		/*
439 		 * The sa6->sin6_scope_id must be filled here because
440 		 * this sockaddr is extracted from kmem(4) directly
441 		 * and has KAME-specific embedded scope id in
442 		 * sa6->sin6_addr.s6_addr[2].
443 		 */
444 		in6_fillscopeid(satosin6(sa));
445 		/* FALLTHROUGH */
446 #endif /*INET6*/
447 	case AF_INET:
448 		if (flags & RTF_HOST)
449 			cp = routename(sa, numeric_addr);
450 		else if (mask)
451 			cp = netname(sa, mask);
452 		else
453 			cp = netname(sa, NULL);
454 		break;
455 	case AF_NETGRAPH:
456 	    {
457 		strlcpy(buf, ((struct sockaddr_ng *)sa)->sg_data,
458 		    sizeof(buf));
459 		cp = buf;
460 		break;
461 	    }
462 	case AF_LINK:
463 	    {
464 #if 0
465 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
466 
467 		/* Interface route. */
468 		if (sdl->sdl_nlen)
469 			cp = sdl->sdl_data;
470 		else
471 #endif
472 			cp = routename(sa, 1);
473 		break;
474 	    }
475 	default:
476 	    {
477 		u_char *s = (u_char *)sa->sa_data, *slim;
478 		char *cq, *cqlim;
479 
480 		cq = buf;
481 		slim =  sa->sa_len + (u_char *) sa;
482 		cqlim = cq + sizeof(buf) - 6;
483 		cq += sprintf(cq, "(%d)", sa->sa_family);
484 		while (s < slim && cq < cqlim) {
485 			cq += sprintf(cq, " %02x", *s++);
486 			if (s < slim)
487 			    cq += sprintf(cq, "%02x", *s++);
488 		}
489 		cp = buf;
490 	    }
491 	}
492 
493 	return (cp);
494 }
495 
496 static void
497 p_flags(int f, const char *format)
498 {
499 	struct bits *p;
500 
501 	xo_emit(format, fmt_flags(f));
502 
503 	xo_open_list("flags_pretty");
504 	for (p = bits; p->b_mask; p++)
505 		if (p->b_mask & f)
506 			xo_emit("{le:flags_pretty/%s}", p->b_name);
507 	xo_close_list("flags_pretty");
508 }
509 
510 static const char *
511 fmt_flags(int f)
512 {
513 	static char name[33];
514 	char *flags;
515 	struct bits *p = bits;
516 
517 	for (flags = name; p->b_mask; p++)
518 		if (p->b_mask & f)
519 			*flags++ = p->b_val;
520 	*flags = '\0';
521 	return (name);
522 }
523 
524 char *
525 routename(struct sockaddr *sa, int flags)
526 {
527 	static char line[NI_MAXHOST];
528 	int error, f;
529 
530 	f = (flags) ? NI_NUMERICHOST : 0;
531 	error = getnameinfo(sa, sa->sa_len, line, sizeof(line),
532 	    NULL, 0, f);
533 	if (error) {
534 		const void *src;
535 		switch (sa->sa_family) {
536 #ifdef INET
537 		case AF_INET:
538 			src = &satosin(sa)->sin_addr;
539 			break;
540 #endif /* INET */
541 #ifdef INET6
542 		case AF_INET6:
543 			src = &satosin6(sa)->sin6_addr;
544 			break;
545 #endif /* INET6 */
546 		default:
547 			return(line);
548 		}
549 		inet_ntop(sa->sa_family, src, line, sizeof(line) - 1);
550 		return (line);
551 	}
552 	trimdomain(line, strlen(line));
553 
554 	return (line);
555 }
556 
557 #define	NSHIFT(m) (							\
558 	(m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT :			\
559 	(m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT :			\
560 	(m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT :			\
561 	0)
562 
563 static void
564 domask(char *dst, in_addr_t addr __unused, u_long mask)
565 {
566 	int b, i;
567 
568 	if (mask == 0) {
569 		*dst = '\0';
570 		return;
571 	}
572 	i = 0;
573 	for (b = 0; b < 32; b++)
574 		if (mask & (1 << b)) {
575 			int bb;
576 
577 			i = b;
578 			for (bb = b+1; bb < 32; bb++)
579 				if (!(mask & (1 << bb))) {
580 					i = -1;	/* noncontig */
581 					break;
582 				}
583 			break;
584 		}
585 	if (i == -1)
586 		sprintf(dst, "&0x%lx", mask);
587 	else
588 		sprintf(dst, "/%d", 32-i);
589 }
590 
591 /*
592  * Return the name of the network whose address is given.
593  */
594 const char *
595 netname(struct sockaddr *sa, struct sockaddr *mask)
596 {
597 	switch (sa->sa_family) {
598 	case AF_INET:
599 		if (mask != NULL)
600 			return (netname4(satosin(sa)->sin_addr.s_addr,
601 			    satosin(mask)->sin_addr.s_addr));
602 		else
603 			return (netname4(satosin(sa)->sin_addr.s_addr,
604 			    INADDR_ANY));
605 		break;
606 #ifdef INET6
607 	case AF_INET6:
608 		return (netname6(satosin6(sa), satosin6(mask)));
609 #endif /* INET6 */
610 	default:
611 		return (NULL);
612 	}
613 }
614 
615 static const char *
616 netname4(in_addr_t in, in_addr_t mask)
617 {
618 	char *cp = 0;
619 	static char line[MAXHOSTNAMELEN + sizeof("/xx")];
620 	char nline[INET_ADDRSTRLEN];
621 	struct netent *np = 0;
622 	in_addr_t i;
623 
624 	if (in == INADDR_ANY && mask == 0) {
625 		strlcpy(line, "default", sizeof(line));
626 		return (line);
627 	}
628 
629 	/* It is ok to supply host address. */
630 	in &= mask;
631 
632 	i = ntohl(in);
633 	if (!numeric_addr && i) {
634 		np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET);
635 		if (np != NULL) {
636 			cp = np->n_name;
637 			trimdomain(cp, strlen(cp));
638 		}
639 	}
640 	inet_ntop(AF_INET, &in, nline, sizeof(line));
641 	if (cp != NULL) {
642 		if (strcpy(cp, nline) != 0)
643 			return (line);
644 		strlcpy(line, cp, sizeof(line));
645 	} else
646 		strlcpy(line, nline, sizeof(line));
647 	domask(line + strlen(line), i, ntohl(mask));
648 
649 	return (line);
650 }
651 
652 #undef NSHIFT
653 
654 #ifdef INET6
655 void
656 in6_fillscopeid(struct sockaddr_in6 *sa6)
657 {
658 #if defined(__KAME__)
659 	/*
660 	 * XXX: This is a special workaround for KAME kernels.
661 	 * sin6_scope_id field of SA should be set in the future.
662 	 */
663 	if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) ||
664 	    IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) ||
665 	    IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
666 		if (sa6->sin6_scope_id == 0)
667 			sa6->sin6_scope_id =
668 			    ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]);
669 		sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0;
670 	}
671 #endif
672 }
673 
674 /* Mask to length table.  To check an invalid value, (length + 1) is used. */
675 static int masktolen[256] = {
676 	[0xff] = 8 + 1,
677 	[0xfe] = 7 + 1,
678 	[0xfc] = 6 + 1,
679 	[0xf8] = 5 + 1,
680 	[0xf0] = 4 + 1,
681 	[0xe0] = 3 + 1,
682 	[0xc0] = 2 + 1,
683 	[0x80] = 1 + 1,
684 	[0x00] = 0 + 1,
685 };
686 
687 static const char *
688 netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
689 {
690 	static char line[NI_MAXHOST + sizeof("/xxx") - 1];
691 	char nline[NI_MAXHOST];
692 	u_char *p, *lim;
693 	int masklen, illegal = 0;
694 
695 	if (mask) {
696 		p = (u_char *)&mask->sin6_addr;
697 		for (masklen = 0, lim = p + 16; p < lim; p++) {
698 			if (masktolen[*p] > 0)
699 				/* -1 is required. */
700 				masklen += masktolen[*p] - 1;
701 			else
702 				illegal++;
703 		}
704 		if (illegal)
705 			xo_error("illegal prefixlen\n");
706 	}
707 	else
708 		masklen = 128;
709 
710 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
711 		return("default");
712 
713 	getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline),
714 	    NULL, 0, NI_NUMERICHOST);
715 	if (numeric_addr)
716 		strlcpy(line, nline, sizeof(line));
717 	else
718 		getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line,
719 		    sizeof(line), NULL, 0, 0);
720 	if (numeric_addr || strcmp(line, nline) == 0)
721 		sprintf(&line[strlen(line)], "/%d", masklen);
722 
723 	return (line);
724 }
725 #endif /*INET6*/
726 
727 /*
728  * Print routing statistics
729  */
730 void
731 rt_stats(void)
732 {
733 	struct rtstat rtstat;
734 	u_long rtsaddr, rttaddr;
735 	int rttrash;
736 
737 	if ((rtsaddr = nl[N_RTSTAT].n_value) == 0) {
738 		xo_emit("{W:rtstat: symbol not in namelist}\n");
739 		return;
740 	}
741 	if ((rttaddr = nl[N_RTTRASH].n_value) == 0) {
742 		xo_emit("{W:rttrash: symbol not in namelist}\n");
743 		return;
744 	}
745 	kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
746 	kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
747 	xo_emit("{T:routing}:\n");
748 
749 #define	p(f, m) if (rtstat.f || sflag <= 1) \
750 	xo_emit(m, rtstat.f, plural(rtstat.f))
751 
752 	p(rts_badredirect, "\t{:bad-redirects/%hu} "
753 	    "{N:/bad routing redirect%s}\n");
754 	p(rts_dynamic, "\t{:dynamically-created/%hu} "
755 	    "{N:/dynamically created route%s}\n");
756 	p(rts_newgateway, "\t{:new-gateways/%hu} "
757 	    "{N:/new gateway%s due to redirects}\n");
758 	p(rts_unreach, "\t{:unreachable-destination/%hu} "
759 	    "{N:/destination%s found unreachable}\n");
760 	p(rts_wildcard, "\t{:wildcard-uses/%hu} "
761 	    "{N:/use%s of a wildcard route}\n");
762 #undef p
763 
764 	if (rttrash || sflag <= 1)
765 		xo_emit("\t{:unused-but-not-freed/%u} "
766 		    "{N:/route%s not in table but not freed}\n",
767 		    rttrash, plural(rttrash));
768 }
769