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
);
95 static inline int is_selfroute(int proto
)
97 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
98 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
99 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
100 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
101 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
102 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
103 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
110 static inline int zebra2proto(int proto
)
113 case ZEBRA_ROUTE_BABEL
:
114 proto
= RTPROT_BABEL
;
116 case ZEBRA_ROUTE_BGP
:
119 case ZEBRA_ROUTE_OSPF
:
120 case ZEBRA_ROUTE_OSPF6
:
123 case ZEBRA_ROUTE_STATIC
:
124 proto
= RTPROT_ZSTATIC
;
126 case ZEBRA_ROUTE_ISIS
:
129 case ZEBRA_ROUTE_RIP
:
132 case ZEBRA_ROUTE_RIPNG
:
133 proto
= RTPROT_RIPNG
;
135 case ZEBRA_ROUTE_NHRP
:
138 case ZEBRA_ROUTE_EIGRP
:
139 proto
= RTPROT_EIGRP
;
141 case ZEBRA_ROUTE_LDP
:
144 case ZEBRA_ROUTE_SHARP
:
145 proto
= RTPROT_SHARP
;
147 case ZEBRA_ROUTE_PBR
:
150 case ZEBRA_ROUTE_OPENFABRIC
:
151 proto
= RTPROT_OPENFABRIC
;
155 * When a user adds a new protocol this will show up
156 * to let them know to do something about it. This
157 * is intentionally a warn because we should see
158 * this as part of development of a new protocol
161 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
162 __PRETTY_FUNCTION__
, proto
);
163 proto
= RTPROT_ZEBRA
;
170 static inline int proto2zebra(int proto
, int family
)
174 proto
= ZEBRA_ROUTE_BABEL
;
177 proto
= ZEBRA_ROUTE_BGP
;
180 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
184 proto
= ZEBRA_ROUTE_ISIS
;
187 proto
= ZEBRA_ROUTE_RIP
;
190 proto
= ZEBRA_ROUTE_RIPNG
;
193 proto
= ZEBRA_ROUTE_NHRP
;
196 proto
= ZEBRA_ROUTE_EIGRP
;
199 proto
= ZEBRA_ROUTE_LDP
;
203 proto
= ZEBRA_ROUTE_STATIC
;
206 proto
= ZEBRA_ROUTE_SHARP
;
209 proto
= ZEBRA_ROUTE_PBR
;
211 case RTPROT_OPENFABRIC
:
212 proto
= ZEBRA_ROUTE_OPENFABRIC
;
216 * When a user adds a new protocol this will show up
217 * to let them know to do something about it. This
218 * is intentionally a warn because we should see
219 * this as part of development of a new protocol
222 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
223 __PRETTY_FUNCTION__
, proto
);
224 proto
= ZEBRA_ROUTE_KERNEL
;
231 Pending: create an efficient table_id (in a tree/hash) based lookup)
233 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
236 struct zebra_vrf
*zvrf
;
238 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
242 /* case vrf with netns : match the netnsid */
243 if (vrf_is_backend_netns()) {
244 if (ns_id
== zvrf_id(zvrf
))
245 return zvrf_id(zvrf
);
247 /* VRF is VRF_BACKEND_VRF_LITE */
248 if (zvrf
->table_id
!= table_id
)
250 return zvrf_id(zvrf
);
258 * @parse_encap_mpls() - Parses encapsulated mpls attributes
259 * @tb: Pointer to rtattr to look for nested items in.
260 * @labels: Pointer to store labels in.
262 * Return: Number of mpls labels found.
264 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
266 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
267 mpls_lse_t
*lses
= NULL
;
272 mpls_label_t label
= 0;
274 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
275 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
276 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
277 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
278 labels
[num_labels
++] = label
;
284 /* Looking up routing table by netlink interface. */
285 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
290 struct rtattr
*tb
[RTA_MAX
+ 1];
293 struct prefix_ipv6 src_p
= {};
296 char anyaddr
[16] = {0};
298 int proto
= ZEBRA_ROUTE_KERNEL
;
303 uint8_t distance
= 0;
308 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
309 void *src
= NULL
; /* IPv6 srcdest source prefix */
310 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
313 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
318 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
320 switch (rtm
->rtm_type
) {
324 bh_type
= BLACKHOLE_NULL
;
326 case RTN_UNREACHABLE
:
327 bh_type
= BLACKHOLE_REJECT
;
330 bh_type
= BLACKHOLE_ADMINPROHIB
;
333 if (IS_ZEBRA_DEBUG_KERNEL
)
334 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
335 nl_rttype_to_str(rtm
->rtm_type
),
340 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
342 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
343 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
344 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
348 memset(tb
, 0, sizeof tb
);
349 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
351 if (rtm
->rtm_flags
& RTM_F_CLONED
)
353 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
355 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
358 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
359 && h
->nlmsg_type
== RTM_NEWROUTE
) {
360 if (IS_ZEBRA_DEBUG_KERNEL
)
361 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
366 /* We don't care about change notifications for the MPLS table. */
367 /* TODO: Revisit this. */
368 if (rtm
->rtm_family
== AF_MPLS
)
371 /* Table corresponding to route. */
373 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
375 table
= rtm
->rtm_table
;
378 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
379 if (vrf_id
== VRF_DEFAULT
) {
380 if (!is_zebra_valid_kernel_table(table
)
381 && !is_zebra_main_routing_table(table
))
385 /* Route which inserted by Zebra. */
386 if (is_selfroute(rtm
->rtm_protocol
)) {
387 flags
|= ZEBRA_FLAG_SELFROUTE
;
388 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
391 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
394 dest
= RTA_DATA(tb
[RTA_DST
]);
399 src
= RTA_DATA(tb
[RTA_SRC
]);
404 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
407 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
409 if (tb
[RTA_PRIORITY
])
410 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
412 #if defined(SUPPORT_REALMS)
414 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
417 if (tb
[RTA_METRICS
]) {
418 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
420 memset(mxrta
, 0, sizeof mxrta
);
421 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
422 RTA_PAYLOAD(tb
[RTA_METRICS
]));
425 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
428 if (rtm
->rtm_family
== AF_INET
) {
430 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
432 "Invalid destination prefix length: %u received from kernel route change",
436 memcpy(&p
.u
.prefix4
, dest
, 4);
437 p
.prefixlen
= rtm
->rtm_dst_len
;
439 if (rtm
->rtm_src_len
!= 0) {
440 char buf
[PREFIX_STRLEN
];
442 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
443 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
444 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
448 /* Force debug below to not display anything for source */
450 } else if (rtm
->rtm_family
== AF_INET6
) {
452 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
454 "Invalid destination prefix length: %u received from kernel route change",
458 memcpy(&p
.u
.prefix6
, dest
, 16);
459 p
.prefixlen
= rtm
->rtm_dst_len
;
461 src_p
.family
= AF_INET6
;
462 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
464 "Invalid source prefix length: %u received from kernel route change",
468 memcpy(&src_p
.prefix
, src
, 16);
469 src_p
.prefixlen
= rtm
->rtm_src_len
;
473 * For ZEBRA_ROUTE_KERNEL types:
475 * The metric/priority of the route received from the kernel
476 * is a 32 bit number. We are going to interpret the high
477 * order byte as the Admin Distance and the low order 3 bytes
480 * This will allow us to do two things:
481 * 1) Allow the creation of kernel routes that can be
482 * overridden by zebra.
483 * 2) Allow the old behavior for 'most' kernel route types
484 * if a user enters 'ip route ...' v4 routes get a metric
485 * of 0 and v6 routes get a metric of 1024. Both of these
486 * values will end up with a admin distance of 0, which
487 * will cause them to win for the purposes of zebra.
489 if (proto
== ZEBRA_ROUTE_KERNEL
) {
490 distance
= (metric
>> 24) & 0xFF;
491 metric
= (metric
& 0x00FFFFFF);
494 if (IS_ZEBRA_DEBUG_KERNEL
) {
495 char buf
[PREFIX_STRLEN
];
496 char buf2
[PREFIX_STRLEN
];
497 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
498 nl_msg_type_to_str(h
->nlmsg_type
),
499 prefix2str(&p
, buf
, sizeof(buf
)),
500 src_p
.prefixlen
? " from " : "",
502 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
504 vrf_id
, table
, metric
, distance
);
508 if (rtm
->rtm_family
== AF_INET6
)
511 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
512 struct interface
*ifp
;
513 vrf_id_t nh_vrf_id
= vrf_id
;
515 if (!tb
[RTA_MULTIPATH
]) {
517 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
519 memset(&nh
, 0, sizeof(nh
));
521 if (bh_type
== BLACKHOLE_UNSPEC
) {
523 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
524 else if (index
&& gate
)
527 ? NEXTHOP_TYPE_IPV4_IFINDEX
528 : NEXTHOP_TYPE_IPV6_IFINDEX
;
529 else if (!index
&& gate
)
530 nh
.type
= (afi
== AFI_IP
)
534 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
535 nh
.bh_type
= bh_type
;
538 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
539 nh
.bh_type
= bh_type
;
543 memcpy(&nh
.src
, prefsrc
, sz
);
545 memcpy(&nh
.gate
, gate
, sz
);
548 ifp
= if_lookup_by_index_per_ns(
549 zebra_ns_lookup(ns_id
),
552 nh_vrf_id
= ifp
->vrf_id
;
554 nh
.vrf_id
= nh_vrf_id
;
556 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
557 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
558 == LWTUNNEL_ENCAP_MPLS
) {
560 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
563 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
564 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
567 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
570 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
571 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
573 /* This is a multipath route */
575 struct route_entry
*re
;
576 struct rtnexthop
*rtnh
=
577 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
579 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
581 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
583 re
->distance
= distance
;
590 re
->uptime
= monotime(NULL
);
594 struct nexthop
*nh
= NULL
;
596 if (len
< (int)sizeof(*rtnh
)
597 || rtnh
->rtnh_len
> len
)
600 index
= rtnh
->rtnh_ifindex
;
603 * Yes we are looking this up
604 * for every nexthop and just
605 * using the last one looked
608 ifp
= if_lookup_by_index_per_ns(
609 zebra_ns_lookup(ns_id
),
612 nh_vrf_id
= ifp
->vrf_id
;
615 EC_ZEBRA_UNKNOWN_INTERFACE
,
616 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
619 nh_vrf_id
= VRF_DEFAULT
;
625 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
626 memset(tb
, 0, sizeof(tb
));
627 netlink_parse_rtattr(
628 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
629 rtnh
->rtnh_len
- sizeof(*rtnh
));
633 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
634 && *(uint16_t *)RTA_DATA(
636 == LWTUNNEL_ENCAP_MPLS
) {
637 num_labels
= parse_encap_mpls(
638 tb
[RTA_ENCAP
], labels
);
643 if (rtm
->rtm_family
== AF_INET
) {
645 nh
= route_entry_nexthop_ipv4_ifindex_add(
650 nh
= route_entry_nexthop_ipv4_add(
654 } else if (rtm
->rtm_family
657 nh
= route_entry_nexthop_ipv6_ifindex_add(
661 nh
= route_entry_nexthop_ipv6_add(
666 nh
= route_entry_nexthop_ifindex_add(
667 re
, index
, nh_vrf_id
);
669 if (nh
&& num_labels
)
670 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
673 if (nh
&& (rtnh
->rtnh_flags
& RTNH_F_ONLINK
))
675 NEXTHOP_FLAG_ONLINK
);
677 if (rtnh
->rtnh_len
== 0)
680 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
681 rtnh
= RTNH_NEXT(rtnh
);
684 zserv_nexthop_num_warn(__func__
,
685 (const struct prefix
*)&p
,
687 if (re
->nexthop_num
== 0)
690 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
694 if (!tb
[RTA_MULTIPATH
]) {
696 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
698 memset(&nh
, 0, sizeof(nh
));
699 if (bh_type
== BLACKHOLE_UNSPEC
) {
701 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
702 else if (index
&& gate
)
705 ? NEXTHOP_TYPE_IPV4_IFINDEX
706 : NEXTHOP_TYPE_IPV6_IFINDEX
;
707 else if (!index
&& gate
)
708 nh
.type
= (afi
== AFI_IP
)
712 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
713 nh
.bh_type
= BLACKHOLE_UNSPEC
;
716 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
717 nh
.bh_type
= bh_type
;
721 memcpy(&nh
.gate
, gate
, sz
);
722 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
723 &p
, &src_p
, &nh
, table
, metric
, distance
,
726 /* XXX: need to compare the entire list of nexthops
727 * here for NLM_F_APPEND stupidity */
728 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
729 &p
, &src_p
, NULL
, table
, metric
, distance
,
737 static struct mcast_route_data
*mroute
= NULL
;
739 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
740 ns_id_t ns_id
, int startup
)
744 struct rtattr
*tb
[RTA_MAX
+ 1];
745 struct mcast_route_data
*m
;
746 struct mcast_route_data mr
;
753 char oif_list
[256] = "\0";
760 memset(&mr
, 0, sizeof(mr
));
766 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
768 memset(tb
, 0, sizeof tb
);
769 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
772 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
774 table
= rtm
->rtm_table
;
776 vrf
= vrf_lookup_by_table(table
, ns_id
);
779 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
782 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
785 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
788 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
790 if (tb
[RTA_MULTIPATH
]) {
791 struct rtnexthop
*rtnh
=
792 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
794 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
796 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
799 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
802 if (rtnh
->rtnh_len
== 0)
805 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
806 rtnh
= RTNH_NEXT(rtnh
);
810 if (IS_ZEBRA_DEBUG_KERNEL
) {
811 struct interface
*ifp
;
812 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
813 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
814 for (count
= 0; count
< oif_count
; count
++) {
815 ifp
= if_lookup_by_index(oif
[count
], vrf
);
818 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
820 strlcat(oif_list
, temp
, sizeof(oif_list
));
822 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
823 ifp
= if_lookup_by_index(iif
, vrf
);
824 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
825 zvrf
->vrf
->name
, vrf
,
826 nl_msg_type_to_str(h
->nlmsg_type
),
827 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
828 oif_list
, m
->lastused
);
833 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
840 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
841 /* If this is not route add/delete message print warning. */
842 zlog_debug("Kernel message: %s NS %u",
843 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
847 if (!(rtm
->rtm_family
== AF_INET
||
848 rtm
->rtm_family
== AF_INET6
||
849 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
851 EC_ZEBRA_UNKNOWN_FAMILY
,
852 "Invalid address family: %u received from kernel route change: %s",
853 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
857 /* Connected route. */
858 if (IS_ZEBRA_DEBUG_KERNEL
)
859 zlog_debug("%s %s %s proto %s NS %u",
860 nl_msg_type_to_str(h
->nlmsg_type
),
861 nl_family_to_str(rtm
->rtm_family
),
862 nl_rttype_to_str(rtm
->rtm_type
),
863 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
866 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
868 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
871 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
875 if (rtm
->rtm_type
== RTN_MULTICAST
)
876 netlink_route_change_read_multicast(h
, ns_id
, startup
);
878 netlink_route_change_read_unicast(h
, ns_id
, startup
);
882 /* Request for specific route information from the kernel */
883 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
890 /* Form the request, specifying filter (rtattr) if needed. */
891 memset(&req
, 0, sizeof(req
));
892 req
.n
.nlmsg_type
= type
;
893 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
894 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
895 req
.rtm
.rtm_family
= family
;
897 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
900 /* Routing table read function using netlink interface. Only called
902 int netlink_route_read(struct zebra_ns
*zns
)
905 struct zebra_dplane_info dp_info
;
907 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
909 /* Get IPv4 routing table. */
910 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
913 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
914 &zns
->netlink_cmd
, &dp_info
, 0, 1);
918 /* Get IPv6 routing table. */
919 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
922 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
923 &zns
->netlink_cmd
, &dp_info
, 0, 1);
930 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
932 struct nlmsghdr
*nlmsg
,
933 size_t req_size
, int bytelen
,
934 const struct nexthop
*nexthop
)
936 if (route_family
== AF_MPLS
) {
937 struct gw_family_t gw_fam
;
939 gw_fam
.family
= gw_family
;
940 if (gw_family
== AF_INET
)
941 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
943 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
944 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
947 if (gw_family
== AF_INET
)
948 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
949 &nexthop
->gate
.ipv4
, bytelen
);
951 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
952 &nexthop
->gate
.ipv6
, bytelen
);
956 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
959 struct rtnexthop
*rtnh
,
960 size_t req_size
, int bytelen
,
961 const struct nexthop
*nexthop
)
963 if (route_family
== AF_MPLS
) {
964 struct gw_family_t gw_fam
;
966 gw_fam
.family
= gw_family
;
967 if (gw_family
== AF_INET
)
968 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
970 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
971 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
973 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
975 if (gw_family
== AF_INET
)
976 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
977 &nexthop
->gate
.ipv4
, bytelen
);
979 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
980 &nexthop
->gate
.ipv6
, bytelen
);
981 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
985 /* This function takes a nexthop as argument and adds
986 * the appropriate netlink attributes to an existing
989 * @param routedesc: Human readable description of route type
990 * (direct/recursive, single-/multipath)
991 * @param bytelen: Length of addresses in bytes.
992 * @param nexthop: Nexthop information
993 * @param nlmsg: nlmsghdr structure to fill in.
994 * @param req_size: The size allocated for the message.
996 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
997 const struct nexthop
*nexthop
,
998 struct nlmsghdr
*nlmsg
,
1000 size_t req_size
, int cmd
)
1002 struct mpls_label_stack
*nh_label
;
1003 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1005 char label_buf
[256];
1008 * label_buf is *only* currently used within debugging.
1009 * As such when we assign it we are guarding it inside
1010 * a debug test. If you want to change this make sure
1011 * you fix this assumption
1013 label_buf
[0] = '\0';
1016 for (const struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1017 char label_buf1
[20];
1019 nh_label
= nh
->nh_label
;
1020 if (!nh_label
|| !nh_label
->num_labels
)
1023 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1024 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1027 if (IS_ZEBRA_DEBUG_KERNEL
) {
1029 sprintf(label_buf
, "label %u",
1030 nh_label
->label
[i
]);
1032 sprintf(label_buf1
, "/%u",
1033 nh_label
->label
[i
]);
1034 strlcat(label_buf
, label_buf1
,
1039 out_lse
[num_labels
] =
1040 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1046 /* Set the BoS bit */
1047 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1049 if (rtmsg
->rtm_family
== AF_MPLS
)
1050 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1051 num_labels
* sizeof(mpls_lse_t
));
1053 struct rtattr
*nest
;
1054 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1056 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1058 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1059 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1060 num_labels
* sizeof(mpls_lse_t
));
1061 addattr_nest_end(nlmsg
, nest
);
1065 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1066 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1068 if (rtmsg
->rtm_family
== AF_INET
1069 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1070 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1071 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1072 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1073 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1075 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1076 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1077 &nexthop
->rmap_src
.ipv4
, bytelen
);
1078 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1079 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1080 &nexthop
->src
.ipv4
, bytelen
);
1082 if (IS_ZEBRA_DEBUG_KERNEL
)
1084 " 5549: _netlink_route_build_singlepath() (%s): "
1085 "nexthop via %s %s if %u(%u)",
1086 routedesc
, ipv4_ll_buf
, label_buf
,
1087 nexthop
->ifindex
, nexthop
->vrf_id
);
1091 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1092 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1093 /* Send deletes to the kernel without specifying the next-hop */
1094 if (cmd
!= RTM_DELROUTE
)
1095 _netlink_route_nl_add_gateway_info(
1096 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1099 if (cmd
== RTM_NEWROUTE
) {
1100 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1101 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1102 &nexthop
->rmap_src
.ipv4
, bytelen
);
1103 else if (nexthop
->src
.ipv4
.s_addr
)
1104 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1105 &nexthop
->src
.ipv4
, bytelen
);
1108 if (IS_ZEBRA_DEBUG_KERNEL
)
1110 "netlink_route_multipath() (%s): "
1111 "nexthop via %s %s if %u(%u)",
1112 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1113 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1116 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1117 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1118 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1119 nlmsg
, req_size
, bytelen
,
1122 if (cmd
== RTM_NEWROUTE
) {
1123 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1124 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1125 &nexthop
->rmap_src
.ipv6
, bytelen
);
1126 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1127 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1128 &nexthop
->src
.ipv6
, bytelen
);
1131 if (IS_ZEBRA_DEBUG_KERNEL
)
1133 "netlink_route_multipath() (%s): "
1134 "nexthop via %s %s if %u(%u)",
1135 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1136 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1140 * We have the ifindex so we should always send it
1141 * This is especially useful if we are doing route
1144 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1145 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1147 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1148 if (cmd
== RTM_NEWROUTE
) {
1149 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1150 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1151 &nexthop
->rmap_src
.ipv4
, bytelen
);
1152 else if (nexthop
->src
.ipv4
.s_addr
)
1153 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1154 &nexthop
->src
.ipv4
, bytelen
);
1157 if (IS_ZEBRA_DEBUG_KERNEL
)
1159 "netlink_route_multipath() (%s): "
1160 "nexthop via if %u(%u)",
1161 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1165 /* This function takes a nexthop as argument and
1166 * appends to the given rtattr/rtnexthop pair the
1167 * representation of the nexthop. If the nexthop
1168 * defines a preferred source, the src parameter
1169 * will be modified to point to that src, otherwise
1170 * it will be kept unmodified.
1172 * @param routedesc: Human readable description of route type
1173 * (direct/recursive, single-/multipath)
1174 * @param bytelen: Length of addresses in bytes.
1175 * @param nexthop: Nexthop information
1176 * @param rta: rtnetlink attribute structure
1177 * @param rtnh: pointer to an rtnetlink nexthop structure
1178 * @param src: pointer pointing to a location where
1179 * the prefsrc should be stored.
1181 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1182 const struct nexthop
*nexthop
,
1184 struct rtnexthop
*rtnh
,
1185 struct rtmsg
*rtmsg
,
1186 const union g_addr
**src
)
1188 struct mpls_label_stack
*nh_label
;
1189 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1191 char label_buf
[256];
1193 rtnh
->rtnh_len
= sizeof(*rtnh
);
1194 rtnh
->rtnh_flags
= 0;
1195 rtnh
->rtnh_hops
= 0;
1196 rta
->rta_len
+= rtnh
->rtnh_len
;
1199 * label_buf is *only* currently used within debugging.
1200 * As such when we assign it we are guarding it inside
1201 * a debug test. If you want to change this make sure
1202 * you fix this assumption
1204 label_buf
[0] = '\0';
1207 for (const struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1208 char label_buf1
[20];
1210 nh_label
= nh
->nh_label
;
1211 if (!nh_label
|| !nh_label
->num_labels
)
1214 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1215 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1218 if (IS_ZEBRA_DEBUG_KERNEL
) {
1220 sprintf(label_buf
, "label %u",
1221 nh_label
->label
[i
]);
1223 sprintf(label_buf1
, "/%u",
1224 nh_label
->label
[i
]);
1225 strlcat(label_buf
, label_buf1
,
1230 out_lse
[num_labels
] =
1231 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1237 /* Set the BoS bit */
1238 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1240 if (rtmsg
->rtm_family
== AF_MPLS
) {
1241 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1243 num_labels
* sizeof(mpls_lse_t
));
1245 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1247 struct rtattr
*nest
;
1248 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1249 int len
= rta
->rta_len
;
1251 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1252 &encap
, sizeof(uint16_t));
1253 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1254 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1256 num_labels
* sizeof(mpls_lse_t
));
1257 rta_nest_end(rta
, nest
);
1258 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1262 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1263 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1265 if (rtmsg
->rtm_family
== AF_INET
1266 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1267 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1269 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1270 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1272 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1273 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1275 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1276 *src
= &nexthop
->rmap_src
;
1277 else if (nexthop
->src
.ipv4
.s_addr
)
1278 *src
= &nexthop
->src
;
1280 if (IS_ZEBRA_DEBUG_KERNEL
)
1282 " 5549: netlink_route_build_multipath() (%s): "
1283 "nexthop via %s %s if %u",
1284 routedesc
, ipv4_ll_buf
, label_buf
,
1289 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1290 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1291 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1292 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1294 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1295 *src
= &nexthop
->rmap_src
;
1296 else if (nexthop
->src
.ipv4
.s_addr
)
1297 *src
= &nexthop
->src
;
1299 if (IS_ZEBRA_DEBUG_KERNEL
)
1301 "netlink_route_multipath() (%s): "
1302 "nexthop via %s %s if %u",
1303 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1304 label_buf
, nexthop
->ifindex
);
1306 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1307 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1308 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1309 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1312 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1313 *src
= &nexthop
->rmap_src
;
1314 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1315 *src
= &nexthop
->src
;
1317 if (IS_ZEBRA_DEBUG_KERNEL
)
1319 "netlink_route_multipath() (%s): "
1320 "nexthop via %s %s if %u",
1321 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1322 label_buf
, nexthop
->ifindex
);
1326 * We have figured out the ifindex so we should always send it
1327 * This is especially useful if we are doing route
1330 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1331 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1334 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1335 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1336 *src
= &nexthop
->rmap_src
;
1337 else if (nexthop
->src
.ipv4
.s_addr
)
1338 *src
= &nexthop
->src
;
1340 if (IS_ZEBRA_DEBUG_KERNEL
)
1342 "netlink_route_multipath() (%s): "
1343 "nexthop via if %u",
1344 routedesc
, nexthop
->ifindex
);
1348 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1349 const zebra_nhlfe_t
*nhlfe
,
1350 struct nlmsghdr
*nlmsg
,
1351 struct rtmsg
*rtmsg
,
1352 size_t req_size
, int cmd
)
1357 family
= NHLFE_FAMILY(nhlfe
);
1358 bytelen
= (family
== AF_INET
? 4 : 16);
1359 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1360 nlmsg
, rtmsg
, req_size
, cmd
);
1365 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1366 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1367 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1372 family
= NHLFE_FAMILY(nhlfe
);
1373 bytelen
= (family
== AF_INET
? 4 : 16);
1374 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1379 /* Log debug information for netlink_route_multipath
1380 * if debug logging is enabled.
1382 * @param cmd: Netlink command which is to be processed
1383 * @param p: Prefix for which the change is due
1384 * @param family: Address family which the change concerns
1385 * @param zvrf: The vrf we are in
1386 * @param tableid: The table we are working on
1388 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1389 int family
, vrf_id_t vrfid
,
1392 if (IS_ZEBRA_DEBUG_KERNEL
) {
1393 char buf
[PREFIX_STRLEN
];
1395 "netlink_route_multipath(): %s %s vrf %u(%u)",
1396 nl_msg_type_to_str(cmd
),
1397 prefix2str(p
, buf
, sizeof(buf
)),
1402 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1404 if (IS_ZEBRA_DEBUG_KERNEL
)
1405 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1406 nl_msg_type_to_str(cmd
), label
);
1409 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1410 int llalen
, ns_id_t ns_id
)
1418 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1420 memset(&req
, 0, sizeof(req
));
1422 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1423 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1424 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1425 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1427 req
.ndm
.ndm_family
= AF_INET
;
1428 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1429 req
.ndm
.ndm_ifindex
= ifindex
;
1430 req
.ndm
.ndm_type
= RTN_UNICAST
;
1432 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1433 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1435 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1440 * Routing table change via netlink interface, using a dataplane context object
1442 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1445 struct nexthop
*nexthop
= NULL
;
1446 unsigned int nexthop_num
;
1448 const char *routedesc
;
1451 const struct prefix
*p
, *src_p
;
1457 char buf
[NL_PKT_BUF_SIZE
];
1460 p
= dplane_ctx_get_dest(ctx
);
1461 src_p
= dplane_ctx_get_src(ctx
);
1463 family
= PREFIX_FAMILY(p
);
1465 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1467 bytelen
= (family
== AF_INET
? 4 : 16);
1469 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1470 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1472 if ((cmd
== RTM_NEWROUTE
) &&
1473 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1474 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1476 req
.n
.nlmsg_type
= cmd
;
1478 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1480 req
.r
.rtm_family
= family
;
1481 req
.r
.rtm_dst_len
= p
->prefixlen
;
1482 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1483 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1485 if (cmd
== RTM_DELROUTE
)
1486 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1488 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1491 * blackhole routes are not RTN_UNICAST, they are
1492 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1493 * so setting this value as a RTN_UNICAST would
1494 * cause the route lookup of just the prefix
1495 * to fail. So no need to specify this for
1496 * the RTM_DELROUTE case
1498 if (cmd
!= RTM_DELROUTE
)
1499 req
.r
.rtm_type
= RTN_UNICAST
;
1501 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1503 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1507 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1509 * either by the kernel or by zebra. Its purely for calculating best
1511 * by the routing protocol and for communicating with protocol peers.
1513 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1515 #if defined(SUPPORT_REALMS)
1519 if (cmd
== RTM_DELROUTE
)
1520 tag
= dplane_ctx_get_old_tag(ctx
);
1522 tag
= dplane_ctx_get_tag(ctx
);
1524 if (tag
> 0 && tag
<= 255)
1525 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1528 /* Table corresponding to this route. */
1529 table_id
= dplane_ctx_get_table(ctx
);
1531 req
.r
.rtm_table
= table_id
;
1533 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1534 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1537 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1540 * If we are not updating the route and we have received
1541 * a route delete, then all we need to fill in is the
1542 * prefix information to tell the kernel to schwack
1545 if (cmd
== RTM_DELROUTE
)
1548 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1549 char buf
[NL_PKT_BUF_SIZE
];
1550 struct rtattr
*rta
= (void *)buf
;
1551 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1552 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1554 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1556 rta
->rta_type
= RTA_METRICS
;
1557 rta
->rta_len
= RTA_LENGTH(0);
1558 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1559 RTAX_MTU
, &mtu
, sizeof(mtu
));
1560 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1564 /* Count overall nexthops so we can decide whether to use singlepath
1565 * or multipath case.
1568 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1569 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1571 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1577 /* Singlepath case. */
1578 if (nexthop_num
== 1) {
1580 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1582 * So we want to cover 2 types of blackhole
1584 * 1) A normal blackhole route( ala from a static
1586 * 2) A recursively resolved blackhole route
1588 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1589 switch (nexthop
->bh_type
) {
1590 case BLACKHOLE_ADMINPROHIB
:
1591 req
.r
.rtm_type
= RTN_PROHIBIT
;
1593 case BLACKHOLE_REJECT
:
1594 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1597 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1602 if (CHECK_FLAG(nexthop
->flags
,
1603 NEXTHOP_FLAG_RECURSIVE
)) {
1608 if (family
== AF_INET
) {
1609 if (nexthop
->rmap_src
.ipv4
.s_addr
1612 nexthop
->rmap_src
.ipv4
;
1614 } else if (nexthop
->src
.ipv4
.s_addr
1620 } else if (family
== AF_INET6
) {
1621 if (!IN6_IS_ADDR_UNSPECIFIED(
1622 &nexthop
->rmap_src
.ipv6
)) {
1624 nexthop
->rmap_src
.ipv6
;
1627 !IN6_IS_ADDR_UNSPECIFIED(
1628 &nexthop
->src
.ipv6
)) {
1637 if ((cmd
== RTM_NEWROUTE
1638 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1639 routedesc
= nexthop
->rparent
1640 ? "recursive, single-path"
1643 _netlink_route_build_singlepath(
1644 routedesc
, bytelen
, nexthop
, &req
.n
,
1645 &req
.r
, sizeof(req
), cmd
);
1650 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1651 if (family
== AF_INET
)
1652 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1653 &src
.ipv4
, bytelen
);
1654 else if (family
== AF_INET6
)
1655 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1656 &src
.ipv6
, bytelen
);
1658 } else { /* Multipath case */
1659 char buf
[NL_PKT_BUF_SIZE
];
1660 struct rtattr
*rta
= (void *)buf
;
1661 struct rtnexthop
*rtnh
;
1662 const union g_addr
*src1
= NULL
;
1664 rta
->rta_type
= RTA_MULTIPATH
;
1665 rta
->rta_len
= RTA_LENGTH(0);
1666 rtnh
= RTA_DATA(rta
);
1669 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1670 if (CHECK_FLAG(nexthop
->flags
,
1671 NEXTHOP_FLAG_RECURSIVE
)) {
1672 /* This only works for IPv4 now */
1676 if (family
== AF_INET
) {
1677 if (nexthop
->rmap_src
.ipv4
.s_addr
1680 nexthop
->rmap_src
.ipv4
;
1682 } else if (nexthop
->src
.ipv4
.s_addr
1688 } else if (family
== AF_INET6
) {
1689 if (!IN6_IS_ADDR_UNSPECIFIED(
1690 &nexthop
->rmap_src
.ipv6
)) {
1692 nexthop
->rmap_src
.ipv6
;
1695 !IN6_IS_ADDR_UNSPECIFIED(
1696 &nexthop
->src
.ipv6
)) {
1706 if ((cmd
== RTM_NEWROUTE
1707 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1708 routedesc
= nexthop
->rparent
1709 ? "recursive, multipath"
1713 _netlink_route_build_multipath(
1714 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1716 rtnh
= RTNH_NEXT(rtnh
);
1718 if (!setsrc
&& src1
) {
1719 if (family
== AF_INET
)
1720 src
.ipv4
= src1
->ipv4
;
1721 else if (family
== AF_INET6
)
1722 src
.ipv6
= src1
->ipv6
;
1728 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1729 if (family
== AF_INET
)
1730 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1731 &src
.ipv4
, bytelen
);
1732 else if (family
== AF_INET6
)
1733 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1734 &src
.ipv6
, bytelen
);
1735 if (IS_ZEBRA_DEBUG_KERNEL
)
1736 zlog_debug("Setting source");
1739 if (rta
->rta_len
> RTA_LENGTH(0))
1740 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1741 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1744 /* If there is no useful nexthop then return. */
1745 if (nexthop_num
== 0) {
1746 if (IS_ZEBRA_DEBUG_KERNEL
)
1748 "netlink_route_multipath(): No useful nexthop.");
1753 /* Talk to netlink socket. */
1754 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1755 dplane_ctx_get_ns(ctx
), 0);
1758 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1760 uint32_t actual_table
;
1762 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1770 struct zebra_ns
*zns
;
1773 memset(&req
, 0, sizeof(req
));
1775 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1776 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1777 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1779 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1780 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1782 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1783 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1784 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1785 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1789 * So during the namespace cleanup we started storing
1790 * the zvrf table_id for the default table as RT_TABLE_MAIN
1791 * which is what the normal routing table for ip routing is.
1792 * This change caused this to break our lookups of sg data
1793 * because prior to this change the zvrf->table_id was 0
1794 * and when the pim multicast kernel code saw a 0,
1795 * it was auto-translated to RT_TABLE_DEFAULT. But since
1796 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1797 * and the kernel goes screw you and the delicious cookies you
1798 * are trying to give me. So now we have this little hack.
1800 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1802 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1804 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1805 &zns
->netlink_cmd
, zns
, 0);
1812 * Update or delete a prefix from the kernel,
1813 * using info from a dataplane context.
1815 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
1818 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
1819 struct nexthop
*nexthop
;
1821 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
1823 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
1825 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
1827 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
1828 /* Single 'replace' operation */
1832 * So v6 route replace semantics are not in
1833 * the kernel at this point as I understand it.
1834 * so let's do a delete then an add.
1835 * In the future once v6 route replace semantics
1836 * are in we can figure out what to do here to
1837 * allow working with old and new kernels.
1839 * I'm also intentionally ignoring the failure case
1840 * of the route delete. If that happens yeah we're
1843 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
1844 (void)netlink_route_multipath(RTM_DELROUTE
,
1850 return ZEBRA_DPLANE_REQUEST_FAILURE
;
1853 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
1854 ret
= netlink_route_multipath(cmd
, ctx
);
1857 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
1858 /* Update installed nexthops to signal which have been
1861 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1862 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1865 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1866 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1872 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
1875 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1876 int llalen
, ns_id_t ns_id
)
1878 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1879 addr
, lla
, llalen
, ns_id
);
1883 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1884 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1886 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1887 struct in_addr
*vtep_ip
, int cmd
)
1889 struct zebra_ns
*zns
;
1895 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1896 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1899 memset(&req
, 0, sizeof(req
));
1901 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1902 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1903 if (cmd
== RTM_NEWNEIGH
)
1904 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1905 req
.n
.nlmsg_type
= cmd
;
1906 req
.ndm
.ndm_family
= PF_BRIDGE
;
1907 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1908 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1911 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1912 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1913 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1915 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1920 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1922 * a "flood" MAC FDB entry.
1924 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1926 if (IS_ZEBRA_DEBUG_VXLAN
)
1927 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1928 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1930 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1934 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1935 * deleting the "flood" MAC FDB entry.
1937 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1939 if (IS_ZEBRA_DEBUG_VXLAN
)
1941 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1942 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1944 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1948 #define NDA_RTA(r) \
1949 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1952 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1955 struct interface
*ifp
;
1956 struct zebra_if
*zif
;
1957 struct rtattr
*tb
[NDA_MAX
+ 1];
1958 struct interface
*br_if
;
1961 struct prefix vtep_ip
;
1962 int vid_present
= 0, dst_present
= 0;
1963 char buf
[ETHER_ADDR_STRLEN
];
1968 ndm
= NLMSG_DATA(h
);
1970 /* We only process macfdb notifications if EVPN is enabled */
1971 if (!is_evpn_enabled())
1974 /* The interface should exist. */
1975 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1977 if (!ifp
|| !ifp
->info
) {
1978 if (IS_ZEBRA_DEBUG_KERNEL
)
1979 zlog_debug("\t%s without associated interface: %u",
1980 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
1984 /* The interface should be something we're interested in. */
1985 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
1986 if (IS_ZEBRA_DEBUG_KERNEL
)
1987 zlog_debug("\t%s Not interested in %s, not a slave",
1988 __PRETTY_FUNCTION__
, ifp
->name
);
1992 /* Drop "permanent" entries. */
1993 if (ndm
->ndm_state
& NUD_PERMANENT
) {
1994 if (IS_ZEBRA_DEBUG_KERNEL
)
1995 zlog_debug("\t%s Entry is PERMANENT, dropping",
1996 __PRETTY_FUNCTION__
);
2000 zif
= (struct zebra_if
*)ifp
->info
;
2001 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2002 if (IS_ZEBRA_DEBUG_KERNEL
)
2004 "%s family %s IF %s(%u) brIF %u - no bridge master",
2005 nl_msg_type_to_str(h
->nlmsg_type
),
2006 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2008 zif
->brslave_info
.bridge_ifindex
);
2012 /* Parse attributes and extract fields of interest. */
2013 memset(tb
, 0, sizeof tb
);
2014 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2016 if (!tb
[NDA_LLADDR
]) {
2017 if (IS_ZEBRA_DEBUG_KERNEL
)
2018 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2019 nl_msg_type_to_str(h
->nlmsg_type
),
2020 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2022 zif
->brslave_info
.bridge_ifindex
);
2026 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2027 if (IS_ZEBRA_DEBUG_KERNEL
)
2029 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2030 nl_msg_type_to_str(h
->nlmsg_type
),
2031 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2033 zif
->brslave_info
.bridge_ifindex
,
2034 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2038 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2040 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2042 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2043 sprintf(vid_buf
, " VLAN %u", vid
);
2047 /* TODO: Only IPv4 supported now. */
2049 vtep_ip
.family
= AF_INET
;
2050 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2051 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2053 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2056 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2058 if (IS_ZEBRA_DEBUG_KERNEL
)
2059 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2060 nl_msg_type_to_str(h
->nlmsg_type
),
2061 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2062 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2063 sticky
? "sticky " : "",
2064 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2065 dst_present
? dst_buf
: "");
2067 if (filter_vlan
&& vid
!= filter_vlan
) {
2068 if (IS_ZEBRA_DEBUG_KERNEL
)
2069 zlog_debug("\tFiltered due to filter vlan: %d",
2074 /* If add or update, do accordingly if learnt on a "local" interface; if
2075 * the notification is over VxLAN, this has to be related to
2077 * so perform an implicit delete of any local entry (if it exists).
2079 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2080 if (IS_ZEBRA_IF_VXLAN(ifp
))
2081 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2084 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2088 /* This is a delete notification.
2089 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2090 * 2. For a MAC over "local" interface, delete the mac
2091 * Note: We will get notifications from both bridge driver and VxLAN
2093 * Ignore the notification from VxLan driver as it is also generated
2094 * when mac moves from remote to local.
2097 if (IS_ZEBRA_DEBUG_KERNEL
)
2098 zlog_debug("\tNo Destination Present");
2102 if (IS_ZEBRA_IF_VXLAN(ifp
))
2103 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2106 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2109 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2114 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2117 /* Length validity. */
2118 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2122 /* We are interested only in AF_BRIDGE notifications. */
2123 ndm
= NLMSG_DATA(h
);
2124 if (ndm
->ndm_family
!= AF_BRIDGE
)
2127 return netlink_macfdb_change(h
, len
, ns_id
);
2130 /* Request for MAC FDB information from the kernel */
2131 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2132 int type
, ifindex_t master_ifindex
)
2136 struct ifinfomsg ifm
;
2140 /* Form the request, specifying filter (rtattr) if needed. */
2141 memset(&req
, 0, sizeof(req
));
2142 req
.n
.nlmsg_type
= type
;
2143 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2144 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2145 req
.ifm
.ifi_family
= family
;
2147 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2149 return netlink_request(netlink_cmd
, &req
.n
);
2153 * MAC forwarding database read using netlink interface. This is invoked
2156 int netlink_macfdb_read(struct zebra_ns
*zns
)
2159 struct zebra_dplane_info dp_info
;
2161 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2163 /* Get bridge FDB table. */
2164 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2168 /* We are reading entire table. */
2170 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2177 * MAC forwarding database read using netlink interface. This is for a
2178 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2180 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2181 struct interface
*br_if
)
2183 struct zebra_if
*br_zif
;
2184 struct zebra_if
*zif
;
2185 struct zebra_l2info_vxlan
*vxl
;
2186 struct zebra_dplane_info dp_info
;
2189 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2191 /* Save VLAN we're filtering on, if needed. */
2192 br_zif
= (struct zebra_if
*)br_if
->info
;
2193 zif
= (struct zebra_if
*)ifp
->info
;
2194 vxl
= &zif
->l2info
.vxl
;
2195 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2196 filter_vlan
= vxl
->access_vlan
;
2198 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2200 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2204 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2207 /* Reset VLAN filter. */
2213 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2214 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2217 struct interface
*br_if
,
2218 struct ethaddr
*mac
,
2226 struct zebra_if
*br_zif
;
2227 char buf
[ETHER_ADDR_STRLEN
];
2229 memset(&req
, 0, sizeof(req
));
2230 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2231 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2232 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2233 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2234 /* req.ndm.ndm_state = NUD_REACHABLE; */
2236 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2238 br_zif
= (struct zebra_if
*)br_if
->info
;
2239 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2240 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2242 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2244 if (IS_ZEBRA_DEBUG_KERNEL
)
2245 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2246 __PRETTY_FUNCTION__
,
2247 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2249 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2251 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2254 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2255 struct interface
*br_if
,
2256 struct ethaddr
*mac
, vlanid_t vid
)
2259 struct zebra_dplane_info dp_info
;
2261 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2263 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2265 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2271 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2276 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2277 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2278 int cmd
, bool sticky
)
2280 struct zebra_ns
*zns
;
2287 struct zebra_if
*zif
;
2288 struct interface
*br_if
;
2289 struct zebra_if
*br_zif
;
2290 char buf
[ETHER_ADDR_STRLEN
];
2291 int vid_present
= 0;
2294 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2298 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2299 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2300 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2305 memset(&req
, 0, sizeof(req
));
2307 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2308 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2309 if (cmd
== RTM_NEWNEIGH
)
2310 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2311 req
.n
.nlmsg_type
= cmd
;
2312 req
.ndm
.ndm_family
= AF_BRIDGE
;
2313 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2314 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2317 req
.ndm
.ndm_state
|= NUD_NOARP
;
2319 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2321 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2322 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2323 dst_alen
= 4; // TODO: hardcoded
2324 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2325 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2326 br_zif
= (struct zebra_if
*)br_if
->info
;
2327 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2328 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2330 sprintf(vid_buf
, " VLAN %u", vid
);
2332 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2334 if (IS_ZEBRA_DEBUG_KERNEL
)
2335 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2336 nl_msg_type_to_str(cmd
),
2337 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2338 ifp
->ifindex
, vid_present
? vid_buf
: "",
2339 sticky
? "sticky " : "",
2340 prefix_mac2str(mac
, buf
, sizeof(buf
)), dst_buf
);
2342 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2347 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2348 * 5549 support, re-install them.
2350 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
2351 struct interface
*ifp
, struct ipaddr
*ip
)
2353 if (ndm
->ndm_family
!= AF_INET
)
2356 if (!zif
->v6_2_v4_ll_neigh_entry
)
2359 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
2362 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2366 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2369 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2372 struct interface
*ifp
;
2373 struct zebra_if
*zif
;
2374 struct rtattr
*tb
[NDA_MAX
+ 1];
2375 struct interface
*link_if
;
2378 char buf
[ETHER_ADDR_STRLEN
];
2379 char buf2
[INET6_ADDRSTRLEN
];
2380 int mac_present
= 0;
2384 ndm
= NLMSG_DATA(h
);
2386 /* The interface should exist. */
2387 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2389 if (!ifp
|| !ifp
->info
)
2392 zif
= (struct zebra_if
*)ifp
->info
;
2394 /* Parse attributes and extract fields of interest. */
2395 memset(tb
, 0, sizeof tb
);
2396 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2399 zlog_debug("%s family %s IF %s(%u) - no DST",
2400 nl_msg_type_to_str(h
->nlmsg_type
),
2401 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2406 memset(&ip
, 0, sizeof(struct ipaddr
));
2407 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2408 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2410 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
2411 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
2412 netlink_handle_5549(ndm
, zif
, ifp
, &ip
);
2413 if (IS_ZEBRA_DEBUG_KERNEL
)
2415 "\tNeighbor Entry Received is a 5549 entry, finished");
2419 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
2420 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
)) {
2421 if (!(ndm
->ndm_state
& NUD_FAILED
))
2422 netlink_handle_5549(ndm
, zif
, ifp
, &ip
);
2424 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
2428 /* The neighbor is present on an SVI. From this, we locate the
2430 * bridge because we're only interested in neighbors on a VxLAN bridge.
2431 * The bridge is located based on the nature of the SVI:
2432 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2434 * and is linked to the bridge
2435 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2439 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2440 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2444 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2447 if (IS_ZEBRA_DEBUG_KERNEL
)
2449 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
2453 memset(&mac
, 0, sizeof(struct ethaddr
));
2454 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2455 if (tb
[NDA_LLADDR
]) {
2456 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2457 if (IS_ZEBRA_DEBUG_KERNEL
)
2459 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2464 ifp
->name
, ndm
->ndm_ifindex
,
2465 (unsigned long)RTA_PAYLOAD(
2471 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2474 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
2475 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
2477 if (IS_ZEBRA_DEBUG_KERNEL
)
2479 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2480 nl_msg_type_to_str(h
->nlmsg_type
),
2481 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2483 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2485 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2487 ndm
->ndm_state
, ndm
->ndm_flags
);
2489 /* If the neighbor state is valid for use, process as an add or
2491 * else process as a delete. Note that the delete handling may
2493 * in re-adding the neighbor if it is a valid "remote" neighbor.
2495 if (ndm
->ndm_state
& NUD_VALID
)
2496 return zebra_vxlan_handle_kernel_neigh_update(
2497 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2500 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2503 if (IS_ZEBRA_DEBUG_KERNEL
)
2504 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2505 nl_msg_type_to_str(h
->nlmsg_type
),
2506 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2508 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2510 /* Process the delete - it may result in re-adding the neighbor if it is
2511 * a valid "remote" neighbor.
2513 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2516 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2521 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2524 /* Length validity. */
2525 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2529 /* We are interested only in AF_INET or AF_INET6 notifications. */
2530 ndm
= NLMSG_DATA(h
);
2531 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2534 return netlink_neigh_change(h
, len
);
2537 /* Request for IP neighbor information from the kernel */
2538 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
2539 int type
, ifindex_t ifindex
)
2547 /* Form the request, specifying filter (rtattr) if needed. */
2548 memset(&req
, 0, sizeof(req
));
2549 req
.n
.nlmsg_type
= type
;
2550 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2551 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2552 req
.ndm
.ndm_family
= family
;
2554 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2556 return netlink_request(netlink_cmd
, &req
.n
);
2560 * IP Neighbor table read using netlink interface. This is invoked
2563 int netlink_neigh_read(struct zebra_ns
*zns
)
2566 struct zebra_dplane_info dp_info
;
2568 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2570 /* Get IP neighbor table. */
2571 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2575 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2582 * IP Neighbor table read using netlink interface. This is for a specific
2585 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2588 struct zebra_dplane_info dp_info
;
2590 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2592 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2596 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2603 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
2604 * read using netlink interface.
2606 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
2607 int type
, struct ipaddr
*ip
,
2617 /* Form the request, specifying filter (rtattr) if needed. */
2618 memset(&req
, 0, sizeof(req
));
2619 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2620 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2621 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2622 req
.ndm
.ndm_ifindex
= ifindex
;
2624 if (IS_IPADDR_V4(ip
)) {
2625 ipa_len
= IPV4_MAX_BYTELEN
;
2626 req
.ndm
.ndm_family
= AF_INET
;
2629 ipa_len
= IPV6_MAX_BYTELEN
;
2630 req
.ndm
.ndm_family
= AF_INET6
;
2633 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2635 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2638 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
2639 struct interface
*vlan_if
)
2642 struct zebra_ns
*zns
;
2643 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
2644 char buf
[INET6_ADDRSTRLEN
];
2645 struct zebra_dplane_info dp_info
;
2649 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2651 if (IS_ZEBRA_DEBUG_KERNEL
)
2652 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
2653 __PRETTY_FUNCTION__
, vlan_if
->name
,
2655 ipaddr2str(ip
, buf
, sizeof(buf
)),
2658 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
2663 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2669 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2674 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2677 /* Length validity. */
2678 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2680 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2681 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2682 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2686 /* Is this a notification for the MAC FDB or IP neighbor table? */
2687 ndm
= NLMSG_DATA(h
);
2688 if (ndm
->ndm_family
== AF_BRIDGE
)
2689 return netlink_macfdb_change(h
, len
, ns_id
);
2691 if (ndm
->ndm_type
!= RTN_UNICAST
)
2694 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2695 return netlink_ipneigh_change(h
, len
, ns_id
);
2698 EC_ZEBRA_UNKNOWN_FAMILY
,
2699 "Invalid address family: %u received from kernel neighbor change: %s",
2700 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
2707 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2708 struct ethaddr
*mac
, uint8_t flags
,
2709 uint16_t state
, int cmd
)
2718 struct zebra_ns
*zns
;
2719 char buf
[INET6_ADDRSTRLEN
];
2720 char buf2
[ETHER_ADDR_STRLEN
];
2721 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2724 memset(&req
, 0, sizeof(req
));
2726 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2727 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2728 if (cmd
== RTM_NEWNEIGH
)
2729 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2730 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2731 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2732 req
.ndm
.ndm_state
= state
;
2733 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2734 req
.ndm
.ndm_type
= RTN_UNICAST
;
2735 req
.ndm
.ndm_flags
= flags
;
2737 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2738 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2740 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2742 if (IS_ZEBRA_DEBUG_KERNEL
)
2743 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
2744 nl_msg_type_to_str(cmd
),
2745 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2746 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2747 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2748 : "null", flags
, state
);
2750 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2754 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2755 struct in_addr vtep_ip
, bool sticky
)
2757 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_NEWNEIGH
,
2761 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2762 struct in_addr vtep_ip
)
2764 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_DELNEIGH
, 0);
2767 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2768 struct ethaddr
*mac
, uint8_t flags
)
2770 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2771 NUD_NOARP
, RTM_NEWNEIGH
);
2774 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2776 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2779 int kernel_upd_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2780 struct ethaddr
*mac
, uint8_t flags
, uint16_t state
)
2782 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2783 state
, RTM_NEWNEIGH
);
2787 * MPLS label forwarding table change via netlink interface, using dataplane
2788 * context information.
2790 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
2793 const zebra_nhlfe_t
*nhlfe
;
2794 struct nexthop
*nexthop
= NULL
;
2795 unsigned int nexthop_num
;
2796 const char *routedesc
;
2802 char buf
[NL_PKT_BUF_SIZE
];
2805 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
2808 * Count # nexthops so we can decide whether to use singlepath
2809 * or multipath case.
2812 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
2813 nexthop
= nhlfe
->nexthop
;
2816 if (cmd
== RTM_NEWROUTE
) {
2817 /* Count all selected NHLFEs */
2818 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2819 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2822 /* Count all installed NHLFEs */
2823 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2824 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2829 if ((nexthop_num
== 0) ||
2830 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
2833 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2834 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2835 req
.n
.nlmsg_type
= cmd
;
2836 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2838 req
.r
.rtm_family
= AF_MPLS
;
2839 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2840 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2841 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2842 req
.r
.rtm_type
= RTN_UNICAST
;
2844 if (cmd
== RTM_NEWROUTE
) {
2845 /* We do a replace to handle update. */
2846 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2848 /* set the protocol value if installing */
2849 route_type
= re_type_from_lsp_type(
2850 dplane_ctx_get_best_nhlfe(ctx
)->type
);
2851 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2854 /* Fill destination */
2855 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
2856 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2858 /* Fill nexthops (paths) based on single-path or multipath. The paths
2859 * chosen depend on the operation.
2861 if (nexthop_num
== 1) {
2862 routedesc
= "single-path";
2863 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
2867 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
2868 nhlfe
; nhlfe
= nhlfe
->next
) {
2869 nexthop
= nhlfe
->nexthop
;
2873 if ((cmd
== RTM_NEWROUTE
2874 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2875 && CHECK_FLAG(nexthop
->flags
,
2876 NEXTHOP_FLAG_ACTIVE
)))
2877 || (cmd
== RTM_DELROUTE
2878 && (CHECK_FLAG(nhlfe
->flags
,
2879 NHLFE_FLAG_INSTALLED
)
2880 && CHECK_FLAG(nexthop
->flags
,
2881 NEXTHOP_FLAG_FIB
)))) {
2882 /* Add the gateway */
2883 _netlink_mpls_build_singlepath(
2892 } else { /* Multipath case */
2893 char buf
[NL_PKT_BUF_SIZE
];
2894 struct rtattr
*rta
= (void *)buf
;
2895 struct rtnexthop
*rtnh
;
2896 const union g_addr
*src1
= NULL
;
2898 rta
->rta_type
= RTA_MULTIPATH
;
2899 rta
->rta_len
= RTA_LENGTH(0);
2900 rtnh
= RTA_DATA(rta
);
2902 routedesc
= "multipath";
2903 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
2907 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
2908 nhlfe
; nhlfe
= nhlfe
->next
) {
2909 nexthop
= nhlfe
->nexthop
;
2913 if ((cmd
== RTM_NEWROUTE
2914 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2915 && CHECK_FLAG(nexthop
->flags
,
2916 NEXTHOP_FLAG_ACTIVE
)))
2917 || (cmd
== RTM_DELROUTE
2918 && (CHECK_FLAG(nhlfe
->flags
,
2919 NHLFE_FLAG_INSTALLED
)
2920 && CHECK_FLAG(nexthop
->flags
,
2921 NEXTHOP_FLAG_FIB
)))) {
2924 /* Build the multipath */
2925 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2928 rtnh
= RTNH_NEXT(rtnh
);
2932 /* Add the multipath */
2933 if (rta
->rta_len
> RTA_LENGTH(0))
2934 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2935 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2938 /* Talk to netlink socket. */
2939 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2940 dplane_ctx_get_ns(ctx
), 0);
2942 #endif /* HAVE_NETLINK */