]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_neighbor.c
Merge pull request #8106 from donaldsharp/fix_bad_interaction
[mirror_frr.git] / ospfd / ospf_neighbor.c
CommitLineData
2d59836a 1/*
2 * OSPF Neighbor functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
896014f4 6 *
2d59836a 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 *
896014f4
DL
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
2d59836a 20 */
21
22#include <zebra.h>
23
24#include "linklist.h"
25#include "prefix.h"
26#include "memory.h"
27#include "command.h"
28#include "thread.h"
29#include "stream.h"
30#include "table.h"
31#include "log.h"
68fe91d6 32#include "json.h"
2d59836a 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"
68fe91d6 45#include "ospfd/ospf_bfd.h"
06bc3110 46#include "ospfd/ospf_gr_helper.h"
2d59836a 47
478aab98
PJ
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
0ab4a2d6
JT
50 * for all cases except Virtual-link and PointToPoint interfaces, where
51 * neighbours are indexed by router-ID instead.
478aab98 52 */
d62a17ae 53static void ospf_nbr_key(struct ospf_interface *oi, struct ospf_neighbor *nbr,
54 struct prefix *key)
478aab98 55{
d62a17ae 56 key->family = AF_INET;
57 key->prefixlen = IPV4_MAX_BITLEN;
58
59 /* vlinks are indexed by router-id */
60 if (oi->type == OSPF_IFTYPE_VIRTUALLINK
61 || oi->type == OSPF_IFTYPE_POINTOPOINT)
62 key->u.prefix4 = nbr->router_id;
63 else
64 key->u.prefix4 = nbr->src;
65 return;
478aab98
PJ
66}
67
d62a17ae 68struct ospf_neighbor *ospf_nbr_new(struct ospf_interface *oi)
2d59836a 69{
d62a17ae 70 struct ospf_neighbor *nbr;
2d59836a 71
d62a17ae 72 /* Allcate new neighbor. */
73 nbr = XCALLOC(MTYPE_OSPF_NEIGHBOR, sizeof(struct ospf_neighbor));
2d59836a 74
d62a17ae 75 /* Relate neighbor to the interface. */
76 nbr->oi = oi;
2d59836a 77
d62a17ae 78 /* Set default values. */
79 nbr->state = NSM_Down;
2d59836a 80
d62a17ae 81 /* Set inheritance values. */
82 nbr->v_inactivity = OSPF_IF_PARAM(oi, v_wait);
83 nbr->v_db_desc = OSPF_IF_PARAM(oi, retransmit_interval);
84 nbr->v_ls_req = OSPF_IF_PARAM(oi, retransmit_interval);
85 nbr->v_ls_upd = OSPF_IF_PARAM(oi, retransmit_interval);
86 nbr->priority = -1;
2d59836a 87
d62a17ae 88 /* DD flags. */
89 nbr->dd_flags = OSPF_DD_FLAG_MS | OSPF_DD_FLAG_M | OSPF_DD_FLAG_I;
2d59836a 90
d62a17ae 91 /* Last received and sent DD. */
92 nbr->last_send = NULL;
2d59836a 93
d62a17ae 94 nbr->nbr_nbma = NULL;
2d59836a 95
d62a17ae 96 ospf_lsdb_init(&nbr->db_sum);
97 ospf_lsdb_init(&nbr->ls_rxmt);
98 ospf_lsdb_init(&nbr->ls_req);
2d59836a 99
d62a17ae 100 nbr->crypt_seqnum = 0;
2d59836a 101
d62a17ae 102 ospf_bfd_info_nbr_create(oi, nbr);
06bc3110 103
104 /* Initialize GR Helper info*/
105 nbr->gr_helper_info.recvd_grace_period = 0;
106 nbr->gr_helper_info.actual_grace_period = 0;
107 nbr->gr_helper_info.gr_helper_status = OSPF_GR_NOT_HELPER;
108 nbr->gr_helper_info.helper_exit_reason = OSPF_GR_HELPER_EXIT_NONE;
109 nbr->gr_helper_info.gr_restart_reason = OSPF_GR_UNKNOWN_RESTART;
110
d62a17ae 111 return nbr;
2d59836a 112}
113
d62a17ae 114void ospf_nbr_free(struct ospf_neighbor *nbr)
2d59836a 115{
d62a17ae 116 /* Free DB summary list. */
117 if (ospf_db_summary_count(nbr))
118 ospf_db_summary_clear(nbr);
119 /* ospf_db_summary_delete_all (nbr); */
120
121 /* Free ls request list. */
122 if (ospf_ls_request_count(nbr))
123 ospf_ls_request_delete_all(nbr);
124
125 /* Free retransmit list. */
126 if (ospf_ls_retransmit_count(nbr))
127 ospf_ls_retransmit_clear(nbr);
128
129 /* Cleanup LSDBs. */
130 ospf_lsdb_cleanup(&nbr->db_sum);
131 ospf_lsdb_cleanup(&nbr->ls_req);
132 ospf_lsdb_cleanup(&nbr->ls_rxmt);
133
134 /* Clear last send packet. */
135 if (nbr->last_send)
136 ospf_packet_free(nbr->last_send);
137
138 if (nbr->nbr_nbma) {
139 nbr->nbr_nbma->nbr = NULL;
140 nbr->nbr_nbma = NULL;
141 }
142
143 /* Cancel all timers. */
144 OSPF_NSM_TIMER_OFF(nbr->t_inactivity);
145 OSPF_NSM_TIMER_OFF(nbr->t_db_desc);
146 OSPF_NSM_TIMER_OFF(nbr->t_ls_req);
147 OSPF_NSM_TIMER_OFF(nbr->t_ls_upd);
148
149 /* Cancel all events. */ /* Thread lookup cost would be negligible. */
150 thread_cancel_event(master, nbr);
151
152 ospf_bfd_info_free(&nbr->bfd_info);
45559c4d 153
06bc3110 154 OSPF_NSM_TIMER_OFF(nbr->gr_helper_info.t_grace_timer);
155
45559c4d 156 nbr->oi = NULL;
d62a17ae 157 XFREE(MTYPE_OSPF_NEIGHBOR, nbr);
2d59836a 158}
159
160/* Delete specified OSPF neighbor from interface. */
d62a17ae 161void ospf_nbr_delete(struct ospf_neighbor *nbr)
2d59836a 162{
d62a17ae 163 struct ospf_interface *oi;
164 struct route_node *rn;
165 struct prefix p;
166
167 oi = nbr->oi;
168
169 /* get appropriate prefix 'key' */
170 ospf_nbr_key(oi, nbr, &p);
171
172 rn = route_node_lookup(oi->nbrs, &p);
173 if (rn) {
174 /* If lookup for a NBR succeeds, the leaf route_node could
175 * only exist because there is (or was) a nbr there.
176 * If the nbr was deleted, the leaf route_node should have
177 * lost its last refcount too, and be deleted.
178 * Therefore a looked-up leaf route_node in nbrs table
179 * should never have NULL info.
180 */
181 assert(rn->info);
182
183 if (rn->info) {
184 rn->info = NULL;
185 route_unlock_node(rn);
186 } else
96b663a3
MS
187 zlog_info("Can't find neighbor %pI4 in the interface %s",
188 &nbr->src, IF_NAME(oi));
d62a17ae 189
190 route_unlock_node(rn);
191 } else {
192 /*
193 * This neighbor was not found, but before we move on and
194 * free the neighbor structre, make sure that it was not
195 * indexed incorrectly and ended up in the "worng" place
196 */
197
198 /* Reverse the lookup rules */
199 if (oi->type == OSPF_IFTYPE_VIRTUALLINK
200 || oi->type == OSPF_IFTYPE_POINTOPOINT)
201 p.u.prefix4 = nbr->src;
202 else
203 p.u.prefix4 = nbr->router_id;
204
205 rn = route_node_lookup(oi->nbrs, &p);
206 if (rn) {
207 /* We found the neighbor!
208 * Now make sure it is not the exact same neighbor
209 * structure that we are about to free
210 */
211 if (nbr == rn->info) {
212 /* Same neighbor, drop the reference to it */
213 rn->info = NULL;
214 route_unlock_node(rn);
215 }
216 route_unlock_node(rn);
217 }
2d59836a 218 }
2d59836a 219
d62a17ae 220 /* Free ospf_neighbor structure. */
221 ospf_nbr_free(nbr);
2d59836a 222}
223
224/* Check myself is in the neighbor list. */
d62a17ae 225int ospf_nbr_bidirectional(struct in_addr *router_id, struct in_addr *neighbors,
226 int size)
2d59836a 227{
d62a17ae 228 int i;
229 int max;
2d59836a 230
d62a17ae 231 max = size / sizeof(struct in_addr);
2d59836a 232
d62a17ae 233 for (i = 0; i < max; i++)
234 if (IPV4_ADDR_SAME(router_id, &neighbors[i]))
235 return 1;
2d59836a 236
d62a17ae 237 return 0;
2d59836a 238}
239
cdd0c849 240/* reset nbr_self */
d62a17ae 241void ospf_nbr_self_reset(struct ospf_interface *oi, struct in_addr router_id)
cdd0c849 242{
d62a17ae 243 if (oi->nbr_self)
244 ospf_nbr_delete(oi->nbr_self);
ecea0cb0 245
d62a17ae 246 oi->nbr_self = ospf_nbr_new(oi);
247 ospf_nbr_add_self(oi, router_id);
cdd0c849
PJ
248}
249
2d59836a 250/* Add self to nbr list. */
d62a17ae 251void ospf_nbr_add_self(struct ospf_interface *oi, struct in_addr router_id)
2d59836a 252{
d62a17ae 253 struct prefix p;
254 struct route_node *rn;
255
256 if (!oi->nbr_self)
257 oi->nbr_self = ospf_nbr_new(oi);
258
259 /* Initial state */
260 oi->nbr_self->address = *oi->address;
261 oi->nbr_self->priority = OSPF_IF_PARAM(oi, priority);
262 oi->nbr_self->router_id = router_id;
263 oi->nbr_self->src = oi->address->u.prefix4;
264 oi->nbr_self->state = NSM_TwoWay;
265
266 switch (oi->area->external_routing) {
267 case OSPF_AREA_DEFAULT:
268 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_E);
269 break;
270 case OSPF_AREA_STUB:
271 UNSET_FLAG(oi->nbr_self->options, OSPF_OPTION_E);
272 break;
273 case OSPF_AREA_NSSA:
274 UNSET_FLAG(oi->nbr_self->options, OSPF_OPTION_E);
275 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_NP);
276 break;
277 }
278
279 /* Add nbr_self to nbrs table */
280 ospf_nbr_key(oi, oi->nbr_self, &p);
281
282 rn = route_node_get(oi->nbrs, &p);
283 if (rn->info) {
284 /* There is already pseudo neighbor. */
ed59abd5
DS
285 if (IS_DEBUG_OSPF_EVENT)
286 zlog_debug(
96b663a3
MS
287 "router_id %pI4 already present in neighbor table. node refcount %u",
288 &router_id, route_node_get_lock_count(rn));
d62a17ae 289 route_unlock_node(rn);
290 } else
291 rn->info = oi->nbr_self;
2d59836a 292}
293
294/* Get neighbor count by status.
295 Specify status = 0, get all neighbor other than myself. */
d62a17ae 296int ospf_nbr_count(struct ospf_interface *oi, int state)
2d59836a 297{
d62a17ae 298 struct ospf_neighbor *nbr;
299 struct route_node *rn;
300 int count = 0;
301
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 (state == 0 || nbr->state == state)
307 count++;
308
309 return count;
2d59836a 310}
311
d62a17ae 312int ospf_nbr_count_opaque_capable(struct ospf_interface *oi)
2d59836a 313{
d62a17ae 314 struct ospf_neighbor *nbr;
315 struct route_node *rn;
316 int count = 0;
317
318 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
319 if ((nbr = rn->info))
320 if (!IPV4_ADDR_SAME(&nbr->router_id,
321 &oi->ospf->router_id))
322 if (nbr->state == NSM_Full)
323 if (CHECK_FLAG(nbr->options,
324 OSPF_OPTION_O))
325 count++;
326
327 return count;
2d59836a 328}
2d59836a 329
d3f0d621 330/* lookup nbr by address - use this only if you know you must
0ab4a2d6
JT
331 * otherwise use the ospf_nbr_lookup() wrapper, which deals
332 * with virtual link and PointToPoint neighbours
d3f0d621 333 */
d62a17ae 334struct ospf_neighbor *ospf_nbr_lookup_by_addr(struct route_table *nbrs,
335 struct in_addr *addr)
336{
337 struct prefix p;
338 struct route_node *rn;
339 struct ospf_neighbor *nbr;
340
341 p.family = AF_INET;
342 p.prefixlen = IPV4_MAX_BITLEN;
343 p.u.prefix4 = *addr;
344
345 rn = route_node_lookup(nbrs, &p);
346 if (!rn)
347 return NULL;
348
349 /* See comment in ospf_nbr_delete */
350 assert(rn->info);
351
352 if (rn->info == NULL) {
353 route_unlock_node(rn);
354 return NULL;
355 }
356
357 nbr = (struct ospf_neighbor *)rn->info;
358 route_unlock_node(rn);
359
360 return nbr;
361}
362
363struct ospf_neighbor *ospf_nbr_lookup_by_routerid(struct route_table *nbrs,
364 struct in_addr *id)
2d59836a 365{
d62a17ae 366 struct route_node *rn;
367 struct ospf_neighbor *nbr;
368
369 for (rn = route_top(nbrs); rn; rn = route_next(rn))
370 if ((nbr = rn->info) != NULL)
371 if (IPV4_ADDR_SAME(&nbr->router_id, id)) {
372 route_unlock_node(rn);
373 return nbr;
374 }
375
376 return NULL;
2d59836a 377}
378
d62a17ae 379void ospf_renegotiate_optional_capabilities(struct ospf *top)
2d59836a 380{
d62a17ae 381 struct listnode *node;
382 struct ospf_interface *oi;
383 struct route_table *nbrs;
384 struct route_node *rn;
385 struct ospf_neighbor *nbr;
386
387 /* At first, flush self-originated LSAs from routing domain. */
388 ospf_flush_self_originated_lsas_now(top);
389
390 /* Revert all neighbor status to ExStart. */
391 for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi)) {
392 if ((nbrs = oi->nbrs) == NULL)
393 continue;
394
395 for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
396 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
397 continue;
398
399 if (nbr->state < NSM_ExStart)
400 continue;
401
402 if (IS_DEBUG_OSPF_EVENT)
403 zlog_debug(
96b663a3
MS
404 "Renegotiate optional capabilities with neighbor(%pI4)",
405 &nbr->router_id);
d62a17ae 406
407 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
408 }
2d59836a 409 }
410
d62a17ae 411 return;
2d59836a 412}
413
d62a17ae 414
415struct ospf_neighbor *ospf_nbr_lookup(struct ospf_interface *oi, struct ip *iph,
416 struct ospf_header *ospfh)
2d59836a 417{
9e030550
MS
418 struct in_addr srcaddr = iph->ip_src;
419
d62a17ae 420 if (oi->type == OSPF_IFTYPE_VIRTUALLINK
421 || oi->type == OSPF_IFTYPE_POINTOPOINT)
422 return (ospf_nbr_lookup_by_routerid(oi->nbrs,
423 &ospfh->router_id));
424 else
9e030550 425 return (ospf_nbr_lookup_by_addr(oi->nbrs, &srcaddr));
d62a17ae 426}
2d59836a 427
d62a17ae 428static struct ospf_neighbor *ospf_nbr_add(struct ospf_interface *oi,
429 struct ospf_header *ospfh,
430 struct prefix *p)
431{
432 struct ospf_neighbor *nbr;
2d59836a 433
d62a17ae 434 nbr = ospf_nbr_new(oi);
435 nbr->state = NSM_Down;
436 nbr->src = p->u.prefix4;
437 memcpy(&nbr->address, p, sizeof(struct prefix));
2d59836a 438
d62a17ae 439 nbr->nbr_nbma = NULL;
440 if (oi->type == OSPF_IFTYPE_NBMA) {
441 struct ospf_nbr_nbma *nbr_nbma;
442 struct listnode *node;
2d59836a 443
d62a17ae 444 for (ALL_LIST_ELEMENTS_RO(oi->nbr_nbma, node, nbr_nbma)) {
445 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src)) {
446 nbr_nbma->nbr = nbr;
447 nbr->nbr_nbma = nbr_nbma;
2d59836a 448
d62a17ae 449 if (nbr_nbma->t_poll)
450 OSPF_POLL_TIMER_OFF(nbr_nbma->t_poll);
2d59836a 451
d62a17ae 452 nbr->state_change = nbr_nbma->state_change + 1;
453 }
454 }
455 }
2d59836a 456
d62a17ae 457 /* New nbr, save the crypto sequence number if necessary */
458 if (ntohs(ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
459 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
d3f0d621 460
d62a17ae 461 if (IS_DEBUG_OSPF_EVENT)
96b663a3
MS
462 zlog_debug("NSM[%s:%pI4]: start", IF_NAME(oi),
463 &nbr->router_id);
d3f0d621 464
d62a17ae 465 return nbr;
d3f0d621 466}
467
d62a17ae 468struct ospf_neighbor *ospf_nbr_get(struct ospf_interface *oi,
469 struct ospf_header *ospfh, struct ip *iph,
470 struct prefix *p)
d3f0d621 471{
d62a17ae 472 struct route_node *rn;
473 struct prefix key;
474 struct ospf_neighbor *nbr;
475
476 key.family = AF_INET;
477 key.prefixlen = IPV4_MAX_BITLEN;
478
479 if (oi->type == OSPF_IFTYPE_VIRTUALLINK
480 || oi->type == OSPF_IFTYPE_POINTOPOINT)
996c9314
LB
481 key.u.prefix4 = ospfh->router_id; /* index vlink and ptp nbrs by
482 router-id */
d62a17ae 483 else
484 key.u.prefix4 = iph->ip_src;
485
486 rn = route_node_get(oi->nbrs, &key);
487 if (rn->info) {
488 route_unlock_node(rn);
489 nbr = rn->info;
490
491 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt) {
492 nbr->src = iph->ip_src;
493 memcpy(&nbr->address, p, sizeof(struct prefix));
494 }
495 } else {
496 rn->info = nbr = ospf_nbr_add(oi, ospfh, p);
497 }
d3f0d621 498
d62a17ae 499 nbr->router_id = ospfh->router_id;
500
501 return nbr;
d3f0d621 502}