1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <net/if_arp.h>
26 #include <linux/lwtunnel.h>
27 #include <linux/mpls_iptunnel.h>
28 #include <linux/neighbour.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/nexthop.h>
32 /* Hack for GNU libc version 2. */
34 #define MSG_TRUNC 0x20
35 #endif /* MSG_TRUNC */
41 #include "connected.h"
44 #include "zebra_memory.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/rt_netlink.h"
67 #include "zebra/zebra_nhg.h"
68 #include "zebra/zebra_mroute.h"
69 #include "zebra/zebra_vxlan.h"
70 #include "zebra/zebra_errors.h"
76 static vlanid_t filter_vlan
= 0;
78 static bool supports_nh
;
86 static const char ipv4_ll_buf
[16] = "169.254.0.1";
87 static struct in_addr ipv4_ll
;
90 * The ipv4_ll data structure is used for all 5549
91 * additions to the kernel. Let's figure out the
92 * correct value one time instead for every
93 * install/remove of a 5549 type route
95 void rt_netlink_init(void)
97 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
101 * Mapping from dataplane neighbor flags to netlink flags
103 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
107 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
108 flags
|= NTF_EXT_LEARNED
;
109 if (dplane_flags
& DPLANE_NTF_ROUTER
)
116 * Mapping from dataplane neighbor state to netlink state
118 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
122 if (dplane_state
& DPLANE_NUD_REACHABLE
)
123 state
|= NUD_REACHABLE
;
124 if (dplane_state
& DPLANE_NUD_STALE
)
126 if (dplane_state
& DPLANE_NUD_NOARP
)
128 if (dplane_state
& DPLANE_NUD_PROBE
)
135 static inline int is_selfroute(int proto
)
137 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
138 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
139 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
140 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
141 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
142 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
143 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
150 static inline int zebra2proto(int proto
)
153 case ZEBRA_ROUTE_BABEL
:
154 proto
= RTPROT_BABEL
;
156 case ZEBRA_ROUTE_BGP
:
159 case ZEBRA_ROUTE_OSPF
:
160 case ZEBRA_ROUTE_OSPF6
:
163 case ZEBRA_ROUTE_STATIC
:
164 proto
= RTPROT_ZSTATIC
;
166 case ZEBRA_ROUTE_ISIS
:
169 case ZEBRA_ROUTE_RIP
:
172 case ZEBRA_ROUTE_RIPNG
:
173 proto
= RTPROT_RIPNG
;
175 case ZEBRA_ROUTE_NHRP
:
178 case ZEBRA_ROUTE_EIGRP
:
179 proto
= RTPROT_EIGRP
;
181 case ZEBRA_ROUTE_LDP
:
184 case ZEBRA_ROUTE_SHARP
:
185 proto
= RTPROT_SHARP
;
187 case ZEBRA_ROUTE_PBR
:
190 case ZEBRA_ROUTE_OPENFABRIC
:
191 proto
= RTPROT_OPENFABRIC
;
193 case ZEBRA_ROUTE_TABLE
:
194 case ZEBRA_ROUTE_NHG
:
195 proto
= RTPROT_ZEBRA
;
199 * When a user adds a new protocol this will show up
200 * to let them know to do something about it. This
201 * is intentionally a warn because we should see
202 * this as part of development of a new protocol
205 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
206 __PRETTY_FUNCTION__
, proto
);
207 proto
= RTPROT_ZEBRA
;
214 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
218 proto
= ZEBRA_ROUTE_BABEL
;
221 proto
= ZEBRA_ROUTE_BGP
;
224 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
228 proto
= ZEBRA_ROUTE_ISIS
;
231 proto
= ZEBRA_ROUTE_RIP
;
234 proto
= ZEBRA_ROUTE_RIPNG
;
237 proto
= ZEBRA_ROUTE_NHRP
;
240 proto
= ZEBRA_ROUTE_EIGRP
;
243 proto
= ZEBRA_ROUTE_LDP
;
247 proto
= ZEBRA_ROUTE_STATIC
;
250 proto
= ZEBRA_ROUTE_SHARP
;
253 proto
= ZEBRA_ROUTE_PBR
;
255 case RTPROT_OPENFABRIC
:
256 proto
= ZEBRA_ROUTE_OPENFABRIC
;
260 proto
= ZEBRA_ROUTE_NHG
;
263 /* Intentional fall thru */
266 * When a user adds a new protocol this will show up
267 * to let them know to do something about it. This
268 * is intentionally a warn because we should see
269 * this as part of development of a new protocol
272 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
273 __PRETTY_FUNCTION__
, proto
);
274 proto
= ZEBRA_ROUTE_KERNEL
;
281 Pending: create an efficient table_id (in a tree/hash) based lookup)
283 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
286 struct zebra_vrf
*zvrf
;
288 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
292 /* case vrf with netns : match the netnsid */
293 if (vrf_is_backend_netns()) {
294 if (ns_id
== zvrf_id(zvrf
))
295 return zvrf_id(zvrf
);
297 /* VRF is VRF_BACKEND_VRF_LITE */
298 if (zvrf
->table_id
!= table_id
)
300 return zvrf_id(zvrf
);
308 * @parse_encap_mpls() - Parses encapsulated mpls attributes
309 * @tb: Pointer to rtattr to look for nested items in.
310 * @labels: Pointer to store labels in.
312 * Return: Number of mpls labels found.
314 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
316 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
317 mpls_lse_t
*lses
= NULL
;
322 mpls_label_t label
= 0;
324 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
325 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
326 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
327 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
328 labels
[num_labels
++] = label
;
334 static struct nexthop
335 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
336 enum blackhole_type bh_type
, int index
, void *prefsrc
,
337 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
339 struct interface
*ifp
= NULL
;
340 struct nexthop nh
= {0};
341 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
344 vrf_id_t nh_vrf_id
= vrf_id
;
345 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
347 if (bh_type
== BLACKHOLE_UNSPEC
) {
349 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
350 else if (index
&& gate
)
351 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
352 : NEXTHOP_TYPE_IPV6_IFINDEX
;
353 else if (!index
&& gate
)
354 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
357 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
358 nh
.bh_type
= bh_type
;
361 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
362 nh
.bh_type
= bh_type
;
366 memcpy(&nh
.src
, prefsrc
, sz
);
368 memcpy(&nh
.gate
, gate
, sz
);
371 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
373 nh_vrf_id
= ifp
->vrf_id
;
375 nh
.vrf_id
= nh_vrf_id
;
377 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
378 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
379 == LWTUNNEL_ENCAP_MPLS
) {
380 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
383 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
384 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
387 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
392 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
393 struct nexthop_group
*ng
,
395 struct rtnexthop
*rtnh
,
397 void *prefsrc
, vrf_id_t vrf_id
)
400 struct interface
*ifp
= NULL
;
403 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
405 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
407 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
408 vrf_id_t nh_vrf_id
= vrf_id
;
411 struct nexthop
*nh
= NULL
;
413 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
416 index
= rtnh
->rtnh_ifindex
;
419 * Yes we are looking this up
420 * for every nexthop and just
421 * using the last one looked
424 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
427 nh_vrf_id
= ifp
->vrf_id
;
430 EC_ZEBRA_UNKNOWN_INTERFACE
,
431 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
432 __PRETTY_FUNCTION__
, index
);
433 nh_vrf_id
= VRF_DEFAULT
;
438 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
439 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
441 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
442 rtnh
->rtnh_len
- sizeof(*rtnh
));
443 if (rtnh_tb
[RTA_GATEWAY
])
444 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
445 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
446 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
447 == LWTUNNEL_ENCAP_MPLS
) {
448 num_labels
= parse_encap_mpls(
449 rtnh_tb
[RTA_ENCAP
], labels
);
453 if (gate
&& rtm
->rtm_family
== AF_INET
) {
455 nh
= nexthop_from_ipv4_ifindex(
456 gate
, prefsrc
, index
, nh_vrf_id
);
458 nh
= nexthop_from_ipv4(gate
, prefsrc
,
460 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
462 nh
= nexthop_from_ipv6_ifindex(
463 gate
, index
, nh_vrf_id
);
465 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
467 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
471 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
474 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
475 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
477 /* Add to temporary list */
478 nexthop_group_add_sorted(ng
, nh
);
481 if (rtnh
->rtnh_len
== 0)
484 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
485 rtnh
= RTNH_NEXT(rtnh
);
488 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
493 /* Looking up routing table by netlink interface. */
494 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
499 struct rtattr
*tb
[RTA_MAX
+ 1];
502 struct prefix_ipv6 src_p
= {};
505 char anyaddr
[16] = {0};
507 int proto
= ZEBRA_ROUTE_KERNEL
;
512 uint8_t distance
= 0;
518 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
519 void *src
= NULL
; /* IPv6 srcdest source prefix */
520 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
524 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
526 switch (rtm
->rtm_type
) {
530 bh_type
= BLACKHOLE_NULL
;
532 case RTN_UNREACHABLE
:
533 bh_type
= BLACKHOLE_REJECT
;
536 bh_type
= BLACKHOLE_ADMINPROHIB
;
539 if (IS_ZEBRA_DEBUG_KERNEL
)
540 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
541 nl_rttype_to_str(rtm
->rtm_type
),
546 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
548 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
549 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
550 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
554 memset(tb
, 0, sizeof tb
);
555 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
557 if (rtm
->rtm_flags
& RTM_F_CLONED
)
559 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
561 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
564 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
565 && h
->nlmsg_type
== RTM_NEWROUTE
) {
566 if (IS_ZEBRA_DEBUG_KERNEL
)
567 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
572 /* We don't care about change notifications for the MPLS table. */
573 /* TODO: Revisit this. */
574 if (rtm
->rtm_family
== AF_MPLS
)
577 /* Table corresponding to route. */
579 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
581 table
= rtm
->rtm_table
;
584 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
585 if (vrf_id
== VRF_DEFAULT
) {
586 if (!is_zebra_valid_kernel_table(table
)
587 && !is_zebra_main_routing_table(table
))
591 /* Route which inserted by Zebra. */
592 if (is_selfroute(rtm
->rtm_protocol
)) {
593 flags
|= ZEBRA_FLAG_SELFROUTE
;
594 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
597 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
600 dest
= RTA_DATA(tb
[RTA_DST
]);
605 src
= RTA_DATA(tb
[RTA_SRC
]);
610 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
613 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
616 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
618 if (tb
[RTA_PRIORITY
])
619 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
621 #if defined(SUPPORT_REALMS)
623 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
626 if (tb
[RTA_METRICS
]) {
627 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
629 memset(mxrta
, 0, sizeof mxrta
);
630 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
631 RTA_PAYLOAD(tb
[RTA_METRICS
]));
634 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
637 if (rtm
->rtm_family
== AF_INET
) {
639 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
641 "Invalid destination prefix length: %u received from kernel route change",
645 memcpy(&p
.u
.prefix4
, dest
, 4);
646 p
.prefixlen
= rtm
->rtm_dst_len
;
648 if (rtm
->rtm_src_len
!= 0) {
649 char buf
[PREFIX_STRLEN
];
651 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
652 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
653 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
657 /* Force debug below to not display anything for source */
659 } else if (rtm
->rtm_family
== AF_INET6
) {
661 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
663 "Invalid destination prefix length: %u received from kernel route change",
667 memcpy(&p
.u
.prefix6
, dest
, 16);
668 p
.prefixlen
= rtm
->rtm_dst_len
;
670 src_p
.family
= AF_INET6
;
671 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
673 "Invalid source prefix length: %u received from kernel route change",
677 memcpy(&src_p
.prefix
, src
, 16);
678 src_p
.prefixlen
= rtm
->rtm_src_len
;
682 * For ZEBRA_ROUTE_KERNEL types:
684 * The metric/priority of the route received from the kernel
685 * is a 32 bit number. We are going to interpret the high
686 * order byte as the Admin Distance and the low order 3 bytes
689 * This will allow us to do two things:
690 * 1) Allow the creation of kernel routes that can be
691 * overridden by zebra.
692 * 2) Allow the old behavior for 'most' kernel route types
693 * if a user enters 'ip route ...' v4 routes get a metric
694 * of 0 and v6 routes get a metric of 1024. Both of these
695 * values will end up with a admin distance of 0, which
696 * will cause them to win for the purposes of zebra.
698 if (proto
== ZEBRA_ROUTE_KERNEL
) {
699 distance
= (metric
>> 24) & 0xFF;
700 metric
= (metric
& 0x00FFFFFF);
703 if (IS_ZEBRA_DEBUG_KERNEL
) {
704 char buf
[PREFIX_STRLEN
];
705 char buf2
[PREFIX_STRLEN
];
706 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
707 nl_msg_type_to_str(h
->nlmsg_type
),
708 prefix2str(&p
, buf
, sizeof(buf
)),
709 src_p
.prefixlen
? " from " : "",
711 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
713 vrf_id
, table
, metric
, distance
);
717 if (rtm
->rtm_family
== AF_INET6
)
720 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
722 if (!tb
[RTA_MULTIPATH
]) {
723 struct nexthop nh
= {0};
726 nh
= parse_nexthop_unicast(
727 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
730 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
731 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
734 /* This is a multipath route */
735 struct route_entry
*re
;
736 struct nexthop_group
*ng
= NULL
;
737 struct rtnexthop
*rtnh
=
738 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
740 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
742 re
->distance
= distance
;
748 re
->uptime
= monotime(NULL
);
755 /* Use temporary list of nexthops; parse
756 * message payload's nexthops.
758 ng
= nexthop_group_new();
760 parse_multipath_nexthops_unicast(
761 ns_id
, ng
, rtm
, rtnh
, tb
,
764 zserv_nexthop_num_warn(
765 __func__
, (const struct prefix
*)&p
,
769 nexthop_group_delete(&ng
);
775 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
782 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
783 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
786 if (!tb
[RTA_MULTIPATH
]) {
788 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
790 memset(&nh
, 0, sizeof(nh
));
791 if (bh_type
== BLACKHOLE_UNSPEC
) {
793 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
794 else if (index
&& gate
)
797 ? NEXTHOP_TYPE_IPV4_IFINDEX
798 : NEXTHOP_TYPE_IPV6_IFINDEX
;
799 else if (!index
&& gate
)
806 NEXTHOP_TYPE_BLACKHOLE
;
807 nh
.bh_type
= BLACKHOLE_UNSPEC
;
810 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
811 nh
.bh_type
= bh_type
;
815 memcpy(&nh
.gate
, gate
, sz
);
816 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
817 flags
, &p
, &src_p
, &nh
, 0, table
,
818 metric
, distance
, true);
820 /* XXX: need to compare the entire list of
821 * nexthops here for NLM_F_APPEND stupidity */
822 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
823 flags
, &p
, &src_p
, NULL
, 0, table
,
824 metric
, distance
, true);
832 static struct mcast_route_data
*mroute
= NULL
;
834 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
835 ns_id_t ns_id
, int startup
)
839 struct rtattr
*tb
[RTA_MAX
+ 1];
840 struct mcast_route_data
*m
;
841 struct mcast_route_data mr
;
848 char oif_list
[256] = "\0";
855 memset(&mr
, 0, sizeof(mr
));
861 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
863 memset(tb
, 0, sizeof tb
);
864 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
867 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
869 table
= rtm
->rtm_table
;
871 vrf
= vrf_lookup_by_table(table
, ns_id
);
874 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
877 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
880 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
883 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
885 if (tb
[RTA_MULTIPATH
]) {
886 struct rtnexthop
*rtnh
=
887 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
889 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
891 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
894 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
897 if (rtnh
->rtnh_len
== 0)
900 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
901 rtnh
= RTNH_NEXT(rtnh
);
905 if (IS_ZEBRA_DEBUG_KERNEL
) {
906 struct interface
*ifp
= NULL
;
907 struct zebra_vrf
*zvrf
= NULL
;
909 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
910 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
911 for (count
= 0; count
< oif_count
; count
++) {
912 ifp
= if_lookup_by_index(oif
[count
], vrf
);
915 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
917 strlcat(oif_list
, temp
, sizeof(oif_list
));
919 zvrf
= zebra_vrf_lookup_by_id(vrf
);
920 ifp
= if_lookup_by_index(iif
, vrf
);
922 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
923 (zvrf
? zvrf
->vrf
->name
: "Unknown"), vrf
,
924 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
925 ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
931 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
938 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
939 /* If this is not route add/delete message print warning. */
940 zlog_debug("Kernel message: %s NS %u",
941 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
945 if (!(rtm
->rtm_family
== AF_INET
||
946 rtm
->rtm_family
== AF_INET6
||
947 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
949 EC_ZEBRA_UNKNOWN_FAMILY
,
950 "Invalid address family: %u received from kernel route change: %s",
951 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
955 /* Connected route. */
956 if (IS_ZEBRA_DEBUG_KERNEL
)
957 zlog_debug("%s %s %s proto %s NS %u",
958 nl_msg_type_to_str(h
->nlmsg_type
),
959 nl_family_to_str(rtm
->rtm_family
),
960 nl_rttype_to_str(rtm
->rtm_type
),
961 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
964 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
966 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
969 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
973 if (rtm
->rtm_type
== RTN_MULTICAST
)
974 netlink_route_change_read_multicast(h
, ns_id
, startup
);
976 netlink_route_change_read_unicast(h
, ns_id
, startup
);
980 /* Request for specific route information from the kernel */
981 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
988 /* Form the request, specifying filter (rtattr) if needed. */
989 memset(&req
, 0, sizeof(req
));
990 req
.n
.nlmsg_type
= type
;
991 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
992 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
993 req
.rtm
.rtm_family
= family
;
995 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
998 /* Routing table read function using netlink interface. Only called
1000 int netlink_route_read(struct zebra_ns
*zns
)
1003 struct zebra_dplane_info dp_info
;
1005 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1007 /* Get IPv4 routing table. */
1008 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1011 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1012 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1016 /* Get IPv6 routing table. */
1017 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1020 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1021 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1028 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
1030 struct nlmsghdr
*nlmsg
,
1031 size_t req_size
, int bytelen
,
1032 const struct nexthop
*nexthop
)
1034 if (route_family
== AF_MPLS
) {
1035 struct gw_family_t gw_fam
;
1037 gw_fam
.family
= gw_family
;
1038 if (gw_family
== AF_INET
)
1039 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1041 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1042 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1045 if (gw_family
== AF_INET
)
1046 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1047 &nexthop
->gate
.ipv4
, bytelen
);
1049 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1050 &nexthop
->gate
.ipv6
, bytelen
);
1054 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
1057 struct rtnexthop
*rtnh
,
1058 size_t req_size
, int bytelen
,
1059 const struct nexthop
*nexthop
)
1061 if (route_family
== AF_MPLS
) {
1062 struct gw_family_t gw_fam
;
1064 gw_fam
.family
= gw_family
;
1065 if (gw_family
== AF_INET
)
1066 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1068 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1069 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
1071 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
1073 if (gw_family
== AF_INET
)
1074 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1075 &nexthop
->gate
.ipv4
, bytelen
);
1077 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1078 &nexthop
->gate
.ipv6
, bytelen
);
1079 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1083 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1084 mpls_lse_t
*out_lse
, char *label_buf
,
1085 size_t label_buf_size
)
1087 char label_buf1
[20];
1090 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1091 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1094 if (IS_ZEBRA_DEBUG_KERNEL
) {
1096 sprintf(label_buf
, "label %u",
1097 nh_label
->label
[i
]);
1099 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1100 strlcat(label_buf
, label_buf1
, label_buf_size
);
1104 out_lse
[num_labels
] =
1105 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1112 /* This function takes a nexthop as argument and adds
1113 * the appropriate netlink attributes to an existing
1116 * @param routedesc: Human readable description of route type
1117 * (direct/recursive, single-/multipath)
1118 * @param bytelen: Length of addresses in bytes.
1119 * @param nexthop: Nexthop information
1120 * @param nlmsg: nlmsghdr structure to fill in.
1121 * @param req_size: The size allocated for the message.
1123 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
1124 const struct nexthop
*nexthop
,
1125 struct nlmsghdr
*nlmsg
,
1126 struct rtmsg
*rtmsg
,
1127 size_t req_size
, int cmd
)
1130 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1131 char label_buf
[256];
1137 * label_buf is *only* currently used within debugging.
1138 * As such when we assign it we are guarding it inside
1139 * a debug test. If you want to change this make sure
1140 * you fix this assumption
1142 label_buf
[0] = '\0';
1144 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1148 /* Set the BoS bit */
1149 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1151 if (rtmsg
->rtm_family
== AF_MPLS
)
1152 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1153 num_labels
* sizeof(mpls_lse_t
));
1155 struct rtattr
*nest
;
1156 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1158 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1160 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1161 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1162 num_labels
* sizeof(mpls_lse_t
));
1163 addattr_nest_end(nlmsg
, nest
);
1167 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1168 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1170 if (rtmsg
->rtm_family
== AF_INET
1171 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1172 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1173 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1174 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1175 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1177 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1178 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1179 &nexthop
->rmap_src
.ipv4
, bytelen
);
1180 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1181 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1182 &nexthop
->src
.ipv4
, bytelen
);
1184 if (IS_ZEBRA_DEBUG_KERNEL
)
1186 " 5549: _netlink_route_build_singlepath() (%s): "
1187 "nexthop via %s %s if %u(%u)",
1188 routedesc
, ipv4_ll_buf
, label_buf
,
1189 nexthop
->ifindex
, nexthop
->vrf_id
);
1193 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1194 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1195 /* Send deletes to the kernel without specifying the next-hop */
1196 if (cmd
!= RTM_DELROUTE
)
1197 _netlink_route_nl_add_gateway_info(
1198 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1201 if (cmd
== RTM_NEWROUTE
) {
1202 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1203 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1204 &nexthop
->rmap_src
.ipv4
, bytelen
);
1205 else if (nexthop
->src
.ipv4
.s_addr
)
1206 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1207 &nexthop
->src
.ipv4
, bytelen
);
1210 if (IS_ZEBRA_DEBUG_KERNEL
)
1212 "netlink_route_multipath() (%s): "
1213 "nexthop via %s %s if %u(%u)",
1214 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1215 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1218 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1219 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1220 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1221 nlmsg
, req_size
, bytelen
,
1224 if (cmd
== RTM_NEWROUTE
) {
1225 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1226 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1227 &nexthop
->rmap_src
.ipv6
, bytelen
);
1228 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1229 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1230 &nexthop
->src
.ipv6
, bytelen
);
1233 if (IS_ZEBRA_DEBUG_KERNEL
)
1235 "netlink_route_multipath() (%s): "
1236 "nexthop via %s %s if %u(%u)",
1237 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1238 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1242 * We have the ifindex so we should always send it
1243 * This is especially useful if we are doing route
1246 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1247 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1249 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1250 if (cmd
== RTM_NEWROUTE
) {
1251 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1252 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1253 &nexthop
->rmap_src
.ipv4
, bytelen
);
1254 else if (nexthop
->src
.ipv4
.s_addr
)
1255 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1256 &nexthop
->src
.ipv4
, bytelen
);
1259 if (IS_ZEBRA_DEBUG_KERNEL
)
1261 "netlink_route_multipath() (%s): "
1262 "nexthop via if %u(%u)",
1263 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1267 /* This function takes a nexthop as argument and
1268 * appends to the given rtattr/rtnexthop pair the
1269 * representation of the nexthop. If the nexthop
1270 * defines a preferred source, the src parameter
1271 * will be modified to point to that src, otherwise
1272 * it will be kept unmodified.
1274 * @param routedesc: Human readable description of route type
1275 * (direct/recursive, single-/multipath)
1276 * @param bytelen: Length of addresses in bytes.
1277 * @param nexthop: Nexthop information
1278 * @param rta: rtnetlink attribute structure
1279 * @param rtnh: pointer to an rtnetlink nexthop structure
1280 * @param src: pointer pointing to a location where
1281 * the prefsrc should be stored.
1283 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1284 const struct nexthop
*nexthop
,
1286 struct rtnexthop
*rtnh
,
1287 struct rtmsg
*rtmsg
,
1288 const union g_addr
**src
)
1290 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1291 char label_buf
[256];
1294 rtnh
->rtnh_len
= sizeof(*rtnh
);
1295 rtnh
->rtnh_flags
= 0;
1296 rtnh
->rtnh_hops
= 0;
1297 rta
->rta_len
+= rtnh
->rtnh_len
;
1302 * label_buf is *only* currently used within debugging.
1303 * As such when we assign it we are guarding it inside
1304 * a debug test. If you want to change this make sure
1305 * you fix this assumption
1307 label_buf
[0] = '\0';
1309 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1313 /* Set the BoS bit */
1314 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1316 if (rtmsg
->rtm_family
== AF_MPLS
) {
1317 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1319 num_labels
* sizeof(mpls_lse_t
));
1321 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1323 struct rtattr
*nest
;
1324 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1325 int len
= rta
->rta_len
;
1327 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1328 &encap
, sizeof(uint16_t));
1329 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1330 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1332 num_labels
* sizeof(mpls_lse_t
));
1333 rta_nest_end(rta
, nest
);
1334 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1338 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1339 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1341 if (rtmsg
->rtm_family
== AF_INET
1342 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1343 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1345 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1346 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1348 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1349 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1351 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1352 *src
= &nexthop
->rmap_src
;
1353 else if (nexthop
->src
.ipv4
.s_addr
)
1354 *src
= &nexthop
->src
;
1356 if (IS_ZEBRA_DEBUG_KERNEL
)
1358 " 5549: netlink_route_build_multipath() (%s): "
1359 "nexthop via %s %s if %u",
1360 routedesc
, ipv4_ll_buf
, label_buf
,
1365 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1366 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1367 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1368 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1370 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1371 *src
= &nexthop
->rmap_src
;
1372 else if (nexthop
->src
.ipv4
.s_addr
)
1373 *src
= &nexthop
->src
;
1375 if (IS_ZEBRA_DEBUG_KERNEL
)
1377 "netlink_route_multipath() (%s): "
1378 "nexthop via %s %s if %u",
1379 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1380 label_buf
, nexthop
->ifindex
);
1382 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1383 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1384 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1385 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1388 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1389 *src
= &nexthop
->rmap_src
;
1390 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1391 *src
= &nexthop
->src
;
1393 if (IS_ZEBRA_DEBUG_KERNEL
)
1395 "netlink_route_multipath() (%s): "
1396 "nexthop via %s %s if %u",
1397 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1398 label_buf
, nexthop
->ifindex
);
1402 * We have figured out the ifindex so we should always send it
1403 * This is especially useful if we are doing route
1406 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1407 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1410 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1411 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1412 *src
= &nexthop
->rmap_src
;
1413 else if (nexthop
->src
.ipv4
.s_addr
)
1414 *src
= &nexthop
->src
;
1416 if (IS_ZEBRA_DEBUG_KERNEL
)
1418 "netlink_route_multipath() (%s): "
1419 "nexthop via if %u",
1420 routedesc
, nexthop
->ifindex
);
1424 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1425 const zebra_nhlfe_t
*nhlfe
,
1426 struct nlmsghdr
*nlmsg
,
1427 struct rtmsg
*rtmsg
,
1428 size_t req_size
, int cmd
)
1433 family
= NHLFE_FAMILY(nhlfe
);
1434 bytelen
= (family
== AF_INET
? 4 : 16);
1435 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1436 nlmsg
, rtmsg
, req_size
, cmd
);
1441 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1442 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1443 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1448 family
= NHLFE_FAMILY(nhlfe
);
1449 bytelen
= (family
== AF_INET
? 4 : 16);
1450 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1455 /* Log debug information for netlink_route_multipath
1456 * if debug logging is enabled.
1458 * @param cmd: Netlink command which is to be processed
1459 * @param p: Prefix for which the change is due
1460 * @param family: Address family which the change concerns
1461 * @param zvrf: The vrf we are in
1462 * @param tableid: The table we are working on
1464 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1465 int family
, vrf_id_t vrfid
,
1468 if (IS_ZEBRA_DEBUG_KERNEL
) {
1469 char buf
[PREFIX_STRLEN
];
1471 "netlink_route_multipath(): %s %s vrf %u(%u)",
1472 nl_msg_type_to_str(cmd
),
1473 prefix2str(p
, buf
, sizeof(buf
)),
1478 static void _netlink_nexthop_debug(int cmd
, uint32_t id
)
1480 if (IS_ZEBRA_DEBUG_KERNEL
)
1481 zlog_debug("netlink_nexthop(): %s, id=%u",
1482 nl_msg_type_to_str(cmd
), id
);
1485 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1487 if (IS_ZEBRA_DEBUG_KERNEL
)
1488 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1489 nl_msg_type_to_str(cmd
), label
);
1492 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1493 int llalen
, ns_id_t ns_id
)
1495 uint8_t protocol
= RTPROT_ZEBRA
;
1502 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1504 memset(&req
, 0, sizeof(req
));
1506 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1507 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1508 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1509 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1511 req
.ndm
.ndm_family
= AF_INET
;
1512 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1513 req
.ndm
.ndm_ifindex
= ifindex
;
1514 req
.ndm
.ndm_type
= RTN_UNICAST
;
1516 addattr_l(&req
.n
, sizeof(req
),
1517 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
1518 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1519 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1521 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1526 * Routing table change via netlink interface, using a dataplane context object
1528 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1531 struct nexthop
*nexthop
= NULL
;
1532 unsigned int nexthop_num
;
1534 const char *routedesc
;
1537 const struct prefix
*p
, *src_p
;
1543 char buf
[NL_PKT_BUF_SIZE
];
1546 p
= dplane_ctx_get_dest(ctx
);
1547 src_p
= dplane_ctx_get_src(ctx
);
1549 family
= PREFIX_FAMILY(p
);
1551 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1553 bytelen
= (family
== AF_INET
? 4 : 16);
1555 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1556 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1558 if ((cmd
== RTM_NEWROUTE
) &&
1559 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1560 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1562 req
.n
.nlmsg_type
= cmd
;
1564 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1566 req
.r
.rtm_family
= family
;
1567 req
.r
.rtm_dst_len
= p
->prefixlen
;
1568 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1569 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1571 if (cmd
== RTM_DELROUTE
)
1572 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1574 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1577 * blackhole routes are not RTN_UNICAST, they are
1578 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1579 * so setting this value as a RTN_UNICAST would
1580 * cause the route lookup of just the prefix
1581 * to fail. So no need to specify this for
1582 * the RTM_DELROUTE case
1584 if (cmd
!= RTM_DELROUTE
)
1585 req
.r
.rtm_type
= RTN_UNICAST
;
1587 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1589 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1593 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1595 * either by the kernel or by zebra. Its purely for calculating best
1597 * by the routing protocol and for communicating with protocol peers.
1599 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1601 #if defined(SUPPORT_REALMS)
1605 if (cmd
== RTM_DELROUTE
)
1606 tag
= dplane_ctx_get_old_tag(ctx
);
1608 tag
= dplane_ctx_get_tag(ctx
);
1610 if (tag
> 0 && tag
<= 255)
1611 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1614 /* Table corresponding to this route. */
1615 table_id
= dplane_ctx_get_table(ctx
);
1617 req
.r
.rtm_table
= table_id
;
1619 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1620 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1623 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1626 * If we are not updating the route and we have received
1627 * a route delete, then all we need to fill in is the
1628 * prefix information to tell the kernel to schwack
1631 if (cmd
== RTM_DELROUTE
)
1634 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1635 char buf
[NL_PKT_BUF_SIZE
];
1636 struct rtattr
*rta
= (void *)buf
;
1637 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1638 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1640 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1642 rta
->rta_type
= RTA_METRICS
;
1643 rta
->rta_len
= RTA_LENGTH(0);
1644 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1645 RTAX_MTU
, &mtu
, sizeof(mtu
));
1646 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1651 /* Kernel supports nexthop objects */
1652 addattr32(&req
.n
, sizeof(req
), RTA_NH_ID
,
1653 dplane_ctx_get_nhe_id(ctx
));
1657 /* Count overall nexthops so we can decide whether to use singlepath
1658 * or multipath case.
1661 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1662 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1664 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1670 /* Singlepath case. */
1671 if (nexthop_num
== 1) {
1673 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1675 * So we want to cover 2 types of blackhole
1677 * 1) A normal blackhole route( ala from a static
1679 * 2) A recursively resolved blackhole route
1681 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1682 switch (nexthop
->bh_type
) {
1683 case BLACKHOLE_ADMINPROHIB
:
1684 req
.r
.rtm_type
= RTN_PROHIBIT
;
1686 case BLACKHOLE_REJECT
:
1687 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1690 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1695 if (CHECK_FLAG(nexthop
->flags
,
1696 NEXTHOP_FLAG_RECURSIVE
)) {
1701 if (family
== AF_INET
) {
1702 if (nexthop
->rmap_src
.ipv4
.s_addr
1705 nexthop
->rmap_src
.ipv4
;
1707 } else if (nexthop
->src
.ipv4
.s_addr
1713 } else if (family
== AF_INET6
) {
1714 if (!IN6_IS_ADDR_UNSPECIFIED(
1715 &nexthop
->rmap_src
.ipv6
)) {
1717 nexthop
->rmap_src
.ipv6
;
1720 !IN6_IS_ADDR_UNSPECIFIED(
1721 &nexthop
->src
.ipv6
)) {
1730 if ((cmd
== RTM_NEWROUTE
1731 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1732 routedesc
= nexthop
->rparent
1733 ? "recursive, single-path"
1736 _netlink_route_build_singlepath(
1737 routedesc
, bytelen
, nexthop
, &req
.n
,
1738 &req
.r
, sizeof(req
), cmd
);
1743 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1744 if (family
== AF_INET
)
1745 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1746 &src
.ipv4
, bytelen
);
1747 else if (family
== AF_INET6
)
1748 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1749 &src
.ipv6
, bytelen
);
1751 } else { /* Multipath case */
1752 char buf
[NL_PKT_BUF_SIZE
];
1753 struct rtattr
*rta
= (void *)buf
;
1754 struct rtnexthop
*rtnh
;
1755 const union g_addr
*src1
= NULL
;
1757 rta
->rta_type
= RTA_MULTIPATH
;
1758 rta
->rta_len
= RTA_LENGTH(0);
1759 rtnh
= RTA_DATA(rta
);
1762 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1763 if (CHECK_FLAG(nexthop
->flags
,
1764 NEXTHOP_FLAG_RECURSIVE
)) {
1765 /* This only works for IPv4 now */
1769 if (family
== AF_INET
) {
1770 if (nexthop
->rmap_src
.ipv4
.s_addr
1773 nexthop
->rmap_src
.ipv4
;
1775 } else if (nexthop
->src
.ipv4
.s_addr
1781 } else if (family
== AF_INET6
) {
1782 if (!IN6_IS_ADDR_UNSPECIFIED(
1783 &nexthop
->rmap_src
.ipv6
)) {
1785 nexthop
->rmap_src
.ipv6
;
1788 !IN6_IS_ADDR_UNSPECIFIED(
1789 &nexthop
->src
.ipv6
)) {
1799 if ((cmd
== RTM_NEWROUTE
1800 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1801 routedesc
= nexthop
->rparent
1802 ? "recursive, multipath"
1806 _netlink_route_build_multipath(
1807 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1809 rtnh
= RTNH_NEXT(rtnh
);
1811 if (!setsrc
&& src1
) {
1812 if (family
== AF_INET
)
1813 src
.ipv4
= src1
->ipv4
;
1814 else if (family
== AF_INET6
)
1815 src
.ipv6
= src1
->ipv6
;
1821 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1822 if (family
== AF_INET
)
1823 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1824 &src
.ipv4
, bytelen
);
1825 else if (family
== AF_INET6
)
1826 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1827 &src
.ipv6
, bytelen
);
1828 if (IS_ZEBRA_DEBUG_KERNEL
)
1829 zlog_debug("Setting source");
1832 if (rta
->rta_len
> RTA_LENGTH(0))
1833 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1834 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1837 /* If there is no useful nexthop then return. */
1838 if (nexthop_num
== 0) {
1839 if (IS_ZEBRA_DEBUG_KERNEL
)
1841 "netlink_route_multipath(): No useful nexthop.");
1846 /* Talk to netlink socket. */
1847 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1848 dplane_ctx_get_ns(ctx
), 0);
1851 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1853 uint32_t actual_table
;
1855 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1863 struct zebra_ns
*zns
;
1866 memset(&req
, 0, sizeof(req
));
1868 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1869 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1870 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1872 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1873 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1875 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1876 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1877 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1878 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1882 * So during the namespace cleanup we started storing
1883 * the zvrf table_id for the default table as RT_TABLE_MAIN
1884 * which is what the normal routing table for ip routing is.
1885 * This change caused this to break our lookups of sg data
1886 * because prior to this change the zvrf->table_id was 0
1887 * and when the pim multicast kernel code saw a 0,
1888 * it was auto-translated to RT_TABLE_DEFAULT. But since
1889 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1890 * and the kernel goes screw you and the delicious cookies you
1891 * are trying to give me. So now we have this little hack.
1893 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1895 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1897 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1898 &zns
->netlink_cmd
, zns
, 0);
1904 /* Char length to debug ID with */
1905 #define ID_LENGTH 10
1907 static void _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
1909 const struct nh_grp
*z_grp
,
1910 const uint8_t count
)
1912 struct nexthop_grp grp
[count
];
1913 /* Need space for max group size, "/", and null term */
1914 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
1915 char buf1
[ID_LENGTH
+ 2];
1919 memset(grp
, 0, sizeof(grp
));
1922 for (int i
= 0; i
< count
; i
++) {
1923 grp
[i
].id
= z_grp
[i
].id
;
1924 grp
[i
].weight
= z_grp
[i
].weight
;
1926 if (IS_ZEBRA_DEBUG_KERNEL
) {
1928 snprintf(buf
, sizeof(buf1
), "group %u",
1931 snprintf(buf1
, sizeof(buf1
), "/%u",
1933 strlcat(buf
, buf1
, sizeof(buf
));
1937 addattr_l(n
, req_size
, NHA_GROUP
, grp
, count
* sizeof(*grp
));
1940 if (IS_ZEBRA_DEBUG_KERNEL
)
1941 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
1945 * netlink_nexthop() - Nexthop change via the netlink interface
1947 * @ctx: Dataplane ctx
1949 * Return: Result status
1951 static int netlink_nexthop(int cmd
, struct zebra_dplane_ctx
*ctx
)
1956 char buf
[NL_PKT_BUF_SIZE
];
1959 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1960 char label_buf
[256];
1962 size_t req_size
= sizeof(req
);
1964 /* Nothing to do if the kernel doesn't support nexthop objects */
1968 label_buf
[0] = '\0';
1970 memset(&req
, 0, req_size
);
1972 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
1973 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1975 if (cmd
== RTM_NEWNEXTHOP
)
1976 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1978 req
.n
.nlmsg_type
= cmd
;
1979 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1981 req
.nhm
.nh_family
= AF_UNSPEC
;
1984 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
1988 EC_ZEBRA_NHG_FIB_UPDATE
,
1989 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1993 addattr32(&req
.n
, req_size
, NHA_ID
, id
);
1995 if (cmd
== RTM_NEWNEXTHOP
) {
1996 if (dplane_ctx_get_nhe_nh_grp_count(ctx
))
1997 _netlink_nexthop_build_group(
1998 &req
.n
, req_size
, id
,
1999 dplane_ctx_get_nhe_nh_grp(ctx
),
2000 dplane_ctx_get_nhe_nh_grp_count(ctx
));
2002 const struct nexthop
*nh
=
2003 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2004 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2007 req
.nhm
.nh_family
= AF_INET
;
2008 else if (afi
== AFI_IP6
)
2009 req
.nhm
.nh_family
= AF_INET6
;
2012 case NEXTHOP_TYPE_IPV4
:
2013 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2014 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2015 &nh
->gate
.ipv4
, IPV4_MAX_BYTELEN
);
2017 case NEXTHOP_TYPE_IPV6
:
2018 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2019 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2020 &nh
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
2022 case NEXTHOP_TYPE_BLACKHOLE
:
2023 addattr_l(&req
.n
, req_size
, NHA_BLACKHOLE
, NULL
,
2025 /* Blackhole shouldn't have anymore attributes
2028 case NEXTHOP_TYPE_IFINDEX
:
2029 /* Don't need anymore info for this */
2035 EC_ZEBRA_NHG_FIB_UPDATE
,
2036 "Context received for kernel nexthop update without an interface");
2040 addattr32(&req
.n
, req_size
, NHA_OIF
, nh
->ifindex
);
2043 build_label_stack(nh
->nh_label
, out_lse
,
2044 label_buf
, sizeof(label_buf
));
2047 /* Set the BoS bit */
2048 out_lse
[num_labels
- 1] |=
2049 htonl(1 << MPLS_LS_S_SHIFT
);
2052 * TODO: MPLS unsupported for now in kernel.
2054 if (req
.nhm
.nh_family
== AF_MPLS
)
2057 addattr_l(&req
.n
, req_size
, NHA_NEWDST
,
2060 * sizeof(mpls_lse_t
));
2063 struct rtattr
*nest
;
2064 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
2066 addattr_l(&req
.n
, req_size
,
2067 NHA_ENCAP_TYPE
, &encap
,
2069 nest
= addattr_nest(&req
.n
, req_size
,
2071 addattr_l(&req
.n
, req_size
,
2072 MPLS_IPTUNNEL_DST
, &out_lse
,
2074 * sizeof(mpls_lse_t
));
2075 addattr_nest_end(&req
.n
, nest
);
2080 if (IS_ZEBRA_DEBUG_KERNEL
) {
2081 char buf
[NEXTHOP_STRLEN
];
2083 snprintfrr(buf
, sizeof(buf
), "%pNHv", nh
);
2084 zlog_debug("%s: ID (%u): %s (%u) %s ", __func__
,
2085 id
, buf
, nh
->vrf_id
, label_buf
);
2089 req
.nhm
.nh_protocol
= zebra2proto(dplane_ctx_get_nhe_type(ctx
));
2091 } else if (cmd
!= RTM_DELNEXTHOP
) {
2093 EC_ZEBRA_NHG_FIB_UPDATE
,
2094 "Nexthop group kernel update command (%d) does not exist",
2099 _netlink_nexthop_debug(cmd
, id
);
2101 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2102 dplane_ctx_get_ns(ctx
), 0);
2106 * kernel_nexthop_update() - Update/delete a nexthop from the kernel
2108 * @ctx: Dataplane context
2110 * Return: Dataplane result flag
2112 enum zebra_dplane_result
kernel_nexthop_update(struct zebra_dplane_ctx
*ctx
)
2117 switch (dplane_ctx_get_op(ctx
)) {
2118 case DPLANE_OP_NH_DELETE
:
2119 cmd
= RTM_DELNEXTHOP
;
2121 case DPLANE_OP_NH_INSTALL
:
2122 case DPLANE_OP_NH_UPDATE
:
2123 cmd
= RTM_NEWNEXTHOP
;
2125 case DPLANE_OP_ROUTE_INSTALL
:
2126 case DPLANE_OP_ROUTE_UPDATE
:
2127 case DPLANE_OP_ROUTE_DELETE
:
2128 case DPLANE_OP_ROUTE_NOTIFY
:
2129 case DPLANE_OP_LSP_INSTALL
:
2130 case DPLANE_OP_LSP_UPDATE
:
2131 case DPLANE_OP_LSP_DELETE
:
2132 case DPLANE_OP_LSP_NOTIFY
:
2133 case DPLANE_OP_PW_INSTALL
:
2134 case DPLANE_OP_PW_UNINSTALL
:
2135 case DPLANE_OP_SYS_ROUTE_ADD
:
2136 case DPLANE_OP_SYS_ROUTE_DELETE
:
2137 case DPLANE_OP_ADDR_INSTALL
:
2138 case DPLANE_OP_ADDR_UNINSTALL
:
2139 case DPLANE_OP_MAC_INSTALL
:
2140 case DPLANE_OP_MAC_DELETE
:
2141 case DPLANE_OP_NEIGH_INSTALL
:
2142 case DPLANE_OP_NEIGH_UPDATE
:
2143 case DPLANE_OP_NEIGH_DELETE
:
2144 case DPLANE_OP_VTEP_ADD
:
2145 case DPLANE_OP_VTEP_DELETE
:
2146 case DPLANE_OP_NONE
:
2148 EC_ZEBRA_NHG_FIB_UPDATE
,
2149 "Context received for kernel nexthop update with incorrect OP code (%u)",
2150 dplane_ctx_get_op(ctx
));
2151 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2154 ret
= netlink_nexthop(cmd
, ctx
);
2156 return (ret
== 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2157 : ZEBRA_DPLANE_REQUEST_FAILURE
);
2161 * Update or delete a prefix from the kernel,
2162 * using info from a dataplane context.
2164 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
2167 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2168 struct nexthop
*nexthop
;
2170 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2172 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2174 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2176 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2177 /* Single 'replace' operation */
2181 * With route replace semantics in place
2182 * for v4 routes and the new route is a system
2183 * route we do not install anything.
2184 * The problem here is that the new system
2185 * route should cause us to withdraw from
2186 * the kernel the old non-system route
2188 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)) &&
2189 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2190 (void)netlink_route_multipath(RTM_DELROUTE
,
2194 * So v6 route replace semantics are not in
2195 * the kernel at this point as I understand it.
2196 * so let's do a delete then an add.
2197 * In the future once v6 route replace semantics
2198 * are in we can figure out what to do here to
2199 * allow working with old and new kernels.
2201 * I'm also intentionally ignoring the failure case
2202 * of the route delete. If that happens yeah we're
2205 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2206 (void)netlink_route_multipath(RTM_DELROUTE
,
2212 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2215 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2216 ret
= netlink_route_multipath(cmd
, ctx
);
2219 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
2220 /* Update installed nexthops to signal which have been
2223 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2224 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2227 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2228 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2234 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2238 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2240 * @tb: Netlink RTA data
2241 * @family: Address family in the nhmsg
2242 * @ifp: Interface connected - this should be NULL, we fill it in
2243 * @ns_id: Namspace id
2245 * Return: New nexthop
2247 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2248 unsigned char family
,
2249 struct interface
**ifp
,
2252 struct nexthop nh
= {};
2254 enum nexthop_types_t type
= 0;
2257 struct interface
*ifp_lookup
;
2259 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2262 if (tb
[NHA_GATEWAY
]) {
2265 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2269 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2274 EC_ZEBRA_BAD_NHG_MESSAGE
,
2275 "Nexthop gateway with bad address family (%d) received from kernel",
2279 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2281 type
= NEXTHOP_TYPE_IFINDEX
;
2287 memcpy(&(nh
.gate
), gate
, sz
);
2290 nh
.ifindex
= if_index
;
2293 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2298 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2301 EC_ZEBRA_UNKNOWN_INTERFACE
,
2302 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2303 __PRETTY_FUNCTION__
, nh
.ifindex
);
2305 nh
.vrf_id
= VRF_DEFAULT
;
2308 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2309 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2312 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2314 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2315 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2318 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2325 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2326 struct nh_grp
*z_grp
, int z_grp_size
)
2329 /* linux/nexthop.h group struct */
2330 struct nexthop_grp
*n_grp
= NULL
;
2332 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2333 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2335 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2336 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2337 "Invalid nexthop group received from the kernel");
2342 // TODO: Need type for something?
2343 zlog_debug("Nexthop group type: %d",
2344 *((uint16_t *)RTA_DATA(tb
[NHA_GROUP_TYPE
])));
2348 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2349 z_grp
[i
].id
= n_grp
[i
].id
;
2350 z_grp
[i
].weight
= n_grp
[i
].weight
;
2356 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2358 * @h: Netlink message header
2359 * @ns_id: Namspace id
2360 * @startup: Are we reading under startup conditions?
2362 * Return: Result status
2364 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2367 /* nexthop group id */
2369 unsigned char family
;
2371 afi_t afi
= AFI_UNSPEC
;
2372 vrf_id_t vrf_id
= 0;
2373 struct interface
*ifp
= NULL
;
2374 struct nhmsg
*nhm
= NULL
;
2375 struct nexthop nh
= {};
2376 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2377 /* Count of nexthops in group array */
2378 uint8_t grp_count
= 0;
2379 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2381 nhm
= NLMSG_DATA(h
);
2383 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2386 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2389 "%s: Message received from netlink is of a broken size %d %zu",
2390 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2391 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2395 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2400 EC_ZEBRA_BAD_NHG_MESSAGE
,
2401 "Nexthop group without an ID received from the kernel");
2405 /* We use the ID key'd nhg table for kernel updates */
2406 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2408 family
= nhm
->nh_family
;
2409 afi
= family2afi(family
);
2411 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2413 if (IS_ZEBRA_DEBUG_KERNEL
)
2414 zlog_debug("%s ID (%u) %s NS %u",
2415 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2416 nl_family_to_str(family
), ns_id
);
2419 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2420 if (tb
[NHA_GROUP
]) {
2422 * If this is a group message its only going to have
2423 * an array of nexthop IDs associated with it
2425 grp_count
= netlink_nexthop_process_group(
2426 tb
, grp
, array_size(grp
));
2428 if (tb
[NHA_BLACKHOLE
]) {
2430 * This nexthop is just for blackhole-ing
2431 * traffic, it should not have an OIF, GATEWAY,
2434 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2435 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2436 } else if (tb
[NHA_OIF
])
2438 * This is a true new nexthop, so we need
2439 * to parse the gateway and device info
2441 nh
= netlink_nexthop_process_nh(tb
, family
,
2446 EC_ZEBRA_BAD_NHG_MESSAGE
,
2447 "Invalid Nexthop message received from the kernel with ID (%u)",
2451 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2452 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2453 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2457 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2461 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2462 zebra_nhg_kernel_del(id
);
2468 * netlink_request_nexthop() - Request nextop information from the kernel
2469 * @zns: Zebra namespace
2470 * @family: AF_* netlink family
2471 * @type: RTM_* route type
2473 * Return: Result status
2475 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2482 /* Form the request, specifying filter (rtattr) if needed. */
2483 memset(&req
, 0, sizeof(req
));
2484 req
.n
.nlmsg_type
= type
;
2485 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2486 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2487 req
.nhm
.nh_family
= family
;
2489 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2494 * netlink_nexthop_read() - Nexthop read function using netlink interface
2496 * @zns: Zebra name space
2498 * Return: Result status
2499 * Only called at bootstrap time.
2501 int netlink_nexthop_read(struct zebra_ns
*zns
)
2504 struct zebra_dplane_info dp_info
;
2506 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2508 /* Get nexthop objects */
2509 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2512 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2516 /* If we succesfully read in nexthop objects,
2517 * this kernel must support them.
2520 else if (IS_ZEBRA_DEBUG_KERNEL
)
2521 zlog_debug("Nexthop objects not supported on this kernel");
2527 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2528 int llalen
, ns_id_t ns_id
)
2530 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2531 addr
, lla
, llalen
, ns_id
);
2535 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2536 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2538 static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2541 uint8_t protocol
= RTPROT_ZEBRA
;
2547 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2548 const struct ipaddr
*addr
;
2550 memset(&req
, 0, sizeof(req
));
2552 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2553 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2554 if (cmd
== RTM_NEWNEIGH
)
2555 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2556 req
.n
.nlmsg_type
= cmd
;
2557 req
.ndm
.ndm_family
= PF_BRIDGE
;
2558 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2559 req
.ndm
.ndm_flags
|= NTF_SELF
; /* Handle by "self", not "master" */
2562 addattr_l(&req
.n
, sizeof(req
),
2563 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2564 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2565 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2567 addr
= dplane_ctx_neigh_get_ipaddr(ctx
);
2569 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &(addr
->ipaddr_v4
), 4);
2571 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2572 dplane_ctx_get_ns(ctx
), 0);
2576 #define NDA_RTA(r) \
2577 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2580 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2583 struct interface
*ifp
;
2584 struct zebra_if
*zif
;
2585 struct rtattr
*tb
[NDA_MAX
+ 1];
2586 struct interface
*br_if
;
2589 struct prefix vtep_ip
;
2590 int vid_present
= 0, dst_present
= 0;
2591 char buf
[ETHER_ADDR_STRLEN
];
2596 ndm
= NLMSG_DATA(h
);
2598 /* We only process macfdb notifications if EVPN is enabled */
2599 if (!is_evpn_enabled())
2602 /* The interface should exist. */
2603 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2605 if (!ifp
|| !ifp
->info
) {
2606 if (IS_ZEBRA_DEBUG_KERNEL
)
2607 zlog_debug("\t%s without associated interface: %u",
2608 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
2612 /* The interface should be something we're interested in. */
2613 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
2614 if (IS_ZEBRA_DEBUG_KERNEL
)
2615 zlog_debug("\t%s Not interested in %s, not a slave",
2616 __PRETTY_FUNCTION__
, ifp
->name
);
2620 /* Drop "permanent" entries. */
2621 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2622 if (IS_ZEBRA_DEBUG_KERNEL
)
2623 zlog_debug("\t%s Entry is PERMANENT, dropping",
2624 __PRETTY_FUNCTION__
);
2628 zif
= (struct zebra_if
*)ifp
->info
;
2629 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2630 if (IS_ZEBRA_DEBUG_KERNEL
)
2632 "%s family %s IF %s(%u) brIF %u - no bridge master",
2633 nl_msg_type_to_str(h
->nlmsg_type
),
2634 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2636 zif
->brslave_info
.bridge_ifindex
);
2640 /* Parse attributes and extract fields of interest. */
2641 memset(tb
, 0, sizeof tb
);
2642 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2644 if (!tb
[NDA_LLADDR
]) {
2645 if (IS_ZEBRA_DEBUG_KERNEL
)
2646 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2647 nl_msg_type_to_str(h
->nlmsg_type
),
2648 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2650 zif
->brslave_info
.bridge_ifindex
);
2654 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2655 if (IS_ZEBRA_DEBUG_KERNEL
)
2657 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2658 nl_msg_type_to_str(h
->nlmsg_type
),
2659 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2661 zif
->brslave_info
.bridge_ifindex
,
2662 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2666 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2668 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2670 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2671 sprintf(vid_buf
, " VLAN %u", vid
);
2675 /* TODO: Only IPv4 supported now. */
2677 vtep_ip
.family
= AF_INET
;
2678 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2679 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2681 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2684 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2686 if (IS_ZEBRA_DEBUG_KERNEL
)
2687 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2688 nl_msg_type_to_str(h
->nlmsg_type
),
2689 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2690 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2691 sticky
? "sticky " : "",
2692 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2693 dst_present
? dst_buf
: "");
2695 if (filter_vlan
&& vid
!= filter_vlan
) {
2696 if (IS_ZEBRA_DEBUG_KERNEL
)
2697 zlog_debug("\tFiltered due to filter vlan: %d",
2702 /* If add or update, do accordingly if learnt on a "local" interface; if
2703 * the notification is over VxLAN, this has to be related to
2705 * so perform an implicit delete of any local entry (if it exists).
2707 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2708 if (IS_ZEBRA_IF_VXLAN(ifp
))
2709 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2712 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2716 /* This is a delete notification.
2717 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2718 * 2. For a MAC over "local" interface, delete the mac
2719 * Note: We will get notifications from both bridge driver and VxLAN
2721 * Ignore the notification from VxLan driver as it is also generated
2722 * when mac moves from remote to local.
2725 if (IS_ZEBRA_DEBUG_KERNEL
)
2726 zlog_debug("\tNo Destination Present");
2730 if (IS_ZEBRA_IF_VXLAN(ifp
))
2731 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2734 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2737 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2742 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2745 /* Length validity. */
2746 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2750 /* We are interested only in AF_BRIDGE notifications. */
2751 ndm
= NLMSG_DATA(h
);
2752 if (ndm
->ndm_family
!= AF_BRIDGE
)
2755 return netlink_macfdb_change(h
, len
, ns_id
);
2758 /* Request for MAC FDB information from the kernel */
2759 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2760 int type
, ifindex_t master_ifindex
)
2764 struct ifinfomsg ifm
;
2768 /* Form the request, specifying filter (rtattr) if needed. */
2769 memset(&req
, 0, sizeof(req
));
2770 req
.n
.nlmsg_type
= type
;
2771 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2772 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2773 req
.ifm
.ifi_family
= family
;
2775 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2777 return netlink_request(netlink_cmd
, &req
.n
);
2781 * MAC forwarding database read using netlink interface. This is invoked
2784 int netlink_macfdb_read(struct zebra_ns
*zns
)
2787 struct zebra_dplane_info dp_info
;
2789 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2791 /* Get bridge FDB table. */
2792 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2796 /* We are reading entire table. */
2798 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2805 * MAC forwarding database read using netlink interface. This is for a
2806 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2808 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2809 struct interface
*br_if
)
2811 struct zebra_if
*br_zif
;
2812 struct zebra_if
*zif
;
2813 struct zebra_l2info_vxlan
*vxl
;
2814 struct zebra_dplane_info dp_info
;
2817 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2819 /* Save VLAN we're filtering on, if needed. */
2820 br_zif
= (struct zebra_if
*)br_if
->info
;
2821 zif
= (struct zebra_if
*)ifp
->info
;
2822 vxl
= &zif
->l2info
.vxl
;
2823 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2824 filter_vlan
= vxl
->access_vlan
;
2826 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2828 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2832 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2835 /* Reset VLAN filter. */
2841 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2842 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2845 struct interface
*br_if
,
2846 struct ethaddr
*mac
,
2854 struct zebra_if
*br_zif
;
2855 char buf
[ETHER_ADDR_STRLEN
];
2857 memset(&req
, 0, sizeof(req
));
2858 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2859 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2860 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2861 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2862 /* req.ndm.ndm_state = NUD_REACHABLE; */
2864 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2866 br_zif
= (struct zebra_if
*)br_if
->info
;
2867 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2868 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2870 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2872 if (IS_ZEBRA_DEBUG_KERNEL
)
2873 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2874 __PRETTY_FUNCTION__
,
2875 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2877 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2879 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2882 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2883 struct interface
*br_if
,
2884 struct ethaddr
*mac
, vlanid_t vid
)
2887 struct zebra_dplane_info dp_info
;
2889 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2891 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2893 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2899 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2906 * Netlink-specific handler for MAC updates using dataplane context object.
2908 static enum zebra_dplane_result
2909 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2911 uint8_t protocol
= RTPROT_ZEBRA
;
2919 int vid_present
= 0;
2921 struct in_addr vtep_ip
;
2924 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2929 memset(&req
, 0, sizeof(req
));
2931 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2932 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2933 if (cmd
== RTM_NEWNEIGH
)
2934 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2935 req
.n
.nlmsg_type
= cmd
;
2936 req
.ndm
.ndm_family
= AF_BRIDGE
;
2937 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2938 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2940 if (dplane_ctx_mac_is_sticky(ctx
))
2941 req
.ndm
.ndm_state
|= NUD_NOARP
;
2943 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2945 addattr_l(&req
.n
, sizeof(req
),
2946 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2947 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2948 dplane_ctx_mac_get_addr(ctx
), 6);
2949 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2951 dst_alen
= 4; // TODO: hardcoded
2952 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2953 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2955 vid
= dplane_ctx_mac_get_vlan(ctx
);
2958 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2961 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
,
2962 dplane_ctx_mac_get_br_ifindex(ctx
));
2964 if (IS_ZEBRA_DEBUG_KERNEL
) {
2965 char ipbuf
[PREFIX_STRLEN
];
2966 char buf
[ETHER_ADDR_STRLEN
];
2967 char dst_buf
[PREFIX_STRLEN
+ 10];
2971 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2975 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2976 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2977 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2979 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2980 nl_msg_type_to_str(cmd
),
2981 nl_family_to_str(req
.ndm
.ndm_family
),
2982 dplane_ctx_get_ifname(ctx
),
2983 dplane_ctx_get_ifindex(ctx
), vid_buf
,
2984 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2988 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
2989 dplane_ctx_get_ns(ctx
), 0);
2991 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2993 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2997 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2998 * 5549 support, re-install them.
3000 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3001 struct interface
*ifp
, struct ipaddr
*ip
,
3004 if (ndm
->ndm_family
!= AF_INET
)
3007 if (!zif
->v6_2_v4_ll_neigh_entry
)
3010 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3013 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3014 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3019 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3023 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3026 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3029 struct interface
*ifp
;
3030 struct zebra_if
*zif
;
3031 struct rtattr
*tb
[NDA_MAX
+ 1];
3032 struct interface
*link_if
;
3035 char buf
[ETHER_ADDR_STRLEN
];
3036 char buf2
[INET6_ADDRSTRLEN
];
3037 int mac_present
= 0;
3041 ndm
= NLMSG_DATA(h
);
3043 /* The interface should exist. */
3044 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3046 if (!ifp
|| !ifp
->info
)
3049 zif
= (struct zebra_if
*)ifp
->info
;
3051 /* Parse attributes and extract fields of interest. */
3052 memset(tb
, 0, sizeof tb
);
3053 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3056 zlog_debug("%s family %s IF %s(%u) - no DST",
3057 nl_msg_type_to_str(h
->nlmsg_type
),
3058 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3063 memset(&ip
, 0, sizeof(struct ipaddr
));
3064 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3065 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3067 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3068 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3069 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3070 if (IS_ZEBRA_DEBUG_KERNEL
)
3072 "\tNeighbor Entry Received is a 5549 entry, finished");
3076 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3077 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3078 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3080 /* The neighbor is present on an SVI. From this, we locate the
3082 * bridge because we're only interested in neighbors on a VxLAN bridge.
3083 * The bridge is located based on the nature of the SVI:
3084 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3086 * and is linked to the bridge
3087 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3091 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3092 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3096 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3099 if (IS_ZEBRA_DEBUG_KERNEL
)
3101 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3105 memset(&mac
, 0, sizeof(struct ethaddr
));
3106 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3107 if (tb
[NDA_LLADDR
]) {
3108 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3109 if (IS_ZEBRA_DEBUG_KERNEL
)
3111 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3116 ifp
->name
, ndm
->ndm_ifindex
,
3117 (unsigned long)RTA_PAYLOAD(
3123 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3126 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3127 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3129 if (IS_ZEBRA_DEBUG_KERNEL
)
3131 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3132 nl_msg_type_to_str(h
->nlmsg_type
),
3133 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3135 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3137 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3139 ndm
->ndm_state
, ndm
->ndm_flags
);
3141 /* If the neighbor state is valid for use, process as an add or
3143 * else process as a delete. Note that the delete handling may
3145 * in re-adding the neighbor if it is a valid "remote" neighbor.
3147 if (ndm
->ndm_state
& NUD_VALID
)
3148 return zebra_vxlan_handle_kernel_neigh_update(
3149 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3152 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3155 if (IS_ZEBRA_DEBUG_KERNEL
)
3156 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3157 nl_msg_type_to_str(h
->nlmsg_type
),
3158 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3160 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3162 /* Process the delete - it may result in re-adding the neighbor if it is
3163 * a valid "remote" neighbor.
3165 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3168 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3173 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3176 /* Length validity. */
3177 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3181 /* We are interested only in AF_INET or AF_INET6 notifications. */
3182 ndm
= NLMSG_DATA(h
);
3183 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3186 return netlink_neigh_change(h
, len
);
3189 /* Request for IP neighbor information from the kernel */
3190 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3191 int type
, ifindex_t ifindex
)
3199 /* Form the request, specifying filter (rtattr) if needed. */
3200 memset(&req
, 0, sizeof(req
));
3201 req
.n
.nlmsg_type
= type
;
3202 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3203 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3204 req
.ndm
.ndm_family
= family
;
3206 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3208 return netlink_request(netlink_cmd
, &req
.n
);
3212 * IP Neighbor table read using netlink interface. This is invoked
3215 int netlink_neigh_read(struct zebra_ns
*zns
)
3218 struct zebra_dplane_info dp_info
;
3220 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3222 /* Get IP neighbor table. */
3223 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3227 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3234 * IP Neighbor table read using netlink interface. This is for a specific
3237 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3240 struct zebra_dplane_info dp_info
;
3242 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3244 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3248 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3255 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3256 * read using netlink interface.
3258 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3259 int type
, struct ipaddr
*ip
,
3269 /* Form the request, specifying filter (rtattr) if needed. */
3270 memset(&req
, 0, sizeof(req
));
3271 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3272 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3273 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3274 req
.ndm
.ndm_ifindex
= ifindex
;
3276 if (IS_IPADDR_V4(ip
)) {
3277 ipa_len
= IPV4_MAX_BYTELEN
;
3278 req
.ndm
.ndm_family
= AF_INET
;
3281 ipa_len
= IPV6_MAX_BYTELEN
;
3282 req
.ndm
.ndm_family
= AF_INET6
;
3285 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3287 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
3290 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3291 struct interface
*vlan_if
)
3294 struct zebra_ns
*zns
;
3295 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3296 char buf
[INET6_ADDRSTRLEN
];
3297 struct zebra_dplane_info dp_info
;
3301 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3303 if (IS_ZEBRA_DEBUG_KERNEL
)
3304 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3305 __PRETTY_FUNCTION__
, vlan_if
->name
,
3307 ipaddr2str(ip
, buf
, sizeof(buf
)),
3310 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3315 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3321 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3326 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3329 /* Length validity. */
3330 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3332 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3333 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
3334 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3338 /* Is this a notification for the MAC FDB or IP neighbor table? */
3339 ndm
= NLMSG_DATA(h
);
3340 if (ndm
->ndm_family
== AF_BRIDGE
)
3341 return netlink_macfdb_change(h
, len
, ns_id
);
3343 if (ndm
->ndm_type
!= RTN_UNICAST
)
3346 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3347 return netlink_ipneigh_change(h
, len
, ns_id
);
3350 EC_ZEBRA_UNKNOWN_FAMILY
,
3351 "Invalid address family: %u received from kernel neighbor change: %s",
3352 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3360 * Utility neighbor-update function, using info from dplane context.
3362 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3365 uint8_t protocol
= RTPROT_ZEBRA
;
3372 char buf
[INET6_ADDRSTRLEN
];
3373 char buf2
[ETHER_ADDR_STRLEN
];
3374 const struct ipaddr
*ip
;
3375 const struct ethaddr
*mac
;
3379 memset(&req
, 0, sizeof(req
));
3381 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3382 mac
= dplane_ctx_neigh_get_mac(ctx
);
3383 if (is_zero_mac(mac
))
3386 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3387 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3389 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3390 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3391 if (cmd
== RTM_NEWNEIGH
)
3392 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3393 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
3394 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3395 req
.ndm
.ndm_state
= state
;
3396 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3397 req
.ndm
.ndm_type
= RTN_UNICAST
;
3398 req
.ndm
.ndm_flags
= flags
;
3400 addattr_l(&req
.n
, sizeof(req
),
3401 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
3402 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3403 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3405 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3407 if (IS_ZEBRA_DEBUG_KERNEL
)
3408 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3409 nl_msg_type_to_str(cmd
),
3410 nl_family_to_str(req
.ndm
.ndm_family
),
3411 dplane_ctx_get_ifname(ctx
),
3412 dplane_ctx_get_ifindex(ctx
),
3413 ipaddr2str(ip
, buf
, sizeof(buf
)),
3414 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
3418 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3419 dplane_ctx_get_ns(ctx
), 0);
3423 * Update MAC, using dataplane context object.
3425 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
3427 return netlink_macfdb_update_ctx(ctx
);
3430 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
3434 switch (dplane_ctx_get_op(ctx
)) {
3435 case DPLANE_OP_NEIGH_INSTALL
:
3436 case DPLANE_OP_NEIGH_UPDATE
:
3437 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
3439 case DPLANE_OP_NEIGH_DELETE
:
3440 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
3442 case DPLANE_OP_VTEP_ADD
:
3443 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
);
3445 case DPLANE_OP_VTEP_DELETE
:
3446 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
);
3453 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
3457 * MPLS label forwarding table change via netlink interface, using dataplane
3458 * context information.
3460 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
3463 const zebra_nhlfe_t
*nhlfe
;
3464 struct nexthop
*nexthop
= NULL
;
3465 unsigned int nexthop_num
;
3466 const char *routedesc
;
3472 char buf
[NL_PKT_BUF_SIZE
];
3475 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
3478 * Count # nexthops so we can decide whether to use singlepath
3479 * or multipath case.
3482 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
3483 nexthop
= nhlfe
->nexthop
;
3486 if (cmd
== RTM_NEWROUTE
) {
3487 /* Count all selected NHLFEs */
3488 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3489 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3492 /* Count all installed NHLFEs */
3493 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3494 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3499 if ((nexthop_num
== 0) ||
3500 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3503 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3504 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3505 req
.n
.nlmsg_type
= cmd
;
3506 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3508 req
.r
.rtm_family
= AF_MPLS
;
3509 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3510 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3511 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3512 req
.r
.rtm_type
= RTN_UNICAST
;
3514 if (cmd
== RTM_NEWROUTE
) {
3515 /* We do a replace to handle update. */
3516 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3518 /* set the protocol value if installing */
3519 route_type
= re_type_from_lsp_type(
3520 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3521 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3524 /* Fill destination */
3525 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3526 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3528 /* Fill nexthops (paths) based on single-path or multipath. The paths
3529 * chosen depend on the operation.
3531 if (nexthop_num
== 1) {
3532 routedesc
= "single-path";
3533 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3537 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3538 nhlfe
; nhlfe
= nhlfe
->next
) {
3539 nexthop
= nhlfe
->nexthop
;
3543 if ((cmd
== RTM_NEWROUTE
3544 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3545 && CHECK_FLAG(nexthop
->flags
,
3546 NEXTHOP_FLAG_ACTIVE
)))
3547 || (cmd
== RTM_DELROUTE
3548 && (CHECK_FLAG(nhlfe
->flags
,
3549 NHLFE_FLAG_INSTALLED
)
3550 && CHECK_FLAG(nexthop
->flags
,
3551 NEXTHOP_FLAG_FIB
)))) {
3552 /* Add the gateway */
3553 _netlink_mpls_build_singlepath(
3562 } else { /* Multipath case */
3563 char buf
[NL_PKT_BUF_SIZE
];
3564 struct rtattr
*rta
= (void *)buf
;
3565 struct rtnexthop
*rtnh
;
3566 const union g_addr
*src1
= NULL
;
3568 rta
->rta_type
= RTA_MULTIPATH
;
3569 rta
->rta_len
= RTA_LENGTH(0);
3570 rtnh
= RTA_DATA(rta
);
3572 routedesc
= "multipath";
3573 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3577 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3578 nhlfe
; nhlfe
= nhlfe
->next
) {
3579 nexthop
= nhlfe
->nexthop
;
3583 if ((cmd
== RTM_NEWROUTE
3584 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3585 && CHECK_FLAG(nexthop
->flags
,
3586 NEXTHOP_FLAG_ACTIVE
)))
3587 || (cmd
== RTM_DELROUTE
3588 && (CHECK_FLAG(nhlfe
->flags
,
3589 NHLFE_FLAG_INSTALLED
)
3590 && CHECK_FLAG(nexthop
->flags
,
3591 NEXTHOP_FLAG_FIB
)))) {
3594 /* Build the multipath */
3595 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3598 rtnh
= RTNH_NEXT(rtnh
);
3602 /* Add the multipath */
3603 if (rta
->rta_len
> RTA_LENGTH(0))
3604 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3605 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3608 /* Talk to netlink socket. */
3609 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3610 dplane_ctx_get_ns(ctx
), 0);
3612 #endif /* HAVE_NETLINK */