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 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
32 #include "ospfd/ospfd.h"
33 #include "ospfd/ospf_interface.h"
34 #include "ospfd/ospf_ism.h"
35 #include "ospfd/ospf_asbr.h"
36 #include "ospfd/ospf_lsa.h"
37 #include "ospfd/ospf_lsdb.h"
38 #include "ospfd/ospf_neighbor.h"
39 #include "ospfd/ospf_nsm.h"
40 #include "ospfd/ospf_network.h"
41 #include "ospfd/ospf_dump.h"
42 #include "ospfd/ospf_packet.h"
43 #include "ospfd/ospf_flood.h"
44 #include "ospfd/ospf_abr.h"
46 DEFINE_HOOK(ospf_ism_change
,
47 (struct ospf_interface
* oi
, int state
, int oldstate
),
48 (oi
, state
, oldstate
))
50 /* elect DR and BDR. Refer to RFC2319 section 9.4 */
51 static struct ospf_neighbor
*ospf_dr_election_sub(struct list
*routers
)
53 struct listnode
*node
;
54 struct ospf_neighbor
*nbr
, *max
= NULL
;
56 /* Choose highest router priority.
57 In case of tie, choose highest Router ID. */
58 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
,
75 static struct ospf_neighbor
*ospf_elect_dr(struct ospf_interface
*oi
,
79 struct listnode
*node
;
80 struct ospf_neighbor
*nbr
, *dr
= NULL
, *bdr
= NULL
;
84 /* Add neighbors to the list. */
85 for (ALL_LIST_ELEMENTS_RO(el_list
, node
, nbr
)) {
86 /* neighbor declared to be DR. */
88 listnode_add(dr_list
, nbr
);
90 /* Preserve neighbor BDR. */
91 if (IPV4_ADDR_SAME(&BDR(oi
), &nbr
->address
.u
.prefix4
))
95 /* Elect Designated Router. */
96 if (listcount(dr_list
) > 0)
97 dr
= ospf_dr_election_sub(dr_list
);
101 /* Set DR to interface. */
103 DR(oi
) = dr
->address
.u
.prefix4
;
107 list_delete(&dr_list
);
112 static struct ospf_neighbor
*ospf_elect_bdr(struct ospf_interface
*oi
,
113 struct list
*el_list
)
115 struct list
*bdr_list
, *no_dr_list
;
116 struct listnode
*node
;
117 struct ospf_neighbor
*nbr
, *bdr
= NULL
;
119 bdr_list
= list_new();
120 no_dr_list
= list_new();
122 /* Add neighbors to the list. */
123 for (ALL_LIST_ELEMENTS_RO(el_list
, node
, nbr
)) {
124 /* neighbor declared to be DR. */
128 /* neighbor declared to be BDR. */
130 listnode_add(bdr_list
, nbr
);
132 listnode_add(no_dr_list
, nbr
);
135 /* Elect Backup Designated Router. */
136 if (listcount(bdr_list
) > 0)
137 bdr
= ospf_dr_election_sub(bdr_list
);
139 bdr
= ospf_dr_election_sub(no_dr_list
);
141 /* Set BDR to interface. */
143 BDR(oi
) = bdr
->address
.u
.prefix4
;
147 list_delete(&bdr_list
);
148 list_delete(&no_dr_list
);
153 static int ospf_ism_state(struct ospf_interface
*oi
)
155 if (IPV4_ADDR_SAME(&DR(oi
), &oi
->address
->u
.prefix4
))
157 else if (IPV4_ADDR_SAME(&BDR(oi
), &oi
->address
->u
.prefix4
))
163 static void ospf_dr_eligible_routers(struct route_table
*nbrs
,
164 struct list
*el_list
)
166 struct route_node
*rn
;
167 struct ospf_neighbor
*nbr
;
169 for (rn
= route_top(nbrs
); rn
; rn
= route_next(rn
))
170 if ((nbr
= rn
->info
) != NULL
)
171 /* Ignore 0.0.0.0 node*/
172 if (nbr
->router_id
.s_addr
!= INADDR_ANY
)
173 /* Is neighbor eligible? */
174 if (nbr
->priority
> 0)
175 /* Is neighbor upper 2-Way? */
176 if (nbr
->state
>= NSM_TwoWay
)
177 listnode_add(el_list
, nbr
);
180 /* Generate AdjOK? NSM event. */
181 static void ospf_dr_change(struct ospf
*ospf
, struct route_table
*nbrs
)
183 struct route_node
*rn
;
184 struct ospf_neighbor
*nbr
;
186 for (rn
= route_top(nbrs
); rn
; rn
= route_next(rn
)) {
193 * Ignore 0.0.0.0 node
197 if (nbr
->router_id
.s_addr
!= INADDR_ANY
198 && nbr
->state
>= NSM_TwoWay
199 && !IPV4_ADDR_SAME(&nbr
->router_id
, &ospf
->router_id
))
200 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_AdjOK
);
204 int ospf_dr_election(struct ospf_interface
*oi
)
206 struct in_addr old_dr
, old_bdr
;
207 int old_state
, new_state
;
208 struct list
*el_list
;
210 /* backup current values. */
213 old_state
= oi
->state
;
215 el_list
= list_new();
217 /* List eligible routers. */
218 ospf_dr_eligible_routers(oi
->nbrs
, el_list
);
220 /* First election of DR and BDR. */
221 ospf_elect_bdr(oi
, el_list
);
222 ospf_elect_dr(oi
, el_list
);
224 new_state
= ospf_ism_state(oi
);
226 zlog_debug("DR-Election[1st]: Backup %pI4", &BDR(oi
));
227 zlog_debug("DR-Election[1st]: DR %pI4", &DR(oi
));
229 if (new_state
!= old_state
230 && !(new_state
== ISM_DROther
&& old_state
< ISM_DROther
)) {
231 ospf_elect_bdr(oi
, el_list
);
232 ospf_elect_dr(oi
, el_list
);
234 new_state
= ospf_ism_state(oi
);
236 zlog_debug("DR-Election[2nd]: Backup %pI4", &BDR(oi
));
237 zlog_debug("DR-Election[2nd]: DR %pI4", &DR(oi
));
240 list_delete(&el_list
);
242 /* if DR or BDR changes, cause AdjOK? neighbor event. */
243 if (!IPV4_ADDR_SAME(&old_dr
, &DR(oi
))
244 || !IPV4_ADDR_SAME(&old_bdr
, &BDR(oi
)))
245 ospf_dr_change(oi
->ospf
, oi
->nbrs
);
251 int ospf_hello_timer(struct thread
*thread
)
253 struct ospf_interface
*oi
;
255 oi
= THREAD_ARG(thread
);
258 if (IS_DEBUG_OSPF(ism
, ISM_TIMERS
))
259 zlog_debug("ISM[%s]: Timer (Hello timer expire)", IF_NAME(oi
));
261 /* Sending hello packet. */
264 /* Hello timer set. */
265 OSPF_HELLO_TIMER_ON(oi
);
270 static int ospf_wait_timer(struct thread
*thread
)
272 struct ospf_interface
*oi
;
274 oi
= THREAD_ARG(thread
);
277 if (IS_DEBUG_OSPF(ism
, ISM_TIMERS
))
278 zlog_debug("ISM[%s]: Timer (Wait timer expire)", IF_NAME(oi
));
280 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_WaitTimer
);
285 /* Hook function called after ospf ISM event is occurred. And vty's
286 network command invoke this function after making interface
288 static void ism_timer_set(struct ospf_interface
*oi
)
292 /* First entry point of ospf interface state machine. In this
294 interface parameters must be set to initial values, and
297 OSPF_ISM_TIMER_OFF(oi
->t_hello
);
298 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
299 OSPF_ISM_TIMER_OFF(oi
->t_ls_ack
);
302 /* In this state, the interface may be looped back and will be
303 unavailable for regular data traffic. */
304 OSPF_ISM_TIMER_OFF(oi
->t_hello
);
305 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
306 OSPF_ISM_TIMER_OFF(oi
->t_ls_ack
);
309 /* The router is trying to determine the identity of DRouter and
310 BDRouter. The router begin to receive and send Hello Packets.
312 /* send first hello immediately */
313 OSPF_ISM_TIMER_MSEC_ON(oi
->t_hello
, ospf_hello_timer
, 1);
314 OSPF_ISM_TIMER_ON(oi
->t_wait
, ospf_wait_timer
,
315 OSPF_IF_PARAM(oi
, v_wait
));
316 OSPF_ISM_TIMER_OFF(oi
->t_ls_ack
);
318 case ISM_PointToPoint
:
319 /* The interface connects to a physical Point-to-point network
321 virtual link. The router attempts to form an adjacency with
322 neighboring router. Hello packets are also sent. */
323 /* send first hello immediately */
324 OSPF_ISM_TIMER_MSEC_ON(oi
->t_hello
, ospf_hello_timer
, 1);
325 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
326 OSPF_ISM_TIMER_ON(oi
->t_ls_ack
, ospf_ls_ack_timer
,
330 /* The network type of the interface is broadcast or NBMA
332 and the router itself is neither Designated Router nor
333 Backup Designated Router. */
334 OSPF_HELLO_TIMER_ON(oi
);
335 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
336 OSPF_ISM_TIMER_ON(oi
->t_ls_ack
, ospf_ls_ack_timer
,
340 /* The network type of the interface is broadcast os NBMA
342 and the router is Backup Designated Router. */
343 OSPF_HELLO_TIMER_ON(oi
);
344 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
345 OSPF_ISM_TIMER_ON(oi
->t_ls_ack
, ospf_ls_ack_timer
,
349 /* The network type of the interface is broadcast or NBMA
351 and the router is Designated Router. */
352 OSPF_HELLO_TIMER_ON(oi
);
353 OSPF_ISM_TIMER_OFF(oi
->t_wait
);
354 OSPF_ISM_TIMER_ON(oi
->t_ls_ack
, ospf_ls_ack_timer
,
360 static int ism_interface_up(struct ospf_interface
*oi
)
364 /* if network type is point-to-point, Point-to-MultiPoint or virtual
366 the state transitions to Point-to-Point. */
367 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
368 || oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
369 || oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
370 next_state
= ISM_PointToPoint
;
371 /* Else if the router is not eligible to DR, the state transitions to
373 else if (PRIORITY(oi
) == 0) /* router is eligible? */
374 next_state
= ISM_DROther
;
376 /* Otherwise, the state transitions to Waiting. */
377 next_state
= ISM_Waiting
;
379 if (oi
->type
== OSPF_IFTYPE_NBMA
)
380 ospf_nbr_nbma_if_update(oi
->ospf
, oi
);
382 /* ospf_ism_event (t); */
386 static int ism_loop_ind(struct ospf_interface
*oi
)
388 /* call ism_interface_down. */
389 /* ret = ism_interface_down (oi); */
394 /* Interface down event handler. */
395 static int ism_interface_down(struct ospf_interface
*oi
)
402 static int ism_backup_seen(struct ospf_interface
*oi
)
404 return ospf_dr_election(oi
);
407 static int ism_wait_timer(struct ospf_interface
*oi
)
409 return ospf_dr_election(oi
);
412 static int ism_neighbor_change(struct ospf_interface
*oi
)
414 return ospf_dr_election(oi
);
417 static int ism_ignore(struct ospf_interface
*oi
)
419 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
420 zlog_debug("ISM[%s]: ism_ignore called", IF_NAME(oi
));
425 /* Interface State Machine */
427 int (*func
)(struct ospf_interface
*);
429 } ISM
[OSPF_ISM_STATE_MAX
][OSPF_ISM_EVENT_MAX
] = {
431 /* DependUpon: dummy state. */
432 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
433 {ism_ignore
, ISM_DependUpon
}, /* InterfaceUp */
434 {ism_ignore
, ISM_DependUpon
}, /* WaitTimer */
435 {ism_ignore
, ISM_DependUpon
}, /* BackupSeen */
436 {ism_ignore
, ISM_DependUpon
}, /* NeighborChange */
437 {ism_ignore
, ISM_DependUpon
}, /* LoopInd */
438 {ism_ignore
, ISM_DependUpon
}, /* UnloopInd */
439 {ism_ignore
, ISM_DependUpon
}, /* InterfaceDown */
443 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
444 {ism_interface_up
, ISM_DependUpon
}, /* InterfaceUp */
445 {ism_ignore
, ISM_Down
}, /* WaitTimer */
446 {ism_ignore
, ISM_Down
}, /* BackupSeen */
447 {ism_ignore
, ISM_Down
}, /* NeighborChange */
448 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
449 {ism_ignore
, ISM_Down
}, /* UnloopInd */
450 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
454 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
455 {ism_ignore
, ISM_Loopback
}, /* InterfaceUp */
456 {ism_ignore
, ISM_Loopback
}, /* WaitTimer */
457 {ism_ignore
, ISM_Loopback
}, /* BackupSeen */
458 {ism_ignore
, ISM_Loopback
}, /* NeighborChange */
459 {ism_ignore
, ISM_Loopback
}, /* LoopInd */
460 {ism_ignore
, ISM_Down
}, /* UnloopInd */
461 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
465 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
466 {ism_ignore
, ISM_Waiting
}, /* InterfaceUp */
467 {ism_wait_timer
, ISM_DependUpon
}, /* WaitTimer */
468 {ism_backup_seen
, ISM_DependUpon
}, /* BackupSeen */
469 {ism_ignore
, ISM_Waiting
}, /* NeighborChange */
470 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
471 {ism_ignore
, ISM_Waiting
}, /* UnloopInd */
472 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
475 /* Point-to-Point: */
476 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
477 {ism_ignore
, ISM_PointToPoint
}, /* InterfaceUp */
478 {ism_ignore
, ISM_PointToPoint
}, /* WaitTimer */
479 {ism_ignore
, ISM_PointToPoint
}, /* BackupSeen */
480 {ism_ignore
, ISM_PointToPoint
}, /* NeighborChange */
481 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
482 {ism_ignore
, ISM_PointToPoint
}, /* UnloopInd */
483 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
487 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
488 {ism_ignore
, ISM_DROther
}, /* InterfaceUp */
489 {ism_ignore
, ISM_DROther
}, /* WaitTimer */
490 {ism_ignore
, ISM_DROther
}, /* BackupSeen */
491 {ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
492 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
493 {ism_ignore
, ISM_DROther
}, /* UnloopInd */
494 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
498 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
499 {ism_ignore
, ISM_Backup
}, /* InterfaceUp */
500 {ism_ignore
, ISM_Backup
}, /* WaitTimer */
501 {ism_ignore
, ISM_Backup
}, /* BackupSeen */
502 {ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
503 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
504 {ism_ignore
, ISM_Backup
}, /* UnloopInd */
505 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
509 {ism_ignore
, ISM_DependUpon
}, /* NoEvent */
510 {ism_ignore
, ISM_DR
}, /* InterfaceUp */
511 {ism_ignore
, ISM_DR
}, /* WaitTimer */
512 {ism_ignore
, ISM_DR
}, /* BackupSeen */
513 {ism_neighbor_change
, ISM_DependUpon
}, /* NeighborChange */
514 {ism_loop_ind
, ISM_Loopback
}, /* LoopInd */
515 {ism_ignore
, ISM_DR
}, /* UnloopInd */
516 {ism_interface_down
, ISM_Down
}, /* InterfaceDown */
520 static const char *const ospf_ism_event_str
[] = {
521 "NoEvent", "InterfaceUp", "WaitTimer", "BackupSeen",
522 "NeighborChange", "LoopInd", "UnLoopInd", "InterfaceDown",
525 static void ism_change_state(struct ospf_interface
*oi
, int state
)
528 struct ospf_lsa
*lsa
;
530 /* Logging change of state. */
531 if (IS_DEBUG_OSPF(ism
, ISM_STATUS
))
532 zlog_debug("ISM[%s]: State change %s -> %s", IF_NAME(oi
),
533 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
),
534 lookup_msg(ospf_ism_state_msg
, state
, NULL
));
536 old_state
= oi
->state
;
540 hook_call(ospf_ism_change
, oi
, state
, old_state
);
542 /* Set multicast memberships appropriately for new state. */
543 ospf_if_set_multicast(oi
);
545 if (old_state
== ISM_Down
|| state
== ISM_Down
)
546 ospf_check_abr_status(oi
->ospf
);
548 /* Originate router-LSA. */
549 if (state
== ISM_Down
) {
550 if (oi
->area
->act_ints
> 0)
551 oi
->area
->act_ints
--;
552 } else if (old_state
== ISM_Down
)
553 oi
->area
->act_ints
++;
555 /* schedule router-LSA originate. */
556 ospf_router_lsa_update_area(oi
->area
);
558 /* Originate network-LSA. */
559 if (old_state
!= ISM_DR
&& state
== ISM_DR
)
560 ospf_network_lsa_update(oi
);
561 else if (old_state
== ISM_DR
&& state
!= ISM_DR
) {
562 /* Free self originated network LSA. */
563 lsa
= oi
->network_lsa_self
;
565 ospf_lsa_flush_area(lsa
, oi
->area
);
567 ospf_lsa_unlock(&oi
->network_lsa_self
);
568 oi
->network_lsa_self
= NULL
;
571 ospf_opaque_ism_change(oi
, old_state
);
573 /* Check area border status. */
574 ospf_check_abr_status(oi
->ospf
);
577 /* Execute ISM event process. */
578 int ospf_ism_event(struct thread
*thread
)
582 struct ospf_interface
*oi
;
584 oi
= THREAD_ARG(thread
);
585 event
= THREAD_VAL(thread
);
588 next_state
= (*(ISM
[oi
->state
][event
].func
))(oi
);
591 next_state
= ISM
[oi
->state
][event
].next_state
;
593 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
594 zlog_debug("ISM[%s]: %s (%s)", IF_NAME(oi
),
595 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
),
596 ospf_ism_event_str
[event
]);
598 /* If state is changed. */
599 if (next_state
!= oi
->state
)
600 ism_change_state(oi
, next_state
);
602 /* Make sure timer is set. */