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