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/cdefs.h>
33 __FBSDID("$FreeBSD$");
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 <netlink/netlink.h>
47 #include <netlink/netlink_route.h>
48 #include <netlink/netlink_snl.h>
49 #include <netlink/netlink_snl_route.h>
50 #include <netlink/netlink_snl_route_parsers.h>
51 #include <netlink/netlink_snl_route_compat.h>
53 #include <netinet/in.h>
54 #include <netgraph/ng_socket.h>
56 #include <arpa/inet.h>
75 static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr);
77 static struct ifmap_entry *ifmap;
78 static size_t ifmap_size;
80 /* Generate ifmap using netlink */
81 static struct ifmap_entry *
82 prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
86 struct ifinfomsg ifmsg;
88 .hdr.nlmsg_type = RTM_GETLINK,
89 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
90 .hdr.nlmsg_seq = snl_get_seq(ss),
92 msg.hdr.nlmsg_len = sizeof(msg);
94 if (!snl_send_message(ss, &msg.hdr))
97 struct ifmap_entry *ifmap = NULL;
98 uint32_t ifmap_size = 0;
100 struct snl_errmsg_data e = {};
102 while ((hdr = snl_read_reply_multi(ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
103 struct snl_parsed_link_simple link = {};
105 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
107 if (link.ifi_index >= ifmap_size) {
108 size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry);
109 if ((ifmap = realloc(ifmap, size)) == NULL)
110 errx(2, "realloc(%zu) failed", size);
111 memset(&ifmap[ifmap_size], 0,
113 sizeof(struct ifmap_entry));
114 ifmap_size = roundup2(link.ifi_index + 1, 32);
116 if (*ifmap[link.ifi_index].ifname != '\0')
118 strlcpy(ifmap[link.ifi_index].ifname, link.ifla_ifname, IFNAMSIZ);
119 ifmap[link.ifi_index].mtu = link.ifla_mtu;
121 *pifmap_size = ifmap_size;
126 ip6_writemask(struct in6_addr *addr6, uint8_t mask)
130 for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
133 *cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
137 gen_mask(int family, int plen, struct sockaddr *sa)
139 if (family == AF_INET6) {
140 struct sockaddr_in6 sin6 = {
141 .sin6_family = AF_INET6,
142 .sin6_len = sizeof(struct sockaddr_in6),
144 ip6_writemask(&sin6.sin6_addr, plen);
145 *((struct sockaddr_in6 *)sa) = sin6;
146 } else if (family == AF_INET) {
147 struct sockaddr_in sin = {
148 .sin_family = AF_INET,
149 .sin_len = sizeof(struct sockaddr_in),
150 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
152 *((struct sockaddr_in *)sa) = sin;
157 add_scopeid(struct sockaddr *sa, int ifindex)
159 if (sa->sa_family == AF_INET6) {
160 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
161 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
162 sin6->sin6_scope_id = ifindex;
167 p_path(struct snl_parsed_route *rt, bool is_mpath)
169 struct sockaddr_in6 mask6;
170 struct sockaddr *pmask = (struct sockaddr *)&mask6;
172 char prettyname[128];
175 gen_mask(rt->rtm_family, rt->rtm_dst_len, pmask);
176 add_scopeid(rt->rta_dst, rt->rta_oif);
177 add_scopeid(rt->rta_gw, rt->rta_oif);
178 protrusion = p_sockaddr("destination", rt->rta_dst, pmask, rt->rta_rtflags, wid.dst);
179 protrusion = p_sockaddr("gateway", rt->rta_gw, NULL, RTF_HOST,
180 wid.gw - protrusion);
181 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
182 wid.flags - protrusion);
183 p_flags(rt->rta_rtflags | RTF_UP, buffer);
184 /* Output path weight as non-visual property */
185 xo_emit("{e:weight/%u}", rt->rtax_weight);
187 xo_emit("{e:nhg-kidx/%u}", rt->rta_knh_id);
189 xo_emit("{e:nhop-kidx/%u}", rt->rta_knh_id);
190 if (rt->rta_nh_id != 0) {
192 xo_emit("{e:nhg-uidx/%u}", rt->rta_nh_id);
194 xo_emit("{e:nhop-uidx/%u}", rt->rta_nh_id);
197 memset(prettyname, 0, sizeof(prettyname));
198 if (rt->rta_oif < ifmap_size) {
199 strlcpy(prettyname, ifmap[rt->rta_oif].ifname,
201 if (*prettyname == '\0')
202 strlcpy(prettyname, "---", sizeof(prettyname));
203 if (rt->rtax_mtu == 0)
204 rt->rtax_mtu = ifmap[rt->rta_oif].mtu;
209 xo_emit("{t:nhop/%*lu} ", wid.mtu, is_mpath ? 0 : rt->rta_knh_id);
211 if (rt->rtax_mtu != 0)
212 xo_emit("{t:mtu/%*lu} ", wid.mtu, rt->rtax_mtu);
214 /* use interface mtu */
215 xo_emit("{P:/%*s} ", wid.mtu, "");
221 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
223 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
225 if (rt->rta_expires > 0) {
226 xo_emit(" {:expire-time/%*u}", wid.expire, rt->rta_expires);
231 p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
234 struct snl_parsed_route rt = {};
235 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &rt))
237 if (rt.rtax_weight == 0)
238 rt.rtax_weight = rt_default_weight;
240 if (rt.rta_multipath.num_nhops != 0) {
241 uint32_t orig_rtflags = rt.rta_rtflags;
242 uint32_t orig_mtu = rt.rtax_mtu;
243 for (uint32_t i = 0; i < rt.rta_multipath.num_nhops; i++) {
244 struct rta_mpath_nh *nhop = rt.rta_multipath.nhops[i];
246 rt.rta_gw = nhop->gw;
247 rt.rta_oif = nhop->ifindex;
248 rt.rtax_weight = nhop->rtnh_weight;
249 rt.rta_rtflags = nhop->rta_rtflags ? nhop->rta_rtflags : orig_rtflags;
250 rt.rtax_mtu = nhop->rtax_mtu ? nhop->rtax_mtu : orig_mtu;
252 xo_open_instance(name);
255 xo_close_instance(name);
260 struct sockaddr_dl sdl_gw = {
261 .sdl_family = AF_LINK,
262 .sdl_len = sizeof(struct sockaddr_dl),
263 .sdl_index = rt.rta_oif,
265 if (rt.rta_gw == NULL)
266 rt.rta_gw = (struct sockaddr *)&sdl_gw;
268 xo_open_instance(name);
271 xo_close_instance(name);
275 p_rtable_netlink(int fibnum, int af)
278 int need_table_close = false;
279 struct nlmsghdr *hdr;
280 struct snl_errmsg_data e = {};
281 struct snl_state ss = {};
283 if (!snl_init(&ss, NETLINK_ROUTE))
286 ifmap = prepare_ifmap_netlink(&ss, &ifmap_size);
295 struct nlattr nla_fibnum;
298 .hdr.nlmsg_type = RTM_GETROUTE,
299 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
300 .hdr.nlmsg_seq = snl_get_seq(&ss),
301 .rtmsg.rtm_family = af,
302 .nla_fibnum.nla_len = sizeof(struct nlattr) + sizeof(uint32_t),
303 .nla_fibnum.nla_type = RTA_TABLE,
306 msg.hdr.nlmsg_len = sizeof(msg);
308 if (!snl_send_message(&ss, &msg.hdr)) {
313 xo_open_container("route-table");
314 xo_open_list("rt-family");
315 while ((hdr = snl_read_reply_multi(&ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
316 struct rtmsg *rtm = (struct rtmsg *)(hdr + 1);
317 /* Only print family first time. */
318 if (fam != rtm->rtm_family) {
319 if (need_table_close) {
320 xo_close_list("rt-entry");
321 xo_close_instance("rt-family");
323 need_table_close = true;
324 fam = rtm->rtm_family;
326 xo_open_instance("rt-family");
328 xo_open_list("rt-entry");
331 p_rtentry_netlink(&ss, "rt-entry", hdr);
334 if (need_table_close) {
335 xo_close_list("rt-entry");
336 xo_close_instance("rt-family");
338 xo_close_list("rt-family");
339 xo_close_container("route-table");