2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
35 #include <net/if_dl.h>
36 #include <net/if_types.h>
37 #include <net/route.h>
38 #include <net/route/nhop.h>
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
55 #define WID_GW_DEFAULT(af) (((af) == AF_INET6) ? 40 : 18)
58 static int wid_if = 10;
59 static int wid_nhidx = 8;
60 static int wid_refcnt = 8;
64 char ifname[IFNAMSIZ];
68 struct nhop_entry *ptr;
71 static struct nhop_map global_nhop_map;
73 static struct ifmap_entry *ifmap;
74 static size_t ifmap_size;
76 static struct nhop_entry *
77 nhop_get(struct nhop_map *map, uint32_t idx)
82 if (*map->ptr[idx].ifname == '\0')
84 return &map->ptr[idx];
88 print_nhgroup_header(int af1 __unused)
91 xo_emit("{T:/%-*.*s}{T:/%-*.*s}{T:/%*.*s}{T:/%*.*s}{T:/%*.*s}"
92 "{T:/%*.*s}{T:/%*s}\n",
93 wid_nhidx, wid_nhidx, "GrpIdx",
94 wid_nhidx, wid_nhidx, "NhIdx",
95 wid_nhidx, wid_nhidx, "Weight",
96 wid_nhidx, wid_nhidx, "Slots",
97 wid_gw, wid_gw, "Gateway",
98 wid_if, wid_if, "Netif",
99 wid_refcnt, "Refcnt");
103 print_padding(char sym, int len)
107 memset(buffer, sym, sizeof(buffer));
112 buffer[len + 3] = '}';
113 buffer[len + 4] = '\0';
119 print_nhgroup_entry_sysctl(const char *name, struct rt_msghdr *rtm,
120 struct nhgrp_external *nhge)
123 struct nhop_entry *ne;
124 struct nhgrp_nhop_external *ext_cp, *ext_dp;
125 struct nhgrp_container *nhg_cp, *nhg_dp;
127 nhg_cp = (struct nhgrp_container *)(nhge + 1);
128 if (nhg_cp->nhgc_type != NHG_C_TYPE_CNHOPS || nhg_cp->nhgc_subtype != 0)
130 ext_cp = (struct nhgrp_nhop_external *)(nhg_cp + 1);
132 nhg_dp = (struct nhgrp_container *)((char *)nhg_cp + nhg_cp->nhgc_len);
133 if (nhg_dp->nhgc_type != NHG_C_TYPE_DNHOPS || nhg_dp->nhgc_subtype != 0)
135 ext_dp = (struct nhgrp_nhop_external *)(nhg_dp + 1);
137 xo_open_instance(name);
139 snprintf(buffer, sizeof(buffer), "{[:-%d}{:nhgrp-index/%%lu}{]:} ", wid_nhidx);
141 xo_emit(buffer, nhge->nhg_idx);
144 print_padding('-', wid_nhidx);
146 print_padding('-', wid_nhidx);
148 print_padding('-', wid_nhidx);
149 print_padding('-', wid_gw);
150 print_padding('-', wid_if);
151 xo_emit("{t:nhg-refcnt/%*lu}", wid_refcnt, nhge->nhg_refcount);
154 xo_open_list("nhop-weights");
155 for (uint32_t i = 0; i < nhg_cp->nhgc_count; i++) {
156 /* TODO: optimize slots calculations */
158 for (uint32_t sidx = 0; sidx < nhg_dp->nhgc_count; sidx++) {
159 if (ext_dp[sidx].nh_idx == ext_cp[i].nh_idx)
162 xo_open_instance("nhop-weight");
163 print_padding(' ', wid_nhidx);
165 xo_emit("{t:nh-index/%*lu}", wid_nhidx, ext_cp[i].nh_idx);
166 xo_emit("{t:nh-weight/%*lu}", wid_nhidx, ext_cp[i].nh_weight);
167 xo_emit("{t:nh-slots/%*lu}", wid_nhidx, slots);
168 ne = nhop_get(&global_nhop_map, ext_cp[i].nh_idx);
170 xo_emit("{t:nh-gw/%*.*s}", wid_gw, wid_gw, ne->gw);
171 xo_emit("{t:nh-interface/%*.*s}", wid_if, wid_if, ne->ifname);
174 xo_close_instance("nhop-weight");
176 xo_close_list("nhop-weights");
177 xo_close_instance(name);
181 cmp_nhg_idx(const void *_a, const void *_b)
183 const struct nhops_map *a, *b;
190 else if (a->idx < b->idx)
196 dump_nhgrp_sysctl(int fibnum, int af, struct nhops_dump *nd)
200 char *buf, *next, *lim;
201 struct rt_msghdr *rtm;
202 struct nhgrp_external *nhg;
203 struct nhops_map *nhg_map;
204 size_t nhg_count, nhg_size;
210 mib[4] = NET_RT_NHGRP;
213 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
214 err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d estimate",
216 if ((buf = malloc(needed)) == NULL)
217 errx(2, "malloc(%lu)", (unsigned long)needed);
218 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
219 err(1, "sysctl: net.route.0.%d.nhgrpdump.%d", af, fibnum);
223 * nexhops groups are received unsorted. Collect everything first,
224 * and sort prior displaying.
228 nhg_map = calloc(nhg_size, sizeof(struct nhops_map));
229 for (next = buf; next < lim; next += rtm->rtm_msglen) {
230 rtm = (struct rt_msghdr *)next;
231 if (rtm->rtm_version != RTM_VERSION)
234 if (nhg_count >= nhg_size) {
236 nhg_map = realloc(nhg_map, nhg_size * sizeof(struct nhops_map));
239 nhg = (struct nhgrp_external *)(rtm + 1);
240 nhg_map[nhg_count].idx = nhg->nhg_idx;
241 nhg_map[nhg_count].rtm = rtm;
246 qsort(nhg_map, nhg_count, sizeof(struct nhops_map), cmp_nhg_idx);
248 nd->nh_count = nhg_count;
249 nd->nh_map = nhg_map;
253 print_nhgrp_sysctl(int fibnum, int af)
255 struct nhops_dump nd;
256 struct nhgrp_external *nhg;
257 struct rt_msghdr *rtm;
259 dump_nhgrp_sysctl(fibnum, af, &nd);
261 xo_open_container("nhgrp-table");
262 xo_open_list("rt-family");
263 if (nd.nh_count > 0) {
264 wid_gw = WID_GW_DEFAULT(af);
265 xo_open_instance("rt-family");
267 xo_open_list("nhgrp-entry");
269 print_nhgroup_header(af);
271 for (size_t i = 0; i < nd.nh_count; i++) {
272 rtm = nd.nh_map[i].rtm;
273 nhg = (struct nhgrp_external *)(rtm + 1);
274 print_nhgroup_entry_sysctl("nhgrp-entry", rtm, nhg);
277 xo_close_list("rt-family");
278 xo_close_container("nhgrp-table");
283 update_global_map(struct nhop_external *nh)
285 char iface_name[128];
287 struct nhop_addrs *na;
288 struct sockaddr *sa_gw;
290 na = (struct nhop_addrs *)((char *)nh + nh->nh_len);
291 sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off);
293 memset(iface_name, 0, sizeof(iface_name));
294 if (nh->ifindex < (uint32_t)ifmap_size) {
295 strlcpy(iface_name, ifmap[nh->ifindex].ifname,
297 if (*iface_name == '\0')
298 strlcpy(iface_name, "---", sizeof(iface_name));
301 if (nh->nh_flags & NHF_GATEWAY) {
303 cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST);
304 strlcpy(gw_addr, cp, sizeof(gw_addr));
306 snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name);
308 nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name);
312 prepare_nh_map(int fibnum, int af)
314 struct nhops_dump nd;
315 struct nhop_external *nh;
316 struct rt_msghdr *rtm;
318 dump_nhops_sysctl(fibnum, af, &nd);
320 for (size_t i = 0; i < nd.nh_count; i++) {
321 rtm = nd.nh_map[i].rtm;
322 nh = (struct nhop_external *)(rtm + 1);
323 update_global_map(nh);
330 nhgrp_print(int fibnum, int af)
335 intsize = sizeof(int);
337 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
339 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
341 if (fibnum < 0 || fibnum > numfibs - 1)
342 errx(EX_USAGE, "%d: invalid fib", fibnum);
344 ifmap = prepare_ifmap(&ifmap_size);
345 prepare_nh_map(fibnum, af);
347 xo_open_container("route-nhgrp-information");
348 xo_emit("{T:Nexthop groups data}");
350 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
352 print_nhgrp_sysctl(fibnum, af);
353 xo_close_container("route-nhgrp-information");