]>
git.proxmox.com Git - mirror_frr.git/blob - lib/table.c
2 * Routing Table functions.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
28 #include "sockunion.h"
30 void route_node_delete (struct route_node
*);
31 void route_table_free (struct route_table
*);
34 route_table_init (void)
36 struct route_table
*rt
;
38 rt
= XCALLOC (MTYPE_ROUTE_TABLE
, sizeof (struct route_table
));
43 route_table_finish (struct route_table
*rt
)
45 route_table_free (rt
);
48 /* Allocate new route node. */
49 static struct route_node
*
52 struct route_node
*node
;
53 node
= XCALLOC (MTYPE_ROUTE_NODE
, sizeof (struct route_node
));
57 /* Allocate new route node with prefix set. */
58 static struct route_node
*
59 route_node_set (struct route_table
*table
, struct prefix
*prefix
)
61 struct route_node
*node
;
63 node
= route_node_new ();
65 prefix_copy (&node
->p
, prefix
);
71 /* Free route node. */
73 route_node_free (struct route_node
*node
)
75 XFREE (MTYPE_ROUTE_NODE
, node
);
78 /* Free route table. */
80 route_table_free (struct route_table
*rt
)
82 struct route_node
*tmp_node
;
83 struct route_node
*node
;
100 node
= node
->l_right
;
109 if (node
->l_left
== tmp_node
)
112 node
->l_right
= NULL
;
114 route_node_free (tmp_node
);
118 route_node_free (tmp_node
);
123 XFREE (MTYPE_ROUTE_TABLE
, rt
);
127 /* Utility mask array. */
128 static const u_char maskbit
[] =
130 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
133 /* Common prefix route genaration. */
135 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
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
;
145 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
153 new->prefixlen
= i
* 8;
155 if (new->prefixlen
!= p
->prefixlen
)
157 diff
= np
[i
] ^ pp
[i
];
159 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
164 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
169 set_link (struct route_node
*node
, struct route_node
*new)
171 unsigned int bit
= prefix_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
173 node
->link
[bit
] = new;
179 route_lock_node (struct route_node
*node
)
187 route_unlock_node (struct route_node
*node
)
192 route_node_delete (node
);
195 /* Find matched prefix. */
197 route_node_match (const struct route_table
*table
, const struct prefix
*p
)
199 struct route_node
*node
;
200 struct route_node
*matched
;
205 /* Walk down tree. If there is matched route then store it to
207 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
208 prefix_match (&node
->p
, p
))
213 if (node
->p
.prefixlen
== p
->prefixlen
)
216 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
219 /* If matched route found, return it. */
221 return route_lock_node (matched
);
227 route_node_match_ipv4 (const struct route_table
*table
,
228 const struct in_addr
*addr
)
230 struct prefix_ipv4 p
;
232 memset (&p
, 0, sizeof (struct prefix_ipv4
));
234 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
237 return route_node_match (table
, (struct prefix
*) &p
);
242 route_node_match_ipv6 (const struct route_table
*table
,
243 const struct in6_addr
*addr
)
245 struct prefix_ipv6 p
;
247 memset (&p
, 0, sizeof (struct prefix_ipv6
));
249 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
252 return route_node_match (table
, (struct prefix
*) &p
);
254 #endif /* HAVE_IPV6 */
256 /* Lookup same prefix node. Return NULL when we can't find route. */
258 route_node_lookup (struct route_table
*table
, struct prefix
*p
)
260 struct route_node
*node
;
264 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
265 prefix_match (&node
->p
, p
))
267 if (node
->p
.prefixlen
== p
->prefixlen
)
268 return node
->info
? route_lock_node (node
) : NULL
;
270 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
276 /* Add node to routing table. */
278 route_node_get (struct route_table
*table
, struct prefix
*p
)
280 struct route_node
*new;
281 struct route_node
*node
;
282 struct route_node
*match
;
286 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
287 prefix_match (&node
->p
, p
))
289 if (node
->p
.prefixlen
== p
->prefixlen
)
290 return route_lock_node (node
);
293 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
298 new = route_node_set (table
, p
);
300 set_link (match
, new);
306 new = route_node_new ();
307 route_common (&node
->p
, p
, &new->p
);
308 new->p
.family
= p
->family
;
310 set_link (new, node
);
313 set_link (match
, new);
317 if (new->p
.prefixlen
!= p
->prefixlen
)
320 new = route_node_set (table
, p
);
321 set_link (match
, new);
324 route_lock_node (new);
329 /* Delete node from the routing table. */
331 route_node_delete (struct route_node
*node
)
333 struct route_node
*child
;
334 struct route_node
*parent
;
336 assert (node
->lock
== 0);
337 assert (node
->info
== NULL
);
339 if (node
->l_left
&& node
->l_right
)
343 child
= node
->l_left
;
345 child
= node
->l_right
;
347 parent
= node
->parent
;
350 child
->parent
= parent
;
354 if (parent
->l_left
== node
)
355 parent
->l_left
= child
;
357 parent
->l_right
= child
;
360 node
->table
->top
= child
;
362 route_node_free (node
);
364 /* If parent node is stub then delete it also. */
365 if (parent
&& parent
->lock
== 0)
366 route_node_delete (parent
);
369 /* Get fist node and lock it. This function is useful when one want
370 to lookup all the node exist in the routing table. */
372 route_top (struct route_table
*table
)
374 /* If there is no node in the routing table return NULL. */
375 if (table
->top
== NULL
)
378 /* Lock the top node and return it. */
379 route_lock_node (table
->top
);
383 /* Unlock current node and lock next node then return it. */
385 route_next (struct route_node
*node
)
387 struct route_node
*next
;
388 struct route_node
*start
;
390 /* Node may be deleted from route_unlock_node so we have to preserve
391 next node's pointer. */
396 route_lock_node (next
);
397 route_unlock_node (node
);
402 next
= node
->l_right
;
403 route_lock_node (next
);
404 route_unlock_node (node
);
411 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
413 next
= node
->parent
->l_right
;
414 route_lock_node (next
);
415 route_unlock_node (start
);
420 route_unlock_node (start
);
424 /* Unlock current node and lock next node until limit. */
426 route_next_until (struct route_node
*node
, struct route_node
*limit
)
428 struct route_node
*next
;
429 struct route_node
*start
;
431 /* Node may be deleted from route_unlock_node so we have to preserve
432 next node's pointer. */
437 route_lock_node (next
);
438 route_unlock_node (node
);
443 next
= node
->l_right
;
444 route_lock_node (next
);
445 route_unlock_node (node
);
450 while (node
->parent
&& node
!= limit
)
452 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
454 next
= node
->parent
->l_right
;
455 route_lock_node (next
);
456 route_unlock_node (start
);
461 route_unlock_node (start
);