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