vserver 1.9.5.x5
[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 <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>
25 #include <linux/mm.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/errno.h>
30 #include <linux/in.h>
31 #include <linux/inet.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
39 #include <net/ip.h>
40 #include <net/protocol.h>
41 #include <net/route.h>
42 #include <net/tcp.h>
43 #include <net/sock.h>
44 #include <net/ip_fib.h>
45
46 #include "fib_lookup.h"
47
48 static kmem_cache_t *fn_hash_kmem;
49 static kmem_cache_t *fn_alias_kmem;
50
51 struct fib_node {
52         struct hlist_node       fn_hash;
53         struct list_head        fn_alias;
54         u32                     fn_key;
55 };
56
57 struct fn_zone {
58         struct fn_zone          *fz_next;       /* Next not empty zone  */
59         struct hlist_head       *fz_hash;       /* Hash table pointer   */
60         int                     fz_nent;        /* Number of entries    */
61
62         int                     fz_divisor;     /* Hash divisor         */
63         u32                     fz_hashmask;    /* (fz_divisor - 1)     */
64 #define FZ_HASHMASK(fz)         ((fz)->fz_hashmask)
65
66         int                     fz_order;       /* Zone order           */
67         u32                     fz_mask;
68 #define FZ_MASK(fz)             ((fz)->fz_mask)
69 };
70
71 /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
72  * can be cheaper than memory lookup, so that FZ_* macros are used.
73  */
74
75 struct fn_hash {
76         struct fn_zone  *fn_zones[33];
77         struct fn_zone  *fn_zone_list;
78 };
79
80 static inline u32 fn_hash(u32 key, struct fn_zone *fz)
81 {
82         u32 h = ntohl(key)>>(32 - fz->fz_order);
83         h ^= (h>>20);
84         h ^= (h>>10);
85         h ^= (h>>5);
86         h &= FZ_HASHMASK(fz);
87         return h;
88 }
89
90 static inline u32 fz_key(u32 dst, struct fn_zone *fz)
91 {
92         return dst & FZ_MASK(fz);
93 }
94
95 static DEFINE_RWLOCK(fib_hash_lock);
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                 write_unlock_bh(&fib_hash_lock);
185
186                 fz_hash_free(old_ht, old_divisor);
187         }
188 }
189
190 static inline void fn_free_node(struct fib_node * f)
191 {
192         kmem_cache_free(fn_hash_kmem, f);
193 }
194
195 static inline void fn_free_alias(struct fib_alias *fa)
196 {
197         fib_release_info(fa->fa_info);
198         kmem_cache_free(fn_alias_kmem, fa);
199 }
200
201 static struct fn_zone *
202 fn_new_zone(struct fn_hash *table, int z)
203 {
204         int i;
205         struct fn_zone *fz = kmalloc(sizeof(struct fn_zone), GFP_KERNEL);
206         if (!fz)
207                 return NULL;
208
209         memset(fz, 0, sizeof(struct fn_zone));
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         write_unlock_bh(&fib_hash_lock);
240         return fz;
241 }
242
243 static int
244 fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
245 {
246         int err;
247         struct fn_zone *fz;
248         struct fn_hash *t = (struct fn_hash*)tb->tb_data;
249
250         read_lock(&fib_hash_lock);
251         for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
252                 struct hlist_head *head;
253                 struct hlist_node *node;
254                 struct fib_node *f;
255                 u32 k = fz_key(flp->fl4_dst, fz);
256
257                 head = &fz->fz_hash[fn_hash(k, fz)];
258                 hlist_for_each_entry(f, node, head, fn_hash) {
259                         if (f->fn_key != k)
260                                 continue;
261
262                         err = fib_semantic_match(&f->fn_alias,
263                                                  flp, res,
264                                                  fz->fz_order);
265                         if (err <= 0)
266                                 goto out;
267                 }
268         }
269         err = 1;
270 out:
271         read_unlock(&fib_hash_lock);
272         return err;
273 }
274
275 static int fn_hash_last_dflt=-1;
276
277 static void
278 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
279 {
280         int order, last_idx;
281         struct hlist_node *node;
282         struct fib_node *f;
283         struct fib_info *fi = NULL;
284         struct fib_info *last_resort;
285         struct fn_hash *t = (struct fn_hash*)tb->tb_data;
286         struct fn_zone *fz = t->fn_zones[0];
287
288         if (fz == NULL)
289                 return;
290
291         last_idx = -1;
292         last_resort = NULL;
293         order = -1;
294
295         read_lock(&fib_hash_lock);
296         hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
297                 struct fib_alias *fa;
298
299                 list_for_each_entry(fa, &f->fn_alias, fa_list) {
300                         struct fib_info *next_fi = fa->fa_info;
301
302                         if (fa->fa_scope != res->scope ||
303                             fa->fa_type != RTN_UNICAST)
304                                 continue;
305
306                         if (next_fi->fib_priority > res->fi->fib_priority)
307                                 break;
308                         if (!next_fi->fib_nh[0].nh_gw ||
309                             next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
310                                 continue;
311                         fa->fa_state |= FA_S_ACCESSED;
312
313                         if (fi == NULL) {
314                                 if (next_fi != res->fi)
315                                         break;
316                         } else if (!fib_detect_death(fi, order, &last_resort,
317                                                      &last_idx, &fn_hash_last_dflt)) {
318                                 if (res->fi)
319                                         fib_info_put(res->fi);
320                                 res->fi = fi;
321                                 atomic_inc(&fi->fib_clntref);
322                                 fn_hash_last_dflt = order;
323                                 goto out;
324                         }
325                         fi = next_fi;
326                         order++;
327                 }
328         }
329
330         if (order <= 0 || fi == NULL) {
331                 fn_hash_last_dflt = -1;
332                 goto out;
333         }
334
335         if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
336                 if (res->fi)
337                         fib_info_put(res->fi);
338                 res->fi = fi;
339                 atomic_inc(&fi->fib_clntref);
340                 fn_hash_last_dflt = order;
341                 goto out;
342         }
343
344         if (last_idx >= 0) {
345                 if (res->fi)
346                         fib_info_put(res->fi);
347                 res->fi = last_resort;
348                 if (last_resort)
349                         atomic_inc(&last_resort->fib_clntref);
350         }
351         fn_hash_last_dflt = last_idx;
352 out:
353         read_unlock(&fib_hash_lock);
354 }
355
356 /* Insert node F to FZ. */
357 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
358 {
359         struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
360
361         hlist_add_head(&f->fn_hash, head);
362 }
363
364 /* Return the node in FZ matching KEY. */
365 static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
366 {
367         struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
368         struct hlist_node *node;
369         struct fib_node *f;
370
371         hlist_for_each_entry(f, node, head, fn_hash) {
372                 if (f->fn_key == key)
373                         return f;
374         }
375
376         return NULL;
377 }
378
379 static int
380 fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
381                struct nlmsghdr *n, struct netlink_skb_parms *req)
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         int z = r->rtm_dst_len;
389         int type = r->rtm_type;
390         u8 tos = r->rtm_tos;
391         u32 key;
392         int err;
393
394         if (z > 32)
395                 return -EINVAL;
396         fz = table->fn_zones[z];
397         if (!fz && !(fz = fn_new_zone(table, z)))
398                 return -ENOBUFS;
399
400         key = 0;
401         if (rta->rta_dst) {
402                 u32 dst;
403                 memcpy(&dst, rta->rta_dst, 4);
404                 if (dst & ~FZ_MASK(fz))
405                         return -EINVAL;
406                 key = fz_key(dst, fz);
407         }
408
409         if  ((fi = fib_create_info(r, rta, n, &err)) == NULL)
410                 return err;
411
412         if (fz->fz_nent > (fz->fz_divisor<<1) &&
413             fz->fz_divisor < FZ_MAX_DIVISOR &&
414             (z==32 || (1<<z) > fz->fz_divisor))
415                 fn_rehash_zone(fz);
416
417         f = fib_find_node(fz, key);
418
419         if (!f)
420                 fa = NULL;
421         else
422                 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
423
424         /* Now fa, if non-NULL, points to the first fib alias
425          * with the same keys [prefix,tos,priority], if such key already
426          * exists or to the node before which we will insert new one.
427          *
428          * If fa is NULL, we will need to allocate a new one and
429          * insert to the head of f.
430          *
431          * If f is NULL, no fib node matched the destination key
432          * and we need to allocate a new one of those as well.
433          */
434
435         if (fa && fa->fa_tos == tos &&
436             fa->fa_info->fib_priority == fi->fib_priority) {
437                 struct fib_alias *fa_orig;
438
439                 err = -EEXIST;
440                 if (n->nlmsg_flags & NLM_F_EXCL)
441                         goto out;
442
443                 if (n->nlmsg_flags & NLM_F_REPLACE) {
444                         struct fib_info *fi_drop;
445                         u8 state;
446
447                         write_lock_bh(&fib_hash_lock);
448                         fi_drop = fa->fa_info;
449                         fa->fa_info = fi;
450                         fa->fa_type = type;
451                         fa->fa_scope = r->rtm_scope;
452                         state = fa->fa_state;
453                         fa->fa_state &= ~FA_S_ACCESSED;
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 == type &&
474                             fa->fa_scope == r->rtm_scope &&
475                             fa->fa_info == fi)
476                                 goto out;
477                 }
478                 if (!(n->nlmsg_flags & NLM_F_APPEND))
479                         fa = fa_orig;
480         }
481
482         err = -ENOENT;
483         if (!(n->nlmsg_flags&NLM_F_CREATE))
484                 goto out;
485
486         err = -ENOBUFS;
487         new_fa = kmem_cache_alloc(fn_alias_kmem, SLAB_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, SLAB_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 = type;
506         new_fa->fa_scope = r->rtm_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         write_unlock_bh(&fib_hash_lock);
519
520         if (new_f)
521                 fz->fz_nent++;
522         rt_cache_flush(-1);
523
524         rtmsg_fib(RTM_NEWROUTE, key, new_fa, z, tb->tb_id, n, req);
525         return 0;
526
527 out_free_new_fa:
528         kmem_cache_free(fn_alias_kmem, new_fa);
529 out:
530         fib_release_info(fi);
531         return err;
532 }
533
534
535 static int
536 fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
537                struct nlmsghdr *n, struct netlink_skb_parms *req)
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         int z = r->rtm_dst_len;
543         struct fn_zone *fz;
544         u32 key;
545         u8 tos = r->rtm_tos;
546
547         if (z > 32)
548                 return -EINVAL;
549         if ((fz  = table->fn_zones[z]) == NULL)
550                 return -ESRCH;
551
552         key = 0;
553         if (rta->rta_dst) {
554                 u32 dst;
555                 memcpy(&dst, rta->rta_dst, 4);
556                 if (dst & ~FZ_MASK(fz))
557                         return -EINVAL;
558                 key = fz_key(dst, fz);
559         }
560
561         f = fib_find_node(fz, key);
562
563         if (!f)
564                 fa = NULL;
565         else
566                 fa = fib_find_alias(&f->fn_alias, tos, 0);
567         if (!fa)
568                 return -ESRCH;
569
570         fa_to_delete = NULL;
571         fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
572         list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
573                 struct fib_info *fi = fa->fa_info;
574
575                 if (fa->fa_tos != tos)
576                         break;
577
578                 if ((!r->rtm_type ||
579                      fa->fa_type == r->rtm_type) &&
580                     (r->rtm_scope == RT_SCOPE_NOWHERE ||
581                      fa->fa_scope == r->rtm_scope) &&
582                     (!r->rtm_protocol ||
583                      fi->fib_protocol == r->rtm_protocol) &&
584                     fib_nh_match(r, n, rta, fi) == 0) {
585                         fa_to_delete = fa;
586                         break;
587                 }
588         }
589
590         if (fa_to_delete) {
591                 int kill_fn;
592
593                 fa = fa_to_delete;
594                 rtmsg_fib(RTM_DELROUTE, key, fa, z, tb->tb_id, n, req);
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                 write_unlock_bh(&fib_hash_lock);
604
605                 if (fa->fa_state & FA_S_ACCESSED)
606                         rt_cache_flush(-1);
607                 fn_free_alias(fa);
608                 if (kill_fn) {
609                         fn_free_node(f);
610                         fz->fz_nent--;
611                 }
612
613                 return 0;
614         }
615         return -ESRCH;
616 }
617
618 static int fn_flush_list(struct fn_zone *fz, int idx)
619 {
620         struct hlist_head *head = &fz->fz_hash[idx];
621         struct hlist_node *node, *n;
622         struct fib_node *f;
623         int found = 0;
624
625         hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
626                 struct fib_alias *fa, *fa_node;
627                 int kill_f;
628
629                 kill_f = 0;
630                 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
631                         struct fib_info *fi = fa->fa_info;
632
633                         if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
634                                 write_lock_bh(&fib_hash_lock);
635                                 list_del(&fa->fa_list);
636                                 if (list_empty(&f->fn_alias)) {
637                                         hlist_del(&f->fn_hash);
638                                         kill_f = 1;
639                                 }
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[3];
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) < 0) {
699                                 cb->args[3] = i;
700                                 return -1;
701                         }
702                 next:
703                         i++;
704                 }
705         }
706         cb->args[3] = i;
707         return skb->len;
708 }
709
710 static inline int
711 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
712                    struct fib_table *tb,
713                    struct fn_zone *fz)
714 {
715         int h, s_h;
716
717         s_h = cb->args[2];
718         for (h=0; h < fz->fz_divisor; h++) {
719                 if (h < s_h) continue;
720                 if (h > s_h)
721                         memset(&cb->args[3], 0,
722                                sizeof(cb->args) - 3*sizeof(cb->args[0]));
723                 if (fz->fz_hash == NULL ||
724                     hlist_empty(&fz->fz_hash[h]))
725                         continue;
726                 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
727                         cb->args[2] = h;
728                         return -1;
729                 }
730         }
731         cb->args[2] = h;
732         return skb->len;
733 }
734
735 static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
736 {
737         int m, s_m;
738         struct fn_zone *fz;
739         struct fn_hash *table = (struct fn_hash*)tb->tb_data;
740
741         s_m = cb->args[1];
742         read_lock(&fib_hash_lock);
743         for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
744                 if (m < s_m) continue;
745                 if (m > s_m)
746                         memset(&cb->args[2], 0,
747                                sizeof(cb->args) - 2*sizeof(cb->args[0]));
748                 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
749                         cb->args[1] = m;
750                         read_unlock(&fib_hash_lock);
751                         return -1;
752                 }
753         }
754         read_unlock(&fib_hash_lock);
755         cb->args[1] = m;
756         return skb->len;
757 }
758
759 #ifdef CONFIG_IP_MULTIPLE_TABLES
760 struct fib_table * fib_hash_init(int id)
761 #else
762 struct fib_table * __init fib_hash_init(int id)
763 #endif
764 {
765         struct fib_table *tb;
766
767         if (fn_hash_kmem == NULL)
768                 fn_hash_kmem = kmem_cache_create("ip_fib_hash",
769                                                  sizeof(struct fib_node),
770                                                  0, SLAB_HWCACHE_ALIGN,
771                                                  NULL, NULL);
772
773         if (fn_alias_kmem == NULL)
774                 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
775                                                   sizeof(struct fib_alias),
776                                                   0, SLAB_HWCACHE_ALIGN,
777                                                   NULL, NULL);
778
779         tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
780                      GFP_KERNEL);
781         if (tb == NULL)
782                 return NULL;
783
784         tb->tb_id = id;
785         tb->tb_lookup = fn_hash_lookup;
786         tb->tb_insert = fn_hash_insert;
787         tb->tb_delete = fn_hash_delete;
788         tb->tb_flush = fn_hash_flush;
789         tb->tb_select_default = fn_hash_select_default;
790         tb->tb_dump = fn_hash_dump;
791         memset(tb->tb_data, 0, sizeof(struct fn_hash));
792         return tb;
793 }
794
795 /* ------------------------------------------------------------------------ */
796 #ifdef CONFIG_PROC_FS
797
798 struct fib_iter_state {
799         struct fn_zone  *zone;
800         int             bucket;
801         struct hlist_head *hash_head;
802         struct fib_node *fn;
803         struct fib_alias *fa;
804 };
805
806 static struct fib_alias *fib_get_first(struct seq_file *seq)
807 {
808         struct fib_iter_state *iter = seq->private;
809         struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
810
811         iter->bucket    = 0;
812         iter->hash_head = NULL;
813         iter->fn        = NULL;
814         iter->fa        = NULL;
815
816         for (iter->zone = table->fn_zone_list; iter->zone;
817              iter->zone = iter->zone->fz_next) {
818                 int maxslot;
819
820                 if (!iter->zone->fz_nent)
821                         continue;
822
823                 iter->hash_head = iter->zone->fz_hash;
824                 maxslot = iter->zone->fz_divisor;
825
826                 for (iter->bucket = 0; iter->bucket < maxslot;
827                      ++iter->bucket, ++iter->hash_head) {
828                         struct hlist_node *node;
829                         struct fib_node *fn;
830
831                         hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
832                                 struct fib_alias *fa;
833
834                                 list_for_each_entry(fa,&fn->fn_alias,fa_list) {
835                                         iter->fn = fn;
836                                         iter->fa = fa;
837                                         goto out;
838                                 }
839                         }
840                 }
841         }
842 out:
843         return iter->fa;
844 }
845
846 static struct fib_alias *fib_get_next(struct seq_file *seq)
847 {
848         struct fib_iter_state *iter = seq->private;
849         struct fib_node *fn;
850         struct fib_alias *fa;
851
852         /* Advance FA, if any. */
853         fn = iter->fn;
854         fa = iter->fa;
855         if (fa) {
856                 BUG_ON(!fn);
857                 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
858                         iter->fa = fa;
859                         goto out;
860                 }
861         }
862
863         fa = iter->fa = NULL;
864
865         /* Advance FN. */
866         if (fn) {
867                 struct hlist_node *node = &fn->fn_hash;
868                 hlist_for_each_entry_continue(fn, node, fn_hash) {
869                         iter->fn = fn;
870
871                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
872                                 iter->fa = fa;
873                                 goto out;
874                         }
875                 }
876         }
877
878         fn = iter->fn = NULL;
879
880         /* Advance hash chain. */
881         if (!iter->zone)
882                 goto out;
883
884         for (;;) {
885                 struct hlist_node *node;
886                 int maxslot;
887
888                 maxslot = iter->zone->fz_divisor;
889
890                 while (++iter->bucket < maxslot) {
891                         iter->hash_head++;
892
893                         hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
894                                 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
895                                         iter->fn = fn;
896                                         iter->fa = fa;
897                                         goto out;
898                                 }
899                         }
900                 }
901
902                 iter->zone = iter->zone->fz_next;
903
904                 if (!iter->zone)
905                         goto out;
906                 
907                 iter->bucket = 0;
908                 iter->hash_head = iter->zone->fz_hash;
909
910                 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
911                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
912                                 iter->fn = fn;
913                                 iter->fa = fa;
914                                 goto out;
915                         }
916                 }
917         }
918 out:
919         return fa;
920 }
921
922 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
923 {
924         struct fib_alias *fa = fib_get_first(seq);
925
926         if (fa)
927                 while (pos && (fa = fib_get_next(seq)))
928                         --pos;
929         return pos ? NULL : fa;
930 }
931
932 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
933 {
934         void *v = NULL;
935
936         read_lock(&fib_hash_lock);
937         if (ip_fib_main_table)
938                 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
939         return v;
940 }
941
942 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
943 {
944         ++*pos;
945         return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
946 }
947
948 static void fib_seq_stop(struct seq_file *seq, void *v)
949 {
950         read_unlock(&fib_hash_lock);
951 }
952
953 static unsigned fib_flag_trans(int type, u32 mask, struct fib_info *fi)
954 {
955         static unsigned type2flags[RTN_MAX + 1] = {
956                 [7] = RTF_REJECT, [8] = RTF_REJECT,
957         };
958         unsigned flags = type2flags[type];
959
960         if (fi && fi->fib_nh->nh_gw)
961                 flags |= RTF_GATEWAY;
962         if (mask == 0xFFFFFFFF)
963                 flags |= RTF_HOST;
964         flags |= RTF_UP;
965         return flags;
966 }
967
968 extern int dev_in_nx_info(struct net_device *, struct nx_info *);
969
970 /* 
971  *      This outputs /proc/net/route.
972  *
973  *      It always works in backward compatibility mode.
974  *      The format of the file is not supposed to be changed.
975  */
976 static int fib_seq_show(struct seq_file *seq, void *v)
977 {
978         struct fib_iter_state *iter;
979         char bf[128];
980         u32 prefix, mask;
981         unsigned flags;
982         struct fib_node *f;
983         struct fib_alias *fa;
984         struct fib_info *fi;
985
986         if (v == SEQ_START_TOKEN) {
987                 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
988                            "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
989                            "\tWindow\tIRTT");
990                 goto out;
991         }
992
993         iter    = seq->private;
994         f       = iter->fn;
995         fa      = iter->fa;
996         fi      = fa->fa_info;
997         prefix  = f->fn_key;
998         mask    = FZ_MASK(iter->zone);
999         flags   = fib_flag_trans(fa->fa_type, mask, fi);
1000         if (fi && (!vx_flags(VXF_HIDE_NETIF, 0) ||
1001                 dev_in_nx_info(fi->fib_dev, current->nx_info)))
1002                 snprintf(bf, sizeof(bf),
1003                          "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1004                          fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1005                          fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1006                          mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1007                          fi->fib_window,
1008                          fi->fib_rtt >> 3);
1009         else
1010                 snprintf(bf, sizeof(bf),
1011                          "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1012                          prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
1013         seq_printf(seq, "%-127s\n", bf);
1014 out:
1015         return 0;
1016 }
1017
1018 static struct seq_operations fib_seq_ops = {
1019         .start  = fib_seq_start,
1020         .next   = fib_seq_next,
1021         .stop   = fib_seq_stop,
1022         .show   = fib_seq_show,
1023 };
1024
1025 static int fib_seq_open(struct inode *inode, struct file *file)
1026 {
1027         struct seq_file *seq;
1028         int rc = -ENOMEM;
1029         struct fib_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1030        
1031         if (!s)
1032                 goto out;
1033
1034         rc = seq_open(file, &fib_seq_ops);
1035         if (rc)
1036                 goto out_kfree;
1037
1038         seq          = file->private_data;
1039         seq->private = s;
1040         memset(s, 0, sizeof(*s));
1041 out:
1042         return rc;
1043 out_kfree:
1044         kfree(s);
1045         goto out;
1046 }
1047
1048 static struct file_operations fib_seq_fops = {
1049         .owner          = THIS_MODULE,
1050         .open           = fib_seq_open,
1051         .read           = seq_read,
1052         .llseek         = seq_lseek,
1053         .release        = seq_release_private,
1054 };
1055
1056 int __init fib_proc_init(void)
1057 {
1058         if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
1059                 return -ENOMEM;
1060         return 0;
1061 }
1062
1063 void __init fib_proc_exit(void)
1064 {
1065         proc_net_remove("route");
1066 }
1067 #endif /* CONFIG_PROC_FS */