Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-table.c @ 4d176e14

History | View | Annotate | Download (28.2 KB)

1
/*
2
 *        BIRD -- Routing Tables
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
/**
10
 * DOC: Routing tables
11
 *
12
 * Routing tables are probably the most important structures BIRD uses. They
13
 * hold all the information about known networks, the associated routes and
14
 * their attributes.
15
 *
16
 * There are multiple routing tables (a primary one together with any
17
 * number of secondary ones if requested by the configuration). Each table
18
 * is basically a FIB containing entries describing the individual
19
 * destination networks. For each network (represented by structure &net),
20
 * there is a one-way linked list of route entries (&rte), the first entry
21
 * on the list being the best one (i.e., the one we currently use
22
 * for routing), the order of the other ones is undetermined.
23
 *
24
 * The &rte contains information specific to the route (preference, protocol
25
 * metrics, time of last modification etc.) and a pointer to a &rta structure
26
 * (see the route attribute module for a precise explanation) holding the
27
 * remaining route attributes which are expected to be shared by multiple
28
 * routes in order to conserve memory.
29
 */
30

    
31
#undef LOCAL_DEBUG
32

    
33
#include "nest/bird.h"
34
#include "nest/route.h"
35
#include "nest/protocol.h"
36
#include "nest/cli.h"
37
#include "nest/iface.h"
38
#include "lib/resource.h"
39
#include "lib/event.h"
40
#include "lib/string.h"
41
#include "conf/conf.h"
42
#include "filter/filter.h"
43
#include "lib/string.h"
44
#include "lib/alloca.h"
45

    
46
static slab *rte_slab;
47
static linpool *rte_update_pool;
48

    
49
static pool *rt_table_pool;
50
static list routing_tables;
51

    
52
static void rt_format_via(rte *e, byte *via);
53

    
54
static void
55
rte_init(struct fib_node *N)
56
{
57
  net *n = (net *) N;
58

    
59
  N->flags = 0;
60
  n->routes = NULL;
61
}
62

    
63
/**
64
 * rte_find - find a route
65
 * @net: network node
66
 * @p: protocol
67
 *
68
 * The rte_find() function returns a route for destination @net
69
 * which belongs has been defined by protocol @p.
70
 */
71
rte *
72
rte_find(net *net, struct proto *p)
73
{
74
  rte *e = net->routes;
75

    
76
  while (e && e->attrs->proto != p)
77
    e = e->next;
78
  return e;
79
}
80

    
81
/**
82
 * rte_get_temp - get a temporary &rte
83
 * @a: attributes to assign to the new route (a &rta; in case it's
84
 * un-cached, rte_update() will create a cached copy automatically)
85
 *
86
 * Create a temporary &rte and bind it with the attributes @a.
87
 * Also set route preference to the default preference set for
88
 * the protocol.
89
 */
90
rte *
91
rte_get_temp(rta *a)
92
{
93
  rte *e = sl_alloc(rte_slab);
94

    
95
  e->attrs = a;
96
  e->flags = 0;
97
  e->pref = a->proto->preference;
98
  return e;
99
}
100

    
101
rte *
102
rte_do_cow(rte *r)
103
{
104
  rte *e = sl_alloc(rte_slab);
105

    
106
  memcpy(e, r, sizeof(rte));
107
  e->attrs = rta_clone(r->attrs);
108
  e->flags = 0;
109
  return e;
110
}
111

    
112
static int                                /* Actually better or at least as good as */
113
rte_better(rte *new, rte *old)
114
{
115
  int (*better)(rte *, rte *);
116

    
117
  if (!old)
118
    return 1;
119
  if (new->pref > old->pref)
120
    return 1;
121
  if (new->pref < old->pref)
122
    return 0;
123
  if (new->attrs->proto->proto != old->attrs->proto->proto)
124
    {
125
      /*
126
       *  If the user has configured protocol preferences, so that two different protocols
127
       *  have the same preference, try to break the tie by comparing addresses. Not too
128
       *  useful, but keeps the ordering of routes unambiguous.
129
       */
130
      return new->attrs->proto->proto > old->attrs->proto->proto;
131
    }
132
  if (better = new->attrs->proto->rte_better)
133
    return better(new, old);
134
  return 0;
135
}
136

    
137
static void
138
rte_trace(struct proto *p, rte *e, int dir, char *msg)
139
{
140
  byte via[STD_ADDRESS_P_LENGTH+32];
141

    
142
  rt_format_via(e, via);
143
  log(L_TRACE "%s %c %s %I/%d %s", p->name, dir, msg, e->net->n.prefix, e->net->n.pxlen, via);
144
}
145

    
146
static inline void
147
rte_trace_in(unsigned int flag, struct proto *p, rte *e, char *msg)
148
{
149
  if (p->debug & flag)
150
    rte_trace(p, e, '>', msg);
151
}
152

    
153
static inline void
154
rte_trace_out(unsigned int flag, struct proto *p, rte *e, char *msg)
155
{
156
  if (p->debug & flag)
157
    rte_trace(p, e, '<', msg);
158
}
159

    
160
static inline void
161
do_rte_announce(struct announce_hook *a, net *net, rte *new, rte *old, ea_list *tmpa, int class)
162
{
163
  struct proto *p = a->proto;
164
  rte *new0 = new;
165
  rte *old0 = old;
166
  int ok;
167

    
168
  if (new)
169
    {
170
      char *drop_reason = NULL;
171
      if ((class & IADDR_SCOPE_MASK) < p->min_scope)
172
        drop_reason = "out of scope";
173
      else if ((ok = p->import_control ? p->import_control(p, &new, &tmpa, rte_update_pool) : 0) < 0)
174
        drop_reason = "rejected by protocol";
175
      else if (ok)
176
        rte_trace_out(D_FILTERS, p, new, "forced accept by protocol");
177
      else if (p->out_filter == FILTER_REJECT ||
178
               p->out_filter && f_run(p->out_filter, &new, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
179
        drop_reason = "filtered out";
180
      if (drop_reason)
181
        {
182
          rte_trace_out(D_FILTERS, p, new, drop_reason);
183
          if (new != new0)
184
            rte_free(new);
185
          new = NULL;
186
        }
187
    }
188
  if (old && p->out_filter)
189
    {
190
      if (p->out_filter == FILTER_REJECT)
191
        old = NULL;
192
      else
193
        {
194
          ea_list *tmpb = p->make_tmp_attrs ? p->make_tmp_attrs(old, rte_update_pool) : NULL;
195
          ok = p->import_control ? p->import_control(p, &old, &tmpb, rte_update_pool) : 0;
196
          if (ok < 0 || (!ok && f_run(p->out_filter, &old, &tmpb, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT))
197
            {
198
              if (old != old0)
199
                rte_free(old);
200
              old = NULL;
201
            }
202
        }
203
    }
204
  if (p->debug & D_ROUTES)
205
    {
206
      if (new && old)
207
        rte_trace_out(D_ROUTES, p, new, "replaced");
208
      else if (new)
209
        rte_trace_out(D_ROUTES, p, new, "added");
210
      else if (old)
211
        rte_trace_out(D_ROUTES, p, old, "removed");
212
    }
213
  if (!new && !old)
214
    return;
215
  if (!new)
216
    p->rt_notify(p, net, NULL, old, NULL);
217
  else if (tmpa)
218
    {
219
      ea_list *t = tmpa;
220
      while (t->next)
221
        t = t->next;
222
      t->next = new->attrs->eattrs;
223
      p->rt_notify(p, net, new, old, tmpa);
224
      t->next = NULL;
225
    }
226
  else
227
    p->rt_notify(p, net, new, old, new->attrs->eattrs);
228
  if (new && new != new0)        /* Discard temporary rte's */
229
    rte_free(new);
230
  if (old && old != old0)
231
    rte_free(old);
232
}
233

    
234
/**
235
 * rte_announce - announce a routing table change
236
 * @tab: table the route has been added to
237
 * @net: network in question
238
 * @new: the new route to be announced
239
 * @old: previous optimal route for the same network
240
 * @tmpa: a list of temporary attributes belonging to the new route
241
 *
242
 * This function gets a routing table update and announces it
243
 * to all protocols connected to the same table by their announcement hooks.
244
 *
245
 * For each such protocol, we first call its import_control() hook which
246
 * performs basic checks on the route (each protocol has a right to veto
247
 * or force accept of the route before any filter is asked) and adds default
248
 * values of attributes specific to the new protocol (metrics, tags etc.).
249
 * Then it consults the protocol's export filter and if it accepts the
250
 * route, the rt_notify() hook of the protocol gets called.
251
 */
252
static void
253
rte_announce(rtable *tab, net *net, rte *new, rte *old, ea_list *tmpa)
254
{
255
  struct announce_hook *a;
256
  int class = ipa_classify(net->n.prefix);
257

    
258
  WALK_LIST(a, tab->hooks)
259
    {
260
      ASSERT(a->proto->core_state == FS_HAPPY || a->proto->core_state == FS_FEEDING);
261
      do_rte_announce(a, net, new, old, tmpa, class);
262
    }
263
}
264

    
265
static inline int
266
rte_validate(rte *e)
267
{
268
  int c;
269
  net *n = e->net;
270

    
271
  if (ipa_nonzero(ipa_and(n->n.prefix, ipa_not(ipa_mkmask(n->n.pxlen)))))
272
    {
273
      log(L_BUG "Ignoring bogus prefix %I/%d received via %s",
274
          n->n.prefix, n->n.pxlen, e->attrs->proto->name);
275
      return 0;
276
    }
277
  if (n->n.pxlen)
278
    {
279
      c = ipa_classify(n->n.prefix);
280
      if (c < 0 || !(c & IADDR_HOST))
281
        {
282
          if (!ipa_nonzero(n->n.prefix))
283
            {
284
              /* Various default routes */
285
#ifdef IPV6
286
              if (n->n.pxlen == 96)
287
#else
288
              if (n->n.pxlen <= 1)
289
#endif
290
                return 1;
291
            }
292
          log(L_WARN "Ignoring bogus route %I/%d received via %s",
293
              n->n.prefix, n->n.pxlen, e->attrs->proto->name);
294
          return 0;
295
        }
296
      if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope)
297
        {
298
          log(L_WARN "Ignoring %s scope route %I/%d received from %I via %s",
299
              ip_scope_text(c & IADDR_SCOPE_MASK),
300
              n->n.prefix, n->n.pxlen, e->attrs->from, e->attrs->proto->name);
301
          return 0;
302
        }
303
    }
304
  return 1;
305
}
306

    
307
/**
308
 * rte_free - delete a &rte
309
 * @e: &rte to be deleted
310
 *
311
 * rte_free() deletes the given &rte from the routing table it's linked to.
312
 */
313
void
314
rte_free(rte *e)
315
{
316
  if (e->attrs->aflags & RTAF_CACHED)
317
    rta_free(e->attrs);
318
  sl_free(rte_slab, e);
319
}
320

    
321
static inline void
322
rte_free_quick(rte *e)
323
{
324
  rta_free(e->attrs);
325
  sl_free(rte_slab, e);
326
}
327

    
328
static int
329
rte_same(rte *x, rte *y)
330
{
331
  return
332
    x->attrs == y->attrs &&
333
    x->flags == y->flags &&
334
    x->pflags == y->pflags &&
335
    x->pref == y->pref &&
336
    (!x->attrs->proto->rte_same || x->attrs->proto->rte_same(x, y));
337
}
338

    
339
static void
340
rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa)
341
{
342
  rte *old_best = net->routes;
343
  rte *old = NULL;
344
  rte **k, *r, *s;
345

    
346
  k = &net->routes;                        /* Find and remove original route from the same protocol */
347
  while (old = *k)
348
    {
349
      if (old->attrs->proto == p)
350
        {
351
          if (new && rte_same(old, new))
352
            {
353
              /* No changes, ignore the new route */
354
              rte_trace_in(D_ROUTES, p, new, "ignored");
355
              rte_free_quick(new);
356
              old->lastmod = now;
357
              return;
358
            }
359
          *k = old->next;
360
          break;
361
        }
362
      k = &old->next;
363
    }
364

    
365
  if (new && rte_better(new, old_best))        /* It's a new optimal route => announce and relink it */
366
    {
367
      rte_trace_in(D_ROUTES, p, new, "added [best]");
368
      rte_announce(table, net, new, old_best, tmpa);
369
      new->next = net->routes;
370
      net->routes = new;
371
    }
372
  else
373
    {
374
      if (old == old_best)                /* It has _replaced_ the old optimal route */
375
        {
376
          r = new;                        /* Find new optimal route and announce it */
377
          for(s=net->routes; s; s=s->next)
378
            if (rte_better(s, r))
379
              r = s;
380
          rte_announce(table, net, r, old_best, tmpa);
381
          if (r)                        /* Re-link the new optimal route */
382
            {
383
              k = &net->routes;
384
              while (s = *k)
385
                {
386
                  if (s == r)
387
                    {
388
                      *k = r->next;
389
                      break;
390
                    }
391
                  k = &s->next;
392
                }
393
              r->next = net->routes;
394
              net->routes = r;
395
            }
396
          else if (table->gc_counter++ >= table->config->gc_max_ops &&
397
                   table->gc_time + table->config->gc_min_time <= now)
398
            ev_schedule(table->gc_event);
399
        }
400
      if (new)                                /* Link in the new non-optimal route */
401
        {
402
          new->next = old_best->next;
403
          old_best->next = new;
404
          rte_trace_in(D_ROUTES, p, new, "added");
405
        }
406
      else if (old && (p->debug & D_ROUTES))
407
        {
408
          if (old != old_best)
409
            rte_trace_in(D_ROUTES, p, old, "removed");
410
          else if (net->routes)
411
            rte_trace_in(D_ROUTES, p, old, "removed [replaced]");
412
          else
413
            rte_trace_in(D_ROUTES, p, old, "removed [sole]");
414
        }
415
    }
416
  if (old)
417
    {
418
      if (p->rte_remove)
419
        p->rte_remove(net, old);
420
      rte_free_quick(old);
421
    }
422
  if (new)
423
    {
424
      new->lastmod = now;
425
      if (p->rte_insert)
426
        p->rte_insert(net, new);
427
    }
428
}
429

    
430
static int rte_update_nest_cnt;                /* Nesting counter to allow recursive updates */
431

    
432
static inline void
433
rte_update_lock(void)
434
{
435
  rte_update_nest_cnt++;
436
}
437

    
438
static inline void
439
rte_update_unlock(void)
440
{
441
  if (!--rte_update_nest_cnt)
442
    lp_flush(rte_update_pool);
443
}
444

    
445
/**
446
 * rte_update - enter a new update to a routing table
447
 * @table: table to be updated
448
 * @net: network node
449
 * @p: protocol submitting the update
450
 * @new: a &rte representing the new route or %NULL for route removal.
451
 *
452
 * This function is called by the routing protocols whenever they discover
453
 * a new route or wish to update/remove an existing route. The right announcement
454
 * sequence is to build route attributes first (either un-cached with @aflags set
455
 * to zero or a cached one using rta_lookup(); in this case please note that
456
 * you need to increase the use count of the attributes yourself by calling
457
 * rta_clone()), call rte_get_temp() to obtain a temporary &rte, fill in all
458
 * the appropriate data and finally submit the new &rte by calling rte_update().
459
 *
460
 * When rte_update() gets any route, it automatically validates it (checks,
461
 * whether the network and next hop address are valid IP addresses and also
462
 * whether a normal routing protocol doesn't try to smuggle a host or link
463
 * scope route to the table), converts all protocol dependent attributes stored
464
 * in the &rte to temporary extended attributes, consults import filters of the
465
 * protocol to see if the route should be accepted and/or its attributes modified,
466
 * stores the temporary attributes back to the &rte.
467
 *
468
 * Now, having a "public" version of the route, we
469
 * automatically find any old route defined by the protocol @p
470
 * for network @n, replace it by the new one (or removing it if @new is %NULL),
471
 * recalculate the optimal route for this destination and finally broadcast
472
 * the change (if any) to all routing protocols by calling rte_announce().
473
 *
474
 * All memory used for attribute lists and other temporary allocations is taken
475
 * from a special linear pool @rte_update_pool and freed when rte_update()
476
 * finishes.
477
 */
478
void
479
rte_update(rtable *table, net *net, struct proto *p, rte *new)
480
{
481
  ea_list *tmpa = NULL;
482

    
483
  rte_update_lock();
484
  if (new)
485
    {
486
      struct filter *filter = p->in_filter;
487

    
488
        /* Do not filter routes going to the secondary side of the pipe, 
489
           that should only go through export filter.
490
           FIXME Make a better check whether p is really a pipe. */
491
      if (p->table != table)
492
        filter = FILTER_ACCEPT;
493

    
494
      if (!rte_validate(new))
495
        {
496
          rte_trace_in(D_FILTERS, p, new, "invalid");
497
          goto drop;
498
        }
499
      if (filter == FILTER_REJECT)
500
        {
501
          rte_trace_in(D_FILTERS, p, new, "filtered out");
502
          goto drop;
503
        }
504
      if (p->make_tmp_attrs)
505
        tmpa = p->make_tmp_attrs(new, rte_update_pool);
506
      if (filter)
507
        {
508
          ea_list *old_tmpa = tmpa;
509
          int fr = f_run(filter, &new, &tmpa, rte_update_pool, 0);
510
          if (fr > F_ACCEPT)
511
            {
512
              rte_trace_in(D_FILTERS, p, new, "filtered out");
513
              goto drop;
514
            }
515
          if (tmpa != old_tmpa && p->store_tmp_attrs)
516
            p->store_tmp_attrs(new, tmpa);
517
        }
518
      if (!(new->attrs->aflags & RTAF_CACHED)) /* Need to copy attributes */
519
        new->attrs = rta_lookup(new->attrs);
520
      new->flags |= REF_COW;
521
    }
522
  rte_recalculate(table, net, p, new, tmpa);
523
  rte_update_unlock();
524
  return;
525

    
526
drop:
527
  rte_free(new);
528
  rte_update_unlock();
529
}
530

    
531
void
532
rte_discard(rtable *t, rte *old)        /* Non-filtered route deletion, used during garbage collection */
533
{
534
  net *n = old->net;
535
  struct proto *p = old->attrs->proto;
536

    
537
  rte_update_lock();
538
  rte_recalculate(t, n, p, NULL, NULL);
539
  rte_update_unlock();
540
}
541

    
542
/**
543
 * rte_dump - dump a route
544
 * @e: &rte to be dumped
545
 *
546
 * This functions dumps contents of a &rte to debug output.
547
 */
548
void
549
rte_dump(rte *e)
550
{
551
  net *n = e->net;
552
  if (n)
553
    debug("%-1I/%2d ", n->n.prefix, n->n.pxlen);
554
  else
555
    debug("??? ");
556
  debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
557
  rta_dump(e->attrs);
558
  if (e->attrs->proto->proto->dump_attrs)
559
    e->attrs->proto->proto->dump_attrs(e);
560
  debug("\n");
561
}
562

    
563
/**
564
 * rt_dump - dump a routing table
565
 * @t: routing table to be dumped
566
 *
567
 * This function dumps contents of a given routing table to debug output.
568
 */
569
void
570
rt_dump(rtable *t)
571
{
572
  rte *e;
573
  net *n;
574
  struct announce_hook *a;
575

    
576
  debug("Dump of routing table <%s>\n", t->name);
577
#ifdef DEBUGGING
578
  fib_check(&t->fib);
579
#endif
580
  FIB_WALK(&t->fib, fn)
581
    {
582
      n = (net *) fn;
583
      for(e=n->routes; e; e=e->next)
584
        rte_dump(e);
585
    }
586
  FIB_WALK_END;
587
  WALK_LIST(a, t->hooks)
588
    debug("\tAnnounces routes to protocol %s\n", a->proto->name);
589
  debug("\n");
590
}
591

    
592
/**
593
 * rt_dump_all - dump all routing tables
594
 *
595
 * This function dumps contents of all routing tables to debug output.
596
 */
597
void
598
rt_dump_all(void)
599
{
600
  rtable *t;
601

    
602
  WALK_LIST(t, routing_tables)
603
    rt_dump(t);
604
}
605

    
606
static void
607
rt_gc(void *tab)
608
{
609
  rtable *t = tab;
610

    
611
  DBG("Entered routing table garbage collector for %s after %d seconds and %d deletes\n",
612
      t->name, (int)(now - t->gc_time), t->gc_counter);
613
  rt_prune(t);
614
}
615

    
616
void
617
rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf)
618
{
619
  bzero(t, sizeof(*t));
620
  fib_init(&t->fib, p, sizeof(net), 0, rte_init);
621
  t->name = name;
622
  t->config = cf;
623
  init_list(&t->hooks);
624
  if (cf)
625
    {
626
      t->gc_event = ev_new(p);
627
      t->gc_event->hook = rt_gc;
628
      t->gc_event->data = t;
629
      t->gc_time = now;
630
    }
631
}
632

    
633
/**
634
 * rt_init - initialize routing tables
635
 *
636
 * This function is called during BIRD startup. It initializes the
637
 * routing table module.
638
 */
639
void
640
rt_init(void)
641
{
642
  rta_init();
643
  rt_table_pool = rp_new(&root_pool, "Routing tables");
644
  rte_update_pool = lp_new(rt_table_pool, 4080);
645
  rte_slab = sl_new(rt_table_pool, sizeof(rte));
646
  init_list(&routing_tables);
647
}
648

    
649
/**
650
 * rt_prune - prune a routing table
651
 * @tab: routing table to be pruned
652
 *
653
 * This function is called whenever a protocol shuts down. It scans
654
 * the routing table and removes all routes belonging to inactive
655
 * protocols and also stale network entries.
656
 */
657
void
658
rt_prune(rtable *tab)
659
{
660
  struct fib_iterator fit;
661
  int rcnt = 0, rdel = 0, ncnt = 0, ndel = 0;
662

    
663
  DBG("Pruning route table %s\n", tab->name);
664
#ifdef DEBUGGING
665
  fib_check(&tab->fib);
666
#endif
667
  FIB_ITERATE_INIT(&fit, &tab->fib);
668
again:
669
  FIB_ITERATE_START(&tab->fib, &fit, f)
670
    {
671
      net *n = (net *) f;
672
      rte *e;
673
      ncnt++;
674
    rescan:
675
      for (e=n->routes; e; e=e->next, rcnt++)
676
        if (e->attrs->proto->core_state != FS_HAPPY &&
677
            e->attrs->proto->core_state != FS_FEEDING)
678
          {
679
            rte_discard(tab, e);
680
            rdel++;
681
            goto rescan;
682
          }
683
      if (!n->routes)                /* Orphaned FIB entry? */
684
        {
685
          FIB_ITERATE_PUT(&fit, f);
686
          fib_delete(&tab->fib, f);
687
          ndel++;
688
          goto again;
689
        }
690
    }
691
  FIB_ITERATE_END(f);
692
  DBG("Pruned %d of %d routes and %d of %d networks\n", rdel, rcnt, ndel, ncnt);
693
#ifdef DEBUGGING
694
  fib_check(&tab->fib);
695
#endif
696
  tab->gc_counter = 0;
697
  tab->gc_time = now;
698
}
699

    
700
/**
701
 * rt_prune_all - prune all routing tables
702
 *
703
 * This function calls rt_prune() for all known routing tables.
704
 */
705
void
706
rt_prune_all(void)
707
{
708
  rtable *t;
709

    
710
  WALK_LIST(t, routing_tables)
711
    rt_prune(t);
712
}
713

    
714
struct rtable_config *
715
rt_new_table(struct symbol *s)
716
{
717
  struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
718

    
719
  cf_define_symbol(s, SYM_TABLE, c);
720
  c->name = s->name;
721
  add_tail(&new_config->tables, &c->n);
722
  c->gc_max_ops = 1000;
723
  c->gc_min_time = 5;
724
  return c;
725
}
726

    
727
void
728
rt_preconfig(struct config *c)
729
{
730
  struct symbol *s = cf_find_symbol("master");
731

    
732
  init_list(&c->tables);
733
  c->master_rtc = rt_new_table(s);
734
}
735

    
736
/**
737
 * rt_lock_table - lock a routing table
738
 * @r: routing table to be locked
739
 *
740
 * Lock a routing table, because it's in use by a protocol,
741
 * preventing it from being freed when it gets undefined in a new
742
 * configuration.
743
 */
744
void
745
rt_lock_table(rtable *r)
746
{
747
  r->use_count++;
748
}
749

    
750
/**
751
 * rt_unlock_table - unlock a routing table
752
 * @r: routing table to be unlocked
753
 *
754
 * Unlock a routing table formerly locked by rt_lock_table(),
755
 * that is decrease its use count and delete it if it's scheduled
756
 * for deletion by configuration changes.
757
 */
758
void
759
rt_unlock_table(rtable *r)
760
{
761
  if (!--r->use_count && r->deleted)
762
    {
763
      struct config *conf = r->deleted;
764
      DBG("Deleting routing table %s\n", r->name);
765
      rem_node(&r->n);
766
      fib_free(&r->fib);
767
      mb_free(r);
768
      config_del_obstacle(conf);
769
    }
770
}
771

    
772
/**
773
 * rt_commit - commit new routing table configuration
774
 * @new: new configuration
775
 * @old: original configuration or %NULL if it's boot time config
776
 *
777
 * Scan differences between @old and @new configuration and modify
778
 * the routing tables according to these changes. If @new defines a
779
 * previously unknown table, create it, if it omits a table existing
780
 * in @old, schedule it for deletion (it gets deleted when all protocols
781
 * disconnect from it by calling rt_unlock_table()), if it exists
782
 * in both configurations, leave it unchanged.
783
 */
784
void
785
rt_commit(struct config *new, struct config *old)
786
{
787
  struct rtable_config *o, *r;
788

    
789
  DBG("rt_commit:\n");
790
  if (old)
791
    {
792
      WALK_LIST(o, old->tables)
793
        {
794
          rtable *ot = o->table;
795
          if (!ot->deleted)
796
            {
797
              struct symbol *sym = cf_find_symbol(o->name);
798
              if (sym && sym->class == SYM_TABLE && !new->shutdown)
799
                {
800
                  DBG("\t%s: same\n", o->name);
801
                  r = sym->def;
802
                  r->table = ot;
803
                  ot->name = r->name;
804
                  ot->config = r;
805
                }
806
              else
807
                {
808
                  DBG("\t%s: deleted\n", o->name);
809
                  ot->deleted = old;
810
                  config_add_obstacle(old);
811
                  rt_lock_table(ot);
812
                  rt_unlock_table(ot);
813
                }
814
            }
815
        }
816
    }
817

    
818
  WALK_LIST(r, new->tables)
819
    if (!r->table)
820
      {
821
        rtable *t = mb_alloc(rt_table_pool, sizeof(struct rtable));
822
        DBG("\t%s: created\n", r->name);
823
        rt_setup(rt_table_pool, t, r->name, r);
824
        add_tail(&routing_tables, &t->n);
825
        r->table = t;
826
      }
827
  DBG("\tdone\n");
828
}
829

    
830
/**
831
 * rt_feed_baby - advertise routes to a new protocol
832
 * @p: protocol to be fed
833
 *
834
 * This function performs one pass of advertisement of routes to a newly
835
 * initialized protocol. It's called by the protocol code as long as it
836
 * has something to do. (We avoid transferring all the routes in single
837
 * pass in order not to monopolize CPU time.)
838
 */
839
int
840
rt_feed_baby(struct proto *p)
841
{
842
  struct announce_hook *h;
843
  struct fib_iterator *fit;
844
  int max_feed = 256;
845

    
846
  if (!p->feed_ahook)                        /* Need to initialize first */
847
    {
848
      if (!p->ahooks)
849
        return 1;
850
      DBG("Announcing routes to new protocol %s\n", p->name);
851
      p->feed_ahook = p->ahooks;
852
      fit = p->feed_iterator = mb_alloc(p->pool, sizeof(struct fib_iterator));
853
      goto next_hook;
854
    }
855
  fit = p->feed_iterator;
856

    
857
again:
858
  h = p->feed_ahook;
859
  FIB_ITERATE_START(&h->table->fib, fit, fn)
860
    {
861
      net *n = (net *) fn;
862
      rte *e = n->routes;
863
      if (max_feed <= 0)
864
        {
865
          FIB_ITERATE_PUT(fit, fn);
866
          return 0;
867
        }
868
      if (e)
869
        {
870
          struct proto *q = e->attrs->proto;
871
          ea_list *tmpa;
872

    
873
          if (p->core_state != FS_FEEDING)
874
            return 1;  /* In the meantime, the protocol fell down. */
875
          rte_update_lock();
876
          tmpa = q->make_tmp_attrs ? q->make_tmp_attrs(e, rte_update_pool) : NULL;
877
          do_rte_announce(h, n, e, NULL, tmpa, ipa_classify(n->n.prefix));
878
          rte_update_unlock();
879
          max_feed--;
880
        }
881
    }
882
  FIB_ITERATE_END(fn);
883
  p->feed_ahook = h->next;
884
  if (!p->feed_ahook)
885
    {
886
      mb_free(p->feed_iterator);
887
      p->feed_iterator = NULL;
888
      return 1;
889
    }
890

    
891
next_hook:
892
  h = p->feed_ahook;
893
  FIB_ITERATE_INIT(fit, &h->table->fib);
894
  goto again;
895
}
896

    
897
/**
898
 * rt_feed_baby_abort - abort protocol feeding
899
 * @p: protocol
900
 *
901
 * This function is called by the protocol code when the protocol
902
 * stops or ceases to exist before the last iteration of rt_feed_baby()
903
 * has finished.
904
 */
905
void
906
rt_feed_baby_abort(struct proto *p)
907
{
908
  if (p->feed_ahook)
909
    {
910
      /* Unlink the iterator and exit */
911
      fit_get(&p->feed_ahook->table->fib, p->feed_iterator);
912
      p->feed_ahook = NULL;
913
    }
914
}
915

    
916
/*
917
 *  CLI commands
918
 */
919

    
920
static void
921
rt_format_via(rte *e, byte *via)
922
{
923
  rta *a = e->attrs;
924

    
925
  switch (a->dest)
926
    {
927
    case RTD_ROUTER:        bsprintf(via, "via %I on %s", a->gw, a->iface->name); break;
928
    case RTD_DEVICE:        bsprintf(via, "dev %s", a->iface->name); break;
929
    case RTD_BLACKHOLE:        bsprintf(via, "blackhole"); break;
930
    case RTD_UNREACHABLE:        bsprintf(via, "unreachable"); break;
931
    case RTD_PROHIBIT:        bsprintf(via, "prohibited"); break;
932
    default:                bsprintf(via, "???");
933
    }
934
}
935

    
936
static void
937
rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
938
{
939
  byte via[STD_ADDRESS_P_LENGTH+32], from[STD_ADDRESS_P_LENGTH+6];
940
  byte tm[TM_RELTIME_BUFFER_SIZE], info[256];
941
  rta *a = e->attrs;
942

    
943
  rt_format_via(e, via);
944
  tm_format_reltime(tm, e->lastmod);
945
  if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->gw))
946
    bsprintf(from, " from %I", a->from);
947
  else
948
    from[0] = 0;
949
  if (a->proto->proto->get_route_info || d->verbose)
950
    {
951
      /* Need to normalize the extended attributes */
952
      ea_list *t = tmpa;
953
      t = ea_append(t, a->eattrs);
954
      tmpa = alloca(ea_scan(t));
955
      ea_merge(t, tmpa);
956
      ea_sort(tmpa);
957
    }
958
  if (a->proto->proto->get_route_info)
959
    a->proto->proto->get_route_info(e, info, tmpa);
960
  else
961
    bsprintf(info, " (%d)", e->pref);
962
  cli_printf(c, -1007, "%-18s %s [%s %s%s]%s", ia, via, a->proto->name, tm, from, info);
963
  if (d->verbose)
964
    rta_show(c, a, tmpa);
965
}
966

    
967
static void
968
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
969
{
970
  rte *e, *ee;
971
  byte ia[STD_ADDRESS_P_LENGTH+8];
972
  int ok;
973

    
974
  bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen);
975
  if (n->routes)
976
    d->net_counter++;
977
  for(e=n->routes; e; e=e->next)
978
    {
979
      struct ea_list *tmpa, *old_tmpa;
980
      struct proto *p0 = e->attrs->proto;
981
      struct proto *p1 = d->import_protocol;
982
      struct proto *p2 = d->show_protocol;
983
      d->rt_counter++;
984
      ee = e;
985
      rte_update_lock();                /* We use the update buffer for filtering */
986
      old_tmpa = tmpa = p0->make_tmp_attrs ? p0->make_tmp_attrs(e, rte_update_pool) : NULL;
987
      ok = (d->filter == FILTER_ACCEPT || f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
988
      if (p2 && p2 != p0) ok = 0;
989
      if (ok && d->import_mode)
990
        {
991
          int ic = (p1->import_control ? p1->import_control(p1, &e, &tmpa, rte_update_pool) : 0);
992
          if (ic < 0)
993
            ok = 0;
994
          else if (!ic && d->import_mode > 1)
995
            {
996
              if (p1->out_filter == FILTER_REJECT ||
997
                  p1->out_filter && f_run(p1->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
998
                ok = 0;
999
            }
1000
        }
1001
      if (ok)
1002
        {
1003
          d->show_counter++;
1004
          if (d->stats < 2)
1005
            rt_show_rte(c, ia, e, d, tmpa);
1006
          ia[0] = 0;
1007
        }
1008
      if (e != ee)
1009
        rte_free(ee);
1010
      rte_update_unlock();
1011
      if (d->primary_only)
1012
        break;
1013
    }
1014
}
1015

    
1016
static void
1017
rt_show_cont(struct cli *c)
1018
{
1019
  struct rt_show_data *d = c->rover;
1020
#ifdef DEBUGGING
1021
  unsigned max = 4;
1022
#else
1023
  unsigned max = 64;
1024
#endif
1025
  struct fib *fib = &d->table->fib;
1026
  struct fib_iterator *it = &d->fit;
1027

    
1028
  FIB_ITERATE_START(fib, it, f)
1029
    {
1030
      net *n = (net *) f;
1031
      if (d->running_on_config && d->running_on_config != config)
1032
        {
1033
          cli_printf(c, 8004, "Stopped due to reconfiguration");
1034
          goto done;
1035
        }
1036
      if (d->import_protocol &&
1037
          d->import_protocol->core_state != FS_HAPPY &&
1038
          d->import_protocol->core_state != FS_FEEDING)
1039
        {
1040
          cli_printf(c, 8005, "Protocol is down");
1041
          goto done;
1042
        }
1043
      if (!max--)
1044
        {
1045
          FIB_ITERATE_PUT(it, f);
1046
          return;
1047
        }
1048
      rt_show_net(c, n, d);
1049
    }
1050
  FIB_ITERATE_END(f);
1051
  if (d->stats)
1052
    cli_printf(c, 14, "%d of %d routes for %d networks", d->show_counter, d->rt_counter, d->net_counter);
1053
  else
1054
    cli_printf(c, 0, "");
1055
done:
1056
  c->cont = c->cleanup = NULL;
1057
}
1058

    
1059
static void
1060
rt_show_cleanup(struct cli *c)
1061
{
1062
  struct rt_show_data *d = c->rover;
1063

    
1064
  /* Unlink the iterator */
1065
  fit_get(&d->table->fib, &d->fit);
1066
}
1067

    
1068
void
1069
rt_show(struct rt_show_data *d)
1070
{
1071
  net *n;
1072

    
1073
  if (d->pxlen == 256)
1074
    {
1075
      FIB_ITERATE_INIT(&d->fit, &d->table->fib);
1076
      this_cli->cont = rt_show_cont;
1077
      this_cli->cleanup = rt_show_cleanup;
1078
      this_cli->rover = d;
1079
    }
1080
  else
1081
    {
1082
      if (d->show_for)
1083
        n = fib_route(&d->table->fib, d->prefix, d->pxlen);
1084
      else
1085
        n = fib_find(&d->table->fib, &d->prefix, d->pxlen);
1086
      if (n)
1087
        {
1088
          rt_show_net(this_cli, n, d);
1089
          cli_msg(0, "");
1090
        }
1091
      else
1092
        cli_msg(8001, "Network not in table");
1093
    }
1094
}
1095

    
1096
/*
1097
 *  Documentation for functions declared inline in route.h
1098
 */
1099
#if 0
1100

1101
/**
1102
 * net_find - find a network entry
1103
 * @tab: a routing table
1104
 * @addr: address of the network
1105
 * @len: length of the network prefix
1106
 *
1107
 * net_find() looks up the given network in routing table @tab and
1108
 * returns a pointer to its &net entry or %NULL if no such network
1109
 * exists.
1110
 */
1111
static inline net *net_find(rtable *tab, ip_addr addr, unsigned len)
1112
{ DUMMY; }
1113

1114
/**
1115
 * net_get - obtain a network entry
1116
 * @tab: a routing table
1117
 * @addr: address of the network
1118
 * @len: length of the network prefix
1119
 *
1120
 * net_get() looks up the given network in routing table @tab and
1121
 * returns a pointer to its &net entry. If no such entry exists, it's
1122
 * created.
1123
 */
1124
static inline net *net_get(rtable *tab, ip_addr addr, unsigned len)
1125
{ DUMMY; }
1126

1127
/**
1128
 * rte_cow - copy a route for writing
1129
 * @r: a route entry to be copied
1130
 *
1131
 * rte_cow() takes a &rte and prepares it for modification. The exact action
1132
 * taken depends on the flags of the &rte -- if it's a temporary entry, it's
1133
 * just returned unchanged, else a new temporary entry with the same contents
1134
 * is created.
1135
 *
1136
 * The primary use of this function is inside the filter machinery -- when
1137
 * a filter wants to modify &rte contents (to change the preference or to
1138
 * attach another set of attributes), it must ensure that the &rte is not
1139
 * shared with anyone else (and especially that it isn't stored in any routing
1140
 * table).
1141
 *
1142
 * Result: a pointer to the new writable &rte.
1143
 */
1144
static inline rte * rte_cow(rte *r)
1145
{ DUMMY; }
1146

1147
#endif