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
;
336 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
338 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
339 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
340 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
344 memset(tb
, 0, sizeof tb
);
345 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
347 if (rtm
->rtm_flags
& RTM_F_CLONED
)
349 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
351 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
354 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
355 && h
->nlmsg_type
== RTM_NEWROUTE
) {
356 if (IS_ZEBRA_DEBUG_KERNEL
)
357 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
362 /* We don't care about change notifications for the MPLS table. */
363 /* TODO: Revisit this. */
364 if (rtm
->rtm_family
== AF_MPLS
)
367 /* Table corresponding to route. */
369 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
371 table
= rtm
->rtm_table
;
374 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
375 if (vrf_id
== VRF_DEFAULT
) {
376 if (!is_zebra_valid_kernel_table(table
)
377 && !is_zebra_main_routing_table(table
))
381 /* Route which inserted by Zebra. */
382 if (is_selfroute(rtm
->rtm_protocol
)) {
383 flags
|= ZEBRA_FLAG_SELFROUTE
;
384 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
387 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
390 dest
= RTA_DATA(tb
[RTA_DST
]);
395 src
= RTA_DATA(tb
[RTA_SRC
]);
400 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
403 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
405 if (tb
[RTA_PRIORITY
])
406 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
408 #if defined(SUPPORT_REALMS)
410 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
413 if (tb
[RTA_METRICS
]) {
414 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
416 memset(mxrta
, 0, sizeof mxrta
);
417 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
418 RTA_PAYLOAD(tb
[RTA_METRICS
]));
421 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
424 if (rtm
->rtm_family
== AF_INET
) {
426 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
428 "Invalid destination prefix length: %u received from kernel route change",
432 memcpy(&p
.u
.prefix4
, dest
, 4);
433 p
.prefixlen
= rtm
->rtm_dst_len
;
435 if (rtm
->rtm_src_len
!= 0) {
436 char buf
[PREFIX_STRLEN
];
438 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
439 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
440 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
444 /* Force debug below to not display anything for source */
446 } else if (rtm
->rtm_family
== AF_INET6
) {
448 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
450 "Invalid destination prefix length: %u received from kernel route change",
454 memcpy(&p
.u
.prefix6
, dest
, 16);
455 p
.prefixlen
= rtm
->rtm_dst_len
;
457 src_p
.family
= AF_INET6
;
458 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
460 "Invalid source prefix length: %u received from kernel route change",
464 memcpy(&src_p
.prefix
, src
, 16);
465 src_p
.prefixlen
= rtm
->rtm_src_len
;
469 * For ZEBRA_ROUTE_KERNEL types:
471 * The metric/priority of the route received from the kernel
472 * is a 32 bit number. We are going to interpret the high
473 * order byte as the Admin Distance and the low order 3 bytes
476 * This will allow us to do two things:
477 * 1) Allow the creation of kernel routes that can be
478 * overridden by zebra.
479 * 2) Allow the old behavior for 'most' kernel route types
480 * if a user enters 'ip route ...' v4 routes get a metric
481 * of 0 and v6 routes get a metric of 1024. Both of these
482 * values will end up with a admin distance of 0, which
483 * will cause them to win for the purposes of zebra.
485 if (proto
== ZEBRA_ROUTE_KERNEL
) {
486 distance
= (metric
>> 24) & 0xFF;
487 metric
= (metric
& 0x00FFFFFF);
490 if (IS_ZEBRA_DEBUG_KERNEL
) {
491 char buf
[PREFIX_STRLEN
];
492 char buf2
[PREFIX_STRLEN
];
493 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
494 nl_msg_type_to_str(h
->nlmsg_type
),
495 prefix2str(&p
, buf
, sizeof(buf
)),
496 src_p
.prefixlen
? " from " : "",
498 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
500 vrf_id
, table
, metric
, distance
);
504 if (rtm
->rtm_family
== AF_INET6
)
507 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
508 struct interface
*ifp
;
509 vrf_id_t nh_vrf_id
= vrf_id
;
511 if (!tb
[RTA_MULTIPATH
]) {
513 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
515 memset(&nh
, 0, sizeof(nh
));
517 if (bh_type
== BLACKHOLE_UNSPEC
) {
519 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
520 else if (index
&& gate
)
523 ? NEXTHOP_TYPE_IPV4_IFINDEX
524 : NEXTHOP_TYPE_IPV6_IFINDEX
;
525 else if (!index
&& gate
)
526 nh
.type
= (afi
== AFI_IP
)
530 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
531 nh
.bh_type
= bh_type
;
534 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
535 nh
.bh_type
= bh_type
;
539 memcpy(&nh
.src
, prefsrc
, sz
);
541 memcpy(&nh
.gate
, gate
, sz
);
544 ifp
= if_lookup_by_index_per_ns(
545 zebra_ns_lookup(ns_id
),
548 nh_vrf_id
= ifp
->vrf_id
;
550 nh
.vrf_id
= nh_vrf_id
;
552 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
553 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
554 == LWTUNNEL_ENCAP_MPLS
) {
556 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
559 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
560 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
563 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
566 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
567 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
569 /* This is a multipath route */
571 struct route_entry
*re
;
572 struct rtnexthop
*rtnh
=
573 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
575 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
577 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
579 re
->distance
= distance
;
586 re
->uptime
= time(NULL
);
590 struct nexthop
*nh
= NULL
;
592 if (len
< (int)sizeof(*rtnh
)
593 || rtnh
->rtnh_len
> len
)
596 index
= rtnh
->rtnh_ifindex
;
599 * Yes we are looking this up
600 * for every nexthop and just
601 * using the last one looked
604 ifp
= if_lookup_by_index_per_ns(
605 zebra_ns_lookup(ns_id
),
608 nh_vrf_id
= ifp
->vrf_id
;
611 EC_ZEBRA_UNKNOWN_INTERFACE
,
612 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
615 nh_vrf_id
= VRF_DEFAULT
;
621 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
622 memset(tb
, 0, sizeof(tb
));
623 netlink_parse_rtattr(
624 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
625 rtnh
->rtnh_len
- sizeof(*rtnh
));
629 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
630 && *(uint16_t *)RTA_DATA(
632 == LWTUNNEL_ENCAP_MPLS
) {
633 num_labels
= parse_encap_mpls(
634 tb
[RTA_ENCAP
], labels
);
639 if (rtm
->rtm_family
== AF_INET
) {
641 nh
= route_entry_nexthop_ipv4_ifindex_add(
646 nh
= route_entry_nexthop_ipv4_add(
650 } else if (rtm
->rtm_family
653 nh
= route_entry_nexthop_ipv6_ifindex_add(
657 nh
= route_entry_nexthop_ipv6_add(
662 nh
= route_entry_nexthop_ifindex_add(
663 re
, index
, nh_vrf_id
);
665 if (nh
&& num_labels
)
666 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
669 if (nh
&& (rtnh
->rtnh_flags
& RTNH_F_ONLINK
))
671 NEXTHOP_FLAG_ONLINK
);
673 if (rtnh
->rtnh_len
== 0)
676 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
677 rtnh
= RTNH_NEXT(rtnh
);
680 zserv_nexthop_num_warn(__func__
,
681 (const struct prefix
*)&p
,
683 if (re
->nexthop_num
== 0)
686 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
690 if (!tb
[RTA_MULTIPATH
]) {
692 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
694 memset(&nh
, 0, sizeof(nh
));
695 if (bh_type
== BLACKHOLE_UNSPEC
) {
697 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
698 else if (index
&& gate
)
701 ? NEXTHOP_TYPE_IPV4_IFINDEX
702 : NEXTHOP_TYPE_IPV6_IFINDEX
;
703 else if (!index
&& gate
)
704 nh
.type
= (afi
== AFI_IP
)
708 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
709 nh
.bh_type
= BLACKHOLE_UNSPEC
;
712 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
713 nh
.bh_type
= bh_type
;
717 memcpy(&nh
.gate
, gate
, sz
);
718 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
719 &p
, &src_p
, &nh
, table
, metric
, distance
,
722 /* XXX: need to compare the entire list of nexthops
723 * here for NLM_F_APPEND stupidity */
724 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
725 &p
, &src_p
, NULL
, table
, metric
, distance
,
733 static struct mcast_route_data
*mroute
= NULL
;
735 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
736 ns_id_t ns_id
, int startup
)
740 struct rtattr
*tb
[RTA_MAX
+ 1];
741 struct mcast_route_data
*m
;
742 struct mcast_route_data mr
;
749 char oif_list
[256] = "\0";
756 memset(&mr
, 0, sizeof(mr
));
762 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
764 memset(tb
, 0, sizeof tb
);
765 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
768 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
770 table
= rtm
->rtm_table
;
772 vrf
= vrf_lookup_by_table(table
, ns_id
);
775 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
778 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
781 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
784 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
786 if (tb
[RTA_MULTIPATH
]) {
787 struct rtnexthop
*rtnh
=
788 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
790 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
792 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
795 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
798 if (rtnh
->rtnh_len
== 0)
801 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
802 rtnh
= RTNH_NEXT(rtnh
);
806 if (IS_ZEBRA_DEBUG_KERNEL
) {
807 struct interface
*ifp
;
808 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
809 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
810 for (count
= 0; count
< oif_count
; count
++) {
811 ifp
= if_lookup_by_index(oif
[count
], vrf
);
814 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
816 strcat(oif_list
, temp
);
818 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
819 ifp
= if_lookup_by_index(iif
, vrf
);
820 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
821 zvrf
->vrf
->name
, vrf
,
822 nl_msg_type_to_str(h
->nlmsg_type
),
823 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
824 oif_list
, m
->lastused
);
829 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
836 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
837 /* If this is not route add/delete message print warning. */
838 zlog_debug("Kernel message: %s NS %u",
839 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
843 if (!(rtm
->rtm_family
== AF_INET
||
844 rtm
->rtm_family
== AF_INET6
||
845 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
847 EC_ZEBRA_UNKNOWN_FAMILY
,
848 "Invalid address family: %u received from kernel route change: %s",
849 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
853 /* Connected route. */
854 if (IS_ZEBRA_DEBUG_KERNEL
)
855 zlog_debug("%s %s %s proto %s NS %u",
856 nl_msg_type_to_str(h
->nlmsg_type
),
857 nl_family_to_str(rtm
->rtm_family
),
858 nl_rttype_to_str(rtm
->rtm_type
),
859 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
862 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
864 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
867 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
871 if (rtm
->rtm_type
== RTN_MULTICAST
)
872 netlink_route_change_read_multicast(h
, ns_id
, startup
);
874 netlink_route_change_read_unicast(h
, ns_id
, startup
);
878 /* Request for specific route information from the kernel */
879 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
886 /* Form the request, specifying filter (rtattr) if needed. */
887 memset(&req
, 0, sizeof(req
));
888 req
.n
.nlmsg_type
= type
;
889 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
890 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
891 req
.rtm
.rtm_family
= family
;
893 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
896 /* Routing table read function using netlink interface. Only called
898 int netlink_route_read(struct zebra_ns
*zns
)
901 struct zebra_dplane_info dp_info
;
903 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
905 /* Get IPv4 routing table. */
906 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
909 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
910 &zns
->netlink_cmd
, &dp_info
, 0, 1);
914 /* Get IPv6 routing table. */
915 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
918 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
919 &zns
->netlink_cmd
, &dp_info
, 0, 1);
926 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
928 struct nlmsghdr
*nlmsg
,
929 size_t req_size
, int bytelen
,
930 const struct nexthop
*nexthop
)
932 if (route_family
== AF_MPLS
) {
933 struct gw_family_t gw_fam
;
935 gw_fam
.family
= gw_family
;
936 if (gw_family
== AF_INET
)
937 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
939 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
940 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
943 if (gw_family
== AF_INET
)
944 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
945 &nexthop
->gate
.ipv4
, bytelen
);
947 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
948 &nexthop
->gate
.ipv6
, bytelen
);
952 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
955 struct rtnexthop
*rtnh
,
956 size_t req_size
, int bytelen
,
957 const struct nexthop
*nexthop
)
959 if (route_family
== AF_MPLS
) {
960 struct gw_family_t gw_fam
;
962 gw_fam
.family
= gw_family
;
963 if (gw_family
== AF_INET
)
964 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
966 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
967 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
969 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
971 if (gw_family
== AF_INET
)
972 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
973 &nexthop
->gate
.ipv4
, bytelen
);
975 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
976 &nexthop
->gate
.ipv6
, bytelen
);
977 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
981 /* This function takes a nexthop as argument and adds
982 * the appropriate netlink attributes to an existing
985 * @param routedesc: Human readable description of route type
986 * (direct/recursive, single-/multipath)
987 * @param bytelen: Length of addresses in bytes.
988 * @param nexthop: Nexthop information
989 * @param nlmsg: nlmsghdr structure to fill in.
990 * @param req_size: The size allocated for the message.
992 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
993 const struct nexthop
*nexthop
,
994 struct nlmsghdr
*nlmsg
,
996 size_t req_size
, int cmd
)
998 struct mpls_label_stack
*nh_label
;
999 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1001 char label_buf
[256];
1004 * label_buf is *only* currently used within debugging.
1005 * As such when we assign it we are guarding it inside
1006 * a debug test. If you want to change this make sure
1007 * you fix this assumption
1009 label_buf
[0] = '\0';
1012 for (const struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1013 char label_buf1
[20];
1015 nh_label
= nh
->nh_label
;
1016 if (!nh_label
|| !nh_label
->num_labels
)
1019 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1020 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1023 if (IS_ZEBRA_DEBUG_KERNEL
) {
1025 sprintf(label_buf
, "label %u",
1026 nh_label
->label
[i
]);
1028 sprintf(label_buf1
, "/%u",
1029 nh_label
->label
[i
]);
1030 strlcat(label_buf
, label_buf1
,
1035 out_lse
[num_labels
] =
1036 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1042 /* Set the BoS bit */
1043 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1045 if (rtmsg
->rtm_family
== AF_MPLS
)
1046 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1047 num_labels
* sizeof(mpls_lse_t
));
1049 struct rtattr
*nest
;
1050 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1052 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1054 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1055 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1056 num_labels
* sizeof(mpls_lse_t
));
1057 addattr_nest_end(nlmsg
, nest
);
1061 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1062 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1064 if (rtmsg
->rtm_family
== AF_INET
1065 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1066 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1067 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1068 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1069 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1071 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1072 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1073 &nexthop
->rmap_src
.ipv4
, bytelen
);
1074 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1075 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1076 &nexthop
->src
.ipv4
, bytelen
);
1078 if (IS_ZEBRA_DEBUG_KERNEL
)
1080 " 5549: _netlink_route_build_singlepath() (%s): "
1081 "nexthop via %s %s if %u(%u)",
1082 routedesc
, ipv4_ll_buf
, label_buf
,
1083 nexthop
->ifindex
, nexthop
->vrf_id
);
1087 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1088 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1089 /* Send deletes to the kernel without specifying the next-hop */
1090 if (cmd
!= RTM_DELROUTE
)
1091 _netlink_route_nl_add_gateway_info(
1092 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1095 if (cmd
== RTM_NEWROUTE
) {
1096 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1097 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1098 &nexthop
->rmap_src
.ipv4
, bytelen
);
1099 else if (nexthop
->src
.ipv4
.s_addr
)
1100 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1101 &nexthop
->src
.ipv4
, bytelen
);
1104 if (IS_ZEBRA_DEBUG_KERNEL
)
1106 "netlink_route_multipath() (%s): "
1107 "nexthop via %s %s if %u(%u)",
1108 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1109 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1112 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1113 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1114 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1115 nlmsg
, req_size
, bytelen
,
1118 if (cmd
== RTM_NEWROUTE
) {
1119 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1120 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1121 &nexthop
->rmap_src
.ipv6
, bytelen
);
1122 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1123 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1124 &nexthop
->src
.ipv6
, bytelen
);
1127 if (IS_ZEBRA_DEBUG_KERNEL
)
1129 "netlink_route_multipath() (%s): "
1130 "nexthop via %s %s if %u(%u)",
1131 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1132 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1136 * We have the ifindex so we should always send it
1137 * This is especially useful if we are doing route
1140 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1141 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1143 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1144 if (cmd
== RTM_NEWROUTE
) {
1145 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1146 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1147 &nexthop
->rmap_src
.ipv4
, bytelen
);
1148 else if (nexthop
->src
.ipv4
.s_addr
)
1149 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1150 &nexthop
->src
.ipv4
, bytelen
);
1153 if (IS_ZEBRA_DEBUG_KERNEL
)
1155 "netlink_route_multipath() (%s): "
1156 "nexthop via if %u(%u)",
1157 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1161 /* This function takes a nexthop as argument and
1162 * appends to the given rtattr/rtnexthop pair the
1163 * representation of the nexthop. If the nexthop
1164 * defines a preferred source, the src parameter
1165 * will be modified to point to that src, otherwise
1166 * it will be kept unmodified.
1168 * @param routedesc: Human readable description of route type
1169 * (direct/recursive, single-/multipath)
1170 * @param bytelen: Length of addresses in bytes.
1171 * @param nexthop: Nexthop information
1172 * @param rta: rtnetlink attribute structure
1173 * @param rtnh: pointer to an rtnetlink nexthop structure
1174 * @param src: pointer pointing to a location where
1175 * the prefsrc should be stored.
1177 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1178 const struct nexthop
*nexthop
,
1180 struct rtnexthop
*rtnh
,
1181 struct rtmsg
*rtmsg
,
1182 const union g_addr
**src
)
1184 struct mpls_label_stack
*nh_label
;
1185 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1187 char label_buf
[256];
1189 rtnh
->rtnh_len
= sizeof(*rtnh
);
1190 rtnh
->rtnh_flags
= 0;
1191 rtnh
->rtnh_hops
= 0;
1192 rta
->rta_len
+= rtnh
->rtnh_len
;
1195 * label_buf is *only* currently used within debugging.
1196 * As such when we assign it we are guarding it inside
1197 * a debug test. If you want to change this make sure
1198 * you fix this assumption
1200 label_buf
[0] = '\0';
1203 for (const struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1204 char label_buf1
[20];
1206 nh_label
= nh
->nh_label
;
1207 if (!nh_label
|| !nh_label
->num_labels
)
1210 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1211 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1214 if (IS_ZEBRA_DEBUG_KERNEL
) {
1216 sprintf(label_buf
, "label %u",
1217 nh_label
->label
[i
]);
1219 sprintf(label_buf1
, "/%u",
1220 nh_label
->label
[i
]);
1221 strlcat(label_buf
, label_buf1
,
1226 out_lse
[num_labels
] =
1227 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1233 /* Set the BoS bit */
1234 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1236 if (rtmsg
->rtm_family
== AF_MPLS
) {
1237 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1239 num_labels
* sizeof(mpls_lse_t
));
1241 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1243 struct rtattr
*nest
;
1244 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1245 int len
= rta
->rta_len
;
1247 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1248 &encap
, sizeof(uint16_t));
1249 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1250 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1252 num_labels
* sizeof(mpls_lse_t
));
1253 rta_nest_end(rta
, nest
);
1254 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1258 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1259 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1261 if (rtmsg
->rtm_family
== AF_INET
1262 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1263 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1265 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1266 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1268 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1269 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1271 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1272 *src
= &nexthop
->rmap_src
;
1273 else if (nexthop
->src
.ipv4
.s_addr
)
1274 *src
= &nexthop
->src
;
1276 if (IS_ZEBRA_DEBUG_KERNEL
)
1278 " 5549: netlink_route_build_multipath() (%s): "
1279 "nexthop via %s %s if %u",
1280 routedesc
, ipv4_ll_buf
, label_buf
,
1285 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1286 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1287 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1288 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1290 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1291 *src
= &nexthop
->rmap_src
;
1292 else if (nexthop
->src
.ipv4
.s_addr
)
1293 *src
= &nexthop
->src
;
1295 if (IS_ZEBRA_DEBUG_KERNEL
)
1297 "netlink_route_multipath() (%s): "
1298 "nexthop via %s %s if %u",
1299 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1300 label_buf
, nexthop
->ifindex
);
1302 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1303 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1304 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1305 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1308 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1309 *src
= &nexthop
->rmap_src
;
1310 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1311 *src
= &nexthop
->src
;
1313 if (IS_ZEBRA_DEBUG_KERNEL
)
1315 "netlink_route_multipath() (%s): "
1316 "nexthop via %s %s if %u",
1317 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1318 label_buf
, nexthop
->ifindex
);
1322 * We have figured out the ifindex so we should always send it
1323 * This is especially useful if we are doing route
1326 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1327 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1330 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1331 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1332 *src
= &nexthop
->rmap_src
;
1333 else if (nexthop
->src
.ipv4
.s_addr
)
1334 *src
= &nexthop
->src
;
1336 if (IS_ZEBRA_DEBUG_KERNEL
)
1338 "netlink_route_multipath() (%s): "
1339 "nexthop via if %u",
1340 routedesc
, nexthop
->ifindex
);
1344 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1345 const zebra_nhlfe_t
*nhlfe
,
1346 struct nlmsghdr
*nlmsg
,
1347 struct rtmsg
*rtmsg
,
1348 size_t req_size
, int cmd
)
1353 family
= NHLFE_FAMILY(nhlfe
);
1354 bytelen
= (family
== AF_INET
? 4 : 16);
1355 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1356 nlmsg
, rtmsg
, req_size
, cmd
);
1361 _netlink_mpls_build_multipath(const char *routedesc
, const zebra_nhlfe_t
*nhlfe
,
1362 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1363 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1368 family
= NHLFE_FAMILY(nhlfe
);
1369 bytelen
= (family
== AF_INET
? 4 : 16);
1370 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1375 /* Log debug information for netlink_route_multipath
1376 * if debug logging is enabled.
1378 * @param cmd: Netlink command which is to be processed
1379 * @param p: Prefix for which the change is due
1380 * @param family: Address family which the change concerns
1381 * @param zvrf: The vrf we are in
1382 * @param tableid: The table we are working on
1384 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1385 int family
, vrf_id_t vrfid
,
1388 if (IS_ZEBRA_DEBUG_KERNEL
) {
1389 char buf
[PREFIX_STRLEN
];
1391 "netlink_route_multipath(): %s %s vrf %u(%u)",
1392 nl_msg_type_to_str(cmd
),
1393 prefix2str(p
, buf
, sizeof(buf
)),
1398 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1400 if (IS_ZEBRA_DEBUG_KERNEL
)
1401 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1402 nl_msg_type_to_str(cmd
), label
);
1405 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1406 int llalen
, ns_id_t ns_id
)
1414 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1416 memset(&req
, 0, sizeof(req
));
1418 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1419 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1420 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1421 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1423 req
.ndm
.ndm_family
= AF_INET
;
1424 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1425 req
.ndm
.ndm_ifindex
= ifindex
;
1426 req
.ndm
.ndm_type
= RTN_UNICAST
;
1428 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1429 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1431 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1436 * Routing table change via netlink interface, using a dataplane context object
1438 static int netlink_route_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
1441 struct nexthop
*nexthop
= NULL
;
1442 unsigned int nexthop_num
;
1444 const char *routedesc
;
1447 const struct prefix
*p
, *src_p
;
1453 char buf
[NL_PKT_BUF_SIZE
];
1456 p
= dplane_ctx_get_dest(ctx
);
1457 src_p
= dplane_ctx_get_src(ctx
);
1459 family
= PREFIX_FAMILY(p
);
1461 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1463 bytelen
= (family
== AF_INET
? 4 : 16);
1465 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1466 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1468 if ((cmd
== RTM_NEWROUTE
) &&
1469 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1470 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1472 req
.n
.nlmsg_type
= cmd
;
1474 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1476 req
.r
.rtm_family
= family
;
1477 req
.r
.rtm_dst_len
= p
->prefixlen
;
1478 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1479 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1481 if (cmd
== RTM_DELROUTE
)
1482 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1484 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1487 * blackhole routes are not RTN_UNICAST, they are
1488 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1489 * so setting this value as a RTN_UNICAST would
1490 * cause the route lookup of just the prefix
1491 * to fail. So no need to specify this for
1492 * the RTM_DELROUTE case
1494 if (cmd
!= RTM_DELROUTE
)
1495 req
.r
.rtm_type
= RTN_UNICAST
;
1497 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1499 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1503 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1505 * either by the kernel or by zebra. Its purely for calculating best
1507 * by the routing protocol and for communicating with protocol peers.
1509 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1511 #if defined(SUPPORT_REALMS)
1515 if (cmd
== RTM_DELROUTE
)
1516 tag
= dplane_ctx_get_old_tag(ctx
);
1518 tag
= dplane_ctx_get_tag(ctx
);
1520 if (tag
> 0 && tag
<= 255)
1521 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1524 /* Table corresponding to this route. */
1525 table_id
= dplane_ctx_get_table(ctx
);
1527 req
.r
.rtm_table
= table_id
;
1529 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1530 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1533 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1536 * If we are not updating the route and we have received
1537 * a route delete, then all we need to fill in is the
1538 * prefix information to tell the kernel to schwack
1541 if (cmd
== RTM_DELROUTE
)
1544 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1545 char buf
[NL_PKT_BUF_SIZE
];
1546 struct rtattr
*rta
= (void *)buf
;
1547 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1548 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1550 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1552 rta
->rta_type
= RTA_METRICS
;
1553 rta
->rta_len
= RTA_LENGTH(0);
1554 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1555 RTAX_MTU
, &mtu
, sizeof(mtu
));
1556 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1560 /* Count overall nexthops so we can decide whether to use singlepath
1561 * or multipath case.
1564 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1565 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1567 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1573 /* Singlepath case. */
1574 if (nexthop_num
== 1) {
1576 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1578 * So we want to cover 2 types of blackhole
1580 * 1) A normal blackhole route( ala from a static
1582 * 2) A recursively resolved blackhole route
1584 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1585 switch (nexthop
->bh_type
) {
1586 case BLACKHOLE_ADMINPROHIB
:
1587 req
.r
.rtm_type
= RTN_PROHIBIT
;
1589 case BLACKHOLE_REJECT
:
1590 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1593 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1598 if (CHECK_FLAG(nexthop
->flags
,
1599 NEXTHOP_FLAG_RECURSIVE
)) {
1604 if (family
== AF_INET
) {
1605 if (nexthop
->rmap_src
.ipv4
.s_addr
1608 nexthop
->rmap_src
.ipv4
;
1610 } else if (nexthop
->src
.ipv4
.s_addr
1616 } else if (family
== AF_INET6
) {
1617 if (!IN6_IS_ADDR_UNSPECIFIED(
1618 &nexthop
->rmap_src
.ipv6
)) {
1620 nexthop
->rmap_src
.ipv6
;
1623 !IN6_IS_ADDR_UNSPECIFIED(
1624 &nexthop
->src
.ipv6
)) {
1633 if ((cmd
== RTM_NEWROUTE
1634 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1635 routedesc
= nexthop
->rparent
1636 ? "recursive, single-path"
1639 _netlink_route_build_singlepath(
1640 routedesc
, bytelen
, nexthop
, &req
.n
,
1641 &req
.r
, sizeof(req
), cmd
);
1646 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1647 if (family
== AF_INET
)
1648 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1649 &src
.ipv4
, bytelen
);
1650 else if (family
== AF_INET6
)
1651 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1652 &src
.ipv6
, bytelen
);
1654 } else { /* Multipath case */
1655 char buf
[NL_PKT_BUF_SIZE
];
1656 struct rtattr
*rta
= (void *)buf
;
1657 struct rtnexthop
*rtnh
;
1658 const union g_addr
*src1
= NULL
;
1660 rta
->rta_type
= RTA_MULTIPATH
;
1661 rta
->rta_len
= RTA_LENGTH(0);
1662 rtnh
= RTA_DATA(rta
);
1665 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1666 if (CHECK_FLAG(nexthop
->flags
,
1667 NEXTHOP_FLAG_RECURSIVE
)) {
1668 /* This only works for IPv4 now */
1672 if (family
== AF_INET
) {
1673 if (nexthop
->rmap_src
.ipv4
.s_addr
1676 nexthop
->rmap_src
.ipv4
;
1678 } else if (nexthop
->src
.ipv4
.s_addr
1684 } else if (family
== AF_INET6
) {
1685 if (!IN6_IS_ADDR_UNSPECIFIED(
1686 &nexthop
->rmap_src
.ipv6
)) {
1688 nexthop
->rmap_src
.ipv6
;
1691 !IN6_IS_ADDR_UNSPECIFIED(
1692 &nexthop
->src
.ipv6
)) {
1702 if ((cmd
== RTM_NEWROUTE
1703 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1704 routedesc
= nexthop
->rparent
1705 ? "recursive, multipath"
1709 _netlink_route_build_multipath(
1710 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1712 rtnh
= RTNH_NEXT(rtnh
);
1714 if (!setsrc
&& src1
) {
1715 if (family
== AF_INET
)
1716 src
.ipv4
= src1
->ipv4
;
1717 else if (family
== AF_INET6
)
1718 src
.ipv6
= src1
->ipv6
;
1724 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1725 if (family
== AF_INET
)
1726 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1727 &src
.ipv4
, bytelen
);
1728 else if (family
== AF_INET6
)
1729 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1730 &src
.ipv6
, bytelen
);
1731 if (IS_ZEBRA_DEBUG_KERNEL
)
1732 zlog_debug("Setting source");
1735 if (rta
->rta_len
> RTA_LENGTH(0))
1736 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1737 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1740 /* If there is no useful nexthop then return. */
1741 if (nexthop_num
== 0) {
1742 if (IS_ZEBRA_DEBUG_KERNEL
)
1744 "netlink_route_multipath(): No useful nexthop.");
1749 /* Talk to netlink socket. */
1750 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
1751 dplane_ctx_get_ns(ctx
), 0);
1754 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1756 uint32_t actual_table
;
1758 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1766 struct zebra_ns
*zns
;
1769 memset(&req
, 0, sizeof(req
));
1771 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1772 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1773 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1775 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1776 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1778 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1779 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1780 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1781 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1785 * So during the namespace cleanup we started storing
1786 * the zvrf table_id for the default table as RT_TABLE_MAIN
1787 * which is what the normal routing table for ip routing is.
1788 * This change caused this to break our lookups of sg data
1789 * because prior to this change the zvrf->table_id was 0
1790 * and when the pim multicast kernel code saw a 0,
1791 * it was auto-translated to RT_TABLE_DEFAULT. But since
1792 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1793 * and the kernel goes screw you and the delicious cookies you
1794 * are trying to give me. So now we have this little hack.
1796 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
1798 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
1800 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1801 &zns
->netlink_cmd
, zns
, 0);
1808 * Update or delete a prefix from the kernel,
1809 * using info from a dataplane context.
1811 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
1814 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
1815 struct nexthop
*nexthop
;
1817 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
1819 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
1821 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
1823 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
1824 /* Single 'replace' operation */
1828 * So v6 route replace semantics are not in
1829 * the kernel at this point as I understand it.
1830 * so let's do a delete then an add.
1831 * In the future once v6 route replace semantics
1832 * are in we can figure out what to do here to
1833 * allow working with old and new kernels.
1835 * I'm also intentionally ignoring the failure case
1836 * of the route delete. If that happens yeah we're
1839 (void)netlink_route_multipath(RTM_DELROUTE
, ctx
);
1844 return ZEBRA_DPLANE_REQUEST_FAILURE
;
1847 ret
= netlink_route_multipath(cmd
, ctx
);
1848 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
1849 /* Update installed nexthops to signal which have been
1852 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1853 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1856 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1857 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1863 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
1866 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1867 int llalen
, ns_id_t ns_id
)
1869 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1870 addr
, lla
, llalen
, ns_id
);
1874 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1875 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1877 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1878 struct in_addr
*vtep_ip
, int cmd
)
1880 struct zebra_ns
*zns
;
1886 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1887 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1890 memset(&req
, 0, sizeof(req
));
1892 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1893 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1894 if (cmd
== RTM_NEWNEIGH
)
1895 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1896 req
.n
.nlmsg_type
= cmd
;
1897 req
.ndm
.ndm_family
= PF_BRIDGE
;
1898 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1899 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1902 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1903 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1904 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1906 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1911 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1913 * a "flood" MAC FDB entry.
1915 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1917 if (IS_ZEBRA_DEBUG_VXLAN
)
1918 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1919 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1921 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1925 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1926 * deleting the "flood" MAC FDB entry.
1928 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1930 if (IS_ZEBRA_DEBUG_VXLAN
)
1932 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1933 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1935 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1939 #define NDA_RTA(r) \
1940 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1943 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1946 struct interface
*ifp
;
1947 struct zebra_if
*zif
;
1948 struct rtattr
*tb
[NDA_MAX
+ 1];
1949 struct interface
*br_if
;
1952 struct prefix vtep_ip
;
1953 int vid_present
= 0, dst_present
= 0;
1954 char buf
[ETHER_ADDR_STRLEN
];
1959 ndm
= NLMSG_DATA(h
);
1961 /* We only process macfdb notifications if EVPN is enabled */
1962 if (!is_evpn_enabled())
1965 /* The interface should exist. */
1966 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1968 if (!ifp
|| !ifp
->info
) {
1969 if (IS_ZEBRA_DEBUG_KERNEL
)
1970 zlog_debug("\t%s without associated interface: %u",
1971 __PRETTY_FUNCTION__
, ndm
->ndm_ifindex
);
1975 /* The interface should be something we're interested in. */
1976 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
)) {
1977 if (IS_ZEBRA_DEBUG_KERNEL
)
1978 zlog_debug("\t%s Not interested in %s, not a slave",
1979 __PRETTY_FUNCTION__
, ifp
->name
);
1983 /* Drop "permanent" entries. */
1984 if (ndm
->ndm_state
& NUD_PERMANENT
) {
1985 if (IS_ZEBRA_DEBUG_KERNEL
)
1986 zlog_debug("\t%s Entry is PERMANENT, dropping",
1987 __PRETTY_FUNCTION__
);
1991 zif
= (struct zebra_if
*)ifp
->info
;
1992 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1993 if (IS_ZEBRA_DEBUG_KERNEL
)
1995 "%s family %s IF %s(%u) brIF %u - no bridge master",
1996 nl_msg_type_to_str(h
->nlmsg_type
),
1997 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1999 zif
->brslave_info
.bridge_ifindex
);
2003 /* Parse attributes and extract fields of interest. */
2004 memset(tb
, 0, sizeof tb
);
2005 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2007 if (!tb
[NDA_LLADDR
]) {
2008 if (IS_ZEBRA_DEBUG_KERNEL
)
2009 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2010 nl_msg_type_to_str(h
->nlmsg_type
),
2011 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2013 zif
->brslave_info
.bridge_ifindex
);
2017 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2018 if (IS_ZEBRA_DEBUG_KERNEL
)
2020 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2021 nl_msg_type_to_str(h
->nlmsg_type
),
2022 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2024 zif
->brslave_info
.bridge_ifindex
,
2025 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2029 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2031 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2033 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2034 sprintf(vid_buf
, " VLAN %u", vid
);
2038 /* TODO: Only IPv4 supported now. */
2040 vtep_ip
.family
= AF_INET
;
2041 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2042 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2044 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2047 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2049 if (IS_ZEBRA_DEBUG_KERNEL
)
2050 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2051 nl_msg_type_to_str(h
->nlmsg_type
),
2052 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2053 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2054 sticky
? "sticky " : "",
2055 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2056 dst_present
? dst_buf
: "");
2058 if (filter_vlan
&& vid
!= filter_vlan
) {
2059 if (IS_ZEBRA_DEBUG_KERNEL
)
2060 zlog_debug("\tFiltered due to filter vlan: %d",
2065 /* If add or update, do accordingly if learnt on a "local" interface; if
2066 * the notification is over VxLAN, this has to be related to
2068 * so perform an implicit delete of any local entry (if it exists).
2070 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2071 if (IS_ZEBRA_IF_VXLAN(ifp
))
2072 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2075 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2079 /* This is a delete notification.
2080 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2081 * 2. For a MAC over "local" interface, delete the mac
2082 * Note: We will get notifications from both bridge driver and VxLAN
2084 * Ignore the notification from VxLan driver as it is also generated
2085 * when mac moves from remote to local.
2088 if (IS_ZEBRA_DEBUG_KERNEL
)
2089 zlog_debug("\tNo Destination Present");
2093 if (IS_ZEBRA_IF_VXLAN(ifp
))
2094 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2097 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2100 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2105 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2108 /* Length validity. */
2109 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2113 /* We are interested only in AF_BRIDGE notifications. */
2114 ndm
= NLMSG_DATA(h
);
2115 if (ndm
->ndm_family
!= AF_BRIDGE
)
2118 return netlink_macfdb_change(h
, len
, ns_id
);
2121 /* Request for MAC FDB information from the kernel */
2122 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2123 int type
, ifindex_t master_ifindex
)
2127 struct ifinfomsg ifm
;
2131 /* Form the request, specifying filter (rtattr) if needed. */
2132 memset(&req
, 0, sizeof(req
));
2133 req
.n
.nlmsg_type
= type
;
2134 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2135 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2136 req
.ifm
.ifi_family
= family
;
2138 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2140 return netlink_request(netlink_cmd
, &req
.n
);
2144 * MAC forwarding database read using netlink interface. This is invoked
2147 int netlink_macfdb_read(struct zebra_ns
*zns
)
2150 struct zebra_dplane_info dp_info
;
2152 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2154 /* Get bridge FDB table. */
2155 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2159 /* We are reading entire table. */
2161 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2168 * MAC forwarding database read using netlink interface. This is for a
2169 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2171 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2172 struct interface
*br_if
)
2174 struct zebra_if
*br_zif
;
2175 struct zebra_if
*zif
;
2176 struct zebra_l2info_vxlan
*vxl
;
2177 struct zebra_dplane_info dp_info
;
2180 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2182 /* Save VLAN we're filtering on, if needed. */
2183 br_zif
= (struct zebra_if
*)br_if
->info
;
2184 zif
= (struct zebra_if
*)ifp
->info
;
2185 vxl
= &zif
->l2info
.vxl
;
2186 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2187 filter_vlan
= vxl
->access_vlan
;
2189 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2191 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2195 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2198 /* Reset VLAN filter. */
2204 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2205 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
2208 struct interface
*br_if
,
2209 struct ethaddr
*mac
,
2217 struct zebra_if
*br_zif
;
2218 char buf
[ETHER_ADDR_STRLEN
];
2220 memset(&req
, 0, sizeof(req
));
2221 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2222 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2223 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2224 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
2225 /* req.ndm.ndm_state = NUD_REACHABLE; */
2227 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2229 br_zif
= (struct zebra_if
*)br_if
->info
;
2230 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
2231 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2233 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2235 if (IS_ZEBRA_DEBUG_KERNEL
)
2236 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2237 __PRETTY_FUNCTION__
,
2238 nl_family_to_str(req
.ndm
.ndm_family
), br_if
->name
,
2240 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
2242 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2245 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
2246 struct interface
*br_if
,
2247 struct ethaddr
*mac
, vlanid_t vid
)
2250 struct zebra_dplane_info dp_info
;
2252 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2254 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2256 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
2262 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2267 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2268 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2269 int cmd
, bool sticky
)
2271 struct zebra_ns
*zns
;
2278 struct zebra_if
*zif
;
2279 struct interface
*br_if
;
2280 struct zebra_if
*br_zif
;
2281 char buf
[ETHER_ADDR_STRLEN
];
2282 int vid_present
= 0;
2285 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2289 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2290 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2291 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2296 memset(&req
, 0, sizeof(req
));
2298 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2299 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2300 if (cmd
== RTM_NEWNEIGH
)
2301 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2302 req
.n
.nlmsg_type
= cmd
;
2303 req
.ndm
.ndm_family
= AF_BRIDGE
;
2304 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2305 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2308 req
.ndm
.ndm_state
|= NUD_NOARP
;
2310 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2312 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2313 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2314 dst_alen
= 4; // TODO: hardcoded
2315 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2316 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2317 br_zif
= (struct zebra_if
*)br_if
->info
;
2318 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2319 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2321 sprintf(vid_buf
, " VLAN %u", vid
);
2323 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2325 if (IS_ZEBRA_DEBUG_KERNEL
)
2326 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2327 nl_msg_type_to_str(cmd
),
2328 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2329 ifp
->ifindex
, vid_present
? vid_buf
: "",
2330 sticky
? "sticky " : "",
2331 prefix_mac2str(mac
, buf
, sizeof(buf
)), dst_buf
);
2333 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2338 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2339 * 5549 support, re-install them.
2341 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
2342 struct interface
*ifp
, struct ipaddr
*ip
)
2344 if (ndm
->ndm_family
!= AF_INET
)
2347 if (!zif
->v6_2_v4_ll_neigh_entry
)
2350 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
2353 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2357 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2360 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2363 struct interface
*ifp
;
2364 struct zebra_if
*zif
;
2365 struct rtattr
*tb
[NDA_MAX
+ 1];
2366 struct interface
*link_if
;
2369 char buf
[ETHER_ADDR_STRLEN
];
2370 char buf2
[INET6_ADDRSTRLEN
];
2371 int mac_present
= 0;
2375 ndm
= NLMSG_DATA(h
);
2377 /* The interface should exist. */
2378 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2380 if (!ifp
|| !ifp
->info
)
2383 zif
= (struct zebra_if
*)ifp
->info
;
2385 /* Parse attributes and extract fields of interest. */
2386 memset(tb
, 0, sizeof tb
);
2387 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2390 zlog_debug("%s family %s IF %s(%u) - no DST",
2391 nl_msg_type_to_str(h
->nlmsg_type
),
2392 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2397 memset(&ip
, 0, sizeof(struct ipaddr
));
2398 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2399 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2401 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
2402 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
2403 netlink_handle_5549(ndm
, zif
, ifp
, &ip
);
2404 if (IS_ZEBRA_DEBUG_KERNEL
)
2406 "\tNeighbor Entry Received is a 5549 entry, finished");
2410 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
2411 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
2412 netlink_handle_5549(ndm
, zif
, ifp
, &ip
);
2414 /* The neighbor is present on an SVI. From this, we locate the
2416 * bridge because we're only interested in neighbors on a VxLAN bridge.
2417 * The bridge is located based on the nature of the SVI:
2418 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2420 * and is linked to the bridge
2421 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2425 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2426 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2430 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2433 if (IS_ZEBRA_DEBUG_KERNEL
)
2435 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
2439 memset(&mac
, 0, sizeof(struct ethaddr
));
2440 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2441 if (tb
[NDA_LLADDR
]) {
2442 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2443 if (IS_ZEBRA_DEBUG_KERNEL
)
2445 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2450 ifp
->name
, ndm
->ndm_ifindex
,
2451 (unsigned long)RTA_PAYLOAD(
2457 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2460 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
2461 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
2463 if (IS_ZEBRA_DEBUG_KERNEL
)
2465 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2466 nl_msg_type_to_str(h
->nlmsg_type
),
2467 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2469 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2471 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2473 ndm
->ndm_state
, ndm
->ndm_flags
);
2475 /* If the neighbor state is valid for use, process as an add or
2477 * else process as a delete. Note that the delete handling may
2479 * in re-adding the neighbor if it is a valid "remote" neighbor.
2481 if (ndm
->ndm_state
& NUD_VALID
)
2482 return zebra_vxlan_handle_kernel_neigh_update(
2483 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2486 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2489 if (IS_ZEBRA_DEBUG_KERNEL
)
2490 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2491 nl_msg_type_to_str(h
->nlmsg_type
),
2492 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2494 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2496 /* Process the delete - it may result in re-adding the neighbor if it is
2497 * a valid "remote" neighbor.
2499 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2502 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2507 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2510 /* Length validity. */
2511 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2515 /* We are interested only in AF_INET or AF_INET6 notifications. */
2516 ndm
= NLMSG_DATA(h
);
2517 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2520 return netlink_neigh_change(h
, len
);
2523 /* Request for IP neighbor information from the kernel */
2524 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
2525 int type
, ifindex_t ifindex
)
2533 /* Form the request, specifying filter (rtattr) if needed. */
2534 memset(&req
, 0, sizeof(req
));
2535 req
.n
.nlmsg_type
= type
;
2536 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2537 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2538 req
.ndm
.ndm_family
= family
;
2540 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2542 return netlink_request(netlink_cmd
, &req
.n
);
2546 * IP Neighbor table read using netlink interface. This is invoked
2549 int netlink_neigh_read(struct zebra_ns
*zns
)
2552 struct zebra_dplane_info dp_info
;
2554 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2556 /* Get IP neighbor table. */
2557 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2561 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2568 * IP Neighbor table read using netlink interface. This is for a specific
2571 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2574 struct zebra_dplane_info dp_info
;
2576 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2578 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2582 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2589 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
2590 * read using netlink interface.
2592 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
2593 int type
, struct ipaddr
*ip
,
2603 /* Form the request, specifying filter (rtattr) if needed. */
2604 memset(&req
, 0, sizeof(req
));
2605 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2606 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2607 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
2608 req
.ndm
.ndm_ifindex
= ifindex
;
2610 if (IS_IPADDR_V4(ip
)) {
2611 ipa_len
= IPV4_MAX_BYTELEN
;
2612 req
.ndm
.ndm_family
= AF_INET
;
2615 ipa_len
= IPV6_MAX_BYTELEN
;
2616 req
.ndm
.ndm_family
= AF_INET6
;
2619 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2621 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2624 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
2625 struct interface
*vlan_if
)
2628 struct zebra_ns
*zns
;
2629 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
2630 char buf
[INET6_ADDRSTRLEN
];
2631 struct zebra_dplane_info dp_info
;
2635 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2637 if (IS_ZEBRA_DEBUG_KERNEL
)
2638 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
2639 __PRETTY_FUNCTION__
, vlan_if
->name
,
2641 ipaddr2str(ip
, buf
, sizeof(buf
)),
2644 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
2649 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2655 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2660 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2663 /* Length validity. */
2664 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2666 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2667 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2668 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2672 /* Is this a notification for the MAC FDB or IP neighbor table? */
2673 ndm
= NLMSG_DATA(h
);
2674 if (ndm
->ndm_family
== AF_BRIDGE
)
2675 return netlink_macfdb_change(h
, len
, ns_id
);
2677 if (ndm
->ndm_type
!= RTN_UNICAST
)
2680 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2681 return netlink_ipneigh_change(h
, len
, ns_id
);
2684 EC_ZEBRA_UNKNOWN_FAMILY
,
2685 "Invalid address family: %u received from kernel neighbor change: %s",
2686 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
2693 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2694 struct ethaddr
*mac
, uint8_t flags
,
2695 uint16_t state
, int cmd
)
2704 struct zebra_ns
*zns
;
2705 char buf
[INET6_ADDRSTRLEN
];
2706 char buf2
[ETHER_ADDR_STRLEN
];
2707 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2710 memset(&req
, 0, sizeof(req
));
2712 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2713 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2714 if (cmd
== RTM_NEWNEIGH
)
2715 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2716 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2717 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2718 req
.ndm
.ndm_state
= state
;
2719 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2720 req
.ndm
.ndm_type
= RTN_UNICAST
;
2721 req
.ndm
.ndm_flags
= flags
;
2723 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2724 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2726 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2728 if (IS_ZEBRA_DEBUG_KERNEL
)
2729 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
2730 nl_msg_type_to_str(cmd
),
2731 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2732 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2733 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2734 : "null", flags
, state
);
2736 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2740 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2741 struct in_addr vtep_ip
, bool sticky
)
2743 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_NEWNEIGH
,
2747 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2748 struct in_addr vtep_ip
)
2750 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_DELNEIGH
, 0);
2753 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2754 struct ethaddr
*mac
, uint8_t flags
)
2756 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2757 NUD_NOARP
, RTM_NEWNEIGH
);
2760 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2762 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2765 int kernel_upd_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2766 struct ethaddr
*mac
, uint8_t flags
, uint16_t state
)
2768 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2769 state
, RTM_NEWNEIGH
);
2773 * MPLS label forwarding table change via netlink interface, using dataplane
2774 * context information.
2776 int netlink_mpls_multipath(int cmd
, struct zebra_dplane_ctx
*ctx
)
2779 const zebra_nhlfe_t
*nhlfe
;
2780 struct nexthop
*nexthop
= NULL
;
2781 unsigned int nexthop_num
;
2782 const char *routedesc
;
2788 char buf
[NL_PKT_BUF_SIZE
];
2791 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
2794 * Count # nexthops so we can decide whether to use singlepath
2795 * or multipath case.
2798 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
); nhlfe
; nhlfe
= nhlfe
->next
) {
2799 nexthop
= nhlfe
->nexthop
;
2802 if (cmd
== RTM_NEWROUTE
) {
2803 /* Count all selected NHLFEs */
2804 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2805 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2808 /* Count all installed NHLFEs */
2809 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2810 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2815 if ((nexthop_num
== 0) ||
2816 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
2819 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2820 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2821 req
.n
.nlmsg_type
= cmd
;
2822 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2824 req
.r
.rtm_family
= AF_MPLS
;
2825 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2826 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2827 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2828 req
.r
.rtm_type
= RTN_UNICAST
;
2830 if (cmd
== RTM_NEWROUTE
) {
2831 /* We do a replace to handle update. */
2832 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2834 /* set the protocol value if installing */
2835 route_type
= re_type_from_lsp_type(
2836 dplane_ctx_get_best_nhlfe(ctx
)->type
);
2837 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2840 /* Fill destination */
2841 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
2842 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2844 /* Fill nexthops (paths) based on single-path or multipath. The paths
2845 * chosen depend on the operation.
2847 if (nexthop_num
== 1) {
2848 routedesc
= "single-path";
2849 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
2853 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
2854 nhlfe
; nhlfe
= nhlfe
->next
) {
2855 nexthop
= nhlfe
->nexthop
;
2859 if ((cmd
== RTM_NEWROUTE
2860 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2861 && CHECK_FLAG(nexthop
->flags
,
2862 NEXTHOP_FLAG_ACTIVE
)))
2863 || (cmd
== RTM_DELROUTE
2864 && (CHECK_FLAG(nhlfe
->flags
,
2865 NHLFE_FLAG_INSTALLED
)
2866 && CHECK_FLAG(nexthop
->flags
,
2867 NEXTHOP_FLAG_FIB
)))) {
2868 /* Add the gateway */
2869 _netlink_mpls_build_singlepath(
2878 } else { /* Multipath case */
2879 char buf
[NL_PKT_BUF_SIZE
];
2880 struct rtattr
*rta
= (void *)buf
;
2881 struct rtnexthop
*rtnh
;
2882 const union g_addr
*src1
= NULL
;
2884 rta
->rta_type
= RTA_MULTIPATH
;
2885 rta
->rta_len
= RTA_LENGTH(0);
2886 rtnh
= RTA_DATA(rta
);
2888 routedesc
= "multipath";
2889 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
2893 for (nhlfe
= dplane_ctx_get_nhlfe(ctx
);
2894 nhlfe
; nhlfe
= nhlfe
->next
) {
2895 nexthop
= nhlfe
->nexthop
;
2899 if ((cmd
== RTM_NEWROUTE
2900 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2901 && CHECK_FLAG(nexthop
->flags
,
2902 NEXTHOP_FLAG_ACTIVE
)))
2903 || (cmd
== RTM_DELROUTE
2904 && (CHECK_FLAG(nhlfe
->flags
,
2905 NHLFE_FLAG_INSTALLED
)
2906 && CHECK_FLAG(nexthop
->flags
,
2907 NEXTHOP_FLAG_FIB
)))) {
2910 /* Build the multipath */
2911 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2914 rtnh
= RTNH_NEXT(rtnh
);
2918 /* Add the multipath */
2919 if (rta
->rta_len
> RTA_LENGTH(0))
2920 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2921 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2924 /* Talk to netlink socket. */
2925 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2926 dplane_ctx_get_ns(ctx
), 0);
2928 #endif /* HAVE_NETLINK */