]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
Merge pull request #82 from opensourcerouting/queue/ldp-xml2cli-build
[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, struct rib *old)
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, old, 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, NULL))
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, old))
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, NULL);
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 (rn, rib)
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",
2463 func,
2464 rib->nexthop_num,
2465 rib->nexthop_active_num
2466 );
2467
2468 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2469 {
2470 inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
2471 zlog_debug
2472 (
2473 "%s: %s %s with flags %s%s%s",
2474 func,
2475 (recursing ? " NH" : "NH"),
2476 straddr,
2477 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
2478 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
2479 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
2480 );
2481 }
2482 zlog_debug ("%s: dump complete", func);
2483 }
2484
2485 /* This is an exported helper to rtm_read() to dump the strange
2486 * RIB entry found by rib_lookup_ipv4_route()
2487 */
2488
2489 void rib_lookup_and_dump (struct prefix_ipv4 * p, vrf_id_t vrf_id)
2490 {
2491 struct route_table *table;
2492 struct route_node *rn;
2493 struct rib *rib;
2494 char prefix_buf[INET_ADDRSTRLEN];
2495
2496 /* Lookup table. */
2497 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
2498 if (! table)
2499 {
2500 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
2501 return;
2502 }
2503
2504 /* Scan the RIB table for exactly matching RIB entry. */
2505 rn = route_node_lookup (table, (struct prefix *) p);
2506
2507 /* No route for this prefix. */
2508 if (! rn)
2509 {
2510 zlog_debug ("%s: lookup failed for %s", __func__,
2511 prefix2str((struct prefix*) p, prefix_buf, sizeof(prefix_buf)));
2512 return;
2513 }
2514
2515 /* Unlock node. */
2516 route_unlock_node (rn);
2517
2518 /* let's go */
2519 RNODE_FOREACH_RIB (rn, rib)
2520 {
2521 zlog_debug
2522 (
2523 "%s: rn %p, rib %p: %s, %s",
2524 __func__,
2525 (void *)rn,
2526 (void *)rib,
2527 (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
2528 (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
2529 );
2530 rib_dump (p, rib);
2531 }
2532 }
2533
2534 /* Check if requested address assignment will fail due to another
2535 * route being installed by zebra in FIB already. Take necessary
2536 * actions, if needed: remove such a route from FIB and deSELECT
2537 * corresponding RIB entry. Then put affected RN into RIBQ head.
2538 */
2539 void rib_lookup_and_pushup (struct prefix_ipv4 * p, vrf_id_t vrf_id)
2540 {
2541 struct route_table *table;
2542 struct route_node *rn;
2543 struct rib *rib;
2544 unsigned changed = 0;
2545
2546 if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id)))
2547 {
2548 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
2549 return;
2550 }
2551
2552 /* No matches would be the simplest case. */
2553 if (NULL == (rn = route_node_lookup (table, (struct prefix *) p)))
2554 return;
2555
2556 /* Unlock node. */
2557 route_unlock_node (rn);
2558
2559 /* Check all RIB entries. In case any changes have to be done, requeue
2560 * the RN into RIBQ head. If the routing message about the new connected
2561 * route (generated by the IP address we are going to assign very soon)
2562 * comes before the RIBQ is processed, the new RIB entry will join
2563 * RIBQ record already on head. This is necessary for proper revalidation
2564 * of the rest of the RIB.
2565 */
2566 RNODE_FOREACH_RIB (rn, rib)
2567 {
2568 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB) &&
2569 ! RIB_SYSTEM_ROUTE (rib))
2570 {
2571 changed = 1;
2572 if (IS_ZEBRA_DEBUG_RIB)
2573 {
2574 char buf[PREFIX_STRLEN];
2575 zlog_debug ("%u:%s: freeing way for connected prefix",
2576 rib->vrf_id, prefix2str(&rn->p, buf, sizeof(buf)));
2577 rib_dump (&rn->p, rib);
2578 }
2579 rib_uninstall (rn, rib);
2580 }
2581 }
2582 if (changed)
2583 rib_queue_add (rn);
2584 }
2585
2586 int
2587 rib_add_multipath (afi_t afi, safi_t safi, struct prefix *p,
2588 struct rib *rib)
2589 {
2590 struct route_table *table;
2591 struct route_node *rn;
2592 struct rib *same;
2593 struct nexthop *nexthop;
2594 int ret = 0;
2595 int family;
2596
2597 if (!rib)
2598 return 0;
2599
2600 if (p->family == AF_INET)
2601 family = AFI_IP;
2602 else
2603 family = AFI_IP6;
2604
2605 /* Lookup table. */
2606 table = zebra_vrf_table_with_table_id (family, safi, rib->vrf_id, rib->table);
2607 if (! table)
2608 return 0;
2609
2610 /* Make it sure prefixlen is applied to the prefix. */
2611 apply_mask (p);
2612
2613 /* Set default distance by route type. */
2614 if (rib->distance == 0)
2615 {
2616 rib->distance = route_info[rib->type].distance;
2617
2618 /* iBGP distance is 200. */
2619 if (rib->type == ZEBRA_ROUTE_BGP
2620 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
2621 rib->distance = 200;
2622 }
2623
2624 /* Lookup route node.*/
2625 rn = route_node_get (table, p);
2626
2627 /* If same type of route are installed, treat it as a implicit
2628 withdraw. */
2629 RNODE_FOREACH_RIB (rn, same)
2630 {
2631 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
2632 continue;
2633
2634 if (same->type == rib->type && same->instance == rib->instance
2635 && same->table == rib->table
2636 && same->type != ZEBRA_ROUTE_CONNECT)
2637 break;
2638 }
2639
2640 /* If this route is kernel route, set FIB flag to the route. */
2641 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
2642 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2643 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2644
2645 /* Link new rib to node.*/
2646 if (IS_ZEBRA_DEBUG_RIB)
2647 {
2648 char buf[INET6_ADDRSTRLEN];
2649 if (IS_ZEBRA_DEBUG_RIB)
2650 {
2651 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2652 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2653 "existing %p",
2654 rib->vrf_id, buf, p->prefixlen, (void *)rn,
2655 (void *)rib, rib->type, (void *)same);
2656 }
2657
2658 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2659 rib_dump ((struct prefix *)p, rib);
2660 }
2661 rib_addnode (rn, rib, 1);
2662 ret = 1;
2663
2664 /* Free implicit route.*/
2665 if (same)
2666 {
2667 rib_delnode (rn, same);
2668 ret = -1;
2669 }
2670
2671 route_unlock_node (rn);
2672 return ret;
2673 }
2674
2675 int
2676 rib_delete (afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, u_short instance,
2677 int flags, struct prefix *p, union g_addr *gate, ifindex_t ifindex,
2678 u_int32_t table_id)
2679 {
2680 struct route_table *table;
2681 struct route_node *rn;
2682 struct rib *rib;
2683 struct rib *fib = NULL;
2684 struct rib *same = NULL;
2685 struct nexthop *nexthop, *tnexthop;
2686 int recursing;
2687 char buf1[PREFIX_STRLEN];
2688 char buf2[INET6_ADDRSTRLEN];
2689
2690 /* Lookup table. */
2691 table = zebra_vrf_table_with_table_id (afi, safi, vrf_id, table_id);
2692 if (! table)
2693 return 0;
2694
2695 /* Apply mask. */
2696 apply_mask (p);
2697
2698 /* Lookup route node. */
2699 rn = route_node_lookup (table, p);
2700 if (! rn)
2701 {
2702 if (IS_ZEBRA_DEBUG_RIB)
2703 zlog_debug ("%u:%s: doesn't exist in rib",
2704 vrf_id, prefix2str (p, buf1, sizeof(buf1)));
2705 return ZEBRA_ERR_RTNOEXIST;
2706 }
2707
2708 /* Lookup same type route. */
2709 RNODE_FOREACH_RIB (rn, rib)
2710 {
2711 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2712 continue;
2713
2714 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
2715 fib = rib;
2716
2717 if (rib->type != type)
2718 continue;
2719 if (rib->instance != instance)
2720 continue;
2721 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
2722 nexthop->type == NEXTHOP_TYPE_IFINDEX)
2723 {
2724 if (nexthop->ifindex != ifindex)
2725 continue;
2726 if (rib->refcnt)
2727 {
2728 rib->refcnt--;
2729 route_unlock_node (rn);
2730 route_unlock_node (rn);
2731 return 0;
2732 }
2733 same = rib;
2734 break;
2735 }
2736 /* Make sure that the route found has the same gateway. */
2737 else
2738 {
2739 if (gate == NULL)
2740 {
2741 same = rib;
2742 break;
2743 }
2744 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2745 if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate) ||
2746 IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
2747 {
2748 same = rib;
2749 break;
2750 }
2751 if (same)
2752 break;
2753 }
2754 }
2755 /* If same type of route can't be found and this message is from
2756 kernel. */
2757 if (! same)
2758 {
2759 if (fib && type == ZEBRA_ROUTE_KERNEL &&
2760 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
2761 {
2762 if (IS_ZEBRA_DEBUG_RIB)
2763 {
2764 zlog_debug ("%u:%s: rn %p, rib %p (type %d) was deleted "
2765 "from kernel, adding",
2766 vrf_id, prefix2str(p, buf1, INET6_ADDRSTRLEN),
2767 rn, fib, fib->type);
2768 }
2769 if (allow_delete)
2770 {
2771 /* Unset flags. */
2772 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
2773 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2774
2775 UNSET_FLAG (fib->status, RIB_ENTRY_SELECTED_FIB);
2776 }
2777 else
2778 {
2779 /* This means someone else, other than Zebra, has deleted
2780 * a Zebra router from the kernel. We will add it back */
2781 rib_install_kernel(rn, fib, NULL);
2782 }
2783 }
2784 else
2785 {
2786 if (IS_ZEBRA_DEBUG_RIB)
2787 {
2788 if (gate)
2789 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2790 "doesn't exist in rib",
2791 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
2792 inet_ntop (family2afi(afi), gate, buf2, INET_ADDRSTRLEN),
2793 ifindex,
2794 type);
2795 else
2796 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2797 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
2798 ifindex,
2799 type);
2800 }
2801 route_unlock_node (rn);
2802 return ZEBRA_ERR_RTNOEXIST;
2803 }
2804 }
2805
2806 if (same)
2807 rib_delnode (rn, same);
2808
2809 route_unlock_node (rn);
2810 return 0;
2811 }
2812
2813
2814
2815 int
2816 rib_add (afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2817 u_short instance, int flags, struct prefix *p,
2818 union g_addr *gate, union g_addr *src, ifindex_t ifindex,
2819 u_int32_t table_id, u_int32_t metric, u_int32_t mtu,
2820 u_char distance)
2821 {
2822 struct rib *rib;
2823 struct rib *same = NULL;
2824 struct route_table *table;
2825 struct route_node *rn;
2826 struct nexthop *nexthop;
2827
2828 /* Lookup table. */
2829 table = zebra_vrf_table_with_table_id (afi, safi, vrf_id, table_id);
2830 if (! table)
2831 return 0;
2832
2833 /* Make sure mask is applied. */
2834 apply_mask (p);
2835
2836 /* Set default distance by route type. */
2837 if (distance == 0)
2838 {
2839 if ((unsigned)type >= array_size(route_info))
2840 distance = 150;
2841 else
2842 distance = route_info[type].distance;
2843
2844 /* iBGP distance is 200. */
2845 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2846 distance = 200;
2847 }
2848
2849 /* Lookup route node.*/
2850 rn = route_node_get (table, p);
2851
2852 /* If same type of route are installed, treat it as a implicit
2853 withdraw. */
2854 RNODE_FOREACH_RIB (rn, rib)
2855 {
2856 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2857 continue;
2858
2859 if (rib->type != type)
2860 continue;
2861 if (rib->instance != instance)
2862 continue;
2863 if (rib->type != ZEBRA_ROUTE_CONNECT)
2864 {
2865 same = rib;
2866 break;
2867 }
2868 /* Duplicate connected route comes in. */
2869 else if ((nexthop = rib->nexthop) &&
2870 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
2871 nexthop->ifindex == ifindex &&
2872 !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2873 {
2874 rib->refcnt++;
2875 return 0 ;
2876 }
2877 }
2878
2879 /* Allocate new rib structure. */
2880 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2881
2882 rib->type = type;
2883 rib->instance = instance;
2884 rib->distance = distance;
2885 rib->flags = flags;
2886 rib->metric = metric;
2887 rib->mtu = mtu;
2888 rib->table = table_id;
2889 rib->vrf_id = vrf_id;
2890 rib->nexthop_num = 0;
2891 rib->uptime = time (NULL);
2892
2893 /* Nexthop settings. */
2894 if (gate)
2895 {
2896 if (afi == AFI_IP6)
2897 {
2898 if (ifindex)
2899 rib_nexthop_ipv6_ifindex_add (rib, &gate->ipv6, ifindex);
2900 else
2901 rib_nexthop_ipv6_add (rib, &gate->ipv6);
2902 }
2903 else
2904 {
2905 if (ifindex)
2906 rib_nexthop_ipv4_ifindex_add (rib, &gate->ipv4, &src->ipv4, ifindex);
2907 else
2908 rib_nexthop_ipv4_add (rib, &gate->ipv4, &src->ipv4);
2909 }
2910 }
2911 else
2912 rib_nexthop_ifindex_add (rib, ifindex);
2913
2914 /* If this route is kernel route, set FIB flag to the route. */
2915 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
2916 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2917 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2918
2919 /* Link new rib to node.*/
2920 if (IS_ZEBRA_DEBUG_RIB)
2921 {
2922 char buf[INET6_ADDRSTRLEN];
2923 if (IS_ZEBRA_DEBUG_RIB)
2924 {
2925 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2926 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2927 "existing %p",
2928 vrf_id, buf, p->prefixlen, (void *)rn,
2929 (void *)rib, rib->type, (void *)same);
2930 }
2931
2932 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2933 rib_dump (p, rib);
2934 }
2935 rib_addnode (rn, rib, 1);
2936
2937 /* Free implicit route.*/
2938 if (same)
2939 rib_delnode (rn, same);
2940
2941 route_unlock_node (rn);
2942 return 0;
2943 }
2944
2945 /* Schedule routes of a particular table (address-family) based on event. */
2946 static void
2947 rib_update_table (struct route_table *table, rib_update_event_t event)
2948 {
2949 struct route_node *rn;
2950 struct rib *rib, *next;
2951
2952 /* Walk all routes and queue for processing, if appropriate for
2953 * the trigger event.
2954 */
2955 for (rn = route_top (table); rn; rn = route_next (rn))
2956 {
2957 switch (event)
2958 {
2959 case RIB_UPDATE_IF_CHANGE:
2960 /* Examine all routes that won't get processed by the protocol or
2961 * triggered by nexthop evaluation (NHT). This would be system,
2962 * kernel and certain static routes. Note that NHT will get
2963 * triggered upon an interface event as connected routes always
2964 * get queued for processing.
2965 */
2966 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
2967 {
2968 if (rib->type == ZEBRA_ROUTE_OSPF ||
2969 rib->type == ZEBRA_ROUTE_OSPF6 ||
2970 rib->type == ZEBRA_ROUTE_BGP)
2971 continue; /* protocol will handle. */
2972 else if (rib->type == ZEBRA_ROUTE_STATIC)
2973 {
2974 struct nexthop *nh;
2975 for (nh = rib->nexthop; nh; nh = nh->next)
2976 if (!(nh->type == NEXTHOP_TYPE_IPV4 ||
2977 nh->type == NEXTHOP_TYPE_IPV6))
2978 break;
2979
2980 /* If we only have nexthops to a gateway, NHT will
2981 * take care.
2982 */
2983 if (nh)
2984 rib_queue_add (rn);
2985 }
2986 else
2987 rib_queue_add (rn);
2988 }
2989 break;
2990
2991 case RIB_UPDATE_RMAP_CHANGE:
2992 case RIB_UPDATE_OTHER:
2993 /* Right now, examine all routes. Can restrict to a protocol in
2994 * some cases (TODO).
2995 */
2996 if (rnode_to_ribs (rn))
2997 rib_queue_add (rn);
2998 break;
2999
3000 default:
3001 break;
3002 }
3003 }
3004 }
3005
3006 /* RIB update function. */
3007 void
3008 rib_update (vrf_id_t vrf_id, rib_update_event_t event)
3009 {
3010 struct route_table *table;
3011
3012 /* Process routes of interested address-families. */
3013 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
3014 if (table)
3015 rib_update_table (table, event);
3016
3017 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
3018 if (table)
3019 rib_update_table (table, event);
3020 }
3021
3022 /* Remove all routes which comes from non main table. */
3023 static void
3024 rib_weed_table (struct route_table *table)
3025 {
3026 struct route_node *rn;
3027 struct rib *rib;
3028 struct rib *next;
3029
3030 if (table)
3031 for (rn = route_top (table); rn; rn = route_next (rn))
3032 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3033 {
3034 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3035 continue;
3036
3037 if (rib->table != zebrad.rtm_table_default &&
3038 rib->table != RT_TABLE_MAIN)
3039 rib_delnode (rn, rib);
3040 }
3041 }
3042
3043 /* Delete all routes from non main table. */
3044 void
3045 rib_weed_tables (void)
3046 {
3047 struct vrf *vrf;
3048 struct zebra_vrf *zvrf;
3049
3050 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
3051 if ((zvrf = vrf->info) != NULL)
3052 {
3053 rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3054 rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3055 }
3056 }
3057
3058 /* Delete self installed routes after zebra is relaunched. */
3059 static void
3060 rib_sweep_table (struct route_table *table)
3061 {
3062 struct route_node *rn;
3063 struct rib *rib;
3064 struct rib *next;
3065 int ret = 0;
3066
3067 if (table)
3068 for (rn = route_top (table); rn; rn = route_next (rn))
3069 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3070 {
3071 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3072 continue;
3073
3074 if (rib->type == ZEBRA_ROUTE_KERNEL &&
3075 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
3076 {
3077 ret = rib_uninstall_kernel (rn, rib);
3078 if (! ret)
3079 rib_delnode (rn, rib);
3080 }
3081 }
3082 }
3083
3084 /* Sweep all RIB tables. */
3085 void
3086 rib_sweep_route (void)
3087 {
3088 struct vrf *vrf;
3089 struct zebra_vrf *zvrf;
3090
3091 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
3092 if ((zvrf = vrf->info) != NULL)
3093 {
3094 rib_sweep_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3095 rib_sweep_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3096 }
3097 }
3098
3099 /* Remove specific by protocol routes from 'table'. */
3100 static unsigned long
3101 rib_score_proto_table (u_char proto, u_short instance, struct route_table *table)
3102 {
3103 struct route_node *rn;
3104 struct rib *rib;
3105 struct rib *next;
3106 unsigned long n = 0;
3107
3108 if (table)
3109 for (rn = route_top (table); rn; rn = route_next (rn))
3110 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3111 {
3112 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3113 continue;
3114 if (rib->type == proto && rib->instance == instance)
3115 {
3116 rib_delnode (rn, rib);
3117 n++;
3118 }
3119 }
3120
3121 return n;
3122 }
3123
3124 /* Remove specific by protocol routes. */
3125 unsigned long
3126 rib_score_proto (u_char proto, u_short instance)
3127 {
3128 struct vrf *vrf;
3129 struct zebra_vrf *zvrf;
3130 unsigned long cnt = 0;
3131
3132 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
3133 if ((zvrf = vrf->info) != NULL)
3134 cnt += rib_score_proto_table (proto, instance, zvrf->table[AFI_IP][SAFI_UNICAST])
3135 +rib_score_proto_table (proto, instance, zvrf->table[AFI_IP6][SAFI_UNICAST]);
3136
3137 return cnt;
3138 }
3139
3140 /* Close RIB and clean up kernel routes. */
3141 void
3142 rib_close_table (struct route_table *table)
3143 {
3144 struct route_node *rn;
3145 rib_table_info_t *info = table->info;
3146 struct rib *rib;
3147
3148 if (table)
3149 for (rn = route_top (table); rn; rn = route_next (rn))
3150 RNODE_FOREACH_RIB (rn, rib)
3151 {
3152 if (!CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
3153 continue;
3154
3155 if (info->safi == SAFI_UNICAST)
3156 zfpm_trigger_update (rn, NULL);
3157
3158 if (! RIB_SYSTEM_ROUTE (rib))
3159 rib_uninstall_kernel (rn, rib);
3160 }
3161 }
3162
3163 /* Routing information base initialize. */
3164 void
3165 rib_init (void)
3166 {
3167 rib_queue_init (&zebrad);
3168 }
3169
3170 /*
3171 * vrf_id_get_next
3172 *
3173 * Get the first vrf id that is greater than the given vrf id if any.
3174 *
3175 * Returns TRUE if a vrf id was found, FALSE otherwise.
3176 */
3177 static inline int
3178 vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p)
3179 {
3180 struct vrf *vrf;
3181
3182 vrf = vrf_lookup_by_id (vrf_id);
3183 if (vrf)
3184 {
3185 vrf = RB_NEXT (vrf_id_head, &vrfs_by_id, vrf);
3186 if (vrf) {
3187 *next_id_p = vrf->vrf_id;
3188 return 1;
3189 }
3190 }
3191
3192 return 0;
3193 }
3194
3195 /*
3196 * rib_tables_iter_next
3197 *
3198 * Returns the next table in the iteration.
3199 */
3200 struct route_table *
3201 rib_tables_iter_next (rib_tables_iter_t *iter)
3202 {
3203 struct route_table *table;
3204
3205 /*
3206 * Array that helps us go over all AFI/SAFI combinations via one
3207 * index.
3208 */
3209 static struct {
3210 afi_t afi;
3211 safi_t safi;
3212 } afi_safis[] = {
3213 { AFI_IP, SAFI_UNICAST },
3214 { AFI_IP, SAFI_MULTICAST },
3215 { AFI_IP6, SAFI_UNICAST },
3216 { AFI_IP6, SAFI_MULTICAST },
3217 };
3218
3219 table = NULL;
3220
3221 switch (iter->state)
3222 {
3223
3224 case RIB_TABLES_ITER_S_INIT:
3225 iter->vrf_id = VRF_DEFAULT;
3226 iter->afi_safi_ix = -1;
3227
3228 /* Fall through */
3229
3230 case RIB_TABLES_ITER_S_ITERATING:
3231 iter->afi_safi_ix++;
3232 while (1)
3233 {
3234
3235 while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
3236 {
3237 table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi,
3238 afi_safis[iter->afi_safi_ix].safi,
3239 iter->vrf_id);
3240 if (table)
3241 break;
3242
3243 iter->afi_safi_ix++;
3244 }
3245
3246 /*
3247 * Found another table in this vrf.
3248 */
3249 if (table)
3250 break;
3251
3252 /*
3253 * Done with all tables in the current vrf, go to the next
3254 * one.
3255 */
3256 if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
3257 break;
3258
3259 iter->afi_safi_ix = 0;
3260 }
3261
3262 break;
3263
3264 case RIB_TABLES_ITER_S_DONE:
3265 return NULL;
3266 }
3267
3268 if (table)
3269 iter->state = RIB_TABLES_ITER_S_ITERATING;
3270 else
3271 iter->state = RIB_TABLES_ITER_S_DONE;
3272
3273 return table;
3274 }
3275