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 bool 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",
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",
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",
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
= {};
518 char anyaddr
[16] = {0};
520 int proto
= ZEBRA_ROUTE_KERNEL
;
525 uint8_t distance
= 0;
531 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
532 void *src
= NULL
; /* IPv6 srcdest source prefix */
533 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
537 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
539 switch (rtm
->rtm_type
) {
543 bh_type
= BLACKHOLE_NULL
;
545 case RTN_UNREACHABLE
:
546 bh_type
= BLACKHOLE_REJECT
;
549 bh_type
= BLACKHOLE_ADMINPROHIB
;
552 if (IS_ZEBRA_DEBUG_KERNEL
)
553 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
554 nl_rttype_to_str(rtm
->rtm_type
),
559 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
562 "%s: Message received from netlink is of a broken size %d %zu",
563 __func__
, h
->nlmsg_len
,
564 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
568 memset(tb
, 0, sizeof(tb
));
569 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
571 if (rtm
->rtm_flags
& RTM_F_CLONED
)
573 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
575 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
578 selfroute
= is_selfroute(rtm
->rtm_protocol
);
580 if (!startup
&& selfroute
&& h
->nlmsg_type
== RTM_NEWROUTE
) {
581 if (IS_ZEBRA_DEBUG_KERNEL
)
582 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
587 /* We don't care about change notifications for the MPLS table. */
588 /* TODO: Revisit this. */
589 if (rtm
->rtm_family
== AF_MPLS
)
592 /* Table corresponding to route. */
594 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
596 table
= rtm
->rtm_table
;
599 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
600 if (vrf_id
== VRF_DEFAULT
) {
601 if (!is_zebra_valid_kernel_table(table
)
602 && !is_zebra_main_routing_table(table
))
606 /* Route which inserted by Zebra. */
608 flags
|= ZEBRA_FLAG_SELFROUTE
;
609 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
612 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
615 dest
= RTA_DATA(tb
[RTA_DST
]);
620 src
= RTA_DATA(tb
[RTA_SRC
]);
625 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
628 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
631 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
633 if (tb
[RTA_PRIORITY
])
634 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
636 #if defined(SUPPORT_REALMS)
638 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
641 if (tb
[RTA_METRICS
]) {
642 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
644 memset(mxrta
, 0, sizeof(mxrta
));
645 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
646 RTA_PAYLOAD(tb
[RTA_METRICS
]));
649 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
652 if (rtm
->rtm_family
== AF_INET
) {
654 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
656 "Invalid destination prefix length: %u received from kernel route change",
660 memcpy(&p
.u
.prefix4
, dest
, 4);
661 p
.prefixlen
= rtm
->rtm_dst_len
;
663 if (rtm
->rtm_src_len
!= 0) {
664 char buf
[PREFIX_STRLEN
];
666 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
667 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
668 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
672 /* Force debug below to not display anything for source */
674 } else if (rtm
->rtm_family
== AF_INET6
) {
676 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
678 "Invalid destination prefix length: %u received from kernel route change",
682 memcpy(&p
.u
.prefix6
, dest
, 16);
683 p
.prefixlen
= rtm
->rtm_dst_len
;
685 src_p
.family
= AF_INET6
;
686 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
688 "Invalid source prefix length: %u received from kernel route change",
692 memcpy(&src_p
.prefix
, src
, 16);
693 src_p
.prefixlen
= rtm
->rtm_src_len
;
697 * For ZEBRA_ROUTE_KERNEL types:
699 * The metric/priority of the route received from the kernel
700 * is a 32 bit number. We are going to interpret the high
701 * order byte as the Admin Distance and the low order 3 bytes
704 * This will allow us to do two things:
705 * 1) Allow the creation of kernel routes that can be
706 * overridden by zebra.
707 * 2) Allow the old behavior for 'most' kernel route types
708 * if a user enters 'ip route ...' v4 routes get a metric
709 * of 0 and v6 routes get a metric of 1024. Both of these
710 * values will end up with a admin distance of 0, which
711 * will cause them to win for the purposes of zebra.
713 if (proto
== ZEBRA_ROUTE_KERNEL
) {
714 distance
= (metric
>> 24) & 0xFF;
715 metric
= (metric
& 0x00FFFFFF);
718 if (IS_ZEBRA_DEBUG_KERNEL
) {
719 char buf
[PREFIX_STRLEN
];
720 char buf2
[PREFIX_STRLEN
];
721 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
722 nl_msg_type_to_str(h
->nlmsg_type
),
723 prefix2str(&p
, buf
, sizeof(buf
)),
724 src_p
.prefixlen
? " from " : "",
726 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
728 vrf_id
, table
, metric
, distance
);
732 if (rtm
->rtm_family
== AF_INET6
)
735 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
737 if (!tb
[RTA_MULTIPATH
]) {
738 struct nexthop nh
= {0};
741 nh
= parse_nexthop_unicast(
742 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
745 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
746 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
749 /* This is a multipath route */
750 struct route_entry
*re
;
751 struct nexthop_group
*ng
= NULL
;
752 struct rtnexthop
*rtnh
=
753 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
755 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
757 re
->distance
= distance
;
763 re
->uptime
= monotime(NULL
);
770 /* Use temporary list of nexthops; parse
771 * message payload's nexthops.
773 ng
= nexthop_group_new();
775 parse_multipath_nexthops_unicast(
776 ns_id
, ng
, rtm
, rtnh
, tb
,
779 zserv_nexthop_num_warn(
780 __func__
, (const struct prefix
*)&p
,
784 nexthop_group_delete(&ng
);
790 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
797 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
798 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
801 if (!tb
[RTA_MULTIPATH
]) {
804 nh
= parse_nexthop_unicast(
805 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
807 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
808 flags
, &p
, &src_p
, &nh
, 0, table
,
809 metric
, distance
, true);
811 /* XXX: need to compare the entire list of
812 * nexthops here for NLM_F_APPEND stupidity */
813 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
814 flags
, &p
, &src_p
, NULL
, 0, table
,
815 metric
, distance
, true);
823 static struct mcast_route_data
*mroute
= NULL
;
825 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
826 ns_id_t ns_id
, int startup
)
830 struct rtattr
*tb
[RTA_MAX
+ 1];
831 struct mcast_route_data
*m
;
832 struct mcast_route_data mr
;
839 char oif_list
[256] = "\0";
846 memset(&mr
, 0, sizeof(mr
));
852 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
854 memset(tb
, 0, sizeof(tb
));
855 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
858 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
860 table
= rtm
->rtm_table
;
862 vrf
= vrf_lookup_by_table(table
, ns_id
);
865 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
868 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
871 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
874 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
876 if (tb
[RTA_MULTIPATH
]) {
877 struct rtnexthop
*rtnh
=
878 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
880 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
882 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
885 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
888 if (rtnh
->rtnh_len
== 0)
891 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
892 rtnh
= RTNH_NEXT(rtnh
);
896 if (IS_ZEBRA_DEBUG_KERNEL
) {
897 struct interface
*ifp
= NULL
;
898 struct zebra_vrf
*zvrf
= NULL
;
900 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
901 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
902 for (count
= 0; count
< oif_count
; count
++) {
903 ifp
= if_lookup_by_index(oif
[count
], vrf
);
906 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
908 strlcat(oif_list
, temp
, sizeof(oif_list
));
910 zvrf
= zebra_vrf_lookup_by_id(vrf
);
911 ifp
= if_lookup_by_index(iif
, vrf
);
913 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
914 (zvrf
? zvrf
->vrf
->name
: "Unknown"), vrf
,
915 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
916 ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
922 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
929 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
930 /* If this is not route add/delete message print warning. */
931 zlog_debug("Kernel message: %s NS %u",
932 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
936 if (!(rtm
->rtm_family
== AF_INET
||
937 rtm
->rtm_family
== AF_INET6
||
938 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
940 EC_ZEBRA_UNKNOWN_FAMILY
,
941 "Invalid address family: %u received from kernel route change: %s",
942 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
946 /* Connected route. */
947 if (IS_ZEBRA_DEBUG_KERNEL
)
948 zlog_debug("%s %s %s proto %s NS %u",
949 nl_msg_type_to_str(h
->nlmsg_type
),
950 nl_family_to_str(rtm
->rtm_family
),
951 nl_rttype_to_str(rtm
->rtm_type
),
952 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
955 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
958 "%s: Message received from netlink is of a broken size: %d %zu",
959 __func__
, h
->nlmsg_len
,
960 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
964 if (rtm
->rtm_type
== RTN_MULTICAST
)
965 netlink_route_change_read_multicast(h
, ns_id
, startup
);
967 netlink_route_change_read_unicast(h
, ns_id
, startup
);
971 /* Request for specific route information from the kernel */
972 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
979 /* Form the request, specifying filter (rtattr) if needed. */
980 memset(&req
, 0, sizeof(req
));
981 req
.n
.nlmsg_type
= type
;
982 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
983 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
984 req
.rtm
.rtm_family
= family
;
986 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
989 /* Routing table read function using netlink interface. Only called
991 int netlink_route_read(struct zebra_ns
*zns
)
994 struct zebra_dplane_info dp_info
;
996 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
998 /* Get IPv4 routing table. */
999 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1002 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1003 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1007 /* Get IPv6 routing table. */
1008 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1011 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1012 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1019 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
1021 struct nlmsghdr
*nlmsg
,
1022 size_t req_size
, int bytelen
,
1023 const struct nexthop
*nexthop
)
1025 if (route_family
== AF_MPLS
) {
1026 struct gw_family_t gw_fam
;
1028 gw_fam
.family
= gw_family
;
1029 if (gw_family
== AF_INET
)
1030 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1032 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1033 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1036 if (gw_family
== AF_INET
)
1037 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1038 &nexthop
->gate
.ipv4
, bytelen
);
1040 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1041 &nexthop
->gate
.ipv6
, bytelen
);
1045 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
1048 struct rtnexthop
*rtnh
,
1049 size_t req_size
, int bytelen
,
1050 const struct nexthop
*nexthop
)
1052 if (route_family
== AF_MPLS
) {
1053 struct gw_family_t gw_fam
;
1055 gw_fam
.family
= gw_family
;
1056 if (gw_family
== AF_INET
)
1057 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1059 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1060 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
1062 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
1064 if (gw_family
== AF_INET
)
1065 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1066 &nexthop
->gate
.ipv4
, bytelen
);
1068 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1069 &nexthop
->gate
.ipv6
, bytelen
);
1070 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1074 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1075 mpls_lse_t
*out_lse
, char *label_buf
,
1076 size_t label_buf_size
)
1078 char label_buf1
[20];
1081 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1082 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1085 if (IS_ZEBRA_DEBUG_KERNEL
) {
1087 sprintf(label_buf
, "label %u",
1088 nh_label
->label
[i
]);
1090 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1091 strlcat(label_buf
, label_buf1
, label_buf_size
);
1095 out_lse
[num_labels
] =
1096 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1103 /* This function takes a nexthop as argument and adds
1104 * the appropriate netlink attributes to an existing
1107 * @param routedesc: Human readable description of route type
1108 * (direct/recursive, single-/multipath)
1109 * @param bytelen: Length of addresses in bytes.
1110 * @param nexthop: Nexthop information
1111 * @param nlmsg: nlmsghdr structure to fill in.
1112 * @param req_size: The size allocated for the message.
1114 static void _netlink_route_build_singlepath(const struct prefix
*p
,
1115 const char *routedesc
, int bytelen
,
1116 const struct nexthop
*nexthop
,
1117 struct nlmsghdr
*nlmsg
,
1118 struct rtmsg
*rtmsg
,
1119 size_t req_size
, int cmd
)
1122 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1123 char label_buf
[256];
1129 * label_buf is *only* currently used within debugging.
1130 * As such when we assign it we are guarding it inside
1131 * a debug test. If you want to change this make sure
1132 * you fix this assumption
1134 label_buf
[0] = '\0';
1136 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1140 /* Set the BoS bit */
1141 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1143 if (rtmsg
->rtm_family
== AF_MPLS
)
1144 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1145 num_labels
* sizeof(mpls_lse_t
));
1147 struct rtattr
*nest
;
1148 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1150 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1152 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1153 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1154 num_labels
* sizeof(mpls_lse_t
));
1155 addattr_nest_end(nlmsg
, nest
);
1159 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1160 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1162 if (rtmsg
->rtm_family
== AF_INET
1163 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1164 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1165 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1166 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1167 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1169 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
1170 && (cmd
== RTM_NEWROUTE
))
1171 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1172 &nexthop
->rmap_src
.ipv4
, bytelen
);
1173 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
1174 && (cmd
== RTM_NEWROUTE
))
1175 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1176 &nexthop
->src
.ipv4
, bytelen
);
1178 if (IS_ZEBRA_DEBUG_KERNEL
)
1180 " 5549: _netlink_route_build_singlepath() (%s): %pFX nexthop via %s %s if %u(%u)",
1181 routedesc
, p
, ipv4_ll_buf
, label_buf
,
1182 nexthop
->ifindex
, nexthop
->vrf_id
);
1186 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1187 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1188 /* Send deletes to the kernel without specifying the next-hop */
1189 if (cmd
!= RTM_DELROUTE
)
1190 _netlink_route_nl_add_gateway_info(
1191 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1194 if (cmd
== RTM_NEWROUTE
) {
1195 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1196 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1197 &nexthop
->rmap_src
.ipv4
, bytelen
);
1198 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1199 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1200 &nexthop
->src
.ipv4
, bytelen
);
1203 if (IS_ZEBRA_DEBUG_KERNEL
)
1205 "netlink_route_multipath() (%s): %pFX nexthop via %s %s if %u(%u)",
1206 routedesc
, p
, inet_ntoa(nexthop
->gate
.ipv4
),
1207 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1210 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1211 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1212 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1213 nlmsg
, req_size
, bytelen
,
1216 if (cmd
== RTM_NEWROUTE
) {
1217 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1218 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1219 &nexthop
->rmap_src
.ipv6
, bytelen
);
1220 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1221 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1222 &nexthop
->src
.ipv6
, bytelen
);
1225 if (IS_ZEBRA_DEBUG_KERNEL
)
1227 "netlink_route_multipath() (%s): %pFX nexthop via %s %s if %u(%u)",
1228 routedesc
, p
, inet6_ntoa(nexthop
->gate
.ipv6
),
1229 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1233 * We have the ifindex so we should always send it
1234 * This is especially useful if we are doing route
1237 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1238 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1240 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1241 if (cmd
== RTM_NEWROUTE
) {
1242 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1243 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1244 &nexthop
->rmap_src
.ipv4
, bytelen
);
1245 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1246 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1247 &nexthop
->src
.ipv4
, bytelen
);
1250 if (IS_ZEBRA_DEBUG_KERNEL
)
1252 "netlink_route_multipath() (%s): %pFX nexthop via if %u(%u)",
1253 routedesc
, p
, nexthop
->ifindex
,
1258 /* This function takes a nexthop as argument and
1259 * appends to the given rtattr/rtnexthop pair the
1260 * representation of the nexthop. If the nexthop
1261 * defines a preferred source, the src parameter
1262 * will be modified to point to that src, otherwise
1263 * it will be kept unmodified.
1265 * @param routedesc: Human readable description of route type
1266 * (direct/recursive, single-/multipath)
1267 * @param bytelen: Length of addresses in bytes.
1268 * @param nexthop: Nexthop information
1269 * @param rta: rtnetlink attribute structure
1270 * @param rtnh: pointer to an rtnetlink nexthop structure
1271 * @param src: pointer pointing to a location where
1272 * the prefsrc should be stored.
1275 _netlink_route_build_multipath(const struct prefix
*p
, const char *routedesc
,
1276 int bytelen
, const struct nexthop
*nexthop
,
1277 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1278 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1280 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1281 char label_buf
[256];
1284 rtnh
->rtnh_len
= sizeof(*rtnh
);
1285 rtnh
->rtnh_flags
= 0;
1286 rtnh
->rtnh_hops
= 0;
1287 rta
->rta_len
+= rtnh
->rtnh_len
;
1292 * label_buf is *only* currently used within debugging.
1293 * As such when we assign it we are guarding it inside
1294 * a debug test. If you want to change this make sure
1295 * you fix this assumption
1297 label_buf
[0] = '\0';
1299 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1303 /* Set the BoS bit */
1304 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1306 if (rtmsg
->rtm_family
== AF_MPLS
) {
1307 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1309 num_labels
* sizeof(mpls_lse_t
));
1311 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1313 struct rtattr
*nest
;
1314 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1315 int len
= rta
->rta_len
;
1317 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1318 &encap
, sizeof(uint16_t));
1319 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1320 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1322 num_labels
* sizeof(mpls_lse_t
));
1323 rta_nest_end(rta
, nest
);
1324 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1328 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1329 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1331 if (rtmsg
->rtm_family
== AF_INET
1332 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1333 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1335 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1336 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1338 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1339 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1340 if (nexthop
->weight
)
1341 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1343 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1344 *src
= &nexthop
->rmap_src
;
1345 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1346 *src
= &nexthop
->src
;
1348 if (IS_ZEBRA_DEBUG_KERNEL
)
1350 " 5549: netlink_route_build_multipath() (%s): %pFX nexthop via %s %s if %u",
1351 routedesc
, p
, ipv4_ll_buf
, label_buf
,
1356 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1357 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1358 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1359 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1361 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1362 *src
= &nexthop
->rmap_src
;
1363 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1364 *src
= &nexthop
->src
;
1366 if (IS_ZEBRA_DEBUG_KERNEL
)
1368 "netlink_route_multipath() (%s): %pFX nexthop via %s %s if %u",
1369 routedesc
, p
, inet_ntoa(nexthop
->gate
.ipv4
),
1370 label_buf
, nexthop
->ifindex
);
1372 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1373 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1374 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1375 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1378 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1379 *src
= &nexthop
->rmap_src
;
1380 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1381 *src
= &nexthop
->src
;
1383 if (IS_ZEBRA_DEBUG_KERNEL
)
1385 "netlink_route_multipath() (%s): %pFX nexthop via %s %s if %u",
1386 routedesc
, p
, inet6_ntoa(nexthop
->gate
.ipv6
),
1387 label_buf
, nexthop
->ifindex
);
1391 * We have figured out the ifindex so we should always send it
1392 * This is especially useful if we are doing route
1395 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1396 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1399 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1400 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1401 *src
= &nexthop
->rmap_src
;
1402 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1403 *src
= &nexthop
->src
;
1405 if (IS_ZEBRA_DEBUG_KERNEL
)
1407 "netlink_route_multipath() (%s): %pFX nexthop via if %u",
1408 routedesc
, p
, nexthop
->ifindex
);
1411 if (nexthop
->weight
)
1412 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1415 static inline void _netlink_mpls_build_singlepath(const struct prefix
*p
,
1416 const char *routedesc
,
1417 const zebra_nhlfe_t
*nhlfe
,
1418 struct nlmsghdr
*nlmsg
,
1419 struct rtmsg
*rtmsg
,
1420 size_t req_size
, int cmd
)
1425 family
= NHLFE_FAMILY(nhlfe
);
1426 bytelen
= (family
== AF_INET
? 4 : 16);
1427 _netlink_route_build_singlepath(p
, routedesc
, bytelen
, nhlfe
->nexthop
,
1428 nlmsg
, rtmsg
, req_size
, cmd
);
1433 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1434 const zebra_nhlfe_t
*nhlfe
, struct rtattr
*rta
,
1435 struct rtnexthop
*rtnh
, struct rtmsg
*rtmsg
,
1436 const union g_addr
**src
)
1441 family
= NHLFE_FAMILY(nhlfe
);
1442 bytelen
= (family
== AF_INET
? 4 : 16);
1443 _netlink_route_build_multipath(p
, routedesc
, bytelen
, nhlfe
->nexthop
,
1444 rta
, rtnh
, rtmsg
, src
);
1448 /* Log debug information for netlink_route_multipath
1449 * if debug logging is enabled.
1451 * @param cmd: Netlink command which is to be processed
1452 * @param p: Prefix for which the change is due
1453 * @param family: Address family which the change concerns
1454 * @param zvrf: The vrf we are in
1455 * @param tableid: The table we are working on
1457 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1458 int family
, vrf_id_t vrfid
,
1461 if (IS_ZEBRA_DEBUG_KERNEL
) {
1462 char buf
[PREFIX_STRLEN
];
1464 "netlink_route_multipath(): %s %s vrf %u(%u)",
1465 nl_msg_type_to_str(cmd
),
1466 prefix2str(p
, buf
, sizeof(buf
)),
1471 static void _netlink_nexthop_debug(int cmd
, uint32_t id
)
1473 if (IS_ZEBRA_DEBUG_KERNEL
)
1474 zlog_debug("netlink_nexthop(): %s, id=%u",
1475 nl_msg_type_to_str(cmd
), id
);
1478 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1480 if (IS_ZEBRA_DEBUG_KERNEL
)
1481 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1482 nl_msg_type_to_str(cmd
), label
);
1485 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1486 int llalen
, ns_id_t ns_id
)
1488 uint8_t protocol
= RTPROT_ZEBRA
;
1495 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1497 memset(&req
, 0, sizeof(req
));
1499 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1500 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1501 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1502 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1504 req
.ndm
.ndm_family
= AF_INET
;
1505 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1506 req
.ndm
.ndm_ifindex
= ifindex
;
1507 req
.ndm
.ndm_type
= RTN_UNICAST
;
1509 addattr_l(&req
.n
, sizeof(req
),
1510 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
1511 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1512 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1514 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1519 * Routing table change via netlink interface, using a dataplane context object
1521 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1524 struct nexthop
*nexthop
= NULL
;
1525 unsigned int nexthop_num
;
1527 const char *routedesc
;
1530 const struct prefix
*p
, *src_p
;
1536 char buf
[NL_PKT_BUF_SIZE
];
1539 p
= dplane_ctx_get_dest(ctx
);
1540 src_p
= dplane_ctx_get_src(ctx
);
1542 family
= PREFIX_FAMILY(p
);
1544 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1546 bytelen
= (family
== AF_INET
? 4 : 16);
1548 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1549 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1551 if ((cmd
== RTM_NEWROUTE
) &&
1552 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1553 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1555 req
.n
.nlmsg_type
= cmd
;
1557 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1559 req
.r
.rtm_family
= family
;
1560 req
.r
.rtm_dst_len
= p
->prefixlen
;
1561 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1562 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1564 if (cmd
== RTM_DELROUTE
)
1565 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1567 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1570 * blackhole routes are not RTN_UNICAST, they are
1571 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1572 * so setting this value as a RTN_UNICAST would
1573 * cause the route lookup of just the prefix
1574 * to fail. So no need to specify this for
1575 * the RTM_DELROUTE case
1577 if (cmd
!= RTM_DELROUTE
)
1578 req
.r
.rtm_type
= RTN_UNICAST
;
1580 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1582 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1586 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1588 * either by the kernel or by zebra. Its purely for calculating best
1590 * by the routing protocol and for communicating with protocol peers.
1592 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1594 #if defined(SUPPORT_REALMS)
1598 if (cmd
== RTM_DELROUTE
)
1599 tag
= dplane_ctx_get_old_tag(ctx
);
1601 tag
= dplane_ctx_get_tag(ctx
);
1603 if (tag
> 0 && tag
<= 255)
1604 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1607 /* Table corresponding to this route. */
1608 table_id
= dplane_ctx_get_table(ctx
);
1610 req
.r
.rtm_table
= table_id
;
1612 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1613 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1616 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1619 * If we are not updating the route and we have received
1620 * a route delete, then all we need to fill in is the
1621 * prefix information to tell the kernel to schwack
1624 if (cmd
== RTM_DELROUTE
)
1627 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1628 char buf
[NL_PKT_BUF_SIZE
];
1629 struct rtattr
*rta
= (void *)buf
;
1630 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1631 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1633 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1635 rta
->rta_type
= RTA_METRICS
;
1636 rta
->rta_len
= RTA_LENGTH(0);
1637 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1638 RTAX_MTU
, &mtu
, sizeof(mtu
));
1639 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1643 if (kernel_nexthops_supported()) {
1644 if (IS_ZEBRA_DEBUG_KERNEL
)
1646 "netlink_route_multipath(): %pFX nhg_id is %u",
1647 p
, dplane_ctx_get_nhe_id(ctx
));
1648 /* Kernel supports nexthop objects */
1649 addattr32(&req
.n
, sizeof(req
), RTA_NH_ID
,
1650 dplane_ctx_get_nhe_id(ctx
));
1654 /* Count overall nexthops so we can decide whether to use singlepath
1655 * or multipath case.
1658 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1659 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1661 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1667 /* Singlepath case. */
1668 if (nexthop_num
== 1) {
1670 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1672 * So we want to cover 2 types of blackhole
1674 * 1) A normal blackhole route( ala from a static
1676 * 2) A recursively resolved blackhole route
1678 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1679 switch (nexthop
->bh_type
) {
1680 case BLACKHOLE_ADMINPROHIB
:
1681 req
.r
.rtm_type
= RTN_PROHIBIT
;
1683 case BLACKHOLE_REJECT
:
1684 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1687 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1692 if (CHECK_FLAG(nexthop
->flags
,
1693 NEXTHOP_FLAG_RECURSIVE
)) {
1698 if (family
== AF_INET
) {
1699 if (nexthop
->rmap_src
.ipv4
.s_addr
1702 nexthop
->rmap_src
.ipv4
;
1704 } else if (nexthop
->src
.ipv4
.s_addr
1710 } else if (family
== AF_INET6
) {
1711 if (!IN6_IS_ADDR_UNSPECIFIED(
1712 &nexthop
->rmap_src
.ipv6
)) {
1714 nexthop
->rmap_src
.ipv6
;
1717 !IN6_IS_ADDR_UNSPECIFIED(
1718 &nexthop
->src
.ipv6
)) {
1727 if ((cmd
== RTM_NEWROUTE
1728 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1729 routedesc
= nexthop
->rparent
1730 ? "recursive, single-path"
1733 _netlink_route_build_singlepath(
1734 p
, routedesc
, bytelen
, nexthop
, &req
.n
,
1735 &req
.r
, sizeof(req
), cmd
);
1740 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1741 if (family
== AF_INET
)
1742 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1743 &src
.ipv4
, bytelen
);
1744 else if (family
== AF_INET6
)
1745 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1746 &src
.ipv6
, bytelen
);
1748 } else { /* Multipath case */
1749 char buf
[NL_PKT_BUF_SIZE
];
1750 struct rtattr
*rta
= (void *)buf
;
1751 struct rtnexthop
*rtnh
;
1752 const union g_addr
*src1
= NULL
;
1754 rta
->rta_type
= RTA_MULTIPATH
;
1755 rta
->rta_len
= RTA_LENGTH(0);
1756 rtnh
= RTA_DATA(rta
);
1759 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1760 if (CHECK_FLAG(nexthop
->flags
,
1761 NEXTHOP_FLAG_RECURSIVE
)) {
1762 /* This only works for IPv4 now */
1766 if (family
== AF_INET
) {
1767 if (nexthop
->rmap_src
.ipv4
.s_addr
1770 nexthop
->rmap_src
.ipv4
;
1772 } else if (nexthop
->src
.ipv4
.s_addr
1778 } else if (family
== AF_INET6
) {
1779 if (!IN6_IS_ADDR_UNSPECIFIED(
1780 &nexthop
->rmap_src
.ipv6
)) {
1782 nexthop
->rmap_src
.ipv6
;
1785 !IN6_IS_ADDR_UNSPECIFIED(
1786 &nexthop
->src
.ipv6
)) {
1796 if ((cmd
== RTM_NEWROUTE
1797 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1798 routedesc
= nexthop
->rparent
1799 ? "recursive, multipath"
1803 _netlink_route_build_multipath(
1804 p
, routedesc
, bytelen
, nexthop
, rta
,
1805 rtnh
, &req
.r
, &src1
);
1806 rtnh
= RTNH_NEXT(rtnh
);
1808 if (!setsrc
&& src1
) {
1809 if (family
== AF_INET
)
1810 src
.ipv4
= src1
->ipv4
;
1811 else if (family
== AF_INET6
)
1812 src
.ipv6
= src1
->ipv6
;
1818 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1819 if (family
== AF_INET
)
1820 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1821 &src
.ipv4
, bytelen
);
1822 else if (family
== AF_INET6
)
1823 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1824 &src
.ipv6
, bytelen
);
1825 if (IS_ZEBRA_DEBUG_KERNEL
)
1826 zlog_debug("Setting source");
1829 if (rta
->rta_len
> RTA_LENGTH(0))
1830 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1831 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1834 /* If there is no useful nexthop then return. */
1835 if (nexthop_num
== 0) {
1836 if (IS_ZEBRA_DEBUG_KERNEL
)
1838 "netlink_route_multipath(): No useful nexthop.");
1843 /* Talk to netlink socket. */
1844 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1845 dplane_ctx_get_ns(ctx
), 0);
1848 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1850 uint32_t actual_table
;
1852 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1860 struct zebra_ns
*zns
;
1863 memset(&req
, 0, sizeof(req
));
1865 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1866 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1867 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1869 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1870 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1872 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1873 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1874 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1875 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1879 * So during the namespace cleanup we started storing
1880 * the zvrf table_id for the default table as RT_TABLE_MAIN
1881 * which is what the normal routing table for ip routing is.
1882 * This change caused this to break our lookups of sg data
1883 * because prior to this change the zvrf->table_id was 0
1884 * and when the pim multicast kernel code saw a 0,
1885 * it was auto-translated to RT_TABLE_DEFAULT. But since
1886 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1887 * and the kernel goes screw you and the delicious cookies you
1888 * are trying to give me. So now we have this little hack.
1890 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1892 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1894 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1895 &zns
->netlink_cmd
, zns
, 0);
1901 /* Char length to debug ID with */
1902 #define ID_LENGTH 10
1904 static void _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
1906 const struct nh_grp
*z_grp
,
1907 const uint8_t count
)
1909 struct nexthop_grp grp
[count
];
1910 /* Need space for max group size, "/", and null term */
1911 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
1912 char buf1
[ID_LENGTH
+ 2];
1916 memset(grp
, 0, sizeof(grp
));
1919 for (int i
= 0; i
< count
; i
++) {
1920 grp
[i
].id
= z_grp
[i
].id
;
1921 grp
[i
].weight
= z_grp
[i
].weight
- 1;
1923 if (IS_ZEBRA_DEBUG_KERNEL
) {
1925 snprintf(buf
, sizeof(buf1
), "group %u",
1928 snprintf(buf1
, sizeof(buf1
), "/%u",
1930 strlcat(buf
, buf1
, sizeof(buf
));
1934 addattr_l(n
, req_size
, NHA_GROUP
, grp
, count
* sizeof(*grp
));
1937 if (IS_ZEBRA_DEBUG_KERNEL
)
1938 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
1942 * netlink_nexthop() - Nexthop change via the netlink interface
1944 * @ctx: Dataplane ctx
1946 * Return: Result status
1948 static int netlink_nexthop(int cmd
, struct zebra_dplane_ctx
*ctx
)
1953 char buf
[NL_PKT_BUF_SIZE
];
1956 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1957 char label_buf
[256];
1959 size_t req_size
= sizeof(req
);
1961 /* Nothing to do if the kernel doesn't support nexthop objects */
1962 if (!kernel_nexthops_supported())
1965 label_buf
[0] = '\0';
1967 memset(&req
, 0, req_size
);
1969 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
1970 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1972 if (cmd
== RTM_NEWNEXTHOP
)
1973 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1975 req
.n
.nlmsg_type
= cmd
;
1976 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1978 req
.nhm
.nh_family
= AF_UNSPEC
;
1981 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
1985 EC_ZEBRA_NHG_FIB_UPDATE
,
1986 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1990 addattr32(&req
.n
, req_size
, NHA_ID
, id
);
1992 if (cmd
== RTM_NEWNEXTHOP
) {
1993 if (dplane_ctx_get_nhe_nh_grp_count(ctx
))
1994 _netlink_nexthop_build_group(
1995 &req
.n
, req_size
, id
,
1996 dplane_ctx_get_nhe_nh_grp(ctx
),
1997 dplane_ctx_get_nhe_nh_grp_count(ctx
));
1999 const struct nexthop
*nh
=
2000 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2001 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2004 req
.nhm
.nh_family
= AF_INET
;
2005 else if (afi
== AFI_IP6
)
2006 req
.nhm
.nh_family
= AF_INET6
;
2009 case NEXTHOP_TYPE_IPV4
:
2010 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2011 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2012 &nh
->gate
.ipv4
, IPV4_MAX_BYTELEN
);
2014 case NEXTHOP_TYPE_IPV6
:
2015 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2016 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2017 &nh
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
2019 case NEXTHOP_TYPE_BLACKHOLE
:
2020 addattr_l(&req
.n
, req_size
, NHA_BLACKHOLE
, NULL
,
2022 /* Blackhole shouldn't have anymore attributes
2025 case NEXTHOP_TYPE_IFINDEX
:
2026 /* Don't need anymore info for this */
2032 EC_ZEBRA_NHG_FIB_UPDATE
,
2033 "Context received for kernel nexthop update without an interface");
2037 addattr32(&req
.n
, req_size
, NHA_OIF
, nh
->ifindex
);
2039 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2040 req
.nhm
.nh_flags
|= RTNH_F_ONLINK
;
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 __func__
, 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
+ 1;
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
= VRF_DEFAULT
;
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
);
2386 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2389 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2392 "%s: Message received from netlink is of a broken size %d %zu",
2393 __func__
, h
->nlmsg_len
,
2394 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2398 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2403 EC_ZEBRA_BAD_NHG_MESSAGE
,
2404 "Nexthop group without an ID received from the kernel");
2408 /* We use the ID key'd nhg table for kernel updates */
2409 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2411 family
= nhm
->nh_family
;
2412 afi
= family2afi(family
);
2414 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2416 if (IS_ZEBRA_DEBUG_KERNEL
)
2417 zlog_debug("%s ID (%u) %s NS %u",
2418 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2419 nl_family_to_str(family
), ns_id
);
2422 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2423 if (tb
[NHA_GROUP
]) {
2425 * If this is a group message its only going to have
2426 * an array of nexthop IDs associated with it
2428 grp_count
= netlink_nexthop_process_group(
2429 tb
, grp
, array_size(grp
));
2431 if (tb
[NHA_BLACKHOLE
]) {
2433 * This nexthop is just for blackhole-ing
2434 * traffic, it should not have an OIF, GATEWAY,
2437 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2438 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2439 } else if (tb
[NHA_OIF
])
2441 * This is a true new nexthop, so we need
2442 * to parse the gateway and device info
2444 nh
= netlink_nexthop_process_nh(tb
, family
,
2449 EC_ZEBRA_BAD_NHG_MESSAGE
,
2450 "Invalid Nexthop message received from the kernel with ID (%u)",
2454 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2455 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2456 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2460 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2464 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2465 zebra_nhg_kernel_del(id
, vrf_id
);
2471 * netlink_request_nexthop() - Request nextop information from the kernel
2472 * @zns: Zebra namespace
2473 * @family: AF_* netlink family
2474 * @type: RTM_* route type
2476 * Return: Result status
2478 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2485 /* Form the request, specifying filter (rtattr) if needed. */
2486 memset(&req
, 0, sizeof(req
));
2487 req
.n
.nlmsg_type
= type
;
2488 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2489 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2490 req
.nhm
.nh_family
= family
;
2492 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2497 * netlink_nexthop_read() - Nexthop read function using netlink interface
2499 * @zns: Zebra name space
2501 * Return: Result status
2502 * Only called at bootstrap time.
2504 int netlink_nexthop_read(struct zebra_ns
*zns
)
2507 struct zebra_dplane_info dp_info
;
2509 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2511 /* Get nexthop objects */
2512 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2515 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2519 /* If we succesfully read in nexthop objects,
2520 * this kernel must support them.
2524 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2525 zlog_debug("Nexthop objects %ssupported on this kernel",
2526 supports_nh
? "" : "not ");
2532 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2533 int llalen
, ns_id_t ns_id
)
2535 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2536 addr
, lla
, llalen
, ns_id
);
2540 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2541 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2543 static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2546 uint8_t protocol
= RTPROT_ZEBRA
;
2552 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2553 const struct ipaddr
*addr
;
2555 memset(&req
, 0, sizeof(req
));
2557 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2558 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2559 if (cmd
== RTM_NEWNEIGH
)
2560 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2561 req
.n
.nlmsg_type
= cmd
;
2562 req
.ndm
.ndm_family
= PF_BRIDGE
;
2563 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2564 req
.ndm
.ndm_flags
|= NTF_SELF
; /* Handle by "self", not "master" */
2567 addattr_l(&req
.n
, sizeof(req
),
2568 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2569 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2570 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2572 addr
= dplane_ctx_neigh_get_ipaddr(ctx
);
2574 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &(addr
->ipaddr_v4
), 4);
2576 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2577 dplane_ctx_get_ns(ctx
), 0);
2581 #define NDA_RTA(r) \
2582 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2585 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2588 struct interface
*ifp
;
2589 struct zebra_if
*zif
;
2590 struct rtattr
*tb
[NDA_MAX
+ 1];
2591 struct interface
*br_if
;
2594 struct in_addr vtep_ip
;
2595 int vid_present
= 0, dst_present
= 0;
2596 char buf
[ETHER_ADDR_STRLEN
];
2601 ndm
= NLMSG_DATA(h
);
2603 /* We only process macfdb notifications if EVPN is enabled */
2604 if (!is_evpn_enabled())
2607 /* Parse attributes and extract fields of interest. Do basic
2608 * validation of the fields.
2610 memset(tb
, 0, sizeof tb
);
2611 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2613 if (!tb
[NDA_LLADDR
]) {
2614 if (IS_ZEBRA_DEBUG_KERNEL
)
2615 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2616 nl_msg_type_to_str(h
->nlmsg_type
),
2621 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2622 if (IS_ZEBRA_DEBUG_KERNEL
)
2624 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2625 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
2626 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2630 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2632 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2634 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2635 sprintf(vid_buf
, " VLAN %u", vid
);
2639 /* TODO: Only IPv4 supported now. */
2641 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
2643 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2646 if (IS_ZEBRA_DEBUG_KERNEL
)
2647 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %s%s",
2648 nl_msg_type_to_str(h
->nlmsg_type
),
2649 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2650 ndm
->ndm_state
, ndm
->ndm_flags
,
2651 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2652 dst_present
? dst_buf
: "");
2654 /* The interface should exist. */
2655 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2657 if (!ifp
|| !ifp
->info
)
2660 /* The interface should be something we're interested in. */
2661 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2664 zif
= (struct zebra_if
*)ifp
->info
;
2665 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2666 if (IS_ZEBRA_DEBUG_KERNEL
)
2668 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2669 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
2671 zif
->brslave_info
.bridge_ifindex
);
2675 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2677 if (filter_vlan
&& vid
!= filter_vlan
) {
2678 if (IS_ZEBRA_DEBUG_KERNEL
)
2679 zlog_debug(" Filtered due to filter vlan: %d",
2684 /* If add or update, do accordingly if learnt on a "local" interface; if
2685 * the notification is over VxLAN, this has to be related to
2687 * so perform an implicit delete of any local entry (if it exists).
2689 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2690 /* Drop "permanent" entries. */
2691 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2692 if (IS_ZEBRA_DEBUG_KERNEL
)
2694 " Dropping entry because of NUD_PERMANENT");
2698 if (IS_ZEBRA_IF_VXLAN(ifp
))
2699 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2702 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2706 /* This is a delete notification.
2707 * Ignore the notification with IP dest as it may just signify that the
2708 * MAC has moved from remote to local. The exception is the special
2709 * all-zeros MAC that represents the BUM flooding entry; we may have
2710 * to readd it. Otherwise,
2711 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2712 * 2. For a MAC over "local" interface, delete the mac
2713 * Note: We will get notifications from both bridge driver and VxLAN
2717 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2719 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
2720 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
2724 if (IS_ZEBRA_IF_VXLAN(ifp
))
2725 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2728 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2731 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2736 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2739 /* Length validity. */
2740 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2744 /* We are interested only in AF_BRIDGE notifications. */
2745 ndm
= NLMSG_DATA(h
);
2746 if (ndm
->ndm_family
!= AF_BRIDGE
)
2749 return netlink_macfdb_change(h
, len
, ns_id
);
2752 /* Request for MAC FDB information from the kernel */
2753 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2754 int type
, ifindex_t master_ifindex
)
2758 struct ifinfomsg ifm
;
2762 /* Form the request, specifying filter (rtattr) if needed. */
2763 memset(&req
, 0, sizeof(req
));
2764 req
.n
.nlmsg_type
= type
;
2765 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2766 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2767 req
.ifm
.ifi_family
= family
;
2769 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2771 return netlink_request(netlink_cmd
, &req
.n
);
2775 * MAC forwarding database read using netlink interface. This is invoked
2778 int netlink_macfdb_read(struct zebra_ns
*zns
)
2781 struct zebra_dplane_info dp_info
;
2783 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2785 /* Get bridge FDB table. */
2786 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2790 /* We are reading entire table. */
2792 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2799 * MAC forwarding database read using netlink interface. This is for a
2800 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2802 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2803 struct interface
*br_if
)
2805 struct zebra_if
*br_zif
;
2806 struct zebra_if
*zif
;
2807 struct zebra_l2info_vxlan
*vxl
;
2808 struct zebra_dplane_info dp_info
;
2811 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2813 /* Save VLAN we're filtering on, if needed. */
2814 br_zif
= (struct zebra_if
*)br_if
->info
;
2815 zif
= (struct zebra_if
*)ifp
->info
;
2816 vxl
= &zif
->l2info
.vxl
;
2817 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2818 filter_vlan
= vxl
->access_vlan
;
2820 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2822 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2826 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2829 /* Reset VLAN filter. */
2835 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2836 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2839 struct interface
*br_if
,
2840 struct ethaddr
*mac
,
2848 struct zebra_if
*br_zif
;
2849 char buf
[ETHER_ADDR_STRLEN
];
2851 memset(&req
, 0, sizeof(req
));
2852 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2853 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2854 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2855 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2856 /* req.ndm.ndm_state = NUD_REACHABLE; */
2858 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2860 br_zif
= (struct zebra_if
*)br_if
->info
;
2861 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2862 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2864 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2866 if (IS_ZEBRA_DEBUG_KERNEL
)
2867 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u", __func__
,
2868 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2870 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2872 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2875 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2876 struct interface
*br_if
,
2877 struct ethaddr
*mac
, vlanid_t vid
)
2880 struct zebra_dplane_info dp_info
;
2882 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2884 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2886 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2892 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2899 * Netlink-specific handler for MAC updates using dataplane context object.
2901 static enum zebra_dplane_result
2902 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2904 uint8_t protocol
= RTPROT_ZEBRA
;
2912 int vid_present
= 0;
2914 struct in_addr vtep_ip
;
2917 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2922 memset(&req
, 0, sizeof(req
));
2924 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2925 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2926 if (cmd
== RTM_NEWNEIGH
)
2927 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2928 req
.n
.nlmsg_type
= cmd
;
2929 req
.ndm
.ndm_family
= AF_BRIDGE
;
2930 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2931 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2933 if (dplane_ctx_mac_is_sticky(ctx
))
2934 req
.ndm
.ndm_state
|= NUD_NOARP
;
2936 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2938 addattr_l(&req
.n
, sizeof(req
),
2939 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2940 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2941 dplane_ctx_mac_get_addr(ctx
), 6);
2942 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2944 dst_alen
= 4; // TODO: hardcoded
2945 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2946 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2948 vid
= dplane_ctx_mac_get_vlan(ctx
);
2951 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2954 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
,
2955 dplane_ctx_mac_get_br_ifindex(ctx
));
2957 if (IS_ZEBRA_DEBUG_KERNEL
) {
2958 char ipbuf
[PREFIX_STRLEN
];
2959 char buf
[ETHER_ADDR_STRLEN
];
2960 char dst_buf
[PREFIX_STRLEN
+ 10];
2964 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2968 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2969 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2970 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2972 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2973 nl_msg_type_to_str(cmd
),
2974 nl_family_to_str(req
.ndm
.ndm_family
),
2975 dplane_ctx_get_ifname(ctx
),
2976 dplane_ctx_get_ifindex(ctx
), vid_buf
,
2977 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2981 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
2982 dplane_ctx_get_ns(ctx
), 0);
2984 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2986 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2990 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2991 * 5549 support, re-install them.
2993 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
2994 struct interface
*ifp
, struct ipaddr
*ip
,
2997 if (ndm
->ndm_family
!= AF_INET
)
3000 if (!zif
->v6_2_v4_ll_neigh_entry
)
3003 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3006 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3007 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3012 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3016 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3019 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3022 struct interface
*ifp
;
3023 struct zebra_if
*zif
;
3024 struct rtattr
*tb
[NDA_MAX
+ 1];
3025 struct interface
*link_if
;
3028 char buf
[ETHER_ADDR_STRLEN
];
3029 char buf2
[INET6_ADDRSTRLEN
];
3030 int mac_present
= 0;
3034 ndm
= NLMSG_DATA(h
);
3036 /* The interface should exist. */
3037 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3039 if (!ifp
|| !ifp
->info
)
3042 zif
= (struct zebra_if
*)ifp
->info
;
3044 /* Parse attributes and extract fields of interest. */
3045 memset(tb
, 0, sizeof(tb
));
3046 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3049 zlog_debug("%s family %s IF %s(%u) - no DST",
3050 nl_msg_type_to_str(h
->nlmsg_type
),
3051 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3056 memset(&ip
, 0, sizeof(struct ipaddr
));
3057 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3058 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3060 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3061 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3062 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3063 if (IS_ZEBRA_DEBUG_KERNEL
)
3065 "\tNeighbor Entry Received is a 5549 entry, finished");
3069 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3070 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3071 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3073 /* The neighbor is present on an SVI. From this, we locate the
3075 * bridge because we're only interested in neighbors on a VxLAN bridge.
3076 * The bridge is located based on the nature of the SVI:
3077 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3079 * and is linked to the bridge
3080 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3084 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3085 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3089 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3092 if (IS_ZEBRA_DEBUG_KERNEL
)
3094 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3098 memset(&mac
, 0, sizeof(struct ethaddr
));
3099 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3100 if (tb
[NDA_LLADDR
]) {
3101 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3102 if (IS_ZEBRA_DEBUG_KERNEL
)
3104 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3109 ifp
->name
, ndm
->ndm_ifindex
,
3110 (unsigned long)RTA_PAYLOAD(
3116 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3119 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3120 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3122 if (IS_ZEBRA_DEBUG_KERNEL
)
3124 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3125 nl_msg_type_to_str(h
->nlmsg_type
),
3126 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3128 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3130 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3132 ndm
->ndm_state
, ndm
->ndm_flags
);
3134 /* If the neighbor state is valid for use, process as an add or
3136 * else process as a delete. Note that the delete handling may
3138 * in re-adding the neighbor if it is a valid "remote" neighbor.
3140 if (ndm
->ndm_state
& NUD_VALID
)
3141 return zebra_vxlan_handle_kernel_neigh_update(
3142 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3145 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3148 if (IS_ZEBRA_DEBUG_KERNEL
)
3149 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3150 nl_msg_type_to_str(h
->nlmsg_type
),
3151 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3153 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3155 /* Process the delete - it may result in re-adding the neighbor if it is
3156 * a valid "remote" neighbor.
3158 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3161 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3166 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3169 /* Length validity. */
3170 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3174 /* We are interested only in AF_INET or AF_INET6 notifications. */
3175 ndm
= NLMSG_DATA(h
);
3176 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3179 return netlink_neigh_change(h
, len
);
3182 /* Request for IP neighbor information from the kernel */
3183 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3184 int type
, ifindex_t ifindex
)
3192 /* Form the request, specifying filter (rtattr) if needed. */
3193 memset(&req
, 0, sizeof(req
));
3194 req
.n
.nlmsg_type
= type
;
3195 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3196 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3197 req
.ndm
.ndm_family
= family
;
3199 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3201 return netlink_request(netlink_cmd
, &req
.n
);
3205 * IP Neighbor table read using netlink interface. This is invoked
3208 int netlink_neigh_read(struct zebra_ns
*zns
)
3211 struct zebra_dplane_info dp_info
;
3213 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3215 /* Get IP neighbor table. */
3216 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3220 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3227 * IP Neighbor table read using netlink interface. This is for a specific
3230 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3233 struct zebra_dplane_info dp_info
;
3235 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3237 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3241 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3248 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3249 * read using netlink interface.
3251 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3252 int type
, struct ipaddr
*ip
,
3262 /* Form the request, specifying filter (rtattr) if needed. */
3263 memset(&req
, 0, sizeof(req
));
3264 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3265 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3266 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3267 req
.ndm
.ndm_ifindex
= ifindex
;
3269 if (IS_IPADDR_V4(ip
)) {
3270 ipa_len
= IPV4_MAX_BYTELEN
;
3271 req
.ndm
.ndm_family
= AF_INET
;
3274 ipa_len
= IPV6_MAX_BYTELEN
;
3275 req
.ndm
.ndm_family
= AF_INET6
;
3278 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3280 if (IS_ZEBRA_DEBUG_KERNEL
) {
3281 char buf
[INET6_ADDRSTRLEN
];
3283 zlog_debug("%s: Tx %s family %s IF %u IP %s flags 0x%x",
3284 __func__
, nl_msg_type_to_str(type
),
3285 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
,
3286 ipaddr2str(ip
, buf
, sizeof(buf
)), req
.n
.nlmsg_flags
);
3289 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
3292 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3293 struct interface
*vlan_if
)
3296 struct zebra_ns
*zns
;
3297 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3298 char buf
[INET6_ADDRSTRLEN
];
3299 struct zebra_dplane_info dp_info
;
3303 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3305 if (IS_ZEBRA_DEBUG_KERNEL
)
3306 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3307 __func__
, vlan_if
->name
, vlan_if
->ifindex
,
3308 ipaddr2str(ip
, buf
, sizeof(buf
)), vlan_if
->vrf_id
);
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
));
3333 "%s: Message received from netlink is of a broken size %d %zu",
3334 __func__
, h
->nlmsg_len
,
3335 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3339 /* Is this a notification for the MAC FDB or IP neighbor table? */
3340 ndm
= NLMSG_DATA(h
);
3341 if (ndm
->ndm_family
== AF_BRIDGE
)
3342 return netlink_macfdb_change(h
, len
, ns_id
);
3344 if (ndm
->ndm_type
!= RTN_UNICAST
)
3347 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3348 return netlink_ipneigh_change(h
, len
, ns_id
);
3351 EC_ZEBRA_UNKNOWN_FAMILY
,
3352 "Invalid address family: %u received from kernel neighbor change: %s",
3353 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3361 * Utility neighbor-update function, using info from dplane context.
3363 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3366 uint8_t protocol
= RTPROT_ZEBRA
;
3373 char buf
[INET6_ADDRSTRLEN
];
3374 char buf2
[ETHER_ADDR_STRLEN
];
3375 const struct ipaddr
*ip
;
3376 const struct ethaddr
*mac
;
3380 memset(&req
, 0, sizeof(req
));
3382 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3383 mac
= dplane_ctx_neigh_get_mac(ctx
);
3384 if (is_zero_mac(mac
))
3387 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3388 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3390 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3391 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3392 if (cmd
== RTM_NEWNEIGH
)
3393 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3394 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
3395 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3396 req
.ndm
.ndm_state
= state
;
3397 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3398 req
.ndm
.ndm_type
= RTN_UNICAST
;
3399 req
.ndm
.ndm_flags
= flags
;
3401 addattr_l(&req
.n
, sizeof(req
),
3402 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
3403 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3404 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3406 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3408 if (IS_ZEBRA_DEBUG_KERNEL
)
3409 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3410 nl_msg_type_to_str(cmd
),
3411 nl_family_to_str(req
.ndm
.ndm_family
),
3412 dplane_ctx_get_ifname(ctx
),
3413 dplane_ctx_get_ifindex(ctx
),
3414 ipaddr2str(ip
, buf
, sizeof(buf
)),
3415 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
3419 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3420 dplane_ctx_get_ns(ctx
), 0);
3424 * Update MAC, using dataplane context object.
3426 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
3428 return netlink_macfdb_update_ctx(ctx
);
3431 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
3435 switch (dplane_ctx_get_op(ctx
)) {
3436 case DPLANE_OP_NEIGH_INSTALL
:
3437 case DPLANE_OP_NEIGH_UPDATE
:
3438 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
3440 case DPLANE_OP_NEIGH_DELETE
:
3441 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
3443 case DPLANE_OP_VTEP_ADD
:
3444 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
);
3446 case DPLANE_OP_VTEP_DELETE
:
3447 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
);
3454 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
3458 * MPLS label forwarding table change via netlink interface, using dataplane
3459 * context information.
3461 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
3464 const zebra_nhlfe_t
*nhlfe
;
3465 struct nexthop
*nexthop
= NULL
;
3466 unsigned int nexthop_num
;
3467 const char *routedesc
;
3469 struct prefix p
= {0};
3474 char buf
[NL_PKT_BUF_SIZE
];
3477 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
3480 * Count # nexthops so we can decide whether to use singlepath
3481 * or multipath case.
3484 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
3485 nexthop
= nhlfe
->nexthop
;
3488 if (cmd
== RTM_NEWROUTE
) {
3489 /* Count all selected NHLFEs */
3490 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3491 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3494 /* Count all installed NHLFEs */
3495 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3496 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3501 if ((nexthop_num
== 0) ||
3502 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3505 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3506 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3507 req
.n
.nlmsg_type
= cmd
;
3508 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3510 req
.r
.rtm_family
= AF_MPLS
;
3511 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3512 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3513 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3514 req
.r
.rtm_type
= RTN_UNICAST
;
3516 if (cmd
== RTM_NEWROUTE
) {
3517 /* We do a replace to handle update. */
3518 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3520 /* set the protocol value if installing */
3521 route_type
= re_type_from_lsp_type(
3522 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3523 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3526 /* Fill destination */
3527 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3528 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3530 /* Fill nexthops (paths) based on single-path or multipath. The paths
3531 * chosen depend on the operation.
3533 if (nexthop_num
== 1) {
3534 routedesc
= "single-path";
3535 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3539 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3540 nhlfe
; nhlfe
= nhlfe
->next
) {
3541 nexthop
= nhlfe
->nexthop
;
3545 if ((cmd
== RTM_NEWROUTE
3546 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3547 && CHECK_FLAG(nexthop
->flags
,
3548 NEXTHOP_FLAG_ACTIVE
)))
3549 || (cmd
== RTM_DELROUTE
3550 && (CHECK_FLAG(nhlfe
->flags
,
3551 NHLFE_FLAG_INSTALLED
)
3552 && CHECK_FLAG(nexthop
->flags
,
3553 NEXTHOP_FLAG_FIB
)))) {
3554 /* Add the gateway */
3555 _netlink_mpls_build_singlepath(
3556 &p
, routedesc
, nhlfe
, &req
.n
, &req
.r
,
3563 } else { /* Multipath case */
3564 char buf
[NL_PKT_BUF_SIZE
];
3565 struct rtattr
*rta
= (void *)buf
;
3566 struct rtnexthop
*rtnh
;
3567 const union g_addr
*src1
= NULL
;
3569 rta
->rta_type
= RTA_MULTIPATH
;
3570 rta
->rta_len
= RTA_LENGTH(0);
3571 rtnh
= RTA_DATA(rta
);
3573 routedesc
= "multipath";
3574 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3578 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3579 nhlfe
; nhlfe
= nhlfe
->next
) {
3580 nexthop
= nhlfe
->nexthop
;
3584 if ((cmd
== RTM_NEWROUTE
3585 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3586 && CHECK_FLAG(nexthop
->flags
,
3587 NEXTHOP_FLAG_ACTIVE
)))
3588 || (cmd
== RTM_DELROUTE
3589 && (CHECK_FLAG(nhlfe
->flags
,
3590 NHLFE_FLAG_INSTALLED
)
3591 && CHECK_FLAG(nexthop
->flags
,
3592 NEXTHOP_FLAG_FIB
)))) {
3595 /* Build the multipath */
3596 _netlink_mpls_build_multipath(&p
, routedesc
,
3599 rtnh
= RTNH_NEXT(rtnh
);
3603 /* Add the multipath */
3604 if (rta
->rta_len
> RTA_LENGTH(0))
3605 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3606 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3609 /* Talk to netlink socket. */
3610 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3611 dplane_ctx_get_ns(ctx
), 0);
3613 #endif /* HAVE_NETLINK */