Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (18.7 KB)

1 de769e24 Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 6f3203fa Ondrej Filip
 *        (c) 2000--2004 Ondrej Filip <feela@network.cz>
5 de769e24 Ondrej Filip
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9
#include "ospf.h"
10
11 8298d780 Ondrej Zajicek
12 c3226991 Ondrej Zajicek
struct ospf_lsupd_packet
13
{
14
  struct ospf_packet ospf_packet;
15
  u32 lsano;                        /* Number of LSA's */
16
};
17
18
19 a6bc04d5 Ondrej Zajicek
/* Beware of unaligned access */
20 8298d780 Ondrej Zajicek
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 34a877cc Ondrej Zajicek
  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 8298d780 Ondrej Zajicek
}
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 3aab39f5 Ondrej Zajicek
  log(L_TRACE "%s:     router   %R", p->name, ntohl(op->routerid));
33 8298d780 Ondrej Zajicek
}
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 a6bc04d5 Ondrej Zajicek
  /* We know that ntohs(op->length) >= sizeof(struct ospf_lsa_header) */
43 8298d780 Ondrej Zajicek
  u8 *pbuf= (u8 *) pkt;
44 a6bc04d5 Ondrej Zajicek
  unsigned int offset = sizeof(struct ospf_lsupd_packet);
45
  unsigned int bound = ntohs(op->length) - sizeof(struct ospf_lsa_header);
46 ae85e28c Ondrej Zajicek
  unsigned int i, j, lsalen;
47 8298d780 Ondrej Zajicek
48
  j = ntohl(pkt->lsano);
49
  for (i = 0; i < j; i++)
50
    {
51 ae85e28c Ondrej Zajicek
      if (offset > bound)
52 8298d780 Ondrej Zajicek
        {
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 ae85e28c Ondrej Zajicek
      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 8298d780 Ondrej Zajicek
    }
68
}
69
70 c3226991 Ondrej Zajicek
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 41b612c3 Ondrej Zajicek
      if (!oa_is_ext(ifa->oa))
81 c3226991 Ondrej Zajicek
        return 0;
82 f9c799a0 Ondrej Zajicek
      return 1;
83 c3226991 Ondrej Zajicek
    }
84
  else
85
    return ifa->oa->areaid == domain;
86
}
87
88
#else /* OSPFv3 */
89
90 061ab802 Ondrej Zajicek
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 41b612c3 Ondrej Zajicek
    case LSA_T_NSSA:
101 061ab802 Ondrej Zajicek
    case LSA_T_LINK:
102
    case LSA_T_PREFIX:
103
      return 0;
104
105
    default:
106
      return 1;
107
    }
108
}
109
110 c3226991 Ondrej Zajicek
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 e4404cef Ondrej Zajicek
  /* Handle inactive vlinks */
116
  if (ifa->state == OSPF_IS_DOWN)
117
    return 0;
118
119 c3226991 Ondrej Zajicek
  /* 4.5.2 (Case 2) */
120 061ab802 Ondrej Zajicek
  if (unknown_lsa_type(lsa) && !(lsa->type & LSA_UBIT))
121 c3226991 Ondrej Zajicek
    scope = LSA_SCOPE_LINK;
122
123
  switch (scope)
124
    {
125
    case LSA_SCOPE_LINK:
126 dd4da6f6 Ondrej Zajicek
      return ifa->iface_id == domain;
127 c3226991 Ondrej Zajicek
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 2918e610 Ondrej Zajicek
      if (!oa_is_ext(ifa->oa))
135 c3226991 Ondrej Zajicek
        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 39e517d4 Ondrej Filip
/**
147 6f3203fa Ondrej Filip
 * ospf_lsupd_flood - send received or generated lsa to the neighbors
148 c3226991 Ondrej Zajicek
 * @po: OSPF protocol
149 39e517d4 Ondrej Filip
 * @n: neighbor than sent this lsa (or NULL if generated)
150 d6c28f3a Ondrej Filip
 * @hn: LSA header followed by lsa body in network endianity (may be NULL) 
151
 * @hh: LSA header in host endianity (must be filled)
152 c3226991 Ondrej Zajicek
 * @domain: domain of LSA (must be filled)
153 39e517d4 Ondrej Filip
 * @rtl: add this LSA into retransmission list
154
 *
155 c3226991 Ondrej Zajicek
 *
156 d6c28f3a Ondrej Filip
 * return value - was the LSA flooded back?
157 39e517d4 Ondrej Filip
 */
158
159 84228eee Ondrej Filip
int
160 c3226991 Ondrej Zajicek
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 2c971094 Ondrej Filip
{
164
  struct ospf_iface *ifa;
165
  struct ospf_neighbor *nn;
166
  struct top_hash_entry *en;
167 6f3203fa Ondrej Filip
  struct proto *p = &po->proto;
168 d6c28f3a Ondrej Filip
  int ret, retval = 0;
169 2c971094 Ondrej Filip
170
  /* pg 148 */
171 b181f444 Ondrej Filip
  WALK_LIST(ifa, po->iface_list)
172 2c971094 Ondrej Filip
  {
173 6f3203fa Ondrej Filip
    if (ifa->stub)
174
      continue;
175 52fa3e38 Ondrej Filip
176 c3226991 Ondrej Zajicek
    if (! ospf_lsa_flooding_allowed(hh, domain, ifa))
177
      continue;
178 23d67029 Ondrej Zajicek
179 3aab39f5 Ondrej Zajicek
    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 23d67029 Ondrej Zajicek
182 6f3203fa Ondrej Filip
    ret = 0;
183 b181f444 Ondrej Filip
    WALK_LIST(nn, ifa->neigh_list)
184 2c971094 Ondrej Filip
    {
185 c3226991 Ondrej Zajicek
      /* 13.3 (1a) */
186 6f3203fa Ondrej Filip
      if (nn->state < NEIGHBOR_EXCHANGE)
187
        continue;
188 c3226991 Ondrej Zajicek
189
      /* 13.3 (1b) */
190 6f3203fa Ondrej Filip
      if (nn->state < NEIGHBOR_FULL)
191 2c971094 Ondrej Filip
      {
192 b49e6f5a Ondrej Zajicek
        if ((en = ospf_hash_find_header(nn->lsrqh, domain, hh)) != NULL)
193 2c971094 Ondrej Filip
        {
194 3aab39f5 Ondrej Zajicek
          DBG("That LSA found in lsreq list for neigh %R\n", nn->rid);
195 23d67029 Ondrej Zajicek
196 6f3203fa Ondrej Filip
          switch (lsa_comp(hh, &en->lsa))
197 2c971094 Ondrej Filip
          {
198 6f3203fa Ondrej Filip
          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 3aab39f5 Ondrej Zajicek
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
207 6f3203fa Ondrej Filip
            ospf_hash_delete(nn->lsrqh, en);
208 c404f4b9 Ondrej Filip
            if ((EMPTY_SLIST(nn->lsrql)) && (nn->state == NEIGHBOR_LOADING))
209 6f3203fa Ondrej Filip
              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 3aab39f5 Ondrej Zajicek
            DBG("Removing from lsreq list for neigh %R\n", nn->rid);
218 6f3203fa Ondrej Filip
            ospf_hash_delete(nn->lsrqh, en);
219 c404f4b9 Ondrej Filip
            if ((EMPTY_SLIST(nn->lsrql)) && (nn->state == NEIGHBOR_LOADING))
220 6f3203fa Ondrej Filip
              ospf_neigh_sm(nn, INM_LOADDONE);
221
            break;
222
          default:
223
            bug("Bug in lsa_comp?");
224 2c971094 Ondrej Filip
          }
225
        }
226
      }
227 39e517d4 Ondrej Filip
228 c3226991 Ondrej Zajicek
      /* 13.3 (1c) */
229 6f3203fa Ondrej Filip
      if (nn == n)
230
        continue;
231 39e517d4 Ondrej Filip
232 c3226991 Ondrej Zajicek
      /* 13.3 (1d) */
233 6f3203fa Ondrej Filip
      if (rtl)
234 9669362f Ondrej Filip
      {
235 c3226991 Ondrej Zajicek
        /* 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 b49e6f5a Ondrej Zajicek
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) == NULL)
240 6f3203fa Ondrej Filip
        {
241 b49e6f5a Ondrej Zajicek
          en = ospf_hash_get_header(nn->lsrth, domain, hh);
242 6f3203fa Ondrej Filip
        }
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 23d67029 Ondrej Zajicek
        DBG("Adding that LSA for flood to %I\n", nn->ip);
250 9669362f Ondrej Filip
      }
251
      else
252
      {
253 b49e6f5a Ondrej Zajicek
        if ((en = ospf_hash_find_header(nn->lsrth, domain, hh)) != NULL)
254 6f3203fa Ondrej Filip
        {
255
          s_rem_node(SNODE en);
256
          ospf_hash_delete(nn->lsrth, en);
257
        }
258 9669362f Ondrej Filip
      }
259 39e517d4 Ondrej Filip
260 6f3203fa Ondrej Filip
      ret = 1;
261 2c971094 Ondrej Filip
    }
262 37282678 Ondrej Filip
263 6f3203fa Ondrej Filip
    if (ret == 0)
264
      continue;                        /* pg 150 (2) */
265 37282678 Ondrej Filip
266 c3226991 Ondrej Zajicek
    if (n && (n->ifa == ifa))
267 2c971094 Ondrej Filip
    {
268 c3226991 Ondrej Zajicek
      if ((n->rid == ifa->drid) || n->rid == ifa->bdrid)
269 6f3203fa Ondrej Filip
        continue;                /* pg 150 (3) */
270 c3226991 Ondrej Zajicek
      if (ifa->state == OSPF_IS_BACKUP)
271 6f3203fa Ondrej Filip
        continue;                /* pg 150 (4) */
272
      retval = 1;
273 2c971094 Ondrej Filip
    }
274 740d16d9 Ondrej Filip
275 2c971094 Ondrej Filip
    {
276 d6c28f3a Ondrej Filip
      u16 len, age;
277 2c971094 Ondrej Filip
      struct ospf_lsupd_packet *pk;
278
      struct ospf_packet *op;
279 d6c28f3a Ondrej Filip
      struct ospf_lsa_header *lh;
280 2c971094 Ondrej Filip
281 1fba34a7 Ondrej Zajicek
      /* Check iface buffer size */
282 48e5f32d Ondrej Zajicek
      uint len2 = sizeof(struct ospf_lsupd_packet) + (hn ? ntohs(hn->length) : hh->length);
283
      if (ospf_iface_assure_bufsize(ifa, len2) < 0)
284 1fba34a7 Ondrej Zajicek
      {
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 48e5f32d Ondrej Zajicek
            ifa->ifname, hh->type, hh->id, hh->rt);
288 1fba34a7 Ondrej Zajicek
        continue;
289
      }
290
291 48e5f32d Ondrej Zajicek
      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 6f3203fa Ondrej Filip
      lh = (struct ospf_lsa_header *) (pk + 1);
298 d6c28f3a Ondrej Filip
299
      /* Copy LSA into the packet */
300 6f3203fa Ondrej Filip
      if (hn)
301 38130b86 Ondrej Filip
      {
302 6f3203fa Ondrej Filip
        memcpy(lh, hn, ntohs(hn->length));
303 38130b86 Ondrej Filip
      }
304
      else
305
      {
306 6f3203fa Ondrej Filip
        u8 *help;
307 38130b86 Ondrej Filip
        struct top_hash_entry *en;
308 249fdef7 Ondrej Filip
309 6f3203fa Ondrej Filip
        htonlsah(hh, lh);
310
        help = (u8 *) (lh + 1);
311 b49e6f5a Ondrej Zajicek
        en = ospf_hash_find_header(po->gr, domain, hh);
312 e81b440f Ondrej Zajicek
        htonlsab(en->lsa_body, help, hh->length - sizeof(struct ospf_lsa_header));
313 38130b86 Ondrej Filip
      }
314 39e517d4 Ondrej Filip
315 6f3203fa Ondrej Filip
      len = sizeof(struct ospf_lsupd_packet) + ntohs(lh->length);
316 d6c28f3a Ondrej Filip
317
      age = ntohs(lh->age);
318
      age += ifa->inftransdelay;
319 6f3203fa Ondrej Filip
      if (age > LSA_MAXAGE)
320
        age = LSA_MAXAGE;
321 d6c28f3a Ondrej Filip
      lh->age = htons(age);
322
323 6f3203fa Ondrej Filip
      op->length = htons(len);
324 8298d780 Ondrej Zajicek
325 48e5f32d Ondrej Zajicek
      OSPF_PACKET(ospf_dump_lsupd, pk, "LSUPD packet flooded via %s", ifa->ifname);
326 2c971094 Ondrej Filip
327 3b16080c Ondrej Filip
      switch (ifa->type)
328 2c971094 Ondrej Filip
      {
329 919f5411 Ondrej Zajicek
      case OSPF_IT_BCAST:
330
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
331 95127cbb Ondrej Zajicek
          ospf_send_to_all(ifa);
332
        else if (ifa->cf->real_bcast)
333
          ospf_send_to_bdr(ifa);
334 919f5411 Ondrej Zajicek
        else
335
          ospf_send_to(ifa, AllDRouters);
336
        break;
337
338 3b16080c Ondrej Filip
      case OSPF_IT_NBMA:
339 6f3203fa Ondrej Filip
        if ((ifa->state == OSPF_IS_BACKUP) || (ifa->state == OSPF_IS_DR))
340 f9c799a0 Ondrej Zajicek
          ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
341 6f3203fa Ondrej Filip
        else
342 f9c799a0 Ondrej Zajicek
          ospf_send_to_bdr(ifa);
343 3b16080c Ondrej Filip
        break;
344
345 919f5411 Ondrej Zajicek
      case OSPF_IT_PTP:
346 95127cbb Ondrej Zajicek
        ospf_send_to_all(ifa);
347 919f5411 Ondrej Zajicek
        break;
348
349
      case OSPF_IT_PTMP:
350
        ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
351
        break;
352
353 3b16080c Ondrej Filip
      case OSPF_IT_VLINK:
354 f9c799a0 Ondrej Zajicek
        ospf_send_to(ifa, ifa->vip);
355 3b16080c Ondrej Filip
        break;
356
357
      default:
358 919f5411 Ondrej Zajicek
        bug("Bug in ospf_lsupd_flood()");
359 2c971094 Ondrej Filip
      }
360
    }
361
  }
362 84228eee Ondrej Filip
  return retval;
363 2c971094 Ondrej Filip
}
364
365 6f3203fa Ondrej Filip
void                                /* I send all I received in LSREQ */
366
ospf_lsupd_send_list(struct ospf_neighbor *n, list * l)
367 f45fd316 Ondrej Filip
{
368 86c84d76 Ondrej Filip
  struct ospf_area *oa = n->ifa->oa;
369 d5356072 Ondrej Zajicek
  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 353729f5 Ondrej Zajicek
376 d5356072 Ondrej Zajicek
  pkt = ospf_tx_buffer(n->ifa);
377
  buf = (void *) pkt;
378 f45fd316 Ondrej Filip
379 d5356072 Ondrej Zajicek
  lsr = HEAD(*l);
380
  while(NODE_NEXT(lsr))
381 f45fd316 Ondrej Filip
  {
382 d5356072 Ondrej Zajicek
    /* 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 f45fd316 Ondrej Filip
    {
390 d5356072 Ondrej Zajicek
      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 6f3203fa Ondrej Filip
401 d5356072 Ondrej Zajicek
      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 f45fd316 Ondrej Filip
408 d5356072 Ondrej Zajicek
        /* LSA is larger than MTU, check buffer size */
409 48e5f32d Ondrej Zajicek
        if (ospf_iface_assure_bufsize(n->ifa, len2) < 0)
410 d5356072 Ondrej Zajicek
        {
411
          /* Cannot fit in a tx buffer, skip that */
412 1fba34a7 Ondrej Zajicek
          log(L_ERR "OSPF: LSA too large to send (Type: %04x, Id: %R, Rt: %R)", 
413 d5356072 Ondrej Zajicek
              lsr->lsh.type, lsr->lsh.id, lsr->lsh.rt);
414
          lsr = NODE_NEXT(lsr);
415
          continue;
416
        }
417 48e5f32d Ondrej Zajicek
418
        /* TX buffer could be reallocated */
419
        pkt = ospf_tx_buffer(n->ifa);
420
        buf = (void *) pkt;
421 d5356072 Ondrej Zajicek
      }
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 f45fd316 Ondrej Filip
    }
431 9f940976 Ondrej Filip
432 d5356072 Ondrej Zajicek
    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 48e5f32d Ondrej Zajicek
                n->ip, n->ifa->ifname);
440 f9c799a0 Ondrej Zajicek
    ospf_send_to(n->ifa, n->ip);
441 b477a9a8 Ondrej Filip
  }
442 f45fd316 Ondrej Filip
}
443
444 de769e24 Ondrej Filip
void
445 c3226991 Ondrej Zajicek
ospf_lsupd_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
446
                   struct ospf_neighbor *n)
447 de769e24 Ondrej Filip
{
448 a6bc04d5 Ondrej Zajicek
449 5e3436d2 Ondrej Filip
  struct ospf_neighbor *ntmp;
450 86c84d76 Ondrej Filip
  struct proto_ospf *po = ifa->oa->po;
451
  struct proto *p = &po->proto;
452 a6bc04d5 Ondrej Zajicek
  unsigned int i, max, sendreq = 1;
453 39e517d4 Ondrej Filip
454 a6bc04d5 Ondrej Zajicek
  unsigned int size = ntohs(ps_i->length);
455
  if (size < (sizeof(struct ospf_lsupd_packet) + sizeof(struct ospf_lsa_header)))
456 394acced Ondrej Filip
  {
457 353729f5 Ondrej Zajicek
    log(L_ERR "OSPF: Bad LSUPD packet from %I - too short (%u B)", n->ip, size);
458 394acced Ondrej Filip
    return;
459
  }
460 39e517d4 Ondrej Filip
461 a6bc04d5 Ondrej Zajicek
  struct ospf_lsupd_packet *ps = (void *) ps_i;
462 48e5f32d Ondrej Zajicek
  OSPF_PACKET(ospf_dump_lsupd, ps, "LSUPD packet received from %I via %s", n->ip, ifa->ifname);
463 a6bc04d5 Ondrej Zajicek
464
  if (n->state < NEIGHBOR_EXCHANGE)
465 5da1f935 Ondrej Filip
  {
466 a6bc04d5 Ondrej Zajicek
    OSPF_TRACE(D_PACKETS, "Received lsupd in lesser state than EXCHANGE from (%I)", n->ip);
467 5da1f935 Ondrej Filip
    return;
468
  }
469 394acced Ondrej Filip
470 39e517d4 Ondrej Filip
  ospf_neigh_sm(n, INM_HELLOREC);        /* Questionable */
471 9669362f Ondrej Filip
472 a6bc04d5 Ondrej Zajicek
  unsigned int offset = sizeof(struct ospf_lsupd_packet);
473
  unsigned int bound = size - sizeof(struct ospf_lsa_header);
474 39e517d4 Ondrej Filip
475 a6bc04d5 Ondrej Zajicek
  max = ntohl(ps->lsano);
476
  for (i = 0; i < max; i++)
477 394acced Ondrej Filip
  {
478 921a93f2 Ondrej Filip
    struct ospf_lsa_header lsatmp;
479
    struct top_hash_entry *lsadb;
480 13741548 Ondrej Filip
481 a6bc04d5 Ondrej Zajicek
    if (offset > bound)
482 39e517d4 Ondrej Filip
    {
483
      log(L_WARN "Received lsupd from %I is too short!", n->ip);
484
      ospf_neigh_sm(n, INM_BADLSREQ);
485 a6bc04d5 Ondrej Zajicek
      return;
486 39e517d4 Ondrej Filip
    }
487 13741548 Ondrej Filip
488 a6bc04d5 Ondrej Zajicek
    struct ospf_lsa_header *lsa = (void *) (((u8 *) ps) + offset);
489
    unsigned int lsalen = ntohs(lsa->length);
490 69fbf9a2 Ondrej Zajicek
    offset += lsalen;
491
 
492
    if ((offset > size) || ((lsalen % 4) != 0) ||
493 a6bc04d5 Ondrej Zajicek
        (lsalen <= sizeof(struct ospf_lsa_header)))
494 13741548 Ondrej Filip
    {
495 39e517d4 Ondrej Filip
      log(L_WARN "Received LSA from %I with bad length", n->ip);
496
      ospf_neigh_sm(n, INM_BADLSREQ);
497 13741548 Ondrej Filip
      break;
498
    }
499 39e517d4 Ondrej Filip
500 2c971094 Ondrej Filip
    /* pg 143 (1) */
501 a6bc04d5 Ondrej Zajicek
    u16 chsum = lsa->checksum;
502 d5d9693c Ondrej Filip
    if (chsum != lsasum_check(lsa, NULL))
503 394acced Ondrej Filip
    {
504 061ab802 Ondrej Zajicek
      log(L_WARN "Received bad lsa checksum from %I: %x %x", n->ip, chsum, lsa->checksum);
505 8496b2e4 Ondrej Filip
      continue;
506
    }
507 39e517d4 Ondrej Filip
508 c3226991 Ondrej Zajicek
#ifdef OSPFv2
509 2c971094 Ondrej Filip
    /* pg 143 (2) */
510 41b612c3 Ondrej Zajicek
    if ((lsa->type == 0) || (lsa->type == 6) || (lsa->type > LSA_T_NSSA))
511 8496b2e4 Ondrej Filip
    {
512 39e517d4 Ondrej Filip
      log(L_WARN "Unknown LSA type from %I", n->ip);
513 8496b2e4 Ondrej Filip
      continue;
514 394acced Ondrej Filip
    }
515 39e517d4 Ondrej Filip
516 2c971094 Ondrej Filip
    /* pg 143 (3) */
517 41b612c3 Ondrej Zajicek
    if ((lsa->type == LSA_T_EXT) && !oa_is_ext(ifa->oa))
518 8496b2e4 Ondrej Filip
    {
519 39e517d4 Ondrej Filip
      log(L_WARN "Received External LSA in stub area from %I", n->ip);
520 8496b2e4 Ondrej Filip
      continue;
521
    }
522 c3226991 Ondrej Zajicek
#else /* OSPFv3 */
523 c06de722 Ondrej Zajicek
    u16 scope = ntoht(lsa->type) & LSA_SCOPE_MASK;
524
525 c3226991 Ondrej Zajicek
    /* 4.5.1 (2) */
526 c06de722 Ondrej Zajicek
    if ((scope == LSA_SCOPE_AS) && !oa_is_ext(ifa->oa))
527 c3226991 Ondrej Zajicek
    {
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 c06de722 Ondrej Zajicek
    if (scope == LSA_SCOPE_RES)
534 c3226991 Ondrej Zajicek
    {
535
      log(L_WARN "Received LSA with invalid scope from %I", n->ip);
536
      continue;
537
    }
538
#endif
539 39e517d4 Ondrej Filip
540 6f3203fa Ondrej Filip
    ntohlsah(lsa, &lsatmp);
541 39e517d4 Ondrej Filip
542 3aab39f5 Ondrej Zajicek
    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 39e517d4 Ondrej Filip
545 cbf8b08d Ondrej Zajicek
    /* FIXME domain should be link id for unknown LSA types with zero Ubit */
546 c3226991 Ondrej Zajicek
    u32 domain = ospf_lsa_domain(lsatmp.type, ifa);
547 b49e6f5a Ondrej Zajicek
    lsadb = ospf_hash_find_header(po->gr, domain, &lsatmp);
548 39e517d4 Ondrej Filip
549
#ifdef LOCAL_DEBUG
550 6f3203fa Ondrej Filip
    if (lsadb)
551 3aab39f5 Ondrej Zajicek
      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 2389c46f Ondrej Zajicek
          lsadb->lsa.sn, lsadb->lsa.age, lsadb->lsa.checksum);
554 39e517d4 Ondrej Filip
#endif
555 d8852b36 Ondrej Filip
556 2c971094 Ondrej Filip
    /* pg 143 (4) */
557 86c84d76 Ondrej Filip
    if ((lsatmp.age == LSA_MAXAGE) && (lsadb == NULL) && can_flush_lsa(po))
558 921a93f2 Ondrej Filip
    {
559 8e15e048 Ondrej Filip
      ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
560 39e517d4 Ondrej Filip
      continue;
561 921a93f2 Ondrej Filip
    }
562 39e517d4 Ondrej Filip
563 2c971094 Ondrej Filip
    /* pg 144 (5) */
564 6f3203fa Ondrej Filip
    if ((lsadb == NULL) || (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_NEWER))
565 921a93f2 Ondrej Filip
    {
566 6f3203fa Ondrej Filip
      struct ospf_iface *ift = NULL;
567 8a70a13e Ondrej Zajicek
      int self = (lsatmp.rt == po->router_id);
568 39e517d4 Ondrej Filip
569
      DBG("PG143(5): Received LSA is newer\n");
570
571 c3226991 Ondrej Zajicek
#ifdef OSPFv2
572
      /* 13.4 - check self-originated LSAs of NET type */
573 6f3203fa Ondrej Filip
      if ((!self) && (lsatmp.type == LSA_T_NET))
574 39e517d4 Ondrej Filip
      {
575 b49e6f5a Ondrej Zajicek
        struct ospf_iface *nifa;
576 6f3203fa Ondrej Filip
        WALK_LIST(nifa, po->iface_list)
577
        {
578 3b16080c Ondrej Filip
          if (!nifa->iface)
579
            continue;
580 353729f5 Ondrej Zajicek
          if (ipa_equal(nifa->addr->ip, ipa_from_u32(lsatmp.id)))
581 6f3203fa Ondrej Filip
          {
582
            self = 1;
583
            break;
584
          }
585
        }
586 39e517d4 Ondrej Filip
      }
587 c3226991 Ondrej Zajicek
#endif
588 39e517d4 Ondrej Filip
589 c3226991 Ondrej Zajicek
      /* pg 145 (5f) - premature aging of self originated lsa */
590 6f3203fa Ondrej Filip
      if (self)
591 39e517d4 Ondrej Filip
      {
592 6f3203fa Ondrej Filip
        if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO))
593
        {
594
          ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
595
          continue;
596
        }
597
598 63542845 Ondrej Zajicek
        OSPF_TRACE(D_EVENTS, "Received old self-originated LSA (Type: %04x, Id: %R, Rt: %R)",
599 3aab39f5 Ondrej Zajicek
                   lsatmp.type, lsatmp.id, lsatmp.rt);
600 28008482 Ondrej Zajicek
601
        if (lsadb)
602
        {
603 63542845 Ondrej Zajicek
          OSPF_TRACE(D_EVENTS, "Reflooding new self-originated LSA with newer sequence number");
604 28008482 Ondrej Zajicek
          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 63542845 Ondrej Zajicek
          ospf_lsupd_flood(po, NULL, NULL, &lsadb->lsa, domain, 1);
610 28008482 Ondrej Zajicek
        }
611
        else
612 6f3203fa Ondrej Filip
        {
613 28008482 Ondrej Zajicek
          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 63542845 Ondrej Zajicek
          ospf_lsupd_flood(po, NULL, lsa, &lsatmp, domain, 0);
621 6f3203fa Ondrej Filip
        }
622
        continue;
623 39e517d4 Ondrej Filip
      }
624 32fa4a5a Ondrej Filip
625 2c971094 Ondrej Filip
      /* pg 144 (5a) */
626 6f3203fa Ondrej Filip
      if (lsadb && ((now - lsadb->inst_t) <= MINLSARRIVAL))        /* FIXME: test for flooding? */
627 85c92555 Ondrej Filip
      {
628 28008482 Ondrej Zajicek
        OSPF_TRACE(D_EVENTS, "Skipping LSA received in less that MINLSARRIVAL");
629 6f3203fa Ondrej Filip
        sendreq = 0;
630 85c92555 Ondrej Filip
        continue;
631
      }
632 6f3203fa Ondrej Filip
633 921a93f2 Ondrej Filip
      /* Remove old from all ret lists */
634 2c971094 Ondrej Filip
      /* pg 144 (5c) */
635 ee7408c2 Ondrej Zajicek
      /* Must be done before (5b), otherwise it also removes the new entries from (5b) */
636 6f3203fa Ondrej Filip
      if (lsadb)
637 b181f444 Ondrej Filip
        WALK_LIST(ift, po->iface_list)
638
          WALK_LIST(ntmp, ift->neigh_list)
639 62924172 Ondrej Filip
      {
640 6f3203fa Ondrej Filip
        struct top_hash_entry *en;
641
        if (ntmp->state > NEIGHBOR_EXSTART)
642 b49e6f5a Ondrej Zajicek
          if ((en = ospf_hash_find_header(ntmp->lsrth, domain, &lsadb->lsa)) != NULL)
643 6f3203fa Ondrej Filip
          {
644
            s_rem_node(SNODE en);
645
            ospf_hash_delete(ntmp->lsrth, en);
646
          }
647
      }
648
649 ee7408c2 Ondrej Zajicek
      /* 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 6f3203fa Ondrej Filip
      if ((lsatmp.age == LSA_MAXAGE) && (lsatmp.sn == LSA_MAXSEQNO)
663 86c84d76 Ondrej Filip
          && lsadb && can_flush_lsa(po))
664 6f3203fa Ondrej Filip
      {
665 86c84d76 Ondrej Filip
        flush_lsa(lsadb, po);
666 b8f17cf1 Ondrej Filip
        schedule_rtcalc(po);
667 6f3203fa Ondrej Filip
        continue;
668
      }                                /* FIXME lsack? */
669 921a93f2 Ondrej Filip
670 2c971094 Ondrej Filip
      /* pg 144 (5d) */
671 a6bc04d5 Ondrej Zajicek
      void *body = mb_alloc(p->pool, lsatmp.length - sizeof(struct ospf_lsa_header));
672 e81b440f Ondrej Zajicek
      ntohlsab(lsa + 1, body, lsatmp.length - sizeof(struct ospf_lsa_header));
673 a6bc04d5 Ondrej Zajicek
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 c3226991 Ondrej Zajicek
      lsadb = lsa_install_new(po, &lsatmp, domain, body);
685 10000b96 Ondrej Filip
      DBG("New LSA installed in DB\n");
686 d8852b36 Ondrej Filip
687 be862406 Ondrej Zajicek
#ifdef OSPFv3
688 be2d38b7 Ondrej Zajicek
      /* Events 6,7 from RFC5340 4.4.3. */
689 be862406 Ondrej Zajicek
      if ((lsatmp.type == LSA_T_LINK) &&
690
          (ifa->state == OSPF_IS_DR))
691
        schedule_net_lsa(ifa);
692
#endif
693
694 921a93f2 Ondrej Filip
      continue;
695
    }
696
697 d6c28f3a Ondrej Filip
    /* FIXME pg145 (6) */
698 921a93f2 Ondrej Filip
699 2c971094 Ondrej Filip
    /* pg145 (7) */
700 6f3203fa Ondrej Filip
    if (lsa_comp(&lsatmp, &lsadb->lsa) == CMP_SAME)
701 921a93f2 Ondrej Filip
    {
702 84228eee Ondrej Filip
      struct top_hash_entry *en;
703 d6c28f3a Ondrej Filip
      DBG("PG145(7) Got the same LSA\n");
704 b49e6f5a Ondrej Zajicek
      if ((en = ospf_hash_find_header(n->lsrth, lsadb->domain, &lsadb->lsa)) != NULL)
705 84228eee Ondrej Filip
      {
706 6f3203fa Ondrej Filip
        /* pg145 (7a) */
707
        s_rem_node(SNODE en);
708
        ospf_hash_delete(n->lsrth, en);
709 ee7408c2 Ondrej Zajicek
710 6f3203fa Ondrej Filip
        if (ifa->state == OSPF_IS_BACKUP)
711
        {
712
          if (n->rid == ifa->drid)
713
            ospf_lsack_enqueue(n, lsa, ACKL_DELAY);
714
        }
715 84228eee Ondrej Filip
      }
716
      else
717
      {
718 6f3203fa Ondrej Filip
        /* pg145 (7b) */
719
        ospf_lsack_enqueue(n, lsa, ACKL_DIRECT);
720 84228eee Ondrej Filip
      }
721 9b7de4c4 Ondrej Filip
      sendreq = 0;
722 84228eee Ondrej Filip
      continue;
723 921a93f2 Ondrej Filip
    }
724
725 2c971094 Ondrej Filip
    /* pg145 (8) */
726 6f3203fa Ondrej Filip
    if ((lsadb->lsa.age == LSA_MAXAGE) && (lsadb->lsa.sn == LSA_MAXSEQNO))
727 67315ef6 Ondrej Filip
    {
728
      continue;
729
    }
730 921a93f2 Ondrej Filip
731 2c971094 Ondrej Filip
    {
732
      list l;
733 a548a7e1 Ondrej Filip
      struct l_lsr_head ll;
734 2c971094 Ondrej Filip
      init_list(&l);
735 6f3203fa Ondrej Filip
      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 2c971094 Ondrej Filip
    }
741 394acced Ondrej Filip
  }
742 39e517d4 Ondrej Filip
743 c76ba51a Ondrej Filip
  /* Send direct LSAs */
744
  ospf_lsack_send(n, ACKL_DIRECT);
745
746 6f3203fa Ondrej Filip
  if (sendreq && (n->state == NEIGHBOR_LOADING))
747 489f800b Ondrej Filip
  {
748 6f3203fa Ondrej Filip
    ospf_lsreq_send(n);                /* Ask for another part of neighbor's database */
749 489f800b Ondrej Filip
  }
750 de769e24 Ondrej Filip
}
751
752 4bd28fb6 Ondrej Filip
void
753 c3226991 Ondrej Zajicek
ospf_lsupd_flush_nlsa(struct proto_ospf *po, struct top_hash_entry *en)
754 4bd28fb6 Ondrej Filip
{
755 6f3203fa Ondrej Filip
  struct ospf_lsa_header *lsa = &en->lsa;
756
  struct proto *p = &po->proto;
757 4bd28fb6 Ondrej Filip
758 6f3203fa Ondrej Filip
  lsa->age = LSA_MAXAGE;
759
  lsa->sn = LSA_MAXSEQNO;
760 d5d9693c Ondrej Filip
  lsasum_calculate(lsa, en->lsa_body);
761 d3995c49 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Premature aging self originated lsa!");
762 34a877cc Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Type: %04x, Id: %R, Rt: %R", lsa->type, lsa->id, lsa->rt);
763 c3226991 Ondrej Zajicek
  ospf_lsupd_flood(po, NULL, NULL, lsa, en->domain, 0);
764 4bd28fb6 Ondrej Filip
}