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