rt-dev.c 2.77 KB
Newer Older
1 2 3
/*
 *	BIRD -- Direct Device Routes
 *
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
/**
 * DOC: Direct
 *
12
 * The Direct protocol works by converting all ifa_notify() events it receives
Martin Mareš's avatar
Martin Mareš committed
13 14 15
 * to rte_update() calls for the corresponding network.
 */

16
#undef LOCAL_DEBUG
17 18 19 20 21

#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "nest/route.h"
22 23
#include "nest/rt-dev.h"
#include "conf/conf.h"
24
#include "lib/resource.h"
25
#include "lib/string.h"
26 27

static void
28
dev_ifa_notify(struct proto *p, unsigned c, struct ifa *ad)
29
{
30
  struct rt_dev_config *P = (void *) p->cf;
31

32
  if (!EMPTY_LIST(P->iface_list) &&
33
      !iface_patt_find(&P->iface_list, ad->iface, ad->iface->addr))
34
    /* Empty list is automagically treated as "*" */
35
    return;
36

37 38
  if (ad->flags & IA_SECONDARY)
    return;
39 40 41 42

  if (ad->scope <= SCOPE_LINK)
    return;

43 44 45 46
  if (c & IF_CHANGE_DOWN)
    {
      net *n;

47 48
      DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
      n = net_find(p->table, ad->prefix, ad->pxlen);
49 50
      if (!n)
	{
51
	  DBG("dev_if_notify: device shutdown: prefix not found\n");
52 53
	  return;
	}
54
      rte_update(p, n, NULL);
55 56 57
    }
  else if (c & IF_CHANGE_UP)
    {
58
      rta *a;
59 60 61
      net *n;
      rte *e;

62
      DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
63 64 65 66 67 68 69 70 71 72 73

      rta a0 = {
	.src = p->main_source,
	.source = RTS_DEVICE,
	.scope = SCOPE_UNIVERSE,
	.cast = RTC_UNICAST,
	.dest = RTD_DEVICE,
	.iface = ad->iface
      };

      a = rta_lookup(&a0);
74
      n = net_get(p->table, ad->prefix, ad->pxlen);
75
      e = rte_get_temp(a);
76
      e->net = n;
77
      e->pflags = 0;
78
      rte_update(p, n, e);
79
    }
80 81
}

82 83
static struct proto *
dev_init(struct proto_config *c)
84
{
85
  struct proto *p = proto_new(c, sizeof(struct proto));
86

87
  p->ifa_notify = dev_ifa_notify;
88
  return p;
89 90
}

91 92 93 94 95 96 97 98 99
static int
dev_reconfigure(struct proto *p, struct proto_config *new)
{
  struct rt_dev_config *o = (struct rt_dev_config *) p->cf;
  struct rt_dev_config *n = (struct rt_dev_config *) new;
  
  return iface_patts_equal(&o->iface_list, &n->iface_list, NULL);
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void
dev_copy_config(struct proto_config *dest, struct proto_config *src)
{
  struct rt_dev_config *d = (struct rt_dev_config *) dest;
  struct rt_dev_config *s = (struct rt_dev_config *) src;

  /*
   * We copy iface_list as ifaces can be shared by more direct protocols.
   * Copy suffices to be is shallow, because new nodes can be added, but
   * old nodes cannot be modified (although they contain internal lists).
   */
  cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct iface_patt));
}

114
struct protocol proto_device = {
115
  name:		"Direct",
116
  template:	"direct%d",
117
  preference:	DEF_PREF_DIRECT,
118
  init:		dev_init,
119 120
  reconfigure:	dev_reconfigure,
  copy_config:	dev_copy_config
121
};