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