blob: 878ab56798f38f4cea3a1acb89b9735c5908627d [file] [log] [blame]
paul2d598362003-01-17 23:48:42 +00001/*
2 * OSPF version 2 Neighbor State Machine
3 * From RFC2328 [OSPF Version 2]
4 * Copyright (C) 1999, 2000 Toshiaki Takada
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
26#include "thread.h"
27#include "memory.h"
28#include "hash.h"
29#include "linklist.h"
30#include "prefix.h"
31#include "if.h"
32#include "table.h"
33#include "stream.h"
34#include "table.h"
35#include "log.h"
36
37#include "ospfd/ospfd.h"
38#include "ospfd/ospf_interface.h"
39#include "ospfd/ospf_ism.h"
40#include "ospfd/ospf_asbr.h"
41#include "ospfd/ospf_lsa.h"
42#include "ospfd/ospf_lsdb.h"
43#include "ospfd/ospf_neighbor.h"
44#include "ospfd/ospf_nsm.h"
45#include "ospfd/ospf_network.h"
46#include "ospfd/ospf_packet.h"
47#include "ospfd/ospf_dump.h"
48#include "ospfd/ospf_flood.h"
49#include "ospfd/ospf_abr.h"
vincent5e4914c2005-09-29 16:34:30 +000050#include "ospfd/ospf_snmp.h"
paul2d598362003-01-17 23:48:42 +000051
52void nsm_reset_nbr (struct ospf_neighbor *);
53
54
55/* OSPF NSM Timer functions. */
paul4dadc292005-05-06 21:37:42 +000056static int
paul2d598362003-01-17 23:48:42 +000057ospf_inactivity_timer (struct thread *thread)
58{
59 struct ospf_neighbor *nbr;
60
61 nbr = THREAD_ARG (thread);
62 nbr->t_inactivity = NULL;
63
64 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
65 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Inactivity timer expire)",
66 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
67
68 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
69
70 return 0;
71}
72
paul4dadc292005-05-06 21:37:42 +000073static int
paul2d598362003-01-17 23:48:42 +000074ospf_db_desc_timer (struct thread *thread)
75{
76 struct ospf_interface *oi;
77 struct ospf_neighbor *nbr;
78
79 nbr = THREAD_ARG (thread);
80 nbr->t_db_desc = NULL;
81
82 oi = nbr->oi;
83
84 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
ajs2a42e282004-12-08 18:43:03 +000085 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (DD Retransmit timer expire)",
paul2d598362003-01-17 23:48:42 +000086 IF_NAME (nbr->oi), inet_ntoa (nbr->src));
87
88 /* resent last send DD packet. */
89 assert (nbr->last_send);
90 ospf_db_desc_resend (nbr);
91
92 /* DD Retransmit timer set. */
93 OSPF_NSM_TIMER_ON (nbr->t_db_desc, ospf_db_desc_timer, nbr->v_db_desc);
94
95 return 0;
96}
97
98/* Hook function called after ospf NSM event is occured. */
99
paul4dadc292005-05-06 21:37:42 +0000100static void
paul2d598362003-01-17 23:48:42 +0000101nsm_timer_set (struct ospf_neighbor *nbr)
102{
103 switch (nbr->state)
104 {
105 case NSM_Down:
106 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
107 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
108 break;
109 case NSM_Attempt:
110 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
111 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
112 break;
113 case NSM_Init:
114 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
115 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
116 break;
117 case NSM_TwoWay:
118 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
119 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
120 break;
121 case NSM_ExStart:
122 OSPF_NSM_TIMER_ON (nbr->t_db_desc, ospf_db_desc_timer, nbr->v_db_desc);
123 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
124 break;
125 case NSM_Exchange:
126 OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
127 if (!IS_SET_DD_MS (nbr->dd_flags))
128 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
129 break;
130 case NSM_Loading:
131 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
132 break;
133 case NSM_Full:
134 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
135 break;
136 default:
137 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
138 break;
139 }
140}
141
142
143/* OSPF NSM functions. */
paul4dadc292005-05-06 21:37:42 +0000144static int
paul2d598362003-01-17 23:48:42 +0000145nsm_ignore (struct ospf_neighbor *nbr)
146{
147 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
ajs2a42e282004-12-08 18:43:03 +0000148 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: nsm_ignore called",
paul2d598362003-01-17 23:48:42 +0000149 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
150
151 return 0;
152}
153
paul4dadc292005-05-06 21:37:42 +0000154static int
paul2d598362003-01-17 23:48:42 +0000155nsm_hello_received (struct ospf_neighbor *nbr)
156{
157 /* Start or Restart Inactivity Timer. */
158 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
159
160 OSPF_NSM_TIMER_ON (nbr->t_inactivity, ospf_inactivity_timer,
161 nbr->v_inactivity);
162
163 if (nbr->oi->type == OSPF_IFTYPE_NBMA && nbr->nbr_nbma)
164 OSPF_POLL_TIMER_OFF (nbr->nbr_nbma->t_poll);
165
166 return 0;
167}
168
paul4dadc292005-05-06 21:37:42 +0000169static int
paul2d598362003-01-17 23:48:42 +0000170nsm_start (struct ospf_neighbor *nbr)
171{
172
173 nsm_reset_nbr (nbr);
174
175 if (nbr->nbr_nbma)
176 OSPF_POLL_TIMER_OFF (nbr->nbr_nbma->t_poll);
177
178 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
179
180 OSPF_NSM_TIMER_ON (nbr->t_inactivity, ospf_inactivity_timer,
181 nbr->v_inactivity);
182
183 return 0;
184}
185
paul4dadc292005-05-06 21:37:42 +0000186static int
paul2d598362003-01-17 23:48:42 +0000187nsm_twoway_received (struct ospf_neighbor *nbr)
188{
189 struct ospf_interface *oi;
190 int next_state = NSM_TwoWay;
191
192 oi = nbr->oi;
193
194 /* These netowork types must be adjacency. */
195 if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
196 oi->type == OSPF_IFTYPE_POINTOMULTIPOINT ||
197 oi->type == OSPF_IFTYPE_VIRTUALLINK)
198 next_state = NSM_ExStart;
199
200 /* Router itself is the DRouter or the BDRouter. */
201 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi)) ||
202 IPV4_ADDR_SAME (&oi->address->u.prefix4, &BDR (oi)))
203 next_state = NSM_ExStart;
204
205 /* Neighboring Router is the DRouter or the BDRouter. */
206 if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router) ||
207 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router))
208 next_state = NSM_ExStart;
209
210 return next_state;
211}
212
213int
214ospf_db_summary_count (struct ospf_neighbor *nbr)
215{
216 return ospf_lsdb_count_all (&nbr->db_sum);
217}
218
219int
220ospf_db_summary_isempty (struct ospf_neighbor *nbr)
221{
222 return ospf_lsdb_isempty (&nbr->db_sum);
223}
224
paul4dadc292005-05-06 21:37:42 +0000225static int
paul68980082003-03-25 05:07:42 +0000226ospf_db_summary_add (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
paul2d598362003-01-17 23:48:42 +0000227{
paul09e4efd2003-01-18 00:12:02 +0000228#ifdef HAVE_OPAQUE_LSA
229 switch (lsa->data->type)
230 {
231 case OSPF_OPAQUE_LINK_LSA:
232 /* Exclude type-9 LSAs that does not have the same "oi" with "nbr". */
233 if (lsa->oi != nbr->oi)
234 return 0;
235 break;
236 case OSPF_OPAQUE_AREA_LSA:
237 /*
238 * It is assured by the caller function "nsm_negotiation_done()"
239 * that every given LSA belongs to the same area with "nbr".
240 */
241 break;
242 case OSPF_OPAQUE_AS_LSA:
243 default:
244 break;
245 }
246#endif /* HAVE_OPAQUE_LSA */
247
paul2d598362003-01-17 23:48:42 +0000248 /* Stay away from any Local Translated Type-7 LSAs */
249 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
250 return 0;
paul2d598362003-01-17 23:48:42 +0000251
252 if (IS_LSA_MAXAGE (lsa))
253 ospf_ls_retransmit_add (nbr, lsa);
254 else
255 ospf_lsdb_add (&nbr->db_sum, lsa);
256
257 return 0;
258}
259
260void
261ospf_db_summary_clear (struct ospf_neighbor *nbr)
262{
263 struct ospf_lsdb *lsdb;
264 int i;
265
266 lsdb = &nbr->db_sum;
267 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
268 {
269 struct route_table *table = lsdb->type[i].db;
270 struct route_node *rn;
271
272 for (rn = route_top (table); rn; rn = route_next (rn))
273 if (rn->info)
274 ospf_lsdb_delete (&nbr->db_sum, rn->info);
275 }
276}
277
278
279
paul2d598362003-01-17 23:48:42 +0000280/* The area link state database consists of the router-LSAs,
281 network-LSAs and summary-LSAs contained in the area structure,
paul68980082003-03-25 05:07:42 +0000282 along with the AS-external-LSAs contained in the global structure.
283 AS-external-LSAs are omitted from a virtual neighbor's Database
paul2d598362003-01-17 23:48:42 +0000284 summary list. AS-external-LSAs are omitted from the Database
285 summary list if the area has been configured as a stub. */
paul4dadc292005-05-06 21:37:42 +0000286static int
paul2d598362003-01-17 23:48:42 +0000287nsm_negotiation_done (struct ospf_neighbor *nbr)
288{
paul68980082003-03-25 05:07:42 +0000289 struct ospf_area *area = nbr->oi->area;
290 struct ospf_lsa *lsa;
291 struct route_node *rn;
paul2d598362003-01-17 23:48:42 +0000292
paul68980082003-03-25 05:07:42 +0000293 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
294 ospf_db_summary_add (nbr, lsa);
295 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
296 ospf_db_summary_add (nbr, lsa);
297 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
298 ospf_db_summary_add (nbr, lsa);
299 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
300 ospf_db_summary_add (nbr, lsa);
paul2d598362003-01-17 23:48:42 +0000301
paul2d598362003-01-17 23:48:42 +0000302#ifdef HAVE_OPAQUE_LSA
303 /* Process only if the neighbor is opaque capable. */
304 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
305 {
paul68980082003-03-25 05:07:42 +0000306 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
307 ospf_db_summary_add (nbr, lsa);
308 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
309 ospf_db_summary_add (nbr, lsa);
paul2d598362003-01-17 23:48:42 +0000310 }
311#endif /* HAVE_OPAQUE_LSA */
312
hassof4833e92005-06-20 20:42:26 +0000313 if (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
314 {
315 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
316 ospf_db_summary_add (nbr, lsa);
317 }
318
paul68980082003-03-25 05:07:42 +0000319 if (nbr->oi->type != OSPF_IFTYPE_VIRTUALLINK
320 && area->external_routing == OSPF_AREA_DEFAULT)
321 LSDB_LOOP (EXTERNAL_LSDB (nbr->oi->ospf), rn, lsa)
322 ospf_db_summary_add (nbr, lsa);
paul2d598362003-01-17 23:48:42 +0000323
324#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000325 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O)
326 && (nbr->oi->type != OSPF_IFTYPE_VIRTUALLINK
327 && area->external_routing == OSPF_AREA_DEFAULT))
328 LSDB_LOOP (OPAQUE_AS_LSDB (nbr->oi->ospf), rn, lsa)
329 ospf_db_summary_add (nbr, lsa);
paul2d598362003-01-17 23:48:42 +0000330#endif /* HAVE_OPAQUE_LSA */
331
paul2d598362003-01-17 23:48:42 +0000332 return 0;
333}
334
paul4dadc292005-05-06 21:37:42 +0000335static int
paul2d598362003-01-17 23:48:42 +0000336nsm_exchange_done (struct ospf_neighbor *nbr)
337{
paul2d598362003-01-17 23:48:42 +0000338 if (ospf_ls_request_isempty (nbr))
339 return NSM_Full;
340
341 /* Cancel dd retransmit timer. */
342 /* OSPF_NSM_TIMER_OFF (nbr->t_db_desc); */
343
344 /* Send Link State Request. */
345 ospf_ls_req_send (nbr);
346
347 return NSM_Loading;
348}
349
paul4dadc292005-05-06 21:37:42 +0000350static int
paul2d598362003-01-17 23:48:42 +0000351nsm_bad_ls_req (struct ospf_neighbor *nbr)
352{
353 /* Clear neighbor. */
354 nsm_reset_nbr (nbr);
355
356 return 0;
357}
358
paul4dadc292005-05-06 21:37:42 +0000359static int
paul2d598362003-01-17 23:48:42 +0000360nsm_adj_ok (struct ospf_neighbor *nbr)
361{
362 struct ospf_interface *oi;
363 int next_state;
364 int flag = 0;
365
366 oi = nbr->oi;
367 next_state = nbr->state;
368
369 /* These netowork types must be adjacency. */
paul68980082003-03-25 05:07:42 +0000370 if (oi->type == OSPF_IFTYPE_POINTOPOINT
371 || oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
372 || oi->type == OSPF_IFTYPE_VIRTUALLINK)
paul2d598362003-01-17 23:48:42 +0000373 flag = 1;
374
375 /* Router itself is the DRouter or the BDRouter. */
paul68980082003-03-25 05:07:42 +0000376 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))
377 || IPV4_ADDR_SAME (&oi->address->u.prefix4, &BDR (oi)))
paul2d598362003-01-17 23:48:42 +0000378 flag = 1;
379
paul68980082003-03-25 05:07:42 +0000380 if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &DR (oi))
381 || IPV4_ADDR_SAME (&nbr->address.u.prefix4, &BDR (oi)))
paul2d598362003-01-17 23:48:42 +0000382 flag = 1;
383
384 if (nbr->state == NSM_TwoWay && flag == 1)
385 next_state = NSM_ExStart;
386 else if (nbr->state >= NSM_ExStart && flag == 0)
387 next_state = NSM_TwoWay;
388
389 return next_state;
390}
391
paul4dadc292005-05-06 21:37:42 +0000392static int
paul2d598362003-01-17 23:48:42 +0000393nsm_seq_number_mismatch (struct ospf_neighbor *nbr)
394{
395 /* Clear neighbor. */
396 nsm_reset_nbr (nbr);
397
398 return 0;
399}
400
paul4dadc292005-05-06 21:37:42 +0000401static int
paul2d598362003-01-17 23:48:42 +0000402nsm_oneway_received (struct ospf_neighbor *nbr)
403{
404 /* Clear neighbor. */
405 nsm_reset_nbr (nbr);
406
407 return 0;
408}
409
410void
411nsm_reset_nbr (struct ospf_neighbor *nbr)
412{
413 /* Clear Database Summary list. */
414 if (!ospf_db_summary_isempty (nbr))
415 ospf_db_summary_clear (nbr);
416
417 /* Clear Link State Request list. */
418 if (!ospf_ls_request_isempty (nbr))
419 ospf_ls_request_delete_all (nbr);
420
421 /* Clear Link State Retransmission list. */
422 if (!ospf_ls_retransmit_isempty (nbr))
423 ospf_ls_retransmit_clear (nbr);
424
425 /* Cancel thread. */
426 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
427 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
428 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
429 OSPF_NSM_TIMER_OFF (nbr->t_hello_reply);
430
431#ifdef HAVE_OPAQUE_LSA
432 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
433 UNSET_FLAG (nbr->options, OSPF_OPTION_O);
434#endif /* HAVE_OPAQUE_LSA */
435}
436
paul4dadc292005-05-06 21:37:42 +0000437static int
paul2d598362003-01-17 23:48:42 +0000438nsm_kill_nbr (struct ospf_neighbor *nbr)
439{
440 /* call it here because we cannot call it from ospf_nsm_event */
441 nsm_change_state (nbr, NSM_Down);
442
Paul Jakma478aab92006-04-03 21:25:32 +0000443 /* killing nbr_self is invalid */
444 assert (nbr != nbr->oi->nbr_self);
445 if (nbr == nbr->oi->nbr_self)
446 return 1;
447
paul2d598362003-01-17 23:48:42 +0000448 /* Reset neighbor. */
449 nsm_reset_nbr (nbr);
450
451 if (nbr->oi->type == OSPF_IFTYPE_NBMA && nbr->nbr_nbma != NULL)
452 {
453 struct ospf_nbr_nbma *nbr_nbma = nbr->nbr_nbma;
454
455 nbr_nbma->nbr = NULL;
456 nbr_nbma->state_change = nbr->state_change;
457
458 nbr->nbr_nbma = NULL;
459
460 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
461 nbr_nbma->v_poll);
462
463 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
ajs2a42e282004-12-08 18:43:03 +0000464 zlog_debug ("NSM[%s:%s]: Down (PollIntervalTimer scheduled)",
paul2d598362003-01-17 23:48:42 +0000465 IF_NAME (nbr->oi), inet_ntoa (nbr->address.u.prefix4));
466 }
467
468 /* Delete neighbor from interface. */
469 ospf_nbr_delete (nbr);
470
471 return 0;
472}
473
paul4dadc292005-05-06 21:37:42 +0000474static int
paul2d598362003-01-17 23:48:42 +0000475nsm_inactivity_timer (struct ospf_neighbor *nbr)
476{
477 /* Kill neighbor. */
478 nsm_kill_nbr (nbr);
479
480 return 0;
481}
482
paul4dadc292005-05-06 21:37:42 +0000483static int
paul2d598362003-01-17 23:48:42 +0000484nsm_ll_down (struct ospf_neighbor *nbr)
485{
486 /* Reset neighbor. */
487 /*nsm_reset_nbr (nbr);*/
488
489 /* Kill neighbor. */
490 nsm_kill_nbr (nbr);
491
492 return 0;
493}
494
495/* Neighbor State Machine */
496struct {
paul4dadc292005-05-06 21:37:42 +0000497 int (*func) (struct ospf_neighbor *);
paul2d598362003-01-17 23:48:42 +0000498 int next_state;
499} NSM [OSPF_NSM_STATE_MAX][OSPF_NSM_EVENT_MAX] =
500{
501 {
502 /* DependUpon: dummy state. */
503 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
504 { nsm_ignore, NSM_DependUpon }, /* HelloReceived */
505 { nsm_ignore, NSM_DependUpon }, /* Start */
506 { nsm_ignore, NSM_DependUpon }, /* 2-WayReceived */
507 { nsm_ignore, NSM_DependUpon }, /* NegotiationDone */
508 { nsm_ignore, NSM_DependUpon }, /* ExchangeDone */
509 { nsm_ignore, NSM_DependUpon }, /* BadLSReq */
510 { nsm_ignore, NSM_DependUpon }, /* LoadingDone */
511 { nsm_ignore, NSM_DependUpon }, /* AdjOK? */
512 { nsm_ignore, NSM_DependUpon }, /* SeqNumberMismatch */
513 { nsm_ignore, NSM_DependUpon }, /* 1-WayReceived */
514 { nsm_ignore, NSM_DependUpon }, /* KillNbr */
515 { nsm_ignore, NSM_DependUpon }, /* InactivityTimer */
516 { nsm_ignore, NSM_DependUpon }, /* LLDown */
517 },
518 {
519 /* Down: */
520 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
521 { nsm_hello_received, NSM_Init }, /* HelloReceived */
522 { nsm_start, NSM_Attempt }, /* Start */
523 { nsm_ignore, NSM_Down }, /* 2-WayReceived */
524 { nsm_ignore, NSM_Down }, /* NegotiationDone */
525 { nsm_ignore, NSM_Down }, /* ExchangeDone */
526 { nsm_ignore, NSM_Down }, /* BadLSReq */
527 { nsm_ignore, NSM_Down }, /* LoadingDone */
528 { nsm_ignore, NSM_Down }, /* AdjOK? */
529 { nsm_ignore, NSM_Down }, /* SeqNumberMismatch */
530 { nsm_ignore, NSM_Down }, /* 1-WayReceived */
531 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
532 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
533 { nsm_ll_down, NSM_Down }, /* LLDown */
534 },
535 {
536 /* Attempt: */
537 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
538 { nsm_hello_received, NSM_Init }, /* HelloReceived */
539 { nsm_ignore, NSM_Attempt }, /* Start */
540 { nsm_ignore, NSM_Attempt }, /* 2-WayReceived */
541 { nsm_ignore, NSM_Attempt }, /* NegotiationDone */
542 { nsm_ignore, NSM_Attempt }, /* ExchangeDone */
543 { nsm_ignore, NSM_Attempt }, /* BadLSReq */
544 { nsm_ignore, NSM_Attempt }, /* LoadingDone */
545 { nsm_ignore, NSM_Attempt }, /* AdjOK? */
546 { nsm_ignore, NSM_Attempt }, /* SeqNumberMismatch */
547 { nsm_ignore, NSM_Attempt }, /* 1-WayReceived */
548 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
549 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
550 { nsm_ll_down, NSM_Down }, /* LLDown */
551 },
552 {
553 /* Init: */
554 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
555 { nsm_hello_received, NSM_Init }, /* HelloReceived */
556 { nsm_ignore, NSM_Init }, /* Start */
557 { nsm_twoway_received, NSM_DependUpon }, /* 2-WayReceived */
558 { nsm_ignore, NSM_Init }, /* NegotiationDone */
559 { nsm_ignore, NSM_Init }, /* ExchangeDone */
560 { nsm_ignore, NSM_Init }, /* BadLSReq */
561 { nsm_ignore, NSM_Init }, /* LoadingDone */
562 { nsm_ignore, NSM_Init }, /* AdjOK? */
563 { nsm_ignore, NSM_Init }, /* SeqNumberMismatch */
564 { nsm_ignore, NSM_Init }, /* 1-WayReceived */
565 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
566 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
567 { nsm_ll_down, NSM_Down }, /* LLDown */
568 },
569 {
570 /* 2-Way: */
571 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
572 { nsm_hello_received, NSM_TwoWay }, /* HelloReceived */
573 { nsm_ignore, NSM_TwoWay }, /* Start */
574 { nsm_ignore, NSM_TwoWay }, /* 2-WayReceived */
575 { nsm_ignore, NSM_TwoWay }, /* NegotiationDone */
576 { nsm_ignore, NSM_TwoWay }, /* ExchangeDone */
577 { nsm_ignore, NSM_TwoWay }, /* BadLSReq */
578 { nsm_ignore, NSM_TwoWay }, /* LoadingDone */
579 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
580 { nsm_ignore, NSM_TwoWay }, /* SeqNumberMismatch */
581 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
582 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
583 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
584 { nsm_ll_down, NSM_Down }, /* LLDown */
585 },
586 {
587 /* ExStart: */
588 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
589 { nsm_hello_received, NSM_ExStart }, /* HelloReceived */
590 { nsm_ignore, NSM_ExStart }, /* Start */
591 { nsm_ignore, NSM_ExStart }, /* 2-WayReceived */
592 { nsm_negotiation_done, NSM_Exchange }, /* NegotiationDone */
593 { nsm_ignore, NSM_ExStart }, /* ExchangeDone */
594 { nsm_ignore, NSM_ExStart }, /* BadLSReq */
595 { nsm_ignore, NSM_ExStart }, /* LoadingDone */
596 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
597 { nsm_ignore, NSM_ExStart }, /* SeqNumberMismatch */
598 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
599 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
600 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
601 { nsm_ll_down, NSM_Down }, /* LLDown */
602 },
603 {
604 /* Exchange: */
605 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
606 { nsm_hello_received, NSM_Exchange }, /* HelloReceived */
607 { nsm_ignore, NSM_Exchange }, /* Start */
608 { nsm_ignore, NSM_Exchange }, /* 2-WayReceived */
609 { nsm_ignore, NSM_Exchange }, /* NegotiationDone */
610 { nsm_exchange_done, NSM_DependUpon }, /* ExchangeDone */
611 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
612 { nsm_ignore, NSM_Exchange }, /* LoadingDone */
613 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
614 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
615 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
616 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
617 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
618 { nsm_ll_down, NSM_Down }, /* LLDown */
619 },
620 {
621 /* Loading: */
622 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
623 { nsm_hello_received, NSM_Loading }, /* HelloReceived */
624 { nsm_ignore, NSM_Loading }, /* Start */
625 { nsm_ignore, NSM_Loading }, /* 2-WayReceived */
626 { nsm_ignore, NSM_Loading }, /* NegotiationDone */
627 { nsm_ignore, NSM_Loading }, /* ExchangeDone */
628 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
629 { nsm_ignore, NSM_Full }, /* LoadingDone */
630 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
631 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
632 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
633 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
634 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
635 { nsm_ll_down, NSM_Down }, /* LLDown */
636 },
637 { /* Full: */
638 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
639 { nsm_hello_received, NSM_Full }, /* HelloReceived */
640 { nsm_ignore, NSM_Full }, /* Start */
641 { nsm_ignore, NSM_Full }, /* 2-WayReceived */
642 { nsm_ignore, NSM_Full }, /* NegotiationDone */
643 { nsm_ignore, NSM_Full }, /* ExchangeDone */
644 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
645 { nsm_ignore, NSM_Full }, /* LoadingDone */
646 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
647 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
648 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
649 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
650 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
651 { nsm_ll_down, NSM_Down }, /* LLDown */
652 },
653};
654
hassoeb1ce602004-10-08 08:17:22 +0000655const static char *ospf_nsm_event_str[] =
paul2d598362003-01-17 23:48:42 +0000656{
657 "NoEvent",
658 "HelloReceived",
659 "Start",
660 "2-WayReceived",
661 "NegotiationDone",
662 "ExchangeDone",
663 "BadLSReq",
664 "LoadingDone",
665 "AdjOK?",
666 "SeqNumberMismatch",
667 "1-WayReceived",
668 "KillNbr",
669 "InactivityTimer",
670 "LLDown",
671};
672
673void
674nsm_change_state (struct ospf_neighbor *nbr, int state)
675{
paul68980082003-03-25 05:07:42 +0000676 struct ospf_interface *oi = nbr->oi;
paul2d598362003-01-17 23:48:42 +0000677 struct ospf_area *vl_area = NULL;
678 u_char old_state;
679 int x;
680 int force = 1;
681
682 /* Logging change of status. */
683 if (IS_DEBUG_OSPF (nsm, NSM_STATUS))
ajs2a42e282004-12-08 18:43:03 +0000684 zlog_debug ("NSM[%s:%s]: State change %s -> %s",
paul2d598362003-01-17 23:48:42 +0000685 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id),
686 LOOKUP (ospf_nsm_state_msg, nbr->state),
687 LOOKUP (ospf_nsm_state_msg, state));
688
689 /* Preserve old status. */
690 old_state = nbr->state;
691
692 /* Change to new status. */
693 nbr->state = state;
694
695 /* Statistics. */
696 nbr->state_change++;
697
paul2d598362003-01-17 23:48:42 +0000698 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
paul68980082003-03-25 05:07:42 +0000699 vl_area = ospf_area_lookup_by_area_id (oi->ospf, oi->vl_data->vl_area_id);
Andrew J. Schorrd7e60dd2006-06-29 20:20:52 +0000700
701 /* Optionally notify about adjacency changes */
702 if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_CHANGES) &&
703 (old_state != state) &&
704 (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL) ||
705 (state == NSM_Full) || (state < old_state)))
706 zlog_notice("AdjChg: Nbr %s on %s: %s -> %s",
707 inet_ntoa (nbr->router_id), IF_NAME (nbr->oi),
708 LOOKUP (ospf_nsm_state_msg, old_state),
709 LOOKUP (ospf_nsm_state_msg, state));
710
vincent5e4914c2005-09-29 16:34:30 +0000711#ifdef HAVE_SNMP
712 /* Terminal state or regression */
713 if ((state == NSM_Full) || (state == NSM_TwoWay) || (state < old_state))
714 {
715 /* ospfVirtNbrStateChange */
716 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
717 ospfTrapVirtNbrStateChange(nbr);
718 /* ospfNbrStateChange trap */
719 else
720 /* To/From FULL, only managed by DR */
721 if (((state != NSM_Full) && (old_state != NSM_Full)) ||
722 (oi->state == ISM_DR))
723 ospfTrapNbrStateChange(nbr);
724 }
725#endif
726
paul2d598362003-01-17 23:48:42 +0000727 /* One of the neighboring routers changes to/from the FULL state. */
728 if ((old_state != NSM_Full && state == NSM_Full) ||
729 (old_state == NSM_Full && state != NSM_Full))
ajs3aa8d5f2004-12-11 18:00:06 +0000730 {
paul2d598362003-01-17 23:48:42 +0000731 if (state == NSM_Full)
732 {
733 oi->full_nbrs++;
734 oi->area->full_nbrs++;
735
paul68980082003-03-25 05:07:42 +0000736 ospf_check_abr_status (oi->ospf);
paul2d598362003-01-17 23:48:42 +0000737
738 if (oi->type == OSPF_IFTYPE_VIRTUALLINK && vl_area)
739 if (++vl_area->full_vls == 1)
paul68980082003-03-25 05:07:42 +0000740 ospf_schedule_abr_task (oi->ospf);
paul2d598362003-01-17 23:48:42 +0000741
742 /* kevinm: refresh any redistributions */
paul68980082003-03-25 05:07:42 +0000743 for (x = ZEBRA_ROUTE_SYSTEM; x < ZEBRA_ROUTE_MAX; x++)
744 {
745 if (x == ZEBRA_ROUTE_OSPF || x == ZEBRA_ROUTE_OSPF6)
746 continue;
747 ospf_external_lsa_refresh_type (oi->ospf, x, force);
748 }
paul2d598362003-01-17 23:48:42 +0000749 }
750 else
751 {
752 oi->full_nbrs--;
753 oi->area->full_nbrs--;
754
paul68980082003-03-25 05:07:42 +0000755 ospf_check_abr_status (oi->ospf);
paul2d598362003-01-17 23:48:42 +0000756
757 if (oi->type == OSPF_IFTYPE_VIRTUALLINK && vl_area)
758 if (vl_area->full_vls > 0)
759 if (--vl_area->full_vls == 0)
paul68980082003-03-25 05:07:42 +0000760 ospf_schedule_abr_task (oi->ospf);
paul2d598362003-01-17 23:48:42 +0000761
762 /* clear neighbor retransmit list */
763 if (!ospf_ls_retransmit_isempty (nbr))
764 ospf_ls_retransmit_clear (nbr);
765 }
766
ajs3aa8d5f2004-12-11 18:00:06 +0000767 zlog_info ("nsm_change_state(%s, %s -> %s): "
768 "scheduling new router-LSA origination",
769 inet_ntoa (nbr->router_id),
770 LOOKUP(ospf_nsm_state_msg, old_state),
771 LOOKUP(ospf_nsm_state_msg, state));
paul2d598362003-01-17 23:48:42 +0000772
773 ospf_router_lsa_timer_add (oi->area);
774
775 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
776 {
777 struct ospf_area *vl_area =
paul68980082003-03-25 05:07:42 +0000778 ospf_area_lookup_by_area_id (oi->ospf, oi->vl_data->vl_area_id);
paul2d598362003-01-17 23:48:42 +0000779
780 if (vl_area)
781 ospf_router_lsa_timer_add (vl_area);
782 }
783
784 /* Originate network-LSA. */
785 if (oi->state == ISM_DR)
786 {
787 if (oi->network_lsa_self && oi->full_nbrs == 0)
788 {
789 ospf_lsa_flush_area (oi->network_lsa_self, oi->area);
790 ospf_lsa_unlock (oi->network_lsa_self);
791 oi->network_lsa_self = NULL;
792 OSPF_TIMER_OFF (oi->t_network_lsa_self);
793 }
794 else
795 ospf_network_lsa_timer_add (oi);
796 }
797 }
798
799#ifdef HAVE_OPAQUE_LSA
800 ospf_opaque_nsm_change (nbr, old_state);
801#endif /* HAVE_OPAQUE_LSA */
802
803 /* Start DD exchange protocol */
804 if (state == NSM_ExStart)
805 {
806 if (nbr->dd_seqnum == 0)
807 nbr->dd_seqnum = time (NULL);
808 else
809 nbr->dd_seqnum++;
810
811 nbr->dd_flags = OSPF_DD_FLAG_I|OSPF_DD_FLAG_M|OSPF_DD_FLAG_MS;
812 ospf_db_desc_send (nbr);
813 }
814
815 /* clear cryptographic sequence number */
816 if (state == NSM_Down)
817 nbr->crypt_seqnum = 0;
818
819 /* Generete NeighborChange ISM event. */
820#ifdef BUGGY_ISM_TRANSITION
821 if ((old_state < NSM_TwoWay && state >= NSM_TwoWay) ||
822 (old_state >= NSM_TwoWay && state < NSM_TwoWay))
823 OSPF_ISM_EVENT_EXECUTE (oi, ISM_NeighborChange);
824#else /* BUGGY_ISM_TRANSITION */
825 switch (oi->state) {
826 case ISM_DROther:
827 case ISM_Backup:
828 case ISM_DR:
829 if ((old_state < NSM_TwoWay && state >= NSM_TwoWay) ||
830 (old_state >= NSM_TwoWay && state < NSM_TwoWay))
831 OSPF_ISM_EVENT_EXECUTE (oi, ISM_NeighborChange);
832 break;
833 default:
834 /* ISM_PointToPoint -> ISM_Down, ISM_Loopback -> ISM_Down, etc. */
835 break;
836 }
837#endif /* BUGGY_ISM_TRANSITION */
838
839 /* Performance hack. Send hello immideately when some neighbor enter
840 Init state. This whay we decrease neighbor discovery time. Gleb.*/
841 if (state == NSM_Init)
842 {
843 OSPF_ISM_TIMER_OFF (oi->t_hello);
paulf9ad9372005-10-21 00:45:17 +0000844 OSPF_ISM_TIMER_MSEC_ON (oi->t_hello, ospf_hello_timer, 1);
paul2d598362003-01-17 23:48:42 +0000845 }
846
847 /* Preserve old status? */
848}
849
850/* Execute NSM event process. */
851int
852ospf_nsm_event (struct thread *thread)
853{
854 int event;
855 int next_state;
856 struct ospf_neighbor *nbr;
857 struct in_addr router_id;
858 int old_state;
859 struct ospf_interface *oi;
860
861 nbr = THREAD_ARG (thread);
862 event = THREAD_VAL (thread);
863 router_id = nbr->router_id;
864
865 old_state = nbr->state;
866 oi = nbr->oi ;
867
868 /* Call function. */
869 next_state = (*(NSM [nbr->state][event].func))(nbr);
870
871 /* When event is NSM_KillNbr or InactivityTimer, the neighbor is
872 deleted. */
873 if (event == NSM_KillNbr || event == NSM_InactivityTimer)
874 {
875 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
ajs2a42e282004-12-08 18:43:03 +0000876 zlog_debug ("NSM[%s:%s]: neighbor deleted",
paul2d598362003-01-17 23:48:42 +0000877 IF_NAME (oi), inet_ntoa (router_id));
878
879 /* Timers are canceled in ospf_nbr_free, moreover we cannot call
880 nsm_timer_set here because nbr is freed already!!!*/
881 /*nsm_timer_set (nbr);*/
882
883 return 0;
884 }
885
886 if (! next_state)
887 next_state = NSM [nbr->state][event].next_state;
888
889 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
ajs2a42e282004-12-08 18:43:03 +0000890 zlog_debug ("NSM[%s:%s]: %s (%s)", IF_NAME (oi),
paul2d598362003-01-17 23:48:42 +0000891 inet_ntoa (nbr->router_id),
892 LOOKUP (ospf_nsm_state_msg, nbr->state),
893 ospf_nsm_event_str [event]);
894
895 /* If state is changed. */
896 if (next_state != nbr->state)
897 nsm_change_state (nbr, next_state);
898
899 /* Make sure timer is set. */
900 nsm_timer_set (nbr);
901
902 return 0;
903}
904
905/* Check loading state. */
906void
907ospf_check_nbr_loading (struct ospf_neighbor *nbr)
908{
909 if (nbr->state == NSM_Loading)
910 {
911 if (ospf_ls_request_isempty (nbr))
912 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_LoadingDone);
913 else if (nbr->ls_req_last == NULL)
914 ospf_ls_req_event (nbr);
915 }
916}