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"
51 #include "ospfd/ospf_errors.h"
53 DEFINE_HOOK(ospf_nsm_change
,
54 (struct ospf_neighbor
* on
, int state
, int oldstate
),
55 (on
, state
, oldstate
))
57 static void nsm_clear_adj(struct ospf_neighbor
*);
59 /* OSPF NSM Timer functions. */
60 static int ospf_inactivity_timer(struct thread
*thread
)
62 struct ospf_neighbor
*nbr
;
64 nbr
= THREAD_ARG(thread
);
65 nbr
->t_inactivity
= NULL
;
67 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
68 zlog_debug("NSM[%s:%s]: Timer (Inactivity timer expire)",
69 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
));
71 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_InactivityTimer
);
76 static int ospf_db_desc_timer(struct thread
*thread
)
78 struct ospf_neighbor
*nbr
;
80 nbr
= THREAD_ARG(thread
);
81 nbr
->t_db_desc
= NULL
;
83 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
84 zlog_debug("NSM[%s:%s]: Timer (DD Retransmit timer expire)",
85 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->src
));
87 /* resent last send DD packet. */
88 assert(nbr
->last_send
);
89 ospf_db_desc_resend(nbr
);
91 /* DD Retransmit timer set. */
92 OSPF_NSM_TIMER_ON(nbr
->t_db_desc
, ospf_db_desc_timer
, nbr
->v_db_desc
);
97 /* Hook function called after ospf NSM event is occurred.
99 * Set/clear any timers whose condition is implicit to the neighbour
100 * state. There may be other timers which are set/unset according to other
103 * We rely on this function to properly clear timers in lower states,
104 * particularly before deleting a neighbour.
106 static void nsm_timer_set(struct ospf_neighbor
*nbr
)
108 switch (nbr
->state
) {
111 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
112 OSPF_NSM_TIMER_OFF(nbr
->t_hello_reply
);
117 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
118 OSPF_NSM_TIMER_OFF(nbr
->t_ls_upd
);
119 OSPF_NSM_TIMER_OFF(nbr
->t_ls_req
);
122 OSPF_NSM_TIMER_ON(nbr
->t_db_desc
, ospf_db_desc_timer
,
124 OSPF_NSM_TIMER_OFF(nbr
->t_ls_upd
);
125 OSPF_NSM_TIMER_OFF(nbr
->t_ls_req
);
128 OSPF_NSM_TIMER_ON(nbr
->t_ls_upd
, ospf_ls_upd_timer
,
130 if (!IS_SET_DD_MS(nbr
->dd_flags
))
131 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
136 OSPF_NSM_TIMER_OFF(nbr
->t_db_desc
);
141 /* 10.4 of RFC2328, indicate whether an adjacency is appropriate with
142 * the given neighbour
144 static int nsm_should_adj(struct ospf_neighbor
*nbr
)
146 struct ospf_interface
*oi
= nbr
->oi
;
148 /* These network types must always form adjacencies. */
149 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
150 || oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
151 || oi
->type
== OSPF_IFTYPE_VIRTUALLINK
152 /* Router itself is the DRouter or the BDRouter. */
153 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
))
154 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &BDR(oi
))
155 /* Neighboring Router is the DRouter or the BDRouter. */
156 || IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &DR(oi
))
157 || IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &BDR(oi
)))
163 /* OSPF NSM functions. */
164 static int nsm_packet_received(struct ospf_neighbor
*nbr
)
166 /* Start or Restart Inactivity Timer. */
167 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
169 OSPF_NSM_TIMER_ON(nbr
->t_inactivity
, ospf_inactivity_timer
,
172 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->nbr_nbma
)
173 OSPF_POLL_TIMER_OFF(nbr
->nbr_nbma
->t_poll
);
175 /* Send proactive ARP requests */
176 if (nbr
->state
< NSM_Exchange
)
177 ospf_proactively_arp(nbr
);
182 static int nsm_start(struct ospf_neighbor
*nbr
)
185 OSPF_POLL_TIMER_OFF(nbr
->nbr_nbma
->t_poll
);
187 OSPF_NSM_TIMER_OFF(nbr
->t_inactivity
);
189 OSPF_NSM_TIMER_ON(nbr
->t_inactivity
, ospf_inactivity_timer
,
192 /* Send proactive ARP requests */
193 ospf_proactively_arp(nbr
);
198 static int nsm_twoway_received(struct ospf_neighbor
*nbr
)
200 int adj
= nsm_should_adj(nbr
);
202 /* Send proactive ARP requests */
204 ospf_proactively_arp(nbr
);
206 return (adj
? NSM_ExStart
: NSM_TwoWay
);
209 int ospf_db_summary_count(struct ospf_neighbor
*nbr
)
211 return ospf_lsdb_count_all(&nbr
->db_sum
);
214 int ospf_db_summary_isempty(struct ospf_neighbor
*nbr
)
216 return ospf_lsdb_isempty(&nbr
->db_sum
);
219 static int ospf_db_summary_add(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
221 switch (lsa
->data
->type
) {
222 case OSPF_OPAQUE_LINK_LSA
:
223 /* Exclude type-9 LSAs that does not have the same "oi" with
225 if (nbr
->oi
&& ospf_if_exists(lsa
->oi
) != nbr
->oi
)
228 case OSPF_OPAQUE_AREA_LSA
:
230 * It is assured by the caller function "nsm_negotiation_done()"
231 * that every given LSA belongs to the same area with "nbr".
234 case OSPF_OPAQUE_AS_LSA
:
239 /* Stay away from any Local Translated Type-7 LSAs */
240 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
243 if (IS_LSA_MAXAGE(lsa
))
244 ospf_ls_retransmit_add(nbr
, lsa
);
246 ospf_lsdb_add(&nbr
->db_sum
, lsa
);
251 void ospf_db_summary_clear(struct ospf_neighbor
*nbr
)
253 struct ospf_lsdb
*lsdb
;
257 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
258 struct route_table
*table
= lsdb
->type
[i
].db
;
259 struct route_node
*rn
;
261 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
263 ospf_lsdb_delete(&nbr
->db_sum
, rn
->info
);
268 /* The area link state database consists of the router-LSAs,
269 network-LSAs and summary-LSAs contained in the area structure,
270 along with the AS-external-LSAs contained in the global structure.
271 AS-external-LSAs are omitted from a virtual neighbor's Database
272 summary list. AS-external-LSAs are omitted from the Database
273 summary list if the area has been configured as a stub. */
274 static int nsm_negotiation_done(struct ospf_neighbor
*nbr
)
276 struct ospf_area
*area
= nbr
->oi
->area
;
277 struct ospf_lsa
*lsa
;
278 struct route_node
*rn
;
280 /* Send proactive ARP requests */
281 ospf_proactively_arp(nbr
);
283 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
284 ospf_db_summary_add(nbr
, lsa
);
285 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
286 ospf_db_summary_add(nbr
, lsa
);
287 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
288 ospf_db_summary_add(nbr
, lsa
);
289 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
290 ospf_db_summary_add(nbr
, lsa
);
292 /* Process only if the neighbor is opaque capable. */
293 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
294 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
295 ospf_db_summary_add(nbr
, lsa
);
296 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
297 ospf_db_summary_add(nbr
, lsa
);
300 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_NP
)) {
301 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
302 ospf_db_summary_add(nbr
, lsa
);
305 if (nbr
->oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
306 && area
->external_routing
== OSPF_AREA_DEFAULT
)
307 LSDB_LOOP (EXTERNAL_LSDB(nbr
->oi
->ospf
), rn
, lsa
)
308 ospf_db_summary_add(nbr
, lsa
);
310 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
311 && (nbr
->oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
312 && area
->external_routing
== OSPF_AREA_DEFAULT
))
313 LSDB_LOOP (OPAQUE_AS_LSDB(nbr
->oi
->ospf
), rn
, lsa
)
314 ospf_db_summary_add(nbr
, lsa
);
319 static int nsm_exchange_done(struct ospf_neighbor
*nbr
)
321 if (ospf_ls_request_isempty(nbr
))
324 /* Send Link State Request. */
325 if (nbr
->t_ls_req
== NULL
)
326 ospf_ls_req_send(nbr
);
331 static int nsm_adj_ok(struct ospf_neighbor
*nbr
)
333 int next_state
= nbr
->state
;
334 int adj
= nsm_should_adj(nbr
);
336 if (nbr
->state
== NSM_TwoWay
&& adj
== 1) {
337 next_state
= NSM_ExStart
;
339 /* Send proactive ARP requests */
340 ospf_proactively_arp(nbr
);
341 } else if (nbr
->state
>= NSM_ExStart
&& adj
== 0)
342 next_state
= NSM_TwoWay
;
347 /* Clear adjacency related state for a neighbour, intended where nbr
348 * transitions from > ExStart (i.e. a Full or forming adjacency)
351 static void nsm_clear_adj(struct ospf_neighbor
*nbr
)
353 /* Clear Database Summary list. */
354 if (!ospf_db_summary_isempty(nbr
))
355 ospf_db_summary_clear(nbr
);
357 /* Clear Link State Request list. */
358 if (!ospf_ls_request_isempty(nbr
))
359 ospf_ls_request_delete_all(nbr
);
361 /* Clear Link State Retransmission list. */
362 if (!ospf_ls_retransmit_isempty(nbr
))
363 ospf_ls_retransmit_clear(nbr
);
365 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
))
366 UNSET_FLAG(nbr
->options
, OSPF_OPTION_O
);
369 static int nsm_kill_nbr(struct ospf_neighbor
*nbr
)
371 /* killing nbr_self is invalid */
372 if (nbr
== nbr
->oi
->nbr_self
) {
373 assert(nbr
!= nbr
->oi
->nbr_self
);
377 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
&& nbr
->nbr_nbma
!= NULL
) {
378 struct ospf_nbr_nbma
*nbr_nbma
= nbr
->nbr_nbma
;
380 nbr_nbma
->nbr
= NULL
;
381 nbr_nbma
->state_change
= nbr
->state_change
;
383 nbr
->nbr_nbma
= NULL
;
385 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
388 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
390 "NSM[%s:%s]: Down (PollIntervalTimer scheduled)",
392 inet_ntoa(nbr
->address
.u
.prefix4
));
398 /* Neighbor State Machine */
400 int (*func
)(struct ospf_neighbor
*);
402 } NSM
[OSPF_NSM_STATE_MAX
][OSPF_NSM_EVENT_MAX
] = {
404 /* DependUpon: dummy state. */
405 {NULL
, NSM_DependUpon
}, /* NoEvent */
406 {NULL
, NSM_DependUpon
}, /* PacketReceived */
407 {NULL
, NSM_DependUpon
}, /* Start */
408 {NULL
, NSM_DependUpon
}, /* 2-WayReceived */
409 {NULL
, NSM_DependUpon
}, /* NegotiationDone */
410 {NULL
, NSM_DependUpon
}, /* ExchangeDone */
411 {NULL
, NSM_DependUpon
}, /* BadLSReq */
412 {NULL
, NSM_DependUpon
}, /* LoadingDone */
413 {NULL
, NSM_DependUpon
}, /* AdjOK? */
414 {NULL
, NSM_DependUpon
}, /* SeqNumberMismatch */
415 {NULL
, NSM_DependUpon
}, /* 1-WayReceived */
416 {NULL
, NSM_DependUpon
}, /* KillNbr */
417 {NULL
, NSM_DependUpon
}, /* InactivityTimer */
418 {NULL
, NSM_DependUpon
}, /* LLDown */
421 /* Deleted: dummy state. */
422 {NULL
, NSM_Deleted
}, /* NoEvent */
423 {NULL
, NSM_Deleted
}, /* PacketReceived */
424 {NULL
, NSM_Deleted
}, /* Start */
425 {NULL
, NSM_Deleted
}, /* 2-WayReceived */
426 {NULL
, NSM_Deleted
}, /* NegotiationDone */
427 {NULL
, NSM_Deleted
}, /* ExchangeDone */
428 {NULL
, NSM_Deleted
}, /* BadLSReq */
429 {NULL
, NSM_Deleted
}, /* LoadingDone */
430 {NULL
, NSM_Deleted
}, /* AdjOK? */
431 {NULL
, NSM_Deleted
}, /* SeqNumberMismatch */
432 {NULL
, NSM_Deleted
}, /* 1-WayReceived */
433 {NULL
, NSM_Deleted
}, /* KillNbr */
434 {NULL
, NSM_Deleted
}, /* InactivityTimer */
435 {NULL
, NSM_Deleted
}, /* LLDown */
439 {NULL
, NSM_DependUpon
}, /* NoEvent */
440 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
441 {nsm_start
, NSM_Attempt
}, /* Start */
442 {NULL
, NSM_Down
}, /* 2-WayReceived */
443 {NULL
, NSM_Down
}, /* NegotiationDone */
444 {NULL
, NSM_Down
}, /* ExchangeDone */
445 {NULL
, NSM_Down
}, /* BadLSReq */
446 {NULL
, NSM_Down
}, /* LoadingDone */
447 {NULL
, NSM_Down
}, /* AdjOK? */
448 {NULL
, NSM_Down
}, /* SeqNumberMismatch */
449 {NULL
, NSM_Down
}, /* 1-WayReceived */
450 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
451 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
452 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
456 {NULL
, NSM_DependUpon
}, /* NoEvent */
457 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
458 {NULL
, NSM_Attempt
}, /* Start */
459 {NULL
, NSM_Attempt
}, /* 2-WayReceived */
460 {NULL
, NSM_Attempt
}, /* NegotiationDone */
461 {NULL
, NSM_Attempt
}, /* ExchangeDone */
462 {NULL
, NSM_Attempt
}, /* BadLSReq */
463 {NULL
, NSM_Attempt
}, /* LoadingDone */
464 {NULL
, NSM_Attempt
}, /* AdjOK? */
465 {NULL
, NSM_Attempt
}, /* SeqNumberMismatch */
466 {NULL
, NSM_Attempt
}, /* 1-WayReceived */
467 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
468 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
469 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
473 {NULL
, NSM_DependUpon
}, /* NoEvent */
474 {nsm_packet_received
, NSM_Init
}, /* PacketReceived */
475 {NULL
, NSM_Init
}, /* Start */
476 {nsm_twoway_received
, NSM_DependUpon
}, /* 2-WayReceived */
477 {NULL
, NSM_Init
}, /* NegotiationDone */
478 {NULL
, NSM_Init
}, /* ExchangeDone */
479 {NULL
, NSM_Init
}, /* BadLSReq */
480 {NULL
, NSM_Init
}, /* LoadingDone */
481 {NULL
, NSM_Init
}, /* AdjOK? */
482 {NULL
, NSM_Init
}, /* SeqNumberMismatch */
483 {NULL
, NSM_Init
}, /* 1-WayReceived */
484 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
485 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
486 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
490 {NULL
, NSM_DependUpon
}, /* NoEvent */
491 {nsm_packet_received
, NSM_TwoWay
}, /* HelloReceived */
492 {NULL
, NSM_TwoWay
}, /* Start */
493 {NULL
, NSM_TwoWay
}, /* 2-WayReceived */
494 {NULL
, NSM_TwoWay
}, /* NegotiationDone */
495 {NULL
, NSM_TwoWay
}, /* ExchangeDone */
496 {NULL
, NSM_TwoWay
}, /* BadLSReq */
497 {NULL
, NSM_TwoWay
}, /* LoadingDone */
498 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
499 {NULL
, NSM_TwoWay
}, /* SeqNumberMismatch */
500 {NULL
, NSM_Init
}, /* 1-WayReceived */
501 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
502 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
503 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
507 {NULL
, NSM_DependUpon
}, /* NoEvent */
508 {nsm_packet_received
, NSM_ExStart
}, /* PacaketReceived */
509 {NULL
, NSM_ExStart
}, /* Start */
510 {NULL
, NSM_ExStart
}, /* 2-WayReceived */
511 {nsm_negotiation_done
, NSM_Exchange
}, /* NegotiationDone */
512 {NULL
, NSM_ExStart
}, /* ExchangeDone */
513 {NULL
, NSM_ExStart
}, /* BadLSReq */
514 {NULL
, NSM_ExStart
}, /* LoadingDone */
515 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
516 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
517 {NULL
, NSM_Init
}, /* 1-WayReceived */
518 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
519 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
520 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
524 {NULL
, NSM_DependUpon
}, /* NoEvent */
525 {nsm_packet_received
, NSM_Exchange
}, /* PacketReceived */
526 {NULL
, NSM_Exchange
}, /* Start */
527 {NULL
, NSM_Exchange
}, /* 2-WayReceived */
528 {NULL
, NSM_Exchange
}, /* NegotiationDone */
529 {nsm_exchange_done
, NSM_DependUpon
}, /* ExchangeDone */
530 {NULL
, NSM_ExStart
}, /* BadLSReq */
531 {NULL
, NSM_Exchange
}, /* LoadingDone */
532 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
533 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
534 {NULL
, NSM_Init
}, /* 1-WayReceived */
535 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
536 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
537 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
541 {NULL
, NSM_DependUpon
}, /* NoEvent */
542 {nsm_packet_received
, NSM_Loading
}, /* PacketReceived */
543 {NULL
, NSM_Loading
}, /* Start */
544 {NULL
, NSM_Loading
}, /* 2-WayReceived */
545 {NULL
, NSM_Loading
}, /* NegotiationDone */
546 {NULL
, NSM_Loading
}, /* ExchangeDone */
547 {NULL
, NSM_ExStart
}, /* BadLSReq */
548 {NULL
, NSM_Full
}, /* LoadingDone */
549 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
550 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
551 {NULL
, NSM_Init
}, /* 1-WayReceived */
552 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
553 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
554 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
558 {NULL
, NSM_DependUpon
}, /* NoEvent */
559 {nsm_packet_received
, NSM_Full
}, /* PacketReceived */
560 {NULL
, NSM_Full
}, /* Start */
561 {NULL
, NSM_Full
}, /* 2-WayReceived */
562 {NULL
, NSM_Full
}, /* NegotiationDone */
563 {NULL
, NSM_Full
}, /* ExchangeDone */
564 {NULL
, NSM_ExStart
}, /* BadLSReq */
565 {NULL
, NSM_Full
}, /* LoadingDone */
566 {nsm_adj_ok
, NSM_DependUpon
}, /* AdjOK? */
567 {NULL
, NSM_ExStart
}, /* SeqNumberMismatch */
568 {NULL
, NSM_Init
}, /* 1-WayReceived */
569 {nsm_kill_nbr
, NSM_Deleted
}, /* KillNbr */
570 {nsm_kill_nbr
, NSM_Deleted
}, /* InactivityTimer */
571 {nsm_kill_nbr
, NSM_Deleted
}, /* LLDown */
575 static const char *ospf_nsm_event_str
[] = {
576 "NoEvent", "PacketReceived", "Start",
577 "2-WayReceived", "NegotiationDone", "ExchangeDone",
578 "BadLSReq", "LoadingDone", "AdjOK?",
579 "SeqNumberMismatch", "1-WayReceived", "KillNbr",
580 "InactivityTimer", "LLDown",
583 static void nsm_notice_state_change(struct ospf_neighbor
*nbr
, int next_state
,
586 /* Logging change of status. */
587 if (IS_DEBUG_OSPF(nsm
, NSM_STATUS
))
588 zlog_debug("NSM[%s:%s]: State change %s -> %s (%s)",
589 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
),
590 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
591 lookup_msg(ospf_nsm_state_msg
, next_state
, NULL
),
592 ospf_nsm_event_str
[event
]);
594 /* Optionally notify about adjacency changes */
595 if (CHECK_FLAG(nbr
->oi
->ospf
->config
, OSPF_LOG_ADJACENCY_CHANGES
)
596 && (CHECK_FLAG(nbr
->oi
->ospf
->config
, OSPF_LOG_ADJACENCY_DETAIL
)
597 || (next_state
== NSM_Full
) || (next_state
< nbr
->state
)))
598 zlog_notice("AdjChg: Nbr %s on %s: %s -> %s (%s)",
599 inet_ntoa(nbr
->router_id
), IF_NAME(nbr
->oi
),
600 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
601 lookup_msg(ospf_nsm_state_msg
, next_state
, NULL
),
602 ospf_nsm_event_str
[event
]);
605 if (next_state
> nbr
->state
)
606 monotime(&nbr
->ts_last_progress
);
607 else /* regression in NSM */
609 monotime(&nbr
->ts_last_regress
);
610 nbr
->last_regress_str
= ospf_nsm_event_str
[event
];
614 static void nsm_change_state(struct ospf_neighbor
*nbr
, int state
)
616 struct ospf_interface
*oi
= nbr
->oi
;
617 struct ospf_area
*vl_area
= NULL
;
620 /* Preserve old status. */
621 old_state
= nbr
->state
;
623 /* Change to new status. */
629 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
630 vl_area
= ospf_area_lookup_by_area_id(oi
->ospf
,
631 oi
->vl_data
->vl_area_id
);
633 /* Generate NeighborChange ISM event.
635 * In response to NeighborChange, DR election is rerun. The information
636 * from the election process is required by the router-lsa construction.
638 * Therefore, trigger the event prior to refreshing the LSAs. */
643 if ((old_state
< NSM_TwoWay
&& state
>= NSM_TwoWay
)
644 || (old_state
>= NSM_TwoWay
&& state
< NSM_TwoWay
))
645 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_NeighborChange
);
648 /* ISM_PointToPoint -> ISM_Down, ISM_Loopback -> ISM_Down, etc.
653 /* One of the neighboring routers changes to/from the FULL state. */
654 if ((old_state
!= NSM_Full
&& state
== NSM_Full
)
655 || (old_state
== NSM_Full
&& state
!= NSM_Full
)) {
656 if (state
== NSM_Full
) {
658 oi
->area
->full_nbrs
++;
660 ospf_check_abr_status(oi
->ospf
);
662 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
&& vl_area
)
663 if (++vl_area
->full_vls
== 1)
664 ospf_schedule_abr_task(oi
->ospf
);
667 oi
->area
->full_nbrs
--;
669 ospf_check_abr_status(oi
->ospf
);
671 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
&& vl_area
)
672 if (vl_area
->full_vls
> 0)
673 if (--vl_area
->full_vls
== 0)
674 ospf_schedule_abr_task(
678 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_LOG_ADJACENCY_DETAIL
))
680 "%s:(%s, %s -> %s): "
681 "scheduling new router-LSA origination",
682 __PRETTY_FUNCTION__
, inet_ntoa(nbr
->router_id
),
683 lookup_msg(ospf_nsm_state_msg
, old_state
, NULL
),
684 lookup_msg(ospf_nsm_state_msg
, state
, NULL
));
686 ospf_router_lsa_update_area(oi
->area
);
688 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
) {
689 vl_area
= ospf_area_lookup_by_area_id(
690 oi
->ospf
, oi
->vl_data
->vl_area_id
);
693 ospf_router_lsa_update_area(vl_area
);
696 /* Originate network-LSA. */
697 if (oi
->state
== ISM_DR
) {
698 if (oi
->network_lsa_self
&& oi
->full_nbrs
== 0) {
699 ospf_lsa_flush_area(oi
->network_lsa_self
,
701 ospf_lsa_unlock(&oi
->network_lsa_self
);
702 oi
->network_lsa_self
= NULL
;
704 ospf_network_lsa_update(oi
);
708 ospf_opaque_nsm_change(nbr
, old_state
);
710 /* State changes from > ExStart to <= ExStart should clear any Exchange
711 * or Full/LSA Update related lists and state.
712 * Potential causal events: BadLSReq, SeqNumberMismatch, AdjOK?
714 if ((old_state
> NSM_ExStart
) && (state
<= NSM_ExStart
))
717 /* Start DD exchange protocol */
718 if (state
== NSM_ExStart
) {
719 if (nbr
->dd_seqnum
== 0)
720 nbr
->dd_seqnum
= (uint32_t)random();
725 OSPF_DD_FLAG_I
| OSPF_DD_FLAG_M
| OSPF_DD_FLAG_MS
;
726 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_LOG_ADJACENCY_DETAIL
))
728 "%s: Intializing [DD]: %s with seqnum:%x , flags:%x",
729 (oi
->ospf
->name
) ? oi
->ospf
->name
731 inet_ntoa(nbr
->router_id
), nbr
->dd_seqnum
,
733 ospf_db_desc_send(nbr
);
736 /* clear cryptographic sequence number */
737 if (state
== NSM_Down
)
738 nbr
->crypt_seqnum
= 0;
740 ospf_bfd_trigger_event(nbr
, old_state
, state
);
742 /* Preserve old status? */
745 /* Execute NSM event process. */
746 int ospf_nsm_event(struct thread
*thread
)
750 struct ospf_neighbor
*nbr
;
752 nbr
= THREAD_ARG(thread
);
753 event
= THREAD_VAL(thread
);
755 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
756 zlog_debug("NSM[%s:%s]: %s (%s)", IF_NAME(nbr
->oi
),
757 inet_ntoa(nbr
->router_id
),
758 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
),
759 ospf_nsm_event_str
[event
]);
761 next_state
= NSM
[nbr
->state
][event
].next_state
;
764 if (NSM
[nbr
->state
][event
].func
!= NULL
) {
765 int func_state
= (*(NSM
[nbr
->state
][event
].func
))(nbr
);
767 if (NSM
[nbr
->state
][event
].next_state
== NSM_DependUpon
)
768 next_state
= func_state
;
769 else if (func_state
) {
770 /* There's a mismatch between the FSM tables and what an
772 * action/state-change function returned. State changes
774 * do not have conditional/DependUpon next-states should
776 * try set next_state.
779 EC_OSPF_FSM_INVALID_STATE
,
780 "NSM[%s:%s]: %s (%s): "
781 "Warning: action tried to change next_state to %s",
782 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
),
783 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
785 ospf_nsm_event_str
[event
],
786 lookup_msg(ospf_nsm_state_msg
, func_state
,
791 assert(next_state
!= NSM_DependUpon
);
793 /* If state is changed. */
794 if (next_state
!= nbr
->state
) {
795 int old_state
= nbr
->state
;
797 nsm_notice_state_change(nbr
, next_state
, event
);
798 nsm_change_state(nbr
, next_state
);
800 hook_call(ospf_nsm_change
, nbr
, next_state
, old_state
);
803 /* Make sure timer is set. */
806 /* When event is NSM_KillNbr, InactivityTimer or LLDown, the neighbor
809 * Rather than encode knowledge here of which events lead to NBR
810 * delete, we take our cue from the NSM table, via the dummy
811 * 'Deleted' neighbour state.
813 if (nbr
->state
== NSM_Deleted
)
814 ospf_nbr_delete(nbr
);
819 /* Check loading state. */
820 void ospf_check_nbr_loading(struct ospf_neighbor
*nbr
)
822 if (nbr
->state
== NSM_Loading
) {
823 if (ospf_ls_request_isempty(nbr
))
824 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_LoadingDone
);
825 else if (nbr
->ls_req_last
== NULL
)
826 ospf_ls_req_event(nbr
);