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
34 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
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>
46 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netgraph/ng_socket.h>
55 #include <arpa/inet.h>
74 * Definitions for showing gateway flags.
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" },
92 { RTF_LLINFO, 'L', "llinfo" },
97 #ifdef WITHOUT_NETLINK
98 static struct ifmap_entry *ifmap;
99 static size_t ifmap_size;
101 static struct timespec uptime;
103 static const char *netname4(in_addr_t, in_addr_t);
105 static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
107 #ifdef WITHOUT_NETLINK
108 static void p_rtable_sysctl(int, int);
109 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
111 static void domask(char *, size_t, u_long);
113 const uint32_t rt_default_weight = RT_DEFAULT_WEIGHT;
116 * Print routing tables.
119 routepr(int fibnum, int af)
127 intsize = sizeof(int);
129 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
131 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
133 if (fibnum < 0 || fibnum > numfibs - 1)
134 errx(EX_USAGE, "%d: invalid fib", fibnum);
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.
140 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
141 err(EX_OSERR, "clock_gettime() failed");
143 xo_open_container("route-information");
144 xo_emit("{T:Routing tables}");
146 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
148 #ifdef WITHOUT_NETLINK
149 p_rtable_sysctl(fibnum, af);
151 p_rtable_netlink(fibnum, af);
153 xo_close_container("route-information");
158 * Print address family header before a section of the routing table.
171 afname = "Internet6";
188 xo_emit("\n{k:address-family/%s}:\n", afname);
190 xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
193 /* column widths; each followed by one space */
195 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
196 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
197 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
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 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
209 * Print header for routing table columns.
212 pr_rthdr(int af1 __unused)
216 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
217 "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
218 wid.dst, wid.dst, "Destination",
219 wid.gw, wid.gw, "Gateway",
220 wid.flags, wid.flags, "Flags",
221 wid.mtu, wid.mtu, "Nhop#",
222 wid.mtu, wid.mtu, "Mtu",
223 wid.iface, wid.iface, "Netif",
224 wid.expire, "Expire");
226 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
228 wid.dst, wid.dst, "Destination",
229 wid.gw, wid.gw, "Gateway",
230 wid.flags, wid.flags, "Flags",
231 wid.iface, wid.iface, "Netif",
232 wid.expire, "Expire");
239 wid.dst = WID_DST_DEFAULT(fam);
240 wid.gw = WID_GW_DEFAULT(fam);
244 wid.iface = WID_IF_DEFAULT(fam);
248 #ifdef WITHOUT_NETLINK
250 p_rtable_sysctl(int fibnum, int af)
254 char *buf, *next, *lim;
255 struct rt_msghdr *rtm;
258 int need_table_close = false;
260 ifmap = prepare_ifmap(&ifmap_size);
266 mib[4] = NET_RT_DUMP;
269 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
270 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
272 if ((buf = malloc(needed)) == NULL)
273 errx(2, "malloc(%lu)", (unsigned long)needed);
274 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
275 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
277 xo_open_container("route-table");
278 xo_open_list("rt-family");
279 for (next = buf; next < lim; next += rtm->rtm_msglen) {
280 rtm = (struct rt_msghdr *)next;
281 if (rtm->rtm_version != RTM_VERSION)
284 * Peek inside header to determine AF
286 sa = (struct sockaddr *)(rtm + 1);
287 /* Only print family first time. */
288 if (fam != sa->sa_family) {
289 if (need_table_close) {
290 xo_close_list("rt-entry");
291 xo_close_instance("rt-family");
293 need_table_close = true;
296 xo_open_instance("rt-family");
298 xo_open_list("rt-entry");
302 p_rtentry_sysctl("rt-entry", rtm);
304 if (need_table_close) {
305 xo_close_list("rt-entry");
306 xo_close_instance("rt-family");
308 xo_close_list("rt-family");
309 xo_close_container("route-table");
314 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
316 struct sockaddr *sa, *addr[RTAX_MAX];
318 char prettyname[128];
321 xo_open_instance(name);
322 sa = (struct sockaddr *)(rtm + 1);
323 for (i = 0; i < RTAX_MAX; i++) {
324 if (rtm->rtm_addrs & (1 << i)) {
326 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
330 protrusion = p_sockaddr("destination", addr[RTAX_DST],
332 rtm->rtm_flags, wid.dst);
333 protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST,
334 wid.gw - protrusion);
335 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
336 wid.flags - protrusion);
337 p_flags(rtm->rtm_flags, buffer);
338 /* Output path weight as non-visual property */
339 xo_emit("{e:weight/%u}", rtm->rtm_rmx.rmx_weight);
342 xo_emit("{t:nhop/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_nhidx);
344 if (rtm->rtm_rmx.rmx_mtu != 0)
345 xo_emit("{t:mtu/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_mtu);
347 xo_emit("{P:/%*s} ", wid.mtu, "");
350 memset(prettyname, 0, sizeof(prettyname));
351 if (rtm->rtm_index < ifmap_size) {
352 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname,
354 if (*prettyname == '\0')
355 strlcpy(prettyname, "---", sizeof(prettyname));
359 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
361 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
363 if (rtm->rtm_rmx.rmx_expire) {
366 if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0)
367 xo_emit(" {:expire-time/%*d}", wid.expire,
372 xo_close_instance(name);
377 p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask,
378 int flags, int width)
384 cp = fmt_sockaddr(sa, mask, flags);
387 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
391 if (Wflag != 0 || numeric_addr) {
392 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
395 protrusion = strlen(cp) - width;
399 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
401 xo_emit(buf, width, cp);
409 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
411 static char buf[128];
417 switch(sa->sa_family) {
421 * The sa6->sin6_scope_id must be filled here because
422 * this sockaddr is extracted from kmem(4) directly
423 * and has KAME-specific embedded scope id in
424 * sa6->sin6_addr.s6_addr[2].
426 in6_fillscopeid(satosin6(sa));
430 if (flags & RTF_HOST)
431 cp = routename(sa, numeric_addr);
433 cp = netname(sa, mask);
435 cp = netname(sa, NULL);
439 strlcpy(buf, ((struct sockaddr_ng *)sa)->sg_data,
447 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
449 /* Interface route. */
454 cp = routename(sa, 1);
459 u_char *s = (u_char *)sa->sa_data, *slim;
463 slim = sa->sa_len + (u_char *) sa;
464 cqlim = cq + sizeof(buf) - sizeof(" ffff");
465 snprintf(cq, sizeof(buf), "(%d)", sa->sa_family);
467 while (s < slim && cq < cqlim) {
468 snprintf(cq, sizeof(" ff"), " %02x", *s++);
471 snprintf(cq, sizeof("ff"), "%02x", *s++);
483 p_flags(int f, const char *format)
486 print_flags_generic(f, rt_bits, format, "flags_pretty");
491 routename(struct sockaddr *sa, int flags)
493 static char line[NI_MAXHOST];
496 f = (flags) ? NI_NUMERICHOST : 0;
497 error = getnameinfo(sa, sa->sa_len, line, sizeof(line),
501 switch (sa->sa_family) {
504 src = &satosin(sa)->sin_addr;
509 src = &satosin6(sa)->sin6_addr;
515 inet_ntop(sa->sa_family, src, line, sizeof(line) - 1);
518 trimdomain(line, strlen(line));
523 #define NSHIFT(m) ( \
524 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
525 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
526 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
530 domask(char *dst, size_t buflen, u_long mask)
539 for (b = 0; b < 32; b++)
540 if (mask & (1 << b)) {
544 for (bb = b+1; bb < 32; bb++)
545 if (!(mask & (1 << bb))) {
546 i = -1; /* noncontig */
552 snprintf(dst, buflen, "&0x%lx", mask);
554 snprintf(dst, buflen, "/%d", 32-i);
558 * Return the name of the network whose address is given.
561 netname(struct sockaddr *sa, struct sockaddr *mask)
563 switch (sa->sa_family) {
566 return (netname4(satosin(sa)->sin_addr.s_addr,
567 satosin(mask)->sin_addr.s_addr));
569 return (netname4(satosin(sa)->sin_addr.s_addr,
574 return (netname6(satosin6(sa), satosin6(mask)));
582 netname4(in_addr_t in, in_addr_t mask)
585 static char line[MAXHOSTNAMELEN + sizeof("&0xffffffff")];
586 char nline[INET_ADDRSTRLEN];
587 struct netent *np = 0;
590 if (in == INADDR_ANY && mask == 0) {
591 strlcpy(line, "default", sizeof(line));
595 /* It is ok to supply host address. */
599 if (!numeric_addr && i) {
600 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET);
603 trimdomain(cp, strlen(cp));
607 strlcpy(line, cp, sizeof(line));
609 inet_ntop(AF_INET, &in, nline, sizeof(nline));
610 strlcpy(line, nline, sizeof(line));
611 domask(line + strlen(line), sizeof(line) - strlen(line), ntohl(mask));
621 in6_fillscopeid(struct sockaddr_in6 *sa6)
623 #if defined(__KAME__)
625 * XXX: This is a special workaround for KAME kernels.
626 * sin6_scope_id field of SA should be set in the future.
628 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) ||
629 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) ||
630 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
631 if (sa6->sin6_scope_id == 0)
633 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]);
634 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0;
639 /* Mask to length table. To check an invalid value, (length + 1) is used. */
640 static const u_char masktolen[256] = {
653 netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
655 static char line[NI_MAXHOST + sizeof("/xxx") - 1];
656 struct sockaddr_in6 addr;
657 char nline[NI_MAXHOST];
658 char maskbuf[sizeof("/xxx")];
662 bool illegal = false;
665 p = (u_char *)&mask->sin6_addr;
666 for (masklen = 0, lim = p + 16; p < lim; p++) {
667 if (masktolen[*p] > 0) {
668 /* -1 is required. */
669 masklen += (masktolen[*p] - 1);
674 xo_error("illegal prefixlen\n");
676 memcpy(&addr, sa6, sizeof(addr));
677 for (i = 0; i < 16; ++i)
678 addr.sin6_addr.s6_addr[i] &=
679 mask->sin6_addr.s6_addr[i];
685 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
688 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline),
689 NULL, 0, NI_NUMERICHOST);
691 strlcpy(line, nline, sizeof(line));
693 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line,
694 sizeof(line), NULL, 0, 0);
695 if (numeric_addr || strcmp(line, nline) == 0) {
696 snprintf(maskbuf, sizeof(maskbuf), "/%d", masklen);
697 strlcat(line, maskbuf, sizeof(line));
705 * Print routing statistics
710 struct rtstat rtstat;
713 if ((rtsaddr = nl[N_RTSTAT].n_value) == 0) {
714 xo_emit("{W:rtstat: symbol not in namelist}\n");
717 kread_counters(rtsaddr, (char *)&rtstat, sizeof (rtstat));
718 xo_emit("{T:routing}:\n");
720 #define p(f, m) if (rtstat.f || sflag <= 1) \
721 xo_emit(m, rtstat.f, plural(rtstat.f))
723 p(rts_badredirect, "\t{:bad-redirects/%ju} "
724 "{N:/bad routing redirect%s}\n");
725 p(rts_dynamic, "\t{:dynamically-created/%ju} "
726 "{N:/dynamically created route%s}\n");
727 p(rts_newgateway, "\t{:new-gateways/%ju} "
728 "{N:/new gateway%s due to redirects}\n");
729 p(rts_unreach, "\t{:unreachable-destination/%ju} "
730 "{N:/destination%s found unreachable}\n");
731 p(rts_wildcard, "\t{:wildcard-uses/%ju} "
732 "{N:/use%s of a wildcard route}\n");