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