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
22 #include <net/if_arp.h>
24 /* Hack for GNU libc version 2. */
26 #define MSG_TRUNC 0x20
27 #endif /* MSG_TRUNC */
33 #include "connected.h"
36 #include "zebra_memory.h"
46 #include "zebra/zserv.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/zebra_vrf.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/interface.h"
52 #include "zebra/debug.h"
53 #include "zebra/rtadv.h"
54 #include "zebra/zebra_ptm.h"
55 #include "zebra/zebra_mpls.h"
56 #include "zebra/kernel_netlink.h"
57 #include "zebra/rt_netlink.h"
58 #include "zebra/zebra_mroute.h"
59 #include "zebra/zebra_vxlan.h"
62 /* TODO - Temporary definitions, need to refine. */
75 #ifndef RTA_ENCAP_TYPE
76 #define RTA_ENCAP_TYPE 21
84 #define RTA_EXPIRES 23
87 #ifndef LWTUNNEL_ENCAP_MPLS
88 #define LWTUNNEL_ENCAP_MPLS 1
91 #ifndef MPLS_IPTUNNEL_DST
92 #define MPLS_IPTUNNEL_DST 1
100 #define NTF_MASTER 0x04
104 #define NTF_SELF 0x02
107 #ifndef NTF_EXT_LEARNED
108 #define NTF_EXT_LEARNED 0x10
112 #define NDA_IFINDEX 8
118 /* End of temporary definitions */
120 static vlanid_t filter_vlan
= 0;
128 char ipv4_ll_buf
[16] = "169.254.0.1";
129 struct in_addr ipv4_ll
;
132 * The ipv4_ll data structure is used for all 5549
133 * additions to the kernel. Let's figure out the
134 * correct value one time instead for every
135 * install/remove of a 5549 type route
137 void rt_netlink_init(void)
139 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
142 static inline int is_selfroute(int proto
)
144 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
145 || (proto
== RTPROT_STATIC
) || (proto
== RTPROT_ZEBRA
)
146 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
147 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
148 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)) {
155 static inline int get_rt_proto(int proto
)
158 case ZEBRA_ROUTE_BABEL
:
159 proto
= RTPROT_BABEL
;
161 case ZEBRA_ROUTE_BGP
:
164 case ZEBRA_ROUTE_OSPF
:
165 case ZEBRA_ROUTE_OSPF6
:
168 case ZEBRA_ROUTE_STATIC
:
169 proto
= RTPROT_STATIC
;
171 case ZEBRA_ROUTE_ISIS
:
174 case ZEBRA_ROUTE_RIP
:
177 case ZEBRA_ROUTE_RIPNG
:
178 proto
= RTPROT_RIPNG
;
180 case ZEBRA_ROUTE_NHRP
:
183 case ZEBRA_ROUTE_EIGRP
:
184 proto
= RTPROT_EIGRP
;
186 case ZEBRA_ROUTE_LDP
:
190 proto
= RTPROT_ZEBRA
;
198 Pending: create an efficient table_id (in a tree/hash) based lookup)
200 static vrf_id_t
vrf_lookup_by_table(u_int32_t table_id
)
203 struct zebra_vrf
*zvrf
;
205 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
207 if ((zvrf
= vrf
->info
) == NULL
|| (zvrf
->table_id
!= table_id
))
210 return zvrf_id(zvrf
);
216 /* Looking up routing table by netlink interface. */
217 static int netlink_route_change_read_unicast(struct sockaddr_nl
*snl
,
218 struct nlmsghdr
*h
, ns_id_t ns_id
,
223 struct rtattr
*tb
[RTA_MAX
+ 1];
226 struct prefix_ipv6 src_p
;
227 vrf_id_t vrf_id
= VRF_DEFAULT
;
229 char anyaddr
[16] = {0};
238 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
239 void *src
= NULL
; /* IPv6 srcdest source prefix */
243 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
245 if (startup
&& rtm
->rtm_type
!= RTN_UNICAST
)
248 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
252 memset(tb
, 0, sizeof tb
);
253 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
255 if (rtm
->rtm_flags
& RTM_F_CLONED
)
257 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
259 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
262 if (!startup
&& is_selfroute(rtm
->rtm_protocol
)
263 && h
->nlmsg_type
== RTM_NEWROUTE
)
266 /* We don't care about change notifications for the MPLS table. */
267 /* TODO: Revisit this. */
268 if (rtm
->rtm_family
== AF_MPLS
)
271 /* Table corresponding to route. */
273 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
275 table
= rtm
->rtm_table
;
278 vrf_id
= vrf_lookup_by_table(table
);
279 if (vrf_id
== VRF_DEFAULT
) {
280 if (!is_zebra_valid_kernel_table(table
)
281 && !is_zebra_main_routing_table(table
))
285 /* Route which inserted by Zebra. */
286 if (is_selfroute(rtm
->rtm_protocol
))
287 flags
|= ZEBRA_FLAG_SELFROUTE
;
290 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
293 dest
= RTA_DATA(tb
[RTA_DST
]);
298 src
= RTA_DATA(tb
[RTA_SRC
]);
303 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
306 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
308 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
309 if (tb
[RTA_PRIORITY
])
310 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
312 if (tb
[RTA_METRICS
]) {
313 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
315 memset(mxrta
, 0, sizeof mxrta
);
316 netlink_parse_rtattr(mxrta
, RTAX_MAX
,
317 RTA_DATA(tb
[RTA_METRICS
]),
318 RTA_PAYLOAD(tb
[RTA_METRICS
]));
321 mtu
= *(u_int32_t
*)RTA_DATA(mxrta
[RTAX_MTU
]);
325 if (rtm
->rtm_family
== AF_INET
) {
327 memcpy(&p
.u
.prefix4
, dest
, 4);
328 p
.prefixlen
= rtm
->rtm_dst_len
;
331 0; // Forces debug below to not display anything
332 } else if (rtm
->rtm_family
== AF_INET6
) {
334 memcpy(&p
.u
.prefix6
, dest
, 16);
335 p
.prefixlen
= rtm
->rtm_dst_len
;
337 src_p
.family
= AF_INET6
;
338 memcpy(&src_p
.prefix
, src
, 16);
339 src_p
.prefixlen
= rtm
->rtm_src_len
;
342 if (rtm
->rtm_src_len
!= 0) {
343 char buf
[PREFIX_STRLEN
];
345 "unsupported IPv[4|6] sourcedest route (dest %s vrf %u)",
346 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
350 if (IS_ZEBRA_DEBUG_KERNEL
) {
351 char buf
[PREFIX_STRLEN
];
352 char buf2
[PREFIX_STRLEN
];
354 "%s %s%s%s vrf %u", nl_msg_type_to_str(h
->nlmsg_type
),
355 prefix2str(&p
, buf
, sizeof(buf
)),
356 src_p
.prefixlen
? " from " : "",
357 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
363 if (rtm
->rtm_family
== AF_INET6
)
366 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
367 if (!tb
[RTA_MULTIPATH
])
368 rib_add(afi
, SAFI_UNICAST
, vrf_id
, ZEBRA_ROUTE_KERNEL
,
369 0, flags
, &p
, NULL
, gate
, prefsrc
, index
, table
,
372 /* This is a multipath route */
374 struct route_entry
*re
;
375 struct rtnexthop
*rtnh
=
376 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
378 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
380 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
381 re
->type
= ZEBRA_ROUTE_KERNEL
;
389 re
->uptime
= time(NULL
);
392 if (len
< (int)sizeof(*rtnh
)
393 || rtnh
->rtnh_len
> len
)
396 index
= rtnh
->rtnh_ifindex
;
398 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
399 memset(tb
, 0, sizeof(tb
));
400 netlink_parse_rtattr(
401 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
402 rtnh
->rtnh_len
- sizeof(*rtnh
));
409 if (rtm
->rtm_family
== AF_INET
) {
411 route_entry_nexthop_ipv4_ifindex_add(
415 route_entry_nexthop_ipv4_add(
418 } else if (rtm
->rtm_family
421 route_entry_nexthop_ipv6_ifindex_add(
425 route_entry_nexthop_ipv6_add(
429 route_entry_nexthop_ifindex_add(re
,
432 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
433 rtnh
= RTNH_NEXT(rtnh
);
436 zserv_nexthop_num_warn(__func__
,
437 (const struct prefix
*)&p
,
439 if (re
->nexthop_num
== 0)
442 rib_add_multipath(AFI_IP
, SAFI_UNICAST
, &p
,
446 if (!tb
[RTA_MULTIPATH
])
447 rib_delete(afi
, SAFI_UNICAST
, vrf_id
,
448 ZEBRA_ROUTE_KERNEL
, 0, flags
, &p
, NULL
, gate
,
451 struct rtnexthop
*rtnh
=
452 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
454 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
457 if (len
< (int)sizeof(*rtnh
)
458 || rtnh
->rtnh_len
> len
)
462 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
463 memset(tb
, 0, sizeof(tb
));
464 netlink_parse_rtattr(
465 tb
, RTA_MAX
, RTNH_DATA(rtnh
),
466 rtnh
->rtnh_len
- sizeof(*rtnh
));
473 rib_delete(afi
, SAFI_UNICAST
, vrf_id
,
474 ZEBRA_ROUTE_KERNEL
, 0, flags
,
475 &p
, NULL
, gate
, index
,
478 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
479 rtnh
= RTNH_NEXT(rtnh
);
487 static struct mcast_route_data
*mroute
= NULL
;
489 static int netlink_route_change_read_multicast(struct sockaddr_nl
*snl
,
491 ns_id_t ns_id
, int startup
)
495 struct rtattr
*tb
[RTA_MAX
+ 1];
496 struct mcast_route_data
*m
;
497 struct mcast_route_data mr
;
504 char oif_list
[256] = "\0";
505 vrf_id_t vrf
= ns_id
;
510 memset(&mr
, 0, sizeof(mr
));
516 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
518 memset(tb
, 0, sizeof tb
);
519 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
522 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
525 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
528 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
530 if ((RTA_EXPIRES
<= RTA_MAX
) && tb
[RTA_EXPIRES
])
531 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
533 if (tb
[RTA_MULTIPATH
]) {
534 struct rtnexthop
*rtnh
=
535 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
537 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
539 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
542 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
545 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
546 rtnh
= RTNH_NEXT(rtnh
);
550 if (IS_ZEBRA_DEBUG_KERNEL
) {
551 struct interface
*ifp
;
552 strcpy(sbuf
, inet_ntoa(m
->sg
.src
));
553 strcpy(gbuf
, inet_ntoa(m
->sg
.grp
));
554 for (count
= 0; count
< oif_count
; count
++) {
555 ifp
= if_lookup_by_index(oif
[count
], vrf
);
558 sprintf(temp
, "%s ", ifp
->name
);
559 strcat(oif_list
, temp
);
561 ifp
= if_lookup_by_index(iif
, vrf
);
562 zlog_debug("MCAST %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
563 nl_msg_type_to_str(h
->nlmsg_type
), sbuf
, gbuf
,
564 ifp
->name
, oif_list
, m
->lastused
);
569 int netlink_route_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
570 ns_id_t ns_id
, int startup
)
573 vrf_id_t vrf_id
= ns_id
;
578 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
579 /* If this is not route add/delete message print warning. */
580 zlog_warn("Kernel message: %d vrf %u\n", h
->nlmsg_type
, vrf_id
);
584 /* Connected route. */
585 if (IS_ZEBRA_DEBUG_KERNEL
)
586 zlog_debug("%s %s %s proto %s vrf %u",
587 nl_msg_type_to_str(h
->nlmsg_type
),
588 nl_family_to_str(rtm
->rtm_family
),
589 nl_rttype_to_str(rtm
->rtm_type
),
590 nl_rtproto_to_str(rtm
->rtm_protocol
), vrf_id
);
592 /* We don't care about change notifications for the MPLS table. */
593 /* TODO: Revisit this. */
594 if (rtm
->rtm_family
== AF_MPLS
)
597 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
601 switch (rtm
->rtm_type
) {
603 netlink_route_change_read_unicast(snl
, h
, ns_id
, startup
);
606 netlink_route_change_read_multicast(snl
, h
, ns_id
, startup
);
616 /* Request for specific route information from the kernel */
617 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
624 /* Form the request, specifying filter (rtattr) if needed. */
625 memset(&req
, 0, sizeof(req
));
626 req
.n
.nlmsg_type
= type
;
627 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
628 req
.rtm
.rtm_family
= family
;
630 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
633 /* Routing table read function using netlink interface. Only called
635 int netlink_route_read(struct zebra_ns
*zns
)
639 /* Get IPv4 routing table. */
640 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
643 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
644 &zns
->netlink_cmd
, zns
, 0, 1);
648 /* Get IPv6 routing table. */
649 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
652 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
653 &zns
->netlink_cmd
, zns
, 0, 1);
660 static void _netlink_route_nl_add_gateway_info(u_char route_family
,
662 struct nlmsghdr
*nlmsg
,
663 size_t req_size
, int bytelen
,
664 struct nexthop
*nexthop
)
666 if (route_family
== AF_MPLS
) {
667 struct gw_family_t gw_fam
;
669 gw_fam
.family
= gw_family
;
670 if (gw_family
== AF_INET
)
671 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
673 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
674 addattr_l(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
677 if (gw_family
== AF_INET
)
678 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
679 &nexthop
->gate
.ipv4
, bytelen
);
681 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
,
682 &nexthop
->gate
.ipv6
, bytelen
);
686 static void _netlink_route_rta_add_gateway_info(u_char route_family
,
689 struct rtnexthop
*rtnh
,
690 size_t req_size
, int bytelen
,
691 struct nexthop
*nexthop
)
693 if (route_family
== AF_MPLS
) {
694 struct gw_family_t gw_fam
;
696 gw_fam
.family
= gw_family
;
697 if (gw_family
== AF_INET
)
698 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
700 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
701 rta_addattr_l(rta
, req_size
, RTA_VIA
, &gw_fam
.family
,
703 rtnh
->rtnh_len
+= RTA_LENGTH(bytelen
+ 2);
705 if (gw_family
== AF_INET
)
706 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
707 &nexthop
->gate
.ipv4
, bytelen
);
709 rta_addattr_l(rta
, req_size
, RTA_GATEWAY
,
710 &nexthop
->gate
.ipv6
, bytelen
);
711 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
715 /* This function takes a nexthop as argument and adds
716 * the appropriate netlink attributes to an existing
719 * @param routedesc: Human readable description of route type
720 * (direct/recursive, single-/multipath)
721 * @param bytelen: Length of addresses in bytes.
722 * @param nexthop: Nexthop information
723 * @param nlmsg: nlmsghdr structure to fill in.
724 * @param req_size: The size allocated for the message.
726 static void _netlink_route_build_singlepath(const char *routedesc
, int bytelen
,
727 struct nexthop
*nexthop
,
728 struct nlmsghdr
*nlmsg
,
730 size_t req_size
, int cmd
)
732 struct nexthop_label
*nh_label
;
733 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
737 * label_buf is *only* currently used within debugging.
738 * As such when we assign it we are guarding it inside
739 * a debug test. If you want to change this make sure
740 * you fix this assumption
743 /* outgoing label - either as NEWDST (in the case of LSR) or as ENCAP
744 * (in the case of LER)
746 nh_label
= nexthop
->nh_label
;
747 if (rtmsg
->rtm_family
== AF_MPLS
) {
749 assert(nh_label
->num_labels
== 1);
752 if (nh_label
&& nh_label
->num_labels
) {
753 int i
, num_labels
= 0;
757 for (i
= 0; i
< nh_label
->num_labels
; i
++) {
758 if (nh_label
->label
[i
] != MPLS_IMP_NULL_LABEL
) {
759 bos
= ((i
== (nh_label
->num_labels
- 1)) ? 1
761 out_lse
[i
] = mpls_lse_encode(nh_label
->label
[i
],
763 if (IS_ZEBRA_DEBUG_KERNEL
) {
765 sprintf(label_buf
, "label %d",
768 sprintf(label_buf1
, "/%d",
770 strcat(label_buf
, label_buf1
);
777 if (rtmsg
->rtm_family
== AF_MPLS
)
778 addattr_l(nlmsg
, req_size
, RTA_NEWDST
, &out_lse
,
779 num_labels
* sizeof(mpls_lse_t
));
782 u_int16_t encap
= LWTUNNEL_ENCAP_MPLS
;
784 addattr_l(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
785 &encap
, sizeof(u_int16_t
));
786 nest
= addattr_nest(nlmsg
, req_size
, RTA_ENCAP
);
787 addattr_l(nlmsg
, req_size
, MPLS_IPTUNNEL_DST
,
789 num_labels
* sizeof(mpls_lse_t
));
790 addattr_nest_end(nlmsg
, nest
);
795 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
796 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
798 if (rtmsg
->rtm_family
== AF_INET
799 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
800 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
801 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
802 addattr_l(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
803 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
805 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
806 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
807 &nexthop
->rmap_src
.ipv4
, bytelen
);
808 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
809 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
810 &nexthop
->src
.ipv4
, bytelen
);
812 if (IS_ZEBRA_DEBUG_KERNEL
)
814 " 5549: _netlink_route_build_singlepath() (%s): "
815 "nexthop via %s %s if %u",
816 routedesc
, ipv4_ll_buf
, label_buf
,
821 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
822 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
823 /* Send deletes to the kernel without specifying the next-hop */
824 if (cmd
!= RTM_DELROUTE
)
825 _netlink_route_nl_add_gateway_info(
826 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
829 if (cmd
== RTM_NEWROUTE
) {
830 if (nexthop
->rmap_src
.ipv4
.s_addr
)
831 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
832 &nexthop
->rmap_src
.ipv4
, bytelen
);
833 else if (nexthop
->src
.ipv4
.s_addr
)
834 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
835 &nexthop
->src
.ipv4
, bytelen
);
838 if (IS_ZEBRA_DEBUG_KERNEL
)
840 "netlink_route_multipath() (%s): "
841 "nexthop via %s %s if %u",
842 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
843 label_buf
, nexthop
->ifindex
);
846 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
847 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
848 _netlink_route_nl_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
849 nlmsg
, req_size
, bytelen
,
852 if (cmd
== RTM_NEWROUTE
) {
853 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
854 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
855 &nexthop
->rmap_src
.ipv6
, bytelen
);
856 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
857 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
858 &nexthop
->src
.ipv6
, bytelen
);
861 if (IS_ZEBRA_DEBUG_KERNEL
)
863 "netlink_route_multipath() (%s): "
864 "nexthop via %s %s if %u",
865 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
866 label_buf
, nexthop
->ifindex
);
868 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
869 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
870 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
872 if (cmd
== RTM_NEWROUTE
) {
873 if (nexthop
->rmap_src
.ipv4
.s_addr
)
874 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
875 &nexthop
->rmap_src
.ipv4
, bytelen
);
876 else if (nexthop
->src
.ipv4
.s_addr
)
877 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
878 &nexthop
->src
.ipv4
, bytelen
);
881 if (IS_ZEBRA_DEBUG_KERNEL
)
883 "netlink_route_multipath() (%s): "
885 routedesc
, nexthop
->ifindex
);
888 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
889 addattr32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
891 if (cmd
== RTM_NEWROUTE
) {
892 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
893 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
894 &nexthop
->rmap_src
.ipv6
, bytelen
);
895 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
896 addattr_l(nlmsg
, req_size
, RTA_PREFSRC
,
897 &nexthop
->src
.ipv6
, bytelen
);
900 if (IS_ZEBRA_DEBUG_KERNEL
)
902 "netlink_route_multipath() (%s): "
904 routedesc
, nexthop
->ifindex
);
908 /* This function takes a nexthop as argument and
909 * appends to the given rtattr/rtnexthop pair the
910 * representation of the nexthop. If the nexthop
911 * defines a preferred source, the src parameter
912 * will be modified to point to that src, otherwise
913 * it will be kept unmodified.
915 * @param routedesc: Human readable description of route type
916 * (direct/recursive, single-/multipath)
917 * @param bytelen: Length of addresses in bytes.
918 * @param nexthop: Nexthop information
919 * @param rta: rtnetlink attribute structure
920 * @param rtnh: pointer to an rtnetlink nexthop structure
921 * @param src: pointer pointing to a location where
922 * the prefsrc should be stored.
924 static void _netlink_route_build_multipath(const char *routedesc
, int bytelen
,
925 struct nexthop
*nexthop
,
927 struct rtnexthop
*rtnh
,
931 struct nexthop_label
*nh_label
;
932 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
935 rtnh
->rtnh_len
= sizeof(*rtnh
);
936 rtnh
->rtnh_flags
= 0;
938 rta
->rta_len
+= rtnh
->rtnh_len
;
941 * label_buf is *only* currently used within debugging.
942 * As such when we assign it we are guarding it inside
943 * a debug test. If you want to change this make sure
944 * you fix this assumption
947 /* outgoing label - either as NEWDST (in the case of LSR) or as ENCAP
948 * (in the case of LER)
950 nh_label
= nexthop
->nh_label
;
951 if (rtmsg
->rtm_family
== AF_MPLS
) {
953 assert(nh_label
->num_labels
== 1);
956 if (nh_label
&& nh_label
->num_labels
) {
957 int i
, num_labels
= 0;
961 for (i
= 0; i
< nh_label
->num_labels
; i
++) {
962 if (nh_label
->label
[i
] != MPLS_IMP_NULL_LABEL
) {
963 bos
= ((i
== (nh_label
->num_labels
- 1)) ? 1
965 out_lse
[i
] = mpls_lse_encode(nh_label
->label
[i
],
967 if (IS_ZEBRA_DEBUG_KERNEL
) {
969 sprintf(label_buf
, "label %d",
972 sprintf(label_buf1
, "/%d",
974 strcat(label_buf
, label_buf1
);
981 if (rtmsg
->rtm_family
== AF_MPLS
) {
982 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_NEWDST
,
984 num_labels
* sizeof(mpls_lse_t
));
985 rtnh
->rtnh_len
+= RTA_LENGTH(
986 num_labels
* sizeof(mpls_lse_t
));
989 u_int16_t encap
= LWTUNNEL_ENCAP_MPLS
;
990 int len
= rta
->rta_len
;
992 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
993 RTA_ENCAP_TYPE
, &encap
,
995 nest
= rta_nest(rta
, NL_PKT_BUF_SIZE
,
997 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
,
998 MPLS_IPTUNNEL_DST
, &out_lse
,
999 num_labels
* sizeof(mpls_lse_t
));
1000 rta_nest_end(rta
, nest
);
1001 rtnh
->rtnh_len
+= rta
->rta_len
- len
;
1006 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1007 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1009 if (rtmsg
->rtm_family
== AF_INET
1010 && (nexthop
->type
== NEXTHOP_TYPE_IPV6
1011 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)) {
1013 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1014 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
, &ipv4_ll
,
1016 rtnh
->rtnh_len
+= sizeof(struct rtattr
) + bytelen
;
1017 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1019 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1020 *src
= &nexthop
->rmap_src
;
1021 else if (nexthop
->src
.ipv4
.s_addr
)
1022 *src
= &nexthop
->src
;
1024 if (IS_ZEBRA_DEBUG_KERNEL
)
1026 " 5549: netlink_route_build_multipath() (%s): "
1027 "nexthop via %s %s if %u",
1028 routedesc
, ipv4_ll_buf
, label_buf
,
1033 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1034 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1035 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1036 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1038 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1039 *src
= &nexthop
->rmap_src
;
1040 else if (nexthop
->src
.ipv4
.s_addr
)
1041 *src
= &nexthop
->src
;
1043 if (IS_ZEBRA_DEBUG_KERNEL
)
1045 "netlink_route_multipath() (%s): "
1046 "nexthop via %s %s if %u",
1047 routedesc
, inet_ntoa(nexthop
->gate
.ipv4
),
1048 label_buf
, nexthop
->ifindex
);
1050 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1051 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1052 _netlink_route_rta_add_gateway_info(rtmsg
->rtm_family
, AF_INET6
,
1053 rta
, rtnh
, NL_PKT_BUF_SIZE
,
1056 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1057 *src
= &nexthop
->rmap_src
;
1058 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1059 *src
= &nexthop
->src
;
1061 if (IS_ZEBRA_DEBUG_KERNEL
)
1063 "netlink_route_multipath() (%s): "
1064 "nexthop via %s %s if %u",
1065 routedesc
, inet6_ntoa(nexthop
->gate
.ipv6
),
1066 label_buf
, nexthop
->ifindex
);
1069 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1070 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1071 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1073 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1074 *src
= &nexthop
->rmap_src
;
1075 else if (nexthop
->src
.ipv4
.s_addr
)
1076 *src
= &nexthop
->src
;
1078 if (IS_ZEBRA_DEBUG_KERNEL
)
1080 "netlink_route_multipath() (%s): "
1081 "nexthop via if %u",
1082 routedesc
, nexthop
->ifindex
);
1083 } else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1084 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1086 if (IS_ZEBRA_DEBUG_KERNEL
)
1088 "netlink_route_multipath() (%s): "
1089 "nexthop via if %u",
1090 routedesc
, nexthop
->ifindex
);
1092 rtnh
->rtnh_ifindex
= 0;
1096 static inline void _netlink_mpls_build_singlepath(const char *routedesc
,
1097 zebra_nhlfe_t
*nhlfe
,
1098 struct nlmsghdr
*nlmsg
,
1099 struct rtmsg
*rtmsg
,
1100 size_t req_size
, int cmd
)
1105 family
= NHLFE_FAMILY(nhlfe
);
1106 bytelen
= (family
== AF_INET
? 4 : 16);
1107 _netlink_route_build_singlepath(routedesc
, bytelen
, nhlfe
->nexthop
,
1108 nlmsg
, rtmsg
, req_size
, cmd
);
1113 _netlink_mpls_build_multipath(const char *routedesc
, zebra_nhlfe_t
*nhlfe
,
1114 struct rtattr
*rta
, struct rtnexthop
*rtnh
,
1115 struct rtmsg
*rtmsg
, union g_addr
**src
)
1120 family
= NHLFE_FAMILY(nhlfe
);
1121 bytelen
= (family
== AF_INET
? 4 : 16);
1122 _netlink_route_build_multipath(routedesc
, bytelen
, nhlfe
->nexthop
, rta
,
1127 /* Log debug information for netlink_route_multipath
1128 * if debug logging is enabled.
1130 * @param cmd: Netlink command which is to be processed
1131 * @param p: Prefix for which the change is due
1132 * @param nexthop: Nexthop which is currently processed
1133 * @param routedesc: Semantic annotation for nexthop
1134 * (recursive, multipath, etc.)
1135 * @param family: Address family which the change concerns
1137 static void _netlink_route_debug(int cmd
, struct prefix
*p
,
1138 struct nexthop
*nexthop
, const char *routedesc
,
1139 int family
, struct zebra_vrf
*zvrf
)
1141 if (IS_ZEBRA_DEBUG_KERNEL
) {
1142 char buf
[PREFIX_STRLEN
];
1144 "netlink_route_multipath() (%s): %s %s vrf %u type %s",
1145 routedesc
, nl_msg_type_to_str(cmd
),
1146 prefix2str(p
, buf
, sizeof(buf
)), zvrf_id(zvrf
),
1147 (nexthop
) ? nexthop_type_to_str(nexthop
->type
) : "UNK");
1151 static void _netlink_mpls_debug(int cmd
, u_int32_t label
, const char *routedesc
)
1153 if (IS_ZEBRA_DEBUG_KERNEL
)
1154 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc
,
1155 nl_msg_type_to_str(cmd
), label
);
1158 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1167 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1169 memset(&req
.n
, 0, sizeof(req
.n
));
1170 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1172 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1173 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1174 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1175 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1177 req
.ndm
.ndm_family
= AF_INET
;
1178 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1179 req
.ndm
.ndm_ifindex
= ifindex
;
1180 req
.ndm
.ndm_type
= RTN_UNICAST
;
1182 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
1183 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1185 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1189 /* Routing table change via netlink interface. */
1190 /* Update flag indicates whether this is a "replace" or not. */
1191 static int netlink_route_multipath(int cmd
, struct prefix
*p
,
1192 struct prefix
*src_p
, struct route_entry
*re
,
1196 struct sockaddr_nl snl
;
1197 struct nexthop
*nexthop
= NULL
;
1198 unsigned int nexthop_num
;
1200 int family
= PREFIX_FAMILY(p
);
1201 const char *routedesc
;
1208 char buf
[NL_PKT_BUF_SIZE
];
1211 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1212 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1214 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1216 bytelen
= (family
== AF_INET
? 4 : 16);
1218 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1219 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1220 if ((cmd
== RTM_NEWROUTE
) && update
)
1221 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
1222 req
.n
.nlmsg_type
= cmd
;
1223 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1225 req
.r
.rtm_family
= family
;
1226 req
.r
.rtm_dst_len
= p
->prefixlen
;
1227 req
.r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1228 req
.r
.rtm_protocol
= get_rt_proto(re
->type
);
1229 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1231 if ((re
->flags
& ZEBRA_FLAG_BLACKHOLE
)
1232 || (re
->flags
& ZEBRA_FLAG_REJECT
))
1237 if (cmd
== RTM_NEWROUTE
) {
1239 if (re
->flags
& ZEBRA_FLAG_BLACKHOLE
)
1240 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1241 else if (re
->flags
& ZEBRA_FLAG_REJECT
)
1242 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1244 assert(RTN_BLACKHOLE
1245 != RTN_UNREACHABLE
); /* false */
1247 req
.r
.rtm_type
= RTN_UNICAST
;
1250 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1252 addattr_l(&req
.n
, sizeof req
, RTA_SRC
, &src_p
->u
.prefix
,
1256 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1258 * either by the kernel or by zebra. Its purely for calculating best
1260 * by the routing protocol and for communicating with protocol peers.
1262 addattr32(&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
1264 /* Table corresponding to this route. */
1265 if (re
->table
< 256)
1266 req
.r
.rtm_table
= re
->table
;
1268 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1269 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, re
->table
);
1272 if (re
->mtu
|| re
->nexthop_mtu
) {
1273 char buf
[NL_PKT_BUF_SIZE
];
1274 struct rtattr
*rta
= (void *)buf
;
1275 u_int32_t mtu
= re
->mtu
;
1276 if (!mtu
|| (re
->nexthop_mtu
&& re
->nexthop_mtu
< mtu
))
1277 mtu
= re
->nexthop_mtu
;
1278 rta
->rta_type
= RTA_METRICS
;
1279 rta
->rta_len
= RTA_LENGTH(0);
1280 rta_addattr_l(rta
, NL_PKT_BUF_SIZE
, RTAX_MTU
, &mtu
, sizeof mtu
);
1281 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_METRICS
, RTA_DATA(rta
),
1286 if (cmd
== RTM_NEWROUTE
)
1287 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1288 /* We shouldn't encounter recursive nexthops on
1290 * but it is probably better to handle that case
1293 if (CHECK_FLAG(nexthop
->flags
,
1294 NEXTHOP_FLAG_RECURSIVE
))
1300 /* Count overall nexthops so we can decide whether to use singlepath
1301 * or multipath case. */
1303 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1304 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1306 if (cmd
== RTM_NEWROUTE
1307 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1309 if (cmd
== RTM_DELROUTE
1310 && !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1316 /* Singlepath case. */
1317 if (nexthop_num
== 1 || multipath_num
== 1) {
1319 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1320 if (CHECK_FLAG(nexthop
->flags
,
1321 NEXTHOP_FLAG_RECURSIVE
)) {
1323 if (family
== AF_INET
) {
1324 if (nexthop
->rmap_src
.ipv4
1331 } else if (nexthop
->src
.ipv4
1339 } else if (family
== AF_INET6
) {
1340 if (!IN6_IS_ADDR_UNSPECIFIED(
1348 !IN6_IS_ADDR_UNSPECIFIED(
1361 if ((cmd
== RTM_NEWROUTE
1362 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1363 || (cmd
== RTM_DELROUTE
1364 && CHECK_FLAG(nexthop
->flags
,
1365 NEXTHOP_FLAG_FIB
))) {
1366 routedesc
= nexthop
->rparent
1367 ? "recursive, 1 hop"
1370 _netlink_route_debug(cmd
, p
, nexthop
, routedesc
,
1372 _netlink_route_build_singlepath(
1373 routedesc
, bytelen
, nexthop
, &req
.n
,
1374 &req
.r
, sizeof req
, cmd
);
1379 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1380 if (family
== AF_INET
)
1381 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1382 &src
.ipv4
, bytelen
);
1383 else if (family
== AF_INET6
)
1384 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1385 &src
.ipv6
, bytelen
);
1388 char buf
[NL_PKT_BUF_SIZE
];
1389 struct rtattr
*rta
= (void *)buf
;
1390 struct rtnexthop
*rtnh
;
1391 union g_addr
*src1
= NULL
;
1393 rta
->rta_type
= RTA_MULTIPATH
;
1394 rta
->rta_len
= RTA_LENGTH(0);
1395 rtnh
= RTA_DATA(rta
);
1398 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1399 if (nexthop_num
>= multipath_num
)
1402 if (CHECK_FLAG(nexthop
->flags
,
1403 NEXTHOP_FLAG_RECURSIVE
)) {
1404 /* This only works for IPv4 now */
1406 if (family
== AF_INET
) {
1407 if (nexthop
->rmap_src
.ipv4
1414 } else if (nexthop
->src
.ipv4
1422 } else if (family
== AF_INET6
) {
1423 if (!IN6_IS_ADDR_UNSPECIFIED(
1431 !IN6_IS_ADDR_UNSPECIFIED(
1444 if ((cmd
== RTM_NEWROUTE
1445 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1446 || (cmd
== RTM_DELROUTE
1447 && CHECK_FLAG(nexthop
->flags
,
1448 NEXTHOP_FLAG_FIB
))) {
1449 routedesc
= nexthop
->rparent
1450 ? "recursive, multihop"
1454 _netlink_route_debug(cmd
, p
, nexthop
, routedesc
,
1456 _netlink_route_build_multipath(
1457 routedesc
, bytelen
, nexthop
, rta
, rtnh
,
1459 rtnh
= RTNH_NEXT(rtnh
);
1461 if (!setsrc
&& src1
) {
1462 if (family
== AF_INET
)
1463 src
.ipv4
= src1
->ipv4
;
1464 else if (family
== AF_INET6
)
1465 src
.ipv6
= src1
->ipv6
;
1471 if (setsrc
&& (cmd
== RTM_NEWROUTE
)) {
1472 if (family
== AF_INET
)
1473 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1474 &src
.ipv4
, bytelen
);
1475 else if (family
== AF_INET6
)
1476 addattr_l(&req
.n
, sizeof req
, RTA_PREFSRC
,
1477 &src
.ipv6
, bytelen
);
1478 if (IS_ZEBRA_DEBUG_KERNEL
)
1479 zlog_debug("Setting source");
1482 if (rta
->rta_len
> RTA_LENGTH(0))
1483 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
1484 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1487 /* If there is no useful nexthop then return. */
1488 if (nexthop_num
== 0) {
1489 if (IS_ZEBRA_DEBUG_KERNEL
)
1491 "netlink_route_multipath(): No useful nexthop.");
1497 /* Destination netlink address. */
1498 memset(&snl
, 0, sizeof snl
);
1499 snl
.nl_family
= AF_NETLINK
;
1501 /* Talk to netlink socket. */
1502 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1506 int kernel_get_ipmr_sg_stats(void *in
)
1509 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1517 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1519 memset(&req
.n
, 0, sizeof(req
.n
));
1520 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1522 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1523 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1524 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1526 req
.ndm
.ndm_family
= AF_INET
;
1527 req
.n
.nlmsg_type
= RTM_GETROUTE
;
1529 addattr_l(&req
.n
, sizeof(req
), RTA_IIF
, &mroute
->ifindex
, 4);
1530 addattr_l(&req
.n
, sizeof(req
), RTA_OIF
, &mroute
->ifindex
, 4);
1531 addattr_l(&req
.n
, sizeof(req
), RTA_SRC
, &mroute
->sg
.src
.s_addr
, 4);
1532 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &mroute
->sg
.grp
.s_addr
, 4);
1534 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
1535 &zns
->netlink_cmd
, zns
, 0);
1541 int kernel_route_rib(struct prefix
*p
, struct prefix
*src_p
,
1542 struct route_entry
*old
, struct route_entry
*new)
1545 return netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
, new, 0);
1547 return netlink_route_multipath(RTM_DELROUTE
, p
, src_p
, old
, 0);
1549 return netlink_route_multipath(RTM_NEWROUTE
, p
, src_p
, new, 1);
1552 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
1555 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
1560 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1561 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1563 static int netlink_vxlan_flood_list_update(struct interface
*ifp
,
1564 struct in_addr
*vtep_ip
, int cmd
)
1566 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1572 u_char dst_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1574 memset(&req
.n
, 0, sizeof(req
.n
));
1575 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1577 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1578 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1579 if (cmd
== RTM_NEWNEIGH
)
1580 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_APPEND
);
1581 req
.n
.nlmsg_type
= cmd
;
1582 req
.ndm
.ndm_family
= PF_BRIDGE
;
1583 req
.ndm
.ndm_state
= NUD_NOARP
| NUD_PERMANENT
;
1584 req
.ndm
.ndm_flags
|= NTF_SELF
; // Handle by "self", not "master"
1587 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, &dst_mac
, 6);
1588 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1589 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
->s_addr
, 4);
1591 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1596 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1598 * a "flood" MAC FDB entry.
1600 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1602 if (IS_ZEBRA_DEBUG_VXLAN
)
1603 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1604 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1606 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_NEWNEIGH
);
1610 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1611 * deleting the "flood" MAC FDB entry.
1613 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
1615 if (IS_ZEBRA_DEBUG_VXLAN
)
1617 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1618 inet_ntoa(*vtep_ip
), vni
, ifp
->name
, ifp
->ifindex
);
1620 return netlink_vxlan_flood_list_update(ifp
, vtep_ip
, RTM_DELNEIGH
);
1624 #define NDA_RTA(r) \
1625 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1628 static int netlink_macfdb_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1632 struct interface
*ifp
;
1633 struct zebra_if
*zif
;
1634 struct zebra_vrf
*zvrf
;
1635 struct rtattr
*tb
[NDA_MAX
+ 1];
1636 struct interface
*br_if
;
1639 struct prefix vtep_ip
;
1640 int vid_present
= 0, dst_present
= 0;
1641 char buf
[ETHER_ADDR_STRLEN
];
1646 ndm
= NLMSG_DATA(h
);
1648 /* The interface should exist. */
1649 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
1654 /* Locate VRF corresponding to interface. We only process MAC
1656 * if EVPN is enabled on this VRF.
1658 zvrf
= vrf_info_lookup(ifp
->vrf_id
);
1659 if (!zvrf
|| !EVPN_ENABLED(zvrf
))
1664 /* The interface should be something we're interested in. */
1665 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1668 /* Drop "permanent" entries. */
1669 if (ndm
->ndm_state
& NUD_PERMANENT
)
1672 zif
= (struct zebra_if
*)ifp
->info
;
1673 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1674 zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master",
1675 nl_msg_type_to_str(h
->nlmsg_type
),
1676 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1677 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1681 /* Parse attributes and extract fields of interest. */
1682 memset(tb
, 0, sizeof tb
);
1683 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
1685 if (!tb
[NDA_LLADDR
]) {
1686 zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR",
1687 nl_msg_type_to_str(h
->nlmsg_type
),
1688 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1689 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
);
1693 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETHER_ADDR_LEN
) {
1695 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %ld",
1696 nl_msg_type_to_str(h
->nlmsg_type
),
1697 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1698 ndm
->ndm_ifindex
, zif
->brslave_info
.bridge_ifindex
,
1699 RTA_PAYLOAD(tb
[NDA_LLADDR
]));
1703 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETHER_ADDR_LEN
);
1705 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
1707 vid
= *(u_int16_t
*)RTA_DATA(tb
[NDA_VLAN
]);
1708 sprintf(vid_buf
, " VLAN %u", vid
);
1712 /* TODO: Only IPv4 supported now. */
1714 vtep_ip
.family
= AF_INET
;
1715 vtep_ip
.prefixlen
= IPV4_MAX_BITLEN
;
1716 memcpy(&(vtep_ip
.u
.prefix4
.s_addr
), RTA_DATA(tb
[NDA_DST
]),
1718 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
.u
.prefix4
));
1721 sticky
= (ndm
->ndm_state
& NUD_NOARP
) ? 1 : 0;
1723 if (IS_ZEBRA_DEBUG_KERNEL
)
1724 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
1725 nl_msg_type_to_str(h
->nlmsg_type
),
1726 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
1727 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
1728 sticky
? "sticky " : "",
1729 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1730 dst_present
? dst_buf
: "");
1732 if (filter_vlan
&& vid
!= filter_vlan
)
1735 /* If add or update, do accordingly if learnt on a "local" interface; if
1736 * the notification is over VxLAN, this has to be related to
1738 * so perform an implicit delete of any local entry (if it exists).
1740 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
1741 /* Drop "permanent" entries. */
1742 if (ndm
->ndm_state
& NUD_PERMANENT
)
1745 if (IS_ZEBRA_IF_VXLAN(ifp
))
1746 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
1749 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
1753 /* This is a delete notification.
1754 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
1755 * 2. For a MAC over "local" interface, delete the mac
1756 * Note: We will get notifications from both bridge driver and VxLAN
1758 * Ignore the notification from VxLan driver as it is also generated
1759 * when mac moves from remote to local.
1764 if (IS_ZEBRA_IF_VXLAN(ifp
))
1765 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
1768 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
1771 static int netlink_macfdb_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1772 ns_id_t ns_id
, int startup
)
1777 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
1780 /* Length validity. */
1781 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
1785 /* We are interested only in AF_BRIDGE notifications. */
1786 ndm
= NLMSG_DATA(h
);
1787 if (ndm
->ndm_family
!= AF_BRIDGE
)
1790 return netlink_macfdb_change(snl
, h
, len
);
1793 /* Request for MAC FDB information from the kernel */
1794 static int netlink_request_macs(struct zebra_ns
*zns
, int family
, int type
,
1795 ifindex_t master_ifindex
)
1799 struct ifinfomsg ifm
;
1803 /* Form the request, specifying filter (rtattr) if needed. */
1804 memset(&req
, 0, sizeof(req
));
1805 req
.n
.nlmsg_type
= type
;
1806 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1807 req
.ifm
.ifi_family
= family
;
1809 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
1811 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
1815 * MAC forwarding database read using netlink interface. This is invoked
1818 int netlink_macfdb_read(struct zebra_ns
*zns
)
1822 /* Get bridge FDB table. */
1823 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
, 0);
1826 /* We are reading entire table. */
1828 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
1835 * MAC forwarding database read using netlink interface. This is for a
1836 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
1838 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
1839 struct interface
*br_if
)
1841 struct zebra_if
*br_zif
;
1842 struct zebra_if
*zif
;
1843 struct zebra_l2info_vxlan
*vxl
;
1847 /* Save VLAN we're filtering on, if needed. */
1848 br_zif
= (struct zebra_if
*)br_if
->info
;
1849 zif
= (struct zebra_if
*)ifp
->info
;
1850 vxl
= &zif
->l2info
.vxl
;
1851 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
1852 filter_vlan
= vxl
->access_vlan
;
1854 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
1856 ret
= netlink_request_macs(zns
, AF_BRIDGE
, RTM_GETNEIGH
,
1860 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
, zns
,
1863 /* Reset VLAN filter. */
1868 static int netlink_macfdb_update(struct interface
*ifp
, vlanid_t vid
,
1869 struct ethaddr
*mac
, struct in_addr vtep_ip
,
1870 int local
, int cmd
, u_char sticky
)
1872 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1879 struct zebra_if
*zif
;
1880 struct interface
*br_if
;
1881 struct zebra_if
*br_zif
;
1882 char buf
[ETHER_ADDR_STRLEN
];
1883 int vid_present
= 0, dst_present
= 0;
1888 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
1889 zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge",
1890 (cmd
== RTM_NEWNEIGH
) ? "add" : "del", ifp
->name
,
1895 memset(&req
.n
, 0, sizeof(req
.n
));
1896 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
1898 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1899 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1900 if (cmd
== RTM_NEWNEIGH
)
1901 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
1902 req
.n
.nlmsg_type
= cmd
;
1903 req
.ndm
.ndm_family
= AF_BRIDGE
;
1904 req
.ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
1905 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1908 req
.ndm
.ndm_state
|= NUD_NOARP
;
1910 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
1912 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
1913 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
1915 dst_alen
= 4; // TODO: hardcoded
1916 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &vtep_ip
, dst_alen
);
1918 sprintf(dst_buf
, " dst %s", inet_ntoa(vtep_ip
));
1920 br_zif
= (struct zebra_if
*)br_if
->info
;
1921 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0) {
1922 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
1924 sprintf(vid_buf
, " VLAN %u", vid
);
1926 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
1928 if (IS_ZEBRA_DEBUG_KERNEL
)
1929 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
1930 nl_msg_type_to_str(cmd
),
1931 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
1932 ifp
->ifindex
, vid_present
? vid_buf
: "",
1933 sticky
? "sticky " : "",
1934 prefix_mac2str(mac
, buf
, sizeof(buf
)),
1935 dst_present
? dst_buf
: "");
1937 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1942 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
1945 static int netlink_ipneigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1949 struct interface
*ifp
;
1950 struct zebra_if
*zif
;
1951 struct zebra_vrf
*zvrf
;
1952 struct rtattr
*tb
[NDA_MAX
+ 1];
1953 struct interface
*link_if
;
1956 char buf
[ETHER_ADDR_STRLEN
];
1957 char buf2
[INET6_ADDRSTRLEN
];
1958 int mac_present
= 0;
1961 ndm
= NLMSG_DATA(h
);
1963 /* The interface should exist. */
1964 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
),
1969 /* Locate VRF corresponding to interface. We only process neigh
1971 * if EVPN is enabled on this VRF.
1973 zvrf
= vrf_info_lookup(ifp
->vrf_id
);
1974 if (!zvrf
|| !EVPN_ENABLED(zvrf
))
1979 /* Drop "permanent" entries. */
1980 if (ndm
->ndm_state
& NUD_PERMANENT
)
1983 zif
= (struct zebra_if
*)ifp
->info
;
1984 /* The neighbor is present on an SVI. From this, we locate the
1986 * bridge because we're only interested in neighbors on a VxLAN bridge.
1987 * The bridge is located based on the nature of the SVI:
1988 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
1990 * and is linked to the bridge
1991 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
1995 if (IS_ZEBRA_IF_VLAN(ifp
)) {
1996 link_if
= zif
->link
;
1999 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
2004 /* Parse attributes and extract fields of interest. */
2005 memset(tb
, 0, sizeof tb
);
2006 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2009 zlog_warn("%s family %s IF %s(%u) - no DST",
2010 nl_msg_type_to_str(h
->nlmsg_type
),
2011 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2015 memset(&mac
, 0, sizeof(struct ethaddr
));
2016 memset(&ip
, 0, sizeof(struct ipaddr
));
2017 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
2018 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
2020 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2021 if (tb
[NDA_LLADDR
]) {
2022 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETHER_ADDR_LEN
) {
2024 "%s family %s IF %s(%u) - LLADDR is not MAC, len %ld",
2025 nl_msg_type_to_str(h
->nlmsg_type
),
2026 nl_family_to_str(ndm
->ndm_family
),
2027 ifp
->name
, ndm
->ndm_ifindex
,
2028 RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2033 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETHER_ADDR_LEN
);
2036 ext_learned
= (ndm
->ndm_flags
& NTF_EXT_LEARNED
) ? 1 : 0;
2038 if (IS_ZEBRA_DEBUG_KERNEL
)
2040 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2041 nl_msg_type_to_str(h
->nlmsg_type
),
2042 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2044 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
2046 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
2048 ndm
->ndm_state
, ndm
->ndm_flags
);
2050 /* If the neighbor state is valid for use, process as an add or
2052 * else process as a delete. Note that the delete handling may
2054 * in re-adding the neighbor if it is a valid "remote" neighbor.
2056 if (ndm
->ndm_state
& NUD_VALID
)
2057 return zebra_vxlan_local_neigh_add_update(
2058 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
2061 return zebra_vxlan_local_neigh_del(ifp
, link_if
, &ip
);
2064 if (IS_ZEBRA_DEBUG_KERNEL
)
2065 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2066 nl_msg_type_to_str(h
->nlmsg_type
),
2067 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
2069 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
2071 /* Process the delete - it may result in re-adding the neighbor if it is
2072 * a valid "remote" neighbor.
2074 return zebra_vxlan_local_neigh_del(ifp
, link_if
, &ip
);
2077 static int netlink_neigh_table(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2078 ns_id_t ns_id
, int startup
)
2083 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
2086 /* Length validity. */
2087 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2091 /* We are interested only in AF_INET or AF_INET6 notifications. */
2092 ndm
= NLMSG_DATA(h
);
2093 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
2096 return netlink_neigh_change(snl
, h
, len
);
2099 /* Request for IP neighbor information from the kernel */
2100 static int netlink_request_neigh(struct zebra_ns
*zns
, int family
, int type
,
2109 /* Form the request, specifying filter (rtattr) if needed. */
2110 memset(&req
, 0, sizeof(req
));
2111 req
.n
.nlmsg_type
= type
;
2112 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2113 req
.ndm
.ndm_family
= family
;
2115 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
2117 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
2121 * IP Neighbor table read using netlink interface. This is invoked
2124 int netlink_neigh_read(struct zebra_ns
*zns
)
2128 /* Get IP neighbor table. */
2129 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
, 0);
2132 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2139 * IP Neighbor table read using netlink interface. This is for a specific
2142 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
2146 ret
= netlink_request_neigh(zns
, AF_UNSPEC
, RTM_GETNEIGH
,
2150 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
, zns
, 0,
2156 int netlink_neigh_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
2162 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
2165 /* Length validity. */
2166 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
2170 /* Is this a notification for the MAC FDB or IP neighbor table? */
2171 ndm
= NLMSG_DATA(h
);
2172 if (ndm
->ndm_family
== AF_BRIDGE
)
2173 return netlink_macfdb_change(snl
, h
, len
);
2175 if (ndm
->ndm_type
!= RTN_UNICAST
)
2178 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
2179 return netlink_ipneigh_change(snl
, h
, len
);
2184 static int netlink_neigh_update2(struct interface
*ifp
, struct ipaddr
*ip
,
2185 struct ethaddr
*mac
, u_int32_t flags
, int cmd
)
2194 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2195 char buf
[INET6_ADDRSTRLEN
];
2196 char buf2
[ETHER_ADDR_STRLEN
];
2198 memset(&req
.n
, 0, sizeof(req
.n
));
2199 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2201 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2202 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2203 if (cmd
== RTM_NEWNEIGH
)
2204 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
2205 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2206 req
.ndm
.ndm_family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
2207 req
.ndm
.ndm_state
= flags
;
2208 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
2209 req
.ndm
.ndm_type
= RTN_UNICAST
;
2210 req
.ndm
.ndm_flags
= NTF_EXT_LEARNED
;
2213 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2214 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
2216 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
2218 if (IS_ZEBRA_DEBUG_KERNEL
)
2219 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
2220 nl_msg_type_to_str(cmd
),
2221 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
2222 ifp
->ifindex
, ipaddr2str(ip
, buf
, sizeof(buf
)),
2223 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
))
2226 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2230 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2231 struct in_addr vtep_ip
, u_char sticky
)
2233 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, 0, RTM_NEWNEIGH
,
2237 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
2238 struct in_addr vtep_ip
, int local
)
2240 return netlink_macfdb_update(ifp
, vid
, mac
, vtep_ip
, local
,
2244 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
2245 struct ethaddr
*mac
)
2247 return netlink_neigh_update2(ifp
, ip
, mac
, NUD_REACHABLE
, RTM_NEWNEIGH
);
2250 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
2252 return netlink_neigh_update2(ifp
, ip
, NULL
, 0, RTM_DELNEIGH
);
2256 * MPLS label forwarding table change via netlink interface.
2258 int netlink_mpls_multipath(int cmd
, zebra_lsp_t
*lsp
)
2261 zebra_nhlfe_t
*nhlfe
;
2262 struct nexthop
*nexthop
= NULL
;
2263 unsigned int nexthop_num
;
2264 const char *routedesc
;
2265 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2270 char buf
[NL_PKT_BUF_SIZE
];
2273 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2277 * Count # nexthops so we can decide whether to use singlepath
2278 * or multipath case.
2281 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2282 nexthop
= nhlfe
->nexthop
;
2285 if (cmd
== RTM_NEWROUTE
) {
2286 /* Count all selected NHLFEs */
2287 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2288 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2292 /* Count all installed NHLFEs */
2293 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
2294 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2299 if (nexthop_num
== 0) // unexpected
2302 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2303 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2304 req
.n
.nlmsg_type
= cmd
;
2305 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2307 req
.r
.rtm_family
= AF_MPLS
;
2308 req
.r
.rtm_table
= RT_TABLE_MAIN
;
2309 req
.r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
2310 req
.r
.rtm_protocol
= RTPROT_ZEBRA
;
2311 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2312 req
.r
.rtm_type
= RTN_UNICAST
;
2314 if (cmd
== RTM_NEWROUTE
)
2315 /* We do a replace to handle update. */
2316 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2318 /* Fill destination */
2319 lse
= mpls_lse_encode(lsp
->ile
.in_label
, 0, 0, 1);
2320 addattr_l(&req
.n
, sizeof req
, RTA_DST
, &lse
, sizeof(mpls_lse_t
));
2322 /* Fill nexthops (paths) based on single-path or multipath. The paths
2323 * chosen depend on the operation.
2325 if (nexthop_num
== 1 || multipath_num
== 1) {
2326 routedesc
= "single hop";
2327 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2330 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2331 nexthop
= nhlfe
->nexthop
;
2335 if ((cmd
== RTM_NEWROUTE
2336 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2337 && CHECK_FLAG(nexthop
->flags
,
2338 NEXTHOP_FLAG_ACTIVE
)))
2339 || (cmd
== RTM_DELROUTE
2340 && (CHECK_FLAG(nhlfe
->flags
,
2341 NHLFE_FLAG_INSTALLED
)
2342 && CHECK_FLAG(nexthop
->flags
,
2343 NEXTHOP_FLAG_FIB
)))) {
2344 /* Add the gateway */
2345 _netlink_mpls_build_singlepath(routedesc
, nhlfe
,
2348 if (cmd
== RTM_NEWROUTE
) {
2349 SET_FLAG(nhlfe
->flags
,
2350 NHLFE_FLAG_INSTALLED
);
2351 SET_FLAG(nexthop
->flags
,
2354 UNSET_FLAG(nhlfe
->flags
,
2355 NHLFE_FLAG_INSTALLED
);
2356 UNSET_FLAG(nexthop
->flags
,
2363 } else /* Multipath case */
2365 char buf
[NL_PKT_BUF_SIZE
];
2366 struct rtattr
*rta
= (void *)buf
;
2367 struct rtnexthop
*rtnh
;
2368 union g_addr
*src1
= NULL
;
2370 rta
->rta_type
= RTA_MULTIPATH
;
2371 rta
->rta_len
= RTA_LENGTH(0);
2372 rtnh
= RTA_DATA(rta
);
2374 routedesc
= "multihop";
2375 _netlink_mpls_debug(cmd
, lsp
->ile
.in_label
, routedesc
);
2378 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2379 nexthop
= nhlfe
->nexthop
;
2383 if (nexthop_num
>= multipath_num
)
2386 if ((cmd
== RTM_NEWROUTE
2387 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
2388 && CHECK_FLAG(nexthop
->flags
,
2389 NEXTHOP_FLAG_ACTIVE
)))
2390 || (cmd
== RTM_DELROUTE
2391 && (CHECK_FLAG(nhlfe
->flags
,
2392 NHLFE_FLAG_INSTALLED
)
2393 && CHECK_FLAG(nexthop
->flags
,
2394 NEXTHOP_FLAG_FIB
)))) {
2397 /* Build the multipath */
2398 _netlink_mpls_build_multipath(routedesc
, nhlfe
,
2401 rtnh
= RTNH_NEXT(rtnh
);
2403 if (cmd
== RTM_NEWROUTE
) {
2404 SET_FLAG(nhlfe
->flags
,
2405 NHLFE_FLAG_INSTALLED
);
2406 SET_FLAG(nexthop
->flags
,
2409 UNSET_FLAG(nhlfe
->flags
,
2410 NHLFE_FLAG_INSTALLED
);
2411 UNSET_FLAG(nexthop
->flags
,
2417 /* Add the multipath */
2418 if (rta
->rta_len
> RTA_LENGTH(0))
2419 addattr_l(&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
,
2420 RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2423 /* Talk to netlink socket. */
2424 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2429 * Handle failure in LSP install, clear flags for NHLFE.
2431 void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
2433 zebra_nhlfe_t
*nhlfe
;
2434 struct nexthop
*nexthop
;
2436 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
2437 nexthop
= nhlfe
->nexthop
;
2441 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2442 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);