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