Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / proto / ospf / lsalib.c @ 6b3f1a54

History | View | Annotate | Download (13.3 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2015 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2015 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
#include "ospf.h"
12

    
13
#include "lib/fletcher16.h"
14

    
15
#ifndef CPU_BIG_ENDIAN
16
void
17
lsa_hton_hdr(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
18
{
19
  n->age = htons(h->age);
20
  n->type_raw = htons(h->type_raw);
21
  n->id = htonl(h->id);
22
  n->rt = htonl(h->rt);
23
  n->sn = htonl(h->sn);
24
  n->checksum = htons(h->checksum);
25
  n->length = htons(h->length);
26
}
27

    
28
void
29
lsa_ntoh_hdr(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
30
{
31
  h->age = ntohs(n->age);
32
  h->type_raw = ntohs(n->type_raw);
33
  h->id = ntohl(n->id);
34
  h->rt = ntohl(n->rt);
35
  h->sn = ntohl(n->sn);
36
  h->checksum = ntohs(n->checksum);
37
  h->length = ntohs(n->length);
38
}
39

    
40
void
41
lsa_hton_body(void *h, void *n, u16 len)
42
{
43
  u32 *hid = h;
44
  u32 *nid = n;
45
  uint i;
46

    
47
  for (i = 0; i < (len / sizeof(u32)); i++)
48
    nid[i] = htonl(hid[i]);
49
}
50

    
51
void
52
lsa_ntoh_body(void *n, void *h, u16 len)
53
{
54
  u32 *nid = n;
55
  u32 *hid = h;
56
  uint i;
57

    
58
  for (i = 0; i < (len / sizeof(u32)); i++)
59
    hid[i] = ntohl(nid[i]);
60
}
61
#endif /* little endian */
62

    
63

    
64

    
65
int
66
lsa_flooding_allowed(u32 type, u32 domain, struct ospf_iface *ifa)
67
{
68
  /* Handle inactive vlinks */
69
  if (ifa->state == OSPF_IS_DOWN)
70
    return 0;
71

    
72
  /* 4.5.2 (Case 2) */
73
  switch (LSA_SCOPE(type))
74
  {
75
  case LSA_SCOPE_LINK:
76
    return ifa->iface_id == domain;
77

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

    
81
  case LSA_SCOPE_AS:
82
    if (ifa->type == OSPF_IT_VLINK)
83
      return 0;
84
    if (!oa_is_ext(ifa->oa))
85
      return 0;
86
    return 1;
87

    
88
  default:
89
    log(L_ERR "OSPF: LSA with invalid scope");
90
    return 0;
91
  }
92
}
93

    
94

    
95
static int
96
unknown_lsa_type(u32 type)
97
{
98
  switch (type)
99
  {
100
  case LSA_T_RT:
101
  case LSA_T_NET:
102
  case LSA_T_SUM_NET:
103
  case LSA_T_SUM_RT:
104
  case LSA_T_EXT:
105
  case LSA_T_NSSA:
106
  case LSA_T_LINK:
107
  case LSA_T_PREFIX:
108
    return 0;
109

    
110
  default:
111
    return 1;
112
  }
113
}
114

    
115
#define LSA_V2_TMAX 8
116
static const u16 lsa_v2_types[LSA_V2_TMAX] =
117
  {0, LSA_T_RT, LSA_T_NET, LSA_T_SUM_NET, LSA_T_SUM_RT, LSA_T_EXT, 0, LSA_T_NSSA};
118

    
119
void
120
lsa_get_type_domain_(u32 itype, struct ospf_iface *ifa, u32 *otype, u32 *domain)
121
{
122
  if (ospf_is_v2(ifa->oa->po))
123
  {
124
    itype = itype & LSA_T_V2_MASK;
125
    itype = (itype < LSA_V2_TMAX) ? lsa_v2_types[itype] : 0;
126
  }
127
  else
128
  {
129
    /* For unkown LSAs without U-bit change scope to LSA_SCOPE_LINK */
130
    if (unknown_lsa_type(itype) && !(itype & LSA_UBIT))
131
      itype = itype & ~LSA_SCOPE_MASK;
132
  }
133

    
134
  *otype = itype;
135

    
136
  switch (LSA_SCOPE(itype))
137
  {
138
  case LSA_SCOPE_LINK:
139
    *domain = ifa->iface_id;
140
    return;
141

    
142
  case LSA_SCOPE_AREA:
143
    *domain = ifa->oa->areaid;
144
    return;
145

    
146
  case LSA_SCOPE_AS:
147
  default:
148
    *domain = 0;
149
    return;
150
  }
151
}
152

    
153

    
154
void
155
lsa_generate_checksum(struct ospf_lsa_header *lsa, const u8 *body)
156
{
157
  struct fletcher16_context ctx;
158
  struct ospf_lsa_header hdr;
159
  u16 len = lsa->length;
160

    
161
  /*
162
   * lsa and body are in the host order, we need to compute Fletcher-16 checksum
163
   * for data in the network order. We also skip the initial age field.
164
   */
165

    
166
  lsa_hton_hdr(lsa, &hdr);
167
  hdr.checksum = 0;
168

    
169
  fletcher16_init(&ctx);
170
  fletcher16_update(&ctx, (u8 *) &hdr + 2, sizeof(struct ospf_lsa_header) - 2);
171
  fletcher16_update_n32(&ctx, body, len - sizeof(struct ospf_lsa_header));
172
  lsa->checksum = fletcher16_final(&ctx, len, OFFSETOF(struct ospf_lsa_header, checksum));
173
}
174

    
175
u16
176
lsa_verify_checksum(const void *lsa_n, int lsa_len)
177
{
178
  struct fletcher16_context ctx;
179

    
180
  /* The whole LSA is at lsa_n in net order, we just skip initial age field */
181

    
182
  fletcher16_init(&ctx);
183
  fletcher16_update(&ctx, (u8 *) lsa_n + 2, lsa_len - 2);
184

    
185
  return fletcher16_compute(&ctx) == 0;
186
}
187

    
188

    
189
int
190
lsa_comp(struct ospf_lsa_header *l1, struct ospf_lsa_header *l2)
191
                        /* Return codes from point of view of l1 */
192
{
193
  u32 sn1, sn2;
194

    
195
  sn1 = l1->sn - LSA_INITSEQNO + 1;
196
  sn2 = l2->sn - LSA_INITSEQNO + 1;
197

    
198
  if (sn1 > sn2)
199
    return CMP_NEWER;
200
  if (sn1 < sn2)
201
    return CMP_OLDER;
202

    
203
  if (l1->checksum != l2->checksum)
204
    return l1->checksum < l2->checksum ? CMP_OLDER : CMP_NEWER;
205

    
206
  if ((l1->age == LSA_MAXAGE) && (l2->age != LSA_MAXAGE))
207
    return CMP_NEWER;
208
  if ((l2->age == LSA_MAXAGE) && (l1->age != LSA_MAXAGE))
209
    return CMP_OLDER;
210

    
211
  if (ABS(l1->age - l2->age) > LSA_MAXAGEDIFF)
212
    return l1->age < l2->age ? CMP_NEWER : CMP_OLDER;
213

    
214
  return CMP_SAME;
215
}
216

    
217

    
218
static inline int
219
lsa_walk_rt2(struct ospf_lsa_rt_walk *rt)
220
{
221
  if (rt->buf >= rt->bufend)
222
    return 0;
223

    
224
  struct ospf_lsa_rt2_link *l = rt->buf;
225
  rt->buf += sizeof(struct ospf_lsa_rt2_link) + l->no_tos * sizeof(struct ospf_lsa_rt2_tos);
226

    
227
  rt->type = l->type;
228
  rt->metric = l->metric;
229
  rt->id = l->id;
230
  rt->data = l->data;
231
  return 1;
232
}
233

    
234
static inline int
235
lsa_walk_rt3(struct ospf_lsa_rt_walk *rt)
236
{
237
  while (rt->buf >= rt->bufend)
238
  {
239
    rt->en = ospf_hash_find_rt3_next(rt->en);
240
    if (!rt->en)
241
      return 0;
242

    
243
    rt->buf = rt->en->lsa_body;
244
    rt->bufend = rt->buf + rt->en->lsa.length - sizeof(struct ospf_lsa_header);
245
    rt->buf += sizeof(struct ospf_lsa_rt);
246
  }
247

    
248
  struct ospf_lsa_rt3_link *l = rt->buf;
249
  rt->buf += sizeof(struct ospf_lsa_rt3_link);
250

    
251
  rt->type = l->type;
252
  rt->metric = l->metric;
253
  rt->lif = l->lif;
254
  rt->nif = l->nif;
255
  rt->id = l->id;
256
  return 1;
257
}
258

    
259
void
260
lsa_walk_rt_init(struct ospf_proto *p, struct top_hash_entry *act, struct ospf_lsa_rt_walk *rt)
261
{
262
  rt->ospf2 = ospf_is_v2(p);
263
  rt->id = rt->data = rt->lif = rt->nif = 0;
264

    
265
  if (rt->ospf2)
266
    rt->en = act;
267
  else
268
    rt->en = ospf_hash_find_rt3_first(p->gr, act->domain, act->lsa.rt);
269

    
270
  rt->buf = rt->en->lsa_body;
271
  rt->bufend = rt->buf + rt->en->lsa.length - sizeof(struct ospf_lsa_header);
272
  rt->buf += sizeof(struct ospf_lsa_rt);
273
}
274

    
275
int
276
lsa_walk_rt(struct ospf_lsa_rt_walk *rt)
277
{
278
  return rt->ospf2 ? lsa_walk_rt2(rt) : lsa_walk_rt3(rt);
279
}
280

    
281

    
282
void
283
lsa_parse_sum_net(struct top_hash_entry *en, int ospf2, int af, net_addr *net, u8 *pxopts, u32 *metric)
284
{
285
  if (ospf2)
286
  {
287
    struct ospf_lsa_sum2 *ls = en->lsa_body;
288
    net_fill_ip4(net, ip4_from_u32(en->lsa.id & ls->netmask), u32_masklen(ls->netmask));
289
    *pxopts = 0;
290
    *metric = ls->metric & LSA_METRIC_MASK;
291
  }
292
  else
293
  {
294
    struct ospf_lsa_sum3_net *ls = en->lsa_body;
295
    ospf3_get_prefix(ls->prefix, af, net, pxopts, NULL);
296
    *metric = ls->metric & LSA_METRIC_MASK;
297
  }
298
}
299

    
300
void
301
lsa_parse_sum_rt(struct top_hash_entry *en, int ospf2, u32 *drid, u32 *metric, u32 *options)
302
{
303
  if (ospf2)
304
  {
305
    struct ospf_lsa_sum2 *ls = en->lsa_body;
306
    *drid = en->lsa.id;
307
    *metric = ls->metric & LSA_METRIC_MASK;
308
    *options = 0;
309
  }
310
  else
311
  {
312
    struct ospf_lsa_sum3_rt *ls = en->lsa_body;
313
    *drid = ls->drid;
314
    *metric = ls->metric & LSA_METRIC_MASK;
315
    *options = ls->options & LSA_OPTIONS_MASK;
316
  }
317
}
318

    
319
void
320
lsa_parse_ext(struct top_hash_entry *en, int ospf2, int af, struct ospf_lsa_ext_local *rt)
321
{
322
  if (ospf2)
323
  {
324
    struct ospf_lsa_ext2 *ext = en->lsa_body;
325
    net_fill_ip4(&rt->net,
326
                 ip4_from_u32(en->lsa.id & ext->netmask),
327
                 u32_masklen(ext->netmask));
328
    rt->pxopts = 0;
329
    rt->metric = ext->metric & LSA_METRIC_MASK;
330
    rt->ebit = ext->metric & LSA_EXT2_EBIT;
331

    
332
    rt->fbit = ext->fwaddr;
333
    rt->fwaddr = ipa_from_u32(ext->fwaddr);
334

    
335
    rt->tag = ext->tag;
336
    rt->propagate = lsa_get_options(&en->lsa) & OPT_P;
337
  }
338
  else
339
  {
340
    struct ospf_lsa_ext3 *ext = en->lsa_body;
341
    u32 *buf = ospf3_get_prefix(ext->rest, af, &rt->net, &rt->pxopts, NULL);
342
    rt->metric = ext->metric & LSA_METRIC_MASK;
343
    rt->ebit = ext->metric & LSA_EXT3_EBIT;
344

    
345
    rt->fbit = ext->metric & LSA_EXT3_FBIT;
346
    if (rt->fbit)
347
      buf = ospf3_get_addr(buf, af, &rt->fwaddr);
348
    else
349
      rt->fwaddr = IPA_NONE;
350

    
351
    rt->tag = (ext->metric & LSA_EXT3_TBIT) ? *buf++ : 0;
352
    rt->propagate = rt->pxopts & OPT_PX_P;
353
  }
354
}
355

    
356
#define HDRLEN sizeof(struct ospf_lsa_header)
357

    
358
static int
359
lsa_validate_rt2(struct ospf_lsa_header *lsa, struct ospf_lsa_rt *body)
360
{
361
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_rt)))
362
    return 0;
363

    
364
  uint i = 0;
365
  void *buf = body;
366
  void *bufend = buf + lsa->length - HDRLEN;
367
  buf += sizeof(struct ospf_lsa_rt);
368

    
369
  while (buf < bufend)
370
  {
371
    struct ospf_lsa_rt2_link *l = buf;
372
    buf += sizeof(struct ospf_lsa_rt2_link) + l->no_tos * sizeof(struct ospf_lsa_rt2_tos);
373
    i++;
374

    
375
    if (buf > bufend)
376
      return 0;
377

    
378
    if (!((l->type == LSART_PTP) ||
379
          (l->type == LSART_NET) ||
380
          (l->type == LSART_STUB) ||
381
          (l->type == LSART_VLNK)))
382
      return 0;
383
  }
384

    
385
  if ((body->options & LSA_RT2_LINKS) != i)
386
    return 0;
387

    
388
  return 1;
389
}
390

    
391

    
392
static int
393
lsa_validate_rt3(struct ospf_lsa_header *lsa, struct ospf_lsa_rt *body)
394
{
395
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_rt)))
396
    return 0;
397

    
398
  void *buf = body;
399
  void *bufend = buf + lsa->length - HDRLEN;
400
  buf += sizeof(struct ospf_lsa_rt);
401

    
402
  while (buf < bufend)
403
  {
404
    struct ospf_lsa_rt3_link *l = buf;
405
    buf += sizeof(struct ospf_lsa_rt3_link);
406

    
407
    if (buf > bufend)
408
      return 0;
409

    
410
    if (!((l->type == LSART_PTP) ||
411
          (l->type == LSART_NET) ||
412
          (l->type == LSART_VLNK)))
413
      return 0;
414
  }
415
  return 1;
416
}
417

    
418
static int
419
lsa_validate_net(struct ospf_lsa_header *lsa, struct ospf_lsa_net *body UNUSED)
420
{
421
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_net)))
422
    return 0;
423

    
424
  return 1;
425
}
426

    
427
static int
428
lsa_validate_sum2(struct ospf_lsa_header *lsa, struct ospf_lsa_sum2 *body)
429
{
430
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_sum2)))
431
    return 0;
432

    
433
  /* First field should have TOS = 0, we ignore other TOS fields */
434
  if ((body->metric & LSA_SUM2_TOS) != 0)
435
    return 0;
436

    
437
  return 1;
438
}
439

    
440
static inline int
441
pxlen(u32 *buf)
442
{
443
  return *buf >> 24;
444
}
445

    
446
static int
447
lsa_validate_sum3_net(struct ospf_lsa_header *lsa, struct ospf_lsa_sum3_net *body)
448
{
449
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_sum3_net) + 4))
450
    return 0;
451

    
452
  u8 pxl = pxlen(body->prefix);
453
  if (pxl > IP6_MAX_PREFIX_LENGTH)
454
    return 0;
455

    
456
  if (lsa->length != (HDRLEN + sizeof(struct ospf_lsa_sum3_net) +
457
                      IPV6_PREFIX_SPACE(pxl)))
458
    return 0;
459

    
460
  return 1;
461
}
462

    
463
static int
464
lsa_validate_sum3_rt(struct ospf_lsa_header *lsa, struct ospf_lsa_sum3_rt *body UNUSED)
465
{
466
  if (lsa->length != (HDRLEN + sizeof(struct ospf_lsa_sum3_rt)))
467
    return 0;
468

    
469
  return 1;
470
}
471

    
472
static int
473
lsa_validate_ext2(struct ospf_lsa_header *lsa, struct ospf_lsa_ext2 *body)
474
{
475
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_ext2)))
476
    return 0;
477

    
478
  /* First field should have TOS = 0, we ignore other TOS fields */
479
  if ((body->metric & LSA_EXT2_TOS) != 0)
480
    return 0;
481

    
482
  return 1;
483
}
484

    
485
static int
486
lsa_validate_ext3(struct ospf_lsa_header *lsa, struct ospf_lsa_ext3 *body)
487
{
488
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_ext3) + 4))
489
    return 0;
490

    
491
  u8 pxl = pxlen(body->rest);
492
  if (pxl > IP6_MAX_PREFIX_LENGTH)
493
    return 0;
494

    
495
  int len = IPV6_PREFIX_SPACE(pxl);
496
  if (body->metric & LSA_EXT3_FBIT) // forwarding address
497
    len += 16;
498
  if (body->metric & LSA_EXT3_TBIT) // route tag
499
    len += 4;
500
  if (*body->rest & 0xFFFF) // referenced LS type field
501
    len += 4;
502

    
503
  if (lsa->length != (HDRLEN + sizeof(struct ospf_lsa_ext3) + len))
504
    return 0;
505

    
506
  return 1;
507
}
508

    
509
static int
510
lsa_validate_pxlist(struct ospf_lsa_header *lsa, u32 pxcount, uint offset, u8 *pbuf)
511
{
512
  uint bound = lsa->length - HDRLEN - 4;
513
  u32 i;
514

    
515
  for (i = 0; i < pxcount; i++)
516
    {
517
      if (offset > bound)
518
        return 0;
519

    
520
      u8 pxl = pxlen((u32 *) (pbuf + offset));
521
      if (pxl > IP6_MAX_PREFIX_LENGTH)
522
        return 0;
523

    
524
      offset += IPV6_PREFIX_SPACE(pxl);
525
    }
526

    
527
  if (lsa->length != (HDRLEN + offset))
528
    return 0;
529

    
530
  return 1;
531
}
532

    
533
static int
534
lsa_validate_link(struct ospf_lsa_header *lsa, struct ospf_lsa_link *body)
535
{
536
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_link)))
537
    return 0;
538

    
539
  return lsa_validate_pxlist(lsa, body->pxcount, sizeof(struct ospf_lsa_link), (u8 *) body);
540
}
541

    
542
static int
543
lsa_validate_prefix(struct ospf_lsa_header *lsa, struct ospf_lsa_prefix *body)
544
{
545
  if (lsa->length < (HDRLEN + sizeof(struct ospf_lsa_prefix)))
546
    return 0;
547

    
548
  return lsa_validate_pxlist(lsa, body->pxcount, sizeof(struct ospf_lsa_prefix), (u8 *) body);
549
}
550

    
551

    
552
/**
553
 * lsa_validate - check whether given LSA is valid
554
 * @lsa: LSA header
555
 * @lsa_type: internal LSA type (%LSA_T_xxx)
556
 * @ospf2: %true for OSPFv2, %false for OSPFv3
557
 * @body: pointer to LSA body
558
 *
559
 * Checks internal structure of given LSA body (minimal length,
560
 * consistency). Returns true if valid.
561
 */
562
int
563
lsa_validate(struct ospf_lsa_header *lsa, u32 lsa_type, int ospf2, void *body)
564
{
565
  if (ospf2)
566
  {
567
    switch (lsa_type)
568
    {
569
    case LSA_T_RT:
570
      return lsa_validate_rt2(lsa, body);
571
    case LSA_T_NET:
572
      return lsa_validate_net(lsa, body);
573
    case LSA_T_SUM_NET:
574
      return lsa_validate_sum2(lsa, body);
575
    case LSA_T_SUM_RT:
576
      return lsa_validate_sum2(lsa, body);
577
    case LSA_T_EXT:
578
    case LSA_T_NSSA:
579
      return lsa_validate_ext2(lsa, body);
580
    default:
581
      return 0;        /* Should not happen, unknown LSAs are already rejected */
582
    }
583
  }
584
  else
585
  {
586
    switch (lsa_type)
587
    {
588
    case LSA_T_RT:
589
      return lsa_validate_rt3(lsa, body);
590
    case LSA_T_NET:
591
      return lsa_validate_net(lsa, body);
592
    case LSA_T_SUM_NET:
593
      return lsa_validate_sum3_net(lsa, body);
594
    case LSA_T_SUM_RT:
595
      return lsa_validate_sum3_rt(lsa, body);
596
    case LSA_T_EXT:
597
    case LSA_T_NSSA:
598
      return lsa_validate_ext3(lsa, body);
599
    case LSA_T_LINK:
600
      return lsa_validate_link(lsa, body);
601
    case LSA_T_PREFIX:
602
      return lsa_validate_prefix(lsa, body);
603
    default:
604
      return 1;        /* Unknown LSAs are OK in OSPFv3 */
605
    }
606
  }
607
}