Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ 5f743d96

History | View | Annotate | Download (9.3 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
      en=ospf_hash_get_header(nn->lsrth, hh);
73
      s_add_tail(&nn->lsrtl, SNODE en);
74
      ret=1;
75
    }
76
    if(ret==0) continue;
77
    if(ifa==iff)
78
    {
79
      if((n->rid==iff->drid)||n->rid==iff->bdrid) continue;
80
      if(iff->state==OSPF_IS_BACKUP) continue;
81
      retval=1;
82
    }
83

    
84
    {
85
      sock *sk;
86
      ip_addr to;
87
      u16 len;
88
      struct ospf_lsupd_packet *pk;
89
      struct ospf_packet *op;
90

    
91
      if(ifa->type==OSPF_IT_NBMA)  sk=ifa->ip_sk;
92
      else sk=ifa->hello_sk;        /* FIXME is this true for PTP? */
93

    
94
      pk=(struct ospf_lsupd_packet *)sk->tbuf;
95
      op=(struct ospf_packet *)sk->tbuf;
96

    
97
      fill_ospf_pkt_hdr(ifa, pk, LSUPD);
98
      pk->lsano=htonl(1);
99
      if(hn!=NULL)
100
      {
101
        memcpy(pk+1,hn,ntohs(hn->length));
102
        len=sizeof(struct ospf_lsupd_packet)+ntohs(hn->length);
103
      }
104
      else
105
      {
106
        u8 *help;
107
        struct top_hash_entry *en;
108
        struct ospf_lsa_header *lh;
109
        u16 age;
110

    
111
        lh=(struct ospf_lsa_header *)(pk+1);
112
        htonlsah(hh,lh);
113
        age=hh->age;
114
        age+=ifa->inftransdelay;
115
        if(age>LSA_MAXAGE) age=LSA_MAXAGE;
116
        lh->age=htons(age);
117
        help=(u8 *)(lh+1);
118
        en=ospf_hash_find_header(oa->gr,hh);
119
        htonlsab(en->lsa_body,help,hh->type,hh->length
120
          -sizeof(struct ospf_lsa_header));
121
        len=hh->length;
122
      }
123
      op->length=htons(len);
124
      ospf_pkt_finalize(ifa, op);
125

    
126
      if(ifa->type==OSPF_IT_NBMA)
127
      {
128
        sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
129
      }
130
      else
131
      {
132
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR))
133
          sk_send_to(sk,len, AllSPFRouters, OSPF_PROTO);
134
        else sk_send_to(sk,len, AllDRouters, OSPF_PROTO);
135
      }
136
    }
137
  }
138
  return retval;
139
}
140

    
141
void                /* I send all I received in LSREQ */
142
ospf_lsupd_tx_list(struct ospf_neighbor *n, list *l)
143
{
144
  struct l_lsr_head *llsh;
145
  u16 len;
146
  u32 lsano;
147
  struct top_hash_entry *en;
148
  struct ospf_lsupd_packet *pk;
149
  struct ospf_packet *op;
150
  void *pktpos;
151
  u8 ii;
152
  u8 *jj=n->ifa->ip_sk->tbuf;
153

    
154
  if(EMPTY_LIST(*l)) return;
155

    
156
  pk=(struct ospf_lsupd_packet *)n->ifa->ip_sk->tbuf;
157
  op=(struct ospf_packet *)n->ifa->ip_sk->tbuf;
158

    
159
  DBG("LSupd: 1st packet\n");
160
       
161
  fill_ospf_pkt_hdr(n->ifa, pk, LSUPD);
162
  len=SIPH+sizeof(struct ospf_lsupd_packet);
163
  lsano=0;
164
  pktpos=(pk+1);
165

    
166
  WALK_LIST(llsh, *l)
167
  {
168
    en=ospf_hash_find(n->ifa->oa->gr,llsh->lsh.id,llsh->lsh.rt,llsh->lsh.type);
169
    DBG("Sending ID=%I, Type=%u, RT=%I\n", llsh->lsh.id, llsh->lsh.type,
170
      llsh->lsh.rt);
171
    if((len+en->lsa.length)>n->ifa->iface->mtu)
172
    {
173
      pk->lsano=htonl(lsano);
174
      op->length=htons(len);
175
      ospf_pkt_finalize(n->ifa, op);
176
                       
177
      for(ii=0;ii<(len-SIPH);ii+=4)
178
        DBG("Out dump: %d,%d,%d,%d\n", *(jj+ii), *(jj+ii+1), *(jj+ii+2), *(jj+ii+3));
179

    
180
      sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
181

    
182
      DBG("LSupd: next packet\n");
183
      fill_ospf_pkt_hdr(n->ifa, pk, LSUPD);
184
      len=SIPH+sizeof(struct ospf_lsupd_packet);
185
      lsano=0;
186
      pktpos=(pk+1);
187
    }
188
    htonlsah(&(en->lsa), pktpos);
189
    pktpos=pktpos+sizeof(struct ospf_lsa_header);
190
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
191
      -sizeof(struct ospf_lsa_header));
192
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
193
    len=len+en->lsa.length;
194
    lsano++;
195
  }
196
  pk->lsano=htonl(lsano);
197
  op->length=htons(len-SIPH);
198
  ospf_pkt_finalize(n->ifa, op);
199

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

    
203
  sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
204
  DBG("LSupd: sent\n");
205
}
206

    
207
void
208
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
209
  struct ospf_iface *ifa, u16 size)
210
{
211
  u32 area,nrid,myrid;
212
  struct ospf_neighbor *n,*ntmp;
213
  struct ospf_lsa_header *lsa;
214
  struct ospf_area *oa;
215
  struct proto_ospf *po=(struct proto_ospf *)p;
216
  u16 length;
217
  u8 i;
218

    
219
  nrid=ntohl(ps->ospf_packet.routerid);
220

    
221
  myrid=p->cf->global->router_id;
222

    
223
  if((n=find_neigh(ifa, nrid))==NULL)
224
  {
225
    debug("%s: Received lsupd from unknown neigbor! (%I)\n", p->name,
226
      nrid);
227
    return ;
228
  }
229
  if(n->state<NEIGHBOR_EXCHANGE)
230
  {
231
    debug("%s: Received lsupd in lesser state than EXCHANGE from (%I)\n",
232
      p->name,n->ip);
233
    return;
234
  }
235

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

    
267
    /* pg 143 (4) */
268
    if((lsatmp.age==LSA_MAXAGE)&&(lsadb==NULL))
269
    {
270
      int flag=0;
271
      struct ospf_iface *ifatmp;
272

    
273
      WALK_LIST(NODE ifatmp,po->iface_list)
274
        WALK_LIST(NODE ntmp,ifatmp->neigh_list)
275
          if((ntmp->state==NEIGHBOR_EXCHANGE)&&
276
            (ntmp->state==NEIGHBOR_LOADING))
277
            flag=1;
278
      DBG("PG143(4): Flag=%u\n",flag);
279

    
280
      if(flag==0)
281
      {
282
        ospf_lsack_direct_tx(n,lsa);
283
        continue;
284
      }
285
    }
286
    /* pg 144 (5) */
287
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
288
    {
289
       struct ospf_iface *ift=NULL;
290
       void *body;
291

    
292
       DBG("PG143(5): Received LSA is newer\n");
293

    
294
      /* pg 144 (5a) */
295
      if(lsadb && ((now-lsadb->inst_t)<MINLSARRIVAL))
296
      {
297
        DBG("I got it in less that MINLSARRIVAL\n");
298
        continue;
299
      }
300
        
301

    
302
      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa)==0)
303
      {
304
        DBG("Wasn't flooded back\n");
305
        if(ifa->state==OSPF_IS_BACKUP)
306
        {
307
          if(ifa->drid==n->rid) ospf_lsa_delay(n, lsa, p);
308
        }
309
        else ospf_lsa_delay(n, lsa, p);
310
      }
311

    
312
      /* Remove old from all ret lists */
313
      /* pg 144 (5c) */
314
      if(lsadb)
315
        WALK_LIST(NODE ift,po->iface_list)
316
          WALK_LIST(NODE ntmp,ift->neigh_list)
317
          {
318
            struct top_hash_entry *en;
319
            if(ntmp->state>NEIGHBOR_EXSTART)
320
              if((en=ospf_hash_find_header(ntmp->lsrth,&lsadb->lsa))!=NULL)
321
              {
322
                s_rem_node(SNODE en);
323
                ospf_hash_delete(ntmp->lsrth,en);
324
              }
325
          }
326

    
327
      /* pg 144 (5d) */
328
      body=mb_alloc(p->pool,lsatmp.length-sizeof(struct ospf_lsa_header));
329
      ntohlsab(lsa+1,body,lsatmp.type,
330
        lsatmp.length-sizeof(struct ospf_lsa_header));
331
      lsadb=lsa_install_new(&lsatmp,body, oa, p);
332
      DBG("New LSA installed in DB\n");
333

    
334
      /* FIXME 145 (5f) self originated? */
335

    
336
      continue;
337
    }
338

    
339
    /* FIXME pg145 (6)?? */
340

    
341
    /* pg145 (7) */
342
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
343
    {
344
      struct top_hash_entry *en;
345
      DBG("PG145(6) Got the same LSA\n");
346
      if((en=ospf_hash_find_header(n->lsrth,&lsadb->lsa))!=NULL)
347
      {
348
        s_rem_node(SNODE en);
349
        ospf_hash_delete(n->lsrth, en);
350
        if(ifa->state==OSPF_IS_BACKUP)
351
        {
352
          if(n->rid==ifa->drid) ospf_lsa_delay(n, lsa, p);
353
        }
354
      }
355
      else
356
      {
357
        ospf_lsack_direct_tx(n,lsa);
358
      }
359
      continue;
360
    }
361

    
362
    /* pg145 (8) */
363
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
364
    {
365
      continue;
366
    }
367

    
368
    {
369
      list l;
370
      struct l_lsr_head llsh;
371
      init_list(&l);
372
      memcpy(&llsh.lsh,&lsadb->lsa,sizeof(struct ospf_lsa_header));
373
      add_tail(&l, NODE &llsh);
374
      ospf_lsupd_tx_list(n, &l);
375
    }
376
  }
377
}
378