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