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