[bgpd] bug #352: IPv6/Multicast address-family config not written out
[jleu-quagga.git] / bgpd / bgp_table.c
blob810dab54e8b007362ae9289f15b63c444240e7f4
1 /* BGP routing table
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "prefix.h"
24 #include "memory.h"
25 #include "sockunion.h"
26 #include "vty.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 *);
34 struct bgp_table *
35 bgp_table_init (afi_t afi, safi_t safi)
37 struct bgp_table *rt;
39 rt = XMALLOC (MTYPE_BGP_TABLE, sizeof (struct bgp_table));
40 memset (rt, 0, sizeof (struct bgp_table));
42 rt->type = BGP_TABLE_MAIN;
43 rt->afi = afi;
44 rt->safi = safi;
46 return rt;
49 void
50 bgp_table_finish (struct bgp_table *rt)
52 bgp_table_free (rt);
55 static struct bgp_node *
56 bgp_node_create ()
58 struct bgp_node *rn;
60 rn = (struct bgp_node *) XMALLOC (MTYPE_BGP_NODE, sizeof (struct bgp_node));
61 memset (rn, 0, sizeof (struct bgp_node));
62 return rn;
65 /* Allocate new route node with prefix set. */
66 static struct bgp_node *
67 bgp_node_set (struct bgp_table *table, struct prefix *prefix)
69 struct bgp_node *node;
71 node = bgp_node_create ();
73 prefix_copy (&node->p, prefix);
74 node->table = table;
76 return node;
79 /* Free route node. */
80 static void
81 bgp_node_free (struct bgp_node *node)
83 XFREE (MTYPE_BGP_NODE, node);
86 /* Free route table. */
87 void
88 bgp_table_free (struct bgp_table *rt)
90 struct bgp_node *tmp_node;
91 struct bgp_node *node;
93 if (rt == NULL)
94 return;
96 node = rt->top;
98 while (node)
100 if (node->l_left)
102 node = node->l_left;
103 continue;
106 if (node->l_right)
108 node = node->l_right;
109 continue;
112 tmp_node = node;
113 node = node->parent;
115 if (node != NULL)
117 if (node->l_left == tmp_node)
118 node->l_left = NULL;
119 else
120 node->l_right = NULL;
122 bgp_node_free (tmp_node);
124 else
126 bgp_node_free (tmp_node);
127 break;
131 XFREE (MTYPE_BGP_TABLE, rt);
132 return;
135 /* Utility mask array. */
136 static u_char maskbit[] =
138 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
141 /* Common prefix route genaration. */
142 static void
143 route_common (struct prefix *n, struct prefix *p, struct prefix *new)
145 int i;
146 u_char diff;
147 u_char mask;
149 u_char *np = (u_char *)&n->u.prefix;
150 u_char *pp = (u_char *)&p->u.prefix;
151 u_char *newp = (u_char *)&new->u.prefix;
153 for (i = 0; i < p->prefixlen / 8; i++)
155 if (np[i] == pp[i])
156 newp[i] = np[i];
157 else
158 break;
161 new->prefixlen = i * 8;
163 if (new->prefixlen != p->prefixlen)
165 diff = np[i] ^ pp[i];
166 mask = 0x80;
167 while (new->prefixlen < p->prefixlen && !(mask & diff))
169 mask >>= 1;
170 new->prefixlen++;
172 newp[i] = np[i] & maskbit[new->prefixlen % 8];
176 /* Macro version of check_bit (). */
177 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
179 /* Check bit of the prefix. */
180 static int
181 check_bit (u_char *prefix, u_char prefixlen)
183 int offset;
184 int shift;
185 u_char *p = (u_char *)prefix;
187 assert (prefixlen <= 128);
189 offset = prefixlen / 8;
190 shift = 7 - (prefixlen % 8);
192 return (p[offset] >> shift & 1);
195 /* Macro version of set_link (). */
196 #define SET_LINK(X,Y) (X)->link[CHECK_BIT(&(Y)->prefix,(X)->prefixlen)] = (Y);\
197 (Y)->parent = (X)
199 static void
200 set_link (struct bgp_node *node, struct bgp_node *new)
202 int bit;
204 bit = check_bit (&new->p.u.prefix, node->p.prefixlen);
206 assert (bit == 0 || bit == 1);
208 node->link[bit] = new;
209 new->parent = node;
212 /* Lock node. */
213 struct bgp_node *
214 bgp_lock_node (struct bgp_node *node)
216 node->lock++;
217 return node;
220 /* Unlock node. */
221 void
222 bgp_unlock_node (struct bgp_node *node)
224 node->lock--;
226 if (node->lock == 0)
227 bgp_node_delete (node);
230 /* Find matched prefix. */
231 struct bgp_node *
232 bgp_node_match (struct bgp_table *table, struct prefix *p)
234 struct bgp_node *node;
235 struct bgp_node *matched;
237 matched = NULL;
238 node = table->top;
240 /* Walk down tree. If there is matched route then store it to
241 matched. */
242 while (node && node->p.prefixlen <= p->prefixlen &&
243 prefix_match (&node->p, p))
245 if (node->info)
246 matched = node;
247 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
250 /* If matched route found, return it. */
251 if (matched)
252 return bgp_lock_node (matched);
254 return NULL;
257 struct bgp_node *
258 bgp_node_match_ipv4 (struct bgp_table *table, struct in_addr *addr)
260 struct prefix_ipv4 p;
262 memset (&p, 0, sizeof (struct prefix_ipv4));
263 p.family = AF_INET;
264 p.prefixlen = IPV4_MAX_PREFIXLEN;
265 p.prefix = *addr;
267 return bgp_node_match (table, (struct prefix *) &p);
270 #ifdef HAVE_IPV6
271 struct bgp_node *
272 bgp_node_match_ipv6 (struct bgp_table *table, struct in6_addr *addr)
274 struct prefix_ipv6 p;
276 memset (&p, 0, sizeof (struct prefix_ipv6));
277 p.family = AF_INET6;
278 p.prefixlen = IPV6_MAX_PREFIXLEN;
279 p.prefix = *addr;
281 return bgp_node_match (table, (struct prefix *) &p);
283 #endif /* HAVE_IPV6 */
285 /* Lookup same prefix node. Return NULL when we can't find route. */
286 struct bgp_node *
287 bgp_node_lookup (struct bgp_table *table, struct prefix *p)
289 struct bgp_node *node;
291 node = table->top;
293 while (node && node->p.prefixlen <= p->prefixlen &&
294 prefix_match (&node->p, p))
296 if (node->p.prefixlen == p->prefixlen && node->info)
297 return bgp_lock_node (node);
299 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
302 return NULL;
305 /* Add node to routing table. */
306 struct bgp_node *
307 bgp_node_get (struct bgp_table *table, struct prefix *p)
309 struct bgp_node *new;
310 struct bgp_node *node;
311 struct bgp_node *match;
313 match = NULL;
314 node = table->top;
315 while (node && node->p.prefixlen <= p->prefixlen &&
316 prefix_match (&node->p, p))
318 if (node->p.prefixlen == p->prefixlen)
320 bgp_lock_node (node);
321 return node;
323 match = node;
324 node = node->link[check_bit(&p->u.prefix, node->p.prefixlen)];
327 if (node == NULL)
329 new = bgp_node_set (table, p);
330 if (match)
331 set_link (match, new);
332 else
333 table->top = new;
335 else
337 new = bgp_node_create ();
338 route_common (&node->p, p, &new->p);
339 new->p.family = p->family;
340 new->table = table;
341 set_link (new, node);
343 if (match)
344 set_link (match, new);
345 else
346 table->top = new;
348 if (new->p.prefixlen != p->prefixlen)
350 match = new;
351 new = bgp_node_set (table, p);
352 set_link (match, new);
353 table->count++;
356 table->count++;
357 bgp_lock_node (new);
359 return new;
362 /* Delete node from the routing table. */
363 void
364 bgp_node_delete (struct bgp_node *node)
366 struct bgp_node *child;
367 struct bgp_node *parent;
369 assert (node->lock == 0);
370 assert (node->info == NULL);
372 if (node->l_left && node->l_right)
373 return;
375 if (node->l_left)
376 child = node->l_left;
377 else
378 child = node->l_right;
380 parent = node->parent;
382 if (child)
383 child->parent = parent;
385 if (parent)
387 if (parent->l_left == node)
388 parent->l_left = child;
389 else
390 parent->l_right = child;
392 else
393 node->table->top = child;
395 node->table->count--;
397 bgp_node_free (node);
399 /* If parent node is stub then delete it also. */
400 if (parent && parent->lock == 0)
401 bgp_node_delete (parent);
404 /* Get fist node and lock it. This function is useful when one want
405 to lookup all the node exist in the routing table. */
406 struct bgp_node *
407 bgp_table_top (struct bgp_table *table)
409 /* If there is no node in the routing table return NULL. */
410 if (table->top == NULL)
411 return NULL;
413 /* Lock the top node and return it. */
414 bgp_lock_node (table->top);
415 return table->top;
418 /* Unlock current node and lock next node then return it. */
419 struct bgp_node *
420 bgp_route_next (struct bgp_node *node)
422 struct bgp_node *next;
423 struct bgp_node *start;
425 /* Node may be deleted from bgp_unlock_node so we have to preserve
426 next node's pointer. */
428 if (node->l_left)
430 next = node->l_left;
431 bgp_lock_node (next);
432 bgp_unlock_node (node);
433 return next;
435 if (node->l_right)
437 next = node->l_right;
438 bgp_lock_node (next);
439 bgp_unlock_node (node);
440 return next;
443 start = node;
444 while (node->parent)
446 if (node->parent->l_left == node && node->parent->l_right)
448 next = node->parent->l_right;
449 bgp_lock_node (next);
450 bgp_unlock_node (start);
451 return next;
453 node = node->parent;
455 bgp_unlock_node (start);
456 return NULL;
459 /* Unlock current node and lock next node until limit. */
460 struct bgp_node *
461 bgp_route_next_until (struct bgp_node *node, struct bgp_node *limit)
463 struct bgp_node *next;
464 struct bgp_node *start;
466 /* Node may be deleted from bgp_unlock_node so we have to preserve
467 next node's pointer. */
469 if (node->l_left)
471 next = node->l_left;
472 bgp_lock_node (next);
473 bgp_unlock_node (node);
474 return next;
476 if (node->l_right)
478 next = node->l_right;
479 bgp_lock_node (next);
480 bgp_unlock_node (node);
481 return next;
484 start = node;
485 while (node->parent && node != limit)
487 if (node->parent->l_left == node && node->parent->l_right)
489 next = node->parent->l_right;
490 bgp_lock_node (next);
491 bgp_unlock_node (start);
492 return next;
494 node = node->parent;
496 bgp_unlock_node (start);
497 return NULL;
500 unsigned long
501 bgp_table_count (struct bgp_table *table)
503 return table->count;