]> git.ipfire.org Git - thirdparty/bird.git/blame - sysdep/unix/krt.c
Don't forget to set proto->min_scope = SCOPE_HOST.
[thirdparty/bird.git] / sysdep / unix / krt.c
CommitLineData
2d140452
MM
1/*
2 * BIRD -- UNIX Kernel Synchronization
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 "nest/bird.h"
12#include "nest/iface.h"
13#include "nest/route.h"
14#include "nest/protocol.h"
15#include "lib/timer.h"
7de45ba4 16#include "conf/conf.h"
2d140452
MM
17
18#include "unix.h"
19#include "krt.h"
20
7de45ba4
MM
21/*
22 * The whole kernel synchronization is a bit messy and touches some internals
23 * of the routing table engine, because routing table maintenance is a typical
24 * example of the proverbial compatibility between different Unices and we want
25 * to keep the overhead of our krt business as low as possible and avoid maintaining
26 * a local routing table copy.
27 *
28 * The kernel syncer can work in three different modes (according to system config header):
29 * o Single routing table, single krt protocol. [traditional Unix]
30 * o Many routing tables, separate krt protocols for all of them.
31 * o Many routing tables, but every scan includes all tables, so we start
32 * separate krt protocols which cooperate with each other. [Linux 2.2]
33 * In this case, we keep only a single scan timer.
34 *
35 * The hacky bits:
36 * o We use FIB node flags to keep track of route synchronization status.
37 * o When starting up, we cheat by looking if there is another kernel
38 * krt instance to be initialized later and performing table scan
39 * only once for all the instances.
40 * o We attach temporary rte's to routing tables.
41 *
42 * If you are brave enough, continue now. You cannot say you haven't been warned.
43 */
44
c10421d3
MM
45static int krt_uptodate(rte *k, rte *e);
46
7e5f5ffd
MM
47/*
48 * Global resources
49 */
50
7de45ba4
MM
51pool *krt_pool;
52
7e5f5ffd
MM
53void
54krt_io_init(void)
55{
7de45ba4 56 krt_pool = rp_new(&root_pool, "Kernel Syncer");
7e5f5ffd
MM
57 krt_if_io_init();
58}
59
60/*
61 * Interfaces
62 */
63
64struct proto_config *cf_kif;
65
66static struct kif_proto *kif_proto;
67static timer *kif_scan_timer;
68static bird_clock_t kif_last_shot;
69
70static void
71kif_scan(timer *t)
72{
73 struct kif_proto *p = t->data;
74
75 DBG("KIF: It's interface scan time...\n");
76 kif_last_shot = now;
77 krt_if_scan(p);
78}
79
80static void
81kif_force_scan(void)
82{
83 if (kif_proto && kif_last_shot + 2 < now)
84 {
85 kif_scan(kif_scan_timer);
86 tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
87 }
88}
89
90static struct proto *
91kif_init(struct proto_config *c)
92{
93 struct kif_proto *p = proto_new(c, sizeof(struct kif_proto));
94 return &p->p;
95}
96
97static int
98kif_start(struct proto *P)
99{
100 struct kif_proto *p = (struct kif_proto *) P;
101
102 kif_proto = p;
103 krt_if_start(p);
104
105 /* Start periodic interface scanning */
106 kif_scan_timer = tm_new(P->pool);
107 kif_scan_timer->hook = kif_scan;
108 kif_scan_timer->data = p;
109 kif_scan_timer->recurrent = KIF_CF->scan_time;
110 kif_scan(kif_scan_timer);
111 tm_start(kif_scan_timer, KIF_CF->scan_time);
112
113 return PS_UP;
114}
115
116static int
117kif_shutdown(struct proto *P)
118{
119 struct kif_proto *p = (struct kif_proto *) P;
120
121 tm_stop(kif_scan_timer);
122 krt_if_shutdown(p);
123 kif_proto = NULL;
124
125 if_start_update(); /* Remove all interfaces */
126 if_end_update();
7de45ba4
MM
127 /*
128 * FIXME: Is it really a good idea? It causes routes to be flushed,
129 * but at the same time it avoids sending of these deletions to the kernel,
130 * because krt thinks the kernel itself has already removed the route
131 * when downing the interface. Sad.
132 */
7e5f5ffd
MM
133
134 return PS_DOWN;
135}
136
137struct protocol proto_unix_iface = {
138 name: "Device",
139 priority: 100,
140 init: kif_init,
141 start: kif_start,
142 shutdown: kif_shutdown,
143};
2d140452 144
c10421d3
MM
145/*
146 * Inherited Routes
147 */
148
149#ifdef KRT_ALLOW_LEARN
150
151static inline int
152krt_same_key(rte *a, rte *b)
153{
154 return a->u.krt.proto == b->u.krt.proto &&
155 a->u.krt.metric == b->u.krt.metric &&
156 a->u.krt.type == b->u.krt.type;
157}
158
159static void
160krt_learn_announce_update(struct krt_proto *p, rte *e)
161{
162 net *n = e->net;
163 rta *aa = rta_clone(e->attrs);
164 rte *ee = rte_get_temp(aa);
08e2d625 165 net *nn = net_get(p->p.table, n->n.prefix, n->n.pxlen);
c10421d3
MM
166 ee->net = nn;
167 ee->pflags = 0;
168 ee->u.krt = e->u.krt;
4f1a6d27 169 rte_update(p->p.table, nn, &p->p, ee);
c10421d3
MM
170}
171
172static void
173krt_learn_announce_delete(struct krt_proto *p, net *n)
174{
08e2d625 175 n = net_find(p->p.table, n->n.prefix, n->n.pxlen);
c10421d3 176 if (n)
4f1a6d27 177 rte_update(p->p.table, n, &p->p, NULL);
c10421d3
MM
178}
179
180static void
181krt_learn_scan(struct krt_proto *p, rte *e)
182{
183 net *n0 = e->net;
08e2d625 184 net *n = net_get(&p->krt_table, n0->n.prefix, n0->n.pxlen);
c10421d3
MM
185 rte *m, **mm;
186
187 e->attrs->source = RTS_INHERIT;
188
189 for(mm=&n->routes; m = *mm; mm=&m->next)
190 if (krt_same_key(m, e))
191 break;
192 if (m)
193 {
194 if (krt_uptodate(m, e))
195 {
196 DBG("krt_learn_scan: SEEN\n");
197 rte_free(e);
198 m->u.krt.seen = 1;
199 }
200 else
201 {
202 DBG("krt_learn_scan: OVERRIDE\n");
203 *mm = m->next;
204 rte_free(m);
205 m = NULL;
206 }
207 }
208 else
209 DBG("krt_learn_scan: CREATE\n");
210 if (!m)
211 {
212 e->attrs = rta_lookup(e->attrs);
213 e->next = n->routes;
214 n->routes = e;
215 e->u.krt.seen = 1;
216 }
217}
218
c10421d3
MM
219static void
220krt_learn_prune(struct krt_proto *p)
221{
222 struct fib *fib = &p->krt_table.fib;
223 struct fib_iterator fit;
224
225 DBG("Pruning inheritance data...\n");
226
227 FIB_ITERATE_INIT(&fit, fib);
228again:
229 FIB_ITERATE_START(fib, &fit, f)
230 {
231 net *n = (net *) f;
232 rte *e, **ee, *best, **pbest, *old_best;
233
234 old_best = n->routes;
235 best = NULL;
236 pbest = NULL;
237 ee = &n->routes;
238 while (e = *ee)
239 {
240 if (!e->u.krt.seen)
241 {
242 *ee = e->next;
243 rte_free(e);
244 continue;
245 }
246 if (!best || best->u.krt.metric > e->u.krt.metric)
247 {
248 best = e;
249 pbest = ee;
250 }
251 e->u.krt.seen = 0;
252 ee = &e->next;
253 }
254 if (!n->routes)
255 {
256 DBG("%I/%d: deleting\n", n->n.prefix, n->n.pxlen);
257 if (old_best)
258 {
259 krt_learn_announce_delete(p, n);
260 n->n.flags &= ~KRF_INSTALLED;
261 }
262 FIB_ITERATE_PUT(&fit, f);
263 fib_delete(fib, f);
264 goto again;
265 }
266 *pbest = best->next;
267 best->next = n->routes;
268 n->routes = best;
269 if (best != old_best || !(n->n.flags & KRF_INSTALLED))
270 {
271 DBG("%I/%d: announcing (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
272 krt_learn_announce_update(p, best);
273 n->n.flags |= KRF_INSTALLED;
274 }
275 else
276 DBG("%I/%d: uptodate (metric=%d)\n", n->n.prefix, n->n.pxlen, best->u.krt.metric);
277 }
278 FIB_ITERATE_END(f);
279}
280
281static void
282krt_learn_async(struct krt_proto *p, rte *e, int new)
283{
284 net *n0 = e->net;
08e2d625 285 net *n = net_get(&p->krt_table, n0->n.prefix, n0->n.pxlen);
c10421d3
MM
286 rte *g, **gg, *best, **bestp, *old_best;
287
288 e->attrs->source = RTS_INHERIT;
289
290 old_best = n->routes;
291 for(gg=&n->routes; g = *gg; gg = &g->next)
292 if (krt_same_key(g, e))
293 break;
294 if (new)
295 {
296 if (g)
297 {
298 if (krt_uptodate(g, e))
299 {
300 DBG("krt_learn_async: same\n");
301 rte_free(e);
302 return;
303 }
304 DBG("krt_learn_async: update\n");
305 *gg = g->next;
306 rte_free(g);
307 }
308 else
309 DBG("krt_learn_async: create\n");
310 e->attrs = rta_lookup(e->attrs);
311 e->next = n->routes;
312 n->routes = e;
313 }
314 else if (!g)
315 {
316 DBG("krt_learn_async: not found\n");
317 rte_free(e);
318 return;
319 }
320 else
321 {
322 DBG("krt_learn_async: delete\n");
323 *gg = g->next;
324 rte_free(e);
325 rte_free(g);
326 }
327 best = n->routes;
328 bestp = &n->routes;
329 for(gg=&n->routes; g=*gg; gg=&g->next)
330 if (best->u.krt.metric > g->u.krt.metric)
331 {
332 best = g;
333 bestp = gg;
334 }
335 if (best)
336 {
337 *bestp = best->next;
338 best->next = n->routes;
339 n->routes = best;
340 }
341 if (best != old_best)
342 {
343 DBG("krt_learn_async: distributing change\n");
344 if (best)
345 {
346 krt_learn_announce_update(p, best);
347 n->n.flags |= KRF_INSTALLED;
348 }
349 else
350 {
351 n->routes = NULL;
352 krt_learn_announce_delete(p, n);
353 n->n.flags &= ~KRF_INSTALLED;
354 }
355 }
356}
357
358static void
359krt_learn_init(struct krt_proto *p)
360{
361 if (KRT_CF->learn)
362 rt_setup(p->p.pool, &p->krt_table, "Inherited");
363}
364
365static void
366krt_dump(struct proto *P)
367{
368 struct krt_proto *p = (struct krt_proto *) P;
369
370 if (!KRT_CF->learn)
371 return;
372 debug("KRT: Table of inheritable routes\n");
373 rt_dump(&p->krt_table);
374}
375
376static void
377krt_dump_attrs(rte *e)
378{
379 debug(" [m=%d,p=%d,t=%d]", e->u.krt.metric, e->u.krt.proto, e->u.krt.type);
380}
381
382#endif
383
2d140452
MM
384/*
385 * Routes
386 */
387
7de45ba4
MM
388#ifdef CONFIG_ALL_TABLES_AT_ONCE
389static timer *krt_scan_timer;
390static int krt_instance_count;
391static list krt_instance_list;
392#endif
393
2d140452
MM
394static void
395krt_flush_routes(struct krt_proto *p)
396{
4f1a6d27 397 struct rtable *t = p->p.table;
2d140452
MM
398
399 DBG("Flushing kernel routes...\n");
2d140452
MM
400 FIB_WALK(&t->fib, f)
401 {
402 net *n = (net *) f;
403 rte *e = n->routes;
404 if (e)
405 {
406 rta *a = e->attrs;
407 if (a->source != RTS_DEVICE && a->source != RTS_INHERIT)
c10421d3 408 krt_set_notify(p, e->net, NULL, e);
2d140452
MM
409 }
410 }
411 FIB_WALK_END;
412}
413
2d140452
MM
414static int
415krt_uptodate(rte *k, rte *e)
416{
417 rta *ka = k->attrs, *ea = e->attrs;
418
419 if (ka->dest != ea->dest)
420 return 0;
421 switch (ka->dest)
422 {
423 case RTD_ROUTER:
424 return ipa_equal(ka->gw, ea->gw);
425 case RTD_DEVICE:
426 return !strcmp(ka->iface->name, ea->iface->name);
427 default:
428 return 1;
429 }
430}
431
432/*
433 * This gets called back when the low-level scanning code discovers a route.
434 * We expect that the route is a temporary rte and its attributes are uncached.
435 */
436
437void
438krt_got_route(struct krt_proto *p, rte *e)
439{
440 rte *old;
441 net *net = e->net;
c10421d3 442 int src = e->u.krt.src;
2d140452
MM
443 int verdict;
444
c10421d3
MM
445#ifdef CONFIG_AUTO_ROUTES
446 if (e->attrs->dest == RTD_DEVICE)
447 {
448 /* It's a device route. Probably a kernel-generated one. */
449 verdict = KRF_IGNORE;
450 goto sentenced;
451 }
452#endif
453
454#ifdef KRT_ALLOW_LEARN
455 if (src == KRT_SRC_ALIEN)
456 {
457 if (KRT_CF->learn)
458 krt_learn_scan(p, e);
459 else
460 DBG("krt_parse_entry: Alien route, ignoring\n");
461 return;
462 }
463#endif
464
465 if (net->n.flags & KRF_VERDICT_MASK)
2d140452
MM
466 {
467 /* Route to this destination was already seen. Strange, but it happens... */
468 DBG("Already seen.\n");
469 return;
470 }
471
c10421d3 472 if (net->n.flags & KRF_INSTALLED)
2d140452 473 {
c10421d3
MM
474 old = net->routes;
475 ASSERT(old);
476 if (krt_uptodate(e, old))
2d140452
MM
477 verdict = KRF_SEEN;
478 else
479 verdict = KRF_UPDATE;
480 }
2d140452
MM
481 else
482 verdict = KRF_DELETE;
483
c10421d3
MM
484sentenced:
485 DBG("krt_parse_entry: verdict=%s\n", ((char *[]) { "CREATE", "SEEN", "UPDATE", "DELETE", "IGNORE" }) [verdict]);
2d140452 486
c10421d3
MM
487 net->n.flags = (net->n.flags & ~KRF_VERDICT_MASK) | verdict;
488 if (verdict == KRF_UPDATE || verdict == KRF_DELETE)
2d140452
MM
489 {
490 /* Get a cached copy of attributes and link the route */
491 rta *a = e->attrs;
492 a->source = RTS_DUMMY;
493 e->attrs = rta_lookup(a);
494 e->next = net->routes;
495 net->routes = e;
496 }
497 else
498 rte_free(e);
499}
500
501static void
502krt_prune(struct krt_proto *p)
503{
504 struct proto *pp = &p->p;
4f1a6d27 505 struct rtable *t = p->p.table;
2d140452
MM
506 struct fib_node *f;
507
7de45ba4 508 DBG("Pruning routes in table %s...\n", t->name);
2d140452
MM
509 FIB_WALK(&t->fib, f)
510 {
511 net *n = (net *) f;
c10421d3 512 int verdict = f->flags & KRF_VERDICT_MASK;
2d140452
MM
513 rte *new, *old;
514
c10421d3 515 if (verdict != KRF_CREATE && verdict != KRF_SEEN && verdict != KRF_IGNORE)
2d140452
MM
516 {
517 old = n->routes;
518 n->routes = old->next;
519 }
520 else
521 old = NULL;
522 new = n->routes;
523
524 switch (verdict)
525 {
526 case KRF_CREATE:
c10421d3 527 if (new && (f->flags & KRF_INSTALLED))
2d140452 528 {
c10421d3
MM
529 DBG("krt_prune: reinstalling %I/%d\n", n->n.prefix, n->n.pxlen);
530 krt_set_notify(p, n, new, NULL);
2d140452
MM
531 }
532 break;
533 case KRF_SEEN:
c10421d3 534 case KRF_IGNORE:
2d140452
MM
535 /* Nothing happens */
536 break;
537 case KRF_UPDATE:
538 DBG("krt_prune: updating %I/%d\n", n->n.prefix, n->n.pxlen);
c10421d3 539 krt_set_notify(p, n, new, old);
2d140452
MM
540 break;
541 case KRF_DELETE:
542 DBG("krt_prune: deleting %I/%d\n", n->n.prefix, n->n.pxlen);
c10421d3 543 krt_set_notify(p, n, NULL, old);
2d140452
MM
544 break;
545 default:
546 bug("krt_prune: invalid route status");
547 }
2d140452
MM
548 if (old)
549 rte_free(old);
c10421d3 550 f->flags &= ~KRF_VERDICT_MASK;
2d140452
MM
551 }
552 FIB_WALK_END;
c10421d3
MM
553
554#ifdef KRT_ALLOW_LEARN
555 if (KRT_CF->learn)
556 krt_learn_prune(p);
557#endif
2d140452
MM
558}
559
e16155ae
MM
560void
561krt_got_route_async(struct krt_proto *p, rte *e, int new)
562{
563 net *net = e->net;
564 rte *old = net->routes;
c10421d3 565 int src = e->u.krt.src;
e16155ae
MM
566
567 switch (src)
568 {
569 case KRT_SRC_BIRD:
570 ASSERT(0);
571 case KRT_SRC_REDIRECT:
572 DBG("It's a redirect, kill him! Kill! Kill!\n");
c10421d3 573 krt_set_notify(p, net, NULL, e);
e16155ae 574 break;
c10421d3
MM
575 case KRT_SRC_ALIEN:
576#ifdef KRT_ALLOW_LEARN
577 if (KRT_CF->learn)
e16155ae 578 {
c10421d3
MM
579 krt_learn_async(p, e, new);
580 return;
e16155ae 581 }
c10421d3
MM
582#endif
583 /* Fall-thru */
584 default:
585 DBG("Discarding\n");
4f1a6d27 586 rte_update(p->p.table, net, &p->p, NULL);
e16155ae 587 }
c10421d3 588 rte_free(e);
e16155ae
MM
589}
590
2d140452
MM
591/*
592 * Periodic scanning
593 */
594
2d140452
MM
595static void
596krt_scan(timer *t)
597{
7de45ba4 598 struct krt_proto *p;
2d140452 599
7e5f5ffd 600 kif_force_scan();
7de45ba4
MM
601#ifdef CONFIG_ALL_TABLES_AT_ONCE
602 {
603 void *q;
604 DBG("KRT: It's route scan time...\n");
605 krt_scan_fire(NULL);
606 WALK_LIST(q, krt_instance_list)
607 {
608 p = SKIP_BACK(struct krt_proto, instance_node, q);
609 krt_prune(p);
610 }
611 }
612#else
613 p = t->data;
614 DBG("KRT: It's route scan time for %s...\n", p->p.name);
7e5f5ffd
MM
615 krt_scan_fire(p);
616 krt_prune(p);
7de45ba4 617#endif
2d140452
MM
618}
619
c10421d3
MM
620/*
621 * Updates
622 */
623
624static void
bb027be1 625krt_notify(struct proto *P, net *net, rte *new, rte *old, struct ea_list *tmpa)
c10421d3
MM
626{
627 struct krt_proto *p = (struct krt_proto *) P;
628
629 if (new && (!krt_capable(new) || new->attrs->source == RTS_INHERIT))
630 new = NULL;
631 if (!(net->n.flags & KRF_INSTALLED))
632 old = NULL;
633 if (new)
634 net->n.flags |= KRF_INSTALLED;
635 else
636 net->n.flags &= ~KRF_INSTALLED;
637 krt_set_notify(p, net, new, old);
638}
639
2d140452
MM
640/*
641 * Protocol glue
642 */
643
7e5f5ffd
MM
644struct proto_config *cf_krt;
645
7de45ba4
MM
646static void
647krt_preconfig(struct protocol *P, struct config *c)
648{
649 krt_scan_preconfig(c);
650}
651
652static void
653krt_postconfig(struct proto_config *C)
654{
655 struct krt_config *c = (struct krt_config *) C;
656
657#ifdef CONFIG_ALL_TABLES_AT_ONCE
658 struct krt_config *first = (struct krt_config *) cf_krt;
659 if (first->scan_time != c->scan_time)
660 cf_error("All kernel syncers must use the same table scan interval");
661#endif
662
663 if (C->table->krt_attached)
664 cf_error("Kernel syncer (%s) already attached to table %s", C->table->krt_attached->name, C->table->name);
665 C->table->krt_attached = C;
666 krt_scan_postconfig(c);
667}
668
669static timer *
670krt_start_timer(struct krt_proto *p)
671{
672 timer *t;
673
674 t = tm_new(p->krt_pool);
675 t->hook = krt_scan;
676 t->data = p;
677 t->recurrent = KRT_CF->scan_time;
678 tm_start(t, KRT_CF->scan_time);
679 return t;
680}
681
2d140452
MM
682static int
683krt_start(struct proto *P)
684{
685 struct krt_proto *p = (struct krt_proto *) P;
7de45ba4
MM
686 int first = 1;
687
688#ifdef CONFIG_ALL_TABLES_AT_ONCE
689 if (!krt_instance_count++)
690 init_list(&krt_instance_list);
691 else
692 first = 0;
693 p->krt_pool = krt_pool;
694 add_tail(&krt_instance_list, &p->instance_node);
695#else
696 p->krt_pool = P->pool;
697#endif
2d140452 698
c10421d3
MM
699#ifdef KRT_ALLOW_LEARN
700 krt_learn_init(p);
701#endif
702
7de45ba4
MM
703 krt_scan_start(p, first);
704 krt_set_start(p, first);
2d140452 705
7e5f5ffd 706 /* Start periodic routing table scanning */
7de45ba4
MM
707#ifdef CONFIG_ALL_TABLES_AT_ONCE
708 if (first)
709 krt_scan_timer = krt_start_timer(p);
710 p->scan_timer = krt_scan_timer;
711 /* If this is the last instance to be initialized, kick the timer */
712 if (!P->proto->startup_counter)
713 krt_scan(p->scan_timer);
714#else
715 p->scan_timer = krt_start_timer(p);
716 krt_scan(p->scan_timer);
717#endif
2d140452
MM
718
719 return PS_UP;
720}
721
7de45ba4 722static int
2d140452
MM
723krt_shutdown(struct proto *P)
724{
725 struct krt_proto *p = (struct krt_proto *) P;
7de45ba4 726 int last = 1;
2d140452 727
7de45ba4
MM
728#ifdef CONFIG_ALL_TABLES_AT_ONCE
729 rem_node(&p->instance_node);
730 if (--krt_instance_count)
731 last = 0;
732 else
733#endif
734 tm_stop(p->scan_timer);
7e5f5ffd 735
2d140452
MM
736 if (!KRT_CF->persist)
737 krt_flush_routes(p);
738
7de45ba4
MM
739 krt_set_shutdown(p, last);
740 krt_scan_shutdown(p, last);
741
742#ifdef CONFIG_ALL_TABLES_AT_ONCE
743 if (last)
744 rfree(krt_scan_timer);
745#endif
2d140452 746
2d140452
MM
747 return PS_DOWN;
748}
749
2d140452
MM
750static struct proto *
751krt_init(struct proto_config *c)
752{
753 struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
754
c10421d3 755 p->p.rt_notify = krt_notify;
0da472d7 756 p->p.min_scope = SCOPE_HOST;
2d140452
MM
757 return &p->p;
758}
759
760struct protocol proto_unix_kernel = {
761 name: "Kernel",
7e5f5ffd 762 priority: 80,
7de45ba4
MM
763 preconfig: krt_preconfig,
764 postconfig: krt_postconfig,
2d140452
MM
765 init: krt_init,
766 start: krt_start,
767 shutdown: krt_shutdown,
c10421d3
MM
768#ifdef KRT_ALLOW_LEARN
769 dump: krt_dump,
770 dump_attrs: krt_dump_attrs,
771#endif
2d140452 772};