]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/fib_hash.c
fib: introduce fib_alias_accessed() helper
[net-next-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  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *              This program is free software; you can redistribute it and/or
11  *              modify it under the terms of the GNU General Public License
12  *              as published by the Free Software Foundation; either version
13  *              2 of the License, or (at your option) any later version.
14  */
15
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/bitops.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/skbuff.h>
33 #include <linux/netlink.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36
37 #include <net/net_namespace.h>
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         struct fib_alias        fn_embedded_alias;
55 };
56
57 #define EMBEDDED_HASH_SIZE (L1_CACHE_BYTES / sizeof(struct hlist_head))
58
59 struct fn_zone {
60         struct fn_zone __rcu    *fz_next;       /* Next not empty zone  */
61         struct hlist_head __rcu *fz_hash;       /* Hash table pointer   */
62         seqlock_t               fz_lock;
63         u32                     fz_hashmask;    /* (fz_divisor - 1)     */
64
65         u8                      fz_order;       /* Zone order (0..32)   */
66         u8                      fz_revorder;    /* 32 - fz_order        */
67         __be32                  fz_mask;        /* inet_make_mask(order) */
68 #define FZ_MASK(fz)             ((fz)->fz_mask)
69
70         struct hlist_head       fz_embedded_hash[EMBEDDED_HASH_SIZE];
71
72         int                     fz_nent;        /* Number of entries    */
73         int                     fz_divisor;     /* Hash size (mask+1)   */
74 };
75
76 struct fn_hash {
77         struct fn_zone          *fn_zones[33];
78         struct fn_zone __rcu    *fn_zone_list;
79 };
80
81 static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
82 {
83         u32 h = ntohl(key) >> fz->fz_revorder;
84         h ^= (h>>20);
85         h ^= (h>>10);
86         h ^= (h>>5);
87         h &= fz->fz_hashmask;
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 unsigned int fib_hash_genid;
97
98 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
99
100 static struct hlist_head *fz_hash_alloc(int divisor)
101 {
102         unsigned long size = divisor * sizeof(struct hlist_head);
103
104         if (size <= PAGE_SIZE)
105                 return kzalloc(size, GFP_KERNEL);
106
107         return (struct hlist_head *)
108                 __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
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 __rcu *new_head;
124
125                         hlist_del_rcu(&f->fn_hash);
126
127                         new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
128                         hlist_add_head_rcu(&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         new_divisor = old_divisor = fz->fz_divisor;
150
151         switch (old_divisor) {
152         case EMBEDDED_HASH_SIZE:
153                 new_divisor *= EMBEDDED_HASH_SIZE;
154                 break;
155         case EMBEDDED_HASH_SIZE*EMBEDDED_HASH_SIZE:
156                 new_divisor *= (EMBEDDED_HASH_SIZE/2);
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(KERN_DEBUG "fn_rehash_zone: hash for zone %d grows from %d\n",
171                fz->fz_order, old_divisor);
172 #endif
173
174         ht = fz_hash_alloc(new_divisor);
175
176         if (ht) {
177                 struct fn_zone nfz;
178
179                 memcpy(&nfz, fz, sizeof(nfz));
180
181                 write_seqlock_bh(&fz->fz_lock);
182                 old_ht = fz->fz_hash;
183                 nfz.fz_hash = ht;
184                 nfz.fz_hashmask = new_hashmask;
185                 nfz.fz_divisor = new_divisor;
186                 fn_rebuild_zone(&nfz, old_ht, old_divisor);
187                 fib_hash_genid++;
188                 rcu_assign_pointer(fz->fz_hash, ht);
189                 fz->fz_hashmask = new_hashmask;
190                 fz->fz_divisor = new_divisor;
191                 write_sequnlock_bh(&fz->fz_lock);
192
193                 if (old_ht != fz->fz_embedded_hash) {
194                         synchronize_rcu();
195                         fz_hash_free(old_ht, old_divisor);
196                 }
197         }
198 }
199
200 static void fn_free_node_rcu(struct rcu_head *head)
201 {
202         struct fib_node *f = container_of(head, struct fib_node, fn_embedded_alias.rcu);
203
204         kmem_cache_free(fn_hash_kmem, f);
205 }
206
207 static inline void fn_free_node(struct fib_node *f)
208 {
209         call_rcu(&f->fn_embedded_alias.rcu, fn_free_node_rcu);
210 }
211
212 static void fn_free_alias_rcu(struct rcu_head *head)
213 {
214         struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
215
216         kmem_cache_free(fn_alias_kmem, fa);
217 }
218
219 static inline void fn_free_alias(struct fib_alias *fa, struct fib_node *f)
220 {
221         fib_release_info(fa->fa_info);
222         if (fa == &f->fn_embedded_alias)
223                 fa->fa_info = NULL;
224         else
225                 call_rcu(&fa->rcu, fn_free_alias_rcu);
226 }
227
228 static struct fn_zone *
229 fn_new_zone(struct fn_hash *table, int z)
230 {
231         int i;
232         struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
233         if (!fz)
234                 return NULL;
235
236         seqlock_init(&fz->fz_lock);
237         fz->fz_divisor = z ? EMBEDDED_HASH_SIZE : 1;
238         fz->fz_hashmask = fz->fz_divisor - 1;
239         fz->fz_hash = fz->fz_embedded_hash;
240         fz->fz_order = z;
241         fz->fz_revorder = 32 - z;
242         fz->fz_mask = inet_make_mask(z);
243
244         /* Find the first not empty zone with more specific mask */
245         for (i = z + 1; i <= 32; i++)
246                 if (table->fn_zones[i])
247                         break;
248         if (i > 32) {
249                 /* No more specific masks, we are the first. */
250                 rcu_assign_pointer(fz->fz_next,
251                                    rtnl_dereference(table->fn_zone_list));
252                 rcu_assign_pointer(table->fn_zone_list, fz);
253         } else {
254                 rcu_assign_pointer(fz->fz_next,
255                                    rtnl_dereference(table->fn_zones[i]->fz_next));
256                 rcu_assign_pointer(table->fn_zones[i]->fz_next, fz);
257         }
258         table->fn_zones[z] = fz;
259         fib_hash_genid++;
260         return fz;
261 }
262
263 int fib_table_lookup(struct fib_table *tb,
264                      const struct flowi *flp, struct fib_result *res,
265                      int fib_flags)
266 {
267         int err;
268         struct fn_zone *fz;
269         struct fn_hash *t = (struct fn_hash *)tb->tb_data;
270
271         rcu_read_lock();
272         for (fz = rcu_dereference(t->fn_zone_list);
273              fz != NULL;
274              fz = rcu_dereference(fz->fz_next)) {
275                 struct hlist_head __rcu *head;
276                 struct hlist_node *node;
277                 struct fib_node *f;
278                 __be32 k;
279                 unsigned int seq;
280
281                 do {
282                         seq = read_seqbegin(&fz->fz_lock);
283                         k = fz_key(flp->fl4_dst, fz);
284
285                         head = &fz->fz_hash[fn_hash(k, fz)];
286                         hlist_for_each_entry_rcu(f, node, head, fn_hash) {
287                                 if (f->fn_key != k)
288                                         continue;
289
290                                 err = fib_semantic_match(&f->fn_alias,
291                                                  flp, res,
292                                                  fz->fz_order, fib_flags);
293                                 if (err <= 0)
294                                         goto out;
295                         }
296                 } while (read_seqretry(&fz->fz_lock, seq));
297         }
298         err = 1;
299 out:
300         rcu_read_unlock();
301         return err;
302 }
303
304 void fib_table_select_default(struct fib_table *tb,
305                               const struct flowi *flp, struct fib_result *res)
306 {
307         int order, last_idx;
308         struct hlist_node *node;
309         struct fib_node *f;
310         struct fib_info *fi = NULL;
311         struct fib_info *last_resort;
312         struct fn_hash *t = (struct fn_hash *)tb->tb_data;
313         struct fn_zone *fz = t->fn_zones[0];
314
315         if (fz == NULL)
316                 return;
317
318         last_idx = -1;
319         last_resort = NULL;
320         order = -1;
321
322         rcu_read_lock();
323         hlist_for_each_entry_rcu(f, node, &fz->fz_hash[0], fn_hash) {
324                 struct fib_alias *fa;
325
326                 list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
327                         struct fib_info *next_fi = fa->fa_info;
328
329                         if (fa->fa_scope != res->scope ||
330                             fa->fa_type != RTN_UNICAST)
331                                 continue;
332
333                         if (next_fi->fib_priority > res->fi->fib_priority)
334                                 break;
335                         if (!next_fi->fib_nh[0].nh_gw ||
336                             next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
337                                 continue;
338
339                         fib_alias_accessed(fa);
340
341                         if (fi == NULL) {
342                                 if (next_fi != res->fi)
343                                         break;
344                         } else if (!fib_detect_death(fi, order, &last_resort,
345                                                 &last_idx, tb->tb_default)) {
346                                 fib_result_assign(res, fi);
347                                 tb->tb_default = order;
348                                 goto out;
349                         }
350                         fi = next_fi;
351                         order++;
352                 }
353         }
354
355         if (order <= 0 || fi == NULL) {
356                 tb->tb_default = -1;
357                 goto out;
358         }
359
360         if (!fib_detect_death(fi, order, &last_resort, &last_idx,
361                                 tb->tb_default)) {
362                 fib_result_assign(res, fi);
363                 tb->tb_default = order;
364                 goto out;
365         }
366
367         if (last_idx >= 0)
368                 fib_result_assign(res, last_resort);
369         tb->tb_default = last_idx;
370 out:
371         rcu_read_unlock();
372 }
373
374 /* Insert node F to FZ. */
375 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
376 {
377         struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
378
379         hlist_add_head_rcu(&f->fn_hash, head);
380 }
381
382 /* Return the node in FZ matching KEY. */
383 static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
384 {
385         struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
386         struct hlist_node *node;
387         struct fib_node *f;
388
389         hlist_for_each_entry_rcu(f, node, head, fn_hash) {
390                 if (f->fn_key == key)
391                         return f;
392         }
393
394         return NULL;
395 }
396
397
398 static struct fib_alias *fib_fast_alloc(struct fib_node *f)
399 {
400         struct fib_alias *fa = &f->fn_embedded_alias;
401
402         if (fa->fa_info != NULL)
403                 fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
404         return fa;
405 }
406
407 /* Caller must hold RTNL. */
408 int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
409 {
410         struct fn_hash *table = (struct fn_hash *) tb->tb_data;
411         struct fib_node *new_f = NULL;
412         struct fib_node *f;
413         struct fib_alias *fa, *new_fa;
414         struct fn_zone *fz;
415         struct fib_info *fi;
416         u8 tos = cfg->fc_tos;
417         __be32 key;
418         int err;
419
420         if (cfg->fc_dst_len > 32)
421                 return -EINVAL;
422
423         fz = table->fn_zones[cfg->fc_dst_len];
424         if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
425                 return -ENOBUFS;
426
427         key = 0;
428         if (cfg->fc_dst) {
429                 if (cfg->fc_dst & ~FZ_MASK(fz))
430                         return -EINVAL;
431                 key = fz_key(cfg->fc_dst, fz);
432         }
433
434         fi = fib_create_info(cfg);
435         if (IS_ERR(fi))
436                 return PTR_ERR(fi);
437
438         if (fz->fz_nent > (fz->fz_divisor<<1) &&
439             fz->fz_divisor < FZ_MAX_DIVISOR &&
440             (cfg->fc_dst_len == 32 ||
441              (1 << cfg->fc_dst_len) > fz->fz_divisor))
442                 fn_rehash_zone(fz);
443
444         f = fib_find_node(fz, key);
445
446         if (!f)
447                 fa = NULL;
448         else
449                 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
450
451         /* Now fa, if non-NULL, points to the first fib alias
452          * with the same keys [prefix,tos,priority], if such key already
453          * exists or to the node before which we will insert new one.
454          *
455          * If fa is NULL, we will need to allocate a new one and
456          * insert to the head of f.
457          *
458          * If f is NULL, no fib node matched the destination key
459          * and we need to allocate a new one of those as well.
460          */
461
462         if (fa && fa->fa_tos == tos &&
463             fa->fa_info->fib_priority == fi->fib_priority) {
464                 struct fib_alias *fa_first, *fa_match;
465
466                 err = -EEXIST;
467                 if (cfg->fc_nlflags & NLM_F_EXCL)
468                         goto out;
469
470                 /* We have 2 goals:
471                  * 1. Find exact match for type, scope, fib_info to avoid
472                  * duplicate routes
473                  * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
474                  */
475                 fa_match = NULL;
476                 fa_first = fa;
477                 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
478                 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
479                         if (fa->fa_tos != tos)
480                                 break;
481                         if (fa->fa_info->fib_priority != fi->fib_priority)
482                                 break;
483                         if (fa->fa_type == cfg->fc_type &&
484                             fa->fa_scope == cfg->fc_scope &&
485                             fa->fa_info == fi) {
486                                 fa_match = fa;
487                                 break;
488                         }
489                 }
490
491                 if (cfg->fc_nlflags & NLM_F_REPLACE) {
492                         u8 state;
493
494                         fa = fa_first;
495                         if (fa_match) {
496                                 if (fa == fa_match)
497                                         err = 0;
498                                 goto out;
499                         }
500                         err = -ENOBUFS;
501                         new_fa = fib_fast_alloc(f);
502                         if (new_fa == NULL)
503                                 goto out;
504
505                         new_fa->fa_tos = fa->fa_tos;
506                         new_fa->fa_info = fi;
507                         new_fa->fa_type = cfg->fc_type;
508                         new_fa->fa_scope = cfg->fc_scope;
509                         state = fa->fa_state;
510                         new_fa->fa_state = state & ~FA_S_ACCESSED;
511                         fib_hash_genid++;
512                         list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
513
514                         fn_free_alias(fa, f);
515                         if (state & FA_S_ACCESSED)
516                                 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
517                         rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len,
518                                   tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
519                         return 0;
520                 }
521
522                 /* Error if we find a perfect match which
523                  * uses the same scope, type, and nexthop
524                  * information.
525                  */
526                 if (fa_match)
527                         goto out;
528
529                 if (!(cfg->fc_nlflags & NLM_F_APPEND))
530                         fa = fa_first;
531         }
532
533         err = -ENOENT;
534         if (!(cfg->fc_nlflags & NLM_F_CREATE))
535                 goto out;
536
537         err = -ENOBUFS;
538
539         if (!f) {
540                 new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
541                 if (new_f == NULL)
542                         goto out;
543
544                 INIT_HLIST_NODE(&new_f->fn_hash);
545                 INIT_LIST_HEAD(&new_f->fn_alias);
546                 new_f->fn_key = key;
547                 f = new_f;
548         }
549
550         new_fa = fib_fast_alloc(f);
551         if (new_fa == NULL)
552                 goto out;
553
554         new_fa->fa_info = fi;
555         new_fa->fa_tos = tos;
556         new_fa->fa_type = cfg->fc_type;
557         new_fa->fa_scope = cfg->fc_scope;
558         new_fa->fa_state = 0;
559
560         /*
561          * Insert new entry to the list.
562          */
563
564         if (new_f)
565                 fib_insert_node(fz, new_f);
566         list_add_tail_rcu(&new_fa->fa_list,
567                  (fa ? &fa->fa_list : &f->fn_alias));
568         fib_hash_genid++;
569
570         if (new_f)
571                 fz->fz_nent++;
572         rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
573
574         rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
575                   &cfg->fc_nlinfo, 0);
576         return 0;
577
578 out:
579         if (new_f)
580                 kmem_cache_free(fn_hash_kmem, new_f);
581         fib_release_info(fi);
582         return err;
583 }
584
585 int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
586 {
587         struct fn_hash *table = (struct fn_hash *)tb->tb_data;
588         struct fib_node *f;
589         struct fib_alias *fa, *fa_to_delete;
590         struct fn_zone *fz;
591         __be32 key;
592
593         if (cfg->fc_dst_len > 32)
594                 return -EINVAL;
595
596         if ((fz  = table->fn_zones[cfg->fc_dst_len]) == NULL)
597                 return -ESRCH;
598
599         key = 0;
600         if (cfg->fc_dst) {
601                 if (cfg->fc_dst & ~FZ_MASK(fz))
602                         return -EINVAL;
603                 key = fz_key(cfg->fc_dst, fz);
604         }
605
606         f = fib_find_node(fz, key);
607
608         if (!f)
609                 fa = NULL;
610         else
611                 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
612         if (!fa)
613                 return -ESRCH;
614
615         fa_to_delete = NULL;
616         fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
617         list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
618                 struct fib_info *fi = fa->fa_info;
619
620                 if (fa->fa_tos != cfg->fc_tos)
621                         break;
622
623                 if ((!cfg->fc_type ||
624                      fa->fa_type == cfg->fc_type) &&
625                     (cfg->fc_scope == RT_SCOPE_NOWHERE ||
626                      fa->fa_scope == cfg->fc_scope) &&
627                     (!cfg->fc_protocol ||
628                      fi->fib_protocol == cfg->fc_protocol) &&
629                     fib_nh_match(cfg, fi) == 0) {
630                         fa_to_delete = fa;
631                         break;
632                 }
633         }
634
635         if (fa_to_delete) {
636                 int kill_fn;
637
638                 fa = fa_to_delete;
639                 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
640                           tb->tb_id, &cfg->fc_nlinfo, 0);
641
642                 kill_fn = 0;
643                 list_del_rcu(&fa->fa_list);
644                 if (list_empty(&f->fn_alias)) {
645                         hlist_del_rcu(&f->fn_hash);
646                         kill_fn = 1;
647                 }
648                 fib_hash_genid++;
649
650                 if (fa->fa_state & FA_S_ACCESSED)
651                         rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
652                 fn_free_alias(fa, f);
653                 if (kill_fn) {
654                         fn_free_node(f);
655                         fz->fz_nent--;
656                 }
657
658                 return 0;
659         }
660         return -ESRCH;
661 }
662
663 static int fn_flush_list(struct fn_zone *fz, int idx)
664 {
665         struct hlist_head *head = &fz->fz_hash[idx];
666         struct hlist_node *node, *n;
667         struct fib_node *f;
668         int found = 0;
669
670         hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
671                 struct fib_alias *fa, *fa_node;
672                 int kill_f;
673
674                 kill_f = 0;
675                 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
676                         struct fib_info *fi = fa->fa_info;
677
678                         if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
679                                 list_del_rcu(&fa->fa_list);
680                                 if (list_empty(&f->fn_alias)) {
681                                         hlist_del_rcu(&f->fn_hash);
682                                         kill_f = 1;
683                                 }
684                                 fib_hash_genid++;
685
686                                 fn_free_alias(fa, f);
687                                 found++;
688                         }
689                 }
690                 if (kill_f) {
691                         fn_free_node(f);
692                         fz->fz_nent--;
693                 }
694         }
695         return found;
696 }
697
698 /* caller must hold RTNL. */
699 int fib_table_flush(struct fib_table *tb)
700 {
701         struct fn_hash *table = (struct fn_hash *) tb->tb_data;
702         struct fn_zone *fz;
703         int found = 0;
704
705         for (fz = rtnl_dereference(table->fn_zone_list);
706              fz != NULL;
707              fz = rtnl_dereference(fz->fz_next)) {
708                 int i;
709
710                 for (i = fz->fz_divisor - 1; i >= 0; i--)
711                         found += fn_flush_list(fz, i);
712         }
713         return found;
714 }
715
716
717 static inline int
718 fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
719                      struct fib_table *tb,
720                      struct fn_zone *fz,
721                      struct hlist_head *head)
722 {
723         struct hlist_node *node;
724         struct fib_node *f;
725         int i, s_i;
726
727         s_i = cb->args[4];
728         i = 0;
729         hlist_for_each_entry_rcu(f, node, head, fn_hash) {
730                 struct fib_alias *fa;
731
732                 list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
733                         if (i < s_i)
734                                 goto next;
735
736                         if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
737                                           cb->nlh->nlmsg_seq,
738                                           RTM_NEWROUTE,
739                                           tb->tb_id,
740                                           fa->fa_type,
741                                           fa->fa_scope,
742                                           f->fn_key,
743                                           fz->fz_order,
744                                           fa->fa_tos,
745                                           fa->fa_info,
746                                           NLM_F_MULTI) < 0) {
747                                 cb->args[4] = i;
748                                 return -1;
749                         }
750 next:
751                         i++;
752                 }
753         }
754         cb->args[4] = i;
755         return skb->len;
756 }
757
758 static inline int
759 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
760                    struct fib_table *tb,
761                    struct fn_zone *fz)
762 {
763         int h, s_h;
764
765         if (fz->fz_hash == NULL)
766                 return skb->len;
767         s_h = cb->args[3];
768         for (h = s_h; h < fz->fz_divisor; h++) {
769                 if (hlist_empty(&fz->fz_hash[h]))
770                         continue;
771                 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
772                         cb->args[3] = h;
773                         return -1;
774                 }
775                 memset(&cb->args[4], 0,
776                        sizeof(cb->args) - 4*sizeof(cb->args[0]));
777         }
778         cb->args[3] = h;
779         return skb->len;
780 }
781
782 int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
783                    struct netlink_callback *cb)
784 {
785         int m = 0, s_m;
786         struct fn_zone *fz;
787         struct fn_hash *table = (struct fn_hash *)tb->tb_data;
788
789         s_m = cb->args[2];
790         rcu_read_lock();
791         for (fz = rcu_dereference(table->fn_zone_list);
792              fz != NULL;
793              fz = rcu_dereference(fz->fz_next), m++) {
794                 if (m < s_m)
795                         continue;
796                 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
797                         cb->args[2] = m;
798                         rcu_read_unlock();
799                         return -1;
800                 }
801                 memset(&cb->args[3], 0,
802                        sizeof(cb->args) - 3*sizeof(cb->args[0]));
803         }
804         rcu_read_unlock();
805         cb->args[2] = m;
806         return skb->len;
807 }
808
809 void __init fib_hash_init(void)
810 {
811         fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
812                                          0, SLAB_PANIC, NULL);
813
814         fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
815                                           0, SLAB_PANIC, NULL);
816
817 }
818
819 struct fib_table *fib_hash_table(u32 id)
820 {
821         struct fib_table *tb;
822
823         tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
824                      GFP_KERNEL);
825         if (tb == NULL)
826                 return NULL;
827
828         tb->tb_id = id;
829         tb->tb_default = -1;
830
831         memset(tb->tb_data, 0, sizeof(struct fn_hash));
832         return tb;
833 }
834
835 /* ------------------------------------------------------------------------ */
836 #ifdef CONFIG_PROC_FS
837
838 struct fib_iter_state {
839         struct seq_net_private p;
840         struct fn_zone  *zone;
841         int             bucket;
842         struct hlist_head *hash_head;
843         struct fib_node *fn;
844         struct fib_alias *fa;
845         loff_t pos;
846         unsigned int genid;
847         int valid;
848 };
849
850 static struct fib_alias *fib_get_first(struct seq_file *seq)
851 {
852         struct fib_iter_state *iter = seq->private;
853         struct fib_table *main_table;
854         struct fn_hash *table;
855
856         main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
857         table = (struct fn_hash *)main_table->tb_data;
858
859         iter->bucket    = 0;
860         iter->hash_head = NULL;
861         iter->fn        = NULL;
862         iter->fa        = NULL;
863         iter->pos       = 0;
864         iter->genid     = fib_hash_genid;
865         iter->valid     = 1;
866
867         for (iter->zone = rcu_dereference(table->fn_zone_list);
868              iter->zone != NULL;
869              iter->zone = rcu_dereference(iter->zone->fz_next)) {
870                 int maxslot;
871
872                 if (!iter->zone->fz_nent)
873                         continue;
874
875                 iter->hash_head = iter->zone->fz_hash;
876                 maxslot = iter->zone->fz_divisor;
877
878                 for (iter->bucket = 0; iter->bucket < maxslot;
879                      ++iter->bucket, ++iter->hash_head) {
880                         struct hlist_node *node;
881                         struct fib_node *fn;
882
883                         hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
884                                 struct fib_alias *fa;
885
886                                 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
887                                         iter->fn = fn;
888                                         iter->fa = fa;
889                                         goto out;
890                                 }
891                         }
892                 }
893         }
894 out:
895         return iter->fa;
896 }
897
898 static struct fib_alias *fib_get_next(struct seq_file *seq)
899 {
900         struct fib_iter_state *iter = seq->private;
901         struct fib_node *fn;
902         struct fib_alias *fa;
903
904         /* Advance FA, if any. */
905         fn = iter->fn;
906         fa = iter->fa;
907         if (fa) {
908                 BUG_ON(!fn);
909                 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
910                         iter->fa = fa;
911                         goto out;
912                 }
913         }
914
915         fa = iter->fa = NULL;
916
917         /* Advance FN. */
918         if (fn) {
919                 struct hlist_node *node = &fn->fn_hash;
920                 hlist_for_each_entry_continue(fn, node, fn_hash) {
921                         iter->fn = fn;
922
923                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
924                                 iter->fa = fa;
925                                 goto out;
926                         }
927                 }
928         }
929
930         fn = iter->fn = NULL;
931
932         /* Advance hash chain. */
933         if (!iter->zone)
934                 goto out;
935
936         for (;;) {
937                 struct hlist_node *node;
938                 int maxslot;
939
940                 maxslot = iter->zone->fz_divisor;
941
942                 while (++iter->bucket < maxslot) {
943                         iter->hash_head++;
944
945                         hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
946                                 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
947                                         iter->fn = fn;
948                                         iter->fa = fa;
949                                         goto out;
950                                 }
951                         }
952                 }
953
954                 iter->zone = rcu_dereference(iter->zone->fz_next);
955
956                 if (!iter->zone)
957                         goto out;
958
959                 iter->bucket = 0;
960                 iter->hash_head = iter->zone->fz_hash;
961
962                 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
963                         list_for_each_entry(fa, &fn->fn_alias, fa_list) {
964                                 iter->fn = fn;
965                                 iter->fa = fa;
966                                 goto out;
967                         }
968                 }
969         }
970 out:
971         iter->pos++;
972         return fa;
973 }
974
975 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
976 {
977         struct fib_iter_state *iter = seq->private;
978         struct fib_alias *fa;
979
980         if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
981                 fa   = iter->fa;
982                 pos -= iter->pos;
983         } else
984                 fa = fib_get_first(seq);
985
986         if (fa)
987                 while (pos && (fa = fib_get_next(seq)))
988                         --pos;
989         return pos ? NULL : fa;
990 }
991
992 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
993         __acquires(RCU)
994 {
995         void *v = NULL;
996
997         rcu_read_lock();
998         if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN))
999                 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1000         return v;
1001 }
1002
1003 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1004 {
1005         ++*pos;
1006         return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
1007 }
1008
1009 static void fib_seq_stop(struct seq_file *seq, void *v)
1010         __releases(RCU)
1011 {
1012         rcu_read_unlock();
1013 }
1014
1015 static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
1016 {
1017         static const unsigned type2flags[RTN_MAX + 1] = {
1018                 [7] = RTF_REJECT,
1019                 [8] = RTF_REJECT,
1020         };
1021         unsigned flags = type2flags[type];
1022
1023         if (fi && fi->fib_nh->nh_gw)
1024                 flags |= RTF_GATEWAY;
1025         if (mask == htonl(0xFFFFFFFF))
1026                 flags |= RTF_HOST;
1027         flags |= RTF_UP;
1028         return flags;
1029 }
1030
1031 /*
1032  *      This outputs /proc/net/route.
1033  *
1034  *      It always works in backward compatibility mode.
1035  *      The format of the file is not supposed to be changed.
1036  */
1037 static int fib_seq_show(struct seq_file *seq, void *v)
1038 {
1039         struct fib_iter_state *iter;
1040         int len;
1041         __be32 prefix, mask;
1042         unsigned flags;
1043         struct fib_node *f;
1044         struct fib_alias *fa;
1045         struct fib_info *fi;
1046
1047         if (v == SEQ_START_TOKEN) {
1048                 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
1049                            "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1050                            "\tWindow\tIRTT");
1051                 goto out;
1052         }
1053
1054         iter    = seq->private;
1055         f       = iter->fn;
1056         fa      = iter->fa;
1057         fi      = fa->fa_info;
1058         prefix  = f->fn_key;
1059         mask    = FZ_MASK(iter->zone);
1060         flags   = fib_flag_trans(fa->fa_type, mask, fi);
1061         if (fi)
1062                 seq_printf(seq,
1063                          "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
1064                          fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1065                          fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1066                          mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1067                          fi->fib_window,
1068                          fi->fib_rtt >> 3, &len);
1069         else
1070                 seq_printf(seq,
1071                          "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
1072                          prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len);
1073
1074         seq_printf(seq, "%*s\n", 127 - len, "");
1075 out:
1076         return 0;
1077 }
1078
1079 static const struct seq_operations fib_seq_ops = {
1080         .start  = fib_seq_start,
1081         .next   = fib_seq_next,
1082         .stop   = fib_seq_stop,
1083         .show   = fib_seq_show,
1084 };
1085
1086 static int fib_seq_open(struct inode *inode, struct file *file)
1087 {
1088         return seq_open_net(inode, file, &fib_seq_ops,
1089                             sizeof(struct fib_iter_state));
1090 }
1091
1092 static const struct file_operations fib_seq_fops = {
1093         .owner          = THIS_MODULE,
1094         .open           = fib_seq_open,
1095         .read           = seq_read,
1096         .llseek         = seq_lseek,
1097         .release        = seq_release_net,
1098 };
1099
1100 int __net_init fib_proc_init(struct net *net)
1101 {
1102         if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
1103                 return -ENOMEM;
1104         return 0;
1105 }
1106
1107 void __net_exit fib_proc_exit(struct net *net)
1108 {
1109         proc_net_remove(net, "route");
1110 }
1111 #endif /* CONFIG_PROC_FS */