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