xref: /freebsd/usr.bin/netstat/route_netlink.c (revision ecaeac805b044f715c98960a8fbf19fe2b76ae6b)
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
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>
40 #include <sys/time.h>
41 
42 #include <net/ethernet.h>
43 #include <net/if.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>
52 
53 #include <netinet/in.h>
54 #include <netgraph/ng_socket.h>
55 
56 #include <arpa/inet.h>
57 #include <ifaddrs.h>
58 #include <libutil.h>
59 #include <netdb.h>
60 #include <stdbool.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <stdbool.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
68 #include <err.h>
69 #include <libxo/xo.h>
70 #include "netstat.h"
71 #include "common.h"
72 #include "nl_defs.h"
73 
74 
75 static void p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr);
76 
77 static struct ifmap_entry *ifmap;
78 static size_t ifmap_size;
79 
80 /* Generate ifmap using netlink */
81 static struct ifmap_entry *
82 prepare_ifmap_netlink(struct snl_state *ss, size_t *pifmap_size)
83 {
84 	struct {
85 		struct nlmsghdr hdr;
86 		struct ifinfomsg ifmsg;
87 	} msg = {
88 		.hdr.nlmsg_type = RTM_GETLINK,
89 		.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
90 		.hdr.nlmsg_seq = snl_get_seq(ss),
91 	};
92 	msg.hdr.nlmsg_len = sizeof(msg);
93 
94 	if (!snl_send_message(ss, &msg.hdr))
95 		return (NULL);
96 
97 	struct ifmap_entry *ifmap = NULL;
98 	uint32_t ifmap_size = 0;
99 	struct nlmsghdr *hdr;
100 	struct snl_errmsg_data e = {};
101 
102 	while ((hdr = snl_read_reply_multi(ss, msg.hdr.nlmsg_seq, &e)) != NULL) {
103 		struct snl_parsed_link_simple link = {};
104 
105 		if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
106 			continue;
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,
112 			    size - ifmap_size *
113 			    sizeof(struct ifmap_entry));
114 			ifmap_size = roundup2(link.ifi_index + 1, 32);
115 		}
116 		if (*ifmap[link.ifi_index].ifname != '\0')
117 			continue;
118 		strlcpy(ifmap[link.ifi_index].ifname, link.ifla_ifname, IFNAMSIZ);
119 		ifmap[link.ifi_index].mtu = link.ifla_mtu;
120 	}
121 	*pifmap_size = ifmap_size;
122 	return (ifmap);
123 }
124 
125 static void
126 ip6_writemask(struct in6_addr *addr6, uint8_t mask)
127 {
128 	uint32_t *cp;
129 
130 	for (cp = (uint32_t *)addr6; mask >= 32; mask -= 32)
131 		*cp++ = 0xFFFFFFFF;
132 	if (mask > 0)
133 		*cp = htonl(mask ? ~((1 << (32 - mask)) - 1) : 0);
134 }
135 
136 static void
137 gen_mask(int family, int plen, struct sockaddr *sa)
138 {
139 	if (family == AF_INET6) {
140 		struct sockaddr_in6 sin6 = {
141 			.sin6_family = AF_INET6,
142 			.sin6_len = sizeof(struct sockaddr_in6),
143 		};
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),
151 		};
152 		*((struct sockaddr_in *)sa) = sin;
153 	}
154 }
155 
156 static void
157 add_scopeid(struct sockaddr *sa, int ifindex)
158 {
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;
163 	}
164 }
165 
166 static void
167 p_path(struct snl_parsed_route *rt, bool is_mpath)
168 {
169 	struct sockaddr_in6 mask6;
170 	struct sockaddr *pmask = (struct sockaddr *)&mask6;
171 	char buffer[128];
172 	char prettyname[128];
173 	int protrusion;
174 
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);
186 	if (is_mpath)
187 		xo_emit("{e:nhg-kidx/%u}", rt->rta_knh_id);
188 	else
189 		xo_emit("{e:nhop-kidx/%u}", rt->rta_knh_id);
190 	if (rt->rta_nh_id != 0) {
191 		if (is_mpath)
192 			xo_emit("{e:nhg-uidx/%u}", rt->rta_nh_id);
193 		else
194 			xo_emit("{e:nhop-uidx/%u}", rt->rta_nh_id);
195 	}
196 
197 	memset(prettyname, 0, sizeof(prettyname));
198 	if (rt->rta_oif < ifmap_size) {
199 		strlcpy(prettyname, ifmap[rt->rta_oif].ifname,
200 		    sizeof(prettyname));
201 		if (*prettyname == '\0')
202 			strlcpy(prettyname, "---", sizeof(prettyname));
203 		if (rt->rtax_mtu == 0)
204 			rt->rtax_mtu = ifmap[rt->rta_oif].mtu;
205 	}
206 
207 	if (Wflag) {
208 		/* XXX: use=0? */
209 		xo_emit("{t:nhop/%*lu} ", wid.mtu, is_mpath ? 0 : rt->rta_knh_id);
210 
211 		if (rt->rtax_mtu != 0)
212 			xo_emit("{t:mtu/%*lu} ", wid.mtu, rt->rtax_mtu);
213 		else {
214 			/* use interface mtu */
215 			xo_emit("{P:/%*s} ", wid.mtu, "");
216 		}
217 
218 	}
219 
220 	if (Wflag)
221 		xo_emit("{t:interface-name/%*s}", wid.iface, prettyname);
222 	else
223 		xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface,
224 		    prettyname);
225 	if (rt->rta_expires > 0) {
226 		xo_emit(" {:expire-time/%*u}", wid.expire, rt->rta_expires);
227 	}
228 }
229 
230 static void
231 p_rtentry_netlink(struct snl_state *ss, const char *name, struct nlmsghdr *hdr)
232 {
233 
234 	struct snl_parsed_route rt = {};
235 	if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_route_parser, &rt))
236 		return;
237 	if (rt.rtax_weight == 0)
238 		rt.rtax_weight = rt_default_weight;
239 
240 	if (rt.rta_multipath != NULL) {
241 		uint32_t orig_rtflags = rt.rta_rtflags;
242 		uint32_t orig_mtu = rt.rtax_mtu;
243 		for (int i = 0; i < rt.rta_multipath->num_nhops; i++) {
244 			struct rta_mpath_nh *nhop = &rt.rta_multipath->nhops[i];
245 
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;
251 
252 			xo_open_instance(name);
253 			p_path(&rt, true);
254 			xo_emit("\n");
255 			xo_close_instance(name);
256 		}
257 		return;
258 	}
259 
260 	struct sockaddr_dl sdl_gw = {
261 		.sdl_family = AF_LINK,
262 		.sdl_len = sizeof(struct sockaddr_dl),
263 		.sdl_index = rt.rta_oif,
264 	};
265 	if (rt.rta_gw == NULL)
266 		rt.rta_gw = (struct sockaddr *)&sdl_gw;
267 
268 	xo_open_instance(name);
269 	p_path(&rt, false);
270 	xo_emit("\n");
271 	xo_close_instance(name);
272 }
273 
274 bool
275 p_rtable_netlink(int fibnum, int af)
276 {
277 	int fam = AF_UNSPEC;
278 	int need_table_close = false;
279 	struct nlmsghdr *hdr;
280 	struct snl_errmsg_data e = {};
281 	struct snl_state ss = {};
282 
283 	if (!snl_init(&ss, NETLINK_ROUTE))
284 		return (false);
285 
286 	ifmap = prepare_ifmap_netlink(&ss, &ifmap_size);
287 	if (ifmap == NULL) {
288 		snl_free(&ss);
289 		return (false);
290 	}
291 
292 	struct {
293 		struct nlmsghdr hdr;
294 		struct rtmsg rtmsg;
295 		struct nlattr nla_fibnum;
296 		uint32_t fibnum;
297 	} msg = {
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,
304 		.fibnum = fibnum,
305 	};
306 	msg.hdr.nlmsg_len = sizeof(msg);
307 
308 	if (!snl_send_message(&ss, &msg.hdr)) {
309 		snl_free(&ss);
310 		return (false);
311 	}
312 
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");
322 			}
323 			need_table_close = true;
324 			fam = rtm->rtm_family;
325 			set_wid(fam);
326 			xo_open_instance("rt-family");
327 			pr_family(fam);
328 			xo_open_list("rt-entry");
329 			pr_rthdr(fam);
330 		}
331 		p_rtentry_netlink(&ss, "rt-entry", hdr);
332 		snl_clear_lb(&ss);
333 	}
334 	if (need_table_close) {
335 		xo_close_list("rt-entry");
336 		xo_close_instance("rt-family");
337 	}
338 	xo_close_list("rt-family");
339 	xo_close_container("route-table");
340 	snl_free(&ss);
341 	return (true);
342 }
343 
344 
345