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