Blackfin arch: Advertise GENERIC_GPIO and remove duplicated GENERIC_CALIBRATE_DELAY
[wrt350n-kernel.git] / net / ipv4 / fib_hash.c
blob9ad1d9ff9ce777232b69ad84d083fa3ba3b0b4c9
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IPv4 FIB: lookup engine and maintenance routines.
8 * Version: $Id: fib_hash.c,v 1.13 2001/10/31 21:55:54 davem Exp $
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <asm/uaccess.h>
19 #include <asm/system.h>
20 #include <linux/bitops.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/errno.h>
28 #include <linux/in.h>
29 #include <linux/inet.h>
30 #include <linux/inetdevice.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
33 #include <linux/proc_fs.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
36 #include <linux/init.h>
38 #include <net/ip.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
41 #include <net/tcp.h>
42 #include <net/sock.h>
43 #include <net/ip_fib.h>
45 #include "fib_lookup.h"
47 static struct kmem_cache *fn_hash_kmem __read_mostly;
48 static struct kmem_cache *fn_alias_kmem __read_mostly;
50 struct fib_node {
51 struct hlist_node fn_hash;
52 struct list_head fn_alias;
53 __be32 fn_key;
56 struct fn_zone {
57 struct fn_zone *fz_next; /* Next not empty zone */
58 struct hlist_head *fz_hash; /* Hash table pointer */
59 int fz_nent; /* Number of entries */
61 int fz_divisor; /* Hash divisor */
62 u32 fz_hashmask; /* (fz_divisor - 1) */
63 #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
65 int fz_order; /* Zone order */
66 __be32 fz_mask;
67 #define FZ_MASK(fz) ((fz)->fz_mask)
70 /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
71 * can be cheaper than memory lookup, so that FZ_* macros are used.
74 struct fn_hash {
75 struct fn_zone *fn_zones[33];
76 struct fn_zone *fn_zone_list;
79 static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
81 u32 h = ntohl(key)>>(32 - fz->fz_order);
82 h ^= (h>>20);
83 h ^= (h>>10);
84 h ^= (h>>5);
85 h &= FZ_HASHMASK(fz);
86 return h;
89 static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
91 return dst & FZ_MASK(fz);
94 static DEFINE_RWLOCK(fib_hash_lock);
95 static unsigned int fib_hash_genid;
97 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
99 static struct hlist_head *fz_hash_alloc(int divisor)
101 unsigned long size = divisor * sizeof(struct hlist_head);
103 if (size <= PAGE_SIZE) {
104 return kmalloc(size, GFP_KERNEL);
105 } else {
106 return (struct hlist_head *)
107 __get_free_pages(GFP_KERNEL, get_order(size));
111 /* The fib hash lock must be held when this is called. */
112 static inline void fn_rebuild_zone(struct fn_zone *fz,
113 struct hlist_head *old_ht,
114 int old_divisor)
116 int i;
118 for (i = 0; i < old_divisor; i++) {
119 struct hlist_node *node, *n;
120 struct fib_node *f;
122 hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
123 struct hlist_head *new_head;
125 hlist_del(&f->fn_hash);
127 new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
128 hlist_add_head(&f->fn_hash, new_head);
133 static void fz_hash_free(struct hlist_head *hash, int divisor)
135 unsigned long size = divisor * sizeof(struct hlist_head);
137 if (size <= PAGE_SIZE)
138 kfree(hash);
139 else
140 free_pages((unsigned long)hash, get_order(size));
143 static void fn_rehash_zone(struct fn_zone *fz)
145 struct hlist_head *ht, *old_ht;
146 int old_divisor, new_divisor;
147 u32 new_hashmask;
149 old_divisor = fz->fz_divisor;
151 switch (old_divisor) {
152 case 16:
153 new_divisor = 256;
154 break;
155 case 256:
156 new_divisor = 1024;
157 break;
158 default:
159 if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
160 printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
161 return;
163 new_divisor = (old_divisor << 1);
164 break;
167 new_hashmask = (new_divisor - 1);
169 #if RT_CACHE_DEBUG >= 2
170 printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
171 #endif
173 ht = fz_hash_alloc(new_divisor);
175 if (ht) {
176 memset(ht, 0, new_divisor * sizeof(struct hlist_head));
178 write_lock_bh(&fib_hash_lock);
179 old_ht = fz->fz_hash;
180 fz->fz_hash = ht;
181 fz->fz_hashmask = new_hashmask;
182 fz->fz_divisor = new_divisor;
183 fn_rebuild_zone(fz, old_ht, old_divisor);
184 fib_hash_genid++;
185 write_unlock_bh(&fib_hash_lock);
187 fz_hash_free(old_ht, old_divisor);
191 static inline void fn_free_node(struct fib_node * f)
193 kmem_cache_free(fn_hash_kmem, f);
196 static inline void fn_free_alias(struct fib_alias *fa)
198 fib_release_info(fa->fa_info);
199 kmem_cache_free(fn_alias_kmem, fa);
202 static struct fn_zone *
203 fn_new_zone(struct fn_hash *table, int z)
205 int i;
206 struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
207 if (!fz)
208 return NULL;
210 if (z) {
211 fz->fz_divisor = 16;
212 } else {
213 fz->fz_divisor = 1;
215 fz->fz_hashmask = (fz->fz_divisor - 1);
216 fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
217 if (!fz->fz_hash) {
218 kfree(fz);
219 return NULL;
221 memset(fz->fz_hash, 0, fz->fz_divisor * sizeof(struct hlist_head *));
222 fz->fz_order = z;
223 fz->fz_mask = inet_make_mask(z);
225 /* Find the first not empty zone with more specific mask */
226 for (i=z+1; i<=32; i++)
227 if (table->fn_zones[i])
228 break;
229 write_lock_bh(&fib_hash_lock);
230 if (i>32) {
231 /* No more specific masks, we are the first. */
232 fz->fz_next = table->fn_zone_list;
233 table->fn_zone_list = fz;
234 } else {
235 fz->fz_next = table->fn_zones[i]->fz_next;
236 table->fn_zones[i]->fz_next = fz;
238 table->fn_zones[z] = fz;
239 fib_hash_genid++;
240 write_unlock_bh(&fib_hash_lock);
241 return fz;
244 static int
245 fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
247 int err;
248 struct fn_zone *fz;
249 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
251 read_lock(&fib_hash_lock);
252 for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
253 struct hlist_head *head;
254 struct hlist_node *node;
255 struct fib_node *f;
256 __be32 k = fz_key(flp->fl4_dst, fz);
258 head = &fz->fz_hash[fn_hash(k, fz)];
259 hlist_for_each_entry(f, node, head, fn_hash) {
260 if (f->fn_key != k)
261 continue;
263 err = fib_semantic_match(&f->fn_alias,
264 flp, res,
265 f->fn_key, fz->fz_mask,
266 fz->fz_order);
267 if (err <= 0)
268 goto out;
271 err = 1;
272 out:
273 read_unlock(&fib_hash_lock);
274 return err;
277 static int fn_hash_last_dflt=-1;
279 static void
280 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
282 int order, last_idx;
283 struct hlist_node *node;
284 struct fib_node *f;
285 struct fib_info *fi = NULL;
286 struct fib_info *last_resort;
287 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
288 struct fn_zone *fz = t->fn_zones[0];
290 if (fz == NULL)
291 return;
293 last_idx = -1;
294 last_resort = NULL;
295 order = -1;
297 read_lock(&fib_hash_lock);
298 hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
299 struct fib_alias *fa;
301 list_for_each_entry(fa, &f->fn_alias, fa_list) {
302 struct fib_info *next_fi = fa->fa_info;
304 if (fa->fa_scope != res->scope ||
305 fa->fa_type != RTN_UNICAST)
306 continue;
308 if (next_fi->fib_priority > res->fi->fib_priority)
309 break;
310 if (!next_fi->fib_nh[0].nh_gw ||
311 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
312 continue;
313 fa->fa_state |= FA_S_ACCESSED;
315 if (fi == NULL) {
316 if (next_fi != res->fi)
317 break;
318 } else if (!fib_detect_death(fi, order, &last_resort,
319 &last_idx, &fn_hash_last_dflt)) {
320 if (res->fi)
321 fib_info_put(res->fi);
322 res->fi = fi;
323 atomic_inc(&fi->fib_clntref);
324 fn_hash_last_dflt = order;
325 goto out;
327 fi = next_fi;
328 order++;
332 if (order <= 0 || fi == NULL) {
333 fn_hash_last_dflt = -1;
334 goto out;
337 if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
338 if (res->fi)
339 fib_info_put(res->fi);
340 res->fi = fi;
341 atomic_inc(&fi->fib_clntref);
342 fn_hash_last_dflt = order;
343 goto out;
346 if (last_idx >= 0) {
347 if (res->fi)
348 fib_info_put(res->fi);
349 res->fi = last_resort;
350 if (last_resort)
351 atomic_inc(&last_resort->fib_clntref);
353 fn_hash_last_dflt = last_idx;
354 out:
355 read_unlock(&fib_hash_lock);
358 /* Insert node F to FZ. */
359 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
361 struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
363 hlist_add_head(&f->fn_hash, head);
366 /* Return the node in FZ matching KEY. */
367 static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
369 struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
370 struct hlist_node *node;
371 struct fib_node *f;
373 hlist_for_each_entry(f, node, head, fn_hash) {
374 if (f->fn_key == key)
375 return f;
378 return NULL;
381 static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
383 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
384 struct fib_node *new_f, *f;
385 struct fib_alias *fa, *new_fa;
386 struct fn_zone *fz;
387 struct fib_info *fi;
388 u8 tos = cfg->fc_tos;
389 __be32 key;
390 int err;
392 if (cfg->fc_dst_len > 32)
393 return -EINVAL;
395 fz = table->fn_zones[cfg->fc_dst_len];
396 if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
397 return -ENOBUFS;
399 key = 0;
400 if (cfg->fc_dst) {
401 if (cfg->fc_dst & ~FZ_MASK(fz))
402 return -EINVAL;
403 key = fz_key(cfg->fc_dst, fz);
406 fi = fib_create_info(cfg);
407 if (IS_ERR(fi))
408 return PTR_ERR(fi);
410 if (fz->fz_nent > (fz->fz_divisor<<1) &&
411 fz->fz_divisor < FZ_MAX_DIVISOR &&
412 (cfg->fc_dst_len == 32 ||
413 (1 << cfg->fc_dst_len) > fz->fz_divisor))
414 fn_rehash_zone(fz);
416 f = fib_find_node(fz, key);
418 if (!f)
419 fa = NULL;
420 else
421 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
423 /* Now fa, if non-NULL, points to the first fib alias
424 * with the same keys [prefix,tos,priority], if such key already
425 * exists or to the node before which we will insert new one.
427 * If fa is NULL, we will need to allocate a new one and
428 * insert to the head of f.
430 * If f is NULL, no fib node matched the destination key
431 * and we need to allocate a new one of those as well.
434 if (fa && fa->fa_tos == tos &&
435 fa->fa_info->fib_priority == fi->fib_priority) {
436 struct fib_alias *fa_orig;
438 err = -EEXIST;
439 if (cfg->fc_nlflags & NLM_F_EXCL)
440 goto out;
442 if (cfg->fc_nlflags & NLM_F_REPLACE) {
443 struct fib_info *fi_drop;
444 u8 state;
446 write_lock_bh(&fib_hash_lock);
447 fi_drop = fa->fa_info;
448 fa->fa_info = fi;
449 fa->fa_type = cfg->fc_type;
450 fa->fa_scope = cfg->fc_scope;
451 state = fa->fa_state;
452 fa->fa_state &= ~FA_S_ACCESSED;
453 fib_hash_genid++;
454 write_unlock_bh(&fib_hash_lock);
456 fib_release_info(fi_drop);
457 if (state & FA_S_ACCESSED)
458 rt_cache_flush(-1);
459 rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
460 &cfg->fc_nlinfo, NLM_F_REPLACE);
461 return 0;
464 /* Error if we find a perfect match which
465 * uses the same scope, type, and nexthop
466 * information.
468 fa_orig = fa;
469 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
470 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
471 if (fa->fa_tos != tos)
472 break;
473 if (fa->fa_info->fib_priority != fi->fib_priority)
474 break;
475 if (fa->fa_type == cfg->fc_type &&
476 fa->fa_scope == cfg->fc_scope &&
477 fa->fa_info == fi)
478 goto out;
480 if (!(cfg->fc_nlflags & NLM_F_APPEND))
481 fa = fa_orig;
484 err = -ENOENT;
485 if (!(cfg->fc_nlflags & NLM_F_CREATE))
486 goto out;
488 err = -ENOBUFS;
489 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
490 if (new_fa == NULL)
491 goto out;
493 new_f = NULL;
494 if (!f) {
495 new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
496 if (new_f == NULL)
497 goto out_free_new_fa;
499 INIT_HLIST_NODE(&new_f->fn_hash);
500 INIT_LIST_HEAD(&new_f->fn_alias);
501 new_f->fn_key = key;
502 f = new_f;
505 new_fa->fa_info = fi;
506 new_fa->fa_tos = tos;
507 new_fa->fa_type = cfg->fc_type;
508 new_fa->fa_scope = cfg->fc_scope;
509 new_fa->fa_state = 0;
512 * Insert new entry to the list.
515 write_lock_bh(&fib_hash_lock);
516 if (new_f)
517 fib_insert_node(fz, new_f);
518 list_add_tail(&new_fa->fa_list,
519 (fa ? &fa->fa_list : &f->fn_alias));
520 fib_hash_genid++;
521 write_unlock_bh(&fib_hash_lock);
523 if (new_f)
524 fz->fz_nent++;
525 rt_cache_flush(-1);
527 rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
528 &cfg->fc_nlinfo, 0);
529 return 0;
531 out_free_new_fa:
532 kmem_cache_free(fn_alias_kmem, new_fa);
533 out:
534 fib_release_info(fi);
535 return err;
539 static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
541 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
542 struct fib_node *f;
543 struct fib_alias *fa, *fa_to_delete;
544 struct fn_zone *fz;
545 __be32 key;
547 if (cfg->fc_dst_len > 32)
548 return -EINVAL;
550 if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
551 return -ESRCH;
553 key = 0;
554 if (cfg->fc_dst) {
555 if (cfg->fc_dst & ~FZ_MASK(fz))
556 return -EINVAL;
557 key = fz_key(cfg->fc_dst, fz);
560 f = fib_find_node(fz, key);
562 if (!f)
563 fa = NULL;
564 else
565 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
566 if (!fa)
567 return -ESRCH;
569 fa_to_delete = NULL;
570 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
571 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
572 struct fib_info *fi = fa->fa_info;
574 if (fa->fa_tos != cfg->fc_tos)
575 break;
577 if ((!cfg->fc_type ||
578 fa->fa_type == cfg->fc_type) &&
579 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
580 fa->fa_scope == cfg->fc_scope) &&
581 (!cfg->fc_protocol ||
582 fi->fib_protocol == cfg->fc_protocol) &&
583 fib_nh_match(cfg, fi) == 0) {
584 fa_to_delete = fa;
585 break;
589 if (fa_to_delete) {
590 int kill_fn;
592 fa = fa_to_delete;
593 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
594 tb->tb_id, &cfg->fc_nlinfo, 0);
596 kill_fn = 0;
597 write_lock_bh(&fib_hash_lock);
598 list_del(&fa->fa_list);
599 if (list_empty(&f->fn_alias)) {
600 hlist_del(&f->fn_hash);
601 kill_fn = 1;
603 fib_hash_genid++;
604 write_unlock_bh(&fib_hash_lock);
606 if (fa->fa_state & FA_S_ACCESSED)
607 rt_cache_flush(-1);
608 fn_free_alias(fa);
609 if (kill_fn) {
610 fn_free_node(f);
611 fz->fz_nent--;
614 return 0;
616 return -ESRCH;
619 static int fn_flush_list(struct fn_zone *fz, int idx)
621 struct hlist_head *head = &fz->fz_hash[idx];
622 struct hlist_node *node, *n;
623 struct fib_node *f;
624 int found = 0;
626 hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
627 struct fib_alias *fa, *fa_node;
628 int kill_f;
630 kill_f = 0;
631 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
632 struct fib_info *fi = fa->fa_info;
634 if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
635 write_lock_bh(&fib_hash_lock);
636 list_del(&fa->fa_list);
637 if (list_empty(&f->fn_alias)) {
638 hlist_del(&f->fn_hash);
639 kill_f = 1;
641 fib_hash_genid++;
642 write_unlock_bh(&fib_hash_lock);
644 fn_free_alias(fa);
645 found++;
648 if (kill_f) {
649 fn_free_node(f);
650 fz->fz_nent--;
653 return found;
656 static int fn_hash_flush(struct fib_table *tb)
658 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
659 struct fn_zone *fz;
660 int found = 0;
662 for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
663 int i;
665 for (i = fz->fz_divisor - 1; i >= 0; i--)
666 found += fn_flush_list(fz, i);
668 return found;
672 static inline int
673 fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
674 struct fib_table *tb,
675 struct fn_zone *fz,
676 struct hlist_head *head)
678 struct hlist_node *node;
679 struct fib_node *f;
680 int i, s_i;
682 s_i = cb->args[4];
683 i = 0;
684 hlist_for_each_entry(f, node, head, fn_hash) {
685 struct fib_alias *fa;
687 list_for_each_entry(fa, &f->fn_alias, fa_list) {
688 if (i < s_i)
689 goto next;
691 if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
692 cb->nlh->nlmsg_seq,
693 RTM_NEWROUTE,
694 tb->tb_id,
695 fa->fa_type,
696 fa->fa_scope,
697 f->fn_key,
698 fz->fz_order,
699 fa->fa_tos,
700 fa->fa_info,
701 NLM_F_MULTI) < 0) {
702 cb->args[4] = i;
703 return -1;
705 next:
706 i++;
709 cb->args[4] = i;
710 return skb->len;
713 static inline int
714 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
715 struct fib_table *tb,
716 struct fn_zone *fz)
718 int h, s_h;
720 s_h = cb->args[3];
721 for (h=0; h < fz->fz_divisor; h++) {
722 if (h < s_h) continue;
723 if (h > s_h)
724 memset(&cb->args[4], 0,
725 sizeof(cb->args) - 4*sizeof(cb->args[0]));
726 if (fz->fz_hash == NULL ||
727 hlist_empty(&fz->fz_hash[h]))
728 continue;
729 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
730 cb->args[3] = h;
731 return -1;
734 cb->args[3] = h;
735 return skb->len;
738 static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
740 int m, s_m;
741 struct fn_zone *fz;
742 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
744 s_m = cb->args[2];
745 read_lock(&fib_hash_lock);
746 for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
747 if (m < s_m) continue;
748 if (m > s_m)
749 memset(&cb->args[3], 0,
750 sizeof(cb->args) - 3*sizeof(cb->args[0]));
751 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
752 cb->args[2] = m;
753 read_unlock(&fib_hash_lock);
754 return -1;
757 read_unlock(&fib_hash_lock);
758 cb->args[2] = m;
759 return skb->len;
762 #ifdef CONFIG_IP_MULTIPLE_TABLES
763 struct fib_table * fib_hash_init(u32 id)
764 #else
765 struct fib_table * __init fib_hash_init(u32 id)
766 #endif
768 struct fib_table *tb;
770 if (fn_hash_kmem == NULL)
771 fn_hash_kmem = kmem_cache_create("ip_fib_hash",
772 sizeof(struct fib_node),
773 0, SLAB_HWCACHE_ALIGN,
774 NULL);
776 if (fn_alias_kmem == NULL)
777 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
778 sizeof(struct fib_alias),
779 0, SLAB_HWCACHE_ALIGN,
780 NULL);
782 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
783 GFP_KERNEL);
784 if (tb == NULL)
785 return NULL;
787 tb->tb_id = id;
788 tb->tb_lookup = fn_hash_lookup;
789 tb->tb_insert = fn_hash_insert;
790 tb->tb_delete = fn_hash_delete;
791 tb->tb_flush = fn_hash_flush;
792 tb->tb_select_default = fn_hash_select_default;
793 tb->tb_dump = fn_hash_dump;
794 memset(tb->tb_data, 0, sizeof(struct fn_hash));
795 return tb;
798 /* ------------------------------------------------------------------------ */
799 #ifdef CONFIG_PROC_FS
801 struct fib_iter_state {
802 struct fn_zone *zone;
803 int bucket;
804 struct hlist_head *hash_head;
805 struct fib_node *fn;
806 struct fib_alias *fa;
807 loff_t pos;
808 unsigned int genid;
809 int valid;
812 static struct fib_alias *fib_get_first(struct seq_file *seq)
814 struct fib_iter_state *iter = seq->private;
815 struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
817 iter->bucket = 0;
818 iter->hash_head = NULL;
819 iter->fn = NULL;
820 iter->fa = NULL;
821 iter->pos = 0;
822 iter->genid = fib_hash_genid;
823 iter->valid = 1;
825 for (iter->zone = table->fn_zone_list; iter->zone;
826 iter->zone = iter->zone->fz_next) {
827 int maxslot;
829 if (!iter->zone->fz_nent)
830 continue;
832 iter->hash_head = iter->zone->fz_hash;
833 maxslot = iter->zone->fz_divisor;
835 for (iter->bucket = 0; iter->bucket < maxslot;
836 ++iter->bucket, ++iter->hash_head) {
837 struct hlist_node *node;
838 struct fib_node *fn;
840 hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
841 struct fib_alias *fa;
843 list_for_each_entry(fa,&fn->fn_alias,fa_list) {
844 iter->fn = fn;
845 iter->fa = fa;
846 goto out;
851 out:
852 return iter->fa;
855 static struct fib_alias *fib_get_next(struct seq_file *seq)
857 struct fib_iter_state *iter = seq->private;
858 struct fib_node *fn;
859 struct fib_alias *fa;
861 /* Advance FA, if any. */
862 fn = iter->fn;
863 fa = iter->fa;
864 if (fa) {
865 BUG_ON(!fn);
866 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
867 iter->fa = fa;
868 goto out;
872 fa = iter->fa = NULL;
874 /* Advance FN. */
875 if (fn) {
876 struct hlist_node *node = &fn->fn_hash;
877 hlist_for_each_entry_continue(fn, node, fn_hash) {
878 iter->fn = fn;
880 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
881 iter->fa = fa;
882 goto out;
887 fn = iter->fn = NULL;
889 /* Advance hash chain. */
890 if (!iter->zone)
891 goto out;
893 for (;;) {
894 struct hlist_node *node;
895 int maxslot;
897 maxslot = iter->zone->fz_divisor;
899 while (++iter->bucket < maxslot) {
900 iter->hash_head++;
902 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
903 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
904 iter->fn = fn;
905 iter->fa = fa;
906 goto out;
911 iter->zone = iter->zone->fz_next;
913 if (!iter->zone)
914 goto out;
916 iter->bucket = 0;
917 iter->hash_head = iter->zone->fz_hash;
919 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
920 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
921 iter->fn = fn;
922 iter->fa = fa;
923 goto out;
927 out:
928 iter->pos++;
929 return fa;
932 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
934 struct fib_iter_state *iter = seq->private;
935 struct fib_alias *fa;
937 if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
938 fa = iter->fa;
939 pos -= iter->pos;
940 } else
941 fa = fib_get_first(seq);
943 if (fa)
944 while (pos && (fa = fib_get_next(seq)))
945 --pos;
946 return pos ? NULL : fa;
949 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
951 void *v = NULL;
953 read_lock(&fib_hash_lock);
954 if (ip_fib_main_table)
955 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
956 return v;
959 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
961 ++*pos;
962 return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
965 static void fib_seq_stop(struct seq_file *seq, void *v)
967 read_unlock(&fib_hash_lock);
970 static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
972 static const unsigned type2flags[RTN_MAX + 1] = {
973 [7] = RTF_REJECT, [8] = RTF_REJECT,
975 unsigned flags = type2flags[type];
977 if (fi && fi->fib_nh->nh_gw)
978 flags |= RTF_GATEWAY;
979 if (mask == htonl(0xFFFFFFFF))
980 flags |= RTF_HOST;
981 flags |= RTF_UP;
982 return flags;
986 * This outputs /proc/net/route.
988 * It always works in backward compatibility mode.
989 * The format of the file is not supposed to be changed.
991 static int fib_seq_show(struct seq_file *seq, void *v)
993 struct fib_iter_state *iter;
994 char bf[128];
995 __be32 prefix, mask;
996 unsigned flags;
997 struct fib_node *f;
998 struct fib_alias *fa;
999 struct fib_info *fi;
1001 if (v == SEQ_START_TOKEN) {
1002 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
1003 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1004 "\tWindow\tIRTT");
1005 goto out;
1008 iter = seq->private;
1009 f = iter->fn;
1010 fa = iter->fa;
1011 fi = fa->fa_info;
1012 prefix = f->fn_key;
1013 mask = FZ_MASK(iter->zone);
1014 flags = fib_flag_trans(fa->fa_type, mask, fi);
1015 if (fi)
1016 snprintf(bf, sizeof(bf),
1017 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1018 fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1019 fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1020 mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1021 fi->fib_window,
1022 fi->fib_rtt >> 3);
1023 else
1024 snprintf(bf, sizeof(bf),
1025 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1026 prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
1027 seq_printf(seq, "%-127s\n", bf);
1028 out:
1029 return 0;
1032 static const struct seq_operations fib_seq_ops = {
1033 .start = fib_seq_start,
1034 .next = fib_seq_next,
1035 .stop = fib_seq_stop,
1036 .show = fib_seq_show,
1039 static int fib_seq_open(struct inode *inode, struct file *file)
1041 struct seq_file *seq;
1042 int rc = -ENOMEM;
1043 struct fib_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
1045 if (!s)
1046 goto out;
1048 rc = seq_open(file, &fib_seq_ops);
1049 if (rc)
1050 goto out_kfree;
1052 seq = file->private_data;
1053 seq->private = s;
1054 out:
1055 return rc;
1056 out_kfree:
1057 kfree(s);
1058 goto out;
1061 static const struct file_operations fib_seq_fops = {
1062 .owner = THIS_MODULE,
1063 .open = fib_seq_open,
1064 .read = seq_read,
1065 .llseek = seq_lseek,
1066 .release = seq_release_private,
1069 int __init fib_proc_init(void)
1071 if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
1072 return -ENOMEM;
1073 return 0;
1076 void __init fib_proc_exit(void)
1078 proc_net_remove("route");
1080 #endif /* CONFIG_PROC_FS */