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