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