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