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
156 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
157 __PRETTY_FUNCTION__
, proto
);
158 proto
= RTPROT_ZEBRA
;
165 static inline int proto2zebra(int proto
, int family
)
169 proto
= ZEBRA_ROUTE_BABEL
;
172 proto
= ZEBRA_ROUTE_BGP
;
175 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
179 proto
= ZEBRA_ROUTE_ISIS
;
182 proto
= ZEBRA_ROUTE_RIP
;
185 proto
= ZEBRA_ROUTE_RIPNG
;
188 proto
= ZEBRA_ROUTE_NHRP
;
191 proto
= ZEBRA_ROUTE_EIGRP
;
194 proto
= ZEBRA_ROUTE_LDP
;
198 proto
= ZEBRA_ROUTE_STATIC
;
201 proto
= ZEBRA_ROUTE_SHARP
;
204 proto
= ZEBRA_ROUTE_PBR
;
208 * When a user adds a new protocol this will show up
209 * to let them know to do something about it. This
210 * is intentionally a warn because we should see
211 * this as part of development of a new protocol
213 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
216 proto
= ZEBRA_ROUTE_KERNEL
;
223 Pending: create an efficient table_id (in a tree/hash) based lookup)
225 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
228 struct zebra_vrf
*zvrf
;
230 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
234 /* case vrf with netns : match the netnsid */
235 if (vrf_is_backend_netns()) {
236 if (ns_id
== zvrf_id(zvrf
))
237 return zvrf_id(zvrf
);
239 /* VRF is VRF_BACKEND_VRF_LITE */
240 if (zvrf
->table_id
!= table_id
)
242 return zvrf_id(zvrf
);
249 /* Looking up routing table by netlink interface. */
250 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
255 struct rtattr
*tb
[RTA_MAX
+ 1];
258 struct prefix_ipv6 src_p
= {};
261 char anyaddr
[16] = {0};
263 int proto
= ZEBRA_ROUTE_KERNEL
;
268 uint8_t distance
= 0;
273 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
274 void *src
= NULL
; /* IPv6 srcdest source prefix */
275 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
279 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
281 switch (rtm
->rtm_type
) {
285 bh_type
= BLACKHOLE_NULL
;
287 case RTN_UNREACHABLE
:
288 bh_type
= BLACKHOLE_REJECT
;
291 bh_type
= BLACKHOLE_ADMINPROHIB
;
297 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
299 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
300 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
301 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
305 memset(tb
, 0, sizeof tb
);
306 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
308 if (rtm
->rtm_flags
& RTM_F_CLONED
)
310 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
312 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
315 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
316 && h
->nlmsg_type
== RTM_NEWROUTE
) {
317 if (IS_ZEBRA_DEBUG_KERNEL
)
318 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
323 /* We don't care about change notifications for the MPLS table. */
324 /* TODO: Revisit this. */
325 if (rtm
->rtm_family
== AF_MPLS
)
328 /* Table corresponding to route. */
330 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
332 table
= rtm
->rtm_table
;
335 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
336 if (vrf_id
== VRF_DEFAULT
) {
337 if (!is_zebra_valid_kernel_table(table
)
338 && !is_zebra_main_routing_table(table
))
342 /* Route which inserted by Zebra. */
343 if (is_selfroute(rtm
->rtm_protocol
)) {
344 flags
|= ZEBRA_FLAG_SELFROUTE
;
345 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
348 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
351 dest
= RTA_DATA(tb
[RTA_DST
]);
356 src
= RTA_DATA(tb
[RTA_SRC
]);
361 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
364 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
366 if (tb
[RTA_PRIORITY
])
367 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
369 #if defined(SUPPORT_REALMS)
371 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
374 if (tb
[RTA_METRICS
]) {
375 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
377 memset(mxrta
, 0, sizeof mxrta
);
378 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
379 RTA_PAYLOAD(tb
[RTA_METRICS
]));
382 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
385 if (rtm
->rtm_family
== AF_INET
) {
387 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
389 "Invalid destination prefix length: %u received from kernel route change",
393 memcpy(&p
.u
.prefix4
, dest
, 4);
394 p
.prefixlen
= rtm
->rtm_dst_len
;
396 if (rtm
->rtm_src_len
!= 0) {
397 char buf
[PREFIX_STRLEN
];
398 zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)",
399 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
403 /* Force debug below to not display anything for source */
405 } else if (rtm
->rtm_family
== AF_INET6
) {
407 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
409 "Invalid destination prefix length: %u received from kernel route change",
413 memcpy(&p
.u
.prefix6
, dest
, 16);
414 p
.prefixlen
= rtm
->rtm_dst_len
;
416 src_p
.family
= AF_INET6
;
417 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
419 "Invalid source prefix length: %u received from kernel route change",
423 memcpy(&src_p
.prefix
, src
, 16);
424 src_p
.prefixlen
= rtm
->rtm_src_len
;
428 * For ZEBRA_ROUTE_KERNEL types:
430 * The metric/priority of the route received from the kernel
431 * is a 32 bit number. We are going to interpret the high
432 * order byte as the Admin Distance and the low order 3 bytes
435 * This will allow us to do two things:
436 * 1) Allow the creation of kernel routes that can be
437 * overridden by zebra.
438 * 2) Allow the old behavior for 'most' kernel route types
439 * if a user enters 'ip route ...' v4 routes get a metric
440 * of 0 and v6 routes get a metric of 1024. Both of these
441 * values will end up with a admin distance of 0, which
442 * will cause them to win for the purposes of zebra.
444 if (proto
== ZEBRA_ROUTE_KERNEL
) {
445 distance
= (metric
>> 24) & 0xFF;
446 metric
= (metric
& 0x00FFFFFF);
449 if (IS_ZEBRA_DEBUG_KERNEL
) {
450 char buf
[PREFIX_STRLEN
];
451 char buf2
[PREFIX_STRLEN
];
452 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
453 nl_msg_type_to_str(h
->nlmsg_type
),
454 prefix2str(&p
, buf
, sizeof(buf
)),
455 src_p
.prefixlen
? " from " : "",
457 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
459 vrf_id
, table
, metric
, distance
);
463 if (rtm
->rtm_family
== AF_INET6
)
466 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
467 struct interface
*ifp
;
468 vrf_id_t nh_vrf_id
= vrf_id
;
470 if (!tb
[RTA_MULTIPATH
]) {
472 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
474 memset(&nh
, 0, sizeof(nh
));
476 if (bh_type
== BLACKHOLE_UNSPEC
) {
478 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
479 else if (index
&& gate
)
482 ? NEXTHOP_TYPE_IPV4_IFINDEX
483 : NEXTHOP_TYPE_IPV6_IFINDEX
;
484 else if (!index
&& gate
)
485 nh
.type
= (afi
== AFI_IP
)
489 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
490 nh
.bh_type
= bh_type
;
493 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
494 nh
.bh_type
= bh_type
;
498 memcpy(&nh
.src
, prefsrc
, sz
);
500 memcpy(&nh
.gate
, gate
, sz
);
503 ifp
= if_lookup_by_index_per_ns(
504 zebra_ns_lookup(ns_id
),
507 nh_vrf_id
= ifp
->vrf_id
;
509 nh
.vrf_id
= nh_vrf_id
;
511 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
512 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
514 /* This is a multipath route */
516 struct route_entry
*re
;
517 struct rtnexthop
*rtnh
=
518 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
520 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
522 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
524 re
->distance
= distance
;
531 re
->uptime
= time(NULL
);
536 if (len
< (int)sizeof(*rtnh
)
537 || rtnh
->rtnh_len
> len
)
540 index
= rtnh
->rtnh_ifindex
;
543 * Yes we are looking this up
544 * for every nexthop and just
545 * using the last one looked
548 ifp
= if_lookup_by_index_per_ns(
549 zebra_ns_lookup(ns_id
),
552 nh_vrf_id
= ifp
->vrf_id
;
555 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
558 nh_vrf_id
= VRF_DEFAULT
;
564 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
565 memset(tb
, 0, sizeof(tb
));
566 netlink_parse_rtattr(
567 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
568 rtnh
->rtnh_len
- sizeof(*rtnh
));
575 if (rtm
->rtm_family
== AF_INET
) {
577 route_entry_nexthop_ipv4_ifindex_add(
582 route_entry_nexthop_ipv4_add(
586 } else if (rtm
->rtm_family
589 route_entry_nexthop_ipv6_ifindex_add(
593 route_entry_nexthop_ipv6_add(
598 route_entry_nexthop_ifindex_add(
599 re
, index
, nh_vrf_id
);
601 if (rtnh
->rtnh_len
== 0)
604 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
605 rtnh
= RTNH_NEXT(rtnh
);
608 zserv_nexthop_num_warn(__func__
,
609 (const struct prefix
*)&p
,
611 if (re
->nexthop_num
== 0)
614 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
618 if (!tb
[RTA_MULTIPATH
]) {
620 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
622 memset(&nh
, 0, sizeof(nh
));
623 if (bh_type
== BLACKHOLE_UNSPEC
) {
625 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
626 else if (index
&& gate
)
629 ? NEXTHOP_TYPE_IPV4_IFINDEX
630 : NEXTHOP_TYPE_IPV6_IFINDEX
;
631 else if (!index
&& gate
)
632 nh
.type
= (afi
== AFI_IP
)
636 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
637 nh
.bh_type
= BLACKHOLE_UNSPEC
;
640 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
641 nh
.bh_type
= bh_type
;
645 memcpy(&nh
.gate
, gate
, sz
);
646 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
647 &p
, &src_p
, &nh
, table
, metric
, true);
649 /* XXX: need to compare the entire list of nexthops
650 * here for NLM_F_APPEND stupidity */
651 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
652 &p
, &src_p
, NULL
, table
, metric
, true);
659 static struct mcast_route_data
*mroute
= NULL
;
661 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
662 ns_id_t ns_id
, int startup
)
666 struct rtattr
*tb
[RTA_MAX
+ 1];
667 struct mcast_route_data
*m
;
668 struct mcast_route_data mr
;
675 char oif_list
[256] = "\0";
682 memset(&mr
, 0, sizeof(mr
));
688 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
690 memset(tb
, 0, sizeof tb
);
691 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
694 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
696 table
= rtm
->rtm_table
;
698 vrf
= vrf_lookup_by_table(table
, ns_id
);
701 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
704 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
707 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
709 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
710 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
712 if (tb
[RTA_MULTIPATH
]) {
713 struct rtnexthop
*rtnh
=
714 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
716 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
718 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
721 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
724 if (rtnh
->rtnh_len
== 0)
727 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
728 rtnh
= RTNH_NEXT(rtnh
);
732 if (IS_ZEBRA_DEBUG_KERNEL
) {
733 struct interface
*ifp
;
734 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
735 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
736 for (count
= 0; count
< oif_count
; count
++) {
737 ifp
= if_lookup_by_index(oif
[count
], vrf
);
740 sprintf(temp
, "%s ", ifp
->name
);
741 strcat(oif_list
, temp
);
743 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
744 ifp
= if_lookup_by_index(iif
, vrf
);
746 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
747 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
748 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
753 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
760 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
761 /* If this is not route add/delete message print warning. */
762 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
766 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
)) {
768 "Invalid address family: %u received from kernel route change: %u",
769 rtm
->rtm_family
, h
->nlmsg_type
);
773 /* Connected route. */
774 if (IS_ZEBRA_DEBUG_KERNEL
)
775 zlog_debug("%s %s %s proto %s NS %u",
776 nl_msg_type_to_str(h
->nlmsg_type
),
777 nl_family_to_str(rtm
->rtm_family
),
778 nl_rttype_to_str(rtm
->rtm_type
),
779 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
782 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
784 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
787 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
791 if (rtm
->rtm_type
== RTN_MULTICAST
)
792 netlink_route_change_read_multicast(h
, ns_id
, startup
);
794 netlink_route_change_read_unicast(h
, ns_id
, startup
);
798 /* Request for specific route information from the kernel */
799 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
806 /* Form the request, specifying filter (rtattr) if needed. */
807 memset(&req
, 0, sizeof(req
));
808 req
.n
.nlmsg_type
= type
;
809 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
810 req
.rtm
.rtm_family
= family
;
812 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
815 /* Routing table read function using netlink interface. Only called
817 int netlink_route_read(struct zebra_ns
*zns
)
821 /* Get IPv4 routing table. */
822 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
825 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
826 &zns
->netlink_cmd
, zns
, 0, 1);
830 /* Get IPv6 routing table. */
831 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
834 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
835 &zns
->netlink_cmd
, zns
, 0, 1);
842 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
844 struct nlmsghdr
*nlmsg
,
845 size_t req_size
, int bytelen
,
846 struct nexthop
*nexthop
)
848 if (route_family
== AF_MPLS
) {
849 struct gw_family_t gw_fam
;
851 gw_fam
.family
= gw_family
;
852 if (gw_family
== AF_INET
)
853 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
855 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
856 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
859 if (gw_family
== AF_INET
)
860 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
861 &nexthop
->gate
.ipv4
, bytelen
);
863 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
864 &nexthop
->gate
.ipv6
, bytelen
);
868 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
871 struct rtnexthop
*rtnh
,
872 size_t req_size
, int bytelen
,
873 struct nexthop
*nexthop
)
875 if (route_family
== AF_MPLS
) {
876 struct gw_family_t gw_fam
;
878 gw_fam
.family
= gw_family
;
879 if (gw_family
== AF_INET
)
880 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
882 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
883 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
885 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
887 if (gw_family
== AF_INET
)
888 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
889 &nexthop
->gate
.ipv4
, bytelen
);
891 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
892 &nexthop
->gate
.ipv6
, bytelen
);
893 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
897 /* This function takes a nexthop as argument and adds
898 * the appropriate netlink attributes to an existing
901 * @param routedesc: Human readable description of route type
902 * (direct/recursive, single-/multipath)
903 * @param bytelen: Length of addresses in bytes.
904 * @param nexthop: Nexthop information
905 * @param nlmsg: nlmsghdr structure to fill in.
906 * @param req_size: The size allocated for the message.
908 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
909 struct nexthop
*nexthop
,
910 struct nlmsghdr
*nlmsg
,
912 size_t req_size
, int cmd
)
914 struct mpls_label_stack
*nh_label
;
915 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
920 * label_buf is *only* currently used within debugging.
921 * As such when we assign it we are guarding it inside
922 * a debug test. If you want to change this make sure
923 * you fix this assumption
928 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
931 nh_label
= nh
->nh_label
;
932 if (!nh_label
|| !nh_label
->num_labels
)
935 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
936 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
939 if (IS_ZEBRA_DEBUG_KERNEL
) {
941 sprintf(label_buf
, "label %u",
944 sprintf(label_buf1
, "/%u",
946 strlcat(label_buf
, label_buf1
,
951 out_lse
[num_labels
] =
952 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
958 /* Set the BoS bit */
959 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
961 if (rtmsg
->rtm_family
== AF_MPLS
)
962 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
963 num_labels
* sizeof(mpls_lse_t
));
966 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
968 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
970 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
971 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
972 num_labels
* sizeof(mpls_lse_t
));
973 addattr_nest_end(nlmsg
, nest
);
977 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
978 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
980 if (rtmsg
->rtm_family
== AF_INET
981 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
982 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
983 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
984 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
985 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
987 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
988 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
989 &nexthop
->rmap_src
.ipv4
, bytelen
);
990 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
991 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
992 &nexthop
->src
.ipv4
, bytelen
);
994 if (IS_ZEBRA_DEBUG_KERNEL
)
996 " 5549: _netlink_route_build_singlepath() (%s): "
997 "nexthop via %s %s if %u(%u)",
998 routedesc
, ipv4_ll_buf
, label_buf
,
999 nexthop
->ifindex
, nexthop
->vrf_id
);
1003 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1004 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1005 /* Send deletes to the kernel without specifying the next-hop */
1006 if (cmd
!= RTM_DELROUTE
)
1007 _netlink_route_nl_add_gateway_info(
1008 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1011 if (cmd
== RTM_NEWROUTE
) {
1012 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1013 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1014 &nexthop
->rmap_src
.ipv4
, bytelen
);
1015 else if (nexthop
->src
.ipv4
.s_addr
)
1016 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1017 &nexthop
->src
.ipv4
, bytelen
);
1020 if (IS_ZEBRA_DEBUG_KERNEL
)
1022 "netlink_route_multipath() (%s): "
1023 "nexthop via %s %s if %u(%u)",
1024 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1025 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1028 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1029 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1030 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1031 nlmsg
, req_size
, bytelen
,
1034 if (cmd
== RTM_NEWROUTE
) {
1035 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1036 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1037 &nexthop
->rmap_src
.ipv6
, bytelen
);
1038 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1039 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1040 &nexthop
->src
.ipv6
, bytelen
);
1043 if (IS_ZEBRA_DEBUG_KERNEL
)
1045 "netlink_route_multipath() (%s): "
1046 "nexthop via %s %s if %u(%u)",
1047 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1048 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1052 * We have the ifindex so we should always send it
1053 * This is especially useful if we are doing route
1056 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1057 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1059 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1060 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1061 if (cmd
== RTM_NEWROUTE
) {
1062 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1063 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1064 &nexthop
->rmap_src
.ipv4
, bytelen
);
1065 else if (nexthop
->src
.ipv4
.s_addr
)
1066 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1067 &nexthop
->src
.ipv4
, bytelen
);
1070 if (IS_ZEBRA_DEBUG_KERNEL
)
1072 "netlink_route_multipath() (%s): "
1073 "nexthop via if %u(%u)",
1074 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1077 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1078 if (cmd
== RTM_NEWROUTE
) {
1079 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1080 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1081 &nexthop
->rmap_src
.ipv6
, bytelen
);
1082 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1083 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1084 &nexthop
->src
.ipv6
, bytelen
);
1087 if (IS_ZEBRA_DEBUG_KERNEL
)
1089 "netlink_route_multipath() (%s): "
1090 "nexthop via if %u(%u)",
1091 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1095 /* This function takes a nexthop as argument and
1096 * appends to the given rtattr/rtnexthop pair the
1097 * representation of the nexthop. If the nexthop
1098 * defines a preferred source, the src parameter
1099 * will be modified to point to that src, otherwise
1100 * it will be kept unmodified.
1102 * @param routedesc: Human readable description of route type
1103 * (direct/recursive, single-/multipath)
1104 * @param bytelen: Length of addresses in bytes.
1105 * @param nexthop: Nexthop information
1106 * @param rta: rtnetlink attribute structure
1107 * @param rtnh: pointer to an rtnetlink nexthop structure
1108 * @param src: pointer pointing to a location where
1109 * the prefsrc should be stored.
1111 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1112 struct nexthop
*nexthop
,
1114 struct rtnexthop
*rtnh
,
1115 struct rtmsg
*rtmsg
,
1118 struct mpls_label_stack
*nh_label
;
1119 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1121 char label_buf
[256];
1123 rtnh
->rtnh_len
= sizeof(*rtnh
);
1124 rtnh
->rtnh_flags
= 0;
1125 rtnh
->rtnh_hops
= 0;
1126 rta
->rta_len
+= rtnh
->rtnh_len
;
1129 * label_buf is *only* currently used within debugging.
1130 * As such when we assign it we are guarding it inside
1131 * a debug test. If you want to change this make sure
1132 * you fix this assumption
1134 label_buf
[0] = '\0';
1137 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1138 char label_buf1
[20];
1140 nh_label
= nh
->nh_label
;
1141 if (!nh_label
|| !nh_label
->num_labels
)
1144 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1145 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1148 if (IS_ZEBRA_DEBUG_KERNEL
) {
1150 sprintf(label_buf
, "label %u",
1151 nh_label
->label
[i
]);
1153 sprintf(label_buf1
, "/%u",
1154 nh_label
->label
[i
]);
1155 strlcat(label_buf
, label_buf1
,
1160 out_lse
[num_labels
] =
1161 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1167 /* Set the BoS bit */
1168 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1170 if (rtmsg
->rtm_family
== AF_MPLS
) {
1171 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1173 num_labels
* sizeof(mpls_lse_t
));
1175 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1177 struct rtattr
*nest
;
1178 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1179 int len
= rta
->rta_len
;
1181 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1182 &encap
, sizeof(uint16_t));
1183 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1184 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1186 num_labels
* sizeof(mpls_lse_t
));
1187 rta_nest_end(rta
, nest
);
1188 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1192 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1193 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1195 if (rtmsg
->rtm_family
== AF_INET
1196 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1197 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1199 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1200 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1202 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1203 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1205 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1206 *src
= &nexthop
->rmap_src
;
1207 else if (nexthop
->src
.ipv4
.s_addr
)
1208 *src
= &nexthop
->src
;
1210 if (IS_ZEBRA_DEBUG_KERNEL
)
1212 " 5549: netlink_route_build_multipath() (%s): "
1213 "nexthop via %s %s if %u",
1214 routedesc
, ipv4_ll_buf
, label_buf
,
1219 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1220 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1221 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1222 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1224 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1225 *src
= &nexthop
->rmap_src
;
1226 else if (nexthop
->src
.ipv4
.s_addr
)
1227 *src
= &nexthop
->src
;
1229 if (IS_ZEBRA_DEBUG_KERNEL
)
1231 "netlink_route_multipath() (%s): "
1232 "nexthop via %s %s if %u",
1233 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1234 label_buf
, nexthop
->ifindex
);
1236 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1237 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1238 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1239 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1242 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1243 *src
= &nexthop
->rmap_src
;
1244 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1245 *src
= &nexthop
->src
;
1247 if (IS_ZEBRA_DEBUG_KERNEL
)
1249 "netlink_route_multipath() (%s): "
1250 "nexthop via %s %s if %u",
1251 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1252 label_buf
, nexthop
->ifindex
);
1256 * We have figured out the ifindex so we should always send it
1257 * This is especially useful if we are doing route
1260 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1261 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1264 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1265 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1266 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1267 *src
= &nexthop
->rmap_src
;
1268 else if (nexthop
->src
.ipv4
.s_addr
)
1269 *src
= &nexthop
->src
;
1271 if (IS_ZEBRA_DEBUG_KERNEL
)
1273 "netlink_route_multipath() (%s): "
1274 "nexthop via if %u",
1275 routedesc
, nexthop
->ifindex
);
1279 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1280 zebra_nhlfe_t
*nhlfe
,
1281 struct nlmsghdr
*nlmsg
,
1282 struct rtmsg
*rtmsg
,
1283 size_t req_size
, int cmd
)
1288 family
= NHLFE_FAMILY(nhlfe
);
1289 bytelen
= (family
== AF_INET
? 4 : 16);
1290 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1291 nlmsg
, rtmsg
, req_size
, cmd
);
1296 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1297 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1298 struct rtmsg
*rtmsg
, union g_addr
**src
)
1303 family
= NHLFE_FAMILY(nhlfe
);
1304 bytelen
= (family
== AF_INET
? 4 : 16);
1305 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1310 /* Log debug information for netlink_route_multipath
1311 * if debug logging is enabled.
1313 * @param cmd: Netlink command which is to be processed
1314 * @param p: Prefix for which the change is due
1315 * @param family: Address family which the change concerns
1316 * @param zvrf: The vrf we are in
1317 * @param tableid: The table we are working on
1319 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1320 int family
, vrf_id_t vrfid
,
1323 if (IS_ZEBRA_DEBUG_KERNEL
) {
1324 char buf
[PREFIX_STRLEN
];
1326 "netlink_route_multipath(): %s %s vrf %u(%u)",
1327 nl_msg_type_to_str(cmd
),
1328 prefix2str(p
, buf
, sizeof(buf
)),
1333 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1335 if (IS_ZEBRA_DEBUG_KERNEL
)
1336 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1337 nl_msg_type_to_str(cmd
), label
);
1340 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1341 int llalen
, ns_id_t ns_id
)
1349 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1351 memset(&req
, 0, sizeof(req
));
1353 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1354 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1355 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1356 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1358 req
.ndm
.ndm_family
= AF_INET
;
1359 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1360 req
.ndm
.ndm_ifindex
= ifindex
;
1361 req
.ndm
.ndm_type
= RTN_UNICAST
;
1363 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1364 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1366 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1370 /* Routing table change via netlink interface. */
1371 /* Update flag indicates whether this is a "replace" or not. */
1372 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1373 const struct prefix
*src_p
,
1374 struct route_entry
*re
,
1378 struct sockaddr_nl snl
;
1379 struct nexthop
*nexthop
= NULL
;
1380 unsigned int nexthop_num
;
1381 int family
= PREFIX_FAMILY(p
);
1382 const char *routedesc
;
1389 char buf
[NL_PKT_BUF_SIZE
];
1392 struct zebra_ns
*zns
;
1393 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1396 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1398 bytelen
= (family
== AF_INET
? 4 : 16);
1400 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1401 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1402 if ((cmd
== RTM_NEWROUTE
) && update
)
1403 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1404 req
.n
.nlmsg_type
= cmd
;
1405 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1407 req
.r
.rtm_family
= family
;
1408 req
.r
.rtm_dst_len
= p
->prefixlen
;
1409 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1410 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1411 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1414 * blackhole routes are not RTN_UNICAST, they are
1415 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1416 * so setting this value as a RTN_UNICAST would
1417 * cause the route lookup of just the prefix
1418 * to fail. So no need to specify this for
1419 * the RTM_DELROUTE case
1421 if (cmd
!= RTM_DELROUTE
)
1422 req
.r
.rtm_type
= RTN_UNICAST
;
1424 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1426 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1430 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1432 * either by the kernel or by zebra. Its purely for calculating best
1434 * by the routing protocol and for communicating with protocol peers.
1436 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1437 #if defined(SUPPORT_REALMS)
1438 if (re
->tag
> 0 && re
->tag
<= 255)
1439 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1441 /* Table corresponding to this route. */
1442 if (re
->table
< 256)
1443 req
.r
.rtm_table
= re
->table
;
1445 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1446 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1449 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1452 * If we are not updating the route and we have received
1453 * a route delete, then all we need to fill in is the
1454 * prefix information to tell the kernel to schwack
1457 if (!update
&& cmd
== RTM_DELROUTE
)
1460 if (re
->mtu
|| re
->nexthop_mtu
) {
1461 char buf
[NL_PKT_BUF_SIZE
];
1462 struct rtattr
*rta
= (void *)buf
;
1463 uint32_t mtu
= re
->mtu
;
1464 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1465 mtu
= re
->nexthop_mtu
;
1466 rta
->rta_type
= RTA_METRICS
;
1467 rta
->rta_len
= RTA_LENGTH(0);
1468 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1469 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1473 /* Count overall nexthops so we can decide whether to use singlepath
1474 * or multipath case. */
1476 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1477 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1479 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1481 if (cmd
== RTM_DELROUTE
1482 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1488 /* Singlepath case. */
1489 if (nexthop_num
== 1 || multipath_num
== 1) {
1491 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1493 * So we want to cover 2 types of blackhole
1495 * 1) A normal blackhole route( ala from a static
1497 * 2) A recursively resolved blackhole route
1499 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1500 switch (nexthop
->bh_type
) {
1501 case BLACKHOLE_ADMINPROHIB
:
1502 req
.r
.rtm_type
= RTN_PROHIBIT
;
1504 case BLACKHOLE_REJECT
:
1505 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1508 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1513 if (CHECK_FLAG(nexthop
->flags
,
1514 NEXTHOP_FLAG_RECURSIVE
)) {
1516 if (family
== AF_INET
) {
1517 if (nexthop
->rmap_src
.ipv4
1524 } else if (nexthop
->src
.ipv4
1532 } else if (family
== AF_INET6
) {
1533 if (!IN6_IS_ADDR_UNSPECIFIED(
1541 !IN6_IS_ADDR_UNSPECIFIED(
1554 if ((cmd
== RTM_NEWROUTE
1555 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1556 || (cmd
== RTM_DELROUTE
1557 && CHECK_FLAG(nexthop
->flags
,
1558 NEXTHOP_FLAG_FIB
))) {
1559 routedesc
= nexthop
->rparent
1560 ? "recursive, single-path"
1563 _netlink_route_build_singlepath(
1564 routedesc
, bytelen
, nexthop
, &req
.n
,
1565 &req
.r
, sizeof req
, cmd
);
1570 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1571 if (family
== AF_INET
)
1572 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1573 &src
.ipv4
, bytelen
);
1574 else if (family
== AF_INET6
)
1575 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1576 &src
.ipv6
, bytelen
);
1579 char buf
[NL_PKT_BUF_SIZE
];
1580 struct rtattr
*rta
= (void *)buf
;
1581 struct rtnexthop
*rtnh
;
1582 union g_addr
*src1
= NULL
;
1584 rta
->rta_type
= RTA_MULTIPATH
;
1585 rta
->rta_len
= RTA_LENGTH(0);
1586 rtnh
= RTA_DATA(rta
);
1589 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1590 if (nexthop_num
>= multipath_num
)
1593 if (CHECK_FLAG(nexthop
->flags
,
1594 NEXTHOP_FLAG_RECURSIVE
)) {
1595 /* This only works for IPv4 now */
1597 if (family
== AF_INET
) {
1598 if (nexthop
->rmap_src
.ipv4
1605 } else if (nexthop
->src
.ipv4
1613 } else if (family
== AF_INET6
) {
1614 if (!IN6_IS_ADDR_UNSPECIFIED(
1622 !IN6_IS_ADDR_UNSPECIFIED(
1635 if ((cmd
== RTM_NEWROUTE
1636 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1637 || (cmd
== RTM_DELROUTE
1638 && CHECK_FLAG(nexthop
->flags
,
1639 NEXTHOP_FLAG_FIB
))) {
1640 routedesc
= nexthop
->rparent
1641 ? "recursive, multipath"
1645 _netlink_route_build_multipath(
1646 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1648 rtnh
= RTNH_NEXT(rtnh
);
1650 if (!setsrc
&& src1
) {
1651 if (family
== AF_INET
)
1652 src
.ipv4
= src1
->ipv4
;
1653 else if (family
== AF_INET6
)
1654 src
.ipv6
= src1
->ipv6
;
1660 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1661 if (family
== AF_INET
)
1662 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1663 &src
.ipv4
, bytelen
);
1664 else if (family
== AF_INET6
)
1665 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1666 &src
.ipv6
, bytelen
);
1667 if (IS_ZEBRA_DEBUG_KERNEL
)
1668 zlog_debug("Setting source");
1671 if (rta
->rta_len
> RTA_LENGTH(0))
1672 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1673 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1676 /* If there is no useful nexthop then return. */
1677 if (nexthop_num
== 0) {
1678 if (IS_ZEBRA_DEBUG_KERNEL
)
1680 "netlink_route_multipath(): No useful nexthop.");
1686 /* Destination netlink address. */
1687 memset(&snl
, 0, sizeof snl
);
1688 snl
.nl_family
= AF_NETLINK
;
1690 /* Talk to netlink socket. */
1691 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1695 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1698 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1706 struct zebra_ns
*zns
;
1709 memset(&req
, 0, sizeof(req
));
1711 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1712 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1713 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1715 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1716 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1718 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1719 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1720 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1721 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1722 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1724 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1725 &zns
->netlink_cmd
, zns
, 0);
1731 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1732 const struct prefix
*p
,
1733 const struct prefix
*src_p
,
1734 struct route_entry
*old
,
1735 struct route_entry
*new)
1742 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1743 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1744 new, (old
) ? 1 : 0);
1747 * So v6 route replace semantics are not in
1748 * the kernel at this point as I understand it.
1749 * So let's do a delete than an add.
1750 * In the future once v6 route replace semantics
1751 * are in we can figure out what to do here to
1752 * allow working with old and new kernels.
1754 * I'm also intentionally ignoring the failure case
1755 * of the route delete. If that happens yeah we're
1759 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1761 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1764 kernel_route_rib_pass_fail(rn
, p
, new,
1765 (!ret
) ? DP_INSTALL_SUCCESS
1766 : DP_INSTALL_FAILURE
);
1767 return DP_REQUEST_SUCCESS
;
1771 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1773 kernel_route_rib_pass_fail(rn
, p
, old
,
1774 (!ret
) ? DP_DELETE_SUCCESS
1775 : DP_DELETE_FAILURE
);
1778 return DP_REQUEST_SUCCESS
;
1781 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1782 int llalen
, ns_id_t ns_id
)
1784 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1785 addr
, lla
, llalen
, ns_id
);
1789 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1790 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1792 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1793 struct in_addr
*vtep_ip
, int cmd
)
1795 struct zebra_ns
*zns
;
1801 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1802 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1805 memset(&req
, 0, sizeof(req
));
1807 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1808 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1809 if (cmd
== RTM_NEWNEIGH
)
1810 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1811 req
.n
.nlmsg_type
= cmd
;
1812 req
.ndm
.ndm_family
= PF_BRIDGE
;
1813 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1814 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1817 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1818 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1819 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1821 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1826 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1828 * a "flood" MAC FDB entry.
1830 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1832 if (IS_ZEBRA_DEBUG_VXLAN
)
1833 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1834 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1836 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1840 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1841 * deleting the "flood" MAC FDB entry.
1843 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1845 if (IS_ZEBRA_DEBUG_VXLAN
)
1847 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1848 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1850 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1854 #define NDA_RTA(r) \
1855 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1858 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1861 struct interface
*ifp
;
1862 struct zebra_if
*zif
;
1863 struct rtattr
*tb
[NDA_MAX
+ 1];
1864 struct interface
*br_if
;
1867 struct prefix vtep_ip
;
1868 int vid_present
= 0, dst_present
= 0;
1869 char buf
[ETHER_ADDR_STRLEN
];
1874 ndm
= NLMSG_DATA(h
);
1876 /* We only process macfdb notifications if EVPN is enabled */
1877 if (!is_evpn_enabled())
1880 /* The interface should exist. */
1881 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1883 if (!ifp
|| !ifp
->info
)
1886 /* The interface should be something we're interested in. */
1887 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1890 /* Drop "permanent" entries. */
1891 if (ndm
->ndm_state
& NUD_PERMANENT
)
1894 zif
= (struct zebra_if
*)ifp
->info
;
1895 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1896 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1897 nl_msg_type_to_str(h
->nlmsg_type
),
1898 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1899 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1903 /* Parse attributes and extract fields of interest. */
1904 memset(tb
, 0, sizeof tb
);
1905 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1907 if (!tb
[NDA_LLADDR
]) {
1908 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1909 nl_msg_type_to_str(h
->nlmsg_type
),
1910 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1911 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1915 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1917 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1918 nl_msg_type_to_str(h
->nlmsg_type
),
1919 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1920 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1921 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1925 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1927 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1929 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1930 sprintf(vid_buf
, " VLAN %u", vid
);
1934 /* TODO: Only IPv4 supported now. */
1936 vtep_ip
.family
= AF_INET
;
1937 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1938 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1940 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1943 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1945 if (IS_ZEBRA_DEBUG_KERNEL
)
1946 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1947 nl_msg_type_to_str(h
->nlmsg_type
),
1948 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1949 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1950 sticky
? "sticky " : "",
1951 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1952 dst_present
? dst_buf
: "");
1954 if (filter_vlan
&& vid
!= filter_vlan
)
1957 /* If add or update, do accordingly if learnt on a "local" interface; if
1958 * the notification is over VxLAN, this has to be related to
1960 * so perform an implicit delete of any local entry (if it exists).
1962 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1963 /* Drop "permanent" entries. */
1964 if (ndm
->ndm_state
& NUD_PERMANENT
)
1967 if (IS_ZEBRA_IF_VXLAN(ifp
))
1968 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1971 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1975 /* This is a delete notification.
1976 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1977 * 2. For a MAC over "local" interface, delete the mac
1978 * Note: We will get notifications from both bridge driver and VxLAN
1980 * Ignore the notification from VxLan driver as it is also generated
1981 * when mac moves from remote to local.
1986 if (IS_ZEBRA_IF_VXLAN(ifp
))
1987 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1990 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1993 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1998 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2001 /* Length validity. */
2002 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2006 /* We are interested only in AF_BRIDGE notifications. */
2007 ndm
= NLMSG_DATA(h
);
2008 if (ndm
->ndm_family
!= AF_BRIDGE
)
2011 return netlink_macfdb_change(h
, len
, ns_id
);
2014 /* Request for MAC FDB information from the kernel */
2015 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2016 ifindex_t master_ifindex
)
2020 struct ifinfomsg ifm
;
2024 /* Form the request, specifying filter (rtattr) if needed. */
2025 memset(&req
, 0, sizeof(req
));
2026 req
.n
.nlmsg_type
= type
;
2027 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2028 req
.ifm
.ifi_family
= family
;
2030 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2032 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2036 * MAC forwarding database read using netlink interface. This is invoked
2039 int netlink_macfdb_read(struct zebra_ns
*zns
)
2043 /* Get bridge FDB table. */
2044 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2047 /* We are reading entire table. */
2049 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2056 * MAC forwarding database read using netlink interface. This is for a
2057 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2059 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2060 struct interface
*br_if
)
2062 struct zebra_if
*br_zif
;
2063 struct zebra_if
*zif
;
2064 struct zebra_l2info_vxlan
*vxl
;
2068 /* Save VLAN we're filtering on, if needed. */
2069 br_zif
= (struct zebra_if
*)br_if
->info
;
2070 zif
= (struct zebra_if
*)ifp
->info
;
2071 vxl
= &zif
->l2info
.vxl
;
2072 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2073 filter_vlan
= vxl
->access_vlan
;
2075 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2077 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2081 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2084 /* Reset VLAN filter. */
2089 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2090 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2091 int local
, int cmd
, uint8_t sticky
)
2093 struct zebra_ns
*zns
;
2100 struct zebra_if
*zif
;
2101 struct interface
*br_if
;
2102 struct zebra_if
*br_zif
;
2103 char buf
[ETHER_ADDR_STRLEN
];
2104 int vid_present
= 0, dst_present
= 0;
2107 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2111 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2112 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2113 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2118 memset(&req
, 0, sizeof(req
));
2120 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2121 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2122 if (cmd
== RTM_NEWNEIGH
)
2123 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2124 req
.n
.nlmsg_type
= cmd
;
2125 req
.ndm
.ndm_family
= AF_BRIDGE
;
2126 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2127 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2130 req
.ndm
.ndm_state
|= NUD_NOARP
;
2132 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2134 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2135 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2137 dst_alen
= 4; // TODO: hardcoded
2138 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2140 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2142 br_zif
= (struct zebra_if
*)br_if
->info
;
2143 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2144 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2146 sprintf(vid_buf
, " VLAN %u", vid
);
2148 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2150 if (IS_ZEBRA_DEBUG_KERNEL
)
2151 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2152 nl_msg_type_to_str(cmd
),
2153 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2154 ifp
->ifindex
, vid_present
? vid_buf
: "",
2155 sticky
? "sticky " : "",
2156 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2157 dst_present
? dst_buf
: "");
2159 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2164 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2167 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2170 struct interface
*ifp
;
2171 struct zebra_if
*zif
;
2172 struct rtattr
*tb
[NDA_MAX
+ 1];
2173 struct interface
*link_if
;
2176 char buf
[ETHER_ADDR_STRLEN
];
2177 char buf2
[INET6_ADDRSTRLEN
];
2178 int mac_present
= 0;
2179 uint8_t ext_learned
;
2180 uint8_t router_flag
;
2182 ndm
= NLMSG_DATA(h
);
2184 /* The interface should exist. */
2185 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2187 if (!ifp
|| !ifp
->info
)
2190 zif
= (struct zebra_if
*)ifp
->info
;
2192 /* Parse attributes and extract fields of interest. */
2193 memset(tb
, 0, sizeof tb
);
2194 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2197 zlog_warn("%s family %s IF %s(%u) - no DST",
2198 nl_msg_type_to_str(h
->nlmsg_type
),
2199 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2204 memset(&ip
, 0, sizeof(struct ipaddr
));
2205 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2206 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2208 /* Drop some "permanent" entries. */
2209 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2210 char buf
[16] = "169.254.0.1";
2211 struct in_addr ipv4_ll
;
2213 if (ndm
->ndm_family
!= AF_INET
)
2216 if (!zif
->v6_2_v4_ll_neigh_entry
)
2219 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2222 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2223 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2226 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2227 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2231 /* The neighbor is present on an SVI. From this, we locate the
2233 * bridge because we're only interested in neighbors on a VxLAN bridge.
2234 * The bridge is located based on the nature of the SVI:
2235 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2237 * and is linked to the bridge
2238 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2242 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2243 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2247 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2252 memset(&mac
, 0, sizeof(struct ethaddr
));
2253 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2254 if (tb
[NDA_LLADDR
]) {
2255 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2257 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2258 nl_msg_type_to_str(h
->nlmsg_type
),
2259 nl_family_to_str(ndm
->ndm_family
),
2260 ifp
->name
, ndm
->ndm_ifindex
,
2261 (unsigned long)RTA_PAYLOAD(
2267 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2270 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2271 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2273 if (IS_ZEBRA_DEBUG_KERNEL
)
2275 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2276 nl_msg_type_to_str(h
->nlmsg_type
),
2277 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2279 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2281 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2283 ndm
->ndm_state
, ndm
->ndm_flags
);
2285 /* If the neighbor state is valid for use, process as an add or
2287 * else process as a delete. Note that the delete handling may
2289 * in re-adding the neighbor if it is a valid "remote" neighbor.
2291 if (ndm
->ndm_state
& NUD_VALID
)
2292 return zebra_vxlan_handle_kernel_neigh_update(
2293 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2294 ext_learned
, router_flag
);
2296 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2299 if (IS_ZEBRA_DEBUG_KERNEL
)
2300 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2301 nl_msg_type_to_str(h
->nlmsg_type
),
2302 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2304 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2306 /* Process the delete - it may result in re-adding the neighbor if it is
2307 * a valid "remote" neighbor.
2309 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2312 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2317 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2320 /* Length validity. */
2321 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2325 /* We are interested only in AF_INET or AF_INET6 notifications. */
2326 ndm
= NLMSG_DATA(h
);
2327 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2330 return netlink_neigh_change(h
, len
);
2333 /* Request for IP neighbor information from the kernel */
2334 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2343 /* Form the request, specifying filter (rtattr) if needed. */
2344 memset(&req
, 0, sizeof(req
));
2345 req
.n
.nlmsg_type
= type
;
2346 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2347 req
.ndm
.ndm_family
= family
;
2349 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2351 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2355 * IP Neighbor table read using netlink interface. This is invoked
2358 int netlink_neigh_read(struct zebra_ns
*zns
)
2362 /* Get IP neighbor table. */
2363 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2366 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2373 * IP Neighbor table read using netlink interface. This is for a specific
2376 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2380 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2384 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2390 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2395 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2398 /* Length validity. */
2399 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2401 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2402 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2403 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2407 /* Is this a notification for the MAC FDB or IP neighbor table? */
2408 ndm
= NLMSG_DATA(h
);
2409 if (ndm
->ndm_family
== AF_BRIDGE
)
2410 return netlink_macfdb_change(h
, len
, ns_id
);
2412 if (ndm
->ndm_type
!= RTN_UNICAST
)
2415 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2416 return netlink_ipneigh_change(h
, len
, ns_id
);
2419 "Invalid address family: %u received from kernel neighbor change: %u",
2420 ndm
->ndm_family
, h
->nlmsg_type
);
2427 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2428 struct ethaddr
*mac
, uint8_t flags
,
2429 uint16_t state
, int cmd
)
2438 struct zebra_ns
*zns
;
2439 char buf
[INET6_ADDRSTRLEN
];
2440 char buf2
[ETHER_ADDR_STRLEN
];
2441 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2444 memset(&req
, 0, sizeof(req
));
2446 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2447 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2448 if (cmd
== RTM_NEWNEIGH
)
2449 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2450 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2451 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2452 req
.ndm
.ndm_state
= state
;
2453 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2454 req
.ndm
.ndm_type
= RTN_UNICAST
;
2455 req
.ndm
.ndm_flags
= flags
;
2457 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2458 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2460 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2462 if (IS_ZEBRA_DEBUG_KERNEL
)
2463 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2464 nl_msg_type_to_str(cmd
),
2465 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2466 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2467 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2470 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2474 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2475 struct in_addr vtep_ip
, uint8_t sticky
)
2477 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2481 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2482 struct in_addr vtep_ip
, int local
)
2484 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2488 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2489 struct ethaddr
*mac
, uint8_t flags
)
2491 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2492 NUD_NOARP
, RTM_NEWNEIGH
);
2495 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2497 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2501 * MPLS label forwarding table change via netlink interface.
2503 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2506 zebra_nhlfe_t
*nhlfe
;
2507 struct nexthop
*nexthop
= NULL
;
2508 unsigned int nexthop_num
;
2509 const char *routedesc
;
2510 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2516 char buf
[NL_PKT_BUF_SIZE
];
2519 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2522 * Count # nexthops so we can decide whether to use singlepath
2523 * or multipath case.
2526 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2527 nexthop
= nhlfe
->nexthop
;
2530 if (cmd
== RTM_NEWROUTE
) {
2531 /* Count all selected NHLFEs */
2532 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2533 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2537 /* Count all installed NHLFEs */
2538 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2539 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2544 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2547 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2548 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2549 req
.n
.nlmsg_type
= cmd
;
2550 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2552 req
.r
.rtm_family
= AF_MPLS
;
2553 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2554 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2555 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2556 req
.r
.rtm_type
= RTN_UNICAST
;
2558 if (cmd
== RTM_NEWROUTE
) {
2559 /* We do a replace to handle update. */
2560 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2562 /* set the protocol value if installing */
2563 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2564 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2567 /* Fill destination */
2568 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2569 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2571 /* Fill nexthops (paths) based on single-path or multipath. The paths
2572 * chosen depend on the operation.
2574 if (nexthop_num
== 1 || multipath_num
== 1) {
2575 routedesc
= "single-path";
2576 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2579 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2580 nexthop
= nhlfe
->nexthop
;
2584 if ((cmd
== RTM_NEWROUTE
2585 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2586 && CHECK_FLAG(nexthop
->flags
,
2587 NEXTHOP_FLAG_ACTIVE
)))
2588 || (cmd
== RTM_DELROUTE
2589 && (CHECK_FLAG(nhlfe
->flags
,
2590 NHLFE_FLAG_INSTALLED
)
2591 && CHECK_FLAG(nexthop
->flags
,
2592 NEXTHOP_FLAG_FIB
)))) {
2593 /* Add the gateway */
2594 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2601 } else /* Multipath case */
2603 char buf
[NL_PKT_BUF_SIZE
];
2604 struct rtattr
*rta
= (void *)buf
;
2605 struct rtnexthop
*rtnh
;
2606 union g_addr
*src1
= NULL
;
2608 rta
->rta_type
= RTA_MULTIPATH
;
2609 rta
->rta_len
= RTA_LENGTH(0);
2610 rtnh
= RTA_DATA(rta
);
2612 routedesc
= "multipath";
2613 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2616 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2617 nexthop
= nhlfe
->nexthop
;
2621 if (nexthop_num
>= multipath_num
)
2624 if ((cmd
== RTM_NEWROUTE
2625 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2626 && CHECK_FLAG(nexthop
->flags
,
2627 NEXTHOP_FLAG_ACTIVE
)))
2628 || (cmd
== RTM_DELROUTE
2629 && (CHECK_FLAG(nhlfe
->flags
,
2630 NHLFE_FLAG_INSTALLED
)
2631 && CHECK_FLAG(nexthop
->flags
,
2632 NEXTHOP_FLAG_FIB
)))) {
2635 /* Build the multipath */
2636 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2639 rtnh
= RTNH_NEXT(rtnh
);
2643 /* Add the multipath */
2644 if (rta
->rta_len
> RTA_LENGTH(0))
2645 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2646 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2649 /* Talk to netlink socket. */
2650 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2653 #endif /* HAVE_NETLINK */