blob: c3bdf99295a61ad9da9138b7102dbcf694ccd95c [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
48 * for all cases except Virtual-link interfaces, where neighbours are
49 * indexed by router-ID instead.
50 */
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 */
59 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
60 key->u.prefix4 = nbr->router_id;
61 else
62 key->u.prefix4 = nbr->src;
63 return;
64}
65
paul2d598362003-01-17 23:48:42 +000066struct ospf_neighbor *
67ospf_nbr_new (struct ospf_interface *oi)
68{
69 struct ospf_neighbor *nbr;
70
71 /* Allcate new neighbor. */
Stephen Hemminger393deb92008-08-18 14:13:29 -070072 nbr = XCALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
paul2d598362003-01-17 23:48:42 +000073
74 /* Relate neighbor to the interface. */
75 nbr->oi = oi;
76
77 /* Set default values. */
78 nbr->state = NSM_Down;
79
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);
85 nbr->priority = -1;
86
87 /* DD flags. */
88 nbr->dd_flags = OSPF_DD_FLAG_MS|OSPF_DD_FLAG_M|OSPF_DD_FLAG_I;
89
90 /* Last received and sent DD. */
91 nbr->last_send = NULL;
92
93 nbr->nbr_nbma = NULL;
94
95 ospf_lsdb_init (&nbr->db_sum);
96 ospf_lsdb_init (&nbr->ls_rxmt);
97 ospf_lsdb_init (&nbr->ls_req);
98
99 nbr->crypt_seqnum = 0;
100
101 return nbr;
102}
103
104void
105ospf_nbr_free (struct ospf_neighbor *nbr)
106{
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); */
111
112 /* Free ls request list. */
113 if (ospf_ls_request_count (nbr))
114 ospf_ls_request_delete_all (nbr);
115
116 /* Free retransmit list. */
117 if (ospf_ls_retransmit_count (nbr))
118 ospf_ls_retransmit_clear (nbr);
119
120 /* Cleanup LSDBs. */
121 ospf_lsdb_cleanup (&nbr->db_sum);
122 ospf_lsdb_cleanup (&nbr->ls_req);
123 ospf_lsdb_cleanup (&nbr->ls_rxmt);
124
125 /* Clear last send packet. */
126 if (nbr->last_send)
127 ospf_packet_free (nbr->last_send);
128
129 if (nbr->nbr_nbma)
130 {
131 nbr->nbr_nbma->nbr = NULL;
132 nbr->nbr_nbma = NULL;
133 }
134
135 /* Cancel all timers. */
136 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
137 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
138 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
139 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
140
141 /* Cancel all events. *//* Thread lookup cost would be negligible. */
142 thread_cancel_event (master, nbr);
143
144 XFREE (MTYPE_OSPF_NEIGHBOR, nbr);
145}
146
147/* Delete specified OSPF neighbor from interface. */
148void
149ospf_nbr_delete (struct ospf_neighbor *nbr)
150{
151 struct ospf_interface *oi;
152 struct route_node *rn;
153 struct prefix p;
154
155 oi = nbr->oi;
Paul Jakma478aab92006-04-03 21:25:32 +0000156
157 /* get appropriate prefix 'key' */
158 ospf_nbr_key (oi, nbr, &p);
paul2d598362003-01-17 23:48:42 +0000159
160 rn = route_node_lookup (oi->nbrs, &p);
161 if (rn)
162 {
Paul Jakma478aab92006-04-03 21:25:32 +0000163 /* If lookup for a NBR succeeds, the leaf route_node could
164 * only exist because there is (or was) a nbr there.
165 * If the nbr was deleted, the leaf route_node should have
166 * lost its last refcount too, and be deleted.
167 * Therefore a looked-up leaf route_node in nbrs table
168 * should never have NULL info.
169 */
170 assert (rn->info);
171
paul2d598362003-01-17 23:48:42 +0000172 if (rn->info)
173 {
174 rn->info = NULL;
175 route_unlock_node (rn);
176 }
177 else
178 zlog_info ("Can't find neighbor %s in the interface %s",
179 inet_ntoa (nbr->src), IF_NAME (oi));
180
181 route_unlock_node (rn);
182 }
183
184 /* Free ospf_neighbor structure. */
185 ospf_nbr_free (nbr);
186}
187
188/* Check myself is in the neighbor list. */
189int
190ospf_nbr_bidirectional (struct in_addr *router_id,
191 struct in_addr *neighbors, int size)
192{
193 int i;
194 int max;
195
196 max = size / sizeof (struct in_addr);
197
198 for (i = 0; i < max; i ++)
199 if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
200 return 1;
201
202 return 0;
203}
204
Paul Jakmac920e512015-09-08 15:31:45 +0100205/* reset nbr_self */
206void
207ospf_nbr_self_reset (struct ospf_interface *oi)
208{
209 ospf_nbr_delete (oi->nbr_self);
210 oi->nbr_self = ospf_nbr_new (oi);
211 ospf_nbr_add_self (oi);
212}
213
paul2d598362003-01-17 23:48:42 +0000214/* Add self to nbr list. */
215void
216ospf_nbr_add_self (struct ospf_interface *oi)
217{
paul2d598362003-01-17 23:48:42 +0000218 struct prefix p;
219 struct route_node *rn;
220
paul1a643f82006-01-11 01:08:19 +0000221 /* Initial state */
222 oi->nbr_self->address = *oi->address;
223 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
224 oi->nbr_self->router_id = oi->ospf->router_id;
225 oi->nbr_self->src = oi->address->u.prefix4;
226 oi->nbr_self->state = NSM_TwoWay;
227
228 switch (oi->area->external_routing)
229 {
230 case OSPF_AREA_DEFAULT:
231 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
232 break;
233 case OSPF_AREA_STUB:
234 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
235 break;
236 case OSPF_AREA_NSSA:
237 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
238 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
239 break;
240 }
Paul Jakma478aab92006-04-03 21:25:32 +0000241
242 /* Add nbr_self to nbrs table */
243 ospf_nbr_key (oi, oi->nbr_self, &p);
244
245 rn = route_node_get (oi->nbrs, &p);
246 if (rn->info)
247 {
248 /* There is already pseudo neighbor. */
249 assert (oi->nbr_self == rn->info);
250 route_unlock_node (rn);
251 }
252 else
253 rn->info = oi->nbr_self;
paul2d598362003-01-17 23:48:42 +0000254}
255
256/* Get neighbor count by status.
257 Specify status = 0, get all neighbor other than myself. */
258int
paul68980082003-03-25 05:07:42 +0000259ospf_nbr_count (struct ospf_interface *oi, int state)
paul2d598362003-01-17 23:48:42 +0000260{
paul2d598362003-01-17 23:48:42 +0000261 struct ospf_neighbor *nbr;
paul68980082003-03-25 05:07:42 +0000262 struct route_node *rn;
paul2d598362003-01-17 23:48:42 +0000263 int count = 0;
264
paul68980082003-03-25 05:07:42 +0000265 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
266 if ((nbr = rn->info))
267 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
paul2d598362003-01-17 23:48:42 +0000268 if (state == 0 || nbr->state == state)
269 count++;
270
271 return count;
272}
273
274#ifdef HAVE_OPAQUE_LSA
275int
paul68980082003-03-25 05:07:42 +0000276ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
paul2d598362003-01-17 23:48:42 +0000277{
paul2d598362003-01-17 23:48:42 +0000278 struct ospf_neighbor *nbr;
paul68980082003-03-25 05:07:42 +0000279 struct route_node *rn;
paul2d598362003-01-17 23:48:42 +0000280 int count = 0;
281
paul68980082003-03-25 05:07:42 +0000282 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
283 if ((nbr = rn->info))
284 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
285 if (nbr->state == NSM_Full)
286 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
287 count++;
paul2d598362003-01-17 23:48:42 +0000288
289 return count;
290}
291#endif /* HAVE_OPAQUE_LSA */
292
pauld3f0d622004-05-05 15:27:15 +0000293/* lookup nbr by address - use this only if you know you must
294 * otherwise use the ospf_nbr_lookup() wrapper, which deals
295 * with virtual link neighbours
296 */
paul2d598362003-01-17 23:48:42 +0000297struct ospf_neighbor *
298ospf_nbr_lookup_by_addr (struct route_table *nbrs,
299 struct in_addr *addr)
300{
301 struct prefix p;
302 struct route_node *rn;
303 struct ospf_neighbor *nbr;
304
305 p.family = AF_INET;
306 p.prefixlen = IPV4_MAX_BITLEN;
307 p.u.prefix4 = *addr;
308
309 rn = route_node_lookup (nbrs, &p);
310 if (! rn)
311 return NULL;
Paul Jakma478aab92006-04-03 21:25:32 +0000312
313 /* See comment in ospf_nbr_delete */
314 assert (rn->info);
paul2d598362003-01-17 23:48:42 +0000315
316 if (rn->info == NULL)
317 {
318 route_unlock_node (rn);
319 return NULL;
320 }
321
322 nbr = (struct ospf_neighbor *) rn->info;
323 route_unlock_node (rn);
324
325 return nbr;
326}
327
328struct ospf_neighbor *
329ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
330 struct in_addr *id)
331{
332 struct route_node *rn;
333 struct ospf_neighbor *nbr;
334
335 for (rn = route_top (nbrs); rn; rn = route_next (rn))
336 if ((nbr = rn->info) != NULL)
337 if (IPV4_ADDR_SAME (&nbr->router_id, id))
338 {
339 route_unlock_node(rn);
340 return nbr;
341 }
342
343 return NULL;
344}
345
346void
347ospf_renegotiate_optional_capabilities (struct ospf *top)
348{
hasso52dc7ee2004-09-23 19:18:23 +0000349 struct listnode *node;
paul2d598362003-01-17 23:48:42 +0000350 struct ospf_interface *oi;
351 struct route_table *nbrs;
352 struct route_node *rn;
353 struct ospf_neighbor *nbr;
354
355 /* At first, flush self-originated LSAs from routing domain. */
356 ospf_flush_self_originated_lsas_now (top);
357
358 /* Revert all neighbor status to ExStart. */
paul1eb8ef22005-04-07 07:30:20 +0000359 for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
paul2d598362003-01-17 23:48:42 +0000360 {
paul1eb8ef22005-04-07 07:30:20 +0000361 if ((nbrs = oi->nbrs) == NULL)
paul2d598362003-01-17 23:48:42 +0000362 continue;
363
364 for (rn = route_top (nbrs); rn; rn = route_next (rn))
365 {
366 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
367 continue;
368
369 if (nbr->state < NSM_ExStart)
370 continue;
371
372 if (IS_DEBUG_OSPF_EVENT)
ajse588f212004-12-08 18:12:06 +0000373 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
paul2d598362003-01-17 23:48:42 +0000374
375 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
376 }
377 }
378
379 return;
380}
pauld3f0d622004-05-05 15:27:15 +0000381
382
383struct ospf_neighbor *
384ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
385 struct ospf_header *ospfh)
386{
387 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
388 return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
389 else
390 return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
391}
392
paul4dadc292005-05-06 21:37:42 +0000393static struct ospf_neighbor *
pauld3f0d622004-05-05 15:27:15 +0000394ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
395 struct prefix *p)
396{
397 struct ospf_neighbor *nbr;
398
399 nbr = ospf_nbr_new (oi);
400 nbr->state = NSM_Down;
401 nbr->src = p->u.prefix4;
402 memcpy (&nbr->address, p, sizeof (struct prefix));
403
404 nbr->nbr_nbma = NULL;
405 if (oi->type == OSPF_IFTYPE_NBMA)
406 {
407 struct ospf_nbr_nbma *nbr_nbma;
hasso52dc7ee2004-09-23 19:18:23 +0000408 struct listnode *node;
pauld3f0d622004-05-05 15:27:15 +0000409
paul1eb8ef22005-04-07 07:30:20 +0000410 for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
pauld3f0d622004-05-05 15:27:15 +0000411 {
pauld3f0d622004-05-05 15:27:15 +0000412 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
413 {
414 nbr_nbma->nbr = nbr;
415 nbr->nbr_nbma = nbr_nbma;
416
417 if (nbr_nbma->t_poll)
418 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
419
420 nbr->state_change = nbr_nbma->state_change + 1;
421 }
422 }
423 }
424
425 /* New nbr, save the crypto sequence number if necessary */
426 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
427 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
428
429 if (IS_DEBUG_OSPF_EVENT)
ajse588f212004-12-08 18:12:06 +0000430 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
pauld3f0d622004-05-05 15:27:15 +0000431 inet_ntoa (nbr->router_id));
432
433 return nbr;
434}
435
436struct ospf_neighbor *
437ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
438 struct ip *iph, struct prefix *p)
439{
440 struct route_node *rn;
441 struct prefix key;
442 struct ospf_neighbor *nbr;
443
444 key.family = AF_INET;
445 key.prefixlen = IPV4_MAX_BITLEN;
446
447 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
448 key.u.prefix4 = ospfh->router_id; /* index vlink nbrs by router-id */
449 else
450 key.u.prefix4 = iph->ip_src;
451
452 rn = route_node_get (oi->nbrs, &key);
453 if (rn->info)
454 {
455 route_unlock_node (rn);
456 nbr = rn->info;
457
458 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
459 {
460 nbr->src = iph->ip_src;
461 memcpy (&nbr->address, p, sizeof (struct prefix));
462 }
463 }
464 else
465 {
466 rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
467 }
468
469 nbr->router_id = ospfh->router_id;
470
471 return nbr;
472}