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