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