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