2 * OSPF version 2 Neighbor State Machine
3 * From RFC2328 [OSPF Version 2]
4 * Copyright (C) 1999, 2000 Toshiaki Takada
6 * This file is part of GNU Zebra.
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
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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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"
50 #include "ospfd/ospf_bfd.h"
52 DEFINE_HOOK(ospf_nsm_change
,
53 (struct ospf_neighbor
* on
, int state
, int oldstate
),
54 (on
, state
, oldstate
))
56 static void nsm_clear_adj(struct ospf_neighbor
*);
58 /* OSPF NSM Timer functions. */
59 static int ospf_inactivity_timer(struct thread
*thread
)
61 struct ospf_neighbor
*nbr
;
63 nbr
= THREAD_ARG(thread
);
64 nbr
->t_inactivity
= NULL
;
66 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
67 zlog_debug("NSM[%s:%s]: Timer (Inactivity timer expire)",
68 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
));
70 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_InactivityTimer
);
75 static int ospf_db_desc_timer(struct thread
*thread
)
77 struct ospf_neighbor
*nbr
;
79 nbr
= THREAD_ARG(thread
);
80 nbr
->t_db_desc
= NULL
;
82 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
83 zlog_debug("NSM[%s:%s]: Timer (DD Retransmit timer expire)",
84 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->src
));
86 /* resent last send DD packet. */
87 assert(nbr
->last_send
);
88 ospf_db_desc_resend(nbr
);
90 /* DD Retransmit timer set. */
91 OSPF_NSM_TIMER_ON(nbr
->t_db_desc
, ospf_db_desc_timer
, nbr
->v_db_desc
);
96 /* Hook function called after ospf NSM event is occured.
98 * Set/clear any timers whose condition is implicit to the neighbour
99 * state. There may be other timers which are set/unset according to other
102 * We rely on this function to properly clear timers in lower states,
103 * particularly before deleting a neighbour.
105 static void nsm_timer_set(struct ospf_neighbor
*nbr
)
107 switch (nbr
->state
) {
110 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
111 OSPF_NSM_TIMER_OFF(nbr
->t_hello_reply
);
116 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
117 OSPF_NSM_TIMER_OFF(nbr
->t_ls_upd
);
118 OSPF_NSM_TIMER_OFF(nbr
->t_ls_req
);
121 OSPF_NSM_TIMER_ON(nbr
->t_db_desc
, ospf_db_desc_timer
,
123 OSPF_NSM_TIMER_OFF(nbr
->t_ls_upd
);
124 OSPF_NSM_TIMER_OFF(nbr
->t_ls_req
);
127 OSPF_NSM_TIMER_ON(nbr
->t_ls_upd
, ospf_ls_upd_timer
,
129 if (!IS_SET_DD_MS(nbr
->dd_flags
))
130 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
135 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
140 /* 10.4 of RFC2328, indicate whether an adjacency is appropriate with
141 * the given neighbour
143 static int nsm_should_adj(struct ospf_neighbor
*nbr
)
145 struct ospf_interface
*oi
= nbr
->oi
;
147 /* These network types must always form adjacencies. */
148 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
149 || oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
150 || oi
->type
== OSPF_IFTYPE_VIRTUALLINK
151 /* Router itself is the DRouter or the BDRouter. */
152 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
))
153 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &BDR(oi
))
154 /* Neighboring Router is the DRouter or the BDRouter. */
155 || IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &DR(oi
))
156 || IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &BDR(oi
)))
162 /* OSPF NSM functions. */
163 static int nsm_packet_received(struct ospf_neighbor
*nbr
)
165 /* Start or Restart Inactivity Timer. */
166 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
168 OSPF_NSM_TIMER_ON(nbr
->t_inactivity
, ospf_inactivity_timer
,
171 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->nbr_nbma
)
172 OSPF_POLL_TIMER_OFF(nbr
->nbr_nbma
->t_poll
);
174 /* Send proactive ARP requests */
175 if (nbr
->state
< NSM_Exchange
)
176 ospf_proactively_arp(nbr
);
181 static int nsm_start(struct ospf_neighbor
*nbr
)
184 OSPF_POLL_TIMER_OFF(nbr
->nbr_nbma
->t_poll
);
186 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
188 OSPF_NSM_TIMER_ON(nbr
->t_inactivity
, ospf_inactivity_timer
,
191 /* Send proactive ARP requests */
192 ospf_proactively_arp(nbr
);
197 static int nsm_twoway_received(struct ospf_neighbor
*nbr
)
199 int adj
= nsm_should_adj(nbr
);
201 /* Send proactive ARP requests */
203 ospf_proactively_arp(nbr
);
205 return (adj
? NSM_ExStart
: NSM_TwoWay
);
208 int ospf_db_summary_count(struct ospf_neighbor
*nbr
)
210 return ospf_lsdb_count_all(&nbr
->db_sum
);
213 int ospf_db_summary_isempty(struct ospf_neighbor
*nbr
)
215 return ospf_lsdb_isempty(&nbr
->db_sum
);
218 static int ospf_db_summary_add(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
220 switch (lsa
->data
->type
) {
221 case OSPF_OPAQUE_LINK_LSA
:
222 /* Exclude type-9 LSAs that does not have the same "oi" with
224 if (nbr
->oi
&& ospf_if_exists(lsa
->oi
) != nbr
->oi
)
227 case OSPF_OPAQUE_AREA_LSA
:
229 * It is assured by the caller function "nsm_negotiation_done()"
230 * that every given LSA belongs to the same area with "nbr".
233 case OSPF_OPAQUE_AS_LSA
:
238 /* Stay away from any Local Translated Type-7 LSAs */
239 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
242 if (IS_LSA_MAXAGE(lsa
))
243 ospf_ls_retransmit_add(nbr
, lsa
);
245 ospf_lsdb_add(&nbr
->db_sum
, lsa
);
250 void ospf_db_summary_clear(struct ospf_neighbor
*nbr
)
252 struct ospf_lsdb
*lsdb
;
256 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
257 struct route_table
*table
= lsdb
->type
[i
].db
;
258 struct route_node
*rn
;
260 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
262 ospf_lsdb_delete(&nbr
->db_sum
, rn
->info
);
267 /* The area link state database consists of the router-LSAs,
268 network-LSAs and summary-LSAs contained in the area structure,
269 along with the AS-external-LSAs contained in the global structure.
270 AS-external-LSAs are omitted from a virtual neighbor's Database
271 summary list. AS-external-LSAs are omitted from the Database
272 summary list if the area has been configured as a stub. */
273 static int nsm_negotiation_done(struct ospf_neighbor
*nbr
)
275 struct ospf_area
*area
= nbr
->oi
->area
;
276 struct ospf_lsa
*lsa
;
277 struct route_node
*rn
;
279 /* Send proactive ARP requests */
280 ospf_proactively_arp(nbr
);
282 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
283 ospf_db_summary_add(nbr
, lsa
);
284 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
285 ospf_db_summary_add(nbr
, lsa
);
286 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
287 ospf_db_summary_add(nbr
, lsa
);
288 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
289 ospf_db_summary_add(nbr
, lsa
);
291 /* Process only if the neighbor is opaque capable. */
292 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
293 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
294 ospf_db_summary_add(nbr
, lsa
);
295 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
296 ospf_db_summary_add(nbr
, lsa
);
299 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_NP
)) {
300 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
301 ospf_db_summary_add(nbr
, lsa
);
304 if (nbr
->oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
305 && area
->external_routing
== OSPF_AREA_DEFAULT
)
306 LSDB_LOOP (EXTERNAL_LSDB(nbr
->oi
->ospf
), rn
, lsa
)
307 ospf_db_summary_add(nbr
, lsa
);
309 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
310 && (nbr
->oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
311 && area
->external_routing
== OSPF_AREA_DEFAULT
))
312 LSDB_LOOP (OPAQUE_AS_LSDB(nbr
->oi
->ospf
), rn
, lsa
)
313 ospf_db_summary_add(nbr
, lsa
);
318 static int nsm_exchange_done(struct ospf_neighbor
*nbr
)
320 if (ospf_ls_request_isempty(nbr
))
323 /* Send Link State Request. */
324 if (nbr
->t_ls_req
== NULL
)
325 ospf_ls_req_send(nbr
);
330 static int nsm_adj_ok(struct ospf_neighbor
*nbr
)
332 int next_state
= nbr
->state
;
333 int adj
= nsm_should_adj(nbr
);
335 if (nbr
->state
== NSM_TwoWay
&& adj
== 1) {
336 next_state
= NSM_ExStart
;
338 /* Send proactive ARP requests */
339 ospf_proactively_arp(nbr
);
340 } else if (nbr
->state
>= NSM_ExStart
&& adj
== 0)
341 next_state
= NSM_TwoWay
;
346 /* Clear adjacency related state for a neighbour, intended where nbr
347 * transitions from > ExStart (i.e. a Full or forming adjacency)
350 static void nsm_clear_adj(struct ospf_neighbor
*nbr
)
352 /* Clear Database Summary list. */
353 if (!ospf_db_summary_isempty(nbr
))
354 ospf_db_summary_clear(nbr
);
356 /* Clear Link State Request list. */
357 if (!ospf_ls_request_isempty(nbr
))
358 ospf_ls_request_delete_all(nbr
);
360 /* Clear Link State Retransmission list. */
361 if (!ospf_ls_retransmit_isempty(nbr
))
362 ospf_ls_retransmit_clear(nbr
);
364 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
))
365 UNSET_FLAG(nbr
->options
, OSPF_OPTION_O
);
368 static int nsm_kill_nbr(struct ospf_neighbor
*nbr
)
370 /* killing nbr_self is invalid */
371 if (nbr
== nbr
->oi
->nbr_self
) {
372 assert(nbr
!= nbr
->oi
->nbr_self
);
376 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->nbr_nbma
!= NULL
) {
377 struct ospf_nbr_nbma
*nbr_nbma
= nbr
->nbr_nbma
;
379 nbr_nbma
->nbr
= NULL
;
380 nbr_nbma
->state_change
= nbr
->state_change
;
382 nbr
->nbr_nbma
= NULL
;
384 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
387 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
389 "NSM[%s:%s]: Down (PollIntervalTimer scheduled)",
391 inet_ntoa(nbr
->address
.u
.prefix4
));
397 /* Neighbor State Machine */
399 int (*func
)(struct ospf_neighbor
*);
401 } NSM
[OSPF_NSM_STATE_MAX
][OSPF_NSM_EVENT_MAX
] = {
403 /* DependUpon: dummy state. */
404 {NULL
, NSM_DependUpon
}, /* NoEvent */
405 {NULL
, NSM_DependUpon
}, /* PacketReceived */
406 {NULL
, NSM_DependUpon
}, /* Start */
407 {NULL
, NSM_DependUpon
}, /* 2-WayReceived */
408 {NULL
, NSM_DependUpon
}, /* NegotiationDone */
409 {NULL
, NSM_DependUpon
}, /* ExchangeDone */
410 {NULL
, NSM_DependUpon
}, /* BadLSReq */
411 {NULL
, NSM_DependUpon
}, /* LoadingDone */
412 {NULL
, NSM_DependUpon
}, /* AdjOK? */
413 {NULL
, NSM_DependUpon
}, /* SeqNumberMismatch */
414 {NULL
, NSM_DependUpon
}, /* 1-WayReceived */
415 {NULL
, NSM_DependUpon
}, /* KillNbr */
416 {NULL
, NSM_DependUpon
}, /* InactivityTimer */
417 {NULL
, NSM_DependUpon
}, /* LLDown */
420 /* Deleted: dummy state. */
421 {NULL
, NSM_Deleted
}, /* NoEvent */
422 {NULL
, NSM_Deleted
}, /* PacketReceived */
423 {NULL
, NSM_Deleted
}, /* Start */
424 {NULL
, NSM_Deleted
}, /* 2-WayReceived */
425 {NULL
, NSM_Deleted
}, /* NegotiationDone */
426 {NULL
, NSM_Deleted
}, /* ExchangeDone */
427 {NULL
, NSM_Deleted
}, /* BadLSReq */
428 {NULL
, NSM_Deleted
}, /* LoadingDone */
429 {NULL
, NSM_Deleted
}, /* AdjOK? */
430 {NULL
, NSM_Deleted
}, /* SeqNumberMismatch */
431 {NULL
, NSM_Deleted
}, /* 1-WayReceived */
432 {NULL
, NSM_Deleted
}, /* KillNbr */
433 {NULL
, NSM_Deleted
}, /* InactivityTimer */
434 {NULL
, NSM_Deleted
}, /* LLDown */
438 {NULL
, NSM_DependUpon
}, /* NoEvent */
439 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
440 {nsm_start
, NSM_Attempt
}, /* Start */
441 {NULL
, NSM_Down
}, /* 2-WayReceived */
442 {NULL
, NSM_Down
}, /* NegotiationDone */
443 {NULL
, NSM_Down
}, /* ExchangeDone */
444 {NULL
, NSM_Down
}, /* BadLSReq */
445 {NULL
, NSM_Down
}, /* LoadingDone */
446 {NULL
, NSM_Down
}, /* AdjOK? */
447 {NULL
, NSM_Down
}, /* SeqNumberMismatch */
448 {NULL
, NSM_Down
}, /* 1-WayReceived */
449 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
450 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
451 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
455 {NULL
, NSM_DependUpon
}, /* NoEvent */
456 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
457 {NULL
, NSM_Attempt
}, /* Start */
458 {NULL
, NSM_Attempt
}, /* 2-WayReceived */
459 {NULL
, NSM_Attempt
}, /* NegotiationDone */
460 {NULL
, NSM_Attempt
}, /* ExchangeDone */
461 {NULL
, NSM_Attempt
}, /* BadLSReq */
462 {NULL
, NSM_Attempt
}, /* LoadingDone */
463 {NULL
, NSM_Attempt
}, /* AdjOK? */
464 {NULL
, NSM_Attempt
}, /* SeqNumberMismatch */
465 {NULL
, NSM_Attempt
}, /* 1-WayReceived */
466 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
467 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
468 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
472 {NULL
, NSM_DependUpon
}, /* NoEvent */
473 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
474 {NULL
, NSM_Init
}, /* Start */
475 {nsm_twoway_received
, NSM_DependUpon
}, /* 2-WayReceived */
476 {NULL
, NSM_Init
}, /* NegotiationDone */
477 {NULL
, NSM_Init
}, /* ExchangeDone */
478 {NULL
, NSM_Init
}, /* BadLSReq */
479 {NULL
, NSM_Init
}, /* LoadingDone */
480 {NULL
, NSM_Init
}, /* AdjOK? */
481 {NULL
, NSM_Init
}, /* SeqNumberMismatch */
482 {NULL
, NSM_Init
}, /* 1-WayReceived */
483 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
484 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
485 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
489 {NULL
, NSM_DependUpon
}, /* NoEvent */
490 {nsm_packet_received
, NSM_TwoWay
}, /* HelloReceived */
491 {NULL
, NSM_TwoWay
}, /* Start */
492 {NULL
, NSM_TwoWay
}, /* 2-WayReceived */
493 {NULL
, NSM_TwoWay
}, /* NegotiationDone */
494 {NULL
, NSM_TwoWay
}, /* ExchangeDone */
495 {NULL
, NSM_TwoWay
}, /* BadLSReq */
496 {NULL
, NSM_TwoWay
}, /* LoadingDone */
497 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
498 {NULL
, NSM_TwoWay
}, /* SeqNumberMismatch */
499 {NULL
, NSM_Init
}, /* 1-WayReceived */
500 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
501 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
502 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
506 {NULL
, NSM_DependUpon
}, /* NoEvent */
507 {nsm_packet_received
, NSM_ExStart
}, /* PacaketReceived */
508 {NULL
, NSM_ExStart
}, /* Start */
509 {NULL
, NSM_ExStart
}, /* 2-WayReceived */
510 {nsm_negotiation_done
, NSM_Exchange
}, /* NegotiationDone */
511 {NULL
, NSM_ExStart
}, /* ExchangeDone */
512 {NULL
, NSM_ExStart
}, /* BadLSReq */
513 {NULL
, NSM_ExStart
}, /* LoadingDone */
514 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
515 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
516 {NULL
, NSM_Init
}, /* 1-WayReceived */
517 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
518 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
519 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
523 {NULL
, NSM_DependUpon
}, /* NoEvent */
524 {nsm_packet_received
, NSM_Exchange
}, /* PacketReceived */
525 {NULL
, NSM_Exchange
}, /* Start */
526 {NULL
, NSM_Exchange
}, /* 2-WayReceived */
527 {NULL
, NSM_Exchange
}, /* NegotiationDone */
528 {nsm_exchange_done
, NSM_DependUpon
}, /* ExchangeDone */
529 {NULL
, NSM_ExStart
}, /* BadLSReq */
530 {NULL
, NSM_Exchange
}, /* LoadingDone */
531 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
532 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
533 {NULL
, NSM_Init
}, /* 1-WayReceived */
534 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
535 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
536 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
540 {NULL
, NSM_DependUpon
}, /* NoEvent */
541 {nsm_packet_received
, NSM_Loading
}, /* PacketReceived */
542 {NULL
, NSM_Loading
}, /* Start */
543 {NULL
, NSM_Loading
}, /* 2-WayReceived */
544 {NULL
, NSM_Loading
}, /* NegotiationDone */
545 {NULL
, NSM_Loading
}, /* ExchangeDone */
546 {NULL
, NSM_ExStart
}, /* BadLSReq */
547 {NULL
, NSM_Full
}, /* LoadingDone */
548 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
549 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
550 {NULL
, NSM_Init
}, /* 1-WayReceived */
551 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
552 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
553 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
557 {NULL
, NSM_DependUpon
}, /* NoEvent */
558 {nsm_packet_received
, NSM_Full
}, /* PacketReceived */
559 {NULL
, NSM_Full
}, /* Start */
560 {NULL
, NSM_Full
}, /* 2-WayReceived */
561 {NULL
, NSM_Full
}, /* NegotiationDone */
562 {NULL
, NSM_Full
}, /* ExchangeDone */
563 {NULL
, NSM_ExStart
}, /* BadLSReq */
564 {NULL
, NSM_Full
}, /* LoadingDone */
565 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
566 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
567 {NULL
, NSM_Init
}, /* 1-WayReceived */
568 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
569 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
570 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
574 static const char *ospf_nsm_event_str
[] = {
575 "NoEvent", "PacketReceived", "Start",
576 "2-WayReceived", "NegotiationDone", "ExchangeDone",
577 "BadLSReq", "LoadingDone", "AdjOK?",
578 "SeqNumberMismatch", "1-WayReceived", "KillNbr",
579 "InactivityTimer", "LLDown",
582 static void nsm_notice_state_change(struct ospf_neighbor
*nbr
, int next_state
,
585 /* Logging change of status. */
586 if (IS_DEBUG_OSPF(nsm
, NSM_STATUS
))
587 zlog_debug("NSM[%s:%s]: State change %s -> %s (%s)",
588 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
),
589 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
590 lookup_msg(ospf_nsm_state_msg
, next_state
, NULL
),
591 ospf_nsm_event_str
[event
]);
593 /* Optionally notify about adjacency changes */
594 if (CHECK_FLAG(nbr
->oi
->ospf
->config
, OSPF_LOG_ADJACENCY_CHANGES
)
595 && (CHECK_FLAG(nbr
->oi
->ospf
->config
, OSPF_LOG_ADJACENCY_DETAIL
)
596 || (next_state
== NSM_Full
) || (next_state
< nbr
->state
)))
597 zlog_notice("AdjChg: Nbr %s on %s: %s -> %s (%s)",
598 inet_ntoa(nbr
->router_id
), IF_NAME(nbr
->oi
),
599 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
600 lookup_msg(ospf_nsm_state_msg
, next_state
, NULL
),
601 ospf_nsm_event_str
[event
]);
604 if (next_state
> nbr
->state
)
605 monotime(&nbr
->ts_last_progress
);
606 else /* regression in NSM */
608 monotime(&nbr
->ts_last_regress
);
609 nbr
->last_regress_str
= ospf_nsm_event_str
[event
];
613 static void nsm_change_state(struct ospf_neighbor
*nbr
, int state
)
615 struct ospf_interface
*oi
= nbr
->oi
;
616 struct ospf_area
*vl_area
= NULL
;
621 /* Preserve old status. */
622 old_state
= nbr
->state
;
624 /* Change to new status. */
630 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
631 vl_area
= ospf_area_lookup_by_area_id(oi
->ospf
,
632 oi
->vl_data
->vl_area_id
);
634 /* Generate NeighborChange ISM event.
636 * In response to NeighborChange, DR election is rerun. The information
637 * from the election process is required by the router-lsa construction.
639 * Therefore, trigger the event prior to refreshing the LSAs. */
644 if ((old_state
< NSM_TwoWay
&& state
>= NSM_TwoWay
)
645 || (old_state
>= NSM_TwoWay
&& state
< NSM_TwoWay
))
646 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_NeighborChange
);
649 /* ISM_PointToPoint -> ISM_Down, ISM_Loopback -> ISM_Down, etc.
654 /* One of the neighboring routers changes to/from the FULL state. */
655 if ((old_state
!= NSM_Full
&& state
== NSM_Full
)
656 || (old_state
== NSM_Full
&& state
!= NSM_Full
)) {
657 if (state
== NSM_Full
) {
659 oi
->area
->full_nbrs
++;
661 ospf_check_abr_status(oi
->ospf
);
663 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
&& vl_area
)
664 if (++vl_area
->full_vls
== 1)
665 ospf_schedule_abr_task(oi
->ospf
);
667 /* kevinm: refresh any redistributions */
668 for (x
= ZEBRA_ROUTE_SYSTEM
; x
< ZEBRA_ROUTE_MAX
; x
++) {
669 struct list
*red_list
;
670 struct listnode
*node
;
671 struct ospf_redist
*red
;
673 if (x
== ZEBRA_ROUTE_OSPF6
)
676 red_list
= oi
->ospf
->redist
[x
];
680 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
681 ospf_external_lsa_refresh_type(
682 oi
->ospf
, x
, red
->instance
,
685 /* XXX: Clearly some thing is wrong with refresh of
687 * this added to hack around defaults not refreshing
691 ospf_external_lsa_refresh_default(oi
->ospf
);
694 oi
->area
->full_nbrs
--;
696 ospf_check_abr_status(oi
->ospf
);
698 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
&& vl_area
)
699 if (vl_area
->full_vls
> 0)
700 if (--vl_area
->full_vls
== 0)
701 ospf_schedule_abr_task(
705 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_LOG_ADJACENCY_DETAIL
))
707 "%s:(%s, %s -> %s): "
708 "scheduling new router-LSA origination",
709 __PRETTY_FUNCTION__
, inet_ntoa(nbr
->router_id
),
710 lookup_msg(ospf_nsm_state_msg
, old_state
, NULL
),
711 lookup_msg(ospf_nsm_state_msg
, state
, NULL
));
713 ospf_router_lsa_update_area(oi
->area
);
715 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
) {
716 struct ospf_area
*vl_area
= ospf_area_lookup_by_area_id(
717 oi
->ospf
, oi
->vl_data
->vl_area_id
);
720 ospf_router_lsa_update_area(vl_area
);
723 /* Originate network-LSA. */
724 if (oi
->state
== ISM_DR
) {
725 if (oi
->network_lsa_self
&& oi
->full_nbrs
== 0) {
726 ospf_lsa_flush_area(oi
->network_lsa_self
,
728 ospf_lsa_unlock(&oi
->network_lsa_self
);
729 oi
->network_lsa_self
= NULL
;
731 ospf_network_lsa_update(oi
);
735 ospf_opaque_nsm_change(nbr
, old_state
);
737 /* State changes from > ExStart to <= ExStart should clear any Exchange
738 * or Full/LSA Update related lists and state.
739 * Potential causal events: BadLSReq, SeqNumberMismatch, AdjOK?
741 if ((old_state
> NSM_ExStart
) && (state
<= NSM_ExStart
))
744 /* Start DD exchange protocol */
745 if (state
== NSM_ExStart
) {
746 if (nbr
->dd_seqnum
== 0)
747 nbr
->dd_seqnum
= (uint32_t)random();
752 OSPF_DD_FLAG_I
| OSPF_DD_FLAG_M
| OSPF_DD_FLAG_MS
;
753 ospf_db_desc_send(nbr
);
756 /* clear cryptographic sequence number */
757 if (state
== NSM_Down
)
758 nbr
->crypt_seqnum
= 0;
760 ospf_bfd_trigger_event(nbr
, old_state
, state
);
762 /* Preserve old status? */
765 /* Execute NSM event process. */
766 int ospf_nsm_event(struct thread
*thread
)
770 struct ospf_neighbor
*nbr
;
772 nbr
= THREAD_ARG(thread
);
773 event
= THREAD_VAL(thread
);
775 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
776 zlog_debug("NSM[%s:%s]: %s (%s)", IF_NAME(nbr
->oi
),
777 inet_ntoa(nbr
->router_id
),
778 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
779 ospf_nsm_event_str
[event
]);
781 next_state
= NSM
[nbr
->state
][event
].next_state
;
784 if (NSM
[nbr
->state
][event
].func
!= NULL
) {
785 int func_state
= (*(NSM
[nbr
->state
][event
].func
))(nbr
);
787 if (NSM
[nbr
->state
][event
].next_state
== NSM_DependUpon
)
788 next_state
= func_state
;
789 else if (func_state
) {
790 /* There's a mismatch between the FSM tables and what an
792 * action/state-change function returned. State changes
794 * do not have conditional/DependUpon next-states should
796 * try set next_state.
799 "NSM[%s:%s]: %s (%s): "
800 "Warning: action tried to change next_state to %s",
801 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
),
802 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
804 ospf_nsm_event_str
[event
],
805 lookup_msg(ospf_nsm_state_msg
, func_state
,
810 assert(next_state
!= NSM_DependUpon
);
812 /* If state is changed. */
813 if (next_state
!= nbr
->state
) {
814 int old_state
= nbr
->state
;
816 nsm_notice_state_change(nbr
, next_state
, event
);
817 nsm_change_state(nbr
, next_state
);
819 hook_call(ospf_nsm_change
, nbr
, next_state
, old_state
);
822 /* Make sure timer is set. */
825 /* When event is NSM_KillNbr, InactivityTimer or LLDown, the neighbor
828 * Rather than encode knowledge here of which events lead to NBR
829 * delete, we take our cue from the NSM table, via the dummy
830 * 'Deleted' neighbour state.
832 if (nbr
->state
== NSM_Deleted
)
833 ospf_nbr_delete(nbr
);
838 /* Check loading state. */
839 void ospf_check_nbr_loading(struct ospf_neighbor
*nbr
)
841 if (nbr
->state
== NSM_Loading
) {
842 if (ospf_ls_request_isempty(nbr
))
843 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_LoadingDone
);
844 else if (nbr
->ls_req_last
== NULL
)
845 ospf_ls_req_event(nbr
);