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