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
;
390 if (rtm
->rtm_src_len
!= 0) {
391 char buf
[PREFIX_STRLEN
];
392 zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)",
393 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
397 /* Force debug below to not display anything for source */
399 } else if (rtm
->rtm_family
== AF_INET6
) {
401 memcpy(&p
.u
.prefix6
, dest
, 16);
402 p
.prefixlen
= rtm
->rtm_dst_len
;
404 src_p
.family
= AF_INET6
;
405 memcpy(&src_p
.prefix
, src
, 16);
406 src_p
.prefixlen
= rtm
->rtm_src_len
;
410 * For ZEBRA_ROUTE_KERNEL types:
412 * The metric/priority of the route received from the kernel
413 * is a 32 bit number. We are going to interpret the high
414 * order byte as the Admin Distance and the low order 3 bytes
417 * This will allow us to do two things:
418 * 1) Allow the creation of kernel routes that can be
419 * overridden by zebra.
420 * 2) Allow the old behavior for 'most' kernel route types
421 * if a user enters 'ip route ...' v4 routes get a metric
422 * of 0 and v6 routes get a metric of 1024. Both of these
423 * values will end up with a admin distance of 0, which
424 * will cause them to win for the purposes of zebra.
426 if (proto
== ZEBRA_ROUTE_KERNEL
) {
427 distance
= (metric
>> 24) & 0xFF;
428 metric
= (metric
& 0x00FFFFFF);
431 if (IS_ZEBRA_DEBUG_KERNEL
) {
432 char buf
[PREFIX_STRLEN
];
433 char buf2
[PREFIX_STRLEN
];
434 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
435 nl_msg_type_to_str(h
->nlmsg_type
),
436 prefix2str(&p
, buf
, sizeof(buf
)),
437 src_p
.prefixlen
? " from " : "",
439 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
441 vrf_id
, table
, metric
, distance
);
445 if (rtm
->rtm_family
== AF_INET6
)
448 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
449 struct interface
*ifp
;
450 vrf_id_t nh_vrf_id
= vrf_id
;
452 if (!tb
[RTA_MULTIPATH
]) {
454 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
456 memset(&nh
, 0, sizeof(nh
));
458 if (bh_type
== BLACKHOLE_UNSPEC
) {
460 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
461 else if (index
&& gate
)
464 ? NEXTHOP_TYPE_IPV4_IFINDEX
465 : NEXTHOP_TYPE_IPV6_IFINDEX
;
466 else if (!index
&& gate
)
467 nh
.type
= (afi
== AFI_IP
)
471 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
472 nh
.bh_type
= bh_type
;
475 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
476 nh
.bh_type
= bh_type
;
480 memcpy(&nh
.src
, prefsrc
, sz
);
482 memcpy(&nh
.gate
, gate
, sz
);
485 ifp
= if_lookup_by_index_per_ns(
486 zebra_ns_lookup(ns_id
),
489 nh_vrf_id
= ifp
->vrf_id
;
491 nh
.vrf_id
= nh_vrf_id
;
493 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
494 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
496 /* This is a multipath route */
498 struct route_entry
*re
;
499 struct rtnexthop
*rtnh
=
500 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
502 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
504 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
506 re
->distance
= distance
;
513 re
->uptime
= time(NULL
);
518 if (len
< (int)sizeof(*rtnh
)
519 || rtnh
->rtnh_len
> len
)
522 index
= rtnh
->rtnh_ifindex
;
525 * Yes we are looking this up
526 * for every nexthop and just
527 * using the last one looked
530 ifp
= if_lookup_by_index_per_ns(
531 zebra_ns_lookup(ns_id
),
534 nh_vrf_id
= ifp
->vrf_id
;
537 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
540 nh_vrf_id
= VRF_DEFAULT
;
546 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
547 memset(tb
, 0, sizeof(tb
));
548 netlink_parse_rtattr(
549 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
550 rtnh
->rtnh_len
- sizeof(*rtnh
));
557 if (rtm
->rtm_family
== AF_INET
) {
559 route_entry_nexthop_ipv4_ifindex_add(
564 route_entry_nexthop_ipv4_add(
568 } else if (rtm
->rtm_family
571 route_entry_nexthop_ipv6_ifindex_add(
575 route_entry_nexthop_ipv6_add(
580 route_entry_nexthop_ifindex_add(
581 re
, index
, nh_vrf_id
);
583 if (rtnh
->rtnh_len
== 0)
586 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
587 rtnh
= RTNH_NEXT(rtnh
);
590 zserv_nexthop_num_warn(__func__
,
591 (const struct prefix
*)&p
,
593 if (re
->nexthop_num
== 0)
596 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
600 if (!tb
[RTA_MULTIPATH
]) {
602 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
604 memset(&nh
, 0, sizeof(nh
));
605 if (bh_type
== BLACKHOLE_UNSPEC
) {
607 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
608 else if (index
&& gate
)
611 ? NEXTHOP_TYPE_IPV4_IFINDEX
612 : NEXTHOP_TYPE_IPV6_IFINDEX
;
613 else if (!index
&& gate
)
614 nh
.type
= (afi
== AFI_IP
)
618 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
619 nh
.bh_type
= BLACKHOLE_UNSPEC
;
622 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
623 nh
.bh_type
= bh_type
;
627 memcpy(&nh
.gate
, gate
, sz
);
628 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
629 &p
, &src_p
, &nh
, table
, metric
, true);
631 /* XXX: need to compare the entire list of nexthops
632 * here for NLM_F_APPEND stupidity */
633 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
634 &p
, &src_p
, NULL
, table
, metric
, true);
641 static struct mcast_route_data
*mroute
= NULL
;
643 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
644 ns_id_t ns_id
, int startup
)
648 struct rtattr
*tb
[RTA_MAX
+ 1];
649 struct mcast_route_data
*m
;
650 struct mcast_route_data mr
;
657 char oif_list
[256] = "\0";
664 memset(&mr
, 0, sizeof(mr
));
670 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
672 memset(tb
, 0, sizeof tb
);
673 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
676 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
678 table
= rtm
->rtm_table
;
680 vrf
= vrf_lookup_by_table(table
, ns_id
);
683 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
686 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
689 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
691 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
692 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
694 if (tb
[RTA_MULTIPATH
]) {
695 struct rtnexthop
*rtnh
=
696 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
698 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
700 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
703 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
706 if (rtnh
->rtnh_len
== 0)
709 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
710 rtnh
= RTNH_NEXT(rtnh
);
714 if (IS_ZEBRA_DEBUG_KERNEL
) {
715 struct interface
*ifp
;
716 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
717 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
718 for (count
= 0; count
< oif_count
; count
++) {
719 ifp
= if_lookup_by_index(oif
[count
], vrf
);
722 sprintf(temp
, "%s ", ifp
->name
);
723 strcat(oif_list
, temp
);
725 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
726 ifp
= if_lookup_by_index(iif
, vrf
);
728 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
729 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
730 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
735 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
742 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
743 /* If this is not route add/delete message print warning. */
744 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
748 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
749 || rtm
->rtm_family
== AF_ETHERNET
750 || rtm
->rtm_family
== AF_MPLS
)) {
752 "Invalid address family: %d received from kernel route change: %d",
753 rtm
->rtm_family
, h
->nlmsg_type
);
757 /* Connected route. */
758 if (IS_ZEBRA_DEBUG_KERNEL
)
759 zlog_debug("%s %s %s proto %s NS %u",
760 nl_msg_type_to_str(h
->nlmsg_type
),
761 nl_family_to_str(rtm
->rtm_family
),
762 nl_rttype_to_str(rtm
->rtm_type
),
763 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
765 /* We don't care about change notifications for the MPLS table. */
766 /* TODO: Revisit this. */
767 if (rtm
->rtm_family
== AF_MPLS
)
770 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
772 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
775 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
779 if (rtm
->rtm_type
== RTN_MULTICAST
)
780 netlink_route_change_read_multicast(h
, ns_id
, startup
);
782 netlink_route_change_read_unicast(h
, ns_id
, startup
);
786 /* Request for specific route information from the kernel */
787 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
794 /* Form the request, specifying filter (rtattr) if needed. */
795 memset(&req
, 0, sizeof(req
));
796 req
.n
.nlmsg_type
= type
;
797 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
798 req
.rtm
.rtm_family
= family
;
800 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
803 /* Routing table read function using netlink interface. Only called
805 int netlink_route_read(struct zebra_ns
*zns
)
809 /* Get IPv4 routing table. */
810 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
813 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
814 &zns
->netlink_cmd
, zns
, 0, 1);
818 /* Get IPv6 routing table. */
819 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
822 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
823 &zns
->netlink_cmd
, zns
, 0, 1);
830 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
832 struct nlmsghdr
*nlmsg
,
833 size_t req_size
, int bytelen
,
834 struct nexthop
*nexthop
)
836 if (route_family
== AF_MPLS
) {
837 struct gw_family_t gw_fam
;
839 gw_fam
.family
= gw_family
;
840 if (gw_family
== AF_INET
)
841 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
843 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
844 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
847 if (gw_family
== AF_INET
)
848 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
849 &nexthop
->gate
.ipv4
, bytelen
);
851 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
852 &nexthop
->gate
.ipv6
, bytelen
);
856 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
859 struct rtnexthop
*rtnh
,
860 size_t req_size
, int bytelen
,
861 struct nexthop
*nexthop
)
863 if (route_family
== AF_MPLS
) {
864 struct gw_family_t gw_fam
;
866 gw_fam
.family
= gw_family
;
867 if (gw_family
== AF_INET
)
868 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
870 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
871 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
873 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
875 if (gw_family
== AF_INET
)
876 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
877 &nexthop
->gate
.ipv4
, bytelen
);
879 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
880 &nexthop
->gate
.ipv6
, bytelen
);
881 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
885 /* This function takes a nexthop as argument and adds
886 * the appropriate netlink attributes to an existing
889 * @param routedesc: Human readable description of route type
890 * (direct/recursive, single-/multipath)
891 * @param bytelen: Length of addresses in bytes.
892 * @param nexthop: Nexthop information
893 * @param nlmsg: nlmsghdr structure to fill in.
894 * @param req_size: The size allocated for the message.
896 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
897 struct nexthop
*nexthop
,
898 struct nlmsghdr
*nlmsg
,
900 size_t req_size
, int cmd
)
902 struct mpls_label_stack
*nh_label
;
903 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
908 * label_buf is *only* currently used within debugging.
909 * As such when we assign it we are guarding it inside
910 * a debug test. If you want to change this make sure
911 * you fix this assumption
916 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
919 nh_label
= nh
->nh_label
;
920 if (!nh_label
|| !nh_label
->num_labels
)
923 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
924 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
927 if (IS_ZEBRA_DEBUG_KERNEL
) {
929 sprintf(label_buf
, "label %u",
932 sprintf(label_buf1
, "/%u",
934 strlcat(label_buf
, label_buf1
,
939 out_lse
[num_labels
] =
940 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
946 /* Set the BoS bit */
947 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
949 if (rtmsg
->rtm_family
== AF_MPLS
)
950 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
951 num_labels
* sizeof(mpls_lse_t
));
954 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
956 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
958 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
959 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
960 num_labels
* sizeof(mpls_lse_t
));
961 addattr_nest_end(nlmsg
, nest
);
965 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
966 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
968 if (rtmsg
->rtm_family
== AF_INET
969 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
970 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
971 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
972 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
973 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
975 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
976 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
977 &nexthop
->rmap_src
.ipv4
, bytelen
);
978 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
979 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
980 &nexthop
->src
.ipv4
, bytelen
);
982 if (IS_ZEBRA_DEBUG_KERNEL
)
984 " 5549: _netlink_route_build_singlepath() (%s): "
985 "nexthop via %s %s if %u(%u)",
986 routedesc
, ipv4_ll_buf
, label_buf
,
987 nexthop
->ifindex
, nexthop
->vrf_id
);
991 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
992 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
993 /* Send deletes to the kernel without specifying the next-hop */
994 if (cmd
!= RTM_DELROUTE
)
995 _netlink_route_nl_add_gateway_info(
996 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
999 if (cmd
== RTM_NEWROUTE
) {
1000 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1001 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1002 &nexthop
->rmap_src
.ipv4
, bytelen
);
1003 else if (nexthop
->src
.ipv4
.s_addr
)
1004 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1005 &nexthop
->src
.ipv4
, bytelen
);
1008 if (IS_ZEBRA_DEBUG_KERNEL
)
1010 "netlink_route_multipath() (%s): "
1011 "nexthop via %s %s if %u(%u)",
1012 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1013 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1016 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1017 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1018 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1019 nlmsg
, req_size
, bytelen
,
1022 if (cmd
== RTM_NEWROUTE
) {
1023 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1024 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1025 &nexthop
->rmap_src
.ipv6
, bytelen
);
1026 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1027 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1028 &nexthop
->src
.ipv6
, bytelen
);
1031 if (IS_ZEBRA_DEBUG_KERNEL
)
1033 "netlink_route_multipath() (%s): "
1034 "nexthop via %s %s if %u(%u)",
1035 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1036 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1040 * We have the ifindex so we should always send it
1041 * This is especially useful if we are doing route
1044 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1045 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1047 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1048 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1049 if (cmd
== RTM_NEWROUTE
) {
1050 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1051 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1052 &nexthop
->rmap_src
.ipv4
, bytelen
);
1053 else if (nexthop
->src
.ipv4
.s_addr
)
1054 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1055 &nexthop
->src
.ipv4
, bytelen
);
1058 if (IS_ZEBRA_DEBUG_KERNEL
)
1060 "netlink_route_multipath() (%s): "
1061 "nexthop via if %u(%u)",
1062 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1065 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1066 if (cmd
== RTM_NEWROUTE
) {
1067 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1068 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1069 &nexthop
->rmap_src
.ipv6
, bytelen
);
1070 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1071 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1072 &nexthop
->src
.ipv6
, bytelen
);
1075 if (IS_ZEBRA_DEBUG_KERNEL
)
1077 "netlink_route_multipath() (%s): "
1078 "nexthop via if %u(%u)",
1079 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1083 /* This function takes a nexthop as argument and
1084 * appends to the given rtattr/rtnexthop pair the
1085 * representation of the nexthop. If the nexthop
1086 * defines a preferred source, the src parameter
1087 * will be modified to point to that src, otherwise
1088 * it will be kept unmodified.
1090 * @param routedesc: Human readable description of route type
1091 * (direct/recursive, single-/multipath)
1092 * @param bytelen: Length of addresses in bytes.
1093 * @param nexthop: Nexthop information
1094 * @param rta: rtnetlink attribute structure
1095 * @param rtnh: pointer to an rtnetlink nexthop structure
1096 * @param src: pointer pointing to a location where
1097 * the prefsrc should be stored.
1099 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1100 struct nexthop
*nexthop
,
1102 struct rtnexthop
*rtnh
,
1103 struct rtmsg
*rtmsg
,
1106 struct mpls_label_stack
*nh_label
;
1107 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1109 char label_buf
[256];
1111 rtnh
->rtnh_len
= sizeof(*rtnh
);
1112 rtnh
->rtnh_flags
= 0;
1113 rtnh
->rtnh_hops
= 0;
1114 rta
->rta_len
+= rtnh
->rtnh_len
;
1117 * label_buf is *only* currently used within debugging.
1118 * As such when we assign it we are guarding it inside
1119 * a debug test. If you want to change this make sure
1120 * you fix this assumption
1122 label_buf
[0] = '\0';
1125 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1126 char label_buf1
[20];
1128 nh_label
= nh
->nh_label
;
1129 if (!nh_label
|| !nh_label
->num_labels
)
1132 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1133 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1136 if (IS_ZEBRA_DEBUG_KERNEL
) {
1138 sprintf(label_buf
, "label %u",
1139 nh_label
->label
[i
]);
1141 sprintf(label_buf1
, "/%u",
1142 nh_label
->label
[i
]);
1143 strlcat(label_buf
, label_buf1
,
1148 out_lse
[num_labels
] =
1149 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1155 /* Set the BoS bit */
1156 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1158 if (rtmsg
->rtm_family
== AF_MPLS
) {
1159 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1161 num_labels
* sizeof(mpls_lse_t
));
1163 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1165 struct rtattr
*nest
;
1166 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1167 int len
= rta
->rta_len
;
1169 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1170 &encap
, sizeof(uint16_t));
1171 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1172 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1174 num_labels
* sizeof(mpls_lse_t
));
1175 rta_nest_end(rta
, nest
);
1176 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1180 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1181 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1183 if (rtmsg
->rtm_family
== AF_INET
1184 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1185 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1187 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1188 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1190 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1191 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1193 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1194 *src
= &nexthop
->rmap_src
;
1195 else if (nexthop
->src
.ipv4
.s_addr
)
1196 *src
= &nexthop
->src
;
1198 if (IS_ZEBRA_DEBUG_KERNEL
)
1200 " 5549: netlink_route_build_multipath() (%s): "
1201 "nexthop via %s %s if %u",
1202 routedesc
, ipv4_ll_buf
, label_buf
,
1207 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1208 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1209 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1210 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1212 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1213 *src
= &nexthop
->rmap_src
;
1214 else if (nexthop
->src
.ipv4
.s_addr
)
1215 *src
= &nexthop
->src
;
1217 if (IS_ZEBRA_DEBUG_KERNEL
)
1219 "netlink_route_multipath() (%s): "
1220 "nexthop via %s %s if %u",
1221 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1222 label_buf
, nexthop
->ifindex
);
1224 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1225 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1226 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1227 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1230 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1231 *src
= &nexthop
->rmap_src
;
1232 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1233 *src
= &nexthop
->src
;
1235 if (IS_ZEBRA_DEBUG_KERNEL
)
1237 "netlink_route_multipath() (%s): "
1238 "nexthop via %s %s if %u",
1239 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1240 label_buf
, nexthop
->ifindex
);
1244 * We have figured out the ifindex so we should always send it
1245 * This is especially useful if we are doing route
1248 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1249 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1252 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1253 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1254 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1255 *src
= &nexthop
->rmap_src
;
1256 else if (nexthop
->src
.ipv4
.s_addr
)
1257 *src
= &nexthop
->src
;
1259 if (IS_ZEBRA_DEBUG_KERNEL
)
1261 "netlink_route_multipath() (%s): "
1262 "nexthop via if %u",
1263 routedesc
, nexthop
->ifindex
);
1267 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1268 zebra_nhlfe_t
*nhlfe
,
1269 struct nlmsghdr
*nlmsg
,
1270 struct rtmsg
*rtmsg
,
1271 size_t req_size
, int cmd
)
1276 family
= NHLFE_FAMILY(nhlfe
);
1277 bytelen
= (family
== AF_INET
? 4 : 16);
1278 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1279 nlmsg
, rtmsg
, req_size
, cmd
);
1284 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1285 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1286 struct rtmsg
*rtmsg
, union g_addr
**src
)
1291 family
= NHLFE_FAMILY(nhlfe
);
1292 bytelen
= (family
== AF_INET
? 4 : 16);
1293 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1298 /* Log debug information for netlink_route_multipath
1299 * if debug logging is enabled.
1301 * @param cmd: Netlink command which is to be processed
1302 * @param p: Prefix for which the change is due
1303 * @param family: Address family which the change concerns
1304 * @param zvrf: The vrf we are in
1305 * @param tableid: The table we are working on
1307 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1308 int family
, vrf_id_t vrfid
,
1311 if (IS_ZEBRA_DEBUG_KERNEL
) {
1312 char buf
[PREFIX_STRLEN
];
1314 "netlink_route_multipath(): %s %s vrf %u(%u)",
1315 nl_msg_type_to_str(cmd
),
1316 prefix2str(p
, buf
, sizeof(buf
)),
1321 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1323 if (IS_ZEBRA_DEBUG_KERNEL
)
1324 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1325 nl_msg_type_to_str(cmd
), label
);
1328 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1329 int llalen
, ns_id_t ns_id
)
1337 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1339 memset(&req
, 0, sizeof(req
));
1341 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1342 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1343 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1344 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1346 req
.ndm
.ndm_family
= AF_INET
;
1347 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1348 req
.ndm
.ndm_ifindex
= ifindex
;
1349 req
.ndm
.ndm_type
= RTN_UNICAST
;
1351 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1352 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1354 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1358 /* Routing table change via netlink interface. */
1359 /* Update flag indicates whether this is a "replace" or not. */
1360 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1361 const struct prefix
*src_p
,
1362 struct route_entry
*re
,
1366 struct sockaddr_nl snl
;
1367 struct nexthop
*nexthop
= NULL
;
1368 unsigned int nexthop_num
;
1369 int family
= PREFIX_FAMILY(p
);
1370 const char *routedesc
;
1377 char buf
[NL_PKT_BUF_SIZE
];
1380 struct zebra_ns
*zns
;
1381 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1384 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1386 bytelen
= (family
== AF_INET
? 4 : 16);
1388 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1389 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1390 if ((cmd
== RTM_NEWROUTE
) && update
)
1391 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1392 req
.n
.nlmsg_type
= cmd
;
1393 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1395 req
.r
.rtm_family
= family
;
1396 req
.r
.rtm_dst_len
= p
->prefixlen
;
1397 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1398 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1399 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1402 * blackhole routes are not RTN_UNICAST, they are
1403 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1404 * so setting this value as a RTN_UNICAST would
1405 * cause the route lookup of just the prefix
1406 * to fail. So no need to specify this for
1407 * the RTM_DELROUTE case
1409 if (cmd
!= RTM_DELROUTE
)
1410 req
.r
.rtm_type
= RTN_UNICAST
;
1412 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1414 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1418 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1420 * either by the kernel or by zebra. Its purely for calculating best
1422 * by the routing protocol and for communicating with protocol peers.
1424 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1425 #if defined(SUPPORT_REALMS)
1426 if (re
->tag
> 0 && re
->tag
<= 255)
1427 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1429 /* Table corresponding to this route. */
1430 if (re
->table
< 256)
1431 req
.r
.rtm_table
= re
->table
;
1433 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1434 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1437 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1440 * If we are not updating the route and we have received
1441 * a route delete, then all we need to fill in is the
1442 * prefix information to tell the kernel to schwack
1445 if (!update
&& cmd
== RTM_DELROUTE
)
1448 if (re
->mtu
|| re
->nexthop_mtu
) {
1449 char buf
[NL_PKT_BUF_SIZE
];
1450 struct rtattr
*rta
= (void *)buf
;
1451 uint32_t mtu
= re
->mtu
;
1452 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1453 mtu
= re
->nexthop_mtu
;
1454 rta
->rta_type
= RTA_METRICS
;
1455 rta
->rta_len
= RTA_LENGTH(0);
1456 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1457 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1461 /* Count overall nexthops so we can decide whether to use singlepath
1462 * or multipath case. */
1464 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1465 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1467 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1469 if (cmd
== RTM_DELROUTE
1470 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1476 /* Singlepath case. */
1477 if (nexthop_num
== 1 || multipath_num
== 1) {
1479 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1481 * So we want to cover 2 types of blackhole
1483 * 1) A normal blackhole route( ala from a static
1485 * 2) A recursively resolved blackhole route
1487 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1488 switch (nexthop
->bh_type
) {
1489 case BLACKHOLE_ADMINPROHIB
:
1490 req
.r
.rtm_type
= RTN_PROHIBIT
;
1492 case BLACKHOLE_REJECT
:
1493 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1496 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1501 if (CHECK_FLAG(nexthop
->flags
,
1502 NEXTHOP_FLAG_RECURSIVE
)) {
1504 if (family
== AF_INET
) {
1505 if (nexthop
->rmap_src
.ipv4
1512 } else if (nexthop
->src
.ipv4
1520 } else if (family
== AF_INET6
) {
1521 if (!IN6_IS_ADDR_UNSPECIFIED(
1529 !IN6_IS_ADDR_UNSPECIFIED(
1542 if ((cmd
== RTM_NEWROUTE
1543 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1544 || (cmd
== RTM_DELROUTE
1545 && CHECK_FLAG(nexthop
->flags
,
1546 NEXTHOP_FLAG_FIB
))) {
1547 routedesc
= nexthop
->rparent
1548 ? "recursive, single-path"
1551 _netlink_route_build_singlepath(
1552 routedesc
, bytelen
, nexthop
, &req
.n
,
1553 &req
.r
, sizeof req
, cmd
);
1558 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1559 if (family
== AF_INET
)
1560 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1561 &src
.ipv4
, bytelen
);
1562 else if (family
== AF_INET6
)
1563 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1564 &src
.ipv6
, bytelen
);
1567 char buf
[NL_PKT_BUF_SIZE
];
1568 struct rtattr
*rta
= (void *)buf
;
1569 struct rtnexthop
*rtnh
;
1570 union g_addr
*src1
= NULL
;
1572 rta
->rta_type
= RTA_MULTIPATH
;
1573 rta
->rta_len
= RTA_LENGTH(0);
1574 rtnh
= RTA_DATA(rta
);
1577 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1578 if (nexthop_num
>= multipath_num
)
1581 if (CHECK_FLAG(nexthop
->flags
,
1582 NEXTHOP_FLAG_RECURSIVE
)) {
1583 /* This only works for IPv4 now */
1585 if (family
== AF_INET
) {
1586 if (nexthop
->rmap_src
.ipv4
1593 } else if (nexthop
->src
.ipv4
1601 } else if (family
== AF_INET6
) {
1602 if (!IN6_IS_ADDR_UNSPECIFIED(
1610 !IN6_IS_ADDR_UNSPECIFIED(
1623 if ((cmd
== RTM_NEWROUTE
1624 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1625 || (cmd
== RTM_DELROUTE
1626 && CHECK_FLAG(nexthop
->flags
,
1627 NEXTHOP_FLAG_FIB
))) {
1628 routedesc
= nexthop
->rparent
1629 ? "recursive, multipath"
1633 _netlink_route_build_multipath(
1634 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1636 rtnh
= RTNH_NEXT(rtnh
);
1638 if (!setsrc
&& src1
) {
1639 if (family
== AF_INET
)
1640 src
.ipv4
= src1
->ipv4
;
1641 else if (family
== AF_INET6
)
1642 src
.ipv6
= src1
->ipv6
;
1648 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1649 if (family
== AF_INET
)
1650 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1651 &src
.ipv4
, bytelen
);
1652 else if (family
== AF_INET6
)
1653 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1654 &src
.ipv6
, bytelen
);
1655 if (IS_ZEBRA_DEBUG_KERNEL
)
1656 zlog_debug("Setting source");
1659 if (rta
->rta_len
> RTA_LENGTH(0))
1660 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1661 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1664 /* If there is no useful nexthop then return. */
1665 if (nexthop_num
== 0) {
1666 if (IS_ZEBRA_DEBUG_KERNEL
)
1668 "netlink_route_multipath(): No useful nexthop.");
1674 /* Destination netlink address. */
1675 memset(&snl
, 0, sizeof snl
);
1676 snl
.nl_family
= AF_NETLINK
;
1678 /* Talk to netlink socket. */
1679 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1683 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1686 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1694 struct zebra_ns
*zns
;
1697 memset(&req
, 0, sizeof(req
));
1699 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1700 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1701 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1703 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1704 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1706 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1707 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1708 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1709 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1710 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1712 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1713 &zns
->netlink_cmd
, zns
, 0);
1719 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1720 const struct prefix
*p
,
1721 const struct prefix
*src_p
,
1722 struct route_entry
*old
,
1723 struct route_entry
*new)
1730 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1731 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1732 new, (old
) ? 1 : 0);
1735 * So v6 route replace semantics are not in
1736 * the kernel at this point as I understand it.
1737 * So let's do a delete than an add.
1738 * In the future once v6 route replace semantics
1739 * are in we can figure out what to do here to
1740 * allow working with old and new kernels.
1742 * I'm also intentionally ignoring the failure case
1743 * of the route delete. If that happens yeah we're
1747 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1749 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1752 kernel_route_rib_pass_fail(rn
, p
, new,
1753 (!ret
) ? DP_INSTALL_SUCCESS
1754 : DP_INSTALL_FAILURE
);
1755 return DP_REQUEST_SUCCESS
;
1759 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1761 kernel_route_rib_pass_fail(rn
, p
, old
,
1762 (!ret
) ? DP_DELETE_SUCCESS
1763 : DP_DELETE_FAILURE
);
1766 return DP_REQUEST_SUCCESS
;
1769 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1770 int llalen
, ns_id_t ns_id
)
1772 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1773 addr
, lla
, llalen
, ns_id
);
1777 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1778 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1780 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1781 struct in_addr
*vtep_ip
, int cmd
)
1783 struct zebra_ns
*zns
;
1789 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1790 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1793 memset(&req
, 0, sizeof(req
));
1795 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1796 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1797 if (cmd
== RTM_NEWNEIGH
)
1798 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1799 req
.n
.nlmsg_type
= cmd
;
1800 req
.ndm
.ndm_family
= PF_BRIDGE
;
1801 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1802 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1805 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1806 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1807 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1809 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1814 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1816 * a "flood" MAC FDB entry.
1818 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1820 if (IS_ZEBRA_DEBUG_VXLAN
)
1821 zlog_debug("Install %s into 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_NEWNEIGH
);
1828 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1829 * deleting the "flood" MAC FDB entry.
1831 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1833 if (IS_ZEBRA_DEBUG_VXLAN
)
1835 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1836 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1838 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1842 #define NDA_RTA(r) \
1843 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1846 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1849 struct interface
*ifp
;
1850 struct zebra_if
*zif
;
1851 struct rtattr
*tb
[NDA_MAX
+ 1];
1852 struct interface
*br_if
;
1855 struct prefix vtep_ip
;
1856 int vid_present
= 0, dst_present
= 0;
1857 char buf
[ETHER_ADDR_STRLEN
];
1862 ndm
= NLMSG_DATA(h
);
1864 /* We only process macfdb notifications if EVPN is enabled */
1865 if (!is_evpn_enabled())
1868 /* The interface should exist. */
1869 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1871 if (!ifp
|| !ifp
->info
)
1874 /* The interface should be something we're interested in. */
1875 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1878 /* Drop "permanent" entries. */
1879 if (ndm
->ndm_state
& NUD_PERMANENT
)
1882 zif
= (struct zebra_if
*)ifp
->info
;
1883 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1884 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1885 nl_msg_type_to_str(h
->nlmsg_type
),
1886 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1887 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1891 /* Parse attributes and extract fields of interest. */
1892 memset(tb
, 0, sizeof tb
);
1893 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1895 if (!tb
[NDA_LLADDR
]) {
1896 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1897 nl_msg_type_to_str(h
->nlmsg_type
),
1898 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1899 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1903 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1905 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1906 nl_msg_type_to_str(h
->nlmsg_type
),
1907 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1908 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1909 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1913 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1915 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1917 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1918 sprintf(vid_buf
, " VLAN %u", vid
);
1922 /* TODO: Only IPv4 supported now. */
1924 vtep_ip
.family
= AF_INET
;
1925 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1926 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1928 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1931 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1933 if (IS_ZEBRA_DEBUG_KERNEL
)
1934 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1935 nl_msg_type_to_str(h
->nlmsg_type
),
1936 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1937 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1938 sticky
? "sticky " : "",
1939 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1940 dst_present
? dst_buf
: "");
1942 if (filter_vlan
&& vid
!= filter_vlan
)
1945 /* If add or update, do accordingly if learnt on a "local" interface; if
1946 * the notification is over VxLAN, this has to be related to
1948 * so perform an implicit delete of any local entry (if it exists).
1950 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1951 /* Drop "permanent" entries. */
1952 if (ndm
->ndm_state
& NUD_PERMANENT
)
1955 if (IS_ZEBRA_IF_VXLAN(ifp
))
1956 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1959 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1963 /* This is a delete notification.
1964 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1965 * 2. For a MAC over "local" interface, delete the mac
1966 * Note: We will get notifications from both bridge driver and VxLAN
1968 * Ignore the notification from VxLan driver as it is also generated
1969 * when mac moves from remote to local.
1974 if (IS_ZEBRA_IF_VXLAN(ifp
))
1975 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1978 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1981 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1986 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1989 /* Length validity. */
1990 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1994 /* We are interested only in AF_BRIDGE notifications. */
1995 ndm
= NLMSG_DATA(h
);
1996 if (ndm
->ndm_family
!= AF_BRIDGE
)
1999 return netlink_macfdb_change(h
, len
, ns_id
);
2002 /* Request for MAC FDB information from the kernel */
2003 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2004 ifindex_t master_ifindex
)
2008 struct ifinfomsg ifm
;
2012 /* Form the request, specifying filter (rtattr) if needed. */
2013 memset(&req
, 0, sizeof(req
));
2014 req
.n
.nlmsg_type
= type
;
2015 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2016 req
.ifm
.ifi_family
= family
;
2018 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2020 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2024 * MAC forwarding database read using netlink interface. This is invoked
2027 int netlink_macfdb_read(struct zebra_ns
*zns
)
2031 /* Get bridge FDB table. */
2032 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2035 /* We are reading entire table. */
2037 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2044 * MAC forwarding database read using netlink interface. This is for a
2045 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2047 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2048 struct interface
*br_if
)
2050 struct zebra_if
*br_zif
;
2051 struct zebra_if
*zif
;
2052 struct zebra_l2info_vxlan
*vxl
;
2056 /* Save VLAN we're filtering on, if needed. */
2057 br_zif
= (struct zebra_if
*)br_if
->info
;
2058 zif
= (struct zebra_if
*)ifp
->info
;
2059 vxl
= &zif
->l2info
.vxl
;
2060 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2061 filter_vlan
= vxl
->access_vlan
;
2063 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2065 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2069 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2072 /* Reset VLAN filter. */
2077 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2078 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2079 int local
, int cmd
, uint8_t sticky
)
2081 struct zebra_ns
*zns
;
2088 struct zebra_if
*zif
;
2089 struct interface
*br_if
;
2090 struct zebra_if
*br_zif
;
2091 char buf
[ETHER_ADDR_STRLEN
];
2092 int vid_present
= 0, dst_present
= 0;
2095 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2099 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2100 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2101 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2106 memset(&req
, 0, sizeof(req
));
2108 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2109 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2110 if (cmd
== RTM_NEWNEIGH
)
2111 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2112 req
.n
.nlmsg_type
= cmd
;
2113 req
.ndm
.ndm_family
= AF_BRIDGE
;
2114 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2115 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2118 req
.ndm
.ndm_state
|= NUD_NOARP
;
2120 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2122 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2123 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2125 dst_alen
= 4; // TODO: hardcoded
2126 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2128 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2130 br_zif
= (struct zebra_if
*)br_if
->info
;
2131 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2132 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2134 sprintf(vid_buf
, " VLAN %u", vid
);
2136 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2138 if (IS_ZEBRA_DEBUG_KERNEL
)
2139 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2140 nl_msg_type_to_str(cmd
),
2141 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2142 ifp
->ifindex
, vid_present
? vid_buf
: "",
2143 sticky
? "sticky " : "",
2144 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2145 dst_present
? dst_buf
: "");
2147 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2152 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2155 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2158 struct interface
*ifp
;
2159 struct zebra_if
*zif
;
2160 struct rtattr
*tb
[NDA_MAX
+ 1];
2161 struct interface
*link_if
;
2164 char buf
[ETHER_ADDR_STRLEN
];
2165 char buf2
[INET6_ADDRSTRLEN
];
2166 int mac_present
= 0;
2167 uint8_t ext_learned
;
2168 uint8_t router_flag
;
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;
2259 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2261 if (IS_ZEBRA_DEBUG_KERNEL
)
2263 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2264 nl_msg_type_to_str(h
->nlmsg_type
),
2265 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2267 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2269 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2271 ndm
->ndm_state
, ndm
->ndm_flags
);
2273 /* If the neighbor state is valid for use, process as an add or
2275 * else process as a delete. Note that the delete handling may
2277 * in re-adding the neighbor if it is a valid "remote" neighbor.
2279 if (ndm
->ndm_state
& NUD_VALID
)
2280 return zebra_vxlan_handle_kernel_neigh_update(
2281 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2282 ext_learned
, router_flag
);
2284 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2287 if (IS_ZEBRA_DEBUG_KERNEL
)
2288 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2289 nl_msg_type_to_str(h
->nlmsg_type
),
2290 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2292 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2294 /* Process the delete - it may result in re-adding the neighbor if it is
2295 * a valid "remote" neighbor.
2297 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2300 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2305 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2308 /* Length validity. */
2309 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2313 /* We are interested only in AF_INET or AF_INET6 notifications. */
2314 ndm
= NLMSG_DATA(h
);
2315 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2318 return netlink_neigh_change(h
, len
);
2321 /* Request for IP neighbor information from the kernel */
2322 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2331 /* Form the request, specifying filter (rtattr) if needed. */
2332 memset(&req
, 0, sizeof(req
));
2333 req
.n
.nlmsg_type
= type
;
2334 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2335 req
.ndm
.ndm_family
= family
;
2337 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2339 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2343 * IP Neighbor table read using netlink interface. This is invoked
2346 int netlink_neigh_read(struct zebra_ns
*zns
)
2350 /* Get IP neighbor table. */
2351 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2354 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2361 * IP Neighbor table read using netlink interface. This is for a specific
2364 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2368 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2372 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2378 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2383 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2386 /* Length validity. */
2387 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2389 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2390 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2391 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2395 /* Is this a notification for the MAC FDB or IP neighbor table? */
2396 ndm
= NLMSG_DATA(h
);
2397 if (ndm
->ndm_family
== AF_BRIDGE
)
2398 return netlink_macfdb_change(h
, len
, ns_id
);
2400 if (ndm
->ndm_type
!= RTN_UNICAST
)
2403 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2404 return netlink_ipneigh_change(h
, len
, ns_id
);
2407 "Invalid address family: %d received from kernel neighbor change: %d",
2408 ndm
->ndm_family
, h
->nlmsg_type
);
2415 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2416 struct ethaddr
*mac
, uint8_t flags
,
2417 uint16_t state
, int cmd
)
2426 struct zebra_ns
*zns
;
2427 char buf
[INET6_ADDRSTRLEN
];
2428 char buf2
[ETHER_ADDR_STRLEN
];
2429 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2432 memset(&req
, 0, sizeof(req
));
2434 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2435 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2436 if (cmd
== RTM_NEWNEIGH
)
2437 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2438 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2439 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2440 req
.ndm
.ndm_state
= state
;
2441 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2442 req
.ndm
.ndm_type
= RTN_UNICAST
;
2443 req
.ndm
.ndm_flags
= flags
;
2445 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2446 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2448 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2450 if (IS_ZEBRA_DEBUG_KERNEL
)
2451 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2452 nl_msg_type_to_str(cmd
),
2453 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2454 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2455 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2458 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2462 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2463 struct in_addr vtep_ip
, uint8_t sticky
)
2465 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2469 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2470 struct in_addr vtep_ip
, int local
)
2472 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2476 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2477 struct ethaddr
*mac
, uint8_t flags
)
2479 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2480 NUD_NOARP
, RTM_NEWNEIGH
);
2483 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2485 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2489 * MPLS label forwarding table change via netlink interface.
2491 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2494 zebra_nhlfe_t
*nhlfe
;
2495 struct nexthop
*nexthop
= NULL
;
2496 unsigned int nexthop_num
;
2497 const char *routedesc
;
2498 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2504 char buf
[NL_PKT_BUF_SIZE
];
2507 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2510 * Count # nexthops so we can decide whether to use singlepath
2511 * or multipath case.
2514 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2515 nexthop
= nhlfe
->nexthop
;
2518 if (cmd
== RTM_NEWROUTE
) {
2519 /* Count all selected NHLFEs */
2520 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2521 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2525 /* Count all installed NHLFEs */
2526 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2527 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2532 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2535 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2536 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2537 req
.n
.nlmsg_type
= cmd
;
2538 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2540 req
.r
.rtm_family
= AF_MPLS
;
2541 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2542 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2543 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2544 req
.r
.rtm_type
= RTN_UNICAST
;
2546 if (cmd
== RTM_NEWROUTE
) {
2547 /* We do a replace to handle update. */
2548 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2550 /* set the protocol value if installing */
2551 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2552 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2555 /* Fill destination */
2556 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2557 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2559 /* Fill nexthops (paths) based on single-path or multipath. The paths
2560 * chosen depend on the operation.
2562 if (nexthop_num
== 1 || multipath_num
== 1) {
2563 routedesc
= "single-path";
2564 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2567 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2568 nexthop
= nhlfe
->nexthop
;
2572 if ((cmd
== RTM_NEWROUTE
2573 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2574 && CHECK_FLAG(nexthop
->flags
,
2575 NEXTHOP_FLAG_ACTIVE
)))
2576 || (cmd
== RTM_DELROUTE
2577 && (CHECK_FLAG(nhlfe
->flags
,
2578 NHLFE_FLAG_INSTALLED
)
2579 && CHECK_FLAG(nexthop
->flags
,
2580 NEXTHOP_FLAG_FIB
)))) {
2581 /* Add the gateway */
2582 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2589 } else /* Multipath case */
2591 char buf
[NL_PKT_BUF_SIZE
];
2592 struct rtattr
*rta
= (void *)buf
;
2593 struct rtnexthop
*rtnh
;
2594 union g_addr
*src1
= NULL
;
2596 rta
->rta_type
= RTA_MULTIPATH
;
2597 rta
->rta_len
= RTA_LENGTH(0);
2598 rtnh
= RTA_DATA(rta
);
2600 routedesc
= "multipath";
2601 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2604 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2605 nexthop
= nhlfe
->nexthop
;
2609 if (nexthop_num
>= multipath_num
)
2612 if ((cmd
== RTM_NEWROUTE
2613 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2614 && CHECK_FLAG(nexthop
->flags
,
2615 NEXTHOP_FLAG_ACTIVE
)))
2616 || (cmd
== RTM_DELROUTE
2617 && (CHECK_FLAG(nhlfe
->flags
,
2618 NHLFE_FLAG_INSTALLED
)
2619 && CHECK_FLAG(nexthop
->flags
,
2620 NEXTHOP_FLAG_FIB
)))) {
2623 /* Build the multipath */
2624 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2627 rtnh
= RTNH_NEXT(rtnh
);
2631 /* Add the multipath */
2632 if (rta
->rta_len
> RTA_LENGTH(0))
2633 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2634 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2637 /* Talk to netlink socket. */
2638 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2641 #endif /* HAVE_NETLINK */