paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
| 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. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 49 | static struct route_node * |
| 50 | route_node_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 58 | static struct route_node * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 72 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
Stephen Hemminger | 38cc00c | 2009-12-08 12:00:50 +0300 | [diff] [blame] | 128 | static const u_char maskbit[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | static void |
| 169 | set_link (struct route_node *node, struct route_node *new) |
| 170 | { |
Stephen Hemminger | 1352ef3 | 2009-12-09 14:43:17 +0300 | [diff] [blame] | 171 | unsigned int bit = prefix_bit (&new->p.u.prefix, node->p.prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | |
| 173 | node->link[bit] = new; |
| 174 | new->parent = node; |
| 175 | } |
| 176 | |
| 177 | /* Lock node. */ |
| 178 | struct route_node * |
| 179 | route_lock_node (struct route_node *node) |
| 180 | { |
| 181 | node->lock++; |
| 182 | return node; |
| 183 | } |
| 184 | |
| 185 | /* Unlock node. */ |
| 186 | void |
| 187 | route_unlock_node (struct route_node *node) |
| 188 | { |
| 189 | node->lock--; |
| 190 | |
| 191 | if (node->lock == 0) |
| 192 | route_node_delete (node); |
| 193 | } |
| 194 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | /* Find matched prefix. */ |
| 196 | struct route_node * |
Stephen Hemminger | 38cc00c | 2009-12-08 12:00:50 +0300 | [diff] [blame] | 197 | route_node_match (const struct route_table *table, const struct prefix *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | { |
| 199 | struct route_node *node; |
| 200 | struct route_node *matched; |
| 201 | |
| 202 | matched = NULL; |
| 203 | node = table->top; |
| 204 | |
| 205 | /* Walk down tree. If there is matched route then store it to |
| 206 | matched. */ |
| 207 | while (node && node->p.prefixlen <= p->prefixlen && |
| 208 | prefix_match (&node->p, p)) |
| 209 | { |
| 210 | if (node->info) |
| 211 | matched = node; |
Paul Jakma | f841681 | 2010-04-13 22:42:33 +0100 | [diff] [blame] | 212 | |
| 213 | if (node->p.prefixlen == p->prefixlen) |
| 214 | break; |
| 215 | |
Stephen Hemminger | 1352ef3 | 2009-12-09 14:43:17 +0300 | [diff] [blame] | 216 | node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* If matched route found, return it. */ |
| 220 | if (matched) |
| 221 | return route_lock_node (matched); |
| 222 | |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | struct route_node * |
Stephen Hemminger | 38cc00c | 2009-12-08 12:00:50 +0300 | [diff] [blame] | 227 | route_node_match_ipv4 (const struct route_table *table, |
| 228 | const struct in_addr *addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | { |
| 230 | struct prefix_ipv4 p; |
| 231 | |
| 232 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 233 | p.family = AF_INET; |
| 234 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 235 | p.prefix = *addr; |
| 236 | |
| 237 | return route_node_match (table, (struct prefix *) &p); |
| 238 | } |
| 239 | |
| 240 | #ifdef HAVE_IPV6 |
| 241 | struct route_node * |
Stephen Hemminger | 38cc00c | 2009-12-08 12:00:50 +0300 | [diff] [blame] | 242 | route_node_match_ipv6 (const struct route_table *table, |
| 243 | const struct in6_addr *addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | { |
| 245 | struct prefix_ipv6 p; |
| 246 | |
| 247 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 248 | p.family = AF_INET6; |
| 249 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 250 | p.prefix = *addr; |
| 251 | |
| 252 | return route_node_match (table, (struct prefix *) &p); |
| 253 | } |
| 254 | #endif /* HAVE_IPV6 */ |
| 255 | |
| 256 | /* Lookup same prefix node. Return NULL when we can't find route. */ |
| 257 | struct route_node * |
| 258 | route_node_lookup (struct route_table *table, struct prefix *p) |
| 259 | { |
| 260 | struct route_node *node; |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 261 | u_char prefixlen = p->prefixlen; |
| 262 | const u_char *prefix = &p->u.prefix; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | |
| 264 | node = table->top; |
| 265 | |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 266 | while (node && node->p.prefixlen <= prefixlen && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | prefix_match (&node->p, p)) |
| 268 | { |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 269 | if (node->p.prefixlen == prefixlen) |
Paul Jakma | f841681 | 2010-04-13 22:42:33 +0100 | [diff] [blame] | 270 | return node->info ? route_lock_node (node) : NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 272 | node = node->link[prefix_bit(prefix, node->p.prefixlen)]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | /* Add node to routing table. */ |
| 279 | struct route_node * |
| 280 | route_node_get (struct route_table *table, struct prefix *p) |
| 281 | { |
| 282 | struct route_node *new; |
| 283 | struct route_node *node; |
| 284 | struct route_node *match; |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 285 | u_char prefixlen = p->prefixlen; |
| 286 | const u_char *prefix = &p->u.prefix; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 287 | |
| 288 | match = NULL; |
| 289 | node = table->top; |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 290 | while (node && node->p.prefixlen <= prefixlen && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | prefix_match (&node->p, p)) |
| 292 | { |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 293 | if (node->p.prefixlen == prefixlen) |
Paul Jakma | f841681 | 2010-04-13 22:42:33 +0100 | [diff] [blame] | 294 | return route_lock_node (node); |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 295 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | match = node; |
Jorge Boncompte [DTI2] | 47d3b60 | 2012-05-07 16:53:11 +0000 | [diff] [blame] | 297 | node = node->link[prefix_bit(prefix, node->p.prefixlen)]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (node == NULL) |
| 301 | { |
| 302 | new = route_node_set (table, p); |
| 303 | if (match) |
| 304 | set_link (match, new); |
| 305 | else |
| 306 | table->top = new; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | new = route_node_new (); |
| 311 | route_common (&node->p, p, &new->p); |
| 312 | new->p.family = p->family; |
| 313 | new->table = table; |
| 314 | set_link (new, node); |
| 315 | |
| 316 | if (match) |
| 317 | set_link (match, new); |
| 318 | else |
| 319 | table->top = new; |
| 320 | |
| 321 | if (new->p.prefixlen != p->prefixlen) |
| 322 | { |
| 323 | match = new; |
| 324 | new = route_node_set (table, p); |
| 325 | set_link (match, new); |
| 326 | } |
| 327 | } |
| 328 | route_lock_node (new); |
| 329 | |
| 330 | return new; |
| 331 | } |
| 332 | |
| 333 | /* Delete node from the routing table. */ |
| 334 | void |
| 335 | route_node_delete (struct route_node *node) |
| 336 | { |
| 337 | struct route_node *child; |
| 338 | struct route_node *parent; |
| 339 | |
| 340 | assert (node->lock == 0); |
| 341 | assert (node->info == NULL); |
| 342 | |
| 343 | if (node->l_left && node->l_right) |
| 344 | return; |
| 345 | |
| 346 | if (node->l_left) |
| 347 | child = node->l_left; |
| 348 | else |
| 349 | child = node->l_right; |
| 350 | |
| 351 | parent = node->parent; |
| 352 | |
| 353 | if (child) |
| 354 | child->parent = parent; |
| 355 | |
| 356 | if (parent) |
| 357 | { |
| 358 | if (parent->l_left == node) |
| 359 | parent->l_left = child; |
| 360 | else |
| 361 | parent->l_right = child; |
| 362 | } |
| 363 | else |
| 364 | node->table->top = child; |
| 365 | |
| 366 | route_node_free (node); |
| 367 | |
| 368 | /* If parent node is stub then delete it also. */ |
| 369 | if (parent && parent->lock == 0) |
| 370 | route_node_delete (parent); |
| 371 | } |
| 372 | |
| 373 | /* Get fist node and lock it. This function is useful when one want |
| 374 | to lookup all the node exist in the routing table. */ |
| 375 | struct route_node * |
| 376 | route_top (struct route_table *table) |
| 377 | { |
| 378 | /* If there is no node in the routing table return NULL. */ |
| 379 | if (table->top == NULL) |
| 380 | return NULL; |
| 381 | |
| 382 | /* Lock the top node and return it. */ |
| 383 | route_lock_node (table->top); |
| 384 | return table->top; |
| 385 | } |
| 386 | |
| 387 | /* Unlock current node and lock next node then return it. */ |
| 388 | struct route_node * |
| 389 | route_next (struct route_node *node) |
| 390 | { |
| 391 | struct route_node *next; |
| 392 | struct route_node *start; |
| 393 | |
| 394 | /* Node may be deleted from route_unlock_node so we have to preserve |
| 395 | next node's pointer. */ |
| 396 | |
| 397 | if (node->l_left) |
| 398 | { |
| 399 | next = node->l_left; |
| 400 | route_lock_node (next); |
| 401 | route_unlock_node (node); |
| 402 | return next; |
| 403 | } |
| 404 | if (node->l_right) |
| 405 | { |
| 406 | next = node->l_right; |
| 407 | route_lock_node (next); |
| 408 | route_unlock_node (node); |
| 409 | return next; |
| 410 | } |
| 411 | |
| 412 | start = node; |
| 413 | while (node->parent) |
| 414 | { |
| 415 | if (node->parent->l_left == node && node->parent->l_right) |
| 416 | { |
| 417 | next = node->parent->l_right; |
| 418 | route_lock_node (next); |
| 419 | route_unlock_node (start); |
| 420 | return next; |
| 421 | } |
| 422 | node = node->parent; |
| 423 | } |
| 424 | route_unlock_node (start); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | /* Unlock current node and lock next node until limit. */ |
| 429 | struct route_node * |
| 430 | route_next_until (struct route_node *node, struct route_node *limit) |
| 431 | { |
| 432 | struct route_node *next; |
| 433 | struct route_node *start; |
| 434 | |
| 435 | /* Node may be deleted from route_unlock_node so we have to preserve |
| 436 | next node's pointer. */ |
| 437 | |
| 438 | if (node->l_left) |
| 439 | { |
| 440 | next = node->l_left; |
| 441 | route_lock_node (next); |
| 442 | route_unlock_node (node); |
| 443 | return next; |
| 444 | } |
| 445 | if (node->l_right) |
| 446 | { |
| 447 | next = node->l_right; |
| 448 | route_lock_node (next); |
| 449 | route_unlock_node (node); |
| 450 | return next; |
| 451 | } |
| 452 | |
| 453 | start = node; |
| 454 | while (node->parent && node != limit) |
| 455 | { |
| 456 | if (node->parent->l_left == node && node->parent->l_right) |
| 457 | { |
| 458 | next = node->parent->l_right; |
| 459 | route_lock_node (next); |
| 460 | route_unlock_node (start); |
| 461 | return next; |
| 462 | } |
| 463 | node = node->parent; |
| 464 | } |
| 465 | route_unlock_node (start); |
| 466 | return NULL; |
| 467 | } |