xref: /freebsd/sys/netlink/route/rt.c (revision e492ad08fc347ebf40979d3a6baa9d7b8917c76b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Ng Peng Nam Sean
5  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include <sys/types.h>
33 #include <sys/malloc.h>
34 #include <sys/rmlock.h>
35 #include <sys/socket.h>
36 
37 #include <net/if.h>
38 #include <net/route.h>
39 #include <net/route/nhop.h>
40 #include <net/route/route_ctl.h>
41 #include <net/route/route_var.h>
42 #include <netinet6/scope6_var.h>
43 #include <netlink/netlink.h>
44 #include <netlink/netlink_ctl.h>
45 #include <netlink/netlink_route.h>
46 #include <netlink/route/route_var.h>
47 
48 #define	DEBUG_MOD_NAME	nl_route
49 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
50 #include <netlink/netlink_debug.h>
51 _DECLARE_DEBUG(LOG_INFO);
52 
53 static unsigned char
get_rtm_type(const struct nhop_object * nh)54 get_rtm_type(const struct nhop_object *nh)
55 {
56 	int nh_flags = nh->nh_flags;
57 
58 	/* Use the fact that nhg runtime flags are only NHF_MULTIPATH */
59 	if (nh_flags & NHF_BLACKHOLE)
60 		return (RTN_BLACKHOLE);
61 	else if (nh_flags & NHF_REJECT)
62 		return (RTN_PROHIBIT);
63 	return (RTN_UNICAST);
64 }
65 
66 static uint8_t
nl_get_rtm_protocol(const struct nhop_object * nh)67 nl_get_rtm_protocol(const struct nhop_object *nh)
68 {
69 	const struct nhgrp_object *nhg = (const struct nhgrp_object *)nh;
70 	int rt_flags;
71 	uint8_t origin;
72 
73 	if (NH_IS_NHGRP(nh)) {
74 		origin = nhgrp_get_origin(nhg);
75 		if (origin != RTPROT_UNSPEC)
76 			return (origin);
77 		nh = nhg->nhops[0];
78 	}
79 	origin = nhop_get_origin(nh);
80 	if (origin != RTPROT_UNSPEC)
81 		return (origin);
82 	/* TODO: remove guesswork once all kernel users fill in origin */
83 	rt_flags = nhop_get_rtflags(nh);
84 	if (rt_flags & RTF_PROTO1)
85 		return (RTPROT_ZEBRA);
86 	if (rt_flags & RTF_STATIC)
87 		return (RTPROT_STATIC);
88 	return (RTPROT_KERNEL);
89 }
90 
91 static int
get_rtmsg_type_from_rtsock(int cmd)92 get_rtmsg_type_from_rtsock(int cmd)
93 {
94 	switch (cmd) {
95 	case RTM_ADD:
96 	case RTM_CHANGE:
97 	case RTM_GET:
98 		return NL_RTM_NEWROUTE;
99 	case RTM_DELETE:
100 		return NL_RTM_DELROUTE;
101 	}
102 
103 	return (0);
104 }
105 
106 /*
107  * fibnum heuristics
108  *
109  * if (dump && rtm_table == 0 && !rta_table) RT_ALL_FIBS
110  * msg                rtm_table     RTA_TABLE            result
111  * RTM_GETROUTE/dump          0             -       RT_ALL_FIBS
112  * RTM_GETROUTE/dump          1             -                 1
113  * RTM_GETROUTE/get           0             -                 0
114  *
115  */
116 
117 static struct nhop_object *
rc_get_nhop(const struct rib_cmd_info * rc)118 rc_get_nhop(const struct rib_cmd_info *rc)
119 {
120 	return ((rc->rc_cmd == RTM_DELETE) ? rc->rc_nh_old : rc->rc_nh_new);
121 }
122 
123 static void
dump_rc_nhop_gw(struct nl_writer * nw,const struct nhop_object * nh)124 dump_rc_nhop_gw(struct nl_writer *nw, const struct nhop_object *nh)
125 {
126 #ifdef INET6
127 	int upper_family;
128 #endif
129 
130 	switch (nhop_get_neigh_family(nh)) {
131 	case AF_LINK:
132 		/* onlink prefix, skip */
133 		break;
134 	case AF_INET:
135 		nlattr_add(nw, NL_RTA_GATEWAY, 4, &nh->gw4_sa.sin_addr);
136 		break;
137 #ifdef INET6
138 	case AF_INET6:
139 		upper_family = nhop_get_upper_family(nh);
140 		if (upper_family == AF_INET6) {
141 			struct in6_addr gw6 = nh->gw6_sa.sin6_addr;
142 			in6_clearscope(&gw6);
143 
144 			nlattr_add(nw, NL_RTA_GATEWAY, 16, &gw6);
145 		} else if (upper_family == AF_INET) {
146 			/* IPv4 over IPv6 */
147 			struct in6_addr gw6 = nh->gw6_sa.sin6_addr;
148 			in6_clearscope(&gw6);
149 
150 			char buf[20];
151 			struct rtvia *via = (struct rtvia *)&buf[0];
152 			via->rtvia_family = AF_INET6;
153 			memcpy(via->rtvia_addr, &gw6, 16);
154 			nlattr_add(nw, NL_RTA_VIA, 17, via);
155 		}
156 		break;
157 #endif
158 	}
159 }
160 
161 static void
dump_rc_nhop_mtu(struct nl_writer * nw,const struct nhop_object * nh)162 dump_rc_nhop_mtu(struct nl_writer *nw, const struct nhop_object *nh)
163 {
164 	int nla_len = sizeof(struct nlattr) * 2 + sizeof(uint32_t);
165 	struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
166 
167 	if (nla == NULL)
168 		return;
169 	nla->nla_type = NL_RTA_METRICS;
170 	nla->nla_len = nla_len;
171 	nla++;
172 	nla->nla_type = NL_RTAX_MTU;
173 	nla->nla_len = sizeof(struct nlattr) + sizeof(uint32_t);
174 	*((uint32_t *)(nla + 1)) = nh->nh_mtu;
175 }
176 
177 static void
dump_rc_nhg(struct nl_writer * nw,const struct route_nhop_data * rnd,struct rtmsg * rtm)178 dump_rc_nhg(struct nl_writer *nw, const struct route_nhop_data *rnd, struct rtmsg *rtm)
179 {
180 	const struct nhgrp_object *nhg = rnd->rnd_nhgrp;
181 	const struct weightened_nhop *wn;
182 	struct nhop_object *nh;
183 	uint32_t uidx, num_nhops, nh_expire;
184 	uint32_t base_rtflags, rtflags, nhop_weight, nhop_metric;
185 
186 	MPASS((NH_IS_NHGRP(rnd->rnd_nhop)));
187 
188 	/* select a nhop from nhgrp to not confuse non-mpath consumers */
189 	nhop_weight = RT_DEFAULT_WEIGHT;
190 	nhop_metric = RT_DEFAULT_METRIC;
191 	nh = nhop_select_func(rnd->rnd_nhop, 0);
192 	rtflags = nhop_get_rtflags(nh);
193 	if (nh->nh_flags & NHF_GATEWAY)
194 		dump_rc_nhop_gw(nw, nh);
195 
196 	wn = nhgrp_get_nhops(nhg, &num_nhops);
197 	base_rtflags = nhop_get_rtflags(wn[0].nh);
198 	uidx = nhgrp_get_uidx(nhg);
199 	if (uidx != 0)
200 		nlattr_add_u32(nw, NL_RTA_NH_ID, uidx);
201 	nlattr_add_u32(nw, NL_RTA_KNH_ID, nhgrp_get_idx(nhg));
202 	nlattr_add_u32(nw, NL_RTA_RTFLAGS, base_rtflags);
203 
204 	if (rtflags & RTF_FIXEDMTU)
205 		dump_rc_nhop_mtu(nw, nh);
206 	/* In any case, fill outgoing interface */
207 	nlattr_add_u32(nw, NL_RTA_OIF, if_getindex(nh->nh_ifp));
208 
209 	int off = nlattr_add_nested(nw, NL_RTA_MULTIPATH);
210 	if (off == 0)
211 		return;
212 
213 	for (int i = 0; i < num_nhops; i++) {
214 		int nh_off = nlattr_save_offset(nw);
215 		struct rtnexthop *rtnh = nlmsg_reserve_object(nw, struct rtnexthop);
216 		if (rtnh == NULL)
217 			return;
218 		rtnh->rtnh_flags = 0;
219 		rtnh->rtnh_ifindex = if_getindex(wn[i].nh->nh_ifp);
220 		rtnh->rtnh_hops = MIN(wn[i].weight, UINT8_MAX);
221 		dump_rc_nhop_gw(nw, wn[i].nh);
222 		uint32_t rtflags = nhop_get_rtflags(wn[i].nh);
223 		if (rtflags != base_rtflags)
224 			nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags);
225 		if (rtflags & RTF_FIXEDMTU)
226 			dump_rc_nhop_mtu(nw, wn[i].nh);
227 		nlattr_add_u32(nw, NL_RTA_PRIORITY, nhop_get_metric(wn[i].nh));
228 		nh_expire = nhop_get_expire(wn[i].nh);
229 		if (nh_expire > 0)
230 			nlattr_add_u32(nw, NL_RTA_EXPIRES, nh_expire - time_uptime);
231 		rtnh = nlattr_restore_offset(nw, nh_off, struct rtnexthop);
232 
233 		if (nh == wn[i].nh) {
234 			nhop_weight = wn[i].weight;
235 			nhop_metric = nhop_get_metric(wn[i].nh);
236 		}
237 		/*
238 		 * nlattr_add() allocates 4-byte aligned storage, no need to aligh
239 		 * length here
240 		 * */
241 		rtnh->rtnh_len = nlattr_save_offset(nw) - nh_off;
242 	}
243 	nlattr_set_len(nw, off);
244 	nlattr_add_u32(nw, NL_RTA_PRIORITY, nhop_metric);
245 	if (nhop_weight != RT_DEFAULT_WEIGHT)
246 		nlattr_add_u32(nw, NL_RTA_WEIGHT, nhop_weight);
247 }
248 
249 static void
dump_rc_nhop(struct nl_writer * nw,const struct route_nhop_data * rnd,struct rtmsg * rtm)250 dump_rc_nhop(struct nl_writer *nw, const struct route_nhop_data *rnd, struct rtmsg *rtm)
251 {
252 	const struct nhop_object *nh = rnd->rnd_nhop;
253 	uint32_t rtflags, uidx, nh_expire;
254 
255 	if (NH_IS_NHGRP(rnd->rnd_nhop)) {
256 		dump_rc_nhg(nw, rnd, rtm);
257 		return;
258 	}
259 
260 	rtflags = nhop_get_rtflags(nh);
261 	/*
262 	 * IPv4 over IPv6
263 	 *    ('RTA_VIA', {'family': 10, 'addr': 'fe80::20c:29ff:fe67:2dd'}), ('RTA_OIF', 2),
264 	 * IPv4 w/ gw
265 	 *    ('RTA_GATEWAY', '172.16.107.131'), ('RTA_OIF', 2)],
266 	 * Direct route:
267 	 *    ('RTA_OIF', 2)
268 	 */
269 	if (nh->nh_flags & NHF_GATEWAY)
270 		dump_rc_nhop_gw(nw, nh);
271 
272 	uidx = nhop_get_uidx(nh);
273 	if (uidx != 0)
274 		nlattr_add_u32(nw, NL_RTA_NH_ID, uidx);
275 	nlattr_add_u32(nw, NL_RTA_KNH_ID, nhop_get_idx(nh));
276 	nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags);
277 
278 	if (rtflags & RTF_FIXEDMTU)
279 		dump_rc_nhop_mtu(nw, nh);
280 	nh_expire = nhop_get_expire(nh);
281 	if (nh_expire > 0)
282 		nlattr_add_u32(nw, NL_RTA_EXPIRES, nh_expire - time_uptime);
283 
284 	/* In any case, fill outgoing interface */
285 	nlattr_add_u32(nw, NL_RTA_OIF, if_getindex(nh->nh_ifp));
286 
287 	nlattr_add_u32(nw, NL_RTA_PRIORITY, nhop_get_metric(nh));
288 	if (rnd->rnd_weight != RT_DEFAULT_WEIGHT)
289 		nlattr_add_u32(nw, NL_RTA_WEIGHT, rnd->rnd_weight);
290 }
291 
292 /*
293  * Dumps output from a rib command into an rtmsg
294  */
295 
296 static int
dump_px(uint32_t fibnum,const struct nlmsghdr * hdr,const struct rtentry * rt,struct route_nhop_data * rnd,struct nl_writer * nw)297 dump_px(uint32_t fibnum, const struct nlmsghdr *hdr,
298     const struct rtentry *rt, struct route_nhop_data *rnd,
299     struct nl_writer *nw)
300 {
301 	struct rtmsg *rtm;
302 	int error = 0;
303 
304 	NET_EPOCH_ASSERT();
305 
306 	if (!nlmsg_reply(nw, hdr, sizeof(struct rtmsg)))
307 		goto enomem;
308 
309 	int family = rt_get_family(rt);
310 	int rtm_off = nlattr_save_offset(nw);
311 	rtm = nlmsg_reserve_object(nw, struct rtmsg);
312 	rtm->rtm_family = family;
313 	rtm->rtm_dst_len = 0;
314 	rtm->rtm_src_len = 0;
315 	rtm->rtm_tos = 0;
316 	if (fibnum < 255)
317 		rtm->rtm_table = (unsigned char)fibnum;
318 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
319 	rtm->rtm_protocol = nl_get_rtm_protocol(rnd->rnd_nhop);
320 	rtm->rtm_type = get_rtm_type(rnd->rnd_nhop);
321 
322 	nlattr_add_u32(nw, NL_RTA_TABLE, fibnum);
323 
324 	int plen = 0;
325 #if defined(INET) || defined(INET6)
326 	uint32_t scopeid;
327 #endif
328 	switch (family) {
329 #ifdef INET
330 	case AF_INET:
331 		{
332 			struct in_addr addr;
333 			rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
334 			nlattr_add(nw, NL_RTA_DST, 4, &addr);
335 			break;
336 		}
337 #endif
338 #ifdef INET6
339 	case AF_INET6:
340 		{
341 			struct in6_addr addr;
342 			rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid);
343 			nlattr_add(nw, NL_RTA_DST, 16, &addr);
344 			break;
345 		}
346 #endif
347 	default:
348 		FIB_LOG(LOG_NOTICE, fibnum, family, "unsupported rt family: %d", family);
349 		error = EAFNOSUPPORT;
350 		goto flush;
351 	}
352 
353 	rtm = nlattr_restore_offset(nw, rtm_off, struct rtmsg);
354 	if (plen > 0)
355 		rtm->rtm_dst_len = plen;
356 	if (NH_IS_NHGRP(rnd->rnd_nhop))
357 		dump_rc_nhg(nw, rnd, rtm);
358 	else
359 		dump_rc_nhop(nw, rnd, rtm);
360 
361 	if (nlmsg_end(nw))
362 		return (0);
363 enomem:
364 	error = ENOMEM;
365 flush:
366 	nlmsg_abort(nw);
367 	return (error);
368 }
369 
370 static int
family_to_group(int family)371 family_to_group(int family)
372 {
373 	switch (family) {
374 	case AF_INET:
375 		return (RTNLGRP_IPV4_ROUTE);
376 	case AF_INET6:
377 		return (RTNLGRP_IPV6_ROUTE);
378 	}
379 	return (0);
380 }
381 
382 static void
report_operation(uint32_t fibnum,struct rib_cmd_info * rc,struct nlpcb * nlp,struct nlmsghdr * hdr)383 report_operation(uint32_t fibnum, struct rib_cmd_info *rc,
384     struct nlpcb *nlp, struct nlmsghdr *hdr)
385 {
386 	struct nl_writer nw;
387 	uint32_t group_id = family_to_group(rt_get_family(rc->rc_rt));
388 
389 	if (nl_writer_group(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id, 0,
390 	    false)) {
391 		struct route_nhop_data rnd = {
392 			.rnd_nhop = rc_get_nhop(rc),
393 			.rnd_weight = rc->rc_nh_weight,
394 		};
395 		hdr->nlmsg_flags &= ~(NLM_F_REPLACE | NLM_F_CREATE);
396 		hdr->nlmsg_flags &= ~(NLM_F_EXCL | NLM_F_APPEND);
397 		switch (rc->rc_cmd) {
398 		case RTM_ADD:
399 			hdr->nlmsg_type = NL_RTM_NEWROUTE;
400 			hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
401 			break;
402 		case RTM_CHANGE:
403 			hdr->nlmsg_type = NL_RTM_NEWROUTE;
404 			hdr->nlmsg_flags |= NLM_F_REPLACE;
405 			break;
406 		case RTM_DELETE:
407 			hdr->nlmsg_type = NL_RTM_DELROUTE;
408 			break;
409 		}
410 		dump_px(fibnum, hdr, rc->rc_rt, &rnd, &nw);
411 		nlmsg_flush(&nw);
412 	}
413 
414 	rtsock_callback_p->route_f(fibnum, rc);
415 }
416 
417 static void
set_scope6(struct sockaddr * sa,struct ifnet * ifp)418 set_scope6(struct sockaddr *sa, struct ifnet *ifp)
419 {
420 #ifdef INET6
421 	if (sa != NULL && sa->sa_family == AF_INET6 && ifp != NULL) {
422 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
423 
424 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
425 			in6_set_unicast_scopeid(&sa6->sin6_addr, if_getindex(ifp));
426 	}
427 #endif
428 }
429 
430 struct rta_mpath_nh {
431 	struct sockaddr	*gw;
432 	struct ifnet	*ifp;
433 	uint8_t		rtnh_flags;
434 	uint8_t		rtnh_weight;
435 };
436 
437 #define	_IN(_field)	offsetof(struct rtnexthop, _field)
438 #define	_OUT(_field)	offsetof(struct rta_mpath_nh, _field)
439 const static struct nlattr_parser nla_p_rtnh[] = {
440 	{ .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = nlattr_get_ip },
441 	{ .type = NL_RTA_VIA, .off = _OUT(gw), .cb = nlattr_get_ipvia },
442 };
443 const static struct nlfield_parser nlf_p_rtnh[] = {
444 	{ .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = nlf_get_u8 },
445 	{ .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = nlf_get_u8 },
446 	{ .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifp), .cb = nlf_get_ifpz },
447 };
448 #undef _IN
449 #undef _OUT
450 
451 static bool
post_p_rtnh(void * _attrs,struct nl_pstate * npt __unused)452 post_p_rtnh(void *_attrs, struct nl_pstate *npt __unused)
453 {
454 	struct rta_mpath_nh *attrs = (struct rta_mpath_nh *)_attrs;
455 
456 	set_scope6(attrs->gw, attrs->ifp);
457 	return (true);
458 }
459 NL_DECLARE_PARSER_EXT(mpath_parser, struct rtnexthop, NULL, nlf_p_rtnh, nla_p_rtnh, post_p_rtnh);
460 
461 struct rta_mpath {
462 	u_int num_nhops;
463 	struct rta_mpath_nh nhops[0];
464 };
465 
466 static int
nlattr_get_multipath(struct nlattr * nla,struct nl_pstate * npt,const void * arg,void * target)467 nlattr_get_multipath(struct nlattr *nla, struct nl_pstate *npt,
468     const void *arg, void *target)
469 {
470 	struct rta_mpath *mp;
471 	struct rtnexthop *rtnh;
472 	uint16_t data_len, len;
473 	u_int max_nhops;
474 	int error;
475 
476 	data_len = nla->nla_len - sizeof(struct nlattr);
477 	max_nhops = data_len / sizeof(struct rtnexthop);
478 
479 	mp = npt_alloc(npt, (max_nhops + 2) * sizeof(struct rta_mpath_nh));
480 	if (mp == NULL) {
481 		NLMSG_REPORT_ERR_MSG(npt, "%s: too many RTA_MULTIPATH", __func__);
482 		return (ENOMEM);
483 	}
484 	mp->num_nhops = 0;
485 
486 	for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) {
487 		struct rta_mpath_nh *mpnh;
488 
489 		len = NL_ITEM_ALIGN(rtnh->rtnh_len);
490 		if (__predict_false(rtnh->rtnh_len <= sizeof(*rtnh) ||
491 		    len < rtnh->rtnh_len || len > data_len)) {
492 			NLMSG_REPORT_ERR_MSG(npt, "%s: bad length %u",
493 			    __func__, rtnh->rtnh_len);
494 			return (EINVAL);
495 		}
496 		mpnh = &mp->nhops[mp->num_nhops++];
497 		error = nl_parse_header(rtnh, rtnh->rtnh_len, &mpath_parser,
498 		    npt, mpnh);
499 		if (error != 0) {
500 			NLMSG_REPORT_ERR_MSG(npt,
501 			    "RTA_MULTIPATH: nexthop %u: parse failed",
502 			    mp->num_nhops - 1);
503 			return (error);
504 		}
505 		data_len -= len;
506 		rtnh = (struct rtnexthop *)((char *)rtnh + len);
507 	}
508 	if (data_len != 0 || mp->num_nhops == 0) {
509 		NLMSG_REPORT_ERR_MSG(npt, "invalid RTA_MULTIPATH attr");
510 		return (EINVAL);
511 	}
512 
513 	*((struct rta_mpath **)target) = mp;
514 	return (0);
515 }
516 
517 
518 struct nl_parsed_route {
519 	struct sockaddr		*rta_dst;
520 	struct sockaddr		*rta_gw;
521 	struct ifnet		*rta_oif;
522 	struct rta_mpath	*rta_multipath;
523 	uint32_t		rta_table;
524 	uint32_t		rta_rtflags;
525 	uint32_t		rta_nh_id;
526 	uint32_t		rta_metric;
527 	uint32_t		rta_weight;
528 	uint32_t		rta_expire;
529 	uint32_t		rtax_mtu;
530 	uint8_t			rtm_table;
531 	uint8_t			rtm_family;
532 	uint8_t			rtm_dst_len;
533 	uint8_t			rtm_protocol;
534 	uint8_t			rtm_type;
535 	uint32_t		rtm_flags;
536 };
537 
538 #define	_IN(_field)	offsetof(struct rtmsg, _field)
539 #define	_OUT(_field)	offsetof(struct nl_parsed_route, _field)
540 static struct nlattr_parser nla_p_rtmetrics[] = {
541 	{ .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = nlattr_get_uint32 },
542 };
543 NL_DECLARE_ATTR_PARSER(metrics_parser, nla_p_rtmetrics);
544 
545 static const struct nlattr_parser nla_p_rtmsg[] = {
546 	{ .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = nlattr_get_ip },
547 	{ .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = nlattr_get_ifp },
548 	{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = nlattr_get_ip },
549 	{ .type = NL_RTA_PRIORITY, .off = _OUT(rta_metric), .cb = nlattr_get_uint32 },
550 	{ .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = nlattr_get_nested },
551 	{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
552 	{ .type = NL_RTA_WEIGHT, .off = _OUT(rta_weight), .cb = nlattr_get_uint32 },
553 	{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = nlattr_get_uint32 },
554 	{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = nlattr_get_uint32 },
555 	{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = nlattr_get_ipvia },
556 	{ .type = NL_RTA_EXPIRES, .off = _OUT(rta_expire), .cb = nlattr_get_uint32 },
557 	{ .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = nlattr_get_uint32 },
558 };
559 
560 static const struct nlfield_parser nlf_p_rtmsg[] = {
561 	{ .off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = nlf_get_u8 },
562 	{ .off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = nlf_get_u8 },
563 	{ .off_in = _IN(rtm_protocol), .off_out = _OUT(rtm_protocol), .cb = nlf_get_u8 },
564 	{ .off_in = _IN(rtm_type), .off_out = _OUT(rtm_type), .cb = nlf_get_u8 },
565 	{ .off_in = _IN(rtm_table), .off_out = _OUT(rtm_table), .cb = nlf_get_u8 },
566 	{ .off_in = _IN(rtm_flags), .off_out = _OUT(rtm_flags), .cb = nlf_get_u32 },
567 };
568 #undef _IN
569 #undef _OUT
570 
571 static bool
post_p_rtmsg(void * _attrs,struct nl_pstate * npt __unused)572 post_p_rtmsg(void *_attrs, struct nl_pstate *npt __unused)
573 {
574 	struct nl_parsed_route *attrs = (struct nl_parsed_route *)_attrs;
575 
576 	set_scope6(attrs->rta_dst, attrs->rta_oif);
577 	set_scope6(attrs->rta_gw, attrs->rta_oif);
578 	return (true);
579 }
580 NL_DECLARE_PARSER_EXT(rtm_parser, struct rtmsg, NULL, nlf_p_rtmsg, nla_p_rtmsg, post_p_rtmsg);
581 
582 struct netlink_walkargs {
583 	struct nl_writer *nw;
584 	struct route_nhop_data rnd;
585 	struct nlmsghdr hdr;
586 	struct nlpcb *nlp;
587 	uint32_t fibnum;
588 	int family;
589 	int error;
590 	int count;
591 	int dumped;
592 	int dumped_tables;
593 };
594 
595 static int
dump_rtentry(struct rtentry * rt,void * _arg)596 dump_rtentry(struct rtentry *rt, void *_arg)
597 {
598 	struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
599 	int error;
600 
601 	wa->count++;
602 	if (wa->error != 0)
603 		return (0);
604 	if (!rt_is_exportable(rt, nlp_get_cred(wa->nlp)))
605 		return (0);
606 	wa->dumped++;
607 
608 	rt_get_rnd(rt, &wa->rnd);
609 
610 	error = dump_px(wa->fibnum, &wa->hdr, rt, &wa->rnd, wa->nw);
611 
612 	IF_DEBUG_LEVEL(LOG_DEBUG3) {
613 		char rtbuf[INET6_ADDRSTRLEN + 5];
614 		FIB_LOG(LOG_DEBUG3, wa->fibnum, wa->family,
615 		    "Dump %s, error %d",
616 		    rt_print_buf(rt, rtbuf, sizeof(rtbuf)), error);
617 	}
618 	wa->error = error;
619 
620 	return (0);
621 }
622 
623 static void
dump_rtable_one(struct netlink_walkargs * wa,uint32_t fibnum,int family)624 dump_rtable_one(struct netlink_walkargs *wa, uint32_t fibnum, int family)
625 {
626 	FIB_LOG(LOG_DEBUG2, fibnum, family, "Start dump");
627 	wa->count = 0;
628 	wa->dumped = 0;
629 
630 	rib_walk(fibnum, family, false, dump_rtentry, wa);
631 
632 	wa->dumped_tables++;
633 
634 	FIB_LOG(LOG_DEBUG2, fibnum, family, "End dump, iterated %d dumped %d",
635 	    wa->count, wa->dumped);
636 }
637 
638 static int
dump_rtable_fib(struct netlink_walkargs * wa,uint32_t fibnum,int family)639 dump_rtable_fib(struct netlink_walkargs *wa, uint32_t fibnum, int family)
640 {
641 	wa->fibnum = fibnum;
642 
643 	if (family == AF_UNSPEC) {
644 		for (int i = 0; i < AF_MAX; i++) {
645 			if (rt_tables_get_rnh(fibnum, i) != 0) {
646 				wa->family = i;
647 				dump_rtable_one(wa, fibnum, i);
648 				if (wa->error != 0)
649 					break;
650 			}
651 		}
652 	} else {
653 		if (rt_tables_get_rnh(fibnum, family) != 0) {
654 			wa->family = family;
655 			dump_rtable_one(wa, fibnum, family);
656 		}
657 	}
658 
659 	return (wa->error);
660 }
661 
662 static int
handle_rtm_getroute(struct nlpcb * nlp,struct nl_parsed_route * attrs,struct nlmsghdr * hdr,struct nl_pstate * npt)663 handle_rtm_getroute(struct nlpcb *nlp, struct nl_parsed_route *attrs,
664     struct nlmsghdr *hdr, struct nl_pstate *npt)
665 {
666 	RIB_RLOCK_TRACKER;
667 	struct rib_head *rnh;
668 	const struct rtentry *rt;
669 	struct route_nhop_data rnd;
670 	uint32_t fibnum = attrs->rta_table;
671 	sa_family_t family = attrs->rtm_family;
672 
673 	if (attrs->rta_dst == NULL) {
674 		NLMSG_REPORT_ERR_MSG(npt, "No RTA_DST supplied");
675 			return (EINVAL);
676 	}
677 
678 	rnh = rt_tables_get_rnh(fibnum, family);
679 	if (rnh == NULL)
680 		return (EAFNOSUPPORT);
681 
682 	RIB_RLOCK(rnh);
683 
684 	struct sockaddr *dst = attrs->rta_dst;
685 
686 	if (attrs->rtm_flags & RTM_F_PREFIX)
687 		rt = rib_lookup_prefix_plen(rnh, dst, attrs->rtm_dst_len, &rnd);
688 	else
689 		rt = (const struct rtentry *)rnh->rnh_matchaddr(dst, &rnh->head);
690 	if (rt == NULL) {
691 		RIB_RUNLOCK(rnh);
692 		return (ESRCH);
693 	}
694 
695 	rt_get_rnd(rt, &rnd);
696 
697 	RIB_RUNLOCK(rnh);
698 
699 	if (!rt_is_exportable(rt, nlp_get_cred(nlp)))
700 		return (ESRCH);
701 
702 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
703 		char rtbuf[NHOP_PRINT_BUFSIZE] __unused, nhbuf[NHOP_PRINT_BUFSIZE] __unused;
704 		FIB_LOG(LOG_DEBUG2, fibnum, family, "getroute completed: got %s for %s",
705 		    nhop_print_buf_any(rnd.rnd_nhop, nhbuf, sizeof(nhbuf)),
706 		    rt_print_buf(rt, rtbuf, sizeof(rtbuf)));
707 	}
708 
709 	hdr->nlmsg_type = NL_RTM_NEWROUTE;
710 	dump_px(fibnum, hdr, rt, &rnd, npt->nw);
711 
712 	return (0);
713 }
714 
715 static int
handle_rtm_dump(struct nlpcb * nlp,uint32_t fibnum,int family,struct nlmsghdr * hdr,struct nl_writer * nw)716 handle_rtm_dump(struct nlpcb *nlp, uint32_t fibnum, int family,
717     struct nlmsghdr *hdr, struct nl_writer *nw)
718 {
719 	struct netlink_walkargs wa = {
720 		.nlp = nlp,
721 		.nw = nw,
722 		.hdr.nlmsg_pid = hdr->nlmsg_pid,
723 		.hdr.nlmsg_seq = hdr->nlmsg_seq,
724 		.hdr.nlmsg_type = NL_RTM_NEWROUTE,
725 		.hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
726 	};
727 
728 	if (fibnum == RT_TABLE_UNSPEC) {
729 		for (int i = 0; i < V_rt_numfibs; i++) {
730 			dump_rtable_fib(&wa, i, family);
731 			if (wa.error != 0)
732 				break;
733 		}
734 	} else
735 		dump_rtable_fib(&wa, fibnum, family);
736 
737 	if (wa.error == 0 && wa.dumped_tables == 0) {
738 		FIB_LOG(LOG_DEBUG, fibnum, family, "incorrect fibnum/family");
739 		wa.error = ESRCH;
740 		// How do we propagate it?
741 	}
742 
743 	if (!nlmsg_end_dump(wa.nw, wa.error, &wa.hdr)) {
744                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
745                 return (ENOMEM);
746         }
747 
748 	return (wa.error);
749 }
750 
751 static struct nhop_object *
finalize_nhop(struct nhop_object * nh,const struct sockaddr * dst,int * perror)752 finalize_nhop(struct nhop_object *nh, const struct sockaddr *dst, int *perror)
753 {
754 	/*
755 	 * The following MUST be filled:
756 	 *  nh_ifp, nh_ifa, nh_gw
757 	 */
758 	if (nh->gw_sa.sa_family == 0) {
759 		/*
760 		 * Empty gateway. Can be direct route with RTA_OIF set.
761 		 */
762 		if (nh->nh_ifp != NULL)
763 			nhop_set_direct_gw(nh, nh->nh_ifp);
764 		else {
765 			NL_LOG(LOG_DEBUG, "empty gateway and interface, skipping");
766 			*perror = EINVAL;
767 			return (NULL);
768 		}
769 		/* Both nh_ifp and gateway are set */
770 	} else {
771 		/* Gateway is set up, we can derive ifp if not set */
772 		if (nh->nh_ifp == NULL) {
773 			uint32_t fibnum = nhop_get_fibnum(nh);
774 			uint32_t flags = 0;
775 
776 			if (nh->nh_flags & NHF_GATEWAY)
777 				flags = RTF_GATEWAY;
778 			else if (nh->nh_flags & NHF_HOST)
779 				flags = RTF_HOST;
780 
781 			struct ifaddr *ifa = ifa_ifwithroute(flags, dst, &nh->gw_sa, fibnum);
782 			if (ifa == NULL) {
783 				NL_LOG(LOG_DEBUG, "Unable to determine ifp, skipping");
784 				*perror = EINVAL;
785 				return (NULL);
786 			}
787 			nhop_set_transmit_ifp(nh, ifa->ifa_ifp);
788 		}
789 	}
790 	/* Both nh_ifp and gateway are set */
791 	if (nh->nh_ifa == NULL) {
792 		const struct sockaddr *gw_sa = &nh->gw_sa;
793 
794 		if (gw_sa->sa_family != dst->sa_family) {
795 			/*
796 			 * Use dst as the target for determining the default
797 			 * preferred ifa IF
798 			 * 1) the gateway is link-level (e.g. direct route)
799 			 * 2) the gateway family is different (e.g. IPv4 over IPv6).
800 			 */
801 			gw_sa = dst;
802 		}
803 
804 		struct ifaddr *ifa = ifaof_ifpforaddr(gw_sa, nh->nh_ifp);
805 		if (ifa == NULL) {
806 			/* Try link-level ifa. */
807 			gw_sa = &nh->gw_sa;
808 			ifa = ifaof_ifpforaddr(gw_sa, nh->nh_ifp);
809 			if (ifa == NULL) {
810 				NL_LOG(LOG_DEBUG, "Unable to determine ifa, skipping");
811 				*perror = EINVAL;
812 				return (NULL);
813 			}
814 		}
815 		nhop_set_src(nh, ifa);
816 	}
817 
818 	return (nhop_get_nhop(nh, perror));
819 }
820 
821 static int
get_pxflag(const struct nl_parsed_route * attrs)822 get_pxflag(const struct nl_parsed_route *attrs)
823 {
824 	int pxflag = 0;
825 	switch (attrs->rtm_family) {
826 	case AF_INET:
827 		if (attrs->rtm_dst_len == 32)
828 			pxflag = NHF_HOST;
829 		else if (attrs->rtm_dst_len == 0)
830 			pxflag = NHF_DEFAULT;
831 		break;
832 	case AF_INET6:
833 		if (attrs->rtm_dst_len == 128)
834 			pxflag = NHF_HOST;
835 		else if (attrs->rtm_dst_len == 0)
836 			pxflag = NHF_DEFAULT;
837 		break;
838 	}
839 
840 	return (pxflag);
841 }
842 
843 static int
get_op_flags(int nlm_flags)844 get_op_flags(int nlm_flags)
845 {
846 	int op_flags = 0;
847 
848 	op_flags |= (nlm_flags & NLM_F_REPLACE) ? RTM_F_REPLACE : 0;
849 	op_flags |= (nlm_flags & NLM_F_EXCL) ? RTM_F_EXCL : 0;
850 	op_flags |= (nlm_flags & NLM_F_CREATE) ? RTM_F_CREATE : 0;
851 	op_flags |= (nlm_flags & NLM_F_APPEND) ? RTM_F_APPEND : 0;
852 
853 	return (op_flags);
854 }
855 
856 static int
create_nexthop_one(struct nl_parsed_route * attrs,struct rta_mpath_nh * mpnh,struct nl_pstate * npt,struct nhop_object ** pnh)857 create_nexthop_one(struct nl_parsed_route *attrs, struct rta_mpath_nh *mpnh,
858     struct nl_pstate *npt, struct nhop_object **pnh)
859 {
860 	int error;
861 
862 	if (mpnh->gw == NULL)
863 		return (EINVAL);
864 
865 	struct nhop_object *nh = nhop_alloc(attrs->rta_table, attrs->rtm_family);
866 	if (nh == NULL)
867 		return (ENOMEM);
868 
869 	error = nl_set_nexthop_gw(nh, mpnh->gw, mpnh->ifp, npt);
870 	if (error != 0) {
871 		nhop_free(nh);
872 		return (error);
873 	}
874 	if (mpnh->ifp != NULL)
875 		nhop_set_transmit_ifp(nh, mpnh->ifp);
876 	nhop_set_pxtype_flag(nh, get_pxflag(attrs));
877 	nhop_set_rtflags(nh, attrs->rta_rtflags);
878 	nhop_set_metric(nh, attrs->rta_metric);
879 	if (attrs->rtm_protocol > RTPROT_STATIC)
880 		nhop_set_origin(nh, attrs->rtm_protocol);
881 
882 	*pnh = finalize_nhop(nh, attrs->rta_dst, &error);
883 
884 	return (error);
885 }
886 
887 static struct nhop_object *
create_nexthop_from_attrs(struct nl_parsed_route * attrs,struct nl_pstate * npt,int * perror)888 create_nexthop_from_attrs(struct nl_parsed_route *attrs,
889     struct nl_pstate *npt, int *perror)
890 {
891 	struct nhop_object *nh = NULL;
892 	int error = 0;
893 	uint32_t nh_expire = 0;
894 
895 	if (attrs->rta_multipath != NULL) {
896 		/* Multipath w/o explicit nexthops */
897 		int num_nhops = attrs->rta_multipath->num_nhops;
898 		struct weightened_nhop *wn = npt_alloc(npt, sizeof(*wn) * num_nhops);
899 
900 		if (wn == NULL) {
901 			*perror = ENOMEM;
902 			return (NULL);
903 		}
904 		for (int i = 0; i < num_nhops; i++) {
905 			struct rta_mpath_nh *mpnh = &attrs->rta_multipath->nhops[i];
906 
907 			error = create_nexthop_one(attrs, mpnh, npt, &wn[i].nh);
908 			if (error != 0) {
909 				for (int j = 0; j < i; j++)
910 					nhop_free(wn[j].nh);
911 				break;
912 			}
913 			wn[i].weight = mpnh->rtnh_weight > 0 ? mpnh->rtnh_weight : 1;
914 		}
915 		if (error == 0) {
916 			struct rib_head *rh = nhop_get_rh(wn[0].nh);
917 			struct nhgrp_object *nhg;
918 
919 			nhg = nhgrp_alloc(rh->rib_fibnum, rh->rib_family,
920 			    wn, num_nhops, perror);
921 			if (nhg != NULL) {
922 				if (attrs->rtm_protocol > RTPROT_STATIC)
923 					nhgrp_set_origin(nhg, attrs->rtm_protocol);
924 				nhg = nhgrp_get_nhgrp(nhg, perror);
925 			}
926 			for (int i = 0; i < num_nhops; i++)
927 				nhop_free(wn[i].nh);
928 			if (nhg != NULL)
929 				return ((struct nhop_object *)nhg);
930 			error = *perror;
931 		}
932 		*perror = error;
933 	} else {
934 		nh = nhop_alloc(attrs->rta_table, attrs->rtm_family);
935 		if (nh == NULL) {
936 			*perror = ENOMEM;
937 			return (NULL);
938 		}
939 		if (attrs->rta_gw != NULL) {
940 			*perror = nl_set_nexthop_gw(nh, attrs->rta_gw, attrs->rta_oif, npt);
941 			if (*perror != 0) {
942 				nhop_free(nh);
943 				return (NULL);
944 			}
945 		}
946 		if (attrs->rta_oif != NULL)
947 			nhop_set_transmit_ifp(nh, attrs->rta_oif);
948 		if (attrs->rtax_mtu != 0)
949 			nhop_set_mtu(nh, attrs->rtax_mtu, true);
950 		if (attrs->rta_expire > 0) {
951 			nh_expire = attrs->rta_expire - time_second + time_uptime;
952 			nhop_set_expire(nh, nh_expire);
953 		}
954 		if (attrs->rta_rtflags & RTF_BROADCAST)
955 			nhop_set_broadcast(nh, true);
956 		if (attrs->rtm_protocol > RTPROT_STATIC)
957 			nhop_set_origin(nh, attrs->rtm_protocol);
958 		nhop_set_metric(nh, attrs->rta_metric);
959 		nhop_set_pxtype_flag(nh, get_pxflag(attrs));
960 		nhop_set_rtflags(nh, attrs->rta_rtflags);
961 
962 		switch (attrs->rtm_type) {
963 		case RTN_UNICAST:
964 			break;
965 		case RTN_BLACKHOLE:
966 			nhop_set_blackhole(nh, RTF_BLACKHOLE);
967 			break;
968 		case RTN_PROHIBIT:
969 		case RTN_UNREACHABLE:
970 			nhop_set_blackhole(nh, RTF_REJECT);
971 			break;
972 		/* TODO: return ENOTSUP for other types if strict option is set */
973 		}
974 
975 		nh = finalize_nhop(nh, attrs->rta_dst, perror);
976 	}
977 
978 	return (nh);
979 }
980 
981 /* pre-2.6.19 Linux API compatibility: prefer RTA_TABLE, fall back to rtm_table */
982 static inline void
old_linux_compat(struct nl_parsed_route * attrs)983 old_linux_compat(struct nl_parsed_route *attrs)
984 {
985 	if (attrs->rtm_table > 0 && attrs->rta_table == 0)
986 		attrs->rta_table = attrs->rtm_table;
987 }
988 
989 static int
rtnl_handle_newroute(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)990 rtnl_handle_newroute(struct nlmsghdr *hdr, struct nlpcb *nlp,
991     struct nl_pstate *npt)
992 {
993 	struct rib_cmd_info rc = {};
994 	struct nhop_object *nh = NULL;
995 	int error;
996 
997 	struct nl_parsed_route attrs = {};
998 	error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
999 	if (error != 0)
1000 		return (error);
1001 
1002 	/* Check if we have enough data */
1003 	if (attrs.rta_dst == NULL) {
1004 		NL_LOG(LOG_DEBUG, "missing RTA_DST");
1005 		return (EINVAL);
1006 	}
1007 
1008 	old_linux_compat(&attrs);
1009 	if (attrs.rta_table >= V_rt_numfibs || attrs.rtm_family > AF_MAX) {
1010 		NLMSG_REPORT_ERR_MSG(npt, "invalid fib");
1011 		return (EINVAL);
1012 	}
1013 
1014 	if (attrs.rta_nh_id != 0) {
1015 		/* Referenced uindex */
1016 		int pxflag = get_pxflag(&attrs);
1017 		nh = nl_find_nhop(attrs.rta_table, attrs.rtm_family, attrs.rta_nh_id,
1018 		    pxflag, &error);
1019 		if (error != 0)
1020 			return (error);
1021 	} else {
1022 		nh = create_nexthop_from_attrs(&attrs, npt, &error);
1023 		if (error != 0) {
1024 			NL_LOG(LOG_DEBUG, "Error creating nexthop");
1025 			return (error);
1026 		}
1027 	}
1028 
1029 	if (!NH_IS_NHGRP(nh) && attrs.rta_weight == 0)
1030 		attrs.rta_weight = RT_DEFAULT_WEIGHT;
1031 	struct route_nhop_data rnd = { .rnd_nhop = nh, .rnd_weight = attrs.rta_weight };
1032 	int op_flags = get_op_flags(hdr->nlmsg_flags);
1033 
1034 	error = rib_add_route_px(attrs.rta_table, attrs.rta_dst, attrs.rtm_dst_len,
1035 	    &rnd, op_flags, &rc);
1036 	if (error == 0)
1037 		report_operation(attrs.rta_table, &rc, nlp, hdr);
1038 	return (error);
1039 }
1040 
1041 static int
path_match_func(const struct rtentry * rt,const struct nhop_object * nh,void * _data)1042 path_match_func(const struct rtentry *rt, const struct nhop_object *nh, void *_data)
1043 {
1044 	struct nl_parsed_route *attrs = (struct nl_parsed_route *)_data;
1045 
1046 	if (attrs->rta_metric != 0 && attrs->rta_metric != nhop_get_metric(nh))
1047 		return (0);
1048 
1049 	if ((attrs->rta_gw != NULL) && !rib_match_gw(rt, nh, attrs->rta_gw))
1050 		return (0);
1051 
1052 	if ((attrs->rta_oif != NULL) && (attrs->rta_oif != nh->nh_ifp))
1053 		return (0);
1054 
1055 	return (1);
1056 }
1057 
1058 static int
rtnl_handle_delroute(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1059 rtnl_handle_delroute(struct nlmsghdr *hdr, struct nlpcb *nlp,
1060     struct nl_pstate *npt)
1061 {
1062 	struct rib_cmd_info rc;
1063 	int error;
1064 
1065 	struct nl_parsed_route attrs = {};
1066 	error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
1067 	if (error != 0)
1068 		return (error);
1069 
1070 	if (attrs.rta_dst == NULL) {
1071 		NLMSG_REPORT_ERR_MSG(npt, "RTA_DST is not set");
1072 		return (ESRCH);
1073 	}
1074 
1075 	old_linux_compat(&attrs);
1076 	if (attrs.rta_table >= V_rt_numfibs || attrs.rtm_family > AF_MAX) {
1077 		NLMSG_REPORT_ERR_MSG(npt, "invalid fib");
1078 		return (EINVAL);
1079 	}
1080 
1081 	error = rib_del_route_px(attrs.rta_table, attrs.rta_dst,
1082 	    attrs.rtm_dst_len, path_match_func, &attrs,
1083 	    (attrs.rta_rtflags & RTF_PINNED) ? RTM_F_FORCE : 0, &rc);
1084 	if (error == 0)
1085 		report_operation(attrs.rta_table, &rc, nlp, hdr);
1086 	return (error);
1087 }
1088 
1089 static int
rtnl_handle_getroute(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1090 rtnl_handle_getroute(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1091 {
1092 	int error;
1093 
1094 	struct nl_parsed_route attrs = {};
1095 	error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
1096 	if (error != 0)
1097 		return (error);
1098 
1099 	old_linux_compat(&attrs);
1100 	if (attrs.rta_table >= V_rt_numfibs || attrs.rtm_family > AF_MAX) {
1101 		NLMSG_REPORT_ERR_MSG(npt, "invalid fib");
1102 		return (EINVAL);
1103 	}
1104 
1105 	if (hdr->nlmsg_flags & NLM_F_DUMP)
1106 		error = handle_rtm_dump(nlp, attrs.rta_table, attrs.rtm_family, hdr, npt->nw);
1107 	else
1108 		error = handle_rtm_getroute(nlp, &attrs, hdr, npt);
1109 
1110 	return (error);
1111 }
1112 
1113 void
rtnl_handle_route_event(uint32_t fibnum,const struct rib_cmd_info * rc)1114 rtnl_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
1115 {
1116 	struct nl_writer nw;
1117 	int family, nlm_flags = 0;
1118 
1119 	family = rt_get_family(rc->rc_rt);
1120 
1121 	/* XXX: check if there are active listeners first */
1122 
1123 	/* TODO: consider passing PID/type/seq */
1124 	switch (rc->rc_cmd) {
1125 	case RTM_ADD:
1126 		nlm_flags = NLM_F_EXCL | NLM_F_CREATE;
1127 		break;
1128 	case RTM_CHANGE:
1129 		nlm_flags = NLM_F_REPLACE;
1130 		break;
1131 	case RTM_DELETE:
1132 		nlm_flags = 0;
1133 		break;
1134 	}
1135 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
1136 		char rtbuf[NHOP_PRINT_BUFSIZE] __unused;
1137 		FIB_LOG(LOG_DEBUG2, fibnum, family,
1138 		    "received event %s for %s / nlm_flags=%X",
1139 		    rib_print_cmd(rc->rc_cmd),
1140 		    rt_print_buf(rc->rc_rt, rtbuf, sizeof(rtbuf)),
1141 		    nlm_flags);
1142 	}
1143 
1144 	struct nlmsghdr hdr = {
1145 		.nlmsg_flags = nlm_flags,
1146 		.nlmsg_type = get_rtmsg_type_from_rtsock(rc->rc_cmd),
1147 	};
1148 
1149 	struct route_nhop_data rnd = {
1150 		.rnd_nhop = rc_get_nhop(rc),
1151 		.rnd_weight = rc->rc_nh_weight,
1152 	};
1153 
1154 	uint32_t group_id = family_to_group(family);
1155 	if (!nl_writer_group(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id, 0,
1156 	    false)) {
1157 		NL_LOG(LOG_DEBUG, "error allocating event buffer");
1158 		return;
1159 	}
1160 
1161 	dump_px(fibnum, &hdr, rc->rc_rt, &rnd, &nw);
1162 	nlmsg_flush(&nw);
1163 }
1164 
1165 static const struct rtnl_cmd_handler cmd_handlers[] = {
1166 	{
1167 		.cmd = NL_RTM_GETROUTE,
1168 		.name = "RTM_GETROUTE",
1169 		.cb = &rtnl_handle_getroute,
1170 		.flags = RTNL_F_ALLOW_NONVNET_JAIL,
1171 	},
1172 	{
1173 		.cmd = NL_RTM_DELROUTE,
1174 		.name = "RTM_DELROUTE",
1175 		.cb = &rtnl_handle_delroute,
1176 		.priv = PRIV_NET_ROUTE,
1177 		.flags = RTNL_F_ALLOW_NONVNET_JAIL,
1178 	},
1179 	{
1180 		.cmd = NL_RTM_NEWROUTE,
1181 		.name = "RTM_NEWROUTE",
1182 		.cb = &rtnl_handle_newroute,
1183 		.priv = PRIV_NET_ROUTE,
1184 		.flags = RTNL_F_ALLOW_NONVNET_JAIL,
1185 	}
1186 };
1187 
1188 static const struct nlhdr_parser *all_parsers[] = {&mpath_parser, &metrics_parser, &rtm_parser};
1189 
1190 void
rtnl_routes_init(void)1191 rtnl_routes_init(void)
1192 {
1193 	NL_VERIFY_PARSERS(all_parsers);
1194 	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
1195 }
1196