Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / rt.c @ 3b8b1bd0

History | View | Annotate | Download (7.86 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
ospf_rt_spfa(struct ospf_area *oa)
22
{
23
  struct top_hash_entry *en, *nx;
24
  u32 i,*rts;
25
  struct ospf_lsa_rt *rt;
26
  struct ospf_lsa_rt_link *rtl,*rr;
27
  struct fib *in=&oa->infib;
28
  struct infib *nf;
29
  bird_clock_t delta;
30
  int age=0,flush=0;
31
  struct proto *p=&oa->po->proto;
32
  struct proto_ospf *po=oa->po;
33
  ip_addr ip;
34
  struct fib_iterator fit;
35
  struct ospf_lsa_net *ln;
36

    
37
  debug("%s: Starting routing table calculation for area %I\n",p->name,
38
    oa->areaid);
39

    
40
  flush=can_flush_lsa(oa);
41

    
42
  if((delta=now-oa->lage)>=AGINGDELTA)
43
  {
44
     oa->lage=now;
45
     age=1;
46
  }
47

    
48
  WALK_SLIST_DELSAFE(SNODE en, nx, oa->lsal)
49
  {
50
    en->color=OUTSPF;
51
    en->dist=LSINFINITY;
52
    en->nhi=NULL;
53
    if(age) ospf_age(en,delta,flush,oa);
54
  }
55

    
56
  FIB_WALK(in,nftmp)
57
  {
58
    nf=(struct infib *)nftmp;
59
    nf->metric=LSINFINITY;
60
  }
61
  FIB_WALK_END;
62

    
63
  init_list(&oa->cand);                /* Empty list of candidates */
64
  oa->trcap=0;
65

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

    
68
  oa->rt->dist=0;
69
  oa->rt->color=CANDIDATE;
70
  add_head(&oa->cand, &oa->rt->cn);
71
  DBG("RT LSA: rt: %I, id: %I, type: %u\n",oa->rt->lsa.rt,oa->rt->lsa.id,oa->rt->lsa.type);
72

    
73
  while(!EMPTY_LIST(oa->cand))
74
  {
75
    struct top_hash_entry *act,*tmp;
76
    node *n;
77
    u16 met;
78

    
79
    n=HEAD(oa->cand);
80
    act=SKIP_BACK(struct top_hash_entry, cn, n);
81
    rem_node(n);
82

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

    
85
    act->color=INSPF;
86
    switch(act->lsa.type)
87
    {
88
      case LSA_T_RT:
89
        rt=(struct ospf_lsa_rt *)act->lsa_body;
90
        if((rt->VEB)&(1>>LSA_RT_V)) oa->trcap=1;
91
        rr=(struct ospf_lsa_rt_link *)(rt+1);
92
        DBG("  Number of links: %u\n",rt->links);
93
        for(i=0;i<rt->links;i++)
94
        {
95
          tmp=NULL;
96
          rtl=(rr+i);
97
          DBG("     Working on link: %I (type: %u)  ",rtl->id,rtl->type);
98
          switch(rtl->type)
99
          {
100
            case LSART_STUB:        /* FIXME add stub network into fib */
101
             DBG("\n");
102
             ip=ipa_from_u32(rtl->id);
103
             nf=fib_get(in,&ip, ipa_mklen(ipa_from_u32(rtl->data)));
104
             if(nf->metric>(met=act->dist+rtl->metric))
105
             {
106
               nf->metric=met;
107
               nf->en=act;
108
             }
109
             break;
110
            case LSART_VLNK:
111
              DBG("Ignoring\n");
112
              continue;
113
              break;
114
            case LSART_NET:
115
              tmp=ospf_hash_find(oa->gr,rtl->id,rtl->id,LSA_T_NET);
116
              if(tmp==NULL) DBG("Fuck!\n");
117
              else DBG("Found. :-)\n");
118
              break;
119
            case LSART_PTP: /* FIXME */
120
              tmp=ospf_hash_find(oa->gr,rtl->id,rtl->id,LSA_T_RT);
121
              DBG("PTP searched.\n");
122
              break;
123
            default:
124
              log("Unknown link type in router lsa.\n");
125
              break;
126
          }
127
          add_cand(&oa->cand,tmp,act,act->dist+rtl->metric,oa);
128
        }
129
        break;
130
      case LSA_T_NET:        /* FIXME Add to fib */
131
        ln=act->lsa_body;
132
        ip=ipa_and(ipa_from_u32(act->lsa.id),ln->netmask);
133
        nf=fib_get(in,&ip, ipa_mklen(ln->netmask));
134
        if(nf->metric>act->dist)
135
        {
136
          nf->metric=act->dist;
137
          nf->en=act;
138
        }
139

    
140
        rts=(u32 *)(ln+1);
141
        for(i=0;i<(act->lsa.length-sizeof(struct ospf_lsa_header)-
142
          sizeof(struct ospf_lsa_net))/sizeof(u32);i++)
143
        {
144
          DBG("     Working on router %I ",*(rts+i));
145
          tmp=ospf_hash_find(oa->gr, *(rts+i), *(rts+i), LSA_T_RT);
146
          if(tmp!=NULL) DBG("Found :-)\n");
147
          else DBG("Fuck!\n");
148
          add_cand(&oa->cand,tmp,act,act->dist,oa);
149
        }
150
        break;
151
    }
152
  }
153
  /* Now sync our fib with nest's */
154
  DBG("\nNow syncing my rt table with nest's\n\n");
155
  FIB_ITERATE_INIT(&fit,in);
156
again:
157
  FIB_ITERATE_START(in,&fit,nftmp)
158
  {
159
    nf=(struct infib *)nftmp;
160
    if(nf->metric==LSINFINITY) 
161
    {
162
      net *ne;
163
      rta a0;
164
      struct top_hash_entry *en=nf->en;
165
      ln=en->lsa_body;
166
  
167
      bzero(&a0, sizeof(a0));
168
  
169
      a0.proto=p;
170
      a0.source=RTS_OSPF;
171
      a0.scope=SCOPE_UNIVERSE;        /* What's this good for? */
172
      a0.cast=RTC_UNICAST;
173
      a0.dest=RTD_ROUTER;
174
      a0.flags=0;
175
      a0.aflags=0;
176
      a0.iface=en->nhi;
177
      a0.gw=en->nh;
178
      a0.from=en->nh;                /* FIXME Just a test */
179
      ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
180
      DBG("Deleting rt entry %I\n     (IP: %I, GW: %I, Iface: %s)\n",
181
        nf->fn.prefix,ip,en->nh,en->nhi->name);
182
      rte_update(p->table, ne, p, NULL);
183

    
184
      /* Now delete my fib */
185
      FIB_ITERATE_PUT(&fit, nftmp);
186
      fib_delete(in, nftmp);
187
      goto again;
188
    }
189
    else
190
    {
191
      /* Update routing table */
192
      if(nf->en->nhi==NULL)
193
      {
194
        struct top_hash_entry *en=nf->en;
195
        struct ospf_neighbor *neigh;
196
        neighbor *nn;
197

    
198
        if((neigh=find_neigh_noifa(po,en->lsa.rt))==NULL)
199
        {
200
          goto skip;
201
        }
202
        nn=neigh_find(p,&neigh->ip,0);
203
        DBG("     Next hop calculated: %I\n", nn->addr);
204
        en->nh=nn->addr;
205
        en->nhi=nn->iface;
206
      }
207

    
208
      {
209
        net *ne;
210
        rta a0;
211
        rte *e;
212
        struct top_hash_entry *en=nf->en;
213
        ln=en->lsa_body;
214
  
215
        bzero(&a0, sizeof(a0));
216
  
217
        a0.proto=p;
218
        a0.source=RTS_OSPF;
219
        a0.scope=SCOPE_UNIVERSE;        /* What's this good for? */
220
        a0.cast=RTC_UNICAST;
221
        a0.dest=RTD_ROUTER;
222
        a0.flags=0;
223
        a0.aflags=0;
224
        a0.iface=en->nhi;
225
        a0.gw=en->nh;
226
        a0.from=en->nh;                /* FIXME Just a test */
227
        ne=net_get(p->table, nf->fn.prefix, nf->fn.pxlen);
228
        e=rte_get_temp(&a0);
229
        e->u.ospf.metric1=nf->metric;
230
        e->u.ospf.metric2=0;
231
        e->u.ospf.tag=0;                        /* FIXME Some config? */
232
        e->pflags = 0;
233
        e->net=ne;
234
        e->pref = p->preference;
235
        DBG("Modifying rt entry %I\n     (IP: %I, GW: %I, Iface: %s)\n",
236
          nf->fn.prefix,ip,en->nh,en->nhi->name);
237
        rte_update(p->table, ne, p, e);
238
      }
239
    }
240

    
241
  }
242
skip:
243
  FIB_ITERATE_END(nftmp);
244
}
245

    
246
void
247
ospf_ext_spfa(struct proto_ospf *po)
248
{
249
  struct top_hash_entry *en;
250

    
251
  /* FIXME Go on */
252
}
253

    
254
void
255
add_cand(list *l, struct top_hash_entry *en, struct top_hash_entry *par, 
256
  u16 dist, struct ospf_area *oa)
257
{
258
  node *prev,*n;
259
  int flag=0,added=0;
260
  struct top_hash_entry *act;
261
  struct proto *p=&oa->po->proto;
262

    
263
  if(en==NULL) return;
264
  if(en->lsa.age==LSA_MAXAGE) return;
265
  /* FIXME Does it have link back? Test it! */
266
  if(en->color==INSPF) return;
267

    
268
  if(dist>=en->dist) return;
269
  /*
270
   * FIXME The line above is not a bug, but we don't support
271
   * multiple next hops. I'll start as soon as nest will
272
   */
273
  DBG("     Adding candidate: rt: %I, id: %I, type: %u\n",en->lsa.rt,en->lsa.id,en->lsa.type);
274

    
275
  en->nhi=NULL;
276
  
277
  calc_next_hop(par,en,oa);
278

    
279
  if(en->color==CANDIDATE)        /* We found a shorter path */
280
  {
281
    rem_node(&en->cn);
282
  }
283

    
284
  en->dist=dist;
285
  en->color=CANDIDATE;
286

    
287
  prev=NULL;
288

    
289
  if(EMPTY_LIST(*l))
290
  {
291
    add_head(l,&en->cn);
292
  }
293
  else
294
  {
295
    WALK_LIST(n,*l)
296
    {
297
      act=SKIP_BACK(struct top_hash_entry, cn, n);
298
      if((act->dist>dist)||
299
        ((act->dist==dist)&&(act->lsa.type==LSA_T_NET)))
300
      {
301
        if(prev==NULL) add_head(l,&en->cn);
302
        else insert_node(&en->cn,prev);
303
        added=1;
304
        break;
305
      }
306
      prev=n;
307
    }
308

    
309
    if(!added)
310
    {
311
      add_tail(l,&en->cn);
312
    }
313
  }
314
  /* FIXME Some VLINK staff should be here */
315
}
316

    
317
void
318
calc_next_hop(struct top_hash_entry *par, struct top_hash_entry *en,
319
  struct ospf_area *oa)
320
{
321
  struct ospf_neighbor *neigh;
322
  struct proto *p=&oa->po->proto;
323
  struct proto_ospf *po=oa->po;
324
  DBG("     Next hop called\n");
325
  if(par==oa->rt) return;
326
  if(par->nhi==NULL)
327
  {
328
    neighbor *nn;
329
    DBG("     Next hop calculating for id: %I rt: %I type: %u\n",en->lsa.id,en->lsa.rt,en->lsa.type);
330
    if(par->lsa.type!=LSA_T_RT) return;
331
    if((neigh=find_neigh_noifa(po,par->lsa.rt))==NULL) return;
332
    nn=neigh_find(p,&neigh->ip,0);
333
    DBG("     Next hop calculated: %I\n", nn->addr);
334
    en->nh=nn->addr;
335
    en->nhi=nn->iface;
336
    return;
337
  }
338
  en->nh=par->nh;
339
  en->nhi=par->nhi;
340
  DBG("     Next hop calculated: %I\n", en->nh);
341
}
342