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
|
/*
* BIRD -- OSPF
*
* (c) 1999-2000 Ondrej Filip <feela@network.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "ospf.h"
void
htonlsah(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
{
n->age=htons(h->age);
n->options=h->options;
n->type=h->type;
n->id=htonl(h->id);
n->rt=htonl(h->rt);
n->sn=htonl(h->sn);
n->checksum=htons(h->checksum);
n->length=htons(h->length);
};
void
ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
{
h->age=ntohs(n->age);
h->options=n->options;
h->type=n->type;
h->id=ntohl(n->id);
h->rt=ntohl(n->rt);
h->sn=ntohl(n->sn);
h->checksum=ntohs(n->checksum);
h->length=ntohs(n->length);
};
void
ospf_dbdes_tx(struct ospf_neighbor *n)
{
struct ospf_dbdes_packet *pkt;
struct ospf_packet *op;
struct ospf_iface *ifa;
u16 length;
struct proto *p;
u16 i,j;
u8 *aa,*bb;
ifa=n->ifa;
p=(struct proto *)(ifa->proto);
switch(n->state)
{
case NEIGHBOR_EXSTART: /* Send empty packets */
n->myimms.bit.i=1;
pkt=(struct ospf_dbdes_packet *)(ifa->ip_sk->tbuf);
op=(struct ospf_packet *)pkt;
fill_ospf_pkt_hdr(ifa, pkt, DBDES);
pkt->iface_mtu=htons(ifa->iface->mtu); /*FIXME NOT for VLINK! */
pkt->options= ifa->options;
pkt->imms=n->myimms;
pkt->ddseq=htonl(n->dds);
length=sizeof(struct ospf_dbdes_packet);
op->length=htons(length);
ospf_pkt_finalize(ifa, op);
sk_send_to(ifa->ip_sk,length, n->ip, OSPF_PROTO);
debug("%s: DB_DES (I) sent for %u.\n", p->name, n->rid);
break;
case NEIGHBOR_EXCHANGE:
n->myimms.bit.i=0;
if(((n->myimms.bit.ms) && (n->dds==n->ddr+1)) ||
((!(n->myimms.bit.ms)) && (n->dds==n->ddr)))
{
snode *sn; /* Send next */
struct ospf_lsa_header *lsa;
pkt=n->ldbdes;
op=(struct ospf_packet *)pkt;
fill_ospf_pkt_hdr(ifa, pkt, DBDES);
pkt->iface_mtu=htons(ifa->iface->mtu);
pkt->options= ifa->options;
pkt->ddseq=htonl(n->dds);
j=i=(ifa->iface->mtu-sizeof(struct ospf_dbdes_packet))/
sizeof(struct ospf_lsa_header); /* Number of lsaheaders */
lsa=(n->ldbdes+sizeof(struct ospf_dbdes_packet));
if(n->myimms.bit.m)
{
sn=s_get(&(n->dbsi));
DBG("Number of LSA: %d\n", j);
for(;i>0;i--)
{
struct top_hash_entry *en;
en=(struct top_hash_entry *)sn;
htonlsah(&(en->lsa), lsa);
DBG("Working on: %d\n", i);
debug("\t%04x %08x %08x %p\n", en->lsa.type, en->lsa.id,
en->lsa.rt, en->lsa_body);
if(sn->next==NULL)
{
break; /* Should set some flag? */
}
sn=sn->next;
lsa++;
}
i--;
if(sn->next==NULL)
{
DBG("Number of LSA NOT sent: %d\n", i);
DBG("M bit unset.\n");
n->myimms.bit.m=0; /* Unset more bit */
}
s_put(&(n->dbsi),sn);
}
pkt->imms.byte=n->myimms.byte;
length=(j-i)*sizeof(struct ospf_lsa_header)+
sizeof(struct ospf_dbdes_packet);
op->length=htons(length);
ospf_pkt_finalize(ifa, op);
}
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
aa=ifa->ip_sk->tbuf;
bb=n->ldbdes;
op=n->ldbdes;
length=ntohs(op->length);
for(i=0; i<ifa->iface->mtu; i++)
{
*(aa+i)=*(bb+i); /* Copy last sent packet again */
}
{
u8 ii;
u8 *jj=ifa->ip_sk->tbuf;
for(ii=0;ii<length;ii+=4)
{
DBG("Out dump: %d,%d,%d,%d\n", *(jj+ii), *(jj+ii+1), *(jj+ii+2), *(jj+ii+3));
}
}
sk_send_to(ifa->ip_sk,length, n->ip, OSPF_PROTO);
debug("%s: DB_DES sent for %u.\n", p->name, n->rid);
if(n->myimms.bit.ms) tm_start(n->rxmt_timer,ifa->rxmtint);
else
{
if((n->myimms.bit.m==0) && (n->imms.bit.m==0) &&
(n->state==NEIGHBOR_EXCHANGE))
{
ospf_neigh_sm(n, INM_EXDONE);
if(n->myimms.bit.ms) tm_stop(n->rxmt_timer);
else tm_start(n->rxmt_timer,ifa->rxmtint);
}
}
break;
default: /* Ignore it */
die("Bug in dbdes sending\n");
break;
}
}
void
rxmt_timer_hook(timer *timer)
{
struct ospf_iface *ifa;
struct proto *p;
struct ospf_neighbor *n;
n=(struct ospf_neighbor *)timer->data;
ifa=n->ifa;
p=(struct proto *)(ifa->proto);
debug("%s: RXMT timer fired on interface %s for neigh: %u.\n",
p->name, ifa->iface->name, n->rid);
if(n->state<NEIGHBOR_LOADING) ospf_dbdes_tx(n);
else
{
tm_stop(n->rxmt_timer);
/* FIXME I should dealloc ldbdes */
}
}
int
lsa_comp(struct ospf_lsa_header *l1, struct ospf_lsa_header *l2)
/*
* 1 l1 is newer
* 0 identical
* -1 l2 is newer
*/
{
if(l1->sn<l2->sn) return 1;
if(l1->sn==l2->sn)
{
if(l1->checksum=!l2->checksum) return l1->checksum<l2->checksum ? -1 :1;
if(l1->age==MAXAGE) return 1;
if(l2->age==MAXAGE) return -1;
if(abs(l1->age-l2->age)>MAXAGEDIFF) return l1->age<l2->age ? 1 : -1;
}
return 0;
}
void
ospf_dbdes_reqladd(struct ospf_dbdes_packet *ps, struct proto *p,
struct ospf_neighbor *n)
{
struct ospf_lsa_header *plsa,lsa;
struct top_hash_entry *he,*sn;
struct top_graph *gr;
struct ospf_packet *op;
int i,j;
gr=n->ifa->oa->gr;
op=(struct ospf_packet *)ps;
plsa=(void *)(ps+1);
j=(ntohs(op->length)-sizeof(struct ospf_dbdes_packet))/
sizeof( struct ospf_lsa_header);
for(i=0;i<j;i++)
{
ntohlsah(plsa+i, &lsa);
/* FIXME Test Checksum */
if(((he=ospf_hash_find(gr,lsa.id,lsa.rt,lsa.type))==NULL)||
(lsa_comp(&lsa, &(he->lsa))==1))
{
sn=sl_alloc(gr->hash_slab);
ntohlsah(plsa+i, &(sn->lsa));
s_add_tail(&(n->lsrql), SNODE sn);
}
}
}
void
ospf_dbdes_rx(struct ospf_dbdes_packet *ps, struct proto *p,
struct ospf_iface *ifa, u16 size)
{
u32 nrid, myrid;
struct ospf_neighbor *n;
u8 i;
nrid=ntohl(((struct ospf_packet *)ps)->routerid);
myrid=p->cf->global->router_id;
if((n=find_neigh(ifa, nrid))==NULL)
{
debug("%s: Received dbdes from unknown neigbor! (%u)\n", p->name,
nrid);
return ;
}
if(ifa->iface->mtu<size)
{
debug("%s: Received dbdes larger than MTU from (%u)!\n", p->name, nrid);
return ;
}
switch(n->state)
{
case NEIGHBOR_DOWN:
case NEIGHBOR_ATTEMPT:
case NEIGHBOR_2WAY:
debug("%s: Received dbdes from %u in bad state. (%u)\n", p->name, nrid);
return;
break;
case NEIGHBOR_INIT:
ospf_neigh_sm(n, INM_2WAYREC);
if(n->state!=NEIGHBOR_EXSTART) return;
case NEIGHBOR_EXSTART:
if((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
&& (n->rid > myrid) &&
(size == sizeof(struct ospf_dbdes_packet)))
{
/* I'm slave! */
n->dds=ntohl(ps->ddseq);
n->ddr=ntohl(ps->ddseq);
n->options=ps->options;
n->myimms.bit.ms=0;
n->imms.byte=ps->imms.byte;
debug("%s: I'm slave to %u. \n", p->name, nrid);
ospf_neigh_sm(n, INM_NEGDONE);
tm_stop(n->rxmt_timer);
ospf_dbdes_tx(n);
break;
}
else
{
if(((ps->imms.bit.i==0) && (ps->imms.bit.ms==0)) &&
(n->rid < myrid) && (n->dds == ntohl(ps->ddseq)))
{
/* I'm master! */
n->options=ps->options;
n->ddr=ntohl(ps->ddseq)-1;
n->imms.byte=ps->imms.byte;
debug("%s: I'm master to %u. \n", p->name, nrid);
ospf_neigh_sm(n, INM_NEGDONE);
}
else
{
debug("%s: Nothing happend to %u (imms=%u)", p->name, nrid,
ps->imms.byte);
break;
}
}
//break; /* I should probably continue processing packet */
case NEIGHBOR_EXCHANGE:
if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
(ntohl(ps->ddseq)==n->ddr))
{
/* Duplicate packet */
debug("%s: Received duplicate dbdes from (%u)!\n", p->name, nrid);
if(n->imms.bit.ms==0)
{
ospf_dbdes_tx(n);
}
return;
}
n->ddr=ntohl(ps->ddseq);
if(ps->imms.bit.ms!=n->imms.bit.ms) /* M/S bit differs */
{
DBG("SEQMIS-BIT-MS\n");
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
if(ps->imms.bit.i) /* I bit is set */
{
DBG("SEQMIS-BIT-I\n");
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
n->imms.byte=ps->imms.byte;
if(ps->options!=n->options) /* Options differs */
{
DBG("SEQMIS-OPT\n");
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
if(n->myimms.bit.ms)
{
if(ntohl(ps->ddseq)!=n->dds) /* MASTER */
{
DBG("SEQMIS-MASTER\n");
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
n->dds++;
DBG("Incrementing dds\n");
ospf_dbdes_reqladd(ps,p,n);
if((n->myimms.bit.m==0) && (ps->imms.bit.m==0))
{
ospf_neigh_sm(n, INM_EXDONE);
}
else
{
ospf_dbdes_tx(n);
}
}
else
{
if(ntohl(ps->ddseq)!=(n->dds+1)) /* SLAVE */
{
DBG("SEQMIS-SLAVE\n");
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
n->ddr=ntohl(ps->ddseq);
n->dds=ntohl(ps->ddseq);
ospf_dbdes_reqladd(ps,p,n);
ospf_dbdes_tx(n);
}
break;
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
(ps->ddseq==n->dds)) /* Only duplicate are accepted */
{
debug("%s: Received duplicate dbdes from (%u)!\n", p->name, nrid);
return;
}
else
{
ospf_neigh_sm(n, INM_SEQMIS);
}
break;
defaut:
die("%s: Received dbdes from %u in unknown state. (%u)\n", p->name, nrid);
break;
}
}
|