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