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(%d) ", ifp
? ifp
->name
: "Unknown",
798 strcat(oif_list
, temp
);
800 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
801 ifp
= if_lookup_by_index(iif
, vrf
);
802 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
803 zvrf
->vrf
->name
, vrf
,
804 nl_msg_type_to_str(h
->nlmsg_type
),
805 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
806 oif_list
, m
->lastused
);
811 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
818 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
819 /* If this is not route add/delete message print warning. */
820 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
824 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
)) {
826 "Invalid address family: %u received from kernel route change: %u",
827 rtm
->rtm_family
, h
->nlmsg_type
);
831 /* Connected route. */
832 if (IS_ZEBRA_DEBUG_KERNEL
)
833 zlog_debug("%s %s %s proto %s NS %u",
834 nl_msg_type_to_str(h
->nlmsg_type
),
835 nl_family_to_str(rtm
->rtm_family
),
836 nl_rttype_to_str(rtm
->rtm_type
),
837 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
840 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
842 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
845 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
849 if (rtm
->rtm_type
== RTN_MULTICAST
)
850 netlink_route_change_read_multicast(h
, ns_id
, startup
);
852 netlink_route_change_read_unicast(h
, ns_id
, startup
);
856 /* Request for specific route information from the kernel */
857 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
864 /* Form the request, specifying filter (rtattr) if needed. */
865 memset(&req
, 0, sizeof(req
));
866 req
.n
.nlmsg_type
= type
;
867 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
868 req
.rtm
.rtm_family
= family
;
870 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
873 /* Routing table read function using netlink interface. Only called
875 int netlink_route_read(struct zebra_ns
*zns
)
879 /* Get IPv4 routing table. */
880 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
883 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
884 &zns
->netlink_cmd
, zns
, 0, 1);
888 /* Get IPv6 routing table. */
889 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
892 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
893 &zns
->netlink_cmd
, zns
, 0, 1);
900 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
902 struct nlmsghdr
*nlmsg
,
903 size_t req_size
, int bytelen
,
904 struct nexthop
*nexthop
)
906 if (route_family
== AF_MPLS
) {
907 struct gw_family_t gw_fam
;
909 gw_fam
.family
= gw_family
;
910 if (gw_family
== AF_INET
)
911 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
913 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
914 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
917 if (gw_family
== AF_INET
)
918 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
919 &nexthop
->gate
.ipv4
, bytelen
);
921 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
922 &nexthop
->gate
.ipv6
, bytelen
);
926 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
929 struct rtnexthop
*rtnh
,
930 size_t req_size
, int bytelen
,
931 struct nexthop
*nexthop
)
933 if (route_family
== AF_MPLS
) {
934 struct gw_family_t gw_fam
;
936 gw_fam
.family
= gw_family
;
937 if (gw_family
== AF_INET
)
938 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
940 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
941 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
943 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
945 if (gw_family
== AF_INET
)
946 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
947 &nexthop
->gate
.ipv4
, bytelen
);
949 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
950 &nexthop
->gate
.ipv6
, bytelen
);
951 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
955 /* This function takes a nexthop as argument and adds
956 * the appropriate netlink attributes to an existing
959 * @param routedesc: Human readable description of route type
960 * (direct/recursive, single-/multipath)
961 * @param bytelen: Length of addresses in bytes.
962 * @param nexthop: Nexthop information
963 * @param nlmsg: nlmsghdr structure to fill in.
964 * @param req_size: The size allocated for the message.
966 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
967 struct nexthop
*nexthop
,
968 struct nlmsghdr
*nlmsg
,
970 size_t req_size
, int cmd
)
972 struct mpls_label_stack
*nh_label
;
973 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
978 * label_buf is *only* currently used within debugging.
979 * As such when we assign it we are guarding it inside
980 * a debug test. If you want to change this make sure
981 * you fix this assumption
986 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
989 nh_label
= nh
->nh_label
;
990 if (!nh_label
|| !nh_label
->num_labels
)
993 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
994 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
997 if (IS_ZEBRA_DEBUG_KERNEL
) {
999 sprintf(label_buf
, "label %u",
1000 nh_label
->label
[i
]);
1002 sprintf(label_buf1
, "/%u",
1003 nh_label
->label
[i
]);
1004 strlcat(label_buf
, label_buf1
,
1009 out_lse
[num_labels
] =
1010 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1016 /* Set the BoS bit */
1017 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1019 if (rtmsg
->rtm_family
== AF_MPLS
)
1020 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1021 num_labels
* sizeof(mpls_lse_t
));
1023 struct rtattr
*nest
;
1024 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1026 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1028 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1029 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1030 num_labels
* sizeof(mpls_lse_t
));
1031 addattr_nest_end(nlmsg
, nest
);
1035 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1036 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1038 if (rtmsg
->rtm_family
== AF_INET
1039 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1040 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1041 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1042 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1043 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1045 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1046 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1047 &nexthop
->rmap_src
.ipv4
, bytelen
);
1048 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1049 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1050 &nexthop
->src
.ipv4
, bytelen
);
1052 if (IS_ZEBRA_DEBUG_KERNEL
)
1054 " 5549: _netlink_route_build_singlepath() (%s): "
1055 "nexthop via %s %s if %u(%u)",
1056 routedesc
, ipv4_ll_buf
, label_buf
,
1057 nexthop
->ifindex
, nexthop
->vrf_id
);
1061 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1062 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1063 /* Send deletes to the kernel without specifying the next-hop */
1064 if (cmd
!= RTM_DELROUTE
)
1065 _netlink_route_nl_add_gateway_info(
1066 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1069 if (cmd
== RTM_NEWROUTE
) {
1070 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1071 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1072 &nexthop
->rmap_src
.ipv4
, bytelen
);
1073 else if (nexthop
->src
.ipv4
.s_addr
)
1074 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1075 &nexthop
->src
.ipv4
, bytelen
);
1078 if (IS_ZEBRA_DEBUG_KERNEL
)
1080 "netlink_route_multipath() (%s): "
1081 "nexthop via %s %s if %u(%u)",
1082 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1083 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1086 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1087 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1088 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1089 nlmsg
, req_size
, bytelen
,
1092 if (cmd
== RTM_NEWROUTE
) {
1093 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1094 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1095 &nexthop
->rmap_src
.ipv6
, bytelen
);
1096 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1097 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1098 &nexthop
->src
.ipv6
, bytelen
);
1101 if (IS_ZEBRA_DEBUG_KERNEL
)
1103 "netlink_route_multipath() (%s): "
1104 "nexthop via %s %s if %u(%u)",
1105 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1106 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1110 * We have the ifindex so we should always send it
1111 * This is especially useful if we are doing route
1114 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1115 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1117 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1118 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1119 if (cmd
== RTM_NEWROUTE
) {
1120 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1121 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1122 &nexthop
->rmap_src
.ipv4
, bytelen
);
1123 else if (nexthop
->src
.ipv4
.s_addr
)
1124 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1125 &nexthop
->src
.ipv4
, bytelen
);
1128 if (IS_ZEBRA_DEBUG_KERNEL
)
1130 "netlink_route_multipath() (%s): "
1131 "nexthop via if %u(%u)",
1132 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1135 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1136 if (cmd
== RTM_NEWROUTE
) {
1137 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1138 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1139 &nexthop
->rmap_src
.ipv6
, bytelen
);
1140 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1141 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1142 &nexthop
->src
.ipv6
, bytelen
);
1145 if (IS_ZEBRA_DEBUG_KERNEL
)
1147 "netlink_route_multipath() (%s): "
1148 "nexthop via if %u(%u)",
1149 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1153 /* This function takes a nexthop as argument and
1154 * appends to the given rtattr/rtnexthop pair the
1155 * representation of the nexthop. If the nexthop
1156 * defines a preferred source, the src parameter
1157 * will be modified to point to that src, otherwise
1158 * it will be kept unmodified.
1160 * @param routedesc: Human readable description of route type
1161 * (direct/recursive, single-/multipath)
1162 * @param bytelen: Length of addresses in bytes.
1163 * @param nexthop: Nexthop information
1164 * @param rta: rtnetlink attribute structure
1165 * @param rtnh: pointer to an rtnetlink nexthop structure
1166 * @param src: pointer pointing to a location where
1167 * the prefsrc should be stored.
1169 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1170 struct nexthop
*nexthop
,
1172 struct rtnexthop
*rtnh
,
1173 struct rtmsg
*rtmsg
,
1176 struct mpls_label_stack
*nh_label
;
1177 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1179 char label_buf
[256];
1181 rtnh
->rtnh_len
= sizeof(*rtnh
);
1182 rtnh
->rtnh_flags
= 0;
1183 rtnh
->rtnh_hops
= 0;
1184 rta
->rta_len
+= rtnh
->rtnh_len
;
1187 * label_buf is *only* currently used within debugging.
1188 * As such when we assign it we are guarding it inside
1189 * a debug test. If you want to change this make sure
1190 * you fix this assumption
1192 label_buf
[0] = '\0';
1195 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1196 char label_buf1
[20];
1198 nh_label
= nh
->nh_label
;
1199 if (!nh_label
|| !nh_label
->num_labels
)
1202 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1203 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1206 if (IS_ZEBRA_DEBUG_KERNEL
) {
1208 sprintf(label_buf
, "label %u",
1209 nh_label
->label
[i
]);
1211 sprintf(label_buf1
, "/%u",
1212 nh_label
->label
[i
]);
1213 strlcat(label_buf
, label_buf1
,
1218 out_lse
[num_labels
] =
1219 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1225 /* Set the BoS bit */
1226 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1228 if (rtmsg
->rtm_family
== AF_MPLS
) {
1229 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1231 num_labels
* sizeof(mpls_lse_t
));
1233 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1235 struct rtattr
*nest
;
1236 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1237 int len
= rta
->rta_len
;
1239 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1240 &encap
, sizeof(uint16_t));
1241 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1242 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1244 num_labels
* sizeof(mpls_lse_t
));
1245 rta_nest_end(rta
, nest
);
1246 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1250 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1251 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1253 if (rtmsg
->rtm_family
== AF_INET
1254 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1255 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1257 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1258 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1260 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1261 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1263 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1264 *src
= &nexthop
->rmap_src
;
1265 else if (nexthop
->src
.ipv4
.s_addr
)
1266 *src
= &nexthop
->src
;
1268 if (IS_ZEBRA_DEBUG_KERNEL
)
1270 " 5549: netlink_route_build_multipath() (%s): "
1271 "nexthop via %s %s if %u",
1272 routedesc
, ipv4_ll_buf
, label_buf
,
1277 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1278 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1279 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1280 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1282 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1283 *src
= &nexthop
->rmap_src
;
1284 else if (nexthop
->src
.ipv4
.s_addr
)
1285 *src
= &nexthop
->src
;
1287 if (IS_ZEBRA_DEBUG_KERNEL
)
1289 "netlink_route_multipath() (%s): "
1290 "nexthop via %s %s if %u",
1291 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1292 label_buf
, nexthop
->ifindex
);
1294 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1295 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1296 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1297 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1300 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1301 *src
= &nexthop
->rmap_src
;
1302 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1303 *src
= &nexthop
->src
;
1305 if (IS_ZEBRA_DEBUG_KERNEL
)
1307 "netlink_route_multipath() (%s): "
1308 "nexthop via %s %s if %u",
1309 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1310 label_buf
, nexthop
->ifindex
);
1314 * We have figured out the ifindex so we should always send it
1315 * This is especially useful if we are doing route
1318 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1319 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1322 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1323 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1324 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1325 *src
= &nexthop
->rmap_src
;
1326 else if (nexthop
->src
.ipv4
.s_addr
)
1327 *src
= &nexthop
->src
;
1329 if (IS_ZEBRA_DEBUG_KERNEL
)
1331 "netlink_route_multipath() (%s): "
1332 "nexthop via if %u",
1333 routedesc
, nexthop
->ifindex
);
1337 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1338 zebra_nhlfe_t
*nhlfe
,
1339 struct nlmsghdr
*nlmsg
,
1340 struct rtmsg
*rtmsg
,
1341 size_t req_size
, int cmd
)
1346 family
= NHLFE_FAMILY(nhlfe
);
1347 bytelen
= (family
== AF_INET
? 4 : 16);
1348 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1349 nlmsg
, rtmsg
, req_size
, cmd
);
1354 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1355 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1356 struct rtmsg
*rtmsg
, union g_addr
**src
)
1361 family
= NHLFE_FAMILY(nhlfe
);
1362 bytelen
= (family
== AF_INET
? 4 : 16);
1363 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1368 /* Log debug information for netlink_route_multipath
1369 * if debug logging is enabled.
1371 * @param cmd: Netlink command which is to be processed
1372 * @param p: Prefix for which the change is due
1373 * @param family: Address family which the change concerns
1374 * @param zvrf: The vrf we are in
1375 * @param tableid: The table we are working on
1377 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1378 int family
, vrf_id_t vrfid
,
1381 if (IS_ZEBRA_DEBUG_KERNEL
) {
1382 char buf
[PREFIX_STRLEN
];
1384 "netlink_route_multipath(): %s %s vrf %u(%u)",
1385 nl_msg_type_to_str(cmd
),
1386 prefix2str(p
, buf
, sizeof(buf
)),
1391 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1393 if (IS_ZEBRA_DEBUG_KERNEL
)
1394 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1395 nl_msg_type_to_str(cmd
), label
);
1398 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1399 int llalen
, ns_id_t ns_id
)
1407 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1409 memset(&req
, 0, sizeof(req
));
1411 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1412 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1413 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1414 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1416 req
.ndm
.ndm_family
= AF_INET
;
1417 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1418 req
.ndm
.ndm_ifindex
= ifindex
;
1419 req
.ndm
.ndm_type
= RTN_UNICAST
;
1421 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1422 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1424 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1428 /* Routing table change via netlink interface. */
1429 /* Update flag indicates whether this is a "replace" or not. */
1430 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1431 const struct prefix
*src_p
,
1432 struct route_entry
*re
,
1436 struct sockaddr_nl snl
;
1437 struct nexthop
*nexthop
= NULL
;
1438 unsigned int nexthop_num
;
1439 int family
= PREFIX_FAMILY(p
);
1440 const char *routedesc
;
1447 char buf
[NL_PKT_BUF_SIZE
];
1450 struct zebra_ns
*zns
;
1451 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1454 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1456 bytelen
= (family
== AF_INET
? 4 : 16);
1458 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1459 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1460 if ((cmd
== RTM_NEWROUTE
) && update
)
1461 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1462 req
.n
.nlmsg_type
= cmd
;
1463 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1465 req
.r
.rtm_family
= family
;
1466 req
.r
.rtm_dst_len
= p
->prefixlen
;
1467 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1468 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1469 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1472 * blackhole routes are not RTN_UNICAST, they are
1473 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1474 * so setting this value as a RTN_UNICAST would
1475 * cause the route lookup of just the prefix
1476 * to fail. So no need to specify this for
1477 * the RTM_DELROUTE case
1479 if (cmd
!= RTM_DELROUTE
)
1480 req
.r
.rtm_type
= RTN_UNICAST
;
1482 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1484 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1488 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1490 * either by the kernel or by zebra. Its purely for calculating best
1492 * by the routing protocol and for communicating with protocol peers.
1494 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1495 #if defined(SUPPORT_REALMS)
1496 if (re
->tag
> 0 && re
->tag
<= 255)
1497 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1499 /* Table corresponding to this route. */
1500 if (re
->table
< 256)
1501 req
.r
.rtm_table
= re
->table
;
1503 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1504 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1507 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1510 * If we are not updating the route and we have received
1511 * a route delete, then all we need to fill in is the
1512 * prefix information to tell the kernel to schwack
1515 if (!update
&& cmd
== RTM_DELROUTE
)
1518 if (re
->mtu
|| re
->nexthop_mtu
) {
1519 char buf
[NL_PKT_BUF_SIZE
];
1520 struct rtattr
*rta
= (void *)buf
;
1521 uint32_t mtu
= re
->mtu
;
1522 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1523 mtu
= re
->nexthop_mtu
;
1524 rta
->rta_type
= RTA_METRICS
;
1525 rta
->rta_len
= RTA_LENGTH(0);
1526 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1527 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1531 /* Count overall nexthops so we can decide whether to use singlepath
1532 * or multipath case. */
1534 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1535 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1537 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1539 if (cmd
== RTM_DELROUTE
1540 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1546 /* Singlepath case. */
1547 if (nexthop_num
== 1 || multipath_num
== 1) {
1549 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1551 * So we want to cover 2 types of blackhole
1553 * 1) A normal blackhole route( ala from a static
1555 * 2) A recursively resolved blackhole route
1557 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1558 switch (nexthop
->bh_type
) {
1559 case BLACKHOLE_ADMINPROHIB
:
1560 req
.r
.rtm_type
= RTN_PROHIBIT
;
1562 case BLACKHOLE_REJECT
:
1563 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1566 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1571 if (CHECK_FLAG(nexthop
->flags
,
1572 NEXTHOP_FLAG_RECURSIVE
)) {
1574 if (family
== AF_INET
) {
1575 if (nexthop
->rmap_src
.ipv4
1582 } else if (nexthop
->src
.ipv4
1590 } else if (family
== AF_INET6
) {
1591 if (!IN6_IS_ADDR_UNSPECIFIED(
1599 !IN6_IS_ADDR_UNSPECIFIED(
1612 if ((cmd
== RTM_NEWROUTE
1613 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1614 || (cmd
== RTM_DELROUTE
1615 && CHECK_FLAG(nexthop
->flags
,
1616 NEXTHOP_FLAG_FIB
))) {
1617 routedesc
= nexthop
->rparent
1618 ? "recursive, single-path"
1621 _netlink_route_build_singlepath(
1622 routedesc
, bytelen
, nexthop
, &req
.n
,
1623 &req
.r
, sizeof req
, cmd
);
1628 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1629 if (family
== AF_INET
)
1630 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1631 &src
.ipv4
, bytelen
);
1632 else if (family
== AF_INET6
)
1633 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1634 &src
.ipv6
, bytelen
);
1637 char buf
[NL_PKT_BUF_SIZE
];
1638 struct rtattr
*rta
= (void *)buf
;
1639 struct rtnexthop
*rtnh
;
1640 union g_addr
*src1
= NULL
;
1642 rta
->rta_type
= RTA_MULTIPATH
;
1643 rta
->rta_len
= RTA_LENGTH(0);
1644 rtnh
= RTA_DATA(rta
);
1647 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1648 if (nexthop_num
>= multipath_num
)
1651 if (CHECK_FLAG(nexthop
->flags
,
1652 NEXTHOP_FLAG_RECURSIVE
)) {
1653 /* This only works for IPv4 now */
1655 if (family
== AF_INET
) {
1656 if (nexthop
->rmap_src
.ipv4
1663 } else if (nexthop
->src
.ipv4
1671 } else if (family
== AF_INET6
) {
1672 if (!IN6_IS_ADDR_UNSPECIFIED(
1680 !IN6_IS_ADDR_UNSPECIFIED(
1693 if ((cmd
== RTM_NEWROUTE
1694 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1695 || (cmd
== RTM_DELROUTE
1696 && CHECK_FLAG(nexthop
->flags
,
1697 NEXTHOP_FLAG_FIB
))) {
1698 routedesc
= nexthop
->rparent
1699 ? "recursive, multipath"
1703 _netlink_route_build_multipath(
1704 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1706 rtnh
= RTNH_NEXT(rtnh
);
1708 if (!setsrc
&& src1
) {
1709 if (family
== AF_INET
)
1710 src
.ipv4
= src1
->ipv4
;
1711 else if (family
== AF_INET6
)
1712 src
.ipv6
= src1
->ipv6
;
1718 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1719 if (family
== AF_INET
)
1720 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1721 &src
.ipv4
, bytelen
);
1722 else if (family
== AF_INET6
)
1723 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1724 &src
.ipv6
, bytelen
);
1725 if (IS_ZEBRA_DEBUG_KERNEL
)
1726 zlog_debug("Setting source");
1729 if (rta
->rta_len
> RTA_LENGTH(0))
1730 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1731 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1734 /* If there is no useful nexthop then return. */
1735 if (nexthop_num
== 0) {
1736 if (IS_ZEBRA_DEBUG_KERNEL
)
1738 "netlink_route_multipath(): No useful nexthop.");
1744 /* Destination netlink address. */
1745 memset(&snl
, 0, sizeof snl
);
1746 snl
.nl_family
= AF_NETLINK
;
1748 /* Talk to netlink socket. */
1749 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1753 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1756 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1764 struct zebra_ns
*zns
;
1767 memset(&req
, 0, sizeof(req
));
1769 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1770 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1771 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1773 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1774 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1776 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1777 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1778 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1779 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1780 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1782 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1783 &zns
->netlink_cmd
, zns
, 0);
1789 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1790 const struct prefix
*p
,
1791 const struct prefix
*src_p
,
1792 struct route_entry
*old
,
1793 struct route_entry
*new)
1800 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1801 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1802 new, (old
) ? 1 : 0);
1805 * So v6 route replace semantics are not in
1806 * the kernel at this point as I understand it.
1807 * So let's do a delete than an add.
1808 * In the future once v6 route replace semantics
1809 * are in we can figure out what to do here to
1810 * allow working with old and new kernels.
1812 * I'm also intentionally ignoring the failure case
1813 * of the route delete. If that happens yeah we're
1817 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1819 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1822 kernel_route_rib_pass_fail(rn
, p
, new,
1823 (!ret
) ? DP_INSTALL_SUCCESS
1824 : DP_INSTALL_FAILURE
);
1825 return DP_REQUEST_SUCCESS
;
1829 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1831 kernel_route_rib_pass_fail(rn
, p
, old
,
1832 (!ret
) ? DP_DELETE_SUCCESS
1833 : DP_DELETE_FAILURE
);
1836 return DP_REQUEST_SUCCESS
;
1839 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1840 int llalen
, ns_id_t ns_id
)
1842 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1843 addr
, lla
, llalen
, ns_id
);
1847 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1848 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1850 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1851 struct in_addr
*vtep_ip
, int cmd
)
1853 struct zebra_ns
*zns
;
1859 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1860 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1863 memset(&req
, 0, sizeof(req
));
1865 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1866 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1867 if (cmd
== RTM_NEWNEIGH
)
1868 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1869 req
.n
.nlmsg_type
= cmd
;
1870 req
.ndm
.ndm_family
= PF_BRIDGE
;
1871 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1872 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1875 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1876 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1877 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1879 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1884 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1886 * a "flood" MAC FDB entry.
1888 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1890 if (IS_ZEBRA_DEBUG_VXLAN
)
1891 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1892 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1894 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1898 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1899 * deleting the "flood" MAC FDB entry.
1901 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1903 if (IS_ZEBRA_DEBUG_VXLAN
)
1905 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1906 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1908 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1912 #define NDA_RTA(r) \
1913 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1916 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1919 struct interface
*ifp
;
1920 struct zebra_if
*zif
;
1921 struct rtattr
*tb
[NDA_MAX
+ 1];
1922 struct interface
*br_if
;
1925 struct prefix vtep_ip
;
1926 int vid_present
= 0, dst_present
= 0;
1927 char buf
[ETHER_ADDR_STRLEN
];
1932 ndm
= NLMSG_DATA(h
);
1934 /* We only process macfdb notifications if EVPN is enabled */
1935 if (!is_evpn_enabled())
1938 /* The interface should exist. */
1939 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1941 if (!ifp
|| !ifp
->info
)
1944 /* The interface should be something we're interested in. */
1945 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1948 /* Drop "permanent" entries. */
1949 if (ndm
->ndm_state
& NUD_PERMANENT
)
1952 zif
= (struct zebra_if
*)ifp
->info
;
1953 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1954 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1955 nl_msg_type_to_str(h
->nlmsg_type
),
1956 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1957 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1961 /* Parse attributes and extract fields of interest. */
1962 memset(tb
, 0, sizeof tb
);
1963 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1965 if (!tb
[NDA_LLADDR
]) {
1966 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1967 nl_msg_type_to_str(h
->nlmsg_type
),
1968 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1969 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1973 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1975 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1976 nl_msg_type_to_str(h
->nlmsg_type
),
1977 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1978 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1979 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1983 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1985 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1987 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1988 sprintf(vid_buf
, " VLAN %u", vid
);
1992 /* TODO: Only IPv4 supported now. */
1994 vtep_ip
.family
= AF_INET
;
1995 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1996 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1998 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2001 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
2003 if (IS_ZEBRA_DEBUG_KERNEL
)
2004 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2005 nl_msg_type_to_str(h
->nlmsg_type
),
2006 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2007 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2008 sticky
? "sticky " : "",
2009 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2010 dst_present
? dst_buf
: "");
2012 if (filter_vlan
&& vid
!= filter_vlan
)
2015 /* If add or update, do accordingly if learnt on a "local" interface; if
2016 * the notification is over VxLAN, this has to be related to
2018 * so perform an implicit delete of any local entry (if it exists).
2020 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2021 /* Drop "permanent" entries. */
2022 if (ndm
->ndm_state
& NUD_PERMANENT
)
2025 if (IS_ZEBRA_IF_VXLAN(ifp
))
2026 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2029 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2033 /* This is a delete notification.
2034 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2035 * 2. For a MAC over "local" interface, delete the mac
2036 * Note: We will get notifications from both bridge driver and VxLAN
2038 * Ignore the notification from VxLan driver as it is also generated
2039 * when mac moves from remote to local.
2044 if (IS_ZEBRA_IF_VXLAN(ifp
))
2045 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2048 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2051 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2056 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2059 /* Length validity. */
2060 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2064 /* We are interested only in AF_BRIDGE notifications. */
2065 ndm
= NLMSG_DATA(h
);
2066 if (ndm
->ndm_family
!= AF_BRIDGE
)
2069 return netlink_macfdb_change(h
, len
, ns_id
);
2072 /* Request for MAC FDB information from the kernel */
2073 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2074 ifindex_t master_ifindex
)
2078 struct ifinfomsg ifm
;
2082 /* Form the request, specifying filter (rtattr) if needed. */
2083 memset(&req
, 0, sizeof(req
));
2084 req
.n
.nlmsg_type
= type
;
2085 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2086 req
.ifm
.ifi_family
= family
;
2088 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2090 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2094 * MAC forwarding database read using netlink interface. This is invoked
2097 int netlink_macfdb_read(struct zebra_ns
*zns
)
2101 /* Get bridge FDB table. */
2102 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2105 /* We are reading entire table. */
2107 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2114 * MAC forwarding database read using netlink interface. This is for a
2115 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2117 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2118 struct interface
*br_if
)
2120 struct zebra_if
*br_zif
;
2121 struct zebra_if
*zif
;
2122 struct zebra_l2info_vxlan
*vxl
;
2126 /* Save VLAN we're filtering on, if needed. */
2127 br_zif
= (struct zebra_if
*)br_if
->info
;
2128 zif
= (struct zebra_if
*)ifp
->info
;
2129 vxl
= &zif
->l2info
.vxl
;
2130 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2131 filter_vlan
= vxl
->access_vlan
;
2133 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2135 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2139 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2142 /* Reset VLAN filter. */
2147 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2148 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2149 int local
, int cmd
, uint8_t sticky
)
2151 struct zebra_ns
*zns
;
2158 struct zebra_if
*zif
;
2159 struct interface
*br_if
;
2160 struct zebra_if
*br_zif
;
2161 char buf
[ETHER_ADDR_STRLEN
];
2162 int vid_present
= 0, dst_present
= 0;
2165 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2169 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2170 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2171 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2176 memset(&req
, 0, sizeof(req
));
2178 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2179 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2180 if (cmd
== RTM_NEWNEIGH
)
2181 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2182 req
.n
.nlmsg_type
= cmd
;
2183 req
.ndm
.ndm_family
= AF_BRIDGE
;
2184 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2185 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2188 req
.ndm
.ndm_state
|= NUD_NOARP
;
2190 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2192 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2193 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2195 dst_alen
= 4; // TODO: hardcoded
2196 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2198 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2200 br_zif
= (struct zebra_if
*)br_if
->info
;
2201 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2202 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2204 sprintf(vid_buf
, " VLAN %u", vid
);
2206 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2208 if (IS_ZEBRA_DEBUG_KERNEL
)
2209 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2210 nl_msg_type_to_str(cmd
),
2211 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2212 ifp
->ifindex
, vid_present
? vid_buf
: "",
2213 sticky
? "sticky " : "",
2214 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2215 dst_present
? dst_buf
: "");
2217 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2222 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2225 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2228 struct interface
*ifp
;
2229 struct zebra_if
*zif
;
2230 struct rtattr
*tb
[NDA_MAX
+ 1];
2231 struct interface
*link_if
;
2234 char buf
[ETHER_ADDR_STRLEN
];
2235 char buf2
[INET6_ADDRSTRLEN
];
2236 int mac_present
= 0;
2237 uint8_t ext_learned
;
2238 uint8_t router_flag
;
2240 ndm
= NLMSG_DATA(h
);
2242 /* The interface should exist. */
2243 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2245 if (!ifp
|| !ifp
->info
)
2248 zif
= (struct zebra_if
*)ifp
->info
;
2250 /* Parse attributes and extract fields of interest. */
2251 memset(tb
, 0, sizeof tb
);
2252 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2255 zlog_warn("%s family %s IF %s(%u) - no DST",
2256 nl_msg_type_to_str(h
->nlmsg_type
),
2257 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2262 memset(&ip
, 0, sizeof(struct ipaddr
));
2263 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2264 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2266 /* Drop some "permanent" entries. */
2267 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2268 char buf
[16] = "169.254.0.1";
2269 struct in_addr ipv4_ll
;
2271 if (ndm
->ndm_family
!= AF_INET
)
2274 if (!zif
->v6_2_v4_ll_neigh_entry
)
2277 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2280 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2281 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2284 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2285 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2289 /* The neighbor is present on an SVI. From this, we locate the
2291 * bridge because we're only interested in neighbors on a VxLAN bridge.
2292 * The bridge is located based on the nature of the SVI:
2293 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2295 * and is linked to the bridge
2296 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2300 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2301 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2305 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2310 memset(&mac
, 0, sizeof(struct ethaddr
));
2311 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2312 if (tb
[NDA_LLADDR
]) {
2313 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2315 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2316 nl_msg_type_to_str(h
->nlmsg_type
),
2317 nl_family_to_str(ndm
->ndm_family
),
2318 ifp
->name
, ndm
->ndm_ifindex
,
2319 (unsigned long)RTA_PAYLOAD(
2325 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2328 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2329 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2331 if (IS_ZEBRA_DEBUG_KERNEL
)
2333 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2334 nl_msg_type_to_str(h
->nlmsg_type
),
2335 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2337 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2339 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2341 ndm
->ndm_state
, ndm
->ndm_flags
);
2343 /* If the neighbor state is valid for use, process as an add or
2345 * else process as a delete. Note that the delete handling may
2347 * in re-adding the neighbor if it is a valid "remote" neighbor.
2349 if (ndm
->ndm_state
& NUD_VALID
)
2350 return zebra_vxlan_handle_kernel_neigh_update(
2351 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2352 ext_learned
, router_flag
);
2354 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2357 if (IS_ZEBRA_DEBUG_KERNEL
)
2358 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2359 nl_msg_type_to_str(h
->nlmsg_type
),
2360 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2362 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2364 /* Process the delete - it may result in re-adding the neighbor if it is
2365 * a valid "remote" neighbor.
2367 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2370 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2375 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2378 /* Length validity. */
2379 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2383 /* We are interested only in AF_INET or AF_INET6 notifications. */
2384 ndm
= NLMSG_DATA(h
);
2385 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2388 return netlink_neigh_change(h
, len
);
2391 /* Request for IP neighbor information from the kernel */
2392 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2401 /* Form the request, specifying filter (rtattr) if needed. */
2402 memset(&req
, 0, sizeof(req
));
2403 req
.n
.nlmsg_type
= type
;
2404 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2405 req
.ndm
.ndm_family
= family
;
2407 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2409 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2413 * IP Neighbor table read using netlink interface. This is invoked
2416 int netlink_neigh_read(struct zebra_ns
*zns
)
2420 /* Get IP neighbor table. */
2421 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2424 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2431 * IP Neighbor table read using netlink interface. This is for a specific
2434 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2438 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2442 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2448 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2453 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2456 /* Length validity. */
2457 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2459 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2460 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2461 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2465 /* Is this a notification for the MAC FDB or IP neighbor table? */
2466 ndm
= NLMSG_DATA(h
);
2467 if (ndm
->ndm_family
== AF_BRIDGE
)
2468 return netlink_macfdb_change(h
, len
, ns_id
);
2470 if (ndm
->ndm_type
!= RTN_UNICAST
)
2473 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2474 return netlink_ipneigh_change(h
, len
, ns_id
);
2477 "Invalid address family: %u received from kernel neighbor change: %u",
2478 ndm
->ndm_family
, h
->nlmsg_type
);
2485 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2486 struct ethaddr
*mac
, uint8_t flags
,
2487 uint16_t state
, int cmd
)
2496 struct zebra_ns
*zns
;
2497 char buf
[INET6_ADDRSTRLEN
];
2498 char buf2
[ETHER_ADDR_STRLEN
];
2499 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2502 memset(&req
, 0, sizeof(req
));
2504 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2505 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2506 if (cmd
== RTM_NEWNEIGH
)
2507 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2508 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2509 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2510 req
.ndm
.ndm_state
= state
;
2511 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2512 req
.ndm
.ndm_type
= RTN_UNICAST
;
2513 req
.ndm
.ndm_flags
= flags
;
2515 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2516 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2518 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2520 if (IS_ZEBRA_DEBUG_KERNEL
)
2521 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2522 nl_msg_type_to_str(cmd
),
2523 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2524 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2525 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2528 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2532 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2533 struct in_addr vtep_ip
, uint8_t sticky
)
2535 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2539 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2540 struct in_addr vtep_ip
, int local
)
2542 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2546 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2547 struct ethaddr
*mac
, uint8_t flags
)
2549 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2550 NUD_NOARP
, RTM_NEWNEIGH
);
2553 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2555 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2559 * MPLS label forwarding table change via netlink interface.
2561 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2564 zebra_nhlfe_t
*nhlfe
;
2565 struct nexthop
*nexthop
= NULL
;
2566 unsigned int nexthop_num
;
2567 const char *routedesc
;
2568 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2574 char buf
[NL_PKT_BUF_SIZE
];
2577 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2580 * Count # nexthops so we can decide whether to use singlepath
2581 * or multipath case.
2584 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2585 nexthop
= nhlfe
->nexthop
;
2588 if (cmd
== RTM_NEWROUTE
) {
2589 /* Count all selected NHLFEs */
2590 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2591 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2595 /* Count all installed NHLFEs */
2596 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2597 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2602 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2605 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2606 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2607 req
.n
.nlmsg_type
= cmd
;
2608 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2610 req
.r
.rtm_family
= AF_MPLS
;
2611 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2612 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2613 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2614 req
.r
.rtm_type
= RTN_UNICAST
;
2616 if (cmd
== RTM_NEWROUTE
) {
2617 /* We do a replace to handle update. */
2618 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2620 /* set the protocol value if installing */
2621 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2622 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2625 /* Fill destination */
2626 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2627 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2629 /* Fill nexthops (paths) based on single-path or multipath. The paths
2630 * chosen depend on the operation.
2632 if (nexthop_num
== 1 || multipath_num
== 1) {
2633 routedesc
= "single-path";
2634 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2637 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2638 nexthop
= nhlfe
->nexthop
;
2642 if ((cmd
== RTM_NEWROUTE
2643 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2644 && CHECK_FLAG(nexthop
->flags
,
2645 NEXTHOP_FLAG_ACTIVE
)))
2646 || (cmd
== RTM_DELROUTE
2647 && (CHECK_FLAG(nhlfe
->flags
,
2648 NHLFE_FLAG_INSTALLED
)
2649 && CHECK_FLAG(nexthop
->flags
,
2650 NEXTHOP_FLAG_FIB
)))) {
2651 /* Add the gateway */
2652 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2659 } else /* Multipath case */
2661 char buf
[NL_PKT_BUF_SIZE
];
2662 struct rtattr
*rta
= (void *)buf
;
2663 struct rtnexthop
*rtnh
;
2664 union g_addr
*src1
= NULL
;
2666 rta
->rta_type
= RTA_MULTIPATH
;
2667 rta
->rta_len
= RTA_LENGTH(0);
2668 rtnh
= RTA_DATA(rta
);
2670 routedesc
= "multipath";
2671 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2674 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2675 nexthop
= nhlfe
->nexthop
;
2679 if (nexthop_num
>= multipath_num
)
2682 if ((cmd
== RTM_NEWROUTE
2683 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2684 && CHECK_FLAG(nexthop
->flags
,
2685 NEXTHOP_FLAG_ACTIVE
)))
2686 || (cmd
== RTM_DELROUTE
2687 && (CHECK_FLAG(nhlfe
->flags
,
2688 NHLFE_FLAG_INSTALLED
)
2689 && CHECK_FLAG(nexthop
->flags
,
2690 NEXTHOP_FLAG_FIB
)))) {
2693 /* Build the multipath */
2694 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2697 rtnh
= RTNH_NEXT(rtnh
);
2701 /* Add the multipath */
2702 if (rta
->rta_len
> RTA_LENGTH(0))
2703 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2704 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2707 /* Talk to netlink socket. */
2708 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2711 #endif /* HAVE_NETLINK */