Statistics
| Branch: | Revision:

iof-bird-daemon / proto / static / static.c @ 725270cb

History | View | Annotate | Download (7.15 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 straightforward way. It keeps
13
 * 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. 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 and it just inserts the
23
 * newly added routes and removes the obsolete ones.
24
 */
25

    
26
#undef LOCAL_DEBUG
27

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

    
36
#include "static.h"
37

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

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

    
48
  DBG("Installing static route %I/%d, rtd=%d\n", r->net, r->masklen, r->dest);
49
  bzero(&a, sizeof(a));
50
  a.proto = p;
51
  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
  n = net_get(p->table, r->net, r->masklen);
60
  e = rte_get_temp(aa);
61
  e->net = n;
62
  e->pflags = 0;
63
  rte_update(p->table, n, p, e);
64
  r->installed = 1;
65
}
66

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

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

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

    
82
static void
83
static_add(struct proto *p, struct static_route *r)
84
{
85
  DBG("static_add(%I/%d,%d)\n", r->net, r->masklen, r->dest);
86
  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
          log(L_ERR "Static route destination %I is invalid. Ignoring.", r->via);
101
        break;
102
      }
103
    case RTD_DEVICE:
104
      break;
105
    default:
106
      static_install(p, r, NULL);
107
    }
108
}
109

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

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

    
122
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
static void
137
static_neigh_notify(struct neighbor *n)
138
{
139
  struct proto *p = n->proto;
140
  struct static_route *r;
141

    
142
  DBG("Static: neighbor notify for %I: iface %p\n", n->addr, n->iface);
143
  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
}
149

    
150
static void
151
static_dump_rt(struct static_route *r)
152
{
153
  debug("%16I/%2d: ", r->net, r->masklen);
154
  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
}
167

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

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

    
182
static void
183
static_if_notify(struct proto *p, unsigned flags, struct iface *i)
184
{
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
        if (!strcmp(r->if_name, i->name))
192
          static_install(p, r, i);
193
    }
194
  else if (flags & IF_CHANGE_DOWN)
195
    {
196
      WALK_LIST(r, c->iface_routes)
197
        if (!strcmp(r->if_name, i->name))
198
          static_remove(p, r);
199
    }
200
}
201

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

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

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

    
220
static int
221
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
static_reconfigure(struct proto *p, struct proto_config *new)
255
{
256
  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
}
278

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

    
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
}