]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_netlink.c
zebra: Cleanup zebra_nhg APIs
[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
fec211ad 78static bool supports_nh;
81505946 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;
fec211ad 1977 /* TODO: Scope? */
f820d025 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:
40a2a6cd
SW
2136 case DPLANE_OP_NEIGH_INSTALL:
2137 case DPLANE_OP_NEIGH_UPDATE:
2138 case DPLANE_OP_NEIGH_DELETE:
2139 case DPLANE_OP_VTEP_ADD:
2140 case DPLANE_OP_VTEP_DELETE:
f820d025
SW
2141 case DPLANE_OP_NONE:
2142 flog_err(
2143 EC_ZEBRA_NHG_FIB_UPDATE,
2144 "Context received for kernel nexthop update with incorrect OP code (%u)",
2145 dplane_ctx_get_op(ctx));
2146 return ZEBRA_DPLANE_REQUEST_FAILURE;
f820d025
SW
2147 }
2148
2149 ret = netlink_nexthop(cmd, ctx);
2150
2151 return (ret == 0 ? ZEBRA_DPLANE_REQUEST_SUCCESS
2152 : ZEBRA_DPLANE_REQUEST_FAILURE);
2153}
2154
7cdb1a84
MS
2155/*
2156 * Update or delete a prefix from the kernel,
2157 * using info from a dataplane context.
2158 */
25779064 2159enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
2160{
2161 int cmd, ret;
2162 const struct prefix *p = dplane_ctx_get_dest(ctx);
f183e380 2163 struct nexthop *nexthop;
7cdb1a84
MS
2164
2165 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
2166 cmd = RTM_DELROUTE;
2167 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
2168 cmd = RTM_NEWROUTE;
2169 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
2170
2171 if (p->family == AF_INET || v6_rr_semantics) {
2172 /* Single 'replace' operation */
2173 cmd = RTM_NEWROUTE;
fe5f21af
DS
2174
2175 /*
2176 * With route replace semantics in place
2177 * for v4 routes and the new route is a system
2178 * route we do not install anything.
2179 * The problem here is that the new system
2180 * route should cause us to withdraw from
2181 * the kernel the old non-system route
2182 */
2183 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)) &&
2184 !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2185 (void)netlink_route_multipath(RTM_DELROUTE,
2186 ctx);
7cdb1a84
MS
2187 } else {
2188 /*
2189 * So v6 route replace semantics are not in
2190 * the kernel at this point as I understand it.
2191 * so let's do a delete then an add.
2192 * In the future once v6 route replace semantics
2193 * are in we can figure out what to do here to
2194 * allow working with old and new kernels.
2195 *
2196 * I'm also intentionally ignoring the failure case
2197 * of the route delete. If that happens yeah we're
2198 * screwed.
2199 */
3cdba47a
DS
2200 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
2201 (void)netlink_route_multipath(RTM_DELROUTE,
2202 ctx);
7cdb1a84
MS
2203 cmd = RTM_NEWROUTE;
2204 }
2205
2206 } else {
2207 return ZEBRA_DPLANE_REQUEST_FAILURE;
2208 }
2209
3cdba47a
DS
2210 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)))
2211 ret = netlink_route_multipath(cmd, ctx);
2212 else
2213 ret = 0;
f183e380
MS
2214 if ((cmd == RTM_NEWROUTE) && (ret == 0)) {
2215 /* Update installed nexthops to signal which have been
2216 * installed.
2217 */
2218 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2219 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
2220 continue;
2221
2222 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
2223 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380
MS
2224 }
2225 }
2226 }
7cdb1a84
MS
2227
2228 return (ret == 0 ?
2229 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
2230}
2231
d9f5b2f5
SW
2232/**
2233 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2234 *
2235 * @tb: Netlink RTA data
2236 * @family: Address family in the nhmsg
8c0a24c1 2237 * @ifp: Interface connected - this should be NULL, we fill it in
d9f5b2f5
SW
2238 * @ns_id: Namspace id
2239 *
2240 * Return: New nexthop
2241 */
e22e8001
SW
2242static struct nexthop netlink_nexthop_process_nh(struct rtattr **tb,
2243 unsigned char family,
2244 struct interface **ifp,
2245 ns_id_t ns_id)
d9f5b2f5 2246{
e22e8001 2247 struct nexthop nh = {};
d9f5b2f5 2248 void *gate = NULL;
8e401b25 2249 enum nexthop_types_t type = 0;
e22e8001
SW
2250 int if_index = 0;
2251 size_t sz = 0;
d9f5b2f5
SW
2252
2253 if_index = *(int *)RTA_DATA(tb[NHA_OIF]);
2254
8e401b25 2255
d9f5b2f5
SW
2256 if (tb[NHA_GATEWAY]) {
2257 switch (family) {
2258 case AF_INET:
8e401b25 2259 type = NEXTHOP_TYPE_IPV4_IFINDEX;
d9f5b2f5
SW
2260 sz = 4;
2261 break;
2262 case AF_INET6:
8e401b25 2263 type = NEXTHOP_TYPE_IPV6_IFINDEX;
d9f5b2f5
SW
2264 sz = 16;
2265 break;
2266 default:
2267 flog_warn(
2268 EC_ZEBRA_BAD_NHG_MESSAGE,
c4239c05 2269 "Nexthop gateway with bad address family (%d) received from kernel",
d9f5b2f5 2270 family);
e22e8001 2271 return nh;
d9f5b2f5
SW
2272 }
2273 gate = RTA_DATA(tb[NHA_GATEWAY]);
e22e8001 2274 } else
8e401b25 2275 type = NEXTHOP_TYPE_IFINDEX;
d9f5b2f5 2276
8e401b25 2277 if (type)
e22e8001 2278 nh.type = type;
8e401b25
SW
2279
2280 if (gate)
e22e8001 2281 memcpy(&(nh.gate), gate, sz);
8e401b25
SW
2282
2283 if (if_index)
e22e8001 2284 nh.ifindex = if_index;
8e401b25 2285
e22e8001
SW
2286 *ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), nh.ifindex);
2287 if (ifp)
2288 nh.vrf_id = (*ifp)->vrf_id;
2289 else {
d9f5b2f5
SW
2290 flog_warn(
2291 EC_ZEBRA_UNKNOWN_INTERFACE,
2292 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
e22e8001 2293 __PRETTY_FUNCTION__, nh.ifindex);
d9f5b2f5 2294
e22e8001 2295 nh.vrf_id = VRF_DEFAULT;
d9f5b2f5
SW
2296 }
2297
2298 if (tb[NHA_ENCAP] && tb[NHA_ENCAP_TYPE]) {
2299 uint16_t encap_type = *(uint16_t *)RTA_DATA(tb[NHA_ENCAP_TYPE]);
2300 int num_labels = 0;
6e728764 2301
d9f5b2f5
SW
2302 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
2303
e22e8001 2304 if (encap_type == LWTUNNEL_ENCAP_MPLS)
d9f5b2f5 2305 num_labels = parse_encap_mpls(tb[NHA_ENCAP], labels);
d9f5b2f5 2306
e22e8001
SW
2307 if (num_labels)
2308 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels,
d9f5b2f5 2309 labels);
d9f5b2f5
SW
2310 }
2311
2312 return nh;
2313}
2314
85f5e761 2315static int netlink_nexthop_process_group(struct rtattr **tb,
5a935f79 2316 struct nh_grp *z_grp, int z_grp_size)
d9f5b2f5 2317{
e22e8001
SW
2318 uint8_t count = 0;
2319 /* linux/nexthop.h group struct */
d9f5b2f5
SW
2320 struct nexthop_grp *n_grp = NULL;
2321
85f5e761 2322 n_grp = (struct nexthop_grp *)RTA_DATA(tb[NHA_GROUP]);
d9f5b2f5
SW
2323 count = (RTA_PAYLOAD(tb[NHA_GROUP]) / sizeof(*n_grp));
2324
2325 if (!count || (count * sizeof(*n_grp)) != RTA_PAYLOAD(tb[NHA_GROUP])) {
2326 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE,
2327 "Invalid nexthop group received from the kernel");
85f5e761 2328 return count;
d9f5b2f5
SW
2329 }
2330
38e40db1 2331#if 0
d9f5b2f5 2332 // TODO: Need type for something?
85f5e761
SW
2333 zlog_debug("Nexthop group type: %d",
2334 *((uint16_t *)RTA_DATA(tb[NHA_GROUP_TYPE])));
d9f5b2f5 2335
38e40db1 2336#endif
d9f5b2f5 2337
5a935f79 2338 for (int i = 0; ((i < count) && (i < z_grp_size)); i++) {
e22e8001
SW
2339 z_grp[i].id = n_grp[i].id;
2340 z_grp[i].weight = n_grp[i].weight;
85f5e761 2341 }
d9f5b2f5
SW
2342 return count;
2343}
2344
2345/**
2346 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2347 *
2348 * @h: Netlink message header
2349 * @ns_id: Namspace id
2350 * @startup: Are we reading under startup conditions?
2351 *
2352 * Return: Result status
2353 */
2354int netlink_nexthop_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2355{
2356 int len;
2357 /* nexthop group id */
2358 uint32_t id;
2359 unsigned char family;
38e40db1 2360 int type;
e8b0e420 2361 afi_t afi = AFI_UNSPEC;
bbb3940e 2362 vrf_id_t vrf_id = 0;
8c0a24c1 2363 struct interface *ifp = NULL;
d9f5b2f5 2364 struct nhmsg *nhm = NULL;
e22e8001
SW
2365 struct nexthop nh = {};
2366 struct nh_grp grp[MULTIPATH_NUM] = {};
85f5e761 2367 /* Count of nexthops in group array */
e22e8001 2368 uint8_t grp_count = 0;
e22e8001 2369 struct rtattr *tb[NHA_MAX + 1] = {};
d9f5b2f5 2370
d9f5b2f5
SW
2371 nhm = NLMSG_DATA(h);
2372
2373 if (startup && h->nlmsg_type != RTM_NEWNEXTHOP)
2374 return 0;
2375
2376 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct nhmsg));
2377 if (len < 0) {
2378 zlog_warn(
2379 "%s: Message received from netlink is of a broken size %d %zu",
2380 __PRETTY_FUNCTION__, h->nlmsg_len,
2381 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg)));
2382 return -1;
2383 }
2384
d9f5b2f5
SW
2385 netlink_parse_rtattr(tb, NHA_MAX, RTM_NHA(nhm), len);
2386
2387
2388 if (!tb[NHA_ID]) {
2389 flog_warn(
2390 EC_ZEBRA_BAD_NHG_MESSAGE,
2391 "Nexthop group without an ID received from the kernel");
2392 return -1;
2393 }
2394
2395 /* We use the ID key'd nhg table for kernel updates */
2396 id = *((uint32_t *)RTA_DATA(tb[NHA_ID]));
d9f5b2f5 2397
e8b0e420 2398 family = nhm->nh_family;
e8b0e420
SW
2399 afi = family2afi(family);
2400
38e40db1
SW
2401 type = proto2zebra(nhm->nh_protocol, 0, true);
2402
fdee485a
SW
2403 if (IS_ZEBRA_DEBUG_KERNEL)
2404 zlog_debug("%s ID (%u) %s NS %u",
2405 nl_msg_type_to_str(h->nlmsg_type), id,
2406 nl_family_to_str(family), ns_id);
2407
2408
d9f5b2f5
SW
2409 if (h->nlmsg_type == RTM_NEWNEXTHOP) {
2410 if (tb[NHA_GROUP]) {
2411 /**
2412 * If this is a group message its only going to have
2413 * an array of nexthop IDs associated with it
2414 */
5a935f79
SW
2415 grp_count = netlink_nexthop_process_group(
2416 tb, grp, array_size(grp));
85f5e761
SW
2417 } else {
2418 if (tb[NHA_BLACKHOLE]) {
2419 /**
2420 * This nexthop is just for blackhole-ing
2421 * traffic, it should not have an OIF, GATEWAY,
2422 * or ENCAP
2423 */
e22e8001
SW
2424 nh.type = NEXTHOP_TYPE_BLACKHOLE;
2425 nh.bh_type = BLACKHOLE_UNSPEC;
2426 } else if (tb[NHA_OIF])
85f5e761
SW
2427 /**
2428 * This is a true new nexthop, so we need
2429 * to parse the gateway and device info
2430 */
2431 nh = netlink_nexthop_process_nh(tb, family,
2432 &ifp, ns_id);
e22e8001
SW
2433 else {
2434
8e401b25
SW
2435 flog_warn(
2436 EC_ZEBRA_BAD_NHG_MESSAGE,
2437 "Invalid Nexthop message received from the kernel with ID (%u)",
2438 id);
2439 return -1;
2440 }
e22e8001
SW
2441 SET_FLAG(nh.flags, NEXTHOP_FLAG_ACTIVE);
2442 if (nhm->nh_flags & RTNH_F_ONLINK)
2443 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
2444 vrf_id = nh.vrf_id;
d9f5b2f5
SW
2445 }
2446
38e40db1
SW
2447 if (zebra_nhg_kernel_find(id, &nh, grp, grp_count, vrf_id, afi,
2448 type, startup))
e22e8001 2449 return -1;
8e401b25 2450
9a1588c4
SW
2451 } else if (h->nlmsg_type == RTM_DELNEXTHOP)
2452 zebra_nhg_kernel_del(id);
d9f5b2f5 2453
d9f5b2f5
SW
2454 return 0;
2455}
2456
7d5bb02b 2457#if 0 /* Force off kernel nexthop group installs for now */
d9f5b2f5
SW
2458/**
2459 * netlink_request_nexthop() - Request nextop information from the kernel
2460 * @zns: Zebra namespace
2461 * @family: AF_* netlink family
2462 * @type: RTM_* route type
2463 *
2464 * Return: Result status
2465 */
2466static int netlink_request_nexthop(struct zebra_ns *zns, int family, int type)
2467{
2468 struct {
2469 struct nlmsghdr n;
2470 struct nhmsg nhm;
2471 } req;
2472
2473 /* Form the request, specifying filter (rtattr) if needed. */
2474 memset(&req, 0, sizeof(req));
2475 req.n.nlmsg_type = type;
2476 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2477 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg));
2478 req.nhm.nh_family = family;
2479
2480 return netlink_request(&zns->netlink_cmd, &req.n);
2481}
2482
7d5bb02b 2483
d9f5b2f5
SW
2484/**
2485 * netlink_nexthop_read() - Nexthop read function using netlink interface
2486 *
2487 * @zns: Zebra name space
2488 *
2489 * Return: Result status
2490 * Only called at bootstrap time.
2491 */
2492int netlink_nexthop_read(struct zebra_ns *zns)
2493{
2494 int ret;
2495 struct zebra_dplane_info dp_info;
2496
2497 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2498
2499 /* Get nexthop objects */
2500 ret = netlink_request_nexthop(zns, AF_UNSPEC, RTM_GETNEXTHOP);
2501 if (ret < 0)
2502 return ret;
2503 ret = netlink_parse_info(netlink_nexthop_change, &zns->netlink_cmd,
2504 &dp_info, 0, 1);
81505946
SW
2505
2506 if (!ret)
2507 /* If we succesfully read in nexthop objects,
2508 * this kernel must support them.
2509 */
2510 supports_nh = true;
2511 else if (IS_ZEBRA_DEBUG_KERNEL)
2512 zlog_debug("Nexthop objects not supported on this kernel");
2513
60e0eaee 2514 return ret;
d9f5b2f5 2515}
7d5bb02b
SW
2516#else
2517int netlink_nexthop_read(struct zebra_ns *zns)
2518{
2519 return 0;
2520}
2521#endif
d9f5b2f5
SW
2522
2523
d62a17ae 2524int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 2525 int llalen, ns_id_t ns_id)
6b8a5694 2526{
d62a17ae 2527 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
5895d33f 2528 addr, lla, llalen, ns_id);
6b8a5694 2529}
718e3744 2530
13d60d35 2531/*
2532 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2533 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2534 */
0bbd4ff4
MS
2535static int netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx *ctx,
2536 int cmd)
13d60d35 2537{
f3dbec60 2538 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 2539 struct {
2540 struct nlmsghdr n;
2541 struct ndmsg ndm;
2542 char buf[256];
2543 } req;
d7c0a89a 2544 uint8_t dst_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
0bbd4ff4 2545 const struct ipaddr *addr;
d62a17ae 2546
5605ecfc 2547 memset(&req, 0, sizeof(req));
d62a17ae 2548
2549 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2550 req.n.nlmsg_flags = NLM_F_REQUEST;
2551 if (cmd == RTM_NEWNEIGH)
2552 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_APPEND);
2553 req.n.nlmsg_type = cmd;
2554 req.ndm.ndm_family = PF_BRIDGE;
2555 req.ndm.ndm_state = NUD_NOARP | NUD_PERMANENT;
fec211ad 2556 req.ndm.ndm_flags |= NTF_SELF; /* Handle by "self", not "master" */
d62a17ae 2557
2558
f3dbec60
DS
2559 addattr_l(&req.n, sizeof(req),
2560 NDA_PROTOCOL, &protocol, sizeof(protocol));
d62a17ae 2561 addattr_l(&req.n, sizeof(req), NDA_LLADDR, &dst_mac, 6);
0bbd4ff4 2562 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
13d60d35 2563
0bbd4ff4 2564 addr = dplane_ctx_neigh_get_ipaddr(ctx);
13d60d35 2565
0bbd4ff4 2566 addattr_l(&req.n, sizeof(req), NDA_DST, &(addr->ipaddr_v4), 4);
13d60d35 2567
0bbd4ff4
MS
2568 return netlink_talk_info(netlink_talk_filter, &req.n,
2569 dplane_ctx_get_ns(ctx), 0);
13d60d35 2570}
2571
2232a77c 2572#ifndef NDA_RTA
d62a17ae 2573#define NDA_RTA(r) \
2574 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2232a77c 2575#endif
2576
2414abd3 2577static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 2578{
d62a17ae 2579 struct ndmsg *ndm;
2580 struct interface *ifp;
2581 struct zebra_if *zif;
d62a17ae 2582 struct rtattr *tb[NDA_MAX + 1];
2583 struct interface *br_if;
2584 struct ethaddr mac;
2585 vlanid_t vid = 0;
2586 struct prefix vtep_ip;
2587 int vid_present = 0, dst_present = 0;
2588 char buf[ETHER_ADDR_STRLEN];
2589 char vid_buf[20];
2590 char dst_buf[30];
a37f4598 2591 bool sticky;
d62a17ae 2592
2593 ndm = NLMSG_DATA(h);
2594
2853fed6 2595 /* We only process macfdb notifications if EVPN is enabled */
2596 if (!is_evpn_enabled())
2597 return 0;
2598
d62a17ae 2599 /* The interface should exist. */
5895d33f 2600 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 2601 ndm->ndm_ifindex);
28bd0652
DS
2602 if (!ifp || !ifp->info) {
2603 if (IS_ZEBRA_DEBUG_KERNEL)
2604 zlog_debug("\t%s without associated interface: %u",
2605 __PRETTY_FUNCTION__, ndm->ndm_ifindex);
d62a17ae 2606 return 0;
28bd0652 2607 }
d62a17ae 2608
2609 /* The interface should be something we're interested in. */
28bd0652
DS
2610 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
2611 if (IS_ZEBRA_DEBUG_KERNEL)
2612 zlog_debug("\t%s Not interested in %s, not a slave",
2613 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 2614 return 0;
28bd0652 2615 }
d62a17ae 2616
2617 /* Drop "permanent" entries. */
28bd0652
DS
2618 if (ndm->ndm_state & NUD_PERMANENT) {
2619 if (IS_ZEBRA_DEBUG_KERNEL)
2620 zlog_debug("\t%s Entry is PERMANENT, dropping",
2621 __PRETTY_FUNCTION__);
d62a17ae 2622 return 0;
28bd0652 2623 }
d62a17ae 2624
2625 zif = (struct zebra_if *)ifp->info;
2626 if ((br_if = zif->brslave_info.br_if) == NULL) {
28bd0652
DS
2627 if (IS_ZEBRA_DEBUG_KERNEL)
2628 zlog_debug(
2629 "%s family %s IF %s(%u) brIF %u - no bridge master",
2630 nl_msg_type_to_str(h->nlmsg_type),
2631 nl_family_to_str(ndm->ndm_family), ifp->name,
2632 ndm->ndm_ifindex,
2633 zif->brslave_info.bridge_ifindex);
d62a17ae 2634 return 0;
2635 }
2636
2637 /* Parse attributes and extract fields of interest. */
2638 memset(tb, 0, sizeof tb);
2639 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
2640
2641 if (!tb[NDA_LLADDR]) {
28bd0652
DS
2642 if (IS_ZEBRA_DEBUG_KERNEL)
2643 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2644 nl_msg_type_to_str(h->nlmsg_type),
2645 nl_family_to_str(ndm->ndm_family), ifp->name,
2646 ndm->ndm_ifindex,
2647 zif->brslave_info.bridge_ifindex);
d62a17ae 2648 return 0;
2649 }
2650
ff8b7eb8 2651 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
2652 if (IS_ZEBRA_DEBUG_KERNEL)
2653 zlog_debug(
2654 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2655 nl_msg_type_to_str(h->nlmsg_type),
2656 nl_family_to_str(ndm->ndm_family), ifp->name,
2657 ndm->ndm_ifindex,
2658 zif->brslave_info.bridge_ifindex,
2659 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
d62a17ae 2660 return 0;
2661 }
2662
ff8b7eb8 2663 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 2664
2665 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
2666 vid_present = 1;
d7c0a89a 2667 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
d62a17ae 2668 sprintf(vid_buf, " VLAN %u", vid);
2669 }
2670
2671 if (tb[NDA_DST]) {
2672 /* TODO: Only IPv4 supported now. */
2673 dst_present = 1;
2674 vtep_ip.family = AF_INET;
2675 vtep_ip.prefixlen = IPV4_MAX_BITLEN;
2676 memcpy(&(vtep_ip.u.prefix4.s_addr), RTA_DATA(tb[NDA_DST]),
2677 IPV4_MAX_BYTELEN);
2678 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip.u.prefix4));
2679 }
2680
a37f4598 2681 sticky = !!(ndm->ndm_state & NUD_NOARP);
d62a17ae 2682
2683 if (IS_ZEBRA_DEBUG_KERNEL)
2684 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2685 nl_msg_type_to_str(h->nlmsg_type),
2686 nl_family_to_str(ndm->ndm_family), ifp->name,
2687 ndm->ndm_ifindex, vid_present ? vid_buf : "",
2688 sticky ? "sticky " : "",
2689 prefix_mac2str(&mac, buf, sizeof(buf)),
2690 dst_present ? dst_buf : "");
2691
28bd0652
DS
2692 if (filter_vlan && vid != filter_vlan) {
2693 if (IS_ZEBRA_DEBUG_KERNEL)
2694 zlog_debug("\tFiltered due to filter vlan: %d",
2695 filter_vlan);
d62a17ae 2696 return 0;
28bd0652 2697 }
d62a17ae 2698
2699 /* If add or update, do accordingly if learnt on a "local" interface; if
2700 * the notification is over VxLAN, this has to be related to
2701 * multi-homing,
2702 * so perform an implicit delete of any local entry (if it exists).
2703 */
2704 if (h->nlmsg_type == RTM_NEWNEIGH) {
d62a17ae 2705 if (IS_ZEBRA_IF_VXLAN(ifp))
2706 return zebra_vxlan_check_del_local_mac(ifp, br_if, &mac,
2707 vid);
2708
2709 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
2710 sticky);
2711 }
2712
2713 /* This is a delete notification.
2714 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2715 * 2. For a MAC over "local" interface, delete the mac
2716 * Note: We will get notifications from both bridge driver and VxLAN
2717 * driver.
2718 * Ignore the notification from VxLan driver as it is also generated
2719 * when mac moves from remote to local.
2720 */
28bd0652
DS
2721 if (dst_present) {
2722 if (IS_ZEBRA_DEBUG_KERNEL)
2723 zlog_debug("\tNo Destination Present");
d62a17ae 2724 return 0;
28bd0652 2725 }
d62a17ae 2726
2727 if (IS_ZEBRA_IF_VXLAN(ifp))
2728 return zebra_vxlan_check_readd_remote_mac(ifp, br_if, &mac,
2729 vid);
2730
2731 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
2232a77c 2732}
2733
2414abd3 2734static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 2735{
d62a17ae 2736 int len;
2737 struct ndmsg *ndm;
2232a77c 2738
d62a17ae 2739 if (h->nlmsg_type != RTM_NEWNEIGH)
2740 return 0;
2232a77c 2741
d62a17ae 2742 /* Length validity. */
2743 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2744 if (len < 0)
2745 return -1;
2232a77c 2746
d62a17ae 2747 /* We are interested only in AF_BRIDGE notifications. */
2748 ndm = NLMSG_DATA(h);
2749 if (ndm->ndm_family != AF_BRIDGE)
2750 return 0;
2232a77c 2751
2414abd3 2752 return netlink_macfdb_change(h, len, ns_id);
2232a77c 2753}
2754
2755/* Request for MAC FDB information from the kernel */
85a75f1e
MS
2756static int netlink_request_macs(struct nlsock *netlink_cmd, int family,
2757 int type, ifindex_t master_ifindex)
2232a77c 2758{
d62a17ae 2759 struct {
2760 struct nlmsghdr n;
2761 struct ifinfomsg ifm;
2762 char buf[256];
2763 } req;
2764
2765 /* Form the request, specifying filter (rtattr) if needed. */
2766 memset(&req, 0, sizeof(req));
2767 req.n.nlmsg_type = type;
718f9b0f 2768 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 2769 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2770 req.ifm.ifi_family = family;
2771 if (master_ifindex)
2772 addattr32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
2773
85a75f1e 2774 return netlink_request(netlink_cmd, &req.n);
2232a77c 2775}
2776
2777/*
2778 * MAC forwarding database read using netlink interface. This is invoked
2779 * at startup.
2780 */
d62a17ae 2781int netlink_macfdb_read(struct zebra_ns *zns)
2232a77c 2782{
d62a17ae 2783 int ret;
85a75f1e
MS
2784 struct zebra_dplane_info dp_info;
2785
2786 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2787
2788 /* Get bridge FDB table. */
85a75f1e
MS
2789 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
2790 0);
d62a17ae 2791 if (ret < 0)
2792 return ret;
2793 /* We are reading entire table. */
2794 filter_vlan = 0;
85a75f1e
MS
2795 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2796 &dp_info, 0, 1);
d62a17ae 2797
2798 return ret;
2232a77c 2799}
2800
2801/*
2802 * MAC forwarding database read using netlink interface. This is for a
2803 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2804 */
d62a17ae 2805int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
2806 struct interface *br_if)
2232a77c 2807{
d62a17ae 2808 struct zebra_if *br_zif;
2809 struct zebra_if *zif;
2810 struct zebra_l2info_vxlan *vxl;
85a75f1e 2811 struct zebra_dplane_info dp_info;
d62a17ae 2812 int ret = 0;
2813
85a75f1e 2814 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2815
2816 /* Save VLAN we're filtering on, if needed. */
2817 br_zif = (struct zebra_if *)br_if->info;
2818 zif = (struct zebra_if *)ifp->info;
2819 vxl = &zif->l2info.vxl;
2820 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
2821 filter_vlan = vxl->access_vlan;
2822
2823 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2824 */
85a75f1e 2825 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
d62a17ae 2826 br_if->ifindex);
2827 if (ret < 0)
2828 return ret;
85a75f1e
MS
2829 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2830 &dp_info, 0, 0);
d62a17ae 2831
2832 /* Reset VLAN filter. */
2833 filter_vlan = 0;
2834 return ret;
2232a77c 2835}
2836
67fb9374
CS
2837
2838/* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2839static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns,
2840 int family,
2841 int type,
2842 struct interface *br_if,
2843 struct ethaddr *mac,
2844 vlanid_t vid)
2845{
2846 struct {
2847 struct nlmsghdr n;
2848 struct ndmsg ndm;
2849 char buf[256];
2850 } req;
2851 struct zebra_if *br_zif;
2852 char buf[ETHER_ADDR_STRLEN];
2853
2854 memset(&req, 0, sizeof(req));
2855 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2856 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
2857 req.n.nlmsg_flags = NLM_F_REQUEST;
2858 req.ndm.ndm_family = family; /* AF_BRIDGE */
2859 /* req.ndm.ndm_state = NUD_REACHABLE; */
2860
2861 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2862
2863 br_zif = (struct zebra_if *)br_if->info;
2864 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0)
2865 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2866
2867 addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
2868
2869 if (IS_ZEBRA_DEBUG_KERNEL)
2870 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2871 __PRETTY_FUNCTION__,
2872 nl_family_to_str(req.ndm.ndm_family), br_if->name,
2873 br_if->ifindex,
2874 prefix_mac2str(mac, buf, sizeof(buf)), vid);
2875
2876 return netlink_request(&zns->netlink_cmd, &req.n);
2877}
2878
2879int netlink_macfdb_read_specific_mac(struct zebra_ns *zns,
2880 struct interface *br_if,
2881 struct ethaddr *mac, vlanid_t vid)
2882{
2883 int ret = 0;
2884 struct zebra_dplane_info dp_info;
2885
2886 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2887
2888 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2889 */
2890 ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE,
2891 RTM_GETNEIGH,
2892 br_if, mac, vid);
2893 if (ret < 0)
2894 return ret;
2895
2896 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2897 &dp_info, 1, 0);
2898
2899 return ret;
2900}
036d93c0
MS
2901
2902/*
2903 * Netlink-specific handler for MAC updates using dataplane context object.
2904 */
2905static enum zebra_dplane_result
2906netlink_macfdb_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 2907{
f3dbec60 2908 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 2909 struct {
2910 struct nlmsghdr n;
2911 struct ndmsg ndm;
2912 char buf[256];
2913 } req;
036d93c0 2914 int ret;
d62a17ae 2915 int dst_alen;
36590076 2916 int vid_present = 0;
036d93c0
MS
2917 int cmd;
2918 struct in_addr vtep_ip;
2919 vlanid_t vid;
2920
2921 if (dplane_ctx_get_op(ctx) == DPLANE_OP_MAC_INSTALL)
2922 cmd = RTM_NEWNEIGH;
2923 else
2924 cmd = RTM_DELNEIGH;
2925
5605ecfc 2926 memset(&req, 0, sizeof(req));
d62a17ae 2927
2928 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2929 req.n.nlmsg_flags = NLM_F_REQUEST;
2930 if (cmd == RTM_NEWNEIGH)
2931 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2932 req.n.nlmsg_type = cmd;
2933 req.ndm.ndm_family = AF_BRIDGE;
2934 req.ndm.ndm_flags |= NTF_SELF | NTF_MASTER;
2935 req.ndm.ndm_state = NUD_REACHABLE;
2936
036d93c0 2937 if (dplane_ctx_mac_is_sticky(ctx))
d62a17ae 2938 req.ndm.ndm_state |= NUD_NOARP;
2939 else
2940 req.ndm.ndm_flags |= NTF_EXT_LEARNED;
2941
f3dbec60
DS
2942 addattr_l(&req.n, sizeof(req),
2943 NDA_PROTOCOL, &protocol, sizeof(protocol));
036d93c0
MS
2944 addattr_l(&req.n, sizeof(req), NDA_LLADDR,
2945 dplane_ctx_mac_get_addr(ctx), 6);
478566d6 2946 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
036d93c0 2947
d63c1b18 2948 dst_alen = 4; // TODO: hardcoded
036d93c0 2949 vtep_ip = *(dplane_ctx_mac_get_vtep_ip(ctx));
d63c1b18 2950 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip, dst_alen);
036d93c0 2951
478566d6
MS
2952 vid = dplane_ctx_mac_get_vlan(ctx);
2953
2954 if (vid > 0) {
d62a17ae 2955 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2956 vid_present = 1;
d62a17ae 2957 }
478566d6
MS
2958 addattr32(&req.n, sizeof(req), NDA_MASTER,
2959 dplane_ctx_mac_get_br_ifindex(ctx));
d62a17ae 2960
036d93c0
MS
2961 if (IS_ZEBRA_DEBUG_KERNEL) {
2962 char ipbuf[PREFIX_STRLEN];
2963 char buf[ETHER_ADDR_STRLEN];
2964 char dst_buf[PREFIX_STRLEN + 10];
478566d6
MS
2965 char vid_buf[20];
2966
2967 if (vid_present)
2968 snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid);
2969 else
2970 vid_buf[0] = '\0';
036d93c0
MS
2971
2972 inet_ntop(AF_INET, &vtep_ip, ipbuf, sizeof(ipbuf));
2973 snprintf(dst_buf, sizeof(dst_buf), " dst %s", ipbuf);
2974 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf, sizeof(buf));
2975
d62a17ae 2976 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2977 nl_msg_type_to_str(cmd),
478566d6
MS
2978 nl_family_to_str(req.ndm.ndm_family),
2979 dplane_ctx_get_ifname(ctx),
2980 dplane_ctx_get_ifindex(ctx), vid_buf,
036d93c0
MS
2981 dplane_ctx_mac_is_sticky(ctx) ? "sticky " : "",
2982 buf, dst_buf);
2983 }
d62a17ae 2984
036d93c0
MS
2985 ret = netlink_talk_info(netlink_talk_filter, &req.n,
2986 dplane_ctx_get_ns(ctx), 0);
2987 if (ret == 0)
2988 return ZEBRA_DPLANE_REQUEST_SUCCESS;
2989 else
2990 return ZEBRA_DPLANE_REQUEST_FAILURE;
2232a77c 2991}
2992
f17b99ed
DS
2993/*
2994 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2995 * 5549 support, re-install them.
2996 */
2997static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif,
9b036974
DS
2998 struct interface *ifp, struct ipaddr *ip,
2999 bool handle_failed)
f17b99ed
DS
3000{
3001 if (ndm->ndm_family != AF_INET)
3002 return;
3003
3004 if (!zif->v6_2_v4_ll_neigh_entry)
3005 return;
3006
3007 if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr)
3008 return;
3009
9b036974
DS
3010 if (handle_failed && ndm->ndm_state & NUD_FAILED) {
3011 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3012 ifp->name);
3013 return;
3014 }
3015
f17b99ed
DS
3016 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true);
3017}
3018
d62a17ae 3019#define NUD_VALID \
3020 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3021 | NUD_DELAY)
2232a77c 3022
2414abd3 3023static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 3024{
d62a17ae 3025 struct ndmsg *ndm;
3026 struct interface *ifp;
3027 struct zebra_if *zif;
d62a17ae 3028 struct rtattr *tb[NDA_MAX + 1];
3029 struct interface *link_if;
3030 struct ethaddr mac;
3031 struct ipaddr ip;
3032 char buf[ETHER_ADDR_STRLEN];
3033 char buf2[INET6_ADDRSTRLEN];
3034 int mac_present = 0;
a37f4598 3035 bool is_ext;
3036 bool is_router;
d62a17ae 3037
3038 ndm = NLMSG_DATA(h);
3039
3040 /* The interface should exist. */
5895d33f 3041 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 3042 ndm->ndm_ifindex);
2853fed6 3043 if (!ifp || !ifp->info)
d62a17ae 3044 return 0;
3045
20089ae2
DS
3046 zif = (struct zebra_if *)ifp->info;
3047
3048 /* Parse attributes and extract fields of interest. */
3049 memset(tb, 0, sizeof tb);
3050 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
3051
3052 if (!tb[NDA_DST]) {
9df414fe
QY
3053 zlog_debug("%s family %s IF %s(%u) - no DST",
3054 nl_msg_type_to_str(h->nlmsg_type),
3055 nl_family_to_str(ndm->ndm_family), ifp->name,
3056 ndm->ndm_ifindex);
d62a17ae 3057 return 0;
20089ae2
DS
3058 }
3059
3060 memset(&ip, 0, sizeof(struct ipaddr));
3061 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
3062 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
3063
f17b99ed
DS
3064 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3065 if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) {
9b036974 3066 netlink_handle_5549(ndm, zif, ifp, &ip, false);
28bd0652
DS
3067 if (IS_ZEBRA_DEBUG_KERNEL)
3068 zlog_debug(
3069 "\tNeighbor Entry Received is a 5549 entry, finished");
20089ae2
DS
3070 return 0;
3071 }
d62a17ae 3072
f17b99ed 3073 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
9b036974
DS
3074 if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID))
3075 netlink_handle_5549(ndm, zif, ifp, &ip, true);
f17b99ed 3076
d62a17ae 3077 /* The neighbor is present on an SVI. From this, we locate the
3078 * underlying
3079 * bridge because we're only interested in neighbors on a VxLAN bridge.
3080 * The bridge is located based on the nature of the SVI:
3081 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3082 * interface
3083 * and is linked to the bridge
3084 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3085 * inteface
3086 * itself
3087 */
3088 if (IS_ZEBRA_IF_VLAN(ifp)) {
5895d33f 3089 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
71349e03 3090 zif->link_ifindex);
d62a17ae 3091 if (!link_if)
3092 return 0;
3093 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
3094 link_if = ifp;
28bd0652
DS
3095 else {
3096 if (IS_ZEBRA_DEBUG_KERNEL)
3097 zlog_debug(
3098 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
d62a17ae 3099 return 0;
28bd0652 3100 }
d62a17ae 3101
d62a17ae 3102 memset(&mac, 0, sizeof(struct ethaddr));
d62a17ae 3103 if (h->nlmsg_type == RTM_NEWNEIGH) {
3104 if (tb[NDA_LLADDR]) {
ff8b7eb8 3105 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
3106 if (IS_ZEBRA_DEBUG_KERNEL)
3107 zlog_debug(
3108 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
3109 nl_msg_type_to_str(
3110 h->nlmsg_type),
3111 nl_family_to_str(
3112 ndm->ndm_family),
3113 ifp->name, ndm->ndm_ifindex,
3114 (unsigned long)RTA_PAYLOAD(
3115 tb[NDA_LLADDR]));
d62a17ae 3116 return 0;
3117 }
3118
3119 mac_present = 1;
ff8b7eb8 3120 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 3121 }
3122
a37f4598 3123 is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED);
3124 is_router = !!(ndm->ndm_flags & NTF_ROUTER);
d62a17ae 3125
3126 if (IS_ZEBRA_DEBUG_KERNEL)
3127 zlog_debug(
3128 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3129 nl_msg_type_to_str(h->nlmsg_type),
3130 nl_family_to_str(ndm->ndm_family), ifp->name,
3131 ndm->ndm_ifindex,
3132 ipaddr2str(&ip, buf2, sizeof(buf2)),
3133 mac_present
3134 ? prefix_mac2str(&mac, buf, sizeof(buf))
3135 : "",
3136 ndm->ndm_state, ndm->ndm_flags);
3137
3138 /* If the neighbor state is valid for use, process as an add or
3139 * update
3140 * else process as a delete. Note that the delete handling may
3141 * result
3142 * in re-adding the neighbor if it is a valid "remote" neighbor.
3143 */
3144 if (ndm->ndm_state & NUD_VALID)
ee69da27 3145 return zebra_vxlan_handle_kernel_neigh_update(
d62a17ae 3146 ifp, link_if, &ip, &mac, ndm->ndm_state,
a37f4598 3147 is_ext, is_router);
d62a17ae 3148
ee69da27 3149 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
d62a17ae 3150 }
3151
3152 if (IS_ZEBRA_DEBUG_KERNEL)
3153 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
3154 nl_msg_type_to_str(h->nlmsg_type),
3155 nl_family_to_str(ndm->ndm_family), ifp->name,
3156 ndm->ndm_ifindex,
3157 ipaddr2str(&ip, buf2, sizeof(buf2)));
3158
3159 /* Process the delete - it may result in re-adding the neighbor if it is
3160 * a valid "remote" neighbor.
3161 */
ee69da27 3162 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2232a77c 3163}
3164
2414abd3 3165static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 3166{
d62a17ae 3167 int len;
3168 struct ndmsg *ndm;
2232a77c 3169
d62a17ae 3170 if (h->nlmsg_type != RTM_NEWNEIGH)
3171 return 0;
2232a77c 3172
d62a17ae 3173 /* Length validity. */
3174 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
3175 if (len < 0)
3176 return -1;
2232a77c 3177
d62a17ae 3178 /* We are interested only in AF_INET or AF_INET6 notifications. */
3179 ndm = NLMSG_DATA(h);
3180 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
3181 return 0;
2232a77c 3182
2414abd3 3183 return netlink_neigh_change(h, len);
2232a77c 3184}
3185
3186/* Request for IP neighbor information from the kernel */
85a75f1e
MS
3187static int netlink_request_neigh(struct nlsock *netlink_cmd, int family,
3188 int type, ifindex_t ifindex)
2232a77c 3189{
d62a17ae 3190 struct {
3191 struct nlmsghdr n;
3192 struct ndmsg ndm;
3193 char buf[256];
3194 } req;
3195
3196 /* Form the request, specifying filter (rtattr) if needed. */
3197 memset(&req, 0, sizeof(req));
3198 req.n.nlmsg_type = type;
718f9b0f 3199 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 3200 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3201 req.ndm.ndm_family = family;
3202 if (ifindex)
3203 addattr32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
3204
85a75f1e 3205 return netlink_request(netlink_cmd, &req.n);
2232a77c 3206}
3207
3208/*
3209 * IP Neighbor table read using netlink interface. This is invoked
3210 * at startup.
3211 */
d62a17ae 3212int netlink_neigh_read(struct zebra_ns *zns)
2232a77c 3213{
d62a17ae 3214 int ret;
85a75f1e
MS
3215 struct zebra_dplane_info dp_info;
3216
3217 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3218
d62a17ae 3219 /* Get IP neighbor table. */
85a75f1e
MS
3220 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
3221 0);
d62a17ae 3222 if (ret < 0)
3223 return ret;
85a75f1e
MS
3224 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3225 &dp_info, 0, 1);
2232a77c 3226
d62a17ae 3227 return ret;
2232a77c 3228}
3229
3230/*
3231 * IP Neighbor table read using netlink interface. This is for a specific
3232 * VLAN device.
3233 */
d62a17ae 3234int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 3235{
d62a17ae 3236 int ret = 0;
85a75f1e
MS
3237 struct zebra_dplane_info dp_info;
3238
3239 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 3240
85a75f1e 3241 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
d62a17ae 3242 vlan_if->ifindex);
3243 if (ret < 0)
3244 return ret;
85a75f1e
MS
3245 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3246 &dp_info, 0, 0);
2232a77c 3247
d62a17ae 3248 return ret;
2232a77c 3249}
3250
67fb9374
CS
3251/*
3252 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3253 * read using netlink interface.
3254 */
3255static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns,
3256 int type, struct ipaddr *ip,
3257 ifindex_t ifindex)
3258{
3259 struct {
3260 struct nlmsghdr n;
3261 struct ndmsg ndm;
3262 char buf[256];
3263 } req;
3264 int ipa_len;
3265
3266 /* Form the request, specifying filter (rtattr) if needed. */
3267 memset(&req, 0, sizeof(req));
3268 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3269 req.n.nlmsg_flags = NLM_F_REQUEST;
3270 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
3271 req.ndm.ndm_ifindex = ifindex;
3272
3273 if (IS_IPADDR_V4(ip)) {
3274 ipa_len = IPV4_MAX_BYTELEN;
3275 req.ndm.ndm_family = AF_INET;
3276
3277 } else {
3278 ipa_len = IPV6_MAX_BYTELEN;
3279 req.ndm.ndm_family = AF_INET6;
3280 }
3281
3282 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
3283
3284 return netlink_request(&zns->netlink_cmd, &req.n);
3285}
3286
3287int netlink_neigh_read_specific_ip(struct ipaddr *ip,
3288 struct interface *vlan_if)
3289{
3290 int ret = 0;
3291 struct zebra_ns *zns;
3292 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vlan_if->vrf_id);
3293 char buf[INET6_ADDRSTRLEN];
3294 struct zebra_dplane_info dp_info;
3295
3296 zns = zvrf->zns;
3297
3298 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
3299
3300 if (IS_ZEBRA_DEBUG_KERNEL)
3301 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
3302 __PRETTY_FUNCTION__, vlan_if->name,
3303 vlan_if->ifindex,
3304 ipaddr2str(ip, buf, sizeof(buf)),
3305 vlan_if->vrf_id);
3306
3307 ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip,
3308 vlan_if->ifindex);
3309 if (ret < 0)
3310 return ret;
3311
3312 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
3313 &dp_info, 1, 0);
3314
3315 return ret;
3316}
3317
2414abd3 3318int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
2232a77c 3319{
d62a17ae 3320 int len;
3321 struct ndmsg *ndm;
2232a77c 3322
d62a17ae 3323 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH))
3324 return 0;
2232a77c 3325
d62a17ae 3326 /* Length validity. */
3327 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
9bdf8618
DS
3328 if (len < 0) {
3329 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
3330 __PRETTY_FUNCTION__, h->nlmsg_len,
3331 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
d62a17ae 3332 return -1;
9bdf8618 3333 }
2232a77c 3334
d62a17ae 3335 /* Is this a notification for the MAC FDB or IP neighbor table? */
3336 ndm = NLMSG_DATA(h);
3337 if (ndm->ndm_family == AF_BRIDGE)
2414abd3 3338 return netlink_macfdb_change(h, len, ns_id);
2232a77c 3339
d62a17ae 3340 if (ndm->ndm_type != RTN_UNICAST)
3341 return 0;
2232a77c 3342
d62a17ae 3343 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
2414abd3 3344 return netlink_ipneigh_change(h, len, ns_id);
8a1b681c 3345 else {
9df414fe 3346 flog_warn(
e914ccbe 3347 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
3348 "Invalid address family: %u received from kernel neighbor change: %s",
3349 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
3350 return 0;
3351 }
2232a77c 3352
d62a17ae 3353 return 0;
2232a77c 3354}
3355
931fa60c
MS
3356/*
3357 * Utility neighbor-update function, using info from dplane context.
3358 */
3359static int netlink_neigh_update_ctx(const struct zebra_dplane_ctx *ctx,
3360 int cmd)
2232a77c 3361{
f3dbec60 3362 uint8_t protocol = RTPROT_ZEBRA;
d62a17ae 3363 struct {
3364 struct nlmsghdr n;
3365 struct ndmsg ndm;
3366 char buf[256];
3367 } req;
3368 int ipa_len;
d62a17ae 3369 char buf[INET6_ADDRSTRLEN];
3370 char buf2[ETHER_ADDR_STRLEN];
931fa60c
MS
3371 const struct ipaddr *ip;
3372 const struct ethaddr *mac;
3373 uint8_t flags;
3374 uint16_t state;
d62a17ae 3375
5605ecfc 3376 memset(&req, 0, sizeof(req));
d62a17ae 3377
931fa60c
MS
3378 ip = dplane_ctx_neigh_get_ipaddr(ctx);
3379 mac = dplane_ctx_neigh_get_mac(ctx);
3380 if (is_zero_mac(mac))
3381 mac = NULL;
3382
3383 flags = neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx));
3384 state = neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx));
3385
d62a17ae 3386 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
3387 req.n.nlmsg_flags = NLM_F_REQUEST;
3388 if (cmd == RTM_NEWNEIGH)
3389 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
3390 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
3391 req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
68e33151 3392 req.ndm.ndm_state = state;
931fa60c 3393 req.ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx);
d62a17ae 3394 req.ndm.ndm_type = RTN_UNICAST;
68e33151 3395 req.ndm.ndm_flags = flags;
d62a17ae 3396
f3dbec60
DS
3397 addattr_l(&req.n, sizeof(req),
3398 NDA_PROTOCOL, &protocol, sizeof(protocol));
d62a17ae 3399 ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
3400 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
3401 if (mac)
3402 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
3403
3404 if (IS_ZEBRA_DEBUG_KERNEL)
6fe2b0e6 3405 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
d62a17ae 3406 nl_msg_type_to_str(cmd),
931fa60c
MS
3407 nl_family_to_str(req.ndm.ndm_family),
3408 dplane_ctx_get_ifname(ctx),
3409 dplane_ctx_get_ifindex(ctx),
3410 ipaddr2str(ip, buf, sizeof(buf)),
d62a17ae 3411 mac ? prefix_mac2str(mac, buf2, sizeof(buf2))
931fa60c
MS
3412 : "null",
3413 flags, state);
d62a17ae 3414
931fa60c
MS
3415 return netlink_talk_info(netlink_talk_filter, &req.n,
3416 dplane_ctx_get_ns(ctx), 0);
2232a77c 3417}
3418
036d93c0
MS
3419/*
3420 * Update MAC, using dataplane context object.
3421 */
3422enum zebra_dplane_result kernel_mac_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 3423{
036d93c0 3424 return netlink_macfdb_update_ctx(ctx);
2232a77c 3425}
3426
931fa60c 3427enum zebra_dplane_result kernel_neigh_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 3428{
931fa60c 3429 int ret = -1;
2232a77c 3430
931fa60c
MS
3431 switch (dplane_ctx_get_op(ctx)) {
3432 case DPLANE_OP_NEIGH_INSTALL:
3433 case DPLANE_OP_NEIGH_UPDATE:
3434 ret = netlink_neigh_update_ctx(ctx, RTM_NEWNEIGH);
3435 break;
3436 case DPLANE_OP_NEIGH_DELETE:
3437 ret = netlink_neigh_update_ctx(ctx, RTM_DELNEIGH);
3438 break;
0bbd4ff4
MS
3439 case DPLANE_OP_VTEP_ADD:
3440 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_NEWNEIGH);
3441 break;
3442 case DPLANE_OP_VTEP_DELETE:
3443 ret = netlink_vxlan_flood_update_ctx(ctx, RTM_DELNEIGH);
3444 break;
931fa60c
MS
3445 default:
3446 break;
3447 }
2232a77c 3448
931fa60c
MS
3449 return (ret == 0 ?
3450 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
6fe2b0e6
CS
3451}
3452
16c628de
MS
3453/*
3454 * MPLS label forwarding table change via netlink interface, using dataplane
3455 * context information.
3456 */
fc608372 3457int netlink_mpls_multipath(int cmd, struct zebra_dplane_ctx *ctx)
16c628de
MS
3458{
3459 mpls_lse_t lse;
81793ac1 3460 const zebra_nhlfe_t *nhlfe;
16c628de
MS
3461 struct nexthop *nexthop = NULL;
3462 unsigned int nexthop_num;
3463 const char *routedesc;
3464 int route_type;
3465
3466 struct {
3467 struct nlmsghdr n;
3468 struct rtmsg r;
3469 char buf[NL_PKT_BUF_SIZE];
3470 } req;
3471
3472 memset(&req, 0, sizeof(req) - NL_PKT_BUF_SIZE);
3473
3474 /*
3475 * Count # nexthops so we can decide whether to use singlepath
3476 * or multipath case.
3477 */
3478 nexthop_num = 0;
3479 for (nhlfe = dplane_ctx_get_nhlfe(ctx); nhlfe; nhlfe = nhlfe->next) {
3480 nexthop = nhlfe->nexthop;
3481 if (!nexthop)
3482 continue;
3483 if (cmd == RTM_NEWROUTE) {
3484 /* Count all selected NHLFEs */
3485 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3486 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
3487 nexthop_num++;
3488 } else { /* DEL */
3489 /* Count all installed NHLFEs */
3490 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
3491 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
3492 nexthop_num++;
3493 }
3494 }
3495
3496 if ((nexthop_num == 0) ||
3497 (!dplane_ctx_get_best_nhlfe(ctx) && (cmd != RTM_DELROUTE)))
3498 return 0;
3499
3500 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
3501 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
3502 req.n.nlmsg_type = cmd;
3503 req.n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
3504
3505 req.r.rtm_family = AF_MPLS;
3506 req.r.rtm_table = RT_TABLE_MAIN;
3507 req.r.rtm_dst_len = MPLS_LABEL_LEN_BITS;
3508 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
3509 req.r.rtm_type = RTN_UNICAST;
3510
3511 if (cmd == RTM_NEWROUTE) {
3512 /* We do a replace to handle update. */
3513 req.n.nlmsg_flags |= NLM_F_REPLACE;
3514
3515 /* set the protocol value if installing */
3516 route_type = re_type_from_lsp_type(
3517 dplane_ctx_get_best_nhlfe(ctx)->type);
3518 req.r.rtm_protocol = zebra2proto(route_type);
3519 }
3520
3521 /* Fill destination */
3522 lse = mpls_lse_encode(dplane_ctx_get_in_label(ctx), 0, 0, 1);
3523 addattr_l(&req.n, sizeof(req), RTA_DST, &lse, sizeof(mpls_lse_t));
3524
3525 /* Fill nexthops (paths) based on single-path or multipath. The paths
3526 * chosen depend on the operation.
3527 */
fc608372 3528 if (nexthop_num == 1) {
16c628de
MS
3529 routedesc = "single-path";
3530 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
3531 routedesc);
3532
3533 nexthop_num = 0;
3534 for (nhlfe = dplane_ctx_get_nhlfe(ctx);
3535 nhlfe; nhlfe = nhlfe->next) {
3536 nexthop = nhlfe->nexthop;
3537 if (!nexthop)
3538 continue;
3539
3540 if ((cmd == RTM_NEWROUTE
3541 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3542 && CHECK_FLAG(nexthop->flags,
3543 NEXTHOP_FLAG_ACTIVE)))
3544 || (cmd == RTM_DELROUTE
3545 && (CHECK_FLAG(nhlfe->flags,
3546 NHLFE_FLAG_INSTALLED)
3547 && CHECK_FLAG(nexthop->flags,
3548 NEXTHOP_FLAG_FIB)))) {
3549 /* Add the gateway */
3550 _netlink_mpls_build_singlepath(
3551 routedesc, nhlfe,
3552 &req.n, &req.r,
3553 sizeof(req), cmd);
3554
3555 nexthop_num++;
3556 break;
3557 }
3558 }
3559 } else { /* Multipath case */
3560 char buf[NL_PKT_BUF_SIZE];
3561 struct rtattr *rta = (void *)buf;
3562 struct rtnexthop *rtnh;
81793ac1 3563 const union g_addr *src1 = NULL;
16c628de
MS
3564
3565 rta->rta_type = RTA_MULTIPATH;
3566 rta->rta_len = RTA_LENGTH(0);
3567 rtnh = RTA_DATA(rta);
3568
3569 routedesc = "multipath";
3570 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
3571 routedesc);
3572
3573 nexthop_num = 0;
3574 for (nhlfe = dplane_ctx_get_nhlfe(ctx);
3575 nhlfe; nhlfe = nhlfe->next) {
3576 nexthop = nhlfe->nexthop;
3577 if (!nexthop)
3578 continue;
3579
16c628de
MS
3580 if ((cmd == RTM_NEWROUTE
3581 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
3582 && CHECK_FLAG(nexthop->flags,
3583 NEXTHOP_FLAG_ACTIVE)))
3584 || (cmd == RTM_DELROUTE
3585 && (CHECK_FLAG(nhlfe->flags,
3586 NHLFE_FLAG_INSTALLED)
3587 && CHECK_FLAG(nexthop->flags,
3588 NEXTHOP_FLAG_FIB)))) {
3589 nexthop_num++;
3590
3591 /* Build the multipath */
3592 _netlink_mpls_build_multipath(routedesc, nhlfe,
3593 rta, rtnh, &req.r,
3594 &src1);
3595 rtnh = RTNH_NEXT(rtnh);
3596 }
3597 }
3598
3599 /* Add the multipath */
3600 if (rta->rta_len > RTA_LENGTH(0))
3601 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
3602 RTA_DATA(rta), RTA_PAYLOAD(rta));
3603 }
3604
3605 /* Talk to netlink socket. */
3606 return netlink_talk_info(netlink_talk_filter, &req.n,
3607 dplane_ctx_get_ns(ctx), 0);
3608}
ddfeb486 3609#endif /* HAVE_NETLINK */