]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
zebra: use prefix2str for logging where possible
[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 "prefix.h"
25 #include "table.h"
26 #include "memory.h"
27 #include "str.h"
28 #include "command.h"
29 #include "if.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
40 #include "zebra/rib.h"
41 #include "zebra/rt.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/zebra_routemap.h"
47 #include "zebra/debug.h"
48 #include "zebra/zebra_fpm.h"
49 #include "zebra/zebra_rnh.h"
50 #include "zebra/interface.h"
51 #include "zebra/connected.h"
52
53 /* Should we allow non Quagga processes to delete our routes */
54 extern int allow_delete;
55
56 /* Hold time for RIB process, should be very minimal.
57 * it is useful to able to set it otherwise for testing, hence exported
58 * as global here for test-rig code.
59 */
60 int rib_process_hold_time = 10;
61
62 /* Each route type's string and default distance value. */
63 static const struct
64 {
65 int key;
66 int distance;
67 } route_info[ZEBRA_ROUTE_MAX] =
68 {
69 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
70 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
71 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
72 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
73 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
74 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
75 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
76 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
77 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
78 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
79 /* no entry/default: 150 */
80 };
81
82 /* RPF lookup behaviour */
83 static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
84
85
86 static void __attribute__((format (printf, 5, 6)))
87 _rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn, int priority,
88 const char *msgfmt, ...)
89 {
90 char buf[PREFIX_STRLEN + 8];
91 char msgbuf[512];
92 va_list ap;
93
94 va_start(ap, msgfmt);
95 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
96 va_end(ap);
97
98 if (rn)
99 {
100 rib_table_info_t *info = rn->table->info;
101
102 prefix2str(&rn->p, buf, sizeof(buf));
103 if (info->safi == SAFI_MULTICAST)
104 strcat(buf, " (MRIB)");
105 }
106 else
107 {
108 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
109 }
110
111 zlog (NULL, priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
112 }
113
114 #define rnode_debug(node, vrf_id, ...) \
115 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
116 #define rnode_info(node, ...) \
117 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
118
119 int
120 is_zebra_valid_kernel_table(u_int32_t table_id)
121 {
122 if ((table_id > ZEBRA_KERNEL_TABLE_MAX) ||
123 (table_id == RT_TABLE_UNSPEC) ||
124 (table_id == RT_TABLE_LOCAL) ||
125 (table_id == RT_TABLE_COMPAT))
126 return 0;
127 else
128 return 1;
129 }
130
131 int
132 is_zebra_main_routing_table(u_int32_t table_id)
133 {
134 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
135 return 1;
136 return 0;
137 }
138
139 int
140 zebra_check_addr (struct prefix *p)
141 {
142 if (p->family == AF_INET)
143 {
144 u_int32_t addr;
145
146 addr = p->u.prefix4.s_addr;
147 addr = ntohl (addr);
148
149 if (IPV4_NET127 (addr)
150 || IN_CLASSD (addr)
151 || IPV4_LINKLOCAL(addr))
152 return 0;
153 }
154 if (p->family == AF_INET6)
155 {
156 if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
157 return 0;
158 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
159 return 0;
160 }
161 return 1;
162 }
163
164 /* Add nexthop to the end of a rib node's nexthop list */
165 void
166 rib_nexthop_add (struct rib *rib, struct nexthop *nexthop)
167 {
168 nexthop_add(&rib->nexthop, nexthop);
169 rib->nexthop_num++;
170 }
171
172
173
174 /**
175 * copy_nexthop - copy a nexthop to the rib structure.
176 */
177 void
178 rib_copy_nexthops (struct rib *rib, struct nexthop *nh)
179 {
180 struct nexthop *nexthop;
181
182 nexthop = nexthop_new();
183 nexthop->flags = nh->flags;
184 nexthop->type = nh->type;
185 nexthop->ifindex = nh->ifindex;
186 memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr));
187 memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr));
188 rib_nexthop_add(rib, nexthop);
189 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
190 copy_nexthops(&nexthop->resolved, nh->resolved);
191 }
192
193 /* Delete specified nexthop from the list. */
194 static void
195 rib_nexthop_delete (struct rib *rib, struct nexthop *nexthop)
196 {
197 if (nexthop->next)
198 nexthop->next->prev = nexthop->prev;
199 if (nexthop->prev)
200 nexthop->prev->next = nexthop->next;
201 else
202 rib->nexthop = nexthop->next;
203 rib->nexthop_num--;
204 }
205
206
207
208 struct nexthop *
209 rib_nexthop_ifindex_add (struct rib *rib, unsigned int ifindex)
210 {
211 struct nexthop *nexthop;
212
213 nexthop = nexthop_new();
214 nexthop->type = NEXTHOP_TYPE_IFINDEX;
215 nexthop->ifindex = ifindex;
216
217 rib_nexthop_add (rib, nexthop);
218
219 return nexthop;
220 }
221
222 struct nexthop *
223 rib_nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
224 {
225 struct nexthop *nexthop;
226
227 nexthop = nexthop_new();
228 nexthop->type = NEXTHOP_TYPE_IPV4;
229 nexthop->gate.ipv4 = *ipv4;
230 if (src)
231 nexthop->src.ipv4 = *src;
232
233 rib_nexthop_add (rib, nexthop);
234
235 return nexthop;
236 }
237
238 struct nexthop *
239 rib_nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
240 struct in_addr *src, unsigned int ifindex)
241 {
242 struct nexthop *nexthop;
243 struct interface *ifp;
244
245 nexthop = nexthop_new();
246 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
247 nexthop->gate.ipv4 = *ipv4;
248 if (src)
249 nexthop->src.ipv4 = *src;
250 nexthop->ifindex = ifindex;
251 ifp = if_lookup_by_index (nexthop->ifindex);
252 /*Pending: need to think if null ifp here is ok during bootup?
253 There was a crash because ifp here was coming to be NULL */
254 if (ifp)
255 if (connected_is_unnumbered(ifp)) {
256 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
257 }
258
259 rib_nexthop_add (rib, nexthop);
260
261 return nexthop;
262 }
263
264 struct nexthop *
265 rib_nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
266 {
267 struct nexthop *nexthop;
268
269 nexthop = nexthop_new();
270 nexthop->type = NEXTHOP_TYPE_IPV6;
271 nexthop->gate.ipv6 = *ipv6;
272
273 rib_nexthop_add (rib, nexthop);
274
275 return nexthop;
276 }
277
278 struct nexthop *
279 rib_nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
280 unsigned int ifindex)
281 {
282 struct nexthop *nexthop;
283
284 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
285 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
286 nexthop->gate.ipv6 = *ipv6;
287 nexthop->ifindex = ifindex;
288
289 rib_nexthop_add (rib, nexthop);
290
291 return nexthop;
292 }
293
294 struct nexthop *
295 rib_nexthop_blackhole_add (struct rib *rib)
296 {
297 struct nexthop *nexthop;
298
299 nexthop = nexthop_new();
300 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
301 SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
302
303 rib_nexthop_add (rib, nexthop);
304
305 return nexthop;
306 }
307
308 /* This method checks whether a recursive nexthop has at
309 * least one resolved nexthop in the fib.
310 */
311 int
312 nexthop_has_fib_child(struct nexthop *nexthop)
313 {
314 struct nexthop *nh;
315
316 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
317 return 0;
318
319 for (nh = nexthop->resolved; nh; nh = nh->next)
320 if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB))
321 return 1;
322
323 return 0;
324 }
325
326 /* If force flag is not set, do not modify falgs at all for uninstall
327 the route from FIB. */
328 static int
329 nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
330 struct route_node *top)
331 {
332 struct prefix_ipv4 p;
333 struct route_table *table;
334 struct route_node *rn;
335 struct rib *match;
336 int resolved;
337 struct nexthop *newhop, *tnewhop;
338 struct nexthop *resolved_hop;
339 int recursing = 0;
340 struct interface *ifp;
341
342 if (nexthop->type == NEXTHOP_TYPE_IPV4)
343 nexthop->ifindex = 0;
344
345 if (set)
346 {
347 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
348 zebra_deregister_rnh_static_nexthops(rib->vrf_id, nexthop->resolved, top);
349 nexthops_free(nexthop->resolved);
350 nexthop->resolved = NULL;
351 }
352
353 /* Skip nexthops that have been filtered out due to route-map */
354 /* The nexthops are specific to this route and so the same */
355 /* nexthop for a different route may not have this flag set */
356 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED))
357 return 0;
358
359 /*
360 * Check to see if we should trust the passed in information
361 * for UNNUMBERED interfaces as that we won't find the GW
362 * address in the routing table.
363 */
364 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
365 {
366 ifp = if_lookup_by_index (nexthop->ifindex);
367 if (ifp && connected_is_unnumbered(ifp))
368 {
369 if (if_is_operative(ifp))
370 return 1;
371 else
372 return 0;
373 }
374 else
375 return 0;
376 }
377
378 /* Make lookup prefix. */
379 memset (&p, 0, sizeof (struct prefix_ipv4));
380 p.family = AF_INET;
381 p.prefixlen = IPV4_MAX_PREFIXLEN;
382 p.prefix = nexthop->gate.ipv4;
383
384 /* Lookup table. */
385 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, rib->vrf_id);
386 if (! table)
387 return 0;
388
389 rn = route_node_match (table, (struct prefix *) &p);
390 while (rn)
391 {
392 route_unlock_node (rn);
393
394 /* If lookup self prefix return immediately. */
395 if (rn == top)
396 return 0;
397
398 /* Pick up selected route. */
399 /* However, do not resolve over default route unless explicitly allowed. */
400 if (is_default_prefix (&rn->p) &&
401 !nh_resolve_via_default (p.family))
402 return 0;
403
404 RNODE_FOREACH_RIB (rn, match)
405 {
406 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
407 continue;
408 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
409 break;
410 }
411
412 /* If there is no selected route or matched route is EGP, go up
413 tree. */
414 if (! match)
415 {
416 do {
417 rn = rn->parent;
418 } while (rn && rn->info == NULL);
419 if (rn)
420 route_lock_node (rn);
421 }
422 else
423 {
424 /* If the longest prefix match for the nexthop yields
425 * a blackhole, mark it as inactive. */
426 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
427 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
428 return 0;
429
430 if (match->type == ZEBRA_ROUTE_CONNECT)
431 {
432 /* Directly point connected route. */
433 newhop = match->nexthop;
434 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
435 nexthop->ifindex = newhop->ifindex;
436
437 return 1;
438 }
439 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
440 {
441 resolved = 0;
442 for (newhop = match->nexthop; newhop; newhop = newhop->next)
443 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
444 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
445 {
446 if (set)
447 {
448 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
449 SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
450
451 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
452 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
453 /* If the resolving route specifies a gateway, use it */
454 if (newhop->type == NEXTHOP_TYPE_IPV4
455 || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
456 {
457 resolved_hop->type = newhop->type;
458 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
459
460 if (newhop->ifindex)
461 {
462 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
463 resolved_hop->ifindex = newhop->ifindex;
464 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
465 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
466 }
467 }
468
469 /* If the resolving route is an interface route,
470 * it means the gateway we are looking up is connected
471 * to that interface. (The actual network is _not_ onlink).
472 * Therefore, the resolved route should have the original
473 * gateway as nexthop as it is directly connected.
474 *
475 * On Linux, we have to set the onlink netlink flag because
476 * otherwise, the kernel won't accept the route. */
477 if (newhop->type == NEXTHOP_TYPE_IFINDEX)
478 {
479 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
480 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
481 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
482 resolved_hop->ifindex = newhop->ifindex;
483 }
484
485 nexthop_add(&nexthop->resolved, resolved_hop);
486 }
487 resolved = 1;
488 }
489 return resolved;
490 }
491 else if (rib->type == ZEBRA_ROUTE_STATIC)
492 {
493 resolved = 0;
494 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
495 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
496 {
497 if (set)
498 {
499 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
500
501 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
502 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
503 /* If the resolving route specifies a gateway, use it */
504 if (newhop->type == NEXTHOP_TYPE_IPV4
505 || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
506 {
507 resolved_hop->type = newhop->type;
508 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
509
510 if (newhop->ifindex)
511 {
512 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
513 resolved_hop->ifindex = newhop->ifindex;
514 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
515 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
516 }
517 }
518
519 /* If the resolving route is an interface route,
520 * it means the gateway we are looking up is connected
521 * to that interface. (The actual network is _not_ onlink).
522 * Therefore, the resolved route should have the original
523 * gateway as nexthop as it is directly connected.
524 *
525 * On Linux, we have to set the onlink netlink flag because
526 * otherwise, the kernel won't accept the route.
527 */
528 if (newhop->type == NEXTHOP_TYPE_IFINDEX)
529 {
530 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
531 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
532 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
533 resolved_hop->ifindex = newhop->ifindex;
534 }
535
536 nexthop_add(&nexthop->resolved, resolved_hop);
537 }
538 resolved = 1;
539 }
540 return resolved;
541 }
542 else
543 {
544 return 0;
545 }
546 }
547 }
548 return 0;
549 }
550
551 /* If force flag is not set, do not modify falgs at all for uninstall
552 the route from FIB. */
553 static int
554 nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
555 struct route_node *top)
556 {
557 struct prefix_ipv6 p;
558 struct route_table *table;
559 struct route_node *rn;
560 struct rib *match;
561 int resolved;
562 struct nexthop *newhop, *tnewhop;
563 int recursing = 0;
564 struct nexthop *resolved_hop;
565
566 if (nexthop->type == NEXTHOP_TYPE_IPV6)
567 nexthop->ifindex = 0;
568
569 if (set)
570 {
571 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
572 zebra_deregister_rnh_static_nexthops (rib->vrf_id, nexthop->resolved, top);
573 nexthops_free(nexthop->resolved);
574 nexthop->resolved = NULL;
575 }
576
577 /* Skip nexthops that have been filtered out due to route-map */
578 /* The nexthops are specific to this route and so the same */
579 /* nexthop for a different route may not have this flag set */
580 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED))
581 return 0;
582
583 /* Make lookup prefix. */
584 memset (&p, 0, sizeof (struct prefix_ipv6));
585 p.family = AF_INET6;
586 p.prefixlen = IPV6_MAX_PREFIXLEN;
587 p.prefix = nexthop->gate.ipv6;
588
589 /* Lookup table. */
590 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, rib->vrf_id);
591 if (! table)
592 return 0;
593
594 rn = route_node_match (table, (struct prefix *) &p);
595 while (rn)
596 {
597 route_unlock_node (rn);
598
599 /* If lookup self prefix return immediately. */
600 if (rn == top)
601 return 0;
602
603 /* Pick up selected route. */
604 /* However, do not resolve over default route unless explicitly allowed. */
605 if (is_default_prefix (&rn->p) &&
606 !nh_resolve_via_default (p.family))
607 return 0;
608
609 RNODE_FOREACH_RIB (rn, match)
610 {
611 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
612 continue;
613 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
614 break;
615 }
616
617 /* If there is no selected route or matched route is EGP, go up
618 tree. */
619 if (! match)
620 {
621 do {
622 rn = rn->parent;
623 } while (rn && rn->info == NULL);
624 if (rn)
625 route_lock_node (rn);
626 }
627 else
628 {
629 /* If the longest prefix match for the nexthop yields
630 * a blackhole, mark it as inactive. */
631 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
632 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
633 return 0;
634
635 if (match->type == ZEBRA_ROUTE_CONNECT)
636 {
637 /* Directly point connected route. */
638 newhop = match->nexthop;
639
640 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
641 nexthop->ifindex = newhop->ifindex;
642
643 return 1;
644 }
645 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
646 {
647 resolved = 0;
648 for (newhop = match->nexthop; newhop; newhop = newhop->next)
649 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
650 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
651 {
652 if (set)
653 {
654 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
655 SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
656
657 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
658 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
659 /* See nexthop_active_ipv4 for a description how the
660 * resolved nexthop is constructed. */
661 if (newhop->type == NEXTHOP_TYPE_IPV6
662 || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
663 {
664 resolved_hop->type = newhop->type;
665 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
666
667 if (newhop->ifindex)
668 {
669 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
670 resolved_hop->ifindex = newhop->ifindex;
671 }
672 }
673
674 if (newhop->type == NEXTHOP_TYPE_IFINDEX)
675 {
676 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
677 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
678 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
679 resolved_hop->ifindex = newhop->ifindex;
680 }
681
682 nexthop_add(&nexthop->resolved, resolved_hop);
683 }
684 resolved = 1;
685 }
686 return resolved;
687 }
688 else if (rib->type == ZEBRA_ROUTE_STATIC)
689 {
690 resolved = 0;
691 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
692 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
693 {
694 if (set)
695 {
696 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
697
698 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
699 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
700 /* See nexthop_active_ipv4 for a description how the
701 * resolved nexthop is constructed. */
702 if (newhop->type == NEXTHOP_TYPE_IPV6
703 || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
704 {
705 resolved_hop->type = newhop->type;
706 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
707
708 if (newhop->ifindex)
709 {
710 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
711 resolved_hop->ifindex = newhop->ifindex;
712 }
713 }
714
715 if (newhop->type == NEXTHOP_TYPE_IFINDEX)
716 {
717 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
718 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
719 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
720 resolved_hop->ifindex = newhop->ifindex;
721 }
722
723 nexthop_add(&nexthop->resolved, resolved_hop);
724 }
725 resolved = 1;
726 }
727 return resolved;
728 }
729 else
730 {
731 return 0;
732 }
733 }
734 }
735 return 0;
736 }
737
738 struct rib *
739 rib_match_ipv4 (struct in_addr addr, safi_t safi, vrf_id_t vrf_id,
740 struct route_node **rn_out)
741 {
742 struct prefix_ipv4 p;
743 struct route_table *table;
744 struct route_node *rn;
745 struct rib *match;
746 struct nexthop *newhop, *tnewhop;
747 int recursing;
748
749 /* Lookup table. */
750 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
751 if (! table)
752 return 0;
753
754 memset (&p, 0, sizeof (struct prefix_ipv4));
755 p.family = AF_INET;
756 p.prefixlen = IPV4_MAX_PREFIXLEN;
757 p.prefix = addr;
758
759 rn = route_node_match (table, (struct prefix *) &p);
760
761 while (rn)
762 {
763 route_unlock_node (rn);
764
765 /* Pick up selected route. */
766 RNODE_FOREACH_RIB (rn, match)
767 {
768 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
769 continue;
770 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
771 break;
772 }
773
774 /* If there is no selected route or matched route is EGP, go up
775 tree. */
776 if (! match)
777 {
778 do {
779 rn = rn->parent;
780 } while (rn && rn->info == NULL);
781 if (rn)
782 route_lock_node (rn);
783 }
784 else
785 {
786 if (match->type != ZEBRA_ROUTE_CONNECT)
787 {
788 int found = 0;
789 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
790 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
791 {
792 found = 1;
793 break;
794 }
795 if (!found)
796 return NULL;
797 }
798
799 if (rn_out)
800 *rn_out = rn;
801 return match;
802 }
803 }
804 return NULL;
805 }
806
807 struct rib *
808 rib_match_ipv4_multicast (struct in_addr addr, struct route_node **rn_out)
809 {
810 struct rib *rib = NULL, *mrib = NULL, *urib = NULL;
811 struct route_node *m_rn = NULL, *u_rn = NULL;
812 int skip_bgp = 0; /* bool */
813
814 switch (ipv4_multicast_mode)
815 {
816 case MCAST_MRIB_ONLY:
817 return rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, rn_out);
818 case MCAST_URIB_ONLY:
819 return rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, rn_out);
820 case MCAST_NO_CONFIG:
821 case MCAST_MIX_MRIB_FIRST:
822 rib = mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
823 if (!mrib)
824 rib = urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
825 break;
826 case MCAST_MIX_DISTANCE:
827 mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
828 urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
829 if (mrib && urib)
830 rib = urib->distance < mrib->distance ? urib : mrib;
831 else if (mrib)
832 rib = mrib;
833 else if (urib)
834 rib = urib;
835 break;
836 case MCAST_MIX_PFXLEN:
837 mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
838 urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
839 if (mrib && urib)
840 rib = u_rn->p.prefixlen > m_rn->p.prefixlen ? urib : mrib;
841 else if (mrib)
842 rib = mrib;
843 else if (urib)
844 rib = urib;
845 break;
846 }
847
848 if (rn_out)
849 *rn_out = (rib == mrib) ? m_rn : u_rn;
850
851 if (IS_ZEBRA_DEBUG_RIB)
852 {
853 char buf[BUFSIZ];
854 inet_ntop (AF_INET, &addr, buf, BUFSIZ);
855
856 zlog_debug("%s: %s: found %s, using %s",
857 __func__, buf,
858 mrib ? (urib ? "MRIB+URIB" : "MRIB") :
859 urib ? "URIB" : "nothing",
860 rib == urib ? "URIB" : rib == mrib ? "MRIB" : "none");
861 }
862 return rib;
863 }
864
865 void
866 multicast_mode_ipv4_set (enum multicast_mode mode)
867 {
868 if (IS_ZEBRA_DEBUG_RIB)
869 zlog_debug("%s: multicast lookup mode set (%d)", __func__, mode);
870 ipv4_multicast_mode = mode;
871 }
872
873 enum multicast_mode
874 multicast_mode_ipv4_get (void)
875 {
876 return ipv4_multicast_mode;
877 }
878
879 struct rib *
880 rib_lookup_ipv4 (struct prefix_ipv4 *p, vrf_id_t vrf_id)
881 {
882 struct route_table *table;
883 struct route_node *rn;
884 struct rib *match;
885 struct nexthop *nexthop, *tnexthop;
886 int recursing;
887
888 /* Lookup table. */
889 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
890 if (! table)
891 return 0;
892
893 rn = route_node_lookup (table, (struct prefix *) p);
894
895 /* No route for this prefix. */
896 if (! rn)
897 return NULL;
898
899 /* Unlock node. */
900 route_unlock_node (rn);
901
902 RNODE_FOREACH_RIB (rn, match)
903 {
904 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
905 continue;
906 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
907 break;
908 }
909
910 if (! match)
911 return NULL;
912
913 if (match->type == ZEBRA_ROUTE_CONNECT)
914 return match;
915
916 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
917 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
918 return match;
919
920 return NULL;
921 }
922
923 /*
924 * This clone function, unlike its original rib_lookup_ipv4(), checks
925 * if specified IPv4 route record (prefix/mask -> gate) exists in
926 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
927 *
928 * Return values:
929 * -1: error
930 * 0: exact match found
931 * 1: a match was found with a different gate
932 * 2: connected route found
933 * 3: no matches found
934 */
935 int
936 rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate,
937 vrf_id_t vrf_id)
938 {
939 struct route_table *table;
940 struct route_node *rn;
941 struct rib *match;
942 struct nexthop *nexthop, *tnexthop;
943 int recursing;
944 int nexthops_active;
945
946 /* Lookup table. */
947 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
948 if (! table)
949 return ZEBRA_RIB_LOOKUP_ERROR;
950
951 /* Scan the RIB table for exactly matching RIB entry. */
952 rn = route_node_lookup (table, (struct prefix *) p);
953
954 /* No route for this prefix. */
955 if (! rn)
956 return ZEBRA_RIB_NOTFOUND;
957
958 /* Unlock node. */
959 route_unlock_node (rn);
960
961 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
962 RNODE_FOREACH_RIB (rn, match)
963 {
964 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
965 continue;
966 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
967 break;
968 }
969
970 /* None such found :( */
971 if (!match)
972 return ZEBRA_RIB_NOTFOUND;
973
974 if (match->type == ZEBRA_ROUTE_CONNECT)
975 return ZEBRA_RIB_FOUND_CONNECTED;
976
977 /* Ok, we have a cood candidate, let's check it's nexthop list... */
978 nexthops_active = 0;
979 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
980 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
981 {
982 nexthops_active = 1;
983 if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate))
984 return ZEBRA_RIB_FOUND_EXACT;
985 if (IS_ZEBRA_DEBUG_RIB)
986 {
987 char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
988 inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
989 inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN);
990 zlog_debug ("%s: qgate == %s, %s == %s", __func__,
991 qgate_buf, recursing ? "rgate" : "gate", gate_buf);
992 }
993 }
994
995 if (nexthops_active)
996 return ZEBRA_RIB_FOUND_NOGATE;
997
998 return ZEBRA_RIB_NOTFOUND;
999 }
1000
1001 struct rib *
1002 rib_match_ipv6 (struct in6_addr *addr, vrf_id_t vrf_id)
1003 {
1004 struct prefix_ipv6 p;
1005 struct route_table *table;
1006 struct route_node *rn;
1007 struct rib *match;
1008 struct nexthop *newhop, *tnewhop;
1009 int recursing;
1010
1011 /* Lookup table. */
1012 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
1013 if (! table)
1014 return 0;
1015
1016 memset (&p, 0, sizeof (struct prefix_ipv6));
1017 p.family = AF_INET6;
1018 p.prefixlen = IPV6_MAX_PREFIXLEN;
1019 IPV6_ADDR_COPY (&p.prefix, addr);
1020
1021 rn = route_node_match (table, (struct prefix *) &p);
1022
1023 while (rn)
1024 {
1025 route_unlock_node (rn);
1026
1027 /* Pick up selected route. */
1028 RNODE_FOREACH_RIB (rn, match)
1029 {
1030 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
1031 continue;
1032 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
1033 break;
1034 }
1035
1036 /* If there is no selected route or matched route is EGP, go up
1037 tree. */
1038 if (! match)
1039 {
1040 do {
1041 rn = rn->parent;
1042 } while (rn && rn->info == NULL);
1043 if (rn)
1044 route_lock_node (rn);
1045 }
1046 else
1047 {
1048 if (match->type == ZEBRA_ROUTE_CONNECT)
1049 /* Directly point connected route. */
1050 return match;
1051 else
1052 {
1053 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
1054 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
1055 return match;
1056 return NULL;
1057 }
1058 }
1059 }
1060 return NULL;
1061 }
1062
1063 #define RIB_SYSTEM_ROUTE(R) \
1064 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1065
1066 /* This function verifies reachability of one given nexthop, which can be
1067 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1068 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1069 * nexthop->ifindex will be updated appropriately as well.
1070 * An existing route map can turn (otherwise active) nexthop into inactive, but
1071 * not vice versa.
1072 *
1073 * The return value is the final value of 'ACTIVE' flag.
1074 */
1075
1076 static unsigned
1077 nexthop_active_check (struct route_node *rn, struct rib *rib,
1078 struct nexthop *nexthop, int set)
1079 {
1080 rib_table_info_t *info = rn->table->info;
1081 struct interface *ifp;
1082 route_map_result_t ret = RMAP_MATCH;
1083 int family;
1084 char buf[INET6_ADDRSTRLEN+1];
1085
1086 if (rn->p.family == AF_INET)
1087 family = AFI_IP;
1088 else if (rn->p.family == AF_INET6)
1089 family = AFI_IP6;
1090 else
1091 family = 0;
1092 switch (nexthop->type)
1093 {
1094 case NEXTHOP_TYPE_IFINDEX:
1095 ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id);
1096 if (ifp && if_is_operative(ifp))
1097 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1098 else
1099 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1100 break;
1101 case NEXTHOP_TYPE_IPV4:
1102 case NEXTHOP_TYPE_IPV4_IFINDEX:
1103 family = AFI_IP;
1104 if (nexthop_active_ipv4 (rib, nexthop, set, rn))
1105 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1106 else
1107 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1108 break;
1109 case NEXTHOP_TYPE_IPV6:
1110 family = AFI_IP6;
1111 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
1112 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1113 else
1114 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1115 break;
1116 case NEXTHOP_TYPE_IPV6_IFINDEX:
1117 /* RFC 5549, v4 prefix with v6 NH */
1118 if (rn->p.family != AF_INET)
1119 family = AFI_IP6;
1120 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
1121 {
1122 ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id);
1123 if (ifp && if_is_operative(ifp))
1124 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1125 else
1126 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1127 }
1128 else
1129 {
1130 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
1131 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1132 else
1133 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1134 }
1135 break;
1136 case NEXTHOP_TYPE_BLACKHOLE:
1137 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1138 break;
1139 default:
1140 break;
1141 }
1142 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1143 return 0;
1144
1145 /* XXX: What exactly do those checks do? Do we support
1146 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1147 if (RIB_SYSTEM_ROUTE(rib) ||
1148 (family == AFI_IP && rn->p.family != AF_INET) ||
1149 (family == AFI_IP6 && rn->p.family != AF_INET6))
1150 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1151
1152 /* The original code didn't determine the family correctly
1153 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1154 * from the rib_table_info in those cases.
1155 * Possibly it may be better to use only the rib_table_info
1156 * in every case.
1157 */
1158 if (!family)
1159 family = info->afi;
1160
1161 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
1162
1163 /* It'll get set if required inside */
1164 ret = zebra_route_map_check(family, rib->type, &rn->p, nexthop, rib->vrf_id,
1165 rib->tag);
1166 if (ret == RMAP_DENYMATCH)
1167 {
1168 if (IS_ZEBRA_DEBUG_RIB)
1169 {
1170 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf));
1171 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1172 rib->vrf_id, buf, rn->p.prefixlen,
1173 ifindex2ifname_vrf (nexthop->ifindex, rib->vrf_id));
1174 }
1175 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1176 }
1177 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1178 }
1179
1180 /* Iterate over all nexthops of the given RIB entry and refresh their
1181 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1182 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1183 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
1184 * transparently passed to nexthop_active_check().
1185 *
1186 * Return value is the new number of active nexthops.
1187 */
1188
1189 static int
1190 nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
1191 {
1192 struct nexthop *nexthop;
1193 union g_addr prev_src;
1194 unsigned int prev_active, prev_index, new_active, old_num_nh;
1195
1196 old_num_nh = rib->nexthop_active_num;
1197
1198 rib->nexthop_active_num = 0;
1199 UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1200
1201 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1202 {
1203 /* No protocol daemon provides src and so we're skipping tracking it */
1204 prev_src = nexthop->rmap_src;
1205 prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1206 prev_index = nexthop->ifindex;
1207 if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
1208 rib->nexthop_active_num++;
1209 /* Don't allow src setting on IPv6 addr for now */
1210 if (prev_active != new_active ||
1211 prev_index != nexthop->ifindex ||
1212 ((nexthop->type >= NEXTHOP_TYPE_IFINDEX &&
1213 nexthop->type < NEXTHOP_TYPE_IPV6) &&
1214 prev_src.ipv4.s_addr != nexthop->rmap_src.ipv4.s_addr) ||
1215 ((nexthop->type >= NEXTHOP_TYPE_IPV6 &&
1216 nexthop->type < NEXTHOP_TYPE_BLACKHOLE) &&
1217 !(IPV6_ADDR_SAME (&prev_src.ipv6, &nexthop->rmap_src.ipv6))))
1218 {
1219 SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1220 SET_FLAG (rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
1221 }
1222 }
1223
1224 if (old_num_nh != rib->nexthop_active_num)
1225 SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1226
1227 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_CHANGED))
1228 {
1229 SET_FLAG (rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
1230 }
1231
1232 return rib->nexthop_active_num;
1233 }
1234
1235
1236
1237 /* Update flag indicates whether this is a "replace" or not. Currently, this
1238 * is only used for IPv4.
1239 */
1240 static int
1241 rib_install_kernel (struct route_node *rn, struct rib *rib, int update)
1242 {
1243 int ret = 0;
1244 struct nexthop *nexthop, *tnexthop;
1245 rib_table_info_t *info = rn->table->info;
1246 int recursing;
1247
1248 if (info->safi != SAFI_UNICAST)
1249 {
1250 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1251 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1252 return ret;
1253 }
1254
1255 /*
1256 * Make sure we update the FPM any time we send new information to
1257 * the kernel.
1258 */
1259 zfpm_trigger_update (rn, "installing in kernel");
1260 switch (PREFIX_FAMILY (&rn->p))
1261 {
1262 case AF_INET:
1263 if (update)
1264 ret = kernel_update_ipv4 (&rn->p, rib);
1265 else
1266 ret = kernel_add_ipv4 (&rn->p, rib);
1267 break;
1268 case AF_INET6:
1269 if (update)
1270 ret = kernel_update_ipv6 (&rn->p, rib);
1271 else
1272 ret = kernel_add_ipv6 (&rn->p, rib);
1273 break;
1274 }
1275
1276 /* If install succeeds, update FIB flag for nexthops. */
1277 if (!ret)
1278 {
1279 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1280 {
1281 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1282 continue;
1283
1284 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1285 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1286 else
1287 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1288 }
1289 }
1290
1291 return ret;
1292 }
1293
1294 /* Uninstall the route from kernel. */
1295 static int
1296 rib_uninstall_kernel (struct route_node *rn, struct rib *rib)
1297 {
1298 int ret = 0;
1299 struct nexthop *nexthop, *tnexthop;
1300 rib_table_info_t *info = rn->table->info;
1301 int recursing;
1302
1303 if (info->safi != SAFI_UNICAST)
1304 {
1305 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1306 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1307 return ret;
1308 }
1309
1310 /*
1311 * Make sure we update the FPM any time we send new information to
1312 * the kernel.
1313 */
1314 zfpm_trigger_update (rn, "uninstalling from kernel");
1315
1316 switch (PREFIX_FAMILY (&rn->p))
1317 {
1318 case AF_INET:
1319 ret = kernel_delete_ipv4 (&rn->p, rib);
1320 break;
1321 case AF_INET6:
1322 ret = kernel_delete_ipv6 (&rn->p, rib);
1323 break;
1324 }
1325
1326 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1327 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1328
1329 return ret;
1330 }
1331
1332 /* Uninstall the route from kernel. */
1333 static void
1334 rib_uninstall (struct route_node *rn, struct rib *rib)
1335 {
1336 rib_table_info_t *info = rn->table->info;
1337
1338 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
1339 {
1340 if (info->safi == SAFI_UNICAST)
1341 zfpm_trigger_update (rn, "rib_uninstall");
1342
1343 redistribute_delete (&rn->p, rib);
1344 if (! RIB_SYSTEM_ROUTE (rib))
1345 rib_uninstall_kernel (rn, rib);
1346 UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED);
1347 }
1348 }
1349
1350 static void rib_unlink (struct route_node *, struct rib *);
1351
1352 /*
1353 * rib_can_delete_dest
1354 *
1355 * Returns TRUE if the given dest can be deleted from the table.
1356 */
1357 static int
1358 rib_can_delete_dest (rib_dest_t *dest)
1359 {
1360 if (dest->routes)
1361 {
1362 return 0;
1363 }
1364
1365 /*
1366 * Don't delete the dest if we have to update the FPM about this
1367 * prefix.
1368 */
1369 if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) ||
1370 CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
1371 return 0;
1372
1373 return 1;
1374 }
1375
1376 /*
1377 * rib_gc_dest
1378 *
1379 * Garbage collect the rib dest corresponding to the given route node
1380 * if appropriate.
1381 *
1382 * Returns TRUE if the dest was deleted, FALSE otherwise.
1383 */
1384 int
1385 rib_gc_dest (struct route_node *rn)
1386 {
1387 rib_dest_t *dest;
1388 struct zebra_vrf *zvrf;
1389
1390 dest = rib_dest_from_rnode (rn);
1391 if (!dest)
1392 return 0;
1393
1394 if (!rib_can_delete_dest (dest))
1395 return 0;
1396
1397 zvrf = rib_dest_vrf (dest);
1398 if (IS_ZEBRA_DEBUG_RIB)
1399 rnode_debug (rn, zvrf->vrf_id, "removing dest from table");
1400
1401 dest->rnode = NULL;
1402 XFREE (MTYPE_RIB_DEST, dest);
1403 rn->info = NULL;
1404
1405 /*
1406 * Release the one reference that we keep on the route node.
1407 */
1408 route_unlock_node (rn);
1409 return 1;
1410 }
1411
1412 static void
1413 rib_process_add_route (struct zebra_vrf *zvrf, struct route_node *rn,
1414 struct rib *select)
1415 {
1416 char buf[INET6_ADDRSTRLEN];
1417 int installed = 1;
1418
1419 zfpm_trigger_update (rn, "new route selected");
1420
1421 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1422 if (!nexthop_active_update (rn, select, 1))
1423 {
1424 UNSET_FLAG(select->flags, ZEBRA_FLAG_CHANGED);
1425 return;
1426 }
1427
1428 SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1429 if (IS_ZEBRA_DEBUG_RIB)
1430 {
1431 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
1432 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1433 zvrf->vrf_id, buf, rn->p.prefixlen, rn, select, select->type);
1434 }
1435
1436 if (!RIB_SYSTEM_ROUTE (select))
1437 {
1438 if (rib_install_kernel (rn, select, 0))
1439 {
1440 installed = 0;
1441 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
1442 zlog_warn ("%u:%s/%d: Route install failed",
1443 zvrf->vrf_id, buf, rn->p.prefixlen);
1444 }
1445 }
1446
1447 /* Update for redistribution. */
1448 if (installed)
1449 redistribute_update (&rn->p, select, NULL);
1450 UNSET_FLAG(select->flags, ZEBRA_FLAG_CHANGED);
1451 }
1452
1453 static void
1454 rib_process_del_route (struct zebra_vrf *zvrf, struct route_node *rn,
1455 struct rib *fib)
1456 {
1457 char buf[INET6_ADDRSTRLEN];
1458
1459 zfpm_trigger_update (rn, "removing existing route");
1460
1461 /* Withdraw redistribute and uninstall from kernel. */
1462 if (IS_ZEBRA_DEBUG_RIB)
1463 {
1464 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
1465 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1466 zvrf->vrf_id, buf, rn->p.prefixlen, rn, fib, fib->type);
1467 }
1468
1469 redistribute_delete(&rn->p, fib);
1470 if (!RIB_SYSTEM_ROUTE (fib))
1471 rib_uninstall_kernel (rn, fib);
1472
1473 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
1474
1475 /* Update nexthop for route, reset changed flag. */
1476 nexthop_active_update (rn, fib, 1);
1477 UNSET_FLAG(fib->flags, ZEBRA_FLAG_CHANGED);
1478 }
1479
1480 static void
1481 rib_process_update_route (struct zebra_vrf *zvrf, struct route_node *rn,
1482 struct rib *select, struct rib *fib)
1483 {
1484 char buf[INET6_ADDRSTRLEN];
1485 struct nexthop *nexthop = NULL, *tnexthop;
1486 int recursing;
1487 int nh_active = 0;
1488 int installed = 1;
1489
1490 if (IS_ZEBRA_DEBUG_RIB)
1491 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
1492
1493 /*
1494 * We have to install or update if a new route has been selected or
1495 * something has changed.
1496 */
1497 if (select != fib ||
1498 CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
1499 {
1500 zfpm_trigger_update (rn, "updating existing route");
1501
1502 /* Update the nexthop; we could determine here that nexthop is inactive. */
1503 if (nexthop_active_update (rn, select, 1))
1504 nh_active = 1;
1505
1506 /* If nexthop is active, install the selected route, if appropriate. If
1507 * the install succeeds, cleanup flags for prior route, if different from
1508 * newly selected.
1509 */
1510 if (nh_active)
1511 {
1512 if (IS_ZEBRA_DEBUG_RIB)
1513 {
1514 if (select != fib)
1515 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1516 "old %p (type %d)", zvrf->vrf_id, buf, rn->p.prefixlen,
1517 rn, select, select->type, fib, fib->type);
1518 else
1519 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1520 zvrf->vrf_id, buf, rn->p.prefixlen, rn, select, select->type);
1521 }
1522 /* Non-system route should be installed. */
1523 if (!RIB_SYSTEM_ROUTE (select))
1524 {
1525 if (rib_install_kernel (rn, select, 1))
1526 {
1527 installed = 0;
1528 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
1529 zlog_warn ("%u:%s/%d: Route install failed",
1530 zvrf->vrf_id, buf, rn->p.prefixlen);
1531 }
1532 }
1533
1534 /* If install succeeded or system route, cleanup flags for prior route. */
1535 if (installed && select != fib)
1536 {
1537 if (RIB_SYSTEM_ROUTE(select))
1538 {
1539 if (!RIB_SYSTEM_ROUTE (fib))
1540 rib_uninstall_kernel (rn, fib);
1541 }
1542 else
1543 {
1544 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
1545 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1546 }
1547
1548 }
1549
1550 /* Update for redistribution. */
1551 if (installed)
1552 {
1553 SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1554 redistribute_update (&rn->p, select, (select == fib) ? NULL : fib);
1555 }
1556 }
1557
1558 /*
1559 * If nexthop for selected route is not active or install failed, we
1560 * may need to uninstall and delete for redistribution.
1561 */
1562 if (!nh_active || !installed)
1563 {
1564 struct rib *del;
1565
1566 if (IS_ZEBRA_DEBUG_RIB)
1567 {
1568 if (select != fib)
1569 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1570 "old %p (type %d) - %s", zvrf->vrf_id, buf, rn->p.prefixlen,
1571 rn, select, select->type, fib, fib->type,
1572 nh_active ? "install failed" : "nexthop inactive");
1573 else
1574 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1575 zvrf->vrf_id, buf, rn->p.prefixlen, rn, select, select->type,
1576 nh_active ? "install failed" : "nexthop inactive");
1577 }
1578
1579 del = (select == fib) ? select : fib;
1580
1581 redistribute_delete(&rn->p, del);
1582
1583 if (!RIB_SYSTEM_ROUTE (del))
1584 rib_uninstall_kernel (rn, del);
1585 UNSET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1586 }
1587 }
1588 else
1589 {
1590 /*
1591 * Same route selected; check if in the FIB and if not, re-install. This
1592 * is housekeeping code to deal with race conditions in kernel with linux
1593 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1594 * to add routes.
1595 */
1596 if (!RIB_SYSTEM_ROUTE (select))
1597 {
1598 int in_fib = 0;
1599
1600 for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing))
1601 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
1602 {
1603 in_fib = 1;
1604 break;
1605 }
1606 if (!in_fib)
1607 rib_install_kernel (rn, select, 0);
1608 }
1609 }
1610
1611 /* Update prior route. */
1612 if (select != fib)
1613 {
1614 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
1615
1616 /* Set real nexthop. */
1617 nexthop_active_update (rn, fib, 1);
1618 UNSET_FLAG(fib->flags, ZEBRA_FLAG_CHANGED);
1619 }
1620
1621 /* Clear changed flag. */
1622 UNSET_FLAG(select->flags, ZEBRA_FLAG_CHANGED);
1623 }
1624
1625 /* Core function for processing routing information base. */
1626 static void
1627 rib_process (struct route_node *rn)
1628 {
1629 struct rib *rib;
1630 struct rib *next;
1631 struct rib *fib = NULL;
1632 struct rib *select = NULL;
1633 struct rib *del = 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->flags, ZEBRA_FLAG_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->flags, ZEBRA_FLAG_CHANGED);
1731 continue;
1732 }
1733
1734 /* Newly selected rib, the common case. */
1735 if (!select)
1736 {
1737 select = rib;
1738 continue;
1739 }
1740
1741 /* filter route selection in following order:
1742 * - connected beats other types
1743 * - lower distance beats higher
1744 * - lower metric beats higher for equal distance
1745 * - last, hence oldest, route wins tie break.
1746 */
1747
1748 /* Connected routes. Pick the last connected
1749 * route of the set of lowest metric connected routes.
1750 */
1751 if (rib->type == ZEBRA_ROUTE_CONNECT)
1752 {
1753 if (select->type != ZEBRA_ROUTE_CONNECT
1754 || rib->metric <= select->metric)
1755 {
1756 UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED);
1757 select = rib;
1758 }
1759 else
1760 UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1761 continue;
1762 }
1763 else if (select->type == ZEBRA_ROUTE_CONNECT)
1764 {
1765 UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1766 continue;
1767 }
1768
1769 /* higher distance loses */
1770 if (rib->distance > select->distance)
1771 {
1772 UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1773 continue;
1774 }
1775
1776 /* lower wins */
1777 if (rib->distance < select->distance)
1778 {
1779 UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED);
1780 select = rib;
1781 continue;
1782 }
1783
1784 /* metric tie-breaks equal distance */
1785 if (rib->metric <= select->metric)
1786 {
1787 UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED);
1788 select = rib;
1789 }
1790 } /* RNODE_FOREACH_RIB_SAFE */
1791
1792 /* After the cycle is finished, the following pointers will be set:
1793 * select --- the winner RIB entry, if any was found, otherwise NULL
1794 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1795 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1796 * rib --- NULL
1797 */
1798 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1799 zlog_debug ("%u:%s/%d: After processing: select %p fib %p del %p",
1800 vrf_id, buf, rn->p.prefixlen, select, fib, del);
1801
1802 /* Same RIB entry is selected. Update FIB and finish. */
1803 if (select && select == fib)
1804 rib_process_update_route (zvrf, rn, select, select);
1805 else if (select && fib)
1806 rib_process_update_route (zvrf, rn, select, fib);
1807 else if (select)
1808 rib_process_add_route (zvrf, rn, select);
1809 else if (fib)
1810 rib_process_del_route (zvrf, rn, fib);
1811
1812 #if 0
1813 if (select && select == fib)
1814 {
1815 if (IS_ZEBRA_DEBUG_RIB)
1816 rnode_debug (rn, vrf_id, "Updating existing route, select %p, fib %p",
1817 (void *)select, (void *)fib);
1818 if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
1819 {
1820 if (info->safi == SAFI_UNICAST)
1821 zfpm_trigger_update (rn, "updating existing route");
1822
1823 /* Set real nexthop. */
1824 /* Need to check if any NHs are active to clear the
1825 * the selected flag
1826 */
1827 if (nexthop_active_update (rn, select, 1))
1828 {
1829 if (IS_ZEBRA_DEBUG_RIB)
1830 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1831 vrf_id, buf, rn->p.prefixlen, rn, select, select->type);
1832 if (! RIB_SYSTEM_ROUTE (select))
1833 {
1834 /* Clear FIB flag if performing a replace, will get set again
1835 * as part of install.
1836 */
1837 for (nexthop = select->nexthop; nexthop; nexthop = nexthop->next)
1838 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1839 rib_install_kernel (rn, select, 1);
1840 }
1841
1842 /* assuming that the receiver knows how to dedup */
1843 redistribute_update (&rn->p, select, NULL);
1844 }
1845 else
1846 {
1847 if (IS_ZEBRA_DEBUG_RIB)
1848 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1849 "- nexthop inactive",
1850 vrf_id, buf, rn->p.prefixlen, rn, select, select->type);
1851
1852 /* Withdraw unreachable redistribute route */
1853 redistribute_delete(&rn->p, select);
1854
1855 /* Do the uninstall here, if not done earlier. */
1856 if (! RIB_SYSTEM_ROUTE (select))
1857 rib_uninstall_kernel (rn, select);
1858 UNSET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1859 }
1860 UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED);
1861 }
1862 else if (! RIB_SYSTEM_ROUTE (select))
1863 {
1864 /* Housekeeping code to deal with
1865 race conditions in kernel with linux
1866 netlink reporting interface up before IPv4 or IPv6 protocol
1867 is ready to add routes.
1868 This makes sure the routes are IN the kernel.
1869 */
1870
1871 for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing))
1872 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
1873 {
1874 installed = 1;
1875 break;
1876 }
1877 if (! installed)
1878 rib_install_kernel (rn, select, 0);
1879 }
1880 goto end;
1881 }
1882
1883 /* At this point we either haven't found the best RIB entry or it is
1884 * different from what we currently intend to flag with SELECTED. In both
1885 * cases, if a RIB block is present in FIB, it should be withdrawn.
1886 */
1887 if (fib)
1888 {
1889 if (IS_ZEBRA_DEBUG_RIB)
1890 rnode_debug (rn, vrf_id, "Removing existing route, fib %p", (void *)fib);
1891
1892 if (info->safi == SAFI_UNICAST)
1893 zfpm_trigger_update (rn, "removing existing route");
1894
1895 /* If there's no route to replace this with, withdraw redistribute and
1896 * uninstall from kernel.
1897 */
1898 if (!select)
1899 {
1900 if (IS_ZEBRA_DEBUG_RIB)
1901 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1902 vrf_id, buf, rn->p.prefixlen, rn, fib, fib->type);
1903
1904 redistribute_delete(&rn->p, fib);
1905 if (! RIB_SYSTEM_ROUTE (fib))
1906 rib_uninstall_kernel (rn, fib);
1907 }
1908
1909 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
1910
1911 /* Set real nexthop. */
1912 nexthop_active_update (rn, fib, 1);
1913 UNSET_FLAG(fib->flags, ZEBRA_FLAG_CHANGED);
1914 }
1915
1916 /* Regardless of some RIB entry being SELECTED or not before, now we can
1917 * tell, that if a new winner exists, FIB is still not updated with this
1918 * data, but ready to be.
1919 */
1920 if (select)
1921 {
1922 if (IS_ZEBRA_DEBUG_RIB)
1923 rnode_debug (rn, "Adding route, select %p", (void *)select);
1924
1925 if (info->safi == SAFI_UNICAST)
1926 zfpm_trigger_update (rn, "new route selected");
1927
1928 /* Set real nexthop. */
1929 if (nexthop_active_update (rn, select, 1))
1930 {
1931 if (IS_ZEBRA_DEBUG_RIB)
1932 {
1933 if (fib)
1934 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1935 "old %p (type %d)", vrf_id, buf, rn->p.prefixlen, rn,
1936 select, select->type, fib, fib->type);
1937 else
1938 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1939 vrf_id, buf, rn->p.prefixlen, rn, select, select->type);
1940 }
1941
1942 if (! RIB_SYSTEM_ROUTE (select))
1943 {
1944 /* Clear FIB flag if performing a replace, will get set again
1945 * as part of install.
1946 */
1947 if (fib)
1948 {
1949 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
1950 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1951 }
1952 rib_install_kernel (rn, select, fib? 1 : 0);
1953 }
1954 else
1955 {
1956 /* Uninstall prior route here, if needed. */
1957 if (fib && !RIB_SYSTEM_ROUTE (fib))
1958 rib_uninstall_kernel (rn, fib);
1959 }
1960
1961 SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1962 /* Unconditionally announce, this part is exercised by new routes */
1963 /* If we cannot add, for example route added is learnt by the */
1964 /* protocol we're trying to redistribute to, delete the redist */
1965 /* This is notified by setting the is_update to 1 */
1966 redistribute_update (&rn->p, select, fib);
1967 }
1968 else
1969 {
1970 /* Uninstall prior route here and do redist delete, if needed. */
1971 if (fib)
1972 {
1973 if (IS_ZEBRA_DEBUG_RIB)
1974 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1975 "- nexthop inactive",
1976 vrf_id, buf, rn->p.prefixlen, rn, fib, fib->type);
1977
1978 if (!RIB_SYSTEM_ROUTE (fib))
1979 rib_uninstall_kernel (rn, fib);
1980 redistribute_delete(&rn->p, fib);
1981 }
1982 }
1983 UNSET_FLAG(select->flags, ZEBRA_FLAG_CHANGED);
1984 }
1985 #endif
1986
1987 /* FIB route was removed, should be deleted */
1988 if (del)
1989 {
1990 if (IS_ZEBRA_DEBUG_RIB)
1991 rnode_debug (rn, vrf_id, "Deleting fib %p, rn %p", (void *)del, (void *)rn);
1992 rib_unlink (rn, del);
1993 }
1994
1995 /*
1996 * Check if the dest can be deleted now.
1997 */
1998 rib_gc_dest (rn);
1999 }
2000
2001 /* Take a list of route_node structs and return 1, if there was a record
2002 * picked from it and processed by rib_process(). Don't process more,
2003 * than one RN record; operate only in the specified sub-queue.
2004 */
2005 static unsigned int
2006 process_subq (struct list * subq, u_char qindex)
2007 {
2008 struct listnode *lnode = listhead (subq);
2009 struct route_node *rnode;
2010 char buf[INET6_ADDRSTRLEN];
2011 rib_dest_t *dest;
2012 struct zebra_vrf *zvrf = NULL;
2013
2014 if (!lnode)
2015 return 0;
2016
2017 rnode = listgetdata (lnode);
2018 dest = rib_dest_from_rnode (rnode);
2019 if (dest)
2020 zvrf = rib_dest_vrf (dest);
2021
2022 rib_process (rnode);
2023
2024 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2025 {
2026 inet_ntop (rnode->p.family, &rnode->p.u.prefix, buf, INET6_ADDRSTRLEN);
2027 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
2028 zvrf ? zvrf->vrf_id : 0, buf, rnode->p.prefixlen, rnode, qindex);
2029 }
2030
2031 if (rnode->info)
2032 UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex));
2033
2034 #if 0
2035 else
2036 {
2037 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2038 __func__, rnode, rnode->lock);
2039 zlog_backtrace(LOG_DEBUG);
2040 }
2041 #endif
2042 route_unlock_node (rnode);
2043 list_delete_node (subq, lnode);
2044 return 1;
2045 }
2046
2047 /*
2048 * All meta queues have been processed. Trigger next-hop evaluation.
2049 */
2050 static void
2051 meta_queue_process_complete (struct work_queue *dummy)
2052 {
2053 vrf_iter_t iter;
2054 struct zebra_vrf *zvrf;
2055
2056 /* Evaluate nexthops for those VRFs which underwent route processing. This
2057 * should limit the evaluation to the necessary VRFs in most common
2058 * situations.
2059 */
2060 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
2061 {
2062 if (((zvrf = vrf_iter2info (iter)) != NULL) &&
2063 (zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED))
2064 {
2065 zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED;
2066 zebra_evaluate_rnh(zvrf->vrf_id, AF_INET, 0, RNH_NEXTHOP_TYPE, NULL);
2067 zebra_evaluate_rnh(zvrf->vrf_id, AF_INET, 0, RNH_IMPORT_CHECK_TYPE, NULL);
2068 zebra_evaluate_rnh(zvrf->vrf_id, AF_INET6, 0, RNH_NEXTHOP_TYPE, NULL);
2069 zebra_evaluate_rnh(zvrf->vrf_id, AF_INET6, 0, RNH_IMPORT_CHECK_TYPE, NULL);
2070 }
2071 }
2072 }
2073
2074 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2075 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2076 * is pointed to the meta queue structure.
2077 */
2078 static wq_item_status
2079 meta_queue_process (struct work_queue *dummy, void *data)
2080 {
2081 struct meta_queue * mq = data;
2082 unsigned i;
2083
2084 for (i = 0; i < MQ_SIZE; i++)
2085 if (process_subq (mq->subq[i], i))
2086 {
2087 mq->size--;
2088 break;
2089 }
2090 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
2091 }
2092
2093 /*
2094 * Map from rib types to queue type (priority) in meta queue
2095 */
2096 static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = {
2097 [ZEBRA_ROUTE_SYSTEM] = 4,
2098 [ZEBRA_ROUTE_KERNEL] = 0,
2099 [ZEBRA_ROUTE_CONNECT] = 0,
2100 [ZEBRA_ROUTE_STATIC] = 1,
2101 [ZEBRA_ROUTE_RIP] = 2,
2102 [ZEBRA_ROUTE_RIPNG] = 2,
2103 [ZEBRA_ROUTE_OSPF] = 2,
2104 [ZEBRA_ROUTE_OSPF6] = 2,
2105 [ZEBRA_ROUTE_ISIS] = 2,
2106 [ZEBRA_ROUTE_BGP] = 3,
2107 [ZEBRA_ROUTE_HSLS] = 4,
2108 [ZEBRA_ROUTE_TABLE] = 1,
2109 };
2110
2111 /* Look into the RN and queue it into one or more priority queues,
2112 * increasing the size for each data push done.
2113 */
2114 static void
2115 rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn)
2116 {
2117 struct rib *rib;
2118
2119 RNODE_FOREACH_RIB (rn, rib)
2120 {
2121 u_char qindex = meta_queue_map[rib->type];
2122 struct zebra_vrf *zvrf;
2123
2124 /* Invariant: at this point we always have rn->info set. */
2125 if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags,
2126 RIB_ROUTE_QUEUED (qindex)))
2127 {
2128 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2129 rnode_debug (rn, rib->vrf_id, "rn %p is already queued in sub-queue %u",
2130 (void *)rn, qindex);
2131 continue;
2132 }
2133
2134 SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex));
2135 listnode_add (mq->subq[qindex], rn);
2136 route_lock_node (rn);
2137 mq->size++;
2138
2139 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2140 rnode_debug (rn, rib->vrf_id, "queued rn %p into sub-queue %u",
2141 (void *)rn, qindex);
2142
2143 zvrf = zebra_vrf_lookup (rib->vrf_id);
2144 if (zvrf)
2145 zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED;
2146 }
2147 }
2148
2149 /* Add route_node to work queue and schedule processing */
2150 void
2151 rib_queue_add (struct route_node *rn)
2152 {
2153 assert (rn);
2154
2155 /* Pointless to queue a route_node with no RIB entries to add or remove */
2156 if (!rnode_to_ribs (rn))
2157 {
2158 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2159 __func__, (void *)rn, rn->lock);
2160 zlog_backtrace(LOG_DEBUG);
2161 return;
2162 }
2163
2164 if (zebrad.ribq == NULL)
2165 {
2166 zlog_err ("%s: work_queue does not exist!", __func__);
2167 return;
2168 }
2169
2170 /*
2171 * The RIB queue should normally be either empty or holding the only
2172 * work_queue_item element. In the latter case this element would
2173 * hold a pointer to the meta queue structure, which must be used to
2174 * actually queue the route nodes to process. So create the MQ
2175 * holder, if necessary, then push the work into it in any case.
2176 * This semantics was introduced after 0.99.9 release.
2177 */
2178 if (!zebrad.ribq->items->count)
2179 work_queue_add (zebrad.ribq, zebrad.mq);
2180
2181 rib_meta_queue_add (zebrad.mq, rn);
2182
2183 return;
2184 }
2185
2186 /* Create new meta queue.
2187 A destructor function doesn't seem to be necessary here.
2188 */
2189 static struct meta_queue *
2190 meta_queue_new (void)
2191 {
2192 struct meta_queue *new;
2193 unsigned i;
2194
2195 new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue));
2196 assert(new);
2197
2198 for (i = 0; i < MQ_SIZE; i++)
2199 {
2200 new->subq[i] = list_new ();
2201 assert(new->subq[i]);
2202 }
2203
2204 return new;
2205 }
2206
2207 /* initialise zebra rib work queue */
2208 static void
2209 rib_queue_init (struct zebra_t *zebra)
2210 {
2211 assert (zebra);
2212
2213 if (! (zebra->ribq = work_queue_new (zebra->master,
2214 "route_node processing")))
2215 {
2216 zlog_err ("%s: could not initialise work queue!", __func__);
2217 return;
2218 }
2219
2220 /* fill in the work queue spec */
2221 zebra->ribq->spec.workfunc = &meta_queue_process;
2222 zebra->ribq->spec.errorfunc = NULL;
2223 zebra->ribq->spec.completion_func = &meta_queue_process_complete;
2224 /* XXX: TODO: These should be runtime configurable via vty */
2225 zebra->ribq->spec.max_retries = 3;
2226 zebra->ribq->spec.hold = rib_process_hold_time;
2227
2228 if (!(zebra->mq = meta_queue_new ()))
2229 {
2230 zlog_err ("%s: could not initialise meta queue!", __func__);
2231 return;
2232 }
2233 return;
2234 }
2235
2236 /* RIB updates are processed via a queue of pointers to route_nodes.
2237 *
2238 * The queue length is bounded by the maximal size of the routing table,
2239 * as a route_node will not be requeued, if already queued.
2240 *
2241 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2242 * state, or static_install_route (when an existing RIB is updated)
2243 * and then submit route_node to queue for best-path selection later.
2244 * Order of add/delete state changes are preserved for any given RIB.
2245 *
2246 * Deleted RIBs are reaped during best-path selection.
2247 *
2248 * rib_addnode
2249 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2250 * |-------->| | best RIB, if required
2251 * | |
2252 * static_install->|->rib_addqueue...... -> rib_process
2253 * | |
2254 * |-------->| |-> rib_unlink
2255 * |-> set RIB_ENTRY_REMOVE |
2256 * rib_delnode (RIB freed)
2257 *
2258 * The 'info' pointer of a route_node points to a rib_dest_t
2259 * ('dest'). Queueing state for a route_node is kept on the dest. The
2260 * dest is created on-demand by rib_link() and is kept around at least
2261 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2262 *
2263 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2264 *
2265 * - route_nodes: refcounted by:
2266 * - dest attached to route_node:
2267 * - managed by: rib_link/rib_gc_dest
2268 * - route_node processing queue
2269 * - managed by: rib_addqueue, rib_process.
2270 *
2271 */
2272
2273 /* Add RIB to head of the route node. */
2274 static void
2275 rib_link (struct route_node *rn, struct rib *rib, int process)
2276 {
2277 struct rib *head;
2278 rib_dest_t *dest;
2279 afi_t afi;
2280 const char *rmap_name;
2281
2282 assert (rib && rn);
2283
2284 dest = rib_dest_from_rnode (rn);
2285 if (!dest)
2286 {
2287 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2288 rnode_debug (rn, rib->vrf_id, "rn %p adding dest", rn);
2289
2290 dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t));
2291 route_lock_node (rn); /* rn route table reference */
2292 rn->info = dest;
2293 dest->rnode = rn;
2294 }
2295
2296 head = dest->routes;
2297 if (head)
2298 {
2299 head->prev = rib;
2300 }
2301 rib->next = head;
2302 dest->routes = rib;
2303
2304 afi = (rn->p.family == AF_INET) ? AFI_IP :
2305 (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2306 if (is_zebra_import_table_enabled (afi, rib->table))
2307 {
2308 rmap_name = zebra_get_import_table_route_map (afi, rib->table);
2309 zebra_add_import_table_entry(rn, rib, rmap_name);
2310 }
2311 else
2312 if (process)
2313 rib_queue_add (rn);
2314 }
2315
2316 static void
2317 rib_addnode (struct route_node *rn, struct rib *rib, int process)
2318 {
2319 /* RIB node has been un-removed before route-node is processed.
2320 * route_node must hence already be on the queue for processing..
2321 */
2322 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2323 {
2324 if (IS_ZEBRA_DEBUG_RIB)
2325 rnode_debug (rn, rib->vrf_id, "rn %p, un-removed rib %p", (void *)rn, (void *)rib);
2326
2327 UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
2328 return;
2329 }
2330 rib_link (rn, rib, process);
2331 }
2332
2333 /*
2334 * rib_unlink
2335 *
2336 * Detach a rib structure from a route_node.
2337 *
2338 * Note that a call to rib_unlink() should be followed by a call to
2339 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2340 * longer required to be deleted.
2341 */
2342 static void
2343 rib_unlink (struct route_node *rn, struct rib *rib)
2344 {
2345 rib_dest_t *dest;
2346
2347 assert (rn && rib);
2348
2349 if (IS_ZEBRA_DEBUG_RIB)
2350 rnode_debug (rn, rib->vrf_id, "rn %p, rib %p", (void *)rn, (void *)rib);
2351
2352 dest = rib_dest_from_rnode (rn);
2353
2354 if (rib->next)
2355 rib->next->prev = rib->prev;
2356
2357 if (rib->prev)
2358 rib->prev->next = rib->next;
2359 else
2360 {
2361 dest->routes = rib->next;
2362 }
2363
2364 /* free RIB and nexthops */
2365 zebra_deregister_rnh_static_nexthops (rib->vrf_id, rib->nexthop, rn);
2366 nexthops_free(rib->nexthop);
2367 XFREE (MTYPE_RIB, rib);
2368
2369 }
2370
2371 static void
2372 rib_delnode (struct route_node *rn, struct rib *rib)
2373 {
2374 afi_t afi;
2375
2376 if (IS_ZEBRA_DEBUG_RIB)
2377 rnode_debug (rn, rib->vrf_id, "rn %p, rib %p, removing", (void *)rn, (void *)rib);
2378 SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
2379
2380 afi = (rn->p.family == AF_INET) ? AFI_IP :
2381 (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2382 if (is_zebra_import_table_enabled (afi, rib->table))
2383 {
2384 zebra_del_import_table_entry(rn, rib);
2385 /* Just clean up if non main table */
2386 if (IS_ZEBRA_DEBUG_RIB)
2387 {
2388 char buf[INET6_ADDRSTRLEN];
2389 if (IS_ZEBRA_DEBUG_RIB)
2390 {
2391 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
2392 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2393 rib->vrf_id, buf, rn->p.prefixlen, rn, rib, rib->type);
2394 }
2395 }
2396
2397 rib_unlink(rn, rib);
2398 }
2399 else
2400 {
2401 rib_queue_add (rn);
2402 }
2403 }
2404
2405 int
2406 rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
2407 struct in_addr *gate, struct in_addr *src,
2408 unsigned int ifindex, vrf_id_t vrf_id, u_int32_t table_id,
2409 u_int32_t metric, u_char distance, safi_t safi)
2410 {
2411 struct rib *rib;
2412 struct rib *same = NULL;
2413 struct route_table *table;
2414 struct route_node *rn;
2415 struct nexthop *nexthop;
2416
2417 /* Lookup table. */
2418 table = zebra_vrf_table_with_table_id (AFI_IP, safi, vrf_id, table_id);
2419 if (! table)
2420 return 0;
2421
2422 /* Make it sure prefixlen is applied to the prefix. */
2423 apply_mask_ipv4 (p);
2424
2425 /* Set default distance by route type. */
2426 if (distance == 0)
2427 {
2428 if ((unsigned)type >= array_size(route_info))
2429 distance = 150;
2430 else
2431 distance = route_info[type].distance;
2432
2433 /* iBGP distance is 200. */
2434 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2435 distance = 200;
2436 }
2437
2438 /* Lookup route node.*/
2439 rn = route_node_get (table, (struct prefix *) p);
2440
2441 /* If same type of route are installed, treat it as a implicit
2442 withdraw. */
2443 RNODE_FOREACH_RIB (rn, rib)
2444 {
2445 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2446 continue;
2447
2448 if (rib->type != type)
2449 continue;
2450 if (rib->instance != instance)
2451 continue;
2452
2453 if (rib->type != ZEBRA_ROUTE_CONNECT)
2454 {
2455 same = rib;
2456 break;
2457 }
2458 /* Duplicate connected route comes in. */
2459 else if ((nexthop = rib->nexthop) &&
2460 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
2461 nexthop->ifindex == ifindex &&
2462 !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2463 {
2464 rib->refcnt++;
2465 return 0 ;
2466 }
2467 }
2468
2469 /* Allocate new rib structure. */
2470 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2471 rib->type = type;
2472 rib->instance = instance;
2473 rib->distance = distance;
2474 rib->flags = flags;
2475 rib->metric = metric;
2476 rib->table = table_id;
2477 rib->vrf_id = vrf_id;
2478 rib->nexthop_num = 0;
2479 rib->uptime = time (NULL);
2480
2481 /* Nexthop settings. */
2482 if (gate)
2483 {
2484 if (ifindex)
2485 rib_nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
2486 else
2487 rib_nexthop_ipv4_add (rib, gate, src);
2488 }
2489 else
2490 rib_nexthop_ifindex_add (rib, ifindex);
2491
2492 /* If this route is kernel route, set FIB flag to the route. */
2493 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
2494 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2495 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2496
2497 /* Link new rib to node.*/
2498 if (IS_ZEBRA_DEBUG_RIB)
2499 {
2500 char buf[INET6_ADDRSTRLEN];
2501 if (IS_ZEBRA_DEBUG_RIB)
2502 {
2503 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
2504 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2505 "existing %p",
2506 vrf_id, buf, p->prefixlen, (void *)rn, (void *)rib, rib->type, (void *)same);
2507 }
2508
2509 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2510 rib_dump ((struct prefix *)p, rib);
2511 }
2512 rib_addnode (rn, rib, 1);
2513
2514 /* Free implicit route.*/
2515 if (same)
2516 rib_delnode (rn, same);
2517
2518 route_unlock_node (rn);
2519 return 0;
2520 }
2521
2522 /* This function dumps the contents of a given RIB entry into
2523 * standard debug log. Calling function name and IP prefix in
2524 * question are passed as 1st and 2nd arguments.
2525 */
2526
2527 void _rib_dump (const char * func,
2528 union prefix46constptr pp, const struct rib * rib)
2529 {
2530 const struct prefix *p = pp.p;
2531 char straddr[PREFIX_STRLEN];
2532 struct nexthop *nexthop, *tnexthop;
2533 int recursing;
2534
2535 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func, (const void *)rib,
2536 prefix2str(pp, straddr, sizeof(straddr)), rib->vrf_id);
2537 zlog_debug
2538 (
2539 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2540 func,
2541 rib->refcnt,
2542 (unsigned long) rib->uptime,
2543 rib->type,
2544 rib->instance,
2545 rib->table
2546 );
2547 zlog_debug
2548 (
2549 "%s: metric == %u, distance == %u, flags == %u, status == %u",
2550 func,
2551 rib->metric,
2552 rib->distance,
2553 rib->flags,
2554 rib->status
2555 );
2556 zlog_debug
2557 (
2558 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2559 func,
2560 rib->nexthop_num,
2561 rib->nexthop_active_num,
2562 rib->nexthop_fib_num
2563 );
2564
2565 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2566 {
2567 inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
2568 zlog_debug
2569 (
2570 "%s: %s %s with flags %s%s%s",
2571 func,
2572 (recursing ? " NH" : "NH"),
2573 straddr,
2574 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
2575 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
2576 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
2577 );
2578 }
2579 zlog_debug ("%s: dump complete", func);
2580 }
2581
2582 /* This is an exported helper to rtm_read() to dump the strange
2583 * RIB entry found by rib_lookup_ipv4_route()
2584 */
2585
2586 void rib_lookup_and_dump (struct prefix_ipv4 * p, vrf_id_t vrf_id)
2587 {
2588 struct route_table *table;
2589 struct route_node *rn;
2590 struct rib *rib;
2591 char prefix_buf[INET_ADDRSTRLEN];
2592
2593 /* Lookup table. */
2594 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
2595 if (! table)
2596 {
2597 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
2598 return;
2599 }
2600
2601 /* Scan the RIB table for exactly matching RIB entry. */
2602 rn = route_node_lookup (table, (struct prefix *) p);
2603
2604 /* No route for this prefix. */
2605 if (! rn)
2606 {
2607 zlog_debug ("%s: lookup failed for %s", __func__,
2608 prefix2str((struct prefix*) p, prefix_buf, sizeof(prefix_buf)));
2609 return;
2610 }
2611
2612 /* Unlock node. */
2613 route_unlock_node (rn);
2614
2615 /* let's go */
2616 RNODE_FOREACH_RIB (rn, rib)
2617 {
2618 zlog_debug
2619 (
2620 "%s: rn %p, rib %p: %s, %s",
2621 __func__,
2622 (void *)rn,
2623 (void *)rib,
2624 (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
2625 (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
2626 );
2627 rib_dump (p, rib);
2628 }
2629 }
2630
2631 /* Check if requested address assignment will fail due to another
2632 * route being installed by zebra in FIB already. Take necessary
2633 * actions, if needed: remove such a route from FIB and deSELECT
2634 * corresponding RIB entry. Then put affected RN into RIBQ head.
2635 */
2636 void rib_lookup_and_pushup (struct prefix_ipv4 * p, vrf_id_t vrf_id)
2637 {
2638 struct route_table *table;
2639 struct route_node *rn;
2640 struct rib *rib;
2641 unsigned changed = 0;
2642
2643 if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id)))
2644 {
2645 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
2646 return;
2647 }
2648
2649 /* No matches would be the simplest case. */
2650 if (NULL == (rn = route_node_lookup (table, (struct prefix *) p)))
2651 return;
2652
2653 /* Unlock node. */
2654 route_unlock_node (rn);
2655
2656 /* Check all RIB entries. In case any changes have to be done, requeue
2657 * the RN into RIBQ head. If the routing message about the new connected
2658 * route (generated by the IP address we are going to assign very soon)
2659 * comes before the RIBQ is processed, the new RIB entry will join
2660 * RIBQ record already on head. This is necessary for proper revalidation
2661 * of the rest of the RIB.
2662 */
2663 RNODE_FOREACH_RIB (rn, rib)
2664 {
2665 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) &&
2666 ! RIB_SYSTEM_ROUTE (rib))
2667 {
2668 changed = 1;
2669 if (IS_ZEBRA_DEBUG_RIB)
2670 {
2671 char buf[PREFIX_STRLEN];
2672 zlog_debug ("%u:%s: freeing way for connected prefix",
2673 rib->vrf_id, prefix2str(&rn->p, buf, sizeof(buf)));
2674 rib_dump (&rn->p, rib);
2675 }
2676 rib_uninstall (rn, rib);
2677 }
2678 }
2679 if (changed)
2680 rib_queue_add (rn);
2681 }
2682
2683 int
2684 rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
2685 {
2686 struct route_table *table;
2687 struct route_node *rn;
2688 struct rib *same;
2689 struct nexthop *nexthop;
2690 int ret = 0;
2691
2692 /* Lookup table. */
2693 table = zebra_vrf_table_with_table_id (AFI_IP, safi, rib->vrf_id, rib->table);
2694 if (! table)
2695 return 0;
2696
2697 /* Make it sure prefixlen is applied to the prefix. */
2698 apply_mask_ipv4 (p);
2699
2700 /* Set default distance by route type. */
2701 if (rib->distance == 0)
2702 {
2703 rib->distance = route_info[rib->type].distance;
2704
2705 /* iBGP distance is 200. */
2706 if (rib->type == ZEBRA_ROUTE_BGP
2707 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
2708 rib->distance = 200;
2709 }
2710
2711 /* Lookup route node.*/
2712 rn = route_node_get (table, (struct prefix *) p);
2713
2714 /* If same type of route are installed, treat it as a implicit
2715 withdraw. */
2716 RNODE_FOREACH_RIB (rn, same)
2717 {
2718 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
2719 continue;
2720
2721 if (same->type == rib->type && same->instance == rib->instance
2722 && same->table == rib->table
2723 && same->type != ZEBRA_ROUTE_CONNECT)
2724 break;
2725 }
2726
2727 /* If this route is kernel route, set FIB flag to the route. */
2728 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
2729 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2730 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2731
2732 /* Link new rib to node.*/
2733 if (IS_ZEBRA_DEBUG_RIB)
2734 {
2735 char buf[INET6_ADDRSTRLEN];
2736 if (IS_ZEBRA_DEBUG_RIB)
2737 {
2738 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
2739 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2740 "existing %p",
2741 rib->vrf_id, buf, p->prefixlen, (void *)rn,
2742 (void *)rib, rib->type, (void *)same);
2743 }
2744
2745 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2746 rib_dump ((struct prefix *)p, rib);
2747 }
2748 rib_addnode (rn, rib, 1);
2749 ret = 1;
2750
2751 /* Free implicit route.*/
2752 if (same)
2753 {
2754 rib_delnode (rn, same);
2755 ret = -1;
2756 }
2757
2758 route_unlock_node (rn);
2759 return ret;
2760 }
2761
2762 /* XXX factor with rib_delete_ipv6 */
2763 int
2764 rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
2765 struct in_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
2766 u_int32_t table_id, safi_t safi)
2767 {
2768 struct route_table *table;
2769 struct route_node *rn;
2770 struct rib *rib;
2771 struct rib *fib = NULL;
2772 struct rib *same = NULL;
2773 struct nexthop *nexthop, *tnexthop;
2774 int recursing;
2775 char buf1[PREFIX_STRLEN];
2776 char buf2[INET6_ADDRSTRLEN];
2777
2778 /* Lookup table. */
2779 table = zebra_vrf_table_with_table_id (AFI_IP, safi, vrf_id, table_id);
2780 if (! table)
2781 return 0;
2782
2783 /* Apply mask. */
2784 apply_mask_ipv4 (p);
2785
2786 /* Lookup route node. */
2787 rn = route_node_lookup (table, (struct prefix *) p);
2788 if (! rn)
2789 {
2790 if (IS_ZEBRA_DEBUG_RIB)
2791 zlog_debug ("%u:%s: doesn't exist in rib",
2792 vrf_id, prefix2str (p, buf1, sizeof(buf1)));
2793 return ZEBRA_ERR_RTNOEXIST;
2794 }
2795
2796 /* Lookup same type route. */
2797 RNODE_FOREACH_RIB (rn, rib)
2798 {
2799 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2800 continue;
2801
2802 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
2803 fib = rib;
2804
2805 if (rib->type != type)
2806 continue;
2807 if (rib->instance != instance)
2808 continue;
2809 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
2810 nexthop->type == NEXTHOP_TYPE_IFINDEX)
2811 {
2812 if (nexthop->ifindex != ifindex)
2813 continue;
2814 if (rib->refcnt)
2815 {
2816 rib->refcnt--;
2817 route_unlock_node (rn);
2818 route_unlock_node (rn);
2819 return 0;
2820 }
2821 same = rib;
2822 break;
2823 }
2824 /* Make sure that the route found has the same gateway. */
2825 else
2826 {
2827 if (gate == NULL)
2828 {
2829 same = rib;
2830 break;
2831 }
2832 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2833 if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate))
2834 {
2835 same = rib;
2836 break;
2837 }
2838 if (same)
2839 break;
2840 }
2841 }
2842 /* If same type of route can't be found and this message is from
2843 kernel. */
2844 if (! same)
2845 {
2846 if (fib && type == ZEBRA_ROUTE_KERNEL &&
2847 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
2848 {
2849 if (IS_ZEBRA_DEBUG_RIB)
2850 {
2851 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
2852 "from kernel, adding",
2853 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
2854 p->prefixlen, rn, fib, fib->type);
2855 }
2856 if (allow_delete)
2857 {
2858 /* Unset flags. */
2859 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
2860 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2861
2862 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
2863 }
2864 else
2865 {
2866 /* This means someone else, other than Zebra, has deleted
2867 * a Zebra router from the kernel. We will add it back */
2868 rib_install_kernel(rn, fib, 0);
2869 }
2870 }
2871 else
2872 {
2873 if (IS_ZEBRA_DEBUG_RIB)
2874 {
2875 if (gate)
2876 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2877 "doesn't exist in rib",
2878 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
2879 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
2880 ifindex,
2881 type);
2882 else
2883 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2884 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
2885 ifindex,
2886 type);
2887 }
2888 route_unlock_node (rn);
2889 return ZEBRA_ERR_RTNOEXIST;
2890 }
2891 }
2892
2893 if (same)
2894 rib_delnode (rn, same);
2895
2896 route_unlock_node (rn);
2897 return 0;
2898 }
2899
2900 /* Install static route into rib. */
2901 void
2902 static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
2903 {
2904 struct rib *rib;
2905 struct route_node *rn;
2906 struct route_table *table;
2907 struct prefix nh_p;
2908
2909 /* Lookup table. */
2910 table = zebra_vrf_table (afi, safi, si->vrf_id);
2911 if (! table)
2912 return;
2913
2914 /* Lookup existing route */
2915 rn = route_node_get (table, p);
2916 RNODE_FOREACH_RIB (rn, rib)
2917 {
2918 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2919 continue;
2920
2921 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2922 break;
2923 }
2924
2925 if (rib)
2926 {
2927 /* if tag value changed , update old value in RIB */
2928 if (rib->tag != si->tag)
2929 rib->tag = si->tag;
2930
2931 /* Same distance static route is there. Update it with new
2932 nexthop. */
2933 route_unlock_node (rn);
2934 switch (si->type)
2935 {
2936 case STATIC_IPV4_GATEWAY:
2937 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
2938 nh_p.family = AF_INET;
2939 nh_p.prefixlen = IPV4_MAX_BITLEN;
2940 nh_p.u.prefix4 = si->addr.ipv4;
2941 zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn);
2942 break;
2943 case STATIC_IFINDEX:
2944 rib_nexthop_ifindex_add (rib, si->ifindex);
2945 break;
2946 case STATIC_IPV4_BLACKHOLE:
2947 rib_nexthop_blackhole_add (rib);
2948 break;
2949 case STATIC_IPV6_GATEWAY:
2950 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
2951 nh_p.family = AF_INET6;
2952 nh_p.prefixlen = IPV6_MAX_BITLEN;
2953 nh_p.u.prefix6 = si->addr.ipv6;
2954 zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn);
2955 break;
2956 case STATIC_IPV6_GATEWAY_IFINDEX:
2957 rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
2958 break;
2959 }
2960
2961 if (IS_ZEBRA_DEBUG_RIB)
2962 {
2963 char buf[INET6_ADDRSTRLEN];
2964 if (IS_ZEBRA_DEBUG_RIB)
2965 {
2966 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2967 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
2968 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
2969 }
2970 }
2971 /* Schedule route for processing or invoke NHT, as appropriate. */
2972 if (si->type == STATIC_IPV4_GATEWAY ||
2973 si->type == STATIC_IPV6_GATEWAY)
2974 zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p);
2975 else
2976 rib_queue_add (rn);
2977 }
2978 else
2979 {
2980 /* This is new static route. */
2981 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2982
2983 rib->type = ZEBRA_ROUTE_STATIC;
2984 rib->instance = 0;
2985 rib->distance = si->distance;
2986 rib->metric = 0;
2987 rib->vrf_id = si->vrf_id;
2988 rib->table = si->vrf_id ? (zebra_vrf_lookup(si->vrf_id))->table_id : zebrad.rtm_table_default;
2989 rib->nexthop_num = 0;
2990 rib->tag = si->tag;
2991
2992 switch (si->type)
2993 {
2994 case STATIC_IPV4_GATEWAY:
2995 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
2996 nh_p.family = AF_INET;
2997 nh_p.prefixlen = IPV4_MAX_BITLEN;
2998 nh_p.u.prefix4 = si->addr.ipv4;
2999 zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn);
3000 break;
3001 case STATIC_IFINDEX:
3002 rib_nexthop_ifindex_add (rib, si->ifindex);
3003 break;
3004 case STATIC_IPV4_BLACKHOLE:
3005 rib_nexthop_blackhole_add (rib);
3006 break;
3007 case STATIC_IPV6_GATEWAY:
3008 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
3009 nh_p.family = AF_INET6;
3010 nh_p.prefixlen = IPV6_MAX_BITLEN;
3011 nh_p.u.prefix6 = si->addr.ipv6;
3012 zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn);
3013 break;
3014 case STATIC_IPV6_GATEWAY_IFINDEX:
3015 rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
3016 break;
3017 }
3018
3019 /* Save the flags of this static routes (reject, blackhole) */
3020 rib->flags = si->flags;
3021
3022 if (IS_ZEBRA_DEBUG_RIB)
3023 {
3024 char buf[INET6_ADDRSTRLEN];
3025 if (IS_ZEBRA_DEBUG_RIB)
3026 {
3027 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
3028 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d)",
3029 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
3030 }
3031 }
3032 /* Link this rib to the tree. Schedule for processing or invoke NHT,
3033 * as appropriate.
3034 */
3035 if (si->type == STATIC_IPV4_GATEWAY ||
3036 si->type == STATIC_IPV6_GATEWAY)
3037 {
3038 rib_addnode (rn, rib, 0);
3039 zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p);
3040 }
3041 else
3042 rib_addnode (rn, rib, 1);
3043 }
3044 }
3045
3046 static int
3047 static_nexthop_same (struct nexthop *nexthop, struct static_route *si)
3048 {
3049 if (nexthop->type == NEXTHOP_TYPE_IPV4
3050 && si->type == STATIC_IPV4_GATEWAY
3051 && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4))
3052 return 1;
3053 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
3054 && si->type == STATIC_IFINDEX
3055 && nexthop->ifindex == si->ifindex)
3056 return 1;
3057 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
3058 && si->type == STATIC_IPV4_BLACKHOLE)
3059 return 1;
3060 if (nexthop->type == NEXTHOP_TYPE_IPV6
3061 && si->type == STATIC_IPV6_GATEWAY
3062 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6))
3063 return 1;
3064 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
3065 && si->type == STATIC_IPV6_GATEWAY_IFINDEX
3066 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)
3067 && nexthop->ifindex == si->ifindex)
3068 return 1;
3069 return 0;
3070 }
3071
3072 /* Uninstall static route from RIB. */
3073 void
3074 static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
3075 {
3076 struct route_node *rn;
3077 struct rib *rib;
3078 struct nexthop *nexthop;
3079 struct route_table *table;
3080 struct prefix nh_p;
3081
3082 /* Lookup table. */
3083 table = zebra_vrf_table (afi, safi, si->vrf_id);
3084 if (! table)
3085 return;
3086
3087 /* Lookup existing route with type and distance. */
3088 rn = route_node_lookup (table, p);
3089 if (! rn)
3090 return;
3091
3092 RNODE_FOREACH_RIB (rn, rib)
3093 {
3094 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3095 continue;
3096
3097 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance &&
3098 rib->tag == si->tag)
3099 break;
3100 }
3101
3102 if (! rib)
3103 {
3104 route_unlock_node (rn);
3105 return;
3106 }
3107
3108 /* Lookup nexthop. */
3109 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
3110 if (static_nexthop_same (nexthop, si))
3111 break;
3112
3113 /* Can't find nexthop. */
3114 if (! nexthop)
3115 {
3116 route_unlock_node (rn);
3117 return;
3118 }
3119
3120 /* Check nexthop. */
3121 if (rib->nexthop_num == 1)
3122 rib_delnode (rn, rib);
3123 else
3124 {
3125 /* Mark this nexthop as inactive and reinstall the route. Then, delete
3126 * the nexthop. There is no need to re-evaluate the route for this
3127 * scenario.
3128 */
3129 if (IS_ZEBRA_DEBUG_RIB)
3130 {
3131 char buf[INET6_ADDRSTRLEN];
3132 if (IS_ZEBRA_DEBUG_RIB)
3133 {
3134 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
3135 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
3136 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
3137 }
3138 }
3139 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
3140 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
3141 {
3142 /* If there are other active nexthops, do an update. */
3143 if (rib->nexthop_active_num > 1)
3144 {
3145 rib_install_kernel (rn, rib, 1);
3146 redistribute_update (&rn->p, rib, NULL);
3147 }
3148 else
3149 {
3150 redistribute_delete (&rn->p, rib);
3151 rib_uninstall_kernel (rn, rib);
3152 }
3153 }
3154
3155 if (afi == AFI_IP)
3156 {
3157 /* Delete the nexthop and dereg from NHT */
3158 nh_p.family = AF_INET;
3159 nh_p.prefixlen = IPV4_MAX_BITLEN;
3160 nh_p.u.prefix4 = nexthop->gate.ipv4;
3161 }
3162 else
3163 {
3164 nh_p.family = AF_INET6;
3165 nh_p.prefixlen = IPV6_MAX_BITLEN;
3166 nh_p.u.prefix6 = nexthop->gate.ipv6;
3167 }
3168 rib_nexthop_delete (rib, nexthop);
3169 zebra_deregister_rnh_static_nh(si->vrf_id, &nh_p, rn);
3170 nexthop_free (nexthop);
3171 }
3172 /* Unlock node. */
3173 route_unlock_node (rn);
3174 }
3175
3176 int
3177 static_add_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, unsigned int ifindex,
3178 const char *ifname, u_char flags, u_short tag,
3179 u_char distance, struct zebra_vrf *zvrf)
3180 {
3181 u_char type = 0;
3182 struct route_node *rn;
3183 struct static_route *si;
3184 struct static_route *pp;
3185 struct static_route *cp;
3186 struct static_route *update = NULL;
3187 struct route_table *stable = zvrf->stable[AFI_IP][safi];
3188
3189 if (! stable)
3190 return -1;
3191
3192 /* Lookup static route prefix. */
3193 rn = route_node_get (stable, p);
3194
3195 /* Make flags. */
3196 if (gate)
3197 type = STATIC_IPV4_GATEWAY;
3198 else if (ifindex)
3199 type = STATIC_IFINDEX;
3200 else
3201 type = STATIC_IPV4_BLACKHOLE;
3202
3203 /* Do nothing if there is a same static route. */
3204 for (si = rn->info; si; si = si->next)
3205 {
3206 if (type == si->type
3207 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
3208 && (! ifindex || ifindex == si->ifindex))
3209 {
3210 if ((distance == si->distance) && (tag == si->tag))
3211 {
3212 route_unlock_node (rn);
3213 return 0;
3214 }
3215 else
3216 update = si;
3217 }
3218 }
3219
3220 /* Distance or tag changed. */
3221 if (update)
3222 static_delete_ipv4 (safi, p, gate, ifindex, update->tag, update->distance, zvrf);
3223
3224 /* Make new static route structure. */
3225 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
3226
3227 si->type = type;
3228 si->distance = distance;
3229 si->flags = flags;
3230 si->tag = tag;
3231 si->vrf_id = zvrf->vrf_id;
3232 si->ifindex = ifindex;
3233 if (si->ifindex)
3234 strcpy(si->ifname, ifname);
3235
3236 if (gate)
3237 si->addr.ipv4 = *gate;
3238
3239 /* Add new static route information to the tree with sort by
3240 distance value and gateway address. */
3241 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
3242 {
3243 if (si->distance < cp->distance)
3244 break;
3245 if (si->distance > cp->distance)
3246 continue;
3247 if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
3248 {
3249 if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr))
3250 break;
3251 if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr))
3252 continue;
3253 }
3254 }
3255
3256 /* Make linked list. */
3257 if (pp)
3258 pp->next = si;
3259 else
3260 rn->info = si;
3261 if (cp)
3262 cp->prev = si;
3263 si->prev = pp;
3264 si->next = cp;
3265
3266 /* Install into rib. */
3267 static_install_route (AFI_IP, safi, p, si);
3268
3269 return 1;
3270 }
3271
3272 int
3273 static_delete_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, unsigned int ifindex,
3274 u_short tag, u_char distance, struct zebra_vrf *zvrf)
3275 {
3276 u_char type = 0;
3277 struct route_node *rn;
3278 struct static_route *si;
3279 struct route_table *stable;
3280
3281 /* Lookup table. */
3282 stable = zebra_vrf_static_table (AFI_IP, safi, zvrf);
3283 if (! stable)
3284 return -1;
3285
3286 /* Lookup static route prefix. */
3287 rn = route_node_lookup (stable, p);
3288 if (! rn)
3289 return 0;
3290
3291 /* Make flags. */
3292 if (gate)
3293 type = STATIC_IPV4_GATEWAY;
3294 else if (ifindex)
3295 type = STATIC_IFINDEX;
3296 else
3297 type = STATIC_IPV4_BLACKHOLE;
3298
3299 /* Find same static route is the tree */
3300 for (si = rn->info; si; si = si->next)
3301 if (type == si->type
3302 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
3303 && (! ifindex || ifindex == si->ifindex)
3304 && (! tag || (tag == si->tag)))
3305 break;
3306
3307 /* Can't find static route. */
3308 if (! si)
3309 {
3310 route_unlock_node (rn);
3311 return 0;
3312 }
3313
3314 /* Install into rib. */
3315 static_uninstall_route (AFI_IP, safi, p, si);
3316
3317 /* Unlink static route from linked list. */
3318 if (si->prev)
3319 si->prev->next = si->next;
3320 else
3321 rn->info = si->next;
3322 if (si->next)
3323 si->next->prev = si->prev;
3324 route_unlock_node (rn);
3325
3326 /* Free static route configuration. */
3327 XFREE (MTYPE_STATIC_ROUTE, si);
3328
3329 route_unlock_node (rn);
3330
3331 return 1;
3332 }
3333
3334 int
3335 rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
3336 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
3337 u_int32_t table_id, u_int32_t metric, u_char distance, safi_t safi)
3338 {
3339 struct rib *rib;
3340 struct rib *same = NULL;
3341 struct route_table *table;
3342 struct route_node *rn;
3343 struct nexthop *nexthop;
3344
3345 /* Lookup table. */
3346 table = zebra_vrf_table_with_table_id (AFI_IP6, safi, vrf_id, table_id);
3347 if (! table)
3348 return 0;
3349
3350 /* Make sure mask is applied. */
3351 apply_mask_ipv6 (p);
3352
3353 /* Set default distance by route type. */
3354 if (!distance)
3355 distance = route_info[type].distance;
3356
3357 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
3358 distance = 200;
3359
3360 /* Lookup route node.*/
3361 rn = route_node_get (table, (struct prefix *) p);
3362
3363 /* If same type of route are installed, treat it as a implicit
3364 withdraw. */
3365 RNODE_FOREACH_RIB (rn, rib)
3366 {
3367 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3368 continue;
3369
3370 if (rib->type != type)
3371 continue;
3372 if (rib->instance != instance)
3373 continue;
3374 if (rib->type != ZEBRA_ROUTE_CONNECT)
3375 {
3376 same = rib;
3377 break;
3378 }
3379 else if ((nexthop = rib->nexthop) &&
3380 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
3381 nexthop->ifindex == ifindex)
3382 {
3383 rib->refcnt++;
3384 return 0;
3385 }
3386 }
3387
3388 /* Allocate new rib structure. */
3389 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
3390
3391 rib->type = type;
3392 rib->instance = instance;
3393 rib->distance = distance;
3394 rib->flags = flags;
3395 rib->metric = metric;
3396 rib->table = table_id;
3397 rib->vrf_id = vrf_id;
3398 rib->nexthop_num = 0;
3399 rib->uptime = time (NULL);
3400
3401 /* Nexthop settings. */
3402 if (gate)
3403 {
3404 if (ifindex)
3405 rib_nexthop_ipv6_ifindex_add (rib, gate, ifindex);
3406 else
3407 rib_nexthop_ipv6_add (rib, gate);
3408 }
3409 else
3410 rib_nexthop_ifindex_add (rib, ifindex);
3411
3412 /* If this route is kernel route, set FIB flag to the route. */
3413 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
3414 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
3415 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3416
3417 /* Link new rib to node.*/
3418 if (IS_ZEBRA_DEBUG_RIB)
3419 {
3420 char buf[INET6_ADDRSTRLEN];
3421 if (IS_ZEBRA_DEBUG_RIB)
3422 {
3423 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
3424 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3425 "existing %p",
3426 vrf_id, buf, p->prefixlen, (void *)rn,
3427 (void *)rib, rib->type, (void *)same);
3428 }
3429
3430 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3431 rib_dump ((struct prefix *)p, rib);
3432 }
3433 rib_addnode (rn, rib, 1);
3434
3435 /* Free implicit route.*/
3436 if (same)
3437 rib_delnode (rn, same);
3438
3439 route_unlock_node (rn);
3440 return 0;
3441 }
3442
3443 int
3444 rib_add_ipv6_multipath (struct prefix *p, struct rib *rib, safi_t safi,
3445 unsigned long ifindex)
3446 {
3447 struct route_table *table;
3448 struct route_node *rn;
3449 struct rib *same = NULL;
3450 struct nexthop *nexthop;
3451 int ret = 0;
3452 int family;
3453
3454 if (!rib)
3455 return 0;
3456
3457 if (p->family == AF_INET)
3458 family = AFI_IP;
3459 else
3460 family = AFI_IP6;
3461
3462 /* Lookup table. */
3463 table = zebra_vrf_table_with_table_id (family, safi, rib->vrf_id, rib->table);
3464 if (! table)
3465 return 0;
3466
3467 if (p->family == AF_INET)
3468 {
3469 /* Make it sure prefixlen is applied to the prefix. */
3470 apply_mask_ipv4 ((struct prefix_ipv4 *)p);
3471 }
3472 else
3473 {
3474 /* Make sure mask is applied. */
3475 apply_mask_ipv6 ((struct prefix_ipv6 *)p);
3476 }
3477
3478 /* Set default distance by route type. */
3479 if (rib->distance == 0)
3480 {
3481 rib->distance = route_info[rib->type].distance;
3482
3483 /* iBGP distance is 200. */
3484 if (rib->type == ZEBRA_ROUTE_BGP
3485 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
3486 rib->distance = 200;
3487 }
3488
3489 /* Lookup route node.*/
3490 rn = route_node_get (table, (struct prefix *) p);
3491
3492 /* If same type of route are installed, treat it as a implicit
3493 withdraw. */
3494 RNODE_FOREACH_RIB (rn, same) {
3495 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) {
3496 continue;
3497 }
3498 if (same->type != rib->type) {
3499 continue;
3500 }
3501
3502 if (same->instance != rib->instance) {
3503 continue;
3504 }
3505
3506 if (same->table != rib->table) {
3507 continue;
3508 }
3509 if (same->type != ZEBRA_ROUTE_CONNECT) {
3510 break;
3511 }
3512 else if ((nexthop = same->nexthop) &&
3513 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
3514 nexthop->ifindex == ifindex) {
3515 same->refcnt++;
3516 return 0;
3517 }
3518 }
3519
3520 /* If this route is kernel route, set FIB flag to the route. */
3521 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) {
3522 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) {
3523 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3524 }
3525 }
3526
3527 /* Link new rib to node.*/
3528 if (IS_ZEBRA_DEBUG_RIB)
3529 {
3530 char buf[INET6_ADDRSTRLEN];
3531 if (IS_ZEBRA_DEBUG_RIB)
3532 {
3533 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
3534 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3535 "existing %p",
3536 rib->vrf_id, buf, p->prefixlen, rn, rib, rib->type, same);
3537 }
3538
3539 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3540 rib_dump ((struct prefix *)p, rib);
3541 }
3542 rib_addnode (rn, rib, 1);
3543 ret = 1;
3544
3545 /* Free implicit route.*/
3546 if (same)
3547 {
3548 rib_delnode (rn, same);
3549 ret = -1;
3550 }
3551
3552 route_unlock_node (rn);
3553 return ret;
3554 }
3555
3556 /* XXX factor with rib_delete_ipv6 */
3557
3558 int
3559 rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
3560 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
3561 u_int32_t table_id, safi_t safi)
3562 {
3563 struct route_table *table;
3564 struct route_node *rn;
3565 struct rib *rib;
3566 struct rib *fib = NULL;
3567 struct rib *same = NULL;
3568 struct nexthop *nexthop, *tnexthop;
3569 int recursing;
3570 char buf1[PREFIX_STRLEN];
3571 char buf2[INET6_ADDRSTRLEN];
3572
3573 /* Apply mask. */
3574 apply_mask_ipv6 (p);
3575
3576 /* Lookup table. */
3577 table = zebra_vrf_table_with_table_id (AFI_IP6, safi, vrf_id, table_id);
3578 if (! table)
3579 return 0;
3580
3581 /* Lookup route node. */
3582 rn = route_node_lookup (table, (struct prefix *) p);
3583 if (! rn)
3584 {
3585 if (IS_ZEBRA_DEBUG_RIB)
3586 zlog_debug ("%u:%s: doesn't exist in rib",
3587 vrf_id, prefix2str (p, buf1, sizeof(buf1)));
3588 return ZEBRA_ERR_RTNOEXIST;
3589 }
3590
3591 /* Lookup same type route. */
3592 RNODE_FOREACH_RIB (rn, rib)
3593 {
3594 if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
3595 continue;
3596
3597 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
3598 fib = rib;
3599
3600 if (rib->type != type)
3601 continue;
3602 if (rib->instance != instance)
3603 continue;
3604 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
3605 nexthop->type == NEXTHOP_TYPE_IFINDEX)
3606 {
3607 if (nexthop->ifindex != ifindex)
3608 continue;
3609 if (rib->refcnt)
3610 {
3611 rib->refcnt--;
3612 route_unlock_node (rn);
3613 route_unlock_node (rn);
3614 return 0;
3615 }
3616 same = rib;
3617 break;
3618 }
3619 /* Make sure that the route found has the same gateway. */
3620 else
3621 {
3622 if (gate == NULL)
3623 {
3624 same = rib;
3625 break;
3626 }
3627 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
3628 if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
3629 {
3630 same = rib;
3631 break;
3632 }
3633 if (same)
3634 break;
3635 }
3636 }
3637
3638 /* If same type of route can't be found and this message is from
3639 kernel. */
3640 if (! same)
3641 {
3642 if (fib && type == ZEBRA_ROUTE_KERNEL &&
3643 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
3644 {
3645 if (IS_ZEBRA_DEBUG_RIB)
3646 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
3647 "from kernel, adding",
3648 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
3649 p->prefixlen, rn, fib, fib->type);
3650 if (allow_delete)
3651 {
3652 /* Unset flags. */
3653 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
3654 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3655
3656 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
3657 }
3658 else
3659 {
3660 /* This means someone else, other than Zebra, has deleted a Zebra
3661 * route from the kernel. We will add it back */
3662 rib_install_kernel(rn, fib, 0);
3663 }
3664 }
3665 else
3666 {
3667 if (IS_ZEBRA_DEBUG_KERNEL)
3668 {
3669 if (gate)
3670 zlog_debug ("%s: vrf %u via %s ifindex %d type %d "
3671 "doesn't exist in rib",
3672 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
3673 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
3674 ifindex,
3675 type);
3676 else
3677 zlog_debug ("%s: vrf %u ifindex %d type %d doesn't exist in rib",
3678 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
3679 ifindex,
3680 type);
3681 }
3682 route_unlock_node (rn);
3683 return ZEBRA_ERR_RTNOEXIST;
3684 }
3685 }
3686
3687 if (same)
3688 rib_delnode (rn, same);
3689
3690 route_unlock_node (rn);
3691 return 0;
3692 }
3693
3694 /* Add static route into static route configuration. */
3695 int
3696 static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
3697 unsigned int ifindex, const char *ifname, u_char flags,
3698 u_short tag, u_char distance, struct zebra_vrf *zvrf)
3699 {
3700 struct route_node *rn;
3701 struct static_route *si;
3702 struct static_route *pp;
3703 struct static_route *cp;
3704 struct static_route *update = NULL;
3705 struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST];
3706
3707 if (! stable)
3708 return -1;
3709
3710 if (!gate &&
3711 (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFINDEX))
3712 return -1;
3713
3714 if (!ifindex &&
3715 (type == STATIC_IPV6_GATEWAY_IFINDEX || type == STATIC_IFINDEX))
3716 return -1;
3717
3718 /* Lookup static route prefix. */
3719 rn = route_node_get (stable, p);
3720
3721 /* Do nothing if there is a same static route. */
3722 for (si = rn->info; si; si = si->next)
3723 {
3724 if (type == si->type
3725 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
3726 && (! ifindex || ifindex == si->ifindex))
3727 {
3728 if ((distance == si->distance) && (tag == si->tag))
3729 {
3730 route_unlock_node (rn);
3731 return 0;
3732 }
3733 else
3734 update = si;
3735 }
3736 }
3737
3738 /* Distance or tag changed. */
3739 if (update)
3740 static_delete_ipv6 (p, type, gate, ifindex, update->tag, update->distance, zvrf);
3741
3742 /* Make new static route structure. */
3743 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
3744
3745 si->type = type;
3746 si->distance = distance;
3747 si->flags = flags;
3748 si->tag = tag;
3749 si->vrf_id = zvrf->vrf_id;
3750 si->ifindex = ifindex;
3751 if (si->ifindex)
3752 strcpy (si->ifname, ifname);
3753
3754 switch (type)
3755 {
3756 case STATIC_IPV6_GATEWAY:
3757 si->addr.ipv6 = *gate;
3758 break;
3759 case STATIC_IPV6_GATEWAY_IFINDEX:
3760 si->addr.ipv6 = *gate;
3761 break;
3762 }
3763
3764 /* Add new static route information to the tree with sort by
3765 distance value and gateway address. */
3766 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
3767 {
3768 if (si->distance < cp->distance)
3769 break;
3770 if (si->distance > cp->distance)
3771 continue;
3772 }
3773
3774 /* Make linked list. */
3775 if (pp)
3776 pp->next = si;
3777 else
3778 rn->info = si;
3779 if (cp)
3780 cp->prev = si;
3781 si->prev = pp;
3782 si->next = cp;
3783
3784 /* Install into rib. */
3785 static_install_route (AFI_IP6, SAFI_UNICAST, p, si);
3786
3787 return 1;
3788 }
3789
3790 /* Delete static route from static route configuration. */
3791 int
3792 static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
3793 unsigned int ifindex, u_short tag, u_char distance,
3794 struct zebra_vrf *zvrf)
3795 {
3796 struct route_node *rn;
3797 struct static_route *si;
3798 struct route_table *stable;
3799
3800 /* Lookup table. */
3801 stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, zvrf);
3802 if (! stable)
3803 return -1;
3804
3805 /* Lookup static route prefix. */
3806 rn = route_node_lookup (stable, p);
3807 if (! rn)
3808 return 0;
3809
3810 /* Find same static route is the tree */
3811 for (si = rn->info; si; si = si->next)
3812 if (distance == si->distance
3813 && type == si->type
3814 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
3815 && (! ifindex || ifindex == si->ifindex)
3816 && (! tag || (tag == si->tag)))
3817 break;
3818
3819 /* Can't find static route. */
3820 if (! si)
3821 {
3822 route_unlock_node (rn);
3823 return 0;
3824 }
3825
3826 /* Install into rib. */
3827 static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si);
3828
3829 /* Unlink static route from linked list. */
3830 if (si->prev)
3831 si->prev->next = si->next;
3832 else
3833 rn->info = si->next;
3834 if (si->next)
3835 si->next->prev = si->prev;
3836
3837 /* Free static route configuration. */
3838 XFREE (MTYPE_STATIC_ROUTE, si);
3839
3840 return 1;
3841 }
3842
3843 /* Schedule routes of a particular table (address-family) based on event. */
3844 static void
3845 rib_update_table (struct route_table *table, rib_update_event_t event)
3846 {
3847 struct route_node *rn;
3848 struct rib *rib, *next;
3849
3850 /* Walk all routes and queue for processing, if appropriate for
3851 * the trigger event.
3852 */
3853 for (rn = route_top (table); rn; rn = route_next (rn))
3854 {
3855 switch (event)
3856 {
3857 case RIB_UPDATE_IF_CHANGE:
3858 /* Examine all routes that won't get processed by the protocol or
3859 * triggered by nexthop evaluation (NHT). This would be system,
3860 * kernel and certain static routes. Note that NHT will get
3861 * triggered upon an interface event as connected routes always
3862 * get queued for processing.
3863 */
3864 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3865 {
3866 if (rib->type == ZEBRA_ROUTE_OSPF ||
3867 rib->type == ZEBRA_ROUTE_OSPF6 ||
3868 rib->type == ZEBRA_ROUTE_BGP)
3869 continue; /* protocol will handle. */
3870 else if (rib->type == ZEBRA_ROUTE_STATIC)
3871 {
3872 struct nexthop *nh;
3873 for (nh = rib->nexthop; nh; nh = nh->next)
3874 if (!(nh->type == NEXTHOP_TYPE_IPV4 ||
3875 nh->type == NEXTHOP_TYPE_IPV6))
3876 break;
3877
3878 /* If we only have nexthops to a gateway, NHT will
3879 * take care.
3880 */
3881 if (nh)
3882 rib_queue_add (rn);
3883 }
3884 else
3885 rib_queue_add (rn);
3886 }
3887 break;
3888
3889 case RIB_UPDATE_RMAP_CHANGE:
3890 case RIB_UPDATE_OTHER:
3891 /* Right now, examine all routes. Can restrict to a protocol in
3892 * some cases (TODO).
3893 */
3894 if (rnode_to_ribs (rn))
3895 rib_queue_add (rn);
3896 break;
3897
3898 default:
3899 break;
3900 }
3901 }
3902 }
3903
3904 /* RIB update function. */
3905 void
3906 rib_update (vrf_id_t vrf_id, rib_update_event_t event)
3907 {
3908 struct route_table *table;
3909
3910 /* Process routes of interested address-families. */
3911 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
3912 if (table)
3913 rib_update_table (table, event);
3914
3915 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
3916 if (table)
3917 rib_update_table (table, event);
3918 }
3919
3920 /* Remove all routes which comes from non main table. */
3921 static void
3922 rib_weed_table (struct route_table *table)
3923 {
3924 struct route_node *rn;
3925 struct rib *rib;
3926 struct rib *next;
3927
3928 if (table)
3929 for (rn = route_top (table); rn; rn = route_next (rn))
3930 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3931 {
3932 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3933 continue;
3934
3935 if (rib->table != zebrad.rtm_table_default &&
3936 rib->table != RT_TABLE_MAIN)
3937 rib_delnode (rn, rib);
3938 }
3939 }
3940
3941 /* Delete all routes from non main table. */
3942 void
3943 rib_weed_tables (void)
3944 {
3945 vrf_iter_t iter;
3946 struct zebra_vrf *zvrf;
3947
3948 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3949 if ((zvrf = vrf_iter2info (iter)) != NULL)
3950 {
3951 rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3952 rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3953 }
3954 }
3955
3956 /* Delete self installed routes after zebra is relaunched. */
3957 static void
3958 rib_sweep_table (struct route_table *table)
3959 {
3960 struct route_node *rn;
3961 struct rib *rib;
3962 struct rib *next;
3963 int ret = 0;
3964
3965 if (table)
3966 for (rn = route_top (table); rn; rn = route_next (rn))
3967 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3968 {
3969 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3970 continue;
3971
3972 if (rib->type == ZEBRA_ROUTE_KERNEL &&
3973 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
3974 {
3975 ret = rib_uninstall_kernel (rn, rib);
3976 if (! ret)
3977 rib_delnode (rn, rib);
3978 }
3979 }
3980 }
3981
3982 /* Sweep all RIB tables. */
3983 void
3984 rib_sweep_route (void)
3985 {
3986 vrf_iter_t iter;
3987 struct zebra_vrf *zvrf;
3988
3989 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3990 if ((zvrf = vrf_iter2info (iter)) != NULL)
3991 {
3992 rib_sweep_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3993 rib_sweep_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3994 }
3995 }
3996
3997 /* Remove specific by protocol routes from 'table'. */
3998 static unsigned long
3999 rib_score_proto_table (u_char proto, u_short instance, struct route_table *table)
4000 {
4001 struct route_node *rn;
4002 struct rib *rib;
4003 struct rib *next;
4004 unsigned long n = 0;
4005
4006 if (table)
4007 for (rn = route_top (table); rn; rn = route_next (rn))
4008 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
4009 {
4010 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
4011 continue;
4012 if (rib->type == proto && rib->instance == instance)
4013 {
4014 rib_delnode (rn, rib);
4015 n++;
4016 }
4017 }
4018
4019 return n;
4020 }
4021
4022 /* Remove specific by protocol routes. */
4023 unsigned long
4024 rib_score_proto (u_char proto, u_short instance)
4025 {
4026 vrf_iter_t iter;
4027 struct zebra_vrf *zvrf;
4028 unsigned long cnt = 0;
4029
4030 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
4031 if ((zvrf = vrf_iter2info (iter)) != NULL)
4032 cnt += rib_score_proto_table (proto, instance, zvrf->table[AFI_IP][SAFI_UNICAST])
4033 +rib_score_proto_table (proto, instance, zvrf->table[AFI_IP6][SAFI_UNICAST]);
4034
4035 return cnt;
4036 }
4037
4038 /* Close RIB and clean up kernel routes. */
4039 void
4040 rib_close_table (struct route_table *table)
4041 {
4042 struct route_node *rn;
4043 rib_table_info_t *info = table->info;
4044 struct rib *rib;
4045
4046 if (table)
4047 for (rn = route_top (table); rn; rn = route_next (rn))
4048 RNODE_FOREACH_RIB (rn, rib)
4049 {
4050 if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
4051 continue;
4052
4053 if (info->safi == SAFI_UNICAST)
4054 zfpm_trigger_update (rn, NULL);
4055
4056 if (! RIB_SYSTEM_ROUTE (rib))
4057 rib_uninstall_kernel (rn, rib);
4058 }
4059 }
4060
4061 /* Close all RIB tables. */
4062 void
4063 rib_close (void)
4064 {
4065 vrf_iter_t iter;
4066 struct zebra_vrf *zvrf;
4067 struct listnode *node;
4068 struct interface *ifp;
4069 u_int32_t table_id;
4070
4071 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
4072 {
4073 if ((zvrf = vrf_iter2info (iter)) != NULL)
4074 {
4075 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
4076 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
4077 }
4078 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
4079 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
4080 }
4081
4082 /* If we do multiple tables per vrf, need to move this to loop above */
4083 zvrf = vrf_info_lookup (VRF_DEFAULT);
4084
4085 for (table_id = 0; table_id < ZEBRA_KERNEL_TABLE_MAX; table_id++)
4086 {
4087 if (zvrf->other_table[AFI_IP][table_id])
4088 rib_close_table (zvrf->other_table[AFI_IP][table_id]);
4089
4090 if (zvrf->other_table[AFI_IP6][table_id])
4091 rib_close_table (zvrf->other_table[AFI_IP6][table_id]);
4092 }
4093 }
4094
4095 /* Routing information base initialize. */
4096 void
4097 rib_init (void)
4098 {
4099 rib_queue_init (&zebrad);
4100 }
4101
4102 /*
4103 * vrf_id_get_next
4104 *
4105 * Get the first vrf id that is greater than the given vrf id if any.
4106 *
4107 * Returns TRUE if a vrf id was found, FALSE otherwise.
4108 */
4109 static inline int
4110 vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p)
4111 {
4112 vrf_iter_t iter = vrf_iterator (vrf_id);
4113 struct zebra_vrf *zvrf = vrf_iter2info (iter);
4114
4115 /* The same one ? Then find out the next. */
4116 if (zvrf && (zvrf->vrf_id == vrf_id))
4117 zvrf = vrf_iter2info (vrf_next (iter));
4118
4119 if (zvrf)
4120 {
4121 *next_id_p = zvrf->vrf_id;
4122 return 1;
4123 }
4124
4125 return 0;
4126 }
4127
4128 /*
4129 * rib_tables_iter_next
4130 *
4131 * Returns the next table in the iteration.
4132 */
4133 struct route_table *
4134 rib_tables_iter_next (rib_tables_iter_t *iter)
4135 {
4136 struct route_table *table;
4137
4138 /*
4139 * Array that helps us go over all AFI/SAFI combinations via one
4140 * index.
4141 */
4142 static struct {
4143 afi_t afi;
4144 safi_t safi;
4145 } afi_safis[] = {
4146 { AFI_IP, SAFI_UNICAST },
4147 { AFI_IP, SAFI_MULTICAST },
4148 { AFI_IP6, SAFI_UNICAST },
4149 { AFI_IP6, SAFI_MULTICAST },
4150 };
4151
4152 table = NULL;
4153
4154 switch (iter->state)
4155 {
4156
4157 case RIB_TABLES_ITER_S_INIT:
4158 iter->vrf_id = VRF_DEFAULT;
4159 iter->afi_safi_ix = -1;
4160
4161 /* Fall through */
4162
4163 case RIB_TABLES_ITER_S_ITERATING:
4164 iter->afi_safi_ix++;
4165 while (1)
4166 {
4167
4168 while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
4169 {
4170 table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi,
4171 afi_safis[iter->afi_safi_ix].safi,
4172 iter->vrf_id);
4173 if (table)
4174 break;
4175
4176 iter->afi_safi_ix++;
4177 }
4178
4179 /*
4180 * Found another table in this vrf.
4181 */
4182 if (table)
4183 break;
4184
4185 /*
4186 * Done with all tables in the current vrf, go to the next
4187 * one.
4188 */
4189 if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
4190 break;
4191
4192 iter->afi_safi_ix = 0;
4193 }
4194
4195 break;
4196
4197 case RIB_TABLES_ITER_S_DONE:
4198 return NULL;
4199 }
4200
4201 if (table)
4202 iter->state = RIB_TABLES_ITER_S_ITERATING;
4203 else
4204 iter->state = RIB_TABLES_ITER_S_DONE;
4205
4206 return table;
4207 }
4208