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