summaryrefslogtreecommitdiffstats
path: root/ospfd/ospf_ism.c
blob: 878ab725bd3270e8d6c4bc588bd3138ea90d2ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * OSPF version 2  Interface State Machine
 *   From RFC2328 [OSPF Version 2]
 * Copyright (C) 1999, 2000 Toshiaki Takada
 */

#include <zebra.h>

#include "frrevent.h"
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "log.h"

#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_network.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_abr.h"

DEFINE_HOOK(ospf_ism_change,
	    (struct ospf_interface * oi, int state, int oldstate),
	    (oi, state, oldstate));

/* elect DR and BDR. Refer to RFC2319 section 9.4 */
static struct ospf_neighbor *ospf_dr_election_sub(struct list *routers)
{
	struct listnode *node;
	struct ospf_neighbor *nbr, *max = NULL;

	/* Choose highest router priority.
	   In case of tie, choose highest Router ID. */
	for (ALL_LIST_ELEMENTS_RO(routers, node, nbr)) {
		if (max == NULL)
			max = nbr;
		else {
			if (max->priority < nbr->priority)
				max = nbr;
			else if (max->priority == nbr->priority)
				if (IPV4_ADDR_CMP(&max->router_id,
						  &nbr->router_id)
				    < 0)
					max = nbr;
		}
	}

	return max;
}

static struct ospf_neighbor *ospf_elect_dr(struct ospf_interface *oi,
					   struct list *el_list)
{
	struct list *dr_list;
	struct listnode *node;
	struct ospf_neighbor *nbr, *dr = NULL, *bdr = NULL;

	dr_list = list_new();

	/* Add neighbors to the list. */
	for (ALL_LIST_ELEMENTS_RO(el_list, node, nbr)) {
		/* neighbor declared to be DR. */
		if (NBR_IS_DR(nbr))
			listnode_add(dr_list, nbr);

		/* Preserve neighbor BDR. */
		if (IPV4_ADDR_SAME(&BDR(oi), &nbr->address.u.prefix4))
			bdr = nbr;
	}

	/* Elect Designated Router. */
	if (listcount(dr_list) > 0)
		dr = ospf_dr_election_sub(dr_list);
	else
		dr = bdr;

	/* Set DR to interface. */
	if (dr)
		DR(oi) = dr->address.u.prefix4;
	else
		DR(oi).s_addr = 0;

	list_delete(&dr_list);

	return dr;
}

static struct ospf_neighbor *ospf_elect_bdr(struct ospf_interface *oi,
					    struct list *el_list)
{
	struct list *bdr_list, *no_dr_list;
	struct listnode *node;
	struct ospf_neighbor *nbr, *bdr = NULL;

	bdr_list = list_new();
	no_dr_list = list_new();

	/* Add neighbors to the list. */
	for (ALL_LIST_ELEMENTS_RO(el_list, node, nbr)) {
		/* neighbor declared to be DR. */
		if (NBR_IS_DR(nbr))
			continue;

		/* neighbor declared to be BDR. */
		if (NBR_IS_BDR(nbr))
			listnode_add(bdr_list, nbr);

		listnode_add(no_dr_list, nbr);
	}

	/* Elect Backup Designated Router. */
	if (listcount(bdr_list) > 0)
		bdr = ospf_dr_election_sub(bdr_list);
	else
		bdr = ospf_dr_election_sub(no_dr_list);

	/* Set BDR to interface. */
	if (bdr)
		BDR(oi) = bdr->address.u.prefix4;
	else
		BDR(oi).s_addr = 0;

	list_delete(&bdr_list);
	list_delete(&no_dr_list);

	return bdr;
}

static int ospf_ism_state(struct ospf_interface *oi)
{
	if (IPV4_ADDR_SAME(&DR(oi), &oi->address->u.prefix4))
		return ISM_DR;
	else if (IPV4_ADDR_SAME(&BDR(oi), &oi->address->u.prefix4))
		return ISM_Backup;
	else
		return ISM_DROther;
}

static void ospf_dr_eligible_routers(struct route_table *nbrs,
				     struct list *el_list)
{
	struct route_node *rn;
	struct ospf_neighbor *nbr;

	for (rn = route_top(nbrs); rn; rn = route_next(rn))
		if ((nbr = rn->info) != NULL)
			/* Ignore 0.0.0.0 node*/
			if (nbr->router_id.s_addr != INADDR_ANY)
				/* Is neighbor eligible? */
				if (nbr->priority > 0)
					/* Is neighbor upper 2-Way? */
					if (nbr->state >= NSM_TwoWay)
						listnode_add(el_list, nbr);
}

/* Generate AdjOK? NSM event. */
static void ospf_dr_change(struct ospf *ospf, struct route_table *nbrs)
{
	struct route_node *rn;
	struct ospf_neighbor *nbr;

	for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
		nbr = rn->info;

		if (!nbr)
			continue;

		/*
		 * Ignore 0.0.0.0 node
		 * Is neighbor 2-Way?
		 * Ignore myself
		 */
		if (nbr->router_id.s_addr != INADDR_ANY
		    && nbr->state >= NSM_TwoWay
		    && !IPV4_ADDR_SAME(&nbr->router_id, &ospf->router_id))
			OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_AdjOK);
	}
}

int ospf_dr_election(struct ospf_interface *oi)
{
	struct in_addr old_dr, old_bdr;
	int old_state, new_state;
	struct list *el_list;

	/* backup current values. */
	old_dr = DR(oi);
	old_bdr = BDR(oi);
	old_state = oi->state;

	el_list = list_new();

	/* List eligible routers. */
	ospf_dr_eligible_routers(oi->nbrs, el_list);

	/* First election of DR and BDR. */
	ospf_elect_bdr(oi, el_list);
	ospf_elect_dr(oi, el_list);

	new_state = ospf_ism_state(oi);

	if (IS_DEBUG_OSPF(ism, ISM_STATUS)) {
		zlog_debug("DR-Election[1st]: Backup %pI4", &BDR(oi));
		zlog_debug("DR-Election[1st]: DR     %pI4", &DR(oi));
	}

	if (new_state != old_state
	    && !(new_state == ISM_DROther && old_state < ISM_DROther)) {
		ospf_elect_bdr(oi, el_list);
		ospf_elect_dr(oi, el_list);

		new_state = ospf_ism_state(oi);

		if (IS_DEBUG_OSPF(ism, ISM_STATUS)) {
			zlog_debug("DR-Election[2nd]: Backup %pI4", &BDR(oi));
			zlog_debug("DR-Election[2nd]: DR     %pI4", &DR(oi));
		}
	}

	list_delete(&el_list);

	/* if DR or BDR changes, cause AdjOK? neighbor event. */
	if (!IPV4_ADDR_SAME(&old_dr, &DR(oi))
	    || !IPV4_ADDR_SAME(&old_bdr, &BDR(oi)))
		ospf_dr_change(oi->ospf, oi->nbrs);

	return new_state;
}


void ospf_hello_timer(struct event *thread)
{
	struct ospf_interface *oi;

	oi = EVENT_ARG(thread);
	oi->t_hello = NULL;

	/* Check if the GR hello-delay is active. */
	if (oi->gr.hello_delay.t_grace_send)
		return;

	if (IS_DEBUG_OSPF(ism, ISM_TIMERS))
		zlog_debug("ISM[%s]: Timer (Hello timer expire)", IF_NAME(oi));

	/* Sending hello packet. */
	ospf_hello_send(oi);

	/* Hello timer set. */
	OSPF_HELLO_TIMER_ON(oi);
}

static void ospf_wait_timer(struct event *thread)
{
	struct ospf_interface *oi;

	oi = EVENT_ARG(thread);
	oi->t_wait = NULL;

	if (IS_DEBUG_OSPF(ism, ISM_TIMERS))
		zlog_debug("ISM[%s]: Timer (Wait timer expire)", IF_NAME(oi));

	OSPF_ISM_EVENT_SCHEDULE(oi, ISM_WaitTimer);
}

/* Hook function called after ospf ISM event is occurred. And vty's
   network command invoke this function after making interface
   structure. */
static void ism_timer_set(struct ospf_interface *oi)
{
	switch (oi->state) {
	case ISM_Down:
		/* First entry point of ospf interface state machine. In this
		   state
		   interface parameters must be set to initial values, and
		   timers are
		   reset also. */
		EVENT_OFF(oi->t_hello);
		EVENT_OFF(oi->t_wait);
		EVENT_OFF(oi->t_ls_ack);
		EVENT_OFF(oi->gr.hello_delay.t_grace_send);
		break;
	case ISM_Loopback:
		/* In this state, the interface may be looped back and will be
		   unavailable for regular data traffic. */
		EVENT_OFF(oi->t_hello);
		EVENT_OFF(oi->t_wait);
		EVENT_OFF(oi->t_ls_ack);
		EVENT_OFF(oi->gr.hello_delay.t_grace_send);
		break;
	case ISM_Waiting:
		/* The router is trying to determine the identity of DRouter and
		   BDRouter. The router begin to receive and send Hello Packets.
		   */
		/* send first hello immediately */
		OSPF_ISM_TIMER_MSEC_ON(oi->t_hello, ospf_hello_timer, 1);
		OSPF_ISM_TIMER_ON(oi->t_wait, ospf_wait_timer,
				  OSPF_IF_PARAM(oi, v_wait));
		EVENT_OFF(oi->t_ls_ack);
		break;
	case ISM_PointToPoint:
		/* The interface connects to a physical Point-to-point network
		   or
		   virtual link. The router attempts to form an adjacency with
		   neighboring router. Hello packets are also sent. */
		/* send first hello immediately */
		OSPF_ISM_TIMER_MSEC_ON(oi->t_hello, ospf_hello_timer, 1);
		EVENT_OFF(oi->t_wait);
		OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
				  oi->v_ls_ack);
		break;
	case ISM_DROther:
		/* The network type of the interface is broadcast or NBMA
		   network,
		   and the router itself is neither Designated Router nor
		   Backup Designated Router. */
		OSPF_HELLO_TIMER_ON(oi);
		EVENT_OFF(oi->t_wait);
		OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
				  oi->v_ls_ack);
		break;
	case ISM_Backup:
		/* The network type of the interface is broadcast os NBMA
		   network,
		   and the router is Backup Designated Router. */
		OSPF_HELLO_TIMER_ON(oi);
		EVENT_OFF(oi->t_wait);
		OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
				  oi->v_ls_ack);
		break;
	case ISM_DR:
		/* The network type of the interface is broadcast or NBMA
		   network,
		   and the router is Designated Router. */
		OSPF_HELLO_TIMER_ON(oi);
		EVENT_OFF(oi->t_wait);
		OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer,
				  oi->v_ls_ack);
		break;
	}
}

static int ism_interface_up(struct ospf_interface *oi)
{
	int next_state = 0;

	/* if network type is point-to-point, Point-to-MultiPoint or virtual
	   link,
	   the state transitions to Point-to-Point. */
	if (oi->type == OSPF_IFTYPE_POINTOPOINT
	    || oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
	    || oi->type == OSPF_IFTYPE_VIRTUALLINK)
		next_state = ISM_PointToPoint;
	/* Else if the router is not eligible to DR, the state transitions to
	   DROther. */
	else if (PRIORITY(oi) == 0) /* router is eligible? */
		next_state = ISM_DROther;
	else
		/* Otherwise, the state transitions to Waiting. */
		next_state = ISM_Waiting;

	if (OSPF_IF_NON_BROADCAST(oi))
		ospf_nbr_nbma_if_update(oi->ospf, oi);

	/*  ospf_ism_event (t); */
	return next_state;
}

static int ism_loop_ind(struct ospf_interface *oi)
{
	/* call ism_interface_down. */
	/* ret = ism_interface_down (oi); */

	return 0;
}

/* Interface down event handler. */
static int ism_interface_down(struct ospf_interface *oi)
{
	ospf_if_cleanup(oi);
	return 0;
}


static int ism_backup_seen(struct ospf_interface *oi)
{
	return ospf_dr_election(oi);
}

static int ism_wait_timer(struct ospf_interface *oi)
{
	return ospf_dr_election(oi);
}

static int ism_neighbor_change(struct ospf_interface *oi)
{
	return ospf_dr_election(oi);
}

static int ism_ignore(struct ospf_interface *oi)
{
	if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
		zlog_debug("ISM[%s]: ism_ignore called", IF_NAME(oi));

	return 0;
}

/* Interface State Machine */
const struct {
	int (*func)(struct ospf_interface *);
	int next_state;
} ISM[OSPF_ISM_STATE_MAX][OSPF_ISM_EVENT_MAX] = {
	{
		/* DependUpon: dummy state. */
		{ism_ignore, ISM_DependUpon}, /* NoEvent        */
		{ism_ignore, ISM_DependUpon}, /* InterfaceUp    */
		{ism_ignore, ISM_DependUpon}, /* WaitTimer      */
		{ism_ignore, ISM_DependUpon}, /* BackupSeen     */
		{ism_ignore, ISM_DependUpon}, /* NeighborChange */
		{ism_ignore, ISM_DependUpon}, /* LoopInd        */
		{ism_ignore, ISM_DependUpon}, /* UnloopInd      */
		{ism_ignore, ISM_DependUpon}, /* InterfaceDown  */
	},
	{
		/* Down:*/
		{ism_ignore, ISM_DependUpon},       /* NoEvent        */
		{ism_interface_up, ISM_DependUpon}, /* InterfaceUp    */
		{ism_ignore, ISM_Down},		    /* WaitTimer      */
		{ism_ignore, ISM_Down},		    /* BackupSeen     */
		{ism_ignore, ISM_Down},		    /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},       /* LoopInd        */
		{ism_ignore, ISM_Down},		    /* UnloopInd      */
		{ism_interface_down, ISM_Down},     /* InterfaceDown  */
	},
	{
		/* Loopback: */
		{ism_ignore, ISM_DependUpon},   /* NoEvent        */
		{ism_ignore, ISM_Loopback},     /* InterfaceUp    */
		{ism_ignore, ISM_Loopback},     /* WaitTimer      */
		{ism_ignore, ISM_Loopback},     /* BackupSeen     */
		{ism_ignore, ISM_Loopback},     /* NeighborChange */
		{ism_ignore, ISM_Loopback},     /* LoopInd        */
		{ism_ignore, ISM_Down},		/* UnloopInd      */
		{ism_interface_down, ISM_Down}, /* InterfaceDown  */
	},
	{
		/* Waiting: */
		{ism_ignore, ISM_DependUpon},      /* NoEvent        */
		{ism_ignore, ISM_Waiting},	 /* InterfaceUp    */
		{ism_wait_timer, ISM_DependUpon},  /* WaitTimer      */
		{ism_backup_seen, ISM_DependUpon}, /* BackupSeen     */
		{ism_ignore, ISM_Waiting},	 /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},      /* LoopInd        */
		{ism_ignore, ISM_Waiting},	 /* UnloopInd      */
		{ism_interface_down, ISM_Down},    /* InterfaceDown  */
	},
	{
		/* Point-to-Point: */
		{ism_ignore, ISM_DependUpon},   /* NoEvent        */
		{ism_ignore, ISM_PointToPoint}, /* InterfaceUp    */
		{ism_ignore, ISM_PointToPoint}, /* WaitTimer      */
		{ism_ignore, ISM_PointToPoint}, /* BackupSeen     */
		{ism_ignore, ISM_PointToPoint}, /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},   /* LoopInd        */
		{ism_ignore, ISM_PointToPoint}, /* UnloopInd      */
		{ism_interface_down, ISM_Down}, /* InterfaceDown  */
	},
	{
		/* DROther: */
		{ism_ignore, ISM_DependUpon},	  /* NoEvent        */
		{ism_ignore, ISM_DROther},	     /* InterfaceUp    */
		{ism_ignore, ISM_DROther},	     /* WaitTimer      */
		{ism_ignore, ISM_DROther},	     /* BackupSeen     */
		{ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},	  /* LoopInd        */
		{ism_ignore, ISM_DROther},	     /* UnloopInd      */
		{ism_interface_down, ISM_Down},	/* InterfaceDown  */
	},
	{
		/* Backup: */
		{ism_ignore, ISM_DependUpon},	  /* NoEvent        */
		{ism_ignore, ISM_Backup},	      /* InterfaceUp    */
		{ism_ignore, ISM_Backup},	      /* WaitTimer      */
		{ism_ignore, ISM_Backup},	      /* BackupSeen     */
		{ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},	  /* LoopInd        */
		{ism_ignore, ISM_Backup},	      /* UnloopInd      */
		{ism_interface_down, ISM_Down},	/* InterfaceDown  */
	},
	{
		/* DR: */
		{ism_ignore, ISM_DependUpon},	  /* NoEvent        */
		{ism_ignore, ISM_DR},		       /* InterfaceUp    */
		{ism_ignore, ISM_DR},		       /* WaitTimer      */
		{ism_ignore, ISM_DR},		       /* BackupSeen     */
		{ism_neighbor_change, ISM_DependUpon}, /* NeighborChange */
		{ism_loop_ind, ISM_Loopback},	  /* LoopInd        */
		{ism_ignore, ISM_DR},		       /* UnloopInd      */
		{ism_interface_down, ISM_Down},	/* InterfaceDown  */
	},
};

static const char *const ospf_ism_event_str[] = {
	"NoEvent",	"InterfaceUp", "WaitTimer", "BackupSeen",
	"NeighborChange", "LoopInd",     "UnLoopInd", "InterfaceDown",
};

static void ism_change_state(struct ospf_interface *oi, int state)
{
	int old_state;
	struct ospf_lsa *lsa;

	/* Logging change of state. */
	if (IS_DEBUG_OSPF(ism, ISM_STATUS))
		zlog_debug("ISM[%s]: State change %s -> %s", IF_NAME(oi),
			   lookup_msg(ospf_ism_state_msg, oi->state, NULL),
			   lookup_msg(ospf_ism_state_msg, state, NULL));

	old_state = oi->state;
	oi->state = state;
	oi->state_change++;

	hook_call(ospf_ism_change, oi, state, old_state);

	/* Set multicast memberships appropriately for new state. */
	ospf_if_set_multicast(oi);

	if (old_state == ISM_Down || state == ISM_Down)
		ospf_check_abr_status(oi->ospf);

	/* Originate router-LSA. */
	if (state == ISM_Down) {
		if (oi->area->act_ints > 0)
			oi->area->act_ints--;
	} else if (old_state == ISM_Down)
		oi->area->act_ints++;

	/* schedule router-LSA originate. */
	ospf_router_lsa_update_area(oi->area);

	/* Originate network-LSA. */
	if (old_state != ISM_DR && state == ISM_DR)
		ospf_network_lsa_update(oi);
	else if (old_state == ISM_DR && state != ISM_DR) {
		/* Free self originated network LSA. */
		lsa = oi->network_lsa_self;
		if (lsa)
			ospf_lsa_flush_area(lsa, oi->area);

		ospf_lsa_unlock(&oi->network_lsa_self);
		oi->network_lsa_self = NULL;
	}

	ospf_opaque_ism_change(oi, old_state);

	/* Check area border status.  */
	ospf_check_abr_status(oi->ospf);
}

/* Execute ISM event process. */
void ospf_ism_event(struct event *thread)
{
	int event;
	int next_state;
	struct ospf_interface *oi;

	oi = EVENT_ARG(thread);
	event = EVENT_VAL(thread);

	/* Call function. */
	next_state = (*(ISM[oi->state][event].func))(oi);

	if (!next_state)
		next_state = ISM[oi->state][event].next_state;

	if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
		zlog_debug("ISM[%s]: %s (%s)", IF_NAME(oi),
			   lookup_msg(ospf_ism_state_msg, oi->state, NULL),
			   ospf_ism_event_str[event]);

	/* If state is changed. */
	if (next_state != oi->state)
		ism_change_state(oi, next_state);

	/* Make sure timer is set. */
	ism_timer_set(oi);
}