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