Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ 13e9bac3

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

    
98
    if(ret==0) continue;
99

    
100
    if(ifa==iff)
101
    {
102
      if((n->rid==iff->drid)||n->rid==iff->bdrid) continue;
103
      if(iff->state==OSPF_IS_BACKUP) continue;
104
      retval=1;
105
    }
106

    
107
    {
108
      sock *sk;
109
      ip_addr to;
110
      u16 len;
111
      struct ospf_lsupd_packet *pk;
112
      struct ospf_packet *op;
113

    
114
      if(ifa->type==OSPF_IT_NBMA)  sk=ifa->ip_sk;
115
      else sk=ifa->hello_sk;        /* FIXME is this true for PTP? */
116

    
117
      pk=(struct ospf_lsupd_packet *)sk->tbuf;
118
      op=(struct ospf_packet *)sk->tbuf;
119

    
120
      fill_ospf_pkt_hdr(ifa, pk, LSUPD_P);
121
      pk->lsano=htonl(1);
122
      if(hn!=NULL)
123
      {
124
        memcpy(pk+1,hn,ntohs(hn->length));
125
        len=sizeof(struct ospf_lsupd_packet)+ntohs(hn->length);
126
      }
127
      else
128
      {
129
        u8 *help;
130
        struct top_hash_entry *en;
131
        struct ospf_lsa_header *lh;
132
        u16 age;
133

    
134
        lh=(struct ospf_lsa_header *)(pk+1);
135
        htonlsah(hh,lh);
136
        age=hh->age;
137
        age+=ifa->inftransdelay;
138
        if(age>LSA_MAXAGE) age=LSA_MAXAGE;
139
        lh->age=htons(age);
140
        help=(u8 *)(lh+1);
141
        en=ospf_hash_find_header(oa->gr,hh);
142
        htonlsab(en->lsa_body,help,hh->type,hh->length
143
          -sizeof(struct ospf_lsa_header));
144
        len=hh->length+sizeof(struct ospf_lsupd_packet);
145
      }
146
      op->length=htons(len);
147
      ospf_pkt_finalize(ifa, op);
148
      debug("%s: LS upd flooded via %s\n", p->name, ifa->iface->name);
149

    
150
      if(ifa->type==OSPF_IT_NBMA)
151
      {
152
        sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
153
      }
154
      else
155
      {
156
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR))
157
          sk_send_to(sk,len, AllSPFRouters, OSPF_PROTO);
158
        else sk_send_to(sk,len, AllDRouters, OSPF_PROTO);
159
      }
160
    }
161
  }
162
  return retval;
163
}
164

    
165
void                /* I send all I received in LSREQ */
166
ospf_lsupd_tx_list(struct ospf_neighbor *n, list *l)
167
{
168
  struct l_lsr_head *llsh;
169
  u16 len;
170
  u32 lsano;
171
  struct top_hash_entry *en;
172
  struct ospf_lsupd_packet *pk;
173
  struct ospf_packet *op;
174
  struct proto *p=&n->ifa->oa->po->proto;
175
  void *pktpos;
176
  u8 ii;
177
  u8 *jj=n->ifa->ip_sk->tbuf;
178

    
179
  if(EMPTY_LIST(*l)) return;
180

    
181
  pk=(struct ospf_lsupd_packet *)n->ifa->ip_sk->tbuf;
182
  op=(struct ospf_packet *)n->ifa->ip_sk->tbuf;
183

    
184
  DBG("LSupd: 1st packet\n");
185
       
186
  fill_ospf_pkt_hdr(n->ifa, pk, LSUPD_P);
187
  len=SIPH+sizeof(struct ospf_lsupd_packet);
188
  lsano=0;
189
  pktpos=(pk+1);
190

    
191
  WALK_LIST(llsh, *l)
192
  {
193
    if((en=ospf_hash_find(n->ifa->oa->gr,llsh->lsh.id,llsh->lsh.rt,
194
      llsh->lsh.type))==NULL) continue;                /* Probably flushed LSA */
195

    
196
    DBG("Sending ID=%I, Type=%u, RT=%I\n", llsh->lsh.id, llsh->lsh.type,
197
      llsh->lsh.rt);
198
    if(((u32)(len+en->lsa.length))>n->ifa->iface->mtu)
199
    {
200
      pk->lsano=htonl(lsano);
201
      op->length=htons(len-SIPH);
202
      ospf_pkt_finalize(n->ifa, op);
203
                       
204
      sk_send_to(n->ifa->ip_sk,len-SIPH, n->ip, OSPF_PROTO);
205
      debug("%s: LS upd sent to %I (%d LSAs)\n", p->name, n->ip, lsano);
206

    
207
      DBG("LSupd: next packet\n");
208
      fill_ospf_pkt_hdr(n->ifa, pk, LSUPD_P);
209
      len=SIPH+sizeof(struct ospf_lsupd_packet);
210
      lsano=0;
211
      pktpos=(pk+1);
212
    }
213
    htonlsah(&(en->lsa), pktpos);
214
    pktpos=pktpos+sizeof(struct ospf_lsa_header);
215
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
216
      -sizeof(struct ospf_lsa_header));
217
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
218
    len+=en->lsa.length;
219
    lsano++;
220
  }
221
  if(lsano>0)
222
  {
223
    pk->lsano=htonl(lsano);
224
    op->length=htons(len-SIPH);
225
    ospf_pkt_finalize(n->ifa, op);
226

    
227
    debug("%s: LS upd sent to %I (%d LSAs)\n", p->name, n->ip, lsano);
228
    sk_send_to(n->ifa->ip_sk,len-SIPH, n->ip, OSPF_PROTO);
229
  }
230
}
231

    
232
void
233
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
234
  struct ospf_iface *ifa, u16 size)
235
{
236
  u32 area,nrid,myrid;
237
  struct ospf_neighbor *n,*ntmp;
238
  struct ospf_lsa_header *lsa;
239
  struct ospf_area *oa;
240
  struct proto_ospf *po=(struct proto_ospf *)p;
241
  u16 length;
242
  u8 i;
243

    
244
  nrid=ntohl(ps->ospf_packet.routerid);
245

    
246
  myrid=p->cf->global->router_id;
247

    
248

    
249
  if((n=find_neigh(ifa, nrid))==NULL)
250
  {
251
    debug("%s: Received lsupd from unknown neigbor! (%I)\n", p->name,
252
      nrid);
253
    return ;
254
  }
255
  if(n->state<NEIGHBOR_EXCHANGE)
256
  {
257
    debug("%s: Received lsupd in lesser state than EXCHANGE from (%I)\n",
258
      p->name,n->ip);
259
    return;
260
  }
261
  if(size<=(sizeof(struct ospf_lsupd_packet)+sizeof(struct ospf_lsa_header)))
262
  {
263
    log("%s: Received lsupd from %I is too short!", p->name,n->ip);
264
    return;
265
  }
266

    
267
  debug("%s: Received LS upd from %I\n", p->name, n->ip); 
268

    
269
  lsa=(struct ospf_lsa_header *)(ps+1);
270
  area=htonl(ps->ospf_packet.areaid);
271
  oa=ospf_find_area((struct proto_ospf *)p,area);
272
  for(i=0;i<ntohl(ps->lsano);i++,
273
    lsa=(struct ospf_lsa_header *)(((u8 *)lsa)+ntohs(lsa->length)))
274
  {
275
    struct ospf_lsa_header lsatmp;
276
    struct top_hash_entry *lsadb;
277
    u16 lenn;
278
    int diff=((u8 *)lsa)-((u8 *)ps);
279

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

    
284
    lenn=ntohs(lsa->length);
285

    
286
    if((lenn<=sizeof(struct ospf_lsa_header))||(lenn!=(4*(lenn/4))))
287
    {
288
      log("Received LSA with bad length");
289
      ospf_neigh_sm(n,INM_BADLSREQ);
290
      break;
291
    }
292
    /* pg 143 (1) */
293
    if(lsa->checksum!=lsasum_check(lsa,NULL,po))
294
    {
295
      log("Received bad lsa checksum from %I",n->rid);
296
      continue;
297
    }
298
    /* pg 143 (2) */
299
    if((lsa->type<LSA_T_RT)||(lsa->type>LSA_T_EXT))
300
    {
301
      log("Unknown LSA type from %I",n->rid);
302
      continue;
303
    }
304
    /* pg 143 (3) */
305
    if((lsa->type==LSA_T_EXT)&&oa->stub)
306
    {
307
      log("Received External LSA in stub area from %I",n->rid);
308
      continue;
309
    }
310
    ntohlsah(lsa,&lsatmp);
311
    debug("Update Type: %u ID: %I RT: %I, Sn: 0x%08x Age: %u, Sum: %u\n",
312
      lsatmp.type, lsatmp.id, lsatmp.rt, lsatmp.sn, lsatmp.age,
313
      lsatmp.checksum);
314
    lsadb=ospf_hash_find_header(oa->gr, &lsatmp);
315
    if(lsadb)
316
      debug("I have Type: %u ID: %I RT: %I, Sn: 0x%08x Age: %u, Sum: %u\n",
317
      lsadb->lsa.type, lsadb->lsa.id, lsadb->lsa.rt, lsadb->lsa.sn,
318
      lsadb->lsa.age, lsadb->lsa.checksum);
319

    
320
    /* pg 143 (4) */
321
    if((lsatmp.age==LSA_MAXAGE)&&(lsadb==NULL))
322
    {
323
      int flag=0;
324
      struct ospf_iface *ifatmp;
325

    
326
      if(can_flush_lsa(oa))
327
      {
328
        ospf_lsack_direct_tx(n,lsa);
329
        continue;
330
      }
331
    }
332
    /* pg 144 (5) */
333
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
334
    {
335
       struct ospf_iface *ift=NULL;
336
       void *body;
337
       struct ospf_iface *nifa;
338
       int self=0;
339

    
340
       DBG("PG143(5): Received LSA is newer\n");
341

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

    
344
       if(lsatmp.type==LSA_T_NET)
345
       {
346
         WALK_LIST(nifa,po->iface_list)
347
         {
348
           if(ipa_compare(nifa->iface->addr->ip,ipa_from_u32(lsatmp.id))==0)
349
           {
350
             self=1;
351
             break;
352
           }
353
         }
354
       }
355

    
356
       if(self)
357
       {
358
         struct top_hash_entry *en;
359

    
360
         if((lsatmp.age==LSA_MAXAGE)&&(lsatmp.sn==LSA_MAXSEQNO)) continue;
361

    
362
         lsatmp.age=LSA_MAXAGE;
363
         lsatmp.sn=LSA_MAXSEQNO;
364
         lsa->age=htons(LSA_MAXAGE);
365
         lsa->sn=htonl(LSA_MAXSEQNO);
366
         debug("%s: Premature aging self originated lsa.\n",p->name);
367
         debug("%s: Type: %d, Id: %I, Rt: %I\n", p->name, lsatmp.type,
368
           lsatmp.id, lsatmp.rt);
369
         lsasum_check(lsa,(lsa+1),po);
370
         lsatmp.checksum=ntohs(lsa->checksum);
371
         flood_lsa(NULL,lsa,&lsatmp,po,NULL,oa,0);
372
         if(en=ospf_hash_find_header(oa->gr,&lsatmp))
373
         {
374
           flood_lsa(NULL,NULL,&en->lsa,po,NULL,oa,1);
375
         }
376
         continue;
377
       }
378

    
379
      /* pg 144 (5a) */
380
      if(lsadb && ((now-lsadb->inst_t)<=MINLSARRIVAL))
381
      {
382
        DBG("I got it in less that MINLSARRIVAL\n");
383
        continue;
384
      }
385
        
386
      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa,1)==0)
387
      {
388
        DBG("Wasn't flooded back\n");
389
        if(ifa->state==OSPF_IS_BACKUP)
390
        {
391
          if(ifa->drid==n->rid) ospf_lsa_delay(n, lsa, p);
392
        }
393
        else ospf_lsa_delay(n, lsa, p);
394
      }
395

    
396
      /* Remove old from all ret lists */
397
      /* pg 144 (5c) */
398
      if(lsadb)
399
        WALK_LIST(NODE ift,po->iface_list)
400
          WALK_LIST(NODE ntmp,ift->neigh_list)
401
          {
402
            struct top_hash_entry *en;
403
            if(ntmp->state>NEIGHBOR_EXSTART)
404
              if((en=ospf_hash_find_header(ntmp->lsrth,&lsadb->lsa))!=NULL)
405
              {
406
                s_rem_node(SNODE en);
407
                ospf_hash_delete(ntmp->lsrth,en);
408
              }
409
          }
410

    
411
      if((lsatmp.age==LSA_MAXAGE)&&(lsatmp.sn==LSA_MAXSEQNO)
412
        &&lsadb&&can_flush_lsa(oa))
413
      {
414
        flush_lsa(lsadb,oa);
415
        schedule_rtcalc(oa);
416
        continue;
417
      }
418

    
419
      /* pg 144 (5d) */
420
      body=mb_alloc(p->pool,lsatmp.length-sizeof(struct ospf_lsa_header));
421
      ntohlsab(lsa+1,body,lsatmp.type,
422
        lsatmp.length-sizeof(struct ospf_lsa_header));
423
      lsadb=lsa_install_new(&lsatmp,body, oa, p);
424
      DBG("New LSA installed in DB\n");
425

    
426
      continue;
427
    }
428

    
429
    /* FIXME pg145 (6)?? */
430

    
431
    /* pg145 (7) */
432
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
433
    {
434
      struct top_hash_entry *en;
435
      DBG("PG145(6) Got the same LSA\n");
436
      if((en=ospf_hash_find_header(n->lsrth,&lsadb->lsa))!=NULL)
437
      {
438
        s_rem_node(SNODE en);
439
        ospf_hash_delete(n->lsrth, en);
440
        if(ifa->state==OSPF_IS_BACKUP)
441
        {
442
          if(n->rid==ifa->drid) ospf_lsa_delay(n, lsa, p);
443
        }
444
      }
445
      else
446
      {
447
        ospf_lsack_direct_tx(n,lsa);
448
      }
449
      continue;
450
    }
451

    
452
    /* pg145 (8) */
453
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
454
    {
455
      continue;
456
    }
457

    
458
    {
459
      list l;
460
      struct l_lsr_head ll;
461
      init_list(&l);
462
      ll.lsh.id=lsadb->lsa.id;
463
      ll.lsh.rt=lsadb->lsa.rt;
464
      ll.lsh.type=lsadb->lsa.type;
465
      add_tail(&l, NODE &ll);
466
      ospf_lsupd_tx_list(n, &l);
467
    }
468
  }
469
  if(n->state==NEIGHBOR_LOADING)
470
  {
471
    ospf_lsreq_tx(n);        /* Send me another part of database */
472
  }
473
}
474

    
475
void
476
net_flush_lsa(struct top_hash_entry *en, struct proto_ospf *po,
477
  struct ospf_area *oa)
478
{
479
  struct ospf_lsa_header *lsa=&en->lsa;
480

    
481
  lsa->age=LSA_MAXAGE;
482
  lsa->sn=LSA_MAXSEQNO;
483
  lsasum_calculate(lsa,en->lsa_body,po);
484
  debug("%s: Premature aging self originated lsa!\n",po->proto.name);
485
  debug("%s: Type: %d, Id: %I, Rt: %I\n", po->proto.name, lsa->type,
486
    lsa->id, lsa->rt);
487
  flood_lsa(NULL,NULL,lsa,po,NULL,oa,0);
488
}
489