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