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