Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / topology.c @ f623ab98

History | View | Annotate | Download (45.7 KB)

1
/*
2
 *        BIRD -- OSPF Topological Database
3
 *
4
 *        (c) 1999       Martin Mares <mj@ucw.cz>
5
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include "nest/bird.h"
11
#include "lib/string.h"
12

    
13
#include "ospf.h"
14

    
15
#define HASH_DEF_ORDER 6
16
#define HASH_HI_MARK *4
17
#define HASH_HI_STEP 2
18
#define HASH_HI_MAX 16
19
#define HASH_LO_MARK /5
20
#define HASH_LO_STEP 2
21
#define HASH_LO_MIN 8
22

    
23
void originate_prefix_rt_lsa(struct ospf_area *oa);
24
void originate_prefix_net_lsa(struct ospf_iface *ifa);
25
void flush_prefix_net_lsa(struct ospf_iface *ifa);
26

    
27
#ifdef OSPFv2
28
#define ipa_to_rid(x) _I(x)
29
#else /* OSPFv3 */
30
#define ipa_to_rid(x) _I3(x)
31
#endif
32

    
33

    
34
#ifdef OSPFv2
35
static inline u32
36
fibnode_to_lsaid(struct proto_ospf *po, struct fib_node *fn)
37
{
38
  /* We have to map IP prefixes to u32 in such manner that resulting
39
     u32 interpreted as IP address is a member of given
40
     prefix. Therefore, /32 prefix have to be mapped on itself.
41
     All received prefixes have to be mapped on different u32s.
42

43
     We have an assumption that if there is nontrivial (non-/32)
44
     network prefix, then there is not /32 prefix for the first
45
     and the last IP address of the network (these are usually
46
     reserved, therefore it is not an important restriction).
47
     The network prefix is mapped to the first or the last
48
     IP address in the manner that disallow collisions - we
49
     use IP address that cannot be used by parent prefix.
50

51
     For example:
52
     192.168.0.0/24 maps to 192.168.0.255
53
     192.168.1.0/24 maps to 192.168.1.0
54
     because 192.168.0.0 and 192.168.1.255 might be used by
55
     192.168.0.0/23 .
56

57
     This is not compatible with older RFC 1583, so we have an option
58
     to the RFC 1583 compatible assignment (that always uses the first
59
     address) which disallows subnetting.
60

61
     Appendig E of RFC 2328 suggests different algorithm, that tries
62
     to maximize both compatibility and subnetting. But as it is not
63
     possible to have both reliably and the suggested algorithm was
64
     unnecessary complicated and it does crazy things like changing
65
     LSA ID for a network because different network appeared, we
66
     choose a different way. */
67

    
68
  u32 id = _I(fn->prefix);
69

    
70
  if ((po->rfc1583) || (fn->pxlen == 0) || (fn->pxlen == 32))
71
    return id;
72

    
73
  if (id & (1 << (32 - fn->pxlen)))
74
    return id;
75
  else
76
    return id | ~u32_mkmask(fn->pxlen);
77
}
78

    
79
#else /* OSPFv3 */
80

    
81
static inline u32
82
fibnode_to_lsaid(struct proto_ospf *po, struct fib_node *fn)
83
{
84
  /*
85
   * In OSPFv3, it is simpler. There is not a requirement for
86
   * membership of the result in the input network, so we just use a
87
   * hash-based unique ID of a routing table entry for a route that
88
   * originated given LSA. For ext-LSA, it is an imported route in the
89
   * nest's routing table (p->table). For summary-LSA, it is a
90
   * 'source' route in the protocol internal routing table (po->rtf).
91
   */
92
  return fn->uid;
93
}
94

    
95
#endif
96

    
97

    
98
static void *
99
lsab_alloc(struct proto_ospf *po, unsigned size)
100
{
101
  unsigned offset = po->lsab_used;
102
  po->lsab_used += size;
103
  if (po->lsab_used > po->lsab_size)
104
    {
105
      po->lsab_size = MAX(po->lsab_used, 2 * po->lsab_size);
106
      po->lsab = mb_realloc(po->proto.pool, po->lsab, po->lsab_size);
107
    }
108
  return ((byte *) po->lsab) + offset;
109
}
110

    
111
static inline void *
112
lsab_allocz(struct proto_ospf *po, unsigned size)
113
{
114
  void *r = lsab_alloc(po, size);
115
  bzero(r, size);
116
  return r;
117
}
118

    
119
static inline void *
120
lsab_flush(struct proto_ospf *po)
121
{
122
  void *r = mb_alloc(po->proto.pool, po->lsab_used);
123
  memcpy(r, po->lsab, po->lsab_used);
124
  po->lsab_used = 0;
125
  return r;
126
}
127

    
128
static inline void *
129
lsab_offset(struct proto_ospf *po, unsigned offset)
130
{
131
  return ((byte *) po->lsab) + offset;
132
}
133

    
134
static inline void *
135
lsab_end(struct proto_ospf *po)
136
{
137
  return ((byte *) po->lsab) + po->lsab_used;
138
}
139

    
140
static s32
141
get_seqnum(struct top_hash_entry *en)
142
{
143
  if (!en)
144
    return LSA_INITSEQNO;
145

    
146
  if (en->lsa.sn == LSA_MAXSEQNO)
147
  {
148
    log(L_WARN "OSPF: Premature origination of LSA (Type: %04x, Id: %R, Rt: %R)",
149
        en->lsa.type, en->lsa.id, en->lsa.rt);
150
    return LSA_INITSEQNO;
151
  }
152

    
153
  return en->lsa.sn + 1;
154
}
155

    
156

    
157
static int
158
configured_stubnet(struct ospf_area *oa, struct ifa *a)
159
{
160
  if (!oa->ac)
161
    return 0;
162

    
163
  /* Does not work for IA_PEER addresses, but it is not called on these */
164
  struct ospf_stubnet_config *sn;
165
  WALK_LIST(sn, oa->ac->stubnet_list)
166
    {
167
      if (sn->summary)
168
        {
169
          if (ipa_in_net(a->prefix, sn->px.addr, sn->px.len) && (a->pxlen >= sn->px.len))
170
            return 1;
171
        }
172
      else
173
        {
174
          if (ipa_equal(a->prefix, sn->px.addr) && (a->pxlen == sn->px.len))
175
            return 1;
176
        }
177
    }
178
  return 0;
179
}
180

    
181
int
182
bcast_net_active(struct ospf_iface *ifa)
183
{
184
  struct ospf_neighbor *neigh;
185

    
186
  if (ifa->state == OSPF_IS_WAITING)
187
    return 0;
188

    
189
  WALK_LIST(neigh, ifa->neigh_list)
190
    {
191
      if (neigh->state == NEIGHBOR_FULL)
192
        {
193
          if (neigh->rid == ifa->drid)
194
            return 1;
195

    
196
          if (ifa->state == OSPF_IS_DR)
197
            return 1;
198
        }
199
    }
200

    
201
  return 0;
202
}
203

    
204

    
205
#ifdef OSPFv2
206

    
207
static void *
208
originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
209
{
210
  struct proto_ospf *po = oa->po;
211
  struct ospf_iface *ifa;
212
  int i = 0, bitv = 0;
213
  struct ospf_lsa_rt *rt;
214
  struct ospf_lsa_rt_link *ln;
215
  struct ospf_neighbor *neigh;
216

    
217
  ASSERT(po->lsab_used == 0);
218
  rt = lsab_allocz(po, sizeof(struct ospf_lsa_rt));
219

    
220
  rt->options = 0;
221

    
222
  if (po->areano > 1)
223
    rt->options |= OPT_RT_B;
224

    
225
  if ((po->areano > 1) && oa_is_nssa(oa) && oa->ac->translator)
226
    rt->options |= OPT_RT_NT;
227

    
228
  if (po->ebit && !oa_is_stub(oa))
229
    rt->options |= OPT_RT_E;
230

    
231
  rt = NULL; /* buffer might be reallocated later */
232

    
233
  WALK_LIST(ifa, po->iface_list)
234
  {
235
    int net_lsa = 0;
236
    u32 link_cost = po->stub_router ? 0xffff : ifa->cost;
237

    
238
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa == oa) &&
239
        (!EMPTY_LIST(ifa->neigh_list)))
240
    {
241
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
242
      if ((neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
243
        bitv = 1;
244
    }
245

    
246
    if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
247
      continue;
248

    
249
    ifa->rt_pos_beg = i;
250

    
251
    /* RFC2328 - 12.4.1.1-4 */
252
    switch (ifa->type)
253
      {
254
      case OSPF_IT_PTP:
255
      case OSPF_IT_PTMP:
256
        WALK_LIST(neigh, ifa->neigh_list)
257
          if (neigh->state == NEIGHBOR_FULL)
258
          {
259
            ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
260
            ln->type = LSART_PTP;
261
            ln->id = neigh->rid;
262

    
263
            /*
264
             * ln->data should be ifa->iface_id in case of no/ptp
265
             * address (ifa->addr->flags & IA_PEER) on PTP link (see
266
             * RFC 2328 12.4.1.1.), but the iface ID value has no use,
267
             * while using IP address even in this case is here for
268
             * compatibility with some broken implementations that use
269
             * this address as a next-hop.
270
             */
271
            ln->data = ipa_to_u32(ifa->addr->ip);
272
            ln->metric = link_cost;
273
            ln->padding = 0;
274
            i++;
275
          }
276
        break;
277

    
278
      case OSPF_IT_BCAST:
279
      case OSPF_IT_NBMA:
280
        if (bcast_net_active(ifa))
281
          {
282
            ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
283
            ln->type = LSART_NET;
284
            ln->id = ipa_to_u32(ifa->drip);
285
            ln->data = ipa_to_u32(ifa->addr->ip);
286
            ln->metric = link_cost;
287
            ln->padding = 0;
288
            i++;
289
            net_lsa = 1;
290
          }
291
        break;
292

    
293
      case OSPF_IT_VLINK:
294
        neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
295
        if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
296
        {
297
          ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
298
          ln->type = LSART_VLNK;
299
          ln->id = neigh->rid;
300
          ln->data = ipa_to_u32(ifa->addr->ip);
301
          ln->metric = link_cost;
302
          ln->padding = 0;
303
          i++;
304
        }
305
        break;
306

    
307
      default:
308
        log("Unknown interface type %s", ifa->iface->name);
309
        break;
310
      }
311

    
312
    ifa->rt_pos_end = i;
313

    
314
    /* Now we will originate stub area if there is no primary */
315
    if (net_lsa ||
316
        (ifa->type == OSPF_IT_VLINK) ||
317
        ((ifa->addr->flags & IA_PEER) && ! ifa->cf->stub) ||
318
        configured_stubnet(oa, ifa->addr))
319
      continue;
320

    
321
    ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
322
    if ((ifa->addr->flags & IA_HOST) ||
323
        (ifa->state == OSPF_IS_LOOP) ||
324
        (ifa->type == OSPF_IT_PTMP))
325
    {
326
      /* Host stub entry */
327
      ln->type = LSART_STUB;
328
      ln->id = ipa_to_u32(ifa->addr->ip);
329
      ln->data = 0xffffffff;
330
      ln->metric = 0;
331
      ln->padding = 0;
332
    }
333
    else 
334
    {
335
      /* Network stub entry */
336
      ln->type = LSART_STUB;
337
      ln->id = ipa_to_u32(ifa->addr->prefix);
338
      ln->data = ipa_to_u32(ipa_mkmask(ifa->addr->pxlen));
339
      ln->metric = ifa->cost;
340
      ln->padding = 0;
341
    }
342
    i++;
343

    
344
    ifa->rt_pos_end = i;
345
  }
346

    
347
  struct ospf_stubnet_config *sn;
348
  if (oa->ac)
349
    WALK_LIST(sn, oa->ac->stubnet_list)
350
      if (!sn->hidden)
351
      {
352
        ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
353
        ln->type = LSART_STUB;
354
        ln->id = ipa_to_u32(sn->px.addr);
355
        ln->data = ipa_to_u32(ipa_mkmask(sn->px.len));
356
        ln->metric = sn->cost;
357
        ln->padding = 0;
358
        i++;
359
      }
360

    
361
  rt = po->lsab;
362
  rt->links = i;
363

    
364
  if (bitv) 
365
    rt->options |= OPT_RT_V;
366

    
367
  *length = po->lsab_used + sizeof(struct ospf_lsa_header);
368
  return lsab_flush(po);
369
}
370

    
371
#else /* OSPFv3 */
372

    
373
static void
374
add_lsa_rt_link(struct proto_ospf *po, struct ospf_iface *ifa, u8 type, u32 nif, u32 id)
375
{
376
  struct ospf_lsa_rt_link *ln = lsab_alloc(po, sizeof(struct ospf_lsa_rt_link));
377
  ln->type = type;
378
  ln->padding = 0;
379
  ln->metric = ifa->cost;
380
  ln->lif = ifa->iface_id;
381
  ln->nif = nif;
382
  ln->id = id;
383
}
384

    
385
static void *
386
originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
387
{
388
  struct proto_ospf *po = oa->po;
389
  struct ospf_iface *ifa;
390
  int bitv = 0;
391
  int i = 0;
392
  struct ospf_lsa_rt *rt;
393
  struct ospf_neighbor *neigh;
394

    
395
  ASSERT(po->lsab_used == 0);
396
  rt = lsab_allocz(po, sizeof(struct ospf_lsa_rt));
397

    
398
  rt->options = oa->options & OPTIONS_MASK;
399

    
400
  if (po->areano > 1)
401
    rt->options |= OPT_RT_B;
402

    
403
  if ((po->areano > 1) && oa_is_nssa(oa) && oa->ac->translator)
404
    rt->options |= OPT_RT_NT;
405

    
406
  if (po->ebit && !oa_is_stub(oa))
407
    rt->options |= OPT_RT_E;
408

    
409
  rt = NULL; /* buffer might be reallocated later */
410

    
411
  WALK_LIST(ifa, po->iface_list)
412
  {
413
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa == oa) &&
414
        (!EMPTY_LIST(ifa->neigh_list)))
415
    {
416
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
417
      if ((neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
418
        bitv = 1;
419
    }
420

    
421
    if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
422
      continue;
423

    
424
    ifa->rt_pos_beg = i;
425

    
426
    /* RFC5340 - 4.4.3.2 */
427
    switch (ifa->type)
428
      {
429
      case OSPF_IT_PTP:
430
      case OSPF_IT_PTMP:
431
        WALK_LIST(neigh, ifa->neigh_list)
432
          if (neigh->state == NEIGHBOR_FULL)
433
            add_lsa_rt_link(po, ifa, LSART_PTP, neigh->iface_id, neigh->rid), i++;
434
        break;
435

    
436
      case OSPF_IT_BCAST:
437
      case OSPF_IT_NBMA:
438
        if (bcast_net_active(ifa))
439
          add_lsa_rt_link(po, ifa, LSART_NET, ifa->dr_iface_id, ifa->drid), i++;
440
        break;
441

    
442
      case OSPF_IT_VLINK:
443
        neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
444
        if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
445
          add_lsa_rt_link(po, ifa, LSART_VLNK, neigh->iface_id, neigh->rid), i++;
446
        break;
447

    
448
      default:
449
        log("Unknown interface type %s", ifa->iface->name);
450
        break;
451
      }
452

    
453
    ifa->rt_pos_end = i;
454
  }
455

    
456
  if (bitv)
457
    {
458
      rt = po->lsab;
459
      rt->options |= OPT_RT_V;
460
    }
461

    
462
  *length = po->lsab_used + sizeof(struct ospf_lsa_header);
463
  return lsab_flush(po);
464
}
465

    
466
#endif
467

    
468
/**
469
 * originate_rt_lsa - build new instance of router LSA
470
 * @oa: ospf_area which is LSA built to
471
 *
472
 * It builds router LSA walking through all OSPF interfaces in
473
 * specified OSPF area. This function is mostly called from
474
 * area_disp(). Builds new LSA, increases sequence number (if old
475
 * instance exists) and sets age of LSA to zero.
476
 */
477
void
478
originate_rt_lsa(struct ospf_area *oa)
479
{
480
  struct ospf_lsa_header lsa;
481
  struct proto_ospf *po = oa->po;
482
  struct proto *p = &po->proto;
483
  void *body;
484

    
485
  OSPF_TRACE(D_EVENTS, "Originating router-LSA for area %R", oa->areaid);
486

    
487
  lsa.age = 0;
488
  lsa.type = LSA_T_RT;
489
  
490
#ifdef OSPFv2
491
  lsa.options = oa->options;
492
  lsa.id = po->router_id;
493
#else /* OSPFv3 */
494
  lsa.id = 0;
495
#endif
496

    
497
  lsa.rt = po->router_id;
498
  lsa.sn = get_seqnum(oa->rt);
499
  u32 dom = oa->areaid;
500

    
501
  body = originate_rt_lsa_body(oa, &lsa.length);
502
  lsasum_calculate(&lsa, body);
503
  oa->rt = lsa_install_new(po, &lsa, dom, body);
504
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
505
}
506

    
507
void
508
update_rt_lsa(struct ospf_area *oa)
509
{
510
  struct proto_ospf *po = oa->po;
511

    
512
  if ((oa->rt) && ((oa->rt->inst_t + MINLSINTERVAL)) > now)
513
    return;
514
  /*
515
   * Tick is probably set to very low value. We cannot
516
   * originate new LSA before MINLSINTERVAL. We will
517
   * try to do it next tick.
518
   */
519

    
520
  originate_rt_lsa(oa);
521
#ifdef OSPFv3
522
  originate_prefix_rt_lsa(oa);
523
#endif
524

    
525
  schedule_rtcalc(po);
526
  oa->origrt = 0;
527
}
528

    
529
static void *
530
originate_net_lsa_body(struct ospf_iface *ifa, u16 *length,
531
                       struct proto_ospf *po)
532
{
533
  u16 i = 1;
534
  struct ospf_neighbor *n;
535
  struct ospf_lsa_net *net;
536
  int nodes = ifa->fadj + 1;
537

    
538
  net = mb_alloc(po->proto.pool, sizeof(struct ospf_lsa_net)
539
                 + nodes * sizeof(u32));
540

    
541
#ifdef OSPFv2
542
  net->netmask = ipa_mkmask(ifa->addr->pxlen);
543
#endif
544

    
545
#ifdef OSPFv3
546
  /* In OSPFv3, we would like to merge options from Link LSAs of added neighbors */
547
  struct top_hash_entry *en;
548
  u32 options = 0;
549
#endif
550

    
551
  net->routers[0] = po->router_id;
552

    
553
  WALK_LIST(n, ifa->neigh_list)
554
  {
555
    if (n->state == NEIGHBOR_FULL)
556
    {
557
#ifdef OSPFv3
558
      en = ospf_hash_find(po->gr, ifa->iface_id, n->iface_id, n->rid, LSA_T_LINK);
559
      if (en)
560
        options |= ((struct ospf_lsa_link *) en->lsa_body)->options;
561
#endif
562

    
563
      net->routers[i] = n->rid;
564
      i++;
565
    }
566
  }
567
  ASSERT(i == nodes);
568

    
569
#ifdef OSPFv3
570
  net->options = options & OPTIONS_MASK;
571
#endif
572
  
573
  *length = sizeof(struct ospf_lsa_header) + sizeof(struct ospf_lsa_net)
574
    + nodes * sizeof(u32);
575
  return net;
576
}
577

    
578

    
579
/**
580
 * originate_net_lsa - originates of deletes network LSA
581
 * @ifa: interface which is LSA originated for
582
 *
583
 * Interface counts number of adjacent neighbors. If this number is
584
 * lower than one or interface is not in state %OSPF_IS_DR it deletes
585
 * and premature ages instance of network LSA for specified interface.
586
 * In other case, new instance of network LSA is originated.
587
 */
588
void
589
originate_net_lsa(struct ospf_iface *ifa)
590
{
591
  struct proto_ospf *po = ifa->oa->po;
592
  struct proto *p = &po->proto;
593
  struct ospf_lsa_header lsa;
594
  u32 dom = ifa->oa->areaid;
595
  
596
  void *body;
597

    
598
  OSPF_TRACE(D_EVENTS, "Originating network-LSA for iface %s",
599
             ifa->iface->name);
600

    
601
  lsa.age = 0;
602
  lsa.type = LSA_T_NET;
603

    
604
#ifdef OSPFv2
605
  lsa.options = ifa->oa->options;
606
  lsa.id = ipa_to_u32(ifa->addr->ip);
607
#else /* OSPFv3 */
608
  lsa.id = ifa->iface_id;
609
#endif
610

    
611
  lsa.rt = po->router_id;
612
  lsa.sn = get_seqnum(ifa->net_lsa);
613

    
614
  body = originate_net_lsa_body(ifa, &lsa.length, po);
615
  lsasum_calculate(&lsa, body);
616
  ifa->net_lsa = lsa_install_new(po, &lsa, dom, body);
617
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
618
}
619

    
620
void
621
flush_net_lsa(struct ospf_iface *ifa)
622
{
623
  struct proto_ospf *po = ifa->oa->po;
624
  struct proto *p = &po->proto;
625
  u32 dom = ifa->oa->areaid;
626

    
627
  if (ifa->net_lsa == NULL)
628
    return;
629

    
630
  OSPF_TRACE(D_EVENTS, "Flushing network-LSA for iface %s",
631
             ifa->iface->name);
632
  ifa->net_lsa->lsa.sn += 1;
633
  ifa->net_lsa->lsa.age = LSA_MAXAGE;
634
  lsasum_calculate(&ifa->net_lsa->lsa, ifa->net_lsa->lsa_body);
635
  ospf_lsupd_flood(po, NULL, NULL, &ifa->net_lsa->lsa, dom, 0);
636
  flush_lsa(ifa->net_lsa, po);
637
  ifa->net_lsa = NULL;
638
}
639

    
640
void
641
update_net_lsa(struct ospf_iface *ifa)
642
{
643
  struct proto_ospf *po = ifa->oa->po;
644
 
645
  if (ifa->net_lsa && ((ifa->net_lsa->inst_t + MINLSINTERVAL) > now))
646
    return;
647
  /*
648
   * It's too early to originate new network LSA. We will
649
   * try to do it next tick
650
   */
651

    
652
  if ((ifa->state != OSPF_IS_DR) || (ifa->fadj == 0))
653
    {
654
      flush_net_lsa(ifa);
655
#ifdef OSPFv3
656
      flush_prefix_net_lsa(ifa);
657
#endif
658
    }
659
  else
660
    {
661
      originate_net_lsa(ifa);
662
#ifdef OSPFv3
663
      originate_prefix_net_lsa(ifa);
664
#endif
665
    }
666

    
667
  schedule_rtcalc(po);
668
  ifa->orignet = 0;
669
}
670

    
671
#ifdef OSPFv2
672

    
673
static inline void *
674
originate_sum_lsa_body(struct proto_ospf *po, u16 *length, u32 mlen, u32 metric)
675
{
676
  struct ospf_lsa_sum *sum = mb_alloc(po->proto.pool, sizeof(struct ospf_lsa_sum));
677
  *length = sizeof(struct ospf_lsa_header) + sizeof(struct ospf_lsa_sum);
678

    
679
  sum->netmask = ipa_mkmask(mlen);
680
  sum->metric = metric;
681

    
682
  return sum;
683
}
684

    
685
#define originate_sum_net_lsa_body(po,length,fn,metric) \
686
  originate_sum_lsa_body(po, length, (fn)->pxlen, metric)
687

    
688
#define originate_sum_rt_lsa_body(po,length,drid,metric,options) \
689
  originate_sum_lsa_body(po, length, 0, metric)
690

    
691
static inline int
692
check_sum_net_lsaid_collision(struct fib_node *fn, struct top_hash_entry *en)
693
{
694
  struct ospf_lsa_sum *sum = en->lsa_body;
695
  return fn->pxlen != ipa_mklen(sum->netmask);
696
}
697

    
698
static inline int
699
check_sum_lsa_same(struct top_hash_entry *en, u32 metric)
700
{
701
  /* Netmask already checked in check_sum_net_lsaid_collision() */
702
  struct ospf_lsa_sum *sum = en->lsa_body;
703
  return (en->lsa.sn != LSA_MAXSEQNO) && (sum->metric == metric);
704
}
705

    
706
#define check_sum_net_lsa_same(en,metric) \
707
  check_sum_lsa_same(en, metric)
708

    
709
#define check_sum_rt_lsa_same(en,drid,metric,options) \
710
  check_sum_lsa_same(en, metric)
711

    
712

    
713
#else /* OSPFv3 */
714

    
715
static inline void *
716
originate_sum_net_lsa_body(struct proto_ospf *po, u16 *length, struct fib_node *fn, u32 metric)
717
{
718
  int size = sizeof(struct ospf_lsa_sum_net) + IPV6_PREFIX_SPACE(fn->pxlen);
719
  struct ospf_lsa_sum_net *sum = mb_alloc(po->proto.pool, size);
720
  *length = sizeof(struct ospf_lsa_header) + size;
721

    
722
  sum->metric = metric;
723
  put_ipv6_prefix(sum->prefix, fn->prefix, fn->pxlen, 0, 0);
724

    
725
  return sum;
726
}
727

    
728
static inline int
729
check_sum_net_lsaid_collision(struct fib_node *fn, struct top_hash_entry *en)
730
{
731
  struct ospf_lsa_sum_net *sum = en->lsa_body;
732
  ip_addr prefix;
733
  int pxlen;
734
  u8 pxopts;
735
  u16 rest;
736

    
737
  lsa_get_ipv6_prefix(sum->prefix, &prefix, &pxlen, &pxopts, &rest);
738
  return (fn->pxlen != pxlen) || !ipa_equal(fn->prefix, prefix);
739
}
740

    
741
static inline int
742
check_sum_net_lsa_same(struct top_hash_entry *en, u32 metric)
743
{
744
  /* Prefix already checked in check_sum_net_lsaid_collision() */
745
  struct ospf_lsa_sum_net *sum = en->lsa_body;
746
  return (en->lsa.sn != LSA_MAXSEQNO) && (sum->metric == metric);
747
}
748

    
749
static inline void *
750
originate_sum_rt_lsa_body(struct proto_ospf *po, u16 *length, u32 drid, u32 metric, u32 options)
751
{
752
  struct ospf_lsa_sum_rt *sum = mb_alloc(po->proto.pool, sizeof(struct ospf_lsa_sum_rt));
753
  *length = sizeof(struct ospf_lsa_header) + sizeof(struct ospf_lsa_sum_rt);
754

    
755
  sum->options = options;
756
  sum->metric = metric;
757
  sum->drid = drid;
758

    
759
  return sum;
760
}
761

    
762
static inline int
763
check_sum_rt_lsa_same(struct top_hash_entry *en, u32 drid, u32 metric, u32 options)
764
{
765
  struct ospf_lsa_sum_rt *sum = en->lsa_body;
766
  return (en->lsa.sn != LSA_MAXSEQNO) && (sum->options == options) &&
767
    (sum->metric == metric) && (sum->drid == drid);
768
}
769

    
770
#endif
771

    
772
void
773
originate_sum_net_lsa(struct ospf_area *oa, struct fib_node *fn, int metric)
774
{
775
  struct proto_ospf *po = oa->po;
776
  struct proto *p = &po->proto;
777
  struct top_hash_entry *en;
778
  u32 dom = oa->areaid;
779
  struct ospf_lsa_header lsa;
780
  void *body;
781

    
782
  OSPF_TRACE(D_EVENTS, "Originating net-summary-LSA for %I/%d (metric %d)",
783
             fn->prefix, fn->pxlen, metric);
784

    
785
  /* options argument is used in ORT_NET and OSPFv3 only */
786
  lsa.age = 0;
787
#ifdef OSPFv2
788
  lsa.options = oa->options;
789
#endif
790
  lsa.type = LSA_T_SUM_NET;
791
  lsa.id = fibnode_to_lsaid(po, fn);
792
  lsa.rt = po->router_id;
793

    
794
  if ((en = ospf_hash_find_header(po->gr, dom, &lsa)) != NULL)
795
  {
796
    if (check_sum_net_lsaid_collision(fn, en))
797
    {
798
      log(L_ERR "%s: LSAID collision for %I/%d",
799
          p->name, fn->prefix, fn->pxlen);
800
      return;
801
    }
802

    
803
    if (check_sum_net_lsa_same(en, metric))
804
      return;
805
  }
806
  lsa.sn = get_seqnum(en);
807

    
808
  body = originate_sum_net_lsa_body(po, &lsa.length, fn, metric);
809
  lsasum_calculate(&lsa, body);
810
  lsa_install_new(po, &lsa, dom, body);
811
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
812
}
813

    
814
void
815
originate_sum_rt_lsa(struct ospf_area *oa, struct fib_node *fn, int metric, u32 options UNUSED)
816
{
817
  struct proto_ospf *po = oa->po;
818
  struct proto *p = &po->proto;
819
  struct top_hash_entry *en;
820
  u32 dom = oa->areaid;
821
  u32 rid = ipa_to_rid(fn->prefix);
822
  struct ospf_lsa_header lsa;
823
  void *body;
824

    
825
  OSPF_TRACE(D_EVENTS, "Originating rt-summary-LSA for %R (metric %d)",
826
             rid, metric);
827

    
828
  lsa.age = 0;
829
#ifdef OSPFv2
830
  lsa.options = oa->options;
831
#endif
832
  lsa.type = LSA_T_SUM_RT;
833
  /* In OSPFv3, LSA ID is meaningless, but we still use Router ID of ASBR */
834
  lsa.id = rid;
835
  lsa.rt = po->router_id;
836

    
837
  options &= OPTIONS_MASK;
838
  if ((en = ospf_hash_find_header(po->gr, dom, &lsa)) != NULL)
839
  {
840
    if (check_sum_rt_lsa_same(en, lsa.id, metric, options))
841
      return;
842
  }
843
  lsa.sn = get_seqnum(en);
844

    
845
  body = originate_sum_rt_lsa_body(po, &lsa.length, lsa.id, metric, options);
846
  lsasum_calculate(&lsa, body);
847
  lsa_install_new(po, &lsa, dom, body);
848
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
849
}
850

    
851
void
852
flush_sum_lsa(struct ospf_area *oa, struct fib_node *fn, int type)
853
{
854
  struct proto_ospf *po = oa->po;
855
  struct proto *p = &po->proto;
856
  struct top_hash_entry *en;
857
  struct ospf_lsa_header lsa;
858

    
859
  lsa.rt = po->router_id;
860
  if (type == ORT_NET)
861
    {
862
      lsa.id = fibnode_to_lsaid(po, fn);
863
      lsa.type = LSA_T_SUM_NET;
864
    }
865
  else
866
    {
867
      /* In OSPFv3, LSA ID is meaningless, but we still use Router ID of ASBR */
868
      lsa.id = ipa_to_rid(fn->prefix);
869
      lsa.type = LSA_T_SUM_RT;
870
    }
871

    
872
  if ((en = ospf_hash_find_header(po->gr, oa->areaid, &lsa)) != NULL)
873
    {
874
      OSPF_TRACE(D_EVENTS, "Flushing summary-LSA (id=%R, type=%d)",
875
                 en->lsa.id, en->lsa.type);
876

    
877
      if ((type == ORT_NET) && check_sum_net_lsaid_collision(fn, en))
878
        {
879
          log(L_ERR "%s: LSAID collision for %I/%d",
880
              p->name, fn->prefix, fn->pxlen);
881
          return;
882
        }
883

    
884
      struct ospf_lsa_sum *sum = en->lsa_body;
885
      en->lsa.age = LSA_MAXAGE;
886
      en->lsa.sn = LSA_MAXSEQNO;
887
      lsasum_calculate(&en->lsa, sum);
888
      ospf_lsupd_flood(po, NULL, NULL, &en->lsa, oa->areaid, 1);
889
      if (can_flush_lsa(po)) flush_lsa(en, po);
890
    }
891
}
892

    
893
#ifdef OSPFv2
894

    
895
static inline void *
896
originate_ext_lsa_body(struct proto_ospf *po, u16 *length, struct fib_node *fn,
897
                       u32 metric, ip_addr fwaddr, u32 tag, int pbit UNUSED)
898
{
899
  struct ospf_lsa_ext *ext = mb_alloc(po->proto.pool, sizeof(struct ospf_lsa_ext));
900
  *length = sizeof(struct ospf_lsa_header) + sizeof(struct ospf_lsa_ext);
901

    
902
  ext->metric = metric; 
903
  ext->netmask = ipa_mkmask(fn->pxlen);
904
  ext->fwaddr = fwaddr;
905
  ext->tag = tag;
906

    
907
  return ext;
908
}
909

    
910
/*
911
 * check_ext_lsa() combines functions of check_*_lsaid_collision() and
912
 * check_*_lsa_same(). 'en' is existing ext LSA, and rest parameters
913
 * are parameters of new ext route.  Function returns -1 if there is
914
 * LSAID collision, returns 1 if the existing LSA is the same and
915
 * returns 0 otherwise (in that case, we need to originate a new LSA).
916
 *
917
 * Really, checking for the same parameters is not as important as in
918
 * summary LSA origination, because in most cases the duplicate
919
 * external route propagation would be stopped by the nest. But there
920
 * are still some cases (route reload, the same route propagated through
921
 * different protocol) so it is also done here.
922
 */
923

    
924
static inline int
925
check_ext_lsa(struct top_hash_entry *en, struct fib_node *fn, u32 metric, ip_addr fwaddr, u32 tag)
926
{
927
  struct ospf_lsa_ext *ext = en->lsa_body;
928

    
929
  /* LSAID collision */
930
  if  (fn->pxlen != ipa_mklen(ext->netmask))
931
    return -1;
932

    
933
  return (en->lsa.sn != LSA_MAXSEQNO) && (ext->metric == metric) &&
934
    (ext->tag == tag) && ipa_equal(ext->fwaddr,fwaddr);
935
}
936

    
937
#else /* OSPFv3 */
938

    
939
static inline void *
940
originate_ext_lsa_body(struct proto_ospf *po, u16 *length, struct fib_node *fn,
941
                       u32 metric, ip_addr fwaddr, u32 tag, int pbit)
942
{
943
  int size = sizeof(struct ospf_lsa_ext)
944
    + IPV6_PREFIX_SPACE(fn->pxlen)
945
    + (ipa_nonzero(fwaddr) ? 16 : 0)
946
    + (tag ? 4 : 0);
947

    
948
  struct ospf_lsa_ext *ext = mb_alloc(po->proto.pool, size);
949
  *length = sizeof(struct ospf_lsa_header) + size;
950

    
951
  ext->metric = metric;
952

    
953
  u32 *buf = ext->rest;
954
  buf = put_ipv6_prefix(buf, fn->prefix, fn->pxlen, pbit ? OPT_PX_P : 0, 0);
955

    
956
  if (ipa_nonzero(fwaddr))
957
  {
958
    ext->metric |= LSA_EXT_FBIT;
959
    buf = put_ipv6_addr(buf, fwaddr);
960
  }
961

    
962
  if (tag)
963
  {
964
    ext->metric |= LSA_EXT_TBIT;
965
    *buf++ = tag;
966
  }
967

    
968
  return ext;
969
}
970

    
971
static inline int
972
check_ext_lsa(struct top_hash_entry *en, struct fib_node *fn, u32 metric, ip_addr fwaddr, u32 tag)
973
{
974
  struct ospf_lsa_ext *ext = en->lsa_body;
975
  ip_addr prefix;
976
  int pxlen;
977
  u8 pxopts;
978
  u16 rest;
979

    
980
  u32 *buf = lsa_get_ipv6_prefix(ext->rest, &prefix, &pxlen, &pxopts, &rest);
981

    
982
  /* LSAID collision */
983
  if ((fn->pxlen != pxlen) || !ipa_equal(fn->prefix, prefix))
984
    return -1;
985

    
986
  if (en->lsa.sn == LSA_MAXSEQNO)
987
    return 0;
988

    
989
  u32 rt_metric = ext->metric & METRIC_MASK;
990
  ip_addr rt_fwaddr = IPA_NONE;
991
  u32 rt_tag = 0;
992

    
993
  if (ext->metric & LSA_EXT_FBIT)
994
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
995

    
996
  if (ext->metric & LSA_EXT_TBIT)
997
    rt_tag = *buf++;
998

    
999
  return (rt_metric == metric) && ipa_equal(rt_fwaddr, fwaddr) && (rt_tag == tag);
1000
}
1001

    
1002

    
1003
#endif
1004

    
1005
static inline ip_addr
1006
find_surrogate_fwaddr(struct ospf_area *oa)
1007
{
1008
  struct proto_ospf *po = oa->po;
1009
  struct ospf_iface *ifa;
1010
  struct ifa *a, *cur_addr = NULL;
1011
  int np, cur_np = 0;
1012

    
1013
  WALK_LIST(ifa, po->iface_list)
1014
  {
1015
    if ((ifa->oa != oa) ||
1016
        (ifa->type == OSPF_IT_VLINK))
1017
      continue;
1018

    
1019
#ifdef OSPFv2
1020
    a = ifa->addr;
1021
    if (a->flags & IA_PEER)
1022
      continue;
1023

    
1024
    np = ((a->flags & IA_HOST) || ifa->stub) ? 2 : 1;
1025
    if (np > cur_np)
1026
    {
1027
      cur_addr = a;
1028
      cur_np = np;
1029
    }
1030

    
1031
#else /* OSPFv3 */
1032
    WALK_LIST(a, ifa->iface->addrs)
1033
    {
1034
      if ((a->flags & IA_SECONDARY) ||
1035
          (a->flags & IA_PEER) ||
1036
          (a->scope <= SCOPE_LINK))
1037
        continue;
1038

    
1039
      np = ((a->flags & IA_HOST) || ifa->stub) ? 2 : 1;
1040
      if (np > cur_np)
1041
      {
1042
        cur_addr = a;
1043
        cur_np = np;
1044
      }
1045
    }
1046
#endif
1047
  }
1048

    
1049
  return cur_addr ? cur_addr->ip : IPA_NONE;
1050
}
1051

    
1052

    
1053
/**
1054
 * originate_ext_lsa - new route received from nest and filters
1055
 * @oa: ospf_area for which LSA is originated
1056
 * @fn: network prefix and mask
1057
 * @src: the source of origination of the LSA (EXT_EXPORT/EXT_NSSA)
1058
 * @metric: the metric of a route
1059
 * @fwaddr: the forwarding address
1060
 * @tag: the route tag
1061
 * @pbit: P-bit for NSSA LSAs, ignored for external LSAs
1062
 *
1063
 * If I receive a message that new route is installed, I try to originate an
1064
 * external LSA. If @oa is an NSSA area, NSSA-LSA is originated instead.
1065
 * @oa should not be a stub area. @src does not specify whether the LSA
1066
 * is external or NSSA, but it specifies the source of origination - 
1067
 * the export from ospf_rt_notify(), or the NSSA-EXT translation.
1068
 *
1069
 * The function also sets flag ebit. If it's the first time, the new router lsa
1070
 * origination is necessary.
1071
 */
1072
void
1073
originate_ext_lsa(struct ospf_area *oa, struct fib_node *fn, int src,
1074
                  u32 metric, ip_addr fwaddr, u32 tag, int pbit)
1075
{
1076
  struct proto_ospf *po = oa->po;
1077
  struct proto *p = &po->proto;
1078
  struct ospf_lsa_header lsa;
1079
  struct top_hash_entry *en = NULL;
1080
  void *body;
1081
  int nssa = oa_is_nssa(oa);
1082
  u32 dom = nssa ? oa->areaid : 0;
1083

    
1084
  OSPF_TRACE(D_EVENTS, "Originating %s-LSA for %I/%d",
1085
             nssa ? "NSSA" : "AS-external", fn->prefix, fn->pxlen);
1086

    
1087
  lsa.age = 0;
1088
#ifdef OSPFv2
1089
  lsa.options = nssa ? (pbit ? OPT_P : 0) : OPT_E;
1090
#endif
1091
  lsa.type = nssa ? LSA_T_NSSA : LSA_T_EXT;
1092
  lsa.id = fibnode_to_lsaid(po, fn);
1093
  lsa.rt = po->router_id;
1094

    
1095
  if (nssa && pbit && ipa_zero(fwaddr))
1096
  {
1097
    /* NSSA-LSA with P-bit set must have non-zero forwarding address */
1098

    
1099
    fwaddr = find_surrogate_fwaddr(oa);
1100
    if (ipa_zero(fwaddr))
1101
    {
1102
      log(L_ERR "%s: Cannot find forwarding address for NSSA-LSA %I/%d",
1103
          p->name, fn->prefix, fn->pxlen);
1104
      return;
1105
    }
1106
  }
1107

    
1108
  if ((en = ospf_hash_find_header(po->gr, dom, &lsa)) != NULL)
1109
  {
1110
    int rv = check_ext_lsa(en, fn, metric, fwaddr, tag);
1111
    if (rv < 0)
1112
    {
1113
      log(L_ERR "%s: LSAID collision for %I/%d",
1114
          p->name, fn->prefix, fn->pxlen);
1115
      return;
1116
    }
1117

    
1118
    if (rv > 0)
1119
      return;
1120
  }
1121
  lsa.sn = get_seqnum(en);
1122

    
1123
  body = originate_ext_lsa_body(po, &lsa.length, fn, metric, fwaddr, tag, pbit);
1124
  lsasum_calculate(&lsa, body);
1125

    
1126
  if (src) 
1127
    fn->x1 = src;
1128

    
1129
  lsa_install_new(po, &lsa, dom, body);
1130
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
1131

    
1132
  if (po->ebit == 0)
1133
  {
1134
    po->ebit = 1;
1135
    WALK_LIST(oa, po->area_list)
1136
    {
1137
      schedule_rt_lsa(oa);
1138
    }
1139
  }
1140
}
1141

    
1142
void
1143
flush_ext_lsa(struct ospf_area *oa, struct fib_node *fn, int nssa)
1144
{
1145
  struct proto_ospf *po = oa->po;
1146
  struct proto *p = &po->proto;
1147
  struct top_hash_entry *en;
1148

    
1149
  u32 dom = nssa ? oa->areaid : 0;
1150
  u32 type = nssa ? LSA_T_NSSA : LSA_T_EXT;
1151
  u32 lsaid = fibnode_to_lsaid(po, fn);
1152

    
1153
  if (en = ospf_hash_find(po->gr, dom, lsaid, po->router_id, type))
1154
    {
1155
      OSPF_TRACE(D_EVENTS, "Flushing %s-LSA for %I/%d",
1156
                 nssa ? "NSSA" : "AS-external", fn->prefix, fn->pxlen);
1157

    
1158
      if (check_ext_lsa(en, fn, 0, IPA_NONE, 0) < 0)
1159
        {
1160
          log(L_ERR "%s: LSAID collision for %I/%d",
1161
              p->name, fn->prefix, fn->pxlen);
1162
          return;
1163
        }
1164

    
1165
      fn->x1 = 0;
1166
      ospf_lsupd_flush_nlsa(po, en);
1167
    }
1168
}
1169

    
1170

    
1171
#ifdef OSPFv3
1172

    
1173
static void *
1174
originate_link_lsa_body(struct ospf_iface *ifa, u16 *length)
1175
{
1176
  struct proto_ospf *po = ifa->oa->po;
1177
  struct ospf_lsa_link *ll;
1178
  int i = 0;
1179
  u8 flags;
1180

    
1181
  ASSERT(po->lsab_used == 0);
1182
  ll = lsab_allocz(po, sizeof(struct ospf_lsa_link));
1183
  ll->options = ifa->oa->options | (ifa->priority << 24);
1184
  ll->lladdr = ifa->addr->ip;
1185
  ll = NULL; /* buffer might be reallocated later */
1186

    
1187
  struct ifa *a;
1188
  WALK_LIST(a, ifa->iface->addrs)
1189
    {
1190
      if ((a->flags & IA_SECONDARY) ||
1191
          (a->scope < SCOPE_SITE))
1192
        continue;
1193

    
1194
      flags = (a->pxlen < MAX_PREFIX_LENGTH) ? 0 : OPT_PX_LA;
1195
      put_ipv6_prefix(lsab_alloc(po, IPV6_PREFIX_SPACE(a->pxlen)),
1196
                      a->ip, a->pxlen, flags, 0);
1197
      i++;
1198
    }
1199

    
1200
  ll = po->lsab;
1201
  ll->pxcount = i;
1202
  *length = po->lsab_used + sizeof(struct ospf_lsa_header);
1203
  return lsab_flush(po);
1204
}
1205

    
1206
void
1207
originate_link_lsa(struct ospf_iface *ifa)
1208
{
1209
  struct ospf_lsa_header lsa;
1210
  struct proto_ospf *po = ifa->oa->po;
1211
  struct proto *p = &po->proto;
1212
  void *body;
1213

    
1214
  /* FIXME check for vlink and skip that? */
1215
  OSPF_TRACE(D_EVENTS, "Originating link-LSA for iface %s", ifa->iface->name);
1216

    
1217
  lsa.age = 0;
1218
  lsa.type = LSA_T_LINK;
1219
  lsa.id = ifa->iface_id;
1220
  lsa.rt = po->router_id;
1221
  lsa.sn = get_seqnum(ifa->link_lsa);
1222
  u32 dom = ifa->iface_id;
1223

    
1224
  body = originate_link_lsa_body(ifa, &lsa.length);
1225
  lsasum_calculate(&lsa, body);
1226
  ifa->link_lsa = lsa_install_new(po, &lsa, dom, body);
1227
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
1228

    
1229
  /* Just to be sure to not forget on our link LSA */
1230
  if (ifa->state == OSPF_IS_DR)
1231
    schedule_net_lsa(ifa);
1232
}
1233

    
1234
void
1235
update_link_lsa(struct ospf_iface *ifa)
1236
{
1237
  if (ifa->link_lsa && ((ifa->link_lsa->inst_t + MINLSINTERVAL) > now))
1238
    return;
1239
  /*
1240
   * It's too early to originate new link LSA. We will
1241
   * try to do it next tick
1242
   */
1243
  originate_link_lsa(ifa);
1244
  ifa->origlink = 0;
1245
}
1246

    
1247
static inline void
1248
lsa_put_prefix(struct proto_ospf *po, ip_addr prefix, u32 pxlen, u32 cost)
1249
{
1250
  put_ipv6_prefix(lsab_alloc(po, IPV6_PREFIX_SPACE(pxlen)), prefix, pxlen,
1251
                  (pxlen < MAX_PREFIX_LENGTH) ? 0 : OPT_PX_LA, cost);
1252
}
1253

    
1254
static void *
1255
originate_prefix_rt_lsa_body(struct ospf_area *oa, u16 *length)
1256
{
1257
  struct proto_ospf *po = oa->po;
1258
  struct ospf_config *cf = (struct ospf_config *) (po->proto.cf);
1259
  struct ospf_iface *ifa;
1260
  struct ospf_lsa_prefix *lp;
1261
  int host_addr = 0;
1262
  int net_lsa;
1263
  int i = 0;
1264

    
1265
  ASSERT(po->lsab_used == 0);
1266
  lp = lsab_allocz(po, sizeof(struct ospf_lsa_prefix));
1267
  lp->ref_type = LSA_T_RT;
1268
  lp->ref_id = 0;
1269
  lp->ref_rt = po->router_id;
1270
  lp = NULL; /* buffer might be reallocated later */
1271

    
1272
  WALK_LIST(ifa, po->iface_list)
1273
  {
1274
    if ((ifa->oa != oa) || (ifa->type == OSPF_IT_VLINK) || (ifa->state == OSPF_IS_DOWN))
1275
      continue;
1276

    
1277
    ifa->px_pos_beg = i;
1278

    
1279
    if ((ifa->type == OSPF_IT_BCAST) ||
1280
        (ifa->type == OSPF_IT_NBMA))
1281
      net_lsa = bcast_net_active(ifa);
1282
    else
1283
      net_lsa = 0;
1284

    
1285
    struct ifa *a;
1286
    WALK_LIST(a, ifa->iface->addrs)
1287
      {
1288
        if ((a->flags & IA_SECONDARY) ||
1289
            (a->flags & IA_PEER) ||
1290
            (a->scope <= SCOPE_LINK))
1291
          continue;
1292

    
1293
        if (((a->pxlen < MAX_PREFIX_LENGTH) && net_lsa) ||
1294
            configured_stubnet(oa, a))
1295
          continue;
1296

    
1297
        if ((a->flags & IA_HOST) ||
1298
            (ifa->state == OSPF_IS_LOOP) ||
1299
            (ifa->type == OSPF_IT_PTMP))
1300
        {
1301
          lsa_put_prefix(po, a->ip, MAX_PREFIX_LENGTH, 0);
1302
          host_addr = 1;
1303
        }
1304
        else
1305
          lsa_put_prefix(po, a->prefix, a->pxlen, ifa->cost);
1306
        i++;
1307
      }
1308

    
1309
    ifa->px_pos_end = i;
1310
  }
1311

    
1312
  struct ospf_stubnet_config *sn;
1313
  if (oa->ac)
1314
    WALK_LIST(sn, oa->ac->stubnet_list)
1315
      if (!sn->hidden)
1316
      {
1317
        lsa_put_prefix(po, sn->px.addr, sn->px.len, sn->cost);
1318
        if (sn->px.len == MAX_PREFIX_LENGTH)
1319
          host_addr = 1;
1320
        i++;
1321
      }
1322

    
1323
  /* If there are some configured vlinks, find some global address
1324
     (even from another area), which will be used as a vlink endpoint. */
1325
  if (!EMPTY_LIST(cf->vlink_list) && !host_addr)
1326
  {
1327
    WALK_LIST(ifa, po->iface_list)
1328
    {
1329
      if ((ifa->type == OSPF_IT_VLINK) || (ifa->state == OSPF_IS_DOWN))
1330
        continue;
1331

    
1332
      struct ifa *a;
1333
      WALK_LIST(a, ifa->iface->addrs)
1334
      {
1335
        if ((a->flags & IA_SECONDARY) || (a->scope <= SCOPE_LINK))
1336
          continue;
1337

    
1338
        /* Found some IP */
1339
        lsa_put_prefix(po, a->ip, MAX_PREFIX_LENGTH, 0);
1340
        i++;
1341
        goto done;
1342
      }
1343
    }
1344
  }
1345

    
1346
 done:
1347
  lp = po->lsab;
1348
  lp->pxcount = i;
1349
  *length = po->lsab_used + sizeof(struct ospf_lsa_header);
1350
  return lsab_flush(po);
1351
}
1352

    
1353
void
1354
originate_prefix_rt_lsa(struct ospf_area *oa)
1355
{
1356
  struct proto_ospf *po = oa->po;
1357
  struct proto *p = &po->proto;  
1358
  struct ospf_lsa_header lsa;
1359
  void *body;
1360

    
1361
  OSPF_TRACE(D_EVENTS, "Originating router prefix-LSA for area %R", oa->areaid);
1362

    
1363
  lsa.age = 0;
1364
  lsa.type = LSA_T_PREFIX;
1365
  lsa.id = 0;
1366
  lsa.rt = po->router_id;
1367
  lsa.sn = get_seqnum(oa->pxr_lsa);
1368
  u32 dom = oa->areaid;
1369

    
1370
  body = originate_prefix_rt_lsa_body(oa, &lsa.length);
1371
  lsasum_calculate(&lsa, body);
1372
  oa->pxr_lsa = lsa_install_new(po, &lsa, dom, body);
1373
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
1374
}
1375

    
1376

    
1377
static inline int
1378
prefix_space(u32 *buf)
1379
{
1380
  int pxl = *buf >> 24;
1381
  return IPV6_PREFIX_SPACE(pxl);
1382
}
1383

    
1384
static inline int
1385
prefix_same(u32 *b1, u32 *b2)
1386
{
1387
  int pxl1 = *b1 >> 24;
1388
  int pxl2 = *b2 >> 24;
1389
  int pxs, i;
1390
  
1391
  if (pxl1 != pxl2)
1392
    return 0;
1393

    
1394
  pxs = IPV6_PREFIX_WORDS(pxl1);
1395
  for (i = 1; i < pxs; i++)
1396
    if (b1[i] != b2[i])
1397
      return 0;
1398

    
1399
  return 1;
1400
}
1401

    
1402
static inline u32 *
1403
prefix_advance(u32 *buf)
1404
{
1405
  int pxl = *buf >> 24;
1406
  return buf + IPV6_PREFIX_WORDS(pxl);
1407
}
1408

    
1409
/* FIXME eliminate items with LA bit set? see 4.4.3.9 */
1410
static void
1411
add_prefix(struct proto_ospf *po, u32 *px, int offset, int *pxc)
1412
{
1413
  u32 *pxl = lsab_offset(po, offset);
1414
  int i;
1415
  for (i = 0; i < *pxc; pxl = prefix_advance(pxl), i++)
1416
    if (prefix_same(px, pxl))
1417
    {
1418
      /* Options should be logically OR'ed together */
1419
      *pxl |= (*px & 0x00FF0000);
1420
      return;
1421
    }
1422

    
1423
  ASSERT(pxl == lsab_end(po));
1424

    
1425
  int pxspace = prefix_space(px);
1426
  pxl = lsab_alloc(po, pxspace);
1427
  memcpy(pxl, px, pxspace);
1428
  *pxl &= 0xFFFF0000;        /* Set metric to zero */
1429
  (*pxc)++;
1430
}
1431

    
1432
static void
1433
add_link_lsa(struct proto_ospf *po, struct top_hash_entry *en, int offset, int *pxc)
1434
{
1435
  struct ospf_lsa_link *ll = en->lsa_body;
1436
  u32 *pxb = ll->rest;
1437
  int j;
1438

    
1439
  for (j = 0; j < ll->pxcount; pxb = prefix_advance(pxb), j++)
1440
  {
1441
    u8 pxlen = (pxb[0] >> 24);
1442
    u8 pxopts = (pxb[0] >> 16);
1443

    
1444
    /* Skip NU or LA prefixes */
1445
    if (pxopts & (OPT_PX_NU | OPT_PX_LA))
1446
      continue;
1447

    
1448
    /* Skip link-local prefixes */
1449
    if ((pxlen >= 10) && ((pxb[1] & 0xffc00000) == 0xfe800000))
1450
      continue;
1451

    
1452
    add_prefix(po, pxb, offset, pxc);
1453
  }
1454
}
1455

    
1456

    
1457

    
1458
static void *
1459
originate_prefix_net_lsa_body(struct ospf_iface *ifa, u16 *length)
1460
{
1461
  struct proto_ospf *po = ifa->oa->po;
1462
  struct ospf_lsa_prefix *lp;
1463
  struct ospf_neighbor *n;
1464
  struct top_hash_entry *en;
1465
  int pxc, offset;
1466

    
1467
  ASSERT(po->lsab_used == 0);
1468
  lp = lsab_allocz(po, sizeof(struct ospf_lsa_prefix));
1469
  lp->ref_type = LSA_T_NET;
1470
  lp->ref_id = ifa->net_lsa->lsa.id;
1471
  lp->ref_rt = po->router_id;
1472
  lp = NULL; /* buffer might be reallocated later */
1473

    
1474
  pxc = 0;
1475
  offset = po->lsab_used;
1476

    
1477
  /* Find all Link LSAs associated with the link and merge their prefixes */
1478
  if (ifa->link_lsa)
1479
    add_link_lsa(po, ifa->link_lsa, offset, &pxc);
1480

    
1481
  WALK_LIST(n, ifa->neigh_list)
1482
    if ((n->state == NEIGHBOR_FULL) &&
1483
              (en = ospf_hash_find(po->gr, ifa->iface_id, n->iface_id, n->rid, LSA_T_LINK)))
1484
      add_link_lsa(po, en, offset, &pxc);
1485

    
1486
  lp = po->lsab;
1487
  lp->pxcount = pxc;
1488
  *length = po->lsab_used + sizeof(struct ospf_lsa_header);
1489
  return lsab_flush(po);
1490
}
1491

    
1492
void
1493
originate_prefix_net_lsa(struct ospf_iface *ifa)
1494
{
1495
  struct proto_ospf *po = ifa->oa->po;
1496
  struct proto *p = &po->proto;
1497
  struct ospf_lsa_header lsa;
1498
  void *body;
1499

    
1500
  OSPF_TRACE(D_EVENTS, "Originating network prefix-LSA for iface %s",
1501
             ifa->iface->name);
1502

    
1503
  lsa.age = 0;
1504
  lsa.type = LSA_T_PREFIX;
1505
  lsa.id = ifa->iface_id;
1506
  lsa.rt = po->router_id;
1507
  lsa.sn = get_seqnum(ifa->pxn_lsa);
1508
  u32 dom = ifa->oa->areaid;
1509

    
1510
  body = originate_prefix_net_lsa_body(ifa, &lsa.length);
1511
  lsasum_calculate(&lsa, body);
1512
  ifa->pxn_lsa = lsa_install_new(po, &lsa, dom, body);
1513
  ospf_lsupd_flood(po, NULL, NULL, &lsa, dom, 1);
1514
}
1515

    
1516
void
1517
flush_prefix_net_lsa(struct ospf_iface *ifa)
1518
{
1519
  struct proto_ospf *po = ifa->oa->po;
1520
  struct proto *p = &po->proto;
1521
  struct top_hash_entry *en = ifa->pxn_lsa;
1522
  u32 dom = ifa->oa->areaid;
1523

    
1524
  if (en == NULL)
1525
    return;
1526

    
1527
  OSPF_TRACE(D_EVENTS, "Flushing network prefix-LSA for iface %s",
1528
             ifa->iface->name);
1529

    
1530
  en->lsa.sn += 1;
1531
  en->lsa.age = LSA_MAXAGE;
1532
  lsasum_calculate(&en->lsa, en->lsa_body);
1533
  ospf_lsupd_flood(po, NULL, NULL, &en->lsa, dom, 0);
1534
  flush_lsa(en, po);
1535
  ifa->pxn_lsa = NULL;
1536
}
1537

    
1538

    
1539
#endif
1540

    
1541

    
1542
static void
1543
ospf_top_ht_alloc(struct top_graph *f)
1544
{
1545
  f->hash_size = 1 << f->hash_order;
1546
  f->hash_mask = f->hash_size - 1;
1547
  if (f->hash_order > HASH_HI_MAX - HASH_HI_STEP)
1548
    f->hash_entries_max = ~0;
1549
  else
1550
    f->hash_entries_max = f->hash_size HASH_HI_MARK;
1551
  if (f->hash_order < HASH_LO_MIN + HASH_LO_STEP)
1552
    f->hash_entries_min = 0;
1553
  else
1554
    f->hash_entries_min = f->hash_size HASH_LO_MARK;
1555
  DBG("Allocating OSPF hash of order %d: %d hash_entries, %d low, %d high\n",
1556
      f->hash_order, f->hash_size, f->hash_entries_min, f->hash_entries_max);
1557
  f->hash_table =
1558
    mb_alloc(f->pool, f->hash_size * sizeof(struct top_hash_entry *));
1559
  bzero(f->hash_table, f->hash_size * sizeof(struct top_hash_entry *));
1560
}
1561

    
1562
static inline void
1563
ospf_top_ht_free(struct top_hash_entry **h)
1564
{
1565
  mb_free(h);
1566
}
1567

    
1568
static inline u32
1569
ospf_top_hash_u32(u32 a)
1570
{
1571
  /* Shamelessly stolen from IP address hashing in ipv4.h */
1572
  a ^= a >> 16;
1573
  a ^= a << 10;
1574
  return a;
1575
}
1576

    
1577
static inline unsigned
1578
ospf_top_hash(struct top_graph *f, u32 domain, u32 lsaid, u32 rtrid, u32 type)
1579
{
1580
  /* In OSPFv2, we don't know Router ID when looking for network LSAs.
1581
     In OSPFv3, we don't know LSA ID when looking for router LSAs.
1582
     In both cases, there is (usually) just one (or small number)
1583
     appropriate LSA, so we just clear unknown part of key. */
1584

    
1585
  return (
1586
#ifdef OSPFv2
1587
          ((type == LSA_T_NET) ? 0 : ospf_top_hash_u32(rtrid)) +
1588
          ospf_top_hash_u32(lsaid) + 
1589
#else /* OSPFv3 */
1590
          ospf_top_hash_u32(rtrid) +
1591
          ((type == LSA_T_RT) ? 0 : ospf_top_hash_u32(lsaid)) +
1592
#endif
1593
          type + domain) & f->hash_mask;
1594

    
1595
  /*
1596
  return (ospf_top_hash_u32(lsaid) + ospf_top_hash_u32(rtrid) +
1597
          type + areaid) & f->hash_mask;
1598
  */
1599
}
1600

    
1601
/**
1602
 * ospf_top_new - allocated new topology database
1603
 * @p: current instance of ospf
1604
 *
1605
 * this dynamically hashed structure is often used for keeping lsas. mainly
1606
 * its used in @ospf_area structure.
1607
 */
1608
struct top_graph *
1609
ospf_top_new(pool *pool)
1610
{
1611
  struct top_graph *f;
1612

    
1613
  f = mb_allocz(pool, sizeof(struct top_graph));
1614
  f->pool = pool;
1615
  f->hash_slab = sl_new(f->pool, sizeof(struct top_hash_entry));
1616
  f->hash_order = HASH_DEF_ORDER;
1617
  ospf_top_ht_alloc(f);
1618
  f->hash_entries = 0;
1619
  f->hash_entries_min = 0;
1620
  return f;
1621
}
1622

    
1623
void
1624
ospf_top_free(struct top_graph *f)
1625
{
1626
  rfree(f->hash_slab);
1627
  ospf_top_ht_free(f->hash_table);
1628
  mb_free(f);
1629
}
1630

    
1631
static void
1632
ospf_top_rehash(struct top_graph *f, int step)
1633
{
1634
  unsigned int oldn, oldh;
1635
  struct top_hash_entry **n, **oldt, **newt, *e, *x;
1636

    
1637
  oldn = f->hash_size;
1638
  oldt = f->hash_table;
1639
  DBG("re-hashing topology hash from order %d to %d\n", f->hash_order,
1640
      f->hash_order + step);
1641
  f->hash_order += step;
1642
  ospf_top_ht_alloc(f);
1643
  newt = f->hash_table;
1644

    
1645
  for (oldh = 0; oldh < oldn; oldh++)
1646
  {
1647
    e = oldt[oldh];
1648
    while (e)
1649
    {
1650
      x = e->next;
1651
      n = newt + ospf_top_hash(f, e->domain, e->lsa.id, e->lsa.rt, e->lsa.type);
1652
      e->next = *n;
1653
      *n = e;
1654
      e = x;
1655
    }
1656
  }
1657
  ospf_top_ht_free(oldt);
1658
}
1659

    
1660
#ifdef OSPFv2
1661

    
1662
u32
1663
ospf_lsa_domain(u32 type, struct ospf_iface *ifa)
1664
{
1665
  return (type == LSA_T_EXT) ? 0 : ifa->oa->areaid;
1666
}
1667

    
1668
#else /* OSPFv3 */
1669

    
1670
u32
1671
ospf_lsa_domain(u32 type, struct ospf_iface *ifa)
1672
{
1673
  switch (type & LSA_SCOPE_MASK)
1674
    {
1675
    case LSA_SCOPE_LINK:
1676
      return ifa->iface_id;
1677

    
1678
    case LSA_SCOPE_AREA:
1679
      return ifa->oa->areaid;
1680

    
1681
    case LSA_SCOPE_AS:
1682
    default:
1683
      return 0;
1684
    }
1685
}
1686

    
1687
#endif
1688

    
1689
struct top_hash_entry *
1690
ospf_hash_find_header(struct top_graph *f, u32 domain, struct ospf_lsa_header *h)
1691
{
1692
  return ospf_hash_find(f, domain, h->id, h->rt, h->type);
1693
}
1694

    
1695
struct top_hash_entry *
1696
ospf_hash_get_header(struct top_graph *f, u32 domain, struct ospf_lsa_header *h)
1697
{
1698
  return ospf_hash_get(f, domain, h->id, h->rt, h->type);
1699
}
1700

    
1701
struct top_hash_entry *
1702
ospf_hash_find(struct top_graph *f, u32 domain, u32 lsa, u32 rtr, u32 type)
1703
{
1704
  struct top_hash_entry *e;
1705
  e = f->hash_table[ospf_top_hash(f, domain, lsa, rtr, type)];
1706

    
1707
  while (e && (e->lsa.id != lsa || e->lsa.type != type || e->lsa.rt != rtr || e->domain != domain))
1708
    e = e->next;
1709

    
1710
  return e;
1711
}
1712

    
1713

    
1714
#ifdef OSPFv2
1715

    
1716
/* In OSPFv2, sometimes we don't know Router ID when looking for network LSAs.
1717
   There should be just one, so we find any match. */
1718
struct top_hash_entry *
1719
ospf_hash_find_net(struct top_graph *f, u32 domain, u32 lsa)
1720
{
1721
  struct top_hash_entry *e;
1722
  e = f->hash_table[ospf_top_hash(f, domain, lsa, 0, LSA_T_NET)];
1723

    
1724
  while (e && (e->lsa.id != lsa || e->lsa.type != LSA_T_NET || e->domain != domain))
1725
    e = e->next;
1726

    
1727
  return e;
1728
}
1729

    
1730
#endif
1731

    
1732

    
1733
#ifdef OSPFv3
1734

    
1735
/* In OSPFv3, usually we don't know LSA ID when looking for router
1736
   LSAs. We return matching LSA with smallest LSA ID. */
1737
struct top_hash_entry *
1738
ospf_hash_find_rt(struct top_graph *f, u32 domain, u32 rtr)
1739
{
1740
  struct top_hash_entry *rv = NULL;
1741
  struct top_hash_entry *e;
1742
  e = f->hash_table[ospf_top_hash(f, domain, 0, rtr, LSA_T_RT)];
1743
  
1744
  while (e)
1745
    {
1746
      if (e->lsa.rt == rtr && e->lsa.type == LSA_T_RT && e->domain == domain)
1747
        if (!rv || e->lsa.id < rv->lsa.id)
1748
          rv = e;
1749
      e = e->next;
1750
    }
1751

    
1752
  return rv;
1753
}
1754

    
1755
static inline struct top_hash_entry *
1756
find_matching_rt(struct top_hash_entry *e, u32 domain, u32 rtr)
1757
{
1758
  while (e && (e->lsa.rt != rtr || e->lsa.type != LSA_T_RT || e->domain != domain))
1759
    e = e->next;
1760
  return e;
1761
}
1762

    
1763
struct top_hash_entry *
1764
ospf_hash_find_rt_first(struct top_graph *f, u32 domain, u32 rtr)
1765
{
1766
  struct top_hash_entry *e;
1767
  e = f->hash_table[ospf_top_hash(f, domain, 0, rtr, LSA_T_RT)];
1768
  return find_matching_rt(e, domain, rtr);
1769
}
1770

    
1771
struct top_hash_entry *
1772
ospf_hash_find_rt_next(struct top_hash_entry *e)
1773
{
1774
  return find_matching_rt(e->next, e->domain, e->lsa.rt);
1775
}
1776

    
1777
#endif
1778

    
1779

    
1780
struct top_hash_entry *
1781
ospf_hash_get(struct top_graph *f, u32 domain, u32 lsa, u32 rtr, u32 type)
1782
{
1783
  struct top_hash_entry **ee;
1784
  struct top_hash_entry *e;
1785

    
1786
  ee = f->hash_table + ospf_top_hash(f, domain, lsa, rtr, type);
1787
  e = *ee;
1788

    
1789
  while (e && (e->lsa.id != lsa || e->lsa.rt != rtr || e->lsa.type != type || e->domain != domain))
1790
    e = e->next;
1791

    
1792
  if (e)
1793
    return e;
1794

    
1795
  e = sl_alloc(f->hash_slab);
1796
  e->color = OUTSPF;
1797
  e->dist = LSINFINITY;
1798
  e->nhs = NULL;
1799
  e->lb = IPA_NONE;
1800
  e->lsa.id = lsa;
1801
  e->lsa.rt = rtr;
1802
  e->lsa.type = type;
1803
  e->lsa_body = NULL;
1804
  e->domain = domain;
1805
  e->next = *ee;
1806
  *ee = e;
1807
  if (f->hash_entries++ > f->hash_entries_max)
1808
    ospf_top_rehash(f, HASH_HI_STEP);
1809
  return e;
1810
}
1811

    
1812
void
1813
ospf_hash_delete(struct top_graph *f, struct top_hash_entry *e)
1814
{
1815
  struct top_hash_entry **ee = f->hash_table + 
1816
    ospf_top_hash(f, e->domain, e->lsa.id, e->lsa.rt, e->lsa.type);
1817

    
1818
  while (*ee)
1819
  {
1820
    if (*ee == e)
1821
    {
1822
      *ee = e->next;
1823
      sl_free(f->hash_slab, e);
1824
      if (f->hash_entries-- < f->hash_entries_min)
1825
        ospf_top_rehash(f, -HASH_LO_STEP);
1826
      return;
1827
    }
1828
    ee = &((*ee)->next);
1829
  }
1830
  bug("ospf_hash_delete() called for invalid node");
1831
}
1832

    
1833
/*
1834
static void
1835
ospf_dump_lsa(struct top_hash_entry *he, struct proto *p)
1836
{
1837

1838
  struct ospf_lsa_rt *rt = NULL;
1839
  struct ospf_lsa_rt_link *rr = NULL;
1840
  struct ospf_lsa_net *ln = NULL;
1841
  u32 *rts = NULL;
1842
  u32 i, max;
1843

1844
  OSPF_TRACE(D_EVENTS, "- %1x %-1R %-1R %4u 0x%08x 0x%04x %-1R",
1845
             he->lsa.type, he->lsa.id, he->lsa.rt, he->lsa.age, he->lsa.sn,
1846
             he->lsa.checksum, he->domain);
1847

1848

1849
  switch (he->lsa.type)
1850
    {
1851
    case LSA_T_RT:
1852
      rt = he->lsa_body;
1853
      rr = (struct ospf_lsa_rt_link *) (rt + 1);
1854

1855
      for (i = 0; i < lsa_rt_items(&he->lsa); i++)
1856
        OSPF_TRACE(D_EVENTS, "  - %1x %-1R %-1R %5u",
1857
                   rr[i].type, rr[i].id, rr[i].data, rr[i].metric);
1858
      break;
1859

1860
    case LSA_T_NET:
1861
      ln = he->lsa_body;
1862
      rts = (u32 *) (ln + 1);
1863

1864
      for (i = 0; i < lsa_net_items(&he->lsa); i++)
1865
        OSPF_TRACE(D_EVENTS, "  - %-1R", rts[i]);
1866
      break;
1867

1868
    default:
1869
      break;
1870
    }
1871
}
1872

1873
void
1874
ospf_top_dump(struct top_graph *f, struct proto *p)
1875
{
1876
  unsigned int i;
1877
  OSPF_TRACE(D_EVENTS, "Hash entries: %d", f->hash_entries);
1878

1879
  for (i = 0; i < f->hash_size; i++)
1880
  {
1881
    struct top_hash_entry *e;
1882
    for (e = f->hash_table[i]; e != NULL; e = e->next)
1883
      ospf_dump_lsa(e, p);
1884
  }
1885
}
1886
*/
1887

    
1888
/* This is very inefficient, please don't call it often */
1889

    
1890
/* I should also test for every LSA if it's in some link state
1891
 * retransmission list for every neighbor. I will not test it.
1892
 * It could happen that I'll receive some strange ls ack's.
1893
 */
1894

    
1895
int
1896
can_flush_lsa(struct proto_ospf *po)
1897
{
1898
  struct ospf_iface *ifa;
1899
  struct ospf_neighbor *n;
1900

    
1901
  WALK_LIST(ifa, po->iface_list)
1902
  {
1903
    WALK_LIST(n, ifa->neigh_list)
1904
    {
1905
      if ((n->state == NEIGHBOR_EXCHANGE) || (n->state == NEIGHBOR_LOADING))
1906
        return 0;
1907

    
1908
      break;
1909
    }
1910
  }
1911

    
1912
  return 1;
1913
}