In this document, I am going to represent how DR is elected in different cases. I have to skip some pictures since there would be so many pictures to demonstrate all the cases. I will try to explain them.
In general, there will be the following cases when a router is selected as a DR.
1: There is no other OSPF router in the segment.
2- There is a DR in the segment and other routers join later after waiting state.
3- There are two or more OSPF routers coming up at the same time (During the waiting state)
4- There are two DRs in a segment.
First, I am using the following topology for my presentation.
With just looking at the topology, we say that R3 would be elected as a DR because it has higher IP address compared to other routers but it is not always the case. Just to mention priority is default on all interfaces.
Lets talk about about the first case. I shut down R2 and R3 interfaces. Only R1 is presented in the segment. In broadcast and non-broadcast network type, we have DR and BDR election. It is said BDR is elected before DR but it is not important in this presentation. When we enable OSPF on broadcast or non-broadcast interfaces, we see DR, BDR fileds in hello packets. When a router comes up, it advertises 0.0.0.0 in DR and BDR fields in hello packets as you see in the following picture.
It waits for 40 seconds in broadcast network ( default dead interval is 40 in broadcast) and then elects itself as DR and then continues sending hello packets specifying itself as DR in the DR field in hello packets. The waiting timer (or state) is equal to OSPF dead interval on the interface.
As you see in the picture above, at 15:40:31 interface comes up and at 15:41:11 waiting state ends and R1 elects itself as DR.
Now, If I unshut R2 interface, R2 will be elected as BDR and both R1 and R2 will send hello packets. In their both hello packets, 172.16.1.1 is in DR field and 172.16.1.2 in BDR field. Now, I unshut R3 interface. In this case, R3 receives hello packets but DR and BDR fields have already entries. R3 does not wait for wait state timer. It agrees on R1 and R2 being DR and BDR and goes directly for DBD exchange even though it has higher interface IP address (eligible to be DR).
DR and BDR election happens before DBD exchange. We can say it is after two-way communication but in the first case, we saw that R1 elects itself as DR before receiving any hello packets(before going to two-way state).
So far we have discussed case 1 and 2:
Lets talk about a case when several routers come up at the same time. In this case they all send hello packets with 0.0.0.0 in DR/BDR field. They all wait until waiting state ends and then elect DR and BDR. The result is going to be the same if one router comes up a little later during the waiting state.
In the picture above, you can see R1 and R2 become DR and BDR after 40 seconds when Wait on the interface ends. ( Sometimes, we see it takes longer than usual in order for routers to become adjacent. one of the reason is Waiting state in DR election)
We noticed that when there is already a DR in the segment, there will be no election(current DR remains) but what if there are two DRs in the segment. In some cases it happens. I will explain it with the following topology:
In the topology above, R2 is DR for the R1/R2 segment and R4 is DR for R3/R4 segment. It means in their hello packets, DR and BDR field have already entries. Now, we connect these segments.
In this case, routers go for election process, but they do not wait for Wait state. R4 will be selected as DR and R3 as BDR after a few seconds. so we see we have preemption in election in some cases.
To refresh your memory
OSPF state machine has different phases
Exstart (Master slave elect happens here)
As I mentioned before, DR and BDR election happen before Exstart.