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"
72 static vlanid_t filter_vlan
= 0;
80 char ipv4_ll_buf
[16] = "169.254.0.1";
81 struct in_addr ipv4_ll
;
84 * The ipv4_ll data structure is used for all 5549
85 * additions to the kernel. Let's figure out the
86 * correct value one time instead for every
87 * install/remove of a 5549 type route
89 void rt_netlink_init(void)
91 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
94 static inline int is_selfroute(int proto
)
96 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
97 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
98 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
99 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
100 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
101 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
102 || (proto
== RTPROT_PBR
)) {
109 static inline int zebra2proto(int proto
)
112 case ZEBRA_ROUTE_BABEL
:
113 proto
= RTPROT_BABEL
;
115 case ZEBRA_ROUTE_BGP
:
118 case ZEBRA_ROUTE_OSPF
:
119 case ZEBRA_ROUTE_OSPF6
:
122 case ZEBRA_ROUTE_STATIC
:
123 proto
= RTPROT_ZSTATIC
;
125 case ZEBRA_ROUTE_ISIS
:
128 case ZEBRA_ROUTE_RIP
:
131 case ZEBRA_ROUTE_RIPNG
:
132 proto
= RTPROT_RIPNG
;
134 case ZEBRA_ROUTE_NHRP
:
137 case ZEBRA_ROUTE_EIGRP
:
138 proto
= RTPROT_EIGRP
;
140 case ZEBRA_ROUTE_LDP
:
143 case ZEBRA_ROUTE_SHARP
:
144 proto
= RTPROT_SHARP
;
146 case ZEBRA_ROUTE_PBR
:
151 * When a user adds a new protocol this will show up
152 * to let them know to do something about it. This
153 * is intentionally a warn because we should see
154 * this as part of development of a new protocol
157 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
158 __PRETTY_FUNCTION__
, proto
);
159 proto
= RTPROT_ZEBRA
;
166 static inline int proto2zebra(int proto
, int family
)
170 proto
= ZEBRA_ROUTE_BABEL
;
173 proto
= ZEBRA_ROUTE_BGP
;
176 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
180 proto
= ZEBRA_ROUTE_ISIS
;
183 proto
= ZEBRA_ROUTE_RIP
;
186 proto
= ZEBRA_ROUTE_RIPNG
;
189 proto
= ZEBRA_ROUTE_NHRP
;
192 proto
= ZEBRA_ROUTE_EIGRP
;
195 proto
= ZEBRA_ROUTE_LDP
;
199 proto
= ZEBRA_ROUTE_STATIC
;
202 proto
= ZEBRA_ROUTE_SHARP
;
205 proto
= ZEBRA_ROUTE_PBR
;
209 * When a user adds a new protocol this will show up
210 * to let them know to do something about it. This
211 * is intentionally a warn because we should see
212 * this as part of development of a new protocol
215 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
216 __PRETTY_FUNCTION__
, proto
);
217 proto
= ZEBRA_ROUTE_KERNEL
;
224 Pending: create an efficient table_id (in a tree/hash) based lookup)
226 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
229 struct zebra_vrf
*zvrf
;
231 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
235 /* case vrf with netns : match the netnsid */
236 if (vrf_is_backend_netns()) {
237 if (ns_id
== zvrf_id(zvrf
))
238 return zvrf_id(zvrf
);
240 /* VRF is VRF_BACKEND_VRF_LITE */
241 if (zvrf
->table_id
!= table_id
)
243 return zvrf_id(zvrf
);
251 * @parse_encap_mpls() - Parses encapsulated mpls attributes
252 * @tb: Pointer to rtattr to look for nested items in.
253 * @labels: Pointer to store labels in.
255 * Return: Number of mpls labels found.
257 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
259 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
260 mpls_lse_t
*lses
= NULL
;
265 mpls_label_t label
= 0;
267 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
268 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
269 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
270 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
271 labels
[num_labels
++] = label
;
277 /* Looking up routing table by netlink interface. */
278 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
283 struct rtattr
*tb
[RTA_MAX
+ 1];
286 struct prefix_ipv6 src_p
= {};
289 char anyaddr
[16] = {0};
291 int proto
= ZEBRA_ROUTE_KERNEL
;
296 uint8_t distance
= 0;
301 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
302 void *src
= NULL
; /* IPv6 srcdest source prefix */
303 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
306 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
311 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
313 switch (rtm
->rtm_type
) {
317 bh_type
= BLACKHOLE_NULL
;
319 case RTN_UNREACHABLE
:
320 bh_type
= BLACKHOLE_REJECT
;
323 bh_type
= BLACKHOLE_ADMINPROHIB
;
329 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
331 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
332 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
333 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
337 memset(tb
, 0, sizeof tb
);
338 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
340 if (rtm
->rtm_flags
& RTM_F_CLONED
)
342 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
344 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
347 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
348 && h
->nlmsg_type
== RTM_NEWROUTE
) {
349 if (IS_ZEBRA_DEBUG_KERNEL
)
350 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
355 /* We don't care about change notifications for the MPLS table. */
356 /* TODO: Revisit this. */
357 if (rtm
->rtm_family
== AF_MPLS
)
360 /* Table corresponding to route. */
362 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
364 table
= rtm
->rtm_table
;
367 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
368 if (vrf_id
== VRF_DEFAULT
) {
369 if (!is_zebra_valid_kernel_table(table
)
370 && !is_zebra_main_routing_table(table
))
374 /* Route which inserted by Zebra. */
375 if (is_selfroute(rtm
->rtm_protocol
)) {
376 flags
|= ZEBRA_FLAG_SELFROUTE
;
377 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
380 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
383 dest
= RTA_DATA(tb
[RTA_DST
]);
388 src
= RTA_DATA(tb
[RTA_SRC
]);
393 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
396 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
398 if (tb
[RTA_PRIORITY
])
399 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
401 #if defined(SUPPORT_REALMS)
403 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
406 if (tb
[RTA_METRICS
]) {
407 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
409 memset(mxrta
, 0, sizeof mxrta
);
410 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
411 RTA_PAYLOAD(tb
[RTA_METRICS
]));
414 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
417 if (rtm
->rtm_family
== AF_INET
) {
419 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
421 "Invalid destination prefix length: %u received from kernel route change",
425 memcpy(&p
.u
.prefix4
, dest
, 4);
426 p
.prefixlen
= rtm
->rtm_dst_len
;
428 if (rtm
->rtm_src_len
!= 0) {
429 char buf
[PREFIX_STRLEN
];
431 ZEBRA_ERR_UNSUPPORTED_V4_SRCDEST
,
432 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
433 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
437 /* Force debug below to not display anything for source */
439 } else if (rtm
->rtm_family
== AF_INET6
) {
441 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
443 "Invalid destination prefix length: %u received from kernel route change",
447 memcpy(&p
.u
.prefix6
, dest
, 16);
448 p
.prefixlen
= rtm
->rtm_dst_len
;
450 src_p
.family
= AF_INET6
;
451 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
453 "Invalid source prefix length: %u received from kernel route change",
457 memcpy(&src_p
.prefix
, src
, 16);
458 src_p
.prefixlen
= rtm
->rtm_src_len
;
462 * For ZEBRA_ROUTE_KERNEL types:
464 * The metric/priority of the route received from the kernel
465 * is a 32 bit number. We are going to interpret the high
466 * order byte as the Admin Distance and the low order 3 bytes
469 * This will allow us to do two things:
470 * 1) Allow the creation of kernel routes that can be
471 * overridden by zebra.
472 * 2) Allow the old behavior for 'most' kernel route types
473 * if a user enters 'ip route ...' v4 routes get a metric
474 * of 0 and v6 routes get a metric of 1024. Both of these
475 * values will end up with a admin distance of 0, which
476 * will cause them to win for the purposes of zebra.
478 if (proto
== ZEBRA_ROUTE_KERNEL
) {
479 distance
= (metric
>> 24) & 0xFF;
480 metric
= (metric
& 0x00FFFFFF);
483 if (IS_ZEBRA_DEBUG_KERNEL
) {
484 char buf
[PREFIX_STRLEN
];
485 char buf2
[PREFIX_STRLEN
];
486 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
487 nl_msg_type_to_str(h
->nlmsg_type
),
488 prefix2str(&p
, buf
, sizeof(buf
)),
489 src_p
.prefixlen
? " from " : "",
491 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
493 vrf_id
, table
, metric
, distance
);
497 if (rtm
->rtm_family
== AF_INET6
)
500 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
501 struct interface
*ifp
;
502 vrf_id_t nh_vrf_id
= vrf_id
;
504 if (!tb
[RTA_MULTIPATH
]) {
506 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
508 memset(&nh
, 0, sizeof(nh
));
510 if (bh_type
== BLACKHOLE_UNSPEC
) {
512 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
513 else if (index
&& gate
)
516 ? NEXTHOP_TYPE_IPV4_IFINDEX
517 : NEXTHOP_TYPE_IPV6_IFINDEX
;
518 else if (!index
&& gate
)
519 nh
.type
= (afi
== AFI_IP
)
523 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
524 nh
.bh_type
= bh_type
;
527 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
528 nh
.bh_type
= bh_type
;
532 memcpy(&nh
.src
, prefsrc
, sz
);
534 memcpy(&nh
.gate
, gate
, sz
);
537 ifp
= if_lookup_by_index_per_ns(
538 zebra_ns_lookup(ns_id
),
541 nh_vrf_id
= ifp
->vrf_id
;
543 nh
.vrf_id
= nh_vrf_id
;
545 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
546 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
547 == LWTUNNEL_ENCAP_MPLS
) {
549 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
553 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
556 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
557 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
559 /* This is a multipath route */
561 struct route_entry
*re
;
562 struct rtnexthop
*rtnh
=
563 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
565 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
567 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
569 re
->distance
= distance
;
576 re
->uptime
= time(NULL
);
580 struct nexthop
*nh
= NULL
;
582 if (len
< (int)sizeof(*rtnh
)
583 || rtnh
->rtnh_len
> len
)
586 index
= rtnh
->rtnh_ifindex
;
589 * Yes we are looking this up
590 * for every nexthop and just
591 * using the last one looked
594 ifp
= if_lookup_by_index_per_ns(
595 zebra_ns_lookup(ns_id
),
598 nh_vrf_id
= ifp
->vrf_id
;
601 ZEBRA_ERR_UNKNOWN_INTERFACE
,
602 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
605 nh_vrf_id
= VRF_DEFAULT
;
611 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
612 memset(tb
, 0, sizeof(tb
));
613 netlink_parse_rtattr(
614 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
615 rtnh
->rtnh_len
- sizeof(*rtnh
));
619 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
620 && *(uint16_t *)RTA_DATA(
622 == LWTUNNEL_ENCAP_MPLS
) {
623 num_labels
= parse_encap_mpls(
624 tb
[RTA_ENCAP
], labels
);
629 if (rtm
->rtm_family
== AF_INET
) {
631 nh
= route_entry_nexthop_ipv4_ifindex_add(
636 nh
= route_entry_nexthop_ipv4_add(
640 } else if (rtm
->rtm_family
643 nh
= route_entry_nexthop_ipv6_ifindex_add(
647 nh
= route_entry_nexthop_ipv6_add(
652 nh
= route_entry_nexthop_ifindex_add(
653 re
, index
, nh_vrf_id
);
655 if (nh
&& num_labels
)
656 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
659 if (rtnh
->rtnh_len
== 0)
662 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
663 rtnh
= RTNH_NEXT(rtnh
);
666 zserv_nexthop_num_warn(__func__
,
667 (const struct prefix
*)&p
,
669 if (re
->nexthop_num
== 0)
672 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
676 if (!tb
[RTA_MULTIPATH
]) {
678 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
680 memset(&nh
, 0, sizeof(nh
));
681 if (bh_type
== BLACKHOLE_UNSPEC
) {
683 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
684 else if (index
&& gate
)
687 ? NEXTHOP_TYPE_IPV4_IFINDEX
688 : NEXTHOP_TYPE_IPV6_IFINDEX
;
689 else if (!index
&& gate
)
690 nh
.type
= (afi
== AFI_IP
)
694 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
695 nh
.bh_type
= BLACKHOLE_UNSPEC
;
698 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
699 nh
.bh_type
= bh_type
;
703 memcpy(&nh
.gate
, gate
, sz
);
704 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
705 &p
, &src_p
, &nh
, table
, metric
, distance
,
708 /* XXX: need to compare the entire list of nexthops
709 * here for NLM_F_APPEND stupidity */
710 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
711 &p
, &src_p
, NULL
, table
, metric
, distance
,
719 static struct mcast_route_data
*mroute
= NULL
;
721 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
722 ns_id_t ns_id
, int startup
)
726 struct rtattr
*tb
[RTA_MAX
+ 1];
727 struct mcast_route_data
*m
;
728 struct mcast_route_data mr
;
735 char oif_list
[256] = "\0";
742 memset(&mr
, 0, sizeof(mr
));
748 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
750 memset(tb
, 0, sizeof tb
);
751 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
754 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
756 table
= rtm
->rtm_table
;
758 vrf
= vrf_lookup_by_table(table
, ns_id
);
761 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
764 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
767 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
769 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
770 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
772 if (tb
[RTA_MULTIPATH
]) {
773 struct rtnexthop
*rtnh
=
774 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
776 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
778 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
781 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
784 if (rtnh
->rtnh_len
== 0)
787 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
788 rtnh
= RTNH_NEXT(rtnh
);
792 if (IS_ZEBRA_DEBUG_KERNEL
) {
793 struct interface
*ifp
;
794 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
795 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
796 for (count
= 0; count
< oif_count
; count
++) {
797 ifp
= if_lookup_by_index(oif
[count
], vrf
);
800 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
802 strcat(oif_list
, temp
);
804 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
805 ifp
= if_lookup_by_index(iif
, vrf
);
806 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
807 zvrf
->vrf
->name
, vrf
,
808 nl_msg_type_to_str(h
->nlmsg_type
),
809 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
810 oif_list
, m
->lastused
);
815 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
822 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
823 /* If this is not route add/delete message print warning. */
824 zlog_debug("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
828 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
)) {
830 ZEBRA_ERR_UNKNOWN_FAMILY
,
831 "Invalid address family: %u received from kernel route change: %u",
832 rtm
->rtm_family
, h
->nlmsg_type
);
836 /* Connected route. */
837 if (IS_ZEBRA_DEBUG_KERNEL
)
838 zlog_debug("%s %s %s proto %s NS %u",
839 nl_msg_type_to_str(h
->nlmsg_type
),
840 nl_family_to_str(rtm
->rtm_family
),
841 nl_rttype_to_str(rtm
->rtm_type
),
842 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
845 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
847 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
850 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
854 if (rtm
->rtm_type
== RTN_MULTICAST
)
855 netlink_route_change_read_multicast(h
, ns_id
, startup
);
857 netlink_route_change_read_unicast(h
, ns_id
, startup
);
861 /* Request for specific route information from the kernel */
862 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
869 /* Form the request, specifying filter (rtattr) if needed. */
870 memset(&req
, 0, sizeof(req
));
871 req
.n
.nlmsg_type
= type
;
872 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
873 req
.rtm
.rtm_family
= family
;
875 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
878 /* Routing table read function using netlink interface. Only called
880 int netlink_route_read(struct zebra_ns
*zns
)
884 /* Get IPv4 routing table. */
885 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
888 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
889 &zns
->netlink_cmd
, zns
, 0, 1);
893 /* Get IPv6 routing table. */
894 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
897 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
898 &zns
->netlink_cmd
, zns
, 0, 1);
905 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
907 struct nlmsghdr
*nlmsg
,
908 size_t req_size
, int bytelen
,
909 struct nexthop
*nexthop
)
911 if (route_family
== AF_MPLS
) {
912 struct gw_family_t gw_fam
;
914 gw_fam
.family
= gw_family
;
915 if (gw_family
== AF_INET
)
916 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
918 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
919 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
922 if (gw_family
== AF_INET
)
923 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
924 &nexthop
->gate
.ipv4
, bytelen
);
926 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
927 &nexthop
->gate
.ipv6
, bytelen
);
931 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
934 struct rtnexthop
*rtnh
,
935 size_t req_size
, int bytelen
,
936 struct nexthop
*nexthop
)
938 if (route_family
== AF_MPLS
) {
939 struct gw_family_t gw_fam
;
941 gw_fam
.family
= gw_family
;
942 if (gw_family
== AF_INET
)
943 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
945 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
946 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
948 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
950 if (gw_family
== AF_INET
)
951 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
952 &nexthop
->gate
.ipv4
, bytelen
);
954 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
955 &nexthop
->gate
.ipv6
, bytelen
);
956 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
960 /* This function takes a nexthop as argument and adds
961 * the appropriate netlink attributes to an existing
964 * @param routedesc: Human readable description of route type
965 * (direct/recursive, single-/multipath)
966 * @param bytelen: Length of addresses in bytes.
967 * @param nexthop: Nexthop information
968 * @param nlmsg: nlmsghdr structure to fill in.
969 * @param req_size: The size allocated for the message.
971 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
972 struct nexthop
*nexthop
,
973 struct nlmsghdr
*nlmsg
,
975 size_t req_size
, int cmd
)
977 struct mpls_label_stack
*nh_label
;
978 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
983 * label_buf is *only* currently used within debugging.
984 * As such when we assign it we are guarding it inside
985 * a debug test. If you want to change this make sure
986 * you fix this assumption
991 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
994 nh_label
= nh
->nh_label
;
995 if (!nh_label
|| !nh_label
->num_labels
)
998 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
999 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1002 if (IS_ZEBRA_DEBUG_KERNEL
) {
1004 sprintf(label_buf
, "label %u",
1005 nh_label
->label
[i
]);
1007 sprintf(label_buf1
, "/%u",
1008 nh_label
->label
[i
]);
1009 strlcat(label_buf
, label_buf1
,
1014 out_lse
[num_labels
] =
1015 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1021 /* Set the BoS bit */
1022 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1024 if (rtmsg
->rtm_family
== AF_MPLS
)
1025 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1026 num_labels
* sizeof(mpls_lse_t
));
1028 struct rtattr
*nest
;
1029 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1031 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1033 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1034 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1035 num_labels
* sizeof(mpls_lse_t
));
1036 addattr_nest_end(nlmsg
, nest
);
1040 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1041 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1043 if (rtmsg
->rtm_family
== AF_INET
1044 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1045 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1046 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1047 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1048 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1050 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1051 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1052 &nexthop
->rmap_src
.ipv4
, bytelen
);
1053 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1054 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1055 &nexthop
->src
.ipv4
, bytelen
);
1057 if (IS_ZEBRA_DEBUG_KERNEL
)
1059 " 5549: _netlink_route_build_singlepath() (%s): "
1060 "nexthop via %s %s if %u(%u)",
1061 routedesc
, ipv4_ll_buf
, label_buf
,
1062 nexthop
->ifindex
, nexthop
->vrf_id
);
1066 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1067 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1068 /* Send deletes to the kernel without specifying the next-hop */
1069 if (cmd
!= RTM_DELROUTE
)
1070 _netlink_route_nl_add_gateway_info(
1071 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1074 if (cmd
== RTM_NEWROUTE
) {
1075 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1076 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1077 &nexthop
->rmap_src
.ipv4
, bytelen
);
1078 else if (nexthop
->src
.ipv4
.s_addr
)
1079 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1080 &nexthop
->src
.ipv4
, bytelen
);
1083 if (IS_ZEBRA_DEBUG_KERNEL
)
1085 "netlink_route_multipath() (%s): "
1086 "nexthop via %s %s if %u(%u)",
1087 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1088 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1091 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1092 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1093 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1094 nlmsg
, req_size
, bytelen
,
1097 if (cmd
== RTM_NEWROUTE
) {
1098 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1099 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1100 &nexthop
->rmap_src
.ipv6
, bytelen
);
1101 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1102 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1103 &nexthop
->src
.ipv6
, bytelen
);
1106 if (IS_ZEBRA_DEBUG_KERNEL
)
1108 "netlink_route_multipath() (%s): "
1109 "nexthop via %s %s if %u(%u)",
1110 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1111 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1115 * We have the ifindex so we should always send it
1116 * This is especially useful if we are doing route
1119 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1120 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1122 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1123 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1124 if (cmd
== RTM_NEWROUTE
) {
1125 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1126 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1127 &nexthop
->rmap_src
.ipv4
, bytelen
);
1128 else if (nexthop
->src
.ipv4
.s_addr
)
1129 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1130 &nexthop
->src
.ipv4
, bytelen
);
1133 if (IS_ZEBRA_DEBUG_KERNEL
)
1135 "netlink_route_multipath() (%s): "
1136 "nexthop via if %u(%u)",
1137 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1140 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1141 if (cmd
== RTM_NEWROUTE
) {
1142 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1143 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1144 &nexthop
->rmap_src
.ipv6
, bytelen
);
1145 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1146 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1147 &nexthop
->src
.ipv6
, bytelen
);
1150 if (IS_ZEBRA_DEBUG_KERNEL
)
1152 "netlink_route_multipath() (%s): "
1153 "nexthop via if %u(%u)",
1154 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1158 /* This function takes a nexthop as argument and
1159 * appends to the given rtattr/rtnexthop pair the
1160 * representation of the nexthop. If the nexthop
1161 * defines a preferred source, the src parameter
1162 * will be modified to point to that src, otherwise
1163 * it will be kept unmodified.
1165 * @param routedesc: Human readable description of route type
1166 * (direct/recursive, single-/multipath)
1167 * @param bytelen: Length of addresses in bytes.
1168 * @param nexthop: Nexthop information
1169 * @param rta: rtnetlink attribute structure
1170 * @param rtnh: pointer to an rtnetlink nexthop structure
1171 * @param src: pointer pointing to a location where
1172 * the prefsrc should be stored.
1174 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1175 struct nexthop
*nexthop
,
1177 struct rtnexthop
*rtnh
,
1178 struct rtmsg
*rtmsg
,
1181 struct mpls_label_stack
*nh_label
;
1182 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1184 char label_buf
[256];
1186 rtnh
->rtnh_len
= sizeof(*rtnh
);
1187 rtnh
->rtnh_flags
= 0;
1188 rtnh
->rtnh_hops
= 0;
1189 rta
->rta_len
+= rtnh
->rtnh_len
;
1192 * label_buf is *only* currently used within debugging.
1193 * As such when we assign it we are guarding it inside
1194 * a debug test. If you want to change this make sure
1195 * you fix this assumption
1197 label_buf
[0] = '\0';
1200 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1201 char label_buf1
[20];
1203 nh_label
= nh
->nh_label
;
1204 if (!nh_label
|| !nh_label
->num_labels
)
1207 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1208 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1211 if (IS_ZEBRA_DEBUG_KERNEL
) {
1213 sprintf(label_buf
, "label %u",
1214 nh_label
->label
[i
]);
1216 sprintf(label_buf1
, "/%u",
1217 nh_label
->label
[i
]);
1218 strlcat(label_buf
, label_buf1
,
1223 out_lse
[num_labels
] =
1224 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1230 /* Set the BoS bit */
1231 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1233 if (rtmsg
->rtm_family
== AF_MPLS
) {
1234 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1236 num_labels
* sizeof(mpls_lse_t
));
1238 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1240 struct rtattr
*nest
;
1241 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1242 int len
= rta
->rta_len
;
1244 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1245 &encap
, sizeof(uint16_t));
1246 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1247 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1249 num_labels
* sizeof(mpls_lse_t
));
1250 rta_nest_end(rta
, nest
);
1251 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1255 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1256 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1258 if (rtmsg
->rtm_family
== AF_INET
1259 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1260 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1262 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1263 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1265 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1266 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1268 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1269 *src
= &nexthop
->rmap_src
;
1270 else if (nexthop
->src
.ipv4
.s_addr
)
1271 *src
= &nexthop
->src
;
1273 if (IS_ZEBRA_DEBUG_KERNEL
)
1275 " 5549: netlink_route_build_multipath() (%s): "
1276 "nexthop via %s %s if %u",
1277 routedesc
, ipv4_ll_buf
, label_buf
,
1282 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1283 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1284 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1285 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1287 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1288 *src
= &nexthop
->rmap_src
;
1289 else if (nexthop
->src
.ipv4
.s_addr
)
1290 *src
= &nexthop
->src
;
1292 if (IS_ZEBRA_DEBUG_KERNEL
)
1294 "netlink_route_multipath() (%s): "
1295 "nexthop via %s %s if %u",
1296 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1297 label_buf
, nexthop
->ifindex
);
1299 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1300 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1301 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1302 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1305 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1306 *src
= &nexthop
->rmap_src
;
1307 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1308 *src
= &nexthop
->src
;
1310 if (IS_ZEBRA_DEBUG_KERNEL
)
1312 "netlink_route_multipath() (%s): "
1313 "nexthop via %s %s if %u",
1314 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1315 label_buf
, nexthop
->ifindex
);
1319 * We have figured out the ifindex so we should always send it
1320 * This is especially useful if we are doing route
1323 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1324 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1327 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1328 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1329 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1330 *src
= &nexthop
->rmap_src
;
1331 else if (nexthop
->src
.ipv4
.s_addr
)
1332 *src
= &nexthop
->src
;
1334 if (IS_ZEBRA_DEBUG_KERNEL
)
1336 "netlink_route_multipath() (%s): "
1337 "nexthop via if %u",
1338 routedesc
, nexthop
->ifindex
);
1342 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1343 zebra_nhlfe_t
*nhlfe
,
1344 struct nlmsghdr
*nlmsg
,
1345 struct rtmsg
*rtmsg
,
1346 size_t req_size
, int cmd
)
1351 family
= NHLFE_FAMILY(nhlfe
);
1352 bytelen
= (family
== AF_INET
? 4 : 16);
1353 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1354 nlmsg
, rtmsg
, req_size
, cmd
);
1359 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1360 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1361 struct rtmsg
*rtmsg
, union g_addr
**src
)
1366 family
= NHLFE_FAMILY(nhlfe
);
1367 bytelen
= (family
== AF_INET
? 4 : 16);
1368 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1373 /* Log debug information for netlink_route_multipath
1374 * if debug logging is enabled.
1376 * @param cmd: Netlink command which is to be processed
1377 * @param p: Prefix for which the change is due
1378 * @param family: Address family which the change concerns
1379 * @param zvrf: The vrf we are in
1380 * @param tableid: The table we are working on
1382 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1383 int family
, vrf_id_t vrfid
,
1386 if (IS_ZEBRA_DEBUG_KERNEL
) {
1387 char buf
[PREFIX_STRLEN
];
1389 "netlink_route_multipath(): %s %s vrf %u(%u)",
1390 nl_msg_type_to_str(cmd
),
1391 prefix2str(p
, buf
, sizeof(buf
)),
1396 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1398 if (IS_ZEBRA_DEBUG_KERNEL
)
1399 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1400 nl_msg_type_to_str(cmd
), label
);
1403 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1404 int llalen
, ns_id_t ns_id
)
1412 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1414 memset(&req
, 0, sizeof(req
));
1416 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1417 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1418 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1419 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1421 req
.ndm
.ndm_family
= AF_INET
;
1422 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1423 req
.ndm
.ndm_ifindex
= ifindex
;
1424 req
.ndm
.ndm_type
= RTN_UNICAST
;
1426 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1427 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1429 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1433 /* Routing table change via netlink interface. */
1434 /* Update flag indicates whether this is a "replace" or not. */
1435 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1436 const struct prefix
*src_p
,
1437 struct route_entry
*re
,
1441 struct sockaddr_nl snl
;
1442 struct nexthop
*nexthop
= NULL
;
1443 unsigned int nexthop_num
;
1444 int family
= PREFIX_FAMILY(p
);
1445 const char *routedesc
;
1452 char buf
[NL_PKT_BUF_SIZE
];
1455 struct zebra_ns
*zns
;
1456 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1459 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1461 bytelen
= (family
== AF_INET
? 4 : 16);
1463 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1464 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1465 if ((cmd
== RTM_NEWROUTE
) && update
)
1466 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1467 req
.n
.nlmsg_type
= cmd
;
1468 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1470 req
.r
.rtm_family
= family
;
1471 req
.r
.rtm_dst_len
= p
->prefixlen
;
1472 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1473 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1474 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1477 * blackhole routes are not RTN_UNICAST, they are
1478 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1479 * so setting this value as a RTN_UNICAST would
1480 * cause the route lookup of just the prefix
1481 * to fail. So no need to specify this for
1482 * the RTM_DELROUTE case
1484 if (cmd
!= RTM_DELROUTE
)
1485 req
.r
.rtm_type
= RTN_UNICAST
;
1487 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1489 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1493 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1495 * either by the kernel or by zebra. Its purely for calculating best
1497 * by the routing protocol and for communicating with protocol peers.
1499 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1500 #if defined(SUPPORT_REALMS)
1501 if (re
->tag
> 0 && re
->tag
<= 255)
1502 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1504 /* Table corresponding to this route. */
1505 if (re
->table
< 256)
1506 req
.r
.rtm_table
= re
->table
;
1508 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1509 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1512 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1515 * If we are not updating the route and we have received
1516 * a route delete, then all we need to fill in is the
1517 * prefix information to tell the kernel to schwack
1520 if (!update
&& cmd
== RTM_DELROUTE
)
1523 if (re
->mtu
|| re
->nexthop_mtu
) {
1524 char buf
[NL_PKT_BUF_SIZE
];
1525 struct rtattr
*rta
= (void *)buf
;
1526 uint32_t mtu
= re
->mtu
;
1527 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1528 mtu
= re
->nexthop_mtu
;
1529 rta
->rta_type
= RTA_METRICS
;
1530 rta
->rta_len
= RTA_LENGTH(0);
1531 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1532 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1536 /* Count overall nexthops so we can decide whether to use singlepath
1537 * or multipath case. */
1539 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1540 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1542 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1544 if (cmd
== RTM_DELROUTE
1545 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1551 /* Singlepath case. */
1552 if (nexthop_num
== 1 || multipath_num
== 1) {
1554 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1556 * So we want to cover 2 types of blackhole
1558 * 1) A normal blackhole route( ala from a static
1560 * 2) A recursively resolved blackhole route
1562 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1563 switch (nexthop
->bh_type
) {
1564 case BLACKHOLE_ADMINPROHIB
:
1565 req
.r
.rtm_type
= RTN_PROHIBIT
;
1567 case BLACKHOLE_REJECT
:
1568 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1571 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1576 if (CHECK_FLAG(nexthop
->flags
,
1577 NEXTHOP_FLAG_RECURSIVE
)) {
1579 if (family
== AF_INET
) {
1580 if (nexthop
->rmap_src
.ipv4
1587 } else if (nexthop
->src
.ipv4
1595 } else if (family
== AF_INET6
) {
1596 if (!IN6_IS_ADDR_UNSPECIFIED(
1604 !IN6_IS_ADDR_UNSPECIFIED(
1617 if ((cmd
== RTM_NEWROUTE
1618 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1619 || (cmd
== RTM_DELROUTE
1620 && CHECK_FLAG(nexthop
->flags
,
1621 NEXTHOP_FLAG_FIB
))) {
1622 routedesc
= nexthop
->rparent
1623 ? "recursive, single-path"
1626 _netlink_route_build_singlepath(
1627 routedesc
, bytelen
, nexthop
, &req
.n
,
1628 &req
.r
, sizeof req
, cmd
);
1633 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1634 if (family
== AF_INET
)
1635 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1636 &src
.ipv4
, bytelen
);
1637 else if (family
== AF_INET6
)
1638 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1639 &src
.ipv6
, bytelen
);
1642 char buf
[NL_PKT_BUF_SIZE
];
1643 struct rtattr
*rta
= (void *)buf
;
1644 struct rtnexthop
*rtnh
;
1645 union g_addr
*src1
= NULL
;
1647 rta
->rta_type
= RTA_MULTIPATH
;
1648 rta
->rta_len
= RTA_LENGTH(0);
1649 rtnh
= RTA_DATA(rta
);
1652 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1653 if (nexthop_num
>= multipath_num
)
1656 if (CHECK_FLAG(nexthop
->flags
,
1657 NEXTHOP_FLAG_RECURSIVE
)) {
1658 /* This only works for IPv4 now */
1660 if (family
== AF_INET
) {
1661 if (nexthop
->rmap_src
.ipv4
1668 } else if (nexthop
->src
.ipv4
1676 } else if (family
== AF_INET6
) {
1677 if (!IN6_IS_ADDR_UNSPECIFIED(
1685 !IN6_IS_ADDR_UNSPECIFIED(
1698 if ((cmd
== RTM_NEWROUTE
1699 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1700 || (cmd
== RTM_DELROUTE
1701 && CHECK_FLAG(nexthop
->flags
,
1702 NEXTHOP_FLAG_FIB
))) {
1703 routedesc
= nexthop
->rparent
1704 ? "recursive, multipath"
1708 _netlink_route_build_multipath(
1709 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1711 rtnh
= RTNH_NEXT(rtnh
);
1713 if (!setsrc
&& src1
) {
1714 if (family
== AF_INET
)
1715 src
.ipv4
= src1
->ipv4
;
1716 else if (family
== AF_INET6
)
1717 src
.ipv6
= src1
->ipv6
;
1723 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1724 if (family
== AF_INET
)
1725 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1726 &src
.ipv4
, bytelen
);
1727 else if (family
== AF_INET6
)
1728 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1729 &src
.ipv6
, bytelen
);
1730 if (IS_ZEBRA_DEBUG_KERNEL
)
1731 zlog_debug("Setting source");
1734 if (rta
->rta_len
> RTA_LENGTH(0))
1735 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1736 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1739 /* If there is no useful nexthop then return. */
1740 if (nexthop_num
== 0) {
1741 if (IS_ZEBRA_DEBUG_KERNEL
)
1743 "netlink_route_multipath(): No useful nexthop.");
1749 /* Destination netlink address. */
1750 memset(&snl
, 0, sizeof snl
);
1751 snl
.nl_family
= AF_NETLINK
;
1753 /* Talk to netlink socket. */
1754 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1758 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1761 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1769 struct zebra_ns
*zns
;
1772 memset(&req
, 0, sizeof(req
));
1774 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1775 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1776 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1778 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1779 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1781 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1782 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1783 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1784 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1785 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1787 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1788 &zns
->netlink_cmd
, zns
, 0);
1794 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1795 const struct prefix
*p
,
1796 const struct prefix
*src_p
,
1797 struct route_entry
*old
,
1798 struct route_entry
*new)
1805 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1806 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1807 new, (old
) ? 1 : 0);
1810 * So v6 route replace semantics are not in
1811 * the kernel at this point as I understand it.
1812 * So let's do a delete than an add.
1813 * In the future once v6 route replace semantics
1814 * are in we can figure out what to do here to
1815 * allow working with old and new kernels.
1817 * I'm also intentionally ignoring the failure case
1818 * of the route delete. If that happens yeah we're
1822 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1824 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1827 kernel_route_rib_pass_fail(rn
, p
, new,
1828 (!ret
) ? DP_INSTALL_SUCCESS
1829 : DP_INSTALL_FAILURE
);
1830 return DP_REQUEST_SUCCESS
;
1834 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1836 kernel_route_rib_pass_fail(rn
, p
, old
,
1837 (!ret
) ? DP_DELETE_SUCCESS
1838 : DP_DELETE_FAILURE
);
1841 return DP_REQUEST_SUCCESS
;
1844 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1845 int llalen
, ns_id_t ns_id
)
1847 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1848 addr
, lla
, llalen
, ns_id
);
1852 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1853 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1855 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1856 struct in_addr
*vtep_ip
, int cmd
)
1858 struct zebra_ns
*zns
;
1864 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1865 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1868 memset(&req
, 0, sizeof(req
));
1870 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1871 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1872 if (cmd
== RTM_NEWNEIGH
)
1873 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1874 req
.n
.nlmsg_type
= cmd
;
1875 req
.ndm
.ndm_family
= PF_BRIDGE
;
1876 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1877 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1880 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1881 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1882 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1884 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1889 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1891 * a "flood" MAC FDB entry.
1893 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1895 if (IS_ZEBRA_DEBUG_VXLAN
)
1896 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1897 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1899 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1903 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1904 * deleting the "flood" MAC FDB entry.
1906 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1908 if (IS_ZEBRA_DEBUG_VXLAN
)
1910 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1911 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1913 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1917 #define NDA_RTA(r) \
1918 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1921 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1924 struct interface
*ifp
;
1925 struct zebra_if
*zif
;
1926 struct rtattr
*tb
[NDA_MAX
+ 1];
1927 struct interface
*br_if
;
1930 struct prefix vtep_ip
;
1931 int vid_present
= 0, dst_present
= 0;
1932 char buf
[ETHER_ADDR_STRLEN
];
1937 ndm
= NLMSG_DATA(h
);
1939 /* We only process macfdb notifications if EVPN is enabled */
1940 if (!is_evpn_enabled())
1943 /* The interface should exist. */
1944 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1946 if (!ifp
|| !ifp
->info
)
1949 /* The interface should be something we're interested in. */
1950 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1953 /* Drop "permanent" entries. */
1954 if (ndm
->ndm_state
& NUD_PERMANENT
)
1957 zif
= (struct zebra_if
*)ifp
->info
;
1958 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1959 zlog_debug("%s family %s IF %s(%u) brIF %u - no bridge master",
1960 nl_msg_type_to_str(h
->nlmsg_type
),
1961 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1962 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1966 /* Parse attributes and extract fields of interest. */
1967 memset(tb
, 0, sizeof tb
);
1968 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1970 if (!tb
[NDA_LLADDR
]) {
1971 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
1972 nl_msg_type_to_str(h
->nlmsg_type
),
1973 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1974 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1978 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1980 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1981 nl_msg_type_to_str(h
->nlmsg_type
),
1982 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1983 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1984 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1988 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1990 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1992 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1993 sprintf(vid_buf
, " VLAN %u", vid
);
1997 /* TODO: Only IPv4 supported now. */
1999 vtep_ip
.family
= AF_INET
;
2000 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2001 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2003 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2006 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
2008 if (IS_ZEBRA_DEBUG_KERNEL
)
2009 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2010 nl_msg_type_to_str(h
->nlmsg_type
),
2011 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2012 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2013 sticky
? "sticky " : "",
2014 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2015 dst_present
? dst_buf
: "");
2017 if (filter_vlan
&& vid
!= filter_vlan
)
2020 /* If add or update, do accordingly if learnt on a "local" interface; if
2021 * the notification is over VxLAN, this has to be related to
2023 * so perform an implicit delete of any local entry (if it exists).
2025 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2026 /* Drop "permanent" entries. */
2027 if (ndm
->ndm_state
& NUD_PERMANENT
)
2030 if (IS_ZEBRA_IF_VXLAN(ifp
))
2031 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2034 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2038 /* This is a delete notification.
2039 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2040 * 2. For a MAC over "local" interface, delete the mac
2041 * Note: We will get notifications from both bridge driver and VxLAN
2043 * Ignore the notification from VxLan driver as it is also generated
2044 * when mac moves from remote to local.
2049 if (IS_ZEBRA_IF_VXLAN(ifp
))
2050 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2053 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2056 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2061 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2064 /* Length validity. */
2065 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2069 /* We are interested only in AF_BRIDGE notifications. */
2070 ndm
= NLMSG_DATA(h
);
2071 if (ndm
->ndm_family
!= AF_BRIDGE
)
2074 return netlink_macfdb_change(h
, len
, ns_id
);
2077 /* Request for MAC FDB information from the kernel */
2078 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2079 ifindex_t master_ifindex
)
2083 struct ifinfomsg ifm
;
2087 /* Form the request, specifying filter (rtattr) if needed. */
2088 memset(&req
, 0, sizeof(req
));
2089 req
.n
.nlmsg_type
= type
;
2090 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2091 req
.ifm
.ifi_family
= family
;
2093 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2095 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2099 * MAC forwarding database read using netlink interface. This is invoked
2102 int netlink_macfdb_read(struct zebra_ns
*zns
)
2106 /* Get bridge FDB table. */
2107 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2110 /* We are reading entire table. */
2112 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2119 * MAC forwarding database read using netlink interface. This is for a
2120 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2122 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2123 struct interface
*br_if
)
2125 struct zebra_if
*br_zif
;
2126 struct zebra_if
*zif
;
2127 struct zebra_l2info_vxlan
*vxl
;
2131 /* Save VLAN we're filtering on, if needed. */
2132 br_zif
= (struct zebra_if
*)br_if
->info
;
2133 zif
= (struct zebra_if
*)ifp
->info
;
2134 vxl
= &zif
->l2info
.vxl
;
2135 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2136 filter_vlan
= vxl
->access_vlan
;
2138 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2140 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2144 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2147 /* Reset VLAN filter. */
2152 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2153 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2154 int local
, int cmd
, uint8_t sticky
)
2156 struct zebra_ns
*zns
;
2163 struct zebra_if
*zif
;
2164 struct interface
*br_if
;
2165 struct zebra_if
*br_zif
;
2166 char buf
[ETHER_ADDR_STRLEN
];
2167 int vid_present
= 0, dst_present
= 0;
2170 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2174 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2175 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2176 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2181 memset(&req
, 0, sizeof(req
));
2183 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2184 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2185 if (cmd
== RTM_NEWNEIGH
)
2186 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2187 req
.n
.nlmsg_type
= cmd
;
2188 req
.ndm
.ndm_family
= AF_BRIDGE
;
2189 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2190 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2193 req
.ndm
.ndm_state
|= NUD_NOARP
;
2195 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2197 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2198 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2200 dst_alen
= 4; // TODO: hardcoded
2201 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2203 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2205 br_zif
= (struct zebra_if
*)br_if
->info
;
2206 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2207 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2209 sprintf(vid_buf
, " VLAN %u", vid
);
2211 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2213 if (IS_ZEBRA_DEBUG_KERNEL
)
2214 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2215 nl_msg_type_to_str(cmd
),
2216 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2217 ifp
->ifindex
, vid_present
? vid_buf
: "",
2218 sticky
? "sticky " : "",
2219 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2220 dst_present
? dst_buf
: "");
2222 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2227 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2230 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2233 struct interface
*ifp
;
2234 struct zebra_if
*zif
;
2235 struct rtattr
*tb
[NDA_MAX
+ 1];
2236 struct interface
*link_if
;
2239 char buf
[ETHER_ADDR_STRLEN
];
2240 char buf2
[INET6_ADDRSTRLEN
];
2241 int mac_present
= 0;
2242 uint8_t ext_learned
;
2243 uint8_t router_flag
;
2245 ndm
= NLMSG_DATA(h
);
2247 /* The interface should exist. */
2248 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2250 if (!ifp
|| !ifp
->info
)
2253 zif
= (struct zebra_if
*)ifp
->info
;
2255 /* Parse attributes and extract fields of interest. */
2256 memset(tb
, 0, sizeof tb
);
2257 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2260 zlog_debug("%s family %s IF %s(%u) - no DST",
2261 nl_msg_type_to_str(h
->nlmsg_type
),
2262 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2267 memset(&ip
, 0, sizeof(struct ipaddr
));
2268 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2269 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2271 /* Drop some "permanent" entries. */
2272 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2273 char buf
[16] = "169.254.0.1";
2274 struct in_addr ipv4_ll
;
2276 if (ndm
->ndm_family
!= AF_INET
)
2279 if (!zif
->v6_2_v4_ll_neigh_entry
)
2282 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2285 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2286 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2289 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2290 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2294 /* The neighbor is present on an SVI. From this, we locate the
2296 * bridge because we're only interested in neighbors on a VxLAN bridge.
2297 * The bridge is located based on the nature of the SVI:
2298 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2300 * and is linked to the bridge
2301 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2305 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2306 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2310 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2315 memset(&mac
, 0, sizeof(struct ethaddr
));
2316 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2317 if (tb
[NDA_LLADDR
]) {
2318 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2320 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2321 nl_msg_type_to_str(h
->nlmsg_type
),
2322 nl_family_to_str(ndm
->ndm_family
),
2323 ifp
->name
, ndm
->ndm_ifindex
,
2324 (unsigned long)RTA_PAYLOAD(
2330 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2333 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2334 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2336 if (IS_ZEBRA_DEBUG_KERNEL
)
2338 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2339 nl_msg_type_to_str(h
->nlmsg_type
),
2340 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2342 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2344 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2346 ndm
->ndm_state
, ndm
->ndm_flags
);
2348 /* If the neighbor state is valid for use, process as an add or
2350 * else process as a delete. Note that the delete handling may
2352 * in re-adding the neighbor if it is a valid "remote" neighbor.
2354 if (ndm
->ndm_state
& NUD_VALID
)
2355 return zebra_vxlan_handle_kernel_neigh_update(
2356 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2357 ext_learned
, router_flag
);
2359 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2362 if (IS_ZEBRA_DEBUG_KERNEL
)
2363 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2364 nl_msg_type_to_str(h
->nlmsg_type
),
2365 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2367 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2369 /* Process the delete - it may result in re-adding the neighbor if it is
2370 * a valid "remote" neighbor.
2372 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2375 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2380 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2383 /* Length validity. */
2384 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2388 /* We are interested only in AF_INET or AF_INET6 notifications. */
2389 ndm
= NLMSG_DATA(h
);
2390 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2393 return netlink_neigh_change(h
, len
);
2396 /* Request for IP neighbor information from the kernel */
2397 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2406 /* Form the request, specifying filter (rtattr) if needed. */
2407 memset(&req
, 0, sizeof(req
));
2408 req
.n
.nlmsg_type
= type
;
2409 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2410 req
.ndm
.ndm_family
= family
;
2412 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2414 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2418 * IP Neighbor table read using netlink interface. This is invoked
2421 int netlink_neigh_read(struct zebra_ns
*zns
)
2425 /* Get IP neighbor table. */
2426 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2429 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2436 * IP Neighbor table read using netlink interface. This is for a specific
2439 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2443 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2447 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2453 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2458 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2461 /* Length validity. */
2462 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2464 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2465 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2466 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2470 /* Is this a notification for the MAC FDB or IP neighbor table? */
2471 ndm
= NLMSG_DATA(h
);
2472 if (ndm
->ndm_family
== AF_BRIDGE
)
2473 return netlink_macfdb_change(h
, len
, ns_id
);
2475 if (ndm
->ndm_type
!= RTN_UNICAST
)
2478 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2479 return netlink_ipneigh_change(h
, len
, ns_id
);
2482 ZEBRA_ERR_UNKNOWN_FAMILY
,
2483 "Invalid address family: %u received from kernel neighbor change: %u",
2484 ndm
->ndm_family
, h
->nlmsg_type
);
2491 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2492 struct ethaddr
*mac
, uint8_t flags
,
2493 uint16_t state
, int cmd
)
2502 struct zebra_ns
*zns
;
2503 char buf
[INET6_ADDRSTRLEN
];
2504 char buf2
[ETHER_ADDR_STRLEN
];
2505 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2508 memset(&req
, 0, sizeof(req
));
2510 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2511 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2512 if (cmd
== RTM_NEWNEIGH
)
2513 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2514 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2515 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2516 req
.ndm
.ndm_state
= state
;
2517 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2518 req
.ndm
.ndm_type
= RTN_UNICAST
;
2519 req
.ndm
.ndm_flags
= flags
;
2521 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2522 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2524 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2526 if (IS_ZEBRA_DEBUG_KERNEL
)
2527 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2528 nl_msg_type_to_str(cmd
),
2529 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2530 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2531 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2534 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2538 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2539 struct in_addr vtep_ip
, uint8_t sticky
)
2541 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2545 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2546 struct in_addr vtep_ip
, int local
)
2548 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2552 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2553 struct ethaddr
*mac
, uint8_t flags
)
2555 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2556 NUD_NOARP
, RTM_NEWNEIGH
);
2559 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2561 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2565 * MPLS label forwarding table change via netlink interface.
2567 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2570 zebra_nhlfe_t
*nhlfe
;
2571 struct nexthop
*nexthop
= NULL
;
2572 unsigned int nexthop_num
;
2573 const char *routedesc
;
2574 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2580 char buf
[NL_PKT_BUF_SIZE
];
2583 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2586 * Count # nexthops so we can decide whether to use singlepath
2587 * or multipath case.
2590 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2591 nexthop
= nhlfe
->nexthop
;
2594 if (cmd
== RTM_NEWROUTE
) {
2595 /* Count all selected NHLFEs */
2596 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2597 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2601 /* Count all installed NHLFEs */
2602 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2603 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2608 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2611 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2612 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2613 req
.n
.nlmsg_type
= cmd
;
2614 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2616 req
.r
.rtm_family
= AF_MPLS
;
2617 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2618 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2619 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2620 req
.r
.rtm_type
= RTN_UNICAST
;
2622 if (cmd
== RTM_NEWROUTE
) {
2623 /* We do a replace to handle update. */
2624 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2626 /* set the protocol value if installing */
2627 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2628 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2631 /* Fill destination */
2632 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2633 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2635 /* Fill nexthops (paths) based on single-path or multipath. The paths
2636 * chosen depend on the operation.
2638 if (nexthop_num
== 1 || multipath_num
== 1) {
2639 routedesc
= "single-path";
2640 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2643 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2644 nexthop
= nhlfe
->nexthop
;
2648 if ((cmd
== RTM_NEWROUTE
2649 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2650 && CHECK_FLAG(nexthop
->flags
,
2651 NEXTHOP_FLAG_ACTIVE
)))
2652 || (cmd
== RTM_DELROUTE
2653 && (CHECK_FLAG(nhlfe
->flags
,
2654 NHLFE_FLAG_INSTALLED
)
2655 && CHECK_FLAG(nexthop
->flags
,
2656 NEXTHOP_FLAG_FIB
)))) {
2657 /* Add the gateway */
2658 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2665 } else /* Multipath case */
2667 char buf
[NL_PKT_BUF_SIZE
];
2668 struct rtattr
*rta
= (void *)buf
;
2669 struct rtnexthop
*rtnh
;
2670 union g_addr
*src1
= NULL
;
2672 rta
->rta_type
= RTA_MULTIPATH
;
2673 rta
->rta_len
= RTA_LENGTH(0);
2674 rtnh
= RTA_DATA(rta
);
2676 routedesc
= "multipath";
2677 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2680 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2681 nexthop
= nhlfe
->nexthop
;
2685 if (nexthop_num
>= multipath_num
)
2688 if ((cmd
== RTM_NEWROUTE
2689 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2690 && CHECK_FLAG(nexthop
->flags
,
2691 NEXTHOP_FLAG_ACTIVE
)))
2692 || (cmd
== RTM_DELROUTE
2693 && (CHECK_FLAG(nhlfe
->flags
,
2694 NHLFE_FLAG_INSTALLED
)
2695 && CHECK_FLAG(nexthop
->flags
,
2696 NEXTHOP_FLAG_FIB
)))) {
2699 /* Build the multipath */
2700 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2703 rtnh
= RTNH_NEXT(rtnh
);
2707 /* Add the multipath */
2708 if (rta
->rta_len
> RTA_LENGTH(0))
2709 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2710 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2713 /* Talk to netlink socket. */
2714 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2717 #endif /* HAVE_NETLINK */