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