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
, distance
, 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
, distance
, 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
767 || rtm
->rtm_family
== AF_ETHERNET
768 || rtm
->rtm_family
== AF_MPLS
)) {
770 "Invalid address family: %d received from kernel route change: %d",
771 rtm
->rtm_family
, h
->nlmsg_type
);
775 /* Connected route. */
776 if (IS_ZEBRA_DEBUG_KERNEL
)
777 zlog_debug("%s %s %s proto %s NS %u",
778 nl_msg_type_to_str(h
->nlmsg_type
),
779 nl_family_to_str(rtm
->rtm_family
),
780 nl_rttype_to_str(rtm
->rtm_type
),
781 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
783 /* We don't care about change notifications for the MPLS table. */
784 /* TODO: Revisit this. */
785 if (rtm
->rtm_family
== AF_MPLS
)
788 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
790 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
793 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
797 if (rtm
->rtm_type
== RTN_MULTICAST
)
798 netlink_route_change_read_multicast(h
, ns_id
, startup
);
800 netlink_route_change_read_unicast(h
, ns_id
, startup
);
804 /* Request for specific route information from the kernel */
805 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
812 /* Form the request, specifying filter (rtattr) if needed. */
813 memset(&req
, 0, sizeof(req
));
814 req
.n
.nlmsg_type
= type
;
815 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
816 req
.rtm
.rtm_family
= family
;
818 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
821 /* Routing table read function using netlink interface. Only called
823 int netlink_route_read(struct zebra_ns
*zns
)
827 /* Get IPv4 routing table. */
828 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
831 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
832 &zns
->netlink_cmd
, zns
, 0, 1);
836 /* Get IPv6 routing table. */
837 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
840 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
841 &zns
->netlink_cmd
, zns
, 0, 1);
848 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
850 struct nlmsghdr
*nlmsg
,
851 size_t req_size
, int bytelen
,
852 struct nexthop
*nexthop
)
854 if (route_family
== AF_MPLS
) {
855 struct gw_family_t gw_fam
;
857 gw_fam
.family
= gw_family
;
858 if (gw_family
== AF_INET
)
859 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
861 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
862 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
865 if (gw_family
== AF_INET
)
866 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
867 &nexthop
->gate
.ipv4
, bytelen
);
869 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
870 &nexthop
->gate
.ipv6
, bytelen
);
874 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
877 struct rtnexthop
*rtnh
,
878 size_t req_size
, int bytelen
,
879 struct nexthop
*nexthop
)
881 if (route_family
== AF_MPLS
) {
882 struct gw_family_t gw_fam
;
884 gw_fam
.family
= gw_family
;
885 if (gw_family
== AF_INET
)
886 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
888 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
889 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
891 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
893 if (gw_family
== AF_INET
)
894 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
895 &nexthop
->gate
.ipv4
, bytelen
);
897 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
898 &nexthop
->gate
.ipv6
, bytelen
);
899 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
903 /* This function takes a nexthop as argument and adds
904 * the appropriate netlink attributes to an existing
907 * @param routedesc: Human readable description of route type
908 * (direct/recursive, single-/multipath)
909 * @param bytelen: Length of addresses in bytes.
910 * @param nexthop: Nexthop information
911 * @param nlmsg: nlmsghdr structure to fill in.
912 * @param req_size: The size allocated for the message.
914 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
915 struct nexthop
*nexthop
,
916 struct nlmsghdr
*nlmsg
,
918 size_t req_size
, int cmd
)
920 struct mpls_label_stack
*nh_label
;
921 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
926 * label_buf is *only* currently used within debugging.
927 * As such when we assign it we are guarding it inside
928 * a debug test. If you want to change this make sure
929 * you fix this assumption
934 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
937 nh_label
= nh
->nh_label
;
938 if (!nh_label
|| !nh_label
->num_labels
)
941 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
942 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
945 if (IS_ZEBRA_DEBUG_KERNEL
) {
947 sprintf(label_buf
, "label %u",
950 sprintf(label_buf1
, "/%u",
952 strlcat(label_buf
, label_buf1
,
957 out_lse
[num_labels
] =
958 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
964 /* Set the BoS bit */
965 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
967 if (rtmsg
->rtm_family
== AF_MPLS
)
968 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
969 num_labels
* sizeof(mpls_lse_t
));
972 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
974 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
976 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
977 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
978 num_labels
* sizeof(mpls_lse_t
));
979 addattr_nest_end(nlmsg
, nest
);
983 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
984 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
986 if (rtmsg
->rtm_family
== AF_INET
987 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
988 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
989 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
990 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
991 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
993 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
994 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
995 &nexthop
->rmap_src
.ipv4
, bytelen
);
996 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
997 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
998 &nexthop
->src
.ipv4
, bytelen
);
1000 if (IS_ZEBRA_DEBUG_KERNEL
)
1002 " 5549: _netlink_route_build_singlepath() (%s): "
1003 "nexthop via %s %s if %u(%u)",
1004 routedesc
, ipv4_ll_buf
, label_buf
,
1005 nexthop
->ifindex
, nexthop
->vrf_id
);
1009 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1010 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1011 /* Send deletes to the kernel without specifying the next-hop */
1012 if (cmd
!= RTM_DELROUTE
)
1013 _netlink_route_nl_add_gateway_info(
1014 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1017 if (cmd
== RTM_NEWROUTE
) {
1018 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1019 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1020 &nexthop
->rmap_src
.ipv4
, bytelen
);
1021 else if (nexthop
->src
.ipv4
.s_addr
)
1022 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1023 &nexthop
->src
.ipv4
, bytelen
);
1026 if (IS_ZEBRA_DEBUG_KERNEL
)
1028 "netlink_route_multipath() (%s): "
1029 "nexthop via %s %s if %u(%u)",
1030 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1031 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1034 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1035 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1036 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1037 nlmsg
, req_size
, bytelen
,
1040 if (cmd
== RTM_NEWROUTE
) {
1041 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1042 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1043 &nexthop
->rmap_src
.ipv6
, bytelen
);
1044 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1045 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1046 &nexthop
->src
.ipv6
, bytelen
);
1049 if (IS_ZEBRA_DEBUG_KERNEL
)
1051 "netlink_route_multipath() (%s): "
1052 "nexthop via %s %s if %u(%u)",
1053 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1054 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1058 * We have the ifindex so we should always send it
1059 * This is especially useful if we are doing route
1062 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1063 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1065 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1066 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1067 if (cmd
== RTM_NEWROUTE
) {
1068 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1069 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1070 &nexthop
->rmap_src
.ipv4
, bytelen
);
1071 else if (nexthop
->src
.ipv4
.s_addr
)
1072 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1073 &nexthop
->src
.ipv4
, bytelen
);
1076 if (IS_ZEBRA_DEBUG_KERNEL
)
1078 "netlink_route_multipath() (%s): "
1079 "nexthop via if %u(%u)",
1080 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1083 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1084 if (cmd
== RTM_NEWROUTE
) {
1085 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1086 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1087 &nexthop
->rmap_src
.ipv6
, bytelen
);
1088 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1089 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1090 &nexthop
->src
.ipv6
, bytelen
);
1093 if (IS_ZEBRA_DEBUG_KERNEL
)
1095 "netlink_route_multipath() (%s): "
1096 "nexthop via if %u(%u)",
1097 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1101 /* This function takes a nexthop as argument and
1102 * appends to the given rtattr/rtnexthop pair the
1103 * representation of the nexthop. If the nexthop
1104 * defines a preferred source, the src parameter
1105 * will be modified to point to that src, otherwise
1106 * it will be kept unmodified.
1108 * @param routedesc: Human readable description of route type
1109 * (direct/recursive, single-/multipath)
1110 * @param bytelen: Length of addresses in bytes.
1111 * @param nexthop: Nexthop information
1112 * @param rta: rtnetlink attribute structure
1113 * @param rtnh: pointer to an rtnetlink nexthop structure
1114 * @param src: pointer pointing to a location where
1115 * the prefsrc should be stored.
1117 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1118 struct nexthop
*nexthop
,
1120 struct rtnexthop
*rtnh
,
1121 struct rtmsg
*rtmsg
,
1124 struct mpls_label_stack
*nh_label
;
1125 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1127 char label_buf
[256];
1129 rtnh
->rtnh_len
= sizeof(*rtnh
);
1130 rtnh
->rtnh_flags
= 0;
1131 rtnh
->rtnh_hops
= 0;
1132 rta
->rta_len
+= rtnh
->rtnh_len
;
1135 * label_buf is *only* currently used within debugging.
1136 * As such when we assign it we are guarding it inside
1137 * a debug test. If you want to change this make sure
1138 * you fix this assumption
1140 label_buf
[0] = '\0';
1143 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1144 char label_buf1
[20];
1146 nh_label
= nh
->nh_label
;
1147 if (!nh_label
|| !nh_label
->num_labels
)
1150 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1151 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1154 if (IS_ZEBRA_DEBUG_KERNEL
) {
1156 sprintf(label_buf
, "label %u",
1157 nh_label
->label
[i
]);
1159 sprintf(label_buf1
, "/%u",
1160 nh_label
->label
[i
]);
1161 strlcat(label_buf
, label_buf1
,
1166 out_lse
[num_labels
] =
1167 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1173 /* Set the BoS bit */
1174 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1176 if (rtmsg
->rtm_family
== AF_MPLS
) {
1177 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1179 num_labels
* sizeof(mpls_lse_t
));
1181 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1183 struct rtattr
*nest
;
1184 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1185 int len
= rta
->rta_len
;
1187 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1188 &encap
, sizeof(uint16_t));
1189 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1190 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1192 num_labels
* sizeof(mpls_lse_t
));
1193 rta_nest_end(rta
, nest
);
1194 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1198 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1199 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1201 if (rtmsg
->rtm_family
== AF_INET
1202 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1203 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1205 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1206 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1208 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1209 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1211 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1212 *src
= &nexthop
->rmap_src
;
1213 else if (nexthop
->src
.ipv4
.s_addr
)
1214 *src
= &nexthop
->src
;
1216 if (IS_ZEBRA_DEBUG_KERNEL
)
1218 " 5549: netlink_route_build_multipath() (%s): "
1219 "nexthop via %s %s if %u",
1220 routedesc
, ipv4_ll_buf
, label_buf
,
1225 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1226 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1227 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1228 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1230 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1231 *src
= &nexthop
->rmap_src
;
1232 else if (nexthop
->src
.ipv4
.s_addr
)
1233 *src
= &nexthop
->src
;
1235 if (IS_ZEBRA_DEBUG_KERNEL
)
1237 "netlink_route_multipath() (%s): "
1238 "nexthop via %s %s if %u",
1239 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1240 label_buf
, nexthop
->ifindex
);
1242 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1243 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1244 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1245 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1248 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1249 *src
= &nexthop
->rmap_src
;
1250 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1251 *src
= &nexthop
->src
;
1253 if (IS_ZEBRA_DEBUG_KERNEL
)
1255 "netlink_route_multipath() (%s): "
1256 "nexthop via %s %s if %u",
1257 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1258 label_buf
, nexthop
->ifindex
);
1262 * We have figured out the ifindex so we should always send it
1263 * This is especially useful if we are doing route
1266 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1267 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1270 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1271 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1272 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1273 *src
= &nexthop
->rmap_src
;
1274 else if (nexthop
->src
.ipv4
.s_addr
)
1275 *src
= &nexthop
->src
;
1277 if (IS_ZEBRA_DEBUG_KERNEL
)
1279 "netlink_route_multipath() (%s): "
1280 "nexthop via if %u",
1281 routedesc
, nexthop
->ifindex
);
1285 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1286 zebra_nhlfe_t
*nhlfe
,
1287 struct nlmsghdr
*nlmsg
,
1288 struct rtmsg
*rtmsg
,
1289 size_t req_size
, int cmd
)
1294 family
= NHLFE_FAMILY(nhlfe
);
1295 bytelen
= (family
== AF_INET
? 4 : 16);
1296 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1297 nlmsg
, rtmsg
, req_size
, cmd
);
1302 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1303 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1304 struct rtmsg
*rtmsg
, union g_addr
**src
)
1309 family
= NHLFE_FAMILY(nhlfe
);
1310 bytelen
= (family
== AF_INET
? 4 : 16);
1311 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1316 /* Log debug information for netlink_route_multipath
1317 * if debug logging is enabled.
1319 * @param cmd: Netlink command which is to be processed
1320 * @param p: Prefix for which the change is due
1321 * @param family: Address family which the change concerns
1322 * @param zvrf: The vrf we are in
1323 * @param tableid: The table we are working on
1325 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1326 int family
, vrf_id_t vrfid
,
1329 if (IS_ZEBRA_DEBUG_KERNEL
) {
1330 char buf
[PREFIX_STRLEN
];
1332 "netlink_route_multipath(): %s %s vrf %u(%u)",
1333 nl_msg_type_to_str(cmd
),
1334 prefix2str(p
, buf
, sizeof(buf
)),
1339 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1341 if (IS_ZEBRA_DEBUG_KERNEL
)
1342 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1343 nl_msg_type_to_str(cmd
), label
);
1346 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1347 int llalen
, ns_id_t ns_id
)
1355 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1357 memset(&req
, 0, sizeof(req
));
1359 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1360 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1361 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1362 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1364 req
.ndm
.ndm_family
= AF_INET
;
1365 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1366 req
.ndm
.ndm_ifindex
= ifindex
;
1367 req
.ndm
.ndm_type
= RTN_UNICAST
;
1369 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1370 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1372 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1376 /* Routing table change via netlink interface. */
1377 /* Update flag indicates whether this is a "replace" or not. */
1378 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1379 const struct prefix
*src_p
,
1380 struct route_entry
*re
,
1384 struct sockaddr_nl snl
;
1385 struct nexthop
*nexthop
= NULL
;
1386 unsigned int nexthop_num
;
1387 int family
= PREFIX_FAMILY(p
);
1388 const char *routedesc
;
1395 char buf
[NL_PKT_BUF_SIZE
];
1398 struct zebra_ns
*zns
;
1399 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1402 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1404 bytelen
= (family
== AF_INET
? 4 : 16);
1406 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1407 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1408 if ((cmd
== RTM_NEWROUTE
) && update
)
1409 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1410 req
.n
.nlmsg_type
= cmd
;
1411 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1413 req
.r
.rtm_family
= family
;
1414 req
.r
.rtm_dst_len
= p
->prefixlen
;
1415 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1416 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1417 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1420 * blackhole routes are not RTN_UNICAST, they are
1421 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1422 * so setting this value as a RTN_UNICAST would
1423 * cause the route lookup of just the prefix
1424 * to fail. So no need to specify this for
1425 * the RTM_DELROUTE case
1427 if (cmd
!= RTM_DELROUTE
)
1428 req
.r
.rtm_type
= RTN_UNICAST
;
1430 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1432 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1436 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1438 * either by the kernel or by zebra. Its purely for calculating best
1440 * by the routing protocol and for communicating with protocol peers.
1442 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1443 #if defined(SUPPORT_REALMS)
1444 if (re
->tag
> 0 && re
->tag
<= 255)
1445 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1447 /* Table corresponding to this route. */
1448 if (re
->table
< 256)
1449 req
.r
.rtm_table
= re
->table
;
1451 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1452 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1455 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1458 * If we are not updating the route and we have received
1459 * a route delete, then all we need to fill in is the
1460 * prefix information to tell the kernel to schwack
1463 if (!update
&& cmd
== RTM_DELROUTE
)
1466 if (re
->mtu
|| re
->nexthop_mtu
) {
1467 char buf
[NL_PKT_BUF_SIZE
];
1468 struct rtattr
*rta
= (void *)buf
;
1469 uint32_t mtu
= re
->mtu
;
1470 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1471 mtu
= re
->nexthop_mtu
;
1472 rta
->rta_type
= RTA_METRICS
;
1473 rta
->rta_len
= RTA_LENGTH(0);
1474 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1475 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1479 /* Count overall nexthops so we can decide whether to use singlepath
1480 * or multipath case. */
1482 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1483 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1485 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1487 if (cmd
== RTM_DELROUTE
1488 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1494 /* Singlepath case. */
1495 if (nexthop_num
== 1 || multipath_num
== 1) {
1497 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1499 * So we want to cover 2 types of blackhole
1501 * 1) A normal blackhole route( ala from a static
1503 * 2) A recursively resolved blackhole route
1505 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1506 switch (nexthop
->bh_type
) {
1507 case BLACKHOLE_ADMINPROHIB
:
1508 req
.r
.rtm_type
= RTN_PROHIBIT
;
1510 case BLACKHOLE_REJECT
:
1511 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1514 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1519 if (CHECK_FLAG(nexthop
->flags
,
1520 NEXTHOP_FLAG_RECURSIVE
)) {
1522 if (family
== AF_INET
) {
1523 if (nexthop
->rmap_src
.ipv4
1530 } else if (nexthop
->src
.ipv4
1538 } else if (family
== AF_INET6
) {
1539 if (!IN6_IS_ADDR_UNSPECIFIED(
1547 !IN6_IS_ADDR_UNSPECIFIED(
1560 if ((cmd
== RTM_NEWROUTE
1561 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1562 || (cmd
== RTM_DELROUTE
1563 && CHECK_FLAG(nexthop
->flags
,
1564 NEXTHOP_FLAG_FIB
))) {
1565 routedesc
= nexthop
->rparent
1566 ? "recursive, single-path"
1569 _netlink_route_build_singlepath(
1570 routedesc
, bytelen
, nexthop
, &req
.n
,
1571 &req
.r
, sizeof req
, cmd
);
1576 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1577 if (family
== AF_INET
)
1578 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1579 &src
.ipv4
, bytelen
);
1580 else if (family
== AF_INET6
)
1581 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1582 &src
.ipv6
, bytelen
);
1585 char buf
[NL_PKT_BUF_SIZE
];
1586 struct rtattr
*rta
= (void *)buf
;
1587 struct rtnexthop
*rtnh
;
1588 union g_addr
*src1
= NULL
;
1590 rta
->rta_type
= RTA_MULTIPATH
;
1591 rta
->rta_len
= RTA_LENGTH(0);
1592 rtnh
= RTA_DATA(rta
);
1595 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1596 if (nexthop_num
>= multipath_num
)
1599 if (CHECK_FLAG(nexthop
->flags
,
1600 NEXTHOP_FLAG_RECURSIVE
)) {
1601 /* This only works for IPv4 now */
1603 if (family
== AF_INET
) {
1604 if (nexthop
->rmap_src
.ipv4
1611 } else if (nexthop
->src
.ipv4
1619 } else if (family
== AF_INET6
) {
1620 if (!IN6_IS_ADDR_UNSPECIFIED(
1628 !IN6_IS_ADDR_UNSPECIFIED(
1641 if ((cmd
== RTM_NEWROUTE
1642 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1643 || (cmd
== RTM_DELROUTE
1644 && CHECK_FLAG(nexthop
->flags
,
1645 NEXTHOP_FLAG_FIB
))) {
1646 routedesc
= nexthop
->rparent
1647 ? "recursive, multipath"
1651 _netlink_route_build_multipath(
1652 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1654 rtnh
= RTNH_NEXT(rtnh
);
1656 if (!setsrc
&& src1
) {
1657 if (family
== AF_INET
)
1658 src
.ipv4
= src1
->ipv4
;
1659 else if (family
== AF_INET6
)
1660 src
.ipv6
= src1
->ipv6
;
1666 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1667 if (family
== AF_INET
)
1668 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1669 &src
.ipv4
, bytelen
);
1670 else if (family
== AF_INET6
)
1671 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1672 &src
.ipv6
, bytelen
);
1673 if (IS_ZEBRA_DEBUG_KERNEL
)
1674 zlog_debug("Setting source");
1677 if (rta
->rta_len
> RTA_LENGTH(0))
1678 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1679 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1682 /* If there is no useful nexthop then return. */
1683 if (nexthop_num
== 0) {
1684 if (IS_ZEBRA_DEBUG_KERNEL
)
1686 "netlink_route_multipath(): No useful nexthop.");
1692 /* Destination netlink address. */
1693 memset(&snl
, 0, sizeof snl
);
1694 snl
.nl_family
= AF_NETLINK
;
1696 /* Talk to netlink socket. */
1697 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1701 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1704 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1712 struct zebra_ns
*zns
;
1715 memset(&req
, 0, sizeof(req
));
1717 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1718 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1719 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1721 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1722 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1724 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1725 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1726 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1727 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1728 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1730 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1731 &zns
->netlink_cmd
, zns
, 0);
1737 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1738 const struct prefix
*p
,
1739 const struct prefix
*src_p
,
1740 struct route_entry
*old
,
1741 struct route_entry
*new)
1748 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1749 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1750 new, (old
) ? 1 : 0);
1753 * So v6 route replace semantics are not in
1754 * the kernel at this point as I understand it.
1755 * So let's do a delete than an add.
1756 * In the future once v6 route replace semantics
1757 * are in we can figure out what to do here to
1758 * allow working with old and new kernels.
1760 * I'm also intentionally ignoring the failure case
1761 * of the route delete. If that happens yeah we're
1765 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1767 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1770 kernel_route_rib_pass_fail(rn
, p
, new,
1771 (!ret
) ? DP_INSTALL_SUCCESS
1772 : DP_INSTALL_FAILURE
);
1773 return DP_REQUEST_SUCCESS
;
1777 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1779 kernel_route_rib_pass_fail(rn
, p
, old
,
1780 (!ret
) ? DP_DELETE_SUCCESS
1781 : DP_DELETE_FAILURE
);
1784 return DP_REQUEST_SUCCESS
;
1787 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1788 int llalen
, ns_id_t ns_id
)
1790 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1791 addr
, lla
, llalen
, ns_id
);
1795 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1796 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1798 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1799 struct in_addr
*vtep_ip
, int cmd
)
1801 struct zebra_ns
*zns
;
1807 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1808 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1811 memset(&req
, 0, sizeof(req
));
1813 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1814 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1815 if (cmd
== RTM_NEWNEIGH
)
1816 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1817 req
.n
.nlmsg_type
= cmd
;
1818 req
.ndm
.ndm_family
= PF_BRIDGE
;
1819 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1820 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1823 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1824 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1825 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1827 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1832 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1834 * a "flood" MAC FDB entry.
1836 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1838 if (IS_ZEBRA_DEBUG_VXLAN
)
1839 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1840 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1842 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1846 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1847 * deleting the "flood" MAC FDB entry.
1849 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1851 if (IS_ZEBRA_DEBUG_VXLAN
)
1853 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1854 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1856 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1860 #define NDA_RTA(r) \
1861 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1864 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1867 struct interface
*ifp
;
1868 struct zebra_if
*zif
;
1869 struct rtattr
*tb
[NDA_MAX
+ 1];
1870 struct interface
*br_if
;
1873 struct prefix vtep_ip
;
1874 int vid_present
= 0, dst_present
= 0;
1875 char buf
[ETHER_ADDR_STRLEN
];
1880 ndm
= NLMSG_DATA(h
);
1882 /* We only process macfdb notifications if EVPN is enabled */
1883 if (!is_evpn_enabled())
1886 /* The interface should exist. */
1887 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1889 if (!ifp
|| !ifp
->info
)
1892 /* The interface should be something we're interested in. */
1893 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1896 /* Drop "permanent" entries. */
1897 if (ndm
->ndm_state
& NUD_PERMANENT
)
1900 zif
= (struct zebra_if
*)ifp
->info
;
1901 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1902 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1903 nl_msg_type_to_str(h
->nlmsg_type
),
1904 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1905 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1909 /* Parse attributes and extract fields of interest. */
1910 memset(tb
, 0, sizeof tb
);
1911 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1913 if (!tb
[NDA_LLADDR
]) {
1914 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1915 nl_msg_type_to_str(h
->nlmsg_type
),
1916 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1917 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1921 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1923 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1924 nl_msg_type_to_str(h
->nlmsg_type
),
1925 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1926 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1927 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1931 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1933 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1935 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1936 sprintf(vid_buf
, " VLAN %u", vid
);
1940 /* TODO: Only IPv4 supported now. */
1942 vtep_ip
.family
= AF_INET
;
1943 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1944 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1946 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1949 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1951 if (IS_ZEBRA_DEBUG_KERNEL
)
1952 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1953 nl_msg_type_to_str(h
->nlmsg_type
),
1954 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1955 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1956 sticky
? "sticky " : "",
1957 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1958 dst_present
? dst_buf
: "");
1960 if (filter_vlan
&& vid
!= filter_vlan
)
1963 /* If add or update, do accordingly if learnt on a "local" interface; if
1964 * the notification is over VxLAN, this has to be related to
1966 * so perform an implicit delete of any local entry (if it exists).
1968 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1969 /* Drop "permanent" entries. */
1970 if (ndm
->ndm_state
& NUD_PERMANENT
)
1973 if (IS_ZEBRA_IF_VXLAN(ifp
))
1974 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1977 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1981 /* This is a delete notification.
1982 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1983 * 2. For a MAC over "local" interface, delete the mac
1984 * Note: We will get notifications from both bridge driver and VxLAN
1986 * Ignore the notification from VxLan driver as it is also generated
1987 * when mac moves from remote to local.
1992 if (IS_ZEBRA_IF_VXLAN(ifp
))
1993 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1996 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1999 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2004 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2007 /* Length validity. */
2008 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2012 /* We are interested only in AF_BRIDGE notifications. */
2013 ndm
= NLMSG_DATA(h
);
2014 if (ndm
->ndm_family
!= AF_BRIDGE
)
2017 return netlink_macfdb_change(h
, len
, ns_id
);
2020 /* Request for MAC FDB information from the kernel */
2021 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2022 ifindex_t master_ifindex
)
2026 struct ifinfomsg ifm
;
2030 /* Form the request, specifying filter (rtattr) if needed. */
2031 memset(&req
, 0, sizeof(req
));
2032 req
.n
.nlmsg_type
= type
;
2033 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2034 req
.ifm
.ifi_family
= family
;
2036 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2038 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2042 * MAC forwarding database read using netlink interface. This is invoked
2045 int netlink_macfdb_read(struct zebra_ns
*zns
)
2049 /* Get bridge FDB table. */
2050 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2053 /* We are reading entire table. */
2055 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2062 * MAC forwarding database read using netlink interface. This is for a
2063 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2065 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2066 struct interface
*br_if
)
2068 struct zebra_if
*br_zif
;
2069 struct zebra_if
*zif
;
2070 struct zebra_l2info_vxlan
*vxl
;
2074 /* Save VLAN we're filtering on, if needed. */
2075 br_zif
= (struct zebra_if
*)br_if
->info
;
2076 zif
= (struct zebra_if
*)ifp
->info
;
2077 vxl
= &zif
->l2info
.vxl
;
2078 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2079 filter_vlan
= vxl
->access_vlan
;
2081 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2083 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2087 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2090 /* Reset VLAN filter. */
2095 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2096 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2097 int local
, int cmd
, uint8_t sticky
)
2099 struct zebra_ns
*zns
;
2106 struct zebra_if
*zif
;
2107 struct interface
*br_if
;
2108 struct zebra_if
*br_zif
;
2109 char buf
[ETHER_ADDR_STRLEN
];
2110 int vid_present
= 0, dst_present
= 0;
2113 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2117 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2118 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2119 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2124 memset(&req
, 0, sizeof(req
));
2126 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2127 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2128 if (cmd
== RTM_NEWNEIGH
)
2129 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2130 req
.n
.nlmsg_type
= cmd
;
2131 req
.ndm
.ndm_family
= AF_BRIDGE
;
2132 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2133 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2136 req
.ndm
.ndm_state
|= NUD_NOARP
;
2138 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2140 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2141 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2143 dst_alen
= 4; // TODO: hardcoded
2144 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2146 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2148 br_zif
= (struct zebra_if
*)br_if
->info
;
2149 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2150 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2152 sprintf(vid_buf
, " VLAN %u", vid
);
2154 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2156 if (IS_ZEBRA_DEBUG_KERNEL
)
2157 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2158 nl_msg_type_to_str(cmd
),
2159 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2160 ifp
->ifindex
, vid_present
? vid_buf
: "",
2161 sticky
? "sticky " : "",
2162 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2163 dst_present
? dst_buf
: "");
2165 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2170 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2173 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2176 struct interface
*ifp
;
2177 struct zebra_if
*zif
;
2178 struct rtattr
*tb
[NDA_MAX
+ 1];
2179 struct interface
*link_if
;
2182 char buf
[ETHER_ADDR_STRLEN
];
2183 char buf2
[INET6_ADDRSTRLEN
];
2184 int mac_present
= 0;
2185 uint8_t ext_learned
;
2186 uint8_t router_flag
;
2188 ndm
= NLMSG_DATA(h
);
2190 /* The interface should exist. */
2191 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2193 if (!ifp
|| !ifp
->info
)
2196 zif
= (struct zebra_if
*)ifp
->info
;
2198 /* Parse attributes and extract fields of interest. */
2199 memset(tb
, 0, sizeof tb
);
2200 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2203 zlog_warn("%s family %s IF %s(%u) - no DST",
2204 nl_msg_type_to_str(h
->nlmsg_type
),
2205 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2210 memset(&ip
, 0, sizeof(struct ipaddr
));
2211 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2212 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2214 /* Drop some "permanent" entries. */
2215 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2216 char buf
[16] = "169.254.0.1";
2217 struct in_addr ipv4_ll
;
2219 if (ndm
->ndm_family
!= AF_INET
)
2222 if (!zif
->v6_2_v4_ll_neigh_entry
)
2225 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2228 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2229 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2232 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2233 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2237 /* The neighbor is present on an SVI. From this, we locate the
2239 * bridge because we're only interested in neighbors on a VxLAN bridge.
2240 * The bridge is located based on the nature of the SVI:
2241 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2243 * and is linked to the bridge
2244 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2248 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2249 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2253 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2258 memset(&mac
, 0, sizeof(struct ethaddr
));
2259 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2260 if (tb
[NDA_LLADDR
]) {
2261 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2263 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2264 nl_msg_type_to_str(h
->nlmsg_type
),
2265 nl_family_to_str(ndm
->ndm_family
),
2266 ifp
->name
, ndm
->ndm_ifindex
,
2267 (unsigned long)RTA_PAYLOAD(
2273 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2276 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2277 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2279 if (IS_ZEBRA_DEBUG_KERNEL
)
2281 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2282 nl_msg_type_to_str(h
->nlmsg_type
),
2283 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2285 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2287 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2289 ndm
->ndm_state
, ndm
->ndm_flags
);
2291 /* If the neighbor state is valid for use, process as an add or
2293 * else process as a delete. Note that the delete handling may
2295 * in re-adding the neighbor if it is a valid "remote" neighbor.
2297 if (ndm
->ndm_state
& NUD_VALID
)
2298 return zebra_vxlan_handle_kernel_neigh_update(
2299 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2300 ext_learned
, router_flag
);
2302 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2305 if (IS_ZEBRA_DEBUG_KERNEL
)
2306 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2307 nl_msg_type_to_str(h
->nlmsg_type
),
2308 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2310 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2312 /* Process the delete - it may result in re-adding the neighbor if it is
2313 * a valid "remote" neighbor.
2315 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2318 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2323 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2326 /* Length validity. */
2327 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2331 /* We are interested only in AF_INET or AF_INET6 notifications. */
2332 ndm
= NLMSG_DATA(h
);
2333 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2336 return netlink_neigh_change(h
, len
);
2339 /* Request for IP neighbor information from the kernel */
2340 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2349 /* Form the request, specifying filter (rtattr) if needed. */
2350 memset(&req
, 0, sizeof(req
));
2351 req
.n
.nlmsg_type
= type
;
2352 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2353 req
.ndm
.ndm_family
= family
;
2355 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2357 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2361 * IP Neighbor table read using netlink interface. This is invoked
2364 int netlink_neigh_read(struct zebra_ns
*zns
)
2368 /* Get IP neighbor table. */
2369 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2372 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2379 * IP Neighbor table read using netlink interface. This is for a specific
2382 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2386 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2390 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2396 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2401 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2404 /* Length validity. */
2405 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2407 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2408 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2409 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2413 /* Is this a notification for the MAC FDB or IP neighbor table? */
2414 ndm
= NLMSG_DATA(h
);
2415 if (ndm
->ndm_family
== AF_BRIDGE
)
2416 return netlink_macfdb_change(h
, len
, ns_id
);
2418 if (ndm
->ndm_type
!= RTN_UNICAST
)
2421 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2422 return netlink_ipneigh_change(h
, len
, ns_id
);
2425 "Invalid address family: %d received from kernel neighbor change: %d",
2426 ndm
->ndm_family
, h
->nlmsg_type
);
2433 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2434 struct ethaddr
*mac
, uint8_t flags
,
2435 uint16_t state
, int cmd
)
2444 struct zebra_ns
*zns
;
2445 char buf
[INET6_ADDRSTRLEN
];
2446 char buf2
[ETHER_ADDR_STRLEN
];
2447 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2450 memset(&req
, 0, sizeof(req
));
2452 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2453 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2454 if (cmd
== RTM_NEWNEIGH
)
2455 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2456 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2457 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2458 req
.ndm
.ndm_state
= state
;
2459 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2460 req
.ndm
.ndm_type
= RTN_UNICAST
;
2461 req
.ndm
.ndm_flags
= flags
;
2463 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2464 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2466 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2468 if (IS_ZEBRA_DEBUG_KERNEL
)
2469 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2470 nl_msg_type_to_str(cmd
),
2471 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2472 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2473 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2476 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2480 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2481 struct in_addr vtep_ip
, uint8_t sticky
)
2483 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2487 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2488 struct in_addr vtep_ip
, int local
)
2490 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2494 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2495 struct ethaddr
*mac
, uint8_t flags
)
2497 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2498 NUD_NOARP
, RTM_NEWNEIGH
);
2501 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2503 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2507 * MPLS label forwarding table change via netlink interface.
2509 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2512 zebra_nhlfe_t
*nhlfe
;
2513 struct nexthop
*nexthop
= NULL
;
2514 unsigned int nexthop_num
;
2515 const char *routedesc
;
2516 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2522 char buf
[NL_PKT_BUF_SIZE
];
2525 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2528 * Count # nexthops so we can decide whether to use singlepath
2529 * or multipath case.
2532 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2533 nexthop
= nhlfe
->nexthop
;
2536 if (cmd
== RTM_NEWROUTE
) {
2537 /* Count all selected NHLFEs */
2538 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2539 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2543 /* Count all installed NHLFEs */
2544 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2545 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2550 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2553 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2554 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2555 req
.n
.nlmsg_type
= cmd
;
2556 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2558 req
.r
.rtm_family
= AF_MPLS
;
2559 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2560 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2561 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2562 req
.r
.rtm_type
= RTN_UNICAST
;
2564 if (cmd
== RTM_NEWROUTE
) {
2565 /* We do a replace to handle update. */
2566 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2568 /* set the protocol value if installing */
2569 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2570 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2573 /* Fill destination */
2574 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2575 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2577 /* Fill nexthops (paths) based on single-path or multipath. The paths
2578 * chosen depend on the operation.
2580 if (nexthop_num
== 1 || multipath_num
== 1) {
2581 routedesc
= "single-path";
2582 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2585 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2586 nexthop
= nhlfe
->nexthop
;
2590 if ((cmd
== RTM_NEWROUTE
2591 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2592 && CHECK_FLAG(nexthop
->flags
,
2593 NEXTHOP_FLAG_ACTIVE
)))
2594 || (cmd
== RTM_DELROUTE
2595 && (CHECK_FLAG(nhlfe
->flags
,
2596 NHLFE_FLAG_INSTALLED
)
2597 && CHECK_FLAG(nexthop
->flags
,
2598 NEXTHOP_FLAG_FIB
)))) {
2599 /* Add the gateway */
2600 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2607 } else /* Multipath case */
2609 char buf
[NL_PKT_BUF_SIZE
];
2610 struct rtattr
*rta
= (void *)buf
;
2611 struct rtnexthop
*rtnh
;
2612 union g_addr
*src1
= NULL
;
2614 rta
->rta_type
= RTA_MULTIPATH
;
2615 rta
->rta_len
= RTA_LENGTH(0);
2616 rtnh
= RTA_DATA(rta
);
2618 routedesc
= "multipath";
2619 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2622 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2623 nexthop
= nhlfe
->nexthop
;
2627 if (nexthop_num
>= multipath_num
)
2630 if ((cmd
== RTM_NEWROUTE
2631 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2632 && CHECK_FLAG(nexthop
->flags
,
2633 NEXTHOP_FLAG_ACTIVE
)))
2634 || (cmd
== RTM_DELROUTE
2635 && (CHECK_FLAG(nhlfe
->flags
,
2636 NHLFE_FLAG_INSTALLED
)
2637 && CHECK_FLAG(nexthop
->flags
,
2638 NEXTHOP_FLAG_FIB
)))) {
2641 /* Build the multipath */
2642 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2645 rtnh
= RTNH_NEXT(rtnh
);
2649 /* Add the multipath */
2650 if (rta
->rta_len
> RTA_LENGTH(0))
2651 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2652 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2655 /* Talk to netlink socket. */
2656 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2659 #endif /* HAVE_NETLINK */