Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ 035f6acb

History | View | Annotate | Download (13.2 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 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
int
12
flood_lsa(struct ospf_neighbor *n, struct ospf_lsa_header *hn,
13
  struct ospf_lsa_header *hh, struct proto_ospf *po, struct ospf_iface *iff,
14
  struct ospf_area *oa, int rtl)
15
{
16
  struct ospf_iface *ifa;
17
  struct ospf_neighbor *nn;
18
  struct top_hash_entry *en;
19
  struct proto *p=&po->proto;
20
  int ret,retval=0;
21

    
22
  /* pg 148 */
23
  WALK_LIST(NODE ifa,po->iface_list)
24
  {
25
    if(ifa->stub) continue;
26

    
27
    if(hh->type==LSA_T_EXT)
28
    {
29
      if(ifa->type==OSPF_IT_VLINK) continue;
30
      if(ifa->oa->stub) continue;
31
    }
32
    else
33
    {
34
      if(oa->areaid==BACKBONE)
35
      {
36
        if((ifa->type!=OSPF_IT_VLINK)&&(ifa->oa!=oa)) continue;
37
      }
38
      else
39
      {
40
        if(ifa->oa!=oa) continue;
41
      }
42
    }
43
    ret=0;
44
    WALK_LIST(NODE nn, ifa->neigh_list)
45
    {
46
      if(nn->state<NEIGHBOR_EXCHANGE) continue;
47
      if(nn->state<NEIGHBOR_FULL)
48
      {
49
        if((en=ospf_hash_find_header(nn->lsrqh,hh))!=NULL)
50
        {
51
          switch(lsa_comp(hh,&en->lsa))
52
          {
53
            case CMP_OLDER:
54
              continue;
55
              break;
56
            case CMP_SAME:
57
              s_rem_node(SNODE en);
58
              if(en->lsa_body!=NULL) mb_free(en->lsa_body);
59
              en->lsa_body=NULL;
60
              DBG("Removing from lsreq list for neigh %I\n", nn->rid);
61
              ospf_hash_delete(nn->lsrqh,en);
62
              if(EMPTY_SLIST(nn->lsrql)) ospf_neigh_sm(nn, INM_LOADDONE);
63
              continue;
64
              break;
65
            case CMP_NEWER:
66
              s_rem_node(SNODE en);
67
              if(en->lsa_body!=NULL) mb_free(en->lsa_body);
68
              en->lsa_body=NULL;
69
              DBG("Removing from lsreq list for neigh %I\n", nn->rid);
70
              ospf_hash_delete(nn->lsrqh,en);
71
              if(EMPTY_SLIST(nn->lsrql)) ospf_neigh_sm(nn, INM_LOADDONE);
72
              break;
73
            default: bug("Bug in lsa_comp?");
74
          }
75
        }
76
      }
77
      if(nn==n) continue;
78
      if(rtl!=0)
79
      {
80
        if((en=ospf_hash_find_header(nn->lsrth, hh))==NULL)
81
        {
82
          en=ospf_hash_get_header(nn->lsrth, hh);
83
        }
84
        else
85
        {
86
          s_rem_node(SNODE en);
87
        }
88
        s_add_tail(&nn->lsrtl, SNODE en);
89
        memcpy(&en->lsa,hh,sizeof(struct ospf_lsa_header));
90
        DBG("Adding LSA lsrt RT: %I, Id: %I, Type: %u for n: %I\n",
91
          en->lsa.rt,en->lsa.id, en->lsa.type, nn->ip);
92
      }
93
      else
94
      {
95
        if((en=ospf_hash_find_header(nn->lsrth, hh))!=NULL)
96
        {
97
          s_rem_node(SNODE en);
98
          if(en->lsa_body!=NULL) mb_free(en->lsa_body);
99
          en->lsa_body=NULL;
100
          ospf_hash_delete(nn->lsrth, en);
101
        }
102
      }
103
      ret=1;
104
    }
105

    
106
    if(ret==0) continue;
107

    
108
    if(ifa==iff)
109
    {
110
      if((n->rid==iff->drid)||n->rid==iff->bdrid) continue;
111
      if(iff->state==OSPF_IS_BACKUP) continue;
112
      retval=1;
113
    }
114

    
115
    {
116
      sock *sk;
117
      ip_addr to;
118
      u16 len;
119
      struct ospf_lsupd_packet *pk;
120
      struct ospf_packet *op;
121

    
122
      if(ifa->type==OSPF_IT_NBMA)  sk=ifa->ip_sk;
123
      else sk=ifa->hello_sk;        /* FIXME is this true for PTP? */
124

    
125
      pk=(struct ospf_lsupd_packet *)sk->tbuf;
126
      op=(struct ospf_packet *)sk->tbuf;
127

    
128
      fill_ospf_pkt_hdr(ifa, pk, LSUPD_P);
129
      pk->lsano=htonl(1);
130
      if(hn!=NULL)
131
      {
132
        memcpy(pk+1,hn,ntohs(hn->length));
133
        len=sizeof(struct ospf_lsupd_packet)+ntohs(hn->length);
134
      }
135
      else
136
      {
137
        u8 *help;
138
        struct top_hash_entry *en;
139
        struct ospf_lsa_header *lh;
140
        u16 age;
141

    
142
        lh=(struct ospf_lsa_header *)(pk+1);
143
        htonlsah(hh,lh);
144
        age=hh->age;
145
        age+=ifa->inftransdelay;
146
        if(age>LSA_MAXAGE) age=LSA_MAXAGE;
147
        lh->age=htons(age);
148
        help=(u8 *)(lh+1);
149
        en=ospf_hash_find_header(oa->gr,hh);
150
        htonlsab(en->lsa_body,help,hh->type,hh->length
151
          -sizeof(struct ospf_lsa_header));
152
        len=hh->length+sizeof(struct ospf_lsupd_packet);
153
      }
154
      op->length=htons(len);
155
      ospf_pkt_finalize(ifa, op);
156
      OSPF_TRACE(D_PACKETS, "LS upd flooded via %s", ifa->iface->name);
157

    
158
      if(ifa->type==OSPF_IT_NBMA)
159
      {
160
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR))
161
          sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
162
        else  sk_send_to_bdr(sk ,len, ifa);
163
      }
164
      else
165
      {
166
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR)||
167
          (ifa->type==OSPF_IT_PTP))
168
          sk_send_to(sk,len, AllSPFRouters, OSPF_PROTO);
169
        else sk_send_to(sk,len, AllDRouters, OSPF_PROTO);
170
      }
171
    }
172
  }
173
  return retval;
174
}
175

    
176
void                /* I send all I received in LSREQ */
177
ospf_lsupd_tx_list(struct ospf_neighbor *n, list *l)
178
{
179
  struct l_lsr_head *llsh;
180
  u16 len;
181
  u32 lsano;
182
  struct top_hash_entry *en;
183
  struct ospf_lsupd_packet *pk;
184
  struct ospf_packet *op;
185
  struct proto *p=&n->ifa->oa->po->proto;
186
  void *pktpos;
187
  u8 ii;
188
  u8 *jj=n->ifa->ip_sk->tbuf;
189

    
190
  if(EMPTY_LIST(*l)) return;
191

    
192
  pk=(struct ospf_lsupd_packet *)n->ifa->ip_sk->tbuf;
193
  op=(struct ospf_packet *)n->ifa->ip_sk->tbuf;
194

    
195
  DBG("LSupd: 1st packet\n");
196
       
197
  fill_ospf_pkt_hdr(n->ifa, pk, LSUPD_P);
198
  len=SIPH+sizeof(struct ospf_lsupd_packet);
199
  lsano=0;
200
  pktpos=(pk+1);
201

    
202
  WALK_LIST(llsh, *l)
203
  {
204
    if((en=ospf_hash_find(n->ifa->oa->gr,llsh->lsh.id,llsh->lsh.rt,
205
      llsh->lsh.type))==NULL) continue;                /* Probably flushed LSA */
206

    
207
    DBG("Sending ID=%I, Type=%u, RT=%I Sn: 0x%x Age: %u\n",
208
      llsh->lsh.id, llsh->lsh.type, llsh->lsh.rt, en->lsa.sn, en->lsa.age);
209
    if(((u32)(len+en->lsa.length))>n->ifa->iface->mtu)
210
    {
211
      pk->lsano=htonl(lsano);
212
      op->length=htons(len-SIPH);
213
      ospf_pkt_finalize(n->ifa, op);
214
                       
215
      sk_send_to(n->ifa->ip_sk,len-SIPH, n->ip, OSPF_PROTO);
216
      OSPF_TRACE(D_PACKETS, "LS upd sent to %I (%d LSAs)", n->ip, lsano);
217

    
218
      DBG("LSupd: next packet\n");
219
      fill_ospf_pkt_hdr(n->ifa, pk, LSUPD_P);
220
      len=SIPH+sizeof(struct ospf_lsupd_packet);
221
      lsano=0;
222
      pktpos=(pk+1);
223
    }
224
    htonlsah(&(en->lsa), pktpos);
225
    pktpos=pktpos+sizeof(struct ospf_lsa_header);
226
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
227
      -sizeof(struct ospf_lsa_header));
228
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
229
    len+=en->lsa.length;
230
    lsano++;
231
  }
232
  if(lsano>0)
233
  {
234
    pk->lsano=htonl(lsano);
235
    op->length=htons(len-SIPH);
236
    ospf_pkt_finalize(n->ifa, op);
237

    
238
    OSPF_TRACE(D_PACKETS, "LS upd sent to %I (%d LSAs)", n->ip, lsano);
239
    sk_send_to(n->ifa->ip_sk,len-SIPH, n->ip, OSPF_PROTO);
240
  }
241
}
242

    
243
void
244
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
245
  struct ospf_iface *ifa, u16 size)
246
{
247
  u32 area,nrid,myrid;
248
  struct ospf_neighbor *n,*ntmp;
249
  struct ospf_lsa_header *lsa;
250
  struct ospf_area *oa;
251
  struct proto_ospf *po=(struct proto_ospf *)p;
252
  u16 length;
253
  u8 i;
254

    
255
  nrid=ntohl(ps->ospf_packet.routerid);
256

    
257
  myrid=p->cf->global->router_id;
258

    
259

    
260
  if((n=find_neigh(ifa, nrid))==NULL)
261
  {
262
    OSPF_TRACE(D_PACKETS, "Received lsupd from unknown neighbor! (%I)",
263
      nrid);
264
    return ;
265
  }
266
  if(n->state<NEIGHBOR_EXCHANGE)
267
  {
268
    OSPF_TRACE(D_PACKETS,"Received lsupd in lesser state than EXCHANGE from (%I)",
269
      n->ip);
270
    return;
271
  }
272
  if(size<=(sizeof(struct ospf_lsupd_packet)+sizeof(struct ospf_lsa_header)))
273
  {
274
    OSPF_TRACE(D_PACKETS, "Received lsupd from %I is too short!", n->ip);
275
    return;
276
  }
277

    
278
  OSPF_TRACE(D_PACKETS, "Received LS upd from %I", n->ip);
279
  ospf_neigh_sm(n, INM_HELLOREC);
280

    
281
  lsa=(struct ospf_lsa_header *)(ps+1);
282
  area=htonl(ps->ospf_packet.areaid);
283
  oa=ospf_find_area((struct proto_ospf *)p,area);
284
  for(i=0;i<ntohl(ps->lsano);i++,
285
    lsa=(struct ospf_lsa_header *)(((u8 *)lsa)+ntohs(lsa->length)))
286
  {
287
    struct ospf_lsa_header lsatmp;
288
    struct top_hash_entry *lsadb;
289
    u16 lenn;
290
    int diff=((u8 *)lsa)-((u8 *)ps);
291
    u16 chsum;
292

    
293
    if(((diff+sizeof(struct ospf_lsa_header))>=size) ||
294
      ((ntohs(lsa->length)+diff)>size))
295
      log("%s: Received lsupd from %I is too short.", p->name,n->ip);
296

    
297
    lenn=ntohs(lsa->length);
298

    
299
    if((lenn<=sizeof(struct ospf_lsa_header))||(lenn!=(4*(lenn/4))))
300
    {
301
      log("Received LSA with bad length");
302
      ospf_neigh_sm(n,INM_BADLSREQ);
303
      break;
304
    }
305
    /* pg 143 (1) */
306
    chsum=lsa->checksum;
307
    if(chsum!=lsasum_check(lsa,NULL,po))
308
    {
309
      log("Received bad lsa checksum from %I",n->rid);
310
      continue;
311
    }
312
    /* pg 143 (2) */
313
    if((lsa->type<LSA_T_RT)||(lsa->type>LSA_T_EXT))
314
    {
315
      log("Unknown LSA type from %I",n->rid);
316
      continue;
317
    }
318
    /* pg 143 (3) */
319
    if((lsa->type==LSA_T_EXT)&&oa->stub)
320
    {
321
      log("Received External LSA in stub area from %I",n->rid);
322
      continue;
323
    }
324
    ntohlsah(lsa,&lsatmp);
325
    DBG("Update Type: %u ID: %I RT: %I, Sn: 0x%08x Age: %u, Sum: %u\n",
326
      lsatmp.type, lsatmp.id, lsatmp.rt, lsatmp.sn, lsatmp.age,
327
      lsatmp.checksum);
328
    lsadb=ospf_hash_find_header(oa->gr, &lsatmp);
329
    if(lsadb)
330
      DBG("I have Type: %u ID: %I RT: %I, Sn: 0x%08x Age: %u, Sum: %u\n",
331
      lsadb->lsa.type, lsadb->lsa.id, lsadb->lsa.rt, lsadb->lsa.sn,
332
      lsadb->lsa.age, lsadb->lsa.checksum);
333

    
334
    /* pg 143 (4) */
335
    if((lsatmp.age==LSA_MAXAGE)&&(lsadb==NULL))
336
    {
337
      int flag=0;
338
      struct ospf_iface *ifatmp;
339

    
340
      if(can_flush_lsa(oa))
341
      {
342
        ospf_lsack_direct_tx(n,lsa);
343
        continue;
344
      }
345
    }
346
    /* pg 144 (5) */
347
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
348
    {
349
       struct ospf_iface *ift=NULL;
350
       void *body;
351
       struct ospf_iface *nifa;
352
       int self=0;
353

    
354
       DBG("PG143(5): Received LSA is newer\n");
355

    
356
       if(lsatmp.rt==p->cf->global->router_id) self=1;
357

    
358
       if(lsatmp.type==LSA_T_NET)
359
       {
360
         WALK_LIST(nifa,po->iface_list)
361
         {
362
           if(ipa_compare(nifa->iface->addr->ip,ipa_from_u32(lsatmp.id))==0)
363
           {
364
             self=1;
365
             break;
366
           }
367
         }
368
       }
369

    
370
       if(self)
371
       {
372
         struct top_hash_entry *en;
373

    
374
         if((lsatmp.age==LSA_MAXAGE)&&(lsatmp.sn==LSA_MAXSEQNO))
375
         {
376
           ospf_lsack_direct_tx(n,lsa);
377
           continue;
378
         }
379

    
380
         lsatmp.age=LSA_MAXAGE;
381
         lsatmp.sn=LSA_MAXSEQNO;
382
         lsa->age=htons(LSA_MAXAGE);
383
         lsa->sn=htonl(LSA_MAXSEQNO);
384
         OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa.");
385
         OSPF_TRACE(D_EVENTS, "Type: %d, Id: %I, Rt: %I",lsatmp.type,
386
           lsatmp.id, lsatmp.rt);
387
         lsasum_check(lsa,(lsa+1),po);
388
         lsatmp.checksum=ntohs(lsa->checksum);
389
         flood_lsa(NULL,lsa,&lsatmp,po,NULL,oa,0);
390
         if(en=ospf_hash_find_header(oa->gr,&lsatmp))
391
         {
392
           flood_lsa(NULL,NULL,&en->lsa,po,NULL,oa,1);
393
         }
394
         continue;
395
       }
396

    
397
      /* pg 144 (5a) */
398
      if(lsadb && ((now-lsadb->inst_t)<=MINLSARRIVAL))
399
      {
400
        DBG("I got it in less that MINLSARRIVAL\n");
401
        continue;
402
      }
403
        
404
      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa,1)==0)
405
      {
406
        DBG("Wasn't flooded back\n");
407
        if(ifa->state==OSPF_IS_BACKUP)
408
        {
409
          if(ifa->drid==n->rid) ospf_lsa_delay(n, lsa, p);
410
        }
411
        else ospf_lsa_delay(n, lsa, p);
412
      }
413

    
414
      /* Remove old from all ret lists */
415
      /* pg 144 (5c) */
416
      if(lsadb)
417
        WALK_LIST(NODE ift,po->iface_list)
418
          WALK_LIST(NODE ntmp,ift->neigh_list)
419
          {
420
            struct top_hash_entry *en;
421
            if(ntmp->state>NEIGHBOR_EXSTART)
422
              if((en=ospf_hash_find_header(ntmp->lsrth,&lsadb->lsa))!=NULL)
423
              {
424
                s_rem_node(SNODE en);
425
                if(en->lsa_body!=NULL) mb_free(en->lsa_body);
426
                en->lsa_body=NULL;
427
                ospf_hash_delete(ntmp->lsrth,en);
428
              }
429
          }
430

    
431
      if((lsatmp.age==LSA_MAXAGE)&&(lsatmp.sn==LSA_MAXSEQNO)
432
        &&lsadb&&can_flush_lsa(oa))
433
      {
434
        flush_lsa(lsadb,oa);
435
        schedule_rtcalc(oa);
436
        continue;
437
      }
438

    
439
      /* pg 144 (5d) */
440
      body=mb_alloc(p->pool,lsatmp.length-sizeof(struct ospf_lsa_header));
441
      ntohlsab(lsa+1,body,lsatmp.type,
442
        lsatmp.length-sizeof(struct ospf_lsa_header));
443
      lsadb=lsa_install_new(&lsatmp,body, oa);
444
      DBG("New LSA installed in DB\n");
445

    
446
      continue;
447
    }
448

    
449
    /* FIXME pg145 (6)?? */
450

    
451
    /* pg145 (7) */
452
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
453
    {
454
      struct top_hash_entry *en;
455
      DBG("PG145(6) Got the same LSA\n");
456
      if((en=ospf_hash_find_header(n->lsrth,&lsadb->lsa))!=NULL)
457
      {
458
        s_rem_node(SNODE en);
459
        if(en->lsa_body!=NULL) mb_free(en->lsa_body);
460
        en->lsa_body=NULL;
461
        ospf_hash_delete(n->lsrth, en);
462
        if(ifa->state==OSPF_IS_BACKUP)
463
        {
464
          if(n->rid==ifa->drid) ospf_lsa_delay(n, lsa, p);
465
        }
466
      }
467
      else
468
      {
469
        ospf_lsack_direct_tx(n,lsa);
470
      }
471
      continue;
472
    }
473

    
474
    /* pg145 (8) */
475
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
476
    {
477
      continue;
478
    }
479

    
480
    {
481
      list l;
482
      struct l_lsr_head ll;
483
      init_list(&l);
484
      ll.lsh.id=lsadb->lsa.id;
485
      ll.lsh.rt=lsadb->lsa.rt;
486
      ll.lsh.type=lsadb->lsa.type;
487
      add_tail(&l, NODE &ll);
488
      ospf_lsupd_tx_list(n, &l);
489
    }
490
  }
491
  if(n->state==NEIGHBOR_LOADING)
492
  {
493
    ospf_lsreq_tx(n);        /* Send me another part of database */
494
  }
495
}
496

    
497
void
498
net_flush_lsa(struct top_hash_entry *en, struct proto_ospf *po,
499
  struct ospf_area *oa)
500
{
501
  struct ospf_lsa_header *lsa=&en->lsa;
502
  struct proto *p=&po->proto;
503

    
504
  lsa->age=LSA_MAXAGE;
505
  lsa->sn=LSA_MAXSEQNO;
506
  lsasum_calculate(lsa,en->lsa_body,po);
507
  OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa!");
508
  OSPF_TRACE(D_EVENTS, "Type: %d, Id: %I, Rt: %I", lsa->type,
509
    lsa->id, lsa->rt);
510
  flood_lsa(NULL,NULL,lsa,po,NULL,oa,0);
511
}
512