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