fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / fib_hash.c
1 /*
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.
5  *
6  *              IPv4 FIB: lookup engine and maintenance routines.
7  *
8  * Version:     $Id: fib_hash.c,v 1.13 2001/10/31 21:55:54 davem Exp $
9  *
10  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11  *
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.
16  */
17
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>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/string.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/errno.h>
29 #include <linux/in.h>
30 #include <linux/inet.h>
31 #include <linux/inetdevice.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/proc_fs.h>
35 #include <linux/skbuff.h>
36 #include <linux/netlink.h>
37 #include <linux/init.h>
38 #include <linux/vs_context.h>
39
40 #include <net/ip.h>
41 #include <net/protocol.h>
42 #include <net/route.h>
43 #include <net/tcp.h>
44 #include <net/sock.h>
45 #include <net/ip_fib.h>
46
47 #include "fib_lookup.h"
48
49 static struct kmem_cache *fn_hash_kmem __read_mostly;
50 static struct kmem_cache *fn_alias_kmem __read_mostly;
51
52 struct fib_node {
53         struct hlist_node       fn_hash;
54         struct list_head        fn_alias;
55         __be32                  fn_key;
56 };
57
58 struct fn_zone {
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    */
62
63         int                     fz_divisor;     /* Hash divisor         */
64         u32                     fz_hashmask;    /* (fz_divisor - 1)     */
65 #define FZ_HASHMASK(fz)         ((fz)->fz_hashmask)
66
67         int                     fz_order;       /* Zone order           */
68         __be32                  fz_mask;
69 #define FZ_MASK(fz)             ((fz)->fz_mask)
70 };
71
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.
74  */
75
76 struct fn_hash {
77         struct fn_zone  *fn_zones[33];
78         struct fn_zone  *fn_zone_list;
79 };
80
81 static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
82 {
83         u32 h = ntohl(key)>>(32 - fz->fz_order);
84         h ^= (h>>20);
85         h ^= (h>>10);
86         h ^= (h>>5);
87         h &= FZ_HASHMASK(fz);
88         return h;
89 }
90
91 static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
92 {
93         return dst & FZ_MASK(fz);
94 }
95
96 static DEFINE_RWLOCK(fib_hash_lock);
97 static unsigned int fib_hash_genid;
98
99 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
100
101 static struct hlist_head *fz_hash_alloc(int divisor)
102 {
103         unsigned long size = divisor * sizeof(struct hlist_head);
104
105         if (size <= PAGE_SIZE) {
106                 return kmalloc(size, GFP_KERNEL);
107         } else {
108                 return (struct hlist_head *)
109                         __get_free_pages(GFP_KERNEL, get_order(size));
110         }
111 }
112
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,
116                                    int old_divisor)
117 {
118         int i;
119
120         for (i = 0; i < old_divisor; i++) {
121                 struct hlist_node *node, *n;
122                 struct fib_node *f;
123
124                 hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
125                         struct hlist_head *new_head;
126
127                         hlist_del(&f->fn_hash);
128
129                         new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
130                         hlist_add_head(&f->fn_hash, new_head);
131                 }
132         }
133 }
134
135 static void fz_hash_free(struct hlist_head *hash, int divisor)
136 {
137         unsigned long size = divisor * sizeof(struct hlist_head);
138
139         if (size <= PAGE_SIZE)
140                 kfree(hash);
141         else
142                 free_pages((unsigned long)hash, get_order(size));
143 }
144
145 static void fn_rehash_zone(struct fn_zone *fz)
146 {
147         struct hlist_head *ht, *old_ht;
148         int old_divisor, new_divisor;
149         u32 new_hashmask;
150                 
151         old_divisor = fz->fz_divisor;
152
153         switch (old_divisor) {
154         case 16:
155                 new_divisor = 256;
156                 break;
157         case 256:
158                 new_divisor = 1024;
159                 break;
160         default:
161                 if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
162                         printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
163                         return;
164                 }
165                 new_divisor = (old_divisor << 1);
166                 break;
167         }
168
169         new_hashmask = (new_divisor - 1);
170
171 #if RT_CACHE_DEBUG >= 2
172         printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
173 #endif
174
175         ht = fz_hash_alloc(new_divisor);
176
177         if (ht) {
178                 memset(ht, 0, new_divisor * sizeof(struct hlist_head));
179
180                 write_lock_bh(&fib_hash_lock);
181                 old_ht = fz->fz_hash;
182                 fz->fz_hash = ht;
183                 fz->fz_hashmask = new_hashmask;
184                 fz->fz_divisor = new_divisor;
185                 fn_rebuild_zone(fz, old_ht, old_divisor);
186                 fib_hash_genid++;
187                 write_unlock_bh(&fib_hash_lock);
188
189                 fz_hash_free(old_ht, old_divisor);
190         }
191 }
192
193 static inline void fn_free_node(struct fib_node * f)
194 {
195         kmem_cache_free(fn_hash_kmem, f);
196 }
197
198 static inline void fn_free_alias(struct fib_alias *fa)
199 {
200         fib_release_info(fa->fa_info);
201         kmem_cache_free(fn_alias_kmem, fa);
202 }
203
204 static struct fn_zone *
205 fn_new_zone(struct fn_hash *table, int z)
206 {
207         int i;
208         struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
209         if (!fz)
210                 return NULL;
211
212         if (z) {
213                 fz->fz_divisor = 16;
214         } else {
215                 fz->fz_divisor = 1;
216         }
217         fz->fz_hashmask = (fz->fz_divisor - 1);
218         fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
219         if (!fz->fz_hash) {
220                 kfree(fz);
221                 return NULL;
222         }
223         memset(fz->fz_hash, 0, fz->fz_divisor * sizeof(struct hlist_head *));
224         fz->fz_order = z;
225         fz->fz_mask = inet_make_mask(z);
226
227         /* Find the first not empty zone with more specific mask */
228         for (i=z+1; i<=32; i++)
229                 if (table->fn_zones[i])
230                         break;
231         write_lock_bh(&fib_hash_lock);
232         if (i>32) {
233                 /* No more specific masks, we are the first. */
234                 fz->fz_next = table->fn_zone_list;
235                 table->fn_zone_list = fz;
236         } else {
237                 fz->fz_next = table->fn_zones[i]->fz_next;
238                 table->fn_zones[i]->fz_next = fz;
239         }
240         table->fn_zones[z] = fz;
241         fib_hash_genid++;
242         write_unlock_bh(&fib_hash_lock);
243         return fz;
244 }
245
246 static int
247 fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
248 {
249         int err;
250         struct fn_zone *fz;
251         struct fn_hash *t = (struct fn_hash*)tb->tb_data;
252
253         read_lock(&fib_hash_lock);
254         for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
255                 struct hlist_head *head;
256                 struct hlist_node *node;
257                 struct fib_node *f;
258                 __be32 k = fz_key(flp->fl4_dst, fz);
259
260                 head = &fz->fz_hash[fn_hash(k, fz)];
261                 hlist_for_each_entry(f, node, head, fn_hash) {
262                         if (f->fn_key != k)
263                                 continue;
264
265                         err = fib_semantic_match(&f->fn_alias,
266                                                  flp, res,
267                                                  f->fn_key, fz->fz_mask,
268                                                  fz->fz_order);
269                         if (err <= 0)
270                                 goto out;
271                 }
272         }
273         err = 1;
274 out:
275         read_unlock(&fib_hash_lock);
276         return err;
277 }
278
279 static int fn_hash_last_dflt=-1;
280
281 static void
282 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
283 {
284         int order, last_idx;
285         struct hlist_node *node;
286         struct fib_node *f;
287         struct fib_info *fi = NULL;
288         struct fib_info *last_resort;
289         struct fn_hash *t = (struct fn_hash*)tb->tb_data;
290         struct fn_zone *fz = t->fn_zones[0];
291
292         if (fz == NULL)
293                 return;
294
295         last_idx = -1;
296         last_resort = NULL;
297         order = -1;
298
299         read_lock(&fib_hash_lock);
300         hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
301                 struct fib_alias *fa;
302
303                 list_for_each_entry(fa, &f->fn_alias, fa_list) {
304                         struct fib_info *next_fi = fa->fa_info;
305
306                         if (fa->fa_scope != res->scope ||
307                             fa->fa_type != RTN_UNICAST)
308                                 continue;
309
310                         if (next_fi->fib_priority > res->fi->fib_priority)
311                                 break;
312                         if (!next_fi->fib_nh[0].nh_gw ||
313                             next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
314                                 continue;
315                         fa->fa_state |= FA_S_ACCESSED;
316
317                         if (fi == NULL) {
318                                 if (next_fi != res->fi)
319                                         break;
320                         } else if (!fib_detect_death(fi, order, &last_resort,
321                                                      &last_idx, &fn_hash_last_dflt)) {
322                                 if (res->fi)
323                                         fib_info_put(res->fi);
324                                 res->fi = fi;
325                                 atomic_inc(&fi->fib_clntref);
326                                 fn_hash_last_dflt = order;
327                                 goto out;
328                         }
329                         fi = next_fi;
330                         order++;
331                 }
332         }
333
334         if (order <= 0 || fi == NULL) {
335                 fn_hash_last_dflt = -1;
336                 goto out;
337         }
338
339         if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
340                 if (res->fi)
341                         fib_info_put(res->fi);
342                 res->fi = fi;
343                 atomic_inc(&fi->fib_clntref);
344                 fn_hash_last_dflt = order;
345                 goto out;
346         }
347
348         if (last_idx >= 0) {
349                 if (res->fi)
350                         fib_info_put(res->fi);
351                 res->fi = last_resort;
352                 if (last_resort)
353                         atomic_inc(&last_resort->fib_clntref);
354         }
355         fn_hash_last_dflt = last_idx;
356 out:
357         read_unlock(&fib_hash_lock);
358 }
359
360 /* Insert node F to FZ. */
361 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
362 {
363         struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
364
365         hlist_add_head(&f->fn_hash, head);
366 }
367
368 /* Return the node in FZ matching KEY. */
369 static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
370 {
371         struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
372         struct hlist_node *node;
373         struct fib_node *f;
374
375         hlist_for_each_entry(f, node, head, fn_hash) {
376                 if (f->fn_key == key)
377                         return f;
378         }
379
380         return NULL;
381 }
382
383 static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
384 {
385         struct fn_hash *table = (struct fn_hash *) tb->tb_data;
386         struct fib_node *new_f, *f;
387         struct fib_alias *fa, *new_fa;
388         struct fn_zone *fz;
389         struct fib_info *fi;
390         u8 tos = cfg->fc_tos;
391         __be32 key;
392         int err;
393
394         if (cfg->fc_dst_len > 32)
395                 return -EINVAL;
396
397         fz = table->fn_zones[cfg->fc_dst_len];
398         if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
399                 return -ENOBUFS;
400
401         key = 0;
402         if (cfg->fc_dst) {
403                 if (cfg->fc_dst & ~FZ_MASK(fz))
404                         return -EINVAL;
405                 key = fz_key(cfg->fc_dst, fz);
406         }
407
408         fi = fib_create_info(cfg);
409         if (IS_ERR(fi))
410                 return PTR_ERR(fi);
411
412         if (fz->fz_nent > (fz->fz_divisor<<1) &&
413             fz->fz_divisor < FZ_MAX_DIVISOR &&
414             (cfg->fc_dst_len == 32 ||
415              (1 << cfg->fc_dst_len) > fz->fz_divisor))
416                 fn_rehash_zone(fz);
417
418         f = fib_find_node(fz, key);
419
420         if (!f)
421                 fa = NULL;
422         else
423                 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
424
425         /* Now fa, if non-NULL, points to the first fib alias
426          * with the same keys [prefix,tos,priority], if such key already
427          * exists or to the node before which we will insert new one.
428          *
429          * If fa is NULL, we will need to allocate a new one and
430          * insert to the head of f.
431          *
432          * If f is NULL, no fib node matched the destination key
433          * and we need to allocate a new one of those as well.
434          */
435
436         if (fa && fa->fa_tos == tos &&
437             fa->fa_info->fib_priority == fi->fib_priority) {
438                 struct fib_alias *fa_orig;
439
440                 err = -EEXIST;
441                 if (cfg->fc_nlflags & NLM_F_EXCL)
442                         goto out;
443
444                 if (cfg->fc_nlflags & NLM_F_REPLACE) {
445                         struct fib_info *fi_drop;
446                         u8 state;
447
448                         write_lock_bh(&fib_hash_lock);
449                         fi_drop = fa->fa_info;
450                         fa->fa_info = fi;
451                         fa->fa_type = cfg->fc_type;
452                         fa->fa_scope = cfg->fc_scope;
453                         state = fa->fa_state;
454                         fa->fa_state &= ~FA_S_ACCESSED;
455                         fib_hash_genid++;
456                         write_unlock_bh(&fib_hash_lock);
457
458                         fib_release_info(fi_drop);
459                         if (state & FA_S_ACCESSED)
460                                 rt_cache_flush(-1);
461                         return 0;
462                 }
463
464                 /* Error if we find a perfect match which
465                  * uses the same scope, type, and nexthop
466                  * information.
467                  */
468                 fa_orig = fa;
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)
472                                 break;
473                         if (fa->fa_info->fib_priority != fi->fib_priority)
474                                 break;
475                         if (fa->fa_type == cfg->fc_type &&
476                             fa->fa_scope == cfg->fc_scope &&
477                             fa->fa_info == fi)
478                                 goto out;
479                 }
480                 if (!(cfg->fc_nlflags & NLM_F_APPEND))
481                         fa = fa_orig;
482         }
483
484         err = -ENOENT;
485         if (!(cfg->fc_nlflags & NLM_F_CREATE))
486                 goto out;
487
488         err = -ENOBUFS;
489         new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
490         if (new_fa == NULL)
491                 goto out;
492
493         new_f = NULL;
494         if (!f) {
495                 new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
496                 if (new_f == NULL)
497                         goto out_free_new_fa;
498
499                 INIT_HLIST_NODE(&new_f->fn_hash);
500                 INIT_LIST_HEAD(&new_f->fn_alias);
501                 new_f->fn_key = key;
502                 f = new_f;
503         }
504
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;
510
511         /*
512          * Insert new entry to the list.
513          */
514
515         write_lock_bh(&fib_hash_lock);
516         if (new_f)
517                 fib_insert_node(fz, new_f);
518         list_add_tail(&new_fa->fa_list,
519                  (fa ? &fa->fa_list : &f->fn_alias));
520         fib_hash_genid++;
521         write_unlock_bh(&fib_hash_lock);
522
523         if (new_f)
524                 fz->fz_nent++;
525         rt_cache_flush(-1);
526
527         rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
528                   &cfg->fc_nlinfo);
529         return 0;
530
531 out_free_new_fa:
532         kmem_cache_free(fn_alias_kmem, new_fa);
533 out:
534         fib_release_info(fi);
535         return err;
536 }
537
538
539 static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
540 {
541         struct fn_hash *table = (struct fn_hash*)tb->tb_data;
542         struct fib_node *f;
543         struct fib_alias *fa, *fa_to_delete;
544         struct fn_zone *fz;
545         __be32 key;
546
547         if (cfg->fc_dst_len > 32)
548                 return -EINVAL;
549
550         if ((fz  = table->fn_zones[cfg->fc_dst_len]) == NULL)
551                 return -ESRCH;
552
553         key = 0;
554         if (cfg->fc_dst) {
555                 if (cfg->fc_dst & ~FZ_MASK(fz))
556                         return -EINVAL;
557                 key = fz_key(cfg->fc_dst, fz);
558         }
559
560         f = fib_find_node(fz, key);
561
562         if (!f)
563                 fa = NULL;
564         else
565                 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
566         if (!fa)
567                 return -ESRCH;
568
569         fa_to_delete = NULL;
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;
573
574                 if (fa->fa_tos != cfg->fc_tos)
575                         break;
576
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) {
584                         fa_to_delete = fa;
585                         break;
586                 }
587         }
588
589         if (fa_to_delete) {
590                 int kill_fn;
591
592                 fa = fa_to_delete;
593                 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
594                           tb->tb_id, &cfg->fc_nlinfo);
595
596                 kill_fn = 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);
601                         kill_fn = 1;
602                 }
603                 fib_hash_genid++;
604                 write_unlock_bh(&fib_hash_lock);
605
606                 if (fa->fa_state & FA_S_ACCESSED)
607                         rt_cache_flush(-1);
608                 fn_free_alias(fa);
609                 if (kill_fn) {
610                         fn_free_node(f);
611                         fz->fz_nent--;
612                 }
613
614                 return 0;
615         }
616         return -ESRCH;
617 }
618
619 static int fn_flush_list(struct fn_zone *fz, int idx)
620 {
621         struct hlist_head *head = &fz->fz_hash[idx];
622         struct hlist_node *node, *n;
623         struct fib_node *f;
624         int found = 0;
625
626         hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
627                 struct fib_alias *fa, *fa_node;
628                 int kill_f;
629
630                 kill_f = 0;
631                 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
632                         struct fib_info *fi = fa->fa_info;
633
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);
639                                         kill_f = 1;
640                                 }
641                                 fib_hash_genid++;
642                                 write_unlock_bh(&fib_hash_lock);
643
644                                 fn_free_alias(fa);
645                                 found++;
646                         }
647                 }
648                 if (kill_f) {
649                         fn_free_node(f);
650                         fz->fz_nent--;
651                 }
652         }
653         return found;
654 }
655
656 static int fn_hash_flush(struct fib_table *tb)
657 {
658         struct fn_hash *table = (struct fn_hash *) tb->tb_data;
659         struct fn_zone *fz;
660         int found = 0;
661
662         for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
663                 int i;
664
665                 for (i = fz->fz_divisor - 1; i >= 0; i--)
666                         found += fn_flush_list(fz, i);
667         }
668         return found;
669 }
670
671
672 static inline int
673 fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
674                      struct fib_table *tb,
675                      struct fn_zone *fz,
676                      struct hlist_head *head)
677 {
678         struct hlist_node *node;
679         struct fib_node *f;
680         int i, s_i;
681
682         s_i = cb->args[4];
683         i = 0;
684         hlist_for_each_entry(f, node, head, fn_hash) {
685                 struct fib_alias *fa;
686
687                 list_for_each_entry(fa, &f->fn_alias, fa_list) {
688                         if (i < s_i)
689                                 goto next;
690
691                         if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
692                                           cb->nlh->nlmsg_seq,
693                                           RTM_NEWROUTE,
694                                           tb->tb_id,
695                                           fa->fa_type,
696                                           fa->fa_scope,
697                                           f->fn_key,
698                                           fz->fz_order,
699                                           fa->fa_tos,
700                                           fa->fa_info,
701                                           NLM_F_MULTI) < 0) {
702                                 cb->args[4] = i;
703                                 return -1;
704                         }
705                 next:
706                         i++;
707                 }
708         }
709         cb->args[4] = i;
710         return skb->len;
711 }
712
713 static inline int
714 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
715                    struct fib_table *tb,
716                    struct fn_zone *fz)
717 {
718         int h, s_h;
719
720         s_h = cb->args[3];
721         for (h=0; h < fz->fz_divisor; h++) {
722                 if (h < s_h) continue;
723                 if (h > s_h)
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]))
728                         continue;
729                 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
730                         cb->args[3] = h;
731                         return -1;
732                 }
733         }
734         cb->args[3] = h;
735         return skb->len;
736 }
737
738 static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
739 {
740         int m, s_m;
741         struct fn_zone *fz;
742         struct fn_hash *table = (struct fn_hash*)tb->tb_data;
743
744         s_m = cb->args[2];
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;
748                 if (m > s_m)
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) {
752                         cb->args[2] = m;
753                         read_unlock(&fib_hash_lock);
754                         return -1;
755                 }
756         }
757         read_unlock(&fib_hash_lock);
758         cb->args[2] = m;
759         return skb->len;
760 }
761
762 #ifdef CONFIG_IP_MULTIPLE_TABLES
763 struct fib_table * fib_hash_init(u32 id)
764 #else
765 struct fib_table * __init fib_hash_init(u32 id)
766 #endif
767 {
768         struct fib_table *tb;
769
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,
774                                                  NULL, NULL);
775
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,
780                                                   NULL, NULL);
781
782         tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
783                      GFP_KERNEL);
784         if (tb == NULL)
785                 return NULL;
786
787         tb->tb_id = id;
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));
795         return tb;
796 }
797
798 /* ------------------------------------------------------------------------ */
799 #ifdef CONFIG_PROC_FS
800
801 struct fib_iter_state {
802         struct fn_zone  *zone;
803         int             bucket;
804         struct hlist_head *hash_head;
805         struct fib_node *fn;
806         struct fib_alias *fa;
807         loff_t pos;
808         unsigned int genid;
809         int valid;
810 };
811
812 static struct fib_alias *fib_get_first(struct seq_file *seq)
813 {
814         struct fib_iter_state *iter = seq->private;
815         struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
816
817         iter->bucket    = 0;
818         iter->hash_head = NULL;
819         iter->fn        = NULL;
820         iter->fa        = NULL;
821         iter->pos       = 0;
822         iter->genid     = fib_hash_genid;
823         iter->valid     = 1;
824
825         for (iter->zone = table->fn_zone_list; iter->zone;
826              iter->zone = iter->zone->fz_next) {
827                 int maxslot;
828
829                 if (!iter->zone->fz_nent)
830                         continue;
831
832                 iter->hash_head = iter->zone->fz_hash;
833                 maxslot = iter->zone->fz_divisor;
834
835                 for (iter->bucket = 0; iter->bucket < maxslot;
836                      ++iter->bucket, ++iter->hash_head) {
837                         struct hlist_node *node;
838                         struct fib_node *fn;
839
840                         hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
841                                 struct fib_alias *fa;
842
843                                 list_for_each_entry(fa,&fn->fn_alias,fa_list) {
844                                         iter->fn = fn;
845                                         iter->fa = fa;
846                                         goto out;
847                                 }
848                         }
849                 }
850         }
851 out:
852         return iter->fa;
853 }
854
855 static struct fib_alias *fib_get_next(struct seq_file *seq)
856 {
857         struct fib_iter_state *iter = seq->private;
858         struct fib_node *fn;
859         struct fib_alias *fa;
860
861         /* Advance FA, if any. */
862         fn = iter->fn;
863         fa = iter->fa;
864         if (fa) {
865                 BUG_ON(!fn);
866                 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
867                         iter->fa = fa;
868                         goto out;
869                 }
870         }
871
872         fa = iter->fa = NULL;
873
874         /* Advance FN. */
875         if (fn) {
876                 struct hlist_node *node = &fn->fn_hash;
877                 hlist_for_each_entry_continue(fn, node, fn_hash) {
878                         iter->fn = fn;
879
880                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
881                                 iter->fa = fa;
882                                 goto out;
883                         }
884                 }
885         }
886
887         fn = iter->fn = NULL;
888
889         /* Advance hash chain. */
890         if (!iter->zone)
891                 goto out;
892
893         for (;;) {
894                 struct hlist_node *node;
895                 int maxslot;
896
897                 maxslot = iter->zone->fz_divisor;
898
899                 while (++iter->bucket < maxslot) {
900                         iter->hash_head++;
901
902                         hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
903                                 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
904                                         iter->fn = fn;
905                                         iter->fa = fa;
906                                         goto out;
907                                 }
908                         }
909                 }
910
911                 iter->zone = iter->zone->fz_next;
912
913                 if (!iter->zone)
914                         goto out;
915                 
916                 iter->bucket = 0;
917                 iter->hash_head = iter->zone->fz_hash;
918
919                 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
920                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
921                                 iter->fn = fn;
922                                 iter->fa = fa;
923                                 goto out;
924                         }
925                 }
926         }
927 out:
928         iter->pos++;
929         return fa;
930 }
931
932 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
933 {
934         struct fib_iter_state *iter = seq->private;
935         struct fib_alias *fa;
936         
937         if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
938                 fa   = iter->fa;
939                 pos -= iter->pos;
940         } else
941                 fa = fib_get_first(seq);
942
943         if (fa)
944                 while (pos && (fa = fib_get_next(seq)))
945                         --pos;
946         return pos ? NULL : fa;
947 }
948
949 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
950 {
951         void *v = NULL;
952
953         read_lock(&fib_hash_lock);
954         if (ip_fib_main_table)
955                 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
956         return v;
957 }
958
959 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
960 {
961         ++*pos;
962         return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
963 }
964
965 static void fib_seq_stop(struct seq_file *seq, void *v)
966 {
967         read_unlock(&fib_hash_lock);
968 }
969
970 static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
971 {
972         static const unsigned type2flags[RTN_MAX + 1] = {
973                 [7] = RTF_REJECT, [8] = RTF_REJECT,
974         };
975         unsigned flags = type2flags[type];
976
977         if (fi && fi->fib_nh->nh_gw)
978                 flags |= RTF_GATEWAY;
979         if (mask == htonl(0xFFFFFFFF))
980                 flags |= RTF_HOST;
981         flags |= RTF_UP;
982         return flags;
983 }
984
985 extern int dev_in_nx_info(struct net_device *, struct nx_info *);
986
987 /* 
988  *      This outputs /proc/net/route.
989  *
990  *      It always works in backward compatibility mode.
991  *      The format of the file is not supposed to be changed.
992  */
993 static int fib_seq_show(struct seq_file *seq, void *v)
994 {
995         struct fib_iter_state *iter;
996         char bf[128];
997         __be32 prefix, mask;
998         unsigned flags;
999         struct fib_node *f;
1000         struct fib_alias *fa;
1001         struct fib_info *fi;
1002
1003         if (v == SEQ_START_TOKEN) {
1004                 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
1005                            "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1006                            "\tWindow\tIRTT");
1007                 goto out;
1008         }
1009
1010         iter    = seq->private;
1011         f       = iter->fn;
1012         fa      = iter->fa;
1013         fi      = fa->fa_info;
1014         prefix  = f->fn_key;
1015         mask    = FZ_MASK(iter->zone);
1016         flags   = fib_flag_trans(fa->fa_type, mask, fi);
1017         if (fi && (!vx_flags(VXF_HIDE_NETIF, 0) ||
1018                 dev_in_nx_info(fi->fib_dev, current->nx_info)))
1019                 snprintf(bf, sizeof(bf),
1020                          "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1021                          fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1022                          fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1023                          mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1024                          fi->fib_window,
1025                          fi->fib_rtt >> 3);
1026         else
1027                 snprintf(bf, sizeof(bf),
1028                          "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1029                          prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
1030         seq_printf(seq, "%-127s\n", bf);
1031 out:
1032         return 0;
1033 }
1034
1035 static struct seq_operations fib_seq_ops = {
1036         .start  = fib_seq_start,
1037         .next   = fib_seq_next,
1038         .stop   = fib_seq_stop,
1039         .show   = fib_seq_show,
1040 };
1041
1042 static int fib_seq_open(struct inode *inode, struct file *file)
1043 {
1044         struct seq_file *seq;
1045         int rc = -ENOMEM;
1046         struct fib_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
1047        
1048         if (!s)
1049                 goto out;
1050
1051         rc = seq_open(file, &fib_seq_ops);
1052         if (rc)
1053                 goto out_kfree;
1054
1055         seq          = file->private_data;
1056         seq->private = s;
1057 out:
1058         return rc;
1059 out_kfree:
1060         kfree(s);
1061         goto out;
1062 }
1063
1064 static struct file_operations fib_seq_fops = {
1065         .owner          = THIS_MODULE,
1066         .open           = fib_seq_open,
1067         .read           = seq_read,
1068         .llseek         = seq_lseek,
1069         .release        = seq_release_private,
1070 };
1071
1072 int __init fib_proc_init(void)
1073 {
1074         if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
1075                 return -ENOMEM;
1076         return 0;
1077 }
1078
1079 void __init fib_proc_exit(void)
1080 {
1081         proc_net_remove("route");
1082 }
1083 #endif /* CONFIG_PROC_FS */