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/zserv.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_STATIC
) || (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
)) {
108 static inline int zebra2proto(int proto
)
111 case ZEBRA_ROUTE_BABEL
:
112 proto
= RTPROT_BABEL
;
114 case ZEBRA_ROUTE_BGP
:
117 case ZEBRA_ROUTE_OSPF
:
118 case ZEBRA_ROUTE_OSPF6
:
121 case ZEBRA_ROUTE_STATIC
:
122 proto
= RTPROT_STATIC
;
124 case ZEBRA_ROUTE_ISIS
:
127 case ZEBRA_ROUTE_RIP
:
130 case ZEBRA_ROUTE_RIPNG
:
131 proto
= RTPROT_RIPNG
;
133 case ZEBRA_ROUTE_NHRP
:
136 case ZEBRA_ROUTE_EIGRP
:
137 proto
= RTPROT_EIGRP
;
139 case ZEBRA_ROUTE_LDP
:
142 case ZEBRA_ROUTE_SHARP
:
143 proto
= RTPROT_SHARP
;
146 proto
= RTPROT_ZEBRA
;
153 static inline int proto2zebra(int proto
, int family
)
157 proto
= ZEBRA_ROUTE_BABEL
;
160 proto
= ZEBRA_ROUTE_BGP
;
163 proto
= (family
== AFI_IP
) ?
164 ZEBRA_ROUTE_OSPF
: ZEBRA_ROUTE_OSPF6
;
167 proto
= ZEBRA_ROUTE_ISIS
;
170 proto
= ZEBRA_ROUTE_RIP
;
173 proto
= ZEBRA_ROUTE_RIPNG
;
176 proto
= ZEBRA_ROUTE_NHRP
;
179 proto
= ZEBRA_ROUTE_EIGRP
;
182 proto
= ZEBRA_ROUTE_LDP
;
185 proto
= ZEBRA_ROUTE_STATIC
;
188 proto
= ZEBRA_ROUTE_KERNEL
;
195 Pending: create an efficient table_id (in a tree/hash) based lookup)
197 static vrf_id_t
vrf_lookup_by_table(u_int32_t table_id
)
200 struct zebra_vrf
*zvrf
;
202 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
203 if ((zvrf
= vrf
->info
) == NULL
|| (zvrf
->table_id
!= table_id
))
206 return zvrf_id(zvrf
);
212 /* Looking up routing table by netlink interface. */
213 static int netlink_route_change_read_unicast(struct sockaddr_nl
*snl
,
214 struct nlmsghdr
*h
, ns_id_t ns_id
,
219 struct rtattr
*tb
[RTA_MAX
+ 1];
222 struct prefix_ipv6 src_p
= {};
223 vrf_id_t vrf_id
= VRF_DEFAULT
;
225 char anyaddr
[16] = {0};
227 int proto
= ZEBRA_ROUTE_KERNEL
;
232 uint8_t distance
= 0;
237 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
238 void *src
= NULL
; /* IPv6 srcdest source prefix */
239 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
243 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
245 switch (rtm
->rtm_type
) {
249 bh_type
= BLACKHOLE_NULL
;
251 case RTN_UNREACHABLE
:
252 bh_type
= BLACKHOLE_REJECT
;
255 bh_type
= BLACKHOLE_ADMINPROHIB
;
261 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
265 memset(tb
, 0, sizeof tb
);
266 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
268 if (rtm
->rtm_flags
& RTM_F_CLONED
)
270 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
272 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
275 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
276 && h
->nlmsg_type
== RTM_NEWROUTE
)
279 /* We don't care about change notifications for the MPLS table. */
280 /* TODO: Revisit this. */
281 if (rtm
->rtm_family
== AF_MPLS
)
284 /* Table corresponding to route. */
286 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
288 table
= rtm
->rtm_table
;
291 vrf_id
= vrf_lookup_by_table(table
);
292 if (vrf_id
== VRF_DEFAULT
) {
293 if (!is_zebra_valid_kernel_table(table
)
294 && !is_zebra_main_routing_table(table
))
298 /* Route which inserted by Zebra. */
299 if (is_selfroute(rtm
->rtm_protocol
)) {
300 flags
|= ZEBRA_FLAG_SELFROUTE
;
301 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
);
304 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
307 dest
= RTA_DATA(tb
[RTA_DST
]);
312 src
= RTA_DATA(tb
[RTA_SRC
]);
317 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
320 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
322 if (tb
[RTA_PRIORITY
])
323 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
325 #if defined(SUPPORT_REALMS)
327 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
330 if (tb
[RTA_METRICS
]) {
331 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
333 memset(mxrta
, 0, sizeof mxrta
);
334 netlink_parse_rtattr(mxrta
, RTAX_MAX
,
335 RTA_DATA(tb
[RTA_METRICS
]),
336 RTA_PAYLOAD(tb
[RTA_METRICS
]));
339 mtu
= *(u_int32_t
*)RTA_DATA(mxrta
[RTAX_MTU
]);
342 if (rtm
->rtm_family
== AF_INET
) {
344 memcpy(&p
.u
.prefix4
, dest
, 4);
345 p
.prefixlen
= rtm
->rtm_dst_len
;
348 0; // Forces debug below to not display anything
349 } else if (rtm
->rtm_family
== AF_INET6
) {
351 memcpy(&p
.u
.prefix6
, dest
, 16);
352 p
.prefixlen
= rtm
->rtm_dst_len
;
354 src_p
.family
= AF_INET6
;
355 memcpy(&src_p
.prefix
, src
, 16);
356 src_p
.prefixlen
= rtm
->rtm_src_len
;
359 if (rtm
->rtm_src_len
!= 0) {
360 char buf
[PREFIX_STRLEN
];
362 "unsupported IPv[4|6] sourcedest route (dest %s vrf %u)",
363 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
368 * For ZEBRA_ROUTE_KERNEL types:
370 * The metric/priority of the route received from the kernel
371 * is a 32 bit number. We are going to interpret the high
372 * order byte as the Admin Distance and the low order 3 bytes
375 * This will allow us to do two things:
376 * 1) Allow the creation of kernel routes that can be
377 * overridden by zebra.
378 * 2) Allow the old behavior for 'most' kernel route types
379 * if a user enters 'ip route ...' v4 routes get a metric
380 * of 0 and v6 routes get a metric of 1024. Both of these
381 * values will end up with a admin distance of 0, which
382 * will cause them to win for the purposes of zebra.
384 if (proto
== ZEBRA_ROUTE_KERNEL
) {
385 distance
= (metric
>> 24) & 0xFF;
386 metric
= (metric
& 0x00FFFFFF);
389 if (IS_ZEBRA_DEBUG_KERNEL
) {
390 char buf
[PREFIX_STRLEN
];
391 char buf2
[PREFIX_STRLEN
];
393 "%s %s%s%s vrf %u metric: %d Admin Distance: %d", nl_msg_type_to_str(h
->nlmsg_type
),
394 prefix2str(&p
, buf
, sizeof(buf
)),
395 src_p
.prefixlen
? " from " : "",
396 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
398 vrf_id
, metric
, distance
);
402 if (rtm
->rtm_family
== AF_INET6
)
405 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
406 struct interface
*ifp
;
407 vrf_id_t nh_vrf_id
= vrf_id
;
409 if (!tb
[RTA_MULTIPATH
]) {
411 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
413 memset(&nh
, 0, sizeof(nh
));
415 if (bh_type
== BLACKHOLE_UNSPEC
) {
417 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
418 else if (index
&& gate
)
419 nh
.type
= (afi
== AFI_IP
)
420 ? NEXTHOP_TYPE_IPV4_IFINDEX
421 : NEXTHOP_TYPE_IPV6_IFINDEX
;
422 else if (!index
&& gate
)
423 nh
.type
= (afi
== AFI_IP
)
427 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
428 nh
.bh_type
= bh_type
;
431 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
432 nh
.bh_type
= bh_type
;
436 memcpy(&nh
.src
, prefsrc
, sz
);
438 memcpy(&nh
.gate
, gate
, sz
);
441 ifp
= if_lookup_by_index(index
,
444 nh_vrf_id
= ifp
->vrf_id
;
446 nh
.vrf_id
= nh_vrf_id
;
448 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
449 NULL
, &nh
, table
, metric
, mtu
, distance
, tag
);
451 /* This is a multipath route */
453 struct route_entry
*re
;
454 struct rtnexthop
*rtnh
=
455 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
457 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
459 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
461 re
->distance
= distance
;
468 re
->uptime
= time(NULL
);
473 if (len
< (int)sizeof(*rtnh
)
474 || rtnh
->rtnh_len
> len
)
477 index
= rtnh
->rtnh_ifindex
;
480 * Yes we are looking this up
481 * for every nexthop and just
482 * using the last one looked
485 ifp
= if_lookup_by_index(index
,
488 nh_vrf_id
= ifp
->vrf_id
;
491 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
494 nh_vrf_id
= VRF_DEFAULT
;
500 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
501 memset(tb
, 0, sizeof(tb
));
502 netlink_parse_rtattr(
503 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
504 rtnh
->rtnh_len
- sizeof(*rtnh
));
511 if (rtm
->rtm_family
== AF_INET
) {
513 route_entry_nexthop_ipv4_ifindex_add(
518 route_entry_nexthop_ipv4_add(
522 } else if (rtm
->rtm_family
525 route_entry_nexthop_ipv6_ifindex_add(
529 route_entry_nexthop_ipv6_add(
534 route_entry_nexthop_ifindex_add(
535 re
, index
, nh_vrf_id
);
537 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
538 rtnh
= RTNH_NEXT(rtnh
);
541 zserv_nexthop_num_warn(__func__
,
542 (const struct prefix
*)&p
,
544 if (re
->nexthop_num
== 0)
547 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
551 if (!tb
[RTA_MULTIPATH
]) {
553 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
555 memset(&nh
, 0, sizeof(nh
));
556 if (bh_type
== BLACKHOLE_UNSPEC
) {
558 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
559 else if (index
&& gate
)
562 ? NEXTHOP_TYPE_IPV4_IFINDEX
563 : NEXTHOP_TYPE_IPV6_IFINDEX
;
564 else if (!index
&& gate
)
565 nh
.type
= (afi
== AFI_IP
)
569 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
570 nh
.bh_type
= BLACKHOLE_UNSPEC
;
573 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
574 nh
.bh_type
= bh_type
;
578 memcpy(&nh
.gate
, gate
, sz
);
579 rib_delete(afi
, SAFI_UNICAST
, vrf_id
,
580 proto
, 0, flags
, &p
, NULL
, &nh
,
581 table
, metric
, true, NULL
);
583 /* XXX: need to compare the entire list of nexthops
584 * here for NLM_F_APPEND stupidity */
585 rib_delete(afi
, SAFI_UNICAST
, vrf_id
,
586 proto
, 0, flags
, &p
, NULL
, NULL
,
587 table
, metric
, true, NULL
);
594 static struct mcast_route_data
*mroute
= NULL
;
596 static int netlink_route_change_read_multicast(struct sockaddr_nl
*snl
,
598 ns_id_t ns_id
, int startup
)
602 struct rtattr
*tb
[RTA_MAX
+ 1];
603 struct mcast_route_data
*m
;
604 struct mcast_route_data mr
;
611 char oif_list
[256] = "\0";
612 vrf_id_t vrf
= ns_id
;
618 memset(&mr
, 0, sizeof(mr
));
624 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
626 memset(tb
, 0, sizeof tb
);
627 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
630 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
632 table
= rtm
->rtm_table
;
634 vrf
= vrf_lookup_by_table(table
);
637 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
640 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
643 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
645 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
646 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
648 if (tb
[RTA_MULTIPATH
]) {
649 struct rtnexthop
*rtnh
=
650 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
652 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
654 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
657 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
660 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
661 rtnh
= RTNH_NEXT(rtnh
);
665 if (IS_ZEBRA_DEBUG_KERNEL
) {
666 struct interface
*ifp
;
667 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
668 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
669 for (count
= 0; count
< oif_count
; count
++) {
670 ifp
= if_lookup_by_index(oif
[count
], vrf
);
673 sprintf(temp
, "%s ", ifp
->name
);
674 strcat(oif_list
, temp
);
676 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vrf
);
677 ifp
= if_lookup_by_index(iif
, vrf
);
679 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
680 zvrf
->vrf
->name
, vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
681 sbuf
, gbuf
, ifp
->name
, oif_list
, m
->lastused
);
686 int netlink_route_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
687 ns_id_t ns_id
, int startup
)
690 vrf_id_t vrf_id
= ns_id
;
695 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
696 /* If this is not route add/delete message print warning. */
697 zlog_warn("Kernel message: %d vrf %u\n", h
->nlmsg_type
, vrf_id
);
701 /* Connected route. */
702 if (IS_ZEBRA_DEBUG_KERNEL
)
703 zlog_debug("%s %s %s proto %s vrf %u",
704 nl_msg_type_to_str(h
->nlmsg_type
),
705 nl_family_to_str(rtm
->rtm_family
),
706 nl_rttype_to_str(rtm
->rtm_type
),
707 nl_rtproto_to_str(rtm
->rtm_protocol
), vrf_id
);
709 /* We don't care about change notifications for the MPLS table. */
710 /* TODO: Revisit this. */
711 if (rtm
->rtm_family
== AF_MPLS
)
714 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
718 if (rtm
->rtm_type
== RTN_MULTICAST
)
719 netlink_route_change_read_multicast(snl
, h
, ns_id
, startup
);
721 netlink_route_change_read_unicast(snl
, h
, ns_id
, startup
);
725 /* Request for specific route information from the kernel */
726 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
733 /* Form the request, specifying filter (rtattr) if needed. */
734 memset(&req
, 0, sizeof(req
));
735 req
.n
.nlmsg_type
= type
;
736 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
737 req
.rtm
.rtm_family
= family
;
739 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
742 /* Routing table read function using netlink interface. Only called
744 int netlink_route_read(struct zebra_ns
*zns
)
748 /* Get IPv4 routing table. */
749 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
752 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
753 &zns
->netlink_cmd
, zns
, 0, 1);
757 /* Get IPv6 routing table. */
758 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
761 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
762 &zns
->netlink_cmd
, zns
, 0, 1);
769 static void _netlink_route_nl_add_gateway_info(u_char route_family
,
771 struct nlmsghdr
*nlmsg
,
772 size_t req_size
, int bytelen
,
773 struct nexthop
*nexthop
)
775 if (route_family
== AF_MPLS
) {
776 struct gw_family_t gw_fam
;
778 gw_fam
.family
= gw_family
;
779 if (gw_family
== AF_INET
)
780 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
782 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
783 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
786 if (gw_family
== AF_INET
)
787 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
788 &nexthop
->gate
.ipv4
, bytelen
);
790 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
791 &nexthop
->gate
.ipv6
, bytelen
);
795 static void _netlink_route_rta_add_gateway_info(u_char route_family
,
798 struct rtnexthop
*rtnh
,
799 size_t req_size
, int bytelen
,
800 struct nexthop
*nexthop
)
802 if (route_family
== AF_MPLS
) {
803 struct gw_family_t gw_fam
;
805 gw_fam
.family
= gw_family
;
806 if (gw_family
== AF_INET
)
807 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
809 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
810 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
812 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
814 if (gw_family
== AF_INET
)
815 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
816 &nexthop
->gate
.ipv4
, bytelen
);
818 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
819 &nexthop
->gate
.ipv6
, bytelen
);
820 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
824 /* This function takes a nexthop as argument and adds
825 * the appropriate netlink attributes to an existing
828 * @param routedesc: Human readable description of route type
829 * (direct/recursive, single-/multipath)
830 * @param bytelen: Length of addresses in bytes.
831 * @param nexthop: Nexthop information
832 * @param nlmsg: nlmsghdr structure to fill in.
833 * @param req_size: The size allocated for the message.
835 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
836 struct nexthop
*nexthop
,
837 struct nlmsghdr
*nlmsg
,
839 size_t req_size
, int cmd
)
841 struct mpls_label_stack
*nh_label
;
842 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
847 * label_buf is *only* currently used within debugging.
848 * As such when we assign it we are guarding it inside
849 * a debug test. If you want to change this make sure
850 * you fix this assumption
855 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
858 nh_label
= nh
->nh_label
;
859 if (!nh_label
|| !nh_label
->num_labels
)
862 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
863 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
866 if (IS_ZEBRA_DEBUG_KERNEL
) {
868 sprintf(label_buf
, "label %u",
871 sprintf(label_buf1
, "/%u",
873 strlcat(label_buf
, label_buf1
,
878 out_lse
[num_labels
] =
879 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
885 /* Set the BoS bit */
886 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
888 if (rtmsg
->rtm_family
== AF_MPLS
)
889 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
890 num_labels
* sizeof(mpls_lse_t
));
893 u_int16_t encap
= LWTUNNEL_ENCAP_MPLS
;
895 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
, &encap
,
897 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
898 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
, &out_lse
,
899 num_labels
* sizeof(mpls_lse_t
));
900 addattr_nest_end(nlmsg
, nest
);
904 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
905 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
907 if (rtmsg
->rtm_family
== AF_INET
908 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
909 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
910 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
911 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
912 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
914 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
915 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
916 &nexthop
->rmap_src
.ipv4
, bytelen
);
917 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
918 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
919 &nexthop
->src
.ipv4
, bytelen
);
921 if (IS_ZEBRA_DEBUG_KERNEL
)
923 " 5549: _netlink_route_build_singlepath() (%s): "
924 "nexthop via %s %s if %u",
925 routedesc
, ipv4_ll_buf
, label_buf
,
930 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
931 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
932 /* Send deletes to the kernel without specifying the next-hop */
933 if (cmd
!= RTM_DELROUTE
)
934 _netlink_route_nl_add_gateway_info(
935 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
938 if (cmd
== RTM_NEWROUTE
) {
939 if (nexthop
->rmap_src
.ipv4
.s_addr
)
940 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
941 &nexthop
->rmap_src
.ipv4
, bytelen
);
942 else if (nexthop
->src
.ipv4
.s_addr
)
943 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
944 &nexthop
->src
.ipv4
, bytelen
);
947 if (IS_ZEBRA_DEBUG_KERNEL
)
949 "netlink_route_multipath() (%s): "
950 "nexthop via %s %s if %u",
951 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
952 label_buf
, nexthop
->ifindex
);
955 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
956 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
957 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
958 nlmsg
, req_size
, bytelen
,
961 if (cmd
== RTM_NEWROUTE
) {
962 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
963 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
964 &nexthop
->rmap_src
.ipv6
, bytelen
);
965 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
966 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
967 &nexthop
->src
.ipv6
, bytelen
);
970 if (IS_ZEBRA_DEBUG_KERNEL
)
972 "netlink_route_multipath() (%s): "
973 "nexthop via %s %s if %u",
974 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
975 label_buf
, nexthop
->ifindex
);
979 * We have the ifindex so we should always send it
980 * This is especially useful if we are doing route
983 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
984 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
986 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
987 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
988 if (cmd
== RTM_NEWROUTE
) {
989 if (nexthop
->rmap_src
.ipv4
.s_addr
)
990 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
991 &nexthop
->rmap_src
.ipv4
, bytelen
);
992 else if (nexthop
->src
.ipv4
.s_addr
)
993 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
994 &nexthop
->src
.ipv4
, bytelen
);
997 if (IS_ZEBRA_DEBUG_KERNEL
)
999 "netlink_route_multipath() (%s): "
1000 "nexthop via if %u",
1001 routedesc
, nexthop
->ifindex
);
1004 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1005 if (cmd
== RTM_NEWROUTE
) {
1006 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1007 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1008 &nexthop
->rmap_src
.ipv6
, bytelen
);
1009 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1010 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
1011 &nexthop
->src
.ipv6
, bytelen
);
1014 if (IS_ZEBRA_DEBUG_KERNEL
)
1016 "netlink_route_multipath() (%s): "
1017 "nexthop via if %u",
1018 routedesc
, nexthop
->ifindex
);
1022 /* This function takes a nexthop as argument and
1023 * appends to the given rtattr/rtnexthop pair the
1024 * representation of the nexthop. If the nexthop
1025 * defines a preferred source, the src parameter
1026 * will be modified to point to that src, otherwise
1027 * it will be kept unmodified.
1029 * @param routedesc: Human readable description of route type
1030 * (direct/recursive, single-/multipath)
1031 * @param bytelen: Length of addresses in bytes.
1032 * @param nexthop: Nexthop information
1033 * @param rta: rtnetlink attribute structure
1034 * @param rtnh: pointer to an rtnetlink nexthop structure
1035 * @param src: pointer pointing to a location where
1036 * the prefsrc should be stored.
1038 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
1039 struct nexthop
*nexthop
,
1041 struct rtnexthop
*rtnh
,
1042 struct rtmsg
*rtmsg
,
1045 struct mpls_label_stack
*nh_label
;
1046 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1048 char label_buf
[256];
1050 rtnh
->rtnh_len
= sizeof(*rtnh
);
1051 rtnh
->rtnh_flags
= 0;
1052 rtnh
->rtnh_hops
= 0;
1053 rta
->rta_len
+= rtnh
->rtnh_len
;
1056 * label_buf is *only* currently used within debugging.
1057 * As such when we assign it we are guarding it inside
1058 * a debug test. If you want to change this make sure
1059 * you fix this assumption
1061 label_buf
[0] = '\0';
1064 for (struct nexthop
*nh
= nexthop
; nh
; nh
= nh
->rparent
) {
1065 char label_buf1
[20];
1067 nh_label
= nh
->nh_label
;
1068 if (!nh_label
|| !nh_label
->num_labels
)
1071 for (int i
= 0; i
< nh_label
->num_labels
; i
++) {
1072 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1075 if (IS_ZEBRA_DEBUG_KERNEL
) {
1077 sprintf(label_buf
, "label %u",
1078 nh_label
->label
[i
]);
1080 sprintf(label_buf1
, "/%u",
1081 nh_label
->label
[i
]);
1082 strlcat(label_buf
, label_buf1
,
1087 out_lse
[num_labels
] =
1088 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1094 /* Set the BoS bit */
1095 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1097 if (rtmsg
->rtm_family
== AF_MPLS
) {
1098 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
1100 num_labels
* sizeof(mpls_lse_t
));
1102 RTA_LENGTH(num_labels
* sizeof(mpls_lse_t
));
1104 struct rtattr
*nest
;
1105 u_int16_t encap
= LWTUNNEL_ENCAP_MPLS
;
1106 int len
= rta
->rta_len
;
1108 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP_TYPE
,
1109 &encap
, sizeof(u_int16_t
));
1110 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
, RTA_ENCAP
);
1111 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, MPLS_IPTUNNEL_DST
,
1113 num_labels
* sizeof(mpls_lse_t
));
1114 rta_nest_end(rta
, nest
);
1115 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1119 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1120 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1122 if (rtmsg
->rtm_family
== AF_INET
1123 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1124 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1126 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1127 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1129 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1130 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1132 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1133 *src
= &nexthop
->rmap_src
;
1134 else if (nexthop
->src
.ipv4
.s_addr
)
1135 *src
= &nexthop
->src
;
1137 if (IS_ZEBRA_DEBUG_KERNEL
)
1139 " 5549: netlink_route_build_multipath() (%s): "
1140 "nexthop via %s %s if %u",
1141 routedesc
, ipv4_ll_buf
, label_buf
,
1146 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1147 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1148 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1149 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1151 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1152 *src
= &nexthop
->rmap_src
;
1153 else if (nexthop
->src
.ipv4
.s_addr
)
1154 *src
= &nexthop
->src
;
1156 if (IS_ZEBRA_DEBUG_KERNEL
)
1158 "netlink_route_multipath() (%s): "
1159 "nexthop via %s %s if %u",
1160 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1161 label_buf
, nexthop
->ifindex
);
1163 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1164 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1165 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1166 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1169 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1170 *src
= &nexthop
->rmap_src
;
1171 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1172 *src
= &nexthop
->src
;
1174 if (IS_ZEBRA_DEBUG_KERNEL
)
1176 "netlink_route_multipath() (%s): "
1177 "nexthop via %s %s if %u",
1178 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1179 label_buf
, nexthop
->ifindex
);
1183 * We have figured out the ifindex so we should always send it
1184 * This is especially useful if we are doing route
1187 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1188 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1191 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1192 || nexthop
->type
== NEXTHOP_TYPE_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 "netlink_route_multipath() (%s): "
1201 "nexthop via if %u",
1202 routedesc
, nexthop
->ifindex
);
1203 } else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1204 if (IS_ZEBRA_DEBUG_KERNEL
)
1206 "netlink_route_multipath() (%s): "
1207 "nexthop via if %u",
1208 routedesc
, nexthop
->ifindex
);
1210 rtnh
->rtnh_ifindex
= 0;
1214 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1215 zebra_nhlfe_t
*nhlfe
,
1216 struct nlmsghdr
*nlmsg
,
1217 struct rtmsg
*rtmsg
,
1218 size_t req_size
, int cmd
)
1223 family
= NHLFE_FAMILY(nhlfe
);
1224 bytelen
= (family
== AF_INET
? 4 : 16);
1225 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1226 nlmsg
, rtmsg
, req_size
, cmd
);
1231 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1232 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1233 struct rtmsg
*rtmsg
, union g_addr
**src
)
1238 family
= NHLFE_FAMILY(nhlfe
);
1239 bytelen
= (family
== AF_INET
? 4 : 16);
1240 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1245 /* Log debug information for netlink_route_multipath
1246 * if debug logging is enabled.
1248 * @param cmd: Netlink command which is to be processed
1249 * @param p: Prefix for which the change is due
1250 * @param nexthop: Nexthop which is currently processed
1251 * @param routedesc: Semantic annotation for nexthop
1252 * (recursive, multipath, etc.)
1253 * @param family: Address family which the change concerns
1255 static void _netlink_route_debug(int cmd
, struct prefix
*p
,
1256 struct nexthop
*nexthop
, const char *routedesc
,
1257 int family
, struct zebra_vrf
*zvrf
)
1259 if (IS_ZEBRA_DEBUG_KERNEL
) {
1260 char buf
[PREFIX_STRLEN
];
1262 "netlink_route_multipath() (%s): %s %s vrf %u type %s",
1263 routedesc
, nl_msg_type_to_str(cmd
),
1264 prefix2str(p
, buf
, sizeof(buf
)), zvrf_id(zvrf
),
1265 (nexthop
) ? nexthop_type_to_str(nexthop
->type
) : "UNK");
1269 static void _netlink_mpls_debug(int cmd
, u_int32_t label
, const char *routedesc
)
1271 if (IS_ZEBRA_DEBUG_KERNEL
)
1272 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1273 nl_msg_type_to_str(cmd
), label
);
1276 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1285 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1287 memset(&req
.n
, 0, sizeof(req
.n
));
1288 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1290 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1291 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1292 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1293 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1295 req
.ndm
.ndm_family
= AF_INET
;
1296 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1297 req
.ndm
.ndm_ifindex
= ifindex
;
1298 req
.ndm
.ndm_type
= RTN_UNICAST
;
1300 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1301 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1303 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1307 /* Routing table change via netlink interface. */
1308 /* Update flag indicates whether this is a "replace" or not. */
1309 static int netlink_route_multipath(int cmd
, struct prefix
*p
,
1310 struct prefix
*src_p
, struct route_entry
*re
,
1314 struct sockaddr_nl snl
;
1315 struct nexthop
*nexthop
= NULL
;
1316 unsigned int nexthop_num
;
1318 int family
= PREFIX_FAMILY(p
);
1319 const char *routedesc
;
1326 char buf
[NL_PKT_BUF_SIZE
];
1329 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1330 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1332 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1334 bytelen
= (family
== AF_INET
? 4 : 16);
1336 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1337 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1338 if ((cmd
== RTM_NEWROUTE
) && update
)
1339 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1340 req
.n
.nlmsg_type
= cmd
;
1341 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1343 req
.r
.rtm_family
= family
;
1344 req
.r
.rtm_dst_len
= p
->prefixlen
;
1345 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1346 req
.r
.rtm_protocol
= zebra2proto(re
->type
);
1347 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1348 req
.r
.rtm_type
= RTN_UNICAST
;
1350 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1352 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1356 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1358 * either by the kernel or by zebra. Its purely for calculating best
1360 * by the routing protocol and for communicating with protocol peers.
1362 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1363 #if defined(SUPPORT_REALMS)
1364 if (re
->tag
> 0 && re
->tag
<= 255)
1365 addattr32(&req
.n
, sizeof req
, RTA_FLOW
, re
->tag
);
1367 /* Table corresponding to this route. */
1368 if (re
->table
< 256)
1369 req
.r
.rtm_table
= re
->table
;
1371 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1372 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1378 if (re
->mtu
|| re
->nexthop_mtu
) {
1379 char buf
[NL_PKT_BUF_SIZE
];
1380 struct rtattr
*rta
= (void *)buf
;
1381 u_int32_t mtu
= re
->mtu
;
1382 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1383 mtu
= re
->nexthop_mtu
;
1384 rta
->rta_type
= RTA_METRICS
;
1385 rta
->rta_len
= RTA_LENGTH(0);
1386 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1387 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1391 /* Count overall nexthops so we can decide whether to use singlepath
1392 * or multipath case. */
1394 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1395 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1397 if (cmd
== RTM_NEWROUTE
1398 && !NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1400 if (cmd
== RTM_DELROUTE
1401 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1407 /* Singlepath case. */
1408 if (nexthop_num
== 1 || multipath_num
== 1) {
1410 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1412 * So we want to cover 2 types of blackhole
1414 * 1) A normal blackhole route( ala from a static
1416 * 2) A recursively resolved blackhole route
1418 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1419 switch (nexthop
->bh_type
) {
1420 case BLACKHOLE_ADMINPROHIB
:
1421 req
.r
.rtm_type
= RTN_PROHIBIT
;
1423 case BLACKHOLE_REJECT
:
1424 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1427 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1432 if (CHECK_FLAG(nexthop
->flags
,
1433 NEXTHOP_FLAG_RECURSIVE
)) {
1435 if (family
== AF_INET
) {
1436 if (nexthop
->rmap_src
.ipv4
1443 } else if (nexthop
->src
.ipv4
1451 } else if (family
== AF_INET6
) {
1452 if (!IN6_IS_ADDR_UNSPECIFIED(
1460 !IN6_IS_ADDR_UNSPECIFIED(
1473 if ((cmd
== RTM_NEWROUTE
1474 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1475 || (cmd
== RTM_DELROUTE
1476 && CHECK_FLAG(nexthop
->flags
,
1477 NEXTHOP_FLAG_FIB
))) {
1478 routedesc
= nexthop
->rparent
1479 ? "recursive, single-path"
1482 _netlink_route_debug(cmd
, p
, nexthop
, routedesc
,
1484 _netlink_route_build_singlepath(
1485 routedesc
, bytelen
, nexthop
, &req
.n
,
1486 &req
.r
, sizeof req
, cmd
);
1491 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1492 if (family
== AF_INET
)
1493 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1494 &src
.ipv4
, bytelen
);
1495 else if (family
== AF_INET6
)
1496 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1497 &src
.ipv6
, bytelen
);
1500 char buf
[NL_PKT_BUF_SIZE
];
1501 struct rtattr
*rta
= (void *)buf
;
1502 struct rtnexthop
*rtnh
;
1503 union g_addr
*src1
= NULL
;
1505 rta
->rta_type
= RTA_MULTIPATH
;
1506 rta
->rta_len
= RTA_LENGTH(0);
1507 rtnh
= RTA_DATA(rta
);
1510 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1511 if (nexthop_num
>= multipath_num
)
1514 if (CHECK_FLAG(nexthop
->flags
,
1515 NEXTHOP_FLAG_RECURSIVE
)) {
1516 /* This only works for IPv4 now */
1518 if (family
== AF_INET
) {
1519 if (nexthop
->rmap_src
.ipv4
1526 } else if (nexthop
->src
.ipv4
1534 } else if (family
== AF_INET6
) {
1535 if (!IN6_IS_ADDR_UNSPECIFIED(
1543 !IN6_IS_ADDR_UNSPECIFIED(
1556 if ((cmd
== RTM_NEWROUTE
1557 && NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1558 || (cmd
== RTM_DELROUTE
1559 && CHECK_FLAG(nexthop
->flags
,
1560 NEXTHOP_FLAG_FIB
))) {
1561 routedesc
= nexthop
->rparent
1562 ? "recursive, multipath"
1566 _netlink_route_debug(cmd
, p
, nexthop
, routedesc
,
1568 _netlink_route_build_multipath(
1569 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1571 rtnh
= RTNH_NEXT(rtnh
);
1573 if (!setsrc
&& src1
) {
1574 if (family
== AF_INET
)
1575 src
.ipv4
= src1
->ipv4
;
1576 else if (family
== AF_INET6
)
1577 src
.ipv6
= src1
->ipv6
;
1583 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1584 if (family
== AF_INET
)
1585 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1586 &src
.ipv4
, bytelen
);
1587 else if (family
== AF_INET6
)
1588 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1589 &src
.ipv6
, bytelen
);
1590 if (IS_ZEBRA_DEBUG_KERNEL
)
1591 zlog_debug("Setting source");
1594 if (rta
->rta_len
> RTA_LENGTH(0))
1595 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1596 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1599 /* If there is no useful nexthop then return. */
1600 if (nexthop_num
== 0) {
1601 if (IS_ZEBRA_DEBUG_KERNEL
)
1603 "netlink_route_multipath(): No useful nexthop.");
1609 /* Destination netlink address. */
1610 memset(&snl
, 0, sizeof snl
);
1611 snl
.nl_family
= AF_NETLINK
;
1613 /* Talk to netlink socket. */
1614 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1618 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1621 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1629 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1631 memset(&req
.n
, 0, sizeof(req
.n
));
1632 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1634 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1635 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1636 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1638 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1639 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1641 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1642 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1643 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1644 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1645 addattr_l(&req
.n
, sizeof(req
), RTA_TABLE
, &zvrf
->table_id
, 4);
1647 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1648 &zns
->netlink_cmd
, zns
, 0);
1654 void kernel_route_rib(struct route_node
*rn
, struct prefix
*p
,
1655 struct prefix
*src_p
, struct route_entry
*old
,
1656 struct route_entry
*new)
1663 if (p
->family
== AF_INET
)
1664 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
,
1665 new, (old
) ? 1 : 0);
1668 * So v6 route replace semantics are not in
1669 * the kernel at this point as I understand it.
1670 * So let's do a delete than an add.
1671 * In the future once v6 route replace semantics
1672 * are in we can figure out what to do here to
1673 * allow working with old and new kernels.
1675 * I'm also intentionally ignoring the failure case
1676 * of the route delete. If that happens yeah we're
1680 netlink_route_multipath(RTM_DELROUTE
, p
,
1682 ret
= netlink_route_multipath(RTM_NEWROUTE
, p
,
1685 kernel_route_rib_pass_fail(rn
, p
, new,
1687 SOUTHBOUND_INSTALL_SUCCESS
:
1688 SOUTHBOUND_INSTALL_FAILURE
);
1693 ret
= netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1695 kernel_route_rib_pass_fail(rn
, p
, old
,
1697 SOUTHBOUND_DELETE_SUCCESS
:
1698 SOUTHBOUND_DELETE_FAILURE
);
1702 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1705 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1710 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1711 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1713 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1714 struct in_addr
*vtep_ip
, int cmd
)
1716 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1722 u_char dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1724 memset(&req
.n
, 0, sizeof(req
.n
));
1725 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1727 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1728 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1729 if (cmd
== RTM_NEWNEIGH
)
1730 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1731 req
.n
.nlmsg_type
= cmd
;
1732 req
.ndm
.ndm_family
= PF_BRIDGE
;
1733 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1734 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1737 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1738 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1739 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1741 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1746 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1748 * a "flood" MAC FDB entry.
1750 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1752 if (IS_ZEBRA_DEBUG_VXLAN
)
1753 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1754 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1756 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1760 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1761 * deleting the "flood" MAC FDB entry.
1763 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1765 if (IS_ZEBRA_DEBUG_VXLAN
)
1767 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1768 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1770 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1774 #define NDA_RTA(r) \
1775 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1778 static int netlink_macfdb_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1782 struct interface
*ifp
;
1783 struct zebra_if
*zif
;
1784 struct rtattr
*tb
[NDA_MAX
+ 1];
1785 struct interface
*br_if
;
1788 struct prefix vtep_ip
;
1789 int vid_present
= 0, dst_present
= 0;
1790 char buf
[ETHER_ADDR_STRLEN
];
1795 ndm
= NLMSG_DATA(h
);
1797 /* We only process macfdb notifications if EVPN is enabled */
1798 if (!is_evpn_enabled())
1801 /* The interface should exist. */
1802 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
1804 if (!ifp
|| !ifp
->info
)
1807 /* The interface should be something we're interested in. */
1808 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1811 /* Drop "permanent" entries. */
1812 if (ndm
->ndm_state
& NUD_PERMANENT
)
1815 zif
= (struct zebra_if
*)ifp
->info
;
1816 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1817 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1818 nl_msg_type_to_str(h
->nlmsg_type
),
1819 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1820 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1824 /* Parse attributes and extract fields of interest. */
1825 memset(tb
, 0, sizeof tb
);
1826 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1828 if (!tb
[NDA_LLADDR
]) {
1829 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1830 nl_msg_type_to_str(h
->nlmsg_type
),
1831 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1832 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1836 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
1838 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
1839 nl_msg_type_to_str(h
->nlmsg_type
),
1840 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1841 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1842 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1846 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
1848 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1850 vid
= *(u_int16_t
*)RTA_DATA(tb
[NDA_VLAN
]);
1851 sprintf(vid_buf
, " VLAN %u", vid
);
1855 /* TODO: Only IPv4 supported now. */
1857 vtep_ip
.family
= AF_INET
;
1858 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1859 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1861 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1864 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1866 if (IS_ZEBRA_DEBUG_KERNEL
)
1867 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1868 nl_msg_type_to_str(h
->nlmsg_type
),
1869 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1870 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1871 sticky
? "sticky " : "",
1872 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1873 dst_present
? dst_buf
: "");
1875 if (filter_vlan
&& vid
!= filter_vlan
)
1878 /* If add or update, do accordingly if learnt on a "local" interface; if
1879 * the notification is over VxLAN, this has to be related to
1881 * so perform an implicit delete of any local entry (if it exists).
1883 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1884 /* Drop "permanent" entries. */
1885 if (ndm
->ndm_state
& NUD_PERMANENT
)
1888 if (IS_ZEBRA_IF_VXLAN(ifp
))
1889 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1892 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1896 /* This is a delete notification.
1897 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1898 * 2. For a MAC over "local" interface, delete the mac
1899 * Note: We will get notifications from both bridge driver and VxLAN
1901 * Ignore the notification from VxLan driver as it is also generated
1902 * when mac moves from remote to local.
1907 if (IS_ZEBRA_IF_VXLAN(ifp
))
1908 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1911 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1914 static int netlink_macfdb_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1915 ns_id_t ns_id
, int startup
)
1920 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1923 /* Length validity. */
1924 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1928 /* We are interested only in AF_BRIDGE notifications. */
1929 ndm
= NLMSG_DATA(h
);
1930 if (ndm
->ndm_family
!= AF_BRIDGE
)
1933 return netlink_macfdb_change(snl
, h
, len
);
1936 /* Request for MAC FDB information from the kernel */
1937 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
1938 ifindex_t master_ifindex
)
1942 struct ifinfomsg ifm
;
1946 /* Form the request, specifying filter (rtattr) if needed. */
1947 memset(&req
, 0, sizeof(req
));
1948 req
.n
.nlmsg_type
= type
;
1949 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1950 req
.ifm
.ifi_family
= family
;
1952 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
1954 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
1958 * MAC forwarding database read using netlink interface. This is invoked
1961 int netlink_macfdb_read(struct zebra_ns
*zns
)
1965 /* Get bridge FDB table. */
1966 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
1969 /* We are reading entire table. */
1971 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
1978 * MAC forwarding database read using netlink interface. This is for a
1979 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
1981 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
1982 struct interface
*br_if
)
1984 struct zebra_if
*br_zif
;
1985 struct zebra_if
*zif
;
1986 struct zebra_l2info_vxlan
*vxl
;
1990 /* Save VLAN we're filtering on, if needed. */
1991 br_zif
= (struct zebra_if
*)br_if
->info
;
1992 zif
= (struct zebra_if
*)ifp
->info
;
1993 vxl
= &zif
->l2info
.vxl
;
1994 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
1995 filter_vlan
= vxl
->access_vlan
;
1997 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
1999 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
2003 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
2006 /* Reset VLAN filter. */
2011 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
2012 struct ethaddr
*mac
, struct in_addr vtep_ip
,
2013 int local
, int cmd
, u_char sticky
)
2015 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2022 struct zebra_if
*zif
;
2023 struct interface
*br_if
;
2024 struct zebra_if
*br_zif
;
2025 char buf
[ETHER_ADDR_STRLEN
];
2026 int vid_present
= 0, dst_present
= 0;
2031 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2032 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
2033 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
2038 memset(&req
.n
, 0, sizeof(req
.n
));
2039 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2041 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2042 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2043 if (cmd
== RTM_NEWNEIGH
)
2044 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2045 req
.n
.nlmsg_type
= cmd
;
2046 req
.ndm
.ndm_family
= AF_BRIDGE
;
2047 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
2048 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2051 req
.ndm
.ndm_state
|= NUD_NOARP
;
2053 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
2055 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2056 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2058 dst_alen
= 4; // TODO: hardcoded
2059 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
2061 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
2063 br_zif
= (struct zebra_if
*)br_if
->info
;
2064 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
2065 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
2067 sprintf(vid_buf
, " VLAN %u", vid
);
2069 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
2071 if (IS_ZEBRA_DEBUG_KERNEL
)
2072 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2073 nl_msg_type_to_str(cmd
),
2074 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2075 ifp
->ifindex
, vid_present
? vid_buf
: "",
2076 sticky
? "sticky " : "",
2077 prefix_mac2str(mac
, buf
, sizeof(buf
)),
2078 dst_present
? dst_buf
: "");
2080 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2085 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2088 static int netlink_ipneigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2092 struct interface
*ifp
;
2093 struct zebra_if
*zif
;
2094 struct rtattr
*tb
[NDA_MAX
+ 1];
2095 struct interface
*link_if
;
2098 char buf
[ETHER_ADDR_STRLEN
];
2099 char buf2
[INET6_ADDRSTRLEN
];
2100 int mac_present
= 0;
2103 ndm
= NLMSG_DATA(h
);
2105 /* We only process neigh notifications if EVPN is enabled */
2106 if (!is_evpn_enabled())
2109 /* The interface should exist. */
2110 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
2112 if (!ifp
|| !ifp
->info
)
2115 /* Drop "permanent" entries. */
2116 if (ndm
->ndm_state
& NUD_PERMANENT
)
2119 zif
= (struct zebra_if
*)ifp
->info
;
2120 /* The neighbor is present on an SVI. From this, we locate the
2122 * bridge because we're only interested in neighbors on a VxLAN bridge.
2123 * The bridge is located based on the nature of the SVI:
2124 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2126 * and is linked to the bridge
2127 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2131 if (IS_ZEBRA_IF_VLAN(ifp
)) {
2132 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
2136 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2141 /* Parse attributes and extract fields of interest. */
2142 memset(tb
, 0, sizeof tb
);
2143 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2146 zlog_warn("%s family %s IF %s(%u) - no DST",
2147 nl_msg_type_to_str(h
->nlmsg_type
),
2148 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2152 memset(&mac
, 0, sizeof(struct ethaddr
));
2153 memset(&ip
, 0, sizeof(struct ipaddr
));
2154 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2155 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2157 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2158 if (tb
[NDA_LLADDR
]) {
2159 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2161 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2162 nl_msg_type_to_str(h
->nlmsg_type
),
2163 nl_family_to_str(ndm
->ndm_family
),
2164 ifp
->name
, ndm
->ndm_ifindex
,
2165 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2170 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2173 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2175 if (IS_ZEBRA_DEBUG_KERNEL
)
2177 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2178 nl_msg_type_to_str(h
->nlmsg_type
),
2179 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2181 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2183 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2185 ndm
->ndm_state
, ndm
->ndm_flags
);
2187 /* If the neighbor state is valid for use, process as an add or
2189 * else process as a delete. Note that the delete handling may
2191 * in re-adding the neighbor if it is a valid "remote" neighbor.
2193 if (ndm
->ndm_state
& NUD_VALID
)
2194 return zebra_vxlan_local_neigh_add_update(
2195 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2198 return zebra_vxlan_local_neigh_del(ifp
, link_if
, &ip
);
2201 if (IS_ZEBRA_DEBUG_KERNEL
)
2202 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2203 nl_msg_type_to_str(h
->nlmsg_type
),
2204 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2206 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2208 /* Process the delete - it may result in re-adding the neighbor if it is
2209 * a valid "remote" neighbor.
2211 return zebra_vxlan_local_neigh_del(ifp
, link_if
, &ip
);
2214 static int netlink_neigh_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2215 ns_id_t ns_id
, int startup
)
2220 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2223 /* Length validity. */
2224 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2228 /* We are interested only in AF_INET or AF_INET6 notifications. */
2229 ndm
= NLMSG_DATA(h
);
2230 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2233 return netlink_neigh_change(snl
, h
, len
);
2236 /* Request for IP neighbor information from the kernel */
2237 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2246 /* Form the request, specifying filter (rtattr) if needed. */
2247 memset(&req
, 0, sizeof(req
));
2248 req
.n
.nlmsg_type
= type
;
2249 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2250 req
.ndm
.ndm_family
= family
;
2252 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2254 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2258 * IP Neighbor table read using netlink interface. This is invoked
2261 int netlink_neigh_read(struct zebra_ns
*zns
)
2265 /* Get IP neighbor table. */
2266 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2269 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2276 * IP Neighbor table read using netlink interface. This is for a specific
2279 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2283 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2287 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2293 int netlink_neigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2299 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2302 /* Length validity. */
2303 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2307 /* Is this a notification for the MAC FDB or IP neighbor table? */
2308 ndm
= NLMSG_DATA(h
);
2309 if (ndm
->ndm_family
== AF_BRIDGE
)
2310 return netlink_macfdb_change(snl
, h
, len
);
2312 if (ndm
->ndm_type
!= RTN_UNICAST
)
2315 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2316 return netlink_ipneigh_change(snl
, h
, len
);
2321 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2322 struct ethaddr
*mac
, u_int32_t flags
, int cmd
)
2331 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2332 char buf
[INET6_ADDRSTRLEN
];
2333 char buf2
[ETHER_ADDR_STRLEN
];
2335 memset(&req
.n
, 0, sizeof(req
.n
));
2336 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2338 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2339 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2340 if (cmd
== RTM_NEWNEIGH
)
2341 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2342 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2343 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2344 req
.ndm
.ndm_state
= flags
;
2345 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2346 req
.ndm
.ndm_type
= RTN_UNICAST
;
2347 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2350 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2351 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2353 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2355 if (IS_ZEBRA_DEBUG_KERNEL
)
2356 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2357 nl_msg_type_to_str(cmd
),
2358 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2359 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2360 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2363 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2367 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2368 struct in_addr vtep_ip
, u_char sticky
)
2370 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2374 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2375 struct in_addr vtep_ip
, int local
)
2377 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2381 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2382 struct ethaddr
*mac
)
2384 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_REACHABLE
, RTM_NEWNEIGH
);
2387 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2389 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2393 * MPLS label forwarding table change via netlink interface.
2395 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2398 zebra_nhlfe_t
*nhlfe
;
2399 struct nexthop
*nexthop
= NULL
;
2400 unsigned int nexthop_num
;
2401 const char *routedesc
;
2402 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2408 char buf
[NL_PKT_BUF_SIZE
];
2411 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2414 * Count # nexthops so we can decide whether to use singlepath
2415 * or multipath case.
2418 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2419 nexthop
= nhlfe
->nexthop
;
2422 if (cmd
== RTM_NEWROUTE
) {
2423 /* Count all selected NHLFEs */
2424 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2425 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2429 /* Count all installed NHLFEs */
2430 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2431 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2436 if ((nexthop_num
== 0) || (!lsp
->best_nhlfe
&& (cmd
!= RTM_DELROUTE
)))
2439 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2440 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2441 req
.n
.nlmsg_type
= cmd
;
2442 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2444 req
.r
.rtm_family
= AF_MPLS
;
2445 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2446 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2447 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2448 req
.r
.rtm_type
= RTN_UNICAST
;
2450 if (cmd
== RTM_NEWROUTE
) {
2451 /* We do a replace to handle update. */
2452 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2454 /* set the protocol value if installing */
2455 route_type
= re_type_from_lsp_type(lsp
->best_nhlfe
->type
);
2456 req
.r
.rtm_protocol
= zebra2proto(route_type
);
2459 /* Fill destination */
2460 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2461 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2463 /* Fill nexthops (paths) based on single-path or multipath. The paths
2464 * chosen depend on the operation.
2466 if (nexthop_num
== 1 || multipath_num
== 1) {
2467 routedesc
= "single-path";
2468 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2471 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2472 nexthop
= nhlfe
->nexthop
;
2476 if ((cmd
== RTM_NEWROUTE
2477 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2478 && CHECK_FLAG(nexthop
->flags
,
2479 NEXTHOP_FLAG_ACTIVE
)))
2480 || (cmd
== RTM_DELROUTE
2481 && (CHECK_FLAG(nhlfe
->flags
,
2482 NHLFE_FLAG_INSTALLED
)
2483 && CHECK_FLAG(nexthop
->flags
,
2484 NEXTHOP_FLAG_FIB
)))) {
2485 /* Add the gateway */
2486 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2493 } else /* Multipath case */
2495 char buf
[NL_PKT_BUF_SIZE
];
2496 struct rtattr
*rta
= (void *)buf
;
2497 struct rtnexthop
*rtnh
;
2498 union g_addr
*src1
= NULL
;
2500 rta
->rta_type
= RTA_MULTIPATH
;
2501 rta
->rta_len
= RTA_LENGTH(0);
2502 rtnh
= RTA_DATA(rta
);
2504 routedesc
= "multipath";
2505 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2508 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2509 nexthop
= nhlfe
->nexthop
;
2513 if (nexthop_num
>= multipath_num
)
2516 if ((cmd
== RTM_NEWROUTE
2517 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2518 && CHECK_FLAG(nexthop
->flags
,
2519 NEXTHOP_FLAG_ACTIVE
)))
2520 || (cmd
== RTM_DELROUTE
2521 && (CHECK_FLAG(nhlfe
->flags
,
2522 NHLFE_FLAG_INSTALLED
)
2523 && CHECK_FLAG(nexthop
->flags
,
2524 NEXTHOP_FLAG_FIB
)))) {
2527 /* Build the multipath */
2528 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2531 rtnh
= RTNH_NEXT(rtnh
);
2535 /* Add the multipath */
2536 if (rta
->rta_len
> RTA_LENGTH(0))
2537 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2538 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2541 /* Talk to netlink socket. */
2542 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2545 #endif /* HAVE_NETLINK */