Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / rt.c @ 061ab802

History | View | Annotate | Download (29.4 KB)

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

    
9
#include "ospf.h"
10

    
11
static void add_cand(list * l, struct top_hash_entry *en, 
12
                     struct top_hash_entry *par, u32 dist,
13
                     struct ospf_area *oa);
14
static void calc_next_hop(struct ospf_area *oa,
15
                          struct top_hash_entry *en,
16
                          struct top_hash_entry *par);
17
static void ospf_ext_spf(struct proto_ospf *po);
18
static void rt_sync(struct proto_ospf *po);
19

    
20
/* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address)
21
   as index, so we need to encapsulate RID to IP addresss */
22
#ifdef OSPFv2
23
#define ipa_from_rid(x) _MI(x)
24
#else /* OSPFv3 */
25
#define ipa_from_rid(x) _MI(0,0,0,x)
26
#endif
27

    
28

    
29
static inline u32 *
30
get_ipv6_prefix(u32 *buf, ip_addr *addr, int *pxlen, u8 *pxopts, u16 *rest)
31
{
32
  u8 pxl = (*buf >> 24);
33
  *pxopts = (*buf >> 16);
34
  *rest = *buf;
35
  *pxlen = pxl;
36
  buf++;
37

    
38
  *addr = IPA_NONE;
39

    
40
  if (pxl > 0)
41
    _I0(*addr) = *buf++;
42
  if (pxl > 32)
43
    _I1(*addr) = *buf++;
44
  if (pxl > 64)
45
    _I2(*addr) = *buf++;
46
  if (pxl > 96)
47
    _I3(*addr) = *buf++;
48

    
49
  return buf;
50
}
51

    
52
static inline u32 *
53
get_ipv6_addr(u32 *buf, ip_addr *addr)
54
{
55
  *addr = *(ip_addr *) buf;
56
  return buf + 4;
57
}
58

    
59
static void
60
fill_ri(orta * orta)
61
{
62
  orta->type = RTS_DUMMY;
63
  orta->options = 0;
64
  orta->oa = NULL;
65
  orta->metric1 = LSINFINITY;
66
  orta->metric2 = LSINFINITY;
67
  orta->nh = IPA_NONE;
68
  orta->ifa = NULL;
69
  orta->ar = NULL;
70
  orta->tag = 0;
71
}
72

    
73
void
74
ospf_rt_initort(struct fib_node *fn)
75
{
76
  ort *ri = (ort *) fn;
77
  fill_ri(&ri->n);
78
  memcpy(&ri->o, &ri->n, sizeof(orta));
79
  ri->efn = NULL;
80
}
81

    
82
/* If new is better return 1 */
83

    
84
/* 
85
 * This is hard to understand:
86
 * If rfc1583 is set to 1, it work likes normal route_better()
87
 * But if it is set to 0, it prunes number of AS boundary
88
 * routes before it starts the router decision
89
 */
90
static int
91
ri_better(struct proto_ospf *po, orta * new, ort *nefn, orta * old, ort *oefn, int rfc1583)
92
{
93
  int newtype = new->type;
94
  int oldtype = old->type;
95

    
96
  if (old->type == RTS_DUMMY)
97
    return 1;
98

    
99
  if (old->metric1 == LSINFINITY)
100
    return 1;
101

    
102
  if (!rfc1583)
103
  {
104
    if ((new->type < RTS_OSPF_EXT1) && (new->oa->areaid == 0)) newtype = RTS_OSPF_IA;
105
    if ((old->type < RTS_OSPF_EXT2) && (old->oa->areaid == 0)) oldtype = RTS_OSPF_IA;
106
  }
107

    
108
  if (newtype < oldtype)
109
    return 1;
110

    
111
  if (newtype > oldtype)
112
    return 0;
113

    
114
  /* Same type */
115
  if (new->type == RTS_OSPF_EXT2)
116
  {
117
    if (new->metric2 < old->metric2) return 1;
118
    if (new->metric2 > old->metric2) return 0;
119
  }
120

    
121
  if (((new->type == RTS_OSPF_EXT2) || (new->type == RTS_OSPF_EXT1)) && (!po->rfc1583))
122
  {
123
    newtype = nefn->n.type;
124
    oldtype = oefn->n.type;
125

    
126
    if (nefn->n.oa->areaid == 0) newtype = RTS_OSPF_IA;
127
    if (oefn->n.oa->areaid == 0) oldtype = RTS_OSPF_IA;
128

    
129
    if (newtype < oldtype) return 1;
130
    if (newtype > oldtype) return 0;
131
  }
132

    
133
  if (new->metric1 < old->metric1)
134
    return 1;
135

    
136
  if (new->metric1 > old->metric1)
137
    return 0;
138

    
139
  if (new->oa->areaid > old->oa->areaid) return 1;        /* Larger AREAID is preffered */
140

    
141
  return 0;                        /* Old is shorter or same */
142
}
143

    
144
static void
145
ri_install(struct proto_ospf *po, ip_addr prefix, int pxlen, int dest,
146
           orta * new, ort * ipath)
147
{
148
  struct ospf_area *oa = new->oa;
149
  ort *old;
150

    
151
  if (dest == ORT_NET)
152
  {
153
    struct area_net *anet;
154
    old = (ort *) fib_get(&po->rtf, &prefix, pxlen);
155
    if (ri_better(po, new, ipath, &old->n, old->efn, 1))
156
    {
157
      memcpy(&old->n, new, sizeof(orta));
158
      old->efn = ipath;
159
      if ((new->type == RTS_OSPF) && (anet = (struct area_net *)fib_route(&oa->net_fib, prefix, pxlen)))
160
      {
161
        anet->active = 1;
162
        if (new->metric1 > anet->metric) anet->metric = new->metric1;
163
      }
164
    }
165
  }
166
  else
167
  {
168
    old = (ort *) fib_get(&oa->rtr, &prefix, pxlen);
169

    
170
    if (ri_better(po, new, ipath, &old->n, old->efn, 1))
171
    {
172
      memcpy(&old->n, new, sizeof(orta));
173
      old->efn = ipath;
174
    }
175
  }
176
}
177

    
178
static void
179
add_network(struct ospf_area *oa, ip_addr px, int pxlen, int metric, struct top_hash_entry *en)
180
{
181
  orta nf;
182
  nf.type = RTS_OSPF;
183
  nf.options = 0;
184
  nf.metric1 = metric;
185
  nf.metric2 = LSINFINITY;
186
  nf.tag = 0;
187
  nf.oa = oa;
188
  nf.ar = en;
189
  nf.nh = en->nh;
190
  nf.ifa = en->nhi;
191

    
192
  /* FIXME check nf.ifa on stubs */
193
  ri_install(oa->po, px, pxlen, ORT_NET, &nf, NULL);
194
}
195

    
196
#ifdef OSPFv3
197
static void
198
process_prefixes(struct ospf_area *oa)
199
{
200
  struct proto_ospf *po = oa->po;
201
  struct proto *p = &po->proto;
202
  struct top_hash_entry *en, *src;
203
  struct ospf_lsa_prefix *px;
204
  ip_addr pxa;
205
  int pxlen;
206
  u8 pxopts;
207
  u16 metric;
208
  u32 *buf;
209
  int i;
210

    
211
  WALK_SLIST(en, po->lsal)
212
  {
213
    if (en->lsa.type != LSA_T_PREFIX)
214
      continue;
215

    
216
    if (en->domain != oa->areaid)
217
      continue;
218

    
219
    if (en->lsa.age == LSA_MAXAGE)
220
      continue;
221

    
222
    px = en->lsa_body;
223
    /* FIXME: for router LSA, we should find the first one */
224
    src = ospf_hash_find(po->gr, oa->areaid,
225
                         ((px->ref_type == LSA_T_RT) ? px->ref_rt : px->ref_id),
226
                         px->ref_rt, px->ref_type);
227

    
228
    if (!src)
229
      continue;
230

    
231
    if (src->lsa.age == LSA_MAXAGE)
232
      continue;
233

    
234
    if ((src->lsa.type != LSA_T_RT) && (src->lsa.type != LSA_T_NET))
235
      continue;
236

    
237
    buf = px->rest;
238
    for (i = 0; i < px->pxcount; i++)
239
      {
240
        buf = get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
241

    
242
        if (pxopts & OPT_PX_NU)
243
          continue;
244

    
245
        add_network(oa, pxa, pxlen, src->dist + metric, src);
246
      }
247
  }
248
}
249
#endif
250

    
251
static void
252
ospf_rt_spfa(struct ospf_area *oa)
253
{
254
  u32 i, *rts;
255
  struct ospf_lsa_rt *rt;
256
  struct ospf_lsa_rt_link *rtl, *rr;
257
  struct proto *p = &oa->po->proto;
258
  struct proto_ospf *po = oa->po;
259
  struct ospf_lsa_net *ln;
260
  orta nf;
261
  struct ospf_iface *iface;
262
  struct top_hash_entry *act, *tmp;
263
  node *n;
264

    
265
  if (oa->rt == NULL)
266
    return;
267

    
268
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for area %R", oa->areaid);
269

    
270
  if (oa->rt->dist != LSINFINITY)
271
    bug("Aging was not processed.");
272

    
273
  /* 16.1. (1) */
274
  init_list(&oa->cand);                /* Empty list of candidates */
275
  oa->trcap = 0;
276

    
277
  DBG("LSA db prepared, adding me into candidate list.\n");
278

    
279
  oa->rt->dist = 0;
280
  oa->rt->color = CANDIDATE;
281
  add_head(&oa->cand, &oa->rt->cn);
282
  DBG("RT LSA: rt: %R, id: %R, type: %u\n",
283
      oa->rt->lsa.rt, oa->rt->lsa.id, oa->rt->lsa.type);
284

    
285
  while (!EMPTY_LIST(oa->cand))
286
  {
287
    n = HEAD(oa->cand);
288
    act = SKIP_BACK(struct top_hash_entry, cn, n);
289
    rem_node(n);
290

    
291
    DBG("Working on LSA: rt: %R, id: %R, type: %u\n",
292
        act->lsa.rt, act->lsa.id, act->lsa.type);
293

    
294
    act->color = INSPF;
295
    switch (act->lsa.type)
296
    {
297
    case LSA_T_RT:
298
      /* FIXME - in OSPFv3 we should process all RT LSAs from that router */
299
      rt = (struct ospf_lsa_rt *) act->lsa_body;
300
      if (rt->options & OPT_RT_V)
301
        oa->trcap = 1;
302

    
303
      /* FIXME - in OSPFv3, should we add all routers, or just ABRs an ASBRs? */
304
      if ((rt->options & OPT_RT_V) || (rt->options & OPT_RT_E))
305
        {
306
          nf.type = RTS_OSPF;
307
          nf.options = rt->options;
308
          nf.metric1 = act->dist;
309
          nf.metric2 = LSINFINITY;
310
          nf.tag = 0;
311
          nf.oa = oa;
312
          nf.ar = act;
313
          nf.nh = act->nh;
314
          nf.ifa = act->nhi;
315
          ri_install(po, ipa_from_rid(act->lsa.rt), MAX_PREFIX_LENGTH, ORT_ROUTER, &nf, NULL);
316
        }
317

    
318
      rr = (struct ospf_lsa_rt_link *) (rt + 1);
319
      DBG("  Number of links: %u\n", rt->links);
320
      for (i = 0; i < lsa_rt_count(&act->lsa); i++)
321
      {
322
        tmp = NULL;
323
        rtl = (rr + i);
324
        DBG("     Working on link: %R (type: %u)  ", rtl->id, rtl->type);
325
        switch (rtl->type)
326
        {
327
#ifdef OSPFv2
328
        case LSART_STUB:
329
          /*
330
           * This violates rfc2328! But it is mostly harmless.
331
           */
332
          DBG("\n");
333

    
334
          nf.type = RTS_OSPF;
335
          nf.options = 0;
336
          nf.metric1 = act->dist + rtl->metric;
337
          nf.metric2 = LSINFINITY;
338
          nf.tag = 0;
339
          nf.oa = oa;
340
          nf.ar = act;
341
          nf.nh = act->nh;
342
          nf.ifa = act->nhi;
343

    
344
          if (act == oa->rt)
345
          {
346
            struct ospf_iface *iff;
347

    
348
            WALK_LIST(iff, po->iface_list)        /* Try to find corresponding interface */
349
            {
350
               if (iff->iface && (iff->type != OSPF_IT_VLINK) &&
351
                 (rtl->id == (ipa_to_u32(ipa_mkmask(iff->iface->addr->pxlen))
352
                 & ipa_to_u32(iff->iface->addr->prefix))))        /* No VLINK and IP must match */
353
               {
354
                 nf.ifa = iff;
355
                 break;
356
               }
357
            }
358
          }
359

    
360
          if (!nf.ifa)
361
            continue;
362

    
363
          ri_install(po, ipa_from_u32(rtl->id),
364
                     ipa_mklen(ipa_from_u32(rtl->data)), ORT_NET, &nf, NULL);
365
          break;
366
#endif
367
        case LSART_NET:
368
#ifdef OSPFv2
369
          /* In OSPFv2, rtl->id is IP addres of DR, router ID is not known */
370
          tmp = ospf_hash_find(po->gr, oa->areaid, rtl->id, 0, LSA_T_NET);
371
#else /* OSPFv3 */
372
          tmp = ospf_hash_find(po->gr, oa->areaid, rtl->nif, rtl->id, LSA_T_NET);
373
#endif
374
          if (tmp == NULL)
375
            DBG("Not found!\n");
376
          else
377
            DBG("Found. :-)\n");
378
          break;
379

    
380
        case LSART_VLNK:
381
        case LSART_PTP:
382
          /* FIXME - in OSPFv3, find lowest LSA ID */
383
          tmp = ospf_hash_find(po->gr, oa->areaid, rtl->id, rtl->id, LSA_T_RT);
384

    
385
          DBG("PTP found.\n");
386
          break;
387
        default:
388
          log("Unknown link type in router lsa. (rid = %R)", act->lsa.id);
389
          break;
390
        }
391
        if (tmp)
392
          DBG("Going to add cand, Mydist: %u, Req: %u\n",
393
              tmp->dist, act->dist + rtl->metric);
394
        add_cand(&oa->cand, tmp, act, act->dist + rtl->metric, oa);
395
      }
396
      break;
397
    case LSA_T_NET:
398
      ln = act->lsa_body;
399

    
400
#ifdef OSPFv2
401
      add_network(oa, ipa_and(ipa_from_u32(act->lsa.id), ln->netmask),
402
                  ipa_mklen(ln->netmask), act->dist, act);
403
#endif
404

    
405
      rts = (u32 *) (ln + 1);
406
      for (i = 0; i < lsa_net_count(&act->lsa); i++)
407
      {
408
        DBG("     Working on router %R ", rts[i]);
409
        /* FIXME - in OSPFv3, find any LSA ID */
410
        tmp = ospf_hash_find(po->gr, oa->areaid, rts[i], rts[i], LSA_T_RT);
411
        if (tmp != NULL)
412
          DBG("Found :-)\n");
413
        else
414
          DBG("Not found!\n");
415
        add_cand(&oa->cand, tmp, act, act->dist, oa);
416
      }
417
      break;
418
    }
419
  }
420

    
421
#ifdef OSPFv3
422
  process_prefixes(oa);
423
#endif
424

    
425
  /* Find new/lost VLINK peers */
426
  WALK_LIST(iface, po->iface_list)
427
  {
428
    if ((iface->type == OSPF_IT_VLINK) && (iface->voa == oa))
429
    {
430
      /* FIXME in OSPFv3, different LSAID */
431
      if ((tmp = ospf_hash_find(po->gr, oa->areaid, iface->vid, iface->vid, LSA_T_RT)) &&
432
        (!ipa_equal(tmp->lb, IPA_NONE)))
433
      {
434
        if ((iface->state != OSPF_IS_PTP) || (iface->iface != tmp->nhi->iface) || (!ipa_equal(iface->vip, tmp->lb)))
435
        {
436
          OSPF_TRACE(D_EVENTS, "Vlink peer %R found", tmp->lsa.id);
437
          ospf_iface_sm(iface, ISM_DOWN);
438
          iface->iface = tmp->nhi->iface;
439
          iface->vip = tmp->lb;
440
          ospf_iface_sm(iface, ISM_UP);
441
        }
442
      }
443
      else
444
      {
445
        if (iface->state > OSPF_IS_DOWN)
446
        {
447
          OSPF_TRACE(D_EVENTS, "Vlink peer %R lost", iface->vid);
448
          ospf_iface_sm(iface, ISM_DOWN);
449
        }
450
      }
451
    }
452
  }
453
}
454

    
455
#if 0
456
static int
457
link_back(struct ospf_area *oa, struct top_hash_entry *fol, struct top_hash_entry *pre)
458
{
459
  u32 i, *rts;
460
  struct ospf_lsa_net *ln;
461
  struct ospf_lsa_rt *rt;
462
  struct ospf_lsa_rt_link *rtl, *rr;
463
  struct proto_ospf *po = oa->po;
464

465
  if (!pre) return 0;
466
  if (!fol) return 0;
467
  switch (fol->lsa.type)
468
  {
469
    case LSA_T_RT:
470
      rt = (struct ospf_lsa_rt *) fol->lsa_body;
471
      rr = (struct ospf_lsa_rt_link *) (rt + 1);
472
      for (i = 0; i < lsa_rt_count(&fol->lsa); i++)
473
      {
474
        rtl = (rr + i);
475
        switch (rtl->type)
476
        {
477
        case LSART_STUB:
478
          break;
479
        case LSART_NET:
480
          if (ospf_hash_find(po->gr, oa->areaid, rtl->id, rtl->id, LSA_T_NET) == pre)
481
          {
482
            fol->lb = ipa_from_u32(rtl->data);
483
            return 1;
484
          }
485
          break;
486
        case LSART_VLNK:
487
        case LSART_PTP:
488
          if (ospf_hash_find(po->gr, oa->areaid, rtl->id, rtl->id, LSA_T_RT) == pre)
489
          {
490
            fol->lb = ipa_from_u32(rtl->data);
491
            return 1;
492
          }
493
          break;
494
        default:
495
          log("Unknown link type in router lsa. (rid = %R)", fol->lsa.id);
496
          break;
497
        }
498
      }
499
      break;
500
    case LSA_T_NET:
501
      ln = fol->lsa_body;
502
      rts = (u32 *) (ln + 1);
503
      for (i = 0; i < lsa_net_count(&fol->lsa); i++)
504
      {
505
        if (ospf_hash_find(po->gr, oa->areaid, *(rts + i), *(rts + i), LSA_T_RT) == pre)
506
        {
507
          return 1;
508
        }
509
      }
510
      break;
511
    default:
512
      bug("Unknown lsa type. (id = %R)", fol->lsa.id);
513
  }
514
  return 0;
515
}
516
#endif
517

    
518
static void
519
ospf_rt_sum_tr(struct ospf_area *oa)
520
{
521
  struct proto *p = &oa->po->proto;
522
  struct proto_ospf *po = oa->po;
523
  struct ospf_area *bb = po->backbone;
524
  ip_addr ip, abrip;
525
  struct top_hash_entry *en;
526
  u32 dst_rid, metric, options;
527
  int pxlen = -1, type = -1;
528
  ort *re = NULL, *abr;
529
  orta nf;
530

    
531
  if (!bb) return;
532

    
533
  WALK_SLIST(en, po->lsal)
534
  {
535
    if ((en->lsa.type != LSA_T_SUM_RT) && (en->lsa.type != LSA_T_SUM_NET))
536
      continue;
537

    
538
    if (en->domain != oa->areaid)
539
      continue;
540

    
541
    if (en->lsa.age == LSA_MAXAGE)
542
      continue;
543

    
544
    if (en->dist == LSINFINITY)
545
      continue;
546

    
547
    if (en->lsa.rt == p->cf->global->router_id)
548
      continue;
549

    
550

    
551
    if (en->lsa.type == LSA_T_SUM_NET)
552
    {
553
#ifdef OSPFv2
554
      struct ospf_lsa_sum *ls = en->lsa_body;
555
      pxlen = ipa_mklen(ls->netmask);
556
      ip = ipa_and(ipa_from_u32(en->lsa.id), ls->netmask);
557
#else /* OSPFv3 */
558
      u8 pxopts;
559
      u16 rest;
560
      struct ospf_lsa_sum_net *ls = en->lsa_body;
561
      get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
562

    
563
      if (pxopts & OPT_PX_NU)
564
        continue;
565
#endif
566

    
567
      metric = ls->metric & METRIC_MASK;
568
      options = 0;
569
      type = ORT_NET;
570
      re = (ort *) fib_find(&po->rtf, &ip, pxlen);
571
    }
572
    else if (en->lsa.type == LSA_T_SUM_RT)
573
    {
574
#ifdef OSPFv2
575
      struct ospf_lsa_sum *ls = en->lsa_body;
576
      dst_rid = en->lsa.id;
577
      options = 0;
578
#else /* OSPFv3 */
579
      struct ospf_lsa_sum_rt *ls = en->lsa_body;
580
      dst_rid = ls->drid; 
581
      options = ls->options & OPTIONS_MASK;
582
#endif
583

    
584
      ip = ipa_from_rid(dst_rid);
585
      pxlen = MAX_PREFIX_LENGTH;
586
      metric = ls->metric & METRIC_MASK;
587
      options |= ORTA_ASBR;
588
      type = ORT_ROUTER;
589
      re = (ort *) fib_find(&bb->rtr, &ip, pxlen);
590
    }
591

    
592
    if (!re) continue;
593
    if (re->n.oa->areaid != 0) continue;
594
    if ((re->n.type != RTS_OSPF) && (re->n.type != RTS_OSPF_IA)) continue;
595

    
596
    abrip = ipa_from_rid(en->lsa.rt);
597

    
598
    abr = fib_find(&oa->rtr, &abrip, MAX_PREFIX_LENGTH);
599
    if (!abr) continue;
600

    
601
    nf.type = re->n.type;
602
    nf.options = options;
603
    nf.metric1 = abr->n.metric1 + metric;
604
    nf.metric2 = LSINFINITY;
605
    nf.tag = 0;
606
    nf.oa = oa;
607
    nf.ar = abr->n.ar;
608
    nf.nh = abr->n.nh;
609
    nf.ifa = abr->n.ifa;
610
    ri_install(po, ip, pxlen, type, &nf, NULL);
611
  }
612
}
613
  
614

    
615
static void
616
ospf_rt_sum(struct ospf_area *oa)
617
{
618
  struct proto_ospf *po = oa->po;
619
  struct proto *p = &po->proto;
620
  struct top_hash_entry *en;
621
  ip_addr ip, abrip;        /* abrIP is actually ID */
622
  u32 dst_rid, metric, options;
623
  struct area_net *anet;
624
  orta nf;
625
  ort *abr;
626
  int pxlen = -1, type = -1;
627

    
628
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for inter-area (area %R)", oa->areaid);
629

    
630
  WALK_SLIST(en, po->lsal)
631
  {
632
    if ((en->lsa.type != LSA_T_SUM_RT) && (en->lsa.type != LSA_T_SUM_NET))
633
      continue;
634

    
635
    if (en->domain != oa->areaid)
636
      continue;
637

    
638
    /* Page 169 (1) */
639
    if (en->lsa.age == LSA_MAXAGE)
640
      continue;
641

    
642
    /* Page 169 (2) */
643
    if (en->lsa.rt == p->cf->global->router_id)
644
      continue;
645

    
646

    
647
    if (en->lsa.type == LSA_T_SUM_NET)
648
    {
649
      struct ospf_area *oaa;
650
      int skip = 0;
651

    
652
#ifdef OSPFv2
653
      struct ospf_lsa_sum *ls = en->lsa_body;
654
      pxlen = ipa_mklen(ls->netmask);
655
      ip = ipa_and(ipa_from_u32(en->lsa.id), ls->netmask);
656
#else /* OSPFv3 */
657
      u8 pxopts;
658
      u16 rest;
659
      struct ospf_lsa_sum_net *ls = en->lsa_body;
660
      get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
661

    
662
      if (pxopts & OPT_PX_NU)
663
        continue;
664
#endif
665

    
666
      metric = ls->metric & METRIC_MASK;
667
      options = 0;
668
      type = ORT_NET;
669

    
670
      /* Page 169 (3) */
671
      WALK_LIST(oaa, po->area_list)
672
      {
673
        if ((anet = fib_find(&oaa->net_fib, &ip, pxlen)) && anet->active)
674
        {
675
          skip = 1;
676
          break;
677
        }
678
      }
679
      if (skip) continue;
680
    }
681
    else
682
    {
683
#ifdef OSPFv2
684
      struct ospf_lsa_sum *ls = en->lsa_body;
685
      dst_rid = en->lsa.id;
686
      options = 0;
687
#else /* OSPFv3 */
688
      struct ospf_lsa_sum_rt *ls = en->lsa_body;
689
      dst_rid = ls->drid; 
690
      options = ls->options & OPTIONS_MASK;
691
#endif
692

    
693
      ip = ipa_from_rid(dst_rid);
694
      pxlen = MAX_PREFIX_LENGTH;
695
      metric = ls->metric & METRIC_MASK;
696
      options |= ORTA_ASBR;
697
      type = ORT_ROUTER;
698
    }
699

    
700
    /* Page 169 (1) */
701
    if (metric == LSINFINITY)
702
      continue;
703

    
704
    /* Page 169 (4) */
705
    abrip = ipa_from_rid(en->lsa.rt);
706
    if (!(abr = (ort *) fib_find(&oa->rtr, &abrip, MAX_PREFIX_LENGTH))) continue;
707
    if (abr->n.metric1 == LSINFINITY) continue;
708
    if (!(abr->n.options & ORTA_ABR)) continue;
709

    
710
    nf.type = RTS_OSPF_IA;
711
    nf.options = options;
712
    nf.metric1 = abr->n.metric1 + metric;
713
    nf.metric2 = LSINFINITY;
714
    nf.tag = 0;
715
    nf.oa = oa;
716
    nf.ar = abr->n.ar;
717
    nf.nh = abr->n.nh;
718
    nf.ifa = abr->n.ifa;
719
    ri_install(po, ip, pxlen, type, &nf, NULL);
720
  }
721
}
722

    
723
/**
724
 * ospf_rt_spf - calculate internal routes
725
 * @po: OSPF protocol
726
 *
727
 * Calculation of internal paths in an area is described in 16.1 of RFC 2328.
728
 * It's based on Dijkstra's shortest path tree algorithms.
729
 * This function is invoked from ospf_disp().
730
 */
731
void
732
ospf_rt_spf(struct proto_ospf *po)
733
{
734
  struct proto *p = &po->proto;
735
  struct ospf_area *oa;
736
  ort *ri;
737
  struct area_net *anet;
738

    
739
  if (po->areano == 0) return;
740

    
741
  po->cleanup = 1;
742

    
743
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation");
744

    
745
  /* 16. (1) - Invalidate old routing table */
746
  FIB_WALK(&po->rtf, nftmp)
747
  {
748
    ri = (ort *) nftmp;
749
    memcpy(&ri->o, &ri->n, sizeof(orta));        /* Backup old data */
750
    fill_ri(&ri->n);
751
  }
752
  FIB_WALK_END;
753

    
754

    
755
  WALK_LIST(oa, po->area_list)
756
  {
757
    FIB_WALK(&oa->rtr, nftmp)
758
    {
759
      ri = (ort *) nftmp;
760
      memcpy(&ri->o, &ri->n, sizeof(orta));        /* Backup old data */
761
      fill_ri(&ri->n);
762
    }
763
    FIB_WALK_END;
764

    
765
    FIB_WALK(&oa->net_fib, nftmp)
766
    {
767
      anet = (struct area_net *) nftmp;
768
      anet->active = 0;
769
      anet->metric = 1;
770
    }
771
    FIB_WALK_END;
772

    
773
    /* 16. (2) */
774
    ospf_rt_spfa(oa);
775
  }
776

    
777
  /* 16. (3) */
778
  if ((po->areano == 1) || (!po->backbone))
779
  {
780
    ospf_rt_sum(HEAD(po->area_list));
781
  }
782
  else
783
  {
784
    ospf_rt_sum(po->backbone);
785
  }
786

    
787
  /* 16. (4) */
788
  WALK_LIST(oa, po->area_list)
789
  {
790
    if (oa->trcap && (oa->areaid != 0))
791
    {
792
      ospf_rt_sum_tr(oa);
793
      break;
794
    }
795
  }
796

    
797
  /* 16. (5) */
798
  ospf_ext_spf(po);
799

    
800
  rt_sync(po);
801

    
802
  po->calcrt = 0;
803
}
804

    
805
/**
806
 * ospf_ext_spf - calculate external paths
807
 * @po: protocol
808
 *
809
 * After routing table for any area is calculated, calculation of external
810
 * path is invoked. This process is described in 16.4 of RFC 2328.
811
 * Inter- and Intra-area paths are always prefered over externals.
812
 */
813
static void
814
ospf_ext_spf(struct proto_ospf *po)
815
{
816
  ort *nf1, *nf2, *nfh;
817
  orta nfa;
818
  struct top_hash_entry *en;
819
  struct proto *p = &po->proto;
820
  struct ospf_lsa_ext *le;
821
  int pxlen, ebit, rt_fwaddr_valid;
822
  ip_addr ip, nh, rtid, rt_fwaddr;
823
  struct ospf_iface *nhi = NULL;
824
  u32 br_metric, rt_metric, rt_tag;
825
  neighbor *nn;
826
  struct ospf_area *atmp;
827

    
828
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for ext routes");
829

    
830
  WALK_SLIST(en, po->lsal)
831
  {
832
    /* 16.4. (1) */
833
    if (en->lsa.type != LSA_T_EXT)
834
      continue;
835
    if (en->lsa.age == LSA_MAXAGE)
836
      continue;
837

    
838
    /* 16.4. (2) */
839
    if (en->lsa.rt == p->cf->global->router_id)
840
      continue;
841

    
842
    DBG("%s: Working on LSA. ID: %R, RT: %R, Type: %u, Mask %I\n",
843
        p->name, en->lsa.id, en->lsa.rt, en->lsa.type, le->netmask);
844

    
845
    le = en->lsa_body;
846

    
847
    rt_metric = le->metric & METRIC_MASK;
848
    ebit = le->metric & LSA_EXT_EBIT;
849

    
850
    if (rt_metric == LSINFINITY)
851
      continue;
852

    
853
#ifdef OSPFv2
854
    ip = ipa_and(ipa_from_u32(en->lsa.id), le->netmask);
855
    pxlen = ipa_mklen(le->netmask);
856
    rt_fwaddr = le->fwaddr;
857
    rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
858
    rt_tag = le->tag;
859
#else /* OSPFv3 */
860
    u8 pxopts;
861
    u16 rest;
862
    u32 *buf = le->rest;
863
    buf = get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
864

    
865
    if (pxopts & OPT_PX_NU)
866
      continue;
867

    
868
    rt_fwaddr_valid = le->metric & LSA_EXT_FBIT;
869
    if (rt_fwaddr_valid)
870
      buf = get_ipv6_addr(buf, &rt_fwaddr);
871
    else 
872
      rt_fwaddr = IPA_NONE;
873

    
874
    if (le->metric & LSA_EXT_TBIT)
875
      rt_tag = *buf++;
876
    else
877
      rt_tag = 0;
878
#endif
879

    
880
    if (pxlen < 0)
881
    {
882
      log("%s: Invalid mask in LSA. ID: %R, RT: %R, Type: %u",
883
          p->name, en->lsa.id, en->lsa.rt, en->lsa.type);
884
      continue;
885
    }
886
    nhi = NULL;
887
    nh = IPA_NONE;
888

    
889
    /* 16.4. (3) */
890
    rtid = ipa_from_rid(en->lsa.rt);
891
    nf1 = NULL;
892
    WALK_LIST(atmp, po->area_list)
893
    {
894
      nfh = fib_find(&atmp->rtr, &rtid, MAX_PREFIX_LENGTH);
895
      if (nfh == NULL) continue;
896
      if (nf1 == NULL) nf1 = nfh;
897
      else if (ri_better(po, &nfh->n, NULL, &nf1->n, NULL, po->rfc1583)) nf1 = nfh;
898
    }
899

    
900
    if (!nf1)
901
      continue;                        /* No AS boundary router found */
902

    
903
    if (nf1->n.metric1 == LSINFINITY)
904
      continue;                        /* distance is INF */
905

    
906
    if (!(nf1->n.options & ORTA_ASBR))
907
      continue;                        /* It is not ASBR */
908

    
909
    if (!rt_fwaddr_valid)
910
    {
911
      nh = nf1->n.nh;
912
      nhi = nf1->n.ifa;
913
      nfh = nf1;
914
      br_metric = nf1->n.metric1;
915
    }
916
    else
917
    {
918
      nf2 = fib_route(&po->rtf, rt_fwaddr, MAX_PREFIX_LENGTH);
919

    
920
      if (!nf2)
921
      {
922
        DBG("Cannot find network route (GW=%I)\n", rt_fwaddr);
923
        continue;
924
      }
925

    
926
      if ((nn = neigh_find(p, &rt_fwaddr, 0)) != NULL)
927
      {
928
        nh = rt_fwaddr;
929
        nhi = ospf_iface_find(po, nn->iface);
930
      }
931
      else
932
      {
933
        nh = nf2->n.nh;
934
        nhi = nf2->n.ifa;
935
      }
936

    
937
      br_metric = nf2->n.metric1;
938
      if (br_metric == LSINFINITY)
939
        continue;                        /* distance is INF */
940
    }
941

    
942
    if (ebit)
943
    {
944
      nfa.type = RTS_OSPF_EXT2;
945
      nfa.metric1 = br_metric;
946
      nfa.metric2 = rt_metric;
947
    }
948
    else
949
    {
950
      nfa.type = RTS_OSPF_EXT1;
951
      nfa.metric1 = br_metric + rt_metric;
952
      nfa.metric2 = LSINFINITY;
953
    }
954

    
955
    nfa.options = 0;
956
    nfa.tag = rt_tag;
957
    nfa.oa = (po->backbone == NULL) ? HEAD(po->area_list) : po->backbone;
958
    nfa.ar = nf1->n.ar;
959
    nfa.nh = nh;
960
    nfa.ifa = nhi;
961
    ri_install(po, ip, pxlen, ORT_NET, &nfa, nfh);
962
  }
963

    
964
}
965

    
966
/* Add LSA into list of candidates in Dijkstra's algorithm */
967
static void
968
add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par,
969
         u32 dist, struct ospf_area *oa)
970
{
971
  node *prev, *n;
972
  int added = 0;
973
  struct top_hash_entry *act;
974

    
975
  /* 16.1. (2b) */
976
  if (en == NULL)
977
    return;
978
  if (en->lsa.age == LSA_MAXAGE)
979
    return;
980

    
981
#ifdef OSPFv3
982
  if (en->lsa.type == LSA_T_RT)
983
    {
984
      struct ospf_lsa_rt *rt = en->lsa_body;
985
      if (!(rt->options & OPT_V6) || !(rt->options & OPT_R))
986
        return;
987
    }
988
#endif
989

    
990
  /* 16.1. (2c) */
991
  if (en->color == INSPF)
992
    return;
993

    
994
  /* 16.1. (2d) */
995
  if (dist >= en->dist)
996
    return;
997
  /*
998
   * FIXME The line above (=) is not a bug, but we don't support multiple
999
   * next hops. I'll start as soon as nest will
1000
   */
1001

    
1002
  /* FIXME - fix link_back()
1003
  if (!link_back(oa, en, par))
1004
    return;
1005
  */
1006

    
1007
  DBG("     Adding candidate: rt: %R, id: %R, type: %u\n",
1008
      en->lsa.rt, en->lsa.id, en->lsa.type);
1009

    
1010
  calc_next_hop(oa, en, par);
1011

    
1012
  if (!en->nhi)
1013
    return;                        /* We cannot find next hop, ignore it */
1014

    
1015
  if (en->color == CANDIDATE)
1016
  {                                /* We found a shorter path */
1017
    rem_node(&en->cn);
1018
  }
1019
  en->dist = dist;
1020
  en->color = CANDIDATE;
1021

    
1022
  prev = NULL;
1023

    
1024
  if (EMPTY_LIST(*l))
1025
  {
1026
    add_head(l, &en->cn);
1027
  }
1028
  else
1029
  {
1030
    WALK_LIST(n, *l)
1031
    {
1032
      act = SKIP_BACK(struct top_hash_entry, cn, n);
1033
      if ((act->dist > dist) ||
1034
          ((act->dist == dist) && (act->lsa.type == LSA_T_NET)))
1035
      /* FIXME - shouldn't be here LSA_T_RT ??? */
1036
      {
1037
        if (prev == NULL)
1038
          add_head(l, &en->cn);
1039
        else
1040
          insert_node(&en->cn, prev);
1041
        added = 1;
1042
        break;
1043
      }
1044
      prev = n;
1045
    }
1046

    
1047
    if (!added)
1048
    {
1049
      add_tail(l, &en->cn);
1050
    }
1051
  }
1052
}
1053

    
1054

    
1055
static inline int
1056
match_dr(struct ospf_iface *ifa, struct top_hash_entry *en)
1057
{
1058
#ifdef OSPFv2
1059
  return (ifa->drid == en->lsa.rt) && (ifa->drip == ipa_from_u32(en->lsa.id));
1060
#else /* OSPFv3 */
1061
  return (ifa->drid == en->lsa.rt) && (ifa->dr_iface_id == en->lsa.id);
1062
#endif
1063
}
1064

    
1065
static void
1066
calc_next_hop(struct ospf_area *oa, struct top_hash_entry *en,
1067
              struct top_hash_entry *par)
1068
{
1069
  struct ospf_neighbor *neigh;
1070
  struct proto *p = &oa->po->proto;
1071
  struct proto_ospf *po = oa->po;
1072
  struct ospf_iface *ifa;
1073

    
1074
  en->nhi = NULL;
1075
  en->nh = IPA_NONE;
1076

    
1077
  /* 16.1.1. The next hop calculation */
1078
  DBG("     Next hop called.\n");
1079
  if (ipa_equal(par->nh, IPA_NONE))
1080
  {
1081
    DBG("     Next hop calculating for id: %R rt: %R type: %u\n",
1082
        en->lsa.id, en->lsa.rt, en->lsa.type);
1083

    
1084
    /* The parent vertex is the root */
1085
    if (par == oa->rt)
1086
    {
1087
      if (en->lsa.type == LSA_T_NET)
1088
      {
1089
        WALK_LIST(ifa, po->iface_list)
1090
          if (match_dr(ifa, en))
1091
          {
1092
              en->nhi = ifa;
1093
              return;
1094
          }
1095
        return;
1096
      }
1097
      else
1098
      {
1099
        if ((neigh = find_neigh_noifa(po, en->lsa.rt)) == NULL)
1100
          return;
1101
        en->nhi = neigh->ifa;
1102
        en->nh = neigh->ip;
1103
        /* Yes, neighbor is it's own next hop */
1104
        return;
1105
      }
1106
    }
1107

    
1108
    /* The parent vertex is a network that directly connects the
1109
       calculating router to the destination router. */
1110
    if (par->lsa.type == LSA_T_NET)
1111
    {
1112
      if (en->lsa.type == LSA_T_NET)
1113
        bug("Parent for net is net?");
1114
      if ((en->nhi = par->nhi) == NULL)
1115
        bug("Did not find next hop interface for INSPF lsa!");
1116

    
1117
      if ((neigh = find_neigh_noifa(po, en->lsa.rt)) == NULL)
1118
        return;
1119
      en->nhi = neigh->ifa;
1120
      en->nh = neigh->ip;
1121
      /* Yes, neighbor is it's own next hop */
1122
      return;
1123
    }
1124
    else
1125
    {                                /* Parent is some RT neighbor */
1126
      log(L_ERR "Router's parent has no next hop. (EN=%R, PAR=%R)",
1127
          en->lsa.id, par->lsa.id);
1128
      /* I hope this would never happen */
1129
      return;
1130
    }
1131
  }
1132
  en->nhi = par->nhi;
1133
  en->nh = par->nh;
1134
  DBG("     Next hop calculated: %I.\n", en->nh);
1135
}
1136

    
1137
static void
1138
rt_sync(struct proto_ospf *po)
1139
{
1140
  struct proto *p = &po->proto;
1141
  struct fib_iterator fit;
1142
  struct fib *fib = &po->rtf;
1143
  ort *nf;
1144
  struct ospf_area *oa, *oaa;
1145
  struct area_net *anet;
1146
  int flush;
1147

    
1148
  OSPF_TRACE(D_EVENTS, "Starting routing table synchronisation");
1149

    
1150
  DBG("Now syncing my rt table with nest's\n");
1151
  FIB_ITERATE_INIT(&fit, fib);
1152
again1:
1153
  FIB_ITERATE_START(fib, &fit, nftmp)
1154
  {
1155
    nf = (ort *) nftmp;
1156
    check_sum_lsa(po, nf, ORT_NET);
1157
    if (memcmp(&nf->n, &nf->o, sizeof(orta)))
1158
    {                                /* Some difference */
1159
      net *ne;
1160
      rta a0;
1161
      rte *e;
1162

    
1163
      bzero(&a0, sizeof(a0));
1164

    
1165
      a0.proto = p;
1166
      a0.source = nf->n.type;
1167
      a0.scope = SCOPE_UNIVERSE;
1168
      a0.cast = RTC_UNICAST;
1169
      a0.dest = RTD_ROUTER;
1170
      a0.flags = 0;
1171
      a0.aflags = 0;
1172
      a0.iface = NULL;
1173
      if (nf->n.ifa) a0.iface = nf->n.ifa->iface;
1174
      a0.gw = nf->n.nh;
1175

    
1176
      if ((!ipa_equal(nf->n.nh, IPA_NONE)) && (!neigh_find(p, &nf->n.nh, 0)))
1177
      {
1178
        int found = 0;
1179
        struct ospf_iface *ifa;
1180
        struct top_hash_entry *en;
1181
        OSPF_TRACE(D_EVENTS, "Trying to find correct next hop %I/%d via %I", nf->fn.prefix, nf->fn.pxlen, nf->n.nh);
1182
        WALK_LIST(ifa, po->iface_list)
1183
        {
1184
          if ((ifa->type == OSPF_IT_VLINK) && ipa_equal(ifa->vip, nf->n.nh))
1185
          {
1186
            /* FIXME in OSPFv3, may be different LSA ID */
1187
            if ((en = ospf_hash_find(po->gr, ifa->voa->areaid, ifa->vid, ifa->vid, LSA_T_RT))
1188
              && (!ipa_equal(en->nh, IPA_NONE)))
1189
            {
1190
              a0.gw = en->nh;
1191
              found = 1;
1192
            }
1193
            break;
1194
          }
1195
        }
1196
        if (!found) nf->n.metric1 = LSINFINITY; /* Delete it */
1197
      }
1198

    
1199
      if (ipa_equal(nf->n.nh, IPA_NONE)) a0.dest = RTD_DEVICE;
1200

    
1201
      if (!a0.iface)        /* Still no match? Can this really happen? */
1202
        nf->n.metric1 = LSINFINITY;
1203

    
1204
      ne = net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
1205
      if (nf->n.metric1 < LSINFINITY)
1206
      {
1207
        e = rte_get_temp(&a0);
1208
        e->u.ospf.metric1 = nf->n.metric1;
1209
        e->u.ospf.metric2 = nf->n.metric2;
1210
        e->u.ospf.tag = nf->n.tag;
1211
        e->pflags = 0;
1212
        e->net = ne;
1213
        e->pref = p->preference;
1214
        DBG("Mod rte type %d - %I/%d via %I on iface %s, met %d\n",
1215
          a0.source, nf->fn.prefix, nf->fn.pxlen, a0.gw, a0.iface ? a0.iface->name : "(none)", nf->n.metric1);
1216
        rte_update(p->table, ne, p, p, e);
1217
      }
1218
      else
1219
      {
1220
        rte_update(p->table, ne, p, p, NULL);
1221
        FIB_ITERATE_PUT(&fit, nftmp);
1222
        fib_delete(fib, nftmp);
1223
        goto again1;
1224
      }
1225
    }
1226
  }
1227
  FIB_ITERATE_END(nftmp);
1228

    
1229
  WALK_LIST(oa, po->area_list)
1230
  {
1231
    FIB_ITERATE_INIT(&fit, &oa->rtr);
1232
again2:
1233
    FIB_ITERATE_START(&oa->rtr, &fit, nftmp)
1234
    {
1235
      nf = (ort *) nftmp;
1236
      if (memcmp(&nf->n, &nf->o, sizeof(orta)))
1237
      {                                /* Some difference */
1238
        check_sum_lsa(po, nf, ORT_ROUTER);
1239
        if (nf->n.metric1 >= LSINFINITY)
1240
        {
1241
          FIB_ITERATE_PUT(&fit, nftmp);
1242
          fib_delete(&oa->rtr, nftmp);
1243
          goto again2;
1244
        }
1245
      }
1246
    }
1247
    FIB_ITERATE_END(nftmp);
1248

    
1249
    /* Check condensed summary LSAs */
1250
    FIB_WALK(&oa->net_fib, nftmp)
1251
    {
1252
      flush = 1;
1253
      anet = (struct area_net *) nftmp;
1254
      if ((!anet->hidden) && anet->active)
1255
        flush = 0;
1256
          
1257
      WALK_LIST(oaa, po->area_list)
1258
      {
1259
        int fl = flush;
1260

    
1261
        if (oaa == oa) continue;
1262

    
1263
        if ((oa == po->backbone) && oaa->trcap) fl = 1;
1264

    
1265
        if (oaa->stub) fl = 1;
1266

    
1267
        if (fl) flush_sum_lsa(oaa, &anet->fn, ORT_NET);
1268
        else originate_sum_lsa(oaa, &anet->fn, ORT_NET, anet->metric, 0);
1269
      }
1270
    }
1271
    FIB_WALK_END;
1272

    
1273
    /* Check default summary LSA for stub areas
1274
     * just for router connected to backbone */
1275
    if (po->backbone)
1276
    {
1277
      struct fib_node fnn;
1278

    
1279
      fnn.prefix = IPA_NONE;
1280
      fnn.pxlen = 0;
1281
      if (oa->stub) originate_sum_lsa(oa, &fnn, ORT_NET, oa->stub, 0);
1282
      else flush_sum_lsa(oa, &fnn, ORT_NET);
1283
    }
1284
  }
1285
}