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