Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-dev.c @ 153f02da

History | View | Annotate | Download (4.06 KB)

1 c5ffa447 Martin Mares
/*
2
 *        BIRD -- Direct Device Routes
3
 *
4 394aec8f Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 c5ffa447 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 47f8e0c2 Martin Mares
/**
10
 * DOC: Direct
11
 *
12 2e9b2421 Martin Mares
 * The Direct protocol works by converting all ifa_notify() events it receives
13 47f8e0c2 Martin Mares
 * to rte_update() calls for the corresponding network.
14
 */
15
16 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
17 c5ffa447 Martin Mares
18
#include "nest/bird.h"
19
#include "nest/iface.h"
20
#include "nest/protocol.h"
21
#include "nest/route.h"
22 50d8424a Martin Mares
#include "nest/rt-dev.h"
23
#include "conf/conf.h"
24 c5ffa447 Martin Mares
#include "lib/resource.h"
25 221135d6 Martin Mares
#include "lib/string.h"
26 c5ffa447 Martin Mares
27 f4a60a9b Ondrej Zajicek (work)
28 c5ffa447 Martin Mares
static void
29 f4a60a9b Ondrej Zajicek (work)
dev_ifa_notify(struct proto *P, uint flags, struct ifa *ad)
30 c5ffa447 Martin Mares
{
31 f4a60a9b Ondrej Zajicek (work)
  struct rt_dev_proto *p = (void *) P;
32
  struct rt_dev_config *cf = (void *) P->cf;
33
  struct channel *c;
34 50d8424a Martin Mares
35 f4a60a9b Ondrej Zajicek (work)
  if (!EMPTY_LIST(cf->iface_list) &&
36 153f02da Ondrej Zajicek (work)
      !iface_patt_find(&cf->iface_list, ad->iface, ad))
37 062d18fd Pavel TvrdĂ­k
    /* Empty list is automatically treated as "*" */
38 50d8424a Martin Mares
    return;
39 ff2857b0 Ondrej Zajicek
40 92f8878c Ondrej Zajicek
  if (ad->flags & IA_SECONDARY)
41
    return;
42 ff2857b0 Ondrej Zajicek
43
  if (ad->scope <= SCOPE_LINK)
44
    return;
45
46 f4a60a9b Ondrej Zajicek (work)
  if (ad->prefix.type == NET_IP4)
47
    c = p->ip4_channel;
48
  else if (ad->prefix.type == NET_IP6)
49
    c = p->ip6_channel;
50
  else
51
    return;
52
53
  if (!c)
54
    return;
55
56
  if (flags & IF_CHANGE_DOWN)
57 b1e4f814 Martin Mares
    {
58 9a158361 Martin Mares
      DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
59 ab4da342 Ondrej Zajicek
60
      /* Use iface ID as local source ID */
61 f4a60a9b Ondrej Zajicek (work)
      struct rte_src *src = rt_get_source(P, ad->iface->index);
62 2003a184 Jan Moskyto Matejka
      rte_update2(c, &ad->prefix, NULL, src);
63 b1e4f814 Martin Mares
    }
64 f4a60a9b Ondrej Zajicek (work)
  else if (flags & IF_CHANGE_UP)
65 b1e4f814 Martin Mares
    {
66 094d2bdb Ondrej Zajicek
      rta *a;
67 b1e4f814 Martin Mares
      rte *e;
68
69 6b9fa320 Martin Mares
      DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
70 094d2bdb Ondrej Zajicek
71 af678af0 Ondrej Zajicek (work)
      if (cf->check_link && !(ad->iface->flags & IF_LINK_UP))
72 e90dd656 Ondrej Zajicek (work)
        return;
73
74 ab4da342 Ondrej Zajicek
      /* Use iface ID as local source ID */
75 f4a60a9b Ondrej Zajicek (work)
      struct rte_src *src = rt_get_source(P, ad->iface->index);
76 ab4da342 Ondrej Zajicek
77 094d2bdb Ondrej Zajicek
      rta a0 = {
78 ab4da342 Ondrej Zajicek
        .src = src,
79 094d2bdb Ondrej Zajicek
        .source = RTS_DEVICE,
80
        .scope = SCOPE_UNIVERSE,
81 4e276a89 Jan Moskyto Matejka
        .dest = RTD_UNICAST,
82 62e64905 Ondrej Zajicek (work)
        .nh.iface = ad->iface,
83 094d2bdb Ondrej Zajicek
      };
84
85
      a = rta_lookup(&a0);
86 b1e4f814 Martin Mares
      e = rte_get_temp(a);
87
      e->pflags = 0;
88 2003a184 Jan Moskyto Matejka
      rte_update2(c, &ad->prefix, e, src);
89 b1e4f814 Martin Mares
    }
90 c5ffa447 Martin Mares
}
91
92 e90dd656 Ondrej Zajicek (work)
static void
93
dev_if_notify(struct proto *p, uint c, struct iface *iface)
94
{
95
  struct rt_dev_config *cf = (void *) p->cf;
96
97
  if (c & (IF_CHANGE_UP | IF_CHANGE_DOWN))
98
    return;
99
100
  if ((c & IF_CHANGE_LINK) && cf->check_link)
101
  {
102
    uint ac = (iface->flags & IF_LINK_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN;
103
104
    struct ifa *a;
105
    WALK_LIST(a, iface->addrs)
106
      dev_ifa_notify(p, ac, a);
107
  }
108
}
109
110
111 31b3e1bb Martin Mares
static struct proto *
112 f4a60a9b Ondrej Zajicek (work)
dev_init(struct proto_config *CF)
113 c5ffa447 Martin Mares
{
114 f4a60a9b Ondrej Zajicek (work)
  struct proto *P = proto_new(CF);
115
  struct rt_dev_proto *p = (void *) P;
116
  // struct rt_dev_config *cf = (void *) CF;
117 c5ffa447 Martin Mares
118 f4a60a9b Ondrej Zajicek (work)
  proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4));
119
  proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
120
121 af678af0 Ondrej Zajicek (work)
  P->if_notify = dev_if_notify;
122 f4a60a9b Ondrej Zajicek (work)
  P->ifa_notify = dev_ifa_notify;
123
124
  return P;
125 c5ffa447 Martin Mares
}
126
127 88dc89f9 Martin Mares
static int
128 f4a60a9b Ondrej Zajicek (work)
dev_reconfigure(struct proto *P, struct proto_config *CF)
129 88dc89f9 Martin Mares
{
130 f4a60a9b Ondrej Zajicek (work)
  struct rt_dev_proto *p = (void *) P;
131
  struct rt_dev_config *o = (void *) P->cf;
132
  struct rt_dev_config *n = (void *) CF;
133
134 af678af0 Ondrej Zajicek (work)
  if (!iface_patts_equal(&o->iface_list, &n->iface_list, NULL) ||
135
      (o->check_link != n->check_link))
136 f4a60a9b Ondrej Zajicek (work)
    return 0;
137
138
  return
139
    proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4)) &&
140
    proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
141 2bbc3083 Ondrej Zajicek
142 f4a60a9b Ondrej Zajicek (work)
  return 1;
143 88dc89f9 Martin Mares
}
144
145 a7f23f58 Ondrej Zajicek
static void
146
dev_copy_config(struct proto_config *dest, struct proto_config *src)
147
{
148 f4a60a9b Ondrej Zajicek (work)
  struct rt_dev_config *d = (void *) dest;
149
  struct rt_dev_config *s = (void *) src;
150 a7f23f58 Ondrej Zajicek
151
  /*
152
   * We copy iface_list as ifaces can be shared by more direct protocols.
153
   * Copy suffices to be is shallow, because new nodes can be added, but
154
   * old nodes cannot be modified (although they contain internal lists).
155
   */
156
  cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct iface_patt));
157 e90dd656 Ondrej Zajicek (work)
158
  d->check_link = s->check_link;
159 a7f23f58 Ondrej Zajicek
}
160
161 c5ffa447 Martin Mares
struct protocol proto_device = {
162 4a591d4b Pavel Tvrdik
  .name =                 "Direct",
163
  .template =                 "direct%d",
164 f4a60a9b Ondrej Zajicek (work)
  .preference =                DEF_PREF_DIRECT,
165
  .channel_mask =        NB_IP,
166
  .proto_size =                sizeof(struct rt_dev_proto),
167 2bbc3083 Ondrej Zajicek
  .config_size =        sizeof(struct rt_dev_config),
168 4a591d4b Pavel Tvrdik
  .init =                 dev_init,
169
  .reconfigure =         dev_reconfigure,
170
  .copy_config =         dev_copy_config
171 c5ffa447 Martin Mares
};