]>
git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_neighbor.c
2 * OSPF Neighbor functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
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.
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.
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.
35 #include "ospfd/ospfd.h"
36 #include "ospfd/ospf_interface.h"
37 #include "ospfd/ospf_asbr.h"
38 #include "ospfd/ospf_lsa.h"
39 #include "ospfd/ospf_lsdb.h"
40 #include "ospfd/ospf_neighbor.h"
41 #include "ospfd/ospf_nsm.h"
42 #include "ospfd/ospf_packet.h"
43 #include "ospfd/ospf_network.h"
44 #include "ospfd/ospf_flood.h"
45 #include "ospfd/ospf_dump.h"
46 #include "ospfd/ospf_bfd.h"
48 /* Fill in the the 'key' as appropriate to retrieve the entry for nbr
49 * from the ospf_interface's nbrs table. Indexed by interface address
50 * for all cases except Virtual-link interfaces, where neighbours are
51 * indexed by router-ID instead.
54 ospf_nbr_key (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
,
57 key
->family
= AF_INET
;
58 key
->prefixlen
= IPV4_MAX_BITLEN
;
60 /* vlinks are indexed by router-id */
61 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
62 key
->u
.prefix4
= nbr
->router_id
;
64 key
->u
.prefix4
= nbr
->src
;
68 struct ospf_neighbor
*
69 ospf_nbr_new (struct ospf_interface
*oi
)
71 struct ospf_neighbor
*nbr
;
73 /* Allcate new neighbor. */
74 nbr
= XCALLOC (MTYPE_OSPF_NEIGHBOR
, sizeof (struct ospf_neighbor
));
76 /* Relate neighbor to the interface. */
79 /* Set default values. */
80 nbr
->state
= NSM_Down
;
82 /* Set inheritance values. */
83 nbr
->v_inactivity
= OSPF_IF_PARAM (oi
, v_wait
);
84 nbr
->v_db_desc
= OSPF_IF_PARAM (oi
, retransmit_interval
);
85 nbr
->v_ls_req
= OSPF_IF_PARAM (oi
, retransmit_interval
);
86 nbr
->v_ls_upd
= OSPF_IF_PARAM (oi
, retransmit_interval
);
90 nbr
->dd_flags
= OSPF_DD_FLAG_MS
|OSPF_DD_FLAG_M
|OSPF_DD_FLAG_I
;
92 /* Last received and sent DD. */
93 nbr
->last_send
= NULL
;
97 ospf_lsdb_init (&nbr
->db_sum
);
98 ospf_lsdb_init (&nbr
->ls_rxmt
);
99 ospf_lsdb_init (&nbr
->ls_req
);
101 nbr
->crypt_seqnum
= 0;
103 ospf_bfd_info_nbr_create(oi
, nbr
);
108 ospf_nbr_free (struct ospf_neighbor
*nbr
)
110 /* Free DB summary list. */
111 if (ospf_db_summary_count (nbr
))
112 ospf_db_summary_clear (nbr
);
113 /* ospf_db_summary_delete_all (nbr); */
115 /* Free ls request list. */
116 if (ospf_ls_request_count (nbr
))
117 ospf_ls_request_delete_all (nbr
);
119 /* Free retransmit list. */
120 if (ospf_ls_retransmit_count (nbr
))
121 ospf_ls_retransmit_clear (nbr
);
124 ospf_lsdb_cleanup (&nbr
->db_sum
);
125 ospf_lsdb_cleanup (&nbr
->ls_req
);
126 ospf_lsdb_cleanup (&nbr
->ls_rxmt
);
128 /* Clear last send packet. */
130 ospf_packet_free (nbr
->last_send
);
134 nbr
->nbr_nbma
->nbr
= NULL
;
135 nbr
->nbr_nbma
= NULL
;
138 /* Cancel all timers. */
139 OSPF_NSM_TIMER_OFF (nbr
->t_inactivity
);
140 OSPF_NSM_TIMER_OFF (nbr
->t_db_desc
);
141 OSPF_NSM_TIMER_OFF (nbr
->t_ls_req
);
142 OSPF_NSM_TIMER_OFF (nbr
->t_ls_upd
);
144 /* Cancel all events. *//* Thread lookup cost would be negligible. */
145 thread_cancel_event (master
, nbr
);
147 ospf_bfd_info_free(&nbr
->bfd_info
);
148 XFREE (MTYPE_OSPF_NEIGHBOR
, nbr
);
151 /* Delete specified OSPF neighbor from interface. */
153 ospf_nbr_delete (struct ospf_neighbor
*nbr
)
155 struct ospf_interface
*oi
;
156 struct route_node
*rn
;
161 /* get appropriate prefix 'key' */
162 ospf_nbr_key (oi
, nbr
, &p
);
164 rn
= route_node_lookup (oi
->nbrs
, &p
);
167 /* If lookup for a NBR succeeds, the leaf route_node could
168 * only exist because there is (or was) a nbr there.
169 * If the nbr was deleted, the leaf route_node should have
170 * lost its last refcount too, and be deleted.
171 * Therefore a looked-up leaf route_node in nbrs table
172 * should never have NULL info.
179 route_unlock_node (rn
);
182 zlog_info ("Can't find neighbor %s in the interface %s",
183 inet_ntoa (nbr
->src
), IF_NAME (oi
));
185 route_unlock_node (rn
);
188 /* Free ospf_neighbor structure. */
192 /* Check myself is in the neighbor list. */
194 ospf_nbr_bidirectional (struct in_addr
*router_id
,
195 struct in_addr
*neighbors
, int size
)
200 max
= size
/ sizeof (struct in_addr
);
202 for (i
= 0; i
< max
; i
++)
203 if (IPV4_ADDR_SAME (router_id
, &neighbors
[i
]))
209 /* Add self to nbr list. */
211 ospf_nbr_add_self (struct ospf_interface
*oi
)
214 struct route_node
*rn
;
217 oi
->nbr_self
->address
= *oi
->address
;
218 oi
->nbr_self
->priority
= OSPF_IF_PARAM (oi
, priority
);
219 oi
->nbr_self
->router_id
= oi
->ospf
->router_id
;
220 oi
->nbr_self
->src
= oi
->address
->u
.prefix4
;
221 oi
->nbr_self
->state
= NSM_TwoWay
;
223 switch (oi
->area
->external_routing
)
225 case OSPF_AREA_DEFAULT
:
226 SET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
229 UNSET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
232 UNSET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
233 SET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_NP
);
237 /* Add nbr_self to nbrs table */
238 ospf_nbr_key (oi
, oi
->nbr_self
, &p
);
240 rn
= route_node_get (oi
->nbrs
, &p
);
243 /* There is already pseudo neighbor. */
244 assert (oi
->nbr_self
== rn
->info
);
245 route_unlock_node (rn
);
248 rn
->info
= oi
->nbr_self
;
251 /* Get neighbor count by status.
252 Specify status = 0, get all neighbor other than myself. */
254 ospf_nbr_count (struct ospf_interface
*oi
, int state
)
256 struct ospf_neighbor
*nbr
;
257 struct route_node
*rn
;
260 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
261 if ((nbr
= rn
->info
))
262 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
263 if (state
== 0 || nbr
->state
== state
)
269 #ifdef HAVE_OPAQUE_LSA
271 ospf_nbr_count_opaque_capable (struct ospf_interface
*oi
)
273 struct ospf_neighbor
*nbr
;
274 struct route_node
*rn
;
277 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
278 if ((nbr
= rn
->info
))
279 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
280 if (nbr
->state
== NSM_Full
)
281 if (CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
286 #endif /* HAVE_OPAQUE_LSA */
288 /* lookup nbr by address - use this only if you know you must
289 * otherwise use the ospf_nbr_lookup() wrapper, which deals
290 * with virtual link neighbours
292 struct ospf_neighbor
*
293 ospf_nbr_lookup_by_addr (struct route_table
*nbrs
,
294 struct in_addr
*addr
)
297 struct route_node
*rn
;
298 struct ospf_neighbor
*nbr
;
301 p
.prefixlen
= IPV4_MAX_BITLEN
;
304 rn
= route_node_lookup (nbrs
, &p
);
308 /* See comment in ospf_nbr_delete */
311 if (rn
->info
== NULL
)
313 route_unlock_node (rn
);
317 nbr
= (struct ospf_neighbor
*) rn
->info
;
318 route_unlock_node (rn
);
323 struct ospf_neighbor
*
324 ospf_nbr_lookup_by_routerid (struct route_table
*nbrs
,
327 struct route_node
*rn
;
328 struct ospf_neighbor
*nbr
;
330 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
331 if ((nbr
= rn
->info
) != NULL
)
332 if (IPV4_ADDR_SAME (&nbr
->router_id
, id
))
334 route_unlock_node(rn
);
342 ospf_renegotiate_optional_capabilities (struct ospf
*top
)
344 struct listnode
*node
;
345 struct ospf_interface
*oi
;
346 struct route_table
*nbrs
;
347 struct route_node
*rn
;
348 struct ospf_neighbor
*nbr
;
350 /* At first, flush self-originated LSAs from routing domain. */
351 ospf_flush_self_originated_lsas_now (top
);
353 /* Revert all neighbor status to ExStart. */
354 for (ALL_LIST_ELEMENTS_RO (top
->oiflist
, node
, oi
))
356 if ((nbrs
= oi
->nbrs
) == NULL
)
359 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
361 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
364 if (nbr
->state
< NSM_ExStart
)
367 if (IS_DEBUG_OSPF_EVENT
)
368 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr
->router_id
));
370 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
378 struct ospf_neighbor
*
379 ospf_nbr_lookup (struct ospf_interface
*oi
, struct ip
*iph
,
380 struct ospf_header
*ospfh
)
382 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
383 return (ospf_nbr_lookup_by_routerid (oi
->nbrs
, &ospfh
->router_id
));
385 return (ospf_nbr_lookup_by_addr (oi
->nbrs
, &iph
->ip_src
));
388 static struct ospf_neighbor
*
389 ospf_nbr_add (struct ospf_interface
*oi
, struct ospf_header
*ospfh
,
392 struct ospf_neighbor
*nbr
;
394 nbr
= ospf_nbr_new (oi
);
395 nbr
->state
= NSM_Down
;
396 nbr
->src
= p
->u
.prefix4
;
397 memcpy (&nbr
->address
, p
, sizeof (struct prefix
));
399 nbr
->nbr_nbma
= NULL
;
400 if (oi
->type
== OSPF_IFTYPE_NBMA
)
402 struct ospf_nbr_nbma
*nbr_nbma
;
403 struct listnode
*node
;
405 for (ALL_LIST_ELEMENTS_RO (oi
->nbr_nbma
, node
, nbr_nbma
))
407 if (IPV4_ADDR_SAME(&nbr_nbma
->addr
, &nbr
->src
))
410 nbr
->nbr_nbma
= nbr_nbma
;
412 if (nbr_nbma
->t_poll
)
413 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
415 nbr
->state_change
= nbr_nbma
->state_change
+ 1;
420 /* New nbr, save the crypto sequence number if necessary */
421 if (ntohs (ospfh
->auth_type
) == OSPF_AUTH_CRYPTOGRAPHIC
)
422 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
424 if (IS_DEBUG_OSPF_EVENT
)
425 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr
->oi
),
426 inet_ntoa (nbr
->router_id
));
431 struct ospf_neighbor
*
432 ospf_nbr_get (struct ospf_interface
*oi
, struct ospf_header
*ospfh
,
433 struct ip
*iph
, struct prefix
*p
)
435 struct route_node
*rn
;
437 struct ospf_neighbor
*nbr
;
439 key
.family
= AF_INET
;
440 key
.prefixlen
= IPV4_MAX_BITLEN
;
442 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
443 key
.u
.prefix4
= ospfh
->router_id
; /* index vlink nbrs by router-id */
445 key
.u
.prefix4
= iph
->ip_src
;
447 rn
= route_node_get (oi
->nbrs
, &key
);
450 route_unlock_node (rn
);
453 if (oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->state
== NSM_Attempt
)
455 nbr
->src
= iph
->ip_src
;
456 memcpy (&nbr
->address
, p
, sizeof (struct prefix
));
461 rn
->info
= nbr
= ospf_nbr_add (oi
, ospfh
, p
);
464 nbr
->router_id
= ospfh
->router_id
;