Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / rt.c @ 1a61882d

History | View | Annotate | Download (14.8 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
static void
11
add_cand(list * l, struct top_hash_entry *en,
12
         struct top_hash_entry *par, u16 dist, struct ospf_area *oa);
13
static void
14
calc_next_hop(struct top_hash_entry *en,
15
              struct top_hash_entry *par, struct ospf_area *oa);
16
static void ospf_ext_spfa(struct ospf_area *oa);
17
static void rt_sync(struct proto_ospf *po);
18

    
19
static void
20
fill_ri(orta * orta)
21
{
22
  orta->type = RTS_DUMMY;
23
  orta->capa = 0;
24
#define ORTA_ASBR 1
25
#define ORTA_ABR 2
26
  orta->oa = NULL;
27
  orta->metric1 = LSINFINITY;
28
  orta->metric2 = LSINFINITY;
29
  orta->nh = ipa_from_u32(0);
30
  orta->ifa = NULL;
31
  orta->ar = NULL;
32
  orta->tag = 0;
33
}
34

    
35
void
36
ospf_rt_initort(struct fib_node *fn)
37
{
38
  ort *ri = (ort *) fn;
39
  fill_ri(&ri->n);
40
  ri->dest = ORT_UNDEF;
41
  memcpy(&ri->o, &ri->n, sizeof(orta));
42
}
43

    
44
/* If new is better return 1 */
45
static int
46
ri_better(struct proto_ospf *po, orta * new, orta * old)
47
{
48
  int newtype = new->type;
49
  int oldtype = old->type;
50

    
51
  if (old->type == RTS_DUMMY)
52
    return 1;
53

    
54
  if (old->metric1 == LSINFINITY)
55
    return 1;
56

    
57
  if (po->rfc1583)
58
  {
59
    if ((newtype == RTS_OSPF) && (new->oa->areaid == 0)) newtype = RTS_OSPF_IA;
60
    if ((oldtype == RTS_OSPF) && (old->oa->areaid == 0)) oldtype = RTS_OSPF_IA;
61
  }
62
  
63
  if (new->type < old->type)
64
    return 1;
65

    
66
  if (new->metric2 < old->metric2)
67
  {
68
    if (old->metric2 == LSINFINITY)
69
      return 0;                        /* Old is E1, new is E2 */
70

    
71
    return 1;                        /* Both are E2 */
72
  }
73
  if (new->metric2 > old->metric2)
74
  {
75
    if (new->metric2 == LSINFINITY)
76
      return 1;                        /* New is E1, old is E2 */
77

    
78
    return 0;                        /* Both are E2 */
79
  }
80

    
81
  /*
82
   * E2 metrics are the same. It means that: 1) Paths are E2 with same
83
   * metric 2) Paths are E1.
84
   */
85
  if (new->metric1 < old->metric1)
86
    return 1;
87

    
88
  if (new->metric1 > old->metric1)
89
    return 0;
90

    
91
  /* Metric 1 are the same */
92
  if (new->oa->areaid > old->oa->areaid) return 1;        /* Larger AREAID is preffered */
93

    
94
  return 0;                        /* Old is shorter or same */
95
}
96

    
97
static void
98
ri_install(struct proto_ospf *po, ip_addr prefix, int pxlen, int dest,
99
           orta * new)
100
{
101
  ort *old = (ort *) fib_get(&po->rtf[dest], &prefix, pxlen);
102

    
103
  if (ri_better(po, new, &old->n))
104
  {
105
    memcpy(&old->n, new, sizeof(orta));
106
  }
107
}
108

    
109
/**
110
 * ospf_rt_spf - calculate internal routes
111
 * @po: OSPF protocol
112
 *
113
 * Calculation of internal paths in an area is described in 16.1 of RFC 2328.
114
 * It's based on Dijkstra's shortest path tree algorithms.
115
 * RFC recommends to add ASBR routers into routing table. I don't do this
116
 * and latter parts of routing table calculation look directly into LSA
117
 * Database. This function is invoked from ospf_disp().
118
 */
119
static void
120
ospf_rt_spfa(struct ospf_area *oa)
121
{
122
  u32 i, *rts;
123
  struct ospf_lsa_rt *rt;
124
  struct ospf_lsa_rt_link *rtl, *rr;
125
  struct proto *p = &oa->po->proto;
126
  struct proto_ospf *po = oa->po;
127
  struct ospf_lsa_net *ln;
128
  orta nf;
129

    
130
  if (oa->rt == NULL)
131
    return;
132

    
133
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for area %I",
134
             oa->areaid);
135

    
136
  if (oa->rt->dist != LSINFINITY)
137
    ospf_age(oa);
138

    
139
  init_list(&oa->cand);                /* Empty list of candidates */
140
  oa->trcap = 0;
141

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

    
144
  oa->rt->dist = 0;
145
  oa->rt->color = CANDIDATE;
146
  add_head(&oa->cand, &oa->rt->cn);
147
  DBG("RT LSA: rt: %I, id: %I, type: %u\n", oa->rt->lsa.rt,
148
      oa->rt->lsa.id, oa->rt->lsa.type);
149

    
150
  while (!EMPTY_LIST(oa->cand))
151
  {
152
    struct top_hash_entry *act, *tmp;
153
    node *n;
154

    
155
    n = HEAD(oa->cand);
156
    act = SKIP_BACK(struct top_hash_entry, cn, n);
157
    rem_node(n);
158

    
159
    DBG("Working on LSA: rt: %I, id: %I, type: %u\n", act->lsa.rt,
160
        act->lsa.id, act->lsa.type);
161

    
162
    act->color = INSPF;
163
    switch (act->lsa.type)
164
    {
165
    case LSA_T_RT:
166
      rt = (struct ospf_lsa_rt *) act->lsa_body;
167
      if (rt->veb.bit.v)
168
        oa->trcap = 1;
169
      if (rt->veb.bit.b || rt->veb.bit.e)
170
      {
171
        nf.type = RTS_OSPF;
172
        nf.capa = 0;
173
        if (rt->veb.bit.b) nf.capa |= ORTA_ABR;
174
        if (rt->veb.bit.e) nf.capa |= ORTA_ASBR;
175
        nf.metric1 = act->dist;
176
        nf.metric2 = LSINFINITY;
177
        nf.oa = oa;
178
        nf.ar = act;
179
        nf.nh = act->nh;
180
        nf.ifa = act->nhi;
181
        ri_install(po, ipa_from_u32(act->lsa.id), 32, ORT_ROUTER, &nf);
182
      }
183
      rr = (struct ospf_lsa_rt_link *) (rt + 1);
184
      DBG("  Number of links: %u\n", rt->links);
185
      for (i = 0; i < rt->links; i++)
186
      {
187
        tmp = NULL;
188
        rtl = (rr + i);
189
        DBG("     Working on link: %I (type: %u)  ", rtl->id, rtl->type);
190
        switch (rtl->type)
191
        {
192
        case LSART_STUB:
193
          /*
194
           * This violates rfc2328! but I hope
195
           * it's also correct.
196
           */
197
          DBG("\n");
198
          if (act == oa->rt)
199
            continue;
200
          if (!act->nhi)
201
            continue;
202
          nf.type = RTS_OSPF;
203
          nf.capa = 0;
204
          nf.metric1 = act->dist + rtl->metric;
205
          nf.metric2 = LSINFINITY;
206
          nf.oa = oa;
207
          nf.ar = act;
208
          nf.nh = act->nh;
209
          nf.ifa = act->nhi;
210
          ri_install(po, ipa_from_u32(rtl->id),
211
                     ipa_mklen(ipa_from_u32(rtl->data)), ORT_NET, &nf);
212
          break;
213

    
214
        case LSART_VLNK:        /* FIXME !!!!!!!! */
215
          DBG("Ignoring\n");
216
          continue;
217
          break;
218
        case LSART_NET:
219
          tmp = ospf_hash_find(oa->gr, rtl->id, rtl->id, LSA_T_NET);
220
          if (tmp == NULL)
221
            DBG("Not found!\n");
222
          else
223
            DBG("Found. :-)\n");
224
          break;
225
        case LSART_PTP:
226
          tmp = ospf_hash_find(oa->gr, rtl->id, rtl->id, LSA_T_RT);
227
          DBG("PTP found.\n");
228
          break;
229
        default:
230
          log("Unknown link type in router lsa.");
231
          break;
232
        }
233
        if (tmp)
234
          DBG("Going to add cand, Mydist: %u, Req: %u\n",
235
              tmp->dist, act->dist + rtl->metric);
236
        add_cand(&oa->cand, tmp, act, act->dist + rtl->metric, oa);
237
      }
238
      break;
239
    case LSA_T_NET:
240
      ln = act->lsa_body;
241
      nf.type = RTS_OSPF;
242
      nf.capa = 0;
243
      nf.metric1 = act->dist;
244
      nf.metric2 = LSINFINITY;
245
      nf.oa = oa;
246
      nf.ar = act;
247
      nf.nh = act->nh;
248
      nf.ifa = act->nhi;
249
      ri_install(po, ipa_and(ipa_from_u32(act->lsa.id), ln->netmask),
250
                 ipa_mklen(ln->netmask), ORT_NET, &nf);
251

    
252
      rts = (u32 *) (ln + 1);
253
      for (i = 0; i < (act->lsa.length - sizeof(struct ospf_lsa_header) -
254
                       sizeof(struct ospf_lsa_net)) / sizeof(u32); i++)
255
      {
256
        DBG("     Working on router %I ", *(rts + i));
257
        tmp = ospf_hash_find(oa->gr, *(rts + i), *(rts + i), LSA_T_RT);
258
        if (tmp != NULL)
259
          DBG("Found :-)\n");
260
        else
261
          DBG("Not found!\n");
262
        add_cand(&oa->cand, tmp, act, act->dist, oa);
263
      }
264
      break;
265
    }
266
  }
267
}
268

    
269

    
270
void
271
ospf_rt_spf(struct proto_ospf *po)
272
{
273
  struct proto *p = &po->proto;
274
  struct ospf_area *oa;
275
  int i;
276
  ort *ri;
277

    
278
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation");
279

    
280
  /* Invalidate old routing table */
281
  for (i = 0; i < 2; i++)
282
    FIB_WALK(&po->rtf[i], nftmp)
283
  {
284
    ri = (ort *) nftmp;
285
    memcpy(&ri->o, &ri->n, sizeof(orta));        /* Backup old data */
286
    fill_ri(&ri->n);
287
  }
288
  FIB_WALK_END;
289

    
290

    
291
  WALK_LIST(oa, po->area_list)
292
  {
293
    ospf_rt_spfa(oa);
294
  }
295

    
296
  if (po->areano > 1)
297
  {
298
    //ospf_rt_sum(oa);
299
  }
300
  else
301
  {
302
    WALK_LIST(oa, po->area_list)
303
    {
304
      //if (oa->id == 0) ospf_rt_sum(oa);
305
    }
306

    
307
    WALK_LIST(oa, po->area_list)
308
    {
309
      //if (oa->trcap == 1) ospf_rt_sum(oa);
310
    }
311
  }
312
  WALK_LIST(oa, po->area_list)
313
  {
314
    if (!oa->stub)
315
    {
316
      ospf_ext_spfa(oa);
317
      break;
318
    }
319
  }
320
  rt_sync(po);
321
}
322

    
323

    
324
/**
325
 * ospf_ext_spfa - calculate external paths
326
 * @po: protocol
327
 *
328
 * After routing table for any area is calculated, calculation of external
329
 * path is invoked. This process is described in 16.6 of RFC 2328.
330
 * Inter- and Intra-area paths are always prefered over externals.
331
 */
332
static void
333
ospf_ext_spfa(struct ospf_area *oa)
334
{
335
  struct proto_ospf *po = oa->po;
336
  ort *nf1, *nf2;
337
  orta nfa;
338
  struct top_hash_entry *en;
339
  struct proto *p = &po->proto;
340
  struct ospf_lsa_ext *le;
341
  struct ospf_lsa_ext_tos *lt;
342
  int mlen;
343
  ip_addr ip, nh, rtid;
344
  struct iface *nhi = NULL;
345
  int met1, met2;
346
  neighbor *nn;
347
  struct ospf_lsa_rt *rt;
348

    
349

    
350
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for ext routes");
351

    
352
  WALK_SLIST(en, oa->lsal)
353
  {
354
    if (en->lsa.type != LSA_T_EXT)
355
      continue;
356
    if (en->lsa.age == LSA_MAXAGE)
357
      continue;
358
    if (en->lsa.rt == p->cf->global->router_id)
359
      continue;
360

    
361
    le = en->lsa_body;
362
    lt = (struct ospf_lsa_ext_tos *) (le + 1);
363

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

    
367
    if (lt->metric == LSINFINITY)
368
      continue;
369
    ip = ipa_and(ipa_from_u32(en->lsa.id), le->netmask);
370
    mlen = ipa_mklen(le->netmask);
371
    if ((mlen < 0) || (mlen > 32))
372
    {
373
      log("%s: Invalid mask in LSA. ID: %I, RT: %I, Type: %u, Mask %I",
374
          p->name, en->lsa.id, en->lsa.rt, en->lsa.type, le->netmask);
375
      continue;
376
    }
377
    nhi = NULL;
378
    nh = IPA_NONE;
379

    
380
    met1 = LSINFINITY;
381
    met2 = LSINFINITY;
382

    
383
    rtid = ipa_from_u32(en->lsa.rt);
384

    
385
    if (!(nf1 = fib_find(&po->rtf[ORT_ROUTER], &rtid, 32)))
386
      continue;                        /* No AS boundary router found */
387

    
388
    if (nf1->n.metric1 == LSINFINITY)
389
      continue;                        /* distance is INF */
390

    
391
    if (!(nf1->n.capa & ORTA_ASBR))
392
      continue;                        /* It is not ASBR */
393

    
394
    if (ipa_compare(lt->fwaddr, ipa_from_u32(0)) == 0)
395
    {
396
      if (lt->etos > 0)
397
      {                                /* FW address == 0 */
398
        met1 = nf1->n.metric1;
399
        met2 = lt->metric;
400
      }
401
      else
402
      {
403
        met1 = nf1->n.metric1 + lt->metric;
404
        met2 = LSINFINITY;
405
      }
406
      nh = nf1->n.nh;
407
      nhi = nf1->n.ifa;
408
    }
409
    else
410
    {                                /* FW address !=0 */
411
      if (!(nf2 = fib_route(&po->rtf[ORT_NET], lt->fwaddr, 32)))
412
      {
413
        DBG("Cannot find network route (GW=%I)\n", lt->fwaddr);
414
        continue;
415
      }
416
      if (lt->etos > 0)
417
      {
418
        met1 = nf2->n.metric1;
419
        met2 = lt->metric;
420
      }
421
      else
422
      {
423
        met1 = nf2->n.metric1 + lt->metric;
424
        met2 = LSINFINITY;
425
      }
426

    
427
      if ((nn = neigh_find(p, &lt->fwaddr, 0)) != NULL)
428
      {
429
        nh = lt->fwaddr;
430
        nhi = nn->iface;
431
      }
432
      else
433
      {
434
        nh = nf2->n.nh;
435
        nhi = nf2->n.ifa;
436
      }
437
    }
438

    
439
    nfa.type = RTS_OSPF_EXT;
440
    nfa.capa = 0;
441
    nfa.metric1 = met1;
442
    nfa.metric2 = met2;
443
    nfa.oa = oa;
444
    nfa.ar = nf1->n.ar;
445
    nfa.nh = nh;
446
    nfa.ifa = nhi;
447
    nfa.tag = lt->tag;
448
    ri_install(po, ip, mlen, ORT_NET, &nfa);
449
  }
450

    
451
}
452

    
453
/* Add LSA into list of candidates in Dijkstra's algorithm */
454
static void
455
add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par,
456
         u16 dist, struct ospf_area *oa)
457
{
458
  node *prev, *n;
459
  int added = 0;
460
  struct top_hash_entry *act;
461

    
462
  if (en == NULL)
463
    return;
464
  if (en->lsa.age == LSA_MAXAGE)
465
    return;
466
  /* FIXME Does it have link back? Test it! */
467
  if (en->color == INSPF)
468
    return;
469

    
470
  if (dist >= en->dist)
471
    return;
472
  /*
473
   * FIXME The line above is not a bug, but we don't support multiple
474
   * next hops. I'll start as soon as nest will
475
   */
476
  DBG("     Adding candidate: rt: %I, id: %I, type: %u\n", en->lsa.rt,
477
      en->lsa.id, en->lsa.type);
478

    
479
  en->nhi = NULL;
480
  en->nh = IPA_NONE;
481

    
482
  calc_next_hop(en, par, oa);
483

    
484
  if (!en->nhi)
485
    return;                        /* We cannot find next hop, ignore it */
486

    
487
  if (en->color == CANDIDATE)
488
  {                                /* We found a shorter path */
489
    rem_node(&en->cn);
490
  }
491
  en->dist = dist;
492
  en->color = CANDIDATE;
493

    
494
  prev = NULL;
495

    
496
  if (EMPTY_LIST(*l))
497
  {
498
    add_head(l, &en->cn);
499
  }
500
  else
501
  {
502
    WALK_LIST(n, *l)
503
    {
504
      act = SKIP_BACK(struct top_hash_entry, cn, n);
505
      if ((act->dist > dist) ||
506
          ((act->dist == dist) && (act->lsa.type == LSA_T_NET)))
507
      {
508
        if (prev == NULL)
509
          add_head(l, &en->cn);
510
        else
511
          insert_node(&en->cn, prev);
512
        added = 1;
513
        break;
514
      }
515
      prev = n;
516
    }
517

    
518
    if (!added)
519
    {
520
      add_tail(l, &en->cn);
521
    }
522
  }
523
  /* FIXME Some VLINK stuff should be here */
524
}
525

    
526
static void
527
calc_next_hop(struct top_hash_entry *en, struct top_hash_entry *par,
528
              struct ospf_area *oa)
529
{
530
  struct ospf_neighbor *neigh;
531
  struct proto *p = &oa->po->proto;
532
  struct proto_ospf *po = oa->po;
533
  struct ospf_iface *ifa;
534
  u32 myrid = p->cf->global->router_id;
535

    
536
  DBG("     Next hop called.\n");
537
  if (ipa_equal(par->nh, IPA_NONE))
538
  {
539
    neighbor *nn;
540
    DBG("     Next hop calculating for id: %I rt: %I type: %u\n",
541
        en->lsa.id, en->lsa.rt, en->lsa.type);
542

    
543
    if (par == oa->rt)
544
    {
545
      if (en->lsa.type == LSA_T_NET)
546
      {
547
        if (en->lsa.rt == myrid)
548
        {
549
          WALK_LIST(ifa, po->iface_list)
550
            if (ipa_compare
551
                (ifa->iface->addr->ip, ipa_from_u32(en->lsa.id)) == 0)
552
          {
553
            en->nhi = ifa->iface;
554
            return;
555
          }
556
          log(L_ERR "I didn't find interface for my self originated LSA!\n");
557
          /* This could sometimes happen */
558
          return;
559
        }
560
        else
561
        {
562
          ip_addr ip = ipa_from_u32(en->lsa.id);
563
          nn = neigh_find(p, &ip, 0);
564
          if (nn)
565
            en->nhi = nn->iface;
566
          return;
567
        }
568
      }
569
      else
570
      {
571
        if ((neigh = find_neigh_noifa(po, en->lsa.rt)) == NULL)
572
          return;
573
        en->nhi = neigh->ifa->iface;
574
        en->nh = neigh->ip;        /* Yes, neighbor is it's
575
                                 * own next hop */
576
        return;
577
      }
578
    }
579
    if (par->lsa.type == LSA_T_NET)
580
    {
581
      if (en->lsa.type == LSA_T_NET)
582
        bug("Parent for net is net?");
583
      if ((en->nhi = par->nhi) == NULL)
584
        bug("Did not find next hop interface for INSPF lsa!");
585
      if ((neigh = find_neigh_noifa(po, en->lsa.rt)) == NULL)
586
        return;
587
      en->nhi = neigh->ifa->iface;
588
      en->nh = neigh->ip;        /* Yes, neighbor is it's own
589
                                 * next hop */
590
      return;
591
    }
592
    else
593
    {                                /* Parent is some RT neighbor */
594
      log(L_ERR "Router's parent has no next hop. (EN=%I, PAR=%I)",
595
          en->lsa.id, par->lsa.id);
596
      /* I hoped this would never happen */
597
      return;
598
    }
599
  }
600
  en->nh = par->nh;
601
  en->nhi = par->nhi;
602
  DBG("     Next hop calculated: %I.\n", en->nh);
603
}
604

    
605
static void
606
rt_sync(struct proto_ospf *po)
607
{
608
  struct proto *p = &po->proto;
609
  struct fib_iterator fit;
610
  struct fib *fib = &po->rtf[ORT_NET];
611
  ort *nf;
612

    
613
  DBG("Now syncing my rt table with nest's\n");
614
  FIB_ITERATE_INIT(&fit, fib);
615
again:
616
  FIB_ITERATE_START(fib, &fit, nftmp)
617
  {
618
    nf = (ort *) nftmp;
619
    if (nf->n.metric1 == LSINFINITY)
620
    {
621
      net *ne;
622

    
623
      ne = net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
624
      DBG("Deleting rt entry %I\n     (IP: %I, GW: %I)\n",
625
          nf->fn.prefix, ip, nf->nh);
626
      rte_update(p->table, ne, p, NULL);
627

    
628
      /* Now delete my fib */
629
      FIB_ITERATE_PUT(&fit, nftmp);
630
      fib_delete(fib, nftmp);
631
      goto again;
632
    }
633
    else if (memcmp(&nf->n, &nf->o, sizeof(orta)))
634
    {                                /* Some difference */
635
      net *ne;
636
      rta a0;
637
      rte *e;
638

    
639
      bzero(&a0, sizeof(a0));
640

    
641
      a0.proto = p;
642
      a0.source = nf->n.type;
643
      a0.scope = SCOPE_UNIVERSE;
644
      a0.cast = RTC_UNICAST;
645
      a0.dest = RTD_ROUTER;
646
      a0.flags = 0;
647
      a0.aflags = 0;
648
      a0.iface = nf->n.ifa;
649
      a0.gw = nf->n.nh;
650
      ne = net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
651
      e = rte_get_temp(&a0);
652
      e->u.ospf.metric1 = nf->n.metric1;
653
      e->u.ospf.metric2 = nf->n.metric2;
654
      e->u.ospf.tag = nf->n.tag;
655
      e->pflags = 0;
656
      e->net = ne;
657
      e->pref = p->preference;
658
      DBG("Modifying rt entry %I\n     (IP: %I, GW: %I)\n",
659
          nf->fn.prefix, ip, nf->nh);
660
      rte_update(p->table, ne, p, e);
661
    }
662
  }
663
  FIB_ITERATE_END(nftmp);
664
}