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