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