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 <linux/config.h>
19 #include <asm/uaccess.h>
20 #include <asm/system.h>
21 #include <linux/bitops.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/errno.h>
31 #include <linux/inet.h>
32 #include <linux/inetdevice.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/proc_fs.h>
36 #include <linux/skbuff.h>
37 #include <linux/netlink.h>
38 #include <linux/init.h>
41 #include <net/protocol.h>
42 #include <net/route.h>
45 #include <net/ip_fib.h>
47 #include "fib_lookup.h"
49 static kmem_cache_t
*fn_hash_kmem __read_mostly
;
50 static kmem_cache_t
*fn_alias_kmem __read_mostly
;
53 struct hlist_node fn_hash
;
54 struct list_head fn_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(u32 key
, struct fn_zone
*fz
)
83 u32 h
= ntohl(key
)>>(32 - fz
->fz_order
);
91 static inline u32
fz_key(u32 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 kmalloc(size
, GFP_KERNEL
);
108 return (struct hlist_head
*)
109 __get_free_pages(GFP_KERNEL
, 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("fn_rehash_zone: hash for zone %d grows from %d\n", fz
->fz_order
, old_divisor
);
175 ht
= fz_hash_alloc(new_divisor
);
178 memset(ht
, 0, new_divisor
* sizeof(struct hlist_head
));
180 write_lock_bh(&fib_hash_lock
);
181 old_ht
= fz
->fz_hash
;
183 fz
->fz_hashmask
= new_hashmask
;
184 fz
->fz_divisor
= new_divisor
;
185 fn_rebuild_zone(fz
, old_ht
, old_divisor
);
187 write_unlock_bh(&fib_hash_lock
);
189 fz_hash_free(old_ht
, old_divisor
);
193 static inline void fn_free_node(struct fib_node
* f
)
195 kmem_cache_free(fn_hash_kmem
, f
);
198 static inline void fn_free_alias(struct fib_alias
*fa
)
200 fib_release_info(fa
->fa_info
);
201 kmem_cache_free(fn_alias_kmem
, fa
);
204 static struct fn_zone
*
205 fn_new_zone(struct fn_hash
*table
, int z
)
208 struct fn_zone
*fz
= kmalloc(sizeof(struct fn_zone
), GFP_KERNEL
);
212 memset(fz
, 0, sizeof(struct fn_zone
));
218 fz
->fz_hashmask
= (fz
->fz_divisor
- 1);
219 fz
->fz_hash
= fz_hash_alloc(fz
->fz_divisor
);
224 memset(fz
->fz_hash
, 0, fz
->fz_divisor
* sizeof(struct hlist_head
*));
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 u32 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
);
280 static int fn_hash_last_dflt
=-1;
283 fn_hash_select_default(struct fib_table
*tb
, const struct flowi
*flp
, struct fib_result
*res
)
286 struct hlist_node
*node
;
288 struct fib_info
*fi
= NULL
;
289 struct fib_info
*last_resort
;
290 struct fn_hash
*t
= (struct fn_hash
*)tb
->tb_data
;
291 struct fn_zone
*fz
= t
->fn_zones
[0];
300 read_lock(&fib_hash_lock
);
301 hlist_for_each_entry(f
, node
, &fz
->fz_hash
[0], fn_hash
) {
302 struct fib_alias
*fa
;
304 list_for_each_entry(fa
, &f
->fn_alias
, fa_list
) {
305 struct fib_info
*next_fi
= fa
->fa_info
;
307 if (fa
->fa_scope
!= res
->scope
||
308 fa
->fa_type
!= RTN_UNICAST
)
311 if (next_fi
->fib_priority
> res
->fi
->fib_priority
)
313 if (!next_fi
->fib_nh
[0].nh_gw
||
314 next_fi
->fib_nh
[0].nh_scope
!= RT_SCOPE_LINK
)
316 fa
->fa_state
|= FA_S_ACCESSED
;
319 if (next_fi
!= res
->fi
)
321 } else if (!fib_detect_death(fi
, order
, &last_resort
,
322 &last_idx
, &fn_hash_last_dflt
)) {
324 fib_info_put(res
->fi
);
326 atomic_inc(&fi
->fib_clntref
);
327 fn_hash_last_dflt
= order
;
335 if (order
<= 0 || fi
== NULL
) {
336 fn_hash_last_dflt
= -1;
340 if (!fib_detect_death(fi
, order
, &last_resort
, &last_idx
, &fn_hash_last_dflt
)) {
342 fib_info_put(res
->fi
);
344 atomic_inc(&fi
->fib_clntref
);
345 fn_hash_last_dflt
= order
;
351 fib_info_put(res
->fi
);
352 res
->fi
= last_resort
;
354 atomic_inc(&last_resort
->fib_clntref
);
356 fn_hash_last_dflt
= last_idx
;
358 read_unlock(&fib_hash_lock
);
361 /* Insert node F to FZ. */
362 static inline void fib_insert_node(struct fn_zone
*fz
, struct fib_node
*f
)
364 struct hlist_head
*head
= &fz
->fz_hash
[fn_hash(f
->fn_key
, fz
)];
366 hlist_add_head(&f
->fn_hash
, head
);
369 /* Return the node in FZ matching KEY. */
370 static struct fib_node
*fib_find_node(struct fn_zone
*fz
, u32 key
)
372 struct hlist_head
*head
= &fz
->fz_hash
[fn_hash(key
, fz
)];
373 struct hlist_node
*node
;
376 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
377 if (f
->fn_key
== key
)
385 fn_hash_insert(struct fib_table
*tb
, struct rtmsg
*r
, struct kern_rta
*rta
,
386 struct nlmsghdr
*n
, struct netlink_skb_parms
*req
)
388 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
389 struct fib_node
*new_f
, *f
;
390 struct fib_alias
*fa
, *new_fa
;
393 int z
= r
->rtm_dst_len
;
394 int type
= r
->rtm_type
;
401 fz
= table
->fn_zones
[z
];
402 if (!fz
&& !(fz
= fn_new_zone(table
, z
)))
408 memcpy(&dst
, rta
->rta_dst
, 4);
409 if (dst
& ~FZ_MASK(fz
))
411 key
= fz_key(dst
, fz
);
414 if ((fi
= fib_create_info(r
, rta
, n
, &err
)) == NULL
)
417 if (fz
->fz_nent
> (fz
->fz_divisor
<<1) &&
418 fz
->fz_divisor
< FZ_MAX_DIVISOR
&&
419 (z
==32 || (1<<z
) > fz
->fz_divisor
))
422 f
= fib_find_node(fz
, key
);
427 fa
= fib_find_alias(&f
->fn_alias
, tos
, fi
->fib_priority
);
429 /* Now fa, if non-NULL, points to the first fib alias
430 * with the same keys [prefix,tos,priority], if such key already
431 * exists or to the node before which we will insert new one.
433 * If fa is NULL, we will need to allocate a new one and
434 * insert to the head of f.
436 * If f is NULL, no fib node matched the destination key
437 * and we need to allocate a new one of those as well.
440 if (fa
&& fa
->fa_tos
== tos
&&
441 fa
->fa_info
->fib_priority
== fi
->fib_priority
) {
442 struct fib_alias
*fa_orig
;
445 if (n
->nlmsg_flags
& NLM_F_EXCL
)
448 if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
449 struct fib_info
*fi_drop
;
452 write_lock_bh(&fib_hash_lock
);
453 fi_drop
= fa
->fa_info
;
456 fa
->fa_scope
= r
->rtm_scope
;
457 state
= fa
->fa_state
;
458 fa
->fa_state
&= ~FA_S_ACCESSED
;
460 write_unlock_bh(&fib_hash_lock
);
462 fib_release_info(fi_drop
);
463 if (state
& FA_S_ACCESSED
)
468 /* Error if we find a perfect match which
469 * uses the same scope, type, and nexthop
473 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
474 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
475 if (fa
->fa_tos
!= tos
)
477 if (fa
->fa_info
->fib_priority
!= fi
->fib_priority
)
479 if (fa
->fa_type
== type
&&
480 fa
->fa_scope
== r
->rtm_scope
&&
484 if (!(n
->nlmsg_flags
& NLM_F_APPEND
))
489 if (!(n
->nlmsg_flags
&NLM_F_CREATE
))
493 new_fa
= kmem_cache_alloc(fn_alias_kmem
, SLAB_KERNEL
);
499 new_f
= kmem_cache_alloc(fn_hash_kmem
, SLAB_KERNEL
);
501 goto out_free_new_fa
;
503 INIT_HLIST_NODE(&new_f
->fn_hash
);
504 INIT_LIST_HEAD(&new_f
->fn_alias
);
509 new_fa
->fa_info
= fi
;
510 new_fa
->fa_tos
= tos
;
511 new_fa
->fa_type
= type
;
512 new_fa
->fa_scope
= r
->rtm_scope
;
513 new_fa
->fa_state
= 0;
516 * Insert new entry to the list.
519 write_lock_bh(&fib_hash_lock
);
521 fib_insert_node(fz
, new_f
);
522 list_add_tail(&new_fa
->fa_list
,
523 (fa
? &fa
->fa_list
: &f
->fn_alias
));
525 write_unlock_bh(&fib_hash_lock
);
531 rtmsg_fib(RTM_NEWROUTE
, key
, new_fa
, z
, tb
->tb_id
, n
, req
);
535 kmem_cache_free(fn_alias_kmem
, new_fa
);
537 fib_release_info(fi
);
543 fn_hash_delete(struct fib_table
*tb
, struct rtmsg
*r
, struct kern_rta
*rta
,
544 struct nlmsghdr
*n
, struct netlink_skb_parms
*req
)
546 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
548 struct fib_alias
*fa
, *fa_to_delete
;
549 int z
= r
->rtm_dst_len
;
556 if ((fz
= table
->fn_zones
[z
]) == NULL
)
562 memcpy(&dst
, rta
->rta_dst
, 4);
563 if (dst
& ~FZ_MASK(fz
))
565 key
= fz_key(dst
, fz
);
568 f
= fib_find_node(fz
, key
);
573 fa
= fib_find_alias(&f
->fn_alias
, tos
, 0);
578 fa
= list_entry(fa
->fa_list
.prev
, struct fib_alias
, fa_list
);
579 list_for_each_entry_continue(fa
, &f
->fn_alias
, fa_list
) {
580 struct fib_info
*fi
= fa
->fa_info
;
582 if (fa
->fa_tos
!= tos
)
586 fa
->fa_type
== r
->rtm_type
) &&
587 (r
->rtm_scope
== RT_SCOPE_NOWHERE
||
588 fa
->fa_scope
== r
->rtm_scope
) &&
590 fi
->fib_protocol
== r
->rtm_protocol
) &&
591 fib_nh_match(r
, n
, rta
, fi
) == 0) {
601 rtmsg_fib(RTM_DELROUTE
, key
, fa
, z
, tb
->tb_id
, n
, req
);
604 write_lock_bh(&fib_hash_lock
);
605 list_del(&fa
->fa_list
);
606 if (list_empty(&f
->fn_alias
)) {
607 hlist_del(&f
->fn_hash
);
611 write_unlock_bh(&fib_hash_lock
);
613 if (fa
->fa_state
& FA_S_ACCESSED
)
626 static int fn_flush_list(struct fn_zone
*fz
, int idx
)
628 struct hlist_head
*head
= &fz
->fz_hash
[idx
];
629 struct hlist_node
*node
, *n
;
633 hlist_for_each_entry_safe(f
, node
, n
, head
, fn_hash
) {
634 struct fib_alias
*fa
, *fa_node
;
638 list_for_each_entry_safe(fa
, fa_node
, &f
->fn_alias
, fa_list
) {
639 struct fib_info
*fi
= fa
->fa_info
;
641 if (fi
&& (fi
->fib_flags
&RTNH_F_DEAD
)) {
642 write_lock_bh(&fib_hash_lock
);
643 list_del(&fa
->fa_list
);
644 if (list_empty(&f
->fn_alias
)) {
645 hlist_del(&f
->fn_hash
);
649 write_unlock_bh(&fib_hash_lock
);
663 static int fn_hash_flush(struct fib_table
*tb
)
665 struct fn_hash
*table
= (struct fn_hash
*) tb
->tb_data
;
669 for (fz
= table
->fn_zone_list
; fz
; fz
= fz
->fz_next
) {
672 for (i
= fz
->fz_divisor
- 1; i
>= 0; i
--)
673 found
+= fn_flush_list(fz
, i
);
680 fn_hash_dump_bucket(struct sk_buff
*skb
, struct netlink_callback
*cb
,
681 struct fib_table
*tb
,
683 struct hlist_head
*head
)
685 struct hlist_node
*node
;
691 hlist_for_each_entry(f
, node
, head
, fn_hash
) {
692 struct fib_alias
*fa
;
694 list_for_each_entry(fa
, &f
->fn_alias
, fa_list
) {
698 if (fib_dump_info(skb
, NETLINK_CB(cb
->skb
).pid
,
721 fn_hash_dump_zone(struct sk_buff
*skb
, struct netlink_callback
*cb
,
722 struct fib_table
*tb
,
728 for (h
=0; h
< fz
->fz_divisor
; h
++) {
729 if (h
< s_h
) continue;
731 memset(&cb
->args
[3], 0,
732 sizeof(cb
->args
) - 3*sizeof(cb
->args
[0]));
733 if (fz
->fz_hash
== NULL
||
734 hlist_empty(&fz
->fz_hash
[h
]))
736 if (fn_hash_dump_bucket(skb
, cb
, tb
, fz
, &fz
->fz_hash
[h
])<0) {
745 static int fn_hash_dump(struct fib_table
*tb
, struct sk_buff
*skb
, struct netlink_callback
*cb
)
749 struct fn_hash
*table
= (struct fn_hash
*)tb
->tb_data
;
752 read_lock(&fib_hash_lock
);
753 for (fz
= table
->fn_zone_list
, m
=0; fz
; fz
= fz
->fz_next
, m
++) {
754 if (m
< s_m
) continue;
756 memset(&cb
->args
[2], 0,
757 sizeof(cb
->args
) - 2*sizeof(cb
->args
[0]));
758 if (fn_hash_dump_zone(skb
, cb
, tb
, fz
) < 0) {
760 read_unlock(&fib_hash_lock
);
764 read_unlock(&fib_hash_lock
);
769 #ifdef CONFIG_IP_MULTIPLE_TABLES
770 struct fib_table
* fib_hash_init(int id
)
772 struct fib_table
* __init
fib_hash_init(int id
)
775 struct fib_table
*tb
;
777 if (fn_hash_kmem
== NULL
)
778 fn_hash_kmem
= kmem_cache_create("ip_fib_hash",
779 sizeof(struct fib_node
),
780 0, SLAB_HWCACHE_ALIGN
,
783 if (fn_alias_kmem
== NULL
)
784 fn_alias_kmem
= kmem_cache_create("ip_fib_alias",
785 sizeof(struct fib_alias
),
786 0, SLAB_HWCACHE_ALIGN
,
789 tb
= kmalloc(sizeof(struct fib_table
) + sizeof(struct fn_hash
),
795 tb
->tb_lookup
= fn_hash_lookup
;
796 tb
->tb_insert
= fn_hash_insert
;
797 tb
->tb_delete
= fn_hash_delete
;
798 tb
->tb_flush
= fn_hash_flush
;
799 tb
->tb_select_default
= fn_hash_select_default
;
800 tb
->tb_dump
= fn_hash_dump
;
801 memset(tb
->tb_data
, 0, sizeof(struct fn_hash
));
805 /* ------------------------------------------------------------------------ */
806 #ifdef CONFIG_PROC_FS
808 struct fib_iter_state
{
809 struct fn_zone
*zone
;
811 struct hlist_head
*hash_head
;
813 struct fib_alias
*fa
;
819 static struct fib_alias
*fib_get_first(struct seq_file
*seq
)
821 struct fib_iter_state
*iter
= seq
->private;
822 struct fn_hash
*table
= (struct fn_hash
*) ip_fib_main_table
->tb_data
;
825 iter
->hash_head
= NULL
;
829 iter
->genid
= fib_hash_genid
;
832 for (iter
->zone
= table
->fn_zone_list
; iter
->zone
;
833 iter
->zone
= iter
->zone
->fz_next
) {
836 if (!iter
->zone
->fz_nent
)
839 iter
->hash_head
= iter
->zone
->fz_hash
;
840 maxslot
= iter
->zone
->fz_divisor
;
842 for (iter
->bucket
= 0; iter
->bucket
< maxslot
;
843 ++iter
->bucket
, ++iter
->hash_head
) {
844 struct hlist_node
*node
;
847 hlist_for_each_entry(fn
,node
,iter
->hash_head
,fn_hash
) {
848 struct fib_alias
*fa
;
850 list_for_each_entry(fa
,&fn
->fn_alias
,fa_list
) {
862 static struct fib_alias
*fib_get_next(struct seq_file
*seq
)
864 struct fib_iter_state
*iter
= seq
->private;
866 struct fib_alias
*fa
;
868 /* Advance FA, if any. */
873 list_for_each_entry_continue(fa
, &fn
->fn_alias
, fa_list
) {
879 fa
= iter
->fa
= NULL
;
883 struct hlist_node
*node
= &fn
->fn_hash
;
884 hlist_for_each_entry_continue(fn
, node
, fn_hash
) {
887 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
894 fn
= iter
->fn
= NULL
;
896 /* Advance hash chain. */
901 struct hlist_node
*node
;
904 maxslot
= iter
->zone
->fz_divisor
;
906 while (++iter
->bucket
< maxslot
) {
909 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
910 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
918 iter
->zone
= iter
->zone
->fz_next
;
924 iter
->hash_head
= iter
->zone
->fz_hash
;
926 hlist_for_each_entry(fn
, node
, iter
->hash_head
, fn_hash
) {
927 list_for_each_entry(fa
, &fn
->fn_alias
, fa_list
) {
939 static struct fib_alias
*fib_get_idx(struct seq_file
*seq
, loff_t pos
)
941 struct fib_iter_state
*iter
= seq
->private;
942 struct fib_alias
*fa
;
944 if (iter
->valid
&& pos
>= iter
->pos
&& iter
->genid
== fib_hash_genid
) {
948 fa
= fib_get_first(seq
);
951 while (pos
&& (fa
= fib_get_next(seq
)))
953 return pos
? NULL
: fa
;
956 static void *fib_seq_start(struct seq_file
*seq
, loff_t
*pos
)
960 read_lock(&fib_hash_lock
);
961 if (ip_fib_main_table
)
962 v
= *pos
? fib_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
966 static void *fib_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
969 return v
== SEQ_START_TOKEN
? fib_get_first(seq
) : fib_get_next(seq
);
972 static void fib_seq_stop(struct seq_file
*seq
, void *v
)
974 read_unlock(&fib_hash_lock
);
977 static unsigned fib_flag_trans(int type
, u32 mask
, struct fib_info
*fi
)
979 static const unsigned type2flags
[RTN_MAX
+ 1] = {
980 [7] = RTF_REJECT
, [8] = RTF_REJECT
,
982 unsigned flags
= type2flags
[type
];
984 if (fi
&& fi
->fib_nh
->nh_gw
)
985 flags
|= RTF_GATEWAY
;
986 if (mask
== 0xFFFFFFFF)
993 * This outputs /proc/net/route.
995 * It always works in backward compatibility mode.
996 * The format of the file is not supposed to be changed.
998 static int fib_seq_show(struct seq_file
*seq
, void *v
)
1000 struct fib_iter_state
*iter
;
1005 struct fib_alias
*fa
;
1006 struct fib_info
*fi
;
1008 if (v
== SEQ_START_TOKEN
) {
1009 seq_printf(seq
, "%-127s\n", "Iface\tDestination\tGateway "
1010 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1015 iter
= seq
->private;
1020 mask
= FZ_MASK(iter
->zone
);
1021 flags
= fib_flag_trans(fa
->fa_type
, mask
, fi
);
1023 snprintf(bf
, sizeof(bf
),
1024 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1025 fi
->fib_dev
? fi
->fib_dev
->name
: "*", prefix
,
1026 fi
->fib_nh
->nh_gw
, flags
, 0, 0, fi
->fib_priority
,
1027 mask
, (fi
->fib_advmss
? fi
->fib_advmss
+ 40 : 0),
1031 snprintf(bf
, sizeof(bf
),
1032 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1033 prefix
, 0, flags
, 0, 0, 0, mask
, 0, 0, 0);
1034 seq_printf(seq
, "%-127s\n", bf
);
1039 static struct seq_operations fib_seq_ops
= {
1040 .start
= fib_seq_start
,
1041 .next
= fib_seq_next
,
1042 .stop
= fib_seq_stop
,
1043 .show
= fib_seq_show
,
1046 static int fib_seq_open(struct inode
*inode
, struct file
*file
)
1048 struct seq_file
*seq
;
1050 struct fib_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1055 rc
= seq_open(file
, &fib_seq_ops
);
1059 seq
= file
->private_data
;
1061 memset(s
, 0, sizeof(*s
));
1069 static struct file_operations fib_seq_fops
= {
1070 .owner
= THIS_MODULE
,
1071 .open
= fib_seq_open
,
1073 .llseek
= seq_lseek
,
1074 .release
= seq_release_private
,
1077 int __init
fib_proc_init(void)
1079 if (!proc_net_fops_create("route", S_IRUGO
, &fib_seq_fops
))
1084 void __init
fib_proc_exit(void)
1086 proc_net_remove("route");
1088 #endif /* CONFIG_PROC_FS */