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