summaryrefslogtreecommitdiff
path: root/proto/ospf/lsupd.c
blob: 75c670248cfcee3ef964042b92fd463855a029bb (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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
 *	BIRD -- OSPF
 *
 *	(c) 2000--2004 Ondrej Filip <feela@network.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"


struct ospf_lsupd_packet
{
  struct ospf_packet ospf_packet;
  u32 lsano;			/* Number of LSA's */
};


/* Beware of unaligned access */
void ospf_dump_lsahdr(struct proto *p, struct ospf_lsa_header *lsa_n)
{
  struct ospf_lsa_header lsa;
  ntohlsah(lsa_n, &lsa);

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

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

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

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

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

  j = ntohl(pkt->lsano);
  for (i = 0; i < j; i++)
    {
      if ((offset > bound) || ((offset % 4) != 0))
	{
	  log(L_TRACE "%s:     LSA      invalid", p->name);
	  return;
	}

      struct ospf_lsa_header *lsa = (void *) (pbuf + offset);
      ospf_dump_lsahdr(p, lsa);
      offset += ntohs(lsa->length);
    }
}


#ifdef OSPFv2

int
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
{
  if (lsa->type == LSA_T_EXT)
    {
      if (ifa->type == OSPF_IT_VLINK)
	return 0;
      if (ifa->oa->stub)
	return 0;
      return 1;
    }
  else
    return ifa->oa->areaid == domain;
}

#else /* OSPFv3 */

static int
unknown_lsa_type(struct ospf_lsa_header *lsa)
{
  switch (lsa->type)
    {
    case LSA_T_RT:
    case LSA_T_NET:
    case LSA_T_SUM_NET:
    case LSA_T_SUM_RT:
    case LSA_T_EXT:
    case LSA_T_LINK:
    case LSA_T_PREFIX:
      return 0;

    default:
      return 1;
    }
}

int
ospf_lsa_flooding_allowed(struct ospf_lsa_header *lsa, u32 domain, struct ospf_iface *ifa)
{    
  u32 scope = LSA_SCOPE(lsa);

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

  switch (scope)
    {
    case LSA_SCOPE_LINK:
      return ifa->iface->index == domain;

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

    case LSA_SCOPE_AS:
      if (ifa->type == OSPF_IT_VLINK)
	return 0;
      if (ifa->oa->stub)
	return 0;
      return 1;

    default:
      log(L_ERR "LSA with invalid scope");
      return 0;
    }
}

#endif

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

int
ospf_lsupd_flood(struct proto_ospf *po,
		 struct ospf_neighbor *n, struct ospf_lsa_header *hn,
		 struct ospf_lsa_header *hh, u32 domain, int rtl)
{
  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(ifa, po->iface_list)
  {
    if (ifa->stub)
      continue;

    if (! ospf_lsa_flooding_allowed(hh, domain, ifa))
      continue;

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

    ret = 0;
    WALK_LIST(nn, ifa->neigh_list)
    {
      /* 13.3 (1a) */
      if (nn->state < NEIGHBOR_EXCHANGE)
	continue;

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

	  switch (lsa_comp(hh, &en->lsa))
	  {
	  case CMP_OLDER:
	    continue;
	    break;
	  case CMP_SAME:
	    s_rem_node(SNODE en);
	    if (en->lsa_body != NULL)
	      mb_free(en->lsa_body);
	    en->lsa_body = NULL;
	    DBG("Removing from lsreq list for neigh %R\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);
	    if (en->lsa_body != NULL)
	      mb_free(en->lsa_body);
	    en->lsa_body = NULL;
	    DBG("Removing from lsreq list for neigh %R\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?");
	  }
	}
      }

      /* 13.3 (1c) */
      if (nn == n)
	continue;

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

	if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) == NULL)
	{
	  en = ospf_hash_get_header(nn->lsrth, domain, hh);
	}
	else
	{
	  s_rem_node(SNODE en);
	}
	s_add_tail(&nn->lsrtl, SNODE en);
	memcpy(&en->lsa, hh, sizeof(struct ospf_lsa_header));
	DBG("Adding that LSA for flood to %I\n", nn->ip);
      }
      else
      {
	if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) != NULL)
	{
	  s_rem_node(SNODE en);
	  if (en->lsa_body != NULL)
	    mb_free(en->lsa_body);
	  en->lsa_body = NULL;
	  ospf_hash_delete(nn->lsrth, en);
	}
      }

      ret = 1;
    }

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

    if (n && (n->ifa == ifa))
    {
      if ((n->rid == ifa->drid) || n->rid == ifa->bdrid)
	continue;		/* pg 150 (3) */
      if (ifa->state == OSPF_IS_BACKUP)
	continue;		/* pg 150 (4) */
      retval = 1;
    }

    {
      u16 len, age;
      struct ospf_lsupd_packet *pk;
      struct ospf_packet *op;
      struct ospf_lsa_header *lh;

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

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

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

      /* Copy LSA into the packet */
      if (hn)
      {
	memcpy(lh, hn, ntohs(hn->length));
      }
      else
      {
	u8 *help;
	struct top_hash_entry *en;

	htonlsah(hh, lh);
	help = (u8 *) (lh + 1);
	en = ospf_hash_find_header(po->gr, domain, hh);
	htonlsab(en->lsa_body, help, hh->type, hh->length
		 - sizeof(struct ospf_lsa_header));
      }

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

      age = ntohs(lh->age);
      age += ifa->inftransdelay;
      if (age > LSA_MAXAGE)
	age = LSA_MAXAGE;
      lh->age = htons(age);

      op->length = htons(len);

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

      switch (ifa->type)
      {
      case OSPF_IT_NBMA:
	if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
	  ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
	else
	  ospf_send_to_bdr(ifa);
	break;

      case OSPF_IT_VLINK:
	ospf_send_to(ifa, ifa->vip);
	break;

      default:
	if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR) ||
	    (ifa->type == OSPF_IT_PTP))
	  ospf_send_to(ifa, AllSPFRouters);
	else
	  ospf_send_to(ifa, AllDRouters);
      }
    }
  }
  return retval;
}

void				/* I send all I received in LSREQ */
ospf_lsupd_send_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 ospf_area *oa = n->ifa->oa;
  struct proto_ospf *po = oa->po;
  struct proto *p = &po->proto;
  void *pktpos;

  if (EMPTY_LIST(*l))
    return;

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

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

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

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

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

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

      DBG("LSupd: next packet\n");
      ospf_pkt_fill_hdr(n->ifa, pk, LSUPD_P);
      len = 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++;
  }
  if (lsano > 0)
  {
    pk->lsano = htonl(lsano);
    op->length = htons(len);

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

void
ospf_lsupd_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
		   struct ospf_neighbor *n)
{

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

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

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

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

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

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

  max = ntohl(ps->lsano);
  for (i = 0; i < max; i++)
  {
    struct ospf_lsa_header lsatmp;
    struct top_hash_entry *lsadb;

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

    struct ospf_lsa_header *lsa = (void *) (((u8 *) ps) + offset);
    unsigned int lsalen = ntohs(lsa->length);
    offset += lsalen;
 
    if ((offset > size) || ((lsalen % 4) != 0) ||
	(lsalen <= sizeof(struct ospf_lsa_header)))
    {
      log(L_WARN "Received LSA from %I with bad length", n->ip);
      ospf_neigh_sm(n, INM_BADLSREQ);
      break;
    }

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

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

    /* pg 143 (3) */
    if ((lsa->type == LSA_T_EXT) && ifa->oa->stub)
    {
      log(L_WARN "Received External LSA in stub area from %I", n->ip);
      continue;
    }
#else /* OSPFv3 */
    /* 4.5.1 (2) */
    if ((LSA_SCOPE(lsa) == LSA_SCOPE_AS) && ifa->oa->stub)
    {
      log(L_WARN "Received LSA with AS scope in stub area from %I", n->ip);
      continue;
    }

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

    ntohlsah(lsa, &lsatmp);

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

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

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

    /* pg 143 (4) */
    if ((lsatmp.age == LSA_MAXAGE) && (lsadb == NULL) && can_flush_lsa(po))
    {
      ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
      continue;
    }

    /* pg 144 (5) */
    if ((lsadb == NULL) || (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_NEWER))
    {
      struct ospf_iface *ift = NULL;
      int self = (lsatmp.rt == p->cf->global->router_id);

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

#ifdef OSPFv2
      /* 13.4 - check self-originated LSAs of NET type */
      if ((!self) && (lsatmp.type == LSA_T_NET))
      {
	struct ospf_iface *nifa;
	WALK_LIST(nifa, po->iface_list)
	{
	  if (!nifa->iface)
	    continue;
	  if (ipa_equal(nifa->iface->addr->ip, ipa_from_u32(lsatmp.id)))
	  {
	    self = 1;
	    break;
	  }
	}
      }
#endif

      /* pg 145 (5f) - premature aging of self originated lsa */
      if (self)
      {
	struct top_hash_entry *en;

	if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO))
	{
	  ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
	  continue;
	}

	lsatmp.age = LSA_MAXAGE;
	lsatmp.sn = LSA_MAXSEQNO;
	lsa->age = htons(LSA_MAXAGE);
	lsa->sn = htonl(LSA_MAXSEQNO);
	OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa.");
	OSPF_TRACE(D_EVENTS, "Type: %d, Id: %R, Rt: %R",
		   lsatmp.type, lsatmp.id, lsatmp.rt);
	lsasum_check(lsa, (lsa + 1));	/* It also calculates chsum! */
	lsatmp.checksum = ntohs(lsa->checksum);
	ospf_lsupd_flood(po, NULL, lsa, &lsatmp, domain, 0);
	if (en = ospf_hash_find_header(po->gr, domain, &lsatmp))
	{ /* FIXME verify hacks */
	  ospf_lsupd_flood(po, NULL, NULL, &en->lsa, domain, 1);
	}
	continue;
      }

      /* pg 144 (5a) */
      if (lsadb && ((now - lsadb->inst_t) <= MINLSARRIVAL))	/* FIXME: test for flooding? */
      {
	DBG("I got it in less that MINLSARRIVAL\n");
	sendreq = 0;
	continue;
      }

      if (ospf_lsupd_flood(po, n, lsa, &lsatmp, domain, 1) == 0)
      {
	DBG("Wasn't flooded back\n");	/* ps 144(5e), pg 153 */
	if (ifa->state == OSPF_IS_BACKUP)
	{
	  if (ifa->drid == n->rid)
	    ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
	}
	else
	  ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
      }

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

      if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO)
	  && lsadb && can_flush_lsa(po))
      {
	flush_lsa(lsadb, po);
	schedule_rtcalc(po);
	continue;
      }				/* FIXME lsack? */

      /* pg 144 (5d) */
      void *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));

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

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

#ifdef OSPFv3
      /* Events 6,7 from 4.4.3. */
      if ((lsatmp.type == LSA_T_LINK) &&
	  (ifa->state == OSPF_IS_DR))
	schedule_net_lsa(ifa);
#endif

      continue;
    }

    /* FIXME pg145 (6) */

    /* pg145 (7) */
    if (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_SAME)
    {
      struct top_hash_entry *en;
      DBG("PG145(7) Got the same LSA\n");
      if ((en = ospf_hash_find_header(n->lsrth, lsadb->domain, &lsadb->lsa)) != NULL)
      {
	/* pg145 (7a) */
	s_rem_node(SNODE en);
	if (en->lsa_body != NULL)
	  mb_free(en->lsa_body);
	en->lsa_body = NULL;
	ospf_hash_delete(n->lsrth, en);
	if (ifa->state == OSPF_IS_BACKUP)
	{
	  if (n->rid == ifa->drid)
	    ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
	}
      }
      else
      {
	/* pg145 (7b) */
	ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
      }
      sendreq = 0;
      continue;
    }

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

    {
      list l;
      struct l_lsr_head ll;
      init_list(&l);
      ll.lsh.id = lsadb->lsa.id;
      ll.lsh.rt = lsadb->lsa.rt;
      ll.lsh.type = lsadb->lsa.type;
      add_tail(&l, NODE & ll);
      ospf_lsupd_send_list(n, &l);
    }
  }

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

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

void
ospf_lsupd_flush_nlsa(struct proto_ospf *po, struct top_hash_entry *en)
{
  struct ospf_lsa_header *lsa = &en->lsa;
  struct proto *p = &po->proto;

  lsa->age = LSA_MAXAGE;
  lsa->sn = LSA_MAXSEQNO;
  lsasum_calculate(lsa, en->lsa_body);
  OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa!");
  OSPF_TRACE(D_EVENTS, "Type: %d, Id: %R, Rt: %R", lsa->type, lsa->id, lsa->rt);
  ospf_lsupd_flood(po, NULL, NULL, lsa, en->domain, 0);
}