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