krt.c 26.1 KB
Newer Older
1 2 3
/*
 *	BIRD -- UNIX Kernel Synchronization
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5 6 7 8
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

9 10 11 12 13 14 15 16 17 18
/**
 * DOC: Kernel synchronization
 *
 * This system dependent module implements the Kernel and Device protocol,
 * that is synchronization of interface lists and routing tables with the
 * OS kernel.
 *
 * The whole kernel synchronization is a bit messy and touches some internals
 * of the routing table engine, because routing table maintenance is a typical
 * 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 21 22
 * a local routing table copy.
 *
 * 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 28
 * In this case, we keep only a single scan timer.
 *
Martin Mareš's avatar
Martin Mareš committed
29 30 31 32
 * We use FIB node flags in the routing table to keep track of route
 * synchronization status. We also attach temporary &rte's to the routing table,
 * but it cannot do any harm to the rest of BIRD since table synchronization is
 * an atomic process.
33 34 35
 *
 * When starting up, we cheat by looking if there is another
 * 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 42 43 44 45
 * This is also used for platform specific protocol options and route attributes.
 *
 * There was also an old code that used traditional UNIX ioctls for these tasks.
 * It was unmaintained and later removed. For reference, see sysdep/krt-* files
 * in commit 396dfa9042305f62da1f56589c4b98fac57fc2f6
46
 */
47 48 49 50 51

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

52
#undef LOCAL_DEBUG
53 54 55 56 57

#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/route.h"
#include "nest/protocol.h"
58
#include "filter/filter.h"
59
#include "lib/timer.h"
60
#include "conf/conf.h"
Martin Mareš's avatar
Martin Mareš committed
61
#include "lib/string.h"
62 63 64 65

#include "unix.h"
#include "krt.h"

66 67 68 69
/*
 *	Global resources
 */

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

74 75 76
void
krt_io_init(void)
{
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 81 82 83 84 85
}

/*
 *	Interfaces
 */

86
static struct kif_config *kif_cf;
87 88 89 90 91 92 93 94 95
static struct kif_proto *kif_proto;
static timer *kif_scan_timer;
static bird_clock_t kif_last_shot;

static void
kif_scan(timer *t)
{
  struct kif_proto *p = t->data;

96
  KRT_TRACE(p, D_EVENTS, "Scanning interfaces");
97
  kif_last_shot = now;
98
  kif_do_scan(p);
99 100 101 102 103 104 105 106 107 108 109 110
}

static void
kif_force_scan(void)
{
  if (kif_proto && kif_last_shot + 2 < now)
    {
      kif_scan(kif_scan_timer);
      tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
    }
}

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

118 119
static inline int
prefer_addr(struct ifa *a, struct ifa *b)
120
{
121 122 123 124 125 126 127 128 129 130
  int sa = a->scope > SCOPE_LINK;
  int sb = b->scope > SCOPE_LINK;

  if (sa < sb)
    return 0;
  else if (sa > sb)
    return 1;
  else
    return ipa_compare(a->ip, b->ip) < 0;
}
131 132 133 134 135 136 137 138 139 140

static inline struct ifa *
find_preferred_ifa(struct iface *i, ip_addr prefix, ip_addr mask)
{
  struct ifa *a, *b = NULL;

  WALK_LIST(a, i->addrs)
    {
      if (!(a->flags & IA_SECONDARY) &&
	  ipa_equal(ipa_and(a->ip, mask), prefix) &&
141
	  (!b || prefer_addr(a, b)))
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	b = a;
    }

  return b;
}

struct ifa *
kif_choose_primary(struct iface *i)
{
  struct kif_config *cf = (struct kif_config *) (kif_proto->p.cf);
  struct kif_primary_item *it;
  struct ifa *a;

  WALK_LIST(it, cf->primary)
    {
      if (!it->pattern || patmatch(it->pattern, i->name))
	if (a = find_preferred_ifa(i, it->prefix, ipa_mkmask(it->pxlen)))
	  return a;
    }

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

165 166 167 168
  return find_preferred_ifa(i, IPA_NONE, IPA_NONE);
}


169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static struct proto *
kif_init(struct proto_config *c)
{
  struct kif_proto *p = proto_new(c, sizeof(struct kif_proto));

  kif_sys_init(p);
  return &p->p;
}

static int
kif_start(struct proto *P)
{
  struct kif_proto *p = (struct kif_proto *) P;

  kif_proto = p;
  kif_sys_start(p);

  /* Start periodic interface scanning */
  kif_scan_timer = tm_new(P->pool);
  kif_scan_timer->hook = kif_scan;
  kif_scan_timer->data = p;
  kif_scan_timer->recurrent = KIF_CF->scan_time;
  kif_scan(kif_scan_timer);
  tm_start(kif_scan_timer, KIF_CF->scan_time);

  return PS_UP;
}

static int
kif_shutdown(struct proto *P)
{
  struct kif_proto *p = (struct kif_proto *) P;

  tm_stop(kif_scan_timer);
  kif_sys_shutdown(p);
  kif_proto = NULL;

  return PS_DOWN;
}

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

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

218 219 220 221 222 223 224
  if (o->scan_time != n->scan_time)
    {
      tm_stop(kif_scan_timer);
      kif_scan_timer->recurrent = n->scan_time;
      kif_scan(kif_scan_timer);
      tm_start(kif_scan_timer, n->scan_time);
    }
225 226 227 228 229 230 231 232 233 234 235 236

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

      ifa_recalc_all_primary_addresses();
    }

237 238 239
  return 1;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253

static void
kif_preconfig(struct protocol *P UNUSED, struct config *c)
{
  kif_cf = NULL;
  kif_sys_preconfig(c);
}

struct proto_config *
kif_init_config(int class)
{
  if (kif_cf)
    cf_error("Kernel device protocol already defined");

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

  kif_sys_init_config(kif_cf);
  return (struct proto_config *) kif_cf;
}

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

  /* Shallow copy of everything (just scan_time currently) */
269
  proto_copy_rest(dest, src, sizeof(struct kif_config));
270 271 272 273 274

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

  /* Fix sysdep parts */
275
  kif_sys_copy_config(d, s);
276 277 278
}


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

292 293 294 295
/*
 *	Tracing of routes
 */

296 297
static inline void
krt_trace_in(struct krt_proto *p, rte *e, char *msg)
298
{
299 300
  if (p->p.debug & D_PACKETS)
    log(L_TRACE "%s: %I/%d: %s", p->p.name, e->net->n.prefix, e->net->n.pxlen, msg);
301 302 303
}

static inline void
304
krt_trace_in_rl(struct tbf *f, struct krt_proto *p, rte *e, char *msg)
305 306
{
  if (p->p.debug & D_PACKETS)
307
    log_rl(f, L_TRACE "%s: %I/%d: %s", p->p.name, e->net->n.prefix, e->net->n.pxlen, msg);
308 309
}

310 311 312 313 314 315
/*
 *	Inherited Routes
 */

#ifdef KRT_ALLOW_LEARN

316
static struct tbf rl_alien = TBF_DEFAULT_LOG_LIMITS;
317

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

326 327 328
static inline int
krt_same_key(rte *a, rte *b)
{
329 330 331 332 333 334 335 336 337 338 339 340 341
  return a->u.krt.metric == b->u.krt.metric;
}

static inline int
krt_uptodate(rte *a, rte *b)
{
  if (a->attrs != b->attrs)
    return 0;

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

  return 1;
342 343 344 345 346 347 348 349
}

static void
krt_learn_announce_update(struct krt_proto *p, rte *e)
{
  net *n = e->net;
  rta *aa = rta_clone(e->attrs);
  rte *ee = rte_get_temp(aa);
350
  net *nn = net_get(p->p.table, n->n.prefix, n->n.pxlen);
351 352
  ee->net = nn;
  ee->pflags = 0;
353
  ee->pref = p->p.preference;
354
  ee->u.krt = e->u.krt;
355
  rte_update(&p->p, nn, ee);
356 357 358 359 360
}

static void
krt_learn_announce_delete(struct krt_proto *p, net *n)
{
361
  n = net_find(p->p.table, n->n.prefix, n->n.pxlen);
362
  rte_update(&p->p, n, NULL);
363 364
}

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

373
  e->attrs = rta_lookup(e->attrs);
374 375 376 377 378 379 380 381

  for(mm=&n->routes; m = *mm; mm=&m->next)
    if (krt_same_key(m, e))
      break;
  if (m)
    {
      if (krt_uptodate(m, e))
	{
382
	  krt_trace_in_rl(&rl_alien, p, e, "[alien] seen");
383 384 385 386 387
	  rte_free(e);
	  m->u.krt.seen = 1;
	}
      else
	{
388
	  krt_trace_in(p, e, "[alien] updated");
389 390 391 392 393 394
	  *mm = m->next;
	  rte_free(m);
	  m = NULL;
	}
    }
  else
395
    krt_trace_in(p, e, "[alien] created");
396 397 398 399 400 401 402 403 404 405 406 407 408 409
  if (!m)
    {
      e->next = n->routes;
      n->routes = e;
      e->u.krt.seen = 1;
    }
}

static void
krt_learn_prune(struct krt_proto *p)
{
  struct fib *fib = &p->krt_table.fib;
  struct fib_iterator fit;

410
  KRT_TRACE(p, D_EVENTS, "Pruning inherited routes");
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

  FIB_ITERATE_INIT(&fit, fib);
again:
  FIB_ITERATE_START(fib, &fit, f)
    {
      net *n = (net *) f;
      rte *e, **ee, *best, **pbest, *old_best;

      old_best = n->routes;
      best = NULL;
      pbest = NULL;
      ee = &n->routes;
      while (e = *ee)
	{
	  if (!e->u.krt.seen)
	    {
	      *ee = e->next;
	      rte_free(e);
	      continue;
	    }
	  if (!best || best->u.krt.metric > e->u.krt.metric)
	    {
	      best = e;
	      pbest = ee;
	    }
	  e->u.krt.seen = 0;
	  ee = &e->next;
	}
      if (!n->routes)
	{
	  DBG("%I/%d: deleting\n", n->n.prefix, n->n.pxlen);
	  if (old_best)
	    {
	      krt_learn_announce_delete(p, n);
	      n->n.flags &= ~KRF_INSTALLED;
	    }
	  FIB_ITERATE_PUT(&fit, f);
	  fib_delete(fib, f);
	  goto again;
	}
      *pbest = best->next;
      best->next = n->routes;
      n->routes = best;
      if (best != old_best || !(n->n.flags & KRF_INSTALLED))
	{
	  DBG("%I/%d: announcing (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
	  krt_learn_announce_update(p, best);
	  n->n.flags |= KRF_INSTALLED;
	}
      else
	DBG("%I/%d: uptodate (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
    }
  FIB_ITERATE_END(f);
}

static void
krt_learn_async(struct krt_proto *p, rte *e, int new)
{
  net *n0 = e->net;
470
  net *n = net_get(&p->krt_table, n0->n.prefix, n0->n.pxlen);
471 472
  rte *g, **gg, *best, **bestp, *old_best;

473
  e->attrs = rta_lookup(e->attrs);
474 475 476 477 478 479 480 481 482 483 484

  old_best = n->routes;
  for(gg=&n->routes; g = *gg; gg = &g->next)
    if (krt_same_key(g, e))
      break;
  if (new)
    {
      if (g)
	{
	  if (krt_uptodate(g, e))
	    {
485
	      krt_trace_in(p, e, "[alien async] same");
486 487 488
	      rte_free(e);
	      return;
	    }
489
	  krt_trace_in(p, e, "[alien async] updated");
490 491 492 493
	  *gg = g->next;
	  rte_free(g);
	}
      else
494
	krt_trace_in(p, e, "[alien async] created");
495

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

static void
krt_learn_init(struct krt_proto *p)
{
  if (KRT_CF->learn)
547
    rt_setup(p->p.pool, &p->krt_table, "Inherited", NULL);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
}

static void
krt_dump(struct proto *P)
{
  struct krt_proto *p = (struct krt_proto *) P;

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

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

#endif

569 570 571 572 573 574 575
/*
 *	Routes
 */

static void
krt_flush_routes(struct krt_proto *p)
{
576
  struct rtable *t = p->p.table;
577

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

static int
594
krt_same_dest(rte *k, rte *e)
595 596 597 598 599 600 601 602 603 604 605
{
  rta *ka = k->attrs, *ea = e->attrs;

  if (ka->dest != ea->dest)
    return 0;
  switch (ka->dest)
    {
    case RTD_ROUTER:
      return ipa_equal(ka->gw, ea->gw);
    case RTD_DEVICE:
      return !strcmp(ka->iface->name, ea->iface->name);
606 607
    case RTD_MULTIPATH:
      return mpnh_same(ka->nexthops, ea->nexthops);
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    default:
      return 1;
    }
}

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

void
krt_got_route(struct krt_proto *p, rte *e)
{
  rte *old;
  net *net = e->net;
  int verdict;

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

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

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

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

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

664
  old = net->routes;
665
  if ((net->n.flags & KRF_INSTALLED) && rte_is_valid(old))
666
    {
667 668 669
      /* There may be changes in route attributes, we ignore that.
         Also, this does not work well if gw is changed in export filter */
      if ((net->n.flags & KRF_SYNC_ERROR) || ! krt_same_dest(e, old))
670
	verdict = KRF_UPDATE;
671 672
      else
	verdict = KRF_SEEN;
673 674 675 676
    }
  else
    verdict = KRF_DELETE;

677
 sentenced:
678
  krt_trace_in(p, e, ((char *[]) { "?", "seen", "will be updated", "will be removed", "ignored" }) [verdict]);
679 680
  net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
  if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
681
    {
682
      /* Get a cached copy of attributes and temporarily link the route */
683 684 685 686 687 688 689 690 691 692
      rta *a = e->attrs;
      a->source = RTS_DUMMY;
      e->attrs = rta_lookup(a);
      e->next = net->routes;
      net->routes = e;
    }
  else
    rte_free(e);
}

693 694 695
static inline int
krt_export_rte(struct krt_proto *p, rte **new, ea_list **tmpa)
{
Ondřej Zajíček's avatar
Ondřej Zajíček committed
696
  struct filter *filter = p->p.main_ahook->out_filter;
697 698 699 700 701 702 703 704 705 706

  if (! *new)
    return 0;

  if (filter == FILTER_REJECT)
    return 0;

  if (filter == FILTER_ACCEPT)
    return 1;

707
  struct proto *src = (*new)->attrs->src->proto;
708 709 710 711
  *tmpa = src->make_tmp_attrs ? src->make_tmp_attrs(*new, krt_filter_lp) : NULL;
  return f_run(filter, new, tmpa, krt_filter_lp, FF_FORCE_TMPATTR) <= F_ACCEPT;
}

712 713 714
static void
krt_prune(struct krt_proto *p)
{
715
  struct rtable *t = p->p.table;
716

717
  KRT_TRACE(p, D_EVENTS, "Pruning table %s", t->name);
718 719 720
  FIB_WALK(&t->fib, f)
    {
      net *n = (net *) f;
721
      int verdict = f->flags & KRF_VERDICT_MASK;
722 723
      rte *new, *new0, *old;
      ea_list *tmpa = NULL;
724

725
      if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
726
	{
727
	  /* Get a dummy route from krt_got_route() */
728 729 730 731 732
	  old = n->routes;
	  n->routes = old->next;
	}
      else
	old = NULL;
733 734 735 736 737 738 739 740

      new = new0 = n->routes;
      if (verdict == KRF_CREATE || verdict == KRF_UPDATE)
	{
	  /* We have to run export filter to get proper 'new' route */
	  if (! krt_export_rte(p, &new, &tmpa))
	    {
	      /* Route rejected, should not happen (KRF_INSTALLED) but to be sure .. */
741
	      verdict = (verdict == KRF_CREATE) ? KRF_IGNORE : KRF_DELETE;
742
	    }
743 744 745 746 747 748 749
	  else
	    {
	      ea_list **x = &tmpa;
	      while (*x)
		x = &((*x)->next);
	      *x = new ? new->attrs->eattrs : NULL;
	    }
750
	}
751 752 753 754

      switch (verdict)
	{
	case KRF_CREATE:
755
	  if (new && (f->flags & KRF_INSTALLED))
756
	    {
757
	      krt_trace_in(p, new, "reinstalling");
758
	      krt_replace_rte(p, n, new, NULL, tmpa);
759 760 761
	    }
	  break;
	case KRF_SEEN:
762
	case KRF_IGNORE:
763 764 765
	  /* Nothing happens */
	  break;
	case KRF_UPDATE:
766
	  krt_trace_in(p, new, "updating");
767
	  krt_replace_rte(p, n, new, old, tmpa);
768 769
	  break;
	case KRF_DELETE:
770
	  krt_trace_in(p, old, "deleting");
771
	  krt_replace_rte(p, n, NULL, old, NULL);
772 773 774 775
	  break;
	default:
	  bug("krt_prune: invalid route status");
	}
776

777 778
      if (old)
	rte_free(old);
779 780 781
      if (new != new0)
	rte_free(new);
      lp_flush(krt_filter_lp);
782
      f->flags &= ~KRF_VERDICT_MASK;
783 784
    }
  FIB_WALK_END;
785 786 787 788 789

#ifdef KRT_ALLOW_LEARN
  if (KRT_CF->learn)
    krt_learn_prune(p);
#endif
790 791 792

  if (p->ready)
    p->initialized = 1;
793 794
}

795
void
796
krt_got_route_async(struct krt_proto *p, rte *e, int new)
797 798 799
{
  net *net = e->net;

800
  switch (e->u.krt.src)
801 802
    {
    case KRT_SRC_BIRD:
803
      ASSERT(0);			/* Should be filtered by the back end */
804

805
    case KRT_SRC_REDIRECT:
806 807 808
      if (new)
	{
	  krt_trace_in(p, e, "[redirect] deleting");
809
	  krt_replace_rte(p, net, NULL, e, NULL);
810 811
	}
      /* If !new, it is probably echo of our deletion */
812
      break;
813

814
#ifdef KRT_ALLOW_LEARN
815
    case KRT_SRC_ALIEN:
816
      if (KRT_CF->learn)
817
	{
818 819
	  krt_learn_async(p, e, new);
	  return;
820
	}
821
#endif
822
    }
823
  rte_free(e);
824 825
}

826 827 828 829
/*
 *	Periodic scanning
 */

830 831 832 833 834 835

#ifdef CONFIG_ALL_TABLES_AT_ONCE

static timer *krt_scan_timer;
static int krt_scan_count;

836
static void
837
krt_scan(timer *t UNUSED)
838
{
839
  struct krt_proto *p;
840

841
  kif_force_scan();
842 843 844 845 846 847 848 849 850

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

  krt_do_scan(NULL);

  void *q;
  WALK_LIST(q, krt_proto_list)
851
  {
852 853 854 855 856 857 858 859 860 861 862 863
    p = SKIP_BACK(struct krt_proto, krt_node, q);
    krt_prune(p);
  }
}

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

  krt_scan_count++;
864

865
  tm_start(krt_scan_timer, 1);
866 867 868 869 870 871 872 873 874 875 876
}

static void
krt_scan_timer_stop(struct krt_proto *p)
{
  krt_scan_count--;

  if (!krt_scan_count)
  {
    rfree(krt_scan_timer);
    krt_scan_timer = NULL;
877
  }
878 879
}

880 881 882 883 884 885
static void
krt_scan_timer_kick(struct krt_proto *p UNUSED)
{
  tm_start(krt_scan_timer, 0);
}

886
#else
887 888 889 890 891 892 893 894

static void
krt_scan(timer *t)
{
  struct krt_proto *p = t->data;

  kif_force_scan();

895
  KRT_TRACE(p, D_EVENTS, "Scanning routing table");
896
  krt_do_scan(p);
897
  krt_prune(p);
898 899
}

900 901 902 903
static void
krt_scan_timer_start(struct krt_proto *p)
{
  p->scan_timer = tm_new_set(p->p.pool, krt_scan, p, 0, KRT_CF->scan_time);
904
  tm_start(p->scan_timer, 1);
905 906 907 908 909 910 911 912
}

static void
krt_scan_timer_stop(struct krt_proto *p)
{
  tm_stop(p->scan_timer);
}

913
static void
914
krt_scan_timer_kick(struct krt_proto *p)
915 916 917 918
{
  tm_start(p->scan_timer, 0);
}

919 920 921 922
#endif



923

924 925 926
/*
 *	Updates
 */
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

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

  l->next = NULL;
  l->flags = EALF_SORTED;
  l->count = 2;

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

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

  return l;
}

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

957 958 959 960 961 962
static int
krt_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
{
  struct krt_proto *p = (struct krt_proto *) P;
  rte *e = *new;

963
  if (e->attrs->src->proto == P)
964 965
    return -1;

966 967
  if (!KRT_CF->devroutes &&
      (e->attrs->dest == RTD_DEVICE) &&
968 969 970 971 972 973 974 975
      (e->attrs->source != RTS_STATIC_DEVICE))
    return -1;

  if (!krt_capable(e))
    return -1;

  return 0;
}
976 977

static void
978 979
krt_rt_notify(struct proto *P, struct rtable *table UNUSED, net *net,
	      rte *new, rte *old, struct ea_list *eattrs)
980 981 982
{
  struct krt_proto *p = (struct krt_proto *) P;

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

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
static void
krt_if_notify(struct proto *P, uint flags, struct iface *iface UNUSED)
{
  struct krt_proto *p = (struct krt_proto *) P;

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

  if ((flags & IF_CHANGE_DOWN) && KRT_CF->learn)
    krt_scan_timer_kick(p);
}

static int
krt_reload_routes(struct proto *P)
{
  struct krt_proto *p = (struct krt_proto *) P;

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

  if (KRT_CF->learn)
    krt_scan_timer_kick(p);

  return 1;
}

1025
static void
1026
krt_feed_end(struct proto *P)
1027 1028 1029 1030 1031 1032 1033 1034
{
  struct krt_proto *p = (struct krt_proto *) P;

  p->ready = 1;
  krt_scan_timer_kick(p);
}


1035 1036 1037 1038 1039 1040 1041 1042
static int
krt_rte_same(rte *a, rte *b)
{
  /* src is always KRT_SRC_ALIEN and type is irrelevant */
  return (a->u.krt.proto == b->u.krt.proto) && (a->u.krt.metric == b->u.krt.metric);
}


1043 1044 1045 1046
/*
 *	Protocol glue
 */

1047
struct krt_config *krt_cf;
1048

1049 1050
static struct proto *
krt_init(struct proto_config *c)
1051
{
1052
  struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
1053

1054 1055
  p->p.accept_ra_types = RA_OPTIMAL;
  p->p.import_control = krt_import_control;
1056 1057 1058
  p->p.rt_notify = krt_rt_notify;
  p->p.if_notify = krt_if_notify;
  p->p.reload_routes = krt_reload_routes;
1059
  p->p.feed_end = krt_feed_end;
1060 1061
  p->p.make_tmp_attrs = krt_make_tmp_attrs;
  p->p.store_tmp_attrs = krt_store_tmp_attrs;
1062
  p->p.rte_same = krt_rte_same;
1063

1064 1065
  krt_sys_init(p);
  return &p->p;
1066 1067
}

1068 1069 1070 1071
static int
krt_start(struct proto *P)
{
  struct krt_proto *p = (struct krt_proto *) P;
1072

1073
  add_tail(&krt_proto_list, &p->krt_node);
1074

1075 1076 1077 1078
#ifdef KRT_ALLOW_LEARN
  krt_learn_init(p);
#endif

1079
  krt_sys_start(p);
1080

1081
  krt_scan_timer_start(p);
1082

1083 1084 1085
  if (P->gr_recovery && KRT_CF->graceful_restart)
    P->gr_wait = 1;

1086 1087 1088
  return PS_UP;
}

1089
static int
1090 1091 1092 1093
krt_shutdown(struct proto *P)
{
  struct krt_proto *p = (struct krt_proto *) P;

1094
  krt_scan_timer_stop(p);
1095

1096 1097
  /* FIXME we should flush routes even when persist during reconfiguration */
  if (p->initialized && !KRT_CF->persist)
1098 1099
    krt_flush_routes(p);

1100 1101 1102
  p->ready = 0;
  p->initialized = 0;

1103
  krt_sys_shutdown(p);
1104

1105
  rem_node(&p->krt_node);
1106 1107 1108 1109

  return PS_DOWN;
}

1110 1111
static int
krt_reconfigure(struct proto *p, struct proto_config *new)
1112
{
1113 1114
  struct krt_config *o = (struct krt_config *) p->cf;
  struct krt_config *n = (struct krt_config *) new;
1115

1116 1117
  if (!krt_sys_reconfigure((struct krt_proto *) p, n, o))
    return 0;
1118

1119
  /* persist, graceful restart need not be the same */
1120
  return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes;
1121 1122
}

1123
static void
1124
krt_preconfig(struct protocol *P UNUSED, struct config *c)
1125
{
1126 1127
  krt_cf = NULL;
  krt_sys_preconfig(c);
1128
}
1129

1130 1131
static void
krt_postconfig(struct proto_config *C)
1132
{
1133
  struct krt_config *c = (struct krt_config *) C;
1134

1135 1136 1137 1138
#ifdef CONFIG_ALL_TABLES_AT_ONCE
  if (krt_cf->scan_time != c->scan_time)
    cf_error("All kernel syncers must use the same table scan interval");
#endif
1139

1140 1141 1142 1143
  if (C->table->krt_attached)
    cf_error("Kernel syncer (%s) already attached to table %s", C->table->krt_attached->name, C->table->name);
  C->table->krt_attached = C;
  krt_sys_postconfig(c);
1144 1145
}

1146 1147
struct proto_config *
krt_init_config(int class)
1148
{
1149 1150 1151 1152 1153
#ifndef CONFIG_MULTIPLE_TABLES
  if (krt_cf)
    cf_error("Kernel protocol already defined");
#endif

1154
  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
1155
  krt_cf->scan_time = 60;
1156

1157 1158
  krt_sys_init_config(krt_cf);
  return (struct proto_config *) krt_cf;
1159 1160
}

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
static void
krt_copy_config(struct proto_config *dest, struct proto_config *src)
{
  struct krt_config *d = (struct krt_config *) dest;
  struct krt_config *s = (struct krt_config *) src;

  /* Shallow copy of everything */
  proto_copy_rest(dest, src, sizeof(struct krt_config));

  /* Fix sysdep parts */
1171
  krt_sys_copy_config(d, s);
1172
}
1173 1174 1175 1176 1177 1178

static int
krt_get_attr(eattr * a, byte * buf, int buflen UNUSED)
{
  switch (a->id)
  {
1179 1180 1181 1182
  case EA_KRT_SOURCE:
    bsprintf(buf, "source");
    return GA_NAME;

1183 1184 1185 1186
  case EA_KRT_METRIC:
    bsprintf(buf, "metric");
    return GA_NAME;

1187 1188 1189
  case EA_KRT_PREFSRC:
    bsprintf(buf, "prefsrc");
    return GA_NAME;
1190

1191 1192 1193
  case EA_KRT_REALM:
    bsprintf(buf, "realm");
    return GA_NAME;
1194

1195 1196 1197 1198 1199 1200
  default:
    return GA_UNKNOWN;
  }
}


1201
struct protocol proto_unix_kernel = {
1202 1203 1204 1205
  .name =		"Kernel",
  .template =		"kernel%d",
  .attr_class =		EAP_KRT,
  .preference =		DEF_PREF_INHERITED,
1206
  .config_size =	sizeof(struct krt_config),
1207 1208 1209 1210 1211 1212 1213 1214
  .preconfig =		krt_preconfig,
  .postconfig =		krt_postconfig,
  .init =		krt_init,
  .start =		krt_start,
  .shutdown =		krt_shutdown,
  .reconfigure =	krt_reconfigure,
  .copy_config =	krt_copy_config,
  .get_attr =		krt_get_attr,
1215
#ifdef KRT_ALLOW_LEARN
1216 1217
  .dump =		krt_dump,
  .dump_attrs =		krt_dump_attrs,
1218
#endif
1219
};