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