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 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/sysctl.h>
42 #include <net/ethernet.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <net/route/nhop.h>
49 #include <netinet/in.h>
50 #include <netgraph/ng_socket.h>
52 #include <arpa/inet.h>
69 /* column widths; each followed by one space */
71 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
72 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
73 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
75 #define WID_DST_DEFAULT(af) \
76 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
77 #define WID_GW_DEFAULT(af) \
78 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
79 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
84 static int wid_pksent;
88 static int wid_nhtype;
89 static int wid_refcnt;
90 static int wid_prepend;
92 static struct bits nh_bits[] = {
93 { NHF_REJECT, 'R', "reject" },
94 { NHF_BLACKHOLE,'B', "blackhole" },
95 { NHF_REDIRECT, 'r', "redirect" },
96 { NHF_GATEWAY, 'G', "gateway" },
97 { NHF_DEFAULT, 'd', "default" },
98 { NHF_BROADCAST,'b', "broadcast" },
102 static char *nh_types[] = {
104 "v4/resolve", /* 1 */
112 char ifname[IFNAMSIZ];
116 struct nhop_entry *ptr;
119 static struct nhop_map global_nhop_map;
121 static void nhop_map_update(struct nhop_map *map, uint32_t idx,
122 char *gw, char *ifname);
123 static struct nhop_entry *nhop_get(struct nhop_map *map, uint32_t idx);
126 static struct ifmap_entry *ifmap;
127 static size_t ifmap_size;
130 print_sockaddr_buf(char *buf, size_t bufsize, const struct sockaddr *sa)
133 switch (sa->sa_family) {
135 inet_ntop(AF_INET, &((struct sockaddr_in *)sa)->sin_addr,
139 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr,
143 snprintf(buf, bufsize, "unknown:%d", sa->sa_family);
149 print_addr(const char *name, const char *addr, int width)
155 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
159 if (Wflag != 0 || numeric_addr) {
160 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
163 protrusion = strlen(addr) - width;
167 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
169 xo_emit(buf, width, addr);
178 print_nhop_header(int af1 __unused)
182 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
183 "{T:/%*.*s} {T:/%-*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
184 wid_nhidx, wid_nhidx, "Idx",
185 wid_nhtype, wid_nhtype, "Type",
186 wid_dst, wid_dst, "IFA",
187 wid_gw, wid_gw, "Gateway",
188 wid_flags, wid_flags, "Flags",
189 wid_pksent, wid_pksent, "Use",
190 wid_mtu, wid_mtu, "Mtu",
191 wid_if, wid_if, "Netif",
192 wid_if, wid_if, "Addrif",
193 wid_refcnt, wid_refcnt, "Refcnt",
194 wid_prepend, "Prepend");
196 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
198 wid_nhidx, wid_nhidx, "Idx",
199 wid_dst, wid_dst, "IFA",
200 wid_gw, wid_gw, "Gateway",
201 wid_flags, wid_flags, "Flags",
202 wid_if, wid_if, "Netif",
203 wid_prepend, "Refcnt");
208 nhop_map_update(struct nhop_map *map, uint32_t idx, char *gw, char *ifname)
210 if (idx >= map->size) {
216 new_size = map->size * 2;
218 new_size = roundup(idx + 1, 32);
220 sz = new_size * (sizeof(struct nhop_entry));
221 if ((map->ptr = realloc(map->ptr, sz)) == NULL)
222 errx(2, "realloc(%zu) failed", sz);
224 memset(&map->ptr[map->size], 0, (new_size - map->size) * sizeof(struct nhop_entry));
225 map->size = new_size;
228 strlcpy(map->ptr[idx].ifname, ifname, sizeof(map->ptr[idx].ifname));
229 strlcpy(map->ptr[idx].gw, gw, sizeof(map->ptr[idx].gw));
232 static struct nhop_entry *
233 nhop_get(struct nhop_map *map, uint32_t idx)
236 if (idx >= map->size)
238 if (*map->ptr[idx].ifname == '\0')
240 return &map->ptr[idx];
244 print_nhop_entry_sysctl(const char *name, struct rt_msghdr *rtm, struct nhop_external *nh)
247 char iface_name[128];
250 struct nhop_addrs *na;
251 struct sockaddr *sa_gw, *sa_ifa;
253 xo_open_instance(name);
255 snprintf(buffer, sizeof(buffer), "{[:-%d}{:index/%%lu}{]:} ", wid_nhidx);
256 //xo_emit("{t:index/%-lu} ", wid_nhidx, nh->nh_idx);
257 xo_emit(buffer, nh->nh_idx);
260 char *cp = nh_types[nh->nh_type];
261 xo_emit("{t:type_str/%*s} ", wid_nhtype, cp);
263 memset(iface_name, 0, sizeof(iface_name));
264 if (nh->ifindex < (uint32_t)ifmap_size) {
265 strlcpy(iface_name, ifmap[nh->ifindex].ifname,
267 if (*iface_name == '\0')
268 strlcpy(iface_name, "---", sizeof(iface_name));
271 na = (struct nhop_addrs *)((char *)nh + nh->nh_len);
272 //inet_ntop(nh->nh_family, &nh->nh_src, src_addr, sizeof(src_addr));
273 //protrusion = p_addr("ifa", src_addr, wid_dst);
274 sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off);
275 sa_ifa = (struct sockaddr *)((char *)na + na->src_sa_off);
276 protrusion = p_sockaddr("ifa", sa_ifa, NULL, RTF_HOST, wid_dst);
278 if (nh->nh_flags & NHF_GATEWAY) {
280 cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST);
281 strlcpy(gw_addr, cp, sizeof(gw_addr));
283 snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name);
284 protrusion = print_addr("gateway", gw_addr, wid_dst - protrusion);
286 nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name);
288 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
289 wid_flags - protrusion);
291 //p_nhflags(nh->nh_flags, buffer);
292 print_flags_generic(rtm->rtm_flags, rt_bits, buffer, "rt_flags_pretty");
295 xo_emit("{t:use/%*lu} ", wid_pksent, nh->nh_pksent);
296 xo_emit("{t:mtu/%*lu} ", wid_mtu, nh->nh_mtu);
298 //printf("IDX: %d IFACE: %s FAMILY: %d TYPE: %d FLAGS: %X GW \n");
301 xo_emit("{t:interface-name/%*s}", wid_if, iface_name);
303 xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, iface_name);
305 memset(iface_name, 0, sizeof(iface_name));
306 if (nh->aifindex < (uint32_t)ifmap_size && nh->ifindex != nh->aifindex) {
307 strlcpy(iface_name, ifmap[nh->aifindex].ifname,
309 if (*iface_name == '\0')
310 strlcpy(iface_name, "---", sizeof(iface_name));
313 xo_emit("{t:address-interface-name/%*s}", wid_if, iface_name);
315 xo_emit("{t:refcount/%*lu} ", wid_refcnt, nh->nh_refcount);
316 if (Wflag && nh->prepend_len) {
317 char *prepend_hex = "AABBCCDDEE";
318 xo_emit(" {:nhop-prepend/%*s}", wid_prepend, prepend_hex);
322 xo_close_instance(name);
327 struct rt_msghdr *rtm;
331 cmp_nh_idx(const void *_a, const void *_b)
333 const struct nhops_map *a, *b;
340 else if (a->idx < b->idx)
346 print_nhops_sysctl(int fibnum, int af)
350 char *buf, *next, *lim;
351 struct rt_msghdr *rtm;
352 struct nhop_external *nh;
354 struct nhops_map *nh_map;
355 size_t nh_count, nh_size;
361 mib[4] = NET_RT_NHOP;
364 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
365 err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af,
367 if ((buf = malloc(needed)) == NULL)
368 errx(2, "malloc(%lu)", (unsigned long)needed);
369 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
370 err(1, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum);
372 xo_open_container("nhop-table");
373 xo_open_list("rt-family");
376 * nexhops are received unsorted. Collect everything first, sort and then display
381 nh_map = calloc(nh_size, sizeof(struct nhops_map));
382 for (next = buf; next < lim; next += rtm->rtm_msglen) {
383 rtm = (struct rt_msghdr *)next;
384 if (rtm->rtm_version != RTM_VERSION)
387 if (nh_count >= nh_size) {
389 nh_map = realloc(nh_map, nh_size * sizeof(struct nhops_map));
392 nh = (struct nhop_external *)(rtm + 1);
393 nh_map[nh_count].idx = nh->nh_idx;
394 nh_map[nh_count].rtm = rtm;
399 qsort(nh_map, nh_count, sizeof(struct nhops_map), cmp_nh_idx);
400 nh = (struct nhop_external *)(nh_map[0].rtm + 1);
403 wid_dst = WID_GW_DEFAULT(fam);
404 wid_gw = WID_GW_DEFAULT(fam);
411 wid_if = WID_IF_DEFAULT(fam);
412 xo_open_instance("rt-family");
414 xo_open_list("nh-entry");
416 print_nhop_header(fam);
418 for (size_t i = 0; i < nh_count; i++) {
420 nh = (struct nhop_external *)(rtm + 1);
421 print_nhop_entry_sysctl("nh-entry", rtm, nh);
424 xo_close_list("nh-entry");
425 xo_close_instance("rt-family");
427 xo_close_list("rt-family");
428 xo_close_container("nhop-table");
433 p_nhflags(int f, const char *format)
436 char *pretty_name = "nh_flags_pretty";
438 xo_emit(format, fmt_flags(nh_bits, f));
440 xo_open_list(pretty_name);
441 for (p = nh_bits; p->b_mask; p++)
443 xo_emit("{le:nh_flags_pretty/%s}", p->b_name);
444 xo_close_list(pretty_name);
448 nhops_print(int fibnum, int af)
453 intsize = sizeof(int);
455 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
457 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
459 if (fibnum < 0 || fibnum > numfibs - 1)
460 errx(EX_USAGE, "%d: invalid fib", fibnum);
462 ifmap = prepare_ifmap(&ifmap_size);
464 xo_open_container("route-nhop-information");
465 xo_emit("{T:Nexthop data}");
467 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
469 print_nhops_sysctl(fibnum, af);
470 xo_close_container("route-nhop-information");