blob: 06e63dd28877aa1681eb08e3db1b8310dc3ae6ad [file] [log] [blame]
paul2d598362003-01-17 23:48:42 +00001/*
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"
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"
45
Paul Jakma478aab92006-04-03 21:25:32 +000046/* Fill in the the 'key' as appropriate to retrieve the entry for nbr
47 * from the ospf_interface's nbrs table. Indexed by interface address
Joakim Tjernlund5c1791f2014-04-25 14:36:16 +020048 * for all cases except Virtual-link and PointToPoint interfaces, where
49 * neighbours are indexed by router-ID instead.
Paul Jakma478aab92006-04-03 21:25:32 +000050 */
51static void
52ospf_nbr_key (struct ospf_interface *oi, struct ospf_neighbor *nbr,
53 struct prefix *key)
54{
55 key->family = AF_INET;
56 key->prefixlen = IPV4_MAX_BITLEN;
57
58 /* vlinks are indexed by router-id */
Joakim Tjernlund5c1791f2014-04-25 14:36:16 +020059 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
60 oi->type == OSPF_IFTYPE_POINTOPOINT)
Paul Jakma478aab92006-04-03 21:25:32 +000061 key->u.prefix4 = nbr->router_id;
62 else
63 key->u.prefix4 = nbr->src;
64 return;
65}
66
paul2d598362003-01-17 23:48:42 +000067struct ospf_neighbor *
68ospf_nbr_new (struct ospf_interface *oi)
69{
70 struct ospf_neighbor *nbr;
71
72 /* Allcate new neighbor. */
Stephen Hemminger393deb92008-08-18 14:13:29 -070073 nbr = XCALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
paul2d598362003-01-17 23:48:42 +000074
75 /* Relate neighbor to the interface. */
76 nbr->oi = oi;
77
78 /* Set default values. */
79 nbr->state = NSM_Down;
80
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;
87
88 /* DD flags. */
89 nbr->dd_flags = OSPF_DD_FLAG_MS|OSPF_DD_FLAG_M|OSPF_DD_FLAG_I;
90
91 /* Last received and sent DD. */
92 nbr->last_send = NULL;
93
94 nbr->nbr_nbma = NULL;
95
96 ospf_lsdb_init (&nbr->db_sum);
97 ospf_lsdb_init (&nbr->ls_rxmt);
98 ospf_lsdb_init (&nbr->ls_req);
99
100 nbr->crypt_seqnum = 0;
101
102 return nbr;
103}
104
105void
106ospf_nbr_free (struct ospf_neighbor *nbr)
107{
108 /* Free DB summary list. */
109 if (ospf_db_summary_count (nbr))
110 ospf_db_summary_clear (nbr);
111 /* ospf_db_summary_delete_all (nbr); */
112
113 /* Free ls request list. */
114 if (ospf_ls_request_count (nbr))
115 ospf_ls_request_delete_all (nbr);
116
117 /* Free retransmit list. */
118 if (ospf_ls_retransmit_count (nbr))
119 ospf_ls_retransmit_clear (nbr);
120
121 /* Cleanup LSDBs. */
122 ospf_lsdb_cleanup (&nbr->db_sum);
123 ospf_lsdb_cleanup (&nbr->ls_req);
124 ospf_lsdb_cleanup (&nbr->ls_rxmt);
125
126 /* Clear last send packet. */
127 if (nbr->last_send)
128 ospf_packet_free (nbr->last_send);
129
130 if (nbr->nbr_nbma)
131 {
132 nbr->nbr_nbma->nbr = NULL;
133 nbr->nbr_nbma = NULL;
134 }
135
136 /* Cancel all timers. */
137 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
138 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
139 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
140 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
141
142 /* Cancel all events. *//* Thread lookup cost would be negligible. */
143 thread_cancel_event (master, nbr);
144
145 XFREE (MTYPE_OSPF_NEIGHBOR, nbr);
146}
147
148/* Delete specified OSPF neighbor from interface. */
149void
150ospf_nbr_delete (struct ospf_neighbor *nbr)
151{
152 struct ospf_interface *oi;
153 struct route_node *rn;
154 struct prefix p;
155
156 oi = nbr->oi;
Paul Jakma478aab92006-04-03 21:25:32 +0000157
158 /* get appropriate prefix 'key' */
159 ospf_nbr_key (oi, nbr, &p);
paul2d598362003-01-17 23:48:42 +0000160
161 rn = route_node_lookup (oi->nbrs, &p);
162 if (rn)
163 {
Paul Jakma478aab92006-04-03 21:25:32 +0000164 /* If lookup for a NBR succeeds, the leaf route_node could
165 * only exist because there is (or was) a nbr there.
166 * If the nbr was deleted, the leaf route_node should have
167 * lost its last refcount too, and be deleted.
168 * Therefore a looked-up leaf route_node in nbrs table
169 * should never have NULL info.
170 */
171 assert (rn->info);
172
paul2d598362003-01-17 23:48:42 +0000173 if (rn->info)
174 {
175 rn->info = NULL;
176 route_unlock_node (rn);
177 }
178 else
179 zlog_info ("Can't find neighbor %s in the interface %s",
180 inet_ntoa (nbr->src), IF_NAME (oi));
181
182 route_unlock_node (rn);
183 }
Jafar Al-Gharaibehbb01bdd2016-04-21 16:22:33 -0500184 else
185 {
186 /*
187 * This neighbor was not found, but before we move on and
188 * free the neighbor structre, make sure that it was not
189 * indexed incorrectly and ended up in the "worng" place
190 */
191
192 /* Reverse the lookup rules */
193 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
194 oi->type == OSPF_IFTYPE_POINTOPOINT)
195 p.u.prefix4 = nbr->src;
196 else
197 p.u.prefix4 = nbr->router_id;
198
199 rn = route_node_lookup (oi->nbrs, &p);
200 if (rn){
201 /* We found the neighbor!
202 * Now make sure it is not the exact same neighbor
203 * structure that we are about to free
204 */
205 if (nbr == rn->info){
206 /* Same neighbor, drop the reference to it */
207 rn->info = NULL;
208 route_unlock_node (rn);
209 }
210 route_unlock_node (rn);
211 }
212 }
paul2d598362003-01-17 23:48:42 +0000213
214 /* Free ospf_neighbor structure. */
215 ospf_nbr_free (nbr);
216}
217
218/* Check myself is in the neighbor list. */
219int
220ospf_nbr_bidirectional (struct in_addr *router_id,
221 struct in_addr *neighbors, int size)
222{
223 int i;
224 int max;
225
226 max = size / sizeof (struct in_addr);
227
228 for (i = 0; i < max; i ++)
229 if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
230 return 1;
231
232 return 0;
233}
234
Paul Jakmac920e512015-09-08 15:31:45 +0100235/* reset nbr_self */
236void
237ospf_nbr_self_reset (struct ospf_interface *oi)
238{
Jafar Al-Gharaibehbb01bdd2016-04-21 16:22:33 -0500239 if (oi->nbr_self)
240 ospf_nbr_delete (oi->nbr_self);
241
Paul Jakmac920e512015-09-08 15:31:45 +0100242 oi->nbr_self = ospf_nbr_new (oi);
243 ospf_nbr_add_self (oi);
244}
245
paul2d598362003-01-17 23:48:42 +0000246/* Add self to nbr list. */
247void
248ospf_nbr_add_self (struct ospf_interface *oi)
249{
paul2d598362003-01-17 23:48:42 +0000250 struct prefix p;
251 struct route_node *rn;
252
paul1a643f82006-01-11 01:08:19 +0000253 /* Initial state */
254 oi->nbr_self->address = *oi->address;
255 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
256 oi->nbr_self->router_id = oi->ospf->router_id;
257 oi->nbr_self->src = oi->address->u.prefix4;
258 oi->nbr_self->state = NSM_TwoWay;
259
260 switch (oi->area->external_routing)
261 {
262 case OSPF_AREA_DEFAULT:
263 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
264 break;
265 case OSPF_AREA_STUB:
266 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
267 break;
268 case OSPF_AREA_NSSA:
269 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
270 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
271 break;
272 }
Paul Jakma478aab92006-04-03 21:25:32 +0000273
274 /* Add nbr_self to nbrs table */
275 ospf_nbr_key (oi, oi->nbr_self, &p);
276
277 rn = route_node_get (oi->nbrs, &p);
278 if (rn->info)
279 {
280 /* There is already pseudo neighbor. */
281 assert (oi->nbr_self == rn->info);
282 route_unlock_node (rn);
283 }
284 else
285 rn->info = oi->nbr_self;
paul2d598362003-01-17 23:48:42 +0000286}
287
288/* Get neighbor count by status.
289 Specify status = 0, get all neighbor other than myself. */
290int
paul68980082003-03-25 05:07:42 +0000291ospf_nbr_count (struct ospf_interface *oi, int state)
paul2d598362003-01-17 23:48:42 +0000292{
paul2d598362003-01-17 23:48:42 +0000293 struct ospf_neighbor *nbr;
paul68980082003-03-25 05:07:42 +0000294 struct route_node *rn;
paul2d598362003-01-17 23:48:42 +0000295 int count = 0;
296
paul68980082003-03-25 05:07:42 +0000297 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
298 if ((nbr = rn->info))
299 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
paul2d598362003-01-17 23:48:42 +0000300 if (state == 0 || nbr->state == state)
301 count++;
302
303 return count;
304}
305
paul2d598362003-01-17 23:48:42 +0000306int
paul68980082003-03-25 05:07:42 +0000307ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
paul2d598362003-01-17 23:48:42 +0000308{
paul2d598362003-01-17 23:48:42 +0000309 struct ospf_neighbor *nbr;
paul68980082003-03-25 05:07:42 +0000310 struct route_node *rn;
paul2d598362003-01-17 23:48:42 +0000311 int count = 0;
312
paul68980082003-03-25 05:07:42 +0000313 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
314 if ((nbr = rn->info))
315 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
316 if (nbr->state == NSM_Full)
317 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
318 count++;
paul2d598362003-01-17 23:48:42 +0000319
320 return count;
321}
paul2d598362003-01-17 23:48:42 +0000322
pauld3f0d622004-05-05 15:27:15 +0000323/* lookup nbr by address - use this only if you know you must
Joakim Tjernlund5c1791f2014-04-25 14:36:16 +0200324 * otherwise use the ospf_nbr_lookup() wrapper, which deals
325 * with virtual link and PointToPoint neighbours
pauld3f0d622004-05-05 15:27:15 +0000326 */
paul2d598362003-01-17 23:48:42 +0000327struct ospf_neighbor *
328ospf_nbr_lookup_by_addr (struct route_table *nbrs,
329 struct in_addr *addr)
330{
331 struct prefix p;
332 struct route_node *rn;
333 struct ospf_neighbor *nbr;
334
335 p.family = AF_INET;
336 p.prefixlen = IPV4_MAX_BITLEN;
337 p.u.prefix4 = *addr;
338
339 rn = route_node_lookup (nbrs, &p);
340 if (! rn)
341 return NULL;
Paul Jakma478aab92006-04-03 21:25:32 +0000342
343 /* See comment in ospf_nbr_delete */
344 assert (rn->info);
paul2d598362003-01-17 23:48:42 +0000345
346 if (rn->info == NULL)
347 {
348 route_unlock_node (rn);
349 return NULL;
350 }
351
352 nbr = (struct ospf_neighbor *) rn->info;
353 route_unlock_node (rn);
354
355 return nbr;
356}
357
358struct ospf_neighbor *
359ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
360 struct in_addr *id)
361{
362 struct route_node *rn;
363 struct ospf_neighbor *nbr;
364
365 for (rn = route_top (nbrs); rn; rn = route_next (rn))
366 if ((nbr = rn->info) != NULL)
367 if (IPV4_ADDR_SAME (&nbr->router_id, id))
368 {
369 route_unlock_node(rn);
370 return nbr;
371 }
372
373 return NULL;
374}
375
376void
377ospf_renegotiate_optional_capabilities (struct ospf *top)
378{
hasso52dc7ee2004-09-23 19:18:23 +0000379 struct listnode *node;
paul2d598362003-01-17 23:48:42 +0000380 struct ospf_interface *oi;
381 struct route_table *nbrs;
382 struct route_node *rn;
383 struct ospf_neighbor *nbr;
384
385 /* At first, flush self-originated LSAs from routing domain. */
386 ospf_flush_self_originated_lsas_now (top);
387
388 /* Revert all neighbor status to ExStart. */
paul1eb8ef22005-04-07 07:30:20 +0000389 for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
paul2d598362003-01-17 23:48:42 +0000390 {
paul1eb8ef22005-04-07 07:30:20 +0000391 if ((nbrs = oi->nbrs) == NULL)
paul2d598362003-01-17 23:48:42 +0000392 continue;
393
394 for (rn = route_top (nbrs); rn; rn = route_next (rn))
395 {
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)
ajse588f212004-12-08 18:12:06 +0000403 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
paul2d598362003-01-17 23:48:42 +0000404
405 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
406 }
407 }
408
409 return;
410}
pauld3f0d622004-05-05 15:27:15 +0000411
412
413struct ospf_neighbor *
414ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
415 struct ospf_header *ospfh)
416{
Joakim Tjernlund5c1791f2014-04-25 14:36:16 +0200417 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
418 oi->type == OSPF_IFTYPE_POINTOPOINT)
pauld3f0d622004-05-05 15:27:15 +0000419 return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
420 else
421 return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
422}
423
paul4dadc292005-05-06 21:37:42 +0000424static struct ospf_neighbor *
pauld3f0d622004-05-05 15:27:15 +0000425ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
426 struct prefix *p)
427{
428 struct ospf_neighbor *nbr;
429
430 nbr = ospf_nbr_new (oi);
431 nbr->state = NSM_Down;
432 nbr->src = p->u.prefix4;
433 memcpy (&nbr->address, p, sizeof (struct prefix));
434
435 nbr->nbr_nbma = NULL;
436 if (oi->type == OSPF_IFTYPE_NBMA)
437 {
438 struct ospf_nbr_nbma *nbr_nbma;
hasso52dc7ee2004-09-23 19:18:23 +0000439 struct listnode *node;
pauld3f0d622004-05-05 15:27:15 +0000440
paul1eb8ef22005-04-07 07:30:20 +0000441 for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
pauld3f0d622004-05-05 15:27:15 +0000442 {
pauld3f0d622004-05-05 15:27:15 +0000443 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
444 {
445 nbr_nbma->nbr = nbr;
446 nbr->nbr_nbma = nbr_nbma;
447
448 if (nbr_nbma->t_poll)
449 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
450
451 nbr->state_change = nbr_nbma->state_change + 1;
452 }
453 }
454 }
455
456 /* New nbr, save the crypto sequence number if necessary */
457 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
458 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
459
460 if (IS_DEBUG_OSPF_EVENT)
ajse588f212004-12-08 18:12:06 +0000461 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
pauld3f0d622004-05-05 15:27:15 +0000462 inet_ntoa (nbr->router_id));
463
464 return nbr;
465}
466
467struct ospf_neighbor *
468ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
469 struct ip *iph, struct prefix *p)
470{
471 struct route_node *rn;
472 struct prefix key;
473 struct ospf_neighbor *nbr;
474
475 key.family = AF_INET;
476 key.prefixlen = IPV4_MAX_BITLEN;
477
Joakim Tjernlund5c1791f2014-04-25 14:36:16 +0200478 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
479 oi->type == OSPF_IFTYPE_POINTOPOINT)
480 key.u.prefix4 = ospfh->router_id;/* index vlink and ptp nbrs by router-id */
pauld3f0d622004-05-05 15:27:15 +0000481 else
482 key.u.prefix4 = iph->ip_src;
483
484 rn = route_node_get (oi->nbrs, &key);
485 if (rn->info)
486 {
487 route_unlock_node (rn);
488 nbr = rn->info;
489
490 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
491 {
492 nbr->src = iph->ip_src;
493 memcpy (&nbr->address, p, sizeof (struct prefix));
494 }
495 }
496 else
497 {
498 rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
499 }
500
501 nbr->router_id = ospfh->router_id;
502
503 return nbr;
504}