Statistics
| Branch: | Revision:

iof-bird-daemon / proto / static / static.c @ 9a158361

History | View | Annotate | Download (3.56 KB)

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

    
9
#define LOCAL_DEBUG
10

    
11
#include <string.h>
12

    
13
#include "nest/bird.h"
14
#include "nest/iface.h"
15
#include "nest/protocol.h"
16
#include "nest/route.h"
17
#include "conf/conf.h"
18

    
19
#include "static.h"
20

    
21
static void
22
static_install(struct proto *p, struct static_route *r, struct iface *ifa)
23
{
24
  net *n;
25
  rta a, *aa;
26
  rte *e;
27

    
28
  DBG("Installing static route %I/%d, rtd=%d\n", r->net, r->masklen, r->dest);
29
  bzero(&a, sizeof(a));
30
  a.proto = p;
31
  a.source = (r->dest == RTD_DEVICE) ? RTS_STATIC_DEVICE : RTS_STATIC;
32
  a.scope = SCOPE_UNIVERSE;
33
  a.cast = RTC_UNICAST;
34
  a.dest = r->dest;
35
  a.gw = r->via;
36
  a.iface = ifa;
37
  aa = rta_lookup(&a);
38

    
39
  n = net_get(p->table, r->net, r->masklen);
40
  e = rte_get_temp(aa);
41
  e->net = n;
42
  e->pflags = 0;
43
  rte_update(n, p, e);
44
}
45

    
46
static void
47
static_remove(struct proto *p, struct static_route *r)
48
{
49
  net *n;
50

    
51
  DBG("Removing static route %I/%d\n", r->net, r->masklen);
52
  n = net_find(p->table, r->net, r->masklen);
53
  if (n)
54
    rte_update(n, p, NULL);
55
}
56

    
57
static int
58
static_start(struct proto *p)
59
{
60
  struct static_config *c = (void *) p->cf;
61
  struct static_route *r;
62

    
63
  DBG("Static: take off!\n");
64
  WALK_LIST(r, c->other_routes)
65
    switch (r->dest)
66
      {
67
      case RTD_ROUTER:
68
        {
69
          struct neighbor *n = neigh_find(p, &r->via, NEF_STICKY);
70
          if (n)
71
            {
72
              r->chain = n->data;
73
              n->data = r;
74
              r->neigh = n;
75
              if (n->iface)
76
                static_install(p, r, n->iface);
77
            }
78
          else
79
            log(L_ERR "Static route destination %I is invalid. Ignoring.\n", r->via);
80
          break;
81
        }
82
      case RTD_DEVICE:
83
        break;
84
      default:
85
        static_install(p, r, NULL);
86
      }
87
  return PS_UP;
88
}
89

    
90
static void
91
static_neigh_notify(struct neighbor *n)
92
{
93
  struct proto *p = n->proto;
94
  struct static_route *r;
95

    
96
  DBG("Static: neighbor notify for %I: iface %p\n", n->addr, n->iface);
97
  for(r=n->data; r; r=r->chain)
98
    if (n->iface)
99
      static_install(p, r, n->iface);
100
    else
101
      static_remove(p, r);
102
}
103

    
104
static void
105
static_dump_rt(struct static_route *r)
106
{
107
  debug("%16I/%2d: ", r->net, r->masklen);
108
  switch (r->dest)
109
    {
110
    case RTD_ROUTER:
111
      debug("via %I\n", r->via);
112
      break;
113
    case RTD_DEVICE:
114
      debug("dev %s\n", r->if_name);
115
      break;
116
    default:
117
      debug("rtd %d\n", r->dest);
118
      break;
119
    }
120
}
121

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

    
128
  debug("Independent static routes:\n");
129
  WALK_LIST(r, c->other_routes)
130
    static_dump_rt(r);
131
  debug("Device static routes:\n");
132
  WALK_LIST(r, c->iface_routes)
133
    static_dump_rt(r);
134
}
135

    
136
static void
137
static_if_notify(struct proto *p, unsigned flags, struct iface *i)
138
{
139
  struct static_route *r;
140
  struct static_config *c = (void *) p->cf;
141

    
142
  if (flags & IF_CHANGE_UP)
143
    {
144
      WALK_LIST(r, c->iface_routes)
145
        if (!strcmp(r->if_name, i->name))
146
          static_install(p, r, i);
147
    }
148
  else if (flags & IF_CHANGE_DOWN)
149
    {
150
      WALK_LIST(r, c->iface_routes)
151
        if (!strcmp(r->if_name, i->name))
152
          static_remove(p, r);
153
    }
154
}
155

    
156
void
157
static_init_config(struct static_config *c)
158
{
159
  c->c.preference = DEF_PREF_STATIC;
160
  init_list(&c->iface_routes);
161
  init_list(&c->other_routes);
162
}
163

    
164
static struct proto *
165
static_init(struct proto_config *c)
166
{
167
  struct proto *p = proto_new(c, sizeof(struct proto));
168

    
169
  p->neigh_notify = static_neigh_notify;
170
  p->if_notify = static_if_notify;
171
  return p;
172
}
173

    
174
struct protocol proto_static = {
175
  name:                "Static",
176
  init:                static_init,
177
  dump:                static_dump,
178
  start:        static_start,
179
};