]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_nsm.c
[ospfd] trivial cleanup of nsm_timer_set
[mirror_frr.git] / ospfd / ospf_nsm.c
CommitLineData
2d59836a 1/*
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"
5e4914c3 50#include "ospfd/ospf_snmp.h"
2d59836a 51
52void nsm_reset_nbr (struct ospf_neighbor *);
53
54\f
55/* OSPF NSM Timer functions. */
4dadc291 56static int
2d59836a 57ospf_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
4dadc291 73static int
2d59836a 74ospf_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))
2a42e285 85 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (DD Retransmit timer expire)",
2d59836a 86 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
4dadc291 100static void
2d59836a 101nsm_timer_set (struct ospf_neighbor *nbr)
102{
103 switch (nbr->state)
104 {
105 case NSM_Down:
e55dd53b
PJ
106 /* This is here for documentation purposes, don't actually get here
107 * as Down neighbours are deleted typically, see nsm_kill_nbr
108 */
109 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
2d59836a 110 case NSM_Attempt:
2d59836a 111 case NSM_Init:
2d59836a 112 case NSM_TwoWay:
113 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
114 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
e55dd53b 115 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
2d59836a 116 break;
117 case NSM_ExStart:
118 OSPF_NSM_TIMER_ON (nbr->t_db_desc, ospf_db_desc_timer, nbr->v_db_desc);
119 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
e55dd53b 120 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
2d59836a 121 break;
122 case NSM_Exchange:
123 OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
124 if (!IS_SET_DD_MS (nbr->dd_flags))
125 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
126 break;
127 case NSM_Loading:
128 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
129 break;
130 case NSM_Full:
131 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
132 break;
133 default:
134 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
135 break;
136 }
137}
138
d7b0fb62
PJ
139/* 10.4 of RFC2328, indicate whether an adjacency is appropriate with
140 * the given neighbour
141 */
142static int
143nsm_should_adj (struct ospf_neighbor *nbr)
144{
145 struct ospf_interface *oi;
146
147 oi = nbr->oi;
148
149 /* These network types must always form adjacencies. */
150 if (oi->type == OSPF_IFTYPE_POINTOPOINT
151 || oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
152 || oi->type == OSPF_IFTYPE_VIRTUALLINK)
153 return 1;
154
155 /* Router itself is the DRouter or the BDRouter. */
156 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))
157 || IPV4_ADDR_SAME (&oi->address->u.prefix4, &BDR (oi)))
158 return 1;
159
160 /* Neighboring Router is the DRouter or the BDRouter. */
161 if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &DR (oi))
162 || IPV4_ADDR_SAME (&nbr->address.u.prefix4, &BDR (oi)))
163 return 1;
164
165 return 0;
166}
2d59836a 167\f
168/* OSPF NSM functions. */
4dadc291 169static int
2d59836a 170nsm_ignore (struct ospf_neighbor *nbr)
171{
172 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
2a42e285 173 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: nsm_ignore called",
2d59836a 174 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
175
176 return 0;
177}
178
4dadc291 179static int
2d59836a 180nsm_hello_received (struct ospf_neighbor *nbr)
181{
182 /* Start or Restart Inactivity Timer. */
183 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
184
185 OSPF_NSM_TIMER_ON (nbr->t_inactivity, ospf_inactivity_timer,
186 nbr->v_inactivity);
187
188 if (nbr->oi->type == OSPF_IFTYPE_NBMA && nbr->nbr_nbma)
189 OSPF_POLL_TIMER_OFF (nbr->nbr_nbma->t_poll);
190
191 return 0;
192}
193
4dadc291 194static int
2d59836a 195nsm_start (struct ospf_neighbor *nbr)
196{
197
198 nsm_reset_nbr (nbr);
199
200 if (nbr->nbr_nbma)
201 OSPF_POLL_TIMER_OFF (nbr->nbr_nbma->t_poll);
202
203 OSPF_NSM_TIMER_OFF (nbr->t_inactivity);
204
205 OSPF_NSM_TIMER_ON (nbr->t_inactivity, ospf_inactivity_timer,
206 nbr->v_inactivity);
207
208 return 0;
209}
210
4dadc291 211static int
2d59836a 212nsm_twoway_received (struct ospf_neighbor *nbr)
213{
2d59836a 214 int next_state = NSM_TwoWay;
215
d7b0fb62 216 if (nsm_should_adj (nbr))
2d59836a 217 next_state = NSM_ExStart;
218
219 return next_state;
220}
221
222int
223ospf_db_summary_count (struct ospf_neighbor *nbr)
224{
225 return ospf_lsdb_count_all (&nbr->db_sum);
226}
227
228int
229ospf_db_summary_isempty (struct ospf_neighbor *nbr)
230{
231 return ospf_lsdb_isempty (&nbr->db_sum);
232}
233
4dadc291 234static int
68980084 235ospf_db_summary_add (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
2d59836a 236{
09e4efdc 237#ifdef HAVE_OPAQUE_LSA
238 switch (lsa->data->type)
239 {
240 case OSPF_OPAQUE_LINK_LSA:
241 /* Exclude type-9 LSAs that does not have the same "oi" with "nbr". */
242 if (lsa->oi != nbr->oi)
243 return 0;
244 break;
245 case OSPF_OPAQUE_AREA_LSA:
246 /*
247 * It is assured by the caller function "nsm_negotiation_done()"
248 * that every given LSA belongs to the same area with "nbr".
249 */
250 break;
251 case OSPF_OPAQUE_AS_LSA:
252 default:
253 break;
254 }
255#endif /* HAVE_OPAQUE_LSA */
256
2d59836a 257 /* Stay away from any Local Translated Type-7 LSAs */
258 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
259 return 0;
2d59836a 260
261 if (IS_LSA_MAXAGE (lsa))
262 ospf_ls_retransmit_add (nbr, lsa);
263 else
264 ospf_lsdb_add (&nbr->db_sum, lsa);
265
266 return 0;
267}
268
269void
270ospf_db_summary_clear (struct ospf_neighbor *nbr)
271{
272 struct ospf_lsdb *lsdb;
273 int i;
274
275 lsdb = &nbr->db_sum;
276 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
277 {
278 struct route_table *table = lsdb->type[i].db;
279 struct route_node *rn;
280
281 for (rn = route_top (table); rn; rn = route_next (rn))
282 if (rn->info)
283 ospf_lsdb_delete (&nbr->db_sum, rn->info);
284 }
285}
286
287\f
288
2d59836a 289/* The area link state database consists of the router-LSAs,
290 network-LSAs and summary-LSAs contained in the area structure,
68980084 291 along with the AS-external-LSAs contained in the global structure.
292 AS-external-LSAs are omitted from a virtual neighbor's Database
2d59836a 293 summary list. AS-external-LSAs are omitted from the Database
294 summary list if the area has been configured as a stub. */
4dadc291 295static int
2d59836a 296nsm_negotiation_done (struct ospf_neighbor *nbr)
297{
68980084 298 struct ospf_area *area = nbr->oi->area;
299 struct ospf_lsa *lsa;
300 struct route_node *rn;
301
302 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
303 ospf_db_summary_add (nbr, lsa);
304 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
305 ospf_db_summary_add (nbr, lsa);
306 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
307 ospf_db_summary_add (nbr, lsa);
308 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
309 ospf_db_summary_add (nbr, lsa);
2d59836a 310
2d59836a 311#ifdef HAVE_OPAQUE_LSA
312 /* Process only if the neighbor is opaque capable. */
313 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
314 {
68980084 315 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
316 ospf_db_summary_add (nbr, lsa);
317 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
318 ospf_db_summary_add (nbr, lsa);
2d59836a 319 }
320#endif /* HAVE_OPAQUE_LSA */
321
f4833e9f 322 if (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
323 {
324 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
325 ospf_db_summary_add (nbr, lsa);
326 }
327
68980084 328 if (nbr->oi->type != OSPF_IFTYPE_VIRTUALLINK
329 && area->external_routing == OSPF_AREA_DEFAULT)
330 LSDB_LOOP (EXTERNAL_LSDB (nbr->oi->ospf), rn, lsa)
331 ospf_db_summary_add (nbr, lsa);
2d59836a 332
333#ifdef HAVE_OPAQUE_LSA
68980084 334 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O)
335 && (nbr->oi->type != OSPF_IFTYPE_VIRTUALLINK
336 && area->external_routing == OSPF_AREA_DEFAULT))
337 LSDB_LOOP (OPAQUE_AS_LSDB (nbr->oi->ospf), rn, lsa)
338 ospf_db_summary_add (nbr, lsa);
2d59836a 339#endif /* HAVE_OPAQUE_LSA */
340
2d59836a 341 return 0;
342}
343
4dadc291 344static int
2d59836a 345nsm_exchange_done (struct ospf_neighbor *nbr)
346{
2d59836a 347 if (ospf_ls_request_isempty (nbr))
348 return NSM_Full;
349
350 /* Cancel dd retransmit timer. */
351 /* OSPF_NSM_TIMER_OFF (nbr->t_db_desc); */
352
353 /* Send Link State Request. */
354 ospf_ls_req_send (nbr);
355
356 return NSM_Loading;
357}
358
4dadc291 359static int
2d59836a 360nsm_bad_ls_req (struct ospf_neighbor *nbr)
361{
362 /* Clear neighbor. */
363 nsm_reset_nbr (nbr);
364
365 return 0;
366}
367
4dadc291 368static int
2d59836a 369nsm_adj_ok (struct ospf_neighbor *nbr)
370{
d7b0fb62
PJ
371 int next_state = nbr->state;
372 int adj = nsm_should_adj (nbr);
2d59836a 373
d7b0fb62 374 if (nbr->state == NSM_TwoWay && adj == 1)
2d59836a 375 next_state = NSM_ExStart;
d7b0fb62 376 else if (nbr->state >= NSM_ExStart && adj == 0)
2d59836a 377 next_state = NSM_TwoWay;
378
379 return next_state;
380}
381
4dadc291 382static int
2d59836a 383nsm_seq_number_mismatch (struct ospf_neighbor *nbr)
384{
385 /* Clear neighbor. */
386 nsm_reset_nbr (nbr);
387
388 return 0;
389}
390
4dadc291 391static int
2d59836a 392nsm_oneway_received (struct ospf_neighbor *nbr)
393{
394 /* Clear neighbor. */
395 nsm_reset_nbr (nbr);
396
397 return 0;
398}
399
400void
401nsm_reset_nbr (struct ospf_neighbor *nbr)
402{
403 /* Clear Database Summary list. */
404 if (!ospf_db_summary_isempty (nbr))
405 ospf_db_summary_clear (nbr);
406
407 /* Clear Link State Request list. */
408 if (!ospf_ls_request_isempty (nbr))
409 ospf_ls_request_delete_all (nbr);
410
411 /* Clear Link State Retransmission list. */
412 if (!ospf_ls_retransmit_isempty (nbr))
413 ospf_ls_retransmit_clear (nbr);
414
415 /* Cancel thread. */
416 OSPF_NSM_TIMER_OFF (nbr->t_db_desc);
417 OSPF_NSM_TIMER_OFF (nbr->t_ls_req);
418 OSPF_NSM_TIMER_OFF (nbr->t_ls_upd);
419 OSPF_NSM_TIMER_OFF (nbr->t_hello_reply);
420
421#ifdef HAVE_OPAQUE_LSA
422 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
423 UNSET_FLAG (nbr->options, OSPF_OPTION_O);
424#endif /* HAVE_OPAQUE_LSA */
425}
426
4dadc291 427static int
2d59836a 428nsm_kill_nbr (struct ospf_neighbor *nbr)
429{
430 /* call it here because we cannot call it from ospf_nsm_event */
431 nsm_change_state (nbr, NSM_Down);
432
478aab98
PJ
433 /* killing nbr_self is invalid */
434 assert (nbr != nbr->oi->nbr_self);
435 if (nbr == nbr->oi->nbr_self)
436 return 1;
437
2d59836a 438 /* Reset neighbor. */
439 nsm_reset_nbr (nbr);
440
441 if (nbr->oi->type == OSPF_IFTYPE_NBMA && nbr->nbr_nbma != NULL)
442 {
443 struct ospf_nbr_nbma *nbr_nbma = nbr->nbr_nbma;
444
445 nbr_nbma->nbr = NULL;
446 nbr_nbma->state_change = nbr->state_change;
447
448 nbr->nbr_nbma = NULL;
449
450 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
451 nbr_nbma->v_poll);
452
453 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
2a42e285 454 zlog_debug ("NSM[%s:%s]: Down (PollIntervalTimer scheduled)",
2d59836a 455 IF_NAME (nbr->oi), inet_ntoa (nbr->address.u.prefix4));
456 }
457
458 /* Delete neighbor from interface. */
459 ospf_nbr_delete (nbr);
460
461 return 0;
462}
463
4dadc291 464static int
2d59836a 465nsm_inactivity_timer (struct ospf_neighbor *nbr)
466{
467 /* Kill neighbor. */
468 nsm_kill_nbr (nbr);
469
470 return 0;
471}
472
4dadc291 473static int
2d59836a 474nsm_ll_down (struct ospf_neighbor *nbr)
475{
476 /* Reset neighbor. */
477 /*nsm_reset_nbr (nbr);*/
478
479 /* Kill neighbor. */
480 nsm_kill_nbr (nbr);
481
482 return 0;
483}
484
485/* Neighbor State Machine */
486struct {
4dadc291 487 int (*func) (struct ospf_neighbor *);
2d59836a 488 int next_state;
489} NSM [OSPF_NSM_STATE_MAX][OSPF_NSM_EVENT_MAX] =
490{
491 {
492 /* DependUpon: dummy state. */
493 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
494 { nsm_ignore, NSM_DependUpon }, /* HelloReceived */
495 { nsm_ignore, NSM_DependUpon }, /* Start */
496 { nsm_ignore, NSM_DependUpon }, /* 2-WayReceived */
497 { nsm_ignore, NSM_DependUpon }, /* NegotiationDone */
498 { nsm_ignore, NSM_DependUpon }, /* ExchangeDone */
499 { nsm_ignore, NSM_DependUpon }, /* BadLSReq */
500 { nsm_ignore, NSM_DependUpon }, /* LoadingDone */
501 { nsm_ignore, NSM_DependUpon }, /* AdjOK? */
502 { nsm_ignore, NSM_DependUpon }, /* SeqNumberMismatch */
503 { nsm_ignore, NSM_DependUpon }, /* 1-WayReceived */
504 { nsm_ignore, NSM_DependUpon }, /* KillNbr */
505 { nsm_ignore, NSM_DependUpon }, /* InactivityTimer */
506 { nsm_ignore, NSM_DependUpon }, /* LLDown */
507 },
508 {
509 /* Down: */
510 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
511 { nsm_hello_received, NSM_Init }, /* HelloReceived */
512 { nsm_start, NSM_Attempt }, /* Start */
513 { nsm_ignore, NSM_Down }, /* 2-WayReceived */
514 { nsm_ignore, NSM_Down }, /* NegotiationDone */
515 { nsm_ignore, NSM_Down }, /* ExchangeDone */
516 { nsm_ignore, NSM_Down }, /* BadLSReq */
517 { nsm_ignore, NSM_Down }, /* LoadingDone */
518 { nsm_ignore, NSM_Down }, /* AdjOK? */
519 { nsm_ignore, NSM_Down }, /* SeqNumberMismatch */
520 { nsm_ignore, NSM_Down }, /* 1-WayReceived */
521 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
522 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
523 { nsm_ll_down, NSM_Down }, /* LLDown */
524 },
525 {
526 /* Attempt: */
527 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
528 { nsm_hello_received, NSM_Init }, /* HelloReceived */
529 { nsm_ignore, NSM_Attempt }, /* Start */
530 { nsm_ignore, NSM_Attempt }, /* 2-WayReceived */
531 { nsm_ignore, NSM_Attempt }, /* NegotiationDone */
532 { nsm_ignore, NSM_Attempt }, /* ExchangeDone */
533 { nsm_ignore, NSM_Attempt }, /* BadLSReq */
534 { nsm_ignore, NSM_Attempt }, /* LoadingDone */
535 { nsm_ignore, NSM_Attempt }, /* AdjOK? */
536 { nsm_ignore, NSM_Attempt }, /* SeqNumberMismatch */
537 { nsm_ignore, NSM_Attempt }, /* 1-WayReceived */
538 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
539 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
540 { nsm_ll_down, NSM_Down }, /* LLDown */
541 },
542 {
543 /* Init: */
544 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
545 { nsm_hello_received, NSM_Init }, /* HelloReceived */
546 { nsm_ignore, NSM_Init }, /* Start */
547 { nsm_twoway_received, NSM_DependUpon }, /* 2-WayReceived */
548 { nsm_ignore, NSM_Init }, /* NegotiationDone */
549 { nsm_ignore, NSM_Init }, /* ExchangeDone */
550 { nsm_ignore, NSM_Init }, /* BadLSReq */
551 { nsm_ignore, NSM_Init }, /* LoadingDone */
552 { nsm_ignore, NSM_Init }, /* AdjOK? */
553 { nsm_ignore, NSM_Init }, /* SeqNumberMismatch */
554 { nsm_ignore, NSM_Init }, /* 1-WayReceived */
555 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
556 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
557 { nsm_ll_down, NSM_Down }, /* LLDown */
558 },
559 {
560 /* 2-Way: */
561 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
562 { nsm_hello_received, NSM_TwoWay }, /* HelloReceived */
563 { nsm_ignore, NSM_TwoWay }, /* Start */
564 { nsm_ignore, NSM_TwoWay }, /* 2-WayReceived */
565 { nsm_ignore, NSM_TwoWay }, /* NegotiationDone */
566 { nsm_ignore, NSM_TwoWay }, /* ExchangeDone */
567 { nsm_ignore, NSM_TwoWay }, /* BadLSReq */
568 { nsm_ignore, NSM_TwoWay }, /* LoadingDone */
569 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
570 { nsm_ignore, NSM_TwoWay }, /* SeqNumberMismatch */
571 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
572 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
573 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
574 { nsm_ll_down, NSM_Down }, /* LLDown */
575 },
576 {
577 /* ExStart: */
578 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
579 { nsm_hello_received, NSM_ExStart }, /* HelloReceived */
580 { nsm_ignore, NSM_ExStart }, /* Start */
581 { nsm_ignore, NSM_ExStart }, /* 2-WayReceived */
582 { nsm_negotiation_done, NSM_Exchange }, /* NegotiationDone */
583 { nsm_ignore, NSM_ExStart }, /* ExchangeDone */
584 { nsm_ignore, NSM_ExStart }, /* BadLSReq */
585 { nsm_ignore, NSM_ExStart }, /* LoadingDone */
586 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
587 { nsm_ignore, NSM_ExStart }, /* SeqNumberMismatch */
588 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
589 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
590 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
591 { nsm_ll_down, NSM_Down }, /* LLDown */
592 },
593 {
594 /* Exchange: */
595 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
596 { nsm_hello_received, NSM_Exchange }, /* HelloReceived */
597 { nsm_ignore, NSM_Exchange }, /* Start */
598 { nsm_ignore, NSM_Exchange }, /* 2-WayReceived */
599 { nsm_ignore, NSM_Exchange }, /* NegotiationDone */
600 { nsm_exchange_done, NSM_DependUpon }, /* ExchangeDone */
601 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
602 { nsm_ignore, NSM_Exchange }, /* LoadingDone */
603 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
604 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
605 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
606 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
607 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
608 { nsm_ll_down, NSM_Down }, /* LLDown */
609 },
610 {
611 /* Loading: */
612 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
613 { nsm_hello_received, NSM_Loading }, /* HelloReceived */
614 { nsm_ignore, NSM_Loading }, /* Start */
615 { nsm_ignore, NSM_Loading }, /* 2-WayReceived */
616 { nsm_ignore, NSM_Loading }, /* NegotiationDone */
617 { nsm_ignore, NSM_Loading }, /* ExchangeDone */
618 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
619 { nsm_ignore, NSM_Full }, /* LoadingDone */
620 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
621 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
622 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
623 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
624 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
625 { nsm_ll_down, NSM_Down }, /* LLDown */
626 },
627 { /* Full: */
628 { nsm_ignore, NSM_DependUpon }, /* NoEvent */
629 { nsm_hello_received, NSM_Full }, /* HelloReceived */
630 { nsm_ignore, NSM_Full }, /* Start */
631 { nsm_ignore, NSM_Full }, /* 2-WayReceived */
632 { nsm_ignore, NSM_Full }, /* NegotiationDone */
633 { nsm_ignore, NSM_Full }, /* ExchangeDone */
634 { nsm_bad_ls_req, NSM_ExStart }, /* BadLSReq */
635 { nsm_ignore, NSM_Full }, /* LoadingDone */
636 { nsm_adj_ok, NSM_DependUpon }, /* AdjOK? */
637 { nsm_seq_number_mismatch, NSM_ExStart }, /* SeqNumberMismatch */
638 { nsm_oneway_received, NSM_Init }, /* 1-WayReceived */
639 { nsm_kill_nbr, NSM_Down }, /* KillNbr */
640 { nsm_inactivity_timer, NSM_Down }, /* InactivityTimer */
641 { nsm_ll_down, NSM_Down }, /* LLDown */
642 },
643};
644
eb1ce605 645const static char *ospf_nsm_event_str[] =
2d59836a 646{
647 "NoEvent",
648 "HelloReceived",
649 "Start",
650 "2-WayReceived",
651 "NegotiationDone",
652 "ExchangeDone",
653 "BadLSReq",
654 "LoadingDone",
655 "AdjOK?",
656 "SeqNumberMismatch",
657 "1-WayReceived",
658 "KillNbr",
659 "InactivityTimer",
660 "LLDown",
661};
662
663void
664nsm_change_state (struct ospf_neighbor *nbr, int state)
665{
68980084 666 struct ospf_interface *oi = nbr->oi;
2d59836a 667 struct ospf_area *vl_area = NULL;
668 u_char old_state;
669 int x;
670 int force = 1;
671
672 /* Logging change of status. */
673 if (IS_DEBUG_OSPF (nsm, NSM_STATUS))
2a42e285 674 zlog_debug ("NSM[%s:%s]: State change %s -> %s",
2d59836a 675 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id),
676 LOOKUP (ospf_nsm_state_msg, nbr->state),
677 LOOKUP (ospf_nsm_state_msg, state));
678
679 /* Preserve old status. */
680 old_state = nbr->state;
681
682 /* Change to new status. */
683 nbr->state = state;
684
685 /* Statistics. */
686 nbr->state_change++;
687
2d59836a 688 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
68980084 689 vl_area = ospf_area_lookup_by_area_id (oi->ospf, oi->vl_data->vl_area_id);
d7e60dd7
AS
690
691 /* Optionally notify about adjacency changes */
692 if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_CHANGES) &&
693 (old_state != state) &&
694 (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL) ||
695 (state == NSM_Full) || (state < old_state)))
696 zlog_notice("AdjChg: Nbr %s on %s: %s -> %s",
697 inet_ntoa (nbr->router_id), IF_NAME (nbr->oi),
698 LOOKUP (ospf_nsm_state_msg, old_state),
699 LOOKUP (ospf_nsm_state_msg, state));
700
5e4914c3 701#ifdef HAVE_SNMP
702 /* Terminal state or regression */
703 if ((state == NSM_Full) || (state == NSM_TwoWay) || (state < old_state))
704 {
705 /* ospfVirtNbrStateChange */
706 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
707 ospfTrapVirtNbrStateChange(nbr);
708 /* ospfNbrStateChange trap */
709 else
710 /* To/From FULL, only managed by DR */
711 if (((state != NSM_Full) && (old_state != NSM_Full)) ||
712 (oi->state == ISM_DR))
713 ospfTrapNbrStateChange(nbr);
714 }
715#endif
716
2d59836a 717 /* One of the neighboring routers changes to/from the FULL state. */
718 if ((old_state != NSM_Full && state == NSM_Full) ||
719 (old_state == NSM_Full && state != NSM_Full))
3aa8d5f9 720 {
2d59836a 721 if (state == NSM_Full)
722 {
723 oi->full_nbrs++;
724 oi->area->full_nbrs++;
725
68980084 726 ospf_check_abr_status (oi->ospf);
2d59836a 727
728 if (oi->type == OSPF_IFTYPE_VIRTUALLINK && vl_area)
729 if (++vl_area->full_vls == 1)
68980084 730 ospf_schedule_abr_task (oi->ospf);
2d59836a 731
732 /* kevinm: refresh any redistributions */
68980084 733 for (x = ZEBRA_ROUTE_SYSTEM; x < ZEBRA_ROUTE_MAX; x++)
734 {
735 if (x == ZEBRA_ROUTE_OSPF || x == ZEBRA_ROUTE_OSPF6)
736 continue;
737 ospf_external_lsa_refresh_type (oi->ospf, x, force);
738 }
2d59836a 739 }
740 else
741 {
742 oi->full_nbrs--;
743 oi->area->full_nbrs--;
744
68980084 745 ospf_check_abr_status (oi->ospf);
2d59836a 746
747 if (oi->type == OSPF_IFTYPE_VIRTUALLINK && vl_area)
748 if (vl_area->full_vls > 0)
749 if (--vl_area->full_vls == 0)
68980084 750 ospf_schedule_abr_task (oi->ospf);
2d59836a 751
752 /* clear neighbor retransmit list */
753 if (!ospf_ls_retransmit_isempty (nbr))
754 ospf_ls_retransmit_clear (nbr);
755 }
756
3aa8d5f9 757 zlog_info ("nsm_change_state(%s, %s -> %s): "
758 "scheduling new router-LSA origination",
759 inet_ntoa (nbr->router_id),
760 LOOKUP(ospf_nsm_state_msg, old_state),
761 LOOKUP(ospf_nsm_state_msg, state));
2d59836a 762
763 ospf_router_lsa_timer_add (oi->area);
764
765 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
766 {
767 struct ospf_area *vl_area =
68980084 768 ospf_area_lookup_by_area_id (oi->ospf, oi->vl_data->vl_area_id);
2d59836a 769
770 if (vl_area)
771 ospf_router_lsa_timer_add (vl_area);
772 }
773
774 /* Originate network-LSA. */
775 if (oi->state == ISM_DR)
776 {
777 if (oi->network_lsa_self && oi->full_nbrs == 0)
778 {
779 ospf_lsa_flush_area (oi->network_lsa_self, oi->area);
780 ospf_lsa_unlock (oi->network_lsa_self);
781 oi->network_lsa_self = NULL;
782 OSPF_TIMER_OFF (oi->t_network_lsa_self);
783 }
784 else
785 ospf_network_lsa_timer_add (oi);
786 }
787 }
788
789#ifdef HAVE_OPAQUE_LSA
790 ospf_opaque_nsm_change (nbr, old_state);
791#endif /* HAVE_OPAQUE_LSA */
792
793 /* Start DD exchange protocol */
794 if (state == NSM_ExStart)
795 {
796 if (nbr->dd_seqnum == 0)
797 nbr->dd_seqnum = time (NULL);
798 else
799 nbr->dd_seqnum++;
800
801 nbr->dd_flags = OSPF_DD_FLAG_I|OSPF_DD_FLAG_M|OSPF_DD_FLAG_MS;
802 ospf_db_desc_send (nbr);
803 }
804
805 /* clear cryptographic sequence number */
806 if (state == NSM_Down)
807 nbr->crypt_seqnum = 0;
808
809 /* Generete NeighborChange ISM event. */
810#ifdef BUGGY_ISM_TRANSITION
811 if ((old_state < NSM_TwoWay && state >= NSM_TwoWay) ||
812 (old_state >= NSM_TwoWay && state < NSM_TwoWay))
813 OSPF_ISM_EVENT_EXECUTE (oi, ISM_NeighborChange);
814#else /* BUGGY_ISM_TRANSITION */
815 switch (oi->state) {
816 case ISM_DROther:
817 case ISM_Backup:
818 case ISM_DR:
819 if ((old_state < NSM_TwoWay && state >= NSM_TwoWay) ||
820 (old_state >= NSM_TwoWay && state < NSM_TwoWay))
821 OSPF_ISM_EVENT_EXECUTE (oi, ISM_NeighborChange);
822 break;
823 default:
824 /* ISM_PointToPoint -> ISM_Down, ISM_Loopback -> ISM_Down, etc. */
825 break;
826 }
827#endif /* BUGGY_ISM_TRANSITION */
828
829 /* Performance hack. Send hello immideately when some neighbor enter
830 Init state. This whay we decrease neighbor discovery time. Gleb.*/
831 if (state == NSM_Init)
832 {
833 OSPF_ISM_TIMER_OFF (oi->t_hello);
f9ad937f 834 OSPF_ISM_TIMER_MSEC_ON (oi->t_hello, ospf_hello_timer, 1);
2d59836a 835 }
836
837 /* Preserve old status? */
838}
839
840/* Execute NSM event process. */
841int
842ospf_nsm_event (struct thread *thread)
843{
844 int event;
845 int next_state;
846 struct ospf_neighbor *nbr;
847 struct in_addr router_id;
848 int old_state;
849 struct ospf_interface *oi;
850
851 nbr = THREAD_ARG (thread);
852 event = THREAD_VAL (thread);
853 router_id = nbr->router_id;
854
855 old_state = nbr->state;
856 oi = nbr->oi ;
857
858 /* Call function. */
859 next_state = (*(NSM [nbr->state][event].func))(nbr);
860
861 /* When event is NSM_KillNbr or InactivityTimer, the neighbor is
862 deleted. */
863 if (event == NSM_KillNbr || event == NSM_InactivityTimer)
864 {
865 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
2a42e285 866 zlog_debug ("NSM[%s:%s]: neighbor deleted",
2d59836a 867 IF_NAME (oi), inet_ntoa (router_id));
868
869 /* Timers are canceled in ospf_nbr_free, moreover we cannot call
870 nsm_timer_set here because nbr is freed already!!!*/
871 /*nsm_timer_set (nbr);*/
872
873 return 0;
874 }
875
876 if (! next_state)
877 next_state = NSM [nbr->state][event].next_state;
ba0beb4a
PJ
878 else if (NSM [nbr->state][event].next_state != NSM_DependUpon)
879 {
880 /* There's a mismatch between the FSM tables and what an FSM
881 * action/state-change function returned. State changes which
882 * do not have conditional/DependUpon next-states should not
883 * try set next_state.
884 */
885 zlog_warn ("NSM[%s:%s]: %s (%s): "
886 "Warning: action tried to change next_state to %s",
887 IF_NAME (oi), inet_ntoa (nbr->router_id),
888 LOOKUP (ospf_nsm_state_msg, nbr->state),
889 ospf_nsm_event_str [event],
890 LOOKUP (ospf_nsm_state_msg, next_state));
891
892 next_state = NSM [nbr->state][event].next_state;
893 }
2d59836a 894
895 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
2a42e285 896 zlog_debug ("NSM[%s:%s]: %s (%s)", IF_NAME (oi),
2d59836a 897 inet_ntoa (nbr->router_id),
898 LOOKUP (ospf_nsm_state_msg, nbr->state),
899 ospf_nsm_event_str [event]);
900
901 /* If state is changed. */
902 if (next_state != nbr->state)
903 nsm_change_state (nbr, next_state);
904
905 /* Make sure timer is set. */
906 nsm_timer_set (nbr);
907
908 return 0;
909}
910
911/* Check loading state. */
912void
913ospf_check_nbr_loading (struct ospf_neighbor *nbr)
914{
915 if (nbr->state == NSM_Loading)
916 {
917 if (ospf_ls_request_isempty (nbr))
918 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_LoadingDone);
919 else if (nbr->ls_req_last == NULL)
920 ospf_ls_req_event (nbr);
921 }
922}