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