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
)
462 /* Error if we find a perfect match which
463 * uses the same scope, type, and nexthop
467 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
468 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
469 if (fa
->fa_tos
!= tos
)
471 if (fa
->fa_info
->fib_priority
!= fi
->fib_priority
)
473 if (fa
->fa_type
== cfg
->fc_type
&&
474 fa
->fa_scope
== cfg
->fc_scope
&&
478 if (!(cfg
->fc_nlflags
& NLM_F_APPEND
))
483 if (!(cfg
->fc_nlflags
& NLM_F_CREATE
))
487 new_fa
= kmem_cache_alloc(fn_alias_kmem
, GFP_KERNEL
);
493 new_f
= kmem_cache_alloc(fn_hash_kmem
, GFP_KERNEL
);
495 goto out_free_new_fa
;
497 INIT_HLIST_NODE(&new_f
->fn_hash
);
498 INIT_LIST_HEAD(&new_f
->fn_alias
);
503 new_fa
->fa_info
= fi
;
504 new_fa
->fa_tos
= tos
;
505 new_fa
->fa_type
= cfg
->fc_type
;
506 new_fa
->fa_scope
= cfg
->fc_scope
;
507 new_fa
->fa_state
= 0;
510 * Insert new entry to the list.
513 write_lock_bh(&fib_hash_lock
);
515 fib_insert_node(fz
, new_f
);
516 list_add_tail(&new_fa
->fa_list
,
517 (fa
? &fa
->fa_list
: &f
->fn_alias
));
519 write_unlock_bh(&fib_hash_lock
);
525 rtmsg_fib(RTM_NEWROUTE
, key
, new_fa
, cfg
->fc_dst_len
, tb
->tb_id
,
530 kmem_cache_free(fn_alias_kmem
, new_fa
);
532 fib_release_info(fi
);
537 static int fn_hash_delete(struct fib_table
*tb
, struct fib_config
*cfg
)
539 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
541 struct fib_alias
*fa
, *fa_to_delete
;
545 if (cfg
->fc_dst_len
> 32)
548 if ((fz
= table
->fn_zones
[cfg
->fc_dst_len
]) == NULL
)
553 if (cfg
->fc_dst
& ~FZ_MASK(fz
))
555 key
= fz_key(cfg
->fc_dst
, fz
);
558 f
= fib_find_node(fz
, key
);
563 fa
= fib_find_alias(&f
->fn_alias
, cfg
->fc_tos
, 0);
568 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
569 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
570 struct fib_info
*fi
= fa
->fa_info
;
572 if (fa
->fa_tos
!= cfg
->fc_tos
)
575 if ((!cfg
->fc_type
||
576 fa
->fa_type
== cfg
->fc_type
) &&
577 (cfg
->fc_scope
== RT_SCOPE_NOWHERE
||
578 fa
->fa_scope
== cfg
->fc_scope
) &&
579 (!cfg
->fc_protocol
||
580 fi
->fib_protocol
== cfg
->fc_protocol
) &&
581 fib_nh_match(cfg
, fi
) == 0) {
591 rtmsg_fib(RTM_DELROUTE
, key
, fa
, cfg
->fc_dst_len
,
592 tb
->tb_id
, &cfg
->fc_nlinfo
);
595 write_lock_bh(&fib_hash_lock
);
596 list_del(&fa
->fa_list
);
597 if (list_empty(&f
->fn_alias
)) {
598 hlist_del(&f
->fn_hash
);
602 write_unlock_bh(&fib_hash_lock
);
604 if (fa
->fa_state
& FA_S_ACCESSED
)
617 static int fn_flush_list(struct fn_zone
*fz
, int idx
)
619 struct hlist_head
*head
= &fz
->fz_hash
[idx
];
620 struct hlist_node
*node
, *n
;
624 hlist_for_each_entry_safe(f
, node
, n
, head
, fn_hash
) {
625 struct fib_alias
*fa
, *fa_node
;
629 list_for_each_entry_safe(fa
, fa_node
, &f
->fn_alias
, fa_list
) {
630 struct fib_info
*fi
= fa
->fa_info
;
632 if (fi
&& (fi
->fib_flags
&RTNH_F_DEAD
)) {
633 write_lock_bh(&fib_hash_lock
);
634 list_del(&fa
->fa_list
);
635 if (list_empty(&f
->fn_alias
)) {
636 hlist_del(&f
->fn_hash
);
640 write_unlock_bh(&fib_hash_lock
);
654 static int fn_hash_flush(struct fib_table
*tb
)
656 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
660 for (fz
= table
->fn_zone_list
; fz
; fz
= fz
->fz_next
) {
663 for (i
= fz
->fz_divisor
- 1; i
>= 0; i
--)
664 found
+= fn_flush_list(fz
, i
);
671 fn_hash_dump_bucket(struct sk_buff
*skb
, struct netlink_callback
*cb
,
672 struct fib_table
*tb
,
674 struct hlist_head
*head
)
676 struct hlist_node
*node
;
682 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
683 struct fib_alias
*fa
;
685 list_for_each_entry(fa
, &f
->fn_alias
, fa_list
) {
689 if (fib_dump_info(skb
, NETLINK_CB(cb
->skb
).pid
,
712 fn_hash_dump_zone(struct sk_buff
*skb
, struct netlink_callback
*cb
,
713 struct fib_table
*tb
,
719 for (h
=0; h
< fz
->fz_divisor
; h
++) {
720 if (h
< s_h
) continue;
722 memset(&cb
->args
[4], 0,
723 sizeof(cb
->args
) - 4*sizeof(cb
->args
[0]));
724 if (fz
->fz_hash
== NULL
||
725 hlist_empty(&fz
->fz_hash
[h
]))
727 if (fn_hash_dump_bucket(skb
, cb
, tb
, fz
, &fz
->fz_hash
[h
])<0) {
736 static int fn_hash_dump(struct fib_table
*tb
, struct sk_buff
*skb
, struct netlink_callback
*cb
)
740 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
743 read_lock(&fib_hash_lock
);
744 for (fz
= table
->fn_zone_list
, m
=0; fz
; fz
= fz
->fz_next
, m
++) {
745 if (m
< s_m
) continue;
747 memset(&cb
->args
[3], 0,
748 sizeof(cb
->args
) - 3*sizeof(cb
->args
[0]));
749 if (fn_hash_dump_zone(skb
, cb
, tb
, fz
) < 0) {
751 read_unlock(&fib_hash_lock
);
755 read_unlock(&fib_hash_lock
);
760 #ifdef CONFIG_IP_MULTIPLE_TABLES
761 struct fib_table
* fib_hash_init(u32 id
)
763 struct fib_table
* __init
fib_hash_init(u32 id
)
766 struct fib_table
*tb
;
768 if (fn_hash_kmem
== NULL
)
769 fn_hash_kmem
= kmem_cache_create("ip_fib_hash",
770 sizeof(struct fib_node
),
771 0, SLAB_HWCACHE_ALIGN
,
774 if (fn_alias_kmem
== NULL
)
775 fn_alias_kmem
= kmem_cache_create("ip_fib_alias",
776 sizeof(struct fib_alias
),
777 0, SLAB_HWCACHE_ALIGN
,
780 tb
= kmalloc(sizeof(struct fib_table
) + sizeof(struct fn_hash
),
786 tb
->tb_lookup
= fn_hash_lookup
;
787 tb
->tb_insert
= fn_hash_insert
;
788 tb
->tb_delete
= fn_hash_delete
;
789 tb
->tb_flush
= fn_hash_flush
;
790 tb
->tb_select_default
= fn_hash_select_default
;
791 tb
->tb_dump
= fn_hash_dump
;
792 memset(tb
->tb_data
, 0, sizeof(struct fn_hash
));
796 /* ------------------------------------------------------------------------ */
797 #ifdef CONFIG_PROC_FS
799 struct fib_iter_state
{
800 struct fn_zone
*zone
;
802 struct hlist_head
*hash_head
;
804 struct fib_alias
*fa
;
810 static struct fib_alias
*fib_get_first(struct seq_file
*seq
)
812 struct fib_iter_state
*iter
= seq
->private;
813 struct fn_hash
*table
= (struct fn_hash
*) ip_fib_main_table
->tb_data
;
816 iter
->hash_head
= NULL
;
820 iter
->genid
= fib_hash_genid
;
823 for (iter
->zone
= table
->fn_zone_list
; iter
->zone
;
824 iter
->zone
= iter
->zone
->fz_next
) {
827 if (!iter
->zone
->fz_nent
)
830 iter
->hash_head
= iter
->zone
->fz_hash
;
831 maxslot
= iter
->zone
->fz_divisor
;
833 for (iter
->bucket
= 0; iter
->bucket
< maxslot
;
834 ++iter
->bucket
, ++iter
->hash_head
) {
835 struct hlist_node
*node
;
838 hlist_for_each_entry(fn
,node
,iter
->hash_head
,fn_hash
) {
839 struct fib_alias
*fa
;
841 list_for_each_entry(fa
,&fn
->fn_alias
,fa_list
) {
853 static struct fib_alias
*fib_get_next(struct seq_file
*seq
)
855 struct fib_iter_state
*iter
= seq
->private;
857 struct fib_alias
*fa
;
859 /* Advance FA, if any. */
864 list_for_each_entry_continue(fa
, &fn
->fn_alias
, fa_list
) {
870 fa
= iter
->fa
= NULL
;
874 struct hlist_node
*node
= &fn
->fn_hash
;
875 hlist_for_each_entry_continue(fn
, node
, fn_hash
) {
878 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
885 fn
= iter
->fn
= NULL
;
887 /* Advance hash chain. */
892 struct hlist_node
*node
;
895 maxslot
= iter
->zone
->fz_divisor
;
897 while (++iter
->bucket
< maxslot
) {
900 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
901 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
909 iter
->zone
= iter
->zone
->fz_next
;
915 iter
->hash_head
= iter
->zone
->fz_hash
;
917 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
918 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
930 static struct fib_alias
*fib_get_idx(struct seq_file
*seq
, loff_t pos
)
932 struct fib_iter_state
*iter
= seq
->private;
933 struct fib_alias
*fa
;
935 if (iter
->valid
&& pos
>= iter
->pos
&& iter
->genid
== fib_hash_genid
) {
939 fa
= fib_get_first(seq
);
942 while (pos
&& (fa
= fib_get_next(seq
)))
944 return pos
? NULL
: fa
;
947 static void *fib_seq_start(struct seq_file
*seq
, loff_t
*pos
)
951 read_lock(&fib_hash_lock
);
952 if (ip_fib_main_table
)
953 v
= *pos
? fib_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
957 static void *fib_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
960 return v
== SEQ_START_TOKEN
? fib_get_first(seq
) : fib_get_next(seq
);
963 static void fib_seq_stop(struct seq_file
*seq
, void *v
)
965 read_unlock(&fib_hash_lock
);
968 static unsigned fib_flag_trans(int type
, __be32 mask
, struct fib_info
*fi
)
970 static const unsigned type2flags
[RTN_MAX
+ 1] = {
971 [7] = RTF_REJECT
, [8] = RTF_REJECT
,
973 unsigned flags
= type2flags
[type
];
975 if (fi
&& fi
->fib_nh
->nh_gw
)
976 flags
|= RTF_GATEWAY
;
977 if (mask
== htonl(0xFFFFFFFF))
984 * This outputs /proc/net/route.
986 * It always works in backward compatibility mode.
987 * The format of the file is not supposed to be changed.
989 static int fib_seq_show(struct seq_file
*seq
, void *v
)
991 struct fib_iter_state
*iter
;
996 struct fib_alias
*fa
;
999 if (v
== SEQ_START_TOKEN
) {
1000 seq_printf(seq
, "%-127s\n", "Iface\tDestination\tGateway "
1001 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1006 iter
= seq
->private;
1011 mask
= FZ_MASK(iter
->zone
);
1012 flags
= fib_flag_trans(fa
->fa_type
, mask
, fi
);
1014 snprintf(bf
, sizeof(bf
),
1015 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1016 fi
->fib_dev
? fi
->fib_dev
->name
: "*", prefix
,
1017 fi
->fib_nh
->nh_gw
, flags
, 0, 0, fi
->fib_priority
,
1018 mask
, (fi
->fib_advmss
? fi
->fib_advmss
+ 40 : 0),
1022 snprintf(bf
, sizeof(bf
),
1023 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1024 prefix
, 0, flags
, 0, 0, 0, mask
, 0, 0, 0);
1025 seq_printf(seq
, "%-127s\n", bf
);
1030 static const struct seq_operations fib_seq_ops
= {
1031 .start
= fib_seq_start
,
1032 .next
= fib_seq_next
,
1033 .stop
= fib_seq_stop
,
1034 .show
= fib_seq_show
,
1037 static int fib_seq_open(struct inode
*inode
, struct file
*file
)
1039 struct seq_file
*seq
;
1041 struct fib_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
1046 rc
= seq_open(file
, &fib_seq_ops
);
1050 seq
= file
->private_data
;
1059 static const struct file_operations fib_seq_fops
= {
1060 .owner
= THIS_MODULE
,
1061 .open
= fib_seq_open
,
1063 .llseek
= seq_lseek
,
1064 .release
= seq_release_private
,
1067 int __init
fib_proc_init(void)
1069 if (!proc_net_fops_create("route", S_IRUGO
, &fib_seq_fops
))
1074 void __init
fib_proc_exit(void)
1076 proc_net_remove("route");
1078 #endif /* CONFIG_PROC_FS */