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