]> git.ipfire.org Git - thirdparty/bird.git/blame - proto/ospf/dbdes.c
Fix i_same for 'c' instruction.
[thirdparty/bird.git] / proto / ospf / dbdes.c
CommitLineData
4364b47e
OF
1/*
2 * BIRD -- OSPF
3 *
eee9cefd 4 * (c) 1999 - 2000 Ondrej Filip <feela@network.cz>
4364b47e
OF
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9#include "ospf.h"
10
eee9cefd
OF
11/**
12 * ospf_dbdes_tx - transmit database description packet
13 * @n: neighbor
14 *
15 * Sending of database description packet is described in 10.6 of RFC 2328.
16 * Reception of each packet is acknoledged in sequence number of another.
17 * When I send a packet to neighbor I keep a copy in buffer. If neighbor
18 * does not reply, I don't create new packet but I just send content
19 * of buffer.
20 */
4364b47e
OF
21void
22ospf_dbdes_tx(struct ospf_neighbor *n)
23{
24 struct ospf_dbdes_packet *pkt;
25 struct ospf_packet *op;
394920a0 26 struct ospf_iface *ifa=n->ifa;
38203d78 27 struct ospf_area *oa=ifa->oa;
4364b47e
OF
28 u16 length;
29 struct proto *p;
30147b89
OF
30 u16 i,j;
31 u8 *aa,*bb;
4364b47e 32
4364b47e
OF
33 p=(struct proto *)(ifa->proto);
34
35 switch(n->state)
36 {
37 case NEIGHBOR_EXSTART: /* Send empty packets */
96501dfe 38 n->myimms.bit.i=1;
963ea03d
OF
39 pkt=(struct ospf_dbdes_packet *)(ifa->ip_sk->tbuf);
40 op=(struct ospf_packet *)pkt;
89d6782d 41 fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
496c819f 42 pkt->iface_mtu=htons(ifa->iface->mtu); /* FIXME NOT for VLINK! */
4364b47e
OF
43 pkt->options= ifa->options;
44 pkt->imms=n->myimms;
910e557b 45 pkt->ddseq=htonl(n->dds);
4364b47e
OF
46 length=sizeof(struct ospf_dbdes_packet);
47 op->length=htons(length);
48 ospf_pkt_finalize(ifa, op);
49 sk_send_to(ifa->ip_sk,length, n->ip, OSPF_PROTO);
abcbfd04 50 OSPF_TRACE(D_PACKETS, "DB_DES (I) sent to %I via %s.", n->ip,
394920a0 51 ifa->iface->name);
30147b89
OF
52 break;
53
54 case NEIGHBOR_EXCHANGE:
96501dfe 55 n->myimms.bit.i=0;
5f743d96 56
38203d78
OF
57 if(oa->rt==NULL) originate_rt_lsa(oa);
58 oa->origrt=0;
59
910e557b
OF
60 if(((n->myimms.bit.ms) && (n->dds==n->ddr+1)) ||
61 ((!(n->myimms.bit.ms)) && (n->dds==n->ddr)))
30147b89
OF
62 {
63 snode *sn; /* Send next */
ce17d4c1 64 struct ospf_lsa_header *lsa;
963ea03d
OF
65
66 pkt=n->ldbdes;
67 op=(struct ospf_packet *)pkt;
30147b89 68
89d6782d 69 fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
963ea03d 70 pkt->iface_mtu=htons(ifa->iface->mtu);
30147b89 71 pkt->options= ifa->options;
910e557b 72 pkt->ddseq=htonl(n->dds);
30147b89 73
5f743d96 74 j=i=(ifa->iface->mtu-sizeof(struct ospf_dbdes_packet)-SIPH)/
ce17d4c1 75 sizeof(struct ospf_lsa_header); /* Number of lsaheaders */
30147b89
OF
76 lsa=(n->ldbdes+sizeof(struct ospf_dbdes_packet));
77
910e557b
OF
78 if(n->myimms.bit.m)
79 {
2560c886 80 sn=s_get(&(n->dbsi));
910e557b 81
2560c886
OF
82 DBG("Number of LSA: %d\n", j);
83 for(;i>0;i--)
84 {
85 struct top_hash_entry *en;
30147b89 86
2560c886
OF
87 en=(struct top_hash_entry *)sn;
88 htonlsah(&(en->lsa), lsa);
89 DBG("Working on: %d\n", i);
5f743d96 90 DBG("\tX%01x %08I %08I %p\n", en->lsa.type, en->lsa.id,
2560c886
OF
91 en->lsa.rt, en->lsa_body);
92
d8852b36 93 if(sn==STAIL(n->ifa->oa->lsal))
2560c886 94 {
5f743d96 95 i--;
0884f492 96 break; /* Should set some flag? */
2560c886
OF
97 }
98 sn=sn->next;
99 lsa++;
100 }
963ea03d 101
d8852b36 102 if(sn==STAIL(n->ifa->oa->lsal))
30147b89 103 {
2560c886
OF
104 DBG("Number of LSA NOT sent: %d\n", i);
105 DBG("M bit unset.\n");
106 n->myimms.bit.m=0; /* Unset more bit */
30147b89 107 }
5f743d96 108 else s_put(&(n->dbsi),sn);
910e557b 109 }
963ea03d 110
96501dfe 111 pkt->imms.byte=n->myimms.byte;
30147b89 112
963ea03d 113 length=(j-i)*sizeof(struct ospf_lsa_header)+
ce17d4c1 114 sizeof(struct ospf_dbdes_packet);
30147b89 115 op->length=htons(length);
96501dfe 116
30147b89 117 ospf_pkt_finalize(ifa, op);
394920a0 118 DBG("%s: DB_DES (M) sent to %I.\n", p->name, n->ip);
30147b89
OF
119 }
120
910e557b
OF
121 case NEIGHBOR_LOADING:
122 case NEIGHBOR_FULL:
30147b89
OF
123 aa=ifa->ip_sk->tbuf;
124 bb=n->ldbdes;
963ea03d 125 op=n->ldbdes;
30147b89
OF
126 length=ntohs(op->length);
127
5f743d96 128 for(i=0; i<length; i++)
30147b89 129 {
96501dfe 130 *(aa+i)=*(bb+i); /* Copy last sent packet again */
30147b89 131 }
04c6319a 132
30147b89 133 sk_send_to(ifa->ip_sk,length, n->ip, OSPF_PROTO);
abcbfd04 134 OSPF_TRACE(D_PACKETS, "DB_DES (M) sent to %I via %s.", n->ip,
394920a0 135 ifa->iface->name);
910e557b
OF
136 if(n->myimms.bit.ms) tm_start(n->rxmt_timer,ifa->rxmtint);
137 else
138 {
139 if((n->myimms.bit.m==0) && (n->imms.bit.m==0) &&
140 (n->state==NEIGHBOR_EXCHANGE))
141 {
142 ospf_neigh_sm(n, INM_EXDONE);
143 if(n->myimms.bit.ms) tm_stop(n->rxmt_timer);
144 else tm_start(n->rxmt_timer,ifa->rxmtint);
145 }
146 }
147 break;
4364b47e 148
4364b47e 149 default: /* Ignore it */
fef1badf 150 bug("Bug in dbdes sending");
4364b47e
OF
151 break;
152 }
153}
154
155void
156rxmt_timer_hook(timer *timer)
157{
158 struct ospf_iface *ifa;
159 struct proto *p;
160 struct ospf_neighbor *n;
161
910e557b
OF
162 n=(struct ospf_neighbor *)timer->data;
163 ifa=n->ifa;
4364b47e 164 p=(struct proto *)(ifa->proto);
9669362f 165 DBG("%s: RXMT timer fired on interface %s for neigh: %I.\n",
353f3261 166 p->name, ifa->iface->name, n->ip);
910e557b 167 if(n->state<NEIGHBOR_LOADING) ospf_dbdes_tx(n);
2560c886
OF
168 else
169 {
170 tm_stop(n->rxmt_timer);
2560c886 171 }
910e557b
OF
172}
173
910e557b
OF
174void
175ospf_dbdes_reqladd(struct ospf_dbdes_packet *ps, struct proto *p,
176 struct ospf_neighbor *n)
177{
178 struct ospf_lsa_header *plsa,lsa;
179 struct top_hash_entry *he,*sn;
180 struct top_graph *gr;
6d2b3211
OF
181 struct ospf_packet *op;
182 int i,j;
910e557b
OF
183
184 gr=n->ifa->oa->gr;
6d2b3211 185 op=(struct ospf_packet *)ps;
910e557b
OF
186
187 plsa=(void *)(ps+1);
6d2b3211
OF
188
189 j=(ntohs(op->length)-sizeof(struct ospf_dbdes_packet))/
190 sizeof( struct ospf_lsa_header);
191
192 for(i=0;i<j;i++)
193 {
95eb1dba 194 ntohlsah(plsa+i, &lsa);
95eb1dba
OF
195 if(((he=ospf_hash_find(gr,lsa.id,lsa.rt,lsa.type))==NULL)||
196 (lsa_comp(&lsa, &(he->lsa))==1))
197 {
394acced 198 /* Is this condition necessary? */
95eb1dba
OF
199 if(ospf_hash_find(n->lsrqh,lsa.id,lsa.rt,lsa.type)==NULL)
200 {
201 sn=ospf_hash_get(n->lsrqh,lsa.id,lsa.rt,lsa.type);
202 ntohlsah(plsa+i, &(sn->lsa));
203 s_add_tail(&(n->lsrql), SNODE sn);
204 }
205 }
6d2b3211 206 }
4364b47e
OF
207}
208
209void
210ospf_dbdes_rx(struct ospf_dbdes_packet *ps, struct proto *p,
211 struct ospf_iface *ifa, u16 size)
212{
213 u32 nrid, myrid;
214 struct ospf_neighbor *n;
215 u8 i;
216
217 nrid=ntohl(((struct ospf_packet *)ps)->routerid);
218
219 myrid=p->cf->global->router_id;
220
221 if((n=find_neigh(ifa, nrid))==NULL)
222 {
abcbfd04 223 OSPF_TRACE(D_PACKETS, "Received dbdes from unknown neigbor! %I.",
4364b47e
OF
224 nrid);
225 return ;
226 }
227
228 if(ifa->iface->mtu<size)
229 {
abcbfd04 230 OSPF_TRACE(D_PACKETS, "Received dbdes larger than MTU from %I!", n->ip);
4364b47e
OF
231 return ;
232 }
233
abcbfd04 234 OSPF_TRACE(D_PACKETS, "Received dbdes from %I via %s.", n->ip,
394920a0 235 ifa->iface->name);
4bb9ce56 236 ospf_neigh_sm(n, INM_HELLOREC);
353f3261 237
4364b47e
OF
238 switch(n->state)
239 {
240 case NEIGHBOR_DOWN:
241 case NEIGHBOR_ATTEMPT:
242 case NEIGHBOR_2WAY:
4364b47e
OF
243 return;
244 break;
245 case NEIGHBOR_INIT:
246 ospf_neigh_sm(n, INM_2WAYREC);
247 if(n->state!=NEIGHBOR_EXSTART) return;
248 case NEIGHBOR_EXSTART:
04c6319a
OF
249 if((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
250 && (n->rid > myrid) &&
4364b47e
OF
251 (size == sizeof(struct ospf_dbdes_packet)))
252 {
253 /* I'm slave! */
910e557b
OF
254 n->dds=ntohl(ps->ddseq);
255 n->ddr=ntohl(ps->ddseq);
4364b47e 256 n->options=ps->options;
04c6319a 257 n->myimms.bit.ms=0;
04c6319a 258 n->imms.byte=ps->imms.byte;
abcbfd04 259 OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip);
4364b47e 260 ospf_neigh_sm(n, INM_NEGDONE);
910e557b
OF
261 tm_stop(n->rxmt_timer);
262 ospf_dbdes_tx(n);
263 break;
4364b47e
OF
264 }
265 else
266 {
04c6319a 267 if(((ps->imms.bit.i==0) && (ps->imms.bit.ms==0)) &&
910e557b 268 (n->rid < myrid) && (n->dds == ntohl(ps->ddseq)))
4364b47e
OF
269 {
270 /* I'm master! */
271 n->options=ps->options;
910e557b 272 n->ddr=ntohl(ps->ddseq)-1;
04c6319a 273 n->imms.byte=ps->imms.byte;
abcbfd04 274 OSPF_TRACE(D_PACKETS, "I'm master to %I.", nrid);
4364b47e
OF
275 ospf_neigh_sm(n, INM_NEGDONE);
276 }
277 else
278 {
353f3261 279 DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip,
910e557b 280 ps->imms.byte);
4364b47e
OF
281 break;
282 }
283 }
394920a0 284 if(ps->imms.bit.i) break;
4364b47e 285 case NEIGHBOR_EXCHANGE:
04c6319a 286 if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
910e557b 287 (ntohl(ps->ddseq)==n->ddr))
4364b47e
OF
288 {
289 /* Duplicate packet */
abcbfd04 290 OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
04c6319a 291 if(n->imms.bit.ms==0)
4364b47e
OF
292 {
293 ospf_dbdes_tx(n);
294 }
295 return;
296 }
297
910e557b
OF
298 n->ddr=ntohl(ps->ddseq);
299
300 if(ps->imms.bit.ms!=n->imms.bit.ms) /* M/S bit differs */
4364b47e 301 {
bd37f45c 302 DBG("SEQMIS-BIT-MS\n");
4364b47e
OF
303 ospf_neigh_sm(n, INM_SEQMIS);
304 break;
305 }
306
04c6319a 307 if(ps->imms.bit.i) /* I bit is set */
4364b47e 308 {
bd37f45c 309 DBG("SEQMIS-BIT-I\n");
4364b47e
OF
310 ospf_neigh_sm(n, INM_SEQMIS);
311 break;
312 }
313
2560c886
OF
314 n->imms.byte=ps->imms.byte;
315
4364b47e
OF
316 if(ps->options!=n->options) /* Options differs */
317 {
bd37f45c 318 DBG("SEQMIS-OPT\n");
4364b47e
OF
319 ospf_neigh_sm(n, INM_SEQMIS);
320 break;
321 }
322
04c6319a 323 if(n->myimms.bit.ms)
4364b47e 324 {
910e557b 325 if(ntohl(ps->ddseq)!=n->dds) /* MASTER */
4364b47e 326 {
bd37f45c 327 DBG("SEQMIS-MASTER\n");
4364b47e
OF
328 ospf_neigh_sm(n, INM_SEQMIS);
329 break;
330 }
910e557b
OF
331 n->dds++;
332 DBG("Incrementing dds\n");
333 ospf_dbdes_reqladd(ps,p,n);
334 if((n->myimms.bit.m==0) && (ps->imms.bit.m==0))
335 {
336 ospf_neigh_sm(n, INM_EXDONE);
337 }
338 else
339 {
340 ospf_dbdes_tx(n);
341 }
342
4364b47e
OF
343 }
344 else
345 {
2560c886 346 if(ntohl(ps->ddseq)!=(n->dds+1)) /* SLAVE */
4364b47e 347 {
bd37f45c 348 DBG("SEQMIS-SLAVE\n");
4364b47e
OF
349 ospf_neigh_sm(n, INM_SEQMIS);
350 break;
351 }
2560c886
OF
352 n->ddr=ntohl(ps->ddseq);
353 n->dds=ntohl(ps->ddseq);
910e557b
OF
354 ospf_dbdes_reqladd(ps,p,n);
355 ospf_dbdes_tx(n);
4364b47e
OF
356 }
357
4364b47e
OF
358 break;
359 case NEIGHBOR_LOADING:
360 case NEIGHBOR_FULL:
04c6319a 361 if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
4364b47e
OF
362 (ps->ddseq==n->dds)) /* Only duplicate are accepted */
363 {
abcbfd04 364 OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.",n->ip);
4364b47e
OF
365 return;
366 }
367 else
368 {
bd37f45c 369 DBG("SEQMIS-FULL\n");
4364b47e
OF
370 ospf_neigh_sm(n, INM_SEQMIS);
371 }
372 break;
373 defaut:
fef1badf 374 bug("%s: Received dbdes from %I in undefined state.", p->name, n->ip);
4364b47e
OF
375 break;
376 }
377}
378