Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / krt.c @ f4a60a9b

History | View | Annotate | Download (27.1 KB)

1
/*
2
 *        BIRD -- UNIX Kernel Synchronization
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: Kernel synchronization
11
 *
12
 * This system dependent module implements the Kernel and Device protocol,
13
 * that is synchronization of interface lists and routing tables with the
14
 * OS kernel.
15
 *
16
 * The whole kernel synchronization is a bit messy and touches some internals
17
 * of the routing table engine, because routing table maintenance is a typical
18
 * example of the proverbial compatibility between different Unices and we want
19
 * to keep the overhead of our KRT business as low as possible and avoid maintaining
20
 * a local routing table copy.
21
 *
22
 * The kernel syncer can work in three different modes (according to system config header):
23
 * Either with a single routing table and single KRT protocol [traditional UNIX]
24
 * or with many routing tables and separate KRT protocols for all of them
25
 * or with many routing tables, but every scan including all tables, so we start
26
 * separate KRT protocols which cooperate with each other [Linux].
27
 * In this case, we keep only a single scan timer.
28
 *
29
 * We use FIB node flags in the routing table to keep track of route
30
 * synchronization status. We also attach temporary &rte's to the routing table,
31
 * but it cannot do any harm to the rest of BIRD since table synchronization is
32
 * an atomic process.
33
 *
34
 * When starting up, we cheat by looking if there is another
35
 * KRT instance to be initialized later and performing table scan
36
 * only once for all the instances.
37
 *
38
 * The code uses OS-dependent parts for kernel updates and scans. These parts are
39
 * in more specific sysdep directories (e.g. sysdep/linux) in functions krt_sys_*
40
 * and kif_sys_* (and some others like krt_replace_rte()) and krt-sys.h header file.
41
 * This is also used for platform specific protocol options and route attributes.
42
 *
43
 * There was also an old code that used traditional UNIX ioctls for these tasks.
44
 * It was unmaintained and later removed. For reference, see sysdep/krt-* files
45
 * in commit 396dfa9042305f62da1f56589c4b98fac57fc2f6
46
 */
47

    
48
/*
49
 *  If you are brave enough, continue now.  You cannot say you haven't been warned.
50
 */
51

    
52
#undef LOCAL_DEBUG
53

    
54
#include "nest/bird.h"
55
#include "nest/iface.h"
56
#include "nest/route.h"
57
#include "nest/protocol.h"
58
#include "filter/filter.h"
59
#include "lib/timer.h"
60
#include "conf/conf.h"
61
#include "lib/string.h"
62

    
63
#include "unix.h"
64
#include "krt.h"
65

    
66
/*
67
 *        Global resources
68
 */
69

    
70
pool *krt_pool;
71
static linpool *krt_filter_lp;
72
static list krt_proto_list;
73

    
74
void
75
krt_io_init(void)
76
{
77
  krt_pool = rp_new(&root_pool, "Kernel Syncer");
78
  krt_filter_lp = lp_new(krt_pool, 4080);
79
  init_list(&krt_proto_list);
80
  krt_sys_io_init();
81
}
82

    
83
/*
84
 *        Interfaces
85
 */
86

    
87
struct kif_proto *kif_proto;
88
static struct kif_config *kif_cf;
89
static timer *kif_scan_timer;
90
static bird_clock_t kif_last_shot;
91

    
92
static void
93
kif_scan(timer *t)
94
{
95
  struct kif_proto *p = t->data;
96

    
97
  KRT_TRACE(p, D_EVENTS, "Scanning interfaces");
98
  kif_last_shot = now;
99
  kif_do_scan(p);
100
}
101

    
102
static void
103
kif_force_scan(void)
104
{
105
  if (kif_proto && kif_last_shot + 2 < now)
106
    {
107
      kif_scan(kif_scan_timer);
108
      tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
109
    }
110
}
111

    
112
void
113
kif_request_scan(void)
114
{
115
  if (kif_proto && kif_scan_timer->expires > now)
116
    tm_start(kif_scan_timer, 1);
117
}
118

    
119
static inline int
120
prefer_addr(struct ifa *a, struct ifa *b)
121
{
122
  int sa = a->scope > SCOPE_LINK;
123
  int sb = b->scope > SCOPE_LINK;
124

    
125
  if (sa < sb)
126
    return 0;
127
  else if (sa > sb)
128
    return 1;
129
  else
130
    return ipa_compare(a->ip, b->ip) < 0;
131
}
132

    
133
static inline struct ifa *
134
find_preferred_ifa(struct iface *i, const net_addr *n)
135
{
136
  struct ifa *a, *b = NULL;
137

    
138
  WALK_LIST(a, i->addrs)
139
    {
140
      if (!(a->flags & IA_SECONDARY) &&
141
          (!n || ipa_in_netX(a->ip, n)) &&
142
          (!b || prefer_addr(a, b)))
143
        b = a;
144
    }
145

    
146
  return b;
147
}
148

    
149
struct ifa *
150
kif_choose_primary(struct iface *i)
151
{
152
  struct kif_config *cf = (struct kif_config *) (kif_proto->p.cf);
153
  struct kif_primary_item *it;
154
  struct ifa *a;
155

    
156
  WALK_LIST(it, cf->primary)
157
    {
158
      if (!it->pattern || patmatch(it->pattern, i->name))
159
        if (a = find_preferred_ifa(i, &it->addr))
160
          return a;
161
    }
162

    
163
  if (a = kif_get_primary_ip(i))
164
    return a;
165

    
166
  return find_preferred_ifa(i, NULL);
167
}
168

    
169

    
170
static struct proto *
171
kif_init(struct proto_config *c)
172
{
173
  struct kif_proto *p = proto_new(c);
174

    
175
  kif_sys_init(p);
176
  return &p->p;
177
}
178

    
179
static int
180
kif_start(struct proto *P)
181
{
182
  struct kif_proto *p = (struct kif_proto *) P;
183

    
184
  kif_proto = p;
185
  kif_sys_start(p);
186

    
187
  /* Start periodic interface scanning */
188
  kif_scan_timer = tm_new(P->pool);
189
  kif_scan_timer->hook = kif_scan;
190
  kif_scan_timer->data = p;
191
  kif_scan_timer->recurrent = KIF_CF->scan_time;
192
  kif_scan(kif_scan_timer);
193
  tm_start(kif_scan_timer, KIF_CF->scan_time);
194

    
195
  return PS_UP;
196
}
197

    
198
static int
199
kif_shutdown(struct proto *P)
200
{
201
  struct kif_proto *p = (struct kif_proto *) P;
202

    
203
  tm_stop(kif_scan_timer);
204
  kif_sys_shutdown(p);
205
  kif_proto = NULL;
206

    
207
  return PS_DOWN;
208
}
209

    
210
static int
211
kif_reconfigure(struct proto *p, struct proto_config *new)
212
{
213
  struct kif_config *o = (struct kif_config *) p->cf;
214
  struct kif_config *n = (struct kif_config *) new;
215

    
216
  if (!kif_sys_reconfigure((struct kif_proto *) p, n, o))
217
    return 0;
218

    
219
  if (o->scan_time != n->scan_time)
220
    {
221
      tm_stop(kif_scan_timer);
222
      kif_scan_timer->recurrent = n->scan_time;
223
      kif_scan(kif_scan_timer);
224
      tm_start(kif_scan_timer, n->scan_time);
225
    }
226

    
227
  if (!EMPTY_LIST(o->primary) || !EMPTY_LIST(n->primary))
228
    {
229
      /* This is hack, we have to update a configuration
230
       * to the new value just now, because it is used
231
       * for recalculation of primary addresses.
232
       */
233
      p->cf = new;
234

    
235
      ifa_recalc_all_primary_addresses();
236
    }
237

    
238
  return 1;
239
}
240

    
241

    
242
static void
243
kif_preconfig(struct protocol *P UNUSED, struct config *c)
244
{
245
  kif_cf = NULL;
246
  kif_sys_preconfig(c);
247
}
248

    
249
struct proto_config *
250
kif_init_config(int class)
251
{
252
  if (kif_cf)
253
    cf_error("Kernel device protocol already defined");
254

    
255
  kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, class);
256
  kif_cf->scan_time = 60;
257
  init_list(&kif_cf->primary);
258

    
259
  kif_sys_init_config(kif_cf);
260
  return (struct proto_config *) kif_cf;
261
}
262

    
263
static void
264
kif_copy_config(struct proto_config *dest, struct proto_config *src)
265
{
266
  struct kif_config *d = (struct kif_config *) dest;
267
  struct kif_config *s = (struct kif_config *) src;
268

    
269
  /* Copy primary addr list */
270
  cfg_copy_list(&d->primary, &s->primary, sizeof(struct kif_primary_item));
271

    
272
  /* Fix sysdep parts */
273
  kif_sys_copy_config(d, s);
274
}
275

    
276

    
277
struct protocol proto_unix_iface = {
278
  .name =                 "Device",
279
  .template =                 "device%d",
280
  .proto_size =                sizeof(struct kif_proto),
281
  .config_size =        sizeof(struct kif_config),
282
  .preconfig =                kif_preconfig,
283
  .init =                kif_init,
284
  .start =                kif_start,
285
  .shutdown =                kif_shutdown,
286
  .reconfigure =        kif_reconfigure,
287
  .copy_config =        kif_copy_config
288
};
289

    
290
/*
291
 *        Tracing of routes
292
 */
293

    
294
static inline void
295
krt_trace_in(struct krt_proto *p, rte *e, char *msg)
296
{
297
  if (p->p.debug & D_PACKETS)
298
    log(L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
299
}
300

    
301
static inline void
302
krt_trace_in_rl(struct tbf *f, struct krt_proto *p, rte *e, char *msg)
303
{
304
  if (p->p.debug & D_PACKETS)
305
    log_rl(f, L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
306
}
307

    
308
/*
309
 *        Inherited Routes
310
 */
311

    
312
#ifdef KRT_ALLOW_LEARN
313

    
314
static struct tbf rl_alien = TBF_DEFAULT_LOG_LIMITS;
315

    
316
/*
317
 * krt_same_key() specifies what (aside from the net) is the key in
318
 * kernel routing tables. It should be OS-dependent, this is for
319
 * Linux. It is important for asynchronous alien updates, because a
320
 * positive update is implicitly a negative one for any old route with
321
 * the same key.
322
 */
323

    
324
static inline int
325
krt_same_key(rte *a, rte *b)
326
{
327
  return a->u.krt.metric == b->u.krt.metric;
328
}
329

    
330
static inline int
331
krt_uptodate(rte *a, rte *b)
332
{
333
  if (a->attrs != b->attrs)
334
    return 0;
335

    
336
  if (a->u.krt.proto != b->u.krt.proto)
337
    return 0;
338

    
339
  return 1;
340
}
341

    
342
static void
343
krt_learn_announce_update(struct krt_proto *p, rte *e)
344
{
345
  net *n = e->net;
346
  rta *aa = rta_clone(e->attrs);
347
  rte *ee = rte_get_temp(aa);
348
  net *nn = net_get(p->p.main_channel->table, n->n.addr);
349
  ee->net = nn;
350
  ee->pflags = 0;
351
  ee->u.krt = e->u.krt;
352
  rte_update(&p->p, nn, ee);
353
}
354

    
355
static void
356
krt_learn_announce_delete(struct krt_proto *p, net *n)
357
{
358
  n = net_find(p->p.main_channel->table, n->n.addr);
359
  rte_update(&p->p, n, NULL);
360
}
361

    
362
/* Called when alien route is discovered during scan */
363
static void
364
krt_learn_scan(struct krt_proto *p, rte *e)
365
{
366
  net *n0 = e->net;
367
  net *n = net_get(&p->krt_table, n0->n.addr);
368
  rte *m, **mm;
369

    
370
  e->attrs = rta_lookup(e->attrs);
371

    
372
  for(mm=&n->routes; m = *mm; mm=&m->next)
373
    if (krt_same_key(m, e))
374
      break;
375
  if (m)
376
    {
377
      if (krt_uptodate(m, e))
378
        {
379
          krt_trace_in_rl(&rl_alien, p, e, "[alien] seen");
380
          rte_free(e);
381
          m->u.krt.seen = 1;
382
        }
383
      else
384
        {
385
          krt_trace_in(p, e, "[alien] updated");
386
          *mm = m->next;
387
          rte_free(m);
388
          m = NULL;
389
        }
390
    }
391
  else
392
    krt_trace_in(p, e, "[alien] created");
393
  if (!m)
394
    {
395
      e->next = n->routes;
396
      n->routes = e;
397
      e->u.krt.seen = 1;
398
    }
399
}
400

    
401
static void
402
krt_learn_prune(struct krt_proto *p)
403
{
404
  struct fib *fib = &p->krt_table.fib;
405
  struct fib_iterator fit;
406

    
407
  KRT_TRACE(p, D_EVENTS, "Pruning inherited routes");
408

    
409
  FIB_ITERATE_INIT(&fit, fib);
410
again:
411
  FIB_ITERATE_START(fib, &fit, net, n)
412
    {
413
      rte *e, **ee, *best, **pbest, *old_best;
414

    
415
      old_best = n->routes;
416
      best = NULL;
417
      pbest = NULL;
418
      ee = &n->routes;
419
      while (e = *ee)
420
        {
421
          if (!e->u.krt.seen)
422
            {
423
              *ee = e->next;
424
              rte_free(e);
425
              continue;
426
            }
427
          if (!best || best->u.krt.metric > e->u.krt.metric)
428
            {
429
              best = e;
430
              pbest = ee;
431
            }
432
          e->u.krt.seen = 0;
433
          ee = &e->next;
434
        }
435
      if (!n->routes)
436
        {
437
          DBG("%I/%d: deleting\n", n->n.prefix, n->n.pxlen);
438
          if (old_best)
439
            {
440
              krt_learn_announce_delete(p, n);
441
              n->n.flags &= ~KRF_INSTALLED;
442
            }
443
          FIB_ITERATE_PUT(&fit);
444
          fib_delete(fib, n);
445
          goto again;
446
        }
447
      *pbest = best->next;
448
      best->next = n->routes;
449
      n->routes = best;
450
      if (best != old_best || !(n->n.flags & KRF_INSTALLED) || p->reload)
451
        {
452
          DBG("%I/%d: announcing (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
453
          krt_learn_announce_update(p, best);
454
          n->n.flags |= KRF_INSTALLED;
455
        }
456
      else
457
        DBG("%I/%d: uptodate (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
458
    }
459
  FIB_ITERATE_END;
460

    
461
  p->reload = 0;
462
}
463

    
464
static void
465
krt_learn_async(struct krt_proto *p, rte *e, int new)
466
{
467
  net *n0 = e->net;
468
  net *n = net_get(&p->krt_table, n0->n.addr);
469
  rte *g, **gg, *best, **bestp, *old_best;
470

    
471
  e->attrs = rta_lookup(e->attrs);
472

    
473
  old_best = n->routes;
474
  for(gg=&n->routes; g = *gg; gg = &g->next)
475
    if (krt_same_key(g, e))
476
      break;
477
  if (new)
478
    {
479
      if (g)
480
        {
481
          if (krt_uptodate(g, e))
482
            {
483
              krt_trace_in(p, e, "[alien async] same");
484
              rte_free(e);
485
              return;
486
            }
487
          krt_trace_in(p, e, "[alien async] updated");
488
          *gg = g->next;
489
          rte_free(g);
490
        }
491
      else
492
        krt_trace_in(p, e, "[alien async] created");
493

    
494
      e->next = n->routes;
495
      n->routes = e;
496
    }
497
  else if (!g)
498
    {
499
      krt_trace_in(p, e, "[alien async] delete failed");
500
      rte_free(e);
501
      return;
502
    }
503
  else
504
    {
505
      krt_trace_in(p, e, "[alien async] removed");
506
      *gg = g->next;
507
      rte_free(e);
508
      rte_free(g);
509
    }
510
  best = n->routes;
511
  bestp = &n->routes;
512
  for(gg=&n->routes; g=*gg; gg=&g->next)
513
    if (best->u.krt.metric > g->u.krt.metric)
514
      {
515
        best = g;
516
        bestp = gg;
517
      }
518
  if (best)
519
    {
520
      *bestp = best->next;
521
      best->next = n->routes;
522
      n->routes = best;
523
    }
524
  if (best != old_best)
525
    {
526
      DBG("krt_learn_async: distributing change\n");
527
      if (best)
528
        {
529
          krt_learn_announce_update(p, best);
530
          n->n.flags |= KRF_INSTALLED;
531
        }
532
      else
533
        {
534
          n->routes = NULL;
535
          krt_learn_announce_delete(p, n);
536
          n->n.flags &= ~KRF_INSTALLED;
537
        }
538
    }
539
}
540

    
541
static void
542
krt_learn_init(struct krt_proto *p)
543
{
544
  if (KRT_CF->learn)
545
    rt_setup(p->p.pool, &p->krt_table, "Inherited", NULL);
546
}
547

    
548
static void
549
krt_dump(struct proto *P)
550
{
551
  struct krt_proto *p = (struct krt_proto *) P;
552

    
553
  if (!KRT_CF->learn)
554
    return;
555
  debug("KRT: Table of inheritable routes\n");
556
  rt_dump(&p->krt_table);
557
}
558

    
559
static void
560
krt_dump_attrs(rte *e)
561
{
562
  debug(" [m=%d,p=%d,t=%d]", e->u.krt.metric, e->u.krt.proto, e->u.krt.type);
563
}
564

    
565
#endif
566

    
567
/*
568
 *        Routes
569
 */
570

    
571
static void
572
krt_flush_routes(struct krt_proto *p)
573
{
574
  struct rtable *t = p->p.main_channel->table;
575

    
576
  KRT_TRACE(p, D_EVENTS, "Flushing kernel routes");
577
  FIB_WALK(&t->fib, net, n)
578
    {
579
      rte *e = n->routes;
580
      if (rte_is_valid(e) && (n->n.flags & KRF_INSTALLED))
581
        {
582
          /* FIXME: this does not work if gw is changed in export filter */
583
          krt_replace_rte(p, e->net, NULL, e, NULL);
584
          n->n.flags &= ~KRF_INSTALLED;
585
        }
586
    }
587
  FIB_WALK_END;
588
}
589

    
590
static struct rte *
591
krt_export_net(struct krt_proto *p, net *net, rte **rt_free, ea_list **tmpa)
592
{
593
  struct channel *c = p->p.main_channel;
594
  struct filter *filter = c->out_filter;
595
  rte *rt;
596

    
597
  if (c->ra_mode == RA_MERGED)
598
    return rt_export_merged(c, net, rt_free, tmpa, 1);
599

    
600
  rt = net->routes;
601
  *rt_free = NULL;
602

    
603
  if (!rte_is_valid(rt))
604
    return NULL;
605

    
606
  if (filter == FILTER_REJECT)
607
    return NULL;
608

    
609
  struct proto *src = rt->attrs->src->proto;
610
  *tmpa = src->make_tmp_attrs ? src->make_tmp_attrs(rt, krt_filter_lp) : NULL;
611

    
612
  /* We could run krt_import_control() here, but it is already handled by KRF_INSTALLED */
613

    
614
  if (filter == FILTER_ACCEPT)
615
    goto accept;
616

    
617
  if (f_run(filter, &rt, tmpa, krt_filter_lp, FF_FORCE_TMPATTR) > F_ACCEPT)
618
    goto reject;
619

    
620

    
621
accept:
622
  if (rt != net->routes)
623
    *rt_free = rt;
624
  return rt;
625

    
626
reject:
627
  if (rt != net->routes)
628
    rte_free(rt);
629
  return NULL;
630
}
631

    
632
static int
633
krt_same_dest(rte *k, rte *e)
634
{
635
  rta *ka = k->attrs, *ea = e->attrs;
636

    
637
  if (ka->dest != ea->dest)
638
    return 0;
639
  switch (ka->dest)
640
    {
641
    case RTD_ROUTER:
642
      return ipa_equal(ka->gw, ea->gw);
643
    case RTD_DEVICE:
644
      return !strcmp(ka->iface->name, ea->iface->name);
645
    case RTD_MULTIPATH:
646
      return mpnh_same(ka->nexthops, ea->nexthops);
647
    default:
648
      return 1;
649
    }
650
}
651

    
652
/*
653
 *  This gets called back when the low-level scanning code discovers a route.
654
 *  We expect that the route is a temporary rte and its attributes are uncached.
655
 */
656

    
657
void
658
krt_got_route(struct krt_proto *p, rte *e)
659
{
660
  net *net = e->net;
661
  int verdict;
662

    
663
#ifdef KRT_ALLOW_LEARN
664
  switch (e->u.krt.src)
665
    {
666
    case KRT_SRC_KERNEL:
667
      verdict = KRF_IGNORE;
668
      goto sentenced;
669

    
670
    case KRT_SRC_REDIRECT:
671
      verdict = KRF_DELETE;
672
      goto sentenced;
673

    
674
    case  KRT_SRC_ALIEN:
675
      if (KRT_CF->learn)
676
        krt_learn_scan(p, e);
677
      else
678
        {
679
          krt_trace_in_rl(&rl_alien, p, e, "[alien] ignored");
680
          rte_free(e);
681
        }
682
      return;
683
    }
684
#endif
685
  /* The rest is for KRT_SRC_BIRD (or KRT_SRC_UNKNOWN) */
686

    
687
  if (net->n.flags & KRF_VERDICT_MASK)
688
    {
689
      /* Route to this destination was already seen. Strange, but it happens... */
690
      krt_trace_in(p, e, "already seen");
691
      rte_free(e);
692
      return;
693
    }
694

    
695
  if (!p->ready)
696
    {
697
      /* We wait for the initial feed to have correct KRF_INSTALLED flag */
698
      verdict = KRF_IGNORE;
699
      goto sentenced;
700
    }
701

    
702
  if (net->n.flags & KRF_INSTALLED)
703
    {
704
      rte *new, *rt_free;
705
      ea_list *tmpa;
706

    
707
      new = krt_export_net(p, net, &rt_free, &tmpa);
708

    
709
      /* TODO: There also may be changes in route eattrs, we ignore that for now. */
710

    
711
      if (!new)
712
        verdict = KRF_DELETE;
713
      else if ((net->n.flags & KRF_SYNC_ERROR) || !krt_same_dest(e, new))
714
        verdict = KRF_UPDATE;
715
      else
716
        verdict = KRF_SEEN;
717

    
718
      if (rt_free)
719
        rte_free(rt_free);
720

    
721
      lp_flush(krt_filter_lp);
722
    }
723
  else
724
    verdict = KRF_DELETE;
725

    
726
 sentenced:
727
  krt_trace_in(p, e, ((char *[]) { "?", "seen", "will be updated", "will be removed", "ignored" }) [verdict]);
728
  net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
729
  if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
730
    {
731
      /* Get a cached copy of attributes and temporarily link the route */
732
      rta *a = e->attrs;
733
      a->source = RTS_DUMMY;
734
      e->attrs = rta_lookup(a);
735
      e->next = net->routes;
736
      net->routes = e;
737
    }
738
  else
739
    rte_free(e);
740
}
741

    
742
static void
743
krt_prune(struct krt_proto *p)
744
{
745
  struct rtable *t = p->p.main_channel->table;
746

    
747
  KRT_TRACE(p, D_EVENTS, "Pruning table %s", t->name);
748
  FIB_WALK(&t->fib, net, n)
749
    {
750
      int verdict = n->n.flags & KRF_VERDICT_MASK;
751
      rte *new, *old, *rt_free = NULL;
752
      ea_list *tmpa = NULL;
753

    
754
      if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
755
        {
756
          /* Get a dummy route from krt_got_route() */
757
          old = n->routes;
758
          n->routes = old->next;
759
        }
760
      else
761
        old = NULL;
762

    
763
      if (verdict == KRF_CREATE || verdict == KRF_UPDATE)
764
        {
765
          /* We have to run export filter to get proper 'new' route */
766
          new = krt_export_net(p, n, &rt_free, &tmpa);
767

    
768
          if (!new)
769
            verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
770
          else
771
            tmpa = ea_append(tmpa, new->attrs->eattrs);
772
        }
773
      else
774
        new = NULL;
775

    
776
      switch (verdict)
777
        {
778
        case KRF_CREATE:
779
          if (new && (n->n.flags & KRF_INSTALLED))
780
            {
781
              krt_trace_in(p, new, "reinstalling");
782
              krt_replace_rte(p, n, new, NULL, tmpa);
783
            }
784
          break;
785
        case KRF_SEEN:
786
        case KRF_IGNORE:
787
          /* Nothing happens */
788
          break;
789
        case KRF_UPDATE:
790
          krt_trace_in(p, new, "updating");
791
          krt_replace_rte(p, n, new, old, tmpa);
792
          break;
793
        case KRF_DELETE:
794
          krt_trace_in(p, old, "deleting");
795
          krt_replace_rte(p, n, NULL, old, NULL);
796
          break;
797
        default:
798
          bug("krt_prune: invalid route status");
799
        }
800

    
801
      if (old)
802
        rte_free(old);
803
      if (rt_free)
804
        rte_free(rt_free);
805
      lp_flush(krt_filter_lp);
806
      n->n.flags &= ~KRF_VERDICT_MASK;
807
    }
808
  FIB_WALK_END;
809

    
810
#ifdef KRT_ALLOW_LEARN
811
  if (KRT_CF->learn)
812
    krt_learn_prune(p);
813
#endif
814

    
815
  if (p->ready)
816
    p->initialized = 1;
817
}
818

    
819
void
820
krt_got_route_async(struct krt_proto *p, rte *e, int new)
821
{
822
  net *net = e->net;
823

    
824
  switch (e->u.krt.src)
825
    {
826
    case KRT_SRC_BIRD:
827
      ASSERT(0);                        /* Should be filtered by the back end */
828

    
829
    case KRT_SRC_REDIRECT:
830
      if (new)
831
        {
832
          krt_trace_in(p, e, "[redirect] deleting");
833
          krt_replace_rte(p, net, NULL, e, NULL);
834
        }
835
      /* If !new, it is probably echo of our deletion */
836
      break;
837

    
838
#ifdef KRT_ALLOW_LEARN
839
    case KRT_SRC_ALIEN:
840
      if (KRT_CF->learn)
841
        {
842
          krt_learn_async(p, e, new);
843
          return;
844
        }
845
#endif
846
    }
847
  rte_free(e);
848
}
849

    
850
/*
851
 *        Periodic scanning
852
 */
853

    
854

    
855
#ifdef CONFIG_ALL_TABLES_AT_ONCE
856

    
857
static timer *krt_scan_timer;
858
static int krt_scan_count;
859

    
860
static void
861
krt_scan(timer *t UNUSED)
862
{
863
  struct krt_proto *p;
864

    
865
  kif_force_scan();
866

    
867
  /* We need some node to decide whether to print the debug messages or not */
868
  p = SKIP_BACK(struct krt_proto, krt_node, HEAD(krt_proto_list));
869
  KRT_TRACE(p, D_EVENTS, "Scanning routing table");
870

    
871
  krt_do_scan(NULL);
872

    
873
  void *q;
874
  WALK_LIST(q, krt_proto_list)
875
  {
876
    p = SKIP_BACK(struct krt_proto, krt_node, q);
877
    krt_prune(p);
878
  }
879
}
880

    
881
static void
882
krt_scan_timer_start(struct krt_proto *p)
883
{
884
  if (!krt_scan_count)
885
    krt_scan_timer = tm_new_set(krt_pool, krt_scan, NULL, 0, KRT_CF->scan_time);
886

    
887
  krt_scan_count++;
888

    
889
  tm_start(krt_scan_timer, 1);
890
}
891

    
892
static void
893
krt_scan_timer_stop(struct krt_proto *p)
894
{
895
  krt_scan_count--;
896

    
897
  if (!krt_scan_count)
898
  {
899
    rfree(krt_scan_timer);
900
    krt_scan_timer = NULL;
901
  }
902
}
903

    
904
static void
905
krt_scan_timer_kick(struct krt_proto *p UNUSED)
906
{
907
  tm_start(krt_scan_timer, 0);
908
}
909

    
910
#else
911

    
912
static void
913
krt_scan(timer *t)
914
{
915
  struct krt_proto *p = t->data;
916

    
917
  kif_force_scan();
918

    
919
  KRT_TRACE(p, D_EVENTS, "Scanning routing table");
920
  krt_do_scan(p);
921
  krt_prune(p);
922
}
923

    
924
static void
925
krt_scan_timer_start(struct krt_proto *p)
926
{
927
  p->scan_timer = tm_new_set(p->p.pool, krt_scan, p, 0, KRT_CF->scan_time);
928
  tm_start(p->scan_timer, 1);
929
}
930

    
931
static void
932
krt_scan_timer_stop(struct krt_proto *p)
933
{
934
  tm_stop(p->scan_timer);
935
}
936

    
937
static void
938
krt_scan_timer_kick(struct krt_proto *p)
939
{
940
  tm_start(p->scan_timer, 0);
941
}
942

    
943
#endif
944

    
945

    
946

    
947

    
948
/*
949
 *        Updates
950
 */
951

    
952
static struct ea_list *
953
krt_make_tmp_attrs(rte *rt, struct linpool *pool)
954
{
955
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2 * sizeof(eattr));
956

    
957
  l->next = NULL;
958
  l->flags = EALF_SORTED;
959
  l->count = 2;
960

    
961
  l->attrs[0].id = EA_KRT_SOURCE;
962
  l->attrs[0].flags = 0;
963
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
964
  l->attrs[0].u.data = rt->u.krt.proto;
965

    
966
  l->attrs[1].id = EA_KRT_METRIC;
967
  l->attrs[1].flags = 0;
968
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
969
  l->attrs[1].u.data = rt->u.krt.metric;
970

    
971
  return l;
972
}
973

    
974
static void
975
krt_store_tmp_attrs(rte *rt, struct ea_list *attrs)
976
{
977
  /* EA_KRT_SOURCE is read-only */
978
  rt->u.krt.metric = ea_get_int(attrs, EA_KRT_METRIC, 0);
979
}
980

    
981
static int
982
krt_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
983
{
984
  struct krt_proto *p = (struct krt_proto *) P;
985
  rte *e = *new;
986

    
987
  if (e->attrs->src->proto == P)
988
  {
989
#ifdef CONFIG_SINGLE_ROUTE
990
    /*
991
     * Implicit withdraw - when the imported kernel route becomes the best one,
992
     * we know that the previous one exported to the kernel was already removed,
993
     * but if we processed the update as usual, we would send withdraw to the
994
     * kernel, which would remove the new imported route instead.
995
     *
996
     * We will remove KRT_INSTALLED flag, which stops such withdraw to be
997
     * processed in krt_rt_notify() and krt_replace_rte().
998
     */
999
    if (e == e->net->routes)
1000
      e->net->n.flags &= ~KRF_INSTALLED;
1001
#endif
1002
    return -1;
1003
  }
1004

    
1005
  if (!KRT_CF->devroutes &&
1006
      (e->attrs->dest == RTD_DEVICE) &&
1007
      (e->attrs->source != RTS_STATIC_DEVICE))
1008
    return -1;
1009

    
1010
  if (!krt_capable(e))
1011
    return -1;
1012

    
1013
  return 0;
1014
}
1015

    
1016
static void
1017
krt_rt_notify(struct proto *P, struct rtable *table UNUSED, net *net,
1018
              rte *new, rte *old, struct ea_list *eattrs)
1019
{
1020
  struct krt_proto *p = (struct krt_proto *) P;
1021

    
1022
  if (config->shutdown)
1023
    return;
1024
  if (!(net->n.flags & KRF_INSTALLED))
1025
    old = NULL;
1026
  if (new)
1027
    net->n.flags |= KRF_INSTALLED;
1028
  else
1029
    net->n.flags &= ~KRF_INSTALLED;
1030
  if (p->initialized)                /* Before first scan we don't touch the routes */
1031
    krt_replace_rte(p, net, new, old, eattrs);
1032
}
1033

    
1034
static void
1035
krt_if_notify(struct proto *P, uint flags, struct iface *iface UNUSED)
1036
{
1037
  struct krt_proto *p = (struct krt_proto *) P;
1038

    
1039
  /*
1040
   * When interface went down, we should remove routes to it. In the ideal world,
1041
   * OS kernel would send us route removal notifications in such cases, but we
1042
   * cannot rely on it as it is often not true. E.g. Linux kernel removes related
1043
   * routes when an interface went down, but it does not notify userspace about
1044
   * that. To be sure, we just schedule a scan to ensure synchronization.
1045
   */
1046

    
1047
  if ((flags & IF_CHANGE_DOWN) && KRT_CF->learn)
1048
    krt_scan_timer_kick(p);
1049
}
1050

    
1051
static void
1052
krt_reload_routes(struct channel *C)
1053
{
1054
  struct krt_proto *p = (void *) C->proto;
1055

    
1056
  /* Although we keep learned routes in krt_table, we rather schedule a scan */
1057

    
1058
  if (KRT_CF->learn)
1059
  {
1060
    p->reload = 1;
1061
    krt_scan_timer_kick(p);
1062
  }
1063
}
1064

    
1065
static void
1066
krt_feed_end(struct channel *C)
1067
{
1068
  struct krt_proto *p = (void *) C->proto;
1069

    
1070
  p->ready = 1;
1071
  krt_scan_timer_kick(p);
1072
}
1073

    
1074

    
1075
static int
1076
krt_rte_same(rte *a, rte *b)
1077
{
1078
  /* src is always KRT_SRC_ALIEN and type is irrelevant */
1079
  return (a->u.krt.proto == b->u.krt.proto) && (a->u.krt.metric == b->u.krt.metric);
1080
}
1081

    
1082

    
1083
/*
1084
 *        Protocol glue
1085
 */
1086

    
1087
struct krt_config *krt_cf;
1088

    
1089
static void
1090
krt_preconfig(struct protocol *P UNUSED, struct config *c)
1091
{
1092
  krt_cf = NULL;
1093
  krt_sys_preconfig(c);
1094
}
1095

    
1096
static void
1097
krt_postconfig(struct proto_config *CF)
1098
{
1099
  struct krt_config *cf = (void *) CF;
1100

    
1101
  if (EMPTY_LIST(CF->channels))
1102
    cf_error("Channel not specified");
1103

    
1104
#ifdef CONFIG_ALL_TABLES_AT_ONCE
1105
  if (krt_cf->scan_time != cf->scan_time)
1106
    cf_error("All kernel syncers must use the same table scan interval");
1107
#endif
1108

    
1109
  struct rtable_config *tab = proto_cf_main_channel(CF)->table;
1110
  if (tab->krt_attached)
1111
    cf_error("Kernel syncer (%s) already attached to table %s", tab->krt_attached->name, tab->name);
1112
  tab->krt_attached = CF;
1113

    
1114
  krt_sys_postconfig(cf);
1115
}
1116

    
1117
static struct proto *
1118
krt_init(struct proto_config *CF)
1119
{
1120
  struct krt_proto *p = proto_new(CF);
1121
  // struct krt_config *cf = (void *) CF;
1122

    
1123
  p->p.main_channel = proto_add_channel(&p->p, proto_cf_main_channel(CF));
1124

    
1125
  p->p.import_control = krt_import_control;
1126
  p->p.rt_notify = krt_rt_notify;
1127
  p->p.if_notify = krt_if_notify;
1128
  p->p.reload_routes = krt_reload_routes;
1129
  p->p.feed_end = krt_feed_end;
1130
  p->p.make_tmp_attrs = krt_make_tmp_attrs;
1131
  p->p.store_tmp_attrs = krt_store_tmp_attrs;
1132
  p->p.rte_same = krt_rte_same;
1133

    
1134
  krt_sys_init(p);
1135
  return &p->p;
1136
}
1137

    
1138
static int
1139
krt_start(struct proto *P)
1140
{
1141
  struct krt_proto *p = (struct krt_proto *) P;
1142

    
1143
  switch (p->p.net_type)
1144
  {
1145
  case NET_IP4:        p->af = AF_INET; break;
1146
  case NET_IP6:        p->af = AF_INET6; break;
1147
  default:        ASSERT(0);
1148
  }
1149

    
1150
  add_tail(&krt_proto_list, &p->krt_node);
1151

    
1152
#ifdef KRT_ALLOW_LEARN
1153
  krt_learn_init(p);
1154
#endif
1155

    
1156
  if (!krt_sys_start(p))
1157
  {
1158
    rem_node(&p->krt_node);
1159
    return PS_START;
1160
  }
1161

    
1162
  krt_scan_timer_start(p);
1163

    
1164
  if (p->p.gr_recovery && KRT_CF->graceful_restart)
1165
    p->p.main_channel->gr_wait = 1;
1166

    
1167
  return PS_UP;
1168
}
1169

    
1170
static int
1171
krt_shutdown(struct proto *P)
1172
{
1173
  struct krt_proto *p = (struct krt_proto *) P;
1174

    
1175
  krt_scan_timer_stop(p);
1176

    
1177
  /* FIXME we should flush routes even when persist during reconfiguration */
1178
  if (p->initialized && !KRT_CF->persist)
1179
    krt_flush_routes(p);
1180

    
1181
  p->ready = 0;
1182
  p->initialized = 0;
1183

    
1184
  if (p->p.proto_state == PS_START)
1185
    return PS_DOWN;
1186

    
1187
  krt_sys_shutdown(p);
1188
  rem_node(&p->krt_node);
1189

    
1190
  return PS_DOWN;
1191
}
1192

    
1193
static int
1194
krt_reconfigure(struct proto *p, struct proto_config *CF)
1195
{
1196
  struct krt_config *o = (void *) p->cf;
1197
  struct krt_config *n = (void *) CF;
1198

    
1199
  if (!proto_configure_channel(p, &p->main_channel, proto_cf_main_channel(CF)))
1200
    return 0;
1201

    
1202
  if (!krt_sys_reconfigure((struct krt_proto *) p, n, o))
1203
    return 0;
1204

    
1205
  /* persist, graceful restart need not be the same */
1206
  return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes;
1207
}
1208

    
1209
struct proto_config *
1210
krt_init_config(int class)
1211
{
1212
#ifndef CONFIG_MULTIPLE_TABLES
1213
  if (krt_cf)
1214
    cf_error("Kernel protocol already defined");
1215
#endif
1216

    
1217
  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
1218
  krt_cf->scan_time = 60;
1219

    
1220
  krt_sys_init_config(krt_cf);
1221
  return (struct proto_config *) krt_cf;
1222
}
1223

    
1224
static void
1225
krt_copy_config(struct proto_config *dest, struct proto_config *src)
1226
{
1227
  struct krt_config *d = (struct krt_config *) dest;
1228
  struct krt_config *s = (struct krt_config *) src;
1229

    
1230
  /* Fix sysdep parts */
1231
  krt_sys_copy_config(d, s);
1232
}
1233

    
1234
static int
1235
krt_get_attr(eattr *a, byte *buf, int buflen)
1236
{
1237
  switch (a->id)
1238
  {
1239
  case EA_KRT_SOURCE:
1240
    bsprintf(buf, "source");
1241
    return GA_NAME;
1242

    
1243
  case EA_KRT_METRIC:
1244
    bsprintf(buf, "metric");
1245
    return GA_NAME;
1246

    
1247
  default:
1248
    return krt_sys_get_attr(a, buf, buflen);
1249
  }
1250
}
1251

    
1252

    
1253
struct protocol proto_unix_kernel = {
1254
  .name =                "Kernel",
1255
  .template =                "kernel%d",
1256
  .attr_class =                EAP_KRT,
1257
  .preference =                DEF_PREF_INHERITED,
1258
  .channel_mask =        NB_IP,
1259
  .proto_size =                sizeof(struct krt_proto),
1260
  .config_size =        sizeof(struct krt_config),
1261
  .preconfig =                krt_preconfig,
1262
  .postconfig =                krt_postconfig,
1263
  .init =                krt_init,
1264
  .start =                krt_start,
1265
  .shutdown =                krt_shutdown,
1266
  .reconfigure =        krt_reconfigure,
1267
  .copy_config =        krt_copy_config,
1268
  .get_attr =                krt_get_attr,
1269
#ifdef KRT_ALLOW_LEARN
1270
  .dump =                krt_dump,
1271
  .dump_attrs =                krt_dump_attrs,
1272
#endif
1273
};