rt-table.c 64.8 KB
Newer Older
1
/*
Martin Mareš's avatar
Martin Mareš committed
2
 *	BIRD -- Routing Tables
3
 *
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.
 */

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

31
#undef LOCAL_DEBUG
32

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

46 47
pool *rt_table_pool;

48
static slab *rte_slab;
49
static linpool *rte_update_pool;
50

51
static list routing_tables;
52

53
static void rt_format_via(rte *e, byte *via);
54 55 56 57
static void rt_free_hostcache(rtable *tab);
static void rt_notify_hostcache(rtable *tab, net *net);
static void rt_update_hostcache(rtable *tab);
static void rt_next_hop_update(rtable *tab);
58
static inline int rt_prune_table(rtable *tab);
59
static inline void rt_schedule_gc(rtable *tab);
60 61
static inline void rt_schedule_prune(rtable *tab);

62

63 64 65 66 67 68 69 70
static inline struct ea_list *
make_tmp_attrs(struct rte *rt, struct linpool *pool)
{
  struct ea_list *(*mta)(struct rte *rt, struct linpool *pool);
  mta = rt->attrs->src->proto->make_tmp_attrs;
  return mta ? mta(rt, rte_update_pool) : NULL;
}

71

72
/* Like fib_route(), but skips empty net entries */
73 74
static inline void *
net_route_ip4(struct fib *f, net_addr_ip4 *n)
75
{
76
  net *r;
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  while (r = fib_find(f, (net_addr *) n),
	 !(r && rte_is_valid(r->routes)) && (n->pxlen > 0))
  {
    n->pxlen--;
    ip4_clrbit(&n->prefix, n->pxlen);
  }

  return r;
}

static inline void *
net_route_ip6(struct fib *f, net_addr_ip6 *n)
{
  net *r;

  while (r = fib_find(f, (net_addr *) n),
	 !(r && rte_is_valid(r->routes)) && (n->pxlen > 0))
  {
    n->pxlen--;
    ip6_clrbit(&n->prefix, n->pxlen);
  }

  return r;
}

void *
net_route(rtable *tab, const net_addr *n)
{
  ASSERT(f->addr_type == n->type);

  net_addr *n0 = alloca(n->length);
  net_copy(n0, n);

  switch (n->type)
  {
  case NET_IP4:
  case NET_VPN4:
    return net_route_ip4(&tab->fib, (net_addr_ip4 *) n0);

  case NET_IP6:
  case NET_VPN6:
    return net_route_ip6(&tab->fib, (net_addr_ip6 *) n0);

  default:
    return NULL;
  }
124
}
125

Martin Mareš's avatar
Martin Mareš committed
126 127 128
/**
 * rte_find - find a route
 * @net: network node
129
 * @src: route source
Martin Mareš's avatar
Martin Mareš committed
130 131
 *
 * The rte_find() function returns a route for destination @net
132
 * which is from route source @src.
Martin Mareš's avatar
Martin Mareš committed
133
 */
134
rte *
135
rte_find(net *net, struct rte_src *src)
136 137 138
{
  rte *e = net->routes;

139
  while (e && e->attrs->src != src)
140 141 142 143
    e = e->next;
  return e;
}

Martin Mareš's avatar
Martin Mareš committed
144 145
/**
 * rte_get_temp - get a temporary &rte
146
 * @a: attributes to assign to the new route (a &rta; in case it's
147
 * un-cached, rte_update() will create a cached copy automatically)
Martin Mareš's avatar
Martin Mareš committed
148 149 150 151 152
 *
 * Create a temporary &rte and bind it with the attributes @a.
 * Also set route preference to the default preference set for
 * the protocol.
 */
153 154 155 156 157 158
rte *
rte_get_temp(rta *a)
{
  rte *e = sl_alloc(rte_slab);

  e->attrs = a;
159
  e->flags = 0;
160
  e->pref = a->src->proto->preference;
161 162 163
  return e;
}

164 165 166 167 168 169 170 171 172 173 174
rte *
rte_do_cow(rte *r)
{
  rte *e = sl_alloc(rte_slab);

  memcpy(e, r, sizeof(rte));
  e->attrs = rta_clone(r->attrs);
  e->flags = 0;
  return e;
}

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
/**
 * rte_cow_rta - get a private writable copy of &rte with writable &rta
 * @r: a route entry to be copied
 * @lp: a linpool from which to allocate &rta
 *
 * rte_cow_rta() takes a &rte and prepares it and associated &rta for
 * modification. There are three possibilities: First, both &rte and &rta are
 * private copies, in that case they are returned unchanged.  Second, &rte is
 * private copy, but &rta is cached, in that case &rta is duplicated using
 * rta_do_cow(). Third, both &rte is shared and &rta is cached, in that case
 * both structures are duplicated by rte_do_cow() and rta_do_cow().
 *
 * Note that in the second case, cached &rta loses one reference, while private
 * copy created by rta_do_cow() is a shallow copy sharing indirect data (eattrs,
 * nexthops, ...) with it. To work properly, original shared &rta should have
 * another reference during the life of created private copy.
 *
 * Result: a pointer to the new writable &rte with writable &rta.
 */
rte *
rte_cow_rta(rte *r, linpool *lp)
{
  if (!rta_is_cached(r->attrs))
    return r;

  rte *e = rte_cow(r);
  rta *a = rta_do_cow(r->attrs, lp);
  rta_free(e->attrs);
  e->attrs = a;
  return e;
}

207 208 209
static int				/* Actually better or at least as good as */
rte_better(rte *new, rte *old)
{
210 211
  int (*better)(rte *, rte *);

212
  if (!rte_is_valid(old))
213
    return 1;
214 215 216
  if (!rte_is_valid(new))
    return 0;

217 218 219 220
  if (new->pref > old->pref)
    return 1;
  if (new->pref < old->pref)
    return 0;
221
  if (new->attrs->src->proto->proto != old->attrs->src->proto->proto)
222 223 224 225 226 227
    {
      /*
       *  If the user has configured protocol preferences, so that two different protocols
       *  have the same preference, try to break the tie by comparing addresses. Not too
       *  useful, but keeps the ordering of routes unambiguous.
       */
228
      return new->attrs->src->proto->proto > old->attrs->src->proto->proto;
229
    }
230
  if (better = new->attrs->src->proto->rte_better)
231 232
    return better(new, old);
  return 0;
233 234
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
static int
rte_mergable(rte *pri, rte *sec)
{
  int (*mergable)(rte *, rte *);

  if (!rte_is_valid(pri) || !rte_is_valid(sec))
    return 0;

  if (pri->pref != sec->pref)
    return 0;

  if (pri->attrs->src->proto->proto != sec->attrs->src->proto->proto)
    return 0;

  if (mergable = pri->attrs->src->proto->rte_mergable)
    return mergable(pri, sec);

  return 0;
}

255 256 257
static void
rte_trace(struct proto *p, rte *e, int dir, char *msg)
{
258
  byte via[IPA_MAX_TEXT_LENGTH+32];
259 260

  rt_format_via(e, via);
261
  log(L_TRACE "%s %c %s %N %s", p->name, dir, msg, e->net->n.addr, via);
262 263 264
}

static inline void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
265
rte_trace_in(uint flag, struct proto *p, rte *e, char *msg)
266 267
{
  if (p->debug & flag)
268
    rte_trace(p, e, '>', msg);
269 270 271
}

static inline void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
272
rte_trace_out(uint flag, struct proto *p, rte *e, char *msg)
273 274
{
  if (p->debug & flag)
275
    rte_trace(p, e, '<', msg);
276 277
}

278 279
static rte *
export_filter(struct announce_hook *ah, rte *rt0, rte **rt_free, ea_list **tmpa, int silent)
280
{
281 282 283
  struct proto *p = ah->proto;
  struct filter *filter = ah->out_filter;
  struct proto_stats *stats = ah->stats;
284 285 286
  ea_list *tmpb = NULL;
  rte *rt;
  int v;
287

288 289
  rt = rt0;
  *rt_free = NULL;
290

291
  if (!tmpa)
292 293 294
    tmpa = &tmpb;

  *tmpa = make_tmp_attrs(rt, rte_update_pool);
295

296 297 298 299 300
  v = p->import_control ? p->import_control(p, &rt, tmpa, rte_update_pool) : 0;
  if (v < 0)
    {
      if (silent)
	goto reject;
301

302
      stats->exp_updates_rejected++;
303 304
      if (v == RIC_REJECT)
	rte_trace_out(D_FILTERS, p, rt, "rejected by protocol");
305 306 307
      goto reject;
    }
  if (v > 0)
308
    {
309 310 311
      if (!silent)
	rte_trace_out(D_FILTERS, p, rt, "forced accept by protocol");
      goto accept;
312
    }
313

314 315 316 317 318 319 320 321 322 323
  v = filter && ((filter == FILTER_REJECT) ||
		 (f_run(filter, &rt, tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT));
  if (v)
    {
      if (silent)
	goto reject;

      stats->exp_updates_filtered++;
      rte_trace_out(D_FILTERS, p, rt, "filtered out");
      goto reject;
324
    }
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
 accept:
  if (rt != rt0)
    *rt_free = rt;
  return rt;

 reject:
  /* Discard temporary rte */
  if (rt != rt0)
    rte_free(rt);
  return NULL;
}

static void
do_rt_notify(struct announce_hook *ah, net *net, rte *new, rte *old, ea_list *tmpa, int refeed)
{
  struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;
343

344

345
  /*
346 347
   * First, apply export limit.
   *
348 349
   * Export route limits has several problems. Because exp_routes
   * counter is reset before refeed, we don't really know whether
350
   * limit is breached and whether the update is new or not. Therefore
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
   * the number of really exported routes may exceed the limit
   * temporarily (routes exported before and new routes in refeed).
   *
   * Minor advantage is that if the limit is decreased and refeed is
   * requested, the number of exported routes really decrease.
   *
   * Second problem is that with export limits, we don't know whether
   * old was really exported (it might be blocked by limit). When a
   * withdraw is exported, we announce it even when the previous
   * update was blocked. This is not a big issue, but the same problem
   * is in updating exp_routes counter. Therefore, to be consistent in
   * increases and decreases of exp_routes, we count exported routes
   * regardless of blocking by limits.
   *
   * Similar problem is in handling updates - when a new route is
   * received and blocking is active, the route would be blocked, but
   * when an update for the route will be received later, the update
   * would be propagated (as old != NULL). Therefore, we have to block
   * also non-new updates (contrary to import blocking).
   */
371

372
  struct proto_limit *l = ah->out_limit;
373
  if (l && new)
374
    {
375
      if ((!old || refeed) && (stats->exp_routes >= l->limit))
376
	proto_notify_limit(ah, l, PLD_OUT, stats->exp_routes);
377 378 379

      if (l->state == PLS_BLOCKED)
	{
380
	  stats->exp_routes++;	/* see note above */
381 382
	  stats->exp_updates_rejected++;
	  rte_trace_out(D_FILTERS, p, new, "rejected [limit]");
383
	  new = NULL;
384 385 386

	  if (!old)
	    return;
387 388 389
	}
    }

390

391
  if (new)
392
    stats->exp_updates_accepted++;
393
  else
394
    stats->exp_withdraws_accepted++;
395

396 397
  /* Hack: We do not decrease exp_routes during refeed, we instead
     reset exp_routes at the start of refeed. */
398
  if (new)
399
    stats->exp_routes++;
400
  if (old && !refeed)
401
    stats->exp_routes--;
402

403 404 405 406 407 408
  if (p->debug & D_ROUTES)
    {
      if (new && old)
	rte_trace_out(D_ROUTES, p, new, "replaced");
      else if (new)
	rte_trace_out(D_ROUTES, p, new, "added");
409
      else if (old)
410 411
	rte_trace_out(D_ROUTES, p, old, "removed");
    }
412
  if (!new)
413
    p->rt_notify(p, ah->table, net, NULL, old, NULL);
414 415
  else if (tmpa)
    {
416 417 418 419
      ea_list *t = tmpa;
      while (t->next)
	t = t->next;
      t->next = new->attrs->eattrs;
420
      p->rt_notify(p, ah->table, net, new, old, tmpa);
421
      t->next = NULL;
422 423
    }
  else
424
    p->rt_notify(p, ah->table, net, new, old, new->attrs->eattrs);
425 426 427
}

static void
428
rt_notify_basic(struct announce_hook *ah, net *net, rte *new0, rte *old0, int refeed)
429
{
430
  struct proto *p = ah->proto;
431 432
  struct proto_stats *stats = ah->stats;

433 434
  rte *new = new0;
  rte *old = old0;
435 436
  rte *new_free = NULL;
  rte *old_free = NULL;
437
  ea_list *tmpa = NULL;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

  if (new)
    stats->exp_updates_received++;
  else
    stats->exp_withdraws_received++;

  /*
   * This is a tricky part - we don't know whether route 'old' was
   * exported to protocol 'p' or was filtered by the export filter.
   * We try to run the export filter to know this to have a correct
   * value in 'old' argument of rte_update (and proper filter value)
   *
   * FIXME - this is broken because 'configure soft' may change
   * filters but keep routes. Refeed is expected to be called after
   * change of the filters and with old == new, therefore we do not
453
   * even try to run the filter on an old route, This may lead to
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
   * 'spurious withdraws' but ensure that there are no 'missing
   * withdraws'.
   *
   * This is not completely safe as there is a window between
   * reconfiguration and the end of refeed - if a newly filtered
   * route disappears during this period, proper withdraw is not
   * sent (because old would be also filtered) and the route is
   * not refeeded (because it disappeared before that).
   */

  if (new)
    new = export_filter(ah, new, &new_free, &tmpa, 0);

  if (old && !refeed)
    old = export_filter(ah, old, &old_free, NULL, 1);

  if (!new && !old)
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  {
    /*
     * As mentioned above, 'old' value may be incorrect in some race conditions.
     * We generally ignore it with the exception of withdraw to pipe protocol.
     * In that case we rather propagate unfiltered withdraws regardless of
     * export filters to ensure that when a protocol is flushed, its routes are
     * removed from all tables. Possible spurious unfiltered withdraws are not
     * problem here as they are ignored if there is no corresponding route at
     * the other end of the pipe. We directly call rt_notify() hook instead of
     * do_rt_notify() to avoid logging and stat counters.
     */

#ifdef CONFIG_PIPE
    if ((p->proto == &proto_pipe) && !new0 && (p != old0->sender->proto))
      p->rt_notify(p, ah->table, net, NULL, old0, NULL);
#endif

488
    return;
489
  }
490 491 492 493 494 495 496 497 498 499 500

  do_rt_notify(ah, net, new, old, tmpa, refeed);

  /* Discard temporary rte's */
  if (new_free)
    rte_free(new_free);
  if (old_free)
    rte_free(old_free);
}

static void
501
rt_notify_accepted(struct announce_hook *ah, net *net, rte *new_changed, rte *old_changed, rte *before_old, int feed)
502 503 504 505
{
  // struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;

506
  rte *r;
507 508 509 510
  rte *new_best = NULL;
  rte *old_best = NULL;
  rte *new_free = NULL;
  rte *old_free = NULL;
511
  ea_list *tmpa = NULL;
512

513 514 515 516 517 518 519
  /* Used to track whether we met old_changed position. If before_old is NULL
     old_changed was the first and we met it implicitly before current best route. */
  int old_meet = old_changed && !before_old;

  /* Note that before_old is either NULL or valid (not rejected) route.
     If old_changed is valid, before_old have to be too. If old changed route
     was not valid, caller must use NULL for both old_changed and before_old. */
520 521 522 523 524 525 526

  if (new_changed)
    stats->exp_updates_received++;
  else
    stats->exp_withdraws_received++;

  /* First, find the new_best route - first accepted by filters */
527
  for (r=net->routes; rte_is_valid(r); r=r->next)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    {
      if (new_best = export_filter(ah, r, &new_free, &tmpa, 0))
	break;

      /* Note if we walked around the position of old_changed route */
      if (r == before_old)
	old_meet = 1;
    }

  /* 
   * Second, handle the feed case. That means we do not care for
   * old_best. It is NULL for feed, and the new_best for refeed. 
   * For refeed, there is a hack similar to one in rt_notify_basic()
   * to ensure withdraws in case of changed filters
   */
  if (feed)
    {
      if (feed == 2)	/* refeed */
546 547
	old_best = new_best ? new_best :
	  (rte_is_valid(net->routes) ? net->routes : NULL);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
      else
	old_best = NULL;

      if (!new_best && !old_best)
	return;

      goto found;
    }

  /*
   * Now, we find the old_best route. Generally, it is the same as the
   * new_best, unless new_best is the same as new_changed or
   * old_changed is accepted before new_best.
   *
   * There are four cases:
   *
   * - We would find and accept old_changed before new_best, therefore
   *   old_changed is old_best. In remaining cases we suppose this
   *   is not true.
   *
   * - We found no new_best, therefore there is also no old_best and
   *   we ignore this withdraw.
   *
   * - We found new_best different than new_changed, therefore
   *   old_best is the same as new_best and we ignore this update.
   *
   * - We found new_best the same as new_changed, therefore it cannot
   *   be old_best and we have to continue search for old_best.
   */

  /* First case */
  if (old_meet)
    if (old_best = export_filter(ah, old_changed, &old_free, NULL, 1))
      goto found;

  /* Second case */
  if (!new_best)
    return;
586

587
  /* Third case, we use r instead of new_best, because export_filter() could change it */
588 589 590 591 592 593 594 595
  if (r != new_changed)
    {
      if (new_free)
	rte_free(new_free);
      return;
    }

  /* Fourth case */
596
  for (r=r->next; rte_is_valid(r); r=r->next)
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    {
      if (old_best = export_filter(ah, r, &old_free, NULL, 1))
	goto found;

      if (r == before_old)
	if (old_best = export_filter(ah, old_changed, &old_free, NULL, 1))
	  goto found;
    }

  /* Implicitly, old_best is NULL and new_best is non-NULL */

 found:
  do_rt_notify(ah, net, new_best, old_best, tmpa, (feed == 2));

  /* Discard temporary rte's */
  if (new_free)
    rte_free(new_free);
  if (old_free)
    rte_free(old_free);
616 617
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

static struct mpnh *
mpnh_merge_rta(struct mpnh *nhs, rta *a, int max)
{
  struct mpnh nh = { .gw = a->gw, .iface = a->iface };
  struct mpnh *nh2 = (a->dest == RTD_MULTIPATH) ? a->nexthops : &nh;
  return mpnh_merge(nhs, nh2, 1, 0, max, rte_update_pool);
}

rte *
rt_export_merged(struct announce_hook *ah, net *net, rte **rt_free, ea_list **tmpa, int silent)
{
  // struct proto *p = ah->proto;
  struct mpnh *nhs = NULL;
  rte *best0, *best, *rt0, *rt, *tmp;

  best0 = net->routes;
  *rt_free = NULL;

  if (!rte_is_valid(best0))
    return NULL;

  best = export_filter(ah, best0, rt_free, tmpa, silent);

  if (!best || !rte_is_reachable(best))
    return best;

  for (rt0 = best0->next; rt0; rt0 = rt0->next)
  {
    if (!rte_mergable(best0, rt0))
      continue;

    rt = export_filter(ah, rt0, &tmp, NULL, 1);

    if (!rt)
      continue;

    if (rte_is_reachable(rt))
      nhs = mpnh_merge_rta(nhs, rt->attrs, ah->proto->merge_limit);

    if (tmp)
      rte_free(tmp);
  }

  if (nhs)
  {
    nhs = mpnh_merge_rta(nhs, best->attrs, ah->proto->merge_limit);

    if (nhs->next)
    {
      best = rte_cow_rta(best, rte_update_pool);
      best->attrs->dest = RTD_MULTIPATH;
      best->attrs->nexthops = nhs;
    }
  }

  if (best != best0)
    *rt_free = best;

  return best;
}


static void
rt_notify_merged(struct announce_hook *ah, net *net, rte *new_changed, rte *old_changed,
		 rte *new_best, rte*old_best, int refeed)
{
  // struct proto *p = ah->proto;

  rte *new_best_free = NULL;
  rte *old_best_free = NULL;
  rte *new_changed_free = NULL;
  rte *old_changed_free = NULL;
  ea_list *tmpa = NULL;

  /* We assume that all rte arguments are either NULL or rte_is_valid() */

  /* This check should be done by the caller */
  if (!new_best && !old_best)
    return;

  /* Check whether the change is relevant to the merged route */
  if ((new_best == old_best) && !refeed)
  {
    new_changed = rte_mergable(new_best, new_changed) ?
      export_filter(ah, new_changed, &new_changed_free, NULL, 1) : NULL;

    old_changed = rte_mergable(old_best, old_changed) ?
      export_filter(ah, old_changed, &old_changed_free, NULL, 1) : NULL;

    if (!new_changed && !old_changed)
      return;
  }

  if (new_best)
    ah->stats->exp_updates_received++;
  else
    ah->stats->exp_withdraws_received++;

  /* Prepare new merged route */
  if (new_best)
    new_best = rt_export_merged(ah, net, &new_best_free, &tmpa, 0);

  /* Prepare old merged route (without proper merged next hops) */
  /* There are some issues with running filter on old route - see rt_notify_basic() */
  if (old_best && !refeed)
    old_best = export_filter(ah, old_best, &old_best_free, NULL, 1);

  if (new_best || old_best)
    do_rt_notify(ah, net, new_best, old_best, tmpa, refeed);

  /* Discard temporary rte's */
  if (new_best_free)
    rte_free(new_best_free);
  if (old_best_free)
    rte_free(old_best_free);
  if (new_changed_free)
    rte_free(new_changed_free);
  if (old_changed_free)
    rte_free(old_changed_free);
}


741 742 743
/**
 * rte_announce - announce a routing table change
 * @tab: table the route has been added to
744
 * @type: type of route announcement (RA_OPTIMAL or RA_ANY)
745 746
 * @net: network in question
 * @new: the new route to be announced
747
 * @old: the previous route for the same network
748 749
 *
 * This function gets a routing table update and announces it
Ondřej Zajíček's avatar
Ondřej Zajíček committed
750 751
 * to all protocols that acccepts given type of route announcement
 * and are connected to the same table by their announcement hooks.
752
 *
Ondřej Zajíček's avatar
Ondřej Zajíček committed
753 754 755
 * Route announcement of type RA_OPTIMAL si generated when optimal
 * route (in routing table @tab) changes. In that case @old stores the
 * old optimal route.
756
 *
Ondřej Zajíček's avatar
Ondřej Zajíček committed
757 758 759 760 761 762 763 764 765 766 767
 * Route announcement of type RA_ANY si generated when any route (in
 * routing table @tab) changes In that case @old stores the old route
 * from the same protocol.
 *
 * For each appropriate protocol, we first call its import_control()
 * hook which performs basic checks on the route (each protocol has a
 * right to veto or force accept of the route before any filter is
 * asked) and adds default values of attributes specific to the new
 * protocol (metrics, tags etc.).  Then it consults the protocol's
 * export filter and if it accepts the route, the rt_notify() hook of
 * the protocol gets called.
768
 */
769
static void
770 771
rte_announce(rtable *tab, unsigned type, net *net, rte *new, rte *old,
	     rte *new_best, rte *old_best, rte *before_old)
772
{
773 774 775
  if (!rte_is_valid(new))
    new = NULL;

776 777 778
  if (!rte_is_valid(old))
    old = before_old = NULL;

779 780 781 782 783
  if (!rte_is_valid(new_best))
    new_best = NULL;

  if (!rte_is_valid(old_best))
    old_best = NULL;
784 785 786

  if (!old && !new)
    return;
787

788 789 790
  if (type == RA_OPTIMAL)
    {
      if (new)
791
	new->attrs->src->proto->stats.pref_routes++;
792
      if (old)
793
	old->attrs->src->proto->stats.pref_routes--;
794 795 796

      if (tab->hostcache)
	rt_notify_hostcache(tab, net);
797 798
    }

799
  struct announce_hook *a;
800
  WALK_LIST(a, tab->hooks)
801
    {
802
      ASSERT(a->proto->export_state != ES_DOWN);
803
      if (a->proto->accept_ra_types == type)
804
	if (type == RA_ACCEPTED)
805
	  rt_notify_accepted(a, net, new, old, before_old, 0);
806 807
	else if (type == RA_MERGED)
	  rt_notify_merged(a, net, new, old, new_best, old_best, 0);
808
	else
809
	  rt_notify_basic(a, net, new, old, 0);
810
    }
811 812
}

813 814 815 816 817 818
static inline int
rte_validate(rte *e)
{
  int c;
  net *n = e->net;

819 820 821 822 823 824 825
  // (n->n.pxlen > BITS_PER_IP_ADDRESS) || !ip_is_prefix(n->n.prefix,n->n.pxlen))
  if (!net_validate(n->n.addr))
  {
    log(L_WARN "Ignoring bogus prefix %N received via %s",
	n->n.addr, e->sender->proto->name);
    return 0;
  }
826

827
  c = net_classify(n->n.addr);
828
  if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
829 830 831 832 833
  {
    log(L_WARN "Ignoring bogus route %N received via %s",
	n->n.addr, e->sender->proto->name);
    return 0;
  }
834

835 836 837
  return 1;
}

Martin Mareš's avatar
Martin Mareš committed
838 839 840 841 842 843
/**
 * rte_free - delete a &rte
 * @e: &rte to be deleted
 *
 * rte_free() deletes the given &rte from the routing table it's linked to.
 */
844
void
845
rte_free(rte *e)
846
{
847
  if (rta_is_cached(e->attrs))
848 849 850 851 852 853
    rta_free(e->attrs);
  sl_free(rte_slab, e);
}

static inline void
rte_free_quick(rte *e)
854 855 856 857 858
{
  rta_free(e->attrs);
  sl_free(rte_slab, e);
}

859 860 861 862 863 864 865 866
static int
rte_same(rte *x, rte *y)
{
  return
    x->attrs == y->attrs &&
    x->flags == y->flags &&
    x->pflags == y->pflags &&
    x->pref == y->pref &&
867
    (!x->attrs->src->proto->rte_same || x->attrs->src->proto->rte_same(x, y));
868 869
}

870 871
static inline int rte_is_ok(rte *e) { return e && !rte_is_filtered(e); }

872
static void
873
rte_recalculate(struct announce_hook *ah, net *net, rte *new, struct rte_src *src)
874
{
875 876 877
  struct proto *p = ah->proto;
  struct rtable *table = ah->table;
  struct proto_stats *stats = ah->stats;
878
  static struct tbf rl_pipe = TBF_DEFAULT_LOG_LIMITS;
879
  rte *before_old = NULL;
880 881
  rte *old_best = net->routes;
  rte *old = NULL;
882
  rte **k;
883 884 885 886

  k = &net->routes;			/* Find and remove original route from the same protocol */
  while (old = *k)
    {
887
      if (old->attrs->src == src)
888
	{
889 890 891 892 893 894 895 896 897
	  /* If there is the same route in the routing table but from
	   * a different sender, then there are two paths from the
	   * source protocol to this routing table through transparent
	   * pipes, which is not allowed.
	   *
	   * We log that and ignore the route. If it is withdraw, we
	   * ignore it completely (there might be 'spurious withdraws',
	   * see FIXME in do_rte_announce())
	   */
898
	  if (old->sender->proto != p)
899 900 901
	    {
	      if (new)
		{
902 903
		  log_rl(&rl_pipe, L_ERR "Pipe collision detected when sending %N to table %s",
		      net->n.addr, table->name);
904 905 906 907 908
		  rte_free_quick(new);
		}
	      return;
	    }

909
	  if (new && rte_same(old, new))
910 911
	    {
	      /* No changes, ignore the new route */
912

913
	      if (!rte_is_filtered(new))
914 915 916 917 918
		{
		  stats->imp_updates_ignored++;
		  rte_trace_in(D_ROUTES, p, new, "ignored");
		}

919 920 921
	      rte_free_quick(new);
	      return;
	    }
922 923 924 925
	  *k = old->next;
	  break;
	}
      k = &old->next;
926
      before_old = old;
927 928
    }

929 930 931
  if (!old)
    before_old = NULL;

932 933
  if (!old && !new)
    {
934
      stats->imp_withdraws_ignored++;
935 936 937
      return;
    }

938 939 940 941
  int new_ok = rte_is_ok(new);
  int old_ok = rte_is_ok(old);

  struct proto_limit *l = ah->rx_limit;
942
  if (l && !old && new)
943
    {
944
      u32 all_routes = stats->imp_routes + stats->filt_routes;
945 946

      if (all_routes >= l->limit)
947
	proto_notify_limit(ah, l, PLD_RX, all_routes);
948 949 950

      if (l->state == PLS_BLOCKED)
	{
951 952 953
	  /* In receive limit the situation is simple, old is NULL so
	     we just free new and exit like nothing happened */

954 955 956 957 958
	  stats->imp_updates_ignored++;
	  rte_trace_in(D_FILTERS, p, new, "ignored [limit]");
	  rte_free_quick(new);
	  return;
	}
959 960
    }

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
  l = ah->in_limit;
  if (l && !old_ok && new_ok)
    {
      if (stats->imp_routes >= l->limit)
	proto_notify_limit(ah, l, PLD_IN, stats->imp_routes);

      if (l->state == PLS_BLOCKED)
	{
	  /* In import limit the situation is more complicated. We
	     shouldn't just drop the route, we should handle it like
	     it was filtered. We also have to continue the route
	     processing if old or new is non-NULL, but we should exit
	     if both are NULL as this case is probably assumed to be
	     already handled. */

	  stats->imp_updates_ignored++;
	  rte_trace_in(D_FILTERS, p, new, "ignored [limit]");

	  if (ah->in_keep_filtered)
	    new->flags |= REF_FILTERED;
	  else
	    { rte_free_quick(new); new = NULL; }

	  /* Note that old && !new could be possible when
	     ah->in_keep_filtered changed in the recent past. */

	  if (!old && !new)
	    return;

	  new_ok = 0;
	  goto skip_stats1;
	}
    }
994 995

  if (new_ok)
996
    stats->imp_updates_accepted++;
997
  else if (old_ok)
998
    stats->imp_withdraws_accepted++;
999 1000
  else
    stats->imp_withdraws_ignored++;
1001

1002
 skip_stats1:
1003 1004

  if (new)
1005
    rte_is_filtered(new) ? stats->filt_routes++ : stats->imp_routes++;
1006
  if (old)
1007
    rte_is_filtered(old) ? stats->filt_routes-- : stats->imp_routes--;
1008

1009
  if (table->config->sorted)
1010
    {
1011 1012 1013 1014 1015 1016 1017
      /* If routes are sorted, just insert new route to appropriate position */
      if (new)
	{
	  if (before_old && !rte_better(new, before_old))
	    k = &before_old->next;
	  else
	    k = &net->routes;
1018

1019 1020 1021
	  for (; *k; k=&(*k)->next)
	    if (rte_better(new, *k))
	      break;
1022

1023 1024 1025
	  new->next = *k;
	  *k = new;
	}
1026
    }
1027
  else
1028
    {
1029 1030 1031
      /* If routes are not sorted, find the best route and move it on
	 the first position. There are several optimized cases. */

1032
      if (src->proto->rte_recalculate && src->proto->rte_recalculate(table, net, new, old, old_best))
1033 1034 1035
	goto do_recalculate;

      if (new && rte_better(new, old_best))
1036
	{
1037 1038 1039
	  /* The first case - the new route is cleary optimal,
	     we link it at the first position */

1040 1041 1042
	  new->next = net->routes;
	  net->routes = new;
	}
1043
      else if (old == old_best)
1044
	{
1045 1046 1047 1048 1049 1050 1051 1052 1053
	  /* The second case - the old best route disappeared, we add the
	     new route (if we have any) to the list (we don't care about
	     position) and then we elect the new optimal route and relink
	     that route at the first position and announce it. New optimal
	     route might be NULL if there is no more routes */

	do_recalculate:
	  /* Add the new route to the list */
	  if (new)
1054
	    {
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	      new->next = net->routes;
	      net->routes = new;
	    }

	  /* Find a new optimal route (if there is any) */
	  if (net->routes)
	    {
	      rte **bp = &net->routes;
	      for (k=&(*bp)->next; *k; k=&(*k)->next)
		if (rte_better(*k, *bp))
		  bp = k;

	      /* And relink it */
	      rte *best = *bp;
	      *bp = best->next;
	      best->next = net->routes;
	      net->routes = best;
1072 1073
	    }
	}
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      else if (new)
	{
	  /* The third case - the new route is not better than the old
	     best route (therefore old_best != NULL) and the old best
	     route was not removed (therefore old_best == net->routes).
	     We just link the new route after the old best route. */

	  ASSERT(net->routes != NULL);
	  new->next = net->routes->next;
	  net->routes->next = new;
	}
      /* The fourth (empty) case - suboptimal route was removed, nothing to do */
1086
    }
1087

1088 1089 1090 1091
  if (new)
    new->lastmod = now;

  /* Log the route change */
1092
  if (p->debug & D_ROUTES)
1093
    {
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
      if (new_ok)
	rte_trace(p, new, '>', new == net->routes ? "added [best]" : "added");
      else if (old_ok)
	{
	  if (old != old_best)
	    rte_trace(p, old, '>', "removed");
	  else if (rte_is_ok(net->routes))
	    rte_trace(p, old, '>', "removed [replaced]");
	  else
	    rte_trace(p, old, '>', "removed [sole]");
	}
1105 1106
    }

1107
  /* Propagate the route change */
1108
  rte_announce(table, RA_ANY, net, new, old, NULL, NULL, NULL);
1109
  if (net->routes != old_best)
1110
    rte_announce(table, RA_OPTIMAL, net, net->routes, old_best, NULL, NULL, NULL);
1111
  if (table->config->sorted)
1112 1113
    rte_announce(table, RA_ACCEPTED, net, new, old, NULL, NULL, before_old);
  rte_announce(table, RA_MERGED, net, new, old, net->routes, old_best, NULL);
1114 1115 1116 1117 1118 1119

  if (!net->routes &&
      (table->gc_counter++ >= table->config->gc_max_ops) &&
      (table->gc_time + table->config->gc_min_time <= now))
    rt_schedule_gc(table);

1120 1121 1122 1123 1124
  if (old_ok && p->rte_remove)
    p->rte_remove(net, old);
  if (new_ok && p->rte_insert)
    p->rte_insert(net, new);

1125
  if (old)
1126
    rte_free_quick(old);
1127 1128
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
static int rte_update_nest_cnt;		/* Nesting counter to allow recursive updates */

static inline void
rte_update_lock(void)
{
  rte_update_nest_cnt++;
}

static inline void
rte_update_unlock(void)
{
  if (!--rte_update_nest_cnt)
    lp_flush(rte_update_pool);
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
static inline void
rte_hide_dummy_routes(net *net, rte **dummy)
{
  if (net->routes && net->routes->attrs->source == RTS_DUMMY)
  {
    *dummy = net->routes;
    net->routes = (*dummy)->next;
  }
}

static inline void
rte_unhide_dummy_routes(net *net, rte **dummy)
{
  if (*dummy)
  {
    (*dummy)->next = net->routes;
    net->routes = *dummy;
  }
}

Martin Mareš's avatar
Martin Mareš committed
1164 1165 1166
/**
 * rte_update - enter a new update to a routing table
 * @table: table to be updated
1167
 * @ah: pointer to table announce hook
Martin Mareš's avatar
Martin Mareš committed
1168 1169
 * @net: network node
 * @p: protocol submitting the update
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1170
 * @src: protocol originating the update
Martin Mareš's avatar
Martin Mareš committed
1171 1172 1173 1174
 * @new: a &rte representing the new route or %NULL for route removal.
 *
 * This function is called by the routing protocols whenever they discover
 * a new route or wish to update/remove an existing route. The right announcement
1175
 * sequence is to build route attributes first (either un-cached with @aflags set
Martin Mareš's avatar
Martin Mareš committed
1176 1177 1178 1179 1180
 * to zero or a cached one using rta_lookup(); in this case please note that
 * you need to increase the use count of the attributes yourself by calling
 * rta_clone()), call rte_get_temp() to obtain a temporary &rte, fill in all
 * the appropriate data and finally submit the new &rte by calling rte_update().
 *
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1181 1182 1183 1184 1185 1186
 * @src specifies the protocol that originally created the route and the meaning
 * of protocol-dependent data of @new. If @new is not %NULL, @src have to be the
 * same value as @new->attrs->proto. @p specifies the protocol that called
 * rte_update(). In most cases it is the same protocol as @src. rte_update()
 * stores @p in @new->sender;
 *
1187 1188 1189 1190 1191 1192 1193 1194 1195
 * When rte_update() gets any route, it automatically validates it (checks,
 * whether the network and next hop address are valid IP addresses and also
 * whether a normal routing protocol doesn't try to smuggle a host or link
 * scope route to the table), converts all protocol dependent attributes stored
 * in the &rte to temporary extended attributes, consults import filters of the
 * protocol to see if the route should be accepted and/or its attributes modified,
 * stores the temporary attributes back to the &rte.
 *
 * Now, having a "public" version of the route, we
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1196
 * automatically find any old route defined by the protocol @src
Martin Mareš's avatar
Martin Mareš committed
1197 1198
 * for network @n, replace it by the new one (or removing it if @new is %NULL),
 * recalculate the optimal route for this destination and finally broadcast
1199
 * the change (if any) to all routing protocols by calling rte_announce().
1200 1201 1202 1203
 *
 * All memory used for attribute lists and other temporary allocations is taken
 * from a special linear pool @rte_update_pool and freed when rte_update()
 * finishes.
Martin Mareš's avatar
Martin Mareš committed
1204
 */
1205 1206

void
1207
rte_update2(struct announce_hook *ah, net *net, rte *new, struct rte_src *src)
1208
{
1209 1210 1211
  struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;
  struct filter *filter = ah->in_filter;
1212
  ea_list *tmpa = NULL;
1213
  rte *dummy = NULL;
1214 1215 1216 1217

  rte_update_lock();
  if (new)
    {
1218
      new->sender = ah;
1219

1220
      stats->imp_updates_received++;
1221 1222 1223
      if (!rte_validate(new))
	{
	  rte_trace_in(D_FILTERS, p, new, "invalid");
1224
	  stats->imp_updates_invalid++;
1225 1226
	  goto drop;
	}
1227

1228
      if (filter == FILTER_REJECT)
1229
	{
1230
	  stats->imp_updates_filtered++;
1231
	  rte_trace_in(D_FILTERS, p, new, "filtered out");
1232

1233
	  if (! ah->in_keep_filtered)
1234 1235 1236
	    goto drop;

	  /* new is a private copy, i could modify it */
1237
	  new->flags |= REF_FILTERED;
1238
	}
1239
      else
1240
	{
1241
	  tmpa = make_tmp_attrs(new, rte_update_pool);
1242
	  if (filter && (filter != FILTER_REJECT))
1243
	    {
1244 1245 1246 1247 1248 1249 1250
	      ea_list *old_tmpa = tmpa;
	      int fr = f_run(filter, &new, &tmpa, rte_update_pool, 0);
	      if (fr > F_ACCEPT)
		{
		  stats->imp_updates_filtered++;
		  rte_trace_in(D_FILTERS, p, new, "filtered out");

1251
		  if (! ah->in_keep_filtered)
1252 1253
		    goto drop;

1254
		  new->flags |= REF_FILTERED;
1255
		}
1256 1257
	      if (tmpa != old_tmpa && src->proto->store_tmp_attrs)
		src->proto->store_tmp_attrs(new, tmpa);
1258
	    }
1259
	}
1260
      if (!rta_is_cached(new->attrs)) /* Need to copy attributes */
1261 1262 1263
	new->attrs = rta_lookup(new->attrs);
      new->flags |= REF_COW;
    }
1264
  else
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    {
      stats->imp_withdraws_received++;

      if (!net || !src)
	{
	  stats->imp_withdraws_ignored++;
	  rte_update_unlock();
	  return;
	}
    }
1275

1276 1277
 recalc:
  rte_hide_dummy_routes(net, &dummy);
1278
  rte_recalculate(ah, net, new, src);
1279
  rte_unhide_dummy_routes(net, &dummy);
1280 1281 1282
  rte_update_unlock();
  return;

1283
 drop:
1284
  rte_free(new);
1285 1286
  new = NULL;
  goto recalc;
1287 1288
}

1289 1290 1291
/* Independent call to rte_announce(), used from next hop
   recalculation, outside of rte_update(). new must be non-NULL */
static inline void 
1292 1293
rte_announce_i(rtable *tab, unsigned type, net *net, rte *new, rte *old,
	       rte *new_best, rte *old_best)
1294 1295
{
  rte_update_lock();
1296
  rte_announce(tab, type, net, new, old, new_best, old_best, NULL);
1297 1298 1299
  rte_update_unlock();
}

1300
void
1301
rte_discard(rtable *t, rte *old)	/* Non-filtered route deletion, used during garbage collection */
1302
{
1303
  rte_update_lock();
1304
  rte_recalculate(old->sender, old->net, NULL, old->attrs->src);
1305
  rte_update_unlock();
1306 1307
}

1308 1309
/* Check rtable for best route to given net whether it would be exported do p */
int
1310
rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter)
1311
{
1312
  net *n = net_find(t, a);
1313 1314 1315 1316 1317 1318 1319 1320
  rte *rt = n ? n->routes : NULL;

  if (!rte_is_valid(rt))
    return 0;

  rte_update_lock();

  /* Rest is stripped down export_filter() */
1321
  ea_list *tmpa = make_tmp_attrs(rt, rte_update_pool);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  int v = p->import_control ? p->import_control(p, &rt, &tmpa, rte_update_pool) : 0;
  if (v == RIC_PROCESS)
    v = (f_run(filter, &rt, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);

   /* Discard temporary rte */
  if (rt != n->routes)
    rte_free(rt);

  rte_update_unlock();

  return v > 0;
}

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

/**
 * rt_refresh_begin - start a refresh cycle
 * @t: related routing table
 * @ah: related announce hook 
 *
 * This function starts a refresh cycle for given routing table and announce
 * hook. The refresh cycle is a sequence where the protocol sends all its valid
 * routes to the routing table (by rte_update()). After that, all protocol
 * routes (more precisely routes with @ah as @sender) not sent during the
 * refresh cycle but still in the table from the past are pruned. This is
 * implemented by marking all related routes as stale by REF_STALE flag in
 * rt_refresh_begin(), then marking all related stale routes with REF_DISCARD
 * flag in rt_refresh_end() and then removing such routes in the prune loop.
 */
1350 1351 1352
void
rt_refresh_begin(rtable *t, struct announce_hook *ah)
{
1353
  FIB_WALK(&t->fib, net, n)
1354
    {
1355
      rte *e;
1356 1357 1358 1359 1360 1361 1362
      for (e = n->routes; e; e = e->next)
	if (e->sender == ah)
	  e->flags |= REF_STALE;
    }
  FIB_WALK_END;
}

1363 1364 1365 1366 1367 1368 1369 1370
/**
 * rt_refresh_end - end a refresh cycle
 * @t: related routing table
 * @ah: related announce hook 
 *
 * This function starts a refresh cycle for given routing table and announce
 * hook. See rt_refresh_begin() for description of refresh cycles.
 */
1371 1372 1373 1374 1375
void
rt_refresh_end(rtable *t, struct announce_hook *ah)
{
  int prune = 0;

1376
  FIB_WALK(&t->fib, net, n)
1377
    {
1378
      rte *e;
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
      for (e = n->routes; e; e = e->next)
	if ((e->sender == ah) && (e->flags & REF_STALE))
	  {
	    e->flags |= REF_DISCARD;
	    prune = 1;
	  }
    }
  FIB_WALK_END;

  if (prune)
    rt_schedule_prune(t);
}


Martin Mareš's avatar
Martin Mareš committed
1393 1394 1395 1396 1397 1398
/**
 * rte_dump - dump a route
 * @e: &rte to be dumped
 *
 * This functions dumps contents of a &rte to debug output.
 */
1399
void
1400
rte_dump(rte *e)
1401
{
1402
  net *n = e->net;
1403
  debug("%-1N ", n->n.addr);
1404
  debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
1405
  rta_dump(e->attrs);
1406 1407
  if (e->attrs->src->proto->proto->dump_attrs)
    e->attrs->src->proto->proto->dump_attrs(e);
1408
  debug("\n");
1409
}
1410

Martin Mareš's avatar
Martin Mareš committed
1411 1412 1413 1414 1415 1416
/**
 * rt_dump - dump a routing table
 * @t: routing table to be dumped
 *
 * This function dumps contents of a given routing table to debug output.
 */
1417 1418 1419
void
rt_dump(rtable *t)
{
1420
  debug("Dump of routing table <%s>\n", t->name);
1421
#ifdef DEBUGGING
1422
  fib_check(&t->fib);
1423
#endif
1424
  FIB_WALK(&t->fib, net, n)
1425
    {
1426
      rte *e;
1427 1428
      for(e=n->routes; e; e=e->next)
	rte_dump(e);
1429
    }
1430
  FIB_WALK_END;
1431 1432

  struct announce_hook *a;
1433 1434
  WALK_LIST(a, t->hooks)
    debug("\tAnnounces routes to protocol %s\n", a->proto->name);
1435
  debug("\n");
1436
}
1437

Martin Mareš's avatar
Martin Mareš committed
1438 1439 1440 1441 1442
/**
 * rt_dump_all - dump all routing tables
 *
 * This function dumps contents of all routing tables to debug output.
 */
1443 1444 1445
void
rt_dump_all(void)
{
1446 1447 1448 1449
  rtable *t;

  WALK_LIST(t, routing_tables)
    rt_dump(t);
1450 1451
}

1452 1453 1454 1455 1456 1457 1458
static inline void
rt_schedule_prune(rtable *tab)
{
  rt_mark_for_prune(tab);
  ev_schedule(tab->rt_event);
}

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
static inline void
rt_schedule_gc(rtable *tab)
{
  if (tab->gc_scheduled)
    return;

  tab->gc_scheduled = 1;
  ev_schedule(tab->rt_event);
}

static inline void
rt_schedule_hcu(rtable *tab)
{
  if (tab->hcu_scheduled)
    return;

  tab->hcu_scheduled = 1;
  ev_schedule(tab->rt_event);
}

static inline void
rt_schedule_nhu(rtable *tab)
{
  if (tab->nhu_state == 0)
    ev_schedule(tab->rt_event);

  /* state change 0->1, 2->3 */
  tab->nhu_state |= 1;
}

1489

1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
static void
rt_prune_nets(rtable *tab)
{
  struct fib_iterator fit;
  int ncnt = 0, ndel = 0;

#ifdef DEBUGGING
  fib_check(&tab->fib);
#endif

  FIB_ITERATE_INIT(&fit, &tab->fib);
again:
1502
  FIB_ITERATE_START(&tab->fib, &fit, net, n)
1503 1504 1505 1506
    {
      ncnt++;
      if (!n->routes)		/* Orphaned FIB entry */
	{
1507 1508
	  FIB_ITERATE_PUT(&fit);
	  fib_delete(&tab->fib, n);
1509 1510 1511 1512
	  ndel++;
	  goto again;
	}
    }
1513
  FIB_ITERATE_END;
1514 1515 1516 1517 1518 1519 1520
  DBG("Pruned %d of %d networks\n", ndel, ncnt);

  tab->gc_counter = 0;
  tab->gc_time = now;
  tab->gc_scheduled = 0;
}

1521
static void
1522
rt_event