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 #include <sys/param.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sysctl.h>
40 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_types.h>
44 #include <netlink/netlink.h>
45 #include <netlink/netlink_route.h>
46 #include <netlink/netlink_snl.h>
47 #include <netlink/netlink_snl_route.h>
48 #include <netlink/netlink_snl_route_parsers.h>
49 #include <netlink/netlink_snl_route_compat.h>
51 #include <netinet/in.h>
52 #include <netgraph/ng_socket.h>
54 #include <arpa/inet.h>
73 static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr);
75 static struct ifmap_entry *ifmap;
76 static size_t ifmap_size;
78 /* Generate ifmap using netlink */
79 static struct ifmap_entry *
80 prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
84 struct ifinfomsg ifmsg;
86 .hdr.nlmsg_type = RTM_GETLINK,
87 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
88 .hdr.nlmsg_seq = snl_get_seq(ss),
90 msg.hdr.nlmsg_len = sizeof(msg);
92 if (!snl_send_message(ss, &msg.hdr))
95 struct ifmap_entry *ifmap = NULL;
96 uint32_t ifmap_size = 0;
98 struct snl_errmsg_data e = {};
100 while ((hdr = snl_read_reply_multi(ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
101 struct snl_parsed_link_simple link = {};
103 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
105 if (link.ifi_index >= ifmap_size) {
106 size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry);
107 if ((ifmap = realloc(ifmap, size)) == NULL)
108 errx(2, "realloc(%zu) failed", size);
109 memset(&ifmap[ifmap_size], 0,
111 sizeof(struct ifmap_entry));
112 ifmap_size = roundup2(link.ifi_index + 1, 32);
114 if (*ifmap[link.ifi_index].ifname != '\0')
116 strlcpy(ifmap[link.ifi_index].ifname, link.ifla_ifname, IFNAMSIZ);
117 ifmap[link.ifi_index].mtu = link.ifla_mtu;
119 *pifmap_size = ifmap_size;
124 ip6_writemask(struct in6_addr *addr6, uint8_t mask)
128 for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
131 *cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
135 gen_mask(int family, int plen, struct sockaddr *sa)
137 if (family == AF_INET6) {
138 struct sockaddr_in6 sin6 = {
139 .sin6_family = AF_INET6,
140 .sin6_len = sizeof(struct sockaddr_in6),
142 ip6_writemask(&sin6.sin6_addr, plen);
143 *((struct sockaddr_in6 *)sa) = sin6;
144 } else if (family == AF_INET) {
145 struct sockaddr_in sin = {
146 .sin_family = AF_INET,
147 .sin_len = sizeof(struct sockaddr_in),
148 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
150 *((struct sockaddr_in *)sa) = sin;
155 add_scopeid(struct sockaddr *sa, int ifindex)
157 if (sa->sa_family == AF_INET6) {
158 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
159 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
160 sin6->sin6_scope_id = ifindex;
165 p_path(struct snl_parsed_route *rt, bool is_mpath)
167 struct sockaddr_in6 mask6;
168 struct sockaddr *pmask = (struct sockaddr *)&mask6;
170 char prettyname[128];
173 gen_mask(rt->rtm_family, rt->rtm_dst_len, pmask);
174 add_scopeid(rt->rta_dst, rt->rta_oif);
175 add_scopeid(rt->rta_gw, rt->rta_oif);
176 protrusion = p_sockaddr("destination", rt->rta_dst, pmask, rt->rta_rtflags, wid.dst);
177 protrusion = p_sockaddr("gateway", rt->rta_gw, NULL, RTF_HOST,
178 wid.gw - protrusion);
179 snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
180 wid.flags - protrusion);
181 p_flags(rt->rta_rtflags | RTF_UP, buffer);
182 /* Output path weight as non-visual property */
183 xo_emit("{e:weight/%u}", rt->rtax_weight);
185 xo_emit("{e:nhg-kidx/%u}", rt->rta_knh_id);
187 xo_emit("{e:nhop-kidx/%u}", rt->rta_knh_id);
188 if (rt->rta_nh_id != 0) {
190 xo_emit("{e:nhg-uidx/%u}", rt->rta_nh_id);
192 xo_emit("{e:nhop-uidx/%u}", rt->rta_nh_id);
195 memset(prettyname, 0, sizeof(prettyname));
196 if (rt->rta_oif < ifmap_size) {
197 strlcpy(prettyname, ifmap[rt->rta_oif].ifname,
199 if (*prettyname == '\0')
200 strlcpy(prettyname, "---", sizeof(prettyname));
201 if (rt->rtax_mtu == 0)
202 rt->rtax_mtu = ifmap[rt->rta_oif].mtu;
207 xo_emit("{t:nhop/%*lu} ", wid.mtu, is_mpath ? 0 : rt->rta_knh_id);
209 if (rt->rtax_mtu != 0)
210 xo_emit("{t:mtu/%*lu} ", wid.mtu, rt->rtax_mtu);
212 /* use interface mtu */
213 xo_emit("{P:/%*s} ", wid.mtu, "");
219 xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
221 xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
223 if (rt->rta_expires > 0) {
224 xo_emit(" {:expire-time/%*u}", wid.expire, rt->rta_expires);
229 p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
232 struct snl_parsed_route rt = {};
233 if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &rt))
235 if (rt.rtax_weight == 0)
236 rt.rtax_weight = rt_default_weight;
238 if (rt.rta_multipath.num_nhops != 0) {
239 uint32_t orig_rtflags = rt.rta_rtflags;
240 uint32_t orig_mtu = rt.rtax_mtu;
241 for (uint32_t i = 0; i < rt.rta_multipath.num_nhops; i++) {
242 struct rta_mpath_nh *nhop = rt.rta_multipath.nhops[i];
244 rt.rta_gw = nhop->gw;
245 rt.rta_oif = nhop->ifindex;
246 rt.rtax_weight = nhop->rtnh_weight;
247 rt.rta_rtflags = nhop->rta_rtflags ? nhop->rta_rtflags : orig_rtflags;
248 rt.rtax_mtu = nhop->rtax_mtu ? nhop->rtax_mtu : orig_mtu;
250 xo_open_instance(name);
253 xo_close_instance(name);
258 struct sockaddr_dl sdl_gw = {
259 .sdl_family = AF_LINK,
260 .sdl_len = sizeof(struct sockaddr_dl),
261 .sdl_index = rt.rta_oif,
263 if (rt.rta_gw == NULL)
264 rt.rta_gw = (struct sockaddr *)&sdl_gw;
266 xo_open_instance(name);
269 xo_close_instance(name);
273 p_rtable_netlink(int fibnum, int af)
276 int need_table_close = false;
277 struct nlmsghdr *hdr;
278 struct snl_errmsg_data e = {};
279 struct snl_state ss = {};
281 if (!snl_init(&ss, NETLINK_ROUTE))
284 ifmap = prepare_ifmap_netlink(&ss, &ifmap_size);
293 struct nlattr nla_fibnum;
296 .hdr.nlmsg_type = RTM_GETROUTE,
297 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
298 .hdr.nlmsg_seq = snl_get_seq(&ss),
299 .rtmsg.rtm_family = af,
300 .nla_fibnum.nla_len = sizeof(struct nlattr) + sizeof(uint32_t),
301 .nla_fibnum.nla_type = RTA_TABLE,
304 msg.hdr.nlmsg_len = sizeof(msg);
306 if (!snl_send_message(&ss, &msg.hdr)) {
311 xo_open_container("route-table");
312 xo_open_list("rt-family");
313 while ((hdr = snl_read_reply_multi(&ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
314 struct rtmsg *rtm = (struct rtmsg *)(hdr + 1);
315 /* Only print family first time. */
316 if (fam != rtm->rtm_family) {
317 if (need_table_close) {
318 xo_close_list("rt-entry");
319 xo_close_instance("rt-family");
321 need_table_close = true;
322 fam = rtm->rtm_family;
324 xo_open_instance("rt-family");
326 xo_open_list("rt-entry");
329 p_rtentry_netlink(&ss, "rt-entry", hdr);
332 if (need_table_close) {
333 xo_close_list("rt-entry");
334 xo_close_instance("rt-family");
336 xo_close_list("rt-family");
337 xo_close_container("route-table");