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