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"
54 #include "zebra/zapi_msg.h"
55 #include "zebra/zebra_ns.h"
56 #include "zebra/zebra_vrf.h"
58 #include "zebra/redistribute.h"
59 #include "zebra/interface.h"
60 #include "zebra/debug.h"
61 #include "zebra/rtadv.h"
62 #include "zebra/zebra_ptm.h"
63 #include "zebra/zebra_mpls.h"
64 #include "zebra/kernel_netlink.h"
65 #include "zebra/rt_netlink.h"
66 #include "zebra/zebra_nhg.h"
67 #include "zebra/zebra_mroute.h"
68 #include "zebra/zebra_vxlan.h"
69 #include "zebra/zebra_errors.h"
75 static vlanid_t filter_vlan
= 0;
83 char ipv4_ll_buf
[16] = "169.254.0.1";
84 struct in_addr ipv4_ll
;
87 * The ipv4_ll data structure is used for all 5549
88 * additions to the kernel. Let's figure out the
89 * correct value one time instead for every
90 * install/remove of a 5549 type route
92 void rt_netlink_init(void)
94 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
98 * Mapping from dataplane neighbor flags to netlink flags
100 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
104 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
105 flags
|= NTF_EXT_LEARNED
;
106 if (dplane_flags
& DPLANE_NTF_ROUTER
)
113 * Mapping from dataplane neighbor state to netlink state
115 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
119 if (dplane_state
& DPLANE_NUD_REACHABLE
)
120 state
|= NUD_REACHABLE
;
121 if (dplane_state
& DPLANE_NUD_STALE
)
123 if (dplane_state
& DPLANE_NUD_NOARP
)
125 if (dplane_state
& DPLANE_NUD_PROBE
)
132 static inline int is_selfroute(int proto
)
134 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
135 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
136 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
137 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
138 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
139 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
140 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
147 static inline int zebra2proto(int proto
)
150 case ZEBRA_ROUTE_BABEL
:
151 proto
= RTPROT_BABEL
;
153 case ZEBRA_ROUTE_BGP
:
156 case ZEBRA_ROUTE_OSPF
:
157 case ZEBRA_ROUTE_OSPF6
:
160 case ZEBRA_ROUTE_STATIC
:
161 proto
= RTPROT_ZSTATIC
;
163 case ZEBRA_ROUTE_ISIS
:
166 case ZEBRA_ROUTE_RIP
:
169 case ZEBRA_ROUTE_RIPNG
:
170 proto
= RTPROT_RIPNG
;
172 case ZEBRA_ROUTE_NHRP
:
175 case ZEBRA_ROUTE_EIGRP
:
176 proto
= RTPROT_EIGRP
;
178 case ZEBRA_ROUTE_LDP
:
181 case ZEBRA_ROUTE_SHARP
:
182 proto
= RTPROT_SHARP
;
184 case ZEBRA_ROUTE_PBR
:
187 case ZEBRA_ROUTE_OPENFABRIC
:
188 proto
= RTPROT_OPENFABRIC
;
190 case ZEBRA_ROUTE_TABLE
:
191 proto
= RTPROT_ZEBRA
;
195 * When a user adds a new protocol this will show up
196 * to let them know to do something about it. This
197 * is intentionally a warn because we should see
198 * this as part of development of a new protocol
201 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
202 __PRETTY_FUNCTION__
, proto
);
203 proto
= RTPROT_ZEBRA
;
210 static inline int proto2zebra(int proto
, int family
)
214 proto
= ZEBRA_ROUTE_BABEL
;
217 proto
= ZEBRA_ROUTE_BGP
;
220 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
224 proto
= ZEBRA_ROUTE_ISIS
;
227 proto
= ZEBRA_ROUTE_RIP
;
230 proto
= ZEBRA_ROUTE_RIPNG
;
233 proto
= ZEBRA_ROUTE_NHRP
;
236 proto
= ZEBRA_ROUTE_EIGRP
;
239 proto
= ZEBRA_ROUTE_LDP
;
243 proto
= ZEBRA_ROUTE_STATIC
;
246 proto
= ZEBRA_ROUTE_SHARP
;
249 proto
= ZEBRA_ROUTE_PBR
;
251 case RTPROT_OPENFABRIC
:
252 proto
= ZEBRA_ROUTE_OPENFABRIC
;
256 * When a user adds a new protocol this will show up
257 * to let them know to do something about it. This
258 * is intentionally a warn because we should see
259 * this as part of development of a new protocol
262 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
263 __PRETTY_FUNCTION__
, proto
);
264 proto
= ZEBRA_ROUTE_KERNEL
;
271 Pending: create an efficient table_id (in a tree/hash) based lookup)
273 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
276 struct zebra_vrf
*zvrf
;
278 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
282 /* case vrf with netns : match the netnsid */
283 if (vrf_is_backend_netns()) {
284 if (ns_id
== zvrf_id(zvrf
))
285 return zvrf_id(zvrf
);
287 /* VRF is VRF_BACKEND_VRF_LITE */
288 if (zvrf
->table_id
!= table_id
)
290 return zvrf_id(zvrf
);
298 * @parse_encap_mpls() - Parses encapsulated mpls attributes
299 * @tb: Pointer to rtattr to look for nested items in.
300 * @labels: Pointer to store labels in.
302 * Return: Number of mpls labels found.
304 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
306 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
307 mpls_lse_t
*lses
= NULL
;
312 mpls_label_t label
= 0;
314 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
315 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
316 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
317 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
318 labels
[num_labels
++] = label
;
324 static struct nexthop
325 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
326 enum blackhole_type bh_type
, int index
, void *prefsrc
,
327 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
329 struct interface
*ifp
= NULL
;
330 struct nexthop nh
= {0};
331 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
334 vrf_id_t nh_vrf_id
= vrf_id
;
335 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
337 if (bh_type
== BLACKHOLE_UNSPEC
) {
339 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
340 else if (index
&& gate
)
341 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
342 : NEXTHOP_TYPE_IPV6_IFINDEX
;
343 else if (!index
&& gate
)
344 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
347 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
348 nh
.bh_type
= bh_type
;
351 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
352 nh
.bh_type
= bh_type
;
356 memcpy(&nh
.src
, prefsrc
, sz
);
358 memcpy(&nh
.gate
, gate
, sz
);
361 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
363 nh_vrf_id
= ifp
->vrf_id
;
365 nh
.vrf_id
= nh_vrf_id
;
367 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
368 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
369 == LWTUNNEL_ENCAP_MPLS
) {
370 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
373 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
374 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
377 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
382 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
383 struct route_entry
*re
,
385 struct rtnexthop
*rtnh
,
387 void *prefsrc
, vrf_id_t vrf_id
)
390 struct interface
*ifp
= NULL
;
393 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
395 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
397 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
398 vrf_id_t nh_vrf_id
= vrf_id
;
400 re
->ng
= nexthop_group_new();
403 struct nexthop
*nh
= NULL
;
405 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
408 index
= rtnh
->rtnh_ifindex
;
411 * Yes we are looking this up
412 * for every nexthop and just
413 * using the last one looked
416 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
419 nh_vrf_id
= ifp
->vrf_id
;
422 EC_ZEBRA_UNKNOWN_INTERFACE
,
423 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
424 __PRETTY_FUNCTION__
, index
);
425 nh_vrf_id
= VRF_DEFAULT
;
430 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
431 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
433 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
434 rtnh
->rtnh_len
- sizeof(*rtnh
));
435 if (rtnh_tb
[RTA_GATEWAY
])
436 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
437 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
438 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
439 == LWTUNNEL_ENCAP_MPLS
) {
440 num_labels
= parse_encap_mpls(
441 rtnh_tb
[RTA_ENCAP
], labels
);
446 if (rtm
->rtm_family
== AF_INET
) {
448 nh
= route_entry_nexthop_ipv4_ifindex_add(
449 re
, gate
, prefsrc
, index
,
452 nh
= route_entry_nexthop_ipv4_add(
453 re
, gate
, prefsrc
, nh_vrf_id
);
454 } else if (rtm
->rtm_family
== AF_INET6
) {
456 nh
= route_entry_nexthop_ipv6_ifindex_add(
457 re
, gate
, index
, nh_vrf_id
);
459 nh
= route_entry_nexthop_ipv6_add(
460 re
, gate
, nh_vrf_id
);
463 nh
= route_entry_nexthop_ifindex_add(re
, index
,
468 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
471 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
472 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
475 if (rtnh
->rtnh_len
== 0)
478 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
479 rtnh
= RTNH_NEXT(rtnh
);
482 uint8_t nhop_num
= nexthop_group_nexthop_num(re
->ng
);
485 nexthop_group_delete(&re
->ng
);
490 /* Looking up routing table by netlink interface. */
491 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
496 struct rtattr
*tb
[RTA_MAX
+ 1];
499 struct prefix_ipv6 src_p
= {};
502 char anyaddr
[16] = {0};
504 int proto
= ZEBRA_ROUTE_KERNEL
;
509 uint8_t distance
= 0;
515 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
516 void *src
= NULL
; /* IPv6 srcdest source prefix */
517 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
521 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
523 switch (rtm
->rtm_type
) {
527 bh_type
= BLACKHOLE_NULL
;
529 case RTN_UNREACHABLE
:
530 bh_type
= BLACKHOLE_REJECT
;
533 bh_type
= BLACKHOLE_ADMINPROHIB
;
536 if (IS_ZEBRA_DEBUG_KERNEL
)
537 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
538 nl_rttype_to_str(rtm
->rtm_type
),
543 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
545 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
546 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
547 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
551 memset(tb
, 0, sizeof tb
);
552 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
554 if (rtm
->rtm_flags
& RTM_F_CLONED
)
556 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
558 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
561 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
562 && h
->nlmsg_type
== RTM_NEWROUTE
) {
563 if (IS_ZEBRA_DEBUG_KERNEL
)
564 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
569 /* We don't care about change notifications for the MPLS table. */
570 /* TODO: Revisit this. */
571 if (rtm
->rtm_family
== AF_MPLS
)
574 /* Table corresponding to route. */
576 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
578 table
= rtm
->rtm_table
;
581 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
582 if (vrf_id
== VRF_DEFAULT
) {
583 if (!is_zebra_valid_kernel_table(table
)
584 && !is_zebra_main_routing_table(table
))
588 /* Route which inserted by Zebra. */
589 if (is_selfroute(rtm
->rtm_protocol
)) {
590 flags
|= ZEBRA_FLAG_SELFROUTE
;
591 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
594 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
597 dest
= RTA_DATA(tb
[RTA_DST
]);
602 src
= RTA_DATA(tb
[RTA_SRC
]);
607 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
610 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
613 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
615 if (tb
[RTA_PRIORITY
])
616 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
618 #if defined(SUPPORT_REALMS)
620 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
623 if (tb
[RTA_METRICS
]) {
624 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
626 memset(mxrta
, 0, sizeof mxrta
);
627 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
628 RTA_PAYLOAD(tb
[RTA_METRICS
]));
631 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
634 if (rtm
->rtm_family
== AF_INET
) {
636 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
638 "Invalid destination prefix length: %u received from kernel route change",
642 memcpy(&p
.u
.prefix4
, dest
, 4);
643 p
.prefixlen
= rtm
->rtm_dst_len
;
645 if (rtm
->rtm_src_len
!= 0) {
646 char buf
[PREFIX_STRLEN
];
648 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
649 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
650 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
654 /* Force debug below to not display anything for source */
656 } else if (rtm
->rtm_family
== AF_INET6
) {
658 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
660 "Invalid destination prefix length: %u received from kernel route change",
664 memcpy(&p
.u
.prefix6
, dest
, 16);
665 p
.prefixlen
= rtm
->rtm_dst_len
;
667 src_p
.family
= AF_INET6
;
668 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
670 "Invalid source prefix length: %u received from kernel route change",
674 memcpy(&src_p
.prefix
, src
, 16);
675 src_p
.prefixlen
= rtm
->rtm_src_len
;
679 * For ZEBRA_ROUTE_KERNEL types:
681 * The metric/priority of the route received from the kernel
682 * is a 32 bit number. We are going to interpret the high
683 * order byte as the Admin Distance and the low order 3 bytes
686 * This will allow us to do two things:
687 * 1) Allow the creation of kernel routes that can be
688 * overridden by zebra.
689 * 2) Allow the old behavior for 'most' kernel route types
690 * if a user enters 'ip route ...' v4 routes get a metric
691 * of 0 and v6 routes get a metric of 1024. Both of these
692 * values will end up with a admin distance of 0, which
693 * will cause them to win for the purposes of zebra.
695 if (proto
== ZEBRA_ROUTE_KERNEL
) {
696 distance
= (metric
>> 24) & 0xFF;
697 metric
= (metric
& 0x00FFFFFF);
700 if (IS_ZEBRA_DEBUG_KERNEL
) {
701 char buf
[PREFIX_STRLEN
];
702 char buf2
[PREFIX_STRLEN
];
703 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
704 nl_msg_type_to_str(h
->nlmsg_type
),
705 prefix2str(&p
, buf
, sizeof(buf
)),
706 src_p
.prefixlen
? " from " : "",
708 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
710 vrf_id
, table
, metric
, distance
);
714 if (rtm
->rtm_family
== AF_INET6
)
717 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
719 if (!tb
[RTA_MULTIPATH
]) {
720 struct nexthop nh
= {0};
723 nh
= parse_nexthop_unicast(
724 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
727 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
728 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
731 /* This is a multipath route */
732 struct route_entry
*re
;
733 struct rtnexthop
*rtnh
=
734 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
736 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
738 re
->distance
= distance
;
744 re
->uptime
= monotime(NULL
);
750 parse_multipath_nexthops_unicast(
751 ns_id
, re
, rtm
, rtnh
, tb
,
754 zserv_nexthop_num_warn(
755 __func__
, (const struct prefix
*)&p
,
759 if (nhe_id
|| re
->ng
)
760 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
766 // TODO: Use nhe_id here as well
767 if (!tb
[RTA_MULTIPATH
]) {
769 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
771 memset(&nh
, 0, sizeof(nh
));
772 if (bh_type
== BLACKHOLE_UNSPEC
) {
774 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
775 else if (index
&& gate
)
778 ? NEXTHOP_TYPE_IPV4_IFINDEX
779 : NEXTHOP_TYPE_IPV6_IFINDEX
;
780 else if (!index
&& gate
)
781 nh
.type
= (afi
== AFI_IP
)
785 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
786 nh
.bh_type
= BLACKHOLE_UNSPEC
;
789 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
790 nh
.bh_type
= bh_type
;
794 memcpy(&nh
.gate
, gate
, sz
);
795 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
796 &p
, &src_p
, &nh
, table
, metric
, distance
,
799 /* XXX: need to compare the entire list of nexthops
800 * here for NLM_F_APPEND stupidity */
801 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
802 &p
, &src_p
, NULL
, table
, metric
, distance
,
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 /* This function takes a nexthop as argument and adds
1062 * the appropriate netlink attributes to an existing
1065 * @param routedesc: Human readable description of route type
1066 * (direct/recursive, single-/multipath)
1067 * @param bytelen: Length of addresses in bytes.
1068 * @param nexthop: Nexthop information
1069 * @param nlmsg: nlmsghdr structure to fill in.
1070 * @param req_size: The size allocated for the message.
1072 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
1073 const struct nexthop
*nexthop
,
1074 struct nlmsghdr
*nlmsg
,
1075 struct rtmsg
*rtmsg
,
1076 size_t req_size
, int cmd
)
1078 struct mpls_label_stack
*nh_label
;
1079 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1081 char label_buf
[256];
1084 * label_buf is *only* currently used within debugging.
1085 * As such when we assign it we are guarding it inside
1086 * a debug test. If you want to change this make sure
1087 * you fix this assumption
1089 label_buf
[0] = '\0';
1092 char label_buf1
[20];
1094 nh_label
= nexthop
->nh_label
;
1096 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1097 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1100 if (IS_ZEBRA_DEBUG_KERNEL
) {
1102 sprintf(label_buf
, "label %u",
1103 nh_label
->label
[i
]);
1105 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1106 strlcat(label_buf
, label_buf1
,
1111 out_lse
[num_labels
] =
1112 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1117 /* Set the BoS bit */
1118 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1120 if (rtmsg
->rtm_family
== AF_MPLS
)
1121 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1122 num_labels
* sizeof(mpls_lse_t
));
1124 struct rtattr
*nest
;
1125 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1127 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1129 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1130 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1131 num_labels
* sizeof(mpls_lse_t
));
1132 addattr_nest_end(nlmsg
, nest
);
1136 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1137 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1139 if (rtmsg
->rtm_family
== AF_INET
1140 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1141 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1142 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1143 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1144 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1146 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1147 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1148 &nexthop
->rmap_src
.ipv4
, bytelen
);
1149 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1150 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1151 &nexthop
->src
.ipv4
, bytelen
);
1153 if (IS_ZEBRA_DEBUG_KERNEL
)
1155 " 5549: _netlink_route_build_singlepath() (%s): "
1156 "nexthop via %s %s if %u(%u)",
1157 routedesc
, ipv4_ll_buf
, label_buf
,
1158 nexthop
->ifindex
, nexthop
->vrf_id
);
1162 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1163 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1164 /* Send deletes to the kernel without specifying the next-hop */
1165 if (cmd
!= RTM_DELROUTE
)
1166 _netlink_route_nl_add_gateway_info(
1167 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1170 if (cmd
== RTM_NEWROUTE
) {
1171 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1172 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1173 &nexthop
->rmap_src
.ipv4
, bytelen
);
1174 else if (nexthop
->src
.ipv4
.s_addr
)
1175 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1176 &nexthop
->src
.ipv4
, bytelen
);
1179 if (IS_ZEBRA_DEBUG_KERNEL
)
1181 "netlink_route_multipath() (%s): "
1182 "nexthop via %s %s if %u(%u)",
1183 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1184 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1187 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1188 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1189 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1190 nlmsg
, req_size
, bytelen
,
1193 if (cmd
== RTM_NEWROUTE
) {
1194 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1195 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1196 &nexthop
->rmap_src
.ipv6
, bytelen
);
1197 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1198 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1199 &nexthop
->src
.ipv6
, bytelen
);
1202 if (IS_ZEBRA_DEBUG_KERNEL
)
1204 "netlink_route_multipath() (%s): "
1205 "nexthop via %s %s if %u(%u)",
1206 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1207 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1211 * We have the ifindex so we should always send it
1212 * This is especially useful if we are doing route
1215 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1216 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1218 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1219 if (cmd
== RTM_NEWROUTE
) {
1220 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1221 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1222 &nexthop
->rmap_src
.ipv4
, bytelen
);
1223 else if (nexthop
->src
.ipv4
.s_addr
)
1224 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1225 &nexthop
->src
.ipv4
, bytelen
);
1228 if (IS_ZEBRA_DEBUG_KERNEL
)
1230 "netlink_route_multipath() (%s): "
1231 "nexthop via if %u(%u)",
1232 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1236 /* This function takes a nexthop as argument and
1237 * appends to the given rtattr/rtnexthop pair the
1238 * representation of the nexthop. If the nexthop
1239 * defines a preferred source, the src parameter
1240 * will be modified to point to that src, otherwise
1241 * it will be kept unmodified.
1243 * @param routedesc: Human readable description of route type
1244 * (direct/recursive, single-/multipath)
1245 * @param bytelen: Length of addresses in bytes.
1246 * @param nexthop: Nexthop information
1247 * @param rta: rtnetlink attribute structure
1248 * @param rtnh: pointer to an rtnetlink nexthop structure
1249 * @param src: pointer pointing to a location where
1250 * the prefsrc should be stored.
1252 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1253 const struct nexthop
*nexthop
,
1255 struct rtnexthop
*rtnh
,
1256 struct rtmsg
*rtmsg
,
1257 const union g_addr
**src
)
1259 struct mpls_label_stack
*nh_label
;
1260 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1262 char label_buf
[256];
1264 rtnh
->rtnh_len
= sizeof(*rtnh
);
1265 rtnh
->rtnh_flags
= 0;
1266 rtnh
->rtnh_hops
= 0;
1267 rta
->rta_len
+= rtnh
->rtnh_len
;
1270 * label_buf is *only* currently used within debugging.
1271 * As such when we assign it we are guarding it inside
1272 * a debug test. If you want to change this make sure
1273 * you fix this assumption
1275 label_buf
[0] = '\0';
1278 char label_buf1
[20];
1280 nh_label
= nexthop
->nh_label
;
1282 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1283 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1286 if (IS_ZEBRA_DEBUG_KERNEL
) {
1288 sprintf(label_buf
, "label %u",
1289 nh_label
->label
[i
]);
1291 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1292 strlcat(label_buf
, label_buf1
,
1297 out_lse
[num_labels
] =
1298 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
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
;
1341 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1342 *src
= &nexthop
->rmap_src
;
1343 else if (nexthop
->src
.ipv4
.s_addr
)
1344 *src
= &nexthop
->src
;
1346 if (IS_ZEBRA_DEBUG_KERNEL
)
1348 " 5549: netlink_route_build_multipath() (%s): "
1349 "nexthop via %s %s if %u",
1350 routedesc
, ipv4_ll_buf
, label_buf
,
1355 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1356 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1357 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1358 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1360 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1361 *src
= &nexthop
->rmap_src
;
1362 else if (nexthop
->src
.ipv4
.s_addr
)
1363 *src
= &nexthop
->src
;
1365 if (IS_ZEBRA_DEBUG_KERNEL
)
1367 "netlink_route_multipath() (%s): "
1368 "nexthop via %s %s if %u",
1369 routedesc
, 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): "
1386 "nexthop via %s %s if %u",
1387 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1388 label_buf
, nexthop
->ifindex
);
1392 * We have figured out the ifindex so we should always send it
1393 * This is especially useful if we are doing route
1396 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1397 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1400 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1401 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1402 *src
= &nexthop
->rmap_src
;
1403 else if (nexthop
->src
.ipv4
.s_addr
)
1404 *src
= &nexthop
->src
;
1406 if (IS_ZEBRA_DEBUG_KERNEL
)
1408 "netlink_route_multipath() (%s): "
1409 "nexthop via if %u",
1410 routedesc
, nexthop
->ifindex
);
1414 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1415 const zebra_nhlfe_t
*nhlfe
,
1416 struct nlmsghdr
*nlmsg
,
1417 struct rtmsg
*rtmsg
,
1418 size_t req_size
, int cmd
)
1423 family
= NHLFE_FAMILY(nhlfe
);
1424 bytelen
= (family
== AF_INET
? 4 : 16);
1425 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1426 nlmsg
, rtmsg
, req_size
, cmd
);
1431 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1432 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1433 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1438 family
= NHLFE_FAMILY(nhlfe
);
1439 bytelen
= (family
== AF_INET
? 4 : 16);
1440 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1445 /* Log debug information for netlink_route_multipath
1446 * if debug logging is enabled.
1448 * @param cmd: Netlink command which is to be processed
1449 * @param p: Prefix for which the change is due
1450 * @param family: Address family which the change concerns
1451 * @param zvrf: The vrf we are in
1452 * @param tableid: The table we are working on
1454 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1455 int family
, vrf_id_t vrfid
,
1458 if (IS_ZEBRA_DEBUG_KERNEL
) {
1459 char buf
[PREFIX_STRLEN
];
1461 "netlink_route_multipath(): %s %s vrf %u(%u)",
1462 nl_msg_type_to_str(cmd
),
1463 prefix2str(p
, buf
, sizeof(buf
)),
1468 static void _netlink_nexthop_debug(int cmd
, uint32_t id
)
1470 if (IS_ZEBRA_DEBUG_KERNEL
)
1471 zlog_debug("netlink_nexthop(): %s, id=%u",
1472 nl_msg_type_to_str(cmd
), id
);
1475 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1477 if (IS_ZEBRA_DEBUG_KERNEL
)
1478 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1479 nl_msg_type_to_str(cmd
), label
);
1482 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1483 int llalen
, ns_id_t ns_id
)
1485 uint8_t protocol
= RTPROT_ZEBRA
;
1492 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1494 memset(&req
, 0, sizeof(req
));
1496 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1497 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1498 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1499 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1501 req
.ndm
.ndm_family
= AF_INET
;
1502 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1503 req
.ndm
.ndm_ifindex
= ifindex
;
1504 req
.ndm
.ndm_type
= RTN_UNICAST
;
1506 addattr_l(&req
.n
, sizeof(req
),
1507 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
1508 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1509 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1511 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1516 * Routing table change via netlink interface, using a dataplane context object
1518 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1521 struct nexthop
*nexthop
= NULL
;
1522 unsigned int nexthop_num
;
1524 const char *routedesc
;
1527 const struct prefix
*p
, *src_p
;
1533 char buf
[NL_PKT_BUF_SIZE
];
1536 p
= dplane_ctx_get_dest(ctx
);
1537 src_p
= dplane_ctx_get_src(ctx
);
1539 family
= PREFIX_FAMILY(p
);
1541 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1543 bytelen
= (family
== AF_INET
? 4 : 16);
1545 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1546 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1548 if ((cmd
== RTM_NEWROUTE
) &&
1549 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1550 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1552 req
.n
.nlmsg_type
= cmd
;
1554 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1556 req
.r
.rtm_family
= family
;
1557 req
.r
.rtm_dst_len
= p
->prefixlen
;
1558 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1559 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1561 if (cmd
== RTM_DELROUTE
)
1562 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1564 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1567 * blackhole routes are not RTN_UNICAST, they are
1568 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1569 * so setting this value as a RTN_UNICAST would
1570 * cause the route lookup of just the prefix
1571 * to fail. So no need to specify this for
1572 * the RTM_DELROUTE case
1574 if (cmd
!= RTM_DELROUTE
)
1575 req
.r
.rtm_type
= RTN_UNICAST
;
1577 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1579 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1583 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1585 * either by the kernel or by zebra. Its purely for calculating best
1587 * by the routing protocol and for communicating with protocol peers.
1589 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1591 #if defined(SUPPORT_REALMS)
1595 if (cmd
== RTM_DELROUTE
)
1596 tag
= dplane_ctx_get_old_tag(ctx
);
1598 tag
= dplane_ctx_get_tag(ctx
);
1600 if (tag
> 0 && tag
<= 255)
1601 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1604 /* Table corresponding to this route. */
1605 table_id
= dplane_ctx_get_table(ctx
);
1607 req
.r
.rtm_table
= table_id
;
1609 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1610 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1613 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1616 * If we are not updating the route and we have received
1617 * a route delete, then all we need to fill in is the
1618 * prefix information to tell the kernel to schwack
1621 if (cmd
== RTM_DELROUTE
)
1624 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1625 char buf
[NL_PKT_BUF_SIZE
];
1626 struct rtattr
*rta
= (void *)buf
;
1627 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1628 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1630 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1632 rta
->rta_type
= RTA_METRICS
;
1633 rta
->rta_len
= RTA_LENGTH(0);
1634 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1635 RTAX_MTU
, &mtu
, sizeof(mtu
));
1636 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1640 /* Count overall nexthops so we can decide whether to use singlepath
1641 * or multipath case.
1644 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1645 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1647 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1653 /* Singlepath case. */
1654 if (nexthop_num
== 1) {
1656 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1658 * So we want to cover 2 types of blackhole
1660 * 1) A normal blackhole route( ala from a static
1662 * 2) A recursively resolved blackhole route
1664 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1665 switch (nexthop
->bh_type
) {
1666 case BLACKHOLE_ADMINPROHIB
:
1667 req
.r
.rtm_type
= RTN_PROHIBIT
;
1669 case BLACKHOLE_REJECT
:
1670 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1673 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1678 if (CHECK_FLAG(nexthop
->flags
,
1679 NEXTHOP_FLAG_RECURSIVE
)) {
1684 if (family
== AF_INET
) {
1685 if (nexthop
->rmap_src
.ipv4
.s_addr
1688 nexthop
->rmap_src
.ipv4
;
1690 } else if (nexthop
->src
.ipv4
.s_addr
1696 } else if (family
== AF_INET6
) {
1697 if (!IN6_IS_ADDR_UNSPECIFIED(
1698 &nexthop
->rmap_src
.ipv6
)) {
1700 nexthop
->rmap_src
.ipv6
;
1703 !IN6_IS_ADDR_UNSPECIFIED(
1704 &nexthop
->src
.ipv6
)) {
1713 if ((cmd
== RTM_NEWROUTE
1714 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1715 routedesc
= nexthop
->rparent
1716 ? "recursive, single-path"
1719 _netlink_route_build_singlepath(
1720 routedesc
, bytelen
, nexthop
, &req
.n
,
1721 &req
.r
, sizeof(req
), cmd
);
1726 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1727 if (family
== AF_INET
)
1728 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1729 &src
.ipv4
, bytelen
);
1730 else if (family
== AF_INET6
)
1731 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1732 &src
.ipv6
, bytelen
);
1734 } else { /* Multipath case */
1735 char buf
[NL_PKT_BUF_SIZE
];
1736 struct rtattr
*rta
= (void *)buf
;
1737 struct rtnexthop
*rtnh
;
1738 const union g_addr
*src1
= NULL
;
1740 rta
->rta_type
= RTA_MULTIPATH
;
1741 rta
->rta_len
= RTA_LENGTH(0);
1742 rtnh
= RTA_DATA(rta
);
1745 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1746 if (CHECK_FLAG(nexthop
->flags
,
1747 NEXTHOP_FLAG_RECURSIVE
)) {
1748 /* This only works for IPv4 now */
1752 if (family
== AF_INET
) {
1753 if (nexthop
->rmap_src
.ipv4
.s_addr
1756 nexthop
->rmap_src
.ipv4
;
1758 } else if (nexthop
->src
.ipv4
.s_addr
1764 } else if (family
== AF_INET6
) {
1765 if (!IN6_IS_ADDR_UNSPECIFIED(
1766 &nexthop
->rmap_src
.ipv6
)) {
1768 nexthop
->rmap_src
.ipv6
;
1771 !IN6_IS_ADDR_UNSPECIFIED(
1772 &nexthop
->src
.ipv6
)) {
1782 if ((cmd
== RTM_NEWROUTE
1783 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1784 routedesc
= nexthop
->rparent
1785 ? "recursive, multipath"
1789 _netlink_route_build_multipath(
1790 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1792 rtnh
= RTNH_NEXT(rtnh
);
1794 if (!setsrc
&& src1
) {
1795 if (family
== AF_INET
)
1796 src
.ipv4
= src1
->ipv4
;
1797 else if (family
== AF_INET6
)
1798 src
.ipv6
= src1
->ipv6
;
1804 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1805 if (family
== AF_INET
)
1806 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1807 &src
.ipv4
, bytelen
);
1808 else if (family
== AF_INET6
)
1809 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1810 &src
.ipv6
, bytelen
);
1811 if (IS_ZEBRA_DEBUG_KERNEL
)
1812 zlog_debug("Setting source");
1815 if (rta
->rta_len
> RTA_LENGTH(0))
1816 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1817 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1820 /* If there is no useful nexthop then return. */
1821 if (nexthop_num
== 0) {
1822 if (IS_ZEBRA_DEBUG_KERNEL
)
1824 "netlink_route_multipath(): No useful nexthop.");
1829 /* Talk to netlink socket. */
1830 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1831 dplane_ctx_get_ns(ctx
), 0);
1834 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1836 uint32_t actual_table
;
1838 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1846 struct zebra_ns
*zns
;
1849 memset(&req
, 0, sizeof(req
));
1851 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1852 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1853 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1855 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1856 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1858 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1859 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1860 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1861 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1865 * So during the namespace cleanup we started storing
1866 * the zvrf table_id for the default table as RT_TABLE_MAIN
1867 * which is what the normal routing table for ip routing is.
1868 * This change caused this to break our lookups of sg data
1869 * because prior to this change the zvrf->table_id was 0
1870 * and when the pim multicast kernel code saw a 0,
1871 * it was auto-translated to RT_TABLE_DEFAULT. But since
1872 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1873 * and the kernel goes screw you and the delicious cookies you
1874 * are trying to give me. So now we have this little hack.
1876 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1878 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1880 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1881 &zns
->netlink_cmd
, zns
, 0);
1888 * _netlink_nexthop_build_group() - Build a nexthop_grp struct for a nlmsg
1890 * @n: Netlink message header struct
1891 * @req_size: Size allocated for this message
1892 * @depends_info: Array of depend_info structs
1893 * @count: How many depencies there are
1895 static void _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
1896 const struct depend_info
*depends_info
,
1897 const uint8_t count
)
1899 struct nexthop_grp grp
[count
];
1901 memset(grp
, 0, sizeof(grp
));
1904 for (int i
= 0; i
< count
; i
++) {
1905 grp
[i
].id
= depends_info
[i
].id
;
1906 grp
[i
].weight
= depends_info
[i
].weight
;
1908 addattr_l(n
, req_size
, NHA_GROUP
, grp
, count
* sizeof(*grp
));
1913 * netlink_nexthop() - Nexthop change via the netlink interface
1915 * @ctx: Dataplane ctx
1917 * Return: Result status
1919 static int netlink_nexthop(int cmd
, struct zebra_dplane_ctx
*ctx
)
1924 char buf
[NL_PKT_BUF_SIZE
];
1927 memset(&req
, 0, sizeof(req
));
1929 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
1930 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1931 req
.n
.nlmsg_type
= cmd
;
1932 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1934 req
.nhm
.nh_family
= AF_UNSPEC
;
1937 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
1941 EC_ZEBRA_NHG_FIB_UPDATE
,
1942 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1946 addattr32(&req
.n
, sizeof(req
), NHA_ID
, id
);
1948 if (cmd
== RTM_NEWNEXTHOP
) {
1949 if (dplane_ctx_get_nhe_depends_count(ctx
))
1950 _netlink_nexthop_build_group(
1951 &req
.n
, sizeof(req
),
1952 dplane_ctx_get_nhe_depends_info(ctx
),
1953 dplane_ctx_get_nhe_depends_count(ctx
));
1955 const struct nexthop
*nh
=
1956 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
1957 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
1960 req
.nhm
.nh_family
= AF_INET
;
1961 else if (afi
== AFI_IP6
)
1962 req
.nhm
.nh_family
= AF_INET6
;
1965 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1966 addattr_l(&req
.n
, sizeof(req
), NHA_GATEWAY
,
1967 &nh
->gate
.ipv4
, IPV4_MAX_BYTELEN
);
1969 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1970 addattr_l(&req
.n
, sizeof(req
), NHA_GATEWAY
,
1971 &nh
->gate
.ipv6
, IPV6_MAX_BYTELEN
);
1973 case NEXTHOP_TYPE_BLACKHOLE
:
1974 // TODO: Handle this
1975 addattr_l(&req
.n
, sizeof(req
), NHA_BLACKHOLE
,
1978 case NEXTHOP_TYPE_IFINDEX
:
1979 /* Don't need anymore info for this */
1981 case NEXTHOP_TYPE_IPV4
:
1982 case NEXTHOP_TYPE_IPV6
:
1984 EC_ZEBRA_NHG_FIB_UPDATE
,
1985 "Context received for kernel nexthop update without an interface");
1990 addattr32(&req
.n
, sizeof(req
), NHA_OIF
, nh
->ifindex
);
1991 // TODO: Handle Encap
1994 req
.nhm
.nh_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1996 } else if (cmd
!= RTM_DELNEXTHOP
) {
1998 EC_ZEBRA_NHG_FIB_UPDATE
,
1999 "Nexthop group kernel update command (%d) does not exist",
2004 _netlink_nexthop_debug(cmd
, id
);
2006 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2007 dplane_ctx_get_ns(ctx
), 0);
2011 * kernel_nexthop_update() - Update/delete a nexthop from the kernel
2013 * @ctx: Dataplane context
2015 * Return: Dataplane result flag
2017 enum zebra_dplane_result
kernel_nexthop_update(struct zebra_dplane_ctx
*ctx
)
2021 switch (dplane_ctx_get_op(ctx
)) {
2022 case DPLANE_OP_NH_DELETE
:
2023 cmd
= RTM_DELNEXTHOP
;
2025 case DPLANE_OP_NH_INSTALL
:
2026 case DPLANE_OP_NH_UPDATE
:
2027 cmd
= RTM_NEWNEXTHOP
;
2029 case DPLANE_OP_ROUTE_INSTALL
:
2030 case DPLANE_OP_ROUTE_UPDATE
:
2031 case DPLANE_OP_ROUTE_DELETE
:
2032 case DPLANE_OP_ROUTE_NOTIFY
:
2033 case DPLANE_OP_LSP_INSTALL
:
2034 case DPLANE_OP_LSP_UPDATE
:
2035 case DPLANE_OP_LSP_DELETE
:
2036 case DPLANE_OP_LSP_NOTIFY
:
2037 case DPLANE_OP_PW_INSTALL
:
2038 case DPLANE_OP_PW_UNINSTALL
:
2039 case DPLANE_OP_SYS_ROUTE_ADD
:
2040 case DPLANE_OP_SYS_ROUTE_DELETE
:
2041 case DPLANE_OP_ADDR_INSTALL
:
2042 case DPLANE_OP_ADDR_UNINSTALL
:
2043 case DPLANE_OP_MAC_INSTALL
:
2044 case DPLANE_OP_MAC_DELETE
:
2045 case DPLANE_OP_NONE
:
2047 EC_ZEBRA_NHG_FIB_UPDATE
,
2048 "Context received for kernel nexthop update with incorrect OP code (%u)",
2049 dplane_ctx_get_op(ctx
));
2050 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2054 ret
= netlink_nexthop(cmd
, ctx
);
2056 return (ret
== 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2057 : ZEBRA_DPLANE_REQUEST_FAILURE
);
2061 * Update or delete a prefix from the kernel,
2062 * using info from a dataplane context.
2064 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
2067 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2068 struct nexthop
*nexthop
;
2070 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2072 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2074 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2076 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2077 /* Single 'replace' operation */
2081 * With route replace semantics in place
2082 * for v4 routes and the new route is a system
2083 * route we do not install anything.
2084 * The problem here is that the new system
2085 * route should cause us to withdraw from
2086 * the kernel the old non-system route
2088 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)) &&
2089 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2090 (void)netlink_route_multipath(RTM_DELROUTE
,
2094 * So v6 route replace semantics are not in
2095 * the kernel at this point as I understand it.
2096 * so let's do a delete then an add.
2097 * In the future once v6 route replace semantics
2098 * are in we can figure out what to do here to
2099 * allow working with old and new kernels.
2101 * I'm also intentionally ignoring the failure case
2102 * of the route delete. If that happens yeah we're
2105 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2106 (void)netlink_route_multipath(RTM_DELROUTE
,
2112 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2115 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2116 ret
= netlink_route_multipath(cmd
, ctx
);
2119 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
2120 /* Update installed nexthops to signal which have been
2123 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2124 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2127 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2128 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2134 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2138 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2140 * @tb: Netlink RTA data
2141 * @family: Address family in the nhmsg
2142 * @ifp: Interface connected - this should be NULL, we fill it in
2143 * @ns_id: Namspace id
2145 * Return: New nexthop
2147 static struct nexthop
*netlink_nexthop_process_nh(struct rtattr
**tb
,
2148 unsigned char family
,
2149 struct interface
**ifp
,
2152 struct nexthop
*nh
= NULL
;
2154 enum nexthop_types_t type
= 0;
2158 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2161 if (tb
[NHA_GATEWAY
]) {
2164 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2168 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2173 EC_ZEBRA_BAD_NHG_MESSAGE
,
2174 "Nexthop gateway with bad address family (%d) received from kernel",
2178 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2180 type
= NEXTHOP_TYPE_IFINDEX
;
2183 /* Allocate the new nexthop */
2190 memcpy(&(nh
->gate
), gate
, sz
);
2193 nh
->ifindex
= if_index
;
2195 *ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
->ifindex
);
2197 nh
->vrf_id
= (*ifp
)->vrf_id
;
2200 EC_ZEBRA_UNKNOWN_INTERFACE
,
2201 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2202 __PRETTY_FUNCTION__
, nh
->ifindex
);
2204 nh
->vrf_id
= VRF_DEFAULT
;
2207 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2208 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2210 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2212 if (encap_type
== LWTUNNEL_ENCAP_MPLS
) {
2213 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2217 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
, num_labels
,
2226 * netlink_nexthop_process_group() - Iterate over nhmsg nexthop group
2228 * @tb: Netlink RTA data
2229 * @nhg_depends: Tree head of nexthops in the group
2230 * @nhg: Nexthop group struct
2232 * Return: Count of nexthops in the group
2234 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2235 struct nexthop_group
*nhg
,
2236 struct nhg_connected_head
*nhg_depends
)
2239 struct nexthop_grp
*n_grp
= NULL
;
2240 struct nhg_hash_entry
*depend
= NULL
;
2242 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2243 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2245 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2246 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2247 "Invalid nexthop group received from the kernel");
2251 // TODO: Need type for something?
2252 zlog_debug("Nexthop group type: %d",
2253 *((uint16_t *)RTA_DATA(tb
[NHA_GROUP_TYPE
])));
2255 nhg_connected_head_init(nhg_depends
);
2257 for (int i
= 0; i
< count
; i
++) {
2258 /* We do not care about nexthop_grp.weight at
2259 * this time. But we should figure out
2260 * how to adapt this to our code in
2263 depend
= zebra_nhg_lookup_id(n_grp
[i
].id
);
2265 nhg_connected_head_add(nhg_depends
, depend
);
2267 * If this is a nexthop with its own group
2268 * dependencies, add them as well. Not sure its
2269 * even possible to have a group within a group
2273 copy_nexthops(&nhg
->nexthop
, depend
->nhg
->nexthop
,
2278 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
2286 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2288 * @h: Netlink message header
2289 * @ns_id: Namspace id
2290 * @startup: Are we reading under startup conditions?
2292 * Return: Result status
2294 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2297 /* nexthop group id */
2299 unsigned char family
;
2300 afi_t afi
= AFI_UNSPEC
;
2301 vrf_id_t vrf_id
= VRF_DEFAULT
;
2302 struct interface
*ifp
= NULL
;
2303 struct nhmsg
*nhm
= NULL
;
2304 /* struct for nexthop group abstraction */
2305 struct nexthop_group
*nhg
= NULL
;
2306 struct nexthop
*nh
= NULL
;
2307 /* If its a group, tree head of nexthops */
2308 struct nhg_connected_head nhg_depends
= {0};
2309 /* Count of nexthops in group array */
2311 /* struct that goes into our tables */
2312 struct nhg_hash_entry
*nhe
= NULL
;
2313 struct rtattr
*tb
[NHA_MAX
+ 1];
2316 nhm
= NLMSG_DATA(h
);
2318 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2321 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2324 "%s: Message received from netlink is of a broken size %d %zu",
2325 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2326 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2330 memset(tb
, 0, sizeof(tb
));
2331 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2336 EC_ZEBRA_BAD_NHG_MESSAGE
,
2337 "Nexthop group without an ID received from the kernel");
2341 /* We use the ID key'd nhg table for kernel updates */
2342 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2344 family
= nhm
->nh_family
;
2346 afi
= family2afi(family
);
2348 if (IS_ZEBRA_DEBUG_KERNEL
)
2349 zlog_debug("%s ID (%u) %s NS %u",
2350 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2351 nl_family_to_str(family
), ns_id
);
2354 nhe
= zebra_nhg_lookup_id(id
);
2356 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2357 nhg
= nexthop_group_new();
2359 if (tb
[NHA_GROUP
]) {
2361 * If this is a group message its only going to have
2362 * an array of nexthop IDs associated with it
2364 dep_count
= netlink_nexthop_process_group(tb
, nhg
,
2367 if (tb
[NHA_BLACKHOLE
]) {
2369 * This nexthop is just for blackhole-ing
2370 * traffic, it should not have an OIF, GATEWAY,
2374 nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
2375 nh
->bh_type
= BLACKHOLE_UNSPEC
;
2376 } else if (tb
[NHA_OIF
]) {
2378 * This is a true new nexthop, so we need
2379 * to parse the gateway and device info
2381 nh
= netlink_nexthop_process_nh(tb
, family
,
2385 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2386 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2388 NEXTHOP_FLAG_ONLINK
);
2389 vrf_id
= nh
->vrf_id
;
2390 nexthop_group_add_sorted(nhg
, nh
);
2393 EC_ZEBRA_BAD_NHG_MESSAGE
,
2394 "Invalid Nexthop message received from the kernel with ID (%u)",
2400 if (!nhg
->nexthop
) {
2401 /* Nothing to lookup */
2402 zebra_nhg_free_group_depends(&nhg
, &nhg_depends
);
2407 // TODO: Apparently we don't want changes
2408 // to already created one in our table.
2409 // They should be immutable...
2410 // Gotta figure that one out.
2412 /* This is a change to a group we already have
2415 zebra_nhg_set_invalid(nhe
);
2416 zebra_nhg_free_group_depends(&nhg
, &nhg_depends
);
2419 /* This is a new nexthop group */
2420 nhe
= zebra_nhg_find(nhg
, vrf_id
, afi
, id
, &nhg_depends
,
2422 nexthop_group_free_delete(&nhg
);
2426 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2427 "Zebra failed to find or create a nexthop hash entry for ID (%u) from the kernel",
2432 nhe
->is_kernel_nh
= true;
2434 if (id
!= nhe
->id
) {
2435 /* Duplicate but with different ID from
2438 /* The kernel allows duplicate nexthops
2439 * as long as they have different IDs.
2440 * We are ignoring those to prevent
2441 * syncing problems with the kernel
2445 EC_ZEBRA_DUPLICATE_NHG_MESSAGE
,
2446 "Nexthop Group from kernel with ID (%d) is a duplicate, ignoring",
2448 nhg_connected_head_free(&nhg_depends
);
2450 /* Add the nhe to the interface's tree
2451 * of connected nhe's
2454 zebra_nhg_set_if(nhe
, ifp
);
2456 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
2457 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
2461 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
) {
2464 EC_ZEBRA_BAD_NHG_MESSAGE
,
2465 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
2470 zebra_nhg_set_invalid(nhe
);
2472 // TODO: Probably won't need this if we expect
2473 // upper level protocol to fix it.
2477 "Kernel deleted a nexthop group with ID (%u) that we are still using for a route, sending it back down",
2479 zebra_nhg_install_kernel(nhe
);
2481 zebra_nhg_release(nhe
);
2489 * netlink_request_nexthop() - Request nextop information from the kernel
2490 * @zns: Zebra namespace
2491 * @family: AF_* netlink family
2492 * @type: RTM_* route type
2494 * Return: Result status
2496 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2503 /* Form the request, specifying filter (rtattr) if needed. */
2504 memset(&req
, 0, sizeof(req
));
2505 req
.n
.nlmsg_type
= type
;
2506 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2507 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2508 req
.nhm
.nh_family
= family
;
2510 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2514 * netlink_nexthop_read() - Nexthop read function using netlink interface
2516 * @zns: Zebra name space
2518 * Return: Result status
2519 * Only called at bootstrap time.
2521 int netlink_nexthop_read(struct zebra_ns
*zns
)
2524 struct zebra_dplane_info dp_info
;
2526 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2528 /* Get nexthop objects */
2529 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2532 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2538 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2539 int llalen
, ns_id_t ns_id
)
2541 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2542 addr
, lla
, llalen
, ns_id
);
2546 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2547 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2549 static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2552 uint8_t protocol
= RTPROT_ZEBRA
;
2558 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2559 const struct ipaddr
*addr
;
2561 memset(&req
, 0, sizeof(req
));
2563 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2564 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2565 if (cmd
== RTM_NEWNEIGH
)
2566 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2567 req
.n
.nlmsg_type
= cmd
;
2568 req
.ndm
.ndm_family
= PF_BRIDGE
;
2569 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2570 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
2573 addattr_l(&req
.n
, sizeof(req
),
2574 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2575 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2576 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2578 addr
= dplane_ctx_neigh_get_ipaddr(ctx
);
2580 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &(addr
->ipaddr_v4
), 4);
2582 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2583 dplane_ctx_get_ns(ctx
), 0);
2587 #define NDA_RTA(r) \
2588 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2591 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2594 struct interface
*ifp
;
2595 struct zebra_if
*zif
;
2596 struct rtattr
*tb
[NDA_MAX
+ 1];
2597 struct interface
*br_if
;
2600 struct prefix vtep_ip
;
2601 int vid_present
= 0, dst_present
= 0;
2602 char buf
[ETHER_ADDR_STRLEN
];
2607 ndm
= NLMSG_DATA(h
);
2609 /* We only process macfdb notifications if EVPN is enabled */
2610 if (!is_evpn_enabled())
2613 /* The interface should exist. */
2614 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2616 if (!ifp
|| !ifp
->info
) {
2617 if (IS_ZEBRA_DEBUG_KERNEL
)
2618 zlog_debug("\t%s without associated interface: %u",
2619 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
2623 /* The interface should be something we're interested in. */
2624 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
2625 if (IS_ZEBRA_DEBUG_KERNEL
)
2626 zlog_debug("\t%s Not interested in %s, not a slave",
2627 __PRETTY_FUNCTION__
, ifp
->name
);
2631 /* Drop "permanent" entries. */
2632 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2633 if (IS_ZEBRA_DEBUG_KERNEL
)
2634 zlog_debug("\t%s Entry is PERMANENT, dropping",
2635 __PRETTY_FUNCTION__
);
2639 zif
= (struct zebra_if
*)ifp
->info
;
2640 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2641 if (IS_ZEBRA_DEBUG_KERNEL
)
2643 "%s family %s IF %s(%u) brIF %u - no bridge master",
2644 nl_msg_type_to_str(h
->nlmsg_type
),
2645 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2647 zif
->brslave_info
.bridge_ifindex
);
2651 /* Parse attributes and extract fields of interest. */
2652 memset(tb
, 0, sizeof tb
);
2653 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2655 if (!tb
[NDA_LLADDR
]) {
2656 if (IS_ZEBRA_DEBUG_KERNEL
)
2657 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2658 nl_msg_type_to_str(h
->nlmsg_type
),
2659 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2661 zif
->brslave_info
.bridge_ifindex
);
2665 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2666 if (IS_ZEBRA_DEBUG_KERNEL
)
2668 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2669 nl_msg_type_to_str(h
->nlmsg_type
),
2670 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2672 zif
->brslave_info
.bridge_ifindex
,
2673 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2677 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2679 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2681 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2682 sprintf(vid_buf
, " VLAN %u", vid
);
2686 /* TODO: Only IPv4 supported now. */
2688 vtep_ip
.family
= AF_INET
;
2689 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2690 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2692 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2695 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2697 if (IS_ZEBRA_DEBUG_KERNEL
)
2698 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2699 nl_msg_type_to_str(h
->nlmsg_type
),
2700 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2701 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2702 sticky
? "sticky " : "",
2703 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2704 dst_present
? dst_buf
: "");
2706 if (filter_vlan
&& vid
!= filter_vlan
) {
2707 if (IS_ZEBRA_DEBUG_KERNEL
)
2708 zlog_debug("\tFiltered due to filter vlan: %d",
2713 /* If add or update, do accordingly if learnt on a "local" interface; if
2714 * the notification is over VxLAN, this has to be related to
2716 * so perform an implicit delete of any local entry (if it exists).
2718 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2719 if (IS_ZEBRA_IF_VXLAN(ifp
))
2720 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2723 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2727 /* This is a delete notification.
2728 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2729 * 2. For a MAC over "local" interface, delete the mac
2730 * Note: We will get notifications from both bridge driver and VxLAN
2732 * Ignore the notification from VxLan driver as it is also generated
2733 * when mac moves from remote to local.
2736 if (IS_ZEBRA_DEBUG_KERNEL
)
2737 zlog_debug("\tNo Destination Present");
2741 if (IS_ZEBRA_IF_VXLAN(ifp
))
2742 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2745 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2748 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2753 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2756 /* Length validity. */
2757 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2761 /* We are interested only in AF_BRIDGE notifications. */
2762 ndm
= NLMSG_DATA(h
);
2763 if (ndm
->ndm_family
!= AF_BRIDGE
)
2766 return netlink_macfdb_change(h
, len
, ns_id
);
2769 /* Request for MAC FDB information from the kernel */
2770 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2771 int type
, ifindex_t master_ifindex
)
2775 struct ifinfomsg ifm
;
2779 /* Form the request, specifying filter (rtattr) if needed. */
2780 memset(&req
, 0, sizeof(req
));
2781 req
.n
.nlmsg_type
= type
;
2782 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2783 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2784 req
.ifm
.ifi_family
= family
;
2786 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2788 return netlink_request(netlink_cmd
, &req
.n
);
2792 * MAC forwarding database read using netlink interface. This is invoked
2795 int netlink_macfdb_read(struct zebra_ns
*zns
)
2798 struct zebra_dplane_info dp_info
;
2800 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2802 /* Get bridge FDB table. */
2803 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2807 /* We are reading entire table. */
2809 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2816 * MAC forwarding database read using netlink interface. This is for a
2817 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2819 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2820 struct interface
*br_if
)
2822 struct zebra_if
*br_zif
;
2823 struct zebra_if
*zif
;
2824 struct zebra_l2info_vxlan
*vxl
;
2825 struct zebra_dplane_info dp_info
;
2828 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2830 /* Save VLAN we're filtering on, if needed. */
2831 br_zif
= (struct zebra_if
*)br_if
->info
;
2832 zif
= (struct zebra_if
*)ifp
->info
;
2833 vxl
= &zif
->l2info
.vxl
;
2834 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2835 filter_vlan
= vxl
->access_vlan
;
2837 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2839 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2843 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2846 /* Reset VLAN filter. */
2852 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2853 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2856 struct interface
*br_if
,
2857 struct ethaddr
*mac
,
2865 struct zebra_if
*br_zif
;
2866 char buf
[ETHER_ADDR_STRLEN
];
2868 memset(&req
, 0, sizeof(req
));
2869 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2870 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2871 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2872 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2873 /* req.ndm.ndm_state = NUD_REACHABLE; */
2875 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2877 br_zif
= (struct zebra_if
*)br_if
->info
;
2878 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2879 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2881 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2883 if (IS_ZEBRA_DEBUG_KERNEL
)
2884 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2885 __PRETTY_FUNCTION__
,
2886 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2888 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2890 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2893 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2894 struct interface
*br_if
,
2895 struct ethaddr
*mac
, vlanid_t vid
)
2898 struct zebra_dplane_info dp_info
;
2900 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2902 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2904 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2910 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2917 * Netlink-specific handler for MAC updates using dataplane context object.
2919 static enum zebra_dplane_result
2920 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2922 uint8_t protocol
= RTPROT_ZEBRA
;
2930 int vid_present
= 0;
2932 struct in_addr vtep_ip
;
2935 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2940 memset(&req
, 0, sizeof(req
));
2942 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2943 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2944 if (cmd
== RTM_NEWNEIGH
)
2945 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2946 req
.n
.nlmsg_type
= cmd
;
2947 req
.ndm
.ndm_family
= AF_BRIDGE
;
2948 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2949 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2951 if (dplane_ctx_mac_is_sticky(ctx
))
2952 req
.ndm
.ndm_state
|= NUD_NOARP
;
2954 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2956 addattr_l(&req
.n
, sizeof(req
),
2957 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
2958 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2959 dplane_ctx_mac_get_addr(ctx
), 6);
2960 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2962 dst_alen
= 4; // TODO: hardcoded
2963 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2964 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2966 vid
= dplane_ctx_mac_get_vlan(ctx
);
2969 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2972 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
,
2973 dplane_ctx_mac_get_br_ifindex(ctx
));
2975 if (IS_ZEBRA_DEBUG_KERNEL
) {
2976 char ipbuf
[PREFIX_STRLEN
];
2977 char buf
[ETHER_ADDR_STRLEN
];
2978 char dst_buf
[PREFIX_STRLEN
+ 10];
2982 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2986 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2987 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2988 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2990 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2991 nl_msg_type_to_str(cmd
),
2992 nl_family_to_str(req
.ndm
.ndm_family
),
2993 dplane_ctx_get_ifname(ctx
),
2994 dplane_ctx_get_ifindex(ctx
), vid_buf
,
2995 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2999 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
3000 dplane_ctx_get_ns(ctx
), 0);
3002 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
3004 return ZEBRA_DPLANE_REQUEST_FAILURE
;
3008 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3009 * 5549 support, re-install them.
3011 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3012 struct interface
*ifp
, struct ipaddr
*ip
,
3015 if (ndm
->ndm_family
!= AF_INET
)
3018 if (!zif
->v6_2_v4_ll_neigh_entry
)
3021 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3024 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3025 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3030 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3034 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3037 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3040 struct interface
*ifp
;
3041 struct zebra_if
*zif
;
3042 struct rtattr
*tb
[NDA_MAX
+ 1];
3043 struct interface
*link_if
;
3046 char buf
[ETHER_ADDR_STRLEN
];
3047 char buf2
[INET6_ADDRSTRLEN
];
3048 int mac_present
= 0;
3052 ndm
= NLMSG_DATA(h
);
3054 /* The interface should exist. */
3055 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3057 if (!ifp
|| !ifp
->info
)
3060 zif
= (struct zebra_if
*)ifp
->info
;
3062 /* Parse attributes and extract fields of interest. */
3063 memset(tb
, 0, sizeof tb
);
3064 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3067 zlog_debug("%s family %s IF %s(%u) - no DST",
3068 nl_msg_type_to_str(h
->nlmsg_type
),
3069 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3074 memset(&ip
, 0, sizeof(struct ipaddr
));
3075 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3076 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3078 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3079 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3080 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3081 if (IS_ZEBRA_DEBUG_KERNEL
)
3083 "\tNeighbor Entry Received is a 5549 entry, finished");
3087 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3088 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3089 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3091 /* The neighbor is present on an SVI. From this, we locate the
3093 * bridge because we're only interested in neighbors on a VxLAN bridge.
3094 * The bridge is located based on the nature of the SVI:
3095 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3097 * and is linked to the bridge
3098 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3102 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3103 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3107 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3110 if (IS_ZEBRA_DEBUG_KERNEL
)
3112 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3116 memset(&mac
, 0, sizeof(struct ethaddr
));
3117 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3118 if (tb
[NDA_LLADDR
]) {
3119 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3120 if (IS_ZEBRA_DEBUG_KERNEL
)
3122 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3127 ifp
->name
, ndm
->ndm_ifindex
,
3128 (unsigned long)RTA_PAYLOAD(
3134 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3137 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3138 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3140 if (IS_ZEBRA_DEBUG_KERNEL
)
3142 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3143 nl_msg_type_to_str(h
->nlmsg_type
),
3144 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3146 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3148 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3150 ndm
->ndm_state
, ndm
->ndm_flags
);
3152 /* If the neighbor state is valid for use, process as an add or
3154 * else process as a delete. Note that the delete handling may
3156 * in re-adding the neighbor if it is a valid "remote" neighbor.
3158 if (ndm
->ndm_state
& NUD_VALID
)
3159 return zebra_vxlan_handle_kernel_neigh_update(
3160 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3163 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3166 if (IS_ZEBRA_DEBUG_KERNEL
)
3167 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3168 nl_msg_type_to_str(h
->nlmsg_type
),
3169 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3171 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3173 /* Process the delete - it may result in re-adding the neighbor if it is
3174 * a valid "remote" neighbor.
3176 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3179 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3184 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3187 /* Length validity. */
3188 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3192 /* We are interested only in AF_INET or AF_INET6 notifications. */
3193 ndm
= NLMSG_DATA(h
);
3194 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3197 return netlink_neigh_change(h
, len
);
3200 /* Request for IP neighbor information from the kernel */
3201 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3202 int type
, ifindex_t ifindex
)
3210 /* Form the request, specifying filter (rtattr) if needed. */
3211 memset(&req
, 0, sizeof(req
));
3212 req
.n
.nlmsg_type
= type
;
3213 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3214 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3215 req
.ndm
.ndm_family
= family
;
3217 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3219 return netlink_request(netlink_cmd
, &req
.n
);
3223 * IP Neighbor table read using netlink interface. This is invoked
3226 int netlink_neigh_read(struct zebra_ns
*zns
)
3229 struct zebra_dplane_info dp_info
;
3231 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3233 /* Get IP neighbor table. */
3234 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3238 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3245 * IP Neighbor table read using netlink interface. This is for a specific
3248 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3251 struct zebra_dplane_info dp_info
;
3253 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3255 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3259 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3266 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3267 * read using netlink interface.
3269 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3270 int type
, struct ipaddr
*ip
,
3280 /* Form the request, specifying filter (rtattr) if needed. */
3281 memset(&req
, 0, sizeof(req
));
3282 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3283 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3284 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3285 req
.ndm
.ndm_ifindex
= ifindex
;
3287 if (IS_IPADDR_V4(ip
)) {
3288 ipa_len
= IPV4_MAX_BYTELEN
;
3289 req
.ndm
.ndm_family
= AF_INET
;
3292 ipa_len
= IPV6_MAX_BYTELEN
;
3293 req
.ndm
.ndm_family
= AF_INET6
;
3296 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3298 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
3301 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3302 struct interface
*vlan_if
)
3305 struct zebra_ns
*zns
;
3306 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3307 char buf
[INET6_ADDRSTRLEN
];
3308 struct zebra_dplane_info dp_info
;
3312 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3314 if (IS_ZEBRA_DEBUG_KERNEL
)
3315 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3316 __PRETTY_FUNCTION__
, vlan_if
->name
,
3318 ipaddr2str(ip
, buf
, sizeof(buf
)),
3321 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3326 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3332 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3337 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3340 /* Length validity. */
3341 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3343 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3344 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
3345 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3349 /* Is this a notification for the MAC FDB or IP neighbor table? */
3350 ndm
= NLMSG_DATA(h
);
3351 if (ndm
->ndm_family
== AF_BRIDGE
)
3352 return netlink_macfdb_change(h
, len
, ns_id
);
3354 if (ndm
->ndm_type
!= RTN_UNICAST
)
3357 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3358 return netlink_ipneigh_change(h
, len
, ns_id
);
3361 EC_ZEBRA_UNKNOWN_FAMILY
,
3362 "Invalid address family: %u received from kernel neighbor change: %s",
3363 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3371 * Utility neighbor-update function, using info from dplane context.
3373 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3376 uint8_t protocol
= RTPROT_ZEBRA
;
3383 char buf
[INET6_ADDRSTRLEN
];
3384 char buf2
[ETHER_ADDR_STRLEN
];
3385 const struct ipaddr
*ip
;
3386 const struct ethaddr
*mac
;
3390 memset(&req
, 0, sizeof(req
));
3392 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3393 mac
= dplane_ctx_neigh_get_mac(ctx
);
3394 if (is_zero_mac(mac
))
3397 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3398 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3400 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3401 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3402 if (cmd
== RTM_NEWNEIGH
)
3403 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3404 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
3405 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3406 req
.ndm
.ndm_state
= state
;
3407 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3408 req
.ndm
.ndm_type
= RTN_UNICAST
;
3409 req
.ndm
.ndm_flags
= flags
;
3411 addattr_l(&req
.n
, sizeof(req
),
3412 NDA_PROTOCOL
, &protocol
, sizeof(protocol
));
3413 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3414 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3416 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3418 if (IS_ZEBRA_DEBUG_KERNEL
)
3419 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3420 nl_msg_type_to_str(cmd
),
3421 nl_family_to_str(req
.ndm
.ndm_family
),
3422 dplane_ctx_get_ifname(ctx
),
3423 dplane_ctx_get_ifindex(ctx
),
3424 ipaddr2str(ip
, buf
, sizeof(buf
)),
3425 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
3429 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3430 dplane_ctx_get_ns(ctx
), 0);
3434 * Update MAC, using dataplane context object.
3436 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
3438 return netlink_macfdb_update_ctx(ctx
);
3441 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
3445 switch (dplane_ctx_get_op(ctx
)) {
3446 case DPLANE_OP_NEIGH_INSTALL
:
3447 case DPLANE_OP_NEIGH_UPDATE
:
3448 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
3450 case DPLANE_OP_NEIGH_DELETE
:
3451 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
3453 case DPLANE_OP_VTEP_ADD
:
3454 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
);
3456 case DPLANE_OP_VTEP_DELETE
:
3457 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
);
3464 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
3468 * MPLS label forwarding table change via netlink interface, using dataplane
3469 * context information.
3471 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
3474 const zebra_nhlfe_t
*nhlfe
;
3475 struct nexthop
*nexthop
= NULL
;
3476 unsigned int nexthop_num
;
3477 const char *routedesc
;
3483 char buf
[NL_PKT_BUF_SIZE
];
3486 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
3489 * Count # nexthops so we can decide whether to use singlepath
3490 * or multipath case.
3493 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
3494 nexthop
= nhlfe
->nexthop
;
3497 if (cmd
== RTM_NEWROUTE
) {
3498 /* Count all selected NHLFEs */
3499 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3500 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3503 /* Count all installed NHLFEs */
3504 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3505 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3510 if ((nexthop_num
== 0) ||
3511 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3514 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3515 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3516 req
.n
.nlmsg_type
= cmd
;
3517 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3519 req
.r
.rtm_family
= AF_MPLS
;
3520 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3521 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3522 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3523 req
.r
.rtm_type
= RTN_UNICAST
;
3525 if (cmd
== RTM_NEWROUTE
) {
3526 /* We do a replace to handle update. */
3527 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3529 /* set the protocol value if installing */
3530 route_type
= re_type_from_lsp_type(
3531 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3532 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3535 /* Fill destination */
3536 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3537 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3539 /* Fill nexthops (paths) based on single-path or multipath. The paths
3540 * chosen depend on the operation.
3542 if (nexthop_num
== 1) {
3543 routedesc
= "single-path";
3544 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3548 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3549 nhlfe
; nhlfe
= nhlfe
->next
) {
3550 nexthop
= nhlfe
->nexthop
;
3554 if ((cmd
== RTM_NEWROUTE
3555 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3556 && CHECK_FLAG(nexthop
->flags
,
3557 NEXTHOP_FLAG_ACTIVE
)))
3558 || (cmd
== RTM_DELROUTE
3559 && (CHECK_FLAG(nhlfe
->flags
,
3560 NHLFE_FLAG_INSTALLED
)
3561 && CHECK_FLAG(nexthop
->flags
,
3562 NEXTHOP_FLAG_FIB
)))) {
3563 /* Add the gateway */
3564 _netlink_mpls_build_singlepath(
3573 } else { /* Multipath case */
3574 char buf
[NL_PKT_BUF_SIZE
];
3575 struct rtattr
*rta
= (void *)buf
;
3576 struct rtnexthop
*rtnh
;
3577 const union g_addr
*src1
= NULL
;
3579 rta
->rta_type
= RTA_MULTIPATH
;
3580 rta
->rta_len
= RTA_LENGTH(0);
3581 rtnh
= RTA_DATA(rta
);
3583 routedesc
= "multipath";
3584 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3588 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3589 nhlfe
; nhlfe
= nhlfe
->next
) {
3590 nexthop
= nhlfe
->nexthop
;
3594 if ((cmd
== RTM_NEWROUTE
3595 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3596 && CHECK_FLAG(nexthop
->flags
,
3597 NEXTHOP_FLAG_ACTIVE
)))
3598 || (cmd
== RTM_DELROUTE
3599 && (CHECK_FLAG(nhlfe
->flags
,
3600 NHLFE_FLAG_INSTALLED
)
3601 && CHECK_FLAG(nexthop
->flags
,
3602 NEXTHOP_FLAG_FIB
)))) {
3605 /* Build the multipath */
3606 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3609 rtnh
= RTNH_NEXT(rtnh
);
3613 /* Add the multipath */
3614 if (rta
->rta_len
> RTA_LENGTH(0))
3615 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3616 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3619 /* Talk to netlink socket. */
3620 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3621 dplane_ctx_get_ns(ctx
), 0);
3623 #endif /* HAVE_NETLINK */