paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF Neighbor 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 "linklist.h" |
| 26 | #include "prefix.h" |
| 27 | #include "memory.h" |
| 28 | #include "command.h" |
| 29 | #include "thread.h" |
| 30 | #include "stream.h" |
| 31 | #include "table.h" |
| 32 | #include "log.h" |
| 33 | |
| 34 | #include "ospfd/ospfd.h" |
| 35 | #include "ospfd/ospf_interface.h" |
| 36 | #include "ospfd/ospf_asbr.h" |
| 37 | #include "ospfd/ospf_lsa.h" |
| 38 | #include "ospfd/ospf_lsdb.h" |
| 39 | #include "ospfd/ospf_neighbor.h" |
| 40 | #include "ospfd/ospf_nsm.h" |
| 41 | #include "ospfd/ospf_packet.h" |
| 42 | #include "ospfd/ospf_network.h" |
| 43 | #include "ospfd/ospf_flood.h" |
| 44 | #include "ospfd/ospf_dump.h" |
| 45 | |
| 46 | struct ospf_neighbor * |
| 47 | ospf_nbr_new (struct ospf_interface *oi) |
| 48 | { |
| 49 | struct ospf_neighbor *nbr; |
| 50 | |
| 51 | /* Allcate new neighbor. */ |
| 52 | nbr = XMALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor)); |
| 53 | memset (nbr, 0, sizeof (struct ospf_neighbor)); |
| 54 | |
| 55 | /* Relate neighbor to the interface. */ |
| 56 | nbr->oi = oi; |
| 57 | |
| 58 | /* Set default values. */ |
| 59 | nbr->state = NSM_Down; |
| 60 | |
| 61 | /* Set inheritance values. */ |
| 62 | nbr->v_inactivity = OSPF_IF_PARAM (oi, v_wait); |
| 63 | nbr->v_db_desc = OSPF_IF_PARAM (oi, retransmit_interval); |
| 64 | nbr->v_ls_req = OSPF_IF_PARAM (oi, retransmit_interval); |
| 65 | nbr->v_ls_upd = OSPF_IF_PARAM (oi, retransmit_interval); |
| 66 | nbr->priority = -1; |
| 67 | |
| 68 | /* DD flags. */ |
| 69 | nbr->dd_flags = OSPF_DD_FLAG_MS|OSPF_DD_FLAG_M|OSPF_DD_FLAG_I; |
| 70 | |
| 71 | /* Last received and sent DD. */ |
| 72 | nbr->last_send = NULL; |
| 73 | |
| 74 | nbr->nbr_nbma = NULL; |
| 75 | |
| 76 | ospf_lsdb_init (&nbr->db_sum); |
| 77 | ospf_lsdb_init (&nbr->ls_rxmt); |
| 78 | ospf_lsdb_init (&nbr->ls_req); |
| 79 | |
| 80 | nbr->crypt_seqnum = 0; |
| 81 | |
| 82 | return nbr; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | ospf_nbr_free (struct ospf_neighbor *nbr) |
| 87 | { |
| 88 | /* Free DB summary list. */ |
| 89 | if (ospf_db_summary_count (nbr)) |
| 90 | ospf_db_summary_clear (nbr); |
| 91 | /* ospf_db_summary_delete_all (nbr); */ |
| 92 | |
| 93 | /* Free ls request list. */ |
| 94 | if (ospf_ls_request_count (nbr)) |
| 95 | ospf_ls_request_delete_all (nbr); |
| 96 | |
| 97 | /* Free retransmit list. */ |
| 98 | if (ospf_ls_retransmit_count (nbr)) |
| 99 | ospf_ls_retransmit_clear (nbr); |
| 100 | |
| 101 | /* Cleanup LSDBs. */ |
| 102 | ospf_lsdb_cleanup (&nbr->db_sum); |
| 103 | ospf_lsdb_cleanup (&nbr->ls_req); |
| 104 | ospf_lsdb_cleanup (&nbr->ls_rxmt); |
| 105 | |
| 106 | /* Clear last send packet. */ |
| 107 | if (nbr->last_send) |
| 108 | ospf_packet_free (nbr->last_send); |
| 109 | |
| 110 | if (nbr->nbr_nbma) |
| 111 | { |
| 112 | nbr->nbr_nbma->nbr = NULL; |
| 113 | nbr->nbr_nbma = NULL; |
| 114 | } |
| 115 | |
| 116 | /* Cancel all timers. */ |
| 117 | OSPF_NSM_TIMER_OFF (nbr->t_inactivity); |
| 118 | OSPF_NSM_TIMER_OFF (nbr->t_db_desc); |
| 119 | OSPF_NSM_TIMER_OFF (nbr->t_ls_req); |
| 120 | OSPF_NSM_TIMER_OFF (nbr->t_ls_upd); |
| 121 | |
| 122 | /* Cancel all events. *//* Thread lookup cost would be negligible. */ |
| 123 | thread_cancel_event (master, nbr); |
| 124 | |
| 125 | XFREE (MTYPE_OSPF_NEIGHBOR, nbr); |
| 126 | } |
| 127 | |
| 128 | /* Delete specified OSPF neighbor from interface. */ |
| 129 | void |
| 130 | ospf_nbr_delete (struct ospf_neighbor *nbr) |
| 131 | { |
| 132 | struct ospf_interface *oi; |
| 133 | struct route_node *rn; |
| 134 | struct prefix p; |
| 135 | |
| 136 | oi = nbr->oi; |
| 137 | |
| 138 | /* Unlink ospf neighbor from the interface. */ |
| 139 | p.family = AF_INET; |
| 140 | p.prefixlen = IPV4_MAX_BITLEN; |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 141 | |
| 142 | /* vlinks are indexed by router-id */ |
| 143 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 144 | p.u.prefix4 = nbr->router_id; |
| 145 | else |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 146 | p.u.prefix4 = nbr->src; |
| 147 | |
| 148 | rn = route_node_lookup (oi->nbrs, &p); |
| 149 | if (rn) |
| 150 | { |
| 151 | if (rn->info) |
| 152 | { |
| 153 | rn->info = NULL; |
| 154 | route_unlock_node (rn); |
| 155 | } |
| 156 | else |
| 157 | zlog_info ("Can't find neighbor %s in the interface %s", |
| 158 | inet_ntoa (nbr->src), IF_NAME (oi)); |
| 159 | |
| 160 | route_unlock_node (rn); |
| 161 | } |
| 162 | |
| 163 | /* Free ospf_neighbor structure. */ |
| 164 | ospf_nbr_free (nbr); |
| 165 | } |
| 166 | |
| 167 | /* Check myself is in the neighbor list. */ |
| 168 | int |
| 169 | ospf_nbr_bidirectional (struct in_addr *router_id, |
| 170 | struct in_addr *neighbors, int size) |
| 171 | { |
| 172 | int i; |
| 173 | int max; |
| 174 | |
| 175 | max = size / sizeof (struct in_addr); |
| 176 | |
| 177 | for (i = 0; i < max; i ++) |
| 178 | if (IPV4_ADDR_SAME (router_id, &neighbors[i])) |
| 179 | return 1; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* Add self to nbr list. */ |
| 185 | void |
| 186 | ospf_nbr_add_self (struct ospf_interface *oi) |
| 187 | { |
| 188 | struct ospf_neighbor *nbr; |
| 189 | struct prefix p; |
| 190 | struct route_node *rn; |
| 191 | |
| 192 | p.family = AF_INET; |
| 193 | p.prefixlen = 32; |
| 194 | p.u.prefix4 = oi->address->u.prefix4; |
| 195 | |
| 196 | rn = route_node_get (oi->nbrs, &p); |
| 197 | if (rn->info) |
| 198 | { |
| 199 | /* There is already pseudo neighbor. */ |
| 200 | nbr = rn->info; |
| 201 | route_unlock_node (rn); |
| 202 | } |
| 203 | else |
| 204 | rn->info = oi->nbr_self; |
paul | 1a643f8 | 2006-01-11 01:08:19 +0000 | [diff] [blame] | 205 | |
| 206 | /* Initial state */ |
| 207 | oi->nbr_self->address = *oi->address; |
| 208 | oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority); |
| 209 | oi->nbr_self->router_id = oi->ospf->router_id; |
| 210 | oi->nbr_self->src = oi->address->u.prefix4; |
| 211 | oi->nbr_self->state = NSM_TwoWay; |
| 212 | |
| 213 | switch (oi->area->external_routing) |
| 214 | { |
| 215 | case OSPF_AREA_DEFAULT: |
| 216 | SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 217 | break; |
| 218 | case OSPF_AREA_STUB: |
| 219 | UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 220 | break; |
| 221 | case OSPF_AREA_NSSA: |
| 222 | UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E); |
| 223 | SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP); |
| 224 | break; |
| 225 | } |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* Get neighbor count by status. |
| 229 | Specify status = 0, get all neighbor other than myself. */ |
| 230 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 231 | ospf_nbr_count (struct ospf_interface *oi, int state) |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 232 | { |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 233 | struct ospf_neighbor *nbr; |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 234 | struct route_node *rn; |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 235 | int count = 0; |
| 236 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 237 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 238 | if ((nbr = rn->info)) |
| 239 | if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id)) |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 240 | if (state == 0 || nbr->state == state) |
| 241 | count++; |
| 242 | |
| 243 | return count; |
| 244 | } |
| 245 | |
| 246 | #ifdef HAVE_OPAQUE_LSA |
| 247 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 248 | ospf_nbr_count_opaque_capable (struct ospf_interface *oi) |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 249 | { |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 250 | struct ospf_neighbor *nbr; |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 251 | struct route_node *rn; |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 252 | int count = 0; |
| 253 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 254 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 255 | if ((nbr = rn->info)) |
| 256 | if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id)) |
| 257 | if (nbr->state == NSM_Full) |
| 258 | if (CHECK_FLAG (nbr->options, OSPF_OPTION_O)) |
| 259 | count++; |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 260 | |
| 261 | return count; |
| 262 | } |
| 263 | #endif /* HAVE_OPAQUE_LSA */ |
| 264 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 265 | /* lookup nbr by address - use this only if you know you must |
| 266 | * otherwise use the ospf_nbr_lookup() wrapper, which deals |
| 267 | * with virtual link neighbours |
| 268 | */ |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 269 | struct ospf_neighbor * |
| 270 | ospf_nbr_lookup_by_addr (struct route_table *nbrs, |
| 271 | struct in_addr *addr) |
| 272 | { |
| 273 | struct prefix p; |
| 274 | struct route_node *rn; |
| 275 | struct ospf_neighbor *nbr; |
| 276 | |
| 277 | p.family = AF_INET; |
| 278 | p.prefixlen = IPV4_MAX_BITLEN; |
| 279 | p.u.prefix4 = *addr; |
| 280 | |
| 281 | rn = route_node_lookup (nbrs, &p); |
| 282 | if (! rn) |
| 283 | return NULL; |
| 284 | |
| 285 | if (rn->info == NULL) |
| 286 | { |
| 287 | route_unlock_node (rn); |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | nbr = (struct ospf_neighbor *) rn->info; |
| 292 | route_unlock_node (rn); |
| 293 | |
| 294 | return nbr; |
| 295 | } |
| 296 | |
| 297 | struct ospf_neighbor * |
| 298 | ospf_nbr_lookup_by_routerid (struct route_table *nbrs, |
| 299 | struct in_addr *id) |
| 300 | { |
| 301 | struct route_node *rn; |
| 302 | struct ospf_neighbor *nbr; |
| 303 | |
| 304 | for (rn = route_top (nbrs); rn; rn = route_next (rn)) |
| 305 | if ((nbr = rn->info) != NULL) |
| 306 | if (IPV4_ADDR_SAME (&nbr->router_id, id)) |
| 307 | { |
| 308 | route_unlock_node(rn); |
| 309 | return nbr; |
| 310 | } |
| 311 | |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | ospf_renegotiate_optional_capabilities (struct ospf *top) |
| 317 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 318 | struct listnode *node; |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 319 | struct ospf_interface *oi; |
| 320 | struct route_table *nbrs; |
| 321 | struct route_node *rn; |
| 322 | struct ospf_neighbor *nbr; |
| 323 | |
| 324 | /* At first, flush self-originated LSAs from routing domain. */ |
| 325 | ospf_flush_self_originated_lsas_now (top); |
| 326 | |
| 327 | /* Revert all neighbor status to ExStart. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 328 | for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi)) |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 329 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 330 | if ((nbrs = oi->nbrs) == NULL) |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 331 | continue; |
| 332 | |
| 333 | for (rn = route_top (nbrs); rn; rn = route_next (rn)) |
| 334 | { |
| 335 | if ((nbr = rn->info) == NULL || nbr == oi->nbr_self) |
| 336 | continue; |
| 337 | |
| 338 | if (nbr->state < NSM_ExStart) |
| 339 | continue; |
| 340 | |
| 341 | if (IS_DEBUG_OSPF_EVENT) |
ajs | e588f21 | 2004-12-08 18:12:06 +0000 | [diff] [blame] | 342 | zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id)); |
paul | 2d59836 | 2003-01-17 23:48:42 +0000 | [diff] [blame] | 343 | |
| 344 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return; |
| 349 | } |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 350 | |
| 351 | |
| 352 | struct ospf_neighbor * |
| 353 | ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph, |
| 354 | struct ospf_header *ospfh) |
| 355 | { |
| 356 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 357 | return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id)); |
| 358 | else |
| 359 | return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src)); |
| 360 | } |
| 361 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 362 | static struct ospf_neighbor * |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 363 | ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh, |
| 364 | struct prefix *p) |
| 365 | { |
| 366 | struct ospf_neighbor *nbr; |
| 367 | |
| 368 | nbr = ospf_nbr_new (oi); |
| 369 | nbr->state = NSM_Down; |
| 370 | nbr->src = p->u.prefix4; |
| 371 | memcpy (&nbr->address, p, sizeof (struct prefix)); |
| 372 | |
| 373 | nbr->nbr_nbma = NULL; |
| 374 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 375 | { |
| 376 | struct ospf_nbr_nbma *nbr_nbma; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 377 | struct listnode *node; |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 378 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 379 | for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma)) |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 380 | { |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 381 | if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src)) |
| 382 | { |
| 383 | nbr_nbma->nbr = nbr; |
| 384 | nbr->nbr_nbma = nbr_nbma; |
| 385 | |
| 386 | if (nbr_nbma->t_poll) |
| 387 | OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll); |
| 388 | |
| 389 | nbr->state_change = nbr_nbma->state_change + 1; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* New nbr, save the crypto sequence number if necessary */ |
| 395 | if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC) |
| 396 | nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum; |
| 397 | |
| 398 | if (IS_DEBUG_OSPF_EVENT) |
ajs | e588f21 | 2004-12-08 18:12:06 +0000 | [diff] [blame] | 399 | zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi), |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 400 | inet_ntoa (nbr->router_id)); |
| 401 | |
| 402 | return nbr; |
| 403 | } |
| 404 | |
| 405 | struct ospf_neighbor * |
| 406 | ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh, |
| 407 | struct ip *iph, struct prefix *p) |
| 408 | { |
| 409 | struct route_node *rn; |
| 410 | struct prefix key; |
| 411 | struct ospf_neighbor *nbr; |
| 412 | |
| 413 | key.family = AF_INET; |
| 414 | key.prefixlen = IPV4_MAX_BITLEN; |
| 415 | |
| 416 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 417 | key.u.prefix4 = ospfh->router_id; /* index vlink nbrs by router-id */ |
| 418 | else |
| 419 | key.u.prefix4 = iph->ip_src; |
| 420 | |
| 421 | rn = route_node_get (oi->nbrs, &key); |
| 422 | if (rn->info) |
| 423 | { |
| 424 | route_unlock_node (rn); |
| 425 | nbr = rn->info; |
| 426 | |
| 427 | if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt) |
| 428 | { |
| 429 | nbr->src = iph->ip_src; |
| 430 | memcpy (&nbr->address, p, sizeof (struct prefix)); |
| 431 | } |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | rn->info = nbr = ospf_nbr_add (oi, ospfh, p); |
| 436 | } |
| 437 | |
| 438 | nbr->router_id = ospfh->router_id; |
| 439 | |
| 440 | return nbr; |
| 441 | } |
| 442 | |