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