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