blob: 70fc0b247f9195e6938bdfad38e1cd3841c458bb [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Routing Table functions.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "table.h"
27#include "memory.h"
28#include "sockunion.h"
29
30void route_node_delete (struct route_node *);
31void route_table_free (struct route_table *);
32
33struct route_table *
34route_table_init (void)
35{
36 struct route_table *rt;
37
38 rt = XCALLOC (MTYPE_ROUTE_TABLE, sizeof (struct route_table));
39 return rt;
40}
41
42void
43route_table_finish (struct route_table *rt)
44{
45 route_table_free (rt);
46}
47
48/* Allocate new route node. */
paul8cc41982005-05-06 21:25:49 +000049static struct route_node *
50route_node_new (void)
paul718e3742002-12-13 20:15:29 +000051{
52 struct route_node *node;
53 node = XCALLOC (MTYPE_ROUTE_NODE, sizeof (struct route_node));
54 return node;
55}
56
57/* Allocate new route node with prefix set. */
paul8cc41982005-05-06 21:25:49 +000058static struct route_node *
paul718e3742002-12-13 20:15:29 +000059route_node_set (struct route_table *table, struct prefix *prefix)
60{
61 struct route_node *node;
62
63 node = route_node_new ();
64
65 prefix_copy (&node->p, prefix);
66 node->table = table;
67
68 return node;
69}
70
71/* Free route node. */
paul8cc41982005-05-06 21:25:49 +000072static void
paul718e3742002-12-13 20:15:29 +000073route_node_free (struct route_node *node)
74{
75 XFREE (MTYPE_ROUTE_NODE, node);
76}
77
78/* Free route table. */
79void
80route_table_free (struct route_table *rt)
81{
82 struct route_node *tmp_node;
83 struct route_node *node;
84
85 if (rt == NULL)
86 return;
87
88 node = rt->top;
89
90 while (node)
91 {
92 if (node->l_left)
93 {
94 node = node->l_left;
95 continue;
96 }
97
98 if (node->l_right)
99 {
100 node = node->l_right;
101 continue;
102 }
103
104 tmp_node = node;
105 node = node->parent;
106
107 if (node != NULL)
108 {
109 if (node->l_left == tmp_node)
110 node->l_left = NULL;
111 else
112 node->l_right = NULL;
113
114 route_node_free (tmp_node);
115 }
116 else
117 {
118 route_node_free (tmp_node);
119 break;
120 }
121 }
122
123 XFREE (MTYPE_ROUTE_TABLE, rt);
124 return;
125}
126
127/* Utility mask array. */
128static u_char maskbit[] =
129{
130 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
131};
132
133/* Common prefix route genaration. */
134static void
135route_common (struct prefix *n, struct prefix *p, struct prefix *new)
136{
137 int i;
138 u_char diff;
139 u_char mask;
140
141 u_char *np = (u_char *)&n->u.prefix;
142 u_char *pp = (u_char *)&p->u.prefix;
143 u_char *newp = (u_char *)&new->u.prefix;
144
145 for (i = 0; i < p->prefixlen / 8; i++)
146 {
147 if (np[i] == pp[i])
148 newp[i] = np[i];
149 else
150 break;
151 }
152
153 new->prefixlen = i * 8;
154
155 if (new->prefixlen != p->prefixlen)
156 {
157 diff = np[i] ^ pp[i];
158 mask = 0x80;
159 while (new->prefixlen < p->prefixlen && !(mask & diff))
160 {
161 mask >>= 1;
162 new->prefixlen++;
163 }
164 newp[i] = np[i] & maskbit[new->prefixlen % 8];
165 }
166}
167
168/* Macro version of check_bit (). */
169#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
170
171/* Check bit of the prefix. */
172static int
173check_bit (u_char *prefix, u_char prefixlen)
174{
175 int offset;
176 int shift;
177 u_char *p = (u_char *)prefix;
178
179 assert (prefixlen <= 128);
180
181 offset = prefixlen / 8;
182 shift = 7 - (prefixlen % 8);
183
184 return (p[offset] >> shift & 1);
185}
186
187/* Macro version of set_link (). */
paul9bca8eb2003-05-29 17:18:47 +0000188#define SET_LINK(X,Y) do { (X)->link[CHECK_BIT(&(Y)->p.u.prefix,(X)->p.prefixlen)] = (Y);\
189 (Y)->parent = (X); } while (0)
paul718e3742002-12-13 20:15:29 +0000190
191static void
192set_link (struct route_node *node, struct route_node *new)
193{
194 int bit;
195
196 bit = check_bit (&new->p.u.prefix, node->p.prefixlen);
197
198 assert (bit == 0 || bit == 1);
199
200 node->link[bit] = new;
201 new->parent = node;
202}
203
204/* Lock node. */
205struct route_node *
206route_lock_node (struct route_node *node)
207{
208 node->lock++;
209 return node;
210}
211
212/* Unlock node. */
213void
214route_unlock_node (struct route_node *node)
215{
216 node->lock--;
217
218 if (node->lock == 0)
219 route_node_delete (node);
220}
221
paul718e3742002-12-13 20:15:29 +0000222/* Find matched prefix. */
223struct route_node *
224route_node_match (struct route_table *table, struct prefix *p)
225{
226 struct route_node *node;
227 struct route_node *matched;
228
229 matched = NULL;
230 node = table->top;
231
232 /* Walk down tree. If there is matched route then store it to
233 matched. */
234 while (node && node->p.prefixlen <= p->prefixlen &&
235 prefix_match (&node->p, p))
236 {
237 if (node->info)
238 matched = node;
239 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
240 }
241
242 /* If matched route found, return it. */
243 if (matched)
244 return route_lock_node (matched);
245
246 return NULL;
247}
248
249struct route_node *
250route_node_match_ipv4 (struct route_table *table, struct in_addr *addr)
251{
252 struct prefix_ipv4 p;
253
254 memset (&p, 0, sizeof (struct prefix_ipv4));
255 p.family = AF_INET;
256 p.prefixlen = IPV4_MAX_PREFIXLEN;
257 p.prefix = *addr;
258
259 return route_node_match (table, (struct prefix *) &p);
260}
261
262#ifdef HAVE_IPV6
263struct route_node *
264route_node_match_ipv6 (struct route_table *table, struct in6_addr *addr)
265{
266 struct prefix_ipv6 p;
267
268 memset (&p, 0, sizeof (struct prefix_ipv6));
269 p.family = AF_INET6;
270 p.prefixlen = IPV6_MAX_PREFIXLEN;
271 p.prefix = *addr;
272
273 return route_node_match (table, (struct prefix *) &p);
274}
275#endif /* HAVE_IPV6 */
276
277/* Lookup same prefix node. Return NULL when we can't find route. */
278struct route_node *
279route_node_lookup (struct route_table *table, struct prefix *p)
280{
281 struct route_node *node;
282
283 node = table->top;
284
285 while (node && node->p.prefixlen <= p->prefixlen &&
286 prefix_match (&node->p, p))
287 {
288 if (node->p.prefixlen == p->prefixlen && node->info)
289 return route_lock_node (node);
290
291 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
292 }
293
294 return NULL;
295}
296
297/* Add node to routing table. */
298struct route_node *
299route_node_get (struct route_table *table, struct prefix *p)
300{
301 struct route_node *new;
302 struct route_node *node;
303 struct route_node *match;
304
305 match = NULL;
306 node = table->top;
307 while (node && node->p.prefixlen <= p->prefixlen &&
308 prefix_match (&node->p, p))
309 {
310 if (node->p.prefixlen == p->prefixlen)
311 {
312 route_lock_node (node);
313 return node;
314 }
315 match = node;
316 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
317 }
318
319 if (node == NULL)
320 {
321 new = route_node_set (table, p);
322 if (match)
323 set_link (match, new);
324 else
325 table->top = new;
326 }
327 else
328 {
329 new = route_node_new ();
330 route_common (&node->p, p, &new->p);
331 new->p.family = p->family;
332 new->table = table;
333 set_link (new, node);
334
335 if (match)
336 set_link (match, new);
337 else
338 table->top = new;
339
340 if (new->p.prefixlen != p->prefixlen)
341 {
342 match = new;
343 new = route_node_set (table, p);
344 set_link (match, new);
345 }
346 }
347 route_lock_node (new);
348
349 return new;
350}
351
352/* Delete node from the routing table. */
353void
354route_node_delete (struct route_node *node)
355{
356 struct route_node *child;
357 struct route_node *parent;
358
359 assert (node->lock == 0);
360 assert (node->info == NULL);
361
362 if (node->l_left && node->l_right)
363 return;
364
365 if (node->l_left)
366 child = node->l_left;
367 else
368 child = node->l_right;
369
370 parent = node->parent;
371
372 if (child)
373 child->parent = parent;
374
375 if (parent)
376 {
377 if (parent->l_left == node)
378 parent->l_left = child;
379 else
380 parent->l_right = child;
381 }
382 else
383 node->table->top = child;
384
385 route_node_free (node);
386
387 /* If parent node is stub then delete it also. */
388 if (parent && parent->lock == 0)
389 route_node_delete (parent);
390}
391
392/* Get fist node and lock it. This function is useful when one want
393 to lookup all the node exist in the routing table. */
394struct route_node *
395route_top (struct route_table *table)
396{
397 /* If there is no node in the routing table return NULL. */
398 if (table->top == NULL)
399 return NULL;
400
401 /* Lock the top node and return it. */
402 route_lock_node (table->top);
403 return table->top;
404}
405
406/* Unlock current node and lock next node then return it. */
407struct route_node *
408route_next (struct route_node *node)
409{
410 struct route_node *next;
411 struct route_node *start;
412
413 /* Node may be deleted from route_unlock_node so we have to preserve
414 next node's pointer. */
415
416 if (node->l_left)
417 {
418 next = node->l_left;
419 route_lock_node (next);
420 route_unlock_node (node);
421 return next;
422 }
423 if (node->l_right)
424 {
425 next = node->l_right;
426 route_lock_node (next);
427 route_unlock_node (node);
428 return next;
429 }
430
431 start = node;
432 while (node->parent)
433 {
434 if (node->parent->l_left == node && node->parent->l_right)
435 {
436 next = node->parent->l_right;
437 route_lock_node (next);
438 route_unlock_node (start);
439 return next;
440 }
441 node = node->parent;
442 }
443 route_unlock_node (start);
444 return NULL;
445}
446
447/* Unlock current node and lock next node until limit. */
448struct route_node *
449route_next_until (struct route_node *node, struct route_node *limit)
450{
451 struct route_node *next;
452 struct route_node *start;
453
454 /* Node may be deleted from route_unlock_node so we have to preserve
455 next node's pointer. */
456
457 if (node->l_left)
458 {
459 next = node->l_left;
460 route_lock_node (next);
461 route_unlock_node (node);
462 return next;
463 }
464 if (node->l_right)
465 {
466 next = node->l_right;
467 route_lock_node (next);
468 route_unlock_node (node);
469 return next;
470 }
471
472 start = node;
473 while (node->parent && node != limit)
474 {
475 if (node->parent->l_left == node && node->parent->l_right)
476 {
477 next = node->parent->l_right;
478 route_lock_node (next);
479 route_unlock_node (start);
480 return next;
481 }
482 node = node->parent;
483 }
484 route_unlock_node (start);
485 return NULL;
486}