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