2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
33 #include <sys/protosw.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/sysctl.h>
39 #include <net/ethernet.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netgraph/ng_socket.h>
48 #include <arpa/inet.h>
67 * Definitions for showing gateway flags.
69 struct bits rt_bits[] = {
70 { RTF_UP, 'U', "up" },
71 { RTF_GATEWAY, 'G', "gateway" },
72 { RTF_HOST, 'H', "host" },
73 { RTF_REJECT, 'R', "reject" },
74 { RTF_DYNAMIC, 'D', "dynamic" },
75 { RTF_MODIFIED, 'M', "modified" },
76 { RTF_DONE, 'd', "done" }, /* Completed -- for routing msgs only */
77 { RTF_XRESOLVE, 'X', "xresolve" },
78 { RTF_STATIC, 'S', "static" },
79 { RTF_PROTO1, '1', "proto1" },
80 { RTF_PROTO2, '2', "proto2" },
81 { RTF_PROTO3, '3', "proto3" },
82 { RTF_BLACKHOLE,'B', "blackhole" },
83 { RTF_BROADCAST,'b', "broadcast" },
85 { RTF_LLINFO, 'L', "llinfo" },
90 #ifdef WITHOUT_NETLINK
91 static struct ifmap_entry *ifmap;
92 static size_t ifmap_size;
94 static struct timespec uptime;
96 static const char *netname4(in_addr_t, in_addr_t);
98 static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
100 #ifdef WITHOUT_NETLINK
101 static void p_rtable_sysctl(int, int);
102 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
104 static void domask(char *, size_t, u_long);
106 const uint32_t rt_default_weight = RT_DEFAULT_WEIGHT;
109 * Print routing tables.
112 routepr(int fibnum, int af)
120 intsize = sizeof(int);
122 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
124 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
126 if (fibnum < 0 || fibnum > numfibs - 1)
127 errx(EX_USAGE, "%d: invalid fib", fibnum);
129 * Since kernel & userland use different timebase
130 * (time_uptime vs time_second) and we are reading kernel memory
131 * directly we should do rt_expire --> expire_time conversion.
133 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
134 err(EX_OSERR, "clock_gettime() failed");
136 xo_open_container("route-information");
137 xo_emit("{T:Routing tables}");
139 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
141 #ifdef WITHOUT_NETLINK
142 p_rtable_sysctl(fibnum, af);
144 p_rtable_netlink(fibnum, af);
146 xo_close_container("route-information");
151 * Print address family header before a section of the routing table.
164 afname = "Internet6";
181 xo_emit("\n{k:address-family/%s}:\n", afname);
183 xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
186 /* column widths; each followed by one space */
188 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
189 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
190 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
192 #define WID_DST_DEFAULT(af) \
193 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
194 #define WID_GW_DEFAULT(af) \
195 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
196 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
202 * Print header for routing table columns.
205 pr_rthdr(int af1 __unused)
209 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
210 "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
211 wid.dst, wid.dst, "Destination",
212 wid.gw, wid.gw, "Gateway",
213 wid.flags, wid.flags, "Flags",
214 wid.mtu, wid.mtu, "Nhop#",
215 wid.mtu, wid.mtu, "Mtu",
216 wid.iface, wid.iface, "Netif",
217 wid.expire, "Expire");
219 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
221 wid.dst, wid.dst, "Destination",
222 wid.gw, wid.gw, "Gateway",
223 wid.flags, wid.flags, "Flags",
224 wid.iface, wid.iface, "Netif",
225 wid.expire, "Expire");
232 wid.dst = WID_DST_DEFAULT(fam);
233 wid.gw = WID_GW_DEFAULT(fam);
237 wid.iface = WID_IF_DEFAULT(fam);
241 #ifdef WITHOUT_NETLINK
243 p_rtable_sysctl(int fibnum, int af)
247 char *buf, *next, *lim;
248 struct rt_msghdr *rtm;
251 int need_table_close = false;
253 ifmap = prepare_ifmap(&ifmap_size);
259 mib[4] = NET_RT_DUMP;
262 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
263 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
265 if ((buf = malloc(needed)) == NULL)
266 errx(2, "malloc(%lu)", (unsigned long)needed);
267 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
268 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
270 xo_open_container("route-table");
271 xo_open_list("rt-family");
272 for (next = buf; next < lim; next += rtm->rtm_msglen) {
273 rtm = (struct rt_msghdr *)next;
274 if (rtm->rtm_version != RTM_VERSION)
277 * Peek inside header to determine AF
279 sa = (struct sockaddr *)(rtm + 1);
280 /* Only print family first time. */
281 if (fam != sa->sa_family) {
282 if (need_table_close) {
283 xo_close_list("rt-entry");
284 xo_close_instance("rt-family");
286 need_table_close = true;
289 xo_open_instance("rt-family");
291 xo_open_list("rt-entry");
295 p_rtentry_sysctl("rt-entry", rtm);
297 if (need_table_close) {
298 xo_close_list("rt-entry");
299 xo_close_instance("rt-family");
301 xo_close_list("rt-family");
302 xo_close_container("route-table");
307 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
309 struct sockaddr *sa, *addr[RTAX_MAX];
311 char prettyname[128];
314 xo_open_instance(name);
315 sa = (struct sockaddr *)(rtm + 1);
316 for (i = 0; i < RTAX_MAX; i++) {
317 if (rtm->rtm_addrs & (1 << i)) {
319 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
323 protrusion = p_sockaddr("destination", addr[RTAX_DST],
325 rtm->rtm_flags, wid.dst);
326 protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST,
327 wid.gw - protrusion);
328 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
329 wid.flags - protrusion);
330 p_flags(rtm->rtm_flags, buffer);
331 /* Output path weight as non-visual property */
332 xo_emit("{e:weight/%u}", rtm->rtm_rmx.rmx_weight);
335 xo_emit("{t:nhop/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_nhidx);
337 if (rtm->rtm_rmx.rmx_mtu != 0)
338 xo_emit("{t:mtu/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_mtu);
340 xo_emit("{P:/%*s} ", wid.mtu, "");
343 memset(prettyname, 0, sizeof(prettyname));
344 if (rtm->rtm_index < ifmap_size) {
345 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname,
347 if (*prettyname == '\0')
348 strlcpy(prettyname, "---", sizeof(prettyname));
352 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
354 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
356 if (rtm->rtm_rmx.rmx_expire) {
359 if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0)
360 xo_emit(" {:expire-time/%*d}", wid.expire,
365 xo_close_instance(name);
370 p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask,
371 int flags, int width)
377 cp = fmt_sockaddr(sa, mask, flags);
380 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
384 if (Wflag != 0 || numeric_addr) {
385 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
388 protrusion = strlen(cp) - width;
392 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
394 xo_emit(buf, width, cp);
402 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
404 static char buf[128];
410 switch(sa->sa_family) {
414 * The sa6->sin6_scope_id must be filled here because
415 * this sockaddr is extracted from kmem(4) directly
416 * and has KAME-specific embedded scope id in
417 * sa6->sin6_addr.s6_addr[2].
419 in6_fillscopeid(satosin6(sa));
423 if (flags & RTF_HOST)
424 cp = routename(sa, numeric_addr);
426 cp = netname(sa, mask);
428 cp = netname(sa, NULL);
432 strlcpy(buf, ((struct sockaddr_ng *)sa)->sg_data,
440 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
442 /* Interface route. */
447 cp = routename(sa, 1);
452 u_char *s = (u_char *)sa->sa_data, *slim;
456 slim = sa->sa_len + (u_char *) sa;
457 cqlim = cq + sizeof(buf) - sizeof(" ffff");
458 snprintf(cq, sizeof(buf), "(%d)", sa->sa_family);
460 while (s < slim && cq < cqlim) {
461 snprintf(cq, sizeof(" ff"), " %02x", *s++);
464 snprintf(cq, sizeof("ff"), "%02x", *s++);
476 p_flags(int f, const char *format)
479 print_flags_generic(f, rt_bits, format, "flags_pretty");
484 routename(struct sockaddr *sa, int flags)
486 static char line[NI_MAXHOST];
489 f = (flags) ? NI_NUMERICHOST : 0;
490 error = getnameinfo(sa, sa->sa_len, line, sizeof(line),
494 switch (sa->sa_family) {
497 src = &satosin(sa)->sin_addr;
502 src = &satosin6(sa)->sin6_addr;
508 inet_ntop(sa->sa_family, src, line, sizeof(line) - 1);
511 trimdomain(line, strlen(line));
516 #define NSHIFT(m) ( \
517 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
518 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
519 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
523 domask(char *dst, size_t buflen, u_long mask)
532 for (b = 0; b < 32; b++)
533 if (mask & (1 << b)) {
537 for (bb = b+1; bb < 32; bb++)
538 if (!(mask & (1 << bb))) {
539 i = -1; /* noncontig */
545 snprintf(dst, buflen, "&0x%lx", mask);
547 snprintf(dst, buflen, "/%d", 32-i);
551 * Return the name of the network whose address is given.
554 netname(struct sockaddr *sa, struct sockaddr *mask)
556 switch (sa->sa_family) {
559 return (netname4(satosin(sa)->sin_addr.s_addr,
560 satosin(mask)->sin_addr.s_addr));
562 return (netname4(satosin(sa)->sin_addr.s_addr,
567 return (netname6(satosin6(sa), satosin6(mask)));
575 netname4(in_addr_t in, in_addr_t mask)
578 static char line[MAXHOSTNAMELEN + sizeof("&0xffffffff")];
579 char nline[INET_ADDRSTRLEN];
580 struct netent *np = 0;
583 if (in == INADDR_ANY && mask == 0) {
584 strlcpy(line, "default", sizeof(line));
588 /* It is ok to supply host address. */
592 if (!numeric_addr && i) {
593 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET);
596 trimdomain(cp, strlen(cp));
600 strlcpy(line, cp, sizeof(line));
602 inet_ntop(AF_INET, &in, nline, sizeof(nline));
603 strlcpy(line, nline, sizeof(line));
604 domask(line + strlen(line), sizeof(line) - strlen(line), ntohl(mask));
614 in6_fillscopeid(struct sockaddr_in6 *sa6)
616 #if defined(__KAME__)
618 * XXX: This is a special workaround for KAME kernels.
619 * sin6_scope_id field of SA should be set in the future.
621 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) ||
622 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) ||
623 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
624 if (sa6->sin6_scope_id == 0)
626 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]);
627 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0;
632 /* Mask to length table. To check an invalid value, (length + 1) is used. */
633 static const u_char masktolen[256] = {
646 netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
648 static char line[NI_MAXHOST + sizeof("/xxx") - 1];
649 struct sockaddr_in6 addr;
650 char nline[NI_MAXHOST];
651 char maskbuf[sizeof("/xxx")];
655 bool illegal = false;
658 p = (u_char *)&mask->sin6_addr;
659 for (masklen = 0, lim = p + 16; p < lim; p++) {
660 if (masktolen[*p] > 0) {
661 /* -1 is required. */
662 masklen += (masktolen[*p] - 1);
667 xo_error("illegal prefixlen\n");
669 memcpy(&addr, sa6, sizeof(addr));
670 for (i = 0; i < 16; ++i)
671 addr.sin6_addr.s6_addr[i] &=
672 mask->sin6_addr.s6_addr[i];
678 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
681 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline),
682 NULL, 0, NI_NUMERICHOST);
684 strlcpy(line, nline, sizeof(line));
686 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line,
687 sizeof(line), NULL, 0, 0);
688 if (numeric_addr || strcmp(line, nline) == 0) {
689 snprintf(maskbuf, sizeof(maskbuf), "/%d", masklen);
690 strlcat(line, maskbuf, sizeof(line));
698 * Print routing statistics
703 struct rtstat rtstat;
706 if ((rtsaddr = nl[N_RTSTAT].n_value) == 0) {
707 xo_emit("{W:rtstat: symbol not in namelist}\n");
710 kread_counters(rtsaddr, (char *)&rtstat, sizeof (rtstat));
711 xo_emit("{T:routing}:\n");
713 #define p(f, m) if (rtstat.f || sflag <= 1) \
714 xo_emit(m, rtstat.f, plural(rtstat.f))
716 p(rts_badredirect, "\t{:bad-redirects/%ju} "
717 "{N:/bad routing redirect%s}\n");
718 p(rts_dynamic, "\t{:dynamically-created/%ju} "
719 "{N:/dynamically created route%s}\n");
720 p(rts_newgateway, "\t{:new-gateways/%ju} "
721 "{N:/new gateway%s due to redirects}\n");
722 p(rts_unreach, "\t{:unreachable-destination/%ju} "
723 "{N:/destination%s found unreachable}\n");
724 p(rts_wildcard, "\t{:wildcard-uses/%ju} "
725 "{N:/use%s of a wildcard route}\n");