Statistics
| Branch: | Revision:

iof-bird-daemon / proto / static / static.c @ e43ae633

History | View | Annotate | Download (7.15 KB)

1 a1bf6440 Martin Mares
/*
2
 *        BIRD -- Static Route Generator
3
 *
4 d272fe22 Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 a1bf6440 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 3b31c538 Martin Mares
/**
10
 * DOC: Static
11
 *
12 725270cb Martin Mares
 * The Static protocol is implemented in a straightforward way. It keeps
13
 * two lists of static routes: one containing interface routes and one
14 3b31c538 Martin Mares
 * holding the remaining ones. Interface routes are inserted and removed according
15 725270cb Martin Mares
 * to interface events received from the core via the if_notify() hook. Routes
16 3b31c538 Martin Mares
 * pointing to a neighboring router use a sticky node in the neighbor cache
17 725270cb Martin Mares
 * to be notified about gaining or losing the neighbor. Special
18 3b31c538 Martin Mares
 * routes like black holes or rejects are inserted all the time.
19
 *
20
 * The only other thing worth mentioning is that when asked for reconfiguration,
21
 * Static not only compares the two configurations, but it also calculates
22 725270cb Martin Mares
 * difference between the lists of static routes and it just inserts the
23
 * newly added routes and removes the obsolete ones.
24 3b31c538 Martin Mares
 */
25
26 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
27 a1bf6440 Martin Mares
28
#include "nest/bird.h"
29
#include "nest/iface.h"
30
#include "nest/protocol.h"
31
#include "nest/route.h"
32 feed8226 Martin Mares
#include "nest/cli.h"
33 a1bf6440 Martin Mares
#include "conf/conf.h"
34 feed8226 Martin Mares
#include "lib/string.h"
35 a1bf6440 Martin Mares
36
#include "static.h"
37
38
static void
39 e9e3dc26 Martin Mares
static_install(struct proto *p, struct static_route *r, struct iface *ifa)
40 f6bd2066 Martin Mares
{
41
  net *n;
42
  rta a, *aa;
43
  rte *e;
44
45 295ae16d Martin Mares
  if (r->installed)
46
    return;
47
48 f6bd2066 Martin Mares
  DBG("Installing static route %I/%d, rtd=%d\n", r->net, r->masklen, r->dest);
49
  bzero(&a, sizeof(a));
50 e9e3dc26 Martin Mares
  a.proto = p;
51 f6bd2066 Martin Mares
  a.source = (r->dest == RTD_DEVICE) ? RTS_STATIC_DEVICE : RTS_STATIC;
52
  a.scope = SCOPE_UNIVERSE;
53
  a.cast = RTC_UNICAST;
54
  a.dest = r->dest;
55
  a.gw = r->via;
56
  a.iface = ifa;
57
  aa = rta_lookup(&a);
58
59 08e2d625 Martin Mares
  n = net_get(p->table, r->net, r->masklen);
60 f6bd2066 Martin Mares
  e = rte_get_temp(aa);
61
  e->net = n;
62
  e->pflags = 0;
63 4f1a6d27 Martin Mares
  rte_update(p->table, n, p, e);
64 feed8226 Martin Mares
  r->installed = 1;
65 f6bd2066 Martin Mares
}
66
67
static void
68 e9e3dc26 Martin Mares
static_remove(struct proto *p, struct static_route *r)
69 f6bd2066 Martin Mares
{
70
  net *n;
71
72 295ae16d Martin Mares
  if (!r->installed)
73
    return;
74
75 f6bd2066 Martin Mares
  DBG("Removing static route %I/%d\n", r->net, r->masklen);
76 08e2d625 Martin Mares
  n = net_find(p->table, r->net, r->masklen);
77 f6bd2066 Martin Mares
  if (n)
78 4f1a6d27 Martin Mares
    rte_update(p->table, n, p, NULL);
79 feed8226 Martin Mares
  r->installed = 0;
80 f6bd2066 Martin Mares
}
81
82 295ae16d Martin Mares
static void
83
static_add(struct proto *p, struct static_route *r)
84
{
85 e4bfafa1 Martin Mares
  DBG("static_add(%I/%d,%d)\n", r->net, r->masklen, r->dest);
86 295ae16d Martin Mares
  switch (r->dest)
87
    {
88
    case RTD_ROUTER:
89
      {
90
        struct neighbor *n = neigh_find(p, &r->via, NEF_STICKY);
91
        if (n)
92
          {
93
            r->chain = n->data;
94
            n->data = r;
95
            r->neigh = n;
96
            if (n->iface)
97
              static_install(p, r, n->iface);
98
          }
99
        else
100 df49d4e1 Martin Mares
          log(L_ERR "Static route destination %I is invalid. Ignoring.", r->via);
101 295ae16d Martin Mares
        break;
102
      }
103
    case RTD_DEVICE:
104
      break;
105
    default:
106
      static_install(p, r, NULL);
107
    }
108
}
109
110 e9e3dc26 Martin Mares
static int
111
static_start(struct proto *p)
112 a1bf6440 Martin Mares
{
113 e9e3dc26 Martin Mares
  struct static_config *c = (void *) p->cf;
114 f6bd2066 Martin Mares
  struct static_route *r;
115
116 a1bf6440 Martin Mares
  DBG("Static: take off!\n");
117 e9e3dc26 Martin Mares
  WALK_LIST(r, c->other_routes)
118 295ae16d Martin Mares
    static_add(p, r);
119 e9e3dc26 Martin Mares
  return PS_UP;
120 a1bf6440 Martin Mares
}
121
122 e4bfafa1 Martin Mares
static int
123
static_shutdown(struct proto *p)
124
{
125
  struct static_config *c = (void *) p->cf;
126
  struct static_route *r;
127
128
  DBG("Static: prepare for landing!\n");
129
  WALK_LIST(r, c->iface_routes)
130
    static_remove(p, r);
131
  WALK_LIST(r, c->other_routes)
132
    static_remove(p, r);
133
  return PS_DOWN;
134
}
135
136 a1bf6440 Martin Mares
static void
137
static_neigh_notify(struct neighbor *n)
138
{
139 e9e3dc26 Martin Mares
  struct proto *p = n->proto;
140 980297d2 Martin Mares
  struct static_route *r;
141
142 f6bd2066 Martin Mares
  DBG("Static: neighbor notify for %I: iface %p\n", n->addr, n->iface);
143 980297d2 Martin Mares
  for(r=n->data; r; r=r->chain)
144
    if (n->iface)
145
      static_install(p, r, n->iface);
146
    else
147
      static_remove(p, r);
148 f6bd2066 Martin Mares
}
149
150
static void
151
static_dump_rt(struct static_route *r)
152
{
153 e43ae633 Martin Mares
  debug("%-1I/%2d: ", r->net, r->masklen);
154 f6bd2066 Martin Mares
  switch (r->dest)
155
    {
156
    case RTD_ROUTER:
157
      debug("via %I\n", r->via);
158
      break;
159
    case RTD_DEVICE:
160
      debug("dev %s\n", r->if_name);
161
      break;
162
    default:
163
      debug("rtd %d\n", r->dest);
164
      break;
165
    }
166 a1bf6440 Martin Mares
}
167
168
static void
169 e9e3dc26 Martin Mares
static_dump(struct proto *p)
170 a1bf6440 Martin Mares
{
171 e9e3dc26 Martin Mares
  struct static_config *c = (void *) p->cf;
172 f6bd2066 Martin Mares
  struct static_route *r;
173
174
  debug("Independent static routes:\n");
175 e9e3dc26 Martin Mares
  WALK_LIST(r, c->other_routes)
176 f6bd2066 Martin Mares
    static_dump_rt(r);
177
  debug("Device static routes:\n");
178 e9e3dc26 Martin Mares
  WALK_LIST(r, c->iface_routes)
179 f6bd2066 Martin Mares
    static_dump_rt(r);
180 a1bf6440 Martin Mares
}
181
182 d88e99a9 Martin Mares
static void
183 9a158361 Martin Mares
static_if_notify(struct proto *p, unsigned flags, struct iface *i)
184 d88e99a9 Martin Mares
{
185
  struct static_route *r;
186
  struct static_config *c = (void *) p->cf;
187
188
  if (flags & IF_CHANGE_UP)
189
    {
190
      WALK_LIST(r, c->iface_routes)
191 9a158361 Martin Mares
        if (!strcmp(r->if_name, i->name))
192
          static_install(p, r, i);
193 d88e99a9 Martin Mares
    }
194
  else if (flags & IF_CHANGE_DOWN)
195
    {
196
      WALK_LIST(r, c->iface_routes)
197 9a158361 Martin Mares
        if (!strcmp(r->if_name, i->name))
198 d88e99a9 Martin Mares
          static_remove(p, r);
199
    }
200
}
201
202 a1bf6440 Martin Mares
void
203 e9e3dc26 Martin Mares
static_init_config(struct static_config *c)
204 a1bf6440 Martin Mares
{
205 e9e3dc26 Martin Mares
  c->c.preference = DEF_PREF_STATIC;
206
  init_list(&c->iface_routes);
207
  init_list(&c->other_routes);
208 a1bf6440 Martin Mares
}
209
210 e9e3dc26 Martin Mares
static struct proto *
211
static_init(struct proto_config *c)
212 a1bf6440 Martin Mares
{
213 e9e3dc26 Martin Mares
  struct proto *p = proto_new(c, sizeof(struct proto));
214 a1bf6440 Martin Mares
215 e9e3dc26 Martin Mares
  p->neigh_notify = static_neigh_notify;
216 d88e99a9 Martin Mares
  p->if_notify = static_if_notify;
217 e9e3dc26 Martin Mares
  return p;
218 a1bf6440 Martin Mares
}
219
220 d272fe22 Martin Mares
static int
221 295ae16d Martin Mares
static_same_route(struct static_route *x, struct static_route *y)
222
{
223
  return ipa_equal(x->net, y->net)
224
    && x->masklen == y->masklen
225
    && x->dest == y->dest
226
    && (x->dest != RTD_ROUTER || ipa_equal(x->via, y->via))
227
    && (x->dest != RTD_DEVICE || !strcmp(x->if_name, y->if_name))
228
    ;
229
}
230
231
static void
232
static_match(struct proto *p, struct static_route *r, struct static_config *n)
233
{
234
  struct static_route *t;
235
236
  if (r->neigh)
237
    r->neigh->data = NULL;
238
  WALK_LIST(t, n->iface_routes)
239
    if (static_same_route(r, t))
240
      {
241
        t->installed = 1;
242
        return;
243
      }
244
  WALK_LIST(t, n->other_routes)
245
    if (static_same_route(r, t))
246
      {
247
        t->installed = 1;
248
        return;
249
      }
250
  static_remove(p, r);
251
}
252
253
static int
254 d272fe22 Martin Mares
static_reconfigure(struct proto *p, struct proto_config *new)
255
{
256 295ae16d Martin Mares
  struct static_config *o = (void *) p->cf;
257
  struct static_config *n = (void *) new;
258
  struct static_route *r;
259
260
  /* Delete all obsolete routes and reset neighbor entries */
261
  WALK_LIST(r, o->iface_routes)
262
    static_match(p, r, n);
263
  WALK_LIST(r, o->other_routes)
264
    static_match(p, r, n);
265
266
  /* Now add all new routes, those not changed will be ignored by static_install() */
267
  WALK_LIST(r, n->iface_routes)
268
    {
269
      struct iface *ifa;
270
      if (ifa = if_find_by_name(r->if_name))
271
        static_install(p, r, ifa);
272
    }
273
  WALK_LIST(r, n->other_routes)
274
    static_add(p, r);
275
276
  return 1;
277 d272fe22 Martin Mares
}
278
279 a1bf6440 Martin Mares
struct protocol proto_static = {
280 e9e3dc26 Martin Mares
  name:                "Static",
281 d272fe22 Martin Mares
  template:        "static%d",
282 e9e3dc26 Martin Mares
  init:                static_init,
283
  dump:                static_dump,
284
  start:        static_start,
285 e4bfafa1 Martin Mares
  shutdown:        static_shutdown,
286 d272fe22 Martin Mares
  reconfigure:        static_reconfigure,
287 a1bf6440 Martin Mares
};
288 feed8226 Martin Mares
289
static void
290
static_show_rt(struct static_route *r)
291
{
292
  byte via[STD_ADDRESS_P_LENGTH + 16];
293
294
  switch (r->dest)
295
    {
296
    case RTD_ROUTER:        bsprintf(via, "via %I", r->via); break;
297
    case RTD_DEVICE:        bsprintf(via, "to %s", r->if_name); break;
298
    case RTD_BLACKHOLE:        bsprintf(via, "blackhole"); break;
299
    case RTD_UNREACHABLE:        bsprintf(via, "unreachable"); break;
300
    case RTD_PROHIBIT:        bsprintf(via, "prohibited"); break;
301
    default:                bsprintf(via, "???");
302
    }
303
  cli_msg(-1009, "%I/%d %s%s", r->net, r->masklen, via, r->installed ? "" : " (dormant)");
304
}
305
306
void
307
static_show(struct proto *P)
308
{
309
  struct static_config *c = (void *) P->cf;
310
  struct static_route *r;
311
312
  WALK_LIST(r, c->other_routes)
313
    static_show_rt(r);
314
  WALK_LIST(r, c->iface_routes)
315
    static_show_rt(r);
316
  cli_msg(0, "");
317
}