]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_neighbor.c
tests: Fix warnings from test-stream.c
[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.
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"
68fe91d6 33#include "json.h"
2d59836a 34
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"
68fe91d6 46#include "ospfd/ospf_bfd.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
50 * for all cases except Virtual-link interfaces, where neighbours are
51 * indexed by router-ID instead.
52 */
53static void
54ospf_nbr_key (struct ospf_interface *oi, struct ospf_neighbor *nbr,
55 struct prefix *key)
56{
57 key->family = AF_INET;
58 key->prefixlen = IPV4_MAX_BITLEN;
59
60 /* vlinks are indexed by router-id */
61 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
62 key->u.prefix4 = nbr->router_id;
63 else
64 key->u.prefix4 = nbr->src;
65 return;
66}
67
2d59836a 68struct ospf_neighbor *
69ospf_nbr_new (struct ospf_interface *oi)
70{
71 struct ospf_neighbor *nbr;
72
73 /* Allcate new neighbor. */
393deb9b 74 nbr = XCALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
2d59836a 75
76 /* Relate neighbor to the interface. */
77 nbr->oi = oi;
78
79 /* Set default values. */
80 nbr->state = NSM_Down;
81
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);
87 nbr->priority = -1;
88
89 /* DD flags. */
90 nbr->dd_flags = OSPF_DD_FLAG_MS|OSPF_DD_FLAG_M|OSPF_DD_FLAG_I;
91
92 /* Last received and sent DD. */
93 nbr->last_send = NULL;
94
95 nbr->nbr_nbma = NULL;
96
97 ospf_lsdb_init (&nbr->db_sum);
98 ospf_lsdb_init (&nbr->ls_rxmt);
99 ospf_lsdb_init (&nbr->ls_req);
100
101 nbr->crypt_seqnum = 0;
102
68fe91d6 103 ospf_bfd_info_nbr_create(oi, nbr);
2d59836a 104 return nbr;
105}
106
107void
108ospf_nbr_free (struct ospf_neighbor *nbr)
109{
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); */
114
115 /* Free ls request list. */
116 if (ospf_ls_request_count (nbr))
117 ospf_ls_request_delete_all (nbr);
118
119 /* Free retransmit list. */
120 if (ospf_ls_retransmit_count (nbr))
121 ospf_ls_retransmit_clear (nbr);
122
123 /* Cleanup LSDBs. */
124 ospf_lsdb_cleanup (&nbr->db_sum);
125 ospf_lsdb_cleanup (&nbr->ls_req);
126 ospf_lsdb_cleanup (&nbr->ls_rxmt);
127
128 /* Clear last send packet. */
129 if (nbr->last_send)
130 ospf_packet_free (nbr->last_send);
131
132 if (nbr->nbr_nbma)
133 {
134 nbr->nbr_nbma->nbr = NULL;
135 nbr->nbr_nbma = NULL;
136 }
137
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);
143
144 /* Cancel all events. *//* Thread lookup cost would be negligible. */
145 thread_cancel_event (master, nbr);
146
68fe91d6 147 ospf_bfd_info_free(&nbr->bfd_info);
2d59836a 148 XFREE (MTYPE_OSPF_NEIGHBOR, nbr);
149}
150
151/* Delete specified OSPF neighbor from interface. */
152void
153ospf_nbr_delete (struct ospf_neighbor *nbr)
154{
155 struct ospf_interface *oi;
156 struct route_node *rn;
157 struct prefix p;
158
159 oi = nbr->oi;
478aab98
PJ
160
161 /* get appropriate prefix 'key' */
162 ospf_nbr_key (oi, nbr, &p);
2d59836a 163
164 rn = route_node_lookup (oi->nbrs, &p);
165 if (rn)
166 {
478aab98
PJ
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.
173 */
174 assert (rn->info);
175
2d59836a 176 if (rn->info)
177 {
178 rn->info = NULL;
179 route_unlock_node (rn);
180 }
181 else
182 zlog_info ("Can't find neighbor %s in the interface %s",
183 inet_ntoa (nbr->src), IF_NAME (oi));
184
185 route_unlock_node (rn);
186 }
187
188 /* Free ospf_neighbor structure. */
189 ospf_nbr_free (nbr);
190}
191
192/* Check myself is in the neighbor list. */
193int
194ospf_nbr_bidirectional (struct in_addr *router_id,
195 struct in_addr *neighbors, int size)
196{
197 int i;
198 int max;
199
200 max = size / sizeof (struct in_addr);
201
202 for (i = 0; i < max; i ++)
203 if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
204 return 1;
205
206 return 0;
207}
208
cdd0c849
PJ
209/* reset nbr_self */
210void
211ospf_nbr_self_reset (struct ospf_interface *oi)
212{
213 ospf_nbr_delete (oi->nbr_self);
214 oi->nbr_self = ospf_nbr_new (oi);
215 ospf_nbr_add_self (oi);
216}
217
2d59836a 218/* Add self to nbr list. */
219void
220ospf_nbr_add_self (struct ospf_interface *oi)
221{
2d59836a 222 struct prefix p;
223 struct route_node *rn;
224
1a643f88 225 /* Initial state */
226 oi->nbr_self->address = *oi->address;
227 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
228 oi->nbr_self->router_id = oi->ospf->router_id;
229 oi->nbr_self->src = oi->address->u.prefix4;
230 oi->nbr_self->state = NSM_TwoWay;
231
232 switch (oi->area->external_routing)
233 {
234 case OSPF_AREA_DEFAULT:
235 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
236 break;
237 case OSPF_AREA_STUB:
238 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
239 break;
240 case OSPF_AREA_NSSA:
241 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
242 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
243 break;
244 }
478aab98
PJ
245
246 /* Add nbr_self to nbrs table */
247 ospf_nbr_key (oi, oi->nbr_self, &p);
248
249 rn = route_node_get (oi->nbrs, &p);
250 if (rn->info)
251 {
252 /* There is already pseudo neighbor. */
253 assert (oi->nbr_self == rn->info);
254 route_unlock_node (rn);
255 }
256 else
257 rn->info = oi->nbr_self;
2d59836a 258}
259
260/* Get neighbor count by status.
261 Specify status = 0, get all neighbor other than myself. */
262int
68980084 263ospf_nbr_count (struct ospf_interface *oi, int state)
2d59836a 264{
2d59836a 265 struct ospf_neighbor *nbr;
68980084 266 struct route_node *rn;
2d59836a 267 int count = 0;
268
68980084 269 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
270 if ((nbr = rn->info))
271 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
2d59836a 272 if (state == 0 || nbr->state == state)
273 count++;
274
275 return count;
276}
277
278#ifdef HAVE_OPAQUE_LSA
279int
68980084 280ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
2d59836a 281{
2d59836a 282 struct ospf_neighbor *nbr;
68980084 283 struct route_node *rn;
2d59836a 284 int count = 0;
285
68980084 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, &oi->ospf->router_id))
289 if (nbr->state == NSM_Full)
290 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
291 count++;
2d59836a 292
293 return count;
294}
295#endif /* HAVE_OPAQUE_LSA */
296
d3f0d621 297/* lookup nbr by address - use this only if you know you must
298 * otherwise use the ospf_nbr_lookup() wrapper, which deals
299 * with virtual link neighbours
300 */
2d59836a 301struct ospf_neighbor *
302ospf_nbr_lookup_by_addr (struct route_table *nbrs,
303 struct in_addr *addr)
304{
305 struct prefix p;
306 struct route_node *rn;
307 struct ospf_neighbor *nbr;
308
309 p.family = AF_INET;
310 p.prefixlen = IPV4_MAX_BITLEN;
311 p.u.prefix4 = *addr;
312
313 rn = route_node_lookup (nbrs, &p);
314 if (! rn)
315 return NULL;
478aab98
PJ
316
317 /* See comment in ospf_nbr_delete */
318 assert (rn->info);
2d59836a 319
320 if (rn->info == NULL)
321 {
322 route_unlock_node (rn);
323 return NULL;
324 }
325
326 nbr = (struct ospf_neighbor *) rn->info;
327 route_unlock_node (rn);
328
329 return nbr;
330}
331
332struct ospf_neighbor *
333ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
334 struct in_addr *id)
335{
336 struct route_node *rn;
337 struct ospf_neighbor *nbr;
338
339 for (rn = route_top (nbrs); rn; rn = route_next (rn))
340 if ((nbr = rn->info) != NULL)
341 if (IPV4_ADDR_SAME (&nbr->router_id, id))
342 {
343 route_unlock_node(rn);
344 return nbr;
345 }
346
347 return NULL;
348}
349
350void
351ospf_renegotiate_optional_capabilities (struct ospf *top)
352{
52dc7ee6 353 struct listnode *node;
2d59836a 354 struct ospf_interface *oi;
355 struct route_table *nbrs;
356 struct route_node *rn;
357 struct ospf_neighbor *nbr;
358
359 /* At first, flush self-originated LSAs from routing domain. */
360 ospf_flush_self_originated_lsas_now (top);
361
362 /* Revert all neighbor status to ExStart. */
1eb8ef25 363 for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
2d59836a 364 {
1eb8ef25 365 if ((nbrs = oi->nbrs) == NULL)
2d59836a 366 continue;
367
368 for (rn = route_top (nbrs); rn; rn = route_next (rn))
369 {
370 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
371 continue;
372
373 if (nbr->state < NSM_ExStart)
374 continue;
375
376 if (IS_DEBUG_OSPF_EVENT)
e588f21f 377 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
2d59836a 378
379 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
380 }
381 }
382
383 return;
384}
d3f0d621 385
386
387struct ospf_neighbor *
388ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
389 struct ospf_header *ospfh)
390{
391 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
392 return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
393 else
394 return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
395}
396
4dadc291 397static struct ospf_neighbor *
d3f0d621 398ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
399 struct prefix *p)
400{
401 struct ospf_neighbor *nbr;
402
403 nbr = ospf_nbr_new (oi);
404 nbr->state = NSM_Down;
405 nbr->src = p->u.prefix4;
406 memcpy (&nbr->address, p, sizeof (struct prefix));
407
408 nbr->nbr_nbma = NULL;
409 if (oi->type == OSPF_IFTYPE_NBMA)
410 {
411 struct ospf_nbr_nbma *nbr_nbma;
52dc7ee6 412 struct listnode *node;
d3f0d621 413
1eb8ef25 414 for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
d3f0d621 415 {
d3f0d621 416 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
417 {
418 nbr_nbma->nbr = nbr;
419 nbr->nbr_nbma = nbr_nbma;
420
421 if (nbr_nbma->t_poll)
422 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
423
424 nbr->state_change = nbr_nbma->state_change + 1;
425 }
426 }
427 }
428
429 /* New nbr, save the crypto sequence number if necessary */
430 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
431 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
432
433 if (IS_DEBUG_OSPF_EVENT)
e588f21f 434 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
d3f0d621 435 inet_ntoa (nbr->router_id));
436
437 return nbr;
438}
439
440struct ospf_neighbor *
441ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
442 struct ip *iph, struct prefix *p)
443{
444 struct route_node *rn;
445 struct prefix key;
446 struct ospf_neighbor *nbr;
447
448 key.family = AF_INET;
449 key.prefixlen = IPV4_MAX_BITLEN;
450
451 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
452 key.u.prefix4 = ospfh->router_id; /* index vlink nbrs by router-id */
453 else
454 key.u.prefix4 = iph->ip_src;
455
456 rn = route_node_get (oi->nbrs, &key);
457 if (rn->info)
458 {
459 route_unlock_node (rn);
460 nbr = rn->info;
461
462 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
463 {
464 nbr->src = iph->ip_src;
465 memcpy (&nbr->address, p, sizeof (struct prefix));
466 }
467 }
468 else
469 {
470 rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
471 }
472
473 nbr->router_id = ospfh->router_id;
474
475 return nbr;
476}