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