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