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