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