]>
Commit | Line | Data |
---|---|---|
62aa008a MM |
1 | /* |
2 | * BIRD -- Routing Table | |
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 | ||
6b9fa320 | 9 | #undef LOCAL_DEBUG |
1a54b1c6 | 10 | |
62aa008a MM |
11 | #include "nest/bird.h" |
12 | #include "nest/route.h" | |
2326b001 | 13 | #include "nest/protocol.h" |
730f2e2c MM |
14 | #include "nest/cli.h" |
15 | #include "nest/iface.h" | |
2326b001 | 16 | #include "lib/resource.h" |
5996da6a | 17 | #include "lib/event.h" |
730f2e2c | 18 | #include "lib/string.h" |
0e02abfd | 19 | #include "conf/conf.h" |
529c4149 | 20 | #include "filter/filter.h" |
221135d6 | 21 | #include "lib/string.h" |
2326b001 | 22 | |
2326b001 | 23 | static slab *rte_slab; |
e2dc2f30 | 24 | static linpool *rte_update_pool; |
2326b001 | 25 | |
5996da6a | 26 | static pool *rt_table_pool; |
0e02abfd | 27 | static list routing_tables; |
5996da6a | 28 | |
cfd46ee4 MM |
29 | static void rt_format_via(rte *e, byte *via); |
30 | ||
5996da6a | 31 | static void |
2326b001 MM |
32 | rte_init(struct fib_node *N) |
33 | { | |
34 | net *n = (net *) N; | |
35 | ||
4c45595e | 36 | N->flags = 0; |
2326b001 MM |
37 | n->routes = NULL; |
38 | } | |
39 | ||
2326b001 MM |
40 | rte * |
41 | rte_find(net *net, struct proto *p) | |
42 | { | |
43 | rte *e = net->routes; | |
44 | ||
45 | while (e && e->attrs->proto != p) | |
46 | e = e->next; | |
47 | return e; | |
48 | } | |
49 | ||
50 | rte * | |
51 | rte_get_temp(rta *a) | |
52 | { | |
53 | rte *e = sl_alloc(rte_slab); | |
54 | ||
55 | e->attrs = a; | |
0cdbd397 | 56 | e->flags = 0; |
2326b001 | 57 | e->pref = a->proto->preference; |
2326b001 MM |
58 | return e; |
59 | } | |
60 | ||
e2dc2f30 MM |
61 | rte * |
62 | rte_do_cow(rte *r) | |
63 | { | |
64 | rte *e = sl_alloc(rte_slab); | |
65 | ||
66 | memcpy(e, r, sizeof(rte)); | |
67 | e->attrs = rta_clone(r->attrs); | |
68 | e->flags = 0; | |
69 | return e; | |
70 | } | |
71 | ||
2326b001 MM |
72 | static int /* Actually better or at least as good as */ |
73 | rte_better(rte *new, rte *old) | |
74 | { | |
d9f330c5 MM |
75 | int (*better)(rte *, rte *); |
76 | ||
2326b001 MM |
77 | if (!old) |
78 | return 1; | |
79 | if (new->pref > old->pref) | |
80 | return 1; | |
81 | if (new->pref < old->pref) | |
82 | return 0; | |
739ebd8e | 83 | if (new->attrs->proto->proto != old->attrs->proto->proto) |
4c1b4e1a MM |
84 | { |
85 | /* | |
86 | * If the user has configured protocol preferences, so that two different protocols | |
87 | * have the same preference, try to break the tie by comparing addresses. Not too | |
88 | * useful, but keeps the ordering of routes unambiguous. | |
89 | */ | |
90 | return new->attrs->proto->proto > old->attrs->proto->proto; | |
91 | } | |
d9f330c5 MM |
92 | if (better = new->attrs->proto->rte_better) |
93 | return better(new, old); | |
94 | return 0; | |
2326b001 MM |
95 | } |
96 | ||
cfd46ee4 MM |
97 | static void |
98 | rte_trace(struct proto *p, rte *e, int dir, char *msg) | |
99 | { | |
100 | byte via[STD_ADDRESS_P_LENGTH+32]; | |
101 | ||
102 | rt_format_via(e, via); | |
103 | log(L_TRACE "%s %c %s %I/%d %s", p->name, dir, msg, e->net->n.prefix, e->net->n.pxlen, via); | |
104 | } | |
105 | ||
106 | static inline void | |
107 | rte_trace_in(unsigned int flag, struct proto *p, rte *e, char *msg) | |
108 | { | |
109 | if (p->debug & flag) | |
b0a47440 | 110 | rte_trace(p, e, '>', msg); |
cfd46ee4 MM |
111 | } |
112 | ||
113 | static inline void | |
114 | rte_trace_out(unsigned int flag, struct proto *p, rte *e, char *msg) | |
115 | { | |
116 | if (p->debug & flag) | |
b0a47440 | 117 | rte_trace(p, e, '<', msg); |
cfd46ee4 MM |
118 | } |
119 | ||
529c4149 | 120 | static inline void |
0da472d7 | 121 | do_rte_announce(struct announce_hook *a, net *net, rte *new, rte *old, ea_list *tmpa, int class) |
529c4149 | 122 | { |
0e02abfd | 123 | struct proto *p = a->proto; |
e2dc2f30 MM |
124 | rte *new0 = new; |
125 | rte *old0 = old; | |
7de45ba4 | 126 | |
e2dc2f30 | 127 | if (new) |
529c4149 | 128 | { |
0da472d7 | 129 | int ok; |
cfd46ee4 MM |
130 | if ((class & IADDR_SCOPE_MASK) < p->min_scope) |
131 | { | |
132 | rte_trace_out(D_FILTERS, p, new, "out of scope"); | |
133 | new = NULL; | |
134 | } | |
135 | else if ((ok = p->import_control ? p->import_control(p, &new, &tmpa, rte_update_pool) : 0) < 0) | |
136 | { | |
137 | rte_trace_out(D_FILTERS, p, new, "rejected by protocol"); | |
138 | new = NULL; | |
139 | } | |
140 | else if (ok) | |
141 | rte_trace_out(D_FILTERS, p, new, "forced accept by protocol"); | |
142 | else if (p->out_filter == FILTER_REJECT || | |
3a6337ec | 143 | p->out_filter && f_run(p->out_filter, &new, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT) |
cfd46ee4 MM |
144 | { |
145 | rte_trace_out(D_FILTERS, p, new, "filtered out"); | |
146 | new = NULL; | |
147 | } | |
529c4149 | 148 | } |
e2dc2f30 MM |
149 | if (old && p->out_filter) |
150 | { | |
151 | /* FIXME: Do we really need to filter old routes? */ | |
152 | if (p->out_filter == FILTER_REJECT) | |
153 | old = NULL; | |
154 | else | |
155 | { | |
156 | ea_list *tmpb = p->make_tmp_attrs ? p->make_tmp_attrs(old, rte_update_pool) : NULL; | |
3a6337ec | 157 | if (f_run(p->out_filter, &old, &tmpb, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT) |
e2dc2f30 MM |
158 | old = NULL; |
159 | } | |
160 | } | |
cfd46ee4 MM |
161 | if (p->debug & D_ROUTES) |
162 | { | |
163 | if (new && old) | |
164 | rte_trace_out(D_ROUTES, p, new, "replaced"); | |
165 | else if (new) | |
166 | rte_trace_out(D_ROUTES, p, new, "added"); | |
349e21bb | 167 | else if (old) |
cfd46ee4 MM |
168 | rte_trace_out(D_ROUTES, p, old, "removed"); |
169 | } | |
529c4149 | 170 | if (new || old) |
bb027be1 | 171 | p->rt_notify(p, net, new, old, tmpa); |
e2dc2f30 MM |
172 | if (new && new != new0) /* Discard temporary rte's */ |
173 | rte_free(new); | |
174 | if (old && old != old0) | |
175 | rte_free(old); | |
529c4149 MM |
176 | } |
177 | ||
e2dc2f30 | 178 | static void |
0e02abfd | 179 | rte_announce(rtable *tab, net *net, rte *new, rte *old, ea_list *tmpa) |
2326b001 | 180 | { |
0e02abfd | 181 | struct announce_hook *a; |
0da472d7 | 182 | int class = ipa_classify(net->n.prefix); |
2326b001 | 183 | |
0e02abfd | 184 | WALK_LIST(a, tab->hooks) |
0a2e9d9f | 185 | { |
8c943173 | 186 | ASSERT(a->proto->core_state == FS_HAPPY || a->proto->core_state == FS_FEEDING); |
0da472d7 | 187 | do_rte_announce(a, net, new, old, tmpa, class); |
0a2e9d9f | 188 | } |
2326b001 MM |
189 | } |
190 | ||
47b79306 MM |
191 | void |
192 | rt_feed_baby(struct proto *p) | |
193 | { | |
0e02abfd | 194 | struct announce_hook *h; |
47b79306 | 195 | |
0e02abfd | 196 | if (!p->ahooks) |
47b79306 | 197 | return; |
6b9fa320 | 198 | DBG("Announcing routes to new protocol %s\n", p->name); |
0e02abfd | 199 | for(h=p->ahooks; h; h=h->next) |
47b79306 | 200 | { |
0e02abfd MM |
201 | rtable *t = h->table; |
202 | FIB_WALK(&t->fib, fn) | |
47b79306 | 203 | { |
0e02abfd MM |
204 | net *n = (net *) fn; |
205 | rte *e; | |
206 | for(e=n->routes; e; e=e->next) | |
207 | { | |
208 | struct proto *q = e->attrs->proto; | |
209 | ea_list *tmpa = q->make_tmp_attrs ? q->make_tmp_attrs(e, rte_update_pool) : NULL; | |
0da472d7 | 210 | do_rte_announce(h, n, e, NULL, tmpa, ipa_classify(n->n.prefix)); |
0e02abfd MM |
211 | lp_flush(rte_update_pool); |
212 | } | |
47b79306 | 213 | } |
0e02abfd | 214 | FIB_WALK_END; |
47b79306 MM |
215 | } |
216 | } | |
217 | ||
421838ff MM |
218 | static inline int |
219 | rte_validate(rte *e) | |
220 | { | |
221 | int c; | |
222 | net *n = e->net; | |
223 | ||
cfd46ee4 MM |
224 | if (ipa_nonzero(ipa_and(n->n.prefix, ipa_not(ipa_mkmask(n->n.pxlen))))) |
225 | { | |
226 | log(L_BUG "Ignoring bogus prefix %I/%d received via %s", | |
227 | n->n.prefix, n->n.pxlen, e->attrs->proto->name); | |
228 | return 0; | |
229 | } | |
421838ff MM |
230 | if (n->n.pxlen) |
231 | { | |
232 | c = ipa_classify(n->n.prefix); | |
233 | if (c < 0 || !(c & IADDR_HOST)) | |
234 | { | |
85a291ff MM |
235 | if (!ipa_nonzero(n->n.prefix)) |
236 | { | |
237 | /* Various default routes */ | |
238 | #ifdef IPV6 | |
239 | if (n->n.pxlen == 96) | |
240 | #else | |
241 | if (n->n.pxlen <= 1) | |
242 | #endif | |
243 | return 1; | |
244 | } | |
245 | log(L_WARN "Ignoring bogus route %I/%d received via %s", | |
246 | n->n.prefix, n->n.pxlen, e->attrs->proto->name); | |
421838ff MM |
247 | return 0; |
248 | } | |
0da472d7 | 249 | if ((c & IADDR_SCOPE_MASK) < e->attrs->proto->min_scope) |
421838ff | 250 | { |
0da472d7 MM |
251 | log(L_WARN "Ignoring %s scope route %I/%d received from %I via %s", |
252 | ip_scope_text(c & IADDR_SCOPE_MASK), | |
253 | n->n.prefix, n->n.pxlen, e->attrs->from, e->attrs->proto->name); | |
254 | return 0; | |
421838ff MM |
255 | } |
256 | } | |
257 | return 1; | |
258 | } | |
259 | ||
04925e90 | 260 | void |
2326b001 | 261 | rte_free(rte *e) |
04925e90 MM |
262 | { |
263 | if (e->attrs->aflags & RTAF_CACHED) | |
264 | rta_free(e->attrs); | |
265 | sl_free(rte_slab, e); | |
266 | } | |
267 | ||
268 | static inline void | |
269 | rte_free_quick(rte *e) | |
2326b001 MM |
270 | { |
271 | rta_free(e->attrs); | |
272 | sl_free(rte_slab, e); | |
273 | } | |
274 | ||
67be5b23 MM |
275 | static int |
276 | rte_same(rte *x, rte *y) | |
277 | { | |
278 | return | |
279 | x->attrs == y->attrs && | |
280 | x->flags == y->flags && | |
281 | x->pflags == y->pflags && | |
282 | x->pref == y->pref && | |
283 | (!x->attrs->proto->rte_same || x->attrs->proto->rte_same(x, y)); | |
284 | } | |
285 | ||
e2dc2f30 | 286 | static void |
0e02abfd | 287 | rte_recalculate(rtable *table, net *net, struct proto *p, rte *new, ea_list *tmpa) |
2326b001 MM |
288 | { |
289 | rte *old_best = net->routes; | |
290 | rte *old = NULL; | |
291 | rte **k, *r, *s; | |
2326b001 MM |
292 | |
293 | k = &net->routes; /* Find and remove original route from the same protocol */ | |
294 | while (old = *k) | |
295 | { | |
296 | if (old->attrs->proto == p) | |
297 | { | |
0b761098 | 298 | if (new && rte_same(old, new)) |
67be5b23 MM |
299 | { |
300 | /* No changes, ignore the new route */ | |
301 | rte_trace_in(D_ROUTES, p, new, "ignored"); | |
302 | rte_free_quick(new); | |
303 | old->lastmod = now; | |
304 | return; | |
305 | } | |
2326b001 MM |
306 | *k = old->next; |
307 | break; | |
308 | } | |
309 | k = &old->next; | |
310 | } | |
311 | ||
0cdbd397 | 312 | if (new && rte_better(new, old_best)) /* It's a new optimal route => announce and relink it */ |
2326b001 | 313 | { |
85810613 | 314 | rte_trace_in(D_ROUTES, p, new, "added [best]"); |
0e02abfd | 315 | rte_announce(table, net, new, old_best, tmpa); |
2326b001 MM |
316 | new->next = net->routes; |
317 | net->routes = new; | |
318 | } | |
319 | else | |
320 | { | |
321 | if (old == old_best) /* It has _replaced_ the old optimal route */ | |
322 | { | |
323 | r = new; /* Find new optimal route and announce it */ | |
324 | for(s=net->routes; s; s=s->next) | |
325 | if (rte_better(s, r)) | |
326 | r = s; | |
0e02abfd | 327 | rte_announce(table, net, r, old_best, tmpa); |
2326b001 MM |
328 | if (r) /* Re-link the new optimal route */ |
329 | { | |
330 | k = &net->routes; | |
331 | while (s = *k) | |
332 | { | |
333 | if (s == r) | |
334 | { | |
335 | *k = r->next; | |
336 | break; | |
337 | } | |
dafd580e | 338 | k = &s->next; |
2326b001 MM |
339 | } |
340 | r->next = net->routes; | |
341 | net->routes = r; | |
b9626ec6 MM |
342 | if (!r && |
343 | table->gc_counter++ >= table->config->gc_max_ops && | |
344 | table->gc_time + table->config->gc_min_time <= now) | |
345 | ev_schedule(table->gc_event); | |
2326b001 MM |
346 | } |
347 | } | |
348 | if (new) /* Link in the new non-optimal route */ | |
349 | { | |
350 | new->next = old_best->next; | |
351 | old_best->next = new; | |
cfd46ee4 MM |
352 | rte_trace_in(D_ROUTES, p, new, "added"); |
353 | } | |
354 | else if (old && (p->debug & D_ROUTES)) | |
355 | { | |
356 | if (old != old_best) | |
357 | rte_trace_in(D_ROUTES, p, old, "removed"); | |
358 | else if (net->routes) | |
359 | rte_trace_in(D_ROUTES, p, old, "removed [replaced]"); | |
360 | else | |
361 | rte_trace_in(D_ROUTES, p, old, "removed [sole]"); | |
2326b001 MM |
362 | } |
363 | } | |
364 | if (old) | |
5b22683d MM |
365 | { |
366 | if (p->rte_remove) | |
367 | p->rte_remove(net, old); | |
04925e90 | 368 | rte_free_quick(old); |
5b22683d | 369 | } |
8ca8683c MM |
370 | if (new) |
371 | { | |
372 | new->lastmod = now; | |
373 | if (p->rte_insert) | |
374 | p->rte_insert(net, new); | |
375 | } | |
5b22683d MM |
376 | } |
377 | ||
e2dc2f30 MM |
378 | static int rte_update_nest_cnt; /* Nesting counter to allow recursive updates */ |
379 | ||
380 | static inline void | |
381 | rte_update_lock(void) | |
382 | { | |
383 | rte_update_nest_cnt++; | |
384 | } | |
385 | ||
386 | static inline void | |
387 | rte_update_unlock(void) | |
388 | { | |
389 | if (!--rte_update_nest_cnt) | |
390 | lp_flush(rte_update_pool); | |
391 | } | |
392 | ||
393 | void | |
0e02abfd | 394 | rte_update(rtable *table, net *net, struct proto *p, rte *new) |
e2dc2f30 MM |
395 | { |
396 | ea_list *tmpa = NULL; | |
397 | ||
398 | rte_update_lock(); | |
399 | if (new) | |
400 | { | |
cfd46ee4 MM |
401 | if (!rte_validate(new)) |
402 | { | |
403 | rte_trace_in(D_FILTERS, p, new, "invalid"); | |
404 | goto drop; | |
405 | } | |
406 | if (p->in_filter == FILTER_REJECT) | |
407 | { | |
408 | rte_trace_in(D_FILTERS, p, new, "filtered out"); | |
409 | goto drop; | |
410 | } | |
e2dc2f30 MM |
411 | if (p->make_tmp_attrs) |
412 | tmpa = p->make_tmp_attrs(new, rte_update_pool); | |
413 | if (p->in_filter) | |
414 | { | |
ccdc3397 | 415 | ea_list *old_tmpa = tmpa; |
0a06a9b8 | 416 | int fr = f_run(p->in_filter, &new, &tmpa, rte_update_pool, 0); |
ccdc3397 | 417 | if (fr > F_ACCEPT) |
cfd46ee4 MM |
418 | { |
419 | rte_trace_in(D_FILTERS, p, new, "filtered out"); | |
420 | goto drop; | |
421 | } | |
ccdc3397 | 422 | if (tmpa != old_tmpa && p->store_tmp_attrs) |
e2dc2f30 MM |
423 | p->store_tmp_attrs(new, tmpa); |
424 | } | |
425 | if (!(new->attrs->aflags & RTAF_CACHED)) /* Need to copy attributes */ | |
426 | new->attrs = rta_lookup(new->attrs); | |
427 | new->flags |= REF_COW; | |
428 | } | |
0e02abfd | 429 | rte_recalculate(table, net, p, new, tmpa); |
e2dc2f30 MM |
430 | rte_update_unlock(); |
431 | return; | |
432 | ||
433 | drop: | |
434 | rte_free(new); | |
435 | rte_update_unlock(); | |
436 | } | |
437 | ||
5b22683d | 438 | void |
0e02abfd | 439 | rte_discard(rtable *t, rte *old) /* Non-filtered route deletion, used during garbage collection */ |
5b22683d | 440 | { |
cfd46ee4 MM |
441 | net *n = old->net; |
442 | struct proto *p = old->attrs->proto; | |
443 | ||
e2dc2f30 | 444 | rte_update_lock(); |
cfd46ee4 | 445 | rte_recalculate(t, n, p, NULL, NULL); |
e2dc2f30 | 446 | rte_update_unlock(); |
2326b001 MM |
447 | } |
448 | ||
449 | void | |
a0762910 | 450 | rte_dump(rte *e) |
2326b001 | 451 | { |
a0762910 | 452 | net *n = e->net; |
0cdbd397 | 453 | if (n) |
962ba482 | 454 | debug("%1I/%2d ", n->n.prefix, n->n.pxlen); |
a0762910 MM |
455 | else |
456 | debug("??? "); | |
c10421d3 | 457 | debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod); |
0cdbd397 | 458 | rta_dump(e->attrs); |
c10421d3 MM |
459 | if (e->attrs->proto->proto->dump_attrs) |
460 | e->attrs->proto->proto->dump_attrs(e); | |
0cdbd397 | 461 | debug("\n"); |
2326b001 | 462 | } |
62aa008a | 463 | |
2326b001 MM |
464 | void |
465 | rt_dump(rtable *t) | |
466 | { | |
0cdbd397 MM |
467 | rte *e; |
468 | net *n; | |
0e02abfd | 469 | struct announce_hook *a; |
0cdbd397 MM |
470 | |
471 | debug("Dump of routing table <%s>\n", t->name); | |
e440395d | 472 | #ifdef DEBUGGING |
08e2d625 | 473 | fib_check(&t->fib); |
e440395d | 474 | #endif |
08e2d625 MM |
475 | FIB_WALK(&t->fib, fn) |
476 | { | |
477 | n = (net *) fn; | |
478 | for(e=n->routes; e; e=e->next) | |
479 | rte_dump(e); | |
0cdbd397 | 480 | } |
08e2d625 | 481 | FIB_WALK_END; |
0e02abfd MM |
482 | WALK_LIST(a, t->hooks) |
483 | debug("\tAnnounces routes to protocol %s\n", a->proto->name); | |
0cdbd397 | 484 | debug("\n"); |
2326b001 | 485 | } |
62aa008a | 486 | |
6d45cf21 MM |
487 | void |
488 | rt_dump_all(void) | |
489 | { | |
0e02abfd MM |
490 | rtable *t; |
491 | ||
492 | WALK_LIST(t, routing_tables) | |
493 | rt_dump(t); | |
6d45cf21 MM |
494 | } |
495 | ||
8f6accb5 | 496 | static void |
b9626ec6 | 497 | rt_gc(void *tab) |
5996da6a | 498 | { |
b9626ec6 | 499 | rtable *t = tab; |
0e02abfd | 500 | |
b9626ec6 MM |
501 | DBG("Entered routing table garbage collector for %s after %d seconds and %d deletes\n", |
502 | t->name, (int)(now - t->gc_time), t->gc_counter); | |
503 | rt_prune(t); | |
5996da6a MM |
504 | } |
505 | ||
b9626ec6 MM |
506 | void |
507 | rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf) | |
508 | { | |
509 | bzero(t, sizeof(*t)); | |
510 | fib_init(&t->fib, p, sizeof(net), 0, rte_init); | |
511 | t->name = name; | |
512 | t->config = cf; | |
513 | init_list(&t->hooks); | |
514 | if (cf) | |
515 | { | |
516 | t->gc_event = ev_new(p); | |
517 | t->gc_event->hook = rt_gc; | |
518 | t->gc_event->data = t; | |
519 | } | |
520 | } | |
521 | ||
2326b001 MM |
522 | void |
523 | rt_init(void) | |
524 | { | |
525 | rta_init(); | |
5996da6a | 526 | rt_table_pool = rp_new(&root_pool, "Routing tables"); |
e2dc2f30 | 527 | rte_update_pool = lp_new(rt_table_pool, 4080); |
5996da6a | 528 | rte_slab = sl_new(rt_table_pool, sizeof(rte)); |
0e02abfd | 529 | init_list(&routing_tables); |
2326b001 | 530 | } |
1a54b1c6 MM |
531 | |
532 | void | |
533 | rt_prune(rtable *tab) | |
534 | { | |
535 | struct fib_iterator fit; | |
5996da6a | 536 | int rcnt = 0, rdel = 0, ncnt = 0, ndel = 0; |
1a54b1c6 MM |
537 | |
538 | DBG("Pruning route table %s\n", tab->name); | |
08e2d625 MM |
539 | FIB_ITERATE_INIT(&fit, &tab->fib); |
540 | again: | |
541 | FIB_ITERATE_START(&tab->fib, &fit, f) | |
1a54b1c6 | 542 | { |
08e2d625 MM |
543 | net *n = (net *) f; |
544 | rte *e; | |
545 | ncnt++; | |
546 | rescan: | |
547 | for (e=n->routes; e; e=e->next, rcnt++) | |
548 | if (e->attrs->proto->core_state != FS_HAPPY) | |
549 | { | |
0e02abfd | 550 | rte_discard(tab, e); |
08e2d625 MM |
551 | rdel++; |
552 | goto rescan; | |
553 | } | |
554 | if (!n->routes) /* Orphaned FIB entry? */ | |
1a54b1c6 | 555 | { |
08e2d625 MM |
556 | FIB_ITERATE_PUT(&fit, f); |
557 | fib_delete(&tab->fib, f); | |
558 | ndel++; | |
559 | goto again; | |
1a54b1c6 | 560 | } |
1a54b1c6 | 561 | } |
08e2d625 | 562 | FIB_ITERATE_END(f); |
5996da6a | 563 | DBG("Pruned %d of %d routes and %d of %d networks\n", rcnt, rdel, ncnt, ndel); |
b9626ec6 MM |
564 | tab->gc_counter = 0; |
565 | tab->gc_time = now; | |
1a54b1c6 | 566 | } |
0e02abfd MM |
567 | |
568 | void | |
569 | rt_prune_all(void) | |
570 | { | |
571 | rtable *t; | |
572 | ||
573 | WALK_LIST(t, routing_tables) | |
574 | rt_prune(t); | |
575 | } | |
576 | ||
b9626ec6 MM |
577 | struct rtable_config * |
578 | rt_new_table(struct symbol *s) | |
579 | { | |
580 | struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config)); | |
581 | ||
582 | cf_define_symbol(s, SYM_TABLE, c); | |
583 | c->name = s->name; | |
584 | add_tail(&new_config->tables, &c->n); | |
585 | c->gc_max_ops = 100; | |
586 | c->gc_min_time = 5; | |
587 | return c; | |
588 | } | |
589 | ||
0e02abfd MM |
590 | void |
591 | rt_preconfig(struct config *c) | |
592 | { | |
593 | struct symbol *s = cf_find_symbol("master"); | |
0e02abfd | 594 | |
0e02abfd | 595 | init_list(&c->tables); |
b9626ec6 | 596 | c->master_rtc = rt_new_table(s); |
0e02abfd MM |
597 | } |
598 | ||
599 | void | |
50fe90ed | 600 | rt_lock_table(rtable *r) |
0e02abfd | 601 | { |
50fe90ed MM |
602 | r->use_count++; |
603 | } | |
604 | ||
605 | void | |
606 | rt_unlock_table(rtable *r) | |
607 | { | |
608 | if (!--r->use_count && r->deleted) | |
609 | { | |
610 | struct config *conf = r->deleted; | |
611 | DBG("Deleting routing table %s\n", r->name); | |
612 | rem_node(&r->n); | |
613 | fib_free(&r->fib); | |
614 | mb_free(r); | |
615 | config_del_obstacle(conf); | |
616 | } | |
617 | } | |
618 | ||
619 | void | |
620 | rt_commit(struct config *new, struct config *old) | |
621 | { | |
622 | struct rtable_config *o, *r; | |
0e02abfd | 623 | |
50fe90ed MM |
624 | DBG("rt_commit:\n"); |
625 | if (old) | |
0e02abfd | 626 | { |
50fe90ed MM |
627 | WALK_LIST(o, old->tables) |
628 | { | |
629 | rtable *ot = o->table; | |
630 | if (!ot->deleted) | |
631 | { | |
632 | struct symbol *sym = cf_find_symbol(o->name); | |
bf8558bc | 633 | if (sym && sym->class == SYM_TABLE && !new->shutdown) |
50fe90ed MM |
634 | { |
635 | DBG("\t%s: same\n", o->name); | |
636 | r = sym->def; | |
637 | r->table = ot; | |
638 | ot->name = r->name; | |
b9626ec6 | 639 | ot->config = r; |
50fe90ed MM |
640 | } |
641 | else | |
642 | { | |
bf8558bc | 643 | DBG("\t%s: deleted\n", o->name); |
50fe90ed MM |
644 | ot->deleted = old; |
645 | config_add_obstacle(old); | |
646 | rt_lock_table(ot); | |
647 | rt_unlock_table(ot); | |
648 | } | |
649 | } | |
650 | } | |
0e02abfd | 651 | } |
50fe90ed MM |
652 | |
653 | WALK_LIST(r, new->tables) | |
654 | if (!r->table) | |
655 | { | |
656 | rtable *t = mb_alloc(rt_table_pool, sizeof(struct rtable)); | |
657 | DBG("\t%s: created\n", r->name); | |
b9626ec6 | 658 | rt_setup(rt_table_pool, t, r->name, r); |
50fe90ed MM |
659 | add_tail(&routing_tables, &t->n); |
660 | r->table = t; | |
661 | } | |
662 | DBG("\tdone\n"); | |
0e02abfd | 663 | } |
730f2e2c MM |
664 | |
665 | /* | |
666 | * CLI commands | |
667 | */ | |
668 | ||
669 | static void | |
cfd46ee4 | 670 | rt_format_via(rte *e, byte *via) |
730f2e2c | 671 | { |
730f2e2c MM |
672 | rta *a = e->attrs; |
673 | ||
674 | switch (a->dest) | |
675 | { | |
676 | case RTD_ROUTER: bsprintf(via, "via %I on %s", a->gw, a->iface->name); break; | |
677 | case RTD_DEVICE: bsprintf(via, "dev %s", a->iface->name); break; | |
678 | case RTD_BLACKHOLE: bsprintf(via, "blackhole"); break; | |
679 | case RTD_UNREACHABLE: bsprintf(via, "unreachable"); break; | |
680 | case RTD_PROHIBIT: bsprintf(via, "prohibited"); break; | |
681 | default: bsprintf(via, "???"); | |
682 | } | |
cfd46ee4 MM |
683 | } |
684 | ||
685 | static void | |
ce1da96e | 686 | rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa) |
cfd46ee4 | 687 | { |
85810613 | 688 | byte via[STD_ADDRESS_P_LENGTH+32], from[STD_ADDRESS_P_LENGTH+6]; |
cfd46ee4 MM |
689 | byte tm[TM_RELTIME_BUFFER_SIZE], info[256]; |
690 | rta *a = e->attrs; | |
691 | ||
692 | rt_format_via(e, via); | |
730f2e2c MM |
693 | tm_format_reltime(tm, e->lastmod); |
694 | if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->gw)) | |
695 | bsprintf(from, " from %I", a->from); | |
696 | else | |
697 | from[0] = 0; | |
ce1da96e MM |
698 | if (a->proto->proto->get_route_info || d->verbose) |
699 | { | |
700 | /* Need to normalize the extended attributes */ | |
701 | ea_list *t = tmpa; | |
702 | t = ea_append(t, a->eattrs); | |
703 | tmpa = alloca(ea_scan(t)); | |
704 | ea_merge(t, tmpa); | |
705 | } | |
730f2e2c | 706 | if (a->proto->proto->get_route_info) |
ce1da96e | 707 | a->proto->proto->get_route_info(e, info, tmpa); |
730f2e2c MM |
708 | else |
709 | bsprintf(info, " (%d)", e->pref); | |
710 | cli_printf(c, -1007, "%-18s %s [%s %s%s]%s", ia, via, a->proto->name, tm, from, info); | |
711 | if (d->verbose) | |
ce1da96e | 712 | rta_show(c, a, tmpa); |
730f2e2c MM |
713 | } |
714 | ||
715 | static void | |
716 | rt_show_net(struct cli *c, net *n, struct rt_show_data *d) | |
717 | { | |
718 | rte *e, *ee; | |
719 | byte ia[STD_ADDRESS_P_LENGTH+8]; | |
ce1da96e | 720 | int ok; |
730f2e2c MM |
721 | |
722 | bsprintf(ia, "%I/%d", n->n.prefix, n->n.pxlen); | |
23693958 | 723 | d->net_counter++; |
730f2e2c MM |
724 | for(e=n->routes; e; e=e->next) |
725 | { | |
ce1da96e MM |
726 | struct ea_list *tmpa, *old_tmpa; |
727 | struct proto *p0 = e->attrs->proto; | |
728 | struct proto *p1 = d->import_protocol; | |
23693958 | 729 | d->rt_counter++; |
730f2e2c MM |
730 | ee = e; |
731 | rte_update_lock(); /* We use the update buffer for filtering */ | |
ce1da96e MM |
732 | old_tmpa = tmpa = p0->make_tmp_attrs ? p0->make_tmp_attrs(e, rte_update_pool) : NULL; |
733 | ok = (d->filter == FILTER_ACCEPT || f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT); | |
734 | if (ok && d->import_mode) | |
735 | { | |
736 | int ic = (p1->import_control ? p1->import_control(p1, &e, &tmpa, rte_update_pool) : 0); | |
737 | if (ic < 0) | |
738 | ok = 0; | |
739 | else if (!ic && d->import_mode > 1) | |
740 | { | |
741 | if (p1->out_filter == FILTER_REJECT || | |
742 | p1->out_filter && f_run(p1->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT) | |
743 | ok = 0; | |
744 | } | |
745 | } | |
746 | if (ok) | |
730f2e2c | 747 | { |
23693958 | 748 | d->show_counter++; |
ce1da96e | 749 | rt_show_rte(c, ia, e, d, tmpa); |
730f2e2c MM |
750 | ia[0] = 0; |
751 | } | |
752 | if (e != ee) | |
753 | rte_free(ee); | |
754 | rte_update_unlock(); | |
ce1da96e MM |
755 | if (d->import_mode) /* In import mode, accept only the primary route */ |
756 | break; | |
730f2e2c MM |
757 | } |
758 | } | |
759 | ||
760 | static void | |
761 | rt_show_cont(struct cli *c) | |
762 | { | |
763 | struct rt_show_data *d = c->rover; | |
f098e072 MM |
764 | #ifdef DEBUGGING |
765 | unsigned max = 4; | |
766 | #else | |
767 | unsigned max = 64; | |
768 | #endif | |
730f2e2c MM |
769 | struct fib *fib = &d->table->fib; |
770 | struct fib_iterator *it = &d->fit; | |
771 | ||
772 | FIB_ITERATE_START(fib, it, f) | |
773 | { | |
774 | net *n = (net *) f; | |
ce1da96e MM |
775 | if (d->running_on_config && d->running_on_config != config) |
776 | { | |
777 | cli_printf(c, 8004, "Stopped due to reconfiguration"); | |
778 | goto done; | |
779 | } | |
780 | if (d->import_protocol && | |
781 | d->import_protocol->core_state != FS_HAPPY && | |
782 | d->import_protocol->core_state != FS_FEEDING) | |
783 | { | |
784 | cli_printf(c, 8005, "Protocol is down"); | |
785 | goto done; | |
786 | } | |
730f2e2c MM |
787 | if (!max--) |
788 | { | |
789 | FIB_ITERATE_PUT(it, f); | |
790 | return; | |
791 | } | |
792 | rt_show_net(c, n, d); | |
793 | } | |
794 | FIB_ITERATE_END(f); | |
23693958 MM |
795 | if (d->stats) |
796 | cli_printf(c, 14, "%d of %d routes for %d networks", d->show_counter, d->rt_counter, d->net_counter); | |
797 | else | |
798 | cli_printf(c, 0, ""); | |
ce1da96e | 799 | done: |
730f2e2c MM |
800 | c->cont = c->cleanup = NULL; |
801 | } | |
802 | ||
803 | static void | |
804 | rt_show_cleanup(struct cli *c) | |
805 | { | |
806 | struct rt_show_data *d = c->rover; | |
807 | ||
808 | /* Unlink the iterator */ | |
809 | fit_get(&d->table->fib, &d->fit); | |
810 | } | |
811 | ||
812 | void | |
813 | rt_show(struct rt_show_data *d) | |
814 | { | |
730f2e2c MM |
815 | net *n; |
816 | ||
817 | if (d->pxlen == 256) | |
818 | { | |
819 | FIB_ITERATE_INIT(&d->fit, &d->table->fib); | |
820 | this_cli->cont = rt_show_cont; | |
821 | this_cli->cleanup = rt_show_cleanup; | |
822 | this_cli->rover = d; | |
823 | } | |
824 | else | |
825 | { | |
826 | n = fib_find(&d->table->fib, &d->prefix, d->pxlen); | |
827 | if (n) | |
828 | { | |
829 | rt_show_net(this_cli, n, d); | |
830 | cli_msg(0, ""); | |
831 | } | |
832 | else | |
833 | cli_msg(8001, "Network not in table"); | |
834 | } | |
835 | } |