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