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
) || (proto
== RTPROT_OPENFABRIC
)) {
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
:
149 case ZEBRA_ROUTE_OPENFABRIC
:
150 proto
= RTPROT_OPENFABRIC
;
154 * When a user adds a new protocol this will show up
155 * to let them know to do something about it. This
156 * is intentionally a warn because we should see
157 * this as part of development of a new protocol
159 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
160 __PRETTY_FUNCTION__
, proto
);
161 proto
= RTPROT_ZEBRA
;
168 static inline int proto2zebra(int proto
, int family
)
172 proto
= ZEBRA_ROUTE_BABEL
;
175 proto
= ZEBRA_ROUTE_BGP
;
178 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
182 proto
= ZEBRA_ROUTE_ISIS
;
185 proto
= ZEBRA_ROUTE_RIP
;
188 proto
= ZEBRA_ROUTE_RIPNG
;
191 proto
= ZEBRA_ROUTE_NHRP
;
194 proto
= ZEBRA_ROUTE_EIGRP
;
197 proto
= ZEBRA_ROUTE_LDP
;
201 proto
= ZEBRA_ROUTE_STATIC
;
204 proto
= ZEBRA_ROUTE_SHARP
;
207 proto
= ZEBRA_ROUTE_PBR
;
209 case RTPROT_OPENFABRIC
:
210 proto
= ZEBRA_ROUTE_OPENFABRIC
;
214 * When a user adds a new protocol this will show up
215 * to let them know to do something about it. This
216 * is intentionally a warn because we should see
217 * this as part of development of a new protocol
219 zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling",
222 proto
= ZEBRA_ROUTE_KERNEL
;
229 Pending: create an efficient table_id (in a tree/hash) based lookup)
231 static vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
234 struct zebra_vrf
*zvrf
;
236 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
240 /* case vrf with netns : match the netnsid */
241 if (vrf_is_backend_netns()) {
242 if (ns_id
== zvrf_id(zvrf
))
243 return zvrf_id(zvrf
);
245 /* VRF is VRF_BACKEND_VRF_LITE */
246 if (zvrf
->table_id
!= table_id
)
248 return zvrf_id(zvrf
);
256 * @parse_encap_mpls() - Parses encapsulated mpls attributes
257 * @tb: Pointer to rtattr to look for nested items in.
258 * @labels: Pointer to store labels in.
260 * Return: Number of mpls labels found.
262 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
264 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
265 mpls_lse_t
*lses
= NULL
;
270 mpls_label_t label
= 0;
272 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
273 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
274 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
275 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
276 labels
[num_labels
++] = label
;
282 /* Looking up routing table by netlink interface. */
283 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
288 struct rtattr
*tb
[RTA_MAX
+ 1];
291 struct prefix_ipv6 src_p
= {};
294 char anyaddr
[16] = {0};
296 int proto
= ZEBRA_ROUTE_KERNEL
;
301 uint8_t distance
= 0;
306 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
307 void *src
= NULL
; /* IPv6 srcdest source prefix */
308 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
311 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
316 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
318 switch (rtm
->rtm_type
) {
322 bh_type
= BLACKHOLE_NULL
;
324 case RTN_UNREACHABLE
:
325 bh_type
= BLACKHOLE_REJECT
;
328 bh_type
= BLACKHOLE_ADMINPROHIB
;
334 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
336 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
337 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
338 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
342 memset(tb
, 0, sizeof tb
);
343 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
345 if (rtm
->rtm_flags
& RTM_F_CLONED
)
347 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
349 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
352 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
353 && h
->nlmsg_type
== RTM_NEWROUTE
) {
354 if (IS_ZEBRA_DEBUG_KERNEL
)
355 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
360 /* We don't care about change notifications for the MPLS table. */
361 /* TODO: Revisit this. */
362 if (rtm
->rtm_family
== AF_MPLS
)
365 /* Table corresponding to route. */
367 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
369 table
= rtm
->rtm_table
;
372 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
373 if (vrf_id
== VRF_DEFAULT
) {
374 if (!is_zebra_valid_kernel_table(table
)
375 && !is_zebra_main_routing_table(table
))
379 /* Route which inserted by Zebra. */
380 if (is_selfroute(rtm
->rtm_protocol
)) {
381 flags
|= ZEBRA_FLAG_SELFROUTE
;
382 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
385 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
388 dest
= RTA_DATA(tb
[RTA_DST
]);
393 src
= RTA_DATA(tb
[RTA_SRC
]);
398 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
401 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
403 if (tb
[RTA_PRIORITY
])
404 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
406 #if defined(SUPPORT_REALMS)
408 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
411 if (tb
[RTA_METRICS
]) {
412 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
414 memset(mxrta
, 0, sizeof mxrta
);
415 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
416 RTA_PAYLOAD(tb
[RTA_METRICS
]));
419 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
422 if (rtm
->rtm_family
== AF_INET
) {
424 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
426 "Invalid destination prefix length: %u received from kernel route change",
430 memcpy(&p
.u
.prefix4
, dest
, 4);
431 p
.prefixlen
= rtm
->rtm_dst_len
;
433 if (rtm
->rtm_src_len
!= 0) {
434 char buf
[PREFIX_STRLEN
];
435 zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)",
436 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
440 /* Force debug below to not display anything for source */
442 } else if (rtm
->rtm_family
== AF_INET6
) {
444 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
446 "Invalid destination prefix length: %u received from kernel route change",
450 memcpy(&p
.u
.prefix6
, dest
, 16);
451 p
.prefixlen
= rtm
->rtm_dst_len
;
453 src_p
.family
= AF_INET6
;
454 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
456 "Invalid source prefix length: %u received from kernel route change",
460 memcpy(&src_p
.prefix
, src
, 16);
461 src_p
.prefixlen
= rtm
->rtm_src_len
;
465 * For ZEBRA_ROUTE_KERNEL types:
467 * The metric/priority of the route received from the kernel
468 * is a 32 bit number. We are going to interpret the high
469 * order byte as the Admin Distance and the low order 3 bytes
472 * This will allow us to do two things:
473 * 1) Allow the creation of kernel routes that can be
474 * overridden by zebra.
475 * 2) Allow the old behavior for 'most' kernel route types
476 * if a user enters 'ip route ...' v4 routes get a metric
477 * of 0 and v6 routes get a metric of 1024. Both of these
478 * values will end up with a admin distance of 0, which
479 * will cause them to win for the purposes of zebra.
481 if (proto
== ZEBRA_ROUTE_KERNEL
) {
482 distance
= (metric
>> 24) & 0xFF;
483 metric
= (metric
& 0x00FFFFFF);
486 if (IS_ZEBRA_DEBUG_KERNEL
) {
487 char buf
[PREFIX_STRLEN
];
488 char buf2
[PREFIX_STRLEN
];
489 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
490 nl_msg_type_to_str(h
->nlmsg_type
),
491 prefix2str(&p
, buf
, sizeof(buf
)),
492 src_p
.prefixlen
? " from " : "",
494 ? prefix2str(&src_p
, buf2
, sizeof(buf2
))
496 vrf_id
, table
, metric
, distance
);
500 if (rtm
->rtm_family
== AF_INET6
)
503 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
504 struct interface
*ifp
;
505 vrf_id_t nh_vrf_id
= vrf_id
;
507 if (!tb
[RTA_MULTIPATH
]) {
509 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
511 memset(&nh
, 0, sizeof(nh
));
513 if (bh_type
== BLACKHOLE_UNSPEC
) {
515 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
516 else if (index
&& gate
)
519 ? NEXTHOP_TYPE_IPV4_IFINDEX
520 : NEXTHOP_TYPE_IPV6_IFINDEX
;
521 else if (!index
&& gate
)
522 nh
.type
= (afi
== AFI_IP
)
526 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
527 nh
.bh_type
= bh_type
;
530 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
531 nh
.bh_type
= bh_type
;
535 memcpy(&nh
.src
, prefsrc
, sz
);
537 memcpy(&nh
.gate
, gate
, sz
);
540 ifp
= if_lookup_by_index_per_ns(
541 zebra_ns_lookup(ns_id
),
544 nh_vrf_id
= ifp
->vrf_id
;
546 nh
.vrf_id
= nh_vrf_id
;
548 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
549 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
550 == LWTUNNEL_ENCAP_MPLS
) {
552 parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
556 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
,
559 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
560 &src_p
, &nh
, table
, metric
, mtu
, distance
, tag
);
562 /* This is a multipath route */
564 struct route_entry
*re
;
565 struct rtnexthop
*rtnh
=
566 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
568 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
570 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
572 re
->distance
= distance
;
579 re
->uptime
= time(NULL
);
583 struct nexthop
*nh
= NULL
;
585 if (len
< (int)sizeof(*rtnh
)
586 || rtnh
->rtnh_len
> len
)
589 index
= rtnh
->rtnh_ifindex
;
592 * Yes we are looking this up
593 * for every nexthop and just
594 * using the last one looked
597 ifp
= if_lookup_by_index_per_ns(
598 zebra_ns_lookup(ns_id
),
601 nh_vrf_id
= ifp
->vrf_id
;
604 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
607 nh_vrf_id
= VRF_DEFAULT
;
613 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
614 memset(tb
, 0, sizeof(tb
));
615 netlink_parse_rtattr(
616 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
617 rtnh
->rtnh_len
- sizeof(*rtnh
));
621 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
622 && *(uint16_t *)RTA_DATA(
624 == LWTUNNEL_ENCAP_MPLS
) {
625 num_labels
= parse_encap_mpls(
626 tb
[RTA_ENCAP
], labels
);
631 if (rtm
->rtm_family
== AF_INET
) {
633 nh
= route_entry_nexthop_ipv4_ifindex_add(
638 nh
= route_entry_nexthop_ipv4_add(
642 } else if (rtm
->rtm_family
645 nh
= route_entry_nexthop_ipv6_ifindex_add(
649 nh
= route_entry_nexthop_ipv6_add(
654 nh
= route_entry_nexthop_ifindex_add(
655 re
, index
, nh_vrf_id
);
657 if (nh
&& num_labels
)
658 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
661 if (rtnh
->rtnh_len
== 0)
664 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
665 rtnh
= RTNH_NEXT(rtnh
);
668 zserv_nexthop_num_warn(__func__
,
669 (const struct prefix
*)&p
,
671 if (re
->nexthop_num
== 0)
674 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
678 if (!tb
[RTA_MULTIPATH
]) {
680 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
682 memset(&nh
, 0, sizeof(nh
));
683 if (bh_type
== BLACKHOLE_UNSPEC
) {
685 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
686 else if (index
&& gate
)
689 ? NEXTHOP_TYPE_IPV4_IFINDEX
690 : NEXTHOP_TYPE_IPV6_IFINDEX
;
691 else if (!index
&& gate
)
692 nh
.type
= (afi
== AFI_IP
)
696 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
697 nh
.bh_type
= BLACKHOLE_UNSPEC
;
700 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
701 nh
.bh_type
= bh_type
;
705 memcpy(&nh
.gate
, gate
, sz
);
706 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
707 &p
, &src_p
, &nh
, table
, metric
, distance
,
710 /* XXX: need to compare the entire list of nexthops
711 * here for NLM_F_APPEND stupidity */
712 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
713 &p
, &src_p
, NULL
, table
, metric
, distance
,
721 static struct mcast_route_data
*mroute
= NULL
;
723 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
724 ns_id_t ns_id
, int startup
)
728 struct rtattr
*tb
[RTA_MAX
+ 1];
729 struct mcast_route_data
*m
;
730 struct mcast_route_data mr
;
737 char oif_list
[256] = "\0";
744 memset(&mr
, 0, sizeof(mr
));
750 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
752 memset(tb
, 0, sizeof tb
);
753 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
756 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
758 table
= rtm
->rtm_table
;
760 vrf
= vrf_lookup_by_table(table
, ns_id
);
763 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
766 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
769 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
771 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
772 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
774 if (tb
[RTA_MULTIPATH
]) {
775 struct rtnexthop
*rtnh
=
776 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
778 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
780 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
783 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
786 if (rtnh
->rtnh_len
== 0)
789 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
790 rtnh
= RTNH_NEXT(rtnh
);
794 if (IS_ZEBRA_DEBUG_KERNEL
) {
795 struct interface
*ifp
;
796 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
797 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
798 for (count
= 0; count
< oif_count
; count
++) {
799 ifp
= if_lookup_by_index(oif
[count
], vrf
);
802 sprintf(temp
, "%s(%d) ", ifp
? ifp
->name
: "Unknown",
804 strcat(oif_list
, temp
);
806 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
807 ifp
= if_lookup_by_index(iif
, vrf
);
808 zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
809 zvrf
->vrf
->name
, vrf
,
810 nl_msg_type_to_str(h
->nlmsg_type
),
811 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
,
812 oif_list
, m
->lastused
);
817 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
824 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
825 /* If this is not route add/delete message print warning. */
826 zlog_warn("Kernel message: %d NS %u\n", h
->nlmsg_type
, ns_id
);
830 if (!(rtm
->rtm_family
== AF_INET
|| rtm
->rtm_family
== AF_INET6
)) {
832 "Invalid address family: %u received from kernel route change: %u",
833 rtm
->rtm_family
, h
->nlmsg_type
);
837 /* Connected route. */
838 if (IS_ZEBRA_DEBUG_KERNEL
)
839 zlog_debug("%s %s %s proto %s NS %u",
840 nl_msg_type_to_str(h
->nlmsg_type
),
841 nl_family_to_str(rtm
->rtm_family
),
842 nl_rttype_to_str(rtm
->rtm_type
),
843 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
846 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
848 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
851 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
855 if (rtm
->rtm_type
== RTN_MULTICAST
)
856 netlink_route_change_read_multicast(h
, ns_id
, startup
);
858 netlink_route_change_read_unicast(h
, ns_id
, startup
);
862 /* Request for specific route information from the kernel */
863 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
870 /* Form the request, specifying filter (rtattr) if needed. */
871 memset(&req
, 0, sizeof(req
));
872 req
.n
.nlmsg_type
= type
;
873 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
874 req
.rtm
.rtm_family
= family
;
876 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
879 /* Routing table read function using netlink interface. Only called
881 int netlink_route_read(struct zebra_ns
*zns
)
885 /* Get IPv4 routing table. */
886 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
889 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
890 &zns
->netlink_cmd
, zns
, 0, 1);
894 /* Get IPv6 routing table. */
895 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
898 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
899 &zns
->netlink_cmd
, zns
, 0, 1);
906 static void _netlink_route_nl_add_gateway_info(uint8_t route_family
,
908 struct nlmsghdr
*nlmsg
,
909 size_t req_size
, int bytelen
,
910 struct nexthop
*nexthop
)
912 if (route_family
== AF_MPLS
) {
913 struct gw_family_t gw_fam
;
915 gw_fam
.family
= gw_family
;
916 if (gw_family
== AF_INET
)
917 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
919 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
920 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
923 if (gw_family
== AF_INET
)
924 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
925 &nexthop
->gate
.ipv4
, bytelen
);
927 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
928 &nexthop
->gate
.ipv6
, bytelen
);
932 static void _netlink_route_rta_add_gateway_info(uint8_t route_family
,
935 struct rtnexthop
*rtnh
,
936 size_t req_size
, int bytelen
,
937 struct nexthop
*nexthop
)
939 if (route_family
== AF_MPLS
) {
940 struct gw_family_t gw_fam
;
942 gw_fam
.family
= gw_family
;
943 if (gw_family
== AF_INET
)
944 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
946 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
947 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
949 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
951 if (gw_family
== AF_INET
)
952 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
953 &nexthop
->gate
.ipv4
, bytelen
);
955 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
956 &nexthop
->gate
.ipv6
, bytelen
);
957 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
961 /* This function takes a nexthop as argument and adds
962 * the appropriate netlink attributes to an existing
965 * @param routedesc: Human readable description of route type
966 * (direct/recursive, single-/multipath)
967 * @param bytelen: Length of addresses in bytes.
968 * @param nexthop: Nexthop information
969 * @param nlmsg: nlmsghdr structure to fill in.
970 * @param req_size: The size allocated for the message.
972 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
973 struct nexthop
*nexthop
,
974 struct nlmsghdr
*nlmsg
,
976 size_t req_size
, int cmd
)
978 struct mpls_label_stack
*nh_label
;
979 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
984 * label_buf is *only* currently used within debugging.
985 * As such when we assign it we are guarding it inside
986 * a debug test. If you want to change this make sure
987 * you fix this assumption
992 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
995 nh_label
= nh
->nh_label
;
996 if (!nh_label
|| !nh_label
->num_labels
)
999 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1000 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1003 if (IS_ZEBRA_DEBUG_KERNEL
) {
1005 sprintf(label_buf
, "label %u",
1006 nh_label
->label
[i
]);
1008 sprintf(label_buf1
, "/%u",
1009 nh_label
->label
[i
]);
1010 strlcat(label_buf
, label_buf1
,
1015 out_lse
[num_labels
] =
1016 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1022 /* Set the BoS bit */
1023 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1025 if (rtmsg
->rtm_family
== AF_MPLS
)
1026 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
1027 num_labels
* sizeof(mpls_lse_t
));
1029 struct rtattr
*nest
;
1030 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1032 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
1034 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1035 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
1036 num_labels
* sizeof(mpls_lse_t
));
1037 addattr_nest_end(nlmsg
, nest
);
1041 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1042 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1044 if (rtmsg
->rtm_family
== AF_INET
1045 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1046 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1047 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1048 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1049 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1051 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1052 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1053 &nexthop
->rmap_src
.ipv4
, bytelen
);
1054 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1055 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1056 &nexthop
->src
.ipv4
, bytelen
);
1058 if (IS_ZEBRA_DEBUG_KERNEL
)
1060 " 5549: _netlink_route_build_singlepath() (%s): "
1061 "nexthop via %s %s if %u(%u)",
1062 routedesc
, ipv4_ll_buf
, label_buf
,
1063 nexthop
->ifindex
, nexthop
->vrf_id
);
1067 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1068 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1069 /* Send deletes to the kernel without specifying the next-hop */
1070 if (cmd
!= RTM_DELROUTE
)
1071 _netlink_route_nl_add_gateway_info(
1072 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1075 if (cmd
== RTM_NEWROUTE
) {
1076 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1077 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1078 &nexthop
->rmap_src
.ipv4
, bytelen
);
1079 else if (nexthop
->src
.ipv4
.s_addr
)
1080 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1081 &nexthop
->src
.ipv4
, bytelen
);
1084 if (IS_ZEBRA_DEBUG_KERNEL
)
1086 "netlink_route_multipath() (%s): "
1087 "nexthop via %s %s if %u(%u)",
1088 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1089 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1092 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1093 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1094 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1095 nlmsg
, req_size
, bytelen
,
1098 if (cmd
== RTM_NEWROUTE
) {
1099 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1100 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1101 &nexthop
->rmap_src
.ipv6
, bytelen
);
1102 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1103 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1104 &nexthop
->src
.ipv6
, bytelen
);
1107 if (IS_ZEBRA_DEBUG_KERNEL
)
1109 "netlink_route_multipath() (%s): "
1110 "nexthop via %s %s if %u(%u)",
1111 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1112 label_buf
, nexthop
->ifindex
, nexthop
->vrf_id
);
1116 * We have the ifindex so we should always send it
1117 * This is especially useful if we are doing route
1120 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1121 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1123 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1124 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1125 if (cmd
== RTM_NEWROUTE
) {
1126 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1127 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1128 &nexthop
->rmap_src
.ipv4
, bytelen
);
1129 else if (nexthop
->src
.ipv4
.s_addr
)
1130 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1131 &nexthop
->src
.ipv4
, bytelen
);
1134 if (IS_ZEBRA_DEBUG_KERNEL
)
1136 "netlink_route_multipath() (%s): "
1137 "nexthop via if %u(%u)",
1138 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1141 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1142 if (cmd
== RTM_NEWROUTE
) {
1143 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1144 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1145 &nexthop
->rmap_src
.ipv6
, bytelen
);
1146 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1147 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1148 &nexthop
->src
.ipv6
, bytelen
);
1151 if (IS_ZEBRA_DEBUG_KERNEL
)
1153 "netlink_route_multipath() (%s): "
1154 "nexthop via if %u(%u)",
1155 routedesc
, nexthop
->ifindex
, nexthop
->vrf_id
);
1159 /* This function takes a nexthop as argument and
1160 * appends to the given rtattr/rtnexthop pair the
1161 * representation of the nexthop. If the nexthop
1162 * defines a preferred source, the src parameter
1163 * will be modified to point to that src, otherwise
1164 * it will be kept unmodified.
1166 * @param routedesc: Human readable description of route type
1167 * (direct/recursive, single-/multipath)
1168 * @param bytelen: Length of addresses in bytes.
1169 * @param nexthop: Nexthop information
1170 * @param rta: rtnetlink attribute structure
1171 * @param rtnh: pointer to an rtnetlink nexthop structure
1172 * @param src: pointer pointing to a location where
1173 * the prefsrc should be stored.
1175 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1176 struct nexthop
*nexthop
,
1178 struct rtnexthop
*rtnh
,
1179 struct rtmsg
*rtmsg
,
1182 struct mpls_label_stack
*nh_label
;
1183 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1185 char label_buf
[256];
1187 rtnh
->rtnh_len
= sizeof(*rtnh
);
1188 rtnh
->rtnh_flags
= 0;
1189 rtnh
->rtnh_hops
= 0;
1190 rta
->rta_len
+= rtnh
->rtnh_len
;
1193 * label_buf is *only* currently used within debugging.
1194 * As such when we assign it we are guarding it inside
1195 * a debug test. If you want to change this make sure
1196 * you fix this assumption
1198 label_buf
[0] = '\0';
1201 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1202 char label_buf1
[20];
1204 nh_label
= nh
->nh_label
;
1205 if (!nh_label
|| !nh_label
->num_labels
)
1208 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1209 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1212 if (IS_ZEBRA_DEBUG_KERNEL
) {
1214 sprintf(label_buf
, "label %u",
1215 nh_label
->label
[i
]);
1217 sprintf(label_buf1
, "/%u",
1218 nh_label
->label
[i
]);
1219 strlcat(label_buf
, label_buf1
,
1224 out_lse
[num_labels
] =
1225 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1231 /* Set the BoS bit */
1232 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1234 if (rtmsg
->rtm_family
== AF_MPLS
) {
1235 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1237 num_labels
* sizeof(mpls_lse_t
));
1239 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1241 struct rtattr
*nest
;
1242 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1243 int len
= rta
->rta_len
;
1245 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1246 &encap
, sizeof(uint16_t));
1247 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1248 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1250 num_labels
* sizeof(mpls_lse_t
));
1251 rta_nest_end(rta
, nest
);
1252 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1256 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1257 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1259 if (rtmsg
->rtm_family
== AF_INET
1260 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1261 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1263 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1264 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1266 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1267 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1269 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1270 *src
= &nexthop
->rmap_src
;
1271 else if (nexthop
->src
.ipv4
.s_addr
)
1272 *src
= &nexthop
->src
;
1274 if (IS_ZEBRA_DEBUG_KERNEL
)
1276 " 5549: netlink_route_build_multipath() (%s): "
1277 "nexthop via %s %s if %u",
1278 routedesc
, ipv4_ll_buf
, label_buf
,
1283 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1284 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1285 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1286 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1288 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1289 *src
= &nexthop
->rmap_src
;
1290 else if (nexthop
->src
.ipv4
.s_addr
)
1291 *src
= &nexthop
->src
;
1293 if (IS_ZEBRA_DEBUG_KERNEL
)
1295 "netlink_route_multipath() (%s): "
1296 "nexthop via %s %s if %u",
1297 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1298 label_buf
, nexthop
->ifindex
);
1300 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1301 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1302 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1303 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1306 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1307 *src
= &nexthop
->rmap_src
;
1308 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1309 *src
= &nexthop
->src
;
1311 if (IS_ZEBRA_DEBUG_KERNEL
)
1313 "netlink_route_multipath() (%s): "
1314 "nexthop via %s %s if %u",
1315 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1316 label_buf
, nexthop
->ifindex
);
1320 * We have figured out the ifindex so we should always send it
1321 * This is especially useful if we are doing route
1324 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1325 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1328 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1329 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1330 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1331 *src
= &nexthop
->rmap_src
;
1332 else if (nexthop
->src
.ipv4
.s_addr
)
1333 *src
= &nexthop
->src
;
1335 if (IS_ZEBRA_DEBUG_KERNEL
)
1337 "netlink_route_multipath() (%s): "
1338 "nexthop via if %u",
1339 routedesc
, nexthop
->ifindex
);
1343 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1344 zebra_nhlfe_t
*nhlfe
,
1345 struct nlmsghdr
*nlmsg
,
1346 struct rtmsg
*rtmsg
,
1347 size_t req_size
, int cmd
)
1352 family
= NHLFE_FAMILY(nhlfe
);
1353 bytelen
= (family
== AF_INET
? 4 : 16);
1354 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1355 nlmsg
, rtmsg
, req_size
, cmd
);
1360 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1361 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1362 struct rtmsg
*rtmsg
, union g_addr
**src
)
1367 family
= NHLFE_FAMILY(nhlfe
);
1368 bytelen
= (family
== AF_INET
? 4 : 16);
1369 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1374 /* Log debug information for netlink_route_multipath
1375 * if debug logging is enabled.
1377 * @param cmd: Netlink command which is to be processed
1378 * @param p: Prefix for which the change is due
1379 * @param family: Address family which the change concerns
1380 * @param zvrf: The vrf we are in
1381 * @param tableid: The table we are working on
1383 static void _netlink_route_debug(int cmd
, const struct prefix
*p
,
1384 int family
, vrf_id_t vrfid
,
1387 if (IS_ZEBRA_DEBUG_KERNEL
) {
1388 char buf
[PREFIX_STRLEN
];
1390 "netlink_route_multipath(): %s %s vrf %u(%u)",
1391 nl_msg_type_to_str(cmd
),
1392 prefix2str(p
, buf
, sizeof(buf
)),
1397 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1399 if (IS_ZEBRA_DEBUG_KERNEL
)
1400 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1401 nl_msg_type_to_str(cmd
), label
);
1404 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1405 int llalen
, ns_id_t ns_id
)
1413 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1415 memset(&req
, 0, sizeof(req
));
1417 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1418 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1419 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1420 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1422 req
.ndm
.ndm_family
= AF_INET
;
1423 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1424 req
.ndm
.ndm_ifindex
= ifindex
;
1425 req
.ndm
.ndm_type
= RTN_UNICAST
;
1427 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1428 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1430 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1434 /* Routing table change via netlink interface. */
1435 /* Update flag indicates whether this is a "replace" or not. */
1436 static int netlink_route_multipath(int cmd
, const struct prefix
*p
,
1437 const struct prefix
*src_p
,
1438 struct route_entry
*re
,
1442 struct sockaddr_nl snl
;
1443 struct nexthop
*nexthop
= NULL
;
1444 unsigned int nexthop_num
;
1445 int family
= PREFIX_FAMILY(p
);
1446 const char *routedesc
;
1453 char buf
[NL_PKT_BUF_SIZE
];
1456 struct zebra_ns
*zns
;
1457 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1460 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1462 bytelen
= (family
== AF_INET
? 4 : 16);
1464 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1465 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1466 if ((cmd
== RTM_NEWROUTE
) && update
)
1467 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1468 req
.n
.nlmsg_type
= cmd
;
1469 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1471 req
.r
.rtm_family
= family
;
1472 req
.r
.rtm_dst_len
= p
->prefixlen
;
1473 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1474 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1475 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1478 * blackhole routes are not RTN_UNICAST, they are
1479 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1480 * so setting this value as a RTN_UNICAST would
1481 * cause the route lookup of just the prefix
1482 * to fail. So no need to specify this for
1483 * the RTM_DELROUTE case
1485 if (cmd
!= RTM_DELROUTE
)
1486 req
.r
.rtm_type
= RTN_UNICAST
;
1488 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1490 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1494 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1496 * either by the kernel or by zebra. Its purely for calculating best
1498 * by the routing protocol and for communicating with protocol peers.
1500 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1501 #if defined(SUPPORT_REALMS)
1502 if (re
->tag
> 0 && re
->tag
<= 255)
1503 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1505 /* Table corresponding to this route. */
1506 if (re
->table
< 256)
1507 req
.r
.rtm_table
= re
->table
;
1509 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1510 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1513 _netlink_route_debug(cmd
, p
, family
, zvrf_id(zvrf
), re
->table
);
1516 * If we are not updating the route and we have received
1517 * a route delete, then all we need to fill in is the
1518 * prefix information to tell the kernel to schwack
1521 if (!update
&& cmd
== RTM_DELROUTE
)
1524 if (re
->mtu
|| re
->nexthop_mtu
) {
1525 char buf
[NL_PKT_BUF_SIZE
];
1526 struct rtattr
*rta
= (void *)buf
;
1527 uint32_t mtu
= re
->mtu
;
1528 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1529 mtu
= re
->nexthop_mtu
;
1530 rta
->rta_type
= RTA_METRICS
;
1531 rta
->rta_len
= RTA_LENGTH(0);
1532 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1533 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1537 /* Count overall nexthops so we can decide whether to use singlepath
1538 * or multipath case. */
1540 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1541 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1543 if (cmd
== RTM_NEWROUTE
&& !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1545 if (cmd
== RTM_DELROUTE
1546 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1552 /* Singlepath case. */
1553 if (nexthop_num
== 1 || multipath_num
== 1) {
1555 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1557 * So we want to cover 2 types of blackhole
1559 * 1) A normal blackhole route( ala from a static
1561 * 2) A recursively resolved blackhole route
1563 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1564 switch (nexthop
->bh_type
) {
1565 case BLACKHOLE_ADMINPROHIB
:
1566 req
.r
.rtm_type
= RTN_PROHIBIT
;
1568 case BLACKHOLE_REJECT
:
1569 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1572 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1577 if (CHECK_FLAG(nexthop
->flags
,
1578 NEXTHOP_FLAG_RECURSIVE
)) {
1580 if (family
== AF_INET
) {
1581 if (nexthop
->rmap_src
.ipv4
1588 } else if (nexthop
->src
.ipv4
1596 } else if (family
== AF_INET6
) {
1597 if (!IN6_IS_ADDR_UNSPECIFIED(
1605 !IN6_IS_ADDR_UNSPECIFIED(
1618 if ((cmd
== RTM_NEWROUTE
1619 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1620 || (cmd
== RTM_DELROUTE
1621 && CHECK_FLAG(nexthop
->flags
,
1622 NEXTHOP_FLAG_FIB
))) {
1623 routedesc
= nexthop
->rparent
1624 ? "recursive, single-path"
1627 _netlink_route_build_singlepath(
1628 routedesc
, bytelen
, nexthop
, &req
.n
,
1629 &req
.r
, sizeof req
, cmd
);
1634 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1635 if (family
== AF_INET
)
1636 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1637 &src
.ipv4
, bytelen
);
1638 else if (family
== AF_INET6
)
1639 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1640 &src
.ipv6
, bytelen
);
1643 char buf
[NL_PKT_BUF_SIZE
];
1644 struct rtattr
*rta
= (void *)buf
;
1645 struct rtnexthop
*rtnh
;
1646 union g_addr
*src1
= NULL
;
1648 rta
->rta_type
= RTA_MULTIPATH
;
1649 rta
->rta_len
= RTA_LENGTH(0);
1650 rtnh
= RTA_DATA(rta
);
1653 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1654 if (nexthop_num
>= multipath_num
)
1657 if (CHECK_FLAG(nexthop
->flags
,
1658 NEXTHOP_FLAG_RECURSIVE
)) {
1659 /* This only works for IPv4 now */
1661 if (family
== AF_INET
) {
1662 if (nexthop
->rmap_src
.ipv4
1669 } else if (nexthop
->src
.ipv4
1677 } else if (family
== AF_INET6
) {
1678 if (!IN6_IS_ADDR_UNSPECIFIED(
1686 !IN6_IS_ADDR_UNSPECIFIED(
1699 if ((cmd
== RTM_NEWROUTE
1700 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1701 || (cmd
== RTM_DELROUTE
1702 && CHECK_FLAG(nexthop
->flags
,
1703 NEXTHOP_FLAG_FIB
))) {
1704 routedesc
= nexthop
->rparent
1705 ? "recursive, multipath"
1709 _netlink_route_build_multipath(
1710 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1712 rtnh
= RTNH_NEXT(rtnh
);
1714 if (!setsrc
&& src1
) {
1715 if (family
== AF_INET
)
1716 src
.ipv4
= src1
->ipv4
;
1717 else if (family
== AF_INET6
)
1718 src
.ipv6
= src1
->ipv6
;
1724 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1725 if (family
== AF_INET
)
1726 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1727 &src
.ipv4
, bytelen
);
1728 else if (family
== AF_INET6
)
1729 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1730 &src
.ipv6
, bytelen
);
1731 if (IS_ZEBRA_DEBUG_KERNEL
)
1732 zlog_debug("Setting source");
1735 if (rta
->rta_len
> RTA_LENGTH(0))
1736 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1737 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1740 /* If there is no useful nexthop then return. */
1741 if (nexthop_num
== 0) {
1742 if (IS_ZEBRA_DEBUG_KERNEL
)
1744 "netlink_route_multipath(): No useful nexthop.");
1750 /* Destination netlink address. */
1751 memset(&snl
, 0, sizeof snl
);
1752 snl
.nl_family
= AF_NETLINK
;
1754 /* Talk to netlink socket. */
1755 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1759 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1762 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1770 struct zebra_ns
*zns
;
1773 memset(&req
, 0, sizeof(req
));
1775 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1776 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1777 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1779 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1780 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1782 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1783 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1784 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1785 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1786 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1788 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1789 &zns
->netlink_cmd
, zns
, 0);
1795 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
1796 const struct prefix
*p
,
1797 const struct prefix
*src_p
,
1798 struct route_entry
*old
,
1799 struct route_entry
*new)
1806 if (p
->family
== AF_INET
|| v6_rr_semantics
)
1807 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1808 new, (old
) ? 1 : 0);
1811 * So v6 route replace semantics are not in
1812 * the kernel at this point as I understand it.
1813 * So let's do a delete than an add.
1814 * In the future once v6 route replace semantics
1815 * are in we can figure out what to do here to
1816 * allow working with old and new kernels.
1818 * I'm also intentionally ignoring the failure case
1819 * of the route delete. If that happens yeah we're
1823 netlink_route_multipath(RTM_DELROUTE
, p
, src_p
,
1825 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1828 kernel_route_rib_pass_fail(rn
, p
, new,
1829 (!ret
) ? DP_INSTALL_SUCCESS
1830 : DP_INSTALL_FAILURE
);
1831 return DP_REQUEST_SUCCESS
;
1835 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1837 kernel_route_rib_pass_fail(rn
, p
, old
,
1838 (!ret
) ? DP_DELETE_SUCCESS
1839 : DP_DELETE_FAILURE
);
1842 return DP_REQUEST_SUCCESS
;
1845 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1846 int llalen
, ns_id_t ns_id
)
1848 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1849 addr
, lla
, llalen
, ns_id
);
1853 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1854 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1856 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1857 struct in_addr
*vtep_ip
, int cmd
)
1859 struct zebra_ns
*zns
;
1865 uint8_t dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1866 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
1869 memset(&req
, 0, sizeof(req
));
1871 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1872 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1873 if (cmd
== RTM_NEWNEIGH
)
1874 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1875 req
.n
.nlmsg_type
= cmd
;
1876 req
.ndm
.ndm_family
= PF_BRIDGE
;
1877 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1878 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1881 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1882 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1883 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1885 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1890 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1892 * a "flood" MAC FDB entry.
1894 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1896 if (IS_ZEBRA_DEBUG_VXLAN
)
1897 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1898 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1900 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1904 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1905 * deleting the "flood" MAC FDB entry.
1907 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1909 if (IS_ZEBRA_DEBUG_VXLAN
)
1911 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1912 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1914 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1918 #define NDA_RTA(r) \
1919 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1922 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
1925 struct interface
*ifp
;
1926 struct zebra_if
*zif
;
1927 struct rtattr
*tb
[NDA_MAX
+ 1];
1928 struct interface
*br_if
;
1931 struct prefix vtep_ip
;
1932 int vid_present
= 0, dst_present
= 0;
1933 char buf
[ETHER_ADDR_STRLEN
];
1938 ndm
= NLMSG_DATA(h
);
1940 /* We only process macfdb notifications if EVPN is enabled */
1941 if (!is_evpn_enabled())
1944 /* The interface should exist. */
1945 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
1947 if (!ifp
|| !ifp
->info
)
1950 /* The interface should be something we're interested in. */
1951 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1954 /* Drop "permanent" entries. */
1955 if (ndm
->ndm_state
& NUD_PERMANENT
)
1958 zif
= (struct zebra_if
*)ifp
->info
;
1959 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1960 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1961 nl_msg_type_to_str(h
->nlmsg_type
),
1962 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1963 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1967 /* Parse attributes and extract fields of interest. */
1968 memset(tb
, 0, sizeof tb
);
1969 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1971 if (!tb
[NDA_LLADDR
]) {
1972 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1973 nl_msg_type_to_str(h
->nlmsg_type
),
1974 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1975 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1979 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1981 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1982 nl_msg_type_to_str(h
->nlmsg_type
),
1983 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1984 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1985 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1989 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1991 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1993 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
1994 sprintf(vid_buf
, " VLAN %u", vid
);
1998 /* TODO: Only IPv4 supported now. */
2000 vtep_ip
.family
= AF_INET
;
2001 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
2002 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
2004 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
2007 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
2009 if (IS_ZEBRA_DEBUG_KERNEL
)
2010 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2011 nl_msg_type_to_str(h
->nlmsg_type
),
2012 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2013 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2014 sticky
? "sticky " : "",
2015 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2016 dst_present
? dst_buf
: "");
2018 if (filter_vlan
&& vid
!= filter_vlan
)
2021 /* If add or update, do accordingly if learnt on a "local" interface; if
2022 * the notification is over VxLAN, this has to be related to
2024 * so perform an implicit delete of any local entry (if it exists).
2026 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2027 /* Drop "permanent" entries. */
2028 if (ndm
->ndm_state
& NUD_PERMANENT
)
2031 if (IS_ZEBRA_IF_VXLAN(ifp
))
2032 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2035 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2039 /* This is a delete notification.
2040 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2041 * 2. For a MAC over "local" interface, delete the mac
2042 * Note: We will get notifications from both bridge driver and VxLAN
2044 * Ignore the notification from VxLan driver as it is also generated
2045 * when mac moves from remote to local.
2050 if (IS_ZEBRA_IF_VXLAN(ifp
))
2051 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
2054 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
2057 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2062 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2065 /* Length validity. */
2066 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2070 /* We are interested only in AF_BRIDGE notifications. */
2071 ndm
= NLMSG_DATA(h
);
2072 if (ndm
->ndm_family
!= AF_BRIDGE
)
2075 return netlink_macfdb_change(h
, len
, ns_id
);
2078 /* Request for MAC FDB information from the kernel */
2079 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
2080 ifindex_t master_ifindex
)
2084 struct ifinfomsg ifm
;
2088 /* Form the request, specifying filter (rtattr) if needed. */
2089 memset(&req
, 0, sizeof(req
));
2090 req
.n
.nlmsg_type
= type
;
2091 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2092 req
.ifm
.ifi_family
= family
;
2094 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
2096 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2100 * MAC forwarding database read using netlink interface. This is invoked
2103 int netlink_macfdb_read(struct zebra_ns
*zns
)
2107 /* Get bridge FDB table. */
2108 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
2111 /* We are reading entire table. */
2113 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2120 * MAC forwarding database read using netlink interface. This is for a
2121 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2123 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
2124 struct interface
*br_if
)
2126 struct zebra_if
*br_zif
;
2127 struct zebra_if
*zif
;
2128 struct zebra_l2info_vxlan
*vxl
;
2132 /* Save VLAN we're filtering on, if needed. */
2133 br_zif
= (struct zebra_if
*)br_if
->info
;
2134 zif
= (struct zebra_if
*)ifp
->info
;
2135 vxl
= &zif
->l2info
.vxl
;
2136 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
2137 filter_vlan
= vxl
->access_vlan
;
2139 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2141 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2145 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2148 /* Reset VLAN filter. */
2153 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2154 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2155 int local
, int cmd
, uint8_t sticky
)
2157 struct zebra_ns
*zns
;
2164 struct zebra_if
*zif
;
2165 struct interface
*br_if
;
2166 struct zebra_if
*br_zif
;
2167 char buf
[ETHER_ADDR_STRLEN
];
2168 int vid_present
= 0, dst_present
= 0;
2171 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2175 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2176 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2177 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2182 memset(&req
, 0, sizeof(req
));
2184 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2185 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2186 if (cmd
== RTM_NEWNEIGH
)
2187 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2188 req
.n
.nlmsg_type
= cmd
;
2189 req
.ndm
.ndm_family
= AF_BRIDGE
;
2190 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2191 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2194 req
.ndm
.ndm_state
|= NUD_NOARP
;
2196 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2198 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2199 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2201 dst_alen
= 4; // TODO: hardcoded
2202 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2204 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2206 br_zif
= (struct zebra_if
*)br_if
->info
;
2207 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2208 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2210 sprintf(vid_buf
, " VLAN %u", vid
);
2212 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2214 if (IS_ZEBRA_DEBUG_KERNEL
)
2215 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2216 nl_msg_type_to_str(cmd
),
2217 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2218 ifp
->ifindex
, vid_present
? vid_buf
: "",
2219 sticky
? "sticky " : "",
2220 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2221 dst_present
? dst_buf
: "");
2223 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2228 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2231 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2234 struct interface
*ifp
;
2235 struct zebra_if
*zif
;
2236 struct rtattr
*tb
[NDA_MAX
+ 1];
2237 struct interface
*link_if
;
2240 char buf
[ETHER_ADDR_STRLEN
];
2241 char buf2
[INET6_ADDRSTRLEN
];
2242 int mac_present
= 0;
2243 uint8_t ext_learned
;
2244 uint8_t router_flag
;
2246 ndm
= NLMSG_DATA(h
);
2248 /* The interface should exist. */
2249 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2251 if (!ifp
|| !ifp
->info
)
2254 zif
= (struct zebra_if
*)ifp
->info
;
2256 /* Parse attributes and extract fields of interest. */
2257 memset(tb
, 0, sizeof tb
);
2258 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2261 zlog_warn("%s family %s IF %s(%u) - no DST",
2262 nl_msg_type_to_str(h
->nlmsg_type
),
2263 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2268 memset(&ip
, 0, sizeof(struct ipaddr
));
2269 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2270 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2272 /* Drop some "permanent" entries. */
2273 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2274 char buf
[16] = "169.254.0.1";
2275 struct in_addr ipv4_ll
;
2277 if (ndm
->ndm_family
!= AF_INET
)
2280 if (!zif
->v6_2_v4_ll_neigh_entry
)
2283 if (h
->nlmsg_type
!= RTM_DELNEIGH
)
2286 inet_pton(AF_INET
, buf
, &ipv4_ll
);
2287 if (ipv4_ll
.s_addr
!= ip
.ip
._v4_addr
.s_addr
)
2290 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2291 ifp
, &zif
->v6_2_v4_ll_addr6
, true);
2295 /* The neighbor is present on an SVI. From this, we locate the
2297 * bridge because we're only interested in neighbors on a VxLAN bridge.
2298 * The bridge is located based on the nature of the SVI:
2299 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2301 * and is linked to the bridge
2302 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2306 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2307 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2311 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2316 memset(&mac
, 0, sizeof(struct ethaddr
));
2317 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2318 if (tb
[NDA_LLADDR
]) {
2319 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2321 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2322 nl_msg_type_to_str(h
->nlmsg_type
),
2323 nl_family_to_str(ndm
->ndm_family
),
2324 ifp
->name
, ndm
->ndm_ifindex
,
2325 (unsigned long)RTA_PAYLOAD(
2331 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2334 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2335 router_flag
= (ndm
->ndm_flags
& NTF_ROUTER
) ? 1 : 0;
2337 if (IS_ZEBRA_DEBUG_KERNEL
)
2339 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2340 nl_msg_type_to_str(h
->nlmsg_type
),
2341 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2343 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2345 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2347 ndm
->ndm_state
, ndm
->ndm_flags
);
2349 /* If the neighbor state is valid for use, process as an add or
2351 * else process as a delete. Note that the delete handling may
2353 * in re-adding the neighbor if it is a valid "remote" neighbor.
2355 if (ndm
->ndm_state
& NUD_VALID
)
2356 return zebra_vxlan_handle_kernel_neigh_update(
2357 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2358 ext_learned
, router_flag
);
2360 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2363 if (IS_ZEBRA_DEBUG_KERNEL
)
2364 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2365 nl_msg_type_to_str(h
->nlmsg_type
),
2366 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2368 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2370 /* Process the delete - it may result in re-adding the neighbor if it is
2371 * a valid "remote" neighbor.
2373 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
2376 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2381 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2384 /* Length validity. */
2385 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2389 /* We are interested only in AF_INET or AF_INET6 notifications. */
2390 ndm
= NLMSG_DATA(h
);
2391 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2394 return netlink_neigh_change(h
, len
);
2397 /* Request for IP neighbor information from the kernel */
2398 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2407 /* Form the request, specifying filter (rtattr) if needed. */
2408 memset(&req
, 0, sizeof(req
));
2409 req
.n
.nlmsg_type
= type
;
2410 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2411 req
.ndm
.ndm_family
= family
;
2413 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2415 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2419 * IP Neighbor table read using netlink interface. This is invoked
2422 int netlink_neigh_read(struct zebra_ns
*zns
)
2426 /* Get IP neighbor table. */
2427 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2430 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2437 * IP Neighbor table read using netlink interface. This is for a specific
2440 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2444 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2448 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2454 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
2459 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2462 /* Length validity. */
2463 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2465 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2466 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
2467 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
2471 /* Is this a notification for the MAC FDB or IP neighbor table? */
2472 ndm
= NLMSG_DATA(h
);
2473 if (ndm
->ndm_family
== AF_BRIDGE
)
2474 return netlink_macfdb_change(h
, len
, ns_id
);
2476 if (ndm
->ndm_type
!= RTN_UNICAST
)
2479 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2480 return netlink_ipneigh_change(h
, len
, ns_id
);
2483 "Invalid address family: %u received from kernel neighbor change: %u",
2484 ndm
->ndm_family
, h
->nlmsg_type
);
2491 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2492 struct ethaddr
*mac
, uint8_t flags
,
2493 uint16_t state
, int cmd
)
2502 struct zebra_ns
*zns
;
2503 char buf
[INET6_ADDRSTRLEN
];
2504 char buf2
[ETHER_ADDR_STRLEN
];
2505 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
2508 memset(&req
, 0, sizeof(req
));
2510 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2511 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2512 if (cmd
== RTM_NEWNEIGH
)
2513 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2514 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2515 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2516 req
.ndm
.ndm_state
= state
;
2517 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2518 req
.ndm
.ndm_type
= RTN_UNICAST
;
2519 req
.ndm
.ndm_flags
= flags
;
2521 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2522 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2524 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2526 if (IS_ZEBRA_DEBUG_KERNEL
)
2527 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2528 nl_msg_type_to_str(cmd
),
2529 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2530 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2531 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2534 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2538 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2539 struct in_addr vtep_ip
, uint8_t sticky
)
2541 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2545 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2546 struct in_addr vtep_ip
, int local
)
2548 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2552 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2553 struct ethaddr
*mac
, uint8_t flags
)
2555 return netlink_neigh_update2(ifp
, ip
, mac
, flags
,
2556 NUD_NOARP
, RTM_NEWNEIGH
);
2559 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2561 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, 0, RTM_DELNEIGH
);
2565 * MPLS label forwarding table change via netlink interface.
2567 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2570 zebra_nhlfe_t
*nhlfe
;
2571 struct nexthop
*nexthop
= NULL
;
2572 unsigned int nexthop_num
;
2573 const char *routedesc
;
2574 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2580 char buf
[NL_PKT_BUF_SIZE
];
2583 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2586 * Count # nexthops so we can decide whether to use singlepath
2587 * or multipath case.
2590 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2591 nexthop
= nhlfe
->nexthop
;
2594 if (cmd
== RTM_NEWROUTE
) {
2595 /* Count all selected NHLFEs */
2596 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2597 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2601 /* Count all installed NHLFEs */
2602 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2603 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2608 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2611 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2612 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2613 req
.n
.nlmsg_type
= cmd
;
2614 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2616 req
.r
.rtm_family
= AF_MPLS
;
2617 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2618 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2619 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2620 req
.r
.rtm_type
= RTN_UNICAST
;
2622 if (cmd
== RTM_NEWROUTE
) {
2623 /* We do a replace to handle update. */
2624 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2626 /* set the protocol value if installing */
2627 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2628 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2631 /* Fill destination */
2632 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2633 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2635 /* Fill nexthops (paths) based on single-path or multipath. The paths
2636 * chosen depend on the operation.
2638 if (nexthop_num
== 1 || multipath_num
== 1) {
2639 routedesc
= "single-path";
2640 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2643 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2644 nexthop
= nhlfe
->nexthop
;
2648 if ((cmd
== RTM_NEWROUTE
2649 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2650 && CHECK_FLAG(nexthop
->flags
,
2651 NEXTHOP_FLAG_ACTIVE
)))
2652 || (cmd
== RTM_DELROUTE
2653 && (CHECK_FLAG(nhlfe
->flags
,
2654 NHLFE_FLAG_INSTALLED
)
2655 && CHECK_FLAG(nexthop
->flags
,
2656 NEXTHOP_FLAG_FIB
)))) {
2657 /* Add the gateway */
2658 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2665 } else /* Multipath case */
2667 char buf
[NL_PKT_BUF_SIZE
];
2668 struct rtattr
*rta
= (void *)buf
;
2669 struct rtnexthop
*rtnh
;
2670 union g_addr
*src1
= NULL
;
2672 rta
->rta_type
= RTA_MULTIPATH
;
2673 rta
->rta_len
= RTA_LENGTH(0);
2674 rtnh
= RTA_DATA(rta
);
2676 routedesc
= "multipath";
2677 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2680 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2681 nexthop
= nhlfe
->nexthop
;
2685 if (nexthop_num
>= multipath_num
)
2688 if ((cmd
== RTM_NEWROUTE
2689 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2690 && CHECK_FLAG(nexthop
->flags
,
2691 NEXTHOP_FLAG_ACTIVE
)))
2692 || (cmd
== RTM_DELROUTE
2693 && (CHECK_FLAG(nhlfe
->flags
,
2694 NHLFE_FLAG_INSTALLED
)
2695 && CHECK_FLAG(nexthop
->flags
,
2696 NEXTHOP_FLAG_FIB
)))) {
2699 /* Build the multipath */
2700 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2703 rtnh
= RTNH_NEXT(rtnh
);
2707 /* Add the multipath */
2708 if (rta
->rta_len
> RTA_LENGTH(0))
2709 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2710 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2713 /* Talk to netlink socket. */
2714 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2717 #endif /* HAVE_NETLINK */