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