]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
Quagga: Implement VRF change semantics for an interface
[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);
a399694f
DS
311 zebra_deregister_rnh_static_nexthops(nexthop->resolved, top);
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);
a399694f
DS
535 zebra_deregister_rnh_static_nexthops (nexthop->resolved, top);
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 */
a399694f
DS
1967 zebra_deregister_rnh_static_nexthops (rib->nexthop, rn);
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
2005int
7c8ff89e 2006rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
7514fb77 2007 struct in_addr *gate, struct in_addr *src,
78104b9b 2008 unsigned int ifindex, vrf_id_t vrf_id, u_int32_t table_id,
cddf391b 2009 u_int32_t metric, u_char distance, safi_t safi)
718e3744 2010{
2011 struct rib *rib;
2012 struct rib *same = NULL;
2013 struct route_table *table;
2014 struct route_node *rn;
2015 struct nexthop *nexthop;
2016
2017 /* Lookup table. */
7a4bb9c5
DS
2018 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
2019 {
78104b9b 2020 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
7a4bb9c5
DS
2021 }
2022 else
2023 {
78104b9b 2024 table = zebra_vrf_other_route_table (AFI_IP, table_id, vrf_id);
7a4bb9c5 2025 }
718e3744 2026 if (! table)
2027 return 0;
2028
2029 /* Make it sure prefixlen is applied to the prefix. */
2030 apply_mask_ipv4 (p);
2031
2032 /* Set default distance by route type. */
2033 if (distance == 0)
2034 {
837d16cc 2035 if ((unsigned)type >= array_size(route_info))
7052f228
DL
2036 distance = 150;
2037 else
2038 distance = route_info[type].distance;
718e3744 2039
2040 /* iBGP distance is 200. */
2041 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2042 distance = 200;
2043 }
2044
2045 /* Lookup route node.*/
2046 rn = route_node_get (table, (struct prefix *) p);
2047
2048 /* If same type of route are installed, treat it as a implicit
2049 withdraw. */
9fd92e3c 2050 RNODE_FOREACH_RIB (rn, rib)
718e3744 2051 {
6d691129
PJ
2052 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2053 continue;
2054
ebf1ead0 2055 if (rib->type != type)
2056 continue;
7c8ff89e
DS
2057 if (rib->instance != instance)
2058 continue;
7a4bb9c5 2059
ebf1ead0 2060 if (rib->type != ZEBRA_ROUTE_CONNECT)
4d38fdb4 2061 {
2062 same = rib;
2063 break;
2064 }
ebf1ead0 2065 /* Duplicate connected route comes in. */
2066 else if ((nexthop = rib->nexthop) &&
2067 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
6d691129
PJ
2068 nexthop->ifindex == ifindex &&
2069 !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
ebf1ead0 2070 {
2071 rib->refcnt++;
2072 return 0 ;
2073 }
718e3744 2074 }
2075
2076 /* Allocate new rib structure. */
4d38fdb4 2077 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
718e3744 2078 rib->type = type;
7c8ff89e 2079 rib->instance = instance;
718e3744 2080 rib->distance = distance;
2081 rib->flags = flags;
2082 rib->metric = metric;
7a4bb9c5 2083 rib->table = table_id;
78104b9b 2084 rib->vrf_id = vrf_id;
718e3744 2085 rib->nexthop_num = 0;
2086 rib->uptime = time (NULL);
2087
2088 /* Nexthop settings. */
2089 if (gate)
2090 {
2091 if (ifindex)
a399694f 2092 rib_nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
718e3744 2093 else
a399694f 2094 rib_nexthop_ipv4_add (rib, gate, src);
718e3744 2095 }
2096 else
a399694f 2097 rib_nexthop_ifindex_add (rib, ifindex);
718e3744 2098
2099 /* If this route is kernel route, set FIB flag to the route. */
2100 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
2101 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2102 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2103
2104 /* Link new rib to node.*/
dc95824a 2105 if (IS_ZEBRA_DEBUG_RIB)
41ec9222 2106 {
2107 char buf[INET6_ADDRSTRLEN];
2108 if (IS_ZEBRA_DEBUG_RIB)
2109 {
2110 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
2111 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2112 "existing %p",
2113 vrf_id, buf, p->prefixlen, rn, rib, rib->type, same);
2114 }
2115
2116 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2117 rib_dump ((struct prefix *)p, rib);
2118 }
2bf26d41 2119 rib_addnode (rn, rib, 1);
4d38fdb4 2120
718e3744 2121 /* Free implicit route.*/
2122 if (same)
4d38fdb4 2123 rib_delnode (rn, same);
2124
2125 route_unlock_node (rn);
718e3744 2126 return 0;
2127}
2128
dc95824a
DO
2129/* This function dumps the contents of a given RIB entry into
2130 * standard debug log. Calling function name and IP prefix in
2131 * question are passed as 1st and 2nd arguments.
2132 */
2133
f7bf4153
DL
2134void _rib_dump (const char * func,
2135 union prefix46constptr pp, const struct rib * rib)
dc95824a 2136{
f7bf4153 2137 const struct prefix *p = pp.p;
4690c7d7 2138 char straddr[PREFIX2STR_BUFFER];
fa713d9e
CF
2139 struct nexthop *nexthop, *tnexthop;
2140 int recursing;
dc95824a 2141
78104b9b
FL
2142 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func, rib,
2143 prefix2str(pp, straddr, sizeof(straddr)), rib->vrf_id);
dc95824a
DO
2144 zlog_debug
2145 (
7c8ff89e 2146 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
dc95824a
DO
2147 func,
2148 rib->refcnt,
d02c56cd 2149 (unsigned long) rib->uptime,
dc95824a 2150 rib->type,
7c8ff89e 2151 rib->instance,
dc95824a
DO
2152 rib->table
2153 );
2154 zlog_debug
2155 (
2156 "%s: metric == %u, distance == %u, flags == %u, status == %u",
2157 func,
2158 rib->metric,
2159 rib->distance,
2160 rib->flags,
2161 rib->status
2162 );
2163 zlog_debug
2164 (
2165 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2166 func,
2167 rib->nexthop_num,
2168 rib->nexthop_active_num,
2169 rib->nexthop_fib_num
2170 );
fed643f4 2171
fa713d9e
CF
2172 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2173 {
fed643f4 2174 inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
fa713d9e
CF
2175 zlog_debug
2176 (
2177 "%s: %s %s with flags %s%s%s",
2178 func,
2179 (recursing ? " NH" : "NH"),
2180 straddr,
2181 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
2182 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
2183 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
2184 );
2185 }
dc95824a
DO
2186 zlog_debug ("%s: dump complete", func);
2187}
2188
2189/* This is an exported helper to rtm_read() to dump the strange
2190 * RIB entry found by rib_lookup_ipv4_route()
2191 */
2192
12f6fb97 2193void rib_lookup_and_dump (struct prefix_ipv4 * p, vrf_id_t vrf_id)
dc95824a
DO
2194{
2195 struct route_table *table;
2196 struct route_node *rn;
2197 struct rib *rib;
2198 char prefix_buf[INET_ADDRSTRLEN];
2199
2200 /* Lookup table. */
12f6fb97 2201 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
dc95824a
DO
2202 if (! table)
2203 {
b72ede27 2204 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
dc95824a
DO
2205 return;
2206 }
2207
2208 inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN);
2209 /* Scan the RIB table for exactly matching RIB entry. */
2210 rn = route_node_lookup (table, (struct prefix *) p);
2211
2212 /* No route for this prefix. */
2213 if (! rn)
2214 {
2215 zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen);
2216 return;
2217 }
2218
2219 /* Unlock node. */
2220 route_unlock_node (rn);
2221
2222 /* let's go */
9fd92e3c 2223 RNODE_FOREACH_RIB (rn, rib)
dc95824a
DO
2224 {
2225 zlog_debug
2226 (
2227 "%s: rn %p, rib %p: %s, %s",
2228 __func__,
2229 rn,
2230 rib,
2231 (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
2232 (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
2233 );
f7bf4153 2234 rib_dump (p, rib);
dc95824a
DO
2235 }
2236}
2237
20e5ff0a
DO
2238/* Check if requested address assignment will fail due to another
2239 * route being installed by zebra in FIB already. Take necessary
2240 * actions, if needed: remove such a route from FIB and deSELECT
2241 * corresponding RIB entry. Then put affected RN into RIBQ head.
2242 */
12f6fb97 2243void rib_lookup_and_pushup (struct prefix_ipv4 * p, vrf_id_t vrf_id)
20e5ff0a
DO
2244{
2245 struct route_table *table;
2246 struct route_node *rn;
2247 struct rib *rib;
2248 unsigned changed = 0;
2249
12f6fb97 2250 if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id)))
20e5ff0a 2251 {
b72ede27 2252 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
20e5ff0a
DO
2253 return;
2254 }
2255
2256 /* No matches would be the simplest case. */
2257 if (NULL == (rn = route_node_lookup (table, (struct prefix *) p)))
2258 return;
2259
2260 /* Unlock node. */
2261 route_unlock_node (rn);
2262
2263 /* Check all RIB entries. In case any changes have to be done, requeue
2264 * the RN into RIBQ head. If the routing message about the new connected
2265 * route (generated by the IP address we are going to assign very soon)
2266 * comes before the RIBQ is processed, the new RIB entry will join
2267 * RIBQ record already on head. This is necessary for proper revalidation
2268 * of the rest of the RIB.
2269 */
9fd92e3c 2270 RNODE_FOREACH_RIB (rn, rib)
20e5ff0a
DO
2271 {
2272 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) &&
2273 ! RIB_SYSTEM_ROUTE (rib))
2274 {
2275 changed = 1;
2276 if (IS_ZEBRA_DEBUG_RIB)
2277 {
2278 char buf[INET_ADDRSTRLEN];
2279 inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN);
41ec9222 2280 zlog_debug ("%u:%s/%d: freeing way for connected prefix",
2281 rib->vrf_id, buf, p->prefixlen);
f7bf4153 2282 rib_dump (&rn->p, rib);
20e5ff0a
DO
2283 }
2284 rib_uninstall (rn, rib);
2285 }
2286 }
2287 if (changed)
20e5ff0a 2288 rib_queue_add (&zebrad, rn);
20e5ff0a
DO
2289}
2290
718e3744 2291int
cddf391b 2292rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
718e3744 2293{
2294 struct route_table *table;
2295 struct route_node *rn;
2296 struct rib *same;
2297 struct nexthop *nexthop;
04b02fda 2298 int ret = 0;
4d38fdb4 2299
718e3744 2300 /* Lookup table. */
7a4bb9c5
DS
2301 if ((rib->table == zebrad.rtm_table_default) || (rib->table == RT_TABLE_MAIN))
2302 {
78104b9b 2303 table = zebra_vrf_table (AFI_IP, safi, rib->vrf_id);
7a4bb9c5
DS
2304 }
2305 else
2306 {
78104b9b 2307 table = zebra_vrf_other_route_table (AFI_IP, rib->table, rib->vrf_id);
7a4bb9c5 2308 }
718e3744 2309 if (! table)
2310 return 0;
cddf391b 2311
718e3744 2312 /* Make it sure prefixlen is applied to the prefix. */
2313 apply_mask_ipv4 (p);
2314
2315 /* Set default distance by route type. */
2316 if (rib->distance == 0)
2317 {
2318 rib->distance = route_info[rib->type].distance;
2319
2320 /* iBGP distance is 200. */
2321 if (rib->type == ZEBRA_ROUTE_BGP
2322 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
2323 rib->distance = 200;
2324 }
2325
2326 /* Lookup route node.*/
2327 rn = route_node_get (table, (struct prefix *) p);
2328
2329 /* If same type of route are installed, treat it as a implicit
2330 withdraw. */
9fd92e3c 2331 RNODE_FOREACH_RIB (rn, same)
718e3744 2332 {
0b8c4f1d 2333 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
6d691129
PJ
2334 continue;
2335
7c8ff89e
DS
2336 if (same->type == rib->type && same->instance == rib->instance
2337 && same->table == rib->table
718e3744 2338 && same->type != ZEBRA_ROUTE_CONNECT)
4d38fdb4 2339 break;
718e3744 2340 }
4d38fdb4 2341
718e3744 2342 /* If this route is kernel route, set FIB flag to the route. */
2343 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
2344 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2345 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2346
2347 /* Link new rib to node.*/
41ec9222 2348 if (IS_ZEBRA_DEBUG_RIB)
2349 {
2350 char buf[INET6_ADDRSTRLEN];
2351 if (IS_ZEBRA_DEBUG_RIB)
2352 {
2353 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
2354 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2355 "existing %p",
2356 rib->vrf_id, buf, p->prefixlen, rn, rib, rib->type, same);
2357 }
2358
2359 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2360 rib_dump ((struct prefix *)p, rib);
2361 }
2bf26d41 2362 rib_addnode (rn, rib, 1);
04b02fda 2363 ret = 1;
718e3744 2364
718e3744 2365 /* Free implicit route.*/
2366 if (same)
dc95824a 2367 {
41ec9222 2368 rib_delnode (rn, same);
2369 ret = -1;
dc95824a 2370 }
4d38fdb4 2371
2372 route_unlock_node (rn);
04b02fda 2373 return ret;
718e3744 2374}
2375
ebf1ead0 2376/* XXX factor with rib_delete_ipv6 */
718e3744 2377int
7c8ff89e 2378rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
78104b9b 2379 struct in_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
7a4bb9c5 2380 u_int32_t table_id, safi_t safi)
718e3744 2381{
2382 struct route_table *table;
2383 struct route_node *rn;
2384 struct rib *rib;
2385 struct rib *fib = NULL;
2386 struct rib *same = NULL;
fa713d9e
CF
2387 struct nexthop *nexthop, *tnexthop;
2388 int recursing;
4690c7d7 2389 char buf1[PREFIX2STR_BUFFER];
41ec9222 2390 char buf2[INET6_ADDRSTRLEN];
718e3744 2391
2392 /* Lookup table. */
7a4bb9c5
DS
2393 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
2394 {
78104b9b 2395 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
7a4bb9c5
DS
2396 }
2397 else
2398 {
78104b9b 2399 table = zebra_vrf_other_route_table(AFI_IP, table_id, vrf_id);
7a4bb9c5 2400 }
718e3744 2401 if (! table)
2402 return 0;
2403
2404 /* Apply mask. */
2405 apply_mask_ipv4 (p);
2406
2407 /* Lookup route node. */
2408 rn = route_node_lookup (table, (struct prefix *) p);
2409 if (! rn)
2410 {
41ec9222 2411 if (IS_ZEBRA_DEBUG_RIB)
2412 zlog_debug ("%u:%s/%d: doesn't exist in rib",
2413 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
2414 p->prefixlen);
718e3744 2415 return ZEBRA_ERR_RTNOEXIST;
2416 }
2417
2418 /* Lookup same type route. */
9fd92e3c 2419 RNODE_FOREACH_RIB (rn, rib)
718e3744 2420 {
6d691129
PJ
2421 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2422 continue;
2423
718e3744 2424 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
2425 fib = rib;
2426
ebf1ead0 2427 if (rib->type != type)
2428 continue;
7c8ff89e
DS
2429 if (rib->instance != instance)
2430 continue;
ebf1ead0 2431 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
4f1735fd 2432 nexthop->type == NEXTHOP_TYPE_IFINDEX)
718e3744 2433 {
4f1735fd
MF
2434 if (nexthop->ifindex != ifindex)
2435 continue;
ebf1ead0 2436 if (rib->refcnt)
718e3744 2437 {
ebf1ead0 2438 rib->refcnt--;
2439 route_unlock_node (rn);
2440 route_unlock_node (rn);
2441 return 0;
718e3744 2442 }
ebf1ead0 2443 same = rib;
2444 break;
718e3744 2445 }
ebf1ead0 2446 /* Make sure that the route found has the same gateway. */
fa713d9e 2447 else
5ec90d28 2448 {
fa713d9e
CF
2449 if (gate == NULL)
2450 {
2451 same = rib;
2452 break;
2453 }
2454 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2455 if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate))
2456 {
2457 same = rib;
2458 break;
2459 }
2460 if (same)
2461 break;
2462 }
718e3744 2463 }
718e3744 2464 /* If same type of route can't be found and this message is from
2465 kernel. */
2466 if (! same)
2467 {
2037f143
DS
2468 if (fib && type == ZEBRA_ROUTE_KERNEL &&
2469 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
2470 {
41ec9222 2471 if (IS_ZEBRA_DEBUG_RIB)
2037f143 2472 {
41ec9222 2473 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
2474 "from kernel, adding",
2475 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
2476 p->prefixlen, rn, fib, fib->type);
2037f143 2477 }
6baf7bb8
DS
2478 if (allow_delete)
2479 {
2480 /* Unset flags. */
2481 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
2482 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2483
2484 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
2485 }
2486 else
2487 {
2488 /* This means someone else, other than Zebra, has deleted
2489 * a Zebra router from the kernel. We will add it back */
2490 rib_install_kernel(rn, fib, 0);
2491 }
2037f143 2492 }
718e3744 2493 else
2494 {
41ec9222 2495 if (IS_ZEBRA_DEBUG_RIB)
718e3744 2496 {
2497 if (gate)
41ec9222 2498 zlog_debug ("%u:%s: via %s ifindex %d type %d "
78104b9b 2499 "doesn't exist in rib",
41ec9222 2500 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
81cce018 2501 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
718e3744 2502 ifindex,
2503 type);
2504 else
41ec9222 2505 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2506 vrf_id, prefix2str (p, buf1, sizeof(buf1)),
718e3744 2507 ifindex,
2508 type);
2509 }
2510 route_unlock_node (rn);
2511 return ZEBRA_ERR_RTNOEXIST;
2512 }
2513 }
4d38fdb4 2514
718e3744 2515 if (same)
4d38fdb4 2516 rib_delnode (rn, same);
2517
718e3744 2518 route_unlock_node (rn);
718e3744 2519 return 0;
2520}
6b0655a2 2521
718e3744 2522/* Install static route into rib. */
a1ac18c4 2523static void
bcd548ff 2524static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
718e3744 2525{
2526 struct rib *rib;
2527 struct route_node *rn;
2528 struct route_table *table;
6e26278c 2529 struct prefix nh_p;
718e3744 2530
2531 /* Lookup table. */
be5e48ab 2532 table = zebra_vrf_table (afi, safi, si->vrf_id);
718e3744 2533 if (! table)
2534 return;
2535
2536 /* Lookup existing route */
2537 rn = route_node_get (table, p);
9fd92e3c 2538 RNODE_FOREACH_RIB (rn, rib)
6d691129
PJ
2539 {
2540 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2541 continue;
2542
2543 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2544 break;
2545 }
718e3744 2546
2547 if (rib)
2548 {
0d9551dc
DS
2549 /* if tag value changed , update old value in RIB */
2550 if (rib->tag != si->tag)
2551 rib->tag = si->tag;
2552
718e3744 2553 /* Same distance static route is there. Update it with new
2554 nexthop. */
718e3744 2555 route_unlock_node (rn);
718e3744 2556 switch (si->type)
7021c425 2557 {
bcd548ff 2558 case STATIC_IPV4_GATEWAY:
a399694f 2559 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
bcd548ff
DS
2560 nh_p.family = AF_INET;
2561 nh_p.prefixlen = IPV4_MAX_BITLEN;
2562 nh_p.u.prefix4 = si->addr.ipv4;
2563 zebra_register_rnh_static_nh(&nh_p, rn);
2564 break;
ba779241
DS
2565 case STATIC_IFINDEX:
2566 rib_nexthop_ifindex_add (rib, si->ifindex);
bcd548ff
DS
2567 break;
2568 case STATIC_IPV4_BLACKHOLE:
a399694f 2569 rib_nexthop_blackhole_add (rib);
bcd548ff
DS
2570 break;
2571 case STATIC_IPV6_GATEWAY:
a399694f 2572 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
bcd548ff
DS
2573 nh_p.family = AF_INET6;
2574 nh_p.prefixlen = IPV6_MAX_BITLEN;
2575 nh_p.u.prefix6 = si->addr.ipv6;
2576 zebra_register_rnh_static_nh(&nh_p, rn);
2577 break;
ba779241
DS
2578 case STATIC_IPV6_GATEWAY_IFINDEX:
2579 rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
bcd548ff 2580 break;
4d38fdb4 2581 }
41ec9222 2582
2583 if (IS_ZEBRA_DEBUG_RIB)
2584 {
2585 char buf[INET6_ADDRSTRLEN];
2586 if (IS_ZEBRA_DEBUG_RIB)
2587 {
2588 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2589 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
2590 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
2591 }
2592 }
2bf26d41 2593 /* Schedule route for processing or invoke NHT, as appropriate. */
2594 if (si->type == STATIC_IPV4_GATEWAY ||
2595 si->type == STATIC_IPV6_GATEWAY)
2596 zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p);
2597 else
2598 rib_queue_add (&zebrad, rn);
718e3744 2599 }
2600 else
2601 {
2602 /* This is new static route. */
4d38fdb4 2603 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2604
718e3744 2605 rib->type = ZEBRA_ROUTE_STATIC;
7c8ff89e 2606 rib->instance = 0;
718e3744 2607 rib->distance = si->distance;
2608 rib->metric = 0;
8f527c5e 2609 rib->vrf_id = si->vrf_id;
12f6fb97 2610 rib->table = si->vrf_id ? (zebra_vrf_lookup(si->vrf_id))->table_id : zebrad.rtm_table_default;
718e3744 2611 rib->nexthop_num = 0;
0d9551dc 2612 rib->tag = si->tag;
718e3744 2613
2614 switch (si->type)
7021c425 2615 {
bcd548ff 2616 case STATIC_IPV4_GATEWAY:
a399694f 2617 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
bcd548ff
DS
2618 nh_p.family = AF_INET;
2619 nh_p.prefixlen = IPV4_MAX_BITLEN;
2620 nh_p.u.prefix4 = si->addr.ipv4;
2621 zebra_register_rnh_static_nh(&nh_p, rn);
2622 break;
ba779241
DS
2623 case STATIC_IFINDEX:
2624 rib_nexthop_ifindex_add (rib, si->ifindex);
bcd548ff
DS
2625 break;
2626 case STATIC_IPV4_BLACKHOLE:
a399694f 2627 rib_nexthop_blackhole_add (rib);
bcd548ff
DS
2628 break;
2629 case STATIC_IPV6_GATEWAY:
a399694f 2630 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
bcd548ff
DS
2631 nh_p.family = AF_INET6;
2632 nh_p.prefixlen = IPV6_MAX_BITLEN;
2633 nh_p.u.prefix6 = si->addr.ipv6;
2634 zebra_register_rnh_static_nh(&nh_p, rn);
2635 break;
ba779241
DS
2636 case STATIC_IPV6_GATEWAY_IFINDEX:
2637 rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
bcd548ff 2638 break;
7021c425 2639 }
718e3744 2640
81dfcaa2 2641 /* Save the flags of this static routes (reject, blackhole) */
2642 rib->flags = si->flags;
2643
41ec9222 2644 if (IS_ZEBRA_DEBUG_RIB)
2645 {
2646 char buf[INET6_ADDRSTRLEN];
2647 if (IS_ZEBRA_DEBUG_RIB)
2648 {
2649 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2650 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d)",
2651 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
2652 }
2653 }
2bf26d41 2654 /* Link this rib to the tree. Schedule for processing or invoke NHT,
2655 * as appropriate.
2656 */
2657 if (si->type == STATIC_IPV4_GATEWAY ||
2658 si->type == STATIC_IPV6_GATEWAY)
2659 {
2660 rib_addnode (rn, rib, 0);
2661 zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p);
2662 }
2663 else
2664 rib_addnode (rn, rib, 1);
718e3744 2665 }
2666}
2667
a1ac18c4 2668static int
f2b49ed0 2669static_nexthop_same (struct nexthop *nexthop, struct static_route *si)
718e3744 2670{
2671 if (nexthop->type == NEXTHOP_TYPE_IPV4
2672 && si->type == STATIC_IPV4_GATEWAY
c0551cbb 2673 && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4))
718e3744 2674 return 1;
ba779241
DS
2675 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
2676 && si->type == STATIC_IFINDEX
2677 && nexthop->ifindex == si->ifindex)
718e3744 2678 return 1;
595db7f1 2679 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
2680 && si->type == STATIC_IPV4_BLACKHOLE)
2681 return 1;
f2b49ed0
DS
2682 if (nexthop->type == NEXTHOP_TYPE_IPV6
2683 && si->type == STATIC_IPV6_GATEWAY
2684 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6))
2685 return 1;
ba779241
DS
2686 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
2687 && si->type == STATIC_IPV6_GATEWAY_IFINDEX
f2b49ed0 2688 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)
ba779241 2689 && nexthop->ifindex == si->ifindex)
f2b49ed0 2690 return 1;
e8e1946e 2691 return 0;
718e3744 2692}
2693
2694/* Uninstall static route from RIB. */
a1ac18c4 2695static void
b8a1effa 2696static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
718e3744 2697{
2698 struct route_node *rn;
2699 struct rib *rib;
2700 struct nexthop *nexthop;
2701 struct route_table *table;
6e26278c 2702 struct prefix nh_p;
718e3744 2703
2704 /* Lookup table. */
be5e48ab 2705 table = zebra_vrf_table (afi, safi, si->vrf_id);
718e3744 2706 if (! table)
2707 return;
4d38fdb4 2708
718e3744 2709 /* Lookup existing route with type and distance. */
2710 rn = route_node_lookup (table, p);
2711 if (! rn)
2712 return;
2713
9fd92e3c 2714 RNODE_FOREACH_RIB (rn, rib)
6d691129
PJ
2715 {
2716 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2717 continue;
2718
0d9551dc
DS
2719 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance &&
2720 rib->tag == si->tag)
6d691129
PJ
2721 break;
2722 }
718e3744 2723
2724 if (! rib)
2725 {
2726 route_unlock_node (rn);
2727 return;
2728 }
2729
2730 /* Lookup nexthop. */
2731 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
f2b49ed0 2732 if (static_nexthop_same (nexthop, si))
718e3744 2733 break;
2734
2735 /* Can't find nexthop. */
2736 if (! nexthop)
2737 {
2738 route_unlock_node (rn);
2739 return;
2740 }
2741
2742 /* Check nexthop. */
2743 if (rib->nexthop_num == 1)
6d691129 2744 rib_delnode (rn, rib);
718e3744 2745 else
2746 {
94ad353d
DS
2747 /* Mark this nexthop as inactive and reinstall the route. Then, delete
2748 * the nexthop. There is no need to re-evaluate the route for this
2749 * scenario.
2750 */
41ec9222 2751 if (IS_ZEBRA_DEBUG_RIB)
2752 {
2753 char buf[INET6_ADDRSTRLEN];
2754 if (IS_ZEBRA_DEBUG_RIB)
2755 {
2756 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
2757 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
2758 si->vrf_id, buf, p->prefixlen, rn, rib, rib->type);
2759 }
2760 }
94ad353d 2761 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
6baeb988 2762 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
94ad353d 2763 {
dccc5225 2764 /* If there are other active nexthops, do an update. */
2765 if (rib->nexthop_active_num > 1)
2766 {
2767 rib_install_kernel (rn, rib, 1);
2768 redistribute_update (&rn->p, rib, NULL);
2769 }
2770 else
2771 {
2772 redistribute_delete (&rn->p, rib);
2773 rib_uninstall_kernel (rn, rib);
2774 }
94ad353d 2775 }
6e26278c 2776
b8a1effa
DS
2777 if (afi == AFI_IP)
2778 {
2779 /* Delete the nexthop and dereg from NHT */
2780 nh_p.family = AF_INET;
2781 nh_p.prefixlen = IPV4_MAX_BITLEN;
2782 nh_p.u.prefix4 = nexthop->gate.ipv4;
2783 }
2784 else
2785 {
2786 nh_p.family = AF_INET6;
2787 nh_p.prefixlen = IPV6_MAX_BITLEN;
2788 nh_p.u.prefix6 = nexthop->gate.ipv6;
2789 }
a399694f 2790 rib_nexthop_delete (rib, nexthop);
6e26278c 2791 zebra_deregister_rnh_static_nh(&nh_p, rn);
a399694f 2792 nexthop_free (nexthop);
718e3744 2793 }
718e3744 2794 /* Unlock node. */
2795 route_unlock_node (rn);
2796}
2797
2798/* Add static route into static route configuration. */
2799int
ba779241 2800static_add_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
78104b9b 2801 u_char flags, u_short tag, u_char distance, vrf_id_t vrf_id)
718e3744 2802{
2803 u_char type = 0;
2804 struct route_node *rn;
c0551cbb
DS
2805 struct static_route *si;
2806 struct static_route *pp;
2807 struct static_route *cp;
2808 struct static_route *update = NULL;
78104b9b
FL
2809 struct zebra_vrf *zvrf = vrf_info_get (vrf_id);
2810 struct route_table *stable = zvrf->stable[AFI_IP][SAFI_UNICAST];
718e3744 2811
718e3744 2812 if (! stable)
2813 return -1;
2814
2815 /* Lookup static route prefix. */
2816 rn = route_node_get (stable, p);
2817
2818 /* Make flags. */
2819 if (gate)
2820 type = STATIC_IPV4_GATEWAY;
ba779241
DS
2821 else if (ifindex)
2822 type = STATIC_IFINDEX;
595db7f1 2823 else
2824 type = STATIC_IPV4_BLACKHOLE;
718e3744 2825
2826 /* Do nothing if there is a same static route. */
2827 for (si = rn->info; si; si = si->next)
2828 {
2829 if (type == si->type
c0551cbb 2830 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
ba779241 2831 && (! ifindex || ifindex == si->ifindex))
718e3744 2832 {
0d9551dc 2833 if ((distance == si->distance) && (tag == si->tag))
718e3744 2834 {
2835 route_unlock_node (rn);
2836 return 0;
2837 }
2838 else
2839 update = si;
2840 }
2841 }
2842
0d9551dc 2843 /* Distance or tag changed. */
718e3744 2844 if (update)
ba779241 2845 static_delete_ipv4 (p, gate, ifindex, update->tag, update->distance, vrf_id);
718e3744 2846
2847 /* Make new static route structure. */
c0551cbb 2848 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
718e3744 2849
2850 si->type = type;
2851 si->distance = distance;
81dfcaa2 2852 si->flags = flags;
0d9551dc 2853 si->tag = tag;
8f527c5e 2854 si->vrf_id = vrf_id;
ba779241 2855 si->ifindex = ifindex;
718e3744 2856
2857 if (gate)
c0551cbb 2858 si->addr.ipv4 = *gate;
718e3744 2859
2860 /* Add new static route information to the tree with sort by
2861 distance value and gateway address. */
2862 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
2863 {
2864 if (si->distance < cp->distance)
2865 break;
2866 if (si->distance > cp->distance)
2867 continue;
2868 if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
2869 {
c0551cbb 2870 if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr))
718e3744 2871 break;
c0551cbb 2872 if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr))
718e3744 2873 continue;
2874 }
2875 }
2876
2877 /* Make linked list. */
2878 if (pp)
2879 pp->next = si;
2880 else
2881 rn->info = si;
2882 if (cp)
2883 cp->prev = si;
2884 si->prev = pp;
2885 si->next = cp;
2886
2887 /* Install into rib. */
bcd548ff 2888 static_install_route (AFI_IP, SAFI_UNICAST, p, si);
718e3744 2889
2890 return 1;
2891}
2892
2893/* Delete static route from static route configuration. */
2894int
ba779241 2895static_delete_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
78104b9b 2896 u_short tag, u_char distance, vrf_id_t vrf_id)
718e3744 2897{
2898 u_char type = 0;
2899 struct route_node *rn;
c0551cbb 2900 struct static_route *si;
718e3744 2901 struct route_table *stable;
2902
2903 /* Lookup table. */
b72ede27 2904 stable = zebra_vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
718e3744 2905 if (! stable)
2906 return -1;
2907
2908 /* Lookup static route prefix. */
2909 rn = route_node_lookup (stable, p);
2910 if (! rn)
2911 return 0;
2912
2913 /* Make flags. */
2914 if (gate)
2915 type = STATIC_IPV4_GATEWAY;
ba779241
DS
2916 else if (ifindex)
2917 type = STATIC_IFINDEX;
595db7f1 2918 else
2919 type = STATIC_IPV4_BLACKHOLE;
718e3744 2920
2921 /* Find same static route is the tree */
2922 for (si = rn->info; si; si = si->next)
2923 if (type == si->type
c0551cbb 2924 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
ba779241 2925 && (! ifindex || ifindex == si->ifindex)
0d9551dc 2926 && (! tag || (tag == si->tag)))
718e3744 2927 break;
2928
2929 /* Can't find static route. */
2930 if (! si)
2931 {
2932 route_unlock_node (rn);
2933 return 0;
2934 }
2935
2936 /* Install into rib. */
b8a1effa 2937 static_uninstall_route (AFI_IP, SAFI_UNICAST, p, si);
718e3744 2938
2939 /* Unlink static route from linked list. */
2940 if (si->prev)
2941 si->prev->next = si->next;
2942 else
2943 rn->info = si->next;
2944 if (si->next)
2945 si->next->prev = si->prev;
143a385f 2946 route_unlock_node (rn);
718e3744 2947
2948 /* Free static route configuration. */
c0551cbb 2949 XFREE (MTYPE_STATIC_ROUTE, si);
718e3744 2950
143a385f 2951 route_unlock_node (rn);
2952
718e3744 2953 return 1;
2954}
2955
6b0655a2 2956
718e3744 2957int
7c8ff89e 2958rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
2959 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
2960 u_int32_t table_id, u_int32_t metric, u_char distance, safi_t safi)
718e3744 2961{
2962 struct rib *rib;
2963 struct rib *same = NULL;
2964 struct route_table *table;
2965 struct route_node *rn;
2966 struct nexthop *nexthop;
2967
718e3744 2968 /* Lookup table. */
7a4bb9c5
DS
2969 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
2970 {
78104b9b 2971 table = zebra_vrf_table (AFI_IP6, safi, vrf_id);
7a4bb9c5
DS
2972 }
2973 else
2974 {
78104b9b 2975 table = zebra_vrf_other_route_table(AFI_IP6, table_id, vrf_id);
7a4bb9c5 2976 }
718e3744 2977 if (! table)
2978 return 0;
2979
2980 /* Make sure mask is applied. */
2981 apply_mask_ipv6 (p);
2982
2983 /* Set default distance by route type. */
be61c4eb 2984 if (!distance)
2985 distance = route_info[type].distance;
718e3744 2986
2987 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2988 distance = 200;
2989
718e3744 2990 /* Lookup route node.*/
2991 rn = route_node_get (table, (struct prefix *) p);
2992
2993 /* If same type of route are installed, treat it as a implicit
2994 withdraw. */
9fd92e3c 2995 RNODE_FOREACH_RIB (rn, rib)
718e3744 2996 {
6d691129
PJ
2997 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2998 continue;
2999
ebf1ead0 3000 if (rib->type != type)
3001 continue;
7c8ff89e
DS
3002 if (rib->instance != instance)
3003 continue;
ebf1ead0 3004 if (rib->type != ZEBRA_ROUTE_CONNECT)
718e3744 3005 {
3006 same = rib;
718e3744 3007 break;
3008 }
ebf1ead0 3009 else if ((nexthop = rib->nexthop) &&
3010 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
3011 nexthop->ifindex == ifindex)
3012 {
3013 rib->refcnt++;
3014 return 0;
3015 }
718e3744 3016 }
3017
3018 /* Allocate new rib structure. */
4d38fdb4 3019 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
3020
718e3744 3021 rib->type = type;
7c8ff89e 3022 rib->instance = instance;
718e3744 3023 rib->distance = distance;
3024 rib->flags = flags;
3025 rib->metric = metric;
7a4bb9c5 3026 rib->table = table_id;
78104b9b 3027 rib->vrf_id = vrf_id;
718e3744 3028 rib->nexthop_num = 0;
3029 rib->uptime = time (NULL);
3030
3031 /* Nexthop settings. */
3032 if (gate)
3033 {
3034 if (ifindex)
a399694f 3035 rib_nexthop_ipv6_ifindex_add (rib, gate, ifindex);
718e3744 3036 else
a399694f 3037 rib_nexthop_ipv6_add (rib, gate);
718e3744 3038 }
3039 else
a399694f 3040 rib_nexthop_ifindex_add (rib, ifindex);
718e3744 3041
3042 /* If this route is kernel route, set FIB flag to the route. */
3043 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
3044 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
3045 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3046
3047 /* Link new rib to node.*/
fed643f4 3048 if (IS_ZEBRA_DEBUG_RIB)
41ec9222 3049 {
3050 char buf[INET6_ADDRSTRLEN];
3051 if (IS_ZEBRA_DEBUG_RIB)
3052 {
3053 inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN);
3054 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3055 "existing %p",
3056 vrf_id, buf, p->prefixlen, rn, rib, rib->type, same);
3057 }
3058
3059 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3060 rib_dump ((struct prefix *)p, rib);
3061 }
2bf26d41 3062 rib_addnode (rn, rib, 1);
718e3744 3063
718e3744 3064 /* Free implicit route.*/
3065 if (same)
4d38fdb4 3066 rib_delnode (rn, same);
3067
3068 route_unlock_node (rn);
718e3744 3069 return 0;
3070}
3071
41fc2714 3072int
8a92a8a0 3073rib_add_ipv6_multipath (struct prefix *p, struct rib *rib, safi_t safi,
41fc2714
DS
3074 unsigned long ifindex)
3075{
3076 struct route_table *table;
3077 struct route_node *rn;
3078 struct rib *same = NULL;
3079 struct nexthop *nexthop;
3080 int ret = 0;
4e3afb14 3081 unsigned int table_id = 0;
41fc2714 3082
8a92a8a0 3083 if (p->family == AF_INET)
7a4bb9c5 3084 {
8a92a8a0
DS
3085 if (!rib)
3086 return 0;
3087
12f6fb97 3088 table = zebra_vrf_table (AFI_IP, safi, rib->vrf_id);
8a92a8a0
DS
3089 if (!table)
3090 return 0;
3091 /* Make it sure prefixlen is applied to the prefix. */
88177fe3 3092 apply_mask_ipv4 ((struct prefix_ipv4 *)p);
7a4bb9c5
DS
3093 }
3094 else
3095 {
8a92a8a0
DS
3096 if (rib)
3097 table_id = rib->table;
3098 else
3099 return 0; /* why are we getting called with NULL rib */
41fc2714 3100
8a92a8a0
DS
3101 /* Lookup table. */
3102 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
3103 {
12f6fb97 3104 table = zebra_vrf_table (AFI_IP6, safi, rib->vrf_id);
8a92a8a0
DS
3105 }
3106 else
3107 {
12f6fb97 3108 table = zebra_vrf_other_route_table(AFI_IP6, table_id, rib->vrf_id);
8a92a8a0 3109 }
41fc2714 3110
8a92a8a0
DS
3111 if (! table)
3112 return 0;
3113
3114 /* Make sure mask is applied. */
88177fe3 3115 apply_mask_ipv6 ((struct prefix_ipv6 *)p);
8a92a8a0
DS
3116
3117 }
41fc2714
DS
3118
3119 /* Set default distance by route type. */
3120 if (rib->distance == 0)
3121 {
3122 rib->distance = route_info[rib->type].distance;
3123
3124 /* iBGP distance is 200. */
3125 if (rib->type == ZEBRA_ROUTE_BGP
3126 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
3127 rib->distance = 200;
3128 }
3129
3130 /* Lookup route node.*/
3131 rn = route_node_get (table, (struct prefix *) p);
3132
3133 /* If same type of route are installed, treat it as a implicit
3134 withdraw. */
3135 RNODE_FOREACH_RIB (rn, same) {
3136 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) {
3137 continue;
3138 }
3139 if (same->type != rib->type) {
3140 continue;
3141 }
3142
7c8ff89e
DS
3143 if (same->instance != rib->instance) {
3144 continue;
3145 }
3146
41fc2714
DS
3147 if (same->table != rib->table) {
3148 continue;
3149 }
3150 if (same->type != ZEBRA_ROUTE_CONNECT) {
3151 break;
3152 }
3153 else if ((nexthop = same->nexthop) &&
3154 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
3155 nexthop->ifindex == ifindex) {
3156 same->refcnt++;
3157 return 0;
3158 }
3159 }
3160
3161 /* If this route is kernel route, set FIB flag to the route. */
3162 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) {
3163 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) {
3164 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3165 }
3166 }
3167
3168 /* Link new rib to node.*/
41ec9222 3169 if (IS_ZEBRA_DEBUG_RIB)
3170 {
3171 char buf[INET6_ADDRSTRLEN];
3172 if (IS_ZEBRA_DEBUG_RIB)
3173 {
3174 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
3175 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3176 "existing %p",
3177 rib->vrf_id, buf, p->prefixlen, rn, rib, rib->type, same);
3178 }
3179
3180 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3181 rib_dump ((struct prefix *)p, rib);
3182 }
2bf26d41 3183 rib_addnode (rn, rib, 1);
41fc2714 3184 ret = 1;
41ec9222 3185
41fc2714
DS
3186 /* Free implicit route.*/
3187 if (same)
41fc2714 3188 {
41ec9222 3189 rib_delnode (rn, same);
3190 ret = -1;
41fc2714 3191 }
41fc2714
DS
3192
3193 route_unlock_node (rn);
3194 return ret;
3195}
3196
ebf1ead0 3197/* XXX factor with rib_delete_ipv6 */
12f6fb97 3198
718e3744 3199int
7c8ff89e 3200rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
3201 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
3202 u_int32_t table_id, safi_t safi)
718e3744 3203{
3204 struct route_table *table;
3205 struct route_node *rn;
3206 struct rib *rib;
3207 struct rib *fib = NULL;
3208 struct rib *same = NULL;
fa713d9e
CF
3209 struct nexthop *nexthop, *tnexthop;
3210 int recursing;
4690c7d7 3211 char buf1[PREFIX2STR_BUFFER];
81cce018 3212 char buf2[INET6_ADDRSTRLEN];
718e3744 3213
3214 /* Apply mask. */
3215 apply_mask_ipv6 (p);
3216
3217 /* Lookup table. */
7a4bb9c5
DS
3218 if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
3219 {
78104b9b 3220 table = zebra_vrf_table (AFI_IP6, safi, vrf_id);
7a4bb9c5
DS
3221 }
3222 else
3223 {
12f6fb97 3224 table = zebra_vrf_other_route_table(AFI_IP6, table_id, vrf_id);
7a4bb9c5 3225 }
718e3744 3226 if (! table)
3227 return 0;
4d38fdb4 3228
718e3744 3229 /* Lookup route node. */
3230 rn = route_node_lookup (table, (struct prefix *) p);
3231 if (! rn)
3232 {
41ec9222 3233 if (IS_ZEBRA_DEBUG_RIB)
3234 zlog_debug ("%u:%s/%d: doesn't exist in rib",
3235 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
3236 p->prefixlen);
718e3744 3237 return ZEBRA_ERR_RTNOEXIST;
3238 }
3239
3240 /* Lookup same type route. */
9fd92e3c 3241 RNODE_FOREACH_RIB (rn, rib)
718e3744 3242 {
6d691129
PJ
3243 if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
3244 continue;
3245
718e3744 3246 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
3247 fib = rib;
3248
ebf1ead0 3249 if (rib->type != type)
3250 continue;
7c8ff89e
DS
3251 if (rib->instance != instance)
3252 continue;
ebf1ead0 3253 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
4f1735fd 3254 nexthop->type == NEXTHOP_TYPE_IFINDEX)
718e3744 3255 {
4f1735fd
MF
3256 if (nexthop->ifindex != ifindex)
3257 continue;
ebf1ead0 3258 if (rib->refcnt)
718e3744 3259 {
ebf1ead0 3260 rib->refcnt--;
3261 route_unlock_node (rn);
3262 route_unlock_node (rn);
3263 return 0;
718e3744 3264 }
ebf1ead0 3265 same = rib;
3266 break;
718e3744 3267 }
ebf1ead0 3268 /* Make sure that the route found has the same gateway. */
fa713d9e
CF
3269 else
3270 {
3271 if (gate == NULL)
3272 {
3273 same = rib;
3274 break;
3275 }
3276 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
3277 if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
3278 {
3279 same = rib;
3280 break;
3281 }
3282 if (same)
3283 break;
3284 }
718e3744 3285 }
3286
3287 /* If same type of route can't be found and this message is from
3288 kernel. */
3289 if (! same)
3290 {
2037f143
DS
3291 if (fib && type == ZEBRA_ROUTE_KERNEL &&
3292 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
3293 {
41ec9222 3294 if (IS_ZEBRA_DEBUG_RIB)
3295 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
3296 "from kernel, adding",
3297 vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN),
3298 p->prefixlen, rn, fib, fib->type);
6baf7bb8
DS
3299 if (allow_delete)
3300 {
3301 /* Unset flags. */
3302 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
3303 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
3304
3305 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
3306 }
3307 else
3308 {
3309 /* This means someone else, other than Zebra, has deleted a Zebra
3310 * route from the kernel. We will add it back */
3311 rib_install_kernel(rn, fib, 0);
3312 }
2037f143 3313 }
718e3744 3314 else
3315 {
3316 if (IS_ZEBRA_DEBUG_KERNEL)
3317 {
3318 if (gate)
41ec9222 3319 zlog_debug ("%s: vrf %u via %s ifindex %d type %d "
78104b9b
FL
3320 "doesn't exist in rib",
3321 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
81cce018 3322 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
718e3744 3323 ifindex,
3324 type);
3325 else
41ec9222 3326 zlog_debug ("%s: vrf %u ifindex %d type %d doesn't exist in rib",
78104b9b 3327 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
718e3744 3328 ifindex,
3329 type);
3330 }
3331 route_unlock_node (rn);
3332 return ZEBRA_ERR_RTNOEXIST;
3333 }
3334 }
3335
718e3744 3336 if (same)
4d38fdb4 3337 rib_delnode (rn, same);
3338
718e3744 3339 route_unlock_node (rn);
718e3744 3340 return 0;
3341}
6b0655a2 3342
718e3744 3343/* Add static route into static route configuration. */
3344int
3345static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ba779241 3346 unsigned int ifindex, u_char flags, u_short tag,
78104b9b 3347 u_char distance, vrf_id_t vrf_id)
718e3744 3348{
3349 struct route_node *rn;
c0551cbb
DS
3350 struct static_route *si;
3351 struct static_route *pp;
3352 struct static_route *cp;
526e1728 3353 struct static_route *update = NULL;
78104b9b
FL
3354 struct zebra_vrf *zvrf = vrf_info_get (vrf_id);
3355 struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST];
718e3744 3356
718e3744 3357 if (! stable)
3358 return -1;
27b47253
PJ
3359
3360 if (!gate &&
ba779241 3361 (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFINDEX))
27b47253
PJ
3362 return -1;
3363
ba779241
DS
3364 if (!ifindex &&
3365 (type == STATIC_IPV6_GATEWAY_IFINDEX || type == STATIC_IFINDEX))
27b47253 3366 return -1;
718e3744 3367
3368 /* Lookup static route prefix. */
3369 rn = route_node_get (stable, p);
3370
3371 /* Do nothing if there is a same static route. */
3372 for (si = rn->info; si; si = si->next)
3373 {
526e1728 3374 if (type == si->type
c0551cbb 3375 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
ba779241 3376 && (! ifindex || ifindex == si->ifindex))
718e3744 3377 {
526e1728
DS
3378 if ((distance == si->distance) && (tag == si->tag))
3379 {
3380 route_unlock_node (rn);
3381 return 0;
3382 }
3383 else
3384 update = si;
718e3744 3385 }
3386 }
3387
526e1728
DS
3388 /* Distance or tag changed. */
3389 if (update)
ba779241 3390 static_delete_ipv6 (p, type, gate, ifindex, update->tag, update->distance, vrf_id);
526e1728 3391
718e3744 3392 /* Make new static route structure. */
c0551cbb 3393 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
718e3744 3394
3395 si->type = type;
3396 si->distance = distance;
81dfcaa2 3397 si->flags = flags;
0d9551dc 3398 si->tag = tag;
8f527c5e 3399 si->vrf_id = vrf_id;
ba779241 3400 si->ifindex = ifindex;
718e3744 3401
3402 switch (type)
3403 {
3404 case STATIC_IPV6_GATEWAY:
c0551cbb 3405 si->addr.ipv6 = *gate;
718e3744 3406 break;
ba779241 3407 case STATIC_IPV6_GATEWAY_IFINDEX:
c0551cbb 3408 si->addr.ipv6 = *gate;
718e3744 3409 break;
3410 }
3411
3412 /* Add new static route information to the tree with sort by
3413 distance value and gateway address. */
3414 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
3415 {
3416 if (si->distance < cp->distance)
3417 break;
3418 if (si->distance > cp->distance)
3419 continue;
3420 }
3421
3422 /* Make linked list. */
3423 if (pp)
3424 pp->next = si;
3425 else
3426 rn->info = si;
3427 if (cp)
3428 cp->prev = si;
3429 si->prev = pp;
3430 si->next = cp;
3431
3432 /* Install into rib. */
bcd548ff 3433 static_install_route (AFI_IP6, SAFI_UNICAST, p, si);
718e3744 3434
3435 return 1;
3436}
3437
3438/* Delete static route from static route configuration. */
3439int
3440static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ba779241 3441 unsigned int ifindex, u_short tag, u_char distance,
78104b9b 3442 vrf_id_t vrf_id)
718e3744 3443{
3444 struct route_node *rn;
c0551cbb 3445 struct static_route *si;
718e3744 3446 struct route_table *stable;
3447
3448 /* Lookup table. */
b72ede27 3449 stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
718e3744 3450 if (! stable)
3451 return -1;
3452
3453 /* Lookup static route prefix. */
3454 rn = route_node_lookup (stable, p);
3455 if (! rn)
3456 return 0;
3457
3458 /* Find same static route is the tree */
3459 for (si = rn->info; si; si = si->next)
3460 if (distance == si->distance
3461 && type == si->type
c0551cbb 3462 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
ba779241 3463 && (! ifindex || ifindex == si->ifindex)
0d9551dc 3464 && (! tag || (tag == si->tag)))
718e3744 3465 break;
3466
3467 /* Can't find static route. */
3468 if (! si)
3469 {
3470 route_unlock_node (rn);
3471 return 0;
3472 }
3473
3474 /* Install into rib. */
b8a1effa 3475 static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si);
718e3744 3476
3477 /* Unlink static route from linked list. */
3478 if (si->prev)
3479 si->prev->next = si->next;
3480 else
3481 rn->info = si->next;
3482 if (si->next)
3483 si->next->prev = si->prev;
3484
3485 /* Free static route configuration. */
c0551cbb 3486 XFREE (MTYPE_STATIC_ROUTE, si);
718e3744 3487
3488 return 1;
3489}
6b0655a2 3490
1c848137 3491/* Schedule routes of a particular table (address-family) based on event. */
3492static void
3493rib_update_table (struct route_table *table, rib_update_event_t event)
b84c7253 3494{
3495 struct route_node *rn;
b84c7253 3496 struct rib *rib, *next;
3497
1c848137 3498 /* Walk all routes and queue for processing, if appropriate for
3499 * the trigger event.
3500 */
3501 for (rn = route_top (table); rn; rn = route_next (rn))
3502 {
3503 switch (event)
3504 {
3505 case RIB_UPDATE_IF_CHANGE:
3506 /* Examine all routes that won't get processed by the protocol or
3507 * triggered by nexthop evaluation (NHT). This would be system,
3508 * kernel and certain static routes. Note that NHT will get
3509 * triggered upon an interface event as connected routes always
3510 * get queued for processing.
3511 */
3512 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3513 {
3514 if (rib->type == ZEBRA_ROUTE_OSPF ||
3515 rib->type == ZEBRA_ROUTE_OSPF6 ||
3516 rib->type == ZEBRA_ROUTE_BGP)
3517 continue; /* protocol will handle. */
3518 else if (rib->type == ZEBRA_ROUTE_STATIC)
3519 {
3520 struct nexthop *nh;
3521 for (nh = rib->nexthop; nh; nh = nh->next)
3522 if (!(nh->type == NEXTHOP_TYPE_IPV4 ||
3523 nh->type == NEXTHOP_TYPE_IPV6))
3524 break;
3525
3526 /* If we only have nexthops to a gateway, NHT will
3527 * take care.
3528 */
3529 if (nh)
3530 rib_queue_add (&zebrad, rn);
3531 }
3532 else
3533 rib_queue_add (&zebrad, rn);
3534 }
3535 break;
b84c7253 3536
1c848137 3537 case RIB_UPDATE_RMAP_CHANGE:
3538 case RIB_UPDATE_OTHER:
3539 /* Right now, examine all routes. Can restrict to a protocol in
3540 * some cases (TODO).
3541 */
3542 if (rnode_to_ribs (rn))
b84c7253 3543 rib_queue_add (&zebrad, rn);
1c848137 3544 break;
3545
3546 default:
3547 break;
3548 }
3549 }
b84c7253 3550}
3551
718e3744 3552/* RIB update function. */
3553void
1c848137 3554rib_update (vrf_id_t vrf_id, rib_update_event_t event)
718e3744 3555{
718e3744 3556 struct route_table *table;
1c848137 3557
3558 /* Process routes of interested address-families. */
78104b9b 3559 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
718e3744 3560 if (table)
1c848137 3561 rib_update_table (table, event);
718e3744 3562
78104b9b 3563 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
718e3744 3564 if (table)
1c848137 3565 rib_update_table (table, event);
718e3744 3566}
3567
718e3744 3568/* Remove all routes which comes from non main table. */
a1ac18c4 3569static void
718e3744 3570rib_weed_table (struct route_table *table)
3571{
3572 struct route_node *rn;
3573 struct rib *rib;
3574 struct rib *next;
3575
3576 if (table)
3577 for (rn = route_top (table); rn; rn = route_next (rn))
9fd92e3c 3578 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
718e3744 3579 {
6d691129
PJ
3580 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3581 continue;
3582
b21b19c5 3583 if (rib->table != zebrad.rtm_table_default &&
718e3744 3584 rib->table != RT_TABLE_MAIN)
4d38fdb4 3585 rib_delnode (rn, rib);
718e3744 3586 }
3587}
3588
3589/* Delete all routes from non main table. */
3590void
a1ac18c4 3591rib_weed_tables (void)
718e3744 3592{
78104b9b
FL
3593 vrf_iter_t iter;
3594 struct zebra_vrf *zvrf;
3595
3596 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3597 if ((zvrf = vrf_iter2info (iter)) != NULL)
3598 {
3599 rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3600 rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3601 }
718e3744 3602}
6b0655a2 3603
718e3744 3604/* Delete self installed routes after zebra is relaunched. */
a1ac18c4 3605static void
718e3744 3606rib_sweep_table (struct route_table *table)
3607{
3608 struct route_node *rn;
3609 struct rib *rib;
3610 struct rib *next;
3611 int ret = 0;
3612
3613 if (table)
3614 for (rn = route_top (table); rn; rn = route_next (rn))
9fd92e3c 3615 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
718e3744 3616 {
6d691129
PJ
3617 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3618 continue;
3619
718e3744 3620 if (rib->type == ZEBRA_ROUTE_KERNEL &&
3621 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
3622 {
3623 ret = rib_uninstall_kernel (rn, rib);
3624 if (! ret)
4d38fdb4 3625 rib_delnode (rn, rib);
718e3744 3626 }
3627 }
3628}
3629
3630/* Sweep all RIB tables. */
3631void
a1ac18c4 3632rib_sweep_route (void)
718e3744 3633{
78104b9b
FL
3634 vrf_iter_t iter;
3635 struct zebra_vrf *zvrf;
3636
3637 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3638 if ((zvrf = vrf_iter2info (iter)) != NULL)
3639 {
3640 rib_sweep_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3641 rib_sweep_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3642 }
718e3744 3643}
2ea1ab1c
VT
3644
3645/* Remove specific by protocol routes from 'table'. */
3646static unsigned long
7c8ff89e 3647rib_score_proto_table (u_char proto, u_short instance, struct route_table *table)
2ea1ab1c
VT
3648{
3649 struct route_node *rn;
3650 struct rib *rib;
3651 struct rib *next;
3652 unsigned long n = 0;
3653
3654 if (table)
3655 for (rn = route_top (table); rn; rn = route_next (rn))
9fd92e3c 3656 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
2ea1ab1c 3657 {
2ea1ab1c
VT
3658 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3659 continue;
7c8ff89e 3660 if (rib->type == proto && rib->instance == instance)
2ea1ab1c
VT
3661 {
3662 rib_delnode (rn, rib);
3663 n++;
3664 }
3665 }
3666
3667 return n;
3668}
3669
3670/* Remove specific by protocol routes. */
3671unsigned long
7c8ff89e 3672rib_score_proto (u_char proto, u_short instance)
2ea1ab1c 3673{
78104b9b
FL
3674 vrf_iter_t iter;
3675 struct zebra_vrf *zvrf;
3676 unsigned long cnt = 0;
3677
3678 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3679 if ((zvrf = vrf_iter2info (iter)) != NULL)
3680 cnt += rib_score_proto_table (proto, instance, zvrf->table[AFI_IP][SAFI_UNICAST])
3681 +rib_score_proto_table (proto, instance, zvrf->table[AFI_IP6][SAFI_UNICAST]);
3682
3683 return cnt;
2ea1ab1c
VT
3684}
3685
718e3744 3686/* Close RIB and clean up kernel routes. */
a31c5886 3687void
718e3744 3688rib_close_table (struct route_table *table)
3689{
3690 struct route_node *rn;
3691 struct rib *rib;
3692
3693 if (table)
3694 for (rn = route_top (table); rn; rn = route_next (rn))
9fd92e3c 3695 RNODE_FOREACH_RIB (rn, rib)
6d691129 3696 {
9fd92e3c
AS
3697 if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
3698 continue;
3699
5adc2528
AS
3700 zfpm_trigger_update (rn, NULL);
3701
9fd92e3c
AS
3702 if (! RIB_SYSTEM_ROUTE (rib))
3703 rib_uninstall_kernel (rn, rib);
6d691129 3704 }
718e3744 3705}
3706
3707/* Close all RIB tables. */
3708void
a1ac18c4 3709rib_close (void)
718e3744 3710{
78104b9b
FL
3711 vrf_iter_t iter;
3712 struct zebra_vrf *zvrf;
3713 struct listnode *node;
5c610faf
DS
3714 struct interface *ifp;
3715
78104b9b
FL
3716 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3717 {
3718 if ((zvrf = vrf_iter2info (iter)) != NULL)
3719 {
3720 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3721 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3722 }
3723 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
3724 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
3725 }
718e3744 3726}
6b0655a2 3727
718e3744 3728/* Routing information base initialize. */
3729void
a1ac18c4 3730rib_init (void)
718e3744 3731{
4d38fdb4 3732 rib_queue_init (&zebrad);
718e3744 3733}
0915bb0c
AS
3734
3735/*
3736 * vrf_id_get_next
3737 *
3738 * Get the first vrf id that is greater than the given vrf id if any.
3739 *
3740 * Returns TRUE if a vrf id was found, FALSE otherwise.
3741 */
3742static inline int
b72ede27 3743vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 3744{
b72ede27
FL
3745 vrf_iter_t iter = vrf_iterator (vrf_id);
3746 struct zebra_vrf *zvrf = vrf_iter2info (iter);
3747
3748 /* The same one ? Then find out the next. */
3749 if (zvrf && (zvrf->vrf_id == vrf_id))
3750 zvrf = vrf_iter2info (vrf_next (iter));
3751
3752 if (zvrf)
0915bb0c 3753 {
b72ede27
FL
3754 *next_id_p = zvrf->vrf_id;
3755 return 1;
0915bb0c
AS
3756 }
3757
3758 return 0;
3759}
3760
3761/*
3762 * rib_tables_iter_next
3763 *
3764 * Returns the next table in the iteration.
3765 */
3766struct route_table *
3767rib_tables_iter_next (rib_tables_iter_t *iter)
3768{
3769 struct route_table *table;
3770
3771 /*
3772 * Array that helps us go over all AFI/SAFI combinations via one
3773 * index.
3774 */
3775 static struct {
3776 afi_t afi;
3777 safi_t safi;
3778 } afi_safis[] = {
3779 { AFI_IP, SAFI_UNICAST },
3780 { AFI_IP, SAFI_MULTICAST },
3781 { AFI_IP6, SAFI_UNICAST },
3782 { AFI_IP6, SAFI_MULTICAST },
3783 };
3784
3785 table = NULL;
3786
3787 switch (iter->state)
3788 {
3789
3790 case RIB_TABLES_ITER_S_INIT:
9c2bf1cf 3791 iter->vrf_id = VRF_DEFAULT;
0915bb0c
AS
3792 iter->afi_safi_ix = -1;
3793
3794 /* Fall through */
3795
3796 case RIB_TABLES_ITER_S_ITERATING:
3797 iter->afi_safi_ix++;
3798 while (1)
3799 {
3800
3801 while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
3802 {
b72ede27 3803 table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi,
0915bb0c
AS
3804 afi_safis[iter->afi_safi_ix].safi,
3805 iter->vrf_id);
3806 if (table)
3807 break;
3808
3809 iter->afi_safi_ix++;
3810 }
3811
3812 /*
3813 * Found another table in this vrf.
3814 */
3815 if (table)
3816 break;
3817
3818 /*
3819 * Done with all tables in the current vrf, go to the next
3820 * one.
3821 */
3822 if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
3823 break;
3824
3825 iter->afi_safi_ix = 0;
3826 }
3827
3828 break;
3829
3830 case RIB_TABLES_ITER_S_DONE:
3831 return NULL;
3832 }
3833
3834 if (table)
3835 iter->state = RIB_TABLES_ITER_S_ITERATING;
3836 else
3837 iter->state = RIB_TABLES_ITER_S_DONE;
3838
3839 return table;
3840}
b72ede27
FL
3841
3842/*
3843 * Create a routing table for the specific AFI/SAFI in the given VRF.
3844 */
3845static void
3846zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi)
3847{
3848 rib_table_info_t *info;
3849 struct route_table *table;
3850
3851 assert (!zvrf->table[afi][safi]);
3852
3853 table = route_table_init ();
3854 zvrf->table[afi][safi] = table;
3855
3856 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
3857 info->zvrf = zvrf;
3858 info->afi = afi;
3859 info->safi = safi;
3860 table->info = info;
3861}
3862
3863/* Allocate new zebra VRF. */
3864struct zebra_vrf *
12f6fb97 3865zebra_vrf_alloc (vrf_id_t vrf_id, const char *name)
b72ede27
FL
3866{
3867 struct zebra_vrf *zvrf;
3868
3869 zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf));
3870
3871 /* Allocate routing table and static table. */
3872 zebra_vrf_table_create (zvrf, AFI_IP, SAFI_UNICAST);
3873 zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_UNICAST);
3874 zvrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
3875 zvrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
3876 zebra_vrf_table_create (zvrf, AFI_IP, SAFI_MULTICAST);
3877 zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_MULTICAST);
3878 zvrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
3879 zvrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
3880
3881 zvrf->rnh_table[AFI_IP] = route_table_init();
3882 zvrf->rnh_table[AFI_IP6] = route_table_init();
3883
3884 zvrf->import_check_table[AFI_IP] = route_table_init();
3885 zvrf->import_check_table[AFI_IP6] = route_table_init();
3886
3887 /* Set VRF ID */
3888 zvrf->vrf_id = vrf_id;
3889
12f6fb97
DS
3890 if (name)
3891 {
3892 strncpy (zvrf->name, name, strlen(name));
3893 zvrf->name[strlen(name)] = '\0';
3894 }
8f7d9fc0 3895
b72ede27
FL
3896 return zvrf;
3897}
3898
3899/* Lookup VRF by identifier. */
3900struct zebra_vrf *
3901zebra_vrf_lookup (vrf_id_t vrf_id)
3902{
92955671 3903 return vrf_info_lookup (vrf_id);
b72ede27
FL
3904}
3905
3906/* Lookup the routing table in an enabled VRF. */
3907struct route_table *
3908zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
3909{
3910 struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
3911
3912 if (!zvrf)
3913 return NULL;
3914
3915 if (afi >= AFI_MAX || safi >= SAFI_MAX)
3916 return NULL;
3917
3918 return zvrf->table[afi][safi];
3919}
3920
3921/* Lookup the static routing table in a VRF. */
3922struct route_table *
3923zebra_vrf_static_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
3924{
3925 struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
3926
3927 if (!zvrf)
3928 return NULL;
3929
3930 if (afi >= AFI_MAX || safi >= SAFI_MAX)
3931 return NULL;
3932
3933 return zvrf->stable[afi][safi];
3934}
3935
3936struct route_table *
3937zebra_vrf_other_route_table (afi_t afi, u_int32_t table_id, vrf_id_t vrf_id)
3938{
3939 struct zebra_vrf *zvrf;
99a654bd 3940 rib_table_info_t *info;
3941 struct route_table *table;
b72ede27
FL
3942
3943 zvrf = vrf_info_lookup (vrf_id);
3944 if (! zvrf)
3945 return NULL;
3946
3947 if(afi >= AFI_MAX)
3948 return NULL;
3949
3950 if (table_id >= ZEBRA_KERNEL_TABLE_MAX)
3951 return NULL;
3952
99a654bd 3953 if ((vrf_id == VRF_DEFAULT) && (table_id != RT_TABLE_MAIN) && (table_id != zebrad.rtm_table_default))
b72ede27 3954 {
99a654bd 3955 if (zvrf->other_table[afi][table_id] == NULL)
3956 {
3957 table = route_table_init();
3958 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
3959 info->zvrf = zvrf;
3960 info->afi = afi;
3961 info->safi = SAFI_UNICAST;
3962 table->info = info;
3963 zvrf->other_table[afi][table_id] = table;
3964 }
12f6fb97 3965
99a654bd 3966 return (zvrf->other_table[afi][table_id]);
3967 }
3968
12f6fb97 3969 return zvrf->table[afi][SAFI_UNICAST];
b72ede27 3970}