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"
67 #include "zebra/zebra_errors.h"
73 static vlanid_t filter_vlan
= 0;
81 char ipv4_ll_buf
[16] = "169.254.0.1";
82 struct in_addr ipv4_ll
;
85 * The ipv4_ll data structure is used for all 5549
86 * additions to the kernel. Let's figure out the
87 * correct value one time instead for every
88 * install/remove of a 5549 type route
90 void rt_netlink_init(void)
92 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
95 static inline int is_selfroute(int proto
)
97 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
98 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
99 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
100 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
101 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
102 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
103 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)) {
110 static inline int zebra2proto(int proto
)
113 case ZEBRA_ROUTE_BABEL
:
114 proto
= RTPROT_BABEL
;
116 case ZEBRA_ROUTE_BGP
:
119 case ZEBRA_ROUTE_OSPF
:
120 case ZEBRA_ROUTE_OSPF6
:
123 case ZEBRA_ROUTE_STATIC
:
124 proto
= RTPROT_ZSTATIC
;
126 case ZEBRA_ROUTE_ISIS
:
129 case ZEBRA_ROUTE_RIP
:
132 case ZEBRA_ROUTE_RIPNG
:
133 proto
= RTPROT_RIPNG
;
135 case ZEBRA_ROUTE_NHRP
:
138 case ZEBRA_ROUTE_EIGRP
:
139 proto
= RTPROT_EIGRP
;
141 case ZEBRA_ROUTE_LDP
:
144 case ZEBRA_ROUTE_SHARP
:
145 proto
= RTPROT_SHARP
;
147 case ZEBRA_ROUTE_PBR
:
150 case ZEBRA_ROUTE_OPENFABRIC
:
151 proto
= RTPROT_OPENFABRIC
;
155 * When a user adds a new protocol this will show up
156 * to let them know to do something about it. This
157 * is intentionally a warn because we should see
158 * this as part of development of a new protocol
161 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
162 __PRETTY_FUNCTION__
, proto
);
163 proto
= RTPROT_ZEBRA
;
170 static inline int proto2zebra(int proto
, int family
)
174 proto
= ZEBRA_ROUTE_BABEL
;
177 proto
= ZEBRA_ROUTE_BGP
;
180 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
184 proto
= ZEBRA_ROUTE_ISIS
;
187 proto
= ZEBRA_ROUTE_RIP
;
190 proto
= ZEBRA_ROUTE_RIPNG
;
193 proto
= ZEBRA_ROUTE_NHRP
;
196 proto
= ZEBRA_ROUTE_EIGRP
;
199 proto
= ZEBRA_ROUTE_LDP
;
203 proto
= ZEBRA_ROUTE_STATIC
;
206 proto
= ZEBRA_ROUTE_SHARP
;
209 proto
= ZEBRA_ROUTE_PBR
;
211 case RTPROT_OPENFABRIC
:
212 proto
= ZEBRA_ROUTE_OPENFABRIC
;
216 * When a user adds a new protocol this will show up
217 * to let them know to do something about it. This
218 * is intentionally a warn because we should see
219 * this as part of development of a new protocol
222 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
223 __PRETTY_FUNCTION__
, proto
);
224 proto
= ZEBRA_ROUTE_KERNEL
;
231 Pending: create an efficient table_id (in a tree/hash) based lookup)
233 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
236 struct zebra_vrf
*zvrf
;
238 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
242 /* case vrf with netns : match the netnsid */
243 if (vrf_is_backend_netns()) {
244 if (ns_id
== zvrf_id(zvrf
))
245 return zvrf_id(zvrf
);
247 /* VRF is VRF_BACKEND_VRF_LITE */
248 if (zvrf
->table_id
!= table_id
)
250 return zvrf_id(zvrf
);
258 * @parse_encap_mpls() - Parses encapsulated mpls attributes
259 * @tb: Pointer to rtattr to look for nested items in.
260 * @labels: Pointer to store labels in.
262 * Return: Number of mpls labels found.
264 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
266 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
267 mpls_lse_t
*lses
= NULL
;
272 mpls_label_t label
= 0;
274 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
275 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
276 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
277 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
278 labels
[num_labels
++] = label
;
284 /* Looking up routing table by netlink interface. */
285 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
290 struct rtattr
*tb
[RTA_MAX
+ 1];
293 struct prefix_ipv6 src_p
= {};
296 char anyaddr
[16] = {0};
298 int proto
= ZEBRA_ROUTE_KERNEL
;
303 uint8_t distance
= 0;
308 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
309 void *src
= NULL
; /* IPv6 srcdest source prefix */
310 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
313 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
318 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
320 switch (rtm
->rtm_type
) {
324 bh_type
= BLACKHOLE_NULL
;
326 case RTN_UNREACHABLE
:
327 bh_type
= BLACKHOLE_REJECT
;
330 bh_type
= BLACKHOLE_ADMINPROHIB
;
336 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
338 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
339 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
340 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
344 memset(tb
, 0, sizeof tb
);
345 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
347 if (rtm
->rtm_flags
& RTM_F_CLONED
)
349 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
351 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
354 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
355 && h
->nlmsg_type
== RTM_NEWROUTE
) {
356 if (IS_ZEBRA_DEBUG_KERNEL
)
357 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
362 /* We don't care about change notifications for the MPLS table. */
363 /* TODO: Revisit this. */
364 if (rtm
->rtm_family
== AF_MPLS
)
367 /* Table corresponding to route. */
369 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
371 table
= rtm
->rtm_table
;
374 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
375 if (vrf_id
== VRF_DEFAULT
) {
376 if (!is_zebra_valid_kernel_table(table
)
377 && !is_zebra_main_routing_table(table
))
381 /* Route which inserted by Zebra. */
382 if (is_selfroute(rtm
->rtm_protocol
)) {
383 flags
|= ZEBRA_FLAG_SELFROUTE
;
384 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
387 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
390 dest
= RTA_DATA(tb
[RTA_DST
]);
395 src
= RTA_DATA(tb
[RTA_SRC
]);
400 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
403 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
405 if (tb
[RTA_PRIORITY
])
406 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
408 #if defined(SUPPORT_REALMS)
410 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
413 if (tb
[RTA_METRICS
]) {
414 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
416 memset(mxrta
, 0, sizeof mxrta
);
417 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
418 RTA_PAYLOAD(tb
[RTA_METRICS
]));
421 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
424 if (rtm
->rtm_family
== AF_INET
) {
426 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
428 "Invalid destination prefix length: %u received from kernel route change",
432 memcpy(&p
.u
.prefix4
, dest
, 4);
433 p
.prefixlen
= rtm
->rtm_dst_len
;
435 if (rtm
->rtm_src_len
!= 0) {
436 char buf
[PREFIX_STRLEN
];
438 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
439 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
440 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
444 /* Force debug below to not display anything for source */
446 } else if (rtm
->rtm_family
== AF_INET6
) {
448 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
450 "Invalid destination prefix length: %u received from kernel route change",
454 memcpy(&p
.u
.prefix6
, dest
, 16);
455 p
.prefixlen
= rtm
->rtm_dst_len
;
457 src_p
.family
= AF_INET6
;
458 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
460 "Invalid source prefix length: %u received from kernel route change",
464 memcpy(&src_p
.prefix
, src
, 16);
465 src_p
.prefixlen
= rtm
->rtm_src_len
;
469 * For ZEBRA_ROUTE_KERNEL types:
471 * The metric/priority of the route received from the kernel
472 * is a 32 bit number. We are going to interpret the high
473 * order byte as the Admin Distance and the low order 3 bytes
476 * This will allow us to do two things:
477 * 1) Allow the creation of kernel routes that can be
478 * overridden by zebra.
479 * 2) Allow the old behavior for 'most' kernel route types
480 * if a user enters 'ip route ...' v4 routes get a metric
481 * of 0 and v6 routes get a metric of 1024. Both of these
482 * values will end up with a admin distance of 0, which
483 * will cause them to win for the purposes of zebra.
485 if (proto
== ZEBRA_ROUTE_KERNEL
) {
486 distance
= (metric
>> 24) & 0xFF;
487 metric
= (metric
& 0x00FFFFFF);
490 if (IS_ZEBRA_DEBUG_KERNEL
) {
491 char buf
[PREFIX_STRLEN
];
492 char buf2
[PREFIX_STRLEN
];
493 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
494 nl_msg_type_to_str(h
->nlmsg_type
),
495 prefix2str(&p
, buf
, sizeof(buf
)),
496 src_p
.prefixlen
? " from " : "",
498 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
500 vrf_id
, table
, metric
, distance
);
504 if (rtm
->rtm_family
== AF_INET6
)
507 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
508 struct interface
*ifp
;
509 vrf_id_t nh_vrf_id
= vrf_id
;
511 if (!tb
[RTA_MULTIPATH
]) {
513 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
515 memset(&nh
, 0, sizeof(nh
));
517 if (bh_type
== BLACKHOLE_UNSPEC
) {
519 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
520 else if (index
&& gate
)
523 ? NEXTHOP_TYPE_IPV4_IFINDEX
524 : NEXTHOP_TYPE_IPV6_IFINDEX
;
525 else if (!index
&& gate
)
526 nh
.type
= (afi
== AFI_IP
)
530 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
531 nh
.bh_type
= bh_type
;
534 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
535 nh
.bh_type
= bh_type
;
539 memcpy(&nh
.src
, prefsrc
, sz
);
541 memcpy(&nh
.gate
, gate
, sz
);
544 ifp
= if_lookup_by_index_per_ns(
545 zebra_ns_lookup(ns_id
),
548 nh_vrf_id
= ifp
->vrf_id
;
550 nh
.vrf_id
= nh_vrf_id
;
552 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
553 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
554 == LWTUNNEL_ENCAP_MPLS
) {
556 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
560 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
563 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
564 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
566 /* This is a multipath route */
568 struct route_entry
*re
;
569 struct rtnexthop
*rtnh
=
570 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
572 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
574 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
576 re
->distance
= distance
;
583 re
->uptime
= time(NULL
);
587 struct nexthop
*nh
= NULL
;
589 if (len
< (int)sizeof(*rtnh
)
590 || rtnh
->rtnh_len
> len
)
593 index
= rtnh
->rtnh_ifindex
;
596 * Yes we are looking this up
597 * for every nexthop and just
598 * using the last one looked
601 ifp
= if_lookup_by_index_per_ns(
602 zebra_ns_lookup(ns_id
),
605 nh_vrf_id
= ifp
->vrf_id
;
608 EC_ZEBRA_UNKNOWN_INTERFACE
,
609 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
612 nh_vrf_id
= VRF_DEFAULT
;
618 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
619 memset(tb
, 0, sizeof(tb
));
620 netlink_parse_rtattr(
621 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
622 rtnh
->rtnh_len
- sizeof(*rtnh
));
626 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
627 && *(uint16_t *)RTA_DATA(
629 == LWTUNNEL_ENCAP_MPLS
) {
630 num_labels
= parse_encap_mpls(
631 tb
[RTA_ENCAP
], labels
);
636 if (rtm
->rtm_family
== AF_INET
) {
638 nh
= route_entry_nexthop_ipv4_ifindex_add(
643 nh
= route_entry_nexthop_ipv4_add(
647 } else if (rtm
->rtm_family
650 nh
= route_entry_nexthop_ipv6_ifindex_add(
654 nh
= route_entry_nexthop_ipv6_add(
659 nh
= route_entry_nexthop_ifindex_add(
660 re
, index
, nh_vrf_id
);
662 if (nh
&& num_labels
)
663 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
666 if (rtnh
->rtnh_len
== 0)
669 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
670 rtnh
= RTNH_NEXT(rtnh
);
673 zserv_nexthop_num_warn(__func__
,
674 (const struct prefix
*)&p
,
676 if (re
->nexthop_num
== 0)
679 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
683 if (!tb
[RTA_MULTIPATH
]) {
685 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
687 memset(&nh
, 0, sizeof(nh
));
688 if (bh_type
== BLACKHOLE_UNSPEC
) {
690 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
691 else if (index
&& gate
)
694 ? NEXTHOP_TYPE_IPV4_IFINDEX
695 : NEXTHOP_TYPE_IPV6_IFINDEX
;
696 else if (!index
&& gate
)
697 nh
.type
= (afi
== AFI_IP
)
701 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
702 nh
.bh_type
= BLACKHOLE_UNSPEC
;
705 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
706 nh
.bh_type
= bh_type
;
710 memcpy(&nh
.gate
, gate
, sz
);
711 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
712 &p
, &src_p
, &nh
, table
, metric
, distance
,
715 /* XXX: need to compare the entire list of nexthops
716 * here for NLM_F_APPEND stupidity */
717 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
718 &p
, &src_p
, NULL
, table
, metric
, distance
,
726 static struct mcast_route_data
*mroute
= NULL
;
728 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
729 ns_id_t ns_id
, int startup
)
733 struct rtattr
*tb
[RTA_MAX
+ 1];
734 struct mcast_route_data
*m
;
735 struct mcast_route_data mr
;
742 char oif_list
[256] = "\0";
749 memset(&mr
, 0, sizeof(mr
));
755 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
757 memset(tb
, 0, sizeof tb
);
758 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
761 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
763 table
= rtm
->rtm_table
;
765 vrf
= vrf_lookup_by_table(table
, ns_id
);
768 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
771 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
774 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
776 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
777 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
779 if (tb
[RTA_MULTIPATH
]) {
780 struct rtnexthop
*rtnh
=
781 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
783 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
785 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
788 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
791 if (rtnh
->rtnh_len
== 0)
794 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
795 rtnh
= RTNH_NEXT(rtnh
);
799 if (IS_ZEBRA_DEBUG_KERNEL
) {
800 struct interface
*ifp
;
801 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
802 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
803 for (count
= 0; count
< oif_count
; count
++) {
804 ifp
= if_lookup_by_index(oif
[count
], vrf
);
807 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
809 strcat(oif_list
, temp
);
811 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
812 ifp
= if_lookup_by_index(iif
, vrf
);
813 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
814 zvrf
->vrf
->name
, vrf
,
815 nl_msg_type_to_str(h
->nlmsg_type
),
816 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
817 oif_list
, m
->lastused
);
822 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
829 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
830 /* If this is not route add/delete message print warning. */
831 zlog_debug("Kernel message: %s NS %u\n",
832 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
836 if (!(rtm
->rtm_family
== AF_INET
||
837 rtm
->rtm_family
== AF_INET6
||
838 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
840 EC_ZEBRA_UNKNOWN_FAMILY
,
841 "Invalid address family: %u received from kernel route change: %s",
842 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
846 /* Connected route. */
847 if (IS_ZEBRA_DEBUG_KERNEL
)
848 zlog_debug("%s %s %s proto %s NS %u",
849 nl_msg_type_to_str(h
->nlmsg_type
),
850 nl_family_to_str(rtm
->rtm_family
),
851 nl_rttype_to_str(rtm
->rtm_type
),
852 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
855 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
857 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
860 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
864 if (rtm
->rtm_type
== RTN_MULTICAST
)
865 netlink_route_change_read_multicast(h
, ns_id
, startup
);
867 netlink_route_change_read_unicast(h
, ns_id
, startup
);
871 /* Request for specific route information from the kernel */
872 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
879 /* Form the request, specifying filter (rtattr) if needed. */
880 memset(&req
, 0, sizeof(req
));
881 req
.n
.nlmsg_type
= type
;
882 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
883 req
.rtm
.rtm_family
= family
;
885 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
888 /* Routing table read function using netlink interface. Only called
890 int netlink_route_read(struct zebra_ns
*zns
)
893 struct zebra_dplane_info dp_info
;
895 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
897 /* Get IPv4 routing table. */
898 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
901 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
902 &zns
->netlink_cmd
, &dp_info
, 0, 1);
906 /* Get IPv6 routing table. */
907 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
910 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
911 &zns
->netlink_cmd
, &dp_info
, 0, 1);
918 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
920 struct nlmsghdr
*nlmsg
,
921 size_t req_size
, int bytelen
,
922 struct nexthop
*nexthop
)
924 if (route_family
== AF_MPLS
) {
925 struct gw_family_t gw_fam
;
927 gw_fam
.family
= gw_family
;
928 if (gw_family
== AF_INET
)
929 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
931 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
932 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
935 if (gw_family
== AF_INET
)
936 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
937 &nexthop
->gate
.ipv4
, bytelen
);
939 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
940 &nexthop
->gate
.ipv6
, bytelen
);
944 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
947 struct rtnexthop
*rtnh
,
948 size_t req_size
, int bytelen
,
949 struct nexthop
*nexthop
)
951 if (route_family
== AF_MPLS
) {
952 struct gw_family_t gw_fam
;
954 gw_fam
.family
= gw_family
;
955 if (gw_family
== AF_INET
)
956 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
958 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
959 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
961 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
963 if (gw_family
== AF_INET
)
964 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
965 &nexthop
->gate
.ipv4
, bytelen
);
967 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
968 &nexthop
->gate
.ipv6
, bytelen
);
969 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
973 /* This function takes a nexthop as argument and adds
974 * the appropriate netlink attributes to an existing
977 * @param routedesc: Human readable description of route type
978 * (direct/recursive, single-/multipath)
979 * @param bytelen: Length of addresses in bytes.
980 * @param nexthop: Nexthop information
981 * @param nlmsg: nlmsghdr structure to fill in.
982 * @param req_size: The size allocated for the message.
984 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
985 struct nexthop
*nexthop
,
986 struct nlmsghdr
*nlmsg
,
988 size_t req_size
, int cmd
)
990 struct mpls_label_stack
*nh_label
;
991 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
996 * label_buf is *only* currently used within debugging.
997 * As such when we assign it we are guarding it inside
998 * a debug test. If you want to change this make sure
999 * you fix this assumption
1001 label_buf
[0] = '\0';
1004 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1005 char label_buf1
[20];
1007 nh_label
= nh
->nh_label
;
1008 if (!nh_label
|| !nh_label
->num_labels
)
1011 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1012 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1015 if (IS_ZEBRA_DEBUG_KERNEL
) {
1017 sprintf(label_buf
, "label %u",
1018 nh_label
->label
[i
]);
1020 sprintf(label_buf1
, "/%u",
1021 nh_label
->label
[i
]);
1022 strlcat(label_buf
, label_buf1
,
1027 out_lse
[num_labels
] =
1028 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1034 /* Set the BoS bit */
1035 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1037 if (rtmsg
->rtm_family
== AF_MPLS
)
1038 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1039 num_labels
* sizeof(mpls_lse_t
));
1041 struct rtattr
*nest
;
1042 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1044 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1046 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1047 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1048 num_labels
* sizeof(mpls_lse_t
));
1049 addattr_nest_end(nlmsg
, nest
);
1053 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1054 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1056 if (rtmsg
->rtm_family
== AF_INET
1057 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1058 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1059 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1060 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1061 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1063 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1064 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1065 &nexthop
->rmap_src
.ipv4
, bytelen
);
1066 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1067 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1068 &nexthop
->src
.ipv4
, bytelen
);
1070 if (IS_ZEBRA_DEBUG_KERNEL
)
1072 " 5549: _netlink_route_build_singlepath() (%s): "
1073 "nexthop via %s %s if %u(%u)",
1074 routedesc
, ipv4_ll_buf
, label_buf
,
1075 nexthop
->ifindex
, nexthop
->vrf_id
);
1079 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1080 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1081 /* Send deletes to the kernel without specifying the next-hop */
1082 if (cmd
!= RTM_DELROUTE
)
1083 _netlink_route_nl_add_gateway_info(
1084 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1087 if (cmd
== RTM_NEWROUTE
) {
1088 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1089 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1090 &nexthop
->rmap_src
.ipv4
, bytelen
);
1091 else if (nexthop
->src
.ipv4
.s_addr
)
1092 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1093 &nexthop
->src
.ipv4
, bytelen
);
1096 if (IS_ZEBRA_DEBUG_KERNEL
)
1098 "netlink_route_multipath() (%s): "
1099 "nexthop via %s %s if %u(%u)",
1100 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1101 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1104 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1105 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1106 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1107 nlmsg
, req_size
, bytelen
,
1110 if (cmd
== RTM_NEWROUTE
) {
1111 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1112 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1113 &nexthop
->rmap_src
.ipv6
, bytelen
);
1114 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1115 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1116 &nexthop
->src
.ipv6
, bytelen
);
1119 if (IS_ZEBRA_DEBUG_KERNEL
)
1121 "netlink_route_multipath() (%s): "
1122 "nexthop via %s %s if %u(%u)",
1123 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1124 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1128 * We have the ifindex so we should always send it
1129 * This is especially useful if we are doing route
1132 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1133 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1135 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1136 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1137 if (cmd
== RTM_NEWROUTE
) {
1138 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1139 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1140 &nexthop
->rmap_src
.ipv4
, bytelen
);
1141 else if (nexthop
->src
.ipv4
.s_addr
)
1142 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1143 &nexthop
->src
.ipv4
, bytelen
);
1146 if (IS_ZEBRA_DEBUG_KERNEL
)
1148 "netlink_route_multipath() (%s): "
1149 "nexthop via if %u(%u)",
1150 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1153 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1154 if (cmd
== RTM_NEWROUTE
) {
1155 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1156 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1157 &nexthop
->rmap_src
.ipv6
, bytelen
);
1158 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1159 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1160 &nexthop
->src
.ipv6
, bytelen
);
1163 if (IS_ZEBRA_DEBUG_KERNEL
)
1165 "netlink_route_multipath() (%s): "
1166 "nexthop via if %u(%u)",
1167 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1171 /* This function takes a nexthop as argument and
1172 * appends to the given rtattr/rtnexthop pair the
1173 * representation of the nexthop. If the nexthop
1174 * defines a preferred source, the src parameter
1175 * will be modified to point to that src, otherwise
1176 * it will be kept unmodified.
1178 * @param routedesc: Human readable description of route type
1179 * (direct/recursive, single-/multipath)
1180 * @param bytelen: Length of addresses in bytes.
1181 * @param nexthop: Nexthop information
1182 * @param rta: rtnetlink attribute structure
1183 * @param rtnh: pointer to an rtnetlink nexthop structure
1184 * @param src: pointer pointing to a location where
1185 * the prefsrc should be stored.
1187 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1188 struct nexthop
*nexthop
,
1190 struct rtnexthop
*rtnh
,
1191 struct rtmsg
*rtmsg
,
1194 struct mpls_label_stack
*nh_label
;
1195 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1197 char label_buf
[256];
1199 rtnh
->rtnh_len
= sizeof(*rtnh
);
1200 rtnh
->rtnh_flags
= 0;
1201 rtnh
->rtnh_hops
= 0;
1202 rta
->rta_len
+= rtnh
->rtnh_len
;
1205 * label_buf is *only* currently used within debugging.
1206 * As such when we assign it we are guarding it inside
1207 * a debug test. If you want to change this make sure
1208 * you fix this assumption
1210 label_buf
[0] = '\0';
1213 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1214 char label_buf1
[20];
1216 nh_label
= nh
->nh_label
;
1217 if (!nh_label
|| !nh_label
->num_labels
)
1220 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1221 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1224 if (IS_ZEBRA_DEBUG_KERNEL
) {
1226 sprintf(label_buf
, "label %u",
1227 nh_label
->label
[i
]);
1229 sprintf(label_buf1
, "/%u",
1230 nh_label
->label
[i
]);
1231 strlcat(label_buf
, label_buf1
,
1236 out_lse
[num_labels
] =
1237 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1243 /* Set the BoS bit */
1244 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1246 if (rtmsg
->rtm_family
== AF_MPLS
) {
1247 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1249 num_labels
* sizeof(mpls_lse_t
));
1251 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1253 struct rtattr
*nest
;
1254 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1255 int len
= rta
->rta_len
;
1257 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1258 &encap
, sizeof(uint16_t));
1259 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1260 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1262 num_labels
* sizeof(mpls_lse_t
));
1263 rta_nest_end(rta
, nest
);
1264 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1268 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1269 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1271 if (rtmsg
->rtm_family
== AF_INET
1272 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1273 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1275 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1276 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1278 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1279 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1281 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1282 *src
= &nexthop
->rmap_src
;
1283 else if (nexthop
->src
.ipv4
.s_addr
)
1284 *src
= &nexthop
->src
;
1286 if (IS_ZEBRA_DEBUG_KERNEL
)
1288 " 5549: netlink_route_build_multipath() (%s): "
1289 "nexthop via %s %s if %u",
1290 routedesc
, ipv4_ll_buf
, label_buf
,
1295 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1296 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1297 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1298 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1300 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1301 *src
= &nexthop
->rmap_src
;
1302 else if (nexthop
->src
.ipv4
.s_addr
)
1303 *src
= &nexthop
->src
;
1305 if (IS_ZEBRA_DEBUG_KERNEL
)
1307 "netlink_route_multipath() (%s): "
1308 "nexthop via %s %s if %u",
1309 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1310 label_buf
, nexthop
->ifindex
);
1312 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1313 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1314 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1315 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1318 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1319 *src
= &nexthop
->rmap_src
;
1320 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1321 *src
= &nexthop
->src
;
1323 if (IS_ZEBRA_DEBUG_KERNEL
)
1325 "netlink_route_multipath() (%s): "
1326 "nexthop via %s %s if %u",
1327 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1328 label_buf
, nexthop
->ifindex
);
1332 * We have figured out the ifindex so we should always send it
1333 * This is especially useful if we are doing route
1336 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1337 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1340 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1341 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1342 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1343 *src
= &nexthop
->rmap_src
;
1344 else if (nexthop
->src
.ipv4
.s_addr
)
1345 *src
= &nexthop
->src
;
1347 if (IS_ZEBRA_DEBUG_KERNEL
)
1349 "netlink_route_multipath() (%s): "
1350 "nexthop via if %u",
1351 routedesc
, nexthop
->ifindex
);
1355 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1356 zebra_nhlfe_t
*nhlfe
,
1357 struct nlmsghdr
*nlmsg
,
1358 struct rtmsg
*rtmsg
,
1359 size_t req_size
, int cmd
)
1364 family
= NHLFE_FAMILY(nhlfe
);
1365 bytelen
= (family
== AF_INET
? 4 : 16);
1366 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1367 nlmsg
, rtmsg
, req_size
, cmd
);
1372 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1373 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1374 struct rtmsg
*rtmsg
, union g_addr
**src
)
1379 family
= NHLFE_FAMILY(nhlfe
);
1380 bytelen
= (family
== AF_INET
? 4 : 16);
1381 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1386 /* Log debug information for netlink_route_multipath
1387 * if debug logging is enabled.
1389 * @param cmd: Netlink command which is to be processed
1390 * @param p: Prefix for which the change is due
1391 * @param family: Address family which the change concerns
1392 * @param zvrf: The vrf we are in
1393 * @param tableid: The table we are working on
1395 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1396 int family
, vrf_id_t vrfid
,
1399 if (IS_ZEBRA_DEBUG_KERNEL
) {
1400 char buf
[PREFIX_STRLEN
];
1402 "netlink_route_multipath(): %s %s vrf %u(%u)",
1403 nl_msg_type_to_str(cmd
),
1404 prefix2str(p
, buf
, sizeof(buf
)),
1409 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1411 if (IS_ZEBRA_DEBUG_KERNEL
)
1412 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1413 nl_msg_type_to_str(cmd
), label
);
1416 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1417 int llalen
, ns_id_t ns_id
)
1425 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1427 memset(&req
, 0, sizeof(req
));
1429 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1430 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1431 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1432 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1434 req
.ndm
.ndm_family
= AF_INET
;
1435 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1436 req
.ndm
.ndm_ifindex
= ifindex
;
1437 req
.ndm
.ndm_type
= RTN_UNICAST
;
1439 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1440 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1442 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1446 /* Routing table change via netlink interface. */
1447 /* Update flag indicates whether this is a "replace" or not. */
1448 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1449 const struct prefix
*src_p
,
1450 struct route_entry
*re
,
1454 struct sockaddr_nl snl
;
1455 struct nexthop
*nexthop
= NULL
;
1456 unsigned int nexthop_num
;
1457 int family
= PREFIX_FAMILY(p
);
1458 const char *routedesc
;
1465 char buf
[NL_PKT_BUF_SIZE
];
1468 struct zebra_ns
*zns
;
1469 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1472 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1474 bytelen
= (family
== AF_INET
? 4 : 16);
1476 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1477 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1478 if ((cmd
== RTM_NEWROUTE
) && update
)
1479 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1480 req
.n
.nlmsg_type
= cmd
;
1481 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1483 req
.r
.rtm_family
= family
;
1484 req
.r
.rtm_dst_len
= p
->prefixlen
;
1485 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1486 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1487 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1490 * blackhole routes are not RTN_UNICAST, they are
1491 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1492 * so setting this value as a RTN_UNICAST would
1493 * cause the route lookup of just the prefix
1494 * to fail. So no need to specify this for
1495 * the RTM_DELROUTE case
1497 if (cmd
!= RTM_DELROUTE
)
1498 req
.r
.rtm_type
= RTN_UNICAST
;
1500 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1502 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1506 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1508 * either by the kernel or by zebra. Its purely for calculating best
1510 * by the routing protocol and for communicating with protocol peers.
1512 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1513 #if defined(SUPPORT_REALMS)
1514 if (re
->tag
> 0 && re
->tag
<= 255)
1515 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1517 /* Table corresponding to this route. */
1518 if (re
->table
< 256)
1519 req
.r
.rtm_table
= re
->table
;
1521 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1522 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1525 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1528 * If we are not updating the route and we have received
1529 * a route delete, then all we need to fill in is the
1530 * prefix information to tell the kernel to schwack
1533 if (!update
&& cmd
== RTM_DELROUTE
)
1536 if (re
->mtu
|| re
->nexthop_mtu
) {
1537 char buf
[NL_PKT_BUF_SIZE
];
1538 struct rtattr
*rta
= (void *)buf
;
1539 uint32_t mtu
= re
->mtu
;
1540 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1541 mtu
= re
->nexthop_mtu
;
1542 rta
->rta_type
= RTA_METRICS
;
1543 rta
->rta_len
= RTA_LENGTH(0);
1544 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1545 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1549 /* Count overall nexthops so we can decide whether to use singlepath
1550 * or multipath case. */
1552 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1553 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1555 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1557 if (cmd
== RTM_DELROUTE
1558 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1564 /* Singlepath case. */
1565 if (nexthop_num
== 1 || multipath_num
== 1) {
1567 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1569 * So we want to cover 2 types of blackhole
1571 * 1) A normal blackhole route( ala from a static
1573 * 2) A recursively resolved blackhole route
1575 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1576 switch (nexthop
->bh_type
) {
1577 case BLACKHOLE_ADMINPROHIB
:
1578 req
.r
.rtm_type
= RTN_PROHIBIT
;
1580 case BLACKHOLE_REJECT
:
1581 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1584 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1589 if (CHECK_FLAG(nexthop
->flags
,
1590 NEXTHOP_FLAG_RECURSIVE
)) {
1592 if (family
== AF_INET
) {
1593 if (nexthop
->rmap_src
.ipv4
1600 } else if (nexthop
->src
.ipv4
1608 } else if (family
== AF_INET6
) {
1609 if (!IN6_IS_ADDR_UNSPECIFIED(
1617 !IN6_IS_ADDR_UNSPECIFIED(
1630 if ((cmd
== RTM_NEWROUTE
1631 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1632 || (cmd
== RTM_DELROUTE
1633 && CHECK_FLAG(nexthop
->flags
,
1634 NEXTHOP_FLAG_FIB
))) {
1635 routedesc
= nexthop
->rparent
1636 ? "recursive, single-path"
1639 _netlink_route_build_singlepath(
1640 routedesc
, bytelen
, nexthop
, &req
.n
,
1641 &req
.r
, sizeof req
, cmd
);
1646 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1647 if (family
== AF_INET
)
1648 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1649 &src
.ipv4
, bytelen
);
1650 else if (family
== AF_INET6
)
1651 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1652 &src
.ipv6
, bytelen
);
1654 } else { /* Multipath case */
1655 char buf
[NL_PKT_BUF_SIZE
];
1656 struct rtattr
*rta
= (void *)buf
;
1657 struct rtnexthop
*rtnh
;
1658 union g_addr
*src1
= NULL
;
1660 rta
->rta_type
= RTA_MULTIPATH
;
1661 rta
->rta_len
= RTA_LENGTH(0);
1662 rtnh
= RTA_DATA(rta
);
1665 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1666 if (nexthop_num
>= multipath_num
)
1669 if (CHECK_FLAG(nexthop
->flags
,
1670 NEXTHOP_FLAG_RECURSIVE
)) {
1671 /* This only works for IPv4 now */
1673 if (family
== AF_INET
) {
1674 if (nexthop
->rmap_src
.ipv4
1681 } else if (nexthop
->src
.ipv4
1689 } else if (family
== AF_INET6
) {
1690 if (!IN6_IS_ADDR_UNSPECIFIED(
1698 !IN6_IS_ADDR_UNSPECIFIED(
1711 if ((cmd
== RTM_NEWROUTE
1712 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1713 || (cmd
== RTM_DELROUTE
1714 && CHECK_FLAG(nexthop
->flags
,
1715 NEXTHOP_FLAG_FIB
))) {
1716 routedesc
= nexthop
->rparent
1717 ? "recursive, multipath"
1721 _netlink_route_build_multipath(
1722 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1724 rtnh
= RTNH_NEXT(rtnh
);
1726 if (!setsrc
&& src1
) {
1727 if (family
== AF_INET
)
1728 src
.ipv4
= src1
->ipv4
;
1729 else if (family
== AF_INET6
)
1730 src
.ipv6
= src1
->ipv6
;
1736 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1737 if (family
== AF_INET
)
1738 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1739 &src
.ipv4
, bytelen
);
1740 else if (family
== AF_INET6
)
1741 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1742 &src
.ipv6
, bytelen
);
1743 if (IS_ZEBRA_DEBUG_KERNEL
)
1744 zlog_debug("Setting source");
1747 if (rta
->rta_len
> RTA_LENGTH(0))
1748 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1749 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1752 /* If there is no useful nexthop then return. */
1753 if (nexthop_num
== 0) {
1754 if (IS_ZEBRA_DEBUG_KERNEL
)
1756 "netlink_route_multipath(): No useful nexthop.");
1762 /* Destination netlink address. */
1763 memset(&snl
, 0, sizeof snl
);
1764 snl
.nl_family
= AF_NETLINK
;
1766 /* Talk to netlink socket. */
1767 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1772 * Routing table change via netlink interface, using a dataplane context object
1774 static int netlink_route_multipath_ctx(int cmd
, dplane_ctx_h ctx
)
1777 struct sockaddr_nl snl
;
1778 struct nexthop
*nexthop
= NULL
;
1779 unsigned int nexthop_num
;
1781 const char *routedesc
;
1784 const struct prefix
*p
, *src_p
;
1790 char buf
[NL_PKT_BUF_SIZE
];
1793 p
= dplane_ctx_get_dest(ctx
);
1794 src_p
= dplane_ctx_get_src(ctx
);
1796 family
= PREFIX_FAMILY(p
);
1798 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
1800 bytelen
= (family
== AF_INET
? 4 : 16);
1802 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1803 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1805 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
1806 if ((p
->family
== AF_INET
) || v6_rr_semantics
)
1807 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1810 req
.n
.nlmsg_type
= cmd
;
1812 req
.n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1814 req
.r
.rtm_family
= family
;
1815 req
.r
.rtm_dst_len
= p
->prefixlen
;
1816 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1817 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1819 if (cmd
== RTM_DELROUTE
)
1820 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1822 req
.r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1825 * blackhole routes are not RTN_UNICAST, they are
1826 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1827 * so setting this value as a RTN_UNICAST would
1828 * cause the route lookup of just the prefix
1829 * to fail. So no need to specify this for
1830 * the RTM_DELROUTE case
1832 if (cmd
!= RTM_DELROUTE
)
1833 req
.r
.rtm_type
= RTN_UNICAST
;
1835 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &p
->u
.prefix
, bytelen
);
1837 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &src_p
->u
.prefix
,
1841 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1843 * either by the kernel or by zebra. Its purely for calculating best
1845 * by the routing protocol and for communicating with protocol peers.
1847 addattr32(&req
.n
, sizeof(req
), RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1849 #if defined(SUPPORT_REALMS)
1853 if (cmd
== RTM_DELROUTE
)
1854 tag
= dplane_ctx_get_old_tag(ctx
);
1856 tag
= dplane_ctx_get_tag(ctx
);
1858 if (tag
> 0 && tag
<= 255)
1859 addattr32(&req
.n
, sizeof(req
), RTA_FLOW
, tag
);
1862 /* Table corresponding to this route. */
1863 table_id
= dplane_ctx_get_table(ctx
);
1865 req
.r
.rtm_table
= table_id
;
1867 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1868 addattr32(&req
.n
, sizeof(req
), RTA_TABLE
, table_id
);
1871 _netlink_route_debug(cmd
, p
, family
, dplane_ctx_get_vrf(ctx
), table_id
);
1874 * If we are not updating the route and we have received
1875 * a route delete, then all we need to fill in is the
1876 * prefix information to tell the kernel to schwack
1879 if (cmd
== RTM_DELROUTE
)
1882 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1883 char buf
[NL_PKT_BUF_SIZE
];
1884 struct rtattr
*rta
= (void *)buf
;
1885 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1886 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1888 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1890 rta
->rta_type
= RTA_METRICS
;
1891 rta
->rta_len
= RTA_LENGTH(0);
1892 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
1893 RTAX_MTU
, &mtu
, sizeof(mtu
));
1894 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1898 /* Count overall nexthops so we can decide whether to use singlepath
1899 * or multipath case.
1902 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1903 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1905 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1911 /* Singlepath case. */
1912 if (nexthop_num
== 1 || multipath_num
== 1) {
1914 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1916 * So we want to cover 2 types of blackhole
1918 * 1) A normal blackhole route( ala from a static
1920 * 2) A recursively resolved blackhole route
1922 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1923 switch (nexthop
->bh_type
) {
1924 case BLACKHOLE_ADMINPROHIB
:
1925 req
.r
.rtm_type
= RTN_PROHIBIT
;
1927 case BLACKHOLE_REJECT
:
1928 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1931 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1936 if (CHECK_FLAG(nexthop
->flags
,
1937 NEXTHOP_FLAG_RECURSIVE
)) {
1942 if (family
== AF_INET
) {
1943 if (nexthop
->rmap_src
.ipv4
.s_addr
1946 nexthop
->rmap_src
.ipv4
;
1948 } else if (nexthop
->src
.ipv4
.s_addr
1954 } else if (family
== AF_INET6
) {
1955 if (!IN6_IS_ADDR_UNSPECIFIED(
1956 &nexthop
->rmap_src
.ipv6
)) {
1958 nexthop
->rmap_src
.ipv6
;
1961 !IN6_IS_ADDR_UNSPECIFIED(
1962 &nexthop
->src
.ipv6
)) {
1971 if ((cmd
== RTM_NEWROUTE
1972 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
1973 routedesc
= nexthop
->rparent
1974 ? "recursive, single-path"
1977 _netlink_route_build_singlepath(
1978 routedesc
, bytelen
, nexthop
, &req
.n
,
1979 &req
.r
, sizeof(req
), cmd
);
1984 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1985 if (family
== AF_INET
)
1986 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1987 &src
.ipv4
, bytelen
);
1988 else if (family
== AF_INET6
)
1989 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
1990 &src
.ipv6
, bytelen
);
1992 } else { /* Multipath case */
1993 char buf
[NL_PKT_BUF_SIZE
];
1994 struct rtattr
*rta
= (void *)buf
;
1995 struct rtnexthop
*rtnh
;
1996 union g_addr
*src1
= NULL
;
1998 rta
->rta_type
= RTA_MULTIPATH
;
1999 rta
->rta_len
= RTA_LENGTH(0);
2000 rtnh
= RTA_DATA(rta
);
2003 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2004 if (nexthop_num
>= multipath_num
)
2007 if (CHECK_FLAG(nexthop
->flags
,
2008 NEXTHOP_FLAG_RECURSIVE
)) {
2009 /* This only works for IPv4 now */
2013 if (family
== AF_INET
) {
2014 if (nexthop
->rmap_src
.ipv4
.s_addr
2017 nexthop
->rmap_src
.ipv4
;
2019 } else if (nexthop
->src
.ipv4
.s_addr
2025 } else if (family
== AF_INET6
) {
2026 if (!IN6_IS_ADDR_UNSPECIFIED(
2027 &nexthop
->rmap_src
.ipv6
)) {
2029 nexthop
->rmap_src
.ipv6
;
2032 !IN6_IS_ADDR_UNSPECIFIED(
2033 &nexthop
->src
.ipv6
)) {
2041 if ((cmd
== RTM_NEWROUTE
2042 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))) {
2043 routedesc
= nexthop
->rparent
2044 ? "recursive, multipath"
2048 _netlink_route_build_multipath(
2049 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
2051 rtnh
= RTNH_NEXT(rtnh
);
2053 if (!setsrc
&& src1
) {
2054 if (family
== AF_INET
)
2055 src
.ipv4
= src1
->ipv4
;
2056 else if (family
== AF_INET6
)
2057 src
.ipv6
= src1
->ipv6
;
2063 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
2064 if (family
== AF_INET
)
2065 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
2066 &src
.ipv4
, bytelen
);
2067 else if (family
== AF_INET6
)
2068 addattr_l(&req
.n
, sizeof(req
), RTA_PREFSRC
,
2069 &src
.ipv6
, bytelen
);
2070 if (IS_ZEBRA_DEBUG_KERNEL
)
2071 zlog_debug("Setting source");
2074 if (rta
->rta_len
> RTA_LENGTH(0))
2075 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2076 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2079 /* If there is no useful nexthop then return. */
2080 if (nexthop_num
== 0) {
2081 if (IS_ZEBRA_DEBUG_KERNEL
)
2083 "netlink_route_multipath(): No useful nexthop.");
2089 /* Destination netlink address. */
2090 memset(&snl
, 0, sizeof(snl
));
2091 snl
.nl_family
= AF_NETLINK
;
2093 /* Talk to netlink socket. */
2094 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
2095 dplane_ctx_get_ns(ctx
), 0);
2098 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2100 uint32_t actual_table
;
2102 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2110 struct zebra_ns
*zns
;
2113 memset(&req
, 0, sizeof(req
));
2115 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2116 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2117 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2119 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2120 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2122 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
2123 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
2124 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
2125 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
2129 * So during the namespace cleanup we started storing
2130 * the zvrf table_id for the default table as RT_TABLE_MAIN
2131 * which is what the normal routing table for ip routing is.
2132 * This change caused this to break our lookups of sg data
2133 * because prior to this change the zvrf->table_id was 0
2134 * and when the pim multicast kernel code saw a 0,
2135 * it was auto-translated to RT_TABLE_DEFAULT. But since
2136 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2137 * and the kernel goes screw you and the delicious cookies you
2138 * are trying to give me. So now we have this little hack.
2140 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2142 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &actual_table
, 4);
2144 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2145 &zns
->netlink_cmd
, zns
, 0);
2151 enum zebra_dplane_result
kernel_route_rib(struct route_node
*rn
,
2152 const struct prefix
*p
,
2153 const struct prefix
*src_p
,
2154 struct route_entry
*old
,
2155 struct route_entry
*new)
2162 if (p
->family
== AF_INET
|| v6_rr_semantics
)
2163 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
2164 new, (old
) ? 1 : 0);
2167 * So v6 route replace semantics are not in
2168 * the kernel at this point as I understand it.
2169 * So let's do a delete than an add.
2170 * In the future once v6 route replace semantics
2171 * are in we can figure out what to do here to
2172 * allow working with old and new kernels.
2174 * I'm also intentionally ignoring the failure case
2175 * of the route delete. If that happens yeah we're
2179 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
2181 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
2184 kernel_route_rib_pass_fail(rn
, p
, new,
2185 (!ret
) ? ZEBRA_DPLANE_INSTALL_SUCCESS
2186 : ZEBRA_DPLANE_INSTALL_FAILURE
);
2187 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2191 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
2193 kernel_route_rib_pass_fail(rn
, p
, old
,
2194 (!ret
) ? ZEBRA_DPLANE_DELETE_SUCCESS
2195 : ZEBRA_DPLANE_DELETE_FAILURE
);
2198 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
2202 * Update or delete a prefix from the kernel,
2203 * using info from a dataplane context.
2205 enum zebra_dplane_result
kernel_route_update(dplane_ctx_h ctx
)
2208 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2209 struct nexthop
*nexthop
;
2211 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2213 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2215 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2217 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2218 /* Single 'replace' operation */
2222 * So v6 route replace semantics are not in
2223 * the kernel at this point as I understand it.
2224 * so let's do a delete then an add.
2225 * In the future once v6 route replace semantics
2226 * are in we can figure out what to do here to
2227 * allow working with old and new kernels.
2229 * I'm also intentionally ignoring the failure case
2230 * of the route delete. If that happens yeah we're
2233 (void)netlink_route_multipath_ctx(RTM_DELROUTE
, ctx
);
2238 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2241 ret
= netlink_route_multipath_ctx(cmd
, ctx
);
2242 if ((cmd
== RTM_NEWROUTE
) && (ret
== 0)) {
2243 /* Update installed nexthops to signal which have been
2246 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2247 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2250 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2251 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2253 /* If we're only allowed a single nh, don't
2256 if (multipath_num
== 1)
2263 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
2266 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2267 int llalen
, ns_id_t ns_id
)
2269 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2270 addr
, lla
, llalen
, ns_id
);
2274 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2275 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2277 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
2278 struct in_addr
*vtep_ip
, int cmd
)
2280 struct zebra_ns
*zns
;
2286 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2287 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2290 memset(&req
, 0, sizeof(req
));
2292 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2293 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2294 if (cmd
== RTM_NEWNEIGH
)
2295 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
2296 req
.n
.nlmsg_type
= cmd
;
2297 req
.ndm
.ndm_family
= PF_BRIDGE
;
2298 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
2299 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
2302 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
2303 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2304 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
2306 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2311 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
2313 * a "flood" MAC FDB entry.
2315 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
2317 if (IS_ZEBRA_DEBUG_VXLAN
)
2318 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
2319 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
2321 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
2325 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
2326 * deleting the "flood" MAC FDB entry.
2328 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
2330 if (IS_ZEBRA_DEBUG_VXLAN
)
2332 "Uninstall %s from flood list for VNI %u intf %s(%u)",
2333 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
2335 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
2339 #define NDA_RTA(r) \
2340 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2343 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2346 struct interface
*ifp
;
2347 struct zebra_if
*zif
;
2348 struct rtattr
*tb
[NDA_MAX
+ 1];
2349 struct interface
*br_if
;
2352 struct prefix vtep_ip
;
2353 int vid_present
= 0, dst_present
= 0;
2354 char buf
[ETHER_ADDR_STRLEN
];
2359 ndm
= NLMSG_DATA(h
);
2361 /* We only process macfdb notifications if EVPN is enabled */
2362 if (!is_evpn_enabled())
2365 /* The interface should exist. */
2366 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2368 if (!ifp
|| !ifp
->info
)
2371 /* The interface should be something we're interested in. */
2372 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2375 /* Drop "permanent" entries. */
2376 if (ndm
->ndm_state
& NUD_PERMANENT
)
2379 zif
= (struct zebra_if
*)ifp
->info
;
2380 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2381 zlog_debug("%s family %s IF %s(%u) brIF %u - no bridge master",
2382 nl_msg_type_to_str(h
->nlmsg_type
),
2383 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2384 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
2388 /* Parse attributes and extract fields of interest. */
2389 memset(tb
, 0, sizeof tb
);
2390 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2392 if (!tb
[NDA_LLADDR
]) {
2393 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2394 nl_msg_type_to_str(h
->nlmsg_type
),
2395 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2396 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
2400 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2402 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2403 nl_msg_type_to_str(h
->nlmsg_type
),
2404 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2405 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
2406 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2410 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2412 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2414 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2415 sprintf(vid_buf
, " VLAN %u", vid
);
2419 /* TODO: Only IPv4 supported now. */
2421 vtep_ip
.family
= AF_INET
;
2422 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2423 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2425 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2428 sticky
= !!(ndm
->ndm_state
& NUD_NOARP
);
2430 if (IS_ZEBRA_DEBUG_KERNEL
)
2431 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2432 nl_msg_type_to_str(h
->nlmsg_type
),
2433 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2434 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2435 sticky
? "sticky " : "",
2436 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2437 dst_present
? dst_buf
: "");
2439 if (filter_vlan
&& vid
!= filter_vlan
)
2442 /* If add or update, do accordingly if learnt on a "local" interface; if
2443 * the notification is over VxLAN, this has to be related to
2445 * so perform an implicit delete of any local entry (if it exists).
2447 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2448 /* Drop "permanent" entries. */
2449 if (ndm
->ndm_state
& NUD_PERMANENT
)
2452 if (IS_ZEBRA_IF_VXLAN(ifp
))
2453 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2456 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2460 /* This is a delete notification.
2461 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2462 * 2. For a MAC over "local" interface, delete the mac
2463 * Note: We will get notifications from both bridge driver and VxLAN
2465 * Ignore the notification from VxLan driver as it is also generated
2466 * when mac moves from remote to local.
2471 if (IS_ZEBRA_IF_VXLAN(ifp
))
2472 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2475 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2478 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2483 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2486 /* Length validity. */
2487 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2491 /* We are interested only in AF_BRIDGE notifications. */
2492 ndm
= NLMSG_DATA(h
);
2493 if (ndm
->ndm_family
!= AF_BRIDGE
)
2496 return netlink_macfdb_change(h
, len
, ns_id
);
2499 /* Request for MAC FDB information from the kernel */
2500 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
2501 int type
, ifindex_t master_ifindex
)
2505 struct ifinfomsg ifm
;
2509 /* Form the request, specifying filter (rtattr) if needed. */
2510 memset(&req
, 0, sizeof(req
));
2511 req
.n
.nlmsg_type
= type
;
2512 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2513 req
.ifm
.ifi_family
= family
;
2515 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2517 return netlink_request(netlink_cmd
, &req
.n
);
2521 * MAC forwarding database read using netlink interface. This is invoked
2524 int netlink_macfdb_read(struct zebra_ns
*zns
)
2527 struct zebra_dplane_info dp_info
;
2529 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2531 /* Get bridge FDB table. */
2532 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2536 /* We are reading entire table. */
2538 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2545 * MAC forwarding database read using netlink interface. This is for a
2546 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2548 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2549 struct interface
*br_if
)
2551 struct zebra_if
*br_zif
;
2552 struct zebra_if
*zif
;
2553 struct zebra_l2info_vxlan
*vxl
;
2554 struct zebra_dplane_info dp_info
;
2557 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2559 /* Save VLAN we're filtering on, if needed. */
2560 br_zif
= (struct zebra_if
*)br_if
->info
;
2561 zif
= (struct zebra_if
*)ifp
->info
;
2562 vxl
= &zif
->l2info
.vxl
;
2563 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2564 filter_vlan
= vxl
->access_vlan
;
2566 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2568 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
2572 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
2575 /* Reset VLAN filter. */
2580 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2581 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2582 int cmd
, bool sticky
)
2584 struct zebra_ns
*zns
;
2591 struct zebra_if
*zif
;
2592 struct interface
*br_if
;
2593 struct zebra_if
*br_zif
;
2594 char buf
[ETHER_ADDR_STRLEN
];
2595 int vid_present
= 0;
2598 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2602 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2603 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2604 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2609 memset(&req
, 0, sizeof(req
));
2611 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2612 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2613 if (cmd
== RTM_NEWNEIGH
)
2614 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2615 req
.n
.nlmsg_type
= cmd
;
2616 req
.ndm
.ndm_family
= AF_BRIDGE
;
2617 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2618 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2621 req
.ndm
.ndm_state
|= NUD_NOARP
;
2623 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2625 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2626 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2627 dst_alen
= 4; // TODO: hardcoded
2628 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2629 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2630 br_zif
= (struct zebra_if
*)br_if
->info
;
2631 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2632 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2634 sprintf(vid_buf
, " VLAN %u", vid
);
2636 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2638 if (IS_ZEBRA_DEBUG_KERNEL
)
2639 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2640 nl_msg_type_to_str(cmd
),
2641 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2642 ifp
->ifindex
, vid_present
? vid_buf
: "",
2643 sticky
? "sticky " : "",
2644 prefix_mac2str(mac
, buf
, sizeof(buf
)), dst_buf
);
2646 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2651 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2654 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2657 struct interface
*ifp
;
2658 struct zebra_if
*zif
;
2659 struct rtattr
*tb
[NDA_MAX
+ 1];
2660 struct interface
*link_if
;
2663 char buf
[ETHER_ADDR_STRLEN
];
2664 char buf2
[INET6_ADDRSTRLEN
];
2665 int mac_present
= 0;
2669 ndm
= NLMSG_DATA(h
);
2671 /* The interface should exist. */
2672 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2674 if (!ifp
|| !ifp
->info
)
2677 zif
= (struct zebra_if
*)ifp
->info
;
2679 /* Parse attributes and extract fields of interest. */
2680 memset(tb
, 0, sizeof tb
);
2681 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2684 zlog_debug("%s family %s IF %s(%u) - no DST",
2685 nl_msg_type_to_str(h
->nlmsg_type
),
2686 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2691 memset(&ip
, 0, sizeof(struct ipaddr
));
2692 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2693 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2695 /* Drop some "permanent" entries. */
2696 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2697 char b
[16] = "169.254.0.1";
2698 struct in_addr ipv4_ll
;
2700 if (ndm
->ndm_family
!= AF_INET
)
2703 if (!zif
->v6_2_v4_ll_neigh_entry
)
2706 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2709 inet_pton(AF_INET
, b
, &ipv4_ll
);
2710 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2713 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2714 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2718 /* The neighbor is present on an SVI. From this, we locate the
2720 * bridge because we're only interested in neighbors on a VxLAN bridge.
2721 * The bridge is located based on the nature of the SVI:
2722 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2724 * and is linked to the bridge
2725 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2729 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2730 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2734 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2739 memset(&mac
, 0, sizeof(struct ethaddr
));
2740 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2741 if (tb
[NDA_LLADDR
]) {
2742 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2744 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2745 nl_msg_type_to_str(h
->nlmsg_type
),
2746 nl_family_to_str(ndm
->ndm_family
),
2747 ifp
->name
, ndm
->ndm_ifindex
,
2748 (unsigned long)RTA_PAYLOAD(
2754 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2757 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
2758 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
2760 if (IS_ZEBRA_DEBUG_KERNEL
)
2762 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2763 nl_msg_type_to_str(h
->nlmsg_type
),
2764 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2766 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2768 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2770 ndm
->ndm_state
, ndm
->ndm_flags
);
2772 /* If the neighbor state is valid for use, process as an add or
2774 * else process as a delete. Note that the delete handling may
2776 * in re-adding the neighbor if it is a valid "remote" neighbor.
2778 if (ndm
->ndm_state
& NUD_VALID
)
2779 return zebra_vxlan_handle_kernel_neigh_update(
2780 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2783 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2786 if (IS_ZEBRA_DEBUG_KERNEL
)
2787 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2788 nl_msg_type_to_str(h
->nlmsg_type
),
2789 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2791 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2793 /* Process the delete - it may result in re-adding the neighbor if it is
2794 * a valid "remote" neighbor.
2796 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2799 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2804 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2807 /* Length validity. */
2808 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2812 /* We are interested only in AF_INET or AF_INET6 notifications. */
2813 ndm
= NLMSG_DATA(h
);
2814 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2817 return netlink_neigh_change(h
, len
);
2820 /* Request for IP neighbor information from the kernel */
2821 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
2822 int type
, ifindex_t ifindex
)
2830 /* Form the request, specifying filter (rtattr) if needed. */
2831 memset(&req
, 0, sizeof(req
));
2832 req
.n
.nlmsg_type
= type
;
2833 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2834 req
.ndm
.ndm_family
= family
;
2836 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2838 return netlink_request(netlink_cmd
, &req
.n
);
2842 * IP Neighbor table read using netlink interface. This is invoked
2845 int netlink_neigh_read(struct zebra_ns
*zns
)
2848 struct zebra_dplane_info dp_info
;
2850 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2852 /* Get IP neighbor table. */
2853 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2857 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2864 * IP Neighbor table read using netlink interface. This is for a specific
2867 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2870 struct zebra_dplane_info dp_info
;
2872 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2874 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
2878 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
2884 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2889 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2892 /* Length validity. */
2893 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2895 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2896 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2897 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2901 /* Is this a notification for the MAC FDB or IP neighbor table? */
2902 ndm
= NLMSG_DATA(h
);
2903 if (ndm
->ndm_family
== AF_BRIDGE
)
2904 return netlink_macfdb_change(h
, len
, ns_id
);
2906 if (ndm
->ndm_type
!= RTN_UNICAST
)
2909 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2910 return netlink_ipneigh_change(h
, len
, ns_id
);
2913 EC_ZEBRA_UNKNOWN_FAMILY
,
2914 "Invalid address family: %u received from kernel neighbor change: %s",
2915 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
2922 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2923 struct ethaddr
*mac
, uint8_t flags
,
2924 uint16_t state
, int cmd
)
2933 struct zebra_ns
*zns
;
2934 char buf
[INET6_ADDRSTRLEN
];
2935 char buf2
[ETHER_ADDR_STRLEN
];
2936 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2939 memset(&req
, 0, sizeof(req
));
2941 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2942 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2943 if (cmd
== RTM_NEWNEIGH
)
2944 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2945 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2946 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2947 req
.ndm
.ndm_state
= state
;
2948 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2949 req
.ndm
.ndm_type
= RTN_UNICAST
;
2950 req
.ndm
.ndm_flags
= flags
;
2952 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2953 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2955 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2957 if (IS_ZEBRA_DEBUG_KERNEL
)
2958 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2959 nl_msg_type_to_str(cmd
),
2960 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2961 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2962 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2965 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2969 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2970 struct in_addr vtep_ip
, bool sticky
)
2972 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_NEWNEIGH
,
2976 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2977 struct in_addr vtep_ip
)
2979 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, RTM_DELNEIGH
, 0);
2982 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2983 struct ethaddr
*mac
, uint8_t flags
)
2985 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2986 NUD_NOARP
, RTM_NEWNEIGH
);
2989 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2991 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2995 * MPLS label forwarding table change via netlink interface.
2997 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
3000 zebra_nhlfe_t
*nhlfe
;
3001 struct nexthop
*nexthop
= NULL
;
3002 unsigned int nexthop_num
;
3003 const char *routedesc
;
3004 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
3010 char buf
[NL_PKT_BUF_SIZE
];
3013 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
3016 * Count # nexthops so we can decide whether to use singlepath
3017 * or multipath case.
3020 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
3021 nexthop
= nhlfe
->nexthop
;
3024 if (cmd
== RTM_NEWROUTE
) {
3025 /* Count all selected NHLFEs */
3026 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3027 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3031 /* Count all installed NHLFEs */
3032 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3033 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3038 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
3041 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3042 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3043 req
.n
.nlmsg_type
= cmd
;
3044 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
3046 req
.r
.rtm_family
= AF_MPLS
;
3047 req
.r
.rtm_table
= RT_TABLE_MAIN
;
3048 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3049 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3050 req
.r
.rtm_type
= RTN_UNICAST
;
3052 if (cmd
== RTM_NEWROUTE
) {
3053 /* We do a replace to handle update. */
3054 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
3056 /* set the protocol value if installing */
3057 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
3058 req
.r
.rtm_protocol
= zebra2proto(route_type
);
3061 /* Fill destination */
3062 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
3063 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
3065 /* Fill nexthops (paths) based on single-path or multipath. The paths
3066 * chosen depend on the operation.
3068 if (nexthop_num
== 1 || multipath_num
== 1) {
3069 routedesc
= "single-path";
3070 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
3073 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
3074 nexthop
= nhlfe
->nexthop
;
3078 if ((cmd
== RTM_NEWROUTE
3079 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3080 && CHECK_FLAG(nexthop
->flags
,
3081 NEXTHOP_FLAG_ACTIVE
)))
3082 || (cmd
== RTM_DELROUTE
3083 && (CHECK_FLAG(nhlfe
->flags
,
3084 NHLFE_FLAG_INSTALLED
)
3085 && CHECK_FLAG(nexthop
->flags
,
3086 NEXTHOP_FLAG_FIB
)))) {
3087 /* Add the gateway */
3088 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
3095 } else /* Multipath case */
3097 char buf
[NL_PKT_BUF_SIZE
];
3098 struct rtattr
*rta
= (void *)buf
;
3099 struct rtnexthop
*rtnh
;
3100 union g_addr
*src1
= NULL
;
3102 rta
->rta_type
= RTA_MULTIPATH
;
3103 rta
->rta_len
= RTA_LENGTH(0);
3104 rtnh
= RTA_DATA(rta
);
3106 routedesc
= "multipath";
3107 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
3110 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
3111 nexthop
= nhlfe
->nexthop
;
3115 if (nexthop_num
>= multipath_num
)
3118 if ((cmd
== RTM_NEWROUTE
3119 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3120 && CHECK_FLAG(nexthop
->flags
,
3121 NEXTHOP_FLAG_ACTIVE
)))
3122 || (cmd
== RTM_DELROUTE
3123 && (CHECK_FLAG(nhlfe
->flags
,
3124 NHLFE_FLAG_INSTALLED
)
3125 && CHECK_FLAG(nexthop
->flags
,
3126 NEXTHOP_FLAG_FIB
)))) {
3129 /* Build the multipath */
3130 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
3133 rtnh
= RTNH_NEXT(rtnh
);
3137 /* Add the multipath */
3138 if (rta
->rta_len
> RTA_LENGTH(0))
3139 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
3140 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
3143 /* Talk to netlink socket. */
3144 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3147 #endif /* HAVE_NETLINK */