blob: ace69cacbe2e6dc9ee6981ad7a39e2118086b436 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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"
paul4d38fdb2005-04-28 17:35:14 +000032#include "linklist.h"
33#include "thread.h"
34#include "workqueue.h"
Paul Jakma7514fb72007-05-02 16:05:35 +000035#include "prefix.h"
36#include "routemap.h"
paul718e3742002-12-13 20:15:29 +000037
38#include "zebra/rib.h"
39#include "zebra/rt.h"
40#include "zebra/zserv.h"
41#include "zebra/redistribute.h"
42#include "zebra/debug.h"
Avneesh Sachdev5adc2522012-11-13 22:48:59 +000043#include "zebra/zebra_fpm.h"
paul718e3742002-12-13 20:15:29 +000044
45/* Default rtm_table for all clients */
paulb21b19c2003-06-15 01:28:29 +000046extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000047
Paul Jakma457eb9a2006-07-27 19:59:58 +000048/* Hold time for RIB process, should be very minimal.
49 * it is useful to able to set it otherwise for testing, hence exported
50 * as global here for test-rig code.
51 */
52int rib_process_hold_time = 10;
53
paul718e3742002-12-13 20:15:29 +000054/* Each route type's string and default distance value. */
Stephen Hemmingerd145bc02008-08-17 17:41:37 +010055static const struct
paul718e3742002-12-13 20:15:29 +000056{
57 int key;
58 int distance;
Paul Jakma57345092011-12-25 17:52:09 +010059} route_info[ZEBRA_ROUTE_MAX] =
paul718e3742002-12-13 20:15:29 +000060{
Paul Jakma57345092011-12-25 17:52:09 +010061 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
62 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
63 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
64 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
65 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
66 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
67 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
68 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
69 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
70 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
71 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 95},
David Lamparter7052f222009-08-27 00:28:28 +020072 /* no entry/default: 150 */
paul718e3742002-12-13 20:15:29 +000073};
74
75/* Vector for routing table. */
Stephen Hemmingerd145bc02008-08-17 17:41:37 +010076static vector vrf_vector;
paul718e3742002-12-13 20:15:29 +000077
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +000078/*
79 * vrf_table_create
80 */
81static void
82vrf_table_create (struct vrf *vrf, afi_t afi, safi_t safi)
83{
84 rib_table_info_t *info;
85 struct route_table *table;
86
87 assert (!vrf->table[afi][safi]);
88
89 table = route_table_init ();
90 vrf->table[afi][safi] = table;
91
92 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
93 info->vrf = vrf;
94 info->afi = afi;
95 info->safi = safi;
96 table->info = info;
97}
98
paul718e3742002-12-13 20:15:29 +000099/* Allocate new VRF. */
paula1ac18c2005-06-28 17:17:12 +0000100static struct vrf *
hassofce954f2004-10-07 20:29:24 +0000101vrf_alloc (const char *name)
paul718e3742002-12-13 20:15:29 +0000102{
103 struct vrf *vrf;
104
105 vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
106
107 /* Put name. */
108 if (name)
109 vrf->name = XSTRDUP (MTYPE_VRF_NAME, name);
110
111 /* Allocate routing table and static table. */
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000112 vrf_table_create (vrf, AFI_IP, SAFI_UNICAST);
113 vrf_table_create (vrf, AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000114 vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
115 vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000116 vrf_table_create (vrf, AFI_IP, SAFI_MULTICAST);
117 vrf_table_create (vrf, AFI_IP6, SAFI_MULTICAST);
G.Balajicddf3912011-11-26 21:59:32 +0400118 vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
119 vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
120
paul718e3742002-12-13 20:15:29 +0000121
122 return vrf;
123}
124
paul718e3742002-12-13 20:15:29 +0000125/* Lookup VRF by identifier. */
126struct vrf *
127vrf_lookup (u_int32_t id)
128{
129 return vector_lookup (vrf_vector, id);
130}
131
paul718e3742002-12-13 20:15:29 +0000132/* Initialize VRF. */
paula1ac18c2005-06-28 17:17:12 +0000133static void
134vrf_init (void)
paul718e3742002-12-13 20:15:29 +0000135{
136 struct vrf *default_table;
137
138 /* Allocate VRF vector. */
139 vrf_vector = vector_init (1);
140
141 /* Allocate default main table. */
142 default_table = vrf_alloc ("Default-IP-Routing-Table");
143
144 /* Default table index must be 0. */
145 vector_set_index (vrf_vector, 0, default_table);
146}
147
148/* Lookup route table. */
149struct route_table *
150vrf_table (afi_t afi, safi_t safi, u_int32_t id)
151{
152 struct vrf *vrf;
153
154 vrf = vrf_lookup (id);
155 if (! vrf)
156 return NULL;
157
Leonid Rosenboim9499bf22012-12-06 20:17:41 +0000158 if( afi >= AFI_MAX || safi >= SAFI_MAX )
159 return NULL;
160
paul718e3742002-12-13 20:15:29 +0000161 return vrf->table[afi][safi];
162}
163
164/* Lookup static route table. */
165struct route_table *
166vrf_static_table (afi_t afi, safi_t safi, u_int32_t id)
167{
168 struct vrf *vrf;
169
170 vrf = vrf_lookup (id);
171 if (! vrf)
172 return NULL;
173
Leonid Rosenboim9499bf22012-12-06 20:17:41 +0000174 if( afi >= AFI_MAX || safi >= SAFI_MAX )
175 return NULL;
176
paul718e3742002-12-13 20:15:29 +0000177 return vrf->stable[afi][safi];
178}
179
Avneesh Sachdev78deec42012-11-13 22:48:56 +0000180/*
181 * nexthop_type_to_str
182 */
183const char *
184nexthop_type_to_str (enum nexthop_types_t nh_type)
185{
186 static const char *desc[] = {
187 "none",
188 "Directly connected",
189 "Interface route",
190 "IPv4 nexthop",
191 "IPv4 nexthop with ifindex",
192 "IPv4 nexthop with ifname",
193 "IPv6 nexthop",
194 "IPv6 nexthop with ifindex",
195 "IPv6 nexthop with ifname",
196 "Null0 nexthop",
197 };
198
199 if (nh_type >= ZEBRA_NUM_OF (desc))
200 return "<Invalid nh type>";
201
202 return desc[nh_type];
203}
204
Christian Frankefa713d92013-07-05 15:35:37 +0000205/* Add nexthop to the end of a nexthop list. */
paula1ac18c2005-06-28 17:17:12 +0000206static void
Christian Frankefa713d92013-07-05 15:35:37 +0000207_nexthop_add (struct nexthop **target, struct nexthop *nexthop)
paul718e3742002-12-13 20:15:29 +0000208{
209 struct nexthop *last;
210
Christian Frankefa713d92013-07-05 15:35:37 +0000211 for (last = *target; last && last->next; last = last->next)
paul718e3742002-12-13 20:15:29 +0000212 ;
213 if (last)
214 last->next = nexthop;
215 else
Christian Frankefa713d92013-07-05 15:35:37 +0000216 *target = nexthop;
paul718e3742002-12-13 20:15:29 +0000217 nexthop->prev = last;
Christian Frankefa713d92013-07-05 15:35:37 +0000218}
paul718e3742002-12-13 20:15:29 +0000219
Christian Frankefa713d92013-07-05 15:35:37 +0000220/* Add nexthop to the end of a rib node's nexthop list */
221static void
222nexthop_add (struct rib *rib, struct nexthop *nexthop)
223{
224 _nexthop_add(&rib->nexthop, nexthop);
paul718e3742002-12-13 20:15:29 +0000225 rib->nexthop_num++;
226}
227
228/* Delete specified nexthop from the list. */
paula1ac18c2005-06-28 17:17:12 +0000229static void
paul718e3742002-12-13 20:15:29 +0000230nexthop_delete (struct rib *rib, struct nexthop *nexthop)
231{
232 if (nexthop->next)
233 nexthop->next->prev = nexthop->prev;
234 if (nexthop->prev)
235 nexthop->prev->next = nexthop->next;
236 else
237 rib->nexthop = nexthop->next;
238 rib->nexthop_num--;
239}
240
Christian Frankefa713d92013-07-05 15:35:37 +0000241static void nexthops_free(struct nexthop *nexthop);
242
paul718e3742002-12-13 20:15:29 +0000243/* Free nexthop. */
paula1ac18c2005-06-28 17:17:12 +0000244static void
paul718e3742002-12-13 20:15:29 +0000245nexthop_free (struct nexthop *nexthop)
246{
paula4b70762003-05-16 17:19:48 +0000247 if (nexthop->ifname)
248 XFREE (0, nexthop->ifname);
Christian Frankefa713d92013-07-05 15:35:37 +0000249 if (nexthop->resolved)
250 nexthops_free(nexthop->resolved);
paul718e3742002-12-13 20:15:29 +0000251 XFREE (MTYPE_NEXTHOP, nexthop);
252}
253
Christian Frankefa713d92013-07-05 15:35:37 +0000254/* Frees a list of nexthops */
255static void
256nexthops_free (struct nexthop *nexthop)
257{
258 struct nexthop *nh, *next;
259
260 for (nh = nexthop; nh; nh = next)
261 {
262 next = nh->next;
263 nexthop_free (nh);
264 }
265}
266
paul718e3742002-12-13 20:15:29 +0000267struct nexthop *
268nexthop_ifindex_add (struct rib *rib, unsigned int ifindex)
269{
270 struct nexthop *nexthop;
271
Stephen Hemminger393deb92008-08-18 14:13:29 -0700272 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000273 nexthop->type = NEXTHOP_TYPE_IFINDEX;
274 nexthop->ifindex = ifindex;
275
276 nexthop_add (rib, nexthop);
277
278 return nexthop;
279}
280
281struct nexthop *
282nexthop_ifname_add (struct rib *rib, char *ifname)
283{
284 struct nexthop *nexthop;
285
Stephen Hemminger393deb92008-08-18 14:13:29 -0700286 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000287 nexthop->type = NEXTHOP_TYPE_IFNAME;
paula4b70762003-05-16 17:19:48 +0000288 nexthop->ifname = XSTRDUP (0, ifname);
paul718e3742002-12-13 20:15:29 +0000289
290 nexthop_add (rib, nexthop);
291
292 return nexthop;
293}
294
295struct nexthop *
Paul Jakma7514fb72007-05-02 16:05:35 +0000296nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
paul718e3742002-12-13 20:15:29 +0000297{
298 struct nexthop *nexthop;
299
Stephen Hemminger393deb92008-08-18 14:13:29 -0700300 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000301 nexthop->type = NEXTHOP_TYPE_IPV4;
302 nexthop->gate.ipv4 = *ipv4;
Paul Jakma7514fb72007-05-02 16:05:35 +0000303 if (src)
304 nexthop->src.ipv4 = *src;
paul718e3742002-12-13 20:15:29 +0000305
306 nexthop_add (rib, nexthop);
307
308 return nexthop;
309}
310
Josh Bailey26e2ae32012-03-22 01:09:21 -0700311struct nexthop *
paul718e3742002-12-13 20:15:29 +0000312nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
Paul Jakma7514fb72007-05-02 16:05:35 +0000313 struct in_addr *src, unsigned int ifindex)
paul718e3742002-12-13 20:15:29 +0000314{
315 struct nexthop *nexthop;
316
Stephen Hemminger393deb92008-08-18 14:13:29 -0700317 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000318 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
319 nexthop->gate.ipv4 = *ipv4;
Paul Jakma7514fb72007-05-02 16:05:35 +0000320 if (src)
321 nexthop->src.ipv4 = *src;
paul718e3742002-12-13 20:15:29 +0000322 nexthop->ifindex = ifindex;
323
324 nexthop_add (rib, nexthop);
325
326 return nexthop;
327}
328
329#ifdef HAVE_IPV6
330struct nexthop *
331nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
332{
333 struct nexthop *nexthop;
334
Stephen Hemminger393deb92008-08-18 14:13:29 -0700335 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000336 nexthop->type = NEXTHOP_TYPE_IPV6;
337 nexthop->gate.ipv6 = *ipv6;
338
339 nexthop_add (rib, nexthop);
340
341 return nexthop;
342}
343
paula1ac18c2005-06-28 17:17:12 +0000344static struct nexthop *
paul718e3742002-12-13 20:15:29 +0000345nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6,
346 char *ifname)
347{
348 struct nexthop *nexthop;
349
Stephen Hemminger393deb92008-08-18 14:13:29 -0700350 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000351 nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME;
352 nexthop->gate.ipv6 = *ipv6;
353 nexthop->ifname = XSTRDUP (0, ifname);
354
355 nexthop_add (rib, nexthop);
356
357 return nexthop;
358}
359
paula1ac18c2005-06-28 17:17:12 +0000360static struct nexthop *
paul718e3742002-12-13 20:15:29 +0000361nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
362 unsigned int ifindex)
363{
364 struct nexthop *nexthop;
365
Stephen Hemminger393deb92008-08-18 14:13:29 -0700366 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000367 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
368 nexthop->gate.ipv6 = *ipv6;
369 nexthop->ifindex = ifindex;
370
371 nexthop_add (rib, nexthop);
372
373 return nexthop;
374}
375#endif /* HAVE_IPV6 */
376
paul595db7f2003-05-25 21:35:06 +0000377struct nexthop *
378nexthop_blackhole_add (struct rib *rib)
379{
380 struct nexthop *nexthop;
381
Stephen Hemminger393deb92008-08-18 14:13:29 -0700382 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul595db7f2003-05-25 21:35:06 +0000383 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
384 SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
385
386 nexthop_add (rib, nexthop);
387
388 return nexthop;
389}
390
Christian Frankefa713d92013-07-05 15:35:37 +0000391/* This method checks whether a recursive nexthop has at
392 * least one resolved nexthop in the fib.
393 */
394int
395nexthop_has_fib_child(struct nexthop *nexthop)
396{
397 struct nexthop *nh;
398
399 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
400 return 0;
401
402 for (nh = nexthop->resolved; nh; nh = nh->next)
403 if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB))
404 return 1;
405
406 return 0;
407}
408
paul718e3742002-12-13 20:15:29 +0000409/* If force flag is not set, do not modify falgs at all for uninstall
410 the route from FIB. */
paula1ac18c2005-06-28 17:17:12 +0000411static int
paul718e3742002-12-13 20:15:29 +0000412nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
413 struct route_node *top)
414{
415 struct prefix_ipv4 p;
416 struct route_table *table;
417 struct route_node *rn;
418 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000419 int resolved;
paul718e3742002-12-13 20:15:29 +0000420 struct nexthop *newhop;
Christian Frankefa713d92013-07-05 15:35:37 +0000421 struct nexthop *resolved_hop;
paul718e3742002-12-13 20:15:29 +0000422
423 if (nexthop->type == NEXTHOP_TYPE_IPV4)
424 nexthop->ifindex = 0;
425
426 if (set)
Christian Frankefa713d92013-07-05 15:35:37 +0000427 {
428 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
429 nexthops_free(nexthop->resolved);
430 nexthop->resolved = NULL;
431 }
paul718e3742002-12-13 20:15:29 +0000432
433 /* Make lookup prefix. */
434 memset (&p, 0, sizeof (struct prefix_ipv4));
435 p.family = AF_INET;
436 p.prefixlen = IPV4_MAX_PREFIXLEN;
437 p.prefix = nexthop->gate.ipv4;
438
439 /* Lookup table. */
440 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
441 if (! table)
442 return 0;
443
444 rn = route_node_match (table, (struct prefix *) &p);
445 while (rn)
446 {
447 route_unlock_node (rn);
448
David Warda50c1072009-12-03 15:34:39 +0300449 /* If lookup self prefix return immediately. */
paul718e3742002-12-13 20:15:29 +0000450 if (rn == top)
451 return 0;
452
453 /* Pick up selected route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000454 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100455 {
456 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
457 continue;
458 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
459 break;
460 }
paul718e3742002-12-13 20:15:29 +0000461
462 /* If there is no selected route or matched route is EGP, go up
463 tree. */
464 if (! match
465 || match->type == ZEBRA_ROUTE_BGP)
466 {
467 do {
468 rn = rn->parent;
469 } while (rn && rn->info == NULL);
470 if (rn)
471 route_lock_node (rn);
472 }
473 else
474 {
Christian Franke48a53dc2013-07-05 15:35:38 +0000475 /* If the longest prefix match for the nexthop yields
476 * a blackhole, mark it as inactive. */
477 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
478 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
479 return 0;
480
paul718e3742002-12-13 20:15:29 +0000481 if (match->type == ZEBRA_ROUTE_CONNECT)
482 {
483 /* Directly point connected route. */
484 newhop = match->nexthop;
485 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
486 nexthop->ifindex = newhop->ifindex;
487
488 return 1;
489 }
490 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
491 {
Christian Frankefa713d92013-07-05 15:35:37 +0000492 resolved = 0;
paul718e3742002-12-13 20:15:29 +0000493 for (newhop = match->nexthop; newhop; newhop = newhop->next)
494 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
495 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
496 {
497 if (set)
498 {
499 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
Christian Frankefa713d92013-07-05 15:35:37 +0000500
501 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
502 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
Christian Frankec3e6b592013-07-05 15:35:40 +0000503 /* If the resolving route specifies a gateway, use it */
504 if (newhop->type == NEXTHOP_TYPE_IPV4
505 || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX
506 || newhop->type == NEXTHOP_TYPE_IPV4_IFNAME)
507 {
508 resolved_hop->type = newhop->type;
509 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
Christian Frankefa713d92013-07-05 15:35:37 +0000510
Christian Frankec3e6b592013-07-05 15:35:40 +0000511 if (newhop->ifindex)
512 {
513 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
514 resolved_hop->ifindex = newhop->ifindex;
515 }
516 }
Christian Frankefa713d92013-07-05 15:35:37 +0000517
Christian Frankec3e6b592013-07-05 15:35:40 +0000518 /* If the resolving route is an interface route,
519 * it means the gateway we are looking up is connected
520 * to that interface. (The actual network is _not_ onlink).
521 * Therefore, the resolved route should have the original
522 * gateway as nexthop as it is directly connected.
523 *
524 * On Linux, we have to set the onlink netlink flag because
525 * otherwise, the kernel won't accept the route. */
paul718e3742002-12-13 20:15:29 +0000526 if (newhop->type == NEXTHOP_TYPE_IFINDEX
Christian Frankec3e6b592013-07-05 15:35:40 +0000527 || newhop->type == NEXTHOP_TYPE_IFNAME)
528 {
529 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
530 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
531 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
532 resolved_hop->ifindex = newhop->ifindex;
533 }
Christian Frankefa713d92013-07-05 15:35:37 +0000534
535 _nexthop_add(&nexthop->resolved, resolved_hop);
paul718e3742002-12-13 20:15:29 +0000536 }
Christian Frankefa713d92013-07-05 15:35:37 +0000537 resolved = 1;
paul718e3742002-12-13 20:15:29 +0000538 }
Christian Frankefa713d92013-07-05 15:35:37 +0000539 return resolved;
paul718e3742002-12-13 20:15:29 +0000540 }
541 else
542 {
543 return 0;
544 }
545 }
546 }
547 return 0;
548}
549
550#ifdef HAVE_IPV6
551/* If force flag is not set, do not modify falgs at all for uninstall
552 the route from FIB. */
paula1ac18c2005-06-28 17:17:12 +0000553static int
paul718e3742002-12-13 20:15:29 +0000554nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
555 struct route_node *top)
556{
557 struct prefix_ipv6 p;
558 struct route_table *table;
559 struct route_node *rn;
560 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000561 int resolved;
paul718e3742002-12-13 20:15:29 +0000562 struct nexthop *newhop;
Christian Frankefa713d92013-07-05 15:35:37 +0000563 struct nexthop *resolved_hop;
paul718e3742002-12-13 20:15:29 +0000564
565 if (nexthop->type == NEXTHOP_TYPE_IPV6)
566 nexthop->ifindex = 0;
567
568 if (set)
Christian Frankefa713d92013-07-05 15:35:37 +0000569 {
570 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
571 nexthops_free(nexthop->resolved);
572 nexthop->resolved = NULL;
573 }
paul718e3742002-12-13 20:15:29 +0000574
575 /* Make lookup prefix. */
576 memset (&p, 0, sizeof (struct prefix_ipv6));
577 p.family = AF_INET6;
578 p.prefixlen = IPV6_MAX_PREFIXLEN;
579 p.prefix = nexthop->gate.ipv6;
580
581 /* Lookup table. */
582 table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
583 if (! table)
584 return 0;
585
586 rn = route_node_match (table, (struct prefix *) &p);
587 while (rn)
588 {
589 route_unlock_node (rn);
590
David Warda50c1072009-12-03 15:34:39 +0300591 /* If lookup self prefix return immediately. */
paul718e3742002-12-13 20:15:29 +0000592 if (rn == top)
593 return 0;
594
595 /* Pick up selected route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000596 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100597 {
598 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
599 continue;
600 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
601 break;
602 }
paul718e3742002-12-13 20:15:29 +0000603
604 /* If there is no selected route or matched route is EGP, go up
605 tree. */
606 if (! match
607 || match->type == ZEBRA_ROUTE_BGP)
608 {
609 do {
610 rn = rn->parent;
611 } while (rn && rn->info == NULL);
612 if (rn)
613 route_lock_node (rn);
614 }
615 else
616 {
Christian Franke48a53dc2013-07-05 15:35:38 +0000617 /* If the longest prefix match for the nexthop yields
618 * a blackhole, mark it as inactive. */
619 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
620 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
621 return 0;
622
paul718e3742002-12-13 20:15:29 +0000623 if (match->type == ZEBRA_ROUTE_CONNECT)
624 {
625 /* Directly point connected route. */
626 newhop = match->nexthop;
627
628 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
629 nexthop->ifindex = newhop->ifindex;
630
631 return 1;
632 }
633 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
634 {
Christian Frankefa713d92013-07-05 15:35:37 +0000635 resolved = 0;
paul718e3742002-12-13 20:15:29 +0000636 for (newhop = match->nexthop; newhop; newhop = newhop->next)
637 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
638 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
639 {
640 if (set)
641 {
642 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
Christian Frankefa713d92013-07-05 15:35:37 +0000643
644 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
645 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
Christian Frankec3e6b592013-07-05 15:35:40 +0000646 /* See nexthop_active_ipv4 for a description how the
647 * resolved nexthop is constructed. */
paul718e3742002-12-13 20:15:29 +0000648 if (newhop->type == NEXTHOP_TYPE_IPV6
649 || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
650 || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME)
Christian Frankec3e6b592013-07-05 15:35:40 +0000651 {
652 resolved_hop->type = newhop->type;
653 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
654
655 if (newhop->ifindex)
656 {
657 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
658 resolved_hop->ifindex = newhop->ifindex;
659 }
660 }
Christian Frankefa713d92013-07-05 15:35:37 +0000661
paul718e3742002-12-13 20:15:29 +0000662 if (newhop->type == NEXTHOP_TYPE_IFINDEX
Christian Frankec3e6b592013-07-05 15:35:40 +0000663 || newhop->type == NEXTHOP_TYPE_IFNAME)
664 {
665 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
666 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
667 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
668 resolved_hop->ifindex = newhop->ifindex;
669 }
Christian Frankefa713d92013-07-05 15:35:37 +0000670
671 _nexthop_add(&nexthop->resolved, resolved_hop);
paul718e3742002-12-13 20:15:29 +0000672 }
Christian Frankefa713d92013-07-05 15:35:37 +0000673 resolved = 1;
paul718e3742002-12-13 20:15:29 +0000674 }
Christian Frankefa713d92013-07-05 15:35:37 +0000675 return resolved;
paul718e3742002-12-13 20:15:29 +0000676 }
677 else
678 {
679 return 0;
680 }
681 }
682 }
683 return 0;
684}
685#endif /* HAVE_IPV6 */
686
687struct rib *
688rib_match_ipv4 (struct in_addr addr)
689{
690 struct prefix_ipv4 p;
691 struct route_table *table;
692 struct route_node *rn;
693 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000694 struct nexthop *newhop, *tnewhop;
695 int recursing;
paul718e3742002-12-13 20:15:29 +0000696
697 /* Lookup table. */
698 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
699 if (! table)
700 return 0;
701
702 memset (&p, 0, sizeof (struct prefix_ipv4));
703 p.family = AF_INET;
704 p.prefixlen = IPV4_MAX_PREFIXLEN;
705 p.prefix = addr;
706
707 rn = route_node_match (table, (struct prefix *) &p);
708
709 while (rn)
710 {
711 route_unlock_node (rn);
712
713 /* Pick up selected route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000714 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100715 {
716 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
717 continue;
718 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
719 break;
720 }
paul718e3742002-12-13 20:15:29 +0000721
722 /* If there is no selected route or matched route is EGP, go up
723 tree. */
724 if (! match
725 || match->type == ZEBRA_ROUTE_BGP)
726 {
727 do {
728 rn = rn->parent;
729 } while (rn && rn->info == NULL);
730 if (rn)
731 route_lock_node (rn);
732 }
733 else
734 {
735 if (match->type == ZEBRA_ROUTE_CONNECT)
736 /* Directly point connected route. */
737 return match;
738 else
739 {
Christian Frankefa713d92013-07-05 15:35:37 +0000740 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
paul718e3742002-12-13 20:15:29 +0000741 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
742 return match;
743 return NULL;
744 }
745 }
746 }
747 return NULL;
748}
749
750struct rib *
751rib_lookup_ipv4 (struct prefix_ipv4 *p)
752{
753 struct route_table *table;
754 struct route_node *rn;
755 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000756 struct nexthop *nexthop, *tnexthop;
757 int recursing;
paul718e3742002-12-13 20:15:29 +0000758
759 /* Lookup table. */
760 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
761 if (! table)
762 return 0;
763
764 rn = route_node_lookup (table, (struct prefix *) p);
765
766 /* No route for this prefix. */
767 if (! rn)
768 return NULL;
769
770 /* Unlock node. */
771 route_unlock_node (rn);
772
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000773 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100774 {
775 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
776 continue;
777 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
778 break;
779 }
paul718e3742002-12-13 20:15:29 +0000780
781 if (! match || match->type == ZEBRA_ROUTE_BGP)
782 return NULL;
783
784 if (match->type == ZEBRA_ROUTE_CONNECT)
785 return match;
786
Christian Frankefa713d92013-07-05 15:35:37 +0000787 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +0000788 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
789 return match;
790
791 return NULL;
792}
793
Denis Ovsienkodc958242007-08-13 16:03:06 +0000794/*
795 * This clone function, unlike its original rib_lookup_ipv4(), checks
796 * if specified IPv4 route record (prefix/mask -> gate) exists in
797 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
798 *
799 * Return values:
800 * -1: error
801 * 0: exact match found
802 * 1: a match was found with a different gate
803 * 2: connected route found
804 * 3: no matches found
805 */
806int
807rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate)
808{
809 struct route_table *table;
810 struct route_node *rn;
811 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000812 struct nexthop *nexthop, *tnexthop;
813 int recursing;
814 int nexthops_active;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000815
816 /* Lookup table. */
817 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
818 if (! table)
819 return ZEBRA_RIB_LOOKUP_ERROR;
820
821 /* Scan the RIB table for exactly matching RIB entry. */
822 rn = route_node_lookup (table, (struct prefix *) p);
823
824 /* No route for this prefix. */
825 if (! rn)
826 return ZEBRA_RIB_NOTFOUND;
827
828 /* Unlock node. */
829 route_unlock_node (rn);
830
831 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000832 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100833 {
834 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
835 continue;
836 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
837 break;
838 }
Denis Ovsienkodc958242007-08-13 16:03:06 +0000839
840 /* None such found :( */
841 if (!match)
842 return ZEBRA_RIB_NOTFOUND;
843
844 if (match->type == ZEBRA_ROUTE_CONNECT)
845 return ZEBRA_RIB_FOUND_CONNECTED;
846
847 /* Ok, we have a cood candidate, let's check it's nexthop list... */
Christian Frankefa713d92013-07-05 15:35:37 +0000848 nexthops_active = 0;
849 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
Denis Ovsienkodc958242007-08-13 16:03:06 +0000850 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
Denis Ovsienkodc958242007-08-13 16:03:06 +0000851 {
Christian Frankefa713d92013-07-05 15:35:37 +0000852 nexthops_active = 1;
853 if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate))
854 return ZEBRA_RIB_FOUND_EXACT;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000855 if (IS_ZEBRA_DEBUG_RIB)
Christian Frankefa713d92013-07-05 15:35:37 +0000856 {
857 char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
858 inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
859 inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN);
860 zlog_debug ("%s: qgate == %s, %s == %s", __func__,
861 qgate_buf, recursing ? "rgate" : "gate", gate_buf);
862 }
Denis Ovsienkodc958242007-08-13 16:03:06 +0000863 }
Christian Frankefa713d92013-07-05 15:35:37 +0000864
865 if (nexthops_active)
866 return ZEBRA_RIB_FOUND_NOGATE;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000867
868 return ZEBRA_RIB_NOTFOUND;
869}
870
paul718e3742002-12-13 20:15:29 +0000871#ifdef HAVE_IPV6
872struct rib *
873rib_match_ipv6 (struct in6_addr *addr)
874{
875 struct prefix_ipv6 p;
876 struct route_table *table;
877 struct route_node *rn;
878 struct rib *match;
Christian Frankefa713d92013-07-05 15:35:37 +0000879 struct nexthop *newhop, *tnewhop;
880 int recursing;
paul718e3742002-12-13 20:15:29 +0000881
882 /* Lookup table. */
883 table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
884 if (! table)
885 return 0;
886
887 memset (&p, 0, sizeof (struct prefix_ipv6));
888 p.family = AF_INET6;
889 p.prefixlen = IPV6_MAX_PREFIXLEN;
890 IPV6_ADDR_COPY (&p.prefix, addr);
891
892 rn = route_node_match (table, (struct prefix *) &p);
893
894 while (rn)
895 {
896 route_unlock_node (rn);
897
898 /* Pick up selected route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000899 RNODE_FOREACH_RIB (rn, match)
Stephen Hemminger16814f92008-08-17 17:39:31 +0100900 {
901 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
902 continue;
903 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
904 break;
905 }
paul718e3742002-12-13 20:15:29 +0000906
907 /* If there is no selected route or matched route is EGP, go up
908 tree. */
909 if (! match
910 || match->type == ZEBRA_ROUTE_BGP)
911 {
912 do {
913 rn = rn->parent;
914 } while (rn && rn->info == NULL);
915 if (rn)
916 route_lock_node (rn);
917 }
918 else
919 {
920 if (match->type == ZEBRA_ROUTE_CONNECT)
921 /* Directly point connected route. */
922 return match;
923 else
924 {
Christian Frankefa713d92013-07-05 15:35:37 +0000925 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
paul718e3742002-12-13 20:15:29 +0000926 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
927 return match;
928 return NULL;
929 }
930 }
931 }
932 return NULL;
933}
934#endif /* HAVE_IPV6 */
935
Paul Jakma7514fb72007-05-02 16:05:35 +0000936#define RIB_SYSTEM_ROUTE(R) \
937 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
938
Denis Ovsienkodc958242007-08-13 16:03:06 +0000939/* This function verifies reachability of one given nexthop, which can be
940 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
941 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
942 * nexthop->ifindex will be updated appropriately as well.
943 * An existing route map can turn (otherwise active) nexthop into inactive, but
944 * not vice versa.
945 *
946 * The return value is the final value of 'ACTIVE' flag.
947 */
948
Stephen Hemmingerd02c56c2009-12-08 13:14:27 +0300949static unsigned
paul718e3742002-12-13 20:15:29 +0000950nexthop_active_check (struct route_node *rn, struct rib *rib,
951 struct nexthop *nexthop, int set)
952{
Christian Frankef3a17322013-07-05 15:35:41 +0000953 rib_table_info_t *info = rn->table->info;
paul718e3742002-12-13 20:15:29 +0000954 struct interface *ifp;
Paul Jakma7514fb72007-05-02 16:05:35 +0000955 route_map_result_t ret = RMAP_MATCH;
956 extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1];
957 struct route_map *rmap;
958 int family;
paul718e3742002-12-13 20:15:29 +0000959
Paul Jakma7514fb72007-05-02 16:05:35 +0000960 family = 0;
paul718e3742002-12-13 20:15:29 +0000961 switch (nexthop->type)
962 {
963 case NEXTHOP_TYPE_IFINDEX:
964 ifp = if_lookup_by_index (nexthop->ifindex);
Andrew J. Schorr3f087672008-01-08 20:12:46 +0000965 if (ifp && if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +0000966 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
967 else
968 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
969 break;
paul718e3742002-12-13 20:15:29 +0000970 case NEXTHOP_TYPE_IPV6_IFNAME:
Paul Jakma7514fb72007-05-02 16:05:35 +0000971 family = AFI_IP6;
972 case NEXTHOP_TYPE_IFNAME:
paul718e3742002-12-13 20:15:29 +0000973 ifp = if_lookup_by_name (nexthop->ifname);
Andrew J. Schorr3f087672008-01-08 20:12:46 +0000974 if (ifp && if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +0000975 {
976 if (set)
977 nexthop->ifindex = ifp->ifindex;
978 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
979 }
980 else
981 {
982 if (set)
983 nexthop->ifindex = 0;
984 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
985 }
986 break;
987 case NEXTHOP_TYPE_IPV4:
988 case NEXTHOP_TYPE_IPV4_IFINDEX:
Paul Jakma7514fb72007-05-02 16:05:35 +0000989 family = AFI_IP;
paul718e3742002-12-13 20:15:29 +0000990 if (nexthop_active_ipv4 (rib, nexthop, set, rn))
991 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
992 else
993 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
994 break;
995#ifdef HAVE_IPV6
996 case NEXTHOP_TYPE_IPV6:
Paul Jakma7514fb72007-05-02 16:05:35 +0000997 family = AFI_IP6;
paul718e3742002-12-13 20:15:29 +0000998 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
999 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1000 else
1001 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1002 break;
1003 case NEXTHOP_TYPE_IPV6_IFINDEX:
Paul Jakma7514fb72007-05-02 16:05:35 +00001004 family = AFI_IP6;
paul718e3742002-12-13 20:15:29 +00001005 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
1006 {
1007 ifp = if_lookup_by_index (nexthop->ifindex);
Andrew J. Schorr3f087672008-01-08 20:12:46 +00001008 if (ifp && if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +00001009 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1010 else
1011 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1012 }
1013 else
1014 {
1015 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
1016 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1017 else
1018 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1019 }
1020 break;
1021#endif /* HAVE_IPV6 */
paul595db7f2003-05-25 21:35:06 +00001022 case NEXTHOP_TYPE_BLACKHOLE:
1023 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1024 break;
paul718e3742002-12-13 20:15:29 +00001025 default:
1026 break;
1027 }
Paul Jakma7514fb72007-05-02 16:05:35 +00001028 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1029 return 0;
1030
Christian Frankef3a17322013-07-05 15:35:41 +00001031 /* XXX: What exactly do those checks do? Do we support
1032 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
Paul Jakma7514fb72007-05-02 16:05:35 +00001033 if (RIB_SYSTEM_ROUTE(rib) ||
1034 (family == AFI_IP && rn->p.family != AF_INET) ||
1035 (family == AFI_IP6 && rn->p.family != AF_INET6))
1036 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1037
Christian Frankef3a17322013-07-05 15:35:41 +00001038 /* The original code didn't determine the family correctly
1039 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1040 * from the rib_table_info in those cases.
1041 * Possibly it may be better to use only the rib_table_info
1042 * in every case.
1043 */
1044 if (!family)
1045 family = info->afi;
1046
Paul Jakma7514fb72007-05-02 16:05:35 +00001047 rmap = 0;
1048 if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX &&
1049 proto_rm[family][rib->type])
1050 rmap = route_map_lookup_by_name (proto_rm[family][rib->type]);
1051 if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX])
1052 rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]);
1053 if (rmap) {
1054 ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop);
1055 }
1056
1057 if (ret == RMAP_DENYMATCH)
1058 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
paul718e3742002-12-13 20:15:29 +00001059 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1060}
1061
Denis Ovsienko03e232a2007-08-14 09:46:48 +00001062/* Iterate over all nexthops of the given RIB entry and refresh their
1063 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1064 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1065 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
1066 * transparently passed to nexthop_active_check().
1067 *
1068 * Return value is the new number of active nexthops.
1069 */
1070
paula1ac18c2005-06-28 17:17:12 +00001071static int
paul718e3742002-12-13 20:15:29 +00001072nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
1073{
1074 struct nexthop *nexthop;
Stephen Hemmingerd02c56c2009-12-08 13:14:27 +03001075 unsigned int prev_active, prev_index, new_active;
paul718e3742002-12-13 20:15:29 +00001076
1077 rib->nexthop_active_num = 0;
1078 UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1079
1080 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Denis Ovsienko03e232a2007-08-14 09:46:48 +00001081 {
1082 prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
Joakim Tjernlundc3a56062009-06-24 19:15:36 +02001083 prev_index = nexthop->ifindex;
Denis Ovsienko03e232a2007-08-14 09:46:48 +00001084 if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
1085 rib->nexthop_active_num++;
Joakim Tjernlundc3a56062009-06-24 19:15:36 +02001086 if (prev_active != new_active ||
1087 prev_index != nexthop->ifindex)
Denis Ovsienko03e232a2007-08-14 09:46:48 +00001088 SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
1089 }
paul718e3742002-12-13 20:15:29 +00001090 return rib->nexthop_active_num;
1091}
paul6baeb982003-10-28 03:47:15 +00001092
paul718e3742002-12-13 20:15:29 +00001093
paul718e3742002-12-13 20:15:29 +00001094
paula1ac18c2005-06-28 17:17:12 +00001095static void
paul718e3742002-12-13 20:15:29 +00001096rib_install_kernel (struct route_node *rn, struct rib *rib)
1097{
1098 int ret = 0;
Christian Frankefa713d92013-07-05 15:35:37 +00001099 struct nexthop *nexthop, *tnexthop;
1100 int recursing;
paul718e3742002-12-13 20:15:29 +00001101
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001102 /*
1103 * Make sure we update the FPM any time we send new information to
1104 * the kernel.
1105 */
1106 zfpm_trigger_update (rn, "installing in kernel");
paul718e3742002-12-13 20:15:29 +00001107 switch (PREFIX_FAMILY (&rn->p))
1108 {
1109 case AF_INET:
1110 ret = kernel_add_ipv4 (&rn->p, rib);
1111 break;
1112#ifdef HAVE_IPV6
1113 case AF_INET6:
1114 ret = kernel_add_ipv6 (&rn->p, rib);
1115 break;
1116#endif /* HAVE_IPV6 */
1117 }
1118
Denis Ovsienkodc958242007-08-13 16:03:06 +00001119 /* This condition is never met, if we are using rt_socket.c */
paul718e3742002-12-13 20:15:29 +00001120 if (ret < 0)
1121 {
Christian Frankefa713d92013-07-05 15:35:37 +00001122 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +00001123 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1124 }
1125}
1126
1127/* Uninstall the route from kernel. */
paula1ac18c2005-06-28 17:17:12 +00001128static int
paul718e3742002-12-13 20:15:29 +00001129rib_uninstall_kernel (struct route_node *rn, struct rib *rib)
1130{
1131 int ret = 0;
Christian Frankefa713d92013-07-05 15:35:37 +00001132 struct nexthop *nexthop, *tnexthop;
1133 int recursing;
paul718e3742002-12-13 20:15:29 +00001134
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001135 /*
1136 * Make sure we update the FPM any time we send new information to
1137 * the kernel.
1138 */
1139 zfpm_trigger_update (rn, "uninstalling from kernel");
1140
paul718e3742002-12-13 20:15:29 +00001141 switch (PREFIX_FAMILY (&rn->p))
1142 {
1143 case AF_INET:
1144 ret = kernel_delete_ipv4 (&rn->p, rib);
1145 break;
1146#ifdef HAVE_IPV6
1147 case AF_INET6:
1148 ret = kernel_delete_ipv6 (&rn->p, rib);
1149 break;
1150#endif /* HAVE_IPV6 */
1151 }
1152
Christian Frankefa713d92013-07-05 15:35:37 +00001153 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +00001154 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1155
1156 return ret;
1157}
1158
1159/* Uninstall the route from kernel. */
paula1ac18c2005-06-28 17:17:12 +00001160static void
paul718e3742002-12-13 20:15:29 +00001161rib_uninstall (struct route_node *rn, struct rib *rib)
1162{
1163 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
1164 {
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001165 zfpm_trigger_update (rn, "rib_uninstall");
1166
paul718e3742002-12-13 20:15:29 +00001167 redistribute_delete (&rn->p, rib);
1168 if (! RIB_SYSTEM_ROUTE (rib))
1169 rib_uninstall_kernel (rn, rib);
1170 UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED);
1171 }
1172}
1173
Paul Jakma6d691122006-07-27 21:49:00 +00001174static void rib_unlink (struct route_node *, struct rib *);
1175
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001176/*
1177 * rib_can_delete_dest
1178 *
1179 * Returns TRUE if the given dest can be deleted from the table.
1180 */
1181static int
1182rib_can_delete_dest (rib_dest_t *dest)
1183{
1184 if (dest->routes)
1185 {
1186 return 0;
1187 }
1188
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001189 /*
1190 * Don't delete the dest if we have to update the FPM about this
1191 * prefix.
1192 */
1193 if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) ||
1194 CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
1195 return 0;
1196
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001197 return 1;
1198}
1199
1200/*
1201 * rib_gc_dest
1202 *
1203 * Garbage collect the rib dest corresponding to the given route node
1204 * if appropriate.
1205 *
1206 * Returns TRUE if the dest was deleted, FALSE otherwise.
1207 */
1208int
1209rib_gc_dest (struct route_node *rn)
1210{
1211 rib_dest_t *dest;
1212 char buf[INET6_ADDRSTRLEN];
1213
1214 dest = rib_dest_from_rnode (rn);
1215 if (!dest)
1216 return 0;
1217
1218 if (!rib_can_delete_dest (dest))
1219 return 0;
1220
1221 if (IS_ZEBRA_DEBUG_RIB)
1222 {
1223 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf));
1224 zlog_debug ("%s: %s/%d: removing dest from table", __func__,
1225 buf, rn->p.prefixlen);
1226 }
1227
1228 dest->rnode = NULL;
1229 XFREE (MTYPE_RIB_DEST, dest);
1230 rn->info = NULL;
1231
1232 /*
1233 * Release the one reference that we keep on the route node.
1234 */
1235 route_unlock_node (rn);
1236 return 1;
1237}
1238
paul718e3742002-12-13 20:15:29 +00001239/* Core function for processing routing information base. */
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001240static void
1241rib_process (struct route_node *rn)
paul718e3742002-12-13 20:15:29 +00001242{
1243 struct rib *rib;
1244 struct rib *next;
1245 struct rib *fib = NULL;
1246 struct rib *select = NULL;
Paul Jakma6d691122006-07-27 21:49:00 +00001247 struct rib *del = NULL;
pauld753e9e2003-01-22 19:45:50 +00001248 int installed = 0;
Christian Frankefa713d92013-07-05 15:35:37 +00001249 struct nexthop *nexthop = NULL, *tnexthop;
1250 int recursing;
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001251 char buf[INET6_ADDRSTRLEN];
paul4d38fdb2005-04-28 17:35:14 +00001252
1253 assert (rn);
1254
Paul Jakma93bdada2007-08-06 19:25:11 +00001255 if (IS_ZEBRA_DEBUG_RIB || IS_ZEBRA_DEBUG_RIB_Q)
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001256 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Paul Jakma93bdada2007-08-06 19:25:11 +00001257
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001258 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
paul718e3742002-12-13 20:15:29 +00001259 {
paul718e3742002-12-13 20:15:29 +00001260 /* Currently installed rib. */
1261 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
Paul Jakma6d691122006-07-27 21:49:00 +00001262 {
1263 assert (fib == NULL);
1264 fib = rib;
1265 }
1266
1267 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1268 * which we need to do do further work with below.
1269 */
1270 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1271 {
1272 if (rib != fib)
1273 {
1274 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001275 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__,
1276 buf, rn->p.prefixlen, rn, rib);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001277 rib_unlink (rn, rib);
Paul Jakma6d691122006-07-27 21:49:00 +00001278 }
1279 else
1280 del = rib;
1281
1282 continue;
1283 }
paul4d38fdb2005-04-28 17:35:14 +00001284
paul718e3742002-12-13 20:15:29 +00001285 /* Skip unreachable nexthop. */
1286 if (! nexthop_active_update (rn, rib, 0))
paul7021c422003-07-15 12:52:22 +00001287 continue;
paul718e3742002-12-13 20:15:29 +00001288
1289 /* Infinit distance. */
1290 if (rib->distance == DISTANCE_INFINITY)
paul7021c422003-07-15 12:52:22 +00001291 continue;
paul718e3742002-12-13 20:15:29 +00001292
paulaf887b52006-01-18 14:52:52 +00001293 /* Newly selected rib, the common case. */
1294 if (!select)
1295 {
1296 select = rib;
1297 continue;
1298 }
1299
1300 /* filter route selection in following order:
paulaf887b52006-01-18 14:52:52 +00001301 * - connected beats other types
paula8d9c1f2006-01-25 06:31:04 +00001302 * - lower distance beats higher
paulaf887b52006-01-18 14:52:52 +00001303 * - lower metric beats higher for equal distance
1304 * - last, hence oldest, route wins tie break.
1305 */
paula1038a12006-01-30 14:08:51 +00001306
1307 /* Connected routes. Pick the last connected
1308 * route of the set of lowest metric connected routes.
1309 */
paula8d9c1f2006-01-25 06:31:04 +00001310 if (rib->type == ZEBRA_ROUTE_CONNECT)
1311 {
paula1038a12006-01-30 14:08:51 +00001312 if (select->type != ZEBRA_ROUTE_CONNECT
paula8d9c1f2006-01-25 06:31:04 +00001313 || rib->metric <= select->metric)
paula1038a12006-01-30 14:08:51 +00001314 select = rib;
1315 continue;
paula8d9c1f2006-01-25 06:31:04 +00001316 }
1317 else if (select->type == ZEBRA_ROUTE_CONNECT)
1318 continue;
1319
1320 /* higher distance loses */
1321 if (rib->distance > select->distance)
1322 continue;
1323
1324 /* lower wins */
1325 if (rib->distance < select->distance)
1326 {
paulaf887b52006-01-18 14:52:52 +00001327 select = rib;
paula8d9c1f2006-01-25 06:31:04 +00001328 continue;
1329 }
1330
1331 /* metric tie-breaks equal distance */
1332 if (rib->metric <= select->metric)
1333 select = rib;
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001334 } /* RNODE_FOREACH_RIB_SAFE */
Denis Ovsienkodc958242007-08-13 16:03:06 +00001335
1336 /* After the cycle is finished, the following pointers will be set:
1337 * select --- the winner RIB entry, if any was found, otherwise NULL
1338 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1339 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1340 * rib --- NULL
1341 */
1342
1343 /* Same RIB entry is selected. Update FIB and finish. */
paul718e3742002-12-13 20:15:29 +00001344 if (select && select == fib)
1345 {
Paul Jakma6d691122006-07-27 21:49:00 +00001346 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001347 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1348 __func__, buf, rn->p.prefixlen, select, fib);
paul718e3742002-12-13 20:15:29 +00001349 if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
paul4d38fdb2005-04-28 17:35:14 +00001350 {
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001351 zfpm_trigger_update (rn, "updating existing route");
1352
paul4d38fdb2005-04-28 17:35:14 +00001353 redistribute_delete (&rn->p, select);
1354 if (! RIB_SYSTEM_ROUTE (select))
1355 rib_uninstall_kernel (rn, select);
paul718e3742002-12-13 20:15:29 +00001356
paul4d38fdb2005-04-28 17:35:14 +00001357 /* Set real nexthop. */
1358 nexthop_active_update (rn, select, 1);
paul718e3742002-12-13 20:15:29 +00001359
paul4d38fdb2005-04-28 17:35:14 +00001360 if (! RIB_SYSTEM_ROUTE (select))
1361 rib_install_kernel (rn, select);
1362 redistribute_add (&rn->p, select);
1363 }
pauld753e9e2003-01-22 19:45:50 +00001364 else if (! RIB_SYSTEM_ROUTE (select))
paul4d38fdb2005-04-28 17:35:14 +00001365 {
1366 /* Housekeeping code to deal with
1367 race conditions in kernel with linux
1368 netlink reporting interface up before IPv4 or IPv6 protocol
1369 is ready to add routes.
1370 This makes sure the routes are IN the kernel.
1371 */
pauld753e9e2003-01-22 19:45:50 +00001372
Christian Frankefa713d92013-07-05 15:35:37 +00001373 for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing))
Denis Ovsienkoa3aaf5b2007-10-04 10:49:21 +00001374 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
paul4d38fdb2005-04-28 17:35:14 +00001375 {
Denis Ovsienkoa3aaf5b2007-10-04 10:49:21 +00001376 installed = 1;
1377 break;
paul4d38fdb2005-04-28 17:35:14 +00001378 }
1379 if (! installed)
1380 rib_install_kernel (rn, select);
1381 }
Paul Jakma6d691122006-07-27 21:49:00 +00001382 goto end;
paul718e3742002-12-13 20:15:29 +00001383 }
1384
Denis Ovsienkodc958242007-08-13 16:03:06 +00001385 /* At this point we either haven't found the best RIB entry or it is
1386 * different from what we currently intend to flag with SELECTED. In both
1387 * cases, if a RIB block is present in FIB, it should be withdrawn.
1388 */
paul718e3742002-12-13 20:15:29 +00001389 if (fib)
1390 {
Paul Jakma6d691122006-07-27 21:49:00 +00001391 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001392 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__,
1393 buf, rn->p.prefixlen, fib);
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001394
1395 zfpm_trigger_update (rn, "removing existing route");
1396
paul718e3742002-12-13 20:15:29 +00001397 redistribute_delete (&rn->p, fib);
1398 if (! RIB_SYSTEM_ROUTE (fib))
1399 rib_uninstall_kernel (rn, fib);
1400 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
1401
1402 /* Set real nexthop. */
1403 nexthop_active_update (rn, fib, 1);
1404 }
1405
Denis Ovsienkodc958242007-08-13 16:03:06 +00001406 /* Regardless of some RIB entry being SELECTED or not before, now we can
1407 * tell, that if a new winner exists, FIB is still not updated with this
1408 * data, but ready to be.
1409 */
paul718e3742002-12-13 20:15:29 +00001410 if (select)
1411 {
Paul Jakma6d691122006-07-27 21:49:00 +00001412 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001413 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__, buf,
1414 rn->p.prefixlen, select);
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001415
1416 zfpm_trigger_update (rn, "new route selected");
1417
paul718e3742002-12-13 20:15:29 +00001418 /* Set real nexthop. */
1419 nexthop_active_update (rn, select, 1);
1420
1421 if (! RIB_SYSTEM_ROUTE (select))
paul4d38fdb2005-04-28 17:35:14 +00001422 rib_install_kernel (rn, select);
paul718e3742002-12-13 20:15:29 +00001423 SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED);
1424 redistribute_add (&rn->p, select);
1425 }
paul4d38fdb2005-04-28 17:35:14 +00001426
Paul Jakma6d691122006-07-27 21:49:00 +00001427 /* FIB route was removed, should be deleted */
1428 if (del)
1429 {
1430 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001431 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__, buf,
1432 rn->p.prefixlen, del, rn);
Paul Jakma6d691122006-07-27 21:49:00 +00001433 rib_unlink (rn, del);
1434 }
paul4d38fdb2005-04-28 17:35:14 +00001435
Paul Jakma6d691122006-07-27 21:49:00 +00001436end:
1437 if (IS_ZEBRA_DEBUG_RIB_Q)
Paul Jakma93bdada2007-08-06 19:25:11 +00001438 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__, buf, rn->p.prefixlen, rn);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001439
1440 /*
1441 * Check if the dest can be deleted now.
1442 */
1443 rib_gc_dest (rn);
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001444}
1445
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001446/* Take a list of route_node structs and return 1, if there was a record
1447 * picked from it and processed by rib_process(). Don't process more,
1448 * than one RN record; operate only in the specified sub-queue.
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001449 */
Stephen Hemmingeref9b1132008-08-17 17:44:47 +01001450static unsigned int
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001451process_subq (struct list * subq, u_char qindex)
1452{
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001453 struct listnode *lnode = listhead (subq);
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001454 struct route_node *rnode;
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001455
1456 if (!lnode)
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001457 return 0;
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001458
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001459 rnode = listgetdata (lnode);
1460 rib_process (rnode);
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001461
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001462 if (rnode->info)
1463 UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex));
1464
Chris Caputo67b94672009-07-18 04:02:26 +00001465#if 0
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001466 else
1467 {
1468 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1469 __func__, rnode, rnode->lock);
1470 zlog_backtrace(LOG_DEBUG);
1471 }
Chris Caputo67b94672009-07-18 04:02:26 +00001472#endif
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001473 route_unlock_node (rnode);
1474 list_delete_node (subq, lnode);
1475 return 1;
1476}
1477
1478/* Dispatch the meta queue by picking, processing and unlocking the next RN from
1479 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1480 * is pointed to the meta queue structure.
1481 */
1482static wq_item_status
1483meta_queue_process (struct work_queue *dummy, void *data)
1484{
1485 struct meta_queue * mq = data;
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001486 unsigned i;
1487
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001488 for (i = 0; i < MQ_SIZE; i++)
1489 if (process_subq (mq->subq[i], i))
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001490 {
1491 mq->size--;
1492 break;
1493 }
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001494 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
1495}
1496
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001497/*
1498 * Map from rib types to queue type (priority) in meta queue
1499 */
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001500static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = {
1501 [ZEBRA_ROUTE_SYSTEM] = 4,
1502 [ZEBRA_ROUTE_KERNEL] = 0,
1503 [ZEBRA_ROUTE_CONNECT] = 0,
1504 [ZEBRA_ROUTE_STATIC] = 1,
1505 [ZEBRA_ROUTE_RIP] = 2,
1506 [ZEBRA_ROUTE_RIPNG] = 2,
1507 [ZEBRA_ROUTE_OSPF] = 2,
1508 [ZEBRA_ROUTE_OSPF6] = 2,
1509 [ZEBRA_ROUTE_ISIS] = 2,
1510 [ZEBRA_ROUTE_BGP] = 3,
1511 [ZEBRA_ROUTE_HSLS] = 4,
Paul Jakma57345092011-12-25 17:52:09 +01001512 [ZEBRA_ROUTE_BABEL] = 2,
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001513};
1514
1515/* Look into the RN and queue it into one or more priority queues,
1516 * increasing the size for each data push done.
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001517 */
Stephen Hemmingeref9b1132008-08-17 17:44:47 +01001518static void
1519rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn)
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001520{
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001521 struct rib *rib;
1522 char buf[INET6_ADDRSTRLEN];
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001523
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001524 if (IS_ZEBRA_DEBUG_RIB_Q)
1525 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001526
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001527 RNODE_FOREACH_RIB (rn, rib)
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001528 {
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001529 u_char qindex = meta_queue_map[rib->type];
1530
1531 /* Invariant: at this point we always have rn->info set. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001532 if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags,
1533 RIB_ROUTE_QUEUED (qindex)))
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001534 {
1535 if (IS_ZEBRA_DEBUG_RIB_Q)
1536 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1537 __func__, buf, rn->p.prefixlen, rn, qindex);
1538 continue;
1539 }
1540
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001541 SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex));
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001542 listnode_add (mq->subq[qindex], rn);
1543 route_lock_node (rn);
1544 mq->size++;
1545
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001546 if (IS_ZEBRA_DEBUG_RIB_Q)
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001547 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1548 __func__, buf, rn->p.prefixlen, rn, qindex);
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001549 }
paul4d38fdb2005-04-28 17:35:14 +00001550}
1551
Paul Jakma6d691122006-07-27 21:49:00 +00001552/* Add route_node to work queue and schedule processing */
paula1ac18c2005-06-28 17:17:12 +00001553static void
Paul Jakma6d691122006-07-27 21:49:00 +00001554rib_queue_add (struct zebra_t *zebra, struct route_node *rn)
paul4d38fdb2005-04-28 17:35:14 +00001555{
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001556 char buf[INET_ADDRSTRLEN];
1557 assert (zebra && rn);
paul4d38fdb2005-04-28 17:35:14 +00001558
Paul Jakma93bdada2007-08-06 19:25:11 +00001559 if (IS_ZEBRA_DEBUG_RIB_Q)
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001560 inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
Stephen Hemmingercc2dd922009-12-09 17:54:49 +03001561
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001562 /* Pointless to queue a route_node with no RIB entries to add or remove */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001563 if (!rnode_to_ribs (rn))
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001564 {
1565 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1566 __func__, rn, rn->lock);
1567 zlog_backtrace(LOG_DEBUG);
1568 return;
1569 }
1570
1571 if (IS_ZEBRA_DEBUG_RIB_Q)
1572 zlog_info ("%s: %s/%d: work queue added", __func__, buf, rn->p.prefixlen);
1573
1574 assert (zebra);
1575
1576 if (zebra->ribq == NULL)
1577 {
1578 zlog_err ("%s: work_queue does not exist!", __func__);
1579 return;
Paul Jakma6d691122006-07-27 21:49:00 +00001580 }
paul4d38fdb2005-04-28 17:35:14 +00001581
Stephen Hemmingercc2dd922009-12-09 17:54:49 +03001582 /*
1583 * The RIB queue should normally be either empty or holding the only
1584 * work_queue_item element. In the latter case this element would
1585 * hold a pointer to the meta queue structure, which must be used to
1586 * actually queue the route nodes to process. So create the MQ
1587 * holder, if necessary, then push the work into it in any case.
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001588 * This semantics was introduced after 0.99.9 release.
1589 */
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001590 if (!zebra->ribq->items->count)
1591 work_queue_add (zebra->ribq, zebra->mq);
1592
1593 rib_meta_queue_add (zebra->mq, rn);
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001594
1595 if (IS_ZEBRA_DEBUG_RIB_Q)
1596 zlog_debug ("%s: %s/%d: rn %p queued", __func__, buf, rn->p.prefixlen, rn);
1597
1598 return;
paul4d38fdb2005-04-28 17:35:14 +00001599}
1600
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001601/* Create new meta queue.
1602 A destructor function doesn't seem to be necessary here.
1603 */
Stephen Hemmingeref9b1132008-08-17 17:44:47 +01001604static struct meta_queue *
1605meta_queue_new (void)
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001606{
1607 struct meta_queue *new;
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001608 unsigned i;
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001609
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001610 new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue));
1611 assert(new);
1612
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001613 for (i = 0; i < MQ_SIZE; i++)
Stephen Hemminger5110a0c2008-08-11 16:22:15 -07001614 {
1615 new->subq[i] = list_new ();
1616 assert(new->subq[i]);
1617 }
1618
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001619 return new;
1620}
1621
paul4d38fdb2005-04-28 17:35:14 +00001622/* initialise zebra rib work queue */
paula1ac18c2005-06-28 17:17:12 +00001623static void
paul4d38fdb2005-04-28 17:35:14 +00001624rib_queue_init (struct zebra_t *zebra)
1625{
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001626 assert (zebra);
1627
paul4d38fdb2005-04-28 17:35:14 +00001628 if (! (zebra->ribq = work_queue_new (zebra->master,
Paul Jakma6d691122006-07-27 21:49:00 +00001629 "route_node processing")))
paul4d38fdb2005-04-28 17:35:14 +00001630 {
Paul Jakma6d691122006-07-27 21:49:00 +00001631 zlog_err ("%s: could not initialise work queue!", __func__);
paul4d38fdb2005-04-28 17:35:14 +00001632 return;
1633 }
1634
1635 /* fill in the work queue spec */
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001636 zebra->ribq->spec.workfunc = &meta_queue_process;
paul4d38fdb2005-04-28 17:35:14 +00001637 zebra->ribq->spec.errorfunc = NULL;
paul4d38fdb2005-04-28 17:35:14 +00001638 /* XXX: TODO: These should be runtime configurable via vty */
1639 zebra->ribq->spec.max_retries = 3;
Paul Jakma457eb9a2006-07-27 19:59:58 +00001640 zebra->ribq->spec.hold = rib_process_hold_time;
paul4d38fdb2005-04-28 17:35:14 +00001641
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001642 if (!(zebra->mq = meta_queue_new ()))
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001643 {
Denis Ovsienkoe96f9202008-06-02 12:03:22 +00001644 zlog_err ("%s: could not initialise meta queue!", __func__);
Subbaiah Venkatafc328ac2012-03-27 16:35:22 -07001645 return;
1646 }
1647 return;
paul718e3742002-12-13 20:15:29 +00001648}
1649
Paul Jakma6d691122006-07-27 21:49:00 +00001650/* RIB updates are processed via a queue of pointers to route_nodes.
1651 *
1652 * The queue length is bounded by the maximal size of the routing table,
1653 * as a route_node will not be requeued, if already queued.
1654 *
Paul Jakma3c0755d2006-12-08 00:53:14 +00001655 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1656 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1657 * and then submit route_node to queue for best-path selection later.
1658 * Order of add/delete state changes are preserved for any given RIB.
Paul Jakma6d691122006-07-27 21:49:00 +00001659 *
1660 * Deleted RIBs are reaped during best-path selection.
1661 *
1662 * rib_addnode
1663 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
Paul Jakma3c0755d2006-12-08 00:53:14 +00001664 * |-------->| | best RIB, if required
1665 * | |
1666 * static_install->|->rib_addqueue...... -> rib_process
1667 * | |
1668 * |-------->| |-> rib_unlink
Paul Jakma6d691122006-07-27 21:49:00 +00001669 * |-> set RIB_ENTRY_REMOVE |
1670 * rib_delnode (RIB freed)
1671 *
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001672 * The 'info' pointer of a route_node points to a rib_dest_t
1673 * ('dest'). Queueing state for a route_node is kept on the dest. The
1674 * dest is created on-demand by rib_link() and is kept around at least
1675 * as long as there are ribs hanging off it (@see rib_gc_dest()).
Paul Jakma6d691122006-07-27 21:49:00 +00001676 *
1677 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1678 *
1679 * - route_nodes: refcounted by:
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001680 * - dest attached to route_node:
1681 * - managed by: rib_link/rib_gc_dest
Paul Jakma6d691122006-07-27 21:49:00 +00001682 * - route_node processing queue
1683 * - managed by: rib_addqueue, rib_process.
1684 *
1685 */
1686
paul718e3742002-12-13 20:15:29 +00001687/* Add RIB to head of the route node. */
paula1ac18c2005-06-28 17:17:12 +00001688static void
Paul Jakma6d691122006-07-27 21:49:00 +00001689rib_link (struct route_node *rn, struct rib *rib)
paul718e3742002-12-13 20:15:29 +00001690{
1691 struct rib *head;
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001692 rib_dest_t *dest;
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001693 char buf[INET6_ADDRSTRLEN];
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001694
paul4d38fdb2005-04-28 17:35:14 +00001695 assert (rib && rn);
1696
Paul Jakma6d691122006-07-27 21:49:00 +00001697 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001698 {
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001699 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Paul Jakma93bdada2007-08-06 19:25:11 +00001700 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__,
1701 buf, rn->p.prefixlen, rn, rib);
1702 }
Paul Jakma6d691122006-07-27 21:49:00 +00001703
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001704 dest = rib_dest_from_rnode (rn);
1705 if (!dest)
Paul Jakma6d691122006-07-27 21:49:00 +00001706 {
1707 if (IS_ZEBRA_DEBUG_RIB)
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001708 {
1709 zlog_debug ("%s: %s/%d: adding dest to table", __func__,
1710 buf, rn->p.prefixlen);
1711 }
1712
1713 dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t));
1714 route_lock_node (rn); /* rn route table reference */
1715 rn->info = dest;
1716 dest->rnode = rn;
1717 }
1718
1719 head = dest->routes;
1720 if (head)
1721 {
Paul Jakma6d691122006-07-27 21:49:00 +00001722 head->prev = rib;
Paul Jakma6d691122006-07-27 21:49:00 +00001723 }
paul718e3742002-12-13 20:15:29 +00001724 rib->next = head;
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001725 dest->routes = rib;
Paul Jakma6d691122006-07-27 21:49:00 +00001726 rib_queue_add (&zebrad, rn);
1727}
1728
1729static void
1730rib_addnode (struct route_node *rn, struct rib *rib)
1731{
1732 /* RIB node has been un-removed before route-node is processed.
1733 * route_node must hence already be on the queue for processing..
1734 */
1735 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1736 {
1737 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001738 {
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001739 char buf[INET6_ADDRSTRLEN];
1740 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Paul Jakma93bdada2007-08-06 19:25:11 +00001741 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1742 __func__, buf, rn->p.prefixlen, rn, rib);
1743 }
Paul Jakma6d691122006-07-27 21:49:00 +00001744 UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1745 return;
1746 }
1747 rib_link (rn, rib);
1748}
1749
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001750/*
1751 * rib_unlink
1752 *
1753 * Detach a rib structure from a route_node.
1754 *
1755 * Note that a call to rib_unlink() should be followed by a call to
1756 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
1757 * longer required to be deleted.
1758 */
Paul Jakma6d691122006-07-27 21:49:00 +00001759static void
1760rib_unlink (struct route_node *rn, struct rib *rib)
1761{
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001762 char buf[INET6_ADDRSTRLEN];
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001763 rib_dest_t *dest;
Paul Jakma6d691122006-07-27 21:49:00 +00001764
1765 assert (rn && rib);
1766
1767 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001768 {
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001769 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Paul Jakma93bdada2007-08-06 19:25:11 +00001770 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1771 __func__, buf, rn->p.prefixlen, rn, rib);
1772 }
Paul Jakma6d691122006-07-27 21:49:00 +00001773
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001774 dest = rib_dest_from_rnode (rn);
1775
Paul Jakma6d691122006-07-27 21:49:00 +00001776 if (rib->next)
1777 rib->next->prev = rib->prev;
1778
1779 if (rib->prev)
1780 rib->prev->next = rib->next;
1781 else
1782 {
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001783 dest->routes = rib->next;
Paul Jakma6d691122006-07-27 21:49:00 +00001784 }
1785
1786 /* free RIB and nexthops */
Christian Frankefa713d92013-07-05 15:35:37 +00001787 nexthops_free(rib->nexthop);
Paul Jakma6d691122006-07-27 21:49:00 +00001788 XFREE (MTYPE_RIB, rib);
1789
paul718e3742002-12-13 20:15:29 +00001790}
1791
paula1ac18c2005-06-28 17:17:12 +00001792static void
paul718e3742002-12-13 20:15:29 +00001793rib_delnode (struct route_node *rn, struct rib *rib)
1794{
Paul Jakma6d691122006-07-27 21:49:00 +00001795 if (IS_ZEBRA_DEBUG_RIB)
Paul Jakma93bdada2007-08-06 19:25:11 +00001796 {
Denis Ovsienkof304cb42007-10-03 12:27:16 +00001797 char buf[INET6_ADDRSTRLEN];
1798 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN);
Paul Jakma93bdada2007-08-06 19:25:11 +00001799 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__,
1800 buf, rn->p.prefixlen, rn, rib);
1801 }
Paul Jakma6d691122006-07-27 21:49:00 +00001802 SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1803 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00001804}
1805
1806int
1807rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
Paul Jakma7514fb72007-05-02 16:05:35 +00001808 struct in_addr *gate, struct in_addr *src,
1809 unsigned int ifindex, u_int32_t vrf_id,
G.Balajicddf3912011-11-26 21:59:32 +04001810 u_int32_t metric, u_char distance, safi_t safi)
paul718e3742002-12-13 20:15:29 +00001811{
1812 struct rib *rib;
1813 struct rib *same = NULL;
1814 struct route_table *table;
1815 struct route_node *rn;
1816 struct nexthop *nexthop;
1817
1818 /* Lookup table. */
G.Balajicddf3912011-11-26 21:59:32 +04001819 table = vrf_table (AFI_IP, safi, 0);
paul718e3742002-12-13 20:15:29 +00001820 if (! table)
1821 return 0;
1822
1823 /* Make it sure prefixlen is applied to the prefix. */
1824 apply_mask_ipv4 (p);
1825
1826 /* Set default distance by route type. */
1827 if (distance == 0)
1828 {
Balaji.G837d16c2012-09-26 14:09:10 +05301829 if ((unsigned)type >= array_size(route_info))
David Lamparter7052f222009-08-27 00:28:28 +02001830 distance = 150;
1831 else
1832 distance = route_info[type].distance;
paul718e3742002-12-13 20:15:29 +00001833
1834 /* iBGP distance is 200. */
1835 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
1836 distance = 200;
1837 }
1838
1839 /* Lookup route node.*/
1840 rn = route_node_get (table, (struct prefix *) p);
1841
1842 /* If same type of route are installed, treat it as a implicit
1843 withdraw. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00001844 RNODE_FOREACH_RIB (rn, rib)
paul718e3742002-12-13 20:15:29 +00001845 {
Paul Jakma6d691122006-07-27 21:49:00 +00001846 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1847 continue;
1848
hassoebf1ead2005-09-21 14:58:20 +00001849 if (rib->type != type)
1850 continue;
1851 if (rib->type != ZEBRA_ROUTE_CONNECT)
paul4d38fdb2005-04-28 17:35:14 +00001852 {
1853 same = rib;
1854 break;
1855 }
hassoebf1ead2005-09-21 14:58:20 +00001856 /* Duplicate connected route comes in. */
1857 else if ((nexthop = rib->nexthop) &&
1858 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
Paul Jakma6d691122006-07-27 21:49:00 +00001859 nexthop->ifindex == ifindex &&
1860 !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
hassoebf1ead2005-09-21 14:58:20 +00001861 {
1862 rib->refcnt++;
1863 return 0 ;
1864 }
paul718e3742002-12-13 20:15:29 +00001865 }
1866
1867 /* Allocate new rib structure. */
paul4d38fdb2005-04-28 17:35:14 +00001868 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001869 rib->type = type;
1870 rib->distance = distance;
1871 rib->flags = flags;
1872 rib->metric = metric;
paulb5f45022003-11-02 07:28:05 +00001873 rib->table = vrf_id;
paul718e3742002-12-13 20:15:29 +00001874 rib->nexthop_num = 0;
1875 rib->uptime = time (NULL);
1876
1877 /* Nexthop settings. */
1878 if (gate)
1879 {
1880 if (ifindex)
Paul Jakma7514fb72007-05-02 16:05:35 +00001881 nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
paul718e3742002-12-13 20:15:29 +00001882 else
Paul Jakma7514fb72007-05-02 16:05:35 +00001883 nexthop_ipv4_add (rib, gate, src);
paul718e3742002-12-13 20:15:29 +00001884 }
1885 else
1886 nexthop_ifindex_add (rib, ifindex);
1887
1888 /* If this route is kernel route, set FIB flag to the route. */
1889 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
1890 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1891 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1892
1893 /* Link new rib to node.*/
Denis Ovsienkodc958242007-08-13 16:03:06 +00001894 if (IS_ZEBRA_DEBUG_RIB)
1895 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib);
paul718e3742002-12-13 20:15:29 +00001896 rib_addnode (rn, rib);
paul4d38fdb2005-04-28 17:35:14 +00001897
paul718e3742002-12-13 20:15:29 +00001898 /* Free implicit route.*/
1899 if (same)
Denis Ovsienkodc958242007-08-13 16:03:06 +00001900 {
1901 if (IS_ZEBRA_DEBUG_RIB)
1902 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib);
paul4d38fdb2005-04-28 17:35:14 +00001903 rib_delnode (rn, same);
Denis Ovsienkodc958242007-08-13 16:03:06 +00001904 }
paul4d38fdb2005-04-28 17:35:14 +00001905
1906 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001907 return 0;
1908}
1909
Denis Ovsienkodc958242007-08-13 16:03:06 +00001910/* This function dumps the contents of a given RIB entry into
1911 * standard debug log. Calling function name and IP prefix in
1912 * question are passed as 1st and 2nd arguments.
1913 */
1914
1915void rib_dump (const char * func, const struct prefix_ipv4 * p, const struct rib * rib)
1916{
Christian Frankefa713d92013-07-05 15:35:37 +00001917 char straddr[INET_ADDRSTRLEN];
1918 struct nexthop *nexthop, *tnexthop;
1919 int recursing;
Denis Ovsienkodc958242007-08-13 16:03:06 +00001920
Christian Frankefa713d92013-07-05 15:35:37 +00001921 inet_ntop (AF_INET, &p->prefix, straddr, INET_ADDRSTRLEN);
1922 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr, p->prefixlen);
Denis Ovsienkodc958242007-08-13 16:03:06 +00001923 zlog_debug
1924 (
Stephen Hemmingerd02c56c2009-12-08 13:14:27 +03001925 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
Denis Ovsienkodc958242007-08-13 16:03:06 +00001926 func,
1927 rib->refcnt,
Stephen Hemmingerd02c56c2009-12-08 13:14:27 +03001928 (unsigned long) rib->uptime,
Denis Ovsienkodc958242007-08-13 16:03:06 +00001929 rib->type,
1930 rib->table
1931 );
1932 zlog_debug
1933 (
1934 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1935 func,
1936 rib->metric,
1937 rib->distance,
1938 rib->flags,
1939 rib->status
1940 );
1941 zlog_debug
1942 (
1943 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1944 func,
1945 rib->nexthop_num,
1946 rib->nexthop_active_num,
1947 rib->nexthop_fib_num
1948 );
Christian Frankefa713d92013-07-05 15:35:37 +00001949 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1950 {
1951 inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, straddr, INET_ADDRSTRLEN);
1952 zlog_debug
1953 (
1954 "%s: %s %s with flags %s%s%s",
1955 func,
1956 (recursing ? " NH" : "NH"),
1957 straddr,
1958 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
1959 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
1960 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
1961 );
1962 }
Denis Ovsienkodc958242007-08-13 16:03:06 +00001963 zlog_debug ("%s: dump complete", func);
1964}
1965
1966/* This is an exported helper to rtm_read() to dump the strange
1967 * RIB entry found by rib_lookup_ipv4_route()
1968 */
1969
1970void rib_lookup_and_dump (struct prefix_ipv4 * p)
1971{
1972 struct route_table *table;
1973 struct route_node *rn;
1974 struct rib *rib;
1975 char prefix_buf[INET_ADDRSTRLEN];
1976
1977 /* Lookup table. */
1978 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
1979 if (! table)
1980 {
1981 zlog_err ("%s: vrf_table() returned NULL", __func__);
1982 return;
1983 }
1984
1985 inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN);
1986 /* Scan the RIB table for exactly matching RIB entry. */
1987 rn = route_node_lookup (table, (struct prefix *) p);
1988
1989 /* No route for this prefix. */
1990 if (! rn)
1991 {
1992 zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen);
1993 return;
1994 }
1995
1996 /* Unlock node. */
1997 route_unlock_node (rn);
1998
1999 /* let's go */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002000 RNODE_FOREACH_RIB (rn, rib)
Denis Ovsienkodc958242007-08-13 16:03:06 +00002001 {
2002 zlog_debug
2003 (
2004 "%s: rn %p, rib %p: %s, %s",
2005 __func__,
2006 rn,
2007 rib,
2008 (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
2009 (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
2010 );
2011 rib_dump (__func__, p, rib);
2012 }
2013}
2014
Denis Ovsienko20e5ff02008-02-26 14:02:24 +00002015/* Check if requested address assignment will fail due to another
2016 * route being installed by zebra in FIB already. Take necessary
2017 * actions, if needed: remove such a route from FIB and deSELECT
2018 * corresponding RIB entry. Then put affected RN into RIBQ head.
2019 */
2020void rib_lookup_and_pushup (struct prefix_ipv4 * p)
2021{
2022 struct route_table *table;
2023 struct route_node *rn;
2024 struct rib *rib;
2025 unsigned changed = 0;
2026
2027 if (NULL == (table = vrf_table (AFI_IP, SAFI_UNICAST, 0)))
2028 {
2029 zlog_err ("%s: vrf_table() returned NULL", __func__);
2030 return;
2031 }
2032
2033 /* No matches would be the simplest case. */
2034 if (NULL == (rn = route_node_lookup (table, (struct prefix *) p)))
2035 return;
2036
2037 /* Unlock node. */
2038 route_unlock_node (rn);
2039
2040 /* Check all RIB entries. In case any changes have to be done, requeue
2041 * the RN into RIBQ head. If the routing message about the new connected
2042 * route (generated by the IP address we are going to assign very soon)
2043 * comes before the RIBQ is processed, the new RIB entry will join
2044 * RIBQ record already on head. This is necessary for proper revalidation
2045 * of the rest of the RIB.
2046 */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002047 RNODE_FOREACH_RIB (rn, rib)
Denis Ovsienko20e5ff02008-02-26 14:02:24 +00002048 {
2049 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) &&
2050 ! RIB_SYSTEM_ROUTE (rib))
2051 {
2052 changed = 1;
2053 if (IS_ZEBRA_DEBUG_RIB)
2054 {
2055 char buf[INET_ADDRSTRLEN];
2056 inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN);
2057 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__, buf, p->prefixlen);
2058 rib_dump (__func__, (struct prefix_ipv4 *)&rn->p, rib);
2059 }
2060 rib_uninstall (rn, rib);
2061 }
2062 }
2063 if (changed)
Denis Ovsienko20e5ff02008-02-26 14:02:24 +00002064 rib_queue_add (&zebrad, rn);
Denis Ovsienko20e5ff02008-02-26 14:02:24 +00002065}
2066
paul718e3742002-12-13 20:15:29 +00002067int
G.Balajicddf3912011-11-26 21:59:32 +04002068rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
paul718e3742002-12-13 20:15:29 +00002069{
2070 struct route_table *table;
2071 struct route_node *rn;
2072 struct rib *same;
2073 struct nexthop *nexthop;
paul4d38fdb2005-04-28 17:35:14 +00002074
paul718e3742002-12-13 20:15:29 +00002075 /* Lookup table. */
G.Balajicddf3912011-11-26 21:59:32 +04002076 table = vrf_table (AFI_IP, safi, 0);
paul718e3742002-12-13 20:15:29 +00002077 if (! table)
2078 return 0;
G.Balajicddf3912011-11-26 21:59:32 +04002079
paul718e3742002-12-13 20:15:29 +00002080 /* Make it sure prefixlen is applied to the prefix. */
2081 apply_mask_ipv4 (p);
2082
2083 /* Set default distance by route type. */
2084 if (rib->distance == 0)
2085 {
2086 rib->distance = route_info[rib->type].distance;
2087
2088 /* iBGP distance is 200. */
2089 if (rib->type == ZEBRA_ROUTE_BGP
2090 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
2091 rib->distance = 200;
2092 }
2093
2094 /* Lookup route node.*/
2095 rn = route_node_get (table, (struct prefix *) p);
2096
2097 /* If same type of route are installed, treat it as a implicit
2098 withdraw. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002099 RNODE_FOREACH_RIB (rn, same)
paul718e3742002-12-13 20:15:29 +00002100 {
Paul Jakma0b8c4f12007-06-27 11:12:38 +00002101 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
Paul Jakma6d691122006-07-27 21:49:00 +00002102 continue;
2103
paul718e3742002-12-13 20:15:29 +00002104 if (same->type == rib->type && same->table == rib->table
2105 && same->type != ZEBRA_ROUTE_CONNECT)
paul4d38fdb2005-04-28 17:35:14 +00002106 break;
paul718e3742002-12-13 20:15:29 +00002107 }
paul4d38fdb2005-04-28 17:35:14 +00002108
paul718e3742002-12-13 20:15:29 +00002109 /* If this route is kernel route, set FIB flag to the route. */
2110 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
2111 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2112 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2113
2114 /* Link new rib to node.*/
2115 rib_addnode (rn, rib);
Denis Ovsienkodc958242007-08-13 16:03:06 +00002116 if (IS_ZEBRA_DEBUG_RIB)
2117 {
2118 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2119 __func__, rn, rib);
2120 rib_dump (__func__, p, rib);
2121 }
paul718e3742002-12-13 20:15:29 +00002122
paul718e3742002-12-13 20:15:29 +00002123 /* Free implicit route.*/
2124 if (same)
Denis Ovsienkodc958242007-08-13 16:03:06 +00002125 {
2126 if (IS_ZEBRA_DEBUG_RIB)
2127 {
2128 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2129 __func__, rn, same);
2130 rib_dump (__func__, p, same);
2131 }
paul4d38fdb2005-04-28 17:35:14 +00002132 rib_delnode (rn, same);
Denis Ovsienkodc958242007-08-13 16:03:06 +00002133 }
paul4d38fdb2005-04-28 17:35:14 +00002134
2135 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002136 return 0;
2137}
2138
hassoebf1ead2005-09-21 14:58:20 +00002139/* XXX factor with rib_delete_ipv6 */
paul718e3742002-12-13 20:15:29 +00002140int
2141rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
G.Balajicddf3912011-11-26 21:59:32 +04002142 struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
paul718e3742002-12-13 20:15:29 +00002143{
2144 struct route_table *table;
2145 struct route_node *rn;
2146 struct rib *rib;
2147 struct rib *fib = NULL;
2148 struct rib *same = NULL;
Christian Frankefa713d92013-07-05 15:35:37 +00002149 struct nexthop *nexthop, *tnexthop;
2150 int recursing;
Stephen Hemminger81cce012009-04-28 14:28:00 -07002151 char buf1[INET_ADDRSTRLEN];
2152 char buf2[INET_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00002153
2154 /* Lookup table. */
G.Balajicddf3912011-11-26 21:59:32 +04002155 table = vrf_table (AFI_IP, safi, 0);
paul718e3742002-12-13 20:15:29 +00002156 if (! table)
2157 return 0;
2158
2159 /* Apply mask. */
2160 apply_mask_ipv4 (p);
2161
paul5ec90d22003-06-19 01:41:37 +00002162 if (IS_ZEBRA_DEBUG_KERNEL && gate)
ajsb6178002004-12-07 21:12:56 +00002163 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002164 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
paul5ec90d22003-06-19 01:41:37 +00002165 p->prefixlen,
2166 inet_ntoa (*gate),
2167 ifindex);
2168
paul718e3742002-12-13 20:15:29 +00002169 /* Lookup route node. */
2170 rn = route_node_lookup (table, (struct prefix *) p);
2171 if (! rn)
2172 {
2173 if (IS_ZEBRA_DEBUG_KERNEL)
2174 {
2175 if (gate)
ajsb6178002004-12-07 21:12:56 +00002176 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002177 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002178 p->prefixlen,
Stephen Hemminger81cce012009-04-28 14:28:00 -07002179 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002180 ifindex);
2181 else
ajsb6178002004-12-07 21:12:56 +00002182 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002183 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002184 p->prefixlen,
2185 ifindex);
2186 }
2187 return ZEBRA_ERR_RTNOEXIST;
2188 }
2189
2190 /* Lookup same type route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002191 RNODE_FOREACH_RIB (rn, rib)
paul718e3742002-12-13 20:15:29 +00002192 {
Paul Jakma6d691122006-07-27 21:49:00 +00002193 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2194 continue;
2195
paul718e3742002-12-13 20:15:29 +00002196 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
2197 fib = rib;
2198
hassoebf1ead2005-09-21 14:58:20 +00002199 if (rib->type != type)
2200 continue;
2201 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
Matthias Ferdinand4f1735f2011-12-26 16:35:30 +04002202 nexthop->type == NEXTHOP_TYPE_IFINDEX)
paul718e3742002-12-13 20:15:29 +00002203 {
Matthias Ferdinand4f1735f2011-12-26 16:35:30 +04002204 if (nexthop->ifindex != ifindex)
2205 continue;
hassoebf1ead2005-09-21 14:58:20 +00002206 if (rib->refcnt)
paul718e3742002-12-13 20:15:29 +00002207 {
hassoebf1ead2005-09-21 14:58:20 +00002208 rib->refcnt--;
2209 route_unlock_node (rn);
2210 route_unlock_node (rn);
2211 return 0;
paul718e3742002-12-13 20:15:29 +00002212 }
hassoebf1ead2005-09-21 14:58:20 +00002213 same = rib;
2214 break;
paul718e3742002-12-13 20:15:29 +00002215 }
hassoebf1ead2005-09-21 14:58:20 +00002216 /* Make sure that the route found has the same gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +00002217 else
paul5ec90d22003-06-19 01:41:37 +00002218 {
Christian Frankefa713d92013-07-05 15:35:37 +00002219 if (gate == NULL)
2220 {
2221 same = rib;
2222 break;
2223 }
2224 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2225 if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate))
2226 {
2227 same = rib;
2228 break;
2229 }
2230 if (same)
2231 break;
2232 }
paul718e3742002-12-13 20:15:29 +00002233 }
paul718e3742002-12-13 20:15:29 +00002234 /* If same type of route can't be found and this message is from
2235 kernel. */
2236 if (! same)
2237 {
2238 if (fib && type == ZEBRA_ROUTE_KERNEL)
2239 {
2240 /* Unset flags. */
2241 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
2242 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2243
2244 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
2245 }
2246 else
2247 {
2248 if (IS_ZEBRA_DEBUG_KERNEL)
2249 {
2250 if (gate)
ajsb6178002004-12-07 21:12:56 +00002251 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002252 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002253 p->prefixlen,
Stephen Hemminger81cce012009-04-28 14:28:00 -07002254 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002255 ifindex,
2256 type);
2257 else
ajsb6178002004-12-07 21:12:56 +00002258 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002259 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002260 p->prefixlen,
2261 ifindex,
2262 type);
2263 }
2264 route_unlock_node (rn);
2265 return ZEBRA_ERR_RTNOEXIST;
2266 }
2267 }
paul4d38fdb2005-04-28 17:35:14 +00002268
paul718e3742002-12-13 20:15:29 +00002269 if (same)
2270 rib_delnode (rn, same);
paul4d38fdb2005-04-28 17:35:14 +00002271
paul718e3742002-12-13 20:15:29 +00002272 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002273 return 0;
2274}
2275
2276/* Install static route into rib. */
paula1ac18c2005-06-28 17:17:12 +00002277static void
paul718e3742002-12-13 20:15:29 +00002278static_install_ipv4 (struct prefix *p, struct static_ipv4 *si)
2279{
2280 struct rib *rib;
2281 struct route_node *rn;
2282 struct route_table *table;
2283
2284 /* Lookup table. */
2285 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
2286 if (! table)
2287 return;
2288
2289 /* Lookup existing route */
2290 rn = route_node_get (table, p);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002291 RNODE_FOREACH_RIB (rn, rib)
Paul Jakma6d691122006-07-27 21:49:00 +00002292 {
2293 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2294 continue;
2295
2296 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2297 break;
2298 }
paul718e3742002-12-13 20:15:29 +00002299
2300 if (rib)
2301 {
2302 /* Same distance static route is there. Update it with new
2303 nexthop. */
paul718e3742002-12-13 20:15:29 +00002304 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002305 switch (si->type)
paul7021c422003-07-15 12:52:22 +00002306 {
2307 case STATIC_IPV4_GATEWAY:
Paul Jakma7514fb72007-05-02 16:05:35 +00002308 nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
paul7021c422003-07-15 12:52:22 +00002309 break;
2310 case STATIC_IPV4_IFNAME:
2311 nexthop_ifname_add (rib, si->gate.ifname);
2312 break;
2313 case STATIC_IPV4_BLACKHOLE:
2314 nexthop_blackhole_add (rib);
2315 break;
paul4d38fdb2005-04-28 17:35:14 +00002316 }
Paul Jakma3c0755d2006-12-08 00:53:14 +00002317 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00002318 }
2319 else
2320 {
2321 /* This is new static route. */
paul4d38fdb2005-04-28 17:35:14 +00002322 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2323
paul718e3742002-12-13 20:15:29 +00002324 rib->type = ZEBRA_ROUTE_STATIC;
2325 rib->distance = si->distance;
2326 rib->metric = 0;
Nolan Leakeb0145dd2012-09-13 17:17:31 +00002327 rib->table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00002328 rib->nexthop_num = 0;
2329
2330 switch (si->type)
paul7021c422003-07-15 12:52:22 +00002331 {
2332 case STATIC_IPV4_GATEWAY:
Paul Jakma7514fb72007-05-02 16:05:35 +00002333 nexthop_ipv4_add (rib, &si->gate.ipv4, NULL);
paul7021c422003-07-15 12:52:22 +00002334 break;
2335 case STATIC_IPV4_IFNAME:
2336 nexthop_ifname_add (rib, si->gate.ifname);
2337 break;
2338 case STATIC_IPV4_BLACKHOLE:
2339 nexthop_blackhole_add (rib);
2340 break;
2341 }
paul718e3742002-12-13 20:15:29 +00002342
hasso81dfcaa2003-05-25 19:21:25 +00002343 /* Save the flags of this static routes (reject, blackhole) */
2344 rib->flags = si->flags;
2345
paul718e3742002-12-13 20:15:29 +00002346 /* Link this rib to the tree. */
2347 rib_addnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00002348 }
2349}
2350
paula1ac18c2005-06-28 17:17:12 +00002351static int
paul718e3742002-12-13 20:15:29 +00002352static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si)
2353{
2354 if (nexthop->type == NEXTHOP_TYPE_IPV4
2355 && si->type == STATIC_IPV4_GATEWAY
2356 && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4))
2357 return 1;
2358 if (nexthop->type == NEXTHOP_TYPE_IFNAME
2359 && si->type == STATIC_IPV4_IFNAME
2360 && strcmp (nexthop->ifname, si->gate.ifname) == 0)
2361 return 1;
paul595db7f2003-05-25 21:35:06 +00002362 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
2363 && si->type == STATIC_IPV4_BLACKHOLE)
2364 return 1;
paule8e19462006-01-19 20:16:55 +00002365 return 0;
paul718e3742002-12-13 20:15:29 +00002366}
2367
2368/* Uninstall static route from RIB. */
paula1ac18c2005-06-28 17:17:12 +00002369static void
paul718e3742002-12-13 20:15:29 +00002370static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si)
2371{
2372 struct route_node *rn;
2373 struct rib *rib;
2374 struct nexthop *nexthop;
2375 struct route_table *table;
2376
2377 /* Lookup table. */
2378 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
2379 if (! table)
2380 return;
paul4d38fdb2005-04-28 17:35:14 +00002381
paul718e3742002-12-13 20:15:29 +00002382 /* Lookup existing route with type and distance. */
2383 rn = route_node_lookup (table, p);
2384 if (! rn)
2385 return;
2386
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002387 RNODE_FOREACH_RIB (rn, rib)
Paul Jakma6d691122006-07-27 21:49:00 +00002388 {
2389 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2390 continue;
2391
2392 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2393 break;
2394 }
paul718e3742002-12-13 20:15:29 +00002395
2396 if (! rib)
2397 {
2398 route_unlock_node (rn);
2399 return;
2400 }
2401
2402 /* Lookup nexthop. */
2403 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2404 if (static_ipv4_nexthop_same (nexthop, si))
2405 break;
2406
2407 /* Can't find nexthop. */
2408 if (! nexthop)
2409 {
2410 route_unlock_node (rn);
2411 return;
2412 }
2413
2414 /* Check nexthop. */
2415 if (rib->nexthop_num == 1)
Paul Jakma6d691122006-07-27 21:49:00 +00002416 rib_delnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00002417 else
2418 {
paul6baeb982003-10-28 03:47:15 +00002419 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
2420 rib_uninstall (rn, rib);
paul319572c2005-09-21 12:30:08 +00002421 nexthop_delete (rib, nexthop);
2422 nexthop_free (nexthop);
Paul Jakma6d691122006-07-27 21:49:00 +00002423 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00002424 }
paul718e3742002-12-13 20:15:29 +00002425 /* Unlock node. */
2426 route_unlock_node (rn);
2427}
2428
2429/* Add static route into static route configuration. */
2430int
hasso39db97e2004-10-12 20:50:58 +00002431static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
hasso81dfcaa2003-05-25 19:21:25 +00002432 u_char flags, u_char distance, u_int32_t vrf_id)
paul718e3742002-12-13 20:15:29 +00002433{
2434 u_char type = 0;
2435 struct route_node *rn;
2436 struct static_ipv4 *si;
2437 struct static_ipv4 *pp;
2438 struct static_ipv4 *cp;
2439 struct static_ipv4 *update = NULL;
2440 struct route_table *stable;
2441
2442 /* Lookup table. */
2443 stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
2444 if (! stable)
2445 return -1;
2446
2447 /* Lookup static route prefix. */
2448 rn = route_node_get (stable, p);
2449
2450 /* Make flags. */
2451 if (gate)
2452 type = STATIC_IPV4_GATEWAY;
paul368aa3f2003-05-25 23:24:50 +00002453 else if (ifname)
paul718e3742002-12-13 20:15:29 +00002454 type = STATIC_IPV4_IFNAME;
paul595db7f2003-05-25 21:35:06 +00002455 else
2456 type = STATIC_IPV4_BLACKHOLE;
paul718e3742002-12-13 20:15:29 +00002457
2458 /* Do nothing if there is a same static route. */
2459 for (si = rn->info; si; si = si->next)
2460 {
2461 if (type == si->type
2462 && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
2463 && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
2464 {
2465 if (distance == si->distance)
2466 {
2467 route_unlock_node (rn);
2468 return 0;
2469 }
2470 else
2471 update = si;
2472 }
2473 }
2474
Paul Jakma3c0755d2006-12-08 00:53:14 +00002475 /* Distance changed. */
paul718e3742002-12-13 20:15:29 +00002476 if (update)
2477 static_delete_ipv4 (p, gate, ifname, update->distance, vrf_id);
2478
2479 /* Make new static route structure. */
Stephen Hemminger393deb92008-08-18 14:13:29 -07002480 si = XCALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4));
paul718e3742002-12-13 20:15:29 +00002481
2482 si->type = type;
2483 si->distance = distance;
hasso81dfcaa2003-05-25 19:21:25 +00002484 si->flags = flags;
paul718e3742002-12-13 20:15:29 +00002485
2486 if (gate)
2487 si->gate.ipv4 = *gate;
2488 if (ifname)
2489 si->gate.ifname = XSTRDUP (0, ifname);
2490
2491 /* Add new static route information to the tree with sort by
2492 distance value and gateway address. */
2493 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
2494 {
2495 if (si->distance < cp->distance)
2496 break;
2497 if (si->distance > cp->distance)
2498 continue;
2499 if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
2500 {
2501 if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr))
2502 break;
2503 if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr))
2504 continue;
2505 }
2506 }
2507
2508 /* Make linked list. */
2509 if (pp)
2510 pp->next = si;
2511 else
2512 rn->info = si;
2513 if (cp)
2514 cp->prev = si;
2515 si->prev = pp;
2516 si->next = cp;
2517
2518 /* Install into rib. */
2519 static_install_ipv4 (p, si);
2520
2521 return 1;
2522}
2523
2524/* Delete static route from static route configuration. */
2525int
hasso39db97e2004-10-12 20:50:58 +00002526static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
paul718e3742002-12-13 20:15:29 +00002527 u_char distance, u_int32_t vrf_id)
2528{
2529 u_char type = 0;
2530 struct route_node *rn;
2531 struct static_ipv4 *si;
2532 struct route_table *stable;
2533
2534 /* Lookup table. */
2535 stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
2536 if (! stable)
2537 return -1;
2538
2539 /* Lookup static route prefix. */
2540 rn = route_node_lookup (stable, p);
2541 if (! rn)
2542 return 0;
2543
2544 /* Make flags. */
2545 if (gate)
2546 type = STATIC_IPV4_GATEWAY;
2547 else if (ifname)
2548 type = STATIC_IPV4_IFNAME;
paul595db7f2003-05-25 21:35:06 +00002549 else
2550 type = STATIC_IPV4_BLACKHOLE;
paul718e3742002-12-13 20:15:29 +00002551
2552 /* Find same static route is the tree */
2553 for (si = rn->info; si; si = si->next)
2554 if (type == si->type
2555 && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4))
2556 && (! ifname || strcmp (ifname, si->gate.ifname) == 0))
2557 break;
2558
2559 /* Can't find static route. */
2560 if (! si)
2561 {
2562 route_unlock_node (rn);
2563 return 0;
2564 }
2565
2566 /* Install into rib. */
2567 static_uninstall_ipv4 (p, si);
2568
2569 /* Unlink static route from linked list. */
2570 if (si->prev)
2571 si->prev->next = si->next;
2572 else
2573 rn->info = si->next;
2574 if (si->next)
2575 si->next->prev = si->prev;
paul143a3852003-09-29 20:06:13 +00002576 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002577
2578 /* Free static route configuration. */
paula0f6acd2003-05-14 18:29:13 +00002579 if (ifname)
2580 XFREE (0, si->gate.ifname);
paul718e3742002-12-13 20:15:29 +00002581 XFREE (MTYPE_STATIC_IPV4, si);
2582
paul143a3852003-09-29 20:06:13 +00002583 route_unlock_node (rn);
2584
paul718e3742002-12-13 20:15:29 +00002585 return 1;
2586}
2587
2588
2589#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +00002590static int
paul718e3742002-12-13 20:15:29 +00002591rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
2592 struct in6_addr *gate, unsigned int ifindex, int table)
2593{
hasso726f9b22003-05-25 21:04:54 +00002594 if (type == ZEBRA_ROUTE_CONNECT && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)) {
2595#if defined (MUSICA) || defined (LINUX)
2596 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2597 if (p->prefixlen == 96)
2598 return 0;
2599#endif /* MUSICA */
paul718e3742002-12-13 20:15:29 +00002600 return 1;
hasso726f9b22003-05-25 21:04:54 +00002601 }
paul718e3742002-12-13 20:15:29 +00002602 if (type == ZEBRA_ROUTE_KERNEL && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)
2603 && p->prefixlen == 96 && gate && IN6_IS_ADDR_UNSPECIFIED (gate))
2604 {
2605 kernel_delete_ipv6_old (p, gate, ifindex, 0, table);
2606 return 1;
2607 }
2608 return 0;
2609}
2610
2611int
2612rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
hassobe61c4e2005-08-27 06:05:47 +00002613 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
G.Balajif768f362011-11-26 22:10:39 +04002614 u_int32_t metric, u_char distance, safi_t safi)
paul718e3742002-12-13 20:15:29 +00002615{
2616 struct rib *rib;
2617 struct rib *same = NULL;
2618 struct route_table *table;
2619 struct route_node *rn;
2620 struct nexthop *nexthop;
2621
paul718e3742002-12-13 20:15:29 +00002622 /* Lookup table. */
G.Balajif768f362011-11-26 22:10:39 +04002623 table = vrf_table (AFI_IP6, safi, 0);
paul718e3742002-12-13 20:15:29 +00002624 if (! table)
2625 return 0;
2626
2627 /* Make sure mask is applied. */
2628 apply_mask_ipv6 (p);
2629
2630 /* Set default distance by route type. */
hassobe61c4e2005-08-27 06:05:47 +00002631 if (!distance)
2632 distance = route_info[type].distance;
paul718e3742002-12-13 20:15:29 +00002633
2634 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2635 distance = 200;
2636
2637 /* Filter bogus route. */
2638 if (rib_bogus_ipv6 (type, p, gate, ifindex, 0))
2639 return 0;
2640
2641 /* Lookup route node.*/
2642 rn = route_node_get (table, (struct prefix *) p);
2643
2644 /* If same type of route are installed, treat it as a implicit
2645 withdraw. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002646 RNODE_FOREACH_RIB (rn, rib)
paul718e3742002-12-13 20:15:29 +00002647 {
Paul Jakma6d691122006-07-27 21:49:00 +00002648 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2649 continue;
2650
hassoebf1ead2005-09-21 14:58:20 +00002651 if (rib->type != type)
2652 continue;
2653 if (rib->type != ZEBRA_ROUTE_CONNECT)
paul718e3742002-12-13 20:15:29 +00002654 {
2655 same = rib;
paul718e3742002-12-13 20:15:29 +00002656 break;
2657 }
hassoebf1ead2005-09-21 14:58:20 +00002658 else if ((nexthop = rib->nexthop) &&
2659 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
2660 nexthop->ifindex == ifindex)
2661 {
2662 rib->refcnt++;
2663 return 0;
2664 }
paul718e3742002-12-13 20:15:29 +00002665 }
2666
2667 /* Allocate new rib structure. */
paul4d38fdb2005-04-28 17:35:14 +00002668 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2669
paul718e3742002-12-13 20:15:29 +00002670 rib->type = type;
2671 rib->distance = distance;
2672 rib->flags = flags;
2673 rib->metric = metric;
paulb5f45022003-11-02 07:28:05 +00002674 rib->table = vrf_id;
paul718e3742002-12-13 20:15:29 +00002675 rib->nexthop_num = 0;
2676 rib->uptime = time (NULL);
2677
2678 /* Nexthop settings. */
2679 if (gate)
2680 {
2681 if (ifindex)
2682 nexthop_ipv6_ifindex_add (rib, gate, ifindex);
2683 else
2684 nexthop_ipv6_add (rib, gate);
2685 }
2686 else
2687 nexthop_ifindex_add (rib, ifindex);
2688
2689 /* If this route is kernel route, set FIB flag to the route. */
2690 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
2691 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2692 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2693
2694 /* Link new rib to node.*/
2695 rib_addnode (rn, rib);
2696
paul718e3742002-12-13 20:15:29 +00002697 /* Free implicit route.*/
2698 if (same)
paul4d38fdb2005-04-28 17:35:14 +00002699 rib_delnode (rn, same);
2700
2701 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002702 return 0;
2703}
2704
hassoebf1ead2005-09-21 14:58:20 +00002705/* XXX factor with rib_delete_ipv6 */
paul718e3742002-12-13 20:15:29 +00002706int
2707rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
G.Balajif768f362011-11-26 22:10:39 +04002708 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
paul718e3742002-12-13 20:15:29 +00002709{
2710 struct route_table *table;
2711 struct route_node *rn;
2712 struct rib *rib;
2713 struct rib *fib = NULL;
2714 struct rib *same = NULL;
Christian Frankefa713d92013-07-05 15:35:37 +00002715 struct nexthop *nexthop, *tnexthop;
2716 int recursing;
Stephen Hemminger81cce012009-04-28 14:28:00 -07002717 char buf1[INET6_ADDRSTRLEN];
2718 char buf2[INET6_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00002719
2720 /* Apply mask. */
2721 apply_mask_ipv6 (p);
2722
2723 /* Lookup table. */
G.Balajif768f362011-11-26 22:10:39 +04002724 table = vrf_table (AFI_IP6, safi, 0);
paul718e3742002-12-13 20:15:29 +00002725 if (! table)
2726 return 0;
paul4d38fdb2005-04-28 17:35:14 +00002727
paul718e3742002-12-13 20:15:29 +00002728 /* Lookup route node. */
2729 rn = route_node_lookup (table, (struct prefix *) p);
2730 if (! rn)
2731 {
2732 if (IS_ZEBRA_DEBUG_KERNEL)
2733 {
2734 if (gate)
ajsb6178002004-12-07 21:12:56 +00002735 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002736 inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002737 p->prefixlen,
Stephen Hemminger81cce012009-04-28 14:28:00 -07002738 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002739 ifindex);
2740 else
ajsb6178002004-12-07 21:12:56 +00002741 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002742 inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002743 p->prefixlen,
2744 ifindex);
2745 }
2746 return ZEBRA_ERR_RTNOEXIST;
2747 }
2748
2749 /* Lookup same type route. */
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002750 RNODE_FOREACH_RIB (rn, rib)
paul718e3742002-12-13 20:15:29 +00002751 {
Paul Jakma6d691122006-07-27 21:49:00 +00002752 if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
2753 continue;
2754
paul718e3742002-12-13 20:15:29 +00002755 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
2756 fib = rib;
2757
hassoebf1ead2005-09-21 14:58:20 +00002758 if (rib->type != type)
2759 continue;
2760 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
Matthias Ferdinand4f1735f2011-12-26 16:35:30 +04002761 nexthop->type == NEXTHOP_TYPE_IFINDEX)
paul718e3742002-12-13 20:15:29 +00002762 {
Matthias Ferdinand4f1735f2011-12-26 16:35:30 +04002763 if (nexthop->ifindex != ifindex)
2764 continue;
hassoebf1ead2005-09-21 14:58:20 +00002765 if (rib->refcnt)
paul718e3742002-12-13 20:15:29 +00002766 {
hassoebf1ead2005-09-21 14:58:20 +00002767 rib->refcnt--;
2768 route_unlock_node (rn);
2769 route_unlock_node (rn);
2770 return 0;
paul718e3742002-12-13 20:15:29 +00002771 }
hassoebf1ead2005-09-21 14:58:20 +00002772 same = rib;
2773 break;
paul718e3742002-12-13 20:15:29 +00002774 }
hassoebf1ead2005-09-21 14:58:20 +00002775 /* Make sure that the route found has the same gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +00002776 else
2777 {
2778 if (gate == NULL)
2779 {
2780 same = rib;
2781 break;
2782 }
2783 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2784 if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
2785 {
2786 same = rib;
2787 break;
2788 }
2789 if (same)
2790 break;
2791 }
paul718e3742002-12-13 20:15:29 +00002792 }
2793
2794 /* If same type of route can't be found and this message is from
2795 kernel. */
2796 if (! same)
2797 {
2798 if (fib && type == ZEBRA_ROUTE_KERNEL)
2799 {
2800 /* Unset flags. */
2801 for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next)
2802 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2803
2804 UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED);
2805 }
2806 else
2807 {
2808 if (IS_ZEBRA_DEBUG_KERNEL)
2809 {
2810 if (gate)
ajsb6178002004-12-07 21:12:56 +00002811 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002812 inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002813 p->prefixlen,
Stephen Hemminger81cce012009-04-28 14:28:00 -07002814 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002815 ifindex,
2816 type);
2817 else
ajsb6178002004-12-07 21:12:56 +00002818 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
Stephen Hemminger81cce012009-04-28 14:28:00 -07002819 inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00002820 p->prefixlen,
2821 ifindex,
2822 type);
2823 }
2824 route_unlock_node (rn);
2825 return ZEBRA_ERR_RTNOEXIST;
2826 }
2827 }
2828
2829 if (same)
2830 rib_delnode (rn, same);
paul4d38fdb2005-04-28 17:35:14 +00002831
paul718e3742002-12-13 20:15:29 +00002832 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00002833 return 0;
2834}
2835
2836/* Install static route into rib. */
paula1ac18c2005-06-28 17:17:12 +00002837static void
paul718e3742002-12-13 20:15:29 +00002838static_install_ipv6 (struct prefix *p, struct static_ipv6 *si)
2839{
2840 struct rib *rib;
2841 struct route_table *table;
2842 struct route_node *rn;
2843
2844 /* Lookup table. */
2845 table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
2846 if (! table)
2847 return;
2848
2849 /* Lookup existing route */
2850 rn = route_node_get (table, p);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002851 RNODE_FOREACH_RIB (rn, rib)
Paul Jakma6d691122006-07-27 21:49:00 +00002852 {
2853 if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
2854 continue;
2855
2856 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2857 break;
2858 }
paul718e3742002-12-13 20:15:29 +00002859
2860 if (rib)
2861 {
2862 /* Same distance static route is there. Update it with new
2863 nexthop. */
paul718e3742002-12-13 20:15:29 +00002864 route_unlock_node (rn);
2865
2866 switch (si->type)
2867 {
2868 case STATIC_IPV6_GATEWAY:
2869 nexthop_ipv6_add (rib, &si->ipv6);
2870 break;
2871 case STATIC_IPV6_IFNAME:
2872 nexthop_ifname_add (rib, si->ifname);
2873 break;
2874 case STATIC_IPV6_GATEWAY_IFNAME:
2875 nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
2876 break;
2877 }
Paul Jakma3c0755d2006-12-08 00:53:14 +00002878 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00002879 }
2880 else
2881 {
2882 /* This is new static route. */
paul4d38fdb2005-04-28 17:35:14 +00002883 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2884
paul718e3742002-12-13 20:15:29 +00002885 rib->type = ZEBRA_ROUTE_STATIC;
2886 rib->distance = si->distance;
2887 rib->metric = 0;
2888 rib->nexthop_num = 0;
2889
2890 switch (si->type)
2891 {
2892 case STATIC_IPV6_GATEWAY:
2893 nexthop_ipv6_add (rib, &si->ipv6);
2894 break;
2895 case STATIC_IPV6_IFNAME:
2896 nexthop_ifname_add (rib, si->ifname);
2897 break;
2898 case STATIC_IPV6_GATEWAY_IFNAME:
2899 nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname);
2900 break;
2901 }
2902
hasso81dfcaa2003-05-25 19:21:25 +00002903 /* Save the flags of this static routes (reject, blackhole) */
2904 rib->flags = si->flags;
2905
paul718e3742002-12-13 20:15:29 +00002906 /* Link this rib to the tree. */
2907 rib_addnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00002908 }
2909}
2910
paula1ac18c2005-06-28 17:17:12 +00002911static int
paul718e3742002-12-13 20:15:29 +00002912static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si)
2913{
2914 if (nexthop->type == NEXTHOP_TYPE_IPV6
2915 && si->type == STATIC_IPV6_GATEWAY
2916 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6))
2917 return 1;
2918 if (nexthop->type == NEXTHOP_TYPE_IFNAME
2919 && si->type == STATIC_IPV6_IFNAME
2920 && strcmp (nexthop->ifname, si->ifname) == 0)
2921 return 1;
2922 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
2923 && si->type == STATIC_IPV6_GATEWAY_IFNAME
2924 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)
2925 && strcmp (nexthop->ifname, si->ifname) == 0)
2926 return 1;
paule8e19462006-01-19 20:16:55 +00002927 return 0;
paul718e3742002-12-13 20:15:29 +00002928}
2929
paula1ac18c2005-06-28 17:17:12 +00002930static void
paul718e3742002-12-13 20:15:29 +00002931static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si)
2932{
2933 struct route_table *table;
2934 struct route_node *rn;
2935 struct rib *rib;
2936 struct nexthop *nexthop;
2937
2938 /* Lookup table. */
2939 table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
2940 if (! table)
2941 return;
2942
2943 /* Lookup existing route with type and distance. */
2944 rn = route_node_lookup (table, (struct prefix *) p);
2945 if (! rn)
2946 return;
2947
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00002948 RNODE_FOREACH_RIB (rn, rib)
Paul Jakma6d691122006-07-27 21:49:00 +00002949 {
2950 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2951 continue;
2952
2953 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2954 break;
2955 }
2956
paul718e3742002-12-13 20:15:29 +00002957 if (! rib)
2958 {
2959 route_unlock_node (rn);
2960 return;
2961 }
2962
2963 /* Lookup nexthop. */
2964 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2965 if (static_ipv6_nexthop_same (nexthop, si))
2966 break;
2967
2968 /* Can't find nexthop. */
2969 if (! nexthop)
2970 {
2971 route_unlock_node (rn);
2972 return;
2973 }
2974
2975 /* Check nexthop. */
2976 if (rib->nexthop_num == 1)
2977 {
2978 rib_delnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00002979 }
2980 else
2981 {
paul6baeb982003-10-28 03:47:15 +00002982 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
2983 rib_uninstall (rn, rib);
paul319572c2005-09-21 12:30:08 +00002984 nexthop_delete (rib, nexthop);
2985 nexthop_free (nexthop);
Paul Jakma6d691122006-07-27 21:49:00 +00002986 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00002987 }
paul718e3742002-12-13 20:15:29 +00002988 /* Unlock node. */
2989 route_unlock_node (rn);
2990}
2991
2992/* Add static route into static route configuration. */
2993int
2994static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +00002995 const char *ifname, u_char flags, u_char distance,
2996 u_int32_t vrf_id)
paul718e3742002-12-13 20:15:29 +00002997{
2998 struct route_node *rn;
2999 struct static_ipv6 *si;
3000 struct static_ipv6 *pp;
3001 struct static_ipv6 *cp;
3002 struct route_table *stable;
3003
3004 /* Lookup table. */
3005 stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
3006 if (! stable)
3007 return -1;
Paul Jakma27b47252006-07-02 16:38:54 +00003008
3009 if (!gate &&
3010 (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME))
3011 return -1;
3012
3013 if (!ifname &&
3014 (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME))
3015 return -1;
paul718e3742002-12-13 20:15:29 +00003016
3017 /* Lookup static route prefix. */
3018 rn = route_node_get (stable, p);
3019
3020 /* Do nothing if there is a same static route. */
3021 for (si = rn->info; si; si = si->next)
3022 {
3023 if (distance == si->distance
3024 && type == si->type
3025 && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
3026 && (! ifname || strcmp (ifname, si->ifname) == 0))
3027 {
3028 route_unlock_node (rn);
3029 return 0;
3030 }
3031 }
3032
3033 /* Make new static route structure. */
Stephen Hemminger393deb92008-08-18 14:13:29 -07003034 si = XCALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6));
paul718e3742002-12-13 20:15:29 +00003035
3036 si->type = type;
3037 si->distance = distance;
hasso81dfcaa2003-05-25 19:21:25 +00003038 si->flags = flags;
paul718e3742002-12-13 20:15:29 +00003039
3040 switch (type)
3041 {
3042 case STATIC_IPV6_GATEWAY:
3043 si->ipv6 = *gate;
3044 break;
3045 case STATIC_IPV6_IFNAME:
3046 si->ifname = XSTRDUP (0, ifname);
3047 break;
3048 case STATIC_IPV6_GATEWAY_IFNAME:
3049 si->ipv6 = *gate;
3050 si->ifname = XSTRDUP (0, ifname);
3051 break;
3052 }
3053
3054 /* Add new static route information to the tree with sort by
3055 distance value and gateway address. */
3056 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
3057 {
3058 if (si->distance < cp->distance)
3059 break;
3060 if (si->distance > cp->distance)
3061 continue;
3062 }
3063
3064 /* Make linked list. */
3065 if (pp)
3066 pp->next = si;
3067 else
3068 rn->info = si;
3069 if (cp)
3070 cp->prev = si;
3071 si->prev = pp;
3072 si->next = cp;
3073
3074 /* Install into rib. */
3075 static_install_ipv6 (p, si);
3076
3077 return 1;
3078}
3079
3080/* Delete static route from static route configuration. */
3081int
3082static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +00003083 const char *ifname, u_char distance, u_int32_t vrf_id)
paul718e3742002-12-13 20:15:29 +00003084{
3085 struct route_node *rn;
3086 struct static_ipv6 *si;
3087 struct route_table *stable;
3088
3089 /* Lookup table. */
3090 stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
3091 if (! stable)
3092 return -1;
3093
3094 /* Lookup static route prefix. */
3095 rn = route_node_lookup (stable, p);
3096 if (! rn)
3097 return 0;
3098
3099 /* Find same static route is the tree */
3100 for (si = rn->info; si; si = si->next)
3101 if (distance == si->distance
3102 && type == si->type
3103 && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6))
3104 && (! ifname || strcmp (ifname, si->ifname) == 0))
3105 break;
3106
3107 /* Can't find static route. */
3108 if (! si)
3109 {
3110 route_unlock_node (rn);
3111 return 0;
3112 }
3113
3114 /* Install into rib. */
3115 static_uninstall_ipv6 (p, si);
3116
3117 /* Unlink static route from linked list. */
3118 if (si->prev)
3119 si->prev->next = si->next;
3120 else
3121 rn->info = si->next;
3122 if (si->next)
3123 si->next->prev = si->prev;
3124
3125 /* Free static route configuration. */
paula0f6acd2003-05-14 18:29:13 +00003126 if (ifname)
3127 XFREE (0, si->ifname);
paul718e3742002-12-13 20:15:29 +00003128 XFREE (MTYPE_STATIC_IPV6, si);
3129
3130 return 1;
3131}
3132#endif /* HAVE_IPV6 */
3133
3134/* RIB update function. */
3135void
paula1ac18c2005-06-28 17:17:12 +00003136rib_update (void)
paul718e3742002-12-13 20:15:29 +00003137{
3138 struct route_node *rn;
3139 struct route_table *table;
paul4d38fdb2005-04-28 17:35:14 +00003140
paul718e3742002-12-13 20:15:29 +00003141 table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
3142 if (table)
3143 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003144 if (rnode_to_ribs (rn))
Paul Jakma6d691122006-07-27 21:49:00 +00003145 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00003146
3147 table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
3148 if (table)
3149 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003150 if (rnode_to_ribs (rn))
Paul Jakma6d691122006-07-27 21:49:00 +00003151 rib_queue_add (&zebrad, rn);
paul718e3742002-12-13 20:15:29 +00003152}
3153
paul718e3742002-12-13 20:15:29 +00003154
3155/* Remove all routes which comes from non main table. */
paula1ac18c2005-06-28 17:17:12 +00003156static void
paul718e3742002-12-13 20:15:29 +00003157rib_weed_table (struct route_table *table)
3158{
3159 struct route_node *rn;
3160 struct rib *rib;
3161 struct rib *next;
3162
3163 if (table)
3164 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003165 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
paul718e3742002-12-13 20:15:29 +00003166 {
Paul Jakma6d691122006-07-27 21:49:00 +00003167 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3168 continue;
3169
paulb21b19c2003-06-15 01:28:29 +00003170 if (rib->table != zebrad.rtm_table_default &&
paul718e3742002-12-13 20:15:29 +00003171 rib->table != RT_TABLE_MAIN)
paul4d38fdb2005-04-28 17:35:14 +00003172 rib_delnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00003173 }
3174}
3175
3176/* Delete all routes from non main table. */
3177void
paula1ac18c2005-06-28 17:17:12 +00003178rib_weed_tables (void)
paul718e3742002-12-13 20:15:29 +00003179{
3180 rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
3181 rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
3182}
3183
3184/* Delete self installed routes after zebra is relaunched. */
paula1ac18c2005-06-28 17:17:12 +00003185static void
paul718e3742002-12-13 20:15:29 +00003186rib_sweep_table (struct route_table *table)
3187{
3188 struct route_node *rn;
3189 struct rib *rib;
3190 struct rib *next;
3191 int ret = 0;
3192
3193 if (table)
3194 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003195 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
paul718e3742002-12-13 20:15:29 +00003196 {
Paul Jakma6d691122006-07-27 21:49:00 +00003197 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3198 continue;
3199
paul718e3742002-12-13 20:15:29 +00003200 if (rib->type == ZEBRA_ROUTE_KERNEL &&
3201 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
3202 {
3203 ret = rib_uninstall_kernel (rn, rib);
3204 if (! ret)
paul4d38fdb2005-04-28 17:35:14 +00003205 rib_delnode (rn, rib);
paul718e3742002-12-13 20:15:29 +00003206 }
3207 }
3208}
3209
3210/* Sweep all RIB tables. */
3211void
paula1ac18c2005-06-28 17:17:12 +00003212rib_sweep_route (void)
paul718e3742002-12-13 20:15:29 +00003213{
3214 rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
3215 rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
3216}
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04003217
3218/* Remove specific by protocol routes from 'table'. */
3219static unsigned long
3220rib_score_proto_table (u_char proto, struct route_table *table)
3221{
3222 struct route_node *rn;
3223 struct rib *rib;
3224 struct rib *next;
3225 unsigned long n = 0;
3226
3227 if (table)
3228 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003229 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04003230 {
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04003231 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3232 continue;
3233 if (rib->type == proto)
3234 {
3235 rib_delnode (rn, rib);
3236 n++;
3237 }
3238 }
3239
3240 return n;
3241}
3242
3243/* Remove specific by protocol routes. */
3244unsigned long
3245rib_score_proto (u_char proto)
3246{
3247 return rib_score_proto_table (proto, vrf_table (AFI_IP, SAFI_UNICAST, 0))
3248 +rib_score_proto_table (proto, vrf_table (AFI_IP6, SAFI_UNICAST, 0));
3249}
3250
paul718e3742002-12-13 20:15:29 +00003251/* Close RIB and clean up kernel routes. */
paula1ac18c2005-06-28 17:17:12 +00003252static void
paul718e3742002-12-13 20:15:29 +00003253rib_close_table (struct route_table *table)
3254{
3255 struct route_node *rn;
3256 struct rib *rib;
3257
3258 if (table)
3259 for (rn = route_top (table); rn; rn = route_next (rn))
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003260 RNODE_FOREACH_RIB (rn, rib)
Paul Jakma6d691122006-07-27 21:49:00 +00003261 {
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003262 if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
3263 continue;
3264
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00003265 zfpm_trigger_update (rn, NULL);
3266
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +00003267 if (! RIB_SYSTEM_ROUTE (rib))
3268 rib_uninstall_kernel (rn, rib);
Paul Jakma6d691122006-07-27 21:49:00 +00003269 }
paul718e3742002-12-13 20:15:29 +00003270}
3271
3272/* Close all RIB tables. */
3273void
paula1ac18c2005-06-28 17:17:12 +00003274rib_close (void)
paul718e3742002-12-13 20:15:29 +00003275{
3276 rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
3277 rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
3278}
3279
3280/* Routing information base initialize. */
3281void
paula1ac18c2005-06-28 17:17:12 +00003282rib_init (void)
paul718e3742002-12-13 20:15:29 +00003283{
paul4d38fdb2005-04-28 17:35:14 +00003284 rib_queue_init (&zebrad);
paul718e3742002-12-13 20:15:29 +00003285 /* VRF initialization. */
3286 vrf_init ();
3287}
Avneesh Sachdev0915bb02012-11-13 22:48:55 +00003288
3289/*
3290 * vrf_id_get_next
3291 *
3292 * Get the first vrf id that is greater than the given vrf id if any.
3293 *
3294 * Returns TRUE if a vrf id was found, FALSE otherwise.
3295 */
3296static inline int
3297vrf_id_get_next (uint32_t id, uint32_t *next_id_p)
3298{
3299 while (++id < vector_active (vrf_vector))
3300 {
3301 if (vrf_lookup (id))
3302 {
3303 *next_id_p = id;
3304 return 1;
3305 }
3306 }
3307
3308 return 0;
3309}
3310
3311/*
3312 * rib_tables_iter_next
3313 *
3314 * Returns the next table in the iteration.
3315 */
3316struct route_table *
3317rib_tables_iter_next (rib_tables_iter_t *iter)
3318{
3319 struct route_table *table;
3320
3321 /*
3322 * Array that helps us go over all AFI/SAFI combinations via one
3323 * index.
3324 */
3325 static struct {
3326 afi_t afi;
3327 safi_t safi;
3328 } afi_safis[] = {
3329 { AFI_IP, SAFI_UNICAST },
3330 { AFI_IP, SAFI_MULTICAST },
3331 { AFI_IP6, SAFI_UNICAST },
3332 { AFI_IP6, SAFI_MULTICAST },
3333 };
3334
3335 table = NULL;
3336
3337 switch (iter->state)
3338 {
3339
3340 case RIB_TABLES_ITER_S_INIT:
3341 iter->vrf_id = 0;
3342 iter->afi_safi_ix = -1;
3343
3344 /* Fall through */
3345
3346 case RIB_TABLES_ITER_S_ITERATING:
3347 iter->afi_safi_ix++;
3348 while (1)
3349 {
3350
3351 while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
3352 {
3353 table = vrf_table (afi_safis[iter->afi_safi_ix].afi,
3354 afi_safis[iter->afi_safi_ix].safi,
3355 iter->vrf_id);
3356 if (table)
3357 break;
3358
3359 iter->afi_safi_ix++;
3360 }
3361
3362 /*
3363 * Found another table in this vrf.
3364 */
3365 if (table)
3366 break;
3367
3368 /*
3369 * Done with all tables in the current vrf, go to the next
3370 * one.
3371 */
3372 if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
3373 break;
3374
3375 iter->afi_safi_ix = 0;
3376 }
3377
3378 break;
3379
3380 case RIB_TABLES_ITER_S_DONE:
3381 return NULL;
3382 }
3383
3384 if (table)
3385 iter->state = RIB_TABLES_ITER_S_ITERATING;
3386 else
3387 iter->state = RIB_TABLES_ITER_S_DONE;
3388
3389 return table;
3390}