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