Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / lsupd.c @ e81b440f

History | View | Annotate | Download (17.9 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;
47

    
48
  j = ntohl(pkt->lsano);
49
  for (i = 0; i < j; i++)
50
    {
51
      if ((offset > bound) || ((offset % 4) != 0))
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
      offset += ntohs(lsa->length);
60
    }
61
}
62

    
63

    
64
#ifdef OSPFv2
65

    
66
int
67
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
68
{
69
  if (lsa->type == LSA_T_EXT)
70
    {
71
      if (ifa->type == OSPF_IT_VLINK)
72
        return 0;
73
      if (ifa->oa->stub)
74
        return 0;
75
      return 1;
76
    }
77
  else
78
    return ifa->oa->areaid == domain;
79
}
80

    
81
#else /* OSPFv3 */
82

    
83
static int
84
unknown_lsa_type(struct ospf_lsa_header *lsa)
85
{
86
  switch (lsa->type)
87
    {
88
    case LSA_T_RT:
89
    case LSA_T_NET:
90
    case LSA_T_SUM_NET:
91
    case LSA_T_SUM_RT:
92
    case LSA_T_EXT:
93
    case LSA_T_LINK:
94
    case LSA_T_PREFIX:
95
      return 0;
96

    
97
    default:
98
      return 1;
99
    }
100
}
101

    
102
int
103
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
104
{    
105
  u32 scope = LSA_SCOPE(lsa);
106

    
107
  /* 4.5.2 (Case 2) */
108
  if (unknown_lsa_type(lsa) && !(lsa->type & LSA_UBIT))
109
    scope = LSA_SCOPE_LINK;
110

    
111
  switch (scope)
112
    {
113
    case LSA_SCOPE_LINK:
114
      return ifa->iface->index == domain;
115

    
116
    case LSA_SCOPE_AREA:
117
      return ifa->oa->areaid == domain;
118

    
119
    case LSA_SCOPE_AS:
120
      if (ifa->type == OSPF_IT_VLINK)
121
        return 0;
122
      if (ifa->oa->stub)
123
        return 0;
124
      return 1;
125

    
126
    default:
127
      log(L_ERR "LSA with invalid scope");
128
      return 0;
129
    }
130
}
131

    
132
#endif
133

    
134
/**
135
 * ospf_lsupd_flood - send received or generated lsa to the neighbors
136
 * @po: OSPF protocol
137
 * @n: neighbor than sent this lsa (or NULL if generated)
138
 * @hn: LSA header followed by lsa body in network endianity (may be NULL) 
139
 * @hh: LSA header in host endianity (must be filled)
140
 * @domain: domain of LSA (must be filled)
141
 * @rtl: add this LSA into retransmission list
142
 *
143
 *
144
 * return value - was the LSA flooded back?
145
 */
146

    
147
int
148
ospf_lsupd_flood(struct proto_ospf *po,
149
                 struct ospf_neighbor *n, struct ospf_lsa_header *hn,
150
                 struct ospf_lsa_header *hh, u32 domain, int rtl)
151
{
152
  struct ospf_iface *ifa;
153
  struct ospf_neighbor *nn;
154
  struct top_hash_entry *en;
155
  struct proto *p = &po->proto;
156
  int ret, retval = 0;
157

    
158
  /* pg 148 */
159
  WALK_LIST(ifa, po->iface_list)
160
  {
161
    if (ifa->stub)
162
      continue;
163

    
164
    if (! ospf_lsa_flooding_allowed(hh, domain, ifa))
165
      continue;
166

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

    
170
    ret = 0;
171
    WALK_LIST(nn, ifa->neigh_list)
172
    {
173
      /* 13.3 (1a) */
174
      if (nn->state < NEIGHBOR_EXCHANGE)
175
        continue;
176

    
177
      /* 13.3 (1b) */
178
      if (nn->state < NEIGHBOR_FULL)
179
      {
180
        if ((en = ospf_hash_find_header(nn->lsrqh, domain, hh)) != NULL)
181
        {
182
          DBG("That LSA found in lsreq list for neigh %R\n", nn->rid);
183

    
184
          switch (lsa_comp(hh, &en->lsa))
185
          {
186
          case CMP_OLDER:
187
            continue;
188
            break;
189
          case CMP_SAME:
190
            s_rem_node(SNODE en);
191
            if (en->lsa_body != NULL)
192
              mb_free(en->lsa_body);
193
            en->lsa_body = NULL;
194
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
195
            ospf_hash_delete(nn->lsrqh, en);
196
            if (EMPTY_SLIST(nn->lsrql))
197
              ospf_neigh_sm(nn, INM_LOADDONE);
198
            continue;
199
            break;
200
          case CMP_NEWER:
201
            s_rem_node(SNODE en);
202
            if (en->lsa_body != NULL)
203
              mb_free(en->lsa_body);
204
            en->lsa_body = NULL;
205
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
206
            ospf_hash_delete(nn->lsrqh, en);
207
            if (EMPTY_SLIST(nn->lsrql))
208
              ospf_neigh_sm(nn, INM_LOADDONE);
209
            break;
210
          default:
211
            bug("Bug in lsa_comp?");
212
          }
213
        }
214
      }
215

    
216
      /* 13.3 (1c) */
217
      if (nn == n)
218
        continue;
219

    
220
      /* 13.3 (1d) */
221
      if (rtl)
222
      {
223
        /* In OSPFv3, there should be check whether receiving router understand
224
           that type of LSA (for LSA types with U-bit == 0). But as we does not support
225
           any optional LSA types, this is not needed yet */
226

    
227
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) == NULL)
228
        {
229
          en = ospf_hash_get_header(nn->lsrth, domain, hh);
230
        }
231
        else
232
        {
233
          s_rem_node(SNODE en);
234
        }
235
        s_add_tail(&nn->lsrtl, SNODE en);
236
        memcpy(&en->lsa, hh, sizeof(struct ospf_lsa_header));
237
        DBG("Adding that LSA for flood to %I\n", nn->ip);
238
      }
239
      else
240
      {
241
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) != NULL)
242
        {
243
          s_rem_node(SNODE en);
244
          if (en->lsa_body != NULL)
245
            mb_free(en->lsa_body);
246
          en->lsa_body = NULL;
247
          ospf_hash_delete(nn->lsrth, en);
248
        }
249
      }
250

    
251
      ret = 1;
252
    }
253

    
254
    if (ret == 0)
255
      continue;                        /* pg 150 (2) */
256

    
257
    if (n && (n->ifa == ifa))
258
    {
259
      if ((n->rid == ifa->drid) || n->rid == ifa->bdrid)
260
        continue;                /* pg 150 (3) */
261
      if (ifa->state == OSPF_IS_BACKUP)
262
        continue;                /* pg 150 (4) */
263
      retval = 1;
264
    }
265

    
266
    {
267
      u16 len, age;
268
      struct ospf_lsupd_packet *pk;
269
      struct ospf_packet *op;
270
      struct ospf_lsa_header *lh;
271

    
272
      pk = (struct ospf_lsupd_packet *) ifa->sk->tbuf;
273
      op = (struct ospf_packet *) ifa->sk->tbuf;
274

    
275
      ospf_pkt_fill_hdr(ifa, pk, LSUPD_P);
276
      pk->lsano = htonl(1);
277

    
278
      lh = (struct ospf_lsa_header *) (pk + 1);
279

    
280
      /* Copy LSA into the packet */
281
      if (hn)
282
      {
283
        memcpy(lh, hn, ntohs(hn->length));
284
      }
285
      else
286
      {
287
        u8 *help;
288
        struct top_hash_entry *en;
289

    
290
        htonlsah(hh, lh);
291
        help = (u8 *) (lh + 1);
292
        en = ospf_hash_find_header(po->gr, domain, hh);
293
        htonlsab(en->lsa_body, help, hh->length - sizeof(struct ospf_lsa_header));
294
      }
295

    
296
      len = sizeof(struct ospf_lsupd_packet) + ntohs(lh->length);
297

    
298
      age = ntohs(lh->age);
299
      age += ifa->inftransdelay;
300
      if (age > LSA_MAXAGE)
301
        age = LSA_MAXAGE;
302
      lh->age = htons(age);
303

    
304
      op->length = htons(len);
305

    
306
      OSPF_PACKET(ospf_dump_lsupd,  (struct ospf_lsupd_packet *) ifa->sk->tbuf,
307
                  "LSUPD packet flooded via %s", ifa->iface->name);
308

    
309
      switch (ifa->type)
310
      {
311
      case OSPF_IT_NBMA:
312
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
313
          ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
314
        else
315
          ospf_send_to_bdr(ifa);
316
        break;
317

    
318
      case OSPF_IT_VLINK:
319
        ospf_send_to(ifa, ifa->vip);
320
        break;
321

    
322
      default:
323
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR) ||
324
            (ifa->type == OSPF_IT_PTP))
325
          ospf_send_to(ifa, AllSPFRouters);
326
        else
327
          ospf_send_to(ifa, AllDRouters);
328
      }
329
    }
330
  }
331
  return retval;
332
}
333

    
334
void                                /* I send all I received in LSREQ */
335
ospf_lsupd_send_list(struct ospf_neighbor *n, list * l)
336
{
337
  struct l_lsr_head *llsh;
338
  u16 len;
339
  u32 lsano;
340
  struct top_hash_entry *en;
341
  struct ospf_lsupd_packet *pk;
342
  struct ospf_packet *op;
343
  struct ospf_area *oa = n->ifa->oa;
344
  struct proto_ospf *po = oa->po;
345
  struct proto *p = &po->proto;
346
  void *pktpos;
347

    
348
  if (EMPTY_LIST(*l))
349
    return;
350

    
351
  pk = (struct ospf_lsupd_packet *) n->ifa->sk->tbuf;
352
  op = (struct ospf_packet *) n->ifa->sk->tbuf;
353

    
354
  DBG("LSupd: 1st packet\n");
355

    
356
  ospf_pkt_fill_hdr(n->ifa, pk, LSUPD_P);
357
  len = sizeof(struct ospf_lsupd_packet);
358
  lsano = 0;
359
  pktpos = (pk + 1);
360

    
361
  WALK_LIST(llsh, *l)
362
  {
363
    u32 domain = ospf_lsa_domain(llsh->lsh.type, n->ifa);
364
    if ((en = ospf_hash_find(po->gr, domain, llsh->lsh.id,
365
                               llsh->lsh.rt, llsh->lsh.type)) == NULL)
366
      continue;                        /* Probably flushed LSA */
367
    /* FIXME This is a bug! I cannot flush LSA that is in lsrt */
368

    
369
    DBG("Sending LSA: Type=%u, ID=%R, RT=%R, SN: 0x%x, Age: %u\n",
370
        llsh->lsh.type, llsh->lsh.id, llsh->lsh.rt, en->lsa.sn, en->lsa.age);
371
    if (((u32) (len + en->lsa.length)) > ospf_pkt_maxsize(n->ifa))
372
    {
373
      pk->lsano = htonl(lsano);
374
      op->length = htons(len);
375

    
376
      OSPF_PACKET(ospf_dump_lsupd,  (struct ospf_lsupd_packet *) n->ifa->sk->tbuf,
377
                  "LSUPD packet sent to %I via %s", n->ip, n->ifa->iface->name);
378
      ospf_send_to(n->ifa, n->ip);
379

    
380
      DBG("LSupd: next packet\n");
381
      ospf_pkt_fill_hdr(n->ifa, pk, LSUPD_P);
382
      len = sizeof(struct ospf_lsupd_packet);
383
      lsano = 0;
384
      pktpos = (pk + 1);
385
    }
386
    htonlsah(&(en->lsa), pktpos);
387
    pktpos = pktpos + sizeof(struct ospf_lsa_header);
388
    htonlsab(en->lsa_body, pktpos, en->lsa.length - sizeof(struct ospf_lsa_header));
389
    pktpos = pktpos + en->lsa.length - sizeof(struct ospf_lsa_header);
390
    len += en->lsa.length;
391
    lsano++;
392
  }
393
  if (lsano > 0)
394
  {
395
    pk->lsano = htonl(lsano);
396
    op->length = htons(len);
397

    
398
    OSPF_PACKET(ospf_dump_lsupd,  (struct ospf_lsupd_packet *) n->ifa->sk->tbuf,
399
                "LSUPD packet sent to %I via %s", n->ip, n->ifa->iface->name);
400
    ospf_send_to(n->ifa, n->ip);
401
  }
402
}
403

    
404
void
405
ospf_lsupd_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
406
                   struct ospf_neighbor *n)
407
{
408

    
409
  struct ospf_neighbor *ntmp;
410
  struct proto_ospf *po = ifa->oa->po;
411
  struct proto *p = &po->proto;
412
  unsigned int i, max, sendreq = 1;
413

    
414
  unsigned int size = ntohs(ps_i->length);
415
  if (size < (sizeof(struct ospf_lsupd_packet) + sizeof(struct ospf_lsa_header)))
416
  {
417
    log(L_ERR "Bad OSPF LSUPD packet from %I -  too short (%u B)", n->ip, size);
418
    return;
419
  }
420

    
421
  struct ospf_lsupd_packet *ps = (void *) ps_i;
422
  OSPF_PACKET(ospf_dump_lsupd, ps, "LSUPD packet received from %I via %s", n->ip, ifa->iface->name);
423

    
424
  if (n->state < NEIGHBOR_EXCHANGE)
425
  {
426
    OSPF_TRACE(D_PACKETS, "Received lsupd in lesser state than EXCHANGE from (%I)", n->ip);
427
    return;
428
  }
429

    
430
  ospf_neigh_sm(n, INM_HELLOREC);        /* Questionable */
431

    
432
  unsigned int offset = sizeof(struct ospf_lsupd_packet);
433
  unsigned int bound = size - sizeof(struct ospf_lsa_header);
434

    
435
  max = ntohl(ps->lsano);
436
  for (i = 0; i < max; i++)
437
  {
438
    struct ospf_lsa_header lsatmp;
439
    struct top_hash_entry *lsadb;
440

    
441
    if (offset > bound)
442
    {
443
      log(L_WARN "Received lsupd from %I is too short!", n->ip);
444
      ospf_neigh_sm(n, INM_BADLSREQ);
445
      return;
446
    }
447

    
448
    struct ospf_lsa_header *lsa = (void *) (((u8 *) ps) + offset);
449
    unsigned int lsalen = ntohs(lsa->length);
450
    offset += lsalen;
451
 
452
    if ((offset > size) || ((lsalen % 4) != 0) ||
453
        (lsalen <= sizeof(struct ospf_lsa_header)))
454
    {
455
      log(L_WARN "Received LSA from %I with bad length", n->ip);
456
      ospf_neigh_sm(n, INM_BADLSREQ);
457
      break;
458
    }
459

    
460
    /* pg 143 (1) */
461
    u16 chsum = lsa->checksum;
462
    if (chsum != lsasum_check(lsa, NULL))
463
    {
464
      log(L_WARN "Received bad lsa checksum from %I: %x %x", n->ip, chsum, lsa->checksum);
465
      continue;
466
    }
467

    
468
#ifdef OSPFv2
469
    /* pg 143 (2) */
470
    if ((lsa->type < LSA_T_RT) || (lsa->type > LSA_T_EXT))
471
    {
472
      log(L_WARN "Unknown LSA type from %I", n->ip);
473
      continue;
474
    }
475

    
476
    /* pg 143 (3) */
477
    if ((lsa->type == LSA_T_EXT) && ifa->oa->stub)
478
    {
479
      log(L_WARN "Received External LSA in stub area from %I", n->ip);
480
      continue;
481
    }
482
#else /* OSPFv3 */
483
    /* 4.5.1 (2) */
484
    if ((LSA_SCOPE(lsa) == LSA_SCOPE_AS) && ifa->oa->stub)
485
    {
486
      log(L_WARN "Received LSA with AS scope in stub area from %I", n->ip);
487
      continue;
488
    }
489

    
490
    /* 4.5.1 (3) */
491
    if ((LSA_SCOPE(lsa) == LSA_SCOPE_RES))
492
    {
493
      log(L_WARN "Received LSA with invalid scope from %I", n->ip);
494
      continue;
495
    }
496
#endif
497

    
498
    ntohlsah(lsa, &lsatmp);
499

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

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

    
507
#ifdef LOCAL_DEBUG
508
    if (lsadb)
509
      DBG("I have Type: %u ID: %R RT: %R, Sn: 0x%08x Age: %u, Sum: %u\n",
510
          lsadb->lsa.type, lsadb->lsa.id, lsadb->lsa.rt,
511
          lsadb->lsa.sn, lsadb->lsa.age, lsadb->lsa.checksum);
512
#endif
513

    
514
    /* pg 143 (4) */
515
    if ((lsatmp.age == LSA_MAXAGE) && (lsadb == NULL) && can_flush_lsa(po))
516
    {
517
      ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
518
      continue;
519
    }
520

    
521
    /* pg 144 (5) */
522
    if ((lsadb == NULL) || (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_NEWER))
523
    {
524
      struct ospf_iface *ift = NULL;
525
      int self = (lsatmp.rt == po->router_id);
526

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

    
529
#ifdef OSPFv2
530
      /* 13.4 - check self-originated LSAs of NET type */
531
      if ((!self) && (lsatmp.type == LSA_T_NET))
532
      {
533
        struct ospf_iface *nifa;
534
        WALK_LIST(nifa, po->iface_list)
535
        {
536
          if (!nifa->iface)
537
            continue;
538
          if (ipa_equal(nifa->iface->addr->ip, ipa_from_u32(lsatmp.id)))
539
          {
540
            self = 1;
541
            break;
542
          }
543
        }
544
      }
545
#endif
546

    
547
      /* pg 145 (5f) - premature aging of self originated lsa */
548
      if (self)
549
      {
550
        if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO))
551
        {
552
          ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
553
          continue;
554
        }
555

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

    
559
        if (lsadb)
560
        {
561
          OSPF_TRACE(D_EVENTS, "Reflooding new self-originated LSA with newer sequence number");
562
          lsadb->lsa.sn = lsatmp.sn + 1;
563
          lsadb->lsa.age = 0;
564
          lsadb->inst_t = now;
565
          lsadb->ini_age = 0;
566
          lsasum_calculate(&lsadb->lsa, lsadb->lsa_body);
567
          ospf_lsupd_flood(po, NULL, NULL, &lsadb->lsa, domain, 1);
568
        }
569
        else
570
        {
571
          OSPF_TRACE(D_EVENTS, "Premature aging it");
572
          lsatmp.age = LSA_MAXAGE;
573
          lsatmp.sn = LSA_MAXSEQNO;
574
          lsa->age = htons(LSA_MAXAGE);
575
          lsa->sn = htonl(LSA_MAXSEQNO);
576
          lsasum_check(lsa, (lsa + 1));        /* It also calculates chsum! */
577
          lsatmp.checksum = ntohs(lsa->checksum);
578
          ospf_lsupd_flood(po, NULL, lsa, &lsatmp, domain, 0);
579
        }
580
        continue;
581
      }
582

    
583
      /* pg 144 (5a) */
584
      if (lsadb && ((now - lsadb->inst_t) <= MINLSARRIVAL))        /* FIXME: test for flooding? */
585
      {
586
        OSPF_TRACE(D_EVENTS, "Skipping LSA received in less that MINLSARRIVAL");
587
        sendreq = 0;
588
        continue;
589
      }
590

    
591
      if (ospf_lsupd_flood(po, n, lsa, &lsatmp, domain, 1) == 0)
592
      {
593
        DBG("Wasn't flooded back\n");        /* ps 144(5e), pg 153 */
594
        if (ifa->state == OSPF_IS_BACKUP)
595
        {
596
          if (ifa->drid == n->rid)
597
            ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
598
        }
599
        else
600
          ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
601
      }
602

    
603
      /* Remove old from all ret lists */
604
      /* pg 144 (5c) */
605
      if (lsadb)
606
        WALK_LIST(ift, po->iface_list)
607
          WALK_LIST(ntmp, ift->neigh_list)
608
      {
609
        struct top_hash_entry *en;
610
        if (ntmp->state > NEIGHBOR_EXSTART)
611
          if ((en = ospf_hash_find_header(ntmp->lsrth, domain, &lsadb->lsa)) != NULL)
612
          {
613
            s_rem_node(SNODE en);
614
            if (en->lsa_body != NULL)
615
              mb_free(en->lsa_body);
616
            en->lsa_body = NULL;
617
            ospf_hash_delete(ntmp->lsrth, en);
618
          }
619
      }
620

    
621
      if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO)
622
          && lsadb && can_flush_lsa(po))
623
      {
624
        flush_lsa(lsadb, po);
625
        schedule_rtcalc(po);
626
        continue;
627
      }                                /* FIXME lsack? */
628

    
629
      /* pg 144 (5d) */
630
      void *body = mb_alloc(p->pool, lsatmp.length - sizeof(struct ospf_lsa_header));
631
      ntohlsab(lsa + 1, body, lsatmp.length - sizeof(struct ospf_lsa_header));
632

    
633
      /* We will do validation check after flooding and
634
         acknowledging given LSA to minimize problems
635
         when communicating with non-validating peer */
636
      if (lsa_validate(&lsatmp, body) == 0)
637
      {
638
        log(L_WARN "Received invalid LSA from %I", n->ip);
639
        mb_free(body);
640
        continue;        
641
      }
642

    
643
      lsadb = lsa_install_new(po, &lsatmp, domain, body);
644
      DBG("New LSA installed in DB\n");
645

    
646
#ifdef OSPFv3
647
      /* Events 6,7 from RFC5340 4.4.3. */
648
      if ((lsatmp.type == LSA_T_LINK) &&
649
          (ifa->state == OSPF_IS_DR))
650
        schedule_net_lsa(ifa);
651
#endif
652

    
653
      continue;
654
    }
655

    
656
    /* FIXME pg145 (6) */
657

    
658
    /* pg145 (7) */
659
    if (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_SAME)
660
    {
661
      struct top_hash_entry *en;
662
      DBG("PG145(7) Got the same LSA\n");
663
      if ((en = ospf_hash_find_header(n->lsrth, lsadb->domain, &lsadb->lsa)) != NULL)
664
      {
665
        /* pg145 (7a) */
666
        s_rem_node(SNODE en);
667
        if (en->lsa_body != NULL)
668
          mb_free(en->lsa_body);
669
        en->lsa_body = NULL;
670
        ospf_hash_delete(n->lsrth, en);
671
        if (ifa->state == OSPF_IS_BACKUP)
672
        {
673
          if (n->rid == ifa->drid)
674
            ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
675
        }
676
      }
677
      else
678
      {
679
        /* pg145 (7b) */
680
        ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
681
      }
682
      sendreq = 0;
683
      continue;
684
    }
685

    
686
    /* pg145 (8) */
687
    if ((lsadb->lsa.age == LSA_MAXAGE) && (lsadb->lsa.sn == LSA_MAXSEQNO))
688
    {
689
      continue;
690
    }
691

    
692
    {
693
      list l;
694
      struct l_lsr_head ll;
695
      init_list(&l);
696
      ll.lsh.id = lsadb->lsa.id;
697
      ll.lsh.rt = lsadb->lsa.rt;
698
      ll.lsh.type = lsadb->lsa.type;
699
      add_tail(&l, NODE & ll);
700
      ospf_lsupd_send_list(n, &l);
701
    }
702
  }
703

    
704
  /* Send direct LSAs */
705
  ospf_lsack_send(n, ACKL_DIRECT);
706

    
707
  if (sendreq && (n->state == NEIGHBOR_LOADING))
708
  {
709
    ospf_lsreq_send(n);                /* Ask for another part of neighbor's database */
710
  }
711
}
712

    
713
void
714
ospf_lsupd_flush_nlsa(struct proto_ospf *po, struct top_hash_entry *en)
715
{
716
  struct ospf_lsa_header *lsa = &en->lsa;
717
  struct proto *p = &po->proto;
718

    
719
  lsa->age = LSA_MAXAGE;
720
  lsa->sn = LSA_MAXSEQNO;
721
  lsasum_calculate(lsa, en->lsa_body);
722
  OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa!");
723
  OSPF_TRACE(D_EVENTS, "Type: %04x, Id: %R, Rt: %R", lsa->type, lsa->id, lsa->rt);
724
  ospf_lsupd_flood(po, NULL, NULL, lsa, en->domain, 0);
725
}