summaryrefslogtreecommitdiffstats
path: root/proto/ospf/lsupd.c
blob: 6ae6866f273fa083db06b06401c77ac4ecc7eeb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 *	BIRD -- OSPF
 *
 *	(c) 2000 Ondrej Filip <feela@network.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

int
flood_lsa(struct ospf_neighbor *n, struct ospf_lsa_header *hn,
  struct ospf_lsa_header *hh, struct proto_ospf *po, struct ospf_iface *iff,
  struct ospf_area *oa)
{
  struct ospf_iface *ifa;
  struct ospf_neighbor *nn;
  struct top_hash_entry *en;
  struct proto *p=&po->proto;
  int ret,retval=0;

  /* pg 148 */
  WALK_LIST(NODE ifa,po->iface_list)
  {
    if(hh->type==LSA_T_EXT)
    {
      if(ifa->type==OSPF_IT_VLINK) continue;
      if(ifa->oa->stub) continue;
    }
    else
    {
      if(oa->areaid==BACKBONE)
      {
        if((ifa->type!=OSPF_IT_VLINK)&&(ifa->oa!=oa)) continue;
      }
      else
      {
        if(ifa->oa!=oa) continue;
      }
    }
    ret=0;
    WALK_LIST(NODE nn, ifa->neigh_list)
    {
      if(nn->state<NEIGHBOR_EXCHANGE) continue;
      if(nn->state<NEIGHBOR_FULL)
      {

        if((en=ospf_hash_find_header(nn->lsrqh,hh))!=NULL)
	{
	  switch(lsa_comp(hh,&en->lsa))
	  {
            case CMP_OLDER:
              continue;
	      break;
	    case CMP_SAME:
              s_rem_node(SNODE en);
	      DBG("Removing from lsreq list for neigh %I\n", nn->rid);
	      ospf_hash_delete(nn->lsrqh,en);
	      if(EMPTY_SLIST(nn->lsrql)) ospf_neigh_sm(nn, INM_LOADDONE);
	      continue;
	      break;
	    case CMP_NEWER:
              s_rem_node(SNODE en);
	      DBG("Removing from lsreq list for neigh %I\n", nn->rid);
	      ospf_hash_delete(nn->lsrqh,en);
	      if(EMPTY_SLIST(nn->lsrql)) ospf_neigh_sm(nn, INM_LOADDONE);
	      break;
	    default: bug("Bug in lsa_comp?\n");
	  }
	}
      }
      if(nn==n) continue;
      if((en=ospf_hash_find_header(nn->lsrth, hh))==NULL)
      {
        en=ospf_hash_get_header(nn->lsrth, hh);
      }
      else
      {
        s_rem_node(SNODE en);
      }
      s_add_tail(&nn->lsrtl, SNODE en);
      memcpy(&en->lsa,hh,sizeof(struct ospf_lsa_header));
      ret=1;
      DBG("Adding LSA lsrt RT: %I, Id: %I, Type: %u for n: %I\n",
        en->lsa.rt,en->lsa.id, en->lsa.type, nn->ip);
    }
    if(ret==0) continue;
    if(ifa==iff)
    {
      if((n->rid==iff->drid)||n->rid==iff->bdrid) continue;
      if(iff->state==OSPF_IS_BACKUP) continue;
      retval=1;
    }

    {
      sock *sk;
      ip_addr to;
      u16 len;
      struct ospf_lsupd_packet *pk;
      struct ospf_packet *op;

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

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

      fill_ospf_pkt_hdr(ifa, pk, LSUPD);
      pk->lsano=htonl(1);
      if(hn!=NULL)
      {
        memcpy(pk+1,hn,ntohs(hn->length));
        len=sizeof(struct ospf_lsupd_packet)+ntohs(hn->length);
      }
      else
      {
        u8 *help;
	struct top_hash_entry *en;
	struct ospf_lsa_header *lh;
	u16 age;

	lh=(struct ospf_lsa_header *)(pk+1);
        htonlsah(hh,lh);
	age=hh->age;
	age+=ifa->inftransdelay;
	if(age>LSA_MAXAGE) age=LSA_MAXAGE;
	lh->age=htons(age);
	help=(u8 *)(lh+1);
	en=ospf_hash_find_header(oa->gr,hh);
	htonlsab(en->lsa_body,help,hh->type,hh->length
          -sizeof(struct ospf_lsa_header));
	len=hh->length+sizeof(struct ospf_lsupd_packet);
      }
      op->length=htons(len);
      ospf_pkt_finalize(ifa, op);
      debug("%s: LS upd flooded via %s\n", p->name, ifa->iface->name);

      if(ifa->type==OSPF_IT_NBMA)
      {
        sk_send_to_agt(sk ,len, ifa, NEIGHBOR_EXCHANGE);
      }
      else
      {
        if((ifa->state==OSPF_IS_BACKUP)||(ifa->state==OSPF_IS_DR))
          sk_send_to(sk,len, AllSPFRouters, OSPF_PROTO);
	else sk_send_to(sk,len, AllDRouters, OSPF_PROTO);
      }
    }
  }
  return retval;
}

void		/* I send all I received in LSREQ */
ospf_lsupd_tx_list(struct ospf_neighbor *n, list *l)
{
  struct l_lsr_head *llsh;
  u16 len;
  u32 lsano;
  struct top_hash_entry *en;
  struct ospf_lsupd_packet *pk;
  struct ospf_packet *op;
  struct proto *p=&n->ifa->oa->po->proto;
  void *pktpos;
  u8 ii;
  u8 *jj=n->ifa->ip_sk->tbuf;

  if(EMPTY_LIST(*l)) return;

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

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

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

    DBG("Sending ID=%I, Type=%u, RT=%I\n", llsh->lsh.id, llsh->lsh.type,
      llsh->lsh.rt);
    if((len+en->lsa.length)>n->ifa->iface->mtu)
    {
      pk->lsano=htonl(lsano);
      op->length=htons(len-SIPH);
      ospf_pkt_finalize(n->ifa, op);
		       
      sk_send_to(n->ifa->ip_sk,len-SIPH, n->ip, OSPF_PROTO);
      debug("%s: LS upd sent to %I\n", p->name, n->ip);

      DBG("LSupd: next packet\n");
      fill_ospf_pkt_hdr(n->ifa, pk, LSUPD);
      len=SIPH+sizeof(struct ospf_lsupd_packet);
      lsano=0;
      pktpos=(pk+1);
    }
    htonlsah(&(en->lsa), pktpos);
    pktpos=pktpos+sizeof(struct ospf_lsa_header);
    htonlsab(en->lsa_body, pktpos, en->lsa.type, en->lsa.length
      -sizeof(struct ospf_lsa_header));
    pktpos=pktpos+en->lsa.length-sizeof(struct ospf_lsa_header);
    len+=en->lsa.length;
    lsano++;
  }
  pk->lsano=htonl(lsano);
  op->length=htons(len-SIPH);
  ospf_pkt_finalize(n->ifa, op);

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

void
ospf_lsupd_rx(struct ospf_lsupd_packet *ps, struct proto *p,
  struct ospf_iface *ifa, u16 size)
{
  u32 area,nrid,myrid;
  struct ospf_neighbor *n,*ntmp;
  struct ospf_lsa_header *lsa;
  struct ospf_area *oa;
  struct proto_ospf *po=(struct proto_ospf *)p;
  u16 length;
  u8 i;

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

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


  if((n=find_neigh(ifa, nrid))==NULL)
  {
    debug("%s: Received lsupd from unknown neigbor! (%I)\n", p->name,
      nrid);
    return ;
  }
  if(n->state<NEIGHBOR_EXCHANGE)
  {
    debug("%s: Received lsupd in lesser state than EXCHANGE from (%I)\n",
      p->name,n->ip);
    return;
  }
  if(size<=(sizeof(struct ospf_lsupd_packet)+sizeof(struct ospf_lsa_header)))
  {
    log("%s: Received lsupd from %I is too short\n", p->name,n->ip);
    return;
  }

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

  lsa=(struct ospf_lsa_header *)(ps+1);
  area=htonl(ps->ospf_packet.areaid);
  oa=ospf_find_area((struct proto_ospf *)p,area);
  for(i=0;i<ntohl(ps->lsano);i++,
    lsa=(struct ospf_lsa_header *)(((u8 *)lsa)+ntohs(lsa->length)))
  {
    struct ospf_lsa_header lsatmp;
    struct top_hash_entry *lsadb;
    u16 lenn;
    int diff=((u8 *)lsa)-((u8 *)ps);

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

    lenn=ntohs(lsa->length);

    if((lenn<=sizeof(struct ospf_lsa_header))||(lenn!=(4*(lenn/4))))
    {
      log("Received LSA with bad length\n");
      ospf_neigh_sm(n,INM_BADLSREQ);
      break;
    }
    /* pg 143 (1) */
    if(lsa->checksum!=lsasum_check(lsa,NULL,po))
    {
      log("Received bad lsa checksum from %I\n",n->rid);
      continue;
    }
    /* pg 143 (2) */
    if((lsa->type<LSA_T_RT)||(lsa->type>LSA_T_EXT))
    {
      log("Unknown LSA type from %I\n",n->rid);
      continue;
    }
    /* pg 143 (3) */
    if((lsa->type==LSA_T_EXT)&&oa->stub)
    {
      log("Received External LSA in stub area from %I\n",n->rid);
      continue;
    }
    ntohlsah(lsa,&lsatmp);
    DBG("Processing update Type: %u ID: %I RT: %I\n",lsatmp.type,
        lsatmp.id, lsatmp.rt);
    lsadb=ospf_hash_find_header(oa->gr, &lsatmp);

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

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

      if(flag==0)
      {
        ospf_lsack_direct_tx(n,lsa);
	continue;
      }
    }
    /* pg 144 (5) */
    if((lsadb==NULL)||(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_NEWER))
    {
       struct ospf_iface *ift=NULL;
       void *body;
       struct ospf_iface *nifa;
       int self=0;

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

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

       if(lsatmp.type==LSA_T_NET)
       {
         WALK_LIST(nifa,po->iface_list)
	 {
	   if(ipa_compare(nifa->iface->addr->ip,ipa_from_u32(lsatmp.id))==0)
	   {
	     self=1;
	     break;
	   }
	 }
       }

       if(self)
       {
         lsa->age=(htons(LSA_MAXAGE));
	 lsatmp.age=LSA_MAXAGE;
	 debug("Premature aging self originated lsa.\n");
         flood_lsa(NULL,lsa,&lsatmp,po,NULL,oa);
	 continue;
       }



      /* pg 144 (5a) */
      if(lsadb && ((now-lsadb->inst_t)<MINLSARRIVAL))
      {
        DBG("I got it in less that MINLSARRIVAL\n");
	continue;
      }
        

      if(flood_lsa(n,lsa,&lsatmp,po,ifa,ifa->oa)==0)
      {
        DBG("Wasn't flooded back\n");
        if(ifa->state==OSPF_IS_BACKUP)
	{
	  if(ifa->drid==n->rid) ospf_lsa_delay(n, lsa, p);
	}
	else ospf_lsa_delay(n, lsa, p);
      }

      /* Remove old from all ret lists */
      /* pg 144 (5c) */
      if(lsadb)
        WALK_LIST(NODE ift,po->iface_list)
          WALK_LIST(NODE ntmp,ift->neigh_list)
	  {
	    struct top_hash_entry *en;
	    if(ntmp->state>NEIGHBOR_EXSTART)
	      if((en=ospf_hash_find_header(ntmp->lsrth,&lsadb->lsa))!=NULL)
	      {
	        s_rem_node(SNODE en);
	        ospf_hash_delete(ntmp->lsrth,en);
	      }
	  }

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

      continue;
    }

    /* FIXME pg145 (6)?? */

    /* pg145 (7) */
    if(lsa_comp(&lsatmp,&lsadb->lsa)==CMP_SAME)
    {
      struct top_hash_entry *en;
      DBG("PG145(6) Got the same LSA\n");
      if((en=ospf_hash_find_header(n->lsrth,&lsadb->lsa))!=NULL)
      {
        s_rem_node(SNODE en);
        ospf_hash_delete(n->lsrth, en);
        if(ifa->state==OSPF_IS_BACKUP)
        {
          if(n->rid==ifa->drid) ospf_lsa_delay(n, lsa, p);
        }
      }
      else
      {
        ospf_lsack_direct_tx(n,lsa);
      }
      continue;
    }

    /* pg145 (8) */
    if((lsadb->lsa.age==LSA_MAXAGE)&&(lsadb->lsa.sn==LSA_MAXSEQNO))
    {
      continue;
    }

    {
      list l;
      struct l_lsr_head llsh;
      init_list(&l);
      memcpy(&llsh.lsh,&lsadb->lsa,sizeof(struct ospf_lsa_header));
      add_tail(&l, NODE &llsh);
      ospf_lsupd_tx_list(n, &l);
    }
  }
}

void
net_flush_lsa(struct top_hash_entry *en, struct proto_ospf *po,
  struct ospf_area *oa)
{
  struct ospf_lsa_header *lsa=&en->lsa;

  lsa->age=LSA_MAXAGE;
  debug("Premature aging self originated lsa.\n");
  flood_lsa(NULL,NULL,lsa,po,NULL,oa);
}