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; |
Adrian Bunk | 94cb150 | 2008-02-19 16:28:54 -0800 | [diff] [blame] | 375 | struct fib_node *new_f = NULL; |
| 376 | struct fib_node *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct fib_alias *fa, *new_fa; |
| 378 | struct fn_zone *fz; |
| 379 | struct fib_info *fi; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 380 | u8 tos = cfg->fc_tos; |
Al Viro | b6e80c6 | 2006-09-26 22:20:01 -0700 | [diff] [blame] | 381 | __be32 key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | int err; |
| 383 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 384 | if (cfg->fc_dst_len > 32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 386 | |
| 387 | fz = table->fn_zones[cfg->fc_dst_len]; |
| 388 | if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return -ENOBUFS; |
| 390 | |
| 391 | key = 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 392 | if (cfg->fc_dst) { |
| 393 | if (cfg->fc_dst & ~FZ_MASK(fz)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 395 | key = fz_key(cfg->fc_dst, fz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 398 | fi = fib_create_info(cfg); |
| 399 | if (IS_ERR(fi)) |
| 400 | return PTR_ERR(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | if (fz->fz_nent > (fz->fz_divisor<<1) && |
| 403 | fz->fz_divisor < FZ_MAX_DIVISOR && |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 404 | (cfg->fc_dst_len == 32 || |
| 405 | (1 << cfg->fc_dst_len) > fz->fz_divisor)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | fn_rehash_zone(fz); |
| 407 | |
| 408 | f = fib_find_node(fz, key); |
| 409 | |
| 410 | if (!f) |
| 411 | fa = NULL; |
| 412 | else |
| 413 | fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority); |
| 414 | |
| 415 | /* Now fa, if non-NULL, points to the first fib alias |
| 416 | * with the same keys [prefix,tos,priority], if such key already |
| 417 | * exists or to the node before which we will insert new one. |
| 418 | * |
| 419 | * If fa is NULL, we will need to allocate a new one and |
| 420 | * insert to the head of f. |
| 421 | * |
| 422 | * If f is NULL, no fib node matched the destination key |
| 423 | * and we need to allocate a new one of those as well. |
| 424 | */ |
| 425 | |
| 426 | if (fa && fa->fa_tos == tos && |
| 427 | fa->fa_info->fib_priority == fi->fib_priority) { |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 428 | struct fib_alias *fa_first, *fa_match; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | err = -EEXIST; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 431 | if (cfg->fc_nlflags & NLM_F_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | goto out; |
| 433 | |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 434 | /* We have 2 goals: |
| 435 | * 1. Find exact match for type, scope, fib_info to avoid |
| 436 | * duplicate routes |
| 437 | * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it |
| 438 | */ |
| 439 | fa_match = NULL; |
| 440 | fa_first = fa; |
| 441 | fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); |
| 442 | list_for_each_entry_continue(fa, &f->fn_alias, fa_list) { |
| 443 | if (fa->fa_tos != tos) |
| 444 | break; |
| 445 | if (fa->fa_info->fib_priority != fi->fib_priority) |
| 446 | break; |
| 447 | if (fa->fa_type == cfg->fc_type && |
| 448 | fa->fa_scope == cfg->fc_scope && |
| 449 | fa->fa_info == fi) { |
| 450 | fa_match = fa; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 455 | if (cfg->fc_nlflags & NLM_F_REPLACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | struct fib_info *fi_drop; |
| 457 | u8 state; |
| 458 | |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 459 | fa = fa_first; |
| 460 | if (fa_match) { |
| 461 | if (fa == fa_match) |
| 462 | err = 0; |
Joonwoo Park | bd566e7 | 2008-01-18 03:44:48 -0800 | [diff] [blame] | 463 | goto out; |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | write_lock_bh(&fib_hash_lock); |
| 466 | fi_drop = fa->fa_info; |
| 467 | fa->fa_info = fi; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 468 | fa->fa_type = cfg->fc_type; |
| 469 | fa->fa_scope = cfg->fc_scope; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | state = fa->fa_state; |
| 471 | fa->fa_state &= ~FA_S_ACCESSED; |
| 472 | fib_hash_genid++; |
| 473 | write_unlock_bh(&fib_hash_lock); |
| 474 | |
| 475 | fib_release_info(fi_drop); |
| 476 | if (state & FA_S_ACCESSED) |
| 477 | rt_cache_flush(-1); |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 478 | rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id, |
| 479 | &cfg->fc_nlinfo, NLM_F_REPLACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* Error if we find a perfect match which |
| 484 | * uses the same scope, type, and nexthop |
| 485 | * information. |
| 486 | */ |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 487 | if (fa_match) |
| 488 | goto out; |
| 489 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 490 | if (!(cfg->fc_nlflags & NLM_F_APPEND)) |
Julian Anastasov | c18865f | 2008-01-28 21:14:10 -0800 | [diff] [blame] | 491 | fa = fa_first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | err = -ENOENT; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 495 | if (!(cfg->fc_nlflags & NLM_F_CREATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | goto out; |
| 497 | |
| 498 | err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (!f) { |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 501 | new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | if (new_f == NULL) |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 503 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | INIT_HLIST_NODE(&new_f->fn_hash); |
| 506 | INIT_LIST_HEAD(&new_f->fn_alias); |
| 507 | new_f->fn_key = key; |
| 508 | f = new_f; |
| 509 | } |
| 510 | |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 511 | new_fa = &f->fn_embedded_alias; |
| 512 | if (new_fa->fa_info != NULL) { |
| 513 | new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL); |
| 514 | if (new_fa == NULL) |
Adrian Bunk | 94cb150 | 2008-02-19 16:28:54 -0800 | [diff] [blame] | 515 | goto out; |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | new_fa->fa_info = fi; |
| 518 | new_fa->fa_tos = tos; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 519 | new_fa->fa_type = cfg->fc_type; |
| 520 | new_fa->fa_scope = cfg->fc_scope; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | new_fa->fa_state = 0; |
| 522 | |
| 523 | /* |
| 524 | * Insert new entry to the list. |
| 525 | */ |
| 526 | |
| 527 | write_lock_bh(&fib_hash_lock); |
| 528 | if (new_f) |
| 529 | fib_insert_node(fz, new_f); |
| 530 | list_add_tail(&new_fa->fa_list, |
| 531 | (fa ? &fa->fa_list : &f->fn_alias)); |
| 532 | fib_hash_genid++; |
| 533 | write_unlock_bh(&fib_hash_lock); |
| 534 | |
| 535 | if (new_f) |
| 536 | fz->fz_nent++; |
| 537 | rt_cache_flush(-1); |
| 538 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 539 | 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] | 540 | &cfg->fc_nlinfo, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return 0; |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | out: |
Adrian Bunk | 94cb150 | 2008-02-19 16:28:54 -0800 | [diff] [blame] | 544 | if (new_f) |
| 545 | kmem_cache_free(fn_hash_kmem, new_f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | fib_release_info(fi); |
| 547 | return err; |
| 548 | } |
| 549 | |
| 550 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 551 | 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] | 552 | { |
| 553 | struct fn_hash *table = (struct fn_hash*)tb->tb_data; |
| 554 | struct fib_node *f; |
| 555 | struct fib_alias *fa, *fa_to_delete; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | struct fn_zone *fz; |
Al Viro | b6e80c6 | 2006-09-26 22:20:01 -0700 | [diff] [blame] | 557 | __be32 key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 559 | if (cfg->fc_dst_len > 32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 561 | |
| 562 | if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return -ESRCH; |
| 564 | |
| 565 | key = 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 566 | if (cfg->fc_dst) { |
| 567 | if (cfg->fc_dst & ~FZ_MASK(fz)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 569 | key = fz_key(cfg->fc_dst, fz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | f = fib_find_node(fz, key); |
| 573 | |
| 574 | if (!f) |
| 575 | fa = NULL; |
| 576 | else |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 577 | fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (!fa) |
| 579 | return -ESRCH; |
| 580 | |
| 581 | fa_to_delete = NULL; |
| 582 | fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); |
| 583 | list_for_each_entry_continue(fa, &f->fn_alias, fa_list) { |
| 584 | struct fib_info *fi = fa->fa_info; |
| 585 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 586 | if (fa->fa_tos != cfg->fc_tos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | break; |
| 588 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 589 | if ((!cfg->fc_type || |
| 590 | fa->fa_type == cfg->fc_type) && |
| 591 | (cfg->fc_scope == RT_SCOPE_NOWHERE || |
| 592 | fa->fa_scope == cfg->fc_scope) && |
| 593 | (!cfg->fc_protocol || |
| 594 | fi->fib_protocol == cfg->fc_protocol) && |
| 595 | fib_nh_match(cfg, fi) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | fa_to_delete = fa; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (fa_to_delete) { |
| 602 | int kill_fn; |
| 603 | |
| 604 | fa = fa_to_delete; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 605 | rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 606 | tb->tb_id, &cfg->fc_nlinfo, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | kill_fn = 0; |
| 609 | write_lock_bh(&fib_hash_lock); |
| 610 | list_del(&fa->fa_list); |
| 611 | if (list_empty(&f->fn_alias)) { |
| 612 | hlist_del(&f->fn_hash); |
| 613 | kill_fn = 1; |
| 614 | } |
| 615 | fib_hash_genid++; |
| 616 | write_unlock_bh(&fib_hash_lock); |
| 617 | |
| 618 | if (fa->fa_state & FA_S_ACCESSED) |
| 619 | rt_cache_flush(-1); |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 620 | fn_free_alias(fa, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (kill_fn) { |
| 622 | fn_free_node(f); |
| 623 | fz->fz_nent--; |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | return -ESRCH; |
| 629 | } |
| 630 | |
| 631 | static int fn_flush_list(struct fn_zone *fz, int idx) |
| 632 | { |
| 633 | struct hlist_head *head = &fz->fz_hash[idx]; |
| 634 | struct hlist_node *node, *n; |
| 635 | struct fib_node *f; |
| 636 | int found = 0; |
| 637 | |
| 638 | hlist_for_each_entry_safe(f, node, n, head, fn_hash) { |
| 639 | struct fib_alias *fa, *fa_node; |
| 640 | int kill_f; |
| 641 | |
| 642 | kill_f = 0; |
| 643 | list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) { |
| 644 | struct fib_info *fi = fa->fa_info; |
| 645 | |
| 646 | if (fi && (fi->fib_flags&RTNH_F_DEAD)) { |
| 647 | write_lock_bh(&fib_hash_lock); |
| 648 | list_del(&fa->fa_list); |
| 649 | if (list_empty(&f->fn_alias)) { |
| 650 | hlist_del(&f->fn_hash); |
| 651 | kill_f = 1; |
| 652 | } |
| 653 | fib_hash_genid++; |
| 654 | write_unlock_bh(&fib_hash_lock); |
| 655 | |
Eric Dumazet | a6501e0 | 2008-01-18 03:33:26 -0800 | [diff] [blame] | 656 | fn_free_alias(fa, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | found++; |
| 658 | } |
| 659 | } |
| 660 | if (kill_f) { |
| 661 | fn_free_node(f); |
| 662 | fz->fz_nent--; |
| 663 | } |
| 664 | } |
| 665 | return found; |
| 666 | } |
| 667 | |
| 668 | static int fn_hash_flush(struct fib_table *tb) |
| 669 | { |
| 670 | struct fn_hash *table = (struct fn_hash *) tb->tb_data; |
| 671 | struct fn_zone *fz; |
| 672 | int found = 0; |
| 673 | |
| 674 | for (fz = table->fn_zone_list; fz; fz = fz->fz_next) { |
| 675 | int i; |
| 676 | |
| 677 | for (i = fz->fz_divisor - 1; i >= 0; i--) |
| 678 | found += fn_flush_list(fz, i); |
| 679 | } |
| 680 | return found; |
| 681 | } |
| 682 | |
| 683 | |
| 684 | static inline int |
| 685 | fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb, |
| 686 | struct fib_table *tb, |
| 687 | struct fn_zone *fz, |
| 688 | struct hlist_head *head) |
| 689 | { |
| 690 | struct hlist_node *node; |
| 691 | struct fib_node *f; |
| 692 | int i, s_i; |
| 693 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 694 | s_i = cb->args[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | i = 0; |
| 696 | hlist_for_each_entry(f, node, head, fn_hash) { |
| 697 | struct fib_alias *fa; |
| 698 | |
| 699 | list_for_each_entry(fa, &f->fn_alias, fa_list) { |
| 700 | if (i < s_i) |
| 701 | goto next; |
| 702 | |
| 703 | if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid, |
| 704 | cb->nlh->nlmsg_seq, |
| 705 | RTM_NEWROUTE, |
| 706 | tb->tb_id, |
| 707 | fa->fa_type, |
| 708 | fa->fa_scope, |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 709 | f->fn_key, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | fz->fz_order, |
| 711 | fa->fa_tos, |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 712 | fa->fa_info, |
| 713 | NLM_F_MULTI) < 0) { |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 714 | cb->args[4] = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return -1; |
| 716 | } |
| 717 | next: |
| 718 | i++; |
| 719 | } |
| 720 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 721 | cb->args[4] = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return skb->len; |
| 723 | } |
| 724 | |
| 725 | static inline int |
| 726 | fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb, |
| 727 | struct fib_table *tb, |
| 728 | struct fn_zone *fz) |
| 729 | { |
| 730 | int h, s_h; |
| 731 | |
Eric Dumazet | 8d3f099 | 2008-01-18 04:30:21 -0800 | [diff] [blame] | 732 | if (fz->fz_hash == NULL) |
| 733 | return skb->len; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 734 | s_h = cb->args[3]; |
Eric Dumazet | 8d3f099 | 2008-01-18 04:30:21 -0800 | [diff] [blame] | 735 | for (h = s_h; h < fz->fz_divisor; h++) { |
| 736 | if (hlist_empty(&fz->fz_hash[h])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | continue; |
Eric Dumazet | 8d3f099 | 2008-01-18 04:30:21 -0800 | [diff] [blame] | 738 | 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] | 739 | cb->args[3] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | return -1; |
| 741 | } |
Eric Dumazet | 8d3f099 | 2008-01-18 04:30:21 -0800 | [diff] [blame] | 742 | memset(&cb->args[4], 0, |
| 743 | sizeof(cb->args) - 4*sizeof(cb->args[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 745 | cb->args[3] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | return skb->len; |
| 747 | } |
| 748 | |
| 749 | static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb) |
| 750 | { |
| 751 | int m, s_m; |
| 752 | struct fn_zone *fz; |
| 753 | struct fn_hash *table = (struct fn_hash*)tb->tb_data; |
| 754 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 755 | s_m = cb->args[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | read_lock(&fib_hash_lock); |
| 757 | for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) { |
| 758 | if (m < s_m) continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) { |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 760 | cb->args[2] = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | read_unlock(&fib_hash_lock); |
| 762 | return -1; |
| 763 | } |
Eric Dumazet | 8d3f099 | 2008-01-18 04:30:21 -0800 | [diff] [blame] | 764 | memset(&cb->args[3], 0, |
| 765 | sizeof(cb->args) - 3*sizeof(cb->args[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | read_unlock(&fib_hash_lock); |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 768 | cb->args[2] = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return skb->len; |
| 770 | } |
| 771 | |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 772 | void __init fib_hash_init(void) |
| 773 | { |
| 774 | 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] | 775 | 0, SLAB_PANIC, NULL); |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 776 | |
| 777 | 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] | 778 | 0, SLAB_PANIC, NULL); |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 779 | |
| 780 | } |
| 781 | |
| 782 | struct fib_table *fib_hash_table(u32 id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
| 784 | struct fib_table *tb; |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash), |
| 787 | GFP_KERNEL); |
| 788 | if (tb == NULL) |
| 789 | return NULL; |
| 790 | |
| 791 | tb->tb_id = id; |
Denis V. Lunev | 971b893 | 2007-12-08 00:32:23 -0800 | [diff] [blame] | 792 | tb->tb_default = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | tb->tb_lookup = fn_hash_lookup; |
| 794 | tb->tb_insert = fn_hash_insert; |
| 795 | tb->tb_delete = fn_hash_delete; |
| 796 | tb->tb_flush = fn_hash_flush; |
| 797 | tb->tb_select_default = fn_hash_select_default; |
| 798 | tb->tb_dump = fn_hash_dump; |
| 799 | memset(tb->tb_data, 0, sizeof(struct fn_hash)); |
| 800 | return tb; |
| 801 | } |
| 802 | |
| 803 | /* ------------------------------------------------------------------------ */ |
| 804 | #ifdef CONFIG_PROC_FS |
| 805 | |
| 806 | struct fib_iter_state { |
Denis V. Lunev | 6e04d01 | 2008-01-10 03:26:50 -0800 | [diff] [blame] | 807 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | struct fn_zone *zone; |
| 809 | int bucket; |
| 810 | struct hlist_head *hash_head; |
| 811 | struct fib_node *fn; |
| 812 | struct fib_alias *fa; |
| 813 | loff_t pos; |
| 814 | unsigned int genid; |
| 815 | int valid; |
| 816 | }; |
| 817 | |
| 818 | static struct fib_alias *fib_get_first(struct seq_file *seq) |
| 819 | { |
| 820 | struct fib_iter_state *iter = seq->private; |
Denis V. Lunev | 6e04d01 | 2008-01-10 03:26:50 -0800 | [diff] [blame] | 821 | struct fib_table *main_table; |
| 822 | struct fn_hash *table; |
| 823 | |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 824 | main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN); |
Denis V. Lunev | 6e04d01 | 2008-01-10 03:26:50 -0800 | [diff] [blame] | 825 | table = (struct fn_hash *)main_table->tb_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
| 827 | iter->bucket = 0; |
| 828 | iter->hash_head = NULL; |
| 829 | iter->fn = NULL; |
| 830 | iter->fa = NULL; |
| 831 | iter->pos = 0; |
| 832 | iter->genid = fib_hash_genid; |
| 833 | iter->valid = 1; |
| 834 | |
| 835 | for (iter->zone = table->fn_zone_list; iter->zone; |
| 836 | iter->zone = iter->zone->fz_next) { |
| 837 | int maxslot; |
| 838 | |
| 839 | if (!iter->zone->fz_nent) |
| 840 | continue; |
| 841 | |
| 842 | iter->hash_head = iter->zone->fz_hash; |
| 843 | maxslot = iter->zone->fz_divisor; |
| 844 | |
| 845 | for (iter->bucket = 0; iter->bucket < maxslot; |
| 846 | ++iter->bucket, ++iter->hash_head) { |
| 847 | struct hlist_node *node; |
| 848 | struct fib_node *fn; |
| 849 | |
| 850 | hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) { |
| 851 | struct fib_alias *fa; |
| 852 | |
| 853 | list_for_each_entry(fa,&fn->fn_alias,fa_list) { |
| 854 | iter->fn = fn; |
| 855 | iter->fa = fa; |
| 856 | goto out; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | out: |
| 862 | return iter->fa; |
| 863 | } |
| 864 | |
| 865 | static struct fib_alias *fib_get_next(struct seq_file *seq) |
| 866 | { |
| 867 | struct fib_iter_state *iter = seq->private; |
| 868 | struct fib_node *fn; |
| 869 | struct fib_alias *fa; |
| 870 | |
| 871 | /* Advance FA, if any. */ |
| 872 | fn = iter->fn; |
| 873 | fa = iter->fa; |
| 874 | if (fa) { |
| 875 | BUG_ON(!fn); |
| 876 | list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) { |
| 877 | iter->fa = fa; |
| 878 | goto out; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | fa = iter->fa = NULL; |
| 883 | |
| 884 | /* Advance FN. */ |
| 885 | if (fn) { |
| 886 | struct hlist_node *node = &fn->fn_hash; |
| 887 | hlist_for_each_entry_continue(fn, node, fn_hash) { |
| 888 | iter->fn = fn; |
| 889 | |
| 890 | list_for_each_entry(fa, &fn->fn_alias, fa_list) { |
| 891 | iter->fa = fa; |
| 892 | goto out; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | fn = iter->fn = NULL; |
| 898 | |
| 899 | /* Advance hash chain. */ |
| 900 | if (!iter->zone) |
| 901 | goto out; |
| 902 | |
| 903 | for (;;) { |
| 904 | struct hlist_node *node; |
| 905 | int maxslot; |
| 906 | |
| 907 | maxslot = iter->zone->fz_divisor; |
| 908 | |
| 909 | while (++iter->bucket < maxslot) { |
| 910 | iter->hash_head++; |
| 911 | |
| 912 | hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) { |
| 913 | list_for_each_entry(fa, &fn->fn_alias, fa_list) { |
| 914 | iter->fn = fn; |
| 915 | iter->fa = fa; |
| 916 | goto out; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | iter->zone = iter->zone->fz_next; |
| 922 | |
| 923 | if (!iter->zone) |
| 924 | goto out; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | iter->bucket = 0; |
| 927 | iter->hash_head = iter->zone->fz_hash; |
| 928 | |
| 929 | hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) { |
| 930 | list_for_each_entry(fa, &fn->fn_alias, fa_list) { |
| 931 | iter->fn = fn; |
| 932 | iter->fa = fa; |
| 933 | goto out; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | out: |
| 938 | iter->pos++; |
| 939 | return fa; |
| 940 | } |
| 941 | |
| 942 | static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos) |
| 943 | { |
| 944 | struct fib_iter_state *iter = seq->private; |
| 945 | struct fib_alias *fa; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) { |
| 948 | fa = iter->fa; |
| 949 | pos -= iter->pos; |
| 950 | } else |
| 951 | fa = fib_get_first(seq); |
| 952 | |
| 953 | if (fa) |
| 954 | while (pos && (fa = fib_get_next(seq))) |
| 955 | --pos; |
| 956 | return pos ? NULL : fa; |
| 957 | } |
| 958 | |
| 959 | static void *fib_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 960 | __acquires(fib_hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
| 962 | void *v = NULL; |
| 963 | |
| 964 | read_lock(&fib_hash_lock); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 965 | if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 967 | return v; |
| 968 | } |
| 969 | |
| 970 | static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 971 | { |
| 972 | ++*pos; |
| 973 | return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq); |
| 974 | } |
| 975 | |
| 976 | static void fib_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 977 | __releases(fib_hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | { |
| 979 | read_unlock(&fib_hash_lock); |
| 980 | } |
| 981 | |
Al Viro | b6e80c6 | 2006-09-26 22:20:01 -0700 | [diff] [blame] | 982 | 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] | 983 | { |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 984 | static const unsigned type2flags[RTN_MAX + 1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | [7] = RTF_REJECT, [8] = RTF_REJECT, |
| 986 | }; |
| 987 | unsigned flags = type2flags[type]; |
| 988 | |
| 989 | if (fi && fi->fib_nh->nh_gw) |
| 990 | flags |= RTF_GATEWAY; |
Al Viro | b6e80c6 | 2006-09-26 22:20:01 -0700 | [diff] [blame] | 991 | if (mask == htonl(0xFFFFFFFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | flags |= RTF_HOST; |
| 993 | flags |= RTF_UP; |
| 994 | return flags; |
| 995 | } |
| 996 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 997 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | * This outputs /proc/net/route. |
| 999 | * |
| 1000 | * It always works in backward compatibility mode. |
| 1001 | * The format of the file is not supposed to be changed. |
| 1002 | */ |
| 1003 | static int fib_seq_show(struct seq_file *seq, void *v) |
| 1004 | { |
| 1005 | struct fib_iter_state *iter; |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1006 | int len; |
Al Viro | b6e80c6 | 2006-09-26 22:20:01 -0700 | [diff] [blame] | 1007 | __be32 prefix, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | unsigned flags; |
| 1009 | struct fib_node *f; |
| 1010 | struct fib_alias *fa; |
| 1011 | struct fib_info *fi; |
| 1012 | |
| 1013 | if (v == SEQ_START_TOKEN) { |
| 1014 | seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway " |
| 1015 | "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU" |
| 1016 | "\tWindow\tIRTT"); |
| 1017 | goto out; |
| 1018 | } |
| 1019 | |
| 1020 | iter = seq->private; |
| 1021 | f = iter->fn; |
| 1022 | fa = iter->fa; |
| 1023 | fi = fa->fa_info; |
| 1024 | prefix = f->fn_key; |
| 1025 | mask = FZ_MASK(iter->zone); |
| 1026 | flags = fib_flag_trans(fa->fa_type, mask, fi); |
| 1027 | if (fi) |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1028 | seq_printf(seq, |
| 1029 | "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | fi->fib_dev ? fi->fib_dev->name : "*", prefix, |
| 1031 | fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority, |
| 1032 | mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0), |
| 1033 | fi->fib_window, |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1034 | fi->fib_rtt >> 3, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | else |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1036 | seq_printf(seq, |
| 1037 | "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n", |
| 1038 | prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len); |
| 1039 | |
| 1040 | seq_printf(seq, "%*s\n", 127 - len, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | out: |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 1045 | static const struct seq_operations fib_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | .start = fib_seq_start, |
| 1047 | .next = fib_seq_next, |
| 1048 | .stop = fib_seq_stop, |
| 1049 | .show = fib_seq_show, |
| 1050 | }; |
| 1051 | |
| 1052 | static int fib_seq_open(struct inode *inode, struct file *file) |
| 1053 | { |
Denis V. Lunev | 6e04d01 | 2008-01-10 03:26:50 -0800 | [diff] [blame] | 1054 | return seq_open_net(inode, file, &fib_seq_ops, |
| 1055 | sizeof(struct fib_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1058 | static const struct file_operations fib_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | .owner = THIS_MODULE, |
| 1060 | .open = fib_seq_open, |
| 1061 | .read = seq_read, |
| 1062 | .llseek = seq_lseek, |
Denis V. Lunev | 6e04d01 | 2008-01-10 03:26:50 -0800 | [diff] [blame] | 1063 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | }; |
| 1065 | |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 1066 | int __net_init fib_proc_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 1068 | if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return -ENOMEM; |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 1073 | void __net_exit fib_proc_exit(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | { |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 1075 | proc_net_remove(net, "route"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | #endif /* CONFIG_PROC_FS */ |