Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7.22 KB)

1
/*
2
 *        BIRD -- Static Route Generator
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Static
11
 *
12
 * The Static protocol is implemented in a very straightforward way. It keeps
13
 * a two lists of static routes: one containing interface routes and one
14
 * holding the remaining ones. Interface routes are inserted and removed according
15
 * to interface events received from the core via the if_notify() hook, routes
16
 * pointing to a neighboring router use a sticky node in the neighbor cache
17
 * to be notified about gaining or losing the neighbor and finally special
18
 * 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
 * difference between the lists of static routes mentioned in the old config
23
 * and the lists in the new one and it just inserts the newly added routes
24
 * and removes the obsolete ones.
25
 */
26

    
27
#undef LOCAL_DEBUG
28

    
29
#include "nest/bird.h"
30
#include "nest/iface.h"
31
#include "nest/protocol.h"
32
#include "nest/route.h"
33
#include "nest/cli.h"
34
#include "conf/conf.h"
35
#include "lib/string.h"
36

    
37
#include "static.h"
38

    
39
static void
40
static_install(struct proto *p, struct static_route *r, struct iface *ifa)
41
{
42
  net *n;
43
  rta a, *aa;
44
  rte *e;
45

    
46
  if (r->installed)
47
    return;
48

    
49
  DBG("Installing static route %I/%d, rtd=%d\n", r->net, r->masklen, r->dest);
50
  bzero(&a, sizeof(a));
51
  a.proto = p;
52
  a.source = (r->dest == RTD_DEVICE) ? RTS_STATIC_DEVICE : RTS_STATIC;
53
  a.scope = SCOPE_UNIVERSE;
54
  a.cast = RTC_UNICAST;
55
  a.dest = r->dest;
56
  a.gw = r->via;
57
  a.iface = ifa;
58
  aa = rta_lookup(&a);
59

    
60
  n = net_get(p->table, r->net, r->masklen);
61
  e = rte_get_temp(aa);
62
  e->net = n;
63
  e->pflags = 0;
64
  rte_update(p->table, n, p, e);
65
  r->installed = 1;
66
}
67

    
68
static void
69
static_remove(struct proto *p, struct static_route *r)
70
{
71
  net *n;
72

    
73
  if (!r->installed)
74
    return;
75

    
76
  DBG("Removing static route %I/%d\n", r->net, r->masklen);
77
  n = net_find(p->table, r->net, r->masklen);
78
  if (n)
79
    rte_update(p->table, n, p, NULL);
80
  r->installed = 0;
81
}
82

    
83
static void
84
static_add(struct proto *p, struct static_route *r)
85
{
86
  DBG("static_add(%I/%d,%d)\n", r->net, r->masklen, r->dest);
87
  switch (r->dest)
88
    {
89
    case RTD_ROUTER:
90
      {
91
        struct neighbor *n = neigh_find(p, &r->via, NEF_STICKY);
92
        if (n)
93
          {
94
            r->chain = n->data;
95
            n->data = r;
96
            r->neigh = n;
97
            if (n->iface)
98
              static_install(p, r, n->iface);
99
          }
100
        else
101
          log(L_ERR "Static route destination %I is invalid. Ignoring.", r->via);
102
        break;
103
      }
104
    case RTD_DEVICE:
105
      break;
106
    default:
107
      static_install(p, r, NULL);
108
    }
109
}
110

    
111
static int
112
static_start(struct proto *p)
113
{
114
  struct static_config *c = (void *) p->cf;
115
  struct static_route *r;
116

    
117
  DBG("Static: take off!\n");
118
  WALK_LIST(r, c->other_routes)
119
    static_add(p, r);
120
  return PS_UP;
121
}
122

    
123
static int
124
static_shutdown(struct proto *p)
125
{
126
  struct static_config *c = (void *) p->cf;
127
  struct static_route *r;
128

    
129
  DBG("Static: prepare for landing!\n");
130
  WALK_LIST(r, c->iface_routes)
131
    static_remove(p, r);
132
  WALK_LIST(r, c->other_routes)
133
    static_remove(p, r);
134
  return PS_DOWN;
135
}
136

    
137
static void
138
static_neigh_notify(struct neighbor *n)
139
{
140
  struct proto *p = n->proto;
141
  struct static_route *r;
142

    
143
  DBG("Static: neighbor notify for %I: iface %p\n", n->addr, n->iface);
144
  for(r=n->data; r; r=r->chain)
145
    if (n->iface)
146
      static_install(p, r, n->iface);
147
    else
148
      static_remove(p, r);
149
}
150

    
151
static void
152
static_dump_rt(struct static_route *r)
153
{
154
  debug("%16I/%2d: ", r->net, r->masklen);
155
  switch (r->dest)
156
    {
157
    case RTD_ROUTER:
158
      debug("via %I\n", r->via);
159
      break;
160
    case RTD_DEVICE:
161
      debug("dev %s\n", r->if_name);
162
      break;
163
    default:
164
      debug("rtd %d\n", r->dest);
165
      break;
166
    }
167
}
168

    
169
static void
170
static_dump(struct proto *p)
171
{
172
  struct static_config *c = (void *) p->cf;
173
  struct static_route *r;
174

    
175
  debug("Independent static routes:\n");
176
  WALK_LIST(r, c->other_routes)
177
    static_dump_rt(r);
178
  debug("Device static routes:\n");
179
  WALK_LIST(r, c->iface_routes)
180
    static_dump_rt(r);
181
}
182

    
183
static void
184
static_if_notify(struct proto *p, unsigned flags, struct iface *i)
185
{
186
  struct static_route *r;
187
  struct static_config *c = (void *) p->cf;
188

    
189
  if (flags & IF_CHANGE_UP)
190
    {
191
      WALK_LIST(r, c->iface_routes)
192
        if (!strcmp(r->if_name, i->name))
193
          static_install(p, r, i);
194
    }
195
  else if (flags & IF_CHANGE_DOWN)
196
    {
197
      WALK_LIST(r, c->iface_routes)
198
        if (!strcmp(r->if_name, i->name))
199
          static_remove(p, r);
200
    }
201
}
202

    
203
void
204
static_init_config(struct static_config *c)
205
{
206
  c->c.preference = DEF_PREF_STATIC;
207
  init_list(&c->iface_routes);
208
  init_list(&c->other_routes);
209
}
210

    
211
static struct proto *
212
static_init(struct proto_config *c)
213
{
214
  struct proto *p = proto_new(c, sizeof(struct proto));
215

    
216
  p->neigh_notify = static_neigh_notify;
217
  p->if_notify = static_if_notify;
218
  return p;
219
}
220

    
221
static int
222
static_same_route(struct static_route *x, struct static_route *y)
223
{
224
  return ipa_equal(x->net, y->net)
225
    && x->masklen == y->masklen
226
    && x->dest == y->dest
227
    && (x->dest != RTD_ROUTER || ipa_equal(x->via, y->via))
228
    && (x->dest != RTD_DEVICE || !strcmp(x->if_name, y->if_name))
229
    ;
230
}
231

    
232
static void
233
static_match(struct proto *p, struct static_route *r, struct static_config *n)
234
{
235
  struct static_route *t;
236

    
237
  if (r->neigh)
238
    r->neigh->data = NULL;
239
  WALK_LIST(t, n->iface_routes)
240
    if (static_same_route(r, t))
241
      {
242
        t->installed = 1;
243
        return;
244
      }
245
  WALK_LIST(t, n->other_routes)
246
    if (static_same_route(r, t))
247
      {
248
        t->installed = 1;
249
        return;
250
      }
251
  static_remove(p, r);
252
}
253

    
254
static int
255
static_reconfigure(struct proto *p, struct proto_config *new)
256
{
257
  struct static_config *o = (void *) p->cf;
258
  struct static_config *n = (void *) new;
259
  struct static_route *r;
260

    
261
  /* Delete all obsolete routes and reset neighbor entries */
262
  WALK_LIST(r, o->iface_routes)
263
    static_match(p, r, n);
264
  WALK_LIST(r, o->other_routes)
265
    static_match(p, r, n);
266

    
267
  /* Now add all new routes, those not changed will be ignored by static_install() */
268
  WALK_LIST(r, n->iface_routes)
269
    {
270
      struct iface *ifa;
271
      if (ifa = if_find_by_name(r->if_name))
272
        static_install(p, r, ifa);
273
    }
274
  WALK_LIST(r, n->other_routes)
275
    static_add(p, r);
276

    
277
  return 1;
278
}
279

    
280
struct protocol proto_static = {
281
  name:                "Static",
282
  template:        "static%d",
283
  init:                static_init,
284
  dump:                static_dump,
285
  start:        static_start,
286
  shutdown:        static_shutdown,
287
  reconfigure:        static_reconfigure,
288
};
289

    
290
static void
291
static_show_rt(struct static_route *r)
292
{
293
  byte via[STD_ADDRESS_P_LENGTH + 16];
294

    
295
  switch (r->dest)
296
    {
297
    case RTD_ROUTER:        bsprintf(via, "via %I", r->via); break;
298
    case RTD_DEVICE:        bsprintf(via, "to %s", r->if_name); break;
299
    case RTD_BLACKHOLE:        bsprintf(via, "blackhole"); break;
300
    case RTD_UNREACHABLE:        bsprintf(via, "unreachable"); break;
301
    case RTD_PROHIBIT:        bsprintf(via, "prohibited"); break;
302
    default:                bsprintf(via, "???");
303
    }
304
  cli_msg(-1009, "%I/%d %s%s", r->net, r->masklen, via, r->installed ? "" : " (dormant)");
305
}
306

    
307
void
308
static_show(struct proto *P)
309
{
310
  struct static_config *c = (void *) P->cf;
311
  struct static_route *r;
312

    
313
  WALK_LIST(r, c->other_routes)
314
    static_show_rt(r);
315
  WALK_LIST(r, c->iface_routes)
316
    static_show_rt(r);
317
  cli_msg(0, "");
318
}