]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_ism.c
Merge pull request #10655 from donaldsharp/timers_warning_when_large
[mirror_frr.git] / ospfd / ospf_ism.c
1 /*
2 * OSPF version 2 Interface 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 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
21 */
22
23 #include <zebra.h>
24
25 #include "thread.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "if.h"
29 #include "table.h"
30 #include "log.h"
31
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"
45
46 DEFINE_HOOK(ospf_ism_change,
47 (struct ospf_interface * oi, int state, int oldstate),
48 (oi, state, oldstate));
49
50 /* elect DR and BDR. Refer to RFC2319 section 9.4 */
51 static struct ospf_neighbor *ospf_dr_election_sub(struct list *routers)
52 {
53 struct listnode *node;
54 struct ospf_neighbor *nbr, *max = NULL;
55
56 /* Choose highest router priority.
57 In case of tie, choose highest Router ID. */
58 for (ALL_LIST_ELEMENTS_RO(routers, node, nbr)) {
59 if (max == NULL)
60 max = nbr;
61 else {
62 if (max->priority < nbr->priority)
63 max = nbr;
64 else if (max->priority == nbr->priority)
65 if (IPV4_ADDR_CMP(&max->router_id,
66 &nbr->router_id)
67 < 0)
68 max = nbr;
69 }
70 }
71
72 return max;
73 }
74
75 static struct ospf_neighbor *ospf_elect_dr(struct ospf_interface *oi,
76 struct list *el_list)
77 {
78 struct list *dr_list;
79 struct listnode *node;
80 struct ospf_neighbor *nbr, *dr = NULL, *bdr = NULL;
81
82 dr_list = list_new();
83
84 /* Add neighbors to the list. */
85 for (ALL_LIST_ELEMENTS_RO(el_list, node, nbr)) {
86 /* neighbor declared to be DR. */
87 if (NBR_IS_DR(nbr))
88 listnode_add(dr_list, nbr);
89
90 /* Preserve neighbor BDR. */
91 if (IPV4_ADDR_SAME(&BDR(oi), &nbr->address.u.prefix4))
92 bdr = nbr;
93 }
94
95 /* Elect Designated Router. */
96 if (listcount(dr_list) > 0)
97 dr = ospf_dr_election_sub(dr_list);
98 else
99 dr = bdr;
100
101 /* Set DR to interface. */
102 if (dr)
103 DR(oi) = dr->address.u.prefix4;
104 else
105 DR(oi).s_addr = 0;
106
107 list_delete(&dr_list);
108
109 return dr;
110 }
111
112 static struct ospf_neighbor *ospf_elect_bdr(struct ospf_interface *oi,
113 struct list *el_list)
114 {
115 struct list *bdr_list, *no_dr_list;
116 struct listnode *node;
117 struct ospf_neighbor *nbr, *bdr = NULL;
118
119 bdr_list = list_new();
120 no_dr_list = list_new();
121
122 /* Add neighbors to the list. */
123 for (ALL_LIST_ELEMENTS_RO(el_list, node, nbr)) {
124 /* neighbor declared to be DR. */
125 if (NBR_IS_DR(nbr))
126 continue;
127
128 /* neighbor declared to be BDR. */
129 if (NBR_IS_BDR(nbr))
130 listnode_add(bdr_list, nbr);
131
132 listnode_add(no_dr_list, nbr);
133 }
134
135 /* Elect Backup Designated Router. */
136 if (listcount(bdr_list) > 0)
137 bdr = ospf_dr_election_sub(bdr_list);
138 else
139 bdr = ospf_dr_election_sub(no_dr_list);
140
141 /* Set BDR to interface. */
142 if (bdr)
143 BDR(oi) = bdr->address.u.prefix4;
144 else
145 BDR(oi).s_addr = 0;
146
147 list_delete(&bdr_list);
148 list_delete(&no_dr_list);
149
150 return bdr;
151 }
152
153 static int ospf_ism_state(struct ospf_interface *oi)
154 {
155 if (IPV4_ADDR_SAME(&DR(oi), &oi->address->u.prefix4))
156 return ISM_DR;
157 else if (IPV4_ADDR_SAME(&BDR(oi), &oi->address->u.prefix4))
158 return ISM_Backup;
159 else
160 return ISM_DROther;
161 }
162
163 static void ospf_dr_eligible_routers(struct route_table *nbrs,
164 struct list *el_list)
165 {
166 struct route_node *rn;
167 struct ospf_neighbor *nbr;
168
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);
178 }
179
180 /* Generate AdjOK? NSM event. */
181 static void ospf_dr_change(struct ospf *ospf, struct route_table *nbrs)
182 {
183 struct route_node *rn;
184 struct ospf_neighbor *nbr;
185
186 for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
187 nbr = rn->info;
188
189 if (!nbr)
190 continue;
191
192 /*
193 * Ignore 0.0.0.0 node
194 * Is neighbor 2-Way?
195 * Ignore myself
196 */
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);
201 }
202 }
203
204 int ospf_dr_election(struct ospf_interface *oi)
205 {
206 struct in_addr old_dr, old_bdr;
207 int old_state, new_state;
208 struct list *el_list;
209
210 /* backup current values. */
211 old_dr = DR(oi);
212 old_bdr = BDR(oi);
213 old_state = oi->state;
214
215 el_list = list_new();
216
217 /* List eligible routers. */
218 ospf_dr_eligible_routers(oi->nbrs, el_list);
219
220 /* First election of DR and BDR. */
221 ospf_elect_bdr(oi, el_list);
222 ospf_elect_dr(oi, el_list);
223
224 new_state = ospf_ism_state(oi);
225
226 zlog_debug("DR-Election[1st]: Backup %pI4", &BDR(oi));
227 zlog_debug("DR-Election[1st]: DR %pI4", &DR(oi));
228
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);
233
234 new_state = ospf_ism_state(oi);
235
236 zlog_debug("DR-Election[2nd]: Backup %pI4", &BDR(oi));
237 zlog_debug("DR-Election[2nd]: DR %pI4", &DR(oi));
238 }
239
240 list_delete(&el_list);
241
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);
246
247 return new_state;
248 }
249
250
251 void ospf_hello_timer(struct thread *thread)
252 {
253 struct ospf_interface *oi;
254
255 oi = THREAD_ARG(thread);
256 oi->t_hello = NULL;
257
258 if (IS_DEBUG_OSPF(ism, ISM_TIMERS))
259 zlog_debug("ISM[%s]: Timer (Hello timer expire)", IF_NAME(oi));
260
261 /* Sending hello packet. */
262 ospf_hello_send(oi);
263
264 /* Hello timer set. */
265 OSPF_HELLO_TIMER_ON(oi);
266 }
267
268 static void ospf_wait_timer(struct thread *thread)
269 {
270 struct ospf_interface *oi;
271
272 oi = THREAD_ARG(thread);
273 oi->t_wait = NULL;
274
275 if (IS_DEBUG_OSPF(ism, ISM_TIMERS))
276 zlog_debug("ISM[%s]: Timer (Wait timer expire)", IF_NAME(oi));
277
278 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_WaitTimer);
279 }
280
281 /* Hook function called after ospf ISM event is occurred. And vty's
282 network command invoke this function after making interface
283 structure. */
284 static void ism_timer_set(struct ospf_interface *oi)
285 {
286 switch (oi->state) {
287 case ISM_Down:
288 /* First entry point of ospf interface state machine. In this
289 state
290 interface parameters must be set to initial values, and
291 timers are
292 reset also. */
293 OSPF_ISM_TIMER_OFF(oi->t_hello);
294 OSPF_ISM_TIMER_OFF(oi->t_wait);
295 OSPF_ISM_TIMER_OFF(oi->t_ls_ack);
296 break;
297 case ISM_Loopback:
298 /* In this state, the interface may be looped back and will be
299 unavailable for regular data traffic. */
300 OSPF_ISM_TIMER_OFF(oi->t_hello);
301 OSPF_ISM_TIMER_OFF(oi->t_wait);
302 OSPF_ISM_TIMER_OFF(oi->t_ls_ack);
303 break;
304 case ISM_Waiting:
305 /* The router is trying to determine the identity of DRouter and
306 BDRouter. The router begin to receive and send Hello Packets.
307 */
308 /* send first hello immediately */
309 OSPF_ISM_TIMER_MSEC_ON(oi->t_hello, ospf_hello_timer, 1);
310 OSPF_ISM_TIMER_ON(oi->t_wait, ospf_wait_timer,
311 OSPF_IF_PARAM(oi, v_wait));
312 OSPF_ISM_TIMER_OFF(oi->t_ls_ack);
313 break;
314 case ISM_PointToPoint:
315 /* The interface connects to a physical Point-to-point network
316 or
317 virtual link. The router attempts to form an adjacency with
318 neighboring router. Hello packets are also sent. */
319 /* send first hello immediately */
320 OSPF_ISM_TIMER_MSEC_ON(oi->t_hello, ospf_hello_timer, 1);
321 OSPF_ISM_TIMER_OFF(oi->t_wait);
322 OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
323 oi->v_ls_ack);
324 break;
325 case ISM_DROther:
326 /* The network type of the interface is broadcast or NBMA
327 network,
328 and the router itself is neither Designated Router nor
329 Backup Designated Router. */
330 OSPF_HELLO_TIMER_ON(oi);
331 OSPF_ISM_TIMER_OFF(oi->t_wait);
332 OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
333 oi->v_ls_ack);
334 break;
335 case ISM_Backup:
336 /* The network type of the interface is broadcast os NBMA
337 network,
338 and the router is Backup Designated Router. */
339 OSPF_HELLO_TIMER_ON(oi);
340 OSPF_ISM_TIMER_OFF(oi->t_wait);
341 OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
342 oi->v_ls_ack);
343 break;
344 case ISM_DR:
345 /* The network type of the interface is broadcast or NBMA
346 network,
347 and the router is Designated Router. */
348 OSPF_HELLO_TIMER_ON(oi);
349 OSPF_ISM_TIMER_OFF(oi->t_wait);
350 OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
351 oi->v_ls_ack);
352 break;
353 }
354 }
355
356 static int ism_interface_up(struct ospf_interface *oi)
357 {
358 int next_state = 0;
359
360 /* if network type is point-to-point, Point-to-MultiPoint or virtual
361 link,
362 the state transitions to Point-to-Point. */
363 if (oi->type == OSPF_IFTYPE_POINTOPOINT
364 || oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
365 || oi->type == OSPF_IFTYPE_VIRTUALLINK)
366 next_state = ISM_PointToPoint;
367 /* Else if the router is not eligible to DR, the state transitions to
368 DROther. */
369 else if (PRIORITY(oi) == 0) /* router is eligible? */
370 next_state = ISM_DROther;
371 else
372 /* Otherwise, the state transitions to Waiting. */
373 next_state = ISM_Waiting;
374
375 if (oi->type == OSPF_IFTYPE_NBMA)
376 ospf_nbr_nbma_if_update(oi->ospf, oi);
377
378 /* ospf_ism_event (t); */
379 return next_state;
380 }
381
382 static int ism_loop_ind(struct ospf_interface *oi)
383 {
384 /* call ism_interface_down. */
385 /* ret = ism_interface_down (oi); */
386
387 return 0;
388 }
389
390 /* Interface down event handler. */
391 static int ism_interface_down(struct ospf_interface *oi)
392 {
393 ospf_if_cleanup(oi);
394 return 0;
395 }
396
397
398 static int ism_backup_seen(struct ospf_interface *oi)
399 {
400 return ospf_dr_election(oi);
401 }
402
403 static int ism_wait_timer(struct ospf_interface *oi)
404 {
405 return ospf_dr_election(oi);
406 }
407
408 static int ism_neighbor_change(struct ospf_interface *oi)
409 {
410 return ospf_dr_election(oi);
411 }
412
413 static int ism_ignore(struct ospf_interface *oi)
414 {
415 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
416 zlog_debug("ISM[%s]: ism_ignore called", IF_NAME(oi));
417
418 return 0;
419 }
420
421 /* Interface State Machine */
422 const struct {
423 int (*func)(struct ospf_interface *);
424 int next_state;
425 } ISM[OSPF_ISM_STATE_MAX][OSPF_ISM_EVENT_MAX] = {
426 {
427 /* DependUpon: dummy state. */
428 {ism_ignore, ISM_DependUpon}, /* NoEvent */
429 {ism_ignore, ISM_DependUpon}, /* InterfaceUp */
430 {ism_ignore, ISM_DependUpon}, /* WaitTimer */
431 {ism_ignore, ISM_DependUpon}, /* BackupSeen */
432 {ism_ignore, ISM_DependUpon}, /* NeighborChange */
433 {ism_ignore, ISM_DependUpon}, /* LoopInd */
434 {ism_ignore, ISM_DependUpon}, /* UnloopInd */
435 {ism_ignore, ISM_DependUpon}, /* InterfaceDown */
436 },
437 {
438 /* Down:*/
439 {ism_ignore, ISM_DependUpon}, /* NoEvent */
440 {ism_interface_up, ISM_DependUpon}, /* InterfaceUp */
441 {ism_ignore, ISM_Down}, /* WaitTimer */
442 {ism_ignore, ISM_Down}, /* BackupSeen */
443 {ism_ignore, ISM_Down}, /* NeighborChange */
444 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
445 {ism_ignore, ISM_Down}, /* UnloopInd */
446 {ism_interface_down, ISM_Down}, /* InterfaceDown */
447 },
448 {
449 /* Loopback: */
450 {ism_ignore, ISM_DependUpon}, /* NoEvent */
451 {ism_ignore, ISM_Loopback}, /* InterfaceUp */
452 {ism_ignore, ISM_Loopback}, /* WaitTimer */
453 {ism_ignore, ISM_Loopback}, /* BackupSeen */
454 {ism_ignore, ISM_Loopback}, /* NeighborChange */
455 {ism_ignore, ISM_Loopback}, /* LoopInd */
456 {ism_ignore, ISM_Down}, /* UnloopInd */
457 {ism_interface_down, ISM_Down}, /* InterfaceDown */
458 },
459 {
460 /* Waiting: */
461 {ism_ignore, ISM_DependUpon}, /* NoEvent */
462 {ism_ignore, ISM_Waiting}, /* InterfaceUp */
463 {ism_wait_timer, ISM_DependUpon}, /* WaitTimer */
464 {ism_backup_seen, ISM_DependUpon}, /* BackupSeen */
465 {ism_ignore, ISM_Waiting}, /* NeighborChange */
466 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
467 {ism_ignore, ISM_Waiting}, /* UnloopInd */
468 {ism_interface_down, ISM_Down}, /* InterfaceDown */
469 },
470 {
471 /* Point-to-Point: */
472 {ism_ignore, ISM_DependUpon}, /* NoEvent */
473 {ism_ignore, ISM_PointToPoint}, /* InterfaceUp */
474 {ism_ignore, ISM_PointToPoint}, /* WaitTimer */
475 {ism_ignore, ISM_PointToPoint}, /* BackupSeen */
476 {ism_ignore, ISM_PointToPoint}, /* NeighborChange */
477 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
478 {ism_ignore, ISM_PointToPoint}, /* UnloopInd */
479 {ism_interface_down, ISM_Down}, /* InterfaceDown */
480 },
481 {
482 /* DROther: */
483 {ism_ignore, ISM_DependUpon}, /* NoEvent */
484 {ism_ignore, ISM_DROther}, /* InterfaceUp */
485 {ism_ignore, ISM_DROther}, /* WaitTimer */
486 {ism_ignore, ISM_DROther}, /* BackupSeen */
487 {ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
488 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
489 {ism_ignore, ISM_DROther}, /* UnloopInd */
490 {ism_interface_down, ISM_Down}, /* InterfaceDown */
491 },
492 {
493 /* Backup: */
494 {ism_ignore, ISM_DependUpon}, /* NoEvent */
495 {ism_ignore, ISM_Backup}, /* InterfaceUp */
496 {ism_ignore, ISM_Backup}, /* WaitTimer */
497 {ism_ignore, ISM_Backup}, /* BackupSeen */
498 {ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
499 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
500 {ism_ignore, ISM_Backup}, /* UnloopInd */
501 {ism_interface_down, ISM_Down}, /* InterfaceDown */
502 },
503 {
504 /* DR: */
505 {ism_ignore, ISM_DependUpon}, /* NoEvent */
506 {ism_ignore, ISM_DR}, /* InterfaceUp */
507 {ism_ignore, ISM_DR}, /* WaitTimer */
508 {ism_ignore, ISM_DR}, /* BackupSeen */
509 {ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
510 {ism_loop_ind, ISM_Loopback}, /* LoopInd */
511 {ism_ignore, ISM_DR}, /* UnloopInd */
512 {ism_interface_down, ISM_Down}, /* InterfaceDown */
513 },
514 };
515
516 static const char *const ospf_ism_event_str[] = {
517 "NoEvent", "InterfaceUp", "WaitTimer", "BackupSeen",
518 "NeighborChange", "LoopInd", "UnLoopInd", "InterfaceDown",
519 };
520
521 static void ism_change_state(struct ospf_interface *oi, int state)
522 {
523 int old_state;
524 struct ospf_lsa *lsa;
525
526 /* Logging change of state. */
527 if (IS_DEBUG_OSPF(ism, ISM_STATUS))
528 zlog_debug("ISM[%s]: State change %s -> %s", IF_NAME(oi),
529 lookup_msg(ospf_ism_state_msg, oi->state, NULL),
530 lookup_msg(ospf_ism_state_msg, state, NULL));
531
532 old_state = oi->state;
533 oi->state = state;
534 oi->state_change++;
535
536 hook_call(ospf_ism_change, oi, state, old_state);
537
538 /* Set multicast memberships appropriately for new state. */
539 ospf_if_set_multicast(oi);
540
541 if (old_state == ISM_Down || state == ISM_Down)
542 ospf_check_abr_status(oi->ospf);
543
544 /* Originate router-LSA. */
545 if (state == ISM_Down) {
546 if (oi->area->act_ints > 0)
547 oi->area->act_ints--;
548 } else if (old_state == ISM_Down)
549 oi->area->act_ints++;
550
551 /* schedule router-LSA originate. */
552 ospf_router_lsa_update_area(oi->area);
553
554 /* Originate network-LSA. */
555 if (old_state != ISM_DR && state == ISM_DR)
556 ospf_network_lsa_update(oi);
557 else if (old_state == ISM_DR && state != ISM_DR) {
558 /* Free self originated network LSA. */
559 lsa = oi->network_lsa_self;
560 if (lsa)
561 ospf_lsa_flush_area(lsa, oi->area);
562
563 ospf_lsa_unlock(&oi->network_lsa_self);
564 oi->network_lsa_self = NULL;
565 }
566
567 ospf_opaque_ism_change(oi, old_state);
568
569 /* Check area border status. */
570 ospf_check_abr_status(oi->ospf);
571 }
572
573 /* Execute ISM event process. */
574 void ospf_ism_event(struct thread *thread)
575 {
576 int event;
577 int next_state;
578 struct ospf_interface *oi;
579
580 oi = THREAD_ARG(thread);
581 event = THREAD_VAL(thread);
582
583 /* Call function. */
584 next_state = (*(ISM[oi->state][event].func))(oi);
585
586 if (!next_state)
587 next_state = ISM[oi->state][event].next_state;
588
589 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
590 zlog_debug("ISM[%s]: %s (%s)", IF_NAME(oi),
591 lookup_msg(ospf_ism_state_msg, oi->state, NULL),
592 ospf_ism_event_str[event]);
593
594 /* If state is changed. */
595 if (next_state != oi->state)
596 ism_change_state(oi, next_state);
597
598 /* Make sure timer is set. */
599 ism_timer_set(oi);
600 }