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