1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <net/if_arp.h>
26 #include <linux/lwtunnel.h>
27 #include <linux/mpls_iptunnel.h>
28 #include <linux/neighbour.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/nexthop.h>
32 /* Hack for GNU libc version 2. */
34 #define MSG_TRUNC 0x20
35 #endif /* MSG_TRUNC */
41 #include "connected.h"
44 #include "zebra_memory.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/rt_netlink.h"
67 #include "zebra/zebra_nhg.h"
68 #include "zebra/zebra_mroute.h"
69 #include "zebra/zebra_vxlan.h"
70 #include "zebra/zebra_errors.h"
76 static vlanid_t filter_vlan
= 0;
78 static bool supports_nh
;
86 static const char ipv4_ll_buf
[16] = "169.254.0.1";
87 static struct in_addr ipv4_ll
;
90 * The ipv4_ll data structure is used for all 5549
91 * additions to the kernel. Let's figure out the
92 * correct value one time instead for every
93 * install/remove of a 5549 type route
95 void rt_netlink_init(void)
97 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
101 * Mapping from dataplane neighbor flags to netlink flags
103 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
107 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
108 flags
|= NTF_EXT_LEARNED
;
109 if (dplane_flags
& DPLANE_NTF_ROUTER
)
116 * Mapping from dataplane neighbor state to netlink state
118 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
122 if (dplane_state
& DPLANE_NUD_REACHABLE
)
123 state
|= NUD_REACHABLE
;
124 if (dplane_state
& DPLANE_NUD_STALE
)
126 if (dplane_state
& DPLANE_NUD_NOARP
)
128 if (dplane_state
& DPLANE_NUD_PROBE
)
135 static inline int is_selfroute(int proto
)
137 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
138 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
139 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
140 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
141 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
142 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
143 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
150 static inline int zebra2proto(int proto
)
153 case ZEBRA_ROUTE_BABEL
:
154 proto
= RTPROT_BABEL
;
156 case ZEBRA_ROUTE_BGP
:
159 case ZEBRA_ROUTE_OSPF
:
160 case ZEBRA_ROUTE_OSPF6
:
163 case ZEBRA_ROUTE_STATIC
:
164 proto
= RTPROT_ZSTATIC
;
166 case ZEBRA_ROUTE_ISIS
:
169 case ZEBRA_ROUTE_RIP
:
172 case ZEBRA_ROUTE_RIPNG
:
173 proto
= RTPROT_RIPNG
;
175 case ZEBRA_ROUTE_NHRP
:
178 case ZEBRA_ROUTE_EIGRP
:
179 proto
= RTPROT_EIGRP
;
181 case ZEBRA_ROUTE_LDP
:
184 case ZEBRA_ROUTE_SHARP
:
185 proto
= RTPROT_SHARP
;
187 case ZEBRA_ROUTE_PBR
:
190 case ZEBRA_ROUTE_OPENFABRIC
:
191 proto
= RTPROT_OPENFABRIC
;
193 case ZEBRA_ROUTE_TABLE
:
194 case ZEBRA_ROUTE_NHG
:
195 proto
= RTPROT_ZEBRA
;
199 * When a user adds a new protocol this will show up
200 * to let them know to do something about it. This
201 * is intentionally a warn because we should see
202 * this as part of development of a new protocol
205 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
206 __PRETTY_FUNCTION__
, proto
);
207 proto
= RTPROT_ZEBRA
;
214 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
218 proto
= ZEBRA_ROUTE_BABEL
;
221 proto
= ZEBRA_ROUTE_BGP
;
224 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
228 proto
= ZEBRA_ROUTE_ISIS
;
231 proto
= ZEBRA_ROUTE_RIP
;
234 proto
= ZEBRA_ROUTE_RIPNG
;
237 proto
= ZEBRA_ROUTE_NHRP
;
240 proto
= ZEBRA_ROUTE_EIGRP
;
243 proto
= ZEBRA_ROUTE_LDP
;
247 proto
= ZEBRA_ROUTE_STATIC
;
250 proto
= ZEBRA_ROUTE_SHARP
;
253 proto
= ZEBRA_ROUTE_PBR
;
255 case RTPROT_OPENFABRIC
:
256 proto
= ZEBRA_ROUTE_OPENFABRIC
;
260 proto
= ZEBRA_ROUTE_NHG
;
263 /* Intentional fall thru */
266 * When a user adds a new protocol this will show up
267 * to let them know to do something about it. This
268 * is intentionally a warn because we should see
269 * this as part of development of a new protocol
272 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
273 __PRETTY_FUNCTION__
, proto
);
274 proto
= ZEBRA_ROUTE_KERNEL
;
281 Pending: create an efficient table_id (in a tree/hash) based lookup)
283 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
286 struct zebra_vrf
*zvrf
;
288 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
292 /* case vrf with netns : match the netnsid */
293 if (vrf_is_backend_netns()) {
294 if (ns_id
== zvrf_id(zvrf
))
295 return zvrf_id(zvrf
);
297 /* VRF is VRF_BACKEND_VRF_LITE */
298 if (zvrf
->table_id
!= table_id
)
300 return zvrf_id(zvrf
);
308 * @parse_encap_mpls() - Parses encapsulated mpls attributes
309 * @tb: Pointer to rtattr to look for nested items in.
310 * @labels: Pointer to store labels in.
312 * Return: Number of mpls labels found.
314 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
316 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
317 mpls_lse_t
*lses
= NULL
;
322 mpls_label_t label
= 0;
324 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
325 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
326 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
327 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
328 labels
[num_labels
++] = label
;
334 static struct nexthop
335 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
336 enum blackhole_type bh_type
, int index
, void *prefsrc
,
337 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
339 struct interface
*ifp
= NULL
;
340 struct nexthop nh
= {0};
341 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
344 vrf_id_t nh_vrf_id
= vrf_id
;
345 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
347 if (bh_type
== BLACKHOLE_UNSPEC
) {
349 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
350 else if (index
&& gate
)
351 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
352 : NEXTHOP_TYPE_IPV6_IFINDEX
;
353 else if (!index
&& gate
)
354 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
357 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
358 nh
.bh_type
= bh_type
;
361 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
362 nh
.bh_type
= bh_type
;
366 memcpy(&nh
.src
, prefsrc
, sz
);
368 memcpy(&nh
.gate
, gate
, sz
);
371 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
373 nh_vrf_id
= ifp
->vrf_id
;
375 nh
.vrf_id
= nh_vrf_id
;
377 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
378 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
379 == LWTUNNEL_ENCAP_MPLS
) {
380 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
383 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
384 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
387 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
392 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
393 struct nexthop_group
*ng
,
395 struct rtnexthop
*rtnh
,
397 void *prefsrc
, vrf_id_t vrf_id
)
400 struct interface
*ifp
= NULL
;
403 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
405 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
407 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
408 vrf_id_t nh_vrf_id
= vrf_id
;
411 struct nexthop
*nh
= NULL
;
413 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
416 index
= rtnh
->rtnh_ifindex
;
419 * Yes we are looking this up
420 * for every nexthop and just
421 * using the last one looked
424 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
427 nh_vrf_id
= ifp
->vrf_id
;
430 EC_ZEBRA_UNKNOWN_INTERFACE
,
431 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
432 __PRETTY_FUNCTION__
, index
);
433 nh_vrf_id
= VRF_DEFAULT
;
438 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
439 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
441 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
442 rtnh
->rtnh_len
- sizeof(*rtnh
));
443 if (rtnh_tb
[RTA_GATEWAY
])
444 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
445 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
446 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
447 == LWTUNNEL_ENCAP_MPLS
) {
448 num_labels
= parse_encap_mpls(
449 rtnh_tb
[RTA_ENCAP
], labels
);
453 if (gate
&& rtm
->rtm_family
== AF_INET
) {
455 nh
= nexthop_from_ipv4_ifindex(
456 gate
, prefsrc
, index
, nh_vrf_id
);
458 nh
= nexthop_from_ipv4(gate
, prefsrc
,
460 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
462 nh
= nexthop_from_ipv6_ifindex(
463 gate
, index
, nh_vrf_id
);
465 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
467 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
470 nh
->weight
= rtnh
->rtnh_hops
+ 1;
473 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
476 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
477 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
479 /* Add to temporary list */
480 nexthop_group_add_sorted(ng
, nh
);
483 if (rtnh
->rtnh_len
== 0)
486 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
487 rtnh
= RTNH_NEXT(rtnh
);
490 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
495 /* Looking up routing table by netlink interface. */
496 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
501 struct rtattr
*tb
[RTA_MAX
+ 1];
504 struct prefix_ipv6 src_p
= {};
507 char anyaddr
[16] = {0};
509 int proto
= ZEBRA_ROUTE_KERNEL
;
514 uint8_t distance
= 0;
520 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
521 void *src
= NULL
; /* IPv6 srcdest source prefix */
522 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
526 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
528 switch (rtm
->rtm_type
) {
532 bh_type
= BLACKHOLE_NULL
;
534 case RTN_UNREACHABLE
:
535 bh_type
= BLACKHOLE_REJECT
;
538 bh_type
= BLACKHOLE_ADMINPROHIB
;
541 if (IS_ZEBRA_DEBUG_KERNEL
)
542 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
543 nl_rttype_to_str(rtm
->rtm_type
),
548 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
550 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
551 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
552 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
556 memset(tb
, 0, sizeof tb
);
557 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
559 if (rtm
->rtm_flags
& RTM_F_CLONED
)
561 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
563 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
566 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
567 && h
->nlmsg_type
== RTM_NEWROUTE
) {
568 if (IS_ZEBRA_DEBUG_KERNEL
)
569 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
574 /* We don't care about change notifications for the MPLS table. */
575 /* TODO: Revisit this. */
576 if (rtm
->rtm_family
== AF_MPLS
)
579 /* Table corresponding to route. */
581 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
583 table
= rtm
->rtm_table
;
586 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
587 if (vrf_id
== VRF_DEFAULT
) {
588 if (!is_zebra_valid_kernel_table(table
)
589 && !is_zebra_main_routing_table(table
))
593 /* Route which inserted by Zebra. */
594 if (is_selfroute(rtm
->rtm_protocol
)) {
595 flags
|= ZEBRA_FLAG_SELFROUTE
;
596 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
599 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
602 dest
= RTA_DATA(tb
[RTA_DST
]);
607 src
= RTA_DATA(tb
[RTA_SRC
]);
612 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
615 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
618 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
620 if (tb
[RTA_PRIORITY
])
621 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
623 #if defined(SUPPORT_REALMS)
625 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
628 if (tb
[RTA_METRICS
]) {
629 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
631 memset(mxrta
, 0, sizeof mxrta
);
632 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
633 RTA_PAYLOAD(tb
[RTA_METRICS
]));
636 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
639 if (rtm
->rtm_family
== AF_INET
) {
641 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
643 "Invalid destination prefix length: %u received from kernel route change",
647 memcpy(&p
.u
.prefix4
, dest
, 4);
648 p
.prefixlen
= rtm
->rtm_dst_len
;
650 if (rtm
->rtm_src_len
!= 0) {
651 char buf
[PREFIX_STRLEN
];
653 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
654 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
655 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
659 /* Force debug below to not display anything for source */
661 } else if (rtm
->rtm_family
== AF_INET6
) {
663 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
665 "Invalid destination prefix length: %u received from kernel route change",
669 memcpy(&p
.u
.prefix6
, dest
, 16);
670 p
.prefixlen
= rtm
->rtm_dst_len
;
672 src_p
.family
= AF_INET6
;
673 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
675 "Invalid source prefix length: %u received from kernel route change",
679 memcpy(&src_p
.prefix
, src
, 16);
680 src_p
.prefixlen
= rtm
->rtm_src_len
;
684 * For ZEBRA_ROUTE_KERNEL types:
686 * The metric/priority of the route received from the kernel
687 * is a 32 bit number. We are going to interpret the high
688 * order byte as the Admin Distance and the low order 3 bytes
691 * This will allow us to do two things:
692 * 1) Allow the creation of kernel routes that can be
693 * overridden by zebra.
694 * 2) Allow the old behavior for 'most' kernel route types
695 * if a user enters 'ip route ...' v4 routes get a metric
696 * of 0 and v6 routes get a metric of 1024. Both of these
697 * values will end up with a admin distance of 0, which
698 * will cause them to win for the purposes of zebra.
700 if (proto
== ZEBRA_ROUTE_KERNEL
) {
701 distance
= (metric
>> 24) & 0xFF;
702 metric
= (metric
& 0x00FFFFFF);
705 if (IS_ZEBRA_DEBUG_KERNEL
) {
706 char buf
[PREFIX_STRLEN
];
707 char buf2
[PREFIX_STRLEN
];
708 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
709 nl_msg_type_to_str(h
->nlmsg_type
),
710 prefix2str(&p
, buf
, sizeof(buf
)),
711 src_p
.prefixlen
? " from " : "",
713 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
715 vrf_id
, table
, metric
, distance
);
719 if (rtm
->rtm_family
== AF_INET6
)
722 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
724 if (!tb
[RTA_MULTIPATH
]) {
725 struct nexthop nh
= {0};
728 nh
= parse_nexthop_unicast(
729 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
732 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
733 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
736 /* This is a multipath route */
737 struct route_entry
*re
;
738 struct nexthop_group
*ng
= NULL
;
739 struct rtnexthop
*rtnh
=
740 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
742 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
744 re
->distance
= distance
;
750 re
->uptime
= monotime(NULL
);
757 /* Use temporary list of nexthops; parse
758 * message payload's nexthops.
760 ng
= nexthop_group_new();
762 parse_multipath_nexthops_unicast(
763 ns_id
, ng
, rtm
, rtnh
, tb
,
766 zserv_nexthop_num_warn(
767 __func__
, (const struct prefix
*)&p
,
771 nexthop_group_delete(&ng
);
777 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
784 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
785 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
788 if (!tb
[RTA_MULTIPATH
]) {
791 nh
= parse_nexthop_unicast(
792 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
794 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
795 flags
, &p
, &src_p
, &nh
, 0, table
,
796 metric
, distance
, true);
798 /* XXX: need to compare the entire list of
799 * nexthops here for NLM_F_APPEND stupidity */
800 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
801 flags
, &p
, &src_p
, NULL
, 0, table
,
802 metric
, distance
, true);
810 static struct mcast_route_data
*mroute
= NULL
;
812 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
813 ns_id_t ns_id
, int startup
)
817 struct rtattr
*tb
[RTA_MAX
+ 1];
818 struct mcast_route_data
*m
;
819 struct mcast_route_data mr
;
826 char oif_list
[256] = "\0";
833 memset(&mr
, 0, sizeof(mr
));
839 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
841 memset(tb
, 0, sizeof tb
);
842 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
845 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
847 table
= rtm
->rtm_table
;
849 vrf
= vrf_lookup_by_table(table
, ns_id
);
852 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
855 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
858 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
861 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
863 if (tb
[RTA_MULTIPATH
]) {
864 struct rtnexthop
*rtnh
=
865 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
867 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
869 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
872 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
875 if (rtnh
->rtnh_len
== 0)
878 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
879 rtnh
= RTNH_NEXT(rtnh
);
883 if (IS_ZEBRA_DEBUG_KERNEL
) {
884 struct interface
*ifp
= NULL
;
885 struct zebra_vrf
*zvrf
= NULL
;
887 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
888 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
889 for (count
= 0; count
< oif_count
; count
++) {
890 ifp
= if_lookup_by_index(oif
[count
], vrf
);
893 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
895 strlcat(oif_list
, temp
, sizeof(oif_list
));
897 zvrf
= zebra_vrf_lookup_by_id(vrf
);
898 ifp
= if_lookup_by_index(iif
, vrf
);
900 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
901 (zvrf
? zvrf
->vrf
->name
: "Unknown"), vrf
,
902 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
903 ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
909 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
916 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
917 /* If this is not route add/delete message print warning. */
918 zlog_debug("Kernel message: %s NS %u",
919 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
923 if (!(rtm
->rtm_family
== AF_INET
||
924 rtm
->rtm_family
== AF_INET6
||
925 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
927 EC_ZEBRA_UNKNOWN_FAMILY
,
928 "Invalid address family: %u received from kernel route change: %s",
929 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
933 /* Connected route. */
934 if (IS_ZEBRA_DEBUG_KERNEL
)
935 zlog_debug("%s %s %s proto %s NS %u",
936 nl_msg_type_to_str(h
->nlmsg_type
),
937 nl_family_to_str(rtm
->rtm_family
),
938 nl_rttype_to_str(rtm
->rtm_type
),
939 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
942 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
944 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
947 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
951 if (rtm
->rtm_type
== RTN_MULTICAST
)
952 netlink_route_change_read_multicast(h
, ns_id
, startup
);
954 netlink_route_change_read_unicast(h
, ns_id
, startup
);
958 /* Request for specific route information from the kernel */
959 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
966 /* Form the request, specifying filter (rtattr) if needed. */
967 memset(&req
, 0, sizeof(req
));
968 req
.n
.nlmsg_type
= type
;
969 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
970 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
971 req
.rtm
.rtm_family
= family
;
973 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
976 /* Routing table read function using netlink interface. Only called
978 int netlink_route_read(struct zebra_ns
*zns
)
981 struct zebra_dplane_info dp_info
;
983 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
985 /* Get IPv4 routing table. */
986 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
989 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
990 &zns
->netlink_cmd
, &dp_info
, 0, 1);
994 /* Get IPv6 routing table. */
995 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
998 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
999 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1006 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
1008 struct nlmsghdr
*nlmsg
,
1009 size_t req_size
, int bytelen
,
1010 const struct nexthop
*nexthop
)
1012 if (route_family
== AF_MPLS
) {
1013 struct gw_family_t gw_fam
;
1015 gw_fam
.family
= gw_family
;
1016 if (gw_family
== AF_INET
)
1017 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1019 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1020 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1023 if (gw_family
== AF_INET
)
1024 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1025 &nexthop
->gate
.ipv4
, bytelen
);
1027 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
1028 &nexthop
->gate
.ipv6
, bytelen
);
1032 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
1035 struct rtnexthop
*rtnh
,
1036 size_t req_size
, int bytelen
,
1037 const struct nexthop
*nexthop
)
1039 if (route_family
== AF_MPLS
) {
1040 struct gw_family_t gw_fam
;
1042 gw_fam
.family
= gw_family
;
1043 if (gw_family
== AF_INET
)
1044 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1046 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1047 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
1049 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
1051 if (gw_family
== AF_INET
)
1052 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1053 &nexthop
->gate
.ipv4
, bytelen
);
1055 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1056 &nexthop
->gate
.ipv6
, bytelen
);
1057 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1061 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1062 mpls_lse_t
*out_lse
, char *label_buf
,
1063 size_t label_buf_size
)
1065 char label_buf1
[20];
1068 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1069 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1072 if (IS_ZEBRA_DEBUG_KERNEL
) {
1074 sprintf(label_buf
, "label %u",
1075 nh_label
->label
[i
]);
1077 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1078 strlcat(label_buf
, label_buf1
, label_buf_size
);
1082 out_lse
[num_labels
] =
1083 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1090 /* This function takes a nexthop as argument and adds
1091 * the appropriate netlink attributes to an existing
1094 * @param routedesc: Human readable description of route type
1095 * (direct/recursive, single-/multipath)
1096 * @param bytelen: Length of addresses in bytes.
1097 * @param nexthop: Nexthop information
1098 * @param nlmsg: nlmsghdr structure to fill in.
1099 * @param req_size: The size allocated for the message.
1101 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
1102 const struct nexthop
*nexthop
,
1103 struct nlmsghdr
*nlmsg
,
1104 struct rtmsg
*rtmsg
,
1105 size_t req_size
, int cmd
)
1108 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1109 char label_buf
[256];
1115 * label_buf is *only* currently used within debugging.
1116 * As such when we assign it we are guarding it inside
1117 * a debug test. If you want to change this make sure
1118 * you fix this assumption
1120 label_buf
[0] = '\0';
1122 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1126 /* Set the BoS bit */
1127 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1129 if (rtmsg
->rtm_family
== AF_MPLS
)
1130 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1131 num_labels
* sizeof(mpls_lse_t
));
1133 struct rtattr
*nest
;
1134 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1136 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1138 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1139 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1140 num_labels
* sizeof(mpls_lse_t
));
1141 addattr_nest_end(nlmsg
, nest
);
1145 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1146 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1148 if (rtmsg
->rtm_family
== AF_INET
1149 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1150 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1151 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1152 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1153 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1155 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1156 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1157 &nexthop
->rmap_src
.ipv4
, bytelen
);
1158 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1159 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1160 &nexthop
->src
.ipv4
, bytelen
);
1162 if (IS_ZEBRA_DEBUG_KERNEL
)
1164 " 5549: _netlink_route_build_singlepath() (%s): "
1165 "nexthop via %s %s if %u(%u)",
1166 routedesc
, ipv4_ll_buf
, label_buf
,
1167 nexthop
->ifindex
, nexthop
->vrf_id
);
1171 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1172 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1173 /* Send deletes to the kernel without specifying the next-hop */
1174 if (cmd
!= RTM_DELROUTE
)
1175 _netlink_route_nl_add_gateway_info(
1176 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1179 if (cmd
== RTM_NEWROUTE
) {
1180 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1181 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1182 &nexthop
->rmap_src
.ipv4
, bytelen
);
1183 else if (nexthop
->src
.ipv4
.s_addr
)
1184 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1185 &nexthop
->src
.ipv4
, bytelen
);
1188 if (IS_ZEBRA_DEBUG_KERNEL
)
1190 "netlink_route_multipath() (%s): "
1191 "nexthop via %s %s if %u(%u)",
1192 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1193 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1196 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1197 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1198 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1199 nlmsg
, req_size
, bytelen
,
1202 if (cmd
== RTM_NEWROUTE
) {
1203 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1204 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1205 &nexthop
->rmap_src
.ipv6
, bytelen
);
1206 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1207 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1208 &nexthop
->src
.ipv6
, bytelen
);
1211 if (IS_ZEBRA_DEBUG_KERNEL
)
1213 "netlink_route_multipath() (%s): "
1214 "nexthop via %s %s if %u(%u)",
1215 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1216 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1220 * We have the ifindex so we should always send it
1221 * This is especially useful if we are doing route
1224 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1225 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1227 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1228 if (cmd
== RTM_NEWROUTE
) {
1229 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1230 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1231 &nexthop
->rmap_src
.ipv4
, bytelen
);
1232 else if (nexthop
->src
.ipv4
.s_addr
)
1233 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1234 &nexthop
->src
.ipv4
, bytelen
);
1237 if (IS_ZEBRA_DEBUG_KERNEL
)
1239 "netlink_route_multipath() (%s): "
1240 "nexthop via if %u(%u)",
1241 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1245 /* This function takes a nexthop as argument and
1246 * appends to the given rtattr/rtnexthop pair the
1247 * representation of the nexthop. If the nexthop
1248 * defines a preferred source, the src parameter
1249 * will be modified to point to that src, otherwise
1250 * it will be kept unmodified.
1252 * @param routedesc: Human readable description of route type
1253 * (direct/recursive, single-/multipath)
1254 * @param bytelen: Length of addresses in bytes.
1255 * @param nexthop: Nexthop information
1256 * @param rta: rtnetlink attribute structure
1257 * @param rtnh: pointer to an rtnetlink nexthop structure
1258 * @param src: pointer pointing to a location where
1259 * the prefsrc should be stored.
1261 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1262 const struct nexthop
*nexthop
,
1264 struct rtnexthop
*rtnh
,
1265 struct rtmsg
*rtmsg
,
1266 const union g_addr
**src
)
1268 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1269 char label_buf
[256];
1272 rtnh
->rtnh_len
= sizeof(*rtnh
);
1273 rtnh
->rtnh_flags
= 0;
1274 rtnh
->rtnh_hops
= 0;
1275 rta
->rta_len
+= rtnh
->rtnh_len
;
1280 * label_buf is *only* currently used within debugging.
1281 * As such when we assign it we are guarding it inside
1282 * a debug test. If you want to change this make sure
1283 * you fix this assumption
1285 label_buf
[0] = '\0';
1287 num_labels
= build_label_stack(nexthop
->nh_label
, out_lse
, label_buf
,
1291 /* Set the BoS bit */
1292 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1294 if (rtmsg
->rtm_family
== AF_MPLS
) {
1295 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1297 num_labels
* sizeof(mpls_lse_t
));
1299 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1301 struct rtattr
*nest
;
1302 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1303 int len
= rta
->rta_len
;
1305 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1306 &encap
, sizeof(uint16_t));
1307 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1308 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1310 num_labels
* sizeof(mpls_lse_t
));
1311 rta_nest_end(rta
, nest
);
1312 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1316 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1317 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1319 if (rtmsg
->rtm_family
== AF_INET
1320 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1321 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1323 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1324 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1326 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1327 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1329 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1330 *src
= &nexthop
->rmap_src
;
1331 else if (nexthop
->src
.ipv4
.s_addr
)
1332 *src
= &nexthop
->src
;
1334 if (IS_ZEBRA_DEBUG_KERNEL
)
1336 " 5549: netlink_route_build_multipath() (%s): "
1337 "nexthop via %s %s if %u",
1338 routedesc
, ipv4_ll_buf
, label_buf
,
1343 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1344 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1345 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1346 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1348 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1349 *src
= &nexthop
->rmap_src
;
1350 else if (nexthop
->src
.ipv4
.s_addr
)
1351 *src
= &nexthop
->src
;
1353 if (IS_ZEBRA_DEBUG_KERNEL
)
1355 "netlink_route_multipath() (%s): "
1356 "nexthop via %s %s if %u",
1357 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1358 label_buf
, nexthop
->ifindex
);
1360 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1361 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1362 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1363 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1366 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1367 *src
= &nexthop
->rmap_src
;
1368 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1369 *src
= &nexthop
->src
;
1371 if (IS_ZEBRA_DEBUG_KERNEL
)
1373 "netlink_route_multipath() (%s): "
1374 "nexthop via %s %s if %u",
1375 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1376 label_buf
, nexthop
->ifindex
);
1380 * We have figured out the ifindex so we should always send it
1381 * This is especially useful if we are doing route
1384 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1385 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1388 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1389 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1390 *src
= &nexthop
->rmap_src
;
1391 else if (nexthop
->src
.ipv4
.s_addr
)
1392 *src
= &nexthop
->src
;
1394 if (IS_ZEBRA_DEBUG_KERNEL
)
1396 "netlink_route_multipath() (%s): "
1397 "nexthop via if %u",
1398 routedesc
, nexthop
->ifindex
);
1401 if (nexthop
->weight
)
1402 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1405 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1406 const zebra_nhlfe_t
*nhlfe
,
1407 struct nlmsghdr
*nlmsg
,
1408 struct rtmsg
*rtmsg
,
1409 size_t req_size
, int cmd
)
1414 family
= NHLFE_FAMILY(nhlfe
);
1415 bytelen
= (family
== AF_INET
? 4 : 16);
1416 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1417 nlmsg
, rtmsg
, req_size
, cmd
);
1422 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1423 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1424 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1429 family
= NHLFE_FAMILY(nhlfe
);
1430 bytelen
= (family
== AF_INET
? 4 : 16);
1431 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1436 /* Log debug information for netlink_route_multipath
1437 * if debug logging is enabled.
1439 * @param cmd: Netlink command which is to be processed
1440 * @param p: Prefix for which the change is due
1441 * @param family: Address family which the change concerns
1442 * @param zvrf: The vrf we are in
1443 * @param tableid: The table we are working on
1445 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1446 int family
, vrf_id_t vrfid
,
1449 if (IS_ZEBRA_DEBUG_KERNEL
) {
1450 char buf
[PREFIX_STRLEN
];
1452 "netlink_route_multipath(): %s %s vrf %u(%u)",
1453 nl_msg_type_to_str(cmd
),
1454 prefix2str(p
, buf
, sizeof(buf
)),
1459 static void _netlink_nexthop_debug(int cmd
, uint32_t id
)
1461 if (IS_ZEBRA_DEBUG_KERNEL
)
1462 zlog_debug("netlink_nexthop(): %s, id=%u",
1463 nl_msg_type_to_str(cmd
), id
);
1466 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1468 if (IS_ZEBRA_DEBUG_KERNEL
)
1469 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1470 nl_msg_type_to_str(cmd
), label
);
1473 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1474 int llalen
, ns_id_t ns_id
)
1476 uint8_t protocol
= RTPROT_ZEBRA
;
1483 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1485 memset(&req
, 0, sizeof(req
));
1487 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1488 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1489 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1490 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1492 req
.ndm
.ndm_family
= AF_INET
;
1493 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1494 req
.ndm
.ndm_ifindex
= ifindex
;
1495 req
.ndm
.ndm_type
= RTN_UNICAST
;
1497 addattr_l(&req
.n
, sizeof(req
),
1498 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
1499 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1500 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1502 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1507 * Routing table change via netlink interface, using a dataplane context object
1509 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1512 struct nexthop
*nexthop
= NULL
;
1513 unsigned int nexthop_num
;
1515 const char *routedesc
;
1518 const struct prefix
*p
, *src_p
;
1524 char buf
[NL_PKT_BUF_SIZE
];
1527 p
= dplane_ctx_get_dest(ctx
);
1528 src_p
= dplane_ctx_get_src(ctx
);
1530 family
= PREFIX_FAMILY(p
);
1532 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1534 bytelen
= (family
== AF_INET
? 4 : 16);
1536 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1537 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1539 if ((cmd
== RTM_NEWROUTE
) &&
1540 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1541 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1543 req
.n
.nlmsg_type
= cmd
;
1545 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1547 req
.r
.rtm_family
= family
;
1548 req
.r
.rtm_dst_len
= p
->prefixlen
;
1549 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1550 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1552 if (cmd
== RTM_DELROUTE
)
1553 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1555 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1558 * blackhole routes are not RTN_UNICAST, they are
1559 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1560 * so setting this value as a RTN_UNICAST would
1561 * cause the route lookup of just the prefix
1562 * to fail. So no need to specify this for
1563 * the RTM_DELROUTE case
1565 if (cmd
!= RTM_DELROUTE
)
1566 req
.r
.rtm_type
= RTN_UNICAST
;
1568 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1570 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1574 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1576 * either by the kernel or by zebra. Its purely for calculating best
1578 * by the routing protocol and for communicating with protocol peers.
1580 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1582 #if defined(SUPPORT_REALMS)
1586 if (cmd
== RTM_DELROUTE
)
1587 tag
= dplane_ctx_get_old_tag(ctx
);
1589 tag
= dplane_ctx_get_tag(ctx
);
1591 if (tag
> 0 && tag
<= 255)
1592 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1595 /* Table corresponding to this route. */
1596 table_id
= dplane_ctx_get_table(ctx
);
1598 req
.r
.rtm_table
= table_id
;
1600 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1601 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1604 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1607 * If we are not updating the route and we have received
1608 * a route delete, then all we need to fill in is the
1609 * prefix information to tell the kernel to schwack
1612 if (cmd
== RTM_DELROUTE
)
1615 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1616 char buf
[NL_PKT_BUF_SIZE
];
1617 struct rtattr
*rta
= (void *)buf
;
1618 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1619 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1621 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1623 rta
->rta_type
= RTA_METRICS
;
1624 rta
->rta_len
= RTA_LENGTH(0);
1625 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1626 RTAX_MTU
, &mtu
, sizeof(mtu
));
1627 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1632 /* Kernel supports nexthop objects */
1633 addattr32(&req
.n
, sizeof(req
), RTA_NH_ID
,
1634 dplane_ctx_get_nhe_id(ctx
));
1638 /* Count overall nexthops so we can decide whether to use singlepath
1639 * or multipath case.
1642 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1643 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1645 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1651 /* Singlepath case. */
1652 if (nexthop_num
== 1) {
1654 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1656 * So we want to cover 2 types of blackhole
1658 * 1) A normal blackhole route( ala from a static
1660 * 2) A recursively resolved blackhole route
1662 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1663 switch (nexthop
->bh_type
) {
1664 case BLACKHOLE_ADMINPROHIB
:
1665 req
.r
.rtm_type
= RTN_PROHIBIT
;
1667 case BLACKHOLE_REJECT
:
1668 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1671 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1676 if (CHECK_FLAG(nexthop
->flags
,
1677 NEXTHOP_FLAG_RECURSIVE
)) {
1682 if (family
== AF_INET
) {
1683 if (nexthop
->rmap_src
.ipv4
.s_addr
1686 nexthop
->rmap_src
.ipv4
;
1688 } else if (nexthop
->src
.ipv4
.s_addr
1694 } else if (family
== AF_INET6
) {
1695 if (!IN6_IS_ADDR_UNSPECIFIED(
1696 &nexthop
->rmap_src
.ipv6
)) {
1698 nexthop
->rmap_src
.ipv6
;
1701 !IN6_IS_ADDR_UNSPECIFIED(
1702 &nexthop
->src
.ipv6
)) {
1711 if ((cmd
== RTM_NEWROUTE
1712 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1713 routedesc
= nexthop
->rparent
1714 ? "recursive, single-path"
1717 _netlink_route_build_singlepath(
1718 routedesc
, bytelen
, nexthop
, &req
.n
,
1719 &req
.r
, sizeof(req
), cmd
);
1724 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1725 if (family
== AF_INET
)
1726 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1727 &src
.ipv4
, bytelen
);
1728 else if (family
== AF_INET6
)
1729 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1730 &src
.ipv6
, bytelen
);
1732 } else { /* Multipath case */
1733 char buf
[NL_PKT_BUF_SIZE
];
1734 struct rtattr
*rta
= (void *)buf
;
1735 struct rtnexthop
*rtnh
;
1736 const union g_addr
*src1
= NULL
;
1738 rta
->rta_type
= RTA_MULTIPATH
;
1739 rta
->rta_len
= RTA_LENGTH(0);
1740 rtnh
= RTA_DATA(rta
);
1743 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1744 if (CHECK_FLAG(nexthop
->flags
,
1745 NEXTHOP_FLAG_RECURSIVE
)) {
1746 /* This only works for IPv4 now */
1750 if (family
== AF_INET
) {
1751 if (nexthop
->rmap_src
.ipv4
.s_addr
1754 nexthop
->rmap_src
.ipv4
;
1756 } else if (nexthop
->src
.ipv4
.s_addr
1762 } else if (family
== AF_INET6
) {
1763 if (!IN6_IS_ADDR_UNSPECIFIED(
1764 &nexthop
->rmap_src
.ipv6
)) {
1766 nexthop
->rmap_src
.ipv6
;
1769 !IN6_IS_ADDR_UNSPECIFIED(
1770 &nexthop
->src
.ipv6
)) {
1780 if ((cmd
== RTM_NEWROUTE
1781 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1782 routedesc
= nexthop
->rparent
1783 ? "recursive, multipath"
1787 _netlink_route_build_multipath(
1788 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1790 rtnh
= RTNH_NEXT(rtnh
);
1792 if (!setsrc
&& src1
) {
1793 if (family
== AF_INET
)
1794 src
.ipv4
= src1
->ipv4
;
1795 else if (family
== AF_INET6
)
1796 src
.ipv6
= src1
->ipv6
;
1802 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1803 if (family
== AF_INET
)
1804 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1805 &src
.ipv4
, bytelen
);
1806 else if (family
== AF_INET6
)
1807 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1808 &src
.ipv6
, bytelen
);
1809 if (IS_ZEBRA_DEBUG_KERNEL
)
1810 zlog_debug("Setting source");
1813 if (rta
->rta_len
> RTA_LENGTH(0))
1814 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1815 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1818 /* If there is no useful nexthop then return. */
1819 if (nexthop_num
== 0) {
1820 if (IS_ZEBRA_DEBUG_KERNEL
)
1822 "netlink_route_multipath(): No useful nexthop.");
1827 /* Talk to netlink socket. */
1828 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1829 dplane_ctx_get_ns(ctx
), 0);
1832 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1834 uint32_t actual_table
;
1836 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1844 struct zebra_ns
*zns
;
1847 memset(&req
, 0, sizeof(req
));
1849 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1850 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1851 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1853 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1854 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1856 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1857 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1858 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1859 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1863 * So during the namespace cleanup we started storing
1864 * the zvrf table_id for the default table as RT_TABLE_MAIN
1865 * which is what the normal routing table for ip routing is.
1866 * This change caused this to break our lookups of sg data
1867 * because prior to this change the zvrf->table_id was 0
1868 * and when the pim multicast kernel code saw a 0,
1869 * it was auto-translated to RT_TABLE_DEFAULT. But since
1870 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1871 * and the kernel goes screw you and the delicious cookies you
1872 * are trying to give me. So now we have this little hack.
1874 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1876 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1878 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1879 &zns
->netlink_cmd
, zns
, 0);
1885 /* Char length to debug ID with */
1886 #define ID_LENGTH 10
1888 static void _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
1890 const struct nh_grp
*z_grp
,
1891 const uint8_t count
)
1893 struct nexthop_grp grp
[count
];
1894 /* Need space for max group size, "/", and null term */
1895 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
1896 char buf1
[ID_LENGTH
+ 2];
1900 memset(grp
, 0, sizeof(grp
));
1903 for (int i
= 0; i
< count
; i
++) {
1904 grp
[i
].id
= z_grp
[i
].id
;
1905 grp
[i
].weight
= z_grp
[i
].weight
- 1;
1907 if (IS_ZEBRA_DEBUG_KERNEL
) {
1909 snprintf(buf
, sizeof(buf1
), "group %u",
1912 snprintf(buf1
, sizeof(buf1
), "/%u",
1914 strlcat(buf
, buf1
, sizeof(buf
));
1918 addattr_l(n
, req_size
, NHA_GROUP
, grp
, count
* sizeof(*grp
));
1921 if (IS_ZEBRA_DEBUG_KERNEL
)
1922 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
1926 * netlink_nexthop() - Nexthop change via the netlink interface
1928 * @ctx: Dataplane ctx
1930 * Return: Result status
1932 static int netlink_nexthop(int cmd
, struct zebra_dplane_ctx
*ctx
)
1937 char buf
[NL_PKT_BUF_SIZE
];
1940 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1941 char label_buf
[256];
1943 size_t req_size
= sizeof(req
);
1945 /* Nothing to do if the kernel doesn't support nexthop objects */
1949 label_buf
[0] = '\0';
1951 memset(&req
, 0, req_size
);
1953 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
1954 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1956 if (cmd
== RTM_NEWNEXTHOP
)
1957 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1959 req
.n
.nlmsg_type
= cmd
;
1960 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1962 req
.nhm
.nh_family
= AF_UNSPEC
;
1965 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
1969 EC_ZEBRA_NHG_FIB_UPDATE
,
1970 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1974 addattr32(&req
.n
, req_size
, NHA_ID
, id
);
1976 if (cmd
== RTM_NEWNEXTHOP
) {
1977 if (dplane_ctx_get_nhe_nh_grp_count(ctx
))
1978 _netlink_nexthop_build_group(
1979 &req
.n
, req_size
, id
,
1980 dplane_ctx_get_nhe_nh_grp(ctx
),
1981 dplane_ctx_get_nhe_nh_grp_count(ctx
));
1983 const struct nexthop
*nh
=
1984 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
1985 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
1988 req
.nhm
.nh_family
= AF_INET
;
1989 else if (afi
== AFI_IP6
)
1990 req
.nhm
.nh_family
= AF_INET6
;
1993 case NEXTHOP_TYPE_IPV4
:
1994 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1995 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
1996 &nh
->gate
.ipv4
, IPV4_MAX_BYTELEN
);
1998 case NEXTHOP_TYPE_IPV6
:
1999 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2000 addattr_l(&req
.n
, req_size
, NHA_GATEWAY
,
2001 &nh
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
2003 case NEXTHOP_TYPE_BLACKHOLE
:
2004 addattr_l(&req
.n
, req_size
, NHA_BLACKHOLE
, NULL
,
2006 /* Blackhole shouldn't have anymore attributes
2009 case NEXTHOP_TYPE_IFINDEX
:
2010 /* Don't need anymore info for this */
2016 EC_ZEBRA_NHG_FIB_UPDATE
,
2017 "Context received for kernel nexthop update without an interface");
2021 addattr32(&req
.n
, req_size
, NHA_OIF
, nh
->ifindex
);
2023 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2024 req
.nhm
.nh_flags
|= RTNH_F_ONLINK
;
2027 build_label_stack(nh
->nh_label
, out_lse
,
2028 label_buf
, sizeof(label_buf
));
2031 /* Set the BoS bit */
2032 out_lse
[num_labels
- 1] |=
2033 htonl(1 << MPLS_LS_S_SHIFT
);
2036 * TODO: MPLS unsupported for now in kernel.
2038 if (req
.nhm
.nh_family
== AF_MPLS
)
2041 addattr_l(&req
.n
, req_size
, NHA_NEWDST
,
2044 * sizeof(mpls_lse_t
));
2047 struct rtattr
*nest
;
2048 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
2050 addattr_l(&req
.n
, req_size
,
2051 NHA_ENCAP_TYPE
, &encap
,
2053 nest
= addattr_nest(&req
.n
, req_size
,
2055 addattr_l(&req
.n
, req_size
,
2056 MPLS_IPTUNNEL_DST
, &out_lse
,
2058 * sizeof(mpls_lse_t
));
2059 addattr_nest_end(&req
.n
, nest
);
2064 if (IS_ZEBRA_DEBUG_KERNEL
) {
2065 char buf
[NEXTHOP_STRLEN
];
2067 snprintfrr(buf
, sizeof(buf
), "%pNHv", nh
);
2068 zlog_debug("%s: ID (%u): %s (%u) %s ", __func__
,
2069 id
, buf
, nh
->vrf_id
, label_buf
);
2073 req
.nhm
.nh_protocol
= zebra2proto(dplane_ctx_get_nhe_type(ctx
));
2075 } else if (cmd
!= RTM_DELNEXTHOP
) {
2077 EC_ZEBRA_NHG_FIB_UPDATE
,
2078 "Nexthop group kernel update command (%d) does not exist",
2083 _netlink_nexthop_debug(cmd
, id
);
2085 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2086 dplane_ctx_get_ns(ctx
), 0);
2090 * kernel_nexthop_update() - Update/delete a nexthop from the kernel
2092 * @ctx: Dataplane context
2094 * Return: Dataplane result flag
2096 enum zebra_dplane_result
kernel_nexthop_update(struct zebra_dplane_ctx
*ctx
)
2101 switch (dplane_ctx_get_op(ctx
)) {
2102 case DPLANE_OP_NH_DELETE
:
2103 cmd
= RTM_DELNEXTHOP
;
2105 case DPLANE_OP_NH_INSTALL
:
2106 case DPLANE_OP_NH_UPDATE
:
2107 cmd
= RTM_NEWNEXTHOP
;
2109 case DPLANE_OP_ROUTE_INSTALL
:
2110 case DPLANE_OP_ROUTE_UPDATE
:
2111 case DPLANE_OP_ROUTE_DELETE
:
2112 case DPLANE_OP_ROUTE_NOTIFY
:
2113 case DPLANE_OP_LSP_INSTALL
:
2114 case DPLANE_OP_LSP_UPDATE
:
2115 case DPLANE_OP_LSP_DELETE
:
2116 case DPLANE_OP_LSP_NOTIFY
:
2117 case DPLANE_OP_PW_INSTALL
:
2118 case DPLANE_OP_PW_UNINSTALL
:
2119 case DPLANE_OP_SYS_ROUTE_ADD
:
2120 case DPLANE_OP_SYS_ROUTE_DELETE
:
2121 case DPLANE_OP_ADDR_INSTALL
:
2122 case DPLANE_OP_ADDR_UNINSTALL
:
2123 case DPLANE_OP_MAC_INSTALL
:
2124 case DPLANE_OP_MAC_DELETE
:
2125 case DPLANE_OP_NEIGH_INSTALL
:
2126 case DPLANE_OP_NEIGH_UPDATE
:
2127 case DPLANE_OP_NEIGH_DELETE
:
2128 case DPLANE_OP_VTEP_ADD
:
2129 case DPLANE_OP_VTEP_DELETE
:
2130 case DPLANE_OP_NONE
:
2132 EC_ZEBRA_NHG_FIB_UPDATE
,
2133 "Context received for kernel nexthop update with incorrect OP code (%u)",
2134 dplane_ctx_get_op(ctx
));
2135 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2138 ret
= netlink_nexthop(cmd
, ctx
);
2140 return (ret
== 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2141 : ZEBRA_DPLANE_REQUEST_FAILURE
);
2145 * Update or delete a prefix from the kernel,
2146 * using info from a dataplane context.
2148 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
2151 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2152 struct nexthop
*nexthop
;
2154 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2156 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2158 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2160 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2161 /* Single 'replace' operation */
2165 * With route replace semantics in place
2166 * for v4 routes and the new route is a system
2167 * route we do not install anything.
2168 * The problem here is that the new system
2169 * route should cause us to withdraw from
2170 * the kernel the old non-system route
2172 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)) &&
2173 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2174 (void)netlink_route_multipath(RTM_DELROUTE
,
2178 * So v6 route replace semantics are not in
2179 * the kernel at this point as I understand it.
2180 * so let's do a delete then an add.
2181 * In the future once v6 route replace semantics
2182 * are in we can figure out what to do here to
2183 * allow working with old and new kernels.
2185 * I'm also intentionally ignoring the failure case
2186 * of the route delete. If that happens yeah we're
2189 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2190 (void)netlink_route_multipath(RTM_DELROUTE
,
2196 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2199 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2200 ret
= netlink_route_multipath(cmd
, ctx
);
2203 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
2204 /* Update installed nexthops to signal which have been
2207 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2208 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2211 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2212 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2218 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2222 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2224 * @tb: Netlink RTA data
2225 * @family: Address family in the nhmsg
2226 * @ifp: Interface connected - this should be NULL, we fill it in
2227 * @ns_id: Namspace id
2229 * Return: New nexthop
2231 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2232 unsigned char family
,
2233 struct interface
**ifp
,
2236 struct nexthop nh
= {};
2238 enum nexthop_types_t type
= 0;
2241 struct interface
*ifp_lookup
;
2243 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2246 if (tb
[NHA_GATEWAY
]) {
2249 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2253 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2258 EC_ZEBRA_BAD_NHG_MESSAGE
,
2259 "Nexthop gateway with bad address family (%d) received from kernel",
2263 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2265 type
= NEXTHOP_TYPE_IFINDEX
;
2271 memcpy(&(nh
.gate
), gate
, sz
);
2274 nh
.ifindex
= if_index
;
2277 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2282 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2285 EC_ZEBRA_UNKNOWN_INTERFACE
,
2286 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2287 __PRETTY_FUNCTION__
, nh
.ifindex
);
2289 nh
.vrf_id
= VRF_DEFAULT
;
2292 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2293 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2296 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2298 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2299 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2302 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2309 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2310 struct nh_grp
*z_grp
, int z_grp_size
)
2313 /* linux/nexthop.h group struct */
2314 struct nexthop_grp
*n_grp
= NULL
;
2316 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2317 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2319 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2320 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2321 "Invalid nexthop group received from the kernel");
2326 // TODO: Need type for something?
2327 zlog_debug("Nexthop group type: %d",
2328 *((uint16_t *)RTA_DATA(tb
[NHA_GROUP_TYPE
])));
2332 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2333 z_grp
[i
].id
= n_grp
[i
].id
;
2334 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2340 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2342 * @h: Netlink message header
2343 * @ns_id: Namspace id
2344 * @startup: Are we reading under startup conditions?
2346 * Return: Result status
2348 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2351 /* nexthop group id */
2353 unsigned char family
;
2355 afi_t afi
= AFI_UNSPEC
;
2356 vrf_id_t vrf_id
= 0;
2357 struct interface
*ifp
= NULL
;
2358 struct nhmsg
*nhm
= NULL
;
2359 struct nexthop nh
= {};
2360 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2361 /* Count of nexthops in group array */
2362 uint8_t grp_count
= 0;
2363 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2365 nhm
= NLMSG_DATA(h
);
2367 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2370 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2373 "%s: Message received from netlink is of a broken size %d %zu",
2374 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2375 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2379 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2384 EC_ZEBRA_BAD_NHG_MESSAGE
,
2385 "Nexthop group without an ID received from the kernel");
2389 /* We use the ID key'd nhg table for kernel updates */
2390 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2392 family
= nhm
->nh_family
;
2393 afi
= family2afi(family
);
2395 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2397 if (IS_ZEBRA_DEBUG_KERNEL
)
2398 zlog_debug("%s ID (%u) %s NS %u",
2399 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2400 nl_family_to_str(family
), ns_id
);
2403 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2404 if (tb
[NHA_GROUP
]) {
2406 * If this is a group message its only going to have
2407 * an array of nexthop IDs associated with it
2409 grp_count
= netlink_nexthop_process_group(
2410 tb
, grp
, array_size(grp
));
2412 if (tb
[NHA_BLACKHOLE
]) {
2414 * This nexthop is just for blackhole-ing
2415 * traffic, it should not have an OIF, GATEWAY,
2418 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2419 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2420 } else if (tb
[NHA_OIF
])
2422 * This is a true new nexthop, so we need
2423 * to parse the gateway and device info
2425 nh
= netlink_nexthop_process_nh(tb
, family
,
2430 EC_ZEBRA_BAD_NHG_MESSAGE
,
2431 "Invalid Nexthop message received from the kernel with ID (%u)",
2435 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2436 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2437 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2441 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2445 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2446 zebra_nhg_kernel_del(id
);
2452 * netlink_request_nexthop() - Request nextop information from the kernel
2453 * @zns: Zebra namespace
2454 * @family: AF_* netlink family
2455 * @type: RTM_* route type
2457 * Return: Result status
2459 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2466 /* Form the request, specifying filter (rtattr) if needed. */
2467 memset(&req
, 0, sizeof(req
));
2468 req
.n
.nlmsg_type
= type
;
2469 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2470 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2471 req
.nhm
.nh_family
= family
;
2473 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2478 * netlink_nexthop_read() - Nexthop read function using netlink interface
2480 * @zns: Zebra name space
2482 * Return: Result status
2483 * Only called at bootstrap time.
2485 int netlink_nexthop_read(struct zebra_ns
*zns
)
2488 struct zebra_dplane_info dp_info
;
2490 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2492 /* Get nexthop objects */
2493 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2496 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2500 /* If we succesfully read in nexthop objects,
2501 * this kernel must support them.
2504 else if (IS_ZEBRA_DEBUG_KERNEL
)
2505 zlog_debug("Nexthop objects not supported on this kernel");
2511 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2512 int llalen
, ns_id_t ns_id
)
2514 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2515 addr
, lla
, llalen
, ns_id
);
2519 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2520 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2522 static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2525 uint8_t protocol
= RTPROT_ZEBRA
;
2531 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2532 const struct ipaddr
*addr
;
2534 memset(&req
, 0, sizeof(req
));
2536 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2537 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2538 if (cmd
== RTM_NEWNEIGH
)
2539 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2540 req
.n
.nlmsg_type
= cmd
;
2541 req
.ndm
.ndm_family
= PF_BRIDGE
;
2542 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2543 req
.ndm
.ndm_flags
|= NTF_SELF
; /* Handle by "self", not "master" */
2546 addattr_l(&req
.n
, sizeof(req
),
2547 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2548 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2549 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2551 addr
= dplane_ctx_neigh_get_ipaddr(ctx
);
2553 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &(addr
->ipaddr_v4
), 4);
2555 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2556 dplane_ctx_get_ns(ctx
), 0);
2560 #define NDA_RTA(r) \
2561 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2564 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2567 struct interface
*ifp
;
2568 struct zebra_if
*zif
;
2569 struct rtattr
*tb
[NDA_MAX
+ 1];
2570 struct interface
*br_if
;
2573 struct prefix vtep_ip
;
2574 int vid_present
= 0, dst_present
= 0;
2575 char buf
[ETHER_ADDR_STRLEN
];
2580 ndm
= NLMSG_DATA(h
);
2582 /* We only process macfdb notifications if EVPN is enabled */
2583 if (!is_evpn_enabled())
2586 /* The interface should exist. */
2587 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2589 if (!ifp
|| !ifp
->info
) {
2590 if (IS_ZEBRA_DEBUG_KERNEL
)
2591 zlog_debug("\t%s without associated interface: %u",
2592 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
2596 /* The interface should be something we're interested in. */
2597 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
2598 if (IS_ZEBRA_DEBUG_KERNEL
)
2599 zlog_debug("\t%s Not interested in %s, not a slave",
2600 __PRETTY_FUNCTION__
, ifp
->name
);
2604 /* Drop "permanent" entries. */
2605 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2606 if (IS_ZEBRA_DEBUG_KERNEL
)
2607 zlog_debug("\t%s Entry is PERMANENT, dropping",
2608 __PRETTY_FUNCTION__
);
2612 zif
= (struct zebra_if
*)ifp
->info
;
2613 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2614 if (IS_ZEBRA_DEBUG_KERNEL
)
2616 "%s family %s IF %s(%u) brIF %u - no bridge master",
2617 nl_msg_type_to_str(h
->nlmsg_type
),
2618 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2620 zif
->brslave_info
.bridge_ifindex
);
2624 /* Parse attributes and extract fields of interest. */
2625 memset(tb
, 0, sizeof tb
);
2626 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2628 if (!tb
[NDA_LLADDR
]) {
2629 if (IS_ZEBRA_DEBUG_KERNEL
)
2630 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2631 nl_msg_type_to_str(h
->nlmsg_type
),
2632 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2634 zif
->brslave_info
.bridge_ifindex
);
2638 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2639 if (IS_ZEBRA_DEBUG_KERNEL
)
2641 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2642 nl_msg_type_to_str(h
->nlmsg_type
),
2643 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2645 zif
->brslave_info
.bridge_ifindex
,
2646 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2650 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2652 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2654 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2655 sprintf(vid_buf
, " VLAN %u", vid
);
2659 /* TODO: Only IPv4 supported now. */
2661 vtep_ip
.family
= AF_INET
;
2662 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2663 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2665 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2668 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2670 if (IS_ZEBRA_DEBUG_KERNEL
)
2671 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2672 nl_msg_type_to_str(h
->nlmsg_type
),
2673 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2674 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2675 sticky
? "sticky " : "",
2676 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2677 dst_present
? dst_buf
: "");
2679 if (filter_vlan
&& vid
!= filter_vlan
) {
2680 if (IS_ZEBRA_DEBUG_KERNEL
)
2681 zlog_debug("\tFiltered due to filter vlan: %d",
2686 /* If add or update, do accordingly if learnt on a "local" interface; if
2687 * the notification is over VxLAN, this has to be related to
2689 * so perform an implicit delete of any local entry (if it exists).
2691 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2692 if (IS_ZEBRA_IF_VXLAN(ifp
))
2693 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2696 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2700 /* This is a delete notification.
2701 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2702 * 2. For a MAC over "local" interface, delete the mac
2703 * Note: We will get notifications from both bridge driver and VxLAN
2705 * Ignore the notification from VxLan driver as it is also generated
2706 * when mac moves from remote to local.
2709 if (IS_ZEBRA_DEBUG_KERNEL
)
2710 zlog_debug("\tNo Destination Present");
2714 if (IS_ZEBRA_IF_VXLAN(ifp
))
2715 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2718 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2721 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2726 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2729 /* Length validity. */
2730 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2734 /* We are interested only in AF_BRIDGE notifications. */
2735 ndm
= NLMSG_DATA(h
);
2736 if (ndm
->ndm_family
!= AF_BRIDGE
)
2739 return netlink_macfdb_change(h
, len
, ns_id
);
2742 /* Request for MAC FDB information from the kernel */
2743 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2744 int type
, ifindex_t master_ifindex
)
2748 struct ifinfomsg ifm
;
2752 /* Form the request, specifying filter (rtattr) if needed. */
2753 memset(&req
, 0, sizeof(req
));
2754 req
.n
.nlmsg_type
= type
;
2755 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2756 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2757 req
.ifm
.ifi_family
= family
;
2759 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2761 return netlink_request(netlink_cmd
, &req
.n
);
2765 * MAC forwarding database read using netlink interface. This is invoked
2768 int netlink_macfdb_read(struct zebra_ns
*zns
)
2771 struct zebra_dplane_info dp_info
;
2773 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2775 /* Get bridge FDB table. */
2776 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2780 /* We are reading entire table. */
2782 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2789 * MAC forwarding database read using netlink interface. This is for a
2790 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2792 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2793 struct interface
*br_if
)
2795 struct zebra_if
*br_zif
;
2796 struct zebra_if
*zif
;
2797 struct zebra_l2info_vxlan
*vxl
;
2798 struct zebra_dplane_info dp_info
;
2801 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2803 /* Save VLAN we're filtering on, if needed. */
2804 br_zif
= (struct zebra_if
*)br_if
->info
;
2805 zif
= (struct zebra_if
*)ifp
->info
;
2806 vxl
= &zif
->l2info
.vxl
;
2807 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2808 filter_vlan
= vxl
->access_vlan
;
2810 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2812 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2816 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2819 /* Reset VLAN filter. */
2825 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2826 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2829 struct interface
*br_if
,
2830 struct ethaddr
*mac
,
2838 struct zebra_if
*br_zif
;
2839 char buf
[ETHER_ADDR_STRLEN
];
2841 memset(&req
, 0, sizeof(req
));
2842 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2843 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2844 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2845 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2846 /* req.ndm.ndm_state = NUD_REACHABLE; */
2848 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2850 br_zif
= (struct zebra_if
*)br_if
->info
;
2851 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2852 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2854 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2856 if (IS_ZEBRA_DEBUG_KERNEL
)
2857 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2858 __PRETTY_FUNCTION__
,
2859 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2861 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2863 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2866 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2867 struct interface
*br_if
,
2868 struct ethaddr
*mac
, vlanid_t vid
)
2871 struct zebra_dplane_info dp_info
;
2873 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2875 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2877 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2883 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2890 * Netlink-specific handler for MAC updates using dataplane context object.
2892 static enum zebra_dplane_result
2893 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2895 uint8_t protocol
= RTPROT_ZEBRA
;
2903 int vid_present
= 0;
2905 struct in_addr vtep_ip
;
2908 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2913 memset(&req
, 0, sizeof(req
));
2915 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2916 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2917 if (cmd
== RTM_NEWNEIGH
)
2918 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2919 req
.n
.nlmsg_type
= cmd
;
2920 req
.ndm
.ndm_family
= AF_BRIDGE
;
2921 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2922 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2924 if (dplane_ctx_mac_is_sticky(ctx
))
2925 req
.ndm
.ndm_state
|= NUD_NOARP
;
2927 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2929 addattr_l(&req
.n
, sizeof(req
),
2930 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2931 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2932 dplane_ctx_mac_get_addr(ctx
), 6);
2933 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2935 dst_alen
= 4; // TODO: hardcoded
2936 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2937 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2939 vid
= dplane_ctx_mac_get_vlan(ctx
);
2942 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2945 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
,
2946 dplane_ctx_mac_get_br_ifindex(ctx
));
2948 if (IS_ZEBRA_DEBUG_KERNEL
) {
2949 char ipbuf
[PREFIX_STRLEN
];
2950 char buf
[ETHER_ADDR_STRLEN
];
2951 char dst_buf
[PREFIX_STRLEN
+ 10];
2955 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2959 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2960 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2961 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2963 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2964 nl_msg_type_to_str(cmd
),
2965 nl_family_to_str(req
.ndm
.ndm_family
),
2966 dplane_ctx_get_ifname(ctx
),
2967 dplane_ctx_get_ifindex(ctx
), vid_buf
,
2968 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2972 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
2973 dplane_ctx_get_ns(ctx
), 0);
2975 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2977 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2981 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2982 * 5549 support, re-install them.
2984 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
2985 struct interface
*ifp
, struct ipaddr
*ip
,
2988 if (ndm
->ndm_family
!= AF_INET
)
2991 if (!zif
->v6_2_v4_ll_neigh_entry
)
2994 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
2997 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
2998 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3003 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3007 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3010 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3013 struct interface
*ifp
;
3014 struct zebra_if
*zif
;
3015 struct rtattr
*tb
[NDA_MAX
+ 1];
3016 struct interface
*link_if
;
3019 char buf
[ETHER_ADDR_STRLEN
];
3020 char buf2
[INET6_ADDRSTRLEN
];
3021 int mac_present
= 0;
3025 ndm
= NLMSG_DATA(h
);
3027 /* The interface should exist. */
3028 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3030 if (!ifp
|| !ifp
->info
)
3033 zif
= (struct zebra_if
*)ifp
->info
;
3035 /* Parse attributes and extract fields of interest. */
3036 memset(tb
, 0, sizeof tb
);
3037 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3040 zlog_debug("%s family %s IF %s(%u) - no DST",
3041 nl_msg_type_to_str(h
->nlmsg_type
),
3042 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3047 memset(&ip
, 0, sizeof(struct ipaddr
));
3048 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3049 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3051 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3052 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3053 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3054 if (IS_ZEBRA_DEBUG_KERNEL
)
3056 "\tNeighbor Entry Received is a 5549 entry, finished");
3060 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3061 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3062 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3064 /* The neighbor is present on an SVI. From this, we locate the
3066 * bridge because we're only interested in neighbors on a VxLAN bridge.
3067 * The bridge is located based on the nature of the SVI:
3068 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3070 * and is linked to the bridge
3071 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3075 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3076 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3080 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3083 if (IS_ZEBRA_DEBUG_KERNEL
)
3085 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3089 memset(&mac
, 0, sizeof(struct ethaddr
));
3090 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3091 if (tb
[NDA_LLADDR
]) {
3092 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3093 if (IS_ZEBRA_DEBUG_KERNEL
)
3095 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3100 ifp
->name
, ndm
->ndm_ifindex
,
3101 (unsigned long)RTA_PAYLOAD(
3107 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3110 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3111 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3113 if (IS_ZEBRA_DEBUG_KERNEL
)
3115 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3116 nl_msg_type_to_str(h
->nlmsg_type
),
3117 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3119 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3121 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3123 ndm
->ndm_state
, ndm
->ndm_flags
);
3125 /* If the neighbor state is valid for use, process as an add or
3127 * else process as a delete. Note that the delete handling may
3129 * in re-adding the neighbor if it is a valid "remote" neighbor.
3131 if (ndm
->ndm_state
& NUD_VALID
)
3132 return zebra_vxlan_handle_kernel_neigh_update(
3133 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3136 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3139 if (IS_ZEBRA_DEBUG_KERNEL
)
3140 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3141 nl_msg_type_to_str(h
->nlmsg_type
),
3142 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3144 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3146 /* Process the delete - it may result in re-adding the neighbor if it is
3147 * a valid "remote" neighbor.
3149 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3152 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3157 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3160 /* Length validity. */
3161 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3165 /* We are interested only in AF_INET or AF_INET6 notifications. */
3166 ndm
= NLMSG_DATA(h
);
3167 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3170 return netlink_neigh_change(h
, len
);
3173 /* Request for IP neighbor information from the kernel */
3174 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3175 int type
, ifindex_t ifindex
)
3183 /* Form the request, specifying filter (rtattr) if needed. */
3184 memset(&req
, 0, sizeof(req
));
3185 req
.n
.nlmsg_type
= type
;
3186 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3187 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3188 req
.ndm
.ndm_family
= family
;
3190 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3192 return netlink_request(netlink_cmd
, &req
.n
);
3196 * IP Neighbor table read using netlink interface. This is invoked
3199 int netlink_neigh_read(struct zebra_ns
*zns
)
3202 struct zebra_dplane_info dp_info
;
3204 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3206 /* Get IP neighbor table. */
3207 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3211 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3218 * IP Neighbor table read using netlink interface. This is for a specific
3221 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3224 struct zebra_dplane_info dp_info
;
3226 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3228 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3232 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3239 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3240 * read using netlink interface.
3242 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3243 int type
, struct ipaddr
*ip
,
3253 /* Form the request, specifying filter (rtattr) if needed. */
3254 memset(&req
, 0, sizeof(req
));
3255 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3256 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3257 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3258 req
.ndm
.ndm_ifindex
= ifindex
;
3260 if (IS_IPADDR_V4(ip
)) {
3261 ipa_len
= IPV4_MAX_BYTELEN
;
3262 req
.ndm
.ndm_family
= AF_INET
;
3265 ipa_len
= IPV6_MAX_BYTELEN
;
3266 req
.ndm
.ndm_family
= AF_INET6
;
3269 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3271 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
3274 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3275 struct interface
*vlan_if
)
3278 struct zebra_ns
*zns
;
3279 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3280 char buf
[INET6_ADDRSTRLEN
];
3281 struct zebra_dplane_info dp_info
;
3285 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3287 if (IS_ZEBRA_DEBUG_KERNEL
)
3288 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3289 __PRETTY_FUNCTION__
, vlan_if
->name
,
3291 ipaddr2str(ip
, buf
, sizeof(buf
)),
3294 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3299 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3305 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3310 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3313 /* Length validity. */
3314 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3316 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3317 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
3318 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3322 /* Is this a notification for the MAC FDB or IP neighbor table? */
3323 ndm
= NLMSG_DATA(h
);
3324 if (ndm
->ndm_family
== AF_BRIDGE
)
3325 return netlink_macfdb_change(h
, len
, ns_id
);
3327 if (ndm
->ndm_type
!= RTN_UNICAST
)
3330 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3331 return netlink_ipneigh_change(h
, len
, ns_id
);
3334 EC_ZEBRA_UNKNOWN_FAMILY
,
3335 "Invalid address family: %u received from kernel neighbor change: %s",
3336 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3344 * Utility neighbor-update function, using info from dplane context.
3346 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3349 uint8_t protocol
= RTPROT_ZEBRA
;
3356 char buf
[INET6_ADDRSTRLEN
];
3357 char buf2
[ETHER_ADDR_STRLEN
];
3358 const struct ipaddr
*ip
;
3359 const struct ethaddr
*mac
;
3363 memset(&req
, 0, sizeof(req
));
3365 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3366 mac
= dplane_ctx_neigh_get_mac(ctx
);
3367 if (is_zero_mac(mac
))
3370 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3371 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3373 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3374 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3375 if (cmd
== RTM_NEWNEIGH
)
3376 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3377 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
3378 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3379 req
.ndm
.ndm_state
= state
;
3380 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3381 req
.ndm
.ndm_type
= RTN_UNICAST
;
3382 req
.ndm
.ndm_flags
= flags
;
3384 addattr_l(&req
.n
, sizeof(req
),
3385 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
3386 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3387 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3389 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3391 if (IS_ZEBRA_DEBUG_KERNEL
)
3392 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3393 nl_msg_type_to_str(cmd
),
3394 nl_family_to_str(req
.ndm
.ndm_family
),
3395 dplane_ctx_get_ifname(ctx
),
3396 dplane_ctx_get_ifindex(ctx
),
3397 ipaddr2str(ip
, buf
, sizeof(buf
)),
3398 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
3402 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3403 dplane_ctx_get_ns(ctx
), 0);
3407 * Update MAC, using dataplane context object.
3409 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
3411 return netlink_macfdb_update_ctx(ctx
);
3414 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
3418 switch (dplane_ctx_get_op(ctx
)) {
3419 case DPLANE_OP_NEIGH_INSTALL
:
3420 case DPLANE_OP_NEIGH_UPDATE
:
3421 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
3423 case DPLANE_OP_NEIGH_DELETE
:
3424 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
3426 case DPLANE_OP_VTEP_ADD
:
3427 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
);
3429 case DPLANE_OP_VTEP_DELETE
:
3430 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
);
3437 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
3441 * MPLS label forwarding table change via netlink interface, using dataplane
3442 * context information.
3444 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
3447 const zebra_nhlfe_t
*nhlfe
;
3448 struct nexthop
*nexthop
= NULL
;
3449 unsigned int nexthop_num
;
3450 const char *routedesc
;
3456 char buf
[NL_PKT_BUF_SIZE
];
3459 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
3462 * Count # nexthops so we can decide whether to use singlepath
3463 * or multipath case.
3466 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
3467 nexthop
= nhlfe
->nexthop
;
3470 if (cmd
== RTM_NEWROUTE
) {
3471 /* Count all selected NHLFEs */
3472 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3473 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3476 /* Count all installed NHLFEs */
3477 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3478 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3483 if ((nexthop_num
== 0) ||
3484 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3487 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3488 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3489 req
.n
.nlmsg_type
= cmd
;
3490 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3492 req
.r
.rtm_family
= AF_MPLS
;
3493 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3494 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3495 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3496 req
.r
.rtm_type
= RTN_UNICAST
;
3498 if (cmd
== RTM_NEWROUTE
) {
3499 /* We do a replace to handle update. */
3500 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3502 /* set the protocol value if installing */
3503 route_type
= re_type_from_lsp_type(
3504 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3505 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3508 /* Fill destination */
3509 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3510 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3512 /* Fill nexthops (paths) based on single-path or multipath. The paths
3513 * chosen depend on the operation.
3515 if (nexthop_num
== 1) {
3516 routedesc
= "single-path";
3517 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3521 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3522 nhlfe
; nhlfe
= nhlfe
->next
) {
3523 nexthop
= nhlfe
->nexthop
;
3527 if ((cmd
== RTM_NEWROUTE
3528 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3529 && CHECK_FLAG(nexthop
->flags
,
3530 NEXTHOP_FLAG_ACTIVE
)))
3531 || (cmd
== RTM_DELROUTE
3532 && (CHECK_FLAG(nhlfe
->flags
,
3533 NHLFE_FLAG_INSTALLED
)
3534 && CHECK_FLAG(nexthop
->flags
,
3535 NEXTHOP_FLAG_FIB
)))) {
3536 /* Add the gateway */
3537 _netlink_mpls_build_singlepath(
3546 } else { /* Multipath case */
3547 char buf
[NL_PKT_BUF_SIZE
];
3548 struct rtattr
*rta
= (void *)buf
;
3549 struct rtnexthop
*rtnh
;
3550 const union g_addr
*src1
= NULL
;
3552 rta
->rta_type
= RTA_MULTIPATH
;
3553 rta
->rta_len
= RTA_LENGTH(0);
3554 rtnh
= RTA_DATA(rta
);
3556 routedesc
= "multipath";
3557 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3561 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3562 nhlfe
; nhlfe
= nhlfe
->next
) {
3563 nexthop
= nhlfe
->nexthop
;
3567 if ((cmd
== RTM_NEWROUTE
3568 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3569 && CHECK_FLAG(nexthop
->flags
,
3570 NEXTHOP_FLAG_ACTIVE
)))
3571 || (cmd
== RTM_DELROUTE
3572 && (CHECK_FLAG(nhlfe
->flags
,
3573 NHLFE_FLAG_INSTALLED
)
3574 && CHECK_FLAG(nexthop
->flags
,
3575 NEXTHOP_FLAG_FIB
)))) {
3578 /* Build the multipath */
3579 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3582 rtnh
= RTNH_NEXT(rtnh
);
3586 /* Add the multipath */
3587 if (rta
->rta_len
> RTA_LENGTH(0))
3588 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3589 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3592 /* Talk to netlink socket. */
3593 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3594 dplane_ctx_get_ns(ctx
), 0);
3596 #endif /* HAVE_NETLINK */