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>
39 #include <net/protocol.h>
40 #include <net/route.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
;
51 struct hlist_node fn_hash
;
52 struct list_head fn_alias
;
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 */
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.
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
);
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
);
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
,
118 for (i
= 0; i
< old_divisor
; i
++) {
119 struct hlist_node
*node
, *n
;
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
)
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
;
149 old_divisor
= fz
->fz_divisor
;
151 switch (old_divisor
) {
159 if ((old_divisor
<< 1) > FZ_MAX_DIVISOR
) {
160 printk(KERN_CRIT
"route.c: bad divisor %d!\n", old_divisor
);
163 new_divisor
= (old_divisor
<< 1);
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
);
173 ht
= fz_hash_alloc(new_divisor
);
176 memset(ht
, 0, new_divisor
* sizeof(struct hlist_head
));
178 write_lock_bh(&fib_hash_lock
);
179 old_ht
= fz
->fz_hash
;
181 fz
->fz_hashmask
= new_hashmask
;
182 fz
->fz_divisor
= new_divisor
;
183 fn_rebuild_zone(fz
, old_ht
, old_divisor
);
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
)
206 struct fn_zone
*fz
= kzalloc(sizeof(struct fn_zone
), GFP_KERNEL
);
215 fz
->fz_hashmask
= (fz
->fz_divisor
- 1);
216 fz
->fz_hash
= fz_hash_alloc(fz
->fz_divisor
);
221 memset(fz
->fz_hash
, 0, fz
->fz_divisor
* sizeof(struct hlist_head
*));
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
])
229 write_lock_bh(&fib_hash_lock
);
231 /* No more specific masks, we are the first. */
232 fz
->fz_next
= table
->fn_zone_list
;
233 table
->fn_zone_list
= fz
;
235 fz
->fz_next
= table
->fn_zones
[i
]->fz_next
;
236 table
->fn_zones
[i
]->fz_next
= fz
;
238 table
->fn_zones
[z
] = fz
;
240 write_unlock_bh(&fib_hash_lock
);
245 fn_hash_lookup(struct fib_table
*tb
, const struct flowi
*flp
, struct fib_result
*res
)
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
;
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
) {
263 err
= fib_semantic_match(&f
->fn_alias
,
265 f
->fn_key
, fz
->fz_mask
,
273 read_unlock(&fib_hash_lock
);
277 static int fn_hash_last_dflt
=-1;
280 fn_hash_select_default(struct fib_table
*tb
, const struct flowi
*flp
, struct fib_result
*res
)
283 struct hlist_node
*node
;
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];
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
)
308 if (next_fi
->fib_priority
> res
->fi
->fib_priority
)
310 if (!next_fi
->fib_nh
[0].nh_gw
||
311 next_fi
->fib_nh
[0].nh_scope
!= RT_SCOPE_LINK
)
313 fa
->fa_state
|= FA_S_ACCESSED
;
316 if (next_fi
!= res
->fi
)
318 } else if (!fib_detect_death(fi
, order
, &last_resort
,
319 &last_idx
, &fn_hash_last_dflt
)) {
321 fib_info_put(res
->fi
);
323 atomic_inc(&fi
->fib_clntref
);
324 fn_hash_last_dflt
= order
;
332 if (order
<= 0 || fi
== NULL
) {
333 fn_hash_last_dflt
= -1;
337 if (!fib_detect_death(fi
, order
, &last_resort
, &last_idx
, &fn_hash_last_dflt
)) {
339 fib_info_put(res
->fi
);
341 atomic_inc(&fi
->fib_clntref
);
342 fn_hash_last_dflt
= order
;
348 fib_info_put(res
->fi
);
349 res
->fi
= last_resort
;
351 atomic_inc(&last_resort
->fib_clntref
);
353 fn_hash_last_dflt
= last_idx
;
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
;
373 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
374 if (f
->fn_key
== key
)
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
;
388 u8 tos
= cfg
->fc_tos
;
392 if (cfg
->fc_dst_len
> 32)
395 fz
= table
->fn_zones
[cfg
->fc_dst_len
];
396 if (!fz
&& !(fz
= fn_new_zone(table
, cfg
->fc_dst_len
)))
401 if (cfg
->fc_dst
& ~FZ_MASK(fz
))
403 key
= fz_key(cfg
->fc_dst
, fz
);
406 fi
= fib_create_info(cfg
);
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
))
416 f
= fib_find_node(fz
, key
);
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
;
439 if (cfg
->fc_nlflags
& NLM_F_EXCL
)
442 if (cfg
->fc_nlflags
& NLM_F_REPLACE
) {
443 struct fib_info
*fi_drop
;
446 write_lock_bh(&fib_hash_lock
);
447 fi_drop
= fa
->fa_info
;
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
;
454 write_unlock_bh(&fib_hash_lock
);
456 fib_release_info(fi_drop
);
457 if (state
& FA_S_ACCESSED
)
459 rtmsg_fib(RTM_NEWROUTE
, key
, fa
, cfg
->fc_dst_len
, tb
->tb_id
,
460 &cfg
->fc_nlinfo
, NLM_F_REPLACE
);
464 /* Error if we find a perfect match which
465 * uses the same scope, type, and nexthop
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
)
473 if (fa
->fa_info
->fib_priority
!= fi
->fib_priority
)
475 if (fa
->fa_type
== cfg
->fc_type
&&
476 fa
->fa_scope
== cfg
->fc_scope
&&
480 if (!(cfg
->fc_nlflags
& NLM_F_APPEND
))
485 if (!(cfg
->fc_nlflags
& NLM_F_CREATE
))
489 new_fa
= kmem_cache_alloc(fn_alias_kmem
, GFP_KERNEL
);
495 new_f
= kmem_cache_alloc(fn_hash_kmem
, GFP_KERNEL
);
497 goto out_free_new_fa
;
499 INIT_HLIST_NODE(&new_f
->fn_hash
);
500 INIT_LIST_HEAD(&new_f
->fn_alias
);
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
);
517 fib_insert_node(fz
, new_f
);
518 list_add_tail(&new_fa
->fa_list
,
519 (fa
? &fa
->fa_list
: &f
->fn_alias
));
521 write_unlock_bh(&fib_hash_lock
);
527 rtmsg_fib(RTM_NEWROUTE
, key
, new_fa
, cfg
->fc_dst_len
, tb
->tb_id
,
532 kmem_cache_free(fn_alias_kmem
, new_fa
);
534 fib_release_info(fi
);
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
;
543 struct fib_alias
*fa
, *fa_to_delete
;
547 if (cfg
->fc_dst_len
> 32)
550 if ((fz
= table
->fn_zones
[cfg
->fc_dst_len
]) == NULL
)
555 if (cfg
->fc_dst
& ~FZ_MASK(fz
))
557 key
= fz_key(cfg
->fc_dst
, fz
);
560 f
= fib_find_node(fz
, key
);
565 fa
= fib_find_alias(&f
->fn_alias
, cfg
->fc_tos
, 0);
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
)
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) {
593 rtmsg_fib(RTM_DELROUTE
, key
, fa
, cfg
->fc_dst_len
,
594 tb
->tb_id
, &cfg
->fc_nlinfo
, 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
);
604 write_unlock_bh(&fib_hash_lock
);
606 if (fa
->fa_state
& FA_S_ACCESSED
)
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
;
626 hlist_for_each_entry_safe(f
, node
, n
, head
, fn_hash
) {
627 struct fib_alias
*fa
, *fa_node
;
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
);
642 write_unlock_bh(&fib_hash_lock
);
656 static int fn_hash_flush(struct fib_table
*tb
)
658 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
662 for (fz
= table
->fn_zone_list
; fz
; fz
= fz
->fz_next
) {
665 for (i
= fz
->fz_divisor
- 1; i
>= 0; i
--)
666 found
+= fn_flush_list(fz
, i
);
673 fn_hash_dump_bucket(struct sk_buff
*skb
, struct netlink_callback
*cb
,
674 struct fib_table
*tb
,
676 struct hlist_head
*head
)
678 struct hlist_node
*node
;
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
) {
691 if (fib_dump_info(skb
, NETLINK_CB(cb
->skb
).pid
,
714 fn_hash_dump_zone(struct sk_buff
*skb
, struct netlink_callback
*cb
,
715 struct fib_table
*tb
,
721 for (h
=0; h
< fz
->fz_divisor
; h
++) {
722 if (h
< s_h
) continue;
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
]))
729 if (fn_hash_dump_bucket(skb
, cb
, tb
, fz
, &fz
->fz_hash
[h
])<0) {
738 static int fn_hash_dump(struct fib_table
*tb
, struct sk_buff
*skb
, struct netlink_callback
*cb
)
742 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
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;
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) {
753 read_unlock(&fib_hash_lock
);
757 read_unlock(&fib_hash_lock
);
762 #ifdef CONFIG_IP_MULTIPLE_TABLES
763 struct fib_table
* fib_hash_init(u32 id
)
765 struct fib_table
* __init
fib_hash_init(u32 id
)
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
,
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
,
782 tb
= kmalloc(sizeof(struct fib_table
) + sizeof(struct fn_hash
),
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
));
798 /* ------------------------------------------------------------------------ */
799 #ifdef CONFIG_PROC_FS
801 struct fib_iter_state
{
802 struct fn_zone
*zone
;
804 struct hlist_head
*hash_head
;
806 struct fib_alias
*fa
;
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
;
818 iter
->hash_head
= NULL
;
822 iter
->genid
= fib_hash_genid
;
825 for (iter
->zone
= table
->fn_zone_list
; iter
->zone
;
826 iter
->zone
= iter
->zone
->fz_next
) {
829 if (!iter
->zone
->fz_nent
)
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
;
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
) {
855 static struct fib_alias
*fib_get_next(struct seq_file
*seq
)
857 struct fib_iter_state
*iter
= seq
->private;
859 struct fib_alias
*fa
;
861 /* Advance FA, if any. */
866 list_for_each_entry_continue(fa
, &fn
->fn_alias
, fa_list
) {
872 fa
= iter
->fa
= NULL
;
876 struct hlist_node
*node
= &fn
->fn_hash
;
877 hlist_for_each_entry_continue(fn
, node
, fn_hash
) {
880 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
887 fn
= iter
->fn
= NULL
;
889 /* Advance hash chain. */
894 struct hlist_node
*node
;
897 maxslot
= iter
->zone
->fz_divisor
;
899 while (++iter
->bucket
< maxslot
) {
902 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
903 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
911 iter
->zone
= iter
->zone
->fz_next
;
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
) {
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
) {
941 fa
= fib_get_first(seq
);
944 while (pos
&& (fa
= fib_get_next(seq
)))
946 return pos
? NULL
: fa
;
949 static void *fib_seq_start(struct seq_file
*seq
, loff_t
*pos
)
953 read_lock(&fib_hash_lock
);
954 if (ip_fib_main_table
)
955 v
= *pos
? fib_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
959 static void *fib_seq_next(struct seq_file
*seq
, void *v
, loff_t
*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))
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
;
998 struct fib_alias
*fa
;
1001 if (v
== SEQ_START_TOKEN
) {
1002 seq_printf(seq
, "%-127s\n", "Iface\tDestination\tGateway "
1003 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1008 iter
= seq
->private;
1013 mask
= FZ_MASK(iter
->zone
);
1014 flags
= fib_flag_trans(fa
->fa_type
, mask
, 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),
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
);
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
;
1043 struct fib_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
1048 rc
= seq_open(file
, &fib_seq_ops
);
1052 seq
= file
->private_data
;
1061 static const struct file_operations fib_seq_fops
= {
1062 .owner
= THIS_MODULE
,
1063 .open
= fib_seq_open
,
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
))
1076 void __init
fib_proc_exit(void)
1078 proc_net_remove("route");
1080 #endif /* CONFIG_PROC_FS */