2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
34 #include <net/if_dl.h>
35 #include <net/if_types.h>
36 #include <net/route.h>
37 #include <net/route/nhop.h>
39 #include <netinet/in.h>
41 #include <arpa/inet.h>
54 #define WID_GW_DEFAULT(af) (((af) == AF_INET6) ? 40 : 18)
57 static int wid_if = 10;
58 static int wid_nhidx = 8;
59 static int wid_refcnt = 8;
63 char ifname[IFNAMSIZ];
67 struct nhop_entry *ptr;
70 static struct nhop_map global_nhop_map;
72 static struct ifmap_entry *ifmap;
73 static size_t ifmap_size;
75 static struct nhop_entry *
76 nhop_get(struct nhop_map *map, uint32_t idx)
81 if (*map->ptr[idx].ifname == '\0')
83 return &map->ptr[idx];
87 print_nhgroup_header(int af1 __unused)
90 xo_emit("{T:/%-*.*s}{T:/%-*.*s}{T:/%*.*s}{T:/%*.*s}{T:/%*.*s}"
91 "{T:/%*.*s}{T:/%*s}\n",
92 wid_nhidx, wid_nhidx, "GrpIdx",
93 wid_nhidx, wid_nhidx, "NhIdx",
94 wid_nhidx, wid_nhidx, "Weight",
95 wid_nhidx, wid_nhidx, "Slots",
96 wid_gw, wid_gw, "Gateway",
97 wid_if, wid_if, "Netif",
98 wid_refcnt, "Refcnt");
102 print_padding(char sym, int len)
106 memset(buffer, sym, sizeof(buffer));
111 buffer[len + 3] = '}';
112 buffer[len + 4] = '\0';
118 print_nhgroup_entry_sysctl(const char *name, struct rt_msghdr *rtm,
119 struct nhgrp_external *nhge)
122 struct nhop_entry *ne;
123 struct nhgrp_nhop_external *ext_cp, *ext_dp;
124 struct nhgrp_container *nhg_cp, *nhg_dp;
126 nhg_cp = (struct nhgrp_container *)(nhge + 1);
127 if (nhg_cp->nhgc_type != NHG_C_TYPE_CNHOPS || nhg_cp->nhgc_subtype != 0)
129 ext_cp = (struct nhgrp_nhop_external *)(nhg_cp + 1);
131 nhg_dp = (struct nhgrp_container *)((char *)nhg_cp + nhg_cp->nhgc_len);
132 if (nhg_dp->nhgc_type != NHG_C_TYPE_DNHOPS || nhg_dp->nhgc_subtype != 0)
134 ext_dp = (struct nhgrp_nhop_external *)(nhg_dp + 1);
136 xo_open_instance(name);
138 snprintf(buffer, sizeof(buffer), "{[:-%d}{:nhgrp-index/%%lu}{]:} ", wid_nhidx);
140 xo_emit(buffer, nhge->nhg_idx);
143 print_padding('-', wid_nhidx);
145 print_padding('-', wid_nhidx);
147 print_padding('-', wid_nhidx);
148 print_padding('-', wid_gw);
149 print_padding('-', wid_if);
150 xo_emit("{t:nhg-refcnt/%*lu}", wid_refcnt, nhge->nhg_refcount);
153 xo_open_list("nhop-weights");
154 for (uint32_t i = 0; i < nhg_cp->nhgc_count; i++) {
155 /* TODO: optimize slots calculations */
157 for (uint32_t sidx = 0; sidx < nhg_dp->nhgc_count; sidx++) {
158 if (ext_dp[sidx].nh_idx == ext_cp[i].nh_idx)
161 xo_open_instance("nhop-weight");
162 print_padding(' ', wid_nhidx);
164 xo_emit("{t:nh-index/%*lu}", wid_nhidx, ext_cp[i].nh_idx);
165 xo_emit("{t:nh-weight/%*lu}", wid_nhidx, ext_cp[i].nh_weight);
166 xo_emit("{t:nh-slots/%*lu}", wid_nhidx, slots);
167 ne = nhop_get(&global_nhop_map, ext_cp[i].nh_idx);
169 xo_emit("{t:nh-gw/%*.*s}", wid_gw, wid_gw, ne->gw);
170 xo_emit("{t:nh-interface/%*.*s}", wid_if, wid_if, ne->ifname);
173 xo_close_instance("nhop-weight");
175 xo_close_list("nhop-weights");
176 xo_close_instance(name);
180 cmp_nhg_idx(const void *_a, const void *_b)
182 const struct nhops_map *a, *b;
189 else if (a->idx < b->idx)
195 dump_nhgrp_sysctl(int fibnum, int af, struct nhops_dump *nd)
199 char *buf, *next, *lim;
200 struct rt_msghdr *rtm;
201 struct nhgrp_external *nhg;
202 struct nhops_map *nhg_map;
203 size_t nhg_count, nhg_size;
209 mib[4] = NET_RT_NHGRP;
212 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
213 err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d estimate",
215 if ((buf = malloc(needed)) == NULL)
216 errx(2, "malloc(%lu)", (unsigned long)needed);
217 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
218 err(1, "sysctl: net.route.0.%d.nhgrpdump.%d", af, fibnum);
222 * nexhops groups are received unsorted. Collect everything first,
223 * and sort prior displaying.
227 nhg_map = calloc(nhg_size, sizeof(struct nhops_map));
228 for (next = buf; next < lim; next += rtm->rtm_msglen) {
229 rtm = (struct rt_msghdr *)next;
230 if (rtm->rtm_version != RTM_VERSION)
233 if (nhg_count >= nhg_size) {
235 nhg_map = realloc(nhg_map, nhg_size * sizeof(struct nhops_map));
238 nhg = (struct nhgrp_external *)(rtm + 1);
239 nhg_map[nhg_count].idx = nhg->nhg_idx;
240 nhg_map[nhg_count].rtm = rtm;
245 qsort(nhg_map, nhg_count, sizeof(struct nhops_map), cmp_nhg_idx);
247 nd->nh_count = nhg_count;
248 nd->nh_map = nhg_map;
252 print_nhgrp_sysctl(int fibnum, int af)
254 struct nhops_dump nd;
255 struct nhgrp_external *nhg;
256 struct rt_msghdr *rtm;
258 dump_nhgrp_sysctl(fibnum, af, &nd);
260 xo_open_container("nhgrp-table");
261 xo_open_list("rt-family");
262 if (nd.nh_count > 0) {
263 wid_gw = WID_GW_DEFAULT(af);
264 xo_open_instance("rt-family");
266 xo_open_list("nhgrp-entry");
268 print_nhgroup_header(af);
270 for (size_t i = 0; i < nd.nh_count; i++) {
271 rtm = nd.nh_map[i].rtm;
272 nhg = (struct nhgrp_external *)(rtm + 1);
273 print_nhgroup_entry_sysctl("nhgrp-entry", rtm, nhg);
276 xo_close_list("rt-family");
277 xo_close_container("nhgrp-table");
282 update_global_map(struct nhop_external *nh)
284 char iface_name[128];
286 struct nhop_addrs *na;
287 struct sockaddr *sa_gw;
289 na = (struct nhop_addrs *)((char *)nh + nh->nh_len);
290 sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off);
292 memset(iface_name, 0, sizeof(iface_name));
293 if (nh->ifindex < (uint32_t)ifmap_size) {
294 strlcpy(iface_name, ifmap[nh->ifindex].ifname,
296 if (*iface_name == '\0')
297 strlcpy(iface_name, "---", sizeof(iface_name));
300 if (nh->nh_flags & NHF_GATEWAY) {
302 cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST);
303 strlcpy(gw_addr, cp, sizeof(gw_addr));
305 snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name);
307 nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name);
311 prepare_nh_map(int fibnum, int af)
313 struct nhops_dump nd;
314 struct nhop_external *nh;
315 struct rt_msghdr *rtm;
317 dump_nhops_sysctl(fibnum, af, &nd);
319 for (size_t i = 0; i < nd.nh_count; i++) {
320 rtm = nd.nh_map[i].rtm;
321 nh = (struct nhop_external *)(rtm + 1);
322 update_global_map(nh);
329 nhgrp_print(int fibnum, int af)
334 intsize = sizeof(int);
336 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
338 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
340 if (fibnum < 0 || fibnum > numfibs - 1)
341 errx(EX_USAGE, "%d: invalid fib", fibnum);
343 ifmap = prepare_ifmap(&ifmap_size);
344 prepare_nh_map(fibnum, af);
346 xo_open_container("route-nhgrp-information");
347 xo_emit("{T:Nexthop groups data}");
349 xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
351 print_nhgrp_sysctl(fibnum, af);
352 xo_close_container("route-nhgrp-information");