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