2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 Ng Peng Nam Sean
5 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
33 #include "opt_route.h"
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/rmlock.h>
37 #include <sys/socket.h>
40 #include <net/route.h>
41 #include <net/route/nhop.h>
42 #include <net/route/route_ctl.h>
43 #include <net/route/route_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>
49 #define DEBUG_MOD_NAME nl_route
50 #define DEBUG_MAX_LEVEL LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_DEBUG);
55 get_rtm_type(const struct nhop_object *nh)
57 int nh_flags = nh->nh_flags;
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);
68 nl_get_rtm_protocol(const struct nhop_object *nh)
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)
79 uint8_t origin = nhop_get_origin(nh);
80 if (origin != RTPROT_UNSPEC)
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);
92 get_rtmsg_type_from_rtsock(int cmd)
98 return NL_RTM_NEWROUTE;
100 return NL_RTM_DELROUTE;
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
117 static struct nhop_object *
118 rc_get_nhop(const struct rib_cmd_info *rc)
120 return ((rc->rc_cmd == RTM_DELETE) ? rc->rc_nh_old : rc->rc_nh_new);
124 dump_rc_nhop_gw(struct nl_writer *nw, const struct nhop_object *nh)
128 switch (nhop_get_neigh_family(nh)) {
130 /* onlink prefix, skip */
133 nlattr_add(nw, NL_RTA_GATEWAY, 4, &nh->gw4_sa.sin_addr);
136 upper_family = nhop_get_upper_family(nh);
137 if (upper_family == AF_INET6) {
138 nlattr_add(nw, NL_RTA_GATEWAY, 16, &nh->gw6_sa.sin6_addr);
139 } else if (upper_family == AF_INET) {
142 struct rtvia *via = (struct rtvia *)&buf[0];
143 via->rtvia_family = AF_INET6;
144 memcpy(via->rtvia_addr, &nh->gw6_sa.sin6_addr, 16);
145 nlattr_add(nw, NL_RTA_VIA, 17, via);
152 dump_rc_nhop_mtu(struct nl_writer *nw, const struct nhop_object *nh)
154 int nla_len = sizeof(struct nlattr) * 2 + sizeof(uint32_t);
155 struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
159 nla->nla_type = NL_RTA_METRICS;
160 nla->nla_len = nla_len;
162 nla->nla_type = NL_RTAX_MTU;
163 nla->nla_len = sizeof(struct nlattr) + sizeof(uint32_t);
164 *((uint32_t *)(nla + 1)) = nh->nh_mtu;
169 dump_rc_nhg(struct nl_writer *nw, const struct nhgrp_object *nhg, struct rtmsg *rtm)
171 uint32_t uidx = nhgrp_get_uidx(nhg);
173 const struct weightened_nhop *wn = nhgrp_get_nhops(nhg, &num_nhops);
174 uint32_t base_rtflags = nhop_get_rtflags(wn[0].nh);
177 nlattr_add_u32(nw, NL_RTA_NH_ID, uidx);
179 nlattr_add_u32(nw, NL_RTA_RTFLAGS, base_rtflags);
180 int off = nlattr_add_nested(nw, NL_RTA_MULTIPATH);
184 for (int i = 0; i < num_nhops; i++) {
185 int nh_off = nlattr_save_offset(nw);
186 struct rtnexthop *rtnh = nlmsg_reserve_object(nw, struct rtnexthop);
189 rtnh->rtnh_flags = 0;
190 rtnh->rtnh_ifindex = wn[i].nh->nh_ifp->if_index;
191 rtnh->rtnh_hops = wn[i].weight;
192 dump_rc_nhop_gw(nw, wn[i].nh);
193 uint32_t rtflags = nhop_get_rtflags(wn[i].nh);
194 if (rtflags != base_rtflags)
195 nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags);
196 if (rtflags & RTF_FIXEDMTU)
197 dump_rc_nhop_mtu(nw, wn[i].nh);
198 rtnh = nlattr_restore_offset(nw, nh_off, struct rtnexthop);
200 * nlattr_add() allocates 4-byte aligned storage, no need to aligh
203 rtnh->rtnh_len = nlattr_save_offset(nw) - nh_off;
205 nlattr_set_len(nw, off);
210 dump_rc_nhop(struct nl_writer *nw, const struct nhop_object *nh, struct rtmsg *rtm)
213 if (NH_IS_NHGRP(nh)) {
214 dump_rc_nhg(nw, (const struct nhgrp_object *)nh, rtm);
218 uint32_t rtflags = nhop_get_rtflags(nh);
222 * ('RTA_VIA', {'family': 10, 'addr': 'fe80::20c:29ff:fe67:2dd'}), ('RTA_OIF', 2),
224 * ('RTA_GATEWAY', '172.16.107.131'), ('RTA_OIF', 2)],
228 if (nh->nh_flags & NHF_GATEWAY)
229 dump_rc_nhop_gw(nw, nh);
231 uint32_t uidx = nhop_get_uidx(nh);
233 nlattr_add_u32(nw, NL_RTA_NH_ID, uidx);
234 nlattr_add_u32(nw, NL_RTA_KNH_ID, nhop_get_idx(nh));
235 nlattr_add_u32(nw, NL_RTA_RTFLAGS, rtflags);
237 if (rtflags & RTF_FIXEDMTU)
238 dump_rc_nhop_mtu(nw, nh);
239 uint32_t nh_expire = nhop_get_expire(nh);
241 nlattr_add_u32(nw, NL_RTA_EXPIRES, nh_expire - time_uptime);
243 /* In any case, fill outgoing interface */
244 nlattr_add_u32(nw, NL_RTA_OIF, nh->nh_ifp->if_index);
248 * Dumps output from a rib command into an rtmsg
252 dump_px(uint32_t fibnum, const struct nlmsghdr *hdr,
253 const struct rtentry *rt, struct route_nhop_data *rnd,
254 struct nl_writer *nw)
261 if (!nlmsg_reply(nw, hdr, sizeof(struct rtmsg)))
264 int family = rt_get_family(rt);
265 int rtm_off = nlattr_save_offset(nw);
266 rtm = nlmsg_reserve_object(nw, struct rtmsg);
267 rtm->rtm_family = family;
268 rtm->rtm_dst_len = 0;
269 rtm->rtm_src_len = 0;
272 rtm->rtm_table = (unsigned char)fibnum;
273 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
274 if (!NH_IS_NHGRP(rnd->rnd_nhop)) {
275 rtm->rtm_protocol = nl_get_rtm_protocol(rnd->rnd_nhop);
276 rtm->rtm_type = get_rtm_type(rnd->rnd_nhop);
278 rtm->rtm_protocol = RTPROT_UNSPEC; /* TODO: protocol from nhg? */
279 rtm->rtm_type = RTN_UNICAST;
282 nlattr_add_u32(nw, NL_RTA_TABLE, fibnum);
285 #if defined(INET) || defined(INET6)
293 rt_get_inet_prefix_plen(rt, &addr, &plen, &scopeid);
294 nlattr_add(nw, NL_RTA_DST, 4, &addr);
301 struct in6_addr addr;
302 rt_get_inet6_prefix_plen(rt, &addr, &plen, &scopeid);
303 nlattr_add(nw, NL_RTA_DST, 16, &addr);
308 FIB_LOG(LOG_NOTICE, fibnum, family, "unsupported rt family: %d", family);
309 error = EAFNOSUPPORT;
313 rtm = nlattr_restore_offset(nw, rtm_off, struct rtmsg);
315 rtm->rtm_dst_len = plen;
316 dump_rc_nhop(nw, rnd->rnd_nhop, rtm);
328 family_to_group(int family)
332 return (RTNLGRP_IPV4_ROUTE);
334 return (RTNLGRP_IPV6_ROUTE);
341 report_operation(uint32_t fibnum, struct rib_cmd_info *rc,
342 struct nlpcb *nlp, struct nlmsghdr *hdr)
346 uint32_t group_id = family_to_group(rt_get_family(rc->rc_rt));
347 if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id)) {
348 struct route_nhop_data rnd = {
349 .rnd_nhop = rc_get_nhop(rc),
350 .rnd_weight = rc->rc_nh_weight,
352 hdr->nlmsg_flags &= ~(NLM_F_REPLACE | NLM_F_CREATE);
353 hdr->nlmsg_flags &= ~(NLM_F_EXCL | NLM_F_APPEND);
354 switch (rc->rc_cmd) {
356 hdr->nlmsg_type = NL_RTM_NEWROUTE;
357 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
360 hdr->nlmsg_type = NL_RTM_NEWROUTE;
361 hdr->nlmsg_flags |= NLM_F_REPLACE;
364 hdr->nlmsg_type = NL_RTM_DELROUTE;
367 dump_px(fibnum, hdr, rc->rc_rt, &rnd, &nw);
371 rtsock_callback_p->route_f(fibnum, rc);
374 struct rta_mpath_nh {
381 #define _IN(_field) offsetof(struct rtnexthop, _field)
382 #define _OUT(_field) offsetof(struct rta_mpath_nh, _field)
383 const static struct nlattr_parser nla_p_rtnh[] = {
384 { .type = NL_RTA_GATEWAY, .off = _OUT(gw), .cb = nlattr_get_ip },
385 { .type = NL_RTA_VIA, .off = _OUT(gw), .cb = nlattr_get_ipvia },
387 const static struct nlfield_parser nlf_p_rtnh[] = {
388 { .off_in = _IN(rtnh_flags), .off_out = _OUT(rtnh_flags), .cb = nlf_get_u8 },
389 { .off_in = _IN(rtnh_hops), .off_out = _OUT(rtnh_weight), .cb = nlf_get_u8 },
390 { .off_in = _IN(rtnh_ifindex), .off_out = _OUT(ifp), .cb = nlf_get_ifpz },
394 NL_DECLARE_PARSER(mpath_parser, struct rtnexthop, nlf_p_rtnh, nla_p_rtnh);
398 struct rta_mpath_nh nhops[0];
402 nlattr_get_multipath(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
404 int data_len = nla->nla_len - sizeof(struct nlattr);
405 struct rtnexthop *rtnh;
407 int max_nhops = data_len / sizeof(struct rtnexthop);
409 struct rta_mpath *mp = npt_alloc(npt, (max_nhops + 2) * sizeof(struct rta_mpath_nh));
412 for (rtnh = (struct rtnexthop *)(nla + 1); data_len > 0; ) {
413 struct rta_mpath_nh *mpnh = &mp->nhops[mp->num_nhops++];
415 int error = nl_parse_header(rtnh, rtnh->rtnh_len, &mpath_parser,
418 NLMSG_REPORT_ERR_MSG(npt, "RTA_MULTIPATH: nexhop %d: parse failed",
423 int len = NL_ITEM_ALIGN(rtnh->rtnh_len);
425 rtnh = (struct rtnexthop *)((char *)rtnh + len);
427 if (data_len != 0 || mp->num_nhops == 0) {
428 NLMSG_REPORT_ERR_MSG(npt, "invalid RTA_MULTIPATH attr");
432 *((struct rta_mpath **)target) = mp;
437 struct nl_parsed_route {
438 struct sockaddr *rta_dst;
439 struct sockaddr *rta_gw;
440 struct ifnet *rta_oif;
441 struct rta_mpath *rta_multipath;
443 uint32_t rta_rtflags;
450 #define _IN(_field) offsetof(struct rtmsg, _field)
451 #define _OUT(_field) offsetof(struct nl_parsed_route, _field)
452 static struct nlattr_parser nla_p_rtmetrics[] = {
453 { .type = NL_RTAX_MTU, .off = _OUT(rtax_mtu), .cb = nlattr_get_uint32 },
455 NL_DECLARE_ATTR_PARSER(metrics_parser, nla_p_rtmetrics);
457 static const struct nlattr_parser nla_p_rtmsg[] = {
458 { .type = NL_RTA_DST, .off = _OUT(rta_dst), .cb = nlattr_get_ip },
459 { .type = NL_RTA_OIF, .off = _OUT(rta_oif), .cb = nlattr_get_ifp },
460 { .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = nlattr_get_ip },
461 { .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = nlattr_get_nested },
462 { .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
463 { .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = nlattr_get_uint32 },
464 { .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = nlattr_get_uint32 },
465 { .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = nlattr_get_ipvia },
466 { .type = NL_RTA_NH_ID, .off = _OUT(rta_nh_id), .cb = nlattr_get_uint32 },
469 static const struct nlfield_parser nlf_p_rtmsg[] = {
470 {.off_in = _IN(rtm_family), .off_out = _OUT(rtm_family), .cb = nlf_get_u8 },
471 {.off_in = _IN(rtm_dst_len), .off_out = _OUT(rtm_dst_len), .cb = nlf_get_u8 },
475 NL_DECLARE_PARSER(rtm_parser, struct rtmsg, nlf_p_rtmsg, nla_p_rtmsg);
477 struct netlink_walkargs {
478 struct nl_writer *nw;
479 struct route_nhop_data rnd;
491 dump_rtentry(struct rtentry *rt, void *_arg)
493 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
501 rt_get_rnd(rt, &wa->rnd);
503 error = dump_px(wa->fibnum, &wa->hdr, rt, &wa->rnd, wa->nw);
505 IF_DEBUG_LEVEL(LOG_DEBUG3) {
506 char rtbuf[INET6_ADDRSTRLEN + 5];
507 FIB_LOG(LOG_DEBUG3, wa->fibnum, wa->family,
508 "Dump %s, offset %u, error %d",
509 rt_print_buf(rt, rtbuf, sizeof(rtbuf)),
510 wa->nw->offset, error);
518 dump_rtable_one(struct netlink_walkargs *wa, uint32_t fibnum, int family)
520 FIB_LOG(LOG_DEBUG2, fibnum, family, "Start dump");
524 rib_walk(fibnum, family, false, dump_rtentry, wa);
528 FIB_LOG(LOG_DEBUG2, fibnum, family, "End dump, iterated %d dumped %d",
529 wa->count, wa->dumped);
530 NL_LOG(LOG_DEBUG2, "Current offset: %d", wa->nw->offset);
534 dump_rtable_fib(struct netlink_walkargs *wa, uint32_t fibnum, int family)
538 if (family == AF_UNSPEC) {
539 for (int i = 0; i < AF_MAX; i++) {
540 if (rt_tables_get_rnh(fibnum, i) != 0) {
542 dump_rtable_one(wa, fibnum, i);
548 if (rt_tables_get_rnh(fibnum, family) != 0) {
550 dump_rtable_one(wa, fibnum, family);
558 handle_rtm_getroute(struct nlpcb *nlp, struct nl_parsed_route *attrs,
559 struct nlmsghdr *hdr, struct nl_pstate *npt)
562 struct rib_head *rnh;
564 uint32_t fibnum = attrs->rta_table;
565 sa_family_t family = attrs->rtm_family;
567 if (attrs->rta_dst == NULL) {
568 NLMSG_REPORT_ERR_MSG(npt, "No RTA_DST supplied");
572 FIB_LOG(LOG_DEBUG, fibnum, family, "getroute called");
574 rnh = rt_tables_get_rnh(fibnum, family);
576 return (EAFNOSUPPORT);
580 rt = (struct rtentry *)rnh->rnh_matchaddr(attrs->rta_dst, &rnh->head);
586 struct route_nhop_data rnd;
587 rt_get_rnd(rt, &rnd);
588 rnd.rnd_nhop = nhop_select_func(rnd.rnd_nhop, 0);
592 IF_DEBUG_LEVEL(LOG_DEBUG2) {
593 char rtbuf[NHOP_PRINT_BUFSIZE] __unused, nhbuf[NHOP_PRINT_BUFSIZE] __unused;
594 FIB_LOG(LOG_DEBUG2, fibnum, family, "getroute completed: got %s for %s",
595 nhop_print_buf_any(rnd.rnd_nhop, nhbuf, sizeof(nhbuf)),
596 rt_print_buf(rt, rtbuf, sizeof(rtbuf)));
599 hdr->nlmsg_type = NL_RTM_NEWROUTE;
600 dump_px(fibnum, hdr, rt, &rnd, npt->nw);
606 handle_rtm_dump(struct nlpcb *nlp, uint32_t fibnum, int family,
607 struct nlmsghdr *hdr, struct nl_writer *nw)
609 struct netlink_walkargs wa = {
612 .hdr.nlmsg_pid = hdr->nlmsg_pid,
613 .hdr.nlmsg_seq = hdr->nlmsg_seq,
614 .hdr.nlmsg_type = NL_RTM_NEWROUTE,
615 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
618 if (fibnum == RT_TABLE_UNSPEC) {
619 for (int i = 0; i < V_rt_numfibs; i++) {
620 dump_rtable_fib(&wa, fibnum, family);
625 dump_rtable_fib(&wa, fibnum, family);
627 if (wa.error == 0 && wa.dumped_tables == 0) {
628 FIB_LOG(LOG_DEBUG, fibnum, family, "incorrect fibnum/family");
630 // How do we propagate it?
633 if (!nlmsg_end_dump(wa.nw, wa.error, &wa.hdr)) {
634 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
641 static struct nhop_object *
642 finalize_nhop(struct nhop_object *nh, int *perror)
645 * The following MUST be filled:
646 * nh_ifp, nh_ifa, nh_gw
648 if (nh->gw_sa.sa_family == 0) {
650 * Empty gateway. Can be direct route with RTA_OIF set.
652 if (nh->nh_ifp != NULL)
653 nhop_set_direct_gw(nh, nh->nh_ifp);
655 NL_LOG(LOG_DEBUG, "empty gateway and interface, skipping");
659 /* Both nh_ifp and gateway are set */
661 /* Gateway is set up, we can derive ifp if not set */
662 if (nh->nh_ifp == NULL) {
663 struct ifaddr *ifa = ifa_ifwithnet(&nh->gw_sa, 1, nhop_get_fibnum(nh));
665 NL_LOG(LOG_DEBUG, "Unable to determine ifp, skipping");
669 nhop_set_transmit_ifp(nh, ifa->ifa_ifp);
672 /* Both nh_ifp and gateway are set */
673 if (nh->nh_ifa == NULL) {
674 struct ifaddr *ifa = ifaof_ifpforaddr(&nh->gw_sa, nh->nh_ifp);
676 NL_LOG(LOG_DEBUG, "Unable to determine ifa, skipping");
680 nhop_set_src(nh, ifa);
683 return (nhop_get_nhop(nh, perror));
687 get_pxflag(const struct nl_parsed_route *attrs)
690 switch (attrs->rtm_family) {
692 if (attrs->rtm_dst_len == 32)
694 else if (attrs->rtm_dst_len == 0)
695 pxflag = NHF_DEFAULT;
698 if (attrs->rtm_dst_len == 32)
700 else if (attrs->rtm_dst_len == 0)
701 pxflag = NHF_DEFAULT;
709 get_op_flags(int nlm_flags)
713 op_flags |= (nlm_flags & NLM_F_REPLACE) ? RTM_F_REPLACE : 0;
714 op_flags |= (nlm_flags & NLM_F_EXCL) ? RTM_F_EXCL : 0;
715 op_flags |= (nlm_flags & NLM_F_CREATE) ? RTM_F_CREATE : 0;
716 op_flags |= (nlm_flags & NLM_F_APPEND) ? RTM_F_APPEND : 0;
723 create_nexthop_one(struct nl_parsed_route *attrs, struct rta_mpath_nh *mpnh,
724 struct nl_pstate *npt, struct nhop_object **pnh)
728 if (mpnh->gw == NULL)
731 struct nhop_object *nh = nhop_alloc(attrs->rta_table, attrs->rtm_family);
735 nhop_set_gw(nh, mpnh->gw, true);
736 if (mpnh->ifp != NULL)
737 nhop_set_transmit_ifp(nh, mpnh->ifp);
738 nhop_set_rtflags(nh, attrs->rta_rtflags);
740 *pnh = finalize_nhop(nh, &error);
746 static struct nhop_object *
747 create_nexthop_from_attrs(struct nl_parsed_route *attrs,
748 struct nl_pstate *npt, int *perror)
750 struct nhop_object *nh = NULL;
753 if (attrs->rta_multipath != NULL) {
755 /* Multipath w/o explicit nexthops */
756 int num_nhops = attrs->rta_multipath->num_nhops;
757 struct weightened_nhop *wn = npt_alloc(npt, sizeof(*wn) * num_nhops);
759 for (int i = 0; i < num_nhops; i++) {
760 struct rta_mpath_nh *mpnh = &attrs->rta_multipath->nhops[i];
762 error = create_nexthop_one(attrs, mpnh, npt, &wn[i].nh);
764 for (int j = 0; j < i; j++)
768 wn[i].weight = mpnh->rtnh_weight > 0 ? mpnh->rtnh_weight : 1;
771 struct rib_head *rh = nhop_get_rh(wn[0].nh);
773 error = nhgrp_get_group(rh, wn, num_nhops, 0,
774 (struct nhgrp_object **)&nh);
776 for (int i = 0; i < num_nhops; i++)
784 nh = nhop_alloc(attrs->rta_table, attrs->rtm_family);
789 if (attrs->rta_gw != NULL)
790 nhop_set_gw(nh, attrs->rta_gw, true);
791 if (attrs->rta_oif != NULL)
792 nhop_set_transmit_ifp(nh, attrs->rta_oif);
793 if (attrs->rtax_mtu != 0)
794 nhop_set_mtu(nh, attrs->rtax_mtu, true);
795 if (attrs->rta_rtflags & RTF_BROADCAST)
796 nhop_set_broadcast(nh, true);
797 if (attrs->rta_rtflags & RTF_BLACKHOLE)
798 nhop_set_blackhole(nh, NHF_BLACKHOLE);
799 if (attrs->rta_rtflags & RTF_REJECT)
800 nhop_set_blackhole(nh, NHF_REJECT);
801 nhop_set_rtflags(nh, attrs->rta_rtflags);
802 nh = finalize_nhop(nh, perror);
809 rtnl_handle_newroute(struct nlmsghdr *hdr, struct nlpcb *nlp,
810 struct nl_pstate *npt)
812 struct rib_cmd_info rc = {};
813 struct nhop_object *nh = NULL;
816 struct nl_parsed_route attrs = {};
817 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
821 /* Check if we have enough data */
822 if (attrs.rta_dst == NULL) {
823 NL_LOG(LOG_DEBUG, "missing RTA_DST");
827 if (attrs.rta_nh_id != 0) {
828 /* Referenced uindex */
829 int pxflag = get_pxflag(&attrs);
830 nh = nl_find_nhop(attrs.rta_table, attrs.rtm_family, attrs.rta_nh_id,
835 nh = create_nexthop_from_attrs(&attrs, npt, &error);
837 NL_LOG(LOG_DEBUG, "Error creating nexthop");
842 int weight = NH_IS_NHGRP(nh) ? 0 : RT_DEFAULT_WEIGHT;
843 struct route_nhop_data rnd = { .rnd_nhop = nh, .rnd_weight = weight };
844 int op_flags = get_op_flags(hdr->nlmsg_flags);
846 error = rib_add_route_px(attrs.rta_table, attrs.rta_dst, attrs.rtm_dst_len,
847 &rnd, op_flags, &rc);
849 report_operation(attrs.rta_table, &rc, nlp, hdr);
854 path_match_func(const struct rtentry *rt, const struct nhop_object *nh, void *_data)
856 struct nl_parsed_route *attrs = (struct nl_parsed_route *)_data;
858 if ((attrs->rta_gw != NULL) && !rib_match_gw(rt, nh, attrs->rta_gw))
861 if ((attrs->rta_oif != NULL) && (attrs->rta_oif != nh->nh_ifp))
868 rtnl_handle_delroute(struct nlmsghdr *hdr, struct nlpcb *nlp,
869 struct nl_pstate *npt)
871 struct rib_cmd_info rc;
874 struct nl_parsed_route attrs = {};
875 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
879 if (attrs.rta_dst == NULL) {
880 NLMSG_REPORT_ERR_MSG(npt, "RTA_DST is not set");
884 error = rib_del_route_px(attrs.rta_table, attrs.rta_dst,
885 attrs.rtm_dst_len, path_match_func, &attrs, 0, &rc);
887 report_operation(attrs.rta_table, &rc, nlp, hdr);
892 rtnl_handle_getroute(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
896 struct nl_parsed_route attrs = {};
897 error = nl_parse_nlmsg(hdr, &rtm_parser, npt, &attrs);
901 if (hdr->nlmsg_flags & NLM_F_DUMP)
902 error = handle_rtm_dump(nlp, attrs.rta_table, attrs.rtm_family, hdr, npt->nw);
904 error = handle_rtm_getroute(nlp, &attrs, hdr, npt);
910 rtnl_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
912 int family, nlm_flags = 0;
916 family = rt_get_family(rc->rc_rt);
918 /* XXX: check if there are active listeners first */
920 /* TODO: consider passing PID/type/seq */
921 switch (rc->rc_cmd) {
923 nlm_flags = NLM_F_EXCL | NLM_F_CREATE;
926 nlm_flags = NLM_F_REPLACE;
932 IF_DEBUG_LEVEL(LOG_DEBUG2) {
933 char rtbuf[NHOP_PRINT_BUFSIZE] __unused;
934 FIB_LOG(LOG_DEBUG2, fibnum, family,
935 "received event %s for %s / nlm_flags=%X",
936 rib_print_cmd(rc->rc_cmd),
937 rt_print_buf(rc->rc_rt, rtbuf, sizeof(rtbuf)),
941 struct nlmsghdr hdr = {
942 .nlmsg_flags = nlm_flags,
943 .nlmsg_type = get_rtmsg_type_from_rtsock(rc->rc_cmd),
946 struct route_nhop_data rnd = {
947 .rnd_nhop = rc_get_nhop(rc),
948 .rnd_weight = rc->rc_nh_weight,
951 uint32_t group_id = family_to_group(family);
952 if (!nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_ROUTE, group_id)) {
953 NL_LOG(LOG_DEBUG, "error allocating event buffer");
957 dump_px(fibnum, &hdr, rc->rc_rt, &rnd, &nw);
961 static const struct rtnl_cmd_handler cmd_handlers[] = {
963 .cmd = NL_RTM_GETROUTE,
964 .name = "RTM_GETROUTE",
965 .cb = &rtnl_handle_getroute,
968 .cmd = NL_RTM_DELROUTE,
969 .name = "RTM_DELROUTE",
970 .cb = &rtnl_handle_delroute,
971 .priv = PRIV_NET_ROUTE,
974 .cmd = NL_RTM_NEWROUTE,
975 .name = "RTM_NEWROUTE",
976 .cb = &rtnl_handle_newroute,
977 .priv = PRIV_NET_ROUTE,
981 static const struct nlhdr_parser *all_parsers[] = {&mpath_parser, &metrics_parser, &rtm_parser};
986 NL_VERIFY_PARSERS(all_parsers);
987 rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));