Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ 9669362f

History | View | Annotate | Download (9.61 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)
15
{
16
  struct ospf_iface *ifa;
17
  struct ospf_neighbor *nn;
18
  struct top_hash_entry *en;
19
  int ret,retval=0;
20

    
21
  /* pg 148 */
22
  WALK_LIST(NODE ifa,po->iface_list)
23
  {
24
    if(hh->type==LSA_T_EXT)
25
    {
26
      if(ifa->type==OSPF_IT_VLINK) continue;
27
      if(ifa->oa->stub) continue;
28
    }
29
    else
30
    {
31
      if(oa->areaid==BACKBONE)
32
      {
33
        if((ifa->type!=OSPF_IT_VLINK)&&(ifa->oa!=oa)) continue;
34
      }
35
      else
36
      {
37
        if(ifa->oa!=oa) continue;
38
      }
39
    }
40
    ret=0;
41
    WALK_LIST(NODE nn, ifa->neigh_list)
42
    {
43
      if(nn->state<NEIGHBOR_EXCHANGE) continue;
44
      if(nn->state<NEIGHBOR_FULL)
45
      {
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?\n");
68
          }
69
        }
70
      }
71
      if(nn==n) continue;
72
      if((en=ospf_hash_find_header(nn->lsrth, hh))==NULL)
73
      {
74
        en=ospf_hash_get_header(nn->lsrth, hh);
75
      }
76
      else
77
      {
78
        s_rem_node(SNODE en);
79
      }
80
      s_add_tail(&nn->lsrtl, SNODE en);
81
      ret=1;
82
      DBG("Adding LSA lsrt RT: %I, Id: %I, Type: %u for n: %I\n",
83
        en->lsa.rt,en->lsa.id, en->lsa.type, nn->ip);
84
    }
85
    if(ret==0) continue;
86
    if(ifa==iff)
87
    {
88
      if((n->rid==iff->drid)||n->rid==iff->bdrid) continue;
89
      if(iff->state==OSPF_IS_BACKUP) continue;
90
      retval=1;
91
    }
92

    
93
    {
94
      sock *sk;
95
      ip_addr to;
96
      u16 len;
97
      struct ospf_lsupd_packet *pk;
98
      struct ospf_packet *op;
99

    
100
      if(ifa->type==OSPF_IT_NBMA)  sk=ifa->ip_sk;
101
      else sk=ifa->hello_sk;        /* FIXME is this true for PTP? */
102

    
103
      pk=(struct ospf_lsupd_packet *)sk->tbuf;
104
      op=(struct ospf_packet *)sk->tbuf;
105

    
106
      fill_ospf_pkt_hdr(ifa, pk, LSUPD);
107
      pk->lsano=htonl(1);
108
      if(hn!=NULL)
109
      {
110
        memcpy(pk+1,hn,ntohs(hn->length));
111
        len=sizeof(struct ospf_lsupd_packet)+ntohs(hn->length);
112
      }
113
      else
114
      {
115
        u8 *help;
116
        struct top_hash_entry *en;
117
        struct ospf_lsa_header *lh;
118
        u16 age;
119

    
120
        lh=(struct ospf_lsa_header *)(pk+1);
121
        htonlsah(hh,lh);
122
        age=hh->age;
123
        age+=ifa->inftransdelay;
124
        if(age>LSA_MAXAGE) age=LSA_MAXAGE;
125
        lh->age=htons(age);
126
        help=(u8 *)(lh+1);
127
        en=ospf_hash_find_header(oa->gr,hh);
128
        htonlsab(en->lsa_body,help,hh->type,hh->length
129
          -sizeof(struct ospf_lsa_header));
130
        len=hh->length;
131
      }
132
      op->length=htons(len);
133
      ospf_pkt_finalize(ifa, op);
134

    
135
      if(ifa->type==OSPF_IT_NBMA)
136
      {
137
        sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
138
      }
139
      else
140
      {
141
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR))
142
          sk_send_to(sk,len, AllSPFRouters, OSPF_PROTO);
143
        else sk_send_to(sk,len, AllDRouters, OSPF_PROTO);
144
      }
145
    }
146
  }
147
  return retval;
148
}
149

    
150
void                /* I send all I received in LSREQ */
151
ospf_lsupd_tx_list(struct ospf_neighbor *n, list *l)
152
{
153
  struct l_lsr_head *llsh;
154
  u16 len;
155
  u32 lsano;
156
  struct top_hash_entry *en;
157
  struct ospf_lsupd_packet *pk;
158
  struct ospf_packet *op;
159
  struct proto *p=&n->ifa->oa->po->proto;
160
  void *pktpos;
161
  u8 ii;
162
  u8 *jj=n->ifa->ip_sk->tbuf;
163

    
164
  if(EMPTY_LIST(*l)) return;
165

    
166
  pk=(struct ospf_lsupd_packet *)n->ifa->ip_sk->tbuf;
167
  op=(struct ospf_packet *)n->ifa->ip_sk->tbuf;
168

    
169
  DBG("LSupd: 1st packet\n");
170
       
171
  fill_ospf_pkt_hdr(n->ifa, pk, LSUPD);
172
  len=SIPH+sizeof(struct ospf_lsupd_packet);
173
  lsano=0;
174
  pktpos=(pk+1);
175

    
176
  WALK_LIST(llsh, *l)
177
  {
178
    en=ospf_hash_find(n->ifa->oa->gr,llsh->lsh.id,llsh->lsh.rt,llsh->lsh.type);
179
    DBG("Sending ID=%I, Type=%u, RT=%I\n", llsh->lsh.id, llsh->lsh.type,
180
      llsh->lsh.rt);
181
    if((len+en->lsa.length)>n->ifa->iface->mtu)
182
    {
183
      pk->lsano=htonl(lsano);
184
      op->length=htons(len);
185
      ospf_pkt_finalize(n->ifa, op);
186
                       
187
      sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
188
      debug("%s: LS upd sent to %I\n", p->name, n->ip);
189

    
190
      DBG("LSupd: next packet\n");
191
      fill_ospf_pkt_hdr(n->ifa, pk, LSUPD);
192
      len=SIPH+sizeof(struct ospf_lsupd_packet);
193
      lsano=0;
194
      pktpos=(pk+1);
195
    }
196
    htonlsah(&(en->lsa), pktpos);
197
    pktpos=pktpos+sizeof(struct ospf_lsa_header);
198
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
199
      -sizeof(struct ospf_lsa_header));
200
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
201
    len=len+en->lsa.length;
202
    lsano++;
203
  }
204
  pk->lsano=htonl(lsano);
205
  op->length=htons(len-SIPH);
206
  ospf_pkt_finalize(n->ifa, op);
207

    
208
  for(ii=0;ii<(len-SIPH);ii+=4)
209
    DBG("Out dump: %d,%d,%d,%d\n", *(jj+ii), *(jj+ii+1), *(jj+ii+2), *(jj+ii+3));
210

    
211
  sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
212
  debug("%s: LS upd sent to %I\n", p->name, n->ip);
213
}
214

    
215
void
216
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
217
  struct ospf_iface *ifa, u16 size)
218
{
219
  u32 area,nrid,myrid;
220
  struct ospf_neighbor *n,*ntmp;
221
  struct ospf_lsa_header *lsa;
222
  struct ospf_area *oa;
223
  struct proto_ospf *po=(struct proto_ospf *)p;
224
  u16 length;
225
  u8 i;
226

    
227
  nrid=ntohl(ps->ospf_packet.routerid);
228

    
229
  myrid=p->cf->global->router_id;
230

    
231
  if((n=find_neigh(ifa, nrid))==NULL)
232
  {
233
    debug("%s: Received lsupd from unknown neigbor! (%I)\n", p->name,
234
      nrid);
235
    return ;
236
  }
237
  if(n->state<NEIGHBOR_EXCHANGE)
238
  {
239
    debug("%s: Received lsupd in lesser state than EXCHANGE from (%I)\n",
240
      p->name,n->ip);
241
    return;
242
  }
243

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

    
246
  lsa=(struct ospf_lsa_header *)(ps+1);
247
  area=htonl(ps->ospf_packet.areaid);
248
  oa=ospf_find_area((struct proto_ospf *)p,area);
249
  for(i=0;i<ntohl(ps->lsano);i++,
250
    lsa=(struct ospf_lsa_header *)(((u8 *)lsa)+ntohs(lsa->length)))
251
  {
252
    struct ospf_lsa_header lsatmp;
253
    struct top_hash_entry *lsadb;
254
    /* pg 143 (1) */
255
    if(lsa->checksum!=lsasum_check(lsa,NULL,po))
256
    {
257
      log("Received bad lsa checksum from %I\n",n->rid);
258
      continue;
259
    }
260
    /* pg 143 (2) */
261
    if((lsa->type<LSA_T_RT)||(lsa->type>LSA_T_EXT))
262
    {
263
      log("Unknown LSA type from %I\n",n->rid);
264
      continue;
265
    }
266
    /* pg 143 (3) */
267
    if((lsa->type==LSA_T_EXT)&&oa->stub)
268
    {
269
      log("Received External LSA in stub area from %I\n",n->rid);
270
      continue;
271
    }
272
    ntohlsah(lsa,&lsatmp);
273
    DBG("Processing update Type: %u ID: %I RT: %I\n",lsatmp.type,
274
        lsatmp.id, lsatmp.rt);
275
    lsadb=ospf_hash_find_header(oa->gr, &lsatmp);
276

    
277
    /* pg 143 (4) */
278
    if((lsatmp.age==LSA_MAXAGE)&&(lsadb==NULL))
279
    {
280
      int flag=0;
281
      struct ospf_iface *ifatmp;
282

    
283
      WALK_LIST(NODE ifatmp,po->iface_list)
284
        WALK_LIST(NODE ntmp,ifatmp->neigh_list)
285
          if((ntmp->state==NEIGHBOR_EXCHANGE)&&
286
            (ntmp->state==NEIGHBOR_LOADING))
287
            flag=1;
288
      DBG("PG143(4): Flag=%u\n",flag);
289

    
290
      if(flag==0)
291
      {
292
        ospf_lsack_direct_tx(n,lsa);
293
        continue;
294
      }
295
    }
296
    /* pg 144 (5) */
297
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
298
    {
299
       struct ospf_iface *ift=NULL;
300
       void *body;
301

    
302
       DBG("PG143(5): Received LSA is newer\n");
303

    
304
      /* pg 144 (5a) */
305
      if(lsadb && ((now-lsadb->inst_t)<MINLSARRIVAL))
306
      {
307
        DBG("I got it in less that MINLSARRIVAL\n");
308
        continue;
309
      }
310
        
311

    
312
      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa)==0)
313
      {
314
        DBG("Wasn't flooded back\n");
315
        if(ifa->state==OSPF_IS_BACKUP)
316
        {
317
          if(ifa->drid==n->rid) ospf_lsa_delay(n, lsa, p);
318
        }
319
        else ospf_lsa_delay(n, lsa, p);
320
      }
321

    
322
      /* Remove old from all ret lists */
323
      /* pg 144 (5c) */
324
      if(lsadb)
325
        WALK_LIST(NODE ift,po->iface_list)
326
          WALK_LIST(NODE ntmp,ift->neigh_list)
327
          {
328
            struct top_hash_entry *en;
329
            if(ntmp->state>NEIGHBOR_EXSTART)
330
              if((en=ospf_hash_find_header(ntmp->lsrth,&lsadb->lsa))!=NULL)
331
              {
332
                s_rem_node(SNODE en);
333
                ospf_hash_delete(ntmp->lsrth,en);
334
              }
335
          }
336

    
337
      /* pg 144 (5d) */
338
      body=mb_alloc(p->pool,lsatmp.length-sizeof(struct ospf_lsa_header));
339
      ntohlsab(lsa+1,body,lsatmp.type,
340
        lsatmp.length-sizeof(struct ospf_lsa_header));
341
      lsadb=lsa_install_new(&lsatmp,body, oa, p);
342
      DBG("New LSA installed in DB\n");
343

    
344
      /* FIXME 145 (5f) self originated? */
345

    
346
      continue;
347
    }
348

    
349
    /* FIXME pg145 (6)?? */
350

    
351
    /* pg145 (7) */
352
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
353
    {
354
      struct top_hash_entry *en;
355
      DBG("PG145(6) Got the same LSA\n");
356
      if((en=ospf_hash_find_header(n->lsrth,&lsadb->lsa))!=NULL)
357
      {
358
        s_rem_node(SNODE en);
359
        ospf_hash_delete(n->lsrth, en);
360
        if(ifa->state==OSPF_IS_BACKUP)
361
        {
362
          if(n->rid==ifa->drid) ospf_lsa_delay(n, lsa, p);
363
        }
364
      }
365
      else
366
      {
367
        ospf_lsack_direct_tx(n,lsa);
368
      }
369
      continue;
370
    }
371

    
372
    /* pg145 (8) */
373
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
374
    {
375
      continue;
376
    }
377

    
378
    {
379
      list l;
380
      struct l_lsr_head llsh;
381
      init_list(&l);
382
      memcpy(&llsh.lsh,&lsadb->lsa,sizeof(struct ospf_lsa_header));
383
      add_tail(&l, NODE &llsh);
384
      ospf_lsupd_tx_list(n, &l);
385
    }
386
  }
387
}
388