]> git.proxmox.com Git - mirror_frr.git/blob - lib/table.c
Merge branch 'master' of ssh://code.quagga.net/var/lib/git/quagga
[mirror_frr.git] / lib / table.c
1 /*
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
30 void route_node_delete (struct route_node *);
31 void route_table_free (struct route_table *);
32 \f
33 struct route_table *
34 route_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
42 void
43 route_table_finish (struct route_table *rt)
44 {
45 route_table_free (rt);
46 }
47
48 /* Allocate new route node. */
49 static struct route_node *
50 route_node_new (void)
51 {
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. */
58 static struct route_node *
59 route_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. */
72 static void
73 route_node_free (struct route_node *node)
74 {
75 XFREE (MTYPE_ROUTE_NODE, node);
76 }
77
78 /* Free route table. */
79 void
80 route_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. */
128 static const u_char maskbit[] =
129 {
130 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
131 };
132
133 /* Common prefix route genaration. */
134 static void
135 route_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. */
172 static int
173 check_bit (const u_char *prefix, u_char prefixlen)
174 {
175 unsigned int offset;
176 unsigned int shift;
177 const u_char *p = 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 (). */
188 #define SET_LINK(X,Y) do { (X)->link[CHECK_BIT(&(Y)->p.u.prefix,(X)->p.prefixlen)] = (Y);\
189 (Y)->parent = (X); } while (0)
190
191 static void
192 set_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. */
205 struct route_node *
206 route_lock_node (struct route_node *node)
207 {
208 node->lock++;
209 return node;
210 }
211
212 /* Unlock node. */
213 void
214 route_unlock_node (struct route_node *node)
215 {
216 node->lock--;
217
218 if (node->lock == 0)
219 route_node_delete (node);
220 }
221
222 /* Find matched prefix. */
223 struct route_node *
224 route_node_match (const struct route_table *table, const 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
249 struct route_node *
250 route_node_match_ipv4 (const struct route_table *table,
251 const struct in_addr *addr)
252 {
253 struct prefix_ipv4 p;
254
255 memset (&p, 0, sizeof (struct prefix_ipv4));
256 p.family = AF_INET;
257 p.prefixlen = IPV4_MAX_PREFIXLEN;
258 p.prefix = *addr;
259
260 return route_node_match (table, (struct prefix *) &p);
261 }
262
263 #ifdef HAVE_IPV6
264 struct route_node *
265 route_node_match_ipv6 (const struct route_table *table,
266 const struct in6_addr *addr)
267 {
268 struct prefix_ipv6 p;
269
270 memset (&p, 0, sizeof (struct prefix_ipv6));
271 p.family = AF_INET6;
272 p.prefixlen = IPV6_MAX_PREFIXLEN;
273 p.prefix = *addr;
274
275 return route_node_match (table, (struct prefix *) &p);
276 }
277 #endif /* HAVE_IPV6 */
278
279 /* Lookup same prefix node. Return NULL when we can't find route. */
280 struct route_node *
281 route_node_lookup (struct route_table *table, struct prefix *p)
282 {
283 struct route_node *node;
284
285 node = table->top;
286
287 while (node && node->p.prefixlen <= p->prefixlen &&
288 prefix_match (&node->p, p))
289 {
290 if (node->p.prefixlen == p->prefixlen && node->info)
291 return route_lock_node (node);
292
293 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
294 }
295
296 return NULL;
297 }
298
299 /* Add node to routing table. */
300 struct route_node *
301 route_node_get (struct route_table *table, struct prefix *p)
302 {
303 struct route_node *new;
304 struct route_node *node;
305 struct route_node *match;
306
307 match = NULL;
308 node = table->top;
309 while (node && node->p.prefixlen <= p->prefixlen &&
310 prefix_match (&node->p, p))
311 {
312 if (node->p.prefixlen == p->prefixlen)
313 {
314 route_lock_node (node);
315 return node;
316 }
317 match = node;
318 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
319 }
320
321 if (node == NULL)
322 {
323 new = route_node_set (table, p);
324 if (match)
325 set_link (match, new);
326 else
327 table->top = new;
328 }
329 else
330 {
331 new = route_node_new ();
332 route_common (&node->p, p, &new->p);
333 new->p.family = p->family;
334 new->table = table;
335 set_link (new, node);
336
337 if (match)
338 set_link (match, new);
339 else
340 table->top = new;
341
342 if (new->p.prefixlen != p->prefixlen)
343 {
344 match = new;
345 new = route_node_set (table, p);
346 set_link (match, new);
347 }
348 }
349 route_lock_node (new);
350
351 return new;
352 }
353
354 /* Delete node from the routing table. */
355 void
356 route_node_delete (struct route_node *node)
357 {
358 struct route_node *child;
359 struct route_node *parent;
360
361 assert (node->lock == 0);
362 assert (node->info == NULL);
363
364 if (node->l_left && node->l_right)
365 return;
366
367 if (node->l_left)
368 child = node->l_left;
369 else
370 child = node->l_right;
371
372 parent = node->parent;
373
374 if (child)
375 child->parent = parent;
376
377 if (parent)
378 {
379 if (parent->l_left == node)
380 parent->l_left = child;
381 else
382 parent->l_right = child;
383 }
384 else
385 node->table->top = child;
386
387 route_node_free (node);
388
389 /* If parent node is stub then delete it also. */
390 if (parent && parent->lock == 0)
391 route_node_delete (parent);
392 }
393
394 /* Get fist node and lock it. This function is useful when one want
395 to lookup all the node exist in the routing table. */
396 struct route_node *
397 route_top (struct route_table *table)
398 {
399 /* If there is no node in the routing table return NULL. */
400 if (table->top == NULL)
401 return NULL;
402
403 /* Lock the top node and return it. */
404 route_lock_node (table->top);
405 return table->top;
406 }
407
408 /* Unlock current node and lock next node then return it. */
409 struct route_node *
410 route_next (struct route_node *node)
411 {
412 struct route_node *next;
413 struct route_node *start;
414
415 /* Node may be deleted from route_unlock_node so we have to preserve
416 next node's pointer. */
417
418 if (node->l_left)
419 {
420 next = node->l_left;
421 route_lock_node (next);
422 route_unlock_node (node);
423 return next;
424 }
425 if (node->l_right)
426 {
427 next = node->l_right;
428 route_lock_node (next);
429 route_unlock_node (node);
430 return next;
431 }
432
433 start = node;
434 while (node->parent)
435 {
436 if (node->parent->l_left == node && node->parent->l_right)
437 {
438 next = node->parent->l_right;
439 route_lock_node (next);
440 route_unlock_node (start);
441 return next;
442 }
443 node = node->parent;
444 }
445 route_unlock_node (start);
446 return NULL;
447 }
448
449 /* Unlock current node and lock next node until limit. */
450 struct route_node *
451 route_next_until (struct route_node *node, struct route_node *limit)
452 {
453 struct route_node *next;
454 struct route_node *start;
455
456 /* Node may be deleted from route_unlock_node so we have to preserve
457 next node's pointer. */
458
459 if (node->l_left)
460 {
461 next = node->l_left;
462 route_lock_node (next);
463 route_unlock_node (node);
464 return next;
465 }
466 if (node->l_right)
467 {
468 next = node->l_right;
469 route_lock_node (next);
470 route_unlock_node (node);
471 return next;
472 }
473
474 start = node;
475 while (node->parent && node != limit)
476 {
477 if (node->parent->l_left == node && node->parent->l_right)
478 {
479 next = node->parent->l_right;
480 route_lock_node (next);
481 route_unlock_node (start);
482 return next;
483 }
484 node = node->parent;
485 }
486 route_unlock_node (start);
487 return NULL;
488 }