Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.3 KB)

1 de769e24 Ondrej Filip
/*
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 84228eee Ondrej Filip
int
12 2c971094 Ondrej Filip
flood_lsa(struct ospf_neighbor *n, struct ospf_lsa_header *hn,
13 38130b86 Ondrej Filip
  struct ospf_lsa_header *hh, struct proto_ospf *po, struct ospf_iface *iff,
14
  struct ospf_area *oa)
15 2c971094 Ondrej Filip
{
16
  struct ospf_iface *ifa;
17
  struct ospf_neighbor *nn;
18
  struct top_hash_entry *en;
19 84228eee Ondrej Filip
  int ret,retval=0;
20 2c971094 Ondrej Filip
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 38130b86 Ondrej Filip
      if(oa->areaid==BACKBONE)
32 2c971094 Ondrej Filip
      {
33 38130b86 Ondrej Filip
        if((ifa->type!=OSPF_IT_VLINK)&&(ifa->oa!=oa)) continue;
34 2c971094 Ondrej Filip
      }
35
      else
36
      {
37 38130b86 Ondrej Filip
        if(ifa->oa!=oa) continue;
38 2c971094 Ondrej Filip
      }
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 44724025 Ondrej Filip
              DBG("Removing from lsreq list for neigh %I\n", nn->rid);
57 2c971094 Ondrej Filip
              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 44724025 Ondrej Filip
              DBG("Removing from lsreq list for neigh %I\n", nn->rid);
64 2c971094 Ondrej Filip
              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 84228eee Ondrej Filip
      if(iff->state==OSPF_IS_BACKUP) continue;
81
      retval=1;
82 2c971094 Ondrej Filip
    }
83 740d16d9 Ondrej Filip
84 2c971094 Ondrej Filip
    {
85
      sock *sk;
86
      ip_addr to;
87
      u16 len;
88
      struct ospf_lsupd_packet *pk;
89
      struct ospf_packet *op;
90
91 38130b86 Ondrej Filip
      if(ifa->type==OSPF_IT_NBMA)  sk=ifa->ip_sk;
92
      else sk=ifa->hello_sk;        /* FIXME is this true for PTP? */
93 2c971094 Ondrej Filip
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 38130b86 Ondrej Filip
      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 249fdef7 Ondrej Filip
        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 38130b86 Ondrej Filip
        en=ospf_hash_find_header(oa->gr,hh);
119 d345cda5 Ondrej Filip
        htonlsab(en->lsa_body,help,hh->type,hh->length
120
          -sizeof(struct ospf_lsa_header));
121 38130b86 Ondrej Filip
        len=hh->length;
122
      }
123 2c971094 Ondrej Filip
      op->length=htons(len);
124
      ospf_pkt_finalize(ifa, op);
125
126
      if(ifa->type==OSPF_IT_NBMA)
127
      {
128 67315ef6 Ondrej Filip
        sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
129 2c971094 Ondrej Filip
      }
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 84228eee Ondrej Filip
  return retval;
139 2c971094 Ondrej Filip
}
140
141 f45fd316 Ondrej Filip
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 9f940976 Ondrej Filip
  u8 ii;
152
  u8 *jj=n->ifa->ip_sk->tbuf;
153 f45fd316 Ondrej Filip
154 5d608eba Ondrej Filip
  if(EMPTY_LIST(*l)) return;
155 f45fd316 Ondrej Filip
156
  pk=(struct ospf_lsupd_packet *)n->ifa->ip_sk->tbuf;
157
  op=(struct ospf_packet *)n->ifa->ip_sk->tbuf;
158 9f940976 Ondrej Filip
159
  DBG("LSupd: 1st packet\n");
160 f45fd316 Ondrej Filip
       
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 44724025 Ondrej Filip
    DBG("Sending ID=%I, Type=%u, RT=%I\n", llsh->lsh.id, llsh->lsh.type,
170 9f940976 Ondrej Filip
      llsh->lsh.rt);
171 5f743d96 Ondrej Filip
    if((len+en->lsa.length)>n->ifa->iface->mtu)
172 f45fd316 Ondrej Filip
    {
173
      pk->lsano=htonl(lsano);
174
      op->length=htons(len);
175
      ospf_pkt_finalize(n->ifa, op);
176 9f940976 Ondrej Filip
                       
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 f45fd316 Ondrej Filip
      sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
181
182 9f940976 Ondrej Filip
      DBG("LSupd: next packet\n");
183 f45fd316 Ondrej Filip
      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 d345cda5 Ondrej Filip
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
191
      -sizeof(struct ospf_lsa_header));
192 9f940976 Ondrej Filip
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
193
    len=len+en->lsa.length;
194
    lsano++;
195 f45fd316 Ondrej Filip
  }
196
  pk->lsano=htonl(lsano);
197 9f940976 Ondrej Filip
  op->length=htons(len-SIPH);
198 f45fd316 Ondrej Filip
  ospf_pkt_finalize(n->ifa, op);
199 9f940976 Ondrej Filip
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 f45fd316 Ondrej Filip
  sk_send_to(n->ifa->ip_sk,len, n->ip, OSPF_PROTO);
204 9f940976 Ondrej Filip
  DBG("LSupd: sent\n");
205 f45fd316 Ondrej Filip
}
206
207 de769e24 Ondrej Filip
void
208
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
209
  struct ospf_iface *ifa, u16 size)
210
{
211 394acced Ondrej Filip
  u32 area,nrid,myrid;
212 d8852b36 Ondrej Filip
  struct ospf_neighbor *n,*ntmp;
213 394acced Ondrej Filip
  struct ospf_lsa_header *lsa;
214 8496b2e4 Ondrej Filip
  struct ospf_area *oa;
215 921a93f2 Ondrej Filip
  struct proto_ospf *po=(struct proto_ospf *)p;
216 394acced Ondrej Filip
  u16 length;
217 de769e24 Ondrej Filip
  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 44724025 Ondrej Filip
    debug("%s: Received lsupd from unknown neigbor! (%I)\n", p->name,
226 de769e24 Ondrej Filip
      nrid);
227
    return ;
228
  }
229 394acced Ondrej Filip
  if(n->state<NEIGHBOR_EXCHANGE)
230
  {
231 44724025 Ondrej Filip
    debug("%s: Received lsupd in lesser state than EXCHANGE from (%I)\n",
232 77fbd315 Ondrej Filip
      p->name,n->ip);
233 394acced Ondrej Filip
    return;
234
  }
235
236
  lsa=(struct ospf_lsa_header *)(ps+1);
237
  area=htonl(ps->ospf_packet.areaid);
238 8496b2e4 Ondrej Filip
  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 394acced Ondrej Filip
  {
242 921a93f2 Ondrej Filip
    struct ospf_lsa_header lsatmp;
243
    struct top_hash_entry *lsadb;
244 2c971094 Ondrej Filip
    /* pg 143 (1) */
245 921a93f2 Ondrej Filip
    if(lsa->checksum!=lsasum_check(lsa,NULL,po))
246 394acced Ondrej Filip
    {
247 44724025 Ondrej Filip
      log("Received bad lsa checksum from %I\n",n->rid);
248 8496b2e4 Ondrej Filip
      continue;
249
    }
250 2c971094 Ondrej Filip
    /* pg 143 (2) */
251 8496b2e4 Ondrej Filip
    if((lsa->type<LSA_T_RT)||(lsa->type>LSA_T_EXT))
252
    {
253 44724025 Ondrej Filip
      log("Unknown LSA type from %I\n",n->rid);
254 8496b2e4 Ondrej Filip
      continue;
255 394acced Ondrej Filip
    }
256 2c971094 Ondrej Filip
    /* pg 143 (3) */
257 8496b2e4 Ondrej Filip
    if((lsa->type==LSA_T_EXT)&&oa->stub)
258
    {
259 44724025 Ondrej Filip
      log("Received External LSA in stub area from %I\n",n->rid);
260 8496b2e4 Ondrej Filip
      continue;
261
    }
262 921a93f2 Ondrej Filip
    ntohlsah(lsa,&lsatmp);
263 44724025 Ondrej Filip
    DBG("Processing update Type: %u ID: %I RT: %I\n",lsatmp.type,
264 d8852b36 Ondrej Filip
        lsatmp.id, lsatmp.rt);
265 921a93f2 Ondrej Filip
    lsadb=ospf_hash_find_header(oa->gr, &lsatmp);
266 d8852b36 Ondrej Filip
267 2c971094 Ondrej Filip
    /* pg 143 (4) */
268 921a93f2 Ondrej Filip
    if((lsatmp.age==LSA_MAXAGE)&&(lsadb==NULL))
269
    {
270
      int flag=0;
271 67315ef6 Ondrej Filip
      struct ospf_iface *ifatmp;
272 921a93f2 Ondrej Filip
273 67315ef6 Ondrej Filip
      WALK_LIST(NODE ifatmp,po->iface_list)
274
        WALK_LIST(NODE ntmp,ifatmp->neigh_list)
275 d8852b36 Ondrej Filip
          if((ntmp->state==NEIGHBOR_EXCHANGE)&&
276
            (ntmp->state==NEIGHBOR_LOADING))
277 921a93f2 Ondrej Filip
            flag=1;
278 84228eee Ondrej Filip
      DBG("PG143(4): Flag=%u\n",flag);
279 921a93f2 Ondrej Filip
280
      if(flag==0)
281
      {
282 67315ef6 Ondrej Filip
        ospf_lsack_direct_tx(n,lsa);
283 921a93f2 Ondrej Filip
        continue;
284
      }
285
    }
286 2c971094 Ondrej Filip
    /* pg 144 (5) */
287 921a93f2 Ondrej Filip
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
288
    {
289 2c971094 Ondrej Filip
       struct ospf_iface *ift=NULL;
290 d8852b36 Ondrej Filip
       void *body;
291 921a93f2 Ondrej Filip
292 84228eee Ondrej Filip
       DBG("PG143(5): Received LSA is newer\n");
293 921a93f2 Ondrej Filip
294 2c971094 Ondrej Filip
      /* pg 144 (5a) */
295 85c92555 Ondrej Filip
      if(lsadb && ((now-lsadb->inst_t)<MINLSARRIVAL))
296
      {
297
        DBG("I got it in less that MINLSARRIVAL\n");
298
        continue;
299
      }
300 740d16d9 Ondrej Filip
        
301 921a93f2 Ondrej Filip
302 84228eee Ondrej Filip
      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa)==0)
303
      {
304 85c92555 Ondrej Filip
        DBG("Wasn't flooded back\n");
305 84228eee Ondrej Filip
        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 2c971094 Ondrej Filip
312 921a93f2 Ondrej Filip
      /* Remove old from all ret lists */
313 2c971094 Ondrej Filip
      /* pg 144 (5c) */
314 d8852b36 Ondrej Filip
      if(lsadb)
315 2c971094 Ondrej Filip
        WALK_LIST(NODE ift,po->iface_list)
316
          WALK_LIST(NODE ntmp,ift->neigh_list)
317 d8852b36 Ondrej Filip
          {
318
            struct top_hash_entry *en;
319 f1f7face Ondrej Filip
            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 d8852b36 Ondrej Filip
          }
326 921a93f2 Ondrej Filip
327 2c971094 Ondrej Filip
      /* pg 144 (5d) */
328 d8852b36 Ondrej Filip
      body=mb_alloc(p->pool,lsatmp.length-sizeof(struct ospf_lsa_header));
329 2c971094 Ondrej Filip
      ntohlsab(lsa+1,body,lsatmp.type,
330
        lsatmp.length-sizeof(struct ospf_lsa_header));
331 85195f1a Ondrej Filip
      lsadb=lsa_install_new(&lsatmp,body, oa, p);
332 10000b96 Ondrej Filip
      DBG("New LSA installed in DB\n");
333 d8852b36 Ondrej Filip
334 2c971094 Ondrej Filip
      /* FIXME 145 (5f) self originated? */
335
336 921a93f2 Ondrej Filip
      continue;
337
    }
338
339 2c971094 Ondrej Filip
    /* FIXME pg145 (6)?? */
340 921a93f2 Ondrej Filip
341 2c971094 Ondrej Filip
    /* pg145 (7) */
342 921a93f2 Ondrej Filip
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
343
    {
344 84228eee Ondrej Filip
      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 921a93f2 Ondrej Filip
    }
361
362 2c971094 Ondrej Filip
    /* pg145 (8) */
363 67315ef6 Ondrej Filip
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
364
    {
365
      continue;
366
    }
367 921a93f2 Ondrej Filip
368 2c971094 Ondrej Filip
    {
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 394acced Ondrej Filip
  }
377 de769e24 Ondrej Filip
}