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>
24 #include <linux/string.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/errno.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/net_namespace.h>
40 #include <net/protocol.h>
41 #include <net/route.h>
44 #include <net/ip_fib.h>
46 #include "fib_lookup.h"
48 static struct kmem_cache *fn_hash_kmem __read_mostly;
49 static struct kmem_cache *fn_alias_kmem __read_mostly;
52 struct hlist_node fn_hash;
53 struct list_head fn_alias;
58 struct fn_zone *fz_next; /* Next not empty zone */
59 struct hlist_head *fz_hash; /* Hash table pointer */
60 int fz_nent; /* Number of entries */
62 int fz_divisor; /* Hash divisor */
63 u32 fz_hashmask; /* (fz_divisor - 1) */
64 #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
66 int fz_order; /* Zone order */
68 #define FZ_MASK(fz) ((fz)->fz_mask)
71 /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
72 * can be cheaper than memory lookup, so that FZ_* macros are used.
76 struct fn_zone *fn_zones[33];
77 struct fn_zone *fn_zone_list;
80 static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
82 u32 h = ntohl(key)>>(32 - fz->fz_order);
90 static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
92 return dst & FZ_MASK(fz);
95 static DEFINE_RWLOCK(fib_hash_lock);
96 static unsigned int fib_hash_genid;
98 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
100 static struct hlist_head *fz_hash_alloc(int divisor)
102 unsigned long size = divisor * sizeof(struct hlist_head);
104 if (size <= PAGE_SIZE) {
105 return kzalloc(size, GFP_KERNEL);
107 return (struct hlist_head *)
108 __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
112 /* The fib hash lock must be held when this is called. */
113 static inline void fn_rebuild_zone(struct fn_zone *fz,
114 struct hlist_head *old_ht,
119 for (i = 0; i < old_divisor; i++) {
120 struct hlist_node *node, *n;
123 hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
124 struct hlist_head *new_head;
126 hlist_del(&f->fn_hash);
128 new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
129 hlist_add_head(&f->fn_hash, new_head);
134 static void fz_hash_free(struct hlist_head *hash, int divisor)
136 unsigned long size = divisor * sizeof(struct hlist_head);
138 if (size <= PAGE_SIZE)
141 free_pages((unsigned long)hash, get_order(size));
144 static void fn_rehash_zone(struct fn_zone *fz)
146 struct hlist_head *ht, *old_ht;
147 int old_divisor, new_divisor;
150 old_divisor = fz->fz_divisor;
152 switch (old_divisor) {
160 if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
161 printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
164 new_divisor = (old_divisor << 1);
168 new_hashmask = (new_divisor - 1);
170 #if RT_CACHE_DEBUG >= 2
171 printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
174 ht = fz_hash_alloc(new_divisor);
177 write_lock_bh(&fib_hash_lock);
178 old_ht = fz->fz_hash;
180 fz->fz_hashmask = new_hashmask;
181 fz->fz_divisor = new_divisor;
182 fn_rebuild_zone(fz, old_ht, old_divisor);
184 write_unlock_bh(&fib_hash_lock);
186 fz_hash_free(old_ht, old_divisor);
190 static inline void fn_free_node(struct fib_node * f)
192 kmem_cache_free(fn_hash_kmem, f);
195 static inline void fn_free_alias(struct fib_alias *fa)
197 fib_release_info(fa->fa_info);
198 kmem_cache_free(fn_alias_kmem, fa);
201 static struct fn_zone *
202 fn_new_zone(struct fn_hash *table, int z)
205 struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
214 fz->fz_hashmask = (fz->fz_divisor - 1);
215 fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
221 fz->fz_mask = inet_make_mask(z);
223 /* Find the first not empty zone with more specific mask */
224 for (i=z+1; i<=32; i++)
225 if (table->fn_zones[i])
227 write_lock_bh(&fib_hash_lock);
229 /* No more specific masks, we are the first. */
230 fz->fz_next = table->fn_zone_list;
231 table->fn_zone_list = fz;
233 fz->fz_next = table->fn_zones[i]->fz_next;
234 table->fn_zones[i]->fz_next = fz;
236 table->fn_zones[z] = fz;
238 write_unlock_bh(&fib_hash_lock);
243 fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
247 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
249 read_lock(&fib_hash_lock);
250 for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
251 struct hlist_head *head;
252 struct hlist_node *node;
254 __be32 k = fz_key(flp->fl4_dst, fz);
256 head = &fz->fz_hash[fn_hash(k, fz)];
257 hlist_for_each_entry(f, node, head, fn_hash) {
261 err = fib_semantic_match(&f->fn_alias,
263 f->fn_key, fz->fz_mask,
271 read_unlock(&fib_hash_lock);
276 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
279 struct hlist_node *node;
281 struct fib_info *fi = NULL;
282 struct fib_info *last_resort;
283 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
284 struct fn_zone *fz = t->fn_zones[0];
293 read_lock(&fib_hash_lock);
294 hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
295 struct fib_alias *fa;
297 list_for_each_entry(fa, &f->fn_alias, fa_list) {
298 struct fib_info *next_fi = fa->fa_info;
300 if (fa->fa_scope != res->scope ||
301 fa->fa_type != RTN_UNICAST)
304 if (next_fi->fib_priority > res->fi->fib_priority)
306 if (!next_fi->fib_nh[0].nh_gw ||
307 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
309 fa->fa_state |= FA_S_ACCESSED;
312 if (next_fi != res->fi)
314 } else if (!fib_detect_death(fi, order, &last_resort,
315 &last_idx, tb->tb_default)) {
316 fib_result_assign(res, fi);
317 tb->tb_default = order;
325 if (order <= 0 || fi == NULL) {
330 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
332 fib_result_assign(res, fi);
333 tb->tb_default = order;
338 fib_result_assign(res, last_resort);
339 tb->tb_default = last_idx;
341 read_unlock(&fib_hash_lock);
344 /* Insert node F to FZ. */
345 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
347 struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
349 hlist_add_head(&f->fn_hash, head);
352 /* Return the node in FZ matching KEY. */
353 static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
355 struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
356 struct hlist_node *node;
359 hlist_for_each_entry(f, node, head, fn_hash) {
360 if (f->fn_key == key)
367 static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
369 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
370 struct fib_node *new_f, *f;
371 struct fib_alias *fa, *new_fa;
374 u8 tos = cfg->fc_tos;
378 if (cfg->fc_dst_len > 32)
381 fz = table->fn_zones[cfg->fc_dst_len];
382 if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
387 if (cfg->fc_dst & ~FZ_MASK(fz))
389 key = fz_key(cfg->fc_dst, fz);
392 fi = fib_create_info(cfg);
396 if (fz->fz_nent > (fz->fz_divisor<<1) &&
397 fz->fz_divisor < FZ_MAX_DIVISOR &&
398 (cfg->fc_dst_len == 32 ||
399 (1 << cfg->fc_dst_len) > fz->fz_divisor))
402 f = fib_find_node(fz, key);
407 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
409 /* Now fa, if non-NULL, points to the first fib alias
410 * with the same keys [prefix,tos,priority], if such key already
411 * exists or to the node before which we will insert new one.
413 * If fa is NULL, we will need to allocate a new one and
414 * insert to the head of f.
416 * If f is NULL, no fib node matched the destination key
417 * and we need to allocate a new one of those as well.
420 if (fa && fa->fa_tos == tos &&
421 fa->fa_info->fib_priority == fi->fib_priority) {
422 struct fib_alias *fa_orig;
425 if (cfg->fc_nlflags & NLM_F_EXCL)
428 if (cfg->fc_nlflags & NLM_F_REPLACE) {
429 struct fib_info *fi_drop;
432 if (fi->fib_treeref > 1)
435 write_lock_bh(&fib_hash_lock);
436 fi_drop = fa->fa_info;
438 fa->fa_type = cfg->fc_type;
439 fa->fa_scope = cfg->fc_scope;
440 state = fa->fa_state;
441 fa->fa_state &= ~FA_S_ACCESSED;
443 write_unlock_bh(&fib_hash_lock);
445 fib_release_info(fi_drop);
446 if (state & FA_S_ACCESSED)
448 rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
449 &cfg->fc_nlinfo, NLM_F_REPLACE);
453 /* Error if we find a perfect match which
454 * uses the same scope, type, and nexthop
458 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
459 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
460 if (fa->fa_tos != tos)
462 if (fa->fa_info->fib_priority != fi->fib_priority)
464 if (fa->fa_type == cfg->fc_type &&
465 fa->fa_scope == cfg->fc_scope &&
469 if (!(cfg->fc_nlflags & NLM_F_APPEND))
474 if (!(cfg->fc_nlflags & NLM_F_CREATE))
478 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
484 new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
486 goto out_free_new_fa;
488 INIT_HLIST_NODE(&new_f->fn_hash);
489 INIT_LIST_HEAD(&new_f->fn_alias);
494 new_fa->fa_info = fi;
495 new_fa->fa_tos = tos;
496 new_fa->fa_type = cfg->fc_type;
497 new_fa->fa_scope = cfg->fc_scope;
498 new_fa->fa_state = 0;
501 * Insert new entry to the list.
504 write_lock_bh(&fib_hash_lock);
506 fib_insert_node(fz, new_f);
507 list_add_tail(&new_fa->fa_list,
508 (fa ? &fa->fa_list : &f->fn_alias));
510 write_unlock_bh(&fib_hash_lock);
516 rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
521 kmem_cache_free(fn_alias_kmem, new_fa);
523 fib_release_info(fi);
528 static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
530 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
532 struct fib_alias *fa, *fa_to_delete;
536 if (cfg->fc_dst_len > 32)
539 if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
544 if (cfg->fc_dst & ~FZ_MASK(fz))
546 key = fz_key(cfg->fc_dst, fz);
549 f = fib_find_node(fz, key);
554 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
559 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
560 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
561 struct fib_info *fi = fa->fa_info;
563 if (fa->fa_tos != cfg->fc_tos)
566 if ((!cfg->fc_type ||
567 fa->fa_type == cfg->fc_type) &&
568 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
569 fa->fa_scope == cfg->fc_scope) &&
570 (!cfg->fc_protocol ||
571 fi->fib_protocol == cfg->fc_protocol) &&
572 fib_nh_match(cfg, fi) == 0) {
582 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
583 tb->tb_id, &cfg->fc_nlinfo, 0);
586 write_lock_bh(&fib_hash_lock);
587 list_del(&fa->fa_list);
588 if (list_empty(&f->fn_alias)) {
589 hlist_del(&f->fn_hash);
593 write_unlock_bh(&fib_hash_lock);
595 if (fa->fa_state & FA_S_ACCESSED)
608 static int fn_flush_list(struct fn_zone *fz, int idx)
610 struct hlist_head *head = &fz->fz_hash[idx];
611 struct hlist_node *node, *n;
615 hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
616 struct fib_alias *fa, *fa_node;
620 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
621 struct fib_info *fi = fa->fa_info;
623 if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
624 write_lock_bh(&fib_hash_lock);
625 list_del(&fa->fa_list);
626 if (list_empty(&f->fn_alias)) {
627 hlist_del(&f->fn_hash);
631 write_unlock_bh(&fib_hash_lock);
645 static int fn_hash_flush(struct fib_table *tb)
647 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
651 for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
654 for (i = fz->fz_divisor - 1; i >= 0; i--)
655 found += fn_flush_list(fz, i);
662 fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
663 struct fib_table *tb,
665 struct hlist_head *head)
667 struct hlist_node *node;
673 hlist_for_each_entry(f, node, head, fn_hash) {
674 struct fib_alias *fa;
676 list_for_each_entry(fa, &f->fn_alias, fa_list) {
680 if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
703 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
704 struct fib_table *tb,
709 if (fz->fz_hash == NULL)
712 for (h = s_h; h < fz->fz_divisor; h++) {
713 if (hlist_empty(&fz->fz_hash[h]))
715 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
719 memset(&cb->args[4], 0,
720 sizeof(cb->args) - 4*sizeof(cb->args[0]));
726 static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
730 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
733 read_lock(&fib_hash_lock);
734 for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
735 if (m < s_m) continue;
736 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
738 read_unlock(&fib_hash_lock);
741 memset(&cb->args[3], 0,
742 sizeof(cb->args) - 3*sizeof(cb->args[0]));
744 read_unlock(&fib_hash_lock);
749 #ifdef CONFIG_IP_MULTIPLE_TABLES
750 struct fib_table * fib_hash_init(u32 id)
752 struct fib_table * __init fib_hash_init(u32 id)
755 struct fib_table *tb;
757 if (fn_hash_kmem == NULL)
758 fn_hash_kmem = kmem_cache_create("ip_fib_hash",
759 sizeof(struct fib_node),
760 0, SLAB_HWCACHE_ALIGN,
763 if (fn_alias_kmem == NULL)
764 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
765 sizeof(struct fib_alias),
766 0, SLAB_HWCACHE_ALIGN,
769 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
776 tb->tb_lookup = fn_hash_lookup;
777 tb->tb_insert = fn_hash_insert;
778 tb->tb_delete = fn_hash_delete;
779 tb->tb_flush = fn_hash_flush;
780 tb->tb_select_default = fn_hash_select_default;
781 tb->tb_dump = fn_hash_dump;
782 memset(tb->tb_data, 0, sizeof(struct fn_hash));
786 /* ------------------------------------------------------------------------ */
787 #ifdef CONFIG_PROC_FS
789 struct fib_iter_state {
790 struct fn_zone *zone;
792 struct hlist_head *hash_head;
794 struct fib_alias *fa;
800 static struct fib_alias *fib_get_first(struct seq_file *seq)
802 struct fib_iter_state *iter = seq->private;
803 struct fib_table *main_table = fib_get_table(RT_TABLE_MAIN);
804 struct fn_hash *table = (struct fn_hash *)main_table->tb_data;
807 iter->hash_head = NULL;
811 iter->genid = fib_hash_genid;
814 for (iter->zone = table->fn_zone_list; iter->zone;
815 iter->zone = iter->zone->fz_next) {
818 if (!iter->zone->fz_nent)
821 iter->hash_head = iter->zone->fz_hash;
822 maxslot = iter->zone->fz_divisor;
824 for (iter->bucket = 0; iter->bucket < maxslot;
825 ++iter->bucket, ++iter->hash_head) {
826 struct hlist_node *node;
829 hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
830 struct fib_alias *fa;
832 list_for_each_entry(fa,&fn->fn_alias,fa_list) {
844 static struct fib_alias *fib_get_next(struct seq_file *seq)
846 struct fib_iter_state *iter = seq->private;
848 struct fib_alias *fa;
850 /* Advance FA, if any. */
855 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
861 fa = iter->fa = NULL;
865 struct hlist_node *node = &fn->fn_hash;
866 hlist_for_each_entry_continue(fn, node, fn_hash) {
869 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
876 fn = iter->fn = NULL;
878 /* Advance hash chain. */
883 struct hlist_node *node;
886 maxslot = iter->zone->fz_divisor;
888 while (++iter->bucket < maxslot) {
891 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
892 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
900 iter->zone = iter->zone->fz_next;
906 iter->hash_head = iter->zone->fz_hash;
908 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
909 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
921 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
923 struct fib_iter_state *iter = seq->private;
924 struct fib_alias *fa;
926 if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
930 fa = fib_get_first(seq);
933 while (pos && (fa = fib_get_next(seq)))
935 return pos ? NULL : fa;
938 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
942 read_lock(&fib_hash_lock);
943 if (fib_get_table(RT_TABLE_MAIN))
944 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
948 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
951 return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
954 static void fib_seq_stop(struct seq_file *seq, void *v)
956 read_unlock(&fib_hash_lock);
959 static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
961 static const unsigned type2flags[RTN_MAX + 1] = {
962 [7] = RTF_REJECT, [8] = RTF_REJECT,
964 unsigned flags = type2flags[type];
966 if (fi && fi->fib_nh->nh_gw)
967 flags |= RTF_GATEWAY;
968 if (mask == htonl(0xFFFFFFFF))
975 * This outputs /proc/net/route.
977 * It always works in backward compatibility mode.
978 * The format of the file is not supposed to be changed.
980 static int fib_seq_show(struct seq_file *seq, void *v)
982 struct fib_iter_state *iter;
987 struct fib_alias *fa;
990 if (v == SEQ_START_TOKEN) {
991 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
992 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1002 mask = FZ_MASK(iter->zone);
1003 flags = fib_flag_trans(fa->fa_type, mask, fi);
1005 snprintf(bf, sizeof(bf),
1006 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1007 fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1008 fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1009 mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1013 snprintf(bf, sizeof(bf),
1014 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1015 prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
1016 seq_printf(seq, "%-127s\n", bf);
1021 static const struct seq_operations fib_seq_ops = {
1022 .start = fib_seq_start,
1023 .next = fib_seq_next,
1024 .stop = fib_seq_stop,
1025 .show = fib_seq_show,
1028 static int fib_seq_open(struct inode *inode, struct file *file)
1030 return seq_open_private(file, &fib_seq_ops,
1031 sizeof(struct fib_iter_state));
1034 static const struct file_operations fib_seq_fops = {
1035 .owner = THIS_MODULE,
1036 .open = fib_seq_open,
1038 .llseek = seq_lseek,
1039 .release = seq_release_private,
1042 int __init fib_proc_init(void)
1044 if (!proc_net_fops_create(&init_net, "route", S_IRUGO, &fib_seq_fops))
1049 void __init fib_proc_exit(void)
1051 proc_net_remove(&init_net, "route");
1053 #endif /* CONFIG_PROC_FS */