]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_netlink.c
zebra: add workqueue support for EVPN updates
[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{
1bda3e62 422 struct rtattr *tb_encap[256] = {};
d49e6c4a
HS
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{
1bda3e62 446 struct rtattr *tb_encap[256] = {};
f16de90b
HS
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;
b9596f13 476 enum seg6local_action_t seg6l_act = ZEBRA_SEG6_LOCAL_ACTION_UNSPEC;
1bda3e62
HS
477 struct seg6local_context seg6l_ctx = {};
478 struct in6_addr seg6_segs = {};
f16de90b 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 536 if (seg6l_act != ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
eab0f8f0 537 nexthop_add_srv6_seg6local(&nh, seg6l_act, &seg6l_ctx);
d49e6c4a 538
f16de90b 539 if (num_segs)
eab0f8f0 540 nexthop_add_srv6_seg6(&nh, &seg6_segs);
f16de90b 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;
b9596f13 558 enum seg6local_action_t seg6l_act = ZEBRA_SEG6_LOCAL_ACTION_UNSPEC;
1bda3e62
HS
559 struct seg6local_context seg6l_ctx = {};
560 struct in6_addr seg6_segs = {};
f16de90b 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 643 if (seg6l_act != ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
eab0f8f0
HS
644 nexthop_add_srv6_seg6local(nh, seg6l_act,
645 &seg6l_ctx);
d49e6c4a 646
f16de90b 647 if (num_segs)
eab0f8f0 648 nexthop_add_srv6_seg6(nh, &seg6_segs);
f16de90b 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
f463eac7
HS
1333static ssize_t fill_seg6ipt_encap(char *buffer, size_t buflen,
1334 const struct in6_addr *seg)
76fb7ae4
HS
1335{
1336 struct seg6_iptunnel_encap *ipt;
1337 struct ipv6_sr_hdr *srh;
1338 const size_t srhlen = 24;
4df9d859 1339
f463eac7
HS
1340 /*
1341 * Caution: Support only SINGLE-SID, not MULTI-SID
1342 * This function only supports the case where segs represents
1343 * a single SID. If you want to extend the SRv6 functionality,
1344 * you should improve the Boundary Check.
1345 * Ex. In case of set a SID-List include multiple-SIDs as an
1346 * argument of the Transit Behavior, we must support variable
1347 * boundary check for buflen.
1348 */
1349 if (buflen < (sizeof(struct seg6_iptunnel_encap) +
1350 sizeof(struct ipv6_sr_hdr) + 16))
1351 return -1;
1352
76fb7ae4
HS
1353 memset(buffer, 0, buflen);
1354
1355 ipt = (struct seg6_iptunnel_encap *)buffer;
1356 ipt->mode = SEG6_IPTUN_MODE_ENCAP;
1357 srh = ipt->srh;
1358 srh->hdrlen = (srhlen >> 3) - 1;
1359 srh->type = 4;
1360 srh->segments_left = 0;
1361 srh->first_segment = 0;
1362 memcpy(&srh->segments[0], seg, sizeof(struct in6_addr));
1363
1364 return srhlen + 4;
1365}
1366
a757997c
JU
1367/* This function takes a nexthop as argument and adds
1368 * the appropriate netlink attributes to an existing
1369 * netlink message.
1370 *
1371 * @param routedesc: Human readable description of route type
1372 * (direct/recursive, single-/multipath)
1373 * @param bytelen: Length of addresses in bytes.
1374 * @param nexthop: Nexthop information
1375 * @param nlmsg: nlmsghdr structure to fill in.
1376 * @param req_size: The size allocated for the message.
1377 *
1378 * The function returns true if the nexthop could be added
1379 * to the message, otherwise false is returned.
1380 */
1381static bool _netlink_route_build_singlepath(const struct prefix *p,
1382 const char *routedesc, int bytelen,
1383 const struct nexthop *nexthop,
1384 struct nlmsghdr *nlmsg,
1385 struct rtmsg *rtmsg,
1386 size_t req_size, int cmd)
1387{
1388
1389 char label_buf[256];
1390 struct vrf *vrf;
1391 char addrstr[INET6_ADDRSTRLEN];
1392
1393 assert(nexthop);
1394
1395 vrf = vrf_lookup_by_id(nexthop->vrf_id);
1396
1397 if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg,
1398 req_size, rtmsg, label_buf,
1399 sizeof(label_buf)))
1400 return false;
1401
eab0f8f0
HS
1402 if (nexthop->nh_srv6) {
1403 if (nexthop->nh_srv6->seg6local_action !=
1404 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC) {
1405 struct rtattr *nest;
1406 const struct seg6local_context *ctx;
52026569 1407
eab0f8f0
HS
1408 ctx = &nexthop->nh_srv6->seg6local_ctx;
1409 if (!nl_attr_put16(nlmsg, req_size, RTA_ENCAP_TYPE,
1410 LWTUNNEL_ENCAP_SEG6_LOCAL))
52026569 1411 return false;
eab0f8f0
HS
1412
1413 nest = nl_attr_nest(nlmsg, req_size, RTA_ENCAP);
1414 if (!nest)
52026569 1415 return false;
eab0f8f0
HS
1416
1417 switch (nexthop->nh_srv6->seg6local_action) {
1418 case ZEBRA_SEG6_LOCAL_ACTION_END:
1419 if (!nl_attr_put32(nlmsg, req_size,
1420 SEG6_LOCAL_ACTION,
1421 SEG6_LOCAL_ACTION_END))
1422 return false;
1423 break;
1424 case ZEBRA_SEG6_LOCAL_ACTION_END_X:
1425 if (!nl_attr_put32(nlmsg, req_size,
1426 SEG6_LOCAL_ACTION,
1427 SEG6_LOCAL_ACTION_END_X))
1428 return false;
1429 if (!nl_attr_put(nlmsg, req_size,
1430 SEG6_LOCAL_NH6, &ctx->nh6,
1431 sizeof(struct in6_addr)))
1432 return false;
1433 break;
1434 case ZEBRA_SEG6_LOCAL_ACTION_END_T:
1435 if (!nl_attr_put32(nlmsg, req_size,
1436 SEG6_LOCAL_ACTION,
1437 SEG6_LOCAL_ACTION_END_T))
1438 return false;
1439 if (!nl_attr_put32(nlmsg, req_size,
1440 SEG6_LOCAL_TABLE,
1441 ctx->table))
1442 return false;
1443 break;
1444 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4:
1445 if (!nl_attr_put32(nlmsg, req_size,
1446 SEG6_LOCAL_ACTION,
1447 SEG6_LOCAL_ACTION_END_DX4))
1448 return false;
1449 if (!nl_attr_put(nlmsg, req_size,
1450 SEG6_LOCAL_NH4, &ctx->nh4,
1451 sizeof(struct in_addr)))
1452 return false;
1453 break;
1454 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6:
1455 if (!nl_attr_put32(nlmsg, req_size,
1456 SEG6_LOCAL_ACTION,
1457 SEG6_LOCAL_ACTION_END_DT6))
1458 return false;
1459 if (!nl_attr_put32(nlmsg, req_size,
1460 SEG6_LOCAL_TABLE,
1461 ctx->table))
1462 return false;
1463 break;
1464 default:
1465 zlog_err("%s: unsupport seg6local behaviour action=%u",
1466 __func__,
1467 nexthop->nh_srv6->seg6local_action);
0a543b79 1468 return false;
eab0f8f0
HS
1469 }
1470 nl_attr_nest_end(nlmsg, nest);
1471 }
1472
1473 if (!sid_zero(&nexthop->nh_srv6->seg6_segs)) {
1474 char tun_buf[4096];
1475 ssize_t tun_len;
1476 struct rtattr *nest;
1477
1478 if (!nl_attr_put16(nlmsg, req_size, RTA_ENCAP_TYPE,
1479 LWTUNNEL_ENCAP_SEG6))
52026569 1480 return false;
eab0f8f0
HS
1481 nest = nl_attr_nest(nlmsg, req_size, RTA_ENCAP);
1482 if (!nest)
52026569 1483 return false;
eab0f8f0
HS
1484 tun_len = fill_seg6ipt_encap(tun_buf, sizeof(tun_buf),
1485 &nexthop->nh_srv6->seg6_segs);
1486 if (tun_len < 0)
52026569 1487 return false;
eab0f8f0
HS
1488 if (!nl_attr_put(nlmsg, req_size, SEG6_IPTUNNEL_SRH,
1489 tun_buf, tun_len))
52026569 1490 return false;
eab0f8f0 1491 nl_attr_nest_end(nlmsg, nest);
8689b25a 1492 }
76fb7ae4
HS
1493 }
1494
d62a17ae 1495 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1496 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1497
002e5c43 1498 if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) {
d62a17ae 1499 rtmsg->rtm_flags |= RTNH_F_ONLINK;
0be6e7d7
JU
1500 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4))
1501 return false;
1502 if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex))
1503 return false;
d62a17ae 1504
a757997c
JU
1505 if (cmd == RTM_NEWROUTE) {
1506 if (!_netlink_route_encode_nexthop_src(
1507 nexthop, AF_INET, nlmsg, req_size, bytelen))
0be6e7d7
JU
1508 return false;
1509 }
d62a17ae 1510
1511 if (IS_ZEBRA_DEBUG_KERNEL)
9266b315
RZ
1512 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1513 __func__, routedesc, p, ipv4_ll_buf,
1514 label_buf, nexthop->ifindex,
1515 VRF_LOGNAME(vrf), nexthop->vrf_id);
0be6e7d7 1516 return true;
0aabccc0
DD
1517 }
1518
d62a17ae 1519 if (nexthop->type == NEXTHOP_TYPE_IPV4
1520 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1521 /* Send deletes to the kernel without specifying the next-hop */
0be6e7d7
JU
1522 if (cmd != RTM_DELROUTE) {
1523 if (!_netlink_route_add_gateway_info(
1524 rtmsg->rtm_family, AF_INET, nlmsg, req_size,
1525 bytelen, nexthop))
1526 return false;
1527 }
d62a17ae 1528
1529 if (cmd == RTM_NEWROUTE) {
a757997c
JU
1530 if (!_netlink_route_encode_nexthop_src(
1531 nexthop, AF_INET, nlmsg, req_size, bytelen))
1532 return false;
d62a17ae 1533 }
1534
9266b315
RZ
1535 if (IS_ZEBRA_DEBUG_KERNEL) {
1536 inet_ntop(AF_INET, &nexthop->gate.ipv4, addrstr,
1537 sizeof(addrstr));
1538 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1539 __func__, routedesc, p, addrstr, label_buf,
1540 nexthop->ifindex, VRF_LOGNAME(vrf),
1541 nexthop->vrf_id);
1542 }
0aabccc0 1543 }
fa713d9e 1544
d62a17ae 1545 if (nexthop->type == NEXTHOP_TYPE_IPV6
1546 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
0be6e7d7
JU
1547 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family,
1548 AF_INET6, nlmsg, req_size,
1549 bytelen, nexthop))
1550 return false;
d62a17ae 1551
1552 if (cmd == RTM_NEWROUTE) {
a757997c
JU
1553 if (!_netlink_route_encode_nexthop_src(
1554 nexthop, AF_INET6, nlmsg, req_size,
1555 bytelen))
1556 return false;
d62a17ae 1557 }
fa713d9e 1558
9266b315
RZ
1559 if (IS_ZEBRA_DEBUG_KERNEL) {
1560 inet_ntop(AF_INET6, &nexthop->gate.ipv6, addrstr,
1561 sizeof(addrstr));
1562 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1563 __func__, routedesc, p, addrstr, label_buf,
1564 nexthop->ifindex, VRF_LOGNAME(vrf),
1565 nexthop->vrf_id);
1566 }
d62a17ae 1567 }
5e210522
DS
1568
1569 /*
1570 * We have the ifindex so we should always send it
1571 * This is especially useful if we are doing route
1572 * leaking.
1573 */
0be6e7d7
JU
1574 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
1575 if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex))
1576 return false;
1577 }
d62a17ae 1578
275565fb 1579 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
d62a17ae 1580 if (cmd == RTM_NEWROUTE) {
a757997c
JU
1581 if (!_netlink_route_encode_nexthop_src(
1582 nexthop, AF_INET, nlmsg, req_size, bytelen))
1583 return false;
d62a17ae 1584 }
fa713d9e 1585
d62a17ae 1586 if (IS_ZEBRA_DEBUG_KERNEL)
9266b315
RZ
1587 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1588 __func__, routedesc, p, nexthop->ifindex,
1589 VRF_LOGNAME(vrf), nexthop->vrf_id);
0aabccc0 1590 }
0be6e7d7
JU
1591
1592 return true;
fa713d9e
CF
1593}
1594
1595/* This function takes a nexthop as argument and
312a6bee 1596 * appends to the given netlink msg. If the nexthop
fa713d9e
CF
1597 * defines a preferred source, the src parameter
1598 * will be modified to point to that src, otherwise
1599 * it will be kept unmodified.
1600 *
1601 * @param routedesc: Human readable description of route type
1602 * (direct/recursive, single-/multipath)
1603 * @param bytelen: Length of addresses in bytes.
1604 * @param nexthop: Nexthop information
312a6bee
JU
1605 * @param nlmsg: nlmsghdr structure to fill in.
1606 * @param req_size: The size allocated for the message.
fa713d9e
CF
1607 * @param src: pointer pointing to a location where
1608 * the prefsrc should be stored.
0be6e7d7
JU
1609 *
1610 * The function returns true if the nexthop could be added
1611 * to the message, otherwise false is returned.
fa713d9e 1612 */
0be6e7d7 1613static bool _netlink_route_build_multipath(const struct prefix *p,
312a6bee
JU
1614 const char *routedesc, int bytelen,
1615 const struct nexthop *nexthop,
1616 struct nlmsghdr *nlmsg,
1617 size_t req_size, struct rtmsg *rtmsg,
1618 const union g_addr **src)
fa713d9e 1619{
9a62e84b 1620 char label_buf[256];
bd47f3a3 1621 struct vrf *vrf;
312a6bee 1622 struct rtnexthop *rtnh;
d62a17ae 1623
312a6bee 1624 rtnh = nl_attr_rtnh(nlmsg, req_size);
0be6e7d7
JU
1625 if (rtnh == NULL)
1626 return false;
d62a17ae 1627
b7537db6
SW
1628 assert(nexthop);
1629
bd47f3a3
JU
1630 vrf = vrf_lookup_by_id(nexthop->vrf_id);
1631
a757997c
JU
1632 if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg,
1633 req_size, rtmsg, label_buf,
1634 sizeof(label_buf)))
1635 return false;
d62a17ae 1636
1637 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1638 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1639
002e5c43 1640 if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) {
d62a17ae 1641 rtnh->rtnh_flags |= RTNH_F_ONLINK;
a757997c 1642 if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4))
0be6e7d7 1643 return false;
d62a17ae 1644 rtnh->rtnh_ifindex = nexthop->ifindex;
8d27e1aa 1645 if (nexthop->weight)
1646 rtnh->rtnh_hops = nexthop->weight - 1;
d62a17ae 1647
975a328e 1648 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1649 *src = &nexthop->rmap_src;
975a328e 1650 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1651 *src = &nexthop->src;
1652
1653 if (IS_ZEBRA_DEBUG_KERNEL)
1654 zlog_debug(
9266b315
RZ
1655 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1656 __func__, routedesc, p, ipv4_ll_buf, label_buf,
bd47f3a3
JU
1657 nexthop->ifindex, VRF_LOGNAME(vrf),
1658 nexthop->vrf_id);
312a6bee 1659 nl_attr_rtnh_end(nlmsg, rtnh);
0be6e7d7 1660 return true;
d62a17ae 1661 }
1662
1663 if (nexthop->type == NEXTHOP_TYPE_IPV4
1664 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
0be6e7d7
JU
1665 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family, AF_INET,
1666 nlmsg, req_size, bytelen,
1667 nexthop))
1668 return false;
1669
975a328e 1670 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1671 *src = &nexthop->rmap_src;
975a328e 1672 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1673 *src = &nexthop->src;
1674
a50404aa
RZ
1675 if (IS_ZEBRA_DEBUG_KERNEL)
1676 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1677 __func__, routedesc, p, &nexthop->gate.ipv4,
1678 label_buf, nexthop->ifindex,
1679 VRF_LOGNAME(vrf), nexthop->vrf_id);
d62a17ae 1680 }
1681 if (nexthop->type == NEXTHOP_TYPE_IPV6
1682 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
0be6e7d7
JU
1683 if (!_netlink_route_add_gateway_info(rtmsg->rtm_family,
1684 AF_INET6, nlmsg, req_size,
1685 bytelen, nexthop))
1686 return false;
d62a17ae 1687
1688 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1689 *src = &nexthop->rmap_src;
1690 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1691 *src = &nexthop->src;
1692
a50404aa
RZ
1693 if (IS_ZEBRA_DEBUG_KERNEL)
1694 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1695 __func__, routedesc, p, &nexthop->gate.ipv6,
1696 label_buf, nexthop->ifindex,
1697 VRF_LOGNAME(vrf), nexthop->vrf_id);
d62a17ae 1698 }
5e210522
DS
1699
1700 /*
1701 * We have figured out the ifindex so we should always send it
1702 * This is especially useful if we are doing route
1703 * leaking.
1704 */
1705 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1706 rtnh->rtnh_ifindex = nexthop->ifindex;
1707
d62a17ae 1708 /* ifindex */
275565fb 1709 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
975a328e 1710 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1711 *src = &nexthop->rmap_src;
975a328e 1712 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1713 *src = &nexthop->src;
1714
1715 if (IS_ZEBRA_DEBUG_KERNEL)
9266b315
RZ
1716 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1717 __func__, routedesc, p, nexthop->ifindex,
1718 VRF_LOGNAME(vrf), nexthop->vrf_id);
d62a17ae 1719 }
df7fb580
DS
1720
1721 if (nexthop->weight)
1722 rtnh->rtnh_hops = nexthop->weight - 1;
0be6e7d7 1723
312a6bee 1724 nl_attr_rtnh_end(nlmsg, rtnh);
0be6e7d7 1725 return true;
fa713d9e
CF
1726}
1727
0be6e7d7 1728static inline bool _netlink_mpls_build_singlepath(const struct prefix *p,
9a0132a5 1729 const char *routedesc,
81793ac1 1730 const zebra_nhlfe_t *nhlfe,
d62a17ae 1731 struct nlmsghdr *nlmsg,
1732 struct rtmsg *rtmsg,
1733 size_t req_size, int cmd)
40c7bdb0 1734{
d62a17ae 1735 int bytelen;
d7c0a89a 1736 uint8_t family;
40c7bdb0 1737
d62a17ae 1738 family = NHLFE_FAMILY(nhlfe);
1739 bytelen = (family == AF_INET ? 4 : 16);
0be6e7d7
JU
1740 return _netlink_route_build_singlepath(p, routedesc, bytelen,
1741 nhlfe->nexthop, nlmsg, rtmsg,
1742 req_size, cmd);
40c7bdb0 1743}
1744
1745
0be6e7d7 1746static inline bool
9a0132a5 1747_netlink_mpls_build_multipath(const struct prefix *p, const char *routedesc,
312a6bee
JU
1748 const zebra_nhlfe_t *nhlfe,
1749 struct nlmsghdr *nlmsg, size_t req_size,
1750 struct rtmsg *rtmsg, const union g_addr **src)
40c7bdb0 1751{
d62a17ae 1752 int bytelen;
d7c0a89a 1753 uint8_t family;
40c7bdb0 1754
d62a17ae 1755 family = NHLFE_FAMILY(nhlfe);
1756 bytelen = (family == AF_INET ? 4 : 16);
0be6e7d7
JU
1757 return _netlink_route_build_multipath(p, routedesc, bytelen,
1758 nhlfe->nexthop, nlmsg, req_size,
1759 rtmsg, src);
40c7bdb0 1760}
1761
d7c0a89a 1762static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc)
40c7bdb0 1763{
d62a17ae 1764 if (IS_ZEBRA_DEBUG_KERNEL)
0be6e7d7
JU
1765 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1766 routedesc, nl_msg_type_to_str(cmd), label);
fa713d9e
CF
1767}
1768
05657ec2
PG
1769static int netlink_neigh_update(int cmd, int ifindex, void *addr, char *lla,
1770 int llalen, ns_id_t ns_id, uint8_t family,
1771 bool permanent, uint8_t protocol)
5c610faf 1772{
d62a17ae 1773 struct {
1774 struct nlmsghdr n;
1775 struct ndmsg ndm;
1776 char buf[256];
1777 } req;
5c610faf 1778
5895d33f 1779 struct zebra_ns *zns = zebra_ns_lookup(ns_id);
8f7d9fc0 1780
5605ecfc 1781 memset(&req, 0, sizeof(req));
5c610faf 1782
d62a17ae 1783 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1784 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1785 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
1786 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
a55ba23f 1787
05657ec2 1788 req.ndm.ndm_family = family;
d62a17ae 1789 req.ndm.ndm_ifindex = ifindex;
1790 req.ndm.ndm_type = RTN_UNICAST;
05657ec2
PG
1791 if (cmd == RTM_NEWNEIGH) {
1792 if (!permanent)
1793 req.ndm.ndm_state = NUD_REACHABLE;
1794 else
1795 req.ndm.ndm_state = NUD_PERMANENT;
1796 } else
1797 req.ndm.ndm_state = NUD_FAILED;
5c610faf 1798
312a6bee
JU
1799 nl_attr_put(&req.n, sizeof(req), NDA_PROTOCOL, &protocol,
1800 sizeof(protocol));
05657ec2 1801 req.ndm.ndm_type = RTN_UNICAST;
df948efc
PG
1802 nl_attr_put(&req.n, sizeof(req), NDA_DST, addr,
1803 family2addrsize(family));
05657ec2
PG
1804 if (lla)
1805 nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, lla, llalen);
5c610faf 1806
d62a17ae 1807 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1808 0);
5c610faf
DS
1809}
1810
762288f5
SW
1811static bool nexthop_set_src(const struct nexthop *nexthop, int family,
1812 union g_addr *src)
1813{
1814 if (family == AF_INET) {
1815 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) {
1816 src->ipv4 = nexthop->rmap_src.ipv4;
1817 return true;
1818 } else if (nexthop->src.ipv4.s_addr != INADDR_ANY) {
1819 src->ipv4 = nexthop->src.ipv4;
1820 return true;
1821 }
1822 } else if (family == AF_INET6) {
1823 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) {
1824 src->ipv6 = nexthop->rmap_src.ipv6;
1825 return true;
1826 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) {
1827 src->ipv6 = nexthop->src.ipv6;
1828 return true;
1829 }
1830 }
1831
1832 return false;
1833}
1834
0be6e7d7
JU
1835/*
1836 * The function returns true if the attribute could be added
1837 * to the message, otherwise false is returned.
1838 */
1839static int netlink_route_nexthop_encap(struct nlmsghdr *n, size_t nlen,
1840 struct nexthop *nh)
f2a0ba3a
RZ
1841{
1842 struct rtattr *nest;
1843
1844 switch (nh->nh_encap_type) {
1845 case NET_VXLAN:
a757997c 1846 if (!nl_attr_put16(n, nlen, RTA_ENCAP_TYPE, nh->nh_encap_type))
0be6e7d7 1847 return false;
f2a0ba3a 1848
312a6bee 1849 nest = nl_attr_nest(n, nlen, RTA_ENCAP);
0be6e7d7
JU
1850 if (!nest)
1851 return false;
1852
1853 if (!nl_attr_put32(n, nlen, 0 /* VXLAN_VNI */,
1854 nh->nh_encap.vni))
1855 return false;
312a6bee 1856 nl_attr_nest_end(n, nest);
f2a0ba3a
RZ
1857 break;
1858 }
0be6e7d7
JU
1859
1860 return true;
f2a0ba3a
RZ
1861}
1862
7cdb1a84
MS
1863/*
1864 * Routing table change via netlink interface, using a dataplane context object
0be6e7d7
JU
1865 *
1866 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1867 * otherwise the number of bytes written to buf.
7cdb1a84 1868 */
0be6e7d7
JU
1869ssize_t netlink_route_multipath_msg_encode(int cmd,
1870 struct zebra_dplane_ctx *ctx,
1871 uint8_t *data, size_t datalen,
1872 bool fpm, bool force_nhg)
7cdb1a84
MS
1873{
1874 int bytelen;
7cdb1a84
MS
1875 struct nexthop *nexthop = NULL;
1876 unsigned int nexthop_num;
7cdb1a84 1877 const char *routedesc;
762288f5 1878 bool setsrc = false;
7cdb1a84
MS
1879 union g_addr src;
1880 const struct prefix *p, *src_p;
1881 uint32_t table_id;
1882
1883 struct {
1884 struct nlmsghdr n;
1885 struct rtmsg r;
e57a3fab
RZ
1886 char buf[];
1887 } *req = (void *)data;
7cdb1a84
MS
1888
1889 p = dplane_ctx_get_dest(ctx);
1890 src_p = dplane_ctx_get_src(ctx);
1891
0be6e7d7
JU
1892 if (datalen < sizeof(*req))
1893 return 0;
1894
e57a3fab 1895 memset(req, 0, sizeof(*req));
7cdb1a84 1896
b9c87515 1897 bytelen = (p->family == AF_INET ? 4 : 16);
7cdb1a84 1898
e57a3fab
RZ
1899 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1900 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
7cdb1a84 1901
334734a8
DS
1902 if ((cmd == RTM_NEWROUTE) &&
1903 ((p->family == AF_INET) || v6_rr_semantics))
e57a3fab 1904 req->n.nlmsg_flags |= NLM_F_REPLACE;
7cdb1a84 1905
e57a3fab 1906 req->n.nlmsg_type = cmd;
7cdb1a84 1907
e57a3fab 1908 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
7cdb1a84 1909
b9c87515 1910 req->r.rtm_family = p->family;
e57a3fab
RZ
1911 req->r.rtm_dst_len = p->prefixlen;
1912 req->r.rtm_src_len = src_p ? src_p->prefixlen : 0;
1913 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
7cdb1a84 1914
5709131c 1915 if (cmd == RTM_DELROUTE)
e57a3fab 1916 req->r.rtm_protocol = zebra2proto(dplane_ctx_get_old_type(ctx));
5709131c 1917 else
e57a3fab 1918 req->r.rtm_protocol = zebra2proto(dplane_ctx_get_type(ctx));
7cdb1a84
MS
1919
1920 /*
1921 * blackhole routes are not RTN_UNICAST, they are
1922 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1923 * so setting this value as a RTN_UNICAST would
1924 * cause the route lookup of just the prefix
1925 * to fail. So no need to specify this for
1926 * the RTM_DELROUTE case
1927 */
1928 if (cmd != RTM_DELROUTE)
e57a3fab 1929 req->r.rtm_type = RTN_UNICAST;
7cdb1a84 1930
0be6e7d7
JU
1931 if (!nl_attr_put(&req->n, datalen, RTA_DST, &p->u.prefix, bytelen))
1932 return 0;
1933 if (src_p) {
1934 if (!nl_attr_put(&req->n, datalen, RTA_SRC, &src_p->u.prefix,
1935 bytelen))
1936 return 0;
1937 }
7cdb1a84
MS
1938
1939 /* Metric. */
1940 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1941 * used
1942 * either by the kernel or by zebra. Its purely for calculating best
1943 * path(s)
1944 * by the routing protocol and for communicating with protocol peers.
1945 */
0be6e7d7
JU
1946 if (!nl_attr_put32(&req->n, datalen, RTA_PRIORITY,
1947 NL_DEFAULT_ROUTE_METRIC))
1948 return 0;
7cdb1a84
MS
1949
1950#if defined(SUPPORT_REALMS)
1951 {
1952 route_tag_t tag;
1953
5709131c 1954 if (cmd == RTM_DELROUTE)
7cdb1a84 1955 tag = dplane_ctx_get_old_tag(ctx);
5709131c 1956 else
7cdb1a84 1957 tag = dplane_ctx_get_tag(ctx);
7cdb1a84 1958
0be6e7d7
JU
1959 if (tag > 0 && tag <= 255) {
1960 if (!nl_attr_put32(&req->n, datalen, RTA_FLOW, tag))
1961 return 0;
1962 }
7cdb1a84
MS
1963 }
1964#endif
1965 /* Table corresponding to this route. */
1966 table_id = dplane_ctx_get_table(ctx);
1967 if (table_id < 256)
e57a3fab 1968 req->r.rtm_table = table_id;
7cdb1a84 1969 else {
e57a3fab 1970 req->r.rtm_table = RT_TABLE_UNSPEC;
0be6e7d7
JU
1971 if (!nl_attr_put32(&req->n, datalen, RTA_TABLE, table_id))
1972 return 0;
7cdb1a84
MS
1973 }
1974
9266b315
RZ
1975 if (IS_ZEBRA_DEBUG_KERNEL)
1976 zlog_debug(
1977 "%s: %s %pFX vrf %u(%u)", __func__,
1978 nl_msg_type_to_str(cmd), p, dplane_ctx_get_vrf(ctx),
1979 table_id);
7cdb1a84
MS
1980
1981 /*
1982 * If we are not updating the route and we have received
1983 * a route delete, then all we need to fill in is the
1984 * prefix information to tell the kernel to schwack
1985 * it.
1986 */
1987 if (cmd == RTM_DELROUTE)
0be6e7d7 1988 return NLMSG_ALIGN(req->n.nlmsg_len);
7cdb1a84
MS
1989
1990 if (dplane_ctx_get_mtu(ctx) || dplane_ctx_get_nh_mtu(ctx)) {
312a6bee 1991 struct rtattr *nest;
7cdb1a84
MS
1992 uint32_t mtu = dplane_ctx_get_mtu(ctx);
1993 uint32_t nexthop_mtu = dplane_ctx_get_nh_mtu(ctx);
5709131c 1994
7cdb1a84
MS
1995 if (!mtu || (nexthop_mtu && nexthop_mtu < mtu))
1996 mtu = nexthop_mtu;
312a6bee
JU
1997
1998 nest = nl_attr_nest(&req->n, datalen, RTA_METRICS);
0be6e7d7
JU
1999 if (nest == NULL)
2000 return 0;
2001
2002 if (!nl_attr_put(&req->n, datalen, RTAX_MTU, &mtu, sizeof(mtu)))
2003 return 0;
312a6bee 2004 nl_attr_nest_end(&req->n, nest);
7cdb1a84
MS
2005 }
2006
4be03ff4
IR
2007 /*
2008 * Always install blackhole routes without using nexthops, because of
2009 * the following kernel problems:
2010 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2011 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2012 */
2013 nexthop = dplane_ctx_get_ng(ctx)->nexthop;
2014 if (nexthop) {
2015 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
2016 nexthop = nexthop->resolved;
2017
2018 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
2019 switch (nexthop->bh_type) {
2020 case BLACKHOLE_ADMINPROHIB:
2021 req->r.rtm_type = RTN_PROHIBIT;
2022 break;
2023 case BLACKHOLE_REJECT:
2024 req->r.rtm_type = RTN_UNREACHABLE;
2025 break;
2026 default:
2027 req->r.rtm_type = RTN_BLACKHOLE;
2028 break;
2029 }
2030 return NLMSG_ALIGN(req->n.nlmsg_len);
2031 }
2032 }
2033
6c67f41f
SW
2034 if ((!fpm && kernel_nexthops_supported()
2035 && (!proto_nexthops_only()
2036 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx), 0)))
2037 || (fpm && force_nhg)) {
d8bfd8dc 2038 /* Kernel supports nexthop objects */
9a0132a5 2039 if (IS_ZEBRA_DEBUG_KERNEL)
0be6e7d7
JU
2040 zlog_debug("%s: %pFX nhg_id is %u", __func__, p,
2041 dplane_ctx_get_nhe_id(ctx));
e57a3fab 2042
0be6e7d7
JU
2043 if (!nl_attr_put32(&req->n, datalen, RTA_NH_ID,
2044 dplane_ctx_get_nhe_id(ctx)))
2045 return 0;
d8bfd8dc
SW
2046
2047 /* Have to determine src still */
2048 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2049 if (setsrc)
2050 break;
2051
e57a3fab 2052 setsrc = nexthop_set_src(nexthop, p->family, &src);
d8bfd8dc
SW
2053 }
2054
2055 if (setsrc) {
0be6e7d7
JU
2056 if (p->family == AF_INET) {
2057 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2058 &src.ipv4, bytelen))
2059 return 0;
2060 } else if (p->family == AF_INET6) {
2061 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2062 &src.ipv6, bytelen))
2063 return 0;
2064 }
d8bfd8dc 2065 }
f78fe8f3 2066
0be6e7d7 2067 return NLMSG_ALIGN(req->n.nlmsg_len);
de3f5488
SW
2068 }
2069
7cdb1a84 2070 /* Count overall nexthops so we can decide whether to use singlepath
5709131c
MS
2071 * or multipath case.
2072 */
7cdb1a84
MS
2073 nexthop_num = 0;
2074 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2075 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
2076 continue;
b9c87515 2077 if (!NEXTHOP_IS_ACTIVE(nexthop->flags))
7cdb1a84
MS
2078 continue;
2079
2080 nexthop_num++;
2081 }
2082
2083 /* Singlepath case. */
220f0f42 2084 if (nexthop_num == 1) {
7cdb1a84
MS
2085 nexthop_num = 0;
2086 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
7cdb1a84
MS
2087 if (CHECK_FLAG(nexthop->flags,
2088 NEXTHOP_FLAG_RECURSIVE)) {
5709131c
MS
2089
2090 if (setsrc)
2091 continue;
2092
b9c87515
RZ
2093 setsrc = nexthop_set_src(nexthop, p->family,
2094 &src);
f183e380 2095 continue;
7cdb1a84
MS
2096 }
2097
b9c87515 2098 if (NEXTHOP_IS_ACTIVE(nexthop->flags)) {
7cdb1a84
MS
2099 routedesc = nexthop->rparent
2100 ? "recursive, single-path"
2101 : "single-path";
2102
0be6e7d7
JU
2103 if (!_netlink_route_build_singlepath(
2104 p, routedesc, bytelen, nexthop,
2105 &req->n, &req->r, datalen, cmd))
2106 return 0;
7cdb1a84
MS
2107 nexthop_num++;
2108 break;
2109 }
f2a0ba3a
RZ
2110
2111 /*
2112 * Add encapsulation information when installing via
2113 * FPM.
2114 */
0be6e7d7
JU
2115 if (fpm) {
2116 if (!netlink_route_nexthop_encap(
2117 &req->n, datalen, nexthop))
2118 return 0;
2119 }
7cdb1a84 2120 }
f2a0ba3a 2121
13e0321a 2122 if (setsrc) {
0be6e7d7
JU
2123 if (p->family == AF_INET) {
2124 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2125 &src.ipv4, bytelen))
2126 return 0;
2127 } else if (p->family == AF_INET6) {
2128 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2129 &src.ipv6, bytelen))
2130 return 0;
2131 }
7cdb1a84
MS
2132 }
2133 } else { /* Multipath case */
312a6bee 2134 struct rtattr *nest;
81793ac1 2135 const union g_addr *src1 = NULL;
7cdb1a84 2136
312a6bee 2137 nest = nl_attr_nest(&req->n, datalen, RTA_MULTIPATH);
0be6e7d7
JU
2138 if (nest == NULL)
2139 return 0;
7cdb1a84
MS
2140
2141 nexthop_num = 0;
2142 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
7cdb1a84
MS
2143 if (CHECK_FLAG(nexthop->flags,
2144 NEXTHOP_FLAG_RECURSIVE)) {
2145 /* This only works for IPv4 now */
5709131c
MS
2146 if (setsrc)
2147 continue;
2148
b9c87515
RZ
2149 setsrc = nexthop_set_src(nexthop, p->family,
2150 &src);
78e54ded 2151 continue;
7cdb1a84
MS
2152 }
2153
b9c87515 2154 if (NEXTHOP_IS_ACTIVE(nexthop->flags)) {
7cdb1a84
MS
2155 routedesc = nexthop->rparent
2156 ? "recursive, multipath"
2157 : "multipath";
2158 nexthop_num++;
2159
0be6e7d7
JU
2160 if (!_netlink_route_build_multipath(
2161 p, routedesc, bytelen, nexthop,
2162 &req->n, datalen, &req->r, &src1))
2163 return 0;
7cdb1a84
MS
2164
2165 if (!setsrc && src1) {
b9c87515 2166 if (p->family == AF_INET)
7cdb1a84 2167 src.ipv4 = src1->ipv4;
b9c87515 2168 else if (p->family == AF_INET6)
7cdb1a84
MS
2169 src.ipv6 = src1->ipv6;
2170
2171 setsrc = 1;
2172 }
2173 }
312a6bee 2174 }
0be6e7d7 2175
312a6bee
JU
2176 nl_attr_nest_end(&req->n, nest);
2177
2178 /*
2179 * Add encapsulation information when installing via
2180 * FPM.
2181 */
2182 if (fpm) {
2183 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
2184 nexthop)) {
2185 if (CHECK_FLAG(nexthop->flags,
2186 NEXTHOP_FLAG_RECURSIVE))
2187 continue;
0be6e7d7
JU
2188 if (!netlink_route_nexthop_encap(
2189 &req->n, datalen, nexthop))
2190 return 0;
312a6bee 2191 }
7cdb1a84 2192 }
f2a0ba3a 2193
312a6bee 2194
13e0321a 2195 if (setsrc) {
0be6e7d7
JU
2196 if (p->family == AF_INET) {
2197 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2198 &src.ipv4, bytelen))
2199 return 0;
2200 } else if (p->family == AF_INET6) {
2201 if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC,
2202 &src.ipv6, bytelen))
2203 return 0;
2204 }
7cdb1a84
MS
2205 if (IS_ZEBRA_DEBUG_KERNEL)
2206 zlog_debug("Setting source");
2207 }
7cdb1a84
MS
2208 }
2209
2210 /* If there is no useful nexthop then return. */
2211 if (nexthop_num == 0) {
2212 if (IS_ZEBRA_DEBUG_KERNEL)
9266b315 2213 zlog_debug("%s: No useful nexthop.", __func__);
7cdb1a84
MS
2214 }
2215
312a6bee 2216 return NLMSG_ALIGN(req->n.nlmsg_len);
7cdb1a84
MS
2217}
2218
43b5cc5e 2219int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in)
e3be0432 2220{
5523c156 2221 uint32_t actual_table;
d62a17ae 2222 int suc = 0;
2223 struct mcast_route_data *mr = (struct mcast_route_data *)in;
bd8b9272
DS
2224 struct {
2225 struct nlmsghdr n;
2226 struct ndmsg ndm;
2227 char buf[256];
2228 } req;
e3be0432 2229
d62a17ae 2230 mroute = mr;
5895d33f 2231 struct zebra_ns *zns;
bd8b9272 2232
009f8ad5 2233 zns = zvrf->zns;
5605ecfc 2234 memset(&req, 0, sizeof(req));
bd8b9272
DS
2235
2236 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2237 req.n.nlmsg_flags = NLM_F_REQUEST;
2238 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
2239
2240 req.ndm.ndm_family = RTNL_FAMILY_IPMR;
2241 req.n.nlmsg_type = RTM_GETROUTE;
2242
a757997c
JU
2243 nl_attr_put32(&req.n, sizeof(req), RTA_IIF, mroute->ifindex);
2244 nl_attr_put32(&req.n, sizeof(req), RTA_OIF, mroute->ifindex);
2245 nl_attr_put32(&req.n, sizeof(req), RTA_SRC, mroute->sg.src.s_addr);
2246 nl_attr_put32(&req.n, sizeof(req), RTA_DST, mroute->sg.grp.s_addr);
5523c156
DS
2247 /*
2248 * What?
2249 *
2250 * So during the namespace cleanup we started storing
2251 * the zvrf table_id for the default table as RT_TABLE_MAIN
2252 * which is what the normal routing table for ip routing is.
2253 * This change caused this to break our lookups of sg data
2254 * because prior to this change the zvrf->table_id was 0
2255 * and when the pim multicast kernel code saw a 0,
2256 * it was auto-translated to RT_TABLE_DEFAULT. But since
2257 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2258 * and the kernel goes screw you and the delicious cookies you
2259 * are trying to give me. So now we have this little hack.
2260 */
2261 actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT :
2262 zvrf->table_id;
a757997c 2263 nl_attr_put32(&req.n, sizeof(req), RTA_TABLE, actual_table);
e3be0432 2264
bd8b9272
DS
2265 suc = netlink_talk(netlink_route_change_read_multicast, &req.n,
2266 &zns->netlink_cmd, zns, 0);
e3be0432 2267
bd8b9272 2268 mroute = NULL;
d62a17ae 2269 return suc;
e3be0432
DS
2270}
2271
8d03bc50
SW
2272/* Char length to debug ID with */
2273#define ID_LENGTH 10
2274
0be6e7d7 2275static bool _netlink_nexthop_build_group(struct nlmsghdr *n, size_t req_size,
8d03bc50 2276 uint32_t id,
e22e8001 2277 const struct nh_grp *z_grp,
0c8215cb 2278 const uint8_t count)
565ce0d3 2279{
565ce0d3 2280 struct nexthop_grp grp[count];
8d03bc50
SW
2281 /* Need space for max group size, "/", and null term */
2282 char buf[(MULTIPATH_NUM * (ID_LENGTH + 1)) + 1];
2283 char buf1[ID_LENGTH + 2];
2284
2285 buf[0] = '\0';
565ce0d3
SW
2286
2287 memset(grp, 0, sizeof(grp));
2288
2289 if (count) {
0c8215cb 2290 for (int i = 0; i < count; i++) {
e22e8001 2291 grp[i].id = z_grp[i].id;
df7fb580 2292 grp[i].weight = z_grp[i].weight - 1;
8d03bc50
SW
2293
2294 if (IS_ZEBRA_DEBUG_KERNEL) {
2295 if (i == 0)
2296 snprintf(buf, sizeof(buf1), "group %u",
2297 grp[i].id);
2298 else {
2299 snprintf(buf1, sizeof(buf1), "/%u",
2300 grp[i].id);
2301 strlcat(buf, buf1, sizeof(buf));
2302 }
2303 }
565ce0d3 2304 }
0be6e7d7
JU
2305 if (!nl_attr_put(n, req_size, NHA_GROUP, grp,
2306 count * sizeof(*grp)))
2307 return false;
565ce0d3 2308 }
8d03bc50
SW
2309
2310 if (IS_ZEBRA_DEBUG_KERNEL)
2311 zlog_debug("%s: ID (%u): %s", __func__, id, buf);
0be6e7d7
JU
2312
2313 return true;
565ce0d3
SW
2314}
2315
f820d025 2316/**
e9a1cd93 2317 * Next hop packet encoding helper function.
f820d025 2318 *
e9a1cd93
RZ
2319 * \param[in] cmd netlink command.
2320 * \param[in] ctx dataplane context (information snapshot).
2321 * \param[out] buf buffer to hold the packet.
2322 * \param[in] buflen amount of buffer bytes.
f820d025 2323 *
0be6e7d7
JU
2324 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2325 * otherwise the number of bytes written to buf.
f820d025 2326 */
0be6e7d7
JU
2327ssize_t netlink_nexthop_msg_encode(uint16_t cmd,
2328 const struct zebra_dplane_ctx *ctx,
2329 void *buf, size_t buflen)
f820d025 2330{
f820d025
SW
2331 struct {
2332 struct nlmsghdr n;
2333 struct nhmsg nhm;
e9a1cd93
RZ
2334 char buf[];
2335 } *req = buf;
f820d025 2336
8d03bc50
SW
2337 mpls_lse_t out_lse[MPLS_MAX_LABELS];
2338 char label_buf[256];
2339 int num_labels = 0;
72938edf
SW
2340 uint32_t id = dplane_ctx_get_nhe_id(ctx);
2341 int type = dplane_ctx_get_nhe_type(ctx);
bdd085a8
MS
2342 struct rtattr *nest;
2343 uint16_t encap;
72938edf
SW
2344
2345 if (!id) {
2346 flog_err(
2347 EC_ZEBRA_NHG_FIB_UPDATE,
2348 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2349 return -1;
2350 }
81505946 2351
6c67f41f
SW
2352 /*
2353 * Nothing to do if the kernel doesn't support nexthop objects or
2354 * we dont want to install this type of NHG
2355 */
72938edf
SW
2356 if (!kernel_nexthops_supported()) {
2357 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2358 zlog_debug(
2359 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2360 __func__, id, zebra_route_string(type));
6c67f41f 2361 return 0;
72938edf
SW
2362 }
2363
2364 if (proto_nexthops_only() && !is_proto_nhg(id, type)) {
2365 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2366 zlog_debug(
2367 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2368 __func__, id, zebra_route_string(type));
2369 return 0;
2370 }
6c67f41f 2371
8d03bc50
SW
2372 label_buf[0] = '\0';
2373
0be6e7d7
JU
2374 if (buflen < sizeof(*req))
2375 return 0;
2376
2377 memset(req, 0, sizeof(*req));
f820d025 2378
e9a1cd93
RZ
2379 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2380 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
9a1588c4
SW
2381
2382 if (cmd == RTM_NEWNEXTHOP)
e9a1cd93 2383 req->n.nlmsg_flags |= NLM_F_REPLACE;
9a1588c4 2384
e9a1cd93
RZ
2385 req->n.nlmsg_type = cmd;
2386 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
f820d025 2387
e9a1cd93 2388 req->nhm.nh_family = AF_UNSPEC;
fec211ad 2389 /* TODO: Scope? */
f820d025 2390
0be6e7d7
JU
2391 if (!nl_attr_put32(&req->n, buflen, NHA_ID, id))
2392 return 0;
f820d025
SW
2393
2394 if (cmd == RTM_NEWNEXTHOP) {
bf1626a6
MS
2395 /*
2396 * We distinguish between a "group", which is a collection
2397 * of ids, and a singleton nexthop with an id. The
2398 * group is installed as an id that just refers to a list of
2399 * other ids.
2400 */
0be6e7d7 2401 if (dplane_ctx_get_nhe_nh_grp_count(ctx)) {
d52c949b 2402 if (!_netlink_nexthop_build_group(
0be6e7d7
JU
2403 &req->n, buflen, id,
2404 dplane_ctx_get_nhe_nh_grp(ctx),
2405 dplane_ctx_get_nhe_nh_grp_count(ctx)))
2406 return 0;
2407 } else {
0c8215cb
SW
2408 const struct nexthop *nh =
2409 dplane_ctx_get_nhe_ng(ctx)->nexthop;
2410 afi_t afi = dplane_ctx_get_nhe_afi(ctx);
e8b0e420 2411
0c8215cb 2412 if (afi == AFI_IP)
e9a1cd93 2413 req->nhm.nh_family = AF_INET;
0c8215cb 2414 else if (afi == AFI_IP6)
e9a1cd93 2415 req->nhm.nh_family = AF_INET6;
f820d025 2416
565ce0d3 2417 switch (nh->type) {
a6e6a6d8 2418 case NEXTHOP_TYPE_IPV4:
565ce0d3 2419 case NEXTHOP_TYPE_IPV4_IFINDEX:
0be6e7d7
JU
2420 if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY,
2421 &nh->gate.ipv4,
2422 IPV4_MAX_BYTELEN))
2423 return 0;
565ce0d3 2424 break;
a6e6a6d8 2425 case NEXTHOP_TYPE_IPV6:
565ce0d3 2426 case NEXTHOP_TYPE_IPV6_IFINDEX:
0be6e7d7
JU
2427 if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY,
2428 &nh->gate.ipv6,
2429 IPV6_MAX_BYTELEN))
2430 return 0;
565ce0d3
SW
2431 break;
2432 case NEXTHOP_TYPE_BLACKHOLE:
0be6e7d7
JU
2433 if (!nl_attr_put(&req->n, buflen, NHA_BLACKHOLE,
2434 NULL, 0))
2435 return 0;
8d03bc50
SW
2436 /* Blackhole shouldn't have anymore attributes
2437 */
2438 goto nexthop_done;
565ce0d3
SW
2439 case NEXTHOP_TYPE_IFINDEX:
2440 /* Don't need anymore info for this */
2441 break;
a6e6a6d8
SW
2442 }
2443
2444 if (!nh->ifindex) {
565ce0d3
SW
2445 flog_err(
2446 EC_ZEBRA_NHG_FIB_UPDATE,
2447 "Context received for kernel nexthop update without an interface");
2448 return -1;
565ce0d3
SW
2449 }
2450
0be6e7d7
JU
2451 if (!nl_attr_put32(&req->n, buflen, NHA_OIF,
2452 nh->ifindex))
2453 return 0;
8d03bc50 2454
62d2ecb2 2455 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK))
e9a1cd93 2456 req->nhm.nh_flags |= RTNH_F_ONLINK;
62d2ecb2 2457
8d03bc50
SW
2458 num_labels =
2459 build_label_stack(nh->nh_label, out_lse,
2460 label_buf, sizeof(label_buf));
2461
2462 if (num_labels) {
2463 /* Set the BoS bit */
2464 out_lse[num_labels - 1] |=
2465 htonl(1 << MPLS_LS_S_SHIFT);
2466
2467 /*
2468 * TODO: MPLS unsupported for now in kernel.
2469 */
e9a1cd93 2470 if (req->nhm.nh_family == AF_MPLS)
8d03bc50 2471 goto nexthop_done;
bdd085a8
MS
2472
2473 encap = LWTUNNEL_ENCAP_MPLS;
2474 if (!nl_attr_put16(&req->n, buflen,
2475 NHA_ENCAP_TYPE, encap))
2476 return 0;
2477 nest = nl_attr_nest(&req->n, buflen, NHA_ENCAP);
2478 if (!nest)
2479 return 0;
2480 if (!nl_attr_put(
2481 &req->n, buflen, MPLS_IPTUNNEL_DST,
2482 &out_lse,
2483 num_labels * sizeof(mpls_lse_t)))
2484 return 0;
2485
2486 nl_attr_nest_end(&req->n, nest);
8d03bc50
SW
2487 }
2488
eab0f8f0
HS
2489 if (nh->nh_srv6) {
2490 if (nh->nh_srv6->seg6local_action !=
2491 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC) {
2492 uint32_t action;
2493 uint16_t encap;
2494 struct rtattr *nest;
2495 const struct seg6local_context *ctx;
2496
2497 req->nhm.nh_family = AF_INET6;
2498 action = nh->nh_srv6->seg6local_action;
2499 ctx = &nh->nh_srv6->seg6local_ctx;
2500 encap = LWTUNNEL_ENCAP_SEG6_LOCAL;
2501 if (!nl_attr_put(&req->n, buflen,
2502 NHA_ENCAP_TYPE,
2503 &encap,
2504 sizeof(uint16_t)))
2505 return 0;
8689b25a 2506
eab0f8f0
HS
2507 nest = nl_attr_nest(&req->n, buflen,
2508 NHA_ENCAP | NLA_F_NESTED);
2509 if (!nest)
2510 return 0;
52026569 2511
eab0f8f0
HS
2512 switch (action) {
2513 case SEG6_LOCAL_ACTION_END:
2514 if (!nl_attr_put32(
2515 &req->n, buflen,
52026569
HS
2516 SEG6_LOCAL_ACTION,
2517 SEG6_LOCAL_ACTION_END))
eab0f8f0
HS
2518 return 0;
2519 break;
2520 case SEG6_LOCAL_ACTION_END_X:
2521 if (!nl_attr_put32(
2522 &req->n, buflen,
52026569
HS
2523 SEG6_LOCAL_ACTION,
2524 SEG6_LOCAL_ACTION_END_X))
eab0f8f0
HS
2525 return 0;
2526 if (!nl_attr_put(
2527 &req->n, buflen,
8689b25a 2528 SEG6_LOCAL_NH6, &ctx->nh6,
52026569 2529 sizeof(struct in6_addr)))
eab0f8f0
HS
2530 return 0;
2531 break;
2532 case SEG6_LOCAL_ACTION_END_T:
2533 if (!nl_attr_put32(
2534 &req->n, buflen,
52026569
HS
2535 SEG6_LOCAL_ACTION,
2536 SEG6_LOCAL_ACTION_END_T))
eab0f8f0
HS
2537 return 0;
2538 if (!nl_attr_put32(
2539 &req->n, buflen,
52026569
HS
2540 SEG6_LOCAL_TABLE,
2541 ctx->table))
eab0f8f0
HS
2542 return 0;
2543 break;
2544 case SEG6_LOCAL_ACTION_END_DX4:
2545 if (!nl_attr_put32(
2546 &req->n, buflen,
52026569
HS
2547 SEG6_LOCAL_ACTION,
2548 SEG6_LOCAL_ACTION_END_DX4))
eab0f8f0
HS
2549 return 0;
2550 if (!nl_attr_put(
2551 &req->n, buflen,
8689b25a 2552 SEG6_LOCAL_NH4, &ctx->nh4,
52026569 2553 sizeof(struct in_addr)))
eab0f8f0
HS
2554 return 0;
2555 break;
2556 case SEG6_LOCAL_ACTION_END_DT6:
2557 if (!nl_attr_put32(
2558 &req->n, buflen,
52026569
HS
2559 SEG6_LOCAL_ACTION,
2560 SEG6_LOCAL_ACTION_END_DT6))
eab0f8f0
HS
2561 return 0;
2562 if (!nl_attr_put32(
2563 &req->n, buflen,
52026569
HS
2564 SEG6_LOCAL_TABLE,
2565 ctx->table))
eab0f8f0
HS
2566 return 0;
2567 break;
2568 default:
2569 zlog_err("%s: unsupport seg6local behaviour action=%u",
2570 __func__, action);
0a543b79 2571 return 0;
eab0f8f0
HS
2572 }
2573 nl_attr_nest_end(&req->n, nest);
8689b25a 2574 }
8689b25a 2575
eab0f8f0
HS
2576 if (!sid_zero(&nh->nh_srv6->seg6_segs)) {
2577 char tun_buf[4096];
2578 ssize_t tun_len;
2579 struct rtattr *nest;
76fb7ae4 2580
eab0f8f0
HS
2581 if (!nl_attr_put16(&req->n, buflen,
2582 NHA_ENCAP_TYPE,
2583 LWTUNNEL_ENCAP_SEG6))
2584 return 0;
2585 nest = nl_attr_nest(&req->n, buflen,
2586 NHA_ENCAP | NLA_F_NESTED);
2587 if (!nest)
2588 return 0;
2589 tun_len = fill_seg6ipt_encap(tun_buf,
2590 sizeof(tun_buf),
2591 &nh->nh_srv6->seg6_segs);
2592 if (tun_len < 0)
2593 return 0;
2594 if (!nl_attr_put(&req->n, buflen,
2595 SEG6_IPTUNNEL_SRH,
2596 tun_buf, tun_len))
2597 return 0;
2598 nl_attr_nest_end(&req->n, nest);
2599 }
76fb7ae4
HS
2600 }
2601
bf1626a6
MS
2602nexthop_done:
2603
2604 if (IS_ZEBRA_DEBUG_KERNEL)
2c77ddee
DS
2605 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2606 __func__, id, nh, nh->ifindex,
bd47f3a3
JU
2607 vrf_id_to_name(nh->vrf_id),
2608 nh->vrf_id, label_buf);
bdd085a8 2609 }
f820d025 2610
bdd085a8 2611 req->nhm.nh_protocol = zebra2proto(type);
f820d025 2612
f820d025
SW
2613 } else if (cmd != RTM_DELNEXTHOP) {
2614 flog_err(
2615 EC_ZEBRA_NHG_FIB_UPDATE,
2616 "Nexthop group kernel update command (%d) does not exist",
2617 cmd);
2618 return -1;
2619 }
2620
9266b315
RZ
2621 if (IS_ZEBRA_DEBUG_KERNEL)
2622 zlog_debug("%s: %s, id=%u", __func__, nl_msg_type_to_str(cmd),
2623 id);
f820d025 2624
e9a1cd93 2625 return NLMSG_ALIGN(req->n.nlmsg_len);
f820d025
SW
2626}
2627
67e3369e
JU
2628static ssize_t netlink_nexthop_msg_encoder(struct zebra_dplane_ctx *ctx,
2629 void *buf, size_t buflen)
f820d025 2630{
bf1626a6 2631 enum dplane_op_e op;
98cda54a 2632 int cmd = 0;
f820d025 2633
bf1626a6
MS
2634 op = dplane_ctx_get_op(ctx);
2635 if (op == DPLANE_OP_NH_INSTALL || op == DPLANE_OP_NH_UPDATE)
f820d025 2636 cmd = RTM_NEWNEXTHOP;
bf1626a6
MS
2637 else if (op == DPLANE_OP_NH_DELETE)
2638 cmd = RTM_DELNEXTHOP;
2639 else {
2640 flog_err(EC_ZEBRA_NHG_FIB_UPDATE,
2641 "Context received for kernel nexthop update with incorrect OP code (%u)",
2642 op);
67e3369e 2643 return -1;
f820d025
SW
2644 }
2645
67e3369e
JU
2646 return netlink_nexthop_msg_encode(cmd, ctx, buf, buflen);
2647}
2648
67e3369e
JU
2649enum netlink_msg_status
2650netlink_put_nexthop_update_msg(struct nl_batch *bth,
2651 struct zebra_dplane_ctx *ctx)
2652{
e9a1cd93
RZ
2653 /* Nothing to do if the kernel doesn't support nexthop objects */
2654 if (!kernel_nexthops_supported())
67e3369e 2655 return FRR_NETLINK_SUCCESS;
e9a1cd93 2656
67e3369e
JU
2657 return netlink_batch_add_msg(bth, ctx, netlink_nexthop_msg_encoder,
2658 false);
2659}
f820d025 2660
67e3369e
JU
2661static ssize_t netlink_newroute_msg_encoder(struct zebra_dplane_ctx *ctx,
2662 void *buf, size_t buflen)
2663{
2664 return netlink_route_multipath_msg_encode(RTM_NEWROUTE, ctx, buf,
2665 buflen, false, false);
f820d025
SW
2666}
2667
67e3369e
JU
2668static ssize_t netlink_delroute_msg_encoder(struct zebra_dplane_ctx *ctx,
2669 void *buf, size_t buflen)
2670{
2671 return netlink_route_multipath_msg_encode(RTM_DELROUTE, ctx, buf,
2672 buflen, false, false);
2673}
2674
2675enum netlink_msg_status
2676netlink_put_route_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
7cdb1a84 2677{
67e3369e 2678 int cmd;
7cdb1a84
MS
2679 const struct prefix *p = dplane_ctx_get_dest(ctx);
2680
2681 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
2682 cmd = RTM_DELROUTE;
2683 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
2684 cmd = RTM_NEWROUTE;
2685 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
2686
2687 if (p->family == AF_INET || v6_rr_semantics) {
2688 /* Single 'replace' operation */
fe5f21af
DS
2689
2690 /*
2691 * With route replace semantics in place
2692 * for v4 routes and the new route is a system
2693 * route we do not install anything.
2694 * The problem here is that the new system
2695 * route should cause us to withdraw from
2696 * the kernel the old non-system route
2697 */
67e3369e
JU
2698 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx))
2699 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2700 netlink_batch_add_msg(
2701 bth, ctx, netlink_delroute_msg_encoder,
2702 true);
7cdb1a84
MS
2703 } else {
2704 /*
2705 * So v6 route replace semantics are not in
2706 * the kernel at this point as I understand it.
2707 * so let's do a delete then an add.
2708 * In the future once v6 route replace semantics
2709 * are in we can figure out what to do here to
2710 * allow working with old and new kernels.
2711 *
2712 * I'm also intentionally ignoring the failure case
2713 * of the route delete. If that happens yeah we're
2714 * screwed.
2715 */
67e3369e
JU
2716 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2717 netlink_batch_add_msg(
2718 bth, ctx, netlink_delroute_msg_encoder,
2719 true);
7cdb1a84
MS
2720 }
2721
67e3369e
JU
2722 cmd = RTM_NEWROUTE;
2723 } else
2724 return FRR_NETLINK_ERROR;
7cdb1a84 2725
67e3369e
JU
2726 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)))
2727 return FRR_NETLINK_SUCCESS;
0be6e7d7 2728
67e3369e
JU
2729 return netlink_batch_add_msg(bth, ctx,
2730 cmd == RTM_NEWROUTE
2731 ? netlink_newroute_msg_encoder
2732 : netlink_delroute_msg_encoder,
2733 false);
2734}
7cdb1a84 2735
d9f5b2f5
SW
2736/**
2737 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2738 *
2739 * @tb: Netlink RTA data
2740 * @family: Address family in the nhmsg
8c0a24c1 2741 * @ifp: Interface connected - this should be NULL, we fill it in
d9f5b2f5
SW
2742 * @ns_id: Namspace id
2743 *
2744 * Return: New nexthop
2745 */
e22e8001
SW
2746static struct nexthop netlink_nexthop_process_nh(struct rtattr **tb,
2747 unsigned char family,
2748 struct interface **ifp,
2749 ns_id_t ns_id)
d9f5b2f5 2750{
e22e8001 2751 struct nexthop nh = {};
d9f5b2f5 2752 void *gate = NULL;
8e401b25 2753 enum nexthop_types_t type = 0;
e22e8001
SW
2754 int if_index = 0;
2755 size_t sz = 0;
7134ba70 2756 struct interface *ifp_lookup;
d9f5b2f5
SW
2757
2758 if_index = *(int *)RTA_DATA(tb[NHA_OIF]);
2759
8e401b25 2760
d9f5b2f5
SW
2761 if (tb[NHA_GATEWAY]) {
2762 switch (family) {
2763 case AF_INET:
8e401b25 2764 type = NEXTHOP_TYPE_IPV4_IFINDEX;
d9f5b2f5
SW
2765 sz = 4;
2766 break;
2767 case AF_INET6:
8e401b25 2768 type = NEXTHOP_TYPE_IPV6_IFINDEX;
d9f5b2f5
SW
2769 sz = 16;
2770 break;
2771 default:
2772 flog_warn(
2773 EC_ZEBRA_BAD_NHG_MESSAGE,
c4239c05 2774 "Nexthop gateway with bad address family (%d) received from kernel",
d9f5b2f5 2775 family);
e22e8001 2776 return nh;
d9f5b2f5
SW
2777 }
2778 gate = RTA_DATA(tb[NHA_GATEWAY]);
e22e8001 2779 } else
8e401b25 2780 type = NEXTHOP_TYPE_IFINDEX;
d9f5b2f5 2781
8e401b25 2782 if (type)
e22e8001 2783 nh.type = type;
8e401b25
SW
2784
2785 if (gate)
e22e8001 2786 memcpy(&(nh.gate), gate, sz);
8e401b25
SW
2787
2788 if (if_index)
e22e8001 2789 nh.ifindex = if_index;
8e401b25 2790
7134ba70
DS
2791 ifp_lookup =
2792 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), nh.ifindex);
2793
e22e8001 2794 if (ifp)
7134ba70
DS
2795 *ifp = ifp_lookup;
2796 if (ifp_lookup)
2797 nh.vrf_id = ifp_lookup->vrf_id;
e22e8001 2798 else {
d9f5b2f5
SW
2799 flog_warn(
2800 EC_ZEBRA_UNKNOWN_INTERFACE,
2801 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
15569c58 2802 __func__, nh.ifindex);
d9f5b2f5 2803
e22e8001 2804 nh.vrf_id = VRF_DEFAULT;
d9f5b2f5
SW
2805 }
2806
2807 if (tb[NHA_ENCAP] && tb[NHA_ENCAP_TYPE]) {
2808 uint16_t encap_type = *(uint16_t *)RTA_DATA(tb[NHA_ENCAP_TYPE]);
2809 int num_labels = 0;
6e728764 2810
d9f5b2f5
SW
2811 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
2812
e22e8001 2813 if (encap_type == LWTUNNEL_ENCAP_MPLS)
d9f5b2f5 2814 num_labels = parse_encap_mpls(tb[NHA_ENCAP], labels);
d9f5b2f5 2815
e22e8001
SW
2816 if (num_labels)
2817 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels,
d9f5b2f5 2818 labels);
d9f5b2f5
SW
2819 }
2820
2821 return nh;
2822}
2823
85f5e761 2824static int netlink_nexthop_process_group(struct rtattr **tb,
5a935f79 2825 struct nh_grp *z_grp, int z_grp_size)
d9f5b2f5 2826{
e22e8001
SW
2827 uint8_t count = 0;
2828 /* linux/nexthop.h group struct */
d9f5b2f5
SW
2829 struct nexthop_grp *n_grp = NULL;
2830
85f5e761 2831 n_grp = (struct nexthop_grp *)RTA_DATA(tb[NHA_GROUP]);
d9f5b2f5
SW
2832 count = (RTA_PAYLOAD(tb[NHA_GROUP]) / sizeof(*n_grp));
2833
2834 if (!count || (count * sizeof(*n_grp)) != RTA_PAYLOAD(tb[NHA_GROUP])) {
2835 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE,
2836 "Invalid nexthop group received from the kernel");
85f5e761 2837 return count;
d9f5b2f5
SW
2838 }
2839
5a935f79 2840 for (int i = 0; ((i < count) && (i < z_grp_size)); i++) {
e22e8001 2841 z_grp[i].id = n_grp[i].id;
df7fb580 2842 z_grp[i].weight = n_grp[i].weight + 1;
85f5e761 2843 }
d9f5b2f5
SW
2844 return count;
2845}
2846
2847/**
2848 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2849 *
2850 * @h: Netlink message header
2851 * @ns_id: Namspace id
2852 * @startup: Are we reading under startup conditions?
2853 *
2854 * Return: Result status
2855 */
2856int netlink_nexthop_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2857{
2858 int len;
2859 /* nexthop group id */
2860 uint32_t id;
2861 unsigned char family;
38e40db1 2862 int type;
e8b0e420 2863 afi_t afi = AFI_UNSPEC;
946de1b9 2864 vrf_id_t vrf_id = VRF_DEFAULT;
8c0a24c1 2865 struct interface *ifp = NULL;
d9f5b2f5 2866 struct nhmsg *nhm = NULL;
e22e8001
SW
2867 struct nexthop nh = {};
2868 struct nh_grp grp[MULTIPATH_NUM] = {};
85f5e761 2869 /* Count of nexthops in group array */
e22e8001 2870 uint8_t grp_count = 0;
e22e8001 2871 struct rtattr *tb[NHA_MAX + 1] = {};
d9f5b2f5 2872
d9f5b2f5
SW
2873 nhm = NLMSG_DATA(h);
2874
88cafda7
DS
2875 if (ns_id)
2876 vrf_id = ns_id;
2877
d9f5b2f5
SW
2878 if (startup && h->nlmsg_type != RTM_NEWNEXTHOP)
2879 return 0;
2880
2881 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct nhmsg));
2882 if (len < 0) {
2883 zlog_warn(
2884 "%s: Message received from netlink is of a broken size %d %zu",
15569c58 2885 __func__, h->nlmsg_len,
d9f5b2f5
SW
2886 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg)));
2887 return -1;
2888 }
2889
d9f5b2f5
SW
2890 netlink_parse_rtattr(tb, NHA_MAX, RTM_NHA(nhm), len);
2891
2892
2893 if (!tb[NHA_ID]) {
2894 flog_warn(
2895 EC_ZEBRA_BAD_NHG_MESSAGE,
2896 "Nexthop group without an ID received from the kernel");
2897 return -1;
2898 }
2899
2900 /* We use the ID key'd nhg table for kernel updates */
2901 id = *((uint32_t *)RTA_DATA(tb[NHA_ID]));
d9f5b2f5 2902
506efd37
AK
2903 if (zebra_evpn_mh_is_fdb_nh(id)) {
2904 /* If this is a L2 NH just ignore it */
2905 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
2906 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2907 h->nlmsg_type, id);
2908 }
2909 return 0;
2910 }
2911
e8b0e420 2912 family = nhm->nh_family;
e8b0e420
SW
2913 afi = family2afi(family);
2914
38e40db1
SW
2915 type = proto2zebra(nhm->nh_protocol, 0, true);
2916
fdee485a
SW
2917 if (IS_ZEBRA_DEBUG_KERNEL)
2918 zlog_debug("%s ID (%u) %s NS %u",
2919 nl_msg_type_to_str(h->nlmsg_type), id,
2920 nl_family_to_str(family), ns_id);
2921
2922
d9f5b2f5
SW
2923 if (h->nlmsg_type == RTM_NEWNEXTHOP) {
2924 if (tb[NHA_GROUP]) {
2925 /**
2926 * If this is a group message its only going to have
2927 * an array of nexthop IDs associated with it
2928 */
5a935f79
SW
2929 grp_count = netlink_nexthop_process_group(
2930 tb, grp, array_size(grp));
85f5e761
SW
2931 } else {
2932 if (tb[NHA_BLACKHOLE]) {
2933 /**
2934 * This nexthop is just for blackhole-ing
2935 * traffic, it should not have an OIF, GATEWAY,
2936 * or ENCAP
2937 */
e22e8001
SW
2938 nh.type = NEXTHOP_TYPE_BLACKHOLE;
2939 nh.bh_type = BLACKHOLE_UNSPEC;
2940 } else if (tb[NHA_OIF])
85f5e761
SW
2941 /**
2942 * This is a true new nexthop, so we need
2943 * to parse the gateway and device info
2944 */
2945 nh = netlink_nexthop_process_nh(tb, family,
2946 &ifp, ns_id);
e22e8001
SW
2947 else {
2948
8e401b25
SW
2949 flog_warn(
2950 EC_ZEBRA_BAD_NHG_MESSAGE,
2951 "Invalid Nexthop message received from the kernel with ID (%u)",
2952 id);
2953 return -1;
2954 }
e22e8001
SW
2955 SET_FLAG(nh.flags, NEXTHOP_FLAG_ACTIVE);
2956 if (nhm->nh_flags & RTNH_F_ONLINK)
2957 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
2958 vrf_id = nh.vrf_id;
d9f5b2f5
SW
2959 }
2960
38e40db1
SW
2961 if (zebra_nhg_kernel_find(id, &nh, grp, grp_count, vrf_id, afi,
2962 type, startup))
e22e8001 2963 return -1;
8e401b25 2964
9a1588c4 2965 } else if (h->nlmsg_type == RTM_DELNEXTHOP)
88cafda7 2966 zebra_nhg_kernel_del(id, vrf_id);
d9f5b2f5 2967
d9f5b2f5
SW
2968 return 0;
2969}
2970
2971/**
2972 * netlink_request_nexthop() - Request nextop information from the kernel
2973 * @zns: Zebra namespace
2974 * @family: AF_* netlink family
2975 * @type: RTM_* route type
2976 *
2977 * Return: Result status
2978 */
2979static int netlink_request_nexthop(struct zebra_ns *zns, int family, int type)
2980{
2981 struct {
2982 struct nlmsghdr n;
2983 struct nhmsg nhm;
2984 } req;
2985
2986 /* Form the request, specifying filter (rtattr) if needed. */
2987 memset(&req, 0, sizeof(req));
2988 req.n.nlmsg_type = type;
2989 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2990 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2991 req.nhm.nh_family = family;
2992
fd3f8e52 2993 return netlink_request(&zns->netlink_cmd, &req);
d9f5b2f5
SW
2994}
2995
7d5bb02b 2996
d9f5b2f5
SW
2997/**
2998 * netlink_nexthop_read() - Nexthop read function using netlink interface
2999 *
3000 * @zns: Zebra name space
3001 *
3002 * Return: Result status
3003 * Only called at bootstrap time.
3004 */
3005int netlink_nexthop_read(struct zebra_ns *zns)
3006{
3007 int ret;
3008 struct zebra_dplane_info dp_info;
3009
3010 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3011
3012 /* Get nexthop objects */
3013 ret = netlink_request_nexthop(zns, AF_UNSPEC, RTM_GETNEXTHOP);
3014 if (ret < 0)
3015 return ret;
3016 ret = netlink_parse_info(netlink_nexthop_change, &zns->netlink_cmd,
3017 &dp_info, 0, 1);
81505946
SW
3018
3019 if (!ret)
3020 /* If we succesfully read in nexthop objects,
3021 * this kernel must support them.
3022 */
3023 supports_nh = true;
7c99d51b
MS
3024
3025 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
3026 zlog_debug("Nexthop objects %ssupported on this kernel",
3027 supports_nh ? "" : "not ");
81505946 3028
60e0eaee 3029 return ret;
d9f5b2f5
SW
3030}
3031
3032
05657ec2
PG
3033int kernel_neigh_update(int add, int ifindex, void *addr, char *lla, int llalen,
3034 ns_id_t ns_id, uint8_t family, bool permanent)
6b8a5694 3035{
d62a17ae 3036 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
05657ec2
PG
3037 addr, lla, llalen, ns_id, family, permanent,
3038 RTPROT_ZEBRA);
6b8a5694 3039}
718e3744 3040
340845e2 3041/**
0be6e7d7
JU
3042 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3043 * evpn neighbor update as netlink messages using dataplane context object.
bbd4285b 3044 * Here, a neighbor refers to a bridge forwarding database entry for
3045 * either unicast forwarding or head-end replication or an IP neighbor
3046 * entry.
340845e2
JU
3047 * @ctx: Dataplane context
3048 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
0a27a2fe
PG
3049 * @lla: A pointer to neighbor cache link layer address
3050 * @llalen: Length of the pointer to neighbor cache link layer
3051 * address
340845e2 3052 * @ip: A neighbor cache n/w layer destination address
bbd4285b 3053 * In the case of bridge FDB, this represnts the remote
3054 * VTEP IP.
340845e2
JU
3055 * @replace_obj: Whether NEW request should replace existing object or
3056 * add to the end of the list
3057 * @family: AF_* netlink family
3058 * @type: RTN_* route type
3059 * @flags: NTF_* flags
3060 * @state: NUD_* states
d4d4ec1c
RZ
3061 * @data: data buffer pointer
3062 * @datalen: total amount of data buffer space
0a27a2fe 3063 * @protocol: protocol information
340845e2 3064 *
0be6e7d7
JU
3065 * Return: 0 when the msg doesn't fit entirely in the buffer
3066 * otherwise the number of bytes written to buf.
13d60d35 3067 */
0be6e7d7 3068static ssize_t netlink_neigh_update_msg_encode(
0a27a2fe
PG
3069 const struct zebra_dplane_ctx *ctx, int cmd, const void *lla,
3070 int llalen, const struct ipaddr *ip, bool replace_obj, uint8_t family,
3071 uint8_t type, uint8_t flags, uint16_t state, uint32_t nhg_id, bool nfy,
ccd187cd 3072 uint8_t nfy_flags, bool ext, uint32_t ext_flags, void *data,
0a27a2fe 3073 size_t datalen, uint8_t protocol)
13d60d35 3074{
d62a17ae 3075 struct {
3076 struct nlmsghdr n;
3077 struct ndmsg ndm;
d4d4ec1c
RZ
3078 char buf[];
3079 } *req = data;
340845e2
JU
3080 int ipa_len;
3081 enum dplane_op_e op;
d62a17ae 3082
0be6e7d7
JU
3083 if (datalen < sizeof(*req))
3084 return 0;
45c80fbd 3085 memset(req, 0, sizeof(*req));
d62a17ae 3086
340845e2
JU
3087 op = dplane_ctx_get_op(ctx);
3088
d4d4ec1c
RZ
3089 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3090 req->n.nlmsg_flags = NLM_F_REQUEST;
d62a17ae 3091 if (cmd == RTM_NEWNEIGH)
d4d4ec1c 3092 req->n.nlmsg_flags |=
340845e2
JU
3093 NLM_F_CREATE
3094 | (replace_obj ? NLM_F_REPLACE : NLM_F_APPEND);
d4d4ec1c
RZ
3095 req->n.nlmsg_type = cmd;
3096 req->ndm.ndm_family = family;
3097 req->ndm.ndm_type = type;
3098 req->ndm.ndm_state = state;
3099 req->ndm.ndm_flags = flags;
3100 req->ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
d62a17ae 3101
45c80fbd 3102 if (!nl_attr_put(&req->n, datalen, NDA_PROTOCOL, &protocol,
0be6e7d7
JU
3103 sizeof(protocol)))
3104 return 0;
3105
0a27a2fe
PG
3106 if (lla) {
3107 if (!nl_attr_put(&req->n, datalen, NDA_LLADDR, lla, llalen))
0be6e7d7
JU
3108 return 0;
3109 }
13d60d35 3110
f188e68e 3111 if (nfy) {
4bcdb608
NA
3112 struct rtattr *nest;
3113
3114 nest = nl_attr_nest(&req->n, datalen,
3115 NDA_FDB_EXT_ATTRS | NLA_F_NESTED);
3116 if (!nest)
3117 return 0;
3118
3119 if (!nl_attr_put(&req->n, datalen, NFEA_ACTIVITY_NOTIFY,
3120 &nfy_flags, sizeof(nfy_flags)))
f188e68e 3121 return 0;
4bcdb608
NA
3122 if (!nl_attr_put(&req->n, datalen, NFEA_DONT_REFRESH, NULL, 0))
3123 return 0;
3124
3125 nl_attr_nest_end(&req->n, nest);
f188e68e 3126 }
506efd37 3127
4bcdb608 3128
ccd187cd
AK
3129 if (ext) {
3130 if (!nl_attr_put(&req->n, datalen, NDA_EXT_FLAGS, &ext_flags,
3131 sizeof(ext_flags)))
3132 return 0;
3133 }
3134
80e19eb7
AK
3135 if (nhg_id) {
3136 if (!nl_attr_put32(&req->n, datalen, NDA_NH_ID, nhg_id))
3137 return 0;
3138 } else {
3139 ipa_len =
3140 IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
3141 if (!nl_attr_put(&req->n, datalen, NDA_DST, &ip->ip.addr,
3142 ipa_len))
3143 return 0;
3144 }
340845e2
JU
3145
3146 if (op == DPLANE_OP_MAC_INSTALL || op == DPLANE_OP_MAC_DELETE) {
3147 vlanid_t vid = dplane_ctx_mac_get_vlan(ctx);
13d60d35 3148
0be6e7d7
JU
3149 if (vid > 0) {
3150 if (!nl_attr_put16(&req->n, datalen, NDA_VLAN, vid))
3151 return 0;
3152 }
13d60d35 3153
0be6e7d7
JU
3154 if (!nl_attr_put32(&req->n, datalen, NDA_MASTER,
3155 dplane_ctx_mac_get_br_ifindex(ctx)))
3156 return 0;
340845e2 3157 }
13d60d35 3158
d4d4ec1c 3159 return NLMSG_ALIGN(req->n.nlmsg_len);
13d60d35 3160}
3161
340845e2
JU
3162/*
3163 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3164 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3165 */
67e3369e
JU
3166static ssize_t
3167netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx *ctx, int cmd,
3168 void *buf, size_t buflen)
340845e2
JU
3169{
3170 struct ethaddr dst_mac = {.octet = {0}};
88217099
PG
3171 int proto = RTPROT_ZEBRA;
3172
3173 if (dplane_ctx_get_type(ctx) != 0)
3174 proto = zebra2proto(dplane_ctx_get_type(ctx));
d4d4ec1c 3175
67e3369e 3176 return netlink_neigh_update_msg_encode(
0a27a2fe
PG
3177 ctx, cmd, (const void *)&dst_mac, ETH_ALEN,
3178 dplane_ctx_neigh_get_ipaddr(ctx), false, PF_BRIDGE, 0, NTF_SELF,
3179 (NUD_NOARP | NUD_PERMANENT), 0 /*nhg*/, false /*nfy*/,
3180 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf, buflen,
88217099 3181 proto);
340845e2
JU
3182}
3183
2232a77c 3184#ifndef NDA_RTA
d62a17ae 3185#define NDA_RTA(r) \
3186 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2232a77c 3187#endif
3188
2414abd3 3189static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 3190{
d62a17ae 3191 struct ndmsg *ndm;
3192 struct interface *ifp;
3193 struct zebra_if *zif;
d62a17ae 3194 struct rtattr *tb[NDA_MAX + 1];
3195 struct interface *br_if;
3196 struct ethaddr mac;
3197 vlanid_t vid = 0;
4b3f26f4 3198 struct in_addr vtep_ip;
d62a17ae 3199 int vid_present = 0, dst_present = 0;
d62a17ae 3200 char vid_buf[20];
3201 char dst_buf[30];
a37f4598 3202 bool sticky;
f188e68e
AK
3203 bool local_inactive = false;
3204 bool dp_static = false;
3205 uint32_t nhg_id = 0;
d62a17ae 3206
3207 ndm = NLMSG_DATA(h);
3208
2853fed6 3209 /* We only process macfdb notifications if EVPN is enabled */
3210 if (!is_evpn_enabled())
3211 return 0;
3212
4b3f26f4 3213 /* Parse attributes and extract fields of interest. Do basic
3214 * validation of the fields.
3215 */
4bcdb608
NA
3216 netlink_parse_rtattr_flags(tb, NDA_MAX, NDA_RTA(ndm), len,
3217 NLA_F_NESTED);
d62a17ae 3218
3219 if (!tb[NDA_LLADDR]) {
28bd0652 3220 if (IS_ZEBRA_DEBUG_KERNEL)
4b3f26f4 3221 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
28bd0652 3222 nl_msg_type_to_str(h->nlmsg_type),
4b3f26f4 3223 ndm->ndm_ifindex);
d62a17ae 3224 return 0;
3225 }
3226
ff8b7eb8 3227 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
3228 if (IS_ZEBRA_DEBUG_KERNEL)
3229 zlog_debug(
4b3f26f4 3230 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3231 nl_msg_type_to_str(h->nlmsg_type), ndm->ndm_ifindex,
28bd0652 3232 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
d62a17ae 3233 return 0;
3234 }
3235
ff8b7eb8 3236 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 3237
3238 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
3239 vid_present = 1;
d7c0a89a 3240 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
772270f3 3241 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
d62a17ae 3242 }
3243
3244 if (tb[NDA_DST]) {
3245 /* TODO: Only IPv4 supported now. */
3246 dst_present = 1;
4b3f26f4 3247 memcpy(&vtep_ip.s_addr, RTA_DATA(tb[NDA_DST]),
d62a17ae 3248 IPV4_MAX_BYTELEN);
9bcef951
MS
3249 snprintfrr(dst_buf, sizeof(dst_buf), " dst %pI4",
3250 &vtep_ip);
d62a17ae 3251 }
3252
f188e68e
AK
3253 if (tb[NDA_NH_ID])
3254 nhg_id = *(uint32_t *)RTA_DATA(tb[NDA_NH_ID]);
3255
3256 if (ndm->ndm_state & NUD_STALE)
3257 local_inactive = true;
3258
4bcdb608
NA
3259 if (tb[NDA_FDB_EXT_ATTRS]) {
3260 struct rtattr *attr = tb[NDA_FDB_EXT_ATTRS];
3261 struct rtattr *nfea_tb[NFEA_MAX + 1] = {0};
3262
3263 netlink_parse_rtattr_nested(nfea_tb, NFEA_MAX, attr);
3264 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
3265 uint8_t nfy_flags;
f188e68e 3266
4bcdb608
NA
3267 nfy_flags = *(uint8_t *)RTA_DATA(
3268 nfea_tb[NFEA_ACTIVITY_NOTIFY]);
3269 if (nfy_flags & FDB_NOTIFY_BIT)
3270 dp_static = true;
3271 if (nfy_flags & FDB_NOTIFY_INACTIVE_BIT)
3272 local_inactive = true;
3273 }
f188e68e
AK
3274 }
3275
d62a17ae 3276 if (IS_ZEBRA_DEBUG_KERNEL)
ef7b8be4 3277 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
d62a17ae 3278 nl_msg_type_to_str(h->nlmsg_type),
d62a17ae 3279 ndm->ndm_ifindex, vid_present ? vid_buf : "",
ef7b8be4 3280 ndm->ndm_state, ndm->ndm_flags, &mac,
f188e68e 3281 dst_present ? dst_buf : "", nhg_id);
d62a17ae 3282
4b3f26f4 3283 /* The interface should exist. */
3284 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
3285 ndm->ndm_ifindex);
3286 if (!ifp || !ifp->info)
3287 return 0;
3288
3289 /* The interface should be something we're interested in. */
3290 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
3291 return 0;
3292
3293 zif = (struct zebra_if *)ifp->info;
3294 if ((br_if = zif->brslave_info.br_if) == NULL) {
3295 if (IS_ZEBRA_DEBUG_KERNEL)
3296 zlog_debug(
3297 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3298 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
3299 ndm->ndm_ifindex,
3300 zif->brslave_info.bridge_ifindex);
3301 return 0;
3302 }
3303
f188e68e 3304 sticky = !!(ndm->ndm_flags & NTF_STICKY);
4b3f26f4 3305
28bd0652
DS
3306 if (filter_vlan && vid != filter_vlan) {
3307 if (IS_ZEBRA_DEBUG_KERNEL)
d6951e5e 3308 zlog_debug(" Filtered due to filter vlan: %d",
28bd0652 3309 filter_vlan);
d62a17ae 3310 return 0;
28bd0652 3311 }
d62a17ae 3312
3313 /* If add or update, do accordingly if learnt on a "local" interface; if
3314 * the notification is over VxLAN, this has to be related to
3315 * multi-homing,
3316 * so perform an implicit delete of any local entry (if it exists).
3317 */
3318 if (h->nlmsg_type == RTM_NEWNEIGH) {
4b3f26f4 3319 /* Drop "permanent" entries. */
3320 if (ndm->ndm_state & NUD_PERMANENT) {
3321 if (IS_ZEBRA_DEBUG_KERNEL)
d6951e5e
DL
3322 zlog_debug(
3323 " Dropping entry because of NUD_PERMANENT");
3324 return 0;
4b3f26f4 3325 }
3326
d62a17ae 3327 if (IS_ZEBRA_IF_VXLAN(ifp))
15400f95
AK
3328 return zebra_vxlan_dp_network_mac_add(
3329 ifp, br_if, &mac, vid, nhg_id, sticky,
3330 !!(ndm->ndm_flags & NTF_EXT_LEARNED));
d62a17ae 3331
3332 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
f188e68e 3333 sticky, local_inactive, dp_static);
d62a17ae 3334 }
3335
3336 /* This is a delete notification.
4b3f26f4 3337 * Ignore the notification with IP dest as it may just signify that the
3338 * MAC has moved from remote to local. The exception is the special
3339 * all-zeros MAC that represents the BUM flooding entry; we may have
3340 * to readd it. Otherwise,
d62a17ae 3341 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3342 * 2. For a MAC over "local" interface, delete the mac
3343 * Note: We will get notifications from both bridge driver and VxLAN
3344 * driver.
d62a17ae 3345 */
f188e68e
AK
3346 if (nhg_id)
3347 return 0;
3348
28bd0652 3349 if (dst_present) {
4b3f26f4 3350 u_char zero_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3351
3352 if (!memcmp(zero_mac, mac.octet, ETH_ALEN))
3353 return zebra_vxlan_check_readd_vtep(ifp, vtep_ip);
d62a17ae 3354 return 0;
28bd0652 3355 }
d62a17ae 3356
3357 if (IS_ZEBRA_IF_VXLAN(ifp))
15400f95 3358 return zebra_vxlan_dp_network_mac_del(ifp, br_if, &mac, vid);
d62a17ae 3359
3360 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
2232a77c 3361}
3362
2414abd3 3363static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 3364{
d62a17ae 3365 int len;
3366 struct ndmsg *ndm;
2232a77c 3367
d62a17ae 3368 if (h->nlmsg_type != RTM_NEWNEIGH)
3369 return 0;
2232a77c 3370
d62a17ae 3371 /* Length validity. */
3372 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3373 if (len < 0)
3374 return -1;
2232a77c 3375
d62a17ae 3376 /* We are interested only in AF_BRIDGE notifications. */
3377 ndm = NLMSG_DATA(h);
3378 if (ndm->ndm_family != AF_BRIDGE)
3379 return 0;
2232a77c 3380
2414abd3 3381 return netlink_macfdb_change(h, len, ns_id);
2232a77c 3382}
3383
3384/* Request for MAC FDB information from the kernel */
85a75f1e
MS
3385static int netlink_request_macs(struct nlsock *netlink_cmd, int family,
3386 int type, ifindex_t master_ifindex)
2232a77c 3387{
d62a17ae 3388 struct {
3389 struct nlmsghdr n;
3390 struct ifinfomsg ifm;
3391 char buf[256];
3392 } req;
3393
3394 /* Form the request, specifying filter (rtattr) if needed. */
3395 memset(&req, 0, sizeof(req));
3396 req.n.nlmsg_type = type;
718f9b0f 3397 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 3398 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
3399 req.ifm.ifi_family = family;
3400 if (master_ifindex)
312a6bee 3401 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
d62a17ae 3402
fd3f8e52 3403 return netlink_request(netlink_cmd, &req);
2232a77c 3404}
3405
3406/*
3407 * MAC forwarding database read using netlink interface. This is invoked
3408 * at startup.
3409 */
d62a17ae 3410int netlink_macfdb_read(struct zebra_ns *zns)
2232a77c 3411{
d62a17ae 3412 int ret;
85a75f1e
MS
3413 struct zebra_dplane_info dp_info;
3414
3415 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 3416
3417 /* Get bridge FDB table. */
85a75f1e
MS
3418 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
3419 0);
d62a17ae 3420 if (ret < 0)
3421 return ret;
3422 /* We are reading entire table. */
3423 filter_vlan = 0;
85a75f1e
MS
3424 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3425 &dp_info, 0, 1);
d62a17ae 3426
3427 return ret;
2232a77c 3428}
3429
3430/*
3431 * MAC forwarding database read using netlink interface. This is for a
3432 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3433 */
d62a17ae 3434int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
3435 struct interface *br_if)
2232a77c 3436{
d62a17ae 3437 struct zebra_if *br_zif;
3438 struct zebra_if *zif;
3439 struct zebra_l2info_vxlan *vxl;
85a75f1e 3440 struct zebra_dplane_info dp_info;
d62a17ae 3441 int ret = 0;
3442
85a75f1e 3443 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 3444
3445 /* Save VLAN we're filtering on, if needed. */
3446 br_zif = (struct zebra_if *)br_if->info;
3447 zif = (struct zebra_if *)ifp->info;
3448 vxl = &zif->l2info.vxl;
3449 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
3450 filter_vlan = vxl->access_vlan;
3451
3452 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3453 */
85a75f1e 3454 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
d62a17ae 3455 br_if->ifindex);
3456 if (ret < 0)
3457 return ret;
85a75f1e
MS
3458 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3459 &dp_info, 0, 0);
d62a17ae 3460
3461 /* Reset VLAN filter. */
3462 filter_vlan = 0;
3463 return ret;
2232a77c 3464}
3465
67fb9374
CS
3466
3467/* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3468static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns,
3469 int family,
3470 int type,
3471 struct interface *br_if,
3472 struct ethaddr *mac,
3473 vlanid_t vid)
3474{
3475 struct {
3476 struct nlmsghdr n;
3477 struct ndmsg ndm;
3478 char buf[256];
3479 } req;
3480 struct zebra_if *br_zif;
67fb9374
CS
3481
3482 memset(&req, 0, sizeof(req));
3483 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3484 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3485 req.n.nlmsg_flags = NLM_F_REQUEST;
3486 req.ndm.ndm_family = family; /* AF_BRIDGE */
3487 /* req.ndm.ndm_state = NUD_REACHABLE; */
3488
312a6bee 3489 nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
67fb9374
CS
3490
3491 br_zif = (struct zebra_if *)br_if->info;
3492 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0)
312a6bee 3493 nl_attr_put16(&req.n, sizeof(req), NDA_VLAN, vid);
67fb9374 3494
312a6bee 3495 nl_attr_put32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
67fb9374
CS
3496
3497 if (IS_ZEBRA_DEBUG_KERNEL)
bd47f3a3 3498 zlog_debug(
ef7b8be4 3499 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
bd47f3a3
JU
3500 __func__, nl_family_to_str(req.ndm.ndm_family),
3501 br_if->name, br_if->ifindex,
ef7b8be4 3502 vrf_id_to_name(br_if->vrf_id), br_if->vrf_id, mac, vid);
67fb9374 3503
fd3f8e52 3504 return netlink_request(&zns->netlink_cmd, &req);
67fb9374
CS
3505}
3506
3507int netlink_macfdb_read_specific_mac(struct zebra_ns *zns,
3508 struct interface *br_if,
3509 struct ethaddr *mac, vlanid_t vid)
3510{
3511 int ret = 0;
3512 struct zebra_dplane_info dp_info;
3513
3514 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3515
3516 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3517 */
3518 ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE,
3519 RTM_GETNEIGH,
3520 br_if, mac, vid);
3521 if (ret < 0)
3522 return ret;
3523
3524 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
3525 &dp_info, 1, 0);
3526
3527 return ret;
3528}
036d93c0
MS
3529
3530/*
3531 * Netlink-specific handler for MAC updates using dataplane context object.
3532 */
67e3369e
JU
3533ssize_t netlink_macfdb_update_ctx(struct zebra_dplane_ctx *ctx, void *data,
3534 size_t datalen)
2232a77c 3535{
340845e2 3536 struct ipaddr vtep_ip;
036d93c0 3537 vlanid_t vid;
d4d4ec1c
RZ
3538 ssize_t total;
3539 int cmd;
340845e2
JU
3540 uint8_t flags;
3541 uint16_t state;
506efd37 3542 uint32_t nhg_id;
f188e68e
AK
3543 uint32_t update_flags;
3544 bool nfy = false;
3545 uint8_t nfy_flags = 0;
88217099
PG
3546 int proto = RTPROT_ZEBRA;
3547
3548 if (dplane_ctx_get_type(ctx) != 0)
3549 proto = zebra2proto(dplane_ctx_get_type(ctx));
d4d4ec1c
RZ
3550
3551 cmd = dplane_ctx_get_op(ctx) == DPLANE_OP_MAC_INSTALL
3552 ? RTM_NEWNEIGH : RTM_DELNEIGH;
036d93c0 3553
f188e68e 3554 flags = NTF_MASTER;
340845e2 3555 state = NUD_REACHABLE;
d62a17ae 3556
f188e68e
AK
3557 update_flags = dplane_ctx_mac_get_update_flags(ctx);
3558 if (update_flags & DPLANE_MAC_REMOTE) {
3559 flags |= NTF_SELF;
60e372e9
AK
3560 if (dplane_ctx_mac_is_sticky(ctx)) {
3561 /* NUD_NOARP prevents the entry from expiring */
3562 state |= NUD_NOARP;
3563 /* sticky the entry from moving */
f188e68e 3564 flags |= NTF_STICKY;
60e372e9 3565 } else {
f188e68e 3566 flags |= NTF_EXT_LEARNED;
60e372e9 3567 }
f188e68e
AK
3568 /* if it was static-local previously we need to clear the
3569 * notify flags on replace with remote
3570 */
3571 if (update_flags & DPLANE_MAC_WAS_STATIC)
3572 nfy = true;
3573 } else {
3574 /* local mac */
3575 if (update_flags & DPLANE_MAC_SET_STATIC) {
4bcdb608 3576 nfy_flags |= FDB_NOTIFY_BIT;
f188e68e
AK
3577 state |= NUD_NOARP;
3578 }
3579
3580 if (update_flags & DPLANE_MAC_SET_INACTIVE)
4bcdb608 3581 nfy_flags |= FDB_NOTIFY_INACTIVE_BIT;
f188e68e
AK
3582
3583 nfy = true;
3584 }
478566d6 3585
506efd37 3586 nhg_id = dplane_ctx_mac_get_nhg_id(ctx);
340845e2
JU
3587 vtep_ip.ipaddr_v4 = *(dplane_ctx_mac_get_vtep_ip(ctx));
3588 SET_IPADDR_V4(&vtep_ip);
d62a17ae 3589
036d93c0 3590 if (IS_ZEBRA_DEBUG_KERNEL) {
478566d6 3591 char vid_buf[20];
506efd37 3592 const struct ethaddr *mac = dplane_ctx_mac_get_addr(ctx);
478566d6 3593
340845e2
JU
3594 vid = dplane_ctx_mac_get_vlan(ctx);
3595 if (vid > 0)
478566d6
MS
3596 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
3597 else
3598 vid_buf[0] = '\0';
036d93c0 3599
ccd187cd 3600 zlog_debug(
ef7b8be4 3601 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
ccd187cd
AK
3602 nl_msg_type_to_str(cmd), nl_family_to_str(AF_BRIDGE),
3603 dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx),
3604 vid_buf, dplane_ctx_mac_is_sticky(ctx) ? "sticky " : "",
ef7b8be4 3605 mac, &vtep_ip, nhg_id,
ccd187cd
AK
3606 (update_flags & DPLANE_MAC_REMOTE) ? " rem" : "",
3607 (update_flags & DPLANE_MAC_WAS_STATIC) ? " clr_sync"
3608 : "",
3609 (update_flags & DPLANE_MAC_SET_STATIC) ? " static" : "",
3610 (update_flags & DPLANE_MAC_SET_INACTIVE) ? " inactive"
3611 : "",
3612 nfy ? " nfy" : "");
036d93c0 3613 }
d62a17ae 3614
0be6e7d7 3615 total = netlink_neigh_update_msg_encode(
0a27a2fe
PG
3616 ctx, cmd, (const void *)dplane_ctx_mac_get_addr(ctx), ETH_ALEN,
3617 &vtep_ip, true, AF_BRIDGE, 0, flags, state, nhg_id, nfy,
3618 nfy_flags, false /*ext*/, 0 /*ext_flags*/, data, datalen,
88217099 3619 proto);
d4d4ec1c
RZ
3620
3621 return total;
2232a77c 3622}
3623
f17b99ed
DS
3624/*
3625 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3626 * 5549 support, re-install them.
3627 */
3628static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif,
9b036974
DS
3629 struct interface *ifp, struct ipaddr *ip,
3630 bool handle_failed)
f17b99ed
DS
3631{
3632 if (ndm->ndm_family != AF_INET)
3633 return;
3634
3635 if (!zif->v6_2_v4_ll_neigh_entry)
3636 return;
3637
3638 if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr)
3639 return;
3640
9b036974
DS
3641 if (handle_failed && ndm->ndm_state & NUD_FAILED) {
3642 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3643 ifp->name);
3644 return;
3645 }
3646
f17b99ed
DS
3647 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true);
3648}
3649
d62a17ae 3650#define NUD_VALID \
3651 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3652 | NUD_DELAY)
f188e68e
AK
3653#define NUD_LOCAL_ACTIVE \
3654 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
2232a77c 3655
2414abd3 3656static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 3657{
d62a17ae 3658 struct ndmsg *ndm;
3659 struct interface *ifp;
3660 struct zebra_if *zif;
d62a17ae 3661 struct rtattr *tb[NDA_MAX + 1];
3662 struct interface *link_if;
3663 struct ethaddr mac;
3664 struct ipaddr ip;
bd47f3a3 3665 struct vrf *vrf;
d62a17ae 3666 char buf[ETHER_ADDR_STRLEN];
d62a17ae 3667 int mac_present = 0;
a37f4598 3668 bool is_ext;
3669 bool is_router;
f188e68e 3670 bool local_inactive;
7c0e4dc6
AK
3671 uint32_t ext_flags = 0;
3672 bool dp_static = false;
7723e8d3
PG
3673 int l2_len = 0;
3674 int cmd;
d62a17ae 3675
3676 ndm = NLMSG_DATA(h);
3677
3678 /* The interface should exist. */
5895d33f 3679 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 3680 ndm->ndm_ifindex);
2853fed6 3681 if (!ifp || !ifp->info)
d62a17ae 3682 return 0;
3683
bd47f3a3 3684 vrf = vrf_lookup_by_id(ifp->vrf_id);
20089ae2
DS
3685 zif = (struct zebra_if *)ifp->info;
3686
3687 /* Parse attributes and extract fields of interest. */
20089ae2
DS
3688 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
3689
3690 if (!tb[NDA_DST]) {
bd47f3a3 3691 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
9df414fe
QY
3692 nl_msg_type_to_str(h->nlmsg_type),
3693 nl_family_to_str(ndm->ndm_family), ifp->name,
bd47f3a3 3694 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id);
d62a17ae 3695 return 0;
20089ae2
DS
3696 }
3697
3698 memset(&ip, 0, sizeof(struct ipaddr));
3699 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
3700 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
3701
f17b99ed
DS
3702 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3703 if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) {
9b036974 3704 netlink_handle_5549(ndm, zif, ifp, &ip, false);
28bd0652
DS
3705 if (IS_ZEBRA_DEBUG_KERNEL)
3706 zlog_debug(
1d5453d6 3707 " Neighbor Entry Received is a 5549 entry, finished");
20089ae2
DS
3708 return 0;
3709 }
d62a17ae 3710
f17b99ed 3711 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
9b036974
DS
3712 if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID))
3713 netlink_handle_5549(ndm, zif, ifp, &ip, true);
f17b99ed 3714
7723e8d3
PG
3715 /* we send link layer information to client:
3716 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3717 * - struct ipaddr ( for DEL and GET)
3718 * - struct ethaddr mac; (for NEW)
3719 */
3720 if (h->nlmsg_type == RTM_NEWNEIGH)
3721 cmd = ZEBRA_NHRP_NEIGH_ADDED;
3722 else if (h->nlmsg_type == RTM_GETNEIGH)
3723 cmd = ZEBRA_NHRP_NEIGH_GET;
3724 else if (h->nlmsg_type == RTM_DELNEIGH)
3725 cmd = ZEBRA_NHRP_NEIGH_REMOVED;
3726 else {
3727 zlog_debug("%s(): unknown nlmsg type %u", __func__,
3728 h->nlmsg_type);
3729 return 0;
3730 }
3731 if (tb[NDA_LLADDR]) {
3732 /* copy LLADDR information */
3733 l2_len = RTA_PAYLOAD(tb[NDA_LLADDR]);
3734 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), l2_len);
3735 }
d603c077
PG
3736 if (l2_len == IPV4_MAX_BYTELEN || l2_len == 0) {
3737 union sockunion link_layer_ipv4;
3738
3739 if (l2_len) {
3740 sockunion_family(&link_layer_ipv4) = AF_INET;
3741 memcpy((void *)sockunion_get_addr(&link_layer_ipv4),
3742 &mac, l2_len);
3743 } else
3744 sockunion_family(&link_layer_ipv4) = AF_UNSPEC;
7723e8d3 3745 zsend_nhrp_neighbor_notify(cmd, ifp, &ip, ndm->ndm_state,
d603c077
PG
3746 &link_layer_ipv4);
3747 }
7723e8d3
PG
3748
3749 if (h->nlmsg_type == RTM_GETNEIGH)
3750 return 0;
3751
d62a17ae 3752 /* The neighbor is present on an SVI. From this, we locate the
3753 * underlying
3754 * bridge because we're only interested in neighbors on a VxLAN bridge.
3755 * The bridge is located based on the nature of the SVI:
3756 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3757 * interface
3758 * and is linked to the bridge
3759 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3760 * inteface
3761 * itself
3762 */
3763 if (IS_ZEBRA_IF_VLAN(ifp)) {
5895d33f 3764 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
71349e03 3765 zif->link_ifindex);
d62a17ae 3766 if (!link_if)
3767 return 0;
3768 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
3769 link_if = ifp;
28bd0652
DS
3770 else {
3771 if (IS_ZEBRA_DEBUG_KERNEL)
3772 zlog_debug(
1d5453d6 3773 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
d62a17ae 3774 return 0;
28bd0652 3775 }
d62a17ae 3776
d62a17ae 3777 memset(&mac, 0, sizeof(struct ethaddr));
d62a17ae 3778 if (h->nlmsg_type == RTM_NEWNEIGH) {
3779 if (tb[NDA_LLADDR]) {
ff8b7eb8 3780 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
3781 if (IS_ZEBRA_DEBUG_KERNEL)
3782 zlog_debug(
bd47f3a3 3783 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
28bd0652
DS
3784 nl_msg_type_to_str(
3785 h->nlmsg_type),
3786 nl_family_to_str(
3787 ndm->ndm_family),
3788 ifp->name, ndm->ndm_ifindex,
bd47f3a3 3789 VRF_LOGNAME(vrf), ifp->vrf_id,
28bd0652
DS
3790 (unsigned long)RTA_PAYLOAD(
3791 tb[NDA_LLADDR]));
d62a17ae 3792 return 0;
3793 }
3794
3795 mac_present = 1;
ff8b7eb8 3796 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 3797 }
3798
a37f4598 3799 is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED);
3800 is_router = !!(ndm->ndm_flags & NTF_ROUTER);
d62a17ae 3801
7c0e4dc6
AK
3802 if (tb[NDA_EXT_FLAGS]) {
3803 ext_flags = *(uint32_t *)RTA_DATA(tb[NDA_EXT_FLAGS]);
3804 if (ext_flags & NTF_E_MH_PEER_SYNC)
3805 dp_static = true;
3806 }
3807
d62a17ae 3808 if (IS_ZEBRA_DEBUG_KERNEL)
3809 zlog_debug(
ef7b8be4 3810 "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 3811 nl_msg_type_to_str(h->nlmsg_type),
3812 nl_family_to_str(ndm->ndm_family), ifp->name,
bd47f3a3 3813 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id,
ef7b8be4 3814 &ip,
d62a17ae 3815 mac_present
3816 ? prefix_mac2str(&mac, buf, sizeof(buf))
3817 : "",
7c0e4dc6 3818 ndm->ndm_state, ndm->ndm_flags, ext_flags);
d62a17ae 3819
3820 /* If the neighbor state is valid for use, process as an add or
3821 * update
3822 * else process as a delete. Note that the delete handling may
3823 * result
3824 * in re-adding the neighbor if it is a valid "remote" neighbor.
3825 */
f188e68e 3826 if (ndm->ndm_state & NUD_VALID) {
c7bfd085
AK
3827 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3828 local_inactive =
3829 !(ndm->ndm_state & NUD_LOCAL_ACTIVE);
3830 else
3831 /* If EVPN-MH is not enabled we treat STALE
3832 * neighbors as locally-active and advertise
3833 * them
3834 */
3835 local_inactive = false;
f188e68e 3836
ee69da27 3837 return zebra_vxlan_handle_kernel_neigh_update(
7c0e4dc6
AK
3838 ifp, link_if, &ip, &mac, ndm->ndm_state, is_ext,
3839 is_router, local_inactive, dp_static);
f188e68e 3840 }
d62a17ae 3841
ee69da27 3842 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
d62a17ae 3843 }
3844
3845 if (IS_ZEBRA_DEBUG_KERNEL)
ef7b8be4 3846 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
d62a17ae 3847 nl_msg_type_to_str(h->nlmsg_type),
3848 nl_family_to_str(ndm->ndm_family), ifp->name,
bd47f3a3 3849 ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id,
ef7b8be4 3850 &ip);
d62a17ae 3851
3852 /* Process the delete - it may result in re-adding the neighbor if it is
3853 * a valid "remote" neighbor.
3854 */
ee69da27 3855 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2232a77c 3856}
3857
2414abd3 3858static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 3859{
d62a17ae 3860 int len;
3861 struct ndmsg *ndm;
2232a77c 3862
d62a17ae 3863 if (h->nlmsg_type != RTM_NEWNEIGH)
3864 return 0;
2232a77c 3865
d62a17ae 3866 /* Length validity. */
3867 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3868 if (len < 0)
3869 return -1;
2232a77c 3870
d62a17ae 3871 /* We are interested only in AF_INET or AF_INET6 notifications. */
3872 ndm = NLMSG_DATA(h);
3873 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
3874 return 0;
2232a77c 3875
2414abd3 3876 return netlink_neigh_change(h, len);
2232a77c 3877}
3878
3879/* Request for IP neighbor information from the kernel */
85a75f1e
MS
3880static int netlink_request_neigh(struct nlsock *netlink_cmd, int family,
3881 int type, ifindex_t ifindex)
2232a77c 3882{
d62a17ae 3883 struct {
3884 struct nlmsghdr n;
3885 struct ndmsg ndm;
3886 char buf[256];
3887 } req;
3888
3889 /* Form the request, specifying filter (rtattr) if needed. */
3890 memset(&req, 0, sizeof(req));
3891 req.n.nlmsg_type = type;
718f9b0f 3892 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 3893 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3894 req.ndm.ndm_family = family;
3895 if (ifindex)
312a6bee 3896 nl_attr_put32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
d62a17ae 3897
fd3f8e52 3898 return netlink_request(netlink_cmd, &req);
2232a77c 3899}
3900
3901/*
3902 * IP Neighbor table read using netlink interface. This is invoked
3903 * at startup.
3904 */
d62a17ae 3905int netlink_neigh_read(struct zebra_ns *zns)
2232a77c 3906{
d62a17ae 3907 int ret;
85a75f1e
MS
3908 struct zebra_dplane_info dp_info;
3909
3910 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3911
d62a17ae 3912 /* Get IP neighbor table. */
85a75f1e
MS
3913 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
3914 0);
d62a17ae 3915 if (ret < 0)
3916 return ret;
85a75f1e
MS
3917 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3918 &dp_info, 0, 1);
2232a77c 3919
d62a17ae 3920 return ret;
2232a77c 3921}
3922
3923/*
3924 * IP Neighbor table read using netlink interface. This is for a specific
3925 * VLAN device.
3926 */
d62a17ae 3927int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 3928{
d62a17ae 3929 int ret = 0;
85a75f1e
MS
3930 struct zebra_dplane_info dp_info;
3931
3932 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3933
85a75f1e 3934 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
d62a17ae 3935 vlan_if->ifindex);
3936 if (ret < 0)
3937 return ret;
85a75f1e
MS
3938 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3939 &dp_info, 0, 0);
2232a77c 3940
d62a17ae 3941 return ret;
2232a77c 3942}
3943
67fb9374
CS
3944/*
3945 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3946 * read using netlink interface.
3947 */
3948static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns,
3949 int type, struct ipaddr *ip,
3950 ifindex_t ifindex)
3951{
3952 struct {
3953 struct nlmsghdr n;
3954 struct ndmsg ndm;
3955 char buf[256];
3956 } req;
3957 int ipa_len;
3958
3959 /* Form the request, specifying filter (rtattr) if needed. */
3960 memset(&req, 0, sizeof(req));
3961 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3962 req.n.nlmsg_flags = NLM_F_REQUEST;
3963 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3964 req.ndm.ndm_ifindex = ifindex;
3965
3966 if (IS_IPADDR_V4(ip)) {
3967 ipa_len = IPV4_MAX_BYTELEN;
3968 req.ndm.ndm_family = AF_INET;
3969
3970 } else {
3971 ipa_len = IPV6_MAX_BYTELEN;
3972 req.ndm.ndm_family = AF_INET6;
3973 }
3974
312a6bee 3975 nl_attr_put(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
67fb9374 3976
ef7b8be4
DL
3977 if (IS_ZEBRA_DEBUG_KERNEL)
3978 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
7c26c121 3979 __func__, nl_msg_type_to_str(type),
ef7b8be4
DL
3980 nl_family_to_str(req.ndm.ndm_family), ifindex, ip,
3981 req.n.nlmsg_flags);
7c26c121 3982
fd3f8e52 3983 return netlink_request(&zns->netlink_cmd, &req);
67fb9374
CS
3984}
3985
3986int netlink_neigh_read_specific_ip(struct ipaddr *ip,
3987 struct interface *vlan_if)
3988{
3989 int ret = 0;
3990 struct zebra_ns *zns;
3991 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vlan_if->vrf_id);
67fb9374
CS
3992 struct zebra_dplane_info dp_info;
3993
3994 zns = zvrf->zns;
3995
3996 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3997
3998 if (IS_ZEBRA_DEBUG_KERNEL)
ef7b8be4
DL
3999 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4000 __func__, vlan_if->name, vlan_if->ifindex, ip,
bd47f3a3 4001 vrf_id_to_name(vlan_if->vrf_id), vlan_if->vrf_id);
67fb9374
CS
4002
4003 ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip,
4004 vlan_if->ifindex);
4005 if (ret < 0)
4006 return ret;
4007
4008 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
4009 &dp_info, 1, 0);
4010
4011 return ret;
4012}
4013
2414abd3 4014int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
2232a77c 4015{
d62a17ae 4016 int len;
4017 struct ndmsg *ndm;
2232a77c 4018
7723e8d3
PG
4019 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH
4020 || h->nlmsg_type == RTM_GETNEIGH))
d62a17ae 4021 return 0;
2232a77c 4022
d62a17ae 4023 /* Length validity. */
4024 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
9bdf8618 4025 if (len < 0) {
15569c58
DA
4026 zlog_err(
4027 "%s: Message received from netlink is of a broken size %d %zu",
4028 __func__, h->nlmsg_len,
4029 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
d62a17ae 4030 return -1;
9bdf8618 4031 }
2232a77c 4032
d62a17ae 4033 /* Is this a notification for the MAC FDB or IP neighbor table? */
4034 ndm = NLMSG_DATA(h);
4035 if (ndm->ndm_family == AF_BRIDGE)
2414abd3 4036 return netlink_macfdb_change(h, len, ns_id);
2232a77c 4037
d62a17ae 4038 if (ndm->ndm_type != RTN_UNICAST)
4039 return 0;
2232a77c 4040
d62a17ae 4041 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
2414abd3 4042 return netlink_ipneigh_change(h, len, ns_id);
8a1b681c 4043 else {
9df414fe 4044 flog_warn(
e914ccbe 4045 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
4046 "Invalid address family: %u received from kernel neighbor change: %s",
4047 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
4048 return 0;
4049 }
2232a77c 4050
d62a17ae 4051 return 0;
2232a77c 4052}
4053
931fa60c
MS
4054/*
4055 * Utility neighbor-update function, using info from dplane context.
4056 */
67e3369e
JU
4057static ssize_t netlink_neigh_update_ctx(const struct zebra_dplane_ctx *ctx,
4058 int cmd, void *buf, size_t buflen)
2232a77c 4059{
931fa60c 4060 const struct ipaddr *ip;
0a27a2fe
PG
4061 const struct ethaddr *mac = NULL;
4062 const struct ipaddr *link_ip = NULL;
4063 const void *link_ptr = NULL;
4064 char buf2[ETHER_ADDR_STRLEN];
4065
4066 int llalen;
931fa60c
MS
4067 uint8_t flags;
4068 uint16_t state;
340845e2 4069 uint8_t family;
ccd187cd
AK
4070 uint32_t update_flags;
4071 uint32_t ext_flags = 0;
4072 bool ext = false;
88217099
PG
4073 int proto = RTPROT_ZEBRA;
4074
4075 if (dplane_ctx_get_type(ctx) != 0)
4076 proto = zebra2proto(dplane_ctx_get_type(ctx));
d62a17ae 4077
931fa60c 4078 ip = dplane_ctx_neigh_get_ipaddr(ctx);
931fa60c 4079
0a27a2fe
PG
4080 if (dplane_ctx_get_op(ctx) == DPLANE_OP_NEIGH_IP_INSTALL
4081 || dplane_ctx_get_op(ctx) == DPLANE_OP_NEIGH_IP_DELETE) {
4082 link_ip = dplane_ctx_neigh_get_link_ip(ctx);
4083 llalen = IPADDRSZ(link_ip);
4084 link_ptr = (const void *)&(link_ip->ip.addr);
4085 ipaddr2str(link_ip, buf2, sizeof(buf2));
4086 } else {
4087 mac = dplane_ctx_neigh_get_mac(ctx);
4088 llalen = ETH_ALEN;
4089 link_ptr = (const void *)mac;
4090 if (is_zero_mac(mac))
4091 mac = NULL;
4092 if (mac)
4093 prefix_mac2str(mac, buf2, sizeof(buf2));
4094 else
4095 snprintf(buf2, sizeof(buf2), "null");
4096 }
ccd187cd 4097 update_flags = dplane_ctx_neigh_get_update_flags(ctx);
931fa60c
MS
4098 flags = neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx));
4099 state = neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx));
4100
340845e2 4101 family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
d62a17ae 4102
ccd187cd
AK
4103 if (update_flags & DPLANE_NEIGH_REMOTE) {
4104 flags |= NTF_EXT_LEARNED;
4105 /* if it was static-local previously we need to clear the
4106 * ext flags on replace with remote
4107 */
4108 if (update_flags & DPLANE_NEIGH_WAS_STATIC)
4109 ext = true;
0a27a2fe 4110 } else if (!(update_flags & DPLANE_NEIGH_NO_EXTENSION)) {
ccd187cd
AK
4111 ext = true;
4112 /* local neigh */
4113 if (update_flags & DPLANE_NEIGH_SET_STATIC)
4114 ext_flags |= NTF_E_MH_PEER_SYNC;
ccd187cd 4115 }
ef7b8be4 4116 if (IS_ZEBRA_DEBUG_KERNEL)
340845e2 4117 zlog_debug(
0a27a2fe 4118 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
340845e2
JU
4119 nl_msg_type_to_str(cmd), nl_family_to_str(family),
4120 dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx),
0a27a2fe
PG
4121 ip, link_ip ? "Link " : "MAC ", buf2, flags, state,
4122 ext ? "ext " : "", ext_flags);
d62a17ae 4123
18f60fe9 4124 return netlink_neigh_update_msg_encode(
0a27a2fe
PG
4125 ctx, cmd, link_ptr, llalen, ip, true, family, RTN_UNICAST,
4126 flags, state, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext,
88217099 4127 ext_flags, buf, buflen, proto);
2232a77c 4128}
4129
e18747a9
PG
4130static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx *ctx,
4131 void *data, size_t datalen)
4132{
4133 struct {
4134 struct nlmsghdr n;
4135 struct ndtmsg ndtm;
4136 char buf[];
4137 } *req = data;
4138 struct rtattr *nest;
4139 uint8_t family;
4140 ifindex_t idx;
4141 uint32_t val;
4142
4143 if (datalen < sizeof(*req))
4144 return 0;
4145 memset(req, 0, sizeof(*req));
4146 family = dplane_ctx_neightable_get_family(ctx);
4147 idx = dplane_ctx_get_ifindex(ctx);
4148
4149 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg));
4150 req->n.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE;
4151 req->n.nlmsg_type = RTM_SETNEIGHTBL;
4152 req->ndtm.ndtm_family = family;
4153
4154 nl_attr_put(&req->n, datalen, NDTA_NAME,
4155 family == AF_INET ? "arp_cache" : "ndisc_cache", 10);
4156 nest = nl_attr_nest(&req->n, datalen, NDTA_PARMS);
4157 if (nest == NULL)
4158 return 0;
4159 if (!nl_attr_put(&req->n, datalen, NDTPA_IFINDEX, &idx, sizeof(idx)))
4160 return 0;
4161 val = dplane_ctx_neightable_get_app_probes(ctx);
4162 if (!nl_attr_put(&req->n, datalen, NDTPA_APP_PROBES, &val, sizeof(val)))
4163 return 0;
4164 val = dplane_ctx_neightable_get_mcast_probes(ctx);
4165 if (!nl_attr_put(&req->n, datalen, NDTPA_MCAST_PROBES, &val,
4166 sizeof(val)))
4167 return 0;
4168 val = dplane_ctx_neightable_get_ucast_probes(ctx);
4169 if (!nl_attr_put(&req->n, datalen, NDTPA_UCAST_PROBES, &val,
4170 sizeof(val)))
4171 return 0;
4172 nl_attr_nest_end(&req->n, nest);
4173
4174 return NLMSG_ALIGN(req->n.nlmsg_len);
4175}
4176
67e3369e
JU
4177static ssize_t netlink_neigh_msg_encoder(struct zebra_dplane_ctx *ctx,
4178 void *buf, size_t buflen)
2232a77c 4179{
67e3369e 4180 ssize_t ret;
2232a77c 4181
931fa60c
MS
4182 switch (dplane_ctx_get_op(ctx)) {
4183 case DPLANE_OP_NEIGH_INSTALL:
4184 case DPLANE_OP_NEIGH_UPDATE:
d68e74b4 4185 case DPLANE_OP_NEIGH_DISCOVER:
0a27a2fe 4186 case DPLANE_OP_NEIGH_IP_INSTALL:
67e3369e 4187 ret = netlink_neigh_update_ctx(ctx, RTM_NEWNEIGH, buf, buflen);
931fa60c
MS
4188 break;
4189 case DPLANE_OP_NEIGH_DELETE:
0a27a2fe 4190 case DPLANE_OP_NEIGH_IP_DELETE:
67e3369e 4191 ret = netlink_neigh_update_ctx(ctx, RTM_DELNEIGH, buf, buflen);
931fa60c 4192 break;
0bbd4ff4 4193 case DPLANE_OP_VTEP_ADD:
67e3369e
JU
4194 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_NEWNEIGH, buf,
4195 buflen);
0bbd4ff4
MS
4196 break;
4197 case DPLANE_OP_VTEP_DELETE:
67e3369e
JU
4198 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_DELNEIGH, buf,
4199 buflen);
0bbd4ff4 4200 break;
e18747a9
PG
4201 case DPLANE_OP_NEIGH_TABLE_UPDATE:
4202 ret = netlink_neigh_table_update_ctx(ctx, buf, buflen);
4203 break;
931fa60c 4204 default:
67e3369e 4205 ret = -1;
931fa60c 4206 }
2232a77c 4207
67e3369e
JU
4208 return ret;
4209}
4210
4211/*
4212 * Update MAC, using dataplane context object.
4213 */
4214
67e3369e
JU
4215enum netlink_msg_status netlink_put_mac_update_msg(struct nl_batch *bth,
4216 struct zebra_dplane_ctx *ctx)
4217{
4218 return netlink_batch_add_msg(bth, ctx, netlink_macfdb_update_ctx,
4219 false);
4220}
4221
67e3369e
JU
4222enum netlink_msg_status
4223netlink_put_neigh_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
4224{
4225 return netlink_batch_add_msg(bth, ctx, netlink_neigh_msg_encoder,
4226 false);
6fe2b0e6
CS
4227}
4228
16c628de
MS
4229/*
4230 * MPLS label forwarding table change via netlink interface, using dataplane
4231 * context information.
4232 */
0be6e7d7
JU
4233ssize_t netlink_mpls_multipath_msg_encode(int cmd, struct zebra_dplane_ctx *ctx,
4234 void *buf, size_t buflen)
16c628de
MS
4235{
4236 mpls_lse_t lse;
ee70f629 4237 const struct nhlfe_list_head *head;
81793ac1 4238 const zebra_nhlfe_t *nhlfe;
16c628de
MS
4239 struct nexthop *nexthop = NULL;
4240 unsigned int nexthop_num;
4241 const char *routedesc;
4242 int route_type;
9a0132a5 4243 struct prefix p = {0};
16c628de
MS
4244
4245 struct {
4246 struct nlmsghdr n;
4247 struct rtmsg r;
0be6e7d7
JU
4248 char buf[0];
4249 } *req = buf;
4250
4251 if (buflen < sizeof(*req))
4252 return 0;
16c628de 4253
0be6e7d7 4254 memset(req, 0, sizeof(*req));
16c628de
MS
4255
4256 /*
4257 * Count # nexthops so we can decide whether to use singlepath
4258 * or multipath case.
4259 */
4260 nexthop_num = 0;
ee70f629
MS
4261 head = dplane_ctx_get_nhlfe_list(ctx);
4262 frr_each(nhlfe_list_const, head, nhlfe) {
16c628de
MS
4263 nexthop = nhlfe->nexthop;
4264 if (!nexthop)
4265 continue;
4266 if (cmd == RTM_NEWROUTE) {
4267 /* Count all selected NHLFEs */
4268 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
4269 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
4270 nexthop_num++;
4271 } else { /* DEL */
4272 /* Count all installed NHLFEs */
4273 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
4274 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
4275 nexthop_num++;
4276 }
4277 }
4278
4279 if ((nexthop_num == 0) ||
4280 (!dplane_ctx_get_best_nhlfe(ctx) && (cmd != RTM_DELROUTE)))
4281 return 0;
4282
0be6e7d7
JU
4283 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
4284 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
4285 req->n.nlmsg_type = cmd;
4286 req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
16c628de 4287
0be6e7d7
JU
4288 req->r.rtm_family = AF_MPLS;
4289 req->r.rtm_table = RT_TABLE_MAIN;
4290 req->r.rtm_dst_len = MPLS_LABEL_LEN_BITS;
4291 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
4292 req->r.rtm_type = RTN_UNICAST;
16c628de
MS
4293
4294 if (cmd == RTM_NEWROUTE) {
4295 /* We do a replace to handle update. */
0be6e7d7 4296 req->n.nlmsg_flags |= NLM_F_REPLACE;
16c628de
MS
4297
4298 /* set the protocol value if installing */
4299 route_type = re_type_from_lsp_type(
4300 dplane_ctx_get_best_nhlfe(ctx)->type);
0be6e7d7 4301 req->r.rtm_protocol = zebra2proto(route_type);
16c628de
MS
4302 }
4303
4304 /* Fill destination */
4305 lse = mpls_lse_encode(dplane_ctx_get_in_label(ctx), 0, 0, 1);
0be6e7d7
JU
4306 if (!nl_attr_put(&req->n, buflen, RTA_DST, &lse, sizeof(mpls_lse_t)))
4307 return 0;
16c628de
MS
4308
4309 /* Fill nexthops (paths) based on single-path or multipath. The paths
4310 * chosen depend on the operation.
4311 */
fc608372 4312 if (nexthop_num == 1) {
16c628de
MS
4313 routedesc = "single-path";
4314 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
4315 routedesc);
4316
4317 nexthop_num = 0;
ee70f629 4318 frr_each(nhlfe_list_const, head, nhlfe) {
16c628de
MS
4319 nexthop = nhlfe->nexthop;
4320 if (!nexthop)
4321 continue;
4322
4323 if ((cmd == RTM_NEWROUTE
4324 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
4325 && CHECK_FLAG(nexthop->flags,
4326 NEXTHOP_FLAG_ACTIVE)))
4327 || (cmd == RTM_DELROUTE
4328 && (CHECK_FLAG(nhlfe->flags,
4329 NHLFE_FLAG_INSTALLED)
4330 && CHECK_FLAG(nexthop->flags,
4331 NEXTHOP_FLAG_FIB)))) {
4332 /* Add the gateway */
0be6e7d7
JU
4333 if (!_netlink_mpls_build_singlepath(
4334 &p, routedesc, nhlfe, &req->n,
4335 &req->r, buflen, cmd))
4336 return false;
16c628de
MS
4337
4338 nexthop_num++;
4339 break;
4340 }
4341 }
4342 } else { /* Multipath case */
312a6bee 4343 struct rtattr *nest;
81793ac1 4344 const union g_addr *src1 = NULL;
16c628de 4345
0be6e7d7
JU
4346 nest = nl_attr_nest(&req->n, buflen, RTA_MULTIPATH);
4347 if (!nest)
4348 return 0;
16c628de
MS
4349
4350 routedesc = "multipath";
4351 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
4352 routedesc);
4353
4354 nexthop_num = 0;
ee70f629 4355 frr_each(nhlfe_list_const, head, nhlfe) {
16c628de
MS
4356 nexthop = nhlfe->nexthop;
4357 if (!nexthop)
4358 continue;
4359
16c628de
MS
4360 if ((cmd == RTM_NEWROUTE
4361 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
4362 && CHECK_FLAG(nexthop->flags,
4363 NEXTHOP_FLAG_ACTIVE)))
4364 || (cmd == RTM_DELROUTE
4365 && (CHECK_FLAG(nhlfe->flags,
4366 NHLFE_FLAG_INSTALLED)
4367 && CHECK_FLAG(nexthop->flags,
4368 NEXTHOP_FLAG_FIB)))) {
4369 nexthop_num++;
4370
4371 /* Build the multipath */
0be6e7d7
JU
4372 if (!_netlink_mpls_build_multipath(
4373 &p, routedesc, nhlfe, &req->n,
4374 buflen, &req->r, &src1))
4375 return 0;
16c628de
MS
4376 }
4377 }
4378
4379 /* Add the multipath */
0be6e7d7 4380 nl_attr_nest_end(&req->n, nest);
16c628de
MS
4381 }
4382
0be6e7d7 4383 return NLMSG_ALIGN(req->n.nlmsg_len);
16c628de 4384}
506efd37
AK
4385
4386/****************************************************************************
4387* This code was developed in a branch that didn't have dplane APIs for
4388* MAC updates. Hence the use of the legacy style. It will be moved to
4389* the new dplane style pre-merge to master. XXX
4390*/
4391static int netlink_fdb_nh_update(uint32_t nh_id, struct in_addr vtep_ip)
4392{
4393 struct {
4394 struct nlmsghdr n;
4395 struct nhmsg nhm;
4396 char buf[256];
4397 } req;
4398 int cmd = RTM_NEWNEXTHOP;
4399 struct zebra_vrf *zvrf;
4400 struct zebra_ns *zns;
4401
4402 zvrf = zebra_vrf_get_evpn();
4403 if (!zvrf)
4404 return -1;
4405 zns = zvrf->zns;
4406
4407 memset(&req, 0, sizeof(req));
4408
4409 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
4410 req.n.nlmsg_flags = NLM_F_REQUEST;
4411 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
4412 req.n.nlmsg_type = cmd;
4413 req.nhm.nh_family = AF_INET;
4414
4415 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id))
4416 return -1;
4417 if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0))
4418 return -1;
4419 if (!nl_attr_put(&req.n, sizeof(req), NHA_GATEWAY,
4420 &vtep_ip, IPV4_MAX_BYTELEN))
4421 return -1;
4422
4423 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
9bcef951
MS
4424 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4425 nl_msg_type_to_str(cmd), nh_id, &vtep_ip);
506efd37
AK
4426 }
4427
4428 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
4429 0);
4430}
4431
4432static int netlink_fdb_nh_del(uint32_t nh_id)
4433{
4434 struct {
4435 struct nlmsghdr n;
4436 struct nhmsg nhm;
4437 char buf[256];
4438 } req;
4439 int cmd = RTM_DELNEXTHOP;
4440 struct zebra_vrf *zvrf;
4441 struct zebra_ns *zns;
4442
4443 zvrf = zebra_vrf_get_evpn();
4444 if (!zvrf)
4445 return -1;
4446 zns = zvrf->zns;
4447
4448 memset(&req, 0, sizeof(req));
4449
4450 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
4451 req.n.nlmsg_flags = NLM_F_REQUEST;
4452 req.n.nlmsg_type = cmd;
4453 req.nhm.nh_family = AF_UNSPEC;
4454
4455 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id))
4456 return -1;
4457
4458 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
4459 zlog_debug("Tx %s fdb-nh 0x%x",
4460 nl_msg_type_to_str(cmd), nh_id);
4461 }
4462
4463 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
4464 0);
4465}
4466
4467static int netlink_fdb_nhg_update(uint32_t nhg_id, uint32_t nh_cnt,
4468 struct nh_grp *nh_ids)
4469{
4470 struct {
4471 struct nlmsghdr n;
4472 struct nhmsg nhm;
4473 char buf[256];
4474 } req;
4475 int cmd = RTM_NEWNEXTHOP;
4476 struct zebra_vrf *zvrf;
4477 struct zebra_ns *zns;
4478 struct nexthop_grp grp[nh_cnt];
4479 uint32_t i;
4480
4481 zvrf = zebra_vrf_get_evpn();
4482 if (!zvrf)
4483 return -1;
4484 zns = zvrf->zns;
4485
4486 memset(&req, 0, sizeof(req));
4487
4488 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
4489 req.n.nlmsg_flags = NLM_F_REQUEST;
4490 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
4491 req.n.nlmsg_type = cmd;
4492 req.nhm.nh_family = AF_UNSPEC;
4493
4494 if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nhg_id))
4495 return -1;
4496 if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0))
4497 return -1;
4498 memset(&grp, 0, sizeof(grp));
4499 for (i = 0; i < nh_cnt; ++i) {
4500 grp[i].id = nh_ids[i].id;
4501 grp[i].weight = nh_ids[i].weight;
4502 }
4503 if (!nl_attr_put(&req.n, sizeof(req), NHA_GROUP,
4504 grp, nh_cnt * sizeof(struct nexthop_grp)))
4505 return -1;
4506
4507
4508 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) {
4509 char vtep_str[ES_VTEP_LIST_STR_SZ];
9e0c2fd1 4510 char nh_buf[16];
506efd37
AK
4511
4512 vtep_str[0] = '\0';
4513 for (i = 0; i < nh_cnt; ++i) {
9e0c2fd1 4514 snprintf(nh_buf, sizeof(nh_buf), "%u ",
506efd37 4515 grp[i].id);
9e0c2fd1 4516 strlcat(vtep_str, nh_buf, sizeof(vtep_str));
506efd37
AK
4517 }
4518
4519 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4520 nl_msg_type_to_str(cmd), nhg_id, vtep_str);
4521 }
4522
4523 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
4524 0);
4525}
4526
4527static int netlink_fdb_nhg_del(uint32_t nhg_id)
4528{
4529 return netlink_fdb_nh_del(nhg_id);
4530}
4531
4532int kernel_upd_mac_nh(uint32_t nh_id, struct in_addr vtep_ip)
4533{
4534 return netlink_fdb_nh_update(nh_id, vtep_ip);
4535}
4536
4537int kernel_del_mac_nh(uint32_t nh_id)
4538{
4539 return netlink_fdb_nh_del(nh_id);
4540}
4541
4542int kernel_upd_mac_nhg(uint32_t nhg_id, uint32_t nh_cnt,
4543 struct nh_grp *nh_ids)
4544{
4545 return netlink_fdb_nhg_update(nhg_id, nh_cnt, nh_ids);
4546}
4547
4548int kernel_del_mac_nhg(uint32_t nhg_id)
4549{
4550 return netlink_fdb_nhg_del(nhg_id);
4551}
4552
ddfeb486 4553#endif /* HAVE_NETLINK */