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