1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*
* BIRD -- UNIX Kernel Synchronization
*
* (c) 1998--1999 Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#define LOCAL_DEBUG
#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "lib/timer.h"
#include "unix.h"
#include "krt.h"
struct proto_config *cf_krt;
/*
* Routes
*/
static void
krt_flush_routes(struct krt_proto *p)
{
struct rtable *t = &master_table;
DBG("Flushing kernel routes...\n");
while (t && t->tos)
t = t->sibling;
if (!t)
return;
FIB_WALK(&t->fib, f)
{
net *n = (net *) f;
rte *e = n->routes;
if (e)
{
rta *a = e->attrs;
if (a->source != RTS_DEVICE && a->source != RTS_INHERIT)
krt_set_notify(&p->p, e->net, NULL, e);
}
}
FIB_WALK_END;
}
/* FIXME: Inbound/outbound route filtering? */
/* FIXME: Synchronization of multiple routing tables? */
static int
krt_uptodate(rte *k, rte *e)
{
rta *ka = k->attrs, *ea = e->attrs;
if (ka->dest != ea->dest)
return 0;
switch (ka->dest)
{
case RTD_ROUTER:
return ipa_equal(ka->gw, ea->gw);
case RTD_DEVICE:
return !strcmp(ka->iface->name, ea->iface->name);
default:
return 1;
}
}
/*
* This gets called back when the low-level scanning code discovers a route.
* We expect that the route is a temporary rte and its attributes are uncached.
*/
void
krt_got_route(struct krt_proto *p, rte *e)
{
rte *old;
net *net = e->net;
int src = e->u.krt_sync.src;
int verdict;
if (net->n.flags)
{
/* Route to this destination was already seen. Strange, but it happens... */
DBG("Already seen.\n");
return;
}
if (old = net->routes)
{
if (!krt_capable(old))
verdict = krt_capable(e) ? KRF_DELETE : KRF_SEEN;
else if (krt_uptodate(e, net->routes))
verdict = KRF_SEEN;
else
verdict = KRF_UPDATE;
}
else if (KRT_CF->learn && !net->routes && (src == KRT_SRC_ALIEN || src < 0))
verdict = KRF_LEARN;
else
verdict = KRF_DELETE;
DBG("krt_parse_entry: verdict=%s\n", ((char *[]) { "CREATE", "SEEN", "UPDATE", "DELETE", "LEARN" }) [verdict]);
net->n.flags = verdict;
if (verdict != KRF_SEEN)
{
/* Get a cached copy of attributes and link the route */
rta *a = e->attrs;
a->source = RTS_DUMMY;
e->attrs = rta_lookup(a);
e->next = net->routes;
net->routes = e;
}
else
rte_free(e);
}
static void
krt_prune(struct krt_proto *p)
{
struct proto *pp = &p->p;
struct rtable *t = &master_table;
struct fib_node *f;
DBG("Pruning routes...\n");
while (t && t->tos)
t = t->sibling;
if (!t)
return;
FIB_WALK(&t->fib, f)
{
net *n = (net *) f;
int verdict = f->flags;
rte *new, *old;
if (verdict != KRF_CREATE && verdict != KRF_SEEN)
{
old = n->routes;
n->routes = old->next;
}
else
old = NULL;
new = n->routes;
switch (verdict)
{
case KRF_CREATE:
if (new)
{
if (new->attrs->source == RTS_INHERIT)
{
DBG("krt_prune: removing inherited %I/%d\n", n->n.prefix, n->n.pxlen);
rte_update(n, pp, NULL);
}
else if (krt_capable(new))
{
DBG("krt_prune: reinstalling %I/%d\n", n->n.prefix, n->n.pxlen);
krt_set_notify(pp, n, new, NULL);
}
}
break;
case KRF_SEEN:
/* Nothing happens */
break;
case KRF_UPDATE:
DBG("krt_prune: updating %I/%d\n", n->n.prefix, n->n.pxlen);
krt_set_notify(pp, n, new, old);
break;
case KRF_DELETE:
DBG("krt_prune: deleting %I/%d\n", n->n.prefix, n->n.pxlen);
krt_set_notify(pp, n, NULL, old);
break;
case KRF_LEARN:
DBG("krt_prune: learning %I/%d\n", n->n.prefix, n->n.pxlen);
rte_update(n, pp, new);
break;
default:
bug("krt_prune: invalid route status");
}
if (old)
rte_free(old);
f->flags = 0;
}
FIB_WALK_END;
}
void
krt_got_route_async(struct krt_proto *p, rte *e, int new)
{
net *net = e->net;
rte *old = net->routes;
int src = e->u.krt_sync.src;
switch (src)
{
case KRT_SRC_BIRD:
ASSERT(0);
case KRT_SRC_REDIRECT:
DBG("It's a redirect, kill him! Kill! Kill!\n");
krt_set_notify(&p->p, net, NULL, e);
break;
default: /* Alien or unspecified */
if (KRT_CF->learn && new)
{
/*
* FIXME: This is limited to one inherited route per destination as we
* use single protocol for all inherited routes. Probably leave it
* as-is (and document it :)), because the correct solution is to
* multiple kernel tables anyway.
*/
DBG("Learning\n");
rte_update(net, &p->p, e);
}
else
{
DBG("Discarding\n");
rte_update(net, &p->p, NULL);
}
}
}
/*
* Periodic scanning
*/
static timer *krt_scan_timer;
static void
krt_scan(timer *t)
{
struct krt_proto *p = t->data;
DBG("KRT: It's scan time...\n");
krt_if_scan(p);
p->accum_time += KRT_CF->scan_time;
if (KRT_CF->route_scan_time && p->accum_time >= KRT_CF->route_scan_time)
{
p->accum_time %= KRT_CF->route_scan_time;
DBG("Scanning kernel routing table...\n");
krt_scan_fire(p);
krt_prune(p);
}
}
/*
* Protocol glue
*/
static int
krt_start(struct proto *P)
{
struct krt_proto *p = (struct krt_proto *) P;
p->accum_time = KRT_CF->route_scan_time - KRT_CF->scan_time;
krt_if_start(p);
krt_scan_start(p);
krt_set_start(p);
/* Start periodic interface scanning */
krt_scan_timer = tm_new(P->pool);
krt_scan_timer->hook = krt_scan;
krt_scan_timer->data = p;
krt_scan_timer->recurrent = KRT_CF->scan_time;
krt_scan(krt_scan_timer);
tm_start(krt_scan_timer, KRT_CF->scan_time);
return PS_UP;
}
int
krt_shutdown(struct proto *P)
{
struct krt_proto *p = (struct krt_proto *) P;
if (!KRT_CF->persist)
krt_flush_routes(p);
krt_set_shutdown(p);
krt_scan_shutdown(p);
/* Stop periodic interface scans */
tm_stop(krt_scan_timer);
krt_if_shutdown(p);
/* FIXME: What should we do with interfaces? */
return PS_DOWN;
}
static void
krt_preconfig(struct protocol *x, struct config *c)
{
struct krt_config *z = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config));
cf_krt = &z->c;
z->c.preference = DEF_PREF_UKR;
z->scan_time = z->route_scan_time = 60;
z->learn = z->persist = 0;
krt_scan_preconfig(z);
krt_set_preconfig(z);
krt_if_preconfig(z);
}
static struct proto *
krt_init(struct proto_config *c)
{
struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
p->p.rt_notify = krt_set_notify;
return &p->p;
}
struct protocol proto_unix_kernel = {
name: "Kernel",
priority: 90,
preconfig: krt_preconfig,
init: krt_init,
start: krt_start,
shutdown: krt_shutdown,
};
|