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