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_per_ns(
487 zebra_ns_lookup(ns_id
),
490 nh_vrf_id
= ifp
->vrf_id
;
492 nh
.vrf_id
= nh_vrf_id
;
494 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
495 NULL
, &nh
, table
, metric
, mtu
, distance
, tag
);
497 /* This is a multipath route */
499 struct route_entry
*re
;
500 struct rtnexthop
*rtnh
=
501 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
503 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
505 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
507 re
->distance
= distance
;
514 re
->uptime
= time(NULL
);
519 if (len
< (int)sizeof(*rtnh
)
520 || rtnh
->rtnh_len
> len
)
523 index
= rtnh
->rtnh_ifindex
;
526 * Yes we are looking this up
527 * for every nexthop and just
528 * using the last one looked
531 ifp
= if_lookup_by_index_per_ns(
532 zebra_ns_lookup(ns_id
),
535 nh_vrf_id
= ifp
->vrf_id
;
538 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
541 nh_vrf_id
= VRF_DEFAULT
;
547 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
548 memset(tb
, 0, sizeof(tb
));
549 netlink_parse_rtattr(
550 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
551 rtnh
->rtnh_len
- sizeof(*rtnh
));
558 if (rtm
->rtm_family
== AF_INET
) {
560 route_entry_nexthop_ipv4_ifindex_add(
565 route_entry_nexthop_ipv4_add(
569 } else if (rtm
->rtm_family
572 route_entry_nexthop_ipv6_ifindex_add(
576 route_entry_nexthop_ipv6_add(
581 route_entry_nexthop_ifindex_add(
582 re
, index
, nh_vrf_id
);
584 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
585 rtnh
= RTNH_NEXT(rtnh
);
588 zserv_nexthop_num_warn(__func__
,
589 (const struct prefix
*)&p
,
591 if (re
->nexthop_num
== 0)
594 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
,
598 if (!tb
[RTA_MULTIPATH
]) {
600 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
602 memset(&nh
, 0, sizeof(nh
));
603 if (bh_type
== BLACKHOLE_UNSPEC
) {
605 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
606 else if (index
&& gate
)
609 ? NEXTHOP_TYPE_IPV4_IFINDEX
610 : NEXTHOP_TYPE_IPV6_IFINDEX
;
611 else if (!index
&& gate
)
612 nh
.type
= (afi
== AFI_IP
)
616 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
617 nh
.bh_type
= BLACKHOLE_UNSPEC
;
620 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
621 nh
.bh_type
= bh_type
;
625 memcpy(&nh
.gate
, gate
, sz
);
626 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
627 &p
, NULL
, &nh
, table
, metric
, true);
629 /* XXX: need to compare the entire list of nexthops
630 * here for NLM_F_APPEND stupidity */
631 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
632 &p
, NULL
, NULL
, table
, metric
, true);
639 static struct mcast_route_data
*mroute
= NULL
;
641 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
642 ns_id_t ns_id
, int startup
)
646 struct rtattr
*tb
[RTA_MAX
+ 1];
647 struct mcast_route_data
*m
;
648 struct mcast_route_data mr
;
655 char oif_list
[256] = "\0";
662 memset(&mr
, 0, sizeof(mr
));
668 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
670 memset(tb
, 0, sizeof tb
);
671 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
674 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
676 table
= rtm
->rtm_table
;
678 vrf
= vrf_lookup_by_table(table
, ns_id
);
681 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
684 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
687 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
689 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
690 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
692 if (tb
[RTA_MULTIPATH
]) {
693 struct rtnexthop
*rtnh
=
694 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
696 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
698 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
701 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
704 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
705 rtnh
= RTNH_NEXT(rtnh
);
709 if (IS_ZEBRA_DEBUG_KERNEL
) {
710 struct interface
*ifp
;
711 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
712 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
713 for (count
= 0; count
< oif_count
; count
++) {
714 ifp
= if_lookup_by_index(oif
[count
], vrf
);
717 sprintf(temp
, "%s ", ifp
->name
);
718 strcat(oif_list
, temp
);
720 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
721 ifp
= if_lookup_by_index(iif
, vrf
);
723 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
724 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
725 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
730 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
737 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
738 /* If this is not route add/delete message print warning. */
739 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
743 /* Connected route. */
744 if (IS_ZEBRA_DEBUG_KERNEL
)
745 zlog_debug("%s %s %s proto %s NS %u",
746 nl_msg_type_to_str(h
->nlmsg_type
),
747 nl_family_to_str(rtm
->rtm_family
),
748 nl_rttype_to_str(rtm
->rtm_type
),
749 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
751 /* We don't care about change notifications for the MPLS table. */
752 /* TODO: Revisit this. */
753 if (rtm
->rtm_family
== AF_MPLS
)
756 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
758 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
761 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
765 if (rtm
->rtm_type
== RTN_MULTICAST
)
766 netlink_route_change_read_multicast(h
, ns_id
, startup
);
768 netlink_route_change_read_unicast(h
, ns_id
, startup
);
772 /* Request for specific route information from the kernel */
773 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
780 /* Form the request, specifying filter (rtattr) if needed. */
781 memset(&req
, 0, sizeof(req
));
782 req
.n
.nlmsg_type
= type
;
783 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
784 req
.rtm
.rtm_family
= family
;
786 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
789 /* Routing table read function using netlink interface. Only called
791 int netlink_route_read(struct zebra_ns
*zns
)
795 /* Get IPv4 routing table. */
796 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
799 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
800 &zns
->netlink_cmd
, zns
, 0, 1);
804 /* Get IPv6 routing table. */
805 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
808 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
809 &zns
->netlink_cmd
, zns
, 0, 1);
816 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
818 struct nlmsghdr
*nlmsg
,
819 size_t req_size
, int bytelen
,
820 struct nexthop
*nexthop
)
822 if (route_family
== AF_MPLS
) {
823 struct gw_family_t gw_fam
;
825 gw_fam
.family
= gw_family
;
826 if (gw_family
== AF_INET
)
827 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
829 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
830 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
833 if (gw_family
== AF_INET
)
834 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
835 &nexthop
->gate
.ipv4
, bytelen
);
837 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
838 &nexthop
->gate
.ipv6
, bytelen
);
842 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
845 struct rtnexthop
*rtnh
,
846 size_t req_size
, int bytelen
,
847 struct nexthop
*nexthop
)
849 if (route_family
== AF_MPLS
) {
850 struct gw_family_t gw_fam
;
852 gw_fam
.family
= gw_family
;
853 if (gw_family
== AF_INET
)
854 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
856 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
857 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
859 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
861 if (gw_family
== AF_INET
)
862 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
863 &nexthop
->gate
.ipv4
, bytelen
);
865 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
866 &nexthop
->gate
.ipv6
, bytelen
);
867 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
871 /* This function takes a nexthop as argument and adds
872 * the appropriate netlink attributes to an existing
875 * @param routedesc: Human readable description of route type
876 * (direct/recursive, single-/multipath)
877 * @param bytelen: Length of addresses in bytes.
878 * @param nexthop: Nexthop information
879 * @param nlmsg: nlmsghdr structure to fill in.
880 * @param req_size: The size allocated for the message.
882 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
883 struct nexthop
*nexthop
,
884 struct nlmsghdr
*nlmsg
,
886 size_t req_size
, int cmd
)
888 struct mpls_label_stack
*nh_label
;
889 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
894 * label_buf is *only* currently used within debugging.
895 * As such when we assign it we are guarding it inside
896 * a debug test. If you want to change this make sure
897 * you fix this assumption
902 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
905 nh_label
= nh
->nh_label
;
906 if (!nh_label
|| !nh_label
->num_labels
)
909 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
910 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
913 if (IS_ZEBRA_DEBUG_KERNEL
) {
915 sprintf(label_buf
, "label %u",
918 sprintf(label_buf1
, "/%u",
920 strlcat(label_buf
, label_buf1
,
925 out_lse
[num_labels
] =
926 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
932 /* Set the BoS bit */
933 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
935 if (rtmsg
->rtm_family
== AF_MPLS
)
936 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
937 num_labels
* sizeof(mpls_lse_t
));
940 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
942 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
944 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
945 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
946 num_labels
* sizeof(mpls_lse_t
));
947 addattr_nest_end(nlmsg
, nest
);
951 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
952 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
954 if (rtmsg
->rtm_family
== AF_INET
955 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
956 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
957 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
958 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
959 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
961 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
962 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
963 &nexthop
->rmap_src
.ipv4
, bytelen
);
964 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
965 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
966 &nexthop
->src
.ipv4
, bytelen
);
968 if (IS_ZEBRA_DEBUG_KERNEL
)
970 " 5549: _netlink_route_build_singlepath() (%s): "
971 "nexthop via %s %s if %u(%u)",
972 routedesc
, ipv4_ll_buf
, label_buf
,
973 nexthop
->ifindex
, nexthop
->vrf_id
);
977 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
978 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
979 /* Send deletes to the kernel without specifying the next-hop */
980 if (cmd
!= RTM_DELROUTE
)
981 _netlink_route_nl_add_gateway_info(
982 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
985 if (cmd
== RTM_NEWROUTE
) {
986 if (nexthop
->rmap_src
.ipv4
.s_addr
)
987 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
988 &nexthop
->rmap_src
.ipv4
, bytelen
);
989 else if (nexthop
->src
.ipv4
.s_addr
)
990 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
991 &nexthop
->src
.ipv4
, bytelen
);
994 if (IS_ZEBRA_DEBUG_KERNEL
)
996 "netlink_route_multipath() (%s): "
997 "nexthop via %s %s if %u(%u)",
998 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
999 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1002 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1003 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1004 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1005 nlmsg
, req_size
, bytelen
,
1008 if (cmd
== RTM_NEWROUTE
) {
1009 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1010 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1011 &nexthop
->rmap_src
.ipv6
, bytelen
);
1012 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1013 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1014 &nexthop
->src
.ipv6
, bytelen
);
1017 if (IS_ZEBRA_DEBUG_KERNEL
)
1019 "netlink_route_multipath() (%s): "
1020 "nexthop via %s %s if %u(%u)",
1021 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1022 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1026 * We have the ifindex so we should always send it
1027 * This is especially useful if we are doing route
1030 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1031 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1033 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1034 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1035 if (cmd
== RTM_NEWROUTE
) {
1036 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1037 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1038 &nexthop
->rmap_src
.ipv4
, bytelen
);
1039 else if (nexthop
->src
.ipv4
.s_addr
)
1040 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1041 &nexthop
->src
.ipv4
, bytelen
);
1044 if (IS_ZEBRA_DEBUG_KERNEL
)
1046 "netlink_route_multipath() (%s): "
1047 "nexthop via if %u(%u)",
1048 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1051 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1052 if (cmd
== RTM_NEWROUTE
) {
1053 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1054 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1055 &nexthop
->rmap_src
.ipv6
, bytelen
);
1056 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1057 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1058 &nexthop
->src
.ipv6
, bytelen
);
1061 if (IS_ZEBRA_DEBUG_KERNEL
)
1063 "netlink_route_multipath() (%s): "
1064 "nexthop via if %u(%u)",
1065 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1069 /* This function takes a nexthop as argument and
1070 * appends to the given rtattr/rtnexthop pair the
1071 * representation of the nexthop. If the nexthop
1072 * defines a preferred source, the src parameter
1073 * will be modified to point to that src, otherwise
1074 * it will be kept unmodified.
1076 * @param routedesc: Human readable description of route type
1077 * (direct/recursive, single-/multipath)
1078 * @param bytelen: Length of addresses in bytes.
1079 * @param nexthop: Nexthop information
1080 * @param rta: rtnetlink attribute structure
1081 * @param rtnh: pointer to an rtnetlink nexthop structure
1082 * @param src: pointer pointing to a location where
1083 * the prefsrc should be stored.
1085 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1086 struct nexthop
*nexthop
,
1088 struct rtnexthop
*rtnh
,
1089 struct rtmsg
*rtmsg
,
1092 struct mpls_label_stack
*nh_label
;
1093 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1095 char label_buf
[256];
1097 rtnh
->rtnh_len
= sizeof(*rtnh
);
1098 rtnh
->rtnh_flags
= 0;
1099 rtnh
->rtnh_hops
= 0;
1100 rta
->rta_len
+= rtnh
->rtnh_len
;
1103 * label_buf is *only* currently used within debugging.
1104 * As such when we assign it we are guarding it inside
1105 * a debug test. If you want to change this make sure
1106 * you fix this assumption
1108 label_buf
[0] = '\0';
1111 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1112 char label_buf1
[20];
1114 nh_label
= nh
->nh_label
;
1115 if (!nh_label
|| !nh_label
->num_labels
)
1118 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1119 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1122 if (IS_ZEBRA_DEBUG_KERNEL
) {
1124 sprintf(label_buf
, "label %u",
1125 nh_label
->label
[i
]);
1127 sprintf(label_buf1
, "/%u",
1128 nh_label
->label
[i
]);
1129 strlcat(label_buf
, label_buf1
,
1134 out_lse
[num_labels
] =
1135 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1141 /* Set the BoS bit */
1142 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1144 if (rtmsg
->rtm_family
== AF_MPLS
) {
1145 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1147 num_labels
* sizeof(mpls_lse_t
));
1149 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1151 struct rtattr
*nest
;
1152 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1153 int len
= rta
->rta_len
;
1155 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1156 &encap
, sizeof(uint16_t));
1157 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1158 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1160 num_labels
* sizeof(mpls_lse_t
));
1161 rta_nest_end(rta
, nest
);
1162 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1166 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1167 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1169 if (rtmsg
->rtm_family
== AF_INET
1170 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1171 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1173 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1174 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1176 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1177 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1179 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1180 *src
= &nexthop
->rmap_src
;
1181 else if (nexthop
->src
.ipv4
.s_addr
)
1182 *src
= &nexthop
->src
;
1184 if (IS_ZEBRA_DEBUG_KERNEL
)
1186 " 5549: netlink_route_build_multipath() (%s): "
1187 "nexthop via %s %s if %u",
1188 routedesc
, ipv4_ll_buf
, label_buf
,
1193 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1194 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1195 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1196 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1198 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1199 *src
= &nexthop
->rmap_src
;
1200 else if (nexthop
->src
.ipv4
.s_addr
)
1201 *src
= &nexthop
->src
;
1203 if (IS_ZEBRA_DEBUG_KERNEL
)
1205 "netlink_route_multipath() (%s): "
1206 "nexthop via %s %s if %u",
1207 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1208 label_buf
, nexthop
->ifindex
);
1210 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1211 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1212 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1213 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1216 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1217 *src
= &nexthop
->rmap_src
;
1218 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1219 *src
= &nexthop
->src
;
1221 if (IS_ZEBRA_DEBUG_KERNEL
)
1223 "netlink_route_multipath() (%s): "
1224 "nexthop via %s %s if %u",
1225 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1226 label_buf
, nexthop
->ifindex
);
1230 * We have figured out the ifindex so we should always send it
1231 * This is especially useful if we are doing route
1234 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1235 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1238 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1239 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1240 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1241 *src
= &nexthop
->rmap_src
;
1242 else if (nexthop
->src
.ipv4
.s_addr
)
1243 *src
= &nexthop
->src
;
1245 if (IS_ZEBRA_DEBUG_KERNEL
)
1247 "netlink_route_multipath() (%s): "
1248 "nexthop via if %u",
1249 routedesc
, nexthop
->ifindex
);
1253 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1254 zebra_nhlfe_t
*nhlfe
,
1255 struct nlmsghdr
*nlmsg
,
1256 struct rtmsg
*rtmsg
,
1257 size_t req_size
, int cmd
)
1262 family
= NHLFE_FAMILY(nhlfe
);
1263 bytelen
= (family
== AF_INET
? 4 : 16);
1264 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1265 nlmsg
, rtmsg
, req_size
, cmd
);
1270 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1271 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1272 struct rtmsg
*rtmsg
, union g_addr
**src
)
1277 family
= NHLFE_FAMILY(nhlfe
);
1278 bytelen
= (family
== AF_INET
? 4 : 16);
1279 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1284 /* Log debug information for netlink_route_multipath
1285 * if debug logging is enabled.
1287 * @param cmd: Netlink command which is to be processed
1288 * @param p: Prefix for which the change is due
1289 * @param family: Address family which the change concerns
1290 * @param zvrf: The vrf we are in
1291 * @param tableid: The table we are working on
1293 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1294 int family
, vrf_id_t vrfid
,
1297 if (IS_ZEBRA_DEBUG_KERNEL
) {
1298 char buf
[PREFIX_STRLEN
];
1300 "netlink_route_multipath(): %s %s vrf %u(%u)",
1301 nl_msg_type_to_str(cmd
),
1302 prefix2str(p
, buf
, sizeof(buf
)),
1307 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1309 if (IS_ZEBRA_DEBUG_KERNEL
)
1310 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1311 nl_msg_type_to_str(cmd
), label
);
1314 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1315 int llalen
, ns_id_t ns_id
)
1323 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1325 memset(&req
, 0, sizeof(req
));
1327 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1328 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1329 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1330 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1332 req
.ndm
.ndm_family
= AF_INET
;
1333 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1334 req
.ndm
.ndm_ifindex
= ifindex
;
1335 req
.ndm
.ndm_type
= RTN_UNICAST
;
1337 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1338 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1340 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1344 /* Routing table change via netlink interface. */
1345 /* Update flag indicates whether this is a "replace" or not. */
1346 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1347 const struct prefix
*src_p
,
1348 struct route_entry
*re
,
1352 struct sockaddr_nl snl
;
1353 struct nexthop
*nexthop
= NULL
;
1354 unsigned int nexthop_num
;
1355 int family
= PREFIX_FAMILY(p
);
1356 const char *routedesc
;
1363 char buf
[NL_PKT_BUF_SIZE
];
1366 struct zebra_ns
*zns
;
1367 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1370 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1372 bytelen
= (family
== AF_INET
? 4 : 16);
1374 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1375 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1376 if ((cmd
== RTM_NEWROUTE
) && update
)
1377 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1378 req
.n
.nlmsg_type
= cmd
;
1379 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1381 req
.r
.rtm_family
= family
;
1382 req
.r
.rtm_dst_len
= p
->prefixlen
;
1383 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1384 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1385 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1388 * blackhole routes are not RTN_UNICAST, they are
1389 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1390 * so setting this value as a RTN_UNICAST would
1391 * cause the route lookup of just the prefix
1392 * to fail. So no need to specify this for
1393 * the RTM_DELROUTE case
1395 if (cmd
!= RTM_DELROUTE
)
1396 req
.r
.rtm_type
= RTN_UNICAST
;
1398 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1400 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1404 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1406 * either by the kernel or by zebra. Its purely for calculating best
1408 * by the routing protocol and for communicating with protocol peers.
1410 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1411 #if defined(SUPPORT_REALMS)
1412 if (re
->tag
> 0 && re
->tag
<= 255)
1413 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1415 /* Table corresponding to this route. */
1416 if (re
->table
< 256)
1417 req
.r
.rtm_table
= re
->table
;
1419 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1420 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1423 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1426 * If we are not updating the route and we have received
1427 * a route delete, then all we need to fill in is the
1428 * prefix information to tell the kernel to schwack
1431 if (!update
&& cmd
== RTM_DELROUTE
)
1434 if (re
->mtu
|| re
->nexthop_mtu
) {
1435 char buf
[NL_PKT_BUF_SIZE
];
1436 struct rtattr
*rta
= (void *)buf
;
1437 uint32_t mtu
= re
->mtu
;
1438 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1439 mtu
= re
->nexthop_mtu
;
1440 rta
->rta_type
= RTA_METRICS
;
1441 rta
->rta_len
= RTA_LENGTH(0);
1442 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1443 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1447 /* Count overall nexthops so we can decide whether to use singlepath
1448 * or multipath case. */
1450 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1451 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1453 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1455 if (cmd
== RTM_DELROUTE
1456 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1462 /* Singlepath case. */
1463 if (nexthop_num
== 1 || multipath_num
== 1) {
1465 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1467 * So we want to cover 2 types of blackhole
1469 * 1) A normal blackhole route( ala from a static
1471 * 2) A recursively resolved blackhole route
1473 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1474 switch (nexthop
->bh_type
) {
1475 case BLACKHOLE_ADMINPROHIB
:
1476 req
.r
.rtm_type
= RTN_PROHIBIT
;
1478 case BLACKHOLE_REJECT
:
1479 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1482 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1487 if (CHECK_FLAG(nexthop
->flags
,
1488 NEXTHOP_FLAG_RECURSIVE
)) {
1490 if (family
== AF_INET
) {
1491 if (nexthop
->rmap_src
.ipv4
1498 } else if (nexthop
->src
.ipv4
1506 } else if (family
== AF_INET6
) {
1507 if (!IN6_IS_ADDR_UNSPECIFIED(
1515 !IN6_IS_ADDR_UNSPECIFIED(
1528 if ((cmd
== RTM_NEWROUTE
1529 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1530 || (cmd
== RTM_DELROUTE
1531 && CHECK_FLAG(nexthop
->flags
,
1532 NEXTHOP_FLAG_FIB
))) {
1533 routedesc
= nexthop
->rparent
1534 ? "recursive, single-path"
1537 _netlink_route_build_singlepath(
1538 routedesc
, bytelen
, nexthop
, &req
.n
,
1539 &req
.r
, sizeof req
, cmd
);
1544 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1545 if (family
== AF_INET
)
1546 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1547 &src
.ipv4
, bytelen
);
1548 else if (family
== AF_INET6
)
1549 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1550 &src
.ipv6
, bytelen
);
1553 char buf
[NL_PKT_BUF_SIZE
];
1554 struct rtattr
*rta
= (void *)buf
;
1555 struct rtnexthop
*rtnh
;
1556 union g_addr
*src1
= NULL
;
1558 rta
->rta_type
= RTA_MULTIPATH
;
1559 rta
->rta_len
= RTA_LENGTH(0);
1560 rtnh
= RTA_DATA(rta
);
1563 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1564 if (nexthop_num
>= multipath_num
)
1567 if (CHECK_FLAG(nexthop
->flags
,
1568 NEXTHOP_FLAG_RECURSIVE
)) {
1569 /* This only works for IPv4 now */
1571 if (family
== AF_INET
) {
1572 if (nexthop
->rmap_src
.ipv4
1579 } else if (nexthop
->src
.ipv4
1587 } else if (family
== AF_INET6
) {
1588 if (!IN6_IS_ADDR_UNSPECIFIED(
1596 !IN6_IS_ADDR_UNSPECIFIED(
1609 if ((cmd
== RTM_NEWROUTE
1610 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1611 || (cmd
== RTM_DELROUTE
1612 && CHECK_FLAG(nexthop
->flags
,
1613 NEXTHOP_FLAG_FIB
))) {
1614 routedesc
= nexthop
->rparent
1615 ? "recursive, multipath"
1619 _netlink_route_build_multipath(
1620 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1622 rtnh
= RTNH_NEXT(rtnh
);
1624 if (!setsrc
&& src1
) {
1625 if (family
== AF_INET
)
1626 src
.ipv4
= src1
->ipv4
;
1627 else if (family
== AF_INET6
)
1628 src
.ipv6
= src1
->ipv6
;
1634 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1635 if (family
== AF_INET
)
1636 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1637 &src
.ipv4
, bytelen
);
1638 else if (family
== AF_INET6
)
1639 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1640 &src
.ipv6
, bytelen
);
1641 if (IS_ZEBRA_DEBUG_KERNEL
)
1642 zlog_debug("Setting source");
1645 if (rta
->rta_len
> RTA_LENGTH(0))
1646 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1647 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1650 /* If there is no useful nexthop then return. */
1651 if (nexthop_num
== 0) {
1652 if (IS_ZEBRA_DEBUG_KERNEL
)
1654 "netlink_route_multipath(): No useful nexthop.");
1660 /* Destination netlink address. */
1661 memset(&snl
, 0, sizeof snl
);
1662 snl
.nl_family
= AF_NETLINK
;
1664 /* Talk to netlink socket. */
1665 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1669 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1672 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1680 struct zebra_ns
*zns
;
1683 memset(&req
, 0, sizeof(req
));
1685 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1686 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1687 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1689 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1690 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1692 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1693 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1694 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1695 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1696 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1698 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1699 &zns
->netlink_cmd
, zns
, 0);
1705 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1706 const struct prefix
*p
,
1707 const struct prefix
*src_p
,
1708 struct route_entry
*old
,
1709 struct route_entry
*new)
1716 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1717 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1718 new, (old
) ? 1 : 0);
1721 * So v6 route replace semantics are not in
1722 * the kernel at this point as I understand it.
1723 * So let's do a delete than an add.
1724 * In the future once v6 route replace semantics
1725 * are in we can figure out what to do here to
1726 * allow working with old and new kernels.
1728 * I'm also intentionally ignoring the failure case
1729 * of the route delete. If that happens yeah we're
1733 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1735 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1738 kernel_route_rib_pass_fail(rn
, p
, new,
1739 (!ret
) ? DP_INSTALL_SUCCESS
1740 : DP_INSTALL_FAILURE
);
1741 return DP_REQUEST_SUCCESS
;
1745 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1747 kernel_route_rib_pass_fail(rn
, p
, old
,
1748 (!ret
) ? DP_DELETE_SUCCESS
1749 : DP_DELETE_FAILURE
);
1752 return DP_REQUEST_SUCCESS
;
1755 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1756 int llalen
, ns_id_t ns_id
)
1758 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1759 addr
, lla
, llalen
, ns_id
);
1763 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1764 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1766 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1767 struct in_addr
*vtep_ip
, int cmd
)
1769 struct zebra_ns
*zns
;
1775 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1776 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1779 memset(&req
, 0, sizeof(req
));
1781 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1782 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1783 if (cmd
== RTM_NEWNEIGH
)
1784 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1785 req
.n
.nlmsg_type
= cmd
;
1786 req
.ndm
.ndm_family
= PF_BRIDGE
;
1787 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1788 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1791 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1792 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1793 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1795 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1800 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1802 * a "flood" MAC FDB entry.
1804 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1806 if (IS_ZEBRA_DEBUG_VXLAN
)
1807 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1808 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1810 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1814 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1815 * deleting the "flood" MAC FDB entry.
1817 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1819 if (IS_ZEBRA_DEBUG_VXLAN
)
1821 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1822 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1824 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1828 #define NDA_RTA(r) \
1829 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1832 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1835 struct interface
*ifp
;
1836 struct zebra_if
*zif
;
1837 struct rtattr
*tb
[NDA_MAX
+ 1];
1838 struct interface
*br_if
;
1841 struct prefix vtep_ip
;
1842 int vid_present
= 0, dst_present
= 0;
1843 char buf
[ETHER_ADDR_STRLEN
];
1848 ndm
= NLMSG_DATA(h
);
1850 /* We only process macfdb notifications if EVPN is enabled */
1851 if (!is_evpn_enabled())
1854 /* The interface should exist. */
1855 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1857 if (!ifp
|| !ifp
->info
)
1860 /* The interface should be something we're interested in. */
1861 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1864 /* Drop "permanent" entries. */
1865 if (ndm
->ndm_state
& NUD_PERMANENT
)
1868 zif
= (struct zebra_if
*)ifp
->info
;
1869 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1870 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1871 nl_msg_type_to_str(h
->nlmsg_type
),
1872 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1873 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1877 /* Parse attributes and extract fields of interest. */
1878 memset(tb
, 0, sizeof tb
);
1879 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1881 if (!tb
[NDA_LLADDR
]) {
1882 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1883 nl_msg_type_to_str(h
->nlmsg_type
),
1884 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1885 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1889 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1891 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1892 nl_msg_type_to_str(h
->nlmsg_type
),
1893 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1894 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1895 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1899 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1901 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1903 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1904 sprintf(vid_buf
, " VLAN %u", vid
);
1908 /* TODO: Only IPv4 supported now. */
1910 vtep_ip
.family
= AF_INET
;
1911 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1912 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1914 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1917 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1919 if (IS_ZEBRA_DEBUG_KERNEL
)
1920 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1921 nl_msg_type_to_str(h
->nlmsg_type
),
1922 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1923 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1924 sticky
? "sticky " : "",
1925 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1926 dst_present
? dst_buf
: "");
1928 if (filter_vlan
&& vid
!= filter_vlan
)
1931 /* If add or update, do accordingly if learnt on a "local" interface; if
1932 * the notification is over VxLAN, this has to be related to
1934 * so perform an implicit delete of any local entry (if it exists).
1936 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1937 /* Drop "permanent" entries. */
1938 if (ndm
->ndm_state
& NUD_PERMANENT
)
1941 if (IS_ZEBRA_IF_VXLAN(ifp
))
1942 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1945 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1949 /* This is a delete notification.
1950 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1951 * 2. For a MAC over "local" interface, delete the mac
1952 * Note: We will get notifications from both bridge driver and VxLAN
1954 * Ignore the notification from VxLan driver as it is also generated
1955 * when mac moves from remote to local.
1960 if (IS_ZEBRA_IF_VXLAN(ifp
))
1961 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1964 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1967 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1972 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1975 /* Length validity. */
1976 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1980 /* We are interested only in AF_BRIDGE notifications. */
1981 ndm
= NLMSG_DATA(h
);
1982 if (ndm
->ndm_family
!= AF_BRIDGE
)
1985 return netlink_macfdb_change(h
, len
, ns_id
);
1988 /* Request for MAC FDB information from the kernel */
1989 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
1990 ifindex_t master_ifindex
)
1994 struct ifinfomsg ifm
;
1998 /* Form the request, specifying filter (rtattr) if needed. */
1999 memset(&req
, 0, sizeof(req
));
2000 req
.n
.nlmsg_type
= type
;
2001 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2002 req
.ifm
.ifi_family
= family
;
2004 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2006 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2010 * MAC forwarding database read using netlink interface. This is invoked
2013 int netlink_macfdb_read(struct zebra_ns
*zns
)
2017 /* Get bridge FDB table. */
2018 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2021 /* We are reading entire table. */
2023 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2030 * MAC forwarding database read using netlink interface. This is for a
2031 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2033 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2034 struct interface
*br_if
)
2036 struct zebra_if
*br_zif
;
2037 struct zebra_if
*zif
;
2038 struct zebra_l2info_vxlan
*vxl
;
2042 /* Save VLAN we're filtering on, if needed. */
2043 br_zif
= (struct zebra_if
*)br_if
->info
;
2044 zif
= (struct zebra_if
*)ifp
->info
;
2045 vxl
= &zif
->l2info
.vxl
;
2046 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2047 filter_vlan
= vxl
->access_vlan
;
2049 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2051 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2055 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2058 /* Reset VLAN filter. */
2063 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2064 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2065 int local
, int cmd
, uint8_t sticky
)
2067 struct zebra_ns
*zns
;
2074 struct zebra_if
*zif
;
2075 struct interface
*br_if
;
2076 struct zebra_if
*br_zif
;
2077 char buf
[ETHER_ADDR_STRLEN
];
2078 int vid_present
= 0, dst_present
= 0;
2081 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2085 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2086 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2087 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2092 memset(&req
, 0, sizeof(req
));
2094 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2095 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2096 if (cmd
== RTM_NEWNEIGH
)
2097 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2098 req
.n
.nlmsg_type
= cmd
;
2099 req
.ndm
.ndm_family
= AF_BRIDGE
;
2100 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2101 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2104 req
.ndm
.ndm_state
|= NUD_NOARP
;
2106 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2108 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2109 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2111 dst_alen
= 4; // TODO: hardcoded
2112 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2114 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2116 br_zif
= (struct zebra_if
*)br_if
->info
;
2117 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2118 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2120 sprintf(vid_buf
, " VLAN %u", vid
);
2122 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2124 if (IS_ZEBRA_DEBUG_KERNEL
)
2125 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2126 nl_msg_type_to_str(cmd
),
2127 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2128 ifp
->ifindex
, vid_present
? vid_buf
: "",
2129 sticky
? "sticky " : "",
2130 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2131 dst_present
? dst_buf
: "");
2133 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2138 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2141 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2144 struct interface
*ifp
;
2145 struct zebra_if
*zif
;
2146 struct rtattr
*tb
[NDA_MAX
+ 1];
2147 struct interface
*link_if
;
2150 char buf
[ETHER_ADDR_STRLEN
];
2151 char buf2
[INET6_ADDRSTRLEN
];
2152 int mac_present
= 0;
2153 uint8_t ext_learned
;
2155 ndm
= NLMSG_DATA(h
);
2157 /* The interface should exist. */
2158 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2160 if (!ifp
|| !ifp
->info
)
2163 zif
= (struct zebra_if
*)ifp
->info
;
2165 /* Parse attributes and extract fields of interest. */
2166 memset(tb
, 0, sizeof tb
);
2167 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2170 zlog_warn("%s family %s IF %s(%u) - no DST",
2171 nl_msg_type_to_str(h
->nlmsg_type
),
2172 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2177 memset(&ip
, 0, sizeof(struct ipaddr
));
2178 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2179 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2181 /* Drop some "permanent" entries. */
2182 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2183 char buf
[16] = "169.254.0.1";
2184 struct in_addr ipv4_ll
;
2186 if (ndm
->ndm_family
!= AF_INET
)
2189 if (!zif
->v6_2_v4_ll_neigh_entry
)
2192 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2195 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2196 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2199 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2200 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2204 /* The neighbor is present on an SVI. From this, we locate the
2206 * bridge because we're only interested in neighbors on a VxLAN bridge.
2207 * The bridge is located based on the nature of the SVI:
2208 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2210 * and is linked to the bridge
2211 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2215 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2216 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2220 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2225 memset(&mac
, 0, sizeof(struct ethaddr
));
2226 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2227 if (tb
[NDA_LLADDR
]) {
2228 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2230 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2231 nl_msg_type_to_str(h
->nlmsg_type
),
2232 nl_family_to_str(ndm
->ndm_family
),
2233 ifp
->name
, ndm
->ndm_ifindex
,
2234 (unsigned long)RTA_PAYLOAD(
2240 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2243 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2245 if (IS_ZEBRA_DEBUG_KERNEL
)
2247 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2248 nl_msg_type_to_str(h
->nlmsg_type
),
2249 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2251 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2253 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2255 ndm
->ndm_state
, ndm
->ndm_flags
);
2257 /* If the neighbor state is valid for use, process as an add or
2259 * else process as a delete. Note that the delete handling may
2261 * in re-adding the neighbor if it is a valid "remote" neighbor.
2263 if (ndm
->ndm_state
& NUD_VALID
)
2264 return zebra_vxlan_handle_kernel_neigh_update(
2265 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2268 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2271 if (IS_ZEBRA_DEBUG_KERNEL
)
2272 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2273 nl_msg_type_to_str(h
->nlmsg_type
),
2274 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2276 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2278 /* Process the delete - it may result in re-adding the neighbor if it is
2279 * a valid "remote" neighbor.
2281 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2284 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2289 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2292 /* Length validity. */
2293 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2297 /* We are interested only in AF_INET or AF_INET6 notifications. */
2298 ndm
= NLMSG_DATA(h
);
2299 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2302 return netlink_neigh_change(h
, len
);
2305 /* Request for IP neighbor information from the kernel */
2306 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2315 /* Form the request, specifying filter (rtattr) if needed. */
2316 memset(&req
, 0, sizeof(req
));
2317 req
.n
.nlmsg_type
= type
;
2318 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2319 req
.ndm
.ndm_family
= family
;
2321 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2323 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2327 * IP Neighbor table read using netlink interface. This is invoked
2330 int netlink_neigh_read(struct zebra_ns
*zns
)
2334 /* Get IP neighbor table. */
2335 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2338 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2345 * IP Neighbor table read using netlink interface. This is for a specific
2348 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2352 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2356 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2362 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2367 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2370 /* Length validity. */
2371 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2373 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2374 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2375 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2379 /* Is this a notification for the MAC FDB or IP neighbor table? */
2380 ndm
= NLMSG_DATA(h
);
2381 if (ndm
->ndm_family
== AF_BRIDGE
)
2382 return netlink_macfdb_change(h
, len
, ns_id
);
2384 if (ndm
->ndm_type
!= RTN_UNICAST
)
2387 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2388 return netlink_ipneigh_change(h
, len
, ns_id
);
2393 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2394 struct ethaddr
*mac
, uint32_t flags
, int cmd
)
2403 struct zebra_ns
*zns
;
2404 char buf
[INET6_ADDRSTRLEN
];
2405 char buf2
[ETHER_ADDR_STRLEN
];
2406 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2409 memset(&req
, 0, sizeof(req
));
2411 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2412 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2413 if (cmd
== RTM_NEWNEIGH
)
2414 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2415 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2416 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2417 req
.ndm
.ndm_state
= flags
;
2418 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2419 req
.ndm
.ndm_type
= RTN_UNICAST
;
2420 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2423 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2424 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2426 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2428 if (IS_ZEBRA_DEBUG_KERNEL
)
2429 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2430 nl_msg_type_to_str(cmd
),
2431 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2432 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2433 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2436 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2440 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2441 struct in_addr vtep_ip
, uint8_t sticky
)
2443 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2447 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2448 struct in_addr vtep_ip
, int local
)
2450 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2454 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2455 struct ethaddr
*mac
)
2457 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_NOARP
, RTM_NEWNEIGH
);
2460 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2462 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2466 * MPLS label forwarding table change via netlink interface.
2468 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2471 zebra_nhlfe_t
*nhlfe
;
2472 struct nexthop
*nexthop
= NULL
;
2473 unsigned int nexthop_num
;
2474 const char *routedesc
;
2475 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2481 char buf
[NL_PKT_BUF_SIZE
];
2484 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2487 * Count # nexthops so we can decide whether to use singlepath
2488 * or multipath case.
2491 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2492 nexthop
= nhlfe
->nexthop
;
2495 if (cmd
== RTM_NEWROUTE
) {
2496 /* Count all selected NHLFEs */
2497 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2498 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2502 /* Count all installed NHLFEs */
2503 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2504 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2509 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2512 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2513 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2514 req
.n
.nlmsg_type
= cmd
;
2515 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2517 req
.r
.rtm_family
= AF_MPLS
;
2518 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2519 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2520 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2521 req
.r
.rtm_type
= RTN_UNICAST
;
2523 if (cmd
== RTM_NEWROUTE
) {
2524 /* We do a replace to handle update. */
2525 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2527 /* set the protocol value if installing */
2528 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2529 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2532 /* Fill destination */
2533 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2534 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2536 /* Fill nexthops (paths) based on single-path or multipath. The paths
2537 * chosen depend on the operation.
2539 if (nexthop_num
== 1 || multipath_num
== 1) {
2540 routedesc
= "single-path";
2541 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2544 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2545 nexthop
= nhlfe
->nexthop
;
2549 if ((cmd
== RTM_NEWROUTE
2550 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2551 && CHECK_FLAG(nexthop
->flags
,
2552 NEXTHOP_FLAG_ACTIVE
)))
2553 || (cmd
== RTM_DELROUTE
2554 && (CHECK_FLAG(nhlfe
->flags
,
2555 NHLFE_FLAG_INSTALLED
)
2556 && CHECK_FLAG(nexthop
->flags
,
2557 NEXTHOP_FLAG_FIB
)))) {
2558 /* Add the gateway */
2559 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2566 } else /* Multipath case */
2568 char buf
[NL_PKT_BUF_SIZE
];
2569 struct rtattr
*rta
= (void *)buf
;
2570 struct rtnexthop
*rtnh
;
2571 union g_addr
*src1
= NULL
;
2573 rta
->rta_type
= RTA_MULTIPATH
;
2574 rta
->rta_len
= RTA_LENGTH(0);
2575 rtnh
= RTA_DATA(rta
);
2577 routedesc
= "multipath";
2578 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2581 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2582 nexthop
= nhlfe
->nexthop
;
2586 if (nexthop_num
>= multipath_num
)
2589 if ((cmd
== RTM_NEWROUTE
2590 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2591 && CHECK_FLAG(nexthop
->flags
,
2592 NEXTHOP_FLAG_ACTIVE
)))
2593 || (cmd
== RTM_DELROUTE
2594 && (CHECK_FLAG(nhlfe
->flags
,
2595 NHLFE_FLAG_INSTALLED
)
2596 && CHECK_FLAG(nexthop
->flags
,
2597 NEXTHOP_FLAG_FIB
)))) {
2600 /* Build the multipath */
2601 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2604 rtnh
= RTNH_NEXT(rtnh
);
2608 /* Add the multipath */
2609 if (rta
->rta_len
> RTA_LENGTH(0))
2610 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2611 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2614 /* Talk to netlink socket. */
2615 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2618 #endif /* HAVE_NETLINK */