Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / krt.c @ 153f02da

History | View | Annotate | Download (26.4 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 "sysdep/unix/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_default(krt_pool);
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 struct kif_iface_config kif_default_iface = {};
93

    
94
struct kif_iface_config *
95
kif_get_iface_config(struct iface *iface)
96
{
97
  struct kif_config *cf = (void *) (kif_proto->p.cf);
98
  struct kif_iface_config *ic = (void *) iface_patt_find(&cf->iface_list, iface, NULL);
99
  return ic ?: &kif_default_iface;
100
}
101

    
102
static void
103
kif_scan(timer *t)
104
{
105
  struct kif_proto *p = t->data;
106

    
107
  KRT_TRACE(p, D_EVENTS, "Scanning interfaces");
108
  kif_last_shot = now;
109
  kif_do_scan(p);
110
}
111

    
112
static void
113
kif_force_scan(void)
114
{
115
  if (kif_proto && kif_last_shot + 2 < now)
116
    {
117
      kif_scan(kif_scan_timer);
118
      tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
119
    }
120
}
121

    
122
void
123
kif_request_scan(void)
124
{
125
  if (kif_proto && kif_scan_timer->expires > now)
126
    tm_start(kif_scan_timer, 1);
127
}
128

    
129
static struct proto *
130
kif_init(struct proto_config *c)
131
{
132
  struct kif_proto *p = proto_new(c);
133

    
134
  kif_sys_init(p);
135
  return &p->p;
136
}
137

    
138
static int
139
kif_start(struct proto *P)
140
{
141
  struct kif_proto *p = (struct kif_proto *) P;
142

    
143
  kif_proto = p;
144
  kif_sys_start(p);
145

    
146
  /* Start periodic interface scanning */
147
  kif_scan_timer = tm_new(P->pool);
148
  kif_scan_timer->hook = kif_scan;
149
  kif_scan_timer->data = p;
150
  kif_scan_timer->recurrent = KIF_CF->scan_time;
151
  kif_scan(kif_scan_timer);
152
  tm_start(kif_scan_timer, KIF_CF->scan_time);
153

    
154
  return PS_UP;
155
}
156

    
157
static int
158
kif_shutdown(struct proto *P)
159
{
160
  struct kif_proto *p = (struct kif_proto *) P;
161

    
162
  tm_stop(kif_scan_timer);
163
  kif_sys_shutdown(p);
164
  kif_proto = NULL;
165

    
166
  return PS_DOWN;
167
}
168

    
169
static int
170
kif_reconfigure(struct proto *p, struct proto_config *new)
171
{
172
  struct kif_config *o = (struct kif_config *) p->cf;
173
  struct kif_config *n = (struct kif_config *) new;
174

    
175
  if (!kif_sys_reconfigure((struct kif_proto *) p, n, o))
176
    return 0;
177

    
178
  if (o->scan_time != n->scan_time)
179
    {
180
      tm_stop(kif_scan_timer);
181
      kif_scan_timer->recurrent = n->scan_time;
182
      kif_scan(kif_scan_timer);
183
      tm_start(kif_scan_timer, n->scan_time);
184
    }
185

    
186
  if (!EMPTY_LIST(o->iface_list) || !EMPTY_LIST(n->iface_list))
187
    {
188
      /* This is hack, we have to update a configuration
189
       * to the new value just now, because it is used
190
       * for recalculation of preferred addresses.
191
       */
192
      p->cf = new;
193

    
194
      if_recalc_all_preferred_addresses();
195
    }
196

    
197
  return 1;
198
}
199

    
200

    
201
static void
202
kif_preconfig(struct protocol *P UNUSED, struct config *c)
203
{
204
  kif_cf = NULL;
205
  kif_sys_preconfig(c);
206
}
207

    
208
struct proto_config *
209
kif_init_config(int class)
210
{
211
  if (kif_cf)
212
    cf_error("Kernel device protocol already defined");
213

    
214
  kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, class);
215
  kif_cf->scan_time = 60;
216
  init_list(&kif_cf->iface_list);
217

    
218
  kif_sys_init_config(kif_cf);
219
  return (struct proto_config *) kif_cf;
220
}
221

    
222
static void
223
kif_copy_config(struct proto_config *dest, struct proto_config *src)
224
{
225
  struct kif_config *d = (struct kif_config *) dest;
226
  struct kif_config *s = (struct kif_config *) src;
227

    
228
  /* Copy interface config list */
229
  cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct kif_iface_config));
230

    
231
  /* Fix sysdep parts */
232
  kif_sys_copy_config(d, s);
233
}
234

    
235
struct protocol proto_unix_iface = {
236
  .name =                 "Device",
237
  .template =                 "device%d",
238
  .proto_size =                sizeof(struct kif_proto),
239
  .config_size =        sizeof(struct kif_config),
240
  .preconfig =                kif_preconfig,
241
  .init =                kif_init,
242
  .start =                kif_start,
243
  .shutdown =                kif_shutdown,
244
  .reconfigure =        kif_reconfigure,
245
  .copy_config =        kif_copy_config
246
};
247

    
248
/*
249
 *        Tracing of routes
250
 */
251

    
252
static inline void
253
krt_trace_in(struct krt_proto *p, rte *e, char *msg)
254
{
255
  if (p->p.debug & D_PACKETS)
256
    log(L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
257
}
258

    
259
static inline void
260
krt_trace_in_rl(struct tbf *f, struct krt_proto *p, rte *e, char *msg)
261
{
262
  if (p->p.debug & D_PACKETS)
263
    log_rl(f, L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
264
}
265

    
266
/*
267
 *        Inherited Routes
268
 */
269

    
270
#ifdef KRT_ALLOW_LEARN
271

    
272
static struct tbf rl_alien = TBF_DEFAULT_LOG_LIMITS;
273

    
274
/*
275
 * krt_same_key() specifies what (aside from the net) is the key in
276
 * kernel routing tables. It should be OS-dependent, this is for
277
 * Linux. It is important for asynchronous alien updates, because a
278
 * positive update is implicitly a negative one for any old route with
279
 * the same key.
280
 */
281

    
282
static inline int
283
krt_same_key(rte *a, rte *b)
284
{
285
  return a->u.krt.metric == b->u.krt.metric;
286
}
287

    
288
static inline int
289
krt_uptodate(rte *a, rte *b)
290
{
291
  if (a->attrs != b->attrs)
292
    return 0;
293

    
294
  if (a->u.krt.proto != b->u.krt.proto)
295
    return 0;
296

    
297
  return 1;
298
}
299

    
300
static void
301
krt_learn_announce_update(struct krt_proto *p, rte *e)
302
{
303
  net *n = e->net;
304
  rta *aa = rta_clone(e->attrs);
305
  rte *ee = rte_get_temp(aa);
306
  ee->pflags = 0;
307
  ee->u.krt = e->u.krt;
308
  rte_update(&p->p, n->n.addr, ee);
309
}
310

    
311
static void
312
krt_learn_announce_delete(struct krt_proto *p, net *n)
313
{
314
  rte_update(&p->p, n->n.addr, NULL);
315
}
316

    
317
/* Called when alien route is discovered during scan */
318
static void
319
krt_learn_scan(struct krt_proto *p, rte *e)
320
{
321
  net *n0 = e->net;
322
  net *n = net_get(&p->krt_table, n0->n.addr);
323
  rte *m, **mm;
324

    
325
  e->attrs = rta_lookup(e->attrs);
326

    
327
  for(mm=&n->routes; m = *mm; mm=&m->next)
328
    if (krt_same_key(m, e))
329
      break;
330
  if (m)
331
    {
332
      if (krt_uptodate(m, e))
333
        {
334
          krt_trace_in_rl(&rl_alien, p, e, "[alien] seen");
335
          rte_free(e);
336
          m->u.krt.seen = 1;
337
        }
338
      else
339
        {
340
          krt_trace_in(p, e, "[alien] updated");
341
          *mm = m->next;
342
          rte_free(m);
343
          m = NULL;
344
        }
345
    }
346
  else
347
    krt_trace_in(p, e, "[alien] created");
348
  if (!m)
349
    {
350
      e->next = n->routes;
351
      n->routes = e;
352
      e->u.krt.seen = 1;
353
    }
354
}
355

    
356
static void
357
krt_learn_prune(struct krt_proto *p)
358
{
359
  struct fib *fib = &p->krt_table.fib;
360
  struct fib_iterator fit;
361

    
362
  KRT_TRACE(p, D_EVENTS, "Pruning inherited routes");
363

    
364
  FIB_ITERATE_INIT(&fit, fib);
365
again:
366
  FIB_ITERATE_START(fib, &fit, net, n)
367
    {
368
      rte *e, **ee, *best, **pbest, *old_best;
369

    
370
      /*
371
       * Note that old_best may be NULL even if there was an old best route in
372
       * the previous step, because it might be replaced in krt_learn_scan().
373
       * But in that case there is a new valid best route.
374
       */
375

    
376
      old_best = NULL;
377
      best = NULL;
378
      pbest = NULL;
379
      ee = &n->routes;
380
      while (e = *ee)
381
        {
382
          if (e->u.krt.best)
383
            old_best = e;
384

    
385
          if (!e->u.krt.seen)
386
            {
387
              *ee = e->next;
388
              rte_free(e);
389
              continue;
390
            }
391

    
392
          if (!best || best->u.krt.metric > e->u.krt.metric)
393
            {
394
              best = e;
395
              pbest = ee;
396
            }
397

    
398
          e->u.krt.seen = 0;
399
          e->u.krt.best = 0;
400
          ee = &e->next;
401
        }
402
      if (!n->routes)
403
        {
404
          DBG("%I/%d: deleting\n", n->n.prefix, n->n.pxlen);
405
          if (old_best)
406
            krt_learn_announce_delete(p, n);
407

    
408
          FIB_ITERATE_PUT(&fit);
409
          fib_delete(fib, n);
410
          goto again;
411
        }
412

    
413
      best->u.krt.best = 1;
414
      *pbest = best->next;
415
      best->next = n->routes;
416
      n->routes = best;
417

    
418
      if ((best != old_best) || p->reload)
419
        {
420
          DBG("%I/%d: announcing (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
421
          krt_learn_announce_update(p, best);
422
        }
423
      else
424
        DBG("%I/%d: uptodate (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
425
    }
426
  FIB_ITERATE_END;
427

    
428
  p->reload = 0;
429
}
430

    
431
static void
432
krt_learn_async(struct krt_proto *p, rte *e, int new)
433
{
434
  net *n0 = e->net;
435
  net *n = net_get(&p->krt_table, n0->n.addr);
436
  rte *g, **gg, *best, **bestp, *old_best;
437

    
438
  e->attrs = rta_lookup(e->attrs);
439

    
440
  old_best = n->routes;
441
  for(gg=&n->routes; g = *gg; gg = &g->next)
442
    if (krt_same_key(g, e))
443
      break;
444
  if (new)
445
    {
446
      if (g)
447
        {
448
          if (krt_uptodate(g, e))
449
            {
450
              krt_trace_in(p, e, "[alien async] same");
451
              rte_free(e);
452
              return;
453
            }
454
          krt_trace_in(p, e, "[alien async] updated");
455
          *gg = g->next;
456
          rte_free(g);
457
        }
458
      else
459
        krt_trace_in(p, e, "[alien async] created");
460

    
461
      e->next = n->routes;
462
      n->routes = e;
463
    }
464
  else if (!g)
465
    {
466
      krt_trace_in(p, e, "[alien async] delete failed");
467
      rte_free(e);
468
      return;
469
    }
470
  else
471
    {
472
      krt_trace_in(p, e, "[alien async] removed");
473
      *gg = g->next;
474
      rte_free(e);
475
      rte_free(g);
476
    }
477
  best = n->routes;
478
  bestp = &n->routes;
479
  for(gg=&n->routes; g=*gg; gg=&g->next)
480
  {
481
    if (best->u.krt.metric > g->u.krt.metric)
482
      {
483
        best = g;
484
        bestp = gg;
485
      }
486

    
487
    g->u.krt.best = 0;
488
  }
489

    
490
  if (best)
491
    {
492
      best->u.krt.best = 1;
493
      *bestp = best->next;
494
      best->next = n->routes;
495
      n->routes = best;
496
    }
497

    
498
  if (best != old_best)
499
    {
500
      DBG("krt_learn_async: distributing change\n");
501
      if (best)
502
        krt_learn_announce_update(p, best);
503
      else
504
        krt_learn_announce_delete(p, n);
505
    }
506
}
507

    
508
static void
509
krt_learn_init(struct krt_proto *p)
510
{
511
  if (KRT_CF->learn)
512
    rt_setup(p->p.pool, &p->krt_table, "Inherited", NULL);
513
}
514

    
515
static void
516
krt_dump(struct proto *P)
517
{
518
  struct krt_proto *p = (struct krt_proto *) P;
519

    
520
  if (!KRT_CF->learn)
521
    return;
522
  debug("KRT: Table of inheritable routes\n");
523
  rt_dump(&p->krt_table);
524
}
525

    
526
static void
527
krt_dump_attrs(rte *e)
528
{
529
  debug(" [m=%d,p=%d]", e->u.krt.metric, e->u.krt.proto);
530
}
531

    
532
#endif
533

    
534
/*
535
 *        Routes
536
 */
537

    
538
static void
539
krt_flush_routes(struct krt_proto *p)
540
{
541
  struct rtable *t = p->p.main_channel->table;
542

    
543
  KRT_TRACE(p, D_EVENTS, "Flushing kernel routes");
544
  FIB_WALK(&t->fib, net, n)
545
    {
546
      rte *e = n->routes;
547
      if (rte_is_valid(e) && (n->n.flags & KRF_INSTALLED))
548
        {
549
          /* FIXME: this does not work if gw is changed in export filter */
550
          krt_replace_rte(p, e->net, NULL, e, NULL);
551
          n->n.flags &= ~KRF_INSTALLED;
552
        }
553
    }
554
  FIB_WALK_END;
555
}
556

    
557
static struct rte *
558
krt_export_net(struct krt_proto *p, net *net, rte **rt_free, ea_list **tmpa)
559
{
560
  struct channel *c = p->p.main_channel;
561
  struct filter *filter = c->out_filter;
562
  rte *rt;
563

    
564
  if (c->ra_mode == RA_MERGED)
565
    return rt_export_merged(c, net, rt_free, tmpa, krt_filter_lp, 1);
566

    
567
  rt = net->routes;
568
  *rt_free = NULL;
569

    
570
  if (!rte_is_valid(rt))
571
    return NULL;
572

    
573
  if (filter == FILTER_REJECT)
574
    return NULL;
575

    
576
  struct proto *src = rt->attrs->src->proto;
577
  *tmpa = src->make_tmp_attrs ? src->make_tmp_attrs(rt, krt_filter_lp) : NULL;
578

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

    
581
  if (filter == FILTER_ACCEPT)
582
    goto accept;
583

    
584
  if (f_run(filter, &rt, tmpa, krt_filter_lp, FF_FORCE_TMPATTR) > F_ACCEPT)
585
    goto reject;
586

    
587

    
588
accept:
589
  if (rt != net->routes)
590
    *rt_free = rt;
591
  return rt;
592

    
593
reject:
594
  if (rt != net->routes)
595
    rte_free(rt);
596
  return NULL;
597
}
598

    
599
static int
600
krt_same_dest(rte *k, rte *e)
601
{
602
  rta *ka = k->attrs, *ea = e->attrs;
603

    
604
  if (ka->dest != ea->dest)
605
    return 0;
606

    
607
  if (ka->dest == RTD_UNICAST)
608
    return nexthop_same(&(ka->nh), &(ea->nh));
609

    
610
  return 1;
611
}
612

    
613
/*
614
 *  This gets called back when the low-level scanning code discovers a route.
615
 *  We expect that the route is a temporary rte and its attributes are uncached.
616
 */
617

    
618
void
619
krt_got_route(struct krt_proto *p, rte *e)
620
{
621
  net *net = e->net;
622
  int verdict;
623

    
624
#ifdef KRT_ALLOW_LEARN
625
  switch (e->u.krt.src)
626
    {
627
    case KRT_SRC_KERNEL:
628
      verdict = KRF_IGNORE;
629
      goto sentenced;
630

    
631
    case KRT_SRC_REDIRECT:
632
      verdict = KRF_DELETE;
633
      goto sentenced;
634

    
635
    case  KRT_SRC_ALIEN:
636
      if (KRT_CF->learn)
637
        krt_learn_scan(p, e);
638
      else
639
        {
640
          krt_trace_in_rl(&rl_alien, p, e, "[alien] ignored");
641
          rte_free(e);
642
        }
643
      return;
644
    }
645
#endif
646
  /* The rest is for KRT_SRC_BIRD (or KRT_SRC_UNKNOWN) */
647

    
648
  if (net->n.flags & KRF_VERDICT_MASK)
649
    {
650
      /* Route to this destination was already seen. Strange, but it happens... */
651
      krt_trace_in(p, e, "already seen");
652
      rte_free(e);
653
      return;
654
    }
655

    
656
  if (!p->ready)
657
    {
658
      /* We wait for the initial feed to have correct KRF_INSTALLED flag */
659
      verdict = KRF_IGNORE;
660
      goto sentenced;
661
    }
662

    
663
  if (net->n.flags & KRF_INSTALLED)
664
    {
665
      rte *new, *rt_free;
666
      ea_list *tmpa;
667

    
668
      new = krt_export_net(p, net, &rt_free, &tmpa);
669

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

    
672
      if (!new)
673
        verdict = KRF_DELETE;
674
      else if ((net->n.flags & KRF_SYNC_ERROR) || !krt_same_dest(e, new))
675
        verdict = KRF_UPDATE;
676
      else
677
        verdict = KRF_SEEN;
678

    
679
      if (rt_free)
680
        rte_free(rt_free);
681

    
682
      lp_flush(krt_filter_lp);
683
    }
684
  else
685
    verdict = KRF_DELETE;
686

    
687
 sentenced:
688
  krt_trace_in(p, e, ((char *[]) { "?", "seen", "will be updated", "will be removed", "ignored" }) [verdict]);
689
  net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
690
  if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
691
    {
692
      /* Get a cached copy of attributes and temporarily link the route */
693
      rta *a = e->attrs;
694
      a->source = RTS_DUMMY;
695
      e->attrs = rta_lookup(a);
696
      e->next = net->routes;
697
      net->routes = e;
698
    }
699
  else
700
    rte_free(e);
701
}
702

    
703
static void
704
krt_prune(struct krt_proto *p)
705
{
706
  struct rtable *t = p->p.main_channel->table;
707

    
708
  KRT_TRACE(p, D_EVENTS, "Pruning table %s", t->name);
709
  FIB_WALK(&t->fib, net, n)
710
    {
711
      int verdict = n->n.flags & KRF_VERDICT_MASK;
712
      rte *new, *old, *rt_free = NULL;
713
      ea_list *tmpa = NULL;
714

    
715
      if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
716
        {
717
          /* Get a dummy route from krt_got_route() */
718
          old = n->routes;
719
          n->routes = old->next;
720
        }
721
      else
722
        old = NULL;
723

    
724
      if (verdict == KRF_CREATE || verdict == KRF_UPDATE)
725
        {
726
          /* We have to run export filter to get proper 'new' route */
727
          new = krt_export_net(p, n, &rt_free, &tmpa);
728

    
729
          if (!new)
730
            verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
731
          else
732
            tmpa = ea_append(tmpa, new->attrs->eattrs);
733
        }
734
      else
735
        new = NULL;
736

    
737
      switch (verdict)
738
        {
739
        case KRF_CREATE:
740
          if (new && (n->n.flags & KRF_INSTALLED))
741
            {
742
              krt_trace_in(p, new, "reinstalling");
743
              krt_replace_rte(p, n, new, NULL, tmpa);
744
            }
745
          break;
746
        case KRF_SEEN:
747
        case KRF_IGNORE:
748
          /* Nothing happens */
749
          break;
750
        case KRF_UPDATE:
751
          krt_trace_in(p, new, "updating");
752
          krt_replace_rte(p, n, new, old, tmpa);
753
          break;
754
        case KRF_DELETE:
755
          krt_trace_in(p, old, "deleting");
756
          krt_replace_rte(p, n, NULL, old, NULL);
757
          break;
758
        default:
759
          bug("krt_prune: invalid route status");
760
        }
761

    
762
      if (old)
763
        rte_free(old);
764
      if (rt_free)
765
        rte_free(rt_free);
766
      lp_flush(krt_filter_lp);
767
      n->n.flags &= ~KRF_VERDICT_MASK;
768
    }
769
  FIB_WALK_END;
770

    
771
#ifdef KRT_ALLOW_LEARN
772
  if (KRT_CF->learn)
773
    krt_learn_prune(p);
774
#endif
775

    
776
  if (p->ready)
777
    p->initialized = 1;
778
}
779

    
780
void
781
krt_got_route_async(struct krt_proto *p, rte *e, int new)
782
{
783
  net *net = e->net;
784

    
785
  switch (e->u.krt.src)
786
    {
787
    case KRT_SRC_BIRD:
788
      ASSERT(0);                        /* Should be filtered by the back end */
789

    
790
    case KRT_SRC_REDIRECT:
791
      if (new)
792
        {
793
          krt_trace_in(p, e, "[redirect] deleting");
794
          krt_replace_rte(p, net, NULL, e, NULL);
795
        }
796
      /* If !new, it is probably echo of our deletion */
797
      break;
798

    
799
#ifdef KRT_ALLOW_LEARN
800
    case KRT_SRC_ALIEN:
801
      if (KRT_CF->learn)
802
        {
803
          krt_learn_async(p, e, new);
804
          return;
805
        }
806
#endif
807
    }
808
  rte_free(e);
809
}
810

    
811
/*
812
 *        Periodic scanning
813
 */
814

    
815

    
816
#ifdef CONFIG_ALL_TABLES_AT_ONCE
817

    
818
static timer *krt_scan_timer;
819
static int krt_scan_count;
820

    
821
static void
822
krt_scan(timer *t UNUSED)
823
{
824
  struct krt_proto *p;
825

    
826
  kif_force_scan();
827

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

    
832
  krt_do_scan(NULL);
833

    
834
  void *q;
835
  WALK_LIST(q, krt_proto_list)
836
  {
837
    p = SKIP_BACK(struct krt_proto, krt_node, q);
838
    krt_prune(p);
839
  }
840
}
841

    
842
static void
843
krt_scan_timer_start(struct krt_proto *p)
844
{
845
  if (!krt_scan_count)
846
    krt_scan_timer = tm_new_set(krt_pool, krt_scan, NULL, 0, KRT_CF->scan_time);
847

    
848
  krt_scan_count++;
849

    
850
  tm_start(krt_scan_timer, 1);
851
}
852

    
853
static void
854
krt_scan_timer_stop(struct krt_proto *p UNUSED)
855
{
856
  krt_scan_count--;
857

    
858
  if (!krt_scan_count)
859
  {
860
    rfree(krt_scan_timer);
861
    krt_scan_timer = NULL;
862
  }
863
}
864

    
865
static void
866
krt_scan_timer_kick(struct krt_proto *p UNUSED)
867
{
868
  tm_start(krt_scan_timer, 0);
869
}
870

    
871
#else
872

    
873
static void
874
krt_scan(timer *t)
875
{
876
  struct krt_proto *p = t->data;
877

    
878
  kif_force_scan();
879

    
880
  KRT_TRACE(p, D_EVENTS, "Scanning routing table");
881
  krt_do_scan(p);
882
  krt_prune(p);
883
}
884

    
885
static void
886
krt_scan_timer_start(struct krt_proto *p)
887
{
888
  p->scan_timer = tm_new_set(p->p.pool, krt_scan, p, 0, KRT_CF->scan_time);
889
  tm_start(p->scan_timer, 1);
890
}
891

    
892
static void
893
krt_scan_timer_stop(struct krt_proto *p)
894
{
895
  tm_stop(p->scan_timer);
896
}
897

    
898
static void
899
krt_scan_timer_kick(struct krt_proto *p)
900
{
901
  tm_start(p->scan_timer, 0);
902
}
903

    
904
#endif
905

    
906

    
907

    
908

    
909
/*
910
 *        Updates
911
 */
912

    
913
static struct ea_list *
914
krt_make_tmp_attrs(rte *rt, struct linpool *pool)
915
{
916
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2 * sizeof(eattr));
917

    
918
  l->next = NULL;
919
  l->flags = EALF_SORTED;
920
  l->count = 2;
921

    
922
  l->attrs[0].id = EA_KRT_SOURCE;
923
  l->attrs[0].flags = 0;
924
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
925
  l->attrs[0].u.data = rt->u.krt.proto;
926

    
927
  l->attrs[1].id = EA_KRT_METRIC;
928
  l->attrs[1].flags = 0;
929
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
930
  l->attrs[1].u.data = rt->u.krt.metric;
931

    
932
  return l;
933
}
934

    
935
static void
936
krt_store_tmp_attrs(rte *rt, struct ea_list *attrs)
937
{
938
  /* EA_KRT_SOURCE is read-only */
939
  rt->u.krt.metric = ea_get_int(attrs, EA_KRT_METRIC, 0);
940
}
941

    
942
static int
943
krt_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
944
{
945
  // struct krt_proto *p = (struct krt_proto *) P;
946
  rte *e = *new;
947

    
948
  if (e->attrs->src->proto == P)
949
  {
950
#ifdef CONFIG_SINGLE_ROUTE
951
    /*
952
     * Implicit withdraw - when the imported kernel route becomes the best one,
953
     * we know that the previous one exported to the kernel was already removed,
954
     * but if we processed the update as usual, we would send withdraw to the
955
     * kernel, which would remove the new imported route instead.
956
     *
957
     * We will remove KRT_INSTALLED flag, which stops such withdraw to be
958
     * processed in krt_rt_notify() and krt_replace_rte().
959
     */
960
    if (e == e->net->routes)
961
      e->net->n.flags &= ~KRF_INSTALLED;
962
#endif
963
    return -1;
964
  }
965

    
966
  if (!krt_capable(e))
967
    return -1;
968

    
969
  return 0;
970
}
971

    
972
static void
973
krt_rt_notify(struct proto *P, struct channel *ch UNUSED, net *net,
974
              rte *new, rte *old, struct ea_list *eattrs)
975
{
976
  struct krt_proto *p = (struct krt_proto *) P;
977

    
978
  if (config->shutdown)
979
    return;
980
  if (!(net->n.flags & KRF_INSTALLED))
981
    old = NULL;
982
  if (new)
983
    net->n.flags |= KRF_INSTALLED;
984
  else
985
    net->n.flags &= ~KRF_INSTALLED;
986
  if (p->initialized)                /* Before first scan we don't touch the routes */
987
    krt_replace_rte(p, net, new, old, eattrs);
988
}
989

    
990
static void
991
krt_if_notify(struct proto *P, uint flags, struct iface *iface UNUSED)
992
{
993
  struct krt_proto *p = (struct krt_proto *) P;
994

    
995
  /*
996
   * When interface went down, we should remove routes to it. In the ideal world,
997
   * OS kernel would send us route removal notifications in such cases, but we
998
   * cannot rely on it as it is often not true. E.g. Linux kernel removes related
999
   * routes when an interface went down, but it does not notify userspace about
1000
   * that. To be sure, we just schedule a scan to ensure synchronization.
1001
   */
1002

    
1003
  if ((flags & IF_CHANGE_DOWN) && KRT_CF->learn)
1004
    krt_scan_timer_kick(p);
1005
}
1006

    
1007
static void
1008
krt_reload_routes(struct channel *C)
1009
{
1010
  struct krt_proto *p = (void *) C->proto;
1011

    
1012
  /* Although we keep learned routes in krt_table, we rather schedule a scan */
1013

    
1014
  if (KRT_CF->learn)
1015
  {
1016
    p->reload = 1;
1017
    krt_scan_timer_kick(p);
1018
  }
1019
}
1020

    
1021
static void
1022
krt_feed_end(struct channel *C)
1023
{
1024
  struct krt_proto *p = (void *) C->proto;
1025

    
1026
  p->ready = 1;
1027
  krt_scan_timer_kick(p);
1028
}
1029

    
1030

    
1031
static int
1032
krt_rte_same(rte *a, rte *b)
1033
{
1034
  /* src is always KRT_SRC_ALIEN and type is irrelevant */
1035
  return (a->u.krt.proto == b->u.krt.proto) && (a->u.krt.metric == b->u.krt.metric);
1036
}
1037

    
1038

    
1039
/*
1040
 *        Protocol glue
1041
 */
1042

    
1043
struct krt_config *krt_cf;
1044

    
1045
static void
1046
krt_preconfig(struct protocol *P UNUSED, struct config *c)
1047
{
1048
  krt_cf = NULL;
1049
  krt_sys_preconfig(c);
1050
}
1051

    
1052
static void
1053
krt_postconfig(struct proto_config *CF)
1054
{
1055
  struct krt_config *cf = (void *) CF;
1056

    
1057
  if (EMPTY_LIST(CF->channels))
1058
    cf_error("Channel not specified");
1059

    
1060
#ifdef CONFIG_ALL_TABLES_AT_ONCE
1061
  if (krt_cf->scan_time != cf->scan_time)
1062
    cf_error("All kernel syncers must use the same table scan interval");
1063
#endif
1064

    
1065
  struct rtable_config *tab = proto_cf_main_channel(CF)->table;
1066
  if (tab->krt_attached)
1067
    cf_error("Kernel syncer (%s) already attached to table %s", tab->krt_attached->name, tab->name);
1068
  tab->krt_attached = CF;
1069

    
1070
  krt_sys_postconfig(cf);
1071
}
1072

    
1073
static struct proto *
1074
krt_init(struct proto_config *CF)
1075
{
1076
  struct krt_proto *p = proto_new(CF);
1077
  // struct krt_config *cf = (void *) CF;
1078

    
1079
  p->p.main_channel = proto_add_channel(&p->p, proto_cf_main_channel(CF));
1080

    
1081
  p->p.import_control = krt_import_control;
1082
  p->p.rt_notify = krt_rt_notify;
1083
  p->p.if_notify = krt_if_notify;
1084
  p->p.reload_routes = krt_reload_routes;
1085
  p->p.feed_end = krt_feed_end;
1086
  p->p.make_tmp_attrs = krt_make_tmp_attrs;
1087
  p->p.store_tmp_attrs = krt_store_tmp_attrs;
1088
  p->p.rte_same = krt_rte_same;
1089

    
1090
  krt_sys_init(p);
1091
  return &p->p;
1092
}
1093

    
1094
static int
1095
krt_start(struct proto *P)
1096
{
1097
  struct krt_proto *p = (struct krt_proto *) P;
1098

    
1099
  switch (p->p.net_type)
1100
  {
1101
  case NET_IP4:        p->af = AF_INET; break;
1102
  case NET_IP6:        p->af = AF_INET6; break;
1103
#ifdef AF_MPLS
1104
  case NET_MPLS: p->af = AF_MPLS; break;
1105
#endif
1106
  default: log(L_ERR "KRT: Tried to start with strange net type: %d", p->p.net_type); return PS_START; break;
1107
  }
1108

    
1109
  add_tail(&krt_proto_list, &p->krt_node);
1110

    
1111
#ifdef KRT_ALLOW_LEARN
1112
  krt_learn_init(p);
1113
#endif
1114

    
1115
  if (!krt_sys_start(p))
1116
  {
1117
    rem_node(&p->krt_node);
1118
    return PS_START;
1119
  }
1120

    
1121
  krt_scan_timer_start(p);
1122

    
1123
  if (p->p.gr_recovery && KRT_CF->graceful_restart)
1124
    p->p.main_channel->gr_wait = 1;
1125

    
1126
  return PS_UP;
1127
}
1128

    
1129
static int
1130
krt_shutdown(struct proto *P)
1131
{
1132
  struct krt_proto *p = (struct krt_proto *) P;
1133

    
1134
  krt_scan_timer_stop(p);
1135

    
1136
  /* FIXME we should flush routes even when persist during reconfiguration */
1137
  if (p->initialized && !KRT_CF->persist)
1138
    krt_flush_routes(p);
1139

    
1140
  p->ready = 0;
1141
  p->initialized = 0;
1142

    
1143
  if (p->p.proto_state == PS_START)
1144
    return PS_DOWN;
1145

    
1146
  krt_sys_shutdown(p);
1147
  rem_node(&p->krt_node);
1148

    
1149
  return PS_DOWN;
1150
}
1151

    
1152
static int
1153
krt_reconfigure(struct proto *p, struct proto_config *CF)
1154
{
1155
  struct krt_config *o = (void *) p->cf;
1156
  struct krt_config *n = (void *) CF;
1157

    
1158
  if (!proto_configure_channel(p, &p->main_channel, proto_cf_main_channel(CF)))
1159
    return 0;
1160

    
1161
  if (!krt_sys_reconfigure((struct krt_proto *) p, n, o))
1162
    return 0;
1163

    
1164
  /* persist, graceful restart need not be the same */
1165
  return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes;
1166
}
1167

    
1168
struct proto_config *
1169
krt_init_config(int class)
1170
{
1171
#ifndef CONFIG_MULTIPLE_TABLES
1172
  if (krt_cf)
1173
    cf_error("Kernel protocol already defined");
1174
#endif
1175

    
1176
  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
1177
  krt_cf->scan_time = 60;
1178

    
1179
  krt_sys_init_config(krt_cf);
1180
  return (struct proto_config *) krt_cf;
1181
}
1182

    
1183
static void
1184
krt_copy_config(struct proto_config *dest, struct proto_config *src)
1185
{
1186
  struct krt_config *d = (struct krt_config *) dest;
1187
  struct krt_config *s = (struct krt_config *) src;
1188

    
1189
  /* Fix sysdep parts */
1190
  krt_sys_copy_config(d, s);
1191
}
1192

    
1193
static int
1194
krt_get_attr(eattr *a, byte *buf, int buflen)
1195
{
1196
  switch (a->id)
1197
  {
1198
  case EA_KRT_SOURCE:
1199
    bsprintf(buf, "source");
1200
    return GA_NAME;
1201

    
1202
  case EA_KRT_METRIC:
1203
    bsprintf(buf, "metric");
1204
    return GA_NAME;
1205

    
1206
  default:
1207
    return krt_sys_get_attr(a, buf, buflen);
1208
  }
1209
}
1210

    
1211

    
1212
struct protocol proto_unix_kernel = {
1213
  .name =                "Kernel",
1214
  .template =                "kernel%d",
1215
  .attr_class =                EAP_KRT,
1216
  .preference =                DEF_PREF_INHERITED,
1217
  .channel_mask =        NB_IP | NB_MPLS,
1218
  .proto_size =                sizeof(struct krt_proto),
1219
  .config_size =        sizeof(struct krt_config),
1220
  .preconfig =                krt_preconfig,
1221
  .postconfig =                krt_postconfig,
1222
  .init =                krt_init,
1223
  .start =                krt_start,
1224
  .shutdown =                krt_shutdown,
1225
  .reconfigure =        krt_reconfigure,
1226
  .copy_config =        krt_copy_config,
1227
  .get_attr =                krt_get_attr,
1228
#ifdef KRT_ALLOW_LEARN
1229
  .dump =                krt_dump,
1230
  .dump_attrs =                krt_dump_attrs,
1231
#endif
1232
};