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