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