]>
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 and PointToPoint interfaces, where
51 * neighbours are 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 oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
63 key
->u
.prefix4
= nbr
->router_id
;
65 key
->u
.prefix4
= nbr
->src
;
69 struct ospf_neighbor
*
70 ospf_nbr_new (struct ospf_interface
*oi
)
72 struct ospf_neighbor
*nbr
;
74 /* Allcate new neighbor. */
75 nbr
= XCALLOC (MTYPE_OSPF_NEIGHBOR
, sizeof (struct ospf_neighbor
));
77 /* Relate neighbor to the interface. */
80 /* Set default values. */
81 nbr
->state
= NSM_Down
;
83 /* Set inheritance values. */
84 nbr
->v_inactivity
= OSPF_IF_PARAM (oi
, v_wait
);
85 nbr
->v_db_desc
= OSPF_IF_PARAM (oi
, retransmit_interval
);
86 nbr
->v_ls_req
= OSPF_IF_PARAM (oi
, retransmit_interval
);
87 nbr
->v_ls_upd
= OSPF_IF_PARAM (oi
, retransmit_interval
);
91 nbr
->dd_flags
= OSPF_DD_FLAG_MS
|OSPF_DD_FLAG_M
|OSPF_DD_FLAG_I
;
93 /* Last received and sent DD. */
94 nbr
->last_send
= NULL
;
98 ospf_lsdb_init (&nbr
->db_sum
);
99 ospf_lsdb_init (&nbr
->ls_rxmt
);
100 ospf_lsdb_init (&nbr
->ls_req
);
102 nbr
->crypt_seqnum
= 0;
104 ospf_bfd_info_nbr_create(oi
, nbr
);
109 ospf_nbr_free (struct ospf_neighbor
*nbr
)
111 /* Free DB summary list. */
112 if (ospf_db_summary_count (nbr
))
113 ospf_db_summary_clear (nbr
);
114 /* ospf_db_summary_delete_all (nbr); */
116 /* Free ls request list. */
117 if (ospf_ls_request_count (nbr
))
118 ospf_ls_request_delete_all (nbr
);
120 /* Free retransmit list. */
121 if (ospf_ls_retransmit_count (nbr
))
122 ospf_ls_retransmit_clear (nbr
);
125 ospf_lsdb_cleanup (&nbr
->db_sum
);
126 ospf_lsdb_cleanup (&nbr
->ls_req
);
127 ospf_lsdb_cleanup (&nbr
->ls_rxmt
);
129 /* Clear last send packet. */
131 ospf_packet_free (nbr
->last_send
);
135 nbr
->nbr_nbma
->nbr
= NULL
;
136 nbr
->nbr_nbma
= NULL
;
139 /* Cancel all timers. */
140 OSPF_NSM_TIMER_OFF (nbr
->t_inactivity
);
141 OSPF_NSM_TIMER_OFF (nbr
->t_db_desc
);
142 OSPF_NSM_TIMER_OFF (nbr
->t_ls_req
);
143 OSPF_NSM_TIMER_OFF (nbr
->t_ls_upd
);
145 /* Cancel all events. *//* Thread lookup cost would be negligible. */
146 thread_cancel_event (master
, nbr
);
148 ospf_bfd_info_free(&nbr
->bfd_info
);
149 XFREE (MTYPE_OSPF_NEIGHBOR
, nbr
);
152 /* Delete specified OSPF neighbor from interface. */
154 ospf_nbr_delete (struct ospf_neighbor
*nbr
)
156 struct ospf_interface
*oi
;
157 struct route_node
*rn
;
162 /* get appropriate prefix 'key' */
163 ospf_nbr_key (oi
, nbr
, &p
);
165 rn
= route_node_lookup (oi
->nbrs
, &p
);
168 /* If lookup for a NBR succeeds, the leaf route_node could
169 * only exist because there is (or was) a nbr there.
170 * If the nbr was deleted, the leaf route_node should have
171 * lost its last refcount too, and be deleted.
172 * Therefore a looked-up leaf route_node in nbrs table
173 * should never have NULL info.
180 route_unlock_node (rn
);
183 zlog_info ("Can't find neighbor %s in the interface %s",
184 inet_ntoa (nbr
->src
), IF_NAME (oi
));
186 route_unlock_node (rn
);
191 * This neighbor was not found, but before we move on and
192 * free the neighbor structre, make sure that it was not
193 * indexed incorrectly and ended up in the "worng" place
196 /* Reverse the lookup rules */
197 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
||
198 oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
199 p
.u
.prefix4
= nbr
->src
;
201 p
.u
.prefix4
= nbr
->router_id
;
203 rn
= route_node_lookup (oi
->nbrs
, &p
);
205 /* We found the neighbor!
206 * Now make sure it is not the exact same neighbor
207 * structure that we are about to free
209 if (nbr
== rn
->info
){
210 /* Same neighbor, drop the reference to it */
212 route_unlock_node (rn
);
214 route_unlock_node (rn
);
218 /* Free ospf_neighbor structure. */
222 /* Check myself is in the neighbor list. */
224 ospf_nbr_bidirectional (struct in_addr
*router_id
,
225 struct in_addr
*neighbors
, int size
)
230 max
= size
/ sizeof (struct in_addr
);
232 for (i
= 0; i
< max
; i
++)
233 if (IPV4_ADDR_SAME (router_id
, &neighbors
[i
]))
241 ospf_nbr_self_reset (struct ospf_interface
*oi
, struct in_addr router_id
)
244 ospf_nbr_delete (oi
->nbr_self
);
246 oi
->nbr_self
= ospf_nbr_new (oi
);
247 ospf_nbr_add_self (oi
, router_id
);
250 /* Add self to nbr list. */
252 ospf_nbr_add_self (struct ospf_interface
*oi
, struct in_addr router_id
)
255 struct route_node
*rn
;
258 oi
->nbr_self
= ospf_nbr_new (oi
);
261 oi
->nbr_self
->address
= *oi
->address
;
262 oi
->nbr_self
->priority
= OSPF_IF_PARAM (oi
, priority
);
263 oi
->nbr_self
->router_id
= router_id
;
264 oi
->nbr_self
->src
= oi
->address
->u
.prefix4
;
265 oi
->nbr_self
->state
= NSM_TwoWay
;
267 switch (oi
->area
->external_routing
)
269 case OSPF_AREA_DEFAULT
:
270 SET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
273 UNSET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
276 UNSET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_E
);
277 SET_FLAG (oi
->nbr_self
->options
, OSPF_OPTION_NP
);
281 /* Add nbr_self to nbrs table */
282 ospf_nbr_key (oi
, oi
->nbr_self
, &p
);
284 rn
= route_node_get (oi
->nbrs
, &p
);
287 /* There is already pseudo neighbor. */
288 assert (oi
->nbr_self
== rn
->info
);
289 route_unlock_node (rn
);
292 rn
->info
= oi
->nbr_self
;
295 /* Get neighbor count by status.
296 Specify status = 0, get all neighbor other than myself. */
298 ospf_nbr_count (struct ospf_interface
*oi
, int state
)
300 struct ospf_neighbor
*nbr
;
301 struct route_node
*rn
;
304 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
305 if ((nbr
= rn
->info
))
306 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
307 if (state
== 0 || nbr
->state
== state
)
314 ospf_nbr_count_opaque_capable (struct ospf_interface
*oi
)
316 struct ospf_neighbor
*nbr
;
317 struct route_node
*rn
;
320 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
321 if ((nbr
= rn
->info
))
322 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
323 if (nbr
->state
== NSM_Full
)
324 if (CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
330 /* lookup nbr by address - use this only if you know you must
331 * otherwise use the ospf_nbr_lookup() wrapper, which deals
332 * with virtual link and PointToPoint neighbours
334 struct ospf_neighbor
*
335 ospf_nbr_lookup_by_addr (struct route_table
*nbrs
,
336 struct in_addr
*addr
)
339 struct route_node
*rn
;
340 struct ospf_neighbor
*nbr
;
343 p
.prefixlen
= IPV4_MAX_BITLEN
;
346 rn
= route_node_lookup (nbrs
, &p
);
350 /* See comment in ospf_nbr_delete */
353 if (rn
->info
== NULL
)
355 route_unlock_node (rn
);
359 nbr
= (struct ospf_neighbor
*) rn
->info
;
360 route_unlock_node (rn
);
365 struct ospf_neighbor
*
366 ospf_nbr_lookup_by_routerid (struct route_table
*nbrs
,
369 struct route_node
*rn
;
370 struct ospf_neighbor
*nbr
;
372 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
373 if ((nbr
= rn
->info
) != NULL
)
374 if (IPV4_ADDR_SAME (&nbr
->router_id
, id
))
376 route_unlock_node(rn
);
384 ospf_renegotiate_optional_capabilities (struct ospf
*top
)
386 struct listnode
*node
;
387 struct ospf_interface
*oi
;
388 struct route_table
*nbrs
;
389 struct route_node
*rn
;
390 struct ospf_neighbor
*nbr
;
392 /* At first, flush self-originated LSAs from routing domain. */
393 ospf_flush_self_originated_lsas_now (top
);
395 /* Revert all neighbor status to ExStart. */
396 for (ALL_LIST_ELEMENTS_RO (top
->oiflist
, node
, oi
))
398 if ((nbrs
= oi
->nbrs
) == NULL
)
401 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
403 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
406 if (nbr
->state
< NSM_ExStart
)
409 if (IS_DEBUG_OSPF_EVENT
)
410 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr
->router_id
));
412 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
420 struct ospf_neighbor
*
421 ospf_nbr_lookup (struct ospf_interface
*oi
, struct ip
*iph
,
422 struct ospf_header
*ospfh
)
424 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
||
425 oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
426 return (ospf_nbr_lookup_by_routerid (oi
->nbrs
, &ospfh
->router_id
));
428 return (ospf_nbr_lookup_by_addr (oi
->nbrs
, &iph
->ip_src
));
431 static struct ospf_neighbor
*
432 ospf_nbr_add (struct ospf_interface
*oi
, struct ospf_header
*ospfh
,
435 struct ospf_neighbor
*nbr
;
437 nbr
= ospf_nbr_new (oi
);
438 nbr
->state
= NSM_Down
;
439 nbr
->src
= p
->u
.prefix4
;
440 memcpy (&nbr
->address
, p
, sizeof (struct prefix
));
442 nbr
->nbr_nbma
= NULL
;
443 if (oi
->type
== OSPF_IFTYPE_NBMA
)
445 struct ospf_nbr_nbma
*nbr_nbma
;
446 struct listnode
*node
;
448 for (ALL_LIST_ELEMENTS_RO (oi
->nbr_nbma
, node
, nbr_nbma
))
450 if (IPV4_ADDR_SAME(&nbr_nbma
->addr
, &nbr
->src
))
453 nbr
->nbr_nbma
= nbr_nbma
;
455 if (nbr_nbma
->t_poll
)
456 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
458 nbr
->state_change
= nbr_nbma
->state_change
+ 1;
463 /* New nbr, save the crypto sequence number if necessary */
464 if (ntohs (ospfh
->auth_type
) == OSPF_AUTH_CRYPTOGRAPHIC
)
465 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
467 if (IS_DEBUG_OSPF_EVENT
)
468 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr
->oi
),
469 inet_ntoa (nbr
->router_id
));
474 struct ospf_neighbor
*
475 ospf_nbr_get (struct ospf_interface
*oi
, struct ospf_header
*ospfh
,
476 struct ip
*iph
, struct prefix
*p
)
478 struct route_node
*rn
;
480 struct ospf_neighbor
*nbr
;
482 key
.family
= AF_INET
;
483 key
.prefixlen
= IPV4_MAX_BITLEN
;
485 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
||
486 oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
487 key
.u
.prefix4
= ospfh
->router_id
;/* index vlink and ptp nbrs by router-id */
489 key
.u
.prefix4
= iph
->ip_src
;
491 rn
= route_node_get (oi
->nbrs
, &key
);
494 route_unlock_node (rn
);
497 if (oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->state
== NSM_Attempt
)
499 nbr
->src
= iph
->ip_src
;
500 memcpy (&nbr
->address
, p
, sizeof (struct prefix
));
505 rn
->info
= nbr
= ospf_nbr_add (oi
, ospfh
, p
);
508 nbr
->router_id
= ospfh
->router_id
;