pipe.c 4.87 KB
Newer Older
Martin Mareš's avatar
Martin Mareš committed
1 2 3
/*
 *	BIRD -- Table-to-Table Routing Protocol a.k.a Pipe
 *
4
 *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
Martin Mareš's avatar
Martin Mareš committed
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 19 20
/**
 * DOC: Pipe
 *
 * The Pipe protocol is very simple. It just connects to two routing tables
 * using proto_add_announce_hook() and whenever it receives a rt_notify()
 * about a change in one of the tables, it converts it to a rte_update()
 * in the other one.
 *
 * To avoid pipe loops, Pipe keeps a `being updated' flag in each routing
 * table.
 */

21
#undef LOCAL_DEBUG
Martin Mareš's avatar
Martin Mareš committed
22 23 24 25 26 27 28

#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "nest/route.h"
#include "conf/conf.h"
#include "filter/filter.h"
29
#include "lib/string.h"
Martin Mareš's avatar
Martin Mareš committed
30 31 32 33

#include "pipe.h"

static void
34
pipe_send(struct pipe_proto *p, rtable *dest, net *n, rte *new, rte *old, ea_list *attrs)
Martin Mareš's avatar
Martin Mareš committed
35
{
36
  struct proto *src;
Martin Mareš's avatar
Martin Mareš committed
37 38 39 40
  net *nn;
  rte *e;
  rta a;

41 42 43
  if (!new && !old)
    return;

Martin Mareš's avatar
Martin Mareš committed
44 45 46 47 48 49 50 51 52 53 54
  if (dest->pipe_busy)
    {
      log(L_ERR "Pipe loop detected when sending %I/%d to table %s",
	  n->n.prefix, n->n.pxlen, dest->name);
      return;
    }
  nn = net_get(dest, n->n.prefix, n->n.pxlen);
  if (new)
    {
      memcpy(&a, new->attrs, sizeof(rta));
      a.aflags = 0;
55
      a.eattrs = attrs;
Martin Mareš's avatar
Martin Mareš committed
56 57
      e = rte_get_temp(&a);
      e->net = nn;
58 59 60 61 62

      /* Copy protocol specific embedded attributes. */
      memcpy(&(e->u), &(new->u), sizeof(e->u));

      src = new->attrs->proto;
Martin Mareš's avatar
Martin Mareš committed
63 64
    }
  else
65 66 67 68 69
    {
      e = NULL;
      src = old->attrs->proto;
    }

Martin Mareš's avatar
Martin Mareš committed
70
  dest->pipe_busy = 1;
71
  rte_update2(dest, nn, &p->p, src, e);
Martin Mareš's avatar
Martin Mareš committed
72 73 74 75
  dest->pipe_busy = 0;
}

static void
76
pipe_rt_notify_pri(struct proto *P, net *net, rte *new, rte *old, ea_list *attrs)
Martin Mareš's avatar
Martin Mareš committed
77 78 79 80
{
  struct pipe_proto *p = (struct pipe_proto *) P;

  DBG("PIPE %c> %I/%d\n", (new ? '+' : '-'), net->n.prefix, net->n.pxlen);
81
  pipe_send(p, p->peer, net, new, old, attrs);
Martin Mareš's avatar
Martin Mareš committed
82 83 84
}

static void
85
pipe_rt_notify_sec(struct proto *P, net *net, rte *new, rte *old, ea_list *attrs)
Martin Mareš's avatar
Martin Mareš committed
86 87 88 89
{
  struct pipe_proto *p = ((struct pipe_proto *) P)->phantom;

  DBG("PIPE %c< %I/%d\n", (new ? '+' : '-'), net->n.prefix, net->n.pxlen);
90
  pipe_send(p, p->p.table, net, new, old, attrs);
Martin Mareš's avatar
Martin Mareš committed
91 92 93
}

static int
Martin Mareš's avatar
Martin Mareš committed
94
pipe_import_control(struct proto *P, rte **ee, ea_list **ea UNUSED, struct linpool *p UNUSED)
Martin Mareš's avatar
Martin Mareš committed
95
{
96
  struct proto *pp = (*ee)->sender;
Martin Mareš's avatar
Martin Mareš committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

  if (pp == P || pp == &((struct pipe_proto *) P)->phantom->p)
    return -1;	/* Avoid local loops automatically */
  return 0;
}

static int
pipe_start(struct proto *P)
{
  struct pipe_proto *p = (struct pipe_proto *) P;
  struct pipe_proto *ph;
  struct announce_hook *a;

  /*
   *  Create a phantom protocol which will represent the remote
   *  end of the pipe (we need to do this in order to get different
   *  filters and announce functions and it unfortunately involves
   *  a couple of magic trickery).
   */
  ph = mb_alloc(P->pool, sizeof(struct pipe_proto));
  memcpy(ph, p, sizeof(struct pipe_proto));
  p->phantom = ph;
  ph->phantom = p;
120
  ph->p.accept_ra_types = RA_ANY;
Martin Mareš's avatar
Martin Mareš committed
121 122 123 124 125 126 127 128 129 130 131 132 133
  ph->p.rt_notify = pipe_rt_notify_sec;
  ph->p.proto_state = PS_UP;
  ph->p.core_state = ph->p.core_goal = FS_HAPPY;
  ph->p.in_filter = ph->p.out_filter = FILTER_ACCEPT;	/* We do all filtering on the local end */

  /*
   *  Connect the phantom protocol to the peer routing table, but
   *  keep it in the list of connections of the primary protocol,
   *  so that it gets disconnected at the right time and we also
   *  get all routes from both sides during the feeding phase.
   */
  a = proto_add_announce_hook(P, p->peer);
  a->proto = &ph->p;
134
  rt_lock_table(p->peer);
Martin Mareš's avatar
Martin Mareš committed
135 136 137 138

  return PS_UP;
}

139 140 141 142 143 144 145 146 147
static int
pipe_shutdown(struct proto *P)
{
  struct pipe_proto *p = (struct pipe_proto *) P;

  rt_unlock_table(p->peer);
  return PS_DOWN;
}

Martin Mareš's avatar
Martin Mareš committed
148 149 150 151 152 153 154 155
static struct proto *
pipe_init(struct proto_config *C)
{
  struct pipe_config *c = (struct pipe_config *) C;
  struct proto *P = proto_new(C, sizeof(struct pipe_proto));
  struct pipe_proto *p = (struct pipe_proto *) P;

  p->peer = c->peer->table;
156
  P->accept_ra_types = RA_ANY;
Martin Mareš's avatar
Martin Mareš committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  P->rt_notify = pipe_rt_notify_pri;
  P->import_control = pipe_import_control;
  return P;
}

static void
pipe_postconfig(struct proto_config *C)
{
  struct pipe_config *c = (struct pipe_config *) C;

  if (!c->peer)
    cf_error("Name of peer routing table not specified");
  if (c->peer == C->table)
    cf_error("Primary table and peer table must be different");
}

173 174 175 176 177 178 179 180
static void
pipe_get_status(struct proto *P, byte *buf)
{
  struct pipe_proto *p = (struct pipe_proto *) P;

  bsprintf(buf, "-> %s", p->peer->name);
}

181 182 183 184 185 186 187 188 189
static int
pipe_reconfigure(struct proto *p, struct proto_config *new)
{
  struct pipe_config *o = (struct pipe_config *) p->cf;
  struct pipe_config *n = (struct pipe_config *) new;

  return o->peer == n->peer;
}

Martin Mareš's avatar
Martin Mareš committed
190 191
struct protocol proto_pipe = {
  name:		"Pipe",
192
  template:	"pipe%d",
Martin Mareš's avatar
Martin Mareš committed
193 194 195
  postconfig:	pipe_postconfig,
  init:		pipe_init,
  start:	pipe_start,
196
  shutdown:	pipe_shutdown,
197
  reconfigure:	pipe_reconfigure,
198
  get_status:	pipe_get_status,
Martin Mareš's avatar
Martin Mareš committed
199
};