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