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