Statistics
| Branch: | Revision:

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

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)", lsr->lsh.type, lsr->lsh.id, lsr->lsh.rt);
396
        lsr = NODE_NEXT(lsr);
397
        continue;                        
398
      }
399

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
539
    ntohlsah(lsa, &lsatmp);
540

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
693
      continue;
694
    }
695

    
696
    /* FIXME pg145 (6) */
697

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

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

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

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

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

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

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

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