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 /* We capture whether the current kernel supports nexthop ids; by
79 * default, we'll use them if possible. There's also a configuration
80 * available to _disable_ use of kernel nexthops.
82 static bool supports_nh
;
90 static const char ipv4_ll_buf
[16] = "169.254.0.1";
91 static struct in_addr ipv4_ll
;
93 /* Helper to control use of kernel-level nexthop ids */
94 static bool kernel_nexthops_supported(void)
96 return (supports_nh
&& zebra_nhg_kernel_nexthops_enabled());
100 * The ipv4_ll data structure is used for all 5549
101 * additions to the kernel. Let's figure out the
102 * correct value one time instead for every
103 * install/remove of a 5549 type route
105 void rt_netlink_init(void)
107 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
111 * Mapping from dataplane neighbor flags to netlink flags
113 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
117 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
118 flags
|= NTF_EXT_LEARNED
;
119 if (dplane_flags
& DPLANE_NTF_ROUTER
)
126 * Mapping from dataplane neighbor state to netlink state
128 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
132 if (dplane_state
& DPLANE_NUD_REACHABLE
)
133 state
|= NUD_REACHABLE
;
134 if (dplane_state
& DPLANE_NUD_STALE
)
136 if (dplane_state
& DPLANE_NUD_NOARP
)
138 if (dplane_state
& DPLANE_NUD_PROBE
)
145 static inline int is_selfroute(int proto
)
147 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
148 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
149 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
150 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
151 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
152 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
153 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
160 static inline int zebra2proto(int proto
)
163 case ZEBRA_ROUTE_BABEL
:
164 proto
= RTPROT_BABEL
;
166 case ZEBRA_ROUTE_BGP
:
169 case ZEBRA_ROUTE_OSPF
:
170 case ZEBRA_ROUTE_OSPF6
:
173 case ZEBRA_ROUTE_STATIC
:
174 proto
= RTPROT_ZSTATIC
;
176 case ZEBRA_ROUTE_ISIS
:
179 case ZEBRA_ROUTE_RIP
:
182 case ZEBRA_ROUTE_RIPNG
:
183 proto
= RTPROT_RIPNG
;
185 case ZEBRA_ROUTE_NHRP
:
188 case ZEBRA_ROUTE_EIGRP
:
189 proto
= RTPROT_EIGRP
;
191 case ZEBRA_ROUTE_LDP
:
194 case ZEBRA_ROUTE_SHARP
:
195 proto
= RTPROT_SHARP
;
197 case ZEBRA_ROUTE_PBR
:
200 case ZEBRA_ROUTE_OPENFABRIC
:
201 proto
= RTPROT_OPENFABRIC
;
203 case ZEBRA_ROUTE_TABLE
:
204 case ZEBRA_ROUTE_NHG
:
205 proto
= RTPROT_ZEBRA
;
209 * When a user adds a new protocol this will show up
210 * to let them know to do something about it. This
211 * is intentionally a warn because we should see
212 * this as part of development of a new protocol
215 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
216 __PRETTY_FUNCTION__
, proto
);
217 proto
= RTPROT_ZEBRA
;
224 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
228 proto
= ZEBRA_ROUTE_BABEL
;
231 proto
= ZEBRA_ROUTE_BGP
;
234 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
238 proto
= ZEBRA_ROUTE_ISIS
;
241 proto
= ZEBRA_ROUTE_RIP
;
244 proto
= ZEBRA_ROUTE_RIPNG
;
247 proto
= ZEBRA_ROUTE_NHRP
;
250 proto
= ZEBRA_ROUTE_EIGRP
;
253 proto
= ZEBRA_ROUTE_LDP
;
257 proto
= ZEBRA_ROUTE_STATIC
;
260 proto
= ZEBRA_ROUTE_SHARP
;
263 proto
= ZEBRA_ROUTE_PBR
;
265 case RTPROT_OPENFABRIC
:
266 proto
= ZEBRA_ROUTE_OPENFABRIC
;
270 proto
= ZEBRA_ROUTE_NHG
;
273 /* Intentional fall thru */
276 * When a user adds a new protocol this will show up
277 * to let them know to do something about it. This
278 * is intentionally a warn because we should see
279 * this as part of development of a new protocol
282 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
283 __PRETTY_FUNCTION__
, proto
);
284 proto
= ZEBRA_ROUTE_KERNEL
;
291 Pending: create an efficient table_id (in a tree/hash) based lookup)
293 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
296 struct zebra_vrf
*zvrf
;
298 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
302 /* case vrf with netns : match the netnsid */
303 if (vrf_is_backend_netns()) {
304 if (ns_id
== zvrf_id(zvrf
))
305 return zvrf_id(zvrf
);
307 /* VRF is VRF_BACKEND_VRF_LITE */
308 if (zvrf
->table_id
!= table_id
)
310 return zvrf_id(zvrf
);
318 * @parse_encap_mpls() - Parses encapsulated mpls attributes
319 * @tb: Pointer to rtattr to look for nested items in.
320 * @labels: Pointer to store labels in.
322 * Return: Number of mpls labels found.
324 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
326 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
327 mpls_lse_t
*lses
= NULL
;
332 mpls_label_t label
= 0;
334 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
335 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
336 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
337 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
338 labels
[num_labels
++] = label
;
344 static struct nexthop
345 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
346 enum blackhole_type bh_type
, int index
, void *prefsrc
,
347 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
349 struct interface
*ifp
= NULL
;
350 struct nexthop nh
= {0};
351 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
354 vrf_id_t nh_vrf_id
= vrf_id
;
355 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
357 if (bh_type
== BLACKHOLE_UNSPEC
) {
359 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
360 else if (index
&& gate
)
361 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
362 : NEXTHOP_TYPE_IPV6_IFINDEX
;
363 else if (!index
&& gate
)
364 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
367 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
368 nh
.bh_type
= bh_type
;
371 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
372 nh
.bh_type
= bh_type
;
376 memcpy(&nh
.src
, prefsrc
, sz
);
378 memcpy(&nh
.gate
, gate
, sz
);
381 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
383 nh_vrf_id
= ifp
->vrf_id
;
385 nh
.vrf_id
= nh_vrf_id
;
387 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
388 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
389 == LWTUNNEL_ENCAP_MPLS
) {
390 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
393 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
394 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
397 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
402 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
403 struct nexthop_group
*ng
,
405 struct rtnexthop
*rtnh
,
407 void *prefsrc
, vrf_id_t vrf_id
)
410 struct interface
*ifp
= NULL
;
413 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
415 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
417 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
418 vrf_id_t nh_vrf_id
= vrf_id
;
421 struct nexthop
*nh
= NULL
;
423 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
426 index
= rtnh
->rtnh_ifindex
;
429 * Yes we are looking this up
430 * for every nexthop and just
431 * using the last one looked
434 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
437 nh_vrf_id
= ifp
->vrf_id
;
440 EC_ZEBRA_UNKNOWN_INTERFACE
,
441 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
442 __PRETTY_FUNCTION__
, index
);
443 nh_vrf_id
= VRF_DEFAULT
;
448 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
449 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
451 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
452 rtnh
->rtnh_len
- sizeof(*rtnh
));
453 if (rtnh_tb
[RTA_GATEWAY
])
454 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
455 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
456 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
457 == LWTUNNEL_ENCAP_MPLS
) {
458 num_labels
= parse_encap_mpls(
459 rtnh_tb
[RTA_ENCAP
], labels
);
463 if (gate
&& rtm
->rtm_family
== AF_INET
) {
465 nh
= nexthop_from_ipv4_ifindex(
466 gate
, prefsrc
, index
, nh_vrf_id
);
468 nh
= nexthop_from_ipv4(gate
, prefsrc
,
470 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
472 nh
= nexthop_from_ipv6_ifindex(
473 gate
, index
, nh_vrf_id
);
475 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
477 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
480 nh
->weight
= rtnh
->rtnh_hops
+ 1;
483 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
486 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
487 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
489 /* Add to temporary list */
490 nexthop_group_add_sorted(ng
, nh
);
493 if (rtnh
->rtnh_len
== 0)
496 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
497 rtnh
= RTNH_NEXT(rtnh
);
500 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
505 /* Looking up routing table by netlink interface. */
506 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
511 struct rtattr
*tb
[RTA_MAX
+ 1];
514 struct prefix_ipv6 src_p
= {};
517 char anyaddr
[16] = {0};
519 int proto
= ZEBRA_ROUTE_KERNEL
;
524 uint8_t distance
= 0;
530 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
531 void *src
= NULL
; /* IPv6 srcdest source prefix */
532 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
536 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
538 switch (rtm
->rtm_type
) {
542 bh_type
= BLACKHOLE_NULL
;
544 case RTN_UNREACHABLE
:
545 bh_type
= BLACKHOLE_REJECT
;
548 bh_type
= BLACKHOLE_ADMINPROHIB
;
551 if (IS_ZEBRA_DEBUG_KERNEL
)
552 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
553 nl_rttype_to_str(rtm
->rtm_type
),
558 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
560 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
561 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
562 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
566 memset(tb
, 0, sizeof tb
);
567 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
569 if (rtm
->rtm_flags
& RTM_F_CLONED
)
571 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
573 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
576 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
577 && h
->nlmsg_type
== RTM_NEWROUTE
) {
578 if (IS_ZEBRA_DEBUG_KERNEL
)
579 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
584 /* We don't care about change notifications for the MPLS table. */
585 /* TODO: Revisit this. */
586 if (rtm
->rtm_family
== AF_MPLS
)
589 /* Table corresponding to route. */
591 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
593 table
= rtm
->rtm_table
;
596 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
597 if (vrf_id
== VRF_DEFAULT
) {
598 if (!is_zebra_valid_kernel_table(table
)
599 && !is_zebra_main_routing_table(table
))
603 /* Route which inserted by Zebra. */
604 if (is_selfroute(rtm
->rtm_protocol
)) {
605 flags
|= ZEBRA_FLAG_SELFROUTE
;
606 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
609 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
612 dest
= RTA_DATA(tb
[RTA_DST
]);
617 src
= RTA_DATA(tb
[RTA_SRC
]);
622 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
625 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
628 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
630 if (tb
[RTA_PRIORITY
])
631 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
633 #if defined(SUPPORT_REALMS)
635 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
638 if (tb
[RTA_METRICS
]) {
639 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
641 memset(mxrta
, 0, sizeof mxrta
);
642 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
643 RTA_PAYLOAD(tb
[RTA_METRICS
]));
646 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
649 if (rtm
->rtm_family
== AF_INET
) {
651 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
653 "Invalid destination prefix length: %u received from kernel route change",
657 memcpy(&p
.u
.prefix4
, dest
, 4);
658 p
.prefixlen
= rtm
->rtm_dst_len
;
660 if (rtm
->rtm_src_len
!= 0) {
661 char buf
[PREFIX_STRLEN
];
663 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
664 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
665 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
669 /* Force debug below to not display anything for source */
671 } else if (rtm
->rtm_family
== AF_INET6
) {
673 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
675 "Invalid destination prefix length: %u received from kernel route change",
679 memcpy(&p
.u
.prefix6
, dest
, 16);
680 p
.prefixlen
= rtm
->rtm_dst_len
;
682 src_p
.family
= AF_INET6
;
683 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
685 "Invalid source prefix length: %u received from kernel route change",
689 memcpy(&src_p
.prefix
, src
, 16);
690 src_p
.prefixlen
= rtm
->rtm_src_len
;
694 * For ZEBRA_ROUTE_KERNEL types:
696 * The metric/priority of the route received from the kernel
697 * is a 32 bit number. We are going to interpret the high
698 * order byte as the Admin Distance and the low order 3 bytes
701 * This will allow us to do two things:
702 * 1) Allow the creation of kernel routes that can be
703 * overridden by zebra.
704 * 2) Allow the old behavior for 'most' kernel route types
705 * if a user enters 'ip route ...' v4 routes get a metric
706 * of 0 and v6 routes get a metric of 1024. Both of these
707 * values will end up with a admin distance of 0, which
708 * will cause them to win for the purposes of zebra.
710 if (proto
== ZEBRA_ROUTE_KERNEL
) {
711 distance
= (metric
>> 24) & 0xFF;
712 metric
= (metric
& 0x00FFFFFF);
715 if (IS_ZEBRA_DEBUG_KERNEL
) {
716 char buf
[PREFIX_STRLEN
];
717 char buf2
[PREFIX_STRLEN
];
718 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
719 nl_msg_type_to_str(h
->nlmsg_type
),
720 prefix2str(&p
, buf
, sizeof(buf
)),
721 src_p
.prefixlen
? " from " : "",
723 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
725 vrf_id
, table
, metric
, distance
);
729 if (rtm
->rtm_family
== AF_INET6
)
732 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
734 if (!tb
[RTA_MULTIPATH
]) {
735 struct nexthop nh
= {0};
738 nh
= parse_nexthop_unicast(
739 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
742 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
743 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
746 /* This is a multipath route */
747 struct route_entry
*re
;
748 struct nexthop_group
*ng
= NULL
;
749 struct rtnexthop
*rtnh
=
750 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
752 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
754 re
->distance
= distance
;
760 re
->uptime
= monotime(NULL
);
767 /* Use temporary list of nexthops; parse
768 * message payload's nexthops.
770 ng
= nexthop_group_new();
772 parse_multipath_nexthops_unicast(
773 ns_id
, ng
, rtm
, rtnh
, tb
,
776 zserv_nexthop_num_warn(
777 __func__
, (const struct prefix
*)&p
,
781 nexthop_group_delete(&ng
);
787 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
794 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
795 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
798 if (!tb
[RTA_MULTIPATH
]) {
801 nh
= parse_nexthop_unicast(
802 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
804 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
805 flags
, &p
, &src_p
, &nh
, 0, table
,
806 metric
, distance
, true);
808 /* XXX: need to compare the entire list of
809 * nexthops here for NLM_F_APPEND stupidity */
810 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
811 flags
, &p
, &src_p
, NULL
, 0, table
,
812 metric
, distance
, true);
820 static struct mcast_route_data
*mroute
= NULL
;
822 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
823 ns_id_t ns_id
, int startup
)
827 struct rtattr
*tb
[RTA_MAX
+ 1];
828 struct mcast_route_data
*m
;
829 struct mcast_route_data mr
;
836 char oif_list
[256] = "\0";
843 memset(&mr
, 0, sizeof(mr
));
849 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
851 memset(tb
, 0, sizeof tb
);
852 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
855 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
857 table
= rtm
->rtm_table
;
859 vrf
= vrf_lookup_by_table(table
, ns_id
);
862 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
865 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
868 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
871 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
873 if (tb
[RTA_MULTIPATH
]) {
874 struct rtnexthop
*rtnh
=
875 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
877 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
879 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
882 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
885 if (rtnh
->rtnh_len
== 0)
888 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
889 rtnh
= RTNH_NEXT(rtnh
);
893 if (IS_ZEBRA_DEBUG_KERNEL
) {
894 struct interface
*ifp
= NULL
;
895 struct zebra_vrf
*zvrf
= NULL
;
897 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
898 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
899 for (count
= 0; count
< oif_count
; count
++) {
900 ifp
= if_lookup_by_index(oif
[count
], vrf
);
903 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
905 strlcat(oif_list
, temp
, sizeof(oif_list
));
907 zvrf
= zebra_vrf_lookup_by_id(vrf
);
908 ifp
= if_lookup_by_index(iif
, vrf
);
910 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
911 (zvrf
? zvrf
->vrf
->name
: "Unknown"), vrf
,
912 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
913 ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
919 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
926 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
927 /* If this is not route add/delete message print warning. */
928 zlog_debug("Kernel message: %s NS %u",
929 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
933 if (!(rtm
->rtm_family
== AF_INET
||
934 rtm
->rtm_family
== AF_INET6
||
935 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
937 EC_ZEBRA_UNKNOWN_FAMILY
,
938 "Invalid address family: %u received from kernel route change: %s",
939 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
943 /* Connected route. */
944 if (IS_ZEBRA_DEBUG_KERNEL
)
945 zlog_debug("%s %s %s proto %s NS %u",
946 nl_msg_type_to_str(h
->nlmsg_type
),
947 nl_family_to_str(rtm
->rtm_family
),
948 nl_rttype_to_str(rtm
->rtm_type
),
949 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
952 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
954 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
957 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
961 if (rtm
->rtm_type
== RTN_MULTICAST
)
962 netlink_route_change_read_multicast(h
, ns_id
, startup
);
964 netlink_route_change_read_unicast(h
, ns_id
, startup
);
968 /* Request for specific route information from the kernel */
969 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
976 /* Form the request, specifying filter (rtattr) if needed. */
977 memset(&req
, 0, sizeof(req
));
978 req
.n
.nlmsg_type
= type
;
979 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
980 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
981 req
.rtm
.rtm_family
= family
;
983 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
986 /* Routing table read function using netlink interface. Only called
988 int netlink_route_read(struct zebra_ns
*zns
)
991 struct zebra_dplane_info dp_info
;
993 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
995 /* Get IPv4 routing table. */
996 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
999 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1000 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1004 /* Get IPv6 routing table. */
1005 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1008 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1009 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1016 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
1018 struct nlmsghdr
*nlmsg
,
1019 size_t req_size
, int bytelen
,
1020 const struct nexthop
*nexthop
)
1022 if (route_family
== AF_MPLS
) {
1023 struct gw_family_t gw_fam
;
1025 gw_fam
.family
= gw_family
;
1026 if (gw_family
== AF_INET
)
1027 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1029 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1030 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1033 if (gw_family
== AF_INET
)
1034 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1035 &nexthop
->gate
.ipv4
, bytelen
);
1037 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1038 &nexthop
->gate
.ipv6
, bytelen
);
1042 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
1045 struct rtnexthop
*rtnh
,
1046 size_t req_size
, int bytelen
,
1047 const struct nexthop
*nexthop
)
1049 if (route_family
== AF_MPLS
) {
1050 struct gw_family_t gw_fam
;
1052 gw_fam
.family
= gw_family
;
1053 if (gw_family
== AF_INET
)
1054 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1056 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1057 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
1059 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
1061 if (gw_family
== AF_INET
)
1062 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1063 &nexthop
->gate
.ipv4
, bytelen
);
1065 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1066 &nexthop
->gate
.ipv6
, bytelen
);
1067 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1071 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1072 mpls_lse_t
*out_lse
, char *label_buf
,
1073 size_t label_buf_size
)
1075 char label_buf1
[20];
1078 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1079 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1082 if (IS_ZEBRA_DEBUG_KERNEL
) {
1084 sprintf(label_buf
, "label %u",
1085 nh_label
->label
[i
]);
1087 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1088 strlcat(label_buf
, label_buf1
, label_buf_size
);
1092 out_lse
[num_labels
] =
1093 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1100 /* This function takes a nexthop as argument and adds
1101 * the appropriate netlink attributes to an existing
1104 * @param routedesc: Human readable description of route type
1105 * (direct/recursive, single-/multipath)
1106 * @param bytelen: Length of addresses in bytes.
1107 * @param nexthop: Nexthop information
1108 * @param nlmsg: nlmsghdr structure to fill in.
1109 * @param req_size: The size allocated for the message.
1111 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
1112 const struct nexthop
*nexthop
,
1113 struct nlmsghdr
*nlmsg
,
1114 struct rtmsg
*rtmsg
,
1115 size_t req_size
, int cmd
)
1118 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1119 char label_buf
[256];
1125 * label_buf is *only* currently used within debugging.
1126 * As such when we assign it we are guarding it inside
1127 * a debug test. If you want to change this make sure
1128 * you fix this assumption
1130 label_buf
[0] = '\0';
1132 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1136 /* Set the BoS bit */
1137 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1139 if (rtmsg
->rtm_family
== AF_MPLS
)
1140 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1141 num_labels
* sizeof(mpls_lse_t
));
1143 struct rtattr
*nest
;
1144 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1146 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1148 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1149 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1150 num_labels
* sizeof(mpls_lse_t
));
1151 addattr_nest_end(nlmsg
, nest
);
1155 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1156 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1158 if (rtmsg
->rtm_family
== AF_INET
1159 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1160 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1161 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1162 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1163 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1165 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1166 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1167 &nexthop
->rmap_src
.ipv4
, bytelen
);
1168 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1169 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1170 &nexthop
->src
.ipv4
, bytelen
);
1172 if (IS_ZEBRA_DEBUG_KERNEL
)
1174 " 5549: _netlink_route_build_singlepath() (%s): "
1175 "nexthop via %s %s if %u(%u)",
1176 routedesc
, ipv4_ll_buf
, label_buf
,
1177 nexthop
->ifindex
, nexthop
->vrf_id
);
1181 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1182 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1183 /* Send deletes to the kernel without specifying the next-hop */
1184 if (cmd
!= RTM_DELROUTE
)
1185 _netlink_route_nl_add_gateway_info(
1186 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1189 if (cmd
== RTM_NEWROUTE
) {
1190 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1191 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1192 &nexthop
->rmap_src
.ipv4
, bytelen
);
1193 else if (nexthop
->src
.ipv4
.s_addr
)
1194 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1195 &nexthop
->src
.ipv4
, bytelen
);
1198 if (IS_ZEBRA_DEBUG_KERNEL
)
1200 "netlink_route_multipath() (%s): "
1201 "nexthop via %s %s if %u(%u)",
1202 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1203 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1206 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1207 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1208 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1209 nlmsg
, req_size
, bytelen
,
1212 if (cmd
== RTM_NEWROUTE
) {
1213 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1214 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1215 &nexthop
->rmap_src
.ipv6
, bytelen
);
1216 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1217 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1218 &nexthop
->src
.ipv6
, bytelen
);
1221 if (IS_ZEBRA_DEBUG_KERNEL
)
1223 "netlink_route_multipath() (%s): "
1224 "nexthop via %s %s if %u(%u)",
1225 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1226 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1230 * We have the ifindex so we should always send it
1231 * This is especially useful if we are doing route
1234 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1235 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1237 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1238 if (cmd
== RTM_NEWROUTE
) {
1239 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1240 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1241 &nexthop
->rmap_src
.ipv4
, bytelen
);
1242 else if (nexthop
->src
.ipv4
.s_addr
)
1243 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1244 &nexthop
->src
.ipv4
, bytelen
);
1247 if (IS_ZEBRA_DEBUG_KERNEL
)
1249 "netlink_route_multipath() (%s): "
1250 "nexthop via if %u(%u)",
1251 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1255 /* This function takes a nexthop as argument and
1256 * appends to the given rtattr/rtnexthop pair the
1257 * representation of the nexthop. If the nexthop
1258 * defines a preferred source, the src parameter
1259 * will be modified to point to that src, otherwise
1260 * it will be kept unmodified.
1262 * @param routedesc: Human readable description of route type
1263 * (direct/recursive, single-/multipath)
1264 * @param bytelen: Length of addresses in bytes.
1265 * @param nexthop: Nexthop information
1266 * @param rta: rtnetlink attribute structure
1267 * @param rtnh: pointer to an rtnetlink nexthop structure
1268 * @param src: pointer pointing to a location where
1269 * the prefsrc should be stored.
1271 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1272 const struct nexthop
*nexthop
,
1274 struct rtnexthop
*rtnh
,
1275 struct rtmsg
*rtmsg
,
1276 const union g_addr
**src
)
1278 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1279 char label_buf
[256];
1282 rtnh
->rtnh_len
= sizeof(*rtnh
);
1283 rtnh
->rtnh_flags
= 0;
1284 rtnh
->rtnh_hops
= 0;
1285 rta
->rta_len
+= rtnh
->rtnh_len
;
1290 * label_buf is *only* currently used within debugging.
1291 * As such when we assign it we are guarding it inside
1292 * a debug test. If you want to change this make sure
1293 * you fix this assumption
1295 label_buf
[0] = '\0';
1297 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1301 /* Set the BoS bit */
1302 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1304 if (rtmsg
->rtm_family
== AF_MPLS
) {
1305 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1307 num_labels
* sizeof(mpls_lse_t
));
1309 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1311 struct rtattr
*nest
;
1312 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1313 int len
= rta
->rta_len
;
1315 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1316 &encap
, sizeof(uint16_t));
1317 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1318 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1320 num_labels
* sizeof(mpls_lse_t
));
1321 rta_nest_end(rta
, nest
);
1322 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1326 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1327 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1329 if (rtmsg
->rtm_family
== AF_INET
1330 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1331 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1333 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1334 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1336 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1337 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1339 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1340 *src
= &nexthop
->rmap_src
;
1341 else if (nexthop
->src
.ipv4
.s_addr
)
1342 *src
= &nexthop
->src
;
1344 if (IS_ZEBRA_DEBUG_KERNEL
)
1346 " 5549: netlink_route_build_multipath() (%s): "
1347 "nexthop via %s %s if %u",
1348 routedesc
, ipv4_ll_buf
, label_buf
,
1353 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1354 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1355 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1356 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1358 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1359 *src
= &nexthop
->rmap_src
;
1360 else if (nexthop
->src
.ipv4
.s_addr
)
1361 *src
= &nexthop
->src
;
1363 if (IS_ZEBRA_DEBUG_KERNEL
)
1365 "netlink_route_multipath() (%s): "
1366 "nexthop via %s %s if %u",
1367 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1368 label_buf
, nexthop
->ifindex
);
1370 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1371 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1372 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1373 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1376 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1377 *src
= &nexthop
->rmap_src
;
1378 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1379 *src
= &nexthop
->src
;
1381 if (IS_ZEBRA_DEBUG_KERNEL
)
1383 "netlink_route_multipath() (%s): "
1384 "nexthop via %s %s if %u",
1385 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1386 label_buf
, nexthop
->ifindex
);
1390 * We have figured out the ifindex so we should always send it
1391 * This is especially useful if we are doing route
1394 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1395 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1398 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1399 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1400 *src
= &nexthop
->rmap_src
;
1401 else if (nexthop
->src
.ipv4
.s_addr
)
1402 *src
= &nexthop
->src
;
1404 if (IS_ZEBRA_DEBUG_KERNEL
)
1406 "netlink_route_multipath() (%s): "
1407 "nexthop via if %u",
1408 routedesc
, nexthop
->ifindex
);
1411 if (nexthop
->weight
)
1412 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1415 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1416 const zebra_nhlfe_t
*nhlfe
,
1417 struct nlmsghdr
*nlmsg
,
1418 struct rtmsg
*rtmsg
,
1419 size_t req_size
, int cmd
)
1424 family
= NHLFE_FAMILY(nhlfe
);
1425 bytelen
= (family
== AF_INET
? 4 : 16);
1426 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1427 nlmsg
, rtmsg
, req_size
, cmd
);
1432 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1433 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1434 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1439 family
= NHLFE_FAMILY(nhlfe
);
1440 bytelen
= (family
== AF_INET
? 4 : 16);
1441 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1446 /* Log debug information for netlink_route_multipath
1447 * if debug logging is enabled.
1449 * @param cmd: Netlink command which is to be processed
1450 * @param p: Prefix for which the change is due
1451 * @param family: Address family which the change concerns
1452 * @param zvrf: The vrf we are in
1453 * @param tableid: The table we are working on
1455 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1456 int family
, vrf_id_t vrfid
,
1459 if (IS_ZEBRA_DEBUG_KERNEL
) {
1460 char buf
[PREFIX_STRLEN
];
1462 "netlink_route_multipath(): %s %s vrf %u(%u)",
1463 nl_msg_type_to_str(cmd
),
1464 prefix2str(p
, buf
, sizeof(buf
)),
1469 static void _netlink_nexthop_debug(int cmd
, uint32_t id
)
1471 if (IS_ZEBRA_DEBUG_KERNEL
)
1472 zlog_debug("netlink_nexthop(): %s, id=%u",
1473 nl_msg_type_to_str(cmd
), id
);
1476 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1478 if (IS_ZEBRA_DEBUG_KERNEL
)
1479 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1480 nl_msg_type_to_str(cmd
), label
);
1483 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1484 int llalen
, ns_id_t ns_id
)
1486 uint8_t protocol
= RTPROT_ZEBRA
;
1493 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1495 memset(&req
, 0, sizeof(req
));
1497 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1498 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1499 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1500 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1502 req
.ndm
.ndm_family
= AF_INET
;
1503 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1504 req
.ndm
.ndm_ifindex
= ifindex
;
1505 req
.ndm
.ndm_type
= RTN_UNICAST
;
1507 addattr_l(&req
.n
, sizeof(req
),
1508 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
1509 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1510 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1512 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1517 * Routing table change via netlink interface, using a dataplane context object
1519 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1522 struct nexthop
*nexthop
= NULL
;
1523 unsigned int nexthop_num
;
1525 const char *routedesc
;
1528 const struct prefix
*p
, *src_p
;
1534 char buf
[NL_PKT_BUF_SIZE
];
1537 p
= dplane_ctx_get_dest(ctx
);
1538 src_p
= dplane_ctx_get_src(ctx
);
1540 family
= PREFIX_FAMILY(p
);
1542 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1544 bytelen
= (family
== AF_INET
? 4 : 16);
1546 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1547 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1549 if ((cmd
== RTM_NEWROUTE
) &&
1550 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1551 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1553 req
.n
.nlmsg_type
= cmd
;
1555 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1557 req
.r
.rtm_family
= family
;
1558 req
.r
.rtm_dst_len
= p
->prefixlen
;
1559 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1560 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1562 if (cmd
== RTM_DELROUTE
)
1563 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1565 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1568 * blackhole routes are not RTN_UNICAST, they are
1569 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1570 * so setting this value as a RTN_UNICAST would
1571 * cause the route lookup of just the prefix
1572 * to fail. So no need to specify this for
1573 * the RTM_DELROUTE case
1575 if (cmd
!= RTM_DELROUTE
)
1576 req
.r
.rtm_type
= RTN_UNICAST
;
1578 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1580 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1584 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1586 * either by the kernel or by zebra. Its purely for calculating best
1588 * by the routing protocol and for communicating with protocol peers.
1590 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1592 #if defined(SUPPORT_REALMS)
1596 if (cmd
== RTM_DELROUTE
)
1597 tag
= dplane_ctx_get_old_tag(ctx
);
1599 tag
= dplane_ctx_get_tag(ctx
);
1601 if (tag
> 0 && tag
<= 255)
1602 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1605 /* Table corresponding to this route. */
1606 table_id
= dplane_ctx_get_table(ctx
);
1608 req
.r
.rtm_table
= table_id
;
1610 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1611 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1614 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1617 * If we are not updating the route and we have received
1618 * a route delete, then all we need to fill in is the
1619 * prefix information to tell the kernel to schwack
1622 if (cmd
== RTM_DELROUTE
)
1625 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1626 char buf
[NL_PKT_BUF_SIZE
];
1627 struct rtattr
*rta
= (void *)buf
;
1628 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1629 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1631 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1633 rta
->rta_type
= RTA_METRICS
;
1634 rta
->rta_len
= RTA_LENGTH(0);
1635 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1636 RTAX_MTU
, &mtu
, sizeof(mtu
));
1637 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1641 if (kernel_nexthops_supported()) {
1642 /* Kernel supports nexthop objects */
1643 addattr32(&req
.n
, sizeof(req
), RTA_NH_ID
,
1644 dplane_ctx_get_nhe_id(ctx
));
1648 /* Count overall nexthops so we can decide whether to use singlepath
1649 * or multipath case.
1652 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1653 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1655 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1661 /* Singlepath case. */
1662 if (nexthop_num
== 1) {
1664 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1666 * So we want to cover 2 types of blackhole
1668 * 1) A normal blackhole route( ala from a static
1670 * 2) A recursively resolved blackhole route
1672 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1673 switch (nexthop
->bh_type
) {
1674 case BLACKHOLE_ADMINPROHIB
:
1675 req
.r
.rtm_type
= RTN_PROHIBIT
;
1677 case BLACKHOLE_REJECT
:
1678 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1681 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1686 if (CHECK_FLAG(nexthop
->flags
,
1687 NEXTHOP_FLAG_RECURSIVE
)) {
1692 if (family
== AF_INET
) {
1693 if (nexthop
->rmap_src
.ipv4
.s_addr
1696 nexthop
->rmap_src
.ipv4
;
1698 } else if (nexthop
->src
.ipv4
.s_addr
1704 } else if (family
== AF_INET6
) {
1705 if (!IN6_IS_ADDR_UNSPECIFIED(
1706 &nexthop
->rmap_src
.ipv6
)) {
1708 nexthop
->rmap_src
.ipv6
;
1711 !IN6_IS_ADDR_UNSPECIFIED(
1712 &nexthop
->src
.ipv6
)) {
1721 if ((cmd
== RTM_NEWROUTE
1722 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1723 routedesc
= nexthop
->rparent
1724 ? "recursive, single-path"
1727 _netlink_route_build_singlepath(
1728 routedesc
, bytelen
, nexthop
, &req
.n
,
1729 &req
.r
, sizeof(req
), cmd
);
1734 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1735 if (family
== AF_INET
)
1736 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1737 &src
.ipv4
, bytelen
);
1738 else if (family
== AF_INET6
)
1739 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1740 &src
.ipv6
, bytelen
);
1742 } else { /* Multipath case */
1743 char buf
[NL_PKT_BUF_SIZE
];
1744 struct rtattr
*rta
= (void *)buf
;
1745 struct rtnexthop
*rtnh
;
1746 const union g_addr
*src1
= NULL
;
1748 rta
->rta_type
= RTA_MULTIPATH
;
1749 rta
->rta_len
= RTA_LENGTH(0);
1750 rtnh
= RTA_DATA(rta
);
1753 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1754 if (CHECK_FLAG(nexthop
->flags
,
1755 NEXTHOP_FLAG_RECURSIVE
)) {
1756 /* This only works for IPv4 now */
1760 if (family
== AF_INET
) {
1761 if (nexthop
->rmap_src
.ipv4
.s_addr
1764 nexthop
->rmap_src
.ipv4
;
1766 } else if (nexthop
->src
.ipv4
.s_addr
1772 } else if (family
== AF_INET6
) {
1773 if (!IN6_IS_ADDR_UNSPECIFIED(
1774 &nexthop
->rmap_src
.ipv6
)) {
1776 nexthop
->rmap_src
.ipv6
;
1779 !IN6_IS_ADDR_UNSPECIFIED(
1780 &nexthop
->src
.ipv6
)) {
1790 if ((cmd
== RTM_NEWROUTE
1791 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1792 routedesc
= nexthop
->rparent
1793 ? "recursive, multipath"
1797 _netlink_route_build_multipath(
1798 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1800 rtnh
= RTNH_NEXT(rtnh
);
1802 if (!setsrc
&& src1
) {
1803 if (family
== AF_INET
)
1804 src
.ipv4
= src1
->ipv4
;
1805 else if (family
== AF_INET6
)
1806 src
.ipv6
= src1
->ipv6
;
1812 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1813 if (family
== AF_INET
)
1814 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1815 &src
.ipv4
, bytelen
);
1816 else if (family
== AF_INET6
)
1817 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1818 &src
.ipv6
, bytelen
);
1819 if (IS_ZEBRA_DEBUG_KERNEL
)
1820 zlog_debug("Setting source");
1823 if (rta
->rta_len
> RTA_LENGTH(0))
1824 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1825 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1828 /* If there is no useful nexthop then return. */
1829 if (nexthop_num
== 0) {
1830 if (IS_ZEBRA_DEBUG_KERNEL
)
1832 "netlink_route_multipath(): No useful nexthop.");
1837 /* Talk to netlink socket. */
1838 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1839 dplane_ctx_get_ns(ctx
), 0);
1842 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1844 uint32_t actual_table
;
1846 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1854 struct zebra_ns
*zns
;
1857 memset(&req
, 0, sizeof(req
));
1859 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1860 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1861 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1863 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1864 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1866 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1867 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1868 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1869 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1873 * So during the namespace cleanup we started storing
1874 * the zvrf table_id for the default table as RT_TABLE_MAIN
1875 * which is what the normal routing table for ip routing is.
1876 * This change caused this to break our lookups of sg data
1877 * because prior to this change the zvrf->table_id was 0
1878 * and when the pim multicast kernel code saw a 0,
1879 * it was auto-translated to RT_TABLE_DEFAULT. But since
1880 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1881 * and the kernel goes screw you and the delicious cookies you
1882 * are trying to give me. So now we have this little hack.
1884 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1886 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1888 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1889 &zns
->netlink_cmd
, zns
, 0);
1895 /* Char length to debug ID with */
1896 #define ID_LENGTH 10
1898 static void _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
1900 const struct nh_grp
*z_grp
,
1901 const uint8_t count
)
1903 struct nexthop_grp grp
[count
];
1904 /* Need space for max group size, "/", and null term */
1905 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
1906 char buf1
[ID_LENGTH
+ 2];
1910 memset(grp
, 0, sizeof(grp
));
1913 for (int i
= 0; i
< count
; i
++) {
1914 grp
[i
].id
= z_grp
[i
].id
;
1915 grp
[i
].weight
= z_grp
[i
].weight
- 1;
1917 if (IS_ZEBRA_DEBUG_KERNEL
) {
1919 snprintf(buf
, sizeof(buf1
), "group %u",
1922 snprintf(buf1
, sizeof(buf1
), "/%u",
1924 strlcat(buf
, buf1
, sizeof(buf
));
1928 addattr_l(n
, req_size
, NHA_GROUP
, grp
, count
* sizeof(*grp
));
1931 if (IS_ZEBRA_DEBUG_KERNEL
)
1932 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
1936 * netlink_nexthop() - Nexthop change via the netlink interface
1938 * @ctx: Dataplane ctx
1940 * Return: Result status
1942 static int netlink_nexthop(int cmd
, struct zebra_dplane_ctx
*ctx
)
1947 char buf
[NL_PKT_BUF_SIZE
];
1950 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1951 char label_buf
[256];
1953 size_t req_size
= sizeof(req
);
1955 /* Nothing to do if the kernel doesn't support nexthop objects */
1956 if (!kernel_nexthops_supported())
1959 label_buf
[0] = '\0';
1961 memset(&req
, 0, req_size
);
1963 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
1964 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1966 if (cmd
== RTM_NEWNEXTHOP
)
1967 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1969 req
.n
.nlmsg_type
= cmd
;
1970 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1972 req
.nhm
.nh_family
= AF_UNSPEC
;
1975 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
1979 EC_ZEBRA_NHG_FIB_UPDATE
,
1980 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1984 addattr32(&req
.n
, req_size
, NHA_ID
, id
);
1986 if (cmd
== RTM_NEWNEXTHOP
) {
1987 if (dplane_ctx_get_nhe_nh_grp_count(ctx
))
1988 _netlink_nexthop_build_group(
1989 &req
.n
, req_size
, id
,
1990 dplane_ctx_get_nhe_nh_grp(ctx
),
1991 dplane_ctx_get_nhe_nh_grp_count(ctx
));
1993 const struct nexthop
*nh
=
1994 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
1995 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
1998 req
.nhm
.nh_family
= AF_INET
;
1999 else if (afi
== AFI_IP6
)
2000 req
.nhm
.nh_family
= AF_INET6
;
2003 case NEXTHOP_TYPE_IPV4
:
2004 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2005 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2006 &nh
->gate
.ipv4
, IPV4_MAX_BYTELEN
);
2008 case NEXTHOP_TYPE_IPV6
:
2009 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2010 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2011 &nh
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
2013 case NEXTHOP_TYPE_BLACKHOLE
:
2014 addattr_l(&req
.n
, req_size
, NHA_BLACKHOLE
, NULL
,
2016 /* Blackhole shouldn't have anymore attributes
2019 case NEXTHOP_TYPE_IFINDEX
:
2020 /* Don't need anymore info for this */
2026 EC_ZEBRA_NHG_FIB_UPDATE
,
2027 "Context received for kernel nexthop update without an interface");
2031 addattr32(&req
.n
, req_size
, NHA_OIF
, nh
->ifindex
);
2033 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2034 req
.nhm
.nh_flags
|= RTNH_F_ONLINK
;
2037 build_label_stack(nh
->nh_label
, out_lse
,
2038 label_buf
, sizeof(label_buf
));
2041 /* Set the BoS bit */
2042 out_lse
[num_labels
- 1] |=
2043 htonl(1 << MPLS_LS_S_SHIFT
);
2046 * TODO: MPLS unsupported for now in kernel.
2048 if (req
.nhm
.nh_family
== AF_MPLS
)
2051 addattr_l(&req
.n
, req_size
, NHA_NEWDST
,
2054 * sizeof(mpls_lse_t
));
2057 struct rtattr
*nest
;
2058 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
2060 addattr_l(&req
.n
, req_size
,
2061 NHA_ENCAP_TYPE
, &encap
,
2063 nest
= addattr_nest(&req
.n
, req_size
,
2065 addattr_l(&req
.n
, req_size
,
2066 MPLS_IPTUNNEL_DST
, &out_lse
,
2068 * sizeof(mpls_lse_t
));
2069 addattr_nest_end(&req
.n
, nest
);
2074 if (IS_ZEBRA_DEBUG_KERNEL
) {
2075 char buf
[NEXTHOP_STRLEN
];
2077 snprintfrr(buf
, sizeof(buf
), "%pNHv", nh
);
2078 zlog_debug("%s: ID (%u): %s (%u) %s ", __func__
,
2079 id
, buf
, nh
->vrf_id
, label_buf
);
2083 req
.nhm
.nh_protocol
= zebra2proto(dplane_ctx_get_nhe_type(ctx
));
2085 } else if (cmd
!= RTM_DELNEXTHOP
) {
2087 EC_ZEBRA_NHG_FIB_UPDATE
,
2088 "Nexthop group kernel update command (%d) does not exist",
2093 _netlink_nexthop_debug(cmd
, id
);
2095 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2096 dplane_ctx_get_ns(ctx
), 0);
2100 * kernel_nexthop_update() - Update/delete a nexthop from the kernel
2102 * @ctx: Dataplane context
2104 * Return: Dataplane result flag
2106 enum zebra_dplane_result
kernel_nexthop_update(struct zebra_dplane_ctx
*ctx
)
2111 switch (dplane_ctx_get_op(ctx
)) {
2112 case DPLANE_OP_NH_DELETE
:
2113 cmd
= RTM_DELNEXTHOP
;
2115 case DPLANE_OP_NH_INSTALL
:
2116 case DPLANE_OP_NH_UPDATE
:
2117 cmd
= RTM_NEWNEXTHOP
;
2119 case DPLANE_OP_ROUTE_INSTALL
:
2120 case DPLANE_OP_ROUTE_UPDATE
:
2121 case DPLANE_OP_ROUTE_DELETE
:
2122 case DPLANE_OP_ROUTE_NOTIFY
:
2123 case DPLANE_OP_LSP_INSTALL
:
2124 case DPLANE_OP_LSP_UPDATE
:
2125 case DPLANE_OP_LSP_DELETE
:
2126 case DPLANE_OP_LSP_NOTIFY
:
2127 case DPLANE_OP_PW_INSTALL
:
2128 case DPLANE_OP_PW_UNINSTALL
:
2129 case DPLANE_OP_SYS_ROUTE_ADD
:
2130 case DPLANE_OP_SYS_ROUTE_DELETE
:
2131 case DPLANE_OP_ADDR_INSTALL
:
2132 case DPLANE_OP_ADDR_UNINSTALL
:
2133 case DPLANE_OP_MAC_INSTALL
:
2134 case DPLANE_OP_MAC_DELETE
:
2135 case DPLANE_OP_NEIGH_INSTALL
:
2136 case DPLANE_OP_NEIGH_UPDATE
:
2137 case DPLANE_OP_NEIGH_DELETE
:
2138 case DPLANE_OP_VTEP_ADD
:
2139 case DPLANE_OP_VTEP_DELETE
:
2140 case DPLANE_OP_NONE
:
2142 EC_ZEBRA_NHG_FIB_UPDATE
,
2143 "Context received for kernel nexthop update with incorrect OP code (%u)",
2144 dplane_ctx_get_op(ctx
));
2145 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2148 ret
= netlink_nexthop(cmd
, ctx
);
2150 return (ret
== 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2151 : ZEBRA_DPLANE_REQUEST_FAILURE
);
2155 * Update or delete a prefix from the kernel,
2156 * using info from a dataplane context.
2158 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
2161 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2162 struct nexthop
*nexthop
;
2164 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2166 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2168 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2170 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2171 /* Single 'replace' operation */
2175 * With route replace semantics in place
2176 * for v4 routes and the new route is a system
2177 * route we do not install anything.
2178 * The problem here is that the new system
2179 * route should cause us to withdraw from
2180 * the kernel the old non-system route
2182 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)) &&
2183 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2184 (void)netlink_route_multipath(RTM_DELROUTE
,
2188 * So v6 route replace semantics are not in
2189 * the kernel at this point as I understand it.
2190 * so let's do a delete then an add.
2191 * In the future once v6 route replace semantics
2192 * are in we can figure out what to do here to
2193 * allow working with old and new kernels.
2195 * I'm also intentionally ignoring the failure case
2196 * of the route delete. If that happens yeah we're
2199 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2200 (void)netlink_route_multipath(RTM_DELROUTE
,
2206 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2209 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2210 ret
= netlink_route_multipath(cmd
, ctx
);
2213 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
2214 /* Update installed nexthops to signal which have been
2217 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2218 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2221 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2222 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2228 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2232 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2234 * @tb: Netlink RTA data
2235 * @family: Address family in the nhmsg
2236 * @ifp: Interface connected - this should be NULL, we fill it in
2237 * @ns_id: Namspace id
2239 * Return: New nexthop
2241 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2242 unsigned char family
,
2243 struct interface
**ifp
,
2246 struct nexthop nh
= {};
2248 enum nexthop_types_t type
= 0;
2251 struct interface
*ifp_lookup
;
2253 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2256 if (tb
[NHA_GATEWAY
]) {
2259 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2263 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2268 EC_ZEBRA_BAD_NHG_MESSAGE
,
2269 "Nexthop gateway with bad address family (%d) received from kernel",
2273 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2275 type
= NEXTHOP_TYPE_IFINDEX
;
2281 memcpy(&(nh
.gate
), gate
, sz
);
2284 nh
.ifindex
= if_index
;
2287 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2292 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2295 EC_ZEBRA_UNKNOWN_INTERFACE
,
2296 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2297 __PRETTY_FUNCTION__
, nh
.ifindex
);
2299 nh
.vrf_id
= VRF_DEFAULT
;
2302 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2303 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2306 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2308 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2309 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2312 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2319 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2320 struct nh_grp
*z_grp
, int z_grp_size
)
2323 /* linux/nexthop.h group struct */
2324 struct nexthop_grp
*n_grp
= NULL
;
2326 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2327 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2329 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2330 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2331 "Invalid nexthop group received from the kernel");
2336 // TODO: Need type for something?
2337 zlog_debug("Nexthop group type: %d",
2338 *((uint16_t *)RTA_DATA(tb
[NHA_GROUP_TYPE
])));
2342 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2343 z_grp
[i
].id
= n_grp
[i
].id
;
2344 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2350 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2352 * @h: Netlink message header
2353 * @ns_id: Namspace id
2354 * @startup: Are we reading under startup conditions?
2356 * Return: Result status
2358 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2361 /* nexthop group id */
2363 unsigned char family
;
2365 afi_t afi
= AFI_UNSPEC
;
2366 vrf_id_t vrf_id
= VRF_DEFAULT
;
2367 struct interface
*ifp
= NULL
;
2368 struct nhmsg
*nhm
= NULL
;
2369 struct nexthop nh
= {};
2370 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2371 /* Count of nexthops in group array */
2372 uint8_t grp_count
= 0;
2373 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2375 nhm
= NLMSG_DATA(h
);
2380 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2383 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2386 "%s: Message received from netlink is of a broken size %d %zu",
2387 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2388 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2392 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2397 EC_ZEBRA_BAD_NHG_MESSAGE
,
2398 "Nexthop group without an ID received from the kernel");
2402 /* We use the ID key'd nhg table for kernel updates */
2403 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2405 family
= nhm
->nh_family
;
2406 afi
= family2afi(family
);
2408 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2410 if (IS_ZEBRA_DEBUG_KERNEL
)
2411 zlog_debug("%s ID (%u) %s NS %u",
2412 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2413 nl_family_to_str(family
), ns_id
);
2416 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2417 if (tb
[NHA_GROUP
]) {
2419 * If this is a group message its only going to have
2420 * an array of nexthop IDs associated with it
2422 grp_count
= netlink_nexthop_process_group(
2423 tb
, grp
, array_size(grp
));
2425 if (tb
[NHA_BLACKHOLE
]) {
2427 * This nexthop is just for blackhole-ing
2428 * traffic, it should not have an OIF, GATEWAY,
2431 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2432 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2433 } else if (tb
[NHA_OIF
])
2435 * This is a true new nexthop, so we need
2436 * to parse the gateway and device info
2438 nh
= netlink_nexthop_process_nh(tb
, family
,
2443 EC_ZEBRA_BAD_NHG_MESSAGE
,
2444 "Invalid Nexthop message received from the kernel with ID (%u)",
2448 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2449 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2450 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2454 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2458 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2459 zebra_nhg_kernel_del(id
, vrf_id
);
2465 * netlink_request_nexthop() - Request nextop information from the kernel
2466 * @zns: Zebra namespace
2467 * @family: AF_* netlink family
2468 * @type: RTM_* route type
2470 * Return: Result status
2472 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2479 /* Form the request, specifying filter (rtattr) if needed. */
2480 memset(&req
, 0, sizeof(req
));
2481 req
.n
.nlmsg_type
= type
;
2482 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2483 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2484 req
.nhm
.nh_family
= family
;
2486 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2491 * netlink_nexthop_read() - Nexthop read function using netlink interface
2493 * @zns: Zebra name space
2495 * Return: Result status
2496 * Only called at bootstrap time.
2498 int netlink_nexthop_read(struct zebra_ns
*zns
)
2501 struct zebra_dplane_info dp_info
;
2503 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2505 /* Get nexthop objects */
2506 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2509 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2513 /* If we succesfully read in nexthop objects,
2514 * this kernel must support them.
2518 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2519 zlog_debug("Nexthop objects %ssupported on this kernel",
2520 supports_nh
? "" : "not ");
2526 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2527 int llalen
, ns_id_t ns_id
)
2529 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2530 addr
, lla
, llalen
, ns_id
);
2534 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2535 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2537 static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2540 uint8_t protocol
= RTPROT_ZEBRA
;
2546 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2547 const struct ipaddr
*addr
;
2549 memset(&req
, 0, sizeof(req
));
2551 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2552 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2553 if (cmd
== RTM_NEWNEIGH
)
2554 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2555 req
.n
.nlmsg_type
= cmd
;
2556 req
.ndm
.ndm_family
= PF_BRIDGE
;
2557 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2558 req
.ndm
.ndm_flags
|= NTF_SELF
; /* Handle by "self", not "master" */
2561 addattr_l(&req
.n
, sizeof(req
),
2562 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2563 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2564 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2566 addr
= dplane_ctx_neigh_get_ipaddr(ctx
);
2568 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &(addr
->ipaddr_v4
), 4);
2570 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2571 dplane_ctx_get_ns(ctx
), 0);
2575 #define NDA_RTA(r) \
2576 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2579 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2582 struct interface
*ifp
;
2583 struct zebra_if
*zif
;
2584 struct rtattr
*tb
[NDA_MAX
+ 1];
2585 struct interface
*br_if
;
2588 struct prefix vtep_ip
;
2589 int vid_present
= 0, dst_present
= 0;
2590 char buf
[ETHER_ADDR_STRLEN
];
2595 ndm
= NLMSG_DATA(h
);
2597 /* We only process macfdb notifications if EVPN is enabled */
2598 if (!is_evpn_enabled())
2601 /* The interface should exist. */
2602 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2604 if (!ifp
|| !ifp
->info
) {
2605 if (IS_ZEBRA_DEBUG_KERNEL
)
2606 zlog_debug("\t%s without associated interface: %u",
2607 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
2611 /* The interface should be something we're interested in. */
2612 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
2613 if (IS_ZEBRA_DEBUG_KERNEL
)
2614 zlog_debug("\t%s Not interested in %s, not a slave",
2615 __PRETTY_FUNCTION__
, ifp
->name
);
2619 /* Drop "permanent" entries. */
2620 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2621 if (IS_ZEBRA_DEBUG_KERNEL
)
2622 zlog_debug("\t%s Entry is PERMANENT, dropping",
2623 __PRETTY_FUNCTION__
);
2627 zif
= (struct zebra_if
*)ifp
->info
;
2628 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2629 if (IS_ZEBRA_DEBUG_KERNEL
)
2631 "%s family %s IF %s(%u) brIF %u - no bridge master",
2632 nl_msg_type_to_str(h
->nlmsg_type
),
2633 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2635 zif
->brslave_info
.bridge_ifindex
);
2639 /* Parse attributes and extract fields of interest. */
2640 memset(tb
, 0, sizeof tb
);
2641 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2643 if (!tb
[NDA_LLADDR
]) {
2644 if (IS_ZEBRA_DEBUG_KERNEL
)
2645 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2646 nl_msg_type_to_str(h
->nlmsg_type
),
2647 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2649 zif
->brslave_info
.bridge_ifindex
);
2653 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2654 if (IS_ZEBRA_DEBUG_KERNEL
)
2656 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2657 nl_msg_type_to_str(h
->nlmsg_type
),
2658 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2660 zif
->brslave_info
.bridge_ifindex
,
2661 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2665 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2667 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2669 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2670 sprintf(vid_buf
, " VLAN %u", vid
);
2674 /* TODO: Only IPv4 supported now. */
2676 vtep_ip
.family
= AF_INET
;
2677 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2678 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2680 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2683 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2685 if (IS_ZEBRA_DEBUG_KERNEL
)
2686 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2687 nl_msg_type_to_str(h
->nlmsg_type
),
2688 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2689 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2690 sticky
? "sticky " : "",
2691 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2692 dst_present
? dst_buf
: "");
2694 if (filter_vlan
&& vid
!= filter_vlan
) {
2695 if (IS_ZEBRA_DEBUG_KERNEL
)
2696 zlog_debug("\tFiltered due to filter vlan: %d",
2701 /* If add or update, do accordingly if learnt on a "local" interface; if
2702 * the notification is over VxLAN, this has to be related to
2704 * so perform an implicit delete of any local entry (if it exists).
2706 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2707 if (IS_ZEBRA_IF_VXLAN(ifp
))
2708 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2711 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2715 /* This is a delete notification.
2716 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2717 * 2. For a MAC over "local" interface, delete the mac
2718 * Note: We will get notifications from both bridge driver and VxLAN
2720 * Ignore the notification from VxLan driver as it is also generated
2721 * when mac moves from remote to local.
2724 if (IS_ZEBRA_DEBUG_KERNEL
)
2725 zlog_debug("\tNo Destination Present");
2729 if (IS_ZEBRA_IF_VXLAN(ifp
))
2730 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2733 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2736 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2741 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2744 /* Length validity. */
2745 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2749 /* We are interested only in AF_BRIDGE notifications. */
2750 ndm
= NLMSG_DATA(h
);
2751 if (ndm
->ndm_family
!= AF_BRIDGE
)
2754 return netlink_macfdb_change(h
, len
, ns_id
);
2757 /* Request for MAC FDB information from the kernel */
2758 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2759 int type
, ifindex_t master_ifindex
)
2763 struct ifinfomsg ifm
;
2767 /* Form the request, specifying filter (rtattr) if needed. */
2768 memset(&req
, 0, sizeof(req
));
2769 req
.n
.nlmsg_type
= type
;
2770 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2771 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2772 req
.ifm
.ifi_family
= family
;
2774 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2776 return netlink_request(netlink_cmd
, &req
.n
);
2780 * MAC forwarding database read using netlink interface. This is invoked
2783 int netlink_macfdb_read(struct zebra_ns
*zns
)
2786 struct zebra_dplane_info dp_info
;
2788 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2790 /* Get bridge FDB table. */
2791 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2795 /* We are reading entire table. */
2797 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2804 * MAC forwarding database read using netlink interface. This is for a
2805 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2807 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2808 struct interface
*br_if
)
2810 struct zebra_if
*br_zif
;
2811 struct zebra_if
*zif
;
2812 struct zebra_l2info_vxlan
*vxl
;
2813 struct zebra_dplane_info dp_info
;
2816 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2818 /* Save VLAN we're filtering on, if needed. */
2819 br_zif
= (struct zebra_if
*)br_if
->info
;
2820 zif
= (struct zebra_if
*)ifp
->info
;
2821 vxl
= &zif
->l2info
.vxl
;
2822 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2823 filter_vlan
= vxl
->access_vlan
;
2825 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2827 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2831 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2834 /* Reset VLAN filter. */
2840 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2841 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2844 struct interface
*br_if
,
2845 struct ethaddr
*mac
,
2853 struct zebra_if
*br_zif
;
2854 char buf
[ETHER_ADDR_STRLEN
];
2856 memset(&req
, 0, sizeof(req
));
2857 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2858 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2859 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2860 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2861 /* req.ndm.ndm_state = NUD_REACHABLE; */
2863 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2865 br_zif
= (struct zebra_if
*)br_if
->info
;
2866 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2867 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2869 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2871 if (IS_ZEBRA_DEBUG_KERNEL
)
2872 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2873 __PRETTY_FUNCTION__
,
2874 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2876 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2878 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2881 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2882 struct interface
*br_if
,
2883 struct ethaddr
*mac
, vlanid_t vid
)
2886 struct zebra_dplane_info dp_info
;
2888 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2890 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2892 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2898 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2905 * Netlink-specific handler for MAC updates using dataplane context object.
2907 static enum zebra_dplane_result
2908 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2910 uint8_t protocol
= RTPROT_ZEBRA
;
2918 int vid_present
= 0;
2920 struct in_addr vtep_ip
;
2923 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2928 memset(&req
, 0, sizeof(req
));
2930 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2931 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2932 if (cmd
== RTM_NEWNEIGH
)
2933 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2934 req
.n
.nlmsg_type
= cmd
;
2935 req
.ndm
.ndm_family
= AF_BRIDGE
;
2936 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2937 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2939 if (dplane_ctx_mac_is_sticky(ctx
))
2940 req
.ndm
.ndm_state
|= NUD_NOARP
;
2942 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2944 addattr_l(&req
.n
, sizeof(req
),
2945 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2946 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2947 dplane_ctx_mac_get_addr(ctx
), 6);
2948 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2950 dst_alen
= 4; // TODO: hardcoded
2951 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2952 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2954 vid
= dplane_ctx_mac_get_vlan(ctx
);
2957 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2960 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
,
2961 dplane_ctx_mac_get_br_ifindex(ctx
));
2963 if (IS_ZEBRA_DEBUG_KERNEL
) {
2964 char ipbuf
[PREFIX_STRLEN
];
2965 char buf
[ETHER_ADDR_STRLEN
];
2966 char dst_buf
[PREFIX_STRLEN
+ 10];
2970 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2974 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2975 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2976 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2978 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2979 nl_msg_type_to_str(cmd
),
2980 nl_family_to_str(req
.ndm
.ndm_family
),
2981 dplane_ctx_get_ifname(ctx
),
2982 dplane_ctx_get_ifindex(ctx
), vid_buf
,
2983 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2987 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
2988 dplane_ctx_get_ns(ctx
), 0);
2990 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2992 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2996 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2997 * 5549 support, re-install them.
2999 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3000 struct interface
*ifp
, struct ipaddr
*ip
,
3003 if (ndm
->ndm_family
!= AF_INET
)
3006 if (!zif
->v6_2_v4_ll_neigh_entry
)
3009 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3012 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3013 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3018 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3022 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3025 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3028 struct interface
*ifp
;
3029 struct zebra_if
*zif
;
3030 struct rtattr
*tb
[NDA_MAX
+ 1];
3031 struct interface
*link_if
;
3034 char buf
[ETHER_ADDR_STRLEN
];
3035 char buf2
[INET6_ADDRSTRLEN
];
3036 int mac_present
= 0;
3040 ndm
= NLMSG_DATA(h
);
3042 /* The interface should exist. */
3043 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3045 if (!ifp
|| !ifp
->info
)
3048 zif
= (struct zebra_if
*)ifp
->info
;
3050 /* Parse attributes and extract fields of interest. */
3051 memset(tb
, 0, sizeof tb
);
3052 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3055 zlog_debug("%s family %s IF %s(%u) - no DST",
3056 nl_msg_type_to_str(h
->nlmsg_type
),
3057 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3062 memset(&ip
, 0, sizeof(struct ipaddr
));
3063 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3064 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3066 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3067 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3068 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3069 if (IS_ZEBRA_DEBUG_KERNEL
)
3071 "\tNeighbor Entry Received is a 5549 entry, finished");
3075 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3076 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3077 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3079 /* The neighbor is present on an SVI. From this, we locate the
3081 * bridge because we're only interested in neighbors on a VxLAN bridge.
3082 * The bridge is located based on the nature of the SVI:
3083 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3085 * and is linked to the bridge
3086 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3090 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3091 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3095 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3098 if (IS_ZEBRA_DEBUG_KERNEL
)
3100 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3104 memset(&mac
, 0, sizeof(struct ethaddr
));
3105 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3106 if (tb
[NDA_LLADDR
]) {
3107 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3108 if (IS_ZEBRA_DEBUG_KERNEL
)
3110 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3115 ifp
->name
, ndm
->ndm_ifindex
,
3116 (unsigned long)RTA_PAYLOAD(
3122 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3125 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3126 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3128 if (IS_ZEBRA_DEBUG_KERNEL
)
3130 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3131 nl_msg_type_to_str(h
->nlmsg_type
),
3132 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3134 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3136 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3138 ndm
->ndm_state
, ndm
->ndm_flags
);
3140 /* If the neighbor state is valid for use, process as an add or
3142 * else process as a delete. Note that the delete handling may
3144 * in re-adding the neighbor if it is a valid "remote" neighbor.
3146 if (ndm
->ndm_state
& NUD_VALID
)
3147 return zebra_vxlan_handle_kernel_neigh_update(
3148 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3151 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3154 if (IS_ZEBRA_DEBUG_KERNEL
)
3155 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3156 nl_msg_type_to_str(h
->nlmsg_type
),
3157 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3159 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3161 /* Process the delete - it may result in re-adding the neighbor if it is
3162 * a valid "remote" neighbor.
3164 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3167 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3172 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3175 /* Length validity. */
3176 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3180 /* We are interested only in AF_INET or AF_INET6 notifications. */
3181 ndm
= NLMSG_DATA(h
);
3182 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3185 return netlink_neigh_change(h
, len
);
3188 /* Request for IP neighbor information from the kernel */
3189 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3190 int type
, ifindex_t ifindex
)
3198 /* Form the request, specifying filter (rtattr) if needed. */
3199 memset(&req
, 0, sizeof(req
));
3200 req
.n
.nlmsg_type
= type
;
3201 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3202 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3203 req
.ndm
.ndm_family
= family
;
3205 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3207 return netlink_request(netlink_cmd
, &req
.n
);
3211 * IP Neighbor table read using netlink interface. This is invoked
3214 int netlink_neigh_read(struct zebra_ns
*zns
)
3217 struct zebra_dplane_info dp_info
;
3219 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3221 /* Get IP neighbor table. */
3222 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3226 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3233 * IP Neighbor table read using netlink interface. This is for a specific
3236 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3239 struct zebra_dplane_info dp_info
;
3241 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3243 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3247 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3254 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3255 * read using netlink interface.
3257 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3258 int type
, struct ipaddr
*ip
,
3268 /* Form the request, specifying filter (rtattr) if needed. */
3269 memset(&req
, 0, sizeof(req
));
3270 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3271 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3272 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3273 req
.ndm
.ndm_ifindex
= ifindex
;
3275 if (IS_IPADDR_V4(ip
)) {
3276 ipa_len
= IPV4_MAX_BYTELEN
;
3277 req
.ndm
.ndm_family
= AF_INET
;
3280 ipa_len
= IPV6_MAX_BYTELEN
;
3281 req
.ndm
.ndm_family
= AF_INET6
;
3284 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3286 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
3289 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3290 struct interface
*vlan_if
)
3293 struct zebra_ns
*zns
;
3294 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3295 char buf
[INET6_ADDRSTRLEN
];
3296 struct zebra_dplane_info dp_info
;
3300 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3302 if (IS_ZEBRA_DEBUG_KERNEL
)
3303 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3304 __PRETTY_FUNCTION__
, vlan_if
->name
,
3306 ipaddr2str(ip
, buf
, sizeof(buf
)),
3309 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3314 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3320 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3325 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3328 /* Length validity. */
3329 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3331 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3332 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
3333 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3337 /* Is this a notification for the MAC FDB or IP neighbor table? */
3338 ndm
= NLMSG_DATA(h
);
3339 if (ndm
->ndm_family
== AF_BRIDGE
)
3340 return netlink_macfdb_change(h
, len
, ns_id
);
3342 if (ndm
->ndm_type
!= RTN_UNICAST
)
3345 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3346 return netlink_ipneigh_change(h
, len
, ns_id
);
3349 EC_ZEBRA_UNKNOWN_FAMILY
,
3350 "Invalid address family: %u received from kernel neighbor change: %s",
3351 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3359 * Utility neighbor-update function, using info from dplane context.
3361 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3364 uint8_t protocol
= RTPROT_ZEBRA
;
3371 char buf
[INET6_ADDRSTRLEN
];
3372 char buf2
[ETHER_ADDR_STRLEN
];
3373 const struct ipaddr
*ip
;
3374 const struct ethaddr
*mac
;
3378 memset(&req
, 0, sizeof(req
));
3380 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3381 mac
= dplane_ctx_neigh_get_mac(ctx
);
3382 if (is_zero_mac(mac
))
3385 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3386 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3388 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3389 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3390 if (cmd
== RTM_NEWNEIGH
)
3391 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3392 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
3393 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3394 req
.ndm
.ndm_state
= state
;
3395 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3396 req
.ndm
.ndm_type
= RTN_UNICAST
;
3397 req
.ndm
.ndm_flags
= flags
;
3399 addattr_l(&req
.n
, sizeof(req
),
3400 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
3401 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3402 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3404 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3406 if (IS_ZEBRA_DEBUG_KERNEL
)
3407 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3408 nl_msg_type_to_str(cmd
),
3409 nl_family_to_str(req
.ndm
.ndm_family
),
3410 dplane_ctx_get_ifname(ctx
),
3411 dplane_ctx_get_ifindex(ctx
),
3412 ipaddr2str(ip
, buf
, sizeof(buf
)),
3413 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
3417 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3418 dplane_ctx_get_ns(ctx
), 0);
3422 * Update MAC, using dataplane context object.
3424 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
3426 return netlink_macfdb_update_ctx(ctx
);
3429 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
3433 switch (dplane_ctx_get_op(ctx
)) {
3434 case DPLANE_OP_NEIGH_INSTALL
:
3435 case DPLANE_OP_NEIGH_UPDATE
:
3436 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
3438 case DPLANE_OP_NEIGH_DELETE
:
3439 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
3441 case DPLANE_OP_VTEP_ADD
:
3442 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
);
3444 case DPLANE_OP_VTEP_DELETE
:
3445 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
);
3452 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
3456 * MPLS label forwarding table change via netlink interface, using dataplane
3457 * context information.
3459 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
3462 const zebra_nhlfe_t
*nhlfe
;
3463 struct nexthop
*nexthop
= NULL
;
3464 unsigned int nexthop_num
;
3465 const char *routedesc
;
3471 char buf
[NL_PKT_BUF_SIZE
];
3474 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
3477 * Count # nexthops so we can decide whether to use singlepath
3478 * or multipath case.
3481 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
3482 nexthop
= nhlfe
->nexthop
;
3485 if (cmd
== RTM_NEWROUTE
) {
3486 /* Count all selected NHLFEs */
3487 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3488 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3491 /* Count all installed NHLFEs */
3492 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3493 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3498 if ((nexthop_num
== 0) ||
3499 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3502 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3503 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3504 req
.n
.nlmsg_type
= cmd
;
3505 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3507 req
.r
.rtm_family
= AF_MPLS
;
3508 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3509 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3510 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3511 req
.r
.rtm_type
= RTN_UNICAST
;
3513 if (cmd
== RTM_NEWROUTE
) {
3514 /* We do a replace to handle update. */
3515 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3517 /* set the protocol value if installing */
3518 route_type
= re_type_from_lsp_type(
3519 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3520 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3523 /* Fill destination */
3524 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3525 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3527 /* Fill nexthops (paths) based on single-path or multipath. The paths
3528 * chosen depend on the operation.
3530 if (nexthop_num
== 1) {
3531 routedesc
= "single-path";
3532 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3536 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3537 nhlfe
; nhlfe
= nhlfe
->next
) {
3538 nexthop
= nhlfe
->nexthop
;
3542 if ((cmd
== RTM_NEWROUTE
3543 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3544 && CHECK_FLAG(nexthop
->flags
,
3545 NEXTHOP_FLAG_ACTIVE
)))
3546 || (cmd
== RTM_DELROUTE
3547 && (CHECK_FLAG(nhlfe
->flags
,
3548 NHLFE_FLAG_INSTALLED
)
3549 && CHECK_FLAG(nexthop
->flags
,
3550 NEXTHOP_FLAG_FIB
)))) {
3551 /* Add the gateway */
3552 _netlink_mpls_build_singlepath(
3561 } else { /* Multipath case */
3562 char buf
[NL_PKT_BUF_SIZE
];
3563 struct rtattr
*rta
= (void *)buf
;
3564 struct rtnexthop
*rtnh
;
3565 const union g_addr
*src1
= NULL
;
3567 rta
->rta_type
= RTA_MULTIPATH
;
3568 rta
->rta_len
= RTA_LENGTH(0);
3569 rtnh
= RTA_DATA(rta
);
3571 routedesc
= "multipath";
3572 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3576 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3577 nhlfe
; nhlfe
= nhlfe
->next
) {
3578 nexthop
= nhlfe
->nexthop
;
3582 if ((cmd
== RTM_NEWROUTE
3583 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3584 && CHECK_FLAG(nexthop
->flags
,
3585 NEXTHOP_FLAG_ACTIVE
)))
3586 || (cmd
== RTM_DELROUTE
3587 && (CHECK_FLAG(nhlfe
->flags
,
3588 NHLFE_FLAG_INSTALLED
)
3589 && CHECK_FLAG(nexthop
->flags
,
3590 NEXTHOP_FLAG_FIB
)))) {
3593 /* Build the multipath */
3594 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3597 rtnh
= RTNH_NEXT(rtnh
);
3601 /* Add the multipath */
3602 if (rta
->rta_len
> RTA_LENGTH(0))
3603 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3604 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3607 /* Talk to netlink socket. */
3608 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3609 dplane_ctx_get_ns(ctx
), 0);
3611 #endif /* HAVE_NETLINK */