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