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