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