]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rt_netlink.c
zebra: Create zebra_dplane.c and .h
[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
894 /* Get IPv4 routing table. */
895 ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE);
896 if (ret < 0)
897 return ret;
898 ret = netlink_parse_info(netlink_route_change_read_unicast,
899 &zns->netlink_cmd, zns, 0, 1);
900 if (ret < 0)
901 return ret;
902
903 /* Get IPv6 routing table. */
904 ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE);
905 if (ret < 0)
906 return ret;
907 ret = netlink_parse_info(netlink_route_change_read_unicast,
908 &zns->netlink_cmd, zns, 0, 1);
909 if (ret < 0)
910 return ret;
911
912 return 0;
913 }
914
915 static void _netlink_route_nl_add_gateway_info(uint8_t route_family,
916 uint8_t gw_family,
917 struct nlmsghdr *nlmsg,
918 size_t req_size, int bytelen,
919 struct nexthop *nexthop)
920 {
921 if (route_family == AF_MPLS) {
922 struct gw_family_t gw_fam;
923
924 gw_fam.family = gw_family;
925 if (gw_family == AF_INET)
926 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
927 else
928 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
929 addattr_l(nlmsg, req_size, RTA_VIA, &gw_fam.family,
930 bytelen + 2);
931 } else {
932 if (gw_family == AF_INET)
933 addattr_l(nlmsg, req_size, RTA_GATEWAY,
934 &nexthop->gate.ipv4, bytelen);
935 else
936 addattr_l(nlmsg, req_size, RTA_GATEWAY,
937 &nexthop->gate.ipv6, bytelen);
938 }
939 }
940
941 static void _netlink_route_rta_add_gateway_info(uint8_t route_family,
942 uint8_t gw_family,
943 struct rtattr *rta,
944 struct rtnexthop *rtnh,
945 size_t req_size, int bytelen,
946 struct nexthop *nexthop)
947 {
948 if (route_family == AF_MPLS) {
949 struct gw_family_t gw_fam;
950
951 gw_fam.family = gw_family;
952 if (gw_family == AF_INET)
953 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
954 else
955 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
956 rta_addattr_l(rta, req_size, RTA_VIA, &gw_fam.family,
957 bytelen + 2);
958 rtnh->rtnh_len += RTA_LENGTH(bytelen + 2);
959 } else {
960 if (gw_family == AF_INET)
961 rta_addattr_l(rta, req_size, RTA_GATEWAY,
962 &nexthop->gate.ipv4, bytelen);
963 else
964 rta_addattr_l(rta, req_size, RTA_GATEWAY,
965 &nexthop->gate.ipv6, bytelen);
966 rtnh->rtnh_len += sizeof(struct rtattr) + bytelen;
967 }
968 }
969
970 /* This function takes a nexthop as argument and adds
971 * the appropriate netlink attributes to an existing
972 * netlink message.
973 *
974 * @param routedesc: Human readable description of route type
975 * (direct/recursive, single-/multipath)
976 * @param bytelen: Length of addresses in bytes.
977 * @param nexthop: Nexthop information
978 * @param nlmsg: nlmsghdr structure to fill in.
979 * @param req_size: The size allocated for the message.
980 */
981 static void _netlink_route_build_singlepath(const char *routedesc, int bytelen,
982 struct nexthop *nexthop,
983 struct nlmsghdr *nlmsg,
984 struct rtmsg *rtmsg,
985 size_t req_size, int cmd)
986 {
987 struct mpls_label_stack *nh_label;
988 mpls_lse_t out_lse[MPLS_MAX_LABELS];
989 int num_labels = 0;
990 char label_buf[256];
991
992 /*
993 * label_buf is *only* currently used within debugging.
994 * As such when we assign it we are guarding it inside
995 * a debug test. If you want to change this make sure
996 * you fix this assumption
997 */
998 label_buf[0] = '\0';
999
1000 assert(nexthop);
1001 for (struct nexthop *nh = nexthop; nh; nh = nh->rparent) {
1002 char label_buf1[20];
1003
1004 nh_label = nh->nh_label;
1005 if (!nh_label || !nh_label->num_labels)
1006 continue;
1007
1008 for (int i = 0; i < nh_label->num_labels; i++) {
1009 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1010 continue;
1011
1012 if (IS_ZEBRA_DEBUG_KERNEL) {
1013 if (!num_labels)
1014 sprintf(label_buf, "label %u",
1015 nh_label->label[i]);
1016 else {
1017 sprintf(label_buf1, "/%u",
1018 nh_label->label[i]);
1019 strlcat(label_buf, label_buf1,
1020 sizeof(label_buf));
1021 }
1022 }
1023
1024 out_lse[num_labels] =
1025 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1026 num_labels++;
1027 }
1028 }
1029
1030 if (num_labels) {
1031 /* Set the BoS bit */
1032 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1033
1034 if (rtmsg->rtm_family == AF_MPLS)
1035 addattr_l(nlmsg, req_size, RTA_NEWDST, &out_lse,
1036 num_labels * sizeof(mpls_lse_t));
1037 else {
1038 struct rtattr *nest;
1039 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
1040
1041 addattr_l(nlmsg, req_size, RTA_ENCAP_TYPE, &encap,
1042 sizeof(uint16_t));
1043 nest = addattr_nest(nlmsg, req_size, RTA_ENCAP);
1044 addattr_l(nlmsg, req_size, MPLS_IPTUNNEL_DST, &out_lse,
1045 num_labels * sizeof(mpls_lse_t));
1046 addattr_nest_end(nlmsg, nest);
1047 }
1048 }
1049
1050 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1051 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1052
1053 if (rtmsg->rtm_family == AF_INET
1054 && (nexthop->type == NEXTHOP_TYPE_IPV6
1055 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) {
1056 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1057 addattr_l(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4);
1058 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1059
1060 if (nexthop->rmap_src.ipv4.s_addr && (cmd == RTM_NEWROUTE))
1061 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1062 &nexthop->rmap_src.ipv4, bytelen);
1063 else if (nexthop->src.ipv4.s_addr && (cmd == RTM_NEWROUTE))
1064 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1065 &nexthop->src.ipv4, bytelen);
1066
1067 if (IS_ZEBRA_DEBUG_KERNEL)
1068 zlog_debug(
1069 " 5549: _netlink_route_build_singlepath() (%s): "
1070 "nexthop via %s %s if %u(%u)",
1071 routedesc, ipv4_ll_buf, label_buf,
1072 nexthop->ifindex, nexthop->vrf_id);
1073 return;
1074 }
1075
1076 if (nexthop->type == NEXTHOP_TYPE_IPV4
1077 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1078 /* Send deletes to the kernel without specifying the next-hop */
1079 if (cmd != RTM_DELROUTE)
1080 _netlink_route_nl_add_gateway_info(
1081 rtmsg->rtm_family, AF_INET, nlmsg, req_size,
1082 bytelen, nexthop);
1083
1084 if (cmd == RTM_NEWROUTE) {
1085 if (nexthop->rmap_src.ipv4.s_addr)
1086 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1087 &nexthop->rmap_src.ipv4, bytelen);
1088 else if (nexthop->src.ipv4.s_addr)
1089 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1090 &nexthop->src.ipv4, bytelen);
1091 }
1092
1093 if (IS_ZEBRA_DEBUG_KERNEL)
1094 zlog_debug(
1095 "netlink_route_multipath() (%s): "
1096 "nexthop via %s %s if %u(%u)",
1097 routedesc, inet_ntoa(nexthop->gate.ipv4),
1098 label_buf, nexthop->ifindex, nexthop->vrf_id);
1099 }
1100
1101 if (nexthop->type == NEXTHOP_TYPE_IPV6
1102 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1103 _netlink_route_nl_add_gateway_info(rtmsg->rtm_family, AF_INET6,
1104 nlmsg, req_size, bytelen,
1105 nexthop);
1106
1107 if (cmd == RTM_NEWROUTE) {
1108 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1109 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1110 &nexthop->rmap_src.ipv6, bytelen);
1111 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1112 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1113 &nexthop->src.ipv6, bytelen);
1114 }
1115
1116 if (IS_ZEBRA_DEBUG_KERNEL)
1117 zlog_debug(
1118 "netlink_route_multipath() (%s): "
1119 "nexthop via %s %s if %u(%u)",
1120 routedesc, inet6_ntoa(nexthop->gate.ipv6),
1121 label_buf, nexthop->ifindex, nexthop->vrf_id);
1122 }
1123
1124 /*
1125 * We have the ifindex so we should always send it
1126 * This is especially useful if we are doing route
1127 * leaking.
1128 */
1129 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1130 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1131
1132 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1133 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1134 if (cmd == RTM_NEWROUTE) {
1135 if (nexthop->rmap_src.ipv4.s_addr)
1136 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1137 &nexthop->rmap_src.ipv4, bytelen);
1138 else if (nexthop->src.ipv4.s_addr)
1139 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1140 &nexthop->src.ipv4, bytelen);
1141 }
1142
1143 if (IS_ZEBRA_DEBUG_KERNEL)
1144 zlog_debug(
1145 "netlink_route_multipath() (%s): "
1146 "nexthop via if %u(%u)",
1147 routedesc, nexthop->ifindex, nexthop->vrf_id);
1148 }
1149
1150 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1151 if (cmd == RTM_NEWROUTE) {
1152 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1153 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1154 &nexthop->rmap_src.ipv6, bytelen);
1155 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1156 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1157 &nexthop->src.ipv6, bytelen);
1158 }
1159
1160 if (IS_ZEBRA_DEBUG_KERNEL)
1161 zlog_debug(
1162 "netlink_route_multipath() (%s): "
1163 "nexthop via if %u(%u)",
1164 routedesc, nexthop->ifindex, nexthop->vrf_id);
1165 }
1166 }
1167
1168 /* This function takes a nexthop as argument and
1169 * appends to the given rtattr/rtnexthop pair the
1170 * representation of the nexthop. If the nexthop
1171 * defines a preferred source, the src parameter
1172 * will be modified to point to that src, otherwise
1173 * it will be kept unmodified.
1174 *
1175 * @param routedesc: Human readable description of route type
1176 * (direct/recursive, single-/multipath)
1177 * @param bytelen: Length of addresses in bytes.
1178 * @param nexthop: Nexthop information
1179 * @param rta: rtnetlink attribute structure
1180 * @param rtnh: pointer to an rtnetlink nexthop structure
1181 * @param src: pointer pointing to a location where
1182 * the prefsrc should be stored.
1183 */
1184 static void _netlink_route_build_multipath(const char *routedesc, int bytelen,
1185 struct nexthop *nexthop,
1186 struct rtattr *rta,
1187 struct rtnexthop *rtnh,
1188 struct rtmsg *rtmsg,
1189 union g_addr **src)
1190 {
1191 struct mpls_label_stack *nh_label;
1192 mpls_lse_t out_lse[MPLS_MAX_LABELS];
1193 int num_labels = 0;
1194 char label_buf[256];
1195
1196 rtnh->rtnh_len = sizeof(*rtnh);
1197 rtnh->rtnh_flags = 0;
1198 rtnh->rtnh_hops = 0;
1199 rta->rta_len += rtnh->rtnh_len;
1200
1201 /*
1202 * label_buf is *only* currently used within debugging.
1203 * As such when we assign it we are guarding it inside
1204 * a debug test. If you want to change this make sure
1205 * you fix this assumption
1206 */
1207 label_buf[0] = '\0';
1208
1209 assert(nexthop);
1210 for (struct nexthop *nh = nexthop; nh; nh = nh->rparent) {
1211 char label_buf1[20];
1212
1213 nh_label = nh->nh_label;
1214 if (!nh_label || !nh_label->num_labels)
1215 continue;
1216
1217 for (int i = 0; i < nh_label->num_labels; i++) {
1218 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1219 continue;
1220
1221 if (IS_ZEBRA_DEBUG_KERNEL) {
1222 if (!num_labels)
1223 sprintf(label_buf, "label %u",
1224 nh_label->label[i]);
1225 else {
1226 sprintf(label_buf1, "/%u",
1227 nh_label->label[i]);
1228 strlcat(label_buf, label_buf1,
1229 sizeof(label_buf));
1230 }
1231 }
1232
1233 out_lse[num_labels] =
1234 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1235 num_labels++;
1236 }
1237 }
1238
1239 if (num_labels) {
1240 /* Set the BoS bit */
1241 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1242
1243 if (rtmsg->rtm_family == AF_MPLS) {
1244 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_NEWDST,
1245 &out_lse,
1246 num_labels * sizeof(mpls_lse_t));
1247 rtnh->rtnh_len +=
1248 RTA_LENGTH(num_labels * sizeof(mpls_lse_t));
1249 } else {
1250 struct rtattr *nest;
1251 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
1252 int len = rta->rta_len;
1253
1254 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_ENCAP_TYPE,
1255 &encap, sizeof(uint16_t));
1256 nest = rta_nest(rta, NL_PKT_BUF_SIZE, RTA_ENCAP);
1257 rta_addattr_l(rta, NL_PKT_BUF_SIZE, MPLS_IPTUNNEL_DST,
1258 &out_lse,
1259 num_labels * sizeof(mpls_lse_t));
1260 rta_nest_end(rta, nest);
1261 rtnh->rtnh_len += rta->rta_len - len;
1262 }
1263 }
1264
1265 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1266 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1267
1268 if (rtmsg->rtm_family == AF_INET
1269 && (nexthop->type == NEXTHOP_TYPE_IPV6
1270 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) {
1271 bytelen = 4;
1272 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1273 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, &ipv4_ll,
1274 bytelen);
1275 rtnh->rtnh_len += sizeof(struct rtattr) + bytelen;
1276 rtnh->rtnh_ifindex = nexthop->ifindex;
1277
1278 if (nexthop->rmap_src.ipv4.s_addr)
1279 *src = &nexthop->rmap_src;
1280 else if (nexthop->src.ipv4.s_addr)
1281 *src = &nexthop->src;
1282
1283 if (IS_ZEBRA_DEBUG_KERNEL)
1284 zlog_debug(
1285 " 5549: netlink_route_build_multipath() (%s): "
1286 "nexthop via %s %s if %u",
1287 routedesc, ipv4_ll_buf, label_buf,
1288 nexthop->ifindex);
1289 return;
1290 }
1291
1292 if (nexthop->type == NEXTHOP_TYPE_IPV4
1293 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1294 _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET,
1295 rta, rtnh, NL_PKT_BUF_SIZE,
1296 bytelen, nexthop);
1297 if (nexthop->rmap_src.ipv4.s_addr)
1298 *src = &nexthop->rmap_src;
1299 else if (nexthop->src.ipv4.s_addr)
1300 *src = &nexthop->src;
1301
1302 if (IS_ZEBRA_DEBUG_KERNEL)
1303 zlog_debug(
1304 "netlink_route_multipath() (%s): "
1305 "nexthop via %s %s if %u",
1306 routedesc, inet_ntoa(nexthop->gate.ipv4),
1307 label_buf, nexthop->ifindex);
1308 }
1309 if (nexthop->type == NEXTHOP_TYPE_IPV6
1310 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1311 _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET6,
1312 rta, rtnh, NL_PKT_BUF_SIZE,
1313 bytelen, nexthop);
1314
1315 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1316 *src = &nexthop->rmap_src;
1317 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1318 *src = &nexthop->src;
1319
1320 if (IS_ZEBRA_DEBUG_KERNEL)
1321 zlog_debug(
1322 "netlink_route_multipath() (%s): "
1323 "nexthop via %s %s if %u",
1324 routedesc, inet6_ntoa(nexthop->gate.ipv6),
1325 label_buf, nexthop->ifindex);
1326 }
1327
1328 /*
1329 * We have figured out the ifindex so we should always send it
1330 * This is especially useful if we are doing route
1331 * leaking.
1332 */
1333 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1334 rtnh->rtnh_ifindex = nexthop->ifindex;
1335
1336 /* ifindex */
1337 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1338 || nexthop->type == NEXTHOP_TYPE_IFINDEX) {
1339 if (nexthop->rmap_src.ipv4.s_addr)
1340 *src = &nexthop->rmap_src;
1341 else if (nexthop->src.ipv4.s_addr)
1342 *src = &nexthop->src;
1343
1344 if (IS_ZEBRA_DEBUG_KERNEL)
1345 zlog_debug(
1346 "netlink_route_multipath() (%s): "
1347 "nexthop via if %u",
1348 routedesc, nexthop->ifindex);
1349 }
1350 }
1351
1352 static inline void _netlink_mpls_build_singlepath(const char *routedesc,
1353 zebra_nhlfe_t *nhlfe,
1354 struct nlmsghdr *nlmsg,
1355 struct rtmsg *rtmsg,
1356 size_t req_size, int cmd)
1357 {
1358 int bytelen;
1359 uint8_t family;
1360
1361 family = NHLFE_FAMILY(nhlfe);
1362 bytelen = (family == AF_INET ? 4 : 16);
1363 _netlink_route_build_singlepath(routedesc, bytelen, nhlfe->nexthop,
1364 nlmsg, rtmsg, req_size, cmd);
1365 }
1366
1367
1368 static inline void
1369 _netlink_mpls_build_multipath(const char *routedesc, zebra_nhlfe_t *nhlfe,
1370 struct rtattr *rta, struct rtnexthop *rtnh,
1371 struct rtmsg *rtmsg, union g_addr **src)
1372 {
1373 int bytelen;
1374 uint8_t family;
1375
1376 family = NHLFE_FAMILY(nhlfe);
1377 bytelen = (family == AF_INET ? 4 : 16);
1378 _netlink_route_build_multipath(routedesc, bytelen, nhlfe->nexthop, rta,
1379 rtnh, rtmsg, src);
1380 }
1381
1382
1383 /* Log debug information for netlink_route_multipath
1384 * if debug logging is enabled.
1385 *
1386 * @param cmd: Netlink command which is to be processed
1387 * @param p: Prefix for which the change is due
1388 * @param family: Address family which the change concerns
1389 * @param zvrf: The vrf we are in
1390 * @param tableid: The table we are working on
1391 */
1392 static void _netlink_route_debug(int cmd, const struct prefix *p,
1393 int family, vrf_id_t vrfid,
1394 uint32_t tableid)
1395 {
1396 if (IS_ZEBRA_DEBUG_KERNEL) {
1397 char buf[PREFIX_STRLEN];
1398 zlog_debug(
1399 "netlink_route_multipath(): %s %s vrf %u(%u)",
1400 nl_msg_type_to_str(cmd),
1401 prefix2str(p, buf, sizeof(buf)),
1402 vrfid, tableid);
1403 }
1404 }
1405
1406 static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc)
1407 {
1408 if (IS_ZEBRA_DEBUG_KERNEL)
1409 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc,
1410 nl_msg_type_to_str(cmd), label);
1411 }
1412
1413 static int netlink_neigh_update(int cmd, int ifindex, uint32_t addr, char *lla,
1414 int llalen, ns_id_t ns_id)
1415 {
1416 struct {
1417 struct nlmsghdr n;
1418 struct ndmsg ndm;
1419 char buf[256];
1420 } req;
1421
1422 struct zebra_ns *zns = zebra_ns_lookup(ns_id);
1423
1424 memset(&req, 0, sizeof(req));
1425
1426 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1427 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1428 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
1429 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1430
1431 req.ndm.ndm_family = AF_INET;
1432 req.ndm.ndm_state = NUD_PERMANENT;
1433 req.ndm.ndm_ifindex = ifindex;
1434 req.ndm.ndm_type = RTN_UNICAST;
1435
1436 addattr_l(&req.n, sizeof(req), NDA_DST, &addr, 4);
1437 addattr_l(&req.n, sizeof(req), NDA_LLADDR, lla, llalen);
1438
1439 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1440 0);
1441 }
1442
1443 /* Routing table change via netlink interface. */
1444 /* Update flag indicates whether this is a "replace" or not. */
1445 static int netlink_route_multipath(int cmd, const struct prefix *p,
1446 const struct prefix *src_p,
1447 struct route_entry *re,
1448 int update)
1449 {
1450 int bytelen;
1451 struct sockaddr_nl snl;
1452 struct nexthop *nexthop = NULL;
1453 unsigned int nexthop_num;
1454 int family = PREFIX_FAMILY(p);
1455 const char *routedesc;
1456 int setsrc = 0;
1457 union g_addr src;
1458
1459 struct {
1460 struct nlmsghdr n;
1461 struct rtmsg r;
1462 char buf[NL_PKT_BUF_SIZE];
1463 } req;
1464
1465 struct zebra_ns *zns;
1466 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
1467
1468 zns = zvrf->zns;
1469 memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE);
1470
1471 bytelen = (family == AF_INET ? 4 : 16);
1472
1473 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1474 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1475 if ((cmd == RTM_NEWROUTE) && update)
1476 req.n.nlmsg_flags |= NLM_F_REPLACE;
1477 req.n.nlmsg_type = cmd;
1478 req.n.nlmsg_pid = zns->netlink_cmd.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_protocol = zebra2proto(re->type);
1484 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1485
1486 /*
1487 * blackhole routes are not RTN_UNICAST, they are
1488 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1489 * so setting this value as a RTN_UNICAST would
1490 * cause the route lookup of just the prefix
1491 * to fail. So no need to specify this for
1492 * the RTM_DELROUTE case
1493 */
1494 if (cmd != RTM_DELROUTE)
1495 req.r.rtm_type = RTN_UNICAST;
1496
1497 addattr_l(&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1498 if (src_p)
1499 addattr_l(&req.n, sizeof req, RTA_SRC, &src_p->u.prefix,
1500 bytelen);
1501
1502 /* Metric. */
1503 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1504 * used
1505 * either by the kernel or by zebra. Its purely for calculating best
1506 * path(s)
1507 * by the routing protocol and for communicating with protocol peers.
1508 */
1509 addattr32(&req.n, sizeof req, RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC);
1510 #if defined(SUPPORT_REALMS)
1511 if (re->tag > 0 && re->tag <= 255)
1512 addattr32(&req.n, sizeof req, RTA_FLOW, re->tag);
1513 #endif
1514 /* Table corresponding to this route. */
1515 if (re->table < 256)
1516 req.r.rtm_table = re->table;
1517 else {
1518 req.r.rtm_table = RT_TABLE_UNSPEC;
1519 addattr32(&req.n, sizeof req, RTA_TABLE, re->table);
1520 }
1521
1522 _netlink_route_debug(cmd, p, family, zvrf_id(zvrf), re->table);
1523
1524 /*
1525 * If we are not updating the route and we have received
1526 * a route delete, then all we need to fill in is the
1527 * prefix information to tell the kernel to schwack
1528 * it.
1529 */
1530 if (!update && cmd == RTM_DELROUTE)
1531 goto skip;
1532
1533 if (re->mtu || re->nexthop_mtu) {
1534 char buf[NL_PKT_BUF_SIZE];
1535 struct rtattr *rta = (void *)buf;
1536 uint32_t mtu = re->mtu;
1537 if (!mtu || (re->nexthop_mtu && re->nexthop_mtu < mtu))
1538 mtu = re->nexthop_mtu;
1539 rta->rta_type = RTA_METRICS;
1540 rta->rta_len = RTA_LENGTH(0);
1541 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu);
1542 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA(rta),
1543 RTA_PAYLOAD(rta));
1544 }
1545
1546 /* Count overall nexthops so we can decide whether to use singlepath
1547 * or multipath case. */
1548 nexthop_num = 0;
1549 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1550 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1551 continue;
1552 if (cmd == RTM_NEWROUTE && !NEXTHOP_IS_ACTIVE(nexthop->flags))
1553 continue;
1554 if (cmd == RTM_DELROUTE
1555 && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1556 continue;
1557
1558 nexthop_num++;
1559 }
1560
1561 /* Singlepath case. */
1562 if (nexthop_num == 1 || multipath_num == 1) {
1563 nexthop_num = 0;
1564 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1565 /*
1566 * So we want to cover 2 types of blackhole
1567 * routes here:
1568 * 1) A normal blackhole route( ala from a static
1569 * install.
1570 * 2) A recursively resolved blackhole route
1571 */
1572 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1573 switch (nexthop->bh_type) {
1574 case BLACKHOLE_ADMINPROHIB:
1575 req.r.rtm_type = RTN_PROHIBIT;
1576 break;
1577 case BLACKHOLE_REJECT:
1578 req.r.rtm_type = RTN_UNREACHABLE;
1579 break;
1580 default:
1581 req.r.rtm_type = RTN_BLACKHOLE;
1582 break;
1583 }
1584 goto skip;
1585 }
1586 if (CHECK_FLAG(nexthop->flags,
1587 NEXTHOP_FLAG_RECURSIVE)) {
1588 if (!setsrc) {
1589 if (family == AF_INET) {
1590 if (nexthop->rmap_src.ipv4
1591 .s_addr
1592 != 0) {
1593 src.ipv4 =
1594 nexthop->rmap_src
1595 .ipv4;
1596 setsrc = 1;
1597 } else if (nexthop->src.ipv4
1598 .s_addr
1599 != 0) {
1600 src.ipv4 =
1601 nexthop->src
1602 .ipv4;
1603 setsrc = 1;
1604 }
1605 } else if (family == AF_INET6) {
1606 if (!IN6_IS_ADDR_UNSPECIFIED(
1607 &nexthop->rmap_src
1608 .ipv6)) {
1609 src.ipv6 =
1610 nexthop->rmap_src
1611 .ipv6;
1612 setsrc = 1;
1613 } else if (
1614 !IN6_IS_ADDR_UNSPECIFIED(
1615 &nexthop->src
1616 .ipv6)) {
1617 src.ipv6 =
1618 nexthop->src
1619 .ipv6;
1620 setsrc = 1;
1621 }
1622 }
1623 }
1624 continue;
1625 }
1626
1627 if ((cmd == RTM_NEWROUTE
1628 && NEXTHOP_IS_ACTIVE(nexthop->flags))
1629 || (cmd == RTM_DELROUTE
1630 && CHECK_FLAG(nexthop->flags,
1631 NEXTHOP_FLAG_FIB))) {
1632 routedesc = nexthop->rparent
1633 ? "recursive, single-path"
1634 : "single-path";
1635
1636 _netlink_route_build_singlepath(
1637 routedesc, bytelen, nexthop, &req.n,
1638 &req.r, sizeof req, cmd);
1639 nexthop_num++;
1640 break;
1641 }
1642 }
1643 if (setsrc && (cmd == RTM_NEWROUTE)) {
1644 if (family == AF_INET)
1645 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1646 &src.ipv4, bytelen);
1647 else if (family == AF_INET6)
1648 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1649 &src.ipv6, bytelen);
1650 }
1651 } else {
1652 char buf[NL_PKT_BUF_SIZE];
1653 struct rtattr *rta = (void *)buf;
1654 struct rtnexthop *rtnh;
1655 union g_addr *src1 = NULL;
1656
1657 rta->rta_type = RTA_MULTIPATH;
1658 rta->rta_len = RTA_LENGTH(0);
1659 rtnh = RTA_DATA(rta);
1660
1661 nexthop_num = 0;
1662 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1663 if (nexthop_num >= multipath_num)
1664 break;
1665
1666 if (CHECK_FLAG(nexthop->flags,
1667 NEXTHOP_FLAG_RECURSIVE)) {
1668 /* This only works for IPv4 now */
1669 if (!setsrc) {
1670 if (family == AF_INET) {
1671 if (nexthop->rmap_src.ipv4
1672 .s_addr
1673 != 0) {
1674 src.ipv4 =
1675 nexthop->rmap_src
1676 .ipv4;
1677 setsrc = 1;
1678 } else if (nexthop->src.ipv4
1679 .s_addr
1680 != 0) {
1681 src.ipv4 =
1682 nexthop->src
1683 .ipv4;
1684 setsrc = 1;
1685 }
1686 } else if (family == AF_INET6) {
1687 if (!IN6_IS_ADDR_UNSPECIFIED(
1688 &nexthop->rmap_src
1689 .ipv6)) {
1690 src.ipv6 =
1691 nexthop->rmap_src
1692 .ipv6;
1693 setsrc = 1;
1694 } else if (
1695 !IN6_IS_ADDR_UNSPECIFIED(
1696 &nexthop->src
1697 .ipv6)) {
1698 src.ipv6 =
1699 nexthop->src
1700 .ipv6;
1701 setsrc = 1;
1702 }
1703 }
1704 }
1705 continue;
1706 }
1707
1708 if ((cmd == RTM_NEWROUTE
1709 && NEXTHOP_IS_ACTIVE(nexthop->flags))
1710 || (cmd == RTM_DELROUTE
1711 && CHECK_FLAG(nexthop->flags,
1712 NEXTHOP_FLAG_FIB))) {
1713 routedesc = nexthop->rparent
1714 ? "recursive, multipath"
1715 : "multipath";
1716 nexthop_num++;
1717
1718 _netlink_route_build_multipath(
1719 routedesc, bytelen, nexthop, rta, rtnh,
1720 &req.r, &src1);
1721 rtnh = RTNH_NEXT(rtnh);
1722
1723 if (!setsrc && src1) {
1724 if (family == AF_INET)
1725 src.ipv4 = src1->ipv4;
1726 else if (family == AF_INET6)
1727 src.ipv6 = src1->ipv6;
1728
1729 setsrc = 1;
1730 }
1731 }
1732 }
1733 if (setsrc && (cmd == RTM_NEWROUTE)) {
1734 if (family == AF_INET)
1735 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1736 &src.ipv4, bytelen);
1737 else if (family == AF_INET6)
1738 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1739 &src.ipv6, bytelen);
1740 if (IS_ZEBRA_DEBUG_KERNEL)
1741 zlog_debug("Setting source");
1742 }
1743
1744 if (rta->rta_len > RTA_LENGTH(0))
1745 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
1746 RTA_DATA(rta), RTA_PAYLOAD(rta));
1747 }
1748
1749 /* If there is no useful nexthop then return. */
1750 if (nexthop_num == 0) {
1751 if (IS_ZEBRA_DEBUG_KERNEL)
1752 zlog_debug(
1753 "netlink_route_multipath(): No useful nexthop.");
1754 return 0;
1755 }
1756
1757 skip:
1758
1759 /* Destination netlink address. */
1760 memset(&snl, 0, sizeof snl);
1761 snl.nl_family = AF_NETLINK;
1762
1763 /* Talk to netlink socket. */
1764 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1765 0);
1766 }
1767
1768 int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in)
1769 {
1770 uint32_t actual_table;
1771 int suc = 0;
1772 struct mcast_route_data *mr = (struct mcast_route_data *)in;
1773 struct {
1774 struct nlmsghdr n;
1775 struct ndmsg ndm;
1776 char buf[256];
1777 } req;
1778
1779 mroute = mr;
1780 struct zebra_ns *zns;
1781
1782 zns = zvrf->zns;
1783 memset(&req, 0, sizeof(req));
1784
1785 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1786 req.n.nlmsg_flags = NLM_F_REQUEST;
1787 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1788
1789 req.ndm.ndm_family = RTNL_FAMILY_IPMR;
1790 req.n.nlmsg_type = RTM_GETROUTE;
1791
1792 addattr_l(&req.n, sizeof(req), RTA_IIF, &mroute->ifindex, 4);
1793 addattr_l(&req.n, sizeof(req), RTA_OIF, &mroute->ifindex, 4);
1794 addattr_l(&req.n, sizeof(req), RTA_SRC, &mroute->sg.src.s_addr, 4);
1795 addattr_l(&req.n, sizeof(req), RTA_DST, &mroute->sg.grp.s_addr, 4);
1796 /*
1797 * What?
1798 *
1799 * So during the namespace cleanup we started storing
1800 * the zvrf table_id for the default table as RT_TABLE_MAIN
1801 * which is what the normal routing table for ip routing is.
1802 * This change caused this to break our lookups of sg data
1803 * because prior to this change the zvrf->table_id was 0
1804 * and when the pim multicast kernel code saw a 0,
1805 * it was auto-translated to RT_TABLE_DEFAULT. But since
1806 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1807 * and the kernel goes screw you and the delicious cookies you
1808 * are trying to give me. So now we have this little hack.
1809 */
1810 actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT :
1811 zvrf->table_id;
1812 addattr_l(&req.n, sizeof(req), RTA_TABLE, &actual_table, 4);
1813
1814 suc = netlink_talk(netlink_route_change_read_multicast, &req.n,
1815 &zns->netlink_cmd, zns, 0);
1816
1817 mroute = NULL;
1818 return suc;
1819 }
1820
1821 enum zebra_dplane_result kernel_route_rib(struct route_node *rn,
1822 const struct prefix *p,
1823 const struct prefix *src_p,
1824 struct route_entry *old,
1825 struct route_entry *new)
1826 {
1827 int ret = 0;
1828
1829 assert(old || new);
1830
1831 if (new) {
1832 if (p->family == AF_INET || v6_rr_semantics)
1833 ret = netlink_route_multipath(RTM_NEWROUTE, p, src_p,
1834 new, (old) ? 1 : 0);
1835 else {
1836 /*
1837 * So v6 route replace semantics are not in
1838 * the kernel at this point as I understand it.
1839 * So let's do a delete than an add.
1840 * In the future once v6 route replace semantics
1841 * are in we can figure out what to do here to
1842 * allow working with old and new kernels.
1843 *
1844 * I'm also intentionally ignoring the failure case
1845 * of the route delete. If that happens yeah we're
1846 * screwed.
1847 */
1848 if (old)
1849 netlink_route_multipath(RTM_DELROUTE, p, src_p,
1850 old, 0);
1851 ret = netlink_route_multipath(RTM_NEWROUTE, p, src_p,
1852 new, 0);
1853 }
1854 kernel_route_rib_pass_fail(rn, p, new,
1855 (!ret) ? ZEBRA_DPLANE_INSTALL_SUCCESS
1856 : ZEBRA_DPLANE_INSTALL_FAILURE);
1857 return ZEBRA_DPLANE_REQUEST_SUCCESS;
1858 }
1859
1860 if (old) {
1861 ret = netlink_route_multipath(RTM_DELROUTE, p, src_p, old, 0);
1862
1863 kernel_route_rib_pass_fail(rn, p, old,
1864 (!ret) ? ZEBRA_DPLANE_DELETE_SUCCESS
1865 : ZEBRA_DPLANE_DELETE_FAILURE);
1866 }
1867
1868 return ZEBRA_DPLANE_REQUEST_SUCCESS;
1869 }
1870
1871 int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
1872 int llalen, ns_id_t ns_id)
1873 {
1874 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
1875 addr, lla, llalen, ns_id);
1876 }
1877
1878 /*
1879 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1880 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1881 */
1882 static int netlink_vxlan_flood_list_update(struct interface *ifp,
1883 struct in_addr *vtep_ip, int cmd)
1884 {
1885 struct zebra_ns *zns;
1886 struct {
1887 struct nlmsghdr n;
1888 struct ndmsg ndm;
1889 char buf[256];
1890 } req;
1891 uint8_t dst_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
1892 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
1893
1894 zns = zvrf->zns;
1895 memset(&req, 0, sizeof(req));
1896
1897 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1898 req.n.nlmsg_flags = NLM_F_REQUEST;
1899 if (cmd == RTM_NEWNEIGH)
1900 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_APPEND);
1901 req.n.nlmsg_type = cmd;
1902 req.ndm.ndm_family = PF_BRIDGE;
1903 req.ndm.ndm_state = NUD_NOARP | NUD_PERMANENT;
1904 req.ndm.ndm_flags |= NTF_SELF; // Handle by "self", not "master"
1905
1906
1907 addattr_l(&req.n, sizeof(req), NDA_LLADDR, &dst_mac, 6);
1908 req.ndm.ndm_ifindex = ifp->ifindex;
1909 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip->s_addr, 4);
1910
1911 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1912 0);
1913 }
1914
1915 /*
1916 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1917 * adding
1918 * a "flood" MAC FDB entry.
1919 */
1920 int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
1921 {
1922 if (IS_ZEBRA_DEBUG_VXLAN)
1923 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1924 inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex);
1925
1926 return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_NEWNEIGH);
1927 }
1928
1929 /*
1930 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1931 * deleting the "flood" MAC FDB entry.
1932 */
1933 int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
1934 {
1935 if (IS_ZEBRA_DEBUG_VXLAN)
1936 zlog_debug(
1937 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1938 inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex);
1939
1940 return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_DELNEIGH);
1941 }
1942
1943 #ifndef NDA_RTA
1944 #define NDA_RTA(r) \
1945 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
1946 #endif
1947
1948 static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
1949 {
1950 struct ndmsg *ndm;
1951 struct interface *ifp;
1952 struct zebra_if *zif;
1953 struct rtattr *tb[NDA_MAX + 1];
1954 struct interface *br_if;
1955 struct ethaddr mac;
1956 vlanid_t vid = 0;
1957 struct prefix vtep_ip;
1958 int vid_present = 0, dst_present = 0;
1959 char buf[ETHER_ADDR_STRLEN];
1960 char vid_buf[20];
1961 char dst_buf[30];
1962 uint8_t sticky = 0;
1963
1964 ndm = NLMSG_DATA(h);
1965
1966 /* We only process macfdb notifications if EVPN is enabled */
1967 if (!is_evpn_enabled())
1968 return 0;
1969
1970 /* The interface should exist. */
1971 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
1972 ndm->ndm_ifindex);
1973 if (!ifp || !ifp->info)
1974 return 0;
1975
1976 /* The interface should be something we're interested in. */
1977 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1978 return 0;
1979
1980 /* Drop "permanent" entries. */
1981 if (ndm->ndm_state & NUD_PERMANENT)
1982 return 0;
1983
1984 zif = (struct zebra_if *)ifp->info;
1985 if ((br_if = zif->brslave_info.br_if) == NULL) {
1986 zlog_debug("%s family %s IF %s(%u) brIF %u - no bridge master",
1987 nl_msg_type_to_str(h->nlmsg_type),
1988 nl_family_to_str(ndm->ndm_family), ifp->name,
1989 ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex);
1990 return 0;
1991 }
1992
1993 /* Parse attributes and extract fields of interest. */
1994 memset(tb, 0, sizeof tb);
1995 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
1996
1997 if (!tb[NDA_LLADDR]) {
1998 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
1999 nl_msg_type_to_str(h->nlmsg_type),
2000 nl_family_to_str(ndm->ndm_family), ifp->name,
2001 ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex);
2002 return 0;
2003 }
2004
2005 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
2006 zlog_debug(
2007 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2008 nl_msg_type_to_str(h->nlmsg_type),
2009 nl_family_to_str(ndm->ndm_family), ifp->name,
2010 ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex,
2011 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
2012 return 0;
2013 }
2014
2015 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
2016
2017 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
2018 vid_present = 1;
2019 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
2020 sprintf(vid_buf, " VLAN %u", vid);
2021 }
2022
2023 if (tb[NDA_DST]) {
2024 /* TODO: Only IPv4 supported now. */
2025 dst_present = 1;
2026 vtep_ip.family = AF_INET;
2027 vtep_ip.prefixlen = IPV4_MAX_BITLEN;
2028 memcpy(&(vtep_ip.u.prefix4.s_addr), RTA_DATA(tb[NDA_DST]),
2029 IPV4_MAX_BYTELEN);
2030 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip.u.prefix4));
2031 }
2032
2033 sticky = (ndm->ndm_state & NUD_NOARP) ? 1 : 0;
2034
2035 if (IS_ZEBRA_DEBUG_KERNEL)
2036 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2037 nl_msg_type_to_str(h->nlmsg_type),
2038 nl_family_to_str(ndm->ndm_family), ifp->name,
2039 ndm->ndm_ifindex, vid_present ? vid_buf : "",
2040 sticky ? "sticky " : "",
2041 prefix_mac2str(&mac, buf, sizeof(buf)),
2042 dst_present ? dst_buf : "");
2043
2044 if (filter_vlan && vid != filter_vlan)
2045 return 0;
2046
2047 /* If add or update, do accordingly if learnt on a "local" interface; if
2048 * the notification is over VxLAN, this has to be related to
2049 * multi-homing,
2050 * so perform an implicit delete of any local entry (if it exists).
2051 */
2052 if (h->nlmsg_type == RTM_NEWNEIGH) {
2053 /* Drop "permanent" entries. */
2054 if (ndm->ndm_state & NUD_PERMANENT)
2055 return 0;
2056
2057 if (IS_ZEBRA_IF_VXLAN(ifp))
2058 return zebra_vxlan_check_del_local_mac(ifp, br_if, &mac,
2059 vid);
2060
2061 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
2062 sticky);
2063 }
2064
2065 /* This is a delete notification.
2066 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2067 * 2. For a MAC over "local" interface, delete the mac
2068 * Note: We will get notifications from both bridge driver and VxLAN
2069 * driver.
2070 * Ignore the notification from VxLan driver as it is also generated
2071 * when mac moves from remote to local.
2072 */
2073 if (dst_present)
2074 return 0;
2075
2076 if (IS_ZEBRA_IF_VXLAN(ifp))
2077 return zebra_vxlan_check_readd_remote_mac(ifp, br_if, &mac,
2078 vid);
2079
2080 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
2081 }
2082
2083 static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2084 {
2085 int len;
2086 struct ndmsg *ndm;
2087
2088 if (h->nlmsg_type != RTM_NEWNEIGH)
2089 return 0;
2090
2091 /* Length validity. */
2092 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2093 if (len < 0)
2094 return -1;
2095
2096 /* We are interested only in AF_BRIDGE notifications. */
2097 ndm = NLMSG_DATA(h);
2098 if (ndm->ndm_family != AF_BRIDGE)
2099 return 0;
2100
2101 return netlink_macfdb_change(h, len, ns_id);
2102 }
2103
2104 /* Request for MAC FDB information from the kernel */
2105 static int netlink_request_macs(struct zebra_ns *zns, int family, int type,
2106 ifindex_t master_ifindex)
2107 {
2108 struct {
2109 struct nlmsghdr n;
2110 struct ifinfomsg ifm;
2111 char buf[256];
2112 } req;
2113
2114 /* Form the request, specifying filter (rtattr) if needed. */
2115 memset(&req, 0, sizeof(req));
2116 req.n.nlmsg_type = type;
2117 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2118 req.ifm.ifi_family = family;
2119 if (master_ifindex)
2120 addattr32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
2121
2122 return netlink_request(&zns->netlink_cmd, &req.n);
2123 }
2124
2125 /*
2126 * MAC forwarding database read using netlink interface. This is invoked
2127 * at startup.
2128 */
2129 int netlink_macfdb_read(struct zebra_ns *zns)
2130 {
2131 int ret;
2132
2133 /* Get bridge FDB table. */
2134 ret = netlink_request_macs(zns, AF_BRIDGE, RTM_GETNEIGH, 0);
2135 if (ret < 0)
2136 return ret;
2137 /* We are reading entire table. */
2138 filter_vlan = 0;
2139 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, zns,
2140 0, 1);
2141
2142 return ret;
2143 }
2144
2145 /*
2146 * MAC forwarding database read using netlink interface. This is for a
2147 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2148 */
2149 int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
2150 struct interface *br_if)
2151 {
2152 struct zebra_if *br_zif;
2153 struct zebra_if *zif;
2154 struct zebra_l2info_vxlan *vxl;
2155 int ret = 0;
2156
2157
2158 /* Save VLAN we're filtering on, if needed. */
2159 br_zif = (struct zebra_if *)br_if->info;
2160 zif = (struct zebra_if *)ifp->info;
2161 vxl = &zif->l2info.vxl;
2162 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
2163 filter_vlan = vxl->access_vlan;
2164
2165 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2166 */
2167 ret = netlink_request_macs(zns, AF_BRIDGE, RTM_GETNEIGH,
2168 br_if->ifindex);
2169 if (ret < 0)
2170 return ret;
2171 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, zns,
2172 0, 0);
2173
2174 /* Reset VLAN filter. */
2175 filter_vlan = 0;
2176 return ret;
2177 }
2178
2179 static int netlink_macfdb_update(struct interface *ifp, vlanid_t vid,
2180 struct ethaddr *mac, struct in_addr vtep_ip,
2181 int local, int cmd, uint8_t sticky)
2182 {
2183 struct zebra_ns *zns;
2184 struct {
2185 struct nlmsghdr n;
2186 struct ndmsg ndm;
2187 char buf[256];
2188 } req;
2189 int dst_alen;
2190 struct zebra_if *zif;
2191 struct interface *br_if;
2192 struct zebra_if *br_zif;
2193 char buf[ETHER_ADDR_STRLEN];
2194 int vid_present = 0, dst_present = 0;
2195 char vid_buf[20];
2196 char dst_buf[30];
2197 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
2198
2199 zns = zvrf->zns;
2200 zif = ifp->info;
2201 if ((br_if = zif->brslave_info.br_if) == NULL) {
2202 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2203 (cmd == RTM_NEWNEIGH) ? "add" : "del", ifp->name,
2204 ifp->ifindex);
2205 return -1;
2206 }
2207
2208 memset(&req, 0, sizeof(req));
2209
2210 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2211 req.n.nlmsg_flags = NLM_F_REQUEST;
2212 if (cmd == RTM_NEWNEIGH)
2213 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2214 req.n.nlmsg_type = cmd;
2215 req.ndm.ndm_family = AF_BRIDGE;
2216 req.ndm.ndm_flags |= NTF_SELF | NTF_MASTER;
2217 req.ndm.ndm_state = NUD_REACHABLE;
2218
2219 if (sticky)
2220 req.ndm.ndm_state |= NUD_NOARP;
2221 else
2222 req.ndm.ndm_flags |= NTF_EXT_LEARNED;
2223
2224 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2225 req.ndm.ndm_ifindex = ifp->ifindex;
2226 if (!local) {
2227 dst_alen = 4; // TODO: hardcoded
2228 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip, dst_alen);
2229 dst_present = 1;
2230 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip));
2231 }
2232 br_zif = (struct zebra_if *)br_if->info;
2233 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0) {
2234 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2235 vid_present = 1;
2236 sprintf(vid_buf, " VLAN %u", vid);
2237 }
2238 addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
2239
2240 if (IS_ZEBRA_DEBUG_KERNEL)
2241 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2242 nl_msg_type_to_str(cmd),
2243 nl_family_to_str(req.ndm.ndm_family), ifp->name,
2244 ifp->ifindex, vid_present ? vid_buf : "",
2245 sticky ? "sticky " : "",
2246 prefix_mac2str(mac, buf, sizeof(buf)),
2247 dst_present ? dst_buf : "");
2248
2249 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2250 0);
2251 }
2252
2253 #define NUD_VALID \
2254 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2255 | NUD_DELAY)
2256
2257 static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2258 {
2259 struct ndmsg *ndm;
2260 struct interface *ifp;
2261 struct zebra_if *zif;
2262 struct rtattr *tb[NDA_MAX + 1];
2263 struct interface *link_if;
2264 struct ethaddr mac;
2265 struct ipaddr ip;
2266 char buf[ETHER_ADDR_STRLEN];
2267 char buf2[INET6_ADDRSTRLEN];
2268 int mac_present = 0;
2269 uint8_t ext_learned;
2270 uint8_t router_flag;
2271
2272 ndm = NLMSG_DATA(h);
2273
2274 /* The interface should exist. */
2275 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
2276 ndm->ndm_ifindex);
2277 if (!ifp || !ifp->info)
2278 return 0;
2279
2280 zif = (struct zebra_if *)ifp->info;
2281
2282 /* Parse attributes and extract fields of interest. */
2283 memset(tb, 0, sizeof tb);
2284 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
2285
2286 if (!tb[NDA_DST]) {
2287 zlog_debug("%s family %s IF %s(%u) - no DST",
2288 nl_msg_type_to_str(h->nlmsg_type),
2289 nl_family_to_str(ndm->ndm_family), ifp->name,
2290 ndm->ndm_ifindex);
2291 return 0;
2292 }
2293
2294 memset(&ip, 0, sizeof(struct ipaddr));
2295 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
2296 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
2297
2298 /* Drop some "permanent" entries. */
2299 if (ndm->ndm_state & NUD_PERMANENT) {
2300 char b[16] = "169.254.0.1";
2301 struct in_addr ipv4_ll;
2302
2303 if (ndm->ndm_family != AF_INET)
2304 return 0;
2305
2306 if (!zif->v6_2_v4_ll_neigh_entry)
2307 return 0;
2308
2309 if (h->nlmsg_type != RTM_DELNEIGH)
2310 return 0;
2311
2312 inet_pton(AF_INET, b, &ipv4_ll);
2313 if (ipv4_ll.s_addr != ip.ip._v4_addr.s_addr)
2314 return 0;
2315
2316 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
2317 ifp, &zif->v6_2_v4_ll_addr6, true);
2318 return 0;
2319 }
2320
2321 /* The neighbor is present on an SVI. From this, we locate the
2322 * underlying
2323 * bridge because we're only interested in neighbors on a VxLAN bridge.
2324 * The bridge is located based on the nature of the SVI:
2325 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2326 * interface
2327 * and is linked to the bridge
2328 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2329 * inteface
2330 * itself
2331 */
2332 if (IS_ZEBRA_IF_VLAN(ifp)) {
2333 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
2334 zif->link_ifindex);
2335 if (!link_if)
2336 return 0;
2337 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
2338 link_if = ifp;
2339 else
2340 return 0;
2341
2342 memset(&mac, 0, sizeof(struct ethaddr));
2343 if (h->nlmsg_type == RTM_NEWNEIGH) {
2344 if (tb[NDA_LLADDR]) {
2345 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
2346 zlog_debug(
2347 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2348 nl_msg_type_to_str(h->nlmsg_type),
2349 nl_family_to_str(ndm->ndm_family),
2350 ifp->name, ndm->ndm_ifindex,
2351 (unsigned long)RTA_PAYLOAD(
2352 tb[NDA_LLADDR]));
2353 return 0;
2354 }
2355
2356 mac_present = 1;
2357 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
2358 }
2359
2360 ext_learned = (ndm->ndm_flags & NTF_EXT_LEARNED) ? 1 : 0;
2361 router_flag = (ndm->ndm_flags & NTF_ROUTER) ? 1 : 0;
2362
2363 if (IS_ZEBRA_DEBUG_KERNEL)
2364 zlog_debug(
2365 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2366 nl_msg_type_to_str(h->nlmsg_type),
2367 nl_family_to_str(ndm->ndm_family), ifp->name,
2368 ndm->ndm_ifindex,
2369 ipaddr2str(&ip, buf2, sizeof(buf2)),
2370 mac_present
2371 ? prefix_mac2str(&mac, buf, sizeof(buf))
2372 : "",
2373 ndm->ndm_state, ndm->ndm_flags);
2374
2375 /* If the neighbor state is valid for use, process as an add or
2376 * update
2377 * else process as a delete. Note that the delete handling may
2378 * result
2379 * in re-adding the neighbor if it is a valid "remote" neighbor.
2380 */
2381 if (ndm->ndm_state & NUD_VALID)
2382 return zebra_vxlan_handle_kernel_neigh_update(
2383 ifp, link_if, &ip, &mac, ndm->ndm_state,
2384 ext_learned, router_flag);
2385
2386 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2387 }
2388
2389 if (IS_ZEBRA_DEBUG_KERNEL)
2390 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2391 nl_msg_type_to_str(h->nlmsg_type),
2392 nl_family_to_str(ndm->ndm_family), ifp->name,
2393 ndm->ndm_ifindex,
2394 ipaddr2str(&ip, buf2, sizeof(buf2)));
2395
2396 /* Process the delete - it may result in re-adding the neighbor if it is
2397 * a valid "remote" neighbor.
2398 */
2399 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2400 }
2401
2402 static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2403 {
2404 int len;
2405 struct ndmsg *ndm;
2406
2407 if (h->nlmsg_type != RTM_NEWNEIGH)
2408 return 0;
2409
2410 /* Length validity. */
2411 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2412 if (len < 0)
2413 return -1;
2414
2415 /* We are interested only in AF_INET or AF_INET6 notifications. */
2416 ndm = NLMSG_DATA(h);
2417 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
2418 return 0;
2419
2420 return netlink_neigh_change(h, len);
2421 }
2422
2423 /* Request for IP neighbor information from the kernel */
2424 static int netlink_request_neigh(struct zebra_ns *zns, int family, int type,
2425 ifindex_t ifindex)
2426 {
2427 struct {
2428 struct nlmsghdr n;
2429 struct ndmsg ndm;
2430 char buf[256];
2431 } req;
2432
2433 /* Form the request, specifying filter (rtattr) if needed. */
2434 memset(&req, 0, sizeof(req));
2435 req.n.nlmsg_type = type;
2436 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2437 req.ndm.ndm_family = family;
2438 if (ifindex)
2439 addattr32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
2440
2441 return netlink_request(&zns->netlink_cmd, &req.n);
2442 }
2443
2444 /*
2445 * IP Neighbor table read using netlink interface. This is invoked
2446 * at startup.
2447 */
2448 int netlink_neigh_read(struct zebra_ns *zns)
2449 {
2450 int ret;
2451
2452 /* Get IP neighbor table. */
2453 ret = netlink_request_neigh(zns, AF_UNSPEC, RTM_GETNEIGH, 0);
2454 if (ret < 0)
2455 return ret;
2456 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, zns, 0,
2457 1);
2458
2459 return ret;
2460 }
2461
2462 /*
2463 * IP Neighbor table read using netlink interface. This is for a specific
2464 * VLAN device.
2465 */
2466 int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2467 {
2468 int ret = 0;
2469
2470 ret = netlink_request_neigh(zns, AF_UNSPEC, RTM_GETNEIGH,
2471 vlan_if->ifindex);
2472 if (ret < 0)
2473 return ret;
2474 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, zns, 0,
2475 0);
2476
2477 return ret;
2478 }
2479
2480 int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
2481 {
2482 int len;
2483 struct ndmsg *ndm;
2484
2485 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH))
2486 return 0;
2487
2488 /* Length validity. */
2489 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2490 if (len < 0) {
2491 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2492 __PRETTY_FUNCTION__, h->nlmsg_len,
2493 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
2494 return -1;
2495 }
2496
2497 /* Is this a notification for the MAC FDB or IP neighbor table? */
2498 ndm = NLMSG_DATA(h);
2499 if (ndm->ndm_family == AF_BRIDGE)
2500 return netlink_macfdb_change(h, len, ns_id);
2501
2502 if (ndm->ndm_type != RTN_UNICAST)
2503 return 0;
2504
2505 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
2506 return netlink_ipneigh_change(h, len, ns_id);
2507 else {
2508 flog_warn(
2509 EC_ZEBRA_UNKNOWN_FAMILY,
2510 "Invalid address family: %u received from kernel neighbor change: %s",
2511 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
2512 return 0;
2513 }
2514
2515 return 0;
2516 }
2517
2518 static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip,
2519 struct ethaddr *mac, uint8_t flags,
2520 uint16_t state, int cmd)
2521 {
2522 struct {
2523 struct nlmsghdr n;
2524 struct ndmsg ndm;
2525 char buf[256];
2526 } req;
2527 int ipa_len;
2528
2529 struct zebra_ns *zns;
2530 char buf[INET6_ADDRSTRLEN];
2531 char buf2[ETHER_ADDR_STRLEN];
2532 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
2533
2534 zns = zvrf->zns;
2535 memset(&req, 0, sizeof(req));
2536
2537 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2538 req.n.nlmsg_flags = NLM_F_REQUEST;
2539 if (cmd == RTM_NEWNEIGH)
2540 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2541 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
2542 req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
2543 req.ndm.ndm_state = state;
2544 req.ndm.ndm_ifindex = ifp->ifindex;
2545 req.ndm.ndm_type = RTN_UNICAST;
2546 req.ndm.ndm_flags = flags;
2547
2548 ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
2549 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
2550 if (mac)
2551 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2552
2553 if (IS_ZEBRA_DEBUG_KERNEL)
2554 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
2555 nl_msg_type_to_str(cmd),
2556 nl_family_to_str(req.ndm.ndm_family), ifp->name,
2557 ifp->ifindex, ipaddr2str(ip, buf, sizeof(buf)),
2558 mac ? prefix_mac2str(mac, buf2, sizeof(buf2))
2559 : "null", flags);
2560
2561 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2562 0);
2563 }
2564
2565 int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
2566 struct in_addr vtep_ip, uint8_t sticky)
2567 {
2568 return netlink_macfdb_update(ifp, vid, mac, vtep_ip, 0, RTM_NEWNEIGH,
2569 sticky);
2570 }
2571
2572 int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
2573 struct in_addr vtep_ip, int local)
2574 {
2575 return netlink_macfdb_update(ifp, vid, mac, vtep_ip, local,
2576 RTM_DELNEIGH, 0);
2577 }
2578
2579 int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
2580 struct ethaddr *mac, uint8_t flags)
2581 {
2582 return netlink_neigh_update2(ifp, ip, mac, flags,
2583 NUD_NOARP, RTM_NEWNEIGH);
2584 }
2585
2586 int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2587 {
2588 return netlink_neigh_update2(ifp, ip, NULL, 0, 0, RTM_DELNEIGH);
2589 }
2590
2591 /*
2592 * MPLS label forwarding table change via netlink interface.
2593 */
2594 int netlink_mpls_multipath(int cmd, zebra_lsp_t *lsp)
2595 {
2596 mpls_lse_t lse;
2597 zebra_nhlfe_t *nhlfe;
2598 struct nexthop *nexthop = NULL;
2599 unsigned int nexthop_num;
2600 const char *routedesc;
2601 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
2602 int route_type;
2603
2604 struct {
2605 struct nlmsghdr n;
2606 struct rtmsg r;
2607 char buf[NL_PKT_BUF_SIZE];
2608 } req;
2609
2610 memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE);
2611
2612 /*
2613 * Count # nexthops so we can decide whether to use singlepath
2614 * or multipath case.
2615 */
2616 nexthop_num = 0;
2617 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
2618 nexthop = nhlfe->nexthop;
2619 if (!nexthop)
2620 continue;
2621 if (cmd == RTM_NEWROUTE) {
2622 /* Count all selected NHLFEs */
2623 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2624 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2625 nexthop_num++;
2626 } else /* DEL */
2627 {
2628 /* Count all installed NHLFEs */
2629 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
2630 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2631 nexthop_num++;
2632 }
2633 }
2634
2635 if ((nexthop_num == 0) || (!lsp->best_nhlfe && (cmd != RTM_DELROUTE)))
2636 return 0;
2637
2638 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2639 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
2640 req.n.nlmsg_type = cmd;
2641 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
2642
2643 req.r.rtm_family = AF_MPLS;
2644 req.r.rtm_table = RT_TABLE_MAIN;
2645 req.r.rtm_dst_len = MPLS_LABEL_LEN_BITS;
2646 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
2647 req.r.rtm_type = RTN_UNICAST;
2648
2649 if (cmd == RTM_NEWROUTE) {
2650 /* We do a replace to handle update. */
2651 req.n.nlmsg_flags |= NLM_F_REPLACE;
2652
2653 /* set the protocol value if installing */
2654 route_type = re_type_from_lsp_type(lsp->best_nhlfe->type);
2655 req.r.rtm_protocol = zebra2proto(route_type);
2656 }
2657
2658 /* Fill destination */
2659 lse = mpls_lse_encode(lsp->ile.in_label, 0, 0, 1);
2660 addattr_l(&req.n, sizeof req, RTA_DST, &lse, sizeof(mpls_lse_t));
2661
2662 /* Fill nexthops (paths) based on single-path or multipath. The paths
2663 * chosen depend on the operation.
2664 */
2665 if (nexthop_num == 1 || multipath_num == 1) {
2666 routedesc = "single-path";
2667 _netlink_mpls_debug(cmd, lsp->ile.in_label, routedesc);
2668
2669 nexthop_num = 0;
2670 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
2671 nexthop = nhlfe->nexthop;
2672 if (!nexthop)
2673 continue;
2674
2675 if ((cmd == RTM_NEWROUTE
2676 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2677 && CHECK_FLAG(nexthop->flags,
2678 NEXTHOP_FLAG_ACTIVE)))
2679 || (cmd == RTM_DELROUTE
2680 && (CHECK_FLAG(nhlfe->flags,
2681 NHLFE_FLAG_INSTALLED)
2682 && CHECK_FLAG(nexthop->flags,
2683 NEXTHOP_FLAG_FIB)))) {
2684 /* Add the gateway */
2685 _netlink_mpls_build_singlepath(routedesc, nhlfe,
2686 &req.n, &req.r,
2687 sizeof req, cmd);
2688 nexthop_num++;
2689 break;
2690 }
2691 }
2692 } else /* Multipath case */
2693 {
2694 char buf[NL_PKT_BUF_SIZE];
2695 struct rtattr *rta = (void *)buf;
2696 struct rtnexthop *rtnh;
2697 union g_addr *src1 = NULL;
2698
2699 rta->rta_type = RTA_MULTIPATH;
2700 rta->rta_len = RTA_LENGTH(0);
2701 rtnh = RTA_DATA(rta);
2702
2703 routedesc = "multipath";
2704 _netlink_mpls_debug(cmd, lsp->ile.in_label, routedesc);
2705
2706 nexthop_num = 0;
2707 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
2708 nexthop = nhlfe->nexthop;
2709 if (!nexthop)
2710 continue;
2711
2712 if (nexthop_num >= multipath_num)
2713 break;
2714
2715 if ((cmd == RTM_NEWROUTE
2716 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2717 && CHECK_FLAG(nexthop->flags,
2718 NEXTHOP_FLAG_ACTIVE)))
2719 || (cmd == RTM_DELROUTE
2720 && (CHECK_FLAG(nhlfe->flags,
2721 NHLFE_FLAG_INSTALLED)
2722 && CHECK_FLAG(nexthop->flags,
2723 NEXTHOP_FLAG_FIB)))) {
2724 nexthop_num++;
2725
2726 /* Build the multipath */
2727 _netlink_mpls_build_multipath(routedesc, nhlfe,
2728 rta, rtnh, &req.r,
2729 &src1);
2730 rtnh = RTNH_NEXT(rtnh);
2731 }
2732 }
2733
2734 /* Add the multipath */
2735 if (rta->rta_len > RTA_LENGTH(0))
2736 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
2737 RTA_DATA(rta), RTA_PAYLOAD(rta));
2738 }
2739
2740 /* Talk to netlink socket. */
2741 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2742 0);
2743 }
2744 #endif /* HAVE_NETLINK */