2 Copyright (C) 1998, 2001 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
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
25 #include "sockunion.h"
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_table.h"
31 void bgp_node_delete (struct bgp_node
*);
32 void bgp_table_free (struct bgp_table
*);
39 rt
= XMALLOC (MTYPE_BGP_TABLE
, sizeof (struct bgp_table
));
40 memset (rt
, 0, sizeof (struct bgp_table
));
45 bgp_table_finish (struct bgp_table
*rt
)
55 rn
= (struct bgp_node
*) XMALLOC (MTYPE_BGP_NODE
, sizeof (struct bgp_node
));
56 memset (rn
, 0, sizeof (struct bgp_node
));
60 /* Allocate new route node with prefix set. */
62 bgp_node_set (struct bgp_table
*table
, struct prefix
*prefix
)
64 struct bgp_node
*node
;
66 node
= bgp_node_create ();
68 prefix_copy (&node
->p
, prefix
);
74 /* Free route node. */
76 bgp_node_free (struct bgp_node
*node
)
78 XFREE (MTYPE_BGP_NODE
, node
);
81 /* Free route table. */
83 bgp_table_free (struct bgp_table
*rt
)
85 struct bgp_node
*tmp_node
;
86 struct bgp_node
*node
;
103 node
= node
->l_right
;
112 if (node
->l_left
== tmp_node
)
115 node
->l_right
= NULL
;
117 bgp_node_free (tmp_node
);
121 bgp_node_free (tmp_node
);
126 XFREE (MTYPE_BGP_TABLE
, rt
);
130 /* Utility mask array. */
131 static u_char maskbit
[] =
133 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
136 /* Common prefix route genaration. */
138 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
144 u_char
*np
= (u_char
*)&n
->u
.prefix
;
145 u_char
*pp
= (u_char
*)&p
->u
.prefix
;
146 u_char
*newp
= (u_char
*)&new->u
.prefix
;
148 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
156 new->prefixlen
= i
* 8;
158 if (new->prefixlen
!= p
->prefixlen
)
160 diff
= np
[i
] ^ pp
[i
];
162 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
167 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
171 /* Macro version of check_bit (). */
172 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
174 /* Check bit of the prefix. */
176 check_bit (u_char
*prefix
, u_char prefixlen
)
180 u_char
*p
= (u_char
*)prefix
;
182 assert (prefixlen
<= 128);
184 offset
= prefixlen
/ 8;
185 shift
= 7 - (prefixlen
% 8);
187 return (p
[offset
] >> shift
& 1);
190 /* Macro version of set_link (). */
191 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
195 set_link (struct bgp_node
*node
, struct bgp_node
*new)
199 bit
= check_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
201 assert (bit
== 0 || bit
== 1);
203 node
->link
[bit
] = new;
209 bgp_lock_node (struct bgp_node
*node
)
217 bgp_unlock_node (struct bgp_node
*node
)
222 bgp_node_delete (node
);
225 /* Find matched prefix. */
227 bgp_node_match (struct bgp_table
*table
, struct prefix
*p
)
229 struct bgp_node
*node
;
230 struct bgp_node
*matched
;
235 /* Walk down tree. If there is matched route then store it to
237 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
238 prefix_match (&node
->p
, p
))
242 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
245 /* If matched route found, return it. */
247 return bgp_lock_node (matched
);
253 bgp_node_match_ipv4 (struct bgp_table
*table
, struct in_addr
*addr
)
255 struct prefix_ipv4 p
;
257 memset (&p
, 0, sizeof (struct prefix_ipv4
));
259 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
262 return bgp_node_match (table
, (struct prefix
*) &p
);
267 bgp_node_match_ipv6 (struct bgp_table
*table
, struct in6_addr
*addr
)
269 struct prefix_ipv6 p
;
271 memset (&p
, 0, sizeof (struct prefix_ipv6
));
273 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
276 return bgp_node_match (table
, (struct prefix
*) &p
);
278 #endif /* HAVE_IPV6 */
280 /* Lookup same prefix node. Return NULL when we can't find route. */
282 bgp_node_lookup (struct bgp_table
*table
, struct prefix
*p
)
284 struct bgp_node
*node
;
288 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
289 prefix_match (&node
->p
, p
))
291 if (node
->p
.prefixlen
== p
->prefixlen
&& node
->info
)
292 return bgp_lock_node (node
);
294 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
300 /* Add node to routing table. */
302 bgp_node_get (struct bgp_table
*table
, struct prefix
*p
)
304 struct bgp_node
*new;
305 struct bgp_node
*node
;
306 struct bgp_node
*match
;
310 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
311 prefix_match (&node
->p
, p
))
313 if (node
->p
.prefixlen
== p
->prefixlen
)
315 bgp_lock_node (node
);
319 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
324 new = bgp_node_set (table
, p
);
326 set_link (match
, new);
332 new = bgp_node_create ();
333 route_common (&node
->p
, p
, &new->p
);
334 new->p
.family
= p
->family
;
336 set_link (new, node
);
339 set_link (match
, new);
343 if (new->p
.prefixlen
!= p
->prefixlen
)
346 new = bgp_node_set (table
, p
);
347 set_link (match
, new);
355 /* Delete node from the routing table. */
357 bgp_node_delete (struct bgp_node
*node
)
359 struct bgp_node
*child
;
360 struct bgp_node
*parent
;
362 assert (node
->lock
== 0);
363 assert (node
->info
== NULL
);
365 if (node
->l_left
&& node
->l_right
)
369 child
= node
->l_left
;
371 child
= node
->l_right
;
373 parent
= node
->parent
;
376 child
->parent
= parent
;
380 if (parent
->l_left
== node
)
381 parent
->l_left
= child
;
383 parent
->l_right
= child
;
386 node
->table
->top
= child
;
388 bgp_node_free (node
);
390 /* If parent node is stub then delete it also. */
391 if (parent
&& parent
->lock
== 0)
392 bgp_node_delete (parent
);
395 /* Get fist node and lock it. This function is useful when one want
396 to lookup all the node exist in the routing table. */
398 bgp_table_top (struct bgp_table
*table
)
400 /* If there is no node in the routing table return NULL. */
401 if (table
->top
== NULL
)
404 /* Lock the top node and return it. */
405 bgp_lock_node (table
->top
);
409 /* Unlock current node and lock next node then return it. */
411 bgp_route_next (struct bgp_node
*node
)
413 struct bgp_node
*next
;
414 struct bgp_node
*start
;
416 /* Node may be deleted from bgp_unlock_node so we have to preserve
417 next node's pointer. */
422 bgp_lock_node (next
);
423 bgp_unlock_node (node
);
428 next
= node
->l_right
;
429 bgp_lock_node (next
);
430 bgp_unlock_node (node
);
437 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
439 next
= node
->parent
->l_right
;
440 bgp_lock_node (next
);
441 bgp_unlock_node (start
);
446 bgp_unlock_node (start
);
450 /* Unlock current node and lock next node until limit. */
452 bgp_route_next_until (struct bgp_node
*node
, struct bgp_node
*limit
)
454 struct bgp_node
*next
;
455 struct bgp_node
*start
;
457 /* Node may be deleted from bgp_unlock_node so we have to preserve
458 next node's pointer. */
463 bgp_lock_node (next
);
464 bgp_unlock_node (node
);
469 next
= node
->l_right
;
470 bgp_lock_node (next
);
471 bgp_unlock_node (node
);
476 while (node
->parent
&& node
!= limit
)
478 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
480 next
= node
->parent
->l_right
;
481 bgp_lock_node (next
);
482 bgp_unlock_node (start
);
487 bgp_unlock_node (start
);