Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / rt.c @ 1186369b

History | View | Annotate | Download (14.6 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 2000 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
void
12
init_infib(struct fib_node *fn)
13
{
14
  struct infib *f=(struct infib *)fn;
15

    
16
  f->metric=LSINFINITY;
17
  f->en=NULL;
18
}
19

    
20
void
21
init_efib(struct fib_node *fn)
22
{
23
  struct extfib *f=(struct extfib *)fn;
24

    
25
  f->metric=LSINFINITY;
26
  f->metric2=LSINFINITY;
27
  f->nh=ipa_from_u32(0);
28
  f->nhi=NULL;
29
}
30

    
31
/**
32
 * ospf_rt_spfa - calculate internal routes
33
 * @oa: OSPF area
34
 *
35
 * Calculation of internal paths in area is described in 16.1 of RFC 2328.
36
 * It's based on Dijkstra shortest path tree algorithmus.
37
 * RFC recommends to add ASBR routers into routing table. I don't do this
38
 * and latter parts of routing table calculation looks directly into LSA
39
 * Database. This function is invoked from area_disp().
40
 */
41
void
42
ospf_rt_spfa(struct ospf_area *oa)
43
{
44
  struct top_hash_entry *en;
45
  u32 i,*rts;
46
  struct ospf_lsa_rt *rt;
47
  struct ospf_lsa_rt_link *rtl,*rr;
48
  struct fib *in=&oa->infib;
49
  struct infib *nf;
50
  struct fib_iterator fit;
51
  int age=0,flush=0;
52
  struct proto *p=&oa->po->proto;
53
  struct proto_ospf *po=oa->po;
54
  ip_addr ip;
55
  struct ospf_lsa_net *ln;
56

    
57
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for area %I",
58
    oa->areaid);
59

    
60
  if(oa->rt==NULL) return;
61

    
62
  WALK_SLIST(SNODE en, oa->lsal)
63
  {
64
    en->color=OUTSPF;
65
    en->dist=LSINFINITY;
66
    en->nhi=NULL;
67
    en->nh=ipa_from_u32(0);
68
    DBG("Infinitying Type: %u, Id: %I, Rt: %I\n", en->lsa.type, en->lsa.id,
69
      en->lsa.rt);
70
  }
71

    
72
  FIB_WALK(in,nftmp)
73
  {
74
    nf=(struct infib *)nftmp;
75
    nf->metric=LSINFINITY;
76
    nf->en=NULL;
77
  }
78
  FIB_WALK_END;
79

    
80
  init_list(&oa->cand);                /* Empty list of candidates */
81
  oa->trcap=0;
82

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

    
85
  oa->rt->dist=0;
86
  oa->rt->color=CANDIDATE;
87
  add_head(&oa->cand, &oa->rt->cn);
88
  DBG("RT LSA: rt: %I, id: %I, type: %u\n",oa->rt->lsa.rt,oa->rt->lsa.id,oa->rt->lsa.type);
89

    
90
  while(!EMPTY_LIST(oa->cand))
91
  {
92
    struct top_hash_entry *act,*tmp;
93
    node *n;
94
    u16 met;
95

    
96
    n=HEAD(oa->cand);
97
    act=SKIP_BACK(struct top_hash_entry, cn, n);
98
    rem_node(n);
99

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

    
102
    act->color=INSPF;
103
    switch(act->lsa.type)
104
    {
105
      case LSA_T_RT:
106
        rt=(struct ospf_lsa_rt *)act->lsa_body;
107
        if((rt->VEB)&(1>>LSA_RT_V)) oa->trcap=1;
108
        rr=(struct ospf_lsa_rt_link *)(rt+1);
109
        DBG("  Number of links: %u\n",rt->links);
110
        for(i=0;i<rt->links;i++)
111
        {
112
          tmp=NULL;
113
          rtl=(rr+i);
114
          DBG("     Working on link: %I (type: %u)  ",rtl->id,rtl->type);
115
          switch(rtl->type)
116
          {
117
            case LSART_STUB:
118
             DBG("\n");
119
             ip=ipa_from_u32(rtl->id);
120
             nf=fib_get(in,&ip, ipa_mklen(ipa_from_u32(rtl->data)));
121
             if(nf->metric>(met=act->dist+rtl->metric))
122
             {
123
               DBG("       Adding stub route....\n");
124
               if(oa->rt==act) break;
125
               if(act->nhi==NULL) break;
126
               nf->metric=met;
127
               nf->en=act;
128
               DBG("            Adding stub route: %I\n",ip);
129
               DBG("            Next hop=%I\n",nf->en->nh);
130
             }
131
             else DBG("            NOT adding stub route: %I\n",ip);
132
             break;
133
            case LSART_VLNK:
134
              DBG("Ignoring\n");
135
              continue;
136
              break;
137
            case LSART_NET:
138
              tmp=ospf_hash_find(oa->gr,rtl->id,rtl->id,LSA_T_NET);
139
              if(tmp==NULL) DBG("Fuck!\n");
140
              else DBG("Found. :-)\n");
141
              break;
142
            case LSART_PTP:
143
              tmp=ospf_hash_find(oa->gr,rtl->id,rtl->id,LSA_T_RT);
144
              DBG("PTP searched.\n");
145
              break;
146
            default:
147
              log("Unknown link type in router lsa.");
148
              break;
149
          }
150
          if(tmp) DBG("Going to add cand, Mydist: %u, Req: %u\n",
151
            tmp->dist, act->dist+rtl->metric);
152
          add_cand(&oa->cand,tmp,act,act->dist+rtl->metric,oa);
153
        }
154
        break;
155
      case LSA_T_NET:
156
        ln=act->lsa_body;
157
        ip=ipa_and(ipa_from_u32(act->lsa.id),ln->netmask);
158
        nf=fib_get(in,&ip, ipa_mklen(ln->netmask));
159
        if(nf->metric>act->dist)
160
        {
161
          nf->metric=act->dist;
162
          nf->en=act;
163
          DBG("    Adding into routing table\n");
164
        }
165
        rts=(u32 *)(ln+1);
166
        for(i=0;i<(act->lsa.length-sizeof(struct ospf_lsa_header)-
167
          sizeof(struct ospf_lsa_net))/sizeof(u32);i++)
168
        {
169
          DBG("     Working on router %I ", *(rts+i));
170
          tmp=ospf_hash_find(oa->gr, *(rts+i), *(rts+i), LSA_T_RT);
171
          if(tmp!=NULL) DBG("Found :-)\n");
172
          else DBG("Fuck!\n");
173
          add_cand(&oa->cand,tmp,act,act->dist,oa);
174
        }
175
        break;
176
    }
177
  }
178
  /* Now sync our fib with nest's */
179
  DBG("\nNow syncing my rt table with nest's\n\n");
180
  FIB_ITERATE_INIT(&fit,in);
181
again:
182
  FIB_ITERATE_START(in,&fit,nftmp)
183
  {
184
    nf=(struct infib *)nftmp;
185
    if(nf->metric==LSINFINITY) 
186
    {
187
      net *ne;
188
      struct top_hash_entry *en=nf->en;
189
      ln=en->lsa_body;
190
  
191
      ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
192
      if((en!=NULL)&&(en->nhi!=NULL))
193
        DBG("Deleting rt entry %I\n     (P: %x, GW: %I, Iface: %s)\n",
194
        nf->fn.prefix, en, en->nh,en->nhi->name);
195
      rte_update(p->table, ne, p, NULL);
196

    
197
      /* Now delete my fib */
198
      FIB_ITERATE_PUT(&fit, nftmp);
199
      fib_delete(in, nftmp);
200
      goto again;
201
    }
202
    else
203
    {
204
      /* Update routing table */
205
      if(nf->en->nhi==NULL)
206
      {
207
        struct top_hash_entry *en=nf->en;
208
        struct ospf_neighbor *neigh;
209
        neighbor *nn;
210

    
211
        if((neigh=find_neigh_noifa(po,en->lsa.rt))==NULL)
212
        {
213
          goto skip;
214
        }
215
        nn=neigh_find(p,&neigh->ip,0);
216
        DBG("     Next hop calculated: %I\n", nn->addr);
217
        en->nh=nn->addr;
218
        en->nhi=nn->iface;
219
      }
220

    
221
      {
222
        net *ne;
223
        rta a0;
224
        rte *e;
225
        struct top_hash_entry *en=nf->en;
226
        ln=en->lsa_body;
227
  
228
        bzero(&a0, sizeof(a0));
229
  
230
        a0.proto=p;
231
        a0.source=RTS_OSPF;
232
        a0.scope=SCOPE_UNIVERSE;
233
        a0.cast=RTC_UNICAST;
234
        if(ipa_to_u32(en->nh)==0) a0.dest=RTD_DEVICE;
235
        else a0.dest=RTD_ROUTER;
236
        a0.flags=0;
237
        a0.aflags=0;
238
        a0.iface=en->nhi;
239
        a0.gw=en->nh;
240
        ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
241
        e=rte_get_temp(&a0);
242
        e->u.ospf.metric1=nf->metric;
243
        e->u.ospf.metric2=LSINFINITY;
244
        e->u.ospf.tag=0;
245
        e->pflags = 0;
246
        e->net=ne;
247
        e->pref = p->preference;
248
        DBG("Modifying rt entry %I\n     (GW: %I, Iface: %s)\n",
249
          nf->fn.prefix,en->nh,en->nhi->name);
250
        rte_update(p->table, ne, p, e);
251
      }
252
    }
253

    
254
  }
255
skip:
256
  FIB_ITERATE_END(nftmp);
257
  ospf_ext_spfa(po);
258
}
259

    
260
/**
261
 * ospf_ext_spfa - calculate external paths
262
 * @po: protocol
263
 *
264
 * After routing table for any area is calculated, calculation of external
265
 * path is invoked. This process is described in 16.6 of RFC 2328.
266
 * Inter- and Intra-area paths are always prefered over externals.
267
 */
268
void
269
ospf_ext_spfa(struct proto_ospf *po)        /* FIXME looking into inter-area */
270
{
271
  struct top_hash_entry *en,*etmp,*absr;
272
  struct fib *ef=&po->efib;
273
  struct extfib *nf;
274
  struct fib_iterator fit;
275
  struct ospf_area *oa=NULL,*atmp,*absroa;
276
  struct proto *p=&po->proto;
277
  struct ospf_lsa_ext *le;
278
  struct ospf_lsa_ext_tos *lt;
279
  int mlen;
280
  ip_addr ip,nnh;
281
  struct iface *nnhi=NULL;
282
  u16 met,met2;
283
  u32 tag;
284
  neighbor *nn;
285

    
286
  OSPF_TRACE(D_EVENTS,"Starting routing table calculation for ext routes");
287

    
288
  FIB_WALK(ef,nftmp)
289
  {
290
    nf=(struct extfib *)nftmp;
291
    nf->metric=LSINFINITY;
292
    nf->metric2=LSINFINITY;
293
  }
294
  FIB_WALK_END;
295

    
296
  WALK_LIST(oa,po->area_list)
297
  {
298
    if(!oa->stub) break;
299
  }
300

    
301
  if(oa==NULL) return;
302

    
303
  WALK_SLIST(en,oa->lsal)
304
  {
305
    if(en->lsa.type!=LSA_T_EXT) continue;
306
    if(en->lsa.age==LSA_MAXAGE) continue;
307
    if(en->lsa.rt==p->cf->global->router_id) continue;
308

    
309
    le=en->lsa_body;
310
    lt=(struct ospf_lsa_ext_tos *)(le+1);
311

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

    
315
    if(lt->metric==LSINFINITY) continue;
316
    ip=ipa_and(ipa_from_u32(en->lsa.id),le->netmask);
317
    mlen=ipa_mklen(le->netmask);
318
    if((mlen<0)||(mlen>32))
319
    {
320
      log("%s: Invalid mask in LSA. ID: %I, RT: %I, Type: %u, Mask %I",
321
        p->name,en->lsa.id,en->lsa.rt,en->lsa.type,le->netmask);
322
      continue;
323
    }
324

    
325
    nf=NULL;
326

    
327
    WALK_LIST(atmp,po->area_list)
328
    {
329
      if((nf=fib_find(&atmp->infib,&ip, mlen))!=NULL) continue;
330
      /* Some intra area path exists */
331
    }
332

    
333
    absr=NULL;
334
    absroa=NULL;
335
    nnhi=NULL;
336

    
337
    met=0;met2=0;tag=0;
338

    
339
    WALK_LIST(atmp,po->area_list)
340
    {
341
      if((etmp=ospf_hash_find(atmp->gr,en->lsa.rt,en->lsa.rt,LSA_T_RT))!=NULL)
342
      {
343
        if((absr==NULL) || (absr->dist>etmp->dist) ||
344
          ((etmp->dist==absr->dist) && (absroa->areaid<atmp->areaid)))
345
        {
346
          absr=etmp;
347
          absroa=atmp;
348
          break;
349
        }
350
      }
351
    }
352
    if((absr==NULL)||(absr->dist==LSINFINITY))
353
    {
354
      DBG("ABSR is null or its dist=INF\n");
355
      continue;
356
    }
357

    
358
    if(ipa_compare(lt->fwaddr,ipa_from_u32(0))==0)
359
    {
360
      if(lt->etos>0)
361
      {
362
        met=absr->dist;
363
        met2=lt->metric;
364
      }
365
      else
366
      {
367
        met=absr->dist+lt->metric;
368
        met2=LSINFINITY;
369
      }
370
      tag=lt->tag;
371
    }
372
    else
373
    {
374
      nf=NULL;
375
      WALK_LIST(atmp,po->area_list)
376
      {
377
        if((nf=fib_route(&atmp->infib,lt->fwaddr,32))!=NULL)
378
        {
379
          break;
380
        }
381
      }
382

    
383
      if(nf==NULL)
384
      {
385
        DBG("Cannot find network route (GW=%I\n",lt->fwaddr);
386
        continue;
387
      }
388

    
389
      if(lt->etos>0)
390
      {
391
        met=nf->metric;
392
        met2=lt->metric;
393
      }
394
      else
395
      {
396
        met=nf->metric+lt->metric;
397
        met2=LSINFINITY;
398
      }
399
      tag=lt->tag;
400

    
401
      if((nn=neigh_find(p,&lt->fwaddr,0))!=NULL)
402
      {
403
        nnh=nn->addr;
404
        nnhi=nn->iface;
405
      }
406
    }
407

    
408
    nf=fib_get(ef,&ip, mlen);
409
    if((nf->metric>met) || ((nf->metric==met)&&(nf->metric2>met2)))
410
    {
411
      nf->metric=met;
412
      nf->metric2=met2;
413
      nf->tag=tag;
414
      if(nnhi!=NULL)
415
      {
416
        nf->nh=nnh;
417
        nf->nhi=nnhi;
418
      }
419
      else
420
      {
421
        if(absr->nhi==NULL)
422
        {
423
          struct ospf_neighbor *neigh;
424

    
425
          if((neigh=find_neigh_noifa(po,absr->lsa.rt))==NULL)
426
          {
427
             DBG("Cannot find neighbor\n");
428
             continue;
429
          }
430
          nn=neigh_find(p,&neigh->ip,0);
431
          DBG("     Next hop calculated: %I\n", nn->addr);
432
          nf->nh=nn->addr;
433
          nf->nhi=nn->iface;
434
        }
435
        else
436
        {
437
          nf->nh=absr->nh;
438
          nf->nhi=absr->nhi;
439
        }
440
      }
441
    }
442
  }
443

    
444
  DBG("\nNow syncing my rt table with nest's\n\n");
445
  FIB_ITERATE_INIT(&fit,ef);
446
noch:
447
  FIB_ITERATE_START(ef,&fit,nftmp)
448
  {
449
    nf=(struct extfib *)nftmp;
450
    if(nf->metric==LSINFINITY) 
451
    {
452
      net *ne;
453
  
454
      ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
455
      DBG("Deleting rt entry %I\n     (IP: %I, GW: %I)\n",
456
        nf->fn.prefix,ip,nf->nh);
457
      rte_update(p->table, ne, p, NULL);
458

    
459
      /* Now delete my fib */
460
      FIB_ITERATE_PUT(&fit, nftmp);
461
      fib_delete(ef, nftmp);
462
      goto noch;
463
    }
464
    else
465
    {
466
      net *ne;
467
      rta a0;
468
      rte *e;
469
  
470
      bzero(&a0, sizeof(a0));
471
  
472
      a0.proto=p;
473
      a0.source=RTS_OSPF_EXT;
474
      a0.scope=SCOPE_UNIVERSE;
475
      a0.cast=RTC_UNICAST;
476
      a0.dest=RTD_ROUTER;
477
      a0.flags=0;
478
      a0.aflags=0;
479
      a0.iface=nf->nhi;
480
      a0.gw=nf->nh;
481
      ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
482
      e=rte_get_temp(&a0);
483
      e->u.ospf.metric1=nf->metric;
484
      e->u.ospf.metric2=nf->metric2;
485
      e->u.ospf.tag=nf->tag;
486
      e->pflags = 0;
487
      e->net=ne;
488
      e->pref = p->preference;
489
      DBG("Modifying rt entry %I\n     (IP: %I, GW: %I)\n",
490
        nf->fn.prefix,ip,nf->nh);
491
      rte_update(p->table, ne, p, e);
492
    }
493
  }
494
let:
495
  FIB_ITERATE_END(nftmp);
496
}
497

    
498
void
499
add_cand(list *l, struct top_hash_entry *en, struct top_hash_entry *par, 
500
  u16 dist, struct ospf_area *oa)
501
{
502
  node *prev,*n;
503
  int flag=0,added=0;
504
  struct top_hash_entry *act;
505
  struct proto *p=&oa->po->proto;
506

    
507
  if(en==NULL) return;
508
  if(en->lsa.age==LSA_MAXAGE) return;
509
  /* FIXME Does it have link back? Test it! */
510
  if(en->color==INSPF) return;
511

    
512
  if(dist>=en->dist) return;
513
  /*
514
   * FIXME The line above is not a bug, but we don't support
515
   * multiple next hops. I'll start as soon as nest will
516
   */
517
  DBG("     Adding candidate: rt: %I, id: %I, type: %u\n",en->lsa.rt,en->lsa.id,en->lsa.type);
518

    
519
  en->nhi=NULL;
520
  
521
  calc_next_hop(par,en,oa);
522

    
523
  if(en->color==CANDIDATE)        /* We found a shorter path */
524
  {
525
    rem_node(&en->cn);
526
  }
527

    
528
  en->dist=dist;
529
  en->color=CANDIDATE;
530

    
531
  prev=NULL;
532

    
533
  if(EMPTY_LIST(*l))
534
  {
535
    add_head(l,&en->cn);
536
  }
537
  else
538
  {
539
    WALK_LIST(n,*l)
540
    {
541
      act=SKIP_BACK(struct top_hash_entry, cn, n);
542
      if((act->dist>dist)||
543
        ((act->dist==dist)&&(act->lsa.type==LSA_T_NET)))
544
      {
545
        if(prev==NULL) add_head(l,&en->cn);
546
        else insert_node(&en->cn,prev);
547
        added=1;
548
        break;
549
      }
550
      prev=n;
551
    }
552

    
553
    if(!added)
554
    {
555
      add_tail(l,&en->cn);
556
    }
557
  }
558
  /* FIXME Some VLINK staff should be here */
559
}
560

    
561
void
562
calc_next_hop(struct top_hash_entry *par, struct top_hash_entry *en,
563
  struct ospf_area *oa)
564
{
565
  struct ospf_neighbor *neigh;
566
  struct proto *p=&oa->po->proto;
567
  struct proto_ospf *po=oa->po;
568
  struct ospf_iface *ifa;
569
  DBG("     Next hop called\n");
570
  if(ipa_to_u32(par->nh)==0)
571
  {
572
    neighbor *nn;
573
    DBG("     Next hop calculating for id: %I rt: %I type: %u\n",en->lsa.id,en->lsa.rt,en->lsa.type);
574
    if(par->lsa.type!=LSA_T_RT) 
575
    {
576
      if((neigh=find_neigh_noifa(po,en->lsa.rt))==NULL) return;
577
      nn=neigh_find(p,&neigh->ip,0);
578
      DBG("     Next hop calculated: %I.\n", nn->addr);
579
      en->nh=nn->addr;
580
      en->nhi=nn->iface;
581
      return;
582
    }
583
    else
584
    {
585
      if(par==oa->rt)
586
      {
587
        if(en->lsa.type==LSA_T_NET)
588
        {
589
          struct ospf_lsa_net *ne;
590
          ne=en->lsa_body;
591
  
592
          DBG("I'm parent (Net=%I)\n",(en->lsa.id & ipa_to_u32(ne->netmask)));
593
  
594
          WALK_LIST(ifa, oa->po->iface_list)
595
          {
596
            if(ipa_to_u32(ipa_and(ifa->iface->addr->ip,
597
              ipa_mkmask(ifa->iface->addr->pxlen))) ==
598
              (en->lsa.id & ipa_to_u32(ne->netmask)))
599
            {
600
              en->nhi=ifa->iface;
601
              en->nh=ipa_from_u32(0);
602
              DBG("Ifa: %s\n",en->nhi->name);
603
              return;
604
            }
605
          }
606
        }
607
        else
608
        {
609
          if((neigh=find_neigh_noifa(po,par->lsa.rt))==NULL) return;
610
          nn=neigh_find(p,&neigh->ip,0);
611
          en->nh=ipa_from_u32(0);
612
          en->nhi=nn->iface;
613
        }
614
        return;
615
      }
616
           
617
      if((neigh=find_neigh_noifa(po,par->lsa.rt))==NULL) return;
618
      nn=neigh_find(p,&neigh->ip,0);
619
      DBG("     Next hop calculated: %I\n", nn->addr);
620
      en->nh=nn->addr;
621
      en->nhi=nn->iface;
622
      return;
623
    }
624
  }
625
  en->nh=par->nh;
626
  en->nhi=par->nhi;
627
  DBG("     Next hop calculated: %I..\n", en->nh);
628
}
629