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