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