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>
44 #include <net/route/nhop.h>
46 #include <netinet/in.h>
47 #include <netgraph/ng_socket.h>
49 #include <arpa/inet.h>
66 /* column widths; each followed by one space */
68 #define WID_DST_DEFAULT(af) 18 /* width of destination column */
69 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */
70 #define WID_IF_DEFAULT(af) (Wflag ? 10 : 8) /* width of netif column */
72 #define WID_DST_DEFAULT(af) \
73 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
74 #define WID_GW_DEFAULT(af) \
75 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
76 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8))
81 static int wid_pksent;
85 static int wid_nhtype;
86 static int wid_refcnt;
87 static int wid_prepend;
89 static struct bits nh_bits[] = {
90 { NHF_REJECT, 'R', "reject" },
91 { NHF_BLACKHOLE,'B', "blackhole" },
92 { NHF_REDIRECT, 'r', "redirect" },
93 { NHF_GATEWAY, 'G', "gateway" },
94 { NHF_DEFAULT, 'd', "default" },
95 { NHF_BROADCAST,'b', "broadcast" },
99 static char *nh_types[] = {
101 "v4/resolve", /* 1 */
109 char ifname[IFNAMSIZ];
113 struct nhop_entry *ptr;
116 static struct nhop_map global_nhop_map;
118 static struct nhop_entry *nhop_get(struct nhop_map *map, uint32_t idx);
121 static struct ifmap_entry *ifmap;
122 static size_t ifmap_size;
125 print_sockaddr_buf(char *buf, size_t bufsize, const struct sockaddr *sa)
128 switch (sa->sa_family) {
130 inet_ntop(AF_INET, &((struct sockaddr_in *)sa)->sin_addr,
134 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr,
138 snprintf(buf, bufsize, "unknown:%d", sa->sa_family);
144 print_addr(const char *name, const char *addr, int width)
150 snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
154 if (Wflag != 0 || numeric_addr) {
155 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
158 protrusion = strlen(addr) - width;
162 snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
164 xo_emit(buf, width, addr);
173 print_nhop_header(int af1 __unused)
177 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
178 "{T:/%*.*s} {T:/%-*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
179 wid_nhidx, wid_nhidx, "Idx",
180 wid_nhtype, wid_nhtype, "Type",
181 wid_dst, wid_dst, "IFA",
182 wid_gw, wid_gw, "Gateway",
183 wid_flags, wid_flags, "Flags",
184 wid_pksent, wid_pksent, "Use",
185 wid_mtu, wid_mtu, "Mtu",
186 wid_if, wid_if, "Netif",
187 wid_if, wid_if, "Addrif",
188 wid_refcnt, wid_refcnt, "Refcnt",
189 wid_prepend, "Prepend");
191 xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
193 wid_nhidx, wid_nhidx, "Idx",
194 wid_dst, wid_dst, "IFA",
195 wid_gw, wid_gw, "Gateway",
196 wid_flags, wid_flags, "Flags",
197 wid_if, wid_if, "Netif",
198 wid_prepend, "Refcnt");
203 nhop_map_update(struct nhop_map *map, uint32_t idx, char *gw, char *ifname)
205 if (idx >= map->size) {
211 new_size = map->size * 2;
213 new_size = roundup2(idx + 1, 32);
215 sz = new_size * (sizeof(struct nhop_entry));
216 if ((map->ptr = realloc(map->ptr, sz)) == NULL)
217 errx(2, "realloc(%zu) failed", sz);
219 memset(&map->ptr[map->size], 0, (new_size - map->size) * sizeof(struct nhop_entry));
220 map->size = new_size;
223 strlcpy(map->ptr[idx].ifname, ifname, sizeof(map->ptr[idx].ifname));
224 strlcpy(map->ptr[idx].gw, gw, sizeof(map->ptr[idx].gw));
227 static struct nhop_entry *
228 nhop_get(struct nhop_map *map, uint32_t idx)
231 if (idx >= map->size)
233 if (*map->ptr[idx].ifname == '\0')
235 return &map->ptr[idx];
239 print_nhop_entry_sysctl(const char *name, struct rt_msghdr *rtm, struct nhop_external *nh)
242 char iface_name[128];
245 struct nhop_addrs *na;
246 struct sockaddr *sa_gw, *sa_ifa;
248 xo_open_instance(name);
250 snprintf(buffer, sizeof(buffer), "{[:-%d}{:index/%%lu}{]:} ", wid_nhidx);
251 //xo_emit("{t:index/%-lu} ", wid_nhidx, nh->nh_idx);
252 xo_emit(buffer, nh->nh_idx);
255 char *cp = nh_types[nh->nh_type];
256 xo_emit("{t:type_str/%*s} ", wid_nhtype, cp);
258 memset(iface_name, 0, sizeof(iface_name));
259 if (nh->ifindex < (uint32_t)ifmap_size) {
260 strlcpy(iface_name, ifmap[nh->ifindex].ifname,
262 if (*iface_name == '\0')
263 strlcpy(iface_name, "---", sizeof(iface_name));
266 na = (struct nhop_addrs *)((char *)nh + nh->nh_len);
267 //inet_ntop(nh->nh_family, &nh->nh_src, src_addr, sizeof(src_addr));
268 //protrusion = p_addr("ifa", src_addr, wid_dst);
269 sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off);
270 sa_ifa = (struct sockaddr *)((char *)na + na->src_sa_off);
271 protrusion = p_sockaddr("ifa", sa_ifa, NULL, RTF_HOST, wid_dst);
273 if (nh->nh_flags & NHF_GATEWAY) {
275 cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST);
276 strlcpy(gw_addr, cp, sizeof(gw_addr));
278 snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name);
279 protrusion = print_addr("gateway", gw_addr, wid_dst - protrusion);
281 nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name);
283 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
284 wid_flags - protrusion);
286 //p_nhflags(nh->nh_flags, buffer);
287 print_flags_generic(rtm->rtm_flags, rt_bits, buffer, "rt_flags_pretty");
290 xo_emit("{t:use/%*lu} ", wid_pksent, nh->nh_pksent);
291 xo_emit("{t:mtu/%*lu} ", wid_mtu, nh->nh_mtu);
293 //printf("IDX: %d IFACE: %s FAMILY: %d TYPE: %d FLAGS: %X GW \n");
296 xo_emit("{t:interface-name/%*s}", wid_if, iface_name);
298 xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, iface_name);
300 memset(iface_name, 0, sizeof(iface_name));
301 if (nh->aifindex < (uint32_t)ifmap_size && nh->ifindex != nh->aifindex) {
302 strlcpy(iface_name, ifmap[nh->aifindex].ifname,
304 if (*iface_name == '\0')
305 strlcpy(iface_name, "---", sizeof(iface_name));
308 xo_emit("{t:address-interface-name/%*s}", wid_if, iface_name);
310 xo_emit("{t:refcount/%*lu} ", wid_refcnt, nh->nh_refcount);
311 if (Wflag && nh->prepend_len) {
312 int max_bytes = MIN(nh->prepend_len, sizeof(buffer) / 2 - 1);
313 for (int i = 0; i < max_bytes; i++)
314 snprintf(&buffer[i * 2], 3, "%02X", nh->nh_prepend[i]);
315 xo_emit(" {:nhop-prepend/%*s}", wid_prepend, buffer);
319 xo_close_instance(name);
323 cmp_nh_idx(const void *_a, const void *_b)
325 const struct nhops_map *a, *b;
332 else if (a->idx < b->idx)
338 dump_nhops_sysctl(int fibnum, int af, struct nhops_dump *nd)
342 char *buf, *next, *lim;
343 struct rt_msghdr *rtm;
344 struct nhop_external *nh;
345 struct nhops_map *nh_map;
346 size_t nh_count, nh_size;
352 mib[4] = NET_RT_NHOP;
355 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
356 err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af,
358 if ((buf = malloc(needed)) == NULL)
359 errx(2, "malloc(%lu)", (unsigned long)needed);
360 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
361 err(1, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum);
365 * nexhops are received unsorted. Collect everything first, sort and then display
370 nh_map = calloc(nh_size, sizeof(struct nhops_map));
371 for (next = buf; next < lim; next += rtm->rtm_msglen) {
372 rtm = (struct rt_msghdr *)next;
373 if (rtm->rtm_version != RTM_VERSION)
376 if (nh_count >= nh_size) {
378 nh_map = realloc(nh_map, nh_size * sizeof(struct nhops_map));
381 nh = (struct nhop_external *)(rtm + 1);
382 nh_map[nh_count].idx = nh->nh_idx;
383 nh_map[nh_count].rtm = rtm;
388 qsort(nh_map, nh_count, sizeof(struct nhops_map), cmp_nh_idx);
390 nd->nh_count = nh_count;
395 print_nhops_sysctl(int fibnum, int af)
397 struct nhops_dump nd;
398 struct nhop_external *nh;
400 struct rt_msghdr *rtm;
402 dump_nhops_sysctl(fibnum, af, &nd);
404 xo_open_container("nhop-table");
405 xo_open_list("rt-family");
406 if (nd.nh_count > 0) {
407 nh = (struct nhop_external *)(nd.nh_map[0].rtm + 1);
410 wid_dst = WID_GW_DEFAULT(fam);
411 wid_gw = WID_GW_DEFAULT(fam);
418 wid_if = WID_IF_DEFAULT(fam);
419 xo_open_instance("rt-family");
421 xo_open_list("nh-entry");
423 print_nhop_header(fam);
425 for (size_t i = 0; i < nd.nh_count; i++) {
426 rtm = nd.nh_map[i].rtm;
427 nh = (struct nhop_external *)(rtm + 1);
428 print_nhop_entry_sysctl("nh-entry", rtm, nh);
431 xo_close_list("nh-entry");
432 xo_close_instance("rt-family");
434 xo_close_list("rt-family");
435 xo_close_container("nhop-table");
440 p_nhflags(int f, const char *format)
443 char *pretty_name = "nh_flags_pretty";
445 xo_emit(format, fmt_flags(nh_bits, f));
447 xo_open_list(pretty_name);
448 for (p = nh_bits; p->b_mask; p++)
450 xo_emit("{le:nh_flags_pretty/%s}", p->b_name);
451 xo_close_list(pretty_name);
455 nhops_print(int fibnum, int af)
460 intsize = sizeof(int);
462 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
464 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
466 if (fibnum < 0 || fibnum > numfibs - 1)
467 errx(EX_USAGE, "%d: invalid fib", fibnum);
469 ifmap = prepare_ifmap(&ifmap_size);
471 xo_open_container("route-nhop-information");
472 xo_emit("{T:Nexthop data}");
474 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
476 print_nhops_sysctl(fibnum, af);
477 xo_close_container("route-nhop-information");