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 __FBSDID("$FreeBSD$");
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>
48 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netgraph/ng_socket.h>
57 #include <arpa/inet.h>
76 * Definitions for showing gateway flags.
78 struct bits rt_bits[] = {
79 { RTF_UP, 'U', "up" },
80 { RTF_GATEWAY, 'G', "gateway" },
81 { RTF_HOST, 'H', "host" },
82 { RTF_REJECT, 'R', "reject" },
83 { RTF_DYNAMIC, 'D', "dynamic" },
84 { RTF_MODIFIED, 'M', "modified" },
85 { RTF_DONE, 'd', "done" }, /* Completed -- for routing msgs only */
86 { RTF_XRESOLVE, 'X', "xresolve" },
87 { RTF_STATIC, 'S', "static" },
88 { RTF_PROTO1, '1', "proto1" },
89 { RTF_PROTO2, '2', "proto2" },
90 { RTF_PROTO3, '3', "proto3" },
91 { RTF_BLACKHOLE,'B', "blackhole" },
92 { RTF_BROADCAST,'b', "broadcast" },
94 { RTF_LLINFO, 'L', "llinfo" },
99 #ifdef WITHOUT_NETLINK
100 static struct ifmap_entry *ifmap;
101 static size_t ifmap_size;
103 static struct timespec uptime;
105 static const char *netname4(in_addr_t, in_addr_t);
107 static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
109 #ifdef WITHOUT_NETLINK
110 static void p_rtable_sysctl(int, int);
111 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
113 static void domask(char *, size_t, u_long);
115 const uint32_t rt_default_weight = RT_DEFAULT_WEIGHT;
118 * Print routing tables.
121 routepr(int fibnum, int af)
129 intsize = sizeof(int);
131 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
133 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
135 if (fibnum < 0 || fibnum > numfibs - 1)
136 errx(EX_USAGE, "%d: invalid fib", fibnum);
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.
142 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
143 err(EX_OSERR, "clock_gettime() failed");
145 xo_open_container("route-information");
146 xo_emit("{T:Routing tables}");
148 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
150 #ifdef WITHOUT_NETLINK
151 p_rtable_sysctl(fibnum, af);
153 p_rtable_netlink(fibnum, af);
155 xo_close_container("route-information");
160 * Print address family header before a section of the routing table.
173 afname = "Internet6";
190 xo_emit("\n{k:address-family/%s}:\n", afname);
192 xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
195 /* column widths; each followed by one space */
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 */
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))
211 * Print header for routing table columns.
214 pr_rthdr(int af1 __unused)
218 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
219 "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
220 wid.dst, wid.dst, "Destination",
221 wid.gw, wid.gw, "Gateway",
222 wid.flags, wid.flags, "Flags",
223 wid.mtu, wid.mtu, "Nhop#",
224 wid.mtu, wid.mtu, "Mtu",
225 wid.iface, wid.iface, "Netif",
226 wid.expire, "Expire");
228 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
230 wid.dst, wid.dst, "Destination",
231 wid.gw, wid.gw, "Gateway",
232 wid.flags, wid.flags, "Flags",
233 wid.iface, wid.iface, "Netif",
234 wid.expire, "Expire");
241 wid.dst = WID_DST_DEFAULT(fam);
242 wid.gw = WID_GW_DEFAULT(fam);
246 wid.iface = WID_IF_DEFAULT(fam);
250 #ifdef WITHOUT_NETLINK
252 p_rtable_sysctl(int fibnum, int af)
256 char *buf, *next, *lim;
257 struct rt_msghdr *rtm;
260 int need_table_close = false;
262 ifmap = prepare_ifmap(&ifmap_size);
268 mib[4] = NET_RT_DUMP;
271 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
272 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
274 if ((buf = malloc(needed)) == NULL)
275 errx(2, "malloc(%lu)", (unsigned long)needed);
276 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
277 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
279 xo_open_container("route-table");
280 xo_open_list("rt-family");
281 for (next = buf; next < lim; next += rtm->rtm_msglen) {
282 rtm = (struct rt_msghdr *)next;
283 if (rtm->rtm_version != RTM_VERSION)
286 * Peek inside header to determine AF
288 sa = (struct sockaddr *)(rtm + 1);
289 /* Only print family first time. */
290 if (fam != sa->sa_family) {
291 if (need_table_close) {
292 xo_close_list("rt-entry");
293 xo_close_instance("rt-family");
295 need_table_close = true;
298 xo_open_instance("rt-family");
300 xo_open_list("rt-entry");
304 p_rtentry_sysctl("rt-entry", rtm);
306 if (need_table_close) {
307 xo_close_list("rt-entry");
308 xo_close_instance("rt-family");
310 xo_close_list("rt-family");
311 xo_close_container("route-table");
316 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
318 struct sockaddr *sa, *addr[RTAX_MAX];
320 char prettyname[128];
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)) {
328 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
332 protrusion = p_sockaddr("destination", addr[RTAX_DST],
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 /* Output path weight as non-visual property */
341 xo_emit("{e:weight/%u}", rtm->rtm_rmx.rmx_weight);
344 xo_emit("{t:nhop/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_nhidx);
346 if (rtm->rtm_rmx.rmx_mtu != 0)
347 xo_emit("{t:mtu/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_mtu);
349 xo_emit("{P:/%*s} ", wid.mtu, "");
352 memset(prettyname, 0, sizeof(prettyname));
353 if (rtm->rtm_index < ifmap_size) {
354 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname,
356 if (*prettyname == '\0')
357 strlcpy(prettyname, "---", sizeof(prettyname));
361 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
363 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
365 if (rtm->rtm_rmx.rmx_expire) {
368 if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0)
369 xo_emit(" {:expire-time/%*d}", wid.expire,
374 xo_close_instance(name);
379 p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask,
380 int flags, int width)
386 cp = fmt_sockaddr(sa, mask, flags);
389 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
393 if (Wflag != 0 || numeric_addr) {
394 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
397 protrusion = strlen(cp) - width;
401 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
403 xo_emit(buf, width, cp);
411 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
413 static char buf[128];
419 switch(sa->sa_family) {
423 * The sa6->sin6_scope_id must be filled here because
424 * this sockaddr is extracted from kmem(4) directly
425 * and has KAME-specific embedded scope id in
426 * sa6->sin6_addr.s6_addr[2].
428 in6_fillscopeid(satosin6(sa));
432 if (flags & RTF_HOST)
433 cp = routename(sa, numeric_addr);
435 cp = netname(sa, mask);
437 cp = netname(sa, NULL);
441 strlcpy(buf, ((struct sockaddr_ng *)sa)->sg_data,
449 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
451 /* Interface route. */
456 cp = routename(sa, 1);
461 u_char *s = (u_char *)sa->sa_data, *slim;
465 slim = sa->sa_len + (u_char *) sa;
466 cqlim = cq + sizeof(buf) - sizeof(" ffff");
467 snprintf(cq, sizeof(buf), "(%d)", sa->sa_family);
469 while (s < slim && cq < cqlim) {
470 snprintf(cq, sizeof(" ff"), " %02x", *s++);
473 snprintf(cq, sizeof("ff"), "%02x", *s++);
485 p_flags(int f, const char *format)
488 print_flags_generic(f, rt_bits, format, "flags_pretty");
493 routename(struct sockaddr *sa, int flags)
495 static char line[NI_MAXHOST];
498 f = (flags) ? NI_NUMERICHOST : 0;
499 error = getnameinfo(sa, sa->sa_len, line, sizeof(line),
503 switch (sa->sa_family) {
506 src = &satosin(sa)->sin_addr;
511 src = &satosin6(sa)->sin6_addr;
517 inet_ntop(sa->sa_family, src, line, sizeof(line) - 1);
520 trimdomain(line, strlen(line));
525 #define NSHIFT(m) ( \
526 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
527 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
528 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
532 domask(char *dst, size_t buflen, u_long mask)
541 for (b = 0; b < 32; b++)
542 if (mask & (1 << b)) {
546 for (bb = b+1; bb < 32; bb++)
547 if (!(mask & (1 << bb))) {
548 i = -1; /* noncontig */
554 snprintf(dst, buflen, "&0x%lx", mask);
556 snprintf(dst, buflen, "/%d", 32-i);
560 * Return the name of the network whose address is given.
563 netname(struct sockaddr *sa, struct sockaddr *mask)
565 switch (sa->sa_family) {
568 return (netname4(satosin(sa)->sin_addr.s_addr,
569 satosin(mask)->sin_addr.s_addr));
571 return (netname4(satosin(sa)->sin_addr.s_addr,
576 return (netname6(satosin6(sa), satosin6(mask)));
584 netname4(in_addr_t in, in_addr_t mask)
587 static char line[MAXHOSTNAMELEN + sizeof("&0xffffffff")];
588 char nline[INET_ADDRSTRLEN];
589 struct netent *np = 0;
592 if (in == INADDR_ANY && mask == 0) {
593 strlcpy(line, "default", sizeof(line));
597 /* It is ok to supply host address. */
601 if (!numeric_addr && i) {
602 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET);
605 trimdomain(cp, strlen(cp));
609 strlcpy(line, cp, sizeof(line));
611 inet_ntop(AF_INET, &in, nline, sizeof(nline));
612 strlcpy(line, nline, sizeof(line));
613 domask(line + strlen(line), sizeof(line) - strlen(line), ntohl(mask));
623 in6_fillscopeid(struct sockaddr_in6 *sa6)
625 #if defined(__KAME__)
627 * XXX: This is a special workaround for KAME kernels.
628 * sin6_scope_id field of SA should be set in the future.
630 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) ||
631 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) ||
632 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
633 if (sa6->sin6_scope_id == 0)
635 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]);
636 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0;
641 /* Mask to length table. To check an invalid value, (length + 1) is used. */
642 static const u_char masktolen[256] = {
655 netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
657 static char line[NI_MAXHOST + sizeof("/xxx") - 1];
658 struct sockaddr_in6 addr;
659 char nline[NI_MAXHOST];
660 char maskbuf[sizeof("/xxx")];
664 bool illegal = false;
667 p = (u_char *)&mask->sin6_addr;
668 for (masklen = 0, lim = p + 16; p < lim; p++) {
669 if (masktolen[*p] > 0) {
670 /* -1 is required. */
671 masklen += (masktolen[*p] - 1);
676 xo_error("illegal prefixlen\n");
678 memcpy(&addr, sa6, sizeof(addr));
679 for (i = 0; i < 16; ++i)
680 addr.sin6_addr.s6_addr[i] &=
681 mask->sin6_addr.s6_addr[i];
687 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
690 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline),
691 NULL, 0, NI_NUMERICHOST);
693 strlcpy(line, nline, sizeof(line));
695 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line,
696 sizeof(line), NULL, 0, 0);
697 if (numeric_addr || strcmp(line, nline) == 0) {
698 snprintf(maskbuf, sizeof(maskbuf), "/%d", masklen);
699 strlcat(line, maskbuf, sizeof(line));
707 * Print routing statistics
712 struct rtstat rtstat;
715 if ((rtsaddr = nl[N_RTSTAT].n_value) == 0) {
716 xo_emit("{W:rtstat: symbol not in namelist}\n");
719 kread_counters(rtsaddr, (char *)&rtstat, sizeof (rtstat));
720 xo_emit("{T:routing}:\n");
722 #define p(f, m) if (rtstat.f || sflag <= 1) \
723 xo_emit(m, rtstat.f, plural(rtstat.f))
725 p(rts_badredirect, "\t{:bad-redirects/%ju} "
726 "{N:/bad routing redirect%s}\n");
727 p(rts_dynamic, "\t{:dynamically-created/%ju} "
728 "{N:/dynamically created route%s}\n");
729 p(rts_newgateway, "\t{:new-gateways/%ju} "
730 "{N:/new gateway%s due to redirects}\n");
731 p(rts_unreach, "\t{:unreachable-destination/%ju} "
732 "{N:/destination%s found unreachable}\n");
733 p(rts_wildcard, "\t{:wildcard-uses/%ju} "
734 "{N:/use%s of a wildcard route}\n");