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