summaryrefslogtreecommitdiff
path: root/proto/ospf/lsupd.c
blob: 12c9cfaafb14e4d4347083b30656caf79e1bb0fe (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
721
722
723
724
/*
 *	BIRD -- OSPF
 *
 *	(c) 2000--2004 Ondrej Filip <feela@network.cz>
 *	(c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
 *	(c) 2009--2014 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"


/*
struct ospf_lsupd_packet
{
  struct ospf_packet hdr;
  // union ospf_auth auth;

  u32 lsa_count;
  void lsas[];
};
*/


void
ospf_dump_lsahdr(struct ospf_proto *p, struct ospf_lsa_header *lsa_n)
{
  struct ospf_lsa_header lsa;
  u32 lsa_etype;

  lsa_ntoh_hdr(lsa_n, &lsa);
  lsa_etype = lsa_get_etype(&lsa, p);

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

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

static inline uint
ospf_lsupd_hdrlen(struct ospf_proto *p)
{
  return ospf_pkt_hdrlen(p) + 4; /* + u32 lsa count field */
}

static inline u32
ospf_lsupd_get_lsa_count(struct ospf_packet *pkt, uint hdrlen)
{
  u32 *c = ((void *) pkt) + hdrlen - 4;
  return ntohl(*c);
}

static inline void
ospf_lsupd_set_lsa_count(struct ospf_packet *pkt, uint hdrlen, u32 val)
{
  u32 *c = ((void *) pkt) + hdrlen - 4;
  *c = htonl(val);
}

static inline void
ospf_lsupd_body(struct ospf_proto *p, struct ospf_packet *pkt,
		uint *offset, uint *lsa_count)
{
  uint hlen = ospf_lsupd_hdrlen(p);
  *offset = hlen;
  *lsa_count = ospf_lsupd_get_lsa_count(pkt, hlen);
}

static void
ospf_dump_lsupd(struct ospf_proto *p, struct ospf_packet *pkt)
{
  uint offset, plen, i, lsa_count, lsa_len;

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

  plen = ntohs(pkt->length);
  ospf_lsupd_body(p, pkt, &offset, &lsa_count);
  for (i = 0; i < lsa_count; i++)
  {
    if ((offset + sizeof(struct ospf_lsa_header)) > plen)
      goto invalid;

    struct ospf_lsa_header *lsa = ((void *) pkt) + offset;
    lsa_len = ntohs(lsa->length);

    if (((lsa_len % 4) != 0) || (lsa_len <= sizeof(struct ospf_lsa_header)))
      goto invalid;

    ospf_dump_lsahdr(p, lsa);
    offset += lsa_len;
  }
  return;

invalid:
  log(L_TRACE "%s:     LSA      invalid", p->p.name);
  return;
}


static inline void
ospf_lsa_lsrq_down(struct top_hash_entry *req, struct ospf_neighbor *n)
{
  if (req == n->lsrqi)
    n->lsrqi = SNODE_NEXT(req);

  s_rem_node(SNODE req);
  ospf_hash_delete(n->lsrqh, req);

  if (EMPTY_SLIST(n->lsrql))
  {
    tm_stop(n->lsrq_timer);

    if (n->state == NEIGHBOR_LOADING)
      ospf_neigh_sm(n, INM_LOADDONE);
  }
}

static inline void
ospf_lsa_lsrt_up(struct top_hash_entry *en, struct ospf_neighbor *n)
{
  struct top_hash_entry *ret = ospf_hash_get_entry(n->lsrth, en);

  if (!SNODE_VALID(ret))
  {
    en->ret_count++;
    s_add_tail(&n->lsrtl, SNODE ret);
  }

  ret->lsa = en->lsa;
  ret->lsa_body = LSA_BODY_DUMMY;

  if (!tm_active(n->lsrt_timer))
    tm_start(n->lsrt_timer, n->ifa->rxmtint S);
}

void
ospf_lsa_lsrt_down_(struct top_hash_entry *en, struct ospf_neighbor *n, struct top_hash_entry *ret)
{
  if (en)
    en->ret_count--;

  s_rem_node(SNODE ret);
  ospf_hash_delete(n->lsrth, ret);

  if (EMPTY_SLIST(n->lsrtl))
    tm_stop(n->lsrt_timer);
}

static inline int
ospf_lsa_lsrt_down(struct top_hash_entry *en, struct ospf_neighbor *n)
{
  struct top_hash_entry *ret = ospf_hash_find_entry(n->lsrth, en);

  if (ret)
    ospf_lsa_lsrt_down_(en, n, ret);

  return ret != NULL;
}

void
ospf_add_flushed_to_lsrt(struct ospf_proto *p, struct ospf_neighbor *n)
{
  struct top_hash_entry *en;

  WALK_SLIST(en, p->lsal)
    if ((en->lsa.age == LSA_MAXAGE) && (en->lsa_body != NULL) &&
	lsa_flooding_allowed(en->lsa_type, en->domain, n->ifa) &&
	lsa_is_acceptable(en->lsa_type, n, p))
      ospf_lsa_lsrt_up(en, n);

  /* If we found any flushed LSA, we send them ASAP */
  if (tm_active(n->lsrt_timer))
    tm_start(n->lsrt_timer, 0);
}

static int ospf_flood_lsupd(struct ospf_proto *p, struct top_hash_entry **lsa_list, uint lsa_count, uint lsa_min_count, struct ospf_iface *ifa);

static void
ospf_enqueue_lsa(struct ospf_proto *p, struct top_hash_entry *en, struct ospf_iface *ifa)
{
  /* Exception for local Grace-LSA, they are flooded synchronously */
  if ((en->lsa_type == LSA_T_GR) && (en->lsa.rt == p->router_id))
  {
    ospf_flood_lsupd(p, &en, 1, 1, ifa);
    return;
  }

  if (ifa->flood_queue_used == ifa->flood_queue_size)
  {
    /* If we already have full queue, we send some packets */
    uint sent = ospf_flood_lsupd(p, ifa->flood_queue, ifa->flood_queue_used, ifa->flood_queue_used / 2, ifa);
    uint i;

    for (i = 0; i < sent; i++)
      ifa->flood_queue[i]->ret_count--;

    ifa->flood_queue_used -= sent;
    memmove(ifa->flood_queue, ifa->flood_queue + sent, ifa->flood_queue_used * sizeof(void *));
    bzero(ifa->flood_queue + ifa->flood_queue_used, sent * sizeof(void *));
  }

  en->ret_count++;
  ifa->flood_queue[ifa->flood_queue_used] = en;
  ifa->flood_queue_used++;

  if (!ev_active(p->flood_event))
    ev_schedule(p->flood_event);
}

void
ospf_flood_event(void *ptr)
{
  struct ospf_proto *p = ptr;
  struct ospf_iface *ifa;
  int i, count;

  WALK_LIST(ifa, p->iface_list)
  {
    if (ifa->flood_queue_used == 0)
      continue;

    count = ifa->flood_queue_used;
    ospf_flood_lsupd(p, ifa->flood_queue, count, count, ifa);

    for (i = 0; i < count; i++)
      ifa->flood_queue[i]->ret_count--;

    ifa->flood_queue_used = 0;
    bzero(ifa->flood_queue, count * sizeof(void *));
  }
}


/**
 * ospf_flood_lsa - send LSA to the neighbors
 * @p: OSPF protocol instance
 * @en: LSA entry
 * @from: neighbor than sent this LSA (or NULL if LSA is local)
 *
 * return value - was the LSA flooded back?
 */
int
ospf_flood_lsa(struct ospf_proto *p, struct top_hash_entry *en, struct ospf_neighbor *from)
{
  struct ospf_iface *ifa;
  struct ospf_neighbor *n;

  /* RFC 2328 13.3 */

  int back = 0;
  WALK_LIST(ifa, p->iface_list)
  {
    if (ifa->stub)
      continue;

    if (! lsa_flooding_allowed(en->lsa_type, en->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);

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

      /* 13.3 (1b) */
      if (n->state < NEIGHBOR_FULL)
      {
	struct top_hash_entry *req = ospf_hash_find_entry(n->lsrqh, en);
	if (req != NULL)
	{
	  int cmp = lsa_comp(&en->lsa, &req->lsa);

	  /* If same or newer, remove LSA from the link state request list */
	  if (cmp > CMP_OLDER)
	    ospf_lsa_lsrq_down(req, n);

	  /* If older or same, skip processing of this neighbor */
	  if (cmp < CMP_NEWER)
	    continue;
	}
      }

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

      /* Check whether optional LSAs are supported by neighbor */
      if (!lsa_is_acceptable(en->lsa_type, n, p))
	continue;

      /* 13.3 (1d) - add LSA to the link state retransmission list */
      ospf_lsa_lsrt_up(en, n);

      used = 1;
    }

    /* 13.3 (2) */
    if (!used)
      continue;

    if (from && (from->ifa == ifa))
    {
      /* 13.3 (3) */
      if ((from->rid == ifa->drid) || (from->rid == ifa->bdrid))
	continue;

      /* 13.3 (4) */
      if (ifa->state == OSPF_IS_BACKUP)
	continue;

      back = 1;
    }

    /* 13.3 (5) - finally flood the packet */
    ospf_enqueue_lsa(p, en, ifa);
  }

  return back;
}

static uint
ospf_prepare_lsupd(struct ospf_proto *p, struct ospf_iface *ifa,
		   struct top_hash_entry **lsa_list, uint lsa_count)
{
  struct ospf_packet *pkt;
  uint hlen, pos, i, maxsize;

  pkt = ospf_tx_buffer(ifa);
  hlen = ospf_lsupd_hdrlen(p);
  maxsize = ospf_pkt_maxsize(ifa);

  ospf_pkt_fill_hdr(ifa, pkt, LSUPD_P);
  pos = hlen;

  for (i = 0; i < lsa_count; i++)
  {
    struct top_hash_entry *en = lsa_list[i];
    uint len = en->lsa.length;

    if ((pos + len) > maxsize)
    {
      /* The packet if full, stop adding LSAs and sent it */
      if (i > 0)
	break;

      /* LSA is larger than MTU, check buffer size */
      if (ospf_iface_assure_bufsize(ifa, pos + len) < 0)
      {
	/* Cannot fit in a tx buffer, skip that */
	log(L_ERR "%s: LSA too large to send on %s (Type: %04x, Id: %R, Rt: %R)",
	    p->p.name, ifa->ifname, en->lsa_type, en->lsa.id, en->lsa.rt);
	break;
      }

      /* TX buffer could be reallocated */
      pkt = ospf_tx_buffer(ifa);
    }

    struct ospf_lsa_header *buf = ((void *) pkt) + pos;
    lsa_hton_hdr(&en->lsa, buf);
    lsa_hton_body(en->lsa_body, ((void *) buf) + sizeof(struct ospf_lsa_header),
		  len - sizeof(struct ospf_lsa_header));
    buf->age = htons(MIN(en->lsa.age + ifa->inftransdelay, LSA_MAXAGE));

    pos += len;
  }

  ospf_lsupd_set_lsa_count(pkt, hlen, i);
  pkt->length = htons(pos);

  return i;
}


static int
ospf_flood_lsupd(struct ospf_proto *p, struct top_hash_entry **lsa_list, uint lsa_count, uint lsa_min_count, struct ospf_iface *ifa)
{
  uint i, c;

  for (i = 0; i < lsa_min_count; i += c)
  {
    c = ospf_prepare_lsupd(p, ifa, lsa_list + i, lsa_count - i);

    if (!c)	/* Too large LSA */
      { i++; continue; }

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

    ospf_send_to_iface(ifa);
  }

  return i;
}

int
ospf_send_lsupd(struct ospf_proto *p, struct top_hash_entry **lsa_list, uint lsa_count, struct ospf_neighbor *n)
{
  struct ospf_iface *ifa = n->ifa;
  uint i, c;

  for (i = 0; i < lsa_count; i += c)
  {
    c = ospf_prepare_lsupd(p, ifa, lsa_list + i, lsa_count - i);

    if (!c)	/* Too large LSA */
      { i++; continue; }

    OSPF_PACKET(ospf_dump_lsupd, ospf_tx_buffer(ifa),
		"LSUPD packet sent to nbr %R on %s", n->rid, ifa->ifname);

    ospf_send_to_nbr(ifa, n);
  }

  return i;
}

void
ospf_rxmt_lsupd(struct ospf_proto *p, struct ospf_neighbor *n)
{
  uint max = 2 * n->ifa->flood_queue_size;
  struct top_hash_entry *entries[max];
  struct top_hash_entry *ret, *nxt, *en;
  uint i = 0;

  /* ASSERT((n->state >= NEIGHBOR_EXCHANGE) && !EMPTY_SLIST(n->lsrtl)); */

  WALK_SLIST_DELSAFE(ret, nxt, n->lsrtl)
  {
    if (i == max)
      break;

    en = ospf_hash_find_entry(p->gr, ret);
    if (!en)
    {
      /* Probably flushed LSA, this should not happen */
      log(L_WARN "%s: LSA disappeared (Type: %04x, Id: %R, Rt: %R)",
	  p->p.name, ret->lsa_type, ret->lsa.id, ret->lsa.rt);

      s_rem_node(SNODE ret);
      ospf_hash_delete(n->lsrth, ret);

      continue;
    }

    entries[i] = en;
    i++;
  }

  ospf_send_lsupd(p, entries, i, n);
}


static inline int
ospf_addr_is_local(struct ospf_proto *p, struct ospf_area *oa, ip_addr ip)
{
  struct ospf_iface *ifa;
  WALK_LIST(ifa, p->iface_list)
    if ((ifa->oa == oa) && ifa->addr && ipa_equal(ifa->addr->ip, ip))
      return 1;

  return 0;
}

void
ospf_receive_lsupd(struct ospf_packet *pkt, struct ospf_iface *ifa,
		   struct ospf_neighbor *n)
{
  struct ospf_proto *p = ifa->oa->po;
  const char *err_dsc = NULL;
  uint plen, err_val = 0;

  /* RFC 2328 13. */

  plen = ntohs(pkt->length);
  if (plen < ospf_lsupd_hdrlen(p))
  {
    LOG_PKT("Bad LSUPD packet from nbr %R on %s - %s (%u)", n->rid, ifa->ifname, "too short", plen);
    return;
  }

  OSPF_PACKET(ospf_dump_lsupd, pkt, "LSUPD packet received from nbr %R on %s", n->rid, ifa->ifname);

  if (n->state < NEIGHBOR_EXCHANGE)
  {
    OSPF_TRACE(D_PACKETS, "LSUPD packet ignored - lesser state than Exchange");
    return;
  }

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

  uint offset, i, lsa_count;
  ospf_lsupd_body(p, pkt, &offset, &lsa_count);

  for (i = 0; i < lsa_count; i++)
  {
    struct ospf_lsa_header lsa, *lsa_n;
    struct top_hash_entry *en;
    u32 lsa_len, lsa_type, lsa_domain;

    if ((offset + sizeof(struct ospf_lsa_header)) > plen)
      DROP("too short", plen);

    /* LSA header in network order */
    lsa_n = ((void *) pkt) + offset;
    lsa_len = ntohs(lsa_n->length);
    offset += lsa_len;

    if (offset > plen)
      DROP("too short", plen);

    if (((lsa_len % 4) != 0) || (lsa_len <= sizeof(struct ospf_lsa_header)))
      DROP("invalid LSA length", lsa_len);

    /* LSA header in host order */
    lsa_ntoh_hdr(lsa_n, &lsa);
    lsa_get_type_domain(&lsa, ifa, &lsa_type, &lsa_domain);

    DBG("Update Type: %04x, Id: %R, Rt: %R, Sn: 0x%08x, Age: %u, Sum: %u\n",
	lsa_type, lsa.id, lsa.rt, lsa.sn, lsa.age, lsa.checksum);

    /* RFC 2328 13. (1) - verify LSA checksum */
    if ((lsa_n->checksum == 0) || !lsa_verify_checksum(lsa_n, lsa_len))
      SKIP("invalid checksum");

    /* RFC 2328 13. (2) */
    if (!lsa_type)
      SKIP("unknown type");

    /* RFC 5340 4.5.1 (2) and RFC 2328 13. (3) */
    if (!oa_is_ext(ifa->oa) && (LSA_SCOPE(lsa_type) == LSA_SCOPE_AS))
      SKIP("AS scope in stub area");

    /* Errata 3746 to RFC 2328 - rt-summary-LSAs forbidden in stub areas */
    if (!oa_is_ext(ifa->oa) && (lsa_type == LSA_T_SUM_RT))
      SKIP("rt-summary-LSA in stub area");

    /* RFC 5340 4.5.1 (3) */
    if (LSA_SCOPE(lsa_type) == LSA_SCOPE_RES)
      SKIP("invalid scope");

    /* Find local copy of LSA in link state database */
    en = ospf_hash_find(p->gr, lsa_domain, lsa.id, lsa.rt, lsa_type);

#ifdef LOCAL_DEBUG
    if (en)
      DBG("I have Type: %04x, Id: %R, Rt: %R, Sn: 0x%08x, Age: %u, Sum: %u\n",
	  en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn, en->lsa.age, en->lsa.checksum);
#endif

    /* 13. (4) - ignore maxage LSA if i have no local copy */
    if ((lsa.age == LSA_MAXAGE) && !en && (p->padj == 0))
    {
      /* 13.5. - schedule ACKs (tbl 19, case 5) */
      ospf_enqueue_lsack(n, lsa_n, ACKL_DIRECT);
      continue;
    }

    /* 13. (5) - received LSA is newer (or no local copy) */
    if (!en || (lsa_comp(&lsa, &en->lsa) == CMP_NEWER))
    {
      /* 13. (5a) - enforce minimum time between updates for received LSAs */
      /* We also use this to ratelimit reactions to received self-originated LSAs */
      if (en && (lsa_inst_age(en) < MINLSARRIVAL))
      {
	OSPF_TRACE(D_EVENTS, "Skipping LSA received in less that MinLSArrival");
	continue;
      }

      /* Copy and validate LSA body */
      int blen = lsa.length - sizeof(struct ospf_lsa_header);
      void *body = mb_alloc(p->p.pool, blen);
      lsa_ntoh_body(lsa_n + 1, body, blen);

      if (lsa_validate(&lsa, lsa_type, ospf_is_v2(p), body) == 0)
      {
	mb_free(body);
	SKIP("invalid body");
      }

      /* 13. (5f) - handle self-originated LSAs, see also 13.4. */
      if (!p->gr_recovery &&
	  ((lsa.rt == p->router_id) ||
	   (ospf_is_v2(p) && (lsa_type == LSA_T_NET) && ospf_addr_is_local(p, ifa->oa, ipa_from_u32(lsa.id)))))
      {
	OSPF_TRACE(D_EVENTS, "Received unexpected self-originated LSA");
	ospf_advance_lsa(p, en, &lsa, lsa_type, lsa_domain, body);
	continue;
      }

      /* 13. (5c) - remove old LSA from all retransmission lists
       *
       * We only need to remove it from the retransmission list of the neighbor
       * that send us the new LSA. The old LSA is automatically replaced in
       * retransmission lists by the new LSA.
       */
      if (en)
	ospf_lsa_lsrt_down(en, n);

#if 0
      /*
       * Old code for removing LSA from all retransmission lists. Must be done
       * before (5b), otherwise it also removes the new entries from (5b).
       */
      struct ospf_iface *ifi;
      struct ospf_neighbor *ni;

      WALK_LIST(ifi, p->iface_list)
	WALK_LIST(ni, ifi->neigh_list)
	  if (ni->state > NEIGHBOR_EXSTART)
	    ospf_lsa_lsrt_down(en, ni);
#endif

      /* 13. (5d) - install new LSA into database */
      en = ospf_install_lsa(p, &lsa, lsa_type, lsa_domain, body);

      /* RFC 5340 4.4.3 Events 6+7 - new Link LSA received */
      if (lsa_type == LSA_T_LINK)
	ospf_notify_net_lsa(ifa);

      /* RFC 3623 3.1 - entering graceful restart helper mode */
      if (lsa_type == LSA_T_GR)
	ospf_neigh_notify_grace_lsa(n, en);

      /* Link received pre-restart router LSA */
      if (p->gr_recovery && (lsa_type == LSA_T_RT) && (lsa.rt == p->router_id))
	ifa->oa->rt = en;

      /* 13. (5b) - flood new LSA */
      int flood_back = ospf_flood_lsa(p, en, n);

      /* 13.5. - schedule ACKs (tbl 19, cases 1+2) */
      if (! flood_back)
	if ((ifa->state != OSPF_IS_BACKUP) || (n->rid == ifa->drid))
	  ospf_enqueue_lsack(n, lsa_n, ACKL_DELAY);

      /* FIXME: remove LSA entry if it is LSA_MAXAGE and it is possible? */

      continue;
    }

    /* 13. (6) - received LSA is in Link state request list (but not newer) */
    if (ospf_hash_find_entry(n->lsrqh, en) != NULL)
      DROP1("error in LSA database exchange");

    /* 13. (7) - received LSA is same */
    if (lsa_comp(&lsa, &en->lsa) == CMP_SAME)
    {
      /* Duplicate LSA, treat as implicit ACK */
      int implicit_ack = ospf_lsa_lsrt_down(en, n);

      /* 13.5. - schedule ACKs (tbl 19, cases 3+4) */
      if (implicit_ack)
      {
	if ((ifa->state == OSPF_IS_BACKUP) && (n->rid == ifa->drid))
	  ospf_enqueue_lsack(n, lsa_n, ACKL_DELAY);
      }
      else
	ospf_enqueue_lsack(n, lsa_n, ACKL_DIRECT);

      continue;
    }

    /* 13. (8) - received LSA is older */
    {
      /* Seqnum is wrapping, wait until it is flushed */
      if ((en->lsa.age == LSA_MAXAGE) && (en->lsa.sn == LSA_MAXSEQNO))
	continue;

      /* Send newer local copy back to neighbor */
      /* FIXME - check for MinLSArrival ? */
      ospf_send_lsupd(p, &en, 1, n);

      continue;
    }

  skip:
    LOG_LSA1("Bad LSA (Type: %04x, Id: %R, Rt: %R) in LSUPD", lsa_type, lsa.id, lsa.rt);
    LOG_LSA2("  received from nbr %R on %s - %s", n->rid, ifa->ifname, err_dsc);
  }

  /* Send direct LSACKs */
  ospf_send_lsack(p, n, ACKL_DIRECT);

  /* Send enqueued LSAs immediately, do not wait for flood_event */
  if (ev_active(p->flood_event))
  {
    ev_postpone(p->flood_event);
    ospf_flood_event(p);
  }

  /*
   * During loading, we should ask for another batch of LSAs. This is only
   * vaguely mentioned in RFC 2328. We send a new LSREQ if all requests sent in
   * the last packet were already answered and/or removed from the LS request
   * list and therefore lsrqi is pointing to the first node of the list.
   */
  if (!EMPTY_SLIST(n->lsrql) && (n->lsrqi == SHEAD(n->lsrql)))
  {
    ospf_send_lsreq(p, n);
    tm_start(n->lsrq_timer, n->ifa->rxmtint S);
  }

  return;

drop:
  LOG_PKT("Bad LSUPD packet from nbr %R on %s - %s (%u)",
	  n->rid, ifa->ifname, err_dsc, err_val);

  /* Malformed LSUPD - there is no defined error event, we abuse BadLSReq */
  ospf_neigh_sm(n, INM_BADLSREQ);
  return;
}