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