xref: /freebsd/usr.bin/netstat/route_netlink.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
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>
37 #include <sys/time.h>
38 
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <netlink/netlink.h>
44 #include <netlink/netlink_route.h>
45 #include <netlink/netlink_snl.h>
46 #include <netlink/netlink_snl_route.h>
47 #include <netlink/netlink_snl_route_parsers.h>
48 #include <netlink/netlink_snl_route_compat.h>
49 
50 #include <netinet/in.h>
51 #include <netgraph/ng_socket.h>
52 
53 #include <arpa/inet.h>
54 #include <ifaddrs.h>
55 #include <libutil.h>
56 #include <netdb.h>
57 #include <stdbool.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdbool.h>
62 #include <string.h>
63 #include <sysexits.h>
64 #include <unistd.h>
65 #include <err.h>
66 #include <libxo/xo.h>
67 #include "netstat.h"
68 #include "common.h"
69 #include "nl_defs.h"
70 
71 
72 static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr);
73 
74 static struct ifmap_entry *ifmap;
75 static size_t ifmap_size;
76 
77 /* Generate ifmap using netlink */
78 static struct ifmap_entry *
79 prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
80 {
81 	struct {
82 		struct nlmsghdr hdr;
83 		struct ifinfomsg ifmsg;
84 	} msg = {
85 		.hdr.nlmsg_type = RTM_GETLINK,
86 		.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
87 		.hdr.nlmsg_seq = snl_get_seq(ss),
88 	};
89 	msg.hdr.nlmsg_len = sizeof(msg);
90 
91 	if (!snl_send_message(ss, &msg.hdr))
92 		return (NULL);
93 
94 	struct ifmap_entry *ifmap = NULL;
95 	uint32_t ifmap_size = 0;
96 	struct nlmsghdr *hdr;
97 	struct snl_errmsg_data e = {};
98 
99 	while ((hdr = snl_read_reply_multi(ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
100 		struct snl_parsed_link_simple link = {};
101 
102 		if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
103 			continue;
104 		if (link.ifi_index >= ifmap_size) {
105 			size_t size = roundup2(link.ifi_index + 1, 32) * sizeof(struct ifmap_entry);
106 			if ((ifmap = realloc(ifmap, size)) == NULL)
107 				errx(2, "realloc(%zu) failed", size);
108 			memset(&ifmap[ifmap_size], 0,
109 			    size - ifmap_size *
110 			    sizeof(struct ifmap_entry));
111 			ifmap_size = roundup2(link.ifi_index + 1, 32);
112 		}
113 		if (*ifmap[link.ifi_index].ifname != '\0')
114 			continue;
115 		strlcpy(ifmap[link.ifi_index].ifname, link.ifla_ifname, IFNAMSIZ);
116 		ifmap[link.ifi_index].mtu = link.ifla_mtu;
117 	}
118 	*pifmap_size = ifmap_size;
119 	return (ifmap);
120 }
121 
122 static void
123 ip6_writemask(struct in6_addr *addr6, uint8_t mask)
124 {
125 	uint32_t *cp;
126 
127 	for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
128 		*cp++ = 0xFFFFFFFF;
129 	if (mask > 0)
130 		*cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
131 }
132 
133 static void
134 gen_mask(int family, int plen, struct sockaddr *sa)
135 {
136 	if (family == AF_INET6) {
137 		struct sockaddr_in6 sin6 = {
138 			.sin6_family = AF_INET6,
139 			.sin6_len = sizeof(struct sockaddr_in6),
140 		};
141 		ip6_writemask(&sin6.sin6_addr, plen);
142 		*((struct sockaddr_in6 *)sa) = sin6;
143 	} else if (family == AF_INET) {
144 		struct sockaddr_in sin = {
145 			.sin_family = AF_INET,
146 			.sin_len = sizeof(struct sockaddr_in),
147 			.sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
148 		};
149 		*((struct sockaddr_in *)sa) = sin;
150 	}
151 }
152 
153 static void
154 add_scopeid(struct sockaddr *sa, int ifindex)
155 {
156 	if (sa->sa_family == AF_INET6) {
157 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
158 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
159 			sin6->sin6_scope_id = ifindex;
160 	}
161 }
162 
163 static void
164 p_path(struct snl_parsed_route *rt, bool is_mpath)
165 {
166 	struct sockaddr_in6 mask6;
167 	struct sockaddr *pmask = (struct sockaddr *)&mask6;
168 	char buffer[128];
169 	char prettyname[128];
170 	int protrusion;
171 
172 	gen_mask(rt->rtm_family, rt->rtm_dst_len, pmask);
173 	add_scopeid(rt->rta_dst, rt->rta_oif);
174 	add_scopeid(rt->rta_gw, rt->rta_oif);
175 	protrusion = p_sockaddr("destination", rt->rta_dst, pmask, rt->rta_rtflags, wid.dst);
176 	protrusion = p_sockaddr("gateway", rt->rta_gw, NULL, RTF_HOST,
177 	    wid.gw - protrusion);
178 	snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
179 	    wid.flags - protrusion);
180 	p_flags(rt->rta_rtflags | RTF_UP, buffer);
181 	/* Output path weight as non-visual property */
182 	xo_emit("{e:weight/%u}", rt->rtax_weight);
183 	if (is_mpath)
184 		xo_emit("{e:nhg-kidx/%u}", rt->rta_knh_id);
185 	else
186 		xo_emit("{e:nhop-kidx/%u}", rt->rta_knh_id);
187 	if (rt->rta_nh_id != 0) {
188 		if (is_mpath)
189 			xo_emit("{e:nhg-uidx/%u}", rt->rta_nh_id);
190 		else
191 			xo_emit("{e:nhop-uidx/%u}", rt->rta_nh_id);
192 	}
193 
194 	memset(prettyname, 0, sizeof(prettyname));
195 	if (rt->rta_oif < ifmap_size) {
196 		strlcpy(prettyname, ifmap[rt->rta_oif].ifname,
197 		    sizeof(prettyname));
198 		if (*prettyname == '\0')
199 			strlcpy(prettyname, "---", sizeof(prettyname));
200 		if (rt->rtax_mtu == 0)
201 			rt->rtax_mtu = ifmap[rt->rta_oif].mtu;
202 	}
203 
204 	if (Wflag) {
205 		/* XXX: use=0? */
206 		xo_emit("{t:nhop/%*lu} ", wid.mtu, is_mpath ? 0 : rt->rta_knh_id);
207 
208 		if (rt->rtax_mtu != 0)
209 			xo_emit("{t:mtu/%*lu} ", wid.mtu, rt->rtax_mtu);
210 		else {
211 			/* use interface mtu */
212 			xo_emit("{P:/%*s} ", wid.mtu, "");
213 		}
214 
215 	}
216 
217 	if (Wflag)
218 		xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
219 	else
220 		xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
221 		    prettyname);
222 	if (rt->rta_expires > 0) {
223 		xo_emit(" {:expire-time/%*u}", wid.expire, rt->rta_expires);
224 	}
225 }
226 
227 static void
228 p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
229 {
230 
231 	struct snl_parsed_route rt = {};
232 	if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &rt))
233 		return;
234 	if (rt.rtax_weight == 0)
235 		rt.rtax_weight = rt_default_weight;
236 
237 	if (rt.rta_multipath.num_nhops != 0) {
238 		uint32_t orig_rtflags = rt.rta_rtflags;
239 		uint32_t orig_mtu = rt.rtax_mtu;
240 		for (uint32_t i = 0; i < rt.rta_multipath.num_nhops; i++) {
241 			struct rta_mpath_nh *nhop = rt.rta_multipath.nhops[i];
242 
243 			rt.rta_gw = nhop->gw;
244 			rt.rta_oif = nhop->ifindex;
245 			rt.rtax_weight = nhop->rtnh_weight;
246 			rt.rta_rtflags = nhop->rta_rtflags ? nhop->rta_rtflags : orig_rtflags;
247 			rt.rtax_mtu = nhop->rtax_mtu ? nhop->rtax_mtu : orig_mtu;
248 
249 			xo_open_instance(name);
250 			p_path(&rt, true);
251 			xo_emit("\n");
252 			xo_close_instance(name);
253 		}
254 		return;
255 	}
256 
257 	struct sockaddr_dl sdl_gw = {
258 		.sdl_family = AF_LINK,
259 		.sdl_len = sizeof(struct sockaddr_dl),
260 		.sdl_index = rt.rta_oif,
261 	};
262 	if (rt.rta_gw == NULL)
263 		rt.rta_gw = (struct sockaddr *)&sdl_gw;
264 
265 	xo_open_instance(name);
266 	p_path(&rt, false);
267 	xo_emit("\n");
268 	xo_close_instance(name);
269 }
270 
271 bool
272 p_rtable_netlink(int fibnum, int af)
273 {
274 	int fam = AF_UNSPEC;
275 	int need_table_close = false;
276 	struct nlmsghdr *hdr;
277 	struct snl_errmsg_data e = {};
278 	struct snl_state ss = {};
279 
280 	if (!snl_init(&ss, NETLINK_ROUTE))
281 		return (false);
282 
283 	ifmap = prepare_ifmap_netlink(&ss, &ifmap_size);
284 	if (ifmap == NULL) {
285 		snl_free(&ss);
286 		return (false);
287 	}
288 
289 	struct {
290 		struct nlmsghdr hdr;
291 		struct rtmsg rtmsg;
292 		struct nlattr nla_fibnum;
293 		uint32_t fibnum;
294 	} msg = {
295 		.hdr.nlmsg_type = RTM_GETROUTE,
296 		.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
297 		.hdr.nlmsg_seq = snl_get_seq(&ss),
298 		.rtmsg.rtm_family = af,
299 		.nla_fibnum.nla_len = sizeof(struct nlattr) + sizeof(uint32_t),
300 		.nla_fibnum.nla_type = RTA_TABLE,
301 		.fibnum = fibnum,
302 	};
303 	msg.hdr.nlmsg_len = sizeof(msg);
304 
305 	if (!snl_send_message(&ss, &msg.hdr)) {
306 		snl_free(&ss);
307 		return (false);
308 	}
309 
310 	xo_open_container("route-table");
311 	xo_open_list("rt-family");
312 	while ((hdr = snl_read_reply_multi(&ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
313 		struct rtmsg *rtm = (struct rtmsg *)(hdr + 1);
314 		/* Only print family first time. */
315 		if (fam != rtm->rtm_family) {
316 			if (need_table_close) {
317 				xo_close_list("rt-entry");
318 				xo_close_instance("rt-family");
319 			}
320 			need_table_close = true;
321 			fam = rtm->rtm_family;
322 			set_wid(fam);
323 			xo_open_instance("rt-family");
324 			pr_family(fam);
325 			xo_open_list("rt-entry");
326 			pr_rthdr(fam);
327 		}
328 		p_rtentry_netlink(&ss, "rt-entry", hdr);
329 		snl_clear_lb(&ss);
330 	}
331 	if (need_table_close) {
332 		xo_close_list("rt-entry");
333 		xo_close_instance("rt-family");
334 	}
335 	xo_close_list("rt-family");
336 	xo_close_container("route-table");
337 	snl_free(&ss);
338 	return (true);
339 }
340 
341 
342