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