]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_neighbor.c
Merge branch 'cmaster-next' into vtysh-grammar
[mirror_frr.git] / ospfd / ospf_neighbor.c
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 #include "json.h"
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"
46 #include "ospfd/ospf_bfd.h"
47
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 and PointToPoint interfaces, where
51 * neighbours are indexed by router-ID instead.
52 */
53 static void
54 ospf_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 oi->type == OSPF_IFTYPE_POINTOPOINT)
63 key->u.prefix4 = nbr->router_id;
64 else
65 key->u.prefix4 = nbr->src;
66 return;
67 }
68
69 struct ospf_neighbor *
70 ospf_nbr_new (struct ospf_interface *oi)
71 {
72 struct ospf_neighbor *nbr;
73
74 /* Allcate new neighbor. */
75 nbr = XCALLOC (MTYPE_OSPF_NEIGHBOR, sizeof (struct ospf_neighbor));
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
104 ospf_bfd_info_nbr_create(oi, nbr);
105 return nbr;
106 }
107
108 void
109 ospf_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
148 ospf_bfd_info_free(&nbr->bfd_info);
149 XFREE (MTYPE_OSPF_NEIGHBOR, nbr);
150 }
151
152 /* Delete specified OSPF neighbor from interface. */
153 void
154 ospf_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;
161
162 /* get appropriate prefix 'key' */
163 ospf_nbr_key (oi, nbr, &p);
164
165 rn = route_node_lookup (oi->nbrs, &p);
166 if (rn)
167 {
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
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 else
189 {
190 /*
191 * This neighbor was not found, but before we move on and
192 * free the neighbor structre, make sure that it was not
193 * indexed incorrectly and ended up in the "worng" place
194 */
195
196 /* Reverse the lookup rules */
197 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
198 oi->type == OSPF_IFTYPE_POINTOPOINT)
199 p.u.prefix4 = nbr->src;
200 else
201 p.u.prefix4 = nbr->router_id;
202
203 rn = route_node_lookup (oi->nbrs, &p);
204 if (rn){
205 /* We found the neighbor!
206 * Now make sure it is not the exact same neighbor
207 * structure that we are about to free
208 */
209 if (nbr == rn->info){
210 /* Same neighbor, drop the reference to it */
211 rn->info = NULL;
212 route_unlock_node (rn);
213 }
214 route_unlock_node (rn);
215 }
216 }
217
218 /* Free ospf_neighbor structure. */
219 ospf_nbr_free (nbr);
220 }
221
222 /* Check myself is in the neighbor list. */
223 int
224 ospf_nbr_bidirectional (struct in_addr *router_id,
225 struct in_addr *neighbors, int size)
226 {
227 int i;
228 int max;
229
230 max = size / sizeof (struct in_addr);
231
232 for (i = 0; i < max; i ++)
233 if (IPV4_ADDR_SAME (router_id, &neighbors[i]))
234 return 1;
235
236 return 0;
237 }
238
239 /* reset nbr_self */
240 void
241 ospf_nbr_self_reset (struct ospf_interface *oi, struct in_addr router_id)
242 {
243 if (oi->nbr_self)
244 ospf_nbr_delete (oi->nbr_self);
245
246 oi->nbr_self = ospf_nbr_new (oi);
247 ospf_nbr_add_self (oi, router_id);
248 }
249
250 /* Add self to nbr list. */
251 void
252 ospf_nbr_add_self (struct ospf_interface *oi, struct in_addr router_id)
253 {
254 struct prefix p;
255 struct route_node *rn;
256
257 if (!oi->nbr_self)
258 oi->nbr_self = ospf_nbr_new (oi);
259
260 /* Initial state */
261 oi->nbr_self->address = *oi->address;
262 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
263 oi->nbr_self->router_id = router_id;
264 oi->nbr_self->src = oi->address->u.prefix4;
265 oi->nbr_self->state = NSM_TwoWay;
266
267 switch (oi->area->external_routing)
268 {
269 case OSPF_AREA_DEFAULT:
270 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
271 break;
272 case OSPF_AREA_STUB:
273 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
274 break;
275 case OSPF_AREA_NSSA:
276 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
277 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
278 break;
279 }
280
281 /* Add nbr_self to nbrs table */
282 ospf_nbr_key (oi, oi->nbr_self, &p);
283
284 rn = route_node_get (oi->nbrs, &p);
285 if (rn->info)
286 {
287 /* There is already pseudo neighbor. */
288 assert (oi->nbr_self == rn->info);
289 route_unlock_node (rn);
290 }
291 else
292 rn->info = oi->nbr_self;
293 }
294
295 /* Get neighbor count by status.
296 Specify status = 0, get all neighbor other than myself. */
297 int
298 ospf_nbr_count (struct ospf_interface *oi, int state)
299 {
300 struct ospf_neighbor *nbr;
301 struct route_node *rn;
302 int count = 0;
303
304 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
305 if ((nbr = rn->info))
306 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
307 if (state == 0 || nbr->state == state)
308 count++;
309
310 return count;
311 }
312
313 int
314 ospf_nbr_count_opaque_capable (struct ospf_interface *oi)
315 {
316 struct ospf_neighbor *nbr;
317 struct route_node *rn;
318 int count = 0;
319
320 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
321 if ((nbr = rn->info))
322 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
323 if (nbr->state == NSM_Full)
324 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
325 count++;
326
327 return count;
328 }
329
330 /* lookup nbr by address - use this only if you know you must
331 * otherwise use the ospf_nbr_lookup() wrapper, which deals
332 * with virtual link and PointToPoint neighbours
333 */
334 struct ospf_neighbor *
335 ospf_nbr_lookup_by_addr (struct route_table *nbrs,
336 struct in_addr *addr)
337 {
338 struct prefix p;
339 struct route_node *rn;
340 struct ospf_neighbor *nbr;
341
342 p.family = AF_INET;
343 p.prefixlen = IPV4_MAX_BITLEN;
344 p.u.prefix4 = *addr;
345
346 rn = route_node_lookup (nbrs, &p);
347 if (! rn)
348 return NULL;
349
350 /* See comment in ospf_nbr_delete */
351 assert (rn->info);
352
353 if (rn->info == NULL)
354 {
355 route_unlock_node (rn);
356 return NULL;
357 }
358
359 nbr = (struct ospf_neighbor *) rn->info;
360 route_unlock_node (rn);
361
362 return nbr;
363 }
364
365 struct ospf_neighbor *
366 ospf_nbr_lookup_by_routerid (struct route_table *nbrs,
367 struct in_addr *id)
368 {
369 struct route_node *rn;
370 struct ospf_neighbor *nbr;
371
372 for (rn = route_top (nbrs); rn; rn = route_next (rn))
373 if ((nbr = rn->info) != NULL)
374 if (IPV4_ADDR_SAME (&nbr->router_id, id))
375 {
376 route_unlock_node(rn);
377 return nbr;
378 }
379
380 return NULL;
381 }
382
383 void
384 ospf_renegotiate_optional_capabilities (struct ospf *top)
385 {
386 struct listnode *node;
387 struct ospf_interface *oi;
388 struct route_table *nbrs;
389 struct route_node *rn;
390 struct ospf_neighbor *nbr;
391
392 /* At first, flush self-originated LSAs from routing domain. */
393 ospf_flush_self_originated_lsas_now (top);
394
395 /* Revert all neighbor status to ExStart. */
396 for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi))
397 {
398 if ((nbrs = oi->nbrs) == NULL)
399 continue;
400
401 for (rn = route_top (nbrs); rn; rn = route_next (rn))
402 {
403 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
404 continue;
405
406 if (nbr->state < NSM_ExStart)
407 continue;
408
409 if (IS_DEBUG_OSPF_EVENT)
410 zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id));
411
412 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
413 }
414 }
415
416 return;
417 }
418
419
420 struct ospf_neighbor *
421 ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph,
422 struct ospf_header *ospfh)
423 {
424 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
425 oi->type == OSPF_IFTYPE_POINTOPOINT)
426 return (ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id));
427 else
428 return (ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src));
429 }
430
431 static struct ospf_neighbor *
432 ospf_nbr_add (struct ospf_interface *oi, struct ospf_header *ospfh,
433 struct prefix *p)
434 {
435 struct ospf_neighbor *nbr;
436
437 nbr = ospf_nbr_new (oi);
438 nbr->state = NSM_Down;
439 nbr->src = p->u.prefix4;
440 memcpy (&nbr->address, p, sizeof (struct prefix));
441
442 nbr->nbr_nbma = NULL;
443 if (oi->type == OSPF_IFTYPE_NBMA)
444 {
445 struct ospf_nbr_nbma *nbr_nbma;
446 struct listnode *node;
447
448 for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma))
449 {
450 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &nbr->src))
451 {
452 nbr_nbma->nbr = nbr;
453 nbr->nbr_nbma = nbr_nbma;
454
455 if (nbr_nbma->t_poll)
456 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
457
458 nbr->state_change = nbr_nbma->state_change + 1;
459 }
460 }
461 }
462
463 /* New nbr, save the crypto sequence number if necessary */
464 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
465 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
466
467 if (IS_DEBUG_OSPF_EVENT)
468 zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
469 inet_ntoa (nbr->router_id));
470
471 return nbr;
472 }
473
474 struct ospf_neighbor *
475 ospf_nbr_get (struct ospf_interface *oi, struct ospf_header *ospfh,
476 struct ip *iph, struct prefix *p)
477 {
478 struct route_node *rn;
479 struct prefix key;
480 struct ospf_neighbor *nbr;
481
482 key.family = AF_INET;
483 key.prefixlen = IPV4_MAX_BITLEN;
484
485 if (oi->type == OSPF_IFTYPE_VIRTUALLINK ||
486 oi->type == OSPF_IFTYPE_POINTOPOINT)
487 key.u.prefix4 = ospfh->router_id;/* index vlink and ptp nbrs by router-id */
488 else
489 key.u.prefix4 = iph->ip_src;
490
491 rn = route_node_get (oi->nbrs, &key);
492 if (rn->info)
493 {
494 route_unlock_node (rn);
495 nbr = rn->info;
496
497 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
498 {
499 nbr->src = iph->ip_src;
500 memcpy (&nbr->address, p, sizeof (struct prefix));
501 }
502 }
503 else
504 {
505 rn->info = nbr = ospf_nbr_add (oi, ospfh, p);
506 }
507
508 nbr->router_id = ospfh->router_id;
509
510 return nbr;
511 }