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