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 static void bgp_node_delete (struct bgp_node
*);
32 static void bgp_table_free (struct bgp_table
*);
35 bgp_table_init (afi_t afi
, safi_t safi
)
39 rt
= XMALLOC (MTYPE_BGP_TABLE
, sizeof (struct bgp_table
));
40 memset (rt
, 0, sizeof (struct bgp_table
));
42 rt
->type
= BGP_TABLE_MAIN
;
50 bgp_table_finish (struct bgp_table
**rt
)
56 static struct bgp_node
*
61 rn
= (struct bgp_node
*) XMALLOC (MTYPE_BGP_NODE
, sizeof (struct bgp_node
));
62 memset (rn
, 0, sizeof (struct bgp_node
));
66 /* Allocate new route node with prefix set. */
67 static struct bgp_node
*
68 bgp_node_set (struct bgp_table
*table
, struct prefix
*prefix
)
70 struct bgp_node
*node
;
72 node
= bgp_node_create ();
74 prefix_copy (&node
->p
, prefix
);
80 /* Free route node. */
82 bgp_node_free (struct bgp_node
*node
)
84 XFREE (MTYPE_BGP_NODE
, node
);
87 /* Free route table. */
89 bgp_table_free (struct bgp_table
*rt
)
91 struct bgp_node
*tmp_node
;
92 struct bgp_node
*node
;
109 node
= node
->l_right
;
118 if (node
->l_left
== tmp_node
)
121 node
->l_right
= NULL
;
123 bgp_node_free (tmp_node
);
127 bgp_node_free (tmp_node
);
132 XFREE (MTYPE_BGP_TABLE
, rt
);
136 /* Utility mask array. */
137 static u_char maskbit
[] =
139 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
142 /* Common prefix route genaration. */
144 route_common (struct prefix
*n
, struct prefix
*p
, struct prefix
*new)
150 u_char
*np
= (u_char
*)&n
->u
.prefix
;
151 u_char
*pp
= (u_char
*)&p
->u
.prefix
;
152 u_char
*newp
= (u_char
*)&new->u
.prefix
;
154 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
162 new->prefixlen
= i
* 8;
164 if (new->prefixlen
!= p
->prefixlen
)
166 diff
= np
[i
] ^ pp
[i
];
168 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
173 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
177 /* Macro version of check_bit (). */
178 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
180 /* Check bit of the prefix. */
182 check_bit (u_char
*prefix
, u_char prefixlen
)
186 u_char
*p
= (u_char
*)prefix
;
188 assert (prefixlen
<= 128);
190 offset
= prefixlen
/ 8;
191 shift
= 7 - (prefixlen
% 8);
193 return (p
[offset
] >> shift
& 1);
196 /* Macro version of set_link (). */
197 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
201 set_link (struct bgp_node
*node
, struct bgp_node
*new)
205 bit
= check_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
207 assert (bit
== 0 || bit
== 1);
209 node
->link
[bit
] = new;
215 bgp_lock_node (struct bgp_node
*node
)
223 bgp_unlock_node (struct bgp_node
*node
)
228 bgp_node_delete (node
);
231 /* Find matched prefix. */
233 bgp_node_match (const struct bgp_table
*table
, struct prefix
*p
)
235 struct bgp_node
*node
;
236 struct bgp_node
*matched
;
241 /* Walk down tree. If there is matched route then store it to
243 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
244 prefix_match (&node
->p
, p
))
248 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
251 /* If matched route found, return it. */
253 return bgp_lock_node (matched
);
259 bgp_node_match_ipv4 (const struct bgp_table
*table
, struct in_addr
*addr
)
261 struct prefix_ipv4 p
;
263 memset (&p
, 0, sizeof (struct prefix_ipv4
));
265 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
268 return bgp_node_match (table
, (struct prefix
*) &p
);
273 bgp_node_match_ipv6 (const struct bgp_table
*table
, struct in6_addr
*addr
)
275 struct prefix_ipv6 p
;
277 memset (&p
, 0, sizeof (struct prefix_ipv6
));
279 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
282 return bgp_node_match (table
, (struct prefix
*) &p
);
284 #endif /* HAVE_IPV6 */
286 /* Lookup same prefix node. Return NULL when we can't find route. */
288 bgp_node_lookup (const struct bgp_table
*table
, struct prefix
*p
)
290 struct bgp_node
*node
;
294 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
295 prefix_match (&node
->p
, p
))
297 if (node
->p
.prefixlen
== p
->prefixlen
&& node
->info
)
298 return bgp_lock_node (node
);
300 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
306 /* Add node to routing table. */
308 bgp_node_get (struct bgp_table
*const table
, struct prefix
*p
)
310 struct bgp_node
*new;
311 struct bgp_node
*node
;
312 struct bgp_node
*match
;
316 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
317 prefix_match (&node
->p
, p
))
319 if (node
->p
.prefixlen
== p
->prefixlen
)
321 bgp_lock_node (node
);
325 node
= node
->link
[check_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
330 new = bgp_node_set (table
, p
);
332 set_link (match
, new);
338 new = bgp_node_create ();
339 route_common (&node
->p
, p
, &new->p
);
340 new->p
.family
= p
->family
;
342 set_link (new, node
);
345 set_link (match
, new);
349 if (new->p
.prefixlen
!= p
->prefixlen
)
352 new = bgp_node_set (table
, p
);
353 set_link (match
, new);
363 /* Delete node from the routing table. */
365 bgp_node_delete (struct bgp_node
*node
)
367 struct bgp_node
*child
;
368 struct bgp_node
*parent
;
370 assert (node
->lock
== 0);
371 assert (node
->info
== NULL
);
373 if (node
->l_left
&& node
->l_right
)
377 child
= node
->l_left
;
379 child
= node
->l_right
;
381 parent
= node
->parent
;
384 child
->parent
= parent
;
388 if (parent
->l_left
== node
)
389 parent
->l_left
= child
;
391 parent
->l_right
= child
;
394 node
->table
->top
= child
;
396 node
->table
->count
--;
398 bgp_node_free (node
);
400 /* If parent node is stub then delete it also. */
401 if (parent
&& parent
->lock
== 0)
402 bgp_node_delete (parent
);
405 /* Get fist node and lock it. This function is useful when one want
406 to lookup all the node exist in the routing table. */
408 bgp_table_top (const struct bgp_table
*const table
)
410 /* If there is no node in the routing table return NULL. */
411 if (table
->top
== NULL
)
414 /* Lock the top node and return it. */
415 bgp_lock_node (table
->top
);
419 /* Unlock current node and lock next node then return it. */
421 bgp_route_next (struct bgp_node
*node
)
423 struct bgp_node
*next
;
424 struct bgp_node
*start
;
426 /* Node may be deleted from bgp_unlock_node so we have to preserve
427 next node's pointer. */
432 bgp_lock_node (next
);
433 bgp_unlock_node (node
);
438 next
= node
->l_right
;
439 bgp_lock_node (next
);
440 bgp_unlock_node (node
);
447 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
449 next
= node
->parent
->l_right
;
450 bgp_lock_node (next
);
451 bgp_unlock_node (start
);
456 bgp_unlock_node (start
);
460 /* Unlock current node and lock next node until limit. */
462 bgp_route_next_until (struct bgp_node
*node
, struct bgp_node
*limit
)
464 struct bgp_node
*next
;
465 struct bgp_node
*start
;
467 /* Node may be deleted from bgp_unlock_node so we have to preserve
468 next node's pointer. */
473 bgp_lock_node (next
);
474 bgp_unlock_node (node
);
479 next
= node
->l_right
;
480 bgp_lock_node (next
);
481 bgp_unlock_node (node
);
486 while (node
->parent
&& node
!= limit
)
488 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
490 next
= node
->parent
->l_right
;
491 bgp_lock_node (next
);
492 bgp_unlock_node (start
);
497 bgp_unlock_node (start
);
502 bgp_table_count (const struct bgp_table
*table
)