]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rt_netlink.c
Merge pull request #8138 from ckishimo/ospfv3_prefix
[mirror_frr.git] / zebra / rt_netlink.c
1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 #ifdef HAVE_NETLINK
24
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>
30 #include <linux/nexthop.h>
31
32 /* Hack for GNU libc version 2. */
33 #ifndef MSG_TRUNC
34 #define MSG_TRUNC 0x20
35 #endif /* MSG_TRUNC */
36
37 #include "linklist.h"
38 #include "if.h"
39 #include "log.h"
40 #include "prefix.h"
41 #include "connected.h"
42 #include "table.h"
43 #include "memory.h"
44 #include "zebra_memory.h"
45 #include "rib.h"
46 #include "thread.h"
47 #include "privs.h"
48 #include "nexthop.h"
49 #include "vrf.h"
50 #include "vty.h"
51 #include "mpls.h"
52 #include "vxlan.h"
53 #include "printfrr.h"
54
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
58 #include "zebra/rt.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/rt_netlink.h"
67 #include "zebra/zebra_nhg.h"
68 #include "zebra/zebra_mroute.h"
69 #include "zebra/zebra_vxlan.h"
70 #include "zebra/zebra_errors.h"
71 #include "zebra/zebra_evpn_mh.h"
72
73 #ifndef AF_MPLS
74 #define AF_MPLS 28
75 #endif
76
77 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
78 * UAPI for MAC sync. */
79 #ifndef _UAPI_LINUX_IF_BRIDGE_H
80 #define BR_SPH_LIST_SIZE 10
81 #endif
82
83 static vlanid_t filter_vlan = 0;
84
85 /* We capture whether the current kernel supports nexthop ids; by
86 * default, we'll use them if possible. There's also a configuration
87 * available to _disable_ use of kernel nexthops.
88 */
89 static bool supports_nh;
90
91 struct gw_family_t {
92 uint16_t filler;
93 uint16_t family;
94 union g_addr gate;
95 };
96
97 static const char ipv4_ll_buf[16] = "169.254.0.1";
98 static struct in_addr ipv4_ll;
99
100 /* Is this a ipv4 over ipv6 route? */
101 static bool is_route_v4_over_v6(unsigned char rtm_family,
102 enum nexthop_types_t nexthop_type)
103 {
104 if (rtm_family == AF_INET
105 && (nexthop_type == NEXTHOP_TYPE_IPV6
106 || nexthop_type == NEXTHOP_TYPE_IPV6_IFINDEX))
107 return true;
108
109 return false;
110 }
111
112 /* Helper to control use of kernel-level nexthop ids */
113 static bool kernel_nexthops_supported(void)
114 {
115 return (supports_nh && !vrf_is_backend_netns()
116 && zebra_nhg_kernel_nexthops_enabled());
117 }
118
119 /*
120 * Some people may only want to use NHGs created by protos and not
121 * implicitly created by Zebra. This check accounts for that.
122 */
123 static bool proto_nexthops_only(void)
124 {
125 return zebra_nhg_proto_nexthops_only();
126 }
127
128 /* Is this a proto created NHG? */
129 static bool is_proto_nhg(uint32_t id, int type)
130 {
131 /* If type is available, use it as the source of truth */
132 if (type) {
133 if (type != ZEBRA_ROUTE_NHG)
134 return true;
135 return false;
136 }
137
138 if (id >= ZEBRA_NHG_PROTO_LOWER)
139 return true;
140
141 return false;
142 }
143
144 /*
145 * The ipv4_ll data structure is used for all 5549
146 * additions to the kernel. Let's figure out the
147 * correct value one time instead for every
148 * install/remove of a 5549 type route
149 */
150 void rt_netlink_init(void)
151 {
152 inet_pton(AF_INET, ipv4_ll_buf, &ipv4_ll);
153 }
154
155 /*
156 * Mapping from dataplane neighbor flags to netlink flags
157 */
158 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags)
159 {
160 uint8_t flags = 0;
161
162 if (dplane_flags & DPLANE_NTF_EXT_LEARNED)
163 flags |= NTF_EXT_LEARNED;
164 if (dplane_flags & DPLANE_NTF_ROUTER)
165 flags |= NTF_ROUTER;
166 if (dplane_flags & DPLANE_NTF_USE)
167 flags |= NTF_USE;
168
169 return flags;
170 }
171
172 /*
173 * Mapping from dataplane neighbor state to netlink state
174 */
175 static uint16_t neigh_state_to_netlink(uint16_t dplane_state)
176 {
177 uint16_t state = 0;
178
179 if (dplane_state & DPLANE_NUD_REACHABLE)
180 state |= NUD_REACHABLE;
181 if (dplane_state & DPLANE_NUD_STALE)
182 state |= NUD_STALE;
183 if (dplane_state & DPLANE_NUD_NOARP)
184 state |= NUD_NOARP;
185 if (dplane_state & DPLANE_NUD_PROBE)
186 state |= NUD_PROBE;
187 if (dplane_state & DPLANE_NUD_INCOMPLETE)
188 state |= NUD_INCOMPLETE;
189
190 return state;
191 }
192
193
194 static inline bool is_selfroute(int proto)
195 {
196 if ((proto == RTPROT_BGP) || (proto == RTPROT_OSPF)
197 || (proto == RTPROT_ZSTATIC) || (proto == RTPROT_ZEBRA)
198 || (proto == RTPROT_ISIS) || (proto == RTPROT_RIPNG)
199 || (proto == RTPROT_NHRP) || (proto == RTPROT_EIGRP)
200 || (proto == RTPROT_LDP) || (proto == RTPROT_BABEL)
201 || (proto == RTPROT_RIP) || (proto == RTPROT_SHARP)
202 || (proto == RTPROT_PBR) || (proto == RTPROT_OPENFABRIC)
203 || (proto == RTPROT_SRTE)) {
204 return true;
205 }
206
207 return false;
208 }
209
210 static inline int zebra2proto(int proto)
211 {
212 switch (proto) {
213 case ZEBRA_ROUTE_BABEL:
214 proto = RTPROT_BABEL;
215 break;
216 case ZEBRA_ROUTE_BGP:
217 proto = RTPROT_BGP;
218 break;
219 case ZEBRA_ROUTE_OSPF:
220 case ZEBRA_ROUTE_OSPF6:
221 proto = RTPROT_OSPF;
222 break;
223 case ZEBRA_ROUTE_STATIC:
224 proto = RTPROT_ZSTATIC;
225 break;
226 case ZEBRA_ROUTE_ISIS:
227 proto = RTPROT_ISIS;
228 break;
229 case ZEBRA_ROUTE_RIP:
230 proto = RTPROT_RIP;
231 break;
232 case ZEBRA_ROUTE_RIPNG:
233 proto = RTPROT_RIPNG;
234 break;
235 case ZEBRA_ROUTE_NHRP:
236 proto = RTPROT_NHRP;
237 break;
238 case ZEBRA_ROUTE_EIGRP:
239 proto = RTPROT_EIGRP;
240 break;
241 case ZEBRA_ROUTE_LDP:
242 proto = RTPROT_LDP;
243 break;
244 case ZEBRA_ROUTE_SHARP:
245 proto = RTPROT_SHARP;
246 break;
247 case ZEBRA_ROUTE_PBR:
248 proto = RTPROT_PBR;
249 break;
250 case ZEBRA_ROUTE_OPENFABRIC:
251 proto = RTPROT_OPENFABRIC;
252 break;
253 case ZEBRA_ROUTE_SRTE:
254 proto = RTPROT_SRTE;
255 break;
256 case ZEBRA_ROUTE_TABLE:
257 case ZEBRA_ROUTE_NHG:
258 proto = RTPROT_ZEBRA;
259 break;
260 case ZEBRA_ROUTE_CONNECT:
261 case ZEBRA_ROUTE_KERNEL:
262 proto = RTPROT_KERNEL;
263 break;
264 default:
265 /*
266 * When a user adds a new protocol this will show up
267 * to let them know to do something about it. This
268 * is intentionally a warn because we should see
269 * this as part of development of a new protocol
270 */
271 zlog_debug(
272 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
273 __func__, proto);
274 proto = RTPROT_ZEBRA;
275 break;
276 }
277
278 return proto;
279 }
280
281 static inline int proto2zebra(int proto, int family, bool is_nexthop)
282 {
283 switch (proto) {
284 case RTPROT_BABEL:
285 proto = ZEBRA_ROUTE_BABEL;
286 break;
287 case RTPROT_BGP:
288 proto = ZEBRA_ROUTE_BGP;
289 break;
290 case RTPROT_OSPF:
291 proto = (family == AF_INET) ? ZEBRA_ROUTE_OSPF
292 : ZEBRA_ROUTE_OSPF6;
293 break;
294 case RTPROT_ISIS:
295 proto = ZEBRA_ROUTE_ISIS;
296 break;
297 case RTPROT_RIP:
298 proto = ZEBRA_ROUTE_RIP;
299 break;
300 case RTPROT_RIPNG:
301 proto = ZEBRA_ROUTE_RIPNG;
302 break;
303 case RTPROT_NHRP:
304 proto = ZEBRA_ROUTE_NHRP;
305 break;
306 case RTPROT_EIGRP:
307 proto = ZEBRA_ROUTE_EIGRP;
308 break;
309 case RTPROT_LDP:
310 proto = ZEBRA_ROUTE_LDP;
311 break;
312 case RTPROT_STATIC:
313 case RTPROT_ZSTATIC:
314 proto = ZEBRA_ROUTE_STATIC;
315 break;
316 case RTPROT_SHARP:
317 proto = ZEBRA_ROUTE_SHARP;
318 break;
319 case RTPROT_PBR:
320 proto = ZEBRA_ROUTE_PBR;
321 break;
322 case RTPROT_OPENFABRIC:
323 proto = ZEBRA_ROUTE_OPENFABRIC;
324 break;
325 case RTPROT_SRTE:
326 proto = ZEBRA_ROUTE_SRTE;
327 break;
328 case RTPROT_ZEBRA:
329 if (is_nexthop) {
330 proto = ZEBRA_ROUTE_NHG;
331 break;
332 }
333 /* Intentional fall thru */
334 default:
335 /*
336 * When a user adds a new protocol this will show up
337 * to let them know to do something about it. This
338 * is intentionally a warn because we should see
339 * this as part of development of a new protocol
340 */
341 zlog_debug(
342 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
343 __func__, proto);
344 proto = ZEBRA_ROUTE_KERNEL;
345 break;
346 }
347 return proto;
348 }
349
350 /*
351 Pending: create an efficient table_id (in a tree/hash) based lookup)
352 */
353 vrf_id_t vrf_lookup_by_table(uint32_t table_id, ns_id_t ns_id)
354 {
355 struct vrf *vrf;
356 struct zebra_vrf *zvrf;
357
358 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
359 zvrf = vrf->info;
360 if (zvrf == NULL)
361 continue;
362 /* case vrf with netns : match the netnsid */
363 if (vrf_is_backend_netns()) {
364 if (ns_id == zvrf_id(zvrf))
365 return zvrf_id(zvrf);
366 } else {
367 /* VRF is VRF_BACKEND_VRF_LITE */
368 if (zvrf->table_id != table_id)
369 continue;
370 return zvrf_id(zvrf);
371 }
372 }
373
374 return VRF_DEFAULT;
375 }
376
377 /**
378 * @parse_encap_mpls() - Parses encapsulated mpls attributes
379 * @tb: Pointer to rtattr to look for nested items in.
380 * @labels: Pointer to store labels in.
381 *
382 * Return: Number of mpls labels found.
383 */
384 static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels)
385 {
386 struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0};
387 mpls_lse_t *lses = NULL;
388 int num_labels = 0;
389 uint32_t ttl = 0;
390 uint32_t bos = 0;
391 uint32_t exp = 0;
392 mpls_label_t label = 0;
393
394 netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb);
395 lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]);
396 while (!bos && num_labels < MPLS_MAX_LABELS) {
397 mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos);
398 labels[num_labels++] = label;
399 }
400
401 return num_labels;
402 }
403
404 static struct nexthop
405 parse_nexthop_unicast(ns_id_t ns_id, struct rtmsg *rtm, struct rtattr **tb,
406 enum blackhole_type bh_type, int index, void *prefsrc,
407 void *gate, afi_t afi, vrf_id_t vrf_id)
408 {
409 struct interface *ifp = NULL;
410 struct nexthop nh = {0};
411 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
412 int num_labels = 0;
413
414 vrf_id_t nh_vrf_id = vrf_id;
415 size_t sz = (afi == AFI_IP) ? 4 : 16;
416
417 if (bh_type == BLACKHOLE_UNSPEC) {
418 if (index && !gate)
419 nh.type = NEXTHOP_TYPE_IFINDEX;
420 else if (index && gate)
421 nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4_IFINDEX
422 : NEXTHOP_TYPE_IPV6_IFINDEX;
423 else if (!index && gate)
424 nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4
425 : NEXTHOP_TYPE_IPV6;
426 else {
427 nh.type = NEXTHOP_TYPE_BLACKHOLE;
428 nh.bh_type = bh_type;
429 }
430 } else {
431 nh.type = NEXTHOP_TYPE_BLACKHOLE;
432 nh.bh_type = bh_type;
433 }
434 nh.ifindex = index;
435 if (prefsrc)
436 memcpy(&nh.src, prefsrc, sz);
437 if (gate)
438 memcpy(&nh.gate, gate, sz);
439
440 if (index) {
441 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), index);
442 if (ifp)
443 nh_vrf_id = ifp->vrf_id;
444 }
445 nh.vrf_id = nh_vrf_id;
446
447 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
448 && *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
449 == LWTUNNEL_ENCAP_MPLS) {
450 num_labels = parse_encap_mpls(tb[RTA_ENCAP], labels);
451 }
452
453 if (rtm->rtm_flags & RTNH_F_ONLINK)
454 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
455
456 if (num_labels)
457 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels, labels);
458
459 return nh;
460 }
461
462 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id,
463 struct nexthop_group *ng,
464 struct rtmsg *rtm,
465 struct rtnexthop *rtnh,
466 struct rtattr **tb,
467 void *prefsrc, vrf_id_t vrf_id)
468 {
469 void *gate = NULL;
470 struct interface *ifp = NULL;
471 int index = 0;
472 /* MPLS labels */
473 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
474 int num_labels = 0;
475 struct rtattr *rtnh_tb[RTA_MAX + 1] = {};
476
477 int len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
478 vrf_id_t nh_vrf_id = vrf_id;
479
480 for (;;) {
481 struct nexthop *nh = NULL;
482
483 if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len)
484 break;
485
486 index = rtnh->rtnh_ifindex;
487 if (index) {
488 /*
489 * Yes we are looking this up
490 * for every nexthop and just
491 * using the last one looked
492 * up right now
493 */
494 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
495 index);
496 if (ifp)
497 nh_vrf_id = ifp->vrf_id;
498 else {
499 flog_warn(
500 EC_ZEBRA_UNKNOWN_INTERFACE,
501 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
502 __func__, index);
503 nh_vrf_id = VRF_DEFAULT;
504 }
505 } else
506 nh_vrf_id = vrf_id;
507
508 if (rtnh->rtnh_len > sizeof(*rtnh)) {
509 memset(rtnh_tb, 0, sizeof(rtnh_tb));
510
511 netlink_parse_rtattr(rtnh_tb, RTA_MAX, RTNH_DATA(rtnh),
512 rtnh->rtnh_len - sizeof(*rtnh));
513 if (rtnh_tb[RTA_GATEWAY])
514 gate = RTA_DATA(rtnh_tb[RTA_GATEWAY]);
515 if (rtnh_tb[RTA_ENCAP] && rtnh_tb[RTA_ENCAP_TYPE]
516 && *(uint16_t *)RTA_DATA(rtnh_tb[RTA_ENCAP_TYPE])
517 == LWTUNNEL_ENCAP_MPLS) {
518 num_labels = parse_encap_mpls(
519 rtnh_tb[RTA_ENCAP], labels);
520 }
521 }
522
523 if (gate && rtm->rtm_family == AF_INET) {
524 if (index)
525 nh = nexthop_from_ipv4_ifindex(
526 gate, prefsrc, index, nh_vrf_id);
527 else
528 nh = nexthop_from_ipv4(gate, prefsrc,
529 nh_vrf_id);
530 } else if (gate && rtm->rtm_family == AF_INET6) {
531 if (index)
532 nh = nexthop_from_ipv6_ifindex(
533 gate, index, nh_vrf_id);
534 else
535 nh = nexthop_from_ipv6(gate, nh_vrf_id);
536 } else
537 nh = nexthop_from_ifindex(index, nh_vrf_id);
538
539 if (nh) {
540 nh->weight = rtnh->rtnh_hops + 1;
541
542 if (num_labels)
543 nexthop_add_labels(nh, ZEBRA_LSP_STATIC,
544 num_labels, labels);
545
546 if (rtnh->rtnh_flags & RTNH_F_ONLINK)
547 SET_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK);
548
549 /* Add to temporary list */
550 nexthop_group_add_sorted(ng, nh);
551 }
552
553 if (rtnh->rtnh_len == 0)
554 break;
555
556 len -= NLMSG_ALIGN(rtnh->rtnh_len);
557 rtnh = RTNH_NEXT(rtnh);
558 }
559
560 uint8_t nhop_num = nexthop_group_nexthop_num(ng);
561
562 return nhop_num;
563 }
564
565 /* Looking up routing table by netlink interface. */
566 static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
567 int startup)
568 {
569 int len;
570 struct rtmsg *rtm;
571 struct rtattr *tb[RTA_MAX + 1];
572 uint32_t flags = 0;
573 struct prefix p;
574 struct prefix_ipv6 src_p = {};
575 vrf_id_t vrf_id;
576 bool selfroute;
577
578 char anyaddr[16] = {0};
579
580 int proto = ZEBRA_ROUTE_KERNEL;
581 int index = 0;
582 int table;
583 int metric = 0;
584 uint32_t mtu = 0;
585 uint8_t distance = 0;
586 route_tag_t tag = 0;
587 uint32_t nhe_id = 0;
588
589 void *dest = NULL;
590 void *gate = NULL;
591 void *prefsrc = NULL; /* IPv4 preferred source host address */
592 void *src = NULL; /* IPv6 srcdest source prefix */
593 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
594
595 rtm = NLMSG_DATA(h);
596
597 if (startup && h->nlmsg_type != RTM_NEWROUTE)
598 return 0;
599 switch (rtm->rtm_type) {
600 case RTN_UNICAST:
601 break;
602 case RTN_BLACKHOLE:
603 bh_type = BLACKHOLE_NULL;
604 break;
605 case RTN_UNREACHABLE:
606 bh_type = BLACKHOLE_REJECT;
607 break;
608 case RTN_PROHIBIT:
609 bh_type = BLACKHOLE_ADMINPROHIB;
610 break;
611 default:
612 if (IS_ZEBRA_DEBUG_KERNEL)
613 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
614 nl_rttype_to_str(rtm->rtm_type),
615 rtm->rtm_type);
616 return 0;
617 }
618
619 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
620 if (len < 0) {
621 zlog_err(
622 "%s: Message received from netlink is of a broken size %d %zu",
623 __func__, h->nlmsg_len,
624 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
625 return -1;
626 }
627
628 memset(tb, 0, sizeof(tb));
629 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
630
631 if (rtm->rtm_flags & RTM_F_CLONED)
632 return 0;
633 if (rtm->rtm_protocol == RTPROT_REDIRECT)
634 return 0;
635 if (rtm->rtm_protocol == RTPROT_KERNEL)
636 return 0;
637
638 selfroute = is_selfroute(rtm->rtm_protocol);
639
640 if (!startup && selfroute
641 && h->nlmsg_type == RTM_NEWROUTE
642 && !zrouter.asic_offloaded) {
643 if (IS_ZEBRA_DEBUG_KERNEL)
644 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
645 rtm->rtm_protocol);
646 return 0;
647 }
648
649 /* We don't care about change notifications for the MPLS table. */
650 /* TODO: Revisit this. */
651 if (rtm->rtm_family == AF_MPLS)
652 return 0;
653
654 /* Table corresponding to route. */
655 if (tb[RTA_TABLE])
656 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
657 else
658 table = rtm->rtm_table;
659
660 /* Map to VRF */
661 vrf_id = vrf_lookup_by_table(table, ns_id);
662 if (vrf_id == VRF_DEFAULT) {
663 if (!is_zebra_valid_kernel_table(table)
664 && !is_zebra_main_routing_table(table))
665 return 0;
666 }
667
668 if (rtm->rtm_flags & RTM_F_TRAP)
669 flags |= ZEBRA_FLAG_TRAPPED;
670 if (rtm->rtm_flags & RTM_F_OFFLOAD)
671 flags |= ZEBRA_FLAG_OFFLOADED;
672 if (rtm->rtm_flags & RTM_F_OFFLOAD_FAILED)
673 flags |= ZEBRA_FLAG_OFFLOAD_FAILED;
674
675 /* Route which inserted by Zebra. */
676 if (selfroute) {
677 flags |= ZEBRA_FLAG_SELFROUTE;
678 proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family, false);
679 }
680 if (tb[RTA_OIF])
681 index = *(int *)RTA_DATA(tb[RTA_OIF]);
682
683 if (tb[RTA_DST])
684 dest = RTA_DATA(tb[RTA_DST]);
685 else
686 dest = anyaddr;
687
688 if (tb[RTA_SRC])
689 src = RTA_DATA(tb[RTA_SRC]);
690 else
691 src = anyaddr;
692
693 if (tb[RTA_PREFSRC])
694 prefsrc = RTA_DATA(tb[RTA_PREFSRC]);
695
696 if (tb[RTA_GATEWAY])
697 gate = RTA_DATA(tb[RTA_GATEWAY]);
698
699 if (tb[RTA_NH_ID])
700 nhe_id = *(uint32_t *)RTA_DATA(tb[RTA_NH_ID]);
701
702 if (tb[RTA_PRIORITY])
703 metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]);
704
705 #if defined(SUPPORT_REALMS)
706 if (tb[RTA_FLOW])
707 tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]);
708 #endif
709
710 if (tb[RTA_METRICS]) {
711 struct rtattr *mxrta[RTAX_MAX + 1];
712
713 memset(mxrta, 0, sizeof(mxrta));
714 netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
715 RTA_PAYLOAD(tb[RTA_METRICS]));
716
717 if (mxrta[RTAX_MTU])
718 mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]);
719 }
720
721 if (rtm->rtm_family == AF_INET) {
722 p.family = AF_INET;
723 if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) {
724 zlog_err(
725 "Invalid destination prefix length: %u received from kernel route change",
726 rtm->rtm_dst_len);
727 return -1;
728 }
729 memcpy(&p.u.prefix4, dest, 4);
730 p.prefixlen = rtm->rtm_dst_len;
731
732 if (rtm->rtm_src_len != 0) {
733 flog_warn(
734 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST,
735 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
736 &p, vrf_id);
737 return 0;
738 }
739
740 /* Force debug below to not display anything for source */
741 src_p.prefixlen = 0;
742 } else if (rtm->rtm_family == AF_INET6) {
743 p.family = AF_INET6;
744 if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) {
745 zlog_err(
746 "Invalid destination prefix length: %u received from kernel route change",
747 rtm->rtm_dst_len);
748 return -1;
749 }
750 memcpy(&p.u.prefix6, dest, 16);
751 p.prefixlen = rtm->rtm_dst_len;
752
753 src_p.family = AF_INET6;
754 if (rtm->rtm_src_len > IPV6_MAX_BITLEN) {
755 zlog_err(
756 "Invalid source prefix length: %u received from kernel route change",
757 rtm->rtm_src_len);
758 return -1;
759 }
760 memcpy(&src_p.prefix, src, 16);
761 src_p.prefixlen = rtm->rtm_src_len;
762 }
763
764 /*
765 * For ZEBRA_ROUTE_KERNEL types:
766 *
767 * The metric/priority of the route received from the kernel
768 * is a 32 bit number. We are going to interpret the high
769 * order byte as the Admin Distance and the low order 3 bytes
770 * as the metric.
771 *
772 * This will allow us to do two things:
773 * 1) Allow the creation of kernel routes that can be
774 * overridden by zebra.
775 * 2) Allow the old behavior for 'most' kernel route types
776 * if a user enters 'ip route ...' v4 routes get a metric
777 * of 0 and v6 routes get a metric of 1024. Both of these
778 * values will end up with a admin distance of 0, which
779 * will cause them to win for the purposes of zebra.
780 */
781 if (proto == ZEBRA_ROUTE_KERNEL) {
782 distance = (metric >> 24) & 0xFF;
783 metric = (metric & 0x00FFFFFF);
784 }
785
786 if (IS_ZEBRA_DEBUG_KERNEL) {
787 char buf2[PREFIX_STRLEN];
788
789 zlog_debug(
790 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
791 nl_msg_type_to_str(h->nlmsg_type), &p,
792 src_p.prefixlen ? " from " : "",
793 src_p.prefixlen ? prefix2str(&src_p, buf2, sizeof(buf2))
794 : "",
795 vrf_id_to_name(vrf_id), vrf_id, table, metric,
796 distance);
797 }
798
799 afi_t afi = AFI_IP;
800 if (rtm->rtm_family == AF_INET6)
801 afi = AFI_IP6;
802
803 if (h->nlmsg_type == RTM_NEWROUTE) {
804
805 if (!tb[RTA_MULTIPATH]) {
806 struct nexthop nh = {0};
807
808 if (!nhe_id) {
809 nh = parse_nexthop_unicast(
810 ns_id, rtm, tb, bh_type, index, prefsrc,
811 gate, afi, vrf_id);
812 }
813 rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p,
814 &src_p, &nh, nhe_id, table, metric, mtu,
815 distance, tag);
816 } else {
817 /* This is a multipath route */
818 struct route_entry *re;
819 struct nexthop_group *ng = NULL;
820 struct rtnexthop *rtnh =
821 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
822
823 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
824 re->type = proto;
825 re->distance = distance;
826 re->flags = flags;
827 re->metric = metric;
828 re->mtu = mtu;
829 re->vrf_id = vrf_id;
830 re->table = table;
831 re->uptime = monotime(NULL);
832 re->tag = tag;
833 re->nhe_id = nhe_id;
834
835 if (!nhe_id) {
836 uint8_t nhop_num;
837
838 /* Use temporary list of nexthops; parse
839 * message payload's nexthops.
840 */
841 ng = nexthop_group_new();
842 nhop_num =
843 parse_multipath_nexthops_unicast(
844 ns_id, ng, rtm, rtnh, tb,
845 prefsrc, vrf_id);
846
847 zserv_nexthop_num_warn(
848 __func__, (const struct prefix *)&p,
849 nhop_num);
850
851 if (nhop_num == 0) {
852 nexthop_group_delete(&ng);
853 ng = NULL;
854 }
855 }
856
857 if (nhe_id || ng)
858 rib_add_multipath(afi, SAFI_UNICAST, &p,
859 &src_p, re, ng);
860 else
861 XFREE(MTYPE_RE, re);
862 }
863 } else {
864 if (nhe_id) {
865 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
866 &p, &src_p, NULL, nhe_id, table, metric,
867 distance, true);
868 } else {
869 if (!tb[RTA_MULTIPATH]) {
870 struct nexthop nh;
871
872 nh = parse_nexthop_unicast(
873 ns_id, rtm, tb, bh_type, index, prefsrc,
874 gate, afi, vrf_id);
875 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0,
876 flags, &p, &src_p, &nh, 0, table,
877 metric, distance, true);
878 } else {
879 /* XXX: need to compare the entire list of
880 * nexthops here for NLM_F_APPEND stupidity */
881 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0,
882 flags, &p, &src_p, NULL, 0, table,
883 metric, distance, true);
884 }
885 }
886 }
887
888 return 0;
889 }
890
891 static struct mcast_route_data *mroute = NULL;
892
893 static int netlink_route_change_read_multicast(struct nlmsghdr *h,
894 ns_id_t ns_id, int startup)
895 {
896 int len;
897 struct rtmsg *rtm;
898 struct rtattr *tb[RTA_MAX + 1];
899 struct mcast_route_data *m;
900 struct mcast_route_data mr;
901 int iif = 0;
902 int count;
903 int oif[256];
904 int oif_count = 0;
905 char oif_list[256] = "\0";
906 vrf_id_t vrf;
907 int table;
908
909 if (mroute)
910 m = mroute;
911 else {
912 memset(&mr, 0, sizeof(mr));
913 m = &mr;
914 }
915
916 rtm = NLMSG_DATA(h);
917
918 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
919
920 memset(tb, 0, sizeof(tb));
921 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
922
923 if (tb[RTA_TABLE])
924 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
925 else
926 table = rtm->rtm_table;
927
928 vrf = vrf_lookup_by_table(table, ns_id);
929
930 if (tb[RTA_IIF])
931 iif = *(int *)RTA_DATA(tb[RTA_IIF]);
932
933 if (tb[RTA_SRC])
934 m->sg.src = *(struct in_addr *)RTA_DATA(tb[RTA_SRC]);
935
936 if (tb[RTA_DST])
937 m->sg.grp = *(struct in_addr *)RTA_DATA(tb[RTA_DST]);
938
939 if (tb[RTA_EXPIRES])
940 m->lastused = *(unsigned long long *)RTA_DATA(tb[RTA_EXPIRES]);
941
942 if (tb[RTA_MULTIPATH]) {
943 struct rtnexthop *rtnh =
944 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
945
946 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
947 for (;;) {
948 if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len)
949 break;
950
951 oif[oif_count] = rtnh->rtnh_ifindex;
952 oif_count++;
953
954 if (rtnh->rtnh_len == 0)
955 break;
956
957 len -= NLMSG_ALIGN(rtnh->rtnh_len);
958 rtnh = RTNH_NEXT(rtnh);
959 }
960 }
961
962 if (IS_ZEBRA_DEBUG_KERNEL) {
963 struct interface *ifp = NULL;
964 struct zebra_vrf *zvrf = NULL;
965
966 for (count = 0; count < oif_count; count++) {
967 ifp = if_lookup_by_index(oif[count], vrf);
968 char temp[256];
969
970 snprintf(temp, sizeof(temp), "%s(%d) ",
971 ifp ? ifp->name : "Unknown", oif[count]);
972 strlcat(oif_list, temp, sizeof(oif_list));
973 }
974 zvrf = zebra_vrf_lookup_by_id(vrf);
975 ifp = if_lookup_by_index(iif, vrf);
976 zlog_debug(
977 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
978 zvrf_name(zvrf), vrf, nl_msg_type_to_str(h->nlmsg_type),
979 &m->sg.src, &m->sg.grp, ifp ? ifp->name : "Unknown",
980 iif, oif_list,
981 m->lastused);
982 }
983 return 0;
984 }
985
986 int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
987 {
988 int len;
989 struct rtmsg *rtm;
990
991 rtm = NLMSG_DATA(h);
992
993 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) {
994 /* If this is not route add/delete message print warning. */
995 zlog_debug("Kernel message: %s NS %u",
996 nl_msg_type_to_str(h->nlmsg_type), ns_id);
997 return 0;
998 }
999
1000 if (!(rtm->rtm_family == AF_INET ||
1001 rtm->rtm_family == AF_INET6 ||
1002 rtm->rtm_family == RTNL_FAMILY_IPMR )) {
1003 flog_warn(
1004 EC_ZEBRA_UNKNOWN_FAMILY,
1005 "Invalid address family: %u received from kernel route change: %s",
1006 rtm->rtm_family, nl_msg_type_to_str(h->nlmsg_type));
1007 return 0;
1008 }
1009
1010 /* Connected route. */
1011 if (IS_ZEBRA_DEBUG_KERNEL)
1012 zlog_debug("%s %s %s proto %s NS %u",
1013 nl_msg_type_to_str(h->nlmsg_type),
1014 nl_family_to_str(rtm->rtm_family),
1015 nl_rttype_to_str(rtm->rtm_type),
1016 nl_rtproto_to_str(rtm->rtm_protocol), ns_id);
1017
1018
1019 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1020 if (len < 0) {
1021 zlog_err(
1022 "%s: Message received from netlink is of a broken size: %d %zu",
1023 __func__, h->nlmsg_len,
1024 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
1025 return -1;
1026 }
1027
1028 if (rtm->rtm_type == RTN_MULTICAST)
1029 netlink_route_change_read_multicast(h, ns_id, startup);
1030 else
1031 netlink_route_change_read_unicast(h, ns_id, startup);
1032 return 0;
1033 }
1034
1035 /* Request for specific route information from the kernel */
1036 static int netlink_request_route(struct zebra_ns *zns, int family, int type)
1037 {
1038 struct {
1039 struct nlmsghdr n;
1040 struct rtmsg rtm;
1041 } req;
1042
1043 /* Form the request, specifying filter (rtattr) if needed. */
1044 memset(&req, 0, sizeof(req));
1045 req.n.nlmsg_type = type;
1046 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
1047 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1048 req.rtm.rtm_family = family;
1049
1050 return netlink_request(&zns->netlink_cmd, &req);
1051 }
1052
1053 /* Routing table read function using netlink interface. Only called
1054 bootstrap time. */
1055 int netlink_route_read(struct zebra_ns *zns)
1056 {
1057 int ret;
1058 struct zebra_dplane_info dp_info;
1059
1060 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1061
1062 /* Get IPv4 routing table. */
1063 ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE);
1064 if (ret < 0)
1065 return ret;
1066 ret = netlink_parse_info(netlink_route_change_read_unicast,
1067 &zns->netlink_cmd, &dp_info, 0, 1);
1068 if (ret < 0)
1069 return ret;
1070
1071 /* Get IPv6 routing table. */
1072 ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE);
1073 if (ret < 0)
1074 return ret;
1075 ret = netlink_parse_info(netlink_route_change_read_unicast,
1076 &zns->netlink_cmd, &dp_info, 0, 1);
1077 if (ret < 0)
1078 return ret;
1079
1080 return 0;
1081 }
1082
1083 /*
1084 * The function returns true if the gateway info could be added
1085 * to the message, otherwise false is returned.
1086 */
1087 static bool _netlink_route_add_gateway_info(uint8_t route_family,
1088 uint8_t gw_family,
1089 struct nlmsghdr *nlmsg,
1090 size_t req_size, int bytelen,
1091 const struct nexthop *nexthop)
1092 {
1093 if (route_family == AF_MPLS) {
1094 struct gw_family_t gw_fam;
1095
1096 gw_fam.family = gw_family;
1097 if (gw_family == AF_INET)
1098 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
1099 else
1100 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
1101 if (!nl_attr_put(nlmsg, req_size, RTA_VIA, &gw_fam.family,
1102 bytelen + 2))
1103 return false;
1104 } else {
1105 if (!(nexthop->rparent
1106 && IS_MAPPED_IPV6(&nexthop->rparent->gate.ipv6))) {
1107 if (gw_family == AF_INET) {
1108 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY,
1109 &nexthop->gate.ipv4, bytelen))
1110 return false;
1111 } else {
1112 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY,
1113 &nexthop->gate.ipv6, bytelen))
1114 return false;
1115 }
1116 }
1117 }
1118
1119 return true;
1120 }
1121
1122 static int build_label_stack(struct mpls_label_stack *nh_label,
1123 mpls_lse_t *out_lse, char *label_buf,
1124 size_t label_buf_size)
1125 {
1126 char label_buf1[20];
1127 int num_labels = 0;
1128
1129 for (int i = 0; nh_label && i < nh_label->num_labels; i++) {
1130 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1131 continue;
1132
1133 if (IS_ZEBRA_DEBUG_KERNEL) {
1134 if (!num_labels)
1135 snprintf(label_buf, label_buf_size, "label %u",
1136 nh_label->label[i]);
1137 else {
1138 snprintf(label_buf1, sizeof(label_buf1), "/%u",
1139 nh_label->label[i]);
1140 strlcat(label_buf, label_buf1, label_buf_size);
1141 }
1142 }
1143
1144 out_lse[num_labels] =
1145 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1146 num_labels++;
1147 }
1148
1149 return num_labels;
1150 }
1151
1152 static bool _netlink_route_encode_label_info(struct mpls_label_stack *nh_label,
1153 struct nlmsghdr *nlmsg,
1154 size_t buflen, struct rtmsg *rtmsg,
1155 char *label_buf,
1156 size_t label_buf_size)
1157 {
1158 mpls_lse_t out_lse[MPLS_MAX_LABELS];
1159 int num_labels;
1160
1161 /*
1162 * label_buf is *only* currently used within debugging.
1163 * As such when we assign it we are guarding it inside
1164 * a debug test. If you want to change this make sure
1165 * you fix this assumption
1166 */
1167 label_buf[0] = '\0';
1168
1169 num_labels =
1170 build_label_stack(nh_label, out_lse, label_buf, label_buf_size);
1171
1172 if (num_labels) {
1173 /* Set the BoS bit */
1174 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1175
1176 if (rtmsg->rtm_family == AF_MPLS) {
1177 if (!nl_attr_put(nlmsg, buflen, RTA_NEWDST, &out_lse,
1178 num_labels * sizeof(mpls_lse_t)))
1179 return false;
1180 } else {
1181 struct rtattr *nest;
1182
1183 if (!nl_attr_put16(nlmsg, buflen, RTA_ENCAP_TYPE,
1184 LWTUNNEL_ENCAP_MPLS))
1185 return false;
1186
1187 nest = nl_attr_nest(nlmsg, buflen, RTA_ENCAP);
1188 if (!nest)
1189 return false;
1190
1191 if (!nl_attr_put(nlmsg, buflen, MPLS_IPTUNNEL_DST,
1192 &out_lse,
1193 num_labels * sizeof(mpls_lse_t)))
1194 return false;
1195 nl_attr_nest_end(nlmsg, nest);
1196 }
1197 }
1198
1199 return true;
1200 }
1201
1202 static bool _netlink_route_encode_nexthop_src(const struct nexthop *nexthop,
1203 int family,
1204 struct nlmsghdr *nlmsg,
1205 size_t buflen, int bytelen)
1206 {
1207 if (family == AF_INET) {
1208 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) {
1209 if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC,
1210 &nexthop->rmap_src.ipv4, bytelen))
1211 return false;
1212 } else if (nexthop->src.ipv4.s_addr != INADDR_ANY) {
1213 if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC,
1214 &nexthop->src.ipv4, bytelen))
1215 return false;
1216 }
1217 } else if (family == AF_INET6) {
1218 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) {
1219 if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC,
1220 &nexthop->rmap_src.ipv6, bytelen))
1221 return false;
1222 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) {
1223 if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC,
1224 &nexthop->src.ipv6, bytelen))
1225 return false;
1226 }
1227 }
1228
1229 return true;
1230 }
1231
1232 /* This function takes a nexthop as argument and adds
1233 * the appropriate netlink attributes to an existing
1234 * netlink message.
1235 *
1236 * @param routedesc: Human readable description of route type
1237 * (direct/recursive, single-/multipath)
1238 * @param bytelen: Length of addresses in bytes.
1239 * @param nexthop: Nexthop information
1240 * @param nlmsg: nlmsghdr structure to fill in.
1241 * @param req_size: The size allocated for the message.
1242 *
1243 * The function returns true if the nexthop could be added
1244 * to the message, otherwise false is returned.
1245 */
1246 static bool _netlink_route_build_singlepath(const struct prefix *p,
1247 const char *routedesc, int bytelen,
1248 const struct nexthop *nexthop,
1249 struct nlmsghdr *nlmsg,
1250 struct rtmsg *rtmsg,
1251 size_t req_size, int cmd)
1252 {
1253
1254 char label_buf[256];
1255 struct vrf *vrf;
1256 char addrstr[INET6_ADDRSTRLEN];
1257
1258 assert(nexthop);
1259
1260 vrf = vrf_lookup_by_id(nexthop->vrf_id);
1261
1262 if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg,
1263 req_size, rtmsg, label_buf,
1264 sizeof(label_buf)))
1265 return false;
1266
1267 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1268 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1269
1270 if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) {
1271 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1272 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4))
1273 return false;
1274 if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex))
1275 return false;
1276
1277 if (cmd == RTM_NEWROUTE) {
1278 if (!_netlink_route_encode_nexthop_src(
1279 nexthop, AF_INET, nlmsg, req_size, bytelen))
1280 return false;
1281 }
1282
1283 if (IS_ZEBRA_DEBUG_KERNEL)
1284 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1285 __func__, routedesc, p, ipv4_ll_buf,
1286 label_buf, nexthop->ifindex,
1287 VRF_LOGNAME(vrf), nexthop->vrf_id);
1288 return true;
1289 }
1290
1291 if (nexthop->type == NEXTHOP_TYPE_IPV4
1292 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1293 /* Send deletes to the kernel without specifying the next-hop */
1294 if (cmd != RTM_DELROUTE) {
1295 if (!_netlink_route_add_gateway_info(
1296 rtmsg->rtm_family, AF_INET, nlmsg, req_size,
1297 bytelen, nexthop))
1298 return false;
1299 }
1300
1301 if (cmd == RTM_NEWROUTE) {
1302 if (!_netlink_route_encode_nexthop_src(
1303 nexthop, AF_INET, nlmsg, req_size, bytelen))
1304 return false;
1305 }
1306
1307 if (IS_ZEBRA_DEBUG_KERNEL) {
1308 inet_ntop(AF_INET, &nexthop->gate.ipv4, addrstr,
1309 sizeof(addrstr));
1310 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1311 __func__, routedesc, p, addrstr, label_buf,
1312 nexthop->ifindex, VRF_LOGNAME(vrf),
1313 nexthop->vrf_id);
1314 }
1315 }
1316
1317 if (nexthop->type == NEXTHOP_TYPE_IPV6
1318 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1319 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family,
1320 AF_INET6, nlmsg, req_size,
1321 bytelen, nexthop))
1322 return false;
1323
1324 if (cmd == RTM_NEWROUTE) {
1325 if (!_netlink_route_encode_nexthop_src(
1326 nexthop, AF_INET6, nlmsg, req_size,
1327 bytelen))
1328 return false;
1329 }
1330
1331 if (IS_ZEBRA_DEBUG_KERNEL) {
1332 inet_ntop(AF_INET6, &nexthop->gate.ipv6, addrstr,
1333 sizeof(addrstr));
1334 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1335 __func__, routedesc, p, addrstr, label_buf,
1336 nexthop->ifindex, VRF_LOGNAME(vrf),
1337 nexthop->vrf_id);
1338 }
1339 }
1340
1341 /*
1342 * We have the ifindex so we should always send it
1343 * This is especially useful if we are doing route
1344 * leaking.
1345 */
1346 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
1347 if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex))
1348 return false;
1349 }
1350
1351 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
1352 if (cmd == RTM_NEWROUTE) {
1353 if (!_netlink_route_encode_nexthop_src(
1354 nexthop, AF_INET, nlmsg, req_size, bytelen))
1355 return false;
1356 }
1357
1358 if (IS_ZEBRA_DEBUG_KERNEL)
1359 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1360 __func__, routedesc, p, nexthop->ifindex,
1361 VRF_LOGNAME(vrf), nexthop->vrf_id);
1362 }
1363
1364 return true;
1365 }
1366
1367 /* This function takes a nexthop as argument and
1368 * appends to the given netlink msg. If the nexthop
1369 * defines a preferred source, the src parameter
1370 * will be modified to point to that src, otherwise
1371 * it will be kept unmodified.
1372 *
1373 * @param routedesc: Human readable description of route type
1374 * (direct/recursive, single-/multipath)
1375 * @param bytelen: Length of addresses in bytes.
1376 * @param nexthop: Nexthop information
1377 * @param nlmsg: nlmsghdr structure to fill in.
1378 * @param req_size: The size allocated for the message.
1379 * @param src: pointer pointing to a location where
1380 * the prefsrc should be stored.
1381 *
1382 * The function returns true if the nexthop could be added
1383 * to the message, otherwise false is returned.
1384 */
1385 static bool _netlink_route_build_multipath(const struct prefix *p,
1386 const char *routedesc, int bytelen,
1387 const struct nexthop *nexthop,
1388 struct nlmsghdr *nlmsg,
1389 size_t req_size, struct rtmsg *rtmsg,
1390 const union g_addr **src)
1391 {
1392 char label_buf[256];
1393 struct vrf *vrf;
1394 struct rtnexthop *rtnh;
1395
1396 rtnh = nl_attr_rtnh(nlmsg, req_size);
1397 if (rtnh == NULL)
1398 return false;
1399
1400 assert(nexthop);
1401
1402 vrf = vrf_lookup_by_id(nexthop->vrf_id);
1403
1404 if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg,
1405 req_size, rtmsg, label_buf,
1406 sizeof(label_buf)))
1407 return false;
1408
1409 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1410 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1411
1412 if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) {
1413 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1414 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4))
1415 return false;
1416 rtnh->rtnh_ifindex = nexthop->ifindex;
1417 if (nexthop->weight)
1418 rtnh->rtnh_hops = nexthop->weight - 1;
1419
1420 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
1421 *src = &nexthop->rmap_src;
1422 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
1423 *src = &nexthop->src;
1424
1425 if (IS_ZEBRA_DEBUG_KERNEL)
1426 zlog_debug(
1427 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1428 __func__, routedesc, p, ipv4_ll_buf, label_buf,
1429 nexthop->ifindex, VRF_LOGNAME(vrf),
1430 nexthop->vrf_id);
1431 nl_attr_rtnh_end(nlmsg, rtnh);
1432 return true;
1433 }
1434
1435 if (nexthop->type == NEXTHOP_TYPE_IPV4
1436 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1437 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family, AF_INET,
1438 nlmsg, req_size, bytelen,
1439 nexthop))
1440 return false;
1441
1442 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
1443 *src = &nexthop->rmap_src;
1444 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
1445 *src = &nexthop->src;
1446
1447 if (IS_ZEBRA_DEBUG_KERNEL)
1448 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1449 __func__, routedesc, p, &nexthop->gate.ipv4,
1450 label_buf, nexthop->ifindex,
1451 VRF_LOGNAME(vrf), nexthop->vrf_id);
1452 }
1453 if (nexthop->type == NEXTHOP_TYPE_IPV6
1454 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1455 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family,
1456 AF_INET6, nlmsg, req_size,
1457 bytelen, nexthop))
1458 return false;
1459
1460 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1461 *src = &nexthop->rmap_src;
1462 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1463 *src = &nexthop->src;
1464
1465 if (IS_ZEBRA_DEBUG_KERNEL)
1466 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1467 __func__, routedesc, p, &nexthop->gate.ipv6,
1468 label_buf, nexthop->ifindex,
1469 VRF_LOGNAME(vrf), nexthop->vrf_id);
1470 }
1471
1472 /*
1473 * We have figured out the ifindex so we should always send it
1474 * This is especially useful if we are doing route
1475 * leaking.
1476 */
1477 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1478 rtnh->rtnh_ifindex = nexthop->ifindex;
1479
1480 /* ifindex */
1481 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
1482 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
1483 *src = &nexthop->rmap_src;
1484 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
1485 *src = &nexthop->src;
1486
1487 if (IS_ZEBRA_DEBUG_KERNEL)
1488 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1489 __func__, routedesc, p, nexthop->ifindex,
1490 VRF_LOGNAME(vrf), nexthop->vrf_id);
1491 }
1492
1493 if (nexthop->weight)
1494 rtnh->rtnh_hops = nexthop->weight - 1;
1495
1496 nl_attr_rtnh_end(nlmsg, rtnh);
1497 return true;
1498 }
1499
1500 static inline bool _netlink_mpls_build_singlepath(const struct prefix *p,
1501 const char *routedesc,
1502 const zebra_nhlfe_t *nhlfe,
1503 struct nlmsghdr *nlmsg,
1504 struct rtmsg *rtmsg,
1505 size_t req_size, int cmd)
1506 {
1507 int bytelen;
1508 uint8_t family;
1509
1510 family = NHLFE_FAMILY(nhlfe);
1511 bytelen = (family == AF_INET ? 4 : 16);
1512 return _netlink_route_build_singlepath(p, routedesc, bytelen,
1513 nhlfe->nexthop, nlmsg, rtmsg,
1514 req_size, cmd);
1515 }
1516
1517
1518 static inline bool
1519 _netlink_mpls_build_multipath(const struct prefix *p, const char *routedesc,
1520 const zebra_nhlfe_t *nhlfe,
1521 struct nlmsghdr *nlmsg, size_t req_size,
1522 struct rtmsg *rtmsg, const union g_addr **src)
1523 {
1524 int bytelen;
1525 uint8_t family;
1526
1527 family = NHLFE_FAMILY(nhlfe);
1528 bytelen = (family == AF_INET ? 4 : 16);
1529 return _netlink_route_build_multipath(p, routedesc, bytelen,
1530 nhlfe->nexthop, nlmsg, req_size,
1531 rtmsg, src);
1532 }
1533
1534 static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc)
1535 {
1536 if (IS_ZEBRA_DEBUG_KERNEL)
1537 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1538 routedesc, nl_msg_type_to_str(cmd), label);
1539 }
1540
1541 static int netlink_neigh_update(int cmd, int ifindex, uint32_t addr, char *lla,
1542 int llalen, ns_id_t ns_id)
1543 {
1544 uint8_t protocol = RTPROT_ZEBRA;
1545 struct {
1546 struct nlmsghdr n;
1547 struct ndmsg ndm;
1548 char buf[256];
1549 } req;
1550
1551 struct zebra_ns *zns = zebra_ns_lookup(ns_id);
1552
1553 memset(&req, 0, sizeof(req));
1554
1555 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1556 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1557 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
1558 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1559
1560 req.ndm.ndm_family = AF_INET;
1561 req.ndm.ndm_state = NUD_PERMANENT;
1562 req.ndm.ndm_ifindex = ifindex;
1563 req.ndm.ndm_type = RTN_UNICAST;
1564
1565 nl_attr_put(&req.n, sizeof(req), NDA_PROTOCOL, &protocol,
1566 sizeof(protocol));
1567 nl_attr_put32(&req.n, sizeof(req), NDA_DST, addr);
1568 nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, lla, llalen);
1569
1570 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1571 0);
1572 }
1573
1574 static bool nexthop_set_src(const struct nexthop *nexthop, int family,
1575 union g_addr *src)
1576 {
1577 if (family == AF_INET) {
1578 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) {
1579 src->ipv4 = nexthop->rmap_src.ipv4;
1580 return true;
1581 } else if (nexthop->src.ipv4.s_addr != INADDR_ANY) {
1582 src->ipv4 = nexthop->src.ipv4;
1583 return true;
1584 }
1585 } else if (family == AF_INET6) {
1586 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) {
1587 src->ipv6 = nexthop->rmap_src.ipv6;
1588 return true;
1589 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) {
1590 src->ipv6 = nexthop->src.ipv6;
1591 return true;
1592 }
1593 }
1594
1595 return false;
1596 }
1597
1598 /*
1599 * The function returns true if the attribute could be added
1600 * to the message, otherwise false is returned.
1601 */
1602 static int netlink_route_nexthop_encap(struct nlmsghdr *n, size_t nlen,
1603 struct nexthop *nh)
1604 {
1605 struct rtattr *nest;
1606
1607 switch (nh->nh_encap_type) {
1608 case NET_VXLAN:
1609 if (!nl_attr_put16(n, nlen, RTA_ENCAP_TYPE, nh->nh_encap_type))
1610 return false;
1611
1612 nest = nl_attr_nest(n, nlen, RTA_ENCAP);
1613 if (!nest)
1614 return false;
1615
1616 if (!nl_attr_put32(n, nlen, 0 /* VXLAN_VNI */,
1617 nh->nh_encap.vni))
1618 return false;
1619 nl_attr_nest_end(n, nest);
1620 break;
1621 }
1622
1623 return true;
1624 }
1625
1626 /*
1627 * Routing table change via netlink interface, using a dataplane context object
1628 *
1629 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1630 * otherwise the number of bytes written to buf.
1631 */
1632 ssize_t netlink_route_multipath_msg_encode(int cmd,
1633 struct zebra_dplane_ctx *ctx,
1634 uint8_t *data, size_t datalen,
1635 bool fpm, bool force_nhg)
1636 {
1637 int bytelen;
1638 struct nexthop *nexthop = NULL;
1639 unsigned int nexthop_num;
1640 const char *routedesc;
1641 bool setsrc = false;
1642 union g_addr src;
1643 const struct prefix *p, *src_p;
1644 uint32_t table_id;
1645
1646 struct {
1647 struct nlmsghdr n;
1648 struct rtmsg r;
1649 char buf[];
1650 } *req = (void *)data;
1651
1652 p = dplane_ctx_get_dest(ctx);
1653 src_p = dplane_ctx_get_src(ctx);
1654
1655 if (datalen < sizeof(*req))
1656 return 0;
1657
1658 memset(req, 0, sizeof(*req));
1659
1660 bytelen = (p->family == AF_INET ? 4 : 16);
1661
1662 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1663 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1664
1665 if ((cmd == RTM_NEWROUTE) &&
1666 ((p->family == AF_INET) || v6_rr_semantics))
1667 req->n.nlmsg_flags |= NLM_F_REPLACE;
1668
1669 req->n.nlmsg_type = cmd;
1670
1671 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
1672
1673 req->r.rtm_family = p->family;
1674 req->r.rtm_dst_len = p->prefixlen;
1675 req->r.rtm_src_len = src_p ? src_p->prefixlen : 0;
1676 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
1677
1678 if (cmd == RTM_DELROUTE)
1679 req->r.rtm_protocol = zebra2proto(dplane_ctx_get_old_type(ctx));
1680 else
1681 req->r.rtm_protocol = zebra2proto(dplane_ctx_get_type(ctx));
1682
1683 /*
1684 * blackhole routes are not RTN_UNICAST, they are
1685 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1686 * so setting this value as a RTN_UNICAST would
1687 * cause the route lookup of just the prefix
1688 * to fail. So no need to specify this for
1689 * the RTM_DELROUTE case
1690 */
1691 if (cmd != RTM_DELROUTE)
1692 req->r.rtm_type = RTN_UNICAST;
1693
1694 if (!nl_attr_put(&req->n, datalen, RTA_DST, &p->u.prefix, bytelen))
1695 return 0;
1696 if (src_p) {
1697 if (!nl_attr_put(&req->n, datalen, RTA_SRC, &src_p->u.prefix,
1698 bytelen))
1699 return 0;
1700 }
1701
1702 /* Metric. */
1703 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1704 * used
1705 * either by the kernel or by zebra. Its purely for calculating best
1706 * path(s)
1707 * by the routing protocol and for communicating with protocol peers.
1708 */
1709 if (!nl_attr_put32(&req->n, datalen, RTA_PRIORITY,
1710 NL_DEFAULT_ROUTE_METRIC))
1711 return 0;
1712
1713 #if defined(SUPPORT_REALMS)
1714 {
1715 route_tag_t tag;
1716
1717 if (cmd == RTM_DELROUTE)
1718 tag = dplane_ctx_get_old_tag(ctx);
1719 else
1720 tag = dplane_ctx_get_tag(ctx);
1721
1722 if (tag > 0 && tag <= 255) {
1723 if (!nl_attr_put32(&req->n, datalen, RTA_FLOW, tag))
1724 return 0;
1725 }
1726 }
1727 #endif
1728 /* Table corresponding to this route. */
1729 table_id = dplane_ctx_get_table(ctx);
1730 if (table_id < 256)
1731 req->r.rtm_table = table_id;
1732 else {
1733 req->r.rtm_table = RT_TABLE_UNSPEC;
1734 if (!nl_attr_put32(&req->n, datalen, RTA_TABLE, table_id))
1735 return 0;
1736 }
1737
1738 if (IS_ZEBRA_DEBUG_KERNEL)
1739 zlog_debug(
1740 "%s: %s %pFX vrf %u(%u)", __func__,
1741 nl_msg_type_to_str(cmd), p, dplane_ctx_get_vrf(ctx),
1742 table_id);
1743
1744 /*
1745 * If we are not updating the route and we have received
1746 * a route delete, then all we need to fill in is the
1747 * prefix information to tell the kernel to schwack
1748 * it.
1749 */
1750 if (cmd == RTM_DELROUTE)
1751 return NLMSG_ALIGN(req->n.nlmsg_len);
1752
1753 if (dplane_ctx_get_mtu(ctx) || dplane_ctx_get_nh_mtu(ctx)) {
1754 struct rtattr *nest;
1755 uint32_t mtu = dplane_ctx_get_mtu(ctx);
1756 uint32_t nexthop_mtu = dplane_ctx_get_nh_mtu(ctx);
1757
1758 if (!mtu || (nexthop_mtu && nexthop_mtu < mtu))
1759 mtu = nexthop_mtu;
1760
1761 nest = nl_attr_nest(&req->n, datalen, RTA_METRICS);
1762 if (nest == NULL)
1763 return 0;
1764
1765 if (!nl_attr_put(&req->n, datalen, RTAX_MTU, &mtu, sizeof(mtu)))
1766 return 0;
1767 nl_attr_nest_end(&req->n, nest);
1768 }
1769
1770 /*
1771 * Always install blackhole routes without using nexthops, because of
1772 * the following kernel problems:
1773 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
1774 * 2. Blackhole kernel nexthops are deleted when loopback is down.
1775 */
1776 nexthop = dplane_ctx_get_ng(ctx)->nexthop;
1777 if (nexthop) {
1778 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1779 nexthop = nexthop->resolved;
1780
1781 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1782 switch (nexthop->bh_type) {
1783 case BLACKHOLE_ADMINPROHIB:
1784 req->r.rtm_type = RTN_PROHIBIT;
1785 break;
1786 case BLACKHOLE_REJECT:
1787 req->r.rtm_type = RTN_UNREACHABLE;
1788 break;
1789 default:
1790 req->r.rtm_type = RTN_BLACKHOLE;
1791 break;
1792 }
1793 return NLMSG_ALIGN(req->n.nlmsg_len);
1794 }
1795 }
1796
1797 if ((!fpm && kernel_nexthops_supported()
1798 && (!proto_nexthops_only()
1799 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx), 0)))
1800 || (fpm && force_nhg)) {
1801 /* Kernel supports nexthop objects */
1802 if (IS_ZEBRA_DEBUG_KERNEL)
1803 zlog_debug("%s: %pFX nhg_id is %u", __func__, p,
1804 dplane_ctx_get_nhe_id(ctx));
1805
1806 if (!nl_attr_put32(&req->n, datalen, RTA_NH_ID,
1807 dplane_ctx_get_nhe_id(ctx)))
1808 return 0;
1809
1810 /* Have to determine src still */
1811 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1812 if (setsrc)
1813 break;
1814
1815 setsrc = nexthop_set_src(nexthop, p->family, &src);
1816 }
1817
1818 if (setsrc) {
1819 if (p->family == AF_INET) {
1820 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1821 &src.ipv4, bytelen))
1822 return 0;
1823 } else if (p->family == AF_INET6) {
1824 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1825 &src.ipv6, bytelen))
1826 return 0;
1827 }
1828 }
1829
1830 return NLMSG_ALIGN(req->n.nlmsg_len);
1831 }
1832
1833 /* Count overall nexthops so we can decide whether to use singlepath
1834 * or multipath case.
1835 */
1836 nexthop_num = 0;
1837 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1838 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1839 continue;
1840 if (!NEXTHOP_IS_ACTIVE(nexthop->flags))
1841 continue;
1842
1843 nexthop_num++;
1844 }
1845
1846 /* Singlepath case. */
1847 if (nexthop_num == 1) {
1848 nexthop_num = 0;
1849 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1850 if (CHECK_FLAG(nexthop->flags,
1851 NEXTHOP_FLAG_RECURSIVE)) {
1852
1853 if (setsrc)
1854 continue;
1855
1856 setsrc = nexthop_set_src(nexthop, p->family,
1857 &src);
1858 continue;
1859 }
1860
1861 if (NEXTHOP_IS_ACTIVE(nexthop->flags)) {
1862 routedesc = nexthop->rparent
1863 ? "recursive, single-path"
1864 : "single-path";
1865
1866 if (!_netlink_route_build_singlepath(
1867 p, routedesc, bytelen, nexthop,
1868 &req->n, &req->r, datalen, cmd))
1869 return 0;
1870 nexthop_num++;
1871 break;
1872 }
1873
1874 /*
1875 * Add encapsulation information when installing via
1876 * FPM.
1877 */
1878 if (fpm) {
1879 if (!netlink_route_nexthop_encap(
1880 &req->n, datalen, nexthop))
1881 return 0;
1882 }
1883 }
1884
1885 if (setsrc) {
1886 if (p->family == AF_INET) {
1887 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1888 &src.ipv4, bytelen))
1889 return 0;
1890 } else if (p->family == AF_INET6) {
1891 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1892 &src.ipv6, bytelen))
1893 return 0;
1894 }
1895 }
1896 } else { /* Multipath case */
1897 struct rtattr *nest;
1898 const union g_addr *src1 = NULL;
1899
1900 nest = nl_attr_nest(&req->n, datalen, RTA_MULTIPATH);
1901 if (nest == NULL)
1902 return 0;
1903
1904 nexthop_num = 0;
1905 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1906 if (CHECK_FLAG(nexthop->flags,
1907 NEXTHOP_FLAG_RECURSIVE)) {
1908 /* This only works for IPv4 now */
1909 if (setsrc)
1910 continue;
1911
1912 setsrc = nexthop_set_src(nexthop, p->family,
1913 &src);
1914 continue;
1915 }
1916
1917 if (NEXTHOP_IS_ACTIVE(nexthop->flags)) {
1918 routedesc = nexthop->rparent
1919 ? "recursive, multipath"
1920 : "multipath";
1921 nexthop_num++;
1922
1923 if (!_netlink_route_build_multipath(
1924 p, routedesc, bytelen, nexthop,
1925 &req->n, datalen, &req->r, &src1))
1926 return 0;
1927
1928 if (!setsrc && src1) {
1929 if (p->family == AF_INET)
1930 src.ipv4 = src1->ipv4;
1931 else if (p->family == AF_INET6)
1932 src.ipv6 = src1->ipv6;
1933
1934 setsrc = 1;
1935 }
1936 }
1937 }
1938
1939 nl_attr_nest_end(&req->n, nest);
1940
1941 /*
1942 * Add encapsulation information when installing via
1943 * FPM.
1944 */
1945 if (fpm) {
1946 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
1947 nexthop)) {
1948 if (CHECK_FLAG(nexthop->flags,
1949 NEXTHOP_FLAG_RECURSIVE))
1950 continue;
1951 if (!netlink_route_nexthop_encap(
1952 &req->n, datalen, nexthop))
1953 return 0;
1954 }
1955 }
1956
1957
1958 if (setsrc) {
1959 if (p->family == AF_INET) {
1960 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1961 &src.ipv4, bytelen))
1962 return 0;
1963 } else if (p->family == AF_INET6) {
1964 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
1965 &src.ipv6, bytelen))
1966 return 0;
1967 }
1968 if (IS_ZEBRA_DEBUG_KERNEL)
1969 zlog_debug("Setting source");
1970 }
1971 }
1972
1973 /* If there is no useful nexthop then return. */
1974 if (nexthop_num == 0) {
1975 if (IS_ZEBRA_DEBUG_KERNEL)
1976 zlog_debug("%s: No useful nexthop.", __func__);
1977 }
1978
1979 return NLMSG_ALIGN(req->n.nlmsg_len);
1980 }
1981
1982 int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in)
1983 {
1984 uint32_t actual_table;
1985 int suc = 0;
1986 struct mcast_route_data *mr = (struct mcast_route_data *)in;
1987 struct {
1988 struct nlmsghdr n;
1989 struct ndmsg ndm;
1990 char buf[256];
1991 } req;
1992
1993 mroute = mr;
1994 struct zebra_ns *zns;
1995
1996 zns = zvrf->zns;
1997 memset(&req, 0, sizeof(req));
1998
1999 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2000 req.n.nlmsg_flags = NLM_F_REQUEST;
2001 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
2002
2003 req.ndm.ndm_family = RTNL_FAMILY_IPMR;
2004 req.n.nlmsg_type = RTM_GETROUTE;
2005
2006 nl_attr_put32(&req.n, sizeof(req), RTA_IIF, mroute->ifindex);
2007 nl_attr_put32(&req.n, sizeof(req), RTA_OIF, mroute->ifindex);
2008 nl_attr_put32(&req.n, sizeof(req), RTA_SRC, mroute->sg.src.s_addr);
2009 nl_attr_put32(&req.n, sizeof(req), RTA_DST, mroute->sg.grp.s_addr);
2010 /*
2011 * What?
2012 *
2013 * So during the namespace cleanup we started storing
2014 * the zvrf table_id for the default table as RT_TABLE_MAIN
2015 * which is what the normal routing table for ip routing is.
2016 * This change caused this to break our lookups of sg data
2017 * because prior to this change the zvrf->table_id was 0
2018 * and when the pim multicast kernel code saw a 0,
2019 * it was auto-translated to RT_TABLE_DEFAULT. But since
2020 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2021 * and the kernel goes screw you and the delicious cookies you
2022 * are trying to give me. So now we have this little hack.
2023 */
2024 actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT :
2025 zvrf->table_id;
2026 nl_attr_put32(&req.n, sizeof(req), RTA_TABLE, actual_table);
2027
2028 suc = netlink_talk(netlink_route_change_read_multicast, &req.n,
2029 &zns->netlink_cmd, zns, 0);
2030
2031 mroute = NULL;
2032 return suc;
2033 }
2034
2035 /* Char length to debug ID with */
2036 #define ID_LENGTH 10
2037
2038 static bool _netlink_nexthop_build_group(struct nlmsghdr *n, size_t req_size,
2039 uint32_t id,
2040 const struct nh_grp *z_grp,
2041 const uint8_t count)
2042 {
2043 struct nexthop_grp grp[count];
2044 /* Need space for max group size, "/", and null term */
2045 char buf[(MULTIPATH_NUM * (ID_LENGTH + 1)) + 1];
2046 char buf1[ID_LENGTH + 2];
2047
2048 buf[0] = '\0';
2049
2050 memset(grp, 0, sizeof(grp));
2051
2052 if (count) {
2053 for (int i = 0; i < count; i++) {
2054 grp[i].id = z_grp[i].id;
2055 grp[i].weight = z_grp[i].weight - 1;
2056
2057 if (IS_ZEBRA_DEBUG_KERNEL) {
2058 if (i == 0)
2059 snprintf(buf, sizeof(buf1), "group %u",
2060 grp[i].id);
2061 else {
2062 snprintf(buf1, sizeof(buf1), "/%u",
2063 grp[i].id);
2064 strlcat(buf, buf1, sizeof(buf));
2065 }
2066 }
2067 }
2068 if (!nl_attr_put(n, req_size, NHA_GROUP, grp,
2069 count * sizeof(*grp)))
2070 return false;
2071 }
2072
2073 if (IS_ZEBRA_DEBUG_KERNEL)
2074 zlog_debug("%s: ID (%u): %s", __func__, id, buf);
2075
2076 return true;
2077 }
2078
2079 /**
2080 * Next hop packet encoding helper function.
2081 *
2082 * \param[in] cmd netlink command.
2083 * \param[in] ctx dataplane context (information snapshot).
2084 * \param[out] buf buffer to hold the packet.
2085 * \param[in] buflen amount of buffer bytes.
2086 *
2087 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2088 * otherwise the number of bytes written to buf.
2089 */
2090 ssize_t netlink_nexthop_msg_encode(uint16_t cmd,
2091 const struct zebra_dplane_ctx *ctx,
2092 void *buf, size_t buflen)
2093 {
2094 struct {
2095 struct nlmsghdr n;
2096 struct nhmsg nhm;
2097 char buf[];
2098 } *req = buf;
2099
2100 mpls_lse_t out_lse[MPLS_MAX_LABELS];
2101 char label_buf[256];
2102 int num_labels = 0;
2103 uint32_t id = dplane_ctx_get_nhe_id(ctx);
2104 int type = dplane_ctx_get_nhe_type(ctx);
2105 struct rtattr *nest;
2106 uint16_t encap;
2107
2108 if (!id) {
2109 flog_err(
2110 EC_ZEBRA_NHG_FIB_UPDATE,
2111 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2112 return -1;
2113 }
2114
2115 /*
2116 * Nothing to do if the kernel doesn't support nexthop objects or
2117 * we dont want to install this type of NHG
2118 */
2119 if (!kernel_nexthops_supported()) {
2120 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2121 zlog_debug(
2122 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2123 __func__, id, zebra_route_string(type));
2124 return 0;
2125 }
2126
2127 if (proto_nexthops_only() && !is_proto_nhg(id, type)) {
2128 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2129 zlog_debug(
2130 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2131 __func__, id, zebra_route_string(type));
2132 return 0;
2133 }
2134
2135 label_buf[0] = '\0';
2136
2137 if (buflen < sizeof(*req))
2138 return 0;
2139
2140 memset(req, 0, sizeof(*req));
2141
2142 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2143 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
2144
2145 if (cmd == RTM_NEWNEXTHOP)
2146 req->n.nlmsg_flags |= NLM_F_REPLACE;
2147
2148 req->n.nlmsg_type = cmd;
2149 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
2150
2151 req->nhm.nh_family = AF_UNSPEC;
2152 /* TODO: Scope? */
2153
2154 if (!nl_attr_put32(&req->n, buflen, NHA_ID, id))
2155 return 0;
2156
2157 if (cmd == RTM_NEWNEXTHOP) {
2158 /*
2159 * We distinguish between a "group", which is a collection
2160 * of ids, and a singleton nexthop with an id. The
2161 * group is installed as an id that just refers to a list of
2162 * other ids.
2163 */
2164 if (dplane_ctx_get_nhe_nh_grp_count(ctx)) {
2165 if (!_netlink_nexthop_build_group(
2166 &req->n, buflen, id,
2167 dplane_ctx_get_nhe_nh_grp(ctx),
2168 dplane_ctx_get_nhe_nh_grp_count(ctx)))
2169 return 0;
2170 } else {
2171 const struct nexthop *nh =
2172 dplane_ctx_get_nhe_ng(ctx)->nexthop;
2173 afi_t afi = dplane_ctx_get_nhe_afi(ctx);
2174
2175 if (afi == AFI_IP)
2176 req->nhm.nh_family = AF_INET;
2177 else if (afi == AFI_IP6)
2178 req->nhm.nh_family = AF_INET6;
2179
2180 switch (nh->type) {
2181 case NEXTHOP_TYPE_IPV4:
2182 case NEXTHOP_TYPE_IPV4_IFINDEX:
2183 if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY,
2184 &nh->gate.ipv4,
2185 IPV4_MAX_BYTELEN))
2186 return 0;
2187 break;
2188 case NEXTHOP_TYPE_IPV6:
2189 case NEXTHOP_TYPE_IPV6_IFINDEX:
2190 if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY,
2191 &nh->gate.ipv6,
2192 IPV6_MAX_BYTELEN))
2193 return 0;
2194 break;
2195 case NEXTHOP_TYPE_BLACKHOLE:
2196 if (!nl_attr_put(&req->n, buflen, NHA_BLACKHOLE,
2197 NULL, 0))
2198 return 0;
2199 /* Blackhole shouldn't have anymore attributes
2200 */
2201 goto nexthop_done;
2202 case NEXTHOP_TYPE_IFINDEX:
2203 /* Don't need anymore info for this */
2204 break;
2205 }
2206
2207 if (!nh->ifindex) {
2208 flog_err(
2209 EC_ZEBRA_NHG_FIB_UPDATE,
2210 "Context received for kernel nexthop update without an interface");
2211 return -1;
2212 }
2213
2214 if (!nl_attr_put32(&req->n, buflen, NHA_OIF,
2215 nh->ifindex))
2216 return 0;
2217
2218 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK))
2219 req->nhm.nh_flags |= RTNH_F_ONLINK;
2220
2221 num_labels =
2222 build_label_stack(nh->nh_label, out_lse,
2223 label_buf, sizeof(label_buf));
2224
2225 if (num_labels) {
2226 /* Set the BoS bit */
2227 out_lse[num_labels - 1] |=
2228 htonl(1 << MPLS_LS_S_SHIFT);
2229
2230 /*
2231 * TODO: MPLS unsupported for now in kernel.
2232 */
2233 if (req->nhm.nh_family == AF_MPLS)
2234 goto nexthop_done;
2235
2236 encap = LWTUNNEL_ENCAP_MPLS;
2237 if (!nl_attr_put16(&req->n, buflen,
2238 NHA_ENCAP_TYPE, encap))
2239 return 0;
2240 nest = nl_attr_nest(&req->n, buflen, NHA_ENCAP);
2241 if (!nest)
2242 return 0;
2243 if (!nl_attr_put(
2244 &req->n, buflen, MPLS_IPTUNNEL_DST,
2245 &out_lse,
2246 num_labels * sizeof(mpls_lse_t)))
2247 return 0;
2248
2249 nl_attr_nest_end(&req->n, nest);
2250 }
2251
2252 nexthop_done:
2253
2254 if (IS_ZEBRA_DEBUG_KERNEL)
2255 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2256 __func__, id, nh, nh->ifindex,
2257 vrf_id_to_name(nh->vrf_id),
2258 nh->vrf_id, label_buf);
2259 }
2260
2261 req->nhm.nh_protocol = zebra2proto(type);
2262
2263 } else if (cmd != RTM_DELNEXTHOP) {
2264 flog_err(
2265 EC_ZEBRA_NHG_FIB_UPDATE,
2266 "Nexthop group kernel update command (%d) does not exist",
2267 cmd);
2268 return -1;
2269 }
2270
2271 if (IS_ZEBRA_DEBUG_KERNEL)
2272 zlog_debug("%s: %s, id=%u", __func__, nl_msg_type_to_str(cmd),
2273 id);
2274
2275 return NLMSG_ALIGN(req->n.nlmsg_len);
2276 }
2277
2278 static ssize_t netlink_nexthop_msg_encoder(struct zebra_dplane_ctx *ctx,
2279 void *buf, size_t buflen)
2280 {
2281 enum dplane_op_e op;
2282 int cmd = 0;
2283
2284 op = dplane_ctx_get_op(ctx);
2285 if (op == DPLANE_OP_NH_INSTALL || op == DPLANE_OP_NH_UPDATE)
2286 cmd = RTM_NEWNEXTHOP;
2287 else if (op == DPLANE_OP_NH_DELETE)
2288 cmd = RTM_DELNEXTHOP;
2289 else {
2290 flog_err(EC_ZEBRA_NHG_FIB_UPDATE,
2291 "Context received for kernel nexthop update with incorrect OP code (%u)",
2292 op);
2293 return -1;
2294 }
2295
2296 return netlink_nexthop_msg_encode(cmd, ctx, buf, buflen);
2297 }
2298
2299 enum netlink_msg_status
2300 netlink_put_nexthop_update_msg(struct nl_batch *bth,
2301 struct zebra_dplane_ctx *ctx)
2302 {
2303 /* Nothing to do if the kernel doesn't support nexthop objects */
2304 if (!kernel_nexthops_supported())
2305 return FRR_NETLINK_SUCCESS;
2306
2307 return netlink_batch_add_msg(bth, ctx, netlink_nexthop_msg_encoder,
2308 false);
2309 }
2310
2311 static ssize_t netlink_newroute_msg_encoder(struct zebra_dplane_ctx *ctx,
2312 void *buf, size_t buflen)
2313 {
2314 return netlink_route_multipath_msg_encode(RTM_NEWROUTE, ctx, buf,
2315 buflen, false, false);
2316 }
2317
2318 static ssize_t netlink_delroute_msg_encoder(struct zebra_dplane_ctx *ctx,
2319 void *buf, size_t buflen)
2320 {
2321 return netlink_route_multipath_msg_encode(RTM_DELROUTE, ctx, buf,
2322 buflen, false, false);
2323 }
2324
2325 enum netlink_msg_status
2326 netlink_put_route_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
2327 {
2328 int cmd;
2329 const struct prefix *p = dplane_ctx_get_dest(ctx);
2330
2331 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
2332 cmd = RTM_DELROUTE;
2333 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
2334 cmd = RTM_NEWROUTE;
2335 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
2336
2337 if (p->family == AF_INET || v6_rr_semantics) {
2338 /* Single 'replace' operation */
2339
2340 /*
2341 * With route replace semantics in place
2342 * for v4 routes and the new route is a system
2343 * route we do not install anything.
2344 * The problem here is that the new system
2345 * route should cause us to withdraw from
2346 * the kernel the old non-system route
2347 */
2348 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx))
2349 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2350 netlink_batch_add_msg(
2351 bth, ctx, netlink_delroute_msg_encoder,
2352 true);
2353 } else {
2354 /*
2355 * So v6 route replace semantics are not in
2356 * the kernel at this point as I understand it.
2357 * so let's do a delete then an add.
2358 * In the future once v6 route replace semantics
2359 * are in we can figure out what to do here to
2360 * allow working with old and new kernels.
2361 *
2362 * I'm also intentionally ignoring the failure case
2363 * of the route delete. If that happens yeah we're
2364 * screwed.
2365 */
2366 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2367 netlink_batch_add_msg(
2368 bth, ctx, netlink_delroute_msg_encoder,
2369 true);
2370 }
2371
2372 cmd = RTM_NEWROUTE;
2373 } else
2374 return FRR_NETLINK_ERROR;
2375
2376 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)))
2377 return FRR_NETLINK_SUCCESS;
2378
2379 return netlink_batch_add_msg(bth, ctx,
2380 cmd == RTM_NEWROUTE
2381 ? netlink_newroute_msg_encoder
2382 : netlink_delroute_msg_encoder,
2383 false);
2384 }
2385
2386 /**
2387 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2388 *
2389 * @tb: Netlink RTA data
2390 * @family: Address family in the nhmsg
2391 * @ifp: Interface connected - this should be NULL, we fill it in
2392 * @ns_id: Namspace id
2393 *
2394 * Return: New nexthop
2395 */
2396 static struct nexthop netlink_nexthop_process_nh(struct rtattr **tb,
2397 unsigned char family,
2398 struct interface **ifp,
2399 ns_id_t ns_id)
2400 {
2401 struct nexthop nh = {};
2402 void *gate = NULL;
2403 enum nexthop_types_t type = 0;
2404 int if_index = 0;
2405 size_t sz = 0;
2406 struct interface *ifp_lookup;
2407
2408 if_index = *(int *)RTA_DATA(tb[NHA_OIF]);
2409
2410
2411 if (tb[NHA_GATEWAY]) {
2412 switch (family) {
2413 case AF_INET:
2414 type = NEXTHOP_TYPE_IPV4_IFINDEX;
2415 sz = 4;
2416 break;
2417 case AF_INET6:
2418 type = NEXTHOP_TYPE_IPV6_IFINDEX;
2419 sz = 16;
2420 break;
2421 default:
2422 flog_warn(
2423 EC_ZEBRA_BAD_NHG_MESSAGE,
2424 "Nexthop gateway with bad address family (%d) received from kernel",
2425 family);
2426 return nh;
2427 }
2428 gate = RTA_DATA(tb[NHA_GATEWAY]);
2429 } else
2430 type = NEXTHOP_TYPE_IFINDEX;
2431
2432 if (type)
2433 nh.type = type;
2434
2435 if (gate)
2436 memcpy(&(nh.gate), gate, sz);
2437
2438 if (if_index)
2439 nh.ifindex = if_index;
2440
2441 ifp_lookup =
2442 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), nh.ifindex);
2443
2444 if (ifp)
2445 *ifp = ifp_lookup;
2446 if (ifp_lookup)
2447 nh.vrf_id = ifp_lookup->vrf_id;
2448 else {
2449 flog_warn(
2450 EC_ZEBRA_UNKNOWN_INTERFACE,
2451 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2452 __func__, nh.ifindex);
2453
2454 nh.vrf_id = VRF_DEFAULT;
2455 }
2456
2457 if (tb[NHA_ENCAP] && tb[NHA_ENCAP_TYPE]) {
2458 uint16_t encap_type = *(uint16_t *)RTA_DATA(tb[NHA_ENCAP_TYPE]);
2459 int num_labels = 0;
2460
2461 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
2462
2463 if (encap_type == LWTUNNEL_ENCAP_MPLS)
2464 num_labels = parse_encap_mpls(tb[NHA_ENCAP], labels);
2465
2466 if (num_labels)
2467 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels,
2468 labels);
2469 }
2470
2471 return nh;
2472 }
2473
2474 static int netlink_nexthop_process_group(struct rtattr **tb,
2475 struct nh_grp *z_grp, int z_grp_size)
2476 {
2477 uint8_t count = 0;
2478 /* linux/nexthop.h group struct */
2479 struct nexthop_grp *n_grp = NULL;
2480
2481 n_grp = (struct nexthop_grp *)RTA_DATA(tb[NHA_GROUP]);
2482 count = (RTA_PAYLOAD(tb[NHA_GROUP]) / sizeof(*n_grp));
2483
2484 if (!count || (count * sizeof(*n_grp)) != RTA_PAYLOAD(tb[NHA_GROUP])) {
2485 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE,
2486 "Invalid nexthop group received from the kernel");
2487 return count;
2488 }
2489
2490 for (int i = 0; ((i < count) && (i < z_grp_size)); i++) {
2491 z_grp[i].id = n_grp[i].id;
2492 z_grp[i].weight = n_grp[i].weight + 1;
2493 }
2494 return count;
2495 }
2496
2497 /**
2498 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2499 *
2500 * @h: Netlink message header
2501 * @ns_id: Namspace id
2502 * @startup: Are we reading under startup conditions?
2503 *
2504 * Return: Result status
2505 */
2506 int netlink_nexthop_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2507 {
2508 int len;
2509 /* nexthop group id */
2510 uint32_t id;
2511 unsigned char family;
2512 int type;
2513 afi_t afi = AFI_UNSPEC;
2514 vrf_id_t vrf_id = VRF_DEFAULT;
2515 struct interface *ifp = NULL;
2516 struct nhmsg *nhm = NULL;
2517 struct nexthop nh = {};
2518 struct nh_grp grp[MULTIPATH_NUM] = {};
2519 /* Count of nexthops in group array */
2520 uint8_t grp_count = 0;
2521 struct rtattr *tb[NHA_MAX + 1] = {};
2522
2523 nhm = NLMSG_DATA(h);
2524
2525 if (ns_id)
2526 vrf_id = ns_id;
2527
2528 if (startup && h->nlmsg_type != RTM_NEWNEXTHOP)
2529 return 0;
2530
2531 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct nhmsg));
2532 if (len < 0) {
2533 zlog_warn(
2534 "%s: Message received from netlink is of a broken size %d %zu",
2535 __func__, h->nlmsg_len,
2536 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg)));
2537 return -1;
2538 }
2539
2540 netlink_parse_rtattr(tb, NHA_MAX, RTM_NHA(nhm), len);
2541
2542
2543 if (!tb[NHA_ID]) {
2544 flog_warn(
2545 EC_ZEBRA_BAD_NHG_MESSAGE,
2546 "Nexthop group without an ID received from the kernel");
2547 return -1;
2548 }
2549
2550 /* We use the ID key'd nhg table for kernel updates */
2551 id = *((uint32_t *)RTA_DATA(tb[NHA_ID]));
2552
2553 if (zebra_evpn_mh_is_fdb_nh(id)) {
2554 /* If this is a L2 NH just ignore it */
2555 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
2556 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2557 h->nlmsg_type, id);
2558 }
2559 return 0;
2560 }
2561
2562 family = nhm->nh_family;
2563 afi = family2afi(family);
2564
2565 type = proto2zebra(nhm->nh_protocol, 0, true);
2566
2567 if (IS_ZEBRA_DEBUG_KERNEL)
2568 zlog_debug("%s ID (%u) %s NS %u",
2569 nl_msg_type_to_str(h->nlmsg_type), id,
2570 nl_family_to_str(family), ns_id);
2571
2572
2573 if (h->nlmsg_type == RTM_NEWNEXTHOP) {
2574 if (tb[NHA_GROUP]) {
2575 /**
2576 * If this is a group message its only going to have
2577 * an array of nexthop IDs associated with it
2578 */
2579 grp_count = netlink_nexthop_process_group(
2580 tb, grp, array_size(grp));
2581 } else {
2582 if (tb[NHA_BLACKHOLE]) {
2583 /**
2584 * This nexthop is just for blackhole-ing
2585 * traffic, it should not have an OIF, GATEWAY,
2586 * or ENCAP
2587 */
2588 nh.type = NEXTHOP_TYPE_BLACKHOLE;
2589 nh.bh_type = BLACKHOLE_UNSPEC;
2590 } else if (tb[NHA_OIF])
2591 /**
2592 * This is a true new nexthop, so we need
2593 * to parse the gateway and device info
2594 */
2595 nh = netlink_nexthop_process_nh(tb, family,
2596 &ifp, ns_id);
2597 else {
2598
2599 flog_warn(
2600 EC_ZEBRA_BAD_NHG_MESSAGE,
2601 "Invalid Nexthop message received from the kernel with ID (%u)",
2602 id);
2603 return -1;
2604 }
2605 SET_FLAG(nh.flags, NEXTHOP_FLAG_ACTIVE);
2606 if (nhm->nh_flags & RTNH_F_ONLINK)
2607 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
2608 vrf_id = nh.vrf_id;
2609 }
2610
2611 if (zebra_nhg_kernel_find(id, &nh, grp, grp_count, vrf_id, afi,
2612 type, startup))
2613 return -1;
2614
2615 } else if (h->nlmsg_type == RTM_DELNEXTHOP)
2616 zebra_nhg_kernel_del(id, vrf_id);
2617
2618 return 0;
2619 }
2620
2621 /**
2622 * netlink_request_nexthop() - Request nextop information from the kernel
2623 * @zns: Zebra namespace
2624 * @family: AF_* netlink family
2625 * @type: RTM_* route type
2626 *
2627 * Return: Result status
2628 */
2629 static int netlink_request_nexthop(struct zebra_ns *zns, int family, int type)
2630 {
2631 struct {
2632 struct nlmsghdr n;
2633 struct nhmsg nhm;
2634 } req;
2635
2636 /* Form the request, specifying filter (rtattr) if needed. */
2637 memset(&req, 0, sizeof(req));
2638 req.n.nlmsg_type = type;
2639 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2640 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2641 req.nhm.nh_family = family;
2642
2643 return netlink_request(&zns->netlink_cmd, &req);
2644 }
2645
2646
2647 /**
2648 * netlink_nexthop_read() - Nexthop read function using netlink interface
2649 *
2650 * @zns: Zebra name space
2651 *
2652 * Return: Result status
2653 * Only called at bootstrap time.
2654 */
2655 int netlink_nexthop_read(struct zebra_ns *zns)
2656 {
2657 int ret;
2658 struct zebra_dplane_info dp_info;
2659
2660 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2661
2662 /* Get nexthop objects */
2663 ret = netlink_request_nexthop(zns, AF_UNSPEC, RTM_GETNEXTHOP);
2664 if (ret < 0)
2665 return ret;
2666 ret = netlink_parse_info(netlink_nexthop_change, &zns->netlink_cmd,
2667 &dp_info, 0, 1);
2668
2669 if (!ret)
2670 /* If we succesfully read in nexthop objects,
2671 * this kernel must support them.
2672 */
2673 supports_nh = true;
2674
2675 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2676 zlog_debug("Nexthop objects %ssupported on this kernel",
2677 supports_nh ? "" : "not ");
2678
2679 return ret;
2680 }
2681
2682
2683 int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
2684 int llalen, ns_id_t ns_id)
2685 {
2686 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
2687 addr, lla, llalen, ns_id);
2688 }
2689
2690 /**
2691 * netlink_neigh_update_msg_encode() - Common helper api for encoding
2692 * evpn neighbor update as netlink messages using dataplane context object.
2693 * Here, a neighbor refers to a bridge forwarding database entry for
2694 * either unicast forwarding or head-end replication or an IP neighbor
2695 * entry.
2696 * @ctx: Dataplane context
2697 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
2698 * @mac: A neighbor cache link layer address
2699 * @ip: A neighbor cache n/w layer destination address
2700 * In the case of bridge FDB, this represnts the remote
2701 * VTEP IP.
2702 * @replace_obj: Whether NEW request should replace existing object or
2703 * add to the end of the list
2704 * @family: AF_* netlink family
2705 * @type: RTN_* route type
2706 * @flags: NTF_* flags
2707 * @state: NUD_* states
2708 * @data: data buffer pointer
2709 * @datalen: total amount of data buffer space
2710 *
2711 * Return: 0 when the msg doesn't fit entirely in the buffer
2712 * otherwise the number of bytes written to buf.
2713 */
2714 static ssize_t netlink_neigh_update_msg_encode(
2715 const struct zebra_dplane_ctx *ctx, int cmd, const struct ethaddr *mac,
2716 const struct ipaddr *ip, bool replace_obj, uint8_t family, uint8_t type,
2717 uint8_t flags, uint16_t state, uint32_t nhg_id, bool nfy,
2718 uint8_t nfy_flags, bool ext, uint32_t ext_flags, void *data,
2719 size_t datalen)
2720 {
2721 uint8_t protocol = RTPROT_ZEBRA;
2722 struct {
2723 struct nlmsghdr n;
2724 struct ndmsg ndm;
2725 char buf[];
2726 } *req = data;
2727 int ipa_len;
2728 enum dplane_op_e op;
2729
2730 if (datalen < sizeof(*req))
2731 return 0;
2732 memset(req, 0, sizeof(*req));
2733
2734 op = dplane_ctx_get_op(ctx);
2735
2736 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2737 req->n.nlmsg_flags = NLM_F_REQUEST;
2738 if (cmd == RTM_NEWNEIGH)
2739 req->n.nlmsg_flags |=
2740 NLM_F_CREATE
2741 | (replace_obj ? NLM_F_REPLACE : NLM_F_APPEND);
2742 req->n.nlmsg_type = cmd;
2743 req->ndm.ndm_family = family;
2744 req->ndm.ndm_type = type;
2745 req->ndm.ndm_state = state;
2746 req->ndm.ndm_flags = flags;
2747 req->ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
2748
2749 if (!nl_attr_put(&req->n, datalen, NDA_PROTOCOL, &protocol,
2750 sizeof(protocol)))
2751 return 0;
2752
2753 if (mac) {
2754 if (!nl_attr_put(&req->n, datalen, NDA_LLADDR, mac, 6))
2755 return 0;
2756 }
2757
2758 if (nfy) {
2759 struct rtattr *nest;
2760
2761 nest = nl_attr_nest(&req->n, datalen,
2762 NDA_FDB_EXT_ATTRS | NLA_F_NESTED);
2763 if (!nest)
2764 return 0;
2765
2766 if (!nl_attr_put(&req->n, datalen, NFEA_ACTIVITY_NOTIFY,
2767 &nfy_flags, sizeof(nfy_flags)))
2768 return 0;
2769 if (!nl_attr_put(&req->n, datalen, NFEA_DONT_REFRESH, NULL, 0))
2770 return 0;
2771
2772 nl_attr_nest_end(&req->n, nest);
2773 }
2774
2775
2776 if (ext) {
2777 if (!nl_attr_put(&req->n, datalen, NDA_EXT_FLAGS, &ext_flags,
2778 sizeof(ext_flags)))
2779 return 0;
2780 }
2781
2782 if (nhg_id) {
2783 if (!nl_attr_put32(&req->n, datalen, NDA_NH_ID, nhg_id))
2784 return 0;
2785 } else {
2786 ipa_len =
2787 IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
2788 if (!nl_attr_put(&req->n, datalen, NDA_DST, &ip->ip.addr,
2789 ipa_len))
2790 return 0;
2791 }
2792
2793 if (op == DPLANE_OP_MAC_INSTALL || op == DPLANE_OP_MAC_DELETE) {
2794 vlanid_t vid = dplane_ctx_mac_get_vlan(ctx);
2795
2796 if (vid > 0) {
2797 if (!nl_attr_put16(&req->n, datalen, NDA_VLAN, vid))
2798 return 0;
2799 }
2800
2801 if (!nl_attr_put32(&req->n, datalen, NDA_MASTER,
2802 dplane_ctx_mac_get_br_ifindex(ctx)))
2803 return 0;
2804 }
2805
2806 return NLMSG_ALIGN(req->n.nlmsg_len);
2807 }
2808
2809 /*
2810 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2811 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2812 */
2813 static ssize_t
2814 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx *ctx, int cmd,
2815 void *buf, size_t buflen)
2816 {
2817 struct ethaddr dst_mac = {.octet = {0}};
2818
2819 return netlink_neigh_update_msg_encode(
2820 ctx, cmd, &dst_mac, dplane_ctx_neigh_get_ipaddr(ctx), false,
2821 PF_BRIDGE, 0, NTF_SELF, (NUD_NOARP | NUD_PERMANENT), 0 /*nhg*/,
2822 false /*nfy*/, 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/,
2823 buf, buflen);
2824 }
2825
2826 #ifndef NDA_RTA
2827 #define NDA_RTA(r) \
2828 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2829 #endif
2830
2831 static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2832 {
2833 struct ndmsg *ndm;
2834 struct interface *ifp;
2835 struct zebra_if *zif;
2836 struct rtattr *tb[NDA_MAX + 1];
2837 struct interface *br_if;
2838 struct ethaddr mac;
2839 vlanid_t vid = 0;
2840 struct in_addr vtep_ip;
2841 int vid_present = 0, dst_present = 0;
2842 char buf[ETHER_ADDR_STRLEN];
2843 char vid_buf[20];
2844 char dst_buf[30];
2845 bool sticky;
2846 bool local_inactive = false;
2847 bool dp_static = false;
2848 uint32_t nhg_id = 0;
2849
2850 ndm = NLMSG_DATA(h);
2851
2852 /* We only process macfdb notifications if EVPN is enabled */
2853 if (!is_evpn_enabled())
2854 return 0;
2855
2856 /* Parse attributes and extract fields of interest. Do basic
2857 * validation of the fields.
2858 */
2859 memset(tb, 0, sizeof tb);
2860 netlink_parse_rtattr_flags(tb, NDA_MAX, NDA_RTA(ndm), len,
2861 NLA_F_NESTED);
2862
2863 if (!tb[NDA_LLADDR]) {
2864 if (IS_ZEBRA_DEBUG_KERNEL)
2865 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2866 nl_msg_type_to_str(h->nlmsg_type),
2867 ndm->ndm_ifindex);
2868 return 0;
2869 }
2870
2871 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
2872 if (IS_ZEBRA_DEBUG_KERNEL)
2873 zlog_debug(
2874 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2875 nl_msg_type_to_str(h->nlmsg_type), ndm->ndm_ifindex,
2876 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
2877 return 0;
2878 }
2879
2880 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
2881
2882 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
2883 vid_present = 1;
2884 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
2885 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
2886 }
2887
2888 if (tb[NDA_DST]) {
2889 /* TODO: Only IPv4 supported now. */
2890 dst_present = 1;
2891 memcpy(&vtep_ip.s_addr, RTA_DATA(tb[NDA_DST]),
2892 IPV4_MAX_BYTELEN);
2893 snprintfrr(dst_buf, sizeof(dst_buf), " dst %pI4",
2894 &vtep_ip);
2895 }
2896
2897 if (tb[NDA_NH_ID])
2898 nhg_id = *(uint32_t *)RTA_DATA(tb[NDA_NH_ID]);
2899
2900 if (ndm->ndm_state & NUD_STALE)
2901 local_inactive = true;
2902
2903 if (tb[NDA_FDB_EXT_ATTRS]) {
2904 struct rtattr *attr = tb[NDA_FDB_EXT_ATTRS];
2905 struct rtattr *nfea_tb[NFEA_MAX + 1] = {0};
2906
2907 netlink_parse_rtattr_nested(nfea_tb, NFEA_MAX, attr);
2908 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
2909 uint8_t nfy_flags;
2910
2911 nfy_flags = *(uint8_t *)RTA_DATA(
2912 nfea_tb[NFEA_ACTIVITY_NOTIFY]);
2913 if (nfy_flags & FDB_NOTIFY_BIT)
2914 dp_static = true;
2915 if (nfy_flags & FDB_NOTIFY_INACTIVE_BIT)
2916 local_inactive = true;
2917 }
2918 }
2919
2920 if (IS_ZEBRA_DEBUG_KERNEL)
2921 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %s%s nhg %d",
2922 nl_msg_type_to_str(h->nlmsg_type),
2923 ndm->ndm_ifindex, vid_present ? vid_buf : "",
2924 ndm->ndm_state, ndm->ndm_flags,
2925 prefix_mac2str(&mac, buf, sizeof(buf)),
2926 dst_present ? dst_buf : "", nhg_id);
2927
2928 /* The interface should exist. */
2929 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
2930 ndm->ndm_ifindex);
2931 if (!ifp || !ifp->info)
2932 return 0;
2933
2934 /* The interface should be something we're interested in. */
2935 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
2936 return 0;
2937
2938 zif = (struct zebra_if *)ifp->info;
2939 if ((br_if = zif->brslave_info.br_if) == NULL) {
2940 if (IS_ZEBRA_DEBUG_KERNEL)
2941 zlog_debug(
2942 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2943 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
2944 ndm->ndm_ifindex,
2945 zif->brslave_info.bridge_ifindex);
2946 return 0;
2947 }
2948
2949 sticky = !!(ndm->ndm_flags & NTF_STICKY);
2950
2951 if (filter_vlan && vid != filter_vlan) {
2952 if (IS_ZEBRA_DEBUG_KERNEL)
2953 zlog_debug(" Filtered due to filter vlan: %d",
2954 filter_vlan);
2955 return 0;
2956 }
2957
2958 /* If add or update, do accordingly if learnt on a "local" interface; if
2959 * the notification is over VxLAN, this has to be related to
2960 * multi-homing,
2961 * so perform an implicit delete of any local entry (if it exists).
2962 */
2963 if (h->nlmsg_type == RTM_NEWNEIGH) {
2964 /* Drop "permanent" entries. */
2965 if (ndm->ndm_state & NUD_PERMANENT) {
2966 if (IS_ZEBRA_DEBUG_KERNEL)
2967 zlog_debug(
2968 " Dropping entry because of NUD_PERMANENT");
2969 return 0;
2970 }
2971
2972 if (IS_ZEBRA_IF_VXLAN(ifp))
2973 return zebra_vxlan_dp_network_mac_add(
2974 ifp, br_if, &mac, vid, nhg_id, sticky,
2975 !!(ndm->ndm_flags & NTF_EXT_LEARNED));
2976
2977 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
2978 sticky, local_inactive, dp_static);
2979 }
2980
2981 /* This is a delete notification.
2982 * Ignore the notification with IP dest as it may just signify that the
2983 * MAC has moved from remote to local. The exception is the special
2984 * all-zeros MAC that represents the BUM flooding entry; we may have
2985 * to readd it. Otherwise,
2986 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2987 * 2. For a MAC over "local" interface, delete the mac
2988 * Note: We will get notifications from both bridge driver and VxLAN
2989 * driver.
2990 */
2991 if (nhg_id)
2992 return 0;
2993
2994 if (dst_present) {
2995 u_char zero_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2996
2997 if (!memcmp(zero_mac, mac.octet, ETH_ALEN))
2998 return zebra_vxlan_check_readd_vtep(ifp, vtep_ip);
2999 return 0;
3000 }
3001
3002 if (IS_ZEBRA_IF_VXLAN(ifp))
3003 return zebra_vxlan_dp_network_mac_del(ifp, br_if, &mac, vid);
3004
3005 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
3006 }
3007
3008 static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
3009 {
3010 int len;
3011 struct ndmsg *ndm;
3012
3013 if (h->nlmsg_type != RTM_NEWNEIGH)
3014 return 0;
3015
3016 /* Length validity. */
3017 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3018 if (len < 0)
3019 return -1;
3020
3021 /* We are interested only in AF_BRIDGE notifications. */
3022 ndm = NLMSG_DATA(h);
3023 if (ndm->ndm_family != AF_BRIDGE)
3024 return 0;
3025
3026 return netlink_macfdb_change(h, len, ns_id);
3027 }
3028
3029 /* Request for MAC FDB information from the kernel */
3030 static int netlink_request_macs(struct nlsock *netlink_cmd, int family,
3031 int type, ifindex_t master_ifindex)
3032 {
3033 struct {
3034 struct nlmsghdr n;
3035 struct ifinfomsg ifm;
3036 char buf[256];
3037 } req;
3038
3039 /* Form the request, specifying filter (rtattr) if needed. */
3040 memset(&req, 0, sizeof(req));
3041 req.n.nlmsg_type = type;
3042 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
3043 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
3044 req.ifm.ifi_family = family;
3045 if (master_ifindex)
3046 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
3047
3048 return netlink_request(netlink_cmd, &req);
3049 }
3050
3051 /*
3052 * MAC forwarding database read using netlink interface. This is invoked
3053 * at startup.
3054 */
3055 int netlink_macfdb_read(struct zebra_ns *zns)
3056 {
3057 int ret;
3058 struct zebra_dplane_info dp_info;
3059
3060 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3061
3062 /* Get bridge FDB table. */
3063 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
3064 0);
3065 if (ret < 0)
3066 return ret;
3067 /* We are reading entire table. */
3068 filter_vlan = 0;
3069 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3070 &dp_info, 0, 1);
3071
3072 return ret;
3073 }
3074
3075 /*
3076 * MAC forwarding database read using netlink interface. This is for a
3077 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3078 */
3079 int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
3080 struct interface *br_if)
3081 {
3082 struct zebra_if *br_zif;
3083 struct zebra_if *zif;
3084 struct zebra_l2info_vxlan *vxl;
3085 struct zebra_dplane_info dp_info;
3086 int ret = 0;
3087
3088 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3089
3090 /* Save VLAN we're filtering on, if needed. */
3091 br_zif = (struct zebra_if *)br_if->info;
3092 zif = (struct zebra_if *)ifp->info;
3093 vxl = &zif->l2info.vxl;
3094 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3095 filter_vlan = vxl->access_vlan;
3096
3097 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3098 */
3099 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
3100 br_if->ifindex);
3101 if (ret < 0)
3102 return ret;
3103 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3104 &dp_info, 0, 0);
3105
3106 /* Reset VLAN filter. */
3107 filter_vlan = 0;
3108 return ret;
3109 }
3110
3111
3112 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3113 static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns,
3114 int family,
3115 int type,
3116 struct interface *br_if,
3117 struct ethaddr *mac,
3118 vlanid_t vid)
3119 {
3120 struct {
3121 struct nlmsghdr n;
3122 struct ndmsg ndm;
3123 char buf[256];
3124 } req;
3125 struct zebra_if *br_zif;
3126 char buf[ETHER_ADDR_STRLEN];
3127
3128 memset(&req, 0, sizeof(req));
3129 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3130 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3131 req.n.nlmsg_flags = NLM_F_REQUEST;
3132 req.ndm.ndm_family = family; /* AF_BRIDGE */
3133 /* req.ndm.ndm_state = NUD_REACHABLE; */
3134
3135 nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
3136
3137 br_zif = (struct zebra_if *)br_if->info;
3138 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0)
3139 nl_attr_put16(&req.n, sizeof(req), NDA_VLAN, vid);
3140
3141 nl_attr_put32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
3142
3143 if (IS_ZEBRA_DEBUG_KERNEL)
3144 zlog_debug(
3145 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %s vid %u",
3146 __func__, nl_family_to_str(req.ndm.ndm_family),
3147 br_if->name, br_if->ifindex,
3148 vrf_id_to_name(br_if->vrf_id), br_if->vrf_id,
3149 prefix_mac2str(mac, buf, sizeof(buf)), vid);
3150
3151 return netlink_request(&zns->netlink_cmd, &req);
3152 }
3153
3154 int netlink_macfdb_read_specific_mac(struct zebra_ns *zns,
3155 struct interface *br_if,
3156 struct ethaddr *mac, vlanid_t vid)
3157 {
3158 int ret = 0;
3159 struct zebra_dplane_info dp_info;
3160
3161 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3162
3163 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3164 */
3165 ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE,
3166 RTM_GETNEIGH,
3167 br_if, mac, vid);
3168 if (ret < 0)
3169 return ret;
3170
3171 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3172 &dp_info, 1, 0);
3173
3174 return ret;
3175 }
3176
3177 /*
3178 * Netlink-specific handler for MAC updates using dataplane context object.
3179 */
3180 ssize_t netlink_macfdb_update_ctx(struct zebra_dplane_ctx *ctx, void *data,
3181 size_t datalen)
3182 {
3183 struct ipaddr vtep_ip;
3184 vlanid_t vid;
3185 ssize_t total;
3186 int cmd;
3187 uint8_t flags;
3188 uint16_t state;
3189 uint32_t nhg_id;
3190 uint32_t update_flags;
3191 bool nfy = false;
3192 uint8_t nfy_flags = 0;
3193
3194 cmd = dplane_ctx_get_op(ctx) == DPLANE_OP_MAC_INSTALL
3195 ? RTM_NEWNEIGH : RTM_DELNEIGH;
3196
3197 flags = NTF_MASTER;
3198 state = NUD_REACHABLE;
3199
3200 update_flags = dplane_ctx_mac_get_update_flags(ctx);
3201 if (update_flags & DPLANE_MAC_REMOTE) {
3202 flags |= NTF_SELF;
3203 if (dplane_ctx_mac_is_sticky(ctx)) {
3204 /* NUD_NOARP prevents the entry from expiring */
3205 state |= NUD_NOARP;
3206 /* sticky the entry from moving */
3207 flags |= NTF_STICKY;
3208 } else {
3209 flags |= NTF_EXT_LEARNED;
3210 }
3211 /* if it was static-local previously we need to clear the
3212 * notify flags on replace with remote
3213 */
3214 if (update_flags & DPLANE_MAC_WAS_STATIC)
3215 nfy = true;
3216 } else {
3217 /* local mac */
3218 if (update_flags & DPLANE_MAC_SET_STATIC) {
3219 nfy_flags |= FDB_NOTIFY_BIT;
3220 state |= NUD_NOARP;
3221 }
3222
3223 if (update_flags & DPLANE_MAC_SET_INACTIVE)
3224 nfy_flags |= FDB_NOTIFY_INACTIVE_BIT;
3225
3226 nfy = true;
3227 }
3228
3229 nhg_id = dplane_ctx_mac_get_nhg_id(ctx);
3230 vtep_ip.ipaddr_v4 = *(dplane_ctx_mac_get_vtep_ip(ctx));
3231 SET_IPADDR_V4(&vtep_ip);
3232
3233 if (IS_ZEBRA_DEBUG_KERNEL) {
3234 char ipbuf[PREFIX_STRLEN];
3235 char buf[ETHER_ADDR_STRLEN];
3236 char vid_buf[20];
3237 const struct ethaddr *mac = dplane_ctx_mac_get_addr(ctx);
3238
3239 vid = dplane_ctx_mac_get_vlan(ctx);
3240 if (vid > 0)
3241 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
3242 else
3243 vid_buf[0] = '\0';
3244
3245 zlog_debug(
3246 "Tx %s family %s IF %s(%u)%s %sMAC %s dst %s nhg %u%s%s%s%s%s",
3247 nl_msg_type_to_str(cmd), nl_family_to_str(AF_BRIDGE),
3248 dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx),
3249 vid_buf, dplane_ctx_mac_is_sticky(ctx) ? "sticky " : "",
3250 prefix_mac2str(mac, buf, sizeof(buf)),
3251 ipaddr2str(&vtep_ip, ipbuf, sizeof(ipbuf)), nhg_id,
3252 (update_flags & DPLANE_MAC_REMOTE) ? " rem" : "",
3253 (update_flags & DPLANE_MAC_WAS_STATIC) ? " clr_sync"
3254 : "",
3255 (update_flags & DPLANE_MAC_SET_STATIC) ? " static" : "",
3256 (update_flags & DPLANE_MAC_SET_INACTIVE) ? " inactive"
3257 : "",
3258 nfy ? " nfy" : "");
3259 }
3260
3261 total = netlink_neigh_update_msg_encode(
3262 ctx, cmd, dplane_ctx_mac_get_addr(ctx), &vtep_ip, true,
3263 AF_BRIDGE, 0, flags, state, nhg_id, nfy, nfy_flags,
3264 false /*ext*/, 0 /*ext_flags*/, data, datalen);
3265
3266 return total;
3267 }
3268
3269 /*
3270 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3271 * 5549 support, re-install them.
3272 */
3273 static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif,
3274 struct interface *ifp, struct ipaddr *ip,
3275 bool handle_failed)
3276 {
3277 if (ndm->ndm_family != AF_INET)
3278 return;
3279
3280 if (!zif->v6_2_v4_ll_neigh_entry)
3281 return;
3282
3283 if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr)
3284 return;
3285
3286 if (handle_failed && ndm->ndm_state & NUD_FAILED) {
3287 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3288 ifp->name);
3289 return;
3290 }
3291
3292 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true);
3293 }
3294
3295 #define NUD_VALID \
3296 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3297 | NUD_DELAY)
3298 #define NUD_LOCAL_ACTIVE \
3299 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3300
3301 static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
3302 {
3303 struct ndmsg *ndm;
3304 struct interface *ifp;
3305 struct zebra_if *zif;
3306 struct rtattr *tb[NDA_MAX + 1];
3307 struct interface *link_if;
3308 struct ethaddr mac;
3309 struct ipaddr ip;
3310 struct vrf *vrf;
3311 char buf[ETHER_ADDR_STRLEN];
3312 char buf2[INET6_ADDRSTRLEN];
3313 int mac_present = 0;
3314 bool is_ext;
3315 bool is_router;
3316 bool local_inactive;
3317 uint32_t ext_flags = 0;
3318 bool dp_static = false;
3319
3320 ndm = NLMSG_DATA(h);
3321
3322 /* The interface should exist. */
3323 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
3324 ndm->ndm_ifindex);
3325 if (!ifp || !ifp->info)
3326 return 0;
3327
3328 vrf = vrf_lookup_by_id(ifp->vrf_id);
3329 zif = (struct zebra_if *)ifp->info;
3330
3331 /* Parse attributes and extract fields of interest. */
3332 memset(tb, 0, sizeof(tb));
3333 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
3334
3335 if (!tb[NDA_DST]) {
3336 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3337 nl_msg_type_to_str(h->nlmsg_type),
3338 nl_family_to_str(ndm->ndm_family), ifp->name,
3339 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id);
3340 return 0;
3341 }
3342
3343 memset(&ip, 0, sizeof(struct ipaddr));
3344 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
3345 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
3346
3347 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3348 if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) {
3349 netlink_handle_5549(ndm, zif, ifp, &ip, false);
3350 if (IS_ZEBRA_DEBUG_KERNEL)
3351 zlog_debug(
3352 " Neighbor Entry Received is a 5549 entry, finished");
3353 return 0;
3354 }
3355
3356 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3357 if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID))
3358 netlink_handle_5549(ndm, zif, ifp, &ip, true);
3359
3360 /* The neighbor is present on an SVI. From this, we locate the
3361 * underlying
3362 * bridge because we're only interested in neighbors on a VxLAN bridge.
3363 * The bridge is located based on the nature of the SVI:
3364 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3365 * interface
3366 * and is linked to the bridge
3367 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3368 * inteface
3369 * itself
3370 */
3371 if (IS_ZEBRA_IF_VLAN(ifp)) {
3372 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
3373 zif->link_ifindex);
3374 if (!link_if)
3375 return 0;
3376 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
3377 link_if = ifp;
3378 else {
3379 if (IS_ZEBRA_DEBUG_KERNEL)
3380 zlog_debug(
3381 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3382 return 0;
3383 }
3384
3385 memset(&mac, 0, sizeof(struct ethaddr));
3386 if (h->nlmsg_type == RTM_NEWNEIGH) {
3387 if (tb[NDA_LLADDR]) {
3388 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
3389 if (IS_ZEBRA_DEBUG_KERNEL)
3390 zlog_debug(
3391 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3392 nl_msg_type_to_str(
3393 h->nlmsg_type),
3394 nl_family_to_str(
3395 ndm->ndm_family),
3396 ifp->name, ndm->ndm_ifindex,
3397 VRF_LOGNAME(vrf), ifp->vrf_id,
3398 (unsigned long)RTA_PAYLOAD(
3399 tb[NDA_LLADDR]));
3400 return 0;
3401 }
3402
3403 mac_present = 1;
3404 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
3405 }
3406
3407 is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED);
3408 is_router = !!(ndm->ndm_flags & NTF_ROUTER);
3409
3410 if (tb[NDA_EXT_FLAGS]) {
3411 ext_flags = *(uint32_t *)RTA_DATA(tb[NDA_EXT_FLAGS]);
3412 if (ext_flags & NTF_E_MH_PEER_SYNC)
3413 dp_static = true;
3414 }
3415
3416 if (IS_ZEBRA_DEBUG_KERNEL)
3417 zlog_debug(
3418 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %s MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3419 nl_msg_type_to_str(h->nlmsg_type),
3420 nl_family_to_str(ndm->ndm_family), ifp->name,
3421 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id,
3422 ipaddr2str(&ip, buf2, sizeof(buf2)),
3423 mac_present
3424 ? prefix_mac2str(&mac, buf, sizeof(buf))
3425 : "",
3426 ndm->ndm_state, ndm->ndm_flags, ext_flags);
3427
3428 /* If the neighbor state is valid for use, process as an add or
3429 * update
3430 * else process as a delete. Note that the delete handling may
3431 * result
3432 * in re-adding the neighbor if it is a valid "remote" neighbor.
3433 */
3434 if (ndm->ndm_state & NUD_VALID) {
3435 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3436 local_inactive =
3437 !(ndm->ndm_state & NUD_LOCAL_ACTIVE);
3438 else
3439 /* If EVPN-MH is not enabled we treat STALE
3440 * neighbors as locally-active and advertise
3441 * them
3442 */
3443 local_inactive = false;
3444
3445 return zebra_vxlan_handle_kernel_neigh_update(
3446 ifp, link_if, &ip, &mac, ndm->ndm_state, is_ext,
3447 is_router, local_inactive, dp_static);
3448 }
3449
3450 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
3451 }
3452
3453 if (IS_ZEBRA_DEBUG_KERNEL)
3454 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %s",
3455 nl_msg_type_to_str(h->nlmsg_type),
3456 nl_family_to_str(ndm->ndm_family), ifp->name,
3457 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id,
3458 ipaddr2str(&ip, buf2, sizeof(buf2)));
3459
3460 /* Process the delete - it may result in re-adding the neighbor if it is
3461 * a valid "remote" neighbor.
3462 */
3463 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
3464 }
3465
3466 static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
3467 {
3468 int len;
3469 struct ndmsg *ndm;
3470
3471 if (h->nlmsg_type != RTM_NEWNEIGH)
3472 return 0;
3473
3474 /* Length validity. */
3475 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3476 if (len < 0)
3477 return -1;
3478
3479 /* We are interested only in AF_INET or AF_INET6 notifications. */
3480 ndm = NLMSG_DATA(h);
3481 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
3482 return 0;
3483
3484 return netlink_neigh_change(h, len);
3485 }
3486
3487 /* Request for IP neighbor information from the kernel */
3488 static int netlink_request_neigh(struct nlsock *netlink_cmd, int family,
3489 int type, ifindex_t ifindex)
3490 {
3491 struct {
3492 struct nlmsghdr n;
3493 struct ndmsg ndm;
3494 char buf[256];
3495 } req;
3496
3497 /* Form the request, specifying filter (rtattr) if needed. */
3498 memset(&req, 0, sizeof(req));
3499 req.n.nlmsg_type = type;
3500 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
3501 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3502 req.ndm.ndm_family = family;
3503 if (ifindex)
3504 nl_attr_put32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
3505
3506 return netlink_request(netlink_cmd, &req);
3507 }
3508
3509 /*
3510 * IP Neighbor table read using netlink interface. This is invoked
3511 * at startup.
3512 */
3513 int netlink_neigh_read(struct zebra_ns *zns)
3514 {
3515 int ret;
3516 struct zebra_dplane_info dp_info;
3517
3518 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3519
3520 /* Get IP neighbor table. */
3521 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
3522 0);
3523 if (ret < 0)
3524 return ret;
3525 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3526 &dp_info, 0, 1);
3527
3528 return ret;
3529 }
3530
3531 /*
3532 * IP Neighbor table read using netlink interface. This is for a specific
3533 * VLAN device.
3534 */
3535 int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
3536 {
3537 int ret = 0;
3538 struct zebra_dplane_info dp_info;
3539
3540 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3541
3542 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
3543 vlan_if->ifindex);
3544 if (ret < 0)
3545 return ret;
3546 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3547 &dp_info, 0, 0);
3548
3549 return ret;
3550 }
3551
3552 /*
3553 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3554 * read using netlink interface.
3555 */
3556 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns,
3557 int type, struct ipaddr *ip,
3558 ifindex_t ifindex)
3559 {
3560 struct {
3561 struct nlmsghdr n;
3562 struct ndmsg ndm;
3563 char buf[256];
3564 } req;
3565 int ipa_len;
3566
3567 /* Form the request, specifying filter (rtattr) if needed. */
3568 memset(&req, 0, sizeof(req));
3569 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3570 req.n.nlmsg_flags = NLM_F_REQUEST;
3571 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3572 req.ndm.ndm_ifindex = ifindex;
3573
3574 if (IS_IPADDR_V4(ip)) {
3575 ipa_len = IPV4_MAX_BYTELEN;
3576 req.ndm.ndm_family = AF_INET;
3577
3578 } else {
3579 ipa_len = IPV6_MAX_BYTELEN;
3580 req.ndm.ndm_family = AF_INET6;
3581 }
3582
3583 nl_attr_put(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
3584
3585 if (IS_ZEBRA_DEBUG_KERNEL) {
3586 char buf[INET6_ADDRSTRLEN];
3587
3588 zlog_debug("%s: Tx %s family %s IF %u IP %s flags 0x%x",
3589 __func__, nl_msg_type_to_str(type),
3590 nl_family_to_str(req.ndm.ndm_family), ifindex,
3591 ipaddr2str(ip, buf, sizeof(buf)), req.n.nlmsg_flags);
3592 }
3593
3594 return netlink_request(&zns->netlink_cmd, &req);
3595 }
3596
3597 int netlink_neigh_read_specific_ip(struct ipaddr *ip,
3598 struct interface *vlan_if)
3599 {
3600 int ret = 0;
3601 struct zebra_ns *zns;
3602 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vlan_if->vrf_id);
3603 char buf[INET6_ADDRSTRLEN];
3604 struct zebra_dplane_info dp_info;
3605
3606 zns = zvrf->zns;
3607
3608 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3609
3610 if (IS_ZEBRA_DEBUG_KERNEL)
3611 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf %s(%u)",
3612 __func__, vlan_if->name, vlan_if->ifindex,
3613 ipaddr2str(ip, buf, sizeof(buf)),
3614 vrf_id_to_name(vlan_if->vrf_id), vlan_if->vrf_id);
3615
3616 ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip,
3617 vlan_if->ifindex);
3618 if (ret < 0)
3619 return ret;
3620
3621 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3622 &dp_info, 1, 0);
3623
3624 return ret;
3625 }
3626
3627 int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
3628 {
3629 int len;
3630 struct ndmsg *ndm;
3631
3632 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH))
3633 return 0;
3634
3635 /* Length validity. */
3636 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3637 if (len < 0) {
3638 zlog_err(
3639 "%s: Message received from netlink is of a broken size %d %zu",
3640 __func__, h->nlmsg_len,
3641 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
3642 return -1;
3643 }
3644
3645 /* Is this a notification for the MAC FDB or IP neighbor table? */
3646 ndm = NLMSG_DATA(h);
3647 if (ndm->ndm_family == AF_BRIDGE)
3648 return netlink_macfdb_change(h, len, ns_id);
3649
3650 if (ndm->ndm_type != RTN_UNICAST)
3651 return 0;
3652
3653 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
3654 return netlink_ipneigh_change(h, len, ns_id);
3655 else {
3656 flog_warn(
3657 EC_ZEBRA_UNKNOWN_FAMILY,
3658 "Invalid address family: %u received from kernel neighbor change: %s",
3659 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
3660 return 0;
3661 }
3662
3663 return 0;
3664 }
3665
3666 /*
3667 * Utility neighbor-update function, using info from dplane context.
3668 */
3669 static ssize_t netlink_neigh_update_ctx(const struct zebra_dplane_ctx *ctx,
3670 int cmd, void *buf, size_t buflen)
3671 {
3672 const struct ipaddr *ip;
3673 const struct ethaddr *mac;
3674 uint8_t flags;
3675 uint16_t state;
3676 uint8_t family;
3677 uint32_t update_flags;
3678 uint32_t ext_flags = 0;
3679 bool ext = false;
3680
3681 ip = dplane_ctx_neigh_get_ipaddr(ctx);
3682 mac = dplane_ctx_neigh_get_mac(ctx);
3683 if (is_zero_mac(mac))
3684 mac = NULL;
3685
3686 update_flags = dplane_ctx_neigh_get_update_flags(ctx);
3687 flags = neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx));
3688 state = neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx));
3689
3690 family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
3691
3692 if (update_flags & DPLANE_NEIGH_REMOTE) {
3693 flags |= NTF_EXT_LEARNED;
3694 /* if it was static-local previously we need to clear the
3695 * ext flags on replace with remote
3696 */
3697 if (update_flags & DPLANE_NEIGH_WAS_STATIC)
3698 ext = true;
3699 } else {
3700 ext = true;
3701 /* local neigh */
3702 if (update_flags & DPLANE_NEIGH_SET_STATIC)
3703 ext_flags |= NTF_E_MH_PEER_SYNC;
3704 }
3705 if (IS_ZEBRA_DEBUG_KERNEL) {
3706 char buf[INET6_ADDRSTRLEN];
3707 char buf2[ETHER_ADDR_STRLEN];
3708
3709 zlog_debug(
3710 "Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x %sext_flags 0x%x",
3711 nl_msg_type_to_str(cmd), nl_family_to_str(family),
3712 dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx),
3713 ipaddr2str(ip, buf, sizeof(buf)),
3714 mac ? prefix_mac2str(mac, buf2, sizeof(buf2)) : "null",
3715 flags, state, ext ? "ext " : "", ext_flags);
3716 }
3717
3718 return netlink_neigh_update_msg_encode(
3719 ctx, cmd, mac, ip, true, family, RTN_UNICAST, flags, state,
3720 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext, ext_flags, buf,
3721 buflen);
3722 }
3723
3724 static ssize_t netlink_neigh_msg_encoder(struct zebra_dplane_ctx *ctx,
3725 void *buf, size_t buflen)
3726 {
3727 ssize_t ret;
3728
3729 switch (dplane_ctx_get_op(ctx)) {
3730 case DPLANE_OP_NEIGH_INSTALL:
3731 case DPLANE_OP_NEIGH_UPDATE:
3732 case DPLANE_OP_NEIGH_DISCOVER:
3733 ret = netlink_neigh_update_ctx(ctx, RTM_NEWNEIGH, buf, buflen);
3734 break;
3735 case DPLANE_OP_NEIGH_DELETE:
3736 ret = netlink_neigh_update_ctx(ctx, RTM_DELNEIGH, buf, buflen);
3737 break;
3738 case DPLANE_OP_VTEP_ADD:
3739 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_NEWNEIGH, buf,
3740 buflen);
3741 break;
3742 case DPLANE_OP_VTEP_DELETE:
3743 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_DELNEIGH, buf,
3744 buflen);
3745 break;
3746 default:
3747 ret = -1;
3748 }
3749
3750 return ret;
3751 }
3752
3753 /*
3754 * Update MAC, using dataplane context object.
3755 */
3756
3757 enum netlink_msg_status netlink_put_mac_update_msg(struct nl_batch *bth,
3758 struct zebra_dplane_ctx *ctx)
3759 {
3760 return netlink_batch_add_msg(bth, ctx, netlink_macfdb_update_ctx,
3761 false);
3762 }
3763
3764 enum netlink_msg_status
3765 netlink_put_neigh_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
3766 {
3767 return netlink_batch_add_msg(bth, ctx, netlink_neigh_msg_encoder,
3768 false);
3769 }
3770
3771 /*
3772 * MPLS label forwarding table change via netlink interface, using dataplane
3773 * context information.
3774 */
3775 ssize_t netlink_mpls_multipath_msg_encode(int cmd, struct zebra_dplane_ctx *ctx,
3776 void *buf, size_t buflen)
3777 {
3778 mpls_lse_t lse;
3779 const struct nhlfe_list_head *head;
3780 const zebra_nhlfe_t *nhlfe;
3781 struct nexthop *nexthop = NULL;
3782 unsigned int nexthop_num;
3783 const char *routedesc;
3784 int route_type;
3785 struct prefix p = {0};
3786
3787 struct {
3788 struct nlmsghdr n;
3789 struct rtmsg r;
3790 char buf[0];
3791 } *req = buf;
3792
3793 if (buflen < sizeof(*req))
3794 return 0;
3795
3796 memset(req, 0, sizeof(*req));
3797
3798 /*
3799 * Count # nexthops so we can decide whether to use singlepath
3800 * or multipath case.
3801 */
3802 nexthop_num = 0;
3803 head = dplane_ctx_get_nhlfe_list(ctx);
3804 frr_each(nhlfe_list_const, head, nhlfe) {
3805 nexthop = nhlfe->nexthop;
3806 if (!nexthop)
3807 continue;
3808 if (cmd == RTM_NEWROUTE) {
3809 /* Count all selected NHLFEs */
3810 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3811 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
3812 nexthop_num++;
3813 } else { /* DEL */
3814 /* Count all installed NHLFEs */
3815 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
3816 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
3817 nexthop_num++;
3818 }
3819 }
3820
3821 if ((nexthop_num == 0) ||
3822 (!dplane_ctx_get_best_nhlfe(ctx) && (cmd != RTM_DELROUTE)))
3823 return 0;
3824
3825 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
3826 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
3827 req->n.nlmsg_type = cmd;
3828 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
3829
3830 req->r.rtm_family = AF_MPLS;
3831 req->r.rtm_table = RT_TABLE_MAIN;
3832 req->r.rtm_dst_len = MPLS_LABEL_LEN_BITS;
3833 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
3834 req->r.rtm_type = RTN_UNICAST;
3835
3836 if (cmd == RTM_NEWROUTE) {
3837 /* We do a replace to handle update. */
3838 req->n.nlmsg_flags |= NLM_F_REPLACE;
3839
3840 /* set the protocol value if installing */
3841 route_type = re_type_from_lsp_type(
3842 dplane_ctx_get_best_nhlfe(ctx)->type);
3843 req->r.rtm_protocol = zebra2proto(route_type);
3844 }
3845
3846 /* Fill destination */
3847 lse = mpls_lse_encode(dplane_ctx_get_in_label(ctx), 0, 0, 1);
3848 if (!nl_attr_put(&req->n, buflen, RTA_DST, &lse, sizeof(mpls_lse_t)))
3849 return 0;
3850
3851 /* Fill nexthops (paths) based on single-path or multipath. The paths
3852 * chosen depend on the operation.
3853 */
3854 if (nexthop_num == 1) {
3855 routedesc = "single-path";
3856 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
3857 routedesc);
3858
3859 nexthop_num = 0;
3860 frr_each(nhlfe_list_const, head, nhlfe) {
3861 nexthop = nhlfe->nexthop;
3862 if (!nexthop)
3863 continue;
3864
3865 if ((cmd == RTM_NEWROUTE
3866 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3867 && CHECK_FLAG(nexthop->flags,
3868 NEXTHOP_FLAG_ACTIVE)))
3869 || (cmd == RTM_DELROUTE
3870 && (CHECK_FLAG(nhlfe->flags,
3871 NHLFE_FLAG_INSTALLED)
3872 && CHECK_FLAG(nexthop->flags,
3873 NEXTHOP_FLAG_FIB)))) {
3874 /* Add the gateway */
3875 if (!_netlink_mpls_build_singlepath(
3876 &p, routedesc, nhlfe, &req->n,
3877 &req->r, buflen, cmd))
3878 return false;
3879
3880 nexthop_num++;
3881 break;
3882 }
3883 }
3884 } else { /* Multipath case */
3885 struct rtattr *nest;
3886 const union g_addr *src1 = NULL;
3887
3888 nest = nl_attr_nest(&req->n, buflen, RTA_MULTIPATH);
3889 if (!nest)
3890 return 0;
3891
3892 routedesc = "multipath";
3893 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
3894 routedesc);
3895
3896 nexthop_num = 0;
3897 frr_each(nhlfe_list_const, head, nhlfe) {
3898 nexthop = nhlfe->nexthop;
3899 if (!nexthop)
3900 continue;
3901
3902 if ((cmd == RTM_NEWROUTE
3903 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3904 && CHECK_FLAG(nexthop->flags,
3905 NEXTHOP_FLAG_ACTIVE)))
3906 || (cmd == RTM_DELROUTE
3907 && (CHECK_FLAG(nhlfe->flags,
3908 NHLFE_FLAG_INSTALLED)
3909 && CHECK_FLAG(nexthop->flags,
3910 NEXTHOP_FLAG_FIB)))) {
3911 nexthop_num++;
3912
3913 /* Build the multipath */
3914 if (!_netlink_mpls_build_multipath(
3915 &p, routedesc, nhlfe, &req->n,
3916 buflen, &req->r, &src1))
3917 return 0;
3918 }
3919 }
3920
3921 /* Add the multipath */
3922 nl_attr_nest_end(&req->n, nest);
3923 }
3924
3925 return NLMSG_ALIGN(req->n.nlmsg_len);
3926 }
3927
3928 /****************************************************************************
3929 * This code was developed in a branch that didn't have dplane APIs for
3930 * MAC updates. Hence the use of the legacy style. It will be moved to
3931 * the new dplane style pre-merge to master. XXX
3932 */
3933 static int netlink_fdb_nh_update(uint32_t nh_id, struct in_addr vtep_ip)
3934 {
3935 struct {
3936 struct nlmsghdr n;
3937 struct nhmsg nhm;
3938 char buf[256];
3939 } req;
3940 int cmd = RTM_NEWNEXTHOP;
3941 struct zebra_vrf *zvrf;
3942 struct zebra_ns *zns;
3943
3944 zvrf = zebra_vrf_get_evpn();
3945 if (!zvrf)
3946 return -1;
3947 zns = zvrf->zns;
3948
3949 memset(&req, 0, sizeof(req));
3950
3951 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
3952 req.n.nlmsg_flags = NLM_F_REQUEST;
3953 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
3954 req.n.nlmsg_type = cmd;
3955 req.nhm.nh_family = AF_INET;
3956
3957 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id))
3958 return -1;
3959 if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0))
3960 return -1;
3961 if (!nl_attr_put(&req.n, sizeof(req), NHA_GATEWAY,
3962 &vtep_ip, IPV4_MAX_BYTELEN))
3963 return -1;
3964
3965 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
3966 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
3967 nl_msg_type_to_str(cmd), nh_id, &vtep_ip);
3968 }
3969
3970 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
3971 0);
3972 }
3973
3974 static int netlink_fdb_nh_del(uint32_t nh_id)
3975 {
3976 struct {
3977 struct nlmsghdr n;
3978 struct nhmsg nhm;
3979 char buf[256];
3980 } req;
3981 int cmd = RTM_DELNEXTHOP;
3982 struct zebra_vrf *zvrf;
3983 struct zebra_ns *zns;
3984
3985 zvrf = zebra_vrf_get_evpn();
3986 if (!zvrf)
3987 return -1;
3988 zns = zvrf->zns;
3989
3990 memset(&req, 0, sizeof(req));
3991
3992 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
3993 req.n.nlmsg_flags = NLM_F_REQUEST;
3994 req.n.nlmsg_type = cmd;
3995 req.nhm.nh_family = AF_UNSPEC;
3996
3997 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id))
3998 return -1;
3999
4000 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
4001 zlog_debug("Tx %s fdb-nh 0x%x",
4002 nl_msg_type_to_str(cmd), nh_id);
4003 }
4004
4005 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
4006 0);
4007 }
4008
4009 static int netlink_fdb_nhg_update(uint32_t nhg_id, uint32_t nh_cnt,
4010 struct nh_grp *nh_ids)
4011 {
4012 struct {
4013 struct nlmsghdr n;
4014 struct nhmsg nhm;
4015 char buf[256];
4016 } req;
4017 int cmd = RTM_NEWNEXTHOP;
4018 struct zebra_vrf *zvrf;
4019 struct zebra_ns *zns;
4020 struct nexthop_grp grp[nh_cnt];
4021 uint32_t i;
4022
4023 zvrf = zebra_vrf_get_evpn();
4024 if (!zvrf)
4025 return -1;
4026 zns = zvrf->zns;
4027
4028 memset(&req, 0, sizeof(req));
4029
4030 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
4031 req.n.nlmsg_flags = NLM_F_REQUEST;
4032 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
4033 req.n.nlmsg_type = cmd;
4034 req.nhm.nh_family = AF_UNSPEC;
4035
4036 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nhg_id))
4037 return -1;
4038 if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0))
4039 return -1;
4040 memset(&grp, 0, sizeof(grp));
4041 for (i = 0; i < nh_cnt; ++i) {
4042 grp[i].id = nh_ids[i].id;
4043 grp[i].weight = nh_ids[i].weight;
4044 }
4045 if (!nl_attr_put(&req.n, sizeof(req), NHA_GROUP,
4046 grp, nh_cnt * sizeof(struct nexthop_grp)))
4047 return -1;
4048
4049
4050 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
4051 char vtep_str[ES_VTEP_LIST_STR_SZ];
4052 char nh_buf[16];
4053
4054 vtep_str[0] = '\0';
4055 for (i = 0; i < nh_cnt; ++i) {
4056 snprintf(nh_buf, sizeof(nh_buf), "%u ",
4057 grp[i].id);
4058 strlcat(vtep_str, nh_buf, sizeof(vtep_str));
4059 }
4060
4061 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4062 nl_msg_type_to_str(cmd), nhg_id, vtep_str);
4063 }
4064
4065 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
4066 0);
4067 }
4068
4069 static int netlink_fdb_nhg_del(uint32_t nhg_id)
4070 {
4071 return netlink_fdb_nh_del(nhg_id);
4072 }
4073
4074 int kernel_upd_mac_nh(uint32_t nh_id, struct in_addr vtep_ip)
4075 {
4076 return netlink_fdb_nh_update(nh_id, vtep_ip);
4077 }
4078
4079 int kernel_del_mac_nh(uint32_t nh_id)
4080 {
4081 return netlink_fdb_nh_del(nh_id);
4082 }
4083
4084 int kernel_upd_mac_nhg(uint32_t nhg_id, uint32_t nh_cnt,
4085 struct nh_grp *nh_ids)
4086 {
4087 return netlink_fdb_nhg_update(nhg_id, nh_cnt, nh_ids);
4088 }
4089
4090 int kernel_del_mac_nhg(uint32_t nhg_id)
4091 {
4092 return netlink_fdb_nhg_del(nhg_id);
4093 }
4094
4095 #endif /* HAVE_NETLINK */