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
|
/*
* BIRD -- OSPF
*
* (c) 1999 Ondrej Filip <feela@network.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "ospf.h"
char *ospf_ns[]={"down", "attempt", "init", "2way", "exstart", "exchange",
"loading", "full"};
const char *ospf_inm[]={ "hello received", "neighbor start", "2-way received",
"negotiation done", "exstart done", "bad ls request", "load done",
"adjacency ok?", "sequence mismatch", "1-way received", "kill neighbor",
"inactivity timer", "line down" };
void
neigh_chstate(struct ospf_neighbor *n, u8 state)
{
struct ospf_iface *ifa;
struct proto *p;
if(n->state!=state)
{
ifa=n->ifa;
p=(struct proto *)(ifa->proto);
debug("%s: Neigbor %I changes state from \"%s\" to \"%s\".\n",
p->name, n->rid, ospf_ns[n->state], ospf_ns[state]);
n->state=state;
}
}
struct ospf_neighbor *
electbdr(list nl)
{
struct ospf_neighbor *neigh,*n1,*n2;
n1=NULL;
n2=NULL;
WALK_LIST (neigh, nl) /* First try those decl. themselves */
{
if(neigh->state>=NEIGHBOR_2WAY) /* Higher than 2WAY */
if(neigh->priority>0) /* Eligible */
if(neigh->rid!=neigh->dr) /* And not declaring itself DR */
{
if(neigh->rid==neigh->bdr) /* Declaring BDR */
{
if(n1!=NULL)
{
if(neigh->priority>n1->priority) n1=neigh;
else if(neigh->priority==n1->priority)
if(neigh->rid>n1->rid) n1=neigh;
}
else
{
n1=neigh;
}
}
else /* And NOT declaring BDR */
{
if(n2!=NULL)
{
if(neigh->priority>n2->priority) n2=neigh;
else if(neigh->priority==n2->priority)
if(neigh->rid>n2->rid) n2=neigh;
}
else
{
n2=neigh;
}
}
}
}
if(n1==NULL) n1=n2;
return(n1);
}
struct ospf_neighbor *
electdr(list nl)
{
struct ospf_neighbor *neigh,*n;
n=NULL;
WALK_LIST (neigh, nl) /* And now DR */
{
if(neigh->state>=NEIGHBOR_2WAY) /* Higher than 2WAY */
if(neigh->priority>0) /* Eligible */
if(neigh->rid==neigh->dr) /* And declaring itself DR */
{
if(n!=NULL)
{
if(neigh->priority>n->priority) n=neigh;
else if(neigh->priority==n->priority)
if(neigh->rid>n->rid) n=neigh;
}
else
{
n=neigh;
}
}
}
return(n);
}
int
can_do_adj(struct ospf_neighbor *n)
{
struct ospf_iface *ifa;
struct proto *p;
int i;
ifa=n->ifa;
p=(struct proto *)(ifa->proto);
i=0;
switch(ifa->type)
{
case OSPF_IT_PTP:
case OSPF_IT_VLINK:
i=1;
break;
case OSPF_IT_BCAST:
case OSPF_IT_NBMA:
switch(ifa->state)
{
case OSPF_IS_DOWN:
die("%s: Iface %s in down state?", p->name, ifa->iface->name);
break;
case OSPF_IS_WAITING:
DBG("%s: Neighbor? on iface %s\n",p->name, ifa->iface->name);
break;
case OSPF_IS_DROTHER:
if(((n->rid==ifa->drid) || (n->rid==ifa->bdrid))
&& (n->state==NEIGHBOR_2WAY)) i=1;
break;
case OSPF_IS_PTP:
case OSPF_IS_BACKUP:
case OSPF_IS_DR:
if(n->state==NEIGHBOR_2WAY) i=1;
break;
default:
die("%s: Iface %s in unknown state?",p->name, ifa->iface->name);
break;
}
break;
default:
die("%s: Iface %s is unknown type?",p->name, ifa->iface->name);
break;
}
DBG("%s: Iface %s can_do_adj=%d\n",p->name, ifa->iface->name,i);
return i;
}
/* Try to build neighbor adjacency (if does not exists) */
void
tryadj(struct ospf_neighbor *n, struct proto *p)
{
DBG("%s: Going to build adjacency.\n", p->name);
neigh_chstate(n,NEIGHBOR_EXSTART);
if(n->adj==0) /* First time adjacency */
{
n->dds=random_u32();
}
n->dds++;
n->myimms.byte=0;
n->myimms.bit.ms=1;
n->myimms.bit.m=1;
n->myimms.bit.i=1;
tm_start(n->rxmt_timer,1); /* Or some other number ? */
}
void
ospf_neigh_sm(struct ospf_neighbor *n, int event)
/* Interface state machine */
{
struct proto *p;
p=(struct proto *)(n->ifa->proto);
DBG("%s: Neighbor state machine for neighbor %I, event \"%s\".\n",
p->name, n->rid, ospf_inm[event]);
switch(event)
{
case INM_START:
neigh_chstate(n,NEIGHBOR_ATTEMPT);
/* FIXME No NBMA now */
break;
case INM_HELLOREC:
switch(n->state)
{
case NEIGHBOR_ATTEMPT:
case NEIGHBOR_DOWN:
neigh_chstate(n,NEIGHBOR_INIT);
default:
restart_inactim(n);
break;
}
break;
case INM_2WAYREC:
if(n->state==NEIGHBOR_INIT)
{
/* Can In build adjacency? */
neigh_chstate(n,NEIGHBOR_2WAY);
if(can_do_adj(n))
{
neigh_chstate(n,NEIGHBOR_EXSTART);
tryadj(n,p);
}
}
break;
case INM_NEGDONE:
if(n->state==NEIGHBOR_EXSTART)
{
neigh_chstate(n,NEIGHBOR_EXCHANGE);
s_init_list(&(n->lsrql));
n->lsrqh=ospf_top_new(n->ifa->proto);
s_init_list(&(n->lsrtl));
n->lsrth=ospf_top_new(n->ifa->proto);
s_init(&(n->dbsi), &(n->ifa->oa->lsal));
s_init(&(n->lsrqi), &(n->lsrql));
s_init(&(n->lsrti), &(n->lsrtl));
tm_start(n->lsrr_timer,n->ifa->rxmtint);
tm_start(n->ackd_timer,n->ifa->rxmtint/2);
}
else die("NEGDONE and I'm not in EXSTART?\n");
break;
case INM_EXDONE:
neigh_chstate(n,NEIGHBOR_LOADING);
/* FIXME Go on */
break;
case INM_LOADDONE:
neigh_chstate(n,NEIGHBOR_FULL);
break;
case INM_ADJOK:
switch(n->state)
{
case NEIGHBOR_2WAY:
/* Can In build adjacency? */
if(can_do_adj(n))
{
neigh_chstate(n,NEIGHBOR_EXSTART);
tryadj(n,p);
}
break;
default:
if(n->state>=NEIGHBOR_EXSTART)
if(!can_do_adj(n))
{
neigh_chstate(n,NEIGHBOR_2WAY);
/* FIXME Stop timers, kill database... */
}
break;
}
break;
case INM_SEQMIS:
case INM_BADLSREQ:
debug("%s: Bad LS req!\n", p->name);
if(n->state>=NEIGHBOR_EXCHANGE)
{
neigh_chstate(n,NEIGHBOR_EXSTART);
/* FIXME: Go on....*/
}
break;
case INM_KILLNBR:
case INM_LLDOWN:
case INM_INACTTIM:
neigh_chstate(n,NEIGHBOR_DOWN);
break;
case INM_1WAYREC:
if(n->state>=NEIGHBOR_2WAY)
{
neigh_chstate(n,NEIGHBOR_DOWN);
}
break;
default:
die("%s: INM - Unknown event?",p->name);
break;
}
}
void
bdr_election(struct ospf_iface *ifa, struct proto *p)
{
struct ospf_neighbor *neigh,*ndr,*nbdr,me;
u32 myid, ndrid, nbdrid;
int doadj;
p=(struct proto *)(ifa->proto);
DBG("%s: (B)DR election.\n",p->name);
myid=p->cf->global->router_id;
me.state=NEIGHBOR_2WAY;
me.rid=myid;
me.priority=ifa->priority;
me.dr=ifa->drid;
me.bdr=ifa->bdrid;
add_tail(&ifa->neigh_list, NODE &me);
nbdr=electbdr(ifa->neigh_list);
ndr=electdr(ifa->neigh_list);
if(ndr==NULL) ndr=nbdr;
if(((ifa->drid==myid) && (ndr->rid!=myid))
|| ((ifa->drid!=myid) && (ndr->rid==myid))
|| ((ifa->bdrid==myid) && (nbdr->rid!=myid))
|| ((ifa->bdrid!=myid) && (nbdr->rid==myid)))
{
if(ndr==NULL) ifa->drid=me.dr=0;
else ifa->drid=me.dr=ndr->rid;
if(nbdr==NULL) ifa->bdrid=me.bdr=0;
else ifa->bdrid=me.bdr=nbdr->rid;
nbdr=electbdr(ifa->neigh_list);
ndr=electdr(ifa->neigh_list);
}
if(ndr==NULL) ifa->drid=0;
if(ndr==NULL) ndrid=0;
else ndrid=ndr->rid;
if(nbdr==NULL) nbdrid=0;
else nbdrid=nbdr->rid;
doadj=0;
if((ifa->drid!=ndrid) || (ifa->bdrid!=nbdrid)) doadj=1;
ifa->drid=ndrid;
ifa->bdrid=nbdrid;
DBG("%s: DR=%I, BDR=%I\n",p->name, ifa->drid, ifa->bdrid);
if(myid==ifa->drid) iface_chstate(ifa, OSPF_IS_DR);
else
{
if(myid==ifa->bdrid) iface_chstate(ifa, OSPF_IS_BACKUP);
else iface_chstate(ifa, OSPF_IS_DROTHER);
}
rem_node(NODE &me);
if(doadj)
{
WALK_LIST (neigh, ifa->neigh_list)
{
ospf_neigh_sm(neigh, INM_ADJOK);
}
}
}
struct ospf_neighbor *
find_neigh(struct ospf_iface *ifa, u32 rid)
{
struct ospf_neighbor *n;
WALK_LIST (n, ifa->neigh_list)
if(n->rid == rid)
return n;
return NULL;
}
struct ospf_area *
ospf_find_area(struct proto_ospf *po, u32 aid)
{
struct ospf_area *oa;
WALK_LIST(NODE oa,po->area_list)
if(((struct ospf_area *)oa)->areaid==aid) return oa;
return NULL;
}
|