2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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>
72 * Definitions for showing gateway flags.
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" },
100 * kvm(3) bindings for every needed symbol
102 static struct nlist rl[] = {
104 { .n_name = "_rtstat" },
106 { .n_name = "_rttrash" },
111 long dummy; /* Helps align structure. */
112 struct sockaddr u_sa;
117 char ifname[IFNAMSIZ];
119 static struct ifmap_entry *ifmap;
120 static int ifmap_size;
121 struct timespec uptime;
123 static void p_rtable_sysctl(int, int);
124 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
125 static void p_sockaddr(const char *name, struct sockaddr *, struct sockaddr *,
127 static const char *fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask,
129 static void p_flags(int, const char *);
130 static const char *fmt_flags(int f);
131 static void domask(char *, in_addr_t, u_long);
134 * Print routing tables.
137 routepr(int fibnum, int af)
145 intsize = sizeof(int);
147 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
149 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
151 if (fibnum < 0 || fibnum > numfibs - 1)
152 errx(EX_USAGE, "%d: invalid fib", fibnum);
154 * Since kernel & userland use different timebase
155 * (time_uptime vs time_second) and we are reading kernel memory
156 * directly we should do rt_expire --> expire_time conversion.
158 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0)
159 err(EX_OSERR, "clock_gettime() failed");
161 xo_open_container("route-information");
162 xo_emit("{T:Routing tables}");
164 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
166 p_rtable_sysctl(fibnum, af);
167 xo_close_container("route-information");
172 * Print address family header before a section of the routing table.
185 afname = "Internet6";
202 xo_emit("\n{k:address-family/%s}:\n", afname);
204 xo_emit("\n{L:Protocol Family} {k:address-family/%d}:\n", af1);
207 /* column widths; each followed by one space */
209 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
210 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
211 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
213 #define WID_DST_DEFAULT(af) \
214 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
215 #define WID_GW_DEFAULT(af) \
216 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
217 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
222 static int wid_flags;
223 static int wid_pksent;
226 static int wid_expire;
229 * Print header for routing table columns.
236 xo_emit("{T:/%-8.8s} ","Address");
238 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
239 "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
240 wid_dst, wid_dst, "Destination",
241 wid_gw, wid_gw, "Gateway",
242 wid_flags, wid_flags, "Flags",
243 wid_pksent, wid_pksent, "Use",
244 wid_mtu, wid_mtu, "Mtu",
245 wid_if, wid_if, "Netif",
246 wid_expire, "Expire");
248 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
250 wid_dst, wid_dst, "Destination",
251 wid_gw, wid_gw, "Gateway",
252 wid_flags, wid_flags, "Flags",
253 wid_if, wid_if, "Netif",
254 wid_expire, "Expire");
259 p_rtable_sysctl(int fibnum, int af)
263 char *buf, *next, *lim;
264 struct rt_msghdr *rtm;
266 int fam = AF_UNSPEC, ifindex = 0, size;
267 int need_table_close = false;
269 struct ifaddrs *ifap, *ifa;
270 struct sockaddr_dl *sdl;
273 * Retrieve interface list at first
274 * since we need #ifindex -> if_xname match
276 if (getifaddrs(&ifap) != 0)
277 err(EX_OSERR, "getifaddrs");
279 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
281 if (ifa->ifa_addr->sa_family != AF_LINK)
284 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
285 ifindex = sdl->sdl_index;
287 if (ifindex >= ifmap_size) {
288 size = roundup(ifindex + 1, 32) *
289 sizeof(struct ifmap_entry);
290 if ((ifmap = realloc(ifmap, size)) == NULL)
291 errx(2, "realloc(%d) failed", size);
292 memset(&ifmap[ifmap_size], 0,
294 sizeof(struct ifmap_entry));
296 ifmap_size = roundup(ifindex + 1, 32);
299 if (*ifmap[ifindex].ifname != '\0')
302 strlcpy(ifmap[ifindex].ifname, ifa->ifa_name, IFNAMSIZ);
311 mib[4] = NET_RT_DUMP;
314 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
315 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af,
317 if ((buf = malloc(needed)) == NULL)
318 errx(2, "malloc(%lu)", (unsigned long)needed);
319 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
320 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum);
322 xo_open_container("route-table");
323 xo_open_list("rt-family");
324 for (next = buf; next < lim; next += rtm->rtm_msglen) {
325 rtm = (struct rt_msghdr *)next;
326 if (rtm->rtm_version != RTM_VERSION)
329 * Peek inside header to determine AF
331 sa = (struct sockaddr *)(rtm + 1);
332 /* Only print family first time. */
333 if (fam != sa->sa_family) {
334 if (need_table_close) {
335 xo_close_list("rt-entry");
336 xo_close_instance("rt-family");
338 need_table_close = true;
341 wid_dst = WID_DST_DEFAULT(fam);
342 wid_gw = WID_GW_DEFAULT(fam);
346 wid_if = WID_IF_DEFAULT(fam);
348 xo_open_instance("rt-family");
350 xo_open_list("rt-entry");
354 p_rtentry_sysctl("rt-entry", rtm);
356 if (need_table_close) {
357 xo_close_list("rt-entry");
358 xo_close_instance("rt-family");
360 xo_close_list("rt-family");
361 xo_close_container("route-table");
366 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
368 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
370 char prettyname[128];
374 xo_open_instance(name);
376 #define GETSA(_s, _f) { \
377 bzero(&(_s), sizeof(_s)); \
378 if (rtm->rtm_addrs & _f) { \
379 l = roundup(sa->sa_len, sizeof(long)); \
380 memcpy(&(_s), sa, (l > sizeof(_s)) ? sizeof(_s) : l); \
381 sa = (struct sockaddr *)((char *)sa + l); \
385 GETSA(addr, RTA_DST);
386 GETSA(gw, RTA_GATEWAY);
387 GETSA(mask, RTA_NETMASK);
389 p_sockaddr("destination", &addr.u_sa, &mask.u_sa, rtm->rtm_flags,
391 p_sockaddr("gateway", &gw.u_sa, NULL, RTF_HOST, wid_gw);
392 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
394 p_flags(rtm->rtm_flags, buffer);
396 xo_emit("{t:use/%*lu} ", wid_pksent, rtm->rtm_rmx.rmx_pksent);
398 if (rtm->rtm_rmx.rmx_mtu != 0)
399 xo_emit("{t:mtu/%*lu} ", wid_mtu, rtm->rtm_rmx.rmx_mtu);
401 xo_emit("{P:/%*s} ", wid_mtu, "");
404 memset(prettyname, 0, sizeof(prettyname));
405 if (rtm->rtm_index < ifmap_size) {
406 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname,
408 if (*prettyname == '\0')
409 strlcpy(prettyname, "---", sizeof(prettyname));
412 xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, prettyname);
413 if (rtm->rtm_rmx.rmx_expire) {
416 if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0)
417 xo_emit(" {:expire-time/%*d}", wid_expire,
422 xo_close_instance(name);
426 p_sockaddr(const char *name, struct sockaddr *sa, struct sockaddr *mask,
427 int flags, int width)
432 cp = fmt_sockaddr(sa, mask, flags);
435 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
439 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
443 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
445 xo_emit(buf, width, cp);
451 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
453 static char workbuf[128];
459 switch(sa->sa_family) {
462 struct sockaddr_in *sockin = (struct sockaddr_in *)sa;
464 if ((sockin->sin_addr.s_addr == INADDR_ANY) &&
466 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
469 else if (flags & RTF_HOST)
470 cp = routename(sockin->sin_addr.s_addr);
472 cp = netname(sockin->sin_addr.s_addr,
473 ((struct sockaddr_in *)mask)->sin_addr.s_addr);
475 cp = netname(sockin->sin_addr.s_addr, INADDR_ANY);
482 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
485 * The sa6->sin6_scope_id must be filled here because
486 * this sockaddr is extracted from kmem(4) directly
487 * and has KAME-specific embedded scope id in
488 * sa6->sin6_addr.s6_addr[2].
490 in6_fillscopeid(sa6);
492 if (flags & RTF_HOST)
493 cp = routename6(sa6);
496 &((struct sockaddr_in6 *)mask)->sin6_addr);
498 cp = netname6(sa6, NULL);
506 strlcpy(workbuf, ((struct sockaddr_ng *)sa)->sg_data,
514 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
516 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
517 sdl->sdl_slen == 0) {
518 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
521 switch (sdl->sdl_type) {
526 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
527 cp = ether_ntoa((struct ether_addr *)
528 (sdl->sdl_data + sdl->sdl_nlen));
541 u_char *s = (u_char *)sa->sa_data, *slim;
545 slim = sa->sa_len + (u_char *) sa;
546 cqlim = cq + sizeof(workbuf) - 6;
547 cq += sprintf(cq, "(%d)", sa->sa_family);
548 while (s < slim && cq < cqlim) {
549 cq += sprintf(cq, " %02x", *s++);
551 cq += sprintf(cq, "%02x", *s++);
561 p_flags(int f, const char *format)
565 xo_emit(format, fmt_flags(f));
567 xo_open_list("flags_pretty");
568 for (p = bits; p->b_mask; p++)
570 xo_emit("{le:flags_pretty/%s}", p->b_name);
571 xo_close_list("flags_pretty");
577 static char name[33];
579 struct bits *p = bits;
581 for (flags = name; p->b_mask; p++)
589 routename(in_addr_t in)
592 static char line[MAXHOSTNAMELEN];
597 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
600 trimdomain(cp, strlen(cp));
604 strlcpy(line, cp, sizeof(line));
606 #define C(x) ((x) & 0xff)
608 sprintf(line, "%u.%u.%u.%u",
609 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
614 #define NSHIFT(m) ( \
615 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
616 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
617 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
621 domask(char *dst, in_addr_t addr __unused, u_long mask)
625 if (mask == 0 || (!numeric_addr && NSHIFT(mask) != 0)) {
630 for (b = 0; b < 32; b++)
631 if (mask & (1 << b)) {
635 for (bb = b+1; bb < 32; bb++)
636 if (!(mask & (1 << bb))) {
637 i = -1; /* noncontig */
643 sprintf(dst, "&0x%lx", mask);
645 sprintf(dst, "/%d", 32-i);
649 * Return the name of the network whose address is given.
652 netname(in_addr_t in, in_addr_t mask)
655 static char line[MAXHOSTNAMELEN];
656 struct netent *np = 0;
659 /* It is ok to supply host address. */
663 if (!numeric_addr && i) {
664 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET);
667 trimdomain(cp, strlen(cp));
671 strlcpy(line, cp, sizeof(line));
673 inet_ntop(AF_INET, &in, line, sizeof(line) - 1);
675 domask(line + strlen(line), i, ntohl(mask));
683 in6_fillscopeid(struct sockaddr_in6 *sa6)
685 #if defined(__KAME__)
687 * XXX: This is a special workaround for KAME kernels.
688 * sin6_scope_id field of SA should be set in the future.
690 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) ||
691 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) ||
692 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
693 if (sa6->sin6_scope_id == 0)
695 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]);
696 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0;
702 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
704 static char line[MAXHOSTNAMELEN];
705 u_char *p = (u_char *)mask;
707 int masklen, illegal = 0, flag = 0;
710 for (masklen = 0, lim = p + 16; p < lim; p++) {
744 xo_error("illegal prefixlen\n");
749 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
753 flag |= NI_NUMERICHOST;
754 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
758 sprintf(&line[strlen(line)], "/%d", masklen);
764 routename6(struct sockaddr_in6 *sa6)
766 static char line[MAXHOSTNAMELEN];
768 /* use local variable for safety */
769 struct sockaddr_in6 sa6_local;
771 sa6_local.sin6_family = AF_INET6;
772 sa6_local.sin6_len = sizeof(sa6_local);
773 sa6_local.sin6_addr = sa6->sin6_addr;
774 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
777 flag |= NI_NUMERICHOST;
779 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
780 line, sizeof(line), NULL, 0, flag);
787 * Print routing statistics
792 struct rtstat rtstat;
793 u_long rtsaddr, rttaddr;
798 if ((rtsaddr = rl[N_RTSTAT].n_value) == 0) {
799 xo_emit("{W:rtstat: symbol not in namelist}\n");
802 if ((rttaddr = rl[N_RTTRASH].n_value) == 0) {
803 xo_emit("{W:rttrash: symbol not in namelist}\n");
806 kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
807 kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
808 xo_emit("{T:routing}:\n");
810 #define p(f, m) if (rtstat.f || sflag <= 1) \
811 xo_emit(m, rtstat.f, plural(rtstat.f))
813 p(rts_badredirect, "\t{:bad-redirects/%hu} "
814 "{N:/bad routing redirect%s}\n");
815 p(rts_dynamic, "\t{:dynamically-created/%hu} "
816 "{N:/dynamically created route%s}\n");
817 p(rts_newgateway, "\t{:new-gateways/%hu} "
818 "{N:/new gateway%s due to redirects}\n");
819 p(rts_unreach, "\t{:unreachable-destination/%hu} "
820 "{N:/destination%s found unreachable}\n");
821 p(rts_wildcard, "\t{:wildcard-uses/%hu} "
822 "{N:/use%s of a wildcard route}\n");
825 if (rttrash || sflag <= 1)
826 xo_emit("\t{:unused-but-not-freed/%u} "
827 "{N:/route%s not in table but not freed}\n",
828 rttrash, plural(rttrash));