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 if (rtnh
->rtnh_len
== 0)
587 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
588 rtnh
= RTNH_NEXT(rtnh
);
591 zserv_nexthop_num_warn(__func__
,
592 (const struct prefix
*)&p
,
594 if (re
->nexthop_num
== 0)
597 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
,
601 if (!tb
[RTA_MULTIPATH
]) {
603 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
605 memset(&nh
, 0, sizeof(nh
));
606 if (bh_type
== BLACKHOLE_UNSPEC
) {
608 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
609 else if (index
&& gate
)
612 ? NEXTHOP_TYPE_IPV4_IFINDEX
613 : NEXTHOP_TYPE_IPV6_IFINDEX
;
614 else if (!index
&& gate
)
615 nh
.type
= (afi
== AFI_IP
)
619 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
620 nh
.bh_type
= BLACKHOLE_UNSPEC
;
623 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
624 nh
.bh_type
= bh_type
;
628 memcpy(&nh
.gate
, gate
, sz
);
629 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
630 &p
, NULL
, &nh
, table
, metric
, true);
632 /* XXX: need to compare the entire list of nexthops
633 * here for NLM_F_APPEND stupidity */
634 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
635 &p
, NULL
, NULL
, table
, metric
, true);
642 static struct mcast_route_data
*mroute
= NULL
;
644 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
645 ns_id_t ns_id
, int startup
)
649 struct rtattr
*tb
[RTA_MAX
+ 1];
650 struct mcast_route_data
*m
;
651 struct mcast_route_data mr
;
658 char oif_list
[256] = "\0";
665 memset(&mr
, 0, sizeof(mr
));
671 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
673 memset(tb
, 0, sizeof tb
);
674 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
677 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
679 table
= rtm
->rtm_table
;
681 vrf
= vrf_lookup_by_table(table
, ns_id
);
684 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
687 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
690 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
692 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
693 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
695 if (tb
[RTA_MULTIPATH
]) {
696 struct rtnexthop
*rtnh
=
697 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
699 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
701 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
704 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
707 if (rtnh
->rtnh_len
== 0)
710 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
711 rtnh
= RTNH_NEXT(rtnh
);
715 if (IS_ZEBRA_DEBUG_KERNEL
) {
716 struct interface
*ifp
;
717 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
718 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
719 for (count
= 0; count
< oif_count
; count
++) {
720 ifp
= if_lookup_by_index(oif
[count
], vrf
);
723 sprintf(temp
, "%s ", ifp
->name
);
724 strcat(oif_list
, temp
);
726 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
727 ifp
= if_lookup_by_index(iif
, vrf
);
729 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
730 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
731 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
736 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
743 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
744 /* If this is not route add/delete message print warning. */
745 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
749 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
750 || rtm
->rtm_family
== AF_ETHERNET
751 || rtm
->rtm_family
== AF_MPLS
)) {
753 "Invalid address family: %d received from kernel route change: %d",
754 rtm
->rtm_family
, h
->nlmsg_type
);
758 /* Connected route. */
759 if (IS_ZEBRA_DEBUG_KERNEL
)
760 zlog_debug("%s %s %s proto %s NS %u",
761 nl_msg_type_to_str(h
->nlmsg_type
),
762 nl_family_to_str(rtm
->rtm_family
),
763 nl_rttype_to_str(rtm
->rtm_type
),
764 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
766 /* We don't care about change notifications for the MPLS table. */
767 /* TODO: Revisit this. */
768 if (rtm
->rtm_family
== AF_MPLS
)
771 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
773 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
776 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
780 if (rtm
->rtm_type
== RTN_MULTICAST
)
781 netlink_route_change_read_multicast(h
, ns_id
, startup
);
783 netlink_route_change_read_unicast(h
, ns_id
, startup
);
787 /* Request for specific route information from the kernel */
788 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
795 /* Form the request, specifying filter (rtattr) if needed. */
796 memset(&req
, 0, sizeof(req
));
797 req
.n
.nlmsg_type
= type
;
798 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
799 req
.rtm
.rtm_family
= family
;
801 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
804 /* Routing table read function using netlink interface. Only called
806 int netlink_route_read(struct zebra_ns
*zns
)
810 /* Get IPv4 routing table. */
811 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
814 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
815 &zns
->netlink_cmd
, zns
, 0, 1);
819 /* Get IPv6 routing table. */
820 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
823 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
824 &zns
->netlink_cmd
, zns
, 0, 1);
831 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
833 struct nlmsghdr
*nlmsg
,
834 size_t req_size
, int bytelen
,
835 struct nexthop
*nexthop
)
837 if (route_family
== AF_MPLS
) {
838 struct gw_family_t gw_fam
;
840 gw_fam
.family
= gw_family
;
841 if (gw_family
== AF_INET
)
842 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
844 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
845 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
848 if (gw_family
== AF_INET
)
849 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
850 &nexthop
->gate
.ipv4
, bytelen
);
852 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
853 &nexthop
->gate
.ipv6
, bytelen
);
857 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
860 struct rtnexthop
*rtnh
,
861 size_t req_size
, int bytelen
,
862 struct nexthop
*nexthop
)
864 if (route_family
== AF_MPLS
) {
865 struct gw_family_t gw_fam
;
867 gw_fam
.family
= gw_family
;
868 if (gw_family
== AF_INET
)
869 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
871 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
872 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
874 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
876 if (gw_family
== AF_INET
)
877 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
878 &nexthop
->gate
.ipv4
, bytelen
);
880 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
881 &nexthop
->gate
.ipv6
, bytelen
);
882 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
886 /* This function takes a nexthop as argument and adds
887 * the appropriate netlink attributes to an existing
890 * @param routedesc: Human readable description of route type
891 * (direct/recursive, single-/multipath)
892 * @param bytelen: Length of addresses in bytes.
893 * @param nexthop: Nexthop information
894 * @param nlmsg: nlmsghdr structure to fill in.
895 * @param req_size: The size allocated for the message.
897 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
898 struct nexthop
*nexthop
,
899 struct nlmsghdr
*nlmsg
,
901 size_t req_size
, int cmd
)
903 struct mpls_label_stack
*nh_label
;
904 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
909 * label_buf is *only* currently used within debugging.
910 * As such when we assign it we are guarding it inside
911 * a debug test. If you want to change this make sure
912 * you fix this assumption
917 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
920 nh_label
= nh
->nh_label
;
921 if (!nh_label
|| !nh_label
->num_labels
)
924 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
925 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
928 if (IS_ZEBRA_DEBUG_KERNEL
) {
930 sprintf(label_buf
, "label %u",
933 sprintf(label_buf1
, "/%u",
935 strlcat(label_buf
, label_buf1
,
940 out_lse
[num_labels
] =
941 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
947 /* Set the BoS bit */
948 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
950 if (rtmsg
->rtm_family
== AF_MPLS
)
951 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
952 num_labels
* sizeof(mpls_lse_t
));
955 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
957 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
959 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
960 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
961 num_labels
* sizeof(mpls_lse_t
));
962 addattr_nest_end(nlmsg
, nest
);
966 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
967 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
969 if (rtmsg
->rtm_family
== AF_INET
970 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
971 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
972 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
973 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
974 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
976 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
977 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
978 &nexthop
->rmap_src
.ipv4
, bytelen
);
979 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
980 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
981 &nexthop
->src
.ipv4
, bytelen
);
983 if (IS_ZEBRA_DEBUG_KERNEL
)
985 " 5549: _netlink_route_build_singlepath() (%s): "
986 "nexthop via %s %s if %u(%u)",
987 routedesc
, ipv4_ll_buf
, label_buf
,
988 nexthop
->ifindex
, nexthop
->vrf_id
);
992 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
993 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
994 /* Send deletes to the kernel without specifying the next-hop */
995 if (cmd
!= RTM_DELROUTE
)
996 _netlink_route_nl_add_gateway_info(
997 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1000 if (cmd
== RTM_NEWROUTE
) {
1001 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1002 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1003 &nexthop
->rmap_src
.ipv4
, bytelen
);
1004 else if (nexthop
->src
.ipv4
.s_addr
)
1005 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1006 &nexthop
->src
.ipv4
, bytelen
);
1009 if (IS_ZEBRA_DEBUG_KERNEL
)
1011 "netlink_route_multipath() (%s): "
1012 "nexthop via %s %s if %u(%u)",
1013 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1014 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1017 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1018 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1019 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1020 nlmsg
, req_size
, bytelen
,
1023 if (cmd
== RTM_NEWROUTE
) {
1024 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1025 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1026 &nexthop
->rmap_src
.ipv6
, bytelen
);
1027 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1028 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1029 &nexthop
->src
.ipv6
, bytelen
);
1032 if (IS_ZEBRA_DEBUG_KERNEL
)
1034 "netlink_route_multipath() (%s): "
1035 "nexthop via %s %s if %u(%u)",
1036 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1037 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1041 * We have the ifindex so we should always send it
1042 * This is especially useful if we are doing route
1045 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1046 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1048 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1049 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1050 if (cmd
== RTM_NEWROUTE
) {
1051 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1052 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1053 &nexthop
->rmap_src
.ipv4
, bytelen
);
1054 else if (nexthop
->src
.ipv4
.s_addr
)
1055 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1056 &nexthop
->src
.ipv4
, bytelen
);
1059 if (IS_ZEBRA_DEBUG_KERNEL
)
1061 "netlink_route_multipath() (%s): "
1062 "nexthop via if %u(%u)",
1063 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1066 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1067 if (cmd
== RTM_NEWROUTE
) {
1068 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1069 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1070 &nexthop
->rmap_src
.ipv6
, bytelen
);
1071 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1072 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1073 &nexthop
->src
.ipv6
, bytelen
);
1076 if (IS_ZEBRA_DEBUG_KERNEL
)
1078 "netlink_route_multipath() (%s): "
1079 "nexthop via if %u(%u)",
1080 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1084 /* This function takes a nexthop as argument and
1085 * appends to the given rtattr/rtnexthop pair the
1086 * representation of the nexthop. If the nexthop
1087 * defines a preferred source, the src parameter
1088 * will be modified to point to that src, otherwise
1089 * it will be kept unmodified.
1091 * @param routedesc: Human readable description of route type
1092 * (direct/recursive, single-/multipath)
1093 * @param bytelen: Length of addresses in bytes.
1094 * @param nexthop: Nexthop information
1095 * @param rta: rtnetlink attribute structure
1096 * @param rtnh: pointer to an rtnetlink nexthop structure
1097 * @param src: pointer pointing to a location where
1098 * the prefsrc should be stored.
1100 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1101 struct nexthop
*nexthop
,
1103 struct rtnexthop
*rtnh
,
1104 struct rtmsg
*rtmsg
,
1107 struct mpls_label_stack
*nh_label
;
1108 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1110 char label_buf
[256];
1112 rtnh
->rtnh_len
= sizeof(*rtnh
);
1113 rtnh
->rtnh_flags
= 0;
1114 rtnh
->rtnh_hops
= 0;
1115 rta
->rta_len
+= rtnh
->rtnh_len
;
1118 * label_buf is *only* currently used within debugging.
1119 * As such when we assign it we are guarding it inside
1120 * a debug test. If you want to change this make sure
1121 * you fix this assumption
1123 label_buf
[0] = '\0';
1126 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1127 char label_buf1
[20];
1129 nh_label
= nh
->nh_label
;
1130 if (!nh_label
|| !nh_label
->num_labels
)
1133 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1134 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1137 if (IS_ZEBRA_DEBUG_KERNEL
) {
1139 sprintf(label_buf
, "label %u",
1140 nh_label
->label
[i
]);
1142 sprintf(label_buf1
, "/%u",
1143 nh_label
->label
[i
]);
1144 strlcat(label_buf
, label_buf1
,
1149 out_lse
[num_labels
] =
1150 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1156 /* Set the BoS bit */
1157 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1159 if (rtmsg
->rtm_family
== AF_MPLS
) {
1160 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1162 num_labels
* sizeof(mpls_lse_t
));
1164 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1166 struct rtattr
*nest
;
1167 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1168 int len
= rta
->rta_len
;
1170 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1171 &encap
, sizeof(uint16_t));
1172 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1173 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1175 num_labels
* sizeof(mpls_lse_t
));
1176 rta_nest_end(rta
, nest
);
1177 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1181 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1182 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1184 if (rtmsg
->rtm_family
== AF_INET
1185 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1186 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1188 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1189 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1191 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1192 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1194 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1195 *src
= &nexthop
->rmap_src
;
1196 else if (nexthop
->src
.ipv4
.s_addr
)
1197 *src
= &nexthop
->src
;
1199 if (IS_ZEBRA_DEBUG_KERNEL
)
1201 " 5549: netlink_route_build_multipath() (%s): "
1202 "nexthop via %s %s if %u",
1203 routedesc
, ipv4_ll_buf
, label_buf
,
1208 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1209 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1210 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1211 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1213 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1214 *src
= &nexthop
->rmap_src
;
1215 else if (nexthop
->src
.ipv4
.s_addr
)
1216 *src
= &nexthop
->src
;
1218 if (IS_ZEBRA_DEBUG_KERNEL
)
1220 "netlink_route_multipath() (%s): "
1221 "nexthop via %s %s if %u",
1222 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1223 label_buf
, nexthop
->ifindex
);
1225 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1226 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1227 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1228 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1231 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1232 *src
= &nexthop
->rmap_src
;
1233 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1234 *src
= &nexthop
->src
;
1236 if (IS_ZEBRA_DEBUG_KERNEL
)
1238 "netlink_route_multipath() (%s): "
1239 "nexthop via %s %s if %u",
1240 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1241 label_buf
, nexthop
->ifindex
);
1245 * We have figured out the ifindex so we should always send it
1246 * This is especially useful if we are doing route
1249 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1250 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1253 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1254 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1255 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1256 *src
= &nexthop
->rmap_src
;
1257 else if (nexthop
->src
.ipv4
.s_addr
)
1258 *src
= &nexthop
->src
;
1260 if (IS_ZEBRA_DEBUG_KERNEL
)
1262 "netlink_route_multipath() (%s): "
1263 "nexthop via if %u",
1264 routedesc
, nexthop
->ifindex
);
1268 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1269 zebra_nhlfe_t
*nhlfe
,
1270 struct nlmsghdr
*nlmsg
,
1271 struct rtmsg
*rtmsg
,
1272 size_t req_size
, int cmd
)
1277 family
= NHLFE_FAMILY(nhlfe
);
1278 bytelen
= (family
== AF_INET
? 4 : 16);
1279 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1280 nlmsg
, rtmsg
, req_size
, cmd
);
1285 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1286 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1287 struct rtmsg
*rtmsg
, union g_addr
**src
)
1292 family
= NHLFE_FAMILY(nhlfe
);
1293 bytelen
= (family
== AF_INET
? 4 : 16);
1294 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1299 /* Log debug information for netlink_route_multipath
1300 * if debug logging is enabled.
1302 * @param cmd: Netlink command which is to be processed
1303 * @param p: Prefix for which the change is due
1304 * @param family: Address family which the change concerns
1305 * @param zvrf: The vrf we are in
1306 * @param tableid: The table we are working on
1308 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1309 int family
, vrf_id_t vrfid
,
1312 if (IS_ZEBRA_DEBUG_KERNEL
) {
1313 char buf
[PREFIX_STRLEN
];
1315 "netlink_route_multipath(): %s %s vrf %u(%u)",
1316 nl_msg_type_to_str(cmd
),
1317 prefix2str(p
, buf
, sizeof(buf
)),
1322 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1324 if (IS_ZEBRA_DEBUG_KERNEL
)
1325 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1326 nl_msg_type_to_str(cmd
), label
);
1329 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1330 int llalen
, ns_id_t ns_id
)
1338 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1340 memset(&req
, 0, sizeof(req
));
1342 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1343 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1344 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1345 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1347 req
.ndm
.ndm_family
= AF_INET
;
1348 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1349 req
.ndm
.ndm_ifindex
= ifindex
;
1350 req
.ndm
.ndm_type
= RTN_UNICAST
;
1352 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1353 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1355 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1359 /* Routing table change via netlink interface. */
1360 /* Update flag indicates whether this is a "replace" or not. */
1361 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1362 const struct prefix
*src_p
,
1363 struct route_entry
*re
,
1367 struct sockaddr_nl snl
;
1368 struct nexthop
*nexthop
= NULL
;
1369 unsigned int nexthop_num
;
1370 int family
= PREFIX_FAMILY(p
);
1371 const char *routedesc
;
1378 char buf
[NL_PKT_BUF_SIZE
];
1381 struct zebra_ns
*zns
;
1382 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1385 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1387 bytelen
= (family
== AF_INET
? 4 : 16);
1389 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1390 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1391 if ((cmd
== RTM_NEWROUTE
) && update
)
1392 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1393 req
.n
.nlmsg_type
= cmd
;
1394 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1396 req
.r
.rtm_family
= family
;
1397 req
.r
.rtm_dst_len
= p
->prefixlen
;
1398 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1399 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1400 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1403 * blackhole routes are not RTN_UNICAST, they are
1404 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1405 * so setting this value as a RTN_UNICAST would
1406 * cause the route lookup of just the prefix
1407 * to fail. So no need to specify this for
1408 * the RTM_DELROUTE case
1410 if (cmd
!= RTM_DELROUTE
)
1411 req
.r
.rtm_type
= RTN_UNICAST
;
1413 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1415 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1419 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1421 * either by the kernel or by zebra. Its purely for calculating best
1423 * by the routing protocol and for communicating with protocol peers.
1425 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1426 #if defined(SUPPORT_REALMS)
1427 if (re
->tag
> 0 && re
->tag
<= 255)
1428 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1430 /* Table corresponding to this route. */
1431 if (re
->table
< 256)
1432 req
.r
.rtm_table
= re
->table
;
1434 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1435 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1438 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1441 * If we are not updating the route and we have received
1442 * a route delete, then all we need to fill in is the
1443 * prefix information to tell the kernel to schwack
1446 if (!update
&& cmd
== RTM_DELROUTE
)
1449 if (re
->mtu
|| re
->nexthop_mtu
) {
1450 char buf
[NL_PKT_BUF_SIZE
];
1451 struct rtattr
*rta
= (void *)buf
;
1452 uint32_t mtu
= re
->mtu
;
1453 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1454 mtu
= re
->nexthop_mtu
;
1455 rta
->rta_type
= RTA_METRICS
;
1456 rta
->rta_len
= RTA_LENGTH(0);
1457 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1458 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1462 /* Count overall nexthops so we can decide whether to use singlepath
1463 * or multipath case. */
1465 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1466 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1468 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1470 if (cmd
== RTM_DELROUTE
1471 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1477 /* Singlepath case. */
1478 if (nexthop_num
== 1 || multipath_num
== 1) {
1480 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1482 * So we want to cover 2 types of blackhole
1484 * 1) A normal blackhole route( ala from a static
1486 * 2) A recursively resolved blackhole route
1488 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1489 switch (nexthop
->bh_type
) {
1490 case BLACKHOLE_ADMINPROHIB
:
1491 req
.r
.rtm_type
= RTN_PROHIBIT
;
1493 case BLACKHOLE_REJECT
:
1494 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1497 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1502 if (CHECK_FLAG(nexthop
->flags
,
1503 NEXTHOP_FLAG_RECURSIVE
)) {
1505 if (family
== AF_INET
) {
1506 if (nexthop
->rmap_src
.ipv4
1513 } else if (nexthop
->src
.ipv4
1521 } else if (family
== AF_INET6
) {
1522 if (!IN6_IS_ADDR_UNSPECIFIED(
1530 !IN6_IS_ADDR_UNSPECIFIED(
1543 if ((cmd
== RTM_NEWROUTE
1544 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1545 || (cmd
== RTM_DELROUTE
1546 && CHECK_FLAG(nexthop
->flags
,
1547 NEXTHOP_FLAG_FIB
))) {
1548 routedesc
= nexthop
->rparent
1549 ? "recursive, single-path"
1552 _netlink_route_build_singlepath(
1553 routedesc
, bytelen
, nexthop
, &req
.n
,
1554 &req
.r
, sizeof req
, cmd
);
1559 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1560 if (family
== AF_INET
)
1561 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1562 &src
.ipv4
, bytelen
);
1563 else if (family
== AF_INET6
)
1564 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1565 &src
.ipv6
, bytelen
);
1568 char buf
[NL_PKT_BUF_SIZE
];
1569 struct rtattr
*rta
= (void *)buf
;
1570 struct rtnexthop
*rtnh
;
1571 union g_addr
*src1
= NULL
;
1573 rta
->rta_type
= RTA_MULTIPATH
;
1574 rta
->rta_len
= RTA_LENGTH(0);
1575 rtnh
= RTA_DATA(rta
);
1578 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1579 if (nexthop_num
>= multipath_num
)
1582 if (CHECK_FLAG(nexthop
->flags
,
1583 NEXTHOP_FLAG_RECURSIVE
)) {
1584 /* This only works for IPv4 now */
1586 if (family
== AF_INET
) {
1587 if (nexthop
->rmap_src
.ipv4
1594 } else if (nexthop
->src
.ipv4
1602 } else if (family
== AF_INET6
) {
1603 if (!IN6_IS_ADDR_UNSPECIFIED(
1611 !IN6_IS_ADDR_UNSPECIFIED(
1624 if ((cmd
== RTM_NEWROUTE
1625 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1626 || (cmd
== RTM_DELROUTE
1627 && CHECK_FLAG(nexthop
->flags
,
1628 NEXTHOP_FLAG_FIB
))) {
1629 routedesc
= nexthop
->rparent
1630 ? "recursive, multipath"
1634 _netlink_route_build_multipath(
1635 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1637 rtnh
= RTNH_NEXT(rtnh
);
1639 if (!setsrc
&& src1
) {
1640 if (family
== AF_INET
)
1641 src
.ipv4
= src1
->ipv4
;
1642 else if (family
== AF_INET6
)
1643 src
.ipv6
= src1
->ipv6
;
1649 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1650 if (family
== AF_INET
)
1651 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1652 &src
.ipv4
, bytelen
);
1653 else if (family
== AF_INET6
)
1654 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1655 &src
.ipv6
, bytelen
);
1656 if (IS_ZEBRA_DEBUG_KERNEL
)
1657 zlog_debug("Setting source");
1660 if (rta
->rta_len
> RTA_LENGTH(0))
1661 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1662 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1665 /* If there is no useful nexthop then return. */
1666 if (nexthop_num
== 0) {
1667 if (IS_ZEBRA_DEBUG_KERNEL
)
1669 "netlink_route_multipath(): No useful nexthop.");
1675 /* Destination netlink address. */
1676 memset(&snl
, 0, sizeof snl
);
1677 snl
.nl_family
= AF_NETLINK
;
1679 /* Talk to netlink socket. */
1680 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1684 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1687 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1695 struct zebra_ns
*zns
;
1698 memset(&req
, 0, sizeof(req
));
1700 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1701 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1702 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1704 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1705 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1707 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1708 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1709 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1710 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1711 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1713 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1714 &zns
->netlink_cmd
, zns
, 0);
1720 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1721 const struct prefix
*p
,
1722 const struct prefix
*src_p
,
1723 struct route_entry
*old
,
1724 struct route_entry
*new)
1731 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1732 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1733 new, (old
) ? 1 : 0);
1736 * So v6 route replace semantics are not in
1737 * the kernel at this point as I understand it.
1738 * So let's do a delete than an add.
1739 * In the future once v6 route replace semantics
1740 * are in we can figure out what to do here to
1741 * allow working with old and new kernels.
1743 * I'm also intentionally ignoring the failure case
1744 * of the route delete. If that happens yeah we're
1748 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1750 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1753 kernel_route_rib_pass_fail(rn
, p
, new,
1754 (!ret
) ? DP_INSTALL_SUCCESS
1755 : DP_INSTALL_FAILURE
);
1756 return DP_REQUEST_SUCCESS
;
1760 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1762 kernel_route_rib_pass_fail(rn
, p
, old
,
1763 (!ret
) ? DP_DELETE_SUCCESS
1764 : DP_DELETE_FAILURE
);
1767 return DP_REQUEST_SUCCESS
;
1770 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1771 int llalen
, ns_id_t ns_id
)
1773 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1774 addr
, lla
, llalen
, ns_id
);
1778 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1779 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1781 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1782 struct in_addr
*vtep_ip
, int cmd
)
1784 struct zebra_ns
*zns
;
1790 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1791 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1794 memset(&req
, 0, sizeof(req
));
1796 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1797 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1798 if (cmd
== RTM_NEWNEIGH
)
1799 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1800 req
.n
.nlmsg_type
= cmd
;
1801 req
.ndm
.ndm_family
= PF_BRIDGE
;
1802 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1803 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1806 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1807 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1808 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1810 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1815 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1817 * a "flood" MAC FDB entry.
1819 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1821 if (IS_ZEBRA_DEBUG_VXLAN
)
1822 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1823 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1825 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1829 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1830 * deleting the "flood" MAC FDB entry.
1832 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1834 if (IS_ZEBRA_DEBUG_VXLAN
)
1836 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1837 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1839 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1843 #define NDA_RTA(r) \
1844 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1847 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1850 struct interface
*ifp
;
1851 struct zebra_if
*zif
;
1852 struct rtattr
*tb
[NDA_MAX
+ 1];
1853 struct interface
*br_if
;
1856 struct prefix vtep_ip
;
1857 int vid_present
= 0, dst_present
= 0;
1858 char buf
[ETHER_ADDR_STRLEN
];
1863 ndm
= NLMSG_DATA(h
);
1865 /* We only process macfdb notifications if EVPN is enabled */
1866 if (!is_evpn_enabled())
1869 /* The interface should exist. */
1870 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1872 if (!ifp
|| !ifp
->info
)
1875 /* The interface should be something we're interested in. */
1876 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1879 /* Drop "permanent" entries. */
1880 if (ndm
->ndm_state
& NUD_PERMANENT
)
1883 zif
= (struct zebra_if
*)ifp
->info
;
1884 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1885 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1886 nl_msg_type_to_str(h
->nlmsg_type
),
1887 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1888 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1892 /* Parse attributes and extract fields of interest. */
1893 memset(tb
, 0, sizeof tb
);
1894 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1896 if (!tb
[NDA_LLADDR
]) {
1897 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1898 nl_msg_type_to_str(h
->nlmsg_type
),
1899 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1900 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1904 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1906 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1907 nl_msg_type_to_str(h
->nlmsg_type
),
1908 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1909 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1910 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1914 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1916 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1918 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1919 sprintf(vid_buf
, " VLAN %u", vid
);
1923 /* TODO: Only IPv4 supported now. */
1925 vtep_ip
.family
= AF_INET
;
1926 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1927 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1929 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1932 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1934 if (IS_ZEBRA_DEBUG_KERNEL
)
1935 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1936 nl_msg_type_to_str(h
->nlmsg_type
),
1937 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1938 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1939 sticky
? "sticky " : "",
1940 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1941 dst_present
? dst_buf
: "");
1943 if (filter_vlan
&& vid
!= filter_vlan
)
1946 /* If add or update, do accordingly if learnt on a "local" interface; if
1947 * the notification is over VxLAN, this has to be related to
1949 * so perform an implicit delete of any local entry (if it exists).
1951 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1952 /* Drop "permanent" entries. */
1953 if (ndm
->ndm_state
& NUD_PERMANENT
)
1956 if (IS_ZEBRA_IF_VXLAN(ifp
))
1957 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1960 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1964 /* This is a delete notification.
1965 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1966 * 2. For a MAC over "local" interface, delete the mac
1967 * Note: We will get notifications from both bridge driver and VxLAN
1969 * Ignore the notification from VxLan driver as it is also generated
1970 * when mac moves from remote to local.
1975 if (IS_ZEBRA_IF_VXLAN(ifp
))
1976 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1979 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1982 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1987 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1990 /* Length validity. */
1991 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1995 /* We are interested only in AF_BRIDGE notifications. */
1996 ndm
= NLMSG_DATA(h
);
1997 if (ndm
->ndm_family
!= AF_BRIDGE
)
2000 return netlink_macfdb_change(h
, len
, ns_id
);
2003 /* Request for MAC FDB information from the kernel */
2004 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2005 ifindex_t master_ifindex
)
2009 struct ifinfomsg ifm
;
2013 /* Form the request, specifying filter (rtattr) if needed. */
2014 memset(&req
, 0, sizeof(req
));
2015 req
.n
.nlmsg_type
= type
;
2016 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2017 req
.ifm
.ifi_family
= family
;
2019 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2021 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2025 * MAC forwarding database read using netlink interface. This is invoked
2028 int netlink_macfdb_read(struct zebra_ns
*zns
)
2032 /* Get bridge FDB table. */
2033 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2036 /* We are reading entire table. */
2038 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2045 * MAC forwarding database read using netlink interface. This is for a
2046 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2048 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2049 struct interface
*br_if
)
2051 struct zebra_if
*br_zif
;
2052 struct zebra_if
*zif
;
2053 struct zebra_l2info_vxlan
*vxl
;
2057 /* Save VLAN we're filtering on, if needed. */
2058 br_zif
= (struct zebra_if
*)br_if
->info
;
2059 zif
= (struct zebra_if
*)ifp
->info
;
2060 vxl
= &zif
->l2info
.vxl
;
2061 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2062 filter_vlan
= vxl
->access_vlan
;
2064 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2066 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2070 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2073 /* Reset VLAN filter. */
2078 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2079 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2080 int local
, int cmd
, uint8_t sticky
)
2082 struct zebra_ns
*zns
;
2089 struct zebra_if
*zif
;
2090 struct interface
*br_if
;
2091 struct zebra_if
*br_zif
;
2092 char buf
[ETHER_ADDR_STRLEN
];
2093 int vid_present
= 0, dst_present
= 0;
2096 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2100 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2101 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2102 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2107 memset(&req
, 0, sizeof(req
));
2109 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2110 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2111 if (cmd
== RTM_NEWNEIGH
)
2112 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2113 req
.n
.nlmsg_type
= cmd
;
2114 req
.ndm
.ndm_family
= AF_BRIDGE
;
2115 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2116 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2119 req
.ndm
.ndm_state
|= NUD_NOARP
;
2121 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2123 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2124 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2126 dst_alen
= 4; // TODO: hardcoded
2127 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2129 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2131 br_zif
= (struct zebra_if
*)br_if
->info
;
2132 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2133 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2135 sprintf(vid_buf
, " VLAN %u", vid
);
2137 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2139 if (IS_ZEBRA_DEBUG_KERNEL
)
2140 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2141 nl_msg_type_to_str(cmd
),
2142 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2143 ifp
->ifindex
, vid_present
? vid_buf
: "",
2144 sticky
? "sticky " : "",
2145 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2146 dst_present
? dst_buf
: "");
2148 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2153 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2156 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2159 struct interface
*ifp
;
2160 struct zebra_if
*zif
;
2161 struct rtattr
*tb
[NDA_MAX
+ 1];
2162 struct interface
*link_if
;
2165 char buf
[ETHER_ADDR_STRLEN
];
2166 char buf2
[INET6_ADDRSTRLEN
];
2167 int mac_present
= 0;
2168 uint8_t ext_learned
;
2170 ndm
= NLMSG_DATA(h
);
2172 /* The interface should exist. */
2173 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2175 if (!ifp
|| !ifp
->info
)
2178 zif
= (struct zebra_if
*)ifp
->info
;
2180 /* Parse attributes and extract fields of interest. */
2181 memset(tb
, 0, sizeof tb
);
2182 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2185 zlog_warn("%s family %s IF %s(%u) - no DST",
2186 nl_msg_type_to_str(h
->nlmsg_type
),
2187 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2192 memset(&ip
, 0, sizeof(struct ipaddr
));
2193 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2194 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2196 /* Drop some "permanent" entries. */
2197 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2198 char buf
[16] = "169.254.0.1";
2199 struct in_addr ipv4_ll
;
2201 if (ndm
->ndm_family
!= AF_INET
)
2204 if (!zif
->v6_2_v4_ll_neigh_entry
)
2207 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2210 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2211 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2214 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2215 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2219 /* The neighbor is present on an SVI. From this, we locate the
2221 * bridge because we're only interested in neighbors on a VxLAN bridge.
2222 * The bridge is located based on the nature of the SVI:
2223 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2225 * and is linked to the bridge
2226 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2230 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2231 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2235 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2240 memset(&mac
, 0, sizeof(struct ethaddr
));
2241 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2242 if (tb
[NDA_LLADDR
]) {
2243 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2245 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2246 nl_msg_type_to_str(h
->nlmsg_type
),
2247 nl_family_to_str(ndm
->ndm_family
),
2248 ifp
->name
, ndm
->ndm_ifindex
,
2249 (unsigned long)RTA_PAYLOAD(
2255 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2258 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2260 if (IS_ZEBRA_DEBUG_KERNEL
)
2262 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2263 nl_msg_type_to_str(h
->nlmsg_type
),
2264 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2266 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2268 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2270 ndm
->ndm_state
, ndm
->ndm_flags
);
2272 /* If the neighbor state is valid for use, process as an add or
2274 * else process as a delete. Note that the delete handling may
2276 * in re-adding the neighbor if it is a valid "remote" neighbor.
2278 if (ndm
->ndm_state
& NUD_VALID
)
2279 return zebra_vxlan_handle_kernel_neigh_update(
2280 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2283 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2286 if (IS_ZEBRA_DEBUG_KERNEL
)
2287 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2288 nl_msg_type_to_str(h
->nlmsg_type
),
2289 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2291 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2293 /* Process the delete - it may result in re-adding the neighbor if it is
2294 * a valid "remote" neighbor.
2296 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2299 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2304 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2307 /* Length validity. */
2308 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2312 /* We are interested only in AF_INET or AF_INET6 notifications. */
2313 ndm
= NLMSG_DATA(h
);
2314 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2317 return netlink_neigh_change(h
, len
);
2320 /* Request for IP neighbor information from the kernel */
2321 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2330 /* Form the request, specifying filter (rtattr) if needed. */
2331 memset(&req
, 0, sizeof(req
));
2332 req
.n
.nlmsg_type
= type
;
2333 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2334 req
.ndm
.ndm_family
= family
;
2336 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2338 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2342 * IP Neighbor table read using netlink interface. This is invoked
2345 int netlink_neigh_read(struct zebra_ns
*zns
)
2349 /* Get IP neighbor table. */
2350 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2353 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2360 * IP Neighbor table read using netlink interface. This is for a specific
2363 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2367 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2371 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2377 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2382 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2385 /* Length validity. */
2386 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2388 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2389 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2390 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2394 /* Is this a notification for the MAC FDB or IP neighbor table? */
2395 ndm
= NLMSG_DATA(h
);
2396 if (ndm
->ndm_family
== AF_BRIDGE
)
2397 return netlink_macfdb_change(h
, len
, ns_id
);
2399 if (ndm
->ndm_type
!= RTN_UNICAST
)
2402 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2403 return netlink_ipneigh_change(h
, len
, ns_id
);
2406 "Invalid address family: %d received from kernel neighbor change: %d",
2407 ndm
->ndm_family
, h
->nlmsg_type
);
2414 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2415 struct ethaddr
*mac
, uint32_t flags
, int cmd
)
2424 struct zebra_ns
*zns
;
2425 char buf
[INET6_ADDRSTRLEN
];
2426 char buf2
[ETHER_ADDR_STRLEN
];
2427 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2430 memset(&req
, 0, sizeof(req
));
2432 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2433 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2434 if (cmd
== RTM_NEWNEIGH
)
2435 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2436 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2437 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2438 req
.ndm
.ndm_state
= flags
;
2439 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2440 req
.ndm
.ndm_type
= RTN_UNICAST
;
2441 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2444 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2445 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2447 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2449 if (IS_ZEBRA_DEBUG_KERNEL
)
2450 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2451 nl_msg_type_to_str(cmd
),
2452 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2453 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2454 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2457 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2461 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2462 struct in_addr vtep_ip
, uint8_t sticky
)
2464 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2468 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2469 struct in_addr vtep_ip
, int local
)
2471 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2475 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2476 struct ethaddr
*mac
)
2478 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_NOARP
, RTM_NEWNEIGH
);
2481 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2483 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2487 * MPLS label forwarding table change via netlink interface.
2489 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2492 zebra_nhlfe_t
*nhlfe
;
2493 struct nexthop
*nexthop
= NULL
;
2494 unsigned int nexthop_num
;
2495 const char *routedesc
;
2496 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2502 char buf
[NL_PKT_BUF_SIZE
];
2505 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2508 * Count # nexthops so we can decide whether to use singlepath
2509 * or multipath case.
2512 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2513 nexthop
= nhlfe
->nexthop
;
2516 if (cmd
== RTM_NEWROUTE
) {
2517 /* Count all selected NHLFEs */
2518 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2519 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2523 /* Count all installed NHLFEs */
2524 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2525 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2530 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2533 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2534 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2535 req
.n
.nlmsg_type
= cmd
;
2536 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2538 req
.r
.rtm_family
= AF_MPLS
;
2539 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2540 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2541 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2542 req
.r
.rtm_type
= RTN_UNICAST
;
2544 if (cmd
== RTM_NEWROUTE
) {
2545 /* We do a replace to handle update. */
2546 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2548 /* set the protocol value if installing */
2549 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2550 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2553 /* Fill destination */
2554 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2555 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2557 /* Fill nexthops (paths) based on single-path or multipath. The paths
2558 * chosen depend on the operation.
2560 if (nexthop_num
== 1 || multipath_num
== 1) {
2561 routedesc
= "single-path";
2562 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2565 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2566 nexthop
= nhlfe
->nexthop
;
2570 if ((cmd
== RTM_NEWROUTE
2571 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2572 && CHECK_FLAG(nexthop
->flags
,
2573 NEXTHOP_FLAG_ACTIVE
)))
2574 || (cmd
== RTM_DELROUTE
2575 && (CHECK_FLAG(nhlfe
->flags
,
2576 NHLFE_FLAG_INSTALLED
)
2577 && CHECK_FLAG(nexthop
->flags
,
2578 NEXTHOP_FLAG_FIB
)))) {
2579 /* Add the gateway */
2580 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2587 } else /* Multipath case */
2589 char buf
[NL_PKT_BUF_SIZE
];
2590 struct rtattr
*rta
= (void *)buf
;
2591 struct rtnexthop
*rtnh
;
2592 union g_addr
*src1
= NULL
;
2594 rta
->rta_type
= RTA_MULTIPATH
;
2595 rta
->rta_len
= RTA_LENGTH(0);
2596 rtnh
= RTA_DATA(rta
);
2598 routedesc
= "multipath";
2599 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2602 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2603 nexthop
= nhlfe
->nexthop
;
2607 if (nexthop_num
>= multipath_num
)
2610 if ((cmd
== RTM_NEWROUTE
2611 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2612 && CHECK_FLAG(nexthop
->flags
,
2613 NEXTHOP_FLAG_ACTIVE
)))
2614 || (cmd
== RTM_DELROUTE
2615 && (CHECK_FLAG(nhlfe
->flags
,
2616 NHLFE_FLAG_INSTALLED
)
2617 && CHECK_FLAG(nexthop
->flags
,
2618 NEXTHOP_FLAG_FIB
)))) {
2621 /* Build the multipath */
2622 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2625 rtnh
= RTNH_NEXT(rtnh
);
2629 /* Add the multipath */
2630 if (rta
->rta_len
> RTA_LENGTH(0))
2631 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2632 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2635 /* Talk to netlink socket. */
2636 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2639 #endif /* HAVE_NETLINK */