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