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
);
250 * @parse_encap_mpls() - Parses encapsulated mpls attributes
251 * @tb: Pointer to rtattr to look for nested items in.
252 * @labels: Pointer to store labels in.
254 * Return: Number of mpls labels found.
256 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
258 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
259 mpls_lse_t
*lses
= NULL
;
264 mpls_label_t label
= 0;
266 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
267 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
268 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
269 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
270 labels
[num_labels
++] = label
;
276 /* Looking up routing table by netlink interface. */
277 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
282 struct rtattr
*tb
[RTA_MAX
+ 1];
285 struct prefix_ipv6 src_p
= {};
288 char anyaddr
[16] = {0};
290 int proto
= ZEBRA_ROUTE_KERNEL
;
295 uint8_t distance
= 0;
300 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
301 void *src
= NULL
; /* IPv6 srcdest source prefix */
302 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
305 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
310 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
312 switch (rtm
->rtm_type
) {
316 bh_type
= BLACKHOLE_NULL
;
318 case RTN_UNREACHABLE
:
319 bh_type
= BLACKHOLE_REJECT
;
322 bh_type
= BLACKHOLE_ADMINPROHIB
;
328 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
330 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
331 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
332 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
336 memset(tb
, 0, sizeof tb
);
337 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
339 if (rtm
->rtm_flags
& RTM_F_CLONED
)
341 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
343 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
346 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
347 && h
->nlmsg_type
== RTM_NEWROUTE
) {
348 if (IS_ZEBRA_DEBUG_KERNEL
)
349 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
354 /* We don't care about change notifications for the MPLS table. */
355 /* TODO: Revisit this. */
356 if (rtm
->rtm_family
== AF_MPLS
)
359 /* Table corresponding to route. */
361 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
363 table
= rtm
->rtm_table
;
366 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
367 if (vrf_id
== VRF_DEFAULT
) {
368 if (!is_zebra_valid_kernel_table(table
)
369 && !is_zebra_main_routing_table(table
))
373 /* Route which inserted by Zebra. */
374 if (is_selfroute(rtm
->rtm_protocol
)) {
375 flags
|= ZEBRA_FLAG_SELFROUTE
;
376 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
379 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
382 dest
= RTA_DATA(tb
[RTA_DST
]);
387 src
= RTA_DATA(tb
[RTA_SRC
]);
392 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
395 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
397 if (tb
[RTA_PRIORITY
])
398 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
400 #if defined(SUPPORT_REALMS)
402 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
405 if (tb
[RTA_METRICS
]) {
406 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
408 memset(mxrta
, 0, sizeof mxrta
);
409 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
410 RTA_PAYLOAD(tb
[RTA_METRICS
]));
413 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
416 if (rtm
->rtm_family
== AF_INET
) {
418 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
420 "Invalid destination prefix length: %u received from kernel route change",
424 memcpy(&p
.u
.prefix4
, dest
, 4);
425 p
.prefixlen
= rtm
->rtm_dst_len
;
427 if (rtm
->rtm_src_len
!= 0) {
428 char buf
[PREFIX_STRLEN
];
429 zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)",
430 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
434 /* Force debug below to not display anything for source */
436 } else if (rtm
->rtm_family
== AF_INET6
) {
438 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
440 "Invalid destination prefix length: %u received from kernel route change",
444 memcpy(&p
.u
.prefix6
, dest
, 16);
445 p
.prefixlen
= rtm
->rtm_dst_len
;
447 src_p
.family
= AF_INET6
;
448 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
450 "Invalid source prefix length: %u received from kernel route change",
454 memcpy(&src_p
.prefix
, src
, 16);
455 src_p
.prefixlen
= rtm
->rtm_src_len
;
459 * For ZEBRA_ROUTE_KERNEL types:
461 * The metric/priority of the route received from the kernel
462 * is a 32 bit number. We are going to interpret the high
463 * order byte as the Admin Distance and the low order 3 bytes
466 * This will allow us to do two things:
467 * 1) Allow the creation of kernel routes that can be
468 * overridden by zebra.
469 * 2) Allow the old behavior for 'most' kernel route types
470 * if a user enters 'ip route ...' v4 routes get a metric
471 * of 0 and v6 routes get a metric of 1024. Both of these
472 * values will end up with a admin distance of 0, which
473 * will cause them to win for the purposes of zebra.
475 if (proto
== ZEBRA_ROUTE_KERNEL
) {
476 distance
= (metric
>> 24) & 0xFF;
477 metric
= (metric
& 0x00FFFFFF);
480 if (IS_ZEBRA_DEBUG_KERNEL
) {
481 char buf
[PREFIX_STRLEN
];
482 char buf2
[PREFIX_STRLEN
];
483 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
484 nl_msg_type_to_str(h
->nlmsg_type
),
485 prefix2str(&p
, buf
, sizeof(buf
)),
486 src_p
.prefixlen
? " from " : "",
488 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
490 vrf_id
, table
, metric
, distance
);
494 if (rtm
->rtm_family
== AF_INET6
)
497 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
498 struct interface
*ifp
;
499 vrf_id_t nh_vrf_id
= vrf_id
;
501 if (!tb
[RTA_MULTIPATH
]) {
503 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
505 memset(&nh
, 0, sizeof(nh
));
507 if (bh_type
== BLACKHOLE_UNSPEC
) {
509 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
510 else if (index
&& gate
)
513 ? NEXTHOP_TYPE_IPV4_IFINDEX
514 : NEXTHOP_TYPE_IPV6_IFINDEX
;
515 else if (!index
&& gate
)
516 nh
.type
= (afi
== AFI_IP
)
520 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
521 nh
.bh_type
= bh_type
;
524 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
525 nh
.bh_type
= bh_type
;
529 memcpy(&nh
.src
, prefsrc
, sz
);
531 memcpy(&nh
.gate
, gate
, sz
);
534 ifp
= if_lookup_by_index_per_ns(
535 zebra_ns_lookup(ns_id
),
538 nh_vrf_id
= ifp
->vrf_id
;
540 nh
.vrf_id
= nh_vrf_id
;
542 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
543 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
544 == LWTUNNEL_ENCAP_MPLS
) {
546 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
550 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
553 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
554 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
556 /* This is a multipath route */
558 struct route_entry
*re
;
559 struct rtnexthop
*rtnh
=
560 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
562 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
564 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
566 re
->distance
= distance
;
573 re
->uptime
= time(NULL
);
577 struct nexthop
*nh
= NULL
;
579 if (len
< (int)sizeof(*rtnh
)
580 || rtnh
->rtnh_len
> len
)
583 index
= rtnh
->rtnh_ifindex
;
586 * Yes we are looking this up
587 * for every nexthop and just
588 * using the last one looked
591 ifp
= if_lookup_by_index_per_ns(
592 zebra_ns_lookup(ns_id
),
595 nh_vrf_id
= ifp
->vrf_id
;
598 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
601 nh_vrf_id
= VRF_DEFAULT
;
607 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
608 memset(tb
, 0, sizeof(tb
));
609 netlink_parse_rtattr(
610 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
611 rtnh
->rtnh_len
- sizeof(*rtnh
));
615 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
616 && *(uint16_t *)RTA_DATA(
618 == LWTUNNEL_ENCAP_MPLS
) {
619 num_labels
= parse_encap_mpls(
620 tb
[RTA_ENCAP
], labels
);
625 if (rtm
->rtm_family
== AF_INET
) {
627 nh
= route_entry_nexthop_ipv4_ifindex_add(
632 nh
= route_entry_nexthop_ipv4_add(
636 } else if (rtm
->rtm_family
639 nh
= route_entry_nexthop_ipv6_ifindex_add(
643 nh
= route_entry_nexthop_ipv6_add(
648 nh
= route_entry_nexthop_ifindex_add(
649 re
, index
, nh_vrf_id
);
651 if (nh
&& num_labels
)
652 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
655 if (rtnh
->rtnh_len
== 0)
658 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
659 rtnh
= RTNH_NEXT(rtnh
);
662 zserv_nexthop_num_warn(__func__
,
663 (const struct prefix
*)&p
,
665 if (re
->nexthop_num
== 0)
668 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
672 if (!tb
[RTA_MULTIPATH
]) {
674 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
676 memset(&nh
, 0, sizeof(nh
));
677 if (bh_type
== BLACKHOLE_UNSPEC
) {
679 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
680 else if (index
&& gate
)
683 ? NEXTHOP_TYPE_IPV4_IFINDEX
684 : NEXTHOP_TYPE_IPV6_IFINDEX
;
685 else if (!index
&& gate
)
686 nh
.type
= (afi
== AFI_IP
)
690 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
691 nh
.bh_type
= BLACKHOLE_UNSPEC
;
694 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
695 nh
.bh_type
= bh_type
;
699 memcpy(&nh
.gate
, gate
, sz
);
700 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
701 &p
, &src_p
, &nh
, table
, metric
, distance
,
704 /* XXX: need to compare the entire list of nexthops
705 * here for NLM_F_APPEND stupidity */
706 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
707 &p
, &src_p
, NULL
, table
, metric
, distance
,
715 static struct mcast_route_data
*mroute
= NULL
;
717 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
718 ns_id_t ns_id
, int startup
)
722 struct rtattr
*tb
[RTA_MAX
+ 1];
723 struct mcast_route_data
*m
;
724 struct mcast_route_data mr
;
731 char oif_list
[256] = "\0";
738 memset(&mr
, 0, sizeof(mr
));
744 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
746 memset(tb
, 0, sizeof tb
);
747 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
750 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
752 table
= rtm
->rtm_table
;
754 vrf
= vrf_lookup_by_table(table
, ns_id
);
757 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
760 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
763 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
765 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
766 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
768 if (tb
[RTA_MULTIPATH
]) {
769 struct rtnexthop
*rtnh
=
770 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
772 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
774 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
777 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
780 if (rtnh
->rtnh_len
== 0)
783 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
784 rtnh
= RTNH_NEXT(rtnh
);
788 if (IS_ZEBRA_DEBUG_KERNEL
) {
789 struct interface
*ifp
;
790 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
791 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
792 for (count
= 0; count
< oif_count
; count
++) {
793 ifp
= if_lookup_by_index(oif
[count
], vrf
);
796 sprintf(temp
, "%s ", ifp
->name
);
797 strcat(oif_list
, temp
);
799 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
800 ifp
= if_lookup_by_index(iif
, vrf
);
802 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
803 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
804 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
809 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
816 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
817 /* If this is not route add/delete message print warning. */
818 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
822 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
)) {
824 "Invalid address family: %u received from kernel route change: %u",
825 rtm
->rtm_family
, h
->nlmsg_type
);
829 /* Connected route. */
830 if (IS_ZEBRA_DEBUG_KERNEL
)
831 zlog_debug("%s %s %s proto %s NS %u",
832 nl_msg_type_to_str(h
->nlmsg_type
),
833 nl_family_to_str(rtm
->rtm_family
),
834 nl_rttype_to_str(rtm
->rtm_type
),
835 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
838 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
840 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
843 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
847 if (rtm
->rtm_type
== RTN_MULTICAST
)
848 netlink_route_change_read_multicast(h
, ns_id
, startup
);
850 netlink_route_change_read_unicast(h
, ns_id
, startup
);
854 /* Request for specific route information from the kernel */
855 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
862 /* Form the request, specifying filter (rtattr) if needed. */
863 memset(&req
, 0, sizeof(req
));
864 req
.n
.nlmsg_type
= type
;
865 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
866 req
.rtm
.rtm_family
= family
;
868 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
871 /* Routing table read function using netlink interface. Only called
873 int netlink_route_read(struct zebra_ns
*zns
)
877 /* Get IPv4 routing table. */
878 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
881 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
882 &zns
->netlink_cmd
, zns
, 0, 1);
886 /* Get IPv6 routing table. */
887 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
890 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
891 &zns
->netlink_cmd
, zns
, 0, 1);
898 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
900 struct nlmsghdr
*nlmsg
,
901 size_t req_size
, int bytelen
,
902 struct nexthop
*nexthop
)
904 if (route_family
== AF_MPLS
) {
905 struct gw_family_t gw_fam
;
907 gw_fam
.family
= gw_family
;
908 if (gw_family
== AF_INET
)
909 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
911 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
912 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
915 if (gw_family
== AF_INET
)
916 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
917 &nexthop
->gate
.ipv4
, bytelen
);
919 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
920 &nexthop
->gate
.ipv6
, bytelen
);
924 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
927 struct rtnexthop
*rtnh
,
928 size_t req_size
, int bytelen
,
929 struct nexthop
*nexthop
)
931 if (route_family
== AF_MPLS
) {
932 struct gw_family_t gw_fam
;
934 gw_fam
.family
= gw_family
;
935 if (gw_family
== AF_INET
)
936 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
938 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
939 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
941 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
943 if (gw_family
== AF_INET
)
944 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
945 &nexthop
->gate
.ipv4
, bytelen
);
947 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
948 &nexthop
->gate
.ipv6
, bytelen
);
949 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
953 /* This function takes a nexthop as argument and adds
954 * the appropriate netlink attributes to an existing
957 * @param routedesc: Human readable description of route type
958 * (direct/recursive, single-/multipath)
959 * @param bytelen: Length of addresses in bytes.
960 * @param nexthop: Nexthop information
961 * @param nlmsg: nlmsghdr structure to fill in.
962 * @param req_size: The size allocated for the message.
964 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
965 struct nexthop
*nexthop
,
966 struct nlmsghdr
*nlmsg
,
968 size_t req_size
, int cmd
)
970 struct mpls_label_stack
*nh_label
;
971 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
976 * label_buf is *only* currently used within debugging.
977 * As such when we assign it we are guarding it inside
978 * a debug test. If you want to change this make sure
979 * you fix this assumption
984 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
987 nh_label
= nh
->nh_label
;
988 if (!nh_label
|| !nh_label
->num_labels
)
991 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
992 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
995 if (IS_ZEBRA_DEBUG_KERNEL
) {
997 sprintf(label_buf
, "label %u",
1000 sprintf(label_buf1
, "/%u",
1001 nh_label
->label
[i
]);
1002 strlcat(label_buf
, label_buf1
,
1007 out_lse
[num_labels
] =
1008 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1014 /* Set the BoS bit */
1015 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1017 if (rtmsg
->rtm_family
== AF_MPLS
)
1018 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1019 num_labels
* sizeof(mpls_lse_t
));
1021 struct rtattr
*nest
;
1022 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1024 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1026 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1027 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1028 num_labels
* sizeof(mpls_lse_t
));
1029 addattr_nest_end(nlmsg
, nest
);
1033 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1034 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1036 if (rtmsg
->rtm_family
== AF_INET
1037 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1038 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1039 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1040 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1041 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1043 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1044 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1045 &nexthop
->rmap_src
.ipv4
, bytelen
);
1046 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1047 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1048 &nexthop
->src
.ipv4
, bytelen
);
1050 if (IS_ZEBRA_DEBUG_KERNEL
)
1052 " 5549: _netlink_route_build_singlepath() (%s): "
1053 "nexthop via %s %s if %u(%u)",
1054 routedesc
, ipv4_ll_buf
, label_buf
,
1055 nexthop
->ifindex
, nexthop
->vrf_id
);
1059 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1060 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1061 /* Send deletes to the kernel without specifying the next-hop */
1062 if (cmd
!= RTM_DELROUTE
)
1063 _netlink_route_nl_add_gateway_info(
1064 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
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 %s %s if %u(%u)",
1080 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1081 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1084 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1085 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1086 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1087 nlmsg
, req_size
, bytelen
,
1090 if (cmd
== RTM_NEWROUTE
) {
1091 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1092 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1093 &nexthop
->rmap_src
.ipv6
, bytelen
);
1094 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1095 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1096 &nexthop
->src
.ipv6
, bytelen
);
1099 if (IS_ZEBRA_DEBUG_KERNEL
)
1101 "netlink_route_multipath() (%s): "
1102 "nexthop via %s %s if %u(%u)",
1103 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1104 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1108 * We have the ifindex so we should always send it
1109 * This is especially useful if we are doing route
1112 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1113 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1115 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1116 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1117 if (cmd
== RTM_NEWROUTE
) {
1118 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1119 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1120 &nexthop
->rmap_src
.ipv4
, bytelen
);
1121 else if (nexthop
->src
.ipv4
.s_addr
)
1122 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1123 &nexthop
->src
.ipv4
, bytelen
);
1126 if (IS_ZEBRA_DEBUG_KERNEL
)
1128 "netlink_route_multipath() (%s): "
1129 "nexthop via if %u(%u)",
1130 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1133 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1134 if (cmd
== RTM_NEWROUTE
) {
1135 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1136 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1137 &nexthop
->rmap_src
.ipv6
, bytelen
);
1138 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1139 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1140 &nexthop
->src
.ipv6
, bytelen
);
1143 if (IS_ZEBRA_DEBUG_KERNEL
)
1145 "netlink_route_multipath() (%s): "
1146 "nexthop via if %u(%u)",
1147 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1151 /* This function takes a nexthop as argument and
1152 * appends to the given rtattr/rtnexthop pair the
1153 * representation of the nexthop. If the nexthop
1154 * defines a preferred source, the src parameter
1155 * will be modified to point to that src, otherwise
1156 * it will be kept unmodified.
1158 * @param routedesc: Human readable description of route type
1159 * (direct/recursive, single-/multipath)
1160 * @param bytelen: Length of addresses in bytes.
1161 * @param nexthop: Nexthop information
1162 * @param rta: rtnetlink attribute structure
1163 * @param rtnh: pointer to an rtnetlink nexthop structure
1164 * @param src: pointer pointing to a location where
1165 * the prefsrc should be stored.
1167 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1168 struct nexthop
*nexthop
,
1170 struct rtnexthop
*rtnh
,
1171 struct rtmsg
*rtmsg
,
1174 struct mpls_label_stack
*nh_label
;
1175 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1177 char label_buf
[256];
1179 rtnh
->rtnh_len
= sizeof(*rtnh
);
1180 rtnh
->rtnh_flags
= 0;
1181 rtnh
->rtnh_hops
= 0;
1182 rta
->rta_len
+= rtnh
->rtnh_len
;
1185 * label_buf is *only* currently used within debugging.
1186 * As such when we assign it we are guarding it inside
1187 * a debug test. If you want to change this make sure
1188 * you fix this assumption
1190 label_buf
[0] = '\0';
1193 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1194 char label_buf1
[20];
1196 nh_label
= nh
->nh_label
;
1197 if (!nh_label
|| !nh_label
->num_labels
)
1200 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1201 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1204 if (IS_ZEBRA_DEBUG_KERNEL
) {
1206 sprintf(label_buf
, "label %u",
1207 nh_label
->label
[i
]);
1209 sprintf(label_buf1
, "/%u",
1210 nh_label
->label
[i
]);
1211 strlcat(label_buf
, label_buf1
,
1216 out_lse
[num_labels
] =
1217 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1223 /* Set the BoS bit */
1224 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1226 if (rtmsg
->rtm_family
== AF_MPLS
) {
1227 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1229 num_labels
* sizeof(mpls_lse_t
));
1231 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1233 struct rtattr
*nest
;
1234 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1235 int len
= rta
->rta_len
;
1237 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1238 &encap
, sizeof(uint16_t));
1239 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1240 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1242 num_labels
* sizeof(mpls_lse_t
));
1243 rta_nest_end(rta
, nest
);
1244 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1248 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1249 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1251 if (rtmsg
->rtm_family
== AF_INET
1252 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1253 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1255 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1256 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1258 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1259 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1261 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1262 *src
= &nexthop
->rmap_src
;
1263 else if (nexthop
->src
.ipv4
.s_addr
)
1264 *src
= &nexthop
->src
;
1266 if (IS_ZEBRA_DEBUG_KERNEL
)
1268 " 5549: netlink_route_build_multipath() (%s): "
1269 "nexthop via %s %s if %u",
1270 routedesc
, ipv4_ll_buf
, label_buf
,
1275 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1276 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1277 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1278 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1280 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1281 *src
= &nexthop
->rmap_src
;
1282 else if (nexthop
->src
.ipv4
.s_addr
)
1283 *src
= &nexthop
->src
;
1285 if (IS_ZEBRA_DEBUG_KERNEL
)
1287 "netlink_route_multipath() (%s): "
1288 "nexthop via %s %s if %u",
1289 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1290 label_buf
, nexthop
->ifindex
);
1292 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1293 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1294 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1295 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1298 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1299 *src
= &nexthop
->rmap_src
;
1300 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1301 *src
= &nexthop
->src
;
1303 if (IS_ZEBRA_DEBUG_KERNEL
)
1305 "netlink_route_multipath() (%s): "
1306 "nexthop via %s %s if %u",
1307 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1308 label_buf
, nexthop
->ifindex
);
1312 * We have figured out the ifindex so we should always send it
1313 * This is especially useful if we are doing route
1316 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1317 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1320 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1321 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1322 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1323 *src
= &nexthop
->rmap_src
;
1324 else if (nexthop
->src
.ipv4
.s_addr
)
1325 *src
= &nexthop
->src
;
1327 if (IS_ZEBRA_DEBUG_KERNEL
)
1329 "netlink_route_multipath() (%s): "
1330 "nexthop via if %u",
1331 routedesc
, nexthop
->ifindex
);
1335 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1336 zebra_nhlfe_t
*nhlfe
,
1337 struct nlmsghdr
*nlmsg
,
1338 struct rtmsg
*rtmsg
,
1339 size_t req_size
, int cmd
)
1344 family
= NHLFE_FAMILY(nhlfe
);
1345 bytelen
= (family
== AF_INET
? 4 : 16);
1346 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1347 nlmsg
, rtmsg
, req_size
, cmd
);
1352 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1353 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1354 struct rtmsg
*rtmsg
, union g_addr
**src
)
1359 family
= NHLFE_FAMILY(nhlfe
);
1360 bytelen
= (family
== AF_INET
? 4 : 16);
1361 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1366 /* Log debug information for netlink_route_multipath
1367 * if debug logging is enabled.
1369 * @param cmd: Netlink command which is to be processed
1370 * @param p: Prefix for which the change is due
1371 * @param family: Address family which the change concerns
1372 * @param zvrf: The vrf we are in
1373 * @param tableid: The table we are working on
1375 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1376 int family
, vrf_id_t vrfid
,
1379 if (IS_ZEBRA_DEBUG_KERNEL
) {
1380 char buf
[PREFIX_STRLEN
];
1382 "netlink_route_multipath(): %s %s vrf %u(%u)",
1383 nl_msg_type_to_str(cmd
),
1384 prefix2str(p
, buf
, sizeof(buf
)),
1389 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1391 if (IS_ZEBRA_DEBUG_KERNEL
)
1392 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1393 nl_msg_type_to_str(cmd
), label
);
1396 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1397 int llalen
, ns_id_t ns_id
)
1405 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1407 memset(&req
, 0, sizeof(req
));
1409 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1410 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1411 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1412 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1414 req
.ndm
.ndm_family
= AF_INET
;
1415 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1416 req
.ndm
.ndm_ifindex
= ifindex
;
1417 req
.ndm
.ndm_type
= RTN_UNICAST
;
1419 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1420 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1422 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1426 /* Routing table change via netlink interface. */
1427 /* Update flag indicates whether this is a "replace" or not. */
1428 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1429 const struct prefix
*src_p
,
1430 struct route_entry
*re
,
1434 struct sockaddr_nl snl
;
1435 struct nexthop
*nexthop
= NULL
;
1436 unsigned int nexthop_num
;
1437 int family
= PREFIX_FAMILY(p
);
1438 const char *routedesc
;
1445 char buf
[NL_PKT_BUF_SIZE
];
1448 struct zebra_ns
*zns
;
1449 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1452 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1454 bytelen
= (family
== AF_INET
? 4 : 16);
1456 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1457 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1458 if ((cmd
== RTM_NEWROUTE
) && update
)
1459 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1460 req
.n
.nlmsg_type
= cmd
;
1461 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1463 req
.r
.rtm_family
= family
;
1464 req
.r
.rtm_dst_len
= p
->prefixlen
;
1465 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1466 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1467 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1470 * blackhole routes are not RTN_UNICAST, they are
1471 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1472 * so setting this value as a RTN_UNICAST would
1473 * cause the route lookup of just the prefix
1474 * to fail. So no need to specify this for
1475 * the RTM_DELROUTE case
1477 if (cmd
!= RTM_DELROUTE
)
1478 req
.r
.rtm_type
= RTN_UNICAST
;
1480 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1482 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1486 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1488 * either by the kernel or by zebra. Its purely for calculating best
1490 * by the routing protocol and for communicating with protocol peers.
1492 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1493 #if defined(SUPPORT_REALMS)
1494 if (re
->tag
> 0 && re
->tag
<= 255)
1495 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1497 /* Table corresponding to this route. */
1498 if (re
->table
< 256)
1499 req
.r
.rtm_table
= re
->table
;
1501 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1502 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1505 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1508 * If we are not updating the route and we have received
1509 * a route delete, then all we need to fill in is the
1510 * prefix information to tell the kernel to schwack
1513 if (!update
&& cmd
== RTM_DELROUTE
)
1516 if (re
->mtu
|| re
->nexthop_mtu
) {
1517 char buf
[NL_PKT_BUF_SIZE
];
1518 struct rtattr
*rta
= (void *)buf
;
1519 uint32_t mtu
= re
->mtu
;
1520 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1521 mtu
= re
->nexthop_mtu
;
1522 rta
->rta_type
= RTA_METRICS
;
1523 rta
->rta_len
= RTA_LENGTH(0);
1524 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1525 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1529 /* Count overall nexthops so we can decide whether to use singlepath
1530 * or multipath case. */
1532 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1533 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1535 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1537 if (cmd
== RTM_DELROUTE
1538 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1544 /* Singlepath case. */
1545 if (nexthop_num
== 1 || multipath_num
== 1) {
1547 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1549 * So we want to cover 2 types of blackhole
1551 * 1) A normal blackhole route( ala from a static
1553 * 2) A recursively resolved blackhole route
1555 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1556 switch (nexthop
->bh_type
) {
1557 case BLACKHOLE_ADMINPROHIB
:
1558 req
.r
.rtm_type
= RTN_PROHIBIT
;
1560 case BLACKHOLE_REJECT
:
1561 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1564 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1569 if (CHECK_FLAG(nexthop
->flags
,
1570 NEXTHOP_FLAG_RECURSIVE
)) {
1572 if (family
== AF_INET
) {
1573 if (nexthop
->rmap_src
.ipv4
1580 } else if (nexthop
->src
.ipv4
1588 } else if (family
== AF_INET6
) {
1589 if (!IN6_IS_ADDR_UNSPECIFIED(
1597 !IN6_IS_ADDR_UNSPECIFIED(
1610 if ((cmd
== RTM_NEWROUTE
1611 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1612 || (cmd
== RTM_DELROUTE
1613 && CHECK_FLAG(nexthop
->flags
,
1614 NEXTHOP_FLAG_FIB
))) {
1615 routedesc
= nexthop
->rparent
1616 ? "recursive, single-path"
1619 _netlink_route_build_singlepath(
1620 routedesc
, bytelen
, nexthop
, &req
.n
,
1621 &req
.r
, sizeof req
, cmd
);
1626 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1627 if (family
== AF_INET
)
1628 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1629 &src
.ipv4
, bytelen
);
1630 else if (family
== AF_INET6
)
1631 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1632 &src
.ipv6
, bytelen
);
1635 char buf
[NL_PKT_BUF_SIZE
];
1636 struct rtattr
*rta
= (void *)buf
;
1637 struct rtnexthop
*rtnh
;
1638 union g_addr
*src1
= NULL
;
1640 rta
->rta_type
= RTA_MULTIPATH
;
1641 rta
->rta_len
= RTA_LENGTH(0);
1642 rtnh
= RTA_DATA(rta
);
1645 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1646 if (nexthop_num
>= multipath_num
)
1649 if (CHECK_FLAG(nexthop
->flags
,
1650 NEXTHOP_FLAG_RECURSIVE
)) {
1651 /* This only works for IPv4 now */
1653 if (family
== AF_INET
) {
1654 if (nexthop
->rmap_src
.ipv4
1661 } else if (nexthop
->src
.ipv4
1669 } else if (family
== AF_INET6
) {
1670 if (!IN6_IS_ADDR_UNSPECIFIED(
1678 !IN6_IS_ADDR_UNSPECIFIED(
1691 if ((cmd
== RTM_NEWROUTE
1692 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1693 || (cmd
== RTM_DELROUTE
1694 && CHECK_FLAG(nexthop
->flags
,
1695 NEXTHOP_FLAG_FIB
))) {
1696 routedesc
= nexthop
->rparent
1697 ? "recursive, multipath"
1701 _netlink_route_build_multipath(
1702 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1704 rtnh
= RTNH_NEXT(rtnh
);
1706 if (!setsrc
&& src1
) {
1707 if (family
== AF_INET
)
1708 src
.ipv4
= src1
->ipv4
;
1709 else if (family
== AF_INET6
)
1710 src
.ipv6
= src1
->ipv6
;
1716 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1717 if (family
== AF_INET
)
1718 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1719 &src
.ipv4
, bytelen
);
1720 else if (family
== AF_INET6
)
1721 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1722 &src
.ipv6
, bytelen
);
1723 if (IS_ZEBRA_DEBUG_KERNEL
)
1724 zlog_debug("Setting source");
1727 if (rta
->rta_len
> RTA_LENGTH(0))
1728 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1729 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1732 /* If there is no useful nexthop then return. */
1733 if (nexthop_num
== 0) {
1734 if (IS_ZEBRA_DEBUG_KERNEL
)
1736 "netlink_route_multipath(): No useful nexthop.");
1742 /* Destination netlink address. */
1743 memset(&snl
, 0, sizeof snl
);
1744 snl
.nl_family
= AF_NETLINK
;
1746 /* Talk to netlink socket. */
1747 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1751 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1754 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1762 struct zebra_ns
*zns
;
1765 memset(&req
, 0, sizeof(req
));
1767 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1768 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1769 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1771 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1772 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1774 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1775 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1776 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1777 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1778 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1780 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1781 &zns
->netlink_cmd
, zns
, 0);
1787 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1788 const struct prefix
*p
,
1789 const struct prefix
*src_p
,
1790 struct route_entry
*old
,
1791 struct route_entry
*new)
1798 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1799 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1800 new, (old
) ? 1 : 0);
1803 * So v6 route replace semantics are not in
1804 * the kernel at this point as I understand it.
1805 * So let's do a delete than an add.
1806 * In the future once v6 route replace semantics
1807 * are in we can figure out what to do here to
1808 * allow working with old and new kernels.
1810 * I'm also intentionally ignoring the failure case
1811 * of the route delete. If that happens yeah we're
1815 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1817 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1820 kernel_route_rib_pass_fail(rn
, p
, new,
1821 (!ret
) ? DP_INSTALL_SUCCESS
1822 : DP_INSTALL_FAILURE
);
1823 return DP_REQUEST_SUCCESS
;
1827 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1829 kernel_route_rib_pass_fail(rn
, p
, old
,
1830 (!ret
) ? DP_DELETE_SUCCESS
1831 : DP_DELETE_FAILURE
);
1834 return DP_REQUEST_SUCCESS
;
1837 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1838 int llalen
, ns_id_t ns_id
)
1840 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1841 addr
, lla
, llalen
, ns_id
);
1845 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1846 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1848 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1849 struct in_addr
*vtep_ip
, int cmd
)
1851 struct zebra_ns
*zns
;
1857 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1858 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1861 memset(&req
, 0, sizeof(req
));
1863 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1864 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1865 if (cmd
== RTM_NEWNEIGH
)
1866 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1867 req
.n
.nlmsg_type
= cmd
;
1868 req
.ndm
.ndm_family
= PF_BRIDGE
;
1869 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1870 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1873 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1874 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1875 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1877 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1882 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1884 * a "flood" MAC FDB entry.
1886 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1888 if (IS_ZEBRA_DEBUG_VXLAN
)
1889 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1890 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1892 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1896 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1897 * deleting the "flood" MAC FDB entry.
1899 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1901 if (IS_ZEBRA_DEBUG_VXLAN
)
1903 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1904 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1906 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1910 #define NDA_RTA(r) \
1911 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1914 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1917 struct interface
*ifp
;
1918 struct zebra_if
*zif
;
1919 struct rtattr
*tb
[NDA_MAX
+ 1];
1920 struct interface
*br_if
;
1923 struct prefix vtep_ip
;
1924 int vid_present
= 0, dst_present
= 0;
1925 char buf
[ETHER_ADDR_STRLEN
];
1930 ndm
= NLMSG_DATA(h
);
1932 /* We only process macfdb notifications if EVPN is enabled */
1933 if (!is_evpn_enabled())
1936 /* The interface should exist. */
1937 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1939 if (!ifp
|| !ifp
->info
)
1942 /* The interface should be something we're interested in. */
1943 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1946 /* Drop "permanent" entries. */
1947 if (ndm
->ndm_state
& NUD_PERMANENT
)
1950 zif
= (struct zebra_if
*)ifp
->info
;
1951 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1952 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1953 nl_msg_type_to_str(h
->nlmsg_type
),
1954 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1955 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1959 /* Parse attributes and extract fields of interest. */
1960 memset(tb
, 0, sizeof tb
);
1961 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1963 if (!tb
[NDA_LLADDR
]) {
1964 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1965 nl_msg_type_to_str(h
->nlmsg_type
),
1966 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1967 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1971 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1973 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1974 nl_msg_type_to_str(h
->nlmsg_type
),
1975 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1976 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1977 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1981 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1983 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1985 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1986 sprintf(vid_buf
, " VLAN %u", vid
);
1990 /* TODO: Only IPv4 supported now. */
1992 vtep_ip
.family
= AF_INET
;
1993 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1994 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1996 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1999 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
2001 if (IS_ZEBRA_DEBUG_KERNEL
)
2002 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2003 nl_msg_type_to_str(h
->nlmsg_type
),
2004 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2005 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2006 sticky
? "sticky " : "",
2007 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2008 dst_present
? dst_buf
: "");
2010 if (filter_vlan
&& vid
!= filter_vlan
)
2013 /* If add or update, do accordingly if learnt on a "local" interface; if
2014 * the notification is over VxLAN, this has to be related to
2016 * so perform an implicit delete of any local entry (if it exists).
2018 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2019 /* Drop "permanent" entries. */
2020 if (ndm
->ndm_state
& NUD_PERMANENT
)
2023 if (IS_ZEBRA_IF_VXLAN(ifp
))
2024 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2027 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2031 /* This is a delete notification.
2032 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2033 * 2. For a MAC over "local" interface, delete the mac
2034 * Note: We will get notifications from both bridge driver and VxLAN
2036 * Ignore the notification from VxLan driver as it is also generated
2037 * when mac moves from remote to local.
2042 if (IS_ZEBRA_IF_VXLAN(ifp
))
2043 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2046 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2049 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2054 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2057 /* Length validity. */
2058 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2062 /* We are interested only in AF_BRIDGE notifications. */
2063 ndm
= NLMSG_DATA(h
);
2064 if (ndm
->ndm_family
!= AF_BRIDGE
)
2067 return netlink_macfdb_change(h
, len
, ns_id
);
2070 /* Request for MAC FDB information from the kernel */
2071 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2072 ifindex_t master_ifindex
)
2076 struct ifinfomsg ifm
;
2080 /* Form the request, specifying filter (rtattr) if needed. */
2081 memset(&req
, 0, sizeof(req
));
2082 req
.n
.nlmsg_type
= type
;
2083 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2084 req
.ifm
.ifi_family
= family
;
2086 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2088 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2092 * MAC forwarding database read using netlink interface. This is invoked
2095 int netlink_macfdb_read(struct zebra_ns
*zns
)
2099 /* Get bridge FDB table. */
2100 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2103 /* We are reading entire table. */
2105 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2112 * MAC forwarding database read using netlink interface. This is for a
2113 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2115 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2116 struct interface
*br_if
)
2118 struct zebra_if
*br_zif
;
2119 struct zebra_if
*zif
;
2120 struct zebra_l2info_vxlan
*vxl
;
2124 /* Save VLAN we're filtering on, if needed. */
2125 br_zif
= (struct zebra_if
*)br_if
->info
;
2126 zif
= (struct zebra_if
*)ifp
->info
;
2127 vxl
= &zif
->l2info
.vxl
;
2128 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2129 filter_vlan
= vxl
->access_vlan
;
2131 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2133 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2137 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2140 /* Reset VLAN filter. */
2145 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2146 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2147 int local
, int cmd
, uint8_t sticky
)
2149 struct zebra_ns
*zns
;
2156 struct zebra_if
*zif
;
2157 struct interface
*br_if
;
2158 struct zebra_if
*br_zif
;
2159 char buf
[ETHER_ADDR_STRLEN
];
2160 int vid_present
= 0, dst_present
= 0;
2163 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2167 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2168 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2169 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2174 memset(&req
, 0, sizeof(req
));
2176 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2177 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2178 if (cmd
== RTM_NEWNEIGH
)
2179 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2180 req
.n
.nlmsg_type
= cmd
;
2181 req
.ndm
.ndm_family
= AF_BRIDGE
;
2182 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2183 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2186 req
.ndm
.ndm_state
|= NUD_NOARP
;
2188 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2190 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2191 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2193 dst_alen
= 4; // TODO: hardcoded
2194 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2196 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2198 br_zif
= (struct zebra_if
*)br_if
->info
;
2199 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2200 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2202 sprintf(vid_buf
, " VLAN %u", vid
);
2204 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2206 if (IS_ZEBRA_DEBUG_KERNEL
)
2207 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2208 nl_msg_type_to_str(cmd
),
2209 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2210 ifp
->ifindex
, vid_present
? vid_buf
: "",
2211 sticky
? "sticky " : "",
2212 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2213 dst_present
? dst_buf
: "");
2215 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2220 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2223 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2226 struct interface
*ifp
;
2227 struct zebra_if
*zif
;
2228 struct rtattr
*tb
[NDA_MAX
+ 1];
2229 struct interface
*link_if
;
2232 char buf
[ETHER_ADDR_STRLEN
];
2233 char buf2
[INET6_ADDRSTRLEN
];
2234 int mac_present
= 0;
2235 uint8_t ext_learned
;
2236 uint8_t router_flag
;
2238 ndm
= NLMSG_DATA(h
);
2240 /* The interface should exist. */
2241 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2243 if (!ifp
|| !ifp
->info
)
2246 zif
= (struct zebra_if
*)ifp
->info
;
2248 /* Parse attributes and extract fields of interest. */
2249 memset(tb
, 0, sizeof tb
);
2250 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2253 zlog_warn("%s family %s IF %s(%u) - no DST",
2254 nl_msg_type_to_str(h
->nlmsg_type
),
2255 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2260 memset(&ip
, 0, sizeof(struct ipaddr
));
2261 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2262 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2264 /* Drop some "permanent" entries. */
2265 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2266 char buf
[16] = "169.254.0.1";
2267 struct in_addr ipv4_ll
;
2269 if (ndm
->ndm_family
!= AF_INET
)
2272 if (!zif
->v6_2_v4_ll_neigh_entry
)
2275 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2278 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2279 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2282 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2283 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2287 /* The neighbor is present on an SVI. From this, we locate the
2289 * bridge because we're only interested in neighbors on a VxLAN bridge.
2290 * The bridge is located based on the nature of the SVI:
2291 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2293 * and is linked to the bridge
2294 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2298 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2299 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2303 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2308 memset(&mac
, 0, sizeof(struct ethaddr
));
2309 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2310 if (tb
[NDA_LLADDR
]) {
2311 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2313 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2314 nl_msg_type_to_str(h
->nlmsg_type
),
2315 nl_family_to_str(ndm
->ndm_family
),
2316 ifp
->name
, ndm
->ndm_ifindex
,
2317 (unsigned long)RTA_PAYLOAD(
2323 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2326 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2327 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2329 if (IS_ZEBRA_DEBUG_KERNEL
)
2331 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2332 nl_msg_type_to_str(h
->nlmsg_type
),
2333 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2335 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2337 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2339 ndm
->ndm_state
, ndm
->ndm_flags
);
2341 /* If the neighbor state is valid for use, process as an add or
2343 * else process as a delete. Note that the delete handling may
2345 * in re-adding the neighbor if it is a valid "remote" neighbor.
2347 if (ndm
->ndm_state
& NUD_VALID
)
2348 return zebra_vxlan_handle_kernel_neigh_update(
2349 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2350 ext_learned
, router_flag
);
2352 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2355 if (IS_ZEBRA_DEBUG_KERNEL
)
2356 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2357 nl_msg_type_to_str(h
->nlmsg_type
),
2358 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2360 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2362 /* Process the delete - it may result in re-adding the neighbor if it is
2363 * a valid "remote" neighbor.
2365 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2368 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2373 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2376 /* Length validity. */
2377 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2381 /* We are interested only in AF_INET or AF_INET6 notifications. */
2382 ndm
= NLMSG_DATA(h
);
2383 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2386 return netlink_neigh_change(h
, len
);
2389 /* Request for IP neighbor information from the kernel */
2390 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2399 /* Form the request, specifying filter (rtattr) if needed. */
2400 memset(&req
, 0, sizeof(req
));
2401 req
.n
.nlmsg_type
= type
;
2402 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2403 req
.ndm
.ndm_family
= family
;
2405 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2407 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2411 * IP Neighbor table read using netlink interface. This is invoked
2414 int netlink_neigh_read(struct zebra_ns
*zns
)
2418 /* Get IP neighbor table. */
2419 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2422 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2429 * IP Neighbor table read using netlink interface. This is for a specific
2432 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2436 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2440 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2446 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2451 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2454 /* Length validity. */
2455 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2457 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2458 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2459 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2463 /* Is this a notification for the MAC FDB or IP neighbor table? */
2464 ndm
= NLMSG_DATA(h
);
2465 if (ndm
->ndm_family
== AF_BRIDGE
)
2466 return netlink_macfdb_change(h
, len
, ns_id
);
2468 if (ndm
->ndm_type
!= RTN_UNICAST
)
2471 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2472 return netlink_ipneigh_change(h
, len
, ns_id
);
2475 "Invalid address family: %u received from kernel neighbor change: %u",
2476 ndm
->ndm_family
, h
->nlmsg_type
);
2483 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2484 struct ethaddr
*mac
, uint8_t flags
,
2485 uint16_t state
, int cmd
)
2494 struct zebra_ns
*zns
;
2495 char buf
[INET6_ADDRSTRLEN
];
2496 char buf2
[ETHER_ADDR_STRLEN
];
2497 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2500 memset(&req
, 0, sizeof(req
));
2502 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2503 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2504 if (cmd
== RTM_NEWNEIGH
)
2505 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2506 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2507 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2508 req
.ndm
.ndm_state
= state
;
2509 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2510 req
.ndm
.ndm_type
= RTN_UNICAST
;
2511 req
.ndm
.ndm_flags
= flags
;
2513 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2514 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2516 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2518 if (IS_ZEBRA_DEBUG_KERNEL
)
2519 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2520 nl_msg_type_to_str(cmd
),
2521 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2522 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2523 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2526 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2530 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2531 struct in_addr vtep_ip
, uint8_t sticky
)
2533 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2537 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2538 struct in_addr vtep_ip
, int local
)
2540 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2544 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2545 struct ethaddr
*mac
, uint8_t flags
)
2547 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2548 NUD_NOARP
, RTM_NEWNEIGH
);
2551 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2553 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2557 * MPLS label forwarding table change via netlink interface.
2559 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2562 zebra_nhlfe_t
*nhlfe
;
2563 struct nexthop
*nexthop
= NULL
;
2564 unsigned int nexthop_num
;
2565 const char *routedesc
;
2566 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2572 char buf
[NL_PKT_BUF_SIZE
];
2575 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2578 * Count # nexthops so we can decide whether to use singlepath
2579 * or multipath case.
2582 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2583 nexthop
= nhlfe
->nexthop
;
2586 if (cmd
== RTM_NEWROUTE
) {
2587 /* Count all selected NHLFEs */
2588 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2589 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2593 /* Count all installed NHLFEs */
2594 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2595 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2600 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2603 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2604 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2605 req
.n
.nlmsg_type
= cmd
;
2606 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2608 req
.r
.rtm_family
= AF_MPLS
;
2609 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2610 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2611 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2612 req
.r
.rtm_type
= RTN_UNICAST
;
2614 if (cmd
== RTM_NEWROUTE
) {
2615 /* We do a replace to handle update. */
2616 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2618 /* set the protocol value if installing */
2619 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2620 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2623 /* Fill destination */
2624 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2625 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2627 /* Fill nexthops (paths) based on single-path or multipath. The paths
2628 * chosen depend on the operation.
2630 if (nexthop_num
== 1 || multipath_num
== 1) {
2631 routedesc
= "single-path";
2632 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2635 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2636 nexthop
= nhlfe
->nexthop
;
2640 if ((cmd
== RTM_NEWROUTE
2641 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2642 && CHECK_FLAG(nexthop
->flags
,
2643 NEXTHOP_FLAG_ACTIVE
)))
2644 || (cmd
== RTM_DELROUTE
2645 && (CHECK_FLAG(nhlfe
->flags
,
2646 NHLFE_FLAG_INSTALLED
)
2647 && CHECK_FLAG(nexthop
->flags
,
2648 NEXTHOP_FLAG_FIB
)))) {
2649 /* Add the gateway */
2650 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2657 } else /* Multipath case */
2659 char buf
[NL_PKT_BUF_SIZE
];
2660 struct rtattr
*rta
= (void *)buf
;
2661 struct rtnexthop
*rtnh
;
2662 union g_addr
*src1
= NULL
;
2664 rta
->rta_type
= RTA_MULTIPATH
;
2665 rta
->rta_len
= RTA_LENGTH(0);
2666 rtnh
= RTA_DATA(rta
);
2668 routedesc
= "multipath";
2669 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2672 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2673 nexthop
= nhlfe
->nexthop
;
2677 if (nexthop_num
>= multipath_num
)
2680 if ((cmd
== RTM_NEWROUTE
2681 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2682 && CHECK_FLAG(nexthop
->flags
,
2683 NEXTHOP_FLAG_ACTIVE
)))
2684 || (cmd
== RTM_DELROUTE
2685 && (CHECK_FLAG(nhlfe
->flags
,
2686 NHLFE_FLAG_INSTALLED
)
2687 && CHECK_FLAG(nexthop
->flags
,
2688 NEXTHOP_FLAG_FIB
)))) {
2691 /* Build the multipath */
2692 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2695 rtnh
= RTNH_NEXT(rtnh
);
2699 /* Add the multipath */
2700 if (rta
->rta_len
> RTA_LENGTH(0))
2701 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2702 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2705 /* Talk to netlink socket. */
2706 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2709 #endif /* HAVE_NETLINK */