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