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