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