rt-dev.c 4.72 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

28
static void
29
dev_ifa_notify(struct proto *P, uint flags, struct ifa *ad)
30
{
31 32 33
  struct rt_dev_proto *p = (void *) P;
  struct rt_dev_config *cf = (void *) P->cf;
  struct channel *c;
34
  net_addr *net = &ad->prefix;
35

36
  if (!EMPTY_LIST(cf->iface_list) &&
37
      !iface_patt_find(&cf->iface_list, ad->iface, ad))
Pavel Tvrdík's avatar
Pavel Tvrdík committed
38
    /* Empty list is automatically treated as "*" */
39
    return;
40

41 42
  if (ad->flags & IA_SECONDARY)
    return;
43 44 45 46

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

47 48 49 50 51 52 53 54 55 56
  if (ad->prefix.type == NET_IP4)
    c = p->ip4_channel;
  else if (ad->prefix.type == NET_IP6)
    c = p->ip6_channel;
  else
    return;

  if (!c)
    return;

57 58 59 60 61 62 63
  /* For IPv6 SADR, replace regular prefix with SADR prefix */
  if (c->net_type == NET_IP6_SADR)
  {
    net = alloca(sizeof(net_addr_ip6_sadr));
    net_fill_ip6_sadr(net, net6_prefix(&ad->prefix), net6_pxlen(&ad->prefix), IP6_NONE, 0);
  }

64
  if (flags & IF_CHANGE_DOWN)
65
    {
66
      DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
67 68

      /* Use iface ID as local source ID */
69
      struct rte_src *src = rt_get_source(P, ad->iface->index);
70
      rte_update2(c, net, NULL, src);
71
    }
72
  else if (flags & IF_CHANGE_UP)
73
    {
74
      rta *a;
75 76
      rte *e;

77
      DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
78

79
      if (cf->check_link && !(ad->iface->flags & IF_LINK_UP))
80 81
	return;

82
      /* Use iface ID as local source ID */
83
      struct rte_src *src = rt_get_source(P, ad->iface->index);
84

85
      rta a0 = {
86
	.src = src,
87 88
	.source = RTS_DEVICE,
	.scope = SCOPE_UNIVERSE,
89
	.dest = RTD_UNICAST,
Ondřej Zajíček's avatar
Ondřej Zajíček committed
90
	.nh.iface = ad->iface,
91 92 93
      };

      a = rta_lookup(&a0);
94 95
      e = rte_get_temp(a);
      e->pflags = 0;
96
      rte_update2(c, net, e, src);
97
    }
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void
dev_if_notify(struct proto *p, uint c, struct iface *iface)
{
  struct rt_dev_config *cf = (void *) p->cf;

  if (c & (IF_CHANGE_UP | IF_CHANGE_DOWN))
    return;

  if ((c & IF_CHANGE_LINK) && cf->check_link)
  {
    uint ac = (iface->flags & IF_LINK_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN;

    struct ifa *a;
    WALK_LIST(a, iface->addrs)
      dev_ifa_notify(p, ac, a);
  }
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void
dev_postconfig(struct proto_config *CF)
{
  struct rt_dev_config *cf = (void *) CF;
  struct channel_config *ip4, *ip6, *ip6_sadr;

  ip4 = proto_cf_find_channel(CF, NET_IP4);
  ip6 = proto_cf_find_channel(CF, NET_IP6);
  ip6_sadr = proto_cf_find_channel(CF, NET_IP6_SADR);

  if (ip6 && ip6_sadr)
    cf_error("Both ipv6 and ipv6-sadr channels");

  cf->ip4_channel = ip4;
  cf->ip6_channel = ip6 ?: ip6_sadr;
}
134

135
static struct proto *
136
dev_init(struct proto_config *CF)
137
{
138 139
  struct proto *P = proto_new(CF);
  struct rt_dev_proto *p = (void *) P;
140
  struct rt_dev_config *cf = (void *) CF;
141

142 143
  proto_configure_channel(P, &p->ip4_channel, cf->ip4_channel);
  proto_configure_channel(P, &p->ip6_channel, cf->ip6_channel);
144

145
  P->if_notify = dev_if_notify;
146 147 148
  P->ifa_notify = dev_ifa_notify;

  return P;
149 150
}

151
static int
152
dev_reconfigure(struct proto *P, struct proto_config *CF)
153
{
154 155 156 157
  struct rt_dev_proto *p = (void *) P;
  struct rt_dev_config *o = (void *) P->cf;
  struct rt_dev_config *n = (void *) CF;

158 159
  if (!iface_patts_equal(&o->iface_list, &n->iface_list, NULL) ||
      (o->check_link != n->check_link))
160 161 162
    return 0;

  return
163 164
    proto_configure_channel(P, &p->ip4_channel, n->ip4_channel) &&
    proto_configure_channel(P, &p->ip6_channel, n->ip6_channel);
165

166
  return 1;
167 168
}

169 170 171
static void
dev_copy_config(struct proto_config *dest, struct proto_config *src)
{
172 173
  struct rt_dev_config *d = (void *) dest;
  struct rt_dev_config *s = (void *) src;
174 175 176 177 178 179 180

  /*
   * 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));
181 182

  d->check_link = s->check_link;
183 184
}

185
struct protocol proto_device = {
186 187
  .name =		"Direct",
  .template =		"direct%d",
188
  .class =		PROTOCOL_DIRECT,
189
  .preference =		DEF_PREF_DIRECT,
190
  .channel_mask =	NB_IP | NB_IP6_SADR,
191
  .proto_size =		sizeof(struct rt_dev_proto),
192
  .config_size =	sizeof(struct rt_dev_config),
193
  .postconfig =		dev_postconfig,
194 195 196
  .init =		dev_init,
  .reconfigure =	dev_reconfigure,
  .copy_config =	dev_copy_config
197
};