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
;
55 struct fib_alias fn_embedded_alias
;
59 struct fn_zone
*fz_next
; /* Next not empty zone */
60 struct hlist_head
*fz_hash
; /* Hash table pointer */
61 int fz_nent
; /* Number of entries */
63 int fz_divisor
; /* Hash divisor */
64 u32 fz_hashmask
; /* (fz_divisor - 1) */
65 #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
67 int fz_order
; /* Zone order */
69 #define FZ_MASK(fz) ((fz)->fz_mask)
72 /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
73 * can be cheaper than memory lookup, so that FZ_* macros are used.
77 struct fn_zone
*fn_zones
[33];
78 struct fn_zone
*fn_zone_list
;
81 static inline u32
fn_hash(__be32 key
, struct fn_zone
*fz
)
83 u32 h
= ntohl(key
)>>(32 - fz
->fz_order
);
91 static inline __be32
fz_key(__be32 dst
, struct fn_zone
*fz
)
93 return dst
& FZ_MASK(fz
);
96 static DEFINE_RWLOCK(fib_hash_lock
);
97 static unsigned int fib_hash_genid
;
99 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
101 static struct hlist_head
*fz_hash_alloc(int divisor
)
103 unsigned long size
= divisor
* sizeof(struct hlist_head
);
105 if (size
<= PAGE_SIZE
) {
106 return kzalloc(size
, GFP_KERNEL
);
108 return (struct hlist_head
*)
109 __get_free_pages(GFP_KERNEL
| __GFP_ZERO
, get_order(size
));
113 /* The fib hash lock must be held when this is called. */
114 static inline void fn_rebuild_zone(struct fn_zone
*fz
,
115 struct hlist_head
*old_ht
,
120 for (i
= 0; i
< old_divisor
; i
++) {
121 struct hlist_node
*node
, *n
;
124 hlist_for_each_entry_safe(f
, node
, n
, &old_ht
[i
], fn_hash
) {
125 struct hlist_head
*new_head
;
127 hlist_del(&f
->fn_hash
);
129 new_head
= &fz
->fz_hash
[fn_hash(f
->fn_key
, fz
)];
130 hlist_add_head(&f
->fn_hash
, new_head
);
135 static void fz_hash_free(struct hlist_head
*hash
, int divisor
)
137 unsigned long size
= divisor
* sizeof(struct hlist_head
);
139 if (size
<= PAGE_SIZE
)
142 free_pages((unsigned long)hash
, get_order(size
));
145 static void fn_rehash_zone(struct fn_zone
*fz
)
147 struct hlist_head
*ht
, *old_ht
;
148 int old_divisor
, new_divisor
;
151 old_divisor
= fz
->fz_divisor
;
153 switch (old_divisor
) {
161 if ((old_divisor
<< 1) > FZ_MAX_DIVISOR
) {
162 printk(KERN_CRIT
"route.c: bad divisor %d!\n", old_divisor
);
165 new_divisor
= (old_divisor
<< 1);
169 new_hashmask
= (new_divisor
- 1);
171 #if RT_CACHE_DEBUG >= 2
172 printk(KERN_DEBUG
"fn_rehash_zone: hash for zone %d grows from %d\n",
173 fz
->fz_order
, old_divisor
);
176 ht
= fz_hash_alloc(new_divisor
);
179 write_lock_bh(&fib_hash_lock
);
180 old_ht
= fz
->fz_hash
;
182 fz
->fz_hashmask
= new_hashmask
;
183 fz
->fz_divisor
= new_divisor
;
184 fn_rebuild_zone(fz
, old_ht
, old_divisor
);
186 write_unlock_bh(&fib_hash_lock
);
188 fz_hash_free(old_ht
, old_divisor
);
192 static inline void fn_free_node(struct fib_node
* f
)
194 kmem_cache_free(fn_hash_kmem
, f
);
197 static inline void fn_free_alias(struct fib_alias
*fa
, struct fib_node
*f
)
199 fib_release_info(fa
->fa_info
);
200 if (fa
== &f
->fn_embedded_alias
)
203 kmem_cache_free(fn_alias_kmem
, fa
);
206 static struct fn_zone
*
207 fn_new_zone(struct fn_hash
*table
, int z
)
210 struct fn_zone
*fz
= kzalloc(sizeof(struct fn_zone
), GFP_KERNEL
);
219 fz
->fz_hashmask
= (fz
->fz_divisor
- 1);
220 fz
->fz_hash
= fz_hash_alloc(fz
->fz_divisor
);
226 fz
->fz_mask
= inet_make_mask(z
);
228 /* Find the first not empty zone with more specific mask */
229 for (i
=z
+1; i
<=32; i
++)
230 if (table
->fn_zones
[i
])
232 write_lock_bh(&fib_hash_lock
);
234 /* No more specific masks, we are the first. */
235 fz
->fz_next
= table
->fn_zone_list
;
236 table
->fn_zone_list
= fz
;
238 fz
->fz_next
= table
->fn_zones
[i
]->fz_next
;
239 table
->fn_zones
[i
]->fz_next
= fz
;
241 table
->fn_zones
[z
] = fz
;
243 write_unlock_bh(&fib_hash_lock
);
248 fn_hash_lookup(struct fib_table
*tb
, const struct flowi
*flp
, struct fib_result
*res
)
252 struct fn_hash
*t
= (struct fn_hash
*)tb
->tb_data
;
254 read_lock(&fib_hash_lock
);
255 for (fz
= t
->fn_zone_list
; fz
; fz
= fz
->fz_next
) {
256 struct hlist_head
*head
;
257 struct hlist_node
*node
;
259 __be32 k
= fz_key(flp
->fl4_dst
, fz
);
261 head
= &fz
->fz_hash
[fn_hash(k
, fz
)];
262 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
266 err
= fib_semantic_match(&f
->fn_alias
,
268 f
->fn_key
, fz
->fz_mask
,
276 read_unlock(&fib_hash_lock
);
281 fn_hash_select_default(struct fib_table
*tb
, const struct flowi
*flp
, struct fib_result
*res
)
284 struct hlist_node
*node
;
286 struct fib_info
*fi
= NULL
;
287 struct fib_info
*last_resort
;
288 struct fn_hash
*t
= (struct fn_hash
*)tb
->tb_data
;
289 struct fn_zone
*fz
= t
->fn_zones
[0];
298 read_lock(&fib_hash_lock
);
299 hlist_for_each_entry(f
, node
, &fz
->fz_hash
[0], fn_hash
) {
300 struct fib_alias
*fa
;
302 list_for_each_entry(fa
, &f
->fn_alias
, fa_list
) {
303 struct fib_info
*next_fi
= fa
->fa_info
;
305 if (fa
->fa_scope
!= res
->scope
||
306 fa
->fa_type
!= RTN_UNICAST
)
309 if (next_fi
->fib_priority
> res
->fi
->fib_priority
)
311 if (!next_fi
->fib_nh
[0].nh_gw
||
312 next_fi
->fib_nh
[0].nh_scope
!= RT_SCOPE_LINK
)
314 fa
->fa_state
|= FA_S_ACCESSED
;
317 if (next_fi
!= res
->fi
)
319 } else if (!fib_detect_death(fi
, order
, &last_resort
,
320 &last_idx
, tb
->tb_default
)) {
321 fib_result_assign(res
, fi
);
322 tb
->tb_default
= order
;
330 if (order
<= 0 || fi
== NULL
) {
335 if (!fib_detect_death(fi
, order
, &last_resort
, &last_idx
,
337 fib_result_assign(res
, fi
);
338 tb
->tb_default
= order
;
343 fib_result_assign(res
, last_resort
);
344 tb
->tb_default
= last_idx
;
346 read_unlock(&fib_hash_lock
);
349 /* Insert node F to FZ. */
350 static inline void fib_insert_node(struct fn_zone
*fz
, struct fib_node
*f
)
352 struct hlist_head
*head
= &fz
->fz_hash
[fn_hash(f
->fn_key
, fz
)];
354 hlist_add_head(&f
->fn_hash
, head
);
357 /* Return the node in FZ matching KEY. */
358 static struct fib_node
*fib_find_node(struct fn_zone
*fz
, __be32 key
)
360 struct hlist_head
*head
= &fz
->fz_hash
[fn_hash(key
, fz
)];
361 struct hlist_node
*node
;
364 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
365 if (f
->fn_key
== key
)
372 static int fn_hash_insert(struct fib_table
*tb
, struct fib_config
*cfg
)
374 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
375 <<<<<<< HEAD
:net
/ipv4
/fib_hash
.c
376 struct fib_node
*new_f
, *f
;
378 struct fib_node
*new_f
= NULL
;
380 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ipv4
/fib_hash
.c
381 struct fib_alias
*fa
, *new_fa
;
384 u8 tos
= cfg
->fc_tos
;
388 if (cfg
->fc_dst_len
> 32)
391 fz
= table
->fn_zones
[cfg
->fc_dst_len
];
392 if (!fz
&& !(fz
= fn_new_zone(table
, cfg
->fc_dst_len
)))
397 if (cfg
->fc_dst
& ~FZ_MASK(fz
))
399 key
= fz_key(cfg
->fc_dst
, fz
);
402 fi
= fib_create_info(cfg
);
406 if (fz
->fz_nent
> (fz
->fz_divisor
<<1) &&
407 fz
->fz_divisor
< FZ_MAX_DIVISOR
&&
408 (cfg
->fc_dst_len
== 32 ||
409 (1 << cfg
->fc_dst_len
) > fz
->fz_divisor
))
412 f
= fib_find_node(fz
, key
);
417 fa
= fib_find_alias(&f
->fn_alias
, tos
, fi
->fib_priority
);
419 /* Now fa, if non-NULL, points to the first fib alias
420 * with the same keys [prefix,tos,priority], if such key already
421 * exists or to the node before which we will insert new one.
423 * If fa is NULL, we will need to allocate a new one and
424 * insert to the head of f.
426 * If f is NULL, no fib node matched the destination key
427 * and we need to allocate a new one of those as well.
430 if (fa
&& fa
->fa_tos
== tos
&&
431 fa
->fa_info
->fib_priority
== fi
->fib_priority
) {
432 struct fib_alias
*fa_first
, *fa_match
;
435 if (cfg
->fc_nlflags
& NLM_F_EXCL
)
439 * 1. Find exact match for type, scope, fib_info to avoid
441 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
445 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
446 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
447 if (fa
->fa_tos
!= tos
)
449 if (fa
->fa_info
->fib_priority
!= fi
->fib_priority
)
451 if (fa
->fa_type
== cfg
->fc_type
&&
452 fa
->fa_scope
== cfg
->fc_scope
&&
459 if (cfg
->fc_nlflags
& NLM_F_REPLACE
) {
460 struct fib_info
*fi_drop
;
469 write_lock_bh(&fib_hash_lock
);
470 fi_drop
= fa
->fa_info
;
472 fa
->fa_type
= cfg
->fc_type
;
473 fa
->fa_scope
= cfg
->fc_scope
;
474 state
= fa
->fa_state
;
475 fa
->fa_state
&= ~FA_S_ACCESSED
;
477 write_unlock_bh(&fib_hash_lock
);
479 fib_release_info(fi_drop
);
480 if (state
& FA_S_ACCESSED
)
482 rtmsg_fib(RTM_NEWROUTE
, key
, fa
, cfg
->fc_dst_len
, tb
->tb_id
,
483 &cfg
->fc_nlinfo
, NLM_F_REPLACE
);
487 /* Error if we find a perfect match which
488 * uses the same scope, type, and nexthop
494 if (!(cfg
->fc_nlflags
& NLM_F_APPEND
))
499 if (!(cfg
->fc_nlflags
& NLM_F_CREATE
))
504 <<<<<<< HEAD
:net
/ipv4
/fib_hash
.c
507 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ipv4
/fib_hash
.c
509 new_f
= kmem_cache_zalloc(fn_hash_kmem
, GFP_KERNEL
);
513 INIT_HLIST_NODE(&new_f
->fn_hash
);
514 INIT_LIST_HEAD(&new_f
->fn_alias
);
519 new_fa
= &f
->fn_embedded_alias
;
520 if (new_fa
->fa_info
!= NULL
) {
521 new_fa
= kmem_cache_alloc(fn_alias_kmem
, GFP_KERNEL
);
523 <<<<<<< HEAD
:net
/ipv4
/fib_hash
.c
527 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ipv4
/fib_hash
.c
529 new_fa
->fa_info
= fi
;
530 new_fa
->fa_tos
= tos
;
531 new_fa
->fa_type
= cfg
->fc_type
;
532 new_fa
->fa_scope
= cfg
->fc_scope
;
533 new_fa
->fa_state
= 0;
536 * Insert new entry to the list.
539 write_lock_bh(&fib_hash_lock
);
541 fib_insert_node(fz
, new_f
);
542 list_add_tail(&new_fa
->fa_list
,
543 (fa
? &fa
->fa_list
: &f
->fn_alias
));
545 write_unlock_bh(&fib_hash_lock
);
551 rtmsg_fib(RTM_NEWROUTE
, key
, new_fa
, cfg
->fc_dst_len
, tb
->tb_id
,
555 <<<<<<< HEAD
:net
/ipv4
/fib_hash
.c
557 kmem_cache_free(fn_hash_kmem
, new_f
);
559 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ipv4
/fib_hash
.c
561 <<<<<<< HEAD
:net
/ipv4
/fib_hash
.c
564 kmem_cache_free(fn_hash_kmem
, new_f
);
565 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/ipv4
/fib_hash
.c
566 fib_release_info(fi
);
571 static int fn_hash_delete(struct fib_table
*tb
, struct fib_config
*cfg
)
573 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
575 struct fib_alias
*fa
, *fa_to_delete
;
579 if (cfg
->fc_dst_len
> 32)
582 if ((fz
= table
->fn_zones
[cfg
->fc_dst_len
]) == NULL
)
587 if (cfg
->fc_dst
& ~FZ_MASK(fz
))
589 key
= fz_key(cfg
->fc_dst
, fz
);
592 f
= fib_find_node(fz
, key
);
597 fa
= fib_find_alias(&f
->fn_alias
, cfg
->fc_tos
, 0);
602 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
603 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
604 struct fib_info
*fi
= fa
->fa_info
;
606 if (fa
->fa_tos
!= cfg
->fc_tos
)
609 if ((!cfg
->fc_type
||
610 fa
->fa_type
== cfg
->fc_type
) &&
611 (cfg
->fc_scope
== RT_SCOPE_NOWHERE
||
612 fa
->fa_scope
== cfg
->fc_scope
) &&
613 (!cfg
->fc_protocol
||
614 fi
->fib_protocol
== cfg
->fc_protocol
) &&
615 fib_nh_match(cfg
, fi
) == 0) {
625 rtmsg_fib(RTM_DELROUTE
, key
, fa
, cfg
->fc_dst_len
,
626 tb
->tb_id
, &cfg
->fc_nlinfo
, 0);
629 write_lock_bh(&fib_hash_lock
);
630 list_del(&fa
->fa_list
);
631 if (list_empty(&f
->fn_alias
)) {
632 hlist_del(&f
->fn_hash
);
636 write_unlock_bh(&fib_hash_lock
);
638 if (fa
->fa_state
& FA_S_ACCESSED
)
640 fn_free_alias(fa
, f
);
651 static int fn_flush_list(struct fn_zone
*fz
, int idx
)
653 struct hlist_head
*head
= &fz
->fz_hash
[idx
];
654 struct hlist_node
*node
, *n
;
658 hlist_for_each_entry_safe(f
, node
, n
, head
, fn_hash
) {
659 struct fib_alias
*fa
, *fa_node
;
663 list_for_each_entry_safe(fa
, fa_node
, &f
->fn_alias
, fa_list
) {
664 struct fib_info
*fi
= fa
->fa_info
;
666 if (fi
&& (fi
->fib_flags
&RTNH_F_DEAD
)) {
667 write_lock_bh(&fib_hash_lock
);
668 list_del(&fa
->fa_list
);
669 if (list_empty(&f
->fn_alias
)) {
670 hlist_del(&f
->fn_hash
);
674 write_unlock_bh(&fib_hash_lock
);
676 fn_free_alias(fa
, f
);
688 static int fn_hash_flush(struct fib_table
*tb
)
690 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
694 for (fz
= table
->fn_zone_list
; fz
; fz
= fz
->fz_next
) {
697 for (i
= fz
->fz_divisor
- 1; i
>= 0; i
--)
698 found
+= fn_flush_list(fz
, i
);
705 fn_hash_dump_bucket(struct sk_buff
*skb
, struct netlink_callback
*cb
,
706 struct fib_table
*tb
,
708 struct hlist_head
*head
)
710 struct hlist_node
*node
;
716 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
717 struct fib_alias
*fa
;
719 list_for_each_entry(fa
, &f
->fn_alias
, fa_list
) {
723 if (fib_dump_info(skb
, NETLINK_CB(cb
->skb
).pid
,
746 fn_hash_dump_zone(struct sk_buff
*skb
, struct netlink_callback
*cb
,
747 struct fib_table
*tb
,
752 if (fz
->fz_hash
== NULL
)
755 for (h
= s_h
; h
< fz
->fz_divisor
; h
++) {
756 if (hlist_empty(&fz
->fz_hash
[h
]))
758 if (fn_hash_dump_bucket(skb
, cb
, tb
, fz
, &fz
->fz_hash
[h
]) < 0) {
762 memset(&cb
->args
[4], 0,
763 sizeof(cb
->args
) - 4*sizeof(cb
->args
[0]));
769 static int fn_hash_dump(struct fib_table
*tb
, struct sk_buff
*skb
, struct netlink_callback
*cb
)
773 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
776 read_lock(&fib_hash_lock
);
777 for (fz
= table
->fn_zone_list
, m
=0; fz
; fz
= fz
->fz_next
, m
++) {
778 if (m
< s_m
) continue;
779 if (fn_hash_dump_zone(skb
, cb
, tb
, fz
) < 0) {
781 read_unlock(&fib_hash_lock
);
784 memset(&cb
->args
[3], 0,
785 sizeof(cb
->args
) - 3*sizeof(cb
->args
[0]));
787 read_unlock(&fib_hash_lock
);
792 void __init
fib_hash_init(void)
794 fn_hash_kmem
= kmem_cache_create("ip_fib_hash", sizeof(struct fib_node
),
795 0, SLAB_PANIC
, NULL
);
797 fn_alias_kmem
= kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias
),
798 0, SLAB_PANIC
, NULL
);
802 struct fib_table
*fib_hash_table(u32 id
)
804 struct fib_table
*tb
;
806 tb
= kmalloc(sizeof(struct fib_table
) + sizeof(struct fn_hash
),
813 tb
->tb_lookup
= fn_hash_lookup
;
814 tb
->tb_insert
= fn_hash_insert
;
815 tb
->tb_delete
= fn_hash_delete
;
816 tb
->tb_flush
= fn_hash_flush
;
817 tb
->tb_select_default
= fn_hash_select_default
;
818 tb
->tb_dump
= fn_hash_dump
;
819 memset(tb
->tb_data
, 0, sizeof(struct fn_hash
));
823 /* ------------------------------------------------------------------------ */
824 #ifdef CONFIG_PROC_FS
826 struct fib_iter_state
{
827 struct seq_net_private p
;
828 struct fn_zone
*zone
;
830 struct hlist_head
*hash_head
;
832 struct fib_alias
*fa
;
838 static struct fib_alias
*fib_get_first(struct seq_file
*seq
)
840 struct fib_iter_state
*iter
= seq
->private;
841 struct fib_table
*main_table
;
842 struct fn_hash
*table
;
844 main_table
= fib_get_table(iter
->p
.net
, RT_TABLE_MAIN
);
845 table
= (struct fn_hash
*)main_table
->tb_data
;
848 iter
->hash_head
= NULL
;
852 iter
->genid
= fib_hash_genid
;
855 for (iter
->zone
= table
->fn_zone_list
; iter
->zone
;
856 iter
->zone
= iter
->zone
->fz_next
) {
859 if (!iter
->zone
->fz_nent
)
862 iter
->hash_head
= iter
->zone
->fz_hash
;
863 maxslot
= iter
->zone
->fz_divisor
;
865 for (iter
->bucket
= 0; iter
->bucket
< maxslot
;
866 ++iter
->bucket
, ++iter
->hash_head
) {
867 struct hlist_node
*node
;
870 hlist_for_each_entry(fn
,node
,iter
->hash_head
,fn_hash
) {
871 struct fib_alias
*fa
;
873 list_for_each_entry(fa
,&fn
->fn_alias
,fa_list
) {
885 static struct fib_alias
*fib_get_next(struct seq_file
*seq
)
887 struct fib_iter_state
*iter
= seq
->private;
889 struct fib_alias
*fa
;
891 /* Advance FA, if any. */
896 list_for_each_entry_continue(fa
, &fn
->fn_alias
, fa_list
) {
902 fa
= iter
->fa
= NULL
;
906 struct hlist_node
*node
= &fn
->fn_hash
;
907 hlist_for_each_entry_continue(fn
, node
, fn_hash
) {
910 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
917 fn
= iter
->fn
= NULL
;
919 /* Advance hash chain. */
924 struct hlist_node
*node
;
927 maxslot
= iter
->zone
->fz_divisor
;
929 while (++iter
->bucket
< maxslot
) {
932 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
933 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
941 iter
->zone
= iter
->zone
->fz_next
;
947 iter
->hash_head
= iter
->zone
->fz_hash
;
949 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
950 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
962 static struct fib_alias
*fib_get_idx(struct seq_file
*seq
, loff_t pos
)
964 struct fib_iter_state
*iter
= seq
->private;
965 struct fib_alias
*fa
;
967 if (iter
->valid
&& pos
>= iter
->pos
&& iter
->genid
== fib_hash_genid
) {
971 fa
= fib_get_first(seq
);
974 while (pos
&& (fa
= fib_get_next(seq
)))
976 return pos
? NULL
: fa
;
979 static void *fib_seq_start(struct seq_file
*seq
, loff_t
*pos
)
980 __acquires(fib_hash_lock
)
982 struct fib_iter_state
*iter
= seq
->private;
985 read_lock(&fib_hash_lock
);
986 if (fib_get_table(iter
->p
.net
, RT_TABLE_MAIN
))
987 v
= *pos
? fib_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
991 static void *fib_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
994 return v
== SEQ_START_TOKEN
? fib_get_first(seq
) : fib_get_next(seq
);
997 static void fib_seq_stop(struct seq_file
*seq
, void *v
)
998 __releases(fib_hash_lock
)
1000 read_unlock(&fib_hash_lock
);
1003 static unsigned fib_flag_trans(int type
, __be32 mask
, struct fib_info
*fi
)
1005 static const unsigned type2flags
[RTN_MAX
+ 1] = {
1006 [7] = RTF_REJECT
, [8] = RTF_REJECT
,
1008 unsigned flags
= type2flags
[type
];
1010 if (fi
&& fi
->fib_nh
->nh_gw
)
1011 flags
|= RTF_GATEWAY
;
1012 if (mask
== htonl(0xFFFFFFFF))
1019 * This outputs /proc/net/route.
1021 * It always works in backward compatibility mode.
1022 * The format of the file is not supposed to be changed.
1024 static int fib_seq_show(struct seq_file
*seq
, void *v
)
1026 struct fib_iter_state
*iter
;
1028 __be32 prefix
, mask
;
1031 struct fib_alias
*fa
;
1032 struct fib_info
*fi
;
1034 if (v
== SEQ_START_TOKEN
) {
1035 seq_printf(seq
, "%-127s\n", "Iface\tDestination\tGateway "
1036 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1041 iter
= seq
->private;
1046 mask
= FZ_MASK(iter
->zone
);
1047 flags
= fib_flag_trans(fa
->fa_type
, mask
, fi
);
1049 snprintf(bf
, sizeof(bf
),
1050 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1051 fi
->fib_dev
? fi
->fib_dev
->name
: "*", prefix
,
1052 fi
->fib_nh
->nh_gw
, flags
, 0, 0, fi
->fib_priority
,
1053 mask
, (fi
->fib_advmss
? fi
->fib_advmss
+ 40 : 0),
1057 snprintf(bf
, sizeof(bf
),
1058 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1059 prefix
, 0, flags
, 0, 0, 0, mask
, 0, 0, 0);
1060 seq_printf(seq
, "%-127s\n", bf
);
1065 static const struct seq_operations fib_seq_ops
= {
1066 .start
= fib_seq_start
,
1067 .next
= fib_seq_next
,
1068 .stop
= fib_seq_stop
,
1069 .show
= fib_seq_show
,
1072 static int fib_seq_open(struct inode
*inode
, struct file
*file
)
1074 return seq_open_net(inode
, file
, &fib_seq_ops
,
1075 sizeof(struct fib_iter_state
));
1078 static const struct file_operations fib_seq_fops
= {
1079 .owner
= THIS_MODULE
,
1080 .open
= fib_seq_open
,
1082 .llseek
= seq_lseek
,
1083 .release
= seq_release_net
,
1086 int __net_init
fib_proc_init(struct net
*net
)
1088 if (!proc_net_fops_create(net
, "route", S_IRUGO
, &fib_seq_fops
))
1093 void __net_exit
fib_proc_exit(struct net
*net
)
1095 proc_net_remove(net
, "route");
1097 #endif /* CONFIG_PROC_FS */