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