Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-table.c @ e48dae3e

History | View | Annotate | Download (18.1 KB)

1 62aa008a Martin Mares
/*
2
 *        BIRD -- Routing Table
3
 *
4 50fe90ed Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 62aa008a Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
10 1a54b1c6 Martin Mares
11 62aa008a Martin Mares
#include "nest/bird.h"
12
#include "nest/route.h"
13 2326b001 Martin Mares
#include "nest/protocol.h"
14 730f2e2c Martin Mares
#include "nest/cli.h"
15
#include "nest/iface.h"
16 2326b001 Martin Mares
#include "lib/resource.h"
17 5996da6a Martin Mares
#include "lib/event.h"
18 730f2e2c Martin Mares
#include "lib/string.h"
19 0e02abfd Martin Mares
#include "conf/conf.h"
20 529c4149 Martin Mares
#include "filter/filter.h"
21 221135d6 Martin Mares
#include "lib/string.h"
22 2326b001 Martin Mares
23
static slab *rte_slab;
24 e2dc2f30 Martin Mares
static linpool *rte_update_pool;
25 2326b001 Martin Mares
26 5996da6a Martin Mares
static pool *rt_table_pool;
27 0e02abfd Martin Mares
static list routing_tables;
28 5996da6a Martin Mares
29 cfd46ee4 Martin Mares
static void rt_format_via(rte *e, byte *via);
30
31 5996da6a Martin Mares
static void
32 2326b001 Martin Mares
rte_init(struct fib_node *N)
33
{
34
  net *n = (net *) N;
35
36 4c45595e Martin Mares
  N->flags = 0;
37 2326b001 Martin Mares
  n->routes = NULL;
38
}
39
40
rte *
41
rte_find(net *net, struct proto *p)
42
{
43
  rte *e = net->routes;
44
45
  while (e && e->attrs->proto != p)
46
    e = e->next;
47
  return e;
48
}
49
50
rte *
51
rte_get_temp(rta *a)
52
{
53
  rte *e = sl_alloc(rte_slab);
54
55
  e->attrs = a;
56 0cdbd397 Martin Mares
  e->flags = 0;
57 2326b001 Martin Mares
  e->pref = a->proto->preference;
58
  return e;
59
}
60
61 e2dc2f30 Martin Mares
rte *
62
rte_do_cow(rte *r)
63
{
64
  rte *e = sl_alloc(rte_slab);
65
66
  memcpy(e, r, sizeof(rte));
67
  e->attrs = rta_clone(r->attrs);
68
  e->flags = 0;
69
  return e;
70
}
71
72 2326b001 Martin Mares
static int                                /* Actually better or at least as good as */
73
rte_better(rte *new, rte *old)
74
{
75 d9f330c5 Martin Mares
  int (*better)(rte *, rte *);
76
77 2326b001 Martin Mares
  if (!old)
78
    return 1;
79
  if (new->pref > old->pref)
80
    return 1;
81
  if (new->pref < old->pref)
82
    return 0;
83 739ebd8e Martin Mares
  if (new->attrs->proto->proto != old->attrs->proto->proto)
84 4c1b4e1a Martin Mares
    {
85
      /*
86
       *  If the user has configured protocol preferences, so that two different protocols
87
       *  have the same preference, try to break the tie by comparing addresses. Not too
88
       *  useful, but keeps the ordering of routes unambiguous.
89
       */
90
      return new->attrs->proto->proto > old->attrs->proto->proto;
91
    }
92 d9f330c5 Martin Mares
  if (better = new->attrs->proto->rte_better)
93
    return better(new, old);
94
  return 0;
95 2326b001 Martin Mares
}
96
97 cfd46ee4 Martin Mares
static void
98
rte_trace(struct proto *p, rte *e, int dir, char *msg)
99
{
100
  byte via[STD_ADDRESS_P_LENGTH+32];
101
102
  rt_format_via(e, via);
103
  log(L_TRACE "%s %c %s %I/%d %s", p->name, dir, msg, e->net->n.prefix, e->net->n.pxlen, via);
104
}
105
106
static inline void
107
rte_trace_in(unsigned int flag, struct proto *p, rte *e, char *msg)
108
{
109
  if (p->debug & flag)
110 b0a47440 Martin Mares
    rte_trace(p, e, '>', msg);
111 cfd46ee4 Martin Mares
}
112
113
static inline void
114
rte_trace_out(unsigned int flag, struct proto *p, rte *e, char *msg)
115
{
116
  if (p->debug & flag)
117 b0a47440 Martin Mares
    rte_trace(p, e, '<', msg);
118 cfd46ee4 Martin Mares
}
119
120 529c4149 Martin Mares
static inline void
121 0da472d7 Martin Mares
do_rte_announce(struct announce_hook *a, net *net, rte *new, rte *old, ea_list *tmpa, int class)
122 529c4149 Martin Mares
{
123 0e02abfd Martin Mares
  struct proto *p = a->proto;
124 e2dc2f30 Martin Mares
  rte *new0 = new;
125
  rte *old0 = old;
126 7de45ba4 Martin Mares
127 e2dc2f30 Martin Mares
  if (new)
128 529c4149 Martin Mares
    {
129 0da472d7 Martin Mares
      int ok;
130 cfd46ee4 Martin Mares
      if ((class & IADDR_SCOPE_MASK) < p->min_scope)
131
        {
132
          rte_trace_out(D_FILTERS, p, new, "out of scope");
133
          new = NULL;
134
        }
135
      else if ((ok = p->import_control ? p->import_control(p, &new, &tmpa, rte_update_pool) : 0) < 0)
136
        {
137
          rte_trace_out(D_FILTERS, p, new, "rejected by protocol");
138
          new = NULL;
139
        }
140
      else if (ok)
141
        rte_trace_out(D_FILTERS, p, new, "forced accept by protocol");
142
      else if (p->out_filter == FILTER_REJECT ||
143 3a6337ec Martin Mares
               p->out_filter && f_run(p->out_filter, &new, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
144 cfd46ee4 Martin Mares
        {
145
          rte_trace_out(D_FILTERS, p, new, "filtered out");
146
          new = NULL;
147
        }
148 529c4149 Martin Mares
    }
149 e2dc2f30 Martin Mares
  if (old && p->out_filter)
150
    {
151
      /* FIXME: Do we really need to filter old routes? */
152
      if (p->out_filter == FILTER_REJECT)
153
        old = NULL;
154
      else
155
        {
156
          ea_list *tmpb = p->make_tmp_attrs ? p->make_tmp_attrs(old, rte_update_pool) : NULL;
157 3a6337ec Martin Mares
          if (f_run(p->out_filter, &old, &tmpb, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
158 e2dc2f30 Martin Mares
            old = NULL;
159
        }
160
    }
161 cfd46ee4 Martin Mares
  if (p->debug & D_ROUTES)
162
    {
163
      if (new && old)
164
        rte_trace_out(D_ROUTES, p, new, "replaced");
165
      else if (new)
166
        rte_trace_out(D_ROUTES, p, new, "added");
167 349e21bb Martin Mares
      else if (old)
168 cfd46ee4 Martin Mares
        rte_trace_out(D_ROUTES, p, old, "removed");
169
    }
170 529c4149 Martin Mares
  if (new || old)
171 bb027be1 Martin Mares
    p->rt_notify(p, net, new, old, tmpa);
172 e2dc2f30 Martin Mares
  if (new && new != new0)        /* Discard temporary rte's */
173
    rte_free(new);
174
  if (old && old != old0)
175
    rte_free(old);
176 529c4149 Martin Mares
}
177
178 e2dc2f30 Martin Mares
static void
179 0e02abfd Martin Mares
rte_announce(rtable *tab, net *net, rte *new, rte *old, ea_list *tmpa)
180 2326b001 Martin Mares
{
181 0e02abfd Martin Mares
  struct announce_hook *a;
182 0da472d7 Martin Mares
  int class = ipa_classify(net->n.prefix);
183 2326b001 Martin Mares
184 0e02abfd Martin Mares
  WALK_LIST(a, tab->hooks)
185 0a2e9d9f Martin Mares
    {
186 8c943173 Martin Mares
      ASSERT(a->proto->core_state == FS_HAPPY || a->proto->core_state == FS_FEEDING);
187 0da472d7 Martin Mares
      do_rte_announce(a, net, new, old, tmpa, class);
188 0a2e9d9f Martin Mares
    }
189 2326b001 Martin Mares
}
190
191 47b79306 Martin Mares
void
192
rt_feed_baby(struct proto *p)
193
{
194 0e02abfd Martin Mares
  struct announce_hook *h;
195 47b79306 Martin Mares
196 0e02abfd Martin Mares
  if (!p->ahooks)
197 47b79306 Martin Mares
    return;
198 6b9fa320 Martin Mares
  DBG("Announcing routes to new protocol %s\n", p->name);
199 0e02abfd Martin Mares
  for(h=p->ahooks; h; h=h->next)
200 47b79306 Martin Mares
    {
201 0e02abfd Martin Mares
      rtable *t = h->table;
202
      FIB_WALK(&t->fib, fn)
203 47b79306 Martin Mares
        {
204 0e02abfd Martin Mares
          net *n = (net *) fn;
205
          rte *e;
206
          for(e=n->routes; e; e=e->next)
207
            {
208
              struct proto *q = e->attrs->proto;
209
              ea_list *tmpa = q->make_tmp_attrs ? q->make_tmp_attrs(e, rte_update_pool) : NULL;
210 0da472d7 Martin Mares
              do_rte_announce(h, n, e, NULL, tmpa, ipa_classify(n->n.prefix));
211 0e02abfd Martin Mares
              lp_flush(rte_update_pool);
212 e48dae3e Martin Mares
              if (p->core_state != FS_FEEDING)
213
                return;  /* In the meantime, the protocol fell down. */
214 0e02abfd Martin Mares
            }
215 47b79306 Martin Mares
        }
216 0e02abfd Martin Mares
      FIB_WALK_END;
217 47b79306 Martin Mares
    }
218
}
219
220 421838ff Martin Mares
static inline int
221
rte_validate(rte *e)
222
{
223
  int c;
224
  net *n = e->net;
225
226 cfd46ee4 Martin Mares
  if (ipa_nonzero(ipa_and(n->n.prefix, ipa_not(ipa_mkmask(n->n.pxlen)))))
227
    {
228
      log(L_BUG "Ignoring bogus prefix %I/%d received via %s",
229
          n->n.prefix, n->n.pxlen, e->attrs->proto->name);
230
      return 0;
231
    }
232 421838ff Martin Mares
  if (n->n.pxlen)
233
    {
234
      c = ipa_classify(n->n.prefix);
235
      if (c < 0 || !(c & IADDR_HOST))
236
        {
237 85a291ff Martin Mares
          if (!ipa_nonzero(n->n.prefix))
238
            {
239
              /* Various default routes */
240
#ifdef IPV6
241
              if (n->n.pxlen == 96)
242
#else
243
              if (n->n.pxlen <= 1)
244
#endif
245
                return 1;
246
            }
247
          log(L_WARN "Ignoring bogus route %I/%d received via %s",
248
              n->n.prefix, n->n.pxlen, e->attrs->proto->name);
249 421838ff Martin Mares
          return 0;
250
        }
251 0da472d7 Martin Mares
      if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope)
252 421838ff Martin Mares
        {
253 0da472d7 Martin Mares
          log(L_WARN "Ignoring %s scope route %I/%d received from %I via %s",
254
              ip_scope_text(c & IADDR_SCOPE_MASK),
255
              n->n.prefix, n->n.pxlen, e->attrs->from, e->attrs->proto->name);
256
          return 0;
257 421838ff Martin Mares
        }
258
    }
259
  return 1;
260
}
261
262 04925e90 Martin Mares
void
263 2326b001 Martin Mares
rte_free(rte *e)
264
{
265 04925e90 Martin Mares
  if (e->attrs->aflags & RTAF_CACHED)
266
    rta_free(e->attrs);
267
  sl_free(rte_slab, e);
268
}
269
270
static inline void
271
rte_free_quick(rte *e)
272
{
273 2326b001 Martin Mares
  rta_free(e->attrs);
274
  sl_free(rte_slab, e);
275
}
276
277 67be5b23 Martin Mares
static int
278
rte_same(rte *x, rte *y)
279
{
280
  return
281
    x->attrs == y->attrs &&
282
    x->flags == y->flags &&
283
    x->pflags == y->pflags &&
284
    x->pref == y->pref &&
285
    (!x->attrs->proto->rte_same || x->attrs->proto->rte_same(x, y));
286
}
287
288 e2dc2f30 Martin Mares
static void
289 0e02abfd Martin Mares
rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa)
290 2326b001 Martin Mares
{
291
  rte *old_best = net->routes;
292
  rte *old = NULL;
293
  rte **k, *r, *s;
294
295
  k = &net->routes;                        /* Find and remove original route from the same protocol */
296
  while (old = *k)
297
    {
298
      if (old->attrs->proto == p)
299
        {
300 0b761098 Martin Mares
          if (new && rte_same(old, new))
301 67be5b23 Martin Mares
            {
302
              /* No changes, ignore the new route */
303
              rte_trace_in(D_ROUTES, p, new, "ignored");
304
              rte_free_quick(new);
305
              old->lastmod = now;
306
              return;
307
            }
308 2326b001 Martin Mares
          *k = old->next;
309
          break;
310
        }
311
      k = &old->next;
312
    }
313
314 0cdbd397 Martin Mares
  if (new && rte_better(new, old_best))        /* It's a new optimal route => announce and relink it */
315 2326b001 Martin Mares
    {
316 85810613 Martin Mares
      rte_trace_in(D_ROUTES, p, new, "added [best]");
317 0e02abfd Martin Mares
      rte_announce(table, net, new, old_best, tmpa);
318 2326b001 Martin Mares
      new->next = net->routes;
319
      net->routes = new;
320
    }
321
  else
322
    {
323
      if (old == old_best)                /* It has _replaced_ the old optimal route */
324
        {
325
          r = new;                        /* Find new optimal route and announce it */
326
          for(s=net->routes; s; s=s->next)
327
            if (rte_better(s, r))
328
              r = s;
329 0e02abfd Martin Mares
          rte_announce(table, net, r, old_best, tmpa);
330 2326b001 Martin Mares
          if (r)                        /* Re-link the new optimal route */
331
            {
332
              k = &net->routes;
333
              while (s = *k)
334
                {
335
                  if (s == r)
336
                    {
337
                      *k = r->next;
338
                      break;
339
                    }
340 dafd580e Martin Mares
                  k = &s->next;
341 2326b001 Martin Mares
                }
342
              r->next = net->routes;
343
              net->routes = r;
344 b9626ec6 Martin Mares
              if (!r &&
345
                  table->gc_counter++ >= table->config->gc_max_ops &&
346
                  table->gc_time + table->config->gc_min_time <= now)
347
                ev_schedule(table->gc_event);
348 2326b001 Martin Mares
            }
349
        }
350
      if (new)                                /* Link in the new non-optimal route */
351
        {
352
          new->next = old_best->next;
353
          old_best->next = new;
354 cfd46ee4 Martin Mares
          rte_trace_in(D_ROUTES, p, new, "added");
355
        }
356
      else if (old && (p->debug & D_ROUTES))
357
        {
358
          if (old != old_best)
359
            rte_trace_in(D_ROUTES, p, old, "removed");
360
          else if (net->routes)
361
            rte_trace_in(D_ROUTES, p, old, "removed [replaced]");
362
          else
363
            rte_trace_in(D_ROUTES, p, old, "removed [sole]");
364 2326b001 Martin Mares
        }
365
    }
366
  if (old)
367 5b22683d Martin Mares
    {
368
      if (p->rte_remove)
369
        p->rte_remove(net, old);
370 04925e90 Martin Mares
      rte_free_quick(old);
371 5b22683d Martin Mares
    }
372 8ca8683c Martin Mares
  if (new)
373
    {
374
      new->lastmod = now;
375
      if (p->rte_insert)
376
        p->rte_insert(net, new);
377
    }
378 5b22683d Martin Mares
}
379
380 e2dc2f30 Martin Mares
static int rte_update_nest_cnt;                /* Nesting counter to allow recursive updates */
381
382
static inline void
383
rte_update_lock(void)
384
{
385
  rte_update_nest_cnt++;
386
}
387
388
static inline void
389
rte_update_unlock(void)
390
{
391
  if (!--rte_update_nest_cnt)
392
    lp_flush(rte_update_pool);
393
}
394
395
void
396 0e02abfd Martin Mares
rte_update(rtable *table, net *net, struct proto *p, rte *new)
397 e2dc2f30 Martin Mares
{
398
  ea_list *tmpa = NULL;
399
400
  rte_update_lock();
401
  if (new)
402
    {
403 cfd46ee4 Martin Mares
      if (!rte_validate(new))
404
        {
405
          rte_trace_in(D_FILTERS, p, new, "invalid");
406
          goto drop;
407
        }
408
      if (p->in_filter == FILTER_REJECT)
409
        {
410
          rte_trace_in(D_FILTERS, p, new, "filtered out");
411
          goto drop;
412
        }
413 e2dc2f30 Martin Mares
      if (p->make_tmp_attrs)
414
        tmpa = p->make_tmp_attrs(new, rte_update_pool);
415
      if (p->in_filter)
416
        {
417 ccdc3397 Martin Mares
          ea_list *old_tmpa = tmpa;
418 0a06a9b8 Pavel Machek
          int fr = f_run(p->in_filter, &new, &tmpa, rte_update_pool, 0);
419 ccdc3397 Martin Mares
          if (fr > F_ACCEPT)
420 cfd46ee4 Martin Mares
            {
421
              rte_trace_in(D_FILTERS, p, new, "filtered out");
422
              goto drop;
423
            }
424 ccdc3397 Martin Mares
          if (tmpa != old_tmpa && p->store_tmp_attrs)
425 e2dc2f30 Martin Mares
            p->store_tmp_attrs(new, tmpa);
426
        }
427
      if (!(new->attrs->aflags & RTAF_CACHED)) /* Need to copy attributes */
428
        new->attrs = rta_lookup(new->attrs);
429
      new->flags |= REF_COW;
430
    }
431 0e02abfd Martin Mares
  rte_recalculate(table, net, p, new, tmpa);
432 e2dc2f30 Martin Mares
  rte_update_unlock();
433
  return;
434
435
drop:
436
  rte_free(new);
437
  rte_update_unlock();
438
}
439
440 5b22683d Martin Mares
void
441 0e02abfd Martin Mares
rte_discard(rtable *t, rte *old)        /* Non-filtered route deletion, used during garbage collection */
442 5b22683d Martin Mares
{
443 cfd46ee4 Martin Mares
  net *n = old->net;
444
  struct proto *p = old->attrs->proto;
445
446 e2dc2f30 Martin Mares
  rte_update_lock();
447 cfd46ee4 Martin Mares
  rte_recalculate(t, n, p, NULL, NULL);
448 e2dc2f30 Martin Mares
  rte_update_unlock();
449 2326b001 Martin Mares
}
450
451
void
452 a0762910 Martin Mares
rte_dump(rte *e)
453 2326b001 Martin Mares
{
454 a0762910 Martin Mares
  net *n = e->net;
455 0cdbd397 Martin Mares
  if (n)
456 962ba482 Martin Mares
    debug("%1I/%2d ", n->n.prefix, n->n.pxlen);
457 a0762910 Martin Mares
  else
458
    debug("??? ");
459 c10421d3 Martin Mares
  debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
460 0cdbd397 Martin Mares
  rta_dump(e->attrs);
461 c10421d3 Martin Mares
  if (e->attrs->proto->proto->dump_attrs)
462
    e->attrs->proto->proto->dump_attrs(e);
463 0cdbd397 Martin Mares
  debug("\n");
464 2326b001 Martin Mares
}
465 62aa008a Martin Mares
466 2326b001 Martin Mares
void
467
rt_dump(rtable *t)
468
{
469 0cdbd397 Martin Mares
  rte *e;
470
  net *n;
471 0e02abfd Martin Mares
  struct announce_hook *a;
472 0cdbd397 Martin Mares
473
  debug("Dump of routing table <%s>\n", t->name);
474 e440395d Martin Mares
#ifdef DEBUGGING
475 08e2d625 Martin Mares
  fib_check(&t->fib);
476 e440395d Martin Mares
#endif
477 08e2d625 Martin Mares
  FIB_WALK(&t->fib, fn)
478
    {
479
      n = (net *) fn;
480
      for(e=n->routes; e; e=e->next)
481
        rte_dump(e);
482 0cdbd397 Martin Mares
    }
483 08e2d625 Martin Mares
  FIB_WALK_END;
484 0e02abfd Martin Mares
  WALK_LIST(a, t->hooks)
485
    debug("\tAnnounces routes to protocol %s\n", a->proto->name);
486 0cdbd397 Martin Mares
  debug("\n");
487 2326b001 Martin Mares
}
488 62aa008a Martin Mares
489 2326b001 Martin Mares
void
490 6d45cf21 Martin Mares
rt_dump_all(void)
491
{
492 0e02abfd Martin Mares
  rtable *t;
493
494
  WALK_LIST(t, routing_tables)
495
    rt_dump(t);
496 6d45cf21 Martin Mares
}
497
498 8f6accb5 Martin Mares
static void
499 b9626ec6 Martin Mares
rt_gc(void *tab)
500 5996da6a Martin Mares
{
501 b9626ec6 Martin Mares
  rtable *t = tab;
502 0e02abfd Martin Mares
503 b9626ec6 Martin Mares
  DBG("Entered routing table garbage collector for %s after %d seconds and %d deletes\n",
504
      t->name, (int)(now - t->gc_time), t->gc_counter);
505
  rt_prune(t);
506 5996da6a Martin Mares
}
507
508 6d45cf21 Martin Mares
void
509 b9626ec6 Martin Mares
rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf)
510
{
511
  bzero(t, sizeof(*t));
512
  fib_init(&t->fib, p, sizeof(net), 0, rte_init);
513
  t->name = name;
514
  t->config = cf;
515
  init_list(&t->hooks);
516
  if (cf)
517
    {
518
      t->gc_event = ev_new(p);
519
      t->gc_event->hook = rt_gc;
520
      t->gc_event->data = t;
521
    }
522
}
523
524
void
525 2326b001 Martin Mares
rt_init(void)
526
{
527
  rta_init();
528 5996da6a Martin Mares
  rt_table_pool = rp_new(&root_pool, "Routing tables");
529 e2dc2f30 Martin Mares
  rte_update_pool = lp_new(rt_table_pool, 4080);
530 5996da6a Martin Mares
  rte_slab = sl_new(rt_table_pool, sizeof(rte));
531 0e02abfd Martin Mares
  init_list(&routing_tables);
532 2326b001 Martin Mares
}
533 1a54b1c6 Martin Mares
534
void
535
rt_prune(rtable *tab)
536
{
537
  struct fib_iterator fit;
538 5996da6a Martin Mares
  int rcnt = 0, rdel = 0, ncnt = 0, ndel = 0;
539 1a54b1c6 Martin Mares
540
  DBG("Pruning route table %s\n", tab->name);
541 08e2d625 Martin Mares
  FIB_ITERATE_INIT(&fit, &tab->fib);
542
again:
543
  FIB_ITERATE_START(&tab->fib, &fit, f)
544 1a54b1c6 Martin Mares
    {
545 08e2d625 Martin Mares
      net *n = (net *) f;
546
      rte *e;
547
      ncnt++;
548
    rescan:
549
      for (e=n->routes; e; e=e->next, rcnt++)
550
        if (e->attrs->proto->core_state != FS_HAPPY)
551
          {
552 0e02abfd Martin Mares
            rte_discard(tab, e);
553 08e2d625 Martin Mares
            rdel++;
554
            goto rescan;
555
          }
556
      if (!n->routes)                /* Orphaned FIB entry? */
557 1a54b1c6 Martin Mares
        {
558 08e2d625 Martin Mares
          FIB_ITERATE_PUT(&fit, f);
559
          fib_delete(&tab->fib, f);
560
          ndel++;
561
          goto again;
562 1a54b1c6 Martin Mares
        }
563
    }
564 08e2d625 Martin Mares
  FIB_ITERATE_END(f);
565 5996da6a Martin Mares
  DBG("Pruned %d of %d routes and %d of %d networks\n", rcnt, rdel, ncnt, ndel);
566 b9626ec6 Martin Mares
  tab->gc_counter = 0;
567
  tab->gc_time = now;
568 1a54b1c6 Martin Mares
}
569 0e02abfd Martin Mares
570
void
571
rt_prune_all(void)
572
{
573
  rtable *t;
574
575
  WALK_LIST(t, routing_tables)
576
    rt_prune(t);
577
}
578
579 b9626ec6 Martin Mares
struct rtable_config *
580
rt_new_table(struct symbol *s)
581
{
582
  struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
583
584
  cf_define_symbol(s, SYM_TABLE, c);
585
  c->name = s->name;
586
  add_tail(&new_config->tables, &c->n);
587
  c->gc_max_ops = 100;
588
  c->gc_min_time = 5;
589
  return c;
590
}
591
592 0e02abfd Martin Mares
void
593
rt_preconfig(struct config *c)
594
{
595
  struct symbol *s = cf_find_symbol("master");
596
597
  init_list(&c->tables);
598 b9626ec6 Martin Mares
  c->master_rtc = rt_new_table(s);
599 0e02abfd Martin Mares
}
600
601
void
602 50fe90ed Martin Mares
rt_lock_table(rtable *r)
603 0e02abfd Martin Mares
{
604 50fe90ed Martin Mares
  r->use_count++;
605
}
606
607
void
608
rt_unlock_table(rtable *r)
609
{
610
  if (!--r->use_count && r->deleted)
611
    {
612
      struct config *conf = r->deleted;
613
      DBG("Deleting routing table %s\n", r->name);
614
      rem_node(&r->n);
615
      fib_free(&r->fib);
616
      mb_free(r);
617
      config_del_obstacle(conf);
618
    }
619
}
620
621
void
622
rt_commit(struct config *new, struct config *old)
623
{
624
  struct rtable_config *o, *r;
625 0e02abfd Martin Mares
626 50fe90ed Martin Mares
  DBG("rt_commit:\n");
627
  if (old)
628 0e02abfd Martin Mares
    {
629 50fe90ed Martin Mares
      WALK_LIST(o, old->tables)
630
        {
631
          rtable *ot = o->table;
632
          if (!ot->deleted)
633
            {
634
              struct symbol *sym = cf_find_symbol(o->name);
635 bf8558bc Martin Mares
              if (sym && sym->class == SYM_TABLE && !new->shutdown)
636 50fe90ed Martin Mares
                {
637
                  DBG("\t%s: same\n", o->name);
638
                  r = sym->def;
639
                  r->table = ot;
640
                  ot->name = r->name;
641 b9626ec6 Martin Mares
                  ot->config = r;
642 50fe90ed Martin Mares
                }
643
              else
644
                {
645 bf8558bc Martin Mares
                  DBG("\t%s: deleted\n", o->name);
646 50fe90ed Martin Mares
                  ot->deleted = old;
647
                  config_add_obstacle(old);
648
                  rt_lock_table(ot);
649
                  rt_unlock_table(ot);
650
                }
651
            }
652
        }
653 0e02abfd Martin Mares
    }
654 50fe90ed Martin Mares
655
  WALK_LIST(r, new->tables)
656
    if (!r->table)
657
      {
658
        rtable *t = mb_alloc(rt_table_pool, sizeof(struct rtable));
659
        DBG("\t%s: created\n", r->name);
660 b9626ec6 Martin Mares
        rt_setup(rt_table_pool, t, r->name, r);
661 50fe90ed Martin Mares
        add_tail(&routing_tables, &t->n);
662
        r->table = t;
663
      }
664
  DBG("\tdone\n");
665 0e02abfd Martin Mares
}
666 730f2e2c Martin Mares
667
/*
668
 *  CLI commands
669
 */
670
671
static void
672 cfd46ee4 Martin Mares
rt_format_via(rte *e, byte *via)
673 730f2e2c Martin Mares
{
674
  rta *a = e->attrs;
675
676
  switch (a->dest)
677
    {
678
    case RTD_ROUTER:        bsprintf(via, "via %I on %s", a->gw, a->iface->name); break;
679
    case RTD_DEVICE:        bsprintf(via, "dev %s", a->iface->name); break;
680
    case RTD_BLACKHOLE:        bsprintf(via, "blackhole"); break;
681
    case RTD_UNREACHABLE:        bsprintf(via, "unreachable"); break;
682
    case RTD_PROHIBIT:        bsprintf(via, "prohibited"); break;
683
    default:                bsprintf(via, "???");
684
    }
685 cfd46ee4 Martin Mares
}
686
687
static void
688 ce1da96e Martin Mares
rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
689 cfd46ee4 Martin Mares
{
690 85810613 Martin Mares
  byte via[STD_ADDRESS_P_LENGTH+32], from[STD_ADDRESS_P_LENGTH+6];
691 cfd46ee4 Martin Mares
  byte tm[TM_RELTIME_BUFFER_SIZE], info[256];
692
  rta *a = e->attrs;
693
694
  rt_format_via(e, via);
695 730f2e2c Martin Mares
  tm_format_reltime(tm, e->lastmod);
696
  if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->gw))
697
    bsprintf(from, " from %I", a->from);
698
  else
699
    from[0] = 0;
700 ce1da96e Martin Mares
  if (a->proto->proto->get_route_info || d->verbose)
701
    {
702
      /* Need to normalize the extended attributes */
703
      ea_list *t = tmpa;
704
      t = ea_append(t, a->eattrs);
705
      tmpa = alloca(ea_scan(t));
706
      ea_merge(t, tmpa);
707
    }
708 730f2e2c Martin Mares
  if (a->proto->proto->get_route_info)
709 ce1da96e Martin Mares
    a->proto->proto->get_route_info(e, info, tmpa);
710 730f2e2c Martin Mares
  else
711
    bsprintf(info, " (%d)", e->pref);
712
  cli_printf(c, -1007, "%-18s %s [%s %s%s]%s", ia, via, a->proto->name, tm, from, info);
713
  if (d->verbose)
714 ce1da96e Martin Mares
    rta_show(c, a, tmpa);
715 730f2e2c Martin Mares
}
716
717
static void
718
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
719
{
720
  rte *e, *ee;
721
  byte ia[STD_ADDRESS_P_LENGTH+8];
722 ce1da96e Martin Mares
  int ok;
723 730f2e2c Martin Mares
724
  bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen);
725 0d307082 Martin Mares
  if (n->routes)
726
    d->net_counter++;
727 730f2e2c Martin Mares
  for(e=n->routes; e; e=e->next)
728
    {
729 ce1da96e Martin Mares
      struct ea_list *tmpa, *old_tmpa;
730
      struct proto *p0 = e->attrs->proto;
731
      struct proto *p1 = d->import_protocol;
732 23693958 Martin Mares
      d->rt_counter++;
733 730f2e2c Martin Mares
      ee = e;
734
      rte_update_lock();                /* We use the update buffer for filtering */
735 ce1da96e Martin Mares
      old_tmpa = tmpa = p0->make_tmp_attrs ? p0->make_tmp_attrs(e, rte_update_pool) : NULL;
736
      ok = (d->filter == FILTER_ACCEPT || f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
737
      if (ok && d->import_mode)
738
        {
739
          int ic = (p1->import_control ? p1->import_control(p1, &e, &tmpa, rte_update_pool) : 0);
740
          if (ic < 0)
741
            ok = 0;
742
          else if (!ic && d->import_mode > 1)
743
            {
744
              if (p1->out_filter == FILTER_REJECT ||
745
                  p1->out_filter && f_run(p1->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
746
                ok = 0;
747
            }
748
        }
749
      if (ok)
750 730f2e2c Martin Mares
        {
751 23693958 Martin Mares
          d->show_counter++;
752 ce1da96e Martin Mares
          rt_show_rte(c, ia, e, d, tmpa);
753 730f2e2c Martin Mares
          ia[0] = 0;
754
        }
755
      if (e != ee)
756
        rte_free(ee);
757
      rte_update_unlock();
758 0117d004 Martin Mares
      if (d->primary_only)
759 ce1da96e Martin Mares
        break;
760 730f2e2c Martin Mares
    }
761
}
762
763
static void
764
rt_show_cont(struct cli *c)
765
{
766
  struct rt_show_data *d = c->rover;
767 f098e072 Martin Mares
#ifdef DEBUGGING
768
  unsigned max = 4;
769
#else
770
  unsigned max = 64;
771
#endif
772 730f2e2c Martin Mares
  struct fib *fib = &d->table->fib;
773
  struct fib_iterator *it = &d->fit;
774
775
  FIB_ITERATE_START(fib, it, f)
776
    {
777
      net *n = (net *) f;
778 ce1da96e Martin Mares
      if (d->running_on_config && d->running_on_config != config)
779
        {
780
          cli_printf(c, 8004, "Stopped due to reconfiguration");
781
          goto done;
782
        }
783
      if (d->import_protocol &&
784
          d->import_protocol->core_state != FS_HAPPY &&
785
          d->import_protocol->core_state != FS_FEEDING)
786
        {
787
          cli_printf(c, 8005, "Protocol is down");
788
          goto done;
789
        }
790 730f2e2c Martin Mares
      if (!max--)
791
        {
792
          FIB_ITERATE_PUT(it, f);
793
          return;
794
        }
795
      rt_show_net(c, n, d);
796
    }
797
  FIB_ITERATE_END(f);
798 23693958 Martin Mares
  if (d->stats)
799
    cli_printf(c, 14, "%d of %d routes for %d networks", d->show_counter, d->rt_counter, d->net_counter);
800
  else
801
    cli_printf(c, 0, "");
802 ce1da96e Martin Mares
done:
803 730f2e2c Martin Mares
  c->cont = c->cleanup = NULL;
804
}
805
806
static void
807
rt_show_cleanup(struct cli *c)
808
{
809
  struct rt_show_data *d = c->rover;
810
811
  /* Unlink the iterator */
812
  fit_get(&d->table->fib, &d->fit);
813
}
814
815
void
816
rt_show(struct rt_show_data *d)
817
{
818
  net *n;
819
820
  if (d->pxlen == 256)
821
    {
822
      FIB_ITERATE_INIT(&d->fit, &d->table->fib);
823
      this_cli->cont = rt_show_cont;
824
      this_cli->cleanup = rt_show_cleanup;
825
      this_cli->rover = d;
826
    }
827
  else
828
    {
829
      n = fib_find(&d->table->fib, &d->prefix, d->pxlen);
830
      if (n)
831
        {
832
          rt_show_net(this_cli, n, d);
833
          cli_msg(0, "");
834
        }
835
      else
836
        cli_msg(8001, "Network not in table");
837
    }
838
}