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