Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ 48e5f32d

History | View | Annotate | Download (18.7 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 2000--2004 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

    
12
struct ospf_lsupd_packet
13
{
14
  struct ospf_packet ospf_packet;
15
  u32 lsano;                        /* Number of LSA's */
16
};
17

    
18

    
19
/* Beware of unaligned access */
20
void ospf_dump_lsahdr(struct proto *p, struct ospf_lsa_header *lsa_n)
21
{
22
  struct ospf_lsa_header lsa;
23
  ntohlsah(lsa_n, &lsa);
24

    
25
  log(L_TRACE "%s:     LSA      Type: %04x, Id: %R, Rt: %R, Age: %u, Seq: %08x, Sum: %04x",
26
      p->name, lsa.type, lsa.id, lsa.rt, lsa.age, lsa.sn, lsa.checksum);
27
}
28

    
29
void ospf_dump_common(struct proto *p, struct ospf_packet *op)
30
{
31
  log(L_TRACE "%s:     length   %d", p->name, ntohs(op->length));
32
  log(L_TRACE "%s:     router   %R", p->name, ntohl(op->routerid));
33
}
34

    
35
static void ospf_dump_lsupd(struct proto *p, struct ospf_lsupd_packet *pkt)
36
{
37
  struct ospf_packet *op = &pkt->ospf_packet;
38

    
39
  ASSERT(op->type == LSUPD_P);
40
  ospf_dump_common(p, op);
41

    
42
  /* We know that ntohs(op->length) >= sizeof(struct ospf_lsa_header) */
43
  u8 *pbuf= (u8 *) pkt;
44
  unsigned int offset = sizeof(struct ospf_lsupd_packet);
45
  unsigned int bound = ntohs(op->length) - sizeof(struct ospf_lsa_header);
46
  unsigned int i, j, lsalen;
47

    
48
  j = ntohl(pkt->lsano);
49
  for (i = 0; i < j; i++)
50
    {
51
      if (offset > bound)
52
        {
53
          log(L_TRACE "%s:     LSA      invalid", p->name);
54
          return;
55
        }
56

    
57
      struct ospf_lsa_header *lsa = (void *) (pbuf + offset);
58
      ospf_dump_lsahdr(p, lsa);
59
      lsalen = ntohs(lsa->length);
60
      offset += lsalen;
61

    
62
      if (((lsalen % 4) != 0) || (lsalen <= sizeof(struct ospf_lsa_header)))
63
        {
64
          log(L_TRACE "%s:     LSA      invalid", p->name);
65
          return;
66
        }
67
    }
68
}
69

    
70

    
71
#ifdef OSPFv2
72

    
73
int
74
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
75
{
76
  if (lsa->type == LSA_T_EXT)
77
    {
78
      if (ifa->type == OSPF_IT_VLINK)
79
        return 0;
80
      if (!oa_is_ext(ifa->oa))
81
        return 0;
82
      return 1;
83
    }
84
  else
85
    return ifa->oa->areaid == domain;
86
}
87

    
88
#else /* OSPFv3 */
89

    
90
static int
91
unknown_lsa_type(struct ospf_lsa_header *lsa)
92
{
93
  switch (lsa->type)
94
    {
95
    case LSA_T_RT:
96
    case LSA_T_NET:
97
    case LSA_T_SUM_NET:
98
    case LSA_T_SUM_RT:
99
    case LSA_T_EXT:
100
    case LSA_T_NSSA:
101
    case LSA_T_LINK:
102
    case LSA_T_PREFIX:
103
      return 0;
104

    
105
    default:
106
      return 1;
107
    }
108
}
109

    
110
int
111
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
112
{    
113
  u32 scope = LSA_SCOPE(lsa);
114

    
115
  /* Handle inactive vlinks */
116
  if (ifa->state == OSPF_IS_DOWN)
117
    return 0;
118

    
119
  /* 4.5.2 (Case 2) */
120
  if (unknown_lsa_type(lsa) && !(lsa->type & LSA_UBIT))
121
    scope = LSA_SCOPE_LINK;
122

    
123
  switch (scope)
124
    {
125
    case LSA_SCOPE_LINK:
126
      return ifa->iface_id == domain;
127

    
128
    case LSA_SCOPE_AREA:
129
      return ifa->oa->areaid == domain;
130

    
131
    case LSA_SCOPE_AS:
132
      if (ifa->type == OSPF_IT_VLINK)
133
        return 0;
134
      if (!oa_is_ext(ifa->oa))
135
        return 0;
136
      return 1;
137

    
138
    default:
139
      log(L_ERR "LSA with invalid scope");
140
      return 0;
141
    }
142
}
143

    
144
#endif
145

    
146
/**
147
 * ospf_lsupd_flood - send received or generated lsa to the neighbors
148
 * @po: OSPF protocol
149
 * @n: neighbor than sent this lsa (or NULL if generated)
150
 * @hn: LSA header followed by lsa body in network endianity (may be NULL) 
151
 * @hh: LSA header in host endianity (must be filled)
152
 * @domain: domain of LSA (must be filled)
153
 * @rtl: add this LSA into retransmission list
154
 *
155
 *
156
 * return value - was the LSA flooded back?
157
 */
158

    
159
int
160
ospf_lsupd_flood(struct proto_ospf *po,
161
                 struct ospf_neighbor *n, struct ospf_lsa_header *hn,
162
                 struct ospf_lsa_header *hh, u32 domain, int rtl)
163
{
164
  struct ospf_iface *ifa;
165
  struct ospf_neighbor *nn;
166
  struct top_hash_entry *en;
167
  struct proto *p = &po->proto;
168
  int ret, retval = 0;
169

    
170
  /* pg 148 */
171
  WALK_LIST(ifa, po->iface_list)
172
  {
173
    if (ifa->stub)
174
      continue;
175

    
176
    if (! ospf_lsa_flooding_allowed(hh, domain, ifa))
177
      continue;
178

    
179
    DBG("Wanted to flood LSA: Type: %u, ID: %R, RT: %R, SN: 0x%x, Age %u\n",
180
        hh->type, hh->id, hh->rt, hh->sn, hh->age);
181

    
182
    ret = 0;
183
    WALK_LIST(nn, ifa->neigh_list)
184
    {
185
      /* 13.3 (1a) */
186
      if (nn->state < NEIGHBOR_EXCHANGE)
187
        continue;
188

    
189
      /* 13.3 (1b) */
190
      if (nn->state < NEIGHBOR_FULL)
191
      {
192
        if ((en = ospf_hash_find_header(nn->lsrqh, domain, hh)) != NULL)
193
        {
194
          DBG("That LSA found in lsreq list for neigh %R\n", nn->rid);
195

    
196
          switch (lsa_comp(hh, &en->lsa))
197
          {
198
          case CMP_OLDER:
199
            continue;
200
            break;
201
          case CMP_SAME:
202
            s_rem_node(SNODE en);
203
            if (en->lsa_body != NULL)
204
              mb_free(en->lsa_body);
205
            en->lsa_body = NULL;
206
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
207
            ospf_hash_delete(nn->lsrqh, en);
208
            if ((EMPTY_SLIST(nn->lsrql)) && (nn->state == NEIGHBOR_LOADING))
209
              ospf_neigh_sm(nn, INM_LOADDONE);
210
            continue;
211
            break;
212
          case CMP_NEWER:
213
            s_rem_node(SNODE en);
214
            if (en->lsa_body != NULL)
215
              mb_free(en->lsa_body);
216
            en->lsa_body = NULL;
217
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
218
            ospf_hash_delete(nn->lsrqh, en);
219
            if ((EMPTY_SLIST(nn->lsrql)) && (nn->state == NEIGHBOR_LOADING))
220
              ospf_neigh_sm(nn, INM_LOADDONE);
221
            break;
222
          default:
223
            bug("Bug in lsa_comp?");
224
          }
225
        }
226
      }
227

    
228
      /* 13.3 (1c) */
229
      if (nn == n)
230
        continue;
231

    
232
      /* 13.3 (1d) */
233
      if (rtl)
234
      {
235
        /* In OSPFv3, there should be check whether receiving router understand
236
           that type of LSA (for LSA types with U-bit == 0). But as we does not support
237
           any optional LSA types, this is not needed yet */
238

    
239
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) == NULL)
240
        {
241
          en = ospf_hash_get_header(nn->lsrth, domain, hh);
242
        }
243
        else
244
        {
245
          s_rem_node(SNODE en);
246
        }
247
        s_add_tail(&nn->lsrtl, SNODE en);
248
        memcpy(&en->lsa, hh, sizeof(struct ospf_lsa_header));
249
        DBG("Adding that LSA for flood to %I\n", nn->ip);
250
      }
251
      else
252
      {
253
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) != NULL)
254
        {
255
          s_rem_node(SNODE en);
256
          ospf_hash_delete(nn->lsrth, en);
257
        }
258
      }
259

    
260
      ret = 1;
261
    }
262

    
263
    if (ret == 0)
264
      continue;                        /* pg 150 (2) */
265

    
266
    if (n && (n->ifa == ifa))
267
    {
268
      if ((n->rid == ifa->drid) || n->rid == ifa->bdrid)
269
        continue;                /* pg 150 (3) */
270
      if (ifa->state == OSPF_IS_BACKUP)
271
        continue;                /* pg 150 (4) */
272
      retval = 1;
273
    }
274

    
275
    {
276
      u16 len, age;
277
      struct ospf_lsupd_packet *pk;
278
      struct ospf_packet *op;
279
      struct ospf_lsa_header *lh;
280

    
281
      /* Check iface buffer size */
282
      uint len2 = sizeof(struct ospf_lsupd_packet) + (hn ? ntohs(hn->length) : hh->length);
283
      if (ospf_iface_assure_bufsize(ifa, len2) < 0)
284
      {
285
        /* Cannot fit in a tx buffer, skip that iface */
286
        log(L_ERR "OSPF: LSA too large to flood on %s (Type: %04x, Id: %R, Rt: %R)", 
287
            ifa->ifname, hh->type, hh->id, hh->rt);
288
        continue;
289
      }
290

    
291
      pk = ospf_tx_buffer(ifa);
292
      op = &pk->ospf_packet;
293

    
294
      ospf_pkt_fill_hdr(ifa, pk, LSUPD_P);
295
      pk->lsano = htonl(1);
296

    
297
      lh = (struct ospf_lsa_header *) (pk + 1);
298

    
299
      /* Copy LSA into the packet */
300
      if (hn)
301
      {
302
        memcpy(lh, hn, ntohs(hn->length));
303
      }
304
      else
305
      {
306
        u8 *help;
307
        struct top_hash_entry *en;
308

    
309
        htonlsah(hh, lh);
310
        help = (u8 *) (lh + 1);
311
        en = ospf_hash_find_header(po->gr, domain, hh);
312
        htonlsab(en->lsa_body, help, hh->length - sizeof(struct ospf_lsa_header));
313
      }
314

    
315
      len = sizeof(struct ospf_lsupd_packet) + ntohs(lh->length);
316

    
317
      age = ntohs(lh->age);
318
      age += ifa->inftransdelay;
319
      if (age > LSA_MAXAGE)
320
        age = LSA_MAXAGE;
321
      lh->age = htons(age);
322

    
323
      op->length = htons(len);
324

    
325
      OSPF_PACKET(ospf_dump_lsupd, pk, "LSUPD packet flooded via %s", ifa->ifname);
326

    
327
      switch (ifa->type)
328
      {
329
      case OSPF_IT_BCAST:
330
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
331
          ospf_send_to_all(ifa);
332
        else if (ifa->cf->real_bcast)
333
          ospf_send_to_bdr(ifa);
334
        else
335
          ospf_send_to(ifa, AllDRouters);
336
        break;
337

    
338
      case OSPF_IT_NBMA:
339
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
340
          ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
341
        else
342
          ospf_send_to_bdr(ifa);
343
        break;
344

    
345
      case OSPF_IT_PTP:
346
        ospf_send_to_all(ifa);
347
        break;
348

    
349
      case OSPF_IT_PTMP:
350
        ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
351
        break;
352

    
353
      case OSPF_IT_VLINK:
354
        ospf_send_to(ifa, ifa->vip);
355
        break;
356

    
357
      default:
358
        bug("Bug in ospf_lsupd_flood()");
359
      }
360
    }
361
  }
362
  return retval;
363
}
364

    
365
void                                /* I send all I received in LSREQ */
366
ospf_lsupd_send_list(struct ospf_neighbor *n, list * l)
367
{
368
  struct ospf_area *oa = n->ifa->oa;
369
  struct proto *p = &oa->po->proto;
370
  struct l_lsr_head *lsr;
371
  struct top_hash_entry *en;
372
  struct ospf_lsupd_packet *pkt;
373
  u32 len, len2, lsano;
374
  char *buf;
375

    
376
  pkt = ospf_tx_buffer(n->ifa);
377
  buf = (void *) pkt;
378

    
379
  lsr = HEAD(*l);
380
  while(NODE_NEXT(lsr))
381
  {
382
    /* Prepare the packet */
383
    ospf_pkt_fill_hdr(n->ifa, pkt, LSUPD_P);
384
    len = sizeof(struct ospf_lsupd_packet);
385
    lsano = 0;
386

    
387
    /* Fill the packet with LSAs */
388
    while(NODE_NEXT(lsr))
389
    {
390
      u32 domain = ospf_lsa_domain(lsr->lsh.type, n->ifa);
391
      en = ospf_hash_find(oa->po->gr, domain, lsr->lsh.id, lsr->lsh.rt, lsr->lsh.type);
392
      if (en == NULL)
393
      {
394
        /* Probably flushed LSA, this should not happen */
395
        log(L_WARN "OSPF: LSA disappeared (Type: %04x, Id: %R, Rt: %R)", 
396
            lsr->lsh.type, lsr->lsh.id, lsr->lsh.rt);
397
        lsr = NODE_NEXT(lsr);
398
        continue;                        
399
      }
400

    
401
      len2 = len + en->lsa.length;
402
      if (len2 > ospf_pkt_maxsize(n->ifa))
403
      {
404
        /* The packet if full, stop adding LSAs and sent it */
405
        if (lsano > 0)
406
          break;
407

    
408
        /* LSA is larger than MTU, check buffer size */
409
        if (ospf_iface_assure_bufsize(n->ifa, len2) < 0)
410
        {
411
          /* Cannot fit in a tx buffer, skip that */
412
          log(L_ERR "OSPF: LSA too large to send (Type: %04x, Id: %R, Rt: %R)", 
413
              lsr->lsh.type, lsr->lsh.id, lsr->lsh.rt);
414
          lsr = NODE_NEXT(lsr);
415
          continue;
416
        }
417

    
418
        /* TX buffer could be reallocated */
419
        pkt = ospf_tx_buffer(n->ifa);
420
        buf = (void *) pkt;
421
      }
422

    
423
      /* Copy the LSA to the packet */
424
      htonlsah(&(en->lsa), (struct ospf_lsa_header *) (buf + len));
425
      htonlsab(en->lsa_body, buf + len + sizeof(struct ospf_lsa_header),
426
               en->lsa.length - sizeof(struct ospf_lsa_header));
427
      len = len2;
428
      lsano++;
429
      lsr = NODE_NEXT(lsr);
430
    }
431

    
432
    if (lsano == 0)
433
      break;
434

    
435
    /* Send the packet */
436
    pkt->lsano = htonl(lsano);
437
    pkt->ospf_packet.length = htons(len);
438
    OSPF_PACKET(ospf_dump_lsupd, pkt, "LSUPD packet sent to %I via %s",
439
                n->ip, n->ifa->ifname);
440
    ospf_send_to(n->ifa, n->ip);
441
  }
442
}
443

    
444
void
445
ospf_lsupd_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
446
                   struct ospf_neighbor *n)
447
{
448

    
449
  struct ospf_neighbor *ntmp;
450
  struct proto_ospf *po = ifa->oa->po;
451
  struct proto *p = &po->proto;
452
  unsigned int i, max, sendreq = 1;
453

    
454
  unsigned int size = ntohs(ps_i->length);
455
  if (size < (sizeof(struct ospf_lsupd_packet) + sizeof(struct ospf_lsa_header)))
456
  {
457
    log(L_ERR "OSPF: Bad LSUPD packet from %I - too short (%u B)", n->ip, size);
458
    return;
459
  }
460

    
461
  struct ospf_lsupd_packet *ps = (void *) ps_i;
462
  OSPF_PACKET(ospf_dump_lsupd, ps, "LSUPD packet received from %I via %s", n->ip, ifa->ifname);
463

    
464
  if (n->state < NEIGHBOR_EXCHANGE)
465
  {
466
    OSPF_TRACE(D_PACKETS, "Received lsupd in lesser state than EXCHANGE from (%I)", n->ip);
467
    return;
468
  }
469

    
470
  ospf_neigh_sm(n, INM_HELLOREC);        /* Questionable */
471

    
472
  unsigned int offset = sizeof(struct ospf_lsupd_packet);
473
  unsigned int bound = size - sizeof(struct ospf_lsa_header);
474

    
475
  max = ntohl(ps->lsano);
476
  for (i = 0; i < max; i++)
477
  {
478
    struct ospf_lsa_header lsatmp;
479
    struct top_hash_entry *lsadb;
480

    
481
    if (offset > bound)
482
    {
483
      log(L_WARN "Received lsupd from %I is too short!", n->ip);
484
      ospf_neigh_sm(n, INM_BADLSREQ);
485
      return;
486
    }
487

    
488
    struct ospf_lsa_header *lsa = (void *) (((u8 *) ps) + offset);
489
    unsigned int lsalen = ntohs(lsa->length);
490
    offset += lsalen;
491
 
492
    if ((offset > size) || ((lsalen % 4) != 0) ||
493
        (lsalen <= sizeof(struct ospf_lsa_header)))
494
    {
495
      log(L_WARN "Received LSA from %I with bad length", n->ip);
496
      ospf_neigh_sm(n, INM_BADLSREQ);
497
      break;
498
    }
499

    
500
    /* pg 143 (1) */
501
    u16 chsum = lsa->checksum;
502
    if (chsum != lsasum_check(lsa, NULL))
503
    {
504
      log(L_WARN "Received bad lsa checksum from %I: %x %x", n->ip, chsum, lsa->checksum);
505
      continue;
506
    }
507

    
508
#ifdef OSPFv2
509
    /* pg 143 (2) */
510
    if ((lsa->type == 0) || (lsa->type == 6) || (lsa->type > LSA_T_NSSA))
511
    {
512
      log(L_WARN "Unknown LSA type from %I", n->ip);
513
      continue;
514
    }
515

    
516
    /* pg 143 (3) */
517
    if ((lsa->type == LSA_T_EXT) && !oa_is_ext(ifa->oa))
518
    {
519
      log(L_WARN "Received External LSA in stub area from %I", n->ip);
520
      continue;
521
    }
522
#else /* OSPFv3 */
523
    u16 scope = ntoht(lsa->type) & LSA_SCOPE_MASK;
524

    
525
    /* 4.5.1 (2) */
526
    if ((scope == LSA_SCOPE_AS) && !oa_is_ext(ifa->oa))
527
    {
528
      log(L_WARN "Received LSA with AS scope in stub area from %I", n->ip);
529
      continue;
530
    }
531

    
532
    /* 4.5.1 (3) */
533
    if (scope == LSA_SCOPE_RES)
534
    {
535
      log(L_WARN "Received LSA with invalid scope from %I", n->ip);
536
      continue;
537
    }
538
#endif
539

    
540
    ntohlsah(lsa, &lsatmp);
541

    
542
    DBG("Update Type: %u ID: %R RT: %R, Sn: 0x%08x Age: %u, Sum: %u\n",
543
        lsatmp.type, lsatmp.id, lsatmp.rt, lsatmp.sn, lsatmp.age, lsatmp.checksum);
544

    
545
    /* FIXME domain should be link id for unknown LSA types with zero Ubit */
546
    u32 domain = ospf_lsa_domain(lsatmp.type, ifa);
547
    lsadb = ospf_hash_find_header(po->gr, domain, &lsatmp);
548

    
549
#ifdef LOCAL_DEBUG
550
    if (lsadb)
551
      DBG("I have Type: %u ID: %R RT: %R, Sn: 0x%08x Age: %u, Sum: %u\n",
552
          lsadb->lsa.type, lsadb->lsa.id, lsadb->lsa.rt,
553
          lsadb->lsa.sn, lsadb->lsa.age, lsadb->lsa.checksum);
554
#endif
555

    
556
    /* pg 143 (4) */
557
    if ((lsatmp.age == LSA_MAXAGE) && (lsadb == NULL) && can_flush_lsa(po))
558
    {
559
      ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
560
      continue;
561
    }
562

    
563
    /* pg 144 (5) */
564
    if ((lsadb == NULL) || (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_NEWER))
565
    {
566
      struct ospf_iface *ift = NULL;
567
      int self = (lsatmp.rt == po->router_id);
568

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

    
571
#ifdef OSPFv2
572
      /* 13.4 - check self-originated LSAs of NET type */
573
      if ((!self) && (lsatmp.type == LSA_T_NET))
574
      {
575
        struct ospf_iface *nifa;
576
        WALK_LIST(nifa, po->iface_list)
577
        {
578
          if (!nifa->iface)
579
            continue;
580
          if (ipa_equal(nifa->addr->ip, ipa_from_u32(lsatmp.id)))
581
          {
582
            self = 1;
583
            break;
584
          }
585
        }
586
      }
587
#endif
588

    
589
      /* pg 145 (5f) - premature aging of self originated lsa */
590
      if (self)
591
      {
592
        if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO))
593
        {
594
          ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
595
          continue;
596
        }
597

    
598
        OSPF_TRACE(D_EVENTS, "Received old self-originated LSA (Type: %04x, Id: %R, Rt: %R)",
599
                   lsatmp.type, lsatmp.id, lsatmp.rt);
600

    
601
        if (lsadb)
602
        {
603
          OSPF_TRACE(D_EVENTS, "Reflooding new self-originated LSA with newer sequence number");
604
          lsadb->lsa.sn = lsatmp.sn + 1;
605
          lsadb->lsa.age = 0;
606
          lsadb->inst_t = now;
607
          lsadb->ini_age = 0;
608
          lsasum_calculate(&lsadb->lsa, lsadb->lsa_body);
609
          ospf_lsupd_flood(po, NULL, NULL, &lsadb->lsa, domain, 1);
610
        }
611
        else
612
        {
613
          OSPF_TRACE(D_EVENTS, "Premature aging it");
614
          lsatmp.age = LSA_MAXAGE;
615
          lsatmp.sn = LSA_MAXSEQNO;
616
          lsa->age = htons(LSA_MAXAGE);
617
          lsa->sn = htonl(LSA_MAXSEQNO);
618
          lsasum_check(lsa, (lsa + 1));        /* It also calculates chsum! */
619
          lsatmp.checksum = ntohs(lsa->checksum);
620
          ospf_lsupd_flood(po, NULL, lsa, &lsatmp, domain, 0);
621
        }
622
        continue;
623
      }
624

    
625
      /* pg 144 (5a) */
626
      if (lsadb && ((now - lsadb->inst_t) <= MINLSARRIVAL))        /* FIXME: test for flooding? */
627
      {
628
        OSPF_TRACE(D_EVENTS, "Skipping LSA received in less that MINLSARRIVAL");
629
        sendreq = 0;
630
        continue;
631
      }
632

    
633
      /* Remove old from all ret lists */
634
      /* pg 144 (5c) */
635
      /* Must be done before (5b), otherwise it also removes the new entries from (5b) */
636
      if (lsadb)
637
        WALK_LIST(ift, po->iface_list)
638
          WALK_LIST(ntmp, ift->neigh_list)
639
      {
640
        struct top_hash_entry *en;
641
        if (ntmp->state > NEIGHBOR_EXSTART)
642
          if ((en = ospf_hash_find_header(ntmp->lsrth, domain, &lsadb->lsa)) != NULL)
643
          {
644
            s_rem_node(SNODE en);
645
            ospf_hash_delete(ntmp->lsrth, en);
646
          }
647
      }
648

    
649
      /* pg 144 (5b) */
650
      if (ospf_lsupd_flood(po, n, lsa, &lsatmp, domain, 1) == 0)
651
      {
652
        DBG("Wasn't flooded back\n");        /* ps 144(5e), pg 153 */
653
        if (ifa->state == OSPF_IS_BACKUP)
654
        {
655
          if (ifa->drid == n->rid)
656
            ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
657
        }
658
        else
659
          ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
660
      }
661

    
662
      if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO)
663
          && lsadb && can_flush_lsa(po))
664
      {
665
        flush_lsa(lsadb, po);
666
        schedule_rtcalc(po);
667
        continue;
668
      }                                /* FIXME lsack? */
669

    
670
      /* pg 144 (5d) */
671
      void *body = mb_alloc(p->pool, lsatmp.length - sizeof(struct ospf_lsa_header));
672
      ntohlsab(lsa + 1, body, lsatmp.length - sizeof(struct ospf_lsa_header));
673

    
674
      /* We will do validation check after flooding and
675
         acknowledging given LSA to minimize problems
676
         when communicating with non-validating peer */
677
      if (lsa_validate(&lsatmp, body) == 0)
678
      {
679
        log(L_WARN "Received invalid LSA from %I", n->ip);
680
        mb_free(body);
681
        continue;        
682
      }
683

    
684
      lsadb = lsa_install_new(po, &lsatmp, domain, body);
685
      DBG("New LSA installed in DB\n");
686

    
687
#ifdef OSPFv3
688
      /* Events 6,7 from RFC5340 4.4.3. */
689
      if ((lsatmp.type == LSA_T_LINK) &&
690
          (ifa->state == OSPF_IS_DR))
691
        schedule_net_lsa(ifa);
692
#endif
693

    
694
      continue;
695
    }
696

    
697
    /* FIXME pg145 (6) */
698

    
699
    /* pg145 (7) */
700
    if (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_SAME)
701
    {
702
      struct top_hash_entry *en;
703
      DBG("PG145(7) Got the same LSA\n");
704
      if ((en = ospf_hash_find_header(n->lsrth, lsadb->domain, &lsadb->lsa)) != NULL)
705
      {
706
        /* pg145 (7a) */
707
        s_rem_node(SNODE en);
708
        ospf_hash_delete(n->lsrth, en);
709

    
710
        if (ifa->state == OSPF_IS_BACKUP)
711
        {
712
          if (n->rid == ifa->drid)
713
            ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
714
        }
715
      }
716
      else
717
      {
718
        /* pg145 (7b) */
719
        ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
720
      }
721
      sendreq = 0;
722
      continue;
723
    }
724

    
725
    /* pg145 (8) */
726
    if ((lsadb->lsa.age == LSA_MAXAGE) && (lsadb->lsa.sn == LSA_MAXSEQNO))
727
    {
728
      continue;
729
    }
730

    
731
    {
732
      list l;
733
      struct l_lsr_head ll;
734
      init_list(&l);
735
      ll.lsh.id = lsadb->lsa.id;
736
      ll.lsh.rt = lsadb->lsa.rt;
737
      ll.lsh.type = lsadb->lsa.type;
738
      add_tail(&l, NODE & ll);
739
      ospf_lsupd_send_list(n, &l);
740
    }
741
  }
742

    
743
  /* Send direct LSAs */
744
  ospf_lsack_send(n, ACKL_DIRECT);
745

    
746
  if (sendreq && (n->state == NEIGHBOR_LOADING))
747
  {
748
    ospf_lsreq_send(n);                /* Ask for another part of neighbor's database */
749
  }
750
}
751

    
752
void
753
ospf_lsupd_flush_nlsa(struct proto_ospf *po, struct top_hash_entry *en)
754
{
755
  struct ospf_lsa_header *lsa = &en->lsa;
756
  struct proto *p = &po->proto;
757

    
758
  lsa->age = LSA_MAXAGE;
759
  lsa->sn = LSA_MAXSEQNO;
760
  lsasum_calculate(lsa, en->lsa_body);
761
  OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa!");
762
  OSPF_TRACE(D_EVENTS, "Type: %04x, Id: %R, Rt: %R", lsa->type, lsa->id, lsa->rt);
763
  ospf_lsupd_flood(po, NULL, NULL, lsa, en->domain, 0);
764
}