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