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