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