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