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