]>
Commit | Line | Data |
---|---|---|
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 | ||
189 | /* Free ospf_neighbor structure. */ | |
190 | ospf_nbr_free (nbr); | |
191 | } | |
192 | ||
193 | /* Check myself is in the neighbor list. */ | |
194 | int | |
195 | ospf_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 | ||
210 | /* reset nbr_self */ | |
211 | void | |
212 | ospf_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 | ||
219 | /* Add self to nbr list. */ | |
220 | void | |
221 | ospf_nbr_add_self (struct ospf_interface *oi) | |
222 | { | |
223 | struct prefix p; | |
224 | struct route_node *rn; | |
225 | ||
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 | } | |
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; | |
259 | } | |
260 | ||
261 | /* Get neighbor count by status. | |
262 | Specify status = 0, get all neighbor other than myself. */ | |
263 | int | |
264 | ospf_nbr_count (struct ospf_interface *oi, int state) | |
265 | { | |
266 | struct ospf_neighbor *nbr; | |
267 | struct route_node *rn; | |
268 | int count = 0; | |
269 | ||
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)) | |
273 | if (state == 0 || nbr->state == state) | |
274 | count++; | |
275 | ||
276 | return count; | |
277 | } | |
278 | ||
279 | #ifdef HAVE_OPAQUE_LSA | |
280 | int | |
281 | ospf_nbr_count_opaque_capable (struct ospf_interface *oi) | |
282 | { | |
283 | struct ospf_neighbor *nbr; | |
284 | struct route_node *rn; | |
285 | int count = 0; | |
286 | ||
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++; | |
293 | ||
294 | return count; | |
295 | } | |
296 | #endif /* HAVE_OPAQUE_LSA */ | |
297 | ||
298 | /* lookup nbr by address - use this only if you know you must | |
299 | * otherwise use the ospf_nbr_lookup() wrapper, which deals | |
300 | * with virtual link and PointToPoint neighbours | |
301 | */ | |
302 | struct ospf_neighbor * | |
303 | ospf_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; | |
317 | ||
318 | /* See comment in ospf_nbr_delete */ | |
319 | assert (rn->info); | |
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 | ||
333 | struct ospf_neighbor * | |
334 | ospf_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 | ||
351 | void | |
352 | ospf_renegotiate_optional_capabilities (struct ospf *top) | |
353 | { | |
354 | struct listnode *node; | |
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. */ | |
364 | for (ALL_LIST_ELEMENTS_RO (top->oiflist, node, oi)) | |
365 | { | |
366 | if ((nbrs = oi->nbrs) == NULL) | |
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) | |
378 | zlog_debug ("Renegotiate optional capabilities with neighbor(%s)", inet_ntoa (nbr->router_id)); | |
379 | ||
380 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
381 | } | |
382 | } | |
383 | ||
384 | return; | |
385 | } | |
386 | ||
387 | ||
388 | struct ospf_neighbor * | |
389 | ospf_nbr_lookup (struct ospf_interface *oi, struct ip *iph, | |
390 | struct ospf_header *ospfh) | |
391 | { | |
392 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK || | |
393 | oi->type == OSPF_IFTYPE_POINTOPOINT) | |
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 | ||
399 | static struct ospf_neighbor * | |
400 | ospf_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; | |
414 | struct listnode *node; | |
415 | ||
416 | for (ALL_LIST_ELEMENTS_RO (oi->nbr_nbma, node, nbr_nbma)) | |
417 | { | |
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) | |
436 | zlog_debug ("NSM[%s:%s]: start", IF_NAME (nbr->oi), | |
437 | inet_ntoa (nbr->router_id)); | |
438 | ||
439 | return nbr; | |
440 | } | |
441 | ||
442 | struct ospf_neighbor * | |
443 | ospf_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 | ||
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 */ | |
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 | } |