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