Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / dbdes.c @ eee9cefd

History | View | Annotate | Download (9.07 KB)

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 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
 */
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
  p=(struct proto *)(ifa->proto);
34

    
35
  switch(n->state)
36
  {
37
    case NEIGHBOR_EXSTART:                /* Send empty packets */
38
      n->myimms.bit.i=1;
39
      pkt=(struct ospf_dbdes_packet *)(ifa->ip_sk->tbuf);
40
      op=(struct ospf_packet *)pkt;
41
      fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
42
      pkt->iface_mtu=htons(ifa->iface->mtu);        /* FIXME NOT for VLINK! */
43
      pkt->options= ifa->options;
44
      pkt->imms=n->myimms;
45
      pkt->ddseq=htonl(n->dds);
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);
50
      OSPF_TRACE(D_PACKETS, "DB_DES (I) sent to %I via %s.", n->ip,
51
        ifa->iface->name);
52
      break;
53

    
54
    case NEIGHBOR_EXCHANGE:
55
      n->myimms.bit.i=0;
56

    
57
      if(oa->rt==NULL) originate_rt_lsa(oa);
58
      oa->origrt=0;
59

    
60
      if(((n->myimms.bit.ms) && (n->dds==n->ddr+1)) ||
61
         ((!(n->myimms.bit.ms)) && (n->dds==n->ddr)))
62
      {
63
        snode *sn;                        /* Send next */
64
        struct ospf_lsa_header *lsa;
65

    
66
        pkt=n->ldbdes;
67
        op=(struct ospf_packet *)pkt;
68
        
69
        fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
70
        pkt->iface_mtu=htons(ifa->iface->mtu);
71
        pkt->options= ifa->options;
72
        pkt->ddseq=htonl(n->dds);
73

    
74
        j=i=(ifa->iface->mtu-sizeof(struct ospf_dbdes_packet)-SIPH)/
75
                sizeof(struct ospf_lsa_header);        /* Number of lsaheaders */
76
        lsa=(n->ldbdes+sizeof(struct ospf_dbdes_packet));
77

    
78
        if(n->myimms.bit.m)
79
        {
80
          sn=s_get(&(n->dbsi));
81

    
82
          DBG("Number of LSA: %d\n", j);
83
          for(;i>0;i--)
84
          {
85
            struct top_hash_entry *en;
86
          
87
            en=(struct top_hash_entry *)sn;
88
            htonlsah(&(en->lsa), lsa);
89
            DBG("Working on: %d\n", i);
90
            DBG("\tX%01x %08I %08I %p\n", en->lsa.type, en->lsa.id,
91
              en->lsa.rt, en->lsa_body);
92

    
93
            if(sn==STAIL(n->ifa->oa->lsal))
94
            {
95
              i--;
96
              break;        /* Should set some flag? */
97
              }
98
            sn=sn->next;
99
            lsa++;
100
          }
101

    
102
          if(sn==STAIL(n->ifa->oa->lsal))
103
          {
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 */
107
          }
108
          else s_put(&(n->dbsi),sn);
109
        }
110

    
111
        pkt->imms.byte=n->myimms.byte;
112

    
113
        length=(j-i)*sizeof(struct ospf_lsa_header)+
114
                sizeof(struct ospf_dbdes_packet);
115
        op->length=htons(length);
116
        
117
        ospf_pkt_finalize(ifa, op);
118
        DBG("%s: DB_DES (M) sent to %I.\n", p->name, n->ip);
119
      }
120

    
121
    case NEIGHBOR_LOADING:
122
    case NEIGHBOR_FULL:
123
      aa=ifa->ip_sk->tbuf;
124
      bb=n->ldbdes;
125
      op=n->ldbdes;
126
      length=ntohs(op->length);
127

    
128
      for(i=0; i<length; i++)
129
      {
130
        *(aa+i)=*(bb+i);        /* Copy last sent packet again */
131
      }
132

    
133
      sk_send_to(ifa->ip_sk,length, n->ip, OSPF_PROTO);
134
      OSPF_TRACE(D_PACKETS, "DB_DES (M) sent to %I via %s.", n->ip,
135
        ifa->iface->name);
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;
148

    
149
    default:                                /* Ignore it */
150
      bug("Bug in dbdes sending");
151
      break;
152
  }
153
}
154

    
155
void
156
rxmt_timer_hook(timer *timer)
157
{
158
  struct ospf_iface *ifa;
159
  struct proto *p;
160
  struct ospf_neighbor *n;
161

    
162
  n=(struct ospf_neighbor *)timer->data;
163
  ifa=n->ifa;
164
  p=(struct proto *)(ifa->proto);
165
  DBG("%s: RXMT timer fired on interface %s for neigh: %I.\n",
166
    p->name, ifa->iface->name, n->ip);
167
  if(n->state<NEIGHBOR_LOADING) ospf_dbdes_tx(n);
168
  else
169
  {
170
    tm_stop(n->rxmt_timer);
171
  }
172
}
173

    
174
void
175
ospf_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;
181
  struct ospf_packet *op;
182
  int i,j;
183

    
184
  gr=n->ifa->oa->gr;
185
  op=(struct ospf_packet *)ps;
186

    
187
  plsa=(void *)(ps+1);
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
  {
194
    ntohlsah(plsa+i, &lsa);
195
    if(((he=ospf_hash_find(gr,lsa.id,lsa.rt,lsa.type))==NULL)||
196
      (lsa_comp(&lsa, &(he->lsa))==1))
197
    {
198
      /* Is this condition necessary? */
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
    }
206
  }
207
}
208

    
209
void
210
ospf_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
  {
223
    OSPF_TRACE(D_PACKETS, "Received dbdes from unknown neigbor! %I.",
224
      nrid);
225
    return ;
226
  }
227

    
228
  if(ifa->iface->mtu<size)
229
  {
230
    OSPF_TRACE(D_PACKETS, "Received dbdes larger than MTU from %I!", n->ip);
231
    return ;
232
  }
233

    
234
  OSPF_TRACE(D_PACKETS, "Received dbdes from %I via %s.", n->ip,
235
    ifa->iface->name);
236
  ospf_neigh_sm(n, INM_HELLOREC);
237

    
238
  switch(n->state)
239
  {
240
    case NEIGHBOR_DOWN:
241
    case NEIGHBOR_ATTEMPT:
242
    case NEIGHBOR_2WAY:
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:
249
        if((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
250
          && (n->rid > myrid) &&
251
          (size == sizeof(struct ospf_dbdes_packet)))
252
        {
253
          /* I'm slave! */
254
          n->dds=ntohl(ps->ddseq);
255
          n->ddr=ntohl(ps->ddseq);
256
          n->options=ps->options;
257
          n->myimms.bit.ms=0;
258
          n->imms.byte=ps->imms.byte;
259
          OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip);
260
          ospf_neigh_sm(n, INM_NEGDONE);
261
          tm_stop(n->rxmt_timer);
262
          ospf_dbdes_tx(n);
263
          break;
264
        }
265
        else
266
        {
267
          if(((ps->imms.bit.i==0) && (ps->imms.bit.ms==0)) &&
268
            (n->rid < myrid) && (n->dds == ntohl(ps->ddseq)))
269
          {
270
            /* I'm master! */
271
            n->options=ps->options;
272
            n->ddr=ntohl(ps->ddseq)-1;
273
            n->imms.byte=ps->imms.byte;
274
            OSPF_TRACE(D_PACKETS, "I'm master to %I.", nrid);
275
            ospf_neigh_sm(n, INM_NEGDONE);
276
          }
277
          else
278
          {
279
            DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip,
280
              ps->imms.byte);
281
            break;
282
          }
283
        }
284
        if(ps->imms.bit.i) break;
285
    case NEIGHBOR_EXCHANGE:
286
        if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
287
          (ntohl(ps->ddseq)==n->ddr))
288
        {
289
          /* Duplicate packet */
290
          OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
291
          if(n->imms.bit.ms==0)
292
          {
293
            ospf_dbdes_tx(n);
294
          }
295
          return;
296
        }
297

    
298
        n->ddr=ntohl(ps->ddseq);
299

    
300
        if(ps->imms.bit.ms!=n->imms.bit.ms) /* M/S bit differs */
301
        {
302
          DBG("SEQMIS-BIT-MS\n");
303
          ospf_neigh_sm(n, INM_SEQMIS);
304
          break;
305
        }
306

    
307
        if(ps->imms.bit.i)        /* I bit is set */
308
        {
309
          DBG("SEQMIS-BIT-I\n");
310
          ospf_neigh_sm(n, INM_SEQMIS);
311
          break;
312
        }
313

    
314
        n->imms.byte=ps->imms.byte;
315

    
316
        if(ps->options!=n->options)        /* Options differs */
317
        {
318
          DBG("SEQMIS-OPT\n");
319
          ospf_neigh_sm(n, INM_SEQMIS);
320
          break;
321
        }
322

    
323
        if(n->myimms.bit.ms)
324
        {
325
          if(ntohl(ps->ddseq)!=n->dds)                /* MASTER */
326
          {
327
            DBG("SEQMIS-MASTER\n");
328
            ospf_neigh_sm(n, INM_SEQMIS);
329
            break;
330
          }
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

    
343
        }
344
        else
345
        {
346
          if(ntohl(ps->ddseq)!=(n->dds+1))        /* SLAVE */
347
          {
348
            DBG("SEQMIS-SLAVE\n");
349
            ospf_neigh_sm(n, INM_SEQMIS);
350
            break;
351
          }
352
          n->ddr=ntohl(ps->ddseq);
353
          n->dds=ntohl(ps->ddseq);
354
          ospf_dbdes_reqladd(ps,p,n);
355
          ospf_dbdes_tx(n);
356
        }
357

    
358
      break;
359
    case NEIGHBOR_LOADING:
360
    case NEIGHBOR_FULL:
361
        if((ps->imms.byte==n->imms.byte) && (ps->options=n->options) &&
362
          (ps->ddseq==n->dds)) /* Only duplicate are accepted */
363
        {
364
          OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.",n->ip);
365
          return;
366
        }
367
        else
368
        {
369
          DBG("SEQMIS-FULL\n");
370
          ospf_neigh_sm(n, INM_SEQMIS);
371
        }
372
      break;
373
    defaut:
374
      bug("%s: Received dbdes from %I in undefined state.", p->name, n->ip);
375
      break;
376
   }
377
}
378