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