Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (26.4 KB)

1 2d140452 Martin Mares
/*
2
 *        BIRD -- UNIX Kernel Synchronization
3
 *
4 50fe90ed Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 2d140452 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 73275d85 Martin Mares
/**
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 2e9b2421 Martin Mares
 * to keep the overhead of our KRT business as low as possible and avoid maintaining
20 73275d85 Martin Mares
 * a local routing table copy.
21
 *
22
 * The kernel syncer can work in three different modes (according to system config header):
23 725270cb Martin Mares
 * Either with a single routing table and single KRT protocol [traditional UNIX]
24 2e9b2421 Martin Mares
 * or with many routing tables and separate KRT protocols for all of them
25 73275d85 Martin Mares
 * or with many routing tables, but every scan including all tables, so we start
26 f1aceff5 Ondrej Zajicek
 * separate KRT protocols which cooperate with each other [Linux].
27 73275d85 Martin Mares
 * In this case, we keep only a single scan timer.
28
 *
29 58f7d004 Martin Mares
 * 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 73275d85 Martin Mares
 *
34
 * When starting up, we cheat by looking if there is another
35
 * KRT instance to be initialized later and performing table scan
36 725270cb Martin Mares
 * only once for all the instances.
37 f1aceff5 Ondrej Zajicek
 *
38
 * The code uses OS-dependent parts for kernel updates and scans. These parts are
39 252c7e4d Ondrej Zajicek
 * in more specific sysdep directories (e.g. sysdep/linux) in functions krt_sys_*
40 7a2c48da Ondrej Zajicek
 * and kif_sys_* (and some others like krt_replace_rte()) and krt-sys.h header file.
41 f1aceff5 Ondrej Zajicek
 * 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 725270cb Martin Mares
 */
47 73275d85 Martin Mares
48
/*
49
 *  If you are brave enough, continue now.  You cannot say you haven't been warned.
50
 */
51
52 832fa033 Martin Mares
#undef LOCAL_DEBUG
53 2d140452 Martin Mares
54
#include "nest/bird.h"
55
#include "nest/iface.h"
56
#include "nest/route.h"
57
#include "nest/protocol.h"
58 c9df01d3 Ondrej Zajicek
#include "filter/filter.h"
59 7152e5ef Jan Moskyto Matejka
#include "sysdep/unix/timer.h"
60 7de45ba4 Martin Mares
#include "conf/conf.h"
61 7d875e09 Martin Mares
#include "lib/string.h"
62 2d140452 Martin Mares
63
#include "unix.h"
64
#include "krt.h"
65
66 7e5f5ffd Martin Mares
/*
67
 *        Global resources
68
 */
69
70 7de45ba4 Martin Mares
pool *krt_pool;
71 c9df01d3 Ondrej Zajicek
static linpool *krt_filter_lp;
72 c6964c30 Ondrej Zajicek
static list krt_proto_list;
73 7de45ba4 Martin Mares
74 7e5f5ffd Martin Mares
void
75
krt_io_init(void)
76
{
77 7de45ba4 Martin Mares
  krt_pool = rp_new(&root_pool, "Kernel Syncer");
78 05d47bd5 Jan Moskyto Matejka
  krt_filter_lp = lp_new_default(krt_pool);
79 c6964c30 Ondrej Zajicek
  init_list(&krt_proto_list);
80 9ddbfbdd Jan Moskyto Matejka
  krt_sys_io_init();
81 7e5f5ffd Martin Mares
}
82
83
/*
84
 *        Interfaces
85
 */
86
87 1e4891e4 Ondrej Zajicek (work)
struct kif_proto *kif_proto;
88 396dfa90 Ondrej Zajicek
static struct kif_config *kif_cf;
89 7e5f5ffd Martin Mares
static timer *kif_scan_timer;
90
static bird_clock_t kif_last_shot;
91
92 153f02da Ondrej Zajicek (work)
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 7e5f5ffd Martin Mares
static void
103
kif_scan(timer *t)
104
{
105
  struct kif_proto *p = t->data;
106
107 832fa033 Martin Mares
  KRT_TRACE(p, D_EVENTS, "Scanning interfaces");
108 7e5f5ffd Martin Mares
  kif_last_shot = now;
109 396dfa90 Ondrej Zajicek
  kif_do_scan(p);
110 7e5f5ffd Martin Mares
}
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 09686693 Ondrej Zajicek
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 396dfa90 Ondrej Zajicek
static struct proto *
130
kif_init(struct proto_config *c)
131
{
132 f4a60a9b Ondrej Zajicek (work)
  struct kif_proto *p = proto_new(c);
133 396dfa90 Ondrej Zajicek
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 f7fcb752 Martin Mares
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 396dfa90 Ondrej Zajicek
  if (!kif_sys_reconfigure((struct kif_proto *) p, n, o))
176 f7fcb752 Martin Mares
    return 0;
177 874b8685 Ondrej Zajicek
178 f7fcb752 Martin Mares
  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 874b8685 Ondrej Zajicek
186 153f02da Ondrej Zajicek (work)
  if (!EMPTY_LIST(o->iface_list) || !EMPTY_LIST(n->iface_list))
187 874b8685 Ondrej Zajicek
    {
188
      /* This is hack, we have to update a configuration
189
       * to the new value just now, because it is used
190 153f02da Ondrej Zajicek (work)
       * for recalculation of preferred addresses.
191 874b8685 Ondrej Zajicek
       */
192
      p->cf = new;
193
194 153f02da Ondrej Zajicek (work)
      if_recalc_all_preferred_addresses();
195 874b8685 Ondrej Zajicek
    }
196
197 f7fcb752 Martin Mares
  return 1;
198
}
199
200 396dfa90 Ondrej Zajicek
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 2bbc3083 Ondrej Zajicek
  kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, class);
215 396dfa90 Ondrej Zajicek
  kif_cf->scan_time = 60;
216 153f02da Ondrej Zajicek (work)
  init_list(&kif_cf->iface_list);
217 396dfa90 Ondrej Zajicek
218
  kif_sys_init_config(kif_cf);
219
  return (struct proto_config *) kif_cf;
220
}
221
222 a7f23f58 Ondrej Zajicek
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 153f02da Ondrej Zajicek (work)
  /* Copy interface config list */
229
  cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct kif_iface_config));
230 a7f23f58 Ondrej Zajicek
231
  /* Fix sysdep parts */
232 396dfa90 Ondrej Zajicek
  kif_sys_copy_config(d, s);
233 a7f23f58 Ondrej Zajicek
}
234
235 7e5f5ffd Martin Mares
struct protocol proto_unix_iface = {
236 4a591d4b Pavel Tvrdik
  .name =                 "Device",
237
  .template =                 "device%d",
238 f4a60a9b Ondrej Zajicek (work)
  .proto_size =                sizeof(struct kif_proto),
239 2bbc3083 Ondrej Zajicek
  .config_size =        sizeof(struct kif_config),
240 4a591d4b Pavel Tvrdik
  .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 7e5f5ffd Martin Mares
};
247 2d140452 Martin Mares
248
/*
249 832fa033 Martin Mares
 *        Tracing of routes
250
 */
251
252 cb530392 Ondrej Zajicek
static inline void
253
krt_trace_in(struct krt_proto *p, rte *e, char *msg)
254 832fa033 Martin Mares
{
255 cb530392 Ondrej Zajicek
  if (p->p.debug & D_PACKETS)
256 fe9f1a6d Ondrej Zajicek (work)
    log(L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
257 832fa033 Martin Mares
}
258
259
static inline void
260 1123e707 Ondrej Zajicek
krt_trace_in_rl(struct tbf *f, struct krt_proto *p, rte *e, char *msg)
261 832fa033 Martin Mares
{
262
  if (p->p.debug & D_PACKETS)
263 fe9f1a6d Ondrej Zajicek (work)
    log_rl(f, L_TRACE "%s: %N: %s", p->p.name, e->net->n.addr, msg);
264 832fa033 Martin Mares
}
265
266
/*
267 c10421d3 Martin Mares
 *        Inherited Routes
268
 */
269
270
#ifdef KRT_ALLOW_LEARN
271
272 1123e707 Ondrej Zajicek
static struct tbf rl_alien = TBF_DEFAULT_LOG_LIMITS;
273 cb530392 Ondrej Zajicek
274 c9df01d3 Ondrej Zajicek
/*
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 c10421d3 Martin Mares
static inline int
283
krt_same_key(rte *a, rte *b)
284
{
285 c9df01d3 Ondrej Zajicek
  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 c10421d3 Martin Mares
}
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 2003a184 Jan Moskyto Matejka
  rte_update(&p->p, n->n.addr, ee);
309 c10421d3 Martin Mares
}
310
311
static void
312
krt_learn_announce_delete(struct krt_proto *p, net *n)
313
{
314 2003a184 Jan Moskyto Matejka
  rte_update(&p->p, n->n.addr, NULL);
315 c10421d3 Martin Mares
}
316
317 c9df01d3 Ondrej Zajicek
/* Called when alien route is discovered during scan */
318 c10421d3 Martin Mares
static void
319
krt_learn_scan(struct krt_proto *p, rte *e)
320
{
321
  net *n0 = e->net;
322 fe9f1a6d Ondrej Zajicek (work)
  net *n = net_get(&p->krt_table, n0->n.addr);
323 c10421d3 Martin Mares
  rte *m, **mm;
324
325 c9df01d3 Ondrej Zajicek
  e->attrs = rta_lookup(e->attrs);
326 c10421d3 Martin Mares
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 1123e707 Ondrej Zajicek
          krt_trace_in_rl(&rl_alien, p, e, "[alien] seen");
335 c10421d3 Martin Mares
          rte_free(e);
336
          m->u.krt.seen = 1;
337
        }
338
      else
339
        {
340 1123e707 Ondrej Zajicek
          krt_trace_in(p, e, "[alien] updated");
341 c10421d3 Martin Mares
          *mm = m->next;
342
          rte_free(m);
343
          m = NULL;
344
        }
345
    }
346
  else
347 1123e707 Ondrej Zajicek
    krt_trace_in(p, e, "[alien] created");
348 c10421d3 Martin Mares
  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 832fa033 Martin Mares
  KRT_TRACE(p, D_EVENTS, "Pruning inherited routes");
363 c10421d3 Martin Mares
364
  FIB_ITERATE_INIT(&fit, fib);
365
again:
366 600998fc Ondrej Zajicek (work)
  FIB_ITERATE_START(fib, &fit, net, n)
367 c10421d3 Martin Mares
    {
368
      rte *e, **ee, *best, **pbest, *old_best;
369
370 e86cfd41 Ondrej Zajicek (work)
      /*
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 c10421d3 Martin Mares
      best = NULL;
378
      pbest = NULL;
379
      ee = &n->routes;
380
      while (e = *ee)
381
        {
382 e86cfd41 Ondrej Zajicek (work)
          if (e->u.krt.best)
383
            old_best = e;
384
385 c10421d3 Martin Mares
          if (!e->u.krt.seen)
386
            {
387
              *ee = e->next;
388
              rte_free(e);
389
              continue;
390
            }
391 e86cfd41 Ondrej Zajicek (work)
392 c10421d3 Martin Mares
          if (!best || best->u.krt.metric > e->u.krt.metric)
393
            {
394
              best = e;
395
              pbest = ee;
396
            }
397 e86cfd41 Ondrej Zajicek (work)
398 c10421d3 Martin Mares
          e->u.krt.seen = 0;
399 e86cfd41 Ondrej Zajicek (work)
          e->u.krt.best = 0;
400 c10421d3 Martin Mares
          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 e86cfd41 Ondrej Zajicek (work)
            krt_learn_announce_delete(p, n);
407
408 600998fc Ondrej Zajicek (work)
          FIB_ITERATE_PUT(&fit);
409
          fib_delete(fib, n);
410 c10421d3 Martin Mares
          goto again;
411
        }
412 e86cfd41 Ondrej Zajicek (work)
413
      best->u.krt.best = 1;
414 c10421d3 Martin Mares
      *pbest = best->next;
415
      best->next = n->routes;
416
      n->routes = best;
417 e86cfd41 Ondrej Zajicek (work)
418
      if ((best != old_best) || p->reload)
419 c10421d3 Martin Mares
        {
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 600998fc Ondrej Zajicek (work)
  FIB_ITERATE_END;
427 7069fc9e Ondrej Zajicek
428
  p->reload = 0;
429 c10421d3 Martin Mares
}
430
431
static void
432
krt_learn_async(struct krt_proto *p, rte *e, int new)
433
{
434
  net *n0 = e->net;
435 fe9f1a6d Ondrej Zajicek (work)
  net *n = net_get(&p->krt_table, n0->n.addr);
436 c10421d3 Martin Mares
  rte *g, **gg, *best, **bestp, *old_best;
437
438 c9df01d3 Ondrej Zajicek
  e->attrs = rta_lookup(e->attrs);
439 c10421d3 Martin Mares
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 832fa033 Martin Mares
              krt_trace_in(p, e, "[alien async] same");
451 c10421d3 Martin Mares
              rte_free(e);
452
              return;
453
            }
454 832fa033 Martin Mares
          krt_trace_in(p, e, "[alien async] updated");
455 c10421d3 Martin Mares
          *gg = g->next;
456
          rte_free(g);
457
        }
458
      else
459 832fa033 Martin Mares
        krt_trace_in(p, e, "[alien async] created");
460 c9df01d3 Ondrej Zajicek
461 c10421d3 Martin Mares
      e->next = n->routes;
462
      n->routes = e;
463
    }
464
  else if (!g)
465
    {
466 832fa033 Martin Mares
      krt_trace_in(p, e, "[alien async] delete failed");
467 c10421d3 Martin Mares
      rte_free(e);
468
      return;
469
    }
470
  else
471
    {
472 832fa033 Martin Mares
      krt_trace_in(p, e, "[alien async] removed");
473 c10421d3 Martin Mares
      *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 e86cfd41 Ondrej Zajicek (work)
  {
481 c10421d3 Martin Mares
    if (best->u.krt.metric > g->u.krt.metric)
482
      {
483
        best = g;
484
        bestp = gg;
485
      }
486 e86cfd41 Ondrej Zajicek (work)
487
    g->u.krt.best = 0;
488
  }
489
490 c10421d3 Martin Mares
  if (best)
491
    {
492 e86cfd41 Ondrej Zajicek (work)
      best->u.krt.best = 1;
493 c10421d3 Martin Mares
      *bestp = best->next;
494
      best->next = n->routes;
495
      n->routes = best;
496
    }
497 e86cfd41 Ondrej Zajicek (work)
498 c10421d3 Martin Mares
  if (best != old_best)
499
    {
500
      DBG("krt_learn_async: distributing change\n");
501
      if (best)
502 e86cfd41 Ondrej Zajicek (work)
        krt_learn_announce_update(p, best);
503 c10421d3 Martin Mares
      else
504 e86cfd41 Ondrej Zajicek (work)
        krt_learn_announce_delete(p, n);
505 c10421d3 Martin Mares
    }
506
}
507
508
static void
509
krt_learn_init(struct krt_proto *p)
510
{
511
  if (KRT_CF->learn)
512 b9626ec6 Martin Mares
    rt_setup(p->p.pool, &p->krt_table, "Inherited", NULL);
513 c10421d3 Martin Mares
}
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 e86cfd41 Ondrej Zajicek (work)
  debug(" [m=%d,p=%d]", e->u.krt.metric, e->u.krt.proto);
530 c10421d3 Martin Mares
}
531
532
#endif
533
534
/*
535 2d140452 Martin Mares
 *        Routes
536
 */
537
538
static void
539
krt_flush_routes(struct krt_proto *p)
540
{
541 f4a60a9b Ondrej Zajicek (work)
  struct rtable *t = p->p.main_channel->table;
542 2d140452 Martin Mares
543 832fa033 Martin Mares
  KRT_TRACE(p, D_EVENTS, "Flushing kernel routes");
544 600998fc Ondrej Zajicek (work)
  FIB_WALK(&t->fib, net, n)
545 2d140452 Martin Mares
    {
546
      rte *e = n->routes;
547 cf98be7b Ondrej Zajicek
      if (rte_is_valid(e) && (n->n.flags & KRF_INSTALLED))
548 2d140452 Martin Mares
        {
549 e14bd380 Ondrej Zajicek
          /* 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 2d140452 Martin Mares
        }
553
    }
554
  FIB_WALK_END;
555
}
556
557 78a2cc28 Ondrej Zajicek
static struct rte *
558
krt_export_net(struct krt_proto *p, net *net, rte **rt_free, ea_list **tmpa)
559
{
560 f4a60a9b Ondrej Zajicek (work)
  struct channel *c = p->p.main_channel;
561
  struct filter *filter = c->out_filter;
562 78a2cc28 Ondrej Zajicek
  rte *rt;
563
564 f4a60a9b Ondrej Zajicek (work)
  if (c->ra_mode == RA_MERGED)
565 cc5b93f7 Ondrej Zajicek (work)
    return rt_export_merged(c, net, rt_free, tmpa, krt_filter_lp, 1);
566 8d9eef17 Ondrej Zajicek
567 78a2cc28 Ondrej Zajicek
  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 2d140452 Martin Mares
static int
600 c9df01d3 Ondrej Zajicek
krt_same_dest(rte *k, rte *e)
601 2d140452 Martin Mares
{
602
  rta *ka = k->attrs, *ea = e->attrs;
603
604
  if (ka->dest != ea->dest)
605
    return 0;
606 4e276a89 Jan Moskyto Matejka
607
  if (ka->dest == RTD_UNICAST)
608
    return nexthop_same(&(ka->nh), &(ea->nh));
609
610
  return 1;
611 2d140452 Martin Mares
}
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 ff2857b0 Ondrej Zajicek
#ifdef KRT_ALLOW_LEARN
625
  switch (e->u.krt.src)
626 c10421d3 Martin Mares
    {
627 ff2857b0 Ondrej Zajicek
    case KRT_SRC_KERNEL:
628 c10421d3 Martin Mares
      verdict = KRF_IGNORE;
629
      goto sentenced;
630
631 ff2857b0 Ondrej Zajicek
    case KRT_SRC_REDIRECT:
632
      verdict = KRF_DELETE;
633
      goto sentenced;
634
635
    case  KRT_SRC_ALIEN:
636 c10421d3 Martin Mares
      if (KRT_CF->learn)
637
        krt_learn_scan(p, e);
638
      else
639 c197d44e Martin Mares
        {
640 1123e707 Ondrej Zajicek
          krt_trace_in_rl(&rl_alien, p, e, "[alien] ignored");
641 c197d44e Martin Mares
          rte_free(e);
642
        }
643 c10421d3 Martin Mares
      return;
644
    }
645
#endif
646 ff2857b0 Ondrej Zajicek
  /* The rest is for KRT_SRC_BIRD (or KRT_SRC_UNKNOWN) */
647 c10421d3 Martin Mares
648
  if (net->n.flags & KRF_VERDICT_MASK)
649 2d140452 Martin Mares
    {
650
      /* Route to this destination was already seen. Strange, but it happens... */
651 832fa033 Martin Mares
      krt_trace_in(p, e, "already seen");
652 c197d44e Martin Mares
      rte_free(e);
653 2d140452 Martin Mares
      return;
654
    }
655
656 0c791f87 Ondrej Zajicek
  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 78a2cc28 Ondrej Zajicek
  if (net->n.flags & KRF_INSTALLED)
664 2d140452 Martin Mares
    {
665 78a2cc28 Ondrej Zajicek
      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 2d140452 Martin Mares
        verdict = KRF_UPDATE;
676 c9df01d3 Ondrej Zajicek
      else
677
        verdict = KRF_SEEN;
678 78a2cc28 Ondrej Zajicek
679
      if (rt_free)
680
        rte_free(rt_free);
681
682
      lp_flush(krt_filter_lp);
683 2d140452 Martin Mares
    }
684
  else
685
    verdict = KRF_DELETE;
686
687 ff2857b0 Ondrej Zajicek
 sentenced:
688 832fa033 Martin Mares
  krt_trace_in(p, e, ((char *[]) { "?", "seen", "will be updated", "will be removed", "ignored" }) [verdict]);
689 c10421d3 Martin Mares
  net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
690
  if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
691 2d140452 Martin Mares
    {
692 c9df01d3 Ondrej Zajicek
      /* Get a cached copy of attributes and temporarily link the route */
693 2d140452 Martin Mares
      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 f4a60a9b Ondrej Zajicek (work)
  struct rtable *t = p->p.main_channel->table;
707 2d140452 Martin Mares
708 832fa033 Martin Mares
  KRT_TRACE(p, D_EVENTS, "Pruning table %s", t->name);
709 600998fc Ondrej Zajicek (work)
  FIB_WALK(&t->fib, net, n)
710 2d140452 Martin Mares
    {
711 600998fc Ondrej Zajicek (work)
      int verdict = n->n.flags & KRF_VERDICT_MASK;
712 78a2cc28 Ondrej Zajicek
      rte *new, *old, *rt_free = NULL;
713 c9df01d3 Ondrej Zajicek
      ea_list *tmpa = NULL;
714 2d140452 Martin Mares
715 c9df01d3 Ondrej Zajicek
      if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
716 2d140452 Martin Mares
        {
717 c9df01d3 Ondrej Zajicek
          /* Get a dummy route from krt_got_route() */
718 2d140452 Martin Mares
          old = n->routes;
719
          n->routes = old->next;
720
        }
721
      else
722
        old = NULL;
723 c9df01d3 Ondrej Zajicek
724
      if (verdict == KRF_CREATE || verdict == KRF_UPDATE)
725
        {
726
          /* We have to run export filter to get proper 'new' route */
727 78a2cc28 Ondrej Zajicek
          new = krt_export_net(p, n, &rt_free, &tmpa);
728
729
          if (!new)
730
            verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
731 2a0130f9 Ondrej Zajicek
          else
732 78a2cc28 Ondrej Zajicek
            tmpa = ea_append(tmpa, new->attrs->eattrs);
733 c9df01d3 Ondrej Zajicek
        }
734 78a2cc28 Ondrej Zajicek
      else
735
        new = NULL;
736 2d140452 Martin Mares
737
      switch (verdict)
738
        {
739
        case KRF_CREATE:
740 600998fc Ondrej Zajicek (work)
          if (new && (n->n.flags & KRF_INSTALLED))
741 2d140452 Martin Mares
            {
742 832fa033 Martin Mares
              krt_trace_in(p, new, "reinstalling");
743 7a2c48da Ondrej Zajicek
              krt_replace_rte(p, n, new, NULL, tmpa);
744 2d140452 Martin Mares
            }
745
          break;
746
        case KRF_SEEN:
747 c10421d3 Martin Mares
        case KRF_IGNORE:
748 2d140452 Martin Mares
          /* Nothing happens */
749
          break;
750
        case KRF_UPDATE:
751 832fa033 Martin Mares
          krt_trace_in(p, new, "updating");
752 7a2c48da Ondrej Zajicek
          krt_replace_rte(p, n, new, old, tmpa);
753 2d140452 Martin Mares
          break;
754
        case KRF_DELETE:
755 1151401e Martin Mares
          krt_trace_in(p, old, "deleting");
756 7a2c48da Ondrej Zajicek
          krt_replace_rte(p, n, NULL, old, NULL);
757 2d140452 Martin Mares
          break;
758
        default:
759
          bug("krt_prune: invalid route status");
760
        }
761 c9df01d3 Ondrej Zajicek
762 2d140452 Martin Mares
      if (old)
763
        rte_free(old);
764 78a2cc28 Ondrej Zajicek
      if (rt_free)
765
        rte_free(rt_free);
766 c9df01d3 Ondrej Zajicek
      lp_flush(krt_filter_lp);
767 600998fc Ondrej Zajicek (work)
      n->n.flags &= ~KRF_VERDICT_MASK;
768 2d140452 Martin Mares
    }
769
  FIB_WALK_END;
770 c10421d3 Martin Mares
771
#ifdef KRT_ALLOW_LEARN
772
  if (KRT_CF->learn)
773
    krt_learn_prune(p);
774
#endif
775 0c791f87 Ondrej Zajicek
776
  if (p->ready)
777
    p->initialized = 1;
778 2d140452 Martin Mares
}
779
780 e16155ae Martin Mares
void
781 ff2857b0 Ondrej Zajicek
krt_got_route_async(struct krt_proto *p, rte *e, int new)
782 e16155ae Martin Mares
{
783
  net *net = e->net;
784
785 ff2857b0 Ondrej Zajicek
  switch (e->u.krt.src)
786 e16155ae Martin Mares
    {
787
    case KRT_SRC_BIRD:
788 832fa033 Martin Mares
      ASSERT(0);                        /* Should be filtered by the back end */
789 ff2857b0 Ondrej Zajicek
790 e16155ae Martin Mares
    case KRT_SRC_REDIRECT:
791 ff2857b0 Ondrej Zajicek
      if (new)
792
        {
793
          krt_trace_in(p, e, "[redirect] deleting");
794 7a2c48da Ondrej Zajicek
          krt_replace_rte(p, net, NULL, e, NULL);
795 ff2857b0 Ondrej Zajicek
        }
796
      /* If !new, it is probably echo of our deletion */
797 e16155ae Martin Mares
      break;
798 ff2857b0 Ondrej Zajicek
799 c10421d3 Martin Mares
#ifdef KRT_ALLOW_LEARN
800 1bc4b2cc Ondrej Filip
    case KRT_SRC_ALIEN:
801 c10421d3 Martin Mares
      if (KRT_CF->learn)
802 e16155ae Martin Mares
        {
803 c10421d3 Martin Mares
          krt_learn_async(p, e, new);
804
          return;
805 e16155ae Martin Mares
        }
806 c10421d3 Martin Mares
#endif
807 e16155ae Martin Mares
    }
808 c10421d3 Martin Mares
  rte_free(e);
809 e16155ae Martin Mares
}
810
811 2d140452 Martin Mares
/*
812
 *        Periodic scanning
813
 */
814
815 c6964c30 Ondrej Zajicek
816
#ifdef CONFIG_ALL_TABLES_AT_ONCE
817
818
static timer *krt_scan_timer;
819
static int krt_scan_count;
820
821 2d140452 Martin Mares
static void
822 6578a604 Martin Mares
krt_scan(timer *t UNUSED)
823 2d140452 Martin Mares
{
824 7de45ba4 Martin Mares
  struct krt_proto *p;
825 2d140452 Martin Mares
826 7e5f5ffd Martin Mares
  kif_force_scan();
827 c6964c30 Ondrej Zajicek
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 7de45ba4 Martin Mares
  {
837 c6964c30 Ondrej Zajicek
    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 f8cc7396 Ondrej Zajicek
850 0c791f87 Ondrej Zajicek
  tm_start(krt_scan_timer, 1);
851 c6964c30 Ondrej Zajicek
}
852
853
static void
854 3e236955 Jan Moskyto Matejka
krt_scan_timer_stop(struct krt_proto *p UNUSED)
855 c6964c30 Ondrej Zajicek
{
856
  krt_scan_count--;
857
858
  if (!krt_scan_count)
859
  {
860
    rfree(krt_scan_timer);
861
    krt_scan_timer = NULL;
862 7de45ba4 Martin Mares
  }
863 c6964c30 Ondrej Zajicek
}
864
865 0c791f87 Ondrej Zajicek
static void
866
krt_scan_timer_kick(struct krt_proto *p UNUSED)
867
{
868
  tm_start(krt_scan_timer, 0);
869
}
870
871 7de45ba4 Martin Mares
#else
872 c6964c30 Ondrej Zajicek
873
static void
874
krt_scan(timer *t)
875
{
876
  struct krt_proto *p = t->data;
877
878
  kif_force_scan();
879
880 832fa033 Martin Mares
  KRT_TRACE(p, D_EVENTS, "Scanning routing table");
881 396dfa90 Ondrej Zajicek
  krt_do_scan(p);
882 7e5f5ffd Martin Mares
  krt_prune(p);
883 2d140452 Martin Mares
}
884
885 c6964c30 Ondrej Zajicek
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 0c791f87 Ondrej Zajicek
  tm_start(p->scan_timer, 1);
890 c6964c30 Ondrej Zajicek
}
891
892
static void
893
krt_scan_timer_stop(struct krt_proto *p)
894
{
895
  tm_stop(p->scan_timer);
896
}
897
898 0c791f87 Ondrej Zajicek
static void
899 252c7e4d Ondrej Zajicek
krt_scan_timer_kick(struct krt_proto *p)
900 0c791f87 Ondrej Zajicek
{
901
  tm_start(p->scan_timer, 0);
902
}
903
904 c6964c30 Ondrej Zajicek
#endif
905
906
907
908 396dfa90 Ondrej Zajicek
909 2d140452 Martin Mares
/*
910 c10421d3 Martin Mares
 *        Updates
911
 */
912 396dfa90 Ondrej Zajicek
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 c429d4a4 Ondrej Zajicek
static int
943 3e236955 Jan Moskyto Matejka
krt_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
944 c429d4a4 Ondrej Zajicek
{
945 62e64905 Ondrej Zajicek (work)
  // struct krt_proto *p = (struct krt_proto *) P;
946 c429d4a4 Ondrej Zajicek
  rte *e = *new;
947
948 094d2bdb Ondrej Zajicek
  if (e->attrs->src->proto == P)
949 c5ff44a7 Ondrej Zajicek
  {
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 78a2cc28 Ondrej Zajicek
    if (e == e->net->routes)
961
      e->net->n.flags &= ~KRF_INSTALLED;
962 c5ff44a7 Ondrej Zajicek
#endif
963 c429d4a4 Ondrej Zajicek
    return -1;
964 c5ff44a7 Ondrej Zajicek
  }
965 c429d4a4 Ondrej Zajicek
966
  if (!krt_capable(e))
967
    return -1;
968
969
  return 0;
970
}
971 c10421d3 Martin Mares
972
static void
973 4bdf1881 Jan Moskyto Matejka
krt_rt_notify(struct proto *P, struct channel *ch UNUSED, net *net,
974 252c7e4d Ondrej Zajicek
              rte *new, rte *old, struct ea_list *eattrs)
975 c10421d3 Martin Mares
{
976
  struct krt_proto *p = (struct krt_proto *) P;
977
978 a92cf57d Ondrej Zajicek
  if (config->shutdown)
979 f990fc61 Martin Mares
    return;
980 c10421d3 Martin Mares
  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 c9df01d3 Ondrej Zajicek
  if (p->initialized)                /* Before first scan we don't touch the routes */
987 7a2c48da Ondrej Zajicek
    krt_replace_rte(p, net, new, old, eattrs);
988 c10421d3 Martin Mares
}
989
990 0c791f87 Ondrej Zajicek
static void
991 252c7e4d Ondrej Zajicek
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 f4a60a9b Ondrej Zajicek (work)
static void
1008
krt_reload_routes(struct channel *C)
1009 252c7e4d Ondrej Zajicek
{
1010 f4a60a9b Ondrej Zajicek (work)
  struct krt_proto *p = (void *) C->proto;
1011 252c7e4d Ondrej Zajicek
1012
  /* Although we keep learned routes in krt_table, we rather schedule a scan */
1013
1014
  if (KRT_CF->learn)
1015 7069fc9e Ondrej Zajicek
  {
1016
    p->reload = 1;
1017 252c7e4d Ondrej Zajicek
    krt_scan_timer_kick(p);
1018 7069fc9e Ondrej Zajicek
  }
1019 252c7e4d Ondrej Zajicek
}
1020
1021
static void
1022 f4a60a9b Ondrej Zajicek (work)
krt_feed_end(struct channel *C)
1023 0c791f87 Ondrej Zajicek
{
1024 f4a60a9b Ondrej Zajicek (work)
  struct krt_proto *p = (void *) C->proto;
1025 0c791f87 Ondrej Zajicek
1026
  p->ready = 1;
1027
  krt_scan_timer_kick(p);
1028
}
1029
1030
1031 396dfa90 Ondrej Zajicek
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 c10421d3 Martin Mares
/*
1040 2d140452 Martin Mares
 *        Protocol glue
1041
 */
1042
1043 396dfa90 Ondrej Zajicek
struct krt_config *krt_cf;
1044 7e5f5ffd Martin Mares
1045 f4a60a9b Ondrej Zajicek (work)
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 396dfa90 Ondrej Zajicek
static struct proto *
1074 f4a60a9b Ondrej Zajicek (work)
krt_init(struct proto_config *CF)
1075 7de45ba4 Martin Mares
{
1076 f4a60a9b Ondrej Zajicek (work)
  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 7de45ba4 Martin Mares
1081 396dfa90 Ondrej Zajicek
  p->p.import_control = krt_import_control;
1082 252c7e4d Ondrej Zajicek
  p->p.rt_notify = krt_rt_notify;
1083
  p->p.if_notify = krt_if_notify;
1084
  p->p.reload_routes = krt_reload_routes;
1085 9aed29e6 Ondrej Zajicek
  p->p.feed_end = krt_feed_end;
1086 094d2bdb Ondrej Zajicek
  p->p.make_tmp_attrs = krt_make_tmp_attrs;
1087
  p->p.store_tmp_attrs = krt_store_tmp_attrs;
1088 396dfa90 Ondrej Zajicek
  p->p.rte_same = krt_rte_same;
1089 7de45ba4 Martin Mares
1090 396dfa90 Ondrej Zajicek
  krt_sys_init(p);
1091
  return &p->p;
1092 7de45ba4 Martin Mares
}
1093
1094 2d140452 Martin Mares
static int
1095
krt_start(struct proto *P)
1096
{
1097
  struct krt_proto *p = (struct krt_proto *) P;
1098 7de45ba4 Martin Mares
1099 f4a60a9b Ondrej Zajicek (work)
  switch (p->p.net_type)
1100 29a64162 Ondrej Zajicek (work)
  {
1101
  case NET_IP4:        p->af = AF_INET; break;
1102
  case NET_IP6:        p->af = AF_INET6; break;
1103 1d213067 Ondrej Zajicek (work)
#ifdef AF_MPLS
1104 d14f8c3c Jan Moskyto Matejka
  case NET_MPLS: p->af = AF_MPLS; break;
1105 1d213067 Ondrej Zajicek (work)
#endif
1106 d14f8c3c Jan Moskyto Matejka
  default: log(L_ERR "KRT: Tried to start with strange net type: %d", p->p.net_type); return PS_START; break;
1107 29a64162 Ondrej Zajicek (work)
  }
1108
1109 c6964c30 Ondrej Zajicek
  add_tail(&krt_proto_list, &p->krt_node);
1110 2d140452 Martin Mares
1111 c10421d3 Martin Mares
#ifdef KRT_ALLOW_LEARN
1112
  krt_learn_init(p);
1113
#endif
1114
1115 9ddbfbdd Jan Moskyto Matejka
  if (!krt_sys_start(p))
1116
  {
1117
    rem_node(&p->krt_node);
1118
    return PS_START;
1119
  }
1120 2d140452 Martin Mares
1121 c6964c30 Ondrej Zajicek
  krt_scan_timer_start(p);
1122 2d140452 Martin Mares
1123 f4a60a9b Ondrej Zajicek (work)
  if (p->p.gr_recovery && KRT_CF->graceful_restart)
1124
    p->p.main_channel->gr_wait = 1;
1125 0c791f87 Ondrej Zajicek
1126 2d140452 Martin Mares
  return PS_UP;
1127
}
1128
1129 7de45ba4 Martin Mares
static int
1130 2d140452 Martin Mares
krt_shutdown(struct proto *P)
1131
{
1132
  struct krt_proto *p = (struct krt_proto *) P;
1133
1134 c6964c30 Ondrej Zajicek
  krt_scan_timer_stop(p);
1135 7e5f5ffd Martin Mares
1136 3d574679 Ondrej Zajicek
  /* FIXME we should flush routes even when persist during reconfiguration */
1137
  if (p->initialized && !KRT_CF->persist)
1138 2d140452 Martin Mares
    krt_flush_routes(p);
1139
1140 0c791f87 Ondrej Zajicek
  p->ready = 0;
1141
  p->initialized = 0;
1142
1143 9ddbfbdd Jan Moskyto Matejka
  if (p->p.proto_state == PS_START)
1144
    return PS_DOWN;
1145 7de45ba4 Martin Mares
1146 9ddbfbdd Jan Moskyto Matejka
  krt_sys_shutdown(p);
1147 c6964c30 Ondrej Zajicek
  rem_node(&p->krt_node);
1148 2d140452 Martin Mares
1149
  return PS_DOWN;
1150
}
1151
1152 396dfa90 Ondrej Zajicek
static int
1153 f4a60a9b Ondrej Zajicek (work)
krt_reconfigure(struct proto *p, struct proto_config *CF)
1154 72aed1a0 Ondrej Zajicek
{
1155 f4a60a9b Ondrej Zajicek (work)
  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 72aed1a0 Ondrej Zajicek
1161 396dfa90 Ondrej Zajicek
  if (!krt_sys_reconfigure((struct krt_proto *) p, n, o))
1162
    return 0;
1163 9ba2798c Ondrej Zajicek
1164 0c791f87 Ondrej Zajicek
  /* persist, graceful restart need not be the same */
1165 f4a60a9b Ondrej Zajicek (work)
  return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes;
1166 2d140452 Martin Mares
}
1167
1168 396dfa90 Ondrej Zajicek
struct proto_config *
1169
krt_init_config(int class)
1170 aa8761de Martin Mares
{
1171 396dfa90 Ondrej Zajicek
#ifndef CONFIG_MULTIPLE_TABLES
1172
  if (krt_cf)
1173
    cf_error("Kernel protocol already defined");
1174
#endif
1175
1176 2bbc3083 Ondrej Zajicek
  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
1177 396dfa90 Ondrej Zajicek
  krt_cf->scan_time = 60;
1178 aa8761de Martin Mares
1179 396dfa90 Ondrej Zajicek
  krt_sys_init_config(krt_cf);
1180
  return (struct proto_config *) krt_cf;
1181 aa8761de Martin Mares
}
1182
1183 a7f23f58 Ondrej Zajicek
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 396dfa90 Ondrej Zajicek
  krt_sys_copy_config(d, s);
1191 a7f23f58 Ondrej Zajicek
}
1192 71ca7716 Ondrej Zajicek
1193
static int
1194 9fdf9d29 Ondrej Zajicek
krt_get_attr(eattr *a, byte *buf, int buflen)
1195 71ca7716 Ondrej Zajicek
{
1196
  switch (a->id)
1197
  {
1198 72aed1a0 Ondrej Zajicek
  case EA_KRT_SOURCE:
1199
    bsprintf(buf, "source");
1200
    return GA_NAME;
1201
1202 9ba2798c Ondrej Zajicek
  case EA_KRT_METRIC:
1203
    bsprintf(buf, "metric");
1204
    return GA_NAME;
1205
1206 71ca7716 Ondrej Zajicek
  default:
1207 9fdf9d29 Ondrej Zajicek
    return krt_sys_get_attr(a, buf, buflen);
1208 71ca7716 Ondrej Zajicek
  }
1209
}
1210
1211
1212 2d140452 Martin Mares
struct protocol proto_unix_kernel = {
1213 4a591d4b Pavel Tvrdik
  .name =                "Kernel",
1214
  .template =                "kernel%d",
1215
  .attr_class =                EAP_KRT,
1216
  .preference =                DEF_PREF_INHERITED,
1217 d14f8c3c Jan Moskyto Matejka
  .channel_mask =        NB_IP | NB_MPLS,
1218 f4a60a9b Ondrej Zajicek (work)
  .proto_size =                sizeof(struct krt_proto),
1219 2bbc3083 Ondrej Zajicek
  .config_size =        sizeof(struct krt_config),
1220 4a591d4b Pavel Tvrdik
  .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 c10421d3 Martin Mares
#ifdef KRT_ALLOW_LEARN
1229 4a591d4b Pavel Tvrdik
  .dump =                krt_dump,
1230
  .dump_attrs =                krt_dump_attrs,
1231 c10421d3 Martin Mares
#endif
1232 2d140452 Martin Mares
};