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