1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <net/if_arp.h>
26 #include <linux/lwtunnel.h>
27 #include <linux/mpls_iptunnel.h>
28 #include <linux/neighbour.h>
29 #include <linux/rtnetlink.h>
31 /* Hack for GNU libc version 2. */
33 #define MSG_TRUNC 0x20
34 #endif /* MSG_TRUNC */
40 #include "connected.h"
43 #include "zebra_memory.h"
53 #include "zebra/zapi_msg.h"
54 #include "zebra/zebra_ns.h"
55 #include "zebra/zebra_vrf.h"
57 #include "zebra/redistribute.h"
58 #include "zebra/interface.h"
59 #include "zebra/debug.h"
60 #include "zebra/rtadv.h"
61 #include "zebra/zebra_ptm.h"
62 #include "zebra/zebra_mpls.h"
63 #include "zebra/kernel_netlink.h"
64 #include "zebra/rt_netlink.h"
65 #include "zebra/zebra_mroute.h"
66 #include "zebra/zebra_vxlan.h"
72 static vlanid_t filter_vlan
= 0;
80 char ipv4_ll_buf
[16] = "169.254.0.1";
81 struct in_addr ipv4_ll
;
84 * The ipv4_ll data structure is used for all 5549
85 * additions to the kernel. Let's figure out the
86 * correct value one time instead for every
87 * install/remove of a 5549 type route
89 void rt_netlink_init(void)
91 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
94 static inline int is_selfroute(int proto
)
96 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
97 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
98 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
99 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
100 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
101 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
102 || (proto
== RTPROT_PBR
)) {
109 static inline int zebra2proto(int proto
)
112 case ZEBRA_ROUTE_BABEL
:
113 proto
= RTPROT_BABEL
;
115 case ZEBRA_ROUTE_BGP
:
118 case ZEBRA_ROUTE_OSPF
:
119 case ZEBRA_ROUTE_OSPF6
:
122 case ZEBRA_ROUTE_STATIC
:
123 proto
= RTPROT_ZSTATIC
;
125 case ZEBRA_ROUTE_ISIS
:
128 case ZEBRA_ROUTE_RIP
:
131 case ZEBRA_ROUTE_RIPNG
:
132 proto
= RTPROT_RIPNG
;
134 case ZEBRA_ROUTE_NHRP
:
137 case ZEBRA_ROUTE_EIGRP
:
138 proto
= RTPROT_EIGRP
;
140 case ZEBRA_ROUTE_LDP
:
143 case ZEBRA_ROUTE_SHARP
:
144 proto
= RTPROT_SHARP
;
146 case ZEBRA_ROUTE_PBR
:
151 * When a user adds a new protocol this will show up
152 * to let them know to do something about it. This
153 * is intentionally a warn because we should see
154 * this as part of development of a new protocol
156 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
157 __PRETTY_FUNCTION__
, proto
);
158 proto
= RTPROT_ZEBRA
;
165 static inline int proto2zebra(int proto
, int family
)
169 proto
= ZEBRA_ROUTE_BABEL
;
172 proto
= ZEBRA_ROUTE_BGP
;
175 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
179 proto
= ZEBRA_ROUTE_ISIS
;
182 proto
= ZEBRA_ROUTE_RIP
;
185 proto
= ZEBRA_ROUTE_RIPNG
;
188 proto
= ZEBRA_ROUTE_NHRP
;
191 proto
= ZEBRA_ROUTE_EIGRP
;
194 proto
= ZEBRA_ROUTE_LDP
;
198 proto
= ZEBRA_ROUTE_STATIC
;
201 proto
= ZEBRA_ROUTE_SHARP
;
204 proto
= ZEBRA_ROUTE_PBR
;
208 * When a user adds a new protocol this will show up
209 * to let them know to do something about it. This
210 * is intentionally a warn because we should see
211 * this as part of development of a new protocol
213 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
216 proto
= ZEBRA_ROUTE_KERNEL
;
223 Pending: create an efficient table_id (in a tree/hash) based lookup)
225 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
228 struct zebra_vrf
*zvrf
;
230 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
234 /* case vrf with netns : match the netnsid */
235 if (vrf_is_backend_netns()) {
236 if (ns_id
== zvrf_id(zvrf
))
237 return zvrf_id(zvrf
);
239 /* VRF is VRF_BACKEND_VRF_LITE */
240 if (zvrf
->table_id
!= table_id
)
242 return zvrf_id(zvrf
);
249 /* Looking up routing table by netlink interface. */
250 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
255 struct rtattr
*tb
[RTA_MAX
+ 1];
258 struct prefix_ipv6 src_p
= {};
261 char anyaddr
[16] = {0};
263 int proto
= ZEBRA_ROUTE_KERNEL
;
268 uint8_t distance
= 0;
273 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
274 void *src
= NULL
; /* IPv6 srcdest source prefix */
275 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
279 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
281 switch (rtm
->rtm_type
) {
285 bh_type
= BLACKHOLE_NULL
;
287 case RTN_UNREACHABLE
:
288 bh_type
= BLACKHOLE_REJECT
;
291 bh_type
= BLACKHOLE_ADMINPROHIB
;
297 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
299 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
300 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
301 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
305 memset(tb
, 0, sizeof tb
);
306 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
308 if (rtm
->rtm_flags
& RTM_F_CLONED
)
310 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
312 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
315 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
316 && h
->nlmsg_type
== RTM_NEWROUTE
) {
317 if (IS_ZEBRA_DEBUG_KERNEL
)
318 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
323 /* We don't care about change notifications for the MPLS table. */
324 /* TODO: Revisit this. */
325 if (rtm
->rtm_family
== AF_MPLS
)
328 /* Table corresponding to route. */
330 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
332 table
= rtm
->rtm_table
;
335 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
336 if (vrf_id
== VRF_DEFAULT
) {
337 if (!is_zebra_valid_kernel_table(table
)
338 && !is_zebra_main_routing_table(table
))
342 /* Route which inserted by Zebra. */
343 if (is_selfroute(rtm
->rtm_protocol
)) {
344 flags
|= ZEBRA_FLAG_SELFROUTE
;
345 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
348 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
351 dest
= RTA_DATA(tb
[RTA_DST
]);
356 src
= RTA_DATA(tb
[RTA_SRC
]);
361 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
364 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
366 if (tb
[RTA_PRIORITY
])
367 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
369 #if defined(SUPPORT_REALMS)
371 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
374 if (tb
[RTA_METRICS
]) {
375 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
377 memset(mxrta
, 0, sizeof mxrta
);
378 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
379 RTA_PAYLOAD(tb
[RTA_METRICS
]));
382 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
385 if (rtm
->rtm_family
== AF_INET
) {
387 memcpy(&p
.u
.prefix4
, dest
, 4);
388 p
.prefixlen
= rtm
->rtm_dst_len
;
391 0; // Forces debug below to not display anything
392 } else if (rtm
->rtm_family
== AF_INET6
) {
394 memcpy(&p
.u
.prefix6
, dest
, 16);
395 p
.prefixlen
= rtm
->rtm_dst_len
;
397 src_p
.family
= AF_INET6
;
398 memcpy(&src_p
.prefix
, src
, 16);
399 src_p
.prefixlen
= rtm
->rtm_src_len
;
402 if (rtm
->rtm_src_len
!= 0) {
403 char buf
[PREFIX_STRLEN
];
405 "unsupported IPv[4|6] sourcedest route (dest %s vrf %u)",
406 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
411 * For ZEBRA_ROUTE_KERNEL types:
413 * The metric/priority of the route received from the kernel
414 * is a 32 bit number. We are going to interpret the high
415 * order byte as the Admin Distance and the low order 3 bytes
418 * This will allow us to do two things:
419 * 1) Allow the creation of kernel routes that can be
420 * overridden by zebra.
421 * 2) Allow the old behavior for 'most' kernel route types
422 * if a user enters 'ip route ...' v4 routes get a metric
423 * of 0 and v6 routes get a metric of 1024. Both of these
424 * values will end up with a admin distance of 0, which
425 * will cause them to win for the purposes of zebra.
427 if (proto
== ZEBRA_ROUTE_KERNEL
) {
428 distance
= (metric
>> 24) & 0xFF;
429 metric
= (metric
& 0x00FFFFFF);
432 if (IS_ZEBRA_DEBUG_KERNEL
) {
433 char buf
[PREFIX_STRLEN
];
434 char buf2
[PREFIX_STRLEN
];
435 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
436 nl_msg_type_to_str(h
->nlmsg_type
),
437 prefix2str(&p
, buf
, sizeof(buf
)),
438 src_p
.prefixlen
? " from " : "",
440 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
442 vrf_id
, table
, metric
, distance
);
446 if (rtm
->rtm_family
== AF_INET6
)
449 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
450 struct interface
*ifp
;
451 vrf_id_t nh_vrf_id
= vrf_id
;
453 if (!tb
[RTA_MULTIPATH
]) {
455 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
457 memset(&nh
, 0, sizeof(nh
));
459 if (bh_type
== BLACKHOLE_UNSPEC
) {
461 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
462 else if (index
&& gate
)
465 ? NEXTHOP_TYPE_IPV4_IFINDEX
466 : NEXTHOP_TYPE_IPV6_IFINDEX
;
467 else if (!index
&& gate
)
468 nh
.type
= (afi
== AFI_IP
)
472 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
473 nh
.bh_type
= bh_type
;
476 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
477 nh
.bh_type
= bh_type
;
481 memcpy(&nh
.src
, prefsrc
, sz
);
483 memcpy(&nh
.gate
, gate
, sz
);
486 ifp
= if_lookup_by_index(index
, VRF_UNKNOWN
);
488 nh_vrf_id
= ifp
->vrf_id
;
490 nh
.vrf_id
= nh_vrf_id
;
492 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
493 NULL
, &nh
, table
, metric
, mtu
, distance
, tag
);
495 /* This is a multipath route */
497 struct route_entry
*re
;
498 struct rtnexthop
*rtnh
=
499 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
501 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
503 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
505 re
->distance
= distance
;
512 re
->uptime
= time(NULL
);
517 if (len
< (int)sizeof(*rtnh
)
518 || rtnh
->rtnh_len
> len
)
521 index
= rtnh
->rtnh_ifindex
;
524 * Yes we are looking this up
525 * for every nexthop and just
526 * using the last one looked
529 ifp
= if_lookup_by_index(index
,
532 nh_vrf_id
= ifp
->vrf_id
;
535 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
538 nh_vrf_id
= VRF_DEFAULT
;
544 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
545 memset(tb
, 0, sizeof(tb
));
546 netlink_parse_rtattr(
547 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
548 rtnh
->rtnh_len
- sizeof(*rtnh
));
555 if (rtm
->rtm_family
== AF_INET
) {
557 route_entry_nexthop_ipv4_ifindex_add(
562 route_entry_nexthop_ipv4_add(
566 } else if (rtm
->rtm_family
569 route_entry_nexthop_ipv6_ifindex_add(
573 route_entry_nexthop_ipv6_add(
578 route_entry_nexthop_ifindex_add(
579 re
, index
, nh_vrf_id
);
581 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
582 rtnh
= RTNH_NEXT(rtnh
);
585 zserv_nexthop_num_warn(__func__
,
586 (const struct prefix
*)&p
,
588 if (re
->nexthop_num
== 0)
591 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
,
595 if (!tb
[RTA_MULTIPATH
]) {
597 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
599 memset(&nh
, 0, sizeof(nh
));
600 if (bh_type
== BLACKHOLE_UNSPEC
) {
602 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
603 else if (index
&& gate
)
606 ? NEXTHOP_TYPE_IPV4_IFINDEX
607 : NEXTHOP_TYPE_IPV6_IFINDEX
;
608 else if (!index
&& gate
)
609 nh
.type
= (afi
== AFI_IP
)
613 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
614 nh
.bh_type
= BLACKHOLE_UNSPEC
;
617 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
618 nh
.bh_type
= bh_type
;
622 memcpy(&nh
.gate
, gate
, sz
);
623 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
624 &p
, NULL
, &nh
, table
, metric
, true);
626 /* XXX: need to compare the entire list of nexthops
627 * here for NLM_F_APPEND stupidity */
628 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
629 &p
, NULL
, NULL
, table
, metric
, true);
636 static struct mcast_route_data
*mroute
= NULL
;
638 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
639 ns_id_t ns_id
, int startup
)
643 struct rtattr
*tb
[RTA_MAX
+ 1];
644 struct mcast_route_data
*m
;
645 struct mcast_route_data mr
;
652 char oif_list
[256] = "\0";
659 memset(&mr
, 0, sizeof(mr
));
665 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
667 memset(tb
, 0, sizeof tb
);
668 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
671 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
673 table
= rtm
->rtm_table
;
675 vrf
= vrf_lookup_by_table(table
, ns_id
);
678 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
681 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
684 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
686 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
687 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
689 if (tb
[RTA_MULTIPATH
]) {
690 struct rtnexthop
*rtnh
=
691 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
693 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
695 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
698 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
701 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
702 rtnh
= RTNH_NEXT(rtnh
);
706 if (IS_ZEBRA_DEBUG_KERNEL
) {
707 struct interface
*ifp
;
708 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
709 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
710 for (count
= 0; count
< oif_count
; count
++) {
711 ifp
= if_lookup_by_index(oif
[count
], vrf
);
714 sprintf(temp
, "%s ", ifp
->name
);
715 strcat(oif_list
, temp
);
717 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
718 ifp
= if_lookup_by_index(iif
, vrf
);
720 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
721 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
722 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
727 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
734 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
735 /* If this is not route add/delete message print warning. */
736 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
740 /* Connected route. */
741 if (IS_ZEBRA_DEBUG_KERNEL
)
742 zlog_debug("%s %s %s proto %s NS %u",
743 nl_msg_type_to_str(h
->nlmsg_type
),
744 nl_family_to_str(rtm
->rtm_family
),
745 nl_rttype_to_str(rtm
->rtm_type
),
746 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
748 /* We don't care about change notifications for the MPLS table. */
749 /* TODO: Revisit this. */
750 if (rtm
->rtm_family
== AF_MPLS
)
753 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
755 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
758 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
762 if (rtm
->rtm_type
== RTN_MULTICAST
)
763 netlink_route_change_read_multicast(h
, ns_id
, startup
);
765 netlink_route_change_read_unicast(h
, ns_id
, startup
);
769 /* Request for specific route information from the kernel */
770 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
777 /* Form the request, specifying filter (rtattr) if needed. */
778 memset(&req
, 0, sizeof(req
));
779 req
.n
.nlmsg_type
= type
;
780 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
781 req
.rtm
.rtm_family
= family
;
783 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
786 /* Routing table read function using netlink interface. Only called
788 int netlink_route_read(struct zebra_ns
*zns
)
792 /* Get IPv4 routing table. */
793 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
796 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
797 &zns
->netlink_cmd
, zns
, 0, 1);
801 /* Get IPv6 routing table. */
802 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
805 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
806 &zns
->netlink_cmd
, zns
, 0, 1);
813 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
815 struct nlmsghdr
*nlmsg
,
816 size_t req_size
, int bytelen
,
817 struct nexthop
*nexthop
)
819 if (route_family
== AF_MPLS
) {
820 struct gw_family_t gw_fam
;
822 gw_fam
.family
= gw_family
;
823 if (gw_family
== AF_INET
)
824 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
826 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
827 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
830 if (gw_family
== AF_INET
)
831 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
832 &nexthop
->gate
.ipv4
, bytelen
);
834 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
835 &nexthop
->gate
.ipv6
, bytelen
);
839 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
842 struct rtnexthop
*rtnh
,
843 size_t req_size
, int bytelen
,
844 struct nexthop
*nexthop
)
846 if (route_family
== AF_MPLS
) {
847 struct gw_family_t gw_fam
;
849 gw_fam
.family
= gw_family
;
850 if (gw_family
== AF_INET
)
851 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
853 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
854 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
856 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
858 if (gw_family
== AF_INET
)
859 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
860 &nexthop
->gate
.ipv4
, bytelen
);
862 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
863 &nexthop
->gate
.ipv6
, bytelen
);
864 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
868 /* This function takes a nexthop as argument and adds
869 * the appropriate netlink attributes to an existing
872 * @param routedesc: Human readable description of route type
873 * (direct/recursive, single-/multipath)
874 * @param bytelen: Length of addresses in bytes.
875 * @param nexthop: Nexthop information
876 * @param nlmsg: nlmsghdr structure to fill in.
877 * @param req_size: The size allocated for the message.
879 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
880 struct nexthop
*nexthop
,
881 struct nlmsghdr
*nlmsg
,
883 size_t req_size
, int cmd
)
885 struct mpls_label_stack
*nh_label
;
886 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
891 * label_buf is *only* currently used within debugging.
892 * As such when we assign it we are guarding it inside
893 * a debug test. If you want to change this make sure
894 * you fix this assumption
899 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
902 nh_label
= nh
->nh_label
;
903 if (!nh_label
|| !nh_label
->num_labels
)
906 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
907 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
910 if (IS_ZEBRA_DEBUG_KERNEL
) {
912 sprintf(label_buf
, "label %u",
915 sprintf(label_buf1
, "/%u",
917 strlcat(label_buf
, label_buf1
,
922 out_lse
[num_labels
] =
923 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
929 /* Set the BoS bit */
930 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
932 if (rtmsg
->rtm_family
== AF_MPLS
)
933 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
934 num_labels
* sizeof(mpls_lse_t
));
937 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
939 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
941 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
942 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
943 num_labels
* sizeof(mpls_lse_t
));
944 addattr_nest_end(nlmsg
, nest
);
948 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
949 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
951 if (rtmsg
->rtm_family
== AF_INET
952 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
953 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
954 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
955 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
956 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
958 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
959 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
960 &nexthop
->rmap_src
.ipv4
, bytelen
);
961 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
962 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
963 &nexthop
->src
.ipv4
, bytelen
);
965 if (IS_ZEBRA_DEBUG_KERNEL
)
967 " 5549: _netlink_route_build_singlepath() (%s): "
968 "nexthop via %s %s if %u(%u)",
969 routedesc
, ipv4_ll_buf
, label_buf
,
970 nexthop
->ifindex
, nexthop
->vrf_id
);
974 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
975 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
976 /* Send deletes to the kernel without specifying the next-hop */
977 if (cmd
!= RTM_DELROUTE
)
978 _netlink_route_nl_add_gateway_info(
979 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
982 if (cmd
== RTM_NEWROUTE
) {
983 if (nexthop
->rmap_src
.ipv4
.s_addr
)
984 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
985 &nexthop
->rmap_src
.ipv4
, bytelen
);
986 else if (nexthop
->src
.ipv4
.s_addr
)
987 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
988 &nexthop
->src
.ipv4
, bytelen
);
991 if (IS_ZEBRA_DEBUG_KERNEL
)
993 "netlink_route_multipath() (%s): "
994 "nexthop via %s %s if %u(%u)",
995 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
996 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
999 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1000 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1001 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1002 nlmsg
, req_size
, bytelen
,
1005 if (cmd
== RTM_NEWROUTE
) {
1006 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1007 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1008 &nexthop
->rmap_src
.ipv6
, bytelen
);
1009 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1010 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1011 &nexthop
->src
.ipv6
, bytelen
);
1014 if (IS_ZEBRA_DEBUG_KERNEL
)
1016 "netlink_route_multipath() (%s): "
1017 "nexthop via %s %s if %u(%u)",
1018 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1019 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1023 * We have the ifindex so we should always send it
1024 * This is especially useful if we are doing route
1027 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1028 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1030 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1031 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1032 if (cmd
== RTM_NEWROUTE
) {
1033 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1034 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1035 &nexthop
->rmap_src
.ipv4
, bytelen
);
1036 else if (nexthop
->src
.ipv4
.s_addr
)
1037 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1038 &nexthop
->src
.ipv4
, bytelen
);
1041 if (IS_ZEBRA_DEBUG_KERNEL
)
1043 "netlink_route_multipath() (%s): "
1044 "nexthop via if %u(%u)",
1045 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1048 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1049 if (cmd
== RTM_NEWROUTE
) {
1050 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1051 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1052 &nexthop
->rmap_src
.ipv6
, bytelen
);
1053 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1054 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1055 &nexthop
->src
.ipv6
, bytelen
);
1058 if (IS_ZEBRA_DEBUG_KERNEL
)
1060 "netlink_route_multipath() (%s): "
1061 "nexthop via if %u(%u)",
1062 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1066 /* This function takes a nexthop as argument and
1067 * appends to the given rtattr/rtnexthop pair the
1068 * representation of the nexthop. If the nexthop
1069 * defines a preferred source, the src parameter
1070 * will be modified to point to that src, otherwise
1071 * it will be kept unmodified.
1073 * @param routedesc: Human readable description of route type
1074 * (direct/recursive, single-/multipath)
1075 * @param bytelen: Length of addresses in bytes.
1076 * @param nexthop: Nexthop information
1077 * @param rta: rtnetlink attribute structure
1078 * @param rtnh: pointer to an rtnetlink nexthop structure
1079 * @param src: pointer pointing to a location where
1080 * the prefsrc should be stored.
1082 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1083 struct nexthop
*nexthop
,
1085 struct rtnexthop
*rtnh
,
1086 struct rtmsg
*rtmsg
,
1089 struct mpls_label_stack
*nh_label
;
1090 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1092 char label_buf
[256];
1094 rtnh
->rtnh_len
= sizeof(*rtnh
);
1095 rtnh
->rtnh_flags
= 0;
1096 rtnh
->rtnh_hops
= 0;
1097 rta
->rta_len
+= rtnh
->rtnh_len
;
1100 * label_buf is *only* currently used within debugging.
1101 * As such when we assign it we are guarding it inside
1102 * a debug test. If you want to change this make sure
1103 * you fix this assumption
1105 label_buf
[0] = '\0';
1108 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1109 char label_buf1
[20];
1111 nh_label
= nh
->nh_label
;
1112 if (!nh_label
|| !nh_label
->num_labels
)
1115 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1116 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1119 if (IS_ZEBRA_DEBUG_KERNEL
) {
1121 sprintf(label_buf
, "label %u",
1122 nh_label
->label
[i
]);
1124 sprintf(label_buf1
, "/%u",
1125 nh_label
->label
[i
]);
1126 strlcat(label_buf
, label_buf1
,
1131 out_lse
[num_labels
] =
1132 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1138 /* Set the BoS bit */
1139 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1141 if (rtmsg
->rtm_family
== AF_MPLS
) {
1142 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1144 num_labels
* sizeof(mpls_lse_t
));
1146 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1148 struct rtattr
*nest
;
1149 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1150 int len
= rta
->rta_len
;
1152 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1153 &encap
, sizeof(uint16_t));
1154 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1155 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1157 num_labels
* sizeof(mpls_lse_t
));
1158 rta_nest_end(rta
, nest
);
1159 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1163 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1164 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1166 if (rtmsg
->rtm_family
== AF_INET
1167 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1168 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1170 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1171 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1173 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1174 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1176 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1177 *src
= &nexthop
->rmap_src
;
1178 else if (nexthop
->src
.ipv4
.s_addr
)
1179 *src
= &nexthop
->src
;
1181 if (IS_ZEBRA_DEBUG_KERNEL
)
1183 " 5549: netlink_route_build_multipath() (%s): "
1184 "nexthop via %s %s if %u",
1185 routedesc
, ipv4_ll_buf
, label_buf
,
1190 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1191 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1192 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1193 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1195 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1196 *src
= &nexthop
->rmap_src
;
1197 else if (nexthop
->src
.ipv4
.s_addr
)
1198 *src
= &nexthop
->src
;
1200 if (IS_ZEBRA_DEBUG_KERNEL
)
1202 "netlink_route_multipath() (%s): "
1203 "nexthop via %s %s if %u",
1204 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1205 label_buf
, nexthop
->ifindex
);
1207 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1208 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1209 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1210 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1213 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1214 *src
= &nexthop
->rmap_src
;
1215 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1216 *src
= &nexthop
->src
;
1218 if (IS_ZEBRA_DEBUG_KERNEL
)
1220 "netlink_route_multipath() (%s): "
1221 "nexthop via %s %s if %u",
1222 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1223 label_buf
, nexthop
->ifindex
);
1227 * We have figured out the ifindex so we should always send it
1228 * This is especially useful if we are doing route
1231 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1232 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1235 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1236 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1237 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1238 *src
= &nexthop
->rmap_src
;
1239 else if (nexthop
->src
.ipv4
.s_addr
)
1240 *src
= &nexthop
->src
;
1242 if (IS_ZEBRA_DEBUG_KERNEL
)
1244 "netlink_route_multipath() (%s): "
1245 "nexthop via if %u",
1246 routedesc
, nexthop
->ifindex
);
1250 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1251 zebra_nhlfe_t
*nhlfe
,
1252 struct nlmsghdr
*nlmsg
,
1253 struct rtmsg
*rtmsg
,
1254 size_t req_size
, int cmd
)
1259 family
= NHLFE_FAMILY(nhlfe
);
1260 bytelen
= (family
== AF_INET
? 4 : 16);
1261 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1262 nlmsg
, rtmsg
, req_size
, cmd
);
1267 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1268 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1269 struct rtmsg
*rtmsg
, union g_addr
**src
)
1274 family
= NHLFE_FAMILY(nhlfe
);
1275 bytelen
= (family
== AF_INET
? 4 : 16);
1276 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1281 /* Log debug information for netlink_route_multipath
1282 * if debug logging is enabled.
1284 * @param cmd: Netlink command which is to be processed
1285 * @param p: Prefix for which the change is due
1286 * @param family: Address family which the change concerns
1287 * @param zvrf: The vrf we are in
1288 * @param tableid: The table we are working on
1290 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1291 int family
, vrf_id_t vrfid
,
1294 if (IS_ZEBRA_DEBUG_KERNEL
) {
1295 char buf
[PREFIX_STRLEN
];
1297 "netlink_route_multipath(): %s %s vrf %u(%u)",
1298 nl_msg_type_to_str(cmd
),
1299 prefix2str(p
, buf
, sizeof(buf
)),
1304 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1306 if (IS_ZEBRA_DEBUG_KERNEL
)
1307 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1308 nl_msg_type_to_str(cmd
), label
);
1311 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1312 int llalen
, ns_id_t ns_id
)
1320 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1322 memset(&req
, 0, sizeof(req
));
1324 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1325 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1326 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1327 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1329 req
.ndm
.ndm_family
= AF_INET
;
1330 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1331 req
.ndm
.ndm_ifindex
= ifindex
;
1332 req
.ndm
.ndm_type
= RTN_UNICAST
;
1334 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1335 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1337 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1341 /* Routing table change via netlink interface. */
1342 /* Update flag indicates whether this is a "replace" or not. */
1343 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1344 const struct prefix
*src_p
,
1345 struct route_entry
*re
,
1349 struct sockaddr_nl snl
;
1350 struct nexthop
*nexthop
= NULL
;
1351 unsigned int nexthop_num
;
1352 int family
= PREFIX_FAMILY(p
);
1353 const char *routedesc
;
1360 char buf
[NL_PKT_BUF_SIZE
];
1363 struct zebra_ns
*zns
;
1364 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1367 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1369 bytelen
= (family
== AF_INET
? 4 : 16);
1371 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1372 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1373 if ((cmd
== RTM_NEWROUTE
) && update
)
1374 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1375 req
.n
.nlmsg_type
= cmd
;
1376 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1378 req
.r
.rtm_family
= family
;
1379 req
.r
.rtm_dst_len
= p
->prefixlen
;
1380 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1381 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1382 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1385 * blackhole routes are not RTN_UNICAST, they are
1386 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1387 * so setting this value as a RTN_UNICAST would
1388 * cause the route lookup of just the prefix
1389 * to fail. So no need to specify this for
1390 * the RTM_DELROUTE case
1392 if (cmd
!= RTM_DELROUTE
)
1393 req
.r
.rtm_type
= RTN_UNICAST
;
1395 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1397 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1401 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1403 * either by the kernel or by zebra. Its purely for calculating best
1405 * by the routing protocol and for communicating with protocol peers.
1407 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1408 #if defined(SUPPORT_REALMS)
1409 if (re
->tag
> 0 && re
->tag
<= 255)
1410 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1412 /* Table corresponding to this route. */
1413 if (re
->table
< 256)
1414 req
.r
.rtm_table
= re
->table
;
1416 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1417 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1420 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1423 * If we are not updating the route and we have received
1424 * a route delete, then all we need to fill in is the
1425 * prefix information to tell the kernel to schwack
1428 if (!update
&& cmd
== RTM_DELROUTE
)
1431 if (re
->mtu
|| re
->nexthop_mtu
) {
1432 char buf
[NL_PKT_BUF_SIZE
];
1433 struct rtattr
*rta
= (void *)buf
;
1434 uint32_t mtu
= re
->mtu
;
1435 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1436 mtu
= re
->nexthop_mtu
;
1437 rta
->rta_type
= RTA_METRICS
;
1438 rta
->rta_len
= RTA_LENGTH(0);
1439 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1440 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1444 /* Count overall nexthops so we can decide whether to use singlepath
1445 * or multipath case. */
1447 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1448 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1450 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1452 if (cmd
== RTM_DELROUTE
1453 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1459 /* Singlepath case. */
1460 if (nexthop_num
== 1 || multipath_num
== 1) {
1462 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1464 * So we want to cover 2 types of blackhole
1466 * 1) A normal blackhole route( ala from a static
1468 * 2) A recursively resolved blackhole route
1470 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1471 switch (nexthop
->bh_type
) {
1472 case BLACKHOLE_ADMINPROHIB
:
1473 req
.r
.rtm_type
= RTN_PROHIBIT
;
1475 case BLACKHOLE_REJECT
:
1476 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1479 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1484 if (CHECK_FLAG(nexthop
->flags
,
1485 NEXTHOP_FLAG_RECURSIVE
)) {
1487 if (family
== AF_INET
) {
1488 if (nexthop
->rmap_src
.ipv4
1495 } else if (nexthop
->src
.ipv4
1503 } else if (family
== AF_INET6
) {
1504 if (!IN6_IS_ADDR_UNSPECIFIED(
1512 !IN6_IS_ADDR_UNSPECIFIED(
1525 if ((cmd
== RTM_NEWROUTE
1526 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1527 || (cmd
== RTM_DELROUTE
1528 && CHECK_FLAG(nexthop
->flags
,
1529 NEXTHOP_FLAG_FIB
))) {
1530 routedesc
= nexthop
->rparent
1531 ? "recursive, single-path"
1534 _netlink_route_build_singlepath(
1535 routedesc
, bytelen
, nexthop
, &req
.n
,
1536 &req
.r
, sizeof req
, cmd
);
1541 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1542 if (family
== AF_INET
)
1543 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1544 &src
.ipv4
, bytelen
);
1545 else if (family
== AF_INET6
)
1546 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1547 &src
.ipv6
, bytelen
);
1550 char buf
[NL_PKT_BUF_SIZE
];
1551 struct rtattr
*rta
= (void *)buf
;
1552 struct rtnexthop
*rtnh
;
1553 union g_addr
*src1
= NULL
;
1555 rta
->rta_type
= RTA_MULTIPATH
;
1556 rta
->rta_len
= RTA_LENGTH(0);
1557 rtnh
= RTA_DATA(rta
);
1560 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1561 if (nexthop_num
>= multipath_num
)
1564 if (CHECK_FLAG(nexthop
->flags
,
1565 NEXTHOP_FLAG_RECURSIVE
)) {
1566 /* This only works for IPv4 now */
1568 if (family
== AF_INET
) {
1569 if (nexthop
->rmap_src
.ipv4
1576 } else if (nexthop
->src
.ipv4
1584 } else if (family
== AF_INET6
) {
1585 if (!IN6_IS_ADDR_UNSPECIFIED(
1593 !IN6_IS_ADDR_UNSPECIFIED(
1606 if ((cmd
== RTM_NEWROUTE
1607 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1608 || (cmd
== RTM_DELROUTE
1609 && CHECK_FLAG(nexthop
->flags
,
1610 NEXTHOP_FLAG_FIB
))) {
1611 routedesc
= nexthop
->rparent
1612 ? "recursive, multipath"
1616 _netlink_route_build_multipath(
1617 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1619 rtnh
= RTNH_NEXT(rtnh
);
1621 if (!setsrc
&& src1
) {
1622 if (family
== AF_INET
)
1623 src
.ipv4
= src1
->ipv4
;
1624 else if (family
== AF_INET6
)
1625 src
.ipv6
= src1
->ipv6
;
1631 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1632 if (family
== AF_INET
)
1633 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1634 &src
.ipv4
, bytelen
);
1635 else if (family
== AF_INET6
)
1636 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1637 &src
.ipv6
, bytelen
);
1638 if (IS_ZEBRA_DEBUG_KERNEL
)
1639 zlog_debug("Setting source");
1642 if (rta
->rta_len
> RTA_LENGTH(0))
1643 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1644 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1647 /* If there is no useful nexthop then return. */
1648 if (nexthop_num
== 0) {
1649 if (IS_ZEBRA_DEBUG_KERNEL
)
1651 "netlink_route_multipath(): No useful nexthop.");
1657 /* Destination netlink address. */
1658 memset(&snl
, 0, sizeof snl
);
1659 snl
.nl_family
= AF_NETLINK
;
1661 /* Talk to netlink socket. */
1662 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1666 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1669 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1677 struct zebra_ns
*zns
;
1680 memset(&req
, 0, sizeof(req
));
1682 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1683 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1684 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1686 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1687 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1689 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1690 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1691 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1692 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1693 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1695 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1696 &zns
->netlink_cmd
, zns
, 0);
1702 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1703 const struct prefix
*p
,
1704 const struct prefix
*src_p
,
1705 struct route_entry
*old
,
1706 struct route_entry
*new)
1713 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1714 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1715 new, (old
) ? 1 : 0);
1718 * So v6 route replace semantics are not in
1719 * the kernel at this point as I understand it.
1720 * So let's do a delete than an add.
1721 * In the future once v6 route replace semantics
1722 * are in we can figure out what to do here to
1723 * allow working with old and new kernels.
1725 * I'm also intentionally ignoring the failure case
1726 * of the route delete. If that happens yeah we're
1730 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1732 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1735 kernel_route_rib_pass_fail(rn
, p
, new,
1736 (!ret
) ? DP_INSTALL_SUCCESS
1737 : DP_INSTALL_FAILURE
);
1738 return DP_REQUEST_SUCCESS
;
1742 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1744 kernel_route_rib_pass_fail(rn
, p
, old
,
1745 (!ret
) ? DP_DELETE_SUCCESS
1746 : DP_DELETE_FAILURE
);
1749 return DP_REQUEST_SUCCESS
;
1752 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1753 int llalen
, ns_id_t ns_id
)
1755 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1756 addr
, lla
, llalen
, ns_id
);
1760 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1761 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1763 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1764 struct in_addr
*vtep_ip
, int cmd
)
1766 struct zebra_ns
*zns
;
1772 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1773 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1776 memset(&req
, 0, sizeof(req
));
1778 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1779 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1780 if (cmd
== RTM_NEWNEIGH
)
1781 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1782 req
.n
.nlmsg_type
= cmd
;
1783 req
.ndm
.ndm_family
= PF_BRIDGE
;
1784 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1785 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1788 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1789 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1790 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1792 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1797 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1799 * a "flood" MAC FDB entry.
1801 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1803 if (IS_ZEBRA_DEBUG_VXLAN
)
1804 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1805 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1807 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1811 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1812 * deleting the "flood" MAC FDB entry.
1814 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1816 if (IS_ZEBRA_DEBUG_VXLAN
)
1818 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1819 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1821 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1825 #define NDA_RTA(r) \
1826 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1829 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1832 struct interface
*ifp
;
1833 struct zebra_if
*zif
;
1834 struct rtattr
*tb
[NDA_MAX
+ 1];
1835 struct interface
*br_if
;
1838 struct prefix vtep_ip
;
1839 int vid_present
= 0, dst_present
= 0;
1840 char buf
[ETHER_ADDR_STRLEN
];
1845 ndm
= NLMSG_DATA(h
);
1847 /* We only process macfdb notifications if EVPN is enabled */
1848 if (!is_evpn_enabled())
1851 /* The interface should exist. */
1852 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1854 if (!ifp
|| !ifp
->info
)
1857 /* The interface should be something we're interested in. */
1858 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1861 /* Drop "permanent" entries. */
1862 if (ndm
->ndm_state
& NUD_PERMANENT
)
1865 zif
= (struct zebra_if
*)ifp
->info
;
1866 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1867 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1868 nl_msg_type_to_str(h
->nlmsg_type
),
1869 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1870 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1874 /* Parse attributes and extract fields of interest. */
1875 memset(tb
, 0, sizeof tb
);
1876 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1878 if (!tb
[NDA_LLADDR
]) {
1879 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1880 nl_msg_type_to_str(h
->nlmsg_type
),
1881 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1882 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1886 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1888 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1889 nl_msg_type_to_str(h
->nlmsg_type
),
1890 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1891 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1892 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1896 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1898 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1900 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1901 sprintf(vid_buf
, " VLAN %u", vid
);
1905 /* TODO: Only IPv4 supported now. */
1907 vtep_ip
.family
= AF_INET
;
1908 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1909 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1911 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1914 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1916 if (IS_ZEBRA_DEBUG_KERNEL
)
1917 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1918 nl_msg_type_to_str(h
->nlmsg_type
),
1919 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1920 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1921 sticky
? "sticky " : "",
1922 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1923 dst_present
? dst_buf
: "");
1925 if (filter_vlan
&& vid
!= filter_vlan
)
1928 /* If add or update, do accordingly if learnt on a "local" interface; if
1929 * the notification is over VxLAN, this has to be related to
1931 * so perform an implicit delete of any local entry (if it exists).
1933 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1934 /* Drop "permanent" entries. */
1935 if (ndm
->ndm_state
& NUD_PERMANENT
)
1938 if (IS_ZEBRA_IF_VXLAN(ifp
))
1939 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1942 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1946 /* This is a delete notification.
1947 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1948 * 2. For a MAC over "local" interface, delete the mac
1949 * Note: We will get notifications from both bridge driver and VxLAN
1951 * Ignore the notification from VxLan driver as it is also generated
1952 * when mac moves from remote to local.
1957 if (IS_ZEBRA_IF_VXLAN(ifp
))
1958 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1961 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1964 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1969 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1972 /* Length validity. */
1973 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1977 /* We are interested only in AF_BRIDGE notifications. */
1978 ndm
= NLMSG_DATA(h
);
1979 if (ndm
->ndm_family
!= AF_BRIDGE
)
1982 return netlink_macfdb_change(h
, len
, ns_id
);
1985 /* Request for MAC FDB information from the kernel */
1986 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
1987 ifindex_t master_ifindex
)
1991 struct ifinfomsg ifm
;
1995 /* Form the request, specifying filter (rtattr) if needed. */
1996 memset(&req
, 0, sizeof(req
));
1997 req
.n
.nlmsg_type
= type
;
1998 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1999 req
.ifm
.ifi_family
= family
;
2001 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2003 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2007 * MAC forwarding database read using netlink interface. This is invoked
2010 int netlink_macfdb_read(struct zebra_ns
*zns
)
2014 /* Get bridge FDB table. */
2015 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2018 /* We are reading entire table. */
2020 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2027 * MAC forwarding database read using netlink interface. This is for a
2028 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2030 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2031 struct interface
*br_if
)
2033 struct zebra_if
*br_zif
;
2034 struct zebra_if
*zif
;
2035 struct zebra_l2info_vxlan
*vxl
;
2039 /* Save VLAN we're filtering on, if needed. */
2040 br_zif
= (struct zebra_if
*)br_if
->info
;
2041 zif
= (struct zebra_if
*)ifp
->info
;
2042 vxl
= &zif
->l2info
.vxl
;
2043 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2044 filter_vlan
= vxl
->access_vlan
;
2046 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2048 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2052 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2055 /* Reset VLAN filter. */
2060 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2061 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2062 int local
, int cmd
, uint8_t sticky
)
2064 struct zebra_ns
*zns
;
2071 struct zebra_if
*zif
;
2072 struct interface
*br_if
;
2073 struct zebra_if
*br_zif
;
2074 char buf
[ETHER_ADDR_STRLEN
];
2075 int vid_present
= 0, dst_present
= 0;
2078 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2082 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2083 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2084 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2089 memset(&req
, 0, sizeof(req
));
2091 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2092 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2093 if (cmd
== RTM_NEWNEIGH
)
2094 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2095 req
.n
.nlmsg_type
= cmd
;
2096 req
.ndm
.ndm_family
= AF_BRIDGE
;
2097 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2098 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2101 req
.ndm
.ndm_state
|= NUD_NOARP
;
2103 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2105 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2106 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2108 dst_alen
= 4; // TODO: hardcoded
2109 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2111 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2113 br_zif
= (struct zebra_if
*)br_if
->info
;
2114 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2115 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2117 sprintf(vid_buf
, " VLAN %u", vid
);
2119 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2121 if (IS_ZEBRA_DEBUG_KERNEL
)
2122 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2123 nl_msg_type_to_str(cmd
),
2124 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2125 ifp
->ifindex
, vid_present
? vid_buf
: "",
2126 sticky
? "sticky " : "",
2127 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2128 dst_present
? dst_buf
: "");
2130 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2135 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2138 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2141 struct interface
*ifp
;
2142 struct zebra_if
*zif
;
2143 struct rtattr
*tb
[NDA_MAX
+ 1];
2144 struct interface
*link_if
;
2147 char buf
[ETHER_ADDR_STRLEN
];
2148 char buf2
[INET6_ADDRSTRLEN
];
2149 int mac_present
= 0;
2150 uint8_t ext_learned
;
2152 ndm
= NLMSG_DATA(h
);
2154 /* The interface should exist. */
2155 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2157 if (!ifp
|| !ifp
->info
)
2160 zif
= (struct zebra_if
*)ifp
->info
;
2162 /* Parse attributes and extract fields of interest. */
2163 memset(tb
, 0, sizeof tb
);
2164 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2167 zlog_warn("%s family %s IF %s(%u) - no DST",
2168 nl_msg_type_to_str(h
->nlmsg_type
),
2169 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2174 memset(&ip
, 0, sizeof(struct ipaddr
));
2175 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2176 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2178 /* Drop some "permanent" entries. */
2179 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2180 char buf
[16] = "169.254.0.1";
2181 struct in_addr ipv4_ll
;
2183 if (ndm
->ndm_family
!= AF_INET
)
2186 if (!zif
->v6_2_v4_ll_neigh_entry
)
2189 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2192 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2193 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2196 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2197 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2201 /* The neighbor is present on an SVI. From this, we locate the
2203 * bridge because we're only interested in neighbors on a VxLAN bridge.
2204 * The bridge is located based on the nature of the SVI:
2205 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2207 * and is linked to the bridge
2208 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2212 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2213 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2217 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2222 memset(&mac
, 0, sizeof(struct ethaddr
));
2223 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2224 if (tb
[NDA_LLADDR
]) {
2225 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2227 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2228 nl_msg_type_to_str(h
->nlmsg_type
),
2229 nl_family_to_str(ndm
->ndm_family
),
2230 ifp
->name
, ndm
->ndm_ifindex
,
2231 (unsigned long)RTA_PAYLOAD(
2237 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2240 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2242 if (IS_ZEBRA_DEBUG_KERNEL
)
2244 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2245 nl_msg_type_to_str(h
->nlmsg_type
),
2246 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2248 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2250 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2252 ndm
->ndm_state
, ndm
->ndm_flags
);
2254 /* If the neighbor state is valid for use, process as an add or
2256 * else process as a delete. Note that the delete handling may
2258 * in re-adding the neighbor if it is a valid "remote" neighbor.
2260 if (ndm
->ndm_state
& NUD_VALID
)
2261 return zebra_vxlan_handle_kernel_neigh_update(
2262 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2265 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2268 if (IS_ZEBRA_DEBUG_KERNEL
)
2269 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2270 nl_msg_type_to_str(h
->nlmsg_type
),
2271 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2273 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2275 /* Process the delete - it may result in re-adding the neighbor if it is
2276 * a valid "remote" neighbor.
2278 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2281 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2286 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2289 /* Length validity. */
2290 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2294 /* We are interested only in AF_INET or AF_INET6 notifications. */
2295 ndm
= NLMSG_DATA(h
);
2296 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2299 return netlink_neigh_change(h
, len
);
2302 /* Request for IP neighbor information from the kernel */
2303 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2312 /* Form the request, specifying filter (rtattr) if needed. */
2313 memset(&req
, 0, sizeof(req
));
2314 req
.n
.nlmsg_type
= type
;
2315 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2316 req
.ndm
.ndm_family
= family
;
2318 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2320 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2324 * IP Neighbor table read using netlink interface. This is invoked
2327 int netlink_neigh_read(struct zebra_ns
*zns
)
2331 /* Get IP neighbor table. */
2332 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2335 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2342 * IP Neighbor table read using netlink interface. This is for a specific
2345 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2349 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2353 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2359 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2364 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2367 /* Length validity. */
2368 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2370 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2371 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2372 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2376 /* Is this a notification for the MAC FDB or IP neighbor table? */
2377 ndm
= NLMSG_DATA(h
);
2378 if (ndm
->ndm_family
== AF_BRIDGE
)
2379 return netlink_macfdb_change(h
, len
, ns_id
);
2381 if (ndm
->ndm_type
!= RTN_UNICAST
)
2384 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2385 return netlink_ipneigh_change(h
, len
, ns_id
);
2390 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2391 struct ethaddr
*mac
, uint32_t flags
, int cmd
)
2400 struct zebra_ns
*zns
;
2401 char buf
[INET6_ADDRSTRLEN
];
2402 char buf2
[ETHER_ADDR_STRLEN
];
2403 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2406 memset(&req
, 0, sizeof(req
));
2408 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2409 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2410 if (cmd
== RTM_NEWNEIGH
)
2411 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2412 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2413 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2414 req
.ndm
.ndm_state
= flags
;
2415 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2416 req
.ndm
.ndm_type
= RTN_UNICAST
;
2417 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2420 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2421 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2423 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2425 if (IS_ZEBRA_DEBUG_KERNEL
)
2426 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2427 nl_msg_type_to_str(cmd
),
2428 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2429 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2430 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2433 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2437 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2438 struct in_addr vtep_ip
, uint8_t sticky
)
2440 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2444 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2445 struct in_addr vtep_ip
, int local
)
2447 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2451 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2452 struct ethaddr
*mac
)
2454 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_NOARP
, RTM_NEWNEIGH
);
2457 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2459 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2463 * MPLS label forwarding table change via netlink interface.
2465 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2468 zebra_nhlfe_t
*nhlfe
;
2469 struct nexthop
*nexthop
= NULL
;
2470 unsigned int nexthop_num
;
2471 const char *routedesc
;
2472 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2478 char buf
[NL_PKT_BUF_SIZE
];
2481 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2484 * Count # nexthops so we can decide whether to use singlepath
2485 * or multipath case.
2488 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2489 nexthop
= nhlfe
->nexthop
;
2492 if (cmd
== RTM_NEWROUTE
) {
2493 /* Count all selected NHLFEs */
2494 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2495 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2499 /* Count all installed NHLFEs */
2500 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2501 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2506 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2509 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2510 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2511 req
.n
.nlmsg_type
= cmd
;
2512 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2514 req
.r
.rtm_family
= AF_MPLS
;
2515 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2516 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2517 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2518 req
.r
.rtm_type
= RTN_UNICAST
;
2520 if (cmd
== RTM_NEWROUTE
) {
2521 /* We do a replace to handle update. */
2522 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2524 /* set the protocol value if installing */
2525 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2526 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2529 /* Fill destination */
2530 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2531 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2533 /* Fill nexthops (paths) based on single-path or multipath. The paths
2534 * chosen depend on the operation.
2536 if (nexthop_num
== 1 || multipath_num
== 1) {
2537 routedesc
= "single-path";
2538 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2541 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2542 nexthop
= nhlfe
->nexthop
;
2546 if ((cmd
== RTM_NEWROUTE
2547 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2548 && CHECK_FLAG(nexthop
->flags
,
2549 NEXTHOP_FLAG_ACTIVE
)))
2550 || (cmd
== RTM_DELROUTE
2551 && (CHECK_FLAG(nhlfe
->flags
,
2552 NHLFE_FLAG_INSTALLED
)
2553 && CHECK_FLAG(nexthop
->flags
,
2554 NEXTHOP_FLAG_FIB
)))) {
2555 /* Add the gateway */
2556 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2563 } else /* Multipath case */
2565 char buf
[NL_PKT_BUF_SIZE
];
2566 struct rtattr
*rta
= (void *)buf
;
2567 struct rtnexthop
*rtnh
;
2568 union g_addr
*src1
= NULL
;
2570 rta
->rta_type
= RTA_MULTIPATH
;
2571 rta
->rta_len
= RTA_LENGTH(0);
2572 rtnh
= RTA_DATA(rta
);
2574 routedesc
= "multipath";
2575 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2578 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2579 nexthop
= nhlfe
->nexthop
;
2583 if (nexthop_num
>= multipath_num
)
2586 if ((cmd
== RTM_NEWROUTE
2587 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2588 && CHECK_FLAG(nexthop
->flags
,
2589 NEXTHOP_FLAG_ACTIVE
)))
2590 || (cmd
== RTM_DELROUTE
2591 && (CHECK_FLAG(nhlfe
->flags
,
2592 NHLFE_FLAG_INSTALLED
)
2593 && CHECK_FLAG(nexthop
->flags
,
2594 NEXTHOP_FLAG_FIB
)))) {
2597 /* Build the multipath */
2598 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2601 rtnh
= RTNH_NEXT(rtnh
);
2605 /* Add the multipath */
2606 if (rta
->rta_len
> RTA_LENGTH(0))
2607 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2608 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2611 /* Talk to netlink socket. */
2612 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2615 #endif /* HAVE_NETLINK */