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>
31 /* Hack for GNU libc version 2. */
33 #define MSG_TRUNC 0x20
34 #endif /* MSG_TRUNC */
40 #include "connected.h"
43 #include "zebra_memory.h"
53 #include "zebra/zapi_msg.h"
54 #include "zebra/zebra_ns.h"
55 #include "zebra/zebra_vrf.h"
57 #include "zebra/redistribute.h"
58 #include "zebra/interface.h"
59 #include "zebra/debug.h"
60 #include "zebra/rtadv.h"
61 #include "zebra/zebra_ptm.h"
62 #include "zebra/zebra_mpls.h"
63 #include "zebra/kernel_netlink.h"
64 #include "zebra/rt_netlink.h"
65 #include "zebra/zebra_mroute.h"
66 #include "zebra/zebra_vxlan.h"
67 #include "zebra/zebra_errors.h"
73 static vlanid_t filter_vlan
= 0;
81 char ipv4_ll_buf
[16] = "169.254.0.1";
82 struct in_addr ipv4_ll
;
85 * The ipv4_ll data structure is used for all 5549
86 * additions to the kernel. Let's figure out the
87 * correct value one time instead for every
88 * install/remove of a 5549 type route
90 void rt_netlink_init(void)
92 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
96 * Mapping from dataplane neighbor flags to netlink flags
98 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
102 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
103 flags
|= NTF_EXT_LEARNED
;
104 if (dplane_flags
& DPLANE_NTF_ROUTER
)
111 * Mapping from dataplane neighbor state to netlink state
113 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
117 if (dplane_state
& DPLANE_NUD_REACHABLE
)
118 state
|= NUD_REACHABLE
;
119 if (dplane_state
& DPLANE_NUD_STALE
)
121 if (dplane_state
& DPLANE_NUD_NOARP
)
123 if (dplane_state
& DPLANE_NUD_PROBE
)
130 static inline int is_selfroute(int proto
)
132 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
133 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
134 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
135 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
136 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
137 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
138 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
145 static inline int zebra2proto(int proto
)
148 case ZEBRA_ROUTE_BABEL
:
149 proto
= RTPROT_BABEL
;
151 case ZEBRA_ROUTE_BGP
:
154 case ZEBRA_ROUTE_OSPF
:
155 case ZEBRA_ROUTE_OSPF6
:
158 case ZEBRA_ROUTE_STATIC
:
159 proto
= RTPROT_ZSTATIC
;
161 case ZEBRA_ROUTE_ISIS
:
164 case ZEBRA_ROUTE_RIP
:
167 case ZEBRA_ROUTE_RIPNG
:
168 proto
= RTPROT_RIPNG
;
170 case ZEBRA_ROUTE_NHRP
:
173 case ZEBRA_ROUTE_EIGRP
:
174 proto
= RTPROT_EIGRP
;
176 case ZEBRA_ROUTE_LDP
:
179 case ZEBRA_ROUTE_SHARP
:
180 proto
= RTPROT_SHARP
;
182 case ZEBRA_ROUTE_PBR
:
185 case ZEBRA_ROUTE_OPENFABRIC
:
186 proto
= RTPROT_OPENFABRIC
;
188 case ZEBRA_ROUTE_TABLE
:
189 proto
= RTPROT_ZEBRA
;
193 * When a user adds a new protocol this will show up
194 * to let them know to do something about it. This
195 * is intentionally a warn because we should see
196 * this as part of development of a new protocol
199 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
200 __PRETTY_FUNCTION__
, proto
);
201 proto
= RTPROT_ZEBRA
;
208 static inline int proto2zebra(int proto
, int family
)
212 proto
= ZEBRA_ROUTE_BABEL
;
215 proto
= ZEBRA_ROUTE_BGP
;
218 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
222 proto
= ZEBRA_ROUTE_ISIS
;
225 proto
= ZEBRA_ROUTE_RIP
;
228 proto
= ZEBRA_ROUTE_RIPNG
;
231 proto
= ZEBRA_ROUTE_NHRP
;
234 proto
= ZEBRA_ROUTE_EIGRP
;
237 proto
= ZEBRA_ROUTE_LDP
;
241 proto
= ZEBRA_ROUTE_STATIC
;
244 proto
= ZEBRA_ROUTE_SHARP
;
247 proto
= ZEBRA_ROUTE_PBR
;
249 case RTPROT_OPENFABRIC
:
250 proto
= ZEBRA_ROUTE_OPENFABRIC
;
254 * When a user adds a new protocol this will show up
255 * to let them know to do something about it. This
256 * is intentionally a warn because we should see
257 * this as part of development of a new protocol
260 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
261 __PRETTY_FUNCTION__
, proto
);
262 proto
= ZEBRA_ROUTE_KERNEL
;
269 Pending: create an efficient table_id (in a tree/hash) based lookup)
271 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
274 struct zebra_vrf
*zvrf
;
276 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
280 /* case vrf with netns : match the netnsid */
281 if (vrf_is_backend_netns()) {
282 if (ns_id
== zvrf_id(zvrf
))
283 return zvrf_id(zvrf
);
285 /* VRF is VRF_BACKEND_VRF_LITE */
286 if (zvrf
->table_id
!= table_id
)
288 return zvrf_id(zvrf
);
296 * @parse_encap_mpls() - Parses encapsulated mpls attributes
297 * @tb: Pointer to rtattr to look for nested items in.
298 * @labels: Pointer to store labels in.
300 * Return: Number of mpls labels found.
302 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
304 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
305 mpls_lse_t
*lses
= NULL
;
310 mpls_label_t label
= 0;
312 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
313 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
314 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
315 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
316 labels
[num_labels
++] = label
;
322 /* Looking up routing table by netlink interface. */
323 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
328 struct rtattr
*tb
[RTA_MAX
+ 1];
331 struct prefix_ipv6 src_p
= {};
334 char anyaddr
[16] = {0};
336 int proto
= ZEBRA_ROUTE_KERNEL
;
341 uint8_t distance
= 0;
346 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
347 void *src
= NULL
; /* IPv6 srcdest source prefix */
348 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
351 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
356 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
358 switch (rtm
->rtm_type
) {
362 bh_type
= BLACKHOLE_NULL
;
364 case RTN_UNREACHABLE
:
365 bh_type
= BLACKHOLE_REJECT
;
368 bh_type
= BLACKHOLE_ADMINPROHIB
;
371 if (IS_ZEBRA_DEBUG_KERNEL
)
372 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
373 nl_rttype_to_str(rtm
->rtm_type
),
378 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
380 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
381 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
382 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
386 memset(tb
, 0, sizeof tb
);
387 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
389 if (rtm
->rtm_flags
& RTM_F_CLONED
)
391 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
393 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
396 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
397 && h
->nlmsg_type
== RTM_NEWROUTE
) {
398 if (IS_ZEBRA_DEBUG_KERNEL
)
399 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
404 /* We don't care about change notifications for the MPLS table. */
405 /* TODO: Revisit this. */
406 if (rtm
->rtm_family
== AF_MPLS
)
409 /* Table corresponding to route. */
411 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
413 table
= rtm
->rtm_table
;
416 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
417 if (vrf_id
== VRF_DEFAULT
) {
418 if (!is_zebra_valid_kernel_table(table
)
419 && !is_zebra_main_routing_table(table
))
423 /* Route which inserted by Zebra. */
424 if (is_selfroute(rtm
->rtm_protocol
)) {
425 flags
|= ZEBRA_FLAG_SELFROUTE
;
426 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
429 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
432 dest
= RTA_DATA(tb
[RTA_DST
]);
437 src
= RTA_DATA(tb
[RTA_SRC
]);
442 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
445 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
447 if (tb
[RTA_PRIORITY
])
448 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
450 #if defined(SUPPORT_REALMS)
452 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
455 if (tb
[RTA_METRICS
]) {
456 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
458 memset(mxrta
, 0, sizeof mxrta
);
459 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
460 RTA_PAYLOAD(tb
[RTA_METRICS
]));
463 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
466 if (rtm
->rtm_family
== AF_INET
) {
468 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
470 "Invalid destination prefix length: %u received from kernel route change",
474 memcpy(&p
.u
.prefix4
, dest
, 4);
475 p
.prefixlen
= rtm
->rtm_dst_len
;
477 if (rtm
->rtm_src_len
!= 0) {
478 char buf
[PREFIX_STRLEN
];
480 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
481 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
482 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
486 /* Force debug below to not display anything for source */
488 } else if (rtm
->rtm_family
== AF_INET6
) {
490 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
492 "Invalid destination prefix length: %u received from kernel route change",
496 memcpy(&p
.u
.prefix6
, dest
, 16);
497 p
.prefixlen
= rtm
->rtm_dst_len
;
499 src_p
.family
= AF_INET6
;
500 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
502 "Invalid source prefix length: %u received from kernel route change",
506 memcpy(&src_p
.prefix
, src
, 16);
507 src_p
.prefixlen
= rtm
->rtm_src_len
;
511 * For ZEBRA_ROUTE_KERNEL types:
513 * The metric/priority of the route received from the kernel
514 * is a 32 bit number. We are going to interpret the high
515 * order byte as the Admin Distance and the low order 3 bytes
518 * This will allow us to do two things:
519 * 1) Allow the creation of kernel routes that can be
520 * overridden by zebra.
521 * 2) Allow the old behavior for 'most' kernel route types
522 * if a user enters 'ip route ...' v4 routes get a metric
523 * of 0 and v6 routes get a metric of 1024. Both of these
524 * values will end up with a admin distance of 0, which
525 * will cause them to win for the purposes of zebra.
527 if (proto
== ZEBRA_ROUTE_KERNEL
) {
528 distance
= (metric
>> 24) & 0xFF;
529 metric
= (metric
& 0x00FFFFFF);
532 if (IS_ZEBRA_DEBUG_KERNEL
) {
533 char buf
[PREFIX_STRLEN
];
534 char buf2
[PREFIX_STRLEN
];
535 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
536 nl_msg_type_to_str(h
->nlmsg_type
),
537 prefix2str(&p
, buf
, sizeof(buf
)),
538 src_p
.prefixlen
? " from " : "",
540 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
542 vrf_id
, table
, metric
, distance
);
546 if (rtm
->rtm_family
== AF_INET6
)
549 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
550 struct interface
*ifp
;
551 vrf_id_t nh_vrf_id
= vrf_id
;
553 if (!tb
[RTA_MULTIPATH
]) {
555 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
557 memset(&nh
, 0, sizeof(nh
));
559 if (bh_type
== BLACKHOLE_UNSPEC
) {
561 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
562 else if (index
&& gate
)
565 ? NEXTHOP_TYPE_IPV4_IFINDEX
566 : NEXTHOP_TYPE_IPV6_IFINDEX
;
567 else if (!index
&& gate
)
568 nh
.type
= (afi
== AFI_IP
)
572 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
573 nh
.bh_type
= bh_type
;
576 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
577 nh
.bh_type
= bh_type
;
581 memcpy(&nh
.src
, prefsrc
, sz
);
583 memcpy(&nh
.gate
, gate
, sz
);
586 ifp
= if_lookup_by_index_per_ns(
587 zebra_ns_lookup(ns_id
),
590 nh_vrf_id
= ifp
->vrf_id
;
592 nh
.vrf_id
= nh_vrf_id
;
594 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
595 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
596 == LWTUNNEL_ENCAP_MPLS
) {
598 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
601 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
602 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
605 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
608 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
609 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
611 /* This is a multipath route */
613 struct route_entry
*re
;
614 struct rtnexthop
*rtnh
=
615 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
617 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
619 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
621 re
->distance
= distance
;
628 re
->uptime
= monotime(NULL
);
632 struct nexthop
*nh
= NULL
;
634 if (len
< (int)sizeof(*rtnh
)
635 || rtnh
->rtnh_len
> len
)
638 index
= rtnh
->rtnh_ifindex
;
641 * Yes we are looking this up
642 * for every nexthop and just
643 * using the last one looked
646 ifp
= if_lookup_by_index_per_ns(
647 zebra_ns_lookup(ns_id
),
650 nh_vrf_id
= ifp
->vrf_id
;
653 EC_ZEBRA_UNKNOWN_INTERFACE
,
654 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
657 nh_vrf_id
= VRF_DEFAULT
;
663 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
664 memset(tb
, 0, sizeof(tb
));
665 netlink_parse_rtattr(
666 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
667 rtnh
->rtnh_len
- sizeof(*rtnh
));
671 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
672 && *(uint16_t *)RTA_DATA(
674 == LWTUNNEL_ENCAP_MPLS
) {
675 num_labels
= parse_encap_mpls(
676 tb
[RTA_ENCAP
], labels
);
681 if (rtm
->rtm_family
== AF_INET
) {
683 nh
= route_entry_nexthop_ipv4_ifindex_add(
688 nh
= route_entry_nexthop_ipv4_add(
692 } else if (rtm
->rtm_family
695 nh
= route_entry_nexthop_ipv6_ifindex_add(
699 nh
= route_entry_nexthop_ipv6_add(
704 nh
= route_entry_nexthop_ifindex_add(
705 re
, index
, nh_vrf_id
);
707 if (nh
&& num_labels
)
708 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
711 if (nh
&& (rtnh
->rtnh_flags
& RTNH_F_ONLINK
))
713 NEXTHOP_FLAG_ONLINK
);
715 if (rtnh
->rtnh_len
== 0)
718 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
719 rtnh
= RTNH_NEXT(rtnh
);
722 zserv_nexthop_num_warn(__func__
,
723 (const struct prefix
*)&p
,
725 if (re
->nexthop_num
== 0)
728 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
732 if (!tb
[RTA_MULTIPATH
]) {
734 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
736 memset(&nh
, 0, sizeof(nh
));
737 if (bh_type
== BLACKHOLE_UNSPEC
) {
739 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
740 else if (index
&& gate
)
743 ? NEXTHOP_TYPE_IPV4_IFINDEX
744 : NEXTHOP_TYPE_IPV6_IFINDEX
;
745 else if (!index
&& gate
)
746 nh
.type
= (afi
== AFI_IP
)
750 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
751 nh
.bh_type
= BLACKHOLE_UNSPEC
;
754 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
755 nh
.bh_type
= bh_type
;
759 memcpy(&nh
.gate
, gate
, sz
);
760 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
761 &p
, &src_p
, &nh
, table
, metric
, distance
,
764 /* XXX: need to compare the entire list of nexthops
765 * here for NLM_F_APPEND stupidity */
766 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
767 &p
, &src_p
, NULL
, table
, metric
, distance
,
775 static struct mcast_route_data
*mroute
= NULL
;
777 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
778 ns_id_t ns_id
, int startup
)
782 struct rtattr
*tb
[RTA_MAX
+ 1];
783 struct mcast_route_data
*m
;
784 struct mcast_route_data mr
;
791 char oif_list
[256] = "\0";
798 memset(&mr
, 0, sizeof(mr
));
804 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
806 memset(tb
, 0, sizeof tb
);
807 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
810 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
812 table
= rtm
->rtm_table
;
814 vrf
= vrf_lookup_by_table(table
, ns_id
);
817 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
820 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
823 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
826 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
828 if (tb
[RTA_MULTIPATH
]) {
829 struct rtnexthop
*rtnh
=
830 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
832 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
834 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
837 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
840 if (rtnh
->rtnh_len
== 0)
843 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
844 rtnh
= RTNH_NEXT(rtnh
);
848 if (IS_ZEBRA_DEBUG_KERNEL
) {
849 struct interface
*ifp
= NULL
;
850 struct zebra_vrf
*zvrf
= NULL
;
852 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
853 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
854 for (count
= 0; count
< oif_count
; count
++) {
855 ifp
= if_lookup_by_index(oif
[count
], vrf
);
858 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
860 strlcat(oif_list
, temp
, sizeof(oif_list
));
862 zvrf
= zebra_vrf_lookup_by_id(vrf
);
863 ifp
= if_lookup_by_index(iif
, vrf
);
865 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
866 (zvrf
? zvrf
->vrf
->name
: "Unknown"), vrf
,
867 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
868 ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
874 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
881 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
882 /* If this is not route add/delete message print warning. */
883 zlog_debug("Kernel message: %s NS %u",
884 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
888 if (!(rtm
->rtm_family
== AF_INET
||
889 rtm
->rtm_family
== AF_INET6
||
890 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
892 EC_ZEBRA_UNKNOWN_FAMILY
,
893 "Invalid address family: %u received from kernel route change: %s",
894 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
898 /* Connected route. */
899 if (IS_ZEBRA_DEBUG_KERNEL
)
900 zlog_debug("%s %s %s proto %s NS %u",
901 nl_msg_type_to_str(h
->nlmsg_type
),
902 nl_family_to_str(rtm
->rtm_family
),
903 nl_rttype_to_str(rtm
->rtm_type
),
904 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
907 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
909 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
912 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
916 if (rtm
->rtm_type
== RTN_MULTICAST
)
917 netlink_route_change_read_multicast(h
, ns_id
, startup
);
919 netlink_route_change_read_unicast(h
, ns_id
, startup
);
923 /* Request for specific route information from the kernel */
924 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
931 /* Form the request, specifying filter (rtattr) if needed. */
932 memset(&req
, 0, sizeof(req
));
933 req
.n
.nlmsg_type
= type
;
934 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
935 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
936 req
.rtm
.rtm_family
= family
;
938 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
941 /* Routing table read function using netlink interface. Only called
943 int netlink_route_read(struct zebra_ns
*zns
)
946 struct zebra_dplane_info dp_info
;
948 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
950 /* Get IPv4 routing table. */
951 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
954 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
955 &zns
->netlink_cmd
, &dp_info
, 0, 1);
959 /* Get IPv6 routing table. */
960 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
963 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
964 &zns
->netlink_cmd
, &dp_info
, 0, 1);
971 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
973 struct nlmsghdr
*nlmsg
,
974 size_t req_size
, int bytelen
,
975 const struct nexthop
*nexthop
)
977 if (route_family
== AF_MPLS
) {
978 struct gw_family_t gw_fam
;
980 gw_fam
.family
= gw_family
;
981 if (gw_family
== AF_INET
)
982 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
984 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
985 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
988 if (gw_family
== AF_INET
)
989 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
990 &nexthop
->gate
.ipv4
, bytelen
);
992 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
993 &nexthop
->gate
.ipv6
, bytelen
);
997 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
1000 struct rtnexthop
*rtnh
,
1001 size_t req_size
, int bytelen
,
1002 const struct nexthop
*nexthop
)
1004 if (route_family
== AF_MPLS
) {
1005 struct gw_family_t gw_fam
;
1007 gw_fam
.family
= gw_family
;
1008 if (gw_family
== AF_INET
)
1009 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1011 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1012 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
1014 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
1016 if (gw_family
== AF_INET
)
1017 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1018 &nexthop
->gate
.ipv4
, bytelen
);
1020 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
1021 &nexthop
->gate
.ipv6
, bytelen
);
1022 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1026 /* This function takes a nexthop as argument and adds
1027 * the appropriate netlink attributes to an existing
1030 * @param routedesc: Human readable description of route type
1031 * (direct/recursive, single-/multipath)
1032 * @param bytelen: Length of addresses in bytes.
1033 * @param nexthop: Nexthop information
1034 * @param nlmsg: nlmsghdr structure to fill in.
1035 * @param req_size: The size allocated for the message.
1037 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
1038 const struct nexthop
*nexthop
,
1039 struct nlmsghdr
*nlmsg
,
1040 struct rtmsg
*rtmsg
,
1041 size_t req_size
, int cmd
)
1043 struct mpls_label_stack
*nh_label
;
1044 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1046 char label_buf
[256];
1049 * label_buf is *only* currently used within debugging.
1050 * As such when we assign it we are guarding it inside
1051 * a debug test. If you want to change this make sure
1052 * you fix this assumption
1054 label_buf
[0] = '\0';
1057 char label_buf1
[20];
1059 nh_label
= nexthop
->nh_label
;
1061 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1062 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1065 if (IS_ZEBRA_DEBUG_KERNEL
) {
1067 sprintf(label_buf
, "label %u",
1068 nh_label
->label
[i
]);
1070 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1071 strlcat(label_buf
, label_buf1
,
1076 out_lse
[num_labels
] =
1077 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1082 /* Set the BoS bit */
1083 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1085 if (rtmsg
->rtm_family
== AF_MPLS
)
1086 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1087 num_labels
* sizeof(mpls_lse_t
));
1089 struct rtattr
*nest
;
1090 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1092 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1094 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1095 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1096 num_labels
* sizeof(mpls_lse_t
));
1097 addattr_nest_end(nlmsg
, nest
);
1101 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1102 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1104 if (rtmsg
->rtm_family
== AF_INET
1105 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1106 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1107 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1108 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1109 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1111 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1112 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1113 &nexthop
->rmap_src
.ipv4
, bytelen
);
1114 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1115 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1116 &nexthop
->src
.ipv4
, bytelen
);
1118 if (IS_ZEBRA_DEBUG_KERNEL
)
1120 " 5549: _netlink_route_build_singlepath() (%s): "
1121 "nexthop via %s %s if %u(%u)",
1122 routedesc
, ipv4_ll_buf
, label_buf
,
1123 nexthop
->ifindex
, nexthop
->vrf_id
);
1127 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1128 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1129 /* Send deletes to the kernel without specifying the next-hop */
1130 if (cmd
!= RTM_DELROUTE
)
1131 _netlink_route_nl_add_gateway_info(
1132 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1135 if (cmd
== RTM_NEWROUTE
) {
1136 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1137 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1138 &nexthop
->rmap_src
.ipv4
, bytelen
);
1139 else if (nexthop
->src
.ipv4
.s_addr
)
1140 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1141 &nexthop
->src
.ipv4
, bytelen
);
1144 if (IS_ZEBRA_DEBUG_KERNEL
)
1146 "netlink_route_multipath() (%s): "
1147 "nexthop via %s %s if %u(%u)",
1148 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1149 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1152 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1153 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1154 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1155 nlmsg
, req_size
, bytelen
,
1158 if (cmd
== RTM_NEWROUTE
) {
1159 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1160 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1161 &nexthop
->rmap_src
.ipv6
, bytelen
);
1162 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1163 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1164 &nexthop
->src
.ipv6
, bytelen
);
1167 if (IS_ZEBRA_DEBUG_KERNEL
)
1169 "netlink_route_multipath() (%s): "
1170 "nexthop via %s %s if %u(%u)",
1171 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1172 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1176 * We have the ifindex so we should always send it
1177 * This is especially useful if we are doing route
1180 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1181 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1183 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1184 if (cmd
== RTM_NEWROUTE
) {
1185 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1186 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1187 &nexthop
->rmap_src
.ipv4
, bytelen
);
1188 else if (nexthop
->src
.ipv4
.s_addr
)
1189 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1190 &nexthop
->src
.ipv4
, bytelen
);
1193 if (IS_ZEBRA_DEBUG_KERNEL
)
1195 "netlink_route_multipath() (%s): "
1196 "nexthop via if %u(%u)",
1197 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1201 /* This function takes a nexthop as argument and
1202 * appends to the given rtattr/rtnexthop pair the
1203 * representation of the nexthop. If the nexthop
1204 * defines a preferred source, the src parameter
1205 * will be modified to point to that src, otherwise
1206 * it will be kept unmodified.
1208 * @param routedesc: Human readable description of route type
1209 * (direct/recursive, single-/multipath)
1210 * @param bytelen: Length of addresses in bytes.
1211 * @param nexthop: Nexthop information
1212 * @param rta: rtnetlink attribute structure
1213 * @param rtnh: pointer to an rtnetlink nexthop structure
1214 * @param src: pointer pointing to a location where
1215 * the prefsrc should be stored.
1217 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1218 const struct nexthop
*nexthop
,
1220 struct rtnexthop
*rtnh
,
1221 struct rtmsg
*rtmsg
,
1222 const union g_addr
**src
)
1224 struct mpls_label_stack
*nh_label
;
1225 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1227 char label_buf
[256];
1229 rtnh
->rtnh_len
= sizeof(*rtnh
);
1230 rtnh
->rtnh_flags
= 0;
1231 rtnh
->rtnh_hops
= 0;
1232 rta
->rta_len
+= rtnh
->rtnh_len
;
1235 * label_buf is *only* currently used within debugging.
1236 * As such when we assign it we are guarding it inside
1237 * a debug test. If you want to change this make sure
1238 * you fix this assumption
1240 label_buf
[0] = '\0';
1243 char label_buf1
[20];
1245 nh_label
= nexthop
->nh_label
;
1247 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1248 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1251 if (IS_ZEBRA_DEBUG_KERNEL
) {
1253 sprintf(label_buf
, "label %u",
1254 nh_label
->label
[i
]);
1256 sprintf(label_buf1
, "/%u", nh_label
->label
[i
]);
1257 strlcat(label_buf
, label_buf1
,
1262 out_lse
[num_labels
] =
1263 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1268 /* Set the BoS bit */
1269 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1271 if (rtmsg
->rtm_family
== AF_MPLS
) {
1272 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1274 num_labels
* sizeof(mpls_lse_t
));
1276 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1278 struct rtattr
*nest
;
1279 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1280 int len
= rta
->rta_len
;
1282 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1283 &encap
, sizeof(uint16_t));
1284 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1285 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1287 num_labels
* sizeof(mpls_lse_t
));
1288 rta_nest_end(rta
, nest
);
1289 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1293 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1294 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1296 if (rtmsg
->rtm_family
== AF_INET
1297 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1298 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1300 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1301 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1303 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1304 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1306 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1307 *src
= &nexthop
->rmap_src
;
1308 else if (nexthop
->src
.ipv4
.s_addr
)
1309 *src
= &nexthop
->src
;
1311 if (IS_ZEBRA_DEBUG_KERNEL
)
1313 " 5549: netlink_route_build_multipath() (%s): "
1314 "nexthop via %s %s if %u",
1315 routedesc
, ipv4_ll_buf
, label_buf
,
1320 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1321 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1322 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1323 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1325 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1326 *src
= &nexthop
->rmap_src
;
1327 else if (nexthop
->src
.ipv4
.s_addr
)
1328 *src
= &nexthop
->src
;
1330 if (IS_ZEBRA_DEBUG_KERNEL
)
1332 "netlink_route_multipath() (%s): "
1333 "nexthop via %s %s if %u",
1334 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1335 label_buf
, nexthop
->ifindex
);
1337 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1338 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1339 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1340 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1343 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1344 *src
= &nexthop
->rmap_src
;
1345 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1346 *src
= &nexthop
->src
;
1348 if (IS_ZEBRA_DEBUG_KERNEL
)
1350 "netlink_route_multipath() (%s): "
1351 "nexthop via %s %s if %u",
1352 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1353 label_buf
, nexthop
->ifindex
);
1357 * We have figured out the ifindex so we should always send it
1358 * This is especially useful if we are doing route
1361 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1362 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1365 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1366 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1367 *src
= &nexthop
->rmap_src
;
1368 else if (nexthop
->src
.ipv4
.s_addr
)
1369 *src
= &nexthop
->src
;
1371 if (IS_ZEBRA_DEBUG_KERNEL
)
1373 "netlink_route_multipath() (%s): "
1374 "nexthop via if %u",
1375 routedesc
, nexthop
->ifindex
);
1379 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1380 const zebra_nhlfe_t
*nhlfe
,
1381 struct nlmsghdr
*nlmsg
,
1382 struct rtmsg
*rtmsg
,
1383 size_t req_size
, int cmd
)
1388 family
= NHLFE_FAMILY(nhlfe
);
1389 bytelen
= (family
== AF_INET
? 4 : 16);
1390 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1391 nlmsg
, rtmsg
, req_size
, cmd
);
1396 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1397 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1398 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1403 family
= NHLFE_FAMILY(nhlfe
);
1404 bytelen
= (family
== AF_INET
? 4 : 16);
1405 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1410 /* Log debug information for netlink_route_multipath
1411 * if debug logging is enabled.
1413 * @param cmd: Netlink command which is to be processed
1414 * @param p: Prefix for which the change is due
1415 * @param family: Address family which the change concerns
1416 * @param zvrf: The vrf we are in
1417 * @param tableid: The table we are working on
1419 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1420 int family
, vrf_id_t vrfid
,
1423 if (IS_ZEBRA_DEBUG_KERNEL
) {
1424 char buf
[PREFIX_STRLEN
];
1426 "netlink_route_multipath(): %s %s vrf %u(%u)",
1427 nl_msg_type_to_str(cmd
),
1428 prefix2str(p
, buf
, sizeof(buf
)),
1433 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1435 if (IS_ZEBRA_DEBUG_KERNEL
)
1436 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1437 nl_msg_type_to_str(cmd
), label
);
1440 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1441 int llalen
, ns_id_t ns_id
)
1449 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1451 memset(&req
, 0, sizeof(req
));
1453 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1454 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1455 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1456 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1458 req
.ndm
.ndm_family
= AF_INET
;
1459 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1460 req
.ndm
.ndm_ifindex
= ifindex
;
1461 req
.ndm
.ndm_type
= RTN_UNICAST
;
1463 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1464 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1466 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1471 * Routing table change via netlink interface, using a dataplane context object
1473 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1476 struct nexthop
*nexthop
= NULL
;
1477 unsigned int nexthop_num
;
1479 const char *routedesc
;
1482 const struct prefix
*p
, *src_p
;
1488 char buf
[NL_PKT_BUF_SIZE
];
1491 p
= dplane_ctx_get_dest(ctx
);
1492 src_p
= dplane_ctx_get_src(ctx
);
1494 family
= PREFIX_FAMILY(p
);
1496 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1498 bytelen
= (family
== AF_INET
? 4 : 16);
1500 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1501 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1503 if ((cmd
== RTM_NEWROUTE
) &&
1504 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1505 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1507 req
.n
.nlmsg_type
= cmd
;
1509 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1511 req
.r
.rtm_family
= family
;
1512 req
.r
.rtm_dst_len
= p
->prefixlen
;
1513 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1514 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1516 if (cmd
== RTM_DELROUTE
)
1517 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1519 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1522 * blackhole routes are not RTN_UNICAST, they are
1523 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1524 * so setting this value as a RTN_UNICAST would
1525 * cause the route lookup of just the prefix
1526 * to fail. So no need to specify this for
1527 * the RTM_DELROUTE case
1529 if (cmd
!= RTM_DELROUTE
)
1530 req
.r
.rtm_type
= RTN_UNICAST
;
1532 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1534 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1538 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1540 * either by the kernel or by zebra. Its purely for calculating best
1542 * by the routing protocol and for communicating with protocol peers.
1544 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1546 #if defined(SUPPORT_REALMS)
1550 if (cmd
== RTM_DELROUTE
)
1551 tag
= dplane_ctx_get_old_tag(ctx
);
1553 tag
= dplane_ctx_get_tag(ctx
);
1555 if (tag
> 0 && tag
<= 255)
1556 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1559 /* Table corresponding to this route. */
1560 table_id
= dplane_ctx_get_table(ctx
);
1562 req
.r
.rtm_table
= table_id
;
1564 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1565 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1568 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1571 * If we are not updating the route and we have received
1572 * a route delete, then all we need to fill in is the
1573 * prefix information to tell the kernel to schwack
1576 if (cmd
== RTM_DELROUTE
)
1579 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1580 char buf
[NL_PKT_BUF_SIZE
];
1581 struct rtattr
*rta
= (void *)buf
;
1582 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1583 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1585 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1587 rta
->rta_type
= RTA_METRICS
;
1588 rta
->rta_len
= RTA_LENGTH(0);
1589 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1590 RTAX_MTU
, &mtu
, sizeof(mtu
));
1591 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1595 /* Count overall nexthops so we can decide whether to use singlepath
1596 * or multipath case.
1599 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1600 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1602 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1608 /* Singlepath case. */
1609 if (nexthop_num
== 1) {
1611 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1613 * So we want to cover 2 types of blackhole
1615 * 1) A normal blackhole route( ala from a static
1617 * 2) A recursively resolved blackhole route
1619 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1620 switch (nexthop
->bh_type
) {
1621 case BLACKHOLE_ADMINPROHIB
:
1622 req
.r
.rtm_type
= RTN_PROHIBIT
;
1624 case BLACKHOLE_REJECT
:
1625 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1628 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1633 if (CHECK_FLAG(nexthop
->flags
,
1634 NEXTHOP_FLAG_RECURSIVE
)) {
1639 if (family
== AF_INET
) {
1640 if (nexthop
->rmap_src
.ipv4
.s_addr
1643 nexthop
->rmap_src
.ipv4
;
1645 } else if (nexthop
->src
.ipv4
.s_addr
1651 } else if (family
== AF_INET6
) {
1652 if (!IN6_IS_ADDR_UNSPECIFIED(
1653 &nexthop
->rmap_src
.ipv6
)) {
1655 nexthop
->rmap_src
.ipv6
;
1658 !IN6_IS_ADDR_UNSPECIFIED(
1659 &nexthop
->src
.ipv6
)) {
1668 if ((cmd
== RTM_NEWROUTE
1669 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1670 routedesc
= nexthop
->rparent
1671 ? "recursive, single-path"
1674 _netlink_route_build_singlepath(
1675 routedesc
, bytelen
, nexthop
, &req
.n
,
1676 &req
.r
, sizeof(req
), cmd
);
1681 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1682 if (family
== AF_INET
)
1683 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1684 &src
.ipv4
, bytelen
);
1685 else if (family
== AF_INET6
)
1686 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1687 &src
.ipv6
, bytelen
);
1689 } else { /* Multipath case */
1690 char buf
[NL_PKT_BUF_SIZE
];
1691 struct rtattr
*rta
= (void *)buf
;
1692 struct rtnexthop
*rtnh
;
1693 const union g_addr
*src1
= NULL
;
1695 rta
->rta_type
= RTA_MULTIPATH
;
1696 rta
->rta_len
= RTA_LENGTH(0);
1697 rtnh
= RTA_DATA(rta
);
1700 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1701 if (CHECK_FLAG(nexthop
->flags
,
1702 NEXTHOP_FLAG_RECURSIVE
)) {
1703 /* This only works for IPv4 now */
1707 if (family
== AF_INET
) {
1708 if (nexthop
->rmap_src
.ipv4
.s_addr
1711 nexthop
->rmap_src
.ipv4
;
1713 } else if (nexthop
->src
.ipv4
.s_addr
1719 } else if (family
== AF_INET6
) {
1720 if (!IN6_IS_ADDR_UNSPECIFIED(
1721 &nexthop
->rmap_src
.ipv6
)) {
1723 nexthop
->rmap_src
.ipv6
;
1726 !IN6_IS_ADDR_UNSPECIFIED(
1727 &nexthop
->src
.ipv6
)) {
1737 if ((cmd
== RTM_NEWROUTE
1738 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1739 routedesc
= nexthop
->rparent
1740 ? "recursive, multipath"
1744 _netlink_route_build_multipath(
1745 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1747 rtnh
= RTNH_NEXT(rtnh
);
1749 if (!setsrc
&& src1
) {
1750 if (family
== AF_INET
)
1751 src
.ipv4
= src1
->ipv4
;
1752 else if (family
== AF_INET6
)
1753 src
.ipv6
= src1
->ipv6
;
1759 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1760 if (family
== AF_INET
)
1761 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1762 &src
.ipv4
, bytelen
);
1763 else if (family
== AF_INET6
)
1764 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1765 &src
.ipv6
, bytelen
);
1766 if (IS_ZEBRA_DEBUG_KERNEL
)
1767 zlog_debug("Setting source");
1770 if (rta
->rta_len
> RTA_LENGTH(0))
1771 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1772 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1775 /* If there is no useful nexthop then return. */
1776 if (nexthop_num
== 0) {
1777 if (IS_ZEBRA_DEBUG_KERNEL
)
1779 "netlink_route_multipath(): No useful nexthop.");
1784 /* Talk to netlink socket. */
1785 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1786 dplane_ctx_get_ns(ctx
), 0);
1789 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1791 uint32_t actual_table
;
1793 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1801 struct zebra_ns
*zns
;
1804 memset(&req
, 0, sizeof(req
));
1806 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1807 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1808 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1810 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1811 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1813 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1814 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1815 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1816 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1820 * So during the namespace cleanup we started storing
1821 * the zvrf table_id for the default table as RT_TABLE_MAIN
1822 * which is what the normal routing table for ip routing is.
1823 * This change caused this to break our lookups of sg data
1824 * because prior to this change the zvrf->table_id was 0
1825 * and when the pim multicast kernel code saw a 0,
1826 * it was auto-translated to RT_TABLE_DEFAULT. But since
1827 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1828 * and the kernel goes screw you and the delicious cookies you
1829 * are trying to give me. So now we have this little hack.
1831 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1833 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1835 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1836 &zns
->netlink_cmd
, zns
, 0);
1843 * Update or delete a prefix from the kernel,
1844 * using info from a dataplane context.
1846 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
1849 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
1850 struct nexthop
*nexthop
;
1852 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
1854 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
1856 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
1858 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
1859 /* Single 'replace' operation */
1863 * With route replace semantics in place
1864 * for v4 routes and the new route is a system
1865 * route we do not install anything.
1866 * The problem here is that the new system
1867 * route should cause us to withdraw from
1868 * the kernel the old non-system route
1870 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)) &&
1871 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
1872 (void)netlink_route_multipath(RTM_DELROUTE
,
1876 * So v6 route replace semantics are not in
1877 * the kernel at this point as I understand it.
1878 * so let's do a delete then an add.
1879 * In the future once v6 route replace semantics
1880 * are in we can figure out what to do here to
1881 * allow working with old and new kernels.
1883 * I'm also intentionally ignoring the failure case
1884 * of the route delete. If that happens yeah we're
1887 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
1888 (void)netlink_route_multipath(RTM_DELROUTE
,
1894 return ZEBRA_DPLANE_REQUEST_FAILURE
;
1897 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
1898 ret
= netlink_route_multipath(cmd
, ctx
);
1901 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
1902 /* Update installed nexthops to signal which have been
1905 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1906 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1909 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1910 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1916 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
1919 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1920 int llalen
, ns_id_t ns_id
)
1922 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1923 addr
, lla
, llalen
, ns_id
);
1927 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1928 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1930 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1931 struct in_addr
*vtep_ip
, int cmd
)
1933 struct zebra_ns
*zns
;
1939 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1940 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1943 memset(&req
, 0, sizeof(req
));
1945 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1946 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1947 if (cmd
== RTM_NEWNEIGH
)
1948 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1949 req
.n
.nlmsg_type
= cmd
;
1950 req
.ndm
.ndm_family
= PF_BRIDGE
;
1951 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1952 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1955 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1956 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1957 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1959 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1964 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1966 * a "flood" MAC FDB entry.
1968 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1970 if (IS_ZEBRA_DEBUG_VXLAN
)
1971 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1972 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1974 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1978 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1979 * deleting the "flood" MAC FDB entry.
1981 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1983 if (IS_ZEBRA_DEBUG_VXLAN
)
1985 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1986 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1988 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1992 #define NDA_RTA(r) \
1993 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1996 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1999 struct interface
*ifp
;
2000 struct zebra_if
*zif
;
2001 struct rtattr
*tb
[NDA_MAX
+ 1];
2002 struct interface
*br_if
;
2005 struct prefix vtep_ip
;
2006 int vid_present
= 0, dst_present
= 0;
2007 char buf
[ETHER_ADDR_STRLEN
];
2012 ndm
= NLMSG_DATA(h
);
2014 /* We only process macfdb notifications if EVPN is enabled */
2015 if (!is_evpn_enabled())
2018 /* The interface should exist. */
2019 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2021 if (!ifp
|| !ifp
->info
) {
2022 if (IS_ZEBRA_DEBUG_KERNEL
)
2023 zlog_debug("\t%s without associated interface: %u",
2024 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
2028 /* The interface should be something we're interested in. */
2029 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
2030 if (IS_ZEBRA_DEBUG_KERNEL
)
2031 zlog_debug("\t%s Not interested in %s, not a slave",
2032 __PRETTY_FUNCTION__
, ifp
->name
);
2036 /* Drop "permanent" entries. */
2037 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2038 if (IS_ZEBRA_DEBUG_KERNEL
)
2039 zlog_debug("\t%s Entry is PERMANENT, dropping",
2040 __PRETTY_FUNCTION__
);
2044 zif
= (struct zebra_if
*)ifp
->info
;
2045 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2046 if (IS_ZEBRA_DEBUG_KERNEL
)
2048 "%s family %s IF %s(%u) brIF %u - no bridge master",
2049 nl_msg_type_to_str(h
->nlmsg_type
),
2050 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2052 zif
->brslave_info
.bridge_ifindex
);
2056 /* Parse attributes and extract fields of interest. */
2057 memset(tb
, 0, sizeof tb
);
2058 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2060 if (!tb
[NDA_LLADDR
]) {
2061 if (IS_ZEBRA_DEBUG_KERNEL
)
2062 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2063 nl_msg_type_to_str(h
->nlmsg_type
),
2064 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2066 zif
->brslave_info
.bridge_ifindex
);
2070 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2071 if (IS_ZEBRA_DEBUG_KERNEL
)
2073 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2074 nl_msg_type_to_str(h
->nlmsg_type
),
2075 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2077 zif
->brslave_info
.bridge_ifindex
,
2078 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2082 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2084 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2086 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2087 sprintf(vid_buf
, " VLAN %u", vid
);
2091 /* TODO: Only IPv4 supported now. */
2093 vtep_ip
.family
= AF_INET
;
2094 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2095 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2097 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2100 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2102 if (IS_ZEBRA_DEBUG_KERNEL
)
2103 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2104 nl_msg_type_to_str(h
->nlmsg_type
),
2105 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2106 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2107 sticky
? "sticky " : "",
2108 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2109 dst_present
? dst_buf
: "");
2111 if (filter_vlan
&& vid
!= filter_vlan
) {
2112 if (IS_ZEBRA_DEBUG_KERNEL
)
2113 zlog_debug("\tFiltered due to filter vlan: %d",
2118 /* If add or update, do accordingly if learnt on a "local" interface; if
2119 * the notification is over VxLAN, this has to be related to
2121 * so perform an implicit delete of any local entry (if it exists).
2123 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2124 if (IS_ZEBRA_IF_VXLAN(ifp
))
2125 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2128 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2132 /* This is a delete notification.
2133 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2134 * 2. For a MAC over "local" interface, delete the mac
2135 * Note: We will get notifications from both bridge driver and VxLAN
2137 * Ignore the notification from VxLan driver as it is also generated
2138 * when mac moves from remote to local.
2141 if (IS_ZEBRA_DEBUG_KERNEL
)
2142 zlog_debug("\tNo Destination Present");
2146 if (IS_ZEBRA_IF_VXLAN(ifp
))
2147 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2150 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2153 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2158 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2161 /* Length validity. */
2162 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2166 /* We are interested only in AF_BRIDGE notifications. */
2167 ndm
= NLMSG_DATA(h
);
2168 if (ndm
->ndm_family
!= AF_BRIDGE
)
2171 return netlink_macfdb_change(h
, len
, ns_id
);
2174 /* Request for MAC FDB information from the kernel */
2175 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2176 int type
, ifindex_t master_ifindex
)
2180 struct ifinfomsg ifm
;
2184 /* Form the request, specifying filter (rtattr) if needed. */
2185 memset(&req
, 0, sizeof(req
));
2186 req
.n
.nlmsg_type
= type
;
2187 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2188 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2189 req
.ifm
.ifi_family
= family
;
2191 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2193 return netlink_request(netlink_cmd
, &req
.n
);
2197 * MAC forwarding database read using netlink interface. This is invoked
2200 int netlink_macfdb_read(struct zebra_ns
*zns
)
2203 struct zebra_dplane_info dp_info
;
2205 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2207 /* Get bridge FDB table. */
2208 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2212 /* We are reading entire table. */
2214 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2221 * MAC forwarding database read using netlink interface. This is for a
2222 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2224 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2225 struct interface
*br_if
)
2227 struct zebra_if
*br_zif
;
2228 struct zebra_if
*zif
;
2229 struct zebra_l2info_vxlan
*vxl
;
2230 struct zebra_dplane_info dp_info
;
2233 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2235 /* Save VLAN we're filtering on, if needed. */
2236 br_zif
= (struct zebra_if
*)br_if
->info
;
2237 zif
= (struct zebra_if
*)ifp
->info
;
2238 vxl
= &zif
->l2info
.vxl
;
2239 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2240 filter_vlan
= vxl
->access_vlan
;
2242 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2244 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2248 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2251 /* Reset VLAN filter. */
2257 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2258 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2261 struct interface
*br_if
,
2262 struct ethaddr
*mac
,
2270 struct zebra_if
*br_zif
;
2271 char buf
[ETHER_ADDR_STRLEN
];
2273 memset(&req
, 0, sizeof(req
));
2274 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2275 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2276 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2277 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2278 /* req.ndm.ndm_state = NUD_REACHABLE; */
2280 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2282 br_zif
= (struct zebra_if
*)br_if
->info
;
2283 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2284 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2286 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2288 if (IS_ZEBRA_DEBUG_KERNEL
)
2289 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2290 __PRETTY_FUNCTION__
,
2291 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2293 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2295 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2298 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2299 struct interface
*br_if
,
2300 struct ethaddr
*mac
, vlanid_t vid
)
2303 struct zebra_dplane_info dp_info
;
2305 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2307 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2309 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2315 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2322 * Netlink-specific handler for MAC updates using dataplane context object.
2324 static enum zebra_dplane_result
2325 netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
)
2334 struct zebra_if
*zif
;
2335 struct interface
*br_if
;
2336 struct zebra_if
*br_zif
;
2337 int vid_present
= 0;
2339 struct zebra_ns
*zns
;
2340 struct interface
*ifp
;
2342 struct in_addr vtep_ip
;
2345 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
)
2350 /* Locate zebra ns and interface objects from context data */
2351 zns
= zebra_ns_lookup(dplane_ctx_get_ns(ctx
)->ns_id
);
2353 /* Nothing to be done */
2354 if (IS_ZEBRA_DEBUG_KERNEL
)
2355 zlog_debug("MAC %s on IF %s(%u) - zebra ns unknown",
2356 (cmd
== RTM_NEWNEIGH
) ? "add" : "del",
2357 dplane_ctx_get_ifname(ctx
),
2358 dplane_ctx_get_ifindex(ctx
));
2360 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2363 ifp
= if_lookup_by_index_per_ns(zns
, dplane_ctx_get_ifindex(ctx
));
2365 /* Nothing to be done */
2366 /* Nothing to be done */
2367 if (IS_ZEBRA_DEBUG_KERNEL
)
2368 zlog_debug("MAC %s on IF %s(%u) - interface unknown",
2369 (cmd
== RTM_NEWNEIGH
) ? "add" : "del",
2370 dplane_ctx_get_ifname(ctx
),
2371 dplane_ctx_get_ifindex(ctx
));
2372 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2375 vid
= dplane_ctx_mac_get_vlan(ctx
);
2378 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2379 if (IS_ZEBRA_DEBUG_KERNEL
)
2380 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2381 (cmd
== RTM_NEWNEIGH
) ? "add" : "del",
2382 ifp
->name
, ifp
->ifindex
);
2383 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2386 memset(&req
, 0, sizeof(req
));
2388 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2389 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2390 if (cmd
== RTM_NEWNEIGH
)
2391 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2392 req
.n
.nlmsg_type
= cmd
;
2393 req
.ndm
.ndm_family
= AF_BRIDGE
;
2394 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2395 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2397 if (dplane_ctx_mac_is_sticky(ctx
))
2398 req
.ndm
.ndm_state
|= NUD_NOARP
;
2400 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2402 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
,
2403 dplane_ctx_mac_get_addr(ctx
), 6);
2404 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2406 dst_alen
= 4; // TODO: hardcoded
2407 vtep_ip
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
2408 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2410 br_zif
= (struct zebra_if
*)br_if
->info
;
2411 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2412 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2414 sprintf(vid_buf
, " VLAN %u", vid
);
2416 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2418 if (IS_ZEBRA_DEBUG_KERNEL
) {
2419 char ipbuf
[PREFIX_STRLEN
];
2420 char buf
[ETHER_ADDR_STRLEN
];
2421 char dst_buf
[PREFIX_STRLEN
+ 10];
2423 inet_ntop(AF_INET
, &vtep_ip
, ipbuf
, sizeof(ipbuf
));
2424 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s", ipbuf
);
2425 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
, sizeof(buf
));
2427 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2428 nl_msg_type_to_str(cmd
),
2429 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2430 ifp
->ifindex
, vid_present
? vid_buf
: "",
2431 dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
2435 ret
= netlink_talk_info(netlink_talk_filter
, &req
.n
,
2436 dplane_ctx_get_ns(ctx
), 0);
2438 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2440 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2444 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2445 * 5549 support, re-install them.
2447 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
2448 struct interface
*ifp
, struct ipaddr
*ip
,
2451 if (ndm
->ndm_family
!= AF_INET
)
2454 if (!zif
->v6_2_v4_ll_neigh_entry
)
2457 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
2460 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
2461 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
2466 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2470 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2473 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2476 struct interface
*ifp
;
2477 struct zebra_if
*zif
;
2478 struct rtattr
*tb
[NDA_MAX
+ 1];
2479 struct interface
*link_if
;
2482 char buf
[ETHER_ADDR_STRLEN
];
2483 char buf2
[INET6_ADDRSTRLEN
];
2484 int mac_present
= 0;
2488 ndm
= NLMSG_DATA(h
);
2490 /* The interface should exist. */
2491 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2493 if (!ifp
|| !ifp
->info
)
2496 zif
= (struct zebra_if
*)ifp
->info
;
2498 /* Parse attributes and extract fields of interest. */
2499 memset(tb
, 0, sizeof tb
);
2500 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2503 zlog_debug("%s family %s IF %s(%u) - no DST",
2504 nl_msg_type_to_str(h
->nlmsg_type
),
2505 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2510 memset(&ip
, 0, sizeof(struct ipaddr
));
2511 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2512 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2514 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
2515 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
2516 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
2517 if (IS_ZEBRA_DEBUG_KERNEL
)
2519 "\tNeighbor Entry Received is a 5549 entry, finished");
2523 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
2524 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
2525 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
2527 /* The neighbor is present on an SVI. From this, we locate the
2529 * bridge because we're only interested in neighbors on a VxLAN bridge.
2530 * The bridge is located based on the nature of the SVI:
2531 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2533 * and is linked to the bridge
2534 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2538 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2539 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2543 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2546 if (IS_ZEBRA_DEBUG_KERNEL
)
2548 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
2552 memset(&mac
, 0, sizeof(struct ethaddr
));
2553 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2554 if (tb
[NDA_LLADDR
]) {
2555 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2556 if (IS_ZEBRA_DEBUG_KERNEL
)
2558 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2563 ifp
->name
, ndm
->ndm_ifindex
,
2564 (unsigned long)RTA_PAYLOAD(
2570 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2573 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
2574 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
2576 if (IS_ZEBRA_DEBUG_KERNEL
)
2578 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2579 nl_msg_type_to_str(h
->nlmsg_type
),
2580 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2582 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2584 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2586 ndm
->ndm_state
, ndm
->ndm_flags
);
2588 /* If the neighbor state is valid for use, process as an add or
2590 * else process as a delete. Note that the delete handling may
2592 * in re-adding the neighbor if it is a valid "remote" neighbor.
2594 if (ndm
->ndm_state
& NUD_VALID
)
2595 return zebra_vxlan_handle_kernel_neigh_update(
2596 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2599 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2602 if (IS_ZEBRA_DEBUG_KERNEL
)
2603 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2604 nl_msg_type_to_str(h
->nlmsg_type
),
2605 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2607 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2609 /* Process the delete - it may result in re-adding the neighbor if it is
2610 * a valid "remote" neighbor.
2612 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2615 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2620 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2623 /* Length validity. */
2624 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2628 /* We are interested only in AF_INET or AF_INET6 notifications. */
2629 ndm
= NLMSG_DATA(h
);
2630 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2633 return netlink_neigh_change(h
, len
);
2636 /* Request for IP neighbor information from the kernel */
2637 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
2638 int type
, ifindex_t ifindex
)
2646 /* Form the request, specifying filter (rtattr) if needed. */
2647 memset(&req
, 0, sizeof(req
));
2648 req
.n
.nlmsg_type
= type
;
2649 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2650 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2651 req
.ndm
.ndm_family
= family
;
2653 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2655 return netlink_request(netlink_cmd
, &req
.n
);
2659 * IP Neighbor table read using netlink interface. This is invoked
2662 int netlink_neigh_read(struct zebra_ns
*zns
)
2665 struct zebra_dplane_info dp_info
;
2667 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2669 /* Get IP neighbor table. */
2670 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2674 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2681 * IP Neighbor table read using netlink interface. This is for a specific
2684 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2687 struct zebra_dplane_info dp_info
;
2689 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2691 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2695 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2702 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
2703 * read using netlink interface.
2705 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
2706 int type
, struct ipaddr
*ip
,
2716 /* Form the request, specifying filter (rtattr) if needed. */
2717 memset(&req
, 0, sizeof(req
));
2718 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2719 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2720 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2721 req
.ndm
.ndm_ifindex
= ifindex
;
2723 if (IS_IPADDR_V4(ip
)) {
2724 ipa_len
= IPV4_MAX_BYTELEN
;
2725 req
.ndm
.ndm_family
= AF_INET
;
2728 ipa_len
= IPV6_MAX_BYTELEN
;
2729 req
.ndm
.ndm_family
= AF_INET6
;
2732 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2734 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2737 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
2738 struct interface
*vlan_if
)
2741 struct zebra_ns
*zns
;
2742 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
2743 char buf
[INET6_ADDRSTRLEN
];
2744 struct zebra_dplane_info dp_info
;
2748 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2750 if (IS_ZEBRA_DEBUG_KERNEL
)
2751 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
2752 __PRETTY_FUNCTION__
, vlan_if
->name
,
2754 ipaddr2str(ip
, buf
, sizeof(buf
)),
2757 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
2762 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2768 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2773 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2776 /* Length validity. */
2777 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2779 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2780 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2781 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2785 /* Is this a notification for the MAC FDB or IP neighbor table? */
2786 ndm
= NLMSG_DATA(h
);
2787 if (ndm
->ndm_family
== AF_BRIDGE
)
2788 return netlink_macfdb_change(h
, len
, ns_id
);
2790 if (ndm
->ndm_type
!= RTN_UNICAST
)
2793 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2794 return netlink_ipneigh_change(h
, len
, ns_id
);
2797 EC_ZEBRA_UNKNOWN_FAMILY
,
2798 "Invalid address family: %u received from kernel neighbor change: %s",
2799 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
2807 * Utility neighbor-update function, using info from dplane context.
2809 static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
2818 char buf
[INET6_ADDRSTRLEN
];
2819 char buf2
[ETHER_ADDR_STRLEN
];
2820 const struct ipaddr
*ip
;
2821 const struct ethaddr
*mac
;
2825 memset(&req
, 0, sizeof(req
));
2827 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
2828 mac
= dplane_ctx_neigh_get_mac(ctx
);
2829 if (is_zero_mac(mac
))
2832 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
2833 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
2835 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2836 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2837 if (cmd
== RTM_NEWNEIGH
)
2838 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2839 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2840 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2841 req
.ndm
.ndm_state
= state
;
2842 req
.ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2843 req
.ndm
.ndm_type
= RTN_UNICAST
;
2844 req
.ndm
.ndm_flags
= flags
;
2846 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2847 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2849 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2851 if (IS_ZEBRA_DEBUG_KERNEL
)
2852 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
2853 nl_msg_type_to_str(cmd
),
2854 nl_family_to_str(req
.ndm
.ndm_family
),
2855 dplane_ctx_get_ifname(ctx
),
2856 dplane_ctx_get_ifindex(ctx
),
2857 ipaddr2str(ip
, buf
, sizeof(buf
)),
2858 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2862 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2863 dplane_ctx_get_ns(ctx
), 0);
2867 * Update MAC, using dataplane context object.
2869 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
2871 return netlink_macfdb_update_ctx(ctx
);
2874 enum zebra_dplane_result
kernel_neigh_update_ctx(struct zebra_dplane_ctx
*ctx
)
2878 switch (dplane_ctx_get_op(ctx
)) {
2879 case DPLANE_OP_NEIGH_INSTALL
:
2880 case DPLANE_OP_NEIGH_UPDATE
:
2881 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
);
2883 case DPLANE_OP_NEIGH_DELETE
:
2884 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
);
2891 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2895 * MPLS label forwarding table change via netlink interface, using dataplane
2896 * context information.
2898 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
2901 const zebra_nhlfe_t
*nhlfe
;
2902 struct nexthop
*nexthop
= NULL
;
2903 unsigned int nexthop_num
;
2904 const char *routedesc
;
2910 char buf
[NL_PKT_BUF_SIZE
];
2913 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
2916 * Count # nexthops so we can decide whether to use singlepath
2917 * or multipath case.
2920 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
2921 nexthop
= nhlfe
->nexthop
;
2924 if (cmd
== RTM_NEWROUTE
) {
2925 /* Count all selected NHLFEs */
2926 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2927 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2930 /* Count all installed NHLFEs */
2931 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2932 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2937 if ((nexthop_num
== 0) ||
2938 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
2941 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2942 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2943 req
.n
.nlmsg_type
= cmd
;
2944 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2946 req
.r
.rtm_family
= AF_MPLS
;
2947 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2948 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2949 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2950 req
.r
.rtm_type
= RTN_UNICAST
;
2952 if (cmd
== RTM_NEWROUTE
) {
2953 /* We do a replace to handle update. */
2954 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2956 /* set the protocol value if installing */
2957 route_type
= re_type_from_lsp_type(
2958 dplane_ctx_get_best_nhlfe(ctx
)->type
);
2959 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2962 /* Fill destination */
2963 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
2964 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2966 /* Fill nexthops (paths) based on single-path or multipath. The paths
2967 * chosen depend on the operation.
2969 if (nexthop_num
== 1) {
2970 routedesc
= "single-path";
2971 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
2975 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
2976 nhlfe
; nhlfe
= nhlfe
->next
) {
2977 nexthop
= nhlfe
->nexthop
;
2981 if ((cmd
== RTM_NEWROUTE
2982 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2983 && CHECK_FLAG(nexthop
->flags
,
2984 NEXTHOP_FLAG_ACTIVE
)))
2985 || (cmd
== RTM_DELROUTE
2986 && (CHECK_FLAG(nhlfe
->flags
,
2987 NHLFE_FLAG_INSTALLED
)
2988 && CHECK_FLAG(nexthop
->flags
,
2989 NEXTHOP_FLAG_FIB
)))) {
2990 /* Add the gateway */
2991 _netlink_mpls_build_singlepath(
3000 } else { /* Multipath case */
3001 char buf
[NL_PKT_BUF_SIZE
];
3002 struct rtattr
*rta
= (void *)buf
;
3003 struct rtnexthop
*rtnh
;
3004 const union g_addr
*src1
= NULL
;
3006 rta
->rta_type
= RTA_MULTIPATH
;
3007 rta
->rta_len
= RTA_LENGTH(0);
3008 rtnh
= RTA_DATA(rta
);
3010 routedesc
= "multipath";
3011 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3015 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
3016 nhlfe
; nhlfe
= nhlfe
->next
) {
3017 nexthop
= nhlfe
->nexthop
;
3021 if ((cmd
== RTM_NEWROUTE
3022 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3023 && CHECK_FLAG(nexthop
->flags
,
3024 NEXTHOP_FLAG_ACTIVE
)))
3025 || (cmd
== RTM_DELROUTE
3026 && (CHECK_FLAG(nhlfe
->flags
,
3027 NHLFE_FLAG_INSTALLED
)
3028 && CHECK_FLAG(nexthop
->flags
,
3029 NEXTHOP_FLAG_FIB
)))) {
3032 /* Build the multipath */
3033 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3036 rtnh
= RTNH_NEXT(rtnh
);
3040 /* Add the multipath */
3041 if (rta
->rta_len
> RTA_LENGTH(0))
3042 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3043 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3046 /* Talk to netlink socket. */
3047 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
3048 dplane_ctx_get_ns(ctx
), 0);
3050 #endif /* HAVE_NETLINK */