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