]>
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 #include "ospfd/ospf_bfd.h"
47 /* Fill in the the 'key' as appropriate to retrieve the entry for nbr
48 * from the ospf_interface's nbrs table. Indexed by interface address
49 * for all cases except Virtual-link and PointToPoint interfaces, where
50 * neighbours are indexed by router-ID instead.
52 static void ospf_nbr_key(struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
,
55 key
->family
= AF_INET
;
56 key
->prefixlen
= IPV4_MAX_BITLEN
;
58 /* vlinks are indexed by router-id */
59 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
60 || oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
61 key
->u
.prefix4
= nbr
->router_id
;
63 key
->u
.prefix4
= nbr
->src
;
67 struct ospf_neighbor
*ospf_nbr_new(struct ospf_interface
*oi
)
69 struct ospf_neighbor
*nbr
;
71 /* Allcate new neighbor. */
72 nbr
= XCALLOC(MTYPE_OSPF_NEIGHBOR
, sizeof(struct ospf_neighbor
));
74 /* Relate neighbor to the interface. */
77 /* Set default values. */
78 nbr
->state
= NSM_Down
;
80 /* Set inheritance values. */
81 nbr
->v_inactivity
= OSPF_IF_PARAM(oi
, v_wait
);
82 nbr
->v_db_desc
= OSPF_IF_PARAM(oi
, retransmit_interval
);
83 nbr
->v_ls_req
= OSPF_IF_PARAM(oi
, retransmit_interval
);
84 nbr
->v_ls_upd
= OSPF_IF_PARAM(oi
, retransmit_interval
);
88 nbr
->dd_flags
= OSPF_DD_FLAG_MS
| OSPF_DD_FLAG_M
| OSPF_DD_FLAG_I
;
90 /* Last received and sent DD. */
91 nbr
->last_send
= NULL
;
95 ospf_lsdb_init(&nbr
->db_sum
);
96 ospf_lsdb_init(&nbr
->ls_rxmt
);
97 ospf_lsdb_init(&nbr
->ls_req
);
99 nbr
->crypt_seqnum
= 0;
101 ospf_bfd_info_nbr_create(oi
, nbr
);
105 void ospf_nbr_free(struct ospf_neighbor
*nbr
)
107 /* Free DB summary list. */
108 if (ospf_db_summary_count(nbr
))
109 ospf_db_summary_clear(nbr
);
110 /* ospf_db_summary_delete_all (nbr); */
112 /* Free ls request list. */
113 if (ospf_ls_request_count(nbr
))
114 ospf_ls_request_delete_all(nbr
);
116 /* Free retransmit list. */
117 if (ospf_ls_retransmit_count(nbr
))
118 ospf_ls_retransmit_clear(nbr
);
121 ospf_lsdb_cleanup(&nbr
->db_sum
);
122 ospf_lsdb_cleanup(&nbr
->ls_req
);
123 ospf_lsdb_cleanup(&nbr
->ls_rxmt
);
125 /* Clear last send packet. */
127 ospf_packet_free(nbr
->last_send
);
130 nbr
->nbr_nbma
->nbr
= NULL
;
131 nbr
->nbr_nbma
= NULL
;
134 /* Cancel all timers. */
135 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
136 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
137 OSPF_NSM_TIMER_OFF(nbr
->t_ls_req
);
138 OSPF_NSM_TIMER_OFF(nbr
->t_ls_upd
);
140 /* Cancel all events. */ /* Thread lookup cost would be negligible. */
141 thread_cancel_event(master
, nbr
);
143 ospf_bfd_info_free(&nbr
->bfd_info
);
144 XFREE(MTYPE_OSPF_NEIGHBOR
, nbr
);
147 /* Delete specified OSPF neighbor from interface. */
148 void ospf_nbr_delete(struct ospf_neighbor
*nbr
)
150 struct ospf_interface
*oi
;
151 struct route_node
*rn
;
156 /* get appropriate prefix 'key' */
157 ospf_nbr_key(oi
, nbr
, &p
);
159 rn
= route_node_lookup(oi
->nbrs
, &p
);
161 /* If lookup for a NBR succeeds, the leaf route_node could
162 * only exist because there is (or was) a nbr there.
163 * If the nbr was deleted, the leaf route_node should have
164 * lost its last refcount too, and be deleted.
165 * Therefore a looked-up leaf route_node in nbrs table
166 * should never have NULL info.
172 route_unlock_node(rn
);
174 zlog_info("Can't find neighbor %s in the interface %s",
175 inet_ntoa(nbr
->src
), IF_NAME(oi
));
177 route_unlock_node(rn
);
180 * This neighbor was not found, but before we move on and
181 * free the neighbor structre, make sure that it was not
182 * indexed incorrectly and ended up in the "worng" place
185 /* Reverse the lookup rules */
186 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
187 || oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
188 p
.u
.prefix4
= nbr
->src
;
190 p
.u
.prefix4
= nbr
->router_id
;
192 rn
= route_node_lookup(oi
->nbrs
, &p
);
194 /* We found the neighbor!
195 * Now make sure it is not the exact same neighbor
196 * structure that we are about to free
198 if (nbr
== rn
->info
) {
199 /* Same neighbor, drop the reference to it */
201 route_unlock_node(rn
);
203 route_unlock_node(rn
);
207 /* Free ospf_neighbor structure. */
211 /* Check myself is in the neighbor list. */
212 int ospf_nbr_bidirectional(struct in_addr
*router_id
, struct in_addr
*neighbors
,
218 max
= size
/ sizeof(struct in_addr
);
220 for (i
= 0; i
< max
; i
++)
221 if (IPV4_ADDR_SAME(router_id
, &neighbors
[i
]))
228 void ospf_nbr_self_reset(struct ospf_interface
*oi
, struct in_addr router_id
)
231 ospf_nbr_delete(oi
->nbr_self
);
233 oi
->nbr_self
= ospf_nbr_new(oi
);
234 ospf_nbr_add_self(oi
, router_id
);
237 /* Add self to nbr list. */
238 void ospf_nbr_add_self(struct ospf_interface
*oi
, struct in_addr router_id
)
241 struct route_node
*rn
;
244 oi
->nbr_self
= ospf_nbr_new(oi
);
247 oi
->nbr_self
->address
= *oi
->address
;
248 oi
->nbr_self
->priority
= OSPF_IF_PARAM(oi
, priority
);
249 oi
->nbr_self
->router_id
= router_id
;
250 oi
->nbr_self
->src
= oi
->address
->u
.prefix4
;
251 oi
->nbr_self
->state
= NSM_TwoWay
;
253 switch (oi
->area
->external_routing
) {
254 case OSPF_AREA_DEFAULT
:
255 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
258 UNSET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
261 UNSET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
262 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_NP
);
266 /* Add nbr_self to nbrs table */
267 ospf_nbr_key(oi
, oi
->nbr_self
, &p
);
269 rn
= route_node_get(oi
->nbrs
, &p
);
271 /* There is already pseudo neighbor. */
272 assert(oi
->nbr_self
== rn
->info
);
273 route_unlock_node(rn
);
275 rn
->info
= oi
->nbr_self
;
278 /* Get neighbor count by status.
279 Specify status = 0, get all neighbor other than myself. */
280 int ospf_nbr_count(struct ospf_interface
*oi
, int state
)
282 struct ospf_neighbor
*nbr
;
283 struct route_node
*rn
;
286 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
287 if ((nbr
= rn
->info
))
288 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
289 &oi
->ospf
->router_id
))
290 if (state
== 0 || nbr
->state
== state
)
296 int ospf_nbr_count_opaque_capable(struct ospf_interface
*oi
)
298 struct ospf_neighbor
*nbr
;
299 struct route_node
*rn
;
302 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
303 if ((nbr
= rn
->info
))
304 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
305 &oi
->ospf
->router_id
))
306 if (nbr
->state
== NSM_Full
)
307 if (CHECK_FLAG(nbr
->options
,
314 /* lookup nbr by address - use this only if you know you must
315 * otherwise use the ospf_nbr_lookup() wrapper, which deals
316 * with virtual link and PointToPoint neighbours
318 struct ospf_neighbor
*ospf_nbr_lookup_by_addr(struct route_table
*nbrs
,
319 struct in_addr
*addr
)
322 struct route_node
*rn
;
323 struct ospf_neighbor
*nbr
;
326 p
.prefixlen
= IPV4_MAX_BITLEN
;
329 rn
= route_node_lookup(nbrs
, &p
);
333 /* See comment in ospf_nbr_delete */
336 if (rn
->info
== NULL
) {
337 route_unlock_node(rn
);
341 nbr
= (struct ospf_neighbor
*)rn
->info
;
342 route_unlock_node(rn
);
347 struct ospf_neighbor
*ospf_nbr_lookup_by_routerid(struct route_table
*nbrs
,
350 struct route_node
*rn
;
351 struct ospf_neighbor
*nbr
;
353 for (rn
= route_top(nbrs
); rn
; rn
= route_next(rn
))
354 if ((nbr
= rn
->info
) != NULL
)
355 if (IPV4_ADDR_SAME(&nbr
->router_id
, id
)) {
356 route_unlock_node(rn
);
363 void ospf_renegotiate_optional_capabilities(struct ospf
*top
)
365 struct listnode
*node
;
366 struct ospf_interface
*oi
;
367 struct route_table
*nbrs
;
368 struct route_node
*rn
;
369 struct ospf_neighbor
*nbr
;
371 /* At first, flush self-originated LSAs from routing domain. */
372 ospf_flush_self_originated_lsas_now(top
);
374 /* Revert all neighbor status to ExStart. */
375 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
)) {
376 if ((nbrs
= oi
->nbrs
) == NULL
)
379 for (rn
= route_top(nbrs
); rn
; rn
= route_next(rn
)) {
380 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
383 if (nbr
->state
< NSM_ExStart
)
386 if (IS_DEBUG_OSPF_EVENT
)
388 "Renegotiate optional capabilities with neighbor(%s)",
389 inet_ntoa(nbr
->router_id
));
391 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
399 struct ospf_neighbor
*ospf_nbr_lookup(struct ospf_interface
*oi
, struct ip
*iph
,
400 struct ospf_header
*ospfh
)
402 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
403 || oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
404 return (ospf_nbr_lookup_by_routerid(oi
->nbrs
,
407 return (ospf_nbr_lookup_by_addr(oi
->nbrs
, &iph
->ip_src
));
410 static struct ospf_neighbor
*ospf_nbr_add(struct ospf_interface
*oi
,
411 struct ospf_header
*ospfh
,
414 struct ospf_neighbor
*nbr
;
416 nbr
= ospf_nbr_new(oi
);
417 nbr
->state
= NSM_Down
;
418 nbr
->src
= p
->u
.prefix4
;
419 memcpy(&nbr
->address
, p
, sizeof(struct prefix
));
421 nbr
->nbr_nbma
= NULL
;
422 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
423 struct ospf_nbr_nbma
*nbr_nbma
;
424 struct listnode
*node
;
426 for (ALL_LIST_ELEMENTS_RO(oi
->nbr_nbma
, node
, nbr_nbma
)) {
427 if (IPV4_ADDR_SAME(&nbr_nbma
->addr
, &nbr
->src
)) {
429 nbr
->nbr_nbma
= nbr_nbma
;
431 if (nbr_nbma
->t_poll
)
432 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
434 nbr
->state_change
= nbr_nbma
->state_change
+ 1;
439 /* New nbr, save the crypto sequence number if necessary */
440 if (ntohs(ospfh
->auth_type
) == OSPF_AUTH_CRYPTOGRAPHIC
)
441 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
443 if (IS_DEBUG_OSPF_EVENT
)
444 zlog_debug("NSM[%s:%s]: start", IF_NAME(nbr
->oi
),
445 inet_ntoa(nbr
->router_id
));
450 struct ospf_neighbor
*ospf_nbr_get(struct ospf_interface
*oi
,
451 struct ospf_header
*ospfh
, struct ip
*iph
,
454 struct route_node
*rn
;
456 struct ospf_neighbor
*nbr
;
458 key
.family
= AF_INET
;
459 key
.prefixlen
= IPV4_MAX_BITLEN
;
461 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
462 || oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
464 ospfh
->router_id
; /* index vlink and ptp nbrs by
467 key
.u
.prefix4
= iph
->ip_src
;
469 rn
= route_node_get(oi
->nbrs
, &key
);
471 route_unlock_node(rn
);
474 if (oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->state
== NSM_Attempt
) {
475 nbr
->src
= iph
->ip_src
;
476 memcpy(&nbr
->address
, p
, sizeof(struct prefix
));
479 rn
->info
= nbr
= ospf_nbr_add(oi
, ospfh
, p
);
482 nbr
->router_id
= ospfh
->router_id
;