2 * OSPF version 2 Interface 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
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
33 #include "ospfd/ospfd.h"
34 #include "ospfd/ospf_interface.h"
35 #include "ospfd/ospf_ism.h"
36 #include "ospfd/ospf_asbr.h"
37 #include "ospfd/ospf_lsa.h"
38 #include "ospfd/ospf_lsdb.h"
39 #include "ospfd/ospf_neighbor.h"
40 #include "ospfd/ospf_nsm.h"
41 #include "ospfd/ospf_network.h"
42 #include "ospfd/ospf_dump.h"
43 #include "ospfd/ospf_packet.h"
44 #include "ospfd/ospf_flood.h"
45 #include "ospfd/ospf_abr.h"
47 /* elect DR and BDR. Refer to RFC2319 section 9.4 */
48 struct ospf_neighbor
*
49 ospf_dr_election_sub (struct list
*routers
)
51 struct listnode
*node
;
52 struct ospf_neighbor
*nbr
, *max
= NULL
;
54 /* Choose highest router priority.
55 In case of tie, choose highest Router ID. */
56 for (ALL_LIST_ELEMENTS_RO (routers
, node
, nbr
))
62 if (max
->priority
< nbr
->priority
)
64 else if (max
->priority
== nbr
->priority
)
65 if (IPV4_ADDR_CMP (&max
->router_id
, &nbr
->router_id
) < 0)
73 struct ospf_neighbor
*
74 ospf_elect_dr (struct ospf_interface
*oi
, struct list
*el_list
)
77 struct listnode
*node
;
78 struct ospf_neighbor
*nbr
, *dr
= NULL
, *bdr
= NULL
;
80 dr_list
= list_new ();
82 /* Add neighbors to the list. */
83 for (ALL_LIST_ELEMENTS_RO (el_list
, node
, nbr
))
85 /* neighbor declared to be DR. */
87 listnode_add (dr_list
, nbr
);
89 /* Preserve neighbor BDR. */
90 if (IPV4_ADDR_SAME (&BDR (oi
), &nbr
->address
.u
.prefix4
))
94 /* Elect Designated Router. */
95 if (listcount (dr_list
) > 0)
96 dr
= ospf_dr_election_sub (dr_list
);
100 /* Set DR to interface. */
103 DR (oi
) = dr
->address
.u
.prefix4
;
104 dr
->d_router
= dr
->address
.u
.prefix4
;
109 list_delete (dr_list
);
114 struct ospf_neighbor
*
115 ospf_elect_bdr (struct ospf_interface
*oi
, struct list
*el_list
)
117 struct list
*bdr_list
, *no_dr_list
;
118 struct listnode
*node
;
119 struct ospf_neighbor
*nbr
, *bdr
= NULL
;
121 bdr_list
= list_new ();
122 no_dr_list
= list_new ();
124 /* Add neighbors to the list. */
125 for (ALL_LIST_ELEMENTS_RO (el_list
, node
, nbr
))
127 /* neighbor declared to be DR. */
131 /* neighbor declared to be BDR. */
132 if (NBR_IS_BDR (nbr
))
133 listnode_add (bdr_list
, nbr
);
135 listnode_add (no_dr_list
, nbr
);
138 /* Elect Backup Designated Router. */
139 if (listcount (bdr_list
) > 0)
140 bdr
= ospf_dr_election_sub (bdr_list
);
142 bdr
= ospf_dr_election_sub (no_dr_list
);
144 /* Set BDR to interface. */
147 BDR (oi
) = bdr
->address
.u
.prefix4
;
148 bdr
->bd_router
= bdr
->address
.u
.prefix4
;
153 list_delete (bdr_list
);
154 list_delete (no_dr_list
);
160 ospf_ism_state (struct ospf_interface
*oi
)
162 if (IPV4_ADDR_SAME (&DR (oi
), &oi
->address
->u
.prefix4
))
164 else if (IPV4_ADDR_SAME (&BDR (oi
), &oi
->address
->u
.prefix4
))
171 ospf_dr_eligible_routers (struct route_table
*nbrs
, struct list
*el_list
)
173 struct route_node
*rn
;
174 struct ospf_neighbor
*nbr
;
176 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
177 if ((nbr
= rn
->info
) != NULL
)
178 /* Ignore 0.0.0.0 node*/
179 if (nbr
->router_id
.s_addr
!= 0)
180 /* Is neighbor eligible? */
181 if (nbr
->priority
!= 0)
182 /* Is neighbor upper 2-Way? */
183 if (nbr
->state
>= NSM_TwoWay
)
184 listnode_add (el_list
, nbr
);
187 /* Generate AdjOK? NSM event. */
189 ospf_dr_change (struct ospf
*ospf
, struct route_table
*nbrs
)
191 struct route_node
*rn
;
192 struct ospf_neighbor
*nbr
;
194 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
195 if ((nbr
= rn
->info
) != NULL
)
196 /* Ignore 0.0.0.0 node*/
197 if (nbr
->router_id
.s_addr
!= 0)
198 /* Is neighbor upper 2-Way? */
199 if (nbr
->state
>= NSM_TwoWay
)
201 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &ospf
->router_id
))
202 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_AdjOK
);
206 ospf_dr_election (struct ospf_interface
*oi
)
208 struct in_addr old_dr
, old_bdr
;
209 int old_state
, new_state
;
210 struct list
*el_list
;
211 struct ospf_neighbor
*dr
, *bdr
;
213 /* backup current values. */
216 old_state
= oi
->state
;
218 el_list
= list_new ();
220 /* List eligible routers. */
221 ospf_dr_eligible_routers (oi
->nbrs
, el_list
);
223 /* First election of DR and BDR. */
224 bdr
= ospf_elect_bdr (oi
, el_list
);
225 dr
= ospf_elect_dr (oi
, el_list
);
227 new_state
= ospf_ism_state (oi
);
229 zlog_info ("DR-Election[1st]: Backup %s", inet_ntoa (BDR (oi
)));
230 zlog_info ("DR-Election[1st]: DR %s", inet_ntoa (DR (oi
)));
232 if (new_state
!= old_state
&&
233 !(new_state
== ISM_DROther
&& old_state
< ISM_DROther
))
235 ospf_elect_bdr (oi
, el_list
);
236 ospf_elect_dr (oi
, el_list
);
238 new_state
= ospf_ism_state (oi
);
240 zlog_info ("DR-Election[2nd]: Backup %s", inet_ntoa (BDR (oi
)));
241 zlog_info ("DR-Election[2nd]: DR %s", inet_ntoa (DR (oi
)));
244 list_delete (el_list
);
246 /* if DR or BDR changes, cause AdjOK? neighbor event. */
247 if (!IPV4_ADDR_SAME (&old_dr
, &DR (oi
)) ||
248 !IPV4_ADDR_SAME (&old_bdr
, &BDR (oi
)))
249 ospf_dr_change (oi
->ospf
, oi
->nbrs
);
256 ospf_hello_timer (struct thread
*thread
)
258 struct ospf_interface
*oi
;
260 oi
= THREAD_ARG (thread
);
263 if (IS_DEBUG_OSPF (ism
, ISM_TIMERS
))
264 zlog (NULL
, LOG_DEBUG
, "ISM[%s]: Timer (Hello timer expire)",
267 /* Sending hello packet. */
268 ospf_hello_send (oi
);
270 /* Hello timer set. */
271 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
,
272 OSPF_IF_PARAM (oi
, v_hello
));
278 ospf_wait_timer (struct thread
*thread
)
280 struct ospf_interface
*oi
;
282 oi
= THREAD_ARG (thread
);
285 if (IS_DEBUG_OSPF (ism
, ISM_TIMERS
))
286 zlog (NULL
, LOG_DEBUG
, "ISM[%s]: Timer (Wait timer expire)",
289 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_WaitTimer
);
294 /* Hook function called after ospf ISM event is occured. And vty's
295 network command invoke this function after making interface
298 ism_timer_set (struct ospf_interface
*oi
)
303 /* First entry point of ospf interface state machine. In this state
304 interface parameters must be set to initial values, and timers are
306 OSPF_ISM_TIMER_OFF (oi
->t_hello
);
307 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
308 OSPF_ISM_TIMER_OFF (oi
->t_ls_ack
);
311 /* In this state, the interface may be looped back and will be
312 unavailable for regular data traffic. */
313 OSPF_ISM_TIMER_OFF (oi
->t_hello
);
314 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
315 OSPF_ISM_TIMER_OFF (oi
->t_ls_ack
);
318 /* The router is trying to determine the identity of DRouter and
319 BDRouter. The router begin to receive and send Hello Packets. */
320 /* send first hello immediately */
321 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
, 1);
322 OSPF_ISM_TIMER_ON (oi
->t_wait
, ospf_wait_timer
,
323 OSPF_IF_PARAM (oi
, v_wait
));
324 OSPF_ISM_TIMER_OFF (oi
->t_ls_ack
);
326 case ISM_PointToPoint
:
327 /* The interface connects to a physical Point-to-point network or
328 virtual link. The router attempts to form an adjacency with
329 neighboring router. Hello packets are also sent. */
330 /* send first hello immediately */
331 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
, 1);
333 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
334 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
337 /* The network type of the interface is broadcast or NBMA network,
338 and the router itself is neither Designated Router nor
339 Backup Designated Router. */
340 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
,
341 OSPF_IF_PARAM (oi
, v_hello
));
342 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
343 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
346 /* The network type of the interface is broadcast os NBMA network,
347 and the router is Backup Designated Router. */
348 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
,
349 OSPF_IF_PARAM (oi
, v_hello
));
350 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
351 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
354 /* The network type of the interface is broadcast or NBMA network,
355 and the router is Designated Router. */
356 OSPF_ISM_TIMER_ON (oi
->t_hello
, ospf_hello_timer
,
357 OSPF_IF_PARAM (oi
, v_hello
));
358 OSPF_ISM_TIMER_OFF (oi
->t_wait
);
359 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
365 ism_stop (struct ospf_interface
*oi
)
371 ism_interface_up (struct ospf_interface
*oi
)
375 /* if network type is point-to-point, Point-to-MultiPoint or virtual link,
376 the state transitions to Point-to-Point. */
377 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
||
378 oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
||
379 oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
380 next_state
= ISM_PointToPoint
;
381 /* Else if the router is not eligible to DR, the state transitions to
383 else if (PRIORITY (oi
) == 0) /* router is eligible? */
384 next_state
= ISM_DROther
;
386 /* Otherwise, the state transitions to Waiting. */
387 next_state
= ISM_Waiting
;
389 if (oi
->type
== OSPF_IFTYPE_NBMA
)
390 ospf_nbr_nbma_if_update (oi
->ospf
, oi
);
392 /* ospf_ism_event (t); */
397 ism_loop_ind (struct ospf_interface
*oi
)
401 /* call ism_interface_down. */
402 /* ret = ism_interface_down (oi); */
407 /* Interface down event handler. */
409 ism_interface_down (struct ospf_interface
*oi
)
411 ospf_if_cleanup (oi
);
417 ism_backup_seen (struct ospf_interface
*oi
)
419 return ospf_dr_election (oi
);
423 ism_wait_timer (struct ospf_interface
*oi
)
425 return ospf_dr_election (oi
);
429 ism_neighbor_change (struct ospf_interface
*oi
)
431 return ospf_dr_election (oi
);
435 ism_ignore (struct ospf_interface
*oi
)
437 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
438 zlog (NULL
, LOG_DEBUG
, "ISM[%s]: ism_ignore called", IF_NAME (oi
));
443 /* Interface State Machine */
447 } ISM
[OSPF_ISM_STATE_MAX
][OSPF_ISM_EVENT_MAX
] =
450 /* DependUpon: dummy state. */
451 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
452 { ism_ignore
, ISM_DependUpon
}, /* InterfaceUp */
453 { ism_ignore
, ISM_DependUpon
}, /* WaitTimer */
454 { ism_ignore
, ISM_DependUpon
}, /* BackupSeen */
455 { ism_ignore
, ISM_DependUpon
}, /* NeighborChange */
456 { ism_ignore
, ISM_DependUpon
}, /* LoopInd */
457 { ism_ignore
, ISM_DependUpon
}, /* UnloopInd */
458 { ism_ignore
, ISM_DependUpon
}, /* InterfaceDown */
462 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
463 { ism_interface_up
, ISM_DependUpon
}, /* InterfaceUp */
464 { ism_ignore
, ISM_Down
}, /* WaitTimer */
465 { ism_ignore
, ISM_Down
}, /* BackupSeen */
466 { ism_ignore
, ISM_Down
}, /* NeighborChange */
467 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
468 { ism_ignore
, ISM_Down
}, /* UnloopInd */
469 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
473 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
474 { ism_ignore
, ISM_Loopback
}, /* InterfaceUp */
475 { ism_ignore
, ISM_Loopback
}, /* WaitTimer */
476 { ism_ignore
, ISM_Loopback
}, /* BackupSeen */
477 { ism_ignore
, ISM_Loopback
}, /* NeighborChange */
478 { ism_ignore
, ISM_Loopback
}, /* LoopInd */
479 { ism_ignore
, ISM_Down
}, /* UnloopInd */
480 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
484 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
485 { ism_ignore
, ISM_Waiting
}, /* InterfaceUp */
486 { ism_wait_timer
, ISM_DependUpon
}, /* WaitTimer */
487 { ism_backup_seen
, ISM_DependUpon
}, /* BackupSeen */
488 { ism_ignore
, ISM_Waiting
}, /* NeighborChange */
489 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
490 { ism_ignore
, ISM_Waiting
}, /* UnloopInd */
491 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
494 /* Point-to-Point: */
495 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
496 { ism_ignore
, ISM_PointToPoint
}, /* InterfaceUp */
497 { ism_ignore
, ISM_PointToPoint
}, /* WaitTimer */
498 { ism_ignore
, ISM_PointToPoint
}, /* BackupSeen */
499 { ism_ignore
, ISM_PointToPoint
}, /* NeighborChange */
500 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
501 { ism_ignore
, ISM_PointToPoint
}, /* UnloopInd */
502 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
506 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
507 { ism_ignore
, ISM_DROther
}, /* InterfaceUp */
508 { ism_ignore
, ISM_DROther
}, /* WaitTimer */
509 { ism_ignore
, ISM_DROther
}, /* BackupSeen */
510 { ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
511 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
512 { ism_ignore
, ISM_DROther
}, /* UnloopInd */
513 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
517 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
518 { ism_ignore
, ISM_Backup
}, /* InterfaceUp */
519 { ism_ignore
, ISM_Backup
}, /* WaitTimer */
520 { ism_ignore
, ISM_Backup
}, /* BackupSeen */
521 { ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
522 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
523 { ism_ignore
, ISM_Backup
}, /* UnloopInd */
524 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
528 { ism_ignore
, ISM_DependUpon
}, /* NoEvent */
529 { ism_ignore
, ISM_DR
}, /* InterfaceUp */
530 { ism_ignore
, ISM_DR
}, /* WaitTimer */
531 { ism_ignore
, ISM_DR
}, /* BackupSeen */
532 { ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
533 { ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
534 { ism_ignore
, ISM_DR
}, /* UnloopInd */
535 { ism_interface_down
, ISM_Down
}, /* InterfaceDown */
539 const static char *ospf_ism_event_str
[] =
552 ism_change_state (struct ospf_interface
*oi
, int state
)
555 struct ospf_lsa
*lsa
;
557 /* Logging change of state. */
558 if (IS_DEBUG_OSPF (ism
, ISM_STATUS
))
559 zlog (NULL
, LOG_DEBUG
, "ISM[%s]: State change %s -> %s", IF_NAME (oi
),
560 LOOKUP (ospf_ism_state_msg
, oi
->state
),
561 LOOKUP (ospf_ism_state_msg
, state
));
563 old_state
= oi
->state
;
567 /* Set multicast memberships appropriately for new state. */
568 ospf_if_set_multicast(oi
);
570 if (old_state
== ISM_Down
|| state
== ISM_Down
)
571 ospf_check_abr_status (oi
->ospf
);
573 /* Originate router-LSA. */
576 if (state
== ISM_Down
)
578 if (oi
->area
->act_ints
> 0)
579 oi
->area
->act_ints
--;
581 else if (old_state
== ISM_Down
)
582 oi
->area
->act_ints
++;
584 /* schedule router-LSA originate. */
585 ospf_router_lsa_timer_add (oi
->area
);
588 /* Originate network-LSA. */
589 if (old_state
!= ISM_DR
&& state
== ISM_DR
)
590 ospf_network_lsa_timer_add (oi
);
591 else if (old_state
== ISM_DR
&& state
!= ISM_DR
)
593 /* Free self originated network LSA. */
594 lsa
= oi
->network_lsa_self
;
597 ospf_lsa_flush_area (lsa
, oi
->area
);
598 OSPF_TIMER_OFF (oi
->t_network_lsa_self
);
601 ospf_lsa_unlock (oi
->network_lsa_self
);
602 oi
->network_lsa_self
= NULL
;
605 #ifdef HAVE_OPAQUE_LSA
606 ospf_opaque_ism_change (oi
, old_state
);
607 #endif /* HAVE_OPAQUE_LSA */
609 /* Check area border status. */
610 ospf_check_abr_status (oi
->ospf
);
613 /* Execute ISM event process. */
615 ospf_ism_event (struct thread
*thread
)
619 struct ospf_interface
*oi
;
621 oi
= THREAD_ARG (thread
);
622 event
= THREAD_VAL (thread
);
625 next_state
= (*(ISM
[oi
->state
][event
].func
))(oi
);
628 next_state
= ISM
[oi
->state
][event
].next_state
;
630 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
631 zlog (NULL
, LOG_DEBUG
, "ISM[%s]: %s (%s)", IF_NAME (oi
),
632 LOOKUP (ospf_ism_state_msg
, oi
->state
),
633 ospf_ism_event_str
[event
]);
635 /* If state is changed. */
636 if (next_state
!= oi
->state
)
637 ism_change_state (oi
, next_state
);
639 /* Make sure timer is set. */