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