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