]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_neighbor.c
[bgpd] Add support for the old Linux 2.4, TCP_MD5_AUTH RFC2385 patch
[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"
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
478aab98
PJ
46/* 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
2d59836a 66struct ospf_neighbor *
67ospf_nbr_new (struct ospf_interface *oi)
68{
69 struct ospf_neighbor *nbr;
70
71 /* Allcate new neighbor. */
72 nbr = XMALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
73 memset (nbr, 0, sizeof (struct ospf_neighbor));
74
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;
478aab98
PJ
157
158 /* get appropriate prefix 'key' */
159 ospf_nbr_key (oi, nbr, &p);
2d59836a 160
161 rn = route_node_lookup (oi->nbrs, &p);
162 if (rn)
163 {
478aab98
PJ
164 /* 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
2d59836a 173 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 }
184
185 /* Free ospf_neighbor structure. */
186 ospf_nbr_free (nbr);
187}
188
189/* Check myself is in the neighbor list. */
190int
191ospf_nbr_bidirectional (struct in_addr *router_id,
192 struct in_addr *neighbors, int size)
193{
194 int i;
195 int max;
196
197 max = size / sizeof (struct in_addr);
198
199 for (i = 0; i < max; i ++)
200 if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
201 return 1;
202
203 return 0;
204}
205
206/* Add self to nbr list. */
207void
208ospf_nbr_add_self (struct ospf_interface *oi)
209{
2d59836a 210 struct prefix p;
211 struct route_node *rn;
212
1a643f88 213 /* Initial state */
214 oi->nbr_self->address = *oi->address;
215 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
216 oi->nbr_self->router_id = oi->ospf->router_id;
217 oi->nbr_self->src = oi->address->u.prefix4;
218 oi->nbr_self->state = NSM_TwoWay;
219
220 switch (oi->area->external_routing)
221 {
222 case OSPF_AREA_DEFAULT:
223 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
224 break;
225 case OSPF_AREA_STUB:
226 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
227 break;
228 case OSPF_AREA_NSSA:
229 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
230 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
231 break;
232 }
478aab98
PJ
233
234 /* Add nbr_self to nbrs table */
235 ospf_nbr_key (oi, oi->nbr_self, &p);
236
237 rn = route_node_get (oi->nbrs, &p);
238 if (rn->info)
239 {
240 /* There is already pseudo neighbor. */
241 assert (oi->nbr_self == rn->info);
242 route_unlock_node (rn);
243 }
244 else
245 rn->info = oi->nbr_self;
2d59836a 246}
247
248/* Get neighbor count by status.
249 Specify status = 0, get all neighbor other than myself. */
250int
68980084 251ospf_nbr_count (struct ospf_interface *oi, int state)
2d59836a 252{
2d59836a 253 struct ospf_neighbor *nbr;
68980084 254 struct route_node *rn;
2d59836a 255 int count = 0;
256
68980084 257 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
258 if ((nbr = rn->info))
259 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
2d59836a 260 if (state == 0 || nbr->state == state)
261 count++;
262
263 return count;
264}
265
266#ifdef HAVE_OPAQUE_LSA
267int
68980084 268ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
2d59836a 269{
2d59836a 270 struct ospf_neighbor *nbr;
68980084 271 struct route_node *rn;
2d59836a 272 int count = 0;
273
68980084 274 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
275 if ((nbr = rn->info))
276 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
277 if (nbr->state == NSM_Full)
278 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
279 count++;
2d59836a 280
281 return count;
282}
283#endif /* HAVE_OPAQUE_LSA */
284
d3f0d621 285/* lookup nbr by address - use this only if you know you must
286 * otherwise use the ospf_nbr_lookup() wrapper, which deals
287 * with virtual link neighbours
288 */
2d59836a 289struct ospf_neighbor *
290ospf_nbr_lookup_by_addr (struct route_table *nbrs,
291 struct in_addr *addr)
292{
293 struct prefix p;
294 struct route_node *rn;
295 struct ospf_neighbor *nbr;
296
297 p.family = AF_INET;
298 p.prefixlen = IPV4_MAX_BITLEN;
299 p.u.prefix4 = *addr;
300
301 rn = route_node_lookup (nbrs, &p);
302 if (! rn)
303 return NULL;
478aab98
PJ
304
305 /* See comment in ospf_nbr_delete */
306 assert (rn->info);
2d59836a 307
308 if (rn->info == NULL)
309 {
310 route_unlock_node (rn);
311 return NULL;
312 }
313
314 nbr = (struct ospf_neighbor *) rn->info;
315 route_unlock_node (rn);
316
317 return nbr;
318}
319
320struct ospf_neighbor *
321ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
322 struct in_addr *id)
323{
324 struct route_node *rn;
325 struct ospf_neighbor *nbr;
326
327 for (rn = route_top (nbrs); rn; rn = route_next (rn))
328 if ((nbr = rn->info) != NULL)
329 if (IPV4_ADDR_SAME (&nbr->router_id, id))
330 {
331 route_unlock_node(rn);
332 return nbr;
333 }
334
335 return NULL;
336}
337
338void
339ospf_renegotiate_optional_capabilities (struct ospf *top)
340{
52dc7ee6 341 struct listnode *node;
2d59836a 342 struct ospf_interface *oi;
343 struct route_table *nbrs;
344 struct route_node *rn;
345 struct ospf_neighbor *nbr;
346
347 /* At first, flush self-originated LSAs from routing domain. */
348 ospf_flush_self_originated_lsas_now (top);
349
350 /* Revert all neighbor status to ExStart. */
1eb8ef25 351 for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
2d59836a 352 {
1eb8ef25 353 if ((nbrs = oi->nbrs) == NULL)
2d59836a 354 continue;
355
356 for (rn = route_top (nbrs); rn; rn = route_next (rn))
357 {
358 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
359 continue;
360
361 if (nbr->state < NSM_ExStart)
362 continue;
363
364 if (IS_DEBUG_OSPF_EVENT)
e588f21f 365 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
2d59836a 366
367 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
368 }
369 }
370
371 return;
372}
d3f0d621 373
374
375struct ospf_neighbor *
376ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
377 struct ospf_header *ospfh)
378{
379 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
380 return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
381 else
382 return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
383}
384
4dadc291 385static struct ospf_neighbor *
d3f0d621 386ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
387 struct prefix *p)
388{
389 struct ospf_neighbor *nbr;
390
391 nbr = ospf_nbr_new (oi);
392 nbr->state = NSM_Down;
393 nbr->src = p->u.prefix4;
394 memcpy (&nbr->address, p, sizeof (struct prefix));
395
396 nbr->nbr_nbma = NULL;
397 if (oi->type == OSPF_IFTYPE_NBMA)
398 {
399 struct ospf_nbr_nbma *nbr_nbma;
52dc7ee6 400 struct listnode *node;
d3f0d621 401
1eb8ef25 402 for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
d3f0d621 403 {
d3f0d621 404 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
405 {
406 nbr_nbma->nbr = nbr;
407 nbr->nbr_nbma = nbr_nbma;
408
409 if (nbr_nbma->t_poll)
410 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
411
412 nbr->state_change = nbr_nbma->state_change + 1;
413 }
414 }
415 }
416
417 /* New nbr, save the crypto sequence number if necessary */
418 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
419 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
420
421 if (IS_DEBUG_OSPF_EVENT)
e588f21f 422 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
d3f0d621 423 inet_ntoa (nbr->router_id));
424
425 return nbr;
426}
427
428struct ospf_neighbor *
429ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
430 struct ip *iph, struct prefix *p)
431{
432 struct route_node *rn;
433 struct prefix key;
434 struct ospf_neighbor *nbr;
435
436 key.family = AF_INET;
437 key.prefixlen = IPV4_MAX_BITLEN;
438
439 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
440 key.u.prefix4 = ospfh->router_id; /* index vlink nbrs by router-id */
441 else
442 key.u.prefix4 = iph->ip_src;
443
444 rn = route_node_get (oi->nbrs, &key);
445 if (rn->info)
446 {
447 route_unlock_node (rn);
448 nbr = rn->info;
449
450 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
451 {
452 nbr->src = iph->ip_src;
453 memcpy (&nbr->address, p, sizeof (struct prefix));
454 }
455 }
456 else
457 {
458 rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
459 }
460
461 nbr->router_id = ospfh->router_id;
462
463 return nbr;
464}