Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (17.9 KB)

1
/*
2
 *        BIRD -- Routing Table
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#undef LOCAL_DEBUG
10

    
11
#include "nest/bird.h"
12
#include "nest/route.h"
13
#include "nest/protocol.h"
14
#include "nest/cli.h"
15
#include "nest/iface.h"
16
#include "lib/resource.h"
17
#include "lib/event.h"
18
#include "lib/string.h"
19
#include "conf/conf.h"
20
#include "filter/filter.h"
21
#include "lib/string.h"
22

    
23
static slab *rte_slab;
24
static linpool *rte_update_pool;
25

    
26
static pool *rt_table_pool;
27
static list routing_tables;
28

    
29
static void rt_format_via(rte *e, byte *via);
30

    
31
static void
32
rte_init(struct fib_node *N)
33
{
34
  net *n = (net *) N;
35

    
36
  N->flags = 0;
37
  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
  e->flags = 0;
57
  e->pref = a->proto->preference;
58
  return e;
59
}
60

    
61
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
static int                                /* Actually better or at least as good as */
73
rte_better(rte *new, rte *old)
74
{
75
  int (*better)(rte *, rte *);
76

    
77
  if (!old)
78
    return 1;
79
  if (new->pref > old->pref)
80
    return 1;
81
  if (new->pref < old->pref)
82
    return 0;
83
  if (new->attrs->proto->proto != old->attrs->proto->proto)
84
    {
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
  if (better = new->attrs->proto->rte_better)
93
    return better(new, old);
94
  return 0;
95
}
96

    
97
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
    rte_trace(p, e, '>', msg);
111
}
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
    rte_trace(p, e, '<', msg);
118
}
119

    
120
static inline void
121
do_rte_announce(struct announce_hook *a, net *net, rte *new, rte *old, ea_list *tmpa, int class)
122
{
123
  struct proto *p = a->proto;
124
  rte *new0 = new;
125
  rte *old0 = old;
126

    
127
  if (new)
128
    {
129
      int ok;
130
      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
               p->out_filter && f_run(p->out_filter, &new, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
144
        {
145
          rte_trace_out(D_FILTERS, p, new, "filtered out");
146
          new = NULL;
147
        }
148
    }
149
  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
          if (f_run(p->out_filter, &old, &tmpb, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
158
            old = NULL;
159
        }
160
    }
161
  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
      else if (old)
168
        rte_trace_out(D_ROUTES, p, old, "removed");
169
    }
170
  if (new || old)
171
    p->rt_notify(p, net, new, old, tmpa);
172
  if (new && new != new0)        /* Discard temporary rte's */
173
    rte_free(new);
174
  if (old && old != old0)
175
    rte_free(old);
176
}
177

    
178
static void
179
rte_announce(rtable *tab, net *net, rte *new, rte *old, ea_list *tmpa)
180
{
181
  struct announce_hook *a;
182
  int class = ipa_classify(net->n.prefix);
183

    
184
  WALK_LIST(a, tab->hooks)
185
    {
186
      ASSERT(a->proto->core_state == FS_HAPPY || a->proto->core_state == FS_FEEDING);
187
      do_rte_announce(a, net, new, old, tmpa, class);
188
    }
189
}
190

    
191
void
192
rt_feed_baby(struct proto *p)
193
{
194
  struct announce_hook *h;
195

    
196
  if (!p->ahooks)
197
    return;
198
  DBG("Announcing routes to new protocol %s\n", p->name);
199
  for(h=p->ahooks; h; h=h->next)
200
    {
201
      rtable *t = h->table;
202
      FIB_WALK(&t->fib, fn)
203
        {
204
          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
              do_rte_announce(h, n, e, NULL, tmpa, ipa_classify(n->n.prefix));
211
              lp_flush(rte_update_pool);
212
            }
213
        }
214
      FIB_WALK_END;
215
    }
216
}
217

    
218
static inline int
219
rte_validate(rte *e)
220
{
221
  int c;
222
  net *n = e->net;
223

    
224
  if (ipa_nonzero(ipa_and(n->n.prefix, ipa_not(ipa_mkmask(n->n.pxlen)))))
225
    {
226
      log(L_BUG "Ignoring bogus prefix %I/%d received via %s",
227
          n->n.prefix, n->n.pxlen, e->attrs->proto->name);
228
      return 0;
229
    }
230
  if (n->n.pxlen)
231
    {
232
      c = ipa_classify(n->n.prefix);
233
      if (c < 0 || !(c & IADDR_HOST))
234
        {
235
          if (!ipa_nonzero(n->n.prefix))
236
            {
237
              /* Various default routes */
238
#ifdef IPV6
239
              if (n->n.pxlen == 96)
240
#else
241
              if (n->n.pxlen <= 1)
242
#endif
243
                return 1;
244
            }
245
          log(L_WARN "Ignoring bogus route %I/%d received via %s",
246
              n->n.prefix, n->n.pxlen, e->attrs->proto->name);
247
          return 0;
248
        }
249
      if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope)
250
        {
251
          log(L_WARN "Ignoring %s scope route %I/%d received from %I via %s",
252
              ip_scope_text(c & IADDR_SCOPE_MASK),
253
              n->n.prefix, n->n.pxlen, e->attrs->from, e->attrs->proto->name);
254
          return 0;
255
        }
256
    }
257
  return 1;
258
}
259

    
260
void
261
rte_free(rte *e)
262
{
263
  if (e->attrs->aflags & RTAF_CACHED)
264
    rta_free(e->attrs);
265
  sl_free(rte_slab, e);
266
}
267

    
268
static inline void
269
rte_free_quick(rte *e)
270
{
271
  rta_free(e->attrs);
272
  sl_free(rte_slab, e);
273
}
274

    
275
static int
276
rte_same(rte *x, rte *y)
277
{
278
  return
279
    x->attrs == y->attrs &&
280
    x->flags == y->flags &&
281
    x->pflags == y->pflags &&
282
    x->pref == y->pref &&
283
    (!x->attrs->proto->rte_same || x->attrs->proto->rte_same(x, y));
284
}
285

    
286
static void
287
rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa)
288
{
289
  rte *old_best = net->routes;
290
  rte *old = NULL;
291
  rte **k, *r, *s;
292

    
293
  k = &net->routes;                        /* Find and remove original route from the same protocol */
294
  while (old = *k)
295
    {
296
      if (old->attrs->proto == p)
297
        {
298
          if (new && rte_same(old, new))
299
            {
300
              /* No changes, ignore the new route */
301
              rte_trace_in(D_ROUTES, p, new, "ignored");
302
              rte_free_quick(new);
303
              old->lastmod = now;
304
              return;
305
            }
306
          *k = old->next;
307
          break;
308
        }
309
      k = &old->next;
310
    }
311

    
312
  if (new && rte_better(new, old_best))        /* It's a new optimal route => announce and relink it */
313
    {
314
      rte_trace_in(D_ROUTES, p, new, "added [best]");
315
      rte_announce(table, net, new, old_best, tmpa);
316
      new->next = net->routes;
317
      net->routes = new;
318
    }
319
  else
320
    {
321
      if (old == old_best)                /* It has _replaced_ the old optimal route */
322
        {
323
          r = new;                        /* Find new optimal route and announce it */
324
          for(s=net->routes; s; s=s->next)
325
            if (rte_better(s, r))
326
              r = s;
327
          rte_announce(table, net, r, old_best, tmpa);
328
          if (r)                        /* Re-link the new optimal route */
329
            {
330
              k = &net->routes;
331
              while (s = *k)
332
                {
333
                  if (s == r)
334
                    {
335
                      *k = r->next;
336
                      break;
337
                    }
338
                  k = &s->next;
339
                }
340
              r->next = net->routes;
341
              net->routes = r;
342
              if (!r &&
343
                  table->gc_counter++ >= table->config->gc_max_ops &&
344
                  table->gc_time + table->config->gc_min_time <= now)
345
                ev_schedule(table->gc_event);
346
            }
347
        }
348
      if (new)                                /* Link in the new non-optimal route */
349
        {
350
          new->next = old_best->next;
351
          old_best->next = new;
352
          rte_trace_in(D_ROUTES, p, new, "added");
353
        }
354
      else if (old && (p->debug & D_ROUTES))
355
        {
356
          if (old != old_best)
357
            rte_trace_in(D_ROUTES, p, old, "removed");
358
          else if (net->routes)
359
            rte_trace_in(D_ROUTES, p, old, "removed [replaced]");
360
          else
361
            rte_trace_in(D_ROUTES, p, old, "removed [sole]");
362
        }
363
    }
364
  if (old)
365
    {
366
      if (p->rte_remove)
367
        p->rte_remove(net, old);
368
      rte_free_quick(old);
369
    }
370
  if (new)
371
    {
372
      new->lastmod = now;
373
      if (p->rte_insert)
374
        p->rte_insert(net, new);
375
    }
376
}
377

    
378
static int rte_update_nest_cnt;                /* Nesting counter to allow recursive updates */
379

    
380
static inline void
381
rte_update_lock(void)
382
{
383
  rte_update_nest_cnt++;
384
}
385

    
386
static inline void
387
rte_update_unlock(void)
388
{
389
  if (!--rte_update_nest_cnt)
390
    lp_flush(rte_update_pool);
391
}
392

    
393
void
394
rte_update(rtable *table, net *net, struct proto *p, rte *new)
395
{
396
  ea_list *tmpa = NULL;
397

    
398
  rte_update_lock();
399
  if (new)
400
    {
401
      if (!rte_validate(new))
402
        {
403
          rte_trace_in(D_FILTERS, p, new, "invalid");
404
          goto drop;
405
        }
406
      if (p->in_filter == FILTER_REJECT)
407
        {
408
          rte_trace_in(D_FILTERS, p, new, "filtered out");
409
          goto drop;
410
        }
411
      if (p->make_tmp_attrs)
412
        tmpa = p->make_tmp_attrs(new, rte_update_pool);
413
      if (p->in_filter)
414
        {
415
          ea_list *old_tmpa = tmpa;
416
          int fr = f_run(p->in_filter, &new, &tmpa, rte_update_pool, 0);
417
          if (fr > F_ACCEPT)
418
            {
419
              rte_trace_in(D_FILTERS, p, new, "filtered out");
420
              goto drop;
421
            }
422
          if (tmpa != old_tmpa && p->store_tmp_attrs)
423
            p->store_tmp_attrs(new, tmpa);
424
        }
425
      if (!(new->attrs->aflags & RTAF_CACHED)) /* Need to copy attributes */
426
        new->attrs = rta_lookup(new->attrs);
427
      new->flags |= REF_COW;
428
    }
429
  rte_recalculate(table, net, p, new, tmpa);
430
  rte_update_unlock();
431
  return;
432

    
433
drop:
434
  rte_free(new);
435
  rte_update_unlock();
436
}
437

    
438
void
439
rte_discard(rtable *t, rte *old)        /* Non-filtered route deletion, used during garbage collection */
440
{
441
  net *n = old->net;
442
  struct proto *p = old->attrs->proto;
443

    
444
  rte_update_lock();
445
  rte_recalculate(t, n, p, NULL, NULL);
446
  rte_update_unlock();
447
}
448

    
449
void
450
rte_dump(rte *e)
451
{
452
  net *n = e->net;
453
  if (n)
454
    debug("%1I/%2d ", n->n.prefix, n->n.pxlen);
455
  else
456
    debug("??? ");
457
  debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
458
  rta_dump(e->attrs);
459
  if (e->attrs->proto->proto->dump_attrs)
460
    e->attrs->proto->proto->dump_attrs(e);
461
  debug("\n");
462
}
463

    
464
void
465
rt_dump(rtable *t)
466
{
467
  rte *e;
468
  net *n;
469
  struct announce_hook *a;
470

    
471
  debug("Dump of routing table <%s>\n", t->name);
472
#ifdef DEBUGGING
473
  fib_check(&t->fib);
474
#endif
475
  FIB_WALK(&t->fib, fn)
476
    {
477
      n = (net *) fn;
478
      for(e=n->routes; e; e=e->next)
479
        rte_dump(e);
480
    }
481
  FIB_WALK_END;
482
  WALK_LIST(a, t->hooks)
483
    debug("\tAnnounces routes to protocol %s\n", a->proto->name);
484
  debug("\n");
485
}
486

    
487
void
488
rt_dump_all(void)
489
{
490
  rtable *t;
491

    
492
  WALK_LIST(t, routing_tables)
493
    rt_dump(t);
494
}
495

    
496
static void
497
rt_gc(void *tab)
498
{
499
  rtable *t = tab;
500

    
501
  DBG("Entered routing table garbage collector for %s after %d seconds and %d deletes\n",
502
      t->name, (int)(now - t->gc_time), t->gc_counter);
503
  rt_prune(t);
504
}
505

    
506
void
507
rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf)
508
{
509
  bzero(t, sizeof(*t));
510
  fib_init(&t->fib, p, sizeof(net), 0, rte_init);
511
  t->name = name;
512
  t->config = cf;
513
  init_list(&t->hooks);
514
  if (cf)
515
    {
516
      t->gc_event = ev_new(p);
517
      t->gc_event->hook = rt_gc;
518
      t->gc_event->data = t;
519
    }
520
}
521

    
522
void
523
rt_init(void)
524
{
525
  rta_init();
526
  rt_table_pool = rp_new(&root_pool, "Routing tables");
527
  rte_update_pool = lp_new(rt_table_pool, 4080);
528
  rte_slab = sl_new(rt_table_pool, sizeof(rte));
529
  init_list(&routing_tables);
530
}
531

    
532
void
533
rt_prune(rtable *tab)
534
{
535
  struct fib_iterator fit;
536
  int rcnt = 0, rdel = 0, ncnt = 0, ndel = 0;
537

    
538
  DBG("Pruning route table %s\n", tab->name);
539
  FIB_ITERATE_INIT(&fit, &tab->fib);
540
again:
541
  FIB_ITERATE_START(&tab->fib, &fit, f)
542
    {
543
      net *n = (net *) f;
544
      rte *e;
545
      ncnt++;
546
    rescan:
547
      for (e=n->routes; e; e=e->next, rcnt++)
548
        if (e->attrs->proto->core_state != FS_HAPPY)
549
          {
550
            rte_discard(tab, e);
551
            rdel++;
552
            goto rescan;
553
          }
554
      if (!n->routes)                /* Orphaned FIB entry? */
555
        {
556
          FIB_ITERATE_PUT(&fit, f);
557
          fib_delete(&tab->fib, f);
558
          ndel++;
559
          goto again;
560
        }
561
    }
562
  FIB_ITERATE_END(f);
563
  DBG("Pruned %d of %d routes and %d of %d networks\n", rcnt, rdel, ncnt, ndel);
564
  tab->gc_counter = 0;
565
  tab->gc_time = now;
566
}
567

    
568
void
569
rt_prune_all(void)
570
{
571
  rtable *t;
572

    
573
  WALK_LIST(t, routing_tables)
574
    rt_prune(t);
575
}
576

    
577
struct rtable_config *
578
rt_new_table(struct symbol *s)
579
{
580
  struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
581

    
582
  cf_define_symbol(s, SYM_TABLE, c);
583
  c->name = s->name;
584
  add_tail(&new_config->tables, &c->n);
585
  c->gc_max_ops = 100;
586
  c->gc_min_time = 5;
587
  return c;
588
}
589

    
590
void
591
rt_preconfig(struct config *c)
592
{
593
  struct symbol *s = cf_find_symbol("master");
594

    
595
  init_list(&c->tables);
596
  c->master_rtc = rt_new_table(s);
597
}
598

    
599
void
600
rt_lock_table(rtable *r)
601
{
602
  r->use_count++;
603
}
604

    
605
void
606
rt_unlock_table(rtable *r)
607
{
608
  if (!--r->use_count && r->deleted)
609
    {
610
      struct config *conf = r->deleted;
611
      DBG("Deleting routing table %s\n", r->name);
612
      rem_node(&r->n);
613
      fib_free(&r->fib);
614
      mb_free(r);
615
      config_del_obstacle(conf);
616
    }
617
}
618

    
619
void
620
rt_commit(struct config *new, struct config *old)
621
{
622
  struct rtable_config *o, *r;
623

    
624
  DBG("rt_commit:\n");
625
  if (old)
626
    {
627
      WALK_LIST(o, old->tables)
628
        {
629
          rtable *ot = o->table;
630
          if (!ot->deleted)
631
            {
632
              struct symbol *sym = cf_find_symbol(o->name);
633
              if (sym && sym->class == SYM_TABLE && !new->shutdown)
634
                {
635
                  DBG("\t%s: same\n", o->name);
636
                  r = sym->def;
637
                  r->table = ot;
638
                  ot->name = r->name;
639
                  ot->config = r;
640
                }
641
              else
642
                {
643
                  DBG("\t%s: deleted\n", o->name);
644
                  ot->deleted = old;
645
                  config_add_obstacle(old);
646
                  rt_lock_table(ot);
647
                  rt_unlock_table(ot);
648
                }
649
            }
650
        }
651
    }
652

    
653
  WALK_LIST(r, new->tables)
654
    if (!r->table)
655
      {
656
        rtable *t = mb_alloc(rt_table_pool, sizeof(struct rtable));
657
        DBG("\t%s: created\n", r->name);
658
        rt_setup(rt_table_pool, t, r->name, r);
659
        add_tail(&routing_tables, &t->n);
660
        r->table = t;
661
      }
662
  DBG("\tdone\n");
663
}
664

    
665
/*
666
 *  CLI commands
667
 */
668

    
669
static void
670
rt_format_via(rte *e, byte *via)
671
{
672
  rta *a = e->attrs;
673

    
674
  switch (a->dest)
675
    {
676
    case RTD_ROUTER:        bsprintf(via, "via %I on %s", a->gw, a->iface->name); break;
677
    case RTD_DEVICE:        bsprintf(via, "dev %s", a->iface->name); break;
678
    case RTD_BLACKHOLE:        bsprintf(via, "blackhole"); break;
679
    case RTD_UNREACHABLE:        bsprintf(via, "unreachable"); break;
680
    case RTD_PROHIBIT:        bsprintf(via, "prohibited"); break;
681
    default:                bsprintf(via, "???");
682
    }
683
}
684

    
685
static void
686
rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
687
{
688
  byte via[STD_ADDRESS_P_LENGTH+32], from[STD_ADDRESS_P_LENGTH+6];
689
  byte tm[TM_RELTIME_BUFFER_SIZE], info[256];
690
  rta *a = e->attrs;
691

    
692
  rt_format_via(e, via);
693
  tm_format_reltime(tm, e->lastmod);
694
  if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->gw))
695
    bsprintf(from, " from %I", a->from);
696
  else
697
    from[0] = 0;
698
  if (a->proto->proto->get_route_info || d->verbose)
699
    {
700
      /* Need to normalize the extended attributes */
701
      ea_list *t = tmpa;
702
      t = ea_append(t, a->eattrs);
703
      tmpa = alloca(ea_scan(t));
704
      ea_merge(t, tmpa);
705
    }
706
  if (a->proto->proto->get_route_info)
707
    a->proto->proto->get_route_info(e, info, tmpa);
708
  else
709
    bsprintf(info, " (%d)", e->pref);
710
  cli_printf(c, -1007, "%-18s %s [%s %s%s]%s", ia, via, a->proto->name, tm, from, info);
711
  if (d->verbose)
712
    rta_show(c, a, tmpa);
713
}
714

    
715
static void
716
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
717
{
718
  rte *e, *ee;
719
  byte ia[STD_ADDRESS_P_LENGTH+8];
720
  int ok;
721

    
722
  bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen);
723
  for(e=n->routes; e; e=e->next)
724
    {
725
      struct ea_list *tmpa, *old_tmpa;
726
      struct proto *p0 = e->attrs->proto;
727
      struct proto *p1 = d->import_protocol;
728
      ee = e;
729
      rte_update_lock();                /* We use the update buffer for filtering */
730
      old_tmpa = tmpa = p0->make_tmp_attrs ? p0->make_tmp_attrs(e, rte_update_pool) : NULL;
731
      ok = (d->filter == FILTER_ACCEPT || f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
732
      if (ok && d->import_mode)
733
        {
734
          int ic = (p1->import_control ? p1->import_control(p1, &e, &tmpa, rte_update_pool) : 0);
735
          if (ic < 0)
736
            ok = 0;
737
          else if (!ic && d->import_mode > 1)
738
            {
739
              if (p1->out_filter == FILTER_REJECT ||
740
                  p1->out_filter && f_run(p1->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
741
                ok = 0;
742
            }
743
        }
744
      if (ok)
745
        {
746
          rt_show_rte(c, ia, e, d, tmpa);
747
          ia[0] = 0;
748
        }
749
      if (e != ee)
750
        rte_free(ee);
751
      rte_update_unlock();
752
      if (d->import_mode)                /* In import mode, accept only the primary route */
753
        break;
754
    }
755
}
756

    
757
static void
758
rt_show_cont(struct cli *c)
759
{
760
  struct rt_show_data *d = c->rover;
761
#ifdef DEBUGGING
762
  unsigned max = 4;
763
#else
764
  unsigned max = 64;
765
#endif
766
  struct fib *fib = &d->table->fib;
767
  struct fib_iterator *it = &d->fit;
768

    
769
  FIB_ITERATE_START(fib, it, f)
770
    {
771
      net *n = (net *) f;
772
      if (d->running_on_config && d->running_on_config != config)
773
        {
774
          cli_printf(c, 8004, "Stopped due to reconfiguration");
775
          goto done;
776
        }
777
      if (d->import_protocol &&
778
          d->import_protocol->core_state != FS_HAPPY &&
779
          d->import_protocol->core_state != FS_FEEDING)
780
        {
781
          cli_printf(c, 8005, "Protocol is down");
782
          goto done;
783
        }
784
      if (!max--)
785
        {
786
          FIB_ITERATE_PUT(it, f);
787
          return;
788
        }
789
      rt_show_net(c, n, d);
790
    }
791
  FIB_ITERATE_END(f);
792
  cli_printf(c, 0, "");
793
done:
794
  c->cont = c->cleanup = NULL;
795
}
796

    
797
static void
798
rt_show_cleanup(struct cli *c)
799
{
800
  struct rt_show_data *d = c->rover;
801

    
802
  /* Unlink the iterator */
803
  fit_get(&d->table->fib, &d->fit);
804
}
805

    
806
void
807
rt_show(struct rt_show_data *d)
808
{
809
  net *n;
810

    
811
  if (d->pxlen == 256)
812
    {
813
      FIB_ITERATE_INIT(&d->fit, &d->table->fib);
814
      this_cli->cont = rt_show_cont;
815
      this_cli->cleanup = rt_show_cleanup;
816
      this_cli->rover = d;
817
    }
818
  else
819
    {
820
      n = fib_find(&d->table->fib, &d->prefix, d->pxlen);
821
      if (n)
822
        {
823
          rt_show_net(this_cli, n, d);
824
          cli_msg(0, "");
825
        }
826
      else
827
        cli_msg(8001, "Network not in table");
828
    }
829
}