Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (18.1 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
              if (p->core_state != FS_FEEDING)
213
                return;  /* In the meantime, the protocol fell down. */
214
            }
215
        }
216
      FIB_WALK_END;
217
    }
218
}
219

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

    
226
  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
  if (n->n.pxlen)
233
    {
234
      c = ipa_classify(n->n.prefix);
235
      if (c < 0 || !(c & IADDR_HOST))
236
        {
237
          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
          return 0;
250
        }
251
      if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope)
252
        {
253
          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
        }
258
    }
259
  return 1;
260
}
261

    
262
void
263
rte_free(rte *e)
264
{
265
  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
  rta_free(e->attrs);
274
  sl_free(rte_slab, e);
275
}
276

    
277
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
static void
289
rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa)
290
{
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
          if (new && rte_same(old, new))
301
            {
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
          *k = old->next;
309
          break;
310
        }
311
      k = &old->next;
312
    }
313

    
314
  if (new && rte_better(new, old_best))        /* It's a new optimal route => announce and relink it */
315
    {
316
      rte_trace_in(D_ROUTES, p, new, "added [best]");
317
      rte_announce(table, net, new, old_best, tmpa);
318
      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
          rte_announce(table, net, r, old_best, tmpa);
330
          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
                  k = &s->next;
341
                }
342
              r->next = net->routes;
343
              net->routes = r;
344
              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
            }
349
        }
350
      if (new)                                /* Link in the new non-optimal route */
351
        {
352
          new->next = old_best->next;
353
          old_best->next = new;
354
          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
        }
365
    }
366
  if (old)
367
    {
368
      if (p->rte_remove)
369
        p->rte_remove(net, old);
370
      rte_free_quick(old);
371
    }
372
  if (new)
373
    {
374
      new->lastmod = now;
375
      if (p->rte_insert)
376
        p->rte_insert(net, new);
377
    }
378
}
379

    
380
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
rte_update(rtable *table, net *net, struct proto *p, rte *new)
397
{
398
  ea_list *tmpa = NULL;
399

    
400
  rte_update_lock();
401
  if (new)
402
    {
403
      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
      if (p->make_tmp_attrs)
414
        tmpa = p->make_tmp_attrs(new, rte_update_pool);
415
      if (p->in_filter)
416
        {
417
          ea_list *old_tmpa = tmpa;
418
          int fr = f_run(p->in_filter, &new, &tmpa, rte_update_pool, 0);
419
          if (fr > F_ACCEPT)
420
            {
421
              rte_trace_in(D_FILTERS, p, new, "filtered out");
422
              goto drop;
423
            }
424
          if (tmpa != old_tmpa && p->store_tmp_attrs)
425
            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
  rte_recalculate(table, net, p, new, tmpa);
432
  rte_update_unlock();
433
  return;
434

    
435
drop:
436
  rte_free(new);
437
  rte_update_unlock();
438
}
439

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

    
446
  rte_update_lock();
447
  rte_recalculate(t, n, p, NULL, NULL);
448
  rte_update_unlock();
449
}
450

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

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

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

    
489
void
490
rt_dump_all(void)
491
{
492
  rtable *t;
493

    
494
  WALK_LIST(t, routing_tables)
495
    rt_dump(t);
496
}
497

    
498
static void
499
rt_gc(void *tab)
500
{
501
  rtable *t = tab;
502

    
503
  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
}
507

    
508
void
509
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
rt_init(void)
526
{
527
  rta_init();
528
  rt_table_pool = rp_new(&root_pool, "Routing tables");
529
  rte_update_pool = lp_new(rt_table_pool, 4080);
530
  rte_slab = sl_new(rt_table_pool, sizeof(rte));
531
  init_list(&routing_tables);
532
}
533

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

    
540
  DBG("Pruning route table %s\n", tab->name);
541
  FIB_ITERATE_INIT(&fit, &tab->fib);
542
again:
543
  FIB_ITERATE_START(&tab->fib, &fit, f)
544
    {
545
      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
            rte_discard(tab, e);
553
            rdel++;
554
            goto rescan;
555
          }
556
      if (!n->routes)                /* Orphaned FIB entry? */
557
        {
558
          FIB_ITERATE_PUT(&fit, f);
559
          fib_delete(&tab->fib, f);
560
          ndel++;
561
          goto again;
562
        }
563
    }
564
  FIB_ITERATE_END(f);
565
  DBG("Pruned %d of %d routes and %d of %d networks\n", rcnt, rdel, ncnt, ndel);
566
  tab->gc_counter = 0;
567
  tab->gc_time = now;
568
}
569

    
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
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
void
593
rt_preconfig(struct config *c)
594
{
595
  struct symbol *s = cf_find_symbol("master");
596

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

    
601
void
602
rt_lock_table(rtable *r)
603
{
604
  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

    
626
  DBG("rt_commit:\n");
627
  if (old)
628
    {
629
      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
              if (sym && sym->class == SYM_TABLE && !new->shutdown)
636
                {
637
                  DBG("\t%s: same\n", o->name);
638
                  r = sym->def;
639
                  r->table = ot;
640
                  ot->name = r->name;
641
                  ot->config = r;
642
                }
643
              else
644
                {
645
                  DBG("\t%s: deleted\n", o->name);
646
                  ot->deleted = old;
647
                  config_add_obstacle(old);
648
                  rt_lock_table(ot);
649
                  rt_unlock_table(ot);
650
                }
651
            }
652
        }
653
    }
654

    
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
        rt_setup(rt_table_pool, t, r->name, r);
661
        add_tail(&routing_tables, &t->n);
662
        r->table = t;
663
      }
664
  DBG("\tdone\n");
665
}
666

    
667
/*
668
 *  CLI commands
669
 */
670

    
671
static void
672
rt_format_via(rte *e, byte *via)
673
{
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
}
686

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

    
694
  rt_format_via(e, via);
695
  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
  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
  if (a->proto->proto->get_route_info)
709
    a->proto->proto->get_route_info(e, info, tmpa);
710
  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
    rta_show(c, a, tmpa);
715
}
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
  int ok;
723

    
724
  bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen);
725
  if (n->routes)
726
    d->net_counter++;
727
  for(e=n->routes; e; e=e->next)
728
    {
729
      struct ea_list *tmpa, *old_tmpa;
730
      struct proto *p0 = e->attrs->proto;
731
      struct proto *p1 = d->import_protocol;
732
      d->rt_counter++;
733
      ee = e;
734
      rte_update_lock();                /* We use the update buffer for filtering */
735
      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
        {
751
          d->show_counter++;
752
          rt_show_rte(c, ia, e, d, tmpa);
753
          ia[0] = 0;
754
        }
755
      if (e != ee)
756
        rte_free(ee);
757
      rte_update_unlock();
758
      if (d->primary_only)
759
        break;
760
    }
761
}
762

    
763
static void
764
rt_show_cont(struct cli *c)
765
{
766
  struct rt_show_data *d = c->rover;
767
#ifdef DEBUGGING
768
  unsigned max = 4;
769
#else
770
  unsigned max = 64;
771
#endif
772
  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
      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
      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
  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
done:
803
  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
}