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