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