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 #ifdef FSTACK
76 #include <time.h>
77 #endif
78
79 /*
80 * Definitions for showing gateway flags.
81 */
82 struct bits rt_bits[] = {
83 { RTF_UP, 'U', "up" },
84 { RTF_GATEWAY, 'G', "gateway" },
85 { RTF_HOST, 'H', "host" },
86 { RTF_REJECT, 'R', "reject" },
87 { RTF_DYNAMIC, 'D', "dynamic" },
88 { RTF_MODIFIED, 'M', "modified" },
89 { RTF_DONE, 'd', "done" }, /* Completed -- for routing msgs only */
90 { RTF_XRESOLVE, 'X', "xresolve" },
91 { RTF_STATIC, 'S', "static" },
92 { RTF_PROTO1, '1', "proto1" },
93 { RTF_PROTO2, '2', "proto2" },
94 { RTF_PROTO3, '3', "proto3" },
95 { RTF_BLACKHOLE,'B', "blackhole" },
96 { RTF_BROADCAST,'b', "broadcast" },
97 #ifdef RTF_LLINFO
98 { RTF_LLINFO, 'L', "llinfo" },
99 #endif
100 { 0 , 0, NULL }
101 };
102
103 static struct ifmap_entry *ifmap;
104 static size_t ifmap_size;
105 static struct timespec uptime;
106
107 static const char *netname4(in_addr_t, in_addr_t);
108 #ifdef INET6
109 static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
110 #endif
111 static void p_rtable_sysctl(int, int);
112 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
113 static void p_flags(int, const char *);
114 static void domask(char *, size_t, u_long);
115
116
117 /*
118 * Print routing tables.
119 */
120 void
routepr(int fibnum,int af)121 routepr(int fibnum, int af)
122 {
123 size_t intsize;
124 int numfibs;
125
126 if (live == 0)
127 return;
128
129 intsize = sizeof(int);
130 if (fibnum == -1 &&
131 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
132 fibnum = 0;
133 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
134 numfibs = 1;
135 if (fibnum < 0 || fibnum > numfibs - 1)
136 errx(EX_USAGE, "%d: invalid fib", fibnum);
137 /*
138 * Since kernel & userland use different timebase
139 * (time_uptime vs time_second) and we are reading kernel memory
140 * directly we should do rt_expire --> expire_time conversion.
141 */
142 #ifndef FSTACK
143 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
144 #else
145 if (clock_gettime(CLOCK_BOOTTIME, &uptime) < 0)
146 #endif
147 err(EX_OSERR, "clock_gettime() failed");
148
149 xo_open_container("route-information");
150 xo_emit("{T:Routing tables}");
151 if (fibnum)
152 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
153 xo_emit("\n");
154 p_rtable_sysctl(fibnum, af);
155 xo_close_container("route-information");
156 }
157
158
159 /*
160 * Print address family header before a section of the routing table.
161 */
162 void
pr_family(int af1)163 pr_family(int af1)
164 {
165 const char *afname;
166
167 switch (af1) {
168 case AF_INET:
169 afname = "Internet";
170 break;
171 #ifdef INET6
172 case AF_INET6:
173 afname = "Internet6";
174 break;
175 #endif /*INET6*/
176 case AF_ISO:
177 afname = "ISO";
178 break;
179 case AF_CCITT:
180 afname = "X.25";
181 break;
182 case AF_NETGRAPH:
183 afname = "Netgraph";
184 break;
185 default:
186 afname = NULL;
187 break;
188 }
189 if (afname)
190 xo_emit("\n{k:address-family/%s}:\n", afname);
191 else
192 xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
193 }
194
195 /* column widths; each followed by one space */
196 #ifndef INET6
197 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
198 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
199 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
200 #else
201 #define WID_DST_DEFAULT(af) \
202 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
203 #define WID_GW_DEFAULT(af) \
204 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
205 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
206 #endif /*INET6*/
207
208 static int wid_dst;
209 static int wid_gw;
210 static int wid_flags;
211 static int wid_pksent;
212 static int wid_mtu;
213 static int wid_if;
214 static int wid_expire;
215
216 /*
217 * Print header for routing table columns.
218 */
219 static void
pr_rthdr(int af1 __unused)220 pr_rthdr(int af1 __unused)
221 {
222
223 if (Wflag) {
224 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
225 "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
226 wid_dst, wid_dst, "Destination",
227 wid_gw, wid_gw, "Gateway",
228 wid_flags, wid_flags, "Flags",
229 wid_mtu, wid_mtu, "Nhop#",
230 wid_mtu, wid_mtu, "Mtu",
231 wid_if, wid_if, "Netif",
232 wid_expire, "Expire");
233 } else {
234 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
235 "{T:/%*s}\n",
236 wid_dst, wid_dst, "Destination",
237 wid_gw, wid_gw, "Gateway",
238 wid_flags, wid_flags, "Flags",
239 wid_if, wid_if, "Netif",
240 wid_expire, "Expire");
241 }
242 }
243
244 static void
p_rtable_sysctl(int fibnum,int af)245 p_rtable_sysctl(int fibnum, int af)
246 {
247 size_t needed;
248 int mib[7];
249 char *buf, *next, *lim;
250 struct rt_msghdr *rtm;
251 struct sockaddr *sa;
252 int fam = AF_UNSPEC;
253 int need_table_close = false;
254
255 ifmap = prepare_ifmap(&ifmap_size);
256
257 mib[0] = CTL_NET;
258 mib[1] = PF_ROUTE;
259 mib[2] = 0;
260 mib[3] = af;
261 mib[4] = NET_RT_DUMP;
262 mib[5] = 0;
263 mib[6] = fibnum;
264 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
265 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
266 fibnum);
267 if ((buf = malloc(needed)) == NULL)
268 errx(2, "malloc(%lu)", (unsigned long)needed);
269 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
270 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
271 lim = buf + needed;
272 xo_open_container("route-table");
273 xo_open_list("rt-family");
274 for (next = buf; next < lim; next += rtm->rtm_msglen) {
275 rtm = (struct rt_msghdr *)next;
276 if (rtm->rtm_version != RTM_VERSION)
277 continue;
278 /*
279 * Peek inside header to determine AF
280 */
281 sa = (struct sockaddr *)(rtm + 1);
282 /* Only print family first time. */
283 if (fam != sa->sa_family) {
284 if (need_table_close) {
285 xo_close_list("rt-entry");
286 xo_close_instance("rt-family");
287 }
288 need_table_close = true;
289
290 fam = sa->sa_family;
291 wid_dst = WID_DST_DEFAULT(fam);
292 wid_gw = WID_GW_DEFAULT(fam);
293 wid_flags = 6;
294 wid_pksent = 8;
295 wid_mtu = 6;
296 wid_if = WID_IF_DEFAULT(fam);
297 wid_expire = 6;
298 xo_open_instance("rt-family");
299 pr_family(fam);
300 xo_open_list("rt-entry");
301
302 pr_rthdr(fam);
303 }
304 p_rtentry_sysctl("rt-entry", rtm);
305 }
306 if (need_table_close) {
307 xo_close_list("rt-entry");
308 xo_close_instance("rt-family");
309 }
310 xo_close_list("rt-family");
311 xo_close_container("route-table");
312 free(buf);
313 }
314
315 static void
p_rtentry_sysctl(const char * name,struct rt_msghdr * rtm)316 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
317 {
318 struct sockaddr *sa, *addr[RTAX_MAX];
319 char buffer[128];
320 char prettyname[128];
321 int i, protrusion;
322
323 xo_open_instance(name);
324 sa = (struct sockaddr *)(rtm + 1);
325 for (i = 0; i < RTAX_MAX; i++) {
326 if (rtm->rtm_addrs & (1 << i)) {
327 addr[i] = sa;
328 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
329 }
330 }
331
332 protrusion = p_sockaddr("destination", addr[RTAX_DST],
333 addr[RTAX_NETMASK],
334 rtm->rtm_flags, wid_dst);
335 protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST,
336 wid_gw - protrusion);
337 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
338 wid_flags - protrusion);
339 p_flags(rtm->rtm_flags, buffer);
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_if, prettyname);
360 else
361 xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if,
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
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 static 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 #ifndef FSTACK
711 u_long rtsaddr, rttaddr;
712 int rttrash;
713
714 if ((rtsaddr = nl[N_RTSTAT].n_value) == 0) {
715 xo_emit("{W:rtstat: symbol not in namelist}\n");
716 return;
717 }
718 if ((rttaddr = nl[N_RTTRASH].n_value) == 0) {
719 xo_emit("{W:rttrash: symbol not in namelist}\n");
720 return;
721 }
722 kread_counters(rtsaddr, (char *)&rtstat, sizeof (rtstat));
723 kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
724 #else
725 if (fetch_stats("net.rtstat", 0, &rtstat,
726 sizeof(rtstat), kread_counters) != 0)
727 return;
728 #endif
729 xo_emit("{T:routing}:\n");
730
731 #define p(f, m) if (rtstat.f || sflag <= 1) \
732 xo_emit(m, rtstat.f, plural(rtstat.f))
733
734 p(rts_badredirect, "\t{:bad-redirects/%ju} "
735 "{N:/bad routing redirect%s}\n");
736 p(rts_dynamic, "\t{:dynamically-created/%ju} "
737 "{N:/dynamically created route%s}\n");
738 p(rts_newgateway, "\t{:new-gateways/%ju} "
739 "{N:/new gateway%s due to redirects}\n");
740 p(rts_unreach, "\t{:unreachable-destination/%ju} "
741 "{N:/destination%s found unreachable}\n");
742 p(rts_wildcard, "\t{:wildcard-uses/%ju} "
743 "{N:/use%s of a wildcard route}\n");
744 #undef p
745
746 #ifndef FSTACK
747 if (rttrash || sflag <= 1)
748 xo_emit("\t{:unused-but-not-freed/%u} "
749 "{N:/route%s not in table but not freed}\n",
750 rttrash, plural(rttrash));
751 #endif
752
753 }
754