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