]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
Merge branch 'pull/3165'
[mirror_frr.git] / zebra / zebra_rib.c
1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 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 #include "command.h"
24 #include "if.h"
25 #include "linklist.h"
26 #include "log.h"
27 #include "log_int.h"
28 #include "memory.h"
29 #include "mpls.h"
30 #include "nexthop.h"
31 #include "prefix.h"
32 #include "prefix.h"
33 #include "routemap.h"
34 #include "sockunion.h"
35 #include "srcdest_table.h"
36 #include "table.h"
37 #include "thread.h"
38 #include "vrf.h"
39 #include "workqueue.h"
40
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
46 #include "zebra/rt.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
55
56 DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
57 (rn, reason))
58
59 /* Should we allow non Quagga processes to delete our routes */
60 extern int allow_delete;
61
62 /* Each route type's string and default distance value. */
63 static const struct {
64 int key;
65 int distance;
66 } route_info[ZEBRA_ROUTE_MAX] = {
67 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
68 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
69 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
70 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
71 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
72 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
73 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
74 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
75 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
76 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
77 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255},
78 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90},
79 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10},
80 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255},
81 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255},
82 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150},
83 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150},
84 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20},
86 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20},
88 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20},
89 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100},
90 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150},
91
92 /* no entry/default: 150 */
93 };
94
95 /* RPF lookup behaviour */
96 static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
97
98
99 static void __attribute__((format(printf, 5, 6)))
100 _rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn,
101 int priority, const char *msgfmt, ...)
102 {
103 char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")];
104 char msgbuf[512];
105 va_list ap;
106
107 va_start(ap, msgfmt);
108 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
109 va_end(ap);
110
111 if (rn) {
112 rib_table_info_t *info = srcdest_rnode_table_info(rn);
113 srcdest_rnode2str(rn, buf, sizeof(buf));
114
115 if (info->safi == SAFI_MULTICAST)
116 strcat(buf, " (MRIB)");
117 } else {
118 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
119 }
120
121 zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
122 }
123
124 #define rnode_debug(node, vrf_id, ...) \
125 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
126 #define rnode_info(node, ...) \
127 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
128
129 uint8_t route_distance(int type)
130 {
131 uint8_t distance;
132
133 if ((unsigned)type >= array_size(route_info))
134 distance = 150;
135 else
136 distance = route_info[type].distance;
137
138 return distance;
139 }
140
141 int is_zebra_valid_kernel_table(uint32_t table_id)
142 {
143 #ifdef linux
144 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
145 || (table_id == RT_TABLE_COMPAT))
146 return 0;
147 #endif
148
149 return 1;
150 }
151
152 int is_zebra_main_routing_table(uint32_t table_id)
153 {
154 if ((table_id == RT_TABLE_MAIN)
155 || (table_id == zebrad.rtm_table_default))
156 return 1;
157 return 0;
158 }
159
160 int zebra_check_addr(const struct prefix *p)
161 {
162 if (p->family == AF_INET) {
163 uint32_t addr;
164
165 addr = p->u.prefix4.s_addr;
166 addr = ntohl(addr);
167
168 if (IPV4_NET127(addr) || IN_CLASSD(addr)
169 || IPV4_LINKLOCAL(addr))
170 return 0;
171 }
172 if (p->family == AF_INET6) {
173 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
174 return 0;
175 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
176 return 0;
177 }
178 return 1;
179 }
180
181 /* Add nexthop to the end of a rib node's nexthop list */
182 void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop)
183 {
184 nexthop_add(&re->ng.nexthop, nexthop);
185 re->nexthop_num++;
186 }
187
188
189 /**
190 * copy_nexthop - copy a nexthop to the rib structure.
191 */
192 void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh)
193 {
194 assert(!re->ng.nexthop);
195 copy_nexthops(&re->ng.nexthop, nh, NULL);
196 for (struct nexthop *nexthop = nh; nexthop; nexthop = nexthop->next)
197 re->nexthop_num++;
198 }
199
200 /* Delete specified nexthop from the list. */
201 void route_entry_nexthop_delete(struct route_entry *re, struct nexthop *nexthop)
202 {
203 if (nexthop->next)
204 nexthop->next->prev = nexthop->prev;
205 if (nexthop->prev)
206 nexthop->prev->next = nexthop->next;
207 else
208 re->ng.nexthop = nexthop->next;
209 re->nexthop_num--;
210 }
211
212
213 struct nexthop *route_entry_nexthop_ifindex_add(struct route_entry *re,
214 ifindex_t ifindex,
215 vrf_id_t nh_vrf_id)
216 {
217 struct nexthop *nexthop;
218
219 nexthop = nexthop_new();
220 nexthop->type = NEXTHOP_TYPE_IFINDEX;
221 nexthop->ifindex = ifindex;
222 nexthop->vrf_id = nh_vrf_id;
223
224 route_entry_nexthop_add(re, nexthop);
225
226 return nexthop;
227 }
228
229 struct nexthop *route_entry_nexthop_ipv4_add(struct route_entry *re,
230 struct in_addr *ipv4,
231 struct in_addr *src,
232 vrf_id_t nh_vrf_id)
233 {
234 struct nexthop *nexthop;
235
236 nexthop = nexthop_new();
237 nexthop->type = NEXTHOP_TYPE_IPV4;
238 nexthop->vrf_id = nh_vrf_id;
239 nexthop->gate.ipv4 = *ipv4;
240 if (src)
241 nexthop->src.ipv4 = *src;
242
243 route_entry_nexthop_add(re, nexthop);
244
245 return nexthop;
246 }
247
248 struct nexthop *route_entry_nexthop_ipv4_ifindex_add(struct route_entry *re,
249 struct in_addr *ipv4,
250 struct in_addr *src,
251 ifindex_t ifindex,
252 vrf_id_t nh_vrf_id)
253 {
254 struct nexthop *nexthop;
255 struct interface *ifp;
256
257 nexthop = nexthop_new();
258 nexthop->vrf_id = nh_vrf_id;
259 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
260 nexthop->gate.ipv4 = *ipv4;
261 if (src)
262 nexthop->src.ipv4 = *src;
263 nexthop->ifindex = ifindex;
264 ifp = if_lookup_by_index(nexthop->ifindex, nh_vrf_id);
265 /*Pending: need to think if null ifp here is ok during bootup?
266 There was a crash because ifp here was coming to be NULL */
267 if (ifp)
268 if (connected_is_unnumbered(ifp)
269 || CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
270 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
271 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
272 }
273
274 route_entry_nexthop_add(re, nexthop);
275
276 return nexthop;
277 }
278
279 struct nexthop *route_entry_nexthop_ipv6_add(struct route_entry *re,
280 struct in6_addr *ipv6,
281 vrf_id_t nh_vrf_id)
282 {
283 struct nexthop *nexthop;
284
285 nexthop = nexthop_new();
286 nexthop->vrf_id = nh_vrf_id;
287 nexthop->type = NEXTHOP_TYPE_IPV6;
288 nexthop->gate.ipv6 = *ipv6;
289
290 route_entry_nexthop_add(re, nexthop);
291
292 return nexthop;
293 }
294
295 struct nexthop *route_entry_nexthop_ipv6_ifindex_add(struct route_entry *re,
296 struct in6_addr *ipv6,
297 ifindex_t ifindex,
298 vrf_id_t nh_vrf_id)
299 {
300 struct nexthop *nexthop;
301
302 nexthop = nexthop_new();
303 nexthop->vrf_id = nh_vrf_id;
304 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
305 nexthop->gate.ipv6 = *ipv6;
306 nexthop->ifindex = ifindex;
307 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
308 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
309 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
310 }
311
312 route_entry_nexthop_add(re, nexthop);
313
314 return nexthop;
315 }
316
317 struct nexthop *route_entry_nexthop_blackhole_add(struct route_entry *re,
318 enum blackhole_type bh_type)
319 {
320 struct nexthop *nexthop;
321
322 nexthop = nexthop_new();
323 nexthop->vrf_id = VRF_DEFAULT;
324 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
325 nexthop->bh_type = bh_type;
326
327 route_entry_nexthop_add(re, nexthop);
328
329 return nexthop;
330 }
331
332 static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
333 struct nexthop *nexthop)
334 {
335 struct nexthop *resolved_hop;
336
337 resolved_hop = nexthop_new();
338 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
339
340 resolved_hop->vrf_id = nexthop->vrf_id;
341 switch (newhop->type) {
342 case NEXTHOP_TYPE_IPV4:
343 case NEXTHOP_TYPE_IPV4_IFINDEX:
344 /* If the resolving route specifies a gateway, use it */
345 resolved_hop->type = newhop->type;
346 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
347
348 if (newhop->ifindex) {
349 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
350 resolved_hop->ifindex = newhop->ifindex;
351 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
352 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
353 }
354 break;
355 case NEXTHOP_TYPE_IPV6:
356 case NEXTHOP_TYPE_IPV6_IFINDEX:
357 resolved_hop->type = newhop->type;
358 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
359
360 if (newhop->ifindex) {
361 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
362 resolved_hop->ifindex = newhop->ifindex;
363 }
364 break;
365 case NEXTHOP_TYPE_IFINDEX:
366 /* If the resolving route is an interface route,
367 * it means the gateway we are looking up is connected
368 * to that interface. (The actual network is _not_ onlink).
369 * Therefore, the resolved route should have the original
370 * gateway as nexthop as it is directly connected.
371 *
372 * On Linux, we have to set the onlink netlink flag because
373 * otherwise, the kernel won't accept the route.
374 */
375 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
376 if (afi == AFI_IP) {
377 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
378 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
379 } else if (afi == AFI_IP6) {
380 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
381 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
382 }
383 resolved_hop->ifindex = newhop->ifindex;
384 break;
385 case NEXTHOP_TYPE_BLACKHOLE:
386 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
387 resolved_hop->bh_type = nexthop->bh_type;
388 break;
389 }
390
391 /* Copy labels of the resolved route */
392 if (newhop->nh_label)
393 nexthop_add_labels(resolved_hop, newhop->nh_label_type,
394 newhop->nh_label->num_labels,
395 &newhop->nh_label->label[0]);
396
397 resolved_hop->rparent = nexthop;
398 nexthop_add(&nexthop->resolved, resolved_hop);
399 }
400
401 /* If force flag is not set, do not modify falgs at all for uninstall
402 the route from FIB. */
403 static int nexthop_active(afi_t afi, struct route_entry *re,
404 struct nexthop *nexthop, int set,
405 struct route_node *top)
406 {
407 struct prefix p;
408 struct route_table *table;
409 struct route_node *rn;
410 struct route_entry *match = NULL;
411 int resolved;
412 struct nexthop *newhop;
413 struct interface *ifp;
414 rib_dest_t *dest;
415
416 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
417 || nexthop->type == NEXTHOP_TYPE_IPV6)
418 nexthop->ifindex = 0;
419
420 if (set) {
421 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
422 nexthops_free(nexthop->resolved);
423 nexthop->resolved = NULL;
424 re->nexthop_mtu = 0;
425 }
426
427 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
428 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN_RVTEP))
429 return 1;
430
431 /* Skip nexthops that have been filtered out due to route-map */
432 /* The nexthops are specific to this route and so the same */
433 /* nexthop for a different route may not have this flag set */
434 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED)) {
435 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
436 zlog_debug("\t%s: Nexthop Filtered",
437 __PRETTY_FUNCTION__);
438 return 0;
439 }
440
441 /*
442 * Check to see if we should trust the passed in information
443 * for UNNUMBERED interfaces as that we won't find the GW
444 * address in the routing table.
445 */
446 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
447 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
448 if ((ifp && connected_is_unnumbered(ifp))
449 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
450 if (if_is_operative(ifp))
451 return 1;
452 else {
453 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
454 zlog_debug(
455 "\t%s: Onlink and interface %s is not operative",
456 __PRETTY_FUNCTION__, ifp->name);
457 return 0;
458 }
459 } else {
460 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
461 zlog_debug(
462 "\t%s: Interface %s is not unnumbered",
463 __PRETTY_FUNCTION__,
464 ifp ? ifp->name : "Unknown");
465 return 0;
466 }
467 }
468
469 /* Make lookup prefix. */
470 memset(&p, 0, sizeof(struct prefix));
471 switch (afi) {
472 case AFI_IP:
473 p.family = AF_INET;
474 p.prefixlen = IPV4_MAX_PREFIXLEN;
475 p.u.prefix4 = nexthop->gate.ipv4;
476 break;
477 case AFI_IP6:
478 p.family = AF_INET6;
479 p.prefixlen = IPV6_MAX_PREFIXLEN;
480 p.u.prefix6 = nexthop->gate.ipv6;
481 break;
482 default:
483 assert(afi != AFI_IP && afi != AFI_IP6);
484 break;
485 }
486 /* Lookup table. */
487 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
488 if (!table) {
489 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
490 zlog_debug("\t%s: Table not found",
491 __PRETTY_FUNCTION__);
492 return 0;
493 }
494
495 rn = route_node_match(table, (struct prefix *)&p);
496 while (rn) {
497 route_unlock_node(rn);
498
499 /* Lookup should halt if we've matched against ourselves ('top',
500 * if specified) - i.e., we cannot have a nexthop NH1 is
501 * resolved by a route NH1. The exception is if the route is a
502 * host route.
503 */
504 if (top && rn == top)
505 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
506 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
507 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
508 zlog_debug(
509 "\t%s: Matched against ourself and prefix length is not max bit length",
510 __PRETTY_FUNCTION__);
511 return 0;
512 }
513
514 /* Pick up selected route. */
515 /* However, do not resolve over default route unless explicitly
516 * allowed. */
517 if (is_default_prefix(&rn->p)
518 && !rnh_resolve_via_default(p.family)) {
519 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
520 zlog_debug(
521 "\t:%s: Resolved against default route",
522 __PRETTY_FUNCTION__);
523 return 0;
524 }
525
526 dest = rib_dest_from_rnode(rn);
527 if (dest && dest->selected_fib
528 && !CHECK_FLAG(dest->selected_fib->status,
529 ROUTE_ENTRY_REMOVED)
530 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
531 match = dest->selected_fib;
532
533 /* If there is no selected route or matched route is EGP, go up
534 tree. */
535 if (!match) {
536 do {
537 rn = rn->parent;
538 } while (rn && rn->info == NULL);
539 if (rn)
540 route_lock_node(rn);
541
542 continue;
543 }
544
545 if (match->type == ZEBRA_ROUTE_CONNECT) {
546 /* Directly point connected route. */
547 newhop = match->ng.nexthop;
548 if (newhop) {
549 if (nexthop->type == NEXTHOP_TYPE_IPV4
550 || nexthop->type == NEXTHOP_TYPE_IPV6)
551 nexthop->ifindex = newhop->ifindex;
552 }
553 return 1;
554 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
555 resolved = 0;
556 for (ALL_NEXTHOPS(match->ng, newhop)) {
557 if (!CHECK_FLAG(newhop->flags,
558 NEXTHOP_FLAG_FIB))
559 continue;
560 if (CHECK_FLAG(newhop->flags,
561 NEXTHOP_FLAG_RECURSIVE))
562 continue;
563
564 if (set) {
565 SET_FLAG(nexthop->flags,
566 NEXTHOP_FLAG_RECURSIVE);
567 SET_FLAG(re->status,
568 ROUTE_ENTRY_NEXTHOPS_CHANGED);
569 nexthop_set_resolved(afi, newhop,
570 nexthop);
571 }
572 resolved = 1;
573 }
574 if (resolved && set)
575 re->nexthop_mtu = match->mtu;
576 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
577 zlog_debug("\t%s: Recursion failed to find",
578 __PRETTY_FUNCTION__);
579 return resolved;
580 } else if (re->type == ZEBRA_ROUTE_STATIC) {
581 resolved = 0;
582 for (ALL_NEXTHOPS(match->ng, newhop)) {
583 if (!CHECK_FLAG(newhop->flags,
584 NEXTHOP_FLAG_FIB))
585 continue;
586
587 if (set) {
588 SET_FLAG(nexthop->flags,
589 NEXTHOP_FLAG_RECURSIVE);
590 nexthop_set_resolved(afi, newhop,
591 nexthop);
592 }
593 resolved = 1;
594 }
595 if (resolved && set)
596 re->nexthop_mtu = match->mtu;
597
598 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
599 zlog_debug(
600 "\t%s: Static route unable to resolve",
601 __PRETTY_FUNCTION__);
602 return resolved;
603 } else {
604 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
605 zlog_debug("\t%s: Route Type %s has not turned on recursion",
606 __PRETTY_FUNCTION__,
607 zebra_route_string(re->type));
608 if (re->type == ZEBRA_ROUTE_BGP &&
609 !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
610 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
611 }
612 return 0;
613 }
614 }
615 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
616 zlog_debug("\t%s: Nexthop did not lookup in table",
617 __PRETTY_FUNCTION__);
618 return 0;
619 }
620
621 struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
622 union g_addr *addr, struct route_node **rn_out)
623 {
624 struct prefix p;
625 struct route_table *table;
626 struct route_node *rn;
627 struct route_entry *match = NULL;
628 struct nexthop *newhop;
629
630 /* Lookup table. */
631 table = zebra_vrf_table(afi, safi, vrf_id);
632 if (!table)
633 return 0;
634
635 memset(&p, 0, sizeof(struct prefix));
636 p.family = afi;
637 if (afi == AFI_IP) {
638 p.u.prefix4 = addr->ipv4;
639 p.prefixlen = IPV4_MAX_PREFIXLEN;
640 } else {
641 p.u.prefix6 = addr->ipv6;
642 p.prefixlen = IPV6_MAX_PREFIXLEN;
643 }
644
645 rn = route_node_match(table, (struct prefix *)&p);
646
647 while (rn) {
648 rib_dest_t *dest;
649
650 route_unlock_node(rn);
651
652 dest = rib_dest_from_rnode(rn);
653 if (dest && dest->selected_fib
654 && !CHECK_FLAG(dest->selected_fib->status,
655 ROUTE_ENTRY_REMOVED))
656 match = dest->selected_fib;
657
658 /* If there is no selected route or matched route is EGP, go up
659 tree. */
660 if (!match) {
661 do {
662 rn = rn->parent;
663 } while (rn && rn->info == NULL);
664 if (rn)
665 route_lock_node(rn);
666 } else {
667 if (match->type != ZEBRA_ROUTE_CONNECT) {
668 int found = 0;
669 for (ALL_NEXTHOPS(match->ng, newhop))
670 if (CHECK_FLAG(newhop->flags,
671 NEXTHOP_FLAG_FIB)) {
672 found = 1;
673 break;
674 }
675 if (!found)
676 return NULL;
677 }
678
679 if (rn_out)
680 *rn_out = rn;
681 return match;
682 }
683 }
684 return NULL;
685 }
686
687 struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
688 struct in_addr addr,
689 struct route_node **rn_out)
690 {
691 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
692 struct route_node *m_rn = NULL, *u_rn = NULL;
693 union g_addr gaddr = {.ipv4 = addr};
694
695 switch (ipv4_multicast_mode) {
696 case MCAST_MRIB_ONLY:
697 return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
698 rn_out);
699 case MCAST_URIB_ONLY:
700 return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
701 case MCAST_NO_CONFIG:
702 case MCAST_MIX_MRIB_FIRST:
703 re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
704 &m_rn);
705 if (!mre)
706 re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
707 &gaddr, &u_rn);
708 break;
709 case MCAST_MIX_DISTANCE:
710 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
711 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
712 if (mre && ure)
713 re = ure->distance < mre->distance ? ure : mre;
714 else if (mre)
715 re = mre;
716 else if (ure)
717 re = ure;
718 break;
719 case MCAST_MIX_PFXLEN:
720 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
721 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
722 if (mre && ure)
723 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
724 else if (mre)
725 re = mre;
726 else if (ure)
727 re = ure;
728 break;
729 }
730
731 if (rn_out)
732 *rn_out = (re == mre) ? m_rn : u_rn;
733
734 if (IS_ZEBRA_DEBUG_RIB) {
735 char buf[BUFSIZ];
736 inet_ntop(AF_INET, &addr, buf, BUFSIZ);
737
738 zlog_debug("%s: %s: vrf: %u found %s, using %s",
739 __func__, buf, vrf_id,
740 mre ? (ure ? "MRIB+URIB" : "MRIB")
741 : ure ? "URIB" : "nothing",
742 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
743 }
744 return re;
745 }
746
747 void multicast_mode_ipv4_set(enum multicast_mode mode)
748 {
749 if (IS_ZEBRA_DEBUG_RIB)
750 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
751 mode);
752 ipv4_multicast_mode = mode;
753 }
754
755 enum multicast_mode multicast_mode_ipv4_get(void)
756 {
757 return ipv4_multicast_mode;
758 }
759
760 struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
761 {
762 struct route_table *table;
763 struct route_node *rn;
764 struct route_entry *match = NULL;
765 struct nexthop *nexthop;
766 rib_dest_t *dest;
767
768 /* Lookup table. */
769 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
770 if (!table)
771 return 0;
772
773 rn = route_node_lookup(table, (struct prefix *)p);
774
775 /* No route for this prefix. */
776 if (!rn)
777 return NULL;
778
779 /* Unlock node. */
780 route_unlock_node(rn);
781 dest = rib_dest_from_rnode(rn);
782
783 if (dest && dest->selected_fib
784 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
785 match = dest->selected_fib;
786
787 if (!match)
788 return NULL;
789
790 if (match->type == ZEBRA_ROUTE_CONNECT)
791 return match;
792
793 for (ALL_NEXTHOPS(match->ng, nexthop))
794 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
795 return match;
796
797 return NULL;
798 }
799
800 /*
801 * This clone function, unlike its original rib_lookup_ipv4(), checks
802 * if specified IPv4 route record (prefix/mask -> gate) exists in
803 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
804 *
805 * Return values:
806 * -1: error
807 * 0: exact match found
808 * 1: a match was found with a different gate
809 * 2: connected route found
810 * 3: no matches found
811 */
812 int rib_lookup_ipv4_route(struct prefix_ipv4 *p, union sockunion *qgate,
813 vrf_id_t vrf_id)
814 {
815 struct route_table *table;
816 struct route_node *rn;
817 struct route_entry *match = NULL;
818 struct nexthop *nexthop;
819 int nexthops_active;
820 rib_dest_t *dest;
821
822 /* Lookup table. */
823 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
824 if (!table)
825 return ZEBRA_RIB_LOOKUP_ERROR;
826
827 /* Scan the RIB table for exactly matching RIB entry. */
828 rn = route_node_lookup(table, (struct prefix *)p);
829
830 /* No route for this prefix. */
831 if (!rn)
832 return ZEBRA_RIB_NOTFOUND;
833
834 /* Unlock node. */
835 route_unlock_node(rn);
836 dest = rib_dest_from_rnode(rn);
837
838 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
839 */
840 if (dest && dest->selected_fib
841 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
842 match = dest->selected_fib;
843
844 /* None such found :( */
845 if (!match)
846 return ZEBRA_RIB_NOTFOUND;
847
848 if (match->type == ZEBRA_ROUTE_CONNECT)
849 return ZEBRA_RIB_FOUND_CONNECTED;
850
851 /* Ok, we have a cood candidate, let's check it's nexthop list... */
852 nexthops_active = 0;
853 for (ALL_NEXTHOPS(match->ng, nexthop))
854 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
855 nexthops_active = 1;
856 if (nexthop->gate.ipv4.s_addr == sockunion2ip(qgate))
857 return ZEBRA_RIB_FOUND_EXACT;
858 if (IS_ZEBRA_DEBUG_RIB) {
859 char gate_buf[INET_ADDRSTRLEN],
860 qgate_buf[INET_ADDRSTRLEN];
861 inet_ntop(AF_INET, &nexthop->gate.ipv4.s_addr,
862 gate_buf, INET_ADDRSTRLEN);
863 inet_ntop(AF_INET, &sockunion2ip(qgate),
864 qgate_buf, INET_ADDRSTRLEN);
865 zlog_debug("%s: qgate == %s, %s == %s",
866 __func__, qgate_buf,
867 nexthop->rparent ? "rgate" : "gate",
868 gate_buf);
869 }
870 }
871
872 if (nexthops_active)
873 return ZEBRA_RIB_FOUND_NOGATE;
874
875 return ZEBRA_RIB_NOTFOUND;
876 }
877
878 #define RIB_SYSTEM_ROUTE(R) \
879 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
880
881 #define RIB_KERNEL_ROUTE(R) \
882 ((R)->type == ZEBRA_ROUTE_KERNEL)
883
884 /* This function verifies reachability of one given nexthop, which can be
885 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
886 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
887 * nexthop->ifindex will be updated appropriately as well.
888 * An existing route map can turn (otherwise active) nexthop into inactive, but
889 * not vice versa.
890 *
891 * The return value is the final value of 'ACTIVE' flag.
892 */
893
894 static unsigned nexthop_active_check(struct route_node *rn,
895 struct route_entry *re,
896 struct nexthop *nexthop, int set)
897 {
898 struct interface *ifp;
899 route_map_result_t ret = RMAP_MATCH;
900 int family;
901 char buf[SRCDEST2STR_BUFFER];
902 const struct prefix *p, *src_p;
903 struct zebra_vrf *zvrf;
904
905 srcdest_rnode_prefixes(rn, &p, &src_p);
906
907 if (rn->p.family == AF_INET)
908 family = AFI_IP;
909 else if (rn->p.family == AF_INET6)
910 family = AFI_IP6;
911 else
912 family = 0;
913 switch (nexthop->type) {
914 case NEXTHOP_TYPE_IFINDEX:
915 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
916 if (ifp && if_is_operative(ifp))
917 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
918 else
919 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
920 break;
921 case NEXTHOP_TYPE_IPV4:
922 case NEXTHOP_TYPE_IPV4_IFINDEX:
923 family = AFI_IP;
924 if (nexthop_active(AFI_IP, re, nexthop, set, rn))
925 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
926 else
927 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
928 break;
929 case NEXTHOP_TYPE_IPV6:
930 family = AFI_IP6;
931 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
932 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
933 else
934 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
935 break;
936 case NEXTHOP_TYPE_IPV6_IFINDEX:
937 /* RFC 5549, v4 prefix with v6 NH */
938 if (rn->p.family != AF_INET)
939 family = AFI_IP6;
940 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
941 ifp = if_lookup_by_index(nexthop->ifindex,
942 nexthop->vrf_id);
943 if (ifp && if_is_operative(ifp))
944 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
945 else
946 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
947 } else {
948 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
949 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
950 else
951 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
952 }
953 break;
954 case NEXTHOP_TYPE_BLACKHOLE:
955 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
956 break;
957 default:
958 break;
959 }
960 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
961 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
962 zlog_debug("\t%s: Unable to find a active nexthop",
963 __PRETTY_FUNCTION__);
964 return 0;
965 }
966
967 /* XXX: What exactly do those checks do? Do we support
968 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
969 */
970 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
971 || (family == AFI_IP6 && p->family != AF_INET6))
972 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
973
974 /* The original code didn't determine the family correctly
975 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
976 * from the rib_table_info in those cases.
977 * Possibly it may be better to use only the rib_table_info
978 * in every case.
979 */
980 if (!family) {
981 rib_table_info_t *info;
982
983 info = srcdest_rnode_table_info(rn);
984 family = info->afi;
985 }
986
987 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
988
989 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
990 if (!zvrf) {
991 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
992 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
993 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
994 }
995
996 /* It'll get set if required inside */
997 ret = zebra_route_map_check(family, re->type, re->instance, p,
998 nexthop, zvrf, re->tag);
999 if (ret == RMAP_DENYMATCH) {
1000 if (IS_ZEBRA_DEBUG_RIB) {
1001 srcdest_rnode2str(rn, buf, sizeof(buf));
1002 zlog_debug(
1003 "%u:%s: Filtering out with NH out %s due to route map",
1004 re->vrf_id, buf,
1005 ifindex2ifname(nexthop->ifindex,
1006 nexthop->vrf_id));
1007 }
1008 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1009 }
1010 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1011 }
1012
1013 /* Iterate over all nexthops of the given RIB entry and refresh their
1014 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
1015 * nexthop is found to toggle the ACTIVE flag, the whole re structure
1016 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
1017 * transparently passed to nexthop_active_check().
1018 *
1019 * Return value is the new number of active nexthops.
1020 */
1021
1022 static int nexthop_active_update(struct route_node *rn, struct route_entry *re,
1023 int set)
1024 {
1025 struct nexthop *nexthop;
1026 union g_addr prev_src;
1027 unsigned int prev_active, new_active, old_num_nh;
1028 ifindex_t prev_index;
1029
1030 old_num_nh = re->nexthop_active_num;
1031
1032 re->nexthop_active_num = 0;
1033 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1034
1035 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
1036 /* No protocol daemon provides src and so we're skipping
1037 * tracking it */
1038 prev_src = nexthop->rmap_src;
1039 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1040 prev_index = nexthop->ifindex;
1041 if ((new_active = nexthop_active_check(rn, re, nexthop, set)))
1042 re->nexthop_active_num++;
1043 /* Don't allow src setting on IPv6 addr for now */
1044 if (prev_active != new_active || prev_index != nexthop->ifindex
1045 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
1046 && nexthop->type < NEXTHOP_TYPE_IPV6)
1047 && prev_src.ipv4.s_addr
1048 != nexthop->rmap_src.ipv4.s_addr)
1049 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
1050 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
1051 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
1052 &nexthop->rmap_src.ipv6)))) {
1053 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1054 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1055 }
1056 }
1057
1058 if (old_num_nh != re->nexthop_active_num)
1059 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1060
1061 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1062 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1063 }
1064
1065 return re->nexthop_active_num;
1066 }
1067
1068 /*
1069 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1070 * (nexthops) must have a label.
1071 */
1072 int zebra_rib_labeled_unicast(struct route_entry *re)
1073 {
1074 struct nexthop *nexthop = NULL;
1075
1076 if (re->type != ZEBRA_ROUTE_BGP)
1077 return 0;
1078
1079 for (ALL_NEXTHOPS(re->ng, nexthop))
1080 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
1081 return 0;
1082
1083 return 1;
1084 }
1085
1086 void kernel_route_rib_pass_fail(struct route_node *rn, const struct prefix *p,
1087 struct route_entry *re,
1088 enum zebra_dplane_status res)
1089 {
1090 struct nexthop *nexthop;
1091 char buf[PREFIX_STRLEN];
1092 rib_dest_t *dest;
1093
1094 dest = rib_dest_from_rnode(rn);
1095
1096 switch (res) {
1097 case ZEBRA_DPLANE_INSTALL_SUCCESS:
1098 dest->selected_fib = re;
1099 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1100 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1101 continue;
1102
1103 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1104 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1105 else
1106 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1107 }
1108 zsend_route_notify_owner(re, p, ZAPI_ROUTE_INSTALLED);
1109 break;
1110 case ZEBRA_DPLANE_INSTALL_FAILURE:
1111 /*
1112 * I am not sure this is the right thing to do here
1113 * but the code always set selected_fib before
1114 * this assignment was moved here.
1115 */
1116 dest->selected_fib = re;
1117
1118 zsend_route_notify_owner(re, p, ZAPI_ROUTE_FAIL_INSTALL);
1119 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
1120 "%u:%s: Route install failed", re->vrf_id,
1121 prefix2str(p, buf, sizeof(buf)));
1122 break;
1123 case ZEBRA_DPLANE_DELETE_SUCCESS:
1124 /*
1125 * The case where selected_fib is not re is
1126 * when we have received a system route
1127 * that is overriding our installed route
1128 * as such we should leave the selected_fib
1129 * pointer alone
1130 */
1131 if (dest->selected_fib == re)
1132 dest->selected_fib = NULL;
1133 for (ALL_NEXTHOPS(re->ng, nexthop))
1134 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1135
1136 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVED);
1137 break;
1138 case ZEBRA_DPLANE_DELETE_FAILURE:
1139 /*
1140 * Should we set this to NULL if the
1141 * delete fails?
1142 */
1143 dest->selected_fib = NULL;
1144 flog_err(EC_ZEBRA_DP_DELETE_FAIL,
1145 "%u:%s: Route Deletion failure", re->vrf_id,
1146 prefix2str(p, buf, sizeof(buf)));
1147
1148 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVE_FAIL);
1149 break;
1150 case ZEBRA_DPLANE_STATUS_NONE:
1151 break;
1152 }
1153 }
1154
1155 /* Update flag indicates whether this is a "replace" or not. Currently, this
1156 * is only used for IPv4.
1157 */
1158 void rib_install_kernel(struct route_node *rn, struct route_entry *re,
1159 struct route_entry *old)
1160 {
1161 struct nexthop *nexthop;
1162 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1163 const struct prefix *p, *src_p;
1164 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
1165
1166 srcdest_rnode_prefixes(rn, &p, &src_p);
1167
1168 if (info->safi != SAFI_UNICAST) {
1169 for (ALL_NEXTHOPS(re->ng, nexthop))
1170 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1171 return;
1172 } else {
1173 struct nexthop *prev;
1174
1175 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1176 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
1177 for (ALL_NEXTHOPS(re->ng, prev)) {
1178 if (prev == nexthop)
1179 break;
1180 if (nexthop_same_firsthop(nexthop, prev)) {
1181 SET_FLAG(nexthop->flags,
1182 NEXTHOP_FLAG_DUPLICATE);
1183 break;
1184 }
1185 }
1186 }
1187 }
1188
1189 /*
1190 * If this is a replace to a new RE let the originator of the RE
1191 * know that they've lost
1192 */
1193 if (old && (old != re) && (old->type != re->type))
1194 zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
1195
1196 /*
1197 * Make sure we update the FPM any time we send new information to
1198 * the kernel.
1199 */
1200 hook_call(rib_update, rn, "installing in kernel");
1201 switch (kernel_route_rib(rn, p, src_p, old, re)) {
1202 case ZEBRA_DPLANE_REQUEST_QUEUED:
1203 flog_err(
1204 EC_ZEBRA_DP_INVALID_RC,
1205 "No current known DataPlane interfaces can return this, please fix");
1206 break;
1207 case ZEBRA_DPLANE_REQUEST_FAILURE:
1208 flog_err(
1209 EC_ZEBRA_DP_INSTALL_FAIL,
1210 "No current known Rib Install Failure cases, please fix");
1211 break;
1212 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1213 zvrf->installs++;
1214 break;
1215 }
1216
1217 return;
1218 }
1219
1220 /* Uninstall the route from kernel. */
1221 void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
1222 {
1223 struct nexthop *nexthop;
1224 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1225 const struct prefix *p, *src_p;
1226 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
1227
1228 srcdest_rnode_prefixes(rn, &p, &src_p);
1229
1230 if (info->safi != SAFI_UNICAST) {
1231 for (ALL_NEXTHOPS(re->ng, nexthop))
1232 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1233 return;
1234 }
1235
1236 /*
1237 * Make sure we update the FPM any time we send new information to
1238 * the kernel.
1239 */
1240 hook_call(rib_update, rn, "uninstalling from kernel");
1241 switch (kernel_route_rib(rn, p, src_p, re, NULL)) {
1242 case ZEBRA_DPLANE_REQUEST_QUEUED:
1243 flog_err(
1244 EC_ZEBRA_DP_INVALID_RC,
1245 "No current known DataPlane interfaces can return this, please fix");
1246 break;
1247 case ZEBRA_DPLANE_REQUEST_FAILURE:
1248 flog_err(
1249 EC_ZEBRA_DP_INSTALL_FAIL,
1250 "No current known RIB Install Failure cases, please fix");
1251 break;
1252 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1253 if (zvrf)
1254 zvrf->removals++;
1255 break;
1256 }
1257
1258 return;
1259 }
1260
1261 /* Uninstall the route from kernel. */
1262 static void rib_uninstall(struct route_node *rn, struct route_entry *re)
1263 {
1264 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1265 rib_dest_t *dest = rib_dest_from_rnode(rn);
1266
1267 if (dest && dest->selected_fib == re) {
1268 if (info->safi == SAFI_UNICAST)
1269 hook_call(rib_update, rn, "rib_uninstall");
1270
1271 if (!RIB_SYSTEM_ROUTE(re))
1272 rib_uninstall_kernel(rn, re);
1273
1274 /* If labeled-unicast route, uninstall transit LSP. */
1275 if (zebra_rib_labeled_unicast(re))
1276 zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
1277 }
1278
1279 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1280 const struct prefix *p, *src_p;
1281
1282 srcdest_rnode_prefixes(rn, &p, &src_p);
1283
1284 redistribute_delete(p, src_p, re);
1285 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
1286 }
1287 }
1288
1289 /*
1290 * rib_can_delete_dest
1291 *
1292 * Returns TRUE if the given dest can be deleted from the table.
1293 */
1294 static int rib_can_delete_dest(rib_dest_t *dest)
1295 {
1296 if (dest->routes) {
1297 return 0;
1298 }
1299
1300 /*
1301 * Don't delete the dest if we have to update the FPM about this
1302 * prefix.
1303 */
1304 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
1305 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
1306 return 0;
1307
1308 return 1;
1309 }
1310
1311 /*
1312 * rib_gc_dest
1313 *
1314 * Garbage collect the rib dest corresponding to the given route node
1315 * if appropriate.
1316 *
1317 * Returns TRUE if the dest was deleted, FALSE otherwise.
1318 */
1319 int rib_gc_dest(struct route_node *rn)
1320 {
1321 rib_dest_t *dest;
1322
1323 dest = rib_dest_from_rnode(rn);
1324 if (!dest)
1325 return 0;
1326
1327 if (!rib_can_delete_dest(dest))
1328 return 0;
1329
1330 if (IS_ZEBRA_DEBUG_RIB) {
1331 struct zebra_vrf *zvrf;
1332
1333 zvrf = rib_dest_vrf(dest);
1334 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
1335 }
1336
1337 dest->rnode = NULL;
1338 XFREE(MTYPE_RIB_DEST, dest);
1339 rn->info = NULL;
1340
1341 /*
1342 * Release the one reference that we keep on the route node.
1343 */
1344 route_unlock_node(rn);
1345 return 1;
1346 }
1347
1348 static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1349 struct route_entry *new)
1350 {
1351 rib_dest_t *dest = rib_dest_from_rnode(rn);
1352
1353 hook_call(rib_update, rn, "new route selected");
1354
1355 /* Update real nexthop. This may actually determine if nexthop is active
1356 * or not. */
1357 if (!nexthop_active_update(rn, new, 1)) {
1358 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1359 return;
1360 }
1361
1362 if (IS_ZEBRA_DEBUG_RIB) {
1363 char buf[SRCDEST2STR_BUFFER];
1364 srcdest_rnode2str(rn, buf, sizeof(buf));
1365 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1366 zvrf_id(zvrf), buf, rn, new, new->type);
1367 }
1368
1369 /* If labeled-unicast route, install transit LSP. */
1370 if (zebra_rib_labeled_unicast(new))
1371 zebra_mpls_lsp_install(zvrf, rn, new);
1372
1373 if (!RIB_SYSTEM_ROUTE(new))
1374 rib_install_kernel(rn, new, NULL);
1375 else
1376 dest->selected_fib = new;
1377
1378 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1379 }
1380
1381 static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1382 struct route_entry *old)
1383 {
1384 rib_dest_t *dest = rib_dest_from_rnode(rn);
1385 hook_call(rib_update, rn, "removing existing route");
1386
1387 /* Uninstall from kernel. */
1388 if (IS_ZEBRA_DEBUG_RIB) {
1389 char buf[SRCDEST2STR_BUFFER];
1390 srcdest_rnode2str(rn, buf, sizeof(buf));
1391 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1392 zvrf_id(zvrf), buf, rn, old, old->type);
1393 }
1394
1395 /* If labeled-unicast route, uninstall transit LSP. */
1396 if (zebra_rib_labeled_unicast(old))
1397 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1398
1399 if (!RIB_SYSTEM_ROUTE(old))
1400 rib_uninstall_kernel(rn, old);
1401 else {
1402 /*
1403 * We are setting this to NULL here
1404 * because that is what we traditionally
1405 * have been doing. I am not positive
1406 * that this is the right thing to do
1407 * but let's leave the code alone
1408 * for the RIB_SYSTEM_ROUTE case
1409 */
1410 dest->selected_fib = NULL;
1411 }
1412
1413 /* Update nexthop for route, reset changed flag. */
1414 /* Note: this code also handles the Linux case when an interface goes
1415 * down, causing the kernel to delete routes without sending DELROUTE
1416 * notifications
1417 */
1418 if (!nexthop_active_update(rn, old, 1) &&
1419 (RIB_KERNEL_ROUTE(old)))
1420 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
1421 else
1422 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1423 }
1424
1425 static void rib_process_update_fib(struct zebra_vrf *zvrf,
1426 struct route_node *rn,
1427 struct route_entry *old,
1428 struct route_entry *new)
1429 {
1430 struct nexthop *nexthop = NULL;
1431 int nh_active = 0;
1432 rib_dest_t *dest = rib_dest_from_rnode(rn);
1433
1434 /*
1435 * We have to install or update if a new route has been selected or
1436 * something has changed.
1437 */
1438 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1439 hook_call(rib_update, rn, "updating existing route");
1440
1441 /* Update the nexthop; we could determine here that nexthop is
1442 * inactive. */
1443 if (nexthop_active_update(rn, new, 1))
1444 nh_active = 1;
1445
1446 /* If nexthop is active, install the selected route, if
1447 * appropriate. If
1448 * the install succeeds, cleanup flags for prior route, if
1449 * different from
1450 * newly selected.
1451 */
1452 if (nh_active) {
1453 if (IS_ZEBRA_DEBUG_RIB) {
1454 char buf[SRCDEST2STR_BUFFER];
1455 srcdest_rnode2str(rn, buf, sizeof(buf));
1456 if (new != old)
1457 zlog_debug(
1458 "%u:%s: Updating route rn %p, re %p (type %d) "
1459 "old %p (type %d)",
1460 zvrf_id(zvrf), buf, rn, new,
1461 new->type, old, old->type);
1462 else
1463 zlog_debug(
1464 "%u:%s: Updating route rn %p, re %p (type %d)",
1465 zvrf_id(zvrf), buf, rn, new,
1466 new->type);
1467 }
1468
1469 /* If labeled-unicast route, uninstall transit LSP. */
1470 if (zebra_rib_labeled_unicast(old))
1471 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1472
1473 /* Non-system route should be installed. */
1474 if (!RIB_SYSTEM_ROUTE(new)) {
1475 /* If labeled-unicast route, install transit
1476 * LSP. */
1477 if (zebra_rib_labeled_unicast(new))
1478 zebra_mpls_lsp_install(zvrf, rn, new);
1479
1480 rib_install_kernel(rn, new, old);
1481 } else {
1482 /*
1483 * We do not need to install the
1484 * selected route because it
1485 * is already isntalled by
1486 * the system( ie not us )
1487 * so just mark it as winning
1488 * we do need to ensure that
1489 * if we uninstall a route
1490 * from ourselves we don't
1491 * over write this pointer
1492 */
1493 dest->selected_fib = NULL;
1494 }
1495 /* If install succeeded or system route, cleanup flags
1496 * for prior route. */
1497 if (new != old) {
1498 if (RIB_SYSTEM_ROUTE(new)) {
1499 if (!RIB_SYSTEM_ROUTE(old))
1500 rib_uninstall_kernel(rn, old);
1501 } else {
1502 for (nexthop = old->ng.nexthop; nexthop;
1503 nexthop = nexthop->next)
1504 UNSET_FLAG(nexthop->flags,
1505 NEXTHOP_FLAG_FIB);
1506 }
1507 }
1508 }
1509
1510 /*
1511 * If nexthop for selected route is not active or install
1512 * failed, we
1513 * may need to uninstall and delete for redistribution.
1514 */
1515 if (!nh_active) {
1516 if (IS_ZEBRA_DEBUG_RIB) {
1517 char buf[SRCDEST2STR_BUFFER];
1518 srcdest_rnode2str(rn, buf, sizeof(buf));
1519 if (new != old)
1520 zlog_debug(
1521 "%u:%s: Deleting route rn %p, re %p (type %d) "
1522 "old %p (type %d) - nexthop inactive",
1523 zvrf_id(zvrf), buf, rn, new,
1524 new->type, old, old->type);
1525 else
1526 zlog_debug(
1527 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1528 zvrf_id(zvrf), buf, rn, new,
1529 new->type);
1530 }
1531
1532 /* If labeled-unicast route, uninstall transit LSP. */
1533 if (zebra_rib_labeled_unicast(old))
1534 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1535
1536 if (!RIB_SYSTEM_ROUTE(old))
1537 rib_uninstall_kernel(rn, old);
1538 else
1539 dest->selected_fib = NULL;
1540 }
1541 } else {
1542 /*
1543 * Same route selected; check if in the FIB and if not,
1544 * re-install. This
1545 * is housekeeping code to deal with race conditions in kernel
1546 * with linux
1547 * netlink reporting interface up before IPv4 or IPv6 protocol
1548 * is ready
1549 * to add routes.
1550 */
1551 if (!RIB_SYSTEM_ROUTE(new)) {
1552 bool in_fib = false;
1553
1554 for (ALL_NEXTHOPS(new->ng, nexthop))
1555 if (CHECK_FLAG(nexthop->flags,
1556 NEXTHOP_FLAG_FIB)) {
1557 in_fib = true;
1558 break;
1559 }
1560 if (!in_fib)
1561 rib_install_kernel(rn, new, NULL);
1562 }
1563 }
1564
1565 /* Update prior route. */
1566 if (new != old) {
1567 /* Set real nexthop. */
1568 nexthop_active_update(rn, old, 1);
1569 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1570 }
1571
1572 /* Clear changed flag. */
1573 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1574 }
1575
1576 /* Check if 'alternate' RIB entry is better than 'current'. */
1577 static struct route_entry *rib_choose_best(struct route_entry *current,
1578 struct route_entry *alternate)
1579 {
1580 if (current == NULL)
1581 return alternate;
1582
1583 /* filter route selection in following order:
1584 * - connected beats other types
1585 * - if both connected, loopback or vrf wins
1586 * - lower distance beats higher
1587 * - lower metric beats higher for equal distance
1588 * - last, hence oldest, route wins tie break.
1589 */
1590
1591 /* Connected routes. Check to see if either are a vrf
1592 * or loopback interface. If not, pick the last connected
1593 * route of the set of lowest metric connected routes.
1594 */
1595 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
1596 if (current->type != ZEBRA_ROUTE_CONNECT)
1597 return alternate;
1598
1599 /* both are connected. are either loop or vrf? */
1600 struct nexthop *nexthop = NULL;
1601
1602 for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
1603 if (if_is_loopback_or_vrf(if_lookup_by_index(
1604 nexthop->ifindex, alternate->vrf_id)))
1605 return alternate;
1606 }
1607
1608 for (ALL_NEXTHOPS(current->ng, nexthop)) {
1609 if (if_is_loopback_or_vrf(if_lookup_by_index(
1610 nexthop->ifindex, current->vrf_id)))
1611 return current;
1612 }
1613
1614 /* Neither are loop or vrf so pick best metric */
1615 if (alternate->metric <= current->metric)
1616 return alternate;
1617
1618 return current;
1619 }
1620
1621 if (current->type == ZEBRA_ROUTE_CONNECT)
1622 return current;
1623
1624 /* higher distance loses */
1625 if (alternate->distance < current->distance)
1626 return alternate;
1627 if (current->distance < alternate->distance)
1628 return current;
1629
1630 /* metric tie-breaks equal distance */
1631 if (alternate->metric <= current->metric)
1632 return alternate;
1633
1634 return current;
1635 }
1636
1637 /* Core function for processing routing information base. */
1638 static void rib_process(struct route_node *rn)
1639 {
1640 struct route_entry *re;
1641 struct route_entry *next;
1642 struct route_entry *old_selected = NULL;
1643 struct route_entry *new_selected = NULL;
1644 struct route_entry *old_fib = NULL;
1645 struct route_entry *new_fib = NULL;
1646 struct route_entry *best = NULL;
1647 char buf[SRCDEST2STR_BUFFER];
1648 rib_dest_t *dest;
1649 struct zebra_vrf *zvrf = NULL;
1650 const struct prefix *p, *src_p;
1651
1652 srcdest_rnode_prefixes(rn, &p, &src_p);
1653 vrf_id_t vrf_id = VRF_UNKNOWN;
1654
1655 assert(rn);
1656
1657 dest = rib_dest_from_rnode(rn);
1658 if (dest) {
1659 zvrf = rib_dest_vrf(dest);
1660 vrf_id = zvrf_id(zvrf);
1661 }
1662
1663 if (IS_ZEBRA_DEBUG_RIB)
1664 srcdest_rnode2str(rn, buf, sizeof(buf));
1665
1666 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1667 zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
1668
1669 /*
1670 * we can have rn's that have a NULL info pointer
1671 * (dest). As such let's not let the deref happen
1672 * additionally we know RNODE_FOREACH_RE_SAFE
1673 * will not iterate so we are ok.
1674 */
1675 if (dest)
1676 old_fib = dest->selected_fib;
1677
1678 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1679 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1680 zlog_debug(
1681 "%u:%s: Examine re %p (type %d) status %x flags %x "
1682 "dist %d metric %d",
1683 vrf_id, buf, re, re->type, re->status,
1684 re->flags, re->distance, re->metric);
1685
1686 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1687
1688 /* Currently selected re. */
1689 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1690 assert(old_selected == NULL);
1691 old_selected = re;
1692 }
1693
1694 /* Skip deleted entries from selection */
1695 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1696 continue;
1697
1698 /* Skip unreachable nexthop. */
1699 /* This first call to nexthop_active_update is merely to
1700 * determine if
1701 * there's any change to nexthops associated with this RIB
1702 * entry. Now,
1703 * rib_process() can be invoked due to an external event such as
1704 * link
1705 * down or due to next-hop-tracking evaluation. In the latter
1706 * case,
1707 * a decision has already been made that the NHs have changed.
1708 * So, no
1709 * need to invoke a potentially expensive call again. Further,
1710 * since
1711 * the change might be in a recursive NH which is not caught in
1712 * the nexthop_active_update() code. Thus, we might miss changes
1713 * to
1714 * recursive NHs.
1715 */
1716 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
1717 && !nexthop_active_update(rn, re, 0)) {
1718 if (re->type == ZEBRA_ROUTE_TABLE) {
1719 /* XXX: HERE BE DRAGONS!!!!!
1720 * In all honesty, I have not yet figured out
1721 * what this part
1722 * does or why the ROUTE_ENTRY_CHANGED test
1723 * above is correct
1724 * or why we need to delete a route here, and
1725 * also not whether
1726 * this concerns both selected and fib route, or
1727 * only selected
1728 * or only fib */
1729 /* This entry was denied by the 'ip protocol
1730 * table' route-map, we
1731 * need to delete it */
1732 if (re != old_selected) {
1733 if (IS_ZEBRA_DEBUG_RIB)
1734 zlog_debug(
1735 "%s: %u:%s: imported via import-table but denied "
1736 "by the ip protocol table route-map",
1737 __func__, vrf_id, buf);
1738 rib_unlink(rn, re);
1739 } else
1740 SET_FLAG(re->status,
1741 ROUTE_ENTRY_REMOVED);
1742 }
1743
1744 continue;
1745 }
1746
1747 /* Infinite distance. */
1748 if (re->distance == DISTANCE_INFINITY) {
1749 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1750 continue;
1751 }
1752
1753 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1754 best = rib_choose_best(new_fib, re);
1755 if (new_fib && best != new_fib)
1756 UNSET_FLAG(new_fib->status,
1757 ROUTE_ENTRY_CHANGED);
1758 new_fib = best;
1759 } else {
1760 best = rib_choose_best(new_selected, re);
1761 if (new_selected && best != new_selected)
1762 UNSET_FLAG(new_selected->status,
1763 ROUTE_ENTRY_CHANGED);
1764 new_selected = best;
1765 }
1766 if (best != re)
1767 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1768 } /* RNODE_FOREACH_RE */
1769
1770 /* If no FIB override route, use the selected route also for FIB */
1771 if (new_fib == NULL)
1772 new_fib = new_selected;
1773
1774 /* After the cycle is finished, the following pointers will be set:
1775 * old_selected --- RE entry currently having SELECTED
1776 * new_selected --- RE entry that is newly SELECTED
1777 * old_fib --- RE entry currently in kernel FIB
1778 * new_fib --- RE entry that is newly to be in kernel FIB
1779 *
1780 * new_selected will get SELECTED flag, and is going to be redistributed
1781 * the zclients. new_fib (which can be new_selected) will be installed
1782 * in kernel.
1783 */
1784
1785 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1786 zlog_debug(
1787 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1788 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1789 (void *)old_fib, (void *)new_fib);
1790 }
1791
1792 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1793 * fib == selected */
1794 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1795 ROUTE_ENTRY_CHANGED);
1796
1797 /* Update fib according to selection results */
1798 if (new_fib && old_fib)
1799 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1800 else if (new_fib)
1801 rib_process_add_fib(zvrf, rn, new_fib);
1802 else if (old_fib)
1803 rib_process_del_fib(zvrf, rn, old_fib);
1804
1805 /* Redistribute SELECTED entry */
1806 if (old_selected != new_selected || selected_changed) {
1807 struct nexthop *nexthop = NULL;
1808
1809 /* Check if we have a FIB route for the destination, otherwise,
1810 * don't redistribute it */
1811 if (new_fib) {
1812 for (ALL_NEXTHOPS(new_fib->ng, nexthop)) {
1813 if (CHECK_FLAG(nexthop->flags,
1814 NEXTHOP_FLAG_FIB)) {
1815 break;
1816 }
1817 }
1818 }
1819 if (!nexthop)
1820 new_selected = NULL;
1821
1822 if (new_selected && new_selected != new_fib) {
1823 nexthop_active_update(rn, new_selected, 1);
1824 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1825 }
1826
1827 if (old_selected) {
1828 if (!new_selected)
1829 redistribute_delete(p, src_p, old_selected);
1830 if (old_selected != new_selected)
1831 UNSET_FLAG(old_selected->flags,
1832 ZEBRA_FLAG_SELECTED);
1833 }
1834
1835 if (new_selected) {
1836 /* Install new or replace existing redistributed entry
1837 */
1838 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1839 redistribute_update(p, src_p, new_selected,
1840 old_selected);
1841 }
1842 }
1843
1844 /* Remove all RE entries queued for removal */
1845 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1846 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1847 if (IS_ZEBRA_DEBUG_RIB) {
1848 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1849 (void *)rn, (void *)re);
1850 }
1851 rib_unlink(rn, re);
1852 }
1853 }
1854
1855 /*
1856 * Check if the dest can be deleted now.
1857 */
1858 rib_gc_dest(rn);
1859 }
1860
1861 /* Take a list of route_node structs and return 1, if there was a record
1862 * picked from it and processed by rib_process(). Don't process more,
1863 * than one RN record; operate only in the specified sub-queue.
1864 */
1865 static unsigned int process_subq(struct list *subq, uint8_t qindex)
1866 {
1867 struct listnode *lnode = listhead(subq);
1868 struct route_node *rnode;
1869 rib_dest_t *dest;
1870 struct zebra_vrf *zvrf = NULL;
1871
1872 if (!lnode)
1873 return 0;
1874
1875 rnode = listgetdata(lnode);
1876 dest = rib_dest_from_rnode(rnode);
1877 if (dest)
1878 zvrf = rib_dest_vrf(dest);
1879
1880 rib_process(rnode);
1881
1882 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1883 char buf[SRCDEST2STR_BUFFER];
1884 srcdest_rnode2str(rnode, buf, sizeof(buf));
1885 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1886 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
1887 }
1888
1889 if (rnode->info)
1890 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
1891 RIB_ROUTE_QUEUED(qindex));
1892
1893 #if 0
1894 else
1895 {
1896 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1897 __func__, rnode, rnode->lock);
1898 zlog_backtrace(LOG_DEBUG);
1899 }
1900 #endif
1901 route_unlock_node(rnode);
1902 list_delete_node(subq, lnode);
1903 return 1;
1904 }
1905
1906 /*
1907 * All meta queues have been processed. Trigger next-hop evaluation.
1908 */
1909 static void meta_queue_process_complete(struct work_queue *dummy)
1910 {
1911 struct vrf *vrf;
1912 struct zebra_vrf *zvrf;
1913
1914 /* Evaluate nexthops for those VRFs which underwent route processing.
1915 * This
1916 * should limit the evaluation to the necessary VRFs in most common
1917 * situations.
1918 */
1919 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1920 zvrf = vrf->info;
1921 if (zvrf == NULL || !(zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED))
1922 continue;
1923
1924 zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED;
1925 zebra_evaluate_rnh(zvrf, AF_INET, 0, RNH_NEXTHOP_TYPE, NULL);
1926 zebra_evaluate_rnh(zvrf, AF_INET, 0, RNH_IMPORT_CHECK_TYPE,
1927 NULL);
1928 zebra_evaluate_rnh(zvrf, AF_INET6, 0, RNH_NEXTHOP_TYPE, NULL);
1929 zebra_evaluate_rnh(zvrf, AF_INET6, 0, RNH_IMPORT_CHECK_TYPE,
1930 NULL);
1931 }
1932
1933 /* Schedule LSPs for processing, if needed. */
1934 zvrf = vrf_info_lookup(VRF_DEFAULT);
1935 if (mpls_should_lsps_be_processed(zvrf)) {
1936 if (IS_ZEBRA_DEBUG_MPLS)
1937 zlog_debug(
1938 "%u: Scheduling all LSPs upon RIB completion",
1939 zvrf_id(zvrf));
1940 zebra_mpls_lsp_schedule(zvrf);
1941 mpls_unmark_lsps_for_processing(zvrf);
1942 }
1943 }
1944
1945 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1946 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1947 * data
1948 * is pointed to the meta queue structure.
1949 */
1950 static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
1951 {
1952 struct meta_queue *mq = data;
1953 unsigned i;
1954
1955 for (i = 0; i < MQ_SIZE; i++)
1956 if (process_subq(mq->subq[i], i)) {
1957 mq->size--;
1958 break;
1959 }
1960 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
1961 }
1962
1963 /*
1964 * Map from rib types to queue type (priority) in meta queue
1965 */
1966 static const uint8_t meta_queue_map[ZEBRA_ROUTE_MAX] = {
1967 [ZEBRA_ROUTE_SYSTEM] = 4,
1968 [ZEBRA_ROUTE_KERNEL] = 0,
1969 [ZEBRA_ROUTE_CONNECT] = 0,
1970 [ZEBRA_ROUTE_STATIC] = 1,
1971 [ZEBRA_ROUTE_RIP] = 2,
1972 [ZEBRA_ROUTE_RIPNG] = 2,
1973 [ZEBRA_ROUTE_OSPF] = 2,
1974 [ZEBRA_ROUTE_OSPF6] = 2,
1975 [ZEBRA_ROUTE_ISIS] = 2,
1976 [ZEBRA_ROUTE_BGP] = 3,
1977 [ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
1978 [ZEBRA_ROUTE_EIGRP] = 2,
1979 [ZEBRA_ROUTE_NHRP] = 2,
1980 [ZEBRA_ROUTE_HSLS] = 4,
1981 [ZEBRA_ROUTE_OLSR] = 4,
1982 [ZEBRA_ROUTE_TABLE] = 1,
1983 [ZEBRA_ROUTE_LDP] = 4,
1984 [ZEBRA_ROUTE_VNC] = 3,
1985 [ZEBRA_ROUTE_VNC_DIRECT] = 3,
1986 [ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
1987 [ZEBRA_ROUTE_BGP_DIRECT] = 3,
1988 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
1989 [ZEBRA_ROUTE_BABEL] = 2,
1990 [ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
1991 };
1992
1993 /* Look into the RN and queue it into one or more priority queues,
1994 * increasing the size for each data push done.
1995 */
1996 static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
1997 {
1998 struct route_entry *re;
1999
2000 RNODE_FOREACH_RE (rn, re) {
2001 uint8_t qindex = meta_queue_map[re->type];
2002 struct zebra_vrf *zvrf;
2003
2004 /* Invariant: at this point we always have rn->info set. */
2005 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2006 RIB_ROUTE_QUEUED(qindex))) {
2007 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2008 rnode_debug(
2009 rn, re->vrf_id,
2010 "rn %p is already queued in sub-queue %u",
2011 (void *)rn, qindex);
2012 continue;
2013 }
2014
2015 SET_FLAG(rib_dest_from_rnode(rn)->flags,
2016 RIB_ROUTE_QUEUED(qindex));
2017 listnode_add(mq->subq[qindex], rn);
2018 route_lock_node(rn);
2019 mq->size++;
2020
2021 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2022 rnode_debug(rn, re->vrf_id,
2023 "queued rn %p into sub-queue %u",
2024 (void *)rn, qindex);
2025
2026 zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
2027 if (zvrf)
2028 zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED;
2029 }
2030 }
2031
2032 /* Add route_node to work queue and schedule processing */
2033 void rib_queue_add(struct route_node *rn)
2034 {
2035 assert(rn);
2036
2037 /* Pointless to queue a route_node with no RIB entries to add or remove
2038 */
2039 if (!rnode_to_ribs(rn)) {
2040 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2041 __func__, (void *)rn, rn->lock);
2042 zlog_backtrace(LOG_DEBUG);
2043 return;
2044 }
2045
2046 if (zebrad.ribq == NULL) {
2047 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
2048 "%s: work_queue does not exist!", __func__);
2049 return;
2050 }
2051
2052 /*
2053 * The RIB queue should normally be either empty or holding the only
2054 * work_queue_item element. In the latter case this element would
2055 * hold a pointer to the meta queue structure, which must be used to
2056 * actually queue the route nodes to process. So create the MQ
2057 * holder, if necessary, then push the work into it in any case.
2058 * This semantics was introduced after 0.99.9 release.
2059 */
2060 if (work_queue_empty(zebrad.ribq))
2061 work_queue_add(zebrad.ribq, zebrad.mq);
2062
2063 rib_meta_queue_add(zebrad.mq, rn);
2064
2065 return;
2066 }
2067
2068 /* Create new meta queue.
2069 A destructor function doesn't seem to be necessary here.
2070 */
2071 static struct meta_queue *meta_queue_new(void)
2072 {
2073 struct meta_queue *new;
2074 unsigned i;
2075
2076 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
2077
2078 for (i = 0; i < MQ_SIZE; i++) {
2079 new->subq[i] = list_new();
2080 assert(new->subq[i]);
2081 }
2082
2083 return new;
2084 }
2085
2086 void meta_queue_free(struct meta_queue *mq)
2087 {
2088 unsigned i;
2089
2090 for (i = 0; i < MQ_SIZE; i++)
2091 list_delete(&mq->subq[i]);
2092
2093 XFREE(MTYPE_WORK_QUEUE, mq);
2094 }
2095
2096 /* initialise zebra rib work queue */
2097 static void rib_queue_init(struct zebra_t *zebra)
2098 {
2099 assert(zebra);
2100
2101 if (!(zebra->ribq =
2102 work_queue_new(zebra->master, "route_node processing"))) {
2103 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
2104 "%s: could not initialise work queue!", __func__);
2105 return;
2106 }
2107
2108 /* fill in the work queue spec */
2109 zebra->ribq->spec.workfunc = &meta_queue_process;
2110 zebra->ribq->spec.errorfunc = NULL;
2111 zebra->ribq->spec.completion_func = &meta_queue_process_complete;
2112 /* XXX: TODO: These should be runtime configurable via vty */
2113 zebra->ribq->spec.max_retries = 3;
2114 zebra->ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
2115
2116 if (!(zebra->mq = meta_queue_new())) {
2117 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
2118 "%s: could not initialise meta queue!", __func__);
2119 return;
2120 }
2121 return;
2122 }
2123
2124 /* RIB updates are processed via a queue of pointers to route_nodes.
2125 *
2126 * The queue length is bounded by the maximal size of the routing table,
2127 * as a route_node will not be requeued, if already queued.
2128 *
2129 * REs are submitted via rib_addnode or rib_delnode which set minimal
2130 * state, or static_install_route (when an existing RE is updated)
2131 * and then submit route_node to queue for best-path selection later.
2132 * Order of add/delete state changes are preserved for any given RE.
2133 *
2134 * Deleted REs are reaped during best-path selection.
2135 *
2136 * rib_addnode
2137 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2138 * |-------->| | best RE, if required
2139 * | |
2140 * static_install->|->rib_addqueue...... -> rib_process
2141 * | |
2142 * |-------->| |-> rib_unlink
2143 * |-> set ROUTE_ENTRY_REMOVE |
2144 * rib_delnode (RE freed)
2145 *
2146 * The 'info' pointer of a route_node points to a rib_dest_t
2147 * ('dest'). Queueing state for a route_node is kept on the dest. The
2148 * dest is created on-demand by rib_link() and is kept around at least
2149 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2150 *
2151 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2152 *
2153 * - route_nodes: refcounted by:
2154 * - dest attached to route_node:
2155 * - managed by: rib_link/rib_gc_dest
2156 * - route_node processing queue
2157 * - managed by: rib_addqueue, rib_process.
2158 *
2159 */
2160
2161 /* Add RE to head of the route node. */
2162 static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2163 {
2164 struct route_entry *head;
2165 rib_dest_t *dest;
2166 afi_t afi;
2167 const char *rmap_name;
2168
2169 assert(re && rn);
2170
2171 dest = rib_dest_from_rnode(rn);
2172 if (!dest) {
2173 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2174 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
2175
2176 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
2177 route_lock_node(rn); /* rn route table reference */
2178 rn->info = dest;
2179 dest->rnode = rn;
2180 }
2181
2182 head = dest->routes;
2183 if (head) {
2184 head->prev = re;
2185 }
2186 re->next = head;
2187 dest->routes = re;
2188
2189 afi = (rn->p.family == AF_INET)
2190 ? AFI_IP
2191 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2192 if (is_zebra_import_table_enabled(afi, re->table)) {
2193 rmap_name = zebra_get_import_table_route_map(afi, re->table);
2194 zebra_add_import_table_entry(rn, re, rmap_name);
2195 } else if (process)
2196 rib_queue_add(rn);
2197 }
2198
2199 static void rib_addnode(struct route_node *rn,
2200 struct route_entry *re, int process)
2201 {
2202 /* RE node has been un-removed before route-node is processed.
2203 * route_node must hence already be on the queue for processing..
2204 */
2205 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2206 if (IS_ZEBRA_DEBUG_RIB)
2207 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2208 (void *)rn, (void *)re);
2209
2210 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2211 return;
2212 }
2213 rib_link(rn, re, process);
2214 }
2215
2216 /*
2217 * rib_unlink
2218 *
2219 * Detach a rib structure from a route_node.
2220 *
2221 * Note that a call to rib_unlink() should be followed by a call to
2222 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2223 * longer required to be deleted.
2224 */
2225 void rib_unlink(struct route_node *rn, struct route_entry *re)
2226 {
2227 rib_dest_t *dest;
2228
2229 assert(rn && re);
2230
2231 if (IS_ZEBRA_DEBUG_RIB)
2232 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2233 (void *)re);
2234
2235 dest = rib_dest_from_rnode(rn);
2236
2237 if (re->next)
2238 re->next->prev = re->prev;
2239
2240 if (re->prev)
2241 re->prev->next = re->next;
2242 else {
2243 dest->routes = re->next;
2244 }
2245
2246 if (dest->selected_fib == re)
2247 dest->selected_fib = NULL;
2248
2249 nexthops_free(re->ng.nexthop);
2250 XFREE(MTYPE_RE, re);
2251 }
2252
2253 void rib_delnode(struct route_node *rn, struct route_entry *re)
2254 {
2255 afi_t afi;
2256
2257 if (IS_ZEBRA_DEBUG_RIB)
2258 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2259 (void *)rn, (void *)re);
2260 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2261
2262 afi = (rn->p.family == AF_INET)
2263 ? AFI_IP
2264 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2265 if (is_zebra_import_table_enabled(afi, re->table)) {
2266 zebra_del_import_table_entry(rn, re);
2267 /* Just clean up if non main table */
2268 if (IS_ZEBRA_DEBUG_RIB) {
2269 char buf[SRCDEST2STR_BUFFER];
2270 srcdest_rnode2str(rn, buf, sizeof(buf));
2271 zlog_debug(
2272 "%u:%s: Freeing route rn %p, re %p (type %d)",
2273 re->vrf_id, buf, rn, re, re->type);
2274 }
2275
2276 rib_unlink(rn, re);
2277 } else {
2278 rib_queue_add(rn);
2279 }
2280 }
2281
2282 /* This function dumps the contents of a given RE entry into
2283 * standard debug log. Calling function name and IP prefix in
2284 * question are passed as 1st and 2nd arguments.
2285 */
2286
2287 void _route_entry_dump(const char *func, union prefixconstptr pp,
2288 union prefixconstptr src_pp,
2289 const struct route_entry *re)
2290 {
2291 const struct prefix *src_p = src_pp.p;
2292 bool is_srcdst = src_p && src_p->prefixlen;
2293 char straddr[PREFIX_STRLEN];
2294 char srcaddr[PREFIX_STRLEN];
2295 struct nexthop *nexthop;
2296
2297 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2298 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2299 is_srcdst ? " from " : "",
2300 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2301 : "",
2302 re->vrf_id);
2303 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2304 func, (unsigned long)re->uptime, re->type, re->instance,
2305 re->table);
2306 zlog_debug(
2307 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2308 func, re->metric, re->mtu, re->distance, re->flags, re->status);
2309 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
2310 re->nexthop_num, re->nexthop_active_num);
2311
2312 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2313 struct interface *ifp;
2314 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2315
2316 switch (nexthop->type) {
2317 case NEXTHOP_TYPE_BLACKHOLE:
2318 sprintf(straddr, "Blackhole");
2319 break;
2320 case NEXTHOP_TYPE_IFINDEX:
2321 ifp = if_lookup_by_index(nexthop->ifindex,
2322 nexthop->vrf_id);
2323 sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
2324 break;
2325 case NEXTHOP_TYPE_IPV4:
2326 /* fallthrough */
2327 case NEXTHOP_TYPE_IPV4_IFINDEX:
2328 inet_ntop(AF_INET, &nexthop->gate, straddr,
2329 INET6_ADDRSTRLEN);
2330 break;
2331 case NEXTHOP_TYPE_IPV6:
2332 case NEXTHOP_TYPE_IPV6_IFINDEX:
2333 inet_ntop(AF_INET6, &nexthop->gate, straddr,
2334 INET6_ADDRSTRLEN);
2335 break;
2336 }
2337 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func,
2338 (nexthop->rparent ? " NH" : "NH"), straddr,
2339 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2340 nexthop->vrf_id,
2341 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2342 ? "ACTIVE "
2343 : ""),
2344 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
2345 ? "FIB "
2346 : ""),
2347 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
2348 ? "RECURSIVE"
2349 : ""));
2350 }
2351 zlog_debug("%s: dump complete", func);
2352 }
2353
2354 /* This is an exported helper to rtm_read() to dump the strange
2355 * RE entry found by rib_lookup_ipv4_route()
2356 */
2357
2358 void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2359 {
2360 struct route_table *table;
2361 struct route_node *rn;
2362 struct route_entry *re;
2363 char prefix_buf[INET_ADDRSTRLEN];
2364
2365 /* Lookup table. */
2366 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2367 if (!table) {
2368 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
2369 "%s:%u zebra_vrf_table() returned NULL", __func__,
2370 vrf_id);
2371 return;
2372 }
2373
2374 /* Scan the RIB table for exactly matching RE entry. */
2375 rn = route_node_lookup(table, (struct prefix *)p);
2376
2377 /* No route for this prefix. */
2378 if (!rn) {
2379 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
2380 prefix2str((struct prefix *)p, prefix_buf,
2381 sizeof(prefix_buf)));
2382 return;
2383 }
2384
2385 /* Unlock node. */
2386 route_unlock_node(rn);
2387
2388 /* let's go */
2389 RNODE_FOREACH_RE (rn, re) {
2390 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2391 __func__, vrf_id,
2392 (void *)rn, (void *)re,
2393 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2394 ? "removed"
2395 : "NOT removed"),
2396 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2397 ? "selected"
2398 : "NOT selected"));
2399 route_entry_dump(p, NULL, re);
2400 }
2401 }
2402
2403 /* Check if requested address assignment will fail due to another
2404 * route being installed by zebra in FIB already. Take necessary
2405 * actions, if needed: remove such a route from FIB and deSELECT
2406 * corresponding RE entry. Then put affected RN into RIBQ head.
2407 */
2408 void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2409 {
2410 struct route_table *table;
2411 struct route_node *rn;
2412 unsigned changed = 0;
2413 rib_dest_t *dest;
2414
2415 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
2416 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
2417 "%s:%u zebra_vrf_table() returned NULL", __func__,
2418 vrf_id);
2419 return;
2420 }
2421
2422 /* No matches would be the simplest case. */
2423 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2424 return;
2425
2426 /* Unlock node. */
2427 route_unlock_node(rn);
2428
2429 dest = rib_dest_from_rnode(rn);
2430 /* Check all RE entries. In case any changes have to be done, requeue
2431 * the RN into RIBQ head. If the routing message about the new connected
2432 * route (generated by the IP address we are going to assign very soon)
2433 * comes before the RIBQ is processed, the new RE entry will join
2434 * RIBQ record already on head. This is necessary for proper
2435 * revalidation
2436 * of the rest of the RE.
2437 */
2438 if (dest->selected_fib && !RIB_SYSTEM_ROUTE(dest->selected_fib)) {
2439 changed = 1;
2440 if (IS_ZEBRA_DEBUG_RIB) {
2441 char buf[PREFIX_STRLEN];
2442
2443 zlog_debug("%u:%s: freeing way for connected prefix",
2444 dest->selected_fib->vrf_id,
2445 prefix2str(&rn->p, buf, sizeof(buf)));
2446 route_entry_dump(&rn->p, NULL, dest->selected_fib);
2447 }
2448 rib_uninstall(rn, dest->selected_fib);
2449 }
2450 if (changed)
2451 rib_queue_add(rn);
2452 }
2453
2454 int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2455 struct prefix_ipv6 *src_p, struct route_entry *re)
2456 {
2457 struct route_table *table;
2458 struct route_node *rn;
2459 struct route_entry *same = NULL;
2460 struct nexthop *nexthop;
2461 int ret = 0;
2462
2463 if (!re)
2464 return 0;
2465
2466 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
2467
2468 /* Lookup table. */
2469 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2470 if (!table) {
2471 XFREE(MTYPE_RE, re);
2472 return 0;
2473 }
2474
2475 /* Make it sure prefixlen is applied to the prefix. */
2476 apply_mask(p);
2477 if (src_p)
2478 apply_mask_ipv6(src_p);
2479
2480 /* Set default distance by route type. */
2481 if (re->distance == 0) {
2482 re->distance = route_distance(re->type);
2483
2484 /* iBGP distance is 200. */
2485 if (re->type == ZEBRA_ROUTE_BGP
2486 && CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2487 re->distance = 200;
2488 }
2489
2490 /* Lookup route node.*/
2491 rn = srcdest_rnode_get(table, p, src_p);
2492
2493 /*
2494 * If same type of route are installed, treat it as a implicit
2495 * withdraw.
2496 * If the user has specified the No route replace semantics
2497 * for the install don't do a route replace.
2498 */
2499 RNODE_FOREACH_RE (rn, same) {
2500 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2501 continue;
2502
2503 if (same->type != re->type)
2504 continue;
2505 if (same->instance != re->instance)
2506 continue;
2507 if (same->type == ZEBRA_ROUTE_KERNEL
2508 && same->metric != re->metric)
2509 continue;
2510
2511 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2512 same->distance != re->distance)
2513 continue;
2514
2515 /*
2516 * We should allow duplicate connected routes
2517 * because of IPv6 link-local routes and unnumbered
2518 * interfaces on Linux.
2519 */
2520 if (same->type != ZEBRA_ROUTE_CONNECT)
2521 break;
2522 }
2523
2524 /* If this route is kernel route, set FIB flag to the route. */
2525 if (RIB_SYSTEM_ROUTE(re))
2526 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
2527 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2528
2529 /* Link new re to node.*/
2530 if (IS_ZEBRA_DEBUG_RIB) {
2531 rnode_debug(
2532 rn, re->vrf_id,
2533 "Inserting route rn %p, re %p (type %d) existing %p",
2534 (void *)rn, (void *)re, re->type, (void *)same);
2535
2536 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2537 route_entry_dump(p, src_p, re);
2538 }
2539 rib_addnode(rn, re, 1);
2540 ret = 1;
2541
2542 /* Free implicit route.*/
2543 if (same) {
2544 rib_delnode(rn, same);
2545 ret = -1;
2546 }
2547
2548 route_unlock_node(rn);
2549 return ret;
2550 }
2551
2552 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2553 unsigned short instance, int flags, struct prefix *p,
2554 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2555 uint32_t table_id, uint32_t metric, uint8_t distance,
2556 bool fromkernel)
2557 {
2558 struct route_table *table;
2559 struct route_node *rn;
2560 struct route_entry *re;
2561 struct route_entry *fib = NULL;
2562 struct route_entry *same = NULL;
2563 struct nexthop *rtnh;
2564 char buf2[INET6_ADDRSTRLEN];
2565 rib_dest_t *dest;
2566
2567 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
2568
2569 /* Lookup table. */
2570 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2571 if (!table)
2572 return;
2573
2574 /* Apply mask. */
2575 apply_mask(p);
2576 if (src_p)
2577 apply_mask_ipv6(src_p);
2578
2579 /* Lookup route node. */
2580 rn = srcdest_rnode_lookup(table, p, src_p);
2581 if (!rn) {
2582 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2583
2584 prefix2str(p, dst_buf, sizeof(dst_buf));
2585 if (src_p && src_p->prefixlen)
2586 prefix2str(src_p, src_buf, sizeof(src_buf));
2587 else
2588 src_buf[0] = '\0';
2589
2590 if (IS_ZEBRA_DEBUG_RIB)
2591 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
2592 dst_buf,
2593 (src_buf[0] != '\0') ? " from " : "",
2594 src_buf);
2595 return;
2596 }
2597
2598 dest = rib_dest_from_rnode(rn);
2599 fib = dest->selected_fib;
2600
2601 /* Lookup same type route. */
2602 RNODE_FOREACH_RE (rn, re) {
2603 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2604 continue;
2605
2606 if (re->type != type)
2607 continue;
2608 if (re->instance != instance)
2609 continue;
2610 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2611 distance != re->distance)
2612 continue;
2613
2614 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
2615 continue;
2616 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
2617 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2618 if (rtnh->ifindex != nh->ifindex)
2619 continue;
2620 same = re;
2621 break;
2622 }
2623 /* Make sure that the route found has the same gateway. */
2624 else {
2625 if (nh == NULL) {
2626 same = re;
2627 break;
2628 }
2629 for (ALL_NEXTHOPS(re->ng, rtnh))
2630 if (nexthop_same_no_recurse(rtnh, nh)) {
2631 same = re;
2632 break;
2633 }
2634 if (same)
2635 break;
2636 }
2637 }
2638 /* If same type of route can't be found and this message is from
2639 kernel. */
2640 if (!same) {
2641 /*
2642 * In the past(HA!) we could get here because
2643 * we were receiving a route delete from the
2644 * kernel and we're not marking the proto
2645 * as coming from it's appropriate originator.
2646 * Now that we are properly noticing the fact
2647 * that the kernel has deleted our route we
2648 * are not going to get called in this path
2649 * I am going to leave this here because
2650 * this might still work this way on non-linux
2651 * platforms as well as some weird state I have
2652 * not properly thought of yet.
2653 * If we can show that this code path is
2654 * dead then we can remove it.
2655 */
2656 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
2657 if (IS_ZEBRA_DEBUG_RIB) {
2658 rnode_debug(
2659 rn, vrf_id,
2660 "rn %p, re %p (type %d) was deleted from kernel, adding",
2661 rn, fib, fib->type);
2662 }
2663 if (allow_delete) {
2664 /* Unset flags. */
2665 for (rtnh = fib->ng.nexthop; rtnh;
2666 rtnh = rtnh->next)
2667 UNSET_FLAG(rtnh->flags,
2668 NEXTHOP_FLAG_FIB);
2669
2670 /*
2671 * This is a non FRR route
2672 * as such we should mark
2673 * it as deleted
2674 */
2675 dest->selected_fib = NULL;
2676 } else {
2677 /* This means someone else, other than Zebra,
2678 * has deleted
2679 * a Zebra router from the kernel. We will add
2680 * it back */
2681 rib_install_kernel(rn, fib, NULL);
2682 }
2683 } else {
2684 if (IS_ZEBRA_DEBUG_RIB) {
2685 if (nh)
2686 rnode_debug(
2687 rn, vrf_id,
2688 "via %s ifindex %d type %d "
2689 "doesn't exist in rib",
2690 inet_ntop(afi2family(afi),
2691 &nh->gate, buf2,
2692 sizeof(buf2)),
2693 nh->ifindex, type);
2694 else
2695 rnode_debug(
2696 rn, vrf_id,
2697 "type %d doesn't exist in rib",
2698 type);
2699 }
2700 route_unlock_node(rn);
2701 return;
2702 }
2703 }
2704
2705 if (same) {
2706 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
2707 && !allow_delete) {
2708 rib_install_kernel(rn, same, NULL);
2709 route_unlock_node(rn);
2710
2711 return;
2712 }
2713
2714 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
2715 struct nexthop *tmp_nh;
2716
2717 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
2718 struct ipaddr vtep_ip;
2719
2720 memset(&vtep_ip, 0, sizeof(struct ipaddr));
2721 if (afi == AFI_IP) {
2722 vtep_ip.ipa_type = IPADDR_V4;
2723 memcpy(&(vtep_ip.ipaddr_v4),
2724 &(tmp_nh->gate.ipv4),
2725 sizeof(struct in_addr));
2726 } else {
2727 vtep_ip.ipa_type = IPADDR_V6;
2728 memcpy(&(vtep_ip.ipaddr_v6),
2729 &(tmp_nh->gate.ipv6),
2730 sizeof(struct in6_addr));
2731 }
2732 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
2733 &vtep_ip, p);
2734 }
2735 }
2736 rib_delnode(rn, same);
2737 }
2738
2739 route_unlock_node(rn);
2740 return;
2741 }
2742
2743
2744 int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2745 unsigned short instance, int flags, struct prefix *p,
2746 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2747 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
2748 route_tag_t tag)
2749 {
2750 struct route_entry *re;
2751 struct nexthop *nexthop;
2752
2753 /* Allocate new route_entry structure. */
2754 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
2755 re->type = type;
2756 re->instance = instance;
2757 re->distance = distance;
2758 re->flags = flags;
2759 re->metric = metric;
2760 re->mtu = mtu;
2761 re->table = table_id;
2762 re->vrf_id = vrf_id;
2763 re->nexthop_num = 0;
2764 re->uptime = time(NULL);
2765 re->tag = tag;
2766
2767 /* Add nexthop. */
2768 nexthop = nexthop_new();
2769 *nexthop = *nh;
2770 route_entry_nexthop_add(re, nexthop);
2771
2772 return rib_add_multipath(afi, safi, p, src_p, re);
2773 }
2774
2775 /* Schedule routes of a particular table (address-family) based on event. */
2776 void rib_update_table(struct route_table *table, rib_update_event_t event)
2777 {
2778 struct route_node *rn;
2779 struct route_entry *re, *next;
2780
2781 /* Walk all routes and queue for processing, if appropriate for
2782 * the trigger event.
2783 */
2784 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2785 /*
2786 * If we are looking at a route node and the node
2787 * has already been queued we don't
2788 * need to queue it up again
2789 */
2790 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2791 RIB_ROUTE_ANY_QUEUED))
2792 continue;
2793 switch (event) {
2794 case RIB_UPDATE_IF_CHANGE:
2795 /* Examine all routes that won't get processed by the
2796 * protocol or
2797 * triggered by nexthop evaluation (NHT). This would be
2798 * system,
2799 * kernel and certain static routes. Note that NHT will
2800 * get
2801 * triggered upon an interface event as connected routes
2802 * always
2803 * get queued for processing.
2804 */
2805 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2806 struct nexthop *nh;
2807
2808 if (re->type != ZEBRA_ROUTE_SYSTEM
2809 && re->type != ZEBRA_ROUTE_KERNEL
2810 && re->type != ZEBRA_ROUTE_CONNECT
2811 && re->type != ZEBRA_ROUTE_STATIC)
2812 continue;
2813
2814 if (re->type != ZEBRA_ROUTE_STATIC) {
2815 rib_queue_add(rn);
2816 continue;
2817 }
2818
2819 for (nh = re->ng.nexthop; nh; nh = nh->next)
2820 if (!(nh->type == NEXTHOP_TYPE_IPV4
2821 || nh->type == NEXTHOP_TYPE_IPV6))
2822 break;
2823
2824 /* If we only have nexthops to a
2825 * gateway, NHT will
2826 * take care.
2827 */
2828 if (nh)
2829 rib_queue_add(rn);
2830 }
2831 break;
2832
2833 case RIB_UPDATE_RMAP_CHANGE:
2834 case RIB_UPDATE_OTHER:
2835 /* Right now, examine all routes. Can restrict to a
2836 * protocol in
2837 * some cases (TODO).
2838 */
2839 if (rnode_to_ribs(rn))
2840 rib_queue_add(rn);
2841 break;
2842
2843 default:
2844 break;
2845 }
2846 }
2847 }
2848
2849 /* RIB update function. */
2850 void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
2851 {
2852 struct route_table *table;
2853
2854 /* Process routes of interested address-families. */
2855 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2856 if (table) {
2857 if (IS_ZEBRA_DEBUG_EVENT)
2858 zlog_debug("%s : AFI_IP event %d", __func__, event);
2859 rib_update_table(table, event);
2860 }
2861
2862 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
2863 if (table) {
2864 if (IS_ZEBRA_DEBUG_EVENT)
2865 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
2866 rib_update_table(table, event);
2867 }
2868 }
2869
2870 /* Delete self installed routes after zebra is relaunched. */
2871 void rib_sweep_table(struct route_table *table)
2872 {
2873 struct route_node *rn;
2874 struct route_entry *re;
2875 struct route_entry *next;
2876 struct nexthop *nexthop;
2877
2878 if (!table)
2879 return;
2880
2881 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2882 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2883 if (IS_ZEBRA_DEBUG_RIB)
2884 route_entry_dump(&rn->p, NULL, re);
2885
2886 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2887 continue;
2888
2889 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
2890 continue;
2891
2892 /*
2893 * So we are starting up and have received
2894 * routes from the kernel that we have installed
2895 * from a previous run of zebra but not cleaned
2896 * up ( say a kill -9 )
2897 * But since we haven't actually installed
2898 * them yet( we received them from the kernel )
2899 * we don't think they are active.
2900 * So let's pretend they are active to actually
2901 * remove them.
2902 * In all honesty I'm not sure if we should
2903 * mark them as active when we receive them
2904 * This is startup only so probably ok.
2905 *
2906 * If we ever decide to move rib_sweep_table
2907 * to a different spot (ie startup )
2908 * this decision needs to be revisited
2909 */
2910 for (ALL_NEXTHOPS(re->ng, nexthop))
2911 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2912
2913 rib_uninstall_kernel(rn, re);
2914 rib_delnode(rn, re);
2915 }
2916 }
2917 }
2918
2919 /* Sweep all RIB tables. */
2920 void rib_sweep_route(void)
2921 {
2922 struct vrf *vrf;
2923 struct zebra_vrf *zvrf;
2924
2925 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2926 if ((zvrf = vrf->info) == NULL)
2927 continue;
2928
2929 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
2930 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
2931 }
2932
2933 zebra_ns_sweep_route();
2934 }
2935
2936 /* Remove specific by protocol routes from 'table'. */
2937 unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
2938 struct route_table *table)
2939 {
2940 struct route_node *rn;
2941 struct route_entry *re;
2942 struct route_entry *next;
2943 unsigned long n = 0;
2944
2945 if (table)
2946 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
2947 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2948 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2949 continue;
2950 if (re->type == proto
2951 && re->instance == instance) {
2952 rib_delnode(rn, re);
2953 n++;
2954 }
2955 }
2956 return n;
2957 }
2958
2959 /* Remove specific by protocol routes. */
2960 unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2961 {
2962 struct vrf *vrf;
2963 struct zebra_vrf *zvrf;
2964 unsigned long cnt = 0;
2965
2966 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
2967 if ((zvrf = vrf->info) != NULL)
2968 cnt += rib_score_proto_table(
2969 proto, instance,
2970 zvrf->table[AFI_IP][SAFI_UNICAST])
2971 + rib_score_proto_table(
2972 proto, instance,
2973 zvrf->table[AFI_IP6][SAFI_UNICAST]);
2974
2975 cnt += zebra_ns_score_proto(proto, instance);
2976
2977 return cnt;
2978 }
2979
2980 /* Close RIB and clean up kernel routes. */
2981 void rib_close_table(struct route_table *table)
2982 {
2983 struct route_node *rn;
2984 rib_table_info_t *info;
2985 rib_dest_t *dest;
2986
2987 if (!table)
2988 return;
2989
2990 info = route_table_get_info(table);
2991
2992 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2993 dest = rib_dest_from_rnode(rn);
2994
2995 if (dest && dest->selected_fib) {
2996 if (info->safi == SAFI_UNICAST)
2997 hook_call(rib_update, rn, NULL);
2998
2999 if (!RIB_SYSTEM_ROUTE(dest->selected_fib))
3000 rib_uninstall_kernel(rn, dest->selected_fib);
3001 }
3002 }
3003 }
3004
3005 /* Routing information base initialize. */
3006 void rib_init(void)
3007 {
3008 rib_queue_init(&zebrad);
3009 }
3010
3011 /*
3012 * vrf_id_get_next
3013 *
3014 * Get the first vrf id that is greater than the given vrf id if any.
3015 *
3016 * Returns TRUE if a vrf id was found, FALSE otherwise.
3017 */
3018 static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
3019 {
3020 struct vrf *vrf;
3021
3022 vrf = vrf_lookup_by_id(vrf_id);
3023 if (vrf) {
3024 vrf = RB_NEXT(vrf_id_head, vrf);
3025 if (vrf) {
3026 *next_id_p = vrf->vrf_id;
3027 return 1;
3028 }
3029 }
3030
3031 return 0;
3032 }
3033
3034 /*
3035 * rib_tables_iter_next
3036 *
3037 * Returns the next table in the iteration.
3038 */
3039 struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3040 {
3041 struct route_table *table;
3042
3043 /*
3044 * Array that helps us go over all AFI/SAFI combinations via one
3045 * index.
3046 */
3047 static struct {
3048 afi_t afi;
3049 safi_t safi;
3050 } afi_safis[] = {
3051 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3052 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3053 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3054 };
3055
3056 table = NULL;
3057
3058 switch (iter->state) {
3059
3060 case RIB_TABLES_ITER_S_INIT:
3061 iter->vrf_id = VRF_DEFAULT;
3062 iter->afi_safi_ix = -1;
3063
3064 /* Fall through */
3065
3066 case RIB_TABLES_ITER_S_ITERATING:
3067 iter->afi_safi_ix++;
3068 while (1) {
3069
3070 while (iter->afi_safi_ix
3071 < (int)ZEBRA_NUM_OF(afi_safis)) {
3072 table = zebra_vrf_table(
3073 afi_safis[iter->afi_safi_ix].afi,
3074 afi_safis[iter->afi_safi_ix].safi,
3075 iter->vrf_id);
3076 if (table)
3077 break;
3078
3079 iter->afi_safi_ix++;
3080 }
3081
3082 /*
3083 * Found another table in this vrf.
3084 */
3085 if (table)
3086 break;
3087
3088 /*
3089 * Done with all tables in the current vrf, go to the
3090 * next
3091 * one.
3092 */
3093 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3094 break;
3095
3096 iter->afi_safi_ix = 0;
3097 }
3098
3099 break;
3100
3101 case RIB_TABLES_ITER_S_DONE:
3102 return NULL;
3103 }
3104
3105 if (table)
3106 iter->state = RIB_TABLES_ITER_S_ITERATING;
3107 else
3108 iter->state = RIB_TABLES_ITER_S_DONE;
3109
3110 return table;
3111 }