paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF Interface functions. |
| 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 |
| 8 | * it under the terms of the GNU General Public License as published |
| 9 | * by the Free Software Foundation; either version 2, or (at your |
| 10 | * option) any 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 |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "thread.h" |
| 26 | #include "linklist.h" |
| 27 | #include "prefix.h" |
| 28 | #include "if.h" |
| 29 | #include "table.h" |
| 30 | #include "memory.h" |
| 31 | #include "command.h" |
| 32 | #include "stream.h" |
| 33 | #include "log.h" |
| 34 | |
| 35 | #include "ospfd/ospf_spf.h" |
| 36 | #include "ospfd/ospf_interface.h" |
| 37 | #include "ospfd/ospf_ism.h" |
| 38 | #include "ospfd/ospf_asbr.h" |
| 39 | #include "ospfd/ospf_lsa.h" |
| 40 | #include "ospfd/ospf_lsdb.h" |
| 41 | #include "ospfd/ospf_neighbor.h" |
| 42 | #include "ospfd/ospf_nsm.h" |
| 43 | #include "ospfd/ospf_packet.h" |
| 44 | #include "ospfd/ospf_abr.h" |
| 45 | #include "ospfd/ospfd.h" |
| 46 | #include "ospfd/ospf_network.h" |
| 47 | #include "ospfd/ospf_dump.h" |
| 48 | #ifdef HAVE_SNMP |
| 49 | #include "ospfd/ospf_snmp.h" |
| 50 | #endif /* HAVE_SNMP */ |
| 51 | |
| 52 | |
| 53 | int |
| 54 | ospf_if_get_output_cost (struct ospf_interface *oi) |
| 55 | { |
| 56 | /* If all else fails, use default OSPF cost */ |
| 57 | u_int32_t cost; |
| 58 | u_int32_t bw, refbw; |
| 59 | |
| 60 | bw = oi->ifp->bandwidth ? oi->ifp->bandwidth : OSPF_DEFAULT_BANDWIDTH; |
| 61 | refbw = ospf_top ? ospf_top->ref_bandwidth : OSPF_DEFAULT_REF_BANDWIDTH; |
| 62 | |
| 63 | /* A specifed ip ospf cost overrides a calculated one. */ |
| 64 | if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi->ifp), output_cost_cmd) || |
| 65 | OSPF_IF_PARAM_CONFIGURED (oi->params, output_cost_cmd)) |
| 66 | cost = OSPF_IF_PARAM (oi, output_cost_cmd); |
| 67 | /* See if a cost can be calculated from the zebra processes |
| 68 | interface bandwidth field. */ |
| 69 | else |
| 70 | { |
| 71 | cost = (u_int32_t) ((double)refbw / (double)bw + (double)0.5); |
| 72 | if (cost < 1) |
| 73 | cost = 1; |
| 74 | else if (cost > 65535) |
| 75 | cost = 65535; |
| 76 | } |
| 77 | |
| 78 | return cost; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | ospf_if_recalculate_output_cost (struct interface *ifp) |
| 83 | { |
| 84 | u_int32_t newcost; |
| 85 | struct route_node *rn; |
| 86 | |
| 87 | for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn)) |
| 88 | { |
| 89 | struct ospf_interface *oi; |
| 90 | |
| 91 | if ( (oi = rn->info) == NULL) |
| 92 | continue; |
| 93 | |
| 94 | newcost = ospf_if_get_output_cost (oi); |
| 95 | |
| 96 | /* Is actual output cost changed? */ |
| 97 | if (oi->output_cost != newcost) |
| 98 | { |
| 99 | oi->output_cost = newcost; |
| 100 | ospf_router_lsa_timer_add (oi->area); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | ospf_if_reset_variables (struct ospf_interface *oi) |
| 107 | { |
| 108 | /* Set default values. */ |
| 109 | /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */ |
| 110 | |
| 111 | if (oi->vl_data) |
| 112 | oi->type = OSPF_IFTYPE_VIRTUALLINK; |
| 113 | else |
| 114 | /* preserve network-type */ |
| 115 | if (oi->type != OSPF_IFTYPE_NBMA) |
| 116 | oi->type = OSPF_IFTYPE_BROADCAST; |
| 117 | |
| 118 | oi->state = ISM_Down; |
| 119 | |
| 120 | oi->crypt_seqnum = 0; |
| 121 | |
| 122 | /* This must be short, (less than RxmtInterval) |
| 123 | - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being |
| 124 | held back for too long - MAG */ |
| 125 | oi->v_ls_ack = 1; |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | ospf_add_to_if (struct interface *ifp, struct ospf_interface *oi) |
| 130 | { |
| 131 | struct route_node *rn; |
| 132 | struct prefix p; |
| 133 | |
| 134 | p = *oi->address; |
| 135 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 136 | |
| 137 | rn = route_node_get (IF_OIFS (ifp), &p); |
| 138 | assert (! rn->info); |
| 139 | rn->info = oi; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | ospf_delete_from_if (struct interface *ifp, struct ospf_interface *oi) |
| 144 | { |
| 145 | struct route_node *rn; |
| 146 | struct prefix p; |
| 147 | |
| 148 | p = *oi->address; |
| 149 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 150 | |
| 151 | rn = route_node_lookup (IF_OIFS (oi->ifp), &p); |
| 152 | assert (rn); |
| 153 | assert (rn->info); |
| 154 | rn->info = NULL; |
| 155 | route_unlock_node (rn); |
| 156 | route_unlock_node (rn); |
| 157 | } |
| 158 | |
| 159 | struct ospf_interface * |
| 160 | ospf_if_new (struct interface *ifp, struct prefix *p) |
| 161 | { |
| 162 | struct ospf_interface *oi; |
| 163 | |
| 164 | oi = XCALLOC (MTYPE_OSPF_IF, sizeof (struct ospf_interface)); |
| 165 | memset (oi, 0, sizeof (struct ospf_interface)); |
| 166 | |
| 167 | /* Set zebra interface pointer. */ |
| 168 | oi->ifp = ifp; |
| 169 | oi->address = p; |
| 170 | |
| 171 | ospf_add_to_if (ifp, oi); |
| 172 | listnode_add (ospf_top->oiflist, oi); |
| 173 | |
| 174 | /* Clear self-originated network-LSA. */ |
| 175 | oi->network_lsa_self = NULL; |
| 176 | |
| 177 | /* Initialize neighbor list. */ |
| 178 | oi->nbrs = route_table_init (); |
| 179 | |
| 180 | /* Initialize static neighbor list. */ |
| 181 | oi->nbr_nbma = list_new (); |
| 182 | |
| 183 | /* Initialize Link State Acknowledgment list. */ |
| 184 | oi->ls_ack = list_new (); |
| 185 | oi->ls_ack_direct.ls_ack = list_new (); |
| 186 | |
| 187 | /* Set default values. */ |
| 188 | ospf_if_reset_variables (oi); |
| 189 | |
| 190 | /* Add pseudo neighbor. */ |
| 191 | oi->nbr_self = ospf_nbr_new (oi); |
| 192 | oi->nbr_self->state = NSM_TwoWay; |
| 193 | /* oi->nbr_self->router_id = ospf_top->router_id; */ |
| 194 | oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority); |
| 195 | oi->nbr_self->options = OSPF_OPTION_E; |
| 196 | |
| 197 | oi->ls_upd_queue = route_table_init (); |
| 198 | oi->t_ls_upd_event = NULL; |
| 199 | oi->t_ls_ack_direct = NULL; |
| 200 | |
| 201 | #ifdef HAVE_OPAQUE_LSA |
| 202 | ospf_opaque_type9_lsa_init (oi); |
| 203 | #endif /* HAVE_OPAQUE_LSA */ |
| 204 | |
| 205 | oi->ospf = ospf_top; |
| 206 | |
| 207 | return oi; |
| 208 | } |
| 209 | |
| 210 | /* Restore an interface to its pre UP state |
| 211 | Used from ism_interface_down only */ |
| 212 | void |
| 213 | ospf_if_cleanup (struct ospf_interface *oi) |
| 214 | { |
| 215 | struct route_node *rn; |
| 216 | listnode node; |
| 217 | struct ospf_neighbor *nbr; |
| 218 | |
| 219 | /* oi->nbrs and oi->nbr_nbma should be deletete on InterafceDown event */ |
| 220 | /* delete all static neighbors attached to this interface */ |
| 221 | for (node = listhead (oi->nbr_nbma); node; ) |
| 222 | { |
| 223 | struct ospf_nbr_nbma *nbr_nbma = getdata (node); |
| 224 | nextnode (node); |
| 225 | |
| 226 | OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll); |
| 227 | |
| 228 | if (nbr_nbma->nbr) |
| 229 | { |
| 230 | nbr_nbma->nbr->nbr_nbma = NULL; |
| 231 | nbr_nbma->nbr = NULL; |
| 232 | } |
| 233 | |
| 234 | nbr_nbma->oi = NULL; |
| 235 | |
| 236 | listnode_delete (oi->nbr_nbma, nbr_nbma); |
| 237 | } |
| 238 | |
| 239 | /* send Neighbor event KillNbr to all associated neighbors. */ |
| 240 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 241 | if ((nbr = rn->info) != NULL) |
| 242 | if (nbr != oi->nbr_self) |
| 243 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr); |
| 244 | |
| 245 | /* Cleanup Link State Acknowlegdment list. */ |
| 246 | for (node = listhead (oi->ls_ack); node; nextnode (node)) |
| 247 | ospf_lsa_unlock (node->data); |
| 248 | list_delete_all_node (oi->ls_ack); |
| 249 | |
| 250 | oi->crypt_seqnum = 0; |
| 251 | |
| 252 | /* Empty link state update queue */ |
| 253 | ospf_ls_upd_queue_empty (oi); |
| 254 | |
| 255 | /* Handle pseudo neighbor. */ |
| 256 | ospf_nbr_delete (oi->nbr_self); |
| 257 | oi->nbr_self = ospf_nbr_new (oi); |
| 258 | oi->nbr_self->state = NSM_TwoWay; |
| 259 | oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority); |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 260 | |
| 261 | switch (oi->area->external_routing) |
| 262 | { |
| 263 | case OSPF_AREA_DEFAULT: |
| 264 | SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 265 | break; |
| 266 | case OSPF_AREA_STUB: |
| 267 | UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 268 | break; |
| 269 | #ifdef HAVE_NSSA |
| 270 | case OSPF_AREA_NSSA: |
| 271 | UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 272 | SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP); |
| 273 | break; |
| 274 | #endif /* HAVE_NSSA */ |
| 275 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 276 | |
| 277 | ospf_lsa_unlock (oi->network_lsa_self); |
| 278 | oi->network_lsa_self = NULL; |
| 279 | OSPF_TIMER_OFF (oi->t_network_lsa_self); |
| 280 | } |
| 281 | |
| 282 | void |
| 283 | ospf_if_free (struct ospf_interface *oi) |
| 284 | { |
| 285 | ospf_if_down (oi); |
| 286 | |
| 287 | assert (oi->state == ISM_Down); |
| 288 | |
| 289 | #ifdef HAVE_OPAQUE_LSA |
| 290 | ospf_opaque_type9_lsa_term (oi); |
| 291 | #endif /* HAVE_OPAQUE_LSA */ |
| 292 | |
| 293 | /* Free Pseudo Neighbour */ |
| 294 | ospf_nbr_delete (oi->nbr_self); |
| 295 | |
| 296 | route_table_finish (oi->nbrs); |
| 297 | route_table_finish (oi->ls_upd_queue); |
| 298 | |
| 299 | /* Free any lists that should be freed */ |
| 300 | list_free (oi->nbr_nbma); |
| 301 | |
| 302 | list_free (oi->ls_ack); |
| 303 | list_free (oi->ls_ack_direct.ls_ack); |
| 304 | |
| 305 | ospf_delete_from_if (oi->ifp, oi); |
| 306 | |
| 307 | listnode_delete (ospf_top->oiflist, oi); |
| 308 | listnode_delete (oi->area->oiflist, oi); |
| 309 | |
| 310 | memset (oi, 0, sizeof (*oi)); |
| 311 | XFREE (MTYPE_OSPF_IF, oi); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /* |
| 316 | * check if interface with given address is configured and |
| 317 | * return it if yes. |
| 318 | */ |
| 319 | struct ospf_interface * |
| 320 | ospf_if_is_configured (struct in_addr *address) |
| 321 | { |
| 322 | listnode node; |
| 323 | struct ospf_interface *oi; |
| 324 | struct prefix *addr; |
| 325 | |
| 326 | for (node = listhead (ospf_top->oiflist); node; nextnode (node)) |
| 327 | if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 328 | { |
| 329 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 330 | addr = oi->connected->destination; |
| 331 | else |
| 332 | addr = oi->address; |
| 333 | |
| 334 | if (IPV4_ADDR_SAME (address, &addr->u.prefix4)) |
| 335 | return oi; |
| 336 | } |
| 337 | |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | int |
| 342 | ospf_if_is_up (struct ospf_interface *oi) |
| 343 | { |
| 344 | return if_is_up (oi->ifp); |
| 345 | } |
| 346 | |
| 347 | struct ospf_interface * |
| 348 | ospf_if_lookup_by_local_addr (struct interface *ifp, struct in_addr address) |
| 349 | { |
| 350 | listnode node; |
| 351 | struct ospf_interface *oi; |
| 352 | |
| 353 | for (node = listhead (ospf_top->oiflist); node; nextnode (node)) |
| 354 | if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 355 | { |
| 356 | if (ifp && oi->ifp != ifp) |
| 357 | continue; |
| 358 | |
| 359 | if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4)) |
| 360 | return oi; |
| 361 | } |
| 362 | |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | struct ospf_interface * |
| 367 | ospf_if_lookup_by_prefix (struct prefix_ipv4 *p) |
| 368 | { |
| 369 | listnode node; |
| 370 | struct ospf_interface *oi; |
| 371 | struct prefix ptmp; |
| 372 | |
| 373 | /* Check each Interface. */ |
| 374 | for (node = listhead (ospf_top->oiflist); node; nextnode (node)) { |
| 375 | if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 376 | { |
| 377 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) { |
| 378 | prefix_copy (&ptmp, oi->connected->destination); |
| 379 | ptmp.prefixlen = IPV4_MAX_BITLEN; |
| 380 | } |
| 381 | else |
| 382 | prefix_copy (&ptmp, oi->address); |
| 383 | |
| 384 | apply_mask (&ptmp); |
| 385 | if (prefix_same (&ptmp, (struct prefix *) p)) |
| 386 | return oi; |
| 387 | } |
| 388 | } |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | /* determine receiving interface by source of packet */ |
| 393 | struct ospf_interface * |
| 394 | ospf_if_lookup_recv_interface (struct in_addr src) |
| 395 | { |
| 396 | listnode node; |
| 397 | struct prefix_ipv4 addr; |
| 398 | struct ospf_interface *oi, *match; |
| 399 | |
| 400 | addr.family = AF_INET; |
| 401 | addr.prefix = src; |
| 402 | addr.prefixlen = IPV4_MAX_BITLEN; |
| 403 | |
| 404 | match = NULL; |
| 405 | |
| 406 | for (node = listhead (ospf_top->oiflist); node; nextnode (node)) |
| 407 | { |
| 408 | oi = getdata (node); |
| 409 | |
| 410 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 411 | continue; |
| 412 | |
| 413 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 414 | { |
| 415 | if (IPV4_ADDR_SAME (&oi->connected->destination->u.prefix4, &src)) |
| 416 | return oi; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | if (prefix_match (oi->address, (struct prefix *) &addr)) |
| 421 | match = oi; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return match; |
| 426 | } |
| 427 | |
| 428 | void |
| 429 | ospf_if_stream_set (struct ospf_interface *oi) |
| 430 | { |
| 431 | /* set output fifo queue. */ |
| 432 | if (oi->obuf == NULL) |
| 433 | oi->obuf = ospf_fifo_new (); |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | ospf_if_stream_unset (struct ospf_interface *oi) |
| 438 | { |
| 439 | if (oi->obuf) |
| 440 | { |
| 441 | ospf_fifo_free (oi->obuf); |
| 442 | oi->obuf = NULL; |
| 443 | |
| 444 | if (oi->on_write_q) |
| 445 | { |
| 446 | listnode_delete (ospf_top->oi_write_q, oi); |
| 447 | if (list_isempty(ospf_top->oi_write_q)) |
| 448 | OSPF_TIMER_OFF (ospf_top->t_write); |
| 449 | oi->on_write_q = 0; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | struct ospf_if_params * |
| 455 | ospf_new_if_params () |
| 456 | { |
| 457 | struct ospf_if_params *oip; |
| 458 | |
| 459 | oip = XMALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params)); |
| 460 | memset (oip, 0, sizeof (struct ospf_if_params)); |
| 461 | |
| 462 | if (!oip) |
| 463 | return NULL; |
| 464 | |
| 465 | memset (oip, 0, sizeof (struct ospf_if_params)); |
| 466 | |
| 467 | UNSET_IF_PARAM (oip, output_cost_cmd); |
| 468 | UNSET_IF_PARAM (oip, transmit_delay); |
| 469 | UNSET_IF_PARAM (oip, retransmit_interval); |
| 470 | UNSET_IF_PARAM (oip, passive_interface); |
| 471 | UNSET_IF_PARAM (oip, v_hello); |
| 472 | UNSET_IF_PARAM (oip, v_wait); |
| 473 | UNSET_IF_PARAM (oip, priority); |
| 474 | UNSET_IF_PARAM (oip, type); |
| 475 | UNSET_IF_PARAM (oip, auth_simple); |
| 476 | UNSET_IF_PARAM (oip, auth_crypt); |
| 477 | UNSET_IF_PARAM (oip, auth_type); |
| 478 | |
| 479 | oip->auth_crypt = list_new (); |
| 480 | |
| 481 | return oip; |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | ospf_del_if_params (struct ospf_if_params *oip) |
| 486 | { |
| 487 | list_delete (oip->auth_crypt); |
| 488 | XFREE (MTYPE_OSPF_IF_PARAMS, oip); |
| 489 | } |
| 490 | |
| 491 | void |
| 492 | ospf_free_if_params (struct interface *ifp, struct in_addr addr) |
| 493 | { |
| 494 | struct ospf_if_params *oip; |
| 495 | struct prefix_ipv4 p; |
| 496 | struct route_node *rn; |
| 497 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 498 | p.prefix = addr; |
| 499 | rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p); |
| 500 | if (!rn || !rn->info) |
| 501 | return; |
| 502 | |
| 503 | oip = rn->info; |
| 504 | route_unlock_node (rn); |
| 505 | |
| 506 | if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) && |
| 507 | !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) && |
| 508 | !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) && |
| 509 | !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) && |
| 510 | !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) && |
| 511 | !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) && |
| 512 | !OSPF_IF_PARAM_CONFIGURED (oip, priority) && |
| 513 | !OSPF_IF_PARAM_CONFIGURED (oip, type) && |
| 514 | !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) && |
| 515 | !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) && |
| 516 | listcount (oip->auth_crypt) == 0) |
| 517 | { |
| 518 | ospf_del_if_params (oip); |
| 519 | rn->info = NULL; |
| 520 | route_unlock_node (rn); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | struct ospf_if_params * |
| 525 | ospf_lookup_if_params (struct interface *ifp, struct in_addr addr) |
| 526 | { |
| 527 | struct prefix_ipv4 p; |
| 528 | struct route_node *rn; |
| 529 | |
| 530 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 531 | p.prefix = addr; |
| 532 | |
| 533 | rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p); |
| 534 | |
| 535 | if (rn) |
| 536 | { |
| 537 | route_unlock_node (rn); |
| 538 | return rn->info; |
| 539 | } |
| 540 | |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | struct ospf_if_params * |
| 545 | ospf_get_if_params (struct interface *ifp, struct in_addr addr) |
| 546 | { |
| 547 | struct prefix_ipv4 p; |
| 548 | struct route_node *rn; |
| 549 | |
| 550 | p.family = AF_INET; |
| 551 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 552 | p.prefix = addr; |
| 553 | |
| 554 | rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p); |
| 555 | |
| 556 | if (rn->info == NULL) |
| 557 | rn->info = ospf_new_if_params (); |
| 558 | else |
| 559 | route_unlock_node (rn); |
| 560 | |
| 561 | return rn->info; |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | ospf_if_update_params (struct interface *ifp, struct in_addr addr) |
| 566 | { |
| 567 | struct route_node *rn; |
| 568 | struct ospf_interface *oi; |
| 569 | |
| 570 | for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn)) |
| 571 | { |
| 572 | if ((oi = rn->info) == NULL) |
| 573 | continue; |
| 574 | |
| 575 | if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr)) |
| 576 | oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | int |
| 581 | ospf_if_new_hook (struct interface *ifp) |
| 582 | { |
| 583 | int rc = 0; |
| 584 | |
| 585 | ifp->info = XMALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info)); |
| 586 | memset (ifp->info, 0, sizeof (struct ospf_if_info)); |
| 587 | |
| 588 | IF_OIFS (ifp) = route_table_init (); |
| 589 | IF_OIFS_PARAMS (ifp) = route_table_init (); |
| 590 | |
| 591 | IF_DEF_PARAMS (ifp) = ospf_new_if_params (); |
| 592 | |
| 593 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay); |
| 594 | IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT; |
| 595 | |
| 596 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval); |
| 597 | IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT; |
| 598 | |
| 599 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority); |
| 600 | IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT; |
| 601 | |
| 602 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), passive_interface); |
| 603 | IF_DEF_PARAMS (ifp)->passive_interface = OSPF_IF_ACTIVE; |
| 604 | |
| 605 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello); |
| 606 | IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT; |
| 607 | |
| 608 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait); |
| 609 | IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT; |
| 610 | |
| 611 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple); |
| 612 | memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE); |
| 613 | |
| 614 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_crypt); |
| 615 | IF_DEF_PARAMS (ifp)->auth_crypt = list_new (); |
| 616 | |
| 617 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type); |
| 618 | IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET; |
| 619 | |
| 620 | #ifdef HAVE_OPAQUE_LSA |
| 621 | rc = ospf_opaque_new_if (ifp); |
| 622 | #endif /* HAVE_OPAQUE_LSA */ |
| 623 | return rc; |
| 624 | } |
| 625 | |
| 626 | int |
| 627 | ospf_if_delete_hook (struct interface *ifp) |
| 628 | { |
| 629 | int rc = 0; |
| 630 | #ifdef HAVE_OPAQUE_LSA |
| 631 | rc = ospf_opaque_del_if (ifp); |
| 632 | #endif /* HAVE_OPAQUE_LSA */ |
| 633 | route_table_finish (IF_OIFS (ifp)); |
| 634 | route_table_finish (IF_OIFS_PARAMS (ifp)); |
| 635 | XFREE (MTYPE_OSPF_IF_INFO, ifp->info); |
| 636 | ifp->info = NULL; |
| 637 | |
| 638 | return rc; |
| 639 | } |
| 640 | |
| 641 | int |
| 642 | ospf_if_is_enable (struct ospf_interface *oi) |
| 643 | { |
| 644 | if (!if_is_loopback (oi->ifp)) |
| 645 | if (if_is_up (oi->ifp)) |
| 646 | return 1; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | int |
| 652 | ospf_if_up (struct ospf_interface *oi) |
| 653 | { |
| 654 | if (oi == NULL) |
| 655 | return 0; |
| 656 | |
| 657 | if (oi->type == OSPF_IFTYPE_LOOPBACK) |
| 658 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd); |
| 659 | else |
| 660 | { |
| 661 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 662 | ospf_if_add_allspfrouters (ospf_top, oi->address, oi->ifp->ifindex); |
| 663 | ospf_if_stream_set (oi); |
| 664 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp); |
| 665 | } |
| 666 | |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | int |
| 671 | ospf_if_down (struct ospf_interface *oi) |
| 672 | { |
| 673 | if (oi == NULL) |
| 674 | return 0; |
| 675 | |
| 676 | OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown); |
| 677 | /* Shutdown packet reception and sending */ |
| 678 | ospf_if_stream_unset (oi); |
| 679 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 680 | ospf_if_drop_allspfrouters (ospf_top, oi->address, oi->ifp->ifindex); |
| 681 | |
| 682 | |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | /* Virtual Link related functions. */ |
| 688 | |
| 689 | struct ospf_vl_data * |
| 690 | ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer) |
| 691 | { |
| 692 | struct ospf_vl_data *vl_data; |
| 693 | |
| 694 | vl_data = XMALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data)); |
| 695 | memset (vl_data, 0, sizeof (struct ospf_vl_data)); |
| 696 | |
| 697 | vl_data->vl_peer.s_addr = vl_peer.s_addr; |
| 698 | vl_data->vl_area_id = area->area_id; |
| 699 | vl_data->format = area->format; |
| 700 | |
| 701 | return vl_data; |
| 702 | } |
| 703 | |
| 704 | void |
| 705 | ospf_vl_data_free (struct ospf_vl_data *vl_data) |
| 706 | { |
| 707 | XFREE (MTYPE_OSPF_VL_DATA, vl_data); |
| 708 | } |
| 709 | |
| 710 | u_int vlink_count = 0; |
| 711 | |
| 712 | struct ospf_interface * |
| 713 | ospf_vl_new (struct ospf_vl_data *vl_data) |
| 714 | { |
| 715 | struct ospf_interface * voi; |
| 716 | struct interface * vi; |
| 717 | char ifname[INTERFACE_NAMSIZ + 1]; |
| 718 | struct ospf_area *area; |
| 719 | struct in_addr area_id; |
| 720 | struct connected *co; |
| 721 | struct prefix_ipv4 *p; |
| 722 | |
| 723 | if (IS_DEBUG_OSPF_EVENT) |
| 724 | zlog_info ("ospf_vl_new(): Start"); |
| 725 | if (vlink_count == OSPF_VL_MAX_COUNT) |
| 726 | { |
| 727 | if (IS_DEBUG_OSPF_EVENT) |
| 728 | zlog_info ("ospf_vl_new(): Alarm: " |
| 729 | "cannot create more than OSPF_MAX_VL_COUNT virtual links"); |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | if (IS_DEBUG_OSPF_EVENT) |
| 734 | zlog_info ("ospf_vl_new(): creating pseudo zebra interface"); |
| 735 | |
| 736 | vi = if_create (); |
| 737 | co = connected_new (); |
| 738 | co->ifp = vi; |
| 739 | listnode_add (vi->connected, co); |
| 740 | |
| 741 | p = prefix_ipv4_new (); |
| 742 | p->family = AF_INET; |
| 743 | p->prefix.s_addr = 0; |
| 744 | p->prefixlen = 0; |
| 745 | |
| 746 | co->address = (struct prefix *)p; |
| 747 | |
| 748 | voi = ospf_if_new (vi, co->address); |
| 749 | if (voi == NULL) |
| 750 | { |
| 751 | if (IS_DEBUG_OSPF_EVENT) |
| 752 | zlog_info ("ospf_vl_new(): Alarm: OSPF int structure is not created"); |
| 753 | return NULL; |
| 754 | } |
| 755 | voi->connected = co; |
| 756 | voi->vl_data = vl_data; |
| 757 | voi->ifp->mtu = OSPF_VL_MTU; |
| 758 | voi->type = OSPF_IFTYPE_VIRTUALLINK; |
| 759 | |
| 760 | sprintf (ifname, "VLINK%d", vlink_count++); |
| 761 | if (IS_DEBUG_OSPF_EVENT) |
| 762 | zlog_info ("ospf_vl_new(): Created name: %s", ifname); |
| 763 | strncpy (vi->name, ifname, IFNAMSIZ); |
| 764 | if (IS_DEBUG_OSPF_EVENT) |
| 765 | zlog_info ("ospf_vl_new(): set if->name to %s", vi->name); |
| 766 | |
| 767 | area_id.s_addr = 0; |
| 768 | area = ospf_area_get (area_id, OSPF_AREA_ID_FORMAT_ADDRESS); |
| 769 | voi->area = area; |
| 770 | |
| 771 | if (IS_DEBUG_OSPF_EVENT) |
| 772 | zlog_info ("ospf_vl_new(): set associated area to the backbone"); |
| 773 | |
| 774 | ospf_area_add_if (voi->area, voi); |
| 775 | |
| 776 | ospf_if_stream_set (voi); |
| 777 | |
| 778 | if (IS_DEBUG_OSPF_EVENT) |
| 779 | zlog_info ("ospf_vl_new(): Stop"); |
| 780 | return voi; |
| 781 | } |
| 782 | |
| 783 | void |
| 784 | ospf_vl_if_delete (struct ospf_vl_data *vl_data) |
| 785 | { |
| 786 | struct interface *ifp = vl_data->vl_oi->ifp; |
| 787 | vl_data->vl_oi->address->u.prefix4.s_addr = 0; |
| 788 | vl_data->vl_oi->address->prefixlen = 0; |
| 789 | ospf_if_free (vl_data->vl_oi); |
| 790 | if_delete (ifp); |
| 791 | vlink_count--; |
| 792 | } |
| 793 | |
| 794 | struct ospf_vl_data * |
| 795 | ospf_vl_lookup (struct ospf_area *area, struct in_addr vl_peer) |
| 796 | { |
| 797 | struct ospf_vl_data *vl_data; |
| 798 | listnode node; |
| 799 | |
| 800 | for (node = listhead (ospf_top->vlinks); node; nextnode (node)) |
| 801 | if ((vl_data = getdata (node)) != NULL) |
| 802 | if (vl_data->vl_peer.s_addr == vl_peer.s_addr && |
| 803 | IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) |
| 804 | return vl_data; |
| 805 | |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | void |
| 810 | ospf_vl_shutdown (struct ospf_vl_data *vl_data) |
| 811 | { |
| 812 | struct ospf_interface *oi; |
| 813 | |
| 814 | if ((oi = vl_data->vl_oi) == NULL) |
| 815 | return; |
| 816 | |
| 817 | oi->address->u.prefix4.s_addr = 0; |
| 818 | oi->address->prefixlen = 0; |
| 819 | |
| 820 | UNSET_FLAG (oi->ifp->flags, IFF_UP); |
| 821 | /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */ |
| 822 | OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown); |
| 823 | } |
| 824 | |
| 825 | void |
| 826 | ospf_vl_add (struct ospf_vl_data *vl_data) |
| 827 | { |
| 828 | listnode_add (ospf_top->vlinks, vl_data); |
| 829 | #ifdef HAVE_SNMP |
| 830 | ospf_snmp_vl_add (vl_data); |
| 831 | #endif /* HAVE_SNMP */ |
| 832 | } |
| 833 | |
| 834 | void |
| 835 | ospf_vl_delete (struct ospf_vl_data *vl_data) |
| 836 | { |
| 837 | ospf_vl_shutdown (vl_data); |
| 838 | ospf_vl_if_delete (vl_data); |
| 839 | |
| 840 | #ifdef HAVE_SNMP |
| 841 | ospf_snmp_vl_delete (vl_data); |
| 842 | #endif /* HAVE_SNMP */ |
| 843 | listnode_delete (ospf_top->vlinks, vl_data); |
| 844 | |
| 845 | ospf_vl_data_free (vl_data); |
| 846 | } |
| 847 | |
| 848 | void |
| 849 | ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v) |
| 850 | { |
| 851 | int changed = 0; |
| 852 | struct ospf_interface *voi; |
| 853 | listnode node; |
| 854 | struct vertex_nexthop *nh; |
| 855 | int i; |
| 856 | struct router_lsa *rl; |
| 857 | |
| 858 | voi = vl_data->vl_oi; |
| 859 | |
| 860 | if (voi->output_cost != v->distance) |
| 861 | { |
| 862 | voi->output_cost = v->distance; |
| 863 | changed = 1; |
| 864 | } |
| 865 | |
| 866 | for (node = listhead (v->nexthop); node; nextnode (node)) |
| 867 | if ((nh = getdata (node)) != NULL) |
| 868 | { |
| 869 | vl_data->out_oi = (struct ospf_interface *) nh->oi; |
| 870 | |
| 871 | voi->address->u.prefix4 = vl_data->out_oi->address->u.prefix4; |
| 872 | voi->address->prefixlen = vl_data->out_oi->address->prefixlen; |
| 873 | |
| 874 | break; /* We take the first interface. */ |
| 875 | } |
| 876 | |
| 877 | rl = (struct router_lsa *)v->lsa; |
| 878 | |
| 879 | for (i = 0; i < ntohs (rl->links); i++) |
| 880 | { |
| 881 | switch (rl->link[i].type) |
| 882 | { |
| 883 | case LSA_LINK_TYPE_VIRTUALLINK: |
| 884 | if (IS_DEBUG_OSPF_EVENT) |
| 885 | zlog_info ("found back link through VL"); |
| 886 | case LSA_LINK_TYPE_TRANSIT: |
| 887 | case LSA_LINK_TYPE_POINTOPOINT: |
| 888 | vl_data->peer_addr = rl->link[i].link_data; |
| 889 | if (IS_DEBUG_OSPF_EVENT) |
| 890 | zlog_info ("%s peer address is %s\n", |
| 891 | vl_data->vl_oi->ifp->name, inet_ntoa(vl_data->peer_addr)); |
| 892 | return; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | |
| 898 | void |
| 899 | ospf_vl_up_check (struct ospf_area * area, struct in_addr rid, |
| 900 | struct vertex *v) |
| 901 | { |
| 902 | listnode node; |
| 903 | struct ospf_vl_data *vl_data; |
| 904 | struct ospf_interface *oi; |
| 905 | |
| 906 | if (IS_DEBUG_OSPF_EVENT) |
| 907 | { |
| 908 | zlog_info ("ospf_vl_up_check(): Start"); |
| 909 | zlog_info ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid)); |
| 910 | zlog_info ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id)); |
| 911 | } |
| 912 | |
| 913 | for (node = listhead (ospf_top->vlinks); node; nextnode (node)) |
| 914 | { |
| 915 | if ((vl_data = getdata (node)) == NULL) |
| 916 | continue; |
| 917 | |
| 918 | if (IS_DEBUG_OSPF_EVENT) |
| 919 | { |
| 920 | zlog_info ("ospf_vl_up_check(): considering VL, name: %s", |
| 921 | vl_data->vl_oi->ifp->name); |
| 922 | zlog_info ("ospf_vl_up_check(): VL area: %s, peer ID: %s", |
| 923 | inet_ntoa (vl_data->vl_area_id), |
| 924 | inet_ntoa (vl_data->vl_peer)); |
| 925 | } |
| 926 | |
| 927 | if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) && |
| 928 | IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) |
| 929 | { |
| 930 | oi = vl_data->vl_oi; |
| 931 | SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED); |
| 932 | |
| 933 | if (IS_DEBUG_OSPF_EVENT) |
| 934 | zlog_info ("ospf_vl_up_check(): this VL matched"); |
| 935 | |
| 936 | if (oi->state == ISM_Down) |
| 937 | { |
| 938 | if (IS_DEBUG_OSPF_EVENT) |
| 939 | zlog_info ("ospf_vl_up_check(): VL is down, waking it up"); |
| 940 | SET_FLAG (oi->ifp->flags, IFF_UP); |
| 941 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp); |
| 942 | } |
| 943 | |
| 944 | ospf_vl_set_params (vl_data, v); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void |
| 950 | ospf_vl_unapprove () |
| 951 | { |
| 952 | listnode node; |
| 953 | struct ospf_vl_data *vl_data; |
| 954 | |
| 955 | for (node = listhead (ospf_top->vlinks); node; nextnode (node)) |
| 956 | if ((vl_data = getdata (node)) != NULL) |
| 957 | UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED); |
| 958 | } |
| 959 | |
| 960 | void |
| 961 | ospf_vl_shut_unapproved () |
| 962 | { |
| 963 | listnode node; |
| 964 | struct ospf_vl_data *vl_data; |
| 965 | |
| 966 | for (node = listhead (ospf_top->vlinks); node; nextnode (node)) |
| 967 | if ((vl_data = getdata (node)) != NULL) |
| 968 | if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED)) |
| 969 | ospf_vl_shutdown (vl_data); |
| 970 | } |
| 971 | |
| 972 | int |
| 973 | ospf_full_virtual_nbrs (struct ospf_area *area) |
| 974 | { |
| 975 | if (IS_DEBUG_OSPF_EVENT) |
| 976 | { |
| 977 | zlog_info ("counting fully adjacent virtual neighbors in area %s", |
| 978 | inet_ntoa (area->area_id)); |
| 979 | zlog_info ("there are %d of them", area->full_vls); |
| 980 | } |
| 981 | |
| 982 | return area->full_vls; |
| 983 | } |
| 984 | |
| 985 | int |
| 986 | ospf_vls_in_area (struct ospf_area *area) |
| 987 | { |
| 988 | listnode node; |
| 989 | struct ospf_vl_data *vl_data; |
| 990 | int c = 0; |
| 991 | |
| 992 | for (node = listhead (ospf_top->vlinks); node; nextnode (node)) |
| 993 | if ((vl_data = getdata (node)) != NULL) |
| 994 | if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) |
| 995 | c++; |
| 996 | |
| 997 | return c; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | struct crypt_key * |
| 1002 | ospf_crypt_key_new () |
| 1003 | { |
| 1004 | struct crypt_key *ck; |
| 1005 | |
| 1006 | ck = XMALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key)); |
| 1007 | memset (ck, 0, sizeof (struct crypt_key)); |
| 1008 | |
| 1009 | return ck; |
| 1010 | } |
| 1011 | |
| 1012 | void |
| 1013 | ospf_crypt_key_add (list crypt, struct crypt_key *ck) |
| 1014 | { |
| 1015 | listnode_add (crypt, ck); |
| 1016 | } |
| 1017 | |
| 1018 | struct crypt_key * |
| 1019 | ospf_crypt_key_lookup (list auth_crypt, u_char key_id) |
| 1020 | { |
| 1021 | listnode node; |
| 1022 | struct crypt_key *ck; |
| 1023 | |
| 1024 | for (node = listhead (auth_crypt); node; nextnode (node)) |
| 1025 | { |
| 1026 | ck = getdata (node); |
| 1027 | if (ck->key_id == key_id) |
| 1028 | return ck; |
| 1029 | } |
| 1030 | |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | int |
| 1035 | ospf_crypt_key_delete (list auth_crypt, u_char key_id) |
| 1036 | { |
| 1037 | listnode node; |
| 1038 | struct crypt_key *ck; |
| 1039 | |
| 1040 | for (node = listhead (auth_crypt); node; nextnode (node)) |
| 1041 | { |
| 1042 | ck = getdata (node); |
| 1043 | if (ck->key_id == key_id) |
| 1044 | { |
| 1045 | listnode_delete (auth_crypt, ck); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | void |
| 1054 | ospf_if_init () |
| 1055 | { |
| 1056 | /* Initialize Zebra interface data structure. */ |
| 1057 | if_init (); |
| 1058 | if_add_hook (IF_NEW_HOOK, ospf_if_new_hook); |
| 1059 | if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook); |
| 1060 | } |