paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Routing Information Base. |
| 2 | * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "prefix.h" |
| 25 | #include "table.h" |
| 26 | #include "memory.h" |
| 27 | #include "str.h" |
| 28 | #include "command.h" |
| 29 | #include "if.h" |
| 30 | #include "log.h" |
| 31 | #include "sockunion.h" |
| 32 | |
| 33 | #include "zebra/rib.h" |
| 34 | #include "zebra/rt.h" |
| 35 | #include "zebra/zserv.h" |
| 36 | #include "zebra/redistribute.h" |
| 37 | #include "zebra/debug.h" |
| 38 | |
| 39 | /* Default rtm_table for all clients */ |
| 40 | extern int rtm_table_default; |
| 41 | |
| 42 | /* Each route type's string and default distance value. */ |
| 43 | struct |
| 44 | { |
| 45 | int key; |
| 46 | int distance; |
| 47 | } route_info[] = |
| 48 | { |
| 49 | {ZEBRA_ROUTE_SYSTEM, 0}, |
| 50 | {ZEBRA_ROUTE_KERNEL, 0}, |
| 51 | {ZEBRA_ROUTE_CONNECT, 0}, |
| 52 | {ZEBRA_ROUTE_STATIC, 1}, |
| 53 | {ZEBRA_ROUTE_RIP, 120}, |
| 54 | {ZEBRA_ROUTE_RIPNG, 120}, |
| 55 | {ZEBRA_ROUTE_OSPF, 110}, |
| 56 | {ZEBRA_ROUTE_OSPF6, 110}, |
| 57 | {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */} |
| 58 | }; |
| 59 | |
| 60 | /* Vector for routing table. */ |
| 61 | vector vrf_vector; |
| 62 | |
| 63 | /* Allocate new VRF. */ |
| 64 | struct vrf * |
| 65 | vrf_alloc (char *name) |
| 66 | { |
| 67 | struct vrf *vrf; |
| 68 | |
| 69 | vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf)); |
| 70 | |
| 71 | /* Put name. */ |
| 72 | if (name) |
| 73 | vrf->name = XSTRDUP (MTYPE_VRF_NAME, name); |
| 74 | |
| 75 | /* Allocate routing table and static table. */ |
| 76 | vrf->table[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 77 | vrf->table[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
| 78 | vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 79 | vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
| 80 | |
| 81 | return vrf; |
| 82 | } |
| 83 | |
| 84 | /* Free VRF. */ |
| 85 | void |
| 86 | vrf_free (struct vrf *vrf) |
| 87 | { |
| 88 | if (vrf->name) |
| 89 | XFREE (MTYPE_VRF_NAME, vrf->name); |
| 90 | XFREE (MTYPE_VRF, vrf); |
| 91 | } |
| 92 | |
| 93 | /* Lookup VRF by identifier. */ |
| 94 | struct vrf * |
| 95 | vrf_lookup (u_int32_t id) |
| 96 | { |
| 97 | return vector_lookup (vrf_vector, id); |
| 98 | } |
| 99 | |
| 100 | /* Lookup VRF by name. */ |
| 101 | struct vrf * |
| 102 | vrf_lookup_by_name (char *name) |
| 103 | { |
| 104 | int i; |
| 105 | struct vrf *vrf; |
| 106 | |
| 107 | for (i = 0; i < vector_max (vrf_vector); i++) |
| 108 | if ((vrf = vector_slot (vrf_vector, i)) != NULL) |
| 109 | if (vrf->name && name && strcmp (vrf->name, name) == 0) |
| 110 | return vrf; |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | /* Initialize VRF. */ |
| 115 | void |
| 116 | vrf_init () |
| 117 | { |
| 118 | struct vrf *default_table; |
| 119 | |
| 120 | /* Allocate VRF vector. */ |
| 121 | vrf_vector = vector_init (1); |
| 122 | |
| 123 | /* Allocate default main table. */ |
| 124 | default_table = vrf_alloc ("Default-IP-Routing-Table"); |
| 125 | |
| 126 | /* Default table index must be 0. */ |
| 127 | vector_set_index (vrf_vector, 0, default_table); |
| 128 | } |
| 129 | |
| 130 | /* Lookup route table. */ |
| 131 | struct route_table * |
| 132 | vrf_table (afi_t afi, safi_t safi, u_int32_t id) |
| 133 | { |
| 134 | struct vrf *vrf; |
| 135 | |
| 136 | vrf = vrf_lookup (id); |
| 137 | if (! vrf) |
| 138 | return NULL; |
| 139 | |
| 140 | return vrf->table[afi][safi]; |
| 141 | } |
| 142 | |
| 143 | /* Lookup static route table. */ |
| 144 | struct route_table * |
| 145 | vrf_static_table (afi_t afi, safi_t safi, u_int32_t id) |
| 146 | { |
| 147 | struct vrf *vrf; |
| 148 | |
| 149 | vrf = vrf_lookup (id); |
| 150 | if (! vrf) |
| 151 | return NULL; |
| 152 | |
| 153 | return vrf->stable[afi][safi]; |
| 154 | } |
| 155 | |
| 156 | /* Add nexthop to the end of the list. */ |
| 157 | void |
| 158 | nexthop_add (struct rib *rib, struct nexthop *nexthop) |
| 159 | { |
| 160 | struct nexthop *last; |
| 161 | |
| 162 | for (last = rib->nexthop; last && last->next; last = last->next) |
| 163 | ; |
| 164 | if (last) |
| 165 | last->next = nexthop; |
| 166 | else |
| 167 | rib->nexthop = nexthop; |
| 168 | nexthop->prev = last; |
| 169 | |
| 170 | rib->nexthop_num++; |
| 171 | } |
| 172 | |
| 173 | /* Delete specified nexthop from the list. */ |
| 174 | void |
| 175 | nexthop_delete (struct rib *rib, struct nexthop *nexthop) |
| 176 | { |
| 177 | if (nexthop->next) |
| 178 | nexthop->next->prev = nexthop->prev; |
| 179 | if (nexthop->prev) |
| 180 | nexthop->prev->next = nexthop->next; |
| 181 | else |
| 182 | rib->nexthop = nexthop->next; |
| 183 | rib->nexthop_num--; |
| 184 | } |
| 185 | |
| 186 | /* Free nexthop. */ |
| 187 | void |
| 188 | nexthop_free (struct nexthop *nexthop) |
| 189 | { |
| 190 | if (nexthop->type == NEXTHOP_TYPE_IFNAME && nexthop->ifname) |
| 191 | free (nexthop->ifname); |
| 192 | XFREE (MTYPE_NEXTHOP, nexthop); |
| 193 | } |
| 194 | |
| 195 | struct nexthop * |
| 196 | nexthop_ifindex_add (struct rib *rib, unsigned int ifindex) |
| 197 | { |
| 198 | struct nexthop *nexthop; |
| 199 | |
| 200 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 201 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 202 | nexthop->type = NEXTHOP_TYPE_IFINDEX; |
| 203 | nexthop->ifindex = ifindex; |
| 204 | |
| 205 | nexthop_add (rib, nexthop); |
| 206 | |
| 207 | return nexthop; |
| 208 | } |
| 209 | |
| 210 | struct nexthop * |
| 211 | nexthop_ifname_add (struct rib *rib, char *ifname) |
| 212 | { |
| 213 | struct nexthop *nexthop; |
| 214 | |
| 215 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 216 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 217 | nexthop->type = NEXTHOP_TYPE_IFNAME; |
| 218 | nexthop->ifname = strdup (ifname); |
| 219 | |
| 220 | nexthop_add (rib, nexthop); |
| 221 | |
| 222 | return nexthop; |
| 223 | } |
| 224 | |
| 225 | struct nexthop * |
| 226 | nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4) |
| 227 | { |
| 228 | struct nexthop *nexthop; |
| 229 | |
| 230 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 231 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 232 | nexthop->type = NEXTHOP_TYPE_IPV4; |
| 233 | nexthop->gate.ipv4 = *ipv4; |
| 234 | |
| 235 | nexthop_add (rib, nexthop); |
| 236 | |
| 237 | return nexthop; |
| 238 | } |
| 239 | |
| 240 | struct nexthop * |
| 241 | nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, |
| 242 | unsigned int ifindex) |
| 243 | { |
| 244 | struct nexthop *nexthop; |
| 245 | |
| 246 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 247 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 248 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
| 249 | nexthop->gate.ipv4 = *ipv4; |
| 250 | nexthop->ifindex = ifindex; |
| 251 | |
| 252 | nexthop_add (rib, nexthop); |
| 253 | |
| 254 | return nexthop; |
| 255 | } |
| 256 | |
| 257 | #ifdef HAVE_IPV6 |
| 258 | struct nexthop * |
| 259 | nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6) |
| 260 | { |
| 261 | struct nexthop *nexthop; |
| 262 | |
| 263 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 264 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 265 | nexthop->type = NEXTHOP_TYPE_IPV6; |
| 266 | nexthop->gate.ipv6 = *ipv6; |
| 267 | |
| 268 | nexthop_add (rib, nexthop); |
| 269 | |
| 270 | return nexthop; |
| 271 | } |
| 272 | |
| 273 | struct nexthop * |
| 274 | nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6, |
| 275 | char *ifname) |
| 276 | { |
| 277 | struct nexthop *nexthop; |
| 278 | |
| 279 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 280 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 281 | nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME; |
| 282 | nexthop->gate.ipv6 = *ipv6; |
| 283 | nexthop->ifname = XSTRDUP (0, ifname); |
| 284 | |
| 285 | nexthop_add (rib, nexthop); |
| 286 | |
| 287 | return nexthop; |
| 288 | } |
| 289 | |
| 290 | struct nexthop * |
| 291 | nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6, |
| 292 | unsigned int ifindex) |
| 293 | { |
| 294 | struct nexthop *nexthop; |
| 295 | |
| 296 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 297 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 298 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 299 | nexthop->gate.ipv6 = *ipv6; |
| 300 | nexthop->ifindex = ifindex; |
| 301 | |
| 302 | nexthop_add (rib, nexthop); |
| 303 | |
| 304 | return nexthop; |
| 305 | } |
| 306 | #endif /* HAVE_IPV6 */ |
| 307 | |
| 308 | |
| 309 | struct nexthop * |
| 310 | nexthop_blackhole_add (struct rib *rib) |
| 311 | { |
| 312 | struct nexthop *nexthop; |
| 313 | |
| 314 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 315 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 316 | nexthop->type = NEXTHOP_TYPE_BLACKHOLE; |
| 317 | SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE); |
| 318 | |
| 319 | nexthop_add (rib, nexthop); |
| 320 | |
| 321 | return nexthop; |
| 322 | } |
| 323 | |
| 324 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 325 | the route from FIB. */ |
| 326 | int |
| 327 | nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set, |
| 328 | struct route_node *top) |
| 329 | { |
| 330 | struct prefix_ipv4 p; |
| 331 | struct route_table *table; |
| 332 | struct route_node *rn; |
| 333 | struct rib *match; |
| 334 | struct nexthop *newhop; |
| 335 | |
| 336 | if (nexthop->type == NEXTHOP_TYPE_IPV4) |
| 337 | nexthop->ifindex = 0; |
| 338 | |
| 339 | if (set) |
| 340 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 341 | |
| 342 | /* Make lookup prefix. */ |
| 343 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 344 | p.family = AF_INET; |
| 345 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 346 | p.prefix = nexthop->gate.ipv4; |
| 347 | |
| 348 | /* Lookup table. */ |
| 349 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 350 | if (! table) |
| 351 | return 0; |
| 352 | |
| 353 | rn = route_node_match (table, (struct prefix *) &p); |
| 354 | while (rn) |
| 355 | { |
| 356 | route_unlock_node (rn); |
| 357 | |
| 358 | /* If lookup self prefix return immidiately. */ |
| 359 | if (rn == top) |
| 360 | return 0; |
| 361 | |
| 362 | /* Pick up selected route. */ |
| 363 | for (match = rn->info; match; match = match->next) |
| 364 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 365 | break; |
| 366 | |
| 367 | /* If there is no selected route or matched route is EGP, go up |
| 368 | tree. */ |
| 369 | if (! match |
| 370 | || match->type == ZEBRA_ROUTE_BGP) |
| 371 | { |
| 372 | do { |
| 373 | rn = rn->parent; |
| 374 | } while (rn && rn->info == NULL); |
| 375 | if (rn) |
| 376 | route_lock_node (rn); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 381 | { |
| 382 | /* Directly point connected route. */ |
| 383 | newhop = match->nexthop; |
| 384 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4) |
| 385 | nexthop->ifindex = newhop->ifindex; |
| 386 | |
| 387 | return 1; |
| 388 | } |
| 389 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 390 | { |
| 391 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 392 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 393 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 394 | { |
| 395 | if (set) |
| 396 | { |
| 397 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 398 | nexthop->rtype = newhop->type; |
| 399 | if (newhop->type == NEXTHOP_TYPE_IPV4 || |
| 400 | newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 401 | nexthop->rgate.ipv4 = newhop->gate.ipv4; |
| 402 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
| 403 | || newhop->type == NEXTHOP_TYPE_IFNAME |
| 404 | || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 405 | nexthop->rifindex = newhop->ifindex; |
| 406 | } |
| 407 | return 1; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | return 0; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | #ifdef HAVE_IPV6 |
| 421 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 422 | the route from FIB. */ |
| 423 | int |
| 424 | nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set, |
| 425 | struct route_node *top) |
| 426 | { |
| 427 | struct prefix_ipv6 p; |
| 428 | struct route_table *table; |
| 429 | struct route_node *rn; |
| 430 | struct rib *match; |
| 431 | struct nexthop *newhop; |
| 432 | |
| 433 | if (nexthop->type == NEXTHOP_TYPE_IPV6) |
| 434 | nexthop->ifindex = 0; |
| 435 | |
| 436 | if (set) |
| 437 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 438 | |
| 439 | /* Make lookup prefix. */ |
| 440 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 441 | p.family = AF_INET6; |
| 442 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 443 | p.prefix = nexthop->gate.ipv6; |
| 444 | |
| 445 | /* Lookup table. */ |
| 446 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 447 | if (! table) |
| 448 | return 0; |
| 449 | |
| 450 | rn = route_node_match (table, (struct prefix *) &p); |
| 451 | while (rn) |
| 452 | { |
| 453 | route_unlock_node (rn); |
| 454 | |
| 455 | /* If lookup self prefix return immidiately. */ |
| 456 | if (rn == top) |
| 457 | return 0; |
| 458 | |
| 459 | /* Pick up selected route. */ |
| 460 | for (match = rn->info; match; match = match->next) |
| 461 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 462 | break; |
| 463 | |
| 464 | /* If there is no selected route or matched route is EGP, go up |
| 465 | tree. */ |
| 466 | if (! match |
| 467 | || match->type == ZEBRA_ROUTE_BGP) |
| 468 | { |
| 469 | do { |
| 470 | rn = rn->parent; |
| 471 | } while (rn && rn->info == NULL); |
| 472 | if (rn) |
| 473 | route_lock_node (rn); |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 478 | { |
| 479 | /* Directly point connected route. */ |
| 480 | newhop = match->nexthop; |
| 481 | |
| 482 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6) |
| 483 | nexthop->ifindex = newhop->ifindex; |
| 484 | |
| 485 | return 1; |
| 486 | } |
| 487 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 488 | { |
| 489 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 490 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 491 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 492 | { |
| 493 | if (set) |
| 494 | { |
| 495 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 496 | nexthop->rtype = newhop->type; |
| 497 | if (newhop->type == NEXTHOP_TYPE_IPV6 |
| 498 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 499 | || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
| 500 | nexthop->rgate.ipv6 = newhop->gate.ipv6; |
| 501 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
| 502 | || newhop->type == NEXTHOP_TYPE_IFNAME |
| 503 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 504 | || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
| 505 | nexthop->rifindex = newhop->ifindex; |
| 506 | } |
| 507 | return 1; |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | return 0; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | #endif /* HAVE_IPV6 */ |
| 520 | |
| 521 | struct rib * |
| 522 | rib_match_ipv4 (struct in_addr addr) |
| 523 | { |
| 524 | struct prefix_ipv4 p; |
| 525 | struct route_table *table; |
| 526 | struct route_node *rn; |
| 527 | struct rib *match; |
| 528 | struct nexthop *newhop; |
| 529 | |
| 530 | /* Lookup table. */ |
| 531 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 532 | if (! table) |
| 533 | return 0; |
| 534 | |
| 535 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 536 | p.family = AF_INET; |
| 537 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 538 | p.prefix = addr; |
| 539 | |
| 540 | rn = route_node_match (table, (struct prefix *) &p); |
| 541 | |
| 542 | while (rn) |
| 543 | { |
| 544 | route_unlock_node (rn); |
| 545 | |
| 546 | /* Pick up selected route. */ |
| 547 | for (match = rn->info; match; match = match->next) |
| 548 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 549 | break; |
| 550 | |
| 551 | /* If there is no selected route or matched route is EGP, go up |
| 552 | tree. */ |
| 553 | if (! match |
| 554 | || match->type == ZEBRA_ROUTE_BGP) |
| 555 | { |
| 556 | do { |
| 557 | rn = rn->parent; |
| 558 | } while (rn && rn->info == NULL); |
| 559 | if (rn) |
| 560 | route_lock_node (rn); |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 565 | /* Directly point connected route. */ |
| 566 | return match; |
| 567 | else |
| 568 | { |
| 569 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 570 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
| 571 | return match; |
| 572 | return NULL; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | struct rib * |
| 580 | rib_lookup_ipv4 (struct prefix_ipv4 *p) |
| 581 | { |
| 582 | struct route_table *table; |
| 583 | struct route_node *rn; |
| 584 | struct rib *match; |
| 585 | struct nexthop *nexthop; |
| 586 | |
| 587 | /* Lookup table. */ |
| 588 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 589 | if (! table) |
| 590 | return 0; |
| 591 | |
| 592 | rn = route_node_lookup (table, (struct prefix *) p); |
| 593 | |
| 594 | /* No route for this prefix. */ |
| 595 | if (! rn) |
| 596 | return NULL; |
| 597 | |
| 598 | /* Unlock node. */ |
| 599 | route_unlock_node (rn); |
| 600 | |
| 601 | /* Pick up selected route. */ |
| 602 | for (match = rn->info; match; match = match->next) |
| 603 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 604 | break; |
| 605 | |
| 606 | if (! match || match->type == ZEBRA_ROUTE_BGP) |
| 607 | return NULL; |
| 608 | |
| 609 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 610 | return match; |
| 611 | |
| 612 | for (nexthop = match->nexthop; nexthop; nexthop = nexthop->next) |
| 613 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 614 | return match; |
| 615 | |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | #ifdef HAVE_IPV6 |
| 620 | struct rib * |
| 621 | rib_match_ipv6 (struct in6_addr *addr) |
| 622 | { |
| 623 | struct prefix_ipv6 p; |
| 624 | struct route_table *table; |
| 625 | struct route_node *rn; |
| 626 | struct rib *match; |
| 627 | struct nexthop *newhop; |
| 628 | |
| 629 | /* Lookup table. */ |
| 630 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 631 | if (! table) |
| 632 | return 0; |
| 633 | |
| 634 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 635 | p.family = AF_INET6; |
| 636 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 637 | IPV6_ADDR_COPY (&p.prefix, addr); |
| 638 | |
| 639 | rn = route_node_match (table, (struct prefix *) &p); |
| 640 | |
| 641 | while (rn) |
| 642 | { |
| 643 | route_unlock_node (rn); |
| 644 | |
| 645 | /* Pick up selected route. */ |
| 646 | for (match = rn->info; match; match = match->next) |
| 647 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 648 | break; |
| 649 | |
| 650 | /* If there is no selected route or matched route is EGP, go up |
| 651 | tree. */ |
| 652 | if (! match |
| 653 | || match->type == ZEBRA_ROUTE_BGP) |
| 654 | { |
| 655 | do { |
| 656 | rn = rn->parent; |
| 657 | } while (rn && rn->info == NULL); |
| 658 | if (rn) |
| 659 | route_lock_node (rn); |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 664 | /* Directly point connected route. */ |
| 665 | return match; |
| 666 | else |
| 667 | { |
| 668 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 669 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
| 670 | return match; |
| 671 | return NULL; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | return NULL; |
| 676 | } |
| 677 | #endif /* HAVE_IPV6 */ |
| 678 | |
| 679 | int |
| 680 | nexthop_active_check (struct route_node *rn, struct rib *rib, |
| 681 | struct nexthop *nexthop, int set) |
| 682 | { |
| 683 | struct interface *ifp; |
| 684 | |
| 685 | switch (nexthop->type) |
| 686 | { |
| 687 | case NEXTHOP_TYPE_IFINDEX: |
| 688 | ifp = if_lookup_by_index (nexthop->ifindex); |
| 689 | if (ifp && if_is_up (ifp)) |
| 690 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 691 | else |
| 692 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 693 | break; |
| 694 | case NEXTHOP_TYPE_IFNAME: |
| 695 | case NEXTHOP_TYPE_IPV6_IFNAME: |
| 696 | ifp = if_lookup_by_name (nexthop->ifname); |
| 697 | if (ifp && if_is_up (ifp)) |
| 698 | { |
| 699 | if (set) |
| 700 | nexthop->ifindex = ifp->ifindex; |
| 701 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 702 | } |
| 703 | else |
| 704 | { |
| 705 | if (set) |
| 706 | nexthop->ifindex = 0; |
| 707 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 708 | } |
| 709 | break; |
| 710 | case NEXTHOP_TYPE_IPV4: |
| 711 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 712 | if (nexthop_active_ipv4 (rib, nexthop, set, rn)) |
| 713 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 714 | else |
| 715 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 716 | break; |
| 717 | #ifdef HAVE_IPV6 |
| 718 | case NEXTHOP_TYPE_IPV6: |
| 719 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 720 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 721 | else |
| 722 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 723 | break; |
| 724 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
| 725 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6)) |
| 726 | { |
| 727 | ifp = if_lookup_by_index (nexthop->ifindex); |
| 728 | if (ifp && if_is_up (ifp)) |
| 729 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 730 | else |
| 731 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 736 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 737 | else |
| 738 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 739 | } |
| 740 | break; |
| 741 | #endif /* HAVE_IPV6 */ |
| 742 | case NEXTHOP_TYPE_BLACKHOLE: |
| 743 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 744 | break; |
| 745 | default: |
| 746 | break; |
| 747 | } |
| 748 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 749 | } |
| 750 | |
| 751 | int |
| 752 | nexthop_active_update (struct route_node *rn, struct rib *rib, int set) |
| 753 | { |
| 754 | struct nexthop *nexthop; |
| 755 | int active; |
| 756 | |
| 757 | rib->nexthop_active_num = 0; |
| 758 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 759 | |
| 760 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 761 | { |
| 762 | active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 763 | rib->nexthop_active_num += nexthop_active_check (rn, rib, nexthop, set); |
| 764 | if (active != CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 765 | SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 766 | } |
| 767 | return rib->nexthop_active_num; |
| 768 | } |
| 769 | |
| 770 | #define RIB_SYSTEM_ROUTE(R) \ |
| 771 | ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT) |
| 772 | |
| 773 | void |
| 774 | newrib_free (struct rib *rib) |
| 775 | { |
| 776 | struct nexthop *nexthop; |
| 777 | struct nexthop *next; |
| 778 | |
| 779 | for (nexthop = rib->nexthop; nexthop; nexthop = next) |
| 780 | { |
| 781 | next = nexthop->next; |
| 782 | nexthop_free (nexthop); |
| 783 | } |
| 784 | XFREE (MTYPE_RIB, rib); |
| 785 | } |
| 786 | |
| 787 | void |
| 788 | rib_install_kernel (struct route_node *rn, struct rib *rib) |
| 789 | { |
| 790 | int ret = 0; |
| 791 | struct nexthop *nexthop; |
| 792 | |
| 793 | switch (PREFIX_FAMILY (&rn->p)) |
| 794 | { |
| 795 | case AF_INET: |
| 796 | ret = kernel_add_ipv4 (&rn->p, rib); |
| 797 | break; |
| 798 | #ifdef HAVE_IPV6 |
| 799 | case AF_INET6: |
| 800 | ret = kernel_add_ipv6 (&rn->p, rib); |
| 801 | break; |
| 802 | #endif /* HAVE_IPV6 */ |
| 803 | } |
| 804 | |
| 805 | if (ret < 0) |
| 806 | { |
| 807 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 808 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* Uninstall the route from kernel. */ |
| 813 | int |
| 814 | rib_uninstall_kernel (struct route_node *rn, struct rib *rib) |
| 815 | { |
| 816 | int ret = 0; |
| 817 | struct nexthop *nexthop; |
| 818 | |
| 819 | switch (PREFIX_FAMILY (&rn->p)) |
| 820 | { |
| 821 | case AF_INET: |
| 822 | ret = kernel_delete_ipv4 (&rn->p, rib); |
| 823 | break; |
| 824 | #ifdef HAVE_IPV6 |
| 825 | case AF_INET6: |
| 826 | ret = kernel_delete_ipv6 (&rn->p, rib); |
| 827 | break; |
| 828 | #endif /* HAVE_IPV6 */ |
| 829 | } |
| 830 | |
| 831 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 832 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 833 | |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | /* Uninstall the route from kernel. */ |
| 838 | void |
| 839 | rib_uninstall (struct route_node *rn, struct rib *rib) |
| 840 | { |
| 841 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 842 | { |
| 843 | redistribute_delete (&rn->p, rib); |
| 844 | if (! RIB_SYSTEM_ROUTE (rib)) |
| 845 | rib_uninstall_kernel (rn, rib); |
| 846 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* Core function for processing routing information base. */ |
| 851 | void |
| 852 | rib_process (struct route_node *rn, struct rib *del) |
| 853 | { |
| 854 | struct rib *rib; |
| 855 | struct rib *next; |
| 856 | struct rib *fib = NULL; |
| 857 | struct rib *select = NULL; |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 858 | int installed = 0; |
| 859 | struct nexthop *nexthop = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 860 | |
| 861 | for (rib = rn->info; rib; rib = next) |
| 862 | { |
| 863 | next = rib->next; |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 864 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | /* Currently installed rib. */ |
| 866 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 867 | fib = rib; |
| 868 | |
| 869 | /* Skip unreachable nexthop. */ |
| 870 | if (! nexthop_active_update (rn, rib, 0)) |
| 871 | continue; |
| 872 | |
| 873 | /* Infinit distance. */ |
| 874 | if (rib->distance == DISTANCE_INFINITY) |
| 875 | continue; |
| 876 | |
| 877 | /* Newly selected rib. */ |
| 878 | if (! select || rib->distance < select->distance |
| 879 | || rib->type == ZEBRA_ROUTE_CONNECT) |
| 880 | select = rib; |
| 881 | } |
| 882 | |
| 883 | /* Deleted route check. */ |
| 884 | if (del && CHECK_FLAG (del->flags, ZEBRA_FLAG_SELECTED)) |
| 885 | fib = del; |
| 886 | |
| 887 | /* Same route is selected. */ |
| 888 | if (select && select == fib) |
| 889 | { |
| 890 | if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED)) |
| 891 | { |
| 892 | redistribute_delete (&rn->p, select); |
| 893 | if (! RIB_SYSTEM_ROUTE (select)) |
| 894 | rib_uninstall_kernel (rn, select); |
| 895 | |
| 896 | /* Set real nexthop. */ |
| 897 | nexthop_active_update (rn, select, 1); |
| 898 | |
| 899 | if (! RIB_SYSTEM_ROUTE (select)) |
| 900 | rib_install_kernel (rn, select); |
| 901 | redistribute_add (&rn->p, select); |
| 902 | } |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 903 | else if (! RIB_SYSTEM_ROUTE (select)) |
| 904 | { |
| 905 | /* Housekeeping code to deal with |
| 906 | race conditions in kernel with linux |
| 907 | netlink reporting interface up before IPv4 or IPv6 protocol |
| 908 | is ready to add routes. |
| 909 | This makes sure the routes are IN the kernel. |
| 910 | */ |
| 911 | |
| 912 | for (nexthop = select->nexthop; nexthop; nexthop = nexthop->next) |
| 913 | { |
| 914 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 915 | installed = 1; |
| 916 | } |
| 917 | if (! installed) rib_install_kernel (rn, select); |
| 918 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
| 922 | /* Uninstall old rib from forwarding table. */ |
| 923 | if (fib) |
| 924 | { |
| 925 | redistribute_delete (&rn->p, fib); |
| 926 | if (! RIB_SYSTEM_ROUTE (fib)) |
| 927 | rib_uninstall_kernel (rn, fib); |
| 928 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 929 | |
| 930 | /* Set real nexthop. */ |
| 931 | nexthop_active_update (rn, fib, 1); |
| 932 | } |
| 933 | |
| 934 | /* Install new rib into forwarding table. */ |
| 935 | if (select) |
| 936 | { |
| 937 | /* Set real nexthop. */ |
| 938 | nexthop_active_update (rn, select, 1); |
| 939 | |
| 940 | if (! RIB_SYSTEM_ROUTE (select)) |
| 941 | rib_install_kernel (rn, select); |
| 942 | SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED); |
| 943 | redistribute_add (&rn->p, select); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /* Add RIB to head of the route node. */ |
| 948 | void |
| 949 | rib_addnode (struct route_node *rn, struct rib *rib) |
| 950 | { |
| 951 | struct rib *head; |
| 952 | |
| 953 | head = rn->info; |
| 954 | if (head) |
| 955 | head->prev = rib; |
| 956 | rib->next = head; |
| 957 | rn->info = rib; |
| 958 | } |
| 959 | |
| 960 | void |
| 961 | rib_delnode (struct route_node *rn, struct rib *rib) |
| 962 | { |
| 963 | if (rib->next) |
| 964 | rib->next->prev = rib->prev; |
| 965 | if (rib->prev) |
| 966 | rib->prev->next = rib->next; |
| 967 | else |
| 968 | rn->info = rib->next; |
| 969 | } |
| 970 | |
| 971 | int |
| 972 | rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
| 973 | struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, |
| 974 | u_int32_t metric, u_char distance) |
| 975 | { |
| 976 | struct rib *rib; |
| 977 | struct rib *same = NULL; |
| 978 | struct route_table *table; |
| 979 | struct route_node *rn; |
| 980 | struct nexthop *nexthop; |
| 981 | |
| 982 | /* Lookup table. */ |
| 983 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 984 | if (! table) |
| 985 | return 0; |
| 986 | |
| 987 | /* Make it sure prefixlen is applied to the prefix. */ |
| 988 | apply_mask_ipv4 (p); |
| 989 | |
| 990 | /* Set default distance by route type. */ |
| 991 | if (distance == 0) |
| 992 | { |
| 993 | distance = route_info[type].distance; |
| 994 | |
| 995 | /* iBGP distance is 200. */ |
| 996 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 997 | distance = 200; |
| 998 | } |
| 999 | |
| 1000 | /* Lookup route node.*/ |
| 1001 | rn = route_node_get (table, (struct prefix *) p); |
| 1002 | |
| 1003 | /* If same type of route are installed, treat it as a implicit |
| 1004 | withdraw. */ |
| 1005 | for (rib = rn->info; rib; rib = rib->next) |
| 1006 | { |
| 1007 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1008 | { |
| 1009 | nexthop = rib->nexthop; |
| 1010 | |
| 1011 | /* Duplicate connected route comes in. */ |
| 1012 | if (rib->type == type |
| 1013 | && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1014 | && nexthop->ifindex == ifindex) |
| 1015 | { |
| 1016 | rib->refcnt++; |
| 1017 | return 0 ; |
| 1018 | } |
| 1019 | } |
| 1020 | else if (rib->type == type) |
| 1021 | { |
| 1022 | same = rib; |
| 1023 | rib_delnode (rn, same); |
| 1024 | route_unlock_node (rn); |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* Allocate new rib structure. */ |
| 1030 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1031 | memset (rib, 0, sizeof (struct rib)); |
| 1032 | rib->type = type; |
| 1033 | rib->distance = distance; |
| 1034 | rib->flags = flags; |
| 1035 | rib->metric = metric; |
| 1036 | rib->nexthop_num = 0; |
| 1037 | rib->uptime = time (NULL); |
| 1038 | |
| 1039 | /* Nexthop settings. */ |
| 1040 | if (gate) |
| 1041 | { |
| 1042 | if (ifindex) |
| 1043 | nexthop_ipv4_ifindex_add (rib, gate, ifindex); |
| 1044 | else |
| 1045 | nexthop_ipv4_add (rib, gate); |
| 1046 | } |
| 1047 | else |
| 1048 | nexthop_ifindex_add (rib, ifindex); |
| 1049 | |
| 1050 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1051 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 1052 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1053 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1054 | |
| 1055 | /* Link new rib to node.*/ |
| 1056 | rib_addnode (rn, rib); |
| 1057 | |
| 1058 | /* Process this route node. */ |
| 1059 | rib_process (rn, same); |
| 1060 | |
| 1061 | /* Free implicit route.*/ |
| 1062 | if (same) |
| 1063 | newrib_free (same); |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | int |
| 1069 | rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib) |
| 1070 | { |
| 1071 | struct route_table *table; |
| 1072 | struct route_node *rn; |
| 1073 | struct rib *same; |
| 1074 | struct nexthop *nexthop; |
| 1075 | |
| 1076 | /* Lookup table. */ |
| 1077 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 1078 | if (! table) |
| 1079 | return 0; |
| 1080 | |
| 1081 | /* Make it sure prefixlen is applied to the prefix. */ |
| 1082 | apply_mask_ipv4 (p); |
| 1083 | |
| 1084 | /* Set default distance by route type. */ |
| 1085 | if (rib->distance == 0) |
| 1086 | { |
| 1087 | rib->distance = route_info[rib->type].distance; |
| 1088 | |
| 1089 | /* iBGP distance is 200. */ |
| 1090 | if (rib->type == ZEBRA_ROUTE_BGP |
| 1091 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 1092 | rib->distance = 200; |
| 1093 | } |
| 1094 | |
| 1095 | /* Lookup route node.*/ |
| 1096 | rn = route_node_get (table, (struct prefix *) p); |
| 1097 | |
| 1098 | /* If same type of route are installed, treat it as a implicit |
| 1099 | withdraw. */ |
| 1100 | for (same = rn->info; same; same = same->next) |
| 1101 | { |
| 1102 | if (same->type == rib->type && same->table == rib->table |
| 1103 | && same->type != ZEBRA_ROUTE_CONNECT) |
| 1104 | { |
| 1105 | rib_delnode (rn, same); |
| 1106 | route_unlock_node (rn); |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1112 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) |
| 1113 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1114 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1115 | |
| 1116 | /* Link new rib to node.*/ |
| 1117 | rib_addnode (rn, rib); |
| 1118 | |
| 1119 | /* Process this route node. */ |
| 1120 | rib_process (rn, same); |
| 1121 | |
| 1122 | /* Free implicit route.*/ |
| 1123 | if (same) |
| 1124 | newrib_free (same); |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | int |
| 1130 | rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
| 1131 | struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id) |
| 1132 | { |
| 1133 | struct route_table *table; |
| 1134 | struct route_node *rn; |
| 1135 | struct rib *rib; |
| 1136 | struct rib *fib = NULL; |
| 1137 | struct rib *same = NULL; |
| 1138 | struct nexthop *nexthop; |
| 1139 | char buf1[BUFSIZ]; |
| 1140 | char buf2[BUFSIZ]; |
| 1141 | |
| 1142 | /* Lookup table. */ |
| 1143 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 1144 | if (! table) |
| 1145 | return 0; |
| 1146 | |
| 1147 | /* Apply mask. */ |
| 1148 | apply_mask_ipv4 (p); |
| 1149 | |
| 1150 | /* Lookup route node. */ |
| 1151 | rn = route_node_lookup (table, (struct prefix *) p); |
| 1152 | if (! rn) |
| 1153 | { |
| 1154 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1155 | { |
| 1156 | if (gate) |
| 1157 | zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib", |
| 1158 | inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ), |
| 1159 | p->prefixlen, |
| 1160 | inet_ntop (AF_INET, gate, buf2, BUFSIZ), |
| 1161 | ifindex); |
| 1162 | else |
| 1163 | zlog_info ("route %s/%d ifindex %d doesn't exist in rib", |
| 1164 | inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ), |
| 1165 | p->prefixlen, |
| 1166 | ifindex); |
| 1167 | } |
| 1168 | return ZEBRA_ERR_RTNOEXIST; |
| 1169 | } |
| 1170 | |
| 1171 | /* Lookup same type route. */ |
| 1172 | for (rib = rn->info; rib; rib = rib->next) |
| 1173 | { |
| 1174 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 1175 | fib = rib; |
| 1176 | |
| 1177 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1178 | { |
| 1179 | nexthop = rib->nexthop; |
| 1180 | |
| 1181 | if (rib->type == type |
| 1182 | && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1183 | && nexthop->ifindex == ifindex) |
| 1184 | { |
| 1185 | if (rib->refcnt) |
| 1186 | { |
| 1187 | rib->refcnt--; |
| 1188 | route_unlock_node (rn); |
| 1189 | route_unlock_node (rn); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | same = rib; |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | if (rib->type == type) |
| 1199 | { |
| 1200 | same = rib; |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* If same type of route can't be found and this message is from |
| 1207 | kernel. */ |
| 1208 | if (! same) |
| 1209 | { |
| 1210 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 1211 | { |
| 1212 | /* Unset flags. */ |
| 1213 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 1214 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1215 | |
| 1216 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 1217 | } |
| 1218 | else |
| 1219 | { |
| 1220 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1221 | { |
| 1222 | if (gate) |
| 1223 | zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
| 1224 | inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ), |
| 1225 | p->prefixlen, |
| 1226 | inet_ntop (AF_INET, gate, buf2, BUFSIZ), |
| 1227 | ifindex, |
| 1228 | type); |
| 1229 | else |
| 1230 | zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib", |
| 1231 | inet_ntop (AF_INET, &p->prefix, buf1, BUFSIZ), |
| 1232 | p->prefixlen, |
| 1233 | ifindex, |
| 1234 | type); |
| 1235 | } |
| 1236 | route_unlock_node (rn); |
| 1237 | return ZEBRA_ERR_RTNOEXIST; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | if (same) |
| 1242 | rib_delnode (rn, same); |
| 1243 | |
| 1244 | /* Process changes. */ |
| 1245 | rib_process (rn, same); |
| 1246 | |
| 1247 | if (same) |
| 1248 | { |
| 1249 | newrib_free (same); |
| 1250 | route_unlock_node (rn); |
| 1251 | } |
| 1252 | |
| 1253 | route_unlock_node (rn); |
| 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | /* Install static route into rib. */ |
| 1259 | void |
| 1260 | static_install_ipv4 (struct prefix *p, struct static_ipv4 *si) |
| 1261 | { |
| 1262 | struct rib *rib; |
| 1263 | struct route_node *rn; |
| 1264 | struct route_table *table; |
| 1265 | |
| 1266 | /* Lookup table. */ |
| 1267 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 1268 | if (! table) |
| 1269 | return; |
| 1270 | |
| 1271 | /* Lookup existing route */ |
| 1272 | rn = route_node_get (table, p); |
| 1273 | for (rib = rn->info; rib; rib = rib->next) |
| 1274 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 1275 | break; |
| 1276 | |
| 1277 | if (rib) |
| 1278 | { |
| 1279 | /* Same distance static route is there. Update it with new |
| 1280 | nexthop. */ |
| 1281 | rib_uninstall (rn, rib); |
| 1282 | route_unlock_node (rn); |
| 1283 | |
| 1284 | switch (si->type) |
| 1285 | { |
| 1286 | case STATIC_IPV4_GATEWAY: |
| 1287 | nexthop_ipv4_add (rib, &si->gate.ipv4); |
| 1288 | break; |
| 1289 | case STATIC_IPV4_IFNAME: |
| 1290 | nexthop_ifname_add (rib, si->gate.ifname); |
| 1291 | break; |
| 1292 | case STATIC_IPV4_BLACKHOLE: |
| 1293 | nexthop_blackhole_add (rib); |
| 1294 | break; |
| 1295 | } |
| 1296 | rib_process (rn, NULL); |
| 1297 | } |
| 1298 | else |
| 1299 | { |
| 1300 | /* This is new static route. */ |
| 1301 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1302 | memset (rib, 0, sizeof (struct rib)); |
| 1303 | |
| 1304 | rib->type = ZEBRA_ROUTE_STATIC; |
| 1305 | rib->distance = si->distance; |
| 1306 | rib->metric = 0; |
| 1307 | rib->nexthop_num = 0; |
| 1308 | |
| 1309 | switch (si->type) |
| 1310 | { |
| 1311 | case STATIC_IPV4_GATEWAY: |
| 1312 | nexthop_ipv4_add (rib, &si->gate.ipv4); |
| 1313 | break; |
| 1314 | case STATIC_IPV4_IFNAME: |
| 1315 | nexthop_ifname_add (rib, si->gate.ifname); |
| 1316 | break; |
| 1317 | case STATIC_IPV4_BLACKHOLE: |
| 1318 | nexthop_blackhole_add (rib); |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | /* Link this rib to the tree. */ |
| 1323 | rib_addnode (rn, rib); |
| 1324 | |
| 1325 | /* Process this prefix. */ |
| 1326 | rib_process (rn, NULL); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | int |
| 1331 | static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si) |
| 1332 | { |
| 1333 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1334 | && si->type == STATIC_IPV4_GATEWAY |
| 1335 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4)) |
| 1336 | return 1; |
| 1337 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1338 | && si->type == STATIC_IPV4_IFNAME |
| 1339 | && strcmp (nexthop->ifname, si->gate.ifname) == 0) |
| 1340 | return 1; |
| 1341 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
| 1342 | && si->type == STATIC_IPV4_BLACKHOLE) |
| 1343 | return 1; |
| 1344 | return 0;; |
| 1345 | } |
| 1346 | |
| 1347 | /* Uninstall static route from RIB. */ |
| 1348 | void |
| 1349 | static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si) |
| 1350 | { |
| 1351 | struct route_node *rn; |
| 1352 | struct rib *rib; |
| 1353 | struct nexthop *nexthop; |
| 1354 | struct route_table *table; |
| 1355 | |
| 1356 | /* Lookup table. */ |
| 1357 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 1358 | if (! table) |
| 1359 | return; |
| 1360 | |
| 1361 | /* Lookup existing route with type and distance. */ |
| 1362 | rn = route_node_lookup (table, p); |
| 1363 | if (! rn) |
| 1364 | return; |
| 1365 | |
| 1366 | for (rib = rn->info; rib; rib = rib->next) |
| 1367 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 1368 | break; |
| 1369 | |
| 1370 | if (! rib) |
| 1371 | { |
| 1372 | route_unlock_node (rn); |
| 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | /* Lookup nexthop. */ |
| 1377 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1378 | if (static_ipv4_nexthop_same (nexthop, si)) |
| 1379 | break; |
| 1380 | |
| 1381 | /* Can't find nexthop. */ |
| 1382 | if (! nexthop) |
| 1383 | { |
| 1384 | route_unlock_node (rn); |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | /* Check nexthop. */ |
| 1389 | if (rib->nexthop_num == 1) |
| 1390 | { |
| 1391 | rib_delnode (rn, rib); |
| 1392 | rib_process (rn, rib); |
| 1393 | newrib_free (rib); |
| 1394 | route_unlock_node (rn); |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | rib_uninstall (rn, rib); |
| 1399 | nexthop_delete (rib, nexthop); |
| 1400 | nexthop_free (nexthop); |
| 1401 | rib_process (rn, rib); |
| 1402 | } |
| 1403 | |
| 1404 | /* Unlock node. */ |
| 1405 | route_unlock_node (rn); |
| 1406 | } |
| 1407 | |
| 1408 | /* Add static route into static route configuration. */ |
| 1409 | int |
| 1410 | static_add_ipv4 (struct prefix *p, struct in_addr *gate, char *ifname, |
| 1411 | u_char distance, u_int32_t vrf_id) |
| 1412 | { |
| 1413 | u_char type = 0; |
| 1414 | struct route_node *rn; |
| 1415 | struct static_ipv4 *si; |
| 1416 | struct static_ipv4 *pp; |
| 1417 | struct static_ipv4 *cp; |
| 1418 | struct static_ipv4 *update = NULL; |
| 1419 | struct route_table *stable; |
| 1420 | |
| 1421 | /* Lookup table. */ |
| 1422 | stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id); |
| 1423 | if (! stable) |
| 1424 | return -1; |
| 1425 | |
| 1426 | /* Lookup static route prefix. */ |
| 1427 | rn = route_node_get (stable, p); |
| 1428 | |
| 1429 | /* Make flags. */ |
| 1430 | if (gate) |
| 1431 | type = STATIC_IPV4_GATEWAY; |
| 1432 | else if (ifname) |
| 1433 | type = STATIC_IPV4_IFNAME; |
| 1434 | else |
| 1435 | type = STATIC_IPV4_BLACKHOLE; |
| 1436 | |
| 1437 | /* Do nothing if there is a same static route. */ |
| 1438 | for (si = rn->info; si; si = si->next) |
| 1439 | { |
| 1440 | if (type == si->type |
| 1441 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 1442 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 1443 | { |
| 1444 | if (distance == si->distance) |
| 1445 | { |
| 1446 | route_unlock_node (rn); |
| 1447 | return 0; |
| 1448 | } |
| 1449 | else |
| 1450 | update = si; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | /* Distance chaged. */ |
| 1455 | if (update) |
| 1456 | static_delete_ipv4 (p, gate, ifname, update->distance, vrf_id); |
| 1457 | |
| 1458 | /* Make new static route structure. */ |
| 1459 | si = XMALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4)); |
| 1460 | memset (si, 0, sizeof (struct static_ipv4)); |
| 1461 | |
| 1462 | si->type = type; |
| 1463 | si->distance = distance; |
| 1464 | |
| 1465 | if (gate) |
| 1466 | si->gate.ipv4 = *gate; |
| 1467 | if (ifname) |
| 1468 | si->gate.ifname = XSTRDUP (0, ifname); |
| 1469 | |
| 1470 | /* Add new static route information to the tree with sort by |
| 1471 | distance value and gateway address. */ |
| 1472 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 1473 | { |
| 1474 | if (si->distance < cp->distance) |
| 1475 | break; |
| 1476 | if (si->distance > cp->distance) |
| 1477 | continue; |
| 1478 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) |
| 1479 | { |
| 1480 | if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr)) |
| 1481 | break; |
| 1482 | if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr)) |
| 1483 | continue; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /* Make linked list. */ |
| 1488 | if (pp) |
| 1489 | pp->next = si; |
| 1490 | else |
| 1491 | rn->info = si; |
| 1492 | if (cp) |
| 1493 | cp->prev = si; |
| 1494 | si->prev = pp; |
| 1495 | si->next = cp; |
| 1496 | |
| 1497 | /* Install into rib. */ |
| 1498 | static_install_ipv4 (p, si); |
| 1499 | |
| 1500 | return 1; |
| 1501 | } |
| 1502 | |
| 1503 | /* Delete static route from static route configuration. */ |
| 1504 | int |
| 1505 | static_delete_ipv4 (struct prefix *p, struct in_addr *gate, char *ifname, |
| 1506 | u_char distance, u_int32_t vrf_id) |
| 1507 | { |
| 1508 | u_char type = 0; |
| 1509 | struct route_node *rn; |
| 1510 | struct static_ipv4 *si; |
| 1511 | struct route_table *stable; |
| 1512 | |
| 1513 | /* Lookup table. */ |
| 1514 | stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id); |
| 1515 | if (! stable) |
| 1516 | return -1; |
| 1517 | |
| 1518 | /* Lookup static route prefix. */ |
| 1519 | rn = route_node_lookup (stable, p); |
| 1520 | if (! rn) |
| 1521 | return 0; |
| 1522 | |
| 1523 | /* Make flags. */ |
| 1524 | if (gate) |
| 1525 | type = STATIC_IPV4_GATEWAY; |
| 1526 | else if (ifname) |
| 1527 | type = STATIC_IPV4_IFNAME; |
| 1528 | else |
| 1529 | type = STATIC_IPV4_BLACKHOLE; |
| 1530 | |
| 1531 | /* Find same static route is the tree */ |
| 1532 | for (si = rn->info; si; si = si->next) |
| 1533 | if (type == si->type |
| 1534 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 1535 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 1536 | break; |
| 1537 | |
| 1538 | /* Can't find static route. */ |
| 1539 | if (! si) |
| 1540 | { |
| 1541 | route_unlock_node (rn); |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /* Install into rib. */ |
| 1546 | static_uninstall_ipv4 (p, si); |
| 1547 | |
| 1548 | /* Unlink static route from linked list. */ |
| 1549 | if (si->prev) |
| 1550 | si->prev->next = si->next; |
| 1551 | else |
| 1552 | rn->info = si->next; |
| 1553 | if (si->next) |
| 1554 | si->next->prev = si->prev; |
| 1555 | |
| 1556 | /* Free static route configuration. */ |
| 1557 | XFREE (MTYPE_STATIC_IPV4, si); |
| 1558 | |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | #ifdef HAVE_IPV6 |
| 1564 | int |
| 1565 | rib_bogus_ipv6 (int type, struct prefix_ipv6 *p, |
| 1566 | struct in6_addr *gate, unsigned int ifindex, int table) |
| 1567 | { |
| 1568 | if (type == ZEBRA_ROUTE_CONNECT && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)) |
| 1569 | return 1; |
| 1570 | if (type == ZEBRA_ROUTE_KERNEL && IN6_IS_ADDR_UNSPECIFIED (&p->prefix) |
| 1571 | && p->prefixlen == 96 && gate && IN6_IS_ADDR_UNSPECIFIED (gate)) |
| 1572 | { |
| 1573 | kernel_delete_ipv6_old (p, gate, ifindex, 0, table); |
| 1574 | return 1; |
| 1575 | } |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | int |
| 1580 | rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
| 1581 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id) |
| 1582 | { |
| 1583 | struct rib *rib; |
| 1584 | struct rib *same = NULL; |
| 1585 | struct route_table *table; |
| 1586 | struct route_node *rn; |
| 1587 | struct nexthop *nexthop; |
| 1588 | |
| 1589 | int distance; |
| 1590 | u_int32_t metric = 0; |
| 1591 | |
| 1592 | /* Lookup table. */ |
| 1593 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 1594 | if (! table) |
| 1595 | return 0; |
| 1596 | |
| 1597 | /* Make sure mask is applied. */ |
| 1598 | apply_mask_ipv6 (p); |
| 1599 | |
| 1600 | /* Set default distance by route type. */ |
| 1601 | distance = route_info[type].distance; |
| 1602 | |
| 1603 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 1604 | distance = 200; |
| 1605 | |
| 1606 | /* Filter bogus route. */ |
| 1607 | if (rib_bogus_ipv6 (type, p, gate, ifindex, 0)) |
| 1608 | return 0; |
| 1609 | |
| 1610 | /* Lookup route node.*/ |
| 1611 | rn = route_node_get (table, (struct prefix *) p); |
| 1612 | |
| 1613 | /* If same type of route are installed, treat it as a implicit |
| 1614 | withdraw. */ |
| 1615 | for (rib = rn->info; rib; rib = rib->next) |
| 1616 | { |
| 1617 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1618 | { |
| 1619 | nexthop = rib->nexthop; |
| 1620 | |
| 1621 | if (rib->type == type |
| 1622 | && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1623 | && nexthop->ifindex == ifindex) |
| 1624 | { |
| 1625 | rib->refcnt++; |
| 1626 | return 0; |
| 1627 | } |
| 1628 | } |
| 1629 | else if (rib->type == type) |
| 1630 | { |
| 1631 | same = rib; |
| 1632 | rib_delnode (rn, same); |
| 1633 | route_unlock_node (rn); |
| 1634 | break; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | /* Allocate new rib structure. */ |
| 1639 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1640 | memset (rib, 0, sizeof (struct rib)); |
| 1641 | rib->type = type; |
| 1642 | rib->distance = distance; |
| 1643 | rib->flags = flags; |
| 1644 | rib->metric = metric; |
| 1645 | rib->nexthop_num = 0; |
| 1646 | rib->uptime = time (NULL); |
| 1647 | |
| 1648 | /* Nexthop settings. */ |
| 1649 | if (gate) |
| 1650 | { |
| 1651 | if (ifindex) |
| 1652 | nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
| 1653 | else |
| 1654 | nexthop_ipv6_add (rib, gate); |
| 1655 | } |
| 1656 | else |
| 1657 | nexthop_ifindex_add (rib, ifindex); |
| 1658 | |
| 1659 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1660 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 1661 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1662 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1663 | |
| 1664 | /* Link new rib to node.*/ |
| 1665 | rib_addnode (rn, rib); |
| 1666 | |
| 1667 | /* Process this route node. */ |
| 1668 | rib_process (rn, same); |
| 1669 | |
| 1670 | /* Free implicit route.*/ |
| 1671 | if (same) |
| 1672 | newrib_free (same); |
| 1673 | |
| 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | int |
| 1678 | rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
| 1679 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id) |
| 1680 | { |
| 1681 | struct route_table *table; |
| 1682 | struct route_node *rn; |
| 1683 | struct rib *rib; |
| 1684 | struct rib *fib = NULL; |
| 1685 | struct rib *same = NULL; |
| 1686 | struct nexthop *nexthop; |
| 1687 | char buf1[BUFSIZ]; |
| 1688 | char buf2[BUFSIZ]; |
| 1689 | |
| 1690 | /* Apply mask. */ |
| 1691 | apply_mask_ipv6 (p); |
| 1692 | |
| 1693 | /* Lookup table. */ |
| 1694 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 1695 | if (! table) |
| 1696 | return 0; |
| 1697 | |
| 1698 | /* Lookup route node. */ |
| 1699 | rn = route_node_lookup (table, (struct prefix *) p); |
| 1700 | if (! rn) |
| 1701 | { |
| 1702 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1703 | { |
| 1704 | if (gate) |
| 1705 | zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib", |
| 1706 | inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ), |
| 1707 | p->prefixlen, |
| 1708 | inet_ntop (AF_INET6, gate, buf2, BUFSIZ), |
| 1709 | ifindex); |
| 1710 | else |
| 1711 | zlog_info ("route %s/%d ifindex %d doesn't exist in rib", |
| 1712 | inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ), |
| 1713 | p->prefixlen, |
| 1714 | ifindex); |
| 1715 | } |
| 1716 | return ZEBRA_ERR_RTNOEXIST; |
| 1717 | } |
| 1718 | |
| 1719 | /* Lookup same type route. */ |
| 1720 | for (rib = rn->info; rib; rib = rib->next) |
| 1721 | { |
| 1722 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 1723 | fib = rib; |
| 1724 | |
| 1725 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1726 | { |
| 1727 | nexthop = rib->nexthop; |
| 1728 | |
| 1729 | if (rib->type == type |
| 1730 | && nexthop && nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1731 | && nexthop->ifindex == ifindex) |
| 1732 | { |
| 1733 | if (rib->refcnt) |
| 1734 | { |
| 1735 | rib->refcnt--; |
| 1736 | route_unlock_node (rn); |
| 1737 | route_unlock_node (rn); |
| 1738 | return 0; |
| 1739 | } |
| 1740 | same = rib; |
| 1741 | break; |
| 1742 | } |
| 1743 | } |
| 1744 | else |
| 1745 | { |
| 1746 | if (rib->type == type) |
| 1747 | { |
| 1748 | same = rib; |
| 1749 | break; |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* If same type of route can't be found and this message is from |
| 1755 | kernel. */ |
| 1756 | if (! same) |
| 1757 | { |
| 1758 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 1759 | { |
| 1760 | /* Unset flags. */ |
| 1761 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 1762 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1763 | |
| 1764 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 1765 | } |
| 1766 | else |
| 1767 | { |
| 1768 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1769 | { |
| 1770 | if (gate) |
| 1771 | zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
| 1772 | inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ), |
| 1773 | p->prefixlen, |
| 1774 | inet_ntop (AF_INET6, gate, buf2, BUFSIZ), |
| 1775 | ifindex, |
| 1776 | type); |
| 1777 | else |
| 1778 | zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib", |
| 1779 | inet_ntop (AF_INET6, &p->prefix, buf1, BUFSIZ), |
| 1780 | p->prefixlen, |
| 1781 | ifindex, |
| 1782 | type); |
| 1783 | } |
| 1784 | route_unlock_node (rn); |
| 1785 | return ZEBRA_ERR_RTNOEXIST; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | if (same) |
| 1790 | rib_delnode (rn, same); |
| 1791 | |
| 1792 | /* Process changes. */ |
| 1793 | rib_process (rn, same); |
| 1794 | |
| 1795 | if (same) |
| 1796 | { |
| 1797 | newrib_free (same); |
| 1798 | route_unlock_node (rn); |
| 1799 | } |
| 1800 | |
| 1801 | route_unlock_node (rn); |
| 1802 | |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | /* Install static route into rib. */ |
| 1807 | void |
| 1808 | static_install_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 1809 | { |
| 1810 | struct rib *rib; |
| 1811 | struct route_table *table; |
| 1812 | struct route_node *rn; |
| 1813 | |
| 1814 | /* Lookup table. */ |
| 1815 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 1816 | if (! table) |
| 1817 | return; |
| 1818 | |
| 1819 | /* Lookup existing route */ |
| 1820 | rn = route_node_get (table, p); |
| 1821 | for (rib = rn->info; rib; rib = rib->next) |
| 1822 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 1823 | break; |
| 1824 | |
| 1825 | if (rib) |
| 1826 | { |
| 1827 | /* Same distance static route is there. Update it with new |
| 1828 | nexthop. */ |
| 1829 | rib_uninstall (rn, rib); |
| 1830 | route_unlock_node (rn); |
| 1831 | |
| 1832 | switch (si->type) |
| 1833 | { |
| 1834 | case STATIC_IPV6_GATEWAY: |
| 1835 | nexthop_ipv6_add (rib, &si->ipv6); |
| 1836 | break; |
| 1837 | case STATIC_IPV6_IFNAME: |
| 1838 | nexthop_ifname_add (rib, si->ifname); |
| 1839 | break; |
| 1840 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 1841 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 1842 | break; |
| 1843 | } |
| 1844 | rib_process (rn, NULL); |
| 1845 | } |
| 1846 | else |
| 1847 | { |
| 1848 | /* This is new static route. */ |
| 1849 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1850 | memset (rib, 0, sizeof (struct rib)); |
| 1851 | |
| 1852 | rib->type = ZEBRA_ROUTE_STATIC; |
| 1853 | rib->distance = si->distance; |
| 1854 | rib->metric = 0; |
| 1855 | rib->nexthop_num = 0; |
| 1856 | |
| 1857 | switch (si->type) |
| 1858 | { |
| 1859 | case STATIC_IPV6_GATEWAY: |
| 1860 | nexthop_ipv6_add (rib, &si->ipv6); |
| 1861 | break; |
| 1862 | case STATIC_IPV6_IFNAME: |
| 1863 | nexthop_ifname_add (rib, si->ifname); |
| 1864 | break; |
| 1865 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 1866 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 1867 | break; |
| 1868 | } |
| 1869 | |
| 1870 | /* Link this rib to the tree. */ |
| 1871 | rib_addnode (rn, rib); |
| 1872 | |
| 1873 | /* Process this prefix. */ |
| 1874 | rib_process (rn, NULL); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | int |
| 1879 | static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si) |
| 1880 | { |
| 1881 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1882 | && si->type == STATIC_IPV6_GATEWAY |
| 1883 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)) |
| 1884 | return 1; |
| 1885 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1886 | && si->type == STATIC_IPV6_IFNAME |
| 1887 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 1888 | return 1; |
| 1889 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1890 | && si->type == STATIC_IPV6_GATEWAY_IFNAME |
| 1891 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6) |
| 1892 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 1893 | return 1; |
| 1894 | return 0;; |
| 1895 | } |
| 1896 | |
| 1897 | void |
| 1898 | static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 1899 | { |
| 1900 | struct route_table *table; |
| 1901 | struct route_node *rn; |
| 1902 | struct rib *rib; |
| 1903 | struct nexthop *nexthop; |
| 1904 | |
| 1905 | /* Lookup table. */ |
| 1906 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 1907 | if (! table) |
| 1908 | return; |
| 1909 | |
| 1910 | /* Lookup existing route with type and distance. */ |
| 1911 | rn = route_node_lookup (table, (struct prefix *) p); |
| 1912 | if (! rn) |
| 1913 | return; |
| 1914 | |
| 1915 | for (rib = rn->info; rib; rib = rib->next) |
| 1916 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 1917 | break; |
| 1918 | if (! rib) |
| 1919 | { |
| 1920 | route_unlock_node (rn); |
| 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | /* Lookup nexthop. */ |
| 1925 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1926 | if (static_ipv6_nexthop_same (nexthop, si)) |
| 1927 | break; |
| 1928 | |
| 1929 | /* Can't find nexthop. */ |
| 1930 | if (! nexthop) |
| 1931 | { |
| 1932 | route_unlock_node (rn); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | /* Check nexthop. */ |
| 1937 | if (rib->nexthop_num == 1) |
| 1938 | { |
| 1939 | rib_delnode (rn, rib); |
| 1940 | rib_process (rn, rib); |
| 1941 | newrib_free (rib); |
| 1942 | route_unlock_node (rn); |
| 1943 | } |
| 1944 | else |
| 1945 | { |
| 1946 | rib_uninstall (rn, rib); |
| 1947 | nexthop_delete (rib, nexthop); |
| 1948 | nexthop_free (nexthop); |
| 1949 | rib_process (rn, rib); |
| 1950 | } |
| 1951 | |
| 1952 | /* Unlock node. */ |
| 1953 | route_unlock_node (rn); |
| 1954 | } |
| 1955 | |
| 1956 | /* Add static route into static route configuration. */ |
| 1957 | int |
| 1958 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
| 1959 | char *ifname, u_char distance, u_int32_t vrf_id) |
| 1960 | { |
| 1961 | struct route_node *rn; |
| 1962 | struct static_ipv6 *si; |
| 1963 | struct static_ipv6 *pp; |
| 1964 | struct static_ipv6 *cp; |
| 1965 | struct route_table *stable; |
| 1966 | |
| 1967 | /* Lookup table. */ |
| 1968 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 1969 | if (! stable) |
| 1970 | return -1; |
| 1971 | |
| 1972 | /* Lookup static route prefix. */ |
| 1973 | rn = route_node_get (stable, p); |
| 1974 | |
| 1975 | /* Do nothing if there is a same static route. */ |
| 1976 | for (si = rn->info; si; si = si->next) |
| 1977 | { |
| 1978 | if (distance == si->distance |
| 1979 | && type == si->type |
| 1980 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 1981 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 1982 | { |
| 1983 | route_unlock_node (rn); |
| 1984 | return 0; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | /* Make new static route structure. */ |
| 1989 | si = XMALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6)); |
| 1990 | memset (si, 0, sizeof (struct static_ipv6)); |
| 1991 | |
| 1992 | si->type = type; |
| 1993 | si->distance = distance; |
| 1994 | |
| 1995 | switch (type) |
| 1996 | { |
| 1997 | case STATIC_IPV6_GATEWAY: |
| 1998 | si->ipv6 = *gate; |
| 1999 | break; |
| 2000 | case STATIC_IPV6_IFNAME: |
| 2001 | si->ifname = XSTRDUP (0, ifname); |
| 2002 | break; |
| 2003 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2004 | si->ipv6 = *gate; |
| 2005 | si->ifname = XSTRDUP (0, ifname); |
| 2006 | break; |
| 2007 | } |
| 2008 | |
| 2009 | /* Add new static route information to the tree with sort by |
| 2010 | distance value and gateway address. */ |
| 2011 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2012 | { |
| 2013 | if (si->distance < cp->distance) |
| 2014 | break; |
| 2015 | if (si->distance > cp->distance) |
| 2016 | continue; |
| 2017 | } |
| 2018 | |
| 2019 | /* Make linked list. */ |
| 2020 | if (pp) |
| 2021 | pp->next = si; |
| 2022 | else |
| 2023 | rn->info = si; |
| 2024 | if (cp) |
| 2025 | cp->prev = si; |
| 2026 | si->prev = pp; |
| 2027 | si->next = cp; |
| 2028 | |
| 2029 | /* Install into rib. */ |
| 2030 | static_install_ipv6 (p, si); |
| 2031 | |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
| 2035 | /* Delete static route from static route configuration. */ |
| 2036 | int |
| 2037 | static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
| 2038 | char *ifname, u_char distance, u_int32_t vrf_id) |
| 2039 | { |
| 2040 | struct route_node *rn; |
| 2041 | struct static_ipv6 *si; |
| 2042 | struct route_table *stable; |
| 2043 | |
| 2044 | /* Lookup table. */ |
| 2045 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 2046 | if (! stable) |
| 2047 | return -1; |
| 2048 | |
| 2049 | /* Lookup static route prefix. */ |
| 2050 | rn = route_node_lookup (stable, p); |
| 2051 | if (! rn) |
| 2052 | return 0; |
| 2053 | |
| 2054 | /* Find same static route is the tree */ |
| 2055 | for (si = rn->info; si; si = si->next) |
| 2056 | if (distance == si->distance |
| 2057 | && type == si->type |
| 2058 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 2059 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2060 | break; |
| 2061 | |
| 2062 | /* Can't find static route. */ |
| 2063 | if (! si) |
| 2064 | { |
| 2065 | route_unlock_node (rn); |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | /* Install into rib. */ |
| 2070 | static_uninstall_ipv6 (p, si); |
| 2071 | |
| 2072 | /* Unlink static route from linked list. */ |
| 2073 | if (si->prev) |
| 2074 | si->prev->next = si->next; |
| 2075 | else |
| 2076 | rn->info = si->next; |
| 2077 | if (si->next) |
| 2078 | si->next->prev = si->prev; |
| 2079 | |
| 2080 | /* Free static route configuration. */ |
| 2081 | XFREE (MTYPE_STATIC_IPV6, si); |
| 2082 | |
| 2083 | return 1; |
| 2084 | } |
| 2085 | #endif /* HAVE_IPV6 */ |
| 2086 | |
| 2087 | /* RIB update function. */ |
| 2088 | void |
| 2089 | rib_update () |
| 2090 | { |
| 2091 | struct route_node *rn; |
| 2092 | struct route_table *table; |
| 2093 | |
| 2094 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 2095 | if (table) |
| 2096 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 2097 | rib_process (rn, NULL); |
| 2098 | |
| 2099 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 2100 | if (table) |
| 2101 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 2102 | rib_process (rn, NULL); |
| 2103 | } |
| 2104 | |
| 2105 | /* Interface goes up. */ |
| 2106 | void |
| 2107 | rib_if_up (struct interface *ifp) |
| 2108 | { |
| 2109 | rib_update (); |
| 2110 | } |
| 2111 | |
| 2112 | /* Interface goes down. */ |
| 2113 | void |
| 2114 | rib_if_down (struct interface *ifp) |
| 2115 | { |
| 2116 | rib_update (); |
| 2117 | } |
| 2118 | |
| 2119 | /* Remove all routes which comes from non main table. */ |
| 2120 | void |
| 2121 | rib_weed_table (struct route_table *table) |
| 2122 | { |
| 2123 | struct route_node *rn; |
| 2124 | struct rib *rib; |
| 2125 | struct rib *next; |
| 2126 | |
| 2127 | if (table) |
| 2128 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 2129 | for (rib = rn->info; rib; rib = next) |
| 2130 | { |
| 2131 | next = rib->next; |
| 2132 | |
| 2133 | if (rib->table != rtm_table_default && |
| 2134 | rib->table != RT_TABLE_MAIN) |
| 2135 | { |
| 2136 | rib_delnode (rn, rib); |
| 2137 | newrib_free (rib); |
| 2138 | route_unlock_node (rn); |
| 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | /* Delete all routes from non main table. */ |
| 2144 | void |
| 2145 | rib_weed_tables () |
| 2146 | { |
| 2147 | rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 2148 | rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 2149 | } |
| 2150 | |
| 2151 | /* Delete self installed routes after zebra is relaunched. */ |
| 2152 | void |
| 2153 | rib_sweep_table (struct route_table *table) |
| 2154 | { |
| 2155 | struct route_node *rn; |
| 2156 | struct rib *rib; |
| 2157 | struct rib *next; |
| 2158 | int ret = 0; |
| 2159 | |
| 2160 | if (table) |
| 2161 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 2162 | for (rib = rn->info; rib; rib = next) |
| 2163 | { |
| 2164 | next = rib->next; |
| 2165 | |
| 2166 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
| 2167 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) |
| 2168 | { |
| 2169 | ret = rib_uninstall_kernel (rn, rib); |
| 2170 | if (! ret) |
| 2171 | { |
| 2172 | rib_delnode (rn, rib); |
| 2173 | newrib_free (rib); |
| 2174 | route_unlock_node (rn); |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | /* Sweep all RIB tables. */ |
| 2181 | void |
| 2182 | rib_sweep_route () |
| 2183 | { |
| 2184 | rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 2185 | rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 2186 | } |
| 2187 | |
| 2188 | /* Close RIB and clean up kernel routes. */ |
| 2189 | void |
| 2190 | rib_close_table (struct route_table *table) |
| 2191 | { |
| 2192 | struct route_node *rn; |
| 2193 | struct rib *rib; |
| 2194 | |
| 2195 | if (table) |
| 2196 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 2197 | for (rib = rn->info; rib; rib = rib->next) |
| 2198 | if (! RIB_SYSTEM_ROUTE (rib) |
| 2199 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 2200 | rib_uninstall_kernel (rn, rib); |
| 2201 | } |
| 2202 | |
| 2203 | /* Close all RIB tables. */ |
| 2204 | void |
| 2205 | rib_close () |
| 2206 | { |
| 2207 | rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 2208 | rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 2209 | } |
| 2210 | |
| 2211 | /* Routing information base initialize. */ |
| 2212 | void |
| 2213 | rib_init () |
| 2214 | { |
| 2215 | /* VRF initialization. */ |
| 2216 | vrf_init (); |
| 2217 | } |