]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/rt-table.c
Fixes minor bug in pipe.
[thirdparty/bird.git] / nest / rt-table.c
CommitLineData
62aa008a 1/*
58740ed4 2 * BIRD -- Routing Tables
62aa008a 3 *
50fe90ed 4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
62aa008a
MM
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
58740ed4
MM
9/**
10 * DOC: Routing tables
11 *
12 * Routing tables are probably the most important structures BIRD uses. They
13 * hold all the information about known networks, the associated routes and
14 * their attributes.
15 *
725270cb 16 * There are multiple routing tables (a primary one together with any
58740ed4
MM
17 * number of secondary ones if requested by the configuration). Each table
18 * is basically a FIB containing entries describing the individual
58f7d004 19 * destination networks. For each network (represented by structure &net),
725270cb
MM
20 * there is a one-way linked list of route entries (&rte), the first entry
21 * on the list being the best one (i.e., the one we currently use
58740ed4
MM
22 * for routing), the order of the other ones is undetermined.
23 *
24 * The &rte contains information specific to the route (preference, protocol
25 * metrics, time of last modification etc.) and a pointer to a &rta structure
26 * (see the route attribute module for a precise explanation) holding the
27 * remaining route attributes which are expected to be shared by multiple
28 * routes in order to conserve memory.
29 */
30
6b9fa320 31#undef LOCAL_DEBUG
1a54b1c6 32
62aa008a
MM
33#include "nest/bird.h"
34#include "nest/route.h"
2326b001 35#include "nest/protocol.h"
730f2e2c
MM
36#include "nest/cli.h"
37#include "nest/iface.h"
2326b001 38#include "lib/resource.h"
5996da6a 39#include "lib/event.h"
730f2e2c 40#include "lib/string.h"
0e02abfd 41#include "conf/conf.h"
529c4149 42#include "filter/filter.h"
221135d6 43#include "lib/string.h"
10af3676 44#include "lib/alloca.h"
7d875e09 45
2326b001 46static slab *rte_slab;
e2dc2f30 47static linpool *rte_update_pool;
2326b001 48
5996da6a 49static pool *rt_table_pool;
0e02abfd 50static list routing_tables;
5996da6a 51
cfd46ee4
MM
52static void rt_format_via(rte *e, byte *via);
53
5996da6a 54static void
2326b001
MM
55rte_init(struct fib_node *N)
56{
57 net *n = (net *) N;
58
4c45595e 59 N->flags = 0;
2326b001
MM
60 n->routes = NULL;
61}
62
58740ed4
MM
63/**
64 * rte_find - find a route
65 * @net: network node
66 * @p: protocol
67 *
68 * The rte_find() function returns a route for destination @net
69 * which belongs has been defined by protocol @p.
70 */
2326b001
MM
71rte *
72rte_find(net *net, struct proto *p)
73{
74 rte *e = net->routes;
75
76 while (e && e->attrs->proto != p)
77 e = e->next;
78 return e;
79}
80
58740ed4
MM
81/**
82 * rte_get_temp - get a temporary &rte
3ce8c610 83 * @a: attributes to assign to the new route (a &rta; in case it's
2e9b2421 84 * un-cached, rte_update() will create a cached copy automatically)
58740ed4
MM
85 *
86 * Create a temporary &rte and bind it with the attributes @a.
87 * Also set route preference to the default preference set for
88 * the protocol.
89 */
2326b001
MM
90rte *
91rte_get_temp(rta *a)
92{
93 rte *e = sl_alloc(rte_slab);
94
95 e->attrs = a;
0cdbd397 96 e->flags = 0;
2326b001 97 e->pref = a->proto->preference;
2326b001
MM
98 return e;
99}
100
e2dc2f30
MM
101rte *
102rte_do_cow(rte *r)
103{
104 rte *e = sl_alloc(rte_slab);
105
106 memcpy(e, r, sizeof(rte));
107 e->attrs = rta_clone(r->attrs);
108 e->flags = 0;
109 return e;
110}
111
2326b001
MM
112static int /* Actually better or at least as good as */
113rte_better(rte *new, rte *old)
114{
d9f330c5
MM
115 int (*better)(rte *, rte *);
116
2326b001
MM
117 if (!old)
118 return 1;
119 if (new->pref > old->pref)
120 return 1;
121 if (new->pref < old->pref)
122 return 0;
739ebd8e 123 if (new->attrs->proto->proto != old->attrs->proto->proto)
4c1b4e1a
MM
124 {
125 /*
126 * If the user has configured protocol preferences, so that two different protocols
127 * have the same preference, try to break the tie by comparing addresses. Not too
128 * useful, but keeps the ordering of routes unambiguous.
129 */
130 return new->attrs->proto->proto > old->attrs->proto->proto;
131 }
d9f330c5
MM
132 if (better = new->attrs->proto->rte_better)
133 return better(new, old);
134 return 0;
2326b001
MM
135}
136
cfd46ee4
MM
137static void
138rte_trace(struct proto *p, rte *e, int dir, char *msg)
139{
140 byte via[STD_ADDRESS_P_LENGTH+32];
141
142 rt_format_via(e, via);
143 log(L_TRACE "%s %c %s %I/%d %s", p->name, dir, msg, e->net->n.prefix, e->net->n.pxlen, via);
144}
145
146static inline void
147rte_trace_in(unsigned int flag, struct proto *p, rte *e, char *msg)
148{
149 if (p->debug & flag)
b0a47440 150 rte_trace(p, e, '>', msg);
cfd46ee4
MM
151}
152
153static inline void
154rte_trace_out(unsigned int flag, struct proto *p, rte *e, char *msg)
155{
156 if (p->debug & flag)
b0a47440 157 rte_trace(p, e, '<', msg);
cfd46ee4
MM
158}
159
529c4149 160static inline void
0da472d7 161do_rte_announce(struct announce_hook *a, net *net, rte *new, rte *old, ea_list *tmpa, int class)
529c4149 162{
0e02abfd 163 struct proto *p = a->proto;
e2dc2f30
MM
164 rte *new0 = new;
165 rte *old0 = old;
08f0290a 166 int ok;
7de45ba4 167
e2dc2f30 168 if (new)
529c4149 169 {
13b75bac 170 char *drop_reason = NULL;
cfd46ee4 171 if ((class & IADDR_SCOPE_MASK) < p->min_scope)
13b75bac 172 drop_reason = "out of scope";
cfd46ee4 173 else if ((ok = p->import_control ? p->import_control(p, &new, &tmpa, rte_update_pool) : 0) < 0)
13b75bac 174 drop_reason = "rejected by protocol";
cfd46ee4
MM
175 else if (ok)
176 rte_trace_out(D_FILTERS, p, new, "forced accept by protocol");
177 else if (p->out_filter == FILTER_REJECT ||
3a6337ec 178 p->out_filter && f_run(p->out_filter, &new, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
13b75bac
MM
179 drop_reason = "filtered out";
180 if (drop_reason)
cfd46ee4 181 {
13b75bac 182 rte_trace_out(D_FILTERS, p, new, drop_reason);
2adab6ae
MM
183 if (new != new0)
184 rte_free(new);
cfd46ee4
MM
185 new = NULL;
186 }
529c4149 187 }
e2dc2f30
MM
188 if (old && p->out_filter)
189 {
e2dc2f30
MM
190 if (p->out_filter == FILTER_REJECT)
191 old = NULL;
192 else
193 {
194 ea_list *tmpb = p->make_tmp_attrs ? p->make_tmp_attrs(old, rte_update_pool) : NULL;
08f0290a
MM
195 ok = p->import_control ? p->import_control(p, &old, &tmpb, rte_update_pool) : 0;
196 if (ok < 0 || (!ok && f_run(p->out_filter, &old, &tmpb, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT))
13b75bac
MM
197 {
198 if (old != old0)
199 rte_free(old);
200 old = NULL;
201 }
e2dc2f30
MM
202 }
203 }
cfd46ee4
MM
204 if (p->debug & D_ROUTES)
205 {
206 if (new && old)
207 rte_trace_out(D_ROUTES, p, new, "replaced");
208 else if (new)
209 rte_trace_out(D_ROUTES, p, new, "added");
349e21bb 210 else if (old)
cfd46ee4
MM
211 rte_trace_out(D_ROUTES, p, old, "removed");
212 }
08f0290a
MM
213 if (!new && !old)
214 return;
215 if (!new)
216 p->rt_notify(p, net, NULL, old, NULL);
217 else if (tmpa)
218 {
2f711231
MM
219 ea_list *t = tmpa;
220 while (t->next)
221 t = t->next;
222 t->next = new->attrs->eattrs;
08f0290a 223 p->rt_notify(p, net, new, old, tmpa);
2f711231 224 t->next = NULL;
08f0290a
MM
225 }
226 else
227 p->rt_notify(p, net, new, old, new->attrs->eattrs);
e2dc2f30
MM
228 if (new && new != new0) /* Discard temporary rte's */
229 rte_free(new);
230 if (old && old != old0)
231 rte_free(old);
529c4149
MM
232}
233
9a8f20fc
MM
234/**
235 * rte_announce - announce a routing table change
236 * @tab: table the route has been added to
237 * @net: network in question
238 * @new: the new route to be announced
239 * @old: previous optimal route for the same network
240 * @tmpa: a list of temporary attributes belonging to the new route
241 *
242 * This function gets a routing table update and announces it
243 * to all protocols connected to the same table by their announcement hooks.
244 *
245 * For each such protocol, we first call its import_control() hook which
246 * performs basic checks on the route (each protocol has a right to veto
247 * or force accept of the route before any filter is asked) and adds default
248 * values of attributes specific to the new protocol (metrics, tags etc.).
249 * Then it consults the protocol's export filter and if it accepts the
250 * route, the rt_notify() hook of the protocol gets called.
251 */
e2dc2f30 252static void
0e02abfd 253rte_announce(rtable *tab, net *net, rte *new, rte *old, ea_list *tmpa)
2326b001 254{
0e02abfd 255 struct announce_hook *a;
0da472d7 256 int class = ipa_classify(net->n.prefix);
2326b001 257
0e02abfd 258 WALK_LIST(a, tab->hooks)
0a2e9d9f 259 {
8c943173 260 ASSERT(a->proto->core_state == FS_HAPPY || a->proto->core_state == FS_FEEDING);
0da472d7 261 do_rte_announce(a, net, new, old, tmpa, class);
0a2e9d9f 262 }
2326b001
MM
263}
264
421838ff
MM
265static inline int
266rte_validate(rte *e)
267{
268 int c;
269 net *n = e->net;
270
cfd46ee4
MM
271 if (ipa_nonzero(ipa_and(n->n.prefix, ipa_not(ipa_mkmask(n->n.pxlen)))))
272 {
273 log(L_BUG "Ignoring bogus prefix %I/%d received via %s",
274 n->n.prefix, n->n.pxlen, e->attrs->proto->name);
275 return 0;
276 }
421838ff
MM
277 if (n->n.pxlen)
278 {
279 c = ipa_classify(n->n.prefix);
280 if (c < 0 || !(c & IADDR_HOST))
281 {
85a291ff
MM
282 if (!ipa_nonzero(n->n.prefix))
283 {
284 /* Various default routes */
285#ifdef IPV6
286 if (n->n.pxlen == 96)
287#else
288 if (n->n.pxlen <= 1)
289#endif
290 return 1;
291 }
292 log(L_WARN "Ignoring bogus route %I/%d received via %s",
293 n->n.prefix, n->n.pxlen, e->attrs->proto->name);
421838ff
MM
294 return 0;
295 }
0da472d7 296 if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope)
421838ff 297 {
0da472d7
MM
298 log(L_WARN "Ignoring %s scope route %I/%d received from %I via %s",
299 ip_scope_text(c & IADDR_SCOPE_MASK),
300 n->n.prefix, n->n.pxlen, e->attrs->from, e->attrs->proto->name);
301 return 0;
421838ff
MM
302 }
303 }
304 return 1;
305}
306
58740ed4
MM
307/**
308 * rte_free - delete a &rte
309 * @e: &rte to be deleted
310 *
311 * rte_free() deletes the given &rte from the routing table it's linked to.
312 */
04925e90 313void
2326b001 314rte_free(rte *e)
04925e90
MM
315{
316 if (e->attrs->aflags & RTAF_CACHED)
317 rta_free(e->attrs);
318 sl_free(rte_slab, e);
319}
320
321static inline void
322rte_free_quick(rte *e)
2326b001
MM
323{
324 rta_free(e->attrs);
325 sl_free(rte_slab, e);
326}
327
67be5b23
MM
328static int
329rte_same(rte *x, rte *y)
330{
331 return
332 x->attrs == y->attrs &&
333 x->flags == y->flags &&
334 x->pflags == y->pflags &&
335 x->pref == y->pref &&
336 (!x->attrs->proto->rte_same || x->attrs->proto->rte_same(x, y));
337}
338
e2dc2f30 339static void
0e02abfd 340rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa)
2326b001
MM
341{
342 rte *old_best = net->routes;
343 rte *old = NULL;
344 rte **k, *r, *s;
2326b001
MM
345
346 k = &net->routes; /* Find and remove original route from the same protocol */
347 while (old = *k)
348 {
349 if (old->attrs->proto == p)
350 {
0b761098 351 if (new && rte_same(old, new))
67be5b23
MM
352 {
353 /* No changes, ignore the new route */
354 rte_trace_in(D_ROUTES, p, new, "ignored");
355 rte_free_quick(new);
356 old->lastmod = now;
357 return;
358 }
2326b001
MM
359 *k = old->next;
360 break;
361 }
362 k = &old->next;
363 }
364
0cdbd397 365 if (new && rte_better(new, old_best)) /* It's a new optimal route => announce and relink it */
2326b001 366 {
85810613 367 rte_trace_in(D_ROUTES, p, new, "added [best]");
0e02abfd 368 rte_announce(table, net, new, old_best, tmpa);
2326b001
MM
369 new->next = net->routes;
370 net->routes = new;
371 }
372 else
373 {
374 if (old == old_best) /* It has _replaced_ the old optimal route */
375 {
376 r = new; /* Find new optimal route and announce it */
377 for(s=net->routes; s; s=s->next)
378 if (rte_better(s, r))
379 r = s;
0e02abfd 380 rte_announce(table, net, r, old_best, tmpa);
2326b001
MM
381 if (r) /* Re-link the new optimal route */
382 {
383 k = &net->routes;
384 while (s = *k)
385 {
386 if (s == r)
387 {
388 *k = r->next;
389 break;
390 }
dafd580e 391 k = &s->next;
2326b001
MM
392 }
393 r->next = net->routes;
394 net->routes = r;
395 }
2eca3b3a
MM
396 else if (table->gc_counter++ >= table->config->gc_max_ops &&
397 table->gc_time + table->config->gc_min_time <= now)
398 ev_schedule(table->gc_event);
2326b001
MM
399 }
400 if (new) /* Link in the new non-optimal route */
401 {
402 new->next = old_best->next;
403 old_best->next = new;
cfd46ee4
MM
404 rte_trace_in(D_ROUTES, p, new, "added");
405 }
406 else if (old && (p->debug & D_ROUTES))
407 {
408 if (old != old_best)
409 rte_trace_in(D_ROUTES, p, old, "removed");
410 else if (net->routes)
411 rte_trace_in(D_ROUTES, p, old, "removed [replaced]");
412 else
413 rte_trace_in(D_ROUTES, p, old, "removed [sole]");
2326b001
MM
414 }
415 }
416 if (old)
5b22683d
MM
417 {
418 if (p->rte_remove)
419 p->rte_remove(net, old);
04925e90 420 rte_free_quick(old);
5b22683d 421 }
8ca8683c
MM
422 if (new)
423 {
424 new->lastmod = now;
425 if (p->rte_insert)
426 p->rte_insert(net, new);
427 }
5b22683d
MM
428}
429
e2dc2f30
MM
430static int rte_update_nest_cnt; /* Nesting counter to allow recursive updates */
431
432static inline void
433rte_update_lock(void)
434{
435 rte_update_nest_cnt++;
436}
437
438static inline void
439rte_update_unlock(void)
440{
441 if (!--rte_update_nest_cnt)
442 lp_flush(rte_update_pool);
443}
444
58740ed4
MM
445/**
446 * rte_update - enter a new update to a routing table
447 * @table: table to be updated
448 * @net: network node
449 * @p: protocol submitting the update
450 * @new: a &rte representing the new route or %NULL for route removal.
451 *
452 * This function is called by the routing protocols whenever they discover
453 * a new route or wish to update/remove an existing route. The right announcement
2e9b2421 454 * sequence is to build route attributes first (either un-cached with @aflags set
58740ed4
MM
455 * to zero or a cached one using rta_lookup(); in this case please note that
456 * you need to increase the use count of the attributes yourself by calling
457 * rta_clone()), call rte_get_temp() to obtain a temporary &rte, fill in all
458 * the appropriate data and finally submit the new &rte by calling rte_update().
459 *
9a8f20fc
MM
460 * When rte_update() gets any route, it automatically validates it (checks,
461 * whether the network and next hop address are valid IP addresses and also
462 * whether a normal routing protocol doesn't try to smuggle a host or link
463 * scope route to the table), converts all protocol dependent attributes stored
464 * in the &rte to temporary extended attributes, consults import filters of the
465 * protocol to see if the route should be accepted and/or its attributes modified,
466 * stores the temporary attributes back to the &rte.
467 *
468 * Now, having a "public" version of the route, we
469 * automatically find any old route defined by the protocol @p
58740ed4
MM
470 * for network @n, replace it by the new one (or removing it if @new is %NULL),
471 * recalculate the optimal route for this destination and finally broadcast
9a8f20fc 472 * the change (if any) to all routing protocols by calling rte_announce().
3ce8c610
MM
473 *
474 * All memory used for attribute lists and other temporary allocations is taken
475 * from a special linear pool @rte_update_pool and freed when rte_update()
476 * finishes.
58740ed4 477 */
e2dc2f30 478void
0e02abfd 479rte_update(rtable *table, net *net, struct proto *p, rte *new)
e2dc2f30
MM
480{
481 ea_list *tmpa = NULL;
482
483 rte_update_lock();
484 if (new)
485 {
cfd46ee4
MM
486 if (!rte_validate(new))
487 {
488 rte_trace_in(D_FILTERS, p, new, "invalid");
489 goto drop;
490 }
491 if (p->in_filter == FILTER_REJECT)
492 {
493 rte_trace_in(D_FILTERS, p, new, "filtered out");
494 goto drop;
495 }
e2dc2f30
MM
496 if (p->make_tmp_attrs)
497 tmpa = p->make_tmp_attrs(new, rte_update_pool);
498 if (p->in_filter)
499 {
ccdc3397 500 ea_list *old_tmpa = tmpa;
0a06a9b8 501 int fr = f_run(p->in_filter, &new, &tmpa, rte_update_pool, 0);
ccdc3397 502 if (fr > F_ACCEPT)
cfd46ee4
MM
503 {
504 rte_trace_in(D_FILTERS, p, new, "filtered out");
505 goto drop;
506 }
ccdc3397 507 if (tmpa != old_tmpa && p->store_tmp_attrs)
e2dc2f30
MM
508 p->store_tmp_attrs(new, tmpa);
509 }
510 if (!(new->attrs->aflags & RTAF_CACHED)) /* Need to copy attributes */
511 new->attrs = rta_lookup(new->attrs);
512 new->flags |= REF_COW;
513 }
0e02abfd 514 rte_recalculate(table, net, p, new, tmpa);
e2dc2f30
MM
515 rte_update_unlock();
516 return;
517
518drop:
519 rte_free(new);
520 rte_update_unlock();
521}
522
5b22683d 523void
0e02abfd 524rte_discard(rtable *t, rte *old) /* Non-filtered route deletion, used during garbage collection */
5b22683d 525{
cfd46ee4
MM
526 net *n = old->net;
527 struct proto *p = old->attrs->proto;
528
e2dc2f30 529 rte_update_lock();
cfd46ee4 530 rte_recalculate(t, n, p, NULL, NULL);
e2dc2f30 531 rte_update_unlock();
2326b001
MM
532}
533
58740ed4
MM
534/**
535 * rte_dump - dump a route
536 * @e: &rte to be dumped
537 *
538 * This functions dumps contents of a &rte to debug output.
539 */
2326b001 540void
a0762910 541rte_dump(rte *e)
2326b001 542{
a0762910 543 net *n = e->net;
0cdbd397 544 if (n)
e43ae633 545 debug("%-1I/%2d ", n->n.prefix, n->n.pxlen);
a0762910
MM
546 else
547 debug("??? ");
c10421d3 548 debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
0cdbd397 549 rta_dump(e->attrs);
c10421d3
MM
550 if (e->attrs->proto->proto->dump_attrs)
551 e->attrs->proto->proto->dump_attrs(e);
0cdbd397 552 debug("\n");
2326b001 553}
62aa008a 554
58740ed4
MM
555/**
556 * rt_dump - dump a routing table
557 * @t: routing table to be dumped
558 *
559 * This function dumps contents of a given routing table to debug output.
560 */
2326b001
MM
561void
562rt_dump(rtable *t)
563{
0cdbd397
MM
564 rte *e;
565 net *n;
0e02abfd 566 struct announce_hook *a;
0cdbd397
MM
567
568 debug("Dump of routing table <%s>\n", t->name);
e440395d 569#ifdef DEBUGGING
08e2d625 570 fib_check(&t->fib);
e440395d 571#endif
08e2d625
MM
572 FIB_WALK(&t->fib, fn)
573 {
574 n = (net *) fn;
575 for(e=n->routes; e; e=e->next)
576 rte_dump(e);
0cdbd397 577 }
08e2d625 578 FIB_WALK_END;
0e02abfd
MM
579 WALK_LIST(a, t->hooks)
580 debug("\tAnnounces routes to protocol %s\n", a->proto->name);
0cdbd397 581 debug("\n");
2326b001 582}
62aa008a 583
58740ed4
MM
584/**
585 * rt_dump_all - dump all routing tables
586 *
587 * This function dumps contents of all routing tables to debug output.
588 */
6d45cf21
MM
589void
590rt_dump_all(void)
591{
0e02abfd
MM
592 rtable *t;
593
594 WALK_LIST(t, routing_tables)
595 rt_dump(t);
6d45cf21
MM
596}
597
8f6accb5 598static void
b9626ec6 599rt_gc(void *tab)
5996da6a 600{
b9626ec6 601 rtable *t = tab;
0e02abfd 602
b9626ec6
MM
603 DBG("Entered routing table garbage collector for %s after %d seconds and %d deletes\n",
604 t->name, (int)(now - t->gc_time), t->gc_counter);
605 rt_prune(t);
5996da6a
MM
606}
607
b9626ec6
MM
608void
609rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf)
610{
611 bzero(t, sizeof(*t));
612 fib_init(&t->fib, p, sizeof(net), 0, rte_init);
613 t->name = name;
614 t->config = cf;
615 init_list(&t->hooks);
616 if (cf)
617 {
618 t->gc_event = ev_new(p);
619 t->gc_event->hook = rt_gc;
620 t->gc_event->data = t;
2eca3b3a 621 t->gc_time = now;
b9626ec6
MM
622 }
623}
624
58740ed4
MM
625/**
626 * rt_init - initialize routing tables
627 *
628 * This function is called during BIRD startup. It initializes the
629 * routing table module.
630 */
2326b001
MM
631void
632rt_init(void)
633{
634 rta_init();
5996da6a 635 rt_table_pool = rp_new(&root_pool, "Routing tables");
e2dc2f30 636 rte_update_pool = lp_new(rt_table_pool, 4080);
5996da6a 637 rte_slab = sl_new(rt_table_pool, sizeof(rte));
0e02abfd 638 init_list(&routing_tables);
2326b001 639}
1a54b1c6 640
58740ed4
MM
641/**
642 * rt_prune - prune a routing table
643 * @tab: routing table to be pruned
644 *
645 * This function is called whenever a protocol shuts down. It scans
646 * the routing table and removes all routes belonging to inactive
647 * protocols and also stale network entries.
648 */
1a54b1c6
MM
649void
650rt_prune(rtable *tab)
651{
652 struct fib_iterator fit;
5996da6a 653 int rcnt = 0, rdel = 0, ncnt = 0, ndel = 0;
1a54b1c6
MM
654
655 DBG("Pruning route table %s\n", tab->name);
0521e4f6
MM
656#ifdef DEBUGGING
657 fib_check(&tab->fib);
658#endif
08e2d625
MM
659 FIB_ITERATE_INIT(&fit, &tab->fib);
660again:
661 FIB_ITERATE_START(&tab->fib, &fit, f)
1a54b1c6 662 {
08e2d625
MM
663 net *n = (net *) f;
664 rte *e;
665 ncnt++;
666 rescan:
667 for (e=n->routes; e; e=e->next, rcnt++)
0521e4f6
MM
668 if (e->attrs->proto->core_state != FS_HAPPY &&
669 e->attrs->proto->core_state != FS_FEEDING)
08e2d625 670 {
0e02abfd 671 rte_discard(tab, e);
08e2d625
MM
672 rdel++;
673 goto rescan;
674 }
675 if (!n->routes) /* Orphaned FIB entry? */
1a54b1c6 676 {
08e2d625
MM
677 FIB_ITERATE_PUT(&fit, f);
678 fib_delete(&tab->fib, f);
679 ndel++;
680 goto again;
1a54b1c6 681 }
1a54b1c6 682 }
08e2d625 683 FIB_ITERATE_END(f);
2eca3b3a 684 DBG("Pruned %d of %d routes and %d of %d networks\n", rdel, rcnt, ndel, ncnt);
0521e4f6
MM
685#ifdef DEBUGGING
686 fib_check(&tab->fib);
687#endif
b9626ec6
MM
688 tab->gc_counter = 0;
689 tab->gc_time = now;
1a54b1c6 690}
0e02abfd 691
58740ed4
MM
692/**
693 * rt_prune_all - prune all routing tables
694 *
695 * This function calls rt_prune() for all known routing tables.
696 */
0e02abfd
MM
697void
698rt_prune_all(void)
699{
700 rtable *t;
701
702 WALK_LIST(t, routing_tables)
703 rt_prune(t);
704}
705
b9626ec6
MM
706struct rtable_config *
707rt_new_table(struct symbol *s)
708{
709 struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
710
711 cf_define_symbol(s, SYM_TABLE, c);
712 c->name = s->name;
713 add_tail(&new_config->tables, &c->n);
2eca3b3a 714 c->gc_max_ops = 1000;
b9626ec6
MM
715 c->gc_min_time = 5;
716 return c;
717}
718
0e02abfd
MM
719void
720rt_preconfig(struct config *c)
721{
722 struct symbol *s = cf_find_symbol("master");
0e02abfd 723
0e02abfd 724 init_list(&c->tables);
b9626ec6 725 c->master_rtc = rt_new_table(s);
0e02abfd
MM
726}
727
58740ed4
MM
728/**
729 * rt_lock_table - lock a routing table
730 * @r: routing table to be locked
731 *
732 * Lock a routing table, because it's in use by a protocol,
733 * preventing it from being freed when it gets undefined in a new
734 * configuration.
735 */
0e02abfd 736void
50fe90ed 737rt_lock_table(rtable *r)
0e02abfd 738{
50fe90ed
MM
739 r->use_count++;
740}
741
58740ed4
MM
742/**
743 * rt_unlock_table - unlock a routing table
744 * @r: routing table to be unlocked
745 *
746 * Unlock a routing table formerly locked by rt_lock_table(),
747 * that is decrease its use count and delete it if it's scheduled
748 * for deletion by configuration changes.
749 */
50fe90ed
MM
750void
751rt_unlock_table(rtable *r)
752{
753 if (!--r->use_count && r->deleted)
754 {
755 struct config *conf = r->deleted;
756 DBG("Deleting routing table %s\n", r->name);
757 rem_node(&r->n);
758 fib_free(&r->fib);
759 mb_free(r);
760 config_del_obstacle(conf);
761 }
762}
763
58740ed4
MM
764/**
765 * rt_commit - commit new routing table configuration
766 * @new: new configuration
767 * @old: original configuration or %NULL if it's boot time config
768 *
769 * Scan differences between @old and @new configuration and modify
770 * the routing tables according to these changes. If @new defines a
771 * previously unknown table, create it, if it omits a table existing
772 * in @old, schedule it for deletion (it gets deleted when all protocols
773 * disconnect from it by calling rt_unlock_table()), if it exists
774 * in both configurations, leave it unchanged.
775 */
50fe90ed
MM
776void
777rt_commit(struct config *new, struct config *old)
778{
779 struct rtable_config *o, *r;
0e02abfd 780
50fe90ed
MM
781 DBG("rt_commit:\n");
782 if (old)
0e02abfd 783 {
50fe90ed
MM
784 WALK_LIST(o, old->tables)
785 {
786 rtable *ot = o->table;
787 if (!ot->deleted)
788 {
789 struct symbol *sym = cf_find_symbol(o->name);
bf8558bc 790 if (sym && sym->class == SYM_TABLE && !new->shutdown)
50fe90ed
MM
791 {
792 DBG("\t%s: same\n", o->name);
793 r = sym->def;
794 r->table = ot;
795 ot->name = r->name;
b9626ec6 796 ot->config = r;
50fe90ed
MM
797 }
798 else
799 {
bf8558bc 800 DBG("\t%s: deleted\n", o->name);
50fe90ed
MM
801 ot->deleted = old;
802 config_add_obstacle(old);
803 rt_lock_table(ot);
804 rt_unlock_table(ot);
805 }
806 }
807 }
0e02abfd 808 }
50fe90ed
MM
809
810 WALK_LIST(r, new->tables)
811 if (!r->table)
812 {
813 rtable *t = mb_alloc(rt_table_pool, sizeof(struct rtable));
814 DBG("\t%s: created\n", r->name);
b9626ec6 815 rt_setup(rt_table_pool, t, r->name, r);
50fe90ed
MM
816 add_tail(&routing_tables, &t->n);
817 r->table = t;
818 }
819 DBG("\tdone\n");
0e02abfd 820}
730f2e2c 821
58740ed4
MM
822/**
823 * rt_feed_baby - advertise routes to a new protocol
824 * @p: protocol to be fed
825 *
826 * This function performs one pass of advertisement of routes to a newly
827 * initialized protocol. It's called by the protocol code as long as it
828 * has something to do. (We avoid transferring all the routes in single
829 * pass in order not to monopolize CPU time.)
830 */
ac5d8012
MM
831int
832rt_feed_baby(struct proto *p)
833{
834 struct announce_hook *h;
835 struct fib_iterator *fit;
76dfda9e 836 int max_feed = 256;
ac5d8012
MM
837
838 if (!p->feed_ahook) /* Need to initialize first */
839 {
840 if (!p->ahooks)
841 return 1;
842 DBG("Announcing routes to new protocol %s\n", p->name);
843 p->feed_ahook = p->ahooks;
844 fit = p->feed_iterator = mb_alloc(p->pool, sizeof(struct fib_iterator));
845 goto next_hook;
846 }
847 fit = p->feed_iterator;
848
849again:
850 h = p->feed_ahook;
851 FIB_ITERATE_START(&h->table->fib, fit, fn)
852 {
853 net *n = (net *) fn;
258d0ad4 854 rte *e = n->routes;
76dfda9e
MM
855 if (max_feed <= 0)
856 {
857 FIB_ITERATE_PUT(fit, fn);
858 return 0;
859 }
258d0ad4 860 if (e)
ac5d8012
MM
861 {
862 struct proto *q = e->attrs->proto;
863 ea_list *tmpa;
864
865 if (p->core_state != FS_FEEDING)
866 return 1; /* In the meantime, the protocol fell down. */
867 rte_update_lock();
868 tmpa = q->make_tmp_attrs ? q->make_tmp_attrs(e, rte_update_pool) : NULL;
869 do_rte_announce(h, n, e, NULL, tmpa, ipa_classify(n->n.prefix));
870 rte_update_unlock();
76dfda9e 871 max_feed--;
ac5d8012
MM
872 }
873 }
874 FIB_ITERATE_END(fn);
875 p->feed_ahook = h->next;
876 if (!p->feed_ahook)
877 {
878 mb_free(p->feed_iterator);
879 p->feed_iterator = NULL;
880 return 1;
881 }
882
883next_hook:
884 h = p->feed_ahook;
885 FIB_ITERATE_INIT(fit, &h->table->fib);
886 goto again;
887}
888
58740ed4
MM
889/**
890 * rt_feed_baby_abort - abort protocol feeding
891 * @p: protocol
892 *
893 * This function is called by the protocol code when the protocol
894 * stops or ceases to exist before the last iteration of rt_feed_baby()
895 * has finished.
896 */
ac5d8012
MM
897void
898rt_feed_baby_abort(struct proto *p)
899{
900 if (p->feed_ahook)
901 {
902 /* Unlink the iterator and exit */
903 fit_get(&p->feed_ahook->table->fib, p->feed_iterator);
904 p->feed_ahook = NULL;
905 }
906}
907
730f2e2c
MM
908/*
909 * CLI commands
910 */
911
912static void
cfd46ee4 913rt_format_via(rte *e, byte *via)
730f2e2c 914{
730f2e2c
MM
915 rta *a = e->attrs;
916
917 switch (a->dest)
918 {
919 case RTD_ROUTER: bsprintf(via, "via %I on %s", a->gw, a->iface->name); break;
920 case RTD_DEVICE: bsprintf(via, "dev %s", a->iface->name); break;
921 case RTD_BLACKHOLE: bsprintf(via, "blackhole"); break;
922 case RTD_UNREACHABLE: bsprintf(via, "unreachable"); break;
923 case RTD_PROHIBIT: bsprintf(via, "prohibited"); break;
924 default: bsprintf(via, "???");
925 }
cfd46ee4
MM
926}
927
928static void
ce1da96e 929rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
cfd46ee4 930{
85810613 931 byte via[STD_ADDRESS_P_LENGTH+32], from[STD_ADDRESS_P_LENGTH+6];
cfd46ee4
MM
932 byte tm[TM_RELTIME_BUFFER_SIZE], info[256];
933 rta *a = e->attrs;
934
935 rt_format_via(e, via);
730f2e2c
MM
936 tm_format_reltime(tm, e->lastmod);
937 if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->gw))
938 bsprintf(from, " from %I", a->from);
939 else
940 from[0] = 0;
ce1da96e
MM
941 if (a->proto->proto->get_route_info || d->verbose)
942 {
943 /* Need to normalize the extended attributes */
944 ea_list *t = tmpa;
945 t = ea_append(t, a->eattrs);
946 tmpa = alloca(ea_scan(t));
947 ea_merge(t, tmpa);
2f711231 948 ea_sort(tmpa);
ce1da96e 949 }
730f2e2c 950 if (a->proto->proto->get_route_info)
ce1da96e 951 a->proto->proto->get_route_info(e, info, tmpa);
730f2e2c
MM
952 else
953 bsprintf(info, " (%d)", e->pref);
954 cli_printf(c, -1007, "%-18s %s [%s %s%s]%s", ia, via, a->proto->name, tm, from, info);
955 if (d->verbose)
ce1da96e 956 rta_show(c, a, tmpa);
730f2e2c
MM
957}
958
959static void
960rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
961{
962 rte *e, *ee;
963 byte ia[STD_ADDRESS_P_LENGTH+8];
ce1da96e 964 int ok;
730f2e2c
MM
965
966 bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen);
0d307082
MM
967 if (n->routes)
968 d->net_counter++;
730f2e2c
MM
969 for(e=n->routes; e; e=e->next)
970 {
ce1da96e
MM
971 struct ea_list *tmpa, *old_tmpa;
972 struct proto *p0 = e->attrs->proto;
973 struct proto *p1 = d->import_protocol;
23693958 974 d->rt_counter++;
730f2e2c
MM
975 ee = e;
976 rte_update_lock(); /* We use the update buffer for filtering */
ce1da96e
MM
977 old_tmpa = tmpa = p0->make_tmp_attrs ? p0->make_tmp_attrs(e, rte_update_pool) : NULL;
978 ok = (d->filter == FILTER_ACCEPT || f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
979 if (ok && d->import_mode)
980 {
981 int ic = (p1->import_control ? p1->import_control(p1, &e, &tmpa, rte_update_pool) : 0);
982 if (ic < 0)
983 ok = 0;
984 else if (!ic && d->import_mode > 1)
985 {
986 if (p1->out_filter == FILTER_REJECT ||
987 p1->out_filter && f_run(p1->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
988 ok = 0;
989 }
990 }
991 if (ok)
730f2e2c 992 {
23693958 993 d->show_counter++;
33a368ad
MM
994 if (d->stats < 2)
995 rt_show_rte(c, ia, e, d, tmpa);
730f2e2c
MM
996 ia[0] = 0;
997 }
998 if (e != ee)
999 rte_free(ee);
1000 rte_update_unlock();
0117d004 1001 if (d->primary_only)
ce1da96e 1002 break;
730f2e2c
MM
1003 }
1004}
1005
1006static void
1007rt_show_cont(struct cli *c)
1008{
1009 struct rt_show_data *d = c->rover;
f098e072
MM
1010#ifdef DEBUGGING
1011 unsigned max = 4;
1012#else
1013 unsigned max = 64;
1014#endif
730f2e2c
MM
1015 struct fib *fib = &d->table->fib;
1016 struct fib_iterator *it = &d->fit;
1017
1018 FIB_ITERATE_START(fib, it, f)
1019 {
1020 net *n = (net *) f;
ce1da96e
MM
1021 if (d->running_on_config && d->running_on_config != config)
1022 {
1023 cli_printf(c, 8004, "Stopped due to reconfiguration");
1024 goto done;
1025 }
1026 if (d->import_protocol &&
1027 d->import_protocol->core_state != FS_HAPPY &&
1028 d->import_protocol->core_state != FS_FEEDING)
1029 {
1030 cli_printf(c, 8005, "Protocol is down");
1031 goto done;
1032 }
730f2e2c
MM
1033 if (!max--)
1034 {
1035 FIB_ITERATE_PUT(it, f);
1036 return;
1037 }
1038 rt_show_net(c, n, d);
1039 }
1040 FIB_ITERATE_END(f);
23693958
MM
1041 if (d->stats)
1042 cli_printf(c, 14, "%d of %d routes for %d networks", d->show_counter, d->rt_counter, d->net_counter);
1043 else
1044 cli_printf(c, 0, "");
ce1da96e 1045done:
730f2e2c
MM
1046 c->cont = c->cleanup = NULL;
1047}
1048
1049static void
1050rt_show_cleanup(struct cli *c)
1051{
1052 struct rt_show_data *d = c->rover;
1053
1054 /* Unlink the iterator */
1055 fit_get(&d->table->fib, &d->fit);
1056}
1057
1058void
1059rt_show(struct rt_show_data *d)
1060{
730f2e2c
MM
1061 net *n;
1062
1063 if (d->pxlen == 256)
1064 {
1065 FIB_ITERATE_INIT(&d->fit, &d->table->fib);
1066 this_cli->cont = rt_show_cont;
1067 this_cli->cleanup = rt_show_cleanup;
1068 this_cli->rover = d;
1069 }
1070 else
1071 {
9449c91a
MM
1072 if (d->show_for)
1073 n = fib_route(&d->table->fib, d->prefix, d->pxlen);
1074 else
1075 n = fib_find(&d->table->fib, &d->prefix, d->pxlen);
730f2e2c
MM
1076 if (n)
1077 {
1078 rt_show_net(this_cli, n, d);
1079 cli_msg(0, "");
1080 }
1081 else
1082 cli_msg(8001, "Network not in table");
1083 }
1084}
3ce8c610
MM
1085
1086/*
1087 * Documentation for functions declared inline in route.h
1088 */
1089#if 0
1090
1091/**
1092 * net_find - find a network entry
1093 * @tab: a routing table
1094 * @addr: address of the network
1095 * @len: length of the network prefix
1096 *
1097 * net_find() looks up the given network in routing table @tab and
1098 * returns a pointer to its &net entry or %NULL if no such network
1099 * exists.
1100 */
1101static inline net *net_find(rtable *tab, ip_addr addr, unsigned len)
1102{ DUMMY; }
1103
1104/**
1105 * net_get - obtain a network entry
1106 * @tab: a routing table
1107 * @addr: address of the network
1108 * @len: length of the network prefix
1109 *
1110 * net_get() looks up the given network in routing table @tab and
1111 * returns a pointer to its &net entry. If no such entry exists, it's
1112 * created.
1113 */
1114static inline net *net_get(rtable *tab, ip_addr addr, unsigned len)
1115{ DUMMY; }
1116
1117/**
1118 * rte_cow - copy a route for writing
1119 * @r: a route entry to be copied
1120 *
1121 * rte_cow() takes a &rte and prepares it for modification. The exact action
1122 * taken depends on the flags of the &rte -- if it's a temporary entry, it's
1123 * just returned unchanged, else a new temporary entry with the same contents
1124 * is created.
1125 *
1126 * The primary use of this function is inside the filter machinery -- when
1127 * a filter wants to modify &rte contents (to change the preference or to
1128 * attach another set of attributes), it must ensure that the &rte is not
1129 * shared with anyone else (and especially that it isn't stored in any routing
1130 * table).
1131 *
2e9b2421 1132 * Result: a pointer to the new writable &rte.
3ce8c610
MM
1133 */
1134static inline rte * rte_cow(rte *r)
1135{ DUMMY; }
1136
1137#endif