]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_netlink.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[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
23b1f334
DD
145static inline int is_selfroute(int proto)
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)) {
d62a17ae 154 return 1;
155 }
156
157 return 0;
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",
216 __PRETTY_FUNCTION__, 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",
283 __PRETTY_FUNCTION__, 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",
442 __PRETTY_FUNCTION__, index);
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;
d62a17ae 516
517 char anyaddr[16] = {0};
518
915902cb 519 int proto = ZEBRA_ROUTE_KERNEL;
d62a17ae 520 int index = 0;
521 int table;
522 int metric = 0;
d7c0a89a 523 uint32_t mtu = 0;
25715c7e 524 uint8_t distance = 0;
4e40b6d6 525 route_tag_t tag = 0;
fcc89a9c 526 uint32_t nhe_id = 0;
d62a17ae 527
528 void *dest = NULL;
529 void *gate = NULL;
530 void *prefsrc = NULL; /* IPv4 preferred source host address */
531 void *src = NULL; /* IPv6 srcdest source prefix */
e655a03c 532 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 533
534 rtm = NLMSG_DATA(h);
535
536 if (startup && h->nlmsg_type != RTM_NEWROUTE)
537 return 0;
e655a03c
DL
538 switch (rtm->rtm_type) {
539 case RTN_UNICAST:
540 break;
541 case RTN_BLACKHOLE:
542 bh_type = BLACKHOLE_NULL;
543 break;
544 case RTN_UNREACHABLE:
545 bh_type = BLACKHOLE_REJECT;
546 break;
547 case RTN_PROHIBIT:
548 bh_type = BLACKHOLE_ADMINPROHIB;
549 break;
550 default:
8c8f250b
DS
551 if (IS_ZEBRA_DEBUG_KERNEL)
552 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
553 nl_rttype_to_str(rtm->rtm_type),
554 rtm->rtm_type);
d62a17ae 555 return 0;
e655a03c 556 }
d62a17ae 557
558 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
9bdf8618
DS
559 if (len < 0) {
560 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
561 __PRETTY_FUNCTION__, h->nlmsg_len,
562 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
d62a17ae 563 return -1;
9bdf8618 564 }
d62a17ae 565
566 memset(tb, 0, sizeof tb);
567 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
568
569 if (rtm->rtm_flags & RTM_F_CLONED)
570 return 0;
571 if (rtm->rtm_protocol == RTPROT_REDIRECT)
572 return 0;
573 if (rtm->rtm_protocol == RTPROT_KERNEL)
574 return 0;
575
576 if (!startup && is_selfroute(rtm->rtm_protocol)
6ab5222f
DS
577 && h->nlmsg_type == RTM_NEWROUTE) {
578 if (IS_ZEBRA_DEBUG_KERNEL)
579 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
580 rtm->rtm_protocol);
d62a17ae 581 return 0;
6ab5222f 582 }
d62a17ae 583
584 /* We don't care about change notifications for the MPLS table. */
585 /* TODO: Revisit this. */
586 if (rtm->rtm_family == AF_MPLS)
587 return 0;
588
589 /* Table corresponding to route. */
590 if (tb[RTA_TABLE])
591 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
592 else
593 table = rtm->rtm_table;
594
595 /* Map to VRF */
78dd30b2 596 vrf_id = vrf_lookup_by_table(table, ns_id);
d62a17ae 597 if (vrf_id == VRF_DEFAULT) {
598 if (!is_zebra_valid_kernel_table(table)
599 && !is_zebra_main_routing_table(table))
600 return 0;
601 }
602
603 /* Route which inserted by Zebra. */
915902cb 604 if (is_selfroute(rtm->rtm_protocol)) {
d62a17ae 605 flags |= ZEBRA_FLAG_SELFROUTE;
38e40db1 606 proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family, false);
915902cb 607 }
d62a17ae 608 if (tb[RTA_OIF])
609 index = *(int *)RTA_DATA(tb[RTA_OIF]);
610
611 if (tb[RTA_DST])
612 dest = RTA_DATA(tb[RTA_DST]);
613 else
614 dest = anyaddr;
615
616 if (tb[RTA_SRC])
617 src = RTA_DATA(tb[RTA_SRC]);
618 else
619 src = anyaddr;
620
621 if (tb[RTA_PREFSRC])
622 prefsrc = RTA_DATA(tb[RTA_PREFSRC]);
623
624 if (tb[RTA_GATEWAY])
625 gate = RTA_DATA(tb[RTA_GATEWAY]);
626
fcc89a9c
SW
627 if (tb[RTA_NH_ID])
628 nhe_id = *(uint32_t *)RTA_DATA(tb[RTA_NH_ID]);
629
f19435a8
DS
630 if (tb[RTA_PRIORITY])
631 metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]);
d62a17ae 632
4e40b6d6
KK
633#if defined(SUPPORT_REALMS)
634 if (tb[RTA_FLOW])
635 tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]);
636#endif
637
f19435a8
DS
638 if (tb[RTA_METRICS]) {
639 struct rtattr *mxrta[RTAX_MAX + 1];
d62a17ae 640
f19435a8 641 memset(mxrta, 0, sizeof mxrta);
996c9314 642 netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
f19435a8 643 RTA_PAYLOAD(tb[RTA_METRICS]));
d62a17ae 644
f19435a8 645 if (mxrta[RTAX_MTU])
d7c0a89a 646 mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]);
d62a17ae 647 }
648
649 if (rtm->rtm_family == AF_INET) {
650 p.family = AF_INET;
930571d2 651 if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) {
e17d9b2d 652 zlog_err(
75829703 653 "Invalid destination prefix length: %u received from kernel route change",
930571d2 654 rtm->rtm_dst_len);
e17d9b2d 655 return -1;
930571d2 656 }
d62a17ae 657 memcpy(&p.u.prefix4, dest, 4);
658 p.prefixlen = rtm->rtm_dst_len;
659
1f610a1f
CF
660 if (rtm->rtm_src_len != 0) {
661 char buf[PREFIX_STRLEN];
9df414fe 662 flog_warn(
e914ccbe 663 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST,
9df414fe
QY
664 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
665 prefix2str(&p, buf, sizeof(buf)), vrf_id);
1f610a1f
CF
666 return 0;
667 }
930571d2 668
1f610a1f
CF
669 /* Force debug below to not display anything for source */
670 src_p.prefixlen = 0;
d62a17ae 671 } else if (rtm->rtm_family == AF_INET6) {
672 p.family = AF_INET6;
930571d2 673 if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) {
e17d9b2d 674 zlog_err(
75829703 675 "Invalid destination prefix length: %u received from kernel route change",
930571d2 676 rtm->rtm_dst_len);
e17d9b2d 677 return -1;
930571d2 678 }
d62a17ae 679 memcpy(&p.u.prefix6, dest, 16);
680 p.prefixlen = rtm->rtm_dst_len;
681
682 src_p.family = AF_INET6;
930571d2 683 if (rtm->rtm_src_len > IPV6_MAX_BITLEN) {
e17d9b2d 684 zlog_err(
75829703 685 "Invalid source prefix length: %u received from kernel route change",
930571d2 686 rtm->rtm_src_len);
e17d9b2d 687 return -1;
930571d2 688 }
d62a17ae 689 memcpy(&src_p.prefix, src, 16);
690 src_p.prefixlen = rtm->rtm_src_len;
691 }
692
25715c7e
DS
693 /*
694 * For ZEBRA_ROUTE_KERNEL types:
695 *
696 * The metric/priority of the route received from the kernel
697 * is a 32 bit number. We are going to interpret the high
698 * order byte as the Admin Distance and the low order 3 bytes
699 * as the metric.
700 *
701 * This will allow us to do two things:
702 * 1) Allow the creation of kernel routes that can be
703 * overridden by zebra.
704 * 2) Allow the old behavior for 'most' kernel route types
705 * if a user enters 'ip route ...' v4 routes get a metric
706 * of 0 and v6 routes get a metric of 1024. Both of these
707 * values will end up with a admin distance of 0, which
708 * will cause them to win for the purposes of zebra.
709 */
710 if (proto == ZEBRA_ROUTE_KERNEL) {
711 distance = (metric >> 24) & 0xFF;
996c9314 712 metric = (metric & 0x00FFFFFF);
25715c7e
DS
713 }
714
d62a17ae 715 if (IS_ZEBRA_DEBUG_KERNEL) {
716 char buf[PREFIX_STRLEN];
717 char buf2[PREFIX_STRLEN];
45df4e96 718 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
996c9314
LB
719 nl_msg_type_to_str(h->nlmsg_type),
720 prefix2str(&p, buf, sizeof(buf)),
721 src_p.prefixlen ? " from " : "",
722 src_p.prefixlen
723 ? prefix2str(&src_p, buf2, sizeof(buf2))
724 : "",
45df4e96 725 vrf_id, table, metric, distance);
d62a17ae 726 }
727
728 afi_t afi = AFI_IP;
729 if (rtm->rtm_family == AF_INET6)
730 afi = AFI_IP6;
731
732 if (h->nlmsg_type == RTM_NEWROUTE) {
8795f904 733
fd36be7e 734 if (!tb[RTA_MULTIPATH]) {
77a44d94 735 struct nexthop nh = {0};
8795f904 736
77a44d94
SW
737 if (!nhe_id) {
738 nh = parse_nexthop_unicast(
739 ns_id, rtm, tb, bh_type, index, prefsrc,
20822f9d 740 gate, afi, vrf_id);
87da6a60 741 }
4a7371e9 742 rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p,
8032b717
SW
743 &src_p, &nh, nhe_id, table, metric, mtu,
744 distance, tag);
fd36be7e 745 } else {
d62a17ae 746 /* This is a multipath route */
d62a17ae 747 struct route_entry *re;
0eb97b86 748 struct nexthop_group *ng = NULL;
d62a17ae 749 struct rtnexthop *rtnh =
750 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
d62a17ae 751
752 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
915902cb 753 re->type = proto;
25715c7e 754 re->distance = distance;
d62a17ae 755 re->flags = flags;
756 re->metric = metric;
757 re->mtu = mtu;
758 re->vrf_id = vrf_id;
759 re->table = table;
98572489 760 re->uptime = monotime(NULL);
4e40b6d6 761 re->tag = tag;
bbb322f2 762 re->nhe_id = nhe_id;
3c04071d 763
20822f9d 764 if (!nhe_id) {
0eb97b86
MS
765 uint8_t nhop_num;
766
767 /* Use temporary list of nexthops; parse
768 * message payload's nexthops.
769 */
770 ng = nexthop_group_new();
771 nhop_num =
20822f9d 772 parse_multipath_nexthops_unicast(
0eb97b86 773 ns_id, ng, rtm, rtnh, tb,
20822f9d
SW
774 prefsrc, vrf_id);
775
776 zserv_nexthop_num_warn(
777 __func__, (const struct prefix *)&p,
778 nhop_num);
0eb97b86
MS
779
780 if (nhop_num == 0) {
781 nexthop_group_delete(&ng);
782 ng = NULL;
783 }
d62a17ae 784 }
785
0eb97b86 786 if (nhe_id || ng)
1f610a1f 787 rib_add_multipath(afi, SAFI_UNICAST, &p,
0eb97b86 788 &src_p, re, ng);
20822f9d
SW
789 else
790 XFREE(MTYPE_RE, re);
d62a17ae 791 }
792 } else {
bc541126
SW
793 if (nhe_id) {
794 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
795 &p, &src_p, NULL, nhe_id, table, metric,
796 distance, true);
797 } else {
798 if (!tb[RTA_MULTIPATH]) {
799 struct nexthop nh;
760f39dc
HS
800
801 nh = parse_nexthop_unicast(
802 ns_id, rtm, tb, bh_type, index, prefsrc,
803 gate, afi, vrf_id);
bc541126
SW
804 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0,
805 flags, &p, &src_p, &nh, 0, table,
806 metric, distance, true);
8ba5bd58 807 } else {
bc541126
SW
808 /* XXX: need to compare the entire list of
809 * nexthops here for NLM_F_APPEND stupidity */
810 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0,
811 flags, &p, &src_p, NULL, 0, table,
812 metric, distance, true);
8ba5bd58 813 }
d62a17ae 814 }
815 }
816
817 return 0;
718e3744 818}
819
e3be0432
DS
820static struct mcast_route_data *mroute = NULL;
821
2414abd3 822static int netlink_route_change_read_multicast(struct nlmsghdr *h,
d62a17ae 823 ns_id_t ns_id, int startup)
565fdc75 824{
d62a17ae 825 int len;
826 struct rtmsg *rtm;
827 struct rtattr *tb[RTA_MAX + 1];
828 struct mcast_route_data *m;
829 struct mcast_route_data mr;
830 int iif = 0;
831 int count;
832 int oif[256];
833 int oif_count = 0;
834 char sbuf[40];
835 char gbuf[40];
836 char oif_list[256] = "\0";
78dd30b2 837 vrf_id_t vrf;
43b5cc5e 838 int table;
d62a17ae 839
840 if (mroute)
841 m = mroute;
842 else {
843 memset(&mr, 0, sizeof(mr));
844 m = &mr;
845 }
846
847 rtm = NLMSG_DATA(h);
848
849 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
850
851 memset(tb, 0, sizeof tb);
852 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
90d82769 853
43b5cc5e
DS
854 if (tb[RTA_TABLE])
855 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
856 else
857 table = rtm->rtm_table;
858
78dd30b2 859 vrf = vrf_lookup_by_table(table, ns_id);
43b5cc5e 860
d62a17ae 861 if (tb[RTA_IIF])
862 iif = *(int *)RTA_DATA(tb[RTA_IIF]);
863
864 if (tb[RTA_SRC])
bd8b9272 865 m->sg.src = *(struct in_addr *)RTA_DATA(tb[RTA_SRC]);
d62a17ae 866
867 if (tb[RTA_DST])
bd8b9272 868 m->sg.grp = *(struct in_addr *)RTA_DATA(tb[RTA_DST]);
d62a17ae 869
62819462 870 if (tb[RTA_EXPIRES])
d62a17ae 871 m->lastused = *(unsigned long long *)RTA_DATA(tb[RTA_EXPIRES]);
872
873 if (tb[RTA_MULTIPATH]) {
874 struct rtnexthop *rtnh =
875 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
876
877 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
878 for (;;) {
879 if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len)
880 break;
881
882 oif[oif_count] = rtnh->rtnh_ifindex;
883 oif_count++;
884
3c04071d
SW
885 if (rtnh->rtnh_len == 0)
886 break;
887
d62a17ae 888 len -= NLMSG_ALIGN(rtnh->rtnh_len);
889 rtnh = RTNH_NEXT(rtnh);
890 }
891 }
892
893 if (IS_ZEBRA_DEBUG_KERNEL) {
822c9af2
SW
894 struct interface *ifp = NULL;
895 struct zebra_vrf *zvrf = NULL;
896
0af35d90
RW
897 strlcpy(sbuf, inet_ntoa(m->sg.src), sizeof(sbuf));
898 strlcpy(gbuf, inet_ntoa(m->sg.grp), sizeof(gbuf));
d62a17ae 899 for (count = 0; count < oif_count; count++) {
900 ifp = if_lookup_by_index(oif[count], vrf);
901 char temp[256];
902
5b4256ca
DS
903 sprintf(temp, "%s(%d) ", ifp ? ifp->name : "Unknown",
904 oif[count]);
eab4a5c2 905 strlcat(oif_list, temp, sizeof(oif_list));
d62a17ae 906 }
822c9af2 907 zvrf = zebra_vrf_lookup_by_id(vrf);
d62a17ae 908 ifp = if_lookup_by_index(iif, vrf);
822c9af2
SW
909 zlog_debug(
910 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
911 (zvrf ? zvrf->vrf->name : "Unknown"), vrf,
912 nl_msg_type_to_str(h->nlmsg_type), sbuf, gbuf,
913 ifp ? ifp->name : "Unknown", iif, oif_list,
914 m->lastused);
90d82769 915 }
d62a17ae 916 return 0;
565fdc75
DS
917}
918
2414abd3 919int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
565fdc75 920{
d62a17ae 921 int len;
d62a17ae 922 struct rtmsg *rtm;
923
924 rtm = NLMSG_DATA(h);
925
926 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) {
927 /* If this is not route add/delete message print warning. */
9165c5f5 928 zlog_debug("Kernel message: %s NS %u",
87b5d1b0 929 nl_msg_type_to_str(h->nlmsg_type), ns_id);
d62a17ae 930 return 0;
931 }
932
c25e2f1a
DS
933 if (!(rtm->rtm_family == AF_INET ||
934 rtm->rtm_family == AF_INET6 ||
935 rtm->rtm_family == RTNL_FAMILY_IPMR )) {
9df414fe 936 flog_warn(
e914ccbe 937 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
938 "Invalid address family: %u received from kernel route change: %s",
939 rtm->rtm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
940 return 0;
941 }
942
d62a17ae 943 /* Connected route. */
944 if (IS_ZEBRA_DEBUG_KERNEL)
78dd30b2 945 zlog_debug("%s %s %s proto %s NS %u",
d62a17ae 946 nl_msg_type_to_str(h->nlmsg_type),
947 nl_family_to_str(rtm->rtm_family),
948 nl_rttype_to_str(rtm->rtm_type),
78dd30b2 949 nl_rtproto_to_str(rtm->rtm_protocol), ns_id);
d62a17ae 950
d62a17ae 951
952 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
9bdf8618
DS
953 if (len < 0) {
954 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
955 __PRETTY_FUNCTION__,
956 h->nlmsg_len,
957 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
d62a17ae 958 return -1;
9bdf8618 959 }
d62a17ae 960
e655a03c 961 if (rtm->rtm_type == RTN_MULTICAST)
2414abd3 962 netlink_route_change_read_multicast(h, ns_id, startup);
e655a03c 963 else
2414abd3 964 netlink_route_change_read_unicast(h, ns_id, startup);
d62a17ae 965 return 0;
565fdc75
DS
966}
967
289602d7 968/* Request for specific route information from the kernel */
d62a17ae 969static int netlink_request_route(struct zebra_ns *zns, int family, int type)
289602d7 970{
d62a17ae 971 struct {
972 struct nlmsghdr n;
973 struct rtmsg rtm;
974 } req;
975
976 /* Form the request, specifying filter (rtattr) if needed. */
977 memset(&req, 0, sizeof(req));
978 req.n.nlmsg_type = type;
718f9b0f 979 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 980 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
981 req.rtm.rtm_family = family;
982
983 return netlink_request(&zns->netlink_cmd, &req.n);
289602d7 984}
985
718e3744 986/* Routing table read function using netlink interface. Only called
987 bootstrap time. */
d62a17ae 988int netlink_route_read(struct zebra_ns *zns)
718e3744 989{
d62a17ae 990 int ret;
85a75f1e
MS
991 struct zebra_dplane_info dp_info;
992
993 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 994
995 /* Get IPv4 routing table. */
996 ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE);
997 if (ret < 0)
998 return ret;
999 ret = netlink_parse_info(netlink_route_change_read_unicast,
85a75f1e 1000 &zns->netlink_cmd, &dp_info, 0, 1);
d62a17ae 1001 if (ret < 0)
1002 return ret;
1003
1004 /* Get IPv6 routing table. */
1005 ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE);
1006 if (ret < 0)
1007 return ret;
1008 ret = netlink_parse_info(netlink_route_change_read_unicast,
85a75f1e 1009 &zns->netlink_cmd, &dp_info, 0, 1);
d62a17ae 1010 if (ret < 0)
1011 return ret;
1012
1013 return 0;
718e3744 1014}
1015
d7c0a89a
QY
1016static void _netlink_route_nl_add_gateway_info(uint8_t route_family,
1017 uint8_t gw_family,
d62a17ae 1018 struct nlmsghdr *nlmsg,
1019 size_t req_size, int bytelen,
81793ac1 1020 const struct nexthop *nexthop)
40c7bdb0 1021{
d62a17ae 1022 if (route_family == AF_MPLS) {
1023 struct gw_family_t gw_fam;
1024
1025 gw_fam.family = gw_family;
1026 if (gw_family == AF_INET)
1027 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
1028 else
1029 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
1030 addattr_l(nlmsg, req_size, RTA_VIA, &gw_fam.family,
1031 bytelen + 2);
1032 } else {
1033 if (gw_family == AF_INET)
1034 addattr_l(nlmsg, req_size, RTA_GATEWAY,
1035 &nexthop->gate.ipv4, bytelen);
1036 else
1037 addattr_l(nlmsg, req_size, RTA_GATEWAY,
1038 &nexthop->gate.ipv6, bytelen);
1039 }
40c7bdb0 1040}
1041
d7c0a89a
QY
1042static void _netlink_route_rta_add_gateway_info(uint8_t route_family,
1043 uint8_t gw_family,
d62a17ae 1044 struct rtattr *rta,
1045 struct rtnexthop *rtnh,
1046 size_t req_size, int bytelen,
81793ac1 1047 const struct nexthop *nexthop)
40c7bdb0 1048{
d62a17ae 1049 if (route_family == AF_MPLS) {
1050 struct gw_family_t gw_fam;
1051
1052 gw_fam.family = gw_family;
1053 if (gw_family == AF_INET)
1054 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
1055 else
1056 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
1057 rta_addattr_l(rta, req_size, RTA_VIA, &gw_fam.family,
1058 bytelen + 2);
1059 rtnh->rtnh_len += RTA_LENGTH(bytelen + 2);
1060 } else {
1061 if (gw_family == AF_INET)
1062 rta_addattr_l(rta, req_size, RTA_GATEWAY,
1063 &nexthop->gate.ipv4, bytelen);
1064 else
1065 rta_addattr_l(rta, req_size, RTA_GATEWAY,
1066 &nexthop->gate.ipv6, bytelen);
1067 rtnh->rtnh_len += sizeof(struct rtattr) + bytelen;
1068 }
40c7bdb0 1069}
1070
b7537db6
SW
1071static int build_label_stack(struct mpls_label_stack *nh_label,
1072 mpls_lse_t *out_lse, char *label_buf,
1073 size_t label_buf_size)
1074{
1075 char label_buf1[20];
1076 int num_labels = 0;
1077
1078 for (int i = 0; nh_label && i < nh_label->num_labels; i++) {
1079 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1080 continue;
1081
1082 if (IS_ZEBRA_DEBUG_KERNEL) {
1083 if (!num_labels)
1084 sprintf(label_buf, "label %u",
1085 nh_label->label[i]);
1086 else {
1087 sprintf(label_buf1, "/%u", nh_label->label[i]);
1088 strlcat(label_buf, label_buf1, label_buf_size);
1089 }
1090 }
1091
1092 out_lse[num_labels] =
1093 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1094 num_labels++;
1095 }
1096
1097 return num_labels;
1098}
1099
fa713d9e
CF
1100/* This function takes a nexthop as argument and adds
1101 * the appropriate netlink attributes to an existing
1102 * netlink message.
1103 *
1104 * @param routedesc: Human readable description of route type
1105 * (direct/recursive, single-/multipath)
1106 * @param bytelen: Length of addresses in bytes.
1107 * @param nexthop: Nexthop information
1108 * @param nlmsg: nlmsghdr structure to fill in.
1109 * @param req_size: The size allocated for the message.
1110 */
d62a17ae 1111static void _netlink_route_build_singlepath(const char *routedesc, int bytelen,
81793ac1 1112 const struct nexthop *nexthop,
d62a17ae 1113 struct nlmsghdr *nlmsg,
1114 struct rtmsg *rtmsg,
1115 size_t req_size, int cmd)
fa713d9e 1116{
b7537db6 1117
d62a17ae 1118 mpls_lse_t out_lse[MPLS_MAX_LABELS];
9a62e84b 1119 char label_buf[256];
b7537db6
SW
1120 int num_labels = 0;
1121
1122 assert(nexthop);
d62a17ae 1123
1124 /*
1125 * label_buf is *only* currently used within debugging.
1126 * As such when we assign it we are guarding it inside
1127 * a debug test. If you want to change this make sure
1128 * you fix this assumption
1129 */
1130 label_buf[0] = '\0';
d62a17ae 1131
b7537db6
SW
1132 num_labels = build_label_stack(nexthop->nh_label, out_lse, label_buf,
1133 sizeof(label_buf));
fa712963
RW
1134
1135 if (num_labels) {
1136 /* Set the BoS bit */
1137 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1138
1139 if (rtmsg->rtm_family == AF_MPLS)
1140 addattr_l(nlmsg, req_size, RTA_NEWDST, &out_lse,
1141 num_labels * sizeof(mpls_lse_t));
1142 else {
1143 struct rtattr *nest;
d7c0a89a 1144 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
fa712963
RW
1145
1146 addattr_l(nlmsg, req_size, RTA_ENCAP_TYPE, &encap,
d7c0a89a 1147 sizeof(uint16_t));
fa712963
RW
1148 nest = addattr_nest(nlmsg, req_size, RTA_ENCAP);
1149 addattr_l(nlmsg, req_size, MPLS_IPTUNNEL_DST, &out_lse,
1150 num_labels * sizeof(mpls_lse_t));
1151 addattr_nest_end(nlmsg, nest);
66d42727 1152 }
0aabccc0 1153 }
fa713d9e 1154
d62a17ae 1155 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1156 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1157
1158 if (rtmsg->rtm_family == AF_INET
1159 && (nexthop->type == NEXTHOP_TYPE_IPV6
1160 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) {
1161 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1162 addattr_l(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4);
1163 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1164
975a328e
DA
1165 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY
1166 && (cmd == RTM_NEWROUTE))
d62a17ae 1167 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1168 &nexthop->rmap_src.ipv4, bytelen);
975a328e
DA
1169 else if (nexthop->src.ipv4.s_addr != INADDR_ANY
1170 && (cmd == RTM_NEWROUTE))
d62a17ae 1171 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1172 &nexthop->src.ipv4, bytelen);
1173
1174 if (IS_ZEBRA_DEBUG_KERNEL)
1175 zlog_debug(
1176 " 5549: _netlink_route_build_singlepath() (%s): "
7556c3fd 1177 "nexthop via %s %s if %u(%u)",
d62a17ae 1178 routedesc, ipv4_ll_buf, label_buf,
7556c3fd 1179 nexthop->ifindex, nexthop->vrf_id);
d62a17ae 1180 return;
0aabccc0
DD
1181 }
1182
d62a17ae 1183 if (nexthop->type == NEXTHOP_TYPE_IPV4
1184 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1185 /* Send deletes to the kernel without specifying the next-hop */
1186 if (cmd != RTM_DELROUTE)
1187 _netlink_route_nl_add_gateway_info(
1188 rtmsg->rtm_family, AF_INET, nlmsg, req_size,
1189 bytelen, nexthop);
1190
1191 if (cmd == RTM_NEWROUTE) {
975a328e 1192 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1193 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1194 &nexthop->rmap_src.ipv4, bytelen);
975a328e 1195 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1196 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1197 &nexthop->src.ipv4, bytelen);
1198 }
1199
1200 if (IS_ZEBRA_DEBUG_KERNEL)
1201 zlog_debug(
1202 "netlink_route_multipath() (%s): "
7556c3fd 1203 "nexthop via %s %s if %u(%u)",
d62a17ae 1204 routedesc, inet_ntoa(nexthop->gate.ipv4),
7556c3fd 1205 label_buf, nexthop->ifindex, nexthop->vrf_id);
0aabccc0 1206 }
fa713d9e 1207
d62a17ae 1208 if (nexthop->type == NEXTHOP_TYPE_IPV6
1209 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1210 _netlink_route_nl_add_gateway_info(rtmsg->rtm_family, AF_INET6,
1211 nlmsg, req_size, bytelen,
1212 nexthop);
1213
1214 if (cmd == RTM_NEWROUTE) {
1215 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1216 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1217 &nexthop->rmap_src.ipv6, bytelen);
1218 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1219 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1220 &nexthop->src.ipv6, bytelen);
1221 }
fa713d9e 1222
d62a17ae 1223 if (IS_ZEBRA_DEBUG_KERNEL)
1224 zlog_debug(
1225 "netlink_route_multipath() (%s): "
7556c3fd 1226 "nexthop via %s %s if %u(%u)",
d62a17ae 1227 routedesc, inet6_ntoa(nexthop->gate.ipv6),
7556c3fd 1228 label_buf, nexthop->ifindex, nexthop->vrf_id);
d62a17ae 1229 }
5e210522
DS
1230
1231 /*
1232 * We have the ifindex so we should always send it
1233 * This is especially useful if we are doing route
1234 * leaking.
1235 */
1236 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
d62a17ae 1237 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1238
275565fb 1239 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
d62a17ae 1240 if (cmd == RTM_NEWROUTE) {
975a328e 1241 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1242 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1243 &nexthop->rmap_src.ipv4, bytelen);
975a328e 1244 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1245 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1246 &nexthop->src.ipv4, bytelen);
1247 }
fa713d9e 1248
d62a17ae 1249 if (IS_ZEBRA_DEBUG_KERNEL)
1250 zlog_debug(
1251 "netlink_route_multipath() (%s): "
7556c3fd
DS
1252 "nexthop via if %u(%u)",
1253 routedesc, nexthop->ifindex, nexthop->vrf_id);
0aabccc0 1254 }
fa713d9e
CF
1255}
1256
1257/* This function takes a nexthop as argument and
1258 * appends to the given rtattr/rtnexthop pair the
1259 * representation of the nexthop. If the nexthop
1260 * defines a preferred source, the src parameter
1261 * will be modified to point to that src, otherwise
1262 * it will be kept unmodified.
1263 *
1264 * @param routedesc: Human readable description of route type
1265 * (direct/recursive, single-/multipath)
1266 * @param bytelen: Length of addresses in bytes.
1267 * @param nexthop: Nexthop information
1268 * @param rta: rtnetlink attribute structure
1269 * @param rtnh: pointer to an rtnetlink nexthop structure
1270 * @param src: pointer pointing to a location where
1271 * the prefsrc should be stored.
1272 */
d62a17ae 1273static void _netlink_route_build_multipath(const char *routedesc, int bytelen,
81793ac1 1274 const struct nexthop *nexthop,
d62a17ae 1275 struct rtattr *rta,
1276 struct rtnexthop *rtnh,
1277 struct rtmsg *rtmsg,
81793ac1 1278 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;
1340
975a328e 1341 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1342 *src = &nexthop->rmap_src;
975a328e 1343 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1344 *src = &nexthop->src;
1345
1346 if (IS_ZEBRA_DEBUG_KERNEL)
1347 zlog_debug(
1348 " 5549: netlink_route_build_multipath() (%s): "
1349 "nexthop via %s %s if %u",
1350 routedesc, ipv4_ll_buf, label_buf,
1351 nexthop->ifindex);
1352 return;
1353 }
1354
1355 if (nexthop->type == NEXTHOP_TYPE_IPV4
1356 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1357 _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET,
1358 rta, rtnh, NL_PKT_BUF_SIZE,
1359 bytelen, nexthop);
975a328e 1360 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1361 *src = &nexthop->rmap_src;
975a328e 1362 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1363 *src = &nexthop->src;
1364
1365 if (IS_ZEBRA_DEBUG_KERNEL)
1366 zlog_debug(
1367 "netlink_route_multipath() (%s): "
1368 "nexthop via %s %s if %u",
1369 routedesc, inet_ntoa(nexthop->gate.ipv4),
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(
1385 "netlink_route_multipath() (%s): "
1386 "nexthop via %s %s if %u",
1387 routedesc, inet6_ntoa(nexthop->gate.ipv6),
1388 label_buf, nexthop->ifindex);
1389 }
5e210522
DS
1390
1391 /*
1392 * We have figured out the ifindex so we should always send it
1393 * This is especially useful if we are doing route
1394 * leaking.
1395 */
1396 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1397 rtnh->rtnh_ifindex = nexthop->ifindex;
1398
d62a17ae 1399 /* ifindex */
275565fb 1400 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
975a328e 1401 if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1402 *src = &nexthop->rmap_src;
975a328e 1403 else if (nexthop->src.ipv4.s_addr != INADDR_ANY)
d62a17ae 1404 *src = &nexthop->src;
1405
1406 if (IS_ZEBRA_DEBUG_KERNEL)
1407 zlog_debug(
1408 "netlink_route_multipath() (%s): "
1409 "nexthop via if %u",
1410 routedesc, nexthop->ifindex);
d62a17ae 1411 }
df7fb580
DS
1412
1413 if (nexthop->weight)
1414 rtnh->rtnh_hops = nexthop->weight - 1;
fa713d9e
CF
1415}
1416
d62a17ae 1417static inline void _netlink_mpls_build_singlepath(const char *routedesc,
81793ac1 1418 const zebra_nhlfe_t *nhlfe,
d62a17ae 1419 struct nlmsghdr *nlmsg,
1420 struct rtmsg *rtmsg,
1421 size_t req_size, int cmd)
40c7bdb0 1422{
d62a17ae 1423 int bytelen;
d7c0a89a 1424 uint8_t family;
40c7bdb0 1425
d62a17ae 1426 family = NHLFE_FAMILY(nhlfe);
1427 bytelen = (family == AF_INET ? 4 : 16);
1428 _netlink_route_build_singlepath(routedesc, bytelen, nhlfe->nexthop,
1429 nlmsg, rtmsg, req_size, cmd);
40c7bdb0 1430}
1431
1432
1433static inline void
81793ac1 1434_netlink_mpls_build_multipath(const char *routedesc, const zebra_nhlfe_t *nhlfe,
d62a17ae 1435 struct rtattr *rta, struct rtnexthop *rtnh,
81793ac1 1436 struct rtmsg *rtmsg, 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);
1443 _netlink_route_build_multipath(routedesc, bytelen, nhlfe->nexthop, rta,
1444 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()) {
de3f5488
SW
1644 /* Kernel supports nexthop objects */
1645 addattr32(&req.n, sizeof(req), RTA_NH_ID,
1646 dplane_ctx_get_nhe_id(ctx));
1647 goto skip;
1648 }
1649
7cdb1a84 1650 /* Count overall nexthops so we can decide whether to use singlepath
5709131c
MS
1651 * or multipath case.
1652 */
7cdb1a84
MS
1653 nexthop_num = 0;
1654 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1655 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1656 continue;
1657 if (cmd == RTM_NEWROUTE && !NEXTHOP_IS_ACTIVE(nexthop->flags))
1658 continue;
1659
1660 nexthop_num++;
1661 }
1662
1663 /* Singlepath case. */
220f0f42 1664 if (nexthop_num == 1) {
7cdb1a84
MS
1665 nexthop_num = 0;
1666 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1667 /*
1668 * So we want to cover 2 types of blackhole
1669 * routes here:
1670 * 1) A normal blackhole route( ala from a static
1671 * install.
1672 * 2) A recursively resolved blackhole route
1673 */
1674 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1675 switch (nexthop->bh_type) {
1676 case BLACKHOLE_ADMINPROHIB:
1677 req.r.rtm_type = RTN_PROHIBIT;
1678 break;
1679 case BLACKHOLE_REJECT:
1680 req.r.rtm_type = RTN_UNREACHABLE;
1681 break;
1682 default:
1683 req.r.rtm_type = RTN_BLACKHOLE;
1684 break;
1685 }
1686 goto skip;
1687 }
1688 if (CHECK_FLAG(nexthop->flags,
1689 NEXTHOP_FLAG_RECURSIVE)) {
5709131c
MS
1690
1691 if (setsrc)
1692 continue;
1693
1694 if (family == AF_INET) {
1695 if (nexthop->rmap_src.ipv4.s_addr
1696 != 0) {
1697 src.ipv4 =
1698 nexthop->rmap_src.ipv4;
1699 setsrc = 1;
1700 } else if (nexthop->src.ipv4.s_addr
1701 != 0) {
1702 src.ipv4 =
1703 nexthop->src.ipv4;
1704 setsrc = 1;
1705 }
1706 } else if (family == AF_INET6) {
1707 if (!IN6_IS_ADDR_UNSPECIFIED(
1708 &nexthop->rmap_src.ipv6)) {
1709 src.ipv6 =
1710 nexthop->rmap_src.ipv6;
1711 setsrc = 1;
1712 } else if (
1713 !IN6_IS_ADDR_UNSPECIFIED(
1714 &nexthop->src.ipv6)) {
1715 src.ipv6 =
1716 nexthop->src.ipv6;
1717 setsrc = 1;
7cdb1a84
MS
1718 }
1719 }
f183e380 1720 continue;
7cdb1a84
MS
1721 }
1722
1723 if ((cmd == RTM_NEWROUTE
1724 && NEXTHOP_IS_ACTIVE(nexthop->flags))) {
1725 routedesc = nexthop->rparent
1726 ? "recursive, single-path"
1727 : "single-path";
1728
1729 _netlink_route_build_singlepath(
1730 routedesc, bytelen, nexthop, &req.n,
5709131c 1731 &req.r, sizeof(req), cmd);
7cdb1a84
MS
1732 nexthop_num++;
1733 break;
1734 }
1735 }
1736 if (setsrc && (cmd == RTM_NEWROUTE)) {
1737 if (family == AF_INET)
5709131c 1738 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1739 &src.ipv4, bytelen);
1740 else if (family == AF_INET6)
5709131c 1741 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1742 &src.ipv6, bytelen);
1743 }
1744 } else { /* Multipath case */
1745 char buf[NL_PKT_BUF_SIZE];
1746 struct rtattr *rta = (void *)buf;
1747 struct rtnexthop *rtnh;
81793ac1 1748 const union g_addr *src1 = NULL;
7cdb1a84
MS
1749
1750 rta->rta_type = RTA_MULTIPATH;
1751 rta->rta_len = RTA_LENGTH(0);
1752 rtnh = RTA_DATA(rta);
1753
1754 nexthop_num = 0;
1755 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
7cdb1a84
MS
1756 if (CHECK_FLAG(nexthop->flags,
1757 NEXTHOP_FLAG_RECURSIVE)) {
1758 /* This only works for IPv4 now */
5709131c
MS
1759 if (setsrc)
1760 continue;
1761
1762 if (family == AF_INET) {
1763 if (nexthop->rmap_src.ipv4.s_addr
1764 != 0) {
1765 src.ipv4 =
1766 nexthop->rmap_src.ipv4;
1767 setsrc = 1;
1768 } else if (nexthop->src.ipv4.s_addr
1769 != 0) {
1770 src.ipv4 =
1771 nexthop->src.ipv4;
1772 setsrc = 1;
1773 }
1774 } else if (family == AF_INET6) {
1775 if (!IN6_IS_ADDR_UNSPECIFIED(
1776 &nexthop->rmap_src.ipv6)) {
1777 src.ipv6 =
1778 nexthop->rmap_src.ipv6;
1779 setsrc = 1;
1780 } else if (
1781 !IN6_IS_ADDR_UNSPECIFIED(
1782 &nexthop->src.ipv6)) {
1783 src.ipv6 =
1784 nexthop->src.ipv6;
1785 setsrc = 1;
7cdb1a84
MS
1786 }
1787 }
78e54ded
MS
1788
1789 continue;
7cdb1a84
MS
1790 }
1791
1792 if ((cmd == RTM_NEWROUTE
1793 && NEXTHOP_IS_ACTIVE(nexthop->flags))) {
1794 routedesc = nexthop->rparent
1795 ? "recursive, multipath"
1796 : "multipath";
1797 nexthop_num++;
1798
1799 _netlink_route_build_multipath(
1800 routedesc, bytelen, nexthop, rta, rtnh,
1801 &req.r, &src1);
1802 rtnh = RTNH_NEXT(rtnh);
1803
1804 if (!setsrc && src1) {
1805 if (family == AF_INET)
1806 src.ipv4 = src1->ipv4;
1807 else if (family == AF_INET6)
1808 src.ipv6 = src1->ipv6;
1809
1810 setsrc = 1;
1811 }
1812 }
1813 }
1814 if (setsrc && (cmd == RTM_NEWROUTE)) {
1815 if (family == AF_INET)
5709131c 1816 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1817 &src.ipv4, bytelen);
1818 else if (family == AF_INET6)
5709131c 1819 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1820 &src.ipv6, bytelen);
1821 if (IS_ZEBRA_DEBUG_KERNEL)
1822 zlog_debug("Setting source");
1823 }
1824
1825 if (rta->rta_len > RTA_LENGTH(0))
1826 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
1827 RTA_DATA(rta), RTA_PAYLOAD(rta));
1828 }
1829
1830 /* If there is no useful nexthop then return. */
1831 if (nexthop_num == 0) {
1832 if (IS_ZEBRA_DEBUG_KERNEL)
1833 zlog_debug(
1834 "netlink_route_multipath(): No useful nexthop.");
1835 return 0;
1836 }
1837
1838skip:
7cdb1a84
MS
1839 /* Talk to netlink socket. */
1840 return netlink_talk_info(netlink_talk_filter, &req.n,
1841 dplane_ctx_get_ns(ctx), 0);
1842}
1843
43b5cc5e 1844int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in)
e3be0432 1845{
5523c156 1846 uint32_t actual_table;
d62a17ae 1847 int suc = 0;
1848 struct mcast_route_data *mr = (struct mcast_route_data *)in;
bd8b9272
DS
1849 struct {
1850 struct nlmsghdr n;
1851 struct ndmsg ndm;
1852 char buf[256];
1853 } req;
e3be0432 1854
d62a17ae 1855 mroute = mr;
5895d33f 1856 struct zebra_ns *zns;
bd8b9272 1857
009f8ad5 1858 zns = zvrf->zns;
5605ecfc 1859 memset(&req, 0, sizeof(req));
bd8b9272
DS
1860
1861 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1862 req.n.nlmsg_flags = NLM_F_REQUEST;
1863 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1864
1865 req.ndm.ndm_family = RTNL_FAMILY_IPMR;
1866 req.n.nlmsg_type = RTM_GETROUTE;
1867
1868 addattr_l(&req.n, sizeof(req), RTA_IIF, &mroute->ifindex, 4);
1869 addattr_l(&req.n, sizeof(req), RTA_OIF, &mroute->ifindex, 4);
1870 addattr_l(&req.n, sizeof(req), RTA_SRC, &mroute->sg.src.s_addr, 4);
1871 addattr_l(&req.n, sizeof(req), RTA_DST, &mroute->sg.grp.s_addr, 4);
5523c156
DS
1872 /*
1873 * What?
1874 *
1875 * So during the namespace cleanup we started storing
1876 * the zvrf table_id for the default table as RT_TABLE_MAIN
1877 * which is what the normal routing table for ip routing is.
1878 * This change caused this to break our lookups of sg data
1879 * because prior to this change the zvrf->table_id was 0
1880 * and when the pim multicast kernel code saw a 0,
1881 * it was auto-translated to RT_TABLE_DEFAULT. But since
1882 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1883 * and the kernel goes screw you and the delicious cookies you
1884 * are trying to give me. So now we have this little hack.
1885 */
1886 actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT :
1887 zvrf->table_id;
1888 addattr_l(&req.n, sizeof(req), RTA_TABLE, &actual_table, 4);
e3be0432 1889
bd8b9272
DS
1890 suc = netlink_talk(netlink_route_change_read_multicast, &req.n,
1891 &zns->netlink_cmd, zns, 0);
e3be0432 1892
bd8b9272 1893 mroute = NULL;
d62a17ae 1894 return suc;
e3be0432
DS
1895}
1896
8d03bc50
SW
1897/* Char length to debug ID with */
1898#define ID_LENGTH 10
1899
565ce0d3 1900static void _netlink_nexthop_build_group(struct nlmsghdr *n, size_t req_size,
8d03bc50 1901 uint32_t id,
e22e8001 1902 const struct nh_grp *z_grp,
0c8215cb 1903 const uint8_t count)
565ce0d3 1904{
565ce0d3 1905 struct nexthop_grp grp[count];
8d03bc50
SW
1906 /* Need space for max group size, "/", and null term */
1907 char buf[(MULTIPATH_NUM * (ID_LENGTH + 1)) + 1];
1908 char buf1[ID_LENGTH + 2];
1909
1910 buf[0] = '\0';
565ce0d3
SW
1911
1912 memset(grp, 0, sizeof(grp));
1913
1914 if (count) {
0c8215cb 1915 for (int i = 0; i < count; i++) {
e22e8001 1916 grp[i].id = z_grp[i].id;
df7fb580 1917 grp[i].weight = z_grp[i].weight - 1;
8d03bc50
SW
1918
1919 if (IS_ZEBRA_DEBUG_KERNEL) {
1920 if (i == 0)
1921 snprintf(buf, sizeof(buf1), "group %u",
1922 grp[i].id);
1923 else {
1924 snprintf(buf1, sizeof(buf1), "/%u",
1925 grp[i].id);
1926 strlcat(buf, buf1, sizeof(buf));
1927 }
1928 }
565ce0d3 1929 }
0c8215cb 1930 addattr_l(n, req_size, NHA_GROUP, grp, count * sizeof(*grp));
565ce0d3 1931 }
8d03bc50
SW
1932
1933 if (IS_ZEBRA_DEBUG_KERNEL)
1934 zlog_debug("%s: ID (%u): %s", __func__, id, buf);
565ce0d3
SW
1935}
1936
f820d025
SW
1937/**
1938 * netlink_nexthop() - Nexthop change via the netlink interface
1939 *
1940 * @ctx: Dataplane ctx
1941 *
1942 * Return: Result status
1943 */
1944static int netlink_nexthop(int cmd, struct zebra_dplane_ctx *ctx)
1945{
f820d025
SW
1946 struct {
1947 struct nlmsghdr n;
1948 struct nhmsg nhm;
1949 char buf[NL_PKT_BUF_SIZE];
1950 } req;
1951
8d03bc50
SW
1952 mpls_lse_t out_lse[MPLS_MAX_LABELS];
1953 char label_buf[256];
1954 int num_labels = 0;
1955 size_t req_size = sizeof(req);
1956
81505946 1957 /* Nothing to do if the kernel doesn't support nexthop objects */
7c99d51b 1958 if (!kernel_nexthops_supported())
81505946
SW
1959 return 0;
1960
8d03bc50
SW
1961 label_buf[0] = '\0';
1962
1963 memset(&req, 0, req_size);
f820d025 1964
f820d025
SW
1965 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
1966 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
9a1588c4
SW
1967
1968 if (cmd == RTM_NEWNEXTHOP)
1969 req.n.nlmsg_flags |= NLM_F_REPLACE;
1970
f820d025 1971 req.n.nlmsg_type = cmd;
4f096395 1972 req.n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
f820d025
SW
1973
1974 req.nhm.nh_family = AF_UNSPEC;
fec211ad 1975 /* TODO: Scope? */
f820d025 1976
0c8215cb
SW
1977 uint32_t id = dplane_ctx_get_nhe_id(ctx);
1978
1979 if (!id) {
f820d025
SW
1980 flog_err(
1981 EC_ZEBRA_NHG_FIB_UPDATE,
1982 "Failed trying to update a nexthop group in the kernel that does not have an ID");
1983 return -1;
1984 }
1985
8d03bc50 1986 addattr32(&req.n, req_size, NHA_ID, id);
f820d025
SW
1987
1988 if (cmd == RTM_NEWNEXTHOP) {
e22e8001 1989 if (dplane_ctx_get_nhe_nh_grp_count(ctx))
0c8215cb 1990 _netlink_nexthop_build_group(
8d03bc50 1991 &req.n, req_size, id,
e22e8001
SW
1992 dplane_ctx_get_nhe_nh_grp(ctx),
1993 dplane_ctx_get_nhe_nh_grp_count(ctx));
0c8215cb
SW
1994 else {
1995 const struct nexthop *nh =
1996 dplane_ctx_get_nhe_ng(ctx)->nexthop;
1997 afi_t afi = dplane_ctx_get_nhe_afi(ctx);
e8b0e420 1998
0c8215cb 1999 if (afi == AFI_IP)
565ce0d3 2000 req.nhm.nh_family = AF_INET;
0c8215cb 2001 else if (afi == AFI_IP6)
565ce0d3 2002 req.nhm.nh_family = AF_INET6;
f820d025 2003
565ce0d3 2004 switch (nh->type) {
a6e6a6d8 2005 case NEXTHOP_TYPE_IPV4:
565ce0d3 2006 case NEXTHOP_TYPE_IPV4_IFINDEX:
8d03bc50 2007 addattr_l(&req.n, req_size, NHA_GATEWAY,
565ce0d3
SW
2008 &nh->gate.ipv4, IPV4_MAX_BYTELEN);
2009 break;
a6e6a6d8 2010 case NEXTHOP_TYPE_IPV6:
565ce0d3 2011 case NEXTHOP_TYPE_IPV6_IFINDEX:
8d03bc50 2012 addattr_l(&req.n, req_size, NHA_GATEWAY,
565ce0d3
SW
2013 &nh->gate.ipv6, IPV6_MAX_BYTELEN);
2014 break;
2015 case NEXTHOP_TYPE_BLACKHOLE:
8d03bc50
SW
2016 addattr_l(&req.n, req_size, NHA_BLACKHOLE, NULL,
2017 0);
2018 /* Blackhole shouldn't have anymore attributes
2019 */
2020 goto nexthop_done;
565ce0d3
SW
2021 case NEXTHOP_TYPE_IFINDEX:
2022 /* Don't need anymore info for this */
2023 break;
a6e6a6d8
SW
2024 }
2025
2026 if (!nh->ifindex) {
565ce0d3
SW
2027 flog_err(
2028 EC_ZEBRA_NHG_FIB_UPDATE,
2029 "Context received for kernel nexthop update without an interface");
2030 return -1;
565ce0d3
SW
2031 }
2032
8d03bc50
SW
2033 addattr32(&req.n, req_size, NHA_OIF, nh->ifindex);
2034
62d2ecb2
SW
2035 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK))
2036 req.nhm.nh_flags |= RTNH_F_ONLINK;
2037
8d03bc50
SW
2038 num_labels =
2039 build_label_stack(nh->nh_label, out_lse,
2040 label_buf, sizeof(label_buf));
2041
2042 if (num_labels) {
2043 /* Set the BoS bit */
2044 out_lse[num_labels - 1] |=
2045 htonl(1 << MPLS_LS_S_SHIFT);
2046
2047 /*
2048 * TODO: MPLS unsupported for now in kernel.
2049 */
2050 if (req.nhm.nh_family == AF_MPLS)
2051 goto nexthop_done;
2052#if 0
2053 addattr_l(&req.n, req_size, NHA_NEWDST,
2054 &out_lse,
2055 num_labels
2056 * sizeof(mpls_lse_t));
2057#endif
2058 else {
2059 struct rtattr *nest;
2060 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
2061
2062 addattr_l(&req.n, req_size,
2063 NHA_ENCAP_TYPE, &encap,
2064 sizeof(uint16_t));
2065 nest = addattr_nest(&req.n, req_size,
2066 NHA_ENCAP);
2067 addattr_l(&req.n, req_size,
2068 MPLS_IPTUNNEL_DST, &out_lse,
2069 num_labels
2070 * sizeof(mpls_lse_t));
2071 addattr_nest_end(&req.n, nest);
2072 }
2073 }
2074
2075 nexthop_done:
2076 if (IS_ZEBRA_DEBUG_KERNEL) {
2077 char buf[NEXTHOP_STRLEN];
2078
2079 snprintfrr(buf, sizeof(buf), "%pNHv", nh);
2080 zlog_debug("%s: ID (%u): %s (%u) %s ", __func__,
2081 id, buf, nh->vrf_id, label_buf);
2082 }
f820d025
SW
2083 }
2084
38e40db1 2085 req.nhm.nh_protocol = zebra2proto(dplane_ctx_get_nhe_type(ctx));
f820d025 2086
f820d025
SW
2087 } else if (cmd != RTM_DELNEXTHOP) {
2088 flog_err(
2089 EC_ZEBRA_NHG_FIB_UPDATE,
2090 "Nexthop group kernel update command (%d) does not exist",
2091 cmd);
2092 return -1;
2093 }
2094
0c8215cb 2095 _netlink_nexthop_debug(cmd, id);
f820d025
SW
2096
2097 return netlink_talk_info(netlink_talk_filter, &req.n,
2098 dplane_ctx_get_ns(ctx), 0);
2099}
2100
2101/**
2102 * kernel_nexthop_update() - Update/delete a nexthop from the kernel
2103 *
2104 * @ctx: Dataplane context
2105 *
2106 * Return: Dataplane result flag
2107 */
2108enum zebra_dplane_result kernel_nexthop_update(struct zebra_dplane_ctx *ctx)
2109{
98cda54a
SW
2110 int cmd = 0;
2111 int ret = 0;
f820d025
SW
2112
2113 switch (dplane_ctx_get_op(ctx)) {
2114 case DPLANE_OP_NH_DELETE:
2115 cmd = RTM_DELNEXTHOP;
2116 break;
2117 case DPLANE_OP_NH_INSTALL:
2118 case DPLANE_OP_NH_UPDATE:
2119 cmd = RTM_NEWNEXTHOP;
2120 break;
2121 case DPLANE_OP_ROUTE_INSTALL:
2122 case DPLANE_OP_ROUTE_UPDATE:
2123 case DPLANE_OP_ROUTE_DELETE:
2124 case DPLANE_OP_ROUTE_NOTIFY:
2125 case DPLANE_OP_LSP_INSTALL:
2126 case DPLANE_OP_LSP_UPDATE:
2127 case DPLANE_OP_LSP_DELETE:
2128 case DPLANE_OP_LSP_NOTIFY:
2129 case DPLANE_OP_PW_INSTALL:
2130 case DPLANE_OP_PW_UNINSTALL:
2131 case DPLANE_OP_SYS_ROUTE_ADD:
2132 case DPLANE_OP_SYS_ROUTE_DELETE:
2133 case DPLANE_OP_ADDR_INSTALL:
2134 case DPLANE_OP_ADDR_UNINSTALL:
2135 case DPLANE_OP_MAC_INSTALL:
2136 case DPLANE_OP_MAC_DELETE:
40a2a6cd
SW
2137 case DPLANE_OP_NEIGH_INSTALL:
2138 case DPLANE_OP_NEIGH_UPDATE:
2139 case DPLANE_OP_NEIGH_DELETE:
2140 case DPLANE_OP_VTEP_ADD:
2141 case DPLANE_OP_VTEP_DELETE:
f820d025
SW
2142 case DPLANE_OP_NONE:
2143 flog_err(
2144 EC_ZEBRA_NHG_FIB_UPDATE,
2145 "Context received for kernel nexthop update with incorrect OP code (%u)",
2146 dplane_ctx_get_op(ctx));
2147 return ZEBRA_DPLANE_REQUEST_FAILURE;
f820d025
SW
2148 }
2149
2150 ret = netlink_nexthop(cmd, ctx);
2151
2152 return (ret == 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2153 : ZEBRA_DPLANE_REQUEST_FAILURE);
2154}
2155
7cdb1a84
MS
2156/*
2157 * Update or delete a prefix from the kernel,
2158 * using info from a dataplane context.
2159 */
25779064 2160enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
2161{
2162 int cmd, ret;
2163 const struct prefix *p = dplane_ctx_get_dest(ctx);
f183e380 2164 struct nexthop *nexthop;
7cdb1a84
MS
2165
2166 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
2167 cmd = RTM_DELROUTE;
2168 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
2169 cmd = RTM_NEWROUTE;
2170 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
2171
2172 if (p->family == AF_INET || v6_rr_semantics) {
2173 /* Single 'replace' operation */
2174 cmd = RTM_NEWROUTE;
fe5f21af
DS
2175
2176 /*
2177 * With route replace semantics in place
2178 * for v4 routes and the new route is a system
2179 * route we do not install anything.
2180 * The problem here is that the new system
2181 * route should cause us to withdraw from
2182 * the kernel the old non-system route
2183 */
2184 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)) &&
2185 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2186 (void)netlink_route_multipath(RTM_DELROUTE,
2187 ctx);
7cdb1a84
MS
2188 } else {
2189 /*
2190 * So v6 route replace semantics are not in
2191 * the kernel at this point as I understand it.
2192 * so let's do a delete then an add.
2193 * In the future once v6 route replace semantics
2194 * are in we can figure out what to do here to
2195 * allow working with old and new kernels.
2196 *
2197 * I'm also intentionally ignoring the failure case
2198 * of the route delete. If that happens yeah we're
2199 * screwed.
2200 */
3cdba47a
DS
2201 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2202 (void)netlink_route_multipath(RTM_DELROUTE,
2203 ctx);
7cdb1a84
MS
2204 cmd = RTM_NEWROUTE;
2205 }
2206
2207 } else {
2208 return ZEBRA_DPLANE_REQUEST_FAILURE;
2209 }
2210
3cdba47a
DS
2211 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)))
2212 ret = netlink_route_multipath(cmd, ctx);
2213 else
2214 ret = 0;
f183e380
MS
2215 if ((cmd == RTM_NEWROUTE) && (ret == 0)) {
2216 /* Update installed nexthops to signal which have been
2217 * installed.
2218 */
2219 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2220 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
2221 continue;
2222
2223 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
2224 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380
MS
2225 }
2226 }
2227 }
7cdb1a84
MS
2228
2229 return (ret == 0 ?
2230 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
2231}
2232
d9f5b2f5
SW
2233/**
2234 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2235 *
2236 * @tb: Netlink RTA data
2237 * @family: Address family in the nhmsg
8c0a24c1 2238 * @ifp: Interface connected - this should be NULL, we fill it in
d9f5b2f5
SW
2239 * @ns_id: Namspace id
2240 *
2241 * Return: New nexthop
2242 */
e22e8001
SW
2243static struct nexthop netlink_nexthop_process_nh(struct rtattr **tb,
2244 unsigned char family,
2245 struct interface **ifp,
2246 ns_id_t ns_id)
d9f5b2f5 2247{
e22e8001 2248 struct nexthop nh = {};
d9f5b2f5 2249 void *gate = NULL;
8e401b25 2250 enum nexthop_types_t type = 0;
e22e8001
SW
2251 int if_index = 0;
2252 size_t sz = 0;
7134ba70 2253 struct interface *ifp_lookup;
d9f5b2f5
SW
2254
2255 if_index = *(int *)RTA_DATA(tb[NHA_OIF]);
2256
8e401b25 2257
d9f5b2f5
SW
2258 if (tb[NHA_GATEWAY]) {
2259 switch (family) {
2260 case AF_INET:
8e401b25 2261 type = NEXTHOP_TYPE_IPV4_IFINDEX;
d9f5b2f5
SW
2262 sz = 4;
2263 break;
2264 case AF_INET6:
8e401b25 2265 type = NEXTHOP_TYPE_IPV6_IFINDEX;
d9f5b2f5
SW
2266 sz = 16;
2267 break;
2268 default:
2269 flog_warn(
2270 EC_ZEBRA_BAD_NHG_MESSAGE,
c4239c05 2271 "Nexthop gateway with bad address family (%d) received from kernel",
d9f5b2f5 2272 family);
e22e8001 2273 return nh;
d9f5b2f5
SW
2274 }
2275 gate = RTA_DATA(tb[NHA_GATEWAY]);
e22e8001 2276 } else
8e401b25 2277 type = NEXTHOP_TYPE_IFINDEX;
d9f5b2f5 2278
8e401b25 2279 if (type)
e22e8001 2280 nh.type = type;
8e401b25
SW
2281
2282 if (gate)
e22e8001 2283 memcpy(&(nh.gate), gate, sz);
8e401b25
SW
2284
2285 if (if_index)
e22e8001 2286 nh.ifindex = if_index;
8e401b25 2287
7134ba70
DS
2288 ifp_lookup =
2289 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), nh.ifindex);
2290
e22e8001 2291 if (ifp)
7134ba70
DS
2292 *ifp = ifp_lookup;
2293 if (ifp_lookup)
2294 nh.vrf_id = ifp_lookup->vrf_id;
e22e8001 2295 else {
d9f5b2f5
SW
2296 flog_warn(
2297 EC_ZEBRA_UNKNOWN_INTERFACE,
2298 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
e22e8001 2299 __PRETTY_FUNCTION__, nh.ifindex);
d9f5b2f5 2300
e22e8001 2301 nh.vrf_id = VRF_DEFAULT;
d9f5b2f5
SW
2302 }
2303
2304 if (tb[NHA_ENCAP] && tb[NHA_ENCAP_TYPE]) {
2305 uint16_t encap_type = *(uint16_t *)RTA_DATA(tb[NHA_ENCAP_TYPE]);
2306 int num_labels = 0;
6e728764 2307
d9f5b2f5
SW
2308 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
2309
e22e8001 2310 if (encap_type == LWTUNNEL_ENCAP_MPLS)
d9f5b2f5 2311 num_labels = parse_encap_mpls(tb[NHA_ENCAP], labels);
d9f5b2f5 2312
e22e8001
SW
2313 if (num_labels)
2314 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels,
d9f5b2f5 2315 labels);
d9f5b2f5
SW
2316 }
2317
2318 return nh;
2319}
2320
85f5e761 2321static int netlink_nexthop_process_group(struct rtattr **tb,
5a935f79 2322 struct nh_grp *z_grp, int z_grp_size)
d9f5b2f5 2323{
e22e8001
SW
2324 uint8_t count = 0;
2325 /* linux/nexthop.h group struct */
d9f5b2f5
SW
2326 struct nexthop_grp *n_grp = NULL;
2327
85f5e761 2328 n_grp = (struct nexthop_grp *)RTA_DATA(tb[NHA_GROUP]);
d9f5b2f5
SW
2329 count = (RTA_PAYLOAD(tb[NHA_GROUP]) / sizeof(*n_grp));
2330
2331 if (!count || (count * sizeof(*n_grp)) != RTA_PAYLOAD(tb[NHA_GROUP])) {
2332 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE,
2333 "Invalid nexthop group received from the kernel");
85f5e761 2334 return count;
d9f5b2f5
SW
2335 }
2336
38e40db1 2337#if 0
d9f5b2f5 2338 // TODO: Need type for something?
85f5e761
SW
2339 zlog_debug("Nexthop group type: %d",
2340 *((uint16_t *)RTA_DATA(tb[NHA_GROUP_TYPE])));
d9f5b2f5 2341
38e40db1 2342#endif
d9f5b2f5 2343
5a935f79 2344 for (int i = 0; ((i < count) && (i < z_grp_size)); i++) {
e22e8001 2345 z_grp[i].id = n_grp[i].id;
df7fb580 2346 z_grp[i].weight = n_grp[i].weight + 1;
85f5e761 2347 }
d9f5b2f5
SW
2348 return count;
2349}
2350
2351/**
2352 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2353 *
2354 * @h: Netlink message header
2355 * @ns_id: Namspace id
2356 * @startup: Are we reading under startup conditions?
2357 *
2358 * Return: Result status
2359 */
2360int netlink_nexthop_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2361{
2362 int len;
2363 /* nexthop group id */
2364 uint32_t id;
2365 unsigned char family;
38e40db1 2366 int type;
e8b0e420 2367 afi_t afi = AFI_UNSPEC;
946de1b9 2368 vrf_id_t vrf_id = VRF_DEFAULT;
8c0a24c1 2369 struct interface *ifp = NULL;
d9f5b2f5 2370 struct nhmsg *nhm = NULL;
e22e8001
SW
2371 struct nexthop nh = {};
2372 struct nh_grp grp[MULTIPATH_NUM] = {};
85f5e761 2373 /* Count of nexthops in group array */
e22e8001 2374 uint8_t grp_count = 0;
e22e8001 2375 struct rtattr *tb[NHA_MAX + 1] = {};
d9f5b2f5 2376
d9f5b2f5
SW
2377 nhm = NLMSG_DATA(h);
2378
88cafda7
DS
2379 if (ns_id)
2380 vrf_id = ns_id;
2381
d9f5b2f5
SW
2382 if (startup && h->nlmsg_type != RTM_NEWNEXTHOP)
2383 return 0;
2384
2385 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct nhmsg));
2386 if (len < 0) {
2387 zlog_warn(
2388 "%s: Message received from netlink is of a broken size %d %zu",
2389 __PRETTY_FUNCTION__, h->nlmsg_len,
2390 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg)));
2391 return -1;
2392 }
2393
d9f5b2f5
SW
2394 netlink_parse_rtattr(tb, NHA_MAX, RTM_NHA(nhm), len);
2395
2396
2397 if (!tb[NHA_ID]) {
2398 flog_warn(
2399 EC_ZEBRA_BAD_NHG_MESSAGE,
2400 "Nexthop group without an ID received from the kernel");
2401 return -1;
2402 }
2403
2404 /* We use the ID key'd nhg table for kernel updates */
2405 id = *((uint32_t *)RTA_DATA(tb[NHA_ID]));
d9f5b2f5 2406
e8b0e420 2407 family = nhm->nh_family;
e8b0e420
SW
2408 afi = family2afi(family);
2409
38e40db1
SW
2410 type = proto2zebra(nhm->nh_protocol, 0, true);
2411
fdee485a
SW
2412 if (IS_ZEBRA_DEBUG_KERNEL)
2413 zlog_debug("%s ID (%u) %s NS %u",
2414 nl_msg_type_to_str(h->nlmsg_type), id,
2415 nl_family_to_str(family), ns_id);
2416
2417
d9f5b2f5
SW
2418 if (h->nlmsg_type == RTM_NEWNEXTHOP) {
2419 if (tb[NHA_GROUP]) {
2420 /**
2421 * If this is a group message its only going to have
2422 * an array of nexthop IDs associated with it
2423 */
5a935f79
SW
2424 grp_count = netlink_nexthop_process_group(
2425 tb, grp, array_size(grp));
85f5e761
SW
2426 } else {
2427 if (tb[NHA_BLACKHOLE]) {
2428 /**
2429 * This nexthop is just for blackhole-ing
2430 * traffic, it should not have an OIF, GATEWAY,
2431 * or ENCAP
2432 */
e22e8001
SW
2433 nh.type = NEXTHOP_TYPE_BLACKHOLE;
2434 nh.bh_type = BLACKHOLE_UNSPEC;
2435 } else if (tb[NHA_OIF])
85f5e761
SW
2436 /**
2437 * This is a true new nexthop, so we need
2438 * to parse the gateway and device info
2439 */
2440 nh = netlink_nexthop_process_nh(tb, family,
2441 &ifp, ns_id);
e22e8001
SW
2442 else {
2443
8e401b25
SW
2444 flog_warn(
2445 EC_ZEBRA_BAD_NHG_MESSAGE,
2446 "Invalid Nexthop message received from the kernel with ID (%u)",
2447 id);
2448 return -1;
2449 }
e22e8001
SW
2450 SET_FLAG(nh.flags, NEXTHOP_FLAG_ACTIVE);
2451 if (nhm->nh_flags & RTNH_F_ONLINK)
2452 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
2453 vrf_id = nh.vrf_id;
d9f5b2f5
SW
2454 }
2455
38e40db1
SW
2456 if (zebra_nhg_kernel_find(id, &nh, grp, grp_count, vrf_id, afi,
2457 type, startup))
e22e8001 2458 return -1;
8e401b25 2459
9a1588c4 2460 } else if (h->nlmsg_type == RTM_DELNEXTHOP)
88cafda7 2461 zebra_nhg_kernel_del(id, vrf_id);
d9f5b2f5 2462
d9f5b2f5
SW
2463 return 0;
2464}
2465
2466/**
2467 * netlink_request_nexthop() - Request nextop information from the kernel
2468 * @zns: Zebra namespace
2469 * @family: AF_* netlink family
2470 * @type: RTM_* route type
2471 *
2472 * Return: Result status
2473 */
2474static int netlink_request_nexthop(struct zebra_ns *zns, int family, int type)
2475{
2476 struct {
2477 struct nlmsghdr n;
2478 struct nhmsg nhm;
2479 } req;
2480
2481 /* Form the request, specifying filter (rtattr) if needed. */
2482 memset(&req, 0, sizeof(req));
2483 req.n.nlmsg_type = type;
2484 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2485 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2486 req.nhm.nh_family = family;
2487
2488 return netlink_request(&zns->netlink_cmd, &req.n);
2489}
2490
7d5bb02b 2491
d9f5b2f5
SW
2492/**
2493 * netlink_nexthop_read() - Nexthop read function using netlink interface
2494 *
2495 * @zns: Zebra name space
2496 *
2497 * Return: Result status
2498 * Only called at bootstrap time.
2499 */
2500int netlink_nexthop_read(struct zebra_ns *zns)
2501{
2502 int ret;
2503 struct zebra_dplane_info dp_info;
2504
2505 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2506
2507 /* Get nexthop objects */
2508 ret = netlink_request_nexthop(zns, AF_UNSPEC, RTM_GETNEXTHOP);
2509 if (ret < 0)
2510 return ret;
2511 ret = netlink_parse_info(netlink_nexthop_change, &zns->netlink_cmd,
2512 &dp_info, 0, 1);
81505946
SW
2513
2514 if (!ret)
2515 /* If we succesfully read in nexthop objects,
2516 * this kernel must support them.
2517 */
2518 supports_nh = true;
7c99d51b
MS
2519
2520 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG)
2521 zlog_debug("Nexthop objects %ssupported on this kernel",
2522 supports_nh ? "" : "not ");
81505946 2523
60e0eaee 2524 return ret;
d9f5b2f5
SW
2525}
2526
2527
d62a17ae 2528int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 2529 int llalen, ns_id_t ns_id)
6b8a5694 2530{
d62a17ae 2531 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
5895d33f 2532 addr, lla, llalen, ns_id);
6b8a5694 2533}
718e3744 2534
13d60d35 2535/*
2536 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2537 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2538 */
0bbd4ff4
MS
2539static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx *ctx,
2540 int cmd)
13d60d35 2541{
f3dbec60 2542 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 2543 struct {
2544 struct nlmsghdr n;
2545 struct ndmsg ndm;
2546 char buf[256];
2547 } req;
d7c0a89a 2548 uint8_t dst_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
0bbd4ff4 2549 const struct ipaddr *addr;
d62a17ae 2550
5605ecfc 2551 memset(&req, 0, sizeof(req));
d62a17ae 2552
2553 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2554 req.n.nlmsg_flags = NLM_F_REQUEST;
2555 if (cmd == RTM_NEWNEIGH)
2556 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_APPEND);
2557 req.n.nlmsg_type = cmd;
2558 req.ndm.ndm_family = PF_BRIDGE;
2559 req.ndm.ndm_state = NUD_NOARP | NUD_PERMANENT;
fec211ad 2560 req.ndm.ndm_flags |= NTF_SELF; /* Handle by "self", not "master" */
d62a17ae 2561
2562
f3dbec60
DS
2563 addattr_l(&req.n, sizeof(req),
2564 NDA_PROTOCOL, &protocol, sizeof(protocol));
d62a17ae 2565 addattr_l(&req.n, sizeof(req), NDA_LLADDR, &dst_mac, 6);
0bbd4ff4 2566 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
13d60d35 2567
0bbd4ff4 2568 addr = dplane_ctx_neigh_get_ipaddr(ctx);
13d60d35 2569
0bbd4ff4 2570 addattr_l(&req.n, sizeof(req), NDA_DST, &(addr->ipaddr_v4), 4);
13d60d35 2571
0bbd4ff4
MS
2572 return netlink_talk_info(netlink_talk_filter, &req.n,
2573 dplane_ctx_get_ns(ctx), 0);
13d60d35 2574}
2575
2232a77c 2576#ifndef NDA_RTA
d62a17ae 2577#define NDA_RTA(r) \
2578 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2232a77c 2579#endif
2580
2414abd3 2581static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 2582{
d62a17ae 2583 struct ndmsg *ndm;
2584 struct interface *ifp;
2585 struct zebra_if *zif;
d62a17ae 2586 struct rtattr *tb[NDA_MAX + 1];
2587 struct interface *br_if;
2588 struct ethaddr mac;
2589 vlanid_t vid = 0;
2590 struct prefix vtep_ip;
2591 int vid_present = 0, dst_present = 0;
2592 char buf[ETHER_ADDR_STRLEN];
2593 char vid_buf[20];
2594 char dst_buf[30];
a37f4598 2595 bool sticky;
d62a17ae 2596
2597 ndm = NLMSG_DATA(h);
2598
2853fed6 2599 /* We only process macfdb notifications if EVPN is enabled */
2600 if (!is_evpn_enabled())
2601 return 0;
2602
d62a17ae 2603 /* The interface should exist. */
5895d33f 2604 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 2605 ndm->ndm_ifindex);
28bd0652
DS
2606 if (!ifp || !ifp->info) {
2607 if (IS_ZEBRA_DEBUG_KERNEL)
2608 zlog_debug("\t%s without associated interface: %u",
2609 __PRETTY_FUNCTION__, ndm->ndm_ifindex);
d62a17ae 2610 return 0;
28bd0652 2611 }
d62a17ae 2612
2613 /* The interface should be something we're interested in. */
28bd0652
DS
2614 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
2615 if (IS_ZEBRA_DEBUG_KERNEL)
2616 zlog_debug("\t%s Not interested in %s, not a slave",
2617 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 2618 return 0;
28bd0652 2619 }
d62a17ae 2620
2621 /* Drop "permanent" entries. */
28bd0652
DS
2622 if (ndm->ndm_state & NUD_PERMANENT) {
2623 if (IS_ZEBRA_DEBUG_KERNEL)
2624 zlog_debug("\t%s Entry is PERMANENT, dropping",
2625 __PRETTY_FUNCTION__);
d62a17ae 2626 return 0;
28bd0652 2627 }
d62a17ae 2628
2629 zif = (struct zebra_if *)ifp->info;
2630 if ((br_if = zif->brslave_info.br_if) == NULL) {
28bd0652
DS
2631 if (IS_ZEBRA_DEBUG_KERNEL)
2632 zlog_debug(
2633 "%s family %s IF %s(%u) brIF %u - no bridge master",
2634 nl_msg_type_to_str(h->nlmsg_type),
2635 nl_family_to_str(ndm->ndm_family), ifp->name,
2636 ndm->ndm_ifindex,
2637 zif->brslave_info.bridge_ifindex);
d62a17ae 2638 return 0;
2639 }
2640
2641 /* Parse attributes and extract fields of interest. */
2642 memset(tb, 0, sizeof tb);
2643 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
2644
2645 if (!tb[NDA_LLADDR]) {
28bd0652
DS
2646 if (IS_ZEBRA_DEBUG_KERNEL)
2647 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2648 nl_msg_type_to_str(h->nlmsg_type),
2649 nl_family_to_str(ndm->ndm_family), ifp->name,
2650 ndm->ndm_ifindex,
2651 zif->brslave_info.bridge_ifindex);
d62a17ae 2652 return 0;
2653 }
2654
ff8b7eb8 2655 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
2656 if (IS_ZEBRA_DEBUG_KERNEL)
2657 zlog_debug(
2658 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2659 nl_msg_type_to_str(h->nlmsg_type),
2660 nl_family_to_str(ndm->ndm_family), ifp->name,
2661 ndm->ndm_ifindex,
2662 zif->brslave_info.bridge_ifindex,
2663 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
d62a17ae 2664 return 0;
2665 }
2666
ff8b7eb8 2667 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 2668
2669 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
2670 vid_present = 1;
d7c0a89a 2671 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
d62a17ae 2672 sprintf(vid_buf, " VLAN %u", vid);
2673 }
2674
2675 if (tb[NDA_DST]) {
2676 /* TODO: Only IPv4 supported now. */
2677 dst_present = 1;
2678 vtep_ip.family = AF_INET;
2679 vtep_ip.prefixlen = IPV4_MAX_BITLEN;
2680 memcpy(&(vtep_ip.u.prefix4.s_addr), RTA_DATA(tb[NDA_DST]),
2681 IPV4_MAX_BYTELEN);
2682 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip.u.prefix4));
2683 }
2684
a37f4598 2685 sticky = !!(ndm->ndm_state & NUD_NOARP);
d62a17ae 2686
2687 if (IS_ZEBRA_DEBUG_KERNEL)
2688 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2689 nl_msg_type_to_str(h->nlmsg_type),
2690 nl_family_to_str(ndm->ndm_family), ifp->name,
2691 ndm->ndm_ifindex, vid_present ? vid_buf : "",
2692 sticky ? "sticky " : "",
2693 prefix_mac2str(&mac, buf, sizeof(buf)),
2694 dst_present ? dst_buf : "");
2695
28bd0652
DS
2696 if (filter_vlan && vid != filter_vlan) {
2697 if (IS_ZEBRA_DEBUG_KERNEL)
2698 zlog_debug("\tFiltered due to filter vlan: %d",
2699 filter_vlan);
d62a17ae 2700 return 0;
28bd0652 2701 }
d62a17ae 2702
2703 /* If add or update, do accordingly if learnt on a "local" interface; if
2704 * the notification is over VxLAN, this has to be related to
2705 * multi-homing,
2706 * so perform an implicit delete of any local entry (if it exists).
2707 */
2708 if (h->nlmsg_type == RTM_NEWNEIGH) {
d62a17ae 2709 if (IS_ZEBRA_IF_VXLAN(ifp))
2710 return zebra_vxlan_check_del_local_mac(ifp, br_if, &mac,
2711 vid);
2712
2713 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
2714 sticky);
2715 }
2716
2717 /* This is a delete notification.
2718 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2719 * 2. For a MAC over "local" interface, delete the mac
2720 * Note: We will get notifications from both bridge driver and VxLAN
2721 * driver.
2722 * Ignore the notification from VxLan driver as it is also generated
2723 * when mac moves from remote to local.
2724 */
28bd0652
DS
2725 if (dst_present) {
2726 if (IS_ZEBRA_DEBUG_KERNEL)
2727 zlog_debug("\tNo Destination Present");
d62a17ae 2728 return 0;
28bd0652 2729 }
d62a17ae 2730
2731 if (IS_ZEBRA_IF_VXLAN(ifp))
2732 return zebra_vxlan_check_readd_remote_mac(ifp, br_if, &mac,
2733 vid);
2734
2735 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
2232a77c 2736}
2737
2414abd3 2738static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 2739{
d62a17ae 2740 int len;
2741 struct ndmsg *ndm;
2232a77c 2742
d62a17ae 2743 if (h->nlmsg_type != RTM_NEWNEIGH)
2744 return 0;
2232a77c 2745
d62a17ae 2746 /* Length validity. */
2747 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2748 if (len < 0)
2749 return -1;
2232a77c 2750
d62a17ae 2751 /* We are interested only in AF_BRIDGE notifications. */
2752 ndm = NLMSG_DATA(h);
2753 if (ndm->ndm_family != AF_BRIDGE)
2754 return 0;
2232a77c 2755
2414abd3 2756 return netlink_macfdb_change(h, len, ns_id);
2232a77c 2757}
2758
2759/* Request for MAC FDB information from the kernel */
85a75f1e
MS
2760static int netlink_request_macs(struct nlsock *netlink_cmd, int family,
2761 int type, ifindex_t master_ifindex)
2232a77c 2762{
d62a17ae 2763 struct {
2764 struct nlmsghdr n;
2765 struct ifinfomsg ifm;
2766 char buf[256];
2767 } req;
2768
2769 /* Form the request, specifying filter (rtattr) if needed. */
2770 memset(&req, 0, sizeof(req));
2771 req.n.nlmsg_type = type;
718f9b0f 2772 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 2773 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2774 req.ifm.ifi_family = family;
2775 if (master_ifindex)
2776 addattr32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
2777
85a75f1e 2778 return netlink_request(netlink_cmd, &req.n);
2232a77c 2779}
2780
2781/*
2782 * MAC forwarding database read using netlink interface. This is invoked
2783 * at startup.
2784 */
d62a17ae 2785int netlink_macfdb_read(struct zebra_ns *zns)
2232a77c 2786{
d62a17ae 2787 int ret;
85a75f1e
MS
2788 struct zebra_dplane_info dp_info;
2789
2790 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2791
2792 /* Get bridge FDB table. */
85a75f1e
MS
2793 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
2794 0);
d62a17ae 2795 if (ret < 0)
2796 return ret;
2797 /* We are reading entire table. */
2798 filter_vlan = 0;
85a75f1e
MS
2799 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2800 &dp_info, 0, 1);
d62a17ae 2801
2802 return ret;
2232a77c 2803}
2804
2805/*
2806 * MAC forwarding database read using netlink interface. This is for a
2807 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2808 */
d62a17ae 2809int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
2810 struct interface *br_if)
2232a77c 2811{
d62a17ae 2812 struct zebra_if *br_zif;
2813 struct zebra_if *zif;
2814 struct zebra_l2info_vxlan *vxl;
85a75f1e 2815 struct zebra_dplane_info dp_info;
d62a17ae 2816 int ret = 0;
2817
85a75f1e 2818 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2819
2820 /* Save VLAN we're filtering on, if needed. */
2821 br_zif = (struct zebra_if *)br_if->info;
2822 zif = (struct zebra_if *)ifp->info;
2823 vxl = &zif->l2info.vxl;
2824 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
2825 filter_vlan = vxl->access_vlan;
2826
2827 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2828 */
85a75f1e 2829 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
d62a17ae 2830 br_if->ifindex);
2831 if (ret < 0)
2832 return ret;
85a75f1e
MS
2833 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2834 &dp_info, 0, 0);
d62a17ae 2835
2836 /* Reset VLAN filter. */
2837 filter_vlan = 0;
2838 return ret;
2232a77c 2839}
2840
67fb9374
CS
2841
2842/* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2843static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns,
2844 int family,
2845 int type,
2846 struct interface *br_if,
2847 struct ethaddr *mac,
2848 vlanid_t vid)
2849{
2850 struct {
2851 struct nlmsghdr n;
2852 struct ndmsg ndm;
2853 char buf[256];
2854 } req;
2855 struct zebra_if *br_zif;
2856 char buf[ETHER_ADDR_STRLEN];
2857
2858 memset(&req, 0, sizeof(req));
2859 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2860 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
2861 req.n.nlmsg_flags = NLM_F_REQUEST;
2862 req.ndm.ndm_family = family; /* AF_BRIDGE */
2863 /* req.ndm.ndm_state = NUD_REACHABLE; */
2864
2865 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2866
2867 br_zif = (struct zebra_if *)br_if->info;
2868 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0)
2869 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2870
2871 addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
2872
2873 if (IS_ZEBRA_DEBUG_KERNEL)
2874 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2875 __PRETTY_FUNCTION__,
2876 nl_family_to_str(req.ndm.ndm_family), br_if->name,
2877 br_if->ifindex,
2878 prefix_mac2str(mac, buf, sizeof(buf)), vid);
2879
2880 return netlink_request(&zns->netlink_cmd, &req.n);
2881}
2882
2883int netlink_macfdb_read_specific_mac(struct zebra_ns *zns,
2884 struct interface *br_if,
2885 struct ethaddr *mac, vlanid_t vid)
2886{
2887 int ret = 0;
2888 struct zebra_dplane_info dp_info;
2889
2890 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2891
2892 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2893 */
2894 ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE,
2895 RTM_GETNEIGH,
2896 br_if, mac, vid);
2897 if (ret < 0)
2898 return ret;
2899
2900 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2901 &dp_info, 1, 0);
2902
2903 return ret;
2904}
036d93c0
MS
2905
2906/*
2907 * Netlink-specific handler for MAC updates using dataplane context object.
2908 */
2909static enum zebra_dplane_result
2910netlink_macfdb_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 2911{
f3dbec60 2912 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 2913 struct {
2914 struct nlmsghdr n;
2915 struct ndmsg ndm;
2916 char buf[256];
2917 } req;
036d93c0 2918 int ret;
d62a17ae 2919 int dst_alen;
36590076 2920 int vid_present = 0;
036d93c0
MS
2921 int cmd;
2922 struct in_addr vtep_ip;
2923 vlanid_t vid;
2924
2925 if (dplane_ctx_get_op(ctx) == DPLANE_OP_MAC_INSTALL)
2926 cmd = RTM_NEWNEIGH;
2927 else
2928 cmd = RTM_DELNEIGH;
2929
5605ecfc 2930 memset(&req, 0, sizeof(req));
d62a17ae 2931
2932 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2933 req.n.nlmsg_flags = NLM_F_REQUEST;
2934 if (cmd == RTM_NEWNEIGH)
2935 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2936 req.n.nlmsg_type = cmd;
2937 req.ndm.ndm_family = AF_BRIDGE;
2938 req.ndm.ndm_flags |= NTF_SELF | NTF_MASTER;
2939 req.ndm.ndm_state = NUD_REACHABLE;
2940
036d93c0 2941 if (dplane_ctx_mac_is_sticky(ctx))
d62a17ae 2942 req.ndm.ndm_state |= NUD_NOARP;
2943 else
2944 req.ndm.ndm_flags |= NTF_EXT_LEARNED;
2945
f3dbec60
DS
2946 addattr_l(&req.n, sizeof(req),
2947 NDA_PROTOCOL, &protocol, sizeof(protocol));
036d93c0
MS
2948 addattr_l(&req.n, sizeof(req), NDA_LLADDR,
2949 dplane_ctx_mac_get_addr(ctx), 6);
478566d6 2950 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
036d93c0 2951
d63c1b18 2952 dst_alen = 4; // TODO: hardcoded
036d93c0 2953 vtep_ip = *(dplane_ctx_mac_get_vtep_ip(ctx));
d63c1b18 2954 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip, dst_alen);
036d93c0 2955
478566d6
MS
2956 vid = dplane_ctx_mac_get_vlan(ctx);
2957
2958 if (vid > 0) {
d62a17ae 2959 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2960 vid_present = 1;
d62a17ae 2961 }
478566d6
MS
2962 addattr32(&req.n, sizeof(req), NDA_MASTER,
2963 dplane_ctx_mac_get_br_ifindex(ctx));
d62a17ae 2964
036d93c0
MS
2965 if (IS_ZEBRA_DEBUG_KERNEL) {
2966 char ipbuf[PREFIX_STRLEN];
2967 char buf[ETHER_ADDR_STRLEN];
2968 char dst_buf[PREFIX_STRLEN + 10];
478566d6
MS
2969 char vid_buf[20];
2970
2971 if (vid_present)
2972 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
2973 else
2974 vid_buf[0] = '\0';
036d93c0
MS
2975
2976 inet_ntop(AF_INET, &vtep_ip, ipbuf, sizeof(ipbuf));
2977 snprintf(dst_buf, sizeof(dst_buf), " dst %s", ipbuf);
2978 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf, sizeof(buf));
2979
d62a17ae 2980 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2981 nl_msg_type_to_str(cmd),
478566d6
MS
2982 nl_family_to_str(req.ndm.ndm_family),
2983 dplane_ctx_get_ifname(ctx),
2984 dplane_ctx_get_ifindex(ctx), vid_buf,
036d93c0
MS
2985 dplane_ctx_mac_is_sticky(ctx) ? "sticky " : "",
2986 buf, dst_buf);
2987 }
d62a17ae 2988
036d93c0
MS
2989 ret = netlink_talk_info(netlink_talk_filter, &req.n,
2990 dplane_ctx_get_ns(ctx), 0);
2991 if (ret == 0)
2992 return ZEBRA_DPLANE_REQUEST_SUCCESS;
2993 else
2994 return ZEBRA_DPLANE_REQUEST_FAILURE;
2232a77c 2995}
2996
f17b99ed
DS
2997/*
2998 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2999 * 5549 support, re-install them.
3000 */
3001static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif,
9b036974
DS
3002 struct interface *ifp, struct ipaddr *ip,
3003 bool handle_failed)
f17b99ed
DS
3004{
3005 if (ndm->ndm_family != AF_INET)
3006 return;
3007
3008 if (!zif->v6_2_v4_ll_neigh_entry)
3009 return;
3010
3011 if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr)
3012 return;
3013
9b036974
DS
3014 if (handle_failed && ndm->ndm_state & NUD_FAILED) {
3015 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3016 ifp->name);
3017 return;
3018 }
3019
f17b99ed
DS
3020 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true);
3021}
3022
d62a17ae 3023#define NUD_VALID \
3024 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3025 | NUD_DELAY)
2232a77c 3026
2414abd3 3027static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 3028{
d62a17ae 3029 struct ndmsg *ndm;
3030 struct interface *ifp;
3031 struct zebra_if *zif;
d62a17ae 3032 struct rtattr *tb[NDA_MAX + 1];
3033 struct interface *link_if;
3034 struct ethaddr mac;
3035 struct ipaddr ip;
3036 char buf[ETHER_ADDR_STRLEN];
3037 char buf2[INET6_ADDRSTRLEN];
3038 int mac_present = 0;
a37f4598 3039 bool is_ext;
3040 bool is_router;
d62a17ae 3041
3042 ndm = NLMSG_DATA(h);
3043
3044 /* The interface should exist. */
5895d33f 3045 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 3046 ndm->ndm_ifindex);
2853fed6 3047 if (!ifp || !ifp->info)
d62a17ae 3048 return 0;
3049
20089ae2
DS
3050 zif = (struct zebra_if *)ifp->info;
3051
3052 /* Parse attributes and extract fields of interest. */
3053 memset(tb, 0, sizeof tb);
3054 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
3055
3056 if (!tb[NDA_DST]) {
9df414fe
QY
3057 zlog_debug("%s family %s IF %s(%u) - no DST",
3058 nl_msg_type_to_str(h->nlmsg_type),
3059 nl_family_to_str(ndm->ndm_family), ifp->name,
3060 ndm->ndm_ifindex);
d62a17ae 3061 return 0;
20089ae2
DS
3062 }
3063
3064 memset(&ip, 0, sizeof(struct ipaddr));
3065 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
3066 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
3067
f17b99ed
DS
3068 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3069 if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) {
9b036974 3070 netlink_handle_5549(ndm, zif, ifp, &ip, false);
28bd0652
DS
3071 if (IS_ZEBRA_DEBUG_KERNEL)
3072 zlog_debug(
3073 "\tNeighbor Entry Received is a 5549 entry, finished");
20089ae2
DS
3074 return 0;
3075 }
d62a17ae 3076
f17b99ed 3077 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
9b036974
DS
3078 if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID))
3079 netlink_handle_5549(ndm, zif, ifp, &ip, true);
f17b99ed 3080
d62a17ae 3081 /* The neighbor is present on an SVI. From this, we locate the
3082 * underlying
3083 * bridge because we're only interested in neighbors on a VxLAN bridge.
3084 * The bridge is located based on the nature of the SVI:
3085 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3086 * interface
3087 * and is linked to the bridge
3088 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3089 * inteface
3090 * itself
3091 */
3092 if (IS_ZEBRA_IF_VLAN(ifp)) {
5895d33f 3093 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
71349e03 3094 zif->link_ifindex);
d62a17ae 3095 if (!link_if)
3096 return 0;
3097 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
3098 link_if = ifp;
28bd0652
DS
3099 else {
3100 if (IS_ZEBRA_DEBUG_KERNEL)
3101 zlog_debug(
3102 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
d62a17ae 3103 return 0;
28bd0652 3104 }
d62a17ae 3105
d62a17ae 3106 memset(&mac, 0, sizeof(struct ethaddr));
d62a17ae 3107 if (h->nlmsg_type == RTM_NEWNEIGH) {
3108 if (tb[NDA_LLADDR]) {
ff8b7eb8 3109 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
3110 if (IS_ZEBRA_DEBUG_KERNEL)
3111 zlog_debug(
3112 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3113 nl_msg_type_to_str(
3114 h->nlmsg_type),
3115 nl_family_to_str(
3116 ndm->ndm_family),
3117 ifp->name, ndm->ndm_ifindex,
3118 (unsigned long)RTA_PAYLOAD(
3119 tb[NDA_LLADDR]));
d62a17ae 3120 return 0;
3121 }
3122
3123 mac_present = 1;
ff8b7eb8 3124 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 3125 }
3126
a37f4598 3127 is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED);
3128 is_router = !!(ndm->ndm_flags & NTF_ROUTER);
d62a17ae 3129
3130 if (IS_ZEBRA_DEBUG_KERNEL)
3131 zlog_debug(
3132 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3133 nl_msg_type_to_str(h->nlmsg_type),
3134 nl_family_to_str(ndm->ndm_family), ifp->name,
3135 ndm->ndm_ifindex,
3136 ipaddr2str(&ip, buf2, sizeof(buf2)),
3137 mac_present
3138 ? prefix_mac2str(&mac, buf, sizeof(buf))
3139 : "",
3140 ndm->ndm_state, ndm->ndm_flags);
3141
3142 /* If the neighbor state is valid for use, process as an add or
3143 * update
3144 * else process as a delete. Note that the delete handling may
3145 * result
3146 * in re-adding the neighbor if it is a valid "remote" neighbor.
3147 */
3148 if (ndm->ndm_state & NUD_VALID)
ee69da27 3149 return zebra_vxlan_handle_kernel_neigh_update(
d62a17ae 3150 ifp, link_if, &ip, &mac, ndm->ndm_state,
a37f4598 3151 is_ext, is_router);
d62a17ae 3152
ee69da27 3153 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
d62a17ae 3154 }
3155
3156 if (IS_ZEBRA_DEBUG_KERNEL)
3157 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3158 nl_msg_type_to_str(h->nlmsg_type),
3159 nl_family_to_str(ndm->ndm_family), ifp->name,
3160 ndm->ndm_ifindex,
3161 ipaddr2str(&ip, buf2, sizeof(buf2)));
3162
3163 /* Process the delete - it may result in re-adding the neighbor if it is
3164 * a valid "remote" neighbor.
3165 */
ee69da27 3166 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2232a77c 3167}
3168
2414abd3 3169static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 3170{
d62a17ae 3171 int len;
3172 struct ndmsg *ndm;
2232a77c 3173
d62a17ae 3174 if (h->nlmsg_type != RTM_NEWNEIGH)
3175 return 0;
2232a77c 3176
d62a17ae 3177 /* Length validity. */
3178 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3179 if (len < 0)
3180 return -1;
2232a77c 3181
d62a17ae 3182 /* We are interested only in AF_INET or AF_INET6 notifications. */
3183 ndm = NLMSG_DATA(h);
3184 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
3185 return 0;
2232a77c 3186
2414abd3 3187 return netlink_neigh_change(h, len);
2232a77c 3188}
3189
3190/* Request for IP neighbor information from the kernel */
85a75f1e
MS
3191static int netlink_request_neigh(struct nlsock *netlink_cmd, int family,
3192 int type, ifindex_t ifindex)
2232a77c 3193{
d62a17ae 3194 struct {
3195 struct nlmsghdr n;
3196 struct ndmsg ndm;
3197 char buf[256];
3198 } req;
3199
3200 /* Form the request, specifying filter (rtattr) if needed. */
3201 memset(&req, 0, sizeof(req));
3202 req.n.nlmsg_type = type;
718f9b0f 3203 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 3204 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3205 req.ndm.ndm_family = family;
3206 if (ifindex)
3207 addattr32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
3208
85a75f1e 3209 return netlink_request(netlink_cmd, &req.n);
2232a77c 3210}
3211
3212/*
3213 * IP Neighbor table read using netlink interface. This is invoked
3214 * at startup.
3215 */
d62a17ae 3216int netlink_neigh_read(struct zebra_ns *zns)
2232a77c 3217{
d62a17ae 3218 int ret;
85a75f1e
MS
3219 struct zebra_dplane_info dp_info;
3220
3221 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3222
d62a17ae 3223 /* Get IP neighbor table. */
85a75f1e
MS
3224 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
3225 0);
d62a17ae 3226 if (ret < 0)
3227 return ret;
85a75f1e
MS
3228 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3229 &dp_info, 0, 1);
2232a77c 3230
d62a17ae 3231 return ret;
2232a77c 3232}
3233
3234/*
3235 * IP Neighbor table read using netlink interface. This is for a specific
3236 * VLAN device.
3237 */
d62a17ae 3238int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 3239{
d62a17ae 3240 int ret = 0;
85a75f1e
MS
3241 struct zebra_dplane_info dp_info;
3242
3243 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3244
85a75f1e 3245 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
d62a17ae 3246 vlan_if->ifindex);
3247 if (ret < 0)
3248 return ret;
85a75f1e
MS
3249 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3250 &dp_info, 0, 0);
2232a77c 3251
d62a17ae 3252 return ret;
2232a77c 3253}
3254
67fb9374
CS
3255/*
3256 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3257 * read using netlink interface.
3258 */
3259static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns,
3260 int type, struct ipaddr *ip,
3261 ifindex_t ifindex)
3262{
3263 struct {
3264 struct nlmsghdr n;
3265 struct ndmsg ndm;
3266 char buf[256];
3267 } req;
3268 int ipa_len;
3269
3270 /* Form the request, specifying filter (rtattr) if needed. */
3271 memset(&req, 0, sizeof(req));
3272 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3273 req.n.nlmsg_flags = NLM_F_REQUEST;
3274 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3275 req.ndm.ndm_ifindex = ifindex;
3276
3277 if (IS_IPADDR_V4(ip)) {
3278 ipa_len = IPV4_MAX_BYTELEN;
3279 req.ndm.ndm_family = AF_INET;
3280
3281 } else {
3282 ipa_len = IPV6_MAX_BYTELEN;
3283 req.ndm.ndm_family = AF_INET6;
3284 }
3285
3286 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
3287
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",
3306 __PRETTY_FUNCTION__, vlan_if->name,
3307 vlan_if->ifindex,
3308 ipaddr2str(ip, buf, sizeof(buf)),
3309 vlan_if->vrf_id);
3310
3311 ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip,
3312 vlan_if->ifindex);
3313 if (ret < 0)
3314 return ret;
3315
3316 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3317 &dp_info, 1, 0);
3318
3319 return ret;
3320}
3321
2414abd3 3322int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
2232a77c 3323{
d62a17ae 3324 int len;
3325 struct ndmsg *ndm;
2232a77c 3326
d62a17ae 3327 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH))
3328 return 0;
2232a77c 3329
d62a17ae 3330 /* Length validity. */
3331 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
9bdf8618
DS
3332 if (len < 0) {
3333 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3334 __PRETTY_FUNCTION__, h->nlmsg_len,
3335 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
d62a17ae 3336 return -1;
9bdf8618 3337 }
2232a77c 3338
d62a17ae 3339 /* Is this a notification for the MAC FDB or IP neighbor table? */
3340 ndm = NLMSG_DATA(h);
3341 if (ndm->ndm_family == AF_BRIDGE)
2414abd3 3342 return netlink_macfdb_change(h, len, ns_id);
2232a77c 3343
d62a17ae 3344 if (ndm->ndm_type != RTN_UNICAST)
3345 return 0;
2232a77c 3346
d62a17ae 3347 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
2414abd3 3348 return netlink_ipneigh_change(h, len, ns_id);
8a1b681c 3349 else {
9df414fe 3350 flog_warn(
e914ccbe 3351 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
3352 "Invalid address family: %u received from kernel neighbor change: %s",
3353 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
3354 return 0;
3355 }
2232a77c 3356
d62a17ae 3357 return 0;
2232a77c 3358}
3359
931fa60c
MS
3360/*
3361 * Utility neighbor-update function, using info from dplane context.
3362 */
3363static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx *ctx,
3364 int cmd)
2232a77c 3365{
f3dbec60 3366 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 3367 struct {
3368 struct nlmsghdr n;
3369 struct ndmsg ndm;
3370 char buf[256];
3371 } req;
3372 int ipa_len;
d62a17ae 3373 char buf[INET6_ADDRSTRLEN];
3374 char buf2[ETHER_ADDR_STRLEN];
931fa60c
MS
3375 const struct ipaddr *ip;
3376 const struct ethaddr *mac;
3377 uint8_t flags;
3378 uint16_t state;
d62a17ae 3379
5605ecfc 3380 memset(&req, 0, sizeof(req));
d62a17ae 3381
931fa60c
MS
3382 ip = dplane_ctx_neigh_get_ipaddr(ctx);
3383 mac = dplane_ctx_neigh_get_mac(ctx);
3384 if (is_zero_mac(mac))
3385 mac = NULL;
3386
3387 flags = neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx));
3388 state = neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx));
3389
d62a17ae 3390 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3391 req.n.nlmsg_flags = NLM_F_REQUEST;
3392 if (cmd == RTM_NEWNEIGH)
3393 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
3394 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
3395 req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
68e33151 3396 req.ndm.ndm_state = state;
931fa60c 3397 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
d62a17ae 3398 req.ndm.ndm_type = RTN_UNICAST;
68e33151 3399 req.ndm.ndm_flags = flags;
d62a17ae 3400
f3dbec60
DS
3401 addattr_l(&req.n, sizeof(req),
3402 NDA_PROTOCOL, &protocol, sizeof(protocol));
d62a17ae 3403 ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
3404 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
3405 if (mac)
3406 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
3407
3408 if (IS_ZEBRA_DEBUG_KERNEL)
6fe2b0e6 3409 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
d62a17ae 3410 nl_msg_type_to_str(cmd),
931fa60c
MS
3411 nl_family_to_str(req.ndm.ndm_family),
3412 dplane_ctx_get_ifname(ctx),
3413 dplane_ctx_get_ifindex(ctx),
3414 ipaddr2str(ip, buf, sizeof(buf)),
d62a17ae 3415 mac ? prefix_mac2str(mac, buf2, sizeof(buf2))
931fa60c
MS
3416 : "null",
3417 flags, state);
d62a17ae 3418
931fa60c
MS
3419 return netlink_talk_info(netlink_talk_filter, &req.n,
3420 dplane_ctx_get_ns(ctx), 0);
2232a77c 3421}
3422
036d93c0
MS
3423/*
3424 * Update MAC, using dataplane context object.
3425 */
3426enum zebra_dplane_result kernel_mac_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 3427{
036d93c0 3428 return netlink_macfdb_update_ctx(ctx);
2232a77c 3429}
3430
931fa60c 3431enum zebra_dplane_result kernel_neigh_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 3432{
931fa60c 3433 int ret = -1;
2232a77c 3434
931fa60c
MS
3435 switch (dplane_ctx_get_op(ctx)) {
3436 case DPLANE_OP_NEIGH_INSTALL:
3437 case DPLANE_OP_NEIGH_UPDATE:
3438 ret = netlink_neigh_update_ctx(ctx, RTM_NEWNEIGH);
3439 break;
3440 case DPLANE_OP_NEIGH_DELETE:
3441 ret = netlink_neigh_update_ctx(ctx, RTM_DELNEIGH);
3442 break;
0bbd4ff4
MS
3443 case DPLANE_OP_VTEP_ADD:
3444 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_NEWNEIGH);
3445 break;
3446 case DPLANE_OP_VTEP_DELETE:
3447 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_DELNEIGH);
3448 break;
931fa60c
MS
3449 default:
3450 break;
3451 }
2232a77c 3452
931fa60c
MS
3453 return (ret == 0 ?
3454 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
6fe2b0e6
CS
3455}
3456
16c628de
MS
3457/*
3458 * MPLS label forwarding table change via netlink interface, using dataplane
3459 * context information.
3460 */
fc608372 3461int netlink_mpls_multipath(int cmd, struct zebra_dplane_ctx *ctx)
16c628de
MS
3462{
3463 mpls_lse_t lse;
81793ac1 3464 const zebra_nhlfe_t *nhlfe;
16c628de
MS
3465 struct nexthop *nexthop = NULL;
3466 unsigned int nexthop_num;
3467 const char *routedesc;
3468 int route_type;
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(
3555 routedesc, nhlfe,
3556 &req.n, &req.r,
3557 sizeof(req), cmd);
3558
3559 nexthop_num++;
3560 break;
3561 }
3562 }
3563 } else { /* Multipath case */
3564 char buf[NL_PKT_BUF_SIZE];
3565 struct rtattr *rta = (void *)buf;
3566 struct rtnexthop *rtnh;
81793ac1 3567 const union g_addr *src1 = NULL;
16c628de
MS
3568
3569 rta->rta_type = RTA_MULTIPATH;
3570 rta->rta_len = RTA_LENGTH(0);
3571 rtnh = RTA_DATA(rta);
3572
3573 routedesc = "multipath";
3574 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
3575 routedesc);
3576
3577 nexthop_num = 0;
3578 for (nhlfe = dplane_ctx_get_nhlfe(ctx);
3579 nhlfe; nhlfe = nhlfe->next) {
3580 nexthop = nhlfe->nexthop;
3581 if (!nexthop)
3582 continue;
3583
16c628de
MS
3584 if ((cmd == RTM_NEWROUTE
3585 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3586 && CHECK_FLAG(nexthop->flags,
3587 NEXTHOP_FLAG_ACTIVE)))
3588 || (cmd == RTM_DELROUTE
3589 && (CHECK_FLAG(nhlfe->flags,
3590 NHLFE_FLAG_INSTALLED)
3591 && CHECK_FLAG(nexthop->flags,
3592 NEXTHOP_FLAG_FIB)))) {
3593 nexthop_num++;
3594
3595 /* Build the multipath */
3596 _netlink_mpls_build_multipath(routedesc, nhlfe,
3597 rta, rtnh, &req.r,
3598 &src1);
3599 rtnh = RTNH_NEXT(rtnh);
3600 }
3601 }
3602
3603 /* Add the multipath */
3604 if (rta->rta_len > RTA_LENGTH(0))
3605 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
3606 RTA_DATA(rta), RTA_PAYLOAD(rta));
3607 }
3608
3609 /* Talk to netlink socket. */
3610 return netlink_talk_info(netlink_talk_filter, &req.n,
3611 dplane_ctx_get_ns(ctx), 0);
3612}
ddfeb486 3613#endif /* HAVE_NETLINK */