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/zserv.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_STATIC
) || (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
)) {
108 static inline int zebra2proto(int proto
)
111 case ZEBRA_ROUTE_BABEL
:
112 proto
= RTPROT_BABEL
;
114 case ZEBRA_ROUTE_BGP
:
117 case ZEBRA_ROUTE_OSPF
:
118 case ZEBRA_ROUTE_OSPF6
:
121 case ZEBRA_ROUTE_STATIC
:
122 proto
= RTPROT_STATIC
;
124 case ZEBRA_ROUTE_ISIS
:
127 case ZEBRA_ROUTE_RIP
:
130 case ZEBRA_ROUTE_RIPNG
:
131 proto
= RTPROT_RIPNG
;
133 case ZEBRA_ROUTE_NHRP
:
136 case ZEBRA_ROUTE_EIGRP
:
137 proto
= RTPROT_EIGRP
;
139 case ZEBRA_ROUTE_LDP
:
142 case ZEBRA_ROUTE_SHARP
:
143 proto
= RTPROT_SHARP
;
146 proto
= RTPROT_ZEBRA
;
153 static inline int proto2zebra(int proto
, int family
)
157 proto
= ZEBRA_ROUTE_BABEL
;
160 proto
= ZEBRA_ROUTE_BGP
;
163 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
167 proto
= ZEBRA_ROUTE_ISIS
;
170 proto
= ZEBRA_ROUTE_RIP
;
173 proto
= ZEBRA_ROUTE_RIPNG
;
176 proto
= ZEBRA_ROUTE_NHRP
;
179 proto
= ZEBRA_ROUTE_EIGRP
;
182 proto
= ZEBRA_ROUTE_LDP
;
185 proto
= ZEBRA_ROUTE_STATIC
;
188 proto
= ZEBRA_ROUTE_KERNEL
;
195 Pending: create an efficient table_id (in a tree/hash) based lookup)
197 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
200 struct zebra_vrf
*zvrf
;
202 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
206 /* case vrf with netns : match the netnsid */
207 if (vrf_is_backend_netns()) {
208 if (ns_id
== zvrf_id(zvrf
))
209 return zvrf_id(zvrf
);
211 /* VRF is VRF_BACKEND_VRF_LITE */
212 if (zvrf
->table_id
!= table_id
)
214 return zvrf_id(zvrf
);
221 /* Looking up routing table by netlink interface. */
222 static int netlink_route_change_read_unicast(struct sockaddr_nl
*snl
,
223 struct nlmsghdr
*h
, ns_id_t ns_id
,
228 struct rtattr
*tb
[RTA_MAX
+ 1];
231 struct prefix_ipv6 src_p
= {};
234 char anyaddr
[16] = {0};
236 int proto
= ZEBRA_ROUTE_KERNEL
;
241 uint8_t distance
= 0;
246 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
247 void *src
= NULL
; /* IPv6 srcdest source prefix */
248 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
252 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
254 switch (rtm
->rtm_type
) {
258 bh_type
= BLACKHOLE_NULL
;
260 case RTN_UNREACHABLE
:
261 bh_type
= BLACKHOLE_REJECT
;
264 bh_type
= BLACKHOLE_ADMINPROHIB
;
270 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
274 memset(tb
, 0, sizeof tb
);
275 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
277 if (rtm
->rtm_flags
& RTM_F_CLONED
)
279 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
281 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
284 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
285 && h
->nlmsg_type
== RTM_NEWROUTE
)
288 /* We don't care about change notifications for the MPLS table. */
289 /* TODO: Revisit this. */
290 if (rtm
->rtm_family
== AF_MPLS
)
293 /* Table corresponding to route. */
295 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
297 table
= rtm
->rtm_table
;
300 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
301 if (vrf_id
== VRF_DEFAULT
) {
302 if (!is_zebra_valid_kernel_table(table
)
303 && !is_zebra_main_routing_table(table
))
307 /* Route which inserted by Zebra. */
308 if (is_selfroute(rtm
->rtm_protocol
)) {
309 flags
|= ZEBRA_FLAG_SELFROUTE
;
310 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
313 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
316 dest
= RTA_DATA(tb
[RTA_DST
]);
321 src
= RTA_DATA(tb
[RTA_SRC
]);
326 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
329 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
331 if (tb
[RTA_PRIORITY
])
332 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
334 #if defined(SUPPORT_REALMS)
336 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
339 if (tb
[RTA_METRICS
]) {
340 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
342 memset(mxrta
, 0, sizeof mxrta
);
343 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
344 RTA_PAYLOAD(tb
[RTA_METRICS
]));
347 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
350 if (rtm
->rtm_family
== AF_INET
) {
352 memcpy(&p
.u
.prefix4
, dest
, 4);
353 p
.prefixlen
= rtm
->rtm_dst_len
;
356 0; // Forces debug below to not display anything
357 } else if (rtm
->rtm_family
== AF_INET6
) {
359 memcpy(&p
.u
.prefix6
, dest
, 16);
360 p
.prefixlen
= rtm
->rtm_dst_len
;
362 src_p
.family
= AF_INET6
;
363 memcpy(&src_p
.prefix
, src
, 16);
364 src_p
.prefixlen
= rtm
->rtm_src_len
;
367 if (rtm
->rtm_src_len
!= 0) {
368 char buf
[PREFIX_STRLEN
];
370 "unsupported IPv[4|6] sourcedest route (dest %s vrf %u)",
371 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
376 * For ZEBRA_ROUTE_KERNEL types:
378 * The metric/priority of the route received from the kernel
379 * is a 32 bit number. We are going to interpret the high
380 * order byte as the Admin Distance and the low order 3 bytes
383 * This will allow us to do two things:
384 * 1) Allow the creation of kernel routes that can be
385 * overridden by zebra.
386 * 2) Allow the old behavior for 'most' kernel route types
387 * if a user enters 'ip route ...' v4 routes get a metric
388 * of 0 and v6 routes get a metric of 1024. Both of these
389 * values will end up with a admin distance of 0, which
390 * will cause them to win for the purposes of zebra.
392 if (proto
== ZEBRA_ROUTE_KERNEL
) {
393 distance
= (metric
>> 24) & 0xFF;
394 metric
= (metric
& 0x00FFFFFF);
397 if (IS_ZEBRA_DEBUG_KERNEL
) {
398 char buf
[PREFIX_STRLEN
];
399 char buf2
[PREFIX_STRLEN
];
400 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
401 nl_msg_type_to_str(h
->nlmsg_type
),
402 prefix2str(&p
, buf
, sizeof(buf
)),
403 src_p
.prefixlen
? " from " : "",
405 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
407 vrf_id
, table
, metric
, distance
);
411 if (rtm
->rtm_family
== AF_INET6
)
414 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
415 struct interface
*ifp
;
416 vrf_id_t nh_vrf_id
= vrf_id
;
418 if (!tb
[RTA_MULTIPATH
]) {
420 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
422 memset(&nh
, 0, sizeof(nh
));
424 if (bh_type
== BLACKHOLE_UNSPEC
) {
426 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
427 else if (index
&& gate
)
430 ? NEXTHOP_TYPE_IPV4_IFINDEX
431 : NEXTHOP_TYPE_IPV6_IFINDEX
;
432 else if (!index
&& gate
)
433 nh
.type
= (afi
== AFI_IP
)
437 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
438 nh
.bh_type
= bh_type
;
441 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
442 nh
.bh_type
= bh_type
;
446 memcpy(&nh
.src
, prefsrc
, sz
);
448 memcpy(&nh
.gate
, gate
, sz
);
451 ifp
= if_lookup_by_index(index
, VRF_UNKNOWN
);
453 nh_vrf_id
= ifp
->vrf_id
;
455 nh
.vrf_id
= nh_vrf_id
;
457 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
458 NULL
, &nh
, table
, metric
, mtu
, distance
, tag
);
460 /* This is a multipath route */
462 struct route_entry
*re
;
463 struct rtnexthop
*rtnh
=
464 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
466 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
468 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
470 re
->distance
= distance
;
477 re
->uptime
= time(NULL
);
482 if (len
< (int)sizeof(*rtnh
)
483 || rtnh
->rtnh_len
> len
)
486 index
= rtnh
->rtnh_ifindex
;
489 * Yes we are looking this up
490 * for every nexthop and just
491 * using the last one looked
494 ifp
= if_lookup_by_index(index
,
497 nh_vrf_id
= ifp
->vrf_id
;
500 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
503 nh_vrf_id
= VRF_DEFAULT
;
509 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
510 memset(tb
, 0, sizeof(tb
));
511 netlink_parse_rtattr(
512 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
513 rtnh
->rtnh_len
- sizeof(*rtnh
));
520 if (rtm
->rtm_family
== AF_INET
) {
522 route_entry_nexthop_ipv4_ifindex_add(
527 route_entry_nexthop_ipv4_add(
531 } else if (rtm
->rtm_family
534 route_entry_nexthop_ipv6_ifindex_add(
538 route_entry_nexthop_ipv6_add(
543 route_entry_nexthop_ifindex_add(
544 re
, index
, nh_vrf_id
);
546 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
547 rtnh
= RTNH_NEXT(rtnh
);
550 zserv_nexthop_num_warn(__func__
,
551 (const struct prefix
*)&p
,
553 if (re
->nexthop_num
== 0)
556 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
,
560 if (!tb
[RTA_MULTIPATH
]) {
562 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
564 memset(&nh
, 0, sizeof(nh
));
565 if (bh_type
== BLACKHOLE_UNSPEC
) {
567 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
568 else if (index
&& gate
)
571 ? NEXTHOP_TYPE_IPV4_IFINDEX
572 : NEXTHOP_TYPE_IPV6_IFINDEX
;
573 else if (!index
&& gate
)
574 nh
.type
= (afi
== AFI_IP
)
578 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
579 nh
.bh_type
= BLACKHOLE_UNSPEC
;
582 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
583 nh
.bh_type
= bh_type
;
587 memcpy(&nh
.gate
, gate
, sz
);
588 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
589 &p
, NULL
, &nh
, table
, metric
, true, NULL
);
591 /* XXX: need to compare the entire list of nexthops
592 * here for NLM_F_APPEND stupidity */
593 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
594 &p
, NULL
, NULL
, table
, metric
, true, NULL
);
601 static struct mcast_route_data
*mroute
= NULL
;
603 static int netlink_route_change_read_multicast(struct sockaddr_nl
*snl
,
605 ns_id_t ns_id
, int startup
)
609 struct rtattr
*tb
[RTA_MAX
+ 1];
610 struct mcast_route_data
*m
;
611 struct mcast_route_data mr
;
618 char oif_list
[256] = "\0";
625 memset(&mr
, 0, sizeof(mr
));
631 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
633 memset(tb
, 0, sizeof tb
);
634 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
637 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
639 table
= rtm
->rtm_table
;
641 vrf
= vrf_lookup_by_table(table
, ns_id
);
644 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
647 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
650 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
652 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
653 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
655 if (tb
[RTA_MULTIPATH
]) {
656 struct rtnexthop
*rtnh
=
657 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
659 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
661 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
664 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
667 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
668 rtnh
= RTNH_NEXT(rtnh
);
672 if (IS_ZEBRA_DEBUG_KERNEL
) {
673 struct interface
*ifp
;
674 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
675 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
676 for (count
= 0; count
< oif_count
; count
++) {
677 ifp
= if_lookup_by_index(oif
[count
], vrf
);
680 sprintf(temp
, "%s ", ifp
->name
);
681 strcat(oif_list
, temp
);
683 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
684 ifp
= if_lookup_by_index(iif
, vrf
);
686 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
687 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
688 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
693 int netlink_route_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
694 ns_id_t ns_id
, int startup
)
701 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
702 /* If this is not route add/delete message print warning. */
703 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
707 /* Connected route. */
708 if (IS_ZEBRA_DEBUG_KERNEL
)
709 zlog_debug("%s %s %s proto %s NS %u",
710 nl_msg_type_to_str(h
->nlmsg_type
),
711 nl_family_to_str(rtm
->rtm_family
),
712 nl_rttype_to_str(rtm
->rtm_type
),
713 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
715 /* We don't care about change notifications for the MPLS table. */
716 /* TODO: Revisit this. */
717 if (rtm
->rtm_family
== AF_MPLS
)
720 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
724 if (rtm
->rtm_type
== RTN_MULTICAST
)
725 netlink_route_change_read_multicast(snl
, h
, ns_id
, startup
);
727 netlink_route_change_read_unicast(snl
, h
, ns_id
, startup
);
731 /* Request for specific route information from the kernel */
732 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
739 /* Form the request, specifying filter (rtattr) if needed. */
740 memset(&req
, 0, sizeof(req
));
741 req
.n
.nlmsg_type
= type
;
742 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
743 req
.rtm
.rtm_family
= family
;
745 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
748 /* Routing table read function using netlink interface. Only called
750 int netlink_route_read(struct zebra_ns
*zns
)
754 /* Get IPv4 routing table. */
755 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
758 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
759 &zns
->netlink_cmd
, zns
, 0, 1);
763 /* Get IPv6 routing table. */
764 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
767 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
768 &zns
->netlink_cmd
, zns
, 0, 1);
775 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
777 struct nlmsghdr
*nlmsg
,
778 size_t req_size
, int bytelen
,
779 struct nexthop
*nexthop
)
781 if (route_family
== AF_MPLS
) {
782 struct gw_family_t gw_fam
;
784 gw_fam
.family
= gw_family
;
785 if (gw_family
== AF_INET
)
786 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
788 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
789 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
792 if (gw_family
== AF_INET
)
793 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
794 &nexthop
->gate
.ipv4
, bytelen
);
796 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
797 &nexthop
->gate
.ipv6
, bytelen
);
801 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
804 struct rtnexthop
*rtnh
,
805 size_t req_size
, int bytelen
,
806 struct nexthop
*nexthop
)
808 if (route_family
== AF_MPLS
) {
809 struct gw_family_t gw_fam
;
811 gw_fam
.family
= gw_family
;
812 if (gw_family
== AF_INET
)
813 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
815 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
816 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
818 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
820 if (gw_family
== AF_INET
)
821 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
822 &nexthop
->gate
.ipv4
, bytelen
);
824 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
825 &nexthop
->gate
.ipv6
, bytelen
);
826 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
830 /* This function takes a nexthop as argument and adds
831 * the appropriate netlink attributes to an existing
834 * @param routedesc: Human readable description of route type
835 * (direct/recursive, single-/multipath)
836 * @param bytelen: Length of addresses in bytes.
837 * @param nexthop: Nexthop information
838 * @param nlmsg: nlmsghdr structure to fill in.
839 * @param req_size: The size allocated for the message.
841 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
842 struct nexthop
*nexthop
,
843 struct nlmsghdr
*nlmsg
,
845 size_t req_size
, int cmd
)
847 struct mpls_label_stack
*nh_label
;
848 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
853 * label_buf is *only* currently used within debugging.
854 * As such when we assign it we are guarding it inside
855 * a debug test. If you want to change this make sure
856 * you fix this assumption
861 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
864 nh_label
= nh
->nh_label
;
865 if (!nh_label
|| !nh_label
->num_labels
)
868 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
869 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
872 if (IS_ZEBRA_DEBUG_KERNEL
) {
874 sprintf(label_buf
, "label %u",
877 sprintf(label_buf1
, "/%u",
879 strlcat(label_buf
, label_buf1
,
884 out_lse
[num_labels
] =
885 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
891 /* Set the BoS bit */
892 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
894 if (rtmsg
->rtm_family
== AF_MPLS
)
895 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
896 num_labels
* sizeof(mpls_lse_t
));
899 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
901 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
903 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
904 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
905 num_labels
* sizeof(mpls_lse_t
));
906 addattr_nest_end(nlmsg
, nest
);
910 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
911 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
913 if (rtmsg
->rtm_family
== AF_INET
914 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
915 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
916 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
917 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
918 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
920 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
921 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
922 &nexthop
->rmap_src
.ipv4
, bytelen
);
923 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
924 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
925 &nexthop
->src
.ipv4
, bytelen
);
927 if (IS_ZEBRA_DEBUG_KERNEL
)
929 " 5549: _netlink_route_build_singlepath() (%s): "
930 "nexthop via %s %s if %u(%u)",
931 routedesc
, ipv4_ll_buf
, label_buf
,
932 nexthop
->ifindex
, nexthop
->vrf_id
);
936 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
937 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
938 /* Send deletes to the kernel without specifying the next-hop */
939 if (cmd
!= RTM_DELROUTE
)
940 _netlink_route_nl_add_gateway_info(
941 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
944 if (cmd
== RTM_NEWROUTE
) {
945 if (nexthop
->rmap_src
.ipv4
.s_addr
)
946 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
947 &nexthop
->rmap_src
.ipv4
, bytelen
);
948 else if (nexthop
->src
.ipv4
.s_addr
)
949 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
950 &nexthop
->src
.ipv4
, bytelen
);
953 if (IS_ZEBRA_DEBUG_KERNEL
)
955 "netlink_route_multipath() (%s): "
956 "nexthop via %s %s if %u(%u)",
957 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
958 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
961 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
962 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
963 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
964 nlmsg
, req_size
, bytelen
,
967 if (cmd
== RTM_NEWROUTE
) {
968 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
969 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
970 &nexthop
->rmap_src
.ipv6
, bytelen
);
971 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
972 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
973 &nexthop
->src
.ipv6
, bytelen
);
976 if (IS_ZEBRA_DEBUG_KERNEL
)
978 "netlink_route_multipath() (%s): "
979 "nexthop via %s %s if %u(%u)",
980 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
981 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
985 * We have the ifindex so we should always send it
986 * This is especially useful if we are doing route
989 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
990 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
992 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
993 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
994 if (cmd
== RTM_NEWROUTE
) {
995 if (nexthop
->rmap_src
.ipv4
.s_addr
)
996 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
997 &nexthop
->rmap_src
.ipv4
, bytelen
);
998 else if (nexthop
->src
.ipv4
.s_addr
)
999 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1000 &nexthop
->src
.ipv4
, bytelen
);
1003 if (IS_ZEBRA_DEBUG_KERNEL
)
1005 "netlink_route_multipath() (%s): "
1006 "nexthop via if %u(%u)",
1007 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1010 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1011 if (cmd
== RTM_NEWROUTE
) {
1012 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1013 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1014 &nexthop
->rmap_src
.ipv6
, bytelen
);
1015 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1016 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1017 &nexthop
->src
.ipv6
, bytelen
);
1020 if (IS_ZEBRA_DEBUG_KERNEL
)
1022 "netlink_route_multipath() (%s): "
1023 "nexthop via if %u(%u)",
1024 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1028 /* This function takes a nexthop as argument and
1029 * appends to the given rtattr/rtnexthop pair the
1030 * representation of the nexthop. If the nexthop
1031 * defines a preferred source, the src parameter
1032 * will be modified to point to that src, otherwise
1033 * it will be kept unmodified.
1035 * @param routedesc: Human readable description of route type
1036 * (direct/recursive, single-/multipath)
1037 * @param bytelen: Length of addresses in bytes.
1038 * @param nexthop: Nexthop information
1039 * @param rta: rtnetlink attribute structure
1040 * @param rtnh: pointer to an rtnetlink nexthop structure
1041 * @param src: pointer pointing to a location where
1042 * the prefsrc should be stored.
1044 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1045 struct nexthop
*nexthop
,
1047 struct rtnexthop
*rtnh
,
1048 struct rtmsg
*rtmsg
,
1051 struct mpls_label_stack
*nh_label
;
1052 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1054 char label_buf
[256];
1056 rtnh
->rtnh_len
= sizeof(*rtnh
);
1057 rtnh
->rtnh_flags
= 0;
1058 rtnh
->rtnh_hops
= 0;
1059 rta
->rta_len
+= rtnh
->rtnh_len
;
1062 * label_buf is *only* currently used within debugging.
1063 * As such when we assign it we are guarding it inside
1064 * a debug test. If you want to change this make sure
1065 * you fix this assumption
1067 label_buf
[0] = '\0';
1070 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1071 char label_buf1
[20];
1073 nh_label
= nh
->nh_label
;
1074 if (!nh_label
|| !nh_label
->num_labels
)
1077 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1078 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1081 if (IS_ZEBRA_DEBUG_KERNEL
) {
1083 sprintf(label_buf
, "label %u",
1084 nh_label
->label
[i
]);
1086 sprintf(label_buf1
, "/%u",
1087 nh_label
->label
[i
]);
1088 strlcat(label_buf
, label_buf1
,
1093 out_lse
[num_labels
] =
1094 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1100 /* Set the BoS bit */
1101 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1103 if (rtmsg
->rtm_family
== AF_MPLS
) {
1104 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1106 num_labels
* sizeof(mpls_lse_t
));
1108 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1110 struct rtattr
*nest
;
1111 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1112 int len
= rta
->rta_len
;
1114 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1115 &encap
, sizeof(uint16_t));
1116 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1117 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1119 num_labels
* sizeof(mpls_lse_t
));
1120 rta_nest_end(rta
, nest
);
1121 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1125 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1126 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1128 if (rtmsg
->rtm_family
== AF_INET
1129 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1130 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1132 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1133 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1135 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1136 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1138 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1139 *src
= &nexthop
->rmap_src
;
1140 else if (nexthop
->src
.ipv4
.s_addr
)
1141 *src
= &nexthop
->src
;
1143 if (IS_ZEBRA_DEBUG_KERNEL
)
1145 " 5549: netlink_route_build_multipath() (%s): "
1146 "nexthop via %s %s if %u",
1147 routedesc
, ipv4_ll_buf
, label_buf
,
1152 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1153 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1154 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1155 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1157 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1158 *src
= &nexthop
->rmap_src
;
1159 else if (nexthop
->src
.ipv4
.s_addr
)
1160 *src
= &nexthop
->src
;
1162 if (IS_ZEBRA_DEBUG_KERNEL
)
1164 "netlink_route_multipath() (%s): "
1165 "nexthop via %s %s if %u",
1166 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1167 label_buf
, nexthop
->ifindex
);
1169 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1170 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1171 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1172 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1175 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1176 *src
= &nexthop
->rmap_src
;
1177 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1178 *src
= &nexthop
->src
;
1180 if (IS_ZEBRA_DEBUG_KERNEL
)
1182 "netlink_route_multipath() (%s): "
1183 "nexthop via %s %s if %u",
1184 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1185 label_buf
, nexthop
->ifindex
);
1189 * We have figured out the ifindex so we should always send it
1190 * This is especially useful if we are doing route
1193 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1194 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1197 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1198 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1199 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1200 *src
= &nexthop
->rmap_src
;
1201 else if (nexthop
->src
.ipv4
.s_addr
)
1202 *src
= &nexthop
->src
;
1204 if (IS_ZEBRA_DEBUG_KERNEL
)
1206 "netlink_route_multipath() (%s): "
1207 "nexthop via if %u",
1208 routedesc
, nexthop
->ifindex
);
1209 } else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1210 if (IS_ZEBRA_DEBUG_KERNEL
)
1212 "netlink_route_multipath() (%s): "
1213 "nexthop via if %u",
1214 routedesc
, nexthop
->ifindex
);
1216 rtnh
->rtnh_ifindex
= 0;
1220 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1221 zebra_nhlfe_t
*nhlfe
,
1222 struct nlmsghdr
*nlmsg
,
1223 struct rtmsg
*rtmsg
,
1224 size_t req_size
, int cmd
)
1229 family
= NHLFE_FAMILY(nhlfe
);
1230 bytelen
= (family
== AF_INET
? 4 : 16);
1231 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1232 nlmsg
, rtmsg
, req_size
, cmd
);
1237 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1238 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1239 struct rtmsg
*rtmsg
, union g_addr
**src
)
1244 family
= NHLFE_FAMILY(nhlfe
);
1245 bytelen
= (family
== AF_INET
? 4 : 16);
1246 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1251 /* Log debug information for netlink_route_multipath
1252 * if debug logging is enabled.
1254 * @param cmd: Netlink command which is to be processed
1255 * @param p: Prefix for which the change is due
1256 * @param family: Address family which the change concerns
1257 * @param zvrf: The vrf we are in
1258 * @param tableid: The table we are working on
1260 static void _netlink_route_debug(int cmd
, struct prefix
*p
,
1261 int family
, struct zebra_vrf
*zvrf
,
1264 if (IS_ZEBRA_DEBUG_KERNEL
) {
1265 char buf
[PREFIX_STRLEN
];
1267 "netlink_route_multipath(): %s %s vrf %u(%u)",
1268 nl_msg_type_to_str(cmd
),
1269 prefix2str(p
, buf
, sizeof(buf
)),
1270 zvrf_id(zvrf
), tableid
);
1274 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1276 if (IS_ZEBRA_DEBUG_KERNEL
)
1277 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1278 nl_msg_type_to_str(cmd
), label
);
1281 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1282 int llalen
, ns_id_t ns_id
)
1290 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1292 memset(&req
.n
, 0, sizeof(req
.n
));
1293 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1295 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1296 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1297 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1298 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1300 req
.ndm
.ndm_family
= AF_INET
;
1301 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1302 req
.ndm
.ndm_ifindex
= ifindex
;
1303 req
.ndm
.ndm_type
= RTN_UNICAST
;
1305 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1306 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1308 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1312 /* Routing table change via netlink interface. */
1313 /* Update flag indicates whether this is a "replace" or not. */
1314 static int netlink_route_multipath(int cmd
, struct prefix
*p
,
1315 struct prefix
*src_p
, struct route_entry
*re
,
1319 struct sockaddr_nl snl
;
1320 struct nexthop
*nexthop
= NULL
;
1321 unsigned int nexthop_num
;
1322 int family
= PREFIX_FAMILY(p
);
1323 const char *routedesc
;
1330 char buf
[NL_PKT_BUF_SIZE
];
1333 struct zebra_ns
*zns
;
1334 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1337 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1339 bytelen
= (family
== AF_INET
? 4 : 16);
1341 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1342 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1343 if ((cmd
== RTM_NEWROUTE
) && update
)
1344 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1345 req
.n
.nlmsg_type
= cmd
;
1346 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1348 req
.r
.rtm_family
= family
;
1349 req
.r
.rtm_dst_len
= p
->prefixlen
;
1350 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1351 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1352 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1355 * blackhole routes are not RTN_UNICAST, they are
1356 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1357 * so setting this value as a RTN_UNICAST would
1358 * cause the route lookup of just the prefix
1359 * to fail. So no need to specify this for
1360 * the RTM_DELROUTE case
1362 if (cmd
!= RTM_DELROUTE
)
1363 req
.r
.rtm_type
= RTN_UNICAST
;
1365 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1367 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1371 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1373 * either by the kernel or by zebra. Its purely for calculating best
1375 * by the routing protocol and for communicating with protocol peers.
1377 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1378 #if defined(SUPPORT_REALMS)
1379 if (re
->tag
> 0 && re
->tag
<= 255)
1380 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1382 /* Table corresponding to this route. */
1383 if (re
->table
< 256)
1384 req
.r
.rtm_table
= re
->table
;
1386 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1387 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1390 _netlink_route_debug(cmd
, p
, family
, zvrf
, re
->table
);
1393 * If we are not updating the route and we have received
1394 * a route delete, then all we need to fill in is the
1395 * prefix information to tell the kernel to schwack
1398 if (!update
&& cmd
== RTM_DELROUTE
)
1401 if (re
->mtu
|| re
->nexthop_mtu
) {
1402 char buf
[NL_PKT_BUF_SIZE
];
1403 struct rtattr
*rta
= (void *)buf
;
1404 uint32_t mtu
= re
->mtu
;
1405 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1406 mtu
= re
->nexthop_mtu
;
1407 rta
->rta_type
= RTA_METRICS
;
1408 rta
->rta_len
= RTA_LENGTH(0);
1409 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1410 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1414 /* Count overall nexthops so we can decide whether to use singlepath
1415 * or multipath case. */
1417 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1418 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1420 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1422 if (cmd
== RTM_DELROUTE
1423 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1429 /* Singlepath case. */
1430 if (nexthop_num
== 1 || multipath_num
== 1) {
1432 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1434 * So we want to cover 2 types of blackhole
1436 * 1) A normal blackhole route( ala from a static
1438 * 2) A recursively resolved blackhole route
1440 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1441 switch (nexthop
->bh_type
) {
1442 case BLACKHOLE_ADMINPROHIB
:
1443 req
.r
.rtm_type
= RTN_PROHIBIT
;
1445 case BLACKHOLE_REJECT
:
1446 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1449 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1454 if (CHECK_FLAG(nexthop
->flags
,
1455 NEXTHOP_FLAG_RECURSIVE
)) {
1457 if (family
== AF_INET
) {
1458 if (nexthop
->rmap_src
.ipv4
1465 } else if (nexthop
->src
.ipv4
1473 } else if (family
== AF_INET6
) {
1474 if (!IN6_IS_ADDR_UNSPECIFIED(
1482 !IN6_IS_ADDR_UNSPECIFIED(
1495 if ((cmd
== RTM_NEWROUTE
1496 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1497 || (cmd
== RTM_DELROUTE
1498 && CHECK_FLAG(nexthop
->flags
,
1499 NEXTHOP_FLAG_FIB
))) {
1500 routedesc
= nexthop
->rparent
1501 ? "recursive, single-path"
1504 _netlink_route_build_singlepath(
1505 routedesc
, bytelen
, nexthop
, &req
.n
,
1506 &req
.r
, sizeof req
, cmd
);
1511 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1512 if (family
== AF_INET
)
1513 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1514 &src
.ipv4
, bytelen
);
1515 else if (family
== AF_INET6
)
1516 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1517 &src
.ipv6
, bytelen
);
1520 char buf
[NL_PKT_BUF_SIZE
];
1521 struct rtattr
*rta
= (void *)buf
;
1522 struct rtnexthop
*rtnh
;
1523 union g_addr
*src1
= NULL
;
1525 rta
->rta_type
= RTA_MULTIPATH
;
1526 rta
->rta_len
= RTA_LENGTH(0);
1527 rtnh
= RTA_DATA(rta
);
1530 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1531 if (nexthop_num
>= multipath_num
)
1534 if (CHECK_FLAG(nexthop
->flags
,
1535 NEXTHOP_FLAG_RECURSIVE
)) {
1536 /* This only works for IPv4 now */
1538 if (family
== AF_INET
) {
1539 if (nexthop
->rmap_src
.ipv4
1546 } else if (nexthop
->src
.ipv4
1554 } else if (family
== AF_INET6
) {
1555 if (!IN6_IS_ADDR_UNSPECIFIED(
1563 !IN6_IS_ADDR_UNSPECIFIED(
1576 if ((cmd
== RTM_NEWROUTE
1577 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1578 || (cmd
== RTM_DELROUTE
1579 && CHECK_FLAG(nexthop
->flags
,
1580 NEXTHOP_FLAG_FIB
))) {
1581 routedesc
= nexthop
->rparent
1582 ? "recursive, multipath"
1586 _netlink_route_build_multipath(
1587 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1589 rtnh
= RTNH_NEXT(rtnh
);
1591 if (!setsrc
&& src1
) {
1592 if (family
== AF_INET
)
1593 src
.ipv4
= src1
->ipv4
;
1594 else if (family
== AF_INET6
)
1595 src
.ipv6
= src1
->ipv6
;
1601 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1602 if (family
== AF_INET
)
1603 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1604 &src
.ipv4
, bytelen
);
1605 else if (family
== AF_INET6
)
1606 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1607 &src
.ipv6
, bytelen
);
1608 if (IS_ZEBRA_DEBUG_KERNEL
)
1609 zlog_debug("Setting source");
1612 if (rta
->rta_len
> RTA_LENGTH(0))
1613 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1614 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1617 /* If there is no useful nexthop then return. */
1618 if (nexthop_num
== 0) {
1619 if (IS_ZEBRA_DEBUG_KERNEL
)
1621 "netlink_route_multipath(): No useful nexthop.");
1627 /* Destination netlink address. */
1628 memset(&snl
, 0, sizeof snl
);
1629 snl
.nl_family
= AF_NETLINK
;
1631 /* Talk to netlink socket. */
1632 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1636 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1639 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1647 struct zebra_ns
*zns
;
1650 memset(&req
.n
, 0, sizeof(req
.n
));
1651 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1653 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1654 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1655 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1657 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1658 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1660 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1661 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1662 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1663 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1664 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1666 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1667 &zns
->netlink_cmd
, zns
, 0);
1673 void kernel_route_rib(struct route_node
*rn
, struct prefix
*p
,
1674 struct prefix
*src_p
, struct route_entry
*old
,
1675 struct route_entry
*new)
1682 if (p
->family
== AF_INET
)
1683 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1684 new, (old
) ? 1 : 0);
1687 * So v6 route replace semantics are not in
1688 * the kernel at this point as I understand it.
1689 * So let's do a delete than an add.
1690 * In the future once v6 route replace semantics
1691 * are in we can figure out what to do here to
1692 * allow working with old and new kernels.
1694 * I'm also intentionally ignoring the failure case
1695 * of the route delete. If that happens yeah we're
1699 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1701 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1704 kernel_route_rib_pass_fail(rn
, p
, new,
1705 (!ret
) ? SOUTHBOUND_INSTALL_SUCCESS
1706 : SOUTHBOUND_INSTALL_FAILURE
);
1711 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1713 kernel_route_rib_pass_fail(rn
, p
, old
,
1714 (!ret
) ? SOUTHBOUND_DELETE_SUCCESS
1715 : SOUTHBOUND_DELETE_FAILURE
);
1719 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1720 int llalen
, ns_id_t ns_id
)
1722 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1723 addr
, lla
, llalen
, ns_id
);
1727 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1728 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1730 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1731 struct in_addr
*vtep_ip
, int cmd
)
1733 struct zebra_ns
*zns
;
1739 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1740 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1743 memset(&req
.n
, 0, sizeof(req
.n
));
1744 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1746 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1747 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1748 if (cmd
== RTM_NEWNEIGH
)
1749 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1750 req
.n
.nlmsg_type
= cmd
;
1751 req
.ndm
.ndm_family
= PF_BRIDGE
;
1752 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1753 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1756 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1757 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1758 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1760 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1765 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1767 * a "flood" MAC FDB entry.
1769 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1771 if (IS_ZEBRA_DEBUG_VXLAN
)
1772 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1773 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1775 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1779 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1780 * deleting the "flood" MAC FDB entry.
1782 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1784 if (IS_ZEBRA_DEBUG_VXLAN
)
1786 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1787 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1789 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1793 #define NDA_RTA(r) \
1794 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1797 static int netlink_macfdb_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1798 int len
, ns_id_t ns_id
)
1801 struct interface
*ifp
;
1802 struct zebra_if
*zif
;
1803 struct rtattr
*tb
[NDA_MAX
+ 1];
1804 struct interface
*br_if
;
1807 struct prefix vtep_ip
;
1808 int vid_present
= 0, dst_present
= 0;
1809 char buf
[ETHER_ADDR_STRLEN
];
1814 ndm
= NLMSG_DATA(h
);
1816 /* We only process macfdb notifications if EVPN is enabled */
1817 if (!is_evpn_enabled())
1820 /* The interface should exist. */
1821 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1823 if (!ifp
|| !ifp
->info
)
1826 /* The interface should be something we're interested in. */
1827 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1830 /* Drop "permanent" entries. */
1831 if (ndm
->ndm_state
& NUD_PERMANENT
)
1834 zif
= (struct zebra_if
*)ifp
->info
;
1835 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1836 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1837 nl_msg_type_to_str(h
->nlmsg_type
),
1838 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1839 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1843 /* Parse attributes and extract fields of interest. */
1844 memset(tb
, 0, sizeof tb
);
1845 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1847 if (!tb
[NDA_LLADDR
]) {
1848 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1849 nl_msg_type_to_str(h
->nlmsg_type
),
1850 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1851 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1855 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1857 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1858 nl_msg_type_to_str(h
->nlmsg_type
),
1859 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1860 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1861 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1865 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1867 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1869 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1870 sprintf(vid_buf
, " VLAN %u", vid
);
1874 /* TODO: Only IPv4 supported now. */
1876 vtep_ip
.family
= AF_INET
;
1877 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1878 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1880 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1883 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1885 if (IS_ZEBRA_DEBUG_KERNEL
)
1886 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1887 nl_msg_type_to_str(h
->nlmsg_type
),
1888 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1889 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1890 sticky
? "sticky " : "",
1891 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1892 dst_present
? dst_buf
: "");
1894 if (filter_vlan
&& vid
!= filter_vlan
)
1897 /* If add or update, do accordingly if learnt on a "local" interface; if
1898 * the notification is over VxLAN, this has to be related to
1900 * so perform an implicit delete of any local entry (if it exists).
1902 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1903 /* Drop "permanent" entries. */
1904 if (ndm
->ndm_state
& NUD_PERMANENT
)
1907 if (IS_ZEBRA_IF_VXLAN(ifp
))
1908 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1911 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1915 /* This is a delete notification.
1916 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1917 * 2. For a MAC over "local" interface, delete the mac
1918 * Note: We will get notifications from both bridge driver and VxLAN
1920 * Ignore the notification from VxLan driver as it is also generated
1921 * when mac moves from remote to local.
1926 if (IS_ZEBRA_IF_VXLAN(ifp
))
1927 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1930 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1933 static int netlink_macfdb_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1934 ns_id_t ns_id
, int startup
)
1939 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1942 /* Length validity. */
1943 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1947 /* We are interested only in AF_BRIDGE notifications. */
1948 ndm
= NLMSG_DATA(h
);
1949 if (ndm
->ndm_family
!= AF_BRIDGE
)
1952 return netlink_macfdb_change(snl
, h
, len
, ns_id
);
1955 /* Request for MAC FDB information from the kernel */
1956 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
1957 ifindex_t master_ifindex
)
1961 struct ifinfomsg ifm
;
1965 /* Form the request, specifying filter (rtattr) if needed. */
1966 memset(&req
, 0, sizeof(req
));
1967 req
.n
.nlmsg_type
= type
;
1968 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1969 req
.ifm
.ifi_family
= family
;
1971 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
1973 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
1977 * MAC forwarding database read using netlink interface. This is invoked
1980 int netlink_macfdb_read(struct zebra_ns
*zns
)
1984 /* Get bridge FDB table. */
1985 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
1988 /* We are reading entire table. */
1990 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
1997 * MAC forwarding database read using netlink interface. This is for a
1998 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2000 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2001 struct interface
*br_if
)
2003 struct zebra_if
*br_zif
;
2004 struct zebra_if
*zif
;
2005 struct zebra_l2info_vxlan
*vxl
;
2009 /* Save VLAN we're filtering on, if needed. */
2010 br_zif
= (struct zebra_if
*)br_if
->info
;
2011 zif
= (struct zebra_if
*)ifp
->info
;
2012 vxl
= &zif
->l2info
.vxl
;
2013 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2014 filter_vlan
= vxl
->access_vlan
;
2016 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2018 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2022 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2025 /* Reset VLAN filter. */
2030 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2031 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2032 int local
, int cmd
, uint8_t sticky
)
2034 struct zebra_ns
*zns
;
2041 struct zebra_if
*zif
;
2042 struct interface
*br_if
;
2043 struct zebra_if
*br_zif
;
2044 char buf
[ETHER_ADDR_STRLEN
];
2045 int vid_present
= 0, dst_present
= 0;
2048 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2052 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2053 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2054 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2059 memset(&req
.n
, 0, sizeof(req
.n
));
2060 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2062 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2063 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2064 if (cmd
== RTM_NEWNEIGH
)
2065 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2066 req
.n
.nlmsg_type
= cmd
;
2067 req
.ndm
.ndm_family
= AF_BRIDGE
;
2068 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2069 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2072 req
.ndm
.ndm_state
|= NUD_NOARP
;
2074 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2076 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2077 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2079 dst_alen
= 4; // TODO: hardcoded
2080 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2082 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2084 br_zif
= (struct zebra_if
*)br_if
->info
;
2085 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2086 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2088 sprintf(vid_buf
, " VLAN %u", vid
);
2090 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2092 if (IS_ZEBRA_DEBUG_KERNEL
)
2093 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2094 nl_msg_type_to_str(cmd
),
2095 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2096 ifp
->ifindex
, vid_present
? vid_buf
: "",
2097 sticky
? "sticky " : "",
2098 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2099 dst_present
? dst_buf
: "");
2101 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2106 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2109 static int netlink_ipneigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2110 int len
, ns_id_t ns_id
)
2113 struct interface
*ifp
;
2114 struct zebra_if
*zif
;
2115 struct rtattr
*tb
[NDA_MAX
+ 1];
2116 struct interface
*link_if
;
2119 char buf
[ETHER_ADDR_STRLEN
];
2120 char buf2
[INET6_ADDRSTRLEN
];
2121 int mac_present
= 0;
2122 uint8_t ext_learned
;
2124 ndm
= NLMSG_DATA(h
);
2126 /* The interface should exist. */
2127 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2129 if (!ifp
|| !ifp
->info
)
2132 zif
= (struct zebra_if
*)ifp
->info
;
2134 /* Parse attributes and extract fields of interest. */
2135 memset(tb
, 0, sizeof tb
);
2136 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2139 zlog_warn("%s family %s IF %s(%u) - no DST",
2140 nl_msg_type_to_str(h
->nlmsg_type
),
2141 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2146 memset(&ip
, 0, sizeof(struct ipaddr
));
2147 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2148 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2150 /* Drop some "permanent" entries. */
2151 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2152 char buf
[16] = "169.254.0.1";
2153 struct in_addr ipv4_ll
;
2155 if (ndm
->ndm_family
!= AF_INET
)
2158 if (!zif
->v6_2_v4_ll_neigh_entry
)
2161 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2164 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2165 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2168 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2169 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2173 /* The neighbor is present on an SVI. From this, we locate the
2175 * bridge because we're only interested in neighbors on a VxLAN bridge.
2176 * The bridge is located based on the nature of the SVI:
2177 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2179 * and is linked to the bridge
2180 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2184 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2185 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2189 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2194 memset(&mac
, 0, sizeof(struct ethaddr
));
2195 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2196 if (tb
[NDA_LLADDR
]) {
2197 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2199 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2200 nl_msg_type_to_str(h
->nlmsg_type
),
2201 nl_family_to_str(ndm
->ndm_family
),
2202 ifp
->name
, ndm
->ndm_ifindex
,
2203 (unsigned long)RTA_PAYLOAD(
2209 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2212 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2214 if (IS_ZEBRA_DEBUG_KERNEL
)
2216 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2217 nl_msg_type_to_str(h
->nlmsg_type
),
2218 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2220 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2222 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2224 ndm
->ndm_state
, ndm
->ndm_flags
);
2226 /* If the neighbor state is valid for use, process as an add or
2228 * else process as a delete. Note that the delete handling may
2230 * in re-adding the neighbor if it is a valid "remote" neighbor.
2232 if (ndm
->ndm_state
& NUD_VALID
)
2233 return zebra_vxlan_handle_kernel_neigh_update(
2234 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2237 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2240 if (IS_ZEBRA_DEBUG_KERNEL
)
2241 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2242 nl_msg_type_to_str(h
->nlmsg_type
),
2243 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2245 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2247 /* Process the delete - it may result in re-adding the neighbor if it is
2248 * a valid "remote" neighbor.
2250 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2253 static int netlink_neigh_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2254 ns_id_t ns_id
, int startup
)
2259 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2262 /* Length validity. */
2263 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2267 /* We are interested only in AF_INET or AF_INET6 notifications. */
2268 ndm
= NLMSG_DATA(h
);
2269 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2272 return netlink_neigh_change(snl
, h
, len
);
2275 /* Request for IP neighbor information from the kernel */
2276 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2285 /* Form the request, specifying filter (rtattr) if needed. */
2286 memset(&req
, 0, sizeof(req
));
2287 req
.n
.nlmsg_type
= type
;
2288 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2289 req
.ndm
.ndm_family
= family
;
2291 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2293 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2297 * IP Neighbor table read using netlink interface. This is invoked
2300 int netlink_neigh_read(struct zebra_ns
*zns
)
2304 /* Get IP neighbor table. */
2305 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2308 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2315 * IP Neighbor table read using netlink interface. This is for a specific
2318 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2322 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2326 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2332 int netlink_neigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2338 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2341 /* Length validity. */
2342 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2346 /* Is this a notification for the MAC FDB or IP neighbor table? */
2347 ndm
= NLMSG_DATA(h
);
2348 if (ndm
->ndm_family
== AF_BRIDGE
)
2349 return netlink_macfdb_change(snl
, h
, len
, ns_id
);
2351 if (ndm
->ndm_type
!= RTN_UNICAST
)
2354 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2355 return netlink_ipneigh_change(snl
, h
, len
, ns_id
);
2360 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2361 struct ethaddr
*mac
, uint32_t flags
, int cmd
)
2370 struct zebra_ns
*zns
;
2371 char buf
[INET6_ADDRSTRLEN
];
2372 char buf2
[ETHER_ADDR_STRLEN
];
2373 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2376 memset(&req
.n
, 0, sizeof(req
.n
));
2377 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2379 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2380 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2381 if (cmd
== RTM_NEWNEIGH
)
2382 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2383 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2384 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2385 req
.ndm
.ndm_state
= flags
;
2386 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2387 req
.ndm
.ndm_type
= RTN_UNICAST
;
2388 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2391 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2392 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2394 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2396 if (IS_ZEBRA_DEBUG_KERNEL
)
2397 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2398 nl_msg_type_to_str(cmd
),
2399 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2400 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2401 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2404 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2408 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2409 struct in_addr vtep_ip
, uint8_t sticky
)
2411 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2415 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2416 struct in_addr vtep_ip
, int local
)
2418 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2422 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2423 struct ethaddr
*mac
)
2425 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_NOARP
, RTM_NEWNEIGH
);
2428 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2430 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2434 * MPLS label forwarding table change via netlink interface.
2436 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2439 zebra_nhlfe_t
*nhlfe
;
2440 struct nexthop
*nexthop
= NULL
;
2441 unsigned int nexthop_num
;
2442 const char *routedesc
;
2443 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2449 char buf
[NL_PKT_BUF_SIZE
];
2452 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2455 * Count # nexthops so we can decide whether to use singlepath
2456 * or multipath case.
2459 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2460 nexthop
= nhlfe
->nexthop
;
2463 if (cmd
== RTM_NEWROUTE
) {
2464 /* Count all selected NHLFEs */
2465 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2466 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2470 /* Count all installed NHLFEs */
2471 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2472 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2477 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2480 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2481 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2482 req
.n
.nlmsg_type
= cmd
;
2483 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2485 req
.r
.rtm_family
= AF_MPLS
;
2486 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2487 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2488 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2489 req
.r
.rtm_type
= RTN_UNICAST
;
2491 if (cmd
== RTM_NEWROUTE
) {
2492 /* We do a replace to handle update. */
2493 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2495 /* set the protocol value if installing */
2496 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2497 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2500 /* Fill destination */
2501 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2502 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2504 /* Fill nexthops (paths) based on single-path or multipath. The paths
2505 * chosen depend on the operation.
2507 if (nexthop_num
== 1 || multipath_num
== 1) {
2508 routedesc
= "single-path";
2509 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2512 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2513 nexthop
= nhlfe
->nexthop
;
2517 if ((cmd
== RTM_NEWROUTE
2518 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2519 && CHECK_FLAG(nexthop
->flags
,
2520 NEXTHOP_FLAG_ACTIVE
)))
2521 || (cmd
== RTM_DELROUTE
2522 && (CHECK_FLAG(nhlfe
->flags
,
2523 NHLFE_FLAG_INSTALLED
)
2524 && CHECK_FLAG(nexthop
->flags
,
2525 NEXTHOP_FLAG_FIB
)))) {
2526 /* Add the gateway */
2527 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2534 } else /* Multipath case */
2536 char buf
[NL_PKT_BUF_SIZE
];
2537 struct rtattr
*rta
= (void *)buf
;
2538 struct rtnexthop
*rtnh
;
2539 union g_addr
*src1
= NULL
;
2541 rta
->rta_type
= RTA_MULTIPATH
;
2542 rta
->rta_len
= RTA_LENGTH(0);
2543 rtnh
= RTA_DATA(rta
);
2545 routedesc
= "multipath";
2546 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2549 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2550 nexthop
= nhlfe
->nexthop
;
2554 if (nexthop_num
>= multipath_num
)
2557 if ((cmd
== RTM_NEWROUTE
2558 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2559 && CHECK_FLAG(nexthop
->flags
,
2560 NEXTHOP_FLAG_ACTIVE
)))
2561 || (cmd
== RTM_DELROUTE
2562 && (CHECK_FLAG(nhlfe
->flags
,
2563 NHLFE_FLAG_INSTALLED
)
2564 && CHECK_FLAG(nexthop
->flags
,
2565 NEXTHOP_FLAG_FIB
)))) {
2568 /* Build the multipath */
2569 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2572 rtnh
= RTNH_NEXT(rtnh
);
2576 /* Add the multipath */
2577 if (rta
->rta_len
> RTA_LENGTH(0))
2578 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2579 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2582 /* Talk to netlink socket. */
2583 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2586 #endif /* HAVE_NETLINK */