blob: 43e1c594ce8f38aeae59dbf32dd6dfac747d2c46 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/system.h>
18#include <linux/bitops.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020037#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *fn_hash_kmem __read_mostly;
48static struct kmem_cache *fn_alias_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct fib_node {
51 struct hlist_node fn_hash;
52 struct list_head fn_alias;
Al Virob6e80c62006-09-26 22:20:01 -070053 __be32 fn_key;
Eric Dumazeta6501e02008-01-18 03:33:26 -080054 struct fib_alias fn_embedded_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
Eric Dumazet9bef83e2010-10-14 20:53:04 +000057#define EMBEDDED_HASH_SIZE (L1_CACHE_BYTES / sizeof(struct hlist_head))
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct fn_zone {
Eric Dumazet117a8cd2010-10-14 20:53:34 +000060 struct fn_zone __rcu *fz_next; /* Next not empty zone */
Eric Dumazet19f57252010-10-14 20:56:39 +000061 struct hlist_head __rcu *fz_hash; /* Hash table pointer */
62 seqlock_t fz_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 u32 fz_hashmask; /* (fz_divisor - 1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric Dumazet9bef83e2010-10-14 20:53:04 +000065 u8 fz_order; /* Zone order (0..32) */
66 u8 fz_revorder; /* 32 - fz_order */
67 __be32 fz_mask; /* inet_make_mask(order) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define FZ_MASK(fz) ((fz)->fz_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet9bef83e2010-10-14 20:53:04 +000070 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76struct fn_hash {
Eric Dumazet117a8cd2010-10-14 20:53:34 +000077 struct fn_zone *fn_zones[33];
78 struct fn_zone __rcu *fn_zone_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Al Virob6e80c62006-09-26 22:20:01 -070081static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Eric Dumazet9bef83e2010-10-14 20:53:04 +000083 u32 h = ntohl(key) >> fz->fz_revorder;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 h ^= (h>>20);
85 h ^= (h>>10);
86 h ^= (h>>5);
Eric Dumazet9bef83e2010-10-14 20:53:04 +000087 h &= fz->fz_hashmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return h;
89}
90
Al Virob6e80c62006-09-26 22:20:01 -070091static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 return dst & FZ_MASK(fz);
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static unsigned int fib_hash_genid;
97
98#define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
99
100static struct hlist_head *fz_hash_alloc(int divisor)
101{
102 unsigned long size = divisor * sizeof(struct hlist_head);
103
Eric Dumazet19f57252010-10-14 20:56:39 +0000104 if (size <= PAGE_SIZE)
Joonwoo Park3015a342007-11-26 23:31:24 +0800105 return kzalloc(size, GFP_KERNEL);
Eric Dumazet19f57252010-10-14 20:56:39 +0000106
107 return (struct hlist_head *)
108 __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/* The fib hash lock must be held when this is called. */
112static 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) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000123 struct hlist_head __rcu *new_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Eric Dumazet19f57252010-10-14 20:56:39 +0000125 hlist_del_rcu(&f->fn_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
Eric Dumazet19f57252010-10-14 20:56:39 +0000128 hlist_add_head_rcu(&f->fn_hash, new_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 }
131}
132
133static 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
143static 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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900148
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000149 new_divisor = old_divisor = fz->fz_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 switch (old_divisor) {
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000152 case EMBEDDED_HASH_SIZE:
153 new_divisor *= EMBEDDED_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000155 case EMBEDDED_HASH_SIZE*EMBEDDED_HASH_SIZE:
156 new_divisor *= (EMBEDDED_HASH_SIZE/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 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
Stephen Hemmingera6db9012008-01-12 20:58:35 -0800170 printk(KERN_DEBUG "fn_rehash_zone: hash for zone %d grows from %d\n",
171 fz->fz_order, old_divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
174 ht = fz_hash_alloc(new_divisor);
175
176 if (ht) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000177 struct fn_zone nfz;
178
179 memcpy(&nfz, fz, sizeof(nfz));
180
181 write_seqlock_bh(&fz->fz_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 old_ht = fz->fz_hash;
Eric Dumazet19f57252010-10-14 20:56:39 +0000183 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 fz->fz_hashmask = new_hashmask;
190 fz->fz_divisor = new_divisor;
Eric Dumazet19f57252010-10-14 20:56:39 +0000191 write_sequnlock_bh(&fz->fz_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Dumazet19f57252010-10-14 20:56:39 +0000193 if (old_ht != fz->fz_embedded_hash) {
194 synchronize_rcu();
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000195 fz_hash_free(old_ht, old_divisor);
Eric Dumazet19f57252010-10-14 20:56:39 +0000196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198}
199
Eric Dumazet19f57252010-10-14 20:56:39 +0000200static void fn_free_node_rcu(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Eric Dumazet19f57252010-10-14 20:56:39 +0000202 struct fib_node *f = container_of(head, struct fib_node, fn_embedded_alias.rcu);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kmem_cache_free(fn_hash_kmem, f);
205}
206
Eric Dumazet19f57252010-10-14 20:56:39 +0000207static inline void fn_free_node(struct fib_node *f)
208{
209 call_rcu(&f->fn_embedded_alias.rcu, fn_free_node_rcu);
210}
211
212static 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
Eric Dumazeta6501e02008-01-18 03:33:26 -0800219static inline void fn_free_alias(struct fib_alias *fa, struct fib_node *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 fib_release_info(fa->fa_info);
Eric Dumazeta6501e02008-01-18 03:33:26 -0800222 if (fa == &f->fn_embedded_alias)
223 fa->fa_info = NULL;
224 else
Eric Dumazet19f57252010-10-14 20:56:39 +0000225 call_rcu(&fa->rcu, fn_free_alias_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static struct fn_zone *
229fn_new_zone(struct fn_hash *table, int z)
230{
231 int i;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700232 struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!fz)
234 return NULL;
235
Eric Dumazet19f57252010-10-14 20:56:39 +0000236 seqlock_init(&fz->fz_lock);
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000237 fz->fz_divisor = z ? EMBEDDED_HASH_SIZE : 1;
238 fz->fz_hashmask = fz->fz_divisor - 1;
239 fz->fz_hash = fz->fz_embedded_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 fz->fz_order = z;
Eric Dumazet9bef83e2010-10-14 20:53:04 +0000241 fz->fz_revorder = 32 - z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 fz->fz_mask = inet_make_mask(z);
243
244 /* Find the first not empty zone with more specific mask */
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000245 for (i = z + 1; i <= 32; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (table->fn_zones[i])
247 break;
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000248 if (i > 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* No more specific masks, we are the first. */
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000250 rcu_assign_pointer(fz->fz_next,
251 rtnl_dereference(table->fn_zone_list));
252 rcu_assign_pointer(table->fn_zone_list, fz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000254 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 table->fn_zones[z] = fz;
259 fib_hash_genid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return fz;
261}
262
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000263int fib_table_lookup(struct fib_table *tb,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000264 const struct flowi *flp, struct fib_result *res,
265 int fib_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int err;
268 struct fn_zone *fz;
Jianjun Kong6ed25332008-11-03 00:25:16 -0800269 struct fn_hash *t = (struct fn_hash *)tb->tb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000271 rcu_read_lock();
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000272 for (fz = rcu_dereference(t->fn_zone_list);
273 fz != NULL;
274 fz = rcu_dereference(fz->fz_next)) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000275 struct hlist_head __rcu *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct hlist_node *node;
277 struct fib_node *f;
Eric Dumazet19f57252010-10-14 20:56:39 +0000278 __be32 k;
279 unsigned int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Eric Dumazet19f57252010-10-14 20:56:39 +0000281 do {
282 seq = read_seqbegin(&fz->fz_lock);
283 k = fz_key(flp->fl4_dst, fz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Eric Dumazet19f57252010-10-14 20:56:39 +0000285 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 flp, res,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000292 fz->fz_order, fib_flags);
Eric Dumazet19f57252010-10-14 20:56:39 +0000293 if (err <= 0)
294 goto out;
295 }
296 } while (read_seqretry(&fz->fz_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 err = 1;
299out:
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000300 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return err;
302}
303
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000304void fib_table_select_default(struct fib_table *tb,
305 const struct flowi *flp, struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
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;
Jianjun Kong6ed25332008-11-03 00:25:16 -0800312 struct fn_hash *t = (struct fn_hash *)tb->tb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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
Eric Dumazet19f57252010-10-14 20:56:39 +0000322 rcu_read_lock();
323 hlist_for_each_entry_rcu(f, node, &fz->fz_hash[0], fn_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct fib_alias *fa;
325
Eric Dumazet19f57252010-10-14 20:56:39 +0000326 list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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;
Eric Dumazet9b0c2902010-10-20 22:03:38 +0000338
339 fib_alias_accessed(fa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (fi == NULL) {
342 if (next_fi != res->fi)
343 break;
344 } else if (!fib_detect_death(fi, order, &last_resort,
Denis V. Lunev971b8932007-12-08 00:32:23 -0800345 &last_idx, tb->tb_default)) {
Denis V. Luneva2bbe682007-12-08 00:31:44 -0800346 fib_result_assign(res, fi);
Denis V. Lunev971b8932007-12-08 00:32:23 -0800347 tb->tb_default = order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 goto out;
349 }
350 fi = next_fi;
351 order++;
352 }
353 }
354
355 if (order <= 0 || fi == NULL) {
Denis V. Lunev971b8932007-12-08 00:32:23 -0800356 tb->tb_default = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto out;
358 }
359
Denis V. Lunev971b8932007-12-08 00:32:23 -0800360 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
361 tb->tb_default)) {
Denis V. Luneva2bbe682007-12-08 00:31:44 -0800362 fib_result_assign(res, fi);
Denis V. Lunev971b8932007-12-08 00:32:23 -0800363 tb->tb_default = order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto out;
365 }
366
Denis V. Luneva2bbe682007-12-08 00:31:44 -0800367 if (last_idx >= 0)
368 fib_result_assign(res, last_resort);
Denis V. Lunev971b8932007-12-08 00:32:23 -0800369 tb->tb_default = last_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370out:
Eric Dumazet19f57252010-10-14 20:56:39 +0000371 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/* Insert node F to FZ. */
375static 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
Eric Dumazet19f57252010-10-14 20:56:39 +0000379 hlist_add_head_rcu(&f->fn_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/* Return the node in FZ matching KEY. */
Al Virob6e80c62006-09-26 22:20:01 -0700383static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
386 struct hlist_node *node;
387 struct fib_node *f;
388
Eric Dumazet19f57252010-10-14 20:56:39 +0000389 hlist_for_each_entry_rcu(f, node, head, fn_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (f->fn_key == key)
391 return f;
392 }
393
394 return NULL;
395}
396
Eric Dumazet19f57252010-10-14 20:56:39 +0000397
398static 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
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000407/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000408int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
Adrian Bunk94cb1502008-02-19 16:28:54 -0800411 struct fib_node *new_f = NULL;
412 struct fib_node *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct fib_alias *fa, *new_fa;
414 struct fn_zone *fz;
415 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -0700416 u8 tos = cfg->fc_tos;
Al Virob6e80c62006-09-26 22:20:01 -0700417 __be32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int err;
419
Thomas Graf4e902c52006-08-17 18:14:52 -0700420 if (cfg->fc_dst_len > 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700422
423 fz = table->fn_zones[cfg->fc_dst_len];
424 if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -ENOBUFS;
426
427 key = 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700428 if (cfg->fc_dst) {
429 if (cfg->fc_dst & ~FZ_MASK(fz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700431 key = fz_key(cfg->fc_dst, fz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Thomas Graf4e902c52006-08-17 18:14:52 -0700434 fi = fib_create_info(cfg);
435 if (IS_ERR(fi))
436 return PTR_ERR(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (fz->fz_nent > (fz->fz_divisor<<1) &&
439 fz->fz_divisor < FZ_MAX_DIVISOR &&
Thomas Graf4e902c52006-08-17 18:14:52 -0700440 (cfg->fc_dst_len == 32 ||
441 (1 << cfg->fc_dst_len) > fz->fz_divisor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 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) {
Julian Anastasovc18865f2008-01-28 21:14:10 -0800464 struct fib_alias *fa_first, *fa_match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -0700467 if (cfg->fc_nlflags & NLM_F_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto out;
469
Julian Anastasovc18865f2008-01-28 21:14:10 -0800470 /* 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
Thomas Graf4e902c52006-08-17 18:14:52 -0700491 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u8 state;
493
Julian Anastasovc18865f2008-01-28 21:14:10 -0800494 fa = fa_first;
495 if (fa_match) {
496 if (fa == fa_match)
497 err = 0;
Joonwoo Parkbd566e72008-01-18 03:44:48 -0800498 goto out;
Julian Anastasovc18865f2008-01-28 21:14:10 -0800499 }
Eric Dumazet19f57252010-10-14 20:56:39 +0000500 err = -ENOBUFS;
501 new_fa = fib_fast_alloc(f);
502 if (new_fa == NULL)
503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Eric Dumazet19f57252010-10-14 20:56:39 +0000505 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (state & FA_S_ACCESSED)
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -0700516 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Eric Dumazet19f57252010-10-14 20:56:39 +0000517 rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len,
518 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520 }
521
522 /* Error if we find a perfect match which
523 * uses the same scope, type, and nexthop
524 * information.
525 */
Julian Anastasovc18865f2008-01-28 21:14:10 -0800526 if (fa_match)
527 goto out;
528
Thomas Graf4e902c52006-08-17 18:14:52 -0700529 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasovc18865f2008-01-28 21:14:10 -0800530 fa = fa_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
533 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -0700534 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out;
536
537 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!f) {
Eric Dumazeta6501e02008-01-18 03:33:26 -0800540 new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (new_f == NULL)
Eric Dumazeta6501e02008-01-18 03:33:26 -0800542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
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
Eric Dumazet19f57252010-10-14 20:56:39 +0000550 new_fa = fib_fast_alloc(f);
551 if (new_fa == NULL)
552 goto out;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new_fa->fa_info = fi;
555 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -0700556 new_fa->fa_type = cfg->fc_type;
557 new_fa->fa_scope = cfg->fc_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 new_fa->fa_state = 0;
559
560 /*
561 * Insert new entry to the list.
562 */
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (new_f)
565 fib_insert_node(fz, new_f);
Eric Dumazet19f57252010-10-14 20:56:39 +0000566 list_add_tail_rcu(&new_fa->fa_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 (fa ? &fa->fa_list : &f->fn_alias));
568 fib_hash_genid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (new_f)
571 fz->fz_nent++;
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -0700572 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Thomas Graf4e902c52006-08-17 18:14:52 -0700574 rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -0700575 &cfg->fc_nlinfo, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578out:
Adrian Bunk94cb1502008-02-19 16:28:54 -0800579 if (new_f)
580 kmem_cache_free(fn_hash_kmem, new_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 fib_release_info(fi);
582 return err;
583}
584
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000585int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Jianjun Kong6ed25332008-11-03 00:25:16 -0800587 struct fn_hash *table = (struct fn_hash *)tb->tb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct fib_node *f;
589 struct fib_alias *fa, *fa_to_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct fn_zone *fz;
Al Virob6e80c62006-09-26 22:20:01 -0700591 __be32 key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Thomas Graf4e902c52006-08-17 18:14:52 -0700593 if (cfg->fc_dst_len > 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700595
596 if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -ESRCH;
598
599 key = 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700600 if (cfg->fc_dst) {
601 if (cfg->fc_dst & ~FZ_MASK(fz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700603 key = fz_key(cfg->fc_dst, fz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 f = fib_find_node(fz, key);
607
608 if (!f)
609 fa = NULL;
610 else
Thomas Graf4e902c52006-08-17 18:14:52 -0700611 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 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
Thomas Graf4e902c52006-08-17 18:14:52 -0700620 if (fa->fa_tos != cfg->fc_tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622
Thomas Graf4e902c52006-08-17 18:14:52 -0700623 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 fa_to_delete = fa;
631 break;
632 }
633 }
634
635 if (fa_to_delete) {
636 int kill_fn;
637
638 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -0700639 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700640 tb->tb_id, &cfg->fc_nlinfo, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 kill_fn = 0;
Eric Dumazet19f57252010-10-14 20:56:39 +0000643 list_del_rcu(&fa->fa_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (list_empty(&f->fn_alias)) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000645 hlist_del_rcu(&f->fn_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kill_fn = 1;
647 }
648 fib_hash_genid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (fa->fa_state & FA_S_ACCESSED)
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -0700651 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Eric Dumazeta6501e02008-01-18 03:33:26 -0800652 fn_free_alias(fa, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (kill_fn) {
654 fn_free_node(f);
655 fz->fz_nent--;
656 }
657
658 return 0;
659 }
660 return -ESRCH;
661}
662
663static 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)) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000679 list_del_rcu(&fa->fa_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (list_empty(&f->fn_alias)) {
Eric Dumazet19f57252010-10-14 20:56:39 +0000681 hlist_del_rcu(&f->fn_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 kill_f = 1;
683 }
684 fib_hash_genid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Eric Dumazeta6501e02008-01-18 03:33:26 -0800686 fn_free_alias(fa, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 found++;
688 }
689 }
690 if (kill_f) {
691 fn_free_node(f);
692 fz->fz_nent--;
693 }
694 }
695 return found;
696}
697
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000698/* caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000699int fib_table_flush(struct fib_table *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
702 struct fn_zone *fz;
703 int found = 0;
704
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000705 for (fz = rtnl_dereference(table->fn_zone_list);
706 fz != NULL;
707 fz = rtnl_dereference(fz->fz_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 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
717static inline int
718fn_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
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700727 s_i = cb->args[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 i = 0;
Eric Dumazet19f57252010-10-14 20:56:39 +0000729 hlist_for_each_entry_rcu(f, node, head, fn_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct fib_alias *fa;
731
Eric Dumazet19f57252010-10-14 20:56:39 +0000732 list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 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,
Thomas Grafbe403ea2006-08-17 18:15:17 -0700742 f->fn_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 fz->fz_order,
744 fa->fa_tos,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700745 fa->fa_info,
746 NLM_F_MULTI) < 0) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700747 cb->args[4] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -1;
749 }
Eric Dumazet19f57252010-10-14 20:56:39 +0000750next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 i++;
752 }
753 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700754 cb->args[4] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return skb->len;
756}
757
758static inline int
759fn_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
Eric Dumazet8d3f0992008-01-18 04:30:21 -0800765 if (fz->fz_hash == NULL)
766 return skb->len;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700767 s_h = cb->args[3];
Eric Dumazet8d3f0992008-01-18 04:30:21 -0800768 for (h = s_h; h < fz->fz_divisor; h++) {
769 if (hlist_empty(&fz->fz_hash[h]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 continue;
Eric Dumazet8d3f0992008-01-18 04:30:21 -0800771 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700772 cb->args[3] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -1;
774 }
Eric Dumazet8d3f0992008-01-18 04:30:21 -0800775 memset(&cb->args[4], 0,
776 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700778 cb->args[3] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return skb->len;
780}
781
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000782int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
783 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000785 int m = 0, s_m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct fn_zone *fz;
Jianjun Kong6ed25332008-11-03 00:25:16 -0800787 struct fn_hash *table = (struct fn_hash *)tb->tb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700789 s_m = cb->args[2];
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000790 rcu_read_lock();
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000791 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700797 cb->args[2] = m;
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -1;
800 }
Eric Dumazet8d3f0992008-01-18 04:30:21 -0800801 memset(&cb->args[3], 0,
802 sizeof(cb->args) - 3*sizeof(cb->args[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000804 rcu_read_unlock();
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700805 cb->args[2] = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return skb->len;
807}
808
Stephen Hemminger7f9b8052008-01-14 23:14:20 -0800809void __init fib_hash_init(void)
810{
811 fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
Eric Dumazeta6501e02008-01-18 03:33:26 -0800812 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -0800813
814 fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
Eric Dumazeta6501e02008-01-18 03:33:26 -0800815 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -0800816
817}
818
819struct fib_table *fib_hash_table(u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct fib_table *tb;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 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;
Denis V. Lunev971b8932007-12-08 00:32:23 -0800829 tb->tb_default = -1;
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 memset(tb->tb_data, 0, sizeof(struct fn_hash));
832 return tb;
833}
834
835/* ------------------------------------------------------------------------ */
836#ifdef CONFIG_PROC_FS
837
838struct fib_iter_state {
Denis V. Lunev6e04d012008-01-10 03:26:50 -0800839 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 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
850static struct fib_alias *fib_get_first(struct seq_file *seq)
851{
852 struct fib_iter_state *iter = seq->private;
Denis V. Lunev6e04d012008-01-10 03:26:50 -0800853 struct fib_table *main_table;
854 struct fn_hash *table;
855
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900856 main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Denis V. Lunev6e04d012008-01-10 03:26:50 -0800857 table = (struct fn_hash *)main_table->tb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
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
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000867 for (iter->zone = rcu_dereference(table->fn_zone_list);
868 iter->zone != NULL;
869 iter->zone = rcu_dereference(iter->zone->fz_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 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
Jianjun Kong6ed25332008-11-03 00:25:16 -0800883 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct fib_alias *fa;
885
Jianjun Kong6ed25332008-11-03 00:25:16 -0800886 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 iter->fn = fn;
888 iter->fa = fa;
889 goto out;
890 }
891 }
892 }
893 }
894out:
895 return iter->fa;
896}
897
898static 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
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000954 iter->zone = rcu_dereference(iter->zone->fz_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (!iter->zone)
957 goto out;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 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 }
970out:
971 iter->pos++;
972 return fa;
973}
974
975static 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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 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
992static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000993 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 void *v = NULL;
996
Eric Dumazet117a8cd2010-10-14 20:53:34 +0000997 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900998 if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1000 return v;
1001}
1002
1003static 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
1009static void fib_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet117a8cd2010-10-14 20:53:34 +00001010 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Eric Dumazet117a8cd2010-10-14 20:53:34 +00001012 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Al Virob6e80c62006-09-26 22:20:01 -07001015static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001017 static const unsigned type2flags[RTN_MAX + 1] = {
Eric Dumazet19f57252010-10-14 20:56:39 +00001018 [7] = RTF_REJECT,
1019 [8] = RTF_REJECT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 };
1021 unsigned flags = type2flags[type];
1022
1023 if (fi && fi->fib_nh->nh_gw)
1024 flags |= RTF_GATEWAY;
Al Virob6e80c62006-09-26 22:20:01 -07001025 if (mask == htonl(0xFFFFFFFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 flags |= RTF_HOST;
1027 flags |= RTF_UP;
1028 return flags;
1029}
1030
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * 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 */
1037static int fib_seq_show(struct seq_file *seq, void *v)
1038{
1039 struct fib_iter_state *iter;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001040 int len;
Al Virob6e80c62006-09-26 22:20:01 -07001041 __be32 prefix, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 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)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001062 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",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 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,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001068 fi->fib_rtt >> 3, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001070 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, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075out:
1076 return 0;
1077}
1078
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001079static const struct seq_operations fib_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 .start = fib_seq_start,
1081 .next = fib_seq_next,
1082 .stop = fib_seq_stop,
1083 .show = fib_seq_show,
1084};
1085
1086static int fib_seq_open(struct inode *inode, struct file *file)
1087{
Denis V. Lunev6e04d012008-01-10 03:26:50 -08001088 return seq_open_net(inode, file, &fib_seq_ops,
1089 sizeof(struct fib_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Arjan van de Ven9a321442007-02-12 00:55:35 -08001092static const struct file_operations fib_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 .owner = THIS_MODULE,
1094 .open = fib_seq_open,
1095 .read = seq_read,
1096 .llseek = seq_lseek,
Denis V. Lunev6e04d012008-01-10 03:26:50 -08001097 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098};
1099
Denis V. Lunev61a02652008-01-10 03:21:09 -08001100int __net_init fib_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Denis V. Lunev61a02652008-01-10 03:21:09 -08001102 if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
1104 return 0;
1105}
1106
Denis V. Lunev61a02652008-01-10 03:21:09 -08001107void __net_exit fib_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Denis V. Lunev61a02652008-01-10 03:21:09 -08001109 proc_net_remove(net, "route");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111#endif /* CONFIG_PROC_FS */