paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF Link State Advertisement |
| 3 | * Copyright (C) 1999, 2000 Toshiaki Takada |
| 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 "linklist.h" |
| 26 | #include "prefix.h" |
| 27 | #include "if.h" |
| 28 | #include "table.h" |
| 29 | #include "memory.h" |
| 30 | #include "stream.h" |
| 31 | #include "log.h" |
| 32 | #include "thread.h" |
| 33 | #include "hash.h" |
| 34 | #include "sockunion.h" /* for inet_aton() */ |
| 35 | |
| 36 | #include "ospfd/ospfd.h" |
| 37 | #include "ospfd/ospf_interface.h" |
| 38 | #include "ospfd/ospf_ism.h" |
| 39 | #include "ospfd/ospf_asbr.h" |
| 40 | #include "ospfd/ospf_lsa.h" |
| 41 | #include "ospfd/ospf_lsdb.h" |
| 42 | #include "ospfd/ospf_neighbor.h" |
| 43 | #include "ospfd/ospf_nsm.h" |
| 44 | #include "ospfd/ospf_flood.h" |
| 45 | #include "ospfd/ospf_packet.h" |
| 46 | #include "ospfd/ospf_spf.h" |
| 47 | #include "ospfd/ospf_dump.h" |
| 48 | #include "ospfd/ospf_route.h" |
| 49 | #include "ospfd/ospf_ase.h" |
| 50 | #include "ospfd/ospf_zebra.h" |
| 51 | |
| 52 | |
| 53 | u_int32_t |
| 54 | get_metric (u_char *metric) |
| 55 | { |
| 56 | u_int32_t m; |
| 57 | m = metric[0]; |
| 58 | m = (m << 8) + metric[1]; |
| 59 | m = (m << 8) + metric[2]; |
| 60 | return m; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | struct timeval |
| 65 | tv_adjust (struct timeval a) |
| 66 | { |
| 67 | while (a.tv_usec >= 1000000) |
| 68 | { |
| 69 | a.tv_usec -= 1000000; |
| 70 | a.tv_sec++; |
| 71 | } |
| 72 | |
| 73 | while (a.tv_usec < 0) |
| 74 | { |
| 75 | a.tv_usec += 1000000; |
| 76 | a.tv_sec--; |
| 77 | } |
| 78 | |
| 79 | return a; |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | tv_ceil (struct timeval a) |
| 84 | { |
| 85 | a = tv_adjust (a); |
| 86 | |
| 87 | return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | tv_floor (struct timeval a) |
| 92 | { |
| 93 | a = tv_adjust (a); |
| 94 | |
| 95 | return a.tv_sec; |
| 96 | } |
| 97 | |
| 98 | struct timeval |
| 99 | int2tv (int a) |
| 100 | { |
| 101 | struct timeval ret; |
| 102 | |
| 103 | ret.tv_sec = a; |
| 104 | ret.tv_usec = 0; |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | struct timeval |
| 110 | tv_add (struct timeval a, struct timeval b) |
| 111 | { |
| 112 | struct timeval ret; |
| 113 | |
| 114 | ret.tv_sec = a.tv_sec + b.tv_sec; |
| 115 | ret.tv_usec = a.tv_usec + b.tv_usec; |
| 116 | |
| 117 | return tv_adjust (ret); |
| 118 | } |
| 119 | |
| 120 | struct timeval |
| 121 | tv_sub (struct timeval a, struct timeval b) |
| 122 | { |
| 123 | struct timeval ret; |
| 124 | |
| 125 | ret.tv_sec = a.tv_sec - b.tv_sec; |
| 126 | ret.tv_usec = a.tv_usec - b.tv_usec; |
| 127 | |
| 128 | return tv_adjust (ret); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | tv_cmp (struct timeval a, struct timeval b) |
| 133 | { |
| 134 | return (a.tv_sec == b.tv_sec ? |
| 135 | a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec); |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | ospf_lsa_refresh_delay (struct ospf_lsa *lsa) |
| 140 | { |
| 141 | struct timeval delta, now; |
| 142 | int delay = 0; |
| 143 | |
| 144 | gettimeofday (&now, NULL); |
| 145 | delta = tv_sub (now, lsa->tv_orig); |
| 146 | |
| 147 | if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0) |
| 148 | { |
| 149 | delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta)); |
| 150 | |
| 151 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 152 | zlog_info ("LSA[Type%d:%s]: Refresh timer delay %d seconds", |
| 153 | lsa->data->type, inet_ntoa (lsa->data->id), delay); |
| 154 | |
| 155 | assert (delay > 0); |
| 156 | } |
| 157 | |
| 158 | return delay; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | int |
| 163 | get_age (struct ospf_lsa *lsa) |
| 164 | { |
| 165 | int age; |
| 166 | struct timeval now; |
| 167 | |
| 168 | gettimeofday (&now, NULL); |
| 169 | age = ntohs (lsa->data->ls_age) + tv_floor (tv_sub (now, lsa->tv_recv)); |
| 170 | |
| 171 | return age; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* Fletcher Checksum -- Refer to RFC1008. */ |
| 176 | #define MODX 4102 |
| 177 | #define LSA_CHECKSUM_OFFSET 15 |
| 178 | |
| 179 | u_int16_t |
| 180 | ospf_lsa_checksum (struct lsa_header *lsa) |
| 181 | { |
| 182 | u_char *sp, *ep, *p, *q; |
| 183 | int c0 = 0, c1 = 0; |
| 184 | int x, y; |
| 185 | u_int16_t length; |
| 186 | |
| 187 | lsa->checksum = 0; |
| 188 | length = ntohs (lsa->length) - 2; |
| 189 | sp = (char *) &lsa->options; |
| 190 | |
| 191 | for (ep = sp + length; sp < ep; sp = q) |
| 192 | { |
| 193 | q = sp + MODX; |
| 194 | if (q > ep) |
| 195 | q = ep; |
| 196 | for (p = sp; p < q; p++) |
| 197 | { |
| 198 | c0 += *p; |
| 199 | c1 += c0; |
| 200 | } |
| 201 | c0 %= 255; |
| 202 | c1 %= 255; |
| 203 | } |
| 204 | |
| 205 | x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255; |
| 206 | if (x <= 0) |
| 207 | x += 255; |
| 208 | y = 510 - c0 - x; |
| 209 | if (y > 255) |
| 210 | y -= 255; |
| 211 | |
| 212 | /* take care endian issue. */ |
| 213 | lsa->checksum = htons ((x << 8) + y); |
| 214 | |
| 215 | return (lsa->checksum); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | /* Create OSPF LSA. */ |
| 221 | struct ospf_lsa * |
| 222 | ospf_lsa_new () |
| 223 | { |
| 224 | struct ospf_lsa *new; |
| 225 | |
| 226 | new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa)); |
| 227 | memset (new, 0, sizeof (struct ospf_lsa)); |
| 228 | |
| 229 | new->flags = 0; |
| 230 | new->lock = 1; |
| 231 | new->retransmit_counter = 0; |
| 232 | gettimeofday (&new->tv_recv, NULL); |
| 233 | new->tv_orig = new->tv_recv; |
| 234 | new->refresh_list = -1; |
| 235 | |
| 236 | return new; |
| 237 | } |
| 238 | |
| 239 | /* Duplicate OSPF LSA. */ |
| 240 | struct ospf_lsa * |
| 241 | ospf_lsa_dup (struct ospf_lsa *lsa) |
| 242 | { |
| 243 | struct ospf_lsa *new; |
| 244 | |
| 245 | if (lsa == NULL) |
| 246 | return NULL; |
| 247 | |
| 248 | new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa)); |
| 249 | |
| 250 | memcpy (new, lsa, sizeof (struct ospf_lsa)); |
| 251 | UNSET_FLAG (new->flags, OSPF_LSA_DISCARD); |
| 252 | new->lock = 1; |
| 253 | new->retransmit_counter = 0; |
| 254 | new->data = ospf_lsa_data_dup (lsa->data); |
| 255 | |
| 256 | return new; |
| 257 | } |
| 258 | |
| 259 | /* Free OSPF LSA. */ |
| 260 | void |
| 261 | ospf_lsa_free (struct ospf_lsa *lsa) |
| 262 | { |
| 263 | assert (lsa->lock == 0); |
| 264 | |
| 265 | if (IS_DEBUG_OSPF (lsa, LSA)) |
| 266 | zlog_info ("LSA: freed %p", lsa); |
| 267 | |
| 268 | /* Delete LSA data. */ |
| 269 | if (lsa->data != NULL) |
| 270 | ospf_lsa_data_free (lsa->data); |
| 271 | |
| 272 | assert (lsa->refresh_list < 0); |
| 273 | |
| 274 | memset (lsa, 0, sizeof (struct ospf_lsa)); |
| 275 | XFREE (MTYPE_OSPF_LSA, lsa); |
| 276 | } |
| 277 | |
| 278 | /* Lock LSA. */ |
| 279 | struct ospf_lsa * |
| 280 | ospf_lsa_lock (struct ospf_lsa *lsa) |
| 281 | { |
| 282 | lsa->lock++; |
| 283 | return lsa; |
| 284 | } |
| 285 | |
| 286 | /* Unlock LSA. */ |
| 287 | void |
| 288 | ospf_lsa_unlock (struct ospf_lsa *lsa) |
| 289 | { |
| 290 | /* This is sanity check. */ |
| 291 | if (!lsa) |
| 292 | return; |
| 293 | |
| 294 | lsa->lock--; |
| 295 | |
| 296 | assert (lsa->lock >= 0); |
| 297 | |
| 298 | if (lsa->lock == 0) |
| 299 | { |
| 300 | assert (CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD)); |
| 301 | ospf_lsa_free (lsa); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* Check discard flag. */ |
| 306 | void |
| 307 | ospf_lsa_discard (struct ospf_lsa *lsa) |
| 308 | { |
| 309 | if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD)) |
| 310 | { |
| 311 | SET_FLAG (lsa->flags, OSPF_LSA_DISCARD); |
| 312 | ospf_lsa_unlock (lsa); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* Create LSA data. */ |
| 317 | struct lsa_header * |
| 318 | ospf_lsa_data_new (size_t size) |
| 319 | { |
| 320 | struct lsa_header *new; |
| 321 | |
| 322 | new = (struct lsa_header *) XMALLOC (MTYPE_OSPF_LSA_DATA, size); |
| 323 | memset (new, 0, size); |
| 324 | |
| 325 | return new; |
| 326 | } |
| 327 | |
| 328 | /* Duplicate LSA data. */ |
| 329 | struct lsa_header * |
| 330 | ospf_lsa_data_dup (struct lsa_header *lsah) |
| 331 | { |
| 332 | struct lsa_header *new; |
| 333 | |
| 334 | new = ospf_lsa_data_new (ntohs (lsah->length)); |
| 335 | memcpy (new, lsah, ntohs (lsah->length)); |
| 336 | |
| 337 | return new; |
| 338 | } |
| 339 | |
| 340 | /* Free LSA data. */ |
| 341 | void |
| 342 | ospf_lsa_data_free (struct lsa_header *lsah) |
| 343 | { |
| 344 | if (IS_DEBUG_OSPF (lsa, LSA)) |
| 345 | zlog_info ("LSA[Type%d:%s]: data freed %p", |
| 346 | lsah->type, inet_ntoa (lsah->id), lsah); |
| 347 | |
| 348 | XFREE (MTYPE_OSPF_LSA_DATA, lsah); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /* LSA general functions. */ |
| 353 | |
| 354 | const char * |
| 355 | dump_lsa_key (struct ospf_lsa *lsa) |
| 356 | { |
| 357 | static char buf[] = { |
| 358 | "Type255,id(255.255.255.255),ar(255.255.255.255)", |
| 359 | }; |
| 360 | struct lsa_header *lsah; |
| 361 | |
| 362 | if (lsa != NULL && (lsah = lsa->data) != NULL) |
| 363 | { |
| 364 | char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN]; |
| 365 | strcpy (id, inet_ntoa (lsah->id)); |
| 366 | strcpy (ar, inet_ntoa (lsah->adv_router)); |
| 367 | |
| 368 | sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar); |
| 369 | } |
| 370 | else |
| 371 | strcpy (buf, "NULL"); |
| 372 | |
| 373 | return buf; |
| 374 | } |
| 375 | |
| 376 | u_int32_t |
| 377 | lsa_seqnum_increment (struct ospf_lsa *lsa) |
| 378 | { |
| 379 | u_int32_t seqnum; |
| 380 | |
| 381 | seqnum = ntohl (lsa->data->ls_seqnum) + 1; |
| 382 | |
| 383 | return htonl (seqnum); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | lsa_header_set (struct stream *s, u_char options, |
| 388 | u_char type, struct in_addr id) |
| 389 | { |
| 390 | struct lsa_header *lsah; |
| 391 | |
| 392 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 393 | |
| 394 | lsah->ls_age = htons (0); |
| 395 | lsah->options = options; |
| 396 | lsah->type = type; |
| 397 | lsah->id = id; |
| 398 | lsah->adv_router = ospf_top->router_id; |
| 399 | lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER); |
| 400 | |
| 401 | ospf_output_forward (s, OSPF_LSA_HEADER_SIZE); |
| 402 | } |
| 403 | |
| 404 | /* router-LSA related functions. */ |
| 405 | /* Get router-LSA flags. */ |
| 406 | u_char |
| 407 | router_lsa_flags (struct ospf_area *area) |
| 408 | { |
| 409 | u_char flags; |
| 410 | |
| 411 | flags = ospf_top->flags; |
| 412 | |
| 413 | /* Set virtual link flag. */ |
| 414 | if (ospf_full_virtual_nbrs (area)) |
| 415 | SET_FLAG (flags, ROUTER_LSA_VIRTUAL); |
| 416 | else |
| 417 | /* Just sanity check */ |
| 418 | UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL); |
| 419 | |
| 420 | /* Set Shortcut ABR behabiour flag. */ |
| 421 | UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT); |
| 422 | if (ospf_top->abr_type == OSPF_ABR_SHORTCUT) |
| 423 | if (!OSPF_IS_AREA_BACKBONE (area)) |
| 424 | if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT && |
| 425 | !ospf_top->backbone) || |
| 426 | area->shortcut_configured == OSPF_SHORTCUT_ENABLE) |
| 427 | SET_FLAG (flags, ROUTER_LSA_SHORTCUT); |
| 428 | |
| 429 | /* ASBR can't exit in stub area. */ |
| 430 | if (area->external_routing == OSPF_AREA_STUB) |
| 431 | UNSET_FLAG (flags, OSPF_FLAG_ASBR); |
| 432 | |
| 433 | return flags; |
| 434 | } |
| 435 | |
| 436 | /* Lookup neighbor other than myself. |
| 437 | And check neighbor count, |
| 438 | Point-to-Point link must have only 1 neighbor. */ |
| 439 | struct ospf_neighbor * |
| 440 | ospf_nbr_lookup_ptop (struct route_table *nbrs, struct in_addr router_id) |
| 441 | { |
| 442 | struct route_node *rn; |
| 443 | struct ospf_neighbor *nbr = NULL; |
| 444 | |
| 445 | /* Search neighbor, there must be one of two nbrs. */ |
| 446 | for (rn = route_top (nbrs); rn; rn = route_next (rn)) |
| 447 | if ((nbr = rn->info) != NULL) |
| 448 | /* Ignore myself. */ |
| 449 | if (!IPV4_ADDR_SAME (&nbr->router_id, &ospf_top->router_id)) |
| 450 | if (nbr->state == NSM_Full) |
| 451 | break; |
| 452 | |
| 453 | /* PtoP link must have only 1 neighbor. */ |
| 454 | if (ospf_nbr_count (nbrs, 0) > 1) |
| 455 | zlog_warn ("Point-to-Point link has more than 1 neighobrs."); |
| 456 | |
| 457 | return nbr; |
| 458 | } |
| 459 | |
| 460 | /* Set a link information. */ |
| 461 | void |
| 462 | link_info_set (struct stream *s, struct in_addr id, |
| 463 | struct in_addr data, u_char type, u_char tos, u_int16_t cost) |
| 464 | { |
| 465 | /* TOS based routing is not supported. */ |
| 466 | stream_put_ipv4 (s, id.s_addr); /* Link ID. */ |
| 467 | stream_put_ipv4 (s, data.s_addr); /* Link Data. */ |
| 468 | stream_putc (s, type); /* Link Type. */ |
| 469 | stream_putc (s, tos); /* TOS = 0. */ |
| 470 | stream_putw (s, cost); /* Link Cost. */ |
| 471 | } |
| 472 | |
| 473 | /* Describe Point-to-Point link. */ |
| 474 | int |
| 475 | lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi) |
| 476 | { |
| 477 | int links = 0; |
| 478 | struct ospf_neighbor *nbr; |
| 479 | struct in_addr id, mask; |
| 480 | |
| 481 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 482 | zlog_info ("LSA[Type1]: Set link Point-to-Point"); |
| 483 | |
| 484 | if ((nbr = ospf_nbr_lookup_ptop (oi->nbrs, ospf_top->router_id))) |
| 485 | if (nbr->state == NSM_Full) |
| 486 | { |
| 487 | /* For unnumbered point-to-point networks, the Link Data field |
| 488 | should specify the interface's MIB-II ifIndex value. */ |
| 489 | link_info_set (s, nbr->router_id, oi->address->u.prefix4, |
| 490 | LSA_LINK_TYPE_POINTOPOINT, 0, oi->output_cost); |
| 491 | links++; |
| 492 | } |
| 493 | |
| 494 | if (oi->connected->destination != NULL) |
| 495 | { |
| 496 | /* Option 1: |
| 497 | link_type = LSA_LINK_TYPE_STUB; |
| 498 | link_id = nbr->address.u.prefix4; |
| 499 | link_data.s_addr = 0xffffffff; |
| 500 | link_cost = o->output_cost; */ |
| 501 | |
| 502 | id.s_addr = oi->connected->destination->u.prefix4.s_addr; |
| 503 | mask.s_addr = 0xffffffff; |
| 504 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | /* Option 2: We need to include link to a stub |
| 509 | network regardless of the state of the neighbor */ |
| 510 | masklen2ip (oi->address->prefixlen, &mask); |
| 511 | id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr; |
| 512 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost); |
| 513 | } |
| 514 | links++; |
| 515 | |
| 516 | return links; |
| 517 | } |
| 518 | |
| 519 | /* Describe Broadcast Link. */ |
| 520 | int |
| 521 | lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi) |
| 522 | { |
| 523 | struct ospf_neighbor *dr; |
| 524 | struct in_addr id, mask; |
| 525 | |
| 526 | /* Describe Type 3 Link. */ |
| 527 | if (oi->state == ISM_Waiting) |
| 528 | { |
| 529 | masklen2ip (oi->address->prefixlen, &mask); |
| 530 | id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr; |
| 531 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost); |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi)); |
| 536 | /* Describe Type 2 link. */ |
| 537 | if (dr && (dr->state == NSM_Full || |
| 538 | IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) && |
| 539 | ospf_nbr_count (oi->nbrs, NSM_Full) > 0) |
| 540 | { |
| 541 | link_info_set (s, DR (oi), oi->address->u.prefix4, |
| 542 | LSA_LINK_TYPE_TRANSIT, 0, oi->output_cost); |
| 543 | } |
| 544 | /* Describe type 3 link. */ |
| 545 | else |
| 546 | { |
| 547 | masklen2ip (oi->address->prefixlen, &mask); |
| 548 | id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr; |
| 549 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost); |
| 550 | } |
| 551 | return 1; |
| 552 | } |
| 553 | |
| 554 | int |
| 555 | lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi) |
| 556 | { |
| 557 | struct in_addr id, mask; |
| 558 | |
| 559 | /* Describe Type 3 Link. */ |
| 560 | if (oi->state != ISM_Loopback) |
| 561 | return 0; |
| 562 | |
| 563 | mask.s_addr = 0xffffffff; |
| 564 | id.s_addr = oi->address->u.prefix4.s_addr; |
| 565 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost); |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* Describe Virtual Link. */ |
| 570 | int |
| 571 | lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi) |
| 572 | { |
| 573 | struct ospf_neighbor *nbr; |
| 574 | |
| 575 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 576 | zlog_info ("LSA[Type1]: Set link type VL, state %d", oi->state); |
| 577 | |
| 578 | if (oi->state == ISM_PointToPoint) |
| 579 | if ((nbr = ospf_nbr_lookup_ptop (oi->nbrs, ospf_top->router_id))) |
| 580 | if (nbr->state == NSM_Full) |
| 581 | { |
| 582 | link_info_set (s, nbr->router_id, oi->address->u.prefix4, |
| 583 | LSA_LINK_TYPE_VIRTUALLINK, 0, oi->output_cost); |
| 584 | return 1; |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O) |
| 591 | |
paul | 7afa08d | 2002-12-13 20:59:45 +0000 | [diff] [blame] | 592 | /* this function add for support point-to-multipoint ,see rfc2328 |
| 593 | 12.4.1.4.*/ |
| 594 | /* from "edward rrr" <edward_rrr@hotmail.com> |
| 595 | http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */ |
| 596 | int lsa_link_ptomultip_set (struct stream *s, struct ospf_interface *oi) |
| 597 | { |
| 598 | int links = 0; |
| 599 | struct route_node *rn; |
| 600 | struct ospf_neighbor *nbr = NULL; |
| 601 | struct in_addr id, mask; |
| 602 | |
| 603 | mask.s_addr = 0xffffffff; |
| 604 | id.s_addr = oi->address->u.prefix4.s_addr; |
| 605 | link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0); |
| 606 | links++; |
| 607 | |
| 608 | zlog_info ("PointToMultipoint: running ptomultip_set"); |
| 609 | |
| 610 | /* Search neighbor, */ |
| 611 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 612 | if ((nbr = rn->info) != NULL) |
| 613 | /* Ignore myself. */ |
| 614 | if (!IPV4_ADDR_SAME (&nbr->router_id, &ospf_top->router_id)) |
| 615 | if (nbr->state == NSM_Full) |
| 616 | |
| 617 | { |
| 618 | |
| 619 | link_info_set (s, nbr->router_id, oi->address->u.prefix4, |
| 620 | LSA_LINK_TYPE_POINTOPOINT, 0, oi->output_cost); |
| 621 | links++; |
| 622 | zlog_info ("PointToMultipoint: set link to %s", |
| 623 | inet_ntoa(oi->address->u.prefix4)); |
| 624 | } |
| 625 | |
| 626 | return links; |
| 627 | |
| 628 | } |
| 629 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | /* Set router-LSA link information. */ |
| 631 | int |
| 632 | router_lsa_link_set (struct stream *s, struct ospf_area *area) |
| 633 | { |
| 634 | listnode node; |
| 635 | int links = 0; |
| 636 | |
| 637 | for (node = listhead (area->oiflist); node; node = nextnode (node)) |
| 638 | { |
| 639 | struct ospf_interface *oi = node->data; |
| 640 | struct interface *ifp = oi->ifp; |
| 641 | |
| 642 | /* Check interface is up, OSPF is enable. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 643 | if (if_is_operative (ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 644 | { |
| 645 | if (oi->state != ISM_Down) |
| 646 | { |
| 647 | /* Describe each link. */ |
| 648 | switch (oi->type) |
| 649 | { |
| 650 | case OSPF_IFTYPE_POINTOPOINT: |
| 651 | links += lsa_link_ptop_set (s, oi); |
| 652 | break; |
| 653 | case OSPF_IFTYPE_BROADCAST: |
| 654 | links += lsa_link_broadcast_set (s, oi); |
| 655 | break; |
| 656 | case OSPF_IFTYPE_NBMA: |
| 657 | links += lsa_link_nbma_set (s, oi); |
| 658 | break; |
| 659 | case OSPF_IFTYPE_POINTOMULTIPOINT: |
paul | 7afa08d | 2002-12-13 20:59:45 +0000 | [diff] [blame] | 660 | links += lsa_link_ptomultip_set (s, oi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | break; |
| 662 | case OSPF_IFTYPE_VIRTUALLINK: |
| 663 | links += lsa_link_virtuallink_set (s, oi); |
| 664 | break; |
| 665 | case OSPF_IFTYPE_LOOPBACK: |
| 666 | links += lsa_link_loopback_set (s, oi); |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return links; |
| 673 | } |
| 674 | |
| 675 | /* Set router-LSA body. */ |
| 676 | void |
| 677 | ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area) |
| 678 | { |
| 679 | unsigned long putp; |
| 680 | u_int16_t cnt; |
| 681 | |
| 682 | /* Set flags. */ |
| 683 | stream_putc (s, router_lsa_flags (area)); |
| 684 | |
| 685 | /* Set Zero fields. */ |
| 686 | stream_putc (s, 0); |
| 687 | |
| 688 | /* Keep pointer to # links. */ |
| 689 | putp = s->putp; |
| 690 | |
| 691 | /* Forward word */ |
| 692 | stream_putw(s, 0); |
| 693 | |
| 694 | /* Set all link information. */ |
| 695 | cnt = router_lsa_link_set (s, area); |
| 696 | |
| 697 | /* Set # of links here. */ |
| 698 | stream_putw_at (s, putp, cnt); |
| 699 | } |
| 700 | |
| 701 | /* Create new router-LSA. */ |
| 702 | struct ospf_lsa * |
| 703 | ospf_router_lsa_new (struct ospf_area *area) |
| 704 | { |
| 705 | struct stream *s; |
| 706 | struct lsa_header *lsah; |
| 707 | struct ospf_lsa *new; |
| 708 | int length; |
| 709 | |
| 710 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 711 | zlog_info ("LSA[Type1]: Create router-LSA instance"); |
| 712 | |
| 713 | /* Create a stream for LSA. */ |
| 714 | s = stream_new (OSPF_MAX_LSA_SIZE); |
| 715 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 716 | |
| 717 | #ifdef HAVE_NSSA |
| 718 | /* Set LSA common header fields. */ |
| 719 | lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_NSSA_GET (area), |
| 720 | OSPF_ROUTER_LSA, ospf_top->router_id); |
| 721 | #else /* ! HAVE_NSSA */ |
| 722 | /* Set LSA common header fields. */ |
| 723 | lsa_header_set (s, LSA_OPTIONS_GET (area), |
| 724 | OSPF_ROUTER_LSA, ospf_top->router_id); |
| 725 | #endif /* HAVE_NSSA */ |
| 726 | |
| 727 | /* Set router-LSA body fields. */ |
| 728 | ospf_router_lsa_body_set (s, area); |
| 729 | |
| 730 | /* Set length. */ |
| 731 | length = stream_get_endp (s); |
| 732 | lsah->length = htons (length); |
| 733 | |
| 734 | /* Now, create OSPF LSA instance. */ |
| 735 | new = ospf_lsa_new (); |
| 736 | new->area = area; |
| 737 | SET_FLAG (new->flags, OSPF_LSA_SELF); |
| 738 | |
| 739 | /* Copy LSA data to store, discard stream. */ |
| 740 | new->data = ospf_lsa_data_new (length); |
| 741 | memcpy (new->data, lsah, length); |
| 742 | stream_free (s); |
| 743 | |
| 744 | return new; |
| 745 | } |
| 746 | |
| 747 | /* Originate Router-LSA. */ |
| 748 | struct ospf_lsa * |
| 749 | ospf_router_lsa_originate (struct ospf_area *area) |
| 750 | { |
| 751 | struct ospf_lsa *new; |
| 752 | |
| 753 | /* Create new router-LSA instance. */ |
| 754 | new = ospf_router_lsa_new (area); |
| 755 | |
| 756 | /* Sanity check. */ |
| 757 | if (new->data->adv_router.s_addr == 0) |
| 758 | { |
| 759 | if (IS_DEBUG_OSPF_EVENT) |
| 760 | zlog_info ("LSA[Type1]: AdvRouter is 0, discard"); |
| 761 | ospf_lsa_discard (new); |
| 762 | return NULL; |
| 763 | } |
| 764 | |
| 765 | /* Install LSA to LSDB. */ |
| 766 | new = ospf_lsa_install (NULL, new); |
| 767 | |
| 768 | /* Update LSA origination count. */ |
| 769 | ospf_top->lsa_originate_count++; |
| 770 | |
| 771 | /* Flooding new LSA through area. */ |
| 772 | ospf_flood_through_area (area, NULL, new); |
| 773 | |
| 774 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 775 | { |
| 776 | zlog_info ("LSA[Type%d:%s]: Originate router-LSA %p", |
| 777 | new->data->type, inet_ntoa (new->data->id), new); |
| 778 | ospf_lsa_header_dump (new->data); |
| 779 | } |
| 780 | |
| 781 | return new; |
| 782 | } |
| 783 | |
| 784 | /* Refresh router-LSA. */ |
| 785 | struct ospf_lsa * |
| 786 | ospf_router_lsa_refresh (struct ospf_lsa *lsa) |
| 787 | { |
| 788 | struct ospf_area *area = lsa->area; |
| 789 | struct ospf_lsa *new; |
| 790 | |
| 791 | /* Sanity check. */ |
| 792 | assert (lsa->data); |
| 793 | |
| 794 | /* Delete LSA from neighbor retransmit-list. */ |
| 795 | ospf_ls_retransmit_delete_nbr_all (area, lsa); |
| 796 | |
| 797 | /* Create new router-LSA instance. */ |
| 798 | new = ospf_router_lsa_new (area); |
| 799 | new->data->ls_seqnum = lsa_seqnum_increment (lsa); |
| 800 | |
| 801 | ospf_lsa_install (NULL, new); |
| 802 | |
| 803 | /* Flood LSA through area. */ |
| 804 | ospf_flood_through_area (area, NULL, new); |
| 805 | |
| 806 | /* Debug logging. */ |
| 807 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 808 | { |
| 809 | zlog_info ("LSA[Type%d:%s]: router-LSA refresh", |
| 810 | new->data->type, inet_ntoa (new->data->id)); |
| 811 | ospf_lsa_header_dump (new->data); |
| 812 | } |
| 813 | |
| 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | int |
| 818 | ospf_router_lsa_timer (struct thread *t) |
| 819 | { |
| 820 | struct ospf_area *area; |
| 821 | |
| 822 | if (IS_DEBUG_OSPF_EVENT) |
| 823 | zlog_info ("Timer[router-LSA]: (router-LSA Refresh expire)"); |
| 824 | |
| 825 | area = THREAD_ARG (t); |
| 826 | area->t_router_lsa_self = NULL; |
| 827 | |
| 828 | /* Now refresh router-LSA. */ |
| 829 | if (area->router_lsa_self) |
| 830 | ospf_router_lsa_refresh (area->router_lsa_self); |
| 831 | /* Newly originate router-LSA. */ |
| 832 | else |
| 833 | ospf_router_lsa_originate (area); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | void |
| 839 | ospf_router_lsa_timer_add (struct ospf_area *area) |
| 840 | { |
| 841 | /* Keep area's self-originated router-LSA. */ |
| 842 | struct ospf_lsa *lsa = area->router_lsa_self; |
| 843 | |
| 844 | /* Cancel previously scheduled router-LSA timer. */ |
| 845 | if (area->t_router_lsa_self) |
| 846 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 847 | zlog_info ("LSA[Type1]: Cancel previous router-LSA timer"); |
| 848 | |
| 849 | OSPF_TIMER_OFF (area->t_router_lsa_self); |
| 850 | |
| 851 | /* If router-LSA is originated previously, check the interval time. */ |
| 852 | if (lsa) |
| 853 | { |
| 854 | int delay; |
| 855 | if ((delay = ospf_lsa_refresh_delay (lsa)) > 0) |
| 856 | { |
| 857 | OSPF_AREA_TIMER_ON (area->t_router_lsa_self, |
| 858 | ospf_router_lsa_timer, delay); |
| 859 | return; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 864 | zlog_info ("LSA[Type1]: Scheduling router-LSA origination right away"); |
| 865 | |
| 866 | /* Immediately refresh router-LSA. */ |
| 867 | OSPF_AREA_TIMER_ON (area->t_router_lsa_self, ospf_router_lsa_timer, 0); |
| 868 | } |
| 869 | |
| 870 | int |
| 871 | ospf_router_lsa_update_timer (struct thread *t) |
| 872 | { |
| 873 | listnode node; |
| 874 | |
| 875 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 876 | zlog_info ("Timer[router-LSA Update]: (timer expire)"); |
| 877 | |
| 878 | ospf_top->t_router_lsa_update = NULL; |
| 879 | |
| 880 | for (node = listhead (ospf_top->areas); node; nextnode (node)) |
| 881 | { |
| 882 | struct ospf_area *area = getdata (node); |
| 883 | struct ospf_lsa *lsa = area->router_lsa_self; |
| 884 | struct router_lsa *rl; |
| 885 | char *area_str; |
| 886 | |
| 887 | /* Keep Area ID string. */ |
| 888 | area_str = AREA_NAME (area); |
| 889 | |
| 890 | /* If LSA not exist in this Area, originate new. */ |
| 891 | if (lsa == NULL) |
| 892 | { |
| 893 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 894 | zlog_info("LSA[Type1]: Create router-LSA for Area %s", area_str); |
| 895 | |
| 896 | ospf_router_lsa_originate (area); |
| 897 | } |
| 898 | /* If router-ID is changed, Link ID must change. |
| 899 | First flush old LSA, then originate new. */ |
| 900 | else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf_top->router_id)) |
| 901 | { |
| 902 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 903 | zlog_info("LSA[Type%d:%s]: Refresh router-LSA for Area %s", |
| 904 | lsa->data->type, inet_ntoa (lsa->data->id), area_str); |
| 905 | ospf_lsa_flush_area (lsa, area); |
| 906 | ospf_lsa_unlock (area->router_lsa_self); |
| 907 | area->router_lsa_self = NULL; |
| 908 | |
| 909 | /* Refresh router-LSA, (not install) and flood through area. */ |
| 910 | ospf_router_lsa_timer_add (area); |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | rl = (struct router_lsa *) lsa->data; |
| 915 | /* Refresh router-LSA, (not install) and flood through area. */ |
| 916 | if (rl->flags != ospf_top->flags) |
| 917 | ospf_router_lsa_timer_add (area); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | |
| 925 | /* network-LSA related functions. */ |
| 926 | /* Originate Network-LSA. */ |
| 927 | void |
| 928 | ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi) |
| 929 | { |
| 930 | struct in_addr mask; |
| 931 | struct route_node *rn; |
| 932 | struct ospf_neighbor *nbr; |
| 933 | |
| 934 | masklen2ip (oi->address->prefixlen, &mask); |
| 935 | stream_put_ipv4 (s, mask.s_addr); |
| 936 | |
| 937 | /* The network-LSA lists those routers that are fully adjacent to |
| 938 | the Designated Router; each fully adjacent router is identified by |
| 939 | its OSPF Router ID. The Designated Router includes itself in this |
| 940 | list. RFC2328, Section 12.4.2 */ |
| 941 | |
| 942 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 943 | if ((nbr = rn->info) != NULL) |
| 944 | if (nbr->state == NSM_Full || nbr == oi->nbr_self) |
| 945 | stream_put_ipv4 (s, nbr->router_id.s_addr); |
| 946 | } |
| 947 | |
| 948 | struct ospf_lsa * |
| 949 | ospf_network_lsa_new (struct ospf_interface *oi) |
| 950 | { |
| 951 | struct stream *s; |
| 952 | struct ospf_lsa *new; |
| 953 | struct lsa_header *lsah; |
| 954 | int length; |
| 955 | |
| 956 | /* If there are no neighbours on this network (the net is stub), |
| 957 | the router does not originate network-LSA (see RFC 12.4.2) */ |
| 958 | if (oi->full_nbrs == 0) |
| 959 | return NULL; |
| 960 | |
| 961 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 962 | zlog_info ("LSA[Type2]: Create network-LSA instance"); |
| 963 | |
| 964 | /* Create new stream for LSA. */ |
| 965 | s = stream_new (OSPF_MAX_LSA_SIZE); |
| 966 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 967 | |
| 968 | lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)), |
| 969 | OSPF_NETWORK_LSA, DR (oi)); |
| 970 | |
| 971 | /* Set network-LSA body fields. */ |
| 972 | ospf_network_lsa_body_set (s, oi); |
| 973 | |
| 974 | /* Set length. */ |
| 975 | length = stream_get_endp (s); |
| 976 | lsah->length = htons (length); |
| 977 | |
| 978 | /* Create OSPF LSA instance. */ |
| 979 | new = ospf_lsa_new (); |
| 980 | new->area = oi->area; |
| 981 | SET_FLAG (new->flags, OSPF_LSA_SELF); |
| 982 | |
| 983 | /* Copy LSA to store. */ |
| 984 | new->data = ospf_lsa_data_new (length); |
| 985 | memcpy (new->data, lsah, length); |
| 986 | stream_free (s); |
| 987 | |
| 988 | return new; |
| 989 | } |
| 990 | |
| 991 | /* Originate network-LSA. */ |
| 992 | struct ospf_lsa * |
| 993 | ospf_network_lsa_originate (struct ospf_interface *oi) |
| 994 | { |
| 995 | struct ospf_lsa *new; |
| 996 | |
| 997 | /* Create new network-LSA instance. */ |
| 998 | new = ospf_network_lsa_new (oi); |
| 999 | if (new == NULL) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | /* Install LSA to LSDB. */ |
| 1003 | new = ospf_lsa_install (oi, new); |
| 1004 | |
| 1005 | /* Update LSA origination count. */ |
| 1006 | ospf_top->lsa_originate_count++; |
| 1007 | |
| 1008 | /* Flooding new LSA through area. */ |
| 1009 | ospf_flood_through_area (oi->area, NULL, new); |
| 1010 | |
| 1011 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1012 | { |
| 1013 | zlog_info ("LSA[Type%d:%s]: Originate network-LSA %p", |
| 1014 | new->data->type, inet_ntoa (new->data->id), new); |
| 1015 | ospf_lsa_header_dump (new->data); |
| 1016 | } |
| 1017 | |
| 1018 | return new; |
| 1019 | } |
| 1020 | |
| 1021 | int |
| 1022 | ospf_network_lsa_refresh (struct ospf_lsa *lsa, struct ospf_interface *oi) |
| 1023 | { |
| 1024 | struct ospf_area *area = lsa->area; |
| 1025 | struct ospf_lsa *new; |
| 1026 | |
| 1027 | assert (lsa->data); |
| 1028 | |
| 1029 | /* Delete LSA from neighbor retransmit-list. */ |
| 1030 | ospf_ls_retransmit_delete_nbr_all (area, lsa); |
| 1031 | |
| 1032 | /* Create new network-LSA instance. */ |
| 1033 | new = ospf_network_lsa_new (oi); |
| 1034 | if (new == NULL) |
| 1035 | return -1; |
| 1036 | new->data->ls_seqnum = lsa_seqnum_increment (lsa); |
| 1037 | |
| 1038 | ospf_lsa_install (oi, new); |
| 1039 | |
| 1040 | /* Flood LSA through aera. */ |
| 1041 | ospf_flood_through_area (area, NULL, new); |
| 1042 | |
| 1043 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1044 | { |
| 1045 | zlog_info ("LSA[Type%d:%s]: network-LSA refresh", |
| 1046 | new->data->type, inet_ntoa (new->data->id)); |
| 1047 | ospf_lsa_header_dump (new->data); |
| 1048 | } |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | int |
| 1054 | ospf_network_lsa_refresh_timer (struct thread *t) |
| 1055 | { |
| 1056 | struct ospf_interface *oi; |
| 1057 | |
| 1058 | oi = THREAD_ARG (t); |
| 1059 | oi->t_network_lsa_self = NULL; |
| 1060 | |
| 1061 | if (oi->network_lsa_self) |
| 1062 | /* Now refresh network-LSA. */ |
| 1063 | ospf_network_lsa_refresh (oi->network_lsa_self, oi); |
| 1064 | else |
| 1065 | /* Newly create network-LSA. */ |
| 1066 | ospf_network_lsa_originate (oi); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | void |
| 1072 | ospf_network_lsa_timer_add (struct ospf_interface *oi) |
| 1073 | { |
| 1074 | /* Keep interface's self-originated network-LSA. */ |
| 1075 | struct ospf_lsa *lsa = oi->network_lsa_self; |
| 1076 | |
| 1077 | /* Cancel previously schedules network-LSA timer. */ |
| 1078 | if (oi->t_network_lsa_self) |
| 1079 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1080 | zlog_info ("LSA[Type2]: Cancel previous network-LSA timer"); |
| 1081 | OSPF_TIMER_OFF (oi->t_network_lsa_self); |
| 1082 | |
| 1083 | /* If network-LSA is originated previously, check the interval time. */ |
| 1084 | if (lsa) |
| 1085 | { |
| 1086 | int delay; |
| 1087 | if ((delay = ospf_lsa_refresh_delay (lsa)) > 0) |
| 1088 | { |
| 1089 | oi->t_network_lsa_self = |
| 1090 | thread_add_timer (master, ospf_network_lsa_refresh_timer, |
| 1091 | oi, delay); |
| 1092 | return; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1097 | zlog_info ("Scheduling network-LSA origination right away"); |
| 1098 | |
| 1099 | /* Immediately refresh network-LSA. */ |
| 1100 | oi->t_network_lsa_self = |
| 1101 | thread_add_event (master, ospf_network_lsa_refresh_timer, oi, 0); |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | void |
| 1106 | stream_put_ospf_metric (struct stream *s, u_int32_t metric_value) |
| 1107 | { |
| 1108 | u_int32_t metric; |
| 1109 | char *mp; |
| 1110 | |
| 1111 | /* Put 0 metric. TOS metric is not supported. */ |
| 1112 | metric = htonl (metric_value); |
| 1113 | mp = (char *) &metric; |
| 1114 | mp++; |
| 1115 | stream_put (s, mp, 3); |
| 1116 | } |
| 1117 | |
| 1118 | /* summary-LSA related functions. */ |
| 1119 | void |
| 1120 | ospf_summary_lsa_body_set (struct stream *s, struct prefix *p, |
| 1121 | u_int32_t metric) |
| 1122 | { |
| 1123 | struct in_addr mask; |
| 1124 | |
| 1125 | masklen2ip (p->prefixlen, &mask); |
| 1126 | |
| 1127 | /* Put Network Mask. */ |
| 1128 | stream_put_ipv4 (s, mask.s_addr); |
| 1129 | |
| 1130 | /* Set # TOS. */ |
| 1131 | stream_putc (s, (u_char) 0); |
| 1132 | |
| 1133 | /* Set metric. */ |
| 1134 | stream_put_ospf_metric (s, metric); |
| 1135 | } |
| 1136 | |
| 1137 | struct ospf_lsa * |
| 1138 | ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p, |
| 1139 | u_int32_t metric, struct in_addr id) |
| 1140 | { |
| 1141 | struct stream *s; |
| 1142 | struct ospf_lsa *new; |
| 1143 | struct lsa_header *lsah; |
| 1144 | int length; |
| 1145 | |
| 1146 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1147 | zlog_info ("LSA[Type3]: Create summary-LSA instance"); |
| 1148 | |
| 1149 | /* Create new stream for LSA. */ |
| 1150 | s = stream_new (OSPF_MAX_LSA_SIZE); |
| 1151 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 1152 | |
| 1153 | lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA, id); |
| 1154 | |
| 1155 | /* Set summary-LSA body fields. */ |
| 1156 | ospf_summary_lsa_body_set (s, p, metric); |
| 1157 | |
| 1158 | /* Set length. */ |
| 1159 | length = stream_get_endp (s); |
| 1160 | lsah->length = htons (length); |
| 1161 | |
| 1162 | /* Create OSPF LSA instance. */ |
| 1163 | new = ospf_lsa_new (); |
| 1164 | new->area = area; |
| 1165 | SET_FLAG (new->flags, OSPF_LSA_SELF); |
| 1166 | |
| 1167 | /* Copy LSA to store. */ |
| 1168 | new->data = ospf_lsa_data_new (length); |
| 1169 | memcpy (new->data, lsah, length); |
| 1170 | stream_free (s); |
| 1171 | |
| 1172 | return new; |
| 1173 | } |
| 1174 | |
| 1175 | /* Originate Summary-LSA. */ |
| 1176 | struct ospf_lsa * |
| 1177 | ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric, |
| 1178 | struct ospf_area *area) |
| 1179 | { |
| 1180 | struct ospf_lsa *new; |
| 1181 | struct in_addr id; |
| 1182 | |
| 1183 | id = ospf_lsa_unique_id (area->lsdb, OSPF_SUMMARY_LSA, p); |
| 1184 | |
| 1185 | /* Create new summary-LSA instance. */ |
| 1186 | new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id); |
| 1187 | |
| 1188 | /* Instlal LSA to LSDB. */ |
| 1189 | new = ospf_lsa_install (NULL, new); |
| 1190 | |
| 1191 | /* Update LSA origination count. */ |
| 1192 | ospf_top->lsa_originate_count++; |
| 1193 | |
| 1194 | /* Flooding new LSA through area. */ |
| 1195 | ospf_flood_through_area (area, NULL, new); |
| 1196 | |
| 1197 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1198 | { |
| 1199 | zlog_info ("LSA[Type%d:%s]: Originate summary-LSA %p", |
| 1200 | new->data->type, inet_ntoa (new->data->id), new); |
| 1201 | ospf_lsa_header_dump (new->data); |
| 1202 | } |
| 1203 | |
| 1204 | return new; |
| 1205 | } |
| 1206 | |
| 1207 | struct ospf_lsa* |
| 1208 | ospf_summary_lsa_refresh (struct ospf_lsa *lsa) |
| 1209 | { |
| 1210 | struct ospf_lsa *new; |
| 1211 | struct summary_lsa *sl; |
| 1212 | struct prefix p; |
| 1213 | |
| 1214 | /* Sanity check. */ |
| 1215 | assert (lsa->data); |
| 1216 | |
| 1217 | sl = (struct summary_lsa *)lsa->data; |
| 1218 | p.prefixlen = ip_masklen (sl->mask); |
| 1219 | new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric), |
| 1220 | sl->header.id); |
| 1221 | |
| 1222 | new->data->ls_seqnum = lsa_seqnum_increment (lsa); |
| 1223 | |
| 1224 | /* Re-calculate checksum. */ |
| 1225 | ospf_lsa_checksum (new->data); |
| 1226 | |
| 1227 | ospf_lsa_install (NULL, new); |
| 1228 | |
| 1229 | /* Flood LSA through AS. */ |
| 1230 | ospf_flood_through_area (new->area, NULL, new); |
| 1231 | |
| 1232 | /* Debug logging. */ |
| 1233 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1234 | { |
| 1235 | zlog_info ("LSA[Type%d:%s]: summary-LSA refresh", |
| 1236 | new->data->type, inet_ntoa (new->data->id)); |
| 1237 | ospf_lsa_header_dump (new->data); |
| 1238 | } |
| 1239 | |
| 1240 | return new; |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | /* summary-ASBR-LSA related functions. */ |
| 1245 | void |
| 1246 | ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p, |
| 1247 | u_int32_t metric) |
| 1248 | { |
| 1249 | struct in_addr mask; |
| 1250 | |
| 1251 | masklen2ip (p->prefixlen, &mask); |
| 1252 | |
| 1253 | /* Put Network Mask. */ |
| 1254 | stream_put_ipv4 (s, mask.s_addr); |
| 1255 | |
| 1256 | /* Set # TOS. */ |
| 1257 | stream_putc (s, (u_char) 0); |
| 1258 | |
| 1259 | /* Set metric. */ |
| 1260 | stream_put_ospf_metric (s, metric); |
| 1261 | } |
| 1262 | |
| 1263 | struct ospf_lsa * |
| 1264 | ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p, |
| 1265 | u_int32_t metric, struct in_addr id) |
| 1266 | { |
| 1267 | struct stream *s; |
| 1268 | struct ospf_lsa *new; |
| 1269 | struct lsa_header *lsah; |
| 1270 | int length; |
| 1271 | |
| 1272 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1273 | zlog_info ("LSA[Type3]: Create summary-LSA instance"); |
| 1274 | |
| 1275 | /* Create new stream for LSA. */ |
| 1276 | s = stream_new (OSPF_MAX_LSA_SIZE); |
| 1277 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 1278 | |
| 1279 | lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA, id); |
| 1280 | |
| 1281 | /* Set summary-LSA body fields. */ |
| 1282 | ospf_summary_asbr_lsa_body_set (s, p, metric); |
| 1283 | |
| 1284 | /* Set length. */ |
| 1285 | length = stream_get_endp (s); |
| 1286 | lsah->length = htons (length); |
| 1287 | |
| 1288 | /* Create OSPF LSA instance. */ |
| 1289 | new = ospf_lsa_new (); |
| 1290 | new->area = area; |
| 1291 | SET_FLAG (new->flags, OSPF_LSA_SELF); |
| 1292 | |
| 1293 | /* Copy LSA to store. */ |
| 1294 | new->data = ospf_lsa_data_new (length); |
| 1295 | memcpy (new->data, lsah, length); |
| 1296 | stream_free (s); |
| 1297 | |
| 1298 | return new; |
| 1299 | } |
| 1300 | |
| 1301 | /* Originate summary-ASBR-LSA. */ |
| 1302 | struct ospf_lsa * |
| 1303 | ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric, |
| 1304 | struct ospf_area *area) |
| 1305 | { |
| 1306 | struct ospf_lsa *new; |
| 1307 | struct in_addr id; |
| 1308 | |
| 1309 | id = ospf_lsa_unique_id (area->lsdb, OSPF_ASBR_SUMMARY_LSA, p); |
| 1310 | |
| 1311 | /* Create new summary-LSA instance. */ |
| 1312 | new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id); |
| 1313 | |
| 1314 | /* Install LSA to LSDB. */ |
| 1315 | new = ospf_lsa_install (NULL, new); |
| 1316 | |
| 1317 | /* Update LSA origination count. */ |
| 1318 | ospf_top->lsa_originate_count++; |
| 1319 | |
| 1320 | /* Flooding new LSA through area. */ |
| 1321 | ospf_flood_through_area (area, NULL, new); |
| 1322 | |
| 1323 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1324 | { |
| 1325 | zlog_info ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p", |
| 1326 | new->data->type, inet_ntoa (new->data->id), new); |
| 1327 | ospf_lsa_header_dump (new->data); |
| 1328 | } |
| 1329 | |
| 1330 | return new; |
| 1331 | } |
| 1332 | |
| 1333 | struct ospf_lsa* |
| 1334 | ospf_summary_asbr_lsa_refresh (struct ospf_lsa *lsa) |
| 1335 | { |
| 1336 | struct ospf_lsa *new; |
| 1337 | struct summary_lsa *sl; |
| 1338 | struct prefix p; |
| 1339 | |
| 1340 | /* Sanity check. */ |
| 1341 | assert (lsa->data); |
| 1342 | |
| 1343 | sl = (struct summary_lsa *)lsa->data; |
| 1344 | p.prefixlen = ip_masklen (sl->mask); |
| 1345 | new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric), |
| 1346 | sl->header.id); |
| 1347 | |
| 1348 | new->data->ls_seqnum = lsa_seqnum_increment (lsa); |
| 1349 | |
| 1350 | /* Re-calculate checksum. */ |
| 1351 | ospf_lsa_checksum (new->data); |
| 1352 | |
| 1353 | ospf_lsa_install (NULL, new); |
| 1354 | |
| 1355 | /* Flood LSA through area. */ |
| 1356 | ospf_flood_through_area (new->area, NULL, new); |
| 1357 | |
| 1358 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1359 | { |
| 1360 | zlog_info ("LSA[Type%d:%s]: summary-ASBR-LSA refresh", |
| 1361 | new->data->type, inet_ntoa (new->data->id)); |
| 1362 | ospf_lsa_header_dump (new->data); |
| 1363 | } |
| 1364 | |
| 1365 | return new; |
| 1366 | } |
| 1367 | |
| 1368 | /* AS-external-LSA related functions. */ |
| 1369 | |
| 1370 | /* Get nexthop for AS-external-LSAs. Return nexthop if its interface |
| 1371 | is connected, else 0*/ |
| 1372 | struct in_addr |
| 1373 | ospf_external_lsa_nexthop_get (struct in_addr nexthop) |
| 1374 | { |
| 1375 | struct in_addr fwd; |
| 1376 | struct prefix nh; |
| 1377 | /* struct route_node *rn; */ |
| 1378 | listnode n1; |
| 1379 | |
| 1380 | fwd.s_addr = 0; |
| 1381 | |
| 1382 | if (!nexthop.s_addr) |
| 1383 | return fwd; |
| 1384 | |
| 1385 | /* Check whether nexthop is covered by OSPF network. */ |
| 1386 | nh.family = AF_INET; |
| 1387 | nh.u.prefix4 = nexthop; |
| 1388 | nh.prefixlen = IPV4_MAX_BITLEN; |
| 1389 | |
| 1390 | for (n1 = listhead (ospf_top->oiflist); n1; nextnode (n1)) |
| 1391 | { |
| 1392 | struct ospf_interface *oi = getdata (n1); |
| 1393 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 1394 | if (if_is_operative (oi->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1395 | if (oi->address->family == AF_INET) |
| 1396 | if (prefix_match (oi->address, &nh)) |
| 1397 | return nexthop; |
| 1398 | } |
| 1399 | |
| 1400 | return fwd; |
| 1401 | } |
| 1402 | |
| 1403 | #ifdef HAVE_NSSA |
| 1404 | /* NSSA-external-LSA related functions. */ |
| 1405 | |
| 1406 | /* Get 1st IP connection for Forward Addr */ |
| 1407 | |
| 1408 | struct in_addr |
| 1409 | ospf_get_ip_from_ifp (struct ospf_interface *oi) |
| 1410 | { |
| 1411 | struct in_addr fwd; |
| 1412 | |
| 1413 | fwd.s_addr = 0; |
| 1414 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 1415 | if (if_is_operative (oi->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1416 | return oi->address->u.prefix4; |
| 1417 | |
| 1418 | return fwd; |
| 1419 | } |
| 1420 | |
| 1421 | /* Get 1st IP connection for Forward Addr */ |
| 1422 | struct in_addr |
| 1423 | ospf_get_nssa_ip (void) |
| 1424 | { |
| 1425 | struct in_addr fwd; |
| 1426 | listnode n1; |
| 1427 | |
| 1428 | fwd.s_addr = 0; |
| 1429 | |
| 1430 | |
| 1431 | for (n1 = listhead (ospf_top->oiflist); n1; nextnode (n1)) |
| 1432 | { |
| 1433 | struct ospf_interface *oi = getdata (n1); |
| 1434 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 1435 | if (if_is_operative (oi->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1436 | if (oi->area->external_routing == OSPF_AREA_NSSA) |
| 1437 | if (oi->address && oi->address->family == AF_INET) |
| 1438 | return (oi->address->u.prefix4 ); |
| 1439 | } |
| 1440 | |
| 1441 | return fwd; |
| 1442 | } |
| 1443 | #endif /* HAVE_NSSA */ |
| 1444 | |
| 1445 | #define DEFAULT_DEFAULT_METRIC 20 |
| 1446 | #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10 |
| 1447 | #define DEFAULT_DEFAULT_ALWAYS_METRIC 1 |
| 1448 | |
| 1449 | #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2 |
| 1450 | |
| 1451 | int |
| 1452 | metric_type (u_char src) |
| 1453 | { |
| 1454 | return (ospf_top->dmetric[src].type < 0 ? |
| 1455 | DEFAULT_METRIC_TYPE : ospf_top->dmetric[src].type); |
| 1456 | } |
| 1457 | |
| 1458 | int |
| 1459 | metric_value (u_char src) |
| 1460 | { |
| 1461 | if (ospf_top->dmetric[src].value < 0) |
| 1462 | { |
| 1463 | if (src == DEFAULT_ROUTE) |
| 1464 | { |
| 1465 | if (ospf_top->default_originate == DEFAULT_ORIGINATE_ZEBRA) |
| 1466 | return DEFAULT_DEFAULT_ORIGINATE_METRIC; |
| 1467 | else |
| 1468 | return DEFAULT_DEFAULT_ALWAYS_METRIC; |
| 1469 | } |
| 1470 | else if (ospf_top->default_metric < 0) |
| 1471 | return DEFAULT_DEFAULT_METRIC; |
| 1472 | else |
| 1473 | return ospf_top->default_metric; |
| 1474 | } |
| 1475 | |
| 1476 | return ospf_top->dmetric[src].value; |
| 1477 | } |
| 1478 | |
| 1479 | /* Set AS-external-LSA body. */ |
| 1480 | void |
| 1481 | ospf_external_lsa_body_set (struct stream *s, struct external_info *ei) |
| 1482 | { |
| 1483 | struct prefix_ipv4 *p = &ei->p; |
| 1484 | struct in_addr mask, fwd_addr; |
| 1485 | u_int32_t mvalue; |
| 1486 | int mtype; |
| 1487 | int type; |
| 1488 | |
| 1489 | /* Put Network Mask. */ |
| 1490 | masklen2ip (p->prefixlen, &mask); |
| 1491 | stream_put_ipv4 (s, mask.s_addr); |
| 1492 | |
| 1493 | /* If prefix is default, specify DEFAULT_ROUTE. */ |
| 1494 | type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type; |
| 1495 | |
| 1496 | mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ? |
| 1497 | ROUTEMAP_METRIC_TYPE (ei) : metric_type (type); |
| 1498 | |
| 1499 | mvalue = (ROUTEMAP_METRIC (ei) != -1) ? |
| 1500 | ROUTEMAP_METRIC (ei) : metric_value (type); |
| 1501 | |
| 1502 | /* Put type of external metric. */ |
| 1503 | stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0)); |
| 1504 | |
| 1505 | /* Put 0 metric. TOS metric is not supported. */ |
| 1506 | stream_put_ospf_metric (s, mvalue); |
| 1507 | |
| 1508 | /* Get forwarding address to nexthop if on the Connection List, else 0. */ |
| 1509 | fwd_addr = ospf_external_lsa_nexthop_get (ei->nexthop); |
| 1510 | |
| 1511 | /* Put forwarding address. */ |
| 1512 | stream_put_ipv4 (s, fwd_addr.s_addr); |
| 1513 | |
| 1514 | /* Put route tag -- This value should be introduced from configuration. */ |
| 1515 | stream_putl (s, 0); |
| 1516 | } |
| 1517 | |
| 1518 | /* Create new external-LSA. */ |
| 1519 | struct ospf_lsa * |
| 1520 | ospf_external_lsa_new (struct external_info *ei, struct in_addr *old_id) |
| 1521 | { |
| 1522 | struct stream *s; |
| 1523 | struct lsa_header *lsah; |
| 1524 | struct ospf_lsa *new; |
| 1525 | struct in_addr id; |
| 1526 | int length; |
| 1527 | |
| 1528 | if (ei == NULL) |
| 1529 | { |
| 1530 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1531 | zlog_warn ("LSA[Type5]: External info is NULL, could not originated"); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | |
| 1535 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1536 | zlog_info ("LSA[Type5]: Originate AS-external-LSA instance"); |
| 1537 | |
| 1538 | /* If old Link State ID is specified, refresh LSA with same ID. */ |
| 1539 | if (old_id) |
| 1540 | id = *old_id; |
| 1541 | /* Get Link State with unique ID. */ |
| 1542 | else |
| 1543 | { |
| 1544 | id = ospf_lsa_unique_id (ospf_top->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p); |
| 1545 | if (id.s_addr == 0xffffffff) |
| 1546 | { |
| 1547 | /* Maybe Link State ID not available. */ |
| 1548 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1549 | zlog_info ("LSA[Type5]: Link ID not available, can't originate"); |
| 1550 | return NULL; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | /* Create new stream for LSA. */ |
| 1555 | s = stream_new (OSPF_MAX_LSA_SIZE); |
| 1556 | lsah = (struct lsa_header *) STREAM_DATA (s); |
| 1557 | |
| 1558 | /* Set LSA common header fields. */ |
| 1559 | lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id); |
| 1560 | |
| 1561 | /* Set AS-external-LSA body fields. */ |
| 1562 | ospf_external_lsa_body_set (s, ei); |
| 1563 | |
| 1564 | /* Set length. */ |
| 1565 | length = stream_get_endp (s); |
| 1566 | lsah->length = htons (length); |
| 1567 | |
| 1568 | /* Now, create OSPF LSA instance. */ |
| 1569 | new = ospf_lsa_new (); |
| 1570 | new->area = NULL; |
| 1571 | SET_FLAG (new->flags, OSPF_LSA_SELF|OSPF_LSA_APPROVED); |
| 1572 | |
| 1573 | /* Copy LSA data to store, discard stream. */ |
| 1574 | new->data = ospf_lsa_data_new (length); |
| 1575 | memcpy (new->data, lsah, length); |
| 1576 | stream_free (s); |
| 1577 | |
| 1578 | return new; |
| 1579 | } |
| 1580 | |
| 1581 | #ifdef HAVE_NSSA |
| 1582 | /* Set AS-external-LSA body test. */ |
| 1583 | void |
| 1584 | ospf_external_lsa_body_test (struct stream *s) |
| 1585 | { |
| 1586 | struct in_addr mask, fwd_addr; |
| 1587 | u_int32_t mvalue = 0; |
| 1588 | /* int mtype; |
| 1589 | int type; */ |
| 1590 | |
| 1591 | mask.s_addr = 0; |
| 1592 | fwd_addr.s_addr = 0; |
| 1593 | |
| 1594 | /* Put Network Mask. */ |
| 1595 | /* masklen2ip (p->prefixlen, &mask); */ |
| 1596 | stream_put_ipv4 (s, mask.s_addr); |
| 1597 | |
| 1598 | /* If prefix is default, specify DEFAULT_ROUTE. */ |
| 1599 | /* type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type; |
| 1600 | |
| 1601 | mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ? |
| 1602 | ROUTEMAP_METRIC_TYPE (ei) : metric_type (type); |
| 1603 | |
| 1604 | mvalue = (ROUTEMAP_METRIC (ei) != -1) ? |
| 1605 | ROUTEMAP_METRIC (ei) : metric_value (type); */ |
| 1606 | |
| 1607 | /* Put type of external metric. */ |
| 1608 | stream_putc (s, 0); |
| 1609 | |
| 1610 | /* Put 0 metric. TOS metric is not supported. */ |
| 1611 | stream_put_ospf_metric (s, mvalue); |
| 1612 | |
| 1613 | |
| 1614 | /* fwd_addr = ospf_top->router_id; */ |
| 1615 | |
| 1616 | /* OLD == ospf_external_lsa_nexthop_get (ei->nexthop); */ |
| 1617 | |
| 1618 | /* Put forwarding address. */ |
| 1619 | /* stream_put_ipv4 (s, fwd_addr.s_addr); */ |
| 1620 | stream_put_ipv4 (s, ospf_top->router_id.s_addr); |
| 1621 | |
| 1622 | /* Put route tag -- This value should be introduced from configuration. */ |
| 1623 | stream_putl (s, 0); |
| 1624 | } |
| 1625 | |
| 1626 | /* As Type-7 */ |
| 1627 | void |
| 1628 | ospf_install_flood_nssa (struct ospf_lsa *lsa, struct external_info *ei) |
| 1629 | { |
| 1630 | struct ospf_lsa *new2; |
| 1631 | struct as_external_lsa *extlsa; |
| 1632 | |
| 1633 | /* NSSA Originate or Refresh (If anyNSSA) |
| 1634 | |
| 1635 | LSA is self-originated. And just installed as Type-5. |
| 1636 | Additionally, install as Type-7 LSDB for every attached NSSA. |
| 1637 | |
| 1638 | P-Bit controls which ABR performs translation to outside world; If |
| 1639 | we are an ABR....do not set the P-bit, because we send the Type-5, |
| 1640 | not as the ABR Translator, but as the ASBR owner within the AS! |
| 1641 | |
| 1642 | If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The |
| 1643 | elected ABR Translator will see the P-bit, Translate, and re-flood. |
| 1644 | |
| 1645 | Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to |
| 1646 | Type-5's to non-NSSA Areas. (it will also attempt a re-install) */ |
| 1647 | |
| 1648 | /* make lsa duplicate, lock=1 */ |
| 1649 | new2 = ospf_lsa_dup(lsa); |
| 1650 | |
| 1651 | /* make type-7 */ |
| 1652 | new2->data->type = OSPF_AS_NSSA_LSA; |
| 1653 | |
| 1654 | /* set P-bit if not ABR */ |
| 1655 | if (! OSPF_IS_ABR) |
| 1656 | { |
| 1657 | SET_FLAG(new2->data->options, OSPF_OPTION_NP); |
| 1658 | |
| 1659 | /* set non-zero FWD ADDR |
| 1660 | |
| 1661 | draft-ietf-ospf-nssa-update-09.txt |
| 1662 | |
| 1663 | if the network between the NSSA AS boundary router and the |
| 1664 | adjacent AS is advertised into OSPF as an internal OSPF route, |
| 1665 | the forwarding address should be the next op address as is cu |
| 1666 | currently done with type-5 LSAs. If the intervening network is |
| 1667 | not adversited into OSPF as an internal OSPF route and the |
| 1668 | type-7 LSA's P-bit is set a forwarding address should be |
| 1669 | selected from one of the router's active OSPF inteface addresses |
| 1670 | which belong to the NSSA. If no such addresses exist, then |
| 1671 | no type-7 LSA's with the P-bit set should originate from this |
| 1672 | router. */ |
| 1673 | |
| 1674 | extlsa = (struct as_external_lsa *)(lsa->data); |
| 1675 | |
| 1676 | if (extlsa->e[0].fwd_addr.s_addr == 0) |
| 1677 | extlsa->e[0].fwd_addr = ospf_get_nssa_ip(); /* this NSSA area in ifp */ |
| 1678 | |
| 1679 | if (IS_DEBUG_OSPF_NSSA) |
| 1680 | if (extlsa->e[0].fwd_addr.s_addr == 0) |
| 1681 | { |
| 1682 | zlog_info ("LSA[Type-7]: Could not build FWD-ADDR"); |
| 1683 | ospf_lsa_discard(new2); |
| 1684 | return; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* Re-calculate checksum. */ |
| 1689 | ospf_lsa_checksum (new2->data); |
| 1690 | |
| 1691 | /* install also as Type-7 */ |
| 1692 | ospf_lsa_install (NULL, new2); /* Remove Old, Lock New = 2 */ |
| 1693 | |
| 1694 | /* will send each copy, lock=2+n */ |
| 1695 | ospf_flood_through_as (NULL, new2); /* all attached NSSA's, no AS/STUBs */ |
| 1696 | |
| 1697 | /* last send, lock=2 LSA is now permanent in Type-7 LSDB */ |
| 1698 | /* It has the same ID as it's Type-5 Counter-Part */ |
| 1699 | |
| 1700 | } |
| 1701 | #endif /* HAVE_NSSA */ |
| 1702 | |
| 1703 | int |
| 1704 | is_prefix_default (struct prefix_ipv4 *p) |
| 1705 | { |
| 1706 | struct prefix_ipv4 q; |
| 1707 | |
| 1708 | q.family = AF_INET; |
| 1709 | q.prefix.s_addr = 0; |
| 1710 | q.prefixlen = 0; |
| 1711 | |
| 1712 | return prefix_same ((struct prefix *) p, (struct prefix *) &q); |
| 1713 | } |
| 1714 | |
| 1715 | /* Originate an AS-external-LSA, install and flood. */ |
| 1716 | struct ospf_lsa * |
| 1717 | ospf_external_lsa_originate (struct external_info *ei) |
| 1718 | { |
| 1719 | struct ospf_lsa *new; |
| 1720 | |
| 1721 | /* Added for NSSA project.... |
| 1722 | |
| 1723 | External LSAs are originated in ASBRs as usual, but for NSSA systems. |
| 1724 | there is the global Type-5 LSDB and a Type-7 LSDB installed for |
| 1725 | every area. The Type-7's are flooded to every IR and every ABR; We |
| 1726 | install the Type-5 LSDB so that the normal "refresh" code operates |
| 1727 | as usual, and flag them as not used during ASE calculations. The |
| 1728 | Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding |
| 1729 | Address of non-zero. |
| 1730 | |
| 1731 | If an ABR is the elected NSSA translator, following SPF and during |
| 1732 | the ABR task it will translate all the scanned Type-7's, with P-bit |
| 1733 | ON and not-self generated, and translate to Type-5's throughout the |
| 1734 | non-NSSA/STUB AS. |
| 1735 | |
| 1736 | A difference in operation depends whether this ASBR is an ABR |
| 1737 | or not. If not an ABR, the P-bit is ON, to indicate that any |
| 1738 | elected NSSA-ABR can perform its translation. |
| 1739 | |
| 1740 | If an ABR, the P-bit is OFF; No ABR will perform translation and |
| 1741 | this ASBR will flood the Type-5 LSA as usual. |
| 1742 | |
| 1743 | For the case where this ASBR is not an ABR, the ASE calculations |
| 1744 | are based on the Type-5 LSDB; The Type-7 LSDB exists just to |
| 1745 | demonstrate to the user that there are LSA's that belong to any |
| 1746 | attached NSSA. |
| 1747 | |
| 1748 | Finally, it just so happens that when the ABR is translating every |
| 1749 | Type-7 into Type-5, it installs it into the Type-5 LSDB as an |
| 1750 | approved Type-5 (translated from Type-7); at the end of translation |
| 1751 | if any Translated Type-5's remain unapproved, then they must be |
| 1752 | flushed from the AS. |
| 1753 | |
| 1754 | */ |
| 1755 | |
| 1756 | /* Check the AS-external-LSA should be originated. */ |
| 1757 | if (!ospf_redistribute_check (ei, NULL)) |
| 1758 | return NULL; |
| 1759 | |
| 1760 | /* Create new AS-external-LSA instance. */ |
| 1761 | if ((new = ospf_external_lsa_new (ei, NULL)) == NULL) |
| 1762 | { |
| 1763 | if (IS_DEBUG_OSPF_EVENT) |
| 1764 | zlog_info ("LSA[Type5:%s]: Could not originate AS-external-LSA", |
| 1765 | inet_ntoa (ei->p.prefix)); |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | |
| 1769 | /* Install newly created LSA into Type-5 LSDB, lock = 1. */ |
| 1770 | ospf_lsa_install (NULL, new); |
| 1771 | |
| 1772 | /* Update LSA origination count. */ |
| 1773 | ospf_top->lsa_originate_count++; |
| 1774 | |
| 1775 | /* Flooding new LSA. only to AS (non-NSSA/STUB) */ |
| 1776 | ospf_flood_through_as (NULL, new); |
| 1777 | |
| 1778 | #ifdef HAVE_NSSA |
| 1779 | /* If there is any attached NSSA, do special handling */ |
| 1780 | if (ospf_top->anyNSSA) |
| 1781 | ospf_install_flood_nssa (new, ei); /* Install/Flood Type-7 to all NSSAs */ |
| 1782 | #endif /* HAVE_NSSA */ |
| 1783 | |
| 1784 | /* Debug logging. */ |
| 1785 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 1786 | { |
| 1787 | zlog_info ("LSA[Type%d:%s]: Originate AS-external-LSA %p", |
| 1788 | new->data->type, inet_ntoa (new->data->id), new); |
| 1789 | ospf_lsa_header_dump (new->data); |
| 1790 | } |
| 1791 | |
| 1792 | return new; |
| 1793 | } |
| 1794 | |
| 1795 | /* Originate AS-external-LSA from external info with initial flag. */ |
| 1796 | int |
| 1797 | ospf_external_lsa_originate_timer (struct thread *t) |
| 1798 | { |
| 1799 | struct route_node *rn; |
| 1800 | struct external_info *ei; |
| 1801 | struct route_table *rt; |
| 1802 | int type; |
| 1803 | |
| 1804 | ospf_top->t_external_lsa = NULL; |
| 1805 | type = THREAD_VAL (t); |
| 1806 | |
| 1807 | /* Originate As-external-LSA from all type of distribute source. */ |
| 1808 | if ((rt = EXTERNAL_INFO (type))) |
| 1809 | for (rn = route_top (rt); rn; rn = route_next (rn)) |
| 1810 | if ((ei = rn->info) != NULL) |
| 1811 | if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p)) |
| 1812 | if (!ospf_external_lsa_originate (ei)) |
| 1813 | zlog_warn ("LSA: AS-external-LSA was not originated."); |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
| 1818 | struct external_info * |
| 1819 | ospf_default_external_info () |
| 1820 | { |
| 1821 | int type; |
| 1822 | struct route_node *rn; |
| 1823 | struct prefix_ipv4 p; |
| 1824 | |
| 1825 | p.family = AF_INET; |
| 1826 | p.prefix.s_addr = 0; |
| 1827 | p.prefixlen = 0; |
| 1828 | |
| 1829 | /* First, lookup redistributed default route. */ |
| 1830 | for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) |
| 1831 | if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF) |
| 1832 | { |
| 1833 | rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p); |
| 1834 | if (rn != NULL) |
| 1835 | { |
| 1836 | route_unlock_node (rn); |
| 1837 | assert (rn->info); |
| 1838 | if (ospf_redistribute_check (rn->info, NULL)) |
| 1839 | return rn->info; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | |
| 1846 | int |
| 1847 | ospf_default_originate_timer (struct thread *t) |
| 1848 | { |
| 1849 | int *origin; |
| 1850 | struct prefix_ipv4 p; |
| 1851 | struct in_addr nexthop; |
| 1852 | struct external_info *ei; |
| 1853 | |
| 1854 | /* Get originate flags. */ |
| 1855 | origin = THREAD_ARG (t); |
| 1856 | |
| 1857 | p.family = AF_INET; |
| 1858 | p.prefix.s_addr = 0; |
| 1859 | p.prefixlen = 0; |
| 1860 | |
| 1861 | if (*origin == DEFAULT_ORIGINATE_ALWAYS) |
| 1862 | { |
| 1863 | /* If there is no default route via redistribute, |
| 1864 | then originate AS-external-LSA with nexthop 0 (self). */ |
| 1865 | nexthop.s_addr = 0; |
| 1866 | ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop); |
| 1867 | } |
| 1868 | |
| 1869 | if ((ei = ospf_default_external_info ())) |
| 1870 | ospf_external_lsa_originate (ei); |
| 1871 | |
| 1872 | return 0; |
| 1873 | } |
| 1874 | |
| 1875 | /* Flush an AS-external-LSA from LSDB and routing domain. */ |
| 1876 | void |
| 1877 | ospf_external_lsa_flush (u_char type, struct prefix_ipv4 *p, |
| 1878 | unsigned int ifindex, struct in_addr nexthop) |
| 1879 | { |
| 1880 | struct ospf_lsa *lsa; |
| 1881 | |
| 1882 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 1883 | zlog_info ("LSA: Flushing AS-external-LSA %s/%d", |
| 1884 | inet_ntoa (p->prefix), p->prefixlen); |
| 1885 | |
| 1886 | /* First lookup LSA from LSDB. */ |
| 1887 | if (!(lsa = ospf_external_info_find_lsa (p))) |
| 1888 | { |
| 1889 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 1890 | zlog_warn ("LSA: There is no such AS-external-LSA %s/%d in LSDB", |
| 1891 | inet_ntoa (p->prefix), p->prefixlen); |
| 1892 | return; |
| 1893 | } |
| 1894 | |
| 1895 | /* Sweep LSA from Link State Retransmit List. */ |
| 1896 | ospf_ls_retransmit_delete_nbr_all (NULL, lsa); |
| 1897 | |
| 1898 | /* There must be no self-originated LSA in rtrs_external. */ |
| 1899 | #if 0 |
| 1900 | /* Remove External route from Zebra. */ |
| 1901 | ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop); |
| 1902 | #endif |
| 1903 | |
| 1904 | if (!IS_LSA_MAXAGE (lsa)) |
| 1905 | { |
| 1906 | /* Unregister LSA from Refresh queue. */ |
| 1907 | ospf_refresher_unregister_lsa (ospf_top, lsa); |
| 1908 | |
| 1909 | /* Flush AS-external-LSA through AS. */ |
| 1910 | ospf_flush_through_as (lsa); |
| 1911 | } |
| 1912 | |
| 1913 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 1914 | zlog_info ("ospf_external_lsa_flush(): stop"); |
| 1915 | } |
| 1916 | |
| 1917 | void |
| 1918 | ospf_external_lsa_refresh_default () |
| 1919 | { |
| 1920 | struct prefix_ipv4 p; |
| 1921 | struct external_info *ei; |
| 1922 | struct ospf_lsa *lsa; |
| 1923 | |
| 1924 | p.family = AF_INET; |
| 1925 | p.prefixlen = 0; |
| 1926 | p.prefix.s_addr = 0; |
| 1927 | |
| 1928 | ei = ospf_default_external_info (); |
| 1929 | lsa = ospf_external_info_find_lsa (&p); |
| 1930 | |
| 1931 | if (ei) |
| 1932 | { |
| 1933 | if (lsa) |
| 1934 | { |
| 1935 | if (IS_DEBUG_OSPF_EVENT) |
| 1936 | zlog_info ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa); |
| 1937 | ospf_external_lsa_refresh (lsa, ei, LSA_REFRESH_FORCE); |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | if (IS_DEBUG_OSPF_EVENT) |
| 1942 | zlog_info ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA"); |
| 1943 | ospf_external_lsa_originate (ei); |
| 1944 | } |
| 1945 | } |
| 1946 | else |
| 1947 | { |
| 1948 | if (lsa) |
| 1949 | { |
| 1950 | if (IS_DEBUG_OSPF_EVENT) |
| 1951 | zlog_info ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA"); |
| 1952 | ospf_lsa_flush_as (lsa); |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | void |
| 1958 | ospf_external_lsa_refresh_type (u_char type, int force) |
| 1959 | { |
| 1960 | struct route_node *rn; |
| 1961 | struct external_info *ei; |
| 1962 | |
| 1963 | if (type != DEFAULT_ROUTE) |
| 1964 | if (EXTERNAL_INFO(type)) |
| 1965 | /* Refresh each redistributed AS-external-LSAs. */ |
| 1966 | for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn)) |
| 1967 | if ((ei = rn->info)) |
| 1968 | if (!is_prefix_default (&ei->p)) |
| 1969 | { |
| 1970 | struct ospf_lsa *lsa; |
| 1971 | |
| 1972 | if ((lsa = ospf_external_info_find_lsa (&ei->p))) |
| 1973 | ospf_external_lsa_refresh (lsa, ei, force); |
| 1974 | else |
| 1975 | ospf_external_lsa_originate (ei); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | /* Refresh AS-external-LSA. */ |
| 1980 | void |
| 1981 | ospf_external_lsa_refresh (struct ospf_lsa *lsa, |
| 1982 | struct external_info *ei, int force) |
| 1983 | { |
| 1984 | struct ospf_lsa *new; |
| 1985 | int changed; |
| 1986 | |
| 1987 | /* Check the AS-external-LSA should be originated. */ |
| 1988 | if (!ospf_redistribute_check (ei, &changed)) |
| 1989 | { |
| 1990 | ospf_external_lsa_flush (ei->type, &ei->p, ei->ifindex, ei->nexthop); |
| 1991 | return; |
| 1992 | } |
| 1993 | |
| 1994 | if (!changed && !force) |
| 1995 | return; |
| 1996 | |
| 1997 | /* Delete LSA from neighbor retransmit-list. */ |
| 1998 | ospf_ls_retransmit_delete_nbr_all (NULL, lsa); |
| 1999 | |
| 2000 | /* Unregister AS-external-LSA from refresh-list. */ |
| 2001 | ospf_refresher_unregister_lsa (ospf_top, lsa); |
| 2002 | |
| 2003 | new = ospf_external_lsa_new (ei, &lsa->data->id); |
| 2004 | |
| 2005 | if (new == NULL) |
| 2006 | { |
| 2007 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 2008 | zlog_warn ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type, |
| 2009 | inet_ntoa (lsa->data->id)); |
| 2010 | return; |
| 2011 | } |
| 2012 | |
| 2013 | new->data->ls_seqnum = lsa_seqnum_increment (lsa); |
| 2014 | |
| 2015 | /* Record timestamp. */ |
| 2016 | gettimeofday (&new->tv_orig, NULL); |
| 2017 | |
| 2018 | /* Re-calculate checksum. */ |
| 2019 | ospf_lsa_checksum (new->data); |
| 2020 | |
| 2021 | ospf_lsa_install (NULL, new); /* As type-5. */ |
| 2022 | |
| 2023 | /* Flood LSA through AS. */ |
| 2024 | ospf_flood_through_as (NULL, new); |
| 2025 | |
| 2026 | #ifdef HAVE_NSSA |
| 2027 | /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */ |
| 2028 | if (ospf_top->anyNSSA) |
| 2029 | ospf_install_flood_nssa (new, ei); /* Install/Flood per new rules */ |
| 2030 | #endif /* HAVE_NSSA */ |
| 2031 | |
| 2032 | /* Register slef-originated LSA to refresh queue. */ |
| 2033 | ospf_refresher_register_lsa (ospf_top, new); |
| 2034 | |
| 2035 | /* Debug logging. */ |
| 2036 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 2037 | { |
| 2038 | zlog_info ("LSA[Type%d:%s]: AS-external-LSA refresh", |
| 2039 | new->data->type, inet_ntoa (new->data->id)); |
| 2040 | ospf_lsa_header_dump (new->data); |
| 2041 | } |
| 2042 | |
| 2043 | return; |
| 2044 | } |
| 2045 | |
| 2046 | |
| 2047 | /* LSA installation functions. */ |
| 2048 | |
| 2049 | /* Install router-LSA to an area. */ |
| 2050 | struct ospf_lsa * |
| 2051 | ospf_router_lsa_install (struct ospf_lsa *new, int rt_recalc) |
| 2052 | { |
| 2053 | struct ospf_area *area = new->area; |
| 2054 | |
| 2055 | /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs |
| 2056 | The entire routing table must be recalculated, starting with |
| 2057 | the shortest path calculations for each area (not just the |
| 2058 | area whose link-state database has changed). |
| 2059 | */ |
| 2060 | if (rt_recalc) |
| 2061 | ospf_spf_calculate_schedule(); |
| 2062 | |
| 2063 | if (IS_LSA_SELF (new)) |
| 2064 | { |
| 2065 | /* Set router-LSA refresh timer. */ |
| 2066 | OSPF_TIMER_OFF (area->t_router_lsa_self); |
| 2067 | OSPF_AREA_TIMER_ON (area->t_router_lsa_self, |
| 2068 | ospf_router_lsa_timer, OSPF_LS_REFRESH_TIME); |
| 2069 | |
| 2070 | /* Set self-originated router-LSA. */ |
| 2071 | ospf_lsa_unlock (area->router_lsa_self); |
| 2072 | area->router_lsa_self = ospf_lsa_lock (new); |
| 2073 | |
| 2074 | if (IS_DEBUG_OSPF (lsa, LSA_INSTALL)) |
| 2075 | zlog_info("LSA[Type%d]: ID %s is self-originated", |
| 2076 | new->data->type, inet_ntoa (new->data->id)); |
| 2077 | } |
| 2078 | |
| 2079 | return new; |
| 2080 | } |
| 2081 | |
| 2082 | #define OSPF_INTERFACE_TIMER_ON(T,F,V) \ |
| 2083 | if (!(T)) \ |
| 2084 | (T) = thread_add_timer (master, (F), oi, (V)) |
| 2085 | |
| 2086 | /* Install network-LSA to an area. */ |
| 2087 | struct ospf_lsa * |
| 2088 | ospf_network_lsa_install (struct ospf_interface *oi, |
| 2089 | struct ospf_lsa *new, |
| 2090 | int rt_recalc) |
| 2091 | { |
| 2092 | |
| 2093 | /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs |
| 2094 | The entire routing table must be recalculated, starting with |
| 2095 | the shortest path calculations for each area (not just the |
| 2096 | area whose link-state database has changed). |
| 2097 | */ |
| 2098 | if (rt_recalc) |
| 2099 | ospf_spf_calculate_schedule(); |
| 2100 | |
| 2101 | /* We supposed that when LSA is originated by us, we pass the int |
| 2102 | for which it was originated. If LSA was received by flooding, |
| 2103 | the RECEIVED flag is set, so we do not link the LSA to the int. */ |
| 2104 | if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED)) |
| 2105 | { |
| 2106 | /* Set LSRefresh timer. */ |
| 2107 | OSPF_TIMER_OFF (oi->t_network_lsa_self); |
| 2108 | |
| 2109 | OSPF_INTERFACE_TIMER_ON (oi->t_network_lsa_self, |
| 2110 | ospf_network_lsa_refresh_timer, |
| 2111 | OSPF_LS_REFRESH_TIME); |
| 2112 | |
| 2113 | ospf_lsa_unlock (oi->network_lsa_self); |
| 2114 | oi->network_lsa_self = ospf_lsa_lock (new); |
| 2115 | } |
| 2116 | |
| 2117 | return new; |
| 2118 | } |
| 2119 | |
| 2120 | /* Install summary-LSA to an area. */ |
| 2121 | struct ospf_lsa * |
| 2122 | ospf_summary_lsa_install (struct ospf_lsa *new, int rt_recalc) |
| 2123 | { |
| 2124 | |
| 2125 | if (rt_recalc && !IS_LSA_SELF (new)) |
| 2126 | { |
| 2127 | /* RFC 2328 Section 13.2 Summary-LSAs |
| 2128 | The best route to the destination described by the summary- |
| 2129 | LSA must be recalculated (see Section 16.5). If this |
| 2130 | destination is an AS boundary router, it may also be |
| 2131 | necessary to re-examine all the AS-external-LSAs. |
| 2132 | */ |
| 2133 | |
| 2134 | #if 0 |
| 2135 | /* This doesn't exist yet... */ |
| 2136 | ospf_summary_incremental_update(new); */ |
| 2137 | #else /* #if 0 */ |
| 2138 | ospf_spf_calculate_schedule(); |
| 2139 | #endif /* #if 0 */ |
| 2140 | |
| 2141 | if (IS_DEBUG_OSPF (lsa, LSA_INSTALL)) |
| 2142 | zlog_info ("ospf_summary_lsa_install(): SPF scheduled"); |
| 2143 | } |
| 2144 | |
| 2145 | if (IS_LSA_SELF (new)) |
| 2146 | ospf_refresher_register_lsa (ospf_top, new); |
| 2147 | |
| 2148 | return new; |
| 2149 | } |
| 2150 | |
| 2151 | /* Install ASBR-summary-LSA to an area. */ |
| 2152 | struct ospf_lsa * |
| 2153 | ospf_summary_asbr_lsa_install (struct ospf_lsa *new, int rt_recalc) |
| 2154 | { |
| 2155 | if (rt_recalc && !IS_LSA_SELF (new)) |
| 2156 | { |
| 2157 | /* RFC 2328 Section 13.2 Summary-LSAs |
| 2158 | The best route to the destination described by the summary- |
| 2159 | LSA must be recalculated (see Section 16.5). If this |
| 2160 | destination is an AS boundary router, it may also be |
| 2161 | necessary to re-examine all the AS-external-LSAs. |
| 2162 | */ |
| 2163 | #if 0 |
| 2164 | /* These don't exist yet... */ |
| 2165 | ospf_summary_incremental_update(new); |
| 2166 | /* Isn't this done by the above call? |
| 2167 | - RFC 2328 Section 16.5 implies it should be */ |
| 2168 | /* ospf_ase_calculate_schedule(); */ |
| 2169 | #else /* #if 0 */ |
| 2170 | ospf_spf_calculate_schedule(); |
| 2171 | #endif /* #if 0 */ |
| 2172 | } |
| 2173 | |
| 2174 | /* register LSA to refresh-list. */ |
| 2175 | if (IS_LSA_SELF (new)) |
| 2176 | ospf_refresher_register_lsa (ospf_top, new); |
| 2177 | |
| 2178 | return new; |
| 2179 | } |
| 2180 | |
| 2181 | /* Install AS-external-LSA. */ |
| 2182 | struct ospf_lsa * |
| 2183 | ospf_external_lsa_install (struct ospf_lsa *new, int rt_recalc) |
| 2184 | { |
| 2185 | ospf_ase_register_external_lsa (new, ospf_top); |
| 2186 | /* If LSA is not self-originated, calculate an external route. */ |
| 2187 | if (rt_recalc) |
| 2188 | { |
| 2189 | /* RFC 2328 Section 13.2 AS-external-LSAs |
| 2190 | The best route to the destination described by the AS- |
| 2191 | external-LSA must be recalculated (see Section 16.6). |
| 2192 | */ |
| 2193 | |
| 2194 | if (!IS_LSA_SELF (new)) |
| 2195 | ospf_ase_incremental_update (new, ospf_top); |
| 2196 | } |
| 2197 | |
| 2198 | /* Register self-originated LSA to refresh queue. */ |
| 2199 | if (IS_LSA_SELF (new)) |
| 2200 | ospf_refresher_register_lsa (ospf_top, new); |
| 2201 | |
| 2202 | return new; |
| 2203 | } |
| 2204 | |
| 2205 | void |
| 2206 | ospf_discard_from_db (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 2207 | { |
| 2208 | struct ospf_lsa *old; |
| 2209 | |
| 2210 | old = ospf_lsdb_lookup (lsdb, lsa); |
| 2211 | |
| 2212 | if (!old) |
| 2213 | return; |
| 2214 | |
| 2215 | if (old->refresh_list >= 0) |
| 2216 | ospf_refresher_unregister_lsa (ospf_top, old); |
| 2217 | |
| 2218 | ospf_ls_retransmit_delete_nbr_all (old->area, old); |
| 2219 | |
| 2220 | switch (old->data->type) |
| 2221 | { |
| 2222 | case OSPF_AS_EXTERNAL_LSA: |
| 2223 | #ifdef HAVE_OPAQUE_LSA |
| 2224 | case OSPF_OPAQUE_AS_LSA: |
| 2225 | #endif /* HAVE_OPAQUE_LSA */ |
| 2226 | ospf_ase_unregister_external_lsa (old, ospf_top); |
| 2227 | break; |
| 2228 | default: |
| 2229 | break; |
| 2230 | } |
| 2231 | |
| 2232 | ospf_lsa_maxage_delete (old); |
| 2233 | ospf_lsa_discard (old); |
| 2234 | } |
| 2235 | |
| 2236 | /* callback for foreach_lsa */ |
| 2237 | int |
| 2238 | ospf_lsa_discard_callback (struct ospf_lsa *lsa, void *p, int i) |
| 2239 | { |
| 2240 | #ifdef HAVE_NSSA |
| 2241 | /* Removed: Stay away from any Local Translated Type-7 LSAs */ |
| 2242 | /* if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)) |
| 2243 | return 0; */ |
| 2244 | #endif /* HAVE_NSSA */ |
| 2245 | ospf_discard_from_db ((struct ospf_lsdb *)p, lsa); |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
| 2249 | struct ospf_lsa * |
| 2250 | ospf_lsa_install (struct ospf_interface *oi, struct ospf_lsa *lsa) |
| 2251 | { |
| 2252 | struct ospf_lsa *new = NULL; |
| 2253 | struct ospf_lsa *old = NULL; |
| 2254 | struct ospf_lsdb *lsdb = NULL; |
| 2255 | int rt_recalc; |
| 2256 | |
| 2257 | /* Set LSDB. */ |
| 2258 | switch (lsa->data->type) |
| 2259 | { |
| 2260 | case OSPF_AS_EXTERNAL_LSA: |
| 2261 | #ifdef HAVE_OPAQUE_LSA |
| 2262 | case OSPF_OPAQUE_AS_LSA: |
| 2263 | #endif /* HAVE_OPAQUE_LSA */ |
| 2264 | lsdb = ospf_top->lsdb; |
| 2265 | break; |
| 2266 | default: |
| 2267 | lsdb = lsa->area->lsdb; |
| 2268 | break; |
| 2269 | } |
| 2270 | |
| 2271 | #ifdef HAVE_NSSA |
| 2272 | if (IS_DEBUG_OSPF_NSSA) |
| 2273 | { |
| 2274 | zlog_info ("LSA[Installing]: Type-%d ", lsa->data->type); |
| 2275 | |
| 2276 | if (lsa->data->type == OSPF_AS_NSSA_LSA ) |
| 2277 | zlog_info ("NSSA LSA AREA = %s", inet_ntoa (lsa->area->area_id)); |
| 2278 | } |
| 2279 | #endif /* HAVE_NSSA */ |
| 2280 | |
| 2281 | assert (lsdb); |
| 2282 | |
| 2283 | /* RFC 2328 13.2. Installing LSAs in the database |
| 2284 | |
| 2285 | Installing a new LSA in the database, either as the result of |
| 2286 | flooding or a newly self-originated LSA, may cause the OSPF |
| 2287 | routing table structure to be recalculated. The contents of the |
| 2288 | new LSA should be compared to the old instance, if present. If |
| 2289 | there is no difference, there is no need to recalculate the |
| 2290 | routing table. When comparing an LSA to its previous instance, |
| 2291 | the following are all considered to be differences in contents: |
| 2292 | |
| 2293 | o The LSA's Options field has changed. |
| 2294 | |
| 2295 | o One of the LSA instances has LS age set to MaxAge, and |
| 2296 | the other does not. |
| 2297 | |
| 2298 | o The length field in the LSA header has changed. |
| 2299 | |
| 2300 | o The body of the LSA (i.e., anything outside the 20-byte |
| 2301 | LSA header) has changed. Note that this excludes changes |
| 2302 | in LS Sequence Number and LS Checksum. |
| 2303 | |
| 2304 | */ |
| 2305 | /* Look up old LSA and determine if any SPF calculation or incremental |
| 2306 | update is needed */ |
| 2307 | old = ospf_lsdb_lookup (lsdb, lsa); |
| 2308 | |
| 2309 | /* Do comparision and record if recalc needed. */ |
| 2310 | rt_recalc = 0; |
| 2311 | if ( old == NULL || ospf_lsa_different(old, lsa)) |
| 2312 | rt_recalc = 1; |
| 2313 | |
| 2314 | /* discard old LSA from LSDB */ |
| 2315 | if (old != NULL) |
| 2316 | ospf_discard_from_db (lsdb, lsa); |
| 2317 | |
| 2318 | /* Insert LSA to LSDB. */ |
| 2319 | ospf_lsdb_add (lsdb, lsa); |
| 2320 | lsa->lsdb = lsdb; |
| 2321 | |
| 2322 | /* Calculate Checksum if self-originated?. */ |
| 2323 | if (IS_LSA_SELF (lsa)) |
| 2324 | ospf_lsa_checksum (lsa->data); |
| 2325 | |
| 2326 | /* Do LSA specific installation process. */ |
| 2327 | switch (lsa->data->type) |
| 2328 | { |
| 2329 | case OSPF_ROUTER_LSA: |
| 2330 | new = ospf_router_lsa_install (lsa, rt_recalc); |
| 2331 | break; |
| 2332 | case OSPF_NETWORK_LSA: |
| 2333 | assert (oi); |
| 2334 | new = ospf_network_lsa_install (oi, lsa, rt_recalc); |
| 2335 | break; |
| 2336 | case OSPF_SUMMARY_LSA: |
| 2337 | new = ospf_summary_lsa_install (lsa, rt_recalc); |
| 2338 | break; |
| 2339 | case OSPF_ASBR_SUMMARY_LSA: |
| 2340 | new = ospf_summary_asbr_lsa_install (lsa, rt_recalc); |
| 2341 | break; |
| 2342 | case OSPF_AS_EXTERNAL_LSA: |
| 2343 | new = ospf_external_lsa_install (lsa, rt_recalc); |
| 2344 | break; |
| 2345 | #ifdef HAVE_OPAQUE_LSA |
| 2346 | case OSPF_OPAQUE_LINK_LSA: |
| 2347 | case OSPF_OPAQUE_AREA_LSA: |
| 2348 | case OSPF_OPAQUE_AS_LSA: |
| 2349 | new = ospf_opaque_lsa_install (lsa, rt_recalc); |
| 2350 | break; |
| 2351 | #endif /* HAVE_OPAQUE_LSA */ |
| 2352 | default: /* NSSA, or type-6,8,9....nothing special */ |
| 2353 | #ifdef HAVE_NSSA |
| 2354 | new = ospf_external_lsa_install (lsa, rt_recalc); |
| 2355 | #endif /* HAVE_NSSA */ |
| 2356 | break; |
| 2357 | } |
| 2358 | |
| 2359 | if (new == NULL) |
| 2360 | return new; /* Installation failed, cannot proceed further -- endo. */ |
| 2361 | |
| 2362 | /* Debug logs. */ |
| 2363 | if (IS_DEBUG_OSPF (lsa, LSA_INSTALL)) |
| 2364 | { |
| 2365 | char area_str[INET_ADDRSTRLEN]; |
| 2366 | |
| 2367 | switch (lsa->data->type) |
| 2368 | { |
| 2369 | case OSPF_AS_EXTERNAL_LSA: |
| 2370 | #ifdef HAVE_OPAQUE_LSA |
| 2371 | case OSPF_OPAQUE_AS_LSA: |
| 2372 | #endif /* HAVE_OPAQUE_LSA */ |
| 2373 | zlog_info ("LSA[%s]: Install %s", |
| 2374 | dump_lsa_key (new), |
| 2375 | LOOKUP (ospf_lsa_type_msg, new->data->type)); |
| 2376 | break; |
| 2377 | default: |
| 2378 | strcpy (area_str, inet_ntoa (new->area->area_id)); |
| 2379 | zlog_info ("LSA[%s]: Install %s to Area %s", |
| 2380 | dump_lsa_key (new), |
| 2381 | LOOKUP (ospf_lsa_type_msg, new->data->type), area_str); |
| 2382 | break; |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | /* If received LSA' ls_age is MaxAge, set LSA on MaxAge LSA list. */ |
| 2387 | if (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)) |
| 2388 | { |
| 2389 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2390 | zlog_info ("LSA[Type%d:%s]: Install LSA, MaxAge", |
| 2391 | new->data->type, inet_ntoa (new->data->id)); |
| 2392 | ospf_lsa_maxage (lsa); |
| 2393 | } |
| 2394 | |
| 2395 | return new; |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | int |
| 2400 | ospf_check_nbr_status () |
| 2401 | { |
| 2402 | listnode node; |
| 2403 | |
| 2404 | for (node = listhead (ospf_top->oiflist); node; node = nextnode (node)) |
| 2405 | { |
| 2406 | struct ospf_interface *oi = getdata (node); |
| 2407 | struct route_node *rn; |
| 2408 | struct ospf_neighbor *nbr; |
| 2409 | |
| 2410 | if (ospf_if_is_enable (oi)) |
| 2411 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 2412 | if ((nbr = rn->info) != NULL) |
| 2413 | if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading) |
| 2414 | { |
| 2415 | route_unlock_node (rn); |
| 2416 | return 0; |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | return 1; |
| 2421 | } |
| 2422 | |
| 2423 | |
| 2424 | #ifdef ORIGINAL_CODING |
| 2425 | /* This function flood the maxaged LSA to DR. */ |
| 2426 | void |
| 2427 | ospf_maxage_flood (struct ospf_lsa *lsa) |
| 2428 | { |
| 2429 | switch (lsa->data->type) |
| 2430 | { |
| 2431 | case OSPF_ROUTER_LSA: |
| 2432 | case OSPF_NETWORK_LSA: |
| 2433 | case OSPF_SUMMARY_LSA: |
| 2434 | case OSPF_ASBR_SUMMARY_LSA: |
| 2435 | #ifdef HAVE_NSSA |
| 2436 | case OSPF_AS_NSSA_LSA: |
| 2437 | #endif /* HAVE_NSSA */ |
| 2438 | #ifdef HAVE_OPAQUE_LSA |
| 2439 | case OSPF_OPAQUE_LINK_LSA: |
| 2440 | case OSPF_OPAQUE_AREA_LSA: |
| 2441 | #endif /* HAVE_OPAQUE_LSA */ |
| 2442 | ospf_flood_through_area (lsa->area, NULL, lsa); |
| 2443 | break; |
| 2444 | case OSPF_AS_EXTERNAL_LSA: |
| 2445 | #ifdef HAVE_OPAQUE_LSA |
| 2446 | case OSPF_OPAQUE_AS_LSA: |
| 2447 | #endif /* HAVE_OPAQUE_LSA */ |
| 2448 | ospf_flood_through_as (NULL, lsa); |
| 2449 | break; |
| 2450 | default: |
| 2451 | break; |
| 2452 | } |
| 2453 | } |
| 2454 | #endif /* ORIGINAL_CODING */ |
| 2455 | |
| 2456 | int |
| 2457 | ospf_maxage_lsa_remover (struct thread *thread) |
| 2458 | { |
| 2459 | listnode node; |
| 2460 | listnode next; |
| 2461 | int reschedule = 0; |
| 2462 | |
| 2463 | ospf_top->t_maxage = NULL; |
| 2464 | |
| 2465 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2466 | zlog_info ("LSA[MaxAge]: remover Start"); |
| 2467 | |
| 2468 | reschedule = !ospf_check_nbr_status (); |
| 2469 | |
| 2470 | if (!reschedule) |
| 2471 | for (node = listhead (ospf_top->maxage_lsa); node; node = next) |
| 2472 | { |
| 2473 | struct ospf_lsa *lsa = getdata (node); |
| 2474 | next = node->next; |
| 2475 | |
| 2476 | if (lsa->retransmit_counter > 0) |
| 2477 | { |
| 2478 | reschedule = 1; |
| 2479 | continue; |
| 2480 | } |
| 2481 | |
| 2482 | /* Remove LSA from the LSDB */ |
| 2483 | if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF)) |
| 2484 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2485 | zlog_info ("LSA[Type%d:%s]: This LSA is self-originated: ", |
| 2486 | lsa->data->type, inet_ntoa (lsa->data->id)); |
| 2487 | |
| 2488 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2489 | zlog_info ("LSA[Type%d:%s]: MaxAge LSA removed from list", |
| 2490 | lsa->data->type, inet_ntoa (lsa->data->id)); |
| 2491 | |
| 2492 | /* Flood max age LSA. */ |
| 2493 | #ifdef ORIGINAL_CODING |
| 2494 | ospf_maxage_flood (lsa); |
| 2495 | #else /* ORIGINAL_CODING */ |
| 2496 | ospf_flood_through (NULL, lsa); |
| 2497 | #endif /* ORIGINAL_CODING */ |
| 2498 | |
| 2499 | /* Remove from lsdb. */ |
| 2500 | ospf_discard_from_db (lsa->lsdb, lsa); |
| 2501 | ospf_lsdb_delete (lsa->lsdb, lsa); |
| 2502 | } |
| 2503 | |
| 2504 | /* A MaxAge LSA must be removed immediately from the router's link |
| 2505 | state database as soon as both a) it is no longer contained on any |
| 2506 | neighbor Link state retransmission lists and b) none of the router's |
| 2507 | neighbors are in states Exchange or Loading. */ |
| 2508 | if (reschedule) |
| 2509 | OSPF_SCHEDULE_MAXAGE (ospf_top->t_maxage, ospf_maxage_lsa_remover); |
| 2510 | |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | int |
| 2515 | ospf_lsa_maxage_exist (struct ospf_lsa *new) |
| 2516 | { |
| 2517 | listnode node; |
| 2518 | |
| 2519 | for (node = listhead (ospf_top->maxage_lsa); node; nextnode (node)) |
| 2520 | if (((struct ospf_lsa *) node->data) == new) |
| 2521 | return 1; |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | void |
| 2527 | ospf_lsa_maxage_delete (struct ospf_lsa *lsa) |
| 2528 | { |
| 2529 | listnode n; |
| 2530 | |
| 2531 | if ((n = listnode_lookup (ospf_top->maxage_lsa, lsa))) |
| 2532 | { |
| 2533 | list_delete_node (ospf_top->maxage_lsa, n); |
| 2534 | ospf_lsa_unlock (lsa); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | void |
| 2539 | ospf_lsa_maxage (struct ospf_lsa *lsa) |
| 2540 | { |
| 2541 | /* When we saw a MaxAge LSA flooded to us, we put it on the list |
| 2542 | and schedule the MaxAge LSA remover. */ |
| 2543 | if (ospf_lsa_maxage_exist (lsa)) |
| 2544 | { |
| 2545 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2546 | zlog_info ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list", |
| 2547 | lsa->data->type, inet_ntoa (lsa->data->id), lsa); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | listnode_add (ospf_top->maxage_lsa, ospf_lsa_lock (lsa)); |
| 2552 | |
| 2553 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2554 | zlog_info ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa)); |
| 2555 | |
| 2556 | OSPF_SCHEDULE_MAXAGE (ospf_top->t_maxage, ospf_maxage_lsa_remover); |
| 2557 | } |
| 2558 | |
| 2559 | int |
| 2560 | ospf_lsa_maxage_walker_remover (struct ospf_lsa *lsa, void *p_arg, int int_arg) |
| 2561 | { |
| 2562 | #ifdef HAVE_NSSA |
| 2563 | /* Stay away from any Local Translated Type-7 LSAs */ |
| 2564 | if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)) |
| 2565 | return 0; |
| 2566 | #endif /* HAVE_NSSA */ |
| 2567 | |
| 2568 | if (IS_LSA_MAXAGE (lsa)) |
| 2569 | /* Self-originated LSAs should NOT time-out instead, |
| 2570 | they're flushed and submitted to the max_age list explicitly. */ |
| 2571 | if (!ospf_lsa_is_self_originated (lsa)) |
| 2572 | { |
| 2573 | if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) |
| 2574 | zlog_info("LSA[%s]: is MaxAge", dump_lsa_key (lsa)); |
| 2575 | |
| 2576 | switch (lsa->data->type) |
| 2577 | { |
| 2578 | case OSPF_AS_EXTERNAL_LSA: |
| 2579 | #ifdef HAVE_OPAQUE_LSA |
| 2580 | case OSPF_OPAQUE_AS_LSA: |
| 2581 | #endif /* HAVE_OPAQUE_LSA */ |
| 2582 | ospf_ase_incremental_update (lsa, ospf_top); |
| 2583 | break; |
| 2584 | default: |
| 2585 | ospf_spf_calculate_schedule (); |
| 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | ospf_lsa_maxage (lsa); |
| 2590 | } |
| 2591 | |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
| 2595 | /* Periodical check of MaxAge LSA. */ |
| 2596 | int |
| 2597 | ospf_lsa_maxage_walker (struct thread *t) |
| 2598 | { |
| 2599 | listnode node; |
| 2600 | |
| 2601 | ospf_top->t_maxage_walker = NULL; |
| 2602 | |
| 2603 | for (node = listhead (ospf_top->areas); node; nextnode (node)) |
| 2604 | { |
| 2605 | struct ospf_area *area = node->data; |
| 2606 | |
| 2607 | foreach_lsa (ROUTER_LSDB (area), NULL, 0, |
| 2608 | ospf_lsa_maxage_walker_remover); |
| 2609 | foreach_lsa (NETWORK_LSDB (area), NULL, 0, |
| 2610 | ospf_lsa_maxage_walker_remover); |
| 2611 | foreach_lsa (SUMMARY_LSDB (area), NULL, 0, |
| 2612 | ospf_lsa_maxage_walker_remover); |
| 2613 | foreach_lsa (ASBR_SUMMARY_LSDB (area), NULL, 0, |
| 2614 | ospf_lsa_maxage_walker_remover); |
| 2615 | #ifdef HAVE_OPAQUE_LSA |
| 2616 | foreach_lsa (OPAQUE_LINK_LSDB (area), NULL, 0, |
| 2617 | ospf_lsa_maxage_walker_remover); |
| 2618 | foreach_lsa (OPAQUE_AREA_LSDB (area), NULL, 0, |
| 2619 | ospf_lsa_maxage_walker_remover); |
| 2620 | #endif /* HAVE_OPAQUE_LSA */ |
| 2621 | } |
| 2622 | |
| 2623 | /* for AS-eternal-LSAs. */ |
| 2624 | if (ospf_top->lsdb) |
| 2625 | foreach_lsa (EXTERNAL_LSDB (ospf_top), NULL, 0, |
| 2626 | ospf_lsa_maxage_walker_remover); |
| 2627 | |
| 2628 | #ifdef HAVE_OPAQUE_LSA |
| 2629 | if (ospf_top->lsdb) |
| 2630 | foreach_lsa (OPAQUE_AS_LSDB (ospf_top), NULL, 0, |
| 2631 | ospf_lsa_maxage_walker_remover); |
| 2632 | #endif /* HAVE_OPAQUE_LSA */ |
| 2633 | |
| 2634 | ospf_top->t_maxage_walker = |
| 2635 | thread_add_timer (master, ospf_lsa_maxage_walker, NULL, |
| 2636 | OSPF_LSA_MAXAGE_CHECK_INTERVAL); |
| 2637 | return 0; |
| 2638 | } |
| 2639 | |
| 2640 | int |
| 2641 | find_summary (struct ospf_lsa *lsa, void * v, int i) |
| 2642 | { |
| 2643 | struct prefix_ipv4 *p, pr; |
| 2644 | |
| 2645 | if ((p = (struct prefix_ipv4 *) v) != NULL) |
| 2646 | if (lsa != NULL) |
| 2647 | /* We're looking for self-originated one */ |
| 2648 | if (ospf_lsa_is_self_originated (lsa)) |
| 2649 | { |
| 2650 | struct summary_lsa *sl = (struct summary_lsa *) lsa->data; |
| 2651 | |
| 2652 | pr.family = AF_INET; |
| 2653 | pr.prefix = sl->header.id; |
| 2654 | pr.prefixlen = ip_masklen (sl->mask); |
| 2655 | apply_mask_ipv4 (&pr); |
| 2656 | |
| 2657 | if (prefix_same ((struct prefix*) &pr, (struct prefix*) p)) |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | int |
| 2665 | find_asbr_summary (struct ospf_lsa *lsa, void * v, int i) |
| 2666 | { |
| 2667 | struct prefix_ipv4 *p; |
| 2668 | |
| 2669 | if ((p = (struct prefix_ipv4 *) v) != NULL) |
| 2670 | if (lsa != NULL) |
| 2671 | /* We're looking for self-originated one */ |
| 2672 | if (ospf_lsa_is_self_originated (lsa)) |
| 2673 | { |
| 2674 | struct summary_lsa *sl = (struct summary_lsa *) lsa->data; |
| 2675 | |
| 2676 | if (IPV4_ADDR_SAME (&p->prefix, &sl->header.id)) |
| 2677 | return 1; |
| 2678 | } |
| 2679 | |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
| 2683 | struct ospf_lsa * |
| 2684 | ospf_lsa_lookup (struct ospf_area *area, u_int32_t type, |
| 2685 | struct in_addr id, struct in_addr adv_router) |
| 2686 | { |
| 2687 | switch (type) |
| 2688 | { |
| 2689 | case OSPF_ROUTER_LSA: |
| 2690 | case OSPF_NETWORK_LSA: |
| 2691 | case OSPF_SUMMARY_LSA: |
| 2692 | case OSPF_ASBR_SUMMARY_LSA: |
| 2693 | #ifdef HAVE_NSSA |
| 2694 | case OSPF_AS_NSSA_LSA: |
| 2695 | #endif /* HAVE_NSSA */ |
| 2696 | #ifdef HAVE_OPAQUE_LSA |
| 2697 | case OSPF_OPAQUE_LINK_LSA: |
| 2698 | case OSPF_OPAQUE_AREA_LSA: |
| 2699 | #endif /* HAVE_OPAQUE_LSA */ |
| 2700 | return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router); |
| 2701 | break; |
| 2702 | case OSPF_AS_EXTERNAL_LSA: |
| 2703 | #ifdef HAVE_OPAQUE_LSA |
| 2704 | case OSPF_OPAQUE_AS_LSA: |
| 2705 | #endif /* HAVE_OPAQUE_LSA */ |
| 2706 | return ospf_lsdb_lookup_by_id (ospf_top->lsdb, type, id, adv_router); |
| 2707 | break; |
| 2708 | default: |
| 2709 | break; |
| 2710 | } |
| 2711 | |
| 2712 | return NULL; |
| 2713 | } |
| 2714 | |
| 2715 | struct ospf_lsa * |
| 2716 | ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type, |
| 2717 | struct in_addr id) |
| 2718 | { |
| 2719 | struct ospf_lsa *lsa; |
| 2720 | struct route_node *rn; |
| 2721 | |
| 2722 | switch (type) |
| 2723 | { |
| 2724 | case OSPF_ROUTER_LSA: |
| 2725 | return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id); |
| 2726 | break; |
| 2727 | case OSPF_NETWORK_LSA: |
| 2728 | for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn)) |
| 2729 | if ((lsa = rn->info)) |
| 2730 | if (IPV4_ADDR_SAME (&lsa->data->id, &id)) |
| 2731 | { |
| 2732 | route_unlock_node (rn); |
| 2733 | return lsa; |
| 2734 | } |
| 2735 | break; |
| 2736 | case OSPF_SUMMARY_LSA: |
| 2737 | case OSPF_ASBR_SUMMARY_LSA: |
| 2738 | /* Currently not used. */ |
| 2739 | assert (1); |
| 2740 | return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id); |
| 2741 | break; |
| 2742 | case OSPF_AS_EXTERNAL_LSA: |
| 2743 | #ifdef HAVE_OPAQUE_LSA |
| 2744 | case OSPF_OPAQUE_LINK_LSA: |
| 2745 | case OSPF_OPAQUE_AREA_LSA: |
| 2746 | case OSPF_OPAQUE_AS_LSA: |
| 2747 | /* Currently not used. */ |
| 2748 | break; |
| 2749 | #endif /* HAVE_OPAQUE_LSA */ |
| 2750 | default: |
| 2751 | break; |
| 2752 | } |
| 2753 | |
| 2754 | return NULL; |
| 2755 | } |
| 2756 | |
| 2757 | struct ospf_lsa * |
| 2758 | ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah) |
| 2759 | { |
| 2760 | struct ospf_lsa *match; |
| 2761 | |
| 2762 | #ifdef HAVE_OPAQUE_LSA |
| 2763 | /* |
| 2764 | * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11) |
| 2765 | * is redefined to have two subfields; opaque-type and opaque-id. |
| 2766 | * However, it is harmless to treat the two sub fields together, as if |
| 2767 | * they two were forming a unique LSA-ID. |
| 2768 | */ |
| 2769 | #endif /* HAVE_OPAQUE_LSA */ |
| 2770 | |
| 2771 | match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router); |
| 2772 | |
| 2773 | if (match == NULL) |
| 2774 | if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA) |
| 2775 | zlog_info ("LSA[Type%d:%s]: Lookup by header, NO MATCH", |
| 2776 | lsah->type, inet_ntoa (lsah->id)); |
| 2777 | |
| 2778 | return match; |
| 2779 | } |
| 2780 | |
| 2781 | /* return +n, l1 is more recent. |
| 2782 | return -n, l2 is more recent. |
| 2783 | return 0, l1 and l2 is identical. */ |
| 2784 | int |
| 2785 | ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2) |
| 2786 | { |
| 2787 | int r; |
| 2788 | int x, y; |
| 2789 | |
| 2790 | if (l1 == NULL && l2 == NULL) |
| 2791 | return 0; |
| 2792 | if (l1 == NULL) |
| 2793 | return -1; |
| 2794 | if (l2 == NULL) |
| 2795 | return 1; |
| 2796 | |
| 2797 | /* compare LS sequence number. */ |
| 2798 | x = (int) ntohl (l1->data->ls_seqnum); |
| 2799 | y = (int) ntohl (l2->data->ls_seqnum); |
| 2800 | if (x > y) |
| 2801 | return 1; |
| 2802 | if (x < y) |
| 2803 | return -1; |
| 2804 | |
| 2805 | /* compare LS checksum. */ |
| 2806 | r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum); |
| 2807 | if (r) |
| 2808 | return r; |
| 2809 | |
| 2810 | /* compare LS age. */ |
| 2811 | if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2)) |
| 2812 | return 1; |
| 2813 | else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2)) |
| 2814 | return -1; |
| 2815 | |
| 2816 | /* compare LS age with MaxAgeDiff. */ |
| 2817 | if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF) |
| 2818 | return -1; |
| 2819 | else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF) |
| 2820 | return 1; |
| 2821 | |
| 2822 | /* LSAs are identical. */ |
| 2823 | return 0; |
| 2824 | } |
| 2825 | |
| 2826 | /* If two LSAs are different, return 1, otherwise return 0. */ |
| 2827 | int |
| 2828 | ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2) |
| 2829 | { |
| 2830 | char *p1, *p2; |
| 2831 | assert (l1); |
| 2832 | assert (l2); |
| 2833 | assert (l1->data); |
| 2834 | assert (l2->data); |
| 2835 | |
| 2836 | if (l1->data->options != l2->data->options) |
| 2837 | return 1; |
| 2838 | |
| 2839 | if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2)) |
| 2840 | return 1; |
| 2841 | |
| 2842 | if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1)) |
| 2843 | return 1; |
| 2844 | |
| 2845 | if (l1->data->length != l2->data->length) |
| 2846 | return 1; |
| 2847 | |
| 2848 | if (l1->data->length == 0) |
| 2849 | return 1; |
| 2850 | |
| 2851 | assert (l1->data->length > OSPF_LSA_HEADER_SIZE); |
| 2852 | |
| 2853 | p1 = (char *) l1->data; |
| 2854 | p2 = (char *) l2->data; |
| 2855 | |
| 2856 | if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE, |
| 2857 | ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0) |
| 2858 | return 1; |
| 2859 | |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
| 2863 | #ifdef ORIGINAL_CODING |
| 2864 | void |
| 2865 | ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr, |
| 2866 | struct ospf_lsa *self, |
| 2867 | struct ospf_lsa *new) |
| 2868 | { |
| 2869 | u_int32_t seqnum; |
| 2870 | |
| 2871 | /* Adjust LS Sequence Number. */ |
| 2872 | seqnum = ntohl (new->data->ls_seqnum) + 1; |
| 2873 | self->data->ls_seqnum = htonl (seqnum); |
| 2874 | |
| 2875 | /* Recalculate LSA checksum. */ |
| 2876 | ospf_lsa_checksum (self->data); |
| 2877 | |
| 2878 | /* Reflooding LSA. */ |
| 2879 | /* RFC2328 Section 13.3 |
| 2880 | On non-broadcast networks, separate Link State Update |
| 2881 | packets must be sent, as unicasts, to each adjacent neighbor |
| 2882 | (i.e., those in state Exchange or greater). The destination |
| 2883 | IP addresses for these packets are the neighbors' IP |
| 2884 | addresses. */ |
| 2885 | if (nbr->oi->type == OSPF_IFTYPE_NBMA) |
| 2886 | { |
| 2887 | struct route_node *rn; |
| 2888 | struct ospf_neighbor *onbr; |
| 2889 | |
| 2890 | for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn)) |
| 2891 | if ((onbr = rn->info) != NULL) |
| 2892 | if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange) |
| 2893 | ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT); |
| 2894 | } |
| 2895 | else |
| 2896 | ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT); |
| 2897 | |
| 2898 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 2899 | zlog_info ("LSA[Type%d:%s]: Flush self-originated LSA", |
| 2900 | self->data->type, inet_ntoa (self->data->id)); |
| 2901 | } |
| 2902 | #else /* ORIGINAL_CODING */ |
| 2903 | static int |
| 2904 | ospf_lsa_flush_schedule (struct ospf_lsa *lsa, void *v, int i) |
| 2905 | { |
| 2906 | if (lsa == NULL || !IS_LSA_SELF (lsa)) |
| 2907 | return 0; |
| 2908 | |
| 2909 | if (IS_DEBUG_OSPF_EVENT) |
| 2910 | zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id)); |
| 2911 | |
| 2912 | /* Force given lsa's age to MaxAge. */ |
| 2913 | lsa->data->ls_age = htons (OSPF_LSA_MAXAGE); |
| 2914 | |
| 2915 | switch (lsa->data->type) |
| 2916 | { |
| 2917 | #ifdef HAVE_OPAQUE_LSA |
| 2918 | case OSPF_OPAQUE_LINK_LSA: |
| 2919 | case OSPF_OPAQUE_AREA_LSA: |
| 2920 | case OSPF_OPAQUE_AS_LSA: |
| 2921 | ospf_opaque_lsa_refresh (lsa); |
| 2922 | break; |
| 2923 | #endif /* HAVE_OPAQUE_LSA */ |
| 2924 | default: |
| 2925 | ospf_lsa_maxage (lsa); |
| 2926 | break; |
| 2927 | } |
| 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
| 2932 | void |
| 2933 | ospf_flush_self_originated_lsas_now (struct ospf *top) |
| 2934 | { |
| 2935 | listnode n1, n2; |
| 2936 | struct ospf_area *area; |
| 2937 | struct ospf_interface *oi; |
| 2938 | struct ospf_lsa *lsa; |
| 2939 | int need_to_flush_ase = 0; |
| 2940 | |
| 2941 | for (n1 = listhead (top->areas); n1; nextnode (n1)) |
| 2942 | { |
| 2943 | if ((area = getdata (n1)) == NULL) |
| 2944 | continue; |
| 2945 | |
| 2946 | if ((lsa = area->router_lsa_self) != NULL) |
| 2947 | { |
| 2948 | if (IS_DEBUG_OSPF_EVENT) |
| 2949 | zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id)); |
| 2950 | |
| 2951 | ospf_lsa_flush_area (lsa, area); |
| 2952 | ospf_lsa_unlock (area->router_lsa_self); |
| 2953 | area->router_lsa_self = NULL; |
| 2954 | OSPF_TIMER_OFF (area->t_router_lsa_self); |
| 2955 | } |
| 2956 | |
| 2957 | for (n2 = listhead (area->oiflist); n2; nextnode (n2)) |
| 2958 | { |
| 2959 | if ((oi = getdata (n2)) == NULL) |
| 2960 | continue; |
| 2961 | |
| 2962 | if ((lsa = oi->network_lsa_self) != NULL |
| 2963 | && oi->state == ISM_DR |
| 2964 | && oi->full_nbrs > 0) |
| 2965 | { |
| 2966 | if (IS_DEBUG_OSPF_EVENT) |
| 2967 | zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id)); |
| 2968 | |
| 2969 | ospf_lsa_flush_area (oi->network_lsa_self, area); |
| 2970 | ospf_lsa_unlock (oi->network_lsa_self); |
| 2971 | oi->network_lsa_self = NULL; |
| 2972 | OSPF_TIMER_OFF (oi->t_network_lsa_self); |
| 2973 | } |
| 2974 | |
| 2975 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK |
| 2976 | && area->external_routing == OSPF_AREA_DEFAULT) |
| 2977 | need_to_flush_ase = 1; |
| 2978 | } |
| 2979 | |
| 2980 | foreach_lsa (SUMMARY_LSDB (area), NULL, 0, ospf_lsa_flush_schedule); |
| 2981 | foreach_lsa (ASBR_SUMMARY_LSDB (area), NULL, 0, ospf_lsa_flush_schedule); |
| 2982 | #ifdef HAVE_OPAQUE_LSA |
| 2983 | foreach_lsa (OPAQUE_LINK_LSDB (area), |
| 2984 | NULL, 0, ospf_lsa_flush_schedule); |
| 2985 | foreach_lsa (OPAQUE_AREA_LSDB (area), |
| 2986 | NULL, 0, ospf_lsa_flush_schedule); |
| 2987 | #endif /* HAVE_OPAQUE_LSA */ |
| 2988 | } |
| 2989 | |
| 2990 | if (need_to_flush_ase) |
| 2991 | { |
| 2992 | foreach_lsa (EXTERNAL_LSDB (top), NULL, 0, ospf_lsa_flush_schedule); |
| 2993 | #ifdef HAVE_OPAQUE_LSA |
| 2994 | foreach_lsa (OPAQUE_AS_LSDB (top), |
| 2995 | NULL, 0, ospf_lsa_flush_schedule); |
| 2996 | #endif /* HAVE_OPAQUE_LSA */ |
| 2997 | } |
| 2998 | |
| 2999 | /* |
| 3000 | * Make sure that the MaxAge LSA remover is executed immediately, |
| 3001 | * without conflicting to other threads. |
| 3002 | */ |
| 3003 | if (top->t_maxage != NULL) |
| 3004 | { |
| 3005 | OSPF_TIMER_OFF (top->t_maxage); |
| 3006 | thread_execute (master, ospf_maxage_lsa_remover, top, 0); |
| 3007 | } |
| 3008 | |
| 3009 | return; |
| 3010 | } |
| 3011 | #endif /* ORIGINAL_CODING */ |
| 3012 | |
| 3013 | /* If there is self-originated LSA, then return 1, otherwise return 0. */ |
| 3014 | /* An interface-independent version of ospf_lsa_is_self_originated */ |
| 3015 | int |
| 3016 | ospf_lsa_is_self_originated (struct ospf_lsa *lsa) |
| 3017 | { |
| 3018 | listnode node; |
| 3019 | |
| 3020 | /* This LSA is already checked. */ |
| 3021 | if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED)) |
| 3022 | return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3023 | |
| 3024 | /* Make sure LSA is self-checked. */ |
| 3025 | SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED); |
| 3026 | |
| 3027 | /* AdvRouter and Router ID is the same. */ |
| 3028 | if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf_top->router_id)) |
| 3029 | SET_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3030 | |
| 3031 | /* LSA is router-LSA. */ |
| 3032 | else if (lsa->data->type == OSPF_ROUTER_LSA && |
| 3033 | IPV4_ADDR_SAME (&lsa->data->id, &ospf_top->router_id)) |
| 3034 | SET_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3035 | |
| 3036 | /* LSA is network-LSA. Compare Link ID with all interfaces. */ |
| 3037 | else if (lsa->data->type == OSPF_NETWORK_LSA) |
| 3038 | for (node = listhead (ospf_top->oiflist); node; nextnode (node)) |
| 3039 | { |
| 3040 | struct ospf_interface *oi = getdata (node); |
| 3041 | |
| 3042 | /* Ignore virtual link. */ |
| 3043 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 3044 | if (oi->address->family == AF_INET) |
| 3045 | if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4)) |
| 3046 | { |
| 3047 | /* to make it easier later */ |
| 3048 | SET_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3049 | return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF); |
| 3054 | } |
| 3055 | |
| 3056 | /* Get unique Link State ID. */ |
| 3057 | struct in_addr |
| 3058 | ospf_lsa_unique_id (struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p) |
| 3059 | { |
| 3060 | struct ospf_lsa *lsa; |
| 3061 | struct in_addr mask, id; |
| 3062 | |
| 3063 | id = p->prefix; |
| 3064 | |
| 3065 | /* Check existence of LSA instance. */ |
| 3066 | lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf_top->router_id); |
| 3067 | if (lsa) |
| 3068 | { |
| 3069 | struct as_external_lsa *al = (struct as_external_lsa *) lsa->data; |
| 3070 | if (ip_masklen (al->mask) == p->prefixlen) |
| 3071 | { |
| 3072 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 3073 | zlog_warn ("ospf_lsa_unique_id(): " |
| 3074 | "Can't get Link State ID for %s/%d", |
| 3075 | inet_ntoa (p->prefix), p->prefixlen); |
| 3076 | /* id.s_addr = 0; */ |
| 3077 | id.s_addr = 0xffffffff; |
| 3078 | return id; |
| 3079 | } |
| 3080 | /* Masklen differs, then apply wildcard mask to Link State ID. */ |
| 3081 | else |
| 3082 | { |
| 3083 | masklen2ip (p->prefixlen, &mask); |
| 3084 | |
| 3085 | id.s_addr = p->prefix.s_addr | (~mask.s_addr); |
| 3086 | lsa = ospf_lsdb_lookup_by_id (ospf_top->lsdb, type, |
| 3087 | id, ospf_top->router_id); |
| 3088 | if (lsa) |
| 3089 | { |
| 3090 | if (IS_DEBUG_OSPF (lsa, LSA_GENERATE)) |
| 3091 | zlog_warn ("ospf_lsa_unique_id(): " |
| 3092 | "Can't get Link State ID for %s/%d", |
| 3093 | inet_ntoa (p->prefix), p->prefixlen); |
| 3094 | /* id.s_addr = 0; */ |
| 3095 | id.s_addr = 0xffffffff; |
| 3096 | return id; |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | return id; |
| 3102 | } |
| 3103 | |
| 3104 | |
| 3105 | #define LSA_ACTION_ORIGN_RTR 1 |
| 3106 | #define LSA_ACTION_ORIGN_NET 2 |
| 3107 | #define LSA_ACTION_FLOOD_AREA 3 |
| 3108 | #define LSA_ACTION_FLOOD_AS 4 |
| 3109 | #define LSA_ACTION_FLUSH_AREA 5 |
| 3110 | #define LSA_ACTION_FLUSH_AS 6 |
| 3111 | |
| 3112 | struct lsa_action |
| 3113 | { |
| 3114 | u_char action; |
| 3115 | struct ospf_area *area; |
| 3116 | struct ospf_interface *oi; |
| 3117 | struct ospf_lsa *lsa; |
| 3118 | }; |
| 3119 | |
| 3120 | int |
| 3121 | ospf_lsa_action (struct thread *t) |
| 3122 | { |
| 3123 | struct lsa_action *data; |
| 3124 | |
| 3125 | data = THREAD_ARG (t); |
| 3126 | |
| 3127 | if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA) |
| 3128 | zlog_info ("LSA[Action]: Performing scheduled LSA action: %d", |
| 3129 | data->action); |
| 3130 | |
| 3131 | switch (data->action) |
| 3132 | { |
| 3133 | case LSA_ACTION_ORIGN_RTR: |
| 3134 | ospf_router_lsa_refresh (data->area->router_lsa_self); |
| 3135 | break; |
| 3136 | case LSA_ACTION_ORIGN_NET: |
| 3137 | ospf_network_lsa_originate (data->oi); |
| 3138 | break; |
| 3139 | case LSA_ACTION_FLOOD_AREA: |
| 3140 | ospf_flood_through_area (data->area, NULL, data->lsa); |
| 3141 | break; |
| 3142 | case LSA_ACTION_FLOOD_AS: |
| 3143 | ospf_flood_through_as (NULL, data->lsa); |
| 3144 | break; |
| 3145 | case LSA_ACTION_FLUSH_AREA: |
| 3146 | ospf_lsa_flush_area (data->lsa, data->area); |
| 3147 | break; |
| 3148 | case LSA_ACTION_FLUSH_AS: |
| 3149 | ospf_lsa_flush_as (data->lsa); |
| 3150 | break; |
| 3151 | } |
| 3152 | |
| 3153 | ospf_lsa_unlock (data->lsa); |
| 3154 | XFREE (MTYPE_OSPF_MESSAGE, data); |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
| 3158 | void |
| 3159 | ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa) |
| 3160 | { |
| 3161 | struct lsa_action *data; |
| 3162 | |
| 3163 | data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action)); |
| 3164 | memset (data, 0, sizeof (struct lsa_action)); |
| 3165 | |
| 3166 | data->action = LSA_ACTION_FLOOD_AREA; |
| 3167 | data->area = area; |
| 3168 | data->lsa = ospf_lsa_lock (lsa); |
| 3169 | |
| 3170 | thread_add_event (master, ospf_lsa_action, data, 0); |
| 3171 | } |
| 3172 | |
| 3173 | void |
| 3174 | ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa) |
| 3175 | { |
| 3176 | struct lsa_action *data; |
| 3177 | |
| 3178 | data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action)); |
| 3179 | memset (data, 0, sizeof (struct lsa_action)); |
| 3180 | |
| 3181 | data->action = LSA_ACTION_FLUSH_AREA; |
| 3182 | data->area = area; |
| 3183 | data->lsa = ospf_lsa_lock (lsa); |
| 3184 | |
| 3185 | thread_add_event (master, ospf_lsa_action, data, 0); |
| 3186 | } |
| 3187 | |
| 3188 | |
| 3189 | /* LSA Refreshment functions. */ |
| 3190 | void |
| 3191 | ospf_lsa_refresh (struct ospf_lsa *lsa) |
| 3192 | { |
| 3193 | struct external_info *ei; |
| 3194 | assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF)); |
| 3195 | |
| 3196 | switch (lsa->data->type) |
| 3197 | { |
| 3198 | /* Router and Network LSAs are processed differently. */ |
| 3199 | case OSPF_ROUTER_LSA: |
| 3200 | case OSPF_NETWORK_LSA: |
| 3201 | break; |
| 3202 | case OSPF_SUMMARY_LSA: |
| 3203 | ospf_summary_lsa_refresh (lsa); |
| 3204 | break; |
| 3205 | case OSPF_ASBR_SUMMARY_LSA: |
| 3206 | ospf_summary_asbr_lsa_refresh (lsa); |
| 3207 | break; |
| 3208 | case OSPF_AS_EXTERNAL_LSA: |
| 3209 | ei = ospf_external_info_check (lsa); |
| 3210 | if (ei) |
| 3211 | ospf_external_lsa_refresh (lsa, ei, LSA_REFRESH_FORCE); |
| 3212 | else |
| 3213 | ospf_lsa_flush_as (lsa); |
| 3214 | break; |
| 3215 | #ifdef HAVE_OPAQUE_LSA |
| 3216 | case OSPF_OPAQUE_LINK_LSA: |
| 3217 | case OSPF_OPAQUE_AREA_LSA: |
| 3218 | case OSPF_OPAQUE_AS_LSA: |
| 3219 | ospf_opaque_lsa_refresh (lsa); |
| 3220 | break; |
| 3221 | default: |
| 3222 | break; |
| 3223 | #endif /* HAVE_OPAQUE_LSA */ |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | void |
| 3228 | ospf_refresher_register_lsa (struct ospf *top, struct ospf_lsa *lsa) |
| 3229 | { |
| 3230 | u_int16_t index, current_index; |
| 3231 | |
| 3232 | assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF)); |
| 3233 | |
| 3234 | if (lsa->refresh_list < 0) |
| 3235 | { |
| 3236 | int delay; |
| 3237 | |
| 3238 | if (LS_AGE (lsa) == 0 && |
| 3239 | ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER) |
| 3240 | /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */ |
| 3241 | delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME); |
| 3242 | else |
| 3243 | /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */ |
| 3244 | delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER |
| 3245 | + (random () % (2*OSPF_LS_REFRESH_JITTER)); |
| 3246 | |
| 3247 | if (delay < 0) |
| 3248 | delay = 0; |
| 3249 | |
| 3250 | current_index = top->lsa_refresh_queue.index + |
| 3251 | (time (NULL) - top->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY; |
| 3252 | |
| 3253 | index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY) |
| 3254 | % (OSPF_LSA_REFRESHER_SLOTS); |
| 3255 | |
| 3256 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3257 | zlog_info ("LSA[Refresh]: lsa with age %d added to index %d", |
| 3258 | LS_AGE (lsa), index); |
| 3259 | if (!top->lsa_refresh_queue.qs[index]) |
| 3260 | top->lsa_refresh_queue.qs[index] = list_new (); |
| 3261 | listnode_add (top->lsa_refresh_queue.qs[index], ospf_lsa_lock (lsa)); |
| 3262 | lsa->refresh_list = index; |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | void |
| 3267 | ospf_refresher_unregister_lsa (struct ospf *top, struct ospf_lsa *lsa) |
| 3268 | { |
| 3269 | assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF)); |
| 3270 | if (lsa->refresh_list >= 0) |
| 3271 | { |
| 3272 | list refresh_list = top->lsa_refresh_queue.qs[lsa->refresh_list]; |
| 3273 | listnode_delete (refresh_list, lsa); |
| 3274 | if (!listcount (refresh_list)) |
| 3275 | { |
| 3276 | list_free (refresh_list); |
| 3277 | top->lsa_refresh_queue.qs[lsa->refresh_list] = NULL; |
| 3278 | } |
| 3279 | ospf_lsa_unlock (lsa); |
| 3280 | lsa->refresh_list = -1; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | int |
| 3285 | ospf_lsa_refresh_walker (struct thread *t) |
| 3286 | { |
| 3287 | list refresh_list; |
| 3288 | listnode node; |
| 3289 | struct ospf *top = THREAD_ARG (t); |
| 3290 | int i; |
| 3291 | list lsa_to_refresh = list_new (); |
| 3292 | |
| 3293 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3294 | zlog_info ("LSA[Refresh]:ospf_lsa_refresh_walker(): start"); |
| 3295 | |
| 3296 | |
| 3297 | i = top->lsa_refresh_queue.index; |
| 3298 | |
| 3299 | top->lsa_refresh_queue.index = |
| 3300 | (top->lsa_refresh_queue.index + |
| 3301 | (time (NULL) - top->lsa_refresher_started) / OSPF_LSA_REFRESHER_GRANULARITY) |
| 3302 | % OSPF_LSA_REFRESHER_SLOTS; |
| 3303 | |
| 3304 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3305 | zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d", |
| 3306 | top->lsa_refresh_queue.index); |
| 3307 | |
| 3308 | for (;i != top->lsa_refresh_queue.index; |
| 3309 | i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) |
| 3310 | { |
| 3311 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3312 | zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d", i); |
| 3313 | |
| 3314 | refresh_list = top->lsa_refresh_queue.qs [i]; |
| 3315 | |
| 3316 | top->lsa_refresh_queue.qs [i] = NULL; |
| 3317 | |
| 3318 | if (refresh_list) |
| 3319 | { |
| 3320 | for (node = listhead (refresh_list); node;) |
| 3321 | { |
| 3322 | listnode next; |
| 3323 | struct ospf_lsa *lsa = getdata (node); |
| 3324 | next = node->next; |
| 3325 | |
| 3326 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3327 | zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): refresh lsa %p", lsa); |
| 3328 | |
| 3329 | list_delete_node (refresh_list, node); |
| 3330 | ospf_lsa_unlock (lsa); |
| 3331 | lsa->refresh_list = -1; |
| 3332 | listnode_add (lsa_to_refresh, lsa); |
| 3333 | node = next; |
| 3334 | } |
| 3335 | list_free (refresh_list); |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | top->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker, |
| 3340 | top, top->lsa_refresh_interval); |
| 3341 | top->lsa_refresher_started = time (NULL); |
| 3342 | |
| 3343 | for (node = listhead (lsa_to_refresh); node; nextnode (node)) |
| 3344 | ospf_lsa_refresh (getdata (node)); |
| 3345 | |
| 3346 | list_delete (lsa_to_refresh); |
| 3347 | |
| 3348 | if (IS_DEBUG_OSPF (lsa, LSA_REFRESH)) |
| 3349 | zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): end"); |
| 3350 | |
| 3351 | return 0; |
| 3352 | } |
| 3353 | |